Use std::string in dwarf2read.c
[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
TT
3172
3173 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
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);
3219 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3220 SECT_OFF_TEXT (objfile));
3221
3222 auto_obstack temp_obstack;
3223 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3224
3225 std::unordered_map<sect_offset,
3226 dwarf2_per_cu_data *,
3227 gdb::hash_enum<sect_offset>>
3228 debug_info_offset_to_per_cu;
b76e467d 3229 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3230 {
927aa2e7
JK
3231 const auto insertpair
3232 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3233 if (!insertpair.second)
3234 {
3235 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3236 "debug_info_offset %s, ignoring .debug_aranges."),
3237 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3238 return;
3239 }
3240 }
3241
3242 dwarf2_read_section (objfile, section);
3243
3244 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3245
3246 const gdb_byte *addr = section->buffer;
3247
3248 while (addr < section->buffer + section->size)
3249 {
3250 const gdb_byte *const entry_addr = addr;
3251 unsigned int bytes_read;
3252
3253 const LONGEST entry_length = read_initial_length (abfd, addr,
3254 &bytes_read);
3255 addr += bytes_read;
3256
3257 const gdb_byte *const entry_end = addr + entry_length;
3258 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3259 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3260 if (addr + entry_length > section->buffer + section->size)
3261 {
47e3f474 3262 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3263 "length %s exceeds section length %s, "
3264 "ignoring .debug_aranges."),
47e3f474
TV
3265 objfile_name (objfile),
3266 plongest (entry_addr - section->buffer),
927aa2e7
JK
3267 plongest (bytes_read + entry_length),
3268 pulongest (section->size));
3269 return;
3270 }
3271
3272 /* The version number. */
3273 const uint16_t version = read_2_bytes (abfd, addr);
3274 addr += 2;
3275 if (version != 2)
3276 {
47e3f474 3277 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3278 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3279 objfile_name (objfile),
3280 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3281 return;
3282 }
3283
3284 const uint64_t debug_info_offset
3285 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3286 addr += offset_size;
3287 const auto per_cu_it
3288 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3289 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3290 {
47e3f474 3291 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3292 "debug_info_offset %s does not exists, "
3293 "ignoring .debug_aranges."),
47e3f474
TV
3294 objfile_name (objfile),
3295 plongest (entry_addr - section->buffer),
927aa2e7
JK
3296 pulongest (debug_info_offset));
3297 return;
3298 }
3299 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3300
3301 const uint8_t address_size = *addr++;
3302 if (address_size < 1 || address_size > 8)
3303 {
47e3f474 3304 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3305 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3306 objfile_name (objfile),
3307 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3308 return;
3309 }
3310
3311 const uint8_t segment_selector_size = *addr++;
3312 if (segment_selector_size != 0)
3313 {
47e3f474 3314 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3315 "segment_selector_size %u is not supported, "
3316 "ignoring .debug_aranges."),
47e3f474
TV
3317 objfile_name (objfile),
3318 plongest (entry_addr - section->buffer),
927aa2e7
JK
3319 segment_selector_size);
3320 return;
3321 }
3322
3323 /* Must pad to an alignment boundary that is twice the address
3324 size. It is undocumented by the DWARF standard but GCC does
3325 use it. */
3326 for (size_t padding = ((-(addr - section->buffer))
3327 & (2 * address_size - 1));
3328 padding > 0; padding--)
3329 if (*addr++ != 0)
3330 {
47e3f474 3331 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3332 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3333 objfile_name (objfile),
3334 plongest (entry_addr - section->buffer));
927aa2e7
JK
3335 return;
3336 }
3337
3338 for (;;)
3339 {
3340 if (addr + 2 * address_size > entry_end)
3341 {
47e3f474 3342 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3343 "address list is not properly terminated, "
3344 "ignoring .debug_aranges."),
47e3f474
TV
3345 objfile_name (objfile),
3346 plongest (entry_addr - section->buffer));
927aa2e7
JK
3347 return;
3348 }
3349 ULONGEST start = extract_unsigned_integer (addr, address_size,
3350 dwarf5_byte_order);
3351 addr += address_size;
3352 ULONGEST length = extract_unsigned_integer (addr, address_size,
3353 dwarf5_byte_order);
3354 addr += address_size;
3355 if (start == 0 && length == 0)
3356 break;
3357 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3358 {
3359 /* Symbol was eliminated due to a COMDAT group. */
3360 continue;
3361 }
3362 ULONGEST end = start + length;
79748972
TT
3363 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3364 - baseaddr);
3365 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3366 - baseaddr);
927aa2e7
JK
3367 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3368 }
3369 }
3370
d320c2b5 3371 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3372 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3373}
3374
9291a0cd
TT
3375/* Find a slot in the mapped index INDEX for the object named NAME.
3376 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3377 constant pool and return true. If NAME cannot be found, return
3378 false. */
2fdf6df6 3379
109483d9 3380static bool
9291a0cd
TT
3381find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3382 offset_type **vec_out)
3383{
0cf03b49 3384 offset_type hash;
9291a0cd 3385 offset_type slot, step;
559a7a62 3386 int (*cmp) (const char *, const char *);
9291a0cd 3387
791afaa2 3388 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3389 if (current_language->la_language == language_cplus
45280282
IB
3390 || current_language->la_language == language_fortran
3391 || current_language->la_language == language_d)
0cf03b49
JK
3392 {
3393 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3394 not contain any. */
a8719064 3395
72998fb3 3396 if (strchr (name, '(') != NULL)
0cf03b49 3397 {
109483d9 3398 without_params = cp_remove_params (name);
0cf03b49 3399
72998fb3 3400 if (without_params != NULL)
791afaa2 3401 name = without_params.get ();
0cf03b49
JK
3402 }
3403 }
3404
559a7a62 3405 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3406 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3407 simulate our NAME being searched is also lowercased. */
3408 hash = mapped_index_string_hash ((index->version == 4
3409 && case_sensitivity == case_sensitive_off
3410 ? 5 : index->version),
3411 name);
3412
f00a2de2
PA
3413 slot = hash & (index->symbol_table.size () - 1);
3414 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3415 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3416
3417 for (;;)
3418 {
9291a0cd 3419 const char *str;
f00a2de2
PA
3420
3421 const auto &bucket = index->symbol_table[slot];
3422 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3423 return false;
9291a0cd 3424
f00a2de2 3425 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3426 if (!cmp (name, str))
9291a0cd
TT
3427 {
3428 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3429 + MAYBE_SWAP (bucket.vec));
109483d9 3430 return true;
9291a0cd
TT
3431 }
3432
f00a2de2 3433 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3434 }
3435}
3436
4485a1c1
SM
3437/* A helper function that reads the .gdb_index from BUFFER and fills
3438 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3439 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3440 ok to use deprecated sections.
3441
3442 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3443 out parameters that are filled in with information about the CU and
3444 TU lists in the section.
3445
4485a1c1 3446 Returns true if all went well, false otherwise. */
2fdf6df6 3447
d33bc52e 3448static bool
4485a1c1
SM
3449read_gdb_index_from_buffer (struct objfile *objfile,
3450 const char *filename,
3451 bool deprecated_ok,
3452 gdb::array_view<const gdb_byte> buffer,
3453 struct mapped_index *map,
3454 const gdb_byte **cu_list,
3455 offset_type *cu_list_elements,
3456 const gdb_byte **types_list,
3457 offset_type *types_list_elements)
3458{
3459 const gdb_byte *addr = &buffer[0];
82430852 3460
9291a0cd 3461 /* Version check. */
4485a1c1 3462 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3463 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3464 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3465 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3466 indices. */
831adc1f 3467 if (version < 4)
481860b3
GB
3468 {
3469 static int warning_printed = 0;
3470 if (!warning_printed)
3471 {
3472 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3473 filename);
481860b3
GB
3474 warning_printed = 1;
3475 }
3476 return 0;
3477 }
3478 /* Index version 4 uses a different hash function than index version
3479 5 and later.
3480
3481 Versions earlier than 6 did not emit psymbols for inlined
3482 functions. Using these files will cause GDB not to be able to
3483 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3484 indices unless the user has done
3485 "set use-deprecated-index-sections on". */
2ec9a5e0 3486 if (version < 6 && !deprecated_ok)
481860b3
GB
3487 {
3488 static int warning_printed = 0;
3489 if (!warning_printed)
3490 {
e615022a
DE
3491 warning (_("\
3492Skipping deprecated .gdb_index section in %s.\n\
3493Do \"set use-deprecated-index-sections on\" before the file is read\n\
3494to use the section anyway."),
2ec9a5e0 3495 filename);
481860b3
GB
3496 warning_printed = 1;
3497 }
3498 return 0;
3499 }
796a7ff8 3500 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3501 of the TU (for symbols coming from TUs),
3502 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3503 Plus gold-generated indices can have duplicate entries for global symbols,
3504 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3505 These are just performance bugs, and we can't distinguish gdb-generated
3506 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3507
481860b3 3508 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3509 longer backward compatible. */
796a7ff8 3510 if (version > 8)
594e8718 3511 return 0;
9291a0cd 3512
559a7a62 3513 map->version = version;
9291a0cd 3514
4485a1c1 3515 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3516
4485a1c1 3517 int i = 0;
2ec9a5e0
TT
3518 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3519 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3520 / 8);
1fd400ff
TT
3521 ++i;
3522
2ec9a5e0
TT
3523 *types_list = addr + MAYBE_SWAP (metadata[i]);
3524 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3525 - MAYBE_SWAP (metadata[i]))
3526 / 8);
987d643c 3527 ++i;
1fd400ff 3528
f00a2de2
PA
3529 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3530 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3531 map->address_table
3532 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3533 ++i;
3534
f00a2de2
PA
3535 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3536 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3537 map->symbol_table
3538 = gdb::array_view<mapped_index::symbol_table_slot>
3539 ((mapped_index::symbol_table_slot *) symbol_table,
3540 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3541
f00a2de2 3542 ++i;
f9d83a0b 3543 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3544
2ec9a5e0
TT
3545 return 1;
3546}
3547
4485a1c1
SM
3548/* Callback types for dwarf2_read_gdb_index. */
3549
3550typedef gdb::function_view
3551 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3552 get_gdb_index_contents_ftype;
3553typedef gdb::function_view
3554 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3555 get_gdb_index_contents_dwz_ftype;
3556
927aa2e7 3557/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3558 elements of all the CUs and return 1. Otherwise, return 0. */
3559
3560static int
4485a1c1
SM
3561dwarf2_read_gdb_index
3562 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3563 get_gdb_index_contents_ftype get_gdb_index_contents,
3564 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3565{
2ec9a5e0
TT
3566 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3567 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3568 struct dwz_file *dwz;
12359b5e 3569 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3570
4485a1c1
SM
3571 gdb::array_view<const gdb_byte> main_index_contents
3572 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3573
3574 if (main_index_contents.empty ())
3575 return 0;
3576
3063847f 3577 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3578 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3579 use_deprecated_index_sections,
3580 main_index_contents, map.get (), &cu_list,
3581 &cu_list_elements, &types_list,
3582 &types_list_elements))
2ec9a5e0
TT
3583 return 0;
3584
0fefef59 3585 /* Don't use the index if it's empty. */
3063847f 3586 if (map->symbol_table.empty ())
0fefef59
DE
3587 return 0;
3588
2ec9a5e0
TT
3589 /* If there is a .dwz file, read it so we can get its CU list as
3590 well. */
ed2dc618 3591 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3592 if (dwz != NULL)
2ec9a5e0 3593 {
2ec9a5e0
TT
3594 struct mapped_index dwz_map;
3595 const gdb_byte *dwz_types_ignore;
3596 offset_type dwz_types_elements_ignore;
3597
4485a1c1
SM
3598 gdb::array_view<const gdb_byte> dwz_index_content
3599 = get_gdb_index_contents_dwz (objfile, dwz);
3600
3601 if (dwz_index_content.empty ())
3602 return 0;
3603
3604 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3605 bfd_get_filename (dwz->dwz_bfd.get ()),
3606 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3607 &dwz_list, &dwz_list_elements,
3608 &dwz_types_ignore,
3609 &dwz_types_elements_ignore))
2ec9a5e0
TT
3610 {
3611 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3612 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3613 return 0;
3614 }
3615 }
3616
12359b5e
SM
3617 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3618 dwz_list, dwz_list_elements);
1fd400ff 3619
8b70b953
TT
3620 if (types_list_elements)
3621 {
8b70b953
TT
3622 /* We can only handle a single .debug_types when we have an
3623 index. */
fd5866f6 3624 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3625 return 0;
3626
fd5866f6 3627 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3628
12359b5e
SM
3629 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3630 types_list, types_list_elements);
8b70b953 3631 }
9291a0cd 3632
3063847f 3633 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3634
3063847f 3635 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3636 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3637 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3638 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3639
3640 return 1;
3641}
3642
dee91e82 3643/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3644
dee91e82
DE
3645static void
3646dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3647 const gdb_byte *info_ptr,
dee91e82
DE
3648 struct die_info *comp_unit_die,
3649 int has_children,
3650 void *data)
9291a0cd 3651{
dee91e82 3652 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3653 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3654 struct dwarf2_per_objfile *dwarf2_per_objfile
3655 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3656 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3657 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3658 struct attribute *attr;
7b9f3c50
DE
3659 void **slot;
3660 struct quick_file_names *qfn;
9291a0cd 3661
0186c6a7
DE
3662 gdb_assert (! this_cu->is_debug_types);
3663
07261596
TT
3664 /* Our callers never want to match partial units -- instead they
3665 will match the enclosing full CU. */
3666 if (comp_unit_die->tag == DW_TAG_partial_unit)
3667 {
3668 this_cu->v.quick->no_file_data = 1;
3669 return;
3670 }
3671
0186c6a7 3672 lh_cu = this_cu;
7b9f3c50 3673 slot = NULL;
dee91e82 3674
fff8551c 3675 line_header_up lh;
9c541725 3676 sect_offset line_offset {};
fff8551c 3677
dee91e82 3678 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3679 if (attr != nullptr)
9291a0cd 3680 {
7b9f3c50
DE
3681 struct quick_file_names find_entry;
3682
9c541725 3683 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3684
3685 /* We may have already read in this line header (TU line header sharing).
3686 If we have we're done. */
094b34ac 3687 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3688 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3689 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3690 &find_entry, INSERT);
3691 if (*slot != NULL)
3692 {
9a3c8263 3693 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3694 return;
7b9f3c50
DE
3695 }
3696
3019eac3 3697 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3698 }
3699 if (lh == NULL)
3700 {
094b34ac 3701 lh_cu->v.quick->no_file_data = 1;
dee91e82 3702 return;
9291a0cd
TT
3703 }
3704
8d749320 3705 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3706 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3707 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3708 gdb_assert (slot != NULL);
3709 *slot = qfn;
9291a0cd 3710
d721ba37 3711 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3712
aa391654
TT
3713 int offset = 0;
3714 if (strcmp (fnd.name, "<unknown>") != 0)
3715 ++offset;
3716
7ba99d21 3717 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3718 qfn->file_names =
aa391654
TT
3719 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3720 if (offset != 0)
3721 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3722 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3723 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3724 qfn->real_names = NULL;
9291a0cd 3725
094b34ac 3726 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3727}
3728
3729/* A helper for the "quick" functions which attempts to read the line
3730 table for THIS_CU. */
3731
3732static struct quick_file_names *
e4a48d9d 3733dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3734{
0186c6a7
DE
3735 /* This should never be called for TUs. */
3736 gdb_assert (! this_cu->is_debug_types);
3737 /* Nor type unit groups. */
3738 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3739
dee91e82
DE
3740 if (this_cu->v.quick->file_names != NULL)
3741 return this_cu->v.quick->file_names;
3742 /* If we know there is no line data, no point in looking again. */
3743 if (this_cu->v.quick->no_file_data)
3744 return NULL;
3745
0186c6a7 3746 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3747
3748 if (this_cu->v.quick->no_file_data)
3749 return NULL;
3750 return this_cu->v.quick->file_names;
9291a0cd
TT
3751}
3752
3753/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3754 real path for a given file name from the line table. */
2fdf6df6 3755
9291a0cd 3756static const char *
7b9f3c50
DE
3757dw2_get_real_path (struct objfile *objfile,
3758 struct quick_file_names *qfn, int index)
9291a0cd 3759{
7b9f3c50
DE
3760 if (qfn->real_names == NULL)
3761 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3762 qfn->num_file_names, const char *);
9291a0cd 3763
7b9f3c50 3764 if (qfn->real_names[index] == NULL)
14278e1f 3765 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3766
7b9f3c50 3767 return qfn->real_names[index];
9291a0cd
TT
3768}
3769
3770static struct symtab *
3771dw2_find_last_source_symtab (struct objfile *objfile)
3772{
ed2dc618
SM
3773 struct dwarf2_per_objfile *dwarf2_per_objfile
3774 = get_dwarf2_per_objfile (objfile);
b76e467d 3775 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3776 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3777
43f3e411
DE
3778 if (cust == NULL)
3779 return NULL;
ed2dc618 3780
43f3e411 3781 return compunit_primary_filetab (cust);
9291a0cd
TT
3782}
3783
7b9f3c50
DE
3784/* Traversal function for dw2_forget_cached_source_info. */
3785
3786static int
3787dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3788{
7b9f3c50 3789 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3790
7b9f3c50 3791 if (file_data->real_names)
9291a0cd 3792 {
7b9f3c50 3793 int i;
9291a0cd 3794
7b9f3c50 3795 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3796 {
7b9f3c50
DE
3797 xfree ((void*) file_data->real_names[i]);
3798 file_data->real_names[i] = NULL;
9291a0cd
TT
3799 }
3800 }
7b9f3c50
DE
3801
3802 return 1;
3803}
3804
3805static void
3806dw2_forget_cached_source_info (struct objfile *objfile)
3807{
ed2dc618
SM
3808 struct dwarf2_per_objfile *dwarf2_per_objfile
3809 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3810
3811 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3812 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3813}
3814
f8eba3c6
TT
3815/* Helper function for dw2_map_symtabs_matching_filename that expands
3816 the symtabs and calls the iterator. */
3817
3818static int
3819dw2_map_expand_apply (struct objfile *objfile,
3820 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3821 const char *name, const char *real_path,
14bc53a8 3822 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3823{
43f3e411 3824 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3825
3826 /* Don't visit already-expanded CUs. */
43f3e411 3827 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3828 return 0;
3829
3830 /* This may expand more than one symtab, and we want to iterate over
3831 all of them. */
58f0c718 3832 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3833
14bc53a8
PA
3834 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3835 last_made, callback);
f8eba3c6
TT
3836}
3837
3838/* Implementation of the map_symtabs_matching_filename method. */
3839
14bc53a8
PA
3840static bool
3841dw2_map_symtabs_matching_filename
3842 (struct objfile *objfile, const char *name, const char *real_path,
3843 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3844{
c011a4f4 3845 const char *name_basename = lbasename (name);
ed2dc618
SM
3846 struct dwarf2_per_objfile *dwarf2_per_objfile
3847 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3848
848e3e78
DE
3849 /* The rule is CUs specify all the files, including those used by
3850 any TU, so there's no need to scan TUs here. */
f4dc4d17 3851
b76e467d 3852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3853 {
3d7bb9d9 3854 /* We only need to look at symtabs not already expanded. */
43f3e411 3855 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3856 continue;
3857
b76e467d 3858 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3859 if (file_data == NULL)
9291a0cd
TT
3860 continue;
3861
b76e467d 3862 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3863 {
7b9f3c50 3864 const char *this_name = file_data->file_names[j];
da235a7c 3865 const char *this_real_name;
9291a0cd 3866
af529f8f 3867 if (compare_filenames_for_search (this_name, name))
9291a0cd 3868 {
f5b95b50 3869 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3870 callback))
3871 return true;
288e77a7 3872 continue;
4aac40c8 3873 }
9291a0cd 3874
c011a4f4
DE
3875 /* Before we invoke realpath, which can get expensive when many
3876 files are involved, do a quick comparison of the basenames. */
3877 if (! basenames_may_differ
3878 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3879 continue;
3880
da235a7c
JK
3881 this_real_name = dw2_get_real_path (objfile, file_data, j);
3882 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3883 {
da235a7c 3884 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3885 callback))
3886 return true;
288e77a7 3887 continue;
da235a7c 3888 }
9291a0cd 3889
da235a7c
JK
3890 if (real_path != NULL)
3891 {
af529f8f
JK
3892 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3893 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3894 if (this_real_name != NULL
af529f8f 3895 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3896 {
f5b95b50 3897 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3898 callback))
3899 return true;
288e77a7 3900 continue;
9291a0cd
TT
3901 }
3902 }
3903 }
3904 }
3905
14bc53a8 3906 return false;
9291a0cd
TT
3907}
3908
da51c347
DE
3909/* Struct used to manage iterating over all CUs looking for a symbol. */
3910
3911struct dw2_symtab_iterator
9291a0cd 3912{
ed2dc618
SM
3913 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3914 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3915 /* If set, only look for symbols that match that block. Valid values are
3916 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3917 gdb::optional<block_enum> block_index;
da51c347
DE
3918 /* The kind of symbol we're looking for. */
3919 domain_enum domain;
3920 /* The list of CUs from the index entry of the symbol,
3921 or NULL if not found. */
3922 offset_type *vec;
3923 /* The next element in VEC to look at. */
3924 int next;
3925 /* The number of elements in VEC, or zero if there is no match. */
3926 int length;
8943b874
DE
3927 /* Have we seen a global version of the symbol?
3928 If so we can ignore all further global instances.
3929 This is to work around gold/15646, inefficient gold-generated
3930 indices. */
3931 int global_seen;
da51c347 3932};
9291a0cd 3933
2b79f376 3934/* Initialize the index symtab iterator ITER. */
2fdf6df6 3935
9291a0cd 3936static void
da51c347 3937dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3938 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3939 gdb::optional<block_enum> block_index,
da51c347
DE
3940 domain_enum domain,
3941 const char *name)
3942{
ed2dc618 3943 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3944 iter->block_index = block_index;
3945 iter->domain = domain;
3946 iter->next = 0;
8943b874 3947 iter->global_seen = 0;
da51c347 3948
3063847f 3949 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3950
3951 /* index is NULL if OBJF_READNOW. */
3952 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3953 iter->length = MAYBE_SWAP (*iter->vec);
3954 else
3955 {
3956 iter->vec = NULL;
3957 iter->length = 0;
3958 }
3959}
3960
3961/* Return the next matching CU or NULL if there are no more. */
3962
3963static struct dwarf2_per_cu_data *
3964dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3965{
ed2dc618
SM
3966 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3967
da51c347
DE
3968 for ( ; iter->next < iter->length; ++iter->next)
3969 {
3970 offset_type cu_index_and_attrs =
3971 MAYBE_SWAP (iter->vec[iter->next + 1]);
3972 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3973 gdb_index_symbol_kind symbol_kind =
3974 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3975 /* Only check the symbol attributes if they're present.
3976 Indices prior to version 7 don't record them,
3977 and indices >= 7 may elide them for certain symbols
3978 (gold does this). */
3979 int attrs_valid =
ed2dc618 3980 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3981 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3982
3190f0c6 3983 /* Don't crash on bad data. */
b76e467d 3984 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3985 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3986 {
b98664d3 3987 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3988 " [in module %s]"),
3989 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3990 continue;
3991 }
3992
ff4c9fec 3993 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3994
da51c347 3995 /* Skip if already read in. */
43f3e411 3996 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3997 continue;
3998
8943b874
DE
3999 /* Check static vs global. */
4000 if (attrs_valid)
4001 {
2b79f376
SM
4002 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4003
4004 if (iter->block_index.has_value ())
4005 {
4006 bool want_static = *iter->block_index == STATIC_BLOCK;
4007
4008 if (is_static != want_static)
4009 continue;
4010 }
4011
8943b874
DE
4012 /* Work around gold/15646. */
4013 if (!is_static && iter->global_seen)
4014 continue;
4015 if (!is_static)
4016 iter->global_seen = 1;
4017 }
da51c347
DE
4018
4019 /* Only check the symbol's kind if it has one. */
4020 if (attrs_valid)
4021 {
4022 switch (iter->domain)
4023 {
4024 case VAR_DOMAIN:
4025 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4026 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4027 /* Some types are also in VAR_DOMAIN. */
4028 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4029 continue;
4030 break;
4031 case STRUCT_DOMAIN:
4032 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4033 continue;
4034 break;
4035 case LABEL_DOMAIN:
4036 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4037 continue;
4038 break;
59c35742
AB
4039 case MODULE_DOMAIN:
4040 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4041 continue;
4042 break;
da51c347
DE
4043 default:
4044 break;
4045 }
4046 }
4047
4048 ++iter->next;
4049 return per_cu;
4050 }
4051
4052 return NULL;
4053}
4054
43f3e411 4055static struct compunit_symtab *
c7f839cb 4056dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4057 const char *name, domain_enum domain)
9291a0cd 4058{
43f3e411 4059 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4060 struct dwarf2_per_objfile *dwarf2_per_objfile
4061 = get_dwarf2_per_objfile (objfile);
9291a0cd 4062
b5ec771e
PA
4063 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4064
ed2dc618
SM
4065 struct dw2_symtab_iterator iter;
4066 struct dwarf2_per_cu_data *per_cu;
da51c347 4067
2b79f376 4068 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4069
ed2dc618
SM
4070 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4071 {
4072 struct symbol *sym, *with_opaque = NULL;
58f0c718 4073 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4074 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4075 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4076
ed2dc618
SM
4077 sym = block_find_symbol (block, name, domain,
4078 block_find_non_opaque_type_preferred,
4079 &with_opaque);
b2e2f908 4080
ed2dc618
SM
4081 /* Some caution must be observed with overloaded functions
4082 and methods, since the index will not contain any overload
4083 information (but NAME might contain it). */
da51c347 4084
ed2dc618
SM
4085 if (sym != NULL
4086 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4087 return stab;
4088 if (with_opaque != NULL
4089 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4090 stab_best = stab;
da51c347 4091
ed2dc618 4092 /* Keep looking through other CUs. */
9291a0cd 4093 }
9291a0cd 4094
da51c347 4095 return stab_best;
9291a0cd
TT
4096}
4097
4098static void
4099dw2_print_stats (struct objfile *objfile)
4100{
ed2dc618
SM
4101 struct dwarf2_per_objfile *dwarf2_per_objfile
4102 = get_dwarf2_per_objfile (objfile);
b76e467d 4103 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4104 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4105 int count = 0;
9291a0cd 4106
ed2dc618 4107 for (int i = 0; i < total; ++i)
9291a0cd 4108 {
ff4c9fec 4109 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4110
43f3e411 4111 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4112 ++count;
4113 }
e4a48d9d 4114 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4115 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4116}
4117
779bd270
DE
4118/* This dumps minimal information about the index.
4119 It is called via "mt print objfiles".
4120 One use is to verify .gdb_index has been loaded by the
4121 gdb.dwarf2/gdb-index.exp testcase. */
4122
9291a0cd
TT
4123static void
4124dw2_dump (struct objfile *objfile)
4125{
ed2dc618
SM
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
4128
779bd270
DE
4129 gdb_assert (dwarf2_per_objfile->using_index);
4130 printf_filtered (".gdb_index:");
4131 if (dwarf2_per_objfile->index_table != NULL)
4132 {
4133 printf_filtered (" version %d\n",
4134 dwarf2_per_objfile->index_table->version);
4135 }
4136 else
4137 printf_filtered (" faked for \"readnow\"\n");
4138 printf_filtered ("\n");
9291a0cd
TT
4139}
4140
9291a0cd
TT
4141static void
4142dw2_expand_symtabs_for_function (struct objfile *objfile,
4143 const char *func_name)
4144{
ed2dc618
SM
4145 struct dwarf2_per_objfile *dwarf2_per_objfile
4146 = get_dwarf2_per_objfile (objfile);
da51c347 4147
ed2dc618
SM
4148 struct dw2_symtab_iterator iter;
4149 struct dwarf2_per_cu_data *per_cu;
da51c347 4150
2b79f376 4151 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4152
ed2dc618 4153 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4154 dw2_instantiate_symtab (per_cu, false);
da51c347 4155
9291a0cd
TT
4156}
4157
4158static void
4159dw2_expand_all_symtabs (struct objfile *objfile)
4160{
ed2dc618
SM
4161 struct dwarf2_per_objfile *dwarf2_per_objfile
4162 = get_dwarf2_per_objfile (objfile);
b76e467d 4163 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4164 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4165
ed2dc618 4166 for (int i = 0; i < total_units; ++i)
9291a0cd 4167 {
ff4c9fec 4168 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4169
58f0c718
TT
4170 /* We don't want to directly expand a partial CU, because if we
4171 read it with the wrong language, then assertion failures can
4172 be triggered later on. See PR symtab/23010. So, tell
4173 dw2_instantiate_symtab to skip partial CUs -- any important
4174 partial CU will be read via DW_TAG_imported_unit anyway. */
4175 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4176 }
4177}
4178
4179static void
652a8996
JK
4180dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4181 const char *fullname)
9291a0cd 4182{
ed2dc618
SM
4183 struct dwarf2_per_objfile *dwarf2_per_objfile
4184 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4185
4186 /* We don't need to consider type units here.
4187 This is only called for examining code, e.g. expand_line_sal.
4188 There can be an order of magnitude (or more) more type units
4189 than comp units, and we avoid them if we can. */
4190
b76e467d 4191 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4192 {
3d7bb9d9 4193 /* We only need to look at symtabs not already expanded. */
43f3e411 4194 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4195 continue;
4196
b76e467d 4197 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4198 if (file_data == NULL)
9291a0cd
TT
4199 continue;
4200
b76e467d 4201 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4202 {
652a8996
JK
4203 const char *this_fullname = file_data->file_names[j];
4204
4205 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4206 {
58f0c718 4207 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4208 break;
4209 }
4210 }
4211 }
4212}
4213
9291a0cd 4214static void
199b4314
TT
4215dw2_map_matching_symbols
4216 (struct objfile *objfile,
b054970d 4217 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4218 int global,
4219 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4220 symbol_compare_ftype *ordered_compare)
9291a0cd 4221{
40658b94 4222 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4223 current language is Ada for a non-Ada objfile using GNU index. As Ada
4224 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4225}
4226
e1ef7d7a
PA
4227/* Starting from a search name, return the string that finds the upper
4228 bound of all strings that start with SEARCH_NAME in a sorted name
4229 list. Returns the empty string to indicate that the upper bound is
4230 the end of the list. */
4231
4232static std::string
4233make_sort_after_prefix_name (const char *search_name)
4234{
4235 /* When looking to complete "func", we find the upper bound of all
4236 symbols that start with "func" by looking for where we'd insert
4237 the closest string that would follow "func" in lexicographical
4238 order. Usually, that's "func"-with-last-character-incremented,
4239 i.e. "fund". Mind non-ASCII characters, though. Usually those
4240 will be UTF-8 multi-byte sequences, but we can't be certain.
4241 Especially mind the 0xff character, which is a valid character in
4242 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4243 rule out compilers allowing it in identifiers. Note that
4244 conveniently, strcmp/strcasecmp are specified to compare
4245 characters interpreted as unsigned char. So what we do is treat
4246 the whole string as a base 256 number composed of a sequence of
4247 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4248 to 0, and carries 1 to the following more-significant position.
4249 If the very first character in SEARCH_NAME ends up incremented
4250 and carries/overflows, then the upper bound is the end of the
4251 list. The string after the empty string is also the empty
4252 string.
4253
4254 Some examples of this operation:
4255
4256 SEARCH_NAME => "+1" RESULT
4257
4258 "abc" => "abd"
4259 "ab\xff" => "ac"
4260 "\xff" "a" "\xff" => "\xff" "b"
4261 "\xff" => ""
4262 "\xff\xff" => ""
4263 "" => ""
4264
4265 Then, with these symbols for example:
4266
4267 func
4268 func1
4269 fund
4270
4271 completing "func" looks for symbols between "func" and
4272 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4273 which finds "func" and "func1", but not "fund".
4274
4275 And with:
4276
4277 funcÿ (Latin1 'ÿ' [0xff])
4278 funcÿ1
4279 fund
4280
4281 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4282 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4283
4284 And with:
4285
4286 ÿÿ (Latin1 'ÿ' [0xff])
4287 ÿÿ1
4288
4289 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4290 the end of the list.
4291 */
4292 std::string after = search_name;
4293 while (!after.empty () && (unsigned char) after.back () == 0xff)
4294 after.pop_back ();
4295 if (!after.empty ())
4296 after.back () = (unsigned char) after.back () + 1;
4297 return after;
4298}
4299
5c58de74 4300/* See declaration. */
61d96d7e 4301
5c58de74
PA
4302std::pair<std::vector<name_component>::const_iterator,
4303 std::vector<name_component>::const_iterator>
44ed8f3e 4304mapped_index_base::find_name_components_bounds
3b00ef10 4305 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4306{
5c58de74
PA
4307 auto *name_cmp
4308 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4309
3b00ef10
TT
4310 const char *lang_name
4311 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4312
3f563c84
PA
4313 /* Comparison function object for lower_bound that matches against a
4314 given symbol name. */
4315 auto lookup_compare_lower = [&] (const name_component &elem,
4316 const char *name)
4317 {
5c58de74 4318 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4319 const char *elem_name = elem_qualified + elem.name_offset;
4320 return name_cmp (elem_name, name) < 0;
4321 };
4322
4323 /* Comparison function object for upper_bound that matches against a
4324 given symbol name. */
4325 auto lookup_compare_upper = [&] (const char *name,
4326 const name_component &elem)
4327 {
5c58de74 4328 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4329 const char *elem_name = elem_qualified + elem.name_offset;
4330 return name_cmp (name, elem_name) < 0;
4331 };
4332
5c58de74
PA
4333 auto begin = this->name_components.begin ();
4334 auto end = this->name_components.end ();
3f563c84
PA
4335
4336 /* Find the lower bound. */
4337 auto lower = [&] ()
4338 {
3b00ef10 4339 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4340 return begin;
4341 else
3b00ef10 4342 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4343 } ();
4344
4345 /* Find the upper bound. */
4346 auto upper = [&] ()
4347 {
5c58de74 4348 if (lookup_name_without_params.completion_mode ())
3f563c84 4349 {
e1ef7d7a
PA
4350 /* In completion mode, we want UPPER to point past all
4351 symbols names that have the same prefix. I.e., with
4352 these symbols, and completing "func":
4353
4354 function << lower bound
4355 function1
4356 other_function << upper bound
4357
4358 We find the upper bound by looking for the insertion
4359 point of "func"-with-last-character-incremented,
4360 i.e. "fund". */
3b00ef10 4361 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4362 if (after.empty ())
3f563c84 4363 return end;
e6b2f5ef
PA
4364 return std::lower_bound (lower, end, after.c_str (),
4365 lookup_compare_lower);
3f563c84
PA
4366 }
4367 else
3b00ef10 4368 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4369 } ();
4370
5c58de74
PA
4371 return {lower, upper};
4372}
4373
4374/* See declaration. */
4375
4376void
44ed8f3e 4377mapped_index_base::build_name_components ()
5c58de74
PA
4378{
4379 if (!this->name_components.empty ())
4380 return;
4381
4382 this->name_components_casing = case_sensitivity;
4383 auto *name_cmp
4384 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4385
4386 /* The code below only knows how to break apart components of C++
4387 symbol names (and other languages that use '::' as
3b00ef10 4388 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4389 auto count = this->symbol_name_count ();
4390 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4391 {
44ed8f3e 4392 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4393 continue;
4394
4395 const char *name = this->symbol_name_at (idx);
4396
4397 /* Add each name component to the name component table. */
4398 unsigned int previous_len = 0;
3b00ef10
TT
4399
4400 if (strstr (name, "::") != nullptr)
4401 {
4402 for (unsigned int current_len = cp_find_first_component (name);
4403 name[current_len] != '\0';
4404 current_len += cp_find_first_component (name + current_len))
4405 {
4406 gdb_assert (name[current_len] == ':');
4407 this->name_components.push_back ({previous_len, idx});
4408 /* Skip the '::'. */
4409 current_len += 2;
4410 previous_len = current_len;
4411 }
4412 }
4413 else
5c58de74 4414 {
3b00ef10
TT
4415 /* Handle the Ada encoded (aka mangled) form here. */
4416 for (const char *iter = strstr (name, "__");
4417 iter != nullptr;
4418 iter = strstr (iter, "__"))
4419 {
4420 this->name_components.push_back ({previous_len, idx});
4421 iter += 2;
4422 previous_len = iter - name;
4423 }
5c58de74 4424 }
3b00ef10 4425
5c58de74
PA
4426 this->name_components.push_back ({previous_len, idx});
4427 }
4428
4429 /* Sort name_components elements by name. */
4430 auto name_comp_compare = [&] (const name_component &left,
4431 const name_component &right)
4432 {
4433 const char *left_qualified = this->symbol_name_at (left.idx);
4434 const char *right_qualified = this->symbol_name_at (right.idx);
4435
4436 const char *left_name = left_qualified + left.name_offset;
4437 const char *right_name = right_qualified + right.name_offset;
4438
4439 return name_cmp (left_name, right_name) < 0;
4440 };
4441
4442 std::sort (this->name_components.begin (),
4443 this->name_components.end (),
4444 name_comp_compare);
4445}
4446
4447/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4448 mapped_index_base instead of the containing objfile. This is split
4449 to a separate function in order to be able to unit test the
4450 name_components matching using a mock mapped_index_base. For each
5c58de74 4451 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4452 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4453
4454static void
4455dw2_expand_symtabs_matching_symbol
44ed8f3e 4456 (mapped_index_base &index,
5c58de74
PA
4457 const lookup_name_info &lookup_name_in,
4458 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4459 enum search_domain kind,
3b00ef10 4460 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4461{
4462 lookup_name_info lookup_name_without_params
4463 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4464
4465 /* Build the symbol name component sorted vector, if we haven't
4466 yet. */
4467 index.build_name_components ();
4468
3f563c84
PA
4469 /* The same symbol may appear more than once in the range though.
4470 E.g., if we're looking for symbols that complete "w", and we have
4471 a symbol named "w1::w2", we'll find the two name components for
4472 that same symbol in the range. To be sure we only call the
4473 callback once per symbol, we first collect the symbol name
4474 indexes that matched in a temporary vector and ignore
4475 duplicates. */
4476 std::vector<offset_type> matches;
3f563c84 4477
3b00ef10
TT
4478 struct name_and_matcher
4479 {
4480 symbol_name_matcher_ftype *matcher;
4481 const std::string &name;
4482
4483 bool operator== (const name_and_matcher &other) const
3f563c84 4484 {
3b00ef10
TT
4485 return matcher == other.matcher && name == other.name;
4486 }
4487 };
4488
4489 /* A vector holding all the different symbol name matchers, for all
4490 languages. */
4491 std::vector<name_and_matcher> matchers;
4492
4493 for (int i = 0; i < nr_languages; i++)
4494 {
4495 enum language lang_e = (enum language) i;
4496
4497 const language_defn *lang = language_def (lang_e);
4498 symbol_name_matcher_ftype *name_matcher
4499 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4500
3b00ef10
TT
4501 name_and_matcher key {
4502 name_matcher,
4503 lookup_name_without_params.language_lookup_name (lang_e)
4504 };
4505
4506 /* Don't insert the same comparison routine more than once.
4507 Note that we do this linear walk. This is not a problem in
4508 practice because the number of supported languages is
4509 low. */
4510 if (std::find (matchers.begin (), matchers.end (), key)
4511 != matchers.end ())
9291a0cd 4512 continue;
3b00ef10
TT
4513 matchers.push_back (std::move (key));
4514
4515 auto bounds
4516 = index.find_name_components_bounds (lookup_name_without_params,
4517 lang_e);
4518
4519 /* Now for each symbol name in range, check to see if we have a name
4520 match, and if so, call the MATCH_CALLBACK callback. */
4521
4522 for (; bounds.first != bounds.second; ++bounds.first)
4523 {
4524 const char *qualified = index.symbol_name_at (bounds.first->idx);
4525
4526 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4527 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4528 continue;
9291a0cd 4529
3b00ef10
TT
4530 matches.push_back (bounds.first->idx);
4531 }
3f563c84
PA
4532 }
4533
4534 std::sort (matches.begin (), matches.end ());
4535
4536 /* Finally call the callback, once per match. */
4537 ULONGEST prev = -1;
4538 for (offset_type idx : matches)
4539 {
4540 if (prev != idx)
4541 {
3b00ef10
TT
4542 if (!match_callback (idx))
4543 break;
3f563c84
PA
4544 prev = idx;
4545 }
4546 }
4547
4548 /* Above we use a type wider than idx's for 'prev', since 0 and
4549 (offset_type)-1 are both possible values. */
4550 static_assert (sizeof (prev) > sizeof (offset_type), "");
4551}
4552
c62446b1
PA
4553#if GDB_SELF_TEST
4554
4555namespace selftests { namespace dw2_expand_symtabs_matching {
4556
a3c5fafd
PA
4557/* A mock .gdb_index/.debug_names-like name index table, enough to
4558 exercise dw2_expand_symtabs_matching_symbol, which works with the
4559 mapped_index_base interface. Builds an index from the symbol list
4560 passed as parameter to the constructor. */
4561class mock_mapped_index : public mapped_index_base
c62446b1
PA
4562{
4563public:
a3c5fafd
PA
4564 mock_mapped_index (gdb::array_view<const char *> symbols)
4565 : m_symbol_table (symbols)
c62446b1
PA
4566 {}
4567
a3c5fafd 4568 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4569
a3c5fafd 4570 /* Return the number of names in the symbol table. */
632e107b 4571 size_t symbol_name_count () const override
c62446b1 4572 {
a3c5fafd 4573 return m_symbol_table.size ();
c62446b1
PA
4574 }
4575
a3c5fafd 4576 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4577 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4578 {
4579 return m_symbol_table[idx];
4580 }
c62446b1 4581
a3c5fafd
PA
4582private:
4583 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4584};
4585
4586/* Convenience function that converts a NULL pointer to a "<null>"
4587 string, to pass to print routines. */
4588
4589static const char *
4590string_or_null (const char *str)
4591{
4592 return str != NULL ? str : "<null>";
4593}
4594
4595/* Check if a lookup_name_info built from
4596 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4597 index. EXPECTED_LIST is the list of expected matches, in expected
4598 matching order. If no match expected, then an empty list is
4599 specified. Returns true on success. On failure prints a warning
4600 indicating the file:line that failed, and returns false. */
4601
4602static bool
4603check_match (const char *file, int line,
4604 mock_mapped_index &mock_index,
4605 const char *name, symbol_name_match_type match_type,
4606 bool completion_mode,
4607 std::initializer_list<const char *> expected_list)
4608{
4609 lookup_name_info lookup_name (name, match_type, completion_mode);
4610
4611 bool matched = true;
4612
4613 auto mismatch = [&] (const char *expected_str,
4614 const char *got)
4615 {
4616 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4617 "expected=\"%s\", got=\"%s\"\n"),
4618 file, line,
4619 (match_type == symbol_name_match_type::FULL
4620 ? "FULL" : "WILD"),
4621 name, string_or_null (expected_str), string_or_null (got));
4622 matched = false;
4623 };
4624
4625 auto expected_it = expected_list.begin ();
4626 auto expected_end = expected_list.end ();
4627
a3c5fafd 4628 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4629 NULL, ALL_DOMAIN,
4630 [&] (offset_type idx)
4631 {
a3c5fafd 4632 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4633 const char *expected_str
4634 = expected_it == expected_end ? NULL : *expected_it++;
4635
4636 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4637 mismatch (expected_str, matched_name);
3b00ef10 4638 return true;
c62446b1
PA
4639 });
4640
4641 const char *expected_str
4642 = expected_it == expected_end ? NULL : *expected_it++;
4643 if (expected_str != NULL)
4644 mismatch (expected_str, NULL);
4645
4646 return matched;
4647}
4648
4649/* The symbols added to the mock mapped_index for testing (in
4650 canonical form). */
4651static const char *test_symbols[] = {
4652 "function",
4653 "std::bar",
4654 "std::zfunction",
4655 "std::zfunction2",
4656 "w1::w2",
4657 "ns::foo<char*>",
4658 "ns::foo<int>",
4659 "ns::foo<long>",
a20714ff
PA
4660 "ns2::tmpl<int>::foo2",
4661 "(anonymous namespace)::A::B::C",
c62446b1 4662
e1ef7d7a
PA
4663 /* These are used to check that the increment-last-char in the
4664 matching algorithm for completion doesn't match "t1_fund" when
4665 completing "t1_func". */
4666 "t1_func",
4667 "t1_func1",
4668 "t1_fund",
4669 "t1_fund1",
4670
4671 /* A UTF-8 name with multi-byte sequences to make sure that
4672 cp-name-parser understands this as a single identifier ("função"
4673 is "function" in PT). */
4674 u8"u8função",
4675
4676 /* \377 (0xff) is Latin1 'ÿ'. */
4677 "yfunc\377",
4678
4679 /* \377 (0xff) is Latin1 'ÿ'. */
4680 "\377",
4681 "\377\377123",
4682
c62446b1
PA
4683 /* A name with all sorts of complications. Starts with "z" to make
4684 it easier for the completion tests below. */
4685#define Z_SYM_NAME \
4686 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4687 "::tuple<(anonymous namespace)::ui*, " \
4688 "std::default_delete<(anonymous namespace)::ui>, void>"
4689
4690 Z_SYM_NAME
4691};
4692
a3c5fafd
PA
4693/* Returns true if the mapped_index_base::find_name_component_bounds
4694 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4695 in completion mode. */
5c58de74
PA
4696
4697static bool
a3c5fafd 4698check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4699 const char *search_name,
4700 gdb::array_view<const char *> expected_syms)
4701{
4702 lookup_name_info lookup_name (search_name,
4703 symbol_name_match_type::FULL, true);
4704
3b00ef10
TT
4705 auto bounds = index.find_name_components_bounds (lookup_name,
4706 language_cplus);
5c58de74
PA
4707
4708 size_t distance = std::distance (bounds.first, bounds.second);
4709 if (distance != expected_syms.size ())
4710 return false;
4711
4712 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4713 {
4714 auto nc_elem = bounds.first + exp_elem;
4715 const char *qualified = index.symbol_name_at (nc_elem->idx);
4716 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4717 return false;
4718 }
4719
4720 return true;
4721}
4722
4723/* Test the lower-level mapped_index::find_name_component_bounds
4724 method. */
4725
c62446b1 4726static void
5c58de74
PA
4727test_mapped_index_find_name_component_bounds ()
4728{
4729 mock_mapped_index mock_index (test_symbols);
4730
a3c5fafd 4731 mock_index.build_name_components ();
5c58de74
PA
4732
4733 /* Test the lower-level mapped_index::find_name_component_bounds
4734 method in completion mode. */
4735 {
4736 static const char *expected_syms[] = {
4737 "t1_func",
4738 "t1_func1",
5c58de74
PA
4739 };
4740
a3c5fafd 4741 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4742 "t1_func", expected_syms));
4743 }
4744
4745 /* Check that the increment-last-char in the name matching algorithm
4746 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4747 {
4748 static const char *expected_syms1[] = {
4749 "\377",
4750 "\377\377123",
4751 };
a3c5fafd 4752 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4753 "\377", expected_syms1));
4754
4755 static const char *expected_syms2[] = {
4756 "\377\377123",
4757 };
a3c5fafd 4758 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4759 "\377\377", expected_syms2));
4760 }
4761}
4762
4763/* Test dw2_expand_symtabs_matching_symbol. */
4764
4765static void
4766test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4767{
4768 mock_mapped_index mock_index (test_symbols);
4769
4770 /* We let all tests run until the end even if some fails, for debug
4771 convenience. */
4772 bool any_mismatch = false;
4773
4774 /* Create the expected symbols list (an initializer_list). Needed
4775 because lists have commas, and we need to pass them to CHECK,
4776 which is a macro. */
4777#define EXPECT(...) { __VA_ARGS__ }
4778
4779 /* Wrapper for check_match that passes down the current
4780 __FILE__/__LINE__. */
4781#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4782 any_mismatch |= !check_match (__FILE__, __LINE__, \
4783 mock_index, \
4784 NAME, MATCH_TYPE, COMPLETION_MODE, \
4785 EXPECTED_LIST)
4786
4787 /* Identity checks. */
4788 for (const char *sym : test_symbols)
4789 {
4790 /* Should be able to match all existing symbols. */
4791 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4792 EXPECT (sym));
4793
4794 /* Should be able to match all existing symbols with
4795 parameters. */
4796 std::string with_params = std::string (sym) + "(int)";
4797 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4798 EXPECT (sym));
4799
4800 /* Should be able to match all existing symbols with
4801 parameters and qualifiers. */
4802 with_params = std::string (sym) + " ( int ) const";
4803 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4804 EXPECT (sym));
4805
4806 /* This should really find sym, but cp-name-parser.y doesn't
4807 know about lvalue/rvalue qualifiers yet. */
4808 with_params = std::string (sym) + " ( int ) &&";
4809 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4810 {});
4811 }
4812
e1ef7d7a
PA
4813 /* Check that the name matching algorithm for completion doesn't get
4814 confused with Latin1 'ÿ' / 0xff. */
4815 {
4816 static const char str[] = "\377";
4817 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4818 EXPECT ("\377", "\377\377123"));
4819 }
4820
4821 /* Check that the increment-last-char in the matching algorithm for
4822 completion doesn't match "t1_fund" when completing "t1_func". */
4823 {
4824 static const char str[] = "t1_func";
4825 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4826 EXPECT ("t1_func", "t1_func1"));
4827 }
4828
c62446b1
PA
4829 /* Check that completion mode works at each prefix of the expected
4830 symbol name. */
4831 {
4832 static const char str[] = "function(int)";
4833 size_t len = strlen (str);
4834 std::string lookup;
4835
4836 for (size_t i = 1; i < len; i++)
4837 {
4838 lookup.assign (str, i);
4839 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4840 EXPECT ("function"));
4841 }
4842 }
4843
4844 /* While "w" is a prefix of both components, the match function
4845 should still only be called once. */
4846 {
4847 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4848 EXPECT ("w1::w2"));
a20714ff
PA
4849 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4850 EXPECT ("w1::w2"));
c62446b1
PA
4851 }
4852
4853 /* Same, with a "complicated" symbol. */
4854 {
4855 static const char str[] = Z_SYM_NAME;
4856 size_t len = strlen (str);
4857 std::string lookup;
4858
4859 for (size_t i = 1; i < len; i++)
4860 {
4861 lookup.assign (str, i);
4862 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4863 EXPECT (Z_SYM_NAME));
4864 }
4865 }
4866
4867 /* In FULL mode, an incomplete symbol doesn't match. */
4868 {
4869 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4870 {});
4871 }
4872
4873 /* A complete symbol with parameters matches any overload, since the
4874 index has no overload info. */
4875 {
4876 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4877 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4878 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4879 EXPECT ("std::zfunction", "std::zfunction2"));
4880 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4881 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4882 }
4883
4884 /* Check that whitespace is ignored appropriately. A symbol with a
4885 template argument list. */
4886 {
4887 static const char expected[] = "ns::foo<int>";
4888 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4889 EXPECT (expected));
a20714ff
PA
4890 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4891 EXPECT (expected));
c62446b1
PA
4892 }
4893
4894 /* Check that whitespace is ignored appropriately. A symbol with a
4895 template argument list that includes a pointer. */
4896 {
4897 static const char expected[] = "ns::foo<char*>";
4898 /* Try both completion and non-completion modes. */
4899 static const bool completion_mode[2] = {false, true};
4900 for (size_t i = 0; i < 2; i++)
4901 {
4902 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4903 completion_mode[i], EXPECT (expected));
a20714ff
PA
4904 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4905 completion_mode[i], EXPECT (expected));
c62446b1
PA
4906
4907 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4908 completion_mode[i], EXPECT (expected));
a20714ff
PA
4909 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4910 completion_mode[i], EXPECT (expected));
c62446b1
PA
4911 }
4912 }
4913
4914 {
4915 /* Check method qualifiers are ignored. */
4916 static const char expected[] = "ns::foo<char*>";
4917 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4918 symbol_name_match_type::FULL, true, EXPECT (expected));
4919 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4920 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4921 CHECK_MATCH ("foo < char * > ( int ) const",
4922 symbol_name_match_type::WILD, true, EXPECT (expected));
4923 CHECK_MATCH ("foo < char * > ( int ) &&",
4924 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4925 }
4926
4927 /* Test lookup names that don't match anything. */
4928 {
a20714ff
PA
4929 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4930 {});
4931
c62446b1
PA
4932 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4933 {});
4934 }
4935
a20714ff
PA
4936 /* Some wild matching tests, exercising "(anonymous namespace)",
4937 which should not be confused with a parameter list. */
4938 {
4939 static const char *syms[] = {
4940 "A::B::C",
4941 "B::C",
4942 "C",
4943 "A :: B :: C ( int )",
4944 "B :: C ( int )",
4945 "C ( int )",
4946 };
4947
4948 for (const char *s : syms)
4949 {
4950 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4951 EXPECT ("(anonymous namespace)::A::B::C"));
4952 }
4953 }
4954
4955 {
4956 static const char expected[] = "ns2::tmpl<int>::foo2";
4957 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4958 EXPECT (expected));
4959 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4960 EXPECT (expected));
4961 }
4962
c62446b1
PA
4963 SELF_CHECK (!any_mismatch);
4964
4965#undef EXPECT
4966#undef CHECK_MATCH
4967}
4968
5c58de74
PA
4969static void
4970run_test ()
4971{
4972 test_mapped_index_find_name_component_bounds ();
4973 test_dw2_expand_symtabs_matching_symbol ();
4974}
4975
c62446b1
PA
4976}} // namespace selftests::dw2_expand_symtabs_matching
4977
4978#endif /* GDB_SELF_TEST */
4979
4b514bc8
JK
4980/* If FILE_MATCHER is NULL or if PER_CU has
4981 dwarf2_per_cu_quick_data::MARK set (see
4982 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4983 EXPANSION_NOTIFY on it. */
4984
4985static void
4986dw2_expand_symtabs_matching_one
4987 (struct dwarf2_per_cu_data *per_cu,
4988 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4989 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4990{
4991 if (file_matcher == NULL || per_cu->v.quick->mark)
4992 {
4993 bool symtab_was_null
4994 = (per_cu->v.quick->compunit_symtab == NULL);
4995
58f0c718 4996 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4997
4998 if (expansion_notify != NULL
4999 && symtab_was_null
5000 && per_cu->v.quick->compunit_symtab != NULL)
5001 expansion_notify (per_cu->v.quick->compunit_symtab);
5002 }
5003}
5004
3f563c84
PA
5005/* Helper for dw2_expand_matching symtabs. Called on each symbol
5006 matched, to expand corresponding CUs that were marked. IDX is the
5007 index of the symbol name that matched. */
5008
5009static void
5010dw2_expand_marked_cus
ed2dc618 5011 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5012 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5013 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5014 search_domain kind)
5015{
3f563c84
PA
5016 offset_type *vec, vec_len, vec_idx;
5017 bool global_seen = false;
ed2dc618 5018 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5019
61920122 5020 vec = (offset_type *) (index.constant_pool
f00a2de2 5021 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5022 vec_len = MAYBE_SWAP (vec[0]);
5023 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5024 {
61920122
PA
5025 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5026 /* This value is only valid for index versions >= 7. */
5027 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5028 gdb_index_symbol_kind symbol_kind =
5029 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5030 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5031 /* Only check the symbol attributes if they're present.
5032 Indices prior to version 7 don't record them,
5033 and indices >= 7 may elide them for certain symbols
5034 (gold does this). */
5035 int attrs_valid =
5036 (index.version >= 7
5037 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5038
5039 /* Work around gold/15646. */
5040 if (attrs_valid)
9291a0cd 5041 {
61920122
PA
5042 if (!is_static && global_seen)
5043 continue;
5044 if (!is_static)
5045 global_seen = true;
5046 }
3190f0c6 5047
61920122
PA
5048 /* Only check the symbol's kind if it has one. */
5049 if (attrs_valid)
5050 {
5051 switch (kind)
8943b874 5052 {
61920122
PA
5053 case VARIABLES_DOMAIN:
5054 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5055 continue;
5056 break;
5057 case FUNCTIONS_DOMAIN:
5058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5059 continue;
61920122
PA
5060 break;
5061 case TYPES_DOMAIN:
5062 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5063 continue;
5064 break;
59c35742
AB
5065 case MODULES_DOMAIN:
5066 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5067 continue;
5068 break;
61920122
PA
5069 default:
5070 break;
8943b874 5071 }
61920122 5072 }
8943b874 5073
61920122 5074 /* Don't crash on bad data. */
b76e467d 5075 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5076 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5077 {
b98664d3 5078 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5079 " [in module %s]"),
5080 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5081 continue;
5082 }
5083
ff4c9fec 5084 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5085 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5086 expansion_notify);
61920122
PA
5087 }
5088}
5089
4b514bc8
JK
5090/* If FILE_MATCHER is non-NULL, set all the
5091 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5092 that match FILE_MATCHER. */
5093
61920122 5094static void
4b514bc8 5095dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5096 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5097 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5098{
4b514bc8 5099 if (file_matcher == NULL)
61920122
PA
5100 return;
5101
4b514bc8
JK
5102 objfile *const objfile = dwarf2_per_objfile->objfile;
5103
5104 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5105 htab_eq_pointer,
5106 NULL, xcalloc, xfree));
5107 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5108 htab_eq_pointer,
5109 NULL, xcalloc, xfree));
61920122 5110
4b514bc8
JK
5111 /* The rule is CUs specify all the files, including those used by
5112 any TU, so there's no need to scan TUs here. */
61920122 5113
b76e467d 5114 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5115 {
927aa2e7
JK
5116 QUIT;
5117
5118 per_cu->v.quick->mark = 0;
5119
5120 /* We only need to look at symtabs not already expanded. */
5121 if (per_cu->v.quick->compunit_symtab)
5122 continue;
5123
b76e467d 5124 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5125 if (file_data == NULL)
5126 continue;
5127
5128 if (htab_find (visited_not_found.get (), file_data) != NULL)
5129 continue;
5130 else if (htab_find (visited_found.get (), file_data) != NULL)
5131 {
5132 per_cu->v.quick->mark = 1;
5133 continue;
5134 }
5135
b76e467d 5136 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5137 {
5138 const char *this_real_name;
5139
5140 if (file_matcher (file_data->file_names[j], false))
5141 {
5142 per_cu->v.quick->mark = 1;
5143 break;
5144 }
5145
5146 /* Before we invoke realpath, which can get expensive when many
5147 files are involved, do a quick comparison of the basenames. */
5148 if (!basenames_may_differ
5149 && !file_matcher (lbasename (file_data->file_names[j]),
5150 true))
5151 continue;
5152
5153 this_real_name = dw2_get_real_path (objfile, file_data, j);
5154 if (file_matcher (this_real_name, false))
5155 {
5156 per_cu->v.quick->mark = 1;
5157 break;
5158 }
5159 }
5160
b76e467d
SM
5161 void **slot = htab_find_slot (per_cu->v.quick->mark
5162 ? visited_found.get ()
5163 : visited_not_found.get (),
5164 file_data, INSERT);
927aa2e7
JK
5165 *slot = file_data;
5166 }
5167}
5168
5169static void
5170dw2_expand_symtabs_matching
5171 (struct objfile *objfile,
5172 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5173 const lookup_name_info &lookup_name,
5174 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5175 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5176 enum search_domain kind)
5177{
ed2dc618
SM
5178 struct dwarf2_per_objfile *dwarf2_per_objfile
5179 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5180
5181 /* index_table is NULL if OBJF_READNOW. */
5182 if (!dwarf2_per_objfile->index_table)
5183 return;
5184
ed2dc618 5185 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5186
5187 mapped_index &index = *dwarf2_per_objfile->index_table;
5188
5189 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5190 symbol_matcher,
5191 kind, [&] (offset_type idx)
5192 {
ed2dc618 5193 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5194 expansion_notify, kind);
3b00ef10 5195 return true;
927aa2e7
JK
5196 });
5197}
5198
5199/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5200 symtab. */
5201
5202static struct compunit_symtab *
5203recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5204 CORE_ADDR pc)
5205{
5206 int i;
5207
5208 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5209 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5210 return cust;
5211
5212 if (cust->includes == NULL)
5213 return NULL;
5214
5215 for (i = 0; cust->includes[i]; ++i)
5216 {
5217 struct compunit_symtab *s = cust->includes[i];
5218
5219 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5220 if (s != NULL)
5221 return s;
5222 }
5223
5224 return NULL;
5225}
5226
5227static struct compunit_symtab *
5228dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5229 struct bound_minimal_symbol msymbol,
5230 CORE_ADDR pc,
5231 struct obj_section *section,
5232 int warn_if_readin)
5233{
5234 struct dwarf2_per_cu_data *data;
5235 struct compunit_symtab *result;
5236
d320c2b5 5237 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5238 return NULL;
5239
79748972
TT
5240 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5241 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5242 data = (struct dwarf2_per_cu_data *) addrmap_find
5243 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5244 if (!data)
5245 return NULL;
5246
5247 if (warn_if_readin && data->v.quick->compunit_symtab)
5248 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5249 paddress (get_objfile_arch (objfile), pc));
5250
5251 result
58f0c718
TT
5252 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5253 false),
927aa2e7
JK
5254 pc);
5255 gdb_assert (result != NULL);
5256 return result;
5257}
5258
5259static void
5260dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5261 void *data, int need_fullname)
5262{
ed2dc618
SM
5263 struct dwarf2_per_objfile *dwarf2_per_objfile
5264 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5265
5266 if (!dwarf2_per_objfile->filenames_cache)
5267 {
5268 dwarf2_per_objfile->filenames_cache.emplace ();
5269
5270 htab_up visited (htab_create_alloc (10,
5271 htab_hash_pointer, htab_eq_pointer,
5272 NULL, xcalloc, xfree));
5273
5274 /* The rule is CUs specify all the files, including those used
5275 by any TU, so there's no need to scan TUs here. We can
5276 ignore file names coming from already-expanded CUs. */
5277
b76e467d 5278 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5279 {
927aa2e7
JK
5280 if (per_cu->v.quick->compunit_symtab)
5281 {
5282 void **slot = htab_find_slot (visited.get (),
5283 per_cu->v.quick->file_names,
5284 INSERT);
5285
5286 *slot = per_cu->v.quick->file_names;
5287 }
5288 }
5289
b76e467d 5290 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5291 {
927aa2e7
JK
5292 /* We only need to look at symtabs not already expanded. */
5293 if (per_cu->v.quick->compunit_symtab)
5294 continue;
5295
b76e467d 5296 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5297 if (file_data == NULL)
5298 continue;
5299
b76e467d 5300 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5301 if (*slot)
5302 {
5303 /* Already visited. */
5304 continue;
5305 }
5306 *slot = file_data;
5307
5308 for (int j = 0; j < file_data->num_file_names; ++j)
5309 {
5310 const char *filename = file_data->file_names[j];
5311 dwarf2_per_objfile->filenames_cache->seen (filename);
5312 }
5313 }
5314 }
5315
5316 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5317 {
5318 gdb::unique_xmalloc_ptr<char> this_real_name;
5319
5320 if (need_fullname)
5321 this_real_name = gdb_realpath (filename);
5322 (*fun) (filename, this_real_name.get (), data);
5323 });
5324}
5325
5326static int
5327dw2_has_symbols (struct objfile *objfile)
5328{
5329 return 1;
5330}
5331
5332const struct quick_symbol_functions dwarf2_gdb_index_functions =
5333{
5334 dw2_has_symbols,
5335 dw2_find_last_source_symtab,
5336 dw2_forget_cached_source_info,
5337 dw2_map_symtabs_matching_filename,
5338 dw2_lookup_symbol,
5339 dw2_print_stats,
5340 dw2_dump,
927aa2e7
JK
5341 dw2_expand_symtabs_for_function,
5342 dw2_expand_all_symtabs,
5343 dw2_expand_symtabs_with_fullname,
5344 dw2_map_matching_symbols,
5345 dw2_expand_symtabs_matching,
5346 dw2_find_pc_sect_compunit_symtab,
5347 NULL,
5348 dw2_map_symbol_filenames
5349};
5350
5351/* DWARF-5 debug_names reader. */
5352
5353/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5354static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5355
5356/* A helper function that reads the .debug_names section in SECTION
5357 and fills in MAP. FILENAME is the name of the file containing the
5358 section; it is used for error reporting.
5359
5360 Returns true if all went well, false otherwise. */
5361
5362static bool
5363read_debug_names_from_section (struct objfile *objfile,
5364 const char *filename,
5365 struct dwarf2_section_info *section,
5366 mapped_debug_names &map)
5367{
5368 if (dwarf2_section_empty_p (section))
5369 return false;
5370
5371 /* Older elfutils strip versions could keep the section in the main
5372 executable while splitting it for the separate debug info file. */
5373 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5374 return false;
5375
5376 dwarf2_read_section (objfile, section);
5377
5378 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5379
5380 const gdb_byte *addr = section->buffer;
5381
5382 bfd *const abfd = get_section_bfd_owner (section);
5383
5384 unsigned int bytes_read;
5385 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5386 addr += bytes_read;
5387
5388 map.dwarf5_is_dwarf64 = bytes_read != 4;
5389 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5390 if (bytes_read + length != section->size)
5391 {
5392 /* There may be multiple per-CU indices. */
5393 warning (_("Section .debug_names in %s length %s does not match "
5394 "section length %s, ignoring .debug_names."),
5395 filename, plongest (bytes_read + length),
5396 pulongest (section->size));
5397 return false;
5398 }
5399
5400 /* The version number. */
5401 uint16_t version = read_2_bytes (abfd, addr);
5402 addr += 2;
5403 if (version != 5)
5404 {
5405 warning (_("Section .debug_names in %s has unsupported version %d, "
5406 "ignoring .debug_names."),
5407 filename, version);
5408 return false;
5409 }
5410
5411 /* Padding. */
5412 uint16_t padding = read_2_bytes (abfd, addr);
5413 addr += 2;
5414 if (padding != 0)
5415 {
5416 warning (_("Section .debug_names in %s has unsupported padding %d, "
5417 "ignoring .debug_names."),
5418 filename, padding);
5419 return false;
5420 }
5421
5422 /* comp_unit_count - The number of CUs in the CU list. */
5423 map.cu_count = read_4_bytes (abfd, addr);
5424 addr += 4;
5425
5426 /* local_type_unit_count - The number of TUs in the local TU
5427 list. */
5428 map.tu_count = read_4_bytes (abfd, addr);
5429 addr += 4;
5430
5431 /* foreign_type_unit_count - The number of TUs in the foreign TU
5432 list. */
5433 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5434 addr += 4;
5435 if (foreign_tu_count != 0)
5436 {
5437 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5438 "ignoring .debug_names."),
5439 filename, static_cast<unsigned long> (foreign_tu_count));
5440 return false;
5441 }
5442
5443 /* bucket_count - The number of hash buckets in the hash lookup
5444 table. */
5445 map.bucket_count = read_4_bytes (abfd, addr);
5446 addr += 4;
5447
5448 /* name_count - The number of unique names in the index. */
5449 map.name_count = read_4_bytes (abfd, addr);
5450 addr += 4;
5451
5452 /* abbrev_table_size - The size in bytes of the abbreviations
5453 table. */
5454 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5455 addr += 4;
5456
5457 /* augmentation_string_size - The size in bytes of the augmentation
5458 string. This value is rounded up to a multiple of 4. */
5459 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5460 addr += 4;
5461 map.augmentation_is_gdb = ((augmentation_string_size
5462 == sizeof (dwarf5_augmentation))
5463 && memcmp (addr, dwarf5_augmentation,
5464 sizeof (dwarf5_augmentation)) == 0);
5465 augmentation_string_size += (-augmentation_string_size) & 3;
5466 addr += augmentation_string_size;
5467
5468 /* List of CUs */
5469 map.cu_table_reordered = addr;
5470 addr += map.cu_count * map.offset_size;
5471
5472 /* List of Local TUs */
5473 map.tu_table_reordered = addr;
5474 addr += map.tu_count * map.offset_size;
5475
5476 /* Hash Lookup Table */
5477 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5478 addr += map.bucket_count * 4;
5479 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5480 addr += map.name_count * 4;
5481
5482 /* Name Table */
5483 map.name_table_string_offs_reordered = addr;
5484 addr += map.name_count * map.offset_size;
5485 map.name_table_entry_offs_reordered = addr;
5486 addr += map.name_count * map.offset_size;
5487
5488 const gdb_byte *abbrev_table_start = addr;
5489 for (;;)
5490 {
927aa2e7
JK
5491 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5492 addr += bytes_read;
5493 if (index_num == 0)
5494 break;
5495
5496 const auto insertpair
5497 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5498 if (!insertpair.second)
5499 {
5500 warning (_("Section .debug_names in %s has duplicate index %s, "
5501 "ignoring .debug_names."),
5502 filename, pulongest (index_num));
5503 return false;
5504 }
5505 mapped_debug_names::index_val &indexval = insertpair.first->second;
5506 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5507 addr += bytes_read;
5508
5509 for (;;)
5510 {
5511 mapped_debug_names::index_val::attr attr;
5512 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5513 addr += bytes_read;
5514 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5515 addr += bytes_read;
5516 if (attr.form == DW_FORM_implicit_const)
5517 {
5518 attr.implicit_const = read_signed_leb128 (abfd, addr,
5519 &bytes_read);
5520 addr += bytes_read;
5521 }
5522 if (attr.dw_idx == 0 && attr.form == 0)
5523 break;
5524 indexval.attr_vec.push_back (std::move (attr));
5525 }
5526 }
5527 if (addr != abbrev_table_start + abbrev_table_size)
5528 {
5529 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5530 "of size %s vs. written as %u, ignoring .debug_names."),
5531 filename, plongest (addr - abbrev_table_start),
5532 abbrev_table_size);
927aa2e7
JK
5533 return false;
5534 }
5535 map.entry_pool = addr;
5536
5537 return true;
5538}
5539
5540/* A helper for create_cus_from_debug_names that handles the MAP's CU
5541 list. */
5542
5543static void
ed2dc618 5544create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5545 const mapped_debug_names &map,
5546 dwarf2_section_info &section,
b76e467d 5547 bool is_dwz)
927aa2e7
JK
5548{
5549 sect_offset sect_off_prev;
5550 for (uint32_t i = 0; i <= map.cu_count; ++i)
5551 {
5552 sect_offset sect_off_next;
5553 if (i < map.cu_count)
5554 {
5555 sect_off_next
5556 = (sect_offset) (extract_unsigned_integer
5557 (map.cu_table_reordered + i * map.offset_size,
5558 map.offset_size,
5559 map.dwarf5_byte_order));
5560 }
5561 else
5562 sect_off_next = (sect_offset) section.size;
5563 if (i >= 1)
5564 {
5565 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5566 dwarf2_per_cu_data *per_cu
ed2dc618 5567 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5568 sect_off_prev, length);
b76e467d 5569 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5570 }
5571 sect_off_prev = sect_off_next;
5572 }
5573}
5574
5575/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5576 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5577
5578static void
ed2dc618 5579create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5580 const mapped_debug_names &map,
5581 const mapped_debug_names &dwz_map)
5582{
b76e467d
SM
5583 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5584 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5585
ed2dc618
SM
5586 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5587 dwarf2_per_objfile->info,
b76e467d 5588 false /* is_dwz */);
927aa2e7
JK
5589
5590 if (dwz_map.cu_count == 0)
5591 return;
5592
ed2dc618
SM
5593 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5594 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5595 true /* is_dwz */);
927aa2e7
JK
5596}
5597
5598/* Read .debug_names. If everything went ok, initialize the "quick"
5599 elements of all the CUs and return true. Otherwise, return false. */
5600
5601static bool
ed2dc618 5602dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5603{
22ca247e
TT
5604 std::unique_ptr<mapped_debug_names> map
5605 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5606 mapped_debug_names dwz_map (dwarf2_per_objfile);
5607 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5608
5609 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5610 &dwarf2_per_objfile->debug_names,
22ca247e 5611 *map))
927aa2e7
JK
5612 return false;
5613
5614 /* Don't use the index if it's empty. */
22ca247e 5615 if (map->name_count == 0)
927aa2e7
JK
5616 return false;
5617
5618 /* If there is a .dwz file, read it so we can get its CU list as
5619 well. */
ed2dc618 5620 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5621 if (dwz != NULL)
5622 {
5623 if (!read_debug_names_from_section (objfile,
00f93c44 5624 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5625 &dwz->debug_names, dwz_map))
5626 {
5627 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5628 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5629 return false;
5630 }
5631 }
5632
22ca247e 5633 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5634
22ca247e 5635 if (map->tu_count != 0)
927aa2e7
JK
5636 {
5637 /* We can only handle a single .debug_types when we have an
5638 index. */
fd5866f6 5639 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5640 return false;
5641
fd5866f6 5642 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5643
5644 create_signatured_type_table_from_debug_names
22ca247e 5645 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5646 }
5647
ed2dc618
SM
5648 create_addrmap_from_aranges (dwarf2_per_objfile,
5649 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5650
22ca247e 5651 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5652 dwarf2_per_objfile->using_index = 1;
5653 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5654 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5655
5656 return true;
5657}
5658
927aa2e7
JK
5659/* Type used to manage iterating over all CUs looking for a symbol for
5660 .debug_names. */
5661
5662class dw2_debug_names_iterator
5663{
5664public:
927aa2e7 5665 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5666 gdb::optional<block_enum> block_index,
5667 domain_enum domain,
927aa2e7 5668 const char *name)
2b79f376 5669 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5670 m_addr (find_vec_in_debug_names (map, name))
5671 {}
5672
5673 dw2_debug_names_iterator (const mapped_debug_names &map,
5674 search_domain search, uint32_t namei)
5675 : m_map (map),
5676 m_search (search),
5677 m_addr (find_vec_in_debug_names (map, namei))
5678 {}
5679
3b00ef10
TT
5680 dw2_debug_names_iterator (const mapped_debug_names &map,
5681 block_enum block_index, domain_enum domain,
5682 uint32_t namei)
5683 : m_map (map), m_block_index (block_index), m_domain (domain),
5684 m_addr (find_vec_in_debug_names (map, namei))
5685 {}
5686
927aa2e7
JK
5687 /* Return the next matching CU or NULL if there are no more. */
5688 dwarf2_per_cu_data *next ();
5689
5690private:
5691 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5692 const char *name);
5693 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5694 uint32_t namei);
5695
5696 /* The internalized form of .debug_names. */
5697 const mapped_debug_names &m_map;
5698
2b79f376
SM
5699 /* If set, only look for symbols that match that block. Valid values are
5700 GLOBAL_BLOCK and STATIC_BLOCK. */
5701 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5702
5703 /* The kind of symbol we're looking for. */
5704 const domain_enum m_domain = UNDEF_DOMAIN;
5705 const search_domain m_search = ALL_DOMAIN;
5706
5707 /* The list of CUs from the index entry of the symbol, or NULL if
5708 not found. */
5709 const gdb_byte *m_addr;
5710};
5711
5712const char *
5713mapped_debug_names::namei_to_name (uint32_t namei) const
5714{
5715 const ULONGEST namei_string_offs
5716 = extract_unsigned_integer ((name_table_string_offs_reordered
5717 + namei * offset_size),
5718 offset_size,
5719 dwarf5_byte_order);
5720 return read_indirect_string_at_offset
ed2dc618 5721 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5722}
5723
5724/* Find a slot in .debug_names for the object named NAME. If NAME is
5725 found, return pointer to its pool data. If NAME cannot be found,
5726 return NULL. */
5727
5728const gdb_byte *
5729dw2_debug_names_iterator::find_vec_in_debug_names
5730 (const mapped_debug_names &map, const char *name)
5731{
5732 int (*cmp) (const char *, const char *);
5733
54ee4252 5734 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5735 if (current_language->la_language == language_cplus
5736 || current_language->la_language == language_fortran
5737 || current_language->la_language == language_d)
5738 {
5739 /* NAME is already canonical. Drop any qualifiers as
5740 .debug_names does not contain any. */
5741
5742 if (strchr (name, '(') != NULL)
5743 {
54ee4252 5744 without_params = cp_remove_params (name);
927aa2e7 5745 if (without_params != NULL)
54ee4252 5746 name = without_params.get ();
927aa2e7
JK
5747 }
5748 }
5749
5750 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5751
5752 const uint32_t full_hash = dwarf5_djb_hash (name);
5753 uint32_t namei
5754 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5755 (map.bucket_table_reordered
5756 + (full_hash % map.bucket_count)), 4,
5757 map.dwarf5_byte_order);
5758 if (namei == 0)
5759 return NULL;
5760 --namei;
5761 if (namei >= map.name_count)
5762 {
b98664d3 5763 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5764 "[in module %s]"),
5765 namei, map.name_count,
ed2dc618 5766 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5767 return NULL;
5768 }
5769
5770 for (;;)
5771 {
5772 const uint32_t namei_full_hash
5773 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5774 (map.hash_table_reordered + namei), 4,
5775 map.dwarf5_byte_order);
5776 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5777 return NULL;
5778
5779 if (full_hash == namei_full_hash)
5780 {
5781 const char *const namei_string = map.namei_to_name (namei);
5782
5783#if 0 /* An expensive sanity check. */
5784 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5785 {
b98664d3 5786 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5787 "[in module %s]"),
5788 namei, objfile_name (dwarf2_per_objfile->objfile));
5789 return NULL;
5790 }
5791#endif
5792
5793 if (cmp (namei_string, name) == 0)
5794 {
5795 const ULONGEST namei_entry_offs
5796 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5797 + namei * map.offset_size),
5798 map.offset_size, map.dwarf5_byte_order);
5799 return map.entry_pool + namei_entry_offs;
5800 }
5801 }
5802
5803 ++namei;
5804 if (namei >= map.name_count)
5805 return NULL;
5806 }
5807}
5808
5809const gdb_byte *
5810dw2_debug_names_iterator::find_vec_in_debug_names
5811 (const mapped_debug_names &map, uint32_t namei)
5812{
5813 if (namei >= map.name_count)
5814 {
b98664d3 5815 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5816 "[in module %s]"),
5817 namei, map.name_count,
ed2dc618 5818 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5819 return NULL;
5820 }
5821
5822 const ULONGEST namei_entry_offs
5823 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5824 + namei * map.offset_size),
5825 map.offset_size, map.dwarf5_byte_order);
5826 return map.entry_pool + namei_entry_offs;
5827}
5828
5829/* See dw2_debug_names_iterator. */
5830
5831dwarf2_per_cu_data *
5832dw2_debug_names_iterator::next ()
5833{
5834 if (m_addr == NULL)
5835 return NULL;
5836
ed2dc618
SM
5837 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5838 struct objfile *objfile = dwarf2_per_objfile->objfile;
5839 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5840
5841 again:
5842
5843 unsigned int bytes_read;
5844 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5845 m_addr += bytes_read;
5846 if (abbrev == 0)
5847 return NULL;
5848
5849 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5850 if (indexval_it == m_map.abbrev_map.cend ())
5851 {
b98664d3 5852 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5853 "[in module %s]"),
ed2dc618 5854 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5855 return NULL;
5856 }
5857 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5858 enum class symbol_linkage {
5859 unknown,
5860 static_,
5861 extern_,
23c13d42 5862 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5863 dwarf2_per_cu_data *per_cu = NULL;
5864 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5865 {
5866 ULONGEST ull;
5867 switch (attr.form)
5868 {
5869 case DW_FORM_implicit_const:
5870 ull = attr.implicit_const;
5871 break;
5872 case DW_FORM_flag_present:
5873 ull = 1;
5874 break;
5875 case DW_FORM_udata:
5876 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5877 m_addr += bytes_read;
5878 break;
5879 default:
b98664d3 5880 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5881 dwarf_form_name (attr.form),
ed2dc618 5882 objfile_name (objfile));
927aa2e7
JK
5883 return NULL;
5884 }
5885 switch (attr.dw_idx)
5886 {
5887 case DW_IDX_compile_unit:
5888 /* Don't crash on bad data. */
b76e467d 5889 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5890 {
b98664d3 5891 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5892 " [in module %s]"),
5893 pulongest (ull),
5894 objfile_name (dwarf2_per_objfile->objfile));
5895 continue;
5896 }
ff4c9fec 5897 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5898 break;
8af5c486
JK
5899 case DW_IDX_type_unit:
5900 /* Don't crash on bad data. */
b2bdb8cf 5901 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5902 {
b98664d3 5903 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5904 " [in module %s]"),
5905 pulongest (ull),
5906 objfile_name (dwarf2_per_objfile->objfile));
5907 continue;
5908 }
ff4c9fec 5909 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5910 break;
927aa2e7
JK
5911 case DW_IDX_GNU_internal:
5912 if (!m_map.augmentation_is_gdb)
5913 break;
23c13d42 5914 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5915 break;
5916 case DW_IDX_GNU_external:
5917 if (!m_map.augmentation_is_gdb)
5918 break;
23c13d42 5919 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5920 break;
5921 }
5922 }
5923
5924 /* Skip if already read in. */
5925 if (per_cu->v.quick->compunit_symtab)
5926 goto again;
5927
5928 /* Check static vs global. */
23c13d42 5929 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5930 {
2b79f376 5931 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5932 const bool symbol_is_static =
5933 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5934 if (want_static != symbol_is_static)
2b79f376 5935 goto again;
927aa2e7
JK
5936 }
5937
5938 /* Match dw2_symtab_iter_next, symbol_kind
5939 and debug_names::psymbol_tag. */
5940 switch (m_domain)
5941 {
5942 case VAR_DOMAIN:
5943 switch (indexval.dwarf_tag)
5944 {
5945 case DW_TAG_variable:
5946 case DW_TAG_subprogram:
5947 /* Some types are also in VAR_DOMAIN. */
5948 case DW_TAG_typedef:
5949 case DW_TAG_structure_type:
5950 break;
5951 default:
5952 goto again;
5953 }
5954 break;
5955 case STRUCT_DOMAIN:
5956 switch (indexval.dwarf_tag)
5957 {
5958 case DW_TAG_typedef:
5959 case DW_TAG_structure_type:
5960 break;
5961 default:
5962 goto again;
5963 }
5964 break;
5965 case LABEL_DOMAIN:
5966 switch (indexval.dwarf_tag)
5967 {
5968 case 0:
5969 case DW_TAG_variable:
5970 break;
5971 default:
5972 goto again;
5973 }
5974 break;
59c35742
AB
5975 case MODULE_DOMAIN:
5976 switch (indexval.dwarf_tag)
5977 {
5978 case DW_TAG_module:
5979 break;
5980 default:
5981 goto again;
5982 }
5983 break;
927aa2e7
JK
5984 default:
5985 break;
5986 }
5987
5988 /* Match dw2_expand_symtabs_matching, symbol_kind and
5989 debug_names::psymbol_tag. */
5990 switch (m_search)
4b514bc8 5991 {
927aa2e7
JK
5992 case VARIABLES_DOMAIN:
5993 switch (indexval.dwarf_tag)
4b514bc8 5994 {
927aa2e7
JK
5995 case DW_TAG_variable:
5996 break;
5997 default:
5998 goto again;
4b514bc8 5999 }
927aa2e7
JK
6000 break;
6001 case FUNCTIONS_DOMAIN:
6002 switch (indexval.dwarf_tag)
4b514bc8 6003 {
927aa2e7
JK
6004 case DW_TAG_subprogram:
6005 break;
6006 default:
6007 goto again;
4b514bc8 6008 }
927aa2e7
JK
6009 break;
6010 case TYPES_DOMAIN:
6011 switch (indexval.dwarf_tag)
6012 {
6013 case DW_TAG_typedef:
6014 case DW_TAG_structure_type:
6015 break;
6016 default:
6017 goto again;
6018 }
6019 break;
59c35742
AB
6020 case MODULES_DOMAIN:
6021 switch (indexval.dwarf_tag)
6022 {
6023 case DW_TAG_module:
6024 break;
6025 default:
6026 goto again;
6027 }
927aa2e7
JK
6028 default:
6029 break;
4b514bc8 6030 }
927aa2e7
JK
6031
6032 return per_cu;
4b514bc8 6033}
61920122 6034
927aa2e7 6035static struct compunit_symtab *
c7f839cb 6036dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6037 const char *name, domain_enum domain)
4b514bc8 6038{
ed2dc618
SM
6039 struct dwarf2_per_objfile *dwarf2_per_objfile
6040 = get_dwarf2_per_objfile (objfile);
61920122 6041
927aa2e7
JK
6042 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6043 if (!mapp)
61920122 6044 {
927aa2e7
JK
6045 /* index is NULL if OBJF_READNOW. */
6046 return NULL;
6047 }
6048 const auto &map = *mapp;
9291a0cd 6049
2b79f376 6050 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6051
927aa2e7
JK
6052 struct compunit_symtab *stab_best = NULL;
6053 struct dwarf2_per_cu_data *per_cu;
6054 while ((per_cu = iter.next ()) != NULL)
6055 {
6056 struct symbol *sym, *with_opaque = NULL;
58f0c718 6057 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6058 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6059 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6060
927aa2e7
JK
6061 sym = block_find_symbol (block, name, domain,
6062 block_find_non_opaque_type_preferred,
6063 &with_opaque);
9703b513 6064
927aa2e7
JK
6065 /* Some caution must be observed with overloaded functions and
6066 methods, since the index will not contain any overload
6067 information (but NAME might contain it). */
a3ec0bb1 6068
927aa2e7 6069 if (sym != NULL
987012b8 6070 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
6071 return stab;
6072 if (with_opaque != NULL
987012b8 6073 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 6074 stab_best = stab;
9703b513 6075
927aa2e7 6076 /* Keep looking through other CUs. */
9703b513
TT
6077 }
6078
927aa2e7 6079 return stab_best;
9703b513
TT
6080}
6081
927aa2e7
JK
6082/* This dumps minimal information about .debug_names. It is called
6083 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6084 uses this to verify that .debug_names has been loaded. */
9291a0cd 6085
927aa2e7
JK
6086static void
6087dw2_debug_names_dump (struct objfile *objfile)
6088{
ed2dc618
SM
6089 struct dwarf2_per_objfile *dwarf2_per_objfile
6090 = get_dwarf2_per_objfile (objfile);
6091
927aa2e7
JK
6092 gdb_assert (dwarf2_per_objfile->using_index);
6093 printf_filtered (".debug_names:");
6094 if (dwarf2_per_objfile->debug_names_table)
6095 printf_filtered (" exists\n");
6096 else
6097 printf_filtered (" faked for \"readnow\"\n");
6098 printf_filtered ("\n");
9291a0cd
TT
6099}
6100
9291a0cd 6101static void
927aa2e7
JK
6102dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6103 const char *func_name)
9291a0cd 6104{
ed2dc618
SM
6105 struct dwarf2_per_objfile *dwarf2_per_objfile
6106 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6107
927aa2e7
JK
6108 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6109 if (dwarf2_per_objfile->debug_names_table)
24c79950 6110 {
927aa2e7 6111 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6112
2b79f376 6113 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6114
927aa2e7
JK
6115 struct dwarf2_per_cu_data *per_cu;
6116 while ((per_cu = iter.next ()) != NULL)
58f0c718 6117 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6118 }
6119}
24c79950 6120
3b00ef10
TT
6121static void
6122dw2_debug_names_map_matching_symbols
6123 (struct objfile *objfile,
6124 const lookup_name_info &name, domain_enum domain,
6125 int global,
6126 gdb::function_view<symbol_found_callback_ftype> callback,
6127 symbol_compare_ftype *ordered_compare)
6128{
6129 struct dwarf2_per_objfile *dwarf2_per_objfile
6130 = get_dwarf2_per_objfile (objfile);
6131
6132 /* debug_names_table is NULL if OBJF_READNOW. */
6133 if (!dwarf2_per_objfile->debug_names_table)
6134 return;
6135
6136 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6137 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6138
6139 const char *match_name = name.ada ().lookup_name ().c_str ();
6140 auto matcher = [&] (const char *symname)
6141 {
6142 if (ordered_compare == nullptr)
6143 return true;
6144 return ordered_compare (symname, match_name) == 0;
6145 };
6146
6147 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6148 [&] (offset_type namei)
6149 {
6150 /* The name was matched, now expand corresponding CUs that were
6151 marked. */
6152 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6153
6154 struct dwarf2_per_cu_data *per_cu;
6155 while ((per_cu = iter.next ()) != NULL)
6156 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6157 return true;
6158 });
6159
6160 /* It's a shame we couldn't do this inside the
6161 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6162 that have already been expanded. Instead, this loop matches what
6163 the psymtab code does. */
6164 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6165 {
6166 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6167 if (cust != nullptr)
6168 {
6169 const struct block *block
6170 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6171 if (!iterate_over_symbols_terminated (block, name,
6172 domain, callback))
6173 break;
6174 }
6175 }
6176}
6177
927aa2e7
JK
6178static void
6179dw2_debug_names_expand_symtabs_matching
6180 (struct objfile *objfile,
6181 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6182 const lookup_name_info &lookup_name,
6183 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6184 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6185 enum search_domain kind)
6186{
ed2dc618
SM
6187 struct dwarf2_per_objfile *dwarf2_per_objfile
6188 = get_dwarf2_per_objfile (objfile);
9291a0cd 6189
927aa2e7
JK
6190 /* debug_names_table is NULL if OBJF_READNOW. */
6191 if (!dwarf2_per_objfile->debug_names_table)
6192 return;
9291a0cd 6193
ed2dc618 6194 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6195
44ed8f3e 6196 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6197
44ed8f3e
PA
6198 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6199 symbol_matcher,
6200 kind, [&] (offset_type namei)
927aa2e7 6201 {
927aa2e7
JK
6202 /* The name was matched, now expand corresponding CUs that were
6203 marked. */
6204 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6205
927aa2e7
JK
6206 struct dwarf2_per_cu_data *per_cu;
6207 while ((per_cu = iter.next ()) != NULL)
6208 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6209 expansion_notify);
3b00ef10 6210 return true;
44ed8f3e 6211 });
9291a0cd
TT
6212}
6213
927aa2e7 6214const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6215{
6216 dw2_has_symbols,
6217 dw2_find_last_source_symtab,
6218 dw2_forget_cached_source_info,
f8eba3c6 6219 dw2_map_symtabs_matching_filename,
927aa2e7 6220 dw2_debug_names_lookup_symbol,
9291a0cd 6221 dw2_print_stats,
927aa2e7 6222 dw2_debug_names_dump,
927aa2e7 6223 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6224 dw2_expand_all_symtabs,
652a8996 6225 dw2_expand_symtabs_with_fullname,
3b00ef10 6226 dw2_debug_names_map_matching_symbols,
927aa2e7 6227 dw2_debug_names_expand_symtabs_matching,
43f3e411 6228 dw2_find_pc_sect_compunit_symtab,
71a3c369 6229 NULL,
9291a0cd
TT
6230 dw2_map_symbol_filenames
6231};
6232
4485a1c1
SM
6233/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6234 to either a dwarf2_per_objfile or dwz_file object. */
6235
6236template <typename T>
6237static gdb::array_view<const gdb_byte>
6238get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6239{
6240 dwarf2_section_info *section = &section_owner->gdb_index;
6241
6242 if (dwarf2_section_empty_p (section))
6243 return {};
6244
6245 /* Older elfutils strip versions could keep the section in the main
6246 executable while splitting it for the separate debug info file. */
6247 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6248 return {};
6249
6250 dwarf2_read_section (obj, section);
6251
8bebfcda
PA
6252 /* dwarf2_section_info::size is a bfd_size_type, while
6253 gdb::array_view works with size_t. On 32-bit hosts, with
6254 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6255 is 32-bit. So we need an explicit narrowing conversion here.
6256 This is fine, because it's impossible to allocate or mmap an
6257 array/buffer larger than what size_t can represent. */
6258 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6259}
6260
87d6a7aa
SM
6261/* Lookup the index cache for the contents of the index associated to
6262 DWARF2_OBJ. */
6263
6264static gdb::array_view<const gdb_byte>
6265get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6266{
6267 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6268 if (build_id == nullptr)
6269 return {};
6270
6271 return global_index_cache.lookup_gdb_index (build_id,
6272 &dwarf2_obj->index_cache_res);
6273}
6274
6275/* Same as the above, but for DWZ. */
6276
6277static gdb::array_view<const gdb_byte>
6278get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6279{
6280 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6281 if (build_id == nullptr)
6282 return {};
6283
6284 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6285}
6286
3c0aa29a 6287/* See symfile.h. */
9291a0cd 6288
3c0aa29a
PA
6289bool
6290dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6291{
ed2dc618
SM
6292 struct dwarf2_per_objfile *dwarf2_per_objfile
6293 = get_dwarf2_per_objfile (objfile);
6294
9291a0cd
TT
6295 /* If we're about to read full symbols, don't bother with the
6296 indices. In this case we also don't care if some other debug
6297 format is making psymtabs, because they are all about to be
6298 expanded anyway. */
6299 if ((objfile->flags & OBJF_READNOW))
6300 {
9291a0cd 6301 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6302 create_all_comp_units (dwarf2_per_objfile);
6303 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6304 dwarf2_per_objfile->quick_file_names_table
6305 = create_quick_file_names_table
6306 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6307
b76e467d 6308 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6309 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6310 {
ff4c9fec 6311 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6312
e254ef6a
DE
6313 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6314 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6315 }
6316
6317 /* Return 1 so that gdb sees the "quick" functions. However,
6318 these functions will be no-ops because we will have expanded
6319 all symtabs. */
3c0aa29a
PA
6320 *index_kind = dw_index_kind::GDB_INDEX;
6321 return true;
9291a0cd
TT
6322 }
6323
ed2dc618 6324 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6325 {
6326 *index_kind = dw_index_kind::DEBUG_NAMES;
6327 return true;
6328 }
927aa2e7 6329
4485a1c1
SM
6330 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6331 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6332 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6333 {
6334 *index_kind = dw_index_kind::GDB_INDEX;
6335 return true;
6336 }
9291a0cd 6337
87d6a7aa
SM
6338 /* ... otherwise, try to find the index in the index cache. */
6339 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6340 get_gdb_index_contents_from_cache,
6341 get_gdb_index_contents_from_cache_dwz))
6342 {
6343 global_index_cache.hit ();
6344 *index_kind = dw_index_kind::GDB_INDEX;
6345 return true;
6346 }
6347
6348 global_index_cache.miss ();
3c0aa29a 6349 return false;
9291a0cd
TT
6350}
6351
6352\f
6353
dce234bc
PP
6354/* Build a partial symbol table. */
6355
6356void
f29dff0a 6357dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6358{
ed2dc618
SM
6359 struct dwarf2_per_objfile *dwarf2_per_objfile
6360 = get_dwarf2_per_objfile (objfile);
c9bf0622 6361
6eee24ce 6362 init_psymbol_list (objfile, 1024);
c906108c 6363
a70b8144 6364 try
c9bf0622
TT
6365 {
6366 /* This isn't really ideal: all the data we allocate on the
6367 objfile's obstack is still uselessly kept around. However,
6368 freeing it seems unsafe. */
906768f9 6369 psymtab_discarder psymtabs (objfile);
ed2dc618 6370 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6371 psymtabs.keep ();
87d6a7aa
SM
6372
6373 /* (maybe) store an index in the cache. */
6374 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6375 }
230d2906 6376 catch (const gdb_exception_error &except)
492d29ea
PA
6377 {
6378 exception_print (gdb_stderr, except);
6379 }
c906108c 6380}
c906108c 6381
1ce1cefd
DE
6382/* Return the total length of the CU described by HEADER. */
6383
6384static unsigned int
6385get_cu_length (const struct comp_unit_head *header)
6386{
6387 return header->initial_length_size + header->length;
6388}
6389
9c541725 6390/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6391
9c541725
PA
6392static inline bool
6393offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6394{
9c541725
PA
6395 sect_offset bottom = cu_header->sect_off;
6396 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6397
9c541725 6398 return sect_off >= bottom && sect_off < top;
45452591
DE
6399}
6400
3b80fe9b
DE
6401/* Find the base address of the compilation unit for range lists and
6402 location lists. It will normally be specified by DW_AT_low_pc.
6403 In DWARF-3 draft 4, the base address could be overridden by
6404 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6405 compilation units with discontinuous ranges. */
6406
6407static void
6408dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6409{
6410 struct attribute *attr;
6411
6412 cu->base_known = 0;
6413 cu->base_address = 0;
6414
6415 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6416 if (attr != nullptr)
3b80fe9b 6417 {
31aa7e4e 6418 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6419 cu->base_known = 1;
6420 }
6421 else
6422 {
6423 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6424 if (attr != nullptr)
3b80fe9b 6425 {
31aa7e4e 6426 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6427 cu->base_known = 1;
6428 }
6429 }
6430}
6431
93311388 6432/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6433 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6434 NOTE: This leaves members offset, first_die_offset to be filled in
6435 by the caller. */
107d2387 6436
d521ce57 6437static const gdb_byte *
107d2387 6438read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6439 const gdb_byte *info_ptr,
6440 struct dwarf2_section_info *section,
6441 rcuh_kind section_kind)
107d2387
AC
6442{
6443 int signed_addr;
891d2f0b 6444 unsigned int bytes_read;
43988095
JK
6445 const char *filename = get_section_file_name (section);
6446 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6447
6448 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6449 cu_header->initial_length_size = bytes_read;
6450 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6451 info_ptr += bytes_read;
107d2387 6452 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6453 if (cu_header->version < 2 || cu_header->version > 5)
6454 error (_("Dwarf Error: wrong version in compilation unit header "
6455 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6456 cu_header->version, filename);
107d2387 6457 info_ptr += 2;
43988095
JK
6458 if (cu_header->version < 5)
6459 switch (section_kind)
6460 {
6461 case rcuh_kind::COMPILE:
6462 cu_header->unit_type = DW_UT_compile;
6463 break;
6464 case rcuh_kind::TYPE:
6465 cu_header->unit_type = DW_UT_type;
6466 break;
6467 default:
6468 internal_error (__FILE__, __LINE__,
6469 _("read_comp_unit_head: invalid section_kind"));
6470 }
6471 else
6472 {
6473 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6474 (read_1_byte (abfd, info_ptr));
6475 info_ptr += 1;
6476 switch (cu_header->unit_type)
6477 {
6478 case DW_UT_compile:
a084a2a6
AT
6479 case DW_UT_partial:
6480 case DW_UT_skeleton:
6481 case DW_UT_split_compile:
43988095
JK
6482 if (section_kind != rcuh_kind::COMPILE)
6483 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6484 "(is %s, should be %s) [in module %s]"),
6485 dwarf_unit_type_name (cu_header->unit_type),
6486 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6487 break;
6488 case DW_UT_type:
a084a2a6 6489 case DW_UT_split_type:
43988095
JK
6490 section_kind = rcuh_kind::TYPE;
6491 break;
6492 default:
6493 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6494 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6495 "[in module %s]"), cu_header->unit_type,
6496 dwarf_unit_type_name (DW_UT_compile),
6497 dwarf_unit_type_name (DW_UT_skeleton),
6498 dwarf_unit_type_name (DW_UT_split_compile),
6499 dwarf_unit_type_name (DW_UT_type),
6500 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6501 }
6502
6503 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6504 info_ptr += 1;
6505 }
9c541725
PA
6506 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6507 cu_header,
6508 &bytes_read);
613e1657 6509 info_ptr += bytes_read;
43988095
JK
6510 if (cu_header->version < 5)
6511 {
6512 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6513 info_ptr += 1;
6514 }
107d2387
AC
6515 signed_addr = bfd_get_sign_extend_vma (abfd);
6516 if (signed_addr < 0)
8e65ff28 6517 internal_error (__FILE__, __LINE__,
e2e0b3e5 6518 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6519 cu_header->signed_addr_p = signed_addr;
c764a876 6520
a084a2a6
AT
6521 bool header_has_signature = section_kind == rcuh_kind::TYPE
6522 || cu_header->unit_type == DW_UT_skeleton
6523 || cu_header->unit_type == DW_UT_split_compile;
43988095 6524
a084a2a6
AT
6525 if (header_has_signature)
6526 {
43988095
JK
6527 cu_header->signature = read_8_bytes (abfd, info_ptr);
6528 info_ptr += 8;
a084a2a6 6529 }
43988095 6530
a084a2a6
AT
6531 if (section_kind == rcuh_kind::TYPE)
6532 {
6533 LONGEST type_offset;
43988095
JK
6534 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6535 info_ptr += bytes_read;
9c541725
PA
6536 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6537 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6538 error (_("Dwarf Error: Too big type_offset in compilation unit "
6539 "header (is %s) [in module %s]"), plongest (type_offset),
6540 filename);
6541 }
6542
107d2387
AC
6543 return info_ptr;
6544}
6545
36586728
TT
6546/* Helper function that returns the proper abbrev section for
6547 THIS_CU. */
6548
6549static struct dwarf2_section_info *
6550get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6551{
6552 struct dwarf2_section_info *abbrev;
ed2dc618 6553 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6554
6555 if (this_cu->is_dwz)
ed2dc618 6556 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6557 else
6558 abbrev = &dwarf2_per_objfile->abbrev;
6559
6560 return abbrev;
6561}
6562
9ff913ba
DE
6563/* Subroutine of read_and_check_comp_unit_head and
6564 read_and_check_type_unit_head to simplify them.
6565 Perform various error checking on the header. */
6566
6567static void
ed2dc618
SM
6568error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6569 struct comp_unit_head *header,
4bdcc0c1
DE
6570 struct dwarf2_section_info *section,
6571 struct dwarf2_section_info *abbrev_section)
9ff913ba 6572{
a32a8923 6573 const char *filename = get_section_file_name (section);
9ff913ba 6574
9c541725 6575 if (to_underlying (header->abbrev_sect_off)
36586728 6576 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6577 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6578 "(offset %s + 6) [in module %s]"),
6579 sect_offset_str (header->abbrev_sect_off),
6580 sect_offset_str (header->sect_off),
9ff913ba
DE
6581 filename);
6582
9c541725 6583 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6584 avoid potential 32-bit overflow. */
9c541725 6585 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6586 > section->size)
9c541725 6587 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6588 "(offset %s + 0) [in module %s]"),
6589 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6590 filename);
6591}
6592
6593/* Read in a CU/TU header and perform some basic error checking.
6594 The contents of the header are stored in HEADER.
6595 The result is a pointer to the start of the first DIE. */
adabb602 6596
d521ce57 6597static const gdb_byte *
ed2dc618
SM
6598read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6599 struct comp_unit_head *header,
9ff913ba 6600 struct dwarf2_section_info *section,
4bdcc0c1 6601 struct dwarf2_section_info *abbrev_section,
d521ce57 6602 const gdb_byte *info_ptr,
43988095 6603 rcuh_kind section_kind)
72bf9492 6604{
d521ce57 6605 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6606
9c541725 6607 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6608
43988095 6609 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6610
9c541725 6611 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6612
ed2dc618
SM
6613 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6614 abbrev_section);
9ff913ba
DE
6615
6616 return info_ptr;
348e048f
DE
6617}
6618
f4dc4d17
DE
6619/* Fetch the abbreviation table offset from a comp or type unit header. */
6620
6621static sect_offset
ed2dc618
SM
6622read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6623 struct dwarf2_section_info *section,
9c541725 6624 sect_offset sect_off)
f4dc4d17 6625{
a32a8923 6626 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6627 const gdb_byte *info_ptr;
ac298888 6628 unsigned int initial_length_size, offset_size;
43988095 6629 uint16_t version;
f4dc4d17
DE
6630
6631 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6632 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6633 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6634 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6635 info_ptr += initial_length_size;
6636
6637 version = read_2_bytes (abfd, info_ptr);
6638 info_ptr += 2;
6639 if (version >= 5)
6640 {
6641 /* Skip unit type and address size. */
6642 info_ptr += 2;
6643 }
6644
9c541725 6645 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6646}
6647
aaa75496
JB
6648/* Allocate a new partial symtab for file named NAME and mark this new
6649 partial symtab as being an include of PST. */
6650
6651static void
d521ce57 6652dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6653 struct objfile *objfile)
6654{
6655 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6656
fbd9ab74
JK
6657 if (!IS_ABSOLUTE_PATH (subpst->filename))
6658 {
6659 /* It shares objfile->objfile_obstack. */
6660 subpst->dirname = pst->dirname;
6661 }
6662
a9342b62 6663 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6664 subpst->dependencies[0] = pst;
6665 subpst->number_of_dependencies = 1;
6666
aaa75496 6667 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6668
6669 /* No private part is necessary for include psymtabs. This property
6670 can be used to differentiate between such include psymtabs and
10b3939b 6671 the regular ones. */
58a9656e 6672 subpst->read_symtab_private = NULL;
aaa75496
JB
6673}
6674
6675/* Read the Line Number Program data and extract the list of files
6676 included by the source file represented by PST. Build an include
d85a05f0 6677 partial symtab for each of these included files. */
aaa75496
JB
6678
6679static void
6680dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6681 struct die_info *die,
6682 struct partial_symtab *pst)
aaa75496 6683{
fff8551c 6684 line_header_up lh;
d85a05f0 6685 struct attribute *attr;
aaa75496 6686
d85a05f0 6687 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6688 if (attr != nullptr)
9c541725 6689 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6690 if (lh == NULL)
6691 return; /* No linetable, so no includes. */
6692
79748972
TT
6693 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6694 that we pass in the raw text_low here; that is ok because we're
6695 only decoding the line table to make include partial symtabs, and
6696 so the addresses aren't really used. */
4ae976d1 6697 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6698 pst->raw_text_low (), 1);
aaa75496
JB
6699}
6700
348e048f 6701static hashval_t
52dc124a 6702hash_signatured_type (const void *item)
348e048f 6703{
9a3c8263
SM
6704 const struct signatured_type *sig_type
6705 = (const struct signatured_type *) item;
9a619af0 6706
348e048f 6707 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6708 return sig_type->signature;
348e048f
DE
6709}
6710
6711static int
52dc124a 6712eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6713{
9a3c8263
SM
6714 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6715 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6716
348e048f
DE
6717 return lhs->signature == rhs->signature;
6718}
6719
1fd400ff
TT
6720/* Allocate a hash table for signatured types. */
6721
6722static htab_t
673bfd45 6723allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6724{
6725 return htab_create_alloc_ex (41,
52dc124a
DE
6726 hash_signatured_type,
6727 eq_signatured_type,
1fd400ff
TT
6728 NULL,
6729 &objfile->objfile_obstack,
6730 hashtab_obstack_allocate,
6731 dummy_obstack_deallocate);
6732}
6733
d467dd73 6734/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6735
6736static int
d467dd73 6737add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6738{
9a3c8263 6739 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6740 std::vector<signatured_type *> *all_type_units
6741 = (std::vector<signatured_type *> *) datum;
1fd400ff 6742
b2bdb8cf 6743 all_type_units->push_back (sigt);
1fd400ff
TT
6744
6745 return 1;
6746}
6747
78d4d2c5 6748/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6749 and fill them into TYPES_HTAB. It will process only type units,
6750 therefore DW_UT_type. */
c88ee1f0 6751
78d4d2c5 6752static void
ed2dc618
SM
6753create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6754 struct dwo_file *dwo_file,
43988095
JK
6755 dwarf2_section_info *section, htab_t &types_htab,
6756 rcuh_kind section_kind)
348e048f 6757{
3019eac3 6758 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6759 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6760 bfd *abfd;
6761 const gdb_byte *info_ptr, *end_ptr;
348e048f 6762
4bdcc0c1
DE
6763 abbrev_section = (dwo_file != NULL
6764 ? &dwo_file->sections.abbrev
6765 : &dwarf2_per_objfile->abbrev);
6766
b4f54984 6767 if (dwarf_read_debug)
43988095
JK
6768 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6769 get_section_name (section),
a32a8923 6770 get_section_file_name (abbrev_section));
09406207 6771
78d4d2c5
JK
6772 dwarf2_read_section (objfile, section);
6773 info_ptr = section->buffer;
348e048f 6774
78d4d2c5
JK
6775 if (info_ptr == NULL)
6776 return;
348e048f 6777
78d4d2c5
JK
6778 /* We can't set abfd until now because the section may be empty or
6779 not present, in which case the bfd is unknown. */
6780 abfd = get_section_bfd_owner (section);
348e048f 6781
78d4d2c5
JK
6782 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6783 because we don't need to read any dies: the signature is in the
6784 header. */
3019eac3 6785
78d4d2c5
JK
6786 end_ptr = info_ptr + section->size;
6787 while (info_ptr < end_ptr)
6788 {
78d4d2c5
JK
6789 struct signatured_type *sig_type;
6790 struct dwo_unit *dwo_tu;
6791 void **slot;
6792 const gdb_byte *ptr = info_ptr;
6793 struct comp_unit_head header;
6794 unsigned int length;
8b70b953 6795
9c541725 6796 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6797
a49dd8dd
JK
6798 /* Initialize it due to a false compiler warning. */
6799 header.signature = -1;
9c541725 6800 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6801
78d4d2c5
JK
6802 /* We need to read the type's signature in order to build the hash
6803 table, but we don't need anything else just yet. */
348e048f 6804
ed2dc618 6805 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6806 abbrev_section, ptr, section_kind);
348e048f 6807
78d4d2c5 6808 length = get_cu_length (&header);
6caca83c 6809
78d4d2c5
JK
6810 /* Skip dummy type units. */
6811 if (ptr >= info_ptr + length
43988095
JK
6812 || peek_abbrev_code (abfd, ptr) == 0
6813 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6814 {
6815 info_ptr += length;
6816 continue;
6817 }
dee91e82 6818
78d4d2c5
JK
6819 if (types_htab == NULL)
6820 {
6821 if (dwo_file)
6822 types_htab = allocate_dwo_unit_table (objfile);
6823 else
6824 types_htab = allocate_signatured_type_table (objfile);
6825 }
8b70b953 6826
78d4d2c5
JK
6827 if (dwo_file)
6828 {
6829 sig_type = NULL;
6830 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6831 struct dwo_unit);
6832 dwo_tu->dwo_file = dwo_file;
43988095 6833 dwo_tu->signature = header.signature;
9c541725 6834 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6835 dwo_tu->section = section;
9c541725 6836 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6837 dwo_tu->length = length;
6838 }
6839 else
6840 {
6841 /* N.B.: type_offset is not usable if this type uses a DWO file.
6842 The real type_offset is in the DWO file. */
6843 dwo_tu = NULL;
6844 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6845 struct signatured_type);
43988095 6846 sig_type->signature = header.signature;
9c541725 6847 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6848 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6849 sig_type->per_cu.is_debug_types = 1;
6850 sig_type->per_cu.section = section;
9c541725 6851 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6852 sig_type->per_cu.length = length;
6853 }
6854
6855 slot = htab_find_slot (types_htab,
6856 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6857 INSERT);
6858 gdb_assert (slot != NULL);
6859 if (*slot != NULL)
6860 {
9c541725 6861 sect_offset dup_sect_off;
0349ea22 6862
3019eac3
DE
6863 if (dwo_file)
6864 {
78d4d2c5
JK
6865 const struct dwo_unit *dup_tu
6866 = (const struct dwo_unit *) *slot;
6867
9c541725 6868 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6869 }
6870 else
6871 {
78d4d2c5
JK
6872 const struct signatured_type *dup_tu
6873 = (const struct signatured_type *) *slot;
6874
9c541725 6875 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6876 }
8b70b953 6877
b98664d3 6878 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6879 " the entry at offset %s, signature %s"),
6880 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6881 hex_string (header.signature));
78d4d2c5
JK
6882 }
6883 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6884
78d4d2c5 6885 if (dwarf_read_debug > 1)
9d8780f0
SM
6886 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6887 sect_offset_str (sect_off),
43988095 6888 hex_string (header.signature));
3019eac3 6889
78d4d2c5
JK
6890 info_ptr += length;
6891 }
6892}
3019eac3 6893
78d4d2c5
JK
6894/* Create the hash table of all entries in the .debug_types
6895 (or .debug_types.dwo) section(s).
6896 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6897 otherwise it is NULL.
b3c8eb43 6898
78d4d2c5 6899 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6900
78d4d2c5 6901 Note: This function processes DWO files only, not DWP files. */
348e048f 6902
78d4d2c5 6903static void
ed2dc618
SM
6904create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6905 struct dwo_file *dwo_file,
fd5866f6 6906 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6907 htab_t &types_htab)
6908{
fd5866f6
SM
6909 for (dwarf2_section_info &section : type_sections)
6910 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6911 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6912}
6913
6914/* Create the hash table of all entries in the .debug_types section,
6915 and initialize all_type_units.
6916 The result is zero if there is an error (e.g. missing .debug_types section),
6917 otherwise non-zero. */
6918
6919static int
ed2dc618 6920create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6921{
78d4d2c5 6922 htab_t types_htab = NULL;
3019eac3 6923
ed2dc618
SM
6924 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6925 &dwarf2_per_objfile->info, types_htab,
43988095 6926 rcuh_kind::COMPILE);
ed2dc618
SM
6927 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6928 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6929 if (types_htab == NULL)
6930 {
6931 dwarf2_per_objfile->signatured_types = NULL;
6932 return 0;
6933 }
6934
348e048f
DE
6935 dwarf2_per_objfile->signatured_types = types_htab;
6936
b2bdb8cf
SM
6937 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6938 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6939
6940 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6941 &dwarf2_per_objfile->all_type_units);
1fd400ff 6942
348e048f
DE
6943 return 1;
6944}
6945
6aa5f3a6
DE
6946/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6947 If SLOT is non-NULL, it is the entry to use in the hash table.
6948 Otherwise we find one. */
6949
6950static struct signatured_type *
ed2dc618
SM
6951add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6952 void **slot)
6aa5f3a6
DE
6953{
6954 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6955
b2bdb8cf
SM
6956 if (dwarf2_per_objfile->all_type_units.size ()
6957 == dwarf2_per_objfile->all_type_units.capacity ())
6958 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6959
b2bdb8cf
SM
6960 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6961 struct signatured_type);
6962
6963 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6964 sig_type->signature = sig;
6965 sig_type->per_cu.is_debug_types = 1;
6966 if (dwarf2_per_objfile->using_index)
6967 {
6968 sig_type->per_cu.v.quick =
6969 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6970 struct dwarf2_per_cu_quick_data);
6971 }
6972
6973 if (slot == NULL)
6974 {
6975 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6976 sig_type, INSERT);
6977 }
6978 gdb_assert (*slot == NULL);
6979 *slot = sig_type;
6980 /* The rest of sig_type must be filled in by the caller. */
6981 return sig_type;
6982}
6983
a2ce51a0
DE
6984/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6985 Fill in SIG_ENTRY with DWO_ENTRY. */
6986
6987static void
ed2dc618 6988fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6989 struct signatured_type *sig_entry,
6990 struct dwo_unit *dwo_entry)
6991{
7ee85ab1 6992 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6993 gdb_assert (! sig_entry->per_cu.queued);
6994 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6995 if (dwarf2_per_objfile->using_index)
6996 {
6997 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6998 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6999 }
7000 else
7001 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7002 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7003 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7004 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7005 gdb_assert (sig_entry->dwo_unit == NULL);
7006
7007 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7008 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7009 sig_entry->per_cu.length = dwo_entry->length;
7010 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7011 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7012 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7013 sig_entry->dwo_unit = dwo_entry;
7014}
7015
7016/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7017 If we haven't read the TU yet, create the signatured_type data structure
7018 for a TU to be read in directly from a DWO file, bypassing the stub.
7019 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7020 using .gdb_index, then when reading a CU we want to stay in the DWO file
7021 containing that CU. Otherwise we could end up reading several other DWO
7022 files (due to comdat folding) to process the transitive closure of all the
7023 mentioned TUs, and that can be slow. The current DWO file will have every
7024 type signature that it needs.
a2ce51a0
DE
7025 We only do this for .gdb_index because in the psymtab case we already have
7026 to read all the DWOs to build the type unit groups. */
7027
7028static struct signatured_type *
7029lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7030{
518817b3
SM
7031 struct dwarf2_per_objfile *dwarf2_per_objfile
7032 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7033 struct objfile *objfile = dwarf2_per_objfile->objfile;
7034 struct dwo_file *dwo_file;
7035 struct dwo_unit find_dwo_entry, *dwo_entry;
7036 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7037 void **slot;
a2ce51a0
DE
7038
7039 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7040
6aa5f3a6
DE
7041 /* If TU skeletons have been removed then we may not have read in any
7042 TUs yet. */
7043 if (dwarf2_per_objfile->signatured_types == NULL)
7044 {
7045 dwarf2_per_objfile->signatured_types
7046 = allocate_signatured_type_table (objfile);
7047 }
a2ce51a0
DE
7048
7049 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7050 Use the global signatured_types array to do our own comdat-folding
7051 of types. If this is the first time we're reading this TU, and
7052 the TU has an entry in .gdb_index, replace the recorded data from
7053 .gdb_index with this TU. */
a2ce51a0 7054
a2ce51a0 7055 find_sig_entry.signature = sig;
6aa5f3a6
DE
7056 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7057 &find_sig_entry, INSERT);
9a3c8263 7058 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7059
7060 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7061 read. Don't reassign the global entry to point to this DWO if that's
7062 the case. Also note that if the TU is already being read, it may not
7063 have come from a DWO, the program may be a mix of Fission-compiled
7064 code and non-Fission-compiled code. */
7065
7066 /* Have we already tried to read this TU?
7067 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7068 needn't exist in the global table yet). */
7069 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7070 return sig_entry;
7071
6aa5f3a6
DE
7072 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7073 dwo_unit of the TU itself. */
7074 dwo_file = cu->dwo_unit->dwo_file;
7075
a2ce51a0
DE
7076 /* Ok, this is the first time we're reading this TU. */
7077 if (dwo_file->tus == NULL)
7078 return NULL;
7079 find_dwo_entry.signature = sig;
9a3c8263 7080 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7081 if (dwo_entry == NULL)
7082 return NULL;
7083
6aa5f3a6
DE
7084 /* If the global table doesn't have an entry for this TU, add one. */
7085 if (sig_entry == NULL)
ed2dc618 7086 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7087
ed2dc618 7088 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7089 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7090 return sig_entry;
7091}
7092
a2ce51a0
DE
7093/* Subroutine of lookup_signatured_type.
7094 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7095 then try the DWP file. If the TU stub (skeleton) has been removed then
7096 it won't be in .gdb_index. */
a2ce51a0
DE
7097
7098static struct signatured_type *
7099lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7100{
518817b3
SM
7101 struct dwarf2_per_objfile *dwarf2_per_objfile
7102 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7103 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7104 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7105 struct dwo_unit *dwo_entry;
7106 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7107 void **slot;
a2ce51a0
DE
7108
7109 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7110 gdb_assert (dwp_file != NULL);
7111
6aa5f3a6
DE
7112 /* If TU skeletons have been removed then we may not have read in any
7113 TUs yet. */
7114 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7115 {
6aa5f3a6
DE
7116 dwarf2_per_objfile->signatured_types
7117 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7118 }
7119
6aa5f3a6
DE
7120 find_sig_entry.signature = sig;
7121 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7122 &find_sig_entry, INSERT);
9a3c8263 7123 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7124
7125 /* Have we already tried to read this TU?
7126 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7127 needn't exist in the global table yet). */
7128 if (sig_entry != NULL)
7129 return sig_entry;
7130
a2ce51a0
DE
7131 if (dwp_file->tus == NULL)
7132 return NULL;
ed2dc618 7133 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7134 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7135 if (dwo_entry == NULL)
7136 return NULL;
7137
ed2dc618
SM
7138 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7139 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7140
a2ce51a0
DE
7141 return sig_entry;
7142}
7143
380bca97 7144/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7145 Returns NULL if signature SIG is not present in the table.
7146 It is up to the caller to complain about this. */
348e048f
DE
7147
7148static struct signatured_type *
a2ce51a0 7149lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7150{
518817b3
SM
7151 struct dwarf2_per_objfile *dwarf2_per_objfile
7152 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7153
a2ce51a0
DE
7154 if (cu->dwo_unit
7155 && dwarf2_per_objfile->using_index)
7156 {
7157 /* We're in a DWO/DWP file, and we're using .gdb_index.
7158 These cases require special processing. */
ed2dc618 7159 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7160 return lookup_dwo_signatured_type (cu, sig);
7161 else
7162 return lookup_dwp_signatured_type (cu, sig);
7163 }
7164 else
7165 {
7166 struct signatured_type find_entry, *entry;
348e048f 7167
a2ce51a0
DE
7168 if (dwarf2_per_objfile->signatured_types == NULL)
7169 return NULL;
7170 find_entry.signature = sig;
9a3c8263
SM
7171 entry = ((struct signatured_type *)
7172 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7173 return entry;
7174 }
348e048f 7175}
42e7ad6c
DE
7176\f
7177/* Low level DIE reading support. */
348e048f 7178
d85a05f0
DJ
7179/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7180
7181static void
7182init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7183 struct dwarf2_cu *cu,
3019eac3 7184 struct dwarf2_section_info *section,
685af9cd
TT
7185 struct dwo_file *dwo_file,
7186 struct abbrev_table *abbrev_table)
d85a05f0 7187{
fceca515 7188 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7189 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7190 reader->cu = cu;
3019eac3 7191 reader->dwo_file = dwo_file;
dee91e82
DE
7192 reader->die_section = section;
7193 reader->buffer = section->buffer;
f664829e 7194 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7195 reader->comp_dir = NULL;
685af9cd 7196 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7197}
7198
b0c7bfa9
DE
7199/* Subroutine of init_cutu_and_read_dies to simplify it.
7200 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7201 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7202 already.
7203
7204 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7205 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7206 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7207 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7208 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7209 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7210 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7211 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7212 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7213 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7214 kept around for at least as long as *RESULT_READER.
7215
b0c7bfa9
DE
7216 The result is non-zero if a valid (non-dummy) DIE was found. */
7217
7218static int
7219read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7220 struct dwo_unit *dwo_unit,
b0c7bfa9 7221 struct die_info *stub_comp_unit_die,
a2ce51a0 7222 const char *stub_comp_dir,
b0c7bfa9 7223 struct die_reader_specs *result_reader,
d521ce57 7224 const gdb_byte **result_info_ptr,
b0c7bfa9 7225 struct die_info **result_comp_unit_die,
685af9cd
TT
7226 int *result_has_children,
7227 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7228{
ed2dc618 7229 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7230 struct objfile *objfile = dwarf2_per_objfile->objfile;
7231 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7232 bfd *abfd;
d521ce57 7233 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7234 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7235 int i,num_extra_attrs;
7236 struct dwarf2_section_info *dwo_abbrev_section;
7237 struct attribute *attr;
7238 struct die_info *comp_unit_die;
7239
b0aeadb3
DE
7240 /* At most one of these may be provided. */
7241 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7242
b0c7bfa9
DE
7243 /* These attributes aren't processed until later:
7244 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7245 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7246 referenced later. However, these attributes are found in the stub
7247 which we won't have later. In order to not impose this complication
7248 on the rest of the code, we read them here and copy them to the
7249 DWO CU/TU die. */
b0c7bfa9
DE
7250
7251 stmt_list = NULL;
7252 low_pc = NULL;
7253 high_pc = NULL;
7254 ranges = NULL;
7255 comp_dir = NULL;
7256
7257 if (stub_comp_unit_die != NULL)
7258 {
7259 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7260 DWO file. */
7261 if (! this_cu->is_debug_types)
7262 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7263 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7264 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7265 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7266 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7267
7268 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7269 We need the value before we can process DW_FORM_GNU_addr_index
7270 or DW_FORM_addrx. */
b0c7bfa9
DE
7271 cu->addr_base = 0;
7272 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
435d3d88 7273 if (attr != nullptr)
b0c7bfa9
DE
7274 cu->addr_base = DW_UNSND (attr);
7275
7276 /* There should be a DW_AT_ranges_base attribute here (if needed).
7277 We need the value before we can process DW_AT_ranges. */
7278 cu->ranges_base = 0;
7279 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
435d3d88 7280 if (attr != nullptr)
b0c7bfa9
DE
7281 cu->ranges_base = DW_UNSND (attr);
7282 }
a2ce51a0
DE
7283 else if (stub_comp_dir != NULL)
7284 {
7285 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7286 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7287 comp_dir->name = DW_AT_comp_dir;
7288 comp_dir->form = DW_FORM_string;
7289 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7290 DW_STRING (comp_dir) = stub_comp_dir;
7291 }
b0c7bfa9
DE
7292
7293 /* Set up for reading the DWO CU/TU. */
7294 cu->dwo_unit = dwo_unit;
685af9cd 7295 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7296 dwarf2_read_section (objfile, section);
a32a8923 7297 abfd = get_section_bfd_owner (section);
9c541725
PA
7298 begin_info_ptr = info_ptr = (section->buffer
7299 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7300 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7301
7302 if (this_cu->is_debug_types)
7303 {
b0c7bfa9
DE
7304 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7305
ed2dc618
SM
7306 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7307 &cu->header, section,
b0c7bfa9 7308 dwo_abbrev_section,
43988095 7309 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7310 /* This is not an assert because it can be caused by bad debug info. */
43988095 7311 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7312 {
7313 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7314 " TU at offset %s [in module %s]"),
a2ce51a0 7315 hex_string (sig_type->signature),
43988095 7316 hex_string (cu->header.signature),
9d8780f0 7317 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7318 bfd_get_filename (abfd));
7319 }
9c541725 7320 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7321 /* For DWOs coming from DWP files, we don't know the CU length
7322 nor the type's offset in the TU until now. */
7323 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7324 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7325
7326 /* Establish the type offset that can be used to lookup the type.
7327 For DWO files, we don't know it until now. */
9c541725
PA
7328 sig_type->type_offset_in_section
7329 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7330 }
7331 else
7332 {
ed2dc618
SM
7333 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7334 &cu->header, section,
b0c7bfa9 7335 dwo_abbrev_section,
43988095 7336 info_ptr, rcuh_kind::COMPILE);
9c541725 7337 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7338 /* For DWOs coming from DWP files, we don't know the CU length
7339 until now. */
7340 dwo_unit->length = get_cu_length (&cu->header);
7341 }
7342
685af9cd
TT
7343 *result_dwo_abbrev_table
7344 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7345 cu->header.abbrev_sect_off);
7346 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7347 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7348
7349 /* Read in the die, but leave space to copy over the attributes
7350 from the stub. This has the benefit of simplifying the rest of
7351 the code - all the work to maintain the illusion of a single
7352 DW_TAG_{compile,type}_unit DIE is done here. */
7353 num_extra_attrs = ((stmt_list != NULL)
7354 + (low_pc != NULL)
7355 + (high_pc != NULL)
7356 + (ranges != NULL)
7357 + (comp_dir != NULL));
7358 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7359 result_has_children, num_extra_attrs);
7360
7361 /* Copy over the attributes from the stub to the DIE we just read in. */
7362 comp_unit_die = *result_comp_unit_die;
7363 i = comp_unit_die->num_attrs;
7364 if (stmt_list != NULL)
7365 comp_unit_die->attrs[i++] = *stmt_list;
7366 if (low_pc != NULL)
7367 comp_unit_die->attrs[i++] = *low_pc;
7368 if (high_pc != NULL)
7369 comp_unit_die->attrs[i++] = *high_pc;
7370 if (ranges != NULL)
7371 comp_unit_die->attrs[i++] = *ranges;
7372 if (comp_dir != NULL)
7373 comp_unit_die->attrs[i++] = *comp_dir;
7374 comp_unit_die->num_attrs += num_extra_attrs;
7375
b4f54984 7376 if (dwarf_die_debug)
bf6af496
DE
7377 {
7378 fprintf_unfiltered (gdb_stdlog,
7379 "Read die from %s@0x%x of %s:\n",
a32a8923 7380 get_section_name (section),
bf6af496
DE
7381 (unsigned) (begin_info_ptr - section->buffer),
7382 bfd_get_filename (abfd));
b4f54984 7383 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7384 }
7385
a2ce51a0
DE
7386 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7387 TUs by skipping the stub and going directly to the entry in the DWO file.
7388 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7389 to get it via circuitous means. Blech. */
7390 if (comp_dir != NULL)
7391 result_reader->comp_dir = DW_STRING (comp_dir);
7392
b0c7bfa9
DE
7393 /* Skip dummy compilation units. */
7394 if (info_ptr >= begin_info_ptr + dwo_unit->length
7395 || peek_abbrev_code (abfd, info_ptr) == 0)
7396 return 0;
7397
7398 *result_info_ptr = info_ptr;
7399 return 1;
7400}
7401
a084a2a6
AT
7402/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7403 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7404 signature is part of the header. */
7405static gdb::optional<ULONGEST>
7406lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7407{
7408 if (cu->header.version >= 5)
7409 return cu->header.signature;
7410 struct attribute *attr;
7411 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7412 if (attr == nullptr)
7413 return gdb::optional<ULONGEST> ();
7414 return DW_UNSND (attr);
7415}
7416
b0c7bfa9
DE
7417/* Subroutine of init_cutu_and_read_dies to simplify it.
7418 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7419 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7420
7421static struct dwo_unit *
7422lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7423 struct die_info *comp_unit_die)
7424{
7425 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7426 struct dwo_unit *dwo_unit;
7427 const char *comp_dir, *dwo_name;
7428
a2ce51a0
DE
7429 gdb_assert (cu != NULL);
7430
b0c7bfa9 7431 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7432 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7433 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7434
7435 if (this_cu->is_debug_types)
7436 {
7437 struct signatured_type *sig_type;
7438
7439 /* Since this_cu is the first member of struct signatured_type,
7440 we can go from a pointer to one to a pointer to the other. */
7441 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7442 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7443 }
7444 else
7445 {
a084a2a6
AT
7446 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7447 if (!signature.has_value ())
b0c7bfa9
DE
7448 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7449 " [in module %s]"),
e3b94546 7450 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7451 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7452 *signature);
b0c7bfa9
DE
7453 }
7454
b0c7bfa9
DE
7455 return dwo_unit;
7456}
7457
a2ce51a0 7458/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7459 See it for a description of the parameters.
fcd3b13d 7460 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7461
7462static void
6aa5f3a6
DE
7463init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7464 int use_existing_cu, int keep,
a2ce51a0
DE
7465 die_reader_func_ftype *die_reader_func,
7466 void *data)
7467{
fcd3b13d 7468 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7469 struct signatured_type *sig_type;
a2ce51a0
DE
7470 struct die_reader_specs reader;
7471 const gdb_byte *info_ptr;
7472 struct die_info *comp_unit_die;
7473 int has_children;
ed2dc618 7474 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7475
7476 /* Verify we can do the following downcast, and that we have the
7477 data we need. */
7478 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7479 sig_type = (struct signatured_type *) this_cu;
7480 gdb_assert (sig_type->dwo_unit != NULL);
7481
6aa5f3a6
DE
7482 if (use_existing_cu && this_cu->cu != NULL)
7483 {
7484 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7485 /* There's no need to do the rereading_dwo_cu handling that
7486 init_cutu_and_read_dies does since we don't read the stub. */
7487 }
7488 else
7489 {
7490 /* If !use_existing_cu, this_cu->cu must be NULL. */
7491 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7492 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7493 }
7494
7495 /* A future optimization, if needed, would be to use an existing
7496 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7497 could share abbrev tables. */
a2ce51a0 7498
685af9cd
TT
7499 /* The abbreviation table used by READER, this must live at least as long as
7500 READER. */
7501 abbrev_table_up dwo_abbrev_table;
7502
a2ce51a0 7503 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7504 NULL /* stub_comp_unit_die */,
7505 sig_type->dwo_unit->dwo_file->comp_dir,
7506 &reader, &info_ptr,
685af9cd
TT
7507 &comp_unit_die, &has_children,
7508 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7509 {
7510 /* Dummy die. */
a2ce51a0
DE
7511 return;
7512 }
7513
7514 /* All the "real" work is done here. */
7515 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7516
6aa5f3a6 7517 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7518 but the alternative is making the latter more complex.
7519 This function is only for the special case of using DWO files directly:
7520 no point in overly complicating the general case just to handle this. */
fcd3b13d 7521 if (new_cu != NULL && keep)
a2ce51a0 7522 {
fcd3b13d
SM
7523 /* Link this CU into read_in_chain. */
7524 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7525 dwarf2_per_objfile->read_in_chain = this_cu;
7526 /* The chain owns it now. */
7527 new_cu.release ();
a2ce51a0 7528 }
a2ce51a0
DE
7529}
7530
fd820528 7531/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7532 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7533
f4dc4d17
DE
7534 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7535 Otherwise the table specified in the comp unit header is read in and used.
7536 This is an optimization for when we already have the abbrev table.
7537
dee91e82
DE
7538 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7539 Otherwise, a new CU is allocated with xmalloc.
7540
7541 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7542 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7543
7544 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7545 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7546
70221824 7547static void
fd820528 7548init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7549 struct abbrev_table *abbrev_table,
fd820528 7550 int use_existing_cu, int keep,
58f0c718 7551 bool skip_partial,
fd820528
DE
7552 die_reader_func_ftype *die_reader_func,
7553 void *data)
c906108c 7554{
ed2dc618 7555 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7556 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7557 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7558 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7559 struct dwarf2_cu *cu;
d521ce57 7560 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7561 struct die_reader_specs reader;
d85a05f0 7562 struct die_info *comp_unit_die;
dee91e82 7563 int has_children;
dee91e82 7564 struct signatured_type *sig_type = NULL;
4bdcc0c1 7565 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7566 /* Non-zero if CU currently points to a DWO file and we need to
7567 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7568 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7569 int rereading_dwo_cu = 0;
c906108c 7570
b4f54984 7571 if (dwarf_die_debug)
9d8780f0 7572 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7573 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7574 sect_offset_str (this_cu->sect_off));
09406207 7575
dee91e82
DE
7576 if (use_existing_cu)
7577 gdb_assert (keep);
23745b47 7578
a2ce51a0
DE
7579 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7580 file (instead of going through the stub), short-circuit all of this. */
7581 if (this_cu->reading_dwo_directly)
7582 {
7583 /* Narrow down the scope of possibilities to have to understand. */
7584 gdb_assert (this_cu->is_debug_types);
7585 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7586 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7587 die_reader_func, data);
a2ce51a0
DE
7588 return;
7589 }
7590
dee91e82
DE
7591 /* This is cheap if the section is already read in. */
7592 dwarf2_read_section (objfile, section);
7593
9c541725 7594 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7595
7596 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7597
fcd3b13d 7598 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7599 if (use_existing_cu && this_cu->cu != NULL)
7600 {
7601 cu = this_cu->cu;
42e7ad6c
DE
7602 /* If this CU is from a DWO file we need to start over, we need to
7603 refetch the attributes from the skeleton CU.
7604 This could be optimized by retrieving those attributes from when we
7605 were here the first time: the previous comp_unit_die was stored in
7606 comp_unit_obstack. But there's no data yet that we need this
7607 optimization. */
7608 if (cu->dwo_unit != NULL)
7609 rereading_dwo_cu = 1;
dee91e82
DE
7610 }
7611 else
7612 {
7613 /* If !use_existing_cu, this_cu->cu must be NULL. */
7614 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7615 new_cu.reset (new dwarf2_cu (this_cu));
7616 cu = new_cu.get ();
42e7ad6c 7617 }
dee91e82 7618
b0c7bfa9 7619 /* Get the header. */
9c541725 7620 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7621 {
7622 /* We already have the header, there's no need to read it in again. */
9c541725 7623 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7624 }
7625 else
7626 {
3019eac3 7627 if (this_cu->is_debug_types)
dee91e82 7628 {
ed2dc618
SM
7629 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7630 &cu->header, section,
4bdcc0c1 7631 abbrev_section, info_ptr,
43988095 7632 rcuh_kind::TYPE);
dee91e82 7633
42e7ad6c
DE
7634 /* Since per_cu is the first member of struct signatured_type,
7635 we can go from a pointer to one to a pointer to the other. */
7636 sig_type = (struct signatured_type *) this_cu;
43988095 7637 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7638 gdb_assert (sig_type->type_offset_in_tu
7639 == cu->header.type_cu_offset_in_tu);
7640 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7641
42e7ad6c
DE
7642 /* LENGTH has not been set yet for type units if we're
7643 using .gdb_index. */
1ce1cefd 7644 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7645
7646 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7647 sig_type->type_offset_in_section =
7648 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7649
7650 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7651 }
7652 else
7653 {
ed2dc618
SM
7654 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7655 &cu->header, section,
4bdcc0c1 7656 abbrev_section,
43988095
JK
7657 info_ptr,
7658 rcuh_kind::COMPILE);
dee91e82 7659
9c541725 7660 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7661 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7662 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7663 }
7664 }
10b3939b 7665
6caca83c 7666 /* Skip dummy compilation units. */
dee91e82 7667 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7668 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7669 return;
6caca83c 7670
433df2d4
DE
7671 /* If we don't have them yet, read the abbrevs for this compilation unit.
7672 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7673 done (own the table through ABBREV_TABLE_HOLDER). */
7674 abbrev_table_up abbrev_table_holder;
f4dc4d17 7675 if (abbrev_table != NULL)
685af9cd
TT
7676 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7677 else
f4dc4d17 7678 {
685af9cd
TT
7679 abbrev_table_holder
7680 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7681 cu->header.abbrev_sect_off);
7682 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7683 }
af703f96 7684
dee91e82 7685 /* Read the top level CU/TU die. */
685af9cd 7686 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7687 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7688
58f0c718
TT
7689 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7690 return;
7691
b0c7bfa9 7692 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7693 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7694 table from the DWO file and pass the ownership over to us. It will be
7695 referenced from READER, so we must make sure to free it after we're done
7696 with READER.
7697
b0c7bfa9
DE
7698 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7699 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7700 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7701 abbrev_table_up dwo_abbrev_table;
a084a2a6 7702 if (dwo_name != nullptr)
3019eac3 7703 {
3019eac3 7704 struct dwo_unit *dwo_unit;
b0c7bfa9 7705 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7706
7707 if (has_children)
6a506a2d 7708 {
b98664d3 7709 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7710 " has children (offset %s) [in module %s]"),
7711 sect_offset_str (this_cu->sect_off),
7712 bfd_get_filename (abfd));
6a506a2d 7713 }
b0c7bfa9 7714 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7715 if (dwo_unit != NULL)
3019eac3 7716 {
6a506a2d 7717 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7718 comp_unit_die, NULL,
6a506a2d 7719 &reader, &info_ptr,
685af9cd
TT
7720 &dwo_comp_unit_die, &has_children,
7721 &dwo_abbrev_table) == 0)
6a506a2d
DE
7722 {
7723 /* Dummy die. */
6a506a2d
DE
7724 return;
7725 }
7726 comp_unit_die = dwo_comp_unit_die;
7727 }
7728 else
7729 {
7730 /* Yikes, we couldn't find the rest of the DIE, we only have
7731 the stub. A complaint has already been logged. There's
7732 not much more we can do except pass on the stub DIE to
7733 die_reader_func. We don't want to throw an error on bad
7734 debug info. */
3019eac3
DE
7735 }
7736 }
7737
b0c7bfa9 7738 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7739 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7740
b0c7bfa9 7741 /* Done, clean up. */
fcd3b13d 7742 if (new_cu != NULL && keep)
348e048f 7743 {
fcd3b13d
SM
7744 /* Link this CU into read_in_chain. */
7745 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7746 dwarf2_per_objfile->read_in_chain = this_cu;
7747 /* The chain owns it now. */
7748 new_cu.release ();
348e048f 7749 }
dee91e82
DE
7750}
7751
33e80786
DE
7752/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7753 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7754 to have already done the lookup to find the DWO file).
dee91e82
DE
7755
7756 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7757 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7758
7759 We fill in THIS_CU->length.
7760
7761 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7762 linker) then DIE_READER_FUNC will not get called.
7763
7764 THIS_CU->cu is always freed when done.
3019eac3
DE
7765 This is done in order to not leave THIS_CU->cu in a state where we have
7766 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7767
7768static void
7769init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7770 struct dwo_file *dwo_file,
dee91e82
DE
7771 die_reader_func_ftype *die_reader_func,
7772 void *data)
7773{
ed2dc618 7774 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7775 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7776 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7777 bfd *abfd = get_section_bfd_owner (section);
33e80786 7778 struct dwarf2_section_info *abbrev_section;
d521ce57 7779 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7780 struct die_reader_specs reader;
dee91e82
DE
7781 struct die_info *comp_unit_die;
7782 int has_children;
7783
b4f54984 7784 if (dwarf_die_debug)
9d8780f0 7785 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7786 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7787 sect_offset_str (this_cu->sect_off));
09406207 7788
dee91e82
DE
7789 gdb_assert (this_cu->cu == NULL);
7790
33e80786
DE
7791 abbrev_section = (dwo_file != NULL
7792 ? &dwo_file->sections.abbrev
7793 : get_abbrev_section_for_cu (this_cu));
7794
dee91e82
DE
7795 /* This is cheap if the section is already read in. */
7796 dwarf2_read_section (objfile, section);
7797
fcd3b13d 7798 struct dwarf2_cu cu (this_cu);
dee91e82 7799
9c541725 7800 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7801 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7802 &cu.header, section,
4bdcc0c1 7803 abbrev_section, info_ptr,
43988095
JK
7804 (this_cu->is_debug_types
7805 ? rcuh_kind::TYPE
7806 : rcuh_kind::COMPILE));
dee91e82 7807
1ce1cefd 7808 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7809
7810 /* Skip dummy compilation units. */
7811 if (info_ptr >= begin_info_ptr + this_cu->length
7812 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7813 return;
72bf9492 7814
685af9cd
TT
7815 abbrev_table_up abbrev_table
7816 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7817 cu.header.abbrev_sect_off);
dee91e82 7818
685af9cd 7819 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7820 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7821
7822 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7823}
7824
3019eac3
DE
7825/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7826 does not lookup the specified DWO file.
7827 This cannot be used to read DWO files.
dee91e82
DE
7828
7829 THIS_CU->cu is always freed when done.
3019eac3
DE
7830 This is done in order to not leave THIS_CU->cu in a state where we have
7831 to care whether it refers to the "main" CU or the DWO CU.
7832 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7833
7834static void
7835init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7836 die_reader_func_ftype *die_reader_func,
7837 void *data)
7838{
33e80786 7839 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7840}
0018ea6f
DE
7841\f
7842/* Type Unit Groups.
dee91e82 7843
0018ea6f
DE
7844 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7845 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7846 so that all types coming from the same compilation (.o file) are grouped
7847 together. A future step could be to put the types in the same symtab as
7848 the CU the types ultimately came from. */
ff013f42 7849
f4dc4d17
DE
7850static hashval_t
7851hash_type_unit_group (const void *item)
7852{
9a3c8263
SM
7853 const struct type_unit_group *tu_group
7854 = (const struct type_unit_group *) item;
f4dc4d17 7855
094b34ac 7856 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7857}
348e048f
DE
7858
7859static int
f4dc4d17 7860eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7861{
9a3c8263
SM
7862 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7863 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7864
094b34ac 7865 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7866}
348e048f 7867
f4dc4d17
DE
7868/* Allocate a hash table for type unit groups. */
7869
7870static htab_t
ed2dc618 7871allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7872{
7873 return htab_create_alloc_ex (3,
7874 hash_type_unit_group,
7875 eq_type_unit_group,
7876 NULL,
ed2dc618 7877 &objfile->objfile_obstack,
f4dc4d17
DE
7878 hashtab_obstack_allocate,
7879 dummy_obstack_deallocate);
7880}
dee91e82 7881
f4dc4d17
DE
7882/* Type units that don't have DW_AT_stmt_list are grouped into their own
7883 partial symtabs. We combine several TUs per psymtab to not let the size
7884 of any one psymtab grow too big. */
7885#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7886#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7887
094b34ac 7888/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7889 Create the type_unit_group object used to hold one or more TUs. */
7890
7891static struct type_unit_group *
094b34ac 7892create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7893{
518817b3
SM
7894 struct dwarf2_per_objfile *dwarf2_per_objfile
7895 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7896 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7897 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7898 struct type_unit_group *tu_group;
f4dc4d17
DE
7899
7900 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7901 struct type_unit_group);
094b34ac 7902 per_cu = &tu_group->per_cu;
518817b3 7903 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7904
094b34ac
DE
7905 if (dwarf2_per_objfile->using_index)
7906 {
7907 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7908 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7909 }
7910 else
7911 {
9c541725 7912 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7913 struct partial_symtab *pst;
528e1572 7914 std::string name;
094b34ac
DE
7915
7916 /* Give the symtab a useful name for debug purposes. */
7917 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7918 name = string_printf ("<type_units_%d>",
7919 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7920 else
528e1572 7921 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7922
528e1572 7923 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7924 pst->anonymous = 1;
094b34ac 7925 }
f4dc4d17 7926
094b34ac 7927 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7928 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7929
7930 return tu_group;
7931}
7932
094b34ac
DE
7933/* Look up the type_unit_group for type unit CU, and create it if necessary.
7934 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7935
7936static struct type_unit_group *
ff39bb5e 7937get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7938{
518817b3
SM
7939 struct dwarf2_per_objfile *dwarf2_per_objfile
7940 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7941 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7942 struct type_unit_group *tu_group;
7943 void **slot;
7944 unsigned int line_offset;
7945 struct type_unit_group type_unit_group_for_lookup;
7946
7947 if (dwarf2_per_objfile->type_unit_groups == NULL)
7948 {
7949 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7950 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7951 }
7952
7953 /* Do we need to create a new group, or can we use an existing one? */
7954
7955 if (stmt_list)
7956 {
7957 line_offset = DW_UNSND (stmt_list);
7958 ++tu_stats->nr_symtab_sharers;
7959 }
7960 else
7961 {
7962 /* Ugh, no stmt_list. Rare, but we have to handle it.
7963 We can do various things here like create one group per TU or
7964 spread them over multiple groups to split up the expansion work.
7965 To avoid worst case scenarios (too many groups or too large groups)
7966 we, umm, group them in bunches. */
7967 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7968 | (tu_stats->nr_stmt_less_type_units
7969 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7970 ++tu_stats->nr_stmt_less_type_units;
7971 }
7972
094b34ac 7973 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7974 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7975 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7976 &type_unit_group_for_lookup, INSERT);
7977 if (*slot != NULL)
7978 {
9a3c8263 7979 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7980 gdb_assert (tu_group != NULL);
7981 }
7982 else
7983 {
9c541725 7984 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7985 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7986 *slot = tu_group;
7987 ++tu_stats->nr_symtabs;
7988 }
7989
7990 return tu_group;
7991}
0018ea6f
DE
7992\f
7993/* Partial symbol tables. */
7994
7995/* Create a psymtab named NAME and assign it to PER_CU.
7996
7997 The caller must fill in the following details:
7998 dirname, textlow, texthigh. */
7999
8000static struct partial_symtab *
8001create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8002{
e3b94546 8003 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8004 struct partial_symtab *pst;
8005
939652a5 8006 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
8007
8008 pst->psymtabs_addrmap_supported = 1;
8009
8010 /* This is the glue that links PST into GDB's symbol API. */
8011 pst->read_symtab_private = per_cu;
8012 pst->read_symtab = dwarf2_read_symtab;
8013 per_cu->v.psymtab = pst;
8014
8015 return pst;
8016}
8017
b93601f3
TT
8018/* The DATA object passed to process_psymtab_comp_unit_reader has this
8019 type. */
8020
8021struct process_psymtab_comp_unit_data
8022{
8023 /* True if we are reading a DW_TAG_partial_unit. */
8024
8025 int want_partial_unit;
8026
8027 /* The "pretend" language that is used if the CU doesn't declare a
8028 language. */
8029
8030 enum language pretend_language;
8031};
8032
0018ea6f
DE
8033/* die_reader_func for process_psymtab_comp_unit. */
8034
8035static void
8036process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8037 const gdb_byte *info_ptr,
0018ea6f
DE
8038 struct die_info *comp_unit_die,
8039 int has_children,
8040 void *data)
8041{
8042 struct dwarf2_cu *cu = reader->cu;
518817b3 8043 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8044 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8045 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8046 CORE_ADDR baseaddr;
8047 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8048 struct partial_symtab *pst;
3a2b436a 8049 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8050 const char *filename;
9a3c8263
SM
8051 struct process_psymtab_comp_unit_data *info
8052 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8053
b93601f3 8054 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8055 return;
8056
8057 gdb_assert (! per_cu->is_debug_types);
8058
b93601f3 8059 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8060
0018ea6f 8061 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8062 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8063 if (filename == NULL)
0018ea6f 8064 filename = "";
0018ea6f
DE
8065
8066 pst = create_partial_symtab (per_cu, filename);
8067
8068 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8069 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8070
8071 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8072
8073 dwarf2_find_base_address (comp_unit_die, cu);
8074
8075 /* Possibly set the default values of LOWPC and HIGHPC from
8076 `DW_AT_ranges'. */
3a2b436a
JK
8077 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8078 &best_highpc, cu, pst);
8079 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8080 {
8081 CORE_ADDR low
8082 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8083 - baseaddr);
8084 CORE_ADDR high
8085 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8086 - baseaddr - 1);
8087 /* Store the contiguous range if it is not empty; it can be
8088 empty for CUs with no code. */
d320c2b5
TT
8089 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8090 low, high, pst);
79748972 8091 }
0018ea6f
DE
8092
8093 /* Check if comp unit has_children.
8094 If so, read the rest of the partial symbols from this comp unit.
8095 If not, there's no more debug_info for this comp unit. */
8096 if (has_children)
8097 {
8098 struct partial_die_info *first_die;
8099 CORE_ADDR lowpc, highpc;
8100
8101 lowpc = ((CORE_ADDR) -1);
8102 highpc = ((CORE_ADDR) 0);
8103
8104 first_die = load_partial_dies (reader, info_ptr, 1);
8105
8106 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8107 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8108
8109 /* If we didn't find a lowpc, set it to highpc to avoid
8110 complaints from `maint check'. */
8111 if (lowpc == ((CORE_ADDR) -1))
8112 lowpc = highpc;
8113
8114 /* If the compilation unit didn't have an explicit address range,
8115 then use the information extracted from its child dies. */
e385593e 8116 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8117 {
8118 best_lowpc = lowpc;
8119 best_highpc = highpc;
8120 }
8121 }
4ae976d1 8122 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8123 best_lowpc + baseaddr)
8124 - baseaddr);
4ae976d1 8125 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8126 best_highpc + baseaddr)
8127 - baseaddr);
0018ea6f 8128
8763cede 8129 end_psymtab_common (objfile, pst);
0018ea6f 8130
ae640021 8131 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8132 {
8133 int i;
ae640021 8134 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8135
8136 /* Fill in 'dependencies' here; we fill in 'users' in a
8137 post-pass. */
8138 pst->number_of_dependencies = len;
a9342b62
TT
8139 pst->dependencies
8140 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8141 for (i = 0; i < len; ++i)
8142 {
8143 pst->dependencies[i]
8144 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8145 }
0018ea6f 8146
ae640021 8147 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8148 }
8149
8150 /* Get the list of files included in the current compilation unit,
8151 and build a psymtab for each of them. */
8152 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8153
b4f54984 8154 if (dwarf_read_debug)
b926417a
TT
8155 fprintf_unfiltered (gdb_stdlog,
8156 "Psymtab for %s unit @%s: %s - %s"
8157 ", %d global, %d static syms\n",
8158 per_cu->is_debug_types ? "type" : "comp",
8159 sect_offset_str (per_cu->sect_off),
8160 paddress (gdbarch, pst->text_low (objfile)),
8161 paddress (gdbarch, pst->text_high (objfile)),
8162 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8163}
8164
8165/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8166 Process compilation unit THIS_CU for a psymtab. */
8167
8168static void
8169process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8170 int want_partial_unit,
8171 enum language pretend_language)
0018ea6f
DE
8172{
8173 /* If this compilation unit was already read in, free the
8174 cached copy in order to read it in again. This is
8175 necessary because we skipped some symbols when we first
8176 read in the compilation unit (see load_partial_dies).
8177 This problem could be avoided, but the benefit is unclear. */
8178 if (this_cu->cu != NULL)
8179 free_one_cached_comp_unit (this_cu);
8180
f1902523 8181 if (this_cu->is_debug_types)
58f0c718
TT
8182 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8183 build_type_psymtabs_reader, NULL);
f1902523
JK
8184 else
8185 {
8186 process_psymtab_comp_unit_data info;
8187 info.want_partial_unit = want_partial_unit;
8188 info.pretend_language = pretend_language;
58f0c718 8189 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8190 process_psymtab_comp_unit_reader, &info);
8191 }
0018ea6f
DE
8192
8193 /* Age out any secondary CUs. */
ed2dc618 8194 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8195}
f4dc4d17
DE
8196
8197/* Reader function for build_type_psymtabs. */
8198
8199static void
8200build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8201 const gdb_byte *info_ptr,
f4dc4d17
DE
8202 struct die_info *type_unit_die,
8203 int has_children,
8204 void *data)
8205{
ed2dc618 8206 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8207 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8208 struct objfile *objfile = dwarf2_per_objfile->objfile;
8209 struct dwarf2_cu *cu = reader->cu;
8210 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8211 struct signatured_type *sig_type;
f4dc4d17
DE
8212 struct type_unit_group *tu_group;
8213 struct attribute *attr;
8214 struct partial_die_info *first_die;
8215 CORE_ADDR lowpc, highpc;
8216 struct partial_symtab *pst;
8217
8218 gdb_assert (data == NULL);
0186c6a7
DE
8219 gdb_assert (per_cu->is_debug_types);
8220 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8221
8222 if (! has_children)
8223 return;
8224
8225 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8226 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8227
df07e2c7 8228 if (tu_group->tus == nullptr)
a8b3b8e9 8229 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8230 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8231
8232 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8233 pst = create_partial_symtab (per_cu, "");
8234 pst->anonymous = 1;
8235
8236 first_die = load_partial_dies (reader, info_ptr, 1);
8237
8238 lowpc = (CORE_ADDR) -1;
8239 highpc = (CORE_ADDR) 0;
8240 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8241
8763cede 8242 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8243}
8244
73051182
DE
8245/* Struct used to sort TUs by their abbreviation table offset. */
8246
8247struct tu_abbrev_offset
8248{
b2bdb8cf
SM
8249 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8250 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8251 {}
8252
8253 signatured_type *sig_type;
73051182
DE
8254 sect_offset abbrev_offset;
8255};
8256
484cf504 8257/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8258
484cf504
TT
8259static bool
8260sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8261 const struct tu_abbrev_offset &b)
73051182 8262{
484cf504 8263 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8264}
8265
8266/* Efficiently read all the type units.
8267 This does the bulk of the work for build_type_psymtabs.
8268
8269 The efficiency is because we sort TUs by the abbrev table they use and
8270 only read each abbrev table once. In one program there are 200K TUs
8271 sharing 8K abbrev tables.
8272
8273 The main purpose of this function is to support building the
8274 dwarf2_per_objfile->type_unit_groups table.
8275 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8276 can collapse the search space by grouping them by stmt_list.
8277 The savings can be significant, in the same program from above the 200K TUs
8278 share 8K stmt_list tables.
8279
8280 FUNC is expected to call get_type_unit_group, which will create the
8281 struct type_unit_group if necessary and add it to
8282 dwarf2_per_objfile->type_unit_groups. */
8283
8284static void
ed2dc618 8285build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8286{
73051182 8287 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8288 abbrev_table_up abbrev_table;
73051182 8289 sect_offset abbrev_offset;
73051182
DE
8290
8291 /* It's up to the caller to not call us multiple times. */
8292 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8293
b2bdb8cf 8294 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8295 return;
8296
8297 /* TUs typically share abbrev tables, and there can be way more TUs than
8298 abbrev tables. Sort by abbrev table to reduce the number of times we
8299 read each abbrev table in.
8300 Alternatives are to punt or to maintain a cache of abbrev tables.
8301 This is simpler and efficient enough for now.
8302
8303 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8304 symtab to use). Typically TUs with the same abbrev offset have the same
8305 stmt_list value too so in practice this should work well.
8306
8307 The basic algorithm here is:
8308
8309 sort TUs by abbrev table
8310 for each TU with same abbrev table:
8311 read abbrev table if first user
8312 read TU top level DIE
8313 [IWBN if DWO skeletons had DW_AT_stmt_list]
8314 call FUNC */
8315
b4f54984 8316 if (dwarf_read_debug)
73051182
DE
8317 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8318
8319 /* Sort in a separate table to maintain the order of all_type_units
8320 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8321 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8322 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8323
8324 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8325 sorted_by_abbrev.emplace_back
8326 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8327 sig_type->per_cu.section,
8328 sig_type->per_cu.sect_off));
73051182 8329
484cf504
TT
8330 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8331 sort_tu_by_abbrev_offset);
73051182 8332
9c541725 8333 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8334
b2bdb8cf 8335 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8336 {
73051182
DE
8337 /* Switch to the next abbrev table if necessary. */
8338 if (abbrev_table == NULL
b2bdb8cf 8339 || tu.abbrev_offset != abbrev_offset)
73051182 8340 {
b2bdb8cf 8341 abbrev_offset = tu.abbrev_offset;
73051182 8342 abbrev_table =
ed2dc618
SM
8343 abbrev_table_read_table (dwarf2_per_objfile,
8344 &dwarf2_per_objfile->abbrev,
73051182
DE
8345 abbrev_offset);
8346 ++tu_stats->nr_uniq_abbrev_tables;
8347 }
8348
b2bdb8cf 8349 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8350 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8351 }
6aa5f3a6 8352}
73051182 8353
6aa5f3a6
DE
8354/* Print collected type unit statistics. */
8355
8356static void
ed2dc618 8357print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8358{
8359 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8360
8361 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8362 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8363 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8364 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8365 tu_stats->nr_uniq_abbrev_tables);
8366 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8367 tu_stats->nr_symtabs);
8368 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8369 tu_stats->nr_symtab_sharers);
8370 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8371 tu_stats->nr_stmt_less_type_units);
8372 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8373 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8374}
8375
f4dc4d17
DE
8376/* Traversal function for build_type_psymtabs. */
8377
8378static int
8379build_type_psymtab_dependencies (void **slot, void *info)
8380{
ed2dc618
SM
8381 struct dwarf2_per_objfile *dwarf2_per_objfile
8382 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8383 struct objfile *objfile = dwarf2_per_objfile->objfile;
8384 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8385 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8386 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8387 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8388 int i;
8389
8390 gdb_assert (len > 0);
0186c6a7 8391 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8392
8393 pst->number_of_dependencies = len;
a9342b62 8394 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8395 for (i = 0; i < len; ++i)
f4dc4d17 8396 {
df07e2c7 8397 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8398 gdb_assert (iter->per_cu.is_debug_types);
8399 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8400 iter->type_unit_group = tu_group;
f4dc4d17
DE
8401 }
8402
df07e2c7
AB
8403 delete tu_group->tus;
8404 tu_group->tus = nullptr;
348e048f
DE
8405
8406 return 1;
8407}
8408
8409/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8410 Build partial symbol tables for the .debug_types comp-units. */
8411
8412static void
ed2dc618 8413build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8414{
ed2dc618 8415 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8416 return;
8417
ed2dc618 8418 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8419}
f4dc4d17 8420
6aa5f3a6
DE
8421/* Traversal function for process_skeletonless_type_unit.
8422 Read a TU in a DWO file and build partial symbols for it. */
8423
8424static int
8425process_skeletonless_type_unit (void **slot, void *info)
8426{
8427 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8428 struct dwarf2_per_objfile *dwarf2_per_objfile
8429 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8430 struct signatured_type find_entry, *entry;
8431
8432 /* If this TU doesn't exist in the global table, add it and read it in. */
8433
8434 if (dwarf2_per_objfile->signatured_types == NULL)
8435 {
8436 dwarf2_per_objfile->signatured_types
ed2dc618 8437 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8438 }
8439
8440 find_entry.signature = dwo_unit->signature;
8441 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8442 INSERT);
8443 /* If we've already seen this type there's nothing to do. What's happening
8444 is we're doing our own version of comdat-folding here. */
8445 if (*slot != NULL)
8446 return 1;
8447
8448 /* This does the job that create_all_type_units would have done for
8449 this TU. */
ed2dc618
SM
8450 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8451 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8452 *slot = entry;
8453
8454 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8455 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8456 build_type_psymtabs_reader, NULL);
8457
8458 return 1;
8459}
8460
8461/* Traversal function for process_skeletonless_type_units. */
8462
8463static int
8464process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8465{
8466 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8467
8468 if (dwo_file->tus != NULL)
8469 {
8470 htab_traverse_noresize (dwo_file->tus,
8471 process_skeletonless_type_unit, info);
8472 }
8473
8474 return 1;
8475}
8476
8477/* Scan all TUs of DWO files, verifying we've processed them.
8478 This is needed in case a TU was emitted without its skeleton.
8479 Note: This can't be done until we know what all the DWO files are. */
8480
8481static void
ed2dc618 8482process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8483{
8484 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8485 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8486 && dwarf2_per_objfile->dwo_files != NULL)
8487 {
51ac9db5 8488 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8489 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8490 dwarf2_per_objfile);
6aa5f3a6 8491 }
348e048f
DE
8492}
8493
ed2dc618 8494/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8495
8496static void
ed2dc618 8497set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8498{
b76e467d 8499 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8500 {
95554aad 8501 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8502
36586728
TT
8503 if (pst == NULL)
8504 continue;
8505
b76e467d 8506 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8507 {
8508 /* Set the 'user' field only if it is not already set. */
8509 if (pst->dependencies[j]->user == NULL)
8510 pst->dependencies[j]->user = pst;
8511 }
8512 }
8513}
8514
93311388
DE
8515/* Build the partial symbol table by doing a quick pass through the
8516 .debug_info and .debug_abbrev sections. */
72bf9492 8517
93311388 8518static void
ed2dc618 8519dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8520{
ed2dc618 8521 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8522
b4f54984 8523 if (dwarf_read_debug)
45cfd468
DE
8524 {
8525 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8526 objfile_name (objfile));
45cfd468
DE
8527 }
8528
98bfdba5
PA
8529 dwarf2_per_objfile->reading_partial_symbols = 1;
8530
be391dca 8531 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8532
93311388
DE
8533 /* Any cached compilation units will be linked by the per-objfile
8534 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8535 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8536
ed2dc618 8537 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8538
ed2dc618 8539 create_all_comp_units (dwarf2_per_objfile);
c906108c 8540
60606b2c
TT
8541 /* Create a temporary address map on a temporary obstack. We later
8542 copy this to the final obstack. */
8268c778 8543 auto_obstack temp_obstack;
791afaa2
TT
8544
8545 scoped_restore save_psymtabs_addrmap
d320c2b5 8546 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8547 addrmap_create_mutable (&temp_obstack));
72bf9492 8548
b76e467d
SM
8549 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8550 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8551
6aa5f3a6 8552 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8553 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8554
8555 /* Now that all TUs have been processed we can fill in the dependencies. */
8556 if (dwarf2_per_objfile->type_unit_groups != NULL)
8557 {
8558 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8559 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8560 }
8561
b4f54984 8562 if (dwarf_read_debug)
ed2dc618 8563 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8564
ed2dc618 8565 set_partial_user (dwarf2_per_objfile);
95554aad 8566
d320c2b5
TT
8567 objfile->partial_symtabs->psymtabs_addrmap
8568 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8569 objfile->partial_symtabs->obstack ());
791afaa2
TT
8570 /* At this point we want to keep the address map. */
8571 save_psymtabs_addrmap.release ();
ff013f42 8572
b4f54984 8573 if (dwarf_read_debug)
45cfd468 8574 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8575 objfile_name (objfile));
ae038cb0
DJ
8576}
8577
3019eac3 8578/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8579
8580static void
dee91e82 8581load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8582 const gdb_byte *info_ptr,
dee91e82
DE
8583 struct die_info *comp_unit_die,
8584 int has_children,
8585 void *data)
ae038cb0 8586{
dee91e82 8587 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8588
95554aad 8589 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8590
ae038cb0
DJ
8591 /* Check if comp unit has_children.
8592 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8593 If not, there's no more debug_info for this comp unit. */
d85a05f0 8594 if (has_children)
dee91e82
DE
8595 load_partial_dies (reader, info_ptr, 0);
8596}
98bfdba5 8597
dee91e82
DE
8598/* Load the partial DIEs for a secondary CU into memory.
8599 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8600
dee91e82
DE
8601static void
8602load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8603{
58f0c718 8604 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8605 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8606}
8607
ae038cb0 8608static void
ed2dc618 8609read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8610 struct dwarf2_section_info *section,
f1902523 8611 struct dwarf2_section_info *abbrev_section,
b76e467d 8612 unsigned int is_dwz)
ae038cb0 8613{
d521ce57 8614 const gdb_byte *info_ptr;
ed2dc618 8615 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8616
b4f54984 8617 if (dwarf_read_debug)
bf6af496 8618 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8619 get_section_name (section),
8620 get_section_file_name (section));
bf6af496 8621
36586728 8622 dwarf2_read_section (objfile, section);
ae038cb0 8623
36586728 8624 info_ptr = section->buffer;
6e70227d 8625
36586728 8626 while (info_ptr < section->buffer + section->size)
ae038cb0 8627 {
ae038cb0 8628 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8629
9c541725 8630 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8631
f1902523 8632 comp_unit_head cu_header;
ed2dc618
SM
8633 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8634 abbrev_section, info_ptr,
8635 rcuh_kind::COMPILE);
ae038cb0
DJ
8636
8637 /* Save the compilation unit for later lookup. */
f1902523
JK
8638 if (cu_header.unit_type != DW_UT_type)
8639 {
8640 this_cu = XOBNEW (&objfile->objfile_obstack,
8641 struct dwarf2_per_cu_data);
8642 memset (this_cu, 0, sizeof (*this_cu));
8643 }
8644 else
8645 {
8646 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8647 struct signatured_type);
8648 memset (sig_type, 0, sizeof (*sig_type));
8649 sig_type->signature = cu_header.signature;
8650 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8651 this_cu = &sig_type->per_cu;
8652 }
8653 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8654 this_cu->sect_off = sect_off;
f1902523 8655 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8656 this_cu->is_dwz = is_dwz;
e3b94546 8657 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8658 this_cu->section = section;
ae038cb0 8659
b76e467d 8660 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8661
8662 info_ptr = info_ptr + this_cu->length;
8663 }
36586728
TT
8664}
8665
8666/* Create a list of all compilation units in OBJFILE.
8667 This is only done for -readnow and building partial symtabs. */
8668
8669static void
ed2dc618 8670create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8671{
b76e467d 8672 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8673 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8674 &dwarf2_per_objfile->abbrev, 0);
36586728 8675
b76e467d 8676 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8677 if (dwz != NULL)
ed2dc618 8678 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8679 1);
c906108c
SS
8680}
8681
5734ee8b 8682/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8683 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8684 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8685 DW_AT_ranges). See the comments of add_partial_subprogram on how
8686 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8687
72bf9492
DJ
8688static void
8689scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8690 CORE_ADDR *highpc, int set_addrmap,
8691 struct dwarf2_cu *cu)
c906108c 8692{
72bf9492 8693 struct partial_die_info *pdi;
c906108c 8694
91c24f0a
DC
8695 /* Now, march along the PDI's, descending into ones which have
8696 interesting children but skipping the children of the other ones,
8697 until we reach the end of the compilation unit. */
c906108c 8698
72bf9492 8699 pdi = first_die;
91c24f0a 8700
72bf9492
DJ
8701 while (pdi != NULL)
8702 {
52356b79 8703 pdi->fixup (cu);
c906108c 8704
f55ee35c 8705 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8706 children, so we need to look at them. Ditto for anonymous
8707 enums. */
933c6fe4 8708
72bf9492 8709 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8710 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8711 || pdi->tag == DW_TAG_imported_unit
8712 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8713 {
72bf9492 8714 switch (pdi->tag)
c906108c
SS
8715 {
8716 case DW_TAG_subprogram:
b1dc1806 8717 case DW_TAG_inlined_subroutine:
cdc07690 8718 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8719 break;
72929c62 8720 case DW_TAG_constant:
c906108c
SS
8721 case DW_TAG_variable:
8722 case DW_TAG_typedef:
91c24f0a 8723 case DW_TAG_union_type:
72bf9492 8724 if (!pdi->is_declaration)
63d06c5c 8725 {
72bf9492 8726 add_partial_symbol (pdi, cu);
63d06c5c
DC
8727 }
8728 break;
c906108c 8729 case DW_TAG_class_type:
680b30c7 8730 case DW_TAG_interface_type:
c906108c 8731 case DW_TAG_structure_type:
72bf9492 8732 if (!pdi->is_declaration)
c906108c 8733 {
72bf9492 8734 add_partial_symbol (pdi, cu);
c906108c 8735 }
b7fee5a3
KS
8736 if ((cu->language == language_rust
8737 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8738 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8739 set_addrmap, cu);
c906108c 8740 break;
91c24f0a 8741 case DW_TAG_enumeration_type:
72bf9492
DJ
8742 if (!pdi->is_declaration)
8743 add_partial_enumeration (pdi, cu);
c906108c
SS
8744 break;
8745 case DW_TAG_base_type:
a02abb62 8746 case DW_TAG_subrange_type:
c906108c 8747 /* File scope base type definitions are added to the partial
c5aa993b 8748 symbol table. */
72bf9492 8749 add_partial_symbol (pdi, cu);
c906108c 8750 break;
d9fa45fe 8751 case DW_TAG_namespace:
cdc07690 8752 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8753 break;
5d7cb8df 8754 case DW_TAG_module:
59c35742
AB
8755 if (!pdi->is_declaration)
8756 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8757 break;
95554aad
TT
8758 case DW_TAG_imported_unit:
8759 {
8760 struct dwarf2_per_cu_data *per_cu;
8761
f4dc4d17
DE
8762 /* For now we don't handle imported units in type units. */
8763 if (cu->per_cu->is_debug_types)
8764 {
8765 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8766 " supported in type units [in module %s]"),
518817b3 8767 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8768 }
8769
e3b94546
SM
8770 per_cu = dwarf2_find_containing_comp_unit
8771 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8772 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8773
8774 /* Go read the partial unit, if needed. */
8775 if (per_cu->v.psymtab == NULL)
b93601f3 8776 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8777
ae640021 8778 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8779 }
8780 break;
74921315
KS
8781 case DW_TAG_imported_declaration:
8782 add_partial_symbol (pdi, cu);
8783 break;
c906108c
SS
8784 default:
8785 break;
8786 }
8787 }
8788
72bf9492
DJ
8789 /* If the die has a sibling, skip to the sibling. */
8790
8791 pdi = pdi->die_sibling;
8792 }
8793}
8794
8795/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8796
72bf9492 8797 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8798 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8799 Enumerators are an exception; they use the scope of their parent
8800 enumeration type, i.e. the name of the enumeration type is not
8801 prepended to the enumerator.
91c24f0a 8802
72bf9492
DJ
8803 There are two complexities. One is DW_AT_specification; in this
8804 case "parent" means the parent of the target of the specification,
8805 instead of the direct parent of the DIE. The other is compilers
8806 which do not emit DW_TAG_namespace; in this case we try to guess
8807 the fully qualified name of structure types from their members'
8808 linkage names. This must be done using the DIE's children rather
8809 than the children of any DW_AT_specification target. We only need
8810 to do this for structures at the top level, i.e. if the target of
8811 any DW_AT_specification (if any; otherwise the DIE itself) does not
8812 have a parent. */
8813
8814/* Compute the scope prefix associated with PDI's parent, in
8815 compilation unit CU. The result will be allocated on CU's
8816 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8817 field. NULL is returned if no prefix is necessary. */
15d034d0 8818static const char *
72bf9492
DJ
8819partial_die_parent_scope (struct partial_die_info *pdi,
8820 struct dwarf2_cu *cu)
8821{
15d034d0 8822 const char *grandparent_scope;
72bf9492 8823 struct partial_die_info *parent, *real_pdi;
91c24f0a 8824
72bf9492
DJ
8825 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8826 then this means the parent of the specification DIE. */
8827
8828 real_pdi = pdi;
72bf9492 8829 while (real_pdi->has_specification)
fb816e8b 8830 {
122cf0f2
AB
8831 auto res = find_partial_die (real_pdi->spec_offset,
8832 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8833 real_pdi = res.pdi;
8834 cu = res.cu;
8835 }
72bf9492
DJ
8836
8837 parent = real_pdi->die_parent;
8838 if (parent == NULL)
8839 return NULL;
8840
8841 if (parent->scope_set)
8842 return parent->scope;
8843
52356b79 8844 parent->fixup (cu);
72bf9492 8845
10b3939b 8846 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8847
acebe513
UW
8848 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8849 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8850 Work around this problem here. */
8851 if (cu->language == language_cplus
6e70227d 8852 && parent->tag == DW_TAG_namespace
acebe513
UW
8853 && strcmp (parent->name, "::") == 0
8854 && grandparent_scope == NULL)
8855 {
8856 parent->scope = NULL;
8857 parent->scope_set = 1;
8858 return NULL;
8859 }
8860
0a4b0913 8861 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8862 if (pdi->tag == DW_TAG_enumerator)
8863 /* Enumerators should not get the name of the enumeration as a prefix. */
8864 parent->scope = grandparent_scope;
8865 else if (parent->tag == DW_TAG_namespace
f55ee35c 8866 || parent->tag == DW_TAG_module
72bf9492
DJ
8867 || parent->tag == DW_TAG_structure_type
8868 || parent->tag == DW_TAG_class_type
680b30c7 8869 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8870 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8871 || parent->tag == DW_TAG_enumeration_type
8872 || (cu->language == language_fortran
8873 && parent->tag == DW_TAG_subprogram
8874 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8875 {
8876 if (grandparent_scope == NULL)
8877 parent->scope = parent->name;
8878 else
3e43a32a
MS
8879 parent->scope = typename_concat (&cu->comp_unit_obstack,
8880 grandparent_scope,
f55ee35c 8881 parent->name, 0, cu);
72bf9492 8882 }
72bf9492
DJ
8883 else
8884 {
8885 /* FIXME drow/2004-04-01: What should we be doing with
8886 function-local names? For partial symbols, we should probably be
8887 ignoring them. */
fa9c3fa0
TT
8888 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8889 dwarf_tag_name (parent->tag),
8890 sect_offset_str (pdi->sect_off));
72bf9492 8891 parent->scope = grandparent_scope;
c906108c
SS
8892 }
8893
72bf9492
DJ
8894 parent->scope_set = 1;
8895 return parent->scope;
8896}
8897
8898/* Return the fully scoped name associated with PDI, from compilation unit
8899 CU. The result will be allocated with malloc. */
4568ecf9 8900
43816ebc 8901static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8902partial_die_full_name (struct partial_die_info *pdi,
8903 struct dwarf2_cu *cu)
8904{
15d034d0 8905 const char *parent_scope;
72bf9492 8906
98bfdba5
PA
8907 /* If this is a template instantiation, we can not work out the
8908 template arguments from partial DIEs. So, unfortunately, we have
8909 to go through the full DIEs. At least any work we do building
8910 types here will be reused if full symbols are loaded later. */
8911 if (pdi->has_template_arguments)
8912 {
52356b79 8913 pdi->fixup (cu);
98bfdba5
PA
8914
8915 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8916 {
8917 struct die_info *die;
8918 struct attribute attr;
8919 struct dwarf2_cu *ref_cu = cu;
8920
b64f50a1 8921 /* DW_FORM_ref_addr is using section offset. */
b4069958 8922 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8923 attr.form = DW_FORM_ref_addr;
9c541725 8924 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8925 die = follow_die_ref (NULL, &attr, &ref_cu);
8926
43816ebc 8927 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8928 }
8929 }
8930
72bf9492
DJ
8931 parent_scope = partial_die_parent_scope (pdi, cu);
8932 if (parent_scope == NULL)
8933 return NULL;
8934 else
43816ebc
TT
8935 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8936 pdi->name, 0, cu));
c906108c
SS
8937}
8938
8939static void
72bf9492 8940add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8941{
518817b3
SM
8942 struct dwarf2_per_objfile *dwarf2_per_objfile
8943 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8944 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8945 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8946 CORE_ADDR addr = 0;
15d034d0 8947 const char *actual_name = NULL;
e142c38c
DJ
8948 CORE_ADDR baseaddr;
8949
8950 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8951
43816ebc
TT
8952 gdb::unique_xmalloc_ptr<char> built_actual_name
8953 = partial_die_full_name (pdi, cu);
15d034d0 8954 if (built_actual_name != NULL)
43816ebc 8955 actual_name = built_actual_name.get ();
63d06c5c 8956
72bf9492
DJ
8957 if (actual_name == NULL)
8958 actual_name = pdi->name;
8959
c906108c
SS
8960 switch (pdi->tag)
8961 {
b1dc1806 8962 case DW_TAG_inlined_subroutine:
c906108c 8963 case DW_TAG_subprogram:
79748972
TT
8964 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8965 - baseaddr);
0a4b0913
AB
8966 if (pdi->is_external
8967 || cu->language == language_ada
8968 || (cu->language == language_fortran
8969 && pdi->die_parent != NULL
8970 && pdi->die_parent->tag == DW_TAG_subprogram))
8971 {
8972 /* Normally, only "external" DIEs are part of the global scope.
8973 But in Ada and Fortran, we want to be able to access nested
8974 procedures globally. So all Ada and Fortran subprograms are
8975 stored in the global scope. */
31edb802 8976 add_psymbol_to_list (actual_name,
15d034d0 8977 built_actual_name != NULL,
f47fb265 8978 VAR_DOMAIN, LOC_BLOCK,
79748972 8979 SECT_OFF_TEXT (objfile),
75aedd27 8980 psymbol_placement::GLOBAL,
79748972
TT
8981 addr,
8982 cu->language, objfile);
c906108c
SS
8983 }
8984 else
8985 {
31edb802 8986 add_psymbol_to_list (actual_name,
15d034d0 8987 built_actual_name != NULL,
f47fb265 8988 VAR_DOMAIN, LOC_BLOCK,
79748972 8989 SECT_OFF_TEXT (objfile),
75aedd27 8990 psymbol_placement::STATIC,
1762568f 8991 addr, cu->language, objfile);
c906108c 8992 }
0c1b455e
TT
8993
8994 if (pdi->main_subprogram && actual_name != NULL)
8995 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8996 break;
72929c62 8997 case DW_TAG_constant:
31edb802 8998 add_psymbol_to_list (actual_name,
75aedd27
TT
8999 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9000 -1, (pdi->is_external
9001 ? psymbol_placement::GLOBAL
9002 : psymbol_placement::STATIC),
9003 0, cu->language, objfile);
72929c62 9004 break;
c906108c 9005 case DW_TAG_variable:
95554aad
TT
9006 if (pdi->d.locdesc)
9007 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9008
95554aad 9009 if (pdi->d.locdesc
caac4577
JG
9010 && addr == 0
9011 && !dwarf2_per_objfile->has_section_at_zero)
9012 {
9013 /* A global or static variable may also have been stripped
9014 out by the linker if unused, in which case its address
9015 will be nullified; do not add such variables into partial
9016 symbol table then. */
9017 }
9018 else if (pdi->is_external)
c906108c
SS
9019 {
9020 /* Global Variable.
9021 Don't enter into the minimal symbol tables as there is
9022 a minimal symbol table entry from the ELF symbols already.
9023 Enter into partial symbol table if it has a location
9024 descriptor or a type.
9025 If the location descriptor is missing, new_symbol will create
9026 a LOC_UNRESOLVED symbol, the address of the variable will then
9027 be determined from the minimal symbol table whenever the variable
9028 is referenced.
9029 The address for the partial symbol table entry is not
9030 used by GDB, but it comes in handy for debugging partial symbol
9031 table building. */
9032
95554aad 9033 if (pdi->d.locdesc || pdi->has_type)
31edb802 9034 add_psymbol_to_list (actual_name,
15d034d0 9035 built_actual_name != NULL,
f47fb265 9036 VAR_DOMAIN, LOC_STATIC,
79748972 9037 SECT_OFF_TEXT (objfile),
75aedd27 9038 psymbol_placement::GLOBAL,
79748972 9039 addr, cu->language, objfile);
c906108c
SS
9040 }
9041 else
9042 {
ff908ebf
AW
9043 int has_loc = pdi->d.locdesc != NULL;
9044
9045 /* Static Variable. Skip symbols whose value we cannot know (those
9046 without location descriptors or constant values). */
9047 if (!has_loc && !pdi->has_const_value)
43816ebc 9048 return;
ff908ebf 9049
31edb802 9050 add_psymbol_to_list (actual_name,
15d034d0 9051 built_actual_name != NULL,
f47fb265 9052 VAR_DOMAIN, LOC_STATIC,
79748972 9053 SECT_OFF_TEXT (objfile),
75aedd27 9054 psymbol_placement::STATIC,
79748972 9055 has_loc ? addr : 0,
f47fb265 9056 cu->language, objfile);
c906108c
SS
9057 }
9058 break;
9059 case DW_TAG_typedef:
9060 case DW_TAG_base_type:
a02abb62 9061 case DW_TAG_subrange_type:
31edb802 9062 add_psymbol_to_list (actual_name,
15d034d0 9063 built_actual_name != NULL,
79748972 9064 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9065 psymbol_placement::STATIC,
1762568f 9066 0, cu->language, objfile);
c906108c 9067 break;
74921315 9068 case DW_TAG_imported_declaration:
72bf9492 9069 case DW_TAG_namespace:
31edb802 9070 add_psymbol_to_list (actual_name,
15d034d0 9071 built_actual_name != NULL,
79748972 9072 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9073 psymbol_placement::GLOBAL,
1762568f 9074 0, cu->language, objfile);
72bf9492 9075 break;
530e8392 9076 case DW_TAG_module:
a5fd13a9
BH
9077 /* With Fortran 77 there might be a "BLOCK DATA" module
9078 available without any name. If so, we skip the module as it
9079 doesn't bring any value. */
9080 if (actual_name != nullptr)
31edb802 9081 add_psymbol_to_list (actual_name,
a5fd13a9
BH
9082 built_actual_name != NULL,
9083 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9084 psymbol_placement::GLOBAL,
9085 0, cu->language, objfile);
530e8392 9086 break;
c906108c 9087 case DW_TAG_class_type:
680b30c7 9088 case DW_TAG_interface_type:
c906108c
SS
9089 case DW_TAG_structure_type:
9090 case DW_TAG_union_type:
9091 case DW_TAG_enumeration_type:
fa4028e9
JB
9092 /* Skip external references. The DWARF standard says in the section
9093 about "Structure, Union, and Class Type Entries": "An incomplete
9094 structure, union or class type is represented by a structure,
9095 union or class entry that does not have a byte size attribute
9096 and that has a DW_AT_declaration attribute." */
9097 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 9098 return;
fa4028e9 9099
63d06c5c
DC
9100 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9101 static vs. global. */
31edb802 9102 add_psymbol_to_list (actual_name,
15d034d0 9103 built_actual_name != NULL,
79748972 9104 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9105 cu->language == language_cplus
75aedd27
TT
9106 ? psymbol_placement::GLOBAL
9107 : psymbol_placement::STATIC,
1762568f 9108 0, cu->language, objfile);
c906108c 9109
c906108c
SS
9110 break;
9111 case DW_TAG_enumerator:
31edb802 9112 add_psymbol_to_list (actual_name,
15d034d0 9113 built_actual_name != NULL,
79748972 9114 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9115 cu->language == language_cplus
75aedd27
TT
9116 ? psymbol_placement::GLOBAL
9117 : psymbol_placement::STATIC,
1762568f 9118 0, cu->language, objfile);
c906108c
SS
9119 break;
9120 default:
9121 break;
9122 }
9123}
9124
5c4e30ca
DC
9125/* Read a partial die corresponding to a namespace; also, add a symbol
9126 corresponding to that namespace to the symbol table. NAMESPACE is
9127 the name of the enclosing namespace. */
91c24f0a 9128
72bf9492
DJ
9129static void
9130add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9131 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9132 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9133{
72bf9492 9134 /* Add a symbol for the namespace. */
e7c27a73 9135
72bf9492 9136 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9137
9138 /* Now scan partial symbols in that namespace. */
9139
91c24f0a 9140 if (pdi->has_children)
cdc07690 9141 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9142}
9143
5d7cb8df
JK
9144/* Read a partial die corresponding to a Fortran module. */
9145
9146static void
9147add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9148 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9149{
530e8392
KB
9150 /* Add a symbol for the namespace. */
9151
9152 add_partial_symbol (pdi, cu);
9153
f55ee35c 9154 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9155
9156 if (pdi->has_children)
cdc07690 9157 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9158}
9159
b1dc1806
XR
9160/* Read a partial die corresponding to a subprogram or an inlined
9161 subprogram and create a partial symbol for that subprogram.
9162 When the CU language allows it, this routine also defines a partial
9163 symbol for each nested subprogram that this subprogram contains.
9164 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9165 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9166
cdc07690
YQ
9167 PDI may also be a lexical block, in which case we simply search
9168 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9169 Again, this is only performed when the CU language allows this
9170 type of definitions. */
9171
9172static void
9173add_partial_subprogram (struct partial_die_info *pdi,
9174 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9175 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9176{
b1dc1806 9177 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9178 {
9179 if (pdi->has_pc_info)
9180 {
9181 if (pdi->lowpc < *lowpc)
9182 *lowpc = pdi->lowpc;
9183 if (pdi->highpc > *highpc)
9184 *highpc = pdi->highpc;
cdc07690 9185 if (set_addrmap)
5734ee8b 9186 {
518817b3 9187 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9188 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9189 CORE_ADDR baseaddr;
b926417a
TT
9190 CORE_ADDR this_highpc;
9191 CORE_ADDR this_lowpc;
5734ee8b
DJ
9192
9193 baseaddr = ANOFFSET (objfile->section_offsets,
9194 SECT_OFF_TEXT (objfile));
b926417a
TT
9195 this_lowpc
9196 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9197 pdi->lowpc + baseaddr)
9198 - baseaddr);
9199 this_highpc
9200 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9201 pdi->highpc + baseaddr)
9202 - baseaddr);
d320c2b5 9203 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9204 this_lowpc, this_highpc - 1,
9291a0cd 9205 cu->per_cu->v.psymtab);
5734ee8b 9206 }
481860b3
GB
9207 }
9208
9209 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9210 {
bc30ff58 9211 if (!pdi->is_declaration)
e8d05480
JB
9212 /* Ignore subprogram DIEs that do not have a name, they are
9213 illegal. Do not emit a complaint at this point, we will
9214 do so when we convert this psymtab into a symtab. */
9215 if (pdi->name)
9216 add_partial_symbol (pdi, cu);
bc30ff58
JB
9217 }
9218 }
6e70227d 9219
bc30ff58
JB
9220 if (! pdi->has_children)
9221 return;
9222
0a4b0913 9223 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9224 {
9225 pdi = pdi->die_child;
9226 while (pdi != NULL)
9227 {
52356b79 9228 pdi->fixup (cu);
bc30ff58 9229 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9230 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9231 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9232 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9233 pdi = pdi->die_sibling;
9234 }
9235 }
9236}
9237
91c24f0a
DC
9238/* Read a partial die corresponding to an enumeration type. */
9239
72bf9492
DJ
9240static void
9241add_partial_enumeration (struct partial_die_info *enum_pdi,
9242 struct dwarf2_cu *cu)
91c24f0a 9243{
72bf9492 9244 struct partial_die_info *pdi;
91c24f0a
DC
9245
9246 if (enum_pdi->name != NULL)
72bf9492
DJ
9247 add_partial_symbol (enum_pdi, cu);
9248
9249 pdi = enum_pdi->die_child;
9250 while (pdi)
91c24f0a 9251 {
72bf9492 9252 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9253 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9254 else
72bf9492
DJ
9255 add_partial_symbol (pdi, cu);
9256 pdi = pdi->die_sibling;
91c24f0a 9257 }
91c24f0a
DC
9258}
9259
6caca83c
CC
9260/* Return the initial uleb128 in the die at INFO_PTR. */
9261
9262static unsigned int
d521ce57 9263peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9264{
9265 unsigned int bytes_read;
9266
9267 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9268}
9269
685af9cd
TT
9270/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9271 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9272
4bb7a0a7
DJ
9273 Return the corresponding abbrev, or NULL if the number is zero (indicating
9274 an empty DIE). In either case *BYTES_READ will be set to the length of
9275 the initial number. */
9276
9277static struct abbrev_info *
685af9cd
TT
9278peek_die_abbrev (const die_reader_specs &reader,
9279 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9280{
685af9cd 9281 dwarf2_cu *cu = reader.cu;
518817b3 9282 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9283 unsigned int abbrev_number
9284 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9285
9286 if (abbrev_number == 0)
9287 return NULL;
9288
685af9cd 9289 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9290 if (!abbrev)
9291 {
422b9917 9292 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9293 " at offset %s [in module %s]"),
422b9917 9294 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9295 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9296 }
9297
9298 return abbrev;
9299}
9300
93311388
DE
9301/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9302 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9303 DIE. Any children of the skipped DIEs will also be skipped. */
9304
d521ce57
TT
9305static const gdb_byte *
9306skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9307{
4bb7a0a7
DJ
9308 while (1)
9309 {
685af9cd
TT
9310 unsigned int bytes_read;
9311 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9312
4bb7a0a7
DJ
9313 if (abbrev == NULL)
9314 return info_ptr + bytes_read;
9315 else
dee91e82 9316 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9317 }
9318}
9319
93311388
DE
9320/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9321 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9322 abbrev corresponding to that skipped uleb128 should be passed in
9323 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9324 children. */
9325
d521ce57
TT
9326static const gdb_byte *
9327skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9328 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9329{
9330 unsigned int bytes_read;
9331 struct attribute attr;
dee91e82
DE
9332 bfd *abfd = reader->abfd;
9333 struct dwarf2_cu *cu = reader->cu;
d521ce57 9334 const gdb_byte *buffer = reader->buffer;
f664829e 9335 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9336 unsigned int form, i;
9337
9338 for (i = 0; i < abbrev->num_attrs; i++)
9339 {
9340 /* The only abbrev we care about is DW_AT_sibling. */
9341 if (abbrev->attrs[i].name == DW_AT_sibling)
9342 {
dee91e82 9343 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9344 if (attr.form == DW_FORM_ref_addr)
b98664d3 9345 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9346 else
b9502d3f 9347 {
9c541725
PA
9348 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9349 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9350
9351 if (sibling_ptr < info_ptr)
b98664d3 9352 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9353 else if (sibling_ptr > reader->buffer_end)
9354 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9355 else
9356 return sibling_ptr;
9357 }
4bb7a0a7
DJ
9358 }
9359
9360 /* If it isn't DW_AT_sibling, skip this attribute. */
9361 form = abbrev->attrs[i].form;
9362 skip_attribute:
9363 switch (form)
9364 {
4bb7a0a7 9365 case DW_FORM_ref_addr:
ae411497
TT
9366 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9367 and later it is offset sized. */
9368 if (cu->header.version == 2)
9369 info_ptr += cu->header.addr_size;
9370 else
9371 info_ptr += cu->header.offset_size;
9372 break;
36586728
TT
9373 case DW_FORM_GNU_ref_alt:
9374 info_ptr += cu->header.offset_size;
9375 break;
ae411497 9376 case DW_FORM_addr:
4bb7a0a7
DJ
9377 info_ptr += cu->header.addr_size;
9378 break;
9379 case DW_FORM_data1:
9380 case DW_FORM_ref1:
9381 case DW_FORM_flag:
8fe0f950 9382 case DW_FORM_strx1:
4bb7a0a7
DJ
9383 info_ptr += 1;
9384 break;
2dc7f7b3 9385 case DW_FORM_flag_present:
43988095 9386 case DW_FORM_implicit_const:
2dc7f7b3 9387 break;
4bb7a0a7
DJ
9388 case DW_FORM_data2:
9389 case DW_FORM_ref2:
8fe0f950 9390 case DW_FORM_strx2:
4bb7a0a7
DJ
9391 info_ptr += 2;
9392 break;
8fe0f950
AT
9393 case DW_FORM_strx3:
9394 info_ptr += 3;
9395 break;
4bb7a0a7
DJ
9396 case DW_FORM_data4:
9397 case DW_FORM_ref4:
8fe0f950 9398 case DW_FORM_strx4:
4bb7a0a7
DJ
9399 info_ptr += 4;
9400 break;
9401 case DW_FORM_data8:
9402 case DW_FORM_ref8:
55f1336d 9403 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9404 info_ptr += 8;
9405 break;
0224619f
JK
9406 case DW_FORM_data16:
9407 info_ptr += 16;
9408 break;
4bb7a0a7 9409 case DW_FORM_string:
9b1c24c8 9410 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9411 info_ptr += bytes_read;
9412 break;
2dc7f7b3 9413 case DW_FORM_sec_offset:
4bb7a0a7 9414 case DW_FORM_strp:
36586728 9415 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9416 info_ptr += cu->header.offset_size;
9417 break;
2dc7f7b3 9418 case DW_FORM_exprloc:
4bb7a0a7
DJ
9419 case DW_FORM_block:
9420 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9421 info_ptr += bytes_read;
9422 break;
9423 case DW_FORM_block1:
9424 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9425 break;
9426 case DW_FORM_block2:
9427 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9428 break;
9429 case DW_FORM_block4:
9430 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9431 break;
336d760d 9432 case DW_FORM_addrx:
cf532bd1 9433 case DW_FORM_strx:
4bb7a0a7
DJ
9434 case DW_FORM_sdata:
9435 case DW_FORM_udata:
9436 case DW_FORM_ref_udata:
3019eac3
DE
9437 case DW_FORM_GNU_addr_index:
9438 case DW_FORM_GNU_str_index:
d521ce57 9439 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9440 break;
9441 case DW_FORM_indirect:
9442 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9443 info_ptr += bytes_read;
9444 /* We need to continue parsing from here, so just go back to
9445 the top. */
9446 goto skip_attribute;
9447
9448 default:
3e43a32a
MS
9449 error (_("Dwarf Error: Cannot handle %s "
9450 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9451 dwarf_form_name (form),
9452 bfd_get_filename (abfd));
9453 }
9454 }
9455
9456 if (abbrev->has_children)
dee91e82 9457 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9458 else
9459 return info_ptr;
9460}
9461
93311388 9462/* Locate ORIG_PDI's sibling.
dee91e82 9463 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9464
d521ce57 9465static const gdb_byte *
dee91e82
DE
9466locate_pdi_sibling (const struct die_reader_specs *reader,
9467 struct partial_die_info *orig_pdi,
d521ce57 9468 const gdb_byte *info_ptr)
91c24f0a
DC
9469{
9470 /* Do we know the sibling already? */
72bf9492 9471
91c24f0a
DC
9472 if (orig_pdi->sibling)
9473 return orig_pdi->sibling;
9474
9475 /* Are there any children to deal with? */
9476
9477 if (!orig_pdi->has_children)
9478 return info_ptr;
9479
4bb7a0a7 9480 /* Skip the children the long way. */
91c24f0a 9481
dee91e82 9482 return skip_children (reader, info_ptr);
91c24f0a
DC
9483}
9484
257e7a09 9485/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9486 not NULL. */
c906108c
SS
9487
9488static void
257e7a09
YQ
9489dwarf2_read_symtab (struct partial_symtab *self,
9490 struct objfile *objfile)
c906108c 9491{
ed2dc618
SM
9492 struct dwarf2_per_objfile *dwarf2_per_objfile
9493 = get_dwarf2_per_objfile (objfile);
9494
257e7a09 9495 if (self->readin)
c906108c 9496 {
442e4d9c 9497 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9498 self->filename);
442e4d9c
YQ
9499 }
9500 else
9501 {
9502 if (info_verbose)
c906108c 9503 {
442e4d9c 9504 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9505 self->filename);
442e4d9c 9506 gdb_flush (gdb_stdout);
c906108c 9507 }
c906108c 9508
442e4d9c
YQ
9509 /* If this psymtab is constructed from a debug-only objfile, the
9510 has_section_at_zero flag will not necessarily be correct. We
9511 can get the correct value for this flag by looking at the data
9512 associated with the (presumably stripped) associated objfile. */
9513 if (objfile->separate_debug_objfile_backlink)
9514 {
9515 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9516 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9517
442e4d9c
YQ
9518 dwarf2_per_objfile->has_section_at_zero
9519 = dpo_backlink->has_section_at_zero;
9520 }
b2ab525c 9521
442e4d9c 9522 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9523
257e7a09 9524 psymtab_to_symtab_1 (self);
c906108c 9525
442e4d9c
YQ
9526 /* Finish up the debug error message. */
9527 if (info_verbose)
9528 printf_filtered (_("done.\n"));
c906108c 9529 }
95554aad 9530
ed2dc618 9531 process_cu_includes (dwarf2_per_objfile);
c906108c 9532}
9cdd5dbd
DE
9533\f
9534/* Reading in full CUs. */
c906108c 9535
10b3939b
DJ
9536/* Add PER_CU to the queue. */
9537
9538static void
95554aad
TT
9539queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9540 enum language pretend_language)
10b3939b
DJ
9541{
9542 struct dwarf2_queue_item *item;
9543
9544 per_cu->queued = 1;
8d749320 9545 item = XNEW (struct dwarf2_queue_item);
10b3939b 9546 item->per_cu = per_cu;
95554aad 9547 item->pretend_language = pretend_language;
10b3939b
DJ
9548 item->next = NULL;
9549
9550 if (dwarf2_queue == NULL)
9551 dwarf2_queue = item;
9552 else
9553 dwarf2_queue_tail->next = item;
9554
9555 dwarf2_queue_tail = item;
9556}
9557
89e63ee4
DE
9558/* If PER_CU is not yet queued, add it to the queue.
9559 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9560 dependency.
0907af0c 9561 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9562 meaning either PER_CU is already queued or it is already loaded.
9563
9564 N.B. There is an invariant here that if a CU is queued then it is loaded.
9565 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9566
9567static int
89e63ee4 9568maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9569 struct dwarf2_per_cu_data *per_cu,
9570 enum language pretend_language)
9571{
9572 /* We may arrive here during partial symbol reading, if we need full
9573 DIEs to process an unusual case (e.g. template arguments). Do
9574 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9575 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9576 {
9577 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9578 return 1;
9579 return 0;
9580 }
9581
9582 /* Mark the dependence relation so that we don't flush PER_CU
9583 too early. */
89e63ee4
DE
9584 if (dependent_cu != NULL)
9585 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9586
9587 /* If it's already on the queue, we have nothing to do. */
9588 if (per_cu->queued)
9589 return 0;
9590
9591 /* If the compilation unit is already loaded, just mark it as
9592 used. */
9593 if (per_cu->cu != NULL)
9594 {
9595 per_cu->cu->last_used = 0;
9596 return 0;
9597 }
9598
9599 /* Add it to the queue. */
9600 queue_comp_unit (per_cu, pretend_language);
9601
9602 return 1;
9603}
9604
10b3939b
DJ
9605/* Process the queue. */
9606
9607static void
ed2dc618 9608process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9609{
9610 struct dwarf2_queue_item *item, *next_item;
9611
b4f54984 9612 if (dwarf_read_debug)
45cfd468
DE
9613 {
9614 fprintf_unfiltered (gdb_stdlog,
9615 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9616 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9617 }
9618
03dd20cc
DJ
9619 /* The queue starts out with one item, but following a DIE reference
9620 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9621 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9622 {
cc12ce38
DE
9623 if ((dwarf2_per_objfile->using_index
9624 ? !item->per_cu->v.quick->compunit_symtab
9625 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9626 /* Skip dummy CUs. */
9627 && item->per_cu->cu != NULL)
f4dc4d17
DE
9628 {
9629 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9630 unsigned int debug_print_threshold;
247f5c4f 9631 char buf[100];
f4dc4d17 9632
247f5c4f 9633 if (per_cu->is_debug_types)
f4dc4d17 9634 {
247f5c4f
DE
9635 struct signatured_type *sig_type =
9636 (struct signatured_type *) per_cu;
9637
9d8780f0 9638 sprintf (buf, "TU %s at offset %s",
73be47f5 9639 hex_string (sig_type->signature),
9d8780f0 9640 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9641 /* There can be 100s of TUs.
9642 Only print them in verbose mode. */
9643 debug_print_threshold = 2;
f4dc4d17 9644 }
247f5c4f 9645 else
73be47f5 9646 {
9d8780f0
SM
9647 sprintf (buf, "CU at offset %s",
9648 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9649 debug_print_threshold = 1;
9650 }
247f5c4f 9651
b4f54984 9652 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9653 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9654
9655 if (per_cu->is_debug_types)
9656 process_full_type_unit (per_cu, item->pretend_language);
9657 else
9658 process_full_comp_unit (per_cu, item->pretend_language);
9659
b4f54984 9660 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9661 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9662 }
10b3939b
DJ
9663
9664 item->per_cu->queued = 0;
9665 next_item = item->next;
9666 xfree (item);
9667 }
9668
9669 dwarf2_queue_tail = NULL;
45cfd468 9670
b4f54984 9671 if (dwarf_read_debug)
45cfd468
DE
9672 {
9673 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9674 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9675 }
10b3939b
DJ
9676}
9677
10b3939b
DJ
9678/* Read in full symbols for PST, and anything it depends on. */
9679
c906108c 9680static void
fba45db2 9681psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9682{
10b3939b 9683 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9684 int i;
9685
95554aad
TT
9686 if (pst->readin)
9687 return;
9688
aaa75496 9689 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9690 if (!pst->dependencies[i]->readin
9691 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9692 {
9693 /* Inform about additional files that need to be read in. */
9694 if (info_verbose)
9695 {
a3f17187 9696 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9697 fputs_filtered (" ", gdb_stdout);
9698 wrap_here ("");
9699 fputs_filtered ("and ", gdb_stdout);
9700 wrap_here ("");
9701 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9702 wrap_here (""); /* Flush output. */
aaa75496
JB
9703 gdb_flush (gdb_stdout);
9704 }
9705 psymtab_to_symtab_1 (pst->dependencies[i]);
9706 }
9707
9a3c8263 9708 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9709
9710 if (per_cu == NULL)
aaa75496
JB
9711 {
9712 /* It's an include file, no symbols to read for it.
9713 Everything is in the parent symtab. */
9714 pst->readin = 1;
9715 return;
9716 }
c906108c 9717
58f0c718 9718 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9719}
9720
dee91e82
DE
9721/* Trivial hash function for die_info: the hash value of a DIE
9722 is its offset in .debug_info for this objfile. */
10b3939b 9723
dee91e82
DE
9724static hashval_t
9725die_hash (const void *item)
10b3939b 9726{
9a3c8263 9727 const struct die_info *die = (const struct die_info *) item;
6502dd73 9728
9c541725 9729 return to_underlying (die->sect_off);
dee91e82 9730}
63d06c5c 9731
dee91e82
DE
9732/* Trivial comparison function for die_info structures: two DIEs
9733 are equal if they have the same offset. */
98bfdba5 9734
dee91e82
DE
9735static int
9736die_eq (const void *item_lhs, const void *item_rhs)
9737{
9a3c8263
SM
9738 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9739 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9740
9c541725 9741 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9742}
c906108c 9743
dee91e82
DE
9744/* die_reader_func for load_full_comp_unit.
9745 This is identical to read_signatured_type_reader,
9746 but is kept separate for now. */
c906108c 9747
dee91e82
DE
9748static void
9749load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9750 const gdb_byte *info_ptr,
dee91e82
DE
9751 struct die_info *comp_unit_die,
9752 int has_children,
9753 void *data)
9754{
9755 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9756 enum language *language_ptr = (enum language *) data;
6caca83c 9757
dee91e82
DE
9758 gdb_assert (cu->die_hash == NULL);
9759 cu->die_hash =
9760 htab_create_alloc_ex (cu->header.length / 12,
9761 die_hash,
9762 die_eq,
9763 NULL,
9764 &cu->comp_unit_obstack,
9765 hashtab_obstack_allocate,
9766 dummy_obstack_deallocate);
e142c38c 9767
dee91e82
DE
9768 if (has_children)
9769 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9770 &info_ptr, comp_unit_die);
9771 cu->dies = comp_unit_die;
9772 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9773
9774 /* We try not to read any attributes in this function, because not
9cdd5dbd 9775 all CUs needed for references have been loaded yet, and symbol
10b3939b 9776 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9777 or we won't be able to build types correctly.
9778 Similarly, if we do not read the producer, we can not apply
9779 producer-specific interpretation. */
95554aad 9780 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9781}
10b3939b 9782
dee91e82 9783/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9784
dee91e82 9785static void
95554aad 9786load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9787 bool skip_partial,
95554aad 9788 enum language pretend_language)
dee91e82 9789{
3019eac3 9790 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9791
58f0c718 9792 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9793 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9794}
9795
3da10d80
KS
9796/* Add a DIE to the delayed physname list. */
9797
9798static void
9799add_to_method_list (struct type *type, int fnfield_index, int index,
9800 const char *name, struct die_info *die,
9801 struct dwarf2_cu *cu)
9802{
9803 struct delayed_method_info mi;
9804 mi.type = type;
9805 mi.fnfield_index = fnfield_index;
9806 mi.index = index;
9807 mi.name = name;
9808 mi.die = die;
c89b44cd 9809 cu->method_list.push_back (mi);
3da10d80
KS
9810}
9811
3693fdb3
PA
9812/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9813 "const" / "volatile". If so, decrements LEN by the length of the
9814 modifier and return true. Otherwise return false. */
9815
9816template<size_t N>
9817static bool
9818check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9819{
9820 size_t mod_len = sizeof (mod) - 1;
9821 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9822 {
9823 len -= mod_len;
9824 return true;
9825 }
9826 return false;
9827}
9828
3da10d80
KS
9829/* Compute the physnames of any methods on the CU's method list.
9830
9831 The computation of method physnames is delayed in order to avoid the
9832 (bad) condition that one of the method's formal parameters is of an as yet
9833 incomplete type. */
9834
9835static void
9836compute_delayed_physnames (struct dwarf2_cu *cu)
9837{
3693fdb3 9838 /* Only C++ delays computing physnames. */
c89b44cd 9839 if (cu->method_list.empty ())
3693fdb3
PA
9840 return;
9841 gdb_assert (cu->language == language_cplus);
9842
52941706 9843 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9844 {
1d06ead6 9845 const char *physname;
3da10d80 9846 struct fn_fieldlist *fn_flp
c89b44cd
TT
9847 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9848 physname = dwarf2_physname (mi.name, mi.die, cu);
9849 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9850 = physname ? physname : "";
3693fdb3
PA
9851
9852 /* Since there's no tag to indicate whether a method is a
9853 const/volatile overload, extract that information out of the
9854 demangled name. */
9855 if (physname != NULL)
9856 {
9857 size_t len = strlen (physname);
9858
9859 while (1)
9860 {
9861 if (physname[len] == ')') /* shortcut */
9862 break;
9863 else if (check_modifier (physname, len, " const"))
c89b44cd 9864 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9865 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9866 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9867 else
9868 break;
9869 }
9870 }
3da10d80 9871 }
c89b44cd
TT
9872
9873 /* The list is no longer needed. */
9874 cu->method_list.clear ();
3da10d80
KS
9875}
9876
a766d390
DE
9877/* Go objects should be embedded in a DW_TAG_module DIE,
9878 and it's not clear if/how imported objects will appear.
9879 To keep Go support simple until that's worked out,
9880 go back through what we've read and create something usable.
9881 We could do this while processing each DIE, and feels kinda cleaner,
9882 but that way is more invasive.
9883 This is to, for example, allow the user to type "p var" or "b main"
9884 without having to specify the package name, and allow lookups
9885 of module.object to work in contexts that use the expression
9886 parser. */
9887
9888static void
9889fixup_go_packaging (struct dwarf2_cu *cu)
9890{
421d1616 9891 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9892 struct pending *list;
9893 int i;
9894
c24bdb02 9895 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9896 list != NULL;
9897 list = list->next)
a766d390
DE
9898 {
9899 for (i = 0; i < list->nsyms; ++i)
9900 {
9901 struct symbol *sym = list->symbol[i];
9902
c1b5c1eb 9903 if (sym->language () == language_go
a766d390
DE
9904 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9905 {
421d1616
TT
9906 gdb::unique_xmalloc_ptr<char> this_package_name
9907 (go_symbol_package_name (sym));
a766d390
DE
9908
9909 if (this_package_name == NULL)
9910 continue;
9911 if (package_name == NULL)
421d1616 9912 package_name = std::move (this_package_name);
a766d390
DE
9913 else
9914 {
518817b3
SM
9915 struct objfile *objfile
9916 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9917 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9918 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9919 (symbol_symtab (sym) != NULL
9920 ? symtab_to_filename_for_display
9921 (symbol_symtab (sym))
e3b94546 9922 : objfile_name (objfile)),
421d1616 9923 this_package_name.get (), package_name.get ());
a766d390
DE
9924 }
9925 }
9926 }
9927 }
9928
9929 if (package_name != NULL)
9930 {
518817b3 9931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9932 const char *saved_package_name
421d1616 9933 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9934 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9935 saved_package_name);
a766d390
DE
9936 struct symbol *sym;
9937
e623cf5d 9938 sym = allocate_symbol (objfile);
d3ecddab 9939 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9940 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9941 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9942 e.g., "main" finds the "main" module and not C's main(). */
9943 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9944 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9945 SYMBOL_TYPE (sym) = type;
9946
c24bdb02 9947 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9948 }
9949}
9950
c9317f21
TT
9951/* Allocate a fully-qualified name consisting of the two parts on the
9952 obstack. */
9953
9954static const char *
9955rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9956{
9957 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9958}
9959
9960/* A helper that allocates a struct discriminant_info to attach to a
9961 union type. */
9962
9963static struct discriminant_info *
9964alloc_discriminant_info (struct type *type, int discriminant_index,
9965 int default_index)
9966{
9967 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9968 gdb_assert (discriminant_index == -1
9969 || (discriminant_index >= 0
9970 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9971 gdb_assert (default_index == -1
c7b15a66 9972 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9973
9974 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9975
9976 struct discriminant_info *disc
9977 = ((struct discriminant_info *)
9978 TYPE_ZALLOC (type,
9979 offsetof (struct discriminant_info, discriminants)
9980 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9981 disc->default_index = default_index;
9982 disc->discriminant_index = discriminant_index;
9983
9984 struct dynamic_prop prop;
9985 prop.kind = PROP_UNDEFINED;
9986 prop.data.baton = disc;
9987
9988 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9989
9990 return disc;
9991}
9992
9993/* Some versions of rustc emitted enums in an unusual way.
9994
9995 Ordinary enums were emitted as unions. The first element of each
9996 structure in the union was named "RUST$ENUM$DISR". This element
9997 held the discriminant.
9998
9999 These versions of Rust also implemented the "non-zero"
10000 optimization. When the enum had two values, and one is empty and
10001 the other holds a pointer that cannot be zero, the pointer is used
10002 as the discriminant, with a zero value meaning the empty variant.
10003 Here, the union's first member is of the form
10004 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10005 where the fieldnos are the indices of the fields that should be
10006 traversed in order to find the field (which may be several fields deep)
10007 and the variantname is the name of the variant of the case when the
10008 field is zero.
10009
10010 This function recognizes whether TYPE is of one of these forms,
10011 and, if so, smashes it to be a variant type. */
10012
10013static void
10014quirk_rust_enum (struct type *type, struct objfile *objfile)
10015{
10016 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10017
10018 /* We don't need to deal with empty enums. */
10019 if (TYPE_NFIELDS (type) == 0)
10020 return;
10021
10022#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10023 if (TYPE_NFIELDS (type) == 1
10024 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10025 {
10026 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10027
10028 /* Decode the field name to find the offset of the
10029 discriminant. */
10030 ULONGEST bit_offset = 0;
10031 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10032 while (name[0] >= '0' && name[0] <= '9')
10033 {
10034 char *tail;
10035 unsigned long index = strtoul (name, &tail, 10);
10036 name = tail;
10037 if (*name != '$'
10038 || index >= TYPE_NFIELDS (field_type)
10039 || (TYPE_FIELD_LOC_KIND (field_type, index)
10040 != FIELD_LOC_KIND_BITPOS))
10041 {
b98664d3 10042 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10043 "[in module %s]"),
10044 TYPE_FIELD_NAME (type, 0),
10045 objfile_name (objfile));
10046 return;
10047 }
10048 ++name;
10049
10050 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10051 field_type = TYPE_FIELD_TYPE (field_type, index);
10052 }
10053
10054 /* Make a union to hold the variants. */
10055 struct type *union_type = alloc_type (objfile);
10056 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10057 TYPE_NFIELDS (union_type) = 3;
10058 TYPE_FIELDS (union_type)
10059 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10060 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10061 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10062
10063 /* Put the discriminant must at index 0. */
10064 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10065 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10066 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10067 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10068
10069 /* The order of fields doesn't really matter, so put the real
10070 field at index 1 and the data-less field at index 2. */
10071 struct discriminant_info *disc
10072 = alloc_discriminant_info (union_type, 0, 1);
10073 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10074 TYPE_FIELD_NAME (union_type, 1)
10075 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10076 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10077 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10078 TYPE_FIELD_NAME (union_type, 1));
10079
10080 const char *dataless_name
10081 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10082 name);
10083 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10084 dataless_name);
10085 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10086 /* NAME points into the original discriminant name, which
10087 already has the correct lifetime. */
10088 TYPE_FIELD_NAME (union_type, 2) = name;
10089 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10090 disc->discriminants[2] = 0;
10091
10092 /* Smash this type to be a structure type. We have to do this
10093 because the type has already been recorded. */
10094 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10095 TYPE_NFIELDS (type) = 1;
10096 TYPE_FIELDS (type)
10097 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10098
10099 /* Install the variant part. */
10100 TYPE_FIELD_TYPE (type, 0) = union_type;
10101 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10102 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10103 }
77c2dba3
TT
10104 /* A union with a single anonymous field is probably an old-style
10105 univariant enum. */
10106 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10107 {
c9317f21
TT
10108 /* Smash this type to be a structure type. We have to do this
10109 because the type has already been recorded. */
10110 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10111
10112 /* Make a union to hold the variants. */
10113 struct type *union_type = alloc_type (objfile);
10114 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10115 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10116 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10117 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10118 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10119
10120 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10121 const char *variant_name
10122 = rust_last_path_segment (TYPE_NAME (field_type));
10123 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10124 TYPE_NAME (field_type)
10125 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10126 TYPE_NAME (type), variant_name);
c9317f21
TT
10127
10128 /* Install the union in the outer struct type. */
10129 TYPE_NFIELDS (type) = 1;
10130 TYPE_FIELDS (type)
10131 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10132 TYPE_FIELD_TYPE (type, 0) = union_type;
10133 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10134 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10135
10136 alloc_discriminant_info (union_type, -1, 0);
10137 }
10138 else
10139 {
10140 struct type *disr_type = nullptr;
10141 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10142 {
10143 disr_type = TYPE_FIELD_TYPE (type, i);
10144
a037790e
TT
10145 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10146 {
10147 /* All fields of a true enum will be structs. */
10148 return;
10149 }
10150 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10151 {
10152 /* Could be data-less variant, so keep going. */
a037790e 10153 disr_type = nullptr;
c9317f21
TT
10154 }
10155 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10156 "RUST$ENUM$DISR") != 0)
10157 {
10158 /* Not a Rust enum. */
10159 return;
10160 }
10161 else
10162 {
10163 /* Found one. */
10164 break;
10165 }
10166 }
10167
10168 /* If we got here without a discriminant, then it's probably
10169 just a union. */
10170 if (disr_type == nullptr)
10171 return;
10172
10173 /* Smash this type to be a structure type. We have to do this
10174 because the type has already been recorded. */
10175 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10176
10177 /* Make a union to hold the variants. */
10178 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10179 struct type *union_type = alloc_type (objfile);
10180 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10181 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10182 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10183 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10184 TYPE_FIELDS (union_type)
10185 = (struct field *) TYPE_ZALLOC (union_type,
10186 (TYPE_NFIELDS (union_type)
10187 * sizeof (struct field)));
10188
10189 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10190 TYPE_NFIELDS (type) * sizeof (struct field));
10191
10192 /* Install the discriminant at index 0 in the union. */
10193 TYPE_FIELD (union_type, 0) = *disr_field;
10194 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10195 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10196
10197 /* Install the union in the outer struct type. */
10198 TYPE_FIELD_TYPE (type, 0) = union_type;
10199 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10200 TYPE_NFIELDS (type) = 1;
10201
10202 /* Set the size and offset of the union type. */
10203 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10204
10205 /* We need a way to find the correct discriminant given a
10206 variant name. For convenience we build a map here. */
10207 struct type *enum_type = FIELD_TYPE (*disr_field);
10208 std::unordered_map<std::string, ULONGEST> discriminant_map;
10209 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10210 {
10211 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10212 {
10213 const char *name
10214 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10215 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10216 }
10217 }
10218
10219 int n_fields = TYPE_NFIELDS (union_type);
10220 struct discriminant_info *disc
10221 = alloc_discriminant_info (union_type, 0, -1);
10222 /* Skip the discriminant here. */
10223 for (int i = 1; i < n_fields; ++i)
10224 {
10225 /* Find the final word in the name of this variant's type.
10226 That name can be used to look up the correct
10227 discriminant. */
10228 const char *variant_name
10229 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10230 i)));
10231
10232 auto iter = discriminant_map.find (variant_name);
10233 if (iter != discriminant_map.end ())
10234 disc->discriminants[i] = iter->second;
10235
bedda9ac 10236 /* Remove the discriminant field, if it exists. */
c9317f21 10237 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10238 if (TYPE_NFIELDS (sub_type) > 0)
10239 {
10240 --TYPE_NFIELDS (sub_type);
10241 ++TYPE_FIELDS (sub_type);
10242 }
c9317f21
TT
10243 TYPE_FIELD_NAME (union_type, i) = variant_name;
10244 TYPE_NAME (sub_type)
10245 = rust_fully_qualify (&objfile->objfile_obstack,
10246 TYPE_NAME (type), variant_name);
10247 }
10248 }
10249}
10250
10251/* Rewrite some Rust unions to be structures with variants parts. */
10252
10253static void
10254rust_union_quirks (struct dwarf2_cu *cu)
10255{
10256 gdb_assert (cu->language == language_rust);
52941706
SM
10257 for (type *type_ : cu->rust_unions)
10258 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10259 /* We don't need this any more. */
10260 cu->rust_unions.clear ();
c9317f21
TT
10261}
10262
95554aad
TT
10263/* Return the symtab for PER_CU. This works properly regardless of
10264 whether we're using the index or psymtabs. */
10265
43f3e411
DE
10266static struct compunit_symtab *
10267get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10268{
ed2dc618 10269 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10270 ? per_cu->v.quick->compunit_symtab
10271 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10272}
10273
10274/* A helper function for computing the list of all symbol tables
10275 included by PER_CU. */
10276
10277static void
4c39bc03 10278recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10279 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10280 struct dwarf2_per_cu_data *per_cu,
43f3e411 10281 struct compunit_symtab *immediate_parent)
95554aad
TT
10282{
10283 void **slot;
43f3e411 10284 struct compunit_symtab *cust;
95554aad
TT
10285
10286 slot = htab_find_slot (all_children, per_cu, INSERT);
10287 if (*slot != NULL)
10288 {
10289 /* This inclusion and its children have been processed. */
10290 return;
10291 }
10292
10293 *slot = per_cu;
10294 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10295 cust = get_compunit_symtab (per_cu);
10296 if (cust != NULL)
ec94af83
DE
10297 {
10298 /* If this is a type unit only add its symbol table if we haven't
10299 seen it yet (type unit per_cu's can share symtabs). */
10300 if (per_cu->is_debug_types)
10301 {
43f3e411 10302 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10303 if (*slot == NULL)
10304 {
43f3e411 10305 *slot = cust;
4c39bc03 10306 result->push_back (cust);
43f3e411
DE
10307 if (cust->user == NULL)
10308 cust->user = immediate_parent;
ec94af83
DE
10309 }
10310 }
10311 else
f9125b6c 10312 {
4c39bc03 10313 result->push_back (cust);
43f3e411
DE
10314 if (cust->user == NULL)
10315 cust->user = immediate_parent;
f9125b6c 10316 }
ec94af83 10317 }
95554aad 10318
ae640021
AB
10319 if (!per_cu->imported_symtabs_empty ())
10320 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10321 {
10322 recursively_compute_inclusions (result, all_children,
10323 all_type_symtabs, ptr, cust);
10324 }
95554aad
TT
10325}
10326
43f3e411 10327/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10328 PER_CU. */
10329
10330static void
43f3e411 10331compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10332{
f4dc4d17
DE
10333 gdb_assert (! per_cu->is_debug_types);
10334
ae640021 10335 if (!per_cu->imported_symtabs_empty ())
95554aad 10336 {
ae640021 10337 int len;
4c39bc03 10338 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10339 htab_t all_children, all_type_symtabs;
43f3e411 10340 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10341
10342 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10343 if (cust == NULL)
95554aad
TT
10344 return;
10345
10346 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10347 NULL, xcalloc, xfree);
ec94af83
DE
10348 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10349 NULL, xcalloc, xfree);
95554aad 10350
ae640021 10351 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10352 {
10353 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10354 all_type_symtabs, ptr, cust);
ec94af83 10355 }
95554aad 10356
ec94af83 10357 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10358 len = result_symtabs.size ();
43f3e411 10359 cust->includes
ed2dc618 10360 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10361 struct compunit_symtab *, len + 1);
4c39bc03
TT
10362 memcpy (cust->includes, result_symtabs.data (),
10363 len * sizeof (compunit_symtab *));
43f3e411 10364 cust->includes[len] = NULL;
95554aad 10365
95554aad 10366 htab_delete (all_children);
ec94af83 10367 htab_delete (all_type_symtabs);
95554aad
TT
10368 }
10369}
10370
10371/* Compute the 'includes' field for the symtabs of all the CUs we just
10372 read. */
10373
10374static void
ed2dc618 10375process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10376{
71b73764 10377 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10378 {
10379 if (! iter->is_debug_types)
43f3e411 10380 compute_compunit_symtab_includes (iter);
f4dc4d17 10381 }
95554aad 10382
c5d0225d 10383 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10384}
10385
9cdd5dbd 10386/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10387 already been loaded into memory. */
10388
10389static void
95554aad
TT
10390process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10391 enum language pretend_language)
10b3939b 10392{
10b3939b 10393 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10394 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10395 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10396 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10397 CORE_ADDR lowpc, highpc;
43f3e411 10398 struct compunit_symtab *cust;
10b3939b 10399 CORE_ADDR baseaddr;
4359dff1 10400 struct block *static_block;
3e29f34a 10401 CORE_ADDR addr;
10b3939b
DJ
10402
10403 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10404
c89b44cd
TT
10405 /* Clear the list here in case something was left over. */
10406 cu->method_list.clear ();
10b3939b 10407
95554aad
TT
10408 cu->language = pretend_language;
10409 cu->language_defn = language_def (cu->language);
10410
c906108c 10411 /* Do line number decoding in read_file_scope () */
10b3939b 10412 process_die (cu->dies, cu);
c906108c 10413
a766d390
DE
10414 /* For now fudge the Go package. */
10415 if (cu->language == language_go)
10416 fixup_go_packaging (cu);
10417
5f48f8f3 10418 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10419 should be complete, and it should now be safe to compute all of the
10420 physnames. */
10421 compute_delayed_physnames (cu);
3da10d80 10422
c9317f21
TT
10423 if (cu->language == language_rust)
10424 rust_union_quirks (cu);
10425
fae299cd
DC
10426 /* Some compilers don't define a DW_AT_high_pc attribute for the
10427 compilation unit. If the DW_AT_high_pc is missing, synthesize
10428 it, by scanning the DIE's below the compilation unit. */
10b3939b 10429 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10430
3e29f34a 10431 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10432 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10433
10434 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10435 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10436 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10437 addrmap to help ensure it has an accurate map of pc values belonging to
10438 this comp unit. */
10439 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10440
c24bdb02 10441 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10442 SECT_OFF_TEXT (objfile),
10443 0);
c906108c 10444
43f3e411 10445 if (cust != NULL)
c906108c 10446 {
df15bd07 10447 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10448
8be455d7
JK
10449 /* Set symtab language to language from DW_AT_language. If the
10450 compilation is from a C file generated by language preprocessors, do
10451 not set the language if it was already deduced by start_subfile. */
43f3e411 10452 if (!(cu->language == language_c
40e3ad0e 10453 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10454 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10455
10456 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10457 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10458 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10459 there were bugs in prologue debug info, fixed later in GCC-4.5
10460 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10461
10462 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10463 needed, it would be wrong due to missing DW_AT_producer there.
10464
10465 Still one can confuse GDB by using non-standard GCC compilation
10466 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10467 */
ab260dad 10468 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10469 cust->locations_valid = 1;
e0d00bc7
JK
10470
10471 if (gcc_4_minor >= 5)
43f3e411 10472 cust->epilogue_unwind_valid = 1;
96408a79 10473
43f3e411 10474 cust->call_site_htab = cu->call_site_htab;
c906108c 10475 }
9291a0cd
TT
10476
10477 if (dwarf2_per_objfile->using_index)
43f3e411 10478 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10479 else
10480 {
10481 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10482 pst->compunit_symtab = cust;
9291a0cd
TT
10483 pst->readin = 1;
10484 }
c906108c 10485
95554aad 10486 /* Push it for inclusion processing later. */
c5d0225d 10487 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10488
10489 /* Not needed any more. */
c24bdb02 10490 cu->reset_builder ();
f4dc4d17 10491}
45cfd468 10492
f4dc4d17
DE
10493/* Generate full symbol information for type unit PER_CU, whose DIEs have
10494 already been loaded into memory. */
10495
10496static void
10497process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10498 enum language pretend_language)
10499{
10500 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10501 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10502 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10503 struct compunit_symtab *cust;
0186c6a7
DE
10504 struct signatured_type *sig_type;
10505
10506 gdb_assert (per_cu->is_debug_types);
10507 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10508
c89b44cd
TT
10509 /* Clear the list here in case something was left over. */
10510 cu->method_list.clear ();
f4dc4d17 10511
f4dc4d17
DE
10512 cu->language = pretend_language;
10513 cu->language_defn = language_def (cu->language);
10514
10515 /* The symbol tables are set up in read_type_unit_scope. */
10516 process_die (cu->dies, cu);
10517
10518 /* For now fudge the Go package. */
10519 if (cu->language == language_go)
10520 fixup_go_packaging (cu);
10521
5f48f8f3 10522 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10523 should be complete, and it should now be safe to compute all of the
10524 physnames. */
10525 compute_delayed_physnames (cu);
f4dc4d17 10526
c9317f21
TT
10527 if (cu->language == language_rust)
10528 rust_union_quirks (cu);
10529
f4dc4d17
DE
10530 /* TUs share symbol tables.
10531 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10532 of it with end_expandable_symtab. Otherwise, complete the addition of
10533 this TU's symbols to the existing symtab. */
43f3e411 10534 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10535 {
c24bdb02
KS
10536 buildsym_compunit *builder = cu->get_builder ();
10537 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10538 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10539
43f3e411 10540 if (cust != NULL)
f4dc4d17
DE
10541 {
10542 /* Set symtab language to language from DW_AT_language. If the
10543 compilation is from a C file generated by language preprocessors,
10544 do not set the language if it was already deduced by
10545 start_subfile. */
43f3e411
DE
10546 if (!(cu->language == language_c
10547 && COMPUNIT_FILETABS (cust)->language != language_c))
10548 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10549 }
10550 }
10551 else
10552 {
c24bdb02 10553 cu->get_builder ()->augment_type_symtab ();
43f3e411 10554 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10555 }
10556
10557 if (dwarf2_per_objfile->using_index)
43f3e411 10558 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10559 else
10560 {
10561 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10562 pst->compunit_symtab = cust;
f4dc4d17 10563 pst->readin = 1;
45cfd468 10564 }
804d2729
TT
10565
10566 /* Not needed any more. */
c24bdb02 10567 cu->reset_builder ();
c906108c
SS
10568}
10569
95554aad
TT
10570/* Process an imported unit DIE. */
10571
10572static void
10573process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10574{
10575 struct attribute *attr;
10576
f4dc4d17
DE
10577 /* For now we don't handle imported units in type units. */
10578 if (cu->per_cu->is_debug_types)
10579 {
10580 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10581 " supported in type units [in module %s]"),
518817b3 10582 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10583 }
10584
95554aad
TT
10585 attr = dwarf2_attr (die, DW_AT_import, cu);
10586 if (attr != NULL)
10587 {
9c541725
PA
10588 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10589 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10590 dwarf2_per_cu_data *per_cu
e3b94546 10591 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10592 cu->per_cu->dwarf2_per_objfile);
95554aad 10593
69d751e3 10594 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10595 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10596 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10597
ae640021 10598 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10599 }
10600}
10601
4c8aa72d
PA
10602/* RAII object that represents a process_die scope: i.e.,
10603 starts/finishes processing a DIE. */
10604class process_die_scope
adde2bff 10605{
4c8aa72d
PA
10606public:
10607 process_die_scope (die_info *die, dwarf2_cu *cu)
10608 : m_die (die), m_cu (cu)
10609 {
10610 /* We should only be processing DIEs not already in process. */
10611 gdb_assert (!m_die->in_process);
10612 m_die->in_process = true;
10613 }
8c3cb9fa 10614
4c8aa72d
PA
10615 ~process_die_scope ()
10616 {
10617 m_die->in_process = false;
10618
10619 /* If we're done processing the DIE for the CU that owns the line
10620 header, we don't need the line header anymore. */
10621 if (m_cu->line_header_die_owner == m_die)
10622 {
10623 delete m_cu->line_header;
10624 m_cu->line_header = NULL;
10625 m_cu->line_header_die_owner = NULL;
10626 }
10627 }
10628
10629private:
10630 die_info *m_die;
10631 dwarf2_cu *m_cu;
10632};
adde2bff 10633
c906108c
SS
10634/* Process a die and its children. */
10635
10636static void
e7c27a73 10637process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10638{
4c8aa72d 10639 process_die_scope scope (die, cu);
adde2bff 10640
c906108c
SS
10641 switch (die->tag)
10642 {
10643 case DW_TAG_padding:
10644 break;
10645 case DW_TAG_compile_unit:
95554aad 10646 case DW_TAG_partial_unit:
e7c27a73 10647 read_file_scope (die, cu);
c906108c 10648 break;
348e048f
DE
10649 case DW_TAG_type_unit:
10650 read_type_unit_scope (die, cu);
10651 break;
c906108c 10652 case DW_TAG_subprogram:
0a4b0913
AB
10653 /* Nested subprograms in Fortran get a prefix. */
10654 if (cu->language == language_fortran
10655 && die->parent != NULL
10656 && die->parent->tag == DW_TAG_subprogram)
10657 cu->processing_has_namespace_info = true;
10658 /* Fall through. */
c906108c 10659 case DW_TAG_inlined_subroutine:
edb3359d 10660 read_func_scope (die, cu);
c906108c
SS
10661 break;
10662 case DW_TAG_lexical_block:
14898363
L
10663 case DW_TAG_try_block:
10664 case DW_TAG_catch_block:
e7c27a73 10665 read_lexical_block_scope (die, cu);
c906108c 10666 break;
216f72a1 10667 case DW_TAG_call_site:
96408a79
SA
10668 case DW_TAG_GNU_call_site:
10669 read_call_site_scope (die, cu);
10670 break;
c906108c 10671 case DW_TAG_class_type:
680b30c7 10672 case DW_TAG_interface_type:
c906108c
SS
10673 case DW_TAG_structure_type:
10674 case DW_TAG_union_type:
134d01f1 10675 process_structure_scope (die, cu);
c906108c
SS
10676 break;
10677 case DW_TAG_enumeration_type:
134d01f1 10678 process_enumeration_scope (die, cu);
c906108c 10679 break;
134d01f1 10680
f792889a
DJ
10681 /* These dies have a type, but processing them does not create
10682 a symbol or recurse to process the children. Therefore we can
10683 read them on-demand through read_type_die. */
c906108c 10684 case DW_TAG_subroutine_type:
72019c9c 10685 case DW_TAG_set_type:
c906108c 10686 case DW_TAG_array_type:
c906108c 10687 case DW_TAG_pointer_type:
c906108c 10688 case DW_TAG_ptr_to_member_type:
c906108c 10689 case DW_TAG_reference_type:
4297a3f0 10690 case DW_TAG_rvalue_reference_type:
c906108c 10691 case DW_TAG_string_type:
c906108c 10692 break;
134d01f1 10693
c906108c 10694 case DW_TAG_base_type:
a02abb62 10695 case DW_TAG_subrange_type:
cb249c71 10696 case DW_TAG_typedef:
134d01f1
DJ
10697 /* Add a typedef symbol for the type definition, if it has a
10698 DW_AT_name. */
f792889a 10699 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10700 break;
c906108c 10701 case DW_TAG_common_block:
e7c27a73 10702 read_common_block (die, cu);
c906108c
SS
10703 break;
10704 case DW_TAG_common_inclusion:
10705 break;
d9fa45fe 10706 case DW_TAG_namespace:
9068261f 10707 cu->processing_has_namespace_info = true;
e7c27a73 10708 read_namespace (die, cu);
d9fa45fe 10709 break;
5d7cb8df 10710 case DW_TAG_module:
9068261f 10711 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10712 read_module (die, cu);
10713 break;
d9fa45fe 10714 case DW_TAG_imported_declaration:
9068261f 10715 cu->processing_has_namespace_info = true;
74921315
KS
10716 if (read_namespace_alias (die, cu))
10717 break;
86a73007
TT
10718 /* The declaration is not a global namespace alias. */
10719 /* Fall through. */
d9fa45fe 10720 case DW_TAG_imported_module:
9068261f 10721 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10722 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10723 || cu->language != language_fortran))
b98664d3 10724 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10725 dwarf_tag_name (die->tag));
10726 read_import_statement (die, cu);
d9fa45fe 10727 break;
95554aad
TT
10728
10729 case DW_TAG_imported_unit:
10730 process_imported_unit_die (die, cu);
10731 break;
10732
71a3c369
TT
10733 case DW_TAG_variable:
10734 read_variable (die, cu);
10735 break;
10736
c906108c 10737 default:
e7c27a73 10738 new_symbol (die, NULL, cu);
c906108c
SS
10739 break;
10740 }
10741}
ca69b9e6
DE
10742\f
10743/* DWARF name computation. */
c906108c 10744
94af9270
KS
10745/* A helper function for dwarf2_compute_name which determines whether DIE
10746 needs to have the name of the scope prepended to the name listed in the
10747 die. */
10748
10749static int
10750die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10751{
1c809c68
TT
10752 struct attribute *attr;
10753
94af9270
KS
10754 switch (die->tag)
10755 {
10756 case DW_TAG_namespace:
10757 case DW_TAG_typedef:
10758 case DW_TAG_class_type:
10759 case DW_TAG_interface_type:
10760 case DW_TAG_structure_type:
10761 case DW_TAG_union_type:
10762 case DW_TAG_enumeration_type:
10763 case DW_TAG_enumerator:
10764 case DW_TAG_subprogram:
08a76f8a 10765 case DW_TAG_inlined_subroutine:
94af9270 10766 case DW_TAG_member:
74921315 10767 case DW_TAG_imported_declaration:
94af9270
KS
10768 return 1;
10769
10770 case DW_TAG_variable:
c2b0a229 10771 case DW_TAG_constant:
94af9270
KS
10772 /* We only need to prefix "globally" visible variables. These include
10773 any variable marked with DW_AT_external or any variable that
10774 lives in a namespace. [Variables in anonymous namespaces
10775 require prefixing, but they are not DW_AT_external.] */
10776
10777 if (dwarf2_attr (die, DW_AT_specification, cu))
10778 {
10779 struct dwarf2_cu *spec_cu = cu;
9a619af0 10780
94af9270
KS
10781 return die_needs_namespace (die_specification (die, &spec_cu),
10782 spec_cu);
10783 }
10784
1c809c68 10785 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10786 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10787 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10788 return 0;
10789 /* A variable in a lexical block of some kind does not need a
10790 namespace, even though in C++ such variables may be external
10791 and have a mangled name. */
10792 if (die->parent->tag == DW_TAG_lexical_block
10793 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10794 || die->parent->tag == DW_TAG_catch_block
10795 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10796 return 0;
10797 return 1;
94af9270
KS
10798
10799 default:
10800 return 0;
10801 }
10802}
10803
73b9be8b
KS
10804/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10805 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10806 defined for the given DIE. */
10807
10808static struct attribute *
10809dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10810{
10811 struct attribute *attr;
10812
10813 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10814 if (attr == NULL)
10815 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10816
10817 return attr;
10818}
10819
10820/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10821 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10822 defined for the given DIE. */
10823
10824static const char *
10825dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10826{
10827 const char *linkage_name;
10828
10829 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10830 if (linkage_name == NULL)
10831 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10832
10833 return linkage_name;
10834}
10835
94af9270 10836/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10837 compute the physname for the object, which include a method's:
9c37b5ae 10838 - formal parameters (C++),
a766d390 10839 - receiver type (Go),
a766d390
DE
10840
10841 The term "physname" is a bit confusing.
10842 For C++, for example, it is the demangled name.
10843 For Go, for example, it's the mangled name.
94af9270 10844
af6b7be1
JB
10845 For Ada, return the DIE's linkage name rather than the fully qualified
10846 name. PHYSNAME is ignored..
10847
94af9270
KS
10848 The result is allocated on the objfile_obstack and canonicalized. */
10849
10850static const char *
15d034d0
TT
10851dwarf2_compute_name (const char *name,
10852 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10853 int physname)
10854{
518817b3 10855 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10856
94af9270
KS
10857 if (name == NULL)
10858 name = dwarf2_name (die, cu);
10859
2ee7123e
DE
10860 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10861 but otherwise compute it by typename_concat inside GDB.
10862 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10863 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10864 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10865 will set the demangled name to the result of dwarf2_full_name, and it is
10866 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10867 if (cu->language == language_ada
10868 || (cu->language == language_fortran && physname))
10869 {
10870 /* For Ada unit, we prefer the linkage name over the name, as
10871 the former contains the exported name, which the user expects
10872 to be able to reference. Ideally, we want the user to be able
10873 to reference this entity using either natural or linkage name,
10874 but we haven't started looking at this enhancement yet. */
73b9be8b 10875 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10876
2ee7123e
DE
10877 if (linkage_name != NULL)
10878 return linkage_name;
f55ee35c
JK
10879 }
10880
94af9270
KS
10881 /* These are the only languages we know how to qualify names in. */
10882 if (name != NULL
9c37b5ae 10883 && (cu->language == language_cplus
c44af4eb
TT
10884 || cu->language == language_fortran || cu->language == language_d
10885 || cu->language == language_rust))
94af9270
KS
10886 {
10887 if (die_needs_namespace (die, cu))
10888 {
0d5cff50 10889 const char *prefix;
34a68019 10890 const char *canonical_name = NULL;
94af9270 10891
d7e74731
PA
10892 string_file buf;
10893
94af9270 10894 prefix = determine_prefix (die, cu);
94af9270
KS
10895 if (*prefix != '\0')
10896 {
43816ebc
TT
10897 gdb::unique_xmalloc_ptr<char> prefixed_name
10898 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10899
43816ebc 10900 buf.puts (prefixed_name.get ());
94af9270
KS
10901 }
10902 else
d7e74731 10903 buf.puts (name);
94af9270 10904
98bfdba5
PA
10905 /* Template parameters may be specified in the DIE's DW_AT_name, or
10906 as children with DW_TAG_template_type_param or
10907 DW_TAG_value_type_param. If the latter, add them to the name
10908 here. If the name already has template parameters, then
10909 skip this step; some versions of GCC emit both, and
10910 it is more efficient to use the pre-computed name.
10911
10912 Something to keep in mind about this process: it is very
10913 unlikely, or in some cases downright impossible, to produce
10914 something that will match the mangled name of a function.
10915 If the definition of the function has the same debug info,
10916 we should be able to match up with it anyway. But fallbacks
10917 using the minimal symbol, for instance to find a method
10918 implemented in a stripped copy of libstdc++, will not work.
10919 If we do not have debug info for the definition, we will have to
10920 match them up some other way.
10921
10922 When we do name matching there is a related problem with function
10923 templates; two instantiated function templates are allowed to
10924 differ only by their return types, which we do not add here. */
10925
10926 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10927 {
10928 struct attribute *attr;
10929 struct die_info *child;
10930 int first = 1;
10931
10932 die->building_fullname = 1;
10933
10934 for (child = die->child; child != NULL; child = child->sibling)
10935 {
10936 struct type *type;
12df843f 10937 LONGEST value;
d521ce57 10938 const gdb_byte *bytes;
98bfdba5
PA
10939 struct dwarf2_locexpr_baton *baton;
10940 struct value *v;
10941
10942 if (child->tag != DW_TAG_template_type_param
10943 && child->tag != DW_TAG_template_value_param)
10944 continue;
10945
10946 if (first)
10947 {
d7e74731 10948 buf.puts ("<");
98bfdba5
PA
10949 first = 0;
10950 }
10951 else
d7e74731 10952 buf.puts (", ");
98bfdba5
PA
10953
10954 attr = dwarf2_attr (child, DW_AT_type, cu);
10955 if (attr == NULL)
10956 {
b98664d3 10957 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10958 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10959 continue;
10960 }
10961 type = die_type (child, cu);
10962
10963 if (child->tag == DW_TAG_template_type_param)
10964 {
c1ec8cea
TT
10965 c_print_type (type, "", &buf, -1, 0, cu->language,
10966 &type_print_raw_options);
98bfdba5
PA
10967 continue;
10968 }
10969
10970 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10971 if (attr == NULL)
10972 {
b98664d3 10973 complaint (_("template parameter missing "
3e43a32a 10974 "DW_AT_const_value"));
d7e74731 10975 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10976 continue;
10977 }
10978
10979 dwarf2_const_value_attr (attr, type, name,
10980 &cu->comp_unit_obstack, cu,
10981 &value, &bytes, &baton);
10982
10983 if (TYPE_NOSIGN (type))
10984 /* GDB prints characters as NUMBER 'CHAR'. If that's
10985 changed, this can use value_print instead. */
d7e74731 10986 c_printchar (value, type, &buf);
98bfdba5
PA
10987 else
10988 {
10989 struct value_print_options opts;
10990
10991 if (baton != NULL)
10992 v = dwarf2_evaluate_loc_desc (type, NULL,
10993 baton->data,
10994 baton->size,
10995 baton->per_cu);
10996 else if (bytes != NULL)
10997 {
10998 v = allocate_value (type);
10999 memcpy (value_contents_writeable (v), bytes,
11000 TYPE_LENGTH (type));
11001 }
11002 else
11003 v = value_from_longest (type, value);
11004
3e43a32a
MS
11005 /* Specify decimal so that we do not depend on
11006 the radix. */
98bfdba5
PA
11007 get_formatted_print_options (&opts, 'd');
11008 opts.raw = 1;
d7e74731 11009 value_print (v, &buf, &opts);
98bfdba5 11010 release_value (v);
98bfdba5
PA
11011 }
11012 }
11013
11014 die->building_fullname = 0;
11015
11016 if (!first)
11017 {
11018 /* Close the argument list, with a space if necessary
11019 (nested templates). */
d7e74731
PA
11020 if (!buf.empty () && buf.string ().back () == '>')
11021 buf.puts (" >");
98bfdba5 11022 else
d7e74731 11023 buf.puts (">");
98bfdba5
PA
11024 }
11025 }
11026
9c37b5ae 11027 /* For C++ methods, append formal parameter type
94af9270 11028 information, if PHYSNAME. */
6e70227d 11029
94af9270 11030 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11031 && cu->language == language_cplus)
94af9270
KS
11032 {
11033 struct type *type = read_type_die (die, cu);
11034
d7e74731 11035 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11036 &type_print_raw_options);
94af9270 11037
9c37b5ae 11038 if (cu->language == language_cplus)
94af9270 11039 {
60430eff
DJ
11040 /* Assume that an artificial first parameter is
11041 "this", but do not crash if it is not. RealView
11042 marks unnamed (and thus unused) parameters as
11043 artificial; there is no way to differentiate
11044 the two cases. */
94af9270
KS
11045 if (TYPE_NFIELDS (type) > 0
11046 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11047 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11048 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11049 0))))
d7e74731 11050 buf.puts (" const");
94af9270
KS
11051 }
11052 }
11053
d7e74731 11054 const std::string &intermediate_name = buf.string ();
94af9270
KS
11055
11056 if (cu->language == language_cplus)
34a68019 11057 canonical_name
322a8516 11058 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11059 &objfile->per_bfd->storage_obstack);
11060
11061 /* If we only computed INTERMEDIATE_NAME, or if
11062 INTERMEDIATE_NAME is already canonical, then we need to
11063 copy it to the appropriate obstack. */
322a8516 11064 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11065 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11066 intermediate_name);
34a68019
TT
11067 else
11068 name = canonical_name;
94af9270
KS
11069 }
11070 }
11071
11072 return name;
11073}
11074
0114d602
DJ
11075/* Return the fully qualified name of DIE, based on its DW_AT_name.
11076 If scope qualifiers are appropriate they will be added. The result
34a68019 11077 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11078 not have a name. NAME may either be from a previous call to
11079 dwarf2_name or NULL.
11080
9c37b5ae 11081 The output string will be canonicalized (if C++). */
0114d602
DJ
11082
11083static const char *
15d034d0 11084dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11085{
94af9270
KS
11086 return dwarf2_compute_name (name, die, cu, 0);
11087}
0114d602 11088
94af9270
KS
11089/* Construct a physname for the given DIE in CU. NAME may either be
11090 from a previous call to dwarf2_name or NULL. The result will be
11091 allocated on the objfile_objstack or NULL if the DIE does not have a
11092 name.
0114d602 11093
9c37b5ae 11094 The output string will be canonicalized (if C++). */
0114d602 11095
94af9270 11096static const char *
15d034d0 11097dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11098{
518817b3 11099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11100 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11101 int need_copy = 1;
11102
11103 /* In this case dwarf2_compute_name is just a shortcut not building anything
11104 on its own. */
11105 if (!die_needs_namespace (die, cu))
11106 return dwarf2_compute_name (name, die, cu, 1);
11107
73b9be8b 11108 mangled = dw2_linkage_name (die, cu);
900e11f9 11109
e98c9e7c
TT
11110 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11111 See https://github.com/rust-lang/rust/issues/32925. */
11112 if (cu->language == language_rust && mangled != NULL
11113 && strchr (mangled, '{') != NULL)
11114 mangled = NULL;
11115
900e11f9
JK
11116 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11117 has computed. */
791afaa2 11118 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11119 if (mangled != NULL)
900e11f9 11120 {
900e11f9 11121
59cc4834
JB
11122 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11123 {
11124 /* Do nothing (do not demangle the symbol name). */
11125 }
11126 else if (cu->language == language_go)
a766d390 11127 {
5e2db402
TT
11128 /* This is a lie, but we already lie to the caller new_symbol.
11129 new_symbol assumes we return the mangled name.
a766d390 11130 This just undoes that lie until things are cleaned up. */
a766d390
DE
11131 }
11132 else
11133 {
0eb876f5
JB
11134 /* Use DMGL_RET_DROP for C++ template functions to suppress
11135 their return type. It is easier for GDB users to search
11136 for such functions as `name(params)' than `long name(params)'.
11137 In such case the minimal symbol names do not match the full
11138 symbol names but for template functions there is never a need
11139 to look up their definition from their declaration so
11140 the only disadvantage remains the minimal symbol variant
11141 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11142 demangled.reset (gdb_demangle (mangled,
11143 (DMGL_PARAMS | DMGL_ANSI
11144 | DMGL_RET_DROP)));
a766d390 11145 }
900e11f9 11146 if (demangled)
791afaa2 11147 canon = demangled.get ();
900e11f9
JK
11148 else
11149 {
11150 canon = mangled;
11151 need_copy = 0;
11152 }
11153 }
11154
11155 if (canon == NULL || check_physname)
11156 {
11157 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11158
11159 if (canon != NULL && strcmp (physname, canon) != 0)
11160 {
11161 /* It may not mean a bug in GDB. The compiler could also
11162 compute DW_AT_linkage_name incorrectly. But in such case
11163 GDB would need to be bug-to-bug compatible. */
11164
b98664d3 11165 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11166 "(from linkage <%s>) - DIE at %s [in module %s]"),
11167 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11168 objfile_name (objfile));
900e11f9
JK
11169
11170 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11171 is available here - over computed PHYSNAME. It is safer
11172 against both buggy GDB and buggy compilers. */
11173
11174 retval = canon;
11175 }
11176 else
11177 {
11178 retval = physname;
11179 need_copy = 0;
11180 }
11181 }
11182 else
11183 retval = canon;
11184
11185 if (need_copy)
021887d8 11186 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11187
900e11f9 11188 return retval;
0114d602
DJ
11189}
11190
74921315
KS
11191/* Inspect DIE in CU for a namespace alias. If one exists, record
11192 a new symbol for it.
11193
11194 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11195
11196static int
11197read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11198{
11199 struct attribute *attr;
11200
11201 /* If the die does not have a name, this is not a namespace
11202 alias. */
11203 attr = dwarf2_attr (die, DW_AT_name, cu);
11204 if (attr != NULL)
11205 {
11206 int num;
11207 struct die_info *d = die;
11208 struct dwarf2_cu *imported_cu = cu;
11209
11210 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11211 keep inspecting DIEs until we hit the underlying import. */
11212#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11213 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11214 {
11215 attr = dwarf2_attr (d, DW_AT_import, cu);
11216 if (attr == NULL)
11217 break;
11218
11219 d = follow_die_ref (d, attr, &imported_cu);
11220 if (d->tag != DW_TAG_imported_declaration)
11221 break;
11222 }
11223
11224 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11225 {
b98664d3 11226 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11227 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11228 return 0;
11229 }
11230
11231 if (attr != NULL)
11232 {
11233 struct type *type;
9c541725 11234 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11235
9c541725 11236 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11237 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11238 {
11239 /* This declaration is a global namespace alias. Add
11240 a symbol for it whose type is the aliased namespace. */
11241 new_symbol (die, type, cu);
11242 return 1;
11243 }
11244 }
11245 }
11246
11247 return 0;
11248}
11249
22cee43f 11250/* Return the using directives repository (global or local?) to use in the
804d2729 11251 current context for CU.
22cee43f
PMR
11252
11253 For Ada, imported declarations can materialize renamings, which *may* be
11254 global. However it is impossible (for now?) in DWARF to distinguish
11255 "external" imported declarations and "static" ones. As all imported
11256 declarations seem to be static in all other languages, make them all CU-wide
11257 global only in Ada. */
11258
11259static struct using_direct **
804d2729 11260using_directives (struct dwarf2_cu *cu)
22cee43f 11261{
c24bdb02
KS
11262 if (cu->language == language_ada
11263 && cu->get_builder ()->outermost_context_p ())
11264 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11265 else
c24bdb02 11266 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11267}
11268
27aa8d6a
SW
11269/* Read the import statement specified by the given die and record it. */
11270
11271static void
11272read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11273{
518817b3 11274 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11275 struct attribute *import_attr;
32019081 11276 struct die_info *imported_die, *child_die;
de4affc9 11277 struct dwarf2_cu *imported_cu;
27aa8d6a 11278 const char *imported_name;
794684b6 11279 const char *imported_name_prefix;
13387711
SW
11280 const char *canonical_name;
11281 const char *import_alias;
11282 const char *imported_declaration = NULL;
794684b6 11283 const char *import_prefix;
eb1e02fd 11284 std::vector<const char *> excludes;
13387711 11285
27aa8d6a
SW
11286 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11287 if (import_attr == NULL)
11288 {
b98664d3 11289 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11290 dwarf_tag_name (die->tag));
11291 return;
11292 }
11293
de4affc9
CC
11294 imported_cu = cu;
11295 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11296 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11297 if (imported_name == NULL)
11298 {
11299 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11300
11301 The import in the following code:
11302 namespace A
11303 {
11304 typedef int B;
11305 }
11306
11307 int main ()
11308 {
11309 using A::B;
11310 B b;
11311 return b;
11312 }
11313
11314 ...
11315 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11316 <52> DW_AT_decl_file : 1
11317 <53> DW_AT_decl_line : 6
11318 <54> DW_AT_import : <0x75>
11319 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11320 <59> DW_AT_name : B
11321 <5b> DW_AT_decl_file : 1
11322 <5c> DW_AT_decl_line : 2
11323 <5d> DW_AT_type : <0x6e>
11324 ...
11325 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11326 <76> DW_AT_byte_size : 4
11327 <77> DW_AT_encoding : 5 (signed)
11328
11329 imports the wrong die ( 0x75 instead of 0x58 ).
11330 This case will be ignored until the gcc bug is fixed. */
11331 return;
11332 }
11333
82856980
SW
11334 /* Figure out the local name after import. */
11335 import_alias = dwarf2_name (die, cu);
27aa8d6a 11336
794684b6
SW
11337 /* Figure out where the statement is being imported to. */
11338 import_prefix = determine_prefix (die, cu);
11339
11340 /* Figure out what the scope of the imported die is and prepend it
11341 to the name of the imported die. */
de4affc9 11342 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11343
f55ee35c
JK
11344 if (imported_die->tag != DW_TAG_namespace
11345 && imported_die->tag != DW_TAG_module)
794684b6 11346 {
13387711
SW
11347 imported_declaration = imported_name;
11348 canonical_name = imported_name_prefix;
794684b6 11349 }
13387711 11350 else if (strlen (imported_name_prefix) > 0)
12aaed36 11351 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11352 imported_name_prefix,
11353 (cu->language == language_d ? "." : "::"),
11354 imported_name, (char *) NULL);
13387711
SW
11355 else
11356 canonical_name = imported_name;
794684b6 11357
32019081
JK
11358 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11359 for (child_die = die->child; child_die && child_die->tag;
11360 child_die = sibling_die (child_die))
11361 {
11362 /* DWARF-4: A Fortran use statement with a “rename list” may be
11363 represented by an imported module entry with an import attribute
11364 referring to the module and owned entries corresponding to those
11365 entities that are renamed as part of being imported. */
11366
11367 if (child_die->tag != DW_TAG_imported_declaration)
11368 {
b98664d3 11369 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11370 "- DIE at %s [in module %s]"),
11371 sect_offset_str (child_die->sect_off),
11372 objfile_name (objfile));
32019081
JK
11373 continue;
11374 }
11375
11376 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11377 if (import_attr == NULL)
11378 {
b98664d3 11379 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11380 dwarf_tag_name (child_die->tag));
11381 continue;
11382 }
11383
11384 imported_cu = cu;
11385 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11386 &imported_cu);
11387 imported_name = dwarf2_name (imported_die, imported_cu);
11388 if (imported_name == NULL)
11389 {
b98664d3 11390 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11391 "imported name - DIE at %s [in module %s]"),
11392 sect_offset_str (child_die->sect_off),
11393 objfile_name (objfile));
32019081
JK
11394 continue;
11395 }
11396
eb1e02fd 11397 excludes.push_back (imported_name);
32019081
JK
11398
11399 process_die (child_die, cu);
11400 }
11401
804d2729 11402 add_using_directive (using_directives (cu),
22cee43f
PMR
11403 import_prefix,
11404 canonical_name,
11405 import_alias,
11406 imported_declaration,
11407 excludes,
11408 0,
11409 &objfile->objfile_obstack);
27aa8d6a
SW
11410}
11411
5230b05a
WT
11412/* ICC<14 does not output the required DW_AT_declaration on incomplete
11413 types, but gives them a size of zero. Starting with version 14,
11414 ICC is compatible with GCC. */
11415
9068261f 11416static bool
5230b05a
WT
11417producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11418{
11419 if (!cu->checked_producer)
11420 check_producer (cu);
11421
11422 return cu->producer_is_icc_lt_14;
11423}
11424
eb77c9df
AB
11425/* ICC generates a DW_AT_type for C void functions. This was observed on
11426 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11427 which says that void functions should not have a DW_AT_type. */
11428
11429static bool
11430producer_is_icc (struct dwarf2_cu *cu)
11431{
11432 if (!cu->checked_producer)
11433 check_producer (cu);
11434
11435 return cu->producer_is_icc;
11436}
11437
1b80a9fa
JK
11438/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11439 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11440 this, it was first present in GCC release 4.3.0. */
11441
9068261f 11442static bool
1b80a9fa
JK
11443producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11444{
11445 if (!cu->checked_producer)
11446 check_producer (cu);
11447
11448 return cu->producer_is_gcc_lt_4_3;
11449}
11450
d721ba37
PA
11451static file_and_directory
11452find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11453{
d721ba37
PA
11454 file_and_directory res;
11455
9291a0cd
TT
11456 /* Find the filename. Do not use dwarf2_name here, since the filename
11457 is not a source language identifier. */
d721ba37
PA
11458 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11459 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11460
d721ba37
PA
11461 if (res.comp_dir == NULL
11462 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11463 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11464 {
d721ba37
PA
11465 res.comp_dir_storage = ldirname (res.name);
11466 if (!res.comp_dir_storage.empty ())
11467 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11468 }
d721ba37 11469 if (res.comp_dir != NULL)
9291a0cd
TT
11470 {
11471 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11472 directory, get rid of it. */
d721ba37 11473 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11474
d721ba37
PA
11475 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11476 res.comp_dir = cp + 1;
9291a0cd
TT
11477 }
11478
d721ba37
PA
11479 if (res.name == NULL)
11480 res.name = "<unknown>";
11481
11482 return res;
9291a0cd
TT
11483}
11484
f4dc4d17
DE
11485/* Handle DW_AT_stmt_list for a compilation unit.
11486 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11487 COMP_DIR is the compilation directory. LOWPC is passed to
11488 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11489
11490static void
11491handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11492 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11493{
518817b3
SM
11494 struct dwarf2_per_objfile *dwarf2_per_objfile
11495 = cu->per_cu->dwarf2_per_objfile;
527f3840 11496 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11497 struct attribute *attr;
527f3840
JK
11498 struct line_header line_header_local;
11499 hashval_t line_header_local_hash;
527f3840
JK
11500 void **slot;
11501 int decode_mapping;
2ab95328 11502
f4dc4d17
DE
11503 gdb_assert (! cu->per_cu->is_debug_types);
11504
2ab95328 11505 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11506 if (attr == NULL)
11507 return;
11508
9c541725 11509 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11510
11511 /* The line header hash table is only created if needed (it exists to
11512 prevent redundant reading of the line table for partial_units).
11513 If we're given a partial_unit, we'll need it. If we're given a
11514 compile_unit, then use the line header hash table if it's already
11515 created, but don't create one just yet. */
11516
11517 if (dwarf2_per_objfile->line_header_hash == NULL
11518 && die->tag == DW_TAG_partial_unit)
2ab95328 11519 {
527f3840
JK
11520 dwarf2_per_objfile->line_header_hash
11521 = htab_create_alloc_ex (127, line_header_hash_voidp,
11522 line_header_eq_voidp,
11523 free_line_header_voidp,
11524 &objfile->objfile_obstack,
11525 hashtab_obstack_allocate,
11526 dummy_obstack_deallocate);
11527 }
2ab95328 11528
9c541725 11529 line_header_local.sect_off = line_offset;
527f3840
JK
11530 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11531 line_header_local_hash = line_header_hash (&line_header_local);
11532 if (dwarf2_per_objfile->line_header_hash != NULL)
11533 {
11534 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11535 &line_header_local,
11536 line_header_local_hash, NO_INSERT);
11537
11538 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11539 is not present in *SLOT (since if there is something in *SLOT then
11540 it will be for a partial_unit). */
11541 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11542 {
527f3840 11543 gdb_assert (*slot != NULL);
9a3c8263 11544 cu->line_header = (struct line_header *) *slot;
527f3840 11545 return;
dee91e82 11546 }
2ab95328 11547 }
527f3840
JK
11548
11549 /* dwarf_decode_line_header does not yet provide sufficient information.
11550 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11551 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11552 if (lh == NULL)
527f3840 11553 return;
4c8aa72d
PA
11554
11555 cu->line_header = lh.release ();
11556 cu->line_header_die_owner = die;
527f3840
JK
11557
11558 if (dwarf2_per_objfile->line_header_hash == NULL)
11559 slot = NULL;
11560 else
11561 {
11562 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11563 &line_header_local,
11564 line_header_local_hash, INSERT);
11565 gdb_assert (slot != NULL);
11566 }
11567 if (slot != NULL && *slot == NULL)
11568 {
11569 /* This newly decoded line number information unit will be owned
11570 by line_header_hash hash table. */
11571 *slot = cu->line_header;
4c8aa72d 11572 cu->line_header_die_owner = NULL;
527f3840
JK
11573 }
11574 else
11575 {
11576 /* We cannot free any current entry in (*slot) as that struct line_header
11577 may be already used by multiple CUs. Create only temporary decoded
11578 line_header for this CU - it may happen at most once for each line
11579 number information unit. And if we're not using line_header_hash
11580 then this is what we want as well. */
11581 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11582 }
11583 decode_mapping = (die->tag != DW_TAG_partial_unit);
11584 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11585 decode_mapping);
fff8551c 11586
2ab95328
TT
11587}
11588
95554aad 11589/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11590
c906108c 11591static void
e7c27a73 11592read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11593{
518817b3
SM
11594 struct dwarf2_per_objfile *dwarf2_per_objfile
11595 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11596 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11597 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11598 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11599 CORE_ADDR highpc = ((CORE_ADDR) 0);
11600 struct attribute *attr;
c906108c 11601 struct die_info *child_die;
e142c38c 11602 CORE_ADDR baseaddr;
6e70227d 11603
380618d6 11604 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11605 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11606
fae299cd 11607 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11608
11609 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11610 from finish_block. */
2acceee2 11611 if (lowpc == ((CORE_ADDR) -1))
c906108c 11612 lowpc = highpc;
3e29f34a 11613 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11614
d721ba37 11615 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11616
f4b8a18d
KW
11617 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11618 standardised yet. As a workaround for the language detection we fall
11619 back to the DW_AT_producer string. */
11620 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11621 cu->language = language_opencl;
11622
3019eac3
DE
11623 /* Similar hack for Go. */
11624 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11625 set_cu_language (DW_LANG_Go, cu);
11626
c24bdb02 11627 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11628
11629 /* Decode line number information if present. We do this before
11630 processing child DIEs, so that the line header table is available
11631 for DW_AT_decl_file. */
d721ba37 11632 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11633
11634 /* Process all dies in compilation unit. */
11635 if (die->child != NULL)
11636 {
11637 child_die = die->child;
11638 while (child_die && child_die->tag)
11639 {
11640 process_die (child_die, cu);
11641 child_die = sibling_die (child_die);
11642 }
11643 }
11644
11645 /* Decode macro information, if present. Dwarf 2 macro information
11646 refers to information in the line number info statement program
11647 header, so we can only read it if we've read the header
11648 successfully. */
0af92d60
JK
11649 attr = dwarf2_attr (die, DW_AT_macros, cu);
11650 if (attr == NULL)
11651 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11652 if (attr && cu->line_header)
11653 {
11654 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11655 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11656
43f3e411 11657 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11658 }
11659 else
11660 {
11661 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11662 if (attr && cu->line_header)
11663 {
11664 unsigned int macro_offset = DW_UNSND (attr);
11665
43f3e411 11666 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11667 }
11668 }
3019eac3
DE
11669}
11670
c24bdb02
KS
11671void
11672dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11673{
f4dc4d17
DE
11674 struct type_unit_group *tu_group;
11675 int first_time;
3019eac3 11676 struct attribute *attr;
9c541725 11677 unsigned int i;
0186c6a7 11678 struct signatured_type *sig_type;
3019eac3 11679
f4dc4d17 11680 gdb_assert (per_cu->is_debug_types);
0186c6a7 11681 sig_type = (struct signatured_type *) per_cu;
3019eac3 11682
c24bdb02 11683 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11684
f4dc4d17 11685 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11686 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11687 if (sig_type->type_unit_group == NULL)
c24bdb02 11688 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11689 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11690
11691 /* If we've already processed this stmt_list there's no real need to
11692 do it again, we could fake it and just recreate the part we need
11693 (file name,index -> symtab mapping). If data shows this optimization
11694 is useful we can do it then. */
43f3e411 11695 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11696
11697 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11698 debug info. */
fff8551c 11699 line_header_up lh;
f4dc4d17 11700 if (attr != NULL)
3019eac3 11701 {
9c541725 11702 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11703 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11704 }
11705 if (lh == NULL)
11706 {
11707 if (first_time)
c24bdb02 11708 start_symtab ("", NULL, 0);
f4dc4d17
DE
11709 else
11710 {
11711 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11712 gdb_assert (m_builder == nullptr);
804d2729 11713 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11714 m_builder.reset (new struct buildsym_compunit
11715 (COMPUNIT_OBJFILE (cust), "",
11716 COMPUNIT_DIRNAME (cust),
11717 compunit_language (cust),
11718 0, cust));
f4dc4d17 11719 }
f4dc4d17 11720 return;
3019eac3
DE
11721 }
11722
c24bdb02
KS
11723 line_header = lh.release ();
11724 line_header_die_owner = die;
3019eac3 11725
f4dc4d17
DE
11726 if (first_time)
11727 {
c24bdb02 11728 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11729
1fd60fc0
DE
11730 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11731 still initializing it, and our caller (a few levels up)
11732 process_full_type_unit still needs to know if this is the first
11733 time. */
11734
7ba99d21 11735 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11736 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11737 line_header->file_names_size ());
3019eac3 11738
7ba99d21
AT
11739 auto &file_names = line_header->file_names ();
11740 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11741 {
7ba99d21 11742 file_entry &fe = file_names[i];
c24bdb02
KS
11743 dwarf2_start_subfile (this, fe.name,
11744 fe.include_dir (line_header));
11745 buildsym_compunit *b = get_builder ();
11746 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11747 {
4c8aa72d
PA
11748 /* NOTE: start_subfile will recognize when it's been
11749 passed a file it has already seen. So we can't
11750 assume there's a simple mapping from
11751 cu->line_header->file_names to subfiles, plus
11752 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11753 b->get_current_subfile ()->symtab
11754 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11755 }
11756
c24bdb02 11757 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11758 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11759 }
11760 }
11761 else
3019eac3 11762 {
c24bdb02 11763 gdb_assert (m_builder == nullptr);
804d2729 11764 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11765 m_builder.reset (new struct buildsym_compunit
11766 (COMPUNIT_OBJFILE (cust), "",
11767 COMPUNIT_DIRNAME (cust),
11768 compunit_language (cust),
11769 0, cust));
f4dc4d17 11770
7ba99d21
AT
11771 auto &file_names = line_header->file_names ();
11772 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11773 {
7ba99d21 11774 file_entry &fe = file_names[i];
4c8aa72d 11775 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11776 }
3019eac3
DE
11777 }
11778
f4dc4d17
DE
11779 /* The main symtab is allocated last. Type units don't have DW_AT_name
11780 so they don't have a "real" (so to speak) symtab anyway.
11781 There is later code that will assign the main symtab to all symbols
11782 that don't have one. We need to handle the case of a symbol with a
11783 missing symtab (DW_AT_decl_file) anyway. */
11784}
3019eac3 11785
f4dc4d17
DE
11786/* Process DW_TAG_type_unit.
11787 For TUs we want to skip the first top level sibling if it's not the
11788 actual type being defined by this TU. In this case the first top
11789 level sibling is there to provide context only. */
3019eac3 11790
f4dc4d17
DE
11791static void
11792read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11793{
11794 struct die_info *child_die;
3019eac3 11795
f4dc4d17
DE
11796 prepare_one_comp_unit (cu, die, language_minimal);
11797
11798 /* Initialize (or reinitialize) the machinery for building symtabs.
11799 We do this before processing child DIEs, so that the line header table
11800 is available for DW_AT_decl_file. */
c24bdb02 11801 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11802
11803 if (die->child != NULL)
11804 {
11805 child_die = die->child;
11806 while (child_die && child_die->tag)
11807 {
11808 process_die (child_die, cu);
11809 child_die = sibling_die (child_die);
11810 }
11811 }
3019eac3
DE
11812}
11813\f
80626a55
DE
11814/* DWO/DWP files.
11815
11816 http://gcc.gnu.org/wiki/DebugFission
11817 http://gcc.gnu.org/wiki/DebugFissionDWP
11818
11819 To simplify handling of both DWO files ("object" files with the DWARF info)
11820 and DWP files (a file with the DWOs packaged up into one file), we treat
11821 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11822
11823static hashval_t
11824hash_dwo_file (const void *item)
11825{
9a3c8263 11826 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11827 hashval_t hash;
3019eac3 11828
a2ce51a0
DE
11829 hash = htab_hash_string (dwo_file->dwo_name);
11830 if (dwo_file->comp_dir != NULL)
11831 hash += htab_hash_string (dwo_file->comp_dir);
11832 return hash;
3019eac3
DE
11833}
11834
11835static int
11836eq_dwo_file (const void *item_lhs, const void *item_rhs)
11837{
9a3c8263
SM
11838 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11839 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11840
a2ce51a0
DE
11841 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11842 return 0;
11843 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11844 return lhs->comp_dir == rhs->comp_dir;
11845 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11846}
11847
11848/* Allocate a hash table for DWO files. */
11849
51ac9db5 11850static htab_up
ed2dc618 11851allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11852{
51ac9db5
SM
11853 auto delete_dwo_file = [] (void *item)
11854 {
11855 struct dwo_file *dwo_file = (struct dwo_file *) item;
11856
11857 delete dwo_file;
11858 };
11859
11860 return htab_up (htab_create_alloc_ex (41,
11861 hash_dwo_file,
11862 eq_dwo_file,
11863 delete_dwo_file,
11864 &objfile->objfile_obstack,
11865 hashtab_obstack_allocate,
11866 dummy_obstack_deallocate));
3019eac3
DE
11867}
11868
80626a55
DE
11869/* Lookup DWO file DWO_NAME. */
11870
11871static void **
ed2dc618
SM
11872lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11873 const char *dwo_name,
11874 const char *comp_dir)
80626a55
DE
11875{
11876 struct dwo_file find_entry;
11877 void **slot;
11878
11879 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11880 dwarf2_per_objfile->dwo_files
11881 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11882
0ac5b59e
DE
11883 find_entry.dwo_name = dwo_name;
11884 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11885 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11886 INSERT);
80626a55
DE
11887
11888 return slot;
11889}
11890
3019eac3
DE
11891static hashval_t
11892hash_dwo_unit (const void *item)
11893{
9a3c8263 11894 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11895
11896 /* This drops the top 32 bits of the id, but is ok for a hash. */
11897 return dwo_unit->signature;
11898}
11899
11900static int
11901eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11902{
9a3c8263
SM
11903 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11904 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11905
11906 /* The signature is assumed to be unique within the DWO file.
11907 So while object file CU dwo_id's always have the value zero,
11908 that's OK, assuming each object file DWO file has only one CU,
11909 and that's the rule for now. */
11910 return lhs->signature == rhs->signature;
11911}
11912
11913/* Allocate a hash table for DWO CUs,TUs.
11914 There is one of these tables for each of CUs,TUs for each DWO file. */
11915
11916static htab_t
11917allocate_dwo_unit_table (struct objfile *objfile)
11918{
11919 /* Start out with a pretty small number.
11920 Generally DWO files contain only one CU and maybe some TUs. */
11921 return htab_create_alloc_ex (3,
11922 hash_dwo_unit,
11923 eq_dwo_unit,
11924 NULL,
11925 &objfile->objfile_obstack,
11926 hashtab_obstack_allocate,
11927 dummy_obstack_deallocate);
11928}
11929
80626a55 11930/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11931
19c3d4c9 11932struct create_dwo_cu_data
3019eac3
DE
11933{
11934 struct dwo_file *dwo_file;
19c3d4c9 11935 struct dwo_unit dwo_unit;
3019eac3
DE
11936};
11937
19c3d4c9 11938/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11939
11940static void
19c3d4c9
DE
11941create_dwo_cu_reader (const struct die_reader_specs *reader,
11942 const gdb_byte *info_ptr,
11943 struct die_info *comp_unit_die,
11944 int has_children,
11945 void *datap)
3019eac3
DE
11946{
11947 struct dwarf2_cu *cu = reader->cu;
9c541725 11948 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11949 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11950 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11951 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11952 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11953
a084a2a6
AT
11954 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11955 if (!signature.has_value ())
3019eac3 11956 {
b98664d3 11957 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11958 " its dwo_id [in module %s]"),
9d8780f0 11959 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11960 return;
11961 }
11962
3019eac3 11963 dwo_unit->dwo_file = dwo_file;
a084a2a6 11964 dwo_unit->signature = *signature;
8a0459fd 11965 dwo_unit->section = section;
9c541725 11966 dwo_unit->sect_off = sect_off;
3019eac3
DE
11967 dwo_unit->length = cu->per_cu->length;
11968
b4f54984 11969 if (dwarf_read_debug)
9d8780f0
SM
11970 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11971 sect_offset_str (sect_off),
9c541725 11972 hex_string (dwo_unit->signature));
3019eac3
DE
11973}
11974
33c5cd75 11975/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11976 Note: This function processes DWO files only, not DWP files. */
3019eac3 11977
33c5cd75 11978static void
ed2dc618
SM
11979create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11980 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11981 htab_t &cus_htab)
3019eac3
DE
11982{
11983 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11984 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11985
33c5cd75
DB
11986 dwarf2_read_section (objfile, &section);
11987 info_ptr = section.buffer;
3019eac3
DE
11988
11989 if (info_ptr == NULL)
33c5cd75 11990 return;
3019eac3 11991
b4f54984 11992 if (dwarf_read_debug)
19c3d4c9
DE
11993 {
11994 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11995 get_section_name (&section),
11996 get_section_file_name (&section));
19c3d4c9 11997 }
3019eac3 11998
33c5cd75 11999 end_ptr = info_ptr + section.size;
3019eac3
DE
12000 while (info_ptr < end_ptr)
12001 {
12002 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12003 struct create_dwo_cu_data create_dwo_cu_data;
12004 struct dwo_unit *dwo_unit;
12005 void **slot;
12006 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12007
19c3d4c9
DE
12008 memset (&create_dwo_cu_data.dwo_unit, 0,
12009 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12010 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12011 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12012 per_cu.is_debug_types = 0;
33c5cd75
DB
12013 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12014 per_cu.section = &section;
c5ed0576 12015 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12016
12017 init_cutu_and_read_dies_no_follow (
12018 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12019 info_ptr += per_cu.length;
12020
12021 // If the unit could not be parsed, skip it.
12022 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12023 continue;
3019eac3 12024
33c5cd75
DB
12025 if (cus_htab == NULL)
12026 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12027
33c5cd75
DB
12028 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12029 *dwo_unit = create_dwo_cu_data.dwo_unit;
12030 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12031 gdb_assert (slot != NULL);
12032 if (*slot != NULL)
19c3d4c9 12033 {
33c5cd75
DB
12034 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12035 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12036
b98664d3 12037 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12038 " the entry at offset %s, signature %s"),
12039 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12040 hex_string (dwo_unit->signature));
19c3d4c9 12041 }
33c5cd75 12042 *slot = (void *)dwo_unit;
3019eac3 12043 }
3019eac3
DE
12044}
12045
80626a55
DE
12046/* DWP file .debug_{cu,tu}_index section format:
12047 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12048
d2415c6c
DE
12049 DWP Version 1:
12050
80626a55
DE
12051 Both index sections have the same format, and serve to map a 64-bit
12052 signature to a set of section numbers. Each section begins with a header,
12053 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12054 indexes, and a pool of 32-bit section numbers. The index sections will be
12055 aligned at 8-byte boundaries in the file.
12056
d2415c6c
DE
12057 The index section header consists of:
12058
12059 V, 32 bit version number
12060 -, 32 bits unused
12061 N, 32 bit number of compilation units or type units in the index
12062 M, 32 bit number of slots in the hash table
80626a55 12063
d2415c6c 12064 Numbers are recorded using the byte order of the application binary.
80626a55 12065
d2415c6c
DE
12066 The hash table begins at offset 16 in the section, and consists of an array
12067 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12068 order of the application binary). Unused slots in the hash table are 0.
12069 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12070
d2415c6c
DE
12071 The parallel table begins immediately after the hash table
12072 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12073 array of 32-bit indexes (using the byte order of the application binary),
12074 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12075 table contains a 32-bit index into the pool of section numbers. For unused
12076 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12077
73869dc2
DE
12078 The pool of section numbers begins immediately following the hash table
12079 (at offset 16 + 12 * M from the beginning of the section). The pool of
12080 section numbers consists of an array of 32-bit words (using the byte order
12081 of the application binary). Each item in the array is indexed starting
12082 from 0. The hash table entry provides the index of the first section
12083 number in the set. Additional section numbers in the set follow, and the
12084 set is terminated by a 0 entry (section number 0 is not used in ELF).
12085
12086 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12087 section must be the first entry in the set, and the .debug_abbrev.dwo must
12088 be the second entry. Other members of the set may follow in any order.
12089
12090 ---
12091
12092 DWP Version 2:
12093
12094 DWP Version 2 combines all the .debug_info, etc. sections into one,
12095 and the entries in the index tables are now offsets into these sections.
12096 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12097 section.
12098
12099 Index Section Contents:
12100 Header
12101 Hash Table of Signatures dwp_hash_table.hash_table
12102 Parallel Table of Indices dwp_hash_table.unit_table
12103 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12104 Table of Section Sizes dwp_hash_table.v2.sizes
12105
12106 The index section header consists of:
12107
12108 V, 32 bit version number
12109 L, 32 bit number of columns in the table of section offsets
12110 N, 32 bit number of compilation units or type units in the index
12111 M, 32 bit number of slots in the hash table
12112
12113 Numbers are recorded using the byte order of the application binary.
12114
12115 The hash table has the same format as version 1.
12116 The parallel table of indices has the same format as version 1,
12117 except that the entries are origin-1 indices into the table of sections
12118 offsets and the table of section sizes.
12119
12120 The table of offsets begins immediately following the parallel table
12121 (at offset 16 + 12 * M from the beginning of the section). The table is
12122 a two-dimensional array of 32-bit words (using the byte order of the
12123 application binary), with L columns and N+1 rows, in row-major order.
12124 Each row in the array is indexed starting from 0. The first row provides
12125 a key to the remaining rows: each column in this row provides an identifier
12126 for a debug section, and the offsets in the same column of subsequent rows
12127 refer to that section. The section identifiers are:
12128
12129 DW_SECT_INFO 1 .debug_info.dwo
12130 DW_SECT_TYPES 2 .debug_types.dwo
12131 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12132 DW_SECT_LINE 4 .debug_line.dwo
12133 DW_SECT_LOC 5 .debug_loc.dwo
12134 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12135 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12136 DW_SECT_MACRO 8 .debug_macro.dwo
12137
12138 The offsets provided by the CU and TU index sections are the base offsets
12139 for the contributions made by each CU or TU to the corresponding section
12140 in the package file. Each CU and TU header contains an abbrev_offset
12141 field, used to find the abbreviations table for that CU or TU within the
12142 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12143 be interpreted as relative to the base offset given in the index section.
12144 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12145 should be interpreted as relative to the base offset for .debug_line.dwo,
12146 and offsets into other debug sections obtained from DWARF attributes should
12147 also be interpreted as relative to the corresponding base offset.
12148
12149 The table of sizes begins immediately following the table of offsets.
12150 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12151 with L columns and N rows, in row-major order. Each row in the array is
12152 indexed starting from 1 (row 0 is shared by the two tables).
12153
12154 ---
12155
12156 Hash table lookup is handled the same in version 1 and 2:
12157
12158 We assume that N and M will not exceed 2^32 - 1.
12159 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12160
d2415c6c
DE
12161 Given a 64-bit compilation unit signature or a type signature S, an entry
12162 in the hash table is located as follows:
80626a55 12163
d2415c6c
DE
12164 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12165 the low-order k bits all set to 1.
80626a55 12166
d2415c6c 12167 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12168
d2415c6c
DE
12169 3) If the hash table entry at index H matches the signature, use that
12170 entry. If the hash table entry at index H is unused (all zeroes),
12171 terminate the search: the signature is not present in the table.
80626a55 12172
d2415c6c 12173 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12174
d2415c6c 12175 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12176 to stop at an unused slot or find the match. */
80626a55
DE
12177
12178/* Create a hash table to map DWO IDs to their CU/TU entry in
12179 .debug_{info,types}.dwo in DWP_FILE.
12180 Returns NULL if there isn't one.
12181 Note: This function processes DWP files only, not DWO files. */
12182
12183static struct dwp_hash_table *
ed2dc618
SM
12184create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12185 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12186{
12187 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12188 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12189 const gdb_byte *index_ptr, *index_end;
80626a55 12190 struct dwarf2_section_info *index;
73869dc2 12191 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12192 struct dwp_hash_table *htab;
12193
12194 if (is_debug_types)
12195 index = &dwp_file->sections.tu_index;
12196 else
12197 index = &dwp_file->sections.cu_index;
12198
12199 if (dwarf2_section_empty_p (index))
12200 return NULL;
12201 dwarf2_read_section (objfile, index);
12202
12203 index_ptr = index->buffer;
12204 index_end = index_ptr + index->size;
12205
12206 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12207 index_ptr += 4;
12208 if (version == 2)
12209 nr_columns = read_4_bytes (dbfd, index_ptr);
12210 else
12211 nr_columns = 0;
12212 index_ptr += 4;
80626a55
DE
12213 nr_units = read_4_bytes (dbfd, index_ptr);
12214 index_ptr += 4;
12215 nr_slots = read_4_bytes (dbfd, index_ptr);
12216 index_ptr += 4;
12217
73869dc2 12218 if (version != 1 && version != 2)
80626a55 12219 {
21aa081e 12220 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12221 " [in module %s]"),
21aa081e 12222 pulongest (version), dwp_file->name);
80626a55
DE
12223 }
12224 if (nr_slots != (nr_slots & -nr_slots))
12225 {
21aa081e 12226 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12227 " is not power of 2 [in module %s]"),
21aa081e 12228 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12229 }
12230
12231 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12232 htab->version = version;
12233 htab->nr_columns = nr_columns;
80626a55
DE
12234 htab->nr_units = nr_units;
12235 htab->nr_slots = nr_slots;
12236 htab->hash_table = index_ptr;
12237 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12238
12239 /* Exit early if the table is empty. */
12240 if (nr_slots == 0 || nr_units == 0
12241 || (version == 2 && nr_columns == 0))
12242 {
12243 /* All must be zero. */
12244 if (nr_slots != 0 || nr_units != 0
12245 || (version == 2 && nr_columns != 0))
12246 {
b98664d3 12247 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12248 " all zero [in modules %s]"),
12249 dwp_file->name);
12250 }
12251 return htab;
12252 }
12253
12254 if (version == 1)
12255 {
12256 htab->section_pool.v1.indices =
12257 htab->unit_table + sizeof (uint32_t) * nr_slots;
12258 /* It's harder to decide whether the section is too small in v1.
12259 V1 is deprecated anyway so we punt. */
12260 }
12261 else
12262 {
12263 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12264 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12265 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12266 /* Reverse map for error checking. */
12267 int ids_seen[DW_SECT_MAX + 1];
12268 int i;
12269
12270 if (nr_columns < 2)
12271 {
12272 error (_("Dwarf Error: bad DWP hash table, too few columns"
12273 " in section table [in module %s]"),
12274 dwp_file->name);
12275 }
12276 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12277 {
12278 error (_("Dwarf Error: bad DWP hash table, too many columns"
12279 " in section table [in module %s]"),
12280 dwp_file->name);
12281 }
04fd5eed
GB
12282 memset (ids, 255, sizeof_ids);
12283 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12284 for (i = 0; i < nr_columns; ++i)
12285 {
12286 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12287
12288 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12289 {
12290 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12291 " in section table [in module %s]"),
12292 id, dwp_file->name);
12293 }
12294 if (ids_seen[id] != -1)
12295 {
12296 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12297 " id %d in section table [in module %s]"),
12298 id, dwp_file->name);
12299 }
12300 ids_seen[id] = i;
12301 ids[i] = id;
12302 }
12303 /* Must have exactly one info or types section. */
12304 if (((ids_seen[DW_SECT_INFO] != -1)
12305 + (ids_seen[DW_SECT_TYPES] != -1))
12306 != 1)
12307 {
12308 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12309 " DWO info/types section [in module %s]"),
12310 dwp_file->name);
12311 }
12312 /* Must have an abbrev section. */
12313 if (ids_seen[DW_SECT_ABBREV] == -1)
12314 {
12315 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12316 " section [in module %s]"),
12317 dwp_file->name);
12318 }
12319 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12320 htab->section_pool.v2.sizes =
12321 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12322 * nr_units * nr_columns);
12323 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12324 * nr_units * nr_columns))
12325 > index_end)
12326 {
12327 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12328 " [in module %s]"),
12329 dwp_file->name);
12330 }
12331 }
80626a55
DE
12332
12333 return htab;
12334}
12335
12336/* Update SECTIONS with the data from SECTP.
12337
12338 This function is like the other "locate" section routines that are
12339 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12340 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12341
12342 The result is non-zero for success, or zero if an error was found. */
12343
12344static int
73869dc2
DE
12345locate_v1_virtual_dwo_sections (asection *sectp,
12346 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12347{
12348 const struct dwop_section_names *names = &dwop_section_names;
12349
12350 if (section_is_p (sectp->name, &names->abbrev_dwo))
12351 {
12352 /* There can be only one. */
049412e3 12353 if (sections->abbrev.s.section != NULL)
80626a55 12354 return 0;
049412e3 12355 sections->abbrev.s.section = sectp;
fd361982 12356 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12357 }
12358 else if (section_is_p (sectp->name, &names->info_dwo)
12359 || section_is_p (sectp->name, &names->types_dwo))
12360 {
12361 /* There can be only one. */
049412e3 12362 if (sections->info_or_types.s.section != NULL)
80626a55 12363 return 0;
049412e3 12364 sections->info_or_types.s.section = sectp;
fd361982 12365 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12366 }
12367 else if (section_is_p (sectp->name, &names->line_dwo))
12368 {
12369 /* There can be only one. */
049412e3 12370 if (sections->line.s.section != NULL)
80626a55 12371 return 0;
049412e3 12372 sections->line.s.section = sectp;
fd361982 12373 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12374 }
12375 else if (section_is_p (sectp->name, &names->loc_dwo))
12376 {
12377 /* There can be only one. */
049412e3 12378 if (sections->loc.s.section != NULL)
80626a55 12379 return 0;
049412e3 12380 sections->loc.s.section = sectp;
fd361982 12381 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12382 }
12383 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12384 {
12385 /* There can be only one. */
049412e3 12386 if (sections->macinfo.s.section != NULL)
80626a55 12387 return 0;
049412e3 12388 sections->macinfo.s.section = sectp;
fd361982 12389 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12390 }
12391 else if (section_is_p (sectp->name, &names->macro_dwo))
12392 {
12393 /* There can be only one. */
049412e3 12394 if (sections->macro.s.section != NULL)
80626a55 12395 return 0;
049412e3 12396 sections->macro.s.section = sectp;
fd361982 12397 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12398 }
12399 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12400 {
12401 /* There can be only one. */
049412e3 12402 if (sections->str_offsets.s.section != NULL)
80626a55 12403 return 0;
049412e3 12404 sections->str_offsets.s.section = sectp;
fd361982 12405 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12406 }
12407 else
12408 {
12409 /* No other kind of section is valid. */
12410 return 0;
12411 }
12412
12413 return 1;
12414}
12415
73869dc2
DE
12416/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12417 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12418 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12419 This is for DWP version 1 files. */
80626a55
DE
12420
12421static struct dwo_unit *
ed2dc618
SM
12422create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12423 struct dwp_file *dwp_file,
73869dc2
DE
12424 uint32_t unit_index,
12425 const char *comp_dir,
12426 ULONGEST signature, int is_debug_types)
80626a55
DE
12427{
12428 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12429 const struct dwp_hash_table *dwp_htab =
12430 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12431 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12432 const char *kind = is_debug_types ? "TU" : "CU";
12433 struct dwo_file *dwo_file;
12434 struct dwo_unit *dwo_unit;
73869dc2 12435 struct virtual_v1_dwo_sections sections;
80626a55 12436 void **dwo_file_slot;
80626a55
DE
12437 int i;
12438
73869dc2
DE
12439 gdb_assert (dwp_file->version == 1);
12440
b4f54984 12441 if (dwarf_read_debug)
80626a55 12442 {
73869dc2 12443 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12444 kind,
73869dc2 12445 pulongest (unit_index), hex_string (signature),
80626a55
DE
12446 dwp_file->name);
12447 }
12448
19ac8c2e 12449 /* Fetch the sections of this DWO unit.
80626a55
DE
12450 Put a limit on the number of sections we look for so that bad data
12451 doesn't cause us to loop forever. */
12452
73869dc2 12453#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12454 (1 /* .debug_info or .debug_types */ \
12455 + 1 /* .debug_abbrev */ \
12456 + 1 /* .debug_line */ \
12457 + 1 /* .debug_loc */ \
12458 + 1 /* .debug_str_offsets */ \
19ac8c2e 12459 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12460 + 1 /* trailing zero */)
12461
12462 memset (&sections, 0, sizeof (sections));
80626a55 12463
73869dc2 12464 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12465 {
12466 asection *sectp;
12467 uint32_t section_nr =
12468 read_4_bytes (dbfd,
73869dc2
DE
12469 dwp_htab->section_pool.v1.indices
12470 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12471
12472 if (section_nr == 0)
12473 break;
12474 if (section_nr >= dwp_file->num_sections)
12475 {
12476 error (_("Dwarf Error: bad DWP hash table, section number too large"
12477 " [in module %s]"),
12478 dwp_file->name);
12479 }
12480
12481 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12482 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12483 {
12484 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12485 " [in module %s]"),
12486 dwp_file->name);
12487 }
12488 }
12489
12490 if (i < 2
a32a8923
DE
12491 || dwarf2_section_empty_p (&sections.info_or_types)
12492 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12493 {
12494 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12495 " [in module %s]"),
12496 dwp_file->name);
12497 }
73869dc2 12498 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12499 {
12500 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12501 " [in module %s]"),
12502 dwp_file->name);
12503 }
12504
12505 /* It's easier for the rest of the code if we fake a struct dwo_file and
12506 have dwo_unit "live" in that. At least for now.
12507
12508 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12509 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12510 file, we can combine them back into a virtual DWO file to save space
12511 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12512 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12513
791afaa2
TT
12514 std::string virtual_dwo_name =
12515 string_printf ("virtual-dwo/%d-%d-%d-%d",
12516 get_section_id (&sections.abbrev),
12517 get_section_id (&sections.line),
12518 get_section_id (&sections.loc),
12519 get_section_id (&sections.str_offsets));
80626a55 12520 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12521 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12522 virtual_dwo_name.c_str (),
12523 comp_dir);
80626a55
DE
12524 /* Create one if necessary. */
12525 if (*dwo_file_slot == NULL)
12526 {
b4f54984 12527 if (dwarf_read_debug)
80626a55
DE
12528 {
12529 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12530 virtual_dwo_name.c_str ());
80626a55 12531 }
51ac9db5 12532 dwo_file = new struct dwo_file;
efba19b0
TT
12533 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12534 virtual_dwo_name);
0ac5b59e 12535 dwo_file->comp_dir = comp_dir;
80626a55
DE
12536 dwo_file->sections.abbrev = sections.abbrev;
12537 dwo_file->sections.line = sections.line;
12538 dwo_file->sections.loc = sections.loc;
12539 dwo_file->sections.macinfo = sections.macinfo;
12540 dwo_file->sections.macro = sections.macro;
12541 dwo_file->sections.str_offsets = sections.str_offsets;
12542 /* The "str" section is global to the entire DWP file. */
12543 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12544 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12545 there's no need to record it in dwo_file.
12546 Also, we can't simply record type sections in dwo_file because
12547 we record a pointer into the vector in dwo_unit. As we collect more
12548 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12549 for it, invalidating all copies of pointers into the previous
12550 contents. */
80626a55
DE
12551 *dwo_file_slot = dwo_file;
12552 }
12553 else
12554 {
b4f54984 12555 if (dwarf_read_debug)
80626a55
DE
12556 {
12557 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12558 virtual_dwo_name.c_str ());
80626a55 12559 }
9a3c8263 12560 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12561 }
80626a55
DE
12562
12563 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12564 dwo_unit->dwo_file = dwo_file;
12565 dwo_unit->signature = signature;
8d749320
SM
12566 dwo_unit->section =
12567 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12568 *dwo_unit->section = sections.info_or_types;
57d63ce2 12569 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12570
12571 return dwo_unit;
12572}
12573
73869dc2
DE
12574/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12575 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12576 piece within that section used by a TU/CU, return a virtual section
12577 of just that piece. */
12578
12579static struct dwarf2_section_info
ed2dc618
SM
12580create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12581 struct dwarf2_section_info *section,
73869dc2
DE
12582 bfd_size_type offset, bfd_size_type size)
12583{
12584 struct dwarf2_section_info result;
12585 asection *sectp;
12586
12587 gdb_assert (section != NULL);
12588 gdb_assert (!section->is_virtual);
12589
12590 memset (&result, 0, sizeof (result));
12591 result.s.containing_section = section;
dc4ccb6f 12592 result.is_virtual = true;
73869dc2
DE
12593
12594 if (size == 0)
12595 return result;
12596
12597 sectp = get_section_bfd_section (section);
12598
12599 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12600 bounds of the real section. This is a pretty-rare event, so just
12601 flag an error (easier) instead of a warning and trying to cope. */
12602 if (sectp == NULL
fd361982 12603 || offset + size > bfd_section_size (sectp))
73869dc2 12604 {
73869dc2
DE
12605 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12606 " in section %s [in module %s]"),
fd361982 12607 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12608 objfile_name (dwarf2_per_objfile->objfile));
12609 }
12610
12611 result.virtual_offset = offset;
12612 result.size = size;
12613 return result;
12614}
12615
12616/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12617 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12618 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12619 This is for DWP version 2 files. */
12620
12621static struct dwo_unit *
ed2dc618
SM
12622create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12623 struct dwp_file *dwp_file,
73869dc2
DE
12624 uint32_t unit_index,
12625 const char *comp_dir,
12626 ULONGEST signature, int is_debug_types)
12627{
12628 struct objfile *objfile = dwarf2_per_objfile->objfile;
12629 const struct dwp_hash_table *dwp_htab =
12630 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12631 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12632 const char *kind = is_debug_types ? "TU" : "CU";
12633 struct dwo_file *dwo_file;
12634 struct dwo_unit *dwo_unit;
12635 struct virtual_v2_dwo_sections sections;
12636 void **dwo_file_slot;
73869dc2
DE
12637 int i;
12638
12639 gdb_assert (dwp_file->version == 2);
12640
b4f54984 12641 if (dwarf_read_debug)
73869dc2
DE
12642 {
12643 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12644 kind,
12645 pulongest (unit_index), hex_string (signature),
12646 dwp_file->name);
12647 }
12648
12649 /* Fetch the section offsets of this DWO unit. */
12650
12651 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12652
12653 for (i = 0; i < dwp_htab->nr_columns; ++i)
12654 {
12655 uint32_t offset = read_4_bytes (dbfd,
12656 dwp_htab->section_pool.v2.offsets
12657 + (((unit_index - 1) * dwp_htab->nr_columns
12658 + i)
12659 * sizeof (uint32_t)));
12660 uint32_t size = read_4_bytes (dbfd,
12661 dwp_htab->section_pool.v2.sizes
12662 + (((unit_index - 1) * dwp_htab->nr_columns
12663 + i)
12664 * sizeof (uint32_t)));
12665
12666 switch (dwp_htab->section_pool.v2.section_ids[i])
12667 {
12668 case DW_SECT_INFO:
12669 case DW_SECT_TYPES:
12670 sections.info_or_types_offset = offset;
12671 sections.info_or_types_size = size;
12672 break;
12673 case DW_SECT_ABBREV:
12674 sections.abbrev_offset = offset;
12675 sections.abbrev_size = size;
12676 break;
12677 case DW_SECT_LINE:
12678 sections.line_offset = offset;
12679 sections.line_size = size;
12680 break;
12681 case DW_SECT_LOC:
12682 sections.loc_offset = offset;
12683 sections.loc_size = size;
12684 break;
12685 case DW_SECT_STR_OFFSETS:
12686 sections.str_offsets_offset = offset;
12687 sections.str_offsets_size = size;
12688 break;
12689 case DW_SECT_MACINFO:
12690 sections.macinfo_offset = offset;
12691 sections.macinfo_size = size;
12692 break;
12693 case DW_SECT_MACRO:
12694 sections.macro_offset = offset;
12695 sections.macro_size = size;
12696 break;
12697 }
12698 }
12699
12700 /* It's easier for the rest of the code if we fake a struct dwo_file and
12701 have dwo_unit "live" in that. At least for now.
12702
12703 The DWP file can be made up of a random collection of CUs and TUs.
12704 However, for each CU + set of TUs that came from the same original DWO
12705 file, we can combine them back into a virtual DWO file to save space
12706 (fewer struct dwo_file objects to allocate). Remember that for really
12707 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12708
791afaa2
TT
12709 std::string virtual_dwo_name =
12710 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12711 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12712 (long) (sections.line_size ? sections.line_offset : 0),
12713 (long) (sections.loc_size ? sections.loc_offset : 0),
12714 (long) (sections.str_offsets_size
12715 ? sections.str_offsets_offset : 0));
73869dc2 12716 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12717 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12718 virtual_dwo_name.c_str (),
12719 comp_dir);
73869dc2
DE
12720 /* Create one if necessary. */
12721 if (*dwo_file_slot == NULL)
12722 {
b4f54984 12723 if (dwarf_read_debug)
73869dc2
DE
12724 {
12725 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12726 virtual_dwo_name.c_str ());
73869dc2 12727 }
51ac9db5 12728 dwo_file = new struct dwo_file;
efba19b0
TT
12729 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12730 virtual_dwo_name);
73869dc2
DE
12731 dwo_file->comp_dir = comp_dir;
12732 dwo_file->sections.abbrev =
ed2dc618 12733 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12734 sections.abbrev_offset, sections.abbrev_size);
12735 dwo_file->sections.line =
ed2dc618 12736 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12737 sections.line_offset, sections.line_size);
12738 dwo_file->sections.loc =
ed2dc618 12739 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12740 sections.loc_offset, sections.loc_size);
12741 dwo_file->sections.macinfo =
ed2dc618 12742 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12743 sections.macinfo_offset, sections.macinfo_size);
12744 dwo_file->sections.macro =
ed2dc618 12745 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12746 sections.macro_offset, sections.macro_size);
12747 dwo_file->sections.str_offsets =
ed2dc618
SM
12748 create_dwp_v2_section (dwarf2_per_objfile,
12749 &dwp_file->sections.str_offsets,
73869dc2
DE
12750 sections.str_offsets_offset,
12751 sections.str_offsets_size);
12752 /* The "str" section is global to the entire DWP file. */
12753 dwo_file->sections.str = dwp_file->sections.str;
12754 /* The info or types section is assigned below to dwo_unit,
12755 there's no need to record it in dwo_file.
12756 Also, we can't simply record type sections in dwo_file because
12757 we record a pointer into the vector in dwo_unit. As we collect more
12758 types we'll grow the vector and eventually have to reallocate space
12759 for it, invalidating all copies of pointers into the previous
12760 contents. */
12761 *dwo_file_slot = dwo_file;
12762 }
12763 else
12764 {
b4f54984 12765 if (dwarf_read_debug)
73869dc2
DE
12766 {
12767 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12768 virtual_dwo_name.c_str ());
73869dc2 12769 }
9a3c8263 12770 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12771 }
73869dc2
DE
12772
12773 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12774 dwo_unit->dwo_file = dwo_file;
12775 dwo_unit->signature = signature;
8d749320
SM
12776 dwo_unit->section =
12777 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12778 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12779 is_debug_types
73869dc2
DE
12780 ? &dwp_file->sections.types
12781 : &dwp_file->sections.info,
12782 sections.info_or_types_offset,
12783 sections.info_or_types_size);
12784 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12785
12786 return dwo_unit;
12787}
12788
57d63ce2
DE
12789/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12790 Returns NULL if the signature isn't found. */
80626a55
DE
12791
12792static struct dwo_unit *
ed2dc618
SM
12793lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12794 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12795 ULONGEST signature, int is_debug_types)
80626a55 12796{
57d63ce2
DE
12797 const struct dwp_hash_table *dwp_htab =
12798 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12799 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12800 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12801 uint32_t hash = signature & mask;
12802 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12803 unsigned int i;
12804 void **slot;
870f88f7 12805 struct dwo_unit find_dwo_cu;
80626a55
DE
12806
12807 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12808 find_dwo_cu.signature = signature;
19ac8c2e
DE
12809 slot = htab_find_slot (is_debug_types
12810 ? dwp_file->loaded_tus
12811 : dwp_file->loaded_cus,
12812 &find_dwo_cu, INSERT);
80626a55
DE
12813
12814 if (*slot != NULL)
9a3c8263 12815 return (struct dwo_unit *) *slot;
80626a55
DE
12816
12817 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12818 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12819 {
12820 ULONGEST signature_in_table;
12821
12822 signature_in_table =
57d63ce2 12823 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12824 if (signature_in_table == signature)
12825 {
57d63ce2
DE
12826 uint32_t unit_index =
12827 read_4_bytes (dbfd,
12828 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12829
73869dc2
DE
12830 if (dwp_file->version == 1)
12831 {
ed2dc618
SM
12832 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12833 dwp_file, unit_index,
73869dc2
DE
12834 comp_dir, signature,
12835 is_debug_types);
12836 }
12837 else
12838 {
ed2dc618
SM
12839 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12840 dwp_file, unit_index,
73869dc2
DE
12841 comp_dir, signature,
12842 is_debug_types);
12843 }
9a3c8263 12844 return (struct dwo_unit *) *slot;
80626a55
DE
12845 }
12846 if (signature_in_table == 0)
12847 return NULL;
12848 hash = (hash + hash2) & mask;
12849 }
12850
12851 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12852 " [in module %s]"),
12853 dwp_file->name);
12854}
12855
ab5088bf 12856/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12857 Open the file specified by FILE_NAME and hand it off to BFD for
12858 preliminary analysis. Return a newly initialized bfd *, which
12859 includes a canonicalized copy of FILE_NAME.
80626a55 12860 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12861 SEARCH_CWD is true if the current directory is to be searched.
12862 It will be searched before debug-file-directory.
13aaf454
DE
12863 If successful, the file is added to the bfd include table of the
12864 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12865 If unable to find/open the file, return NULL.
3019eac3
DE
12866 NOTE: This function is derived from symfile_bfd_open. */
12867
192b62ce 12868static gdb_bfd_ref_ptr
ed2dc618
SM
12869try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12870 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12871{
24b9144d 12872 int desc;
9c02c129
DE
12873 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12874 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12875 to debug_file_directory. */
e0cc99a6 12876 const char *search_path;
9c02c129
DE
12877 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12878
e0cc99a6 12879 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12880 if (search_cwd)
12881 {
12882 if (*debug_file_directory != '\0')
e0cc99a6
TT
12883 {
12884 search_path_holder.reset (concat (".", dirname_separator_string,
12885 debug_file_directory,
12886 (char *) NULL));
12887 search_path = search_path_holder.get ();
12888 }
6ac97d4c 12889 else
e0cc99a6 12890 search_path = ".";
6ac97d4c 12891 }
9c02c129 12892 else
e0cc99a6 12893 search_path = debug_file_directory;
3019eac3 12894
24b9144d 12895 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12896 if (is_dwp)
12897 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12898
12899 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12900 desc = openp (search_path, flags, file_name,
3019eac3
DE
12901 O_RDONLY | O_BINARY, &absolute_name);
12902 if (desc < 0)
12903 return NULL;
12904
e0cc99a6
TT
12905 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12906 gnutarget, desc));
9c02c129
DE
12907 if (sym_bfd == NULL)
12908 return NULL;
192b62ce 12909 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12910
192b62ce
TT
12911 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12912 return NULL;
3019eac3 12913
13aaf454
DE
12914 /* Success. Record the bfd as having been included by the objfile's bfd.
12915 This is important because things like demangled_names_hash lives in the
12916 objfile's per_bfd space and may have references to things like symbol
12917 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12918 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12919
3019eac3
DE
12920 return sym_bfd;
12921}
12922
ab5088bf 12923/* Try to open DWO file FILE_NAME.
3019eac3
DE
12924 COMP_DIR is the DW_AT_comp_dir attribute.
12925 The result is the bfd handle of the file.
12926 If there is a problem finding or opening the file, return NULL.
12927 Upon success, the canonicalized path of the file is stored in the bfd,
12928 same as symfile_bfd_open. */
12929
192b62ce 12930static gdb_bfd_ref_ptr
ed2dc618
SM
12931open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12932 const char *file_name, const char *comp_dir)
3019eac3 12933{
80626a55 12934 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12935 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12936 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12937
12938 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12939
12940 if (comp_dir != NULL)
12941 {
43816ebc
TT
12942 gdb::unique_xmalloc_ptr<char> path_to_try
12943 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12944
12945 /* NOTE: If comp_dir is a relative path, this will also try the
12946 search path, which seems useful. */
ed2dc618 12947 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12948 path_to_try.get (),
ed2dc618 12949 0 /*is_dwp*/,
192b62ce 12950 1 /*search_cwd*/));
3019eac3
DE
12951 if (abfd != NULL)
12952 return abfd;
12953 }
12954
12955 /* That didn't work, try debug-file-directory, which, despite its name,
12956 is a list of paths. */
12957
12958 if (*debug_file_directory == '\0')
12959 return NULL;
12960
ed2dc618
SM
12961 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12962 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12963}
12964
80626a55
DE
12965/* This function is mapped across the sections and remembers the offset and
12966 size of each of the DWO debugging sections we are interested in. */
12967
12968static void
12969dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12970{
9a3c8263 12971 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12972 const struct dwop_section_names *names = &dwop_section_names;
12973
12974 if (section_is_p (sectp->name, &names->abbrev_dwo))
12975 {
049412e3 12976 dwo_sections->abbrev.s.section = sectp;
fd361982 12977 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12978 }
12979 else if (section_is_p (sectp->name, &names->info_dwo))
12980 {
049412e3 12981 dwo_sections->info.s.section = sectp;
fd361982 12982 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12983 }
12984 else if (section_is_p (sectp->name, &names->line_dwo))
12985 {
049412e3 12986 dwo_sections->line.s.section = sectp;
fd361982 12987 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12988 }
12989 else if (section_is_p (sectp->name, &names->loc_dwo))
12990 {
049412e3 12991 dwo_sections->loc.s.section = sectp;
fd361982 12992 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12993 }
12994 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12995 {
049412e3 12996 dwo_sections->macinfo.s.section = sectp;
fd361982 12997 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12998 }
12999 else if (section_is_p (sectp->name, &names->macro_dwo))
13000 {
049412e3 13001 dwo_sections->macro.s.section = sectp;
fd361982 13002 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
13003 }
13004 else if (section_is_p (sectp->name, &names->str_dwo))
13005 {
049412e3 13006 dwo_sections->str.s.section = sectp;
fd361982 13007 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
13008 }
13009 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13010 {
049412e3 13011 dwo_sections->str_offsets.s.section = sectp;
fd361982 13012 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
13013 }
13014 else if (section_is_p (sectp->name, &names->types_dwo))
13015 {
13016 struct dwarf2_section_info type_section;
13017
13018 memset (&type_section, 0, sizeof (type_section));
049412e3 13019 type_section.s.section = sectp;
fd361982 13020 type_section.size = bfd_section_size (sectp);
fd5866f6 13021 dwo_sections->types.push_back (type_section);
80626a55
DE
13022 }
13023}
13024
ab5088bf 13025/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13026 by PER_CU. This is for the non-DWP case.
80626a55 13027 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13028
13029static struct dwo_file *
0ac5b59e
DE
13030open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13031 const char *dwo_name, const char *comp_dir)
3019eac3 13032{
ed2dc618 13033 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13034
fb1eb2f9 13035 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13036 if (dbfd == NULL)
13037 {
b4f54984 13038 if (dwarf_read_debug)
80626a55
DE
13039 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13040 return NULL;
13041 }
263db9a1 13042
51ac9db5 13043 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13044 dwo_file->dwo_name = dwo_name;
13045 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13046 dwo_file->dbfd = std::move (dbfd);
3019eac3 13047
fb1eb2f9 13048 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13049 &dwo_file->sections);
3019eac3 13050
ed2dc618
SM
13051 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13052 dwo_file->cus);
3019eac3 13053
263db9a1 13054 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13055 dwo_file->sections.types, dwo_file->tus);
3019eac3 13056
b4f54984 13057 if (dwarf_read_debug)
80626a55
DE
13058 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13059
263db9a1 13060 return dwo_file.release ();
3019eac3
DE
13061}
13062
80626a55 13063/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13064 size of each of the DWP debugging sections common to version 1 and 2 that
13065 we are interested in. */
3019eac3 13066
80626a55 13067static void
73869dc2
DE
13068dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13069 void *dwp_file_ptr)
3019eac3 13070{
9a3c8263 13071 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13072 const struct dwop_section_names *names = &dwop_section_names;
13073 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13074
80626a55 13075 /* Record the ELF section number for later lookup: this is what the
73869dc2 13076 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13077 gdb_assert (elf_section_nr < dwp_file->num_sections);
13078 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13079
80626a55
DE
13080 /* Look for specific sections that we need. */
13081 if (section_is_p (sectp->name, &names->str_dwo))
13082 {
049412e3 13083 dwp_file->sections.str.s.section = sectp;
fd361982 13084 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13085 }
13086 else if (section_is_p (sectp->name, &names->cu_index))
13087 {
049412e3 13088 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13089 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13090 }
13091 else if (section_is_p (sectp->name, &names->tu_index))
13092 {
049412e3 13093 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13094 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13095 }
13096}
3019eac3 13097
73869dc2
DE
13098/* This function is mapped across the sections and remembers the offset and
13099 size of each of the DWP version 2 debugging sections that we are interested
13100 in. This is split into a separate function because we don't know if we
13101 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13102
13103static void
13104dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13105{
9a3c8263 13106 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13107 const struct dwop_section_names *names = &dwop_section_names;
13108 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13109
13110 /* Record the ELF section number for later lookup: this is what the
13111 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13112 gdb_assert (elf_section_nr < dwp_file->num_sections);
13113 dwp_file->elf_sections[elf_section_nr] = sectp;
13114
13115 /* Look for specific sections that we need. */
13116 if (section_is_p (sectp->name, &names->abbrev_dwo))
13117 {
049412e3 13118 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13119 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13120 }
13121 else if (section_is_p (sectp->name, &names->info_dwo))
13122 {
049412e3 13123 dwp_file->sections.info.s.section = sectp;
fd361982 13124 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13125 }
13126 else if (section_is_p (sectp->name, &names->line_dwo))
13127 {
049412e3 13128 dwp_file->sections.line.s.section = sectp;
fd361982 13129 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13130 }
13131 else if (section_is_p (sectp->name, &names->loc_dwo))
13132 {
049412e3 13133 dwp_file->sections.loc.s.section = sectp;
fd361982 13134 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13135 }
13136 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13137 {
049412e3 13138 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13139 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13140 }
13141 else if (section_is_p (sectp->name, &names->macro_dwo))
13142 {
049412e3 13143 dwp_file->sections.macro.s.section = sectp;
fd361982 13144 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13145 }
13146 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13147 {
049412e3 13148 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13149 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13150 }
13151 else if (section_is_p (sectp->name, &names->types_dwo))
13152 {
049412e3 13153 dwp_file->sections.types.s.section = sectp;
fd361982 13154 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13155 }
13156}
13157
80626a55 13158/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13159
80626a55
DE
13160static hashval_t
13161hash_dwp_loaded_cutus (const void *item)
13162{
9a3c8263 13163 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13164
80626a55
DE
13165 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13166 return dwo_unit->signature;
3019eac3
DE
13167}
13168
80626a55 13169/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13170
80626a55
DE
13171static int
13172eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13173{
9a3c8263
SM
13174 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13175 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13176
80626a55
DE
13177 return dua->signature == dub->signature;
13178}
3019eac3 13179
80626a55 13180/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13181
80626a55
DE
13182static htab_t
13183allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13184{
13185 return htab_create_alloc_ex (3,
13186 hash_dwp_loaded_cutus,
13187 eq_dwp_loaded_cutus,
13188 NULL,
13189 &objfile->objfile_obstack,
13190 hashtab_obstack_allocate,
13191 dummy_obstack_deallocate);
13192}
3019eac3 13193
ab5088bf
DE
13194/* Try to open DWP file FILE_NAME.
13195 The result is the bfd handle of the file.
13196 If there is a problem finding or opening the file, return NULL.
13197 Upon success, the canonicalized path of the file is stored in the bfd,
13198 same as symfile_bfd_open. */
13199
192b62ce 13200static gdb_bfd_ref_ptr
ed2dc618
SM
13201open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13202 const char *file_name)
ab5088bf 13203{
ed2dc618
SM
13204 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13205 1 /*is_dwp*/,
192b62ce 13206 1 /*search_cwd*/));
6ac97d4c
DE
13207 if (abfd != NULL)
13208 return abfd;
13209
13210 /* Work around upstream bug 15652.
13211 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13212 [Whether that's a "bug" is debatable, but it is getting in our way.]
13213 We have no real idea where the dwp file is, because gdb's realpath-ing
13214 of the executable's path may have discarded the needed info.
13215 [IWBN if the dwp file name was recorded in the executable, akin to
13216 .gnu_debuglink, but that doesn't exist yet.]
13217 Strip the directory from FILE_NAME and search again. */
13218 if (*debug_file_directory != '\0')
13219 {
13220 /* Don't implicitly search the current directory here.
13221 If the user wants to search "." to handle this case,
13222 it must be added to debug-file-directory. */
ed2dc618
SM
13223 return try_open_dwop_file (dwarf2_per_objfile,
13224 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13225 0 /*search_cwd*/);
13226 }
13227
13228 return NULL;
ab5088bf
DE
13229}
13230
80626a55
DE
13231/* Initialize the use of the DWP file for the current objfile.
13232 By convention the name of the DWP file is ${objfile}.dwp.
13233 The result is NULL if it can't be found. */
a766d390 13234
400174b1 13235static std::unique_ptr<struct dwp_file>
ed2dc618 13236open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13237{
13238 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13239
82bf32bc
JK
13240 /* Try to find first .dwp for the binary file before any symbolic links
13241 resolving. */
6c447423
DE
13242
13243 /* If the objfile is a debug file, find the name of the real binary
13244 file and get the name of dwp file from there. */
d721ba37 13245 std::string dwp_name;
6c447423
DE
13246 if (objfile->separate_debug_objfile_backlink != NULL)
13247 {
13248 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13249 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13250
d721ba37 13251 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13252 }
13253 else
d721ba37
PA
13254 dwp_name = objfile->original_name;
13255
13256 dwp_name += ".dwp";
80626a55 13257
ed2dc618 13258 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13259 if (dbfd == NULL
13260 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13261 {
13262 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13263 dwp_name = objfile_name (objfile);
13264 dwp_name += ".dwp";
ed2dc618 13265 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13266 }
13267
80626a55
DE
13268 if (dbfd == NULL)
13269 {
b4f54984 13270 if (dwarf_read_debug)
d721ba37 13271 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13272 return std::unique_ptr<dwp_file> ();
3019eac3 13273 }
400174b1
TT
13274
13275 const char *name = bfd_get_filename (dbfd.get ());
13276 std::unique_ptr<struct dwp_file> dwp_file
13277 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13278
0a0f4c01 13279 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13280 dwp_file->elf_sections =
13281 OBSTACK_CALLOC (&objfile->objfile_obstack,
13282 dwp_file->num_sections, asection *);
13283
400174b1
TT
13284 bfd_map_over_sections (dwp_file->dbfd.get (),
13285 dwarf2_locate_common_dwp_sections,
13286 dwp_file.get ());
80626a55 13287
400174b1
TT
13288 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13289 0);
80626a55 13290
400174b1
TT
13291 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13292 1);
80626a55 13293
73869dc2 13294 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13295 if (dwp_file->cus && dwp_file->tus
13296 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13297 {
13298 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13299 pretty bizarre. We use pulongest here because that's the established
4d65956b 13300 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13301 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13302 " TU version %s [in DWP file %s]"),
13303 pulongest (dwp_file->cus->version),
d721ba37 13304 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13305 }
08302ed2
DE
13306
13307 if (dwp_file->cus)
13308 dwp_file->version = dwp_file->cus->version;
13309 else if (dwp_file->tus)
13310 dwp_file->version = dwp_file->tus->version;
13311 else
13312 dwp_file->version = 2;
73869dc2
DE
13313
13314 if (dwp_file->version == 2)
400174b1
TT
13315 bfd_map_over_sections (dwp_file->dbfd.get (),
13316 dwarf2_locate_v2_dwp_sections,
13317 dwp_file.get ());
73869dc2 13318
19ac8c2e
DE
13319 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13320 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13321
b4f54984 13322 if (dwarf_read_debug)
80626a55
DE
13323 {
13324 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13325 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13326 " %s CUs, %s TUs\n",
13327 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13328 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13329 }
13330
13331 return dwp_file;
3019eac3 13332}
c906108c 13333
ab5088bf
DE
13334/* Wrapper around open_and_init_dwp_file, only open it once. */
13335
13336static struct dwp_file *
ed2dc618 13337get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13338{
13339 if (! dwarf2_per_objfile->dwp_checked)
13340 {
ed2dc618
SM
13341 dwarf2_per_objfile->dwp_file
13342 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13343 dwarf2_per_objfile->dwp_checked = 1;
13344 }
400174b1 13345 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13346}
13347
80626a55
DE
13348/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13349 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13350 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13351 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13352 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13353
13354 This is called, for example, when wanting to read a variable with a
13355 complex location. Therefore we don't want to do file i/o for every call.
13356 Therefore we don't want to look for a DWO file on every call.
13357 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13358 then we check if we've already seen DWO_NAME, and only THEN do we check
13359 for a DWO file.
13360
1c658ad5 13361 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13362 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13363
3019eac3 13364static struct dwo_unit *
80626a55
DE
13365lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13366 const char *dwo_name, const char *comp_dir,
13367 ULONGEST signature, int is_debug_types)
3019eac3 13368{
ed2dc618 13369 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13370 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13371 const char *kind = is_debug_types ? "TU" : "CU";
13372 void **dwo_file_slot;
3019eac3 13373 struct dwo_file *dwo_file;
80626a55 13374 struct dwp_file *dwp_file;
cb1df416 13375
6a506a2d
DE
13376 /* First see if there's a DWP file.
13377 If we have a DWP file but didn't find the DWO inside it, don't
13378 look for the original DWO file. It makes gdb behave differently
13379 depending on whether one is debugging in the build tree. */
cf2c3c16 13380
ed2dc618 13381 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13382 if (dwp_file != NULL)
cf2c3c16 13383 {
80626a55
DE
13384 const struct dwp_hash_table *dwp_htab =
13385 is_debug_types ? dwp_file->tus : dwp_file->cus;
13386
13387 if (dwp_htab != NULL)
13388 {
13389 struct dwo_unit *dwo_cutu =
ed2dc618 13390 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13391 signature, is_debug_types);
80626a55
DE
13392
13393 if (dwo_cutu != NULL)
13394 {
b4f54984 13395 if (dwarf_read_debug)
80626a55
DE
13396 {
13397 fprintf_unfiltered (gdb_stdlog,
13398 "Virtual DWO %s %s found: @%s\n",
13399 kind, hex_string (signature),
13400 host_address_to_string (dwo_cutu));
13401 }
13402 return dwo_cutu;
13403 }
13404 }
13405 }
6a506a2d 13406 else
80626a55 13407 {
6a506a2d 13408 /* No DWP file, look for the DWO file. */
80626a55 13409
ed2dc618
SM
13410 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13411 dwo_name, comp_dir);
6a506a2d 13412 if (*dwo_file_slot == NULL)
80626a55 13413 {
6a506a2d
DE
13414 /* Read in the file and build a table of the CUs/TUs it contains. */
13415 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13416 }
6a506a2d 13417 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13418 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13419
6a506a2d 13420 if (dwo_file != NULL)
19c3d4c9 13421 {
6a506a2d
DE
13422 struct dwo_unit *dwo_cutu = NULL;
13423
13424 if (is_debug_types && dwo_file->tus)
13425 {
13426 struct dwo_unit find_dwo_cutu;
13427
13428 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13429 find_dwo_cutu.signature = signature;
9a3c8263
SM
13430 dwo_cutu
13431 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13432 }
33c5cd75 13433 else if (!is_debug_types && dwo_file->cus)
80626a55 13434 {
33c5cd75
DB
13435 struct dwo_unit find_dwo_cutu;
13436
13437 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13438 find_dwo_cutu.signature = signature;
13439 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13440 &find_dwo_cutu);
6a506a2d
DE
13441 }
13442
13443 if (dwo_cutu != NULL)
13444 {
b4f54984 13445 if (dwarf_read_debug)
6a506a2d
DE
13446 {
13447 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13448 kind, dwo_name, hex_string (signature),
13449 host_address_to_string (dwo_cutu));
13450 }
13451 return dwo_cutu;
80626a55
DE
13452 }
13453 }
2e276125 13454 }
9cdd5dbd 13455
80626a55
DE
13456 /* We didn't find it. This could mean a dwo_id mismatch, or
13457 someone deleted the DWO/DWP file, or the search path isn't set up
13458 correctly to find the file. */
13459
b4f54984 13460 if (dwarf_read_debug)
80626a55
DE
13461 {
13462 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13463 kind, dwo_name, hex_string (signature));
13464 }
3019eac3 13465
6656a72d
DE
13466 /* This is a warning and not a complaint because it can be caused by
13467 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13468 {
13469 /* Print the name of the DWP file if we looked there, helps the user
13470 better diagnose the problem. */
791afaa2 13471 std::string dwp_text;
43942612
DE
13472
13473 if (dwp_file != NULL)
791afaa2
TT
13474 dwp_text = string_printf (" [in DWP file %s]",
13475 lbasename (dwp_file->name));
43942612 13476
9d8780f0 13477 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13478 " [in module %s]"),
13479 kind, dwo_name, hex_string (signature),
791afaa2 13480 dwp_text.c_str (),
43942612 13481 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13482 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13483 }
3019eac3 13484 return NULL;
5fb290d7
DJ
13485}
13486
80626a55
DE
13487/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13488 See lookup_dwo_cutu_unit for details. */
13489
13490static struct dwo_unit *
13491lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13492 const char *dwo_name, const char *comp_dir,
13493 ULONGEST signature)
13494{
13495 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13496}
13497
13498/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13499 See lookup_dwo_cutu_unit for details. */
13500
13501static struct dwo_unit *
13502lookup_dwo_type_unit (struct signatured_type *this_tu,
13503 const char *dwo_name, const char *comp_dir)
13504{
13505 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13506}
13507
89e63ee4
DE
13508/* Traversal function for queue_and_load_all_dwo_tus. */
13509
13510static int
13511queue_and_load_dwo_tu (void **slot, void *info)
13512{
13513 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13514 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13515 ULONGEST signature = dwo_unit->signature;
13516 struct signatured_type *sig_type =
13517 lookup_dwo_signatured_type (per_cu->cu, signature);
13518
13519 if (sig_type != NULL)
13520 {
13521 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13522
13523 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13524 a real dependency of PER_CU on SIG_TYPE. That is detected later
13525 while processing PER_CU. */
13526 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13527 load_full_type_unit (sig_cu);
ae640021 13528 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13529 }
13530
13531 return 1;
13532}
13533
13534/* Queue all TUs contained in the DWO of PER_CU to be read in.
13535 The DWO may have the only definition of the type, though it may not be
13536 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13537 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13538
13539static void
13540queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13541{
13542 struct dwo_unit *dwo_unit;
13543 struct dwo_file *dwo_file;
13544
13545 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13546 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13547 gdb_assert (per_cu->cu != NULL);
13548
13549 dwo_unit = per_cu->cu->dwo_unit;
13550 gdb_assert (dwo_unit != NULL);
13551
13552 dwo_file = dwo_unit->dwo_file;
13553 if (dwo_file->tus != NULL)
13554 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13555}
13556
3019eac3 13557/* Read in various DIEs. */
348e048f 13558
d389af10 13559/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13560 Inherit only the children of the DW_AT_abstract_origin DIE not being
13561 already referenced by DW_AT_abstract_origin from the children of the
13562 current DIE. */
d389af10
JK
13563
13564static void
13565inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13566{
13567 struct die_info *child_die;
791afaa2 13568 sect_offset *offsetp;
d389af10
JK
13569 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13570 struct die_info *origin_die;
13571 /* Iterator of the ORIGIN_DIE children. */
13572 struct die_info *origin_child_die;
d389af10 13573 struct attribute *attr;
cd02d79d
PA
13574 struct dwarf2_cu *origin_cu;
13575 struct pending **origin_previous_list_in_scope;
d389af10
JK
13576
13577 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13578 if (!attr)
13579 return;
13580
cd02d79d
PA
13581 /* Note that following die references may follow to a die in a
13582 different cu. */
13583
13584 origin_cu = cu;
13585 origin_die = follow_die_ref (die, attr, &origin_cu);
13586
13587 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13588 symbols in. */
13589 origin_previous_list_in_scope = origin_cu->list_in_scope;
13590 origin_cu->list_in_scope = cu->list_in_scope;
13591
edb3359d
DJ
13592 if (die->tag != origin_die->tag
13593 && !(die->tag == DW_TAG_inlined_subroutine
13594 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13595 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13596 sect_offset_str (die->sect_off),
13597 sect_offset_str (origin_die->sect_off));
d389af10 13598
791afaa2 13599 std::vector<sect_offset> offsets;
d389af10 13600
3ea89b92
PMR
13601 for (child_die = die->child;
13602 child_die && child_die->tag;
13603 child_die = sibling_die (child_die))
13604 {
13605 struct die_info *child_origin_die;
13606 struct dwarf2_cu *child_origin_cu;
13607
13608 /* We are trying to process concrete instance entries:
216f72a1 13609 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13610 it's not relevant to our analysis here. i.e. detecting DIEs that are
13611 present in the abstract instance but not referenced in the concrete
13612 one. */
216f72a1
JK
13613 if (child_die->tag == DW_TAG_call_site
13614 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13615 continue;
13616
c38f313d
DJ
13617 /* For each CHILD_DIE, find the corresponding child of
13618 ORIGIN_DIE. If there is more than one layer of
13619 DW_AT_abstract_origin, follow them all; there shouldn't be,
13620 but GCC versions at least through 4.4 generate this (GCC PR
13621 40573). */
3ea89b92
PMR
13622 child_origin_die = child_die;
13623 child_origin_cu = cu;
c38f313d
DJ
13624 while (1)
13625 {
cd02d79d
PA
13626 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13627 child_origin_cu);
c38f313d
DJ
13628 if (attr == NULL)
13629 break;
cd02d79d
PA
13630 child_origin_die = follow_die_ref (child_origin_die, attr,
13631 &child_origin_cu);
c38f313d
DJ
13632 }
13633
d389af10
JK
13634 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13635 counterpart may exist. */
c38f313d 13636 if (child_origin_die != child_die)
d389af10 13637 {
edb3359d
DJ
13638 if (child_die->tag != child_origin_die->tag
13639 && !(child_die->tag == DW_TAG_inlined_subroutine
13640 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13641 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13642 "different tags"),
9d8780f0
SM
13643 sect_offset_str (child_die->sect_off),
13644 sect_offset_str (child_origin_die->sect_off));
c38f313d 13645 if (child_origin_die->parent != origin_die)
b98664d3 13646 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13647 "different parents"),
9d8780f0
SM
13648 sect_offset_str (child_die->sect_off),
13649 sect_offset_str (child_origin_die->sect_off));
c38f313d 13650 else
791afaa2 13651 offsets.push_back (child_origin_die->sect_off);
d389af10 13652 }
d389af10 13653 }
791afaa2
TT
13654 std::sort (offsets.begin (), offsets.end ());
13655 sect_offset *offsets_end = offsets.data () + offsets.size ();
13656 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13657 if (offsetp[-1] == *offsetp)
b98664d3 13658 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13659 "to DIE %s as their abstract origin"),
13660 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13661
791afaa2 13662 offsetp = offsets.data ();
d389af10
JK
13663 origin_child_die = origin_die->child;
13664 while (origin_child_die && origin_child_die->tag)
13665 {
13666 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13667 while (offsetp < offsets_end
9c541725 13668 && *offsetp < origin_child_die->sect_off)
d389af10 13669 offsetp++;
b64f50a1 13670 if (offsetp >= offsets_end
9c541725 13671 || *offsetp > origin_child_die->sect_off)
d389af10 13672 {
adde2bff
DE
13673 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13674 Check whether we're already processing ORIGIN_CHILD_DIE.
13675 This can happen with mutually referenced abstract_origins.
13676 PR 16581. */
13677 if (!origin_child_die->in_process)
13678 process_die (origin_child_die, origin_cu);
d389af10
JK
13679 }
13680 origin_child_die = sibling_die (origin_child_die);
13681 }
cd02d79d 13682 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13683
13684 if (cu != origin_cu)
13685 compute_delayed_physnames (origin_cu);
d389af10
JK
13686}
13687
c906108c 13688static void
e7c27a73 13689read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13690{
518817b3 13691 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13692 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13693 struct context_stack *newobj;
c906108c
SS
13694 CORE_ADDR lowpc;
13695 CORE_ADDR highpc;
13696 struct die_info *child_die;
edb3359d 13697 struct attribute *attr, *call_line, *call_file;
15d034d0 13698 const char *name;
e142c38c 13699 CORE_ADDR baseaddr;
801e3a5b 13700 struct block *block;
edb3359d 13701 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13702 std::vector<struct symbol *> template_args;
34eaf542 13703 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13704
13705 if (inlined_func)
13706 {
13707 /* If we do not have call site information, we can't show the
13708 caller of this inlined function. That's too confusing, so
13709 only use the scope for local variables. */
13710 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13711 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13712 if (call_line == NULL || call_file == NULL)
13713 {
13714 read_lexical_block_scope (die, cu);
13715 return;
13716 }
13717 }
c906108c 13718
e142c38c
DJ
13719 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13720
94af9270 13721 name = dwarf2_name (die, cu);
c906108c 13722
e8d05480
JB
13723 /* Ignore functions with missing or empty names. These are actually
13724 illegal according to the DWARF standard. */
13725 if (name == NULL)
13726 {
b98664d3 13727 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13728 sect_offset_str (die->sect_off));
e8d05480
JB
13729 return;
13730 }
13731
13732 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13733 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13734 <= PC_BOUNDS_INVALID)
e8d05480 13735 {
ae4d0c03
PM
13736 attr = dwarf2_attr (die, DW_AT_external, cu);
13737 if (!attr || !DW_UNSND (attr))
b98664d3 13738 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13739 "for subprogram DIE at %s"),
13740 sect_offset_str (die->sect_off));
e8d05480
JB
13741 return;
13742 }
c906108c 13743
3e29f34a
MR
13744 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13745 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13746
34eaf542
TT
13747 /* If we have any template arguments, then we must allocate a
13748 different sort of symbol. */
13749 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13750 {
13751 if (child_die->tag == DW_TAG_template_type_param
13752 || child_die->tag == DW_TAG_template_value_param)
13753 {
e623cf5d 13754 templ_func = allocate_template_symbol (objfile);
cf724bc9 13755 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13756 break;
13757 }
13758 }
13759
c24bdb02 13760 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13761 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13762 (struct symbol *) templ_func);
4c2df51b 13763
81873cc8 13764 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13765 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13766 cu->language);
13767
4cecd739
DJ
13768 /* If there is a location expression for DW_AT_frame_base, record
13769 it. */
e142c38c 13770 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13771 if (attr != nullptr)
fe978cb0 13772 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13773
63e43d3a
PMR
13774 /* If there is a location for the static link, record it. */
13775 newobj->static_link = NULL;
13776 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13777 if (attr != nullptr)
63e43d3a 13778 {
224c3ddb
SM
13779 newobj->static_link
13780 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13781 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13782 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13783 }
13784
c24bdb02 13785 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13786
639d11d3 13787 if (die->child != NULL)
c906108c 13788 {
639d11d3 13789 child_die = die->child;
c906108c
SS
13790 while (child_die && child_die->tag)
13791 {
34eaf542
TT
13792 if (child_die->tag == DW_TAG_template_type_param
13793 || child_die->tag == DW_TAG_template_value_param)
13794 {
13795 struct symbol *arg = new_symbol (child_die, NULL, cu);
13796
f1078f66 13797 if (arg != NULL)
2f4732b0 13798 template_args.push_back (arg);
34eaf542
TT
13799 }
13800 else
13801 process_die (child_die, cu);
c906108c
SS
13802 child_die = sibling_die (child_die);
13803 }
13804 }
13805
d389af10
JK
13806 inherit_abstract_dies (die, cu);
13807
4a811a97
UW
13808 /* If we have a DW_AT_specification, we might need to import using
13809 directives from the context of the specification DIE. See the
13810 comment in determine_prefix. */
13811 if (cu->language == language_cplus
13812 && dwarf2_attr (die, DW_AT_specification, cu))
13813 {
13814 struct dwarf2_cu *spec_cu = cu;
13815 struct die_info *spec_die = die_specification (die, &spec_cu);
13816
13817 while (spec_die)
13818 {
13819 child_die = spec_die->child;
13820 while (child_die && child_die->tag)
13821 {
13822 if (child_die->tag == DW_TAG_imported_module)
13823 process_die (child_die, spec_cu);
13824 child_die = sibling_die (child_die);
13825 }
13826
13827 /* In some cases, GCC generates specification DIEs that
13828 themselves contain DW_AT_specification attributes. */
13829 spec_die = die_specification (spec_die, &spec_cu);
13830 }
13831 }
13832
c24bdb02 13833 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13834 /* Make a block for the local symbols within. */
c24bdb02 13835 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13836 cstk.static_link, lowpc, highpc);
801e3a5b 13837
df8a16a1 13838 /* For C++, set the block's scope. */
45280282
IB
13839 if ((cu->language == language_cplus
13840 || cu->language == language_fortran
c44af4eb
TT
13841 || cu->language == language_d
13842 || cu->language == language_rust)
4d4ec4e5 13843 && cu->processing_has_namespace_info)
195a3f6c
TT
13844 block_set_scope (block, determine_prefix (die, cu),
13845 &objfile->objfile_obstack);
df8a16a1 13846
801e3a5b
JB
13847 /* If we have address ranges, record them. */
13848 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13849
a60f3166 13850 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13851
34eaf542 13852 /* Attach template arguments to function. */
2f4732b0 13853 if (!template_args.empty ())
34eaf542
TT
13854 {
13855 gdb_assert (templ_func != NULL);
13856
2f4732b0 13857 templ_func->n_template_arguments = template_args.size ();
34eaf542 13858 templ_func->template_arguments
8d749320
SM
13859 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13860 templ_func->n_template_arguments);
34eaf542 13861 memcpy (templ_func->template_arguments,
2f4732b0 13862 template_args.data (),
34eaf542 13863 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13864
13865 /* Make sure that the symtab is set on the new symbols. Even
13866 though they don't appear in this symtab directly, other parts
13867 of gdb assume that symbols do, and this is reasonably
13868 true. */
8634679f 13869 for (symbol *sym : template_args)
3e1d3d8c 13870 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13871 }
13872
208d8187
JB
13873 /* In C++, we can have functions nested inside functions (e.g., when
13874 a function declares a class that has methods). This means that
13875 when we finish processing a function scope, we may need to go
13876 back to building a containing block's symbol lists. */
c24bdb02
KS
13877 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13878 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13879
921e78cf
JB
13880 /* If we've finished processing a top-level function, subsequent
13881 symbols go in the file symbol list. */
c24bdb02
KS
13882 if (cu->get_builder ()->outermost_context_p ())
13883 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13884}
13885
13886/* Process all the DIES contained within a lexical block scope. Start
13887 a new scope, process the dies, and then close the scope. */
13888
13889static void
e7c27a73 13890read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13891{
518817b3 13892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13893 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13894 CORE_ADDR lowpc, highpc;
13895 struct die_info *child_die;
e142c38c
DJ
13896 CORE_ADDR baseaddr;
13897
13898 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13899
13900 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13901 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13902 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13903 be nasty. Might be easier to properly extend generic blocks to
af34e669 13904 describe ranges. */
e385593e
JK
13905 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13906 {
13907 case PC_BOUNDS_NOT_PRESENT:
13908 /* DW_TAG_lexical_block has no attributes, process its children as if
13909 there was no wrapping by that DW_TAG_lexical_block.
13910 GCC does no longer produces such DWARF since GCC r224161. */
13911 for (child_die = die->child;
13912 child_die != NULL && child_die->tag;
13913 child_die = sibling_die (child_die))
13914 process_die (child_die, cu);
13915 return;
13916 case PC_BOUNDS_INVALID:
13917 return;
13918 }
3e29f34a
MR
13919 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13920 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13921
c24bdb02 13922 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13923 if (die->child != NULL)
c906108c 13924 {
639d11d3 13925 child_die = die->child;
c906108c
SS
13926 while (child_die && child_die->tag)
13927 {
e7c27a73 13928 process_die (child_die, cu);
c906108c
SS
13929 child_die = sibling_die (child_die);
13930 }
13931 }
3ea89b92 13932 inherit_abstract_dies (die, cu);
c24bdb02 13933 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13934
c24bdb02
KS
13935 if (*cu->get_builder ()->get_local_symbols () != NULL
13936 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13937 {
801e3a5b 13938 struct block *block
c24bdb02 13939 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13940 cstk.start_addr, highpc);
801e3a5b
JB
13941
13942 /* Note that recording ranges after traversing children, as we
13943 do here, means that recording a parent's ranges entails
13944 walking across all its children's ranges as they appear in
13945 the address map, which is quadratic behavior.
13946
13947 It would be nicer to record the parent's ranges before
13948 traversing its children, simply overriding whatever you find
13949 there. But since we don't even decide whether to create a
13950 block until after we've traversed its children, that's hard
13951 to do. */
13952 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13953 }
c24bdb02
KS
13954 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13955 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13956}
13957
216f72a1 13958/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13959
13960static void
13961read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13962{
518817b3 13963 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13964 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13965 CORE_ADDR pc, baseaddr;
13966 struct attribute *attr;
13967 struct call_site *call_site, call_site_local;
13968 void **slot;
13969 int nparams;
13970 struct die_info *child_die;
13971
13972 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13973
216f72a1
JK
13974 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13975 if (attr == NULL)
13976 {
13977 /* This was a pre-DWARF-5 GNU extension alias
13978 for DW_AT_call_return_pc. */
13979 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13980 }
96408a79
SA
13981 if (!attr)
13982 {
b98664d3 13983 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13984 "DIE %s [in module %s]"),
13985 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13986 return;
13987 }
31aa7e4e 13988 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13989 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13990
13991 if (cu->call_site_htab == NULL)
13992 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13993 NULL, &objfile->objfile_obstack,
13994 hashtab_obstack_allocate, NULL);
13995 call_site_local.pc = pc;
13996 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13997 if (*slot != NULL)
13998 {
b98664d3 13999 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
14000 "DIE %s [in module %s]"),
14001 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 14002 objfile_name (objfile));
96408a79
SA
14003 return;
14004 }
14005
14006 /* Count parameters at the caller. */
14007
14008 nparams = 0;
14009 for (child_die = die->child; child_die && child_die->tag;
14010 child_die = sibling_die (child_die))
14011 {
216f72a1
JK
14012 if (child_die->tag != DW_TAG_call_site_parameter
14013 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 14014 {
b98664d3 14015 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14016 "DW_TAG_call_site child DIE %s [in module %s]"),
14017 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14018 objfile_name (objfile));
96408a79
SA
14019 continue;
14020 }
14021
14022 nparams++;
14023 }
14024
224c3ddb
SM
14025 call_site
14026 = ((struct call_site *)
14027 obstack_alloc (&objfile->objfile_obstack,
14028 sizeof (*call_site)
14029 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14030 *slot = call_site;
14031 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14032 call_site->pc = pc;
14033
216f72a1
JK
14034 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14035 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14036 {
14037 struct die_info *func_die;
14038
14039 /* Skip also over DW_TAG_inlined_subroutine. */
14040 for (func_die = die->parent;
14041 func_die && func_die->tag != DW_TAG_subprogram
14042 && func_die->tag != DW_TAG_subroutine_type;
14043 func_die = func_die->parent);
14044
216f72a1
JK
14045 /* DW_AT_call_all_calls is a superset
14046 of DW_AT_call_all_tail_calls. */
96408a79 14047 if (func_die
216f72a1 14048 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14049 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14050 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14051 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14052 {
14053 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14054 not complete. But keep CALL_SITE for look ups via call_site_htab,
14055 both the initial caller containing the real return address PC and
14056 the final callee containing the current PC of a chain of tail
14057 calls do not need to have the tail call list complete. But any
14058 function candidate for a virtual tail call frame searched via
14059 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14060 determined unambiguously. */
14061 }
14062 else
14063 {
14064 struct type *func_type = NULL;
14065
14066 if (func_die)
14067 func_type = get_die_type (func_die, cu);
14068 if (func_type != NULL)
14069 {
14070 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14071
14072 /* Enlist this call site to the function. */
14073 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14074 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14075 }
14076 else
b98664d3 14077 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14078 "DIE %s [in module %s]"),
14079 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14080 }
14081 }
14082
216f72a1
JK
14083 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14084 if (attr == NULL)
14085 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14086 if (attr == NULL)
14087 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14088 if (attr == NULL)
216f72a1
JK
14089 {
14090 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14091 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14092 }
96408a79
SA
14093 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14094 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14095 /* Keep NULL DWARF_BLOCK. */;
14096 else if (attr_form_is_block (attr))
14097 {
14098 struct dwarf2_locexpr_baton *dlbaton;
14099
8d749320 14100 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14101 dlbaton->data = DW_BLOCK (attr)->data;
14102 dlbaton->size = DW_BLOCK (attr)->size;
14103 dlbaton->per_cu = cu->per_cu;
14104
14105 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14106 }
7771576e 14107 else if (attr_form_is_ref (attr))
96408a79 14108 {
96408a79
SA
14109 struct dwarf2_cu *target_cu = cu;
14110 struct die_info *target_die;
14111
ac9ec31b 14112 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14113 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14114 if (die_is_declaration (target_die, target_cu))
14115 {
7d45c7c3 14116 const char *target_physname;
9112db09
JK
14117
14118 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14119 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14120 if (target_physname == NULL)
9112db09 14121 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14122 if (target_physname == NULL)
b98664d3 14123 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14124 "physname, for referencing DIE %s [in module %s]"),
14125 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14126 else
7d455152 14127 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14128 }
14129 else
14130 {
14131 CORE_ADDR lowpc;
14132
14133 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14134 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14135 <= PC_BOUNDS_INVALID)
b98664d3 14136 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14137 "low pc, for referencing DIE %s [in module %s]"),
14138 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14139 else
3e29f34a
MR
14140 {
14141 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14142 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14143 }
96408a79
SA
14144 }
14145 }
14146 else
b98664d3 14147 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14148 "block nor reference, for DIE %s [in module %s]"),
14149 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14150
14151 call_site->per_cu = cu->per_cu;
14152
14153 for (child_die = die->child;
14154 child_die && child_die->tag;
14155 child_die = sibling_die (child_die))
14156 {
96408a79 14157 struct call_site_parameter *parameter;
1788b2d3 14158 struct attribute *loc, *origin;
96408a79 14159
216f72a1
JK
14160 if (child_die->tag != DW_TAG_call_site_parameter
14161 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14162 {
14163 /* Already printed the complaint above. */
14164 continue;
14165 }
14166
14167 gdb_assert (call_site->parameter_count < nparams);
14168 parameter = &call_site->parameter[call_site->parameter_count];
14169
1788b2d3
JK
14170 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14171 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14172 register is contained in DW_AT_call_value. */
96408a79 14173
24c5c679 14174 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14175 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14176 if (origin == NULL)
14177 {
14178 /* This was a pre-DWARF-5 GNU extension alias
14179 for DW_AT_call_parameter. */
14180 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14181 }
7771576e 14182 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14183 {
1788b2d3 14184 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14185
14186 sect_offset sect_off
14187 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14188 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14189 {
14190 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14191 binding can be done only inside one CU. Such referenced DIE
14192 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14193 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14194 "DW_TAG_call_site child DIE %s [in module %s]"),
14195 sect_offset_str (child_die->sect_off),
9c541725 14196 objfile_name (objfile));
d76b7dbc
JK
14197 continue;
14198 }
9c541725
PA
14199 parameter->u.param_cu_off
14200 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14201 }
14202 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14203 {
b98664d3 14204 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14205 "DW_TAG_call_site child DIE %s [in module %s]"),
14206 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14207 continue;
14208 }
24c5c679 14209 else
96408a79 14210 {
24c5c679
JK
14211 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14212 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14213 if (parameter->u.dwarf_reg != -1)
14214 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14215 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14216 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14217 &parameter->u.fb_offset))
14218 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14219 else
14220 {
b98664d3 14221 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14222 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14223 "DW_TAG_call_site child DIE %s "
24c5c679 14224 "[in module %s]"),
9d8780f0 14225 sect_offset_str (child_die->sect_off),
9c541725 14226 objfile_name (objfile));
24c5c679
JK
14227 continue;
14228 }
96408a79
SA
14229 }
14230
216f72a1
JK
14231 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14232 if (attr == NULL)
14233 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14234 if (!attr_form_is_block (attr))
14235 {
b98664d3 14236 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14237 "DW_TAG_call_site child DIE %s [in module %s]"),
14238 sect_offset_str (child_die->sect_off),
9c541725 14239 objfile_name (objfile));
96408a79
SA
14240 continue;
14241 }
14242 parameter->value = DW_BLOCK (attr)->data;
14243 parameter->value_size = DW_BLOCK (attr)->size;
14244
14245 /* Parameters are not pre-cleared by memset above. */
14246 parameter->data_value = NULL;
14247 parameter->data_value_size = 0;
14248 call_site->parameter_count++;
14249
216f72a1
JK
14250 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14251 if (attr == NULL)
14252 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14253 if (attr != nullptr)
96408a79
SA
14254 {
14255 if (!attr_form_is_block (attr))
b98664d3 14256 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14257 "DW_TAG_call_site child DIE %s [in module %s]"),
14258 sect_offset_str (child_die->sect_off),
9c541725 14259 objfile_name (objfile));
96408a79
SA
14260 else
14261 {
14262 parameter->data_value = DW_BLOCK (attr)->data;
14263 parameter->data_value_size = DW_BLOCK (attr)->size;
14264 }
14265 }
14266 }
14267}
14268
71a3c369
TT
14269/* Helper function for read_variable. If DIE represents a virtual
14270 table, then return the type of the concrete object that is
14271 associated with the virtual table. Otherwise, return NULL. */
14272
14273static struct type *
14274rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14275{
14276 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14277 if (attr == NULL)
14278 return NULL;
14279
14280 /* Find the type DIE. */
14281 struct die_info *type_die = NULL;
14282 struct dwarf2_cu *type_cu = cu;
14283
14284 if (attr_form_is_ref (attr))
14285 type_die = follow_die_ref (die, attr, &type_cu);
14286 if (type_die == NULL)
14287 return NULL;
14288
14289 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14290 return NULL;
14291 return die_containing_type (type_die, type_cu);
14292}
14293
14294/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14295
14296static void
14297read_variable (struct die_info *die, struct dwarf2_cu *cu)
14298{
14299 struct rust_vtable_symbol *storage = NULL;
14300
14301 if (cu->language == language_rust)
14302 {
14303 struct type *containing_type = rust_containing_type (die, cu);
14304
14305 if (containing_type != NULL)
14306 {
518817b3 14307 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 14308
468c0cbb 14309 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
14310 initialize_objfile_symbol (storage);
14311 storage->concrete_type = containing_type;
cf724bc9 14312 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14313 }
14314 }
14315
e4a62c65
TV
14316 struct symbol *res = new_symbol (die, NULL, cu, storage);
14317 struct attribute *abstract_origin
14318 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14319 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14320 if (res == NULL && loc && abstract_origin)
14321 {
14322 /* We have a variable without a name, but with a location and an abstract
14323 origin. This may be a concrete instance of an abstract variable
14324 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14325 later. */
14326 struct dwarf2_cu *origin_cu = cu;
14327 struct die_info *origin_die
14328 = follow_die_ref (die, abstract_origin, &origin_cu);
14329 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14330 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14331 }
71a3c369
TT
14332}
14333
43988095
JK
14334/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14335 reading .debug_rnglists.
14336 Callback's type should be:
14337 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14338 Return true if the attributes are present and valid, otherwise,
14339 return false. */
14340
14341template <typename Callback>
14342static bool
14343dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14344 Callback &&callback)
14345{
ed2dc618 14346 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14347 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14348 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14349 bfd *obfd = objfile->obfd;
43988095
JK
14350 /* Base address selection entry. */
14351 CORE_ADDR base;
14352 int found_base;
43988095 14353 const gdb_byte *buffer;
43988095
JK
14354 CORE_ADDR baseaddr;
14355 bool overflow = false;
14356
14357 found_base = cu->base_known;
14358 base = cu->base_address;
14359
14360 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14361 if (offset >= dwarf2_per_objfile->rnglists.size)
14362 {
b98664d3 14363 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14364 offset);
14365 return false;
14366 }
14367 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14368
14369 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14370
14371 while (1)
14372 {
7814882a
JK
14373 /* Initialize it due to a false compiler warning. */
14374 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14375 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14376 + dwarf2_per_objfile->rnglists.size);
14377 unsigned int bytes_read;
14378
14379 if (buffer == buf_end)
14380 {
14381 overflow = true;
14382 break;
14383 }
14384 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14385 switch (rlet)
14386 {
14387 case DW_RLE_end_of_list:
14388 break;
14389 case DW_RLE_base_address:
14390 if (buffer + cu->header.addr_size > buf_end)
14391 {
14392 overflow = true;
14393 break;
14394 }
14395 base = read_address (obfd, buffer, cu, &bytes_read);
14396 found_base = 1;
14397 buffer += bytes_read;
14398 break;
14399 case DW_RLE_start_length:
14400 if (buffer + cu->header.addr_size > buf_end)
14401 {
14402 overflow = true;
14403 break;
14404 }
14405 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14406 buffer += bytes_read;
14407 range_end = (range_beginning
14408 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14409 buffer += bytes_read;
14410 if (buffer > buf_end)
14411 {
14412 overflow = true;
14413 break;
14414 }
14415 break;
14416 case DW_RLE_offset_pair:
14417 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14418 buffer += bytes_read;
14419 if (buffer > buf_end)
14420 {
14421 overflow = true;
14422 break;
14423 }
14424 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14425 buffer += bytes_read;
14426 if (buffer > buf_end)
14427 {
14428 overflow = true;
14429 break;
14430 }
14431 break;
14432 case DW_RLE_start_end:
14433 if (buffer + 2 * cu->header.addr_size > buf_end)
14434 {
14435 overflow = true;
14436 break;
14437 }
14438 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14439 buffer += bytes_read;
14440 range_end = read_address (obfd, buffer, cu, &bytes_read);
14441 buffer += bytes_read;
14442 break;
14443 default:
b98664d3 14444 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14445 return false;
14446 }
14447 if (rlet == DW_RLE_end_of_list || overflow)
14448 break;
14449 if (rlet == DW_RLE_base_address)
14450 continue;
14451
14452 if (!found_base)
14453 {
14454 /* We have no valid base address for the ranges
14455 data. */
b98664d3 14456 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14457 return false;
14458 }
14459
14460 if (range_beginning > range_end)
14461 {
14462 /* Inverted range entries are invalid. */
b98664d3 14463 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14464 return false;
14465 }
14466
14467 /* Empty range entries have no effect. */
14468 if (range_beginning == range_end)
14469 continue;
14470
14471 range_beginning += base;
14472 range_end += base;
14473
14474 /* A not-uncommon case of bad debug info.
14475 Don't pollute the addrmap with bad data. */
14476 if (range_beginning + baseaddr == 0
14477 && !dwarf2_per_objfile->has_section_at_zero)
14478 {
b98664d3 14479 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14480 " [in module %s]"), objfile_name (objfile));
14481 continue;
14482 }
14483
14484 callback (range_beginning, range_end);
14485 }
14486
14487 if (overflow)
14488 {
b98664d3 14489 complaint (_("Offset %d is not terminated "
43988095
JK
14490 "for DW_AT_ranges attribute"),
14491 offset);
14492 return false;
14493 }
14494
14495 return true;
14496}
14497
14498/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14499 Callback's type should be:
14500 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14501 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14502
43988095 14503template <typename Callback>
43039443 14504static int
5f46c5a5 14505dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14506 Callback &&callback)
43039443 14507{
ed2dc618 14508 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14509 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14510 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14511 struct comp_unit_head *cu_header = &cu->header;
14512 bfd *obfd = objfile->obfd;
14513 unsigned int addr_size = cu_header->addr_size;
14514 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14515 /* Base address selection entry. */
14516 CORE_ADDR base;
14517 int found_base;
14518 unsigned int dummy;
d521ce57 14519 const gdb_byte *buffer;
ff013f42 14520 CORE_ADDR baseaddr;
43039443 14521
43988095
JK
14522 if (cu_header->version >= 5)
14523 return dwarf2_rnglists_process (offset, cu, callback);
14524
d00adf39
DE
14525 found_base = cu->base_known;
14526 base = cu->base_address;
43039443 14527
be391dca 14528 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14529 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14530 {
b98664d3 14531 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14532 offset);
14533 return 0;
14534 }
dce234bc 14535 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14536
e7030f15 14537 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14538
43039443
JK
14539 while (1)
14540 {
14541 CORE_ADDR range_beginning, range_end;
14542
14543 range_beginning = read_address (obfd, buffer, cu, &dummy);
14544 buffer += addr_size;
14545 range_end = read_address (obfd, buffer, cu, &dummy);
14546 buffer += addr_size;
14547 offset += 2 * addr_size;
14548
14549 /* An end of list marker is a pair of zero addresses. */
14550 if (range_beginning == 0 && range_end == 0)
14551 /* Found the end of list entry. */
14552 break;
14553
14554 /* Each base address selection entry is a pair of 2 values.
14555 The first is the largest possible address, the second is
14556 the base address. Check for a base address here. */
14557 if ((range_beginning & mask) == mask)
14558 {
28d2bfb9
AB
14559 /* If we found the largest possible address, then we already
14560 have the base address in range_end. */
14561 base = range_end;
43039443
JK
14562 found_base = 1;
14563 continue;
14564 }
14565
14566 if (!found_base)
14567 {
14568 /* We have no valid base address for the ranges
14569 data. */
b98664d3 14570 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14571 return 0;
14572 }
14573
9277c30c
UW
14574 if (range_beginning > range_end)
14575 {
14576 /* Inverted range entries are invalid. */
b98664d3 14577 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14578 return 0;
14579 }
14580
14581 /* Empty range entries have no effect. */
14582 if (range_beginning == range_end)
14583 continue;
14584
43039443
JK
14585 range_beginning += base;
14586 range_end += base;
14587
01093045
DE
14588 /* A not-uncommon case of bad debug info.
14589 Don't pollute the addrmap with bad data. */
14590 if (range_beginning + baseaddr == 0
14591 && !dwarf2_per_objfile->has_section_at_zero)
14592 {
b98664d3 14593 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14594 " [in module %s]"), objfile_name (objfile));
01093045
DE
14595 continue;
14596 }
14597
5f46c5a5
JK
14598 callback (range_beginning, range_end);
14599 }
14600
14601 return 1;
14602}
14603
14604/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14605 Return 1 if the attributes are present and valid, otherwise, return 0.
14606 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14607
14608static int
14609dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14610 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14611 struct partial_symtab *ranges_pst)
14612{
518817b3 14613 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14614 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14615 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14616 SECT_OFF_TEXT (objfile));
14617 int low_set = 0;
14618 CORE_ADDR low = 0;
14619 CORE_ADDR high = 0;
14620 int retval;
14621
14622 retval = dwarf2_ranges_process (offset, cu,
14623 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14624 {
9277c30c 14625 if (ranges_pst != NULL)
3e29f34a
MR
14626 {
14627 CORE_ADDR lowpc;
14628 CORE_ADDR highpc;
14629
79748972
TT
14630 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14631 range_beginning + baseaddr)
14632 - baseaddr);
14633 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14634 range_end + baseaddr)
14635 - baseaddr);
d320c2b5
TT
14636 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14637 lowpc, highpc - 1, ranges_pst);
3e29f34a 14638 }
ff013f42 14639
43039443
JK
14640 /* FIXME: This is recording everything as a low-high
14641 segment of consecutive addresses. We should have a
14642 data structure for discontiguous block ranges
14643 instead. */
14644 if (! low_set)
14645 {
14646 low = range_beginning;
14647 high = range_end;
14648 low_set = 1;
14649 }
14650 else
14651 {
14652 if (range_beginning < low)
14653 low = range_beginning;
14654 if (range_end > high)
14655 high = range_end;
14656 }
5f46c5a5
JK
14657 });
14658 if (!retval)
14659 return 0;
43039443
JK
14660
14661 if (! low_set)
14662 /* If the first entry is an end-of-list marker, the range
14663 describes an empty scope, i.e. no instructions. */
14664 return 0;
14665
14666 if (low_return)
14667 *low_return = low;
14668 if (high_return)
14669 *high_return = high;
14670 return 1;
14671}
14672
3a2b436a
JK
14673/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14674 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14675 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14676
3a2b436a 14677static enum pc_bounds_kind
af34e669 14678dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14679 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14680 struct partial_symtab *pst)
c906108c 14681{
518817b3
SM
14682 struct dwarf2_per_objfile *dwarf2_per_objfile
14683 = cu->per_cu->dwarf2_per_objfile;
c906108c 14684 struct attribute *attr;
91da1414 14685 struct attribute *attr_high;
af34e669
DJ
14686 CORE_ADDR low = 0;
14687 CORE_ADDR high = 0;
e385593e 14688 enum pc_bounds_kind ret;
c906108c 14689
91da1414
MW
14690 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14691 if (attr_high)
af34e669 14692 {
e142c38c 14693 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14694 if (attr != nullptr)
91da1414 14695 {
31aa7e4e
JB
14696 low = attr_value_as_address (attr);
14697 high = attr_value_as_address (attr_high);
14698 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14699 high += low;
91da1414 14700 }
af34e669
DJ
14701 else
14702 /* Found high w/o low attribute. */
e385593e 14703 return PC_BOUNDS_INVALID;
af34e669
DJ
14704
14705 /* Found consecutive range of addresses. */
3a2b436a 14706 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14707 }
c906108c 14708 else
af34e669 14709 {
e142c38c 14710 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14711 if (attr != NULL)
14712 {
ab435259
DE
14713 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14714 We take advantage of the fact that DW_AT_ranges does not appear
14715 in DW_TAG_compile_unit of DWO files. */
14716 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14717 unsigned int ranges_offset = (DW_UNSND (attr)
14718 + (need_ranges_base
14719 ? cu->ranges_base
14720 : 0));
2e3cf129 14721
af34e669 14722 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14723 .debug_ranges section. */
2e3cf129 14724 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14725 return PC_BOUNDS_INVALID;
43039443 14726 /* Found discontinuous range of addresses. */
3a2b436a 14727 ret = PC_BOUNDS_RANGES;
af34e669 14728 }
e385593e
JK
14729 else
14730 return PC_BOUNDS_NOT_PRESENT;
af34e669 14731 }
c906108c 14732
48fbe735 14733 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14734 if (high <= low)
e385593e 14735 return PC_BOUNDS_INVALID;
c906108c
SS
14736
14737 /* When using the GNU linker, .gnu.linkonce. sections are used to
14738 eliminate duplicate copies of functions and vtables and such.
14739 The linker will arbitrarily choose one and discard the others.
14740 The AT_*_pc values for such functions refer to local labels in
14741 these sections. If the section from that file was discarded, the
14742 labels are not in the output, so the relocs get a value of 0.
14743 If this is a discarded function, mark the pc bounds as invalid,
14744 so that GDB will ignore it. */
72dca2f5 14745 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14746 return PC_BOUNDS_INVALID;
c906108c
SS
14747
14748 *lowpc = low;
96408a79
SA
14749 if (highpc)
14750 *highpc = high;
af34e669 14751 return ret;
c906108c
SS
14752}
14753
b084d499
JB
14754/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14755 its low and high PC addresses. Do nothing if these addresses could not
14756 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14757 and HIGHPC to the high address if greater than HIGHPC. */
14758
14759static void
14760dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14761 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14762 struct dwarf2_cu *cu)
14763{
14764 CORE_ADDR low, high;
14765 struct die_info *child = die->child;
14766
e385593e 14767 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14768 {
325fac50
PA
14769 *lowpc = std::min (*lowpc, low);
14770 *highpc = std::max (*highpc, high);
b084d499
JB
14771 }
14772
14773 /* If the language does not allow nested subprograms (either inside
14774 subprograms or lexical blocks), we're done. */
14775 if (cu->language != language_ada)
14776 return;
6e70227d 14777
b084d499
JB
14778 /* Check all the children of the given DIE. If it contains nested
14779 subprograms, then check their pc bounds. Likewise, we need to
14780 check lexical blocks as well, as they may also contain subprogram
14781 definitions. */
14782 while (child && child->tag)
14783 {
14784 if (child->tag == DW_TAG_subprogram
14785 || child->tag == DW_TAG_lexical_block)
14786 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14787 child = sibling_die (child);
14788 }
14789}
14790
fae299cd
DC
14791/* Get the low and high pc's represented by the scope DIE, and store
14792 them in *LOWPC and *HIGHPC. If the correct values can't be
14793 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14794
14795static void
14796get_scope_pc_bounds (struct die_info *die,
14797 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14798 struct dwarf2_cu *cu)
14799{
14800 CORE_ADDR best_low = (CORE_ADDR) -1;
14801 CORE_ADDR best_high = (CORE_ADDR) 0;
14802 CORE_ADDR current_low, current_high;
14803
3a2b436a 14804 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14805 >= PC_BOUNDS_RANGES)
fae299cd
DC
14806 {
14807 best_low = current_low;
14808 best_high = current_high;
14809 }
14810 else
14811 {
14812 struct die_info *child = die->child;
14813
14814 while (child && child->tag)
14815 {
14816 switch (child->tag) {
14817 case DW_TAG_subprogram:
b084d499 14818 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14819 break;
14820 case DW_TAG_namespace:
f55ee35c 14821 case DW_TAG_module:
fae299cd
DC
14822 /* FIXME: carlton/2004-01-16: Should we do this for
14823 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14824 that current GCC's always emit the DIEs corresponding
14825 to definitions of methods of classes as children of a
14826 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14827 the DIEs giving the declarations, which could be
14828 anywhere). But I don't see any reason why the
14829 standards says that they have to be there. */
14830 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14831
14832 if (current_low != ((CORE_ADDR) -1))
14833 {
325fac50
PA
14834 best_low = std::min (best_low, current_low);
14835 best_high = std::max (best_high, current_high);
fae299cd
DC
14836 }
14837 break;
14838 default:
0963b4bd 14839 /* Ignore. */
fae299cd
DC
14840 break;
14841 }
14842
14843 child = sibling_die (child);
14844 }
14845 }
14846
14847 *lowpc = best_low;
14848 *highpc = best_high;
14849}
14850
801e3a5b
JB
14851/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14852 in DIE. */
380bca97 14853
801e3a5b
JB
14854static void
14855dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14856 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14857{
518817b3 14858 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14859 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14860 struct attribute *attr;
91da1414 14861 struct attribute *attr_high;
801e3a5b 14862
91da1414
MW
14863 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14864 if (attr_high)
801e3a5b 14865 {
801e3a5b 14866 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14867 if (attr != nullptr)
801e3a5b 14868 {
31aa7e4e
JB
14869 CORE_ADDR low = attr_value_as_address (attr);
14870 CORE_ADDR high = attr_value_as_address (attr_high);
14871
14872 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14873 high += low;
9a619af0 14874
3e29f34a
MR
14875 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14876 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14877 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14878 }
14879 }
14880
14881 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14882 if (attr != nullptr)
801e3a5b 14883 {
ab435259
DE
14884 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14885 We take advantage of the fact that DW_AT_ranges does not appear
14886 in DW_TAG_compile_unit of DWO files. */
14887 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14888
14889 /* The value of the DW_AT_ranges attribute is the offset of the
14890 address range list in the .debug_ranges section. */
ab435259
DE
14891 unsigned long offset = (DW_UNSND (attr)
14892 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14893
2d5f09ec 14894 std::vector<blockrange> blockvec;
5f46c5a5
JK
14895 dwarf2_ranges_process (offset, cu,
14896 [&] (CORE_ADDR start, CORE_ADDR end)
14897 {
58fdfd2c
JK
14898 start += baseaddr;
14899 end += baseaddr;
5f46c5a5
JK
14900 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14901 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14902 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14903 blockvec.emplace_back (start, end);
5f46c5a5 14904 });
2d5f09ec
KB
14905
14906 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14907 }
14908}
14909
685b1105
JK
14910/* Check whether the producer field indicates either of GCC < 4.6, or the
14911 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14912
685b1105
JK
14913static void
14914check_producer (struct dwarf2_cu *cu)
60d5a603 14915{
38360086 14916 int major, minor;
60d5a603
JK
14917
14918 if (cu->producer == NULL)
14919 {
14920 /* For unknown compilers expect their behavior is DWARF version
14921 compliant.
14922
14923 GCC started to support .debug_types sections by -gdwarf-4 since
14924 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14925 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14926 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14927 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14928 }
b1ffba5a 14929 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14930 {
38360086
MW
14931 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14932 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14933 }
5230b05a 14934 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14935 {
14936 cu->producer_is_icc = true;
14937 cu->producer_is_icc_lt_14 = major < 14;
14938 }
c258c396
JD
14939 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14940 cu->producer_is_codewarrior = true;
685b1105
JK
14941 else
14942 {
14943 /* For other non-GCC compilers, expect their behavior is DWARF version
14944 compliant. */
60d5a603
JK
14945 }
14946
9068261f 14947 cu->checked_producer = true;
685b1105 14948}
ba919b58 14949
685b1105
JK
14950/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14951 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14952 during 4.6.0 experimental. */
14953
9068261f 14954static bool
685b1105
JK
14955producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14956{
14957 if (!cu->checked_producer)
14958 check_producer (cu);
14959
14960 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14961}
14962
c258c396
JD
14963
14964/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14965 with incorrect is_stmt attributes. */
14966
14967static bool
14968producer_is_codewarrior (struct dwarf2_cu *cu)
14969{
14970 if (!cu->checked_producer)
14971 check_producer (cu);
14972
14973 return cu->producer_is_codewarrior;
14974}
14975
405feb71 14976/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14977 DW_AT_accessibility. */
14978
14979static enum dwarf_access_attribute
14980dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14981{
14982 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14983 {
14984 /* The default DWARF 2 accessibility for members is public, the default
14985 accessibility for inheritance is private. */
14986
14987 if (die->tag != DW_TAG_inheritance)
14988 return DW_ACCESS_public;
14989 else
14990 return DW_ACCESS_private;
14991 }
14992 else
14993 {
14994 /* DWARF 3+ defines the default accessibility a different way. The same
14995 rules apply now for DW_TAG_inheritance as for the members and it only
14996 depends on the container kind. */
14997
14998 if (die->parent->tag == DW_TAG_class_type)
14999 return DW_ACCESS_private;
15000 else
15001 return DW_ACCESS_public;
15002 }
15003}
15004
74ac6d43
TT
15005/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15006 offset. If the attribute was not found return 0, otherwise return
15007 1. If it was found but could not properly be handled, set *OFFSET
15008 to 0. */
15009
15010static int
15011handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15012 LONGEST *offset)
15013{
15014 struct attribute *attr;
15015
15016 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15017 if (attr != NULL)
15018 {
15019 *offset = 0;
15020
15021 /* Note that we do not check for a section offset first here.
15022 This is because DW_AT_data_member_location is new in DWARF 4,
15023 so if we see it, we can assume that a constant form is really
15024 a constant and not a section offset. */
15025 if (attr_form_is_constant (attr))
15026 *offset = dwarf2_get_attr_constant_value (attr, 0);
15027 else if (attr_form_is_section_offset (attr))
15028 dwarf2_complex_location_expr_complaint ();
15029 else if (attr_form_is_block (attr))
15030 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15031 else
15032 dwarf2_complex_location_expr_complaint ();
15033
15034 return 1;
15035 }
15036
15037 return 0;
15038}
15039
c906108c
SS
15040/* Add an aggregate field to the field list. */
15041
15042static void
107d2387 15043dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15044 struct dwarf2_cu *cu)
6e70227d 15045{
518817b3 15046 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15047 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15048 struct nextfield *new_field;
15049 struct attribute *attr;
15050 struct field *fp;
15d034d0 15051 const char *fieldname = "";
c906108c 15052
7d0ccb61
DJ
15053 if (die->tag == DW_TAG_inheritance)
15054 {
be2daae6
TT
15055 fip->baseclasses.emplace_back ();
15056 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15057 }
15058 else
15059 {
be2daae6
TT
15060 fip->fields.emplace_back ();
15061 new_field = &fip->fields.back ();
7d0ccb61 15062 }
be2daae6 15063
c906108c
SS
15064 fip->nfields++;
15065
e142c38c 15066 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15067 if (attr != nullptr)
c906108c 15068 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15069 else
15070 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15071 if (new_field->accessibility != DW_ACCESS_public)
15072 fip->non_public_fields = 1;
60d5a603 15073
e142c38c 15074 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15075 if (attr != nullptr)
c906108c 15076 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15077 else
15078 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15079
15080 fp = &new_field->field;
a9a9bd0f 15081
e142c38c 15082 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15083 {
74ac6d43
TT
15084 LONGEST offset;
15085
a9a9bd0f 15086 /* Data member other than a C++ static data member. */
6e70227d 15087
c906108c 15088 /* Get type of field. */
e7c27a73 15089 fp->type = die_type (die, cu);
c906108c 15090
d6a843b5 15091 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15092
c906108c 15093 /* Get bit size of field (zero if none). */
e142c38c 15094 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15095 if (attr != nullptr)
c906108c
SS
15096 {
15097 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15098 }
15099 else
15100 {
15101 FIELD_BITSIZE (*fp) = 0;
15102 }
15103
15104 /* Get bit offset of field. */
74ac6d43
TT
15105 if (handle_data_member_location (die, cu, &offset))
15106 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15107 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15108 if (attr != nullptr)
c906108c 15109 {
d5a22e77 15110 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15111 {
15112 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15113 additional bit offset from the MSB of the containing
15114 anonymous object to the MSB of the field. We don't
15115 have to do anything special since we don't need to
15116 know the size of the anonymous object. */
f41f5e61 15117 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15118 }
15119 else
15120 {
15121 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15122 MSB of the anonymous object, subtract off the number of
15123 bits from the MSB of the field to the MSB of the
15124 object, and then subtract off the number of bits of
15125 the field itself. The result is the bit offset of
15126 the LSB of the field. */
c906108c
SS
15127 int anonymous_size;
15128 int bit_offset = DW_UNSND (attr);
15129
e142c38c 15130 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15131 if (attr != nullptr)
c906108c
SS
15132 {
15133 /* The size of the anonymous object containing
15134 the bit field is explicit, so use the
15135 indicated size (in bytes). */
15136 anonymous_size = DW_UNSND (attr);
15137 }
15138 else
15139 {
15140 /* The size of the anonymous object containing
15141 the bit field must be inferred from the type
15142 attribute of the data member containing the
15143 bit field. */
15144 anonymous_size = TYPE_LENGTH (fp->type);
15145 }
f41f5e61
PA
15146 SET_FIELD_BITPOS (*fp,
15147 (FIELD_BITPOS (*fp)
15148 + anonymous_size * bits_per_byte
15149 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15150 }
15151 }
da5b30da
AA
15152 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15153 if (attr != NULL)
15154 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15155 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15156
15157 /* Get name of field. */
39cbfefa
DJ
15158 fieldname = dwarf2_name (die, cu);
15159 if (fieldname == NULL)
15160 fieldname = "";
d8151005
DJ
15161
15162 /* The name is already allocated along with this objfile, so we don't
15163 need to duplicate it for the type. */
15164 fp->name = fieldname;
c906108c
SS
15165
15166 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15167 pointer or virtual base class pointer) to private. */
e142c38c 15168 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15169 {
d48cc9dd 15170 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15171 new_field->accessibility = DW_ACCESS_private;
15172 fip->non_public_fields = 1;
15173 }
15174 }
a9a9bd0f 15175 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15176 {
a9a9bd0f
DC
15177 /* C++ static member. */
15178
15179 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15180 is a declaration, but all versions of G++ as of this writing
15181 (so through at least 3.2.1) incorrectly generate
15182 DW_TAG_variable tags. */
6e70227d 15183
ff355380 15184 const char *physname;
c906108c 15185
a9a9bd0f 15186 /* Get name of field. */
39cbfefa
DJ
15187 fieldname = dwarf2_name (die, cu);
15188 if (fieldname == NULL)
c906108c
SS
15189 return;
15190
254e6b9e 15191 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15192 if (attr
15193 /* Only create a symbol if this is an external value.
15194 new_symbol checks this and puts the value in the global symbol
15195 table, which we want. If it is not external, new_symbol
15196 will try to put the value in cu->list_in_scope which is wrong. */
15197 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15198 {
15199 /* A static const member, not much different than an enum as far as
15200 we're concerned, except that we can support more types. */
15201 new_symbol (die, NULL, cu);
15202 }
15203
2df3850c 15204 /* Get physical name. */
ff355380 15205 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15206
d8151005
DJ
15207 /* The name is already allocated along with this objfile, so we don't
15208 need to duplicate it for the type. */
15209 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15210 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15211 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15212 }
15213 else if (die->tag == DW_TAG_inheritance)
15214 {
74ac6d43 15215 LONGEST offset;
d4b96c9a 15216
74ac6d43
TT
15217 /* C++ base class field. */
15218 if (handle_data_member_location (die, cu, &offset))
15219 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15220 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15221 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15222 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15223 }
2ddeaf8a
TT
15224 else if (die->tag == DW_TAG_variant_part)
15225 {
15226 /* process_structure_scope will treat this DIE as a union. */
15227 process_structure_scope (die, cu);
15228
15229 /* The variant part is relative to the start of the enclosing
15230 structure. */
15231 SET_FIELD_BITPOS (*fp, 0);
15232 fp->type = get_die_type (die, cu);
15233 fp->artificial = 1;
15234 fp->name = "<<variant>>";
c8c81635
TT
15235
15236 /* Normally a DW_TAG_variant_part won't have a size, but our
15237 representation requires one, so set it to the maximum of the
489dbda6
TT
15238 child sizes, being sure to account for the offset at which
15239 each child is seen. */
c8c81635
TT
15240 if (TYPE_LENGTH (fp->type) == 0)
15241 {
15242 unsigned max = 0;
15243 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
15244 {
15245 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15246 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15247 if (len > max)
15248 max = len;
15249 }
c8c81635
TT
15250 TYPE_LENGTH (fp->type) = max;
15251 }
2ddeaf8a
TT
15252 }
15253 else
15254 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15255}
15256
883fd55a
KS
15257/* Can the type given by DIE define another type? */
15258
15259static bool
15260type_can_define_types (const struct die_info *die)
15261{
15262 switch (die->tag)
15263 {
15264 case DW_TAG_typedef:
15265 case DW_TAG_class_type:
15266 case DW_TAG_structure_type:
15267 case DW_TAG_union_type:
15268 case DW_TAG_enumeration_type:
15269 return true;
15270
15271 default:
15272 return false;
15273 }
15274}
15275
15276/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15277
15278static void
883fd55a
KS
15279dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15280 struct dwarf2_cu *cu)
6e70227d 15281{
be2daae6
TT
15282 struct decl_field fp;
15283 memset (&fp, 0, sizeof (fp));
98751a41 15284
883fd55a 15285 gdb_assert (type_can_define_types (die));
98751a41 15286
883fd55a 15287 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15288 fp.name = dwarf2_name (die, cu);
15289 fp.type = read_type_die (die, cu);
98751a41 15290
c191a687
KS
15291 /* Save accessibility. */
15292 enum dwarf_access_attribute accessibility;
15293 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15294 if (attr != NULL)
15295 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15296 else
15297 accessibility = dwarf2_default_access_attribute (die, cu);
15298 switch (accessibility)
15299 {
15300 case DW_ACCESS_public:
15301 /* The assumed value if neither private nor protected. */
15302 break;
15303 case DW_ACCESS_private:
be2daae6 15304 fp.is_private = 1;
c191a687
KS
15305 break;
15306 case DW_ACCESS_protected:
be2daae6 15307 fp.is_protected = 1;
c191a687
KS
15308 break;
15309 default:
b98664d3 15310 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15311 }
15312
883fd55a 15313 if (die->tag == DW_TAG_typedef)
be2daae6 15314 fip->typedef_field_list.push_back (fp);
883fd55a 15315 else
be2daae6 15316 fip->nested_types_list.push_back (fp);
98751a41
JK
15317}
15318
c906108c
SS
15319/* Create the vector of fields, and attach it to the type. */
15320
15321static void
fba45db2 15322dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15323 struct dwarf2_cu *cu)
c906108c
SS
15324{
15325 int nfields = fip->nfields;
15326
15327 /* Record the field count, allocate space for the array of fields,
15328 and create blank accessibility bitfields if necessary. */
15329 TYPE_NFIELDS (type) = nfields;
15330 TYPE_FIELDS (type) = (struct field *)
be2daae6 15331 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15332
b4ba55a1 15333 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15334 {
15335 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15336
15337 TYPE_FIELD_PRIVATE_BITS (type) =
15338 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15339 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15340
15341 TYPE_FIELD_PROTECTED_BITS (type) =
15342 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15343 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15344
774b6a14
TT
15345 TYPE_FIELD_IGNORE_BITS (type) =
15346 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15347 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15348 }
15349
15350 /* If the type has baseclasses, allocate and clear a bit vector for
15351 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15352 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15353 {
be2daae6 15354 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15355 unsigned char *pointer;
c906108c
SS
15356
15357 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15358 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15359 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15360 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15361 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15362 }
15363
2ddeaf8a
TT
15364 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15365 {
15366 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15367
be2daae6 15368 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15369 {
be2daae6
TT
15370 struct nextfield &field = fip->fields[index];
15371
15372 if (field.variant.is_discriminant)
2ddeaf8a 15373 di->discriminant_index = index;
be2daae6 15374 else if (field.variant.default_branch)
2ddeaf8a
TT
15375 di->default_index = index;
15376 else
be2daae6 15377 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15378 }
15379 }
15380
be2daae6
TT
15381 /* Copy the saved-up fields into the field vector. */
15382 for (int i = 0; i < nfields; ++i)
c906108c 15383 {
be2daae6
TT
15384 struct nextfield &field
15385 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15386 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15387
be2daae6
TT
15388 TYPE_FIELD (type, i) = field.field;
15389 switch (field.accessibility)
c906108c 15390 {
c5aa993b 15391 case DW_ACCESS_private:
b4ba55a1 15392 if (cu->language != language_ada)
be2daae6 15393 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15394 break;
c906108c 15395
c5aa993b 15396 case DW_ACCESS_protected:
b4ba55a1 15397 if (cu->language != language_ada)
be2daae6 15398 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15399 break;
c906108c 15400
c5aa993b
JM
15401 case DW_ACCESS_public:
15402 break;
c906108c 15403
c5aa993b
JM
15404 default:
15405 /* Unknown accessibility. Complain and treat it as public. */
15406 {
b98664d3 15407 complaint (_("unsupported accessibility %d"),
be2daae6 15408 field.accessibility);
c5aa993b
JM
15409 }
15410 break;
c906108c 15411 }
be2daae6 15412 if (i < fip->baseclasses.size ())
c906108c 15413 {
be2daae6 15414 switch (field.virtuality)
c906108c 15415 {
c5aa993b
JM
15416 case DW_VIRTUALITY_virtual:
15417 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15418 if (cu->language == language_ada)
a73c6dcd 15419 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15420 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15421 break;
c906108c
SS
15422 }
15423 }
c906108c
SS
15424 }
15425}
15426
7d27a96d
TT
15427/* Return true if this member function is a constructor, false
15428 otherwise. */
15429
15430static int
15431dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15432{
15433 const char *fieldname;
fe978cb0 15434 const char *type_name;
7d27a96d
TT
15435 int len;
15436
15437 if (die->parent == NULL)
15438 return 0;
15439
15440 if (die->parent->tag != DW_TAG_structure_type
15441 && die->parent->tag != DW_TAG_union_type
15442 && die->parent->tag != DW_TAG_class_type)
15443 return 0;
15444
15445 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15446 type_name = dwarf2_name (die->parent, cu);
15447 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15448 return 0;
15449
15450 len = strlen (fieldname);
fe978cb0
PA
15451 return (strncmp (fieldname, type_name, len) == 0
15452 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15453}
15454
e35000a7
TBA
15455/* Check if the given VALUE is a recognized enum
15456 dwarf_defaulted_attribute constant according to DWARF5 spec,
15457 Table 7.24. */
15458
15459static bool
15460is_valid_DW_AT_defaulted (ULONGEST value)
15461{
15462 switch (value)
15463 {
15464 case DW_DEFAULTED_no:
15465 case DW_DEFAULTED_in_class:
15466 case DW_DEFAULTED_out_of_class:
15467 return true;
15468 }
15469
3142e908 15470 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15471 return false;
15472}
15473
c906108c
SS
15474/* Add a member function to the proper fieldlist. */
15475
15476static void
107d2387 15477dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15478 struct type *type, struct dwarf2_cu *cu)
c906108c 15479{
518817b3 15480 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15481 struct attribute *attr;
c906108c 15482 int i;
be2daae6 15483 struct fnfieldlist *flp = nullptr;
c906108c 15484 struct fn_field *fnp;
15d034d0 15485 const char *fieldname;
f792889a 15486 struct type *this_type;
60d5a603 15487 enum dwarf_access_attribute accessibility;
c906108c 15488
b4ba55a1 15489 if (cu->language == language_ada)
a73c6dcd 15490 error (_("unexpected member function in Ada type"));
b4ba55a1 15491
2df3850c 15492 /* Get name of member function. */
39cbfefa
DJ
15493 fieldname = dwarf2_name (die, cu);
15494 if (fieldname == NULL)
2df3850c 15495 return;
c906108c 15496
c906108c 15497 /* Look up member function name in fieldlist. */
be2daae6 15498 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15499 {
27bfe10e 15500 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15501 {
15502 flp = &fip->fnfieldlists[i];
15503 break;
15504 }
c906108c
SS
15505 }
15506
be2daae6
TT
15507 /* Create a new fnfieldlist if necessary. */
15508 if (flp == nullptr)
c906108c 15509 {
be2daae6
TT
15510 fip->fnfieldlists.emplace_back ();
15511 flp = &fip->fnfieldlists.back ();
c906108c 15512 flp->name = fieldname;
be2daae6 15513 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15514 }
15515
be2daae6
TT
15516 /* Create a new member function field and add it to the vector of
15517 fnfieldlists. */
15518 flp->fnfields.emplace_back ();
15519 fnp = &flp->fnfields.back ();
3da10d80
KS
15520
15521 /* Delay processing of the physname until later. */
9c37b5ae 15522 if (cu->language == language_cplus)
be2daae6
TT
15523 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15524 die, cu);
3da10d80
KS
15525 else
15526 {
1d06ead6 15527 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15528 fnp->physname = physname ? physname : "";
15529 }
15530
c906108c 15531 fnp->type = alloc_type (objfile);
f792889a
DJ
15532 this_type = read_type_die (die, cu);
15533 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15534 {
f792889a 15535 int nparams = TYPE_NFIELDS (this_type);
c906108c 15536
f792889a 15537 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15538 of the method itself (TYPE_CODE_METHOD). */
15539 smash_to_method_type (fnp->type, type,
f792889a
DJ
15540 TYPE_TARGET_TYPE (this_type),
15541 TYPE_FIELDS (this_type),
15542 TYPE_NFIELDS (this_type),
15543 TYPE_VARARGS (this_type));
c906108c
SS
15544
15545 /* Handle static member functions.
c5aa993b 15546 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15547 member functions. G++ helps GDB by marking the first
15548 parameter for non-static member functions (which is the this
15549 pointer) as artificial. We obtain this information from
15550 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15551 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15552 fnp->voffset = VOFFSET_STATIC;
15553 }
15554 else
b98664d3 15555 complaint (_("member function type missing for '%s'"),
3da10d80 15556 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15557
15558 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15559 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15560 fnp->fcontext = die_containing_type (die, cu);
c906108c 15561
3e43a32a
MS
15562 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15563 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15564
15565 /* Get accessibility. */
e142c38c 15566 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15567 if (attr != nullptr)
aead7601 15568 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15569 else
15570 accessibility = dwarf2_default_access_attribute (die, cu);
15571 switch (accessibility)
c906108c 15572 {
60d5a603
JK
15573 case DW_ACCESS_private:
15574 fnp->is_private = 1;
15575 break;
15576 case DW_ACCESS_protected:
15577 fnp->is_protected = 1;
15578 break;
c906108c
SS
15579 }
15580
b02dede2 15581 /* Check for artificial methods. */
e142c38c 15582 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15583 if (attr && DW_UNSND (attr) != 0)
15584 fnp->is_artificial = 1;
15585
e35000a7
TBA
15586 /* Check for defaulted methods. */
15587 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15588 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15589 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15590
15591 /* Check for deleted methods. */
15592 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15593 if (attr != nullptr && DW_UNSND (attr) != 0)
15594 fnp->is_deleted = 1;
15595
7d27a96d
TT
15596 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15597
0d564a31 15598 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15599 function. For older versions of GCC, this is an offset in the
15600 appropriate virtual table, as specified by DW_AT_containing_type.
15601 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15602 to the object address. */
15603
e142c38c 15604 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15605 if (attr != nullptr)
8e19ed76 15606 {
aec5aa8b 15607 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15608 {
aec5aa8b
TT
15609 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15610 {
15611 /* Old-style GCC. */
15612 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15613 }
15614 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15615 || (DW_BLOCK (attr)->size > 1
15616 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15617 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15618 {
aec5aa8b
TT
15619 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15620 if ((fnp->voffset % cu->header.addr_size) != 0)
15621 dwarf2_complex_location_expr_complaint ();
15622 else
15623 fnp->voffset /= cu->header.addr_size;
15624 fnp->voffset += 2;
15625 }
15626 else
15627 dwarf2_complex_location_expr_complaint ();
15628
15629 if (!fnp->fcontext)
7e993ebf
KS
15630 {
15631 /* If there is no `this' field and no DW_AT_containing_type,
15632 we cannot actually find a base class context for the
15633 vtable! */
15634 if (TYPE_NFIELDS (this_type) == 0
15635 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15636 {
b98664d3 15637 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15638 "function \"%s\" (offset %s)"),
15639 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15640 }
15641 else
15642 {
15643 fnp->fcontext
15644 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15645 }
15646 }
aec5aa8b 15647 }
3690dd37 15648 else if (attr_form_is_section_offset (attr))
8e19ed76 15649 {
4d3c2250 15650 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15651 }
15652 else
15653 {
4d3c2250
KB
15654 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15655 fieldname);
8e19ed76 15656 }
0d564a31 15657 }
d48cc9dd
DJ
15658 else
15659 {
15660 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15661 if (attr && DW_UNSND (attr))
15662 {
15663 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15664 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15665 "but the vtable offset is not specified"),
9d8780f0 15666 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15667 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15668 TYPE_CPLUS_DYNAMIC (type) = 1;
15669 }
15670 }
c906108c
SS
15671}
15672
15673/* Create the vector of member function fields, and attach it to the type. */
15674
15675static void
fba45db2 15676dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15677 struct dwarf2_cu *cu)
c906108c 15678{
b4ba55a1 15679 if (cu->language == language_ada)
a73c6dcd 15680 error (_("unexpected member functions in Ada type"));
b4ba55a1 15681
c906108c
SS
15682 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15683 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15684 TYPE_ALLOC (type,
15685 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15686
be2daae6 15687 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15688 {
be2daae6 15689 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15690 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15691
be2daae6
TT
15692 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15693 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15694 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15695 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15696
15697 for (int k = 0; k < nf.fnfields.size (); ++k)
15698 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15699 }
15700
be2daae6 15701 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15702}
15703
1168df01
JB
15704/* Returns non-zero if NAME is the name of a vtable member in CU's
15705 language, zero otherwise. */
15706static int
15707is_vtable_name (const char *name, struct dwarf2_cu *cu)
15708{
15709 static const char vptr[] = "_vptr";
15710
9c37b5ae
TT
15711 /* Look for the C++ form of the vtable. */
15712 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15713 return 1;
15714
15715 return 0;
15716}
15717
c0dd20ea 15718/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15719 functions, with the ABI-specified layout. If TYPE describes
15720 such a structure, smash it into a member function type.
61049d3b
DJ
15721
15722 GCC shouldn't do this; it should just output pointer to member DIEs.
15723 This is GCC PR debug/28767. */
c0dd20ea 15724
0b92b5bb
TT
15725static void
15726quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15727{
09e2d7c7 15728 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15729
15730 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15731 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15732 return;
c0dd20ea
DJ
15733
15734 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15735 if (TYPE_FIELD_NAME (type, 0) == NULL
15736 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15737 || TYPE_FIELD_NAME (type, 1) == NULL
15738 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15739 return;
c0dd20ea
DJ
15740
15741 /* Find the type of the method. */
0b92b5bb 15742 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15743 if (pfn_type == NULL
15744 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15745 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15746 return;
c0dd20ea
DJ
15747
15748 /* Look for the "this" argument. */
15749 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15750 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15751 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15752 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15753 return;
c0dd20ea 15754
09e2d7c7 15755 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15756 new_type = alloc_type (objfile);
09e2d7c7 15757 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15758 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15759 TYPE_VARARGS (pfn_type));
0b92b5bb 15760 smash_to_methodptr_type (type, new_type);
c0dd20ea 15761}
1168df01 15762
2b4424c3
TT
15763/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15764 appropriate error checking and issuing complaints if there is a
15765 problem. */
15766
15767static ULONGEST
15768get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15769{
15770 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15771
15772 if (attr == nullptr)
15773 return 0;
15774
15775 if (!attr_form_is_constant (attr))
15776 {
b98664d3 15777 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15778 " - DIE at %s [in module %s]"),
15779 sect_offset_str (die->sect_off),
15780 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15781 return 0;
15782 }
15783
15784 ULONGEST align;
15785 if (attr->form == DW_FORM_sdata)
15786 {
15787 LONGEST val = DW_SND (attr);
15788 if (val < 0)
15789 {
b98664d3 15790 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15791 " - DIE at %s [in module %s]"),
15792 sect_offset_str (die->sect_off),
15793 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15794 return 0;
15795 }
15796 align = val;
15797 }
15798 else
15799 align = DW_UNSND (attr);
15800
15801 if (align == 0)
15802 {
b98664d3 15803 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15804 " - DIE at %s [in module %s]"),
15805 sect_offset_str (die->sect_off),
15806 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15807 return 0;
15808 }
15809 if ((align & (align - 1)) != 0)
15810 {
b98664d3 15811 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15812 " - DIE at %s [in module %s]"),
15813 sect_offset_str (die->sect_off),
15814 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15815 return 0;
15816 }
15817
15818 return align;
15819}
15820
15821/* If the DIE has a DW_AT_alignment attribute, use its value to set
15822 the alignment for TYPE. */
15823
15824static void
15825maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15826 struct type *type)
15827{
15828 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15829 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15830 " - DIE at %s [in module %s]"),
15831 sect_offset_str (die->sect_off),
15832 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15833}
685b1105 15834
e35000a7
TBA
15835/* Check if the given VALUE is a valid enum dwarf_calling_convention
15836 constant for a type, according to DWARF5 spec, Table 5.5. */
15837
15838static bool
15839is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15840{
15841 switch (value)
15842 {
15843 case DW_CC_normal:
15844 case DW_CC_pass_by_reference:
15845 case DW_CC_pass_by_value:
15846 return true;
15847
15848 default:
15849 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15850 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15851 return false;
15852 }
15853}
15854
d0922fcf
TBA
15855/* Check if the given VALUE is a valid enum dwarf_calling_convention
15856 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15857 also according to GNU-specific values (see include/dwarf2.h). */
15858
15859static bool
15860is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15861{
15862 switch (value)
15863 {
15864 case DW_CC_normal:
15865 case DW_CC_program:
15866 case DW_CC_nocall:
15867 return true;
15868
15869 case DW_CC_GNU_renesas_sh:
15870 case DW_CC_GNU_borland_fastcall_i386:
15871 case DW_CC_GDB_IBM_OpenCL:
15872 return true;
15873
15874 default:
15875 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15876 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15877 return false;
15878 }
15879}
15880
c906108c 15881/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15882 (definition) to create a type for the structure or union. Fill in
15883 the type's name and general properties; the members will not be
83655187
DE
15884 processed until process_structure_scope. A symbol table entry for
15885 the type will also not be done until process_structure_scope (assuming
15886 the type has a name).
c906108c 15887
c767944b
DJ
15888 NOTE: we need to call these functions regardless of whether or not the
15889 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15890 structure or union. This gets the type entered into our set of
83655187 15891 user defined types. */
c906108c 15892
f792889a 15893static struct type *
134d01f1 15894read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15895{
518817b3 15896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15897 struct type *type;
15898 struct attribute *attr;
15d034d0 15899 const char *name;
c906108c 15900
348e048f
DE
15901 /* If the definition of this type lives in .debug_types, read that type.
15902 Don't follow DW_AT_specification though, that will take us back up
15903 the chain and we want to go down. */
45e58e77 15904 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15905 if (attr != nullptr)
348e048f 15906 {
ac9ec31b 15907 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15908
ac9ec31b 15909 /* The type's CU may not be the same as CU.
02142a6c 15910 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15911 return set_die_type (die, type, cu);
15912 }
15913
c0dd20ea 15914 type = alloc_type (objfile);
c906108c 15915 INIT_CPLUS_SPECIFIC (type);
93311388 15916
39cbfefa
DJ
15917 name = dwarf2_name (die, cu);
15918 if (name != NULL)
c906108c 15919 {
987504bb 15920 if (cu->language == language_cplus
c44af4eb
TT
15921 || cu->language == language_d
15922 || cu->language == language_rust)
63d06c5c 15923 {
15d034d0 15924 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15925
15926 /* dwarf2_full_name might have already finished building the DIE's
15927 type. If so, there is no need to continue. */
15928 if (get_die_type (die, cu) != NULL)
15929 return get_die_type (die, cu);
15930
e86ca25f 15931 TYPE_NAME (type) = full_name;
63d06c5c
DC
15932 }
15933 else
15934 {
d8151005
DJ
15935 /* The name is already allocated along with this objfile, so
15936 we don't need to duplicate it for the type. */
e86ca25f 15937 TYPE_NAME (type) = name;
63d06c5c 15938 }
c906108c
SS
15939 }
15940
15941 if (die->tag == DW_TAG_structure_type)
15942 {
15943 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15944 }
15945 else if (die->tag == DW_TAG_union_type)
15946 {
15947 TYPE_CODE (type) = TYPE_CODE_UNION;
15948 }
2ddeaf8a
TT
15949 else if (die->tag == DW_TAG_variant_part)
15950 {
15951 TYPE_CODE (type) = TYPE_CODE_UNION;
15952 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15953 }
c906108c
SS
15954 else
15955 {
4753d33b 15956 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15957 }
15958
0cc2414c
TT
15959 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15960 TYPE_DECLARED_CLASS (type) = 1;
15961
e35000a7
TBA
15962 /* Store the calling convention in the type if it's available in
15963 the die. Otherwise the calling convention remains set to
15964 the default value DW_CC_normal. */
15965 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15966 if (attr != nullptr
15967 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15968 {
15969 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15970 TYPE_CPLUS_CALLING_CONVENTION (type)
15971 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15972 }
15973
e142c38c 15974 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15975 if (attr != nullptr)
c906108c 15976 {
155bfbd3
JB
15977 if (attr_form_is_constant (attr))
15978 TYPE_LENGTH (type) = DW_UNSND (attr);
15979 else
15980 {
15981 /* For the moment, dynamic type sizes are not supported
15982 by GDB's struct type. The actual size is determined
15983 on-demand when resolving the type of a given object,
15984 so set the type's length to zero for now. Otherwise,
15985 we record an expression as the length, and that expression
15986 could lead to a very large value, which could eventually
15987 lead to us trying to allocate that much memory when creating
15988 a value of that type. */
15989 TYPE_LENGTH (type) = 0;
15990 }
c906108c
SS
15991 }
15992 else
15993 {
15994 TYPE_LENGTH (type) = 0;
15995 }
15996
2b4424c3
TT
15997 maybe_set_alignment (cu, die, type);
15998
5230b05a 15999 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16000 {
5230b05a
WT
16001 /* ICC<14 does not output the required DW_AT_declaration on
16002 incomplete types, but gives them a size of zero. */
422b1cb0 16003 TYPE_STUB (type) = 1;
685b1105
JK
16004 }
16005 else
16006 TYPE_STUB_SUPPORTED (type) = 1;
16007
dc718098 16008 if (die_is_declaration (die, cu))
876cecd0 16009 TYPE_STUB (type) = 1;
a6c727b2
DJ
16010 else if (attr == NULL && die->child == NULL
16011 && producer_is_realview (cu->producer))
16012 /* RealView does not output the required DW_AT_declaration
16013 on incomplete types. */
16014 TYPE_STUB (type) = 1;
dc718098 16015
c906108c
SS
16016 /* We need to add the type field to the die immediately so we don't
16017 infinitely recurse when dealing with pointers to the structure
0963b4bd 16018 type within the structure itself. */
1c379e20 16019 set_die_type (die, type, cu);
c906108c 16020
7e314c57
JK
16021 /* set_die_type should be already done. */
16022 set_descriptive_type (type, die, cu);
16023
c767944b
DJ
16024 return type;
16025}
16026
2ddeaf8a
TT
16027/* A helper for process_structure_scope that handles a single member
16028 DIE. */
16029
16030static void
16031handle_struct_member_die (struct die_info *child_die, struct type *type,
16032 struct field_info *fi,
16033 std::vector<struct symbol *> *template_args,
16034 struct dwarf2_cu *cu)
16035{
16036 if (child_die->tag == DW_TAG_member
16037 || child_die->tag == DW_TAG_variable
16038 || child_die->tag == DW_TAG_variant_part)
16039 {
16040 /* NOTE: carlton/2002-11-05: A C++ static data member
16041 should be a DW_TAG_member that is a declaration, but
16042 all versions of G++ as of this writing (so through at
16043 least 3.2.1) incorrectly generate DW_TAG_variable
16044 tags for them instead. */
16045 dwarf2_add_field (fi, child_die, cu);
16046 }
16047 else if (child_die->tag == DW_TAG_subprogram)
16048 {
16049 /* Rust doesn't have member functions in the C++ sense.
16050 However, it does emit ordinary functions as children
16051 of a struct DIE. */
16052 if (cu->language == language_rust)
16053 read_func_scope (child_die, cu);
16054 else
16055 {
16056 /* C++ member function. */
16057 dwarf2_add_member_fn (fi, child_die, type, cu);
16058 }
16059 }
16060 else if (child_die->tag == DW_TAG_inheritance)
16061 {
16062 /* C++ base class field. */
16063 dwarf2_add_field (fi, child_die, cu);
16064 }
16065 else if (type_can_define_types (child_die))
16066 dwarf2_add_type_defn (fi, child_die, cu);
16067 else if (child_die->tag == DW_TAG_template_type_param
16068 || child_die->tag == DW_TAG_template_value_param)
16069 {
16070 struct symbol *arg = new_symbol (child_die, NULL, cu);
16071
16072 if (arg != NULL)
16073 template_args->push_back (arg);
16074 }
16075 else if (child_die->tag == DW_TAG_variant)
16076 {
16077 /* In a variant we want to get the discriminant and also add a
16078 field for our sole member child. */
16079 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16080
bde09ab7 16081 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
16082 variant_child != NULL;
16083 variant_child = sibling_die (variant_child))
16084 {
16085 if (variant_child->tag == DW_TAG_member)
16086 {
16087 handle_struct_member_die (variant_child, type, fi,
16088 template_args, cu);
16089 /* Only handle the one. */
16090 break;
16091 }
16092 }
16093
16094 /* We don't handle this but we might as well report it if we see
16095 it. */
16096 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 16097 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
16098 " - DIE at %s [in module %s]"),
16099 sect_offset_str (child_die->sect_off),
16100 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16101
16102 /* The first field was just added, so we can stash the
16103 discriminant there. */
be2daae6 16104 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16105 if (discr == NULL)
be2daae6 16106 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16107 else
be2daae6 16108 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16109 }
16110}
16111
c767944b
DJ
16112/* Finish creating a structure or union type, including filling in
16113 its members and creating a symbol for it. */
16114
16115static void
16116process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16117{
518817b3 16118 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16119 struct die_info *child_die;
c767944b
DJ
16120 struct type *type;
16121
16122 type = get_die_type (die, cu);
16123 if (type == NULL)
16124 type = read_structure_type (die, cu);
16125
2ddeaf8a
TT
16126 /* When reading a DW_TAG_variant_part, we need to notice when we
16127 read the discriminant member, so we can record it later in the
16128 discriminant_info. */
16129 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 16130 sect_offset discr_offset {};
3e1d3d8c 16131 bool has_template_parameters = false;
2ddeaf8a
TT
16132
16133 if (is_variant_part)
16134 {
16135 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16136 if (discr == NULL)
16137 {
16138 /* Maybe it's a univariant form, an extension we support.
16139 In this case arrange not to check the offset. */
16140 is_variant_part = false;
16141 }
16142 else if (attr_form_is_ref (discr))
16143 {
16144 struct dwarf2_cu *target_cu = cu;
16145 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16146
16147 discr_offset = target_die->sect_off;
16148 }
16149 else
16150 {
b98664d3 16151 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16152 " - DIE at %s [in module %s]"),
16153 sect_offset_str (die->sect_off),
16154 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16155 is_variant_part = false;
16156 }
16157 }
16158
e142c38c 16159 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16160 {
16161 struct field_info fi;
2f4732b0 16162 std::vector<struct symbol *> template_args;
c906108c 16163
639d11d3 16164 child_die = die->child;
c906108c
SS
16165
16166 while (child_die && child_die->tag)
16167 {
2ddeaf8a 16168 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16169
2ddeaf8a 16170 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16171 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16172
c906108c
SS
16173 child_die = sibling_die (child_die);
16174 }
16175
34eaf542 16176 /* Attach template arguments to type. */
2f4732b0 16177 if (!template_args.empty ())
34eaf542 16178 {
3e1d3d8c 16179 has_template_parameters = true;
34eaf542 16180 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16181 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16182 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16183 = XOBNEWVEC (&objfile->objfile_obstack,
16184 struct symbol *,
16185 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16186 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16187 template_args.data (),
34eaf542
TT
16188 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16189 * sizeof (struct symbol *)));
34eaf542
TT
16190 }
16191
c906108c
SS
16192 /* Attach fields and member functions to the type. */
16193 if (fi.nfields)
e7c27a73 16194 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16195 if (!fi.fnfieldlists.empty ())
c906108c 16196 {
e7c27a73 16197 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16198
c5aa993b 16199 /* Get the type which refers to the base class (possibly this
c906108c 16200 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16201 class from the DW_AT_containing_type attribute. This use of
16202 DW_AT_containing_type is a GNU extension. */
c906108c 16203
e142c38c 16204 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16205 {
e7c27a73 16206 struct type *t = die_containing_type (die, cu);
c906108c 16207
ae6ae975 16208 set_type_vptr_basetype (type, t);
c906108c
SS
16209 if (type == t)
16210 {
c906108c
SS
16211 int i;
16212
16213 /* Our own class provides vtbl ptr. */
16214 for (i = TYPE_NFIELDS (t) - 1;
16215 i >= TYPE_N_BASECLASSES (t);
16216 --i)
16217 {
0d5cff50 16218 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16219
1168df01 16220 if (is_vtable_name (fieldname, cu))
c906108c 16221 {
ae6ae975 16222 set_type_vptr_fieldno (type, i);
c906108c
SS
16223 break;
16224 }
16225 }
16226
16227 /* Complain if virtual function table field not found. */
16228 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16229 complaint (_("virtual function table pointer "
3e43a32a 16230 "not found when defining class '%s'"),
e86ca25f 16231 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16232 }
16233 else
16234 {
ae6ae975 16235 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16236 }
16237 }
f6235d4c 16238 else if (cu->producer
61012eef 16239 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16240 {
16241 /* The IBM XLC compiler does not provide direct indication
16242 of the containing type, but the vtable pointer is
16243 always named __vfp. */
16244
16245 int i;
16246
16247 for (i = TYPE_NFIELDS (type) - 1;
16248 i >= TYPE_N_BASECLASSES (type);
16249 --i)
16250 {
16251 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16252 {
ae6ae975
DE
16253 set_type_vptr_fieldno (type, i);
16254 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16255 break;
16256 }
16257 }
16258 }
c906108c 16259 }
98751a41
JK
16260
16261 /* Copy fi.typedef_field_list linked list elements content into the
16262 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16263 if (!fi.typedef_field_list.empty ())
98751a41 16264 {
be2daae6 16265 int count = fi.typedef_field_list.size ();
98751a41 16266
a0d7a4ff 16267 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16268 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16269 = ((struct decl_field *)
be2daae6
TT
16270 TYPE_ALLOC (type,
16271 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16272 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16273
be2daae6
TT
16274 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16275 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16276 }
c767944b 16277
883fd55a
KS
16278 /* Copy fi.nested_types_list linked list elements content into the
16279 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16280 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16281 {
be2daae6 16282 int count = fi.nested_types_list.size ();
883fd55a
KS
16283
16284 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16285 TYPE_NESTED_TYPES_ARRAY (type)
16286 = ((struct decl_field *)
be2daae6
TT
16287 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16288 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16289
be2daae6
TT
16290 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16291 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16292 }
c906108c 16293 }
63d06c5c 16294
bb5ed363 16295 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16296 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16297 cu->rust_unions.push_back (type);
0b92b5bb 16298
90aeadfc
DC
16299 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16300 snapshots) has been known to create a die giving a declaration
16301 for a class that has, as a child, a die giving a definition for a
16302 nested class. So we have to process our children even if the
16303 current die is a declaration. Normally, of course, a declaration
16304 won't have any children at all. */
134d01f1 16305
ca040673
DE
16306 child_die = die->child;
16307
90aeadfc
DC
16308 while (child_die != NULL && child_die->tag)
16309 {
16310 if (child_die->tag == DW_TAG_member
16311 || child_die->tag == DW_TAG_variable
34eaf542
TT
16312 || child_die->tag == DW_TAG_inheritance
16313 || child_die->tag == DW_TAG_template_value_param
16314 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16315 {
90aeadfc 16316 /* Do nothing. */
134d01f1 16317 }
90aeadfc
DC
16318 else
16319 process_die (child_die, cu);
134d01f1 16320
90aeadfc 16321 child_die = sibling_die (child_die);
134d01f1
DJ
16322 }
16323
fa4028e9
JB
16324 /* Do not consider external references. According to the DWARF standard,
16325 these DIEs are identified by the fact that they have no byte_size
16326 attribute, and a declaration attribute. */
16327 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16328 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16329 {
16330 struct symbol *sym = new_symbol (die, type, cu);
16331
16332 if (has_template_parameters)
16333 {
a776957c
TT
16334 struct symtab *symtab;
16335 if (sym != nullptr)
16336 symtab = symbol_symtab (sym);
16337 else if (cu->line_header != nullptr)
16338 {
16339 /* Any related symtab will do. */
16340 symtab
7ba99d21 16341 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16342 }
16343 else
16344 {
16345 symtab = nullptr;
16346 complaint (_("could not find suitable "
16347 "symtab for template parameter"
16348 " - DIE at %s [in module %s]"),
16349 sect_offset_str (die->sect_off),
16350 objfile_name (objfile));
16351 }
16352
16353 if (symtab != nullptr)
16354 {
16355 /* Make sure that the symtab is set on the new symbols.
16356 Even though they don't appear in this symtab directly,
16357 other parts of gdb assume that symbols do, and this is
16358 reasonably true. */
16359 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16360 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16361 }
3e1d3d8c
TT
16362 }
16363 }
134d01f1
DJ
16364}
16365
55426c9d
JB
16366/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16367 update TYPE using some information only available in DIE's children. */
16368
16369static void
16370update_enumeration_type_from_children (struct die_info *die,
16371 struct type *type,
16372 struct dwarf2_cu *cu)
16373{
60f7655a 16374 struct die_info *child_die;
55426c9d
JB
16375 int unsigned_enum = 1;
16376 int flag_enum = 1;
16377 ULONGEST mask = 0;
55426c9d 16378
8268c778 16379 auto_obstack obstack;
55426c9d 16380
60f7655a
DE
16381 for (child_die = die->child;
16382 child_die != NULL && child_die->tag;
16383 child_die = sibling_die (child_die))
55426c9d
JB
16384 {
16385 struct attribute *attr;
16386 LONGEST value;
16387 const gdb_byte *bytes;
16388 struct dwarf2_locexpr_baton *baton;
16389 const char *name;
60f7655a 16390
55426c9d
JB
16391 if (child_die->tag != DW_TAG_enumerator)
16392 continue;
16393
16394 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16395 if (attr == NULL)
16396 continue;
16397
16398 name = dwarf2_name (child_die, cu);
16399 if (name == NULL)
16400 name = "<anonymous enumerator>";
16401
16402 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16403 &value, &bytes, &baton);
16404 if (value < 0)
16405 {
16406 unsigned_enum = 0;
16407 flag_enum = 0;
16408 }
16409 else if ((mask & value) != 0)
16410 flag_enum = 0;
16411 else
16412 mask |= value;
16413
16414 /* If we already know that the enum type is neither unsigned, nor
16415 a flag type, no need to look at the rest of the enumerates. */
16416 if (!unsigned_enum && !flag_enum)
16417 break;
55426c9d
JB
16418 }
16419
16420 if (unsigned_enum)
16421 TYPE_UNSIGNED (type) = 1;
16422 if (flag_enum)
16423 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16424}
16425
134d01f1
DJ
16426/* Given a DW_AT_enumeration_type die, set its type. We do not
16427 complete the type's fields yet, or create any symbols. */
c906108c 16428
f792889a 16429static struct type *
134d01f1 16430read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16431{
518817b3 16432 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16433 struct type *type;
c906108c 16434 struct attribute *attr;
0114d602 16435 const char *name;
134d01f1 16436
348e048f
DE
16437 /* If the definition of this type lives in .debug_types, read that type.
16438 Don't follow DW_AT_specification though, that will take us back up
16439 the chain and we want to go down. */
45e58e77 16440 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16441 if (attr != nullptr)
348e048f 16442 {
ac9ec31b 16443 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16444
ac9ec31b 16445 /* The type's CU may not be the same as CU.
02142a6c 16446 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16447 return set_die_type (die, type, cu);
16448 }
16449
c906108c
SS
16450 type = alloc_type (objfile);
16451
16452 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16453 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16454 if (name != NULL)
e86ca25f 16455 TYPE_NAME (type) = name;
c906108c 16456
0626fc76
TT
16457 attr = dwarf2_attr (die, DW_AT_type, cu);
16458 if (attr != NULL)
16459 {
16460 struct type *underlying_type = die_type (die, cu);
16461
16462 TYPE_TARGET_TYPE (type) = underlying_type;
16463 }
16464
e142c38c 16465 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16466 if (attr != nullptr)
c906108c
SS
16467 {
16468 TYPE_LENGTH (type) = DW_UNSND (attr);
16469 }
16470 else
16471 {
16472 TYPE_LENGTH (type) = 0;
16473 }
16474
2b4424c3
TT
16475 maybe_set_alignment (cu, die, type);
16476
137033e9
JB
16477 /* The enumeration DIE can be incomplete. In Ada, any type can be
16478 declared as private in the package spec, and then defined only
16479 inside the package body. Such types are known as Taft Amendment
16480 Types. When another package uses such a type, an incomplete DIE
16481 may be generated by the compiler. */
02eb380e 16482 if (die_is_declaration (die, cu))
876cecd0 16483 TYPE_STUB (type) = 1;
02eb380e 16484
0626fc76
TT
16485 /* Finish the creation of this type by using the enum's children.
16486 We must call this even when the underlying type has been provided
16487 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16488 update_enumeration_type_from_children (die, type, cu);
16489
0626fc76
TT
16490 /* If this type has an underlying type that is not a stub, then we
16491 may use its attributes. We always use the "unsigned" attribute
16492 in this situation, because ordinarily we guess whether the type
16493 is unsigned -- but the guess can be wrong and the underlying type
16494 can tell us the reality. However, we defer to a local size
16495 attribute if one exists, because this lets the compiler override
16496 the underlying type if needed. */
16497 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16498 {
16499 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16500 if (TYPE_LENGTH (type) == 0)
16501 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16502 if (TYPE_RAW_ALIGN (type) == 0
16503 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16504 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16505 }
16506
3d567982
TT
16507 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16508
f792889a 16509 return set_die_type (die, type, cu);
134d01f1
DJ
16510}
16511
16512/* Given a pointer to a die which begins an enumeration, process all
16513 the dies that define the members of the enumeration, and create the
16514 symbol for the enumeration type.
16515
16516 NOTE: We reverse the order of the element list. */
16517
16518static void
16519process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16520{
f792889a 16521 struct type *this_type;
134d01f1 16522
f792889a
DJ
16523 this_type = get_die_type (die, cu);
16524 if (this_type == NULL)
16525 this_type = read_enumeration_type (die, cu);
9dc481d3 16526
639d11d3 16527 if (die->child != NULL)
c906108c 16528 {
9dc481d3
DE
16529 struct die_info *child_die;
16530 struct symbol *sym;
43816ebc 16531 std::vector<struct field> fields;
15d034d0 16532 const char *name;
9dc481d3 16533
639d11d3 16534 child_die = die->child;
c906108c
SS
16535 while (child_die && child_die->tag)
16536 {
16537 if (child_die->tag != DW_TAG_enumerator)
16538 {
e7c27a73 16539 process_die (child_die, cu);
c906108c
SS
16540 }
16541 else
16542 {
39cbfefa
DJ
16543 name = dwarf2_name (child_die, cu);
16544 if (name)
c906108c 16545 {
f792889a 16546 sym = new_symbol (child_die, this_type, cu);
c906108c 16547
43816ebc
TT
16548 fields.emplace_back ();
16549 struct field &field = fields.back ();
c906108c 16550
43816ebc
TT
16551 FIELD_NAME (field) = sym->linkage_name ();
16552 FIELD_TYPE (field) = NULL;
16553 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16554 FIELD_BITSIZE (field) = 0;
c906108c
SS
16555 }
16556 }
16557
16558 child_die = sibling_die (child_die);
16559 }
16560
43816ebc 16561 if (!fields.empty ())
c906108c 16562 {
43816ebc 16563 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 16564 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
16565 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16566 memcpy (TYPE_FIELDS (this_type), fields.data (),
16567 sizeof (struct field) * fields.size ());
c906108c 16568 }
c906108c 16569 }
134d01f1 16570
6c83ed52
TT
16571 /* If we are reading an enum from a .debug_types unit, and the enum
16572 is a declaration, and the enum is not the signatured type in the
16573 unit, then we do not want to add a symbol for it. Adding a
16574 symbol would in some cases obscure the true definition of the
16575 enum, giving users an incomplete type when the definition is
16576 actually available. Note that we do not want to do this for all
16577 enums which are just declarations, because C++0x allows forward
16578 enum declarations. */
3019eac3 16579 if (cu->per_cu->is_debug_types
6c83ed52
TT
16580 && die_is_declaration (die, cu))
16581 {
52dc124a 16582 struct signatured_type *sig_type;
6c83ed52 16583
c0f78cd4 16584 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16585 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16586 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16587 return;
16588 }
16589
f792889a 16590 new_symbol (die, this_type, cu);
c906108c
SS
16591}
16592
16593/* Extract all information from a DW_TAG_array_type DIE and put it in
16594 the DIE's type field. For now, this only handles one dimensional
16595 arrays. */
16596
f792889a 16597static struct type *
e7c27a73 16598read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16599{
518817b3 16600 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16601 struct die_info *child_die;
7e314c57 16602 struct type *type;
c906108c 16603 struct type *element_type, *range_type, *index_type;
c906108c 16604 struct attribute *attr;
15d034d0 16605 const char *name;
a405673c 16606 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16607 unsigned int bit_stride = 0;
c906108c 16608
e7c27a73 16609 element_type = die_type (die, cu);
c906108c 16610
7e314c57
JK
16611 /* The die_type call above may have already set the type for this DIE. */
16612 type = get_die_type (die, cu);
16613 if (type)
16614 return type;
16615
dc53a7ad
JB
16616 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16617 if (attr != NULL)
a405673c
JB
16618 {
16619 int stride_ok;
9a49df9d
AB
16620 struct type *prop_type
16621 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16622
16623 byte_stride_prop
16624 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16625 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16626 prop_type);
a405673c
JB
16627 if (!stride_ok)
16628 {
b98664d3 16629 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16630 " - DIE at %s [in module %s]"),
16631 sect_offset_str (die->sect_off),
518817b3 16632 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16633 /* Ignore this attribute. We will likely not be able to print
16634 arrays of this type correctly, but there is little we can do
16635 to help if we cannot read the attribute's value. */
16636 byte_stride_prop = NULL;
16637 }
16638 }
dc53a7ad
JB
16639
16640 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16641 if (attr != NULL)
16642 bit_stride = DW_UNSND (attr);
16643
c906108c
SS
16644 /* Irix 6.2 native cc creates array types without children for
16645 arrays with unspecified length. */
639d11d3 16646 if (die->child == NULL)
c906108c 16647 {
46bf5051 16648 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16649 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16650 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16651 byte_stride_prop, bit_stride);
f792889a 16652 return set_die_type (die, type, cu);
c906108c
SS
16653 }
16654
791afaa2 16655 std::vector<struct type *> range_types;
639d11d3 16656 child_die = die->child;
c906108c
SS
16657 while (child_die && child_die->tag)
16658 {
16659 if (child_die->tag == DW_TAG_subrange_type)
16660 {
f792889a 16661 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16662
f792889a 16663 if (child_type != NULL)
a02abb62 16664 {
0963b4bd
MS
16665 /* The range type was succesfully read. Save it for the
16666 array type creation. */
791afaa2 16667 range_types.push_back (child_type);
a02abb62 16668 }
c906108c
SS
16669 }
16670 child_die = sibling_die (child_die);
16671 }
16672
16673 /* Dwarf2 dimensions are output from left to right, create the
16674 necessary array types in backwards order. */
7ca2d3a3 16675
c906108c 16676 type = element_type;
7ca2d3a3
DL
16677
16678 if (read_array_order (die, cu) == DW_ORD_col_major)
16679 {
16680 int i = 0;
9a619af0 16681
791afaa2 16682 while (i < range_types.size ())
dc53a7ad 16683 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16684 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16685 }
16686 else
16687 {
791afaa2 16688 size_t ndim = range_types.size ();
7ca2d3a3 16689 while (ndim-- > 0)
dc53a7ad 16690 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16691 byte_stride_prop, bit_stride);
7ca2d3a3 16692 }
c906108c 16693
f5f8a009
EZ
16694 /* Understand Dwarf2 support for vector types (like they occur on
16695 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16696 array type. This is not part of the Dwarf2/3 standard yet, but a
16697 custom vendor extension. The main difference between a regular
16698 array and the vector variant is that vectors are passed by value
16699 to functions. */
e142c38c 16700 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16701 if (attr != nullptr)
ea37ba09 16702 make_vector_type (type);
f5f8a009 16703
dbc98a8b
KW
16704 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16705 implementation may choose to implement triple vectors using this
16706 attribute. */
16707 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16708 if (attr != nullptr)
dbc98a8b
KW
16709 {
16710 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16711 TYPE_LENGTH (type) = DW_UNSND (attr);
16712 else
b98664d3 16713 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16714 "than the total size of elements"));
dbc98a8b
KW
16715 }
16716
39cbfefa
DJ
16717 name = dwarf2_name (die, cu);
16718 if (name)
16719 TYPE_NAME (type) = name;
6e70227d 16720
2b4424c3
TT
16721 maybe_set_alignment (cu, die, type);
16722
0963b4bd 16723 /* Install the type in the die. */
7e314c57
JK
16724 set_die_type (die, type, cu);
16725
16726 /* set_die_type should be already done. */
b4ba55a1
JB
16727 set_descriptive_type (type, die, cu);
16728
7e314c57 16729 return type;
c906108c
SS
16730}
16731
7ca2d3a3 16732static enum dwarf_array_dim_ordering
6e70227d 16733read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16734{
16735 struct attribute *attr;
16736
16737 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16738
435d3d88 16739 if (attr != nullptr)
aead7601 16740 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16741
0963b4bd
MS
16742 /* GNU F77 is a special case, as at 08/2004 array type info is the
16743 opposite order to the dwarf2 specification, but data is still
16744 laid out as per normal fortran.
7ca2d3a3 16745
0963b4bd
MS
16746 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16747 version checking. */
7ca2d3a3 16748
905e0470
PM
16749 if (cu->language == language_fortran
16750 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16751 {
16752 return DW_ORD_row_major;
16753 }
16754
6e70227d 16755 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16756 {
16757 case array_column_major:
16758 return DW_ORD_col_major;
16759 case array_row_major:
16760 default:
16761 return DW_ORD_row_major;
16762 };
16763}
16764
72019c9c 16765/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16766 the DIE's type field. */
72019c9c 16767
f792889a 16768static struct type *
72019c9c
GM
16769read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16770{
7e314c57
JK
16771 struct type *domain_type, *set_type;
16772 struct attribute *attr;
f792889a 16773
7e314c57
JK
16774 domain_type = die_type (die, cu);
16775
16776 /* The die_type call above may have already set the type for this DIE. */
16777 set_type = get_die_type (die, cu);
16778 if (set_type)
16779 return set_type;
16780
16781 set_type = create_set_type (NULL, domain_type);
16782
16783 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16784 if (attr != nullptr)
d09039dd 16785 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16786
2b4424c3
TT
16787 maybe_set_alignment (cu, die, set_type);
16788
f792889a 16789 return set_die_type (die, set_type, cu);
72019c9c 16790}
7ca2d3a3 16791
0971de02
TT
16792/* A helper for read_common_block that creates a locexpr baton.
16793 SYM is the symbol which we are marking as computed.
16794 COMMON_DIE is the DIE for the common block.
16795 COMMON_LOC is the location expression attribute for the common
16796 block itself.
16797 MEMBER_LOC is the location expression attribute for the particular
16798 member of the common block that we are processing.
16799 CU is the CU from which the above come. */
16800
16801static void
16802mark_common_block_symbol_computed (struct symbol *sym,
16803 struct die_info *common_die,
16804 struct attribute *common_loc,
16805 struct attribute *member_loc,
16806 struct dwarf2_cu *cu)
16807{
518817b3
SM
16808 struct dwarf2_per_objfile *dwarf2_per_objfile
16809 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16810 struct objfile *objfile = dwarf2_per_objfile->objfile;
16811 struct dwarf2_locexpr_baton *baton;
16812 gdb_byte *ptr;
16813 unsigned int cu_off;
16814 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16815 LONGEST offset = 0;
16816
16817 gdb_assert (common_loc && member_loc);
16818 gdb_assert (attr_form_is_block (common_loc));
16819 gdb_assert (attr_form_is_block (member_loc)
16820 || attr_form_is_constant (member_loc));
16821
8d749320 16822 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16823 baton->per_cu = cu->per_cu;
16824 gdb_assert (baton->per_cu);
16825
16826 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16827
16828 if (attr_form_is_constant (member_loc))
16829 {
16830 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16831 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16832 }
16833 else
16834 baton->size += DW_BLOCK (member_loc)->size;
16835
224c3ddb 16836 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16837 baton->data = ptr;
16838
16839 *ptr++ = DW_OP_call4;
9c541725 16840 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16841 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16842 ptr += 4;
16843
16844 if (attr_form_is_constant (member_loc))
16845 {
16846 *ptr++ = DW_OP_addr;
16847 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16848 ptr += cu->header.addr_size;
16849 }
16850 else
16851 {
16852 /* We have to copy the data here, because DW_OP_call4 will only
16853 use a DW_AT_location attribute. */
16854 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16855 ptr += DW_BLOCK (member_loc)->size;
16856 }
16857
16858 *ptr++ = DW_OP_plus;
16859 gdb_assert (ptr - baton->data == baton->size);
16860
0971de02 16861 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16862 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16863}
16864
4357ac6c
TT
16865/* Create appropriate locally-scoped variables for all the
16866 DW_TAG_common_block entries. Also create a struct common_block
16867 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16868 is used to separate the common blocks name namespace from regular
4357ac6c 16869 variable names. */
c906108c
SS
16870
16871static void
e7c27a73 16872read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16873{
0971de02
TT
16874 struct attribute *attr;
16875
16876 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16877 if (attr != nullptr)
0971de02
TT
16878 {
16879 /* Support the .debug_loc offsets. */
16880 if (attr_form_is_block (attr))
16881 {
16882 /* Ok. */
16883 }
16884 else if (attr_form_is_section_offset (attr))
16885 {
16886 dwarf2_complex_location_expr_complaint ();
16887 attr = NULL;
16888 }
16889 else
16890 {
16891 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16892 "common block member");
16893 attr = NULL;
16894 }
16895 }
16896
639d11d3 16897 if (die->child != NULL)
c906108c 16898 {
518817b3 16899 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16900 struct die_info *child_die;
16901 size_t n_entries = 0, size;
16902 struct common_block *common_block;
16903 struct symbol *sym;
74ac6d43 16904
4357ac6c
TT
16905 for (child_die = die->child;
16906 child_die && child_die->tag;
16907 child_die = sibling_die (child_die))
16908 ++n_entries;
16909
16910 size = (sizeof (struct common_block)
16911 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16912 common_block
16913 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16914 size);
4357ac6c
TT
16915 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16916 common_block->n_entries = 0;
16917
16918 for (child_die = die->child;
16919 child_die && child_die->tag;
16920 child_die = sibling_die (child_die))
16921 {
16922 /* Create the symbol in the DW_TAG_common_block block in the current
16923 symbol scope. */
e7c27a73 16924 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16925 if (sym != NULL)
16926 {
16927 struct attribute *member_loc;
16928
16929 common_block->contents[common_block->n_entries++] = sym;
16930
16931 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16932 cu);
16933 if (member_loc)
16934 {
16935 /* GDB has handled this for a long time, but it is
16936 not specified by DWARF. It seems to have been
16937 emitted by gfortran at least as recently as:
16938 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16939 complaint (_("Variable in common block has "
0971de02 16940 "DW_AT_data_member_location "
9d8780f0
SM
16941 "- DIE at %s [in module %s]"),
16942 sect_offset_str (child_die->sect_off),
518817b3 16943 objfile_name (objfile));
0971de02
TT
16944
16945 if (attr_form_is_section_offset (member_loc))
16946 dwarf2_complex_location_expr_complaint ();
16947 else if (attr_form_is_constant (member_loc)
16948 || attr_form_is_block (member_loc))
16949 {
435d3d88 16950 if (attr != nullptr)
0971de02
TT
16951 mark_common_block_symbol_computed (sym, die, attr,
16952 member_loc, cu);
16953 }
16954 else
16955 dwarf2_complex_location_expr_complaint ();
16956 }
16957 }
c906108c 16958 }
4357ac6c
TT
16959
16960 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16961 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16962 }
16963}
16964
0114d602 16965/* Create a type for a C++ namespace. */
d9fa45fe 16966
0114d602
DJ
16967static struct type *
16968read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16969{
518817b3 16970 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16971 const char *previous_prefix, *name;
9219021c 16972 int is_anonymous;
0114d602
DJ
16973 struct type *type;
16974
16975 /* For extensions, reuse the type of the original namespace. */
16976 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16977 {
16978 struct die_info *ext_die;
16979 struct dwarf2_cu *ext_cu = cu;
9a619af0 16980
0114d602
DJ
16981 ext_die = dwarf2_extension (die, &ext_cu);
16982 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16983
16984 /* EXT_CU may not be the same as CU.
02142a6c 16985 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16986 return set_die_type (die, type, cu);
16987 }
9219021c 16988
e142c38c 16989 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16990
16991 /* Now build the name of the current namespace. */
16992
0114d602
DJ
16993 previous_prefix = determine_prefix (die, cu);
16994 if (previous_prefix[0] != '\0')
16995 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16996 previous_prefix, name, 0, cu);
0114d602
DJ
16997
16998 /* Create the type. */
19f392bc 16999 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17000
60531b24 17001 return set_die_type (die, type, cu);
0114d602
DJ
17002}
17003
22cee43f 17004/* Read a namespace scope. */
0114d602
DJ
17005
17006static void
17007read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17008{
518817b3 17009 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17010 int is_anonymous;
9219021c 17011
5c4e30ca
DC
17012 /* Add a symbol associated to this if we haven't seen the namespace
17013 before. Also, add a using directive if it's an anonymous
17014 namespace. */
9219021c 17015
f2f0e013 17016 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17017 {
17018 struct type *type;
17019
0114d602 17020 type = read_type_die (die, cu);
e7c27a73 17021 new_symbol (die, type, cu);
5c4e30ca 17022
e8e80198 17023 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17024 if (is_anonymous)
0114d602
DJ
17025 {
17026 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17027
eb1e02fd 17028 std::vector<const char *> excludes;
804d2729 17029 add_using_directive (using_directives (cu),
22cee43f 17030 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 17031 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17032 }
5c4e30ca 17033 }
9219021c 17034
639d11d3 17035 if (die->child != NULL)
d9fa45fe 17036 {
639d11d3 17037 struct die_info *child_die = die->child;
6e70227d 17038
d9fa45fe
DC
17039 while (child_die && child_die->tag)
17040 {
e7c27a73 17041 process_die (child_die, cu);
d9fa45fe
DC
17042 child_die = sibling_die (child_die);
17043 }
17044 }
38d518c9
EZ
17045}
17046
f55ee35c
JK
17047/* Read a Fortran module as type. This DIE can be only a declaration used for
17048 imported module. Still we need that type as local Fortran "use ... only"
17049 declaration imports depend on the created type in determine_prefix. */
17050
17051static struct type *
17052read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17053{
518817b3 17054 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 17055 const char *module_name;
f55ee35c
JK
17056 struct type *type;
17057
17058 module_name = dwarf2_name (die, cu);
19f392bc 17059 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17060
f55ee35c
JK
17061 return set_die_type (die, type, cu);
17062}
17063
5d7cb8df
JK
17064/* Read a Fortran module. */
17065
17066static void
17067read_module (struct die_info *die, struct dwarf2_cu *cu)
17068{
17069 struct die_info *child_die = die->child;
530e8392
KB
17070 struct type *type;
17071
17072 type = read_type_die (die, cu);
17073 new_symbol (die, type, cu);
5d7cb8df 17074
5d7cb8df
JK
17075 while (child_die && child_die->tag)
17076 {
17077 process_die (child_die, cu);
17078 child_die = sibling_die (child_die);
17079 }
17080}
17081
38d518c9
EZ
17082/* Return the name of the namespace represented by DIE. Set
17083 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17084 namespace. */
17085
17086static const char *
e142c38c 17087namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17088{
17089 struct die_info *current_die;
17090 const char *name = NULL;
17091
17092 /* Loop through the extensions until we find a name. */
17093
17094 for (current_die = die;
17095 current_die != NULL;
f2f0e013 17096 current_die = dwarf2_extension (die, &cu))
38d518c9 17097 {
96553a0c
DE
17098 /* We don't use dwarf2_name here so that we can detect the absence
17099 of a name -> anonymous namespace. */
7d45c7c3 17100 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17101
38d518c9
EZ
17102 if (name != NULL)
17103 break;
17104 }
17105
17106 /* Is it an anonymous namespace? */
17107
17108 *is_anonymous = (name == NULL);
17109 if (*is_anonymous)
2b1dbab0 17110 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17111
17112 return name;
d9fa45fe
DC
17113}
17114
c906108c
SS
17115/* Extract all information from a DW_TAG_pointer_type DIE and add to
17116 the user defined type vector. */
17117
f792889a 17118static struct type *
e7c27a73 17119read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17120{
518817b3
SM
17121 struct gdbarch *gdbarch
17122 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17123 struct comp_unit_head *cu_header = &cu->header;
c906108c 17124 struct type *type;
8b2dbe47
KB
17125 struct attribute *attr_byte_size;
17126 struct attribute *attr_address_class;
17127 int byte_size, addr_class;
7e314c57
JK
17128 struct type *target_type;
17129
17130 target_type = die_type (die, cu);
c906108c 17131
7e314c57
JK
17132 /* The die_type call above may have already set the type for this DIE. */
17133 type = get_die_type (die, cu);
17134 if (type)
17135 return type;
17136
17137 type = lookup_pointer_type (target_type);
8b2dbe47 17138
e142c38c 17139 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17140 if (attr_byte_size)
17141 byte_size = DW_UNSND (attr_byte_size);
c906108c 17142 else
8b2dbe47
KB
17143 byte_size = cu_header->addr_size;
17144
e142c38c 17145 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17146 if (attr_address_class)
17147 addr_class = DW_UNSND (attr_address_class);
17148 else
17149 addr_class = DW_ADDR_none;
17150
2b4424c3
TT
17151 ULONGEST alignment = get_alignment (cu, die);
17152
17153 /* If the pointer size, alignment, or address class is different
17154 than the default, create a type variant marked as such and set
17155 the length accordingly. */
17156 if (TYPE_LENGTH (type) != byte_size
17157 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17158 && alignment != TYPE_RAW_ALIGN (type))
17159 || addr_class != DW_ADDR_none)
c906108c 17160 {
5e2b427d 17161 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17162 {
17163 int type_flags;
17164
849957d9 17165 type_flags = gdbarch_address_class_type_flags
5e2b427d 17166 (gdbarch, byte_size, addr_class);
876cecd0
TT
17167 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17168 == 0);
8b2dbe47
KB
17169 type = make_type_with_address_space (type, type_flags);
17170 }
17171 else if (TYPE_LENGTH (type) != byte_size)
17172 {
b98664d3 17173 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17174 }
2b4424c3
TT
17175 else if (TYPE_RAW_ALIGN (type) != alignment)
17176 {
b98664d3 17177 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17178 " - DIE at %s [in module %s]"),
17179 sect_offset_str (die->sect_off),
17180 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17181 }
6e70227d 17182 else
9a619af0
MS
17183 {
17184 /* Should we also complain about unhandled address classes? */
17185 }
c906108c 17186 }
8b2dbe47
KB
17187
17188 TYPE_LENGTH (type) = byte_size;
2b4424c3 17189 set_type_align (type, alignment);
f792889a 17190 return set_die_type (die, type, cu);
c906108c
SS
17191}
17192
17193/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17194 the user defined type vector. */
17195
f792889a 17196static struct type *
e7c27a73 17197read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17198{
17199 struct type *type;
17200 struct type *to_type;
17201 struct type *domain;
17202
e7c27a73
DJ
17203 to_type = die_type (die, cu);
17204 domain = die_containing_type (die, cu);
0d5de010 17205
7e314c57
JK
17206 /* The calls above may have already set the type for this DIE. */
17207 type = get_die_type (die, cu);
17208 if (type)
17209 return type;
17210
0d5de010
DJ
17211 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17212 type = lookup_methodptr_type (to_type);
7078baeb
TT
17213 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17214 {
518817b3
SM
17215 struct type *new_type
17216 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17217
17218 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17219 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17220 TYPE_VARARGS (to_type));
17221 type = lookup_methodptr_type (new_type);
17222 }
0d5de010
DJ
17223 else
17224 type = lookup_memberptr_type (to_type, domain);
c906108c 17225
f792889a 17226 return set_die_type (die, type, cu);
c906108c
SS
17227}
17228
4297a3f0 17229/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17230 the user defined type vector. */
17231
f792889a 17232static struct type *
4297a3f0
AV
17233read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17234 enum type_code refcode)
c906108c 17235{
e7c27a73 17236 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17237 struct type *type, *target_type;
c906108c
SS
17238 struct attribute *attr;
17239
4297a3f0
AV
17240 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17241
7e314c57
JK
17242 target_type = die_type (die, cu);
17243
17244 /* The die_type call above may have already set the type for this DIE. */
17245 type = get_die_type (die, cu);
17246 if (type)
17247 return type;
17248
4297a3f0 17249 type = lookup_reference_type (target_type, refcode);
e142c38c 17250 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17251 if (attr != nullptr)
c906108c
SS
17252 {
17253 TYPE_LENGTH (type) = DW_UNSND (attr);
17254 }
17255 else
17256 {
107d2387 17257 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17258 }
2b4424c3 17259 maybe_set_alignment (cu, die, type);
f792889a 17260 return set_die_type (die, type, cu);
c906108c
SS
17261}
17262
cf363f18
MW
17263/* Add the given cv-qualifiers to the element type of the array. GCC
17264 outputs DWARF type qualifiers that apply to an array, not the
17265 element type. But GDB relies on the array element type to carry
17266 the cv-qualifiers. This mimics section 6.7.3 of the C99
17267 specification. */
17268
17269static struct type *
17270add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17271 struct type *base_type, int cnst, int voltl)
17272{
17273 struct type *el_type, *inner_array;
17274
17275 base_type = copy_type (base_type);
17276 inner_array = base_type;
17277
17278 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17279 {
17280 TYPE_TARGET_TYPE (inner_array) =
17281 copy_type (TYPE_TARGET_TYPE (inner_array));
17282 inner_array = TYPE_TARGET_TYPE (inner_array);
17283 }
17284
17285 el_type = TYPE_TARGET_TYPE (inner_array);
17286 cnst |= TYPE_CONST (el_type);
17287 voltl |= TYPE_VOLATILE (el_type);
17288 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17289
17290 return set_die_type (die, base_type, cu);
17291}
17292
f792889a 17293static struct type *
e7c27a73 17294read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17295{
f792889a 17296 struct type *base_type, *cv_type;
c906108c 17297
e7c27a73 17298 base_type = die_type (die, cu);
7e314c57
JK
17299
17300 /* The die_type call above may have already set the type for this DIE. */
17301 cv_type = get_die_type (die, cu);
17302 if (cv_type)
17303 return cv_type;
17304
2f608a3a
KW
17305 /* In case the const qualifier is applied to an array type, the element type
17306 is so qualified, not the array type (section 6.7.3 of C99). */
17307 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17308 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17309
f792889a
DJ
17310 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17311 return set_die_type (die, cv_type, cu);
c906108c
SS
17312}
17313
f792889a 17314static struct type *
e7c27a73 17315read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17316{
f792889a 17317 struct type *base_type, *cv_type;
c906108c 17318
e7c27a73 17319 base_type = die_type (die, cu);
7e314c57
JK
17320
17321 /* The die_type call above may have already set the type for this DIE. */
17322 cv_type = get_die_type (die, cu);
17323 if (cv_type)
17324 return cv_type;
17325
cf363f18
MW
17326 /* In case the volatile qualifier is applied to an array type, the
17327 element type is so qualified, not the array type (section 6.7.3
17328 of C99). */
17329 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17330 return add_array_cv_type (die, cu, base_type, 0, 1);
17331
f792889a
DJ
17332 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17333 return set_die_type (die, cv_type, cu);
c906108c
SS
17334}
17335
06d66ee9
TT
17336/* Handle DW_TAG_restrict_type. */
17337
17338static struct type *
17339read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17340{
17341 struct type *base_type, *cv_type;
17342
17343 base_type = die_type (die, cu);
17344
17345 /* The die_type call above may have already set the type for this DIE. */
17346 cv_type = get_die_type (die, cu);
17347 if (cv_type)
17348 return cv_type;
17349
17350 cv_type = make_restrict_type (base_type);
17351 return set_die_type (die, cv_type, cu);
17352}
17353
a2c2acaf
MW
17354/* Handle DW_TAG_atomic_type. */
17355
17356static struct type *
17357read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17358{
17359 struct type *base_type, *cv_type;
17360
17361 base_type = die_type (die, cu);
17362
17363 /* The die_type call above may have already set the type for this DIE. */
17364 cv_type = get_die_type (die, cu);
17365 if (cv_type)
17366 return cv_type;
17367
17368 cv_type = make_atomic_type (base_type);
17369 return set_die_type (die, cv_type, cu);
17370}
17371
c906108c
SS
17372/* Extract all information from a DW_TAG_string_type DIE and add to
17373 the user defined type vector. It isn't really a user defined type,
17374 but it behaves like one, with other DIE's using an AT_user_def_type
17375 attribute to reference it. */
17376
f792889a 17377static struct type *
e7c27a73 17378read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17379{
518817b3 17380 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17381 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17382 struct type *type, *range_type, *index_type, *char_type;
17383 struct attribute *attr;
216a7e6b
AB
17384 struct dynamic_prop prop;
17385 bool length_is_constant = true;
17386 LONGEST length;
17387
17388 /* There are a couple of places where bit sizes might be made use of
17389 when parsing a DW_TAG_string_type, however, no producer that we know
17390 of make use of these. Handling bit sizes that are a multiple of the
17391 byte size is easy enough, but what about other bit sizes? Lets deal
17392 with that problem when we have to. Warn about these attributes being
17393 unsupported, then parse the type and ignore them like we always
17394 have. */
17395 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17396 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17397 {
17398 static bool warning_printed = false;
17399 if (!warning_printed)
17400 {
17401 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17402 "currently supported on DW_TAG_string_type."));
17403 warning_printed = true;
17404 }
17405 }
c906108c 17406
e142c38c 17407 attr = dwarf2_attr (die, DW_AT_string_length, cu);
216a7e6b
AB
17408 if (attr != nullptr && !attr_form_is_constant (attr))
17409 {
17410 /* The string length describes the location at which the length of
17411 the string can be found. The size of the length field can be
17412 specified with one of the attributes below. */
17413 struct type *prop_type;
17414 struct attribute *len
17415 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17416 if (len == nullptr)
17417 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17418 if (len != nullptr && attr_form_is_constant (len))
17419 {
17420 /* Pass 0 as the default as we know this attribute is constant
17421 and the default value will not be returned. */
17422 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17423 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17424 }
17425 else
17426 {
17427 /* If the size is not specified then we assume it is the size of
17428 an address on this target. */
17429 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17430 }
17431
17432 /* Convert the attribute into a dynamic property. */
17433 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17434 length = 1;
17435 else
17436 length_is_constant = false;
17437 }
17438 else if (attr != nullptr)
17439 {
17440 /* This DW_AT_string_length just contains the length with no
17441 indirection. There's no need to create a dynamic property in this
17442 case. Pass 0 for the default value as we know it will not be
17443 returned in this case. */
17444 length = dwarf2_get_attr_constant_value (attr, 0);
17445 }
17446 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17447 {
216a7e6b
AB
17448 /* We don't currently support non-constant byte sizes for strings. */
17449 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17450 }
17451 else
17452 {
216a7e6b
AB
17453 /* Use 1 as a fallback length if we have nothing else. */
17454 length = 1;
c906108c 17455 }
6ccb9162 17456
46bf5051 17457 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17458 if (length_is_constant)
17459 range_type = create_static_range_type (NULL, index_type, 1, length);
17460 else
17461 {
17462 struct dynamic_prop low_bound;
17463
17464 low_bound.kind = PROP_CONST;
17465 low_bound.data.const_val = 1;
17466 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17467 }
3b7538c0
UW
17468 char_type = language_string_char_type (cu->language_defn, gdbarch);
17469 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17470
f792889a 17471 return set_die_type (die, type, cu);
c906108c
SS
17472}
17473
4d804846
JB
17474/* Assuming that DIE corresponds to a function, returns nonzero
17475 if the function is prototyped. */
17476
17477static int
17478prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17479{
17480 struct attribute *attr;
17481
17482 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17483 if (attr && (DW_UNSND (attr) != 0))
17484 return 1;
17485
17486 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17487 is only meaningful for C, but the concept also extends to other
4d804846
JB
17488 languages that allow unprototyped functions (Eg: Objective C).
17489 For all other languages, assume that functions are always
17490 prototyped. */
17491 if (cu->language != language_c
17492 && cu->language != language_objc
17493 && cu->language != language_opencl)
17494 return 1;
17495
17496 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17497 prototyped and unprototyped functions; default to prototyped,
17498 since that is more common in modern code (and RealView warns
17499 about unprototyped functions). */
17500 if (producer_is_realview (cu->producer))
17501 return 1;
17502
17503 return 0;
17504}
17505
c906108c
SS
17506/* Handle DIES due to C code like:
17507
17508 struct foo
c5aa993b
JM
17509 {
17510 int (*funcp)(int a, long l);
17511 int b;
17512 };
c906108c 17513
0963b4bd 17514 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17515
f792889a 17516static struct type *
e7c27a73 17517read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17518{
518817b3 17519 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17520 struct type *type; /* Type that this function returns. */
17521 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17522 struct attribute *attr;
17523
e7c27a73 17524 type = die_type (die, cu);
7e314c57
JK
17525
17526 /* The die_type call above may have already set the type for this DIE. */
17527 ftype = get_die_type (die, cu);
17528 if (ftype)
17529 return ftype;
17530
0c8b41f1 17531 ftype = lookup_function_type (type);
c906108c 17532
4d804846 17533 if (prototyped_function_p (die, cu))
a6c727b2 17534 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17535
c055b101
CV
17536 /* Store the calling convention in the type if it's available in
17537 the subroutine die. Otherwise set the calling convention to
17538 the default value DW_CC_normal. */
17539 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17540 if (attr != nullptr
17541 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17542 TYPE_CALLING_CONVENTION (ftype)
17543 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17544 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17545 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17546 else
17547 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17548
743649fd
MW
17549 /* Record whether the function returns normally to its caller or not
17550 if the DWARF producer set that information. */
17551 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17552 if (attr && (DW_UNSND (attr) != 0))
17553 TYPE_NO_RETURN (ftype) = 1;
17554
76c10ea2
GM
17555 /* We need to add the subroutine type to the die immediately so
17556 we don't infinitely recurse when dealing with parameters
0963b4bd 17557 declared as the same subroutine type. */
76c10ea2 17558 set_die_type (die, ftype, cu);
6e70227d 17559
639d11d3 17560 if (die->child != NULL)
c906108c 17561 {
bb5ed363 17562 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17563 struct die_info *child_die;
8072405b 17564 int nparams, iparams;
c906108c
SS
17565
17566 /* Count the number of parameters.
17567 FIXME: GDB currently ignores vararg functions, but knows about
17568 vararg member functions. */
8072405b 17569 nparams = 0;
639d11d3 17570 child_die = die->child;
c906108c
SS
17571 while (child_die && child_die->tag)
17572 {
17573 if (child_die->tag == DW_TAG_formal_parameter)
17574 nparams++;
17575 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17576 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17577 child_die = sibling_die (child_die);
17578 }
17579
17580 /* Allocate storage for parameters and fill them in. */
17581 TYPE_NFIELDS (ftype) = nparams;
17582 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17583 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17584
8072405b
JK
17585 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17586 even if we error out during the parameters reading below. */
17587 for (iparams = 0; iparams < nparams; iparams++)
17588 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17589
17590 iparams = 0;
639d11d3 17591 child_die = die->child;
c906108c
SS
17592 while (child_die && child_die->tag)
17593 {
17594 if (child_die->tag == DW_TAG_formal_parameter)
17595 {
3ce3b1ba
PA
17596 struct type *arg_type;
17597
17598 /* DWARF version 2 has no clean way to discern C++
17599 static and non-static member functions. G++ helps
17600 GDB by marking the first parameter for non-static
17601 member functions (which is the this pointer) as
17602 artificial. We pass this information to
17603 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17604
17605 DWARF version 3 added DW_AT_object_pointer, which GCC
17606 4.5 does not yet generate. */
e142c38c 17607 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17608 if (attr != nullptr)
c906108c
SS
17609 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17610 else
9c37b5ae 17611 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17612 arg_type = die_type (child_die, cu);
17613
17614 /* RealView does not mark THIS as const, which the testsuite
17615 expects. GCC marks THIS as const in method definitions,
17616 but not in the class specifications (GCC PR 43053). */
17617 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17618 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17619 {
17620 int is_this = 0;
17621 struct dwarf2_cu *arg_cu = cu;
17622 const char *name = dwarf2_name (child_die, cu);
17623
17624 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17625 if (attr != nullptr)
3ce3b1ba
PA
17626 {
17627 /* If the compiler emits this, use it. */
17628 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17629 is_this = 1;
17630 }
17631 else if (name && strcmp (name, "this") == 0)
17632 /* Function definitions will have the argument names. */
17633 is_this = 1;
17634 else if (name == NULL && iparams == 0)
17635 /* Declarations may not have the names, so like
17636 elsewhere in GDB, assume an artificial first
17637 argument is "this". */
17638 is_this = 1;
17639
17640 if (is_this)
17641 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17642 arg_type, 0);
17643 }
17644
17645 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17646 iparams++;
17647 }
17648 child_die = sibling_die (child_die);
17649 }
17650 }
17651
76c10ea2 17652 return ftype;
c906108c
SS
17653}
17654
f792889a 17655static struct type *
e7c27a73 17656read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17657{
518817b3 17658 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17659 const char *name = NULL;
3c8e0968 17660 struct type *this_type, *target_type;
c906108c 17661
94af9270 17662 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17663 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17664 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17665 set_die_type (die, this_type, cu);
3c8e0968
DE
17666 target_type = die_type (die, cu);
17667 if (target_type != this_type)
17668 TYPE_TARGET_TYPE (this_type) = target_type;
17669 else
17670 {
17671 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17672 spec and cause infinite loops in GDB. */
b98664d3 17673 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17674 "- DIE at %s [in module %s]"),
17675 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17676 TYPE_TARGET_TYPE (this_type) = NULL;
17677 }
f792889a 17678 return this_type;
c906108c
SS
17679}
17680
9b790ce7
UW
17681/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17682 (which may be different from NAME) to the architecture back-end to allow
17683 it to guess the correct format if necessary. */
17684
17685static struct type *
17686dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17687 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17688{
17689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17690 const struct floatformat **format;
17691 struct type *type;
17692
17693 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17694 if (format)
103a685e 17695 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17696 else
77b7c781 17697 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17698
17699 return type;
17700}
17701
eb77c9df
AB
17702/* Allocate an integer type of size BITS and name NAME. */
17703
17704static struct type *
17705dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17706 int bits, int unsigned_p, const char *name)
17707{
17708 struct type *type;
17709
17710 /* Versions of Intel's C Compiler generate an integer type called "void"
17711 instead of using DW_TAG_unspecified_type. This has been seen on
17712 at least versions 14, 17, and 18. */
35ee2dc2
AB
17713 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17714 && strcmp (name, "void") == 0)
eb77c9df
AB
17715 type = objfile_type (objfile)->builtin_void;
17716 else
17717 type = init_integer_type (objfile, bits, unsigned_p, name);
17718
17719 return type;
17720}
17721
8bdc1658
AB
17722/* Initialise and return a floating point type of size BITS suitable for
17723 use as a component of a complex number. The NAME_HINT is passed through
17724 when initialising the floating point type and is the name of the complex
17725 type.
17726
17727 As DWARF doesn't currently provide an explicit name for the components
17728 of a complex number, but it can be helpful to have these components
17729 named, we try to select a suitable name based on the size of the
17730 component. */
17731static struct type *
17732dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17733 struct objfile *objfile,
103a685e
TT
17734 int bits, const char *name_hint,
17735 enum bfd_endian byte_order)
8bdc1658
AB
17736{
17737 gdbarch *gdbarch = get_objfile_arch (objfile);
17738 struct type *tt = nullptr;
17739
35add35e
AB
17740 /* Try to find a suitable floating point builtin type of size BITS.
17741 We're going to use the name of this type as the name for the complex
17742 target type that we are about to create. */
1db455a7 17743 switch (cu->language)
8bdc1658 17744 {
1db455a7
AB
17745 case language_fortran:
17746 switch (bits)
17747 {
17748 case 32:
17749 tt = builtin_f_type (gdbarch)->builtin_real;
17750 break;
17751 case 64:
17752 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17753 break;
17754 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17755 case 128:
17756 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17757 break;
17758 }
8bdc1658 17759 break;
1db455a7
AB
17760 default:
17761 switch (bits)
17762 {
17763 case 32:
17764 tt = builtin_type (gdbarch)->builtin_float;
17765 break;
17766 case 64:
17767 tt = builtin_type (gdbarch)->builtin_double;
17768 break;
17769 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17770 case 128:
17771 tt = builtin_type (gdbarch)->builtin_long_double;
17772 break;
17773 }
8bdc1658
AB
17774 break;
17775 }
17776
35add35e
AB
17777 /* If the type we found doesn't match the size we were looking for, then
17778 pretend we didn't find a type at all, the complex target type we
17779 create will then be nameless. */
a12e5744 17780 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17781 tt = nullptr;
17782
8bdc1658 17783 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17784 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17785}
17786
c906108c
SS
17787/* Find a representation of a given base type and install
17788 it in the TYPE field of the die. */
17789
f792889a 17790static struct type *
e7c27a73 17791read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17792{
518817b3 17793 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17794 struct type *type;
17795 struct attribute *attr;
19f392bc 17796 int encoding = 0, bits = 0;
15d034d0 17797 const char *name;
34877895 17798 gdbarch *arch;
c906108c 17799
e142c38c 17800 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17801 if (attr != nullptr)
34877895 17802 encoding = DW_UNSND (attr);
e142c38c 17803 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17804 if (attr != nullptr)
34877895 17805 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17806 name = dwarf2_name (die, cu);
6ccb9162 17807 if (!name)
34877895 17808 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17809
17810 arch = get_objfile_arch (objfile);
17811 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17812
34877895
PJ
17813 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17814 if (attr)
103a685e
TT
17815 {
17816 int endianity = DW_UNSND (attr);
17817
17818 switch (endianity)
17819 {
17820 case DW_END_big:
17821 byte_order = BFD_ENDIAN_BIG;
17822 break;
17823 case DW_END_little:
17824 byte_order = BFD_ENDIAN_LITTLE;
17825 break;
17826 default:
17827 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17828 break;
17829 }
17830 }
6ccb9162
UW
17831
17832 switch (encoding)
c906108c 17833 {
6ccb9162
UW
17834 case DW_ATE_address:
17835 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17836 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17837 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17838 break;
17839 case DW_ATE_boolean:
19f392bc 17840 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17841 break;
17842 case DW_ATE_complex_float:
103a685e
TT
17843 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17844 byte_order);
19f392bc 17845 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17846 break;
17847 case DW_ATE_decimal_float:
19f392bc 17848 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17849 break;
17850 case DW_ATE_float:
103a685e 17851 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17852 break;
17853 case DW_ATE_signed:
eb77c9df 17854 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17855 break;
17856 case DW_ATE_unsigned:
3b2b8fea
TT
17857 if (cu->language == language_fortran
17858 && name
61012eef 17859 && startswith (name, "character("))
19f392bc
UW
17860 type = init_character_type (objfile, bits, 1, name);
17861 else
eb77c9df 17862 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17863 break;
17864 case DW_ATE_signed_char:
6e70227d 17865 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17866 || cu->language == language_pascal
17867 || cu->language == language_fortran)
19f392bc
UW
17868 type = init_character_type (objfile, bits, 0, name);
17869 else
eb77c9df 17870 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17871 break;
17872 case DW_ATE_unsigned_char:
868a0084 17873 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17874 || cu->language == language_pascal
c44af4eb
TT
17875 || cu->language == language_fortran
17876 || cu->language == language_rust)
19f392bc
UW
17877 type = init_character_type (objfile, bits, 1, name);
17878 else
eb77c9df 17879 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17880 break;
75079b2b 17881 case DW_ATE_UTF:
53e710ac 17882 {
53e710ac
PA
17883 if (bits == 16)
17884 type = builtin_type (arch)->builtin_char16;
17885 else if (bits == 32)
17886 type = builtin_type (arch)->builtin_char32;
17887 else
17888 {
b98664d3 17889 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17890 bits);
eb77c9df 17891 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17892 }
17893 return set_die_type (die, type, cu);
17894 }
75079b2b
TT
17895 break;
17896
6ccb9162 17897 default:
b98664d3 17898 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17899 dwarf_type_encoding_name (encoding));
77b7c781 17900 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17901 break;
c906108c 17902 }
6ccb9162 17903
0114d602 17904 if (name && strcmp (name, "char") == 0)
876cecd0 17905 TYPE_NOSIGN (type) = 1;
0114d602 17906
2b4424c3
TT
17907 maybe_set_alignment (cu, die, type);
17908
103a685e 17909 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17910
f792889a 17911 return set_die_type (die, type, cu);
c906108c
SS
17912}
17913
80180f79
SA
17914/* Parse dwarf attribute if it's a block, reference or constant and put the
17915 resulting value of the attribute into struct bound_prop.
17916 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17917
17918static int
17919attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17920 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17921 struct type *default_type)
80180f79
SA
17922{
17923 struct dwarf2_property_baton *baton;
518817b3
SM
17924 struct obstack *obstack
17925 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17926
9a49df9d
AB
17927 gdb_assert (default_type != NULL);
17928
80180f79
SA
17929 if (attr == NULL || prop == NULL)
17930 return 0;
17931
17932 if (attr_form_is_block (attr))
17933 {
8d749320 17934 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17935 baton->property_type = default_type;
80180f79
SA
17936 baton->locexpr.per_cu = cu->per_cu;
17937 baton->locexpr.size = DW_BLOCK (attr)->size;
17938 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17939 switch (attr->name)
17940 {
17941 case DW_AT_string_length:
17942 baton->locexpr.is_reference = true;
17943 break;
17944 default:
17945 baton->locexpr.is_reference = false;
17946 break;
17947 }
80180f79
SA
17948 prop->data.baton = baton;
17949 prop->kind = PROP_LOCEXPR;
17950 gdb_assert (prop->data.baton != NULL);
17951 }
17952 else if (attr_form_is_ref (attr))
17953 {
17954 struct dwarf2_cu *target_cu = cu;
17955 struct die_info *target_die;
17956 struct attribute *target_attr;
17957
17958 target_die = follow_die_ref (die, attr, &target_cu);
17959 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17960 if (target_attr == NULL)
17961 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17962 target_cu);
80180f79
SA
17963 if (target_attr == NULL)
17964 return 0;
17965
df25ebbd 17966 switch (target_attr->name)
80180f79 17967 {
df25ebbd
JB
17968 case DW_AT_location:
17969 if (attr_form_is_section_offset (target_attr))
17970 {
8d749320 17971 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17972 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17973 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17974 prop->data.baton = baton;
17975 prop->kind = PROP_LOCLIST;
17976 gdb_assert (prop->data.baton != NULL);
17977 }
17978 else if (attr_form_is_block (target_attr))
17979 {
8d749320 17980 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17981 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17982 baton->locexpr.per_cu = cu->per_cu;
17983 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17984 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17985 baton->locexpr.is_reference = true;
df25ebbd
JB
17986 prop->data.baton = baton;
17987 prop->kind = PROP_LOCEXPR;
17988 gdb_assert (prop->data.baton != NULL);
17989 }
17990 else
17991 {
17992 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17993 "dynamic property");
17994 return 0;
17995 }
17996 break;
17997 case DW_AT_data_member_location:
17998 {
17999 LONGEST offset;
18000
18001 if (!handle_data_member_location (target_die, target_cu,
18002 &offset))
18003 return 0;
18004
8d749320 18005 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18006 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18007 target_cu);
df25ebbd
JB
18008 baton->offset_info.offset = offset;
18009 baton->offset_info.type = die_type (target_die, target_cu);
18010 prop->data.baton = baton;
18011 prop->kind = PROP_ADDR_OFFSET;
18012 break;
18013 }
80180f79
SA
18014 }
18015 }
18016 else if (attr_form_is_constant (attr))
18017 {
18018 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18019 prop->kind = PROP_CONST;
18020 }
18021 else
18022 {
18023 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18024 dwarf2_name (die, cu));
18025 return 0;
18026 }
18027
18028 return 1;
18029}
18030
11a8b164
AB
18031/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18032 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
18033
18034static struct type *
11a8b164
AB
18035dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18036 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
18037{
18038 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
18039 struct type *int_type;
18040
18041 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18042#define TRY_TYPE(F) \
18043 int_type = (unsigned_p \
18044 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18045 : objfile_type (objfile)->builtin_ ## F); \
18046 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18047 return int_type
18048
18049 TRY_TYPE (char);
18050 TRY_TYPE (short);
18051 TRY_TYPE (int);
18052 TRY_TYPE (long);
18053 TRY_TYPE (long_long);
18054
18055#undef TRY_TYPE
18056
18057 gdb_assert_not_reached ("unable to find suitable integer type");
18058}
18059
11a8b164
AB
18060/* Find an integer type the same size as the address size given in the
18061 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18062 is unsigned or not. */
18063
18064static struct type *
18065dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18066 bool unsigned_p)
18067{
18068 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18069 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18070}
18071
b86352cf
AB
18072/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18073 present (which is valid) then compute the default type based on the
18074 compilation units address size. */
18075
18076static struct type *
18077read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18078{
18079 struct type *index_type = die_type (die, cu);
18080
18081 /* Dwarf-2 specifications explicitly allows to create subrange types
18082 without specifying a base type.
18083 In that case, the base type must be set to the type of
18084 the lower bound, upper bound or count, in that order, if any of these
18085 three attributes references an object that has a type.
18086 If no base type is found, the Dwarf-2 specifications say that
18087 a signed integer type of size equal to the size of an address should
18088 be used.
18089 For the following C code: `extern char gdb_int [];'
18090 GCC produces an empty range DIE.
18091 FIXME: muller/2010-05-28: Possible references to object for low bound,
18092 high bound or count are not yet handled by this code. */
18093 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 18094 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
18095
18096 return index_type;
18097}
18098
a02abb62
JB
18099/* Read the given DW_AT_subrange DIE. */
18100
f792889a 18101static struct type *
a02abb62
JB
18102read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18103{
4c9ad8c2 18104 struct type *base_type, *orig_base_type;
a02abb62
JB
18105 struct type *range_type;
18106 struct attribute *attr;
729efb13 18107 struct dynamic_prop low, high;
4fae6e18 18108 int low_default_is_valid;
c451ebe5 18109 int high_bound_is_count = 0;
15d034d0 18110 const char *name;
d359392f 18111 ULONGEST negative_mask;
e77813c8 18112
b86352cf
AB
18113 orig_base_type = read_subrange_index_type (die, cu);
18114
4c9ad8c2
TT
18115 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18116 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18117 creating the range type, but we use the result of check_typedef
18118 when examining properties of the type. */
18119 base_type = check_typedef (orig_base_type);
a02abb62 18120
7e314c57
JK
18121 /* The die_type call above may have already set the type for this DIE. */
18122 range_type = get_die_type (die, cu);
18123 if (range_type)
18124 return range_type;
18125
729efb13
SA
18126 low.kind = PROP_CONST;
18127 high.kind = PROP_CONST;
18128 high.data.const_val = 0;
18129
4fae6e18
JK
18130 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18131 omitting DW_AT_lower_bound. */
18132 switch (cu->language)
6e70227d 18133 {
4fae6e18
JK
18134 case language_c:
18135 case language_cplus:
729efb13 18136 low.data.const_val = 0;
4fae6e18
JK
18137 low_default_is_valid = 1;
18138 break;
18139 case language_fortran:
729efb13 18140 low.data.const_val = 1;
4fae6e18
JK
18141 low_default_is_valid = 1;
18142 break;
18143 case language_d:
4fae6e18 18144 case language_objc:
c44af4eb 18145 case language_rust:
729efb13 18146 low.data.const_val = 0;
4fae6e18
JK
18147 low_default_is_valid = (cu->header.version >= 4);
18148 break;
18149 case language_ada:
18150 case language_m2:
18151 case language_pascal:
729efb13 18152 low.data.const_val = 1;
4fae6e18
JK
18153 low_default_is_valid = (cu->header.version >= 4);
18154 break;
18155 default:
729efb13 18156 low.data.const_val = 0;
4fae6e18
JK
18157 low_default_is_valid = 0;
18158 break;
a02abb62
JB
18159 }
18160
e142c38c 18161 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18162 if (attr != nullptr)
9a49df9d 18163 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18164 else if (!low_default_is_valid)
b98664d3 18165 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18166 "- DIE at %s [in module %s]"),
18167 sect_offset_str (die->sect_off),
518817b3 18168 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18169
506f5c41
TV
18170 struct attribute *attr_ub, *attr_count;
18171 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18172 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18173 {
506f5c41 18174 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18175 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18176 {
c451ebe5
SA
18177 /* If bounds are constant do the final calculation here. */
18178 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18179 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18180 else
18181 high_bound_is_count = 1;
c2ff108b 18182 }
506f5c41
TV
18183 else
18184 {
18185 if (attr_ub != NULL)
18186 complaint (_("Unresolved DW_AT_upper_bound "
18187 "- DIE at %s [in module %s]"),
18188 sect_offset_str (die->sect_off),
18189 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18190 if (attr_count != NULL)
18191 complaint (_("Unresolved DW_AT_count "
18192 "- DIE at %s [in module %s]"),
18193 sect_offset_str (die->sect_off),
18194 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18195 }
e77813c8 18196 }
a02abb62 18197
4e962e74
TT
18198 LONGEST bias = 0;
18199 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18200 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18201 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18202
dbb9c2b1
JB
18203 /* Normally, the DWARF producers are expected to use a signed
18204 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18205 But this is unfortunately not always the case, as witnessed
18206 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18207 is used instead. To work around that ambiguity, we treat
18208 the bounds as signed, and thus sign-extend their values, when
18209 the base type is signed. */
6e70227d 18210 negative_mask =
d359392f 18211 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18212 if (low.kind == PROP_CONST
18213 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18214 low.data.const_val |= negative_mask;
18215 if (high.kind == PROP_CONST
18216 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18217 high.data.const_val |= negative_mask;
43bbcdc2 18218
5bbd8269
AB
18219 /* Check for bit and byte strides. */
18220 struct dynamic_prop byte_stride_prop;
18221 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18222 if (attr_byte_stride != nullptr)
18223 {
18224 struct type *prop_type
18225 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18226 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18227 prop_type);
18228 }
18229
18230 struct dynamic_prop bit_stride_prop;
18231 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18232 if (attr_bit_stride != nullptr)
18233 {
18234 /* It only makes sense to have either a bit or byte stride. */
18235 if (attr_byte_stride != nullptr)
18236 {
18237 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18238 "- DIE at %s [in module %s]"),
18239 sect_offset_str (die->sect_off),
18240 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18241 attr_bit_stride = nullptr;
18242 }
18243 else
18244 {
18245 struct type *prop_type
18246 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18247 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18248 prop_type);
18249 }
18250 }
18251
18252 if (attr_byte_stride != nullptr
18253 || attr_bit_stride != nullptr)
18254 {
18255 bool byte_stride_p = (attr_byte_stride != nullptr);
18256 struct dynamic_prop *stride
18257 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18258
18259 range_type
18260 = create_range_type_with_stride (NULL, orig_base_type, &low,
18261 &high, bias, stride, byte_stride_p);
18262 }
18263 else
18264 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18265
c451ebe5
SA
18266 if (high_bound_is_count)
18267 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18268
c2ff108b
JK
18269 /* Ada expects an empty array on no boundary attributes. */
18270 if (attr == NULL && cu->language != language_ada)
729efb13 18271 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18272
39cbfefa
DJ
18273 name = dwarf2_name (die, cu);
18274 if (name)
18275 TYPE_NAME (range_type) = name;
6e70227d 18276
e142c38c 18277 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18278 if (attr != nullptr)
a02abb62
JB
18279 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18280
2b4424c3
TT
18281 maybe_set_alignment (cu, die, range_type);
18282
7e314c57
JK
18283 set_die_type (die, range_type, cu);
18284
18285 /* set_die_type should be already done. */
b4ba55a1
JB
18286 set_descriptive_type (range_type, die, cu);
18287
7e314c57 18288 return range_type;
a02abb62 18289}
6e70227d 18290
f792889a 18291static struct type *
81a17f79
JB
18292read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18293{
18294 struct type *type;
81a17f79 18295
518817b3
SM
18296 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18297 NULL);
0114d602 18298 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18299
74a2f8ff 18300 /* In Ada, an unspecified type is typically used when the description
85102364 18301 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18302 such a type, we treat it as a stub, and try to resolve it later on,
18303 when needed. */
18304 if (cu->language == language_ada)
18305 TYPE_STUB (type) = 1;
18306
f792889a 18307 return set_die_type (die, type, cu);
81a17f79 18308}
a02abb62 18309
639d11d3
DC
18310/* Read a single die and all its descendents. Set the die's sibling
18311 field to NULL; set other fields in the die correctly, and set all
18312 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18313 location of the info_ptr after reading all of those dies. PARENT
18314 is the parent of the die in question. */
18315
18316static struct die_info *
dee91e82 18317read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18318 const gdb_byte *info_ptr,
18319 const gdb_byte **new_info_ptr,
dee91e82 18320 struct die_info *parent)
639d11d3
DC
18321{
18322 struct die_info *die;
d521ce57 18323 const gdb_byte *cur_ptr;
639d11d3
DC
18324 int has_children;
18325
bf6af496 18326 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18327 if (die == NULL)
18328 {
18329 *new_info_ptr = cur_ptr;
18330 return NULL;
18331 }
93311388 18332 store_in_ref_table (die, reader->cu);
639d11d3
DC
18333
18334 if (has_children)
bf6af496 18335 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18336 else
18337 {
18338 die->child = NULL;
18339 *new_info_ptr = cur_ptr;
18340 }
18341
18342 die->sibling = NULL;
18343 die->parent = parent;
18344 return die;
18345}
18346
18347/* Read a die, all of its descendents, and all of its siblings; set
18348 all of the fields of all of the dies correctly. Arguments are as
18349 in read_die_and_children. */
18350
18351static struct die_info *
bf6af496 18352read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18353 const gdb_byte *info_ptr,
18354 const gdb_byte **new_info_ptr,
bf6af496 18355 struct die_info *parent)
639d11d3
DC
18356{
18357 struct die_info *first_die, *last_sibling;
d521ce57 18358 const gdb_byte *cur_ptr;
639d11d3 18359
c906108c 18360 cur_ptr = info_ptr;
639d11d3
DC
18361 first_die = last_sibling = NULL;
18362
18363 while (1)
c906108c 18364 {
639d11d3 18365 struct die_info *die
dee91e82 18366 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18367
1d325ec1 18368 if (die == NULL)
c906108c 18369 {
639d11d3
DC
18370 *new_info_ptr = cur_ptr;
18371 return first_die;
c906108c 18372 }
1d325ec1
DJ
18373
18374 if (!first_die)
18375 first_die = die;
c906108c 18376 else
1d325ec1
DJ
18377 last_sibling->sibling = die;
18378
18379 last_sibling = die;
c906108c 18380 }
c906108c
SS
18381}
18382
bf6af496
DE
18383/* Read a die, all of its descendents, and all of its siblings; set
18384 all of the fields of all of the dies correctly. Arguments are as
18385 in read_die_and_children.
18386 This the main entry point for reading a DIE and all its children. */
18387
18388static struct die_info *
18389read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18390 const gdb_byte *info_ptr,
18391 const gdb_byte **new_info_ptr,
bf6af496
DE
18392 struct die_info *parent)
18393{
18394 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18395 new_info_ptr, parent);
18396
b4f54984 18397 if (dwarf_die_debug)
bf6af496
DE
18398 {
18399 fprintf_unfiltered (gdb_stdlog,
18400 "Read die from %s@0x%x of %s:\n",
a32a8923 18401 get_section_name (reader->die_section),
bf6af496
DE
18402 (unsigned) (info_ptr - reader->die_section->buffer),
18403 bfd_get_filename (reader->abfd));
b4f54984 18404 dump_die (die, dwarf_die_debug);
bf6af496
DE
18405 }
18406
18407 return die;
18408}
18409
3019eac3
DE
18410/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18411 attributes.
18412 The caller is responsible for filling in the extra attributes
18413 and updating (*DIEP)->num_attrs.
18414 Set DIEP to point to a newly allocated die with its information,
18415 except for its child, sibling, and parent fields.
18416 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18417
d521ce57 18418static const gdb_byte *
3019eac3 18419read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18420 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18421 int *has_children, int num_extra_attrs)
93311388 18422{
b64f50a1 18423 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18424 struct abbrev_info *abbrev;
18425 struct die_info *die;
18426 struct dwarf2_cu *cu = reader->cu;
18427 bfd *abfd = reader->abfd;
18428
9c541725 18429 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18430 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18431 info_ptr += bytes_read;
18432 if (!abbrev_number)
18433 {
18434 *diep = NULL;
18435 *has_children = 0;
18436 return info_ptr;
18437 }
18438
685af9cd 18439 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18440 if (!abbrev)
348e048f
DE
18441 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18442 abbrev_number,
18443 bfd_get_filename (abfd));
18444
3019eac3 18445 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18446 die->sect_off = sect_off;
93311388
DE
18447 die->tag = abbrev->tag;
18448 die->abbrev = abbrev_number;
18449
3019eac3
DE
18450 /* Make the result usable.
18451 The caller needs to update num_attrs after adding the extra
18452 attributes. */
93311388
DE
18453 die->num_attrs = abbrev->num_attrs;
18454
18455 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18456 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18457 info_ptr);
93311388
DE
18458
18459 *diep = die;
18460 *has_children = abbrev->has_children;
18461 return info_ptr;
18462}
18463
3019eac3
DE
18464/* Read a die and all its attributes.
18465 Set DIEP to point to a newly allocated die with its information,
18466 except for its child, sibling, and parent fields.
18467 Set HAS_CHILDREN to tell whether the die has children or not. */
18468
d521ce57 18469static const gdb_byte *
3019eac3 18470read_full_die (const struct die_reader_specs *reader,
d521ce57 18471 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18472 int *has_children)
18473{
d521ce57 18474 const gdb_byte *result;
bf6af496
DE
18475
18476 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18477
b4f54984 18478 if (dwarf_die_debug)
bf6af496
DE
18479 {
18480 fprintf_unfiltered (gdb_stdlog,
18481 "Read die from %s@0x%x of %s:\n",
a32a8923 18482 get_section_name (reader->die_section),
bf6af496
DE
18483 (unsigned) (info_ptr - reader->die_section->buffer),
18484 bfd_get_filename (reader->abfd));
b4f54984 18485 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18486 }
18487
18488 return result;
3019eac3 18489}
433df2d4
DE
18490\f
18491/* Abbreviation tables.
3019eac3 18492
433df2d4 18493 In DWARF version 2, the description of the debugging information is
c906108c
SS
18494 stored in a separate .debug_abbrev section. Before we read any
18495 dies from a section we read in all abbreviations and install them
433df2d4
DE
18496 in a hash table. */
18497
18498/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18499
685af9cd
TT
18500struct abbrev_info *
18501abbrev_table::alloc_abbrev ()
433df2d4
DE
18502{
18503 struct abbrev_info *abbrev;
18504
685af9cd 18505 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18506 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18507
433df2d4
DE
18508 return abbrev;
18509}
18510
18511/* Add an abbreviation to the table. */
c906108c 18512
685af9cd
TT
18513void
18514abbrev_table::add_abbrev (unsigned int abbrev_number,
18515 struct abbrev_info *abbrev)
433df2d4
DE
18516{
18517 unsigned int hash_number;
18518
18519 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18520 abbrev->next = m_abbrevs[hash_number];
18521 m_abbrevs[hash_number] = abbrev;
433df2d4 18522}
dee91e82 18523
433df2d4
DE
18524/* Look up an abbrev in the table.
18525 Returns NULL if the abbrev is not found. */
18526
685af9cd
TT
18527struct abbrev_info *
18528abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18529{
433df2d4
DE
18530 unsigned int hash_number;
18531 struct abbrev_info *abbrev;
18532
18533 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18534 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18535
18536 while (abbrev)
18537 {
18538 if (abbrev->number == abbrev_number)
18539 return abbrev;
18540 abbrev = abbrev->next;
18541 }
18542 return NULL;
18543}
18544
18545/* Read in an abbrev table. */
18546
685af9cd 18547static abbrev_table_up
ed2dc618
SM
18548abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18549 struct dwarf2_section_info *section,
9c541725 18550 sect_offset sect_off)
433df2d4
DE
18551{
18552 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18553 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18554 const gdb_byte *abbrev_ptr;
c906108c
SS
18555 struct abbrev_info *cur_abbrev;
18556 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18557 unsigned int abbrev_form;
6dfa2fc2 18558 std::vector<struct attr_abbrev> cur_attrs;
c906108c 18559
685af9cd 18560 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18561
433df2d4 18562 dwarf2_read_section (objfile, section);
9c541725 18563 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18564 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18565 abbrev_ptr += bytes_read;
18566
0963b4bd 18567 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18568 while (abbrev_number)
18569 {
6dfa2fc2 18570 cur_attrs.clear ();
685af9cd 18571 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18572
18573 /* read in abbrev header */
18574 cur_abbrev->number = abbrev_number;
aead7601
SM
18575 cur_abbrev->tag
18576 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18577 abbrev_ptr += bytes_read;
18578 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18579 abbrev_ptr += 1;
18580
18581 /* now read in declarations */
22d2f3ab 18582 for (;;)
c906108c 18583 {
43988095
JK
18584 LONGEST implicit_const;
18585
22d2f3ab
JK
18586 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18587 abbrev_ptr += bytes_read;
18588 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18589 abbrev_ptr += bytes_read;
43988095
JK
18590 if (abbrev_form == DW_FORM_implicit_const)
18591 {
18592 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18593 &bytes_read);
18594 abbrev_ptr += bytes_read;
18595 }
18596 else
18597 {
18598 /* Initialize it due to a false compiler warning. */
18599 implicit_const = -1;
18600 }
22d2f3ab
JK
18601
18602 if (abbrev_name == 0)
18603 break;
18604
6dfa2fc2
TT
18605 cur_attrs.emplace_back ();
18606 struct attr_abbrev &cur_attr = cur_attrs.back ();
18607 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18608 cur_attr.form = (enum dwarf_form) abbrev_form;
18609 cur_attr.implicit_const = implicit_const;
22d2f3ab 18610 ++cur_abbrev->num_attrs;
c906108c
SS
18611 }
18612
8d749320
SM
18613 cur_abbrev->attrs =
18614 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18615 cur_abbrev->num_attrs);
6dfa2fc2 18616 memcpy (cur_abbrev->attrs, cur_attrs.data (),
f3dd6933
DJ
18617 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18618
685af9cd 18619 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18620
18621 /* Get next abbreviation.
18622 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18623 always properly terminated with an abbrev number of 0.
18624 Exit loop if we encounter an abbreviation which we have
18625 already read (which means we are about to read the abbreviations
18626 for the next compile unit) or if the end of the abbreviation
18627 table is reached. */
433df2d4 18628 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18629 break;
18630 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18631 abbrev_ptr += bytes_read;
685af9cd 18632 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18633 break;
18634 }
f3dd6933 18635
433df2d4 18636 return abbrev_table;
c906108c
SS
18637}
18638
72bf9492
DJ
18639/* Returns nonzero if TAG represents a type that we might generate a partial
18640 symbol for. */
18641
18642static int
18643is_type_tag_for_partial (int tag)
18644{
18645 switch (tag)
18646 {
18647#if 0
18648 /* Some types that would be reasonable to generate partial symbols for,
18649 that we don't at present. */
18650 case DW_TAG_array_type:
18651 case DW_TAG_file_type:
18652 case DW_TAG_ptr_to_member_type:
18653 case DW_TAG_set_type:
18654 case DW_TAG_string_type:
18655 case DW_TAG_subroutine_type:
18656#endif
18657 case DW_TAG_base_type:
18658 case DW_TAG_class_type:
680b30c7 18659 case DW_TAG_interface_type:
72bf9492
DJ
18660 case DW_TAG_enumeration_type:
18661 case DW_TAG_structure_type:
18662 case DW_TAG_subrange_type:
18663 case DW_TAG_typedef:
18664 case DW_TAG_union_type:
18665 return 1;
18666 default:
18667 return 0;
18668 }
18669}
18670
18671/* Load all DIEs that are interesting for partial symbols into memory. */
18672
18673static struct partial_die_info *
dee91e82 18674load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18675 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18676{
dee91e82 18677 struct dwarf2_cu *cu = reader->cu;
518817b3 18678 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18679 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18680 unsigned int bytes_read;
5afb4e99 18681 unsigned int load_all = 0;
72bf9492
DJ
18682 int nesting_level = 1;
18683
18684 parent_die = NULL;
18685 last_die = NULL;
18686
7adf1e79
DE
18687 gdb_assert (cu->per_cu != NULL);
18688 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18689 load_all = 1;
18690
72bf9492
DJ
18691 cu->partial_dies
18692 = htab_create_alloc_ex (cu->header.length / 12,
18693 partial_die_hash,
18694 partial_die_eq,
18695 NULL,
18696 &cu->comp_unit_obstack,
18697 hashtab_obstack_allocate,
18698 dummy_obstack_deallocate);
18699
72bf9492
DJ
18700 while (1)
18701 {
685af9cd 18702 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18703
18704 /* A NULL abbrev means the end of a series of children. */
18705 if (abbrev == NULL)
18706 {
18707 if (--nesting_level == 0)
cd9983dd
YQ
18708 return first_die;
18709
72bf9492
DJ
18710 info_ptr += bytes_read;
18711 last_die = parent_die;
18712 parent_die = parent_die->die_parent;
18713 continue;
18714 }
18715
98bfdba5
PA
18716 /* Check for template arguments. We never save these; if
18717 they're seen, we just mark the parent, and go on our way. */
18718 if (parent_die != NULL
18719 && cu->language == language_cplus
18720 && (abbrev->tag == DW_TAG_template_type_param
18721 || abbrev->tag == DW_TAG_template_value_param))
18722 {
18723 parent_die->has_template_arguments = 1;
18724
18725 if (!load_all)
18726 {
18727 /* We don't need a partial DIE for the template argument. */
dee91e82 18728 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18729 continue;
18730 }
18731 }
18732
0d99eb77 18733 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18734 Skip their other children. */
18735 if (!load_all
18736 && cu->language == language_cplus
18737 && parent_die != NULL
18738 && parent_die->tag == DW_TAG_subprogram)
18739 {
dee91e82 18740 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18741 continue;
18742 }
18743
5afb4e99
DJ
18744 /* Check whether this DIE is interesting enough to save. Normally
18745 we would not be interested in members here, but there may be
18746 later variables referencing them via DW_AT_specification (for
18747 static members). */
18748 if (!load_all
18749 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18750 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18751 && abbrev->tag != DW_TAG_enumerator
18752 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18753 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18754 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18755 && abbrev->tag != DW_TAG_variable
5afb4e99 18756 && abbrev->tag != DW_TAG_namespace
f55ee35c 18757 && abbrev->tag != DW_TAG_module
95554aad 18758 && abbrev->tag != DW_TAG_member
74921315
KS
18759 && abbrev->tag != DW_TAG_imported_unit
18760 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18761 {
18762 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18763 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18764 continue;
18765 }
18766
6f06d47b
YQ
18767 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18768 abbrev);
cd9983dd 18769
48fbe735 18770 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18771
18772 /* This two-pass algorithm for processing partial symbols has a
18773 high cost in cache pressure. Thus, handle some simple cases
18774 here which cover the majority of C partial symbols. DIEs
18775 which neither have specification tags in them, nor could have
18776 specification tags elsewhere pointing at them, can simply be
18777 processed and discarded.
18778
18779 This segment is also optional; scan_partial_symbols and
18780 add_partial_symbol will handle these DIEs if we chain
18781 them in normally. When compilers which do not emit large
18782 quantities of duplicate debug information are more common,
18783 this code can probably be removed. */
18784
18785 /* Any complete simple types at the top level (pretty much all
18786 of them, for a language without namespaces), can be processed
18787 directly. */
18788 if (parent_die == NULL
cd9983dd
YQ
18789 && pdi.has_specification == 0
18790 && pdi.is_declaration == 0
18791 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18792 || pdi.tag == DW_TAG_base_type
18793 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18794 {
cd9983dd 18795 if (building_psymtab && pdi.name != NULL)
31edb802 18796 add_psymbol_to_list (pdi.name, false,
79748972 18797 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18798 psymbol_placement::STATIC,
1762568f 18799 0, cu->language, objfile);
cd9983dd 18800 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18801 continue;
18802 }
18803
d8228535
JK
18804 /* The exception for DW_TAG_typedef with has_children above is
18805 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18806 type_name_or_error will error on such types later.
d8228535
JK
18807
18808 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18809 it could not find the child DIEs referenced later, this is checked
18810 above. In correct DWARF DW_TAG_typedef should have no children. */
18811
cd9983dd 18812 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18813 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18814 "- DIE at %s [in module %s]"),
cd9983dd 18815 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18816
72bf9492
DJ
18817 /* If we're at the second level, and we're an enumerator, and
18818 our parent has no specification (meaning possibly lives in a
18819 namespace elsewhere), then we can add the partial symbol now
18820 instead of queueing it. */
cd9983dd 18821 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18822 && parent_die != NULL
18823 && parent_die->die_parent == NULL
18824 && parent_die->tag == DW_TAG_enumeration_type
18825 && parent_die->has_specification == 0)
18826 {
cd9983dd 18827 if (pdi.name == NULL)
b98664d3 18828 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18829 else if (building_psymtab)
31edb802 18830 add_psymbol_to_list (pdi.name, false,
79748972 18831 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18832 cu->language == language_cplus
75aedd27
TT
18833 ? psymbol_placement::GLOBAL
18834 : psymbol_placement::STATIC,
1762568f 18835 0, cu->language, objfile);
72bf9492 18836
cd9983dd 18837 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18838 continue;
18839 }
18840
cd9983dd 18841 struct partial_die_info *part_die
6f06d47b 18842 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18843
72bf9492
DJ
18844 /* We'll save this DIE so link it in. */
18845 part_die->die_parent = parent_die;
18846 part_die->die_sibling = NULL;
18847 part_die->die_child = NULL;
18848
18849 if (last_die && last_die == parent_die)
18850 last_die->die_child = part_die;
18851 else if (last_die)
18852 last_die->die_sibling = part_die;
18853
18854 last_die = part_die;
18855
18856 if (first_die == NULL)
18857 first_die = part_die;
18858
18859 /* Maybe add the DIE to the hash table. Not all DIEs that we
18860 find interesting need to be in the hash table, because we
18861 also have the parent/sibling/child chains; only those that we
18862 might refer to by offset later during partial symbol reading.
18863
18864 For now this means things that might have be the target of a
18865 DW_AT_specification, DW_AT_abstract_origin, or
18866 DW_AT_extension. DW_AT_extension will refer only to
18867 namespaces; DW_AT_abstract_origin refers to functions (and
18868 many things under the function DIE, but we do not recurse
18869 into function DIEs during partial symbol reading) and
18870 possibly variables as well; DW_AT_specification refers to
18871 declarations. Declarations ought to have the DW_AT_declaration
18872 flag. It happens that GCC forgets to put it in sometimes, but
18873 only for functions, not for types.
18874
18875 Adding more things than necessary to the hash table is harmless
18876 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18877 wasted time in find_partial_die, when we reread the compilation
18878 unit with load_all_dies set. */
72bf9492 18879
5afb4e99 18880 if (load_all
72929c62 18881 || abbrev->tag == DW_TAG_constant
5afb4e99 18882 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18883 || abbrev->tag == DW_TAG_variable
18884 || abbrev->tag == DW_TAG_namespace
18885 || part_die->is_declaration)
18886 {
18887 void **slot;
18888
18889 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18890 to_underlying (part_die->sect_off),
18891 INSERT);
72bf9492
DJ
18892 *slot = part_die;
18893 }
18894
72bf9492 18895 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18896 we have no reason to follow the children of structures; for other
98bfdba5
PA
18897 languages we have to, so that we can get at method physnames
18898 to infer fully qualified class names, for DW_AT_specification,
18899 and for C++ template arguments. For C++, we also look one level
18900 inside functions to find template arguments (if the name of the
18901 function does not already contain the template arguments).
bc30ff58 18902
0a4b0913
AB
18903 For Ada and Fortran, we need to scan the children of subprograms
18904 and lexical blocks as well because these languages allow the
18905 definition of nested entities that could be interesting for the
18906 debugger, such as nested subprograms for instance. */
72bf9492 18907 if (last_die->has_children
5afb4e99
DJ
18908 && (load_all
18909 || last_die->tag == DW_TAG_namespace
f55ee35c 18910 || last_die->tag == DW_TAG_module
72bf9492 18911 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18912 || (cu->language == language_cplus
18913 && last_die->tag == DW_TAG_subprogram
18914 && (last_die->name == NULL
18915 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18916 || (cu->language != language_c
18917 && (last_die->tag == DW_TAG_class_type
680b30c7 18918 || last_die->tag == DW_TAG_interface_type
72bf9492 18919 || last_die->tag == DW_TAG_structure_type
bc30ff58 18920 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18921 || ((cu->language == language_ada
18922 || cu->language == language_fortran)
bc30ff58
JB
18923 && (last_die->tag == DW_TAG_subprogram
18924 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18925 {
18926 nesting_level++;
18927 parent_die = last_die;
18928 continue;
18929 }
18930
18931 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18932 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18933
18934 /* Back to the top, do it again. */
18935 }
18936}
18937
6f06d47b
YQ
18938partial_die_info::partial_die_info (sect_offset sect_off_,
18939 struct abbrev_info *abbrev)
18940 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18941{
18942}
18943
35cc7ed7
YQ
18944/* Read a minimal amount of information into the minimal die structure.
18945 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18946
48fbe735
YQ
18947const gdb_byte *
18948partial_die_info::read (const struct die_reader_specs *reader,
18949 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18950{
dee91e82 18951 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18952 struct dwarf2_per_objfile *dwarf2_per_objfile
18953 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18954 unsigned int i;
c5aa993b 18955 int has_low_pc_attr = 0;
c906108c 18956 int has_high_pc_attr = 0;
91da1414 18957 int high_pc_relative = 0;
c906108c 18958
fd0a254f 18959 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18960 {
48fbe735
YQ
18961 struct attribute attr;
18962
fd0a254f 18963 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18964
18965 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18966 partial symbol table. */
c906108c
SS
18967 switch (attr.name)
18968 {
18969 case DW_AT_name:
48fbe735 18970 switch (tag)
71c25dea
TT
18971 {
18972 case DW_TAG_compile_unit:
95554aad 18973 case DW_TAG_partial_unit:
348e048f 18974 case DW_TAG_type_unit:
71c25dea
TT
18975 /* Compilation units have a DW_AT_name that is a filename, not
18976 a source language identifier. */
18977 case DW_TAG_enumeration_type:
18978 case DW_TAG_enumerator:
18979 /* These tags always have simple identifiers already; no need
18980 to canonicalize them. */
48fbe735 18981 name = DW_STRING (&attr);
71c25dea
TT
18982 break;
18983 default:
48fbe735
YQ
18984 {
18985 struct objfile *objfile = dwarf2_per_objfile->objfile;
18986
18987 name
18988 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18989 &objfile->per_bfd->storage_obstack);
18990 }
71c25dea
TT
18991 break;
18992 }
c906108c 18993 break;
31ef98ae 18994 case DW_AT_linkage_name:
c906108c 18995 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18996 /* Note that both forms of linkage name might appear. We
18997 assume they will be the same, and we only store the last
18998 one we see. */
48fbe735 18999 linkage_name = DW_STRING (&attr);
c906108c
SS
19000 break;
19001 case DW_AT_low_pc:
19002 has_low_pc_attr = 1;
48fbe735 19003 lowpc = attr_value_as_address (&attr);
c906108c
SS
19004 break;
19005 case DW_AT_high_pc:
19006 has_high_pc_attr = 1;
48fbe735 19007 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
19008 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19009 high_pc_relative = 1;
c906108c
SS
19010 break;
19011 case DW_AT_location:
0963b4bd 19012 /* Support the .debug_loc offsets. */
8e19ed76
PS
19013 if (attr_form_is_block (&attr))
19014 {
48fbe735 19015 d.locdesc = DW_BLOCK (&attr);
8e19ed76 19016 }
3690dd37 19017 else if (attr_form_is_section_offset (&attr))
8e19ed76 19018 {
4d3c2250 19019 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
19020 }
19021 else
19022 {
4d3c2250
KB
19023 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19024 "partial symbol information");
8e19ed76 19025 }
c906108c 19026 break;
c906108c 19027 case DW_AT_external:
48fbe735 19028 is_external = DW_UNSND (&attr);
c906108c
SS
19029 break;
19030 case DW_AT_declaration:
48fbe735 19031 is_declaration = DW_UNSND (&attr);
c906108c
SS
19032 break;
19033 case DW_AT_type:
48fbe735 19034 has_type = 1;
c906108c
SS
19035 break;
19036 case DW_AT_abstract_origin:
19037 case DW_AT_specification:
72bf9492 19038 case DW_AT_extension:
48fbe735
YQ
19039 has_specification = 1;
19040 spec_offset = dwarf2_get_ref_die_offset (&attr);
19041 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19042 || cu->per_cu->is_dwz);
c906108c
SS
19043 break;
19044 case DW_AT_sibling:
19045 /* Ignore absolute siblings, they might point outside of
19046 the current compile unit. */
19047 if (attr.form == DW_FORM_ref_addr)
b98664d3 19048 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19049 else
b9502d3f 19050 {
48fbe735 19051 const gdb_byte *buffer = reader->buffer;
9c541725
PA
19052 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19053 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19054
19055 if (sibling_ptr < info_ptr)
b98664d3 19056 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
19057 else if (sibling_ptr > reader->buffer_end)
19058 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19059 else
48fbe735 19060 sibling = sibling_ptr;
b9502d3f 19061 }
c906108c 19062 break;
fa4028e9 19063 case DW_AT_byte_size:
48fbe735 19064 has_byte_size = 1;
fa4028e9 19065 break;
ff908ebf 19066 case DW_AT_const_value:
48fbe735 19067 has_const_value = 1;
ff908ebf 19068 break;
68511cec
CES
19069 case DW_AT_calling_convention:
19070 /* DWARF doesn't provide a way to identify a program's source-level
19071 entry point. DW_AT_calling_convention attributes are only meant
19072 to describe functions' calling conventions.
19073
19074 However, because it's a necessary piece of information in
0c1b455e
TT
19075 Fortran, and before DWARF 4 DW_CC_program was the only
19076 piece of debugging information whose definition refers to
19077 a 'main program' at all, several compilers marked Fortran
19078 main programs with DW_CC_program --- even when those
19079 functions use the standard calling conventions.
19080
19081 Although DWARF now specifies a way to provide this
19082 information, we support this practice for backward
19083 compatibility. */
68511cec 19084 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19085 && cu->language == language_fortran)
48fbe735 19086 main_subprogram = 1;
68511cec 19087 break;
481860b3
GB
19088 case DW_AT_inline:
19089 if (DW_UNSND (&attr) == DW_INL_inlined
19090 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19091 may_be_inlined = 1;
481860b3 19092 break;
95554aad
TT
19093
19094 case DW_AT_import:
48fbe735 19095 if (tag == DW_TAG_imported_unit)
36586728 19096 {
48fbe735
YQ
19097 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19098 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19099 || cu->per_cu->is_dwz);
19100 }
95554aad
TT
19101 break;
19102
0c1b455e 19103 case DW_AT_main_subprogram:
48fbe735 19104 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19105 break;
19106
05caa1d2
TT
19107 case DW_AT_ranges:
19108 {
19109 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19110 but that requires a full DIE, so instead we just
19111 reimplement it. */
19112 int need_ranges_base = tag != DW_TAG_compile_unit;
19113 unsigned int ranges_offset = (DW_UNSND (&attr)
19114 + (need_ranges_base
19115 ? cu->ranges_base
19116 : 0));
19117
19118 /* Value of the DW_AT_ranges attribute is the offset in the
19119 .debug_ranges section. */
19120 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19121 nullptr))
19122 has_pc_info = 1;
19123 }
19124 break;
19125
c906108c
SS
19126 default:
19127 break;
19128 }
19129 }
19130
10d06d82
TT
19131 /* For Ada, if both the name and the linkage name appear, we prefer
19132 the latter. This lets "catch exception" work better, regardless
19133 of the order in which the name and linkage name were emitted.
19134 Really, though, this is just a workaround for the fact that gdb
19135 doesn't store both the name and the linkage name. */
19136 if (cu->language == language_ada && linkage_name != nullptr)
19137 name = linkage_name;
19138
91da1414 19139 if (high_pc_relative)
48fbe735 19140 highpc += lowpc;
91da1414 19141
9373cf26
JK
19142 if (has_low_pc_attr && has_high_pc_attr)
19143 {
19144 /* When using the GNU linker, .gnu.linkonce. sections are used to
19145 eliminate duplicate copies of functions and vtables and such.
19146 The linker will arbitrarily choose one and discard the others.
19147 The AT_*_pc values for such functions refer to local labels in
19148 these sections. If the section from that file was discarded, the
19149 labels are not in the output, so the relocs get a value of 0.
19150 If this is a discarded function, mark the pc bounds as invalid,
19151 so that GDB will ignore it. */
48fbe735 19152 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19153 {
48fbe735 19154 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19155 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19156
b98664d3 19157 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19158 "for DIE at %s [in module %s]"),
48fbe735
YQ
19159 paddress (gdbarch, lowpc),
19160 sect_offset_str (sect_off),
9d8780f0 19161 objfile_name (objfile));
9373cf26
JK
19162 }
19163 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19164 else if (lowpc >= highpc)
9373cf26 19165 {
48fbe735 19166 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19167 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19168
b98664d3 19169 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19170 "for DIE at %s [in module %s]"),
48fbe735
YQ
19171 paddress (gdbarch, lowpc),
19172 paddress (gdbarch, highpc),
19173 sect_offset_str (sect_off),
9c541725 19174 objfile_name (objfile));
9373cf26
JK
19175 }
19176 else
48fbe735 19177 has_pc_info = 1;
9373cf26 19178 }
85cbf3d3 19179
c906108c
SS
19180 return info_ptr;
19181}
19182
72bf9492
DJ
19183/* Find a cached partial DIE at OFFSET in CU. */
19184
d590ff25
YQ
19185struct partial_die_info *
19186dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19187{
19188 struct partial_die_info *lookup_die = NULL;
6f06d47b 19189 struct partial_die_info part_die (sect_off);
72bf9492 19190
9a3c8263 19191 lookup_die = ((struct partial_die_info *)
d590ff25 19192 htab_find_with_hash (partial_dies, &part_die,
9c541725 19193 to_underlying (sect_off)));
72bf9492 19194
72bf9492
DJ
19195 return lookup_die;
19196}
19197
348e048f
DE
19198/* Find a partial DIE at OFFSET, which may or may not be in CU,
19199 except in the case of .debug_types DIEs which do not reference
19200 outside their CU (they do however referencing other types via
55f1336d 19201 DW_FORM_ref_sig8). */
72bf9492 19202
122cf0f2 19203static const struct cu_partial_die_info
9c541725 19204find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19205{
518817b3
SM
19206 struct dwarf2_per_objfile *dwarf2_per_objfile
19207 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19208 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19209 struct dwarf2_per_cu_data *per_cu = NULL;
19210 struct partial_die_info *pd = NULL;
72bf9492 19211
36586728 19212 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19213 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19214 {
d590ff25 19215 pd = cu->find_partial_die (sect_off);
5afb4e99 19216 if (pd != NULL)
fb816e8b 19217 return { cu, pd };
0d99eb77
DE
19218 /* We missed recording what we needed.
19219 Load all dies and try again. */
19220 per_cu = cu->per_cu;
5afb4e99 19221 }
0d99eb77
DE
19222 else
19223 {
19224 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19225 if (cu->per_cu->is_debug_types)
0d99eb77 19226 {
9d8780f0
SM
19227 error (_("Dwarf Error: Type Unit at offset %s contains"
19228 " external reference to offset %s [in module %s].\n"),
19229 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19230 bfd_get_filename (objfile->obfd));
19231 }
9c541725 19232 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19233 dwarf2_per_objfile);
72bf9492 19234
0d99eb77
DE
19235 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19236 load_partial_comp_unit (per_cu);
ae038cb0 19237
0d99eb77 19238 per_cu->cu->last_used = 0;
d590ff25 19239 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19240 }
5afb4e99 19241
dee91e82
DE
19242 /* If we didn't find it, and not all dies have been loaded,
19243 load them all and try again. */
19244
5afb4e99
DJ
19245 if (pd == NULL && per_cu->load_all_dies == 0)
19246 {
5afb4e99 19247 per_cu->load_all_dies = 1;
fd820528
DE
19248
19249 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19250 THIS_CU->cu may already be in use. So we can't just free it and
19251 replace its DIEs with the ones we read in. Instead, we leave those
19252 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19253 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19254 set. */
dee91e82 19255 load_partial_comp_unit (per_cu);
5afb4e99 19256
d590ff25 19257 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19258 }
19259
19260 if (pd == NULL)
19261 internal_error (__FILE__, __LINE__,
9d8780f0 19262 _("could not find partial DIE %s "
3e43a32a 19263 "in cache [from module %s]\n"),
9d8780f0 19264 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19265 return { per_cu->cu, pd };
72bf9492
DJ
19266}
19267
abc72ce4
DE
19268/* See if we can figure out if the class lives in a namespace. We do
19269 this by looking for a member function; its demangled name will
19270 contain namespace info, if there is any. */
19271
19272static void
19273guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19274 struct dwarf2_cu *cu)
19275{
19276 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19277 what template types look like, because the demangler
19278 frequently doesn't give the same name as the debug info. We
19279 could fix this by only using the demangled name to get the
19280 prefix (but see comment in read_structure_type). */
19281
19282 struct partial_die_info *real_pdi;
19283 struct partial_die_info *child_pdi;
19284
19285 /* If this DIE (this DIE's specification, if any) has a parent, then
19286 we should not do this. We'll prepend the parent's fully qualified
19287 name when we create the partial symbol. */
19288
19289 real_pdi = struct_pdi;
19290 while (real_pdi->has_specification)
fb816e8b 19291 {
122cf0f2
AB
19292 auto res = find_partial_die (real_pdi->spec_offset,
19293 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19294 real_pdi = res.pdi;
19295 cu = res.cu;
19296 }
abc72ce4
DE
19297
19298 if (real_pdi->die_parent != NULL)
19299 return;
19300
19301 for (child_pdi = struct_pdi->die_child;
19302 child_pdi != NULL;
19303 child_pdi = child_pdi->die_sibling)
19304 {
19305 if (child_pdi->tag == DW_TAG_subprogram
19306 && child_pdi->linkage_name != NULL)
19307 {
43816ebc
TT
19308 gdb::unique_xmalloc_ptr<char> actual_class_name
19309 (language_class_name_from_physname (cu->language_defn,
19310 child_pdi->linkage_name));
abc72ce4
DE
19311 if (actual_class_name != NULL)
19312 {
518817b3 19313 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19314 struct_pdi->name
021887d8 19315 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 19316 actual_class_name.get ());
abc72ce4
DE
19317 }
19318 break;
19319 }
19320 }
19321}
19322
52356b79
YQ
19323void
19324partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19325{
abc72ce4
DE
19326 /* Once we've fixed up a die, there's no point in doing so again.
19327 This also avoids a memory leak if we were to call
19328 guess_partial_die_structure_name multiple times. */
52356b79 19329 if (fixup_called)
abc72ce4
DE
19330 return;
19331
72bf9492
DJ
19332 /* If we found a reference attribute and the DIE has no name, try
19333 to find a name in the referred to DIE. */
19334
52356b79 19335 if (name == NULL && has_specification)
72bf9492
DJ
19336 {
19337 struct partial_die_info *spec_die;
72bf9492 19338
122cf0f2 19339 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19340 spec_die = res.pdi;
19341 cu = res.cu;
72bf9492 19342
52356b79 19343 spec_die->fixup (cu);
72bf9492
DJ
19344
19345 if (spec_die->name)
19346 {
52356b79 19347 name = spec_die->name;
72bf9492
DJ
19348
19349 /* Copy DW_AT_external attribute if it is set. */
19350 if (spec_die->is_external)
52356b79 19351 is_external = spec_die->is_external;
72bf9492
DJ
19352 }
19353 }
19354
19355 /* Set default names for some unnamed DIEs. */
72bf9492 19356
52356b79
YQ
19357 if (name == NULL && tag == DW_TAG_namespace)
19358 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19359
abc72ce4
DE
19360 /* If there is no parent die to provide a namespace, and there are
19361 children, see if we can determine the namespace from their linkage
122d1940 19362 name. */
abc72ce4 19363 if (cu->language == language_cplus
fd5866f6 19364 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19365 && die_parent == NULL
19366 && has_children
19367 && (tag == DW_TAG_class_type
19368 || tag == DW_TAG_structure_type
19369 || tag == DW_TAG_union_type))
19370 guess_partial_die_structure_name (this, cu);
abc72ce4 19371
53832f31
TT
19372 /* GCC might emit a nameless struct or union that has a linkage
19373 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19374 if (name == NULL
19375 && (tag == DW_TAG_class_type
19376 || tag == DW_TAG_interface_type
19377 || tag == DW_TAG_structure_type
19378 || tag == DW_TAG_union_type)
19379 && linkage_name != NULL)
53832f31 19380 {
43816ebc
TT
19381 gdb::unique_xmalloc_ptr<char> demangled
19382 (gdb_demangle (linkage_name, DMGL_TYPES));
19383 if (demangled != nullptr)
53832f31 19384 {
96408a79
SA
19385 const char *base;
19386
19387 /* Strip any leading namespaces/classes, keep only the base name.
19388 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19389 base = strrchr (demangled.get (), ':');
19390 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19391 base++;
19392 else
43816ebc 19393 base = demangled.get ();
96408a79 19394
518817b3 19395 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19396 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19397 }
19398 }
19399
52356b79 19400 fixup_called = 1;
72bf9492
DJ
19401}
19402
a8329558 19403/* Read an attribute value described by an attribute form. */
c906108c 19404
d521ce57 19405static const gdb_byte *
dee91e82
DE
19406read_attribute_value (const struct die_reader_specs *reader,
19407 struct attribute *attr, unsigned form,
43988095 19408 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19409{
dee91e82 19410 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19411 struct dwarf2_per_objfile *dwarf2_per_objfile
19412 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19413 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19414 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19415 bfd *abfd = reader->abfd;
e7c27a73 19416 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19417 unsigned int bytes_read;
19418 struct dwarf_block *blk;
19419
aead7601 19420 attr->form = (enum dwarf_form) form;
a8329558 19421 switch (form)
c906108c 19422 {
c906108c 19423 case DW_FORM_ref_addr:
ae411497 19424 if (cu->header.version == 2)
4568ecf9 19425 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19426 else
4568ecf9
DE
19427 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19428 &cu->header, &bytes_read);
ae411497
TT
19429 info_ptr += bytes_read;
19430 break;
36586728
TT
19431 case DW_FORM_GNU_ref_alt:
19432 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19433 info_ptr += bytes_read;
19434 break;
ae411497 19435 case DW_FORM_addr:
e7c27a73 19436 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19437 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19438 info_ptr += bytes_read;
c906108c
SS
19439 break;
19440 case DW_FORM_block2:
7b5a2f43 19441 blk = dwarf_alloc_block (cu);
c906108c
SS
19442 blk->size = read_2_bytes (abfd, info_ptr);
19443 info_ptr += 2;
19444 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19445 info_ptr += blk->size;
19446 DW_BLOCK (attr) = blk;
19447 break;
19448 case DW_FORM_block4:
7b5a2f43 19449 blk = dwarf_alloc_block (cu);
c906108c
SS
19450 blk->size = read_4_bytes (abfd, info_ptr);
19451 info_ptr += 4;
19452 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19453 info_ptr += blk->size;
19454 DW_BLOCK (attr) = blk;
19455 break;
19456 case DW_FORM_data2:
19457 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19458 info_ptr += 2;
19459 break;
19460 case DW_FORM_data4:
19461 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19462 info_ptr += 4;
19463 break;
19464 case DW_FORM_data8:
19465 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19466 info_ptr += 8;
19467 break;
0224619f
JK
19468 case DW_FORM_data16:
19469 blk = dwarf_alloc_block (cu);
19470 blk->size = 16;
19471 blk->data = read_n_bytes (abfd, info_ptr, 16);
19472 info_ptr += 16;
19473 DW_BLOCK (attr) = blk;
19474 break;
2dc7f7b3
TT
19475 case DW_FORM_sec_offset:
19476 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19477 info_ptr += bytes_read;
19478 break;
c906108c 19479 case DW_FORM_string:
9b1c24c8 19480 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19481 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19482 info_ptr += bytes_read;
19483 break;
4bdf3d34 19484 case DW_FORM_strp:
36586728
TT
19485 if (!cu->per_cu->is_dwz)
19486 {
ed2dc618
SM
19487 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19488 abfd, info_ptr, cu_header,
36586728
TT
19489 &bytes_read);
19490 DW_STRING_IS_CANONICAL (attr) = 0;
19491 info_ptr += bytes_read;
19492 break;
19493 }
19494 /* FALLTHROUGH */
43988095
JK
19495 case DW_FORM_line_strp:
19496 if (!cu->per_cu->is_dwz)
19497 {
ed2dc618
SM
19498 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19499 abfd, info_ptr,
43988095
JK
19500 cu_header, &bytes_read);
19501 DW_STRING_IS_CANONICAL (attr) = 0;
19502 info_ptr += bytes_read;
19503 break;
19504 }
19505 /* FALLTHROUGH */
36586728
TT
19506 case DW_FORM_GNU_strp_alt:
19507 {
ed2dc618 19508 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19509 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19510 &bytes_read);
19511
ed2dc618
SM
19512 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19513 dwz, str_offset);
36586728
TT
19514 DW_STRING_IS_CANONICAL (attr) = 0;
19515 info_ptr += bytes_read;
19516 }
4bdf3d34 19517 break;
2dc7f7b3 19518 case DW_FORM_exprloc:
c906108c 19519 case DW_FORM_block:
7b5a2f43 19520 blk = dwarf_alloc_block (cu);
c906108c
SS
19521 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19522 info_ptr += bytes_read;
19523 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19524 info_ptr += blk->size;
19525 DW_BLOCK (attr) = blk;
19526 break;
19527 case DW_FORM_block1:
7b5a2f43 19528 blk = dwarf_alloc_block (cu);
c906108c
SS
19529 blk->size = read_1_byte (abfd, info_ptr);
19530 info_ptr += 1;
19531 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19532 info_ptr += blk->size;
19533 DW_BLOCK (attr) = blk;
19534 break;
19535 case DW_FORM_data1:
19536 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19537 info_ptr += 1;
19538 break;
19539 case DW_FORM_flag:
19540 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19541 info_ptr += 1;
19542 break;
2dc7f7b3
TT
19543 case DW_FORM_flag_present:
19544 DW_UNSND (attr) = 1;
19545 break;
c906108c
SS
19546 case DW_FORM_sdata:
19547 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19548 info_ptr += bytes_read;
19549 break;
19550 case DW_FORM_udata:
19551 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19552 info_ptr += bytes_read;
19553 break;
19554 case DW_FORM_ref1:
9c541725 19555 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19556 + read_1_byte (abfd, info_ptr));
c906108c
SS
19557 info_ptr += 1;
19558 break;
19559 case DW_FORM_ref2:
9c541725 19560 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19561 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19562 info_ptr += 2;
19563 break;
19564 case DW_FORM_ref4:
9c541725 19565 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19566 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19567 info_ptr += 4;
19568 break;
613e1657 19569 case DW_FORM_ref8:
9c541725 19570 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19571 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19572 info_ptr += 8;
19573 break;
55f1336d 19574 case DW_FORM_ref_sig8:
ac9ec31b 19575 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19576 info_ptr += 8;
19577 break;
c906108c 19578 case DW_FORM_ref_udata:
9c541725 19579 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19580 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19581 info_ptr += bytes_read;
19582 break;
c906108c 19583 case DW_FORM_indirect:
a8329558
KW
19584 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19585 info_ptr += bytes_read;
43988095
JK
19586 if (form == DW_FORM_implicit_const)
19587 {
19588 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19589 info_ptr += bytes_read;
19590 }
19591 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19592 info_ptr);
19593 break;
19594 case DW_FORM_implicit_const:
19595 DW_SND (attr) = implicit_const;
a8329558 19596 break;
336d760d 19597 case DW_FORM_addrx:
3019eac3
DE
19598 case DW_FORM_GNU_addr_index:
19599 if (reader->dwo_file == NULL)
19600 {
19601 /* For now flag a hard error.
19602 Later we can turn this into a complaint. */
19603 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19604 dwarf_form_name (form),
19605 bfd_get_filename (abfd));
19606 }
19607 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19608 info_ptr += bytes_read;
19609 break;
cf532bd1 19610 case DW_FORM_strx:
15f18d14
AT
19611 case DW_FORM_strx1:
19612 case DW_FORM_strx2:
19613 case DW_FORM_strx3:
19614 case DW_FORM_strx4:
3019eac3
DE
19615 case DW_FORM_GNU_str_index:
19616 if (reader->dwo_file == NULL)
19617 {
19618 /* For now flag a hard error.
19619 Later we can turn this into a complaint if warranted. */
19620 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19621 dwarf_form_name (form),
19622 bfd_get_filename (abfd));
19623 }
19624 {
15f18d14
AT
19625 ULONGEST str_index;
19626 if (form == DW_FORM_strx1)
19627 {
19628 str_index = read_1_byte (abfd, info_ptr);
19629 info_ptr += 1;
19630 }
19631 else if (form == DW_FORM_strx2)
19632 {
19633 str_index = read_2_bytes (abfd, info_ptr);
19634 info_ptr += 2;
19635 }
19636 else if (form == DW_FORM_strx3)
19637 {
19638 str_index = read_3_bytes (abfd, info_ptr);
19639 info_ptr += 3;
19640 }
19641 else if (form == DW_FORM_strx4)
19642 {
19643 str_index = read_4_bytes (abfd, info_ptr);
19644 info_ptr += 4;
19645 }
19646 else
19647 {
19648 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19649 info_ptr += bytes_read;
19650 }
342587c4 19651 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19652 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19653 }
19654 break;
c906108c 19655 default:
8a3fe4f8 19656 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19657 dwarf_form_name (form),
19658 bfd_get_filename (abfd));
c906108c 19659 }
28e94949 19660
36586728 19661 /* Super hack. */
7771576e 19662 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19663 attr->form = DW_FORM_GNU_ref_alt;
19664
28e94949
JB
19665 /* We have seen instances where the compiler tried to emit a byte
19666 size attribute of -1 which ended up being encoded as an unsigned
19667 0xffffffff. Although 0xffffffff is technically a valid size value,
19668 an object of this size seems pretty unlikely so we can relatively
19669 safely treat these cases as if the size attribute was invalid and
19670 treat them as zero by default. */
19671 if (attr->name == DW_AT_byte_size
19672 && form == DW_FORM_data4
19673 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19674 {
19675 complaint
b98664d3 19676 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19677 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19678 DW_UNSND (attr) = 0;
19679 }
28e94949 19680
c906108c
SS
19681 return info_ptr;
19682}
19683
a8329558
KW
19684/* Read an attribute described by an abbreviated attribute. */
19685
d521ce57 19686static const gdb_byte *
dee91e82
DE
19687read_attribute (const struct die_reader_specs *reader,
19688 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19689 const gdb_byte *info_ptr)
a8329558
KW
19690{
19691 attr->name = abbrev->name;
43988095
JK
19692 return read_attribute_value (reader, attr, abbrev->form,
19693 abbrev->implicit_const, info_ptr);
a8329558
KW
19694}
19695
0963b4bd 19696/* Read dwarf information from a buffer. */
c906108c
SS
19697
19698static unsigned int
a1855c1d 19699read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19700{
fe1b8b76 19701 return bfd_get_8 (abfd, buf);
c906108c
SS
19702}
19703
19704static int
a1855c1d 19705read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19706{
fe1b8b76 19707 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19708}
19709
19710static unsigned int
a1855c1d 19711read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19712{
fe1b8b76 19713 return bfd_get_16 (abfd, buf);
c906108c
SS
19714}
19715
21ae7a4d 19716static int
a1855c1d 19717read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19718{
19719 return bfd_get_signed_16 (abfd, buf);
19720}
19721
15f18d14
AT
19722static unsigned int
19723read_3_bytes (bfd *abfd, const gdb_byte *buf)
19724{
19725 unsigned int result = 0;
19726 for (int i = 0; i < 3; ++i)
19727 {
19728 unsigned char byte = bfd_get_8 (abfd, buf);
19729 buf++;
19730 result |= ((unsigned int) byte << (i * 8));
19731 }
19732 return result;
19733}
19734
c906108c 19735static unsigned int
a1855c1d 19736read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19737{
fe1b8b76 19738 return bfd_get_32 (abfd, buf);
c906108c
SS
19739}
19740
21ae7a4d 19741static int
a1855c1d 19742read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19743{
19744 return bfd_get_signed_32 (abfd, buf);
19745}
19746
93311388 19747static ULONGEST
a1855c1d 19748read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19749{
fe1b8b76 19750 return bfd_get_64 (abfd, buf);
c906108c
SS
19751}
19752
19753static CORE_ADDR
d521ce57 19754read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19755 unsigned int *bytes_read)
c906108c 19756{
e7c27a73 19757 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19758 CORE_ADDR retval = 0;
19759
107d2387 19760 if (cu_header->signed_addr_p)
c906108c 19761 {
107d2387
AC
19762 switch (cu_header->addr_size)
19763 {
19764 case 2:
fe1b8b76 19765 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19766 break;
19767 case 4:
fe1b8b76 19768 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19769 break;
19770 case 8:
fe1b8b76 19771 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19772 break;
19773 default:
8e65ff28 19774 internal_error (__FILE__, __LINE__,
e2e0b3e5 19775 _("read_address: bad switch, signed [in module %s]"),
659b0389 19776 bfd_get_filename (abfd));
107d2387
AC
19777 }
19778 }
19779 else
19780 {
19781 switch (cu_header->addr_size)
19782 {
19783 case 2:
fe1b8b76 19784 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19785 break;
19786 case 4:
fe1b8b76 19787 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19788 break;
19789 case 8:
fe1b8b76 19790 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19791 break;
19792 default:
8e65ff28 19793 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19794 _("read_address: bad switch, "
19795 "unsigned [in module %s]"),
659b0389 19796 bfd_get_filename (abfd));
107d2387 19797 }
c906108c 19798 }
64367e0a 19799
107d2387
AC
19800 *bytes_read = cu_header->addr_size;
19801 return retval;
c906108c
SS
19802}
19803
f7ef9339 19804/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19805 specification allows the initial length to take up either 4 bytes
19806 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19807 bytes describe the length and all offsets will be 8 bytes in length
19808 instead of 4.
19809
f7ef9339
KB
19810 An older, non-standard 64-bit format is also handled by this
19811 function. The older format in question stores the initial length
19812 as an 8-byte quantity without an escape value. Lengths greater
19813 than 2^32 aren't very common which means that the initial 4 bytes
19814 is almost always zero. Since a length value of zero doesn't make
19815 sense for the 32-bit format, this initial zero can be considered to
19816 be an escape value which indicates the presence of the older 64-bit
19817 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19818 greater than 4GB. If it becomes necessary to handle lengths
19819 somewhat larger than 4GB, we could allow other small values (such
19820 as the non-sensical values of 1, 2, and 3) to also be used as
19821 escape values indicating the presence of the old format.
f7ef9339 19822
917c78fc
MK
19823 The value returned via bytes_read should be used to increment the
19824 relevant pointer after calling read_initial_length().
c764a876 19825
613e1657
KB
19826 [ Note: read_initial_length() and read_offset() are based on the
19827 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19828 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19829 from:
19830
f7ef9339 19831 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19832
613e1657
KB
19833 This document is only a draft and is subject to change. (So beware.)
19834
f7ef9339 19835 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19836 determined empirically by examining 64-bit ELF files produced by
19837 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19838
19839 - Kevin, July 16, 2002
613e1657
KB
19840 ] */
19841
19842static LONGEST
d521ce57 19843read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19844{
fe1b8b76 19845 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19846
dd373385 19847 if (length == 0xffffffff)
613e1657 19848 {
fe1b8b76 19849 length = bfd_get_64 (abfd, buf + 4);
613e1657 19850 *bytes_read = 12;
613e1657 19851 }
dd373385 19852 else if (length == 0)
f7ef9339 19853 {
dd373385 19854 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19855 length = bfd_get_64 (abfd, buf);
f7ef9339 19856 *bytes_read = 8;
f7ef9339 19857 }
613e1657
KB
19858 else
19859 {
19860 *bytes_read = 4;
613e1657
KB
19861 }
19862
c764a876
DE
19863 return length;
19864}
dd373385 19865
c764a876
DE
19866/* Cover function for read_initial_length.
19867 Returns the length of the object at BUF, and stores the size of the
19868 initial length in *BYTES_READ and stores the size that offsets will be in
19869 *OFFSET_SIZE.
19870 If the initial length size is not equivalent to that specified in
19871 CU_HEADER then issue a complaint.
19872 This is useful when reading non-comp-unit headers. */
dd373385 19873
c764a876 19874static LONGEST
d521ce57 19875read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19876 const struct comp_unit_head *cu_header,
19877 unsigned int *bytes_read,
19878 unsigned int *offset_size)
19879{
19880 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19881
19882 gdb_assert (cu_header->initial_length_size == 4
19883 || cu_header->initial_length_size == 8
19884 || cu_header->initial_length_size == 12);
19885
19886 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19887 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19888
c764a876 19889 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19890 return length;
613e1657
KB
19891}
19892
19893/* Read an offset from the data stream. The size of the offset is
917c78fc 19894 given by cu_header->offset_size. */
613e1657
KB
19895
19896static LONGEST
d521ce57
TT
19897read_offset (bfd *abfd, const gdb_byte *buf,
19898 const struct comp_unit_head *cu_header,
891d2f0b 19899 unsigned int *bytes_read)
c764a876
DE
19900{
19901 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19902
c764a876
DE
19903 *bytes_read = cu_header->offset_size;
19904 return offset;
19905}
19906
19907/* Read an offset from the data stream. */
19908
19909static LONGEST
d521ce57 19910read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19911{
19912 LONGEST retval = 0;
19913
c764a876 19914 switch (offset_size)
613e1657
KB
19915 {
19916 case 4:
fe1b8b76 19917 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19918 break;
19919 case 8:
fe1b8b76 19920 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19921 break;
19922 default:
8e65ff28 19923 internal_error (__FILE__, __LINE__,
c764a876 19924 _("read_offset_1: bad switch [in module %s]"),
659b0389 19925 bfd_get_filename (abfd));
613e1657
KB
19926 }
19927
917c78fc 19928 return retval;
613e1657
KB
19929}
19930
d521ce57
TT
19931static const gdb_byte *
19932read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19933{
19934 /* If the size of a host char is 8 bits, we can return a pointer
19935 to the buffer, otherwise we have to copy the data to a buffer
19936 allocated on the temporary obstack. */
4bdf3d34 19937 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19938 return buf;
c906108c
SS
19939}
19940
d521ce57
TT
19941static const char *
19942read_direct_string (bfd *abfd, const gdb_byte *buf,
19943 unsigned int *bytes_read_ptr)
c906108c
SS
19944{
19945 /* If the size of a host char is 8 bits, we can return a pointer
19946 to the string, otherwise we have to copy the string to a buffer
19947 allocated on the temporary obstack. */
4bdf3d34 19948 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19949 if (*buf == '\0')
19950 {
19951 *bytes_read_ptr = 1;
19952 return NULL;
19953 }
d521ce57
TT
19954 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19955 return (const char *) buf;
4bdf3d34
JJ
19956}
19957
43988095
JK
19958/* Return pointer to string at section SECT offset STR_OFFSET with error
19959 reporting strings FORM_NAME and SECT_NAME. */
19960
d521ce57 19961static const char *
ed2dc618
SM
19962read_indirect_string_at_offset_from (struct objfile *objfile,
19963 bfd *abfd, LONGEST str_offset,
43988095
JK
19964 struct dwarf2_section_info *sect,
19965 const char *form_name,
19966 const char *sect_name)
19967{
ed2dc618 19968 dwarf2_read_section (objfile, sect);
43988095
JK
19969 if (sect->buffer == NULL)
19970 error (_("%s used without %s section [in module %s]"),
19971 form_name, sect_name, bfd_get_filename (abfd));
19972 if (str_offset >= sect->size)
19973 error (_("%s pointing outside of %s section [in module %s]"),
19974 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19975 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19976 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19977 return NULL;
43988095
JK
19978 return (const char *) (sect->buffer + str_offset);
19979}
19980
19981/* Return pointer to string at .debug_str offset STR_OFFSET. */
19982
19983static const char *
ed2dc618
SM
19984read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19985 bfd *abfd, LONGEST str_offset)
43988095 19986{
ed2dc618
SM
19987 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19988 abfd, str_offset,
43988095
JK
19989 &dwarf2_per_objfile->str,
19990 "DW_FORM_strp", ".debug_str");
19991}
19992
19993/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19994
19995static const char *
ed2dc618
SM
19996read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19997 bfd *abfd, LONGEST str_offset)
43988095 19998{
ed2dc618
SM
19999 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20000 abfd, str_offset,
43988095
JK
20001 &dwarf2_per_objfile->line_str,
20002 "DW_FORM_line_strp",
20003 ".debug_line_str");
c906108c
SS
20004}
20005
36586728
TT
20006/* Read a string at offset STR_OFFSET in the .debug_str section from
20007 the .dwz file DWZ. Throw an error if the offset is too large. If
20008 the string consists of a single NUL byte, return NULL; otherwise
20009 return a pointer to the string. */
20010
d521ce57 20011static const char *
ed2dc618
SM
20012read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20013 LONGEST str_offset)
36586728 20014{
ed2dc618 20015 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
20016
20017 if (dwz->str.buffer == NULL)
20018 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20019 "section [in module %s]"),
00f93c44 20020 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20021 if (str_offset >= dwz->str.size)
20022 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20023 ".debug_str section [in module %s]"),
00f93c44 20024 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20025 gdb_assert (HOST_CHAR_BIT == 8);
20026 if (dwz->str.buffer[str_offset] == '\0')
20027 return NULL;
d521ce57 20028 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
20029}
20030
43988095
JK
20031/* Return pointer to string at .debug_str offset as read from BUF.
20032 BUF is assumed to be in a compilation unit described by CU_HEADER.
20033 Return *BYTES_READ_PTR count of bytes read from BUF. */
20034
d521ce57 20035static const char *
ed2dc618
SM
20036read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20037 const gdb_byte *buf,
cf2c3c16
TT
20038 const struct comp_unit_head *cu_header,
20039 unsigned int *bytes_read_ptr)
20040{
20041 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20042
ed2dc618 20043 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
20044}
20045
43988095
JK
20046/* Return pointer to string at .debug_line_str offset as read from BUF.
20047 BUF is assumed to be in a compilation unit described by CU_HEADER.
20048 Return *BYTES_READ_PTR count of bytes read from BUF. */
20049
20050static const char *
ed2dc618
SM
20051read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20052 bfd *abfd, const gdb_byte *buf,
43988095
JK
20053 const struct comp_unit_head *cu_header,
20054 unsigned int *bytes_read_ptr)
20055{
20056 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20057
ed2dc618
SM
20058 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20059 str_offset);
43988095
JK
20060}
20061
20062ULONGEST
d521ce57 20063read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 20064 unsigned int *bytes_read_ptr)
c906108c 20065{
12df843f 20066 ULONGEST result;
ce5d95e1 20067 unsigned int num_read;
870f88f7 20068 int shift;
c906108c
SS
20069 unsigned char byte;
20070
20071 result = 0;
20072 shift = 0;
20073 num_read = 0;
c906108c
SS
20074 while (1)
20075 {
fe1b8b76 20076 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20077 buf++;
20078 num_read++;
12df843f 20079 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20080 if ((byte & 128) == 0)
20081 {
20082 break;
20083 }
20084 shift += 7;
20085 }
20086 *bytes_read_ptr = num_read;
20087 return result;
20088}
20089
12df843f 20090static LONGEST
d521ce57
TT
20091read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20092 unsigned int *bytes_read_ptr)
c906108c 20093{
4dd1b460 20094 ULONGEST result;
870f88f7 20095 int shift, num_read;
c906108c
SS
20096 unsigned char byte;
20097
20098 result = 0;
20099 shift = 0;
c906108c 20100 num_read = 0;
c906108c
SS
20101 while (1)
20102 {
fe1b8b76 20103 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20104 buf++;
20105 num_read++;
4dd1b460 20106 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20107 shift += 7;
20108 if ((byte & 128) == 0)
20109 {
20110 break;
20111 }
20112 }
77e0b926 20113 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 20114 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
20115 *bytes_read_ptr = num_read;
20116 return result;
20117}
20118
3019eac3
DE
20119/* Given index ADDR_INDEX in .debug_addr, fetch the value.
20120 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20121 ADDR_SIZE is the size of addresses from the CU header. */
20122
20123static CORE_ADDR
ed2dc618
SM
20124read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20125 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
20126{
20127 struct objfile *objfile = dwarf2_per_objfile->objfile;
20128 bfd *abfd = objfile->obfd;
20129 const gdb_byte *info_ptr;
20130
20131 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20132 if (dwarf2_per_objfile->addr.buffer == NULL)
20133 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20134 objfile_name (objfile));
3019eac3
DE
20135 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20136 error (_("DW_FORM_addr_index pointing outside of "
20137 ".debug_addr section [in module %s]"),
4262abfb 20138 objfile_name (objfile));
3019eac3
DE
20139 info_ptr = (dwarf2_per_objfile->addr.buffer
20140 + addr_base + addr_index * addr_size);
20141 if (addr_size == 4)
20142 return bfd_get_32 (abfd, info_ptr);
20143 else
20144 return bfd_get_64 (abfd, info_ptr);
20145}
20146
20147/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20148
20149static CORE_ADDR
20150read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20151{
518817b3
SM
20152 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20153 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20154}
20155
20156/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20157
20158static CORE_ADDR
d521ce57 20159read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20160 unsigned int *bytes_read)
20161{
518817b3 20162 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20163 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20164
20165 return read_addr_index (cu, addr_index);
20166}
20167
20168/* Data structure to pass results from dwarf2_read_addr_index_reader
20169 back to dwarf2_read_addr_index. */
20170
20171struct dwarf2_read_addr_index_data
20172{
20173 ULONGEST addr_base;
20174 int addr_size;
20175};
20176
20177/* die_reader_func for dwarf2_read_addr_index. */
20178
20179static void
20180dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 20181 const gdb_byte *info_ptr,
3019eac3
DE
20182 struct die_info *comp_unit_die,
20183 int has_children,
20184 void *data)
20185{
20186 struct dwarf2_cu *cu = reader->cu;
20187 struct dwarf2_read_addr_index_data *aidata =
20188 (struct dwarf2_read_addr_index_data *) data;
20189
20190 aidata->addr_base = cu->addr_base;
20191 aidata->addr_size = cu->header.addr_size;
20192}
20193
20194/* Given an index in .debug_addr, fetch the value.
20195 NOTE: This can be called during dwarf expression evaluation,
20196 long after the debug information has been read, and thus per_cu->cu
20197 may no longer exist. */
20198
20199CORE_ADDR
20200dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20201 unsigned int addr_index)
20202{
ed2dc618 20203 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
20204 struct dwarf2_cu *cu = per_cu->cu;
20205 ULONGEST addr_base;
20206 int addr_size;
20207
3019eac3
DE
20208 /* We need addr_base and addr_size.
20209 If we don't have PER_CU->cu, we have to get it.
20210 Nasty, but the alternative is storing the needed info in PER_CU,
20211 which at this point doesn't seem justified: it's not clear how frequently
20212 it would get used and it would increase the size of every PER_CU.
20213 Entry points like dwarf2_per_cu_addr_size do a similar thing
20214 so we're not in uncharted territory here.
20215 Alas we need to be a bit more complicated as addr_base is contained
20216 in the DIE.
20217
20218 We don't need to read the entire CU(/TU).
20219 We just need the header and top level die.
a1b64ce1 20220
3019eac3 20221 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20222 For now we skip this optimization. */
3019eac3
DE
20223
20224 if (cu != NULL)
20225 {
20226 addr_base = cu->addr_base;
20227 addr_size = cu->header.addr_size;
20228 }
20229 else
20230 {
20231 struct dwarf2_read_addr_index_data aidata;
20232
a1b64ce1
DE
20233 /* Note: We can't use init_cutu_and_read_dies_simple here,
20234 we need addr_base. */
58f0c718 20235 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 20236 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20237 addr_base = aidata.addr_base;
20238 addr_size = aidata.addr_size;
20239 }
20240
ed2dc618
SM
20241 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20242 addr_size);
3019eac3
DE
20243}
20244
cf532bd1 20245/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 20246 This is only used by the Fission support. */
3019eac3 20247
d521ce57 20248static const char *
342587c4 20249read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20250{
ed2dc618 20251 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20252 struct dwarf2_per_objfile *dwarf2_per_objfile
20253 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20254 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20255 const char *objf_name = objfile_name (objfile);
3019eac3 20256 bfd *abfd = objfile->obfd;
73869dc2
DE
20257 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20258 struct dwarf2_section_info *str_offsets_section =
20259 &reader->dwo_file->sections.str_offsets;
d521ce57 20260 const gdb_byte *info_ptr;
3019eac3 20261 ULONGEST str_offset;
cf532bd1 20262 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20263
73869dc2
DE
20264 dwarf2_read_section (objfile, str_section);
20265 dwarf2_read_section (objfile, str_offsets_section);
20266 if (str_section->buffer == NULL)
57d63ce2 20267 error (_("%s used without .debug_str.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_offsets_section->buffer == NULL)
57d63ce2 20271 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20272 " in CU at offset %s [in module %s]"),
20273 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20274 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20275 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20276 " section in CU at offset %s [in module %s]"),
20277 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20278 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20279 + str_index * cu->header.offset_size);
20280 if (cu->header.offset_size == 4)
20281 str_offset = bfd_get_32 (abfd, info_ptr);
20282 else
20283 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20284 if (str_offset >= str_section->size)
57d63ce2 20285 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20286 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20287 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20288 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20289}
20290
3019eac3
DE
20291/* Return the length of an LEB128 number in BUF. */
20292
20293static int
20294leb128_size (const gdb_byte *buf)
20295{
20296 const gdb_byte *begin = buf;
20297 gdb_byte byte;
20298
20299 while (1)
20300 {
20301 byte = *buf++;
20302 if ((byte & 128) == 0)
20303 return buf - begin;
20304 }
20305}
20306
c906108c 20307static void
e142c38c 20308set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20309{
20310 switch (lang)
20311 {
20312 case DW_LANG_C89:
76bee0cc 20313 case DW_LANG_C99:
0cfd832f 20314 case DW_LANG_C11:
c906108c 20315 case DW_LANG_C:
d1be3247 20316 case DW_LANG_UPC:
e142c38c 20317 cu->language = language_c;
c906108c 20318 break;
9c37b5ae 20319 case DW_LANG_Java:
c906108c 20320 case DW_LANG_C_plus_plus:
0cfd832f
MW
20321 case DW_LANG_C_plus_plus_11:
20322 case DW_LANG_C_plus_plus_14:
e142c38c 20323 cu->language = language_cplus;
c906108c 20324 break;
6aecb9c2
JB
20325 case DW_LANG_D:
20326 cu->language = language_d;
20327 break;
c906108c
SS
20328 case DW_LANG_Fortran77:
20329 case DW_LANG_Fortran90:
b21b22e0 20330 case DW_LANG_Fortran95:
f7de9aab
MW
20331 case DW_LANG_Fortran03:
20332 case DW_LANG_Fortran08:
e142c38c 20333 cu->language = language_fortran;
c906108c 20334 break;
a766d390
DE
20335 case DW_LANG_Go:
20336 cu->language = language_go;
20337 break;
c906108c 20338 case DW_LANG_Mips_Assembler:
e142c38c 20339 cu->language = language_asm;
c906108c
SS
20340 break;
20341 case DW_LANG_Ada83:
8aaf0b47 20342 case DW_LANG_Ada95:
bc5f45f8
JB
20343 cu->language = language_ada;
20344 break;
72019c9c
GM
20345 case DW_LANG_Modula2:
20346 cu->language = language_m2;
20347 break;
fe8e67fd
PM
20348 case DW_LANG_Pascal83:
20349 cu->language = language_pascal;
20350 break;
22566fbd
DJ
20351 case DW_LANG_ObjC:
20352 cu->language = language_objc;
20353 break;
c44af4eb
TT
20354 case DW_LANG_Rust:
20355 case DW_LANG_Rust_old:
20356 cu->language = language_rust;
20357 break;
c906108c
SS
20358 case DW_LANG_Cobol74:
20359 case DW_LANG_Cobol85:
c906108c 20360 default:
e142c38c 20361 cu->language = language_minimal;
c906108c
SS
20362 break;
20363 }
e142c38c 20364 cu->language_defn = language_def (cu->language);
c906108c
SS
20365}
20366
20367/* Return the named attribute or NULL if not there. */
20368
20369static struct attribute *
e142c38c 20370dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20371{
a48e046c 20372 for (;;)
c906108c 20373 {
a48e046c
TT
20374 unsigned int i;
20375 struct attribute *spec = NULL;
20376
20377 for (i = 0; i < die->num_attrs; ++i)
20378 {
20379 if (die->attrs[i].name == name)
20380 return &die->attrs[i];
20381 if (die->attrs[i].name == DW_AT_specification
20382 || die->attrs[i].name == DW_AT_abstract_origin)
20383 spec = &die->attrs[i];
20384 }
20385
20386 if (!spec)
20387 break;
c906108c 20388
f2f0e013 20389 die = follow_die_ref (die, spec, &cu);
f2f0e013 20390 }
c5aa993b 20391
c906108c
SS
20392 return NULL;
20393}
20394
348e048f
DE
20395/* Return the named attribute or NULL if not there,
20396 but do not follow DW_AT_specification, etc.
20397 This is for use in contexts where we're reading .debug_types dies.
20398 Following DW_AT_specification, DW_AT_abstract_origin will take us
20399 back up the chain, and we want to go down. */
20400
20401static struct attribute *
45e58e77 20402dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20403{
20404 unsigned int i;
20405
20406 for (i = 0; i < die->num_attrs; ++i)
20407 if (die->attrs[i].name == name)
20408 return &die->attrs[i];
20409
20410 return NULL;
20411}
20412
7d45c7c3
KB
20413/* Return the string associated with a string-typed attribute, or NULL if it
20414 is either not found or is of an incorrect type. */
20415
20416static const char *
20417dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20418{
20419 struct attribute *attr;
20420 const char *str = NULL;
20421
20422 attr = dwarf2_attr (die, name, cu);
20423
20424 if (attr != NULL)
20425 {
43988095 20426 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20427 || attr->form == DW_FORM_string
cf532bd1 20428 || attr->form == DW_FORM_strx
8fe0f950
AT
20429 || attr->form == DW_FORM_strx1
20430 || attr->form == DW_FORM_strx2
20431 || attr->form == DW_FORM_strx3
20432 || attr->form == DW_FORM_strx4
b3340438 20433 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20434 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20435 str = DW_STRING (attr);
20436 else
b98664d3 20437 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20438 "DIE at %s in module %s"),
20439 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20440 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20441 }
20442
20443 return str;
20444}
20445
a084a2a6 20446/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20447 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20448static const char *
20449dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20450{
20451 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20452 if (dwo_name == nullptr)
20453 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20454 return dwo_name;
20455}
20456
05cf31d1
JB
20457/* Return non-zero iff the attribute NAME is defined for the given DIE,
20458 and holds a non-zero value. This function should only be used for
2dc7f7b3 20459 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20460
20461static int
20462dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20463{
20464 struct attribute *attr = dwarf2_attr (die, name, cu);
20465
20466 return (attr && DW_UNSND (attr));
20467}
20468
3ca72b44 20469static int
e142c38c 20470die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20471{
05cf31d1
JB
20472 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20473 which value is non-zero. However, we have to be careful with
20474 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20475 (via dwarf2_flag_true_p) follows this attribute. So we may
20476 end up accidently finding a declaration attribute that belongs
20477 to a different DIE referenced by the specification attribute,
20478 even though the given DIE does not have a declaration attribute. */
20479 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20480 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20481}
20482
63d06c5c 20483/* Return the die giving the specification for DIE, if there is
f2f0e013 20484 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20485 containing the return value on output. If there is no
20486 specification, but there is an abstract origin, that is
20487 returned. */
63d06c5c
DC
20488
20489static struct die_info *
f2f0e013 20490die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20491{
f2f0e013
DJ
20492 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20493 *spec_cu);
63d06c5c 20494
edb3359d
DJ
20495 if (spec_attr == NULL)
20496 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20497
63d06c5c
DC
20498 if (spec_attr == NULL)
20499 return NULL;
20500 else
f2f0e013 20501 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20502}
c906108c 20503
527f3840
JK
20504/* Stub for free_line_header to match void * callback types. */
20505
20506static void
20507free_line_header_voidp (void *arg)
20508{
9a3c8263 20509 struct line_header *lh = (struct line_header *) arg;
527f3840 20510
fff8551c 20511 delete lh;
527f3840
JK
20512}
20513
fff8551c
PA
20514void
20515line_header::add_include_dir (const char *include_dir)
c906108c 20516{
27e0867f 20517 if (dwarf_line_debug >= 2)
7ba99d21
AT
20518 {
20519 size_t new_size;
20520 if (version >= 5)
20521 new_size = m_include_dirs.size ();
20522 else
20523 new_size = m_include_dirs.size () + 1;
20524 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20525 new_size, include_dir);
20526 }
20527 m_include_dirs.push_back (include_dir);
debd256d 20528}
6e70227d 20529
fff8551c
PA
20530void
20531line_header::add_file_name (const char *name,
ecfb656c 20532 dir_index d_index,
fff8551c
PA
20533 unsigned int mod_time,
20534 unsigned int length)
debd256d 20535{
27e0867f 20536 if (dwarf_line_debug >= 2)
7ba99d21
AT
20537 {
20538 size_t new_size;
20539 if (version >= 5)
20540 new_size = file_names_size ();
20541 else
20542 new_size = file_names_size () + 1;
20543 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20544 new_size, name);
20545 }
20546 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20547}
6e70227d 20548
83769d0b 20549/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20550
20551static struct dwarf2_section_info *
20552get_debug_line_section (struct dwarf2_cu *cu)
20553{
20554 struct dwarf2_section_info *section;
518817b3
SM
20555 struct dwarf2_per_objfile *dwarf2_per_objfile
20556 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20557
20558 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20559 DWO file. */
20560 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20561 section = &cu->dwo_unit->dwo_file->sections.line;
20562 else if (cu->per_cu->is_dwz)
20563 {
ed2dc618 20564 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20565
20566 section = &dwz->line;
20567 }
20568 else
20569 section = &dwarf2_per_objfile->line;
20570
20571 return section;
20572}
20573
43988095
JK
20574/* Read directory or file name entry format, starting with byte of
20575 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20576 entries count and the entries themselves in the described entry
20577 format. */
20578
20579static void
ed2dc618
SM
20580read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20581 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20582 struct line_header *lh,
20583 const struct comp_unit_head *cu_header,
20584 void (*callback) (struct line_header *lh,
20585 const char *name,
ecfb656c 20586 dir_index d_index,
43988095
JK
20587 unsigned int mod_time,
20588 unsigned int length))
20589{
20590 gdb_byte format_count, formati;
20591 ULONGEST data_count, datai;
20592 const gdb_byte *buf = *bufp;
20593 const gdb_byte *format_header_data;
43988095
JK
20594 unsigned int bytes_read;
20595
20596 format_count = read_1_byte (abfd, buf);
20597 buf += 1;
20598 format_header_data = buf;
20599 for (formati = 0; formati < format_count; formati++)
20600 {
20601 read_unsigned_leb128 (abfd, buf, &bytes_read);
20602 buf += bytes_read;
20603 read_unsigned_leb128 (abfd, buf, &bytes_read);
20604 buf += bytes_read;
20605 }
20606
20607 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20608 buf += bytes_read;
20609 for (datai = 0; datai < data_count; datai++)
20610 {
20611 const gdb_byte *format = format_header_data;
20612 struct file_entry fe;
20613
43988095
JK
20614 for (formati = 0; formati < format_count; formati++)
20615 {
ecfb656c 20616 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20617 format += bytes_read;
43988095 20618
ecfb656c 20619 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20620 format += bytes_read;
ecfb656c
PA
20621
20622 gdb::optional<const char *> string;
20623 gdb::optional<unsigned int> uint;
20624
43988095
JK
20625 switch (form)
20626 {
20627 case DW_FORM_string:
ecfb656c 20628 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20629 buf += bytes_read;
20630 break;
20631
20632 case DW_FORM_line_strp:
ed2dc618
SM
20633 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20634 abfd, buf,
ecfb656c
PA
20635 cu_header,
20636 &bytes_read));
43988095
JK
20637 buf += bytes_read;
20638 break;
20639
20640 case DW_FORM_data1:
ecfb656c 20641 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20642 buf += 1;
20643 break;
20644
20645 case DW_FORM_data2:
ecfb656c 20646 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20647 buf += 2;
20648 break;
20649
20650 case DW_FORM_data4:
ecfb656c 20651 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20652 buf += 4;
20653 break;
20654
20655 case DW_FORM_data8:
ecfb656c 20656 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20657 buf += 8;
20658 break;
20659
7ba99d21
AT
20660 case DW_FORM_data16:
20661 /* This is used for MD5, but file_entry does not record MD5s. */
20662 buf += 16;
20663 break;
20664
43988095 20665 case DW_FORM_udata:
ecfb656c 20666 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20667 buf += bytes_read;
20668 break;
20669
20670 case DW_FORM_block:
20671 /* It is valid only for DW_LNCT_timestamp which is ignored by
20672 current GDB. */
20673 break;
20674 }
ecfb656c
PA
20675
20676 switch (content_type)
20677 {
20678 case DW_LNCT_path:
20679 if (string.has_value ())
20680 fe.name = *string;
20681 break;
20682 case DW_LNCT_directory_index:
20683 if (uint.has_value ())
20684 fe.d_index = (dir_index) *uint;
20685 break;
20686 case DW_LNCT_timestamp:
20687 if (uint.has_value ())
20688 fe.mod_time = *uint;
20689 break;
20690 case DW_LNCT_size:
20691 if (uint.has_value ())
20692 fe.length = *uint;
20693 break;
20694 case DW_LNCT_MD5:
20695 break;
20696 default:
b98664d3 20697 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20698 pulongest (content_type));
20699 }
43988095
JK
20700 }
20701
ecfb656c 20702 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20703 }
20704
20705 *bufp = buf;
20706}
20707
debd256d 20708/* Read the statement program header starting at OFFSET in
3019eac3 20709 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20710 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20711 Returns NULL if there is a problem reading the header, e.g., if it
20712 has a version we don't understand.
debd256d
JB
20713
20714 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20715 the returned object point into the dwarf line section buffer,
20716 and must not be freed. */
ae2de4f8 20717
fff8551c 20718static line_header_up
9c541725 20719dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20720{
d521ce57 20721 const gdb_byte *line_ptr;
c764a876 20722 unsigned int bytes_read, offset_size;
debd256d 20723 int i;
d521ce57 20724 const char *cur_dir, *cur_file;
3019eac3
DE
20725 struct dwarf2_section_info *section;
20726 bfd *abfd;
518817b3
SM
20727 struct dwarf2_per_objfile *dwarf2_per_objfile
20728 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20729
36586728 20730 section = get_debug_line_section (cu);
3019eac3
DE
20731 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20732 if (section->buffer == NULL)
debd256d 20733 {
3019eac3 20734 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20735 complaint (_("missing .debug_line.dwo section"));
3019eac3 20736 else
b98664d3 20737 complaint (_("missing .debug_line section"));
debd256d
JB
20738 return 0;
20739 }
20740
fceca515
DE
20741 /* We can't do this until we know the section is non-empty.
20742 Only then do we know we have such a section. */
a32a8923 20743 abfd = get_section_bfd_owner (section);
fceca515 20744
a738430d
MK
20745 /* Make sure that at least there's room for the total_length field.
20746 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20747 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20748 {
4d3c2250 20749 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20750 return 0;
20751 }
20752
fff8551c 20753 line_header_up lh (new line_header ());
debd256d 20754
9c541725 20755 lh->sect_off = sect_off;
527f3840
JK
20756 lh->offset_in_dwz = cu->per_cu->is_dwz;
20757
9c541725 20758 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20759
a738430d 20760 /* Read in the header. */
6e70227d 20761 lh->total_length =
c764a876
DE
20762 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20763 &bytes_read, &offset_size);
debd256d 20764 line_ptr += bytes_read;
7ba99d21
AT
20765
20766 const gdb_byte *start_here = line_ptr;
20767
3019eac3 20768 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20769 {
4d3c2250 20770 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20771 return 0;
20772 }
7ba99d21 20773 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20774 lh->version = read_2_bytes (abfd, line_ptr);
20775 line_ptr += 2;
43988095 20776 if (lh->version > 5)
cd366ee8
DE
20777 {
20778 /* This is a version we don't understand. The format could have
20779 changed in ways we don't handle properly so just punt. */
b98664d3 20780 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20781 return NULL;
20782 }
43988095
JK
20783 if (lh->version >= 5)
20784 {
20785 gdb_byte segment_selector_size;
20786
20787 /* Skip address size. */
20788 read_1_byte (abfd, line_ptr);
20789 line_ptr += 1;
20790
20791 segment_selector_size = read_1_byte (abfd, line_ptr);
20792 line_ptr += 1;
20793 if (segment_selector_size != 0)
20794 {
b98664d3 20795 complaint (_("unsupported segment selector size %u "
43988095
JK
20796 "in .debug_line section"),
20797 segment_selector_size);
20798 return NULL;
20799 }
20800 }
c764a876
DE
20801 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20802 line_ptr += offset_size;
7ba99d21 20803 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20804 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20805 line_ptr += 1;
2dc7f7b3
TT
20806 if (lh->version >= 4)
20807 {
20808 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20809 line_ptr += 1;
20810 }
20811 else
20812 lh->maximum_ops_per_instruction = 1;
20813
20814 if (lh->maximum_ops_per_instruction == 0)
20815 {
20816 lh->maximum_ops_per_instruction = 1;
b98664d3 20817 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20818 "in `.debug_line' section"));
2dc7f7b3
TT
20819 }
20820
debd256d
JB
20821 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20822 line_ptr += 1;
20823 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20824 line_ptr += 1;
20825 lh->line_range = read_1_byte (abfd, line_ptr);
20826 line_ptr += 1;
20827 lh->opcode_base = read_1_byte (abfd, line_ptr);
20828 line_ptr += 1;
fff8551c 20829 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20830
20831 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20832 for (i = 1; i < lh->opcode_base; ++i)
20833 {
20834 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20835 line_ptr += 1;
20836 }
20837
43988095 20838 if (lh->version >= 5)
debd256d 20839 {
43988095 20840 /* Read directory table. */
ed2dc618
SM
20841 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20842 &cu->header,
b926417a 20843 [] (struct line_header *header, const char *name,
ecfb656c 20844 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20845 unsigned int length)
20846 {
b926417a 20847 header->add_include_dir (name);
fff8551c 20848 });
debd256d 20849
43988095 20850 /* Read file name table. */
ed2dc618
SM
20851 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20852 &cu->header,
b926417a 20853 [] (struct line_header *header, const char *name,
ecfb656c 20854 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20855 unsigned int length)
20856 {
b926417a 20857 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20858 });
43988095
JK
20859 }
20860 else
debd256d 20861 {
43988095
JK
20862 /* Read directory table. */
20863 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20864 {
20865 line_ptr += bytes_read;
fff8551c 20866 lh->add_include_dir (cur_dir);
43988095 20867 }
debd256d
JB
20868 line_ptr += bytes_read;
20869
43988095
JK
20870 /* Read file name table. */
20871 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20872 {
ecfb656c
PA
20873 unsigned int mod_time, length;
20874 dir_index d_index;
43988095
JK
20875
20876 line_ptr += bytes_read;
ecfb656c 20877 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20878 line_ptr += bytes_read;
20879 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20880 line_ptr += bytes_read;
20881 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20882 line_ptr += bytes_read;
20883
ecfb656c 20884 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20885 }
20886 line_ptr += bytes_read;
debd256d 20887 }
debd256d 20888
3019eac3 20889 if (line_ptr > (section->buffer + section->size))
b98664d3 20890 complaint (_("line number info header doesn't "
3e43a32a 20891 "fit in `.debug_line' section"));
debd256d 20892
debd256d
JB
20893 return lh;
20894}
c906108c 20895
c6da4cef 20896/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20897 Return the file name of the psymtab for the given file_entry.
c6da4cef 20898 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20899 If space for the result is malloc'd, *NAME_HOLDER will be set.
20900 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20901
d521ce57 20902static const char *
7ba99d21 20903psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
c6da4cef 20904 const struct partial_symtab *pst,
c89b44cd
TT
20905 const char *comp_dir,
20906 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20907{
d521ce57
TT
20908 const char *include_name = fe.name;
20909 const char *include_name_to_compare = include_name;
72b9f47f 20910 const char *pst_filename;
c6da4cef
DE
20911 int file_is_pst;
20912
8c43009f 20913 const char *dir_name = fe.include_dir (lh);
c6da4cef 20914
c89b44cd 20915 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20916 if (!IS_ABSOLUTE_PATH (include_name)
20917 && (dir_name != NULL || comp_dir != NULL))
20918 {
20919 /* Avoid creating a duplicate psymtab for PST.
20920 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20921 Before we do the comparison, however, we need to account
20922 for DIR_NAME and COMP_DIR.
20923 First prepend dir_name (if non-NULL). If we still don't
20924 have an absolute path prepend comp_dir (if non-NULL).
20925 However, the directory we record in the include-file's
20926 psymtab does not contain COMP_DIR (to match the
20927 corresponding symtab(s)).
20928
20929 Example:
20930
20931 bash$ cd /tmp
20932 bash$ gcc -g ./hello.c
20933 include_name = "hello.c"
20934 dir_name = "."
20935 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20936 DW_AT_name = "./hello.c"
20937
20938 */
c6da4cef
DE
20939
20940 if (dir_name != NULL)
20941 {
c89b44cd
TT
20942 name_holder->reset (concat (dir_name, SLASH_STRING,
20943 include_name, (char *) NULL));
20944 include_name = name_holder->get ();
c6da4cef 20945 include_name_to_compare = include_name;
c6da4cef
DE
20946 }
20947 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20948 {
c89b44cd
TT
20949 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20950 include_name, (char *) NULL));
20951 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20952 }
20953 }
20954
20955 pst_filename = pst->filename;
c89b44cd 20956 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20957 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20958 {
c89b44cd
TT
20959 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20960 pst_filename, (char *) NULL));
20961 pst_filename = copied_name.get ();
c6da4cef
DE
20962 }
20963
1e3fad37 20964 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20965
c6da4cef
DE
20966 if (file_is_pst)
20967 return NULL;
20968 return include_name;
20969}
20970
d9b3de22
DE
20971/* State machine to track the state of the line number program. */
20972
6f77053d 20973class lnp_state_machine
d9b3de22 20974{
6f77053d
PA
20975public:
20976 /* Initialize a machine state for the start of a line number
20977 program. */
804d2729
TT
20978 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20979 bool record_lines_p);
6f77053d 20980
8c43009f
PA
20981 file_entry *current_file ()
20982 {
20983 /* lh->file_names is 0-based, but the file name numbers in the
20984 statement program are 1-based. */
6f77053d
PA
20985 return m_line_header->file_name_at (m_file);
20986 }
20987
20988 /* Record the line in the state machine. END_SEQUENCE is true if
20989 we're processing the end of a sequence. */
20990 void record_line (bool end_sequence);
20991
7ab6656f
OJ
20992 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20993 nop-out rest of the lines in this sequence. */
6f77053d
PA
20994 void check_line_address (struct dwarf2_cu *cu,
20995 const gdb_byte *line_ptr,
7ab6656f 20996 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20997
20998 void handle_set_discriminator (unsigned int discriminator)
20999 {
21000 m_discriminator = discriminator;
21001 m_line_has_non_zero_discriminator |= discriminator != 0;
21002 }
21003
21004 /* Handle DW_LNE_set_address. */
21005 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21006 {
21007 m_op_index = 0;
21008 address += baseaddr;
21009 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21010 }
21011
21012 /* Handle DW_LNS_advance_pc. */
21013 void handle_advance_pc (CORE_ADDR adjust);
21014
21015 /* Handle a special opcode. */
21016 void handle_special_opcode (unsigned char op_code);
21017
21018 /* Handle DW_LNS_advance_line. */
21019 void handle_advance_line (int line_delta)
21020 {
21021 advance_line (line_delta);
21022 }
21023
21024 /* Handle DW_LNS_set_file. */
21025 void handle_set_file (file_name_index file);
21026
21027 /* Handle DW_LNS_negate_stmt. */
21028 void handle_negate_stmt ()
21029 {
21030 m_is_stmt = !m_is_stmt;
21031 }
21032
21033 /* Handle DW_LNS_const_add_pc. */
21034 void handle_const_add_pc ();
21035
21036 /* Handle DW_LNS_fixed_advance_pc. */
21037 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21038 {
21039 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21040 m_op_index = 0;
21041 }
21042
21043 /* Handle DW_LNS_copy. */
21044 void handle_copy ()
21045 {
21046 record_line (false);
21047 m_discriminator = 0;
21048 }
21049
21050 /* Handle DW_LNE_end_sequence. */
21051 void handle_end_sequence ()
21052 {
804d2729 21053 m_currently_recording_lines = true;
6f77053d
PA
21054 }
21055
21056private:
21057 /* Advance the line by LINE_DELTA. */
21058 void advance_line (int line_delta)
21059 {
21060 m_line += line_delta;
21061
21062 if (line_delta != 0)
21063 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21064 }
21065
804d2729
TT
21066 struct dwarf2_cu *m_cu;
21067
6f77053d
PA
21068 gdbarch *m_gdbarch;
21069
21070 /* True if we're recording lines.
21071 Otherwise we're building partial symtabs and are just interested in
21072 finding include files mentioned by the line number program. */
21073 bool m_record_lines_p;
21074
8c43009f 21075 /* The line number header. */
6f77053d 21076 line_header *m_line_header;
8c43009f 21077
6f77053d
PA
21078 /* These are part of the standard DWARF line number state machine,
21079 and initialized according to the DWARF spec. */
d9b3de22 21080
6f77053d 21081 unsigned char m_op_index = 0;
7ba99d21
AT
21082 /* The line table index of the current file. */
21083 file_name_index m_file = 1;
6f77053d
PA
21084 unsigned int m_line = 1;
21085
21086 /* These are initialized in the constructor. */
21087
21088 CORE_ADDR m_address;
21089 bool m_is_stmt;
21090 unsigned int m_discriminator;
d9b3de22
DE
21091
21092 /* Additional bits of state we need to track. */
21093
21094 /* The last file that we called dwarf2_start_subfile for.
21095 This is only used for TLLs. */
6f77053d 21096 unsigned int m_last_file = 0;
d9b3de22 21097 /* The last file a line number was recorded for. */
6f77053d 21098 struct subfile *m_last_subfile = NULL;
d9b3de22 21099
804d2729
TT
21100 /* When true, record the lines we decode. */
21101 bool m_currently_recording_lines = false;
d9b3de22
DE
21102
21103 /* The last line number that was recorded, used to coalesce
21104 consecutive entries for the same line. This can happen, for
21105 example, when discriminators are present. PR 17276. */
6f77053d
PA
21106 unsigned int m_last_line = 0;
21107 bool m_line_has_non_zero_discriminator = false;
8c43009f 21108};
d9b3de22 21109
6f77053d
PA
21110void
21111lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21112{
21113 CORE_ADDR addr_adj = (((m_op_index + adjust)
21114 / m_line_header->maximum_ops_per_instruction)
21115 * m_line_header->minimum_instruction_length);
21116 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21117 m_op_index = ((m_op_index + adjust)
21118 % m_line_header->maximum_ops_per_instruction);
21119}
d9b3de22 21120
6f77053d
PA
21121void
21122lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21123{
6f77053d
PA
21124 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21125 CORE_ADDR addr_adj = (((m_op_index
21126 + (adj_opcode / m_line_header->line_range))
21127 / m_line_header->maximum_ops_per_instruction)
21128 * m_line_header->minimum_instruction_length);
21129 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21130 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21131 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21132
6f77053d
PA
21133 int line_delta = (m_line_header->line_base
21134 + (adj_opcode % m_line_header->line_range));
21135 advance_line (line_delta);
21136 record_line (false);
21137 m_discriminator = 0;
21138}
d9b3de22 21139
6f77053d
PA
21140void
21141lnp_state_machine::handle_set_file (file_name_index file)
21142{
21143 m_file = file;
21144
21145 const file_entry *fe = current_file ();
21146 if (fe == NULL)
21147 dwarf2_debug_line_missing_file_complaint ();
21148 else if (m_record_lines_p)
21149 {
21150 const char *dir = fe->include_dir (m_line_header);
21151
c24bdb02 21152 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21153 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21154 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21155 }
21156}
21157
21158void
21159lnp_state_machine::handle_const_add_pc ()
21160{
21161 CORE_ADDR adjust
21162 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21163
21164 CORE_ADDR addr_adj
21165 = (((m_op_index + adjust)
21166 / m_line_header->maximum_ops_per_instruction)
21167 * m_line_header->minimum_instruction_length);
21168
21169 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21170 m_op_index = ((m_op_index + adjust)
21171 % m_line_header->maximum_ops_per_instruction);
21172}
d9b3de22 21173
a05a36a5
DE
21174/* Return non-zero if we should add LINE to the line number table.
21175 LINE is the line to add, LAST_LINE is the last line that was added,
21176 LAST_SUBFILE is the subfile for LAST_LINE.
21177 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21178 had a non-zero discriminator.
21179
21180 We have to be careful in the presence of discriminators.
21181 E.g., for this line:
21182
21183 for (i = 0; i < 100000; i++);
21184
21185 clang can emit four line number entries for that one line,
21186 each with a different discriminator.
21187 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21188
21189 However, we want gdb to coalesce all four entries into one.
21190 Otherwise the user could stepi into the middle of the line and
21191 gdb would get confused about whether the pc really was in the
21192 middle of the line.
21193
21194 Things are further complicated by the fact that two consecutive
21195 line number entries for the same line is a heuristic used by gcc
21196 to denote the end of the prologue. So we can't just discard duplicate
21197 entries, we have to be selective about it. The heuristic we use is
21198 that we only collapse consecutive entries for the same line if at least
21199 one of those entries has a non-zero discriminator. PR 17276.
21200
21201 Note: Addresses in the line number state machine can never go backwards
21202 within one sequence, thus this coalescing is ok. */
21203
21204static int
804d2729
TT
21205dwarf_record_line_p (struct dwarf2_cu *cu,
21206 unsigned int line, unsigned int last_line,
a05a36a5
DE
21207 int line_has_non_zero_discriminator,
21208 struct subfile *last_subfile)
21209{
c24bdb02 21210 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21211 return 1;
21212 if (line != last_line)
21213 return 1;
21214 /* Same line for the same file that we've seen already.
21215 As a last check, for pr 17276, only record the line if the line
21216 has never had a non-zero discriminator. */
21217 if (!line_has_non_zero_discriminator)
21218 return 1;
21219 return 0;
21220}
21221
804d2729
TT
21222/* Use the CU's builder to record line number LINE beginning at
21223 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21224
21225static void
d9b3de22
DE
21226dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21227 unsigned int line, CORE_ADDR address,
804d2729 21228 struct dwarf2_cu *cu)
252a6764
DE
21229{
21230 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21231
27e0867f
DE
21232 if (dwarf_line_debug)
21233 {
21234 fprintf_unfiltered (gdb_stdlog,
21235 "Recording line %u, file %s, address %s\n",
21236 line, lbasename (subfile->name),
21237 paddress (gdbarch, address));
21238 }
21239
804d2729 21240 if (cu != nullptr)
c24bdb02 21241 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21242}
21243
21244/* Subroutine of dwarf_decode_lines_1 to simplify it.
21245 Mark the end of a set of line number records.
d9b3de22 21246 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21247 If SUBFILE is NULL the request is ignored. */
21248
21249static void
21250dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21251 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21252{
27e0867f
DE
21253 if (subfile == NULL)
21254 return;
21255
21256 if (dwarf_line_debug)
21257 {
21258 fprintf_unfiltered (gdb_stdlog,
21259 "Finishing current line, file %s, address %s\n",
21260 lbasename (subfile->name),
21261 paddress (gdbarch, address));
21262 }
21263
804d2729 21264 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21265}
21266
6f77053d
PA
21267void
21268lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21269{
d9b3de22
DE
21270 if (dwarf_line_debug)
21271 {
21272 fprintf_unfiltered (gdb_stdlog,
21273 "Processing actual line %u: file %u,"
21274 " address %s, is_stmt %u, discrim %u\n",
7ba99d21 21275 m_line, m_file,
6f77053d
PA
21276 paddress (m_gdbarch, m_address),
21277 m_is_stmt, m_discriminator);
d9b3de22
DE
21278 }
21279
6f77053d 21280 file_entry *fe = current_file ();
8c43009f
PA
21281
21282 if (fe == NULL)
d9b3de22
DE
21283 dwarf2_debug_line_missing_file_complaint ();
21284 /* For now we ignore lines not starting on an instruction boundary.
21285 But not when processing end_sequence for compatibility with the
21286 previous version of the code. */
6f77053d 21287 else if (m_op_index == 0 || end_sequence)
d9b3de22 21288 {
8c43009f 21289 fe->included_p = 1;
c258c396 21290 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21291 {
c24bdb02 21292 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21293 || end_sequence)
d9b3de22 21294 {
804d2729
TT
21295 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21296 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21297 }
21298
21299 if (!end_sequence)
21300 {
804d2729 21301 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21302 m_line_has_non_zero_discriminator,
21303 m_last_subfile))
d9b3de22 21304 {
c24bdb02 21305 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21306 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21307 builder->get_current_subfile (),
6f77053d 21308 m_line, m_address,
804d2729 21309 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21310 }
c24bdb02 21311 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21312 m_last_line = m_line;
d9b3de22
DE
21313 }
21314 }
21315 }
21316}
21317
804d2729
TT
21318lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21319 line_header *lh, bool record_lines_p)
d9b3de22 21320{
804d2729 21321 m_cu = cu;
6f77053d
PA
21322 m_gdbarch = arch;
21323 m_record_lines_p = record_lines_p;
21324 m_line_header = lh;
d9b3de22 21325
804d2729 21326 m_currently_recording_lines = true;
d9b3de22 21327
d9b3de22
DE
21328 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21329 was a line entry for it so that the backend has a chance to adjust it
21330 and also record it in case it needs it. This is currently used by MIPS
21331 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21332 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21333 m_is_stmt = lh->default_is_stmt;
21334 m_discriminator = 0;
252a6764
DE
21335}
21336
6f77053d
PA
21337void
21338lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21339 const gdb_byte *line_ptr,
7ab6656f 21340 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21341{
7ab6656f
OJ
21342 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21343 the pc range of the CU. However, we restrict the test to only ADDRESS
21344 values of zero to preserve GDB's previous behaviour which is to handle
21345 the specific case of a function being GC'd by the linker. */
924c2928 21346
7ab6656f 21347 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21348 {
21349 /* This line table is for a function which has been
21350 GCd by the linker. Ignore it. PR gdb/12528 */
21351
518817b3 21352 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21353 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21354
b98664d3 21355 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21356 line_offset, objfile_name (objfile));
804d2729
TT
21357 m_currently_recording_lines = false;
21358 /* Note: m_currently_recording_lines is left as false until we see
21359 DW_LNE_end_sequence. */
924c2928
DE
21360 }
21361}
21362
f3f5162e 21363/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21364 Process the line number information in LH.
21365 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21366 program in order to set included_p for every referenced header. */
debd256d 21367
c906108c 21368static void
43f3e411
DE
21369dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21370 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21371{
d521ce57
TT
21372 const gdb_byte *line_ptr, *extended_end;
21373 const gdb_byte *line_end;
a8c50c1f 21374 unsigned int bytes_read, extended_len;
699ca60a 21375 unsigned char op_code, extended_op;
e142c38c 21376 CORE_ADDR baseaddr;
518817b3 21377 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21378 bfd *abfd = objfile->obfd;
fbf65064 21379 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21380 /* True if we're recording line info (as opposed to building partial
21381 symtabs and just interested in finding include files mentioned by
21382 the line number program). */
21383 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21384
21385 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21386
debd256d
JB
21387 line_ptr = lh->statement_program_start;
21388 line_end = lh->statement_program_end;
c906108c
SS
21389
21390 /* Read the statement sequences until there's nothing left. */
21391 while (line_ptr < line_end)
21392 {
6f77053d
PA
21393 /* The DWARF line number program state machine. Reset the state
21394 machine at the start of each sequence. */
804d2729 21395 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21396 bool end_sequence = false;
d9b3de22 21397
8c43009f 21398 if (record_lines_p)
c906108c 21399 {
8c43009f
PA
21400 /* Start a subfile for the current file of the state
21401 machine. */
21402 const file_entry *fe = state_machine.current_file ();
21403
21404 if (fe != NULL)
804d2729 21405 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21406 }
21407
a738430d 21408 /* Decode the table. */
d9b3de22 21409 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21410 {
21411 op_code = read_1_byte (abfd, line_ptr);
21412 line_ptr += 1;
9aa1fe7e 21413
debd256d 21414 if (op_code >= lh->opcode_base)
6e70227d 21415 {
8e07a239 21416 /* Special opcode. */
6f77053d 21417 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21418 }
21419 else switch (op_code)
c906108c
SS
21420 {
21421 case DW_LNS_extended_op:
3e43a32a
MS
21422 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21423 &bytes_read);
473b7be6 21424 line_ptr += bytes_read;
a8c50c1f 21425 extended_end = line_ptr + extended_len;
c906108c
SS
21426 extended_op = read_1_byte (abfd, line_ptr);
21427 line_ptr += 1;
21428 switch (extended_op)
21429 {
21430 case DW_LNE_end_sequence:
6f77053d
PA
21431 state_machine.handle_end_sequence ();
21432 end_sequence = true;
c906108c
SS
21433 break;
21434 case DW_LNE_set_address:
d9b3de22
DE
21435 {
21436 CORE_ADDR address
21437 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21438 line_ptr += bytes_read;
6f77053d
PA
21439
21440 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21441 lowpc - baseaddr, address);
6f77053d 21442 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21443 }
c906108c
SS
21444 break;
21445 case DW_LNE_define_file:
debd256d 21446 {
d521ce57 21447 const char *cur_file;
ecfb656c
PA
21448 unsigned int mod_time, length;
21449 dir_index dindex;
6e70227d 21450
3e43a32a
MS
21451 cur_file = read_direct_string (abfd, line_ptr,
21452 &bytes_read);
debd256d 21453 line_ptr += bytes_read;
ecfb656c 21454 dindex = (dir_index)
debd256d
JB
21455 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21456 line_ptr += bytes_read;
21457 mod_time =
21458 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21459 line_ptr += bytes_read;
21460 length =
21461 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21462 line_ptr += bytes_read;
ecfb656c 21463 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21464 }
c906108c 21465 break;
d0c6ba3d 21466 case DW_LNE_set_discriminator:
6f77053d
PA
21467 {
21468 /* The discriminator is not interesting to the
21469 debugger; just ignore it. We still need to
21470 check its value though:
21471 if there are consecutive entries for the same
21472 (non-prologue) line we want to coalesce them.
21473 PR 17276. */
21474 unsigned int discr
21475 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21476 line_ptr += bytes_read;
21477
21478 state_machine.handle_set_discriminator (discr);
21479 }
d0c6ba3d 21480 break;
c906108c 21481 default:
b98664d3 21482 complaint (_("mangled .debug_line section"));
debd256d 21483 return;
c906108c 21484 }
a8c50c1f
DJ
21485 /* Make sure that we parsed the extended op correctly. If e.g.
21486 we expected a different address size than the producer used,
21487 we may have read the wrong number of bytes. */
21488 if (line_ptr != extended_end)
21489 {
b98664d3 21490 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21491 return;
21492 }
c906108c
SS
21493 break;
21494 case DW_LNS_copy:
6f77053d 21495 state_machine.handle_copy ();
c906108c
SS
21496 break;
21497 case DW_LNS_advance_pc:
2dc7f7b3
TT
21498 {
21499 CORE_ADDR adjust
21500 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21501 line_ptr += bytes_read;
6f77053d
PA
21502
21503 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21504 }
c906108c
SS
21505 break;
21506 case DW_LNS_advance_line:
a05a36a5
DE
21507 {
21508 int line_delta
21509 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21510 line_ptr += bytes_read;
6f77053d
PA
21511
21512 state_machine.handle_advance_line (line_delta);
a05a36a5 21513 }
c906108c
SS
21514 break;
21515 case DW_LNS_set_file:
d9b3de22 21516 {
6f77053d 21517 file_name_index file
ecfb656c
PA
21518 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21519 &bytes_read);
d9b3de22 21520 line_ptr += bytes_read;
8c43009f 21521
6f77053d 21522 state_machine.handle_set_file (file);
d9b3de22 21523 }
c906108c
SS
21524 break;
21525 case DW_LNS_set_column:
0ad93d4f 21526 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21527 line_ptr += bytes_read;
21528 break;
21529 case DW_LNS_negate_stmt:
6f77053d 21530 state_machine.handle_negate_stmt ();
c906108c
SS
21531 break;
21532 case DW_LNS_set_basic_block:
c906108c 21533 break;
c2c6d25f
JM
21534 /* Add to the address register of the state machine the
21535 address increment value corresponding to special opcode
a738430d
MK
21536 255. I.e., this value is scaled by the minimum
21537 instruction length since special opcode 255 would have
b021a221 21538 scaled the increment. */
c906108c 21539 case DW_LNS_const_add_pc:
6f77053d 21540 state_machine.handle_const_add_pc ();
c906108c
SS
21541 break;
21542 case DW_LNS_fixed_advance_pc:
3e29f34a 21543 {
6f77053d 21544 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21545 line_ptr += 2;
6f77053d
PA
21546
21547 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21548 }
c906108c 21549 break;
9aa1fe7e 21550 default:
a738430d
MK
21551 {
21552 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21553 int i;
a738430d 21554
debd256d 21555 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21556 {
21557 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21558 line_ptr += bytes_read;
21559 }
21560 }
c906108c
SS
21561 }
21562 }
d9b3de22
DE
21563
21564 if (!end_sequence)
21565 dwarf2_debug_line_missing_end_sequence_complaint ();
21566
21567 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21568 in which case we still finish recording the last line). */
6f77053d 21569 state_machine.record_line (true);
c906108c 21570 }
f3f5162e
DE
21571}
21572
21573/* Decode the Line Number Program (LNP) for the given line_header
21574 structure and CU. The actual information extracted and the type
21575 of structures created from the LNP depends on the value of PST.
21576
21577 1. If PST is NULL, then this procedure uses the data from the program
21578 to create all necessary symbol tables, and their linetables.
21579
21580 2. If PST is not NULL, this procedure reads the program to determine
21581 the list of files included by the unit represented by PST, and
21582 builds all the associated partial symbol tables.
21583
21584 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21585 It is used for relative paths in the line table.
21586 NOTE: When processing partial symtabs (pst != NULL),
21587 comp_dir == pst->dirname.
21588
21589 NOTE: It is important that psymtabs have the same file name (via strcmp)
21590 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21591 symtab we don't use it in the name of the psymtabs we create.
21592 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21593 A good testcase for this is mb-inline.exp.
21594
527f3840
JK
21595 LOWPC is the lowest address in CU (or 0 if not known).
21596
21597 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21598 for its PC<->lines mapping information. Otherwise only the filename
21599 table is read in. */
f3f5162e
DE
21600
21601static void
21602dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21603 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21604 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21605{
518817b3 21606 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21607 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21608
527f3840
JK
21609 if (decode_mapping)
21610 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21611
21612 if (decode_for_pst_p)
21613 {
aaa75496
JB
21614 /* Now that we're done scanning the Line Header Program, we can
21615 create the psymtab of each included file. */
7ba99d21
AT
21616 for (auto &file_entry : lh->file_names ())
21617 if (file_entry.included_p == 1)
aaa75496 21618 {
c89b44cd 21619 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21620 const char *include_name =
7ba99d21
AT
21621 psymtab_include_file_name (lh, file_entry, pst,
21622 comp_dir, &name_holder);
c6da4cef 21623 if (include_name != NULL)
aaa75496
JB
21624 dwarf2_create_include_psymtab (include_name, pst, objfile);
21625 }
21626 }
cb1df416
DJ
21627 else
21628 {
21629 /* Make sure a symtab is created for every file, even files
21630 which contain only variables (i.e. no code with associated
21631 line numbers). */
c24bdb02
KS
21632 buildsym_compunit *builder = cu->get_builder ();
21633 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21634
7ba99d21 21635 for (auto &fe : lh->file_names ())
cb1df416 21636 {
804d2729 21637 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21638 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21639 {
c24bdb02 21640 builder->get_current_subfile ()->symtab
804d2729 21641 = allocate_symtab (cust,
c24bdb02 21642 builder->get_current_subfile ()->name);
43f3e411 21643 }
c24bdb02 21644 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21645 }
21646 }
c906108c
SS
21647}
21648
21649/* Start a subfile for DWARF. FILENAME is the name of the file and
21650 DIRNAME the name of the source directory which contains FILENAME
4d663531 21651 or NULL if not known.
c906108c
SS
21652 This routine tries to keep line numbers from identical absolute and
21653 relative file names in a common subfile.
21654
21655 Using the `list' example from the GDB testsuite, which resides in
21656 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21657 of /srcdir/list0.c yields the following debugging information for list0.c:
21658
c5aa993b 21659 DW_AT_name: /srcdir/list0.c
4d663531 21660 DW_AT_comp_dir: /compdir
357e46e7 21661 files.files[0].name: list0.h
c5aa993b 21662 files.files[0].dir: /srcdir
357e46e7 21663 files.files[1].name: list0.c
c5aa993b 21664 files.files[1].dir: /srcdir
c906108c
SS
21665
21666 The line number information for list0.c has to end up in a single
4f1520fb
FR
21667 subfile, so that `break /srcdir/list0.c:1' works as expected.
21668 start_subfile will ensure that this happens provided that we pass the
21669 concatenation of files.files[1].dir and files.files[1].name as the
21670 subfile's name. */
c906108c
SS
21671
21672static void
804d2729
TT
21673dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21674 const char *dirname)
c906108c 21675{
43816ebc 21676 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21677
4d663531 21678 /* In order not to lose the line information directory,
4f1520fb
FR
21679 we concatenate it to the filename when it makes sense.
21680 Note that the Dwarf3 standard says (speaking of filenames in line
21681 information): ``The directory index is ignored for file names
21682 that represent full path names''. Thus ignoring dirname in the
21683 `else' branch below isn't an issue. */
c906108c 21684
d5166ae1 21685 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21686 {
43816ebc
TT
21687 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21688 filename = copy.get ();
d521ce57 21689 }
c906108c 21690
c24bdb02 21691 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21692}
21693
804d2729
TT
21694/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21695 buildsym_compunit constructor. */
f4dc4d17 21696
c24bdb02
KS
21697struct compunit_symtab *
21698dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21699 CORE_ADDR low_pc)
f4dc4d17 21700{
c24bdb02 21701 gdb_assert (m_builder == nullptr);
43f3e411 21702
c24bdb02
KS
21703 m_builder.reset (new struct buildsym_compunit
21704 (per_cu->dwarf2_per_objfile->objfile,
21705 name, comp_dir, language, low_pc));
93b8bea4 21706
c24bdb02 21707 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21708
c24bdb02
KS
21709 get_builder ()->record_debugformat ("DWARF 2");
21710 get_builder ()->record_producer (producer);
f4dc4d17 21711
c24bdb02 21712 processing_has_namespace_info = false;
43f3e411 21713
c24bdb02 21714 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21715}
21716
4c2df51b
DJ
21717static void
21718var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21719 struct dwarf2_cu *cu)
4c2df51b 21720{
518817b3 21721 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21722 struct comp_unit_head *cu_header = &cu->header;
21723
4c2df51b
DJ
21724 /* NOTE drow/2003-01-30: There used to be a comment and some special
21725 code here to turn a symbol with DW_AT_external and a
21726 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21727 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21728 with some versions of binutils) where shared libraries could have
21729 relocations against symbols in their debug information - the
21730 minimal symbol would have the right address, but the debug info
21731 would not. It's no longer necessary, because we will explicitly
21732 apply relocations when we read in the debug information now. */
21733
21734 /* A DW_AT_location attribute with no contents indicates that a
21735 variable has been optimized away. */
21736 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21737 {
f1e6e072 21738 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21739 return;
21740 }
21741
21742 /* Handle one degenerate form of location expression specially, to
21743 preserve GDB's previous behavior when section offsets are
336d760d
AT
21744 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21745 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21746
21747 if (attr_form_is_block (attr)
3019eac3
DE
21748 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21749 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21750 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21751 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21752 && (DW_BLOCK (attr)->size
21753 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21754 {
891d2f0b 21755 unsigned int dummy;
4c2df51b 21756
3019eac3 21757 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21758 SET_SYMBOL_VALUE_ADDRESS (sym,
21759 read_address (objfile->obfd,
21760 DW_BLOCK (attr)->data + 1,
21761 cu, &dummy));
3019eac3 21762 else
38583298
TT
21763 SET_SYMBOL_VALUE_ADDRESS
21764 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21765 &dummy));
f1e6e072 21766 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21767 fixup_symbol_section (sym, objfile);
38583298
TT
21768 SET_SYMBOL_VALUE_ADDRESS (sym,
21769 SYMBOL_VALUE_ADDRESS (sym)
21770 + ANOFFSET (objfile->section_offsets,
21771 SYMBOL_SECTION (sym)));
4c2df51b
DJ
21772 return;
21773 }
21774
21775 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21776 expression evaluator, and use LOC_COMPUTED only when necessary
21777 (i.e. when the value of a register or memory location is
21778 referenced, or a thread-local block, etc.). Then again, it might
21779 not be worthwhile. I'm assuming that it isn't unless performance
21780 or memory numbers show me otherwise. */
21781
f1e6e072 21782 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21783
f1e6e072 21784 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21785 cu->has_loclist = true;
4c2df51b
DJ
21786}
21787
c906108c
SS
21788/* Given a pointer to a DWARF information entry, figure out if we need
21789 to make a symbol table entry for it, and if so, create a new entry
21790 and return a pointer to it.
21791 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21792 used the passed type.
21793 If SPACE is not NULL, use it to hold the new symbol. If it is
21794 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21795
21796static struct symbol *
5e2db402
TT
21797new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21798 struct symbol *space)
c906108c 21799{
518817b3
SM
21800 struct dwarf2_per_objfile *dwarf2_per_objfile
21801 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21802 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21803 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21804 struct symbol *sym = NULL;
15d034d0 21805 const char *name;
c906108c
SS
21806 struct attribute *attr = NULL;
21807 struct attribute *attr2 = NULL;
e142c38c 21808 CORE_ADDR baseaddr;
e37fd15a
SW
21809 struct pending **list_to_add = NULL;
21810
edb3359d 21811 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21812
21813 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21814
94af9270 21815 name = dwarf2_name (die, cu);
c906108c
SS
21816 if (name)
21817 {
94af9270 21818 const char *linkagename;
34eaf542 21819 int suppress_add = 0;
94af9270 21820
34eaf542
TT
21821 if (space)
21822 sym = space;
21823 else
e623cf5d 21824 sym = allocate_symbol (objfile);
c906108c 21825 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21826
21827 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21828 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 21829 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 21830 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 21831
f55ee35c
JK
21832 /* Fortran does not have mangling standard and the mangling does differ
21833 between gfortran, iFort etc. */
21834 if (cu->language == language_fortran
468c0cbb
CB
21835 && symbol_get_demangled_name (sym) == NULL)
21836 symbol_set_demangled_name (sym,
cfc594ee 21837 dwarf2_full_name (name, die, cu),
29df156d 21838 NULL);
f55ee35c 21839
c906108c 21840 /* Default assumptions.
c5aa993b 21841 Use the passed type or decode it from the die. */
176620f1 21842 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21843 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21844 if (type != NULL)
21845 SYMBOL_TYPE (sym) = type;
21846 else
e7c27a73 21847 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21848 attr = dwarf2_attr (die,
21849 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21850 cu);
435d3d88 21851 if (attr != nullptr)
c906108c
SS
21852 {
21853 SYMBOL_LINE (sym) = DW_UNSND (attr);
21854 }
cb1df416 21855
edb3359d
DJ
21856 attr = dwarf2_attr (die,
21857 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21858 cu);
435d3d88 21859 if (attr != nullptr)
cb1df416 21860 {
ecfb656c 21861 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21862 struct file_entry *fe;
9a619af0 21863
ecfb656c
PA
21864 if (cu->line_header != NULL)
21865 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21866 else
21867 fe = NULL;
21868
21869 if (fe == NULL)
b98664d3 21870 complaint (_("file index out of range"));
8c43009f
PA
21871 else
21872 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21873 }
21874
c906108c
SS
21875 switch (die->tag)
21876 {
21877 case DW_TAG_label:
e142c38c 21878 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21879 if (attr != nullptr)
3e29f34a
MR
21880 {
21881 CORE_ADDR addr;
21882
21883 addr = attr_value_as_address (attr);
21884 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21885 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21886 }
0f5238ed
TT
21887 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21888 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21889 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21890 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21891 break;
21892 case DW_TAG_subprogram:
21893 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21894 finish_block. */
f1e6e072 21895 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21896 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21897 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21898 || cu->language == language_ada
21899 || cu->language == language_fortran)
c906108c 21900 {
2cfa0c8d 21901 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21902 Ada and Fortran subprograms, whether marked external or
21903 not, are always stored as a global symbol, because we want
21904 to be able to access them globally. For instance, we want
21905 to be able to break on a nested subprogram without having
21906 to specify the context. */
c24bdb02 21907 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21908 }
21909 else
21910 {
e37fd15a 21911 list_to_add = cu->list_in_scope;
c906108c
SS
21912 }
21913 break;
edb3359d
DJ
21914 case DW_TAG_inlined_subroutine:
21915 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21916 finish_block. */
f1e6e072 21917 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21918 SYMBOL_INLINED (sym) = 1;
481860b3 21919 list_to_add = cu->list_in_scope;
edb3359d 21920 break;
34eaf542
TT
21921 case DW_TAG_template_value_param:
21922 suppress_add = 1;
21923 /* Fall through. */
72929c62 21924 case DW_TAG_constant:
c906108c 21925 case DW_TAG_variable:
254e6b9e 21926 case DW_TAG_member:
0963b4bd
MS
21927 /* Compilation with minimal debug info may result in
21928 variables with missing type entries. Change the
21929 misleading `void' type to something sensible. */
c906108c 21930 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21931 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21932
e142c38c 21933 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21934 /* In the case of DW_TAG_member, we should only be called for
21935 static const members. */
21936 if (die->tag == DW_TAG_member)
21937 {
3863f96c
DE
21938 /* dwarf2_add_field uses die_is_declaration,
21939 so we do the same. */
254e6b9e
DE
21940 gdb_assert (die_is_declaration (die, cu));
21941 gdb_assert (attr);
21942 }
435d3d88 21943 if (attr != nullptr)
c906108c 21944 {
e7c27a73 21945 dwarf2_const_value (attr, sym, cu);
e142c38c 21946 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21947 if (!suppress_add)
34eaf542
TT
21948 {
21949 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21950 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21951 else
e37fd15a 21952 list_to_add = cu->list_in_scope;
34eaf542 21953 }
c906108c
SS
21954 break;
21955 }
e142c38c 21956 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21957 if (attr != nullptr)
c906108c 21958 {
e7c27a73 21959 var_decode_location (attr, sym, cu);
e142c38c 21960 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21961
21962 /* Fortran explicitly imports any global symbols to the local
21963 scope by DW_TAG_common_block. */
21964 if (cu->language == language_fortran && die->parent
21965 && die->parent->tag == DW_TAG_common_block)
21966 attr2 = NULL;
21967
caac4577
JG
21968 if (SYMBOL_CLASS (sym) == LOC_STATIC
21969 && SYMBOL_VALUE_ADDRESS (sym) == 0
21970 && !dwarf2_per_objfile->has_section_at_zero)
21971 {
21972 /* When a static variable is eliminated by the linker,
21973 the corresponding debug information is not stripped
21974 out, but the variable address is set to null;
21975 do not add such variables into symbol table. */
21976 }
21977 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21978 {
4b610737
TT
21979 if (SYMBOL_CLASS (sym) == LOC_STATIC
21980 && (objfile->flags & OBJF_MAINLINE) == 0
21981 && dwarf2_per_objfile->can_copy)
21982 {
21983 /* A global static variable might be subject to
21984 copy relocation. We first check for a local
21985 minsym, though, because maybe the symbol was
21986 marked hidden, in which case this would not
21987 apply. */
21988 bound_minimal_symbol found
21989 = (lookup_minimal_symbol_linkage
987012b8 21990 (sym->linkage_name (), objfile));
4b610737
TT
21991 if (found.minsym != nullptr)
21992 sym->maybe_copied = 1;
21993 }
f55ee35c 21994
1c809c68
TT
21995 /* A variable with DW_AT_external is never static,
21996 but it may be block-scoped. */
804d2729 21997 list_to_add
c24bdb02
KS
21998 = ((cu->list_in_scope
21999 == cu->get_builder ()->get_file_symbols ())
22000 ? cu->get_builder ()->get_global_symbols ()
804d2729 22001 : cu->list_in_scope);
1c809c68 22002 }
c906108c 22003 else
e37fd15a 22004 list_to_add = cu->list_in_scope;
c906108c
SS
22005 }
22006 else
22007 {
22008 /* We do not know the address of this symbol.
c5aa993b
JM
22009 If it is an external symbol and we have type information
22010 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22011 The address of the variable will then be determined from
22012 the minimal symbol table whenever the variable is
22013 referenced. */
e142c38c 22014 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22015
22016 /* Fortran explicitly imports any global symbols to the local
22017 scope by DW_TAG_common_block. */
22018 if (cu->language == language_fortran && die->parent
22019 && die->parent->tag == DW_TAG_common_block)
22020 {
22021 /* SYMBOL_CLASS doesn't matter here because
22022 read_common_block is going to reset it. */
22023 if (!suppress_add)
22024 list_to_add = cu->list_in_scope;
22025 }
22026 else if (attr2 && (DW_UNSND (attr2) != 0)
22027 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22028 {
0fe7935b
DJ
22029 /* A variable with DW_AT_external is never static, but it
22030 may be block-scoped. */
804d2729 22031 list_to_add
c24bdb02
KS
22032 = ((cu->list_in_scope
22033 == cu->get_builder ()->get_file_symbols ())
22034 ? cu->get_builder ()->get_global_symbols ()
804d2729 22035 : cu->list_in_scope);
0fe7935b 22036
f1e6e072 22037 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22038 }
442ddf59
JK
22039 else if (!die_is_declaration (die, cu))
22040 {
22041 /* Use the default LOC_OPTIMIZED_OUT class. */
22042 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22043 if (!suppress_add)
22044 list_to_add = cu->list_in_scope;
442ddf59 22045 }
c906108c
SS
22046 }
22047 break;
22048 case DW_TAG_formal_parameter:
a60f3166
TT
22049 {
22050 /* If we are inside a function, mark this as an argument. If
22051 not, we might be looking at an argument to an inlined function
22052 when we do not have enough information to show inlined frames;
22053 pretend it's a local variable in that case so that the user can
22054 still see it. */
804d2729 22055 struct context_stack *curr
c24bdb02 22056 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22057 if (curr != nullptr && curr->name != nullptr)
22058 SYMBOL_IS_ARGUMENT (sym) = 1;
22059 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22060 if (attr != nullptr)
a60f3166
TT
22061 {
22062 var_decode_location (attr, sym, cu);
22063 }
22064 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22065 if (attr != nullptr)
a60f3166
TT
22066 {
22067 dwarf2_const_value (attr, sym, cu);
22068 }
f346a30d 22069
a60f3166
TT
22070 list_to_add = cu->list_in_scope;
22071 }
c906108c
SS
22072 break;
22073 case DW_TAG_unspecified_parameters:
22074 /* From varargs functions; gdb doesn't seem to have any
22075 interest in this information, so just ignore it for now.
22076 (FIXME?) */
22077 break;
34eaf542
TT
22078 case DW_TAG_template_type_param:
22079 suppress_add = 1;
22080 /* Fall through. */
c906108c 22081 case DW_TAG_class_type:
680b30c7 22082 case DW_TAG_interface_type:
c906108c
SS
22083 case DW_TAG_structure_type:
22084 case DW_TAG_union_type:
72019c9c 22085 case DW_TAG_set_type:
c906108c 22086 case DW_TAG_enumeration_type:
f1e6e072 22087 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22088 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22089
63d06c5c 22090 {
9c37b5ae 22091 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22092 really ever be static objects: otherwise, if you try
22093 to, say, break of a class's method and you're in a file
22094 which doesn't mention that class, it won't work unless
22095 the check for all static symbols in lookup_symbol_aux
22096 saves you. See the OtherFileClass tests in
22097 gdb.c++/namespace.exp. */
22098
e37fd15a 22099 if (!suppress_add)
34eaf542 22100 {
c24bdb02 22101 buildsym_compunit *builder = cu->get_builder ();
804d2729 22102 list_to_add
c24bdb02 22103 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22104 && cu->language == language_cplus
c24bdb02 22105 ? builder->get_global_symbols ()
804d2729 22106 : cu->list_in_scope);
63d06c5c 22107
64382290 22108 /* The semantics of C++ state that "struct foo {
9c37b5ae 22109 ... }" also defines a typedef for "foo". */
64382290 22110 if (cu->language == language_cplus
45280282 22111 || cu->language == language_ada
c44af4eb
TT
22112 || cu->language == language_d
22113 || cu->language == language_rust)
64382290
TT
22114 {
22115 /* The symbol's name is already allocated along
22116 with this objfile, so we don't need to
22117 duplicate it for the type. */
22118 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 22119 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 22120 }
63d06c5c
DC
22121 }
22122 }
c906108c
SS
22123 break;
22124 case DW_TAG_typedef:
f1e6e072 22125 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22126 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22127 list_to_add = cu->list_in_scope;
63d06c5c 22128 break;
c906108c 22129 case DW_TAG_base_type:
a02abb62 22130 case DW_TAG_subrange_type:
f1e6e072 22131 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22132 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22133 list_to_add = cu->list_in_scope;
c906108c
SS
22134 break;
22135 case DW_TAG_enumerator:
e142c38c 22136 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22137 if (attr != nullptr)
c906108c 22138 {
e7c27a73 22139 dwarf2_const_value (attr, sym, cu);
c906108c 22140 }
63d06c5c
DC
22141 {
22142 /* NOTE: carlton/2003-11-10: See comment above in the
22143 DW_TAG_class_type, etc. block. */
22144
804d2729 22145 list_to_add
c24bdb02 22146 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22147 && cu->language == language_cplus
c24bdb02 22148 ? cu->get_builder ()->get_global_symbols ()
804d2729 22149 : cu->list_in_scope);
63d06c5c 22150 }
c906108c 22151 break;
74921315 22152 case DW_TAG_imported_declaration:
5c4e30ca 22153 case DW_TAG_namespace:
f1e6e072 22154 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22155 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22156 break;
530e8392
KB
22157 case DW_TAG_module:
22158 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22159 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22160 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22161 break;
4357ac6c 22162 case DW_TAG_common_block:
f1e6e072 22163 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22164 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22165 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22166 break;
c906108c
SS
22167 default:
22168 /* Not a tag we recognize. Hopefully we aren't processing
22169 trash data, but since we must specifically ignore things
22170 we don't recognize, there is nothing else we should do at
0963b4bd 22171 this point. */
b98664d3 22172 complaint (_("unsupported tag: '%s'"),
4d3c2250 22173 dwarf_tag_name (die->tag));
c906108c
SS
22174 break;
22175 }
df8a16a1 22176
e37fd15a
SW
22177 if (suppress_add)
22178 {
22179 sym->hash_next = objfile->template_symbols;
22180 objfile->template_symbols = sym;
22181 list_to_add = NULL;
22182 }
22183
22184 if (list_to_add != NULL)
d3cb6808 22185 add_symbol_to_list (sym, list_to_add);
e37fd15a 22186
df8a16a1
DJ
22187 /* For the benefit of old versions of GCC, check for anonymous
22188 namespaces based on the demangled name. */
4d4ec4e5 22189 if (!cu->processing_has_namespace_info
94af9270 22190 && cu->language == language_cplus)
c24bdb02 22191 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22192 }
22193 return (sym);
22194}
22195
98bfdba5
PA
22196/* Given an attr with a DW_FORM_dataN value in host byte order,
22197 zero-extend it as appropriate for the symbol's type. The DWARF
22198 standard (v4) is not entirely clear about the meaning of using
22199 DW_FORM_dataN for a constant with a signed type, where the type is
22200 wider than the data. The conclusion of a discussion on the DWARF
22201 list was that this is unspecified. We choose to always zero-extend
22202 because that is the interpretation long in use by GCC. */
c906108c 22203
98bfdba5 22204static gdb_byte *
ff39bb5e 22205dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22206 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22207{
518817b3 22208 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22209 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22210 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22211 LONGEST l = DW_UNSND (attr);
22212
22213 if (bits < sizeof (*value) * 8)
22214 {
22215 l &= ((LONGEST) 1 << bits) - 1;
22216 *value = l;
22217 }
22218 else if (bits == sizeof (*value) * 8)
22219 *value = l;
22220 else
22221 {
224c3ddb 22222 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22223 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22224 return bytes;
22225 }
22226
22227 return NULL;
22228}
22229
22230/* Read a constant value from an attribute. Either set *VALUE, or if
22231 the value does not fit in *VALUE, set *BYTES - either already
22232 allocated on the objfile obstack, or newly allocated on OBSTACK,
22233 or, set *BATON, if we translated the constant to a location
22234 expression. */
22235
22236static void
ff39bb5e 22237dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22238 const char *name, struct obstack *obstack,
22239 struct dwarf2_cu *cu,
d521ce57 22240 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22241 struct dwarf2_locexpr_baton **baton)
22242{
518817b3 22243 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22244 struct comp_unit_head *cu_header = &cu->header;
c906108c 22245 struct dwarf_block *blk;
98bfdba5
PA
22246 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22247 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22248
22249 *value = 0;
22250 *bytes = NULL;
22251 *baton = NULL;
c906108c
SS
22252
22253 switch (attr->form)
22254 {
22255 case DW_FORM_addr:
336d760d 22256 case DW_FORM_addrx:
3019eac3 22257 case DW_FORM_GNU_addr_index:
ac56253d 22258 {
ac56253d
TT
22259 gdb_byte *data;
22260
98bfdba5
PA
22261 if (TYPE_LENGTH (type) != cu_header->addr_size)
22262 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22263 cu_header->addr_size,
98bfdba5 22264 TYPE_LENGTH (type));
ac56253d
TT
22265 /* Symbols of this form are reasonably rare, so we just
22266 piggyback on the existing location code rather than writing
22267 a new implementation of symbol_computed_ops. */
8d749320 22268 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22269 (*baton)->per_cu = cu->per_cu;
22270 gdb_assert ((*baton)->per_cu);
ac56253d 22271
98bfdba5 22272 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22273 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22274 (*baton)->data = data;
ac56253d
TT
22275
22276 data[0] = DW_OP_addr;
22277 store_unsigned_integer (&data[1], cu_header->addr_size,
22278 byte_order, DW_ADDR (attr));
22279 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22280 }
c906108c 22281 break;
4ac36638 22282 case DW_FORM_string:
93b5768b 22283 case DW_FORM_strp:
cf532bd1 22284 case DW_FORM_strx:
3019eac3 22285 case DW_FORM_GNU_str_index:
36586728 22286 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22287 /* DW_STRING is already allocated on the objfile obstack, point
22288 directly to it. */
d521ce57 22289 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22290 break;
c906108c
SS
22291 case DW_FORM_block1:
22292 case DW_FORM_block2:
22293 case DW_FORM_block4:
22294 case DW_FORM_block:
2dc7f7b3 22295 case DW_FORM_exprloc:
0224619f 22296 case DW_FORM_data16:
c906108c 22297 blk = DW_BLOCK (attr);
98bfdba5
PA
22298 if (TYPE_LENGTH (type) != blk->size)
22299 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22300 TYPE_LENGTH (type));
22301 *bytes = blk->data;
c906108c 22302 break;
2df3850c
JM
22303
22304 /* The DW_AT_const_value attributes are supposed to carry the
22305 symbol's value "represented as it would be on the target
22306 architecture." By the time we get here, it's already been
22307 converted to host endianness, so we just need to sign- or
22308 zero-extend it as appropriate. */
22309 case DW_FORM_data1:
3aef2284 22310 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22311 break;
c906108c 22312 case DW_FORM_data2:
3aef2284 22313 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22314 break;
c906108c 22315 case DW_FORM_data4:
3aef2284 22316 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22317 break;
c906108c 22318 case DW_FORM_data8:
3aef2284 22319 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22320 break;
22321
c906108c 22322 case DW_FORM_sdata:
663c44ac 22323 case DW_FORM_implicit_const:
98bfdba5 22324 *value = DW_SND (attr);
2df3850c
JM
22325 break;
22326
c906108c 22327 case DW_FORM_udata:
98bfdba5 22328 *value = DW_UNSND (attr);
c906108c 22329 break;
2df3850c 22330
c906108c 22331 default:
b98664d3 22332 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22333 dwarf_form_name (attr->form));
98bfdba5 22334 *value = 0;
c906108c
SS
22335 break;
22336 }
22337}
22338
2df3850c 22339
98bfdba5
PA
22340/* Copy constant value from an attribute to a symbol. */
22341
2df3850c 22342static void
ff39bb5e 22343dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22344 struct dwarf2_cu *cu)
2df3850c 22345{
518817b3 22346 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22347 LONGEST value;
d521ce57 22348 const gdb_byte *bytes;
98bfdba5 22349 struct dwarf2_locexpr_baton *baton;
2df3850c 22350
98bfdba5 22351 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22352 sym->print_name (),
98bfdba5
PA
22353 &objfile->objfile_obstack, cu,
22354 &value, &bytes, &baton);
2df3850c 22355
98bfdba5
PA
22356 if (baton != NULL)
22357 {
98bfdba5 22358 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22359 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22360 }
22361 else if (bytes != NULL)
22362 {
22363 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22364 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22365 }
22366 else
22367 {
22368 SYMBOL_VALUE (sym) = value;
f1e6e072 22369 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22370 }
2df3850c
JM
22371}
22372
c906108c
SS
22373/* Return the type of the die in question using its DW_AT_type attribute. */
22374
22375static struct type *
e7c27a73 22376die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22377{
c906108c 22378 struct attribute *type_attr;
c906108c 22379
e142c38c 22380 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22381 if (!type_attr)
22382 {
518817b3 22383 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22384 /* A missing DW_AT_type represents a void type. */
518817b3 22385 return objfile_type (objfile)->builtin_void;
c906108c 22386 }
348e048f 22387
673bfd45 22388 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22389}
22390
b4ba55a1
JB
22391/* True iff CU's producer generates GNAT Ada auxiliary information
22392 that allows to find parallel types through that information instead
22393 of having to do expensive parallel lookups by type name. */
22394
22395static int
22396need_gnat_info (struct dwarf2_cu *cu)
22397{
de4cb04a
JB
22398 /* Assume that the Ada compiler was GNAT, which always produces
22399 the auxiliary information. */
22400 return (cu->language == language_ada);
b4ba55a1
JB
22401}
22402
b4ba55a1
JB
22403/* Return the auxiliary type of the die in question using its
22404 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22405 attribute is not present. */
22406
22407static struct type *
22408die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22409{
b4ba55a1 22410 struct attribute *type_attr;
b4ba55a1
JB
22411
22412 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22413 if (!type_attr)
22414 return NULL;
22415
673bfd45 22416 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22417}
22418
22419/* If DIE has a descriptive_type attribute, then set the TYPE's
22420 descriptive type accordingly. */
22421
22422static void
22423set_descriptive_type (struct type *type, struct die_info *die,
22424 struct dwarf2_cu *cu)
22425{
22426 struct type *descriptive_type = die_descriptive_type (die, cu);
22427
22428 if (descriptive_type)
22429 {
22430 ALLOCATE_GNAT_AUX_TYPE (type);
22431 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22432 }
22433}
22434
c906108c
SS
22435/* Return the containing type of the die in question using its
22436 DW_AT_containing_type attribute. */
22437
22438static struct type *
e7c27a73 22439die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22440{
c906108c 22441 struct attribute *type_attr;
518817b3 22442 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22443
e142c38c 22444 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22445 if (!type_attr)
22446 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22447 "[in module %s]"), objfile_name (objfile));
33ac96f0 22448
673bfd45 22449 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22450}
22451
ac9ec31b
DE
22452/* Return an error marker type to use for the ill formed type in DIE/CU. */
22453
22454static struct type *
22455build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22456{
518817b3
SM
22457 struct dwarf2_per_objfile *dwarf2_per_objfile
22458 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22459 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22460 char *saved;
ac9ec31b 22461
528e1572
SM
22462 std::string message
22463 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22464 objfile_name (objfile),
22465 sect_offset_str (cu->header.sect_off),
22466 sect_offset_str (die->sect_off));
efba19b0 22467 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22468
19f392bc 22469 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22470}
22471
673bfd45 22472/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22473 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22474 DW_AT_containing_type.
673bfd45
DE
22475 If there is no type substitute an error marker. */
22476
c906108c 22477static struct type *
ff39bb5e 22478lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22479 struct dwarf2_cu *cu)
c906108c 22480{
518817b3
SM
22481 struct dwarf2_per_objfile *dwarf2_per_objfile
22482 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22483 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22484 struct type *this_type;
22485
ac9ec31b
DE
22486 gdb_assert (attr->name == DW_AT_type
22487 || attr->name == DW_AT_GNAT_descriptive_type
22488 || attr->name == DW_AT_containing_type);
22489
673bfd45
DE
22490 /* First see if we have it cached. */
22491
36586728
TT
22492 if (attr->form == DW_FORM_GNU_ref_alt)
22493 {
22494 struct dwarf2_per_cu_data *per_cu;
9c541725 22495 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22496
ed2dc618
SM
22497 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22498 dwarf2_per_objfile);
9c541725 22499 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22500 }
7771576e 22501 else if (attr_form_is_ref (attr))
673bfd45 22502 {
9c541725 22503 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22504
9c541725 22505 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22506 }
55f1336d 22507 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22508 {
ac9ec31b 22509 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22510
ac9ec31b 22511 return get_signatured_type (die, signature, cu);
673bfd45
DE
22512 }
22513 else
22514 {
b98664d3 22515 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22516 " at %s [in module %s]"),
22517 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22518 objfile_name (objfile));
ac9ec31b 22519 return build_error_marker_type (cu, die);
673bfd45
DE
22520 }
22521
22522 /* If not cached we need to read it in. */
22523
22524 if (this_type == NULL)
22525 {
ac9ec31b 22526 struct die_info *type_die = NULL;
673bfd45
DE
22527 struct dwarf2_cu *type_cu = cu;
22528
7771576e 22529 if (attr_form_is_ref (attr))
ac9ec31b
DE
22530 type_die = follow_die_ref (die, attr, &type_cu);
22531 if (type_die == NULL)
22532 return build_error_marker_type (cu, die);
22533 /* If we find the type now, it's probably because the type came
3019eac3
DE
22534 from an inter-CU reference and the type's CU got expanded before
22535 ours. */
ac9ec31b 22536 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22537 }
22538
22539 /* If we still don't have a type use an error marker. */
22540
22541 if (this_type == NULL)
ac9ec31b 22542 return build_error_marker_type (cu, die);
673bfd45 22543
f792889a 22544 return this_type;
c906108c
SS
22545}
22546
673bfd45
DE
22547/* Return the type in DIE, CU.
22548 Returns NULL for invalid types.
22549
02142a6c 22550 This first does a lookup in die_type_hash,
673bfd45
DE
22551 and only reads the die in if necessary.
22552
22553 NOTE: This can be called when reading in partial or full symbols. */
22554
f792889a 22555static struct type *
e7c27a73 22556read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22557{
f792889a
DJ
22558 struct type *this_type;
22559
22560 this_type = get_die_type (die, cu);
22561 if (this_type)
22562 return this_type;
22563
673bfd45
DE
22564 return read_type_die_1 (die, cu);
22565}
22566
22567/* Read the type in DIE, CU.
22568 Returns NULL for invalid types. */
22569
22570static struct type *
22571read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22572{
22573 struct type *this_type = NULL;
22574
c906108c
SS
22575 switch (die->tag)
22576 {
22577 case DW_TAG_class_type:
680b30c7 22578 case DW_TAG_interface_type:
c906108c
SS
22579 case DW_TAG_structure_type:
22580 case DW_TAG_union_type:
f792889a 22581 this_type = read_structure_type (die, cu);
c906108c
SS
22582 break;
22583 case DW_TAG_enumeration_type:
f792889a 22584 this_type = read_enumeration_type (die, cu);
c906108c
SS
22585 break;
22586 case DW_TAG_subprogram:
22587 case DW_TAG_subroutine_type:
edb3359d 22588 case DW_TAG_inlined_subroutine:
f792889a 22589 this_type = read_subroutine_type (die, cu);
c906108c
SS
22590 break;
22591 case DW_TAG_array_type:
f792889a 22592 this_type = read_array_type (die, cu);
c906108c 22593 break;
72019c9c 22594 case DW_TAG_set_type:
f792889a 22595 this_type = read_set_type (die, cu);
72019c9c 22596 break;
c906108c 22597 case DW_TAG_pointer_type:
f792889a 22598 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22599 break;
22600 case DW_TAG_ptr_to_member_type:
f792889a 22601 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22602 break;
22603 case DW_TAG_reference_type:
4297a3f0
AV
22604 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22605 break;
22606 case DW_TAG_rvalue_reference_type:
22607 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22608 break;
22609 case DW_TAG_const_type:
f792889a 22610 this_type = read_tag_const_type (die, cu);
c906108c
SS
22611 break;
22612 case DW_TAG_volatile_type:
f792889a 22613 this_type = read_tag_volatile_type (die, cu);
c906108c 22614 break;
06d66ee9
TT
22615 case DW_TAG_restrict_type:
22616 this_type = read_tag_restrict_type (die, cu);
22617 break;
c906108c 22618 case DW_TAG_string_type:
f792889a 22619 this_type = read_tag_string_type (die, cu);
c906108c
SS
22620 break;
22621 case DW_TAG_typedef:
f792889a 22622 this_type = read_typedef (die, cu);
c906108c 22623 break;
a02abb62 22624 case DW_TAG_subrange_type:
f792889a 22625 this_type = read_subrange_type (die, cu);
a02abb62 22626 break;
c906108c 22627 case DW_TAG_base_type:
f792889a 22628 this_type = read_base_type (die, cu);
c906108c 22629 break;
81a17f79 22630 case DW_TAG_unspecified_type:
f792889a 22631 this_type = read_unspecified_type (die, cu);
81a17f79 22632 break;
0114d602
DJ
22633 case DW_TAG_namespace:
22634 this_type = read_namespace_type (die, cu);
22635 break;
f55ee35c
JK
22636 case DW_TAG_module:
22637 this_type = read_module_type (die, cu);
22638 break;
a2c2acaf
MW
22639 case DW_TAG_atomic_type:
22640 this_type = read_tag_atomic_type (die, cu);
22641 break;
c906108c 22642 default:
b98664d3 22643 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22644 dwarf_tag_name (die->tag));
c906108c
SS
22645 break;
22646 }
63d06c5c 22647
f792889a 22648 return this_type;
63d06c5c
DC
22649}
22650
abc72ce4
DE
22651/* See if we can figure out if the class lives in a namespace. We do
22652 this by looking for a member function; its demangled name will
22653 contain namespace info, if there is any.
22654 Return the computed name or NULL.
22655 Space for the result is allocated on the objfile's obstack.
22656 This is the full-die version of guess_partial_die_structure_name.
22657 In this case we know DIE has no useful parent. */
22658
43816ebc 22659static const char *
abc72ce4
DE
22660guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22661{
22662 struct die_info *spec_die;
22663 struct dwarf2_cu *spec_cu;
22664 struct die_info *child;
518817b3 22665 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22666
22667 spec_cu = cu;
22668 spec_die = die_specification (die, &spec_cu);
22669 if (spec_die != NULL)
22670 {
22671 die = spec_die;
22672 cu = spec_cu;
22673 }
22674
22675 for (child = die->child;
22676 child != NULL;
22677 child = child->sibling)
22678 {
22679 if (child->tag == DW_TAG_subprogram)
22680 {
73b9be8b 22681 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22682
7d45c7c3 22683 if (linkage_name != NULL)
abc72ce4 22684 {
43816ebc
TT
22685 gdb::unique_xmalloc_ptr<char> actual_name
22686 (language_class_name_from_physname (cu->language_defn,
22687 linkage_name));
22688 const char *name = NULL;
abc72ce4
DE
22689
22690 if (actual_name != NULL)
22691 {
15d034d0 22692 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22693
22694 if (die_name != NULL
43816ebc 22695 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22696 {
22697 /* Strip off the class name from the full name.
22698 We want the prefix. */
22699 int die_name_len = strlen (die_name);
43816ebc
TT
22700 int actual_name_len = strlen (actual_name.get ());
22701 const char *ptr = actual_name.get ();
abc72ce4
DE
22702
22703 /* Test for '::' as a sanity check. */
22704 if (actual_name_len > die_name_len + 2
43816ebc 22705 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22706 name = obstack_strndup (
e3b94546 22707 &objfile->per_bfd->storage_obstack,
43816ebc 22708 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22709 }
22710 }
abc72ce4
DE
22711 return name;
22712 }
22713 }
22714 }
22715
22716 return NULL;
22717}
22718
96408a79
SA
22719/* GCC might emit a nameless typedef that has a linkage name. Determine the
22720 prefix part in such case. See
22721 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22722
a121b7c1 22723static const char *
96408a79
SA
22724anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22725{
22726 struct attribute *attr;
e6a959d6 22727 const char *base;
96408a79
SA
22728
22729 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22730 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22731 return NULL;
22732
7d45c7c3 22733 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22734 return NULL;
22735
73b9be8b 22736 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22737 if (attr == NULL || DW_STRING (attr) == NULL)
22738 return NULL;
22739
22740 /* dwarf2_name had to be already called. */
22741 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22742
22743 /* Strip the base name, keep any leading namespaces/classes. */
22744 base = strrchr (DW_STRING (attr), ':');
22745 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22746 return "";
22747
518817b3 22748 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22749 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22750 DW_STRING (attr),
22751 &base[-1] - DW_STRING (attr));
96408a79
SA
22752}
22753
fdde2d81 22754/* Return the name of the namespace/class that DIE is defined within,
0114d602 22755 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22756
0114d602
DJ
22757 For example, if we're within the method foo() in the following
22758 code:
22759
22760 namespace N {
22761 class C {
22762 void foo () {
22763 }
22764 };
22765 }
22766
22767 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22768
0d5cff50 22769static const char *
e142c38c 22770determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22771{
518817b3
SM
22772 struct dwarf2_per_objfile *dwarf2_per_objfile
22773 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22774 struct die_info *parent, *spec_die;
22775 struct dwarf2_cu *spec_cu;
22776 struct type *parent_type;
a121b7c1 22777 const char *retval;
63d06c5c 22778
9c37b5ae 22779 if (cu->language != language_cplus
c44af4eb
TT
22780 && cu->language != language_fortran && cu->language != language_d
22781 && cu->language != language_rust)
0114d602
DJ
22782 return "";
22783
96408a79
SA
22784 retval = anonymous_struct_prefix (die, cu);
22785 if (retval)
22786 return retval;
22787
0114d602
DJ
22788 /* We have to be careful in the presence of DW_AT_specification.
22789 For example, with GCC 3.4, given the code
22790
22791 namespace N {
22792 void foo() {
22793 // Definition of N::foo.
22794 }
22795 }
22796
22797 then we'll have a tree of DIEs like this:
22798
22799 1: DW_TAG_compile_unit
22800 2: DW_TAG_namespace // N
22801 3: DW_TAG_subprogram // declaration of N::foo
22802 4: DW_TAG_subprogram // definition of N::foo
22803 DW_AT_specification // refers to die #3
22804
22805 Thus, when processing die #4, we have to pretend that we're in
22806 the context of its DW_AT_specification, namely the contex of die
22807 #3. */
22808 spec_cu = cu;
22809 spec_die = die_specification (die, &spec_cu);
22810 if (spec_die == NULL)
22811 parent = die->parent;
22812 else
63d06c5c 22813 {
0114d602
DJ
22814 parent = spec_die->parent;
22815 cu = spec_cu;
63d06c5c 22816 }
0114d602
DJ
22817
22818 if (parent == NULL)
22819 return "";
98bfdba5
PA
22820 else if (parent->building_fullname)
22821 {
22822 const char *name;
22823 const char *parent_name;
22824
22825 /* It has been seen on RealView 2.2 built binaries,
22826 DW_TAG_template_type_param types actually _defined_ as
22827 children of the parent class:
22828
22829 enum E {};
22830 template class <class Enum> Class{};
22831 Class<enum E> class_e;
22832
22833 1: DW_TAG_class_type (Class)
22834 2: DW_TAG_enumeration_type (E)
22835 3: DW_TAG_enumerator (enum1:0)
22836 3: DW_TAG_enumerator (enum2:1)
22837 ...
22838 2: DW_TAG_template_type_param
22839 DW_AT_type DW_FORM_ref_udata (E)
22840
22841 Besides being broken debug info, it can put GDB into an
22842 infinite loop. Consider:
22843
22844 When we're building the full name for Class<E>, we'll start
22845 at Class, and go look over its template type parameters,
22846 finding E. We'll then try to build the full name of E, and
22847 reach here. We're now trying to build the full name of E,
22848 and look over the parent DIE for containing scope. In the
22849 broken case, if we followed the parent DIE of E, we'd again
22850 find Class, and once again go look at its template type
22851 arguments, etc., etc. Simply don't consider such parent die
22852 as source-level parent of this die (it can't be, the language
22853 doesn't allow it), and break the loop here. */
22854 name = dwarf2_name (die, cu);
22855 parent_name = dwarf2_name (parent, cu);
b98664d3 22856 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22857 name ? name : "<unknown>",
22858 parent_name ? parent_name : "<unknown>");
22859 return "";
22860 }
63d06c5c 22861 else
0114d602
DJ
22862 switch (parent->tag)
22863 {
63d06c5c 22864 case DW_TAG_namespace:
0114d602 22865 parent_type = read_type_die (parent, cu);
acebe513
UW
22866 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22867 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22868 Work around this problem here. */
22869 if (cu->language == language_cplus
e86ca25f 22870 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22871 return "";
0114d602 22872 /* We give a name to even anonymous namespaces. */
e86ca25f 22873 return TYPE_NAME (parent_type);
63d06c5c 22874 case DW_TAG_class_type:
680b30c7 22875 case DW_TAG_interface_type:
63d06c5c 22876 case DW_TAG_structure_type:
0114d602 22877 case DW_TAG_union_type:
f55ee35c 22878 case DW_TAG_module:
0114d602 22879 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22880 if (TYPE_NAME (parent_type) != NULL)
22881 return TYPE_NAME (parent_type);
0114d602
DJ
22882 else
22883 /* An anonymous structure is only allowed non-static data
22884 members; no typedefs, no member functions, et cetera.
22885 So it does not need a prefix. */
22886 return "";
abc72ce4 22887 case DW_TAG_compile_unit:
95554aad 22888 case DW_TAG_partial_unit:
abc72ce4
DE
22889 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22890 if (cu->language == language_cplus
fd5866f6 22891 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22892 && die->child != NULL
22893 && (die->tag == DW_TAG_class_type
22894 || die->tag == DW_TAG_structure_type
22895 || die->tag == DW_TAG_union_type))
22896 {
43816ebc 22897 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22898 if (name != NULL)
22899 return name;
22900 }
22901 return "";
0a4b0913
AB
22902 case DW_TAG_subprogram:
22903 /* Nested subroutines in Fortran get a prefix with the name
22904 of the parent's subroutine. */
22905 if (cu->language == language_fortran)
22906 {
22907 if ((die->tag == DW_TAG_subprogram)
22908 && (dwarf2_name (parent, cu) != NULL))
22909 return dwarf2_name (parent, cu);
22910 }
22911 return determine_prefix (parent, cu);
3d567982
TT
22912 case DW_TAG_enumeration_type:
22913 parent_type = read_type_die (parent, cu);
22914 if (TYPE_DECLARED_CLASS (parent_type))
22915 {
e86ca25f
TT
22916 if (TYPE_NAME (parent_type) != NULL)
22917 return TYPE_NAME (parent_type);
3d567982
TT
22918 return "";
22919 }
22920 /* Fall through. */
63d06c5c 22921 default:
8176b9b8 22922 return determine_prefix (parent, cu);
63d06c5c 22923 }
63d06c5c
DC
22924}
22925
3e43a32a
MS
22926/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22927 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22928 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22929 an obconcat, otherwise allocate storage for the result. The CU argument is
22930 used to determine the language and hence, the appropriate separator. */
987504bb 22931
f55ee35c 22932#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22933
22934static char *
f55ee35c
JK
22935typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22936 int physname, struct dwarf2_cu *cu)
63d06c5c 22937{
f55ee35c 22938 const char *lead = "";
5c315b68 22939 const char *sep;
63d06c5c 22940
3e43a32a
MS
22941 if (suffix == NULL || suffix[0] == '\0'
22942 || prefix == NULL || prefix[0] == '\0')
987504bb 22943 sep = "";
45280282
IB
22944 else if (cu->language == language_d)
22945 {
22946 /* For D, the 'main' function could be defined in any module, but it
22947 should never be prefixed. */
22948 if (strcmp (suffix, "D main") == 0)
22949 {
22950 prefix = "";
22951 sep = "";
22952 }
22953 else
22954 sep = ".";
22955 }
f55ee35c
JK
22956 else if (cu->language == language_fortran && physname)
22957 {
22958 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22959 DW_AT_MIPS_linkage_name is preferred and used instead. */
22960
22961 lead = "__";
22962 sep = "_MOD_";
22963 }
987504bb
JJ
22964 else
22965 sep = "::";
63d06c5c 22966
6dd47d34
DE
22967 if (prefix == NULL)
22968 prefix = "";
22969 if (suffix == NULL)
22970 suffix = "";
22971
987504bb
JJ
22972 if (obs == NULL)
22973 {
3e43a32a 22974 char *retval
224c3ddb
SM
22975 = ((char *)
22976 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22977
f55ee35c
JK
22978 strcpy (retval, lead);
22979 strcat (retval, prefix);
6dd47d34
DE
22980 strcat (retval, sep);
22981 strcat (retval, suffix);
63d06c5c
DC
22982 return retval;
22983 }
987504bb
JJ
22984 else
22985 {
22986 /* We have an obstack. */
f55ee35c 22987 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22988 }
63d06c5c
DC
22989}
22990
c906108c
SS
22991/* Return sibling of die, NULL if no sibling. */
22992
f9aca02d 22993static struct die_info *
fba45db2 22994sibling_die (struct die_info *die)
c906108c 22995{
639d11d3 22996 return die->sibling;
c906108c
SS
22997}
22998
71c25dea
TT
22999/* Get name of a die, return NULL if not found. */
23000
15d034d0
TT
23001static const char *
23002dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
23003 struct obstack *obstack)
23004{
23005 if (name && cu->language == language_cplus)
23006 {
2f408ecb 23007 std::string canon_name = cp_canonicalize_string (name);
71c25dea 23008
2f408ecb 23009 if (!canon_name.empty ())
71c25dea 23010 {
2f408ecb 23011 if (canon_name != name)
efba19b0 23012 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
23013 }
23014 }
23015
23016 return name;
c906108c
SS
23017}
23018
96553a0c
DE
23019/* Get name of a die, return NULL if not found.
23020 Anonymous namespaces are converted to their magic string. */
9219021c 23021
15d034d0 23022static const char *
e142c38c 23023dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23024{
23025 struct attribute *attr;
518817b3 23026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 23027
e142c38c 23028 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 23029 if ((!attr || !DW_STRING (attr))
96553a0c 23030 && die->tag != DW_TAG_namespace
53832f31
TT
23031 && die->tag != DW_TAG_class_type
23032 && die->tag != DW_TAG_interface_type
23033 && die->tag != DW_TAG_structure_type
23034 && die->tag != DW_TAG_union_type)
71c25dea
TT
23035 return NULL;
23036
23037 switch (die->tag)
23038 {
23039 case DW_TAG_compile_unit:
95554aad 23040 case DW_TAG_partial_unit:
71c25dea
TT
23041 /* Compilation units have a DW_AT_name that is a filename, not
23042 a source language identifier. */
23043 case DW_TAG_enumeration_type:
23044 case DW_TAG_enumerator:
23045 /* These tags always have simple identifiers already; no need
23046 to canonicalize them. */
23047 return DW_STRING (attr);
907af001 23048
96553a0c
DE
23049 case DW_TAG_namespace:
23050 if (attr != NULL && DW_STRING (attr) != NULL)
23051 return DW_STRING (attr);
23052 return CP_ANONYMOUS_NAMESPACE_STR;
23053
907af001
UW
23054 case DW_TAG_class_type:
23055 case DW_TAG_interface_type:
23056 case DW_TAG_structure_type:
23057 case DW_TAG_union_type:
23058 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23059 structures or unions. These were of the form "._%d" in GCC 4.1,
23060 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23061 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 23062 if (attr && DW_STRING (attr)
61012eef
GB
23063 && (startswith (DW_STRING (attr), "._")
23064 || startswith (DW_STRING (attr), "<anonymous")))
907af001 23065 return NULL;
53832f31
TT
23066
23067 /* GCC might emit a nameless typedef that has a linkage name. See
23068 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23069 if (!attr || DW_STRING (attr) == NULL)
23070 {
73b9be8b 23071 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
23072 if (attr == NULL || DW_STRING (attr) == NULL)
23073 return NULL;
23074
df5c6c50
JK
23075 /* Avoid demangling DW_STRING (attr) the second time on a second
23076 call for the same DIE. */
23077 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 23078 {
43816ebc
TT
23079 gdb::unique_xmalloc_ptr<char> demangled
23080 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23081
e6a959d6 23082 const char *base;
96408a79 23083
53832f31 23084 /* FIXME: we already did this for the partial symbol... */
34a68019 23085 DW_STRING (attr)
021887d8 23086 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 23087 demangled.get ());
53832f31 23088 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
23089
23090 /* Strip any leading namespaces/classes, keep only the base name.
23091 DW_AT_name for named DIEs does not contain the prefixes. */
23092 base = strrchr (DW_STRING (attr), ':');
23093 if (base && base > DW_STRING (attr) && base[-1] == ':')
23094 return &base[1];
23095 else
23096 return DW_STRING (attr);
53832f31
TT
23097 }
23098 }
907af001
UW
23099 break;
23100
71c25dea 23101 default:
907af001
UW
23102 break;
23103 }
23104
23105 if (!DW_STRING_IS_CANONICAL (attr))
23106 {
23107 DW_STRING (attr)
23108 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 23109 &objfile->per_bfd->storage_obstack);
907af001 23110 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 23111 }
907af001 23112 return DW_STRING (attr);
9219021c
DC
23113}
23114
23115/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23116 is none. *EXT_CU is the CU containing DIE on input, and the CU
23117 containing the return value on output. */
9219021c
DC
23118
23119static struct die_info *
f2f0e013 23120dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23121{
23122 struct attribute *attr;
9219021c 23123
f2f0e013 23124 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23125 if (attr == NULL)
23126 return NULL;
23127
f2f0e013 23128 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23129}
23130
fa9c3fa0
TT
23131/* A convenience function that returns an "unknown" DWARF name,
23132 including the value of V. STR is the name of the entity being
23133 printed, e.g., "TAG". */
23134
23135static const char *
23136dwarf_unknown (const char *str, unsigned v)
23137{
23138 char *cell = get_print_cell ();
23139 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23140 return cell;
23141}
23142
c906108c
SS
23143/* Convert a DIE tag into its string name. */
23144
f39c6ffd 23145static const char *
aa1ee363 23146dwarf_tag_name (unsigned tag)
c906108c 23147{
f39c6ffd
TT
23148 const char *name = get_DW_TAG_name (tag);
23149
23150 if (name == NULL)
fa9c3fa0 23151 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
23152
23153 return name;
c906108c
SS
23154}
23155
23156/* Convert a DWARF attribute code into its string name. */
23157
f39c6ffd 23158static const char *
aa1ee363 23159dwarf_attr_name (unsigned attr)
c906108c 23160{
f39c6ffd
TT
23161 const char *name;
23162
c764a876 23163#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
23164 if (attr == DW_AT_MIPS_fde)
23165 return "DW_AT_MIPS_fde";
23166#else
23167 if (attr == DW_AT_HP_block_index)
23168 return "DW_AT_HP_block_index";
c764a876 23169#endif
f39c6ffd
TT
23170
23171 name = get_DW_AT_name (attr);
23172
23173 if (name == NULL)
fa9c3fa0 23174 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
23175
23176 return name;
c906108c
SS
23177}
23178
a084a2a6
AT
23179/* Convert a unit type to corresponding DW_UT name. */
23180
23181static const char *
23182dwarf_unit_type_name (int unit_type) {
23183 switch (unit_type)
23184 {
23185 case 0x01:
23186 return "DW_UT_compile (0x01)";
23187 case 0x02:
23188 return "DW_UT_type (0x02)";
23189 case 0x03:
23190 return "DW_UT_partial (0x03)";
23191 case 0x04:
23192 return "DW_UT_skeleton (0x04)";
23193 case 0x05:
23194 return "DW_UT_split_compile (0x05)";
23195 case 0x06:
23196 return "DW_UT_split_type (0x06)";
23197 case 0x80:
23198 return "DW_UT_lo_user (0x80)";
23199 case 0xff:
23200 return "DW_UT_hi_user (0xff)";
23201 default:
23202 return nullptr;
23203 }
23204}
23205
c906108c
SS
23206/* Convert a DWARF value form code into its string name. */
23207
f39c6ffd 23208static const char *
aa1ee363 23209dwarf_form_name (unsigned form)
c906108c 23210{
f39c6ffd
TT
23211 const char *name = get_DW_FORM_name (form);
23212
23213 if (name == NULL)
fa9c3fa0 23214 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
23215
23216 return name;
c906108c
SS
23217}
23218
a121b7c1 23219static const char *
fba45db2 23220dwarf_bool_name (unsigned mybool)
c906108c
SS
23221{
23222 if (mybool)
23223 return "TRUE";
23224 else
23225 return "FALSE";
23226}
23227
23228/* Convert a DWARF type code into its string name. */
23229
f39c6ffd 23230static const char *
aa1ee363 23231dwarf_type_encoding_name (unsigned enc)
c906108c 23232{
f39c6ffd 23233 const char *name = get_DW_ATE_name (enc);
c906108c 23234
f39c6ffd 23235 if (name == NULL)
fa9c3fa0 23236 return dwarf_unknown ("ATE", enc);
c906108c 23237
f39c6ffd 23238 return name;
c906108c 23239}
c906108c 23240
f9aca02d 23241static void
d97bc12b 23242dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23243{
23244 unsigned int i;
23245
d97bc12b 23246 print_spaces (indent, f);
9d8780f0 23247 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23248 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23249 sect_offset_str (die->sect_off));
d97bc12b
DE
23250
23251 if (die->parent != NULL)
23252 {
23253 print_spaces (indent, f);
9d8780f0
SM
23254 fprintf_unfiltered (f, " parent at offset: %s\n",
23255 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23256 }
23257
23258 print_spaces (indent, f);
23259 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23260 dwarf_bool_name (die->child != NULL));
c906108c 23261
d97bc12b
DE
23262 print_spaces (indent, f);
23263 fprintf_unfiltered (f, " attributes:\n");
23264
c906108c
SS
23265 for (i = 0; i < die->num_attrs; ++i)
23266 {
d97bc12b
DE
23267 print_spaces (indent, f);
23268 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23269 dwarf_attr_name (die->attrs[i].name),
23270 dwarf_form_name (die->attrs[i].form));
d97bc12b 23271
c906108c
SS
23272 switch (die->attrs[i].form)
23273 {
c906108c 23274 case DW_FORM_addr:
336d760d 23275 case DW_FORM_addrx:
3019eac3 23276 case DW_FORM_GNU_addr_index:
d97bc12b 23277 fprintf_unfiltered (f, "address: ");
5af949e3 23278 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23279 break;
23280 case DW_FORM_block2:
23281 case DW_FORM_block4:
23282 case DW_FORM_block:
23283 case DW_FORM_block1:
56eb65bd
SP
23284 fprintf_unfiltered (f, "block: size %s",
23285 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23286 break;
2dc7f7b3 23287 case DW_FORM_exprloc:
56eb65bd
SP
23288 fprintf_unfiltered (f, "expression: size %s",
23289 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23290 break;
0224619f
JK
23291 case DW_FORM_data16:
23292 fprintf_unfiltered (f, "constant of 16 bytes");
23293 break;
4568ecf9
DE
23294 case DW_FORM_ref_addr:
23295 fprintf_unfiltered (f, "ref address: ");
23296 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23297 break;
36586728
TT
23298 case DW_FORM_GNU_ref_alt:
23299 fprintf_unfiltered (f, "alt ref address: ");
23300 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23301 break;
10b3939b
DJ
23302 case DW_FORM_ref1:
23303 case DW_FORM_ref2:
23304 case DW_FORM_ref4:
4568ecf9
DE
23305 case DW_FORM_ref8:
23306 case DW_FORM_ref_udata:
d97bc12b 23307 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23308 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23309 break;
c906108c
SS
23310 case DW_FORM_data1:
23311 case DW_FORM_data2:
23312 case DW_FORM_data4:
ce5d95e1 23313 case DW_FORM_data8:
c906108c
SS
23314 case DW_FORM_udata:
23315 case DW_FORM_sdata:
43bbcdc2
PH
23316 fprintf_unfiltered (f, "constant: %s",
23317 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23318 break;
2dc7f7b3
TT
23319 case DW_FORM_sec_offset:
23320 fprintf_unfiltered (f, "section offset: %s",
23321 pulongest (DW_UNSND (&die->attrs[i])));
23322 break;
55f1336d 23323 case DW_FORM_ref_sig8:
ac9ec31b
DE
23324 fprintf_unfiltered (f, "signature: %s",
23325 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23326 break;
c906108c 23327 case DW_FORM_string:
4bdf3d34 23328 case DW_FORM_strp:
43988095 23329 case DW_FORM_line_strp:
cf532bd1 23330 case DW_FORM_strx:
3019eac3 23331 case DW_FORM_GNU_str_index:
36586728 23332 case DW_FORM_GNU_strp_alt:
8285870a 23333 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23334 DW_STRING (&die->attrs[i])
8285870a
JK
23335 ? DW_STRING (&die->attrs[i]) : "",
23336 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23337 break;
23338 case DW_FORM_flag:
23339 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23340 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23341 else
d97bc12b 23342 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23343 break;
2dc7f7b3
TT
23344 case DW_FORM_flag_present:
23345 fprintf_unfiltered (f, "flag: TRUE");
23346 break;
a8329558 23347 case DW_FORM_indirect:
0963b4bd
MS
23348 /* The reader will have reduced the indirect form to
23349 the "base form" so this form should not occur. */
5f48f8f3 23350 fprintf_unfiltered (f,
3e43a32a 23351 "unexpected attribute form: DW_FORM_indirect");
a8329558 23352 break;
663c44ac
JK
23353 case DW_FORM_implicit_const:
23354 fprintf_unfiltered (f, "constant: %s",
23355 plongest (DW_SND (&die->attrs[i])));
23356 break;
c906108c 23357 default:
d97bc12b 23358 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23359 die->attrs[i].form);
d97bc12b 23360 break;
c906108c 23361 }
d97bc12b 23362 fprintf_unfiltered (f, "\n");
c906108c
SS
23363 }
23364}
23365
f9aca02d 23366static void
d97bc12b 23367dump_die_for_error (struct die_info *die)
c906108c 23368{
d97bc12b
DE
23369 dump_die_shallow (gdb_stderr, 0, die);
23370}
23371
23372static void
23373dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23374{
23375 int indent = level * 4;
23376
23377 gdb_assert (die != NULL);
23378
23379 if (level >= max_level)
23380 return;
23381
23382 dump_die_shallow (f, indent, die);
23383
23384 if (die->child != NULL)
c906108c 23385 {
d97bc12b
DE
23386 print_spaces (indent, f);
23387 fprintf_unfiltered (f, " Children:");
23388 if (level + 1 < max_level)
23389 {
23390 fprintf_unfiltered (f, "\n");
23391 dump_die_1 (f, level + 1, max_level, die->child);
23392 }
23393 else
23394 {
3e43a32a
MS
23395 fprintf_unfiltered (f,
23396 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23397 }
23398 }
23399
23400 if (die->sibling != NULL && level > 0)
23401 {
23402 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23403 }
23404}
23405
d97bc12b
DE
23406/* This is called from the pdie macro in gdbinit.in.
23407 It's not static so gcc will keep a copy callable from gdb. */
23408
23409void
23410dump_die (struct die_info *die, int max_level)
23411{
23412 dump_die_1 (gdb_stdlog, 0, max_level, die);
23413}
23414
f9aca02d 23415static void
51545339 23416store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23417{
51545339 23418 void **slot;
c906108c 23419
9c541725
PA
23420 slot = htab_find_slot_with_hash (cu->die_hash, die,
23421 to_underlying (die->sect_off),
b64f50a1 23422 INSERT);
51545339
DJ
23423
23424 *slot = die;
c906108c
SS
23425}
23426
b64f50a1
JK
23427/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23428 required kind. */
23429
23430static sect_offset
ff39bb5e 23431dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23432{
7771576e 23433 if (attr_form_is_ref (attr))
9c541725 23434 return (sect_offset) DW_UNSND (attr);
93311388 23435
b98664d3 23436 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23437 dwarf_form_name (attr->form));
9c541725 23438 return {};
c906108c
SS
23439}
23440
43bbcdc2
PH
23441/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23442 * the value held by the attribute is not constant. */
a02abb62 23443
43bbcdc2 23444static LONGEST
ff39bb5e 23445dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23446{
663c44ac 23447 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23448 return DW_SND (attr);
23449 else if (attr->form == DW_FORM_udata
23450 || attr->form == DW_FORM_data1
23451 || attr->form == DW_FORM_data2
23452 || attr->form == DW_FORM_data4
23453 || attr->form == DW_FORM_data8)
23454 return DW_UNSND (attr);
23455 else
23456 {
0224619f 23457 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23458 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23459 dwarf_form_name (attr->form));
23460 return default_value;
23461 }
23462}
23463
348e048f
DE
23464/* Follow reference or signature attribute ATTR of SRC_DIE.
23465 On entry *REF_CU is the CU of SRC_DIE.
23466 On exit *REF_CU is the CU of the result. */
23467
23468static struct die_info *
ff39bb5e 23469follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23470 struct dwarf2_cu **ref_cu)
23471{
23472 struct die_info *die;
23473
7771576e 23474 if (attr_form_is_ref (attr))
348e048f 23475 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23476 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23477 die = follow_die_sig (src_die, attr, ref_cu);
23478 else
23479 {
23480 dump_die_for_error (src_die);
23481 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23482 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23483 }
23484
23485 return die;
03dd20cc
DJ
23486}
23487
5c631832 23488/* Follow reference OFFSET.
673bfd45
DE
23489 On entry *REF_CU is the CU of the source die referencing OFFSET.
23490 On exit *REF_CU is the CU of the result.
23491 Returns NULL if OFFSET is invalid. */
f504f079 23492
f9aca02d 23493static struct die_info *
9c541725 23494follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23495 struct dwarf2_cu **ref_cu)
c906108c 23496{
10b3939b 23497 struct die_info temp_die;
f2f0e013 23498 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23499 struct dwarf2_per_objfile *dwarf2_per_objfile
23500 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23501
348e048f
DE
23502 gdb_assert (cu->per_cu != NULL);
23503
98bfdba5
PA
23504 target_cu = cu;
23505
3019eac3 23506 if (cu->per_cu->is_debug_types)
348e048f
DE
23507 {
23508 /* .debug_types CUs cannot reference anything outside their CU.
23509 If they need to, they have to reference a signatured type via
55f1336d 23510 DW_FORM_ref_sig8. */
9c541725 23511 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23512 return NULL;
348e048f 23513 }
36586728 23514 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23515 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23516 {
23517 struct dwarf2_per_cu_data *per_cu;
9a619af0 23518
9c541725 23519 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23520 dwarf2_per_objfile);
03dd20cc
DJ
23521
23522 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23523 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23524 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23525
10b3939b
DJ
23526 target_cu = per_cu->cu;
23527 }
98bfdba5
PA
23528 else if (cu->dies == NULL)
23529 {
23530 /* We're loading full DIEs during partial symbol reading. */
23531 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23532 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23533 }
c906108c 23534
f2f0e013 23535 *ref_cu = target_cu;
9c541725 23536 temp_die.sect_off = sect_off;
c24bdb02
KS
23537
23538 if (target_cu != cu)
23539 target_cu->ancestor = cu;
23540
9a3c8263 23541 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23542 &temp_die,
23543 to_underlying (sect_off));
5c631832 23544}
10b3939b 23545
5c631832
JK
23546/* Follow reference attribute ATTR of SRC_DIE.
23547 On entry *REF_CU is the CU of SRC_DIE.
23548 On exit *REF_CU is the CU of the result. */
23549
23550static struct die_info *
ff39bb5e 23551follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23552 struct dwarf2_cu **ref_cu)
23553{
9c541725 23554 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23555 struct dwarf2_cu *cu = *ref_cu;
23556 struct die_info *die;
23557
9c541725 23558 die = follow_die_offset (sect_off,
36586728
TT
23559 (attr->form == DW_FORM_GNU_ref_alt
23560 || cu->per_cu->is_dwz),
23561 ref_cu);
5c631832 23562 if (!die)
9d8780f0
SM
23563 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23564 "at %s [in module %s]"),
23565 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23566 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23567
5c631832
JK
23568 return die;
23569}
23570
9c541725 23571/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23572 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23573 dwarf2_locexpr_baton->data has lifetime of
23574 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23575
23576struct dwarf2_locexpr_baton
9c541725 23577dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23578 struct dwarf2_per_cu_data *per_cu,
23579 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23580 void *baton, bool resolve_abstract_p)
5c631832 23581{
918dd910 23582 struct dwarf2_cu *cu;
5c631832
JK
23583 struct die_info *die;
23584 struct attribute *attr;
23585 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23586 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23587 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23588
918dd910 23589 if (per_cu->cu == NULL)
58f0c718 23590 load_cu (per_cu, false);
918dd910 23591 cu = per_cu->cu;
cc12ce38
DE
23592 if (cu == NULL)
23593 {
23594 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23595 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23596 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23597 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23598 }
918dd910 23599
9c541725 23600 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23601 if (!die)
9d8780f0
SM
23602 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23603 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23604
23605 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23606 if (!attr && resolve_abstract_p
3360b6e7 23607 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23608 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23609 {
23610 CORE_ADDR pc = (*get_frame_pc) (baton);
eba4caf2
TV
23611 CORE_ADDR baseaddr
23612 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23613 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23614
3360b6e7
TV
23615 for (const auto &cand_off
23616 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23617 {
3360b6e7
TV
23618 struct dwarf2_cu *cand_cu = cu;
23619 struct die_info *cand
23620 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23621 if (!cand
23622 || !cand->parent
e4a62c65
TV
23623 || cand->parent->tag != DW_TAG_subprogram)
23624 continue;
23625
23626 CORE_ADDR pc_low, pc_high;
23627 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23628 if (pc_low == ((CORE_ADDR) -1))
23629 continue;
23630 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23631 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23632 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23633 continue;
23634
23635 die = cand;
23636 attr = dwarf2_attr (die, DW_AT_location, cu);
23637 break;
23638 }
23639 }
23640
5c631832
JK
23641 if (!attr)
23642 {
e103e986
JK
23643 /* DWARF: "If there is no such attribute, then there is no effect.".
23644 DATA is ignored if SIZE is 0. */
5c631832 23645
e103e986 23646 retval.data = NULL;
5c631832
JK
23647 retval.size = 0;
23648 }
8cf6f0b1
TT
23649 else if (attr_form_is_section_offset (attr))
23650 {
23651 struct dwarf2_loclist_baton loclist_baton;
23652 CORE_ADDR pc = (*get_frame_pc) (baton);
23653 size_t size;
23654
23655 fill_in_loclist_baton (cu, &loclist_baton, attr);
23656
23657 retval.data = dwarf2_find_location_expression (&loclist_baton,
23658 &size, pc);
23659 retval.size = size;
23660 }
5c631832
JK
23661 else
23662 {
23663 if (!attr_form_is_block (attr))
9d8780f0 23664 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23665 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23666 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23667
23668 retval.data = DW_BLOCK (attr)->data;
23669 retval.size = DW_BLOCK (attr)->size;
23670 }
23671 retval.per_cu = cu->per_cu;
918dd910 23672
ed2dc618 23673 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23674
5c631832 23675 return retval;
348e048f
DE
23676}
23677
8b9737bf
TT
23678/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23679 offset. */
23680
23681struct dwarf2_locexpr_baton
23682dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23683 struct dwarf2_per_cu_data *per_cu,
23684 CORE_ADDR (*get_frame_pc) (void *baton),
23685 void *baton)
23686{
9c541725 23687 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23688
9c541725 23689 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23690}
23691
b6807d98
TT
23692/* Write a constant of a given type as target-ordered bytes into
23693 OBSTACK. */
23694
23695static const gdb_byte *
23696write_constant_as_bytes (struct obstack *obstack,
23697 enum bfd_endian byte_order,
23698 struct type *type,
23699 ULONGEST value,
23700 LONGEST *len)
23701{
23702 gdb_byte *result;
23703
23704 *len = TYPE_LENGTH (type);
224c3ddb 23705 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23706 store_unsigned_integer (result, *len, byte_order, value);
23707
23708 return result;
23709}
23710
23711/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23712 pointer to the constant bytes and set LEN to the length of the
23713 data. If memory is needed, allocate it on OBSTACK. If the DIE
23714 does not have a DW_AT_const_value, return NULL. */
23715
23716const gdb_byte *
9c541725 23717dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23718 struct dwarf2_per_cu_data *per_cu,
23719 struct obstack *obstack,
23720 LONGEST *len)
23721{
23722 struct dwarf2_cu *cu;
23723 struct die_info *die;
23724 struct attribute *attr;
23725 const gdb_byte *result = NULL;
23726 struct type *type;
23727 LONGEST value;
23728 enum bfd_endian byte_order;
e3b94546 23729 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23730
b6807d98 23731 if (per_cu->cu == NULL)
58f0c718 23732 load_cu (per_cu, false);
b6807d98 23733 cu = per_cu->cu;
cc12ce38
DE
23734 if (cu == NULL)
23735 {
23736 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23737 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23738 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23739 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23740 }
b6807d98 23741
9c541725 23742 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23743 if (!die)
9d8780f0
SM
23744 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23745 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23746
23747 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23748 if (attr == NULL)
23749 return NULL;
23750
e3b94546 23751 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23752 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23753
23754 switch (attr->form)
23755 {
23756 case DW_FORM_addr:
336d760d 23757 case DW_FORM_addrx:
b6807d98
TT
23758 case DW_FORM_GNU_addr_index:
23759 {
23760 gdb_byte *tem;
23761
23762 *len = cu->header.addr_size;
224c3ddb 23763 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23764 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23765 result = tem;
23766 }
23767 break;
23768 case DW_FORM_string:
23769 case DW_FORM_strp:
cf532bd1 23770 case DW_FORM_strx:
b6807d98
TT
23771 case DW_FORM_GNU_str_index:
23772 case DW_FORM_GNU_strp_alt:
23773 /* DW_STRING is already allocated on the objfile obstack, point
23774 directly to it. */
23775 result = (const gdb_byte *) DW_STRING (attr);
23776 *len = strlen (DW_STRING (attr));
23777 break;
23778 case DW_FORM_block1:
23779 case DW_FORM_block2:
23780 case DW_FORM_block4:
23781 case DW_FORM_block:
23782 case DW_FORM_exprloc:
0224619f 23783 case DW_FORM_data16:
b6807d98
TT
23784 result = DW_BLOCK (attr)->data;
23785 *len = DW_BLOCK (attr)->size;
23786 break;
23787
23788 /* The DW_AT_const_value attributes are supposed to carry the
23789 symbol's value "represented as it would be on the target
23790 architecture." By the time we get here, it's already been
23791 converted to host endianness, so we just need to sign- or
23792 zero-extend it as appropriate. */
23793 case DW_FORM_data1:
23794 type = die_type (die, cu);
23795 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23796 if (result == NULL)
23797 result = write_constant_as_bytes (obstack, byte_order,
23798 type, value, len);
23799 break;
23800 case DW_FORM_data2:
23801 type = die_type (die, cu);
23802 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23803 if (result == NULL)
23804 result = write_constant_as_bytes (obstack, byte_order,
23805 type, value, len);
23806 break;
23807 case DW_FORM_data4:
23808 type = die_type (die, cu);
23809 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23810 if (result == NULL)
23811 result = write_constant_as_bytes (obstack, byte_order,
23812 type, value, len);
23813 break;
23814 case DW_FORM_data8:
23815 type = die_type (die, cu);
23816 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23817 if (result == NULL)
23818 result = write_constant_as_bytes (obstack, byte_order,
23819 type, value, len);
23820 break;
23821
23822 case DW_FORM_sdata:
663c44ac 23823 case DW_FORM_implicit_const:
b6807d98
TT
23824 type = die_type (die, cu);
23825 result = write_constant_as_bytes (obstack, byte_order,
23826 type, DW_SND (attr), len);
23827 break;
23828
23829 case DW_FORM_udata:
23830 type = die_type (die, cu);
23831 result = write_constant_as_bytes (obstack, byte_order,
23832 type, DW_UNSND (attr), len);
23833 break;
23834
23835 default:
b98664d3 23836 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23837 dwarf_form_name (attr->form));
23838 break;
23839 }
23840
23841 return result;
23842}
23843
7942e96e
AA
23844/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23845 valid type for this die is found. */
23846
23847struct type *
9c541725 23848dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23849 struct dwarf2_per_cu_data *per_cu)
23850{
23851 struct dwarf2_cu *cu;
23852 struct die_info *die;
23853
7942e96e 23854 if (per_cu->cu == NULL)
58f0c718 23855 load_cu (per_cu, false);
7942e96e
AA
23856 cu = per_cu->cu;
23857 if (!cu)
23858 return NULL;
23859
9c541725 23860 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23861 if (!die)
23862 return NULL;
23863
23864 return die_type (die, cu);
23865}
23866
8a9b8146
TT
23867/* Return the type of the DIE at DIE_OFFSET in the CU named by
23868 PER_CU. */
23869
23870struct type *
b64f50a1 23871dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23872 struct dwarf2_per_cu_data *per_cu)
23873{
9c541725 23874 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23875 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23876}
23877
ac9ec31b 23878/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23879 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23880 On exit *REF_CU is the CU of the result.
23881 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23882
23883static struct die_info *
ac9ec31b
DE
23884follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23885 struct dwarf2_cu **ref_cu)
348e048f 23886{
348e048f 23887 struct die_info temp_die;
c24bdb02 23888 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23889 struct die_info *die;
23890
ac9ec31b
DE
23891 /* While it might be nice to assert sig_type->type == NULL here,
23892 we can get here for DW_AT_imported_declaration where we need
23893 the DIE not the type. */
348e048f
DE
23894
23895 /* If necessary, add it to the queue and load its DIEs. */
23896
95554aad 23897 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23898 read_signatured_type (sig_type);
348e048f 23899
348e048f 23900 sig_cu = sig_type->per_cu.cu;
69d751e3 23901 gdb_assert (sig_cu != NULL);
9c541725
PA
23902 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23903 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23904 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23905 to_underlying (temp_die.sect_off));
348e048f
DE
23906 if (die)
23907 {
ed2dc618 23908 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23909 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23910
796a7ff8
DE
23911 /* For .gdb_index version 7 keep track of included TUs.
23912 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23913 if (dwarf2_per_objfile->index_table != NULL
23914 && dwarf2_per_objfile->index_table->version <= 7)
23915 {
ae640021 23916 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23917 }
23918
348e048f 23919 *ref_cu = sig_cu;
c24bdb02
KS
23920 if (sig_cu != cu)
23921 sig_cu->ancestor = cu;
23922
348e048f
DE
23923 return die;
23924 }
23925
ac9ec31b
DE
23926 return NULL;
23927}
23928
23929/* Follow signatured type referenced by ATTR in SRC_DIE.
23930 On entry *REF_CU is the CU of SRC_DIE.
23931 On exit *REF_CU is the CU of the result.
23932 The result is the DIE of the type.
23933 If the referenced type cannot be found an error is thrown. */
23934
23935static struct die_info *
ff39bb5e 23936follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23937 struct dwarf2_cu **ref_cu)
23938{
23939 ULONGEST signature = DW_SIGNATURE (attr);
23940 struct signatured_type *sig_type;
23941 struct die_info *die;
23942
23943 gdb_assert (attr->form == DW_FORM_ref_sig8);
23944
a2ce51a0 23945 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23946 /* sig_type will be NULL if the signatured type is missing from
23947 the debug info. */
23948 if (sig_type == NULL)
23949 {
23950 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23951 " from DIE at %s [in module %s]"),
23952 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23953 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23954 }
23955
23956 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23957 if (die == NULL)
23958 {
23959 dump_die_for_error (src_die);
23960 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23961 " from DIE at %s [in module %s]"),
23962 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23963 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23964 }
23965
23966 return die;
23967}
23968
23969/* Get the type specified by SIGNATURE referenced in DIE/CU,
23970 reading in and processing the type unit if necessary. */
23971
23972static struct type *
23973get_signatured_type (struct die_info *die, ULONGEST signature,
23974 struct dwarf2_cu *cu)
23975{
518817b3
SM
23976 struct dwarf2_per_objfile *dwarf2_per_objfile
23977 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23978 struct signatured_type *sig_type;
23979 struct dwarf2_cu *type_cu;
23980 struct die_info *type_die;
23981 struct type *type;
23982
a2ce51a0 23983 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23984 /* sig_type will be NULL if the signatured type is missing from
23985 the debug info. */
23986 if (sig_type == NULL)
23987 {
b98664d3 23988 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23989 " from DIE at %s [in module %s]"),
23990 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23991 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23992 return build_error_marker_type (cu, die);
23993 }
23994
23995 /* If we already know the type we're done. */
23996 if (sig_type->type != NULL)
23997 return sig_type->type;
23998
23999 type_cu = cu;
24000 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24001 if (type_die != NULL)
24002 {
24003 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24004 is created. This is important, for example, because for c++ classes
24005 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24006 type = read_type_die (type_die, type_cu);
24007 if (type == NULL)
24008 {
b98664d3 24009 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24010 " referenced from DIE at %s [in module %s]"),
24011 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24012 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24013 type = build_error_marker_type (cu, die);
24014 }
24015 }
24016 else
24017 {
b98664d3 24018 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24019 " from DIE at %s [in module %s]"),
24020 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24021 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24022 type = build_error_marker_type (cu, die);
24023 }
24024 sig_type->type = type;
24025
24026 return type;
24027}
24028
24029/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24030 reading in and processing the type unit if necessary. */
24031
24032static struct type *
ff39bb5e 24033get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24034 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24035{
24036 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 24037 if (attr_form_is_ref (attr))
ac9ec31b
DE
24038 {
24039 struct dwarf2_cu *type_cu = cu;
24040 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24041
24042 return read_type_die (type_die, type_cu);
24043 }
24044 else if (attr->form == DW_FORM_ref_sig8)
24045 {
24046 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24047 }
24048 else
24049 {
518817b3
SM
24050 struct dwarf2_per_objfile *dwarf2_per_objfile
24051 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24052
b98664d3 24053 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24054 " at %s [in module %s]"),
24055 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 24056 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24057 return build_error_marker_type (cu, die);
24058 }
348e048f
DE
24059}
24060
e5fe5e75 24061/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24062
24063static void
e5fe5e75 24064load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 24065{
52dc124a 24066 struct signatured_type *sig_type;
348e048f 24067
f4dc4d17
DE
24068 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24069 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24070
6721b2ec
DE
24071 /* We have the per_cu, but we need the signatured_type.
24072 Fortunately this is an easy translation. */
24073 gdb_assert (per_cu->is_debug_types);
24074 sig_type = (struct signatured_type *) per_cu;
348e048f 24075
6721b2ec 24076 gdb_assert (per_cu->cu == NULL);
348e048f 24077
52dc124a 24078 read_signatured_type (sig_type);
348e048f 24079
6721b2ec 24080 gdb_assert (per_cu->cu != NULL);
348e048f
DE
24081}
24082
dee91e82
DE
24083/* die_reader_func for read_signatured_type.
24084 This is identical to load_full_comp_unit_reader,
24085 but is kept separate for now. */
348e048f
DE
24086
24087static void
dee91e82 24088read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 24089 const gdb_byte *info_ptr,
dee91e82
DE
24090 struct die_info *comp_unit_die,
24091 int has_children,
24092 void *data)
348e048f 24093{
dee91e82 24094 struct dwarf2_cu *cu = reader->cu;
348e048f 24095
dee91e82
DE
24096 gdb_assert (cu->die_hash == NULL);
24097 cu->die_hash =
24098 htab_create_alloc_ex (cu->header.length / 12,
24099 die_hash,
24100 die_eq,
24101 NULL,
24102 &cu->comp_unit_obstack,
24103 hashtab_obstack_allocate,
24104 dummy_obstack_deallocate);
348e048f 24105
dee91e82
DE
24106 if (has_children)
24107 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24108 &info_ptr, comp_unit_die);
24109 cu->dies = comp_unit_die;
24110 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
24111
24112 /* We try not to read any attributes in this function, because not
9cdd5dbd 24113 all CUs needed for references have been loaded yet, and symbol
348e048f 24114 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
24115 or we won't be able to build types correctly.
24116 Similarly, if we do not read the producer, we can not apply
24117 producer-specific interpretation. */
95554aad 24118 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 24119}
348e048f 24120
3019eac3
DE
24121/* Read in a signatured type and build its CU and DIEs.
24122 If the type is a stub for the real type in a DWO file,
24123 read in the real type from the DWO file as well. */
dee91e82
DE
24124
24125static void
24126read_signatured_type (struct signatured_type *sig_type)
24127{
24128 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24129
3019eac3 24130 gdb_assert (per_cu->is_debug_types);
dee91e82 24131 gdb_assert (per_cu->cu == NULL);
348e048f 24132
58f0c718 24133 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 24134 read_signatured_type_reader, NULL);
7ee85ab1 24135 sig_type->per_cu.tu_read = 1;
c906108c
SS
24136}
24137
c906108c
SS
24138/* Decode simple location descriptions.
24139 Given a pointer to a dwarf block that defines a location, compute
24140 the location and return the value.
24141
4cecd739
DJ
24142 NOTE drow/2003-11-18: This function is called in two situations
24143 now: for the address of static or global variables (partial symbols
24144 only) and for offsets into structures which are expected to be
24145 (more or less) constant. The partial symbol case should go away,
24146 and only the constant case should remain. That will let this
24147 function complain more accurately. A few special modes are allowed
24148 without complaint for global variables (for instance, global
24149 register values and thread-local values).
c906108c
SS
24150
24151 A location description containing no operations indicates that the
4cecd739 24152 object is optimized out. The return value is 0 for that case.
6b992462
DJ
24153 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24154 callers will only want a very basic result and this can become a
21ae7a4d
JK
24155 complaint.
24156
24157 Note that stack[0] is unused except as a default error return. */
c906108c
SS
24158
24159static CORE_ADDR
e7c27a73 24160decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 24161{
518817b3 24162 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
24163 size_t i;
24164 size_t size = blk->size;
d521ce57 24165 const gdb_byte *data = blk->data;
21ae7a4d
JK
24166 CORE_ADDR stack[64];
24167 int stacki;
24168 unsigned int bytes_read, unsnd;
24169 gdb_byte op;
c906108c 24170
21ae7a4d
JK
24171 i = 0;
24172 stacki = 0;
24173 stack[stacki] = 0;
24174 stack[++stacki] = 0;
24175
24176 while (i < size)
24177 {
24178 op = data[i++];
24179 switch (op)
24180 {
24181 case DW_OP_lit0:
24182 case DW_OP_lit1:
24183 case DW_OP_lit2:
24184 case DW_OP_lit3:
24185 case DW_OP_lit4:
24186 case DW_OP_lit5:
24187 case DW_OP_lit6:
24188 case DW_OP_lit7:
24189 case DW_OP_lit8:
24190 case DW_OP_lit9:
24191 case DW_OP_lit10:
24192 case DW_OP_lit11:
24193 case DW_OP_lit12:
24194 case DW_OP_lit13:
24195 case DW_OP_lit14:
24196 case DW_OP_lit15:
24197 case DW_OP_lit16:
24198 case DW_OP_lit17:
24199 case DW_OP_lit18:
24200 case DW_OP_lit19:
24201 case DW_OP_lit20:
24202 case DW_OP_lit21:
24203 case DW_OP_lit22:
24204 case DW_OP_lit23:
24205 case DW_OP_lit24:
24206 case DW_OP_lit25:
24207 case DW_OP_lit26:
24208 case DW_OP_lit27:
24209 case DW_OP_lit28:
24210 case DW_OP_lit29:
24211 case DW_OP_lit30:
24212 case DW_OP_lit31:
24213 stack[++stacki] = op - DW_OP_lit0;
24214 break;
f1bea926 24215
21ae7a4d
JK
24216 case DW_OP_reg0:
24217 case DW_OP_reg1:
24218 case DW_OP_reg2:
24219 case DW_OP_reg3:
24220 case DW_OP_reg4:
24221 case DW_OP_reg5:
24222 case DW_OP_reg6:
24223 case DW_OP_reg7:
24224 case DW_OP_reg8:
24225 case DW_OP_reg9:
24226 case DW_OP_reg10:
24227 case DW_OP_reg11:
24228 case DW_OP_reg12:
24229 case DW_OP_reg13:
24230 case DW_OP_reg14:
24231 case DW_OP_reg15:
24232 case DW_OP_reg16:
24233 case DW_OP_reg17:
24234 case DW_OP_reg18:
24235 case DW_OP_reg19:
24236 case DW_OP_reg20:
24237 case DW_OP_reg21:
24238 case DW_OP_reg22:
24239 case DW_OP_reg23:
24240 case DW_OP_reg24:
24241 case DW_OP_reg25:
24242 case DW_OP_reg26:
24243 case DW_OP_reg27:
24244 case DW_OP_reg28:
24245 case DW_OP_reg29:
24246 case DW_OP_reg30:
24247 case DW_OP_reg31:
24248 stack[++stacki] = op - DW_OP_reg0;
24249 if (i < size)
24250 dwarf2_complex_location_expr_complaint ();
24251 break;
c906108c 24252
21ae7a4d
JK
24253 case DW_OP_regx:
24254 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24255 i += bytes_read;
24256 stack[++stacki] = unsnd;
24257 if (i < size)
24258 dwarf2_complex_location_expr_complaint ();
24259 break;
c906108c 24260
21ae7a4d
JK
24261 case DW_OP_addr:
24262 stack[++stacki] = read_address (objfile->obfd, &data[i],
24263 cu, &bytes_read);
24264 i += bytes_read;
24265 break;
d53d4ac5 24266
21ae7a4d
JK
24267 case DW_OP_const1u:
24268 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24269 i += 1;
24270 break;
24271
24272 case DW_OP_const1s:
24273 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24274 i += 1;
24275 break;
24276
24277 case DW_OP_const2u:
24278 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24279 i += 2;
24280 break;
24281
24282 case DW_OP_const2s:
24283 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24284 i += 2;
24285 break;
d53d4ac5 24286
21ae7a4d
JK
24287 case DW_OP_const4u:
24288 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24289 i += 4;
24290 break;
24291
24292 case DW_OP_const4s:
24293 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24294 i += 4;
24295 break;
24296
585861ea
JK
24297 case DW_OP_const8u:
24298 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24299 i += 8;
24300 break;
24301
21ae7a4d
JK
24302 case DW_OP_constu:
24303 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24304 &bytes_read);
24305 i += bytes_read;
24306 break;
24307
24308 case DW_OP_consts:
24309 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24310 i += bytes_read;
24311 break;
24312
24313 case DW_OP_dup:
24314 stack[stacki + 1] = stack[stacki];
24315 stacki++;
24316 break;
24317
24318 case DW_OP_plus:
24319 stack[stacki - 1] += stack[stacki];
24320 stacki--;
24321 break;
24322
24323 case DW_OP_plus_uconst:
24324 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24325 &bytes_read);
24326 i += bytes_read;
24327 break;
24328
24329 case DW_OP_minus:
24330 stack[stacki - 1] -= stack[stacki];
24331 stacki--;
24332 break;
24333
24334 case DW_OP_deref:
24335 /* If we're not the last op, then we definitely can't encode
24336 this using GDB's address_class enum. This is valid for partial
24337 global symbols, although the variable's address will be bogus
24338 in the psymtab. */
24339 if (i < size)
24340 dwarf2_complex_location_expr_complaint ();
24341 break;
24342
24343 case DW_OP_GNU_push_tls_address:
4aa4e28b 24344 case DW_OP_form_tls_address:
21ae7a4d
JK
24345 /* The top of the stack has the offset from the beginning
24346 of the thread control block at which the variable is located. */
24347 /* Nothing should follow this operator, so the top of stack would
24348 be returned. */
24349 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24350 address will be bogus in the psymtab. Make it always at least
24351 non-zero to not look as a variable garbage collected by linker
24352 which have DW_OP_addr 0. */
21ae7a4d
JK
24353 if (i < size)
24354 dwarf2_complex_location_expr_complaint ();
585861ea 24355 stack[stacki]++;
21ae7a4d
JK
24356 break;
24357
24358 case DW_OP_GNU_uninit:
24359 break;
24360
336d760d 24361 case DW_OP_addrx:
3019eac3 24362 case DW_OP_GNU_addr_index:
49f6c839 24363 case DW_OP_GNU_const_index:
3019eac3
DE
24364 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24365 &bytes_read);
24366 i += bytes_read;
24367 break;
24368
21ae7a4d
JK
24369 default:
24370 {
f39c6ffd 24371 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24372
24373 if (name)
b98664d3 24374 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24375 name);
24376 else
b98664d3 24377 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24378 op);
24379 }
24380
24381 return (stack[stacki]);
d53d4ac5 24382 }
3c6e0cb3 24383
21ae7a4d
JK
24384 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24385 outside of the allocated space. Also enforce minimum>0. */
24386 if (stacki >= ARRAY_SIZE (stack) - 1)
24387 {
b98664d3 24388 complaint (_("location description stack overflow"));
21ae7a4d
JK
24389 return 0;
24390 }
24391
24392 if (stacki <= 0)
24393 {
b98664d3 24394 complaint (_("location description stack underflow"));
21ae7a4d
JK
24395 return 0;
24396 }
24397 }
24398 return (stack[stacki]);
c906108c
SS
24399}
24400
24401/* memory allocation interface */
24402
c906108c 24403static struct dwarf_block *
7b5a2f43 24404dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24405{
8d749320 24406 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24407}
24408
c906108c 24409static struct die_info *
b60c80d6 24410dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24411{
24412 struct die_info *die;
b60c80d6
DJ
24413 size_t size = sizeof (struct die_info);
24414
24415 if (num_attrs > 1)
24416 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24417
b60c80d6 24418 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24419 memset (die, 0, sizeof (struct die_info));
24420 return (die);
24421}
2e276125
JB
24422
24423\f
24424/* Macro support. */
24425
233d95b5
JK
24426/* Return file name relative to the compilation directory of file number I in
24427 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24428 responsible for freeing it. */
233d95b5 24429
2e276125 24430static char *
233d95b5 24431file_file_name (int file, struct line_header *lh)
2e276125 24432{
6a83a1e6
EZ
24433 /* Is the file number a valid index into the line header's file name
24434 table? Remember that file numbers start with one, not zero. */
7ba99d21 24435 if (lh->is_valid_file_index (file))
6a83a1e6 24436 {
7ba99d21 24437 const file_entry *fe = lh->file_name_at (file);
6e70227d 24438
7ba99d21 24439 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24440 {
7ba99d21 24441 const char *dir = fe->include_dir (lh);
8c43009f 24442 if (dir != NULL)
7ba99d21 24443 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24444 }
7ba99d21 24445 return xstrdup (fe->name);
6a83a1e6 24446 }
2e276125
JB
24447 else
24448 {
6a83a1e6
EZ
24449 /* The compiler produced a bogus file number. We can at least
24450 record the macro definitions made in the file, even if we
24451 won't be able to find the file by name. */
24452 char fake_name[80];
9a619af0 24453
8c042590
PM
24454 xsnprintf (fake_name, sizeof (fake_name),
24455 "<bad macro file number %d>", file);
2e276125 24456
b98664d3 24457 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24458 file);
2e276125 24459
6a83a1e6 24460 return xstrdup (fake_name);
2e276125
JB
24461 }
24462}
24463
233d95b5
JK
24464/* Return the full name of file number I in *LH's file name table.
24465 Use COMP_DIR as the name of the current directory of the
24466 compilation. The result is allocated using xmalloc; the caller is
24467 responsible for freeing it. */
24468static char *
24469file_full_name (int file, struct line_header *lh, const char *comp_dir)
24470{
24471 /* Is the file number a valid index into the line header's file name
24472 table? Remember that file numbers start with one, not zero. */
7ba99d21 24473 if (lh->is_valid_file_index (file))
233d95b5
JK
24474 {
24475 char *relative = file_file_name (file, lh);
24476
24477 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24478 return relative;
b36cec19
PA
24479 return reconcat (relative, comp_dir, SLASH_STRING,
24480 relative, (char *) NULL);
233d95b5
JK
24481 }
24482 else
24483 return file_file_name (file, lh);
24484}
24485
2e276125
JB
24486
24487static struct macro_source_file *
804d2729
TT
24488macro_start_file (struct dwarf2_cu *cu,
24489 int file, int line,
2e276125 24490 struct macro_source_file *current_file,
43f3e411 24491 struct line_header *lh)
2e276125 24492{
233d95b5
JK
24493 /* File name relative to the compilation directory of this source file. */
24494 char *file_name = file_file_name (file, lh);
2e276125 24495
2e276125 24496 if (! current_file)
abc9d0dc 24497 {
fc474241
DE
24498 /* Note: We don't create a macro table for this compilation unit
24499 at all until we actually get a filename. */
c24bdb02 24500 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24501
abc9d0dc
TT
24502 /* If we have no current file, then this must be the start_file
24503 directive for the compilation unit's main source file. */
fc474241
DE
24504 current_file = macro_set_main (macro_table, file_name);
24505 macro_define_special (macro_table);
abc9d0dc 24506 }
2e276125 24507 else
233d95b5 24508 current_file = macro_include (current_file, line, file_name);
2e276125 24509
233d95b5 24510 xfree (file_name);
6e70227d 24511
2e276125
JB
24512 return current_file;
24513}
24514
2e276125
JB
24515static const char *
24516consume_improper_spaces (const char *p, const char *body)
24517{
24518 if (*p == ' ')
24519 {
b98664d3 24520 complaint (_("macro definition contains spaces "
3e43a32a 24521 "in formal argument list:\n`%s'"),
4d3c2250 24522 body);
2e276125
JB
24523
24524 while (*p == ' ')
24525 p++;
24526 }
24527
24528 return p;
24529}
24530
24531
24532static void
24533parse_macro_definition (struct macro_source_file *file, int line,
24534 const char *body)
24535{
24536 const char *p;
24537
24538 /* The body string takes one of two forms. For object-like macro
24539 definitions, it should be:
24540
24541 <macro name> " " <definition>
24542
24543 For function-like macro definitions, it should be:
24544
24545 <macro name> "() " <definition>
24546 or
24547 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24548
24549 Spaces may appear only where explicitly indicated, and in the
24550 <definition>.
24551
24552 The Dwarf 2 spec says that an object-like macro's name is always
24553 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24554 the space when the macro's definition is the empty string.
2e276125
JB
24555
24556 The Dwarf 2 spec says that there should be no spaces between the
24557 formal arguments in a function-like macro's formal argument list,
24558 but versions of GCC around March 2002 include spaces after the
24559 commas. */
24560
24561
24562 /* Find the extent of the macro name. The macro name is terminated
24563 by either a space or null character (for an object-like macro) or
24564 an opening paren (for a function-like macro). */
24565 for (p = body; *p; p++)
24566 if (*p == ' ' || *p == '(')
24567 break;
24568
24569 if (*p == ' ' || *p == '\0')
24570 {
24571 /* It's an object-like macro. */
24572 int name_len = p - body;
456e800a 24573 std::string name (body, name_len);
2e276125
JB
24574 const char *replacement;
24575
24576 if (*p == ' ')
24577 replacement = body + name_len + 1;
24578 else
24579 {
4d3c2250 24580 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24581 replacement = body + name_len;
24582 }
6e70227d 24583
456e800a 24584 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
24585 }
24586 else if (*p == '(')
24587 {
24588 /* It's a function-like macro. */
456e800a 24589 std::string name (body, p - body);
2e276125
JB
24590 int argc = 0;
24591 int argv_size = 1;
8d749320 24592 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24593
24594 p++;
24595
24596 p = consume_improper_spaces (p, body);
24597
24598 /* Parse the formal argument list. */
24599 while (*p && *p != ')')
24600 {
24601 /* Find the extent of the current argument name. */
24602 const char *arg_start = p;
24603
24604 while (*p && *p != ',' && *p != ')' && *p != ' ')
24605 p++;
24606
24607 if (! *p || p == arg_start)
4d3c2250 24608 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24609 else
24610 {
24611 /* Make sure argv has room for the new argument. */
24612 if (argc >= argv_size)
24613 {
24614 argv_size *= 2;
224c3ddb 24615 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24616 }
24617
3f8a7804 24618 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24619 }
24620
24621 p = consume_improper_spaces (p, body);
24622
24623 /* Consume the comma, if present. */
24624 if (*p == ',')
24625 {
24626 p++;
24627
24628 p = consume_improper_spaces (p, body);
24629 }
24630 }
24631
24632 if (*p == ')')
24633 {
24634 p++;
24635
24636 if (*p == ' ')
24637 /* Perfectly formed definition, no complaints. */
456e800a 24638 macro_define_function (file, line, name.c_str (),
6e70227d 24639 argc, (const char **) argv,
2e276125
JB
24640 p + 1);
24641 else if (*p == '\0')
24642 {
24643 /* Complain, but do define it. */
4d3c2250 24644 dwarf2_macro_malformed_definition_complaint (body);
456e800a 24645 macro_define_function (file, line, name.c_str (),
6e70227d 24646 argc, (const char **) argv,
2e276125
JB
24647 p);
24648 }
24649 else
24650 /* Just complain. */
4d3c2250 24651 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24652 }
24653 else
24654 /* Just complain. */
4d3c2250 24655 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24656
2e276125
JB
24657 {
24658 int i;
24659
24660 for (i = 0; i < argc; i++)
24661 xfree (argv[i]);
24662 }
24663 xfree (argv);
24664 }
24665 else
4d3c2250 24666 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24667}
24668
cf2c3c16
TT
24669/* Skip some bytes from BYTES according to the form given in FORM.
24670 Returns the new pointer. */
2e276125 24671
d521ce57
TT
24672static const gdb_byte *
24673skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24674 enum dwarf_form form,
24675 unsigned int offset_size,
24676 struct dwarf2_section_info *section)
2e276125 24677{
cf2c3c16 24678 unsigned int bytes_read;
2e276125 24679
cf2c3c16 24680 switch (form)
2e276125 24681 {
cf2c3c16
TT
24682 case DW_FORM_data1:
24683 case DW_FORM_flag:
24684 ++bytes;
24685 break;
24686
24687 case DW_FORM_data2:
24688 bytes += 2;
24689 break;
24690
24691 case DW_FORM_data4:
24692 bytes += 4;
24693 break;
24694
24695 case DW_FORM_data8:
24696 bytes += 8;
24697 break;
24698
0224619f
JK
24699 case DW_FORM_data16:
24700 bytes += 16;
24701 break;
24702
cf2c3c16
TT
24703 case DW_FORM_string:
24704 read_direct_string (abfd, bytes, &bytes_read);
24705 bytes += bytes_read;
24706 break;
24707
24708 case DW_FORM_sec_offset:
24709 case DW_FORM_strp:
36586728 24710 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24711 bytes += offset_size;
24712 break;
24713
24714 case DW_FORM_block:
24715 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24716 bytes += bytes_read;
24717 break;
24718
24719 case DW_FORM_block1:
24720 bytes += 1 + read_1_byte (abfd, bytes);
24721 break;
24722 case DW_FORM_block2:
24723 bytes += 2 + read_2_bytes (abfd, bytes);
24724 break;
24725 case DW_FORM_block4:
24726 bytes += 4 + read_4_bytes (abfd, bytes);
24727 break;
24728
336d760d 24729 case DW_FORM_addrx:
cf2c3c16 24730 case DW_FORM_sdata:
cf532bd1 24731 case DW_FORM_strx:
cf2c3c16 24732 case DW_FORM_udata:
3019eac3
DE
24733 case DW_FORM_GNU_addr_index:
24734 case DW_FORM_GNU_str_index:
d521ce57 24735 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24736 if (bytes == NULL)
24737 {
24738 dwarf2_section_buffer_overflow_complaint (section);
24739 return NULL;
24740 }
cf2c3c16
TT
24741 break;
24742
663c44ac
JK
24743 case DW_FORM_implicit_const:
24744 break;
24745
cf2c3c16
TT
24746 default:
24747 {
b98664d3 24748 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24749 form, get_section_name (section));
cf2c3c16
TT
24750 return NULL;
24751 }
2e276125
JB
24752 }
24753
cf2c3c16
TT
24754 return bytes;
24755}
757a13d0 24756
cf2c3c16
TT
24757/* A helper for dwarf_decode_macros that handles skipping an unknown
24758 opcode. Returns an updated pointer to the macro data buffer; or,
24759 on error, issues a complaint and returns NULL. */
757a13d0 24760
d521ce57 24761static const gdb_byte *
cf2c3c16 24762skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24763 const gdb_byte **opcode_definitions,
24764 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24765 bfd *abfd,
24766 unsigned int offset_size,
24767 struct dwarf2_section_info *section)
24768{
24769 unsigned int bytes_read, i;
24770 unsigned long arg;
d521ce57 24771 const gdb_byte *defn;
2e276125 24772
cf2c3c16 24773 if (opcode_definitions[opcode] == NULL)
2e276125 24774 {
b98664d3 24775 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24776 opcode);
24777 return NULL;
24778 }
2e276125 24779
cf2c3c16
TT
24780 defn = opcode_definitions[opcode];
24781 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24782 defn += bytes_read;
2e276125 24783
cf2c3c16
TT
24784 for (i = 0; i < arg; ++i)
24785 {
aead7601
SM
24786 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24787 (enum dwarf_form) defn[i], offset_size,
f664829e 24788 section);
cf2c3c16
TT
24789 if (mac_ptr == NULL)
24790 {
24791 /* skip_form_bytes already issued the complaint. */
24792 return NULL;
24793 }
24794 }
757a13d0 24795
cf2c3c16
TT
24796 return mac_ptr;
24797}
757a13d0 24798
cf2c3c16
TT
24799/* A helper function which parses the header of a macro section.
24800 If the macro section is the extended (for now called "GNU") type,
24801 then this updates *OFFSET_SIZE. Returns a pointer to just after
24802 the header, or issues a complaint and returns NULL on error. */
757a13d0 24803
d521ce57
TT
24804static const gdb_byte *
24805dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24806 bfd *abfd,
d521ce57 24807 const gdb_byte *mac_ptr,
cf2c3c16
TT
24808 unsigned int *offset_size,
24809 int section_is_gnu)
24810{
24811 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24812
cf2c3c16
TT
24813 if (section_is_gnu)
24814 {
24815 unsigned int version, flags;
757a13d0 24816
cf2c3c16 24817 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24818 if (version != 4 && version != 5)
cf2c3c16 24819 {
b98664d3 24820 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24821 version);
24822 return NULL;
24823 }
24824 mac_ptr += 2;
757a13d0 24825
cf2c3c16
TT
24826 flags = read_1_byte (abfd, mac_ptr);
24827 ++mac_ptr;
24828 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24829
cf2c3c16
TT
24830 if ((flags & 2) != 0)
24831 /* We don't need the line table offset. */
24832 mac_ptr += *offset_size;
757a13d0 24833
cf2c3c16
TT
24834 /* Vendor opcode descriptions. */
24835 if ((flags & 4) != 0)
24836 {
24837 unsigned int i, count;
757a13d0 24838
cf2c3c16
TT
24839 count = read_1_byte (abfd, mac_ptr);
24840 ++mac_ptr;
24841 for (i = 0; i < count; ++i)
24842 {
24843 unsigned int opcode, bytes_read;
24844 unsigned long arg;
24845
24846 opcode = read_1_byte (abfd, mac_ptr);
24847 ++mac_ptr;
24848 opcode_definitions[opcode] = mac_ptr;
24849 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24850 mac_ptr += bytes_read;
24851 mac_ptr += arg;
24852 }
757a13d0 24853 }
cf2c3c16 24854 }
757a13d0 24855
cf2c3c16
TT
24856 return mac_ptr;
24857}
757a13d0 24858
cf2c3c16 24859/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24860 including DW_MACRO_import. */
cf2c3c16
TT
24861
24862static void
804d2729 24863dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24864 bfd *abfd,
d521ce57 24865 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24866 struct macro_source_file *current_file,
43f3e411 24867 struct line_header *lh,
cf2c3c16 24868 struct dwarf2_section_info *section,
36586728 24869 int section_is_gnu, int section_is_dwz,
cf2c3c16 24870 unsigned int offset_size,
8fc3fc34 24871 htab_t include_hash)
cf2c3c16 24872{
804d2729
TT
24873 struct dwarf2_per_objfile *dwarf2_per_objfile
24874 = cu->per_cu->dwarf2_per_objfile;
4d663531 24875 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24876 enum dwarf_macro_record_type macinfo_type;
24877 int at_commandline;
d521ce57 24878 const gdb_byte *opcode_definitions[256];
757a13d0 24879
cf2c3c16
TT
24880 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24881 &offset_size, section_is_gnu);
24882 if (mac_ptr == NULL)
24883 {
24884 /* We already issued a complaint. */
24885 return;
24886 }
757a13d0
JK
24887
24888 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24889 GDB is still reading the definitions from command line. First
24890 DW_MACINFO_start_file will need to be ignored as it was already executed
24891 to create CURRENT_FILE for the main source holding also the command line
24892 definitions. On first met DW_MACINFO_start_file this flag is reset to
24893 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24894
24895 at_commandline = 1;
24896
24897 do
24898 {
24899 /* Do we at least have room for a macinfo type byte? */
24900 if (mac_ptr >= mac_end)
24901 {
f664829e 24902 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24903 break;
24904 }
24905
aead7601 24906 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24907 mac_ptr++;
24908
cf2c3c16
TT
24909 /* Note that we rely on the fact that the corresponding GNU and
24910 DWARF constants are the same. */
132448f8
SM
24911 DIAGNOSTIC_PUSH
24912 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24913 switch (macinfo_type)
24914 {
24915 /* A zero macinfo type indicates the end of the macro
24916 information. */
24917 case 0:
24918 break;
2e276125 24919
0af92d60
JK
24920 case DW_MACRO_define:
24921 case DW_MACRO_undef:
24922 case DW_MACRO_define_strp:
24923 case DW_MACRO_undef_strp:
24924 case DW_MACRO_define_sup:
24925 case DW_MACRO_undef_sup:
2e276125 24926 {
891d2f0b 24927 unsigned int bytes_read;
2e276125 24928 int line;
d521ce57 24929 const char *body;
cf2c3c16 24930 int is_define;
2e276125 24931
cf2c3c16
TT
24932 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24933 mac_ptr += bytes_read;
24934
0af92d60
JK
24935 if (macinfo_type == DW_MACRO_define
24936 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24937 {
24938 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24939 mac_ptr += bytes_read;
24940 }
24941 else
24942 {
24943 LONGEST str_offset;
24944
24945 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24946 mac_ptr += offset_size;
2e276125 24947
0af92d60
JK
24948 if (macinfo_type == DW_MACRO_define_sup
24949 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24950 || section_is_dwz)
36586728 24951 {
ed2dc618
SM
24952 struct dwz_file *dwz
24953 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24954
ed2dc618
SM
24955 body = read_indirect_string_from_dwz (objfile,
24956 dwz, str_offset);
36586728
TT
24957 }
24958 else
ed2dc618
SM
24959 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24960 abfd, str_offset);
cf2c3c16
TT
24961 }
24962
0af92d60
JK
24963 is_define = (macinfo_type == DW_MACRO_define
24964 || macinfo_type == DW_MACRO_define_strp
24965 || macinfo_type == DW_MACRO_define_sup);
2e276125 24966 if (! current_file)
757a13d0
JK
24967 {
24968 /* DWARF violation as no main source is present. */
b98664d3 24969 complaint (_("debug info with no main source gives macro %s "
757a13d0 24970 "on line %d: %s"),
cf2c3c16
TT
24971 is_define ? _("definition") : _("undefinition"),
24972 line, body);
757a13d0
JK
24973 break;
24974 }
3e43a32a
MS
24975 if ((line == 0 && !at_commandline)
24976 || (line != 0 && at_commandline))
b98664d3 24977 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24978 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24979 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24980 line == 0 ? _("zero") : _("non-zero"), line, body);
24981
955b06fa 24982 if (body == NULL)
7bede828 24983 {
955b06fa
SDJ
24984 /* Fedora's rpm-build's "debugedit" binary
24985 corrupted .debug_macro sections.
24986
24987 For more info, see
24988 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24989 complaint (_("debug info gives %s invalid macro %s "
24990 "without body (corrupted?) at line %d "
24991 "on file %s"),
24992 at_commandline ? _("command-line") : _("in-file"),
24993 is_define ? _("definition") : _("undefinition"),
24994 line, current_file->filename);
7bede828 24995 }
955b06fa
SDJ
24996 else if (is_define)
24997 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24998 else
24999 {
0af92d60
JK
25000 gdb_assert (macinfo_type == DW_MACRO_undef
25001 || macinfo_type == DW_MACRO_undef_strp
25002 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
25003 macro_undef (current_file, line, body);
25004 }
2e276125
JB
25005 }
25006 break;
25007
0af92d60 25008 case DW_MACRO_start_file:
2e276125 25009 {
891d2f0b 25010 unsigned int bytes_read;
2e276125
JB
25011 int line, file;
25012
25013 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25014 mac_ptr += bytes_read;
25015 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25016 mac_ptr += bytes_read;
25017
3e43a32a
MS
25018 if ((line == 0 && !at_commandline)
25019 || (line != 0 && at_commandline))
b98664d3 25020 complaint (_("debug info gives source %d included "
757a13d0
JK
25021 "from %s at %s line %d"),
25022 file, at_commandline ? _("command-line") : _("file"),
25023 line == 0 ? _("zero") : _("non-zero"), line);
25024
25025 if (at_commandline)
25026 {
0af92d60 25027 /* This DW_MACRO_start_file was executed in the
cf2c3c16 25028 pass one. */
757a13d0
JK
25029 at_commandline = 0;
25030 }
25031 else
804d2729
TT
25032 current_file = macro_start_file (cu, file, line, current_file,
25033 lh);
2e276125
JB
25034 }
25035 break;
25036
0af92d60 25037 case DW_MACRO_end_file:
2e276125 25038 if (! current_file)
b98664d3 25039 complaint (_("macro debug info has an unmatched "
3e43a32a 25040 "`close_file' directive"));
2e276125
JB
25041 else
25042 {
25043 current_file = current_file->included_by;
25044 if (! current_file)
25045 {
cf2c3c16 25046 enum dwarf_macro_record_type next_type;
2e276125
JB
25047
25048 /* GCC circa March 2002 doesn't produce the zero
25049 type byte marking the end of the compilation
25050 unit. Complain if it's not there, but exit no
25051 matter what. */
25052
25053 /* Do we at least have room for a macinfo type byte? */
25054 if (mac_ptr >= mac_end)
25055 {
f664829e 25056 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
25057 return;
25058 }
25059
25060 /* We don't increment mac_ptr here, so this is just
25061 a look-ahead. */
aead7601
SM
25062 next_type
25063 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25064 mac_ptr);
2e276125 25065 if (next_type != 0)
b98664d3 25066 complaint (_("no terminating 0-type entry for "
3e43a32a 25067 "macros in `.debug_macinfo' section"));
2e276125
JB
25068
25069 return;
25070 }
25071 }
25072 break;
25073
0af92d60
JK
25074 case DW_MACRO_import:
25075 case DW_MACRO_import_sup:
cf2c3c16
TT
25076 {
25077 LONGEST offset;
8fc3fc34 25078 void **slot;
a036ba48
TT
25079 bfd *include_bfd = abfd;
25080 struct dwarf2_section_info *include_section = section;
d521ce57 25081 const gdb_byte *include_mac_end = mac_end;
a036ba48 25082 int is_dwz = section_is_dwz;
d521ce57 25083 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
25084
25085 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25086 mac_ptr += offset_size;
25087
0af92d60 25088 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 25089 {
ed2dc618 25090 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 25091
4d663531 25092 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 25093
a036ba48 25094 include_section = &dwz->macro;
a32a8923 25095 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
25096 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25097 is_dwz = 1;
25098 }
25099
25100 new_mac_ptr = include_section->buffer + offset;
25101 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25102
8fc3fc34
TT
25103 if (*slot != NULL)
25104 {
25105 /* This has actually happened; see
25106 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 25107 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
25108 ".debug_macro section"));
25109 }
25110 else
25111 {
d521ce57 25112 *slot = (void *) new_mac_ptr;
36586728 25113
804d2729 25114 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 25115 include_mac_end, current_file, lh,
36586728 25116 section, section_is_gnu, is_dwz,
4d663531 25117 offset_size, include_hash);
8fc3fc34 25118
d521ce57 25119 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 25120 }
cf2c3c16
TT
25121 }
25122 break;
25123
2e276125 25124 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
25125 if (!section_is_gnu)
25126 {
25127 unsigned int bytes_read;
2e276125 25128
ac298888
TT
25129 /* This reads the constant, but since we don't recognize
25130 any vendor extensions, we ignore it. */
25131 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
25132 mac_ptr += bytes_read;
25133 read_direct_string (abfd, mac_ptr, &bytes_read);
25134 mac_ptr += bytes_read;
2e276125 25135
cf2c3c16
TT
25136 /* We don't recognize any vendor extensions. */
25137 break;
25138 }
25139 /* FALLTHROUGH */
25140
25141 default:
25142 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25143 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25144 section);
25145 if (mac_ptr == NULL)
25146 return;
25147 break;
2e276125 25148 }
132448f8 25149 DIAGNOSTIC_POP
757a13d0 25150 } while (macinfo_type != 0);
2e276125 25151}
8e19ed76 25152
cf2c3c16 25153static void
09262596 25154dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 25155 int section_is_gnu)
cf2c3c16 25156{
518817b3
SM
25157 struct dwarf2_per_objfile *dwarf2_per_objfile
25158 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25159 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
25160 struct line_header *lh = cu->line_header;
25161 bfd *abfd;
d521ce57 25162 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
25163 struct macro_source_file *current_file = 0;
25164 enum dwarf_macro_record_type macinfo_type;
25165 unsigned int offset_size = cu->header.offset_size;
d521ce57 25166 const gdb_byte *opcode_definitions[256];
8fc3fc34 25167 void **slot;
09262596
DE
25168 struct dwarf2_section_info *section;
25169 const char *section_name;
25170
25171 if (cu->dwo_unit != NULL)
25172 {
25173 if (section_is_gnu)
25174 {
25175 section = &cu->dwo_unit->dwo_file->sections.macro;
25176 section_name = ".debug_macro.dwo";
25177 }
25178 else
25179 {
25180 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25181 section_name = ".debug_macinfo.dwo";
25182 }
25183 }
25184 else
25185 {
25186 if (section_is_gnu)
25187 {
25188 section = &dwarf2_per_objfile->macro;
25189 section_name = ".debug_macro";
25190 }
25191 else
25192 {
25193 section = &dwarf2_per_objfile->macinfo;
25194 section_name = ".debug_macinfo";
25195 }
25196 }
cf2c3c16 25197
bb5ed363 25198 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25199 if (section->buffer == NULL)
25200 {
b98664d3 25201 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
25202 return;
25203 }
a32a8923 25204 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25205
25206 /* First pass: Find the name of the base filename.
25207 This filename is needed in order to process all macros whose definition
25208 (or undefinition) comes from the command line. These macros are defined
25209 before the first DW_MACINFO_start_file entry, and yet still need to be
25210 associated to the base file.
25211
25212 To determine the base file name, we scan the macro definitions until we
25213 reach the first DW_MACINFO_start_file entry. We then initialize
25214 CURRENT_FILE accordingly so that any macro definition found before the
25215 first DW_MACINFO_start_file can still be associated to the base file. */
25216
25217 mac_ptr = section->buffer + offset;
25218 mac_end = section->buffer + section->size;
25219
25220 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25221 &offset_size, section_is_gnu);
25222 if (mac_ptr == NULL)
25223 {
25224 /* We already issued a complaint. */
25225 return;
25226 }
25227
25228 do
25229 {
25230 /* Do we at least have room for a macinfo type byte? */
25231 if (mac_ptr >= mac_end)
25232 {
25233 /* Complaint is printed during the second pass as GDB will probably
25234 stop the first pass earlier upon finding
25235 DW_MACINFO_start_file. */
25236 break;
25237 }
25238
aead7601 25239 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25240 mac_ptr++;
25241
25242 /* Note that we rely on the fact that the corresponding GNU and
25243 DWARF constants are the same. */
132448f8
SM
25244 DIAGNOSTIC_PUSH
25245 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25246 switch (macinfo_type)
25247 {
25248 /* A zero macinfo type indicates the end of the macro
25249 information. */
25250 case 0:
25251 break;
25252
0af92d60
JK
25253 case DW_MACRO_define:
25254 case DW_MACRO_undef:
cf2c3c16
TT
25255 /* Only skip the data by MAC_PTR. */
25256 {
25257 unsigned int bytes_read;
25258
25259 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25260 mac_ptr += bytes_read;
25261 read_direct_string (abfd, mac_ptr, &bytes_read);
25262 mac_ptr += bytes_read;
25263 }
25264 break;
25265
0af92d60 25266 case DW_MACRO_start_file:
cf2c3c16
TT
25267 {
25268 unsigned int bytes_read;
25269 int line, file;
25270
25271 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25272 mac_ptr += bytes_read;
25273 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25274 mac_ptr += bytes_read;
25275
804d2729 25276 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25277 }
25278 break;
25279
0af92d60 25280 case DW_MACRO_end_file:
cf2c3c16
TT
25281 /* No data to skip by MAC_PTR. */
25282 break;
25283
0af92d60
JK
25284 case DW_MACRO_define_strp:
25285 case DW_MACRO_undef_strp:
25286 case DW_MACRO_define_sup:
25287 case DW_MACRO_undef_sup:
cf2c3c16
TT
25288 {
25289 unsigned int bytes_read;
25290
25291 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25292 mac_ptr += bytes_read;
25293 mac_ptr += offset_size;
25294 }
25295 break;
25296
0af92d60
JK
25297 case DW_MACRO_import:
25298 case DW_MACRO_import_sup:
cf2c3c16 25299 /* Note that, according to the spec, a transparent include
0af92d60 25300 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25301 skip this opcode. */
25302 mac_ptr += offset_size;
25303 break;
25304
25305 case DW_MACINFO_vendor_ext:
25306 /* Only skip the data by MAC_PTR. */
25307 if (!section_is_gnu)
25308 {
25309 unsigned int bytes_read;
25310
25311 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25312 mac_ptr += bytes_read;
25313 read_direct_string (abfd, mac_ptr, &bytes_read);
25314 mac_ptr += bytes_read;
25315 }
25316 /* FALLTHROUGH */
25317
25318 default:
25319 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25320 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25321 section);
25322 if (mac_ptr == NULL)
25323 return;
25324 break;
25325 }
132448f8 25326 DIAGNOSTIC_POP
cf2c3c16
TT
25327 } while (macinfo_type != 0 && current_file == NULL);
25328
25329 /* Second pass: Process all entries.
25330
25331 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25332 command-line macro definitions/undefinitions. This flag is unset when we
25333 reach the first DW_MACINFO_start_file entry. */
25334
fc4007c9
TT
25335 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25336 htab_eq_pointer,
25337 NULL, xcalloc, xfree));
8fc3fc34 25338 mac_ptr = section->buffer + offset;
fc4007c9 25339 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25340 *slot = (void *) mac_ptr;
804d2729 25341 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25342 current_file, lh, section,
fc4007c9
TT
25343 section_is_gnu, 0, offset_size,
25344 include_hash.get ());
cf2c3c16
TT
25345}
25346
8e19ed76 25347/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25348 if so return true else false. */
380bca97 25349
8e19ed76 25350static int
6e5a29e1 25351attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25352{
25353 return (attr == NULL ? 0 :
25354 attr->form == DW_FORM_block1
25355 || attr->form == DW_FORM_block2
25356 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25357 || attr->form == DW_FORM_block
25358 || attr->form == DW_FORM_exprloc);
8e19ed76 25359}
4c2df51b 25360
c6a0999f
JB
25361/* Return non-zero if ATTR's value is a section offset --- classes
25362 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25363 You may use DW_UNSND (attr) to retrieve such offsets.
25364
25365 Section 7.5.4, "Attribute Encodings", explains that no attribute
25366 may have a value that belongs to more than one of these classes; it
25367 would be ambiguous if we did, because we use the same forms for all
25368 of them. */
380bca97 25369
3690dd37 25370static int
6e5a29e1 25371attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25372{
25373 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25374 || attr->form == DW_FORM_data8
25375 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25376}
25377
3690dd37
JB
25378/* Return non-zero if ATTR's value falls in the 'constant' class, or
25379 zero otherwise. When this function returns true, you can apply
25380 dwarf2_get_attr_constant_value to it.
25381
25382 However, note that for some attributes you must check
25383 attr_form_is_section_offset before using this test. DW_FORM_data4
25384 and DW_FORM_data8 are members of both the constant class, and of
25385 the classes that contain offsets into other debug sections
25386 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25387 that, if an attribute's can be either a constant or one of the
25388 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25389 taken as section offsets, not constants.
25390
25391 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25392 cannot handle that. */
380bca97 25393
3690dd37 25394static int
6e5a29e1 25395attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25396{
25397 switch (attr->form)
25398 {
25399 case DW_FORM_sdata:
25400 case DW_FORM_udata:
25401 case DW_FORM_data1:
25402 case DW_FORM_data2:
25403 case DW_FORM_data4:
25404 case DW_FORM_data8:
663c44ac 25405 case DW_FORM_implicit_const:
3690dd37
JB
25406 return 1;
25407 default:
25408 return 0;
25409 }
25410}
25411
7771576e
SA
25412
25413/* DW_ADDR is always stored already as sect_offset; despite for the forms
25414 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25415
25416static int
6e5a29e1 25417attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25418{
25419 switch (attr->form)
25420 {
25421 case DW_FORM_ref_addr:
25422 case DW_FORM_ref1:
25423 case DW_FORM_ref2:
25424 case DW_FORM_ref4:
25425 case DW_FORM_ref8:
25426 case DW_FORM_ref_udata:
25427 case DW_FORM_GNU_ref_alt:
25428 return 1;
25429 default:
25430 return 0;
25431 }
25432}
25433
3019eac3
DE
25434/* Return the .debug_loc section to use for CU.
25435 For DWO files use .debug_loc.dwo. */
25436
25437static struct dwarf2_section_info *
25438cu_debug_loc_section (struct dwarf2_cu *cu)
25439{
518817b3
SM
25440 struct dwarf2_per_objfile *dwarf2_per_objfile
25441 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25442
3019eac3 25443 if (cu->dwo_unit)
43988095
JK
25444 {
25445 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25446
43988095
JK
25447 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25448 }
25449 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25450 : &dwarf2_per_objfile->loc);
3019eac3
DE
25451}
25452
8cf6f0b1
TT
25453/* A helper function that fills in a dwarf2_loclist_baton. */
25454
25455static void
25456fill_in_loclist_baton (struct dwarf2_cu *cu,
25457 struct dwarf2_loclist_baton *baton,
ff39bb5e 25458 const struct attribute *attr)
8cf6f0b1 25459{
518817b3
SM
25460 struct dwarf2_per_objfile *dwarf2_per_objfile
25461 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25462 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25463
25464 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25465
25466 baton->per_cu = cu->per_cu;
25467 gdb_assert (baton->per_cu);
25468 /* We don't know how long the location list is, but make sure we
25469 don't run off the edge of the section. */
3019eac3
DE
25470 baton->size = section->size - DW_UNSND (attr);
25471 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25472 baton->base_address = cu->base_address;
f664829e 25473 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25474}
25475
4c2df51b 25476static void
ff39bb5e 25477dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25478 struct dwarf2_cu *cu, int is_block)
4c2df51b 25479{
518817b3
SM
25480 struct dwarf2_per_objfile *dwarf2_per_objfile
25481 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25482 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25483 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25484
3690dd37 25485 if (attr_form_is_section_offset (attr)
3019eac3 25486 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25487 the section. If so, fall through to the complaint in the
25488 other branch. */
3019eac3 25489 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25490 {
0d53c4c4 25491 struct dwarf2_loclist_baton *baton;
4c2df51b 25492
8d749320 25493 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25494
8cf6f0b1 25495 fill_in_loclist_baton (cu, baton, attr);
be391dca 25496
d00adf39 25497 if (cu->base_known == 0)
b98664d3 25498 complaint (_("Location list used without "
3e43a32a 25499 "specifying the CU base address."));
4c2df51b 25500
f1e6e072
TT
25501 SYMBOL_ACLASS_INDEX (sym) = (is_block
25502 ? dwarf2_loclist_block_index
25503 : dwarf2_loclist_index);
0d53c4c4
DJ
25504 SYMBOL_LOCATION_BATON (sym) = baton;
25505 }
25506 else
25507 {
25508 struct dwarf2_locexpr_baton *baton;
25509
8d749320 25510 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25511 baton->per_cu = cu->per_cu;
25512 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25513
25514 if (attr_form_is_block (attr))
25515 {
25516 /* Note that we're just copying the block's data pointer
25517 here, not the actual data. We're still pointing into the
6502dd73
DJ
25518 info_buffer for SYM's objfile; right now we never release
25519 that buffer, but when we do clean up properly this may
25520 need to change. */
0d53c4c4
DJ
25521 baton->size = DW_BLOCK (attr)->size;
25522 baton->data = DW_BLOCK (attr)->data;
25523 }
25524 else
25525 {
25526 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 25527 sym->natural_name ());
0d53c4c4 25528 baton->size = 0;
0d53c4c4 25529 }
6e70227d 25530
f1e6e072
TT
25531 SYMBOL_ACLASS_INDEX (sym) = (is_block
25532 ? dwarf2_locexpr_block_index
25533 : dwarf2_locexpr_index);
0d53c4c4
DJ
25534 SYMBOL_LOCATION_BATON (sym) = baton;
25535 }
4c2df51b 25536}
6502dd73 25537
9aa1f1e3
TT
25538/* Return the OBJFILE associated with the compilation unit CU. If CU
25539 came from a separate debuginfo file, then the master objfile is
25540 returned. */
ae0d2f24
UW
25541
25542struct objfile *
25543dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25544{
e3b94546 25545 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25546
25547 /* Return the master objfile, so that we can report and look up the
25548 correct file containing this variable. */
25549 if (objfile->separate_debug_objfile_backlink)
25550 objfile = objfile->separate_debug_objfile_backlink;
25551
25552 return objfile;
25553}
25554
96408a79
SA
25555/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25556 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25557 CU_HEADERP first. */
25558
25559static const struct comp_unit_head *
25560per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25561 struct dwarf2_per_cu_data *per_cu)
25562{
d521ce57 25563 const gdb_byte *info_ptr;
96408a79
SA
25564
25565 if (per_cu->cu)
25566 return &per_cu->cu->header;
25567
9c541725 25568 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25569
25570 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25571 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25572 rcuh_kind::COMPILE);
96408a79
SA
25573
25574 return cu_headerp;
25575}
25576
ae0d2f24
UW
25577/* Return the address size given in the compilation unit header for CU. */
25578
98714339 25579int
ae0d2f24
UW
25580dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25581{
96408a79
SA
25582 struct comp_unit_head cu_header_local;
25583 const struct comp_unit_head *cu_headerp;
c471e790 25584
96408a79
SA
25585 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25586
25587 return cu_headerp->addr_size;
ae0d2f24
UW
25588}
25589
9eae7c52
TT
25590/* Return the offset size given in the compilation unit header for CU. */
25591
25592int
25593dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25594{
96408a79
SA
25595 struct comp_unit_head cu_header_local;
25596 const struct comp_unit_head *cu_headerp;
9c6c53f7 25597
96408a79
SA
25598 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25599
25600 return cu_headerp->offset_size;
25601}
25602
25603/* See its dwarf2loc.h declaration. */
25604
25605int
25606dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25607{
25608 struct comp_unit_head cu_header_local;
25609 const struct comp_unit_head *cu_headerp;
25610
25611 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25612
25613 if (cu_headerp->version == 2)
25614 return cu_headerp->addr_size;
25615 else
25616 return cu_headerp->offset_size;
181cebd4
JK
25617}
25618
9aa1f1e3
TT
25619/* Return the text offset of the CU. The returned offset comes from
25620 this CU's objfile. If this objfile came from a separate debuginfo
25621 file, then the offset may be different from the corresponding
25622 offset in the parent objfile. */
25623
25624CORE_ADDR
25625dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25626{
e3b94546 25627 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25628
25629 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25630}
25631
9a49df9d
AB
25632/* Return a type that is a generic pointer type, the size of which matches
25633 the address size given in the compilation unit header for PER_CU. */
25634static struct type *
25635dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25636{
25637 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25638 struct type *void_type = objfile_type (objfile)->builtin_void;
25639 struct type *addr_type = lookup_pointer_type (void_type);
25640 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25641
25642 if (TYPE_LENGTH (addr_type) == addr_size)
25643 return addr_type;
25644
25645 addr_type
25646 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25647 return addr_type;
25648}
25649
43988095
JK
25650/* Return DWARF version number of PER_CU. */
25651
25652short
25653dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25654{
25655 return per_cu->dwarf_version;
25656}
25657
348e048f
DE
25658/* Locate the .debug_info compilation unit from CU's objfile which contains
25659 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25660
25661static struct dwarf2_per_cu_data *
9c541725 25662dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25663 unsigned int offset_in_dwz,
ed2dc618 25664 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25665{
25666 struct dwarf2_per_cu_data *this_cu;
25667 int low, high;
25668
ae038cb0 25669 low = 0;
b76e467d 25670 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25671 while (high > low)
25672 {
36586728 25673 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25674 int mid = low + (high - low) / 2;
9a619af0 25675
36586728 25676 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25677 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25678 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25679 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25680 high = mid;
25681 else
25682 low = mid + 1;
25683 }
25684 gdb_assert (low == high);
36586728 25685 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25686 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25687 {
36586728 25688 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25689 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25690 "offset %s [in module %s]"),
25691 sect_offset_str (sect_off),
ed2dc618 25692 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25693
9c541725
PA
25694 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25695 <= sect_off);
ae038cb0
DJ
25696 return dwarf2_per_objfile->all_comp_units[low-1];
25697 }
25698 else
25699 {
b76e467d 25700 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25701 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25702 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25703 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25704 return this_cu;
25705 }
25706}
25707
23745b47 25708/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25709
fcd3b13d
SM
25710dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25711 : per_cu (per_cu_),
9068261f
AB
25712 mark (false),
25713 has_loclist (false),
25714 checked_producer (false),
25715 producer_is_gxx_lt_4_6 (false),
25716 producer_is_gcc_lt_4_3 (false),
eb77c9df 25717 producer_is_icc (false),
9068261f 25718 producer_is_icc_lt_14 (false),
c258c396 25719 producer_is_codewarrior (false),
9068261f 25720 processing_has_namespace_info (false)
93311388 25721{
fcd3b13d
SM
25722 per_cu->cu = this;
25723}
25724
25725/* Destroy a dwarf2_cu. */
25726
25727dwarf2_cu::~dwarf2_cu ()
25728{
25729 per_cu->cu = NULL;
9816fde3
JK
25730}
25731
25732/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25733
25734static void
95554aad
TT
25735prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25736 enum language pretend_language)
9816fde3
JK
25737{
25738 struct attribute *attr;
25739
25740 /* Set the language we're debugging. */
25741 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25742 if (attr != nullptr)
9816fde3
JK
25743 set_cu_language (DW_UNSND (attr), cu);
25744 else
9cded63f 25745 {
95554aad 25746 cu->language = pretend_language;
9cded63f
TT
25747 cu->language_defn = language_def (cu->language);
25748 }
dee91e82 25749
7d45c7c3 25750 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25751}
25752
ae038cb0
DJ
25753/* Increase the age counter on each cached compilation unit, and free
25754 any that are too old. */
25755
25756static void
ed2dc618 25757age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25758{
25759 struct dwarf2_per_cu_data *per_cu, **last_chain;
25760
25761 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25762 per_cu = dwarf2_per_objfile->read_in_chain;
25763 while (per_cu != NULL)
25764 {
25765 per_cu->cu->last_used ++;
b4f54984 25766 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25767 dwarf2_mark (per_cu->cu);
25768 per_cu = per_cu->cu->read_in_chain;
25769 }
25770
25771 per_cu = dwarf2_per_objfile->read_in_chain;
25772 last_chain = &dwarf2_per_objfile->read_in_chain;
25773 while (per_cu != NULL)
25774 {
25775 struct dwarf2_per_cu_data *next_cu;
25776
25777 next_cu = per_cu->cu->read_in_chain;
25778
25779 if (!per_cu->cu->mark)
25780 {
fcd3b13d 25781 delete per_cu->cu;
ae038cb0
DJ
25782 *last_chain = next_cu;
25783 }
25784 else
25785 last_chain = &per_cu->cu->read_in_chain;
25786
25787 per_cu = next_cu;
25788 }
25789}
25790
25791/* Remove a single compilation unit from the cache. */
25792
25793static void
dee91e82 25794free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25795{
25796 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25797 struct dwarf2_per_objfile *dwarf2_per_objfile
25798 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25799
25800 per_cu = dwarf2_per_objfile->read_in_chain;
25801 last_chain = &dwarf2_per_objfile->read_in_chain;
25802 while (per_cu != NULL)
25803 {
25804 struct dwarf2_per_cu_data *next_cu;
25805
25806 next_cu = per_cu->cu->read_in_chain;
25807
dee91e82 25808 if (per_cu == target_per_cu)
ae038cb0 25809 {
fcd3b13d 25810 delete per_cu->cu;
dee91e82 25811 per_cu->cu = NULL;
ae038cb0
DJ
25812 *last_chain = next_cu;
25813 break;
25814 }
25815 else
25816 last_chain = &per_cu->cu->read_in_chain;
25817
25818 per_cu = next_cu;
25819 }
25820}
25821
dee91e82
DE
25822/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25823 We store these in a hash table separate from the DIEs, and preserve them
25824 when the DIEs are flushed out of cache.
25825
25826 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25827 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25828 or the type may come from a DWO file. Furthermore, while it's more logical
25829 to use per_cu->section+offset, with Fission the section with the data is in
25830 the DWO file but we don't know that section at the point we need it.
25831 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25832 because we can enter the lookup routine, get_die_type_at_offset, from
25833 outside this file, and thus won't necessarily have PER_CU->cu.
25834 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25835
dee91e82 25836struct dwarf2_per_cu_offset_and_type
1c379e20 25837{
dee91e82 25838 const struct dwarf2_per_cu_data *per_cu;
9c541725 25839 sect_offset sect_off;
1c379e20
DJ
25840 struct type *type;
25841};
25842
dee91e82 25843/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25844
25845static hashval_t
dee91e82 25846per_cu_offset_and_type_hash (const void *item)
1c379e20 25847{
9a3c8263
SM
25848 const struct dwarf2_per_cu_offset_and_type *ofs
25849 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25850
9c541725 25851 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25852}
25853
dee91e82 25854/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25855
25856static int
dee91e82 25857per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25858{
9a3c8263
SM
25859 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25860 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25861 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25862 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25863
dee91e82 25864 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25865 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25866}
25867
25868/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25869 table if necessary. For convenience, return TYPE.
25870
25871 The DIEs reading must have careful ordering to:
85102364 25872 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25873 reading current DIE.
25874 * Not trying to dereference contents of still incompletely read in types
25875 while reading in other DIEs.
25876 * Enable referencing still incompletely read in types just by a pointer to
25877 the type without accessing its fields.
25878
25879 Therefore caller should follow these rules:
25880 * Try to fetch any prerequisite types we may need to build this DIE type
25881 before building the type and calling set_die_type.
e71ec853 25882 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25883 possible before fetching more types to complete the current type.
25884 * Make the type as complete as possible before fetching more types. */
1c379e20 25885
f792889a 25886static struct type *
1c379e20
DJ
25887set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25888{
518817b3
SM
25889 struct dwarf2_per_objfile *dwarf2_per_objfile
25890 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25891 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25892 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25893 struct attribute *attr;
25894 struct dynamic_prop prop;
1c379e20 25895
b4ba55a1
JB
25896 /* For Ada types, make sure that the gnat-specific data is always
25897 initialized (if not already set). There are a few types where
25898 we should not be doing so, because the type-specific area is
25899 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25900 where the type-specific area is used to store the floatformat).
25901 But this is not a problem, because the gnat-specific information
25902 is actually not needed for these types. */
25903 if (need_gnat_info (cu)
25904 && TYPE_CODE (type) != TYPE_CODE_FUNC
25905 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25906 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25907 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25908 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25909 && !HAVE_GNAT_AUX_INFO (type))
25910 INIT_GNAT_SPECIFIC (type);
25911
3f2f83dd
KB
25912 /* Read DW_AT_allocated and set in type. */
25913 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25914 if (attr_form_is_block (attr))
25915 {
9a49df9d
AB
25916 struct type *prop_type
25917 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25918 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25919 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25920 }
25921 else if (attr != NULL)
25922 {
b98664d3 25923 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25924 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25925 sect_offset_str (die->sect_off));
3f2f83dd
KB
25926 }
25927
25928 /* Read DW_AT_associated and set in type. */
25929 attr = dwarf2_attr (die, DW_AT_associated, cu);
25930 if (attr_form_is_block (attr))
25931 {
9a49df9d
AB
25932 struct type *prop_type
25933 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25934 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25935 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25936 }
25937 else if (attr != NULL)
25938 {
b98664d3 25939 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25940 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25941 sect_offset_str (die->sect_off));
3f2f83dd
KB
25942 }
25943
3cdcd0ce
JB
25944 /* Read DW_AT_data_location and set in type. */
25945 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25946 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25947 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25948 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25949
dee91e82 25950 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25951 {
dee91e82
DE
25952 dwarf2_per_objfile->die_type_hash =
25953 htab_create_alloc_ex (127,
25954 per_cu_offset_and_type_hash,
25955 per_cu_offset_and_type_eq,
25956 NULL,
25957 &objfile->objfile_obstack,
25958 hashtab_obstack_allocate,
25959 dummy_obstack_deallocate);
f792889a 25960 }
1c379e20 25961
dee91e82 25962 ofs.per_cu = cu->per_cu;
9c541725 25963 ofs.sect_off = die->sect_off;
1c379e20 25964 ofs.type = type;
dee91e82
DE
25965 slot = (struct dwarf2_per_cu_offset_and_type **)
25966 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25967 if (*slot)
b98664d3 25968 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25969 sect_offset_str (die->sect_off));
8d749320
SM
25970 *slot = XOBNEW (&objfile->objfile_obstack,
25971 struct dwarf2_per_cu_offset_and_type);
1c379e20 25972 **slot = ofs;
f792889a 25973 return type;
1c379e20
DJ
25974}
25975
9c541725 25976/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25977 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25978
25979static struct type *
9c541725 25980get_die_type_at_offset (sect_offset sect_off,
673bfd45 25981 struct dwarf2_per_cu_data *per_cu)
1c379e20 25982{
dee91e82 25983 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25984 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25985
dee91e82 25986 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25987 return NULL;
1c379e20 25988
dee91e82 25989 ofs.per_cu = per_cu;
9c541725 25990 ofs.sect_off = sect_off;
9a3c8263
SM
25991 slot = ((struct dwarf2_per_cu_offset_and_type *)
25992 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25993 if (slot)
25994 return slot->type;
25995 else
25996 return NULL;
25997}
25998
02142a6c 25999/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
26000 or return NULL if DIE does not have a saved type. */
26001
26002static struct type *
26003get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26004{
9c541725 26005 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
26006}
26007
10b3939b
DJ
26008/* Add a dependence relationship from CU to REF_PER_CU. */
26009
26010static void
26011dwarf2_add_dependence (struct dwarf2_cu *cu,
26012 struct dwarf2_per_cu_data *ref_per_cu)
26013{
26014 void **slot;
26015
26016 if (cu->dependencies == NULL)
26017 cu->dependencies
26018 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26019 NULL, &cu->comp_unit_obstack,
26020 hashtab_obstack_allocate,
26021 dummy_obstack_deallocate);
26022
26023 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26024 if (*slot == NULL)
26025 *slot = ref_per_cu;
26026}
1c379e20 26027
f504f079
DE
26028/* Subroutine of dwarf2_mark to pass to htab_traverse.
26029 Set the mark field in every compilation unit in the
ae038cb0
DJ
26030 cache that we must keep because we are keeping CU. */
26031
10b3939b
DJ
26032static int
26033dwarf2_mark_helper (void **slot, void *data)
26034{
26035 struct dwarf2_per_cu_data *per_cu;
26036
26037 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
26038
26039 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26040 reading of the chain. As such dependencies remain valid it is not much
26041 useful to track and undo them during QUIT cleanups. */
26042 if (per_cu->cu == NULL)
26043 return 1;
26044
10b3939b
DJ
26045 if (per_cu->cu->mark)
26046 return 1;
9068261f 26047 per_cu->cu->mark = true;
10b3939b
DJ
26048
26049 if (per_cu->cu->dependencies != NULL)
26050 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26051
26052 return 1;
26053}
26054
f504f079
DE
26055/* Set the mark field in CU and in every other compilation unit in the
26056 cache that we must keep because we are keeping CU. */
26057
ae038cb0
DJ
26058static void
26059dwarf2_mark (struct dwarf2_cu *cu)
26060{
26061 if (cu->mark)
26062 return;
9068261f 26063 cu->mark = true;
10b3939b
DJ
26064 if (cu->dependencies != NULL)
26065 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
26066}
26067
26068static void
26069dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26070{
26071 while (per_cu)
26072 {
9068261f 26073 per_cu->cu->mark = false;
ae038cb0
DJ
26074 per_cu = per_cu->cu->read_in_chain;
26075 }
72bf9492
DJ
26076}
26077
72bf9492
DJ
26078/* Trivial hash function for partial_die_info: the hash value of a DIE
26079 is its offset in .debug_info for this objfile. */
26080
26081static hashval_t
26082partial_die_hash (const void *item)
26083{
9a3c8263
SM
26084 const struct partial_die_info *part_die
26085 = (const struct partial_die_info *) item;
9a619af0 26086
9c541725 26087 return to_underlying (part_die->sect_off);
72bf9492
DJ
26088}
26089
26090/* Trivial comparison function for partial_die_info structures: two DIEs
26091 are equal if they have the same offset. */
26092
26093static int
26094partial_die_eq (const void *item_lhs, const void *item_rhs)
26095{
9a3c8263
SM
26096 const struct partial_die_info *part_die_lhs
26097 = (const struct partial_die_info *) item_lhs;
26098 const struct partial_die_info *part_die_rhs
26099 = (const struct partial_die_info *) item_rhs;
9a619af0 26100
9c541725 26101 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
26102}
26103
3c3bb058
AB
26104struct cmd_list_element *set_dwarf_cmdlist;
26105struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
26106
26107static void
981a3fb3 26108set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 26109{
b4f54984 26110 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 26111 gdb_stdout);
ae038cb0
DJ
26112}
26113
26114static void
981a3fb3 26115show_dwarf_cmd (const char *args, int from_tty)
6e70227d 26116{
b4f54984 26117 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
26118}
26119
491144b5 26120bool dwarf_always_disassemble;
437afbb8 26121
437afbb8 26122static void
cd4fb1b2
SM
26123show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26124 struct cmd_list_element *c, const char *value)
9291a0cd 26125{
cd4fb1b2
SM
26126 fprintf_filtered (file,
26127 _("Whether to always disassemble "
26128 "DWARF expressions is %s.\n"),
26129 value);
9291a0cd
TT
26130}
26131
9291a0cd 26132static void
cd4fb1b2
SM
26133show_check_physname (struct ui_file *file, int from_tty,
26134 struct cmd_list_element *c, const char *value)
9291a0cd 26135{
cd4fb1b2
SM
26136 fprintf_filtered (file,
26137 _("Whether to check \"physname\" is %s.\n"),
26138 value);
9291a0cd
TT
26139}
26140
cd4fb1b2
SM
26141void
26142_initialize_dwarf2_read (void)
9291a0cd 26143{
cd4fb1b2
SM
26144 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26145Set DWARF specific variables.\n\
590042fc 26146Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
26147 &set_dwarf_cmdlist, "maintenance set dwarf ",
26148 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 26149
cd4fb1b2 26150 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
26151Show DWARF specific variables.\n\
26152Show DWARF variables such as the cache size."),
cd4fb1b2
SM
26153 &show_dwarf_cmdlist, "maintenance show dwarf ",
26154 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 26155
cd4fb1b2
SM
26156 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26157 &dwarf_max_cache_age, _("\
26158Set the upper bound on the age of cached DWARF compilation units."), _("\
26159Show the upper bound on the age of cached DWARF compilation units."), _("\
26160A higher limit means that cached compilation units will be stored\n\
26161in memory longer, and more total memory will be used. Zero disables\n\
26162caching, which can slow down startup."),
26163 NULL,
26164 show_dwarf_max_cache_age,
26165 &set_dwarf_cmdlist,
26166 &show_dwarf_cmdlist);
156942c7 26167
cd4fb1b2
SM
26168 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26169 &dwarf_always_disassemble, _("\
26170Set whether `info address' always disassembles DWARF expressions."), _("\
26171Show whether `info address' always disassembles DWARF expressions."), _("\
26172When enabled, DWARF expressions are always printed in an assembly-like\n\
26173syntax. When disabled, expressions will be printed in a more\n\
26174conversational style, when possible."),
26175 NULL,
26176 show_dwarf_always_disassemble,
26177 &set_dwarf_cmdlist,
26178 &show_dwarf_cmdlist);
9291a0cd 26179
cd4fb1b2
SM
26180 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26181Set debugging of the DWARF reader."), _("\
26182Show debugging of the DWARF reader."), _("\
26183When enabled (non-zero), debugging messages are printed during DWARF\n\
26184reading and symtab expansion. A value of 1 (one) provides basic\n\
26185information. A value greater than 1 provides more verbose information."),
26186 NULL,
26187 NULL,
26188 &setdebuglist, &showdebuglist);
9291a0cd 26189
cd4fb1b2
SM
26190 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26191Set debugging of the DWARF DIE reader."), _("\
26192Show debugging of the DWARF DIE reader."), _("\
26193When enabled (non-zero), DIEs are dumped after they are read in.\n\
26194The value is the maximum depth to print."),
26195 NULL,
26196 NULL,
26197 &setdebuglist, &showdebuglist);
9291a0cd 26198
cd4fb1b2
SM
26199 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26200Set debugging of the dwarf line reader."), _("\
26201Show debugging of the dwarf line reader."), _("\
26202When enabled (non-zero), line number entries are dumped as they are read in.\n\
26203A value of 1 (one) provides basic information.\n\
26204A value greater than 1 provides more verbose information."),
26205 NULL,
26206 NULL,
26207 &setdebuglist, &showdebuglist);
437afbb8 26208
cd4fb1b2
SM
26209 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26210Set cross-checking of \"physname\" code against demangler."), _("\
26211Show cross-checking of \"physname\" code against demangler."), _("\
26212When enabled, GDB's internal \"physname\" code is checked against\n\
26213the demangler."),
26214 NULL, show_check_physname,
26215 &setdebuglist, &showdebuglist);
900e11f9 26216
e615022a
DE
26217 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26218 no_class, &use_deprecated_index_sections, _("\
26219Set whether to use deprecated gdb_index sections."), _("\
26220Show whether to use deprecated gdb_index sections."), _("\
26221When enabled, deprecated .gdb_index sections are used anyway.\n\
26222Normally they are ignored either because of a missing feature or\n\
26223performance issue.\n\
26224Warning: This option must be enabled before gdb reads the file."),
26225 NULL,
26226 NULL,
26227 &setlist, &showlist);
26228
f1e6e072
TT
26229 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26230 &dwarf2_locexpr_funcs);
26231 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26232 &dwarf2_loclist_funcs);
26233
26234 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26235 &dwarf2_block_frame_base_locexpr_funcs);
26236 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26237 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26238
26239#if GDB_SELF_TEST
26240 selftests::register_test ("dw2_expand_symtabs_matching",
26241 selftests::dw2_expand_symtabs_matching::run_test);
26242#endif
6502dd73 26243}
This page took 5.157192 seconds and 4 git commands to generate.