Use unique_xmalloc_ptr in fixup_go_packaging
[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#ifndef ATTR_ALLOC_CHUNK
1343#define ATTR_ALLOC_CHUNK 4
1344#endif
1345
c906108c
SS
1346/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1347 but this would require a corresponding change in unpack_field_as_long
1348 and friends. */
1349static int bits_per_byte = 8;
1350
2ddeaf8a
TT
1351/* When reading a variant or variant part, we track a bit more
1352 information about the field, and store it in an object of this
1353 type. */
1354
1355struct variant_field
1356{
1357 /* If we see a DW_TAG_variant, then this will be the discriminant
1358 value. */
1359 ULONGEST discriminant_value;
1360 /* If we see a DW_TAG_variant, then this will be set if this is the
1361 default branch. */
1362 bool default_branch;
1363 /* While reading a DW_TAG_variant_part, this will be set if this
1364 field is the discriminant. */
1365 bool is_discriminant;
1366};
1367
52059ffd
TT
1368struct nextfield
1369{
be2daae6
TT
1370 int accessibility = 0;
1371 int virtuality = 0;
2ddeaf8a 1372 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1373 struct variant_field variant {};
1374 struct field field {};
52059ffd
TT
1375};
1376
1377struct fnfieldlist
1378{
be2daae6
TT
1379 const char *name = nullptr;
1380 std::vector<struct fn_field> fnfields;
52059ffd
TT
1381};
1382
c906108c
SS
1383/* The routines that read and process dies for a C struct or C++ class
1384 pass lists of data member fields and lists of member function fields
1385 in an instance of a field_info structure, as defined below. */
1386struct field_info
c5aa993b 1387 {
0963b4bd 1388 /* List of data member and baseclasses fields. */
be2daae6
TT
1389 std::vector<struct nextfield> fields;
1390 std::vector<struct nextfield> baseclasses;
c906108c 1391
7d0ccb61 1392 /* Number of fields (including baseclasses). */
be2daae6 1393 int nfields = 0;
c906108c 1394
85102364 1395 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1396 int non_public_fields = 0;
c906108c 1397
c5aa993b
JM
1398 /* Member function fieldlist array, contains name of possibly overloaded
1399 member function, number of overloaded member functions and a pointer
1400 to the head of the member function field chain. */
be2daae6 1401 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1402
1403 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1404 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1405 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1406
1407 /* Nested types defined by this class and the number of elements in this
1408 list. */
be2daae6 1409 std::vector<struct decl_field> nested_types_list;
c5aa993b 1410 };
c906108c 1411
10b3939b
DJ
1412/* One item on the queue of compilation units to read in full symbols
1413 for. */
1414struct dwarf2_queue_item
1415{
1416 struct dwarf2_per_cu_data *per_cu;
95554aad 1417 enum language pretend_language;
10b3939b
DJ
1418 struct dwarf2_queue_item *next;
1419};
1420
1421/* The current queue. */
1422static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1423
ae038cb0
DJ
1424/* Loaded secondary compilation units are kept in memory until they
1425 have not been referenced for the processing of this many
1426 compilation units. Set this to zero to disable caching. Cache
1427 sizes of up to at least twenty will improve startup time for
1428 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1429static int dwarf_max_cache_age = 5;
920d2a44 1430static void
b4f54984
DE
1431show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1432 struct cmd_list_element *c, const char *value)
920d2a44 1433{
3e43a32a 1434 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1435 "DWARF compilation units is %s.\n"),
920d2a44
AC
1436 value);
1437}
4390d890 1438\f
c906108c
SS
1439/* local function prototypes */
1440
a32a8923
DE
1441static const char *get_section_name (const struct dwarf2_section_info *);
1442
1443static const char *get_section_file_name (const struct dwarf2_section_info *);
1444
918dd910
JK
1445static void dwarf2_find_base_address (struct die_info *die,
1446 struct dwarf2_cu *cu);
1447
0018ea6f
DE
1448static struct partial_symtab *create_partial_symtab
1449 (struct dwarf2_per_cu_data *per_cu, const char *name);
1450
f1902523
JK
1451static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1452 const gdb_byte *info_ptr,
1453 struct die_info *type_unit_die,
1454 int has_children, void *data);
1455
ed2dc618
SM
1456static void dwarf2_build_psymtabs_hard
1457 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1458
72bf9492
DJ
1459static void scan_partial_symbols (struct partial_die_info *,
1460 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1461 int, struct dwarf2_cu *);
c906108c 1462
72bf9492
DJ
1463static void add_partial_symbol (struct partial_die_info *,
1464 struct dwarf2_cu *);
63d06c5c 1465
72bf9492
DJ
1466static void add_partial_namespace (struct partial_die_info *pdi,
1467 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1468 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1469
5d7cb8df 1470static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1471 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1472 struct dwarf2_cu *cu);
1473
72bf9492
DJ
1474static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1475 struct dwarf2_cu *cu);
91c24f0a 1476
bc30ff58
JB
1477static void add_partial_subprogram (struct partial_die_info *pdi,
1478 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1479 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1480
257e7a09
YQ
1481static void dwarf2_read_symtab (struct partial_symtab *,
1482 struct objfile *);
c906108c 1483
a14ed312 1484static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1485
685af9cd 1486static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1487 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1488 sect_offset);
433df2d4 1489
d521ce57 1490static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1491
dee91e82 1492static struct partial_die_info *load_partial_dies
d521ce57 1493 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1494
fb816e8b
TV
1495/* A pair of partial_die_info and compilation unit. */
1496struct cu_partial_die_info
1497{
1498 /* The compilation unit of the partial_die_info. */
1499 struct dwarf2_cu *cu;
1500 /* A partial_die_info. */
1501 struct partial_die_info *pdi;
122cf0f2
AB
1502
1503 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1504 : cu (cu),
1505 pdi (pdi)
405feb71 1506 { /* Nothing. */ }
122cf0f2
AB
1507
1508private:
1509 cu_partial_die_info () = delete;
fb816e8b
TV
1510};
1511
122cf0f2
AB
1512static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1513 struct dwarf2_cu *);
72bf9492 1514
d521ce57
TT
1515static const gdb_byte *read_attribute (const struct die_reader_specs *,
1516 struct attribute *, struct attr_abbrev *,
1517 const gdb_byte *);
a8329558 1518
a1855c1d 1519static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1520
a1855c1d 1521static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1522
a1855c1d 1523static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1524
15f18d14
AT
1525/* Read the next three bytes (little-endian order) as an unsigned integer. */
1526static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1527
a1855c1d 1528static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1529
a1855c1d 1530static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1531
d521ce57 1532static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1533 unsigned int *);
c906108c 1534
d521ce57 1535static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1536
1537static LONGEST read_checked_initial_length_and_offset
d521ce57 1538 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1539 unsigned int *, unsigned int *);
613e1657 1540
d521ce57
TT
1541static LONGEST read_offset (bfd *, const gdb_byte *,
1542 const struct comp_unit_head *,
c764a876
DE
1543 unsigned int *);
1544
d521ce57 1545static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1546
ed2dc618
SM
1547static sect_offset read_abbrev_offset
1548 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1549 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1550
d521ce57 1551static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1552
d521ce57 1553static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1554
ed2dc618
SM
1555static const char *read_indirect_string
1556 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1557 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1558
ed2dc618
SM
1559static const char *read_indirect_line_string
1560 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1561 const struct comp_unit_head *, unsigned int *);
36586728 1562
ed2dc618
SM
1563static const char *read_indirect_string_at_offset
1564 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1565 LONGEST str_offset);
927aa2e7 1566
ed2dc618
SM
1567static const char *read_indirect_string_from_dwz
1568 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1569
d521ce57 1570static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1571
d521ce57
TT
1572static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1573 const gdb_byte *,
3019eac3
DE
1574 unsigned int *);
1575
d521ce57 1576static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1577 ULONGEST str_index);
3019eac3 1578
e142c38c 1579static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1580
e142c38c
DJ
1581static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1582 struct dwarf2_cu *);
c906108c 1583
348e048f 1584static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1585 unsigned int);
348e048f 1586
7d45c7c3
KB
1587static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1588 struct dwarf2_cu *cu);
1589
a084a2a6
AT
1590static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1591
05cf31d1
JB
1592static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1593 struct dwarf2_cu *cu);
1594
e142c38c 1595static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1596
e142c38c 1597static struct die_info *die_specification (struct die_info *die,
f2f0e013 1598 struct dwarf2_cu **);
63d06c5c 1599
9c541725 1600static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1601 struct dwarf2_cu *cu);
debd256d 1602
f3f5162e 1603static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1604 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1605 CORE_ADDR, int decode_mapping);
c906108c 1606
804d2729
TT
1607static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1608 const char *);
c906108c 1609
a14ed312 1610static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1611 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1612
ff39bb5e 1613static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1614 struct dwarf2_cu *);
c906108c 1615
ff39bb5e 1616static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1617 struct type *type,
1618 const char *name,
1619 struct obstack *obstack,
12df843f 1620 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1621 const gdb_byte **bytes,
98bfdba5 1622 struct dwarf2_locexpr_baton **baton);
2df3850c 1623
e7c27a73 1624static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1625
b4ba55a1
JB
1626static int need_gnat_info (struct dwarf2_cu *);
1627
3e43a32a
MS
1628static struct type *die_descriptive_type (struct die_info *,
1629 struct dwarf2_cu *);
b4ba55a1
JB
1630
1631static void set_descriptive_type (struct type *, struct die_info *,
1632 struct dwarf2_cu *);
1633
e7c27a73
DJ
1634static struct type *die_containing_type (struct die_info *,
1635 struct dwarf2_cu *);
c906108c 1636
ff39bb5e 1637static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1638 struct dwarf2_cu *);
c906108c 1639
f792889a 1640static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1641
673bfd45
DE
1642static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1643
0d5cff50 1644static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1645
6e70227d 1646static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1647 const char *suffix, int physname,
1648 struct dwarf2_cu *cu);
63d06c5c 1649
e7c27a73 1650static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1651
348e048f
DE
1652static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1653
e7c27a73 1654static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1655
e7c27a73 1656static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1657
96408a79
SA
1658static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1659
71a3c369
TT
1660static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1661
ff013f42
JK
1662static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1663 struct dwarf2_cu *, struct partial_symtab *);
1664
3a2b436a 1665/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1666 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1667enum pc_bounds_kind
1668{
e385593e 1669 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1670 PC_BOUNDS_NOT_PRESENT,
1671
e385593e
JK
1672 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1673 were present but they do not form a valid range of PC addresses. */
1674 PC_BOUNDS_INVALID,
1675
3a2b436a
JK
1676 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1677 PC_BOUNDS_RANGES,
1678
1679 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1680 PC_BOUNDS_HIGH_LOW,
1681};
1682
1683static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1684 CORE_ADDR *, CORE_ADDR *,
1685 struct dwarf2_cu *,
1686 struct partial_symtab *);
c906108c 1687
fae299cd
DC
1688static void get_scope_pc_bounds (struct die_info *,
1689 CORE_ADDR *, CORE_ADDR *,
1690 struct dwarf2_cu *);
1691
801e3a5b
JB
1692static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1693 CORE_ADDR, struct dwarf2_cu *);
1694
a14ed312 1695static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1696 struct dwarf2_cu *);
c906108c 1697
a14ed312 1698static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1699 struct type *, struct dwarf2_cu *);
c906108c 1700
a14ed312 1701static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1702 struct die_info *, struct type *,
e7c27a73 1703 struct dwarf2_cu *);
c906108c 1704
a14ed312 1705static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1706 struct type *,
1707 struct dwarf2_cu *);
c906108c 1708
134d01f1 1709static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1710
e7c27a73 1711static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1712
e7c27a73 1713static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1714
5d7cb8df
JK
1715static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1716
804d2729 1717static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1718
27aa8d6a
SW
1719static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1720
74921315
KS
1721static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1722
f55ee35c
JK
1723static struct type *read_module_type (struct die_info *die,
1724 struct dwarf2_cu *cu);
1725
38d518c9 1726static const char *namespace_name (struct die_info *die,
e142c38c 1727 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1728
134d01f1 1729static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1730
e7c27a73 1731static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1732
6e70227d 1733static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1734 struct dwarf2_cu *);
1735
bf6af496 1736static struct die_info *read_die_and_siblings_1
d521ce57 1737 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1738 struct die_info *);
639d11d3 1739
dee91e82 1740static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1741 const gdb_byte *info_ptr,
1742 const gdb_byte **new_info_ptr,
639d11d3
DC
1743 struct die_info *parent);
1744
d521ce57
TT
1745static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1746 struct die_info **, const gdb_byte *,
1747 int *, int);
3019eac3 1748
d521ce57
TT
1749static const gdb_byte *read_full_die (const struct die_reader_specs *,
1750 struct die_info **, const gdb_byte *,
1751 int *);
93311388 1752
e7c27a73 1753static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1754
15d034d0
TT
1755static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1756 struct obstack *);
71c25dea 1757
15d034d0 1758static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1759
15d034d0 1760static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1761 struct die_info *die,
1762 struct dwarf2_cu *cu);
1763
ca69b9e6
DE
1764static const char *dwarf2_physname (const char *name, struct die_info *die,
1765 struct dwarf2_cu *cu);
1766
e142c38c 1767static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1768 struct dwarf2_cu **);
9219021c 1769
f39c6ffd 1770static const char *dwarf_tag_name (unsigned int);
c906108c 1771
f39c6ffd 1772static const char *dwarf_attr_name (unsigned int);
c906108c 1773
a084a2a6
AT
1774static const char *dwarf_unit_type_name (int unit_type);
1775
f39c6ffd 1776static const char *dwarf_form_name (unsigned int);
c906108c 1777
a121b7c1 1778static const char *dwarf_bool_name (unsigned int);
c906108c 1779
f39c6ffd 1780static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1781
f9aca02d 1782static struct die_info *sibling_die (struct die_info *);
c906108c 1783
d97bc12b
DE
1784static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1785
1786static void dump_die_for_error (struct die_info *);
1787
1788static void dump_die_1 (struct ui_file *, int level, int max_level,
1789 struct die_info *);
c906108c 1790
d97bc12b 1791/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1792
51545339 1793static void store_in_ref_table (struct die_info *,
10b3939b 1794 struct dwarf2_cu *);
c906108c 1795
ff39bb5e 1796static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1797
ff39bb5e 1798static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1799
348e048f 1800static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1801 const struct attribute *,
348e048f
DE
1802 struct dwarf2_cu **);
1803
10b3939b 1804static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1805 const struct attribute *,
f2f0e013 1806 struct dwarf2_cu **);
c906108c 1807
348e048f 1808static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1809 const struct attribute *,
348e048f
DE
1810 struct dwarf2_cu **);
1811
ac9ec31b
DE
1812static struct type *get_signatured_type (struct die_info *, ULONGEST,
1813 struct dwarf2_cu *);
1814
1815static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1816 const struct attribute *,
ac9ec31b
DE
1817 struct dwarf2_cu *);
1818
e5fe5e75 1819static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1820
52dc124a 1821static void read_signatured_type (struct signatured_type *);
348e048f 1822
63e43d3a
PMR
1823static int attr_to_dynamic_prop (const struct attribute *attr,
1824 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1825 struct dynamic_prop *prop, struct type *type);
63e43d3a 1826
c906108c
SS
1827/* memory allocation interface */
1828
7b5a2f43 1829static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1830
b60c80d6 1831static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1832
43f3e411 1833static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1834
6e5a29e1 1835static int attr_form_is_block (const struct attribute *);
8e19ed76 1836
6e5a29e1 1837static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1838
6e5a29e1 1839static int attr_form_is_constant (const struct attribute *);
3690dd37 1840
6e5a29e1 1841static int attr_form_is_ref (const struct attribute *);
7771576e 1842
8cf6f0b1
TT
1843static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1844 struct dwarf2_loclist_baton *baton,
ff39bb5e 1845 const struct attribute *attr);
8cf6f0b1 1846
ff39bb5e 1847static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1848 struct symbol *sym,
f1e6e072
TT
1849 struct dwarf2_cu *cu,
1850 int is_block);
4c2df51b 1851
d521ce57
TT
1852static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1853 const gdb_byte *info_ptr,
1854 struct abbrev_info *abbrev);
4bb7a0a7 1855
72bf9492
DJ
1856static hashval_t partial_die_hash (const void *item);
1857
1858static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1859
ae038cb0 1860static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1861 (sect_offset sect_off, unsigned int offset_in_dwz,
1862 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1863
9816fde3 1864static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1865 struct die_info *comp_unit_die,
1866 enum language pretend_language);
93311388 1867
ed2dc618 1868static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1869
dee91e82 1870static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1871
f792889a
DJ
1872static struct type *set_die_type (struct die_info *, struct type *,
1873 struct dwarf2_cu *);
1c379e20 1874
ed2dc618 1875static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1876
ed2dc618 1877static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1878
58f0c718 1879static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1880 enum language);
10b3939b 1881
95554aad
TT
1882static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1883 enum language);
10b3939b 1884
f4dc4d17
DE
1885static void process_full_type_unit (struct dwarf2_per_cu_data *,
1886 enum language);
1887
10b3939b
DJ
1888static void dwarf2_add_dependence (struct dwarf2_cu *,
1889 struct dwarf2_per_cu_data *);
1890
ae038cb0
DJ
1891static void dwarf2_mark (struct dwarf2_cu *);
1892
1893static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1894
b64f50a1 1895static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1896 struct dwarf2_per_cu_data *);
673bfd45 1897
f792889a 1898static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1899
95554aad
TT
1900static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1901 enum language pretend_language);
1902
ed2dc618 1903static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1904
9a49df9d
AB
1905static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1906static struct type *dwarf2_per_cu_addr_sized_int_type
1907 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1908static struct type *dwarf2_per_cu_int_type
1909 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1910 bool unsigned_p);
9a49df9d 1911
b303c6f6
AB
1912/* Class, the destructor of which frees all allocated queue entries. This
1913 will only have work to do if an error was thrown while processing the
1914 dwarf. If no error was thrown then the queue entries should have all
1915 been processed, and freed, as we went along. */
1916
1917class dwarf2_queue_guard
1918{
1919public:
1920 dwarf2_queue_guard () = default;
1921
1922 /* Free any entries remaining on the queue. There should only be
1923 entries left if we hit an error while processing the dwarf. */
1924 ~dwarf2_queue_guard ()
1925 {
1926 struct dwarf2_queue_item *item, *last;
1927
1928 item = dwarf2_queue;
1929 while (item)
1930 {
1931 /* Anything still marked queued is likely to be in an
1932 inconsistent state, so discard it. */
1933 if (item->per_cu->queued)
1934 {
1935 if (item->per_cu->cu != NULL)
1936 free_one_cached_comp_unit (item->per_cu);
1937 item->per_cu->queued = 0;
1938 }
1939
1940 last = item;
1941 item = item->next;
1942 xfree (last);
1943 }
1944
1945 dwarf2_queue = dwarf2_queue_tail = NULL;
1946 }
1947};
1948
d721ba37
PA
1949/* The return type of find_file_and_directory. Note, the enclosed
1950 string pointers are only valid while this object is valid. */
1951
1952struct file_and_directory
1953{
1954 /* The filename. This is never NULL. */
1955 const char *name;
1956
1957 /* The compilation directory. NULL if not known. If we needed to
1958 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1959 points directly to the DW_AT_comp_dir string attribute owned by
1960 the obstack that owns the DIE. */
1961 const char *comp_dir;
1962
1963 /* If we needed to build a new string for comp_dir, this is what
1964 owns the storage. */
1965 std::string comp_dir_storage;
1966};
1967
1968static file_and_directory find_file_and_directory (struct die_info *die,
1969 struct dwarf2_cu *cu);
9291a0cd
TT
1970
1971static char *file_full_name (int file, struct line_header *lh,
1972 const char *comp_dir);
1973
43988095
JK
1974/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1975enum class rcuh_kind { COMPILE, TYPE };
1976
d521ce57 1977static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1978 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1979 struct comp_unit_head *header,
36586728 1980 struct dwarf2_section_info *section,
d521ce57 1981 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1982 rcuh_kind section_kind);
36586728 1983
fd820528 1984static void init_cutu_and_read_dies
f4dc4d17 1985 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1986 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1987 die_reader_func_ftype *die_reader_func, void *data);
1988
dee91e82
DE
1989static void init_cutu_and_read_dies_simple
1990 (struct dwarf2_per_cu_data *this_cu,
1991 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1992
673bfd45 1993static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1994
3019eac3
DE
1995static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1996
57d63ce2 1997static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1998 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1999 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2000 ULONGEST signature, int is_debug_types);
a2ce51a0 2001
ed2dc618
SM
2002static struct dwp_file *get_dwp_file
2003 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2004
3019eac3 2005static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2006 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2007
2008static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2009 (struct signatured_type *, const char *, const char *);
3019eac3 2010
89e63ee4
DE
2011static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2012
263db9a1
TT
2013/* A unique pointer to a dwo_file. */
2014
51ac9db5 2015typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2016
ed2dc618 2017static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2018
1b80a9fa 2019static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2020
2021static void free_line_header_voidp (void *arg);
4390d890
DE
2022\f
2023/* Various complaints about symbol reading that don't abort the process. */
2024
2025static void
2026dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2027{
b98664d3 2028 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2029}
2030
2031static void
2032dwarf2_debug_line_missing_file_complaint (void)
2033{
b98664d3 2034 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2035}
2036
2037static void
2038dwarf2_debug_line_missing_end_sequence_complaint (void)
2039{
b98664d3 2040 complaint (_(".debug_line section has line "
4390d890
DE
2041 "program sequence without an end"));
2042}
2043
2044static void
2045dwarf2_complex_location_expr_complaint (void)
2046{
b98664d3 2047 complaint (_("location expression too complex"));
4390d890
DE
2048}
2049
2050static void
2051dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2052 int arg3)
2053{
b98664d3 2054 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2055 arg1, arg2, arg3);
2056}
2057
2058static void
2059dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2060{
b98664d3 2061 complaint (_("debug info runs off end of %s section"
4390d890 2062 " [in module %s]"),
a32a8923
DE
2063 get_section_name (section),
2064 get_section_file_name (section));
4390d890 2065}
1b80a9fa 2066
4390d890
DE
2067static void
2068dwarf2_macro_malformed_definition_complaint (const char *arg1)
2069{
b98664d3 2070 complaint (_("macro debug info contains a "
4390d890
DE
2071 "malformed macro definition:\n`%s'"),
2072 arg1);
2073}
2074
2075static void
2076dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2077{
b98664d3 2078 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2079 arg1, arg2);
2080}
527f3840
JK
2081
2082/* Hash function for line_header_hash. */
2083
2084static hashval_t
2085line_header_hash (const struct line_header *ofs)
2086{
9c541725 2087 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2088}
2089
2090/* Hash function for htab_create_alloc_ex for line_header_hash. */
2091
2092static hashval_t
2093line_header_hash_voidp (const void *item)
2094{
9a3c8263 2095 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2096
2097 return line_header_hash (ofs);
2098}
2099
2100/* Equality function for line_header_hash. */
2101
2102static int
2103line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2104{
9a3c8263
SM
2105 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2106 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2107
9c541725 2108 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2109 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2110}
2111
4390d890 2112\f
9291a0cd 2113
31aa7e4e
JB
2114/* Read the given attribute value as an address, taking the attribute's
2115 form into account. */
2116
2117static CORE_ADDR
2118attr_value_as_address (struct attribute *attr)
2119{
2120 CORE_ADDR addr;
2121
336d760d
AT
2122 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2123 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2124 {
2125 /* Aside from a few clearly defined exceptions, attributes that
2126 contain an address must always be in DW_FORM_addr form.
2127 Unfortunately, some compilers happen to be violating this
2128 requirement by encoding addresses using other forms, such
2129 as DW_FORM_data4 for example. For those broken compilers,
2130 we try to do our best, without any guarantee of success,
2131 to interpret the address correctly. It would also be nice
2132 to generate a complaint, but that would require us to maintain
2133 a list of legitimate cases where a non-address form is allowed,
2134 as well as update callers to pass in at least the CU's DWARF
2135 version. This is more overhead than what we're willing to
2136 expand for a pretty rare case. */
2137 addr = DW_UNSND (attr);
2138 }
2139 else
2140 addr = DW_ADDR (attr);
2141
2142 return addr;
2143}
2144
330cdd98
PA
2145/* See declaration. */
2146
2147dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2148 const dwarf2_debug_sections *names,
2149 bool can_copy_)
2150 : objfile (objfile_),
2151 can_copy (can_copy_)
330cdd98
PA
2152{
2153 if (names == NULL)
2154 names = &dwarf2_elf_names;
2155
2156 bfd *obfd = objfile->obfd;
2157
2158 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2159 locate_sections (obfd, sec, *names);
2160}
2161
2162dwarf2_per_objfile::~dwarf2_per_objfile ()
2163{
2164 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2165 free_cached_comp_units ();
2166
2167 if (quick_file_names_table)
2168 htab_delete (quick_file_names_table);
2169
2170 if (line_header_hash)
2171 htab_delete (line_header_hash);
2172
b76e467d 2173 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2174 per_cu->imported_symtabs_free ();
fc8e7e75 2175
b2bdb8cf 2176 for (signatured_type *sig_type : all_type_units)
ae640021 2177 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2178
330cdd98
PA
2179 /* Everything else should be on the objfile obstack. */
2180}
2181
2182/* See declaration. */
2183
2184void
2185dwarf2_per_objfile::free_cached_comp_units ()
2186{
2187 dwarf2_per_cu_data *per_cu = read_in_chain;
2188 dwarf2_per_cu_data **last_chain = &read_in_chain;
2189 while (per_cu != NULL)
2190 {
2191 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2192
fcd3b13d 2193 delete per_cu->cu;
330cdd98
PA
2194 *last_chain = next_cu;
2195 per_cu = next_cu;
2196 }
2197}
2198
11ed8cad
TT
2199/* A helper class that calls free_cached_comp_units on
2200 destruction. */
2201
2202class free_cached_comp_units
2203{
2204public:
2205
2206 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2207 : m_per_objfile (per_objfile)
2208 {
2209 }
2210
2211 ~free_cached_comp_units ()
2212 {
2213 m_per_objfile->free_cached_comp_units ();
2214 }
2215
2216 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2217
2218private:
2219
2220 dwarf2_per_objfile *m_per_objfile;
2221};
2222
c906108c 2223/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2224 information and return true if we have enough to do something.
2225 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2226 ELF names are used. CAN_COPY is true for formats where symbol
2227 interposition is possible and so symbol values must follow copy
2228 relocation rules. */
c906108c
SS
2229
2230int
251d32d9 2231dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2232 const struct dwarf2_debug_sections *names,
2233 bool can_copy)
c906108c 2234{
97cbe998
SDJ
2235 if (objfile->flags & OBJF_READNEVER)
2236 return 0;
2237
ed2dc618
SM
2238 struct dwarf2_per_objfile *dwarf2_per_objfile
2239 = get_dwarf2_per_objfile (objfile);
2240
2241 if (dwarf2_per_objfile == NULL)
5bfd760d 2242 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2243 names,
2244 can_copy);
5bfd760d 2245
73869dc2 2246 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2247 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2248 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2249 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2250}
2251
2252/* Return the containing section of virtual section SECTION. */
2253
2254static struct dwarf2_section_info *
2255get_containing_section (const struct dwarf2_section_info *section)
2256{
2257 gdb_assert (section->is_virtual);
2258 return section->s.containing_section;
c906108c
SS
2259}
2260
a32a8923
DE
2261/* Return the bfd owner of SECTION. */
2262
2263static struct bfd *
2264get_section_bfd_owner (const struct dwarf2_section_info *section)
2265{
73869dc2
DE
2266 if (section->is_virtual)
2267 {
2268 section = get_containing_section (section);
2269 gdb_assert (!section->is_virtual);
2270 }
049412e3 2271 return section->s.section->owner;
a32a8923
DE
2272}
2273
2274/* Return the bfd section of SECTION.
2275 Returns NULL if the section is not present. */
2276
2277static asection *
2278get_section_bfd_section (const struct dwarf2_section_info *section)
2279{
73869dc2
DE
2280 if (section->is_virtual)
2281 {
2282 section = get_containing_section (section);
2283 gdb_assert (!section->is_virtual);
2284 }
049412e3 2285 return section->s.section;
a32a8923
DE
2286}
2287
2288/* Return the name of SECTION. */
2289
2290static const char *
2291get_section_name (const struct dwarf2_section_info *section)
2292{
2293 asection *sectp = get_section_bfd_section (section);
2294
2295 gdb_assert (sectp != NULL);
fd361982 2296 return bfd_section_name (sectp);
a32a8923
DE
2297}
2298
2299/* Return the name of the file SECTION is in. */
2300
2301static const char *
2302get_section_file_name (const struct dwarf2_section_info *section)
2303{
2304 bfd *abfd = get_section_bfd_owner (section);
2305
2306 return bfd_get_filename (abfd);
2307}
2308
2309/* Return the id of SECTION.
2310 Returns 0 if SECTION doesn't exist. */
2311
2312static int
2313get_section_id (const struct dwarf2_section_info *section)
2314{
2315 asection *sectp = get_section_bfd_section (section);
2316
2317 if (sectp == NULL)
2318 return 0;
2319 return sectp->id;
2320}
2321
2322/* Return the flags of SECTION.
73869dc2 2323 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2324
2325static int
2326get_section_flags (const struct dwarf2_section_info *section)
2327{
2328 asection *sectp = get_section_bfd_section (section);
2329
2330 gdb_assert (sectp != NULL);
fd361982 2331 return bfd_section_flags (sectp);
a32a8923
DE
2332}
2333
251d32d9
TG
2334/* When loading sections, we look either for uncompressed section or for
2335 compressed section names. */
233a11ab
CS
2336
2337static int
251d32d9
TG
2338section_is_p (const char *section_name,
2339 const struct dwarf2_section_names *names)
233a11ab 2340{
251d32d9
TG
2341 if (names->normal != NULL
2342 && strcmp (section_name, names->normal) == 0)
2343 return 1;
2344 if (names->compressed != NULL
2345 && strcmp (section_name, names->compressed) == 0)
2346 return 1;
2347 return 0;
233a11ab
CS
2348}
2349
330cdd98 2350/* See declaration. */
c906108c 2351
330cdd98
PA
2352void
2353dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2354 const dwarf2_debug_sections &names)
c906108c 2355{
fd361982 2356 flagword aflag = bfd_section_flags (sectp);
251d32d9 2357
dc7650b8
JK
2358 if ((aflag & SEC_HAS_CONTENTS) == 0)
2359 {
2360 }
950b7495
KS
2361 else if (elf_section_data (sectp)->this_hdr.sh_size
2362 > bfd_get_file_size (abfd))
2363 {
2364 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2365 warning (_("Discarding section %s which has a section size (%s"
2366 ") larger than the file size [in module %s]"),
2367 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2368 bfd_get_filename (abfd));
2369 }
330cdd98 2370 else if (section_is_p (sectp->name, &names.info))
c906108c 2371 {
330cdd98 2372 this->info.s.section = sectp;
fd361982 2373 this->info.size = bfd_section_size (sectp);
c906108c 2374 }
330cdd98 2375 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2376 {
330cdd98 2377 this->abbrev.s.section = sectp;
fd361982 2378 this->abbrev.size = bfd_section_size (sectp);
c906108c 2379 }
330cdd98 2380 else if (section_is_p (sectp->name, &names.line))
c906108c 2381 {
330cdd98 2382 this->line.s.section = sectp;
fd361982 2383 this->line.size = bfd_section_size (sectp);
c906108c 2384 }
330cdd98 2385 else if (section_is_p (sectp->name, &names.loc))
c906108c 2386 {
330cdd98 2387 this->loc.s.section = sectp;
fd361982 2388 this->loc.size = bfd_section_size (sectp);
c906108c 2389 }
330cdd98 2390 else if (section_is_p (sectp->name, &names.loclists))
43988095 2391 {
330cdd98 2392 this->loclists.s.section = sectp;
fd361982 2393 this->loclists.size = bfd_section_size (sectp);
43988095 2394 }
330cdd98 2395 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2396 {
330cdd98 2397 this->macinfo.s.section = sectp;
fd361982 2398 this->macinfo.size = bfd_section_size (sectp);
c906108c 2399 }
330cdd98 2400 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2401 {
330cdd98 2402 this->macro.s.section = sectp;
fd361982 2403 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2404 }
330cdd98 2405 else if (section_is_p (sectp->name, &names.str))
c906108c 2406 {
330cdd98 2407 this->str.s.section = sectp;
fd361982 2408 this->str.size = bfd_section_size (sectp);
c906108c 2409 }
330cdd98 2410 else if (section_is_p (sectp->name, &names.line_str))
43988095 2411 {
330cdd98 2412 this->line_str.s.section = sectp;
fd361982 2413 this->line_str.size = bfd_section_size (sectp);
43988095 2414 }
330cdd98 2415 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2416 {
330cdd98 2417 this->addr.s.section = sectp;
fd361982 2418 this->addr.size = bfd_section_size (sectp);
3019eac3 2419 }
330cdd98 2420 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2421 {
330cdd98 2422 this->frame.s.section = sectp;
fd361982 2423 this->frame.size = bfd_section_size (sectp);
b6af0555 2424 }
330cdd98 2425 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2426 {
330cdd98 2427 this->eh_frame.s.section = sectp;
fd361982 2428 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2429 }
330cdd98 2430 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2431 {
330cdd98 2432 this->ranges.s.section = sectp;
fd361982 2433 this->ranges.size = bfd_section_size (sectp);
af34e669 2434 }
330cdd98 2435 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2436 {
330cdd98 2437 this->rnglists.s.section = sectp;
fd361982 2438 this->rnglists.size = bfd_section_size (sectp);
43988095 2439 }
330cdd98 2440 else if (section_is_p (sectp->name, &names.types))
348e048f 2441 {
8b70b953
TT
2442 struct dwarf2_section_info type_section;
2443
2444 memset (&type_section, 0, sizeof (type_section));
049412e3 2445 type_section.s.section = sectp;
fd361982 2446 type_section.size = bfd_section_size (sectp);
8b70b953 2447
fd5866f6 2448 this->types.push_back (type_section);
348e048f 2449 }
330cdd98 2450 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2451 {
330cdd98 2452 this->gdb_index.s.section = sectp;
fd361982 2453 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2454 }
927aa2e7
JK
2455 else if (section_is_p (sectp->name, &names.debug_names))
2456 {
2457 this->debug_names.s.section = sectp;
fd361982 2458 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2459 }
2460 else if (section_is_p (sectp->name, &names.debug_aranges))
2461 {
2462 this->debug_aranges.s.section = sectp;
fd361982 2463 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2464 }
dce234bc 2465
fd361982
AM
2466 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2467 && bfd_section_vma (sectp) == 0)
330cdd98 2468 this->has_section_at_zero = true;
c906108c
SS
2469}
2470
fceca515
DE
2471/* A helper function that decides whether a section is empty,
2472 or not present. */
9e0ac564
TT
2473
2474static int
19ac8c2e 2475dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2476{
73869dc2
DE
2477 if (section->is_virtual)
2478 return section->size == 0;
049412e3 2479 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2480}
2481
cd4fb1b2 2482/* See dwarf2read.h. */
c906108c 2483
cd4fb1b2
SM
2484void
2485dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2486{
a32a8923 2487 asection *sectp;
3019eac3 2488 bfd *abfd;
dce234bc 2489 gdb_byte *buf, *retbuf;
c906108c 2490
be391dca
TT
2491 if (info->readin)
2492 return;
dce234bc 2493 info->buffer = NULL;
dc4ccb6f 2494 info->readin = true;
188dd5d6 2495
9e0ac564 2496 if (dwarf2_section_empty_p (info))
dce234bc 2497 return;
c906108c 2498
a32a8923 2499 sectp = get_section_bfd_section (info);
3019eac3 2500
73869dc2
DE
2501 /* If this is a virtual section we need to read in the real one first. */
2502 if (info->is_virtual)
2503 {
2504 struct dwarf2_section_info *containing_section =
2505 get_containing_section (info);
2506
2507 gdb_assert (sectp != NULL);
2508 if ((sectp->flags & SEC_RELOC) != 0)
2509 {
2510 error (_("Dwarf Error: DWP format V2 with relocations is not"
2511 " supported in section %s [in module %s]"),
2512 get_section_name (info), get_section_file_name (info));
2513 }
2514 dwarf2_read_section (objfile, containing_section);
2515 /* Other code should have already caught virtual sections that don't
2516 fit. */
2517 gdb_assert (info->virtual_offset + info->size
2518 <= containing_section->size);
2519 /* If the real section is empty or there was a problem reading the
2520 section we shouldn't get here. */
2521 gdb_assert (containing_section->buffer != NULL);
2522 info->buffer = containing_section->buffer + info->virtual_offset;
2523 return;
2524 }
2525
4bf44c1c
TT
2526 /* If the section has relocations, we must read it ourselves.
2527 Otherwise we attach it to the BFD. */
2528 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2529 {
d521ce57 2530 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2531 return;
dce234bc 2532 }
dce234bc 2533
224c3ddb 2534 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2535 info->buffer = buf;
dce234bc
PP
2536
2537 /* When debugging .o files, we may need to apply relocations; see
2538 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2539 We never compress sections in .o files, so we only need to
2540 try this when the section is not compressed. */
ac8035ab 2541 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2542 if (retbuf != NULL)
2543 {
2544 info->buffer = retbuf;
2545 return;
2546 }
2547
a32a8923
DE
2548 abfd = get_section_bfd_owner (info);
2549 gdb_assert (abfd != NULL);
2550
dce234bc
PP
2551 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2552 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2553 {
2554 error (_("Dwarf Error: Can't read DWARF data"
2555 " in section %s [in module %s]"),
fd361982 2556 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2557 }
dce234bc
PP
2558}
2559
9e0ac564
TT
2560/* A helper function that returns the size of a section in a safe way.
2561 If you are positive that the section has been read before using the
2562 size, then it is safe to refer to the dwarf2_section_info object's
2563 "size" field directly. In other cases, you must call this
2564 function, because for compressed sections the size field is not set
2565 correctly until the section has been read. */
2566
2567static bfd_size_type
2568dwarf2_section_size (struct objfile *objfile,
2569 struct dwarf2_section_info *info)
2570{
2571 if (!info->readin)
2572 dwarf2_read_section (objfile, info);
2573 return info->size;
2574}
2575
dce234bc 2576/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2577 SECTION_NAME. */
af34e669 2578
dce234bc 2579void
3017a003
TG
2580dwarf2_get_section_info (struct objfile *objfile,
2581 enum dwarf2_section_enum sect,
d521ce57 2582 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2583 bfd_size_type *sizep)
2584{
5bfd760d 2585 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2586 struct dwarf2_section_info *info;
a3b2a86b
TT
2587
2588 /* We may see an objfile without any DWARF, in which case we just
2589 return nothing. */
2590 if (data == NULL)
2591 {
2592 *sectp = NULL;
2593 *bufp = NULL;
2594 *sizep = 0;
2595 return;
2596 }
3017a003
TG
2597 switch (sect)
2598 {
2599 case DWARF2_DEBUG_FRAME:
2600 info = &data->frame;
2601 break;
2602 case DWARF2_EH_FRAME:
2603 info = &data->eh_frame;
2604 break;
2605 default:
2606 gdb_assert_not_reached ("unexpected section");
2607 }
dce234bc 2608
9e0ac564 2609 dwarf2_read_section (objfile, info);
dce234bc 2610
a32a8923 2611 *sectp = get_section_bfd_section (info);
dce234bc
PP
2612 *bufp = info->buffer;
2613 *sizep = info->size;
2614}
2615
36586728
TT
2616/* A helper function to find the sections for a .dwz file. */
2617
2618static void
2619locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2620{
9a3c8263 2621 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2622
2623 /* Note that we only support the standard ELF names, because .dwz
2624 is ELF-only (at the time of writing). */
2625 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2626 {
049412e3 2627 dwz_file->abbrev.s.section = sectp;
fd361982 2628 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2629 }
2630 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2631 {
049412e3 2632 dwz_file->info.s.section = sectp;
fd361982 2633 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2634 }
2635 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2636 {
049412e3 2637 dwz_file->str.s.section = sectp;
fd361982 2638 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2639 }
2640 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2641 {
049412e3 2642 dwz_file->line.s.section = sectp;
fd361982 2643 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2644 }
2645 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2646 {
049412e3 2647 dwz_file->macro.s.section = sectp;
fd361982 2648 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2649 }
2ec9a5e0
TT
2650 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2651 {
049412e3 2652 dwz_file->gdb_index.s.section = sectp;
fd361982 2653 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2654 }
927aa2e7
JK
2655 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2656 {
2657 dwz_file->debug_names.s.section = sectp;
fd361982 2658 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2659 }
36586728
TT
2660}
2661
c4973306 2662/* See dwarf2read.h. */
36586728 2663
c4973306 2664struct dwz_file *
ed2dc618 2665dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2666{
36586728 2667 const char *filename;
acd13123 2668 bfd_size_type buildid_len_arg;
dc294be5
TT
2669 size_t buildid_len;
2670 bfd_byte *buildid;
36586728
TT
2671
2672 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2673 return dwarf2_per_objfile->dwz_file.get ();
36586728 2674
4db1a1dc 2675 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2676 gdb::unique_xmalloc_ptr<char> data
2677 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2678 &buildid_len_arg, &buildid));
4db1a1dc
TT
2679 if (data == NULL)
2680 {
2681 if (bfd_get_error () == bfd_error_no_error)
2682 return NULL;
2683 error (_("could not read '.gnu_debugaltlink' section: %s"),
2684 bfd_errmsg (bfd_get_error ()));
2685 }
791afaa2
TT
2686
2687 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2688
acd13123
TT
2689 buildid_len = (size_t) buildid_len_arg;
2690
791afaa2 2691 filename = data.get ();
d721ba37
PA
2692
2693 std::string abs_storage;
36586728
TT
2694 if (!IS_ABSOLUTE_PATH (filename))
2695 {
14278e1f
TT
2696 gdb::unique_xmalloc_ptr<char> abs
2697 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2698
14278e1f 2699 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2700 filename = abs_storage.c_str ();
36586728
TT
2701 }
2702
dc294be5
TT
2703 /* First try the file name given in the section. If that doesn't
2704 work, try to use the build-id instead. */
192b62ce 2705 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2706 if (dwz_bfd != NULL)
36586728 2707 {
192b62ce 2708 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2709 dwz_bfd.reset (nullptr);
36586728
TT
2710 }
2711
dc294be5
TT
2712 if (dwz_bfd == NULL)
2713 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2714
2715 if (dwz_bfd == NULL)
2716 error (_("could not find '.gnu_debugaltlink' file for %s"),
2717 objfile_name (dwarf2_per_objfile->objfile));
2718
7ff8cb8c
TT
2719 std::unique_ptr<struct dwz_file> result
2720 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2721
7ff8cb8c
TT
2722 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2723 result.get ());
36586728 2724
7ff8cb8c
TT
2725 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2726 result->dwz_bfd.get ());
2727 dwarf2_per_objfile->dwz_file = std::move (result);
2728 return dwarf2_per_objfile->dwz_file.get ();
36586728 2729}
9291a0cd 2730\f
7b9f3c50
DE
2731/* DWARF quick_symbols_functions support. */
2732
2733/* TUs can share .debug_line entries, and there can be a lot more TUs than
2734 unique line tables, so we maintain a separate table of all .debug_line
2735 derived entries to support the sharing.
2736 All the quick functions need is the list of file names. We discard the
2737 line_header when we're done and don't need to record it here. */
2738struct quick_file_names
2739{
094b34ac
DE
2740 /* The data used to construct the hash key. */
2741 struct stmt_list_hash hash;
7b9f3c50
DE
2742
2743 /* The number of entries in file_names, real_names. */
2744 unsigned int num_file_names;
2745
2746 /* The file names from the line table, after being run through
2747 file_full_name. */
2748 const char **file_names;
2749
2750 /* The file names from the line table after being run through
2751 gdb_realpath. These are computed lazily. */
2752 const char **real_names;
2753};
2754
2755/* When using the index (and thus not using psymtabs), each CU has an
2756 object of this type. This is used to hold information needed by
2757 the various "quick" methods. */
2758struct dwarf2_per_cu_quick_data
2759{
2760 /* The file table. This can be NULL if there was no file table
2761 or it's currently not read in.
2762 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2763 struct quick_file_names *file_names;
2764
2765 /* The corresponding symbol table. This is NULL if symbols for this
2766 CU have not yet been read. */
43f3e411 2767 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2768
2769 /* A temporary mark bit used when iterating over all CUs in
2770 expand_symtabs_matching. */
2771 unsigned int mark : 1;
2772
2773 /* True if we've tried to read the file table and found there isn't one.
2774 There will be no point in trying to read it again next time. */
2775 unsigned int no_file_data : 1;
2776};
2777
094b34ac
DE
2778/* Utility hash function for a stmt_list_hash. */
2779
2780static hashval_t
2781hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2782{
2783 hashval_t v = 0;
2784
2785 if (stmt_list_hash->dwo_unit != NULL)
2786 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2787 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2788 return v;
2789}
2790
2791/* Utility equality function for a stmt_list_hash. */
2792
2793static int
2794eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2795 const struct stmt_list_hash *rhs)
2796{
2797 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2798 return 0;
2799 if (lhs->dwo_unit != NULL
2800 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2801 return 0;
2802
9c541725 2803 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2804}
2805
7b9f3c50
DE
2806/* Hash function for a quick_file_names. */
2807
2808static hashval_t
2809hash_file_name_entry (const void *e)
2810{
9a3c8263
SM
2811 const struct quick_file_names *file_data
2812 = (const struct quick_file_names *) e;
7b9f3c50 2813
094b34ac 2814 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2815}
2816
2817/* Equality function for a quick_file_names. */
2818
2819static int
2820eq_file_name_entry (const void *a, const void *b)
2821{
9a3c8263
SM
2822 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2823 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2824
094b34ac 2825 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2826}
2827
2828/* Delete function for a quick_file_names. */
2829
2830static void
2831delete_file_name_entry (void *e)
2832{
9a3c8263 2833 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2834 int i;
2835
2836 for (i = 0; i < file_data->num_file_names; ++i)
2837 {
2838 xfree ((void*) file_data->file_names[i]);
2839 if (file_data->real_names)
2840 xfree ((void*) file_data->real_names[i]);
2841 }
2842
2843 /* The space for the struct itself lives on objfile_obstack,
2844 so we don't free it here. */
2845}
2846
2847/* Create a quick_file_names hash table. */
2848
2849static htab_t
2850create_quick_file_names_table (unsigned int nr_initial_entries)
2851{
2852 return htab_create_alloc (nr_initial_entries,
2853 hash_file_name_entry, eq_file_name_entry,
2854 delete_file_name_entry, xcalloc, xfree);
2855}
9291a0cd 2856
918dd910
JK
2857/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2858 have to be created afterwards. You should call age_cached_comp_units after
2859 processing PER_CU->CU. dw2_setup must have been already called. */
2860
2861static void
58f0c718 2862load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2863{
3019eac3 2864 if (per_cu->is_debug_types)
e5fe5e75 2865 load_full_type_unit (per_cu);
918dd910 2866 else
58f0c718 2867 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2868
cc12ce38
DE
2869 if (per_cu->cu == NULL)
2870 return; /* Dummy CU. */
2dc860c0
DE
2871
2872 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2873}
2874
a0f42c21 2875/* Read in the symbols for PER_CU. */
2fdf6df6 2876
9291a0cd 2877static void
58f0c718 2878dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2879{
ed2dc618 2880 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2881
f4dc4d17
DE
2882 /* Skip type_unit_groups, reading the type units they contain
2883 is handled elsewhere. */
2884 if (IS_TYPE_UNIT_GROUP (per_cu))
2885 return;
2886
b303c6f6
AB
2887 /* The destructor of dwarf2_queue_guard frees any entries left on
2888 the queue. After this point we're guaranteed to leave this function
2889 with the dwarf queue empty. */
2890 dwarf2_queue_guard q_guard;
9291a0cd 2891
95554aad 2892 if (dwarf2_per_objfile->using_index
43f3e411 2893 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2894 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2895 {
2896 queue_comp_unit (per_cu, language_minimal);
58f0c718 2897 load_cu (per_cu, skip_partial);
89e63ee4
DE
2898
2899 /* If we just loaded a CU from a DWO, and we're working with an index
2900 that may badly handle TUs, load all the TUs in that DWO as well.
2901 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2902 if (!per_cu->is_debug_types
cc12ce38 2903 && per_cu->cu != NULL
89e63ee4
DE
2904 && per_cu->cu->dwo_unit != NULL
2905 && dwarf2_per_objfile->index_table != NULL
2906 && dwarf2_per_objfile->index_table->version <= 7
2907 /* DWP files aren't supported yet. */
ed2dc618 2908 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2909 queue_and_load_all_dwo_tus (per_cu);
95554aad 2910 }
9291a0cd 2911
ed2dc618 2912 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2913
2914 /* Age the cache, releasing compilation units that have not
2915 been used recently. */
ed2dc618 2916 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2917}
2918
2919/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2920 the objfile from which this CU came. Returns the resulting symbol
2921 table. */
2fdf6df6 2922
43f3e411 2923static struct compunit_symtab *
58f0c718 2924dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2925{
ed2dc618
SM
2926 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2927
95554aad 2928 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2929 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2930 {
11ed8cad 2931 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2932 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2933 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2934 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2935 }
f194fefb 2936
43f3e411 2937 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2938}
2939
ff4c9fec 2940/* See declaration. */
f4dc4d17 2941
ff4c9fec
SM
2942dwarf2_per_cu_data *
2943dwarf2_per_objfile::get_cutu (int index)
2944{
b76e467d 2945 if (index >= this->all_comp_units.size ())
ff4c9fec 2946 {
b76e467d 2947 index -= this->all_comp_units.size ();
b2bdb8cf 2948 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2949 return &this->all_type_units[index]->per_cu;
2950 }
f4dc4d17 2951
ff4c9fec
SM
2952 return this->all_comp_units[index];
2953}
f4dc4d17 2954
ff4c9fec 2955/* See declaration. */
2fdf6df6 2956
ff4c9fec
SM
2957dwarf2_per_cu_data *
2958dwarf2_per_objfile::get_cu (int index)
1fd400ff 2959{
b76e467d 2960 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2961
ff4c9fec 2962 return this->all_comp_units[index];
f4dc4d17
DE
2963}
2964
ff4c9fec 2965/* See declaration. */
f4dc4d17 2966
ff4c9fec
SM
2967signatured_type *
2968dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2969{
b2bdb8cf 2970 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2971
ff4c9fec 2972 return this->all_type_units[index];
1fd400ff
TT
2973}
2974
4b514bc8
JK
2975/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2976 objfile_obstack, and constructed with the specified field
2977 values. */
2978
2979static dwarf2_per_cu_data *
ed2dc618 2980create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2981 struct dwarf2_section_info *section,
2982 int is_dwz,
2983 sect_offset sect_off, ULONGEST length)
2984{
ed2dc618 2985 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2986 dwarf2_per_cu_data *the_cu
2987 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2988 struct dwarf2_per_cu_data);
2989 the_cu->sect_off = sect_off;
2990 the_cu->length = length;
e3b94546 2991 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2992 the_cu->section = section;
2993 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2994 struct dwarf2_per_cu_quick_data);
2995 the_cu->is_dwz = is_dwz;
2996 return the_cu;
2997}
2998
2ec9a5e0
TT
2999/* A helper for create_cus_from_index that handles a given list of
3000 CUs. */
2fdf6df6 3001
74a0d9f6 3002static void
12359b5e 3003create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3004 const gdb_byte *cu_list, offset_type n_elements,
3005 struct dwarf2_section_info *section,
b76e467d 3006 int is_dwz)
9291a0cd 3007{
12359b5e 3008 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3009 {
74a0d9f6 3010 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3011
3012 sect_offset sect_off
3013 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3014 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3015 cu_list += 2 * 8;
3016
b76e467d 3017 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3018 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3019 sect_off, length);
b76e467d 3020 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3021 }
9291a0cd
TT
3022}
3023
2ec9a5e0 3024/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3025 the CU objects for this objfile. */
2ec9a5e0 3026
74a0d9f6 3027static void
12359b5e 3028create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3029 const gdb_byte *cu_list, offset_type cu_list_elements,
3030 const gdb_byte *dwz_list, offset_type dwz_elements)
3031{
b76e467d
SM
3032 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3033 dwarf2_per_objfile->all_comp_units.reserve
3034 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3035
12359b5e 3036 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3037 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3038
3039 if (dwz_elements == 0)
74a0d9f6 3040 return;
2ec9a5e0 3041
12359b5e
SM
3042 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3043 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3044 &dwz->info, 1);
2ec9a5e0
TT
3045}
3046
1fd400ff 3047/* Create the signatured type hash table from the index. */
673bfd45 3048
74a0d9f6 3049static void
12359b5e
SM
3050create_signatured_type_table_from_index
3051 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3052 struct dwarf2_section_info *section,
3053 const gdb_byte *bytes,
3054 offset_type elements)
1fd400ff 3055{
12359b5e 3056 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3057
b2bdb8cf
SM
3058 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3059 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3060
12359b5e 3061 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3062
12359b5e 3063 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3064 {
52dc124a 3065 struct signatured_type *sig_type;
9c541725 3066 ULONGEST signature;
1fd400ff 3067 void **slot;
9c541725 3068 cu_offset type_offset_in_tu;
1fd400ff 3069
74a0d9f6 3070 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3071 sect_offset sect_off
3072 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3073 type_offset_in_tu
3074 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3075 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3076 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3077 bytes += 3 * 8;
3078
52dc124a 3079 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3080 struct signatured_type);
52dc124a 3081 sig_type->signature = signature;
9c541725 3082 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3083 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3084 sig_type->per_cu.section = section;
9c541725 3085 sig_type->per_cu.sect_off = sect_off;
e3b94546 3086 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3087 sig_type->per_cu.v.quick
1fd400ff
TT
3088 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3089 struct dwarf2_per_cu_quick_data);
3090
52dc124a
DE
3091 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3092 *slot = sig_type;
1fd400ff 3093
b2bdb8cf 3094 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3095 }
3096
673bfd45 3097 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3098}
3099
927aa2e7
JK
3100/* Create the signatured type hash table from .debug_names. */
3101
3102static void
3103create_signatured_type_table_from_debug_names
ed2dc618 3104 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3105 const mapped_debug_names &map,
3106 struct dwarf2_section_info *section,
3107 struct dwarf2_section_info *abbrev_section)
3108{
ed2dc618
SM
3109 struct objfile *objfile = dwarf2_per_objfile->objfile;
3110
927aa2e7
JK
3111 dwarf2_read_section (objfile, section);
3112 dwarf2_read_section (objfile, abbrev_section);
3113
b2bdb8cf
SM
3114 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3115 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3116
3117 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3118
3119 for (uint32_t i = 0; i < map.tu_count; ++i)
3120 {
3121 struct signatured_type *sig_type;
927aa2e7 3122 void **slot;
927aa2e7
JK
3123
3124 sect_offset sect_off
3125 = (sect_offset) (extract_unsigned_integer
3126 (map.tu_table_reordered + i * map.offset_size,
3127 map.offset_size,
3128 map.dwarf5_byte_order));
3129
3130 comp_unit_head cu_header;
ed2dc618
SM
3131 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3132 abbrev_section,
927aa2e7
JK
3133 section->buffer + to_underlying (sect_off),
3134 rcuh_kind::TYPE);
3135
3136 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3137 struct signatured_type);
3138 sig_type->signature = cu_header.signature;
3139 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3140 sig_type->per_cu.is_debug_types = 1;
3141 sig_type->per_cu.section = section;
3142 sig_type->per_cu.sect_off = sect_off;
e3b94546 3143 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3144 sig_type->per_cu.v.quick
3145 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3146 struct dwarf2_per_cu_quick_data);
3147
3148 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3149 *slot = sig_type;
3150
b2bdb8cf 3151 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3152 }
3153
3154 dwarf2_per_objfile->signatured_types = sig_types_hash;
3155}
3156
9291a0cd
TT
3157/* Read the address map data from the mapped index, and use it to
3158 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3159
9291a0cd 3160static void
ed2dc618
SM
3161create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3162 struct mapped_index *index)
9291a0cd 3163{
ed2dc618 3164 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3165 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3166 const gdb_byte *iter, *end;
9291a0cd 3167 struct addrmap *mutable_map;
9291a0cd
TT
3168 CORE_ADDR baseaddr;
3169
8268c778
PA
3170 auto_obstack temp_obstack;
3171
9291a0cd
TT
3172 mutable_map = addrmap_create_mutable (&temp_obstack);
3173
f00a2de2
PA
3174 iter = index->address_table.data ();
3175 end = iter + index->address_table.size ();
9291a0cd
TT
3176
3177 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3178
3179 while (iter < end)
3180 {
3181 ULONGEST hi, lo, cu_index;
3182 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3183 iter += 8;
3184 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3185 iter += 8;
3186 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3187 iter += 4;
f652bce2 3188
24a55014 3189 if (lo > hi)
f652bce2 3190 {
b98664d3 3191 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3192 hex_string (lo), hex_string (hi));
24a55014 3193 continue;
f652bce2 3194 }
24a55014 3195
b76e467d 3196 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3197 {
b98664d3 3198 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3199 (unsigned) cu_index);
24a55014 3200 continue;
f652bce2 3201 }
24a55014 3202
79748972
TT
3203 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3204 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3205 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3206 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3207 }
3208
d320c2b5 3209 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3210 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3211}
3212
927aa2e7
JK
3213/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3214 populate the objfile's psymtabs_addrmap. */
3215
3216static void
ed2dc618 3217create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3218 struct dwarf2_section_info *section)
3219{
ed2dc618 3220 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3221 bfd *abfd = objfile->obfd;
3222 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3223 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3224 SECT_OFF_TEXT (objfile));
3225
3226 auto_obstack temp_obstack;
3227 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3228
3229 std::unordered_map<sect_offset,
3230 dwarf2_per_cu_data *,
3231 gdb::hash_enum<sect_offset>>
3232 debug_info_offset_to_per_cu;
b76e467d 3233 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3234 {
927aa2e7
JK
3235 const auto insertpair
3236 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3237 if (!insertpair.second)
3238 {
3239 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3240 "debug_info_offset %s, ignoring .debug_aranges."),
3241 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3242 return;
3243 }
3244 }
3245
3246 dwarf2_read_section (objfile, section);
3247
3248 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3249
3250 const gdb_byte *addr = section->buffer;
3251
3252 while (addr < section->buffer + section->size)
3253 {
3254 const gdb_byte *const entry_addr = addr;
3255 unsigned int bytes_read;
3256
3257 const LONGEST entry_length = read_initial_length (abfd, addr,
3258 &bytes_read);
3259 addr += bytes_read;
3260
3261 const gdb_byte *const entry_end = addr + entry_length;
3262 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3263 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3264 if (addr + entry_length > section->buffer + section->size)
3265 {
47e3f474 3266 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3267 "length %s exceeds section length %s, "
3268 "ignoring .debug_aranges."),
47e3f474
TV
3269 objfile_name (objfile),
3270 plongest (entry_addr - section->buffer),
927aa2e7
JK
3271 plongest (bytes_read + entry_length),
3272 pulongest (section->size));
3273 return;
3274 }
3275
3276 /* The version number. */
3277 const uint16_t version = read_2_bytes (abfd, addr);
3278 addr += 2;
3279 if (version != 2)
3280 {
47e3f474 3281 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3282 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3283 objfile_name (objfile),
3284 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3285 return;
3286 }
3287
3288 const uint64_t debug_info_offset
3289 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3290 addr += offset_size;
3291 const auto per_cu_it
3292 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3293 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3294 {
47e3f474 3295 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3296 "debug_info_offset %s does not exists, "
3297 "ignoring .debug_aranges."),
47e3f474
TV
3298 objfile_name (objfile),
3299 plongest (entry_addr - section->buffer),
927aa2e7
JK
3300 pulongest (debug_info_offset));
3301 return;
3302 }
3303 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3304
3305 const uint8_t address_size = *addr++;
3306 if (address_size < 1 || address_size > 8)
3307 {
47e3f474 3308 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3309 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3310 objfile_name (objfile),
3311 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3312 return;
3313 }
3314
3315 const uint8_t segment_selector_size = *addr++;
3316 if (segment_selector_size != 0)
3317 {
47e3f474 3318 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3319 "segment_selector_size %u is not supported, "
3320 "ignoring .debug_aranges."),
47e3f474
TV
3321 objfile_name (objfile),
3322 plongest (entry_addr - section->buffer),
927aa2e7
JK
3323 segment_selector_size);
3324 return;
3325 }
3326
3327 /* Must pad to an alignment boundary that is twice the address
3328 size. It is undocumented by the DWARF standard but GCC does
3329 use it. */
3330 for (size_t padding = ((-(addr - section->buffer))
3331 & (2 * address_size - 1));
3332 padding > 0; padding--)
3333 if (*addr++ != 0)
3334 {
47e3f474 3335 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3336 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3337 objfile_name (objfile),
3338 plongest (entry_addr - section->buffer));
927aa2e7
JK
3339 return;
3340 }
3341
3342 for (;;)
3343 {
3344 if (addr + 2 * address_size > entry_end)
3345 {
47e3f474 3346 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3347 "address list is not properly terminated, "
3348 "ignoring .debug_aranges."),
47e3f474
TV
3349 objfile_name (objfile),
3350 plongest (entry_addr - section->buffer));
927aa2e7
JK
3351 return;
3352 }
3353 ULONGEST start = extract_unsigned_integer (addr, address_size,
3354 dwarf5_byte_order);
3355 addr += address_size;
3356 ULONGEST length = extract_unsigned_integer (addr, address_size,
3357 dwarf5_byte_order);
3358 addr += address_size;
3359 if (start == 0 && length == 0)
3360 break;
3361 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3362 {
3363 /* Symbol was eliminated due to a COMDAT group. */
3364 continue;
3365 }
3366 ULONGEST end = start + length;
79748972
TT
3367 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3368 - baseaddr);
3369 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3370 - baseaddr);
927aa2e7
JK
3371 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3372 }
3373 }
3374
d320c2b5 3375 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3376 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3377}
3378
9291a0cd
TT
3379/* Find a slot in the mapped index INDEX for the object named NAME.
3380 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3381 constant pool and return true. If NAME cannot be found, return
3382 false. */
2fdf6df6 3383
109483d9 3384static bool
9291a0cd
TT
3385find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3386 offset_type **vec_out)
3387{
0cf03b49 3388 offset_type hash;
9291a0cd 3389 offset_type slot, step;
559a7a62 3390 int (*cmp) (const char *, const char *);
9291a0cd 3391
791afaa2 3392 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3393 if (current_language->la_language == language_cplus
45280282
IB
3394 || current_language->la_language == language_fortran
3395 || current_language->la_language == language_d)
0cf03b49
JK
3396 {
3397 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3398 not contain any. */
a8719064 3399
72998fb3 3400 if (strchr (name, '(') != NULL)
0cf03b49 3401 {
109483d9 3402 without_params = cp_remove_params (name);
0cf03b49 3403
72998fb3 3404 if (without_params != NULL)
791afaa2 3405 name = without_params.get ();
0cf03b49
JK
3406 }
3407 }
3408
559a7a62 3409 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3410 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3411 simulate our NAME being searched is also lowercased. */
3412 hash = mapped_index_string_hash ((index->version == 4
3413 && case_sensitivity == case_sensitive_off
3414 ? 5 : index->version),
3415 name);
3416
f00a2de2
PA
3417 slot = hash & (index->symbol_table.size () - 1);
3418 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3419 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3420
3421 for (;;)
3422 {
9291a0cd 3423 const char *str;
f00a2de2
PA
3424
3425 const auto &bucket = index->symbol_table[slot];
3426 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3427 return false;
9291a0cd 3428
f00a2de2 3429 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3430 if (!cmp (name, str))
9291a0cd
TT
3431 {
3432 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3433 + MAYBE_SWAP (bucket.vec));
109483d9 3434 return true;
9291a0cd
TT
3435 }
3436
f00a2de2 3437 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3438 }
3439}
3440
4485a1c1
SM
3441/* A helper function that reads the .gdb_index from BUFFER and fills
3442 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3443 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3444 ok to use deprecated sections.
3445
3446 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3447 out parameters that are filled in with information about the CU and
3448 TU lists in the section.
3449
4485a1c1 3450 Returns true if all went well, false otherwise. */
2fdf6df6 3451
d33bc52e 3452static bool
4485a1c1
SM
3453read_gdb_index_from_buffer (struct objfile *objfile,
3454 const char *filename,
3455 bool deprecated_ok,
3456 gdb::array_view<const gdb_byte> buffer,
3457 struct mapped_index *map,
3458 const gdb_byte **cu_list,
3459 offset_type *cu_list_elements,
3460 const gdb_byte **types_list,
3461 offset_type *types_list_elements)
3462{
3463 const gdb_byte *addr = &buffer[0];
82430852 3464
9291a0cd 3465 /* Version check. */
4485a1c1 3466 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3467 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3468 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3469 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3470 indices. */
831adc1f 3471 if (version < 4)
481860b3
GB
3472 {
3473 static int warning_printed = 0;
3474 if (!warning_printed)
3475 {
3476 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3477 filename);
481860b3
GB
3478 warning_printed = 1;
3479 }
3480 return 0;
3481 }
3482 /* Index version 4 uses a different hash function than index version
3483 5 and later.
3484
3485 Versions earlier than 6 did not emit psymbols for inlined
3486 functions. Using these files will cause GDB not to be able to
3487 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3488 indices unless the user has done
3489 "set use-deprecated-index-sections on". */
2ec9a5e0 3490 if (version < 6 && !deprecated_ok)
481860b3
GB
3491 {
3492 static int warning_printed = 0;
3493 if (!warning_printed)
3494 {
e615022a
DE
3495 warning (_("\
3496Skipping deprecated .gdb_index section in %s.\n\
3497Do \"set use-deprecated-index-sections on\" before the file is read\n\
3498to use the section anyway."),
2ec9a5e0 3499 filename);
481860b3
GB
3500 warning_printed = 1;
3501 }
3502 return 0;
3503 }
796a7ff8 3504 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3505 of the TU (for symbols coming from TUs),
3506 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3507 Plus gold-generated indices can have duplicate entries for global symbols,
3508 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3509 These are just performance bugs, and we can't distinguish gdb-generated
3510 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3511
481860b3 3512 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3513 longer backward compatible. */
796a7ff8 3514 if (version > 8)
594e8718 3515 return 0;
9291a0cd 3516
559a7a62 3517 map->version = version;
9291a0cd 3518
4485a1c1 3519 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3520
4485a1c1 3521 int i = 0;
2ec9a5e0
TT
3522 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3523 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3524 / 8);
1fd400ff
TT
3525 ++i;
3526
2ec9a5e0
TT
3527 *types_list = addr + MAYBE_SWAP (metadata[i]);
3528 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3529 - MAYBE_SWAP (metadata[i]))
3530 / 8);
987d643c 3531 ++i;
1fd400ff 3532
f00a2de2
PA
3533 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3534 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3535 map->address_table
3536 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3537 ++i;
3538
f00a2de2
PA
3539 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3540 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3541 map->symbol_table
3542 = gdb::array_view<mapped_index::symbol_table_slot>
3543 ((mapped_index::symbol_table_slot *) symbol_table,
3544 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3545
f00a2de2 3546 ++i;
f9d83a0b 3547 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3548
2ec9a5e0
TT
3549 return 1;
3550}
3551
4485a1c1
SM
3552/* Callback types for dwarf2_read_gdb_index. */
3553
3554typedef gdb::function_view
3555 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3556 get_gdb_index_contents_ftype;
3557typedef gdb::function_view
3558 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3559 get_gdb_index_contents_dwz_ftype;
3560
927aa2e7 3561/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3562 elements of all the CUs and return 1. Otherwise, return 0. */
3563
3564static int
4485a1c1
SM
3565dwarf2_read_gdb_index
3566 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3567 get_gdb_index_contents_ftype get_gdb_index_contents,
3568 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3569{
2ec9a5e0
TT
3570 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3571 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3572 struct dwz_file *dwz;
12359b5e 3573 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3574
4485a1c1
SM
3575 gdb::array_view<const gdb_byte> main_index_contents
3576 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3577
3578 if (main_index_contents.empty ())
3579 return 0;
3580
3063847f 3581 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3582 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3583 use_deprecated_index_sections,
3584 main_index_contents, map.get (), &cu_list,
3585 &cu_list_elements, &types_list,
3586 &types_list_elements))
2ec9a5e0
TT
3587 return 0;
3588
0fefef59 3589 /* Don't use the index if it's empty. */
3063847f 3590 if (map->symbol_table.empty ())
0fefef59
DE
3591 return 0;
3592
2ec9a5e0
TT
3593 /* If there is a .dwz file, read it so we can get its CU list as
3594 well. */
ed2dc618 3595 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3596 if (dwz != NULL)
2ec9a5e0 3597 {
2ec9a5e0
TT
3598 struct mapped_index dwz_map;
3599 const gdb_byte *dwz_types_ignore;
3600 offset_type dwz_types_elements_ignore;
3601
4485a1c1
SM
3602 gdb::array_view<const gdb_byte> dwz_index_content
3603 = get_gdb_index_contents_dwz (objfile, dwz);
3604
3605 if (dwz_index_content.empty ())
3606 return 0;
3607
3608 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3609 bfd_get_filename (dwz->dwz_bfd.get ()),
3610 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3611 &dwz_list, &dwz_list_elements,
3612 &dwz_types_ignore,
3613 &dwz_types_elements_ignore))
2ec9a5e0
TT
3614 {
3615 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3616 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3617 return 0;
3618 }
3619 }
3620
12359b5e
SM
3621 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3622 dwz_list, dwz_list_elements);
1fd400ff 3623
8b70b953
TT
3624 if (types_list_elements)
3625 {
8b70b953
TT
3626 /* We can only handle a single .debug_types when we have an
3627 index. */
fd5866f6 3628 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3629 return 0;
3630
fd5866f6 3631 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3632
12359b5e
SM
3633 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3634 types_list, types_list_elements);
8b70b953 3635 }
9291a0cd 3636
3063847f 3637 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3638
3063847f 3639 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3640 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3641 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3642 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3643
3644 return 1;
3645}
3646
dee91e82 3647/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3648
dee91e82
DE
3649static void
3650dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3651 const gdb_byte *info_ptr,
dee91e82
DE
3652 struct die_info *comp_unit_die,
3653 int has_children,
3654 void *data)
9291a0cd 3655{
dee91e82 3656 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3657 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3658 struct dwarf2_per_objfile *dwarf2_per_objfile
3659 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3660 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3661 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3662 struct attribute *attr;
7b9f3c50
DE
3663 void **slot;
3664 struct quick_file_names *qfn;
9291a0cd 3665
0186c6a7
DE
3666 gdb_assert (! this_cu->is_debug_types);
3667
07261596
TT
3668 /* Our callers never want to match partial units -- instead they
3669 will match the enclosing full CU. */
3670 if (comp_unit_die->tag == DW_TAG_partial_unit)
3671 {
3672 this_cu->v.quick->no_file_data = 1;
3673 return;
3674 }
3675
0186c6a7 3676 lh_cu = this_cu;
7b9f3c50 3677 slot = NULL;
dee91e82 3678
fff8551c 3679 line_header_up lh;
9c541725 3680 sect_offset line_offset {};
fff8551c 3681
dee91e82 3682 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3683 if (attr != nullptr)
9291a0cd 3684 {
7b9f3c50
DE
3685 struct quick_file_names find_entry;
3686
9c541725 3687 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3688
3689 /* We may have already read in this line header (TU line header sharing).
3690 If we have we're done. */
094b34ac 3691 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3692 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3693 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3694 &find_entry, INSERT);
3695 if (*slot != NULL)
3696 {
9a3c8263 3697 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3698 return;
7b9f3c50
DE
3699 }
3700
3019eac3 3701 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3702 }
3703 if (lh == NULL)
3704 {
094b34ac 3705 lh_cu->v.quick->no_file_data = 1;
dee91e82 3706 return;
9291a0cd
TT
3707 }
3708
8d749320 3709 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3710 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3711 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3712 gdb_assert (slot != NULL);
3713 *slot = qfn;
9291a0cd 3714
d721ba37 3715 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3716
aa391654
TT
3717 int offset = 0;
3718 if (strcmp (fnd.name, "<unknown>") != 0)
3719 ++offset;
3720
7ba99d21 3721 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3722 qfn->file_names =
aa391654
TT
3723 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3724 if (offset != 0)
3725 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3726 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3727 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3728 qfn->real_names = NULL;
9291a0cd 3729
094b34ac 3730 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3731}
3732
3733/* A helper for the "quick" functions which attempts to read the line
3734 table for THIS_CU. */
3735
3736static struct quick_file_names *
e4a48d9d 3737dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3738{
0186c6a7
DE
3739 /* This should never be called for TUs. */
3740 gdb_assert (! this_cu->is_debug_types);
3741 /* Nor type unit groups. */
3742 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3743
dee91e82
DE
3744 if (this_cu->v.quick->file_names != NULL)
3745 return this_cu->v.quick->file_names;
3746 /* If we know there is no line data, no point in looking again. */
3747 if (this_cu->v.quick->no_file_data)
3748 return NULL;
3749
0186c6a7 3750 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3751
3752 if (this_cu->v.quick->no_file_data)
3753 return NULL;
3754 return this_cu->v.quick->file_names;
9291a0cd
TT
3755}
3756
3757/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3758 real path for a given file name from the line table. */
2fdf6df6 3759
9291a0cd 3760static const char *
7b9f3c50
DE
3761dw2_get_real_path (struct objfile *objfile,
3762 struct quick_file_names *qfn, int index)
9291a0cd 3763{
7b9f3c50
DE
3764 if (qfn->real_names == NULL)
3765 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3766 qfn->num_file_names, const char *);
9291a0cd 3767
7b9f3c50 3768 if (qfn->real_names[index] == NULL)
14278e1f 3769 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3770
7b9f3c50 3771 return qfn->real_names[index];
9291a0cd
TT
3772}
3773
3774static struct symtab *
3775dw2_find_last_source_symtab (struct objfile *objfile)
3776{
ed2dc618
SM
3777 struct dwarf2_per_objfile *dwarf2_per_objfile
3778 = get_dwarf2_per_objfile (objfile);
b76e467d 3779 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3780 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3781
43f3e411
DE
3782 if (cust == NULL)
3783 return NULL;
ed2dc618 3784
43f3e411 3785 return compunit_primary_filetab (cust);
9291a0cd
TT
3786}
3787
7b9f3c50
DE
3788/* Traversal function for dw2_forget_cached_source_info. */
3789
3790static int
3791dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3792{
7b9f3c50 3793 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3794
7b9f3c50 3795 if (file_data->real_names)
9291a0cd 3796 {
7b9f3c50 3797 int i;
9291a0cd 3798
7b9f3c50 3799 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3800 {
7b9f3c50
DE
3801 xfree ((void*) file_data->real_names[i]);
3802 file_data->real_names[i] = NULL;
9291a0cd
TT
3803 }
3804 }
7b9f3c50
DE
3805
3806 return 1;
3807}
3808
3809static void
3810dw2_forget_cached_source_info (struct objfile *objfile)
3811{
ed2dc618
SM
3812 struct dwarf2_per_objfile *dwarf2_per_objfile
3813 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3814
3815 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3816 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3817}
3818
f8eba3c6
TT
3819/* Helper function for dw2_map_symtabs_matching_filename that expands
3820 the symtabs and calls the iterator. */
3821
3822static int
3823dw2_map_expand_apply (struct objfile *objfile,
3824 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3825 const char *name, const char *real_path,
14bc53a8 3826 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3827{
43f3e411 3828 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3829
3830 /* Don't visit already-expanded CUs. */
43f3e411 3831 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3832 return 0;
3833
3834 /* This may expand more than one symtab, and we want to iterate over
3835 all of them. */
58f0c718 3836 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3837
14bc53a8
PA
3838 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3839 last_made, callback);
f8eba3c6
TT
3840}
3841
3842/* Implementation of the map_symtabs_matching_filename method. */
3843
14bc53a8
PA
3844static bool
3845dw2_map_symtabs_matching_filename
3846 (struct objfile *objfile, const char *name, const char *real_path,
3847 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3848{
c011a4f4 3849 const char *name_basename = lbasename (name);
ed2dc618
SM
3850 struct dwarf2_per_objfile *dwarf2_per_objfile
3851 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3852
848e3e78
DE
3853 /* The rule is CUs specify all the files, including those used by
3854 any TU, so there's no need to scan TUs here. */
f4dc4d17 3855
b76e467d 3856 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3857 {
3d7bb9d9 3858 /* We only need to look at symtabs not already expanded. */
43f3e411 3859 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3860 continue;
3861
b76e467d 3862 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3863 if (file_data == NULL)
9291a0cd
TT
3864 continue;
3865
b76e467d 3866 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3867 {
7b9f3c50 3868 const char *this_name = file_data->file_names[j];
da235a7c 3869 const char *this_real_name;
9291a0cd 3870
af529f8f 3871 if (compare_filenames_for_search (this_name, name))
9291a0cd 3872 {
f5b95b50 3873 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3874 callback))
3875 return true;
288e77a7 3876 continue;
4aac40c8 3877 }
9291a0cd 3878
c011a4f4
DE
3879 /* Before we invoke realpath, which can get expensive when many
3880 files are involved, do a quick comparison of the basenames. */
3881 if (! basenames_may_differ
3882 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3883 continue;
3884
da235a7c
JK
3885 this_real_name = dw2_get_real_path (objfile, file_data, j);
3886 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3887 {
da235a7c 3888 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3889 callback))
3890 return true;
288e77a7 3891 continue;
da235a7c 3892 }
9291a0cd 3893
da235a7c
JK
3894 if (real_path != NULL)
3895 {
af529f8f
JK
3896 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3897 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3898 if (this_real_name != NULL
af529f8f 3899 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3900 {
f5b95b50 3901 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3902 callback))
3903 return true;
288e77a7 3904 continue;
9291a0cd
TT
3905 }
3906 }
3907 }
3908 }
3909
14bc53a8 3910 return false;
9291a0cd
TT
3911}
3912
da51c347
DE
3913/* Struct used to manage iterating over all CUs looking for a symbol. */
3914
3915struct dw2_symtab_iterator
9291a0cd 3916{
ed2dc618
SM
3917 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3918 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3919 /* If set, only look for symbols that match that block. Valid values are
3920 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3921 gdb::optional<block_enum> block_index;
da51c347
DE
3922 /* The kind of symbol we're looking for. */
3923 domain_enum domain;
3924 /* The list of CUs from the index entry of the symbol,
3925 or NULL if not found. */
3926 offset_type *vec;
3927 /* The next element in VEC to look at. */
3928 int next;
3929 /* The number of elements in VEC, or zero if there is no match. */
3930 int length;
8943b874
DE
3931 /* Have we seen a global version of the symbol?
3932 If so we can ignore all further global instances.
3933 This is to work around gold/15646, inefficient gold-generated
3934 indices. */
3935 int global_seen;
da51c347 3936};
9291a0cd 3937
2b79f376 3938/* Initialize the index symtab iterator ITER. */
2fdf6df6 3939
9291a0cd 3940static void
da51c347 3941dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3942 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3943 gdb::optional<block_enum> block_index,
da51c347
DE
3944 domain_enum domain,
3945 const char *name)
3946{
ed2dc618 3947 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3948 iter->block_index = block_index;
3949 iter->domain = domain;
3950 iter->next = 0;
8943b874 3951 iter->global_seen = 0;
da51c347 3952
3063847f 3953 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3954
3955 /* index is NULL if OBJF_READNOW. */
3956 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3957 iter->length = MAYBE_SWAP (*iter->vec);
3958 else
3959 {
3960 iter->vec = NULL;
3961 iter->length = 0;
3962 }
3963}
3964
3965/* Return the next matching CU or NULL if there are no more. */
3966
3967static struct dwarf2_per_cu_data *
3968dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3969{
ed2dc618
SM
3970 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3971
da51c347
DE
3972 for ( ; iter->next < iter->length; ++iter->next)
3973 {
3974 offset_type cu_index_and_attrs =
3975 MAYBE_SWAP (iter->vec[iter->next + 1]);
3976 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3977 gdb_index_symbol_kind symbol_kind =
3978 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3979 /* Only check the symbol attributes if they're present.
3980 Indices prior to version 7 don't record them,
3981 and indices >= 7 may elide them for certain symbols
3982 (gold does this). */
3983 int attrs_valid =
ed2dc618 3984 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3985 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3986
3190f0c6 3987 /* Don't crash on bad data. */
b76e467d 3988 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3989 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3990 {
b98664d3 3991 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3992 " [in module %s]"),
3993 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3994 continue;
3995 }
3996
ff4c9fec 3997 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3998
da51c347 3999 /* Skip if already read in. */
43f3e411 4000 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4001 continue;
4002
8943b874
DE
4003 /* Check static vs global. */
4004 if (attrs_valid)
4005 {
2b79f376
SM
4006 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4007
4008 if (iter->block_index.has_value ())
4009 {
4010 bool want_static = *iter->block_index == STATIC_BLOCK;
4011
4012 if (is_static != want_static)
4013 continue;
4014 }
4015
8943b874
DE
4016 /* Work around gold/15646. */
4017 if (!is_static && iter->global_seen)
4018 continue;
4019 if (!is_static)
4020 iter->global_seen = 1;
4021 }
da51c347
DE
4022
4023 /* Only check the symbol's kind if it has one. */
4024 if (attrs_valid)
4025 {
4026 switch (iter->domain)
4027 {
4028 case VAR_DOMAIN:
4029 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4030 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4031 /* Some types are also in VAR_DOMAIN. */
4032 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4033 continue;
4034 break;
4035 case STRUCT_DOMAIN:
4036 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4037 continue;
4038 break;
4039 case LABEL_DOMAIN:
4040 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4041 continue;
4042 break;
59c35742
AB
4043 case MODULE_DOMAIN:
4044 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4045 continue;
4046 break;
da51c347
DE
4047 default:
4048 break;
4049 }
4050 }
4051
4052 ++iter->next;
4053 return per_cu;
4054 }
4055
4056 return NULL;
4057}
4058
43f3e411 4059static struct compunit_symtab *
c7f839cb 4060dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4061 const char *name, domain_enum domain)
9291a0cd 4062{
43f3e411 4063 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4064 struct dwarf2_per_objfile *dwarf2_per_objfile
4065 = get_dwarf2_per_objfile (objfile);
9291a0cd 4066
b5ec771e
PA
4067 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4068
ed2dc618
SM
4069 struct dw2_symtab_iterator iter;
4070 struct dwarf2_per_cu_data *per_cu;
da51c347 4071
2b79f376 4072 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4073
ed2dc618
SM
4074 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4075 {
4076 struct symbol *sym, *with_opaque = NULL;
58f0c718 4077 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4078 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4079 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4080
ed2dc618
SM
4081 sym = block_find_symbol (block, name, domain,
4082 block_find_non_opaque_type_preferred,
4083 &with_opaque);
b2e2f908 4084
ed2dc618
SM
4085 /* Some caution must be observed with overloaded functions
4086 and methods, since the index will not contain any overload
4087 information (but NAME might contain it). */
da51c347 4088
ed2dc618
SM
4089 if (sym != NULL
4090 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4091 return stab;
4092 if (with_opaque != NULL
4093 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4094 stab_best = stab;
da51c347 4095
ed2dc618 4096 /* Keep looking through other CUs. */
9291a0cd 4097 }
9291a0cd 4098
da51c347 4099 return stab_best;
9291a0cd
TT
4100}
4101
4102static void
4103dw2_print_stats (struct objfile *objfile)
4104{
ed2dc618
SM
4105 struct dwarf2_per_objfile *dwarf2_per_objfile
4106 = get_dwarf2_per_objfile (objfile);
b76e467d 4107 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4108 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4109 int count = 0;
9291a0cd 4110
ed2dc618 4111 for (int i = 0; i < total; ++i)
9291a0cd 4112 {
ff4c9fec 4113 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4114
43f3e411 4115 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4116 ++count;
4117 }
e4a48d9d 4118 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4119 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4120}
4121
779bd270
DE
4122/* This dumps minimal information about the index.
4123 It is called via "mt print objfiles".
4124 One use is to verify .gdb_index has been loaded by the
4125 gdb.dwarf2/gdb-index.exp testcase. */
4126
9291a0cd
TT
4127static void
4128dw2_dump (struct objfile *objfile)
4129{
ed2dc618
SM
4130 struct dwarf2_per_objfile *dwarf2_per_objfile
4131 = get_dwarf2_per_objfile (objfile);
4132
779bd270
DE
4133 gdb_assert (dwarf2_per_objfile->using_index);
4134 printf_filtered (".gdb_index:");
4135 if (dwarf2_per_objfile->index_table != NULL)
4136 {
4137 printf_filtered (" version %d\n",
4138 dwarf2_per_objfile->index_table->version);
4139 }
4140 else
4141 printf_filtered (" faked for \"readnow\"\n");
4142 printf_filtered ("\n");
9291a0cd
TT
4143}
4144
9291a0cd
TT
4145static void
4146dw2_expand_symtabs_for_function (struct objfile *objfile,
4147 const char *func_name)
4148{
ed2dc618
SM
4149 struct dwarf2_per_objfile *dwarf2_per_objfile
4150 = get_dwarf2_per_objfile (objfile);
da51c347 4151
ed2dc618
SM
4152 struct dw2_symtab_iterator iter;
4153 struct dwarf2_per_cu_data *per_cu;
da51c347 4154
2b79f376 4155 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4156
ed2dc618 4157 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4158 dw2_instantiate_symtab (per_cu, false);
da51c347 4159
9291a0cd
TT
4160}
4161
4162static void
4163dw2_expand_all_symtabs (struct objfile *objfile)
4164{
ed2dc618
SM
4165 struct dwarf2_per_objfile *dwarf2_per_objfile
4166 = get_dwarf2_per_objfile (objfile);
b76e467d 4167 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4168 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4169
ed2dc618 4170 for (int i = 0; i < total_units; ++i)
9291a0cd 4171 {
ff4c9fec 4172 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4173
58f0c718
TT
4174 /* We don't want to directly expand a partial CU, because if we
4175 read it with the wrong language, then assertion failures can
4176 be triggered later on. See PR symtab/23010. So, tell
4177 dw2_instantiate_symtab to skip partial CUs -- any important
4178 partial CU will be read via DW_TAG_imported_unit anyway. */
4179 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4180 }
4181}
4182
4183static void
652a8996
JK
4184dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4185 const char *fullname)
9291a0cd 4186{
ed2dc618
SM
4187 struct dwarf2_per_objfile *dwarf2_per_objfile
4188 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4189
4190 /* We don't need to consider type units here.
4191 This is only called for examining code, e.g. expand_line_sal.
4192 There can be an order of magnitude (or more) more type units
4193 than comp units, and we avoid them if we can. */
4194
b76e467d 4195 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4196 {
3d7bb9d9 4197 /* We only need to look at symtabs not already expanded. */
43f3e411 4198 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4199 continue;
4200
b76e467d 4201 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4202 if (file_data == NULL)
9291a0cd
TT
4203 continue;
4204
b76e467d 4205 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4206 {
652a8996
JK
4207 const char *this_fullname = file_data->file_names[j];
4208
4209 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4210 {
58f0c718 4211 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4212 break;
4213 }
4214 }
4215 }
4216}
4217
9291a0cd 4218static void
199b4314
TT
4219dw2_map_matching_symbols
4220 (struct objfile *objfile,
b054970d 4221 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4222 int global,
4223 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4224 symbol_compare_ftype *ordered_compare)
9291a0cd 4225{
40658b94 4226 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4227 current language is Ada for a non-Ada objfile using GNU index. As Ada
4228 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4229}
4230
e1ef7d7a
PA
4231/* Starting from a search name, return the string that finds the upper
4232 bound of all strings that start with SEARCH_NAME in a sorted name
4233 list. Returns the empty string to indicate that the upper bound is
4234 the end of the list. */
4235
4236static std::string
4237make_sort_after_prefix_name (const char *search_name)
4238{
4239 /* When looking to complete "func", we find the upper bound of all
4240 symbols that start with "func" by looking for where we'd insert
4241 the closest string that would follow "func" in lexicographical
4242 order. Usually, that's "func"-with-last-character-incremented,
4243 i.e. "fund". Mind non-ASCII characters, though. Usually those
4244 will be UTF-8 multi-byte sequences, but we can't be certain.
4245 Especially mind the 0xff character, which is a valid character in
4246 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4247 rule out compilers allowing it in identifiers. Note that
4248 conveniently, strcmp/strcasecmp are specified to compare
4249 characters interpreted as unsigned char. So what we do is treat
4250 the whole string as a base 256 number composed of a sequence of
4251 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4252 to 0, and carries 1 to the following more-significant position.
4253 If the very first character in SEARCH_NAME ends up incremented
4254 and carries/overflows, then the upper bound is the end of the
4255 list. The string after the empty string is also the empty
4256 string.
4257
4258 Some examples of this operation:
4259
4260 SEARCH_NAME => "+1" RESULT
4261
4262 "abc" => "abd"
4263 "ab\xff" => "ac"
4264 "\xff" "a" "\xff" => "\xff" "b"
4265 "\xff" => ""
4266 "\xff\xff" => ""
4267 "" => ""
4268
4269 Then, with these symbols for example:
4270
4271 func
4272 func1
4273 fund
4274
4275 completing "func" looks for symbols between "func" and
4276 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4277 which finds "func" and "func1", but not "fund".
4278
4279 And with:
4280
4281 funcÿ (Latin1 'ÿ' [0xff])
4282 funcÿ1
4283 fund
4284
4285 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4286 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4287
4288 And with:
4289
4290 ÿÿ (Latin1 'ÿ' [0xff])
4291 ÿÿ1
4292
4293 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4294 the end of the list.
4295 */
4296 std::string after = search_name;
4297 while (!after.empty () && (unsigned char) after.back () == 0xff)
4298 after.pop_back ();
4299 if (!after.empty ())
4300 after.back () = (unsigned char) after.back () + 1;
4301 return after;
4302}
4303
5c58de74 4304/* See declaration. */
61d96d7e 4305
5c58de74
PA
4306std::pair<std::vector<name_component>::const_iterator,
4307 std::vector<name_component>::const_iterator>
44ed8f3e 4308mapped_index_base::find_name_components_bounds
3b00ef10 4309 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4310{
5c58de74
PA
4311 auto *name_cmp
4312 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4313
3b00ef10
TT
4314 const char *lang_name
4315 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4316
3f563c84
PA
4317 /* Comparison function object for lower_bound that matches against a
4318 given symbol name. */
4319 auto lookup_compare_lower = [&] (const name_component &elem,
4320 const char *name)
4321 {
5c58de74 4322 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4323 const char *elem_name = elem_qualified + elem.name_offset;
4324 return name_cmp (elem_name, name) < 0;
4325 };
4326
4327 /* Comparison function object for upper_bound that matches against a
4328 given symbol name. */
4329 auto lookup_compare_upper = [&] (const char *name,
4330 const name_component &elem)
4331 {
5c58de74 4332 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4333 const char *elem_name = elem_qualified + elem.name_offset;
4334 return name_cmp (name, elem_name) < 0;
4335 };
4336
5c58de74
PA
4337 auto begin = this->name_components.begin ();
4338 auto end = this->name_components.end ();
3f563c84
PA
4339
4340 /* Find the lower bound. */
4341 auto lower = [&] ()
4342 {
3b00ef10 4343 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4344 return begin;
4345 else
3b00ef10 4346 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4347 } ();
4348
4349 /* Find the upper bound. */
4350 auto upper = [&] ()
4351 {
5c58de74 4352 if (lookup_name_without_params.completion_mode ())
3f563c84 4353 {
e1ef7d7a
PA
4354 /* In completion mode, we want UPPER to point past all
4355 symbols names that have the same prefix. I.e., with
4356 these symbols, and completing "func":
4357
4358 function << lower bound
4359 function1
4360 other_function << upper bound
4361
4362 We find the upper bound by looking for the insertion
4363 point of "func"-with-last-character-incremented,
4364 i.e. "fund". */
3b00ef10 4365 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4366 if (after.empty ())
3f563c84 4367 return end;
e6b2f5ef
PA
4368 return std::lower_bound (lower, end, after.c_str (),
4369 lookup_compare_lower);
3f563c84
PA
4370 }
4371 else
3b00ef10 4372 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4373 } ();
4374
5c58de74
PA
4375 return {lower, upper};
4376}
4377
4378/* See declaration. */
4379
4380void
44ed8f3e 4381mapped_index_base::build_name_components ()
5c58de74
PA
4382{
4383 if (!this->name_components.empty ())
4384 return;
4385
4386 this->name_components_casing = case_sensitivity;
4387 auto *name_cmp
4388 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4389
4390 /* The code below only knows how to break apart components of C++
4391 symbol names (and other languages that use '::' as
3b00ef10 4392 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4393 auto count = this->symbol_name_count ();
4394 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4395 {
44ed8f3e 4396 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4397 continue;
4398
4399 const char *name = this->symbol_name_at (idx);
4400
4401 /* Add each name component to the name component table. */
4402 unsigned int previous_len = 0;
3b00ef10
TT
4403
4404 if (strstr (name, "::") != nullptr)
4405 {
4406 for (unsigned int current_len = cp_find_first_component (name);
4407 name[current_len] != '\0';
4408 current_len += cp_find_first_component (name + current_len))
4409 {
4410 gdb_assert (name[current_len] == ':');
4411 this->name_components.push_back ({previous_len, idx});
4412 /* Skip the '::'. */
4413 current_len += 2;
4414 previous_len = current_len;
4415 }
4416 }
4417 else
5c58de74 4418 {
3b00ef10
TT
4419 /* Handle the Ada encoded (aka mangled) form here. */
4420 for (const char *iter = strstr (name, "__");
4421 iter != nullptr;
4422 iter = strstr (iter, "__"))
4423 {
4424 this->name_components.push_back ({previous_len, idx});
4425 iter += 2;
4426 previous_len = iter - name;
4427 }
5c58de74 4428 }
3b00ef10 4429
5c58de74
PA
4430 this->name_components.push_back ({previous_len, idx});
4431 }
4432
4433 /* Sort name_components elements by name. */
4434 auto name_comp_compare = [&] (const name_component &left,
4435 const name_component &right)
4436 {
4437 const char *left_qualified = this->symbol_name_at (left.idx);
4438 const char *right_qualified = this->symbol_name_at (right.idx);
4439
4440 const char *left_name = left_qualified + left.name_offset;
4441 const char *right_name = right_qualified + right.name_offset;
4442
4443 return name_cmp (left_name, right_name) < 0;
4444 };
4445
4446 std::sort (this->name_components.begin (),
4447 this->name_components.end (),
4448 name_comp_compare);
4449}
4450
4451/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4452 mapped_index_base instead of the containing objfile. This is split
4453 to a separate function in order to be able to unit test the
4454 name_components matching using a mock mapped_index_base. For each
5c58de74 4455 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4456 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4457
4458static void
4459dw2_expand_symtabs_matching_symbol
44ed8f3e 4460 (mapped_index_base &index,
5c58de74
PA
4461 const lookup_name_info &lookup_name_in,
4462 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4463 enum search_domain kind,
3b00ef10 4464 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4465{
4466 lookup_name_info lookup_name_without_params
4467 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4468
4469 /* Build the symbol name component sorted vector, if we haven't
4470 yet. */
4471 index.build_name_components ();
4472
3f563c84
PA
4473 /* The same symbol may appear more than once in the range though.
4474 E.g., if we're looking for symbols that complete "w", and we have
4475 a symbol named "w1::w2", we'll find the two name components for
4476 that same symbol in the range. To be sure we only call the
4477 callback once per symbol, we first collect the symbol name
4478 indexes that matched in a temporary vector and ignore
4479 duplicates. */
4480 std::vector<offset_type> matches;
3f563c84 4481
3b00ef10
TT
4482 struct name_and_matcher
4483 {
4484 symbol_name_matcher_ftype *matcher;
4485 const std::string &name;
4486
4487 bool operator== (const name_and_matcher &other) const
3f563c84 4488 {
3b00ef10
TT
4489 return matcher == other.matcher && name == other.name;
4490 }
4491 };
4492
4493 /* A vector holding all the different symbol name matchers, for all
4494 languages. */
4495 std::vector<name_and_matcher> matchers;
4496
4497 for (int i = 0; i < nr_languages; i++)
4498 {
4499 enum language lang_e = (enum language) i;
4500
4501 const language_defn *lang = language_def (lang_e);
4502 symbol_name_matcher_ftype *name_matcher
4503 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4504
3b00ef10
TT
4505 name_and_matcher key {
4506 name_matcher,
4507 lookup_name_without_params.language_lookup_name (lang_e)
4508 };
4509
4510 /* Don't insert the same comparison routine more than once.
4511 Note that we do this linear walk. This is not a problem in
4512 practice because the number of supported languages is
4513 low. */
4514 if (std::find (matchers.begin (), matchers.end (), key)
4515 != matchers.end ())
9291a0cd 4516 continue;
3b00ef10
TT
4517 matchers.push_back (std::move (key));
4518
4519 auto bounds
4520 = index.find_name_components_bounds (lookup_name_without_params,
4521 lang_e);
4522
4523 /* Now for each symbol name in range, check to see if we have a name
4524 match, and if so, call the MATCH_CALLBACK callback. */
4525
4526 for (; bounds.first != bounds.second; ++bounds.first)
4527 {
4528 const char *qualified = index.symbol_name_at (bounds.first->idx);
4529
4530 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4531 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4532 continue;
9291a0cd 4533
3b00ef10
TT
4534 matches.push_back (bounds.first->idx);
4535 }
3f563c84
PA
4536 }
4537
4538 std::sort (matches.begin (), matches.end ());
4539
4540 /* Finally call the callback, once per match. */
4541 ULONGEST prev = -1;
4542 for (offset_type idx : matches)
4543 {
4544 if (prev != idx)
4545 {
3b00ef10
TT
4546 if (!match_callback (idx))
4547 break;
3f563c84
PA
4548 prev = idx;
4549 }
4550 }
4551
4552 /* Above we use a type wider than idx's for 'prev', since 0 and
4553 (offset_type)-1 are both possible values. */
4554 static_assert (sizeof (prev) > sizeof (offset_type), "");
4555}
4556
c62446b1
PA
4557#if GDB_SELF_TEST
4558
4559namespace selftests { namespace dw2_expand_symtabs_matching {
4560
a3c5fafd
PA
4561/* A mock .gdb_index/.debug_names-like name index table, enough to
4562 exercise dw2_expand_symtabs_matching_symbol, which works with the
4563 mapped_index_base interface. Builds an index from the symbol list
4564 passed as parameter to the constructor. */
4565class mock_mapped_index : public mapped_index_base
c62446b1
PA
4566{
4567public:
a3c5fafd
PA
4568 mock_mapped_index (gdb::array_view<const char *> symbols)
4569 : m_symbol_table (symbols)
c62446b1
PA
4570 {}
4571
a3c5fafd 4572 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4573
a3c5fafd 4574 /* Return the number of names in the symbol table. */
632e107b 4575 size_t symbol_name_count () const override
c62446b1 4576 {
a3c5fafd 4577 return m_symbol_table.size ();
c62446b1
PA
4578 }
4579
a3c5fafd 4580 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4581 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4582 {
4583 return m_symbol_table[idx];
4584 }
c62446b1 4585
a3c5fafd
PA
4586private:
4587 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4588};
4589
4590/* Convenience function that converts a NULL pointer to a "<null>"
4591 string, to pass to print routines. */
4592
4593static const char *
4594string_or_null (const char *str)
4595{
4596 return str != NULL ? str : "<null>";
4597}
4598
4599/* Check if a lookup_name_info built from
4600 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4601 index. EXPECTED_LIST is the list of expected matches, in expected
4602 matching order. If no match expected, then an empty list is
4603 specified. Returns true on success. On failure prints a warning
4604 indicating the file:line that failed, and returns false. */
4605
4606static bool
4607check_match (const char *file, int line,
4608 mock_mapped_index &mock_index,
4609 const char *name, symbol_name_match_type match_type,
4610 bool completion_mode,
4611 std::initializer_list<const char *> expected_list)
4612{
4613 lookup_name_info lookup_name (name, match_type, completion_mode);
4614
4615 bool matched = true;
4616
4617 auto mismatch = [&] (const char *expected_str,
4618 const char *got)
4619 {
4620 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4621 "expected=\"%s\", got=\"%s\"\n"),
4622 file, line,
4623 (match_type == symbol_name_match_type::FULL
4624 ? "FULL" : "WILD"),
4625 name, string_or_null (expected_str), string_or_null (got));
4626 matched = false;
4627 };
4628
4629 auto expected_it = expected_list.begin ();
4630 auto expected_end = expected_list.end ();
4631
a3c5fafd 4632 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4633 NULL, ALL_DOMAIN,
4634 [&] (offset_type idx)
4635 {
a3c5fafd 4636 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4637 const char *expected_str
4638 = expected_it == expected_end ? NULL : *expected_it++;
4639
4640 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4641 mismatch (expected_str, matched_name);
3b00ef10 4642 return true;
c62446b1
PA
4643 });
4644
4645 const char *expected_str
4646 = expected_it == expected_end ? NULL : *expected_it++;
4647 if (expected_str != NULL)
4648 mismatch (expected_str, NULL);
4649
4650 return matched;
4651}
4652
4653/* The symbols added to the mock mapped_index for testing (in
4654 canonical form). */
4655static const char *test_symbols[] = {
4656 "function",
4657 "std::bar",
4658 "std::zfunction",
4659 "std::zfunction2",
4660 "w1::w2",
4661 "ns::foo<char*>",
4662 "ns::foo<int>",
4663 "ns::foo<long>",
a20714ff
PA
4664 "ns2::tmpl<int>::foo2",
4665 "(anonymous namespace)::A::B::C",
c62446b1 4666
e1ef7d7a
PA
4667 /* These are used to check that the increment-last-char in the
4668 matching algorithm for completion doesn't match "t1_fund" when
4669 completing "t1_func". */
4670 "t1_func",
4671 "t1_func1",
4672 "t1_fund",
4673 "t1_fund1",
4674
4675 /* A UTF-8 name with multi-byte sequences to make sure that
4676 cp-name-parser understands this as a single identifier ("função"
4677 is "function" in PT). */
4678 u8"u8função",
4679
4680 /* \377 (0xff) is Latin1 'ÿ'. */
4681 "yfunc\377",
4682
4683 /* \377 (0xff) is Latin1 'ÿ'. */
4684 "\377",
4685 "\377\377123",
4686
c62446b1
PA
4687 /* A name with all sorts of complications. Starts with "z" to make
4688 it easier for the completion tests below. */
4689#define Z_SYM_NAME \
4690 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4691 "::tuple<(anonymous namespace)::ui*, " \
4692 "std::default_delete<(anonymous namespace)::ui>, void>"
4693
4694 Z_SYM_NAME
4695};
4696
a3c5fafd
PA
4697/* Returns true if the mapped_index_base::find_name_component_bounds
4698 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4699 in completion mode. */
5c58de74
PA
4700
4701static bool
a3c5fafd 4702check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4703 const char *search_name,
4704 gdb::array_view<const char *> expected_syms)
4705{
4706 lookup_name_info lookup_name (search_name,
4707 symbol_name_match_type::FULL, true);
4708
3b00ef10
TT
4709 auto bounds = index.find_name_components_bounds (lookup_name,
4710 language_cplus);
5c58de74
PA
4711
4712 size_t distance = std::distance (bounds.first, bounds.second);
4713 if (distance != expected_syms.size ())
4714 return false;
4715
4716 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4717 {
4718 auto nc_elem = bounds.first + exp_elem;
4719 const char *qualified = index.symbol_name_at (nc_elem->idx);
4720 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4721 return false;
4722 }
4723
4724 return true;
4725}
4726
4727/* Test the lower-level mapped_index::find_name_component_bounds
4728 method. */
4729
c62446b1 4730static void
5c58de74
PA
4731test_mapped_index_find_name_component_bounds ()
4732{
4733 mock_mapped_index mock_index (test_symbols);
4734
a3c5fafd 4735 mock_index.build_name_components ();
5c58de74
PA
4736
4737 /* Test the lower-level mapped_index::find_name_component_bounds
4738 method in completion mode. */
4739 {
4740 static const char *expected_syms[] = {
4741 "t1_func",
4742 "t1_func1",
5c58de74
PA
4743 };
4744
a3c5fafd 4745 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4746 "t1_func", expected_syms));
4747 }
4748
4749 /* Check that the increment-last-char in the name matching algorithm
4750 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4751 {
4752 static const char *expected_syms1[] = {
4753 "\377",
4754 "\377\377123",
4755 };
a3c5fafd 4756 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4757 "\377", expected_syms1));
4758
4759 static const char *expected_syms2[] = {
4760 "\377\377123",
4761 };
a3c5fafd 4762 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4763 "\377\377", expected_syms2));
4764 }
4765}
4766
4767/* Test dw2_expand_symtabs_matching_symbol. */
4768
4769static void
4770test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4771{
4772 mock_mapped_index mock_index (test_symbols);
4773
4774 /* We let all tests run until the end even if some fails, for debug
4775 convenience. */
4776 bool any_mismatch = false;
4777
4778 /* Create the expected symbols list (an initializer_list). Needed
4779 because lists have commas, and we need to pass them to CHECK,
4780 which is a macro. */
4781#define EXPECT(...) { __VA_ARGS__ }
4782
4783 /* Wrapper for check_match that passes down the current
4784 __FILE__/__LINE__. */
4785#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4786 any_mismatch |= !check_match (__FILE__, __LINE__, \
4787 mock_index, \
4788 NAME, MATCH_TYPE, COMPLETION_MODE, \
4789 EXPECTED_LIST)
4790
4791 /* Identity checks. */
4792 for (const char *sym : test_symbols)
4793 {
4794 /* Should be able to match all existing symbols. */
4795 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4796 EXPECT (sym));
4797
4798 /* Should be able to match all existing symbols with
4799 parameters. */
4800 std::string with_params = std::string (sym) + "(int)";
4801 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4802 EXPECT (sym));
4803
4804 /* Should be able to match all existing symbols with
4805 parameters and qualifiers. */
4806 with_params = std::string (sym) + " ( int ) const";
4807 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4808 EXPECT (sym));
4809
4810 /* This should really find sym, but cp-name-parser.y doesn't
4811 know about lvalue/rvalue qualifiers yet. */
4812 with_params = std::string (sym) + " ( int ) &&";
4813 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4814 {});
4815 }
4816
e1ef7d7a
PA
4817 /* Check that the name matching algorithm for completion doesn't get
4818 confused with Latin1 'ÿ' / 0xff. */
4819 {
4820 static const char str[] = "\377";
4821 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4822 EXPECT ("\377", "\377\377123"));
4823 }
4824
4825 /* Check that the increment-last-char in the matching algorithm for
4826 completion doesn't match "t1_fund" when completing "t1_func". */
4827 {
4828 static const char str[] = "t1_func";
4829 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4830 EXPECT ("t1_func", "t1_func1"));
4831 }
4832
c62446b1
PA
4833 /* Check that completion mode works at each prefix of the expected
4834 symbol name. */
4835 {
4836 static const char str[] = "function(int)";
4837 size_t len = strlen (str);
4838 std::string lookup;
4839
4840 for (size_t i = 1; i < len; i++)
4841 {
4842 lookup.assign (str, i);
4843 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4844 EXPECT ("function"));
4845 }
4846 }
4847
4848 /* While "w" is a prefix of both components, the match function
4849 should still only be called once. */
4850 {
4851 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4852 EXPECT ("w1::w2"));
a20714ff
PA
4853 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4854 EXPECT ("w1::w2"));
c62446b1
PA
4855 }
4856
4857 /* Same, with a "complicated" symbol. */
4858 {
4859 static const char str[] = Z_SYM_NAME;
4860 size_t len = strlen (str);
4861 std::string lookup;
4862
4863 for (size_t i = 1; i < len; i++)
4864 {
4865 lookup.assign (str, i);
4866 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4867 EXPECT (Z_SYM_NAME));
4868 }
4869 }
4870
4871 /* In FULL mode, an incomplete symbol doesn't match. */
4872 {
4873 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4874 {});
4875 }
4876
4877 /* A complete symbol with parameters matches any overload, since the
4878 index has no overload info. */
4879 {
4880 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4881 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4882 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4883 EXPECT ("std::zfunction", "std::zfunction2"));
4884 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4885 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4886 }
4887
4888 /* Check that whitespace is ignored appropriately. A symbol with a
4889 template argument list. */
4890 {
4891 static const char expected[] = "ns::foo<int>";
4892 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4893 EXPECT (expected));
a20714ff
PA
4894 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4895 EXPECT (expected));
c62446b1
PA
4896 }
4897
4898 /* Check that whitespace is ignored appropriately. A symbol with a
4899 template argument list that includes a pointer. */
4900 {
4901 static const char expected[] = "ns::foo<char*>";
4902 /* Try both completion and non-completion modes. */
4903 static const bool completion_mode[2] = {false, true};
4904 for (size_t i = 0; i < 2; i++)
4905 {
4906 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4907 completion_mode[i], EXPECT (expected));
a20714ff
PA
4908 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4909 completion_mode[i], EXPECT (expected));
c62446b1
PA
4910
4911 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4912 completion_mode[i], EXPECT (expected));
a20714ff
PA
4913 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4914 completion_mode[i], EXPECT (expected));
c62446b1
PA
4915 }
4916 }
4917
4918 {
4919 /* Check method qualifiers are ignored. */
4920 static const char expected[] = "ns::foo<char*>";
4921 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4922 symbol_name_match_type::FULL, true, EXPECT (expected));
4923 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4924 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4925 CHECK_MATCH ("foo < char * > ( int ) const",
4926 symbol_name_match_type::WILD, true, EXPECT (expected));
4927 CHECK_MATCH ("foo < char * > ( int ) &&",
4928 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4929 }
4930
4931 /* Test lookup names that don't match anything. */
4932 {
a20714ff
PA
4933 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4934 {});
4935
c62446b1
PA
4936 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4937 {});
4938 }
4939
a20714ff
PA
4940 /* Some wild matching tests, exercising "(anonymous namespace)",
4941 which should not be confused with a parameter list. */
4942 {
4943 static const char *syms[] = {
4944 "A::B::C",
4945 "B::C",
4946 "C",
4947 "A :: B :: C ( int )",
4948 "B :: C ( int )",
4949 "C ( int )",
4950 };
4951
4952 for (const char *s : syms)
4953 {
4954 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4955 EXPECT ("(anonymous namespace)::A::B::C"));
4956 }
4957 }
4958
4959 {
4960 static const char expected[] = "ns2::tmpl<int>::foo2";
4961 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4962 EXPECT (expected));
4963 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4964 EXPECT (expected));
4965 }
4966
c62446b1
PA
4967 SELF_CHECK (!any_mismatch);
4968
4969#undef EXPECT
4970#undef CHECK_MATCH
4971}
4972
5c58de74
PA
4973static void
4974run_test ()
4975{
4976 test_mapped_index_find_name_component_bounds ();
4977 test_dw2_expand_symtabs_matching_symbol ();
4978}
4979
c62446b1
PA
4980}} // namespace selftests::dw2_expand_symtabs_matching
4981
4982#endif /* GDB_SELF_TEST */
4983
4b514bc8
JK
4984/* If FILE_MATCHER is NULL or if PER_CU has
4985 dwarf2_per_cu_quick_data::MARK set (see
4986 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4987 EXPANSION_NOTIFY on it. */
4988
4989static void
4990dw2_expand_symtabs_matching_one
4991 (struct dwarf2_per_cu_data *per_cu,
4992 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4993 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4994{
4995 if (file_matcher == NULL || per_cu->v.quick->mark)
4996 {
4997 bool symtab_was_null
4998 = (per_cu->v.quick->compunit_symtab == NULL);
4999
58f0c718 5000 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5001
5002 if (expansion_notify != NULL
5003 && symtab_was_null
5004 && per_cu->v.quick->compunit_symtab != NULL)
5005 expansion_notify (per_cu->v.quick->compunit_symtab);
5006 }
5007}
5008
3f563c84
PA
5009/* Helper for dw2_expand_matching symtabs. Called on each symbol
5010 matched, to expand corresponding CUs that were marked. IDX is the
5011 index of the symbol name that matched. */
5012
5013static void
5014dw2_expand_marked_cus
ed2dc618 5015 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5016 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5017 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5018 search_domain kind)
5019{
3f563c84
PA
5020 offset_type *vec, vec_len, vec_idx;
5021 bool global_seen = false;
ed2dc618 5022 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5023
61920122 5024 vec = (offset_type *) (index.constant_pool
f00a2de2 5025 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5026 vec_len = MAYBE_SWAP (vec[0]);
5027 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5028 {
61920122
PA
5029 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5030 /* This value is only valid for index versions >= 7. */
5031 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5032 gdb_index_symbol_kind symbol_kind =
5033 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5034 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5035 /* Only check the symbol attributes if they're present.
5036 Indices prior to version 7 don't record them,
5037 and indices >= 7 may elide them for certain symbols
5038 (gold does this). */
5039 int attrs_valid =
5040 (index.version >= 7
5041 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5042
5043 /* Work around gold/15646. */
5044 if (attrs_valid)
9291a0cd 5045 {
61920122
PA
5046 if (!is_static && global_seen)
5047 continue;
5048 if (!is_static)
5049 global_seen = true;
5050 }
3190f0c6 5051
61920122
PA
5052 /* Only check the symbol's kind if it has one. */
5053 if (attrs_valid)
5054 {
5055 switch (kind)
8943b874 5056 {
61920122
PA
5057 case VARIABLES_DOMAIN:
5058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5059 continue;
5060 break;
5061 case FUNCTIONS_DOMAIN:
5062 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5063 continue;
61920122
PA
5064 break;
5065 case TYPES_DOMAIN:
5066 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5067 continue;
5068 break;
59c35742
AB
5069 case MODULES_DOMAIN:
5070 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5071 continue;
5072 break;
61920122
PA
5073 default:
5074 break;
8943b874 5075 }
61920122 5076 }
8943b874 5077
61920122 5078 /* Don't crash on bad data. */
b76e467d 5079 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5080 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5081 {
b98664d3 5082 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5083 " [in module %s]"),
5084 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5085 continue;
5086 }
5087
ff4c9fec 5088 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5089 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5090 expansion_notify);
61920122
PA
5091 }
5092}
5093
4b514bc8
JK
5094/* If FILE_MATCHER is non-NULL, set all the
5095 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5096 that match FILE_MATCHER. */
5097
61920122 5098static void
4b514bc8 5099dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5100 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5101 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5102{
4b514bc8 5103 if (file_matcher == NULL)
61920122
PA
5104 return;
5105
4b514bc8
JK
5106 objfile *const objfile = dwarf2_per_objfile->objfile;
5107
5108 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5109 htab_eq_pointer,
5110 NULL, xcalloc, xfree));
5111 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5112 htab_eq_pointer,
5113 NULL, xcalloc, xfree));
61920122 5114
4b514bc8
JK
5115 /* The rule is CUs specify all the files, including those used by
5116 any TU, so there's no need to scan TUs here. */
61920122 5117
b76e467d 5118 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5119 {
927aa2e7
JK
5120 QUIT;
5121
5122 per_cu->v.quick->mark = 0;
5123
5124 /* We only need to look at symtabs not already expanded. */
5125 if (per_cu->v.quick->compunit_symtab)
5126 continue;
5127
b76e467d 5128 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5129 if (file_data == NULL)
5130 continue;
5131
5132 if (htab_find (visited_not_found.get (), file_data) != NULL)
5133 continue;
5134 else if (htab_find (visited_found.get (), file_data) != NULL)
5135 {
5136 per_cu->v.quick->mark = 1;
5137 continue;
5138 }
5139
b76e467d 5140 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5141 {
5142 const char *this_real_name;
5143
5144 if (file_matcher (file_data->file_names[j], false))
5145 {
5146 per_cu->v.quick->mark = 1;
5147 break;
5148 }
5149
5150 /* Before we invoke realpath, which can get expensive when many
5151 files are involved, do a quick comparison of the basenames. */
5152 if (!basenames_may_differ
5153 && !file_matcher (lbasename (file_data->file_names[j]),
5154 true))
5155 continue;
5156
5157 this_real_name = dw2_get_real_path (objfile, file_data, j);
5158 if (file_matcher (this_real_name, false))
5159 {
5160 per_cu->v.quick->mark = 1;
5161 break;
5162 }
5163 }
5164
b76e467d
SM
5165 void **slot = htab_find_slot (per_cu->v.quick->mark
5166 ? visited_found.get ()
5167 : visited_not_found.get (),
5168 file_data, INSERT);
927aa2e7
JK
5169 *slot = file_data;
5170 }
5171}
5172
5173static void
5174dw2_expand_symtabs_matching
5175 (struct objfile *objfile,
5176 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5177 const lookup_name_info &lookup_name,
5178 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5179 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5180 enum search_domain kind)
5181{
ed2dc618
SM
5182 struct dwarf2_per_objfile *dwarf2_per_objfile
5183 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5184
5185 /* index_table is NULL if OBJF_READNOW. */
5186 if (!dwarf2_per_objfile->index_table)
5187 return;
5188
ed2dc618 5189 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5190
5191 mapped_index &index = *dwarf2_per_objfile->index_table;
5192
5193 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5194 symbol_matcher,
5195 kind, [&] (offset_type idx)
5196 {
ed2dc618 5197 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5198 expansion_notify, kind);
3b00ef10 5199 return true;
927aa2e7
JK
5200 });
5201}
5202
5203/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5204 symtab. */
5205
5206static struct compunit_symtab *
5207recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5208 CORE_ADDR pc)
5209{
5210 int i;
5211
5212 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5213 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5214 return cust;
5215
5216 if (cust->includes == NULL)
5217 return NULL;
5218
5219 for (i = 0; cust->includes[i]; ++i)
5220 {
5221 struct compunit_symtab *s = cust->includes[i];
5222
5223 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5224 if (s != NULL)
5225 return s;
5226 }
5227
5228 return NULL;
5229}
5230
5231static struct compunit_symtab *
5232dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5233 struct bound_minimal_symbol msymbol,
5234 CORE_ADDR pc,
5235 struct obj_section *section,
5236 int warn_if_readin)
5237{
5238 struct dwarf2_per_cu_data *data;
5239 struct compunit_symtab *result;
5240
d320c2b5 5241 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5242 return NULL;
5243
79748972
TT
5244 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5245 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5246 data = (struct dwarf2_per_cu_data *) addrmap_find
5247 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5248 if (!data)
5249 return NULL;
5250
5251 if (warn_if_readin && data->v.quick->compunit_symtab)
5252 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5253 paddress (get_objfile_arch (objfile), pc));
5254
5255 result
58f0c718
TT
5256 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5257 false),
927aa2e7
JK
5258 pc);
5259 gdb_assert (result != NULL);
5260 return result;
5261}
5262
5263static void
5264dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5265 void *data, int need_fullname)
5266{
ed2dc618
SM
5267 struct dwarf2_per_objfile *dwarf2_per_objfile
5268 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5269
5270 if (!dwarf2_per_objfile->filenames_cache)
5271 {
5272 dwarf2_per_objfile->filenames_cache.emplace ();
5273
5274 htab_up visited (htab_create_alloc (10,
5275 htab_hash_pointer, htab_eq_pointer,
5276 NULL, xcalloc, xfree));
5277
5278 /* The rule is CUs specify all the files, including those used
5279 by any TU, so there's no need to scan TUs here. We can
5280 ignore file names coming from already-expanded CUs. */
5281
b76e467d 5282 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5283 {
927aa2e7
JK
5284 if (per_cu->v.quick->compunit_symtab)
5285 {
5286 void **slot = htab_find_slot (visited.get (),
5287 per_cu->v.quick->file_names,
5288 INSERT);
5289
5290 *slot = per_cu->v.quick->file_names;
5291 }
5292 }
5293
b76e467d 5294 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5295 {
927aa2e7
JK
5296 /* We only need to look at symtabs not already expanded. */
5297 if (per_cu->v.quick->compunit_symtab)
5298 continue;
5299
b76e467d 5300 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5301 if (file_data == NULL)
5302 continue;
5303
b76e467d 5304 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5305 if (*slot)
5306 {
5307 /* Already visited. */
5308 continue;
5309 }
5310 *slot = file_data;
5311
5312 for (int j = 0; j < file_data->num_file_names; ++j)
5313 {
5314 const char *filename = file_data->file_names[j];
5315 dwarf2_per_objfile->filenames_cache->seen (filename);
5316 }
5317 }
5318 }
5319
5320 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5321 {
5322 gdb::unique_xmalloc_ptr<char> this_real_name;
5323
5324 if (need_fullname)
5325 this_real_name = gdb_realpath (filename);
5326 (*fun) (filename, this_real_name.get (), data);
5327 });
5328}
5329
5330static int
5331dw2_has_symbols (struct objfile *objfile)
5332{
5333 return 1;
5334}
5335
5336const struct quick_symbol_functions dwarf2_gdb_index_functions =
5337{
5338 dw2_has_symbols,
5339 dw2_find_last_source_symtab,
5340 dw2_forget_cached_source_info,
5341 dw2_map_symtabs_matching_filename,
5342 dw2_lookup_symbol,
5343 dw2_print_stats,
5344 dw2_dump,
927aa2e7
JK
5345 dw2_expand_symtabs_for_function,
5346 dw2_expand_all_symtabs,
5347 dw2_expand_symtabs_with_fullname,
5348 dw2_map_matching_symbols,
5349 dw2_expand_symtabs_matching,
5350 dw2_find_pc_sect_compunit_symtab,
5351 NULL,
5352 dw2_map_symbol_filenames
5353};
5354
5355/* DWARF-5 debug_names reader. */
5356
5357/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5358static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5359
5360/* A helper function that reads the .debug_names section in SECTION
5361 and fills in MAP. FILENAME is the name of the file containing the
5362 section; it is used for error reporting.
5363
5364 Returns true if all went well, false otherwise. */
5365
5366static bool
5367read_debug_names_from_section (struct objfile *objfile,
5368 const char *filename,
5369 struct dwarf2_section_info *section,
5370 mapped_debug_names &map)
5371{
5372 if (dwarf2_section_empty_p (section))
5373 return false;
5374
5375 /* Older elfutils strip versions could keep the section in the main
5376 executable while splitting it for the separate debug info file. */
5377 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5378 return false;
5379
5380 dwarf2_read_section (objfile, section);
5381
5382 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5383
5384 const gdb_byte *addr = section->buffer;
5385
5386 bfd *const abfd = get_section_bfd_owner (section);
5387
5388 unsigned int bytes_read;
5389 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5390 addr += bytes_read;
5391
5392 map.dwarf5_is_dwarf64 = bytes_read != 4;
5393 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5394 if (bytes_read + length != section->size)
5395 {
5396 /* There may be multiple per-CU indices. */
5397 warning (_("Section .debug_names in %s length %s does not match "
5398 "section length %s, ignoring .debug_names."),
5399 filename, plongest (bytes_read + length),
5400 pulongest (section->size));
5401 return false;
5402 }
5403
5404 /* The version number. */
5405 uint16_t version = read_2_bytes (abfd, addr);
5406 addr += 2;
5407 if (version != 5)
5408 {
5409 warning (_("Section .debug_names in %s has unsupported version %d, "
5410 "ignoring .debug_names."),
5411 filename, version);
5412 return false;
5413 }
5414
5415 /* Padding. */
5416 uint16_t padding = read_2_bytes (abfd, addr);
5417 addr += 2;
5418 if (padding != 0)
5419 {
5420 warning (_("Section .debug_names in %s has unsupported padding %d, "
5421 "ignoring .debug_names."),
5422 filename, padding);
5423 return false;
5424 }
5425
5426 /* comp_unit_count - The number of CUs in the CU list. */
5427 map.cu_count = read_4_bytes (abfd, addr);
5428 addr += 4;
5429
5430 /* local_type_unit_count - The number of TUs in the local TU
5431 list. */
5432 map.tu_count = read_4_bytes (abfd, addr);
5433 addr += 4;
5434
5435 /* foreign_type_unit_count - The number of TUs in the foreign TU
5436 list. */
5437 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5438 addr += 4;
5439 if (foreign_tu_count != 0)
5440 {
5441 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5442 "ignoring .debug_names."),
5443 filename, static_cast<unsigned long> (foreign_tu_count));
5444 return false;
5445 }
5446
5447 /* bucket_count - The number of hash buckets in the hash lookup
5448 table. */
5449 map.bucket_count = read_4_bytes (abfd, addr);
5450 addr += 4;
5451
5452 /* name_count - The number of unique names in the index. */
5453 map.name_count = read_4_bytes (abfd, addr);
5454 addr += 4;
5455
5456 /* abbrev_table_size - The size in bytes of the abbreviations
5457 table. */
5458 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5459 addr += 4;
5460
5461 /* augmentation_string_size - The size in bytes of the augmentation
5462 string. This value is rounded up to a multiple of 4. */
5463 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5464 addr += 4;
5465 map.augmentation_is_gdb = ((augmentation_string_size
5466 == sizeof (dwarf5_augmentation))
5467 && memcmp (addr, dwarf5_augmentation,
5468 sizeof (dwarf5_augmentation)) == 0);
5469 augmentation_string_size += (-augmentation_string_size) & 3;
5470 addr += augmentation_string_size;
5471
5472 /* List of CUs */
5473 map.cu_table_reordered = addr;
5474 addr += map.cu_count * map.offset_size;
5475
5476 /* List of Local TUs */
5477 map.tu_table_reordered = addr;
5478 addr += map.tu_count * map.offset_size;
5479
5480 /* Hash Lookup Table */
5481 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5482 addr += map.bucket_count * 4;
5483 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5484 addr += map.name_count * 4;
5485
5486 /* Name Table */
5487 map.name_table_string_offs_reordered = addr;
5488 addr += map.name_count * map.offset_size;
5489 map.name_table_entry_offs_reordered = addr;
5490 addr += map.name_count * map.offset_size;
5491
5492 const gdb_byte *abbrev_table_start = addr;
5493 for (;;)
5494 {
927aa2e7
JK
5495 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5496 addr += bytes_read;
5497 if (index_num == 0)
5498 break;
5499
5500 const auto insertpair
5501 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5502 if (!insertpair.second)
5503 {
5504 warning (_("Section .debug_names in %s has duplicate index %s, "
5505 "ignoring .debug_names."),
5506 filename, pulongest (index_num));
5507 return false;
5508 }
5509 mapped_debug_names::index_val &indexval = insertpair.first->second;
5510 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5511 addr += bytes_read;
5512
5513 for (;;)
5514 {
5515 mapped_debug_names::index_val::attr attr;
5516 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5517 addr += bytes_read;
5518 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5519 addr += bytes_read;
5520 if (attr.form == DW_FORM_implicit_const)
5521 {
5522 attr.implicit_const = read_signed_leb128 (abfd, addr,
5523 &bytes_read);
5524 addr += bytes_read;
5525 }
5526 if (attr.dw_idx == 0 && attr.form == 0)
5527 break;
5528 indexval.attr_vec.push_back (std::move (attr));
5529 }
5530 }
5531 if (addr != abbrev_table_start + abbrev_table_size)
5532 {
5533 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5534 "of size %s vs. written as %u, ignoring .debug_names."),
5535 filename, plongest (addr - abbrev_table_start),
5536 abbrev_table_size);
927aa2e7
JK
5537 return false;
5538 }
5539 map.entry_pool = addr;
5540
5541 return true;
5542}
5543
5544/* A helper for create_cus_from_debug_names that handles the MAP's CU
5545 list. */
5546
5547static void
ed2dc618 5548create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5549 const mapped_debug_names &map,
5550 dwarf2_section_info &section,
b76e467d 5551 bool is_dwz)
927aa2e7
JK
5552{
5553 sect_offset sect_off_prev;
5554 for (uint32_t i = 0; i <= map.cu_count; ++i)
5555 {
5556 sect_offset sect_off_next;
5557 if (i < map.cu_count)
5558 {
5559 sect_off_next
5560 = (sect_offset) (extract_unsigned_integer
5561 (map.cu_table_reordered + i * map.offset_size,
5562 map.offset_size,
5563 map.dwarf5_byte_order));
5564 }
5565 else
5566 sect_off_next = (sect_offset) section.size;
5567 if (i >= 1)
5568 {
5569 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5570 dwarf2_per_cu_data *per_cu
ed2dc618 5571 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5572 sect_off_prev, length);
b76e467d 5573 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5574 }
5575 sect_off_prev = sect_off_next;
5576 }
5577}
5578
5579/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5580 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5581
5582static void
ed2dc618 5583create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5584 const mapped_debug_names &map,
5585 const mapped_debug_names &dwz_map)
5586{
b76e467d
SM
5587 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5588 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5589
ed2dc618
SM
5590 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5591 dwarf2_per_objfile->info,
b76e467d 5592 false /* is_dwz */);
927aa2e7
JK
5593
5594 if (dwz_map.cu_count == 0)
5595 return;
5596
ed2dc618
SM
5597 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5598 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5599 true /* is_dwz */);
927aa2e7
JK
5600}
5601
5602/* Read .debug_names. If everything went ok, initialize the "quick"
5603 elements of all the CUs and return true. Otherwise, return false. */
5604
5605static bool
ed2dc618 5606dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5607{
22ca247e
TT
5608 std::unique_ptr<mapped_debug_names> map
5609 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5610 mapped_debug_names dwz_map (dwarf2_per_objfile);
5611 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5612
5613 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5614 &dwarf2_per_objfile->debug_names,
22ca247e 5615 *map))
927aa2e7
JK
5616 return false;
5617
5618 /* Don't use the index if it's empty. */
22ca247e 5619 if (map->name_count == 0)
927aa2e7
JK
5620 return false;
5621
5622 /* If there is a .dwz file, read it so we can get its CU list as
5623 well. */
ed2dc618 5624 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5625 if (dwz != NULL)
5626 {
5627 if (!read_debug_names_from_section (objfile,
00f93c44 5628 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5629 &dwz->debug_names, dwz_map))
5630 {
5631 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5632 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5633 return false;
5634 }
5635 }
5636
22ca247e 5637 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5638
22ca247e 5639 if (map->tu_count != 0)
927aa2e7
JK
5640 {
5641 /* We can only handle a single .debug_types when we have an
5642 index. */
fd5866f6 5643 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5644 return false;
5645
fd5866f6 5646 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5647
5648 create_signatured_type_table_from_debug_names
22ca247e 5649 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5650 }
5651
ed2dc618
SM
5652 create_addrmap_from_aranges (dwarf2_per_objfile,
5653 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5654
22ca247e 5655 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5656 dwarf2_per_objfile->using_index = 1;
5657 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5658 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5659
5660 return true;
5661}
5662
927aa2e7
JK
5663/* Type used to manage iterating over all CUs looking for a symbol for
5664 .debug_names. */
5665
5666class dw2_debug_names_iterator
5667{
5668public:
927aa2e7 5669 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5670 gdb::optional<block_enum> block_index,
5671 domain_enum domain,
927aa2e7 5672 const char *name)
2b79f376 5673 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5674 m_addr (find_vec_in_debug_names (map, name))
5675 {}
5676
5677 dw2_debug_names_iterator (const mapped_debug_names &map,
5678 search_domain search, uint32_t namei)
5679 : m_map (map),
5680 m_search (search),
5681 m_addr (find_vec_in_debug_names (map, namei))
5682 {}
5683
3b00ef10
TT
5684 dw2_debug_names_iterator (const mapped_debug_names &map,
5685 block_enum block_index, domain_enum domain,
5686 uint32_t namei)
5687 : m_map (map), m_block_index (block_index), m_domain (domain),
5688 m_addr (find_vec_in_debug_names (map, namei))
5689 {}
5690
927aa2e7
JK
5691 /* Return the next matching CU or NULL if there are no more. */
5692 dwarf2_per_cu_data *next ();
5693
5694private:
5695 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5696 const char *name);
5697 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5698 uint32_t namei);
5699
5700 /* The internalized form of .debug_names. */
5701 const mapped_debug_names &m_map;
5702
2b79f376
SM
5703 /* If set, only look for symbols that match that block. Valid values are
5704 GLOBAL_BLOCK and STATIC_BLOCK. */
5705 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5706
5707 /* The kind of symbol we're looking for. */
5708 const domain_enum m_domain = UNDEF_DOMAIN;
5709 const search_domain m_search = ALL_DOMAIN;
5710
5711 /* The list of CUs from the index entry of the symbol, or NULL if
5712 not found. */
5713 const gdb_byte *m_addr;
5714};
5715
5716const char *
5717mapped_debug_names::namei_to_name (uint32_t namei) const
5718{
5719 const ULONGEST namei_string_offs
5720 = extract_unsigned_integer ((name_table_string_offs_reordered
5721 + namei * offset_size),
5722 offset_size,
5723 dwarf5_byte_order);
5724 return read_indirect_string_at_offset
ed2dc618 5725 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5726}
5727
5728/* Find a slot in .debug_names for the object named NAME. If NAME is
5729 found, return pointer to its pool data. If NAME cannot be found,
5730 return NULL. */
5731
5732const gdb_byte *
5733dw2_debug_names_iterator::find_vec_in_debug_names
5734 (const mapped_debug_names &map, const char *name)
5735{
5736 int (*cmp) (const char *, const char *);
5737
54ee4252 5738 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5739 if (current_language->la_language == language_cplus
5740 || current_language->la_language == language_fortran
5741 || current_language->la_language == language_d)
5742 {
5743 /* NAME is already canonical. Drop any qualifiers as
5744 .debug_names does not contain any. */
5745
5746 if (strchr (name, '(') != NULL)
5747 {
54ee4252 5748 without_params = cp_remove_params (name);
927aa2e7 5749 if (without_params != NULL)
54ee4252 5750 name = without_params.get ();
927aa2e7
JK
5751 }
5752 }
5753
5754 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5755
5756 const uint32_t full_hash = dwarf5_djb_hash (name);
5757 uint32_t namei
5758 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5759 (map.bucket_table_reordered
5760 + (full_hash % map.bucket_count)), 4,
5761 map.dwarf5_byte_order);
5762 if (namei == 0)
5763 return NULL;
5764 --namei;
5765 if (namei >= map.name_count)
5766 {
b98664d3 5767 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5768 "[in module %s]"),
5769 namei, map.name_count,
ed2dc618 5770 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5771 return NULL;
5772 }
5773
5774 for (;;)
5775 {
5776 const uint32_t namei_full_hash
5777 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5778 (map.hash_table_reordered + namei), 4,
5779 map.dwarf5_byte_order);
5780 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5781 return NULL;
5782
5783 if (full_hash == namei_full_hash)
5784 {
5785 const char *const namei_string = map.namei_to_name (namei);
5786
5787#if 0 /* An expensive sanity check. */
5788 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5789 {
b98664d3 5790 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5791 "[in module %s]"),
5792 namei, objfile_name (dwarf2_per_objfile->objfile));
5793 return NULL;
5794 }
5795#endif
5796
5797 if (cmp (namei_string, name) == 0)
5798 {
5799 const ULONGEST namei_entry_offs
5800 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5801 + namei * map.offset_size),
5802 map.offset_size, map.dwarf5_byte_order);
5803 return map.entry_pool + namei_entry_offs;
5804 }
5805 }
5806
5807 ++namei;
5808 if (namei >= map.name_count)
5809 return NULL;
5810 }
5811}
5812
5813const gdb_byte *
5814dw2_debug_names_iterator::find_vec_in_debug_names
5815 (const mapped_debug_names &map, uint32_t namei)
5816{
5817 if (namei >= map.name_count)
5818 {
b98664d3 5819 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5820 "[in module %s]"),
5821 namei, map.name_count,
ed2dc618 5822 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5823 return NULL;
5824 }
5825
5826 const ULONGEST namei_entry_offs
5827 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5828 + namei * map.offset_size),
5829 map.offset_size, map.dwarf5_byte_order);
5830 return map.entry_pool + namei_entry_offs;
5831}
5832
5833/* See dw2_debug_names_iterator. */
5834
5835dwarf2_per_cu_data *
5836dw2_debug_names_iterator::next ()
5837{
5838 if (m_addr == NULL)
5839 return NULL;
5840
ed2dc618
SM
5841 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5842 struct objfile *objfile = dwarf2_per_objfile->objfile;
5843 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5844
5845 again:
5846
5847 unsigned int bytes_read;
5848 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5849 m_addr += bytes_read;
5850 if (abbrev == 0)
5851 return NULL;
5852
5853 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5854 if (indexval_it == m_map.abbrev_map.cend ())
5855 {
b98664d3 5856 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5857 "[in module %s]"),
ed2dc618 5858 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5859 return NULL;
5860 }
5861 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5862 enum class symbol_linkage {
5863 unknown,
5864 static_,
5865 extern_,
23c13d42 5866 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5867 dwarf2_per_cu_data *per_cu = NULL;
5868 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5869 {
5870 ULONGEST ull;
5871 switch (attr.form)
5872 {
5873 case DW_FORM_implicit_const:
5874 ull = attr.implicit_const;
5875 break;
5876 case DW_FORM_flag_present:
5877 ull = 1;
5878 break;
5879 case DW_FORM_udata:
5880 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5881 m_addr += bytes_read;
5882 break;
5883 default:
b98664d3 5884 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5885 dwarf_form_name (attr.form),
ed2dc618 5886 objfile_name (objfile));
927aa2e7
JK
5887 return NULL;
5888 }
5889 switch (attr.dw_idx)
5890 {
5891 case DW_IDX_compile_unit:
5892 /* Don't crash on bad data. */
b76e467d 5893 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5894 {
b98664d3 5895 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5896 " [in module %s]"),
5897 pulongest (ull),
5898 objfile_name (dwarf2_per_objfile->objfile));
5899 continue;
5900 }
ff4c9fec 5901 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5902 break;
8af5c486
JK
5903 case DW_IDX_type_unit:
5904 /* Don't crash on bad data. */
b2bdb8cf 5905 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5906 {
b98664d3 5907 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5908 " [in module %s]"),
5909 pulongest (ull),
5910 objfile_name (dwarf2_per_objfile->objfile));
5911 continue;
5912 }
ff4c9fec 5913 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5914 break;
927aa2e7
JK
5915 case DW_IDX_GNU_internal:
5916 if (!m_map.augmentation_is_gdb)
5917 break;
23c13d42 5918 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5919 break;
5920 case DW_IDX_GNU_external:
5921 if (!m_map.augmentation_is_gdb)
5922 break;
23c13d42 5923 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5924 break;
5925 }
5926 }
5927
5928 /* Skip if already read in. */
5929 if (per_cu->v.quick->compunit_symtab)
5930 goto again;
5931
5932 /* Check static vs global. */
23c13d42 5933 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5934 {
2b79f376 5935 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5936 const bool symbol_is_static =
5937 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5938 if (want_static != symbol_is_static)
2b79f376 5939 goto again;
927aa2e7
JK
5940 }
5941
5942 /* Match dw2_symtab_iter_next, symbol_kind
5943 and debug_names::psymbol_tag. */
5944 switch (m_domain)
5945 {
5946 case VAR_DOMAIN:
5947 switch (indexval.dwarf_tag)
5948 {
5949 case DW_TAG_variable:
5950 case DW_TAG_subprogram:
5951 /* Some types are also in VAR_DOMAIN. */
5952 case DW_TAG_typedef:
5953 case DW_TAG_structure_type:
5954 break;
5955 default:
5956 goto again;
5957 }
5958 break;
5959 case STRUCT_DOMAIN:
5960 switch (indexval.dwarf_tag)
5961 {
5962 case DW_TAG_typedef:
5963 case DW_TAG_structure_type:
5964 break;
5965 default:
5966 goto again;
5967 }
5968 break;
5969 case LABEL_DOMAIN:
5970 switch (indexval.dwarf_tag)
5971 {
5972 case 0:
5973 case DW_TAG_variable:
5974 break;
5975 default:
5976 goto again;
5977 }
5978 break;
59c35742
AB
5979 case MODULE_DOMAIN:
5980 switch (indexval.dwarf_tag)
5981 {
5982 case DW_TAG_module:
5983 break;
5984 default:
5985 goto again;
5986 }
5987 break;
927aa2e7
JK
5988 default:
5989 break;
5990 }
5991
5992 /* Match dw2_expand_symtabs_matching, symbol_kind and
5993 debug_names::psymbol_tag. */
5994 switch (m_search)
4b514bc8 5995 {
927aa2e7
JK
5996 case VARIABLES_DOMAIN:
5997 switch (indexval.dwarf_tag)
4b514bc8 5998 {
927aa2e7
JK
5999 case DW_TAG_variable:
6000 break;
6001 default:
6002 goto again;
4b514bc8 6003 }
927aa2e7
JK
6004 break;
6005 case FUNCTIONS_DOMAIN:
6006 switch (indexval.dwarf_tag)
4b514bc8 6007 {
927aa2e7
JK
6008 case DW_TAG_subprogram:
6009 break;
6010 default:
6011 goto again;
4b514bc8 6012 }
927aa2e7
JK
6013 break;
6014 case TYPES_DOMAIN:
6015 switch (indexval.dwarf_tag)
6016 {
6017 case DW_TAG_typedef:
6018 case DW_TAG_structure_type:
6019 break;
6020 default:
6021 goto again;
6022 }
6023 break;
59c35742
AB
6024 case MODULES_DOMAIN:
6025 switch (indexval.dwarf_tag)
6026 {
6027 case DW_TAG_module:
6028 break;
6029 default:
6030 goto again;
6031 }
927aa2e7
JK
6032 default:
6033 break;
4b514bc8 6034 }
927aa2e7
JK
6035
6036 return per_cu;
4b514bc8 6037}
61920122 6038
927aa2e7 6039static struct compunit_symtab *
c7f839cb 6040dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6041 const char *name, domain_enum domain)
4b514bc8 6042{
ed2dc618
SM
6043 struct dwarf2_per_objfile *dwarf2_per_objfile
6044 = get_dwarf2_per_objfile (objfile);
61920122 6045
927aa2e7
JK
6046 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6047 if (!mapp)
61920122 6048 {
927aa2e7
JK
6049 /* index is NULL if OBJF_READNOW. */
6050 return NULL;
6051 }
6052 const auto &map = *mapp;
9291a0cd 6053
2b79f376 6054 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6055
927aa2e7
JK
6056 struct compunit_symtab *stab_best = NULL;
6057 struct dwarf2_per_cu_data *per_cu;
6058 while ((per_cu = iter.next ()) != NULL)
6059 {
6060 struct symbol *sym, *with_opaque = NULL;
58f0c718 6061 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6062 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6063 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6064
927aa2e7
JK
6065 sym = block_find_symbol (block, name, domain,
6066 block_find_non_opaque_type_preferred,
6067 &with_opaque);
9703b513 6068
927aa2e7
JK
6069 /* Some caution must be observed with overloaded functions and
6070 methods, since the index will not contain any overload
6071 information (but NAME might contain it). */
a3ec0bb1 6072
927aa2e7 6073 if (sym != NULL
987012b8 6074 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
6075 return stab;
6076 if (with_opaque != NULL
987012b8 6077 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 6078 stab_best = stab;
9703b513 6079
927aa2e7 6080 /* Keep looking through other CUs. */
9703b513
TT
6081 }
6082
927aa2e7 6083 return stab_best;
9703b513
TT
6084}
6085
927aa2e7
JK
6086/* This dumps minimal information about .debug_names. It is called
6087 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6088 uses this to verify that .debug_names has been loaded. */
9291a0cd 6089
927aa2e7
JK
6090static void
6091dw2_debug_names_dump (struct objfile *objfile)
6092{
ed2dc618
SM
6093 struct dwarf2_per_objfile *dwarf2_per_objfile
6094 = get_dwarf2_per_objfile (objfile);
6095
927aa2e7
JK
6096 gdb_assert (dwarf2_per_objfile->using_index);
6097 printf_filtered (".debug_names:");
6098 if (dwarf2_per_objfile->debug_names_table)
6099 printf_filtered (" exists\n");
6100 else
6101 printf_filtered (" faked for \"readnow\"\n");
6102 printf_filtered ("\n");
9291a0cd
TT
6103}
6104
9291a0cd 6105static void
927aa2e7
JK
6106dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6107 const char *func_name)
9291a0cd 6108{
ed2dc618
SM
6109 struct dwarf2_per_objfile *dwarf2_per_objfile
6110 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6111
927aa2e7
JK
6112 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6113 if (dwarf2_per_objfile->debug_names_table)
24c79950 6114 {
927aa2e7 6115 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6116
2b79f376 6117 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6118
927aa2e7
JK
6119 struct dwarf2_per_cu_data *per_cu;
6120 while ((per_cu = iter.next ()) != NULL)
58f0c718 6121 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6122 }
6123}
24c79950 6124
3b00ef10
TT
6125static void
6126dw2_debug_names_map_matching_symbols
6127 (struct objfile *objfile,
6128 const lookup_name_info &name, domain_enum domain,
6129 int global,
6130 gdb::function_view<symbol_found_callback_ftype> callback,
6131 symbol_compare_ftype *ordered_compare)
6132{
6133 struct dwarf2_per_objfile *dwarf2_per_objfile
6134 = get_dwarf2_per_objfile (objfile);
6135
6136 /* debug_names_table is NULL if OBJF_READNOW. */
6137 if (!dwarf2_per_objfile->debug_names_table)
6138 return;
6139
6140 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6141 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6142
6143 const char *match_name = name.ada ().lookup_name ().c_str ();
6144 auto matcher = [&] (const char *symname)
6145 {
6146 if (ordered_compare == nullptr)
6147 return true;
6148 return ordered_compare (symname, match_name) == 0;
6149 };
6150
6151 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6152 [&] (offset_type namei)
6153 {
6154 /* The name was matched, now expand corresponding CUs that were
6155 marked. */
6156 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6157
6158 struct dwarf2_per_cu_data *per_cu;
6159 while ((per_cu = iter.next ()) != NULL)
6160 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6161 return true;
6162 });
6163
6164 /* It's a shame we couldn't do this inside the
6165 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6166 that have already been expanded. Instead, this loop matches what
6167 the psymtab code does. */
6168 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6169 {
6170 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6171 if (cust != nullptr)
6172 {
6173 const struct block *block
6174 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6175 if (!iterate_over_symbols_terminated (block, name,
6176 domain, callback))
6177 break;
6178 }
6179 }
6180}
6181
927aa2e7
JK
6182static void
6183dw2_debug_names_expand_symtabs_matching
6184 (struct objfile *objfile,
6185 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6186 const lookup_name_info &lookup_name,
6187 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6188 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6189 enum search_domain kind)
6190{
ed2dc618
SM
6191 struct dwarf2_per_objfile *dwarf2_per_objfile
6192 = get_dwarf2_per_objfile (objfile);
9291a0cd 6193
927aa2e7
JK
6194 /* debug_names_table is NULL if OBJF_READNOW. */
6195 if (!dwarf2_per_objfile->debug_names_table)
6196 return;
9291a0cd 6197
ed2dc618 6198 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6199
44ed8f3e 6200 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6201
44ed8f3e
PA
6202 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6203 symbol_matcher,
6204 kind, [&] (offset_type namei)
927aa2e7 6205 {
927aa2e7
JK
6206 /* The name was matched, now expand corresponding CUs that were
6207 marked. */
6208 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6209
927aa2e7
JK
6210 struct dwarf2_per_cu_data *per_cu;
6211 while ((per_cu = iter.next ()) != NULL)
6212 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6213 expansion_notify);
3b00ef10 6214 return true;
44ed8f3e 6215 });
9291a0cd
TT
6216}
6217
927aa2e7 6218const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6219{
6220 dw2_has_symbols,
6221 dw2_find_last_source_symtab,
6222 dw2_forget_cached_source_info,
f8eba3c6 6223 dw2_map_symtabs_matching_filename,
927aa2e7 6224 dw2_debug_names_lookup_symbol,
9291a0cd 6225 dw2_print_stats,
927aa2e7 6226 dw2_debug_names_dump,
927aa2e7 6227 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6228 dw2_expand_all_symtabs,
652a8996 6229 dw2_expand_symtabs_with_fullname,
3b00ef10 6230 dw2_debug_names_map_matching_symbols,
927aa2e7 6231 dw2_debug_names_expand_symtabs_matching,
43f3e411 6232 dw2_find_pc_sect_compunit_symtab,
71a3c369 6233 NULL,
9291a0cd
TT
6234 dw2_map_symbol_filenames
6235};
6236
4485a1c1
SM
6237/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6238 to either a dwarf2_per_objfile or dwz_file object. */
6239
6240template <typename T>
6241static gdb::array_view<const gdb_byte>
6242get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6243{
6244 dwarf2_section_info *section = &section_owner->gdb_index;
6245
6246 if (dwarf2_section_empty_p (section))
6247 return {};
6248
6249 /* Older elfutils strip versions could keep the section in the main
6250 executable while splitting it for the separate debug info file. */
6251 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6252 return {};
6253
6254 dwarf2_read_section (obj, section);
6255
8bebfcda
PA
6256 /* dwarf2_section_info::size is a bfd_size_type, while
6257 gdb::array_view works with size_t. On 32-bit hosts, with
6258 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6259 is 32-bit. So we need an explicit narrowing conversion here.
6260 This is fine, because it's impossible to allocate or mmap an
6261 array/buffer larger than what size_t can represent. */
6262 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6263}
6264
87d6a7aa
SM
6265/* Lookup the index cache for the contents of the index associated to
6266 DWARF2_OBJ. */
6267
6268static gdb::array_view<const gdb_byte>
6269get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6270{
6271 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6272 if (build_id == nullptr)
6273 return {};
6274
6275 return global_index_cache.lookup_gdb_index (build_id,
6276 &dwarf2_obj->index_cache_res);
6277}
6278
6279/* Same as the above, but for DWZ. */
6280
6281static gdb::array_view<const gdb_byte>
6282get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6283{
6284 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6285 if (build_id == nullptr)
6286 return {};
6287
6288 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6289}
6290
3c0aa29a 6291/* See symfile.h. */
9291a0cd 6292
3c0aa29a
PA
6293bool
6294dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6295{
ed2dc618
SM
6296 struct dwarf2_per_objfile *dwarf2_per_objfile
6297 = get_dwarf2_per_objfile (objfile);
6298
9291a0cd
TT
6299 /* If we're about to read full symbols, don't bother with the
6300 indices. In this case we also don't care if some other debug
6301 format is making psymtabs, because they are all about to be
6302 expanded anyway. */
6303 if ((objfile->flags & OBJF_READNOW))
6304 {
9291a0cd 6305 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6306 create_all_comp_units (dwarf2_per_objfile);
6307 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6308 dwarf2_per_objfile->quick_file_names_table
6309 = create_quick_file_names_table
6310 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6311
b76e467d 6312 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6313 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6314 {
ff4c9fec 6315 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6316
e254ef6a
DE
6317 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6318 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6319 }
6320
6321 /* Return 1 so that gdb sees the "quick" functions. However,
6322 these functions will be no-ops because we will have expanded
6323 all symtabs. */
3c0aa29a
PA
6324 *index_kind = dw_index_kind::GDB_INDEX;
6325 return true;
9291a0cd
TT
6326 }
6327
ed2dc618 6328 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6329 {
6330 *index_kind = dw_index_kind::DEBUG_NAMES;
6331 return true;
6332 }
927aa2e7 6333
4485a1c1
SM
6334 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6335 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6336 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6337 {
6338 *index_kind = dw_index_kind::GDB_INDEX;
6339 return true;
6340 }
9291a0cd 6341
87d6a7aa
SM
6342 /* ... otherwise, try to find the index in the index cache. */
6343 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6344 get_gdb_index_contents_from_cache,
6345 get_gdb_index_contents_from_cache_dwz))
6346 {
6347 global_index_cache.hit ();
6348 *index_kind = dw_index_kind::GDB_INDEX;
6349 return true;
6350 }
6351
6352 global_index_cache.miss ();
3c0aa29a 6353 return false;
9291a0cd
TT
6354}
6355
6356\f
6357
dce234bc
PP
6358/* Build a partial symbol table. */
6359
6360void
f29dff0a 6361dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6362{
ed2dc618
SM
6363 struct dwarf2_per_objfile *dwarf2_per_objfile
6364 = get_dwarf2_per_objfile (objfile);
c9bf0622 6365
6eee24ce 6366 init_psymbol_list (objfile, 1024);
c906108c 6367
a70b8144 6368 try
c9bf0622
TT
6369 {
6370 /* This isn't really ideal: all the data we allocate on the
6371 objfile's obstack is still uselessly kept around. However,
6372 freeing it seems unsafe. */
906768f9 6373 psymtab_discarder psymtabs (objfile);
ed2dc618 6374 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6375 psymtabs.keep ();
87d6a7aa
SM
6376
6377 /* (maybe) store an index in the cache. */
6378 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6379 }
230d2906 6380 catch (const gdb_exception_error &except)
492d29ea
PA
6381 {
6382 exception_print (gdb_stderr, except);
6383 }
c906108c 6384}
c906108c 6385
1ce1cefd
DE
6386/* Return the total length of the CU described by HEADER. */
6387
6388static unsigned int
6389get_cu_length (const struct comp_unit_head *header)
6390{
6391 return header->initial_length_size + header->length;
6392}
6393
9c541725 6394/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6395
9c541725
PA
6396static inline bool
6397offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6398{
9c541725
PA
6399 sect_offset bottom = cu_header->sect_off;
6400 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6401
9c541725 6402 return sect_off >= bottom && sect_off < top;
45452591
DE
6403}
6404
3b80fe9b
DE
6405/* Find the base address of the compilation unit for range lists and
6406 location lists. It will normally be specified by DW_AT_low_pc.
6407 In DWARF-3 draft 4, the base address could be overridden by
6408 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6409 compilation units with discontinuous ranges. */
6410
6411static void
6412dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6413{
6414 struct attribute *attr;
6415
6416 cu->base_known = 0;
6417 cu->base_address = 0;
6418
6419 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6420 if (attr != nullptr)
3b80fe9b 6421 {
31aa7e4e 6422 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6423 cu->base_known = 1;
6424 }
6425 else
6426 {
6427 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6428 if (attr != nullptr)
3b80fe9b 6429 {
31aa7e4e 6430 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6431 cu->base_known = 1;
6432 }
6433 }
6434}
6435
93311388 6436/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6437 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6438 NOTE: This leaves members offset, first_die_offset to be filled in
6439 by the caller. */
107d2387 6440
d521ce57 6441static const gdb_byte *
107d2387 6442read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6443 const gdb_byte *info_ptr,
6444 struct dwarf2_section_info *section,
6445 rcuh_kind section_kind)
107d2387
AC
6446{
6447 int signed_addr;
891d2f0b 6448 unsigned int bytes_read;
43988095
JK
6449 const char *filename = get_section_file_name (section);
6450 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6451
6452 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6453 cu_header->initial_length_size = bytes_read;
6454 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6455 info_ptr += bytes_read;
107d2387 6456 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6457 if (cu_header->version < 2 || cu_header->version > 5)
6458 error (_("Dwarf Error: wrong version in compilation unit header "
6459 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6460 cu_header->version, filename);
107d2387 6461 info_ptr += 2;
43988095
JK
6462 if (cu_header->version < 5)
6463 switch (section_kind)
6464 {
6465 case rcuh_kind::COMPILE:
6466 cu_header->unit_type = DW_UT_compile;
6467 break;
6468 case rcuh_kind::TYPE:
6469 cu_header->unit_type = DW_UT_type;
6470 break;
6471 default:
6472 internal_error (__FILE__, __LINE__,
6473 _("read_comp_unit_head: invalid section_kind"));
6474 }
6475 else
6476 {
6477 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6478 (read_1_byte (abfd, info_ptr));
6479 info_ptr += 1;
6480 switch (cu_header->unit_type)
6481 {
6482 case DW_UT_compile:
a084a2a6
AT
6483 case DW_UT_partial:
6484 case DW_UT_skeleton:
6485 case DW_UT_split_compile:
43988095
JK
6486 if (section_kind != rcuh_kind::COMPILE)
6487 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6488 "(is %s, should be %s) [in module %s]"),
6489 dwarf_unit_type_name (cu_header->unit_type),
6490 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6491 break;
6492 case DW_UT_type:
a084a2a6 6493 case DW_UT_split_type:
43988095
JK
6494 section_kind = rcuh_kind::TYPE;
6495 break;
6496 default:
6497 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6498 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6499 "[in module %s]"), cu_header->unit_type,
6500 dwarf_unit_type_name (DW_UT_compile),
6501 dwarf_unit_type_name (DW_UT_skeleton),
6502 dwarf_unit_type_name (DW_UT_split_compile),
6503 dwarf_unit_type_name (DW_UT_type),
6504 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6505 }
6506
6507 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6508 info_ptr += 1;
6509 }
9c541725
PA
6510 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6511 cu_header,
6512 &bytes_read);
613e1657 6513 info_ptr += bytes_read;
43988095
JK
6514 if (cu_header->version < 5)
6515 {
6516 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6517 info_ptr += 1;
6518 }
107d2387
AC
6519 signed_addr = bfd_get_sign_extend_vma (abfd);
6520 if (signed_addr < 0)
8e65ff28 6521 internal_error (__FILE__, __LINE__,
e2e0b3e5 6522 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6523 cu_header->signed_addr_p = signed_addr;
c764a876 6524
a084a2a6
AT
6525 bool header_has_signature = section_kind == rcuh_kind::TYPE
6526 || cu_header->unit_type == DW_UT_skeleton
6527 || cu_header->unit_type == DW_UT_split_compile;
43988095 6528
a084a2a6
AT
6529 if (header_has_signature)
6530 {
43988095
JK
6531 cu_header->signature = read_8_bytes (abfd, info_ptr);
6532 info_ptr += 8;
a084a2a6 6533 }
43988095 6534
a084a2a6
AT
6535 if (section_kind == rcuh_kind::TYPE)
6536 {
6537 LONGEST type_offset;
43988095
JK
6538 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6539 info_ptr += bytes_read;
9c541725
PA
6540 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6541 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6542 error (_("Dwarf Error: Too big type_offset in compilation unit "
6543 "header (is %s) [in module %s]"), plongest (type_offset),
6544 filename);
6545 }
6546
107d2387
AC
6547 return info_ptr;
6548}
6549
36586728
TT
6550/* Helper function that returns the proper abbrev section for
6551 THIS_CU. */
6552
6553static struct dwarf2_section_info *
6554get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6555{
6556 struct dwarf2_section_info *abbrev;
ed2dc618 6557 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6558
6559 if (this_cu->is_dwz)
ed2dc618 6560 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6561 else
6562 abbrev = &dwarf2_per_objfile->abbrev;
6563
6564 return abbrev;
6565}
6566
9ff913ba
DE
6567/* Subroutine of read_and_check_comp_unit_head and
6568 read_and_check_type_unit_head to simplify them.
6569 Perform various error checking on the header. */
6570
6571static void
ed2dc618
SM
6572error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6573 struct comp_unit_head *header,
4bdcc0c1
DE
6574 struct dwarf2_section_info *section,
6575 struct dwarf2_section_info *abbrev_section)
9ff913ba 6576{
a32a8923 6577 const char *filename = get_section_file_name (section);
9ff913ba 6578
9c541725 6579 if (to_underlying (header->abbrev_sect_off)
36586728 6580 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6581 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6582 "(offset %s + 6) [in module %s]"),
6583 sect_offset_str (header->abbrev_sect_off),
6584 sect_offset_str (header->sect_off),
9ff913ba
DE
6585 filename);
6586
9c541725 6587 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6588 avoid potential 32-bit overflow. */
9c541725 6589 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6590 > section->size)
9c541725 6591 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6592 "(offset %s + 0) [in module %s]"),
6593 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6594 filename);
6595}
6596
6597/* Read in a CU/TU header and perform some basic error checking.
6598 The contents of the header are stored in HEADER.
6599 The result is a pointer to the start of the first DIE. */
adabb602 6600
d521ce57 6601static const gdb_byte *
ed2dc618
SM
6602read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6603 struct comp_unit_head *header,
9ff913ba 6604 struct dwarf2_section_info *section,
4bdcc0c1 6605 struct dwarf2_section_info *abbrev_section,
d521ce57 6606 const gdb_byte *info_ptr,
43988095 6607 rcuh_kind section_kind)
72bf9492 6608{
d521ce57 6609 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6610
9c541725 6611 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6612
43988095 6613 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6614
9c541725 6615 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6616
ed2dc618
SM
6617 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6618 abbrev_section);
9ff913ba
DE
6619
6620 return info_ptr;
348e048f
DE
6621}
6622
f4dc4d17
DE
6623/* Fetch the abbreviation table offset from a comp or type unit header. */
6624
6625static sect_offset
ed2dc618
SM
6626read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6627 struct dwarf2_section_info *section,
9c541725 6628 sect_offset sect_off)
f4dc4d17 6629{
a32a8923 6630 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6631 const gdb_byte *info_ptr;
ac298888 6632 unsigned int initial_length_size, offset_size;
43988095 6633 uint16_t version;
f4dc4d17
DE
6634
6635 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6636 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6637 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6638 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6639 info_ptr += initial_length_size;
6640
6641 version = read_2_bytes (abfd, info_ptr);
6642 info_ptr += 2;
6643 if (version >= 5)
6644 {
6645 /* Skip unit type and address size. */
6646 info_ptr += 2;
6647 }
6648
9c541725 6649 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6650}
6651
aaa75496
JB
6652/* Allocate a new partial symtab for file named NAME and mark this new
6653 partial symtab as being an include of PST. */
6654
6655static void
d521ce57 6656dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6657 struct objfile *objfile)
6658{
6659 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6660
fbd9ab74
JK
6661 if (!IS_ABSOLUTE_PATH (subpst->filename))
6662 {
6663 /* It shares objfile->objfile_obstack. */
6664 subpst->dirname = pst->dirname;
6665 }
6666
a9342b62 6667 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6668 subpst->dependencies[0] = pst;
6669 subpst->number_of_dependencies = 1;
6670
aaa75496 6671 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6672
6673 /* No private part is necessary for include psymtabs. This property
6674 can be used to differentiate between such include psymtabs and
10b3939b 6675 the regular ones. */
58a9656e 6676 subpst->read_symtab_private = NULL;
aaa75496
JB
6677}
6678
6679/* Read the Line Number Program data and extract the list of files
6680 included by the source file represented by PST. Build an include
d85a05f0 6681 partial symtab for each of these included files. */
aaa75496
JB
6682
6683static void
6684dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6685 struct die_info *die,
6686 struct partial_symtab *pst)
aaa75496 6687{
fff8551c 6688 line_header_up lh;
d85a05f0 6689 struct attribute *attr;
aaa75496 6690
d85a05f0 6691 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6692 if (attr != nullptr)
9c541725 6693 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6694 if (lh == NULL)
6695 return; /* No linetable, so no includes. */
6696
79748972
TT
6697 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6698 that we pass in the raw text_low here; that is ok because we're
6699 only decoding the line table to make include partial symtabs, and
6700 so the addresses aren't really used. */
4ae976d1 6701 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6702 pst->raw_text_low (), 1);
aaa75496
JB
6703}
6704
348e048f 6705static hashval_t
52dc124a 6706hash_signatured_type (const void *item)
348e048f 6707{
9a3c8263
SM
6708 const struct signatured_type *sig_type
6709 = (const struct signatured_type *) item;
9a619af0 6710
348e048f 6711 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6712 return sig_type->signature;
348e048f
DE
6713}
6714
6715static int
52dc124a 6716eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6717{
9a3c8263
SM
6718 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6719 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6720
348e048f
DE
6721 return lhs->signature == rhs->signature;
6722}
6723
1fd400ff
TT
6724/* Allocate a hash table for signatured types. */
6725
6726static htab_t
673bfd45 6727allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6728{
6729 return htab_create_alloc_ex (41,
52dc124a
DE
6730 hash_signatured_type,
6731 eq_signatured_type,
1fd400ff
TT
6732 NULL,
6733 &objfile->objfile_obstack,
6734 hashtab_obstack_allocate,
6735 dummy_obstack_deallocate);
6736}
6737
d467dd73 6738/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6739
6740static int
d467dd73 6741add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6742{
9a3c8263 6743 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6744 std::vector<signatured_type *> *all_type_units
6745 = (std::vector<signatured_type *> *) datum;
1fd400ff 6746
b2bdb8cf 6747 all_type_units->push_back (sigt);
1fd400ff
TT
6748
6749 return 1;
6750}
6751
78d4d2c5 6752/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6753 and fill them into TYPES_HTAB. It will process only type units,
6754 therefore DW_UT_type. */
c88ee1f0 6755
78d4d2c5 6756static void
ed2dc618
SM
6757create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6758 struct dwo_file *dwo_file,
43988095
JK
6759 dwarf2_section_info *section, htab_t &types_htab,
6760 rcuh_kind section_kind)
348e048f 6761{
3019eac3 6762 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6763 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6764 bfd *abfd;
6765 const gdb_byte *info_ptr, *end_ptr;
348e048f 6766
4bdcc0c1
DE
6767 abbrev_section = (dwo_file != NULL
6768 ? &dwo_file->sections.abbrev
6769 : &dwarf2_per_objfile->abbrev);
6770
b4f54984 6771 if (dwarf_read_debug)
43988095
JK
6772 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6773 get_section_name (section),
a32a8923 6774 get_section_file_name (abbrev_section));
09406207 6775
78d4d2c5
JK
6776 dwarf2_read_section (objfile, section);
6777 info_ptr = section->buffer;
348e048f 6778
78d4d2c5
JK
6779 if (info_ptr == NULL)
6780 return;
348e048f 6781
78d4d2c5
JK
6782 /* We can't set abfd until now because the section may be empty or
6783 not present, in which case the bfd is unknown. */
6784 abfd = get_section_bfd_owner (section);
348e048f 6785
78d4d2c5
JK
6786 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6787 because we don't need to read any dies: the signature is in the
6788 header. */
3019eac3 6789
78d4d2c5
JK
6790 end_ptr = info_ptr + section->size;
6791 while (info_ptr < end_ptr)
6792 {
78d4d2c5
JK
6793 struct signatured_type *sig_type;
6794 struct dwo_unit *dwo_tu;
6795 void **slot;
6796 const gdb_byte *ptr = info_ptr;
6797 struct comp_unit_head header;
6798 unsigned int length;
8b70b953 6799
9c541725 6800 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6801
a49dd8dd
JK
6802 /* Initialize it due to a false compiler warning. */
6803 header.signature = -1;
9c541725 6804 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6805
78d4d2c5
JK
6806 /* We need to read the type's signature in order to build the hash
6807 table, but we don't need anything else just yet. */
348e048f 6808
ed2dc618 6809 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6810 abbrev_section, ptr, section_kind);
348e048f 6811
78d4d2c5 6812 length = get_cu_length (&header);
6caca83c 6813
78d4d2c5
JK
6814 /* Skip dummy type units. */
6815 if (ptr >= info_ptr + length
43988095
JK
6816 || peek_abbrev_code (abfd, ptr) == 0
6817 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6818 {
6819 info_ptr += length;
6820 continue;
6821 }
dee91e82 6822
78d4d2c5
JK
6823 if (types_htab == NULL)
6824 {
6825 if (dwo_file)
6826 types_htab = allocate_dwo_unit_table (objfile);
6827 else
6828 types_htab = allocate_signatured_type_table (objfile);
6829 }
8b70b953 6830
78d4d2c5
JK
6831 if (dwo_file)
6832 {
6833 sig_type = NULL;
6834 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6835 struct dwo_unit);
6836 dwo_tu->dwo_file = dwo_file;
43988095 6837 dwo_tu->signature = header.signature;
9c541725 6838 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6839 dwo_tu->section = section;
9c541725 6840 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6841 dwo_tu->length = length;
6842 }
6843 else
6844 {
6845 /* N.B.: type_offset is not usable if this type uses a DWO file.
6846 The real type_offset is in the DWO file. */
6847 dwo_tu = NULL;
6848 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6849 struct signatured_type);
43988095 6850 sig_type->signature = header.signature;
9c541725 6851 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6852 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6853 sig_type->per_cu.is_debug_types = 1;
6854 sig_type->per_cu.section = section;
9c541725 6855 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6856 sig_type->per_cu.length = length;
6857 }
6858
6859 slot = htab_find_slot (types_htab,
6860 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6861 INSERT);
6862 gdb_assert (slot != NULL);
6863 if (*slot != NULL)
6864 {
9c541725 6865 sect_offset dup_sect_off;
0349ea22 6866
3019eac3
DE
6867 if (dwo_file)
6868 {
78d4d2c5
JK
6869 const struct dwo_unit *dup_tu
6870 = (const struct dwo_unit *) *slot;
6871
9c541725 6872 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6873 }
6874 else
6875 {
78d4d2c5
JK
6876 const struct signatured_type *dup_tu
6877 = (const struct signatured_type *) *slot;
6878
9c541725 6879 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6880 }
8b70b953 6881
b98664d3 6882 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6883 " the entry at offset %s, signature %s"),
6884 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6885 hex_string (header.signature));
78d4d2c5
JK
6886 }
6887 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6888
78d4d2c5 6889 if (dwarf_read_debug > 1)
9d8780f0
SM
6890 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6891 sect_offset_str (sect_off),
43988095 6892 hex_string (header.signature));
3019eac3 6893
78d4d2c5
JK
6894 info_ptr += length;
6895 }
6896}
3019eac3 6897
78d4d2c5
JK
6898/* Create the hash table of all entries in the .debug_types
6899 (or .debug_types.dwo) section(s).
6900 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6901 otherwise it is NULL.
b3c8eb43 6902
78d4d2c5 6903 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6904
78d4d2c5 6905 Note: This function processes DWO files only, not DWP files. */
348e048f 6906
78d4d2c5 6907static void
ed2dc618
SM
6908create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6909 struct dwo_file *dwo_file,
fd5866f6 6910 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6911 htab_t &types_htab)
6912{
fd5866f6
SM
6913 for (dwarf2_section_info &section : type_sections)
6914 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6915 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6916}
6917
6918/* Create the hash table of all entries in the .debug_types section,
6919 and initialize all_type_units.
6920 The result is zero if there is an error (e.g. missing .debug_types section),
6921 otherwise non-zero. */
6922
6923static int
ed2dc618 6924create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6925{
78d4d2c5 6926 htab_t types_htab = NULL;
3019eac3 6927
ed2dc618
SM
6928 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6929 &dwarf2_per_objfile->info, types_htab,
43988095 6930 rcuh_kind::COMPILE);
ed2dc618
SM
6931 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6932 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6933 if (types_htab == NULL)
6934 {
6935 dwarf2_per_objfile->signatured_types = NULL;
6936 return 0;
6937 }
6938
348e048f
DE
6939 dwarf2_per_objfile->signatured_types = types_htab;
6940
b2bdb8cf
SM
6941 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6942 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6943
6944 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6945 &dwarf2_per_objfile->all_type_units);
1fd400ff 6946
348e048f
DE
6947 return 1;
6948}
6949
6aa5f3a6
DE
6950/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6951 If SLOT is non-NULL, it is the entry to use in the hash table.
6952 Otherwise we find one. */
6953
6954static struct signatured_type *
ed2dc618
SM
6955add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6956 void **slot)
6aa5f3a6
DE
6957{
6958 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6959
b2bdb8cf
SM
6960 if (dwarf2_per_objfile->all_type_units.size ()
6961 == dwarf2_per_objfile->all_type_units.capacity ())
6962 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6963
b2bdb8cf
SM
6964 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6965 struct signatured_type);
6966
6967 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6968 sig_type->signature = sig;
6969 sig_type->per_cu.is_debug_types = 1;
6970 if (dwarf2_per_objfile->using_index)
6971 {
6972 sig_type->per_cu.v.quick =
6973 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6974 struct dwarf2_per_cu_quick_data);
6975 }
6976
6977 if (slot == NULL)
6978 {
6979 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6980 sig_type, INSERT);
6981 }
6982 gdb_assert (*slot == NULL);
6983 *slot = sig_type;
6984 /* The rest of sig_type must be filled in by the caller. */
6985 return sig_type;
6986}
6987
a2ce51a0
DE
6988/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6989 Fill in SIG_ENTRY with DWO_ENTRY. */
6990
6991static void
ed2dc618 6992fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6993 struct signatured_type *sig_entry,
6994 struct dwo_unit *dwo_entry)
6995{
7ee85ab1 6996 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6997 gdb_assert (! sig_entry->per_cu.queued);
6998 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6999 if (dwarf2_per_objfile->using_index)
7000 {
7001 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7002 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7003 }
7004 else
7005 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7006 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7007 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7008 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7009 gdb_assert (sig_entry->dwo_unit == NULL);
7010
7011 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7012 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7013 sig_entry->per_cu.length = dwo_entry->length;
7014 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7015 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7016 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7017 sig_entry->dwo_unit = dwo_entry;
7018}
7019
7020/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7021 If we haven't read the TU yet, create the signatured_type data structure
7022 for a TU to be read in directly from a DWO file, bypassing the stub.
7023 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7024 using .gdb_index, then when reading a CU we want to stay in the DWO file
7025 containing that CU. Otherwise we could end up reading several other DWO
7026 files (due to comdat folding) to process the transitive closure of all the
7027 mentioned TUs, and that can be slow. The current DWO file will have every
7028 type signature that it needs.
a2ce51a0
DE
7029 We only do this for .gdb_index because in the psymtab case we already have
7030 to read all the DWOs to build the type unit groups. */
7031
7032static struct signatured_type *
7033lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7034{
518817b3
SM
7035 struct dwarf2_per_objfile *dwarf2_per_objfile
7036 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7037 struct objfile *objfile = dwarf2_per_objfile->objfile;
7038 struct dwo_file *dwo_file;
7039 struct dwo_unit find_dwo_entry, *dwo_entry;
7040 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7041 void **slot;
a2ce51a0
DE
7042
7043 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7044
6aa5f3a6
DE
7045 /* If TU skeletons have been removed then we may not have read in any
7046 TUs yet. */
7047 if (dwarf2_per_objfile->signatured_types == NULL)
7048 {
7049 dwarf2_per_objfile->signatured_types
7050 = allocate_signatured_type_table (objfile);
7051 }
a2ce51a0
DE
7052
7053 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7054 Use the global signatured_types array to do our own comdat-folding
7055 of types. If this is the first time we're reading this TU, and
7056 the TU has an entry in .gdb_index, replace the recorded data from
7057 .gdb_index with this TU. */
a2ce51a0 7058
a2ce51a0 7059 find_sig_entry.signature = sig;
6aa5f3a6
DE
7060 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7061 &find_sig_entry, INSERT);
9a3c8263 7062 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7063
7064 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7065 read. Don't reassign the global entry to point to this DWO if that's
7066 the case. Also note that if the TU is already being read, it may not
7067 have come from a DWO, the program may be a mix of Fission-compiled
7068 code and non-Fission-compiled code. */
7069
7070 /* Have we already tried to read this TU?
7071 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7072 needn't exist in the global table yet). */
7073 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7074 return sig_entry;
7075
6aa5f3a6
DE
7076 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7077 dwo_unit of the TU itself. */
7078 dwo_file = cu->dwo_unit->dwo_file;
7079
a2ce51a0
DE
7080 /* Ok, this is the first time we're reading this TU. */
7081 if (dwo_file->tus == NULL)
7082 return NULL;
7083 find_dwo_entry.signature = sig;
9a3c8263 7084 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7085 if (dwo_entry == NULL)
7086 return NULL;
7087
6aa5f3a6
DE
7088 /* If the global table doesn't have an entry for this TU, add one. */
7089 if (sig_entry == NULL)
ed2dc618 7090 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7091
ed2dc618 7092 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7093 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7094 return sig_entry;
7095}
7096
a2ce51a0
DE
7097/* Subroutine of lookup_signatured_type.
7098 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7099 then try the DWP file. If the TU stub (skeleton) has been removed then
7100 it won't be in .gdb_index. */
a2ce51a0
DE
7101
7102static struct signatured_type *
7103lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7104{
518817b3
SM
7105 struct dwarf2_per_objfile *dwarf2_per_objfile
7106 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7107 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7108 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7109 struct dwo_unit *dwo_entry;
7110 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7111 void **slot;
a2ce51a0
DE
7112
7113 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7114 gdb_assert (dwp_file != NULL);
7115
6aa5f3a6
DE
7116 /* If TU skeletons have been removed then we may not have read in any
7117 TUs yet. */
7118 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7119 {
6aa5f3a6
DE
7120 dwarf2_per_objfile->signatured_types
7121 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7122 }
7123
6aa5f3a6
DE
7124 find_sig_entry.signature = sig;
7125 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7126 &find_sig_entry, INSERT);
9a3c8263 7127 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7128
7129 /* Have we already tried to read this TU?
7130 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7131 needn't exist in the global table yet). */
7132 if (sig_entry != NULL)
7133 return sig_entry;
7134
a2ce51a0
DE
7135 if (dwp_file->tus == NULL)
7136 return NULL;
ed2dc618 7137 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7138 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7139 if (dwo_entry == NULL)
7140 return NULL;
7141
ed2dc618
SM
7142 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7143 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7144
a2ce51a0
DE
7145 return sig_entry;
7146}
7147
380bca97 7148/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7149 Returns NULL if signature SIG is not present in the table.
7150 It is up to the caller to complain about this. */
348e048f
DE
7151
7152static struct signatured_type *
a2ce51a0 7153lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7154{
518817b3
SM
7155 struct dwarf2_per_objfile *dwarf2_per_objfile
7156 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7157
a2ce51a0
DE
7158 if (cu->dwo_unit
7159 && dwarf2_per_objfile->using_index)
7160 {
7161 /* We're in a DWO/DWP file, and we're using .gdb_index.
7162 These cases require special processing. */
ed2dc618 7163 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7164 return lookup_dwo_signatured_type (cu, sig);
7165 else
7166 return lookup_dwp_signatured_type (cu, sig);
7167 }
7168 else
7169 {
7170 struct signatured_type find_entry, *entry;
348e048f 7171
a2ce51a0
DE
7172 if (dwarf2_per_objfile->signatured_types == NULL)
7173 return NULL;
7174 find_entry.signature = sig;
9a3c8263
SM
7175 entry = ((struct signatured_type *)
7176 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7177 return entry;
7178 }
348e048f 7179}
42e7ad6c
DE
7180\f
7181/* Low level DIE reading support. */
348e048f 7182
d85a05f0
DJ
7183/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7184
7185static void
7186init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7187 struct dwarf2_cu *cu,
3019eac3 7188 struct dwarf2_section_info *section,
685af9cd
TT
7189 struct dwo_file *dwo_file,
7190 struct abbrev_table *abbrev_table)
d85a05f0 7191{
fceca515 7192 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7193 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7194 reader->cu = cu;
3019eac3 7195 reader->dwo_file = dwo_file;
dee91e82
DE
7196 reader->die_section = section;
7197 reader->buffer = section->buffer;
f664829e 7198 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7199 reader->comp_dir = NULL;
685af9cd 7200 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7201}
7202
b0c7bfa9
DE
7203/* Subroutine of init_cutu_and_read_dies to simplify it.
7204 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7205 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7206 already.
7207
7208 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7209 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7210 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7211 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7212 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7213 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7214 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7215 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7216 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7217 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7218 kept around for at least as long as *RESULT_READER.
7219
b0c7bfa9
DE
7220 The result is non-zero if a valid (non-dummy) DIE was found. */
7221
7222static int
7223read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7224 struct dwo_unit *dwo_unit,
b0c7bfa9 7225 struct die_info *stub_comp_unit_die,
a2ce51a0 7226 const char *stub_comp_dir,
b0c7bfa9 7227 struct die_reader_specs *result_reader,
d521ce57 7228 const gdb_byte **result_info_ptr,
b0c7bfa9 7229 struct die_info **result_comp_unit_die,
685af9cd
TT
7230 int *result_has_children,
7231 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7232{
ed2dc618 7233 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7234 struct objfile *objfile = dwarf2_per_objfile->objfile;
7235 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7236 bfd *abfd;
d521ce57 7237 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7238 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7239 int i,num_extra_attrs;
7240 struct dwarf2_section_info *dwo_abbrev_section;
7241 struct attribute *attr;
7242 struct die_info *comp_unit_die;
7243
b0aeadb3
DE
7244 /* At most one of these may be provided. */
7245 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7246
b0c7bfa9
DE
7247 /* These attributes aren't processed until later:
7248 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7249 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7250 referenced later. However, these attributes are found in the stub
7251 which we won't have later. In order to not impose this complication
7252 on the rest of the code, we read them here and copy them to the
7253 DWO CU/TU die. */
b0c7bfa9
DE
7254
7255 stmt_list = NULL;
7256 low_pc = NULL;
7257 high_pc = NULL;
7258 ranges = NULL;
7259 comp_dir = NULL;
7260
7261 if (stub_comp_unit_die != NULL)
7262 {
7263 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7264 DWO file. */
7265 if (! this_cu->is_debug_types)
7266 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7267 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7268 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7269 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7270 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7271
7272 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7273 We need the value before we can process DW_FORM_GNU_addr_index
7274 or DW_FORM_addrx. */
b0c7bfa9
DE
7275 cu->addr_base = 0;
7276 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
435d3d88 7277 if (attr != nullptr)
b0c7bfa9
DE
7278 cu->addr_base = DW_UNSND (attr);
7279
7280 /* There should be a DW_AT_ranges_base attribute here (if needed).
7281 We need the value before we can process DW_AT_ranges. */
7282 cu->ranges_base = 0;
7283 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
435d3d88 7284 if (attr != nullptr)
b0c7bfa9
DE
7285 cu->ranges_base = DW_UNSND (attr);
7286 }
a2ce51a0
DE
7287 else if (stub_comp_dir != NULL)
7288 {
7289 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7290 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7291 comp_dir->name = DW_AT_comp_dir;
7292 comp_dir->form = DW_FORM_string;
7293 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7294 DW_STRING (comp_dir) = stub_comp_dir;
7295 }
b0c7bfa9
DE
7296
7297 /* Set up for reading the DWO CU/TU. */
7298 cu->dwo_unit = dwo_unit;
685af9cd 7299 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7300 dwarf2_read_section (objfile, section);
a32a8923 7301 abfd = get_section_bfd_owner (section);
9c541725
PA
7302 begin_info_ptr = info_ptr = (section->buffer
7303 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7304 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7305
7306 if (this_cu->is_debug_types)
7307 {
b0c7bfa9
DE
7308 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7309
ed2dc618
SM
7310 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7311 &cu->header, section,
b0c7bfa9 7312 dwo_abbrev_section,
43988095 7313 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7314 /* This is not an assert because it can be caused by bad debug info. */
43988095 7315 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7316 {
7317 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7318 " TU at offset %s [in module %s]"),
a2ce51a0 7319 hex_string (sig_type->signature),
43988095 7320 hex_string (cu->header.signature),
9d8780f0 7321 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7322 bfd_get_filename (abfd));
7323 }
9c541725 7324 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7325 /* For DWOs coming from DWP files, we don't know the CU length
7326 nor the type's offset in the TU until now. */
7327 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7328 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7329
7330 /* Establish the type offset that can be used to lookup the type.
7331 For DWO files, we don't know it until now. */
9c541725
PA
7332 sig_type->type_offset_in_section
7333 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7334 }
7335 else
7336 {
ed2dc618
SM
7337 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7338 &cu->header, section,
b0c7bfa9 7339 dwo_abbrev_section,
43988095 7340 info_ptr, rcuh_kind::COMPILE);
9c541725 7341 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7342 /* For DWOs coming from DWP files, we don't know the CU length
7343 until now. */
7344 dwo_unit->length = get_cu_length (&cu->header);
7345 }
7346
685af9cd
TT
7347 *result_dwo_abbrev_table
7348 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7349 cu->header.abbrev_sect_off);
7350 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7351 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7352
7353 /* Read in the die, but leave space to copy over the attributes
7354 from the stub. This has the benefit of simplifying the rest of
7355 the code - all the work to maintain the illusion of a single
7356 DW_TAG_{compile,type}_unit DIE is done here. */
7357 num_extra_attrs = ((stmt_list != NULL)
7358 + (low_pc != NULL)
7359 + (high_pc != NULL)
7360 + (ranges != NULL)
7361 + (comp_dir != NULL));
7362 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7363 result_has_children, num_extra_attrs);
7364
7365 /* Copy over the attributes from the stub to the DIE we just read in. */
7366 comp_unit_die = *result_comp_unit_die;
7367 i = comp_unit_die->num_attrs;
7368 if (stmt_list != NULL)
7369 comp_unit_die->attrs[i++] = *stmt_list;
7370 if (low_pc != NULL)
7371 comp_unit_die->attrs[i++] = *low_pc;
7372 if (high_pc != NULL)
7373 comp_unit_die->attrs[i++] = *high_pc;
7374 if (ranges != NULL)
7375 comp_unit_die->attrs[i++] = *ranges;
7376 if (comp_dir != NULL)
7377 comp_unit_die->attrs[i++] = *comp_dir;
7378 comp_unit_die->num_attrs += num_extra_attrs;
7379
b4f54984 7380 if (dwarf_die_debug)
bf6af496
DE
7381 {
7382 fprintf_unfiltered (gdb_stdlog,
7383 "Read die from %s@0x%x of %s:\n",
a32a8923 7384 get_section_name (section),
bf6af496
DE
7385 (unsigned) (begin_info_ptr - section->buffer),
7386 bfd_get_filename (abfd));
b4f54984 7387 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7388 }
7389
a2ce51a0
DE
7390 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7391 TUs by skipping the stub and going directly to the entry in the DWO file.
7392 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7393 to get it via circuitous means. Blech. */
7394 if (comp_dir != NULL)
7395 result_reader->comp_dir = DW_STRING (comp_dir);
7396
b0c7bfa9
DE
7397 /* Skip dummy compilation units. */
7398 if (info_ptr >= begin_info_ptr + dwo_unit->length
7399 || peek_abbrev_code (abfd, info_ptr) == 0)
7400 return 0;
7401
7402 *result_info_ptr = info_ptr;
7403 return 1;
7404}
7405
a084a2a6
AT
7406/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7407 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7408 signature is part of the header. */
7409static gdb::optional<ULONGEST>
7410lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7411{
7412 if (cu->header.version >= 5)
7413 return cu->header.signature;
7414 struct attribute *attr;
7415 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7416 if (attr == nullptr)
7417 return gdb::optional<ULONGEST> ();
7418 return DW_UNSND (attr);
7419}
7420
b0c7bfa9
DE
7421/* Subroutine of init_cutu_and_read_dies to simplify it.
7422 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7423 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7424
7425static struct dwo_unit *
7426lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7427 struct die_info *comp_unit_die)
7428{
7429 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7430 struct dwo_unit *dwo_unit;
7431 const char *comp_dir, *dwo_name;
7432
a2ce51a0
DE
7433 gdb_assert (cu != NULL);
7434
b0c7bfa9 7435 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7436 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7437 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7438
7439 if (this_cu->is_debug_types)
7440 {
7441 struct signatured_type *sig_type;
7442
7443 /* Since this_cu is the first member of struct signatured_type,
7444 we can go from a pointer to one to a pointer to the other. */
7445 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7446 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7447 }
7448 else
7449 {
a084a2a6
AT
7450 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7451 if (!signature.has_value ())
b0c7bfa9
DE
7452 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7453 " [in module %s]"),
e3b94546 7454 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7455 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7456 *signature);
b0c7bfa9
DE
7457 }
7458
b0c7bfa9
DE
7459 return dwo_unit;
7460}
7461
a2ce51a0 7462/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7463 See it for a description of the parameters.
fcd3b13d 7464 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7465
7466static void
6aa5f3a6
DE
7467init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7468 int use_existing_cu, int keep,
a2ce51a0
DE
7469 die_reader_func_ftype *die_reader_func,
7470 void *data)
7471{
fcd3b13d 7472 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7473 struct signatured_type *sig_type;
a2ce51a0
DE
7474 struct die_reader_specs reader;
7475 const gdb_byte *info_ptr;
7476 struct die_info *comp_unit_die;
7477 int has_children;
ed2dc618 7478 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7479
7480 /* Verify we can do the following downcast, and that we have the
7481 data we need. */
7482 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7483 sig_type = (struct signatured_type *) this_cu;
7484 gdb_assert (sig_type->dwo_unit != NULL);
7485
6aa5f3a6
DE
7486 if (use_existing_cu && this_cu->cu != NULL)
7487 {
7488 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7489 /* There's no need to do the rereading_dwo_cu handling that
7490 init_cutu_and_read_dies does since we don't read the stub. */
7491 }
7492 else
7493 {
7494 /* If !use_existing_cu, this_cu->cu must be NULL. */
7495 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7496 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7497 }
7498
7499 /* A future optimization, if needed, would be to use an existing
7500 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7501 could share abbrev tables. */
a2ce51a0 7502
685af9cd
TT
7503 /* The abbreviation table used by READER, this must live at least as long as
7504 READER. */
7505 abbrev_table_up dwo_abbrev_table;
7506
a2ce51a0 7507 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7508 NULL /* stub_comp_unit_die */,
7509 sig_type->dwo_unit->dwo_file->comp_dir,
7510 &reader, &info_ptr,
685af9cd
TT
7511 &comp_unit_die, &has_children,
7512 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7513 {
7514 /* Dummy die. */
a2ce51a0
DE
7515 return;
7516 }
7517
7518 /* All the "real" work is done here. */
7519 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7520
6aa5f3a6 7521 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7522 but the alternative is making the latter more complex.
7523 This function is only for the special case of using DWO files directly:
7524 no point in overly complicating the general case just to handle this. */
fcd3b13d 7525 if (new_cu != NULL && keep)
a2ce51a0 7526 {
fcd3b13d
SM
7527 /* Link this CU into read_in_chain. */
7528 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7529 dwarf2_per_objfile->read_in_chain = this_cu;
7530 /* The chain owns it now. */
7531 new_cu.release ();
a2ce51a0 7532 }
a2ce51a0
DE
7533}
7534
fd820528 7535/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7536 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7537
f4dc4d17
DE
7538 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7539 Otherwise the table specified in the comp unit header is read in and used.
7540 This is an optimization for when we already have the abbrev table.
7541
dee91e82
DE
7542 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7543 Otherwise, a new CU is allocated with xmalloc.
7544
7545 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7546 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7547
7548 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7549 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7550
70221824 7551static void
fd820528 7552init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7553 struct abbrev_table *abbrev_table,
fd820528 7554 int use_existing_cu, int keep,
58f0c718 7555 bool skip_partial,
fd820528
DE
7556 die_reader_func_ftype *die_reader_func,
7557 void *data)
c906108c 7558{
ed2dc618 7559 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7560 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7561 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7562 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7563 struct dwarf2_cu *cu;
d521ce57 7564 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7565 struct die_reader_specs reader;
d85a05f0 7566 struct die_info *comp_unit_die;
dee91e82 7567 int has_children;
dee91e82 7568 struct signatured_type *sig_type = NULL;
4bdcc0c1 7569 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7570 /* Non-zero if CU currently points to a DWO file and we need to
7571 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7572 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7573 int rereading_dwo_cu = 0;
c906108c 7574
b4f54984 7575 if (dwarf_die_debug)
9d8780f0 7576 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7577 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7578 sect_offset_str (this_cu->sect_off));
09406207 7579
dee91e82
DE
7580 if (use_existing_cu)
7581 gdb_assert (keep);
23745b47 7582
a2ce51a0
DE
7583 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7584 file (instead of going through the stub), short-circuit all of this. */
7585 if (this_cu->reading_dwo_directly)
7586 {
7587 /* Narrow down the scope of possibilities to have to understand. */
7588 gdb_assert (this_cu->is_debug_types);
7589 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7590 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7591 die_reader_func, data);
a2ce51a0
DE
7592 return;
7593 }
7594
dee91e82
DE
7595 /* This is cheap if the section is already read in. */
7596 dwarf2_read_section (objfile, section);
7597
9c541725 7598 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7599
7600 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7601
fcd3b13d 7602 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7603 if (use_existing_cu && this_cu->cu != NULL)
7604 {
7605 cu = this_cu->cu;
42e7ad6c
DE
7606 /* If this CU is from a DWO file we need to start over, we need to
7607 refetch the attributes from the skeleton CU.
7608 This could be optimized by retrieving those attributes from when we
7609 were here the first time: the previous comp_unit_die was stored in
7610 comp_unit_obstack. But there's no data yet that we need this
7611 optimization. */
7612 if (cu->dwo_unit != NULL)
7613 rereading_dwo_cu = 1;
dee91e82
DE
7614 }
7615 else
7616 {
7617 /* If !use_existing_cu, this_cu->cu must be NULL. */
7618 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7619 new_cu.reset (new dwarf2_cu (this_cu));
7620 cu = new_cu.get ();
42e7ad6c 7621 }
dee91e82 7622
b0c7bfa9 7623 /* Get the header. */
9c541725 7624 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7625 {
7626 /* We already have the header, there's no need to read it in again. */
9c541725 7627 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7628 }
7629 else
7630 {
3019eac3 7631 if (this_cu->is_debug_types)
dee91e82 7632 {
ed2dc618
SM
7633 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7634 &cu->header, section,
4bdcc0c1 7635 abbrev_section, info_ptr,
43988095 7636 rcuh_kind::TYPE);
dee91e82 7637
42e7ad6c
DE
7638 /* Since per_cu is the first member of struct signatured_type,
7639 we can go from a pointer to one to a pointer to the other. */
7640 sig_type = (struct signatured_type *) this_cu;
43988095 7641 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7642 gdb_assert (sig_type->type_offset_in_tu
7643 == cu->header.type_cu_offset_in_tu);
7644 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7645
42e7ad6c
DE
7646 /* LENGTH has not been set yet for type units if we're
7647 using .gdb_index. */
1ce1cefd 7648 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7649
7650 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7651 sig_type->type_offset_in_section =
7652 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7653
7654 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7655 }
7656 else
7657 {
ed2dc618
SM
7658 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7659 &cu->header, section,
4bdcc0c1 7660 abbrev_section,
43988095
JK
7661 info_ptr,
7662 rcuh_kind::COMPILE);
dee91e82 7663
9c541725 7664 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7665 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7666 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7667 }
7668 }
10b3939b 7669
6caca83c 7670 /* Skip dummy compilation units. */
dee91e82 7671 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7672 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7673 return;
6caca83c 7674
433df2d4
DE
7675 /* If we don't have them yet, read the abbrevs for this compilation unit.
7676 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7677 done (own the table through ABBREV_TABLE_HOLDER). */
7678 abbrev_table_up abbrev_table_holder;
f4dc4d17 7679 if (abbrev_table != NULL)
685af9cd
TT
7680 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7681 else
f4dc4d17 7682 {
685af9cd
TT
7683 abbrev_table_holder
7684 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7685 cu->header.abbrev_sect_off);
7686 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7687 }
af703f96 7688
dee91e82 7689 /* Read the top level CU/TU die. */
685af9cd 7690 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7691 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7692
58f0c718
TT
7693 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7694 return;
7695
b0c7bfa9 7696 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7697 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7698 table from the DWO file and pass the ownership over to us. It will be
7699 referenced from READER, so we must make sure to free it after we're done
7700 with READER.
7701
b0c7bfa9
DE
7702 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7703 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7704 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7705 abbrev_table_up dwo_abbrev_table;
a084a2a6 7706 if (dwo_name != nullptr)
3019eac3 7707 {
3019eac3 7708 struct dwo_unit *dwo_unit;
b0c7bfa9 7709 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7710
7711 if (has_children)
6a506a2d 7712 {
b98664d3 7713 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7714 " has children (offset %s) [in module %s]"),
7715 sect_offset_str (this_cu->sect_off),
7716 bfd_get_filename (abfd));
6a506a2d 7717 }
b0c7bfa9 7718 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7719 if (dwo_unit != NULL)
3019eac3 7720 {
6a506a2d 7721 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7722 comp_unit_die, NULL,
6a506a2d 7723 &reader, &info_ptr,
685af9cd
TT
7724 &dwo_comp_unit_die, &has_children,
7725 &dwo_abbrev_table) == 0)
6a506a2d
DE
7726 {
7727 /* Dummy die. */
6a506a2d
DE
7728 return;
7729 }
7730 comp_unit_die = dwo_comp_unit_die;
7731 }
7732 else
7733 {
7734 /* Yikes, we couldn't find the rest of the DIE, we only have
7735 the stub. A complaint has already been logged. There's
7736 not much more we can do except pass on the stub DIE to
7737 die_reader_func. We don't want to throw an error on bad
7738 debug info. */
3019eac3
DE
7739 }
7740 }
7741
b0c7bfa9 7742 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7743 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7744
b0c7bfa9 7745 /* Done, clean up. */
fcd3b13d 7746 if (new_cu != NULL && keep)
348e048f 7747 {
fcd3b13d
SM
7748 /* Link this CU into read_in_chain. */
7749 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7750 dwarf2_per_objfile->read_in_chain = this_cu;
7751 /* The chain owns it now. */
7752 new_cu.release ();
348e048f 7753 }
dee91e82
DE
7754}
7755
33e80786
DE
7756/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7757 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7758 to have already done the lookup to find the DWO file).
dee91e82
DE
7759
7760 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7761 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7762
7763 We fill in THIS_CU->length.
7764
7765 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7766 linker) then DIE_READER_FUNC will not get called.
7767
7768 THIS_CU->cu is always freed when done.
3019eac3
DE
7769 This is done in order to not leave THIS_CU->cu in a state where we have
7770 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7771
7772static void
7773init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7774 struct dwo_file *dwo_file,
dee91e82
DE
7775 die_reader_func_ftype *die_reader_func,
7776 void *data)
7777{
ed2dc618 7778 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7779 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7780 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7781 bfd *abfd = get_section_bfd_owner (section);
33e80786 7782 struct dwarf2_section_info *abbrev_section;
d521ce57 7783 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7784 struct die_reader_specs reader;
dee91e82
DE
7785 struct die_info *comp_unit_die;
7786 int has_children;
7787
b4f54984 7788 if (dwarf_die_debug)
9d8780f0 7789 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7790 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7791 sect_offset_str (this_cu->sect_off));
09406207 7792
dee91e82
DE
7793 gdb_assert (this_cu->cu == NULL);
7794
33e80786
DE
7795 abbrev_section = (dwo_file != NULL
7796 ? &dwo_file->sections.abbrev
7797 : get_abbrev_section_for_cu (this_cu));
7798
dee91e82
DE
7799 /* This is cheap if the section is already read in. */
7800 dwarf2_read_section (objfile, section);
7801
fcd3b13d 7802 struct dwarf2_cu cu (this_cu);
dee91e82 7803
9c541725 7804 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7805 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7806 &cu.header, section,
4bdcc0c1 7807 abbrev_section, info_ptr,
43988095
JK
7808 (this_cu->is_debug_types
7809 ? rcuh_kind::TYPE
7810 : rcuh_kind::COMPILE));
dee91e82 7811
1ce1cefd 7812 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7813
7814 /* Skip dummy compilation units. */
7815 if (info_ptr >= begin_info_ptr + this_cu->length
7816 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7817 return;
72bf9492 7818
685af9cd
TT
7819 abbrev_table_up abbrev_table
7820 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7821 cu.header.abbrev_sect_off);
dee91e82 7822
685af9cd 7823 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7824 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7825
7826 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7827}
7828
3019eac3
DE
7829/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7830 does not lookup the specified DWO file.
7831 This cannot be used to read DWO files.
dee91e82
DE
7832
7833 THIS_CU->cu is always freed when done.
3019eac3
DE
7834 This is done in order to not leave THIS_CU->cu in a state where we have
7835 to care whether it refers to the "main" CU or the DWO CU.
7836 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7837
7838static void
7839init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7840 die_reader_func_ftype *die_reader_func,
7841 void *data)
7842{
33e80786 7843 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7844}
0018ea6f
DE
7845\f
7846/* Type Unit Groups.
dee91e82 7847
0018ea6f
DE
7848 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7849 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7850 so that all types coming from the same compilation (.o file) are grouped
7851 together. A future step could be to put the types in the same symtab as
7852 the CU the types ultimately came from. */
ff013f42 7853
f4dc4d17
DE
7854static hashval_t
7855hash_type_unit_group (const void *item)
7856{
9a3c8263
SM
7857 const struct type_unit_group *tu_group
7858 = (const struct type_unit_group *) item;
f4dc4d17 7859
094b34ac 7860 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7861}
348e048f
DE
7862
7863static int
f4dc4d17 7864eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7865{
9a3c8263
SM
7866 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7867 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7868
094b34ac 7869 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7870}
348e048f 7871
f4dc4d17
DE
7872/* Allocate a hash table for type unit groups. */
7873
7874static htab_t
ed2dc618 7875allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7876{
7877 return htab_create_alloc_ex (3,
7878 hash_type_unit_group,
7879 eq_type_unit_group,
7880 NULL,
ed2dc618 7881 &objfile->objfile_obstack,
f4dc4d17
DE
7882 hashtab_obstack_allocate,
7883 dummy_obstack_deallocate);
7884}
dee91e82 7885
f4dc4d17
DE
7886/* Type units that don't have DW_AT_stmt_list are grouped into their own
7887 partial symtabs. We combine several TUs per psymtab to not let the size
7888 of any one psymtab grow too big. */
7889#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7890#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7891
094b34ac 7892/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7893 Create the type_unit_group object used to hold one or more TUs. */
7894
7895static struct type_unit_group *
094b34ac 7896create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7897{
518817b3
SM
7898 struct dwarf2_per_objfile *dwarf2_per_objfile
7899 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7900 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7901 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7902 struct type_unit_group *tu_group;
f4dc4d17
DE
7903
7904 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7905 struct type_unit_group);
094b34ac 7906 per_cu = &tu_group->per_cu;
518817b3 7907 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7908
094b34ac
DE
7909 if (dwarf2_per_objfile->using_index)
7910 {
7911 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7912 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7913 }
7914 else
7915 {
9c541725 7916 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7917 struct partial_symtab *pst;
528e1572 7918 std::string name;
094b34ac
DE
7919
7920 /* Give the symtab a useful name for debug purposes. */
7921 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7922 name = string_printf ("<type_units_%d>",
7923 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7924 else
528e1572 7925 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7926
528e1572 7927 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7928 pst->anonymous = 1;
094b34ac 7929 }
f4dc4d17 7930
094b34ac 7931 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7932 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7933
7934 return tu_group;
7935}
7936
094b34ac
DE
7937/* Look up the type_unit_group for type unit CU, and create it if necessary.
7938 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7939
7940static struct type_unit_group *
ff39bb5e 7941get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7942{
518817b3
SM
7943 struct dwarf2_per_objfile *dwarf2_per_objfile
7944 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7945 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7946 struct type_unit_group *tu_group;
7947 void **slot;
7948 unsigned int line_offset;
7949 struct type_unit_group type_unit_group_for_lookup;
7950
7951 if (dwarf2_per_objfile->type_unit_groups == NULL)
7952 {
7953 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7954 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7955 }
7956
7957 /* Do we need to create a new group, or can we use an existing one? */
7958
7959 if (stmt_list)
7960 {
7961 line_offset = DW_UNSND (stmt_list);
7962 ++tu_stats->nr_symtab_sharers;
7963 }
7964 else
7965 {
7966 /* Ugh, no stmt_list. Rare, but we have to handle it.
7967 We can do various things here like create one group per TU or
7968 spread them over multiple groups to split up the expansion work.
7969 To avoid worst case scenarios (too many groups or too large groups)
7970 we, umm, group them in bunches. */
7971 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7972 | (tu_stats->nr_stmt_less_type_units
7973 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7974 ++tu_stats->nr_stmt_less_type_units;
7975 }
7976
094b34ac 7977 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7978 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7979 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7980 &type_unit_group_for_lookup, INSERT);
7981 if (*slot != NULL)
7982 {
9a3c8263 7983 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7984 gdb_assert (tu_group != NULL);
7985 }
7986 else
7987 {
9c541725 7988 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7989 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7990 *slot = tu_group;
7991 ++tu_stats->nr_symtabs;
7992 }
7993
7994 return tu_group;
7995}
0018ea6f
DE
7996\f
7997/* Partial symbol tables. */
7998
7999/* Create a psymtab named NAME and assign it to PER_CU.
8000
8001 The caller must fill in the following details:
8002 dirname, textlow, texthigh. */
8003
8004static struct partial_symtab *
8005create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8006{
e3b94546 8007 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8008 struct partial_symtab *pst;
8009
939652a5 8010 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
8011
8012 pst->psymtabs_addrmap_supported = 1;
8013
8014 /* This is the glue that links PST into GDB's symbol API. */
8015 pst->read_symtab_private = per_cu;
8016 pst->read_symtab = dwarf2_read_symtab;
8017 per_cu->v.psymtab = pst;
8018
8019 return pst;
8020}
8021
b93601f3
TT
8022/* The DATA object passed to process_psymtab_comp_unit_reader has this
8023 type. */
8024
8025struct process_psymtab_comp_unit_data
8026{
8027 /* True if we are reading a DW_TAG_partial_unit. */
8028
8029 int want_partial_unit;
8030
8031 /* The "pretend" language that is used if the CU doesn't declare a
8032 language. */
8033
8034 enum language pretend_language;
8035};
8036
0018ea6f
DE
8037/* die_reader_func for process_psymtab_comp_unit. */
8038
8039static void
8040process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8041 const gdb_byte *info_ptr,
0018ea6f
DE
8042 struct die_info *comp_unit_die,
8043 int has_children,
8044 void *data)
8045{
8046 struct dwarf2_cu *cu = reader->cu;
518817b3 8047 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8048 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8049 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8050 CORE_ADDR baseaddr;
8051 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8052 struct partial_symtab *pst;
3a2b436a 8053 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8054 const char *filename;
9a3c8263
SM
8055 struct process_psymtab_comp_unit_data *info
8056 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8057
b93601f3 8058 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8059 return;
8060
8061 gdb_assert (! per_cu->is_debug_types);
8062
b93601f3 8063 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8064
0018ea6f 8065 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8066 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8067 if (filename == NULL)
0018ea6f 8068 filename = "";
0018ea6f
DE
8069
8070 pst = create_partial_symtab (per_cu, filename);
8071
8072 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8073 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8074
8075 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8076
8077 dwarf2_find_base_address (comp_unit_die, cu);
8078
8079 /* Possibly set the default values of LOWPC and HIGHPC from
8080 `DW_AT_ranges'. */
3a2b436a
JK
8081 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8082 &best_highpc, cu, pst);
8083 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8084 {
8085 CORE_ADDR low
8086 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8087 - baseaddr);
8088 CORE_ADDR high
8089 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8090 - baseaddr - 1);
8091 /* Store the contiguous range if it is not empty; it can be
8092 empty for CUs with no code. */
d320c2b5
TT
8093 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8094 low, high, pst);
79748972 8095 }
0018ea6f
DE
8096
8097 /* Check if comp unit has_children.
8098 If so, read the rest of the partial symbols from this comp unit.
8099 If not, there's no more debug_info for this comp unit. */
8100 if (has_children)
8101 {
8102 struct partial_die_info *first_die;
8103 CORE_ADDR lowpc, highpc;
8104
8105 lowpc = ((CORE_ADDR) -1);
8106 highpc = ((CORE_ADDR) 0);
8107
8108 first_die = load_partial_dies (reader, info_ptr, 1);
8109
8110 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8111 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8112
8113 /* If we didn't find a lowpc, set it to highpc to avoid
8114 complaints from `maint check'. */
8115 if (lowpc == ((CORE_ADDR) -1))
8116 lowpc = highpc;
8117
8118 /* If the compilation unit didn't have an explicit address range,
8119 then use the information extracted from its child dies. */
e385593e 8120 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8121 {
8122 best_lowpc = lowpc;
8123 best_highpc = highpc;
8124 }
8125 }
4ae976d1 8126 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8127 best_lowpc + baseaddr)
8128 - baseaddr);
4ae976d1 8129 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8130 best_highpc + baseaddr)
8131 - baseaddr);
0018ea6f 8132
8763cede 8133 end_psymtab_common (objfile, pst);
0018ea6f 8134
ae640021 8135 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8136 {
8137 int i;
ae640021 8138 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8139
8140 /* Fill in 'dependencies' here; we fill in 'users' in a
8141 post-pass. */
8142 pst->number_of_dependencies = len;
a9342b62
TT
8143 pst->dependencies
8144 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8145 for (i = 0; i < len; ++i)
8146 {
8147 pst->dependencies[i]
8148 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8149 }
0018ea6f 8150
ae640021 8151 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8152 }
8153
8154 /* Get the list of files included in the current compilation unit,
8155 and build a psymtab for each of them. */
8156 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8157
b4f54984 8158 if (dwarf_read_debug)
b926417a
TT
8159 fprintf_unfiltered (gdb_stdlog,
8160 "Psymtab for %s unit @%s: %s - %s"
8161 ", %d global, %d static syms\n",
8162 per_cu->is_debug_types ? "type" : "comp",
8163 sect_offset_str (per_cu->sect_off),
8164 paddress (gdbarch, pst->text_low (objfile)),
8165 paddress (gdbarch, pst->text_high (objfile)),
8166 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8167}
8168
8169/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8170 Process compilation unit THIS_CU for a psymtab. */
8171
8172static void
8173process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8174 int want_partial_unit,
8175 enum language pretend_language)
0018ea6f
DE
8176{
8177 /* If this compilation unit was already read in, free the
8178 cached copy in order to read it in again. This is
8179 necessary because we skipped some symbols when we first
8180 read in the compilation unit (see load_partial_dies).
8181 This problem could be avoided, but the benefit is unclear. */
8182 if (this_cu->cu != NULL)
8183 free_one_cached_comp_unit (this_cu);
8184
f1902523 8185 if (this_cu->is_debug_types)
58f0c718
TT
8186 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8187 build_type_psymtabs_reader, NULL);
f1902523
JK
8188 else
8189 {
8190 process_psymtab_comp_unit_data info;
8191 info.want_partial_unit = want_partial_unit;
8192 info.pretend_language = pretend_language;
58f0c718 8193 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8194 process_psymtab_comp_unit_reader, &info);
8195 }
0018ea6f
DE
8196
8197 /* Age out any secondary CUs. */
ed2dc618 8198 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8199}
f4dc4d17
DE
8200
8201/* Reader function for build_type_psymtabs. */
8202
8203static void
8204build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8205 const gdb_byte *info_ptr,
f4dc4d17
DE
8206 struct die_info *type_unit_die,
8207 int has_children,
8208 void *data)
8209{
ed2dc618 8210 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8211 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8212 struct objfile *objfile = dwarf2_per_objfile->objfile;
8213 struct dwarf2_cu *cu = reader->cu;
8214 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8215 struct signatured_type *sig_type;
f4dc4d17
DE
8216 struct type_unit_group *tu_group;
8217 struct attribute *attr;
8218 struct partial_die_info *first_die;
8219 CORE_ADDR lowpc, highpc;
8220 struct partial_symtab *pst;
8221
8222 gdb_assert (data == NULL);
0186c6a7
DE
8223 gdb_assert (per_cu->is_debug_types);
8224 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8225
8226 if (! has_children)
8227 return;
8228
8229 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8230 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8231
df07e2c7 8232 if (tu_group->tus == nullptr)
a8b3b8e9 8233 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8234 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8235
8236 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8237 pst = create_partial_symtab (per_cu, "");
8238 pst->anonymous = 1;
8239
8240 first_die = load_partial_dies (reader, info_ptr, 1);
8241
8242 lowpc = (CORE_ADDR) -1;
8243 highpc = (CORE_ADDR) 0;
8244 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8245
8763cede 8246 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8247}
8248
73051182
DE
8249/* Struct used to sort TUs by their abbreviation table offset. */
8250
8251struct tu_abbrev_offset
8252{
b2bdb8cf
SM
8253 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8254 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8255 {}
8256
8257 signatured_type *sig_type;
73051182
DE
8258 sect_offset abbrev_offset;
8259};
8260
484cf504 8261/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8262
484cf504
TT
8263static bool
8264sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8265 const struct tu_abbrev_offset &b)
73051182 8266{
484cf504 8267 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8268}
8269
8270/* Efficiently read all the type units.
8271 This does the bulk of the work for build_type_psymtabs.
8272
8273 The efficiency is because we sort TUs by the abbrev table they use and
8274 only read each abbrev table once. In one program there are 200K TUs
8275 sharing 8K abbrev tables.
8276
8277 The main purpose of this function is to support building the
8278 dwarf2_per_objfile->type_unit_groups table.
8279 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8280 can collapse the search space by grouping them by stmt_list.
8281 The savings can be significant, in the same program from above the 200K TUs
8282 share 8K stmt_list tables.
8283
8284 FUNC is expected to call get_type_unit_group, which will create the
8285 struct type_unit_group if necessary and add it to
8286 dwarf2_per_objfile->type_unit_groups. */
8287
8288static void
ed2dc618 8289build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8290{
73051182 8291 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8292 abbrev_table_up abbrev_table;
73051182 8293 sect_offset abbrev_offset;
73051182
DE
8294
8295 /* It's up to the caller to not call us multiple times. */
8296 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8297
b2bdb8cf 8298 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8299 return;
8300
8301 /* TUs typically share abbrev tables, and there can be way more TUs than
8302 abbrev tables. Sort by abbrev table to reduce the number of times we
8303 read each abbrev table in.
8304 Alternatives are to punt or to maintain a cache of abbrev tables.
8305 This is simpler and efficient enough for now.
8306
8307 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8308 symtab to use). Typically TUs with the same abbrev offset have the same
8309 stmt_list value too so in practice this should work well.
8310
8311 The basic algorithm here is:
8312
8313 sort TUs by abbrev table
8314 for each TU with same abbrev table:
8315 read abbrev table if first user
8316 read TU top level DIE
8317 [IWBN if DWO skeletons had DW_AT_stmt_list]
8318 call FUNC */
8319
b4f54984 8320 if (dwarf_read_debug)
73051182
DE
8321 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8322
8323 /* Sort in a separate table to maintain the order of all_type_units
8324 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8325 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8326 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8327
8328 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8329 sorted_by_abbrev.emplace_back
8330 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8331 sig_type->per_cu.section,
8332 sig_type->per_cu.sect_off));
73051182 8333
484cf504
TT
8334 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8335 sort_tu_by_abbrev_offset);
73051182 8336
9c541725 8337 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8338
b2bdb8cf 8339 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8340 {
73051182
DE
8341 /* Switch to the next abbrev table if necessary. */
8342 if (abbrev_table == NULL
b2bdb8cf 8343 || tu.abbrev_offset != abbrev_offset)
73051182 8344 {
b2bdb8cf 8345 abbrev_offset = tu.abbrev_offset;
73051182 8346 abbrev_table =
ed2dc618
SM
8347 abbrev_table_read_table (dwarf2_per_objfile,
8348 &dwarf2_per_objfile->abbrev,
73051182
DE
8349 abbrev_offset);
8350 ++tu_stats->nr_uniq_abbrev_tables;
8351 }
8352
b2bdb8cf 8353 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8354 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8355 }
6aa5f3a6 8356}
73051182 8357
6aa5f3a6
DE
8358/* Print collected type unit statistics. */
8359
8360static void
ed2dc618 8361print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8362{
8363 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8364
8365 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8366 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8367 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8368 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8369 tu_stats->nr_uniq_abbrev_tables);
8370 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8371 tu_stats->nr_symtabs);
8372 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8373 tu_stats->nr_symtab_sharers);
8374 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8375 tu_stats->nr_stmt_less_type_units);
8376 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8377 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8378}
8379
f4dc4d17
DE
8380/* Traversal function for build_type_psymtabs. */
8381
8382static int
8383build_type_psymtab_dependencies (void **slot, void *info)
8384{
ed2dc618
SM
8385 struct dwarf2_per_objfile *dwarf2_per_objfile
8386 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8387 struct objfile *objfile = dwarf2_per_objfile->objfile;
8388 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8389 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8390 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8391 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8392 int i;
8393
8394 gdb_assert (len > 0);
0186c6a7 8395 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8396
8397 pst->number_of_dependencies = len;
a9342b62 8398 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8399 for (i = 0; i < len; ++i)
f4dc4d17 8400 {
df07e2c7 8401 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8402 gdb_assert (iter->per_cu.is_debug_types);
8403 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8404 iter->type_unit_group = tu_group;
f4dc4d17
DE
8405 }
8406
df07e2c7
AB
8407 delete tu_group->tus;
8408 tu_group->tus = nullptr;
348e048f
DE
8409
8410 return 1;
8411}
8412
8413/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8414 Build partial symbol tables for the .debug_types comp-units. */
8415
8416static void
ed2dc618 8417build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8418{
ed2dc618 8419 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8420 return;
8421
ed2dc618 8422 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8423}
f4dc4d17 8424
6aa5f3a6
DE
8425/* Traversal function for process_skeletonless_type_unit.
8426 Read a TU in a DWO file and build partial symbols for it. */
8427
8428static int
8429process_skeletonless_type_unit (void **slot, void *info)
8430{
8431 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8432 struct dwarf2_per_objfile *dwarf2_per_objfile
8433 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8434 struct signatured_type find_entry, *entry;
8435
8436 /* If this TU doesn't exist in the global table, add it and read it in. */
8437
8438 if (dwarf2_per_objfile->signatured_types == NULL)
8439 {
8440 dwarf2_per_objfile->signatured_types
ed2dc618 8441 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8442 }
8443
8444 find_entry.signature = dwo_unit->signature;
8445 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8446 INSERT);
8447 /* If we've already seen this type there's nothing to do. What's happening
8448 is we're doing our own version of comdat-folding here. */
8449 if (*slot != NULL)
8450 return 1;
8451
8452 /* This does the job that create_all_type_units would have done for
8453 this TU. */
ed2dc618
SM
8454 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8455 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8456 *slot = entry;
8457
8458 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8459 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8460 build_type_psymtabs_reader, NULL);
8461
8462 return 1;
8463}
8464
8465/* Traversal function for process_skeletonless_type_units. */
8466
8467static int
8468process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8469{
8470 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8471
8472 if (dwo_file->tus != NULL)
8473 {
8474 htab_traverse_noresize (dwo_file->tus,
8475 process_skeletonless_type_unit, info);
8476 }
8477
8478 return 1;
8479}
8480
8481/* Scan all TUs of DWO files, verifying we've processed them.
8482 This is needed in case a TU was emitted without its skeleton.
8483 Note: This can't be done until we know what all the DWO files are. */
8484
8485static void
ed2dc618 8486process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8487{
8488 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8489 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8490 && dwarf2_per_objfile->dwo_files != NULL)
8491 {
51ac9db5 8492 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8493 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8494 dwarf2_per_objfile);
6aa5f3a6 8495 }
348e048f
DE
8496}
8497
ed2dc618 8498/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8499
8500static void
ed2dc618 8501set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8502{
b76e467d 8503 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8504 {
95554aad 8505 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8506
36586728
TT
8507 if (pst == NULL)
8508 continue;
8509
b76e467d 8510 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8511 {
8512 /* Set the 'user' field only if it is not already set. */
8513 if (pst->dependencies[j]->user == NULL)
8514 pst->dependencies[j]->user = pst;
8515 }
8516 }
8517}
8518
93311388
DE
8519/* Build the partial symbol table by doing a quick pass through the
8520 .debug_info and .debug_abbrev sections. */
72bf9492 8521
93311388 8522static void
ed2dc618 8523dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8524{
ed2dc618 8525 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8526
b4f54984 8527 if (dwarf_read_debug)
45cfd468
DE
8528 {
8529 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8530 objfile_name (objfile));
45cfd468
DE
8531 }
8532
98bfdba5
PA
8533 dwarf2_per_objfile->reading_partial_symbols = 1;
8534
be391dca 8535 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8536
93311388
DE
8537 /* Any cached compilation units will be linked by the per-objfile
8538 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8539 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8540
ed2dc618 8541 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8542
ed2dc618 8543 create_all_comp_units (dwarf2_per_objfile);
c906108c 8544
60606b2c
TT
8545 /* Create a temporary address map on a temporary obstack. We later
8546 copy this to the final obstack. */
8268c778 8547 auto_obstack temp_obstack;
791afaa2
TT
8548
8549 scoped_restore save_psymtabs_addrmap
d320c2b5 8550 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8551 addrmap_create_mutable (&temp_obstack));
72bf9492 8552
b76e467d
SM
8553 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8554 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8555
6aa5f3a6 8556 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8557 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8558
8559 /* Now that all TUs have been processed we can fill in the dependencies. */
8560 if (dwarf2_per_objfile->type_unit_groups != NULL)
8561 {
8562 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8563 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8564 }
8565
b4f54984 8566 if (dwarf_read_debug)
ed2dc618 8567 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8568
ed2dc618 8569 set_partial_user (dwarf2_per_objfile);
95554aad 8570
d320c2b5
TT
8571 objfile->partial_symtabs->psymtabs_addrmap
8572 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8573 objfile->partial_symtabs->obstack ());
791afaa2
TT
8574 /* At this point we want to keep the address map. */
8575 save_psymtabs_addrmap.release ();
ff013f42 8576
b4f54984 8577 if (dwarf_read_debug)
45cfd468 8578 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8579 objfile_name (objfile));
ae038cb0
DJ
8580}
8581
3019eac3 8582/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8583
8584static void
dee91e82 8585load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8586 const gdb_byte *info_ptr,
dee91e82
DE
8587 struct die_info *comp_unit_die,
8588 int has_children,
8589 void *data)
ae038cb0 8590{
dee91e82 8591 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8592
95554aad 8593 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8594
ae038cb0
DJ
8595 /* Check if comp unit has_children.
8596 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8597 If not, there's no more debug_info for this comp unit. */
d85a05f0 8598 if (has_children)
dee91e82
DE
8599 load_partial_dies (reader, info_ptr, 0);
8600}
98bfdba5 8601
dee91e82
DE
8602/* Load the partial DIEs for a secondary CU into memory.
8603 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8604
dee91e82
DE
8605static void
8606load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8607{
58f0c718 8608 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8609 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8610}
8611
ae038cb0 8612static void
ed2dc618 8613read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8614 struct dwarf2_section_info *section,
f1902523 8615 struct dwarf2_section_info *abbrev_section,
b76e467d 8616 unsigned int is_dwz)
ae038cb0 8617{
d521ce57 8618 const gdb_byte *info_ptr;
ed2dc618 8619 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8620
b4f54984 8621 if (dwarf_read_debug)
bf6af496 8622 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8623 get_section_name (section),
8624 get_section_file_name (section));
bf6af496 8625
36586728 8626 dwarf2_read_section (objfile, section);
ae038cb0 8627
36586728 8628 info_ptr = section->buffer;
6e70227d 8629
36586728 8630 while (info_ptr < section->buffer + section->size)
ae038cb0 8631 {
ae038cb0 8632 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8633
9c541725 8634 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8635
f1902523 8636 comp_unit_head cu_header;
ed2dc618
SM
8637 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8638 abbrev_section, info_ptr,
8639 rcuh_kind::COMPILE);
ae038cb0
DJ
8640
8641 /* Save the compilation unit for later lookup. */
f1902523
JK
8642 if (cu_header.unit_type != DW_UT_type)
8643 {
8644 this_cu = XOBNEW (&objfile->objfile_obstack,
8645 struct dwarf2_per_cu_data);
8646 memset (this_cu, 0, sizeof (*this_cu));
8647 }
8648 else
8649 {
8650 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8651 struct signatured_type);
8652 memset (sig_type, 0, sizeof (*sig_type));
8653 sig_type->signature = cu_header.signature;
8654 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8655 this_cu = &sig_type->per_cu;
8656 }
8657 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8658 this_cu->sect_off = sect_off;
f1902523 8659 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8660 this_cu->is_dwz = is_dwz;
e3b94546 8661 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8662 this_cu->section = section;
ae038cb0 8663
b76e467d 8664 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8665
8666 info_ptr = info_ptr + this_cu->length;
8667 }
36586728
TT
8668}
8669
8670/* Create a list of all compilation units in OBJFILE.
8671 This is only done for -readnow and building partial symtabs. */
8672
8673static void
ed2dc618 8674create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8675{
b76e467d 8676 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8677 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8678 &dwarf2_per_objfile->abbrev, 0);
36586728 8679
b76e467d 8680 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8681 if (dwz != NULL)
ed2dc618 8682 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8683 1);
c906108c
SS
8684}
8685
5734ee8b 8686/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8687 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8688 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8689 DW_AT_ranges). See the comments of add_partial_subprogram on how
8690 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8691
72bf9492
DJ
8692static void
8693scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8694 CORE_ADDR *highpc, int set_addrmap,
8695 struct dwarf2_cu *cu)
c906108c 8696{
72bf9492 8697 struct partial_die_info *pdi;
c906108c 8698
91c24f0a
DC
8699 /* Now, march along the PDI's, descending into ones which have
8700 interesting children but skipping the children of the other ones,
8701 until we reach the end of the compilation unit. */
c906108c 8702
72bf9492 8703 pdi = first_die;
91c24f0a 8704
72bf9492
DJ
8705 while (pdi != NULL)
8706 {
52356b79 8707 pdi->fixup (cu);
c906108c 8708
f55ee35c 8709 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8710 children, so we need to look at them. Ditto for anonymous
8711 enums. */
933c6fe4 8712
72bf9492 8713 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8714 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8715 || pdi->tag == DW_TAG_imported_unit
8716 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8717 {
72bf9492 8718 switch (pdi->tag)
c906108c
SS
8719 {
8720 case DW_TAG_subprogram:
b1dc1806 8721 case DW_TAG_inlined_subroutine:
cdc07690 8722 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8723 break;
72929c62 8724 case DW_TAG_constant:
c906108c
SS
8725 case DW_TAG_variable:
8726 case DW_TAG_typedef:
91c24f0a 8727 case DW_TAG_union_type:
72bf9492 8728 if (!pdi->is_declaration)
63d06c5c 8729 {
72bf9492 8730 add_partial_symbol (pdi, cu);
63d06c5c
DC
8731 }
8732 break;
c906108c 8733 case DW_TAG_class_type:
680b30c7 8734 case DW_TAG_interface_type:
c906108c 8735 case DW_TAG_structure_type:
72bf9492 8736 if (!pdi->is_declaration)
c906108c 8737 {
72bf9492 8738 add_partial_symbol (pdi, cu);
c906108c 8739 }
b7fee5a3
KS
8740 if ((cu->language == language_rust
8741 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8742 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8743 set_addrmap, cu);
c906108c 8744 break;
91c24f0a 8745 case DW_TAG_enumeration_type:
72bf9492
DJ
8746 if (!pdi->is_declaration)
8747 add_partial_enumeration (pdi, cu);
c906108c
SS
8748 break;
8749 case DW_TAG_base_type:
a02abb62 8750 case DW_TAG_subrange_type:
c906108c 8751 /* File scope base type definitions are added to the partial
c5aa993b 8752 symbol table. */
72bf9492 8753 add_partial_symbol (pdi, cu);
c906108c 8754 break;
d9fa45fe 8755 case DW_TAG_namespace:
cdc07690 8756 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8757 break;
5d7cb8df 8758 case DW_TAG_module:
59c35742
AB
8759 if (!pdi->is_declaration)
8760 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8761 break;
95554aad
TT
8762 case DW_TAG_imported_unit:
8763 {
8764 struct dwarf2_per_cu_data *per_cu;
8765
f4dc4d17
DE
8766 /* For now we don't handle imported units in type units. */
8767 if (cu->per_cu->is_debug_types)
8768 {
8769 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8770 " supported in type units [in module %s]"),
518817b3 8771 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8772 }
8773
e3b94546
SM
8774 per_cu = dwarf2_find_containing_comp_unit
8775 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8776 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8777
8778 /* Go read the partial unit, if needed. */
8779 if (per_cu->v.psymtab == NULL)
b93601f3 8780 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8781
ae640021 8782 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8783 }
8784 break;
74921315
KS
8785 case DW_TAG_imported_declaration:
8786 add_partial_symbol (pdi, cu);
8787 break;
c906108c
SS
8788 default:
8789 break;
8790 }
8791 }
8792
72bf9492
DJ
8793 /* If the die has a sibling, skip to the sibling. */
8794
8795 pdi = pdi->die_sibling;
8796 }
8797}
8798
8799/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8800
72bf9492 8801 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8802 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8803 Enumerators are an exception; they use the scope of their parent
8804 enumeration type, i.e. the name of the enumeration type is not
8805 prepended to the enumerator.
91c24f0a 8806
72bf9492
DJ
8807 There are two complexities. One is DW_AT_specification; in this
8808 case "parent" means the parent of the target of the specification,
8809 instead of the direct parent of the DIE. The other is compilers
8810 which do not emit DW_TAG_namespace; in this case we try to guess
8811 the fully qualified name of structure types from their members'
8812 linkage names. This must be done using the DIE's children rather
8813 than the children of any DW_AT_specification target. We only need
8814 to do this for structures at the top level, i.e. if the target of
8815 any DW_AT_specification (if any; otherwise the DIE itself) does not
8816 have a parent. */
8817
8818/* Compute the scope prefix associated with PDI's parent, in
8819 compilation unit CU. The result will be allocated on CU's
8820 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8821 field. NULL is returned if no prefix is necessary. */
15d034d0 8822static const char *
72bf9492
DJ
8823partial_die_parent_scope (struct partial_die_info *pdi,
8824 struct dwarf2_cu *cu)
8825{
15d034d0 8826 const char *grandparent_scope;
72bf9492 8827 struct partial_die_info *parent, *real_pdi;
91c24f0a 8828
72bf9492
DJ
8829 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8830 then this means the parent of the specification DIE. */
8831
8832 real_pdi = pdi;
72bf9492 8833 while (real_pdi->has_specification)
fb816e8b 8834 {
122cf0f2
AB
8835 auto res = find_partial_die (real_pdi->spec_offset,
8836 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8837 real_pdi = res.pdi;
8838 cu = res.cu;
8839 }
72bf9492
DJ
8840
8841 parent = real_pdi->die_parent;
8842 if (parent == NULL)
8843 return NULL;
8844
8845 if (parent->scope_set)
8846 return parent->scope;
8847
52356b79 8848 parent->fixup (cu);
72bf9492 8849
10b3939b 8850 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8851
acebe513
UW
8852 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8853 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8854 Work around this problem here. */
8855 if (cu->language == language_cplus
6e70227d 8856 && parent->tag == DW_TAG_namespace
acebe513
UW
8857 && strcmp (parent->name, "::") == 0
8858 && grandparent_scope == NULL)
8859 {
8860 parent->scope = NULL;
8861 parent->scope_set = 1;
8862 return NULL;
8863 }
8864
0a4b0913 8865 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8866 if (pdi->tag == DW_TAG_enumerator)
8867 /* Enumerators should not get the name of the enumeration as a prefix. */
8868 parent->scope = grandparent_scope;
8869 else if (parent->tag == DW_TAG_namespace
f55ee35c 8870 || parent->tag == DW_TAG_module
72bf9492
DJ
8871 || parent->tag == DW_TAG_structure_type
8872 || parent->tag == DW_TAG_class_type
680b30c7 8873 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8874 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8875 || parent->tag == DW_TAG_enumeration_type
8876 || (cu->language == language_fortran
8877 && parent->tag == DW_TAG_subprogram
8878 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8879 {
8880 if (grandparent_scope == NULL)
8881 parent->scope = parent->name;
8882 else
3e43a32a
MS
8883 parent->scope = typename_concat (&cu->comp_unit_obstack,
8884 grandparent_scope,
f55ee35c 8885 parent->name, 0, cu);
72bf9492 8886 }
72bf9492
DJ
8887 else
8888 {
8889 /* FIXME drow/2004-04-01: What should we be doing with
8890 function-local names? For partial symbols, we should probably be
8891 ignoring them. */
fa9c3fa0
TT
8892 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8893 dwarf_tag_name (parent->tag),
8894 sect_offset_str (pdi->sect_off));
72bf9492 8895 parent->scope = grandparent_scope;
c906108c
SS
8896 }
8897
72bf9492
DJ
8898 parent->scope_set = 1;
8899 return parent->scope;
8900}
8901
8902/* Return the fully scoped name associated with PDI, from compilation unit
8903 CU. The result will be allocated with malloc. */
4568ecf9 8904
43816ebc 8905static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8906partial_die_full_name (struct partial_die_info *pdi,
8907 struct dwarf2_cu *cu)
8908{
15d034d0 8909 const char *parent_scope;
72bf9492 8910
98bfdba5
PA
8911 /* If this is a template instantiation, we can not work out the
8912 template arguments from partial DIEs. So, unfortunately, we have
8913 to go through the full DIEs. At least any work we do building
8914 types here will be reused if full symbols are loaded later. */
8915 if (pdi->has_template_arguments)
8916 {
52356b79 8917 pdi->fixup (cu);
98bfdba5
PA
8918
8919 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8920 {
8921 struct die_info *die;
8922 struct attribute attr;
8923 struct dwarf2_cu *ref_cu = cu;
8924
b64f50a1 8925 /* DW_FORM_ref_addr is using section offset. */
b4069958 8926 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8927 attr.form = DW_FORM_ref_addr;
9c541725 8928 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8929 die = follow_die_ref (NULL, &attr, &ref_cu);
8930
43816ebc 8931 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8932 }
8933 }
8934
72bf9492
DJ
8935 parent_scope = partial_die_parent_scope (pdi, cu);
8936 if (parent_scope == NULL)
8937 return NULL;
8938 else
43816ebc
TT
8939 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8940 pdi->name, 0, cu));
c906108c
SS
8941}
8942
8943static void
72bf9492 8944add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8945{
518817b3
SM
8946 struct dwarf2_per_objfile *dwarf2_per_objfile
8947 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8948 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8949 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8950 CORE_ADDR addr = 0;
15d034d0 8951 const char *actual_name = NULL;
e142c38c
DJ
8952 CORE_ADDR baseaddr;
8953
8954 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8955
43816ebc
TT
8956 gdb::unique_xmalloc_ptr<char> built_actual_name
8957 = partial_die_full_name (pdi, cu);
15d034d0 8958 if (built_actual_name != NULL)
43816ebc 8959 actual_name = built_actual_name.get ();
63d06c5c 8960
72bf9492
DJ
8961 if (actual_name == NULL)
8962 actual_name = pdi->name;
8963
c906108c
SS
8964 switch (pdi->tag)
8965 {
b1dc1806 8966 case DW_TAG_inlined_subroutine:
c906108c 8967 case DW_TAG_subprogram:
79748972
TT
8968 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8969 - baseaddr);
0a4b0913
AB
8970 if (pdi->is_external
8971 || cu->language == language_ada
8972 || (cu->language == language_fortran
8973 && pdi->die_parent != NULL
8974 && pdi->die_parent->tag == DW_TAG_subprogram))
8975 {
8976 /* Normally, only "external" DIEs are part of the global scope.
8977 But in Ada and Fortran, we want to be able to access nested
8978 procedures globally. So all Ada and Fortran subprograms are
8979 stored in the global scope. */
31edb802 8980 add_psymbol_to_list (actual_name,
15d034d0 8981 built_actual_name != NULL,
f47fb265 8982 VAR_DOMAIN, LOC_BLOCK,
79748972 8983 SECT_OFF_TEXT (objfile),
75aedd27 8984 psymbol_placement::GLOBAL,
79748972
TT
8985 addr,
8986 cu->language, objfile);
c906108c
SS
8987 }
8988 else
8989 {
31edb802 8990 add_psymbol_to_list (actual_name,
15d034d0 8991 built_actual_name != NULL,
f47fb265 8992 VAR_DOMAIN, LOC_BLOCK,
79748972 8993 SECT_OFF_TEXT (objfile),
75aedd27 8994 psymbol_placement::STATIC,
1762568f 8995 addr, cu->language, objfile);
c906108c 8996 }
0c1b455e
TT
8997
8998 if (pdi->main_subprogram && actual_name != NULL)
8999 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9000 break;
72929c62 9001 case DW_TAG_constant:
31edb802 9002 add_psymbol_to_list (actual_name,
75aedd27
TT
9003 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9004 -1, (pdi->is_external
9005 ? psymbol_placement::GLOBAL
9006 : psymbol_placement::STATIC),
9007 0, cu->language, objfile);
72929c62 9008 break;
c906108c 9009 case DW_TAG_variable:
95554aad
TT
9010 if (pdi->d.locdesc)
9011 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9012
95554aad 9013 if (pdi->d.locdesc
caac4577
JG
9014 && addr == 0
9015 && !dwarf2_per_objfile->has_section_at_zero)
9016 {
9017 /* A global or static variable may also have been stripped
9018 out by the linker if unused, in which case its address
9019 will be nullified; do not add such variables into partial
9020 symbol table then. */
9021 }
9022 else if (pdi->is_external)
c906108c
SS
9023 {
9024 /* Global Variable.
9025 Don't enter into the minimal symbol tables as there is
9026 a minimal symbol table entry from the ELF symbols already.
9027 Enter into partial symbol table if it has a location
9028 descriptor or a type.
9029 If the location descriptor is missing, new_symbol will create
9030 a LOC_UNRESOLVED symbol, the address of the variable will then
9031 be determined from the minimal symbol table whenever the variable
9032 is referenced.
9033 The address for the partial symbol table entry is not
9034 used by GDB, but it comes in handy for debugging partial symbol
9035 table building. */
9036
95554aad 9037 if (pdi->d.locdesc || pdi->has_type)
31edb802 9038 add_psymbol_to_list (actual_name,
15d034d0 9039 built_actual_name != NULL,
f47fb265 9040 VAR_DOMAIN, LOC_STATIC,
79748972 9041 SECT_OFF_TEXT (objfile),
75aedd27 9042 psymbol_placement::GLOBAL,
79748972 9043 addr, cu->language, objfile);
c906108c
SS
9044 }
9045 else
9046 {
ff908ebf
AW
9047 int has_loc = pdi->d.locdesc != NULL;
9048
9049 /* Static Variable. Skip symbols whose value we cannot know (those
9050 without location descriptors or constant values). */
9051 if (!has_loc && !pdi->has_const_value)
43816ebc 9052 return;
ff908ebf 9053
31edb802 9054 add_psymbol_to_list (actual_name,
15d034d0 9055 built_actual_name != NULL,
f47fb265 9056 VAR_DOMAIN, LOC_STATIC,
79748972 9057 SECT_OFF_TEXT (objfile),
75aedd27 9058 psymbol_placement::STATIC,
79748972 9059 has_loc ? addr : 0,
f47fb265 9060 cu->language, objfile);
c906108c
SS
9061 }
9062 break;
9063 case DW_TAG_typedef:
9064 case DW_TAG_base_type:
a02abb62 9065 case DW_TAG_subrange_type:
31edb802 9066 add_psymbol_to_list (actual_name,
15d034d0 9067 built_actual_name != NULL,
79748972 9068 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9069 psymbol_placement::STATIC,
1762568f 9070 0, cu->language, objfile);
c906108c 9071 break;
74921315 9072 case DW_TAG_imported_declaration:
72bf9492 9073 case DW_TAG_namespace:
31edb802 9074 add_psymbol_to_list (actual_name,
15d034d0 9075 built_actual_name != NULL,
79748972 9076 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9077 psymbol_placement::GLOBAL,
1762568f 9078 0, cu->language, objfile);
72bf9492 9079 break;
530e8392 9080 case DW_TAG_module:
a5fd13a9
BH
9081 /* With Fortran 77 there might be a "BLOCK DATA" module
9082 available without any name. If so, we skip the module as it
9083 doesn't bring any value. */
9084 if (actual_name != nullptr)
31edb802 9085 add_psymbol_to_list (actual_name,
a5fd13a9
BH
9086 built_actual_name != NULL,
9087 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9088 psymbol_placement::GLOBAL,
9089 0, cu->language, objfile);
530e8392 9090 break;
c906108c 9091 case DW_TAG_class_type:
680b30c7 9092 case DW_TAG_interface_type:
c906108c
SS
9093 case DW_TAG_structure_type:
9094 case DW_TAG_union_type:
9095 case DW_TAG_enumeration_type:
fa4028e9
JB
9096 /* Skip external references. The DWARF standard says in the section
9097 about "Structure, Union, and Class Type Entries": "An incomplete
9098 structure, union or class type is represented by a structure,
9099 union or class entry that does not have a byte size attribute
9100 and that has a DW_AT_declaration attribute." */
9101 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 9102 return;
fa4028e9 9103
63d06c5c
DC
9104 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9105 static vs. global. */
31edb802 9106 add_psymbol_to_list (actual_name,
15d034d0 9107 built_actual_name != NULL,
79748972 9108 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9109 cu->language == language_cplus
75aedd27
TT
9110 ? psymbol_placement::GLOBAL
9111 : psymbol_placement::STATIC,
1762568f 9112 0, cu->language, objfile);
c906108c 9113
c906108c
SS
9114 break;
9115 case DW_TAG_enumerator:
31edb802 9116 add_psymbol_to_list (actual_name,
15d034d0 9117 built_actual_name != NULL,
79748972 9118 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9119 cu->language == language_cplus
75aedd27
TT
9120 ? psymbol_placement::GLOBAL
9121 : psymbol_placement::STATIC,
1762568f 9122 0, cu->language, objfile);
c906108c
SS
9123 break;
9124 default:
9125 break;
9126 }
9127}
9128
5c4e30ca
DC
9129/* Read a partial die corresponding to a namespace; also, add a symbol
9130 corresponding to that namespace to the symbol table. NAMESPACE is
9131 the name of the enclosing namespace. */
91c24f0a 9132
72bf9492
DJ
9133static void
9134add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9135 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9136 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9137{
72bf9492 9138 /* Add a symbol for the namespace. */
e7c27a73 9139
72bf9492 9140 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9141
9142 /* Now scan partial symbols in that namespace. */
9143
91c24f0a 9144 if (pdi->has_children)
cdc07690 9145 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9146}
9147
5d7cb8df
JK
9148/* Read a partial die corresponding to a Fortran module. */
9149
9150static void
9151add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9152 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9153{
530e8392
KB
9154 /* Add a symbol for the namespace. */
9155
9156 add_partial_symbol (pdi, cu);
9157
f55ee35c 9158 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9159
9160 if (pdi->has_children)
cdc07690 9161 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9162}
9163
b1dc1806
XR
9164/* Read a partial die corresponding to a subprogram or an inlined
9165 subprogram and create a partial symbol for that subprogram.
9166 When the CU language allows it, this routine also defines a partial
9167 symbol for each nested subprogram that this subprogram contains.
9168 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9169 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9170
cdc07690
YQ
9171 PDI may also be a lexical block, in which case we simply search
9172 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9173 Again, this is only performed when the CU language allows this
9174 type of definitions. */
9175
9176static void
9177add_partial_subprogram (struct partial_die_info *pdi,
9178 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9179 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9180{
b1dc1806 9181 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9182 {
9183 if (pdi->has_pc_info)
9184 {
9185 if (pdi->lowpc < *lowpc)
9186 *lowpc = pdi->lowpc;
9187 if (pdi->highpc > *highpc)
9188 *highpc = pdi->highpc;
cdc07690 9189 if (set_addrmap)
5734ee8b 9190 {
518817b3 9191 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9192 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9193 CORE_ADDR baseaddr;
b926417a
TT
9194 CORE_ADDR this_highpc;
9195 CORE_ADDR this_lowpc;
5734ee8b
DJ
9196
9197 baseaddr = ANOFFSET (objfile->section_offsets,
9198 SECT_OFF_TEXT (objfile));
b926417a
TT
9199 this_lowpc
9200 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9201 pdi->lowpc + baseaddr)
9202 - baseaddr);
9203 this_highpc
9204 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9205 pdi->highpc + baseaddr)
9206 - baseaddr);
d320c2b5 9207 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9208 this_lowpc, this_highpc - 1,
9291a0cd 9209 cu->per_cu->v.psymtab);
5734ee8b 9210 }
481860b3
GB
9211 }
9212
9213 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9214 {
bc30ff58 9215 if (!pdi->is_declaration)
e8d05480
JB
9216 /* Ignore subprogram DIEs that do not have a name, they are
9217 illegal. Do not emit a complaint at this point, we will
9218 do so when we convert this psymtab into a symtab. */
9219 if (pdi->name)
9220 add_partial_symbol (pdi, cu);
bc30ff58
JB
9221 }
9222 }
6e70227d 9223
bc30ff58
JB
9224 if (! pdi->has_children)
9225 return;
9226
0a4b0913 9227 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9228 {
9229 pdi = pdi->die_child;
9230 while (pdi != NULL)
9231 {
52356b79 9232 pdi->fixup (cu);
bc30ff58 9233 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9234 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9235 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9236 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9237 pdi = pdi->die_sibling;
9238 }
9239 }
9240}
9241
91c24f0a
DC
9242/* Read a partial die corresponding to an enumeration type. */
9243
72bf9492
DJ
9244static void
9245add_partial_enumeration (struct partial_die_info *enum_pdi,
9246 struct dwarf2_cu *cu)
91c24f0a 9247{
72bf9492 9248 struct partial_die_info *pdi;
91c24f0a
DC
9249
9250 if (enum_pdi->name != NULL)
72bf9492
DJ
9251 add_partial_symbol (enum_pdi, cu);
9252
9253 pdi = enum_pdi->die_child;
9254 while (pdi)
91c24f0a 9255 {
72bf9492 9256 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9257 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9258 else
72bf9492
DJ
9259 add_partial_symbol (pdi, cu);
9260 pdi = pdi->die_sibling;
91c24f0a 9261 }
91c24f0a
DC
9262}
9263
6caca83c
CC
9264/* Return the initial uleb128 in the die at INFO_PTR. */
9265
9266static unsigned int
d521ce57 9267peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9268{
9269 unsigned int bytes_read;
9270
9271 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9272}
9273
685af9cd
TT
9274/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9275 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9276
4bb7a0a7
DJ
9277 Return the corresponding abbrev, or NULL if the number is zero (indicating
9278 an empty DIE). In either case *BYTES_READ will be set to the length of
9279 the initial number. */
9280
9281static struct abbrev_info *
685af9cd
TT
9282peek_die_abbrev (const die_reader_specs &reader,
9283 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9284{
685af9cd 9285 dwarf2_cu *cu = reader.cu;
518817b3 9286 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9287 unsigned int abbrev_number
9288 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9289
9290 if (abbrev_number == 0)
9291 return NULL;
9292
685af9cd 9293 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9294 if (!abbrev)
9295 {
422b9917 9296 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9297 " at offset %s [in module %s]"),
422b9917 9298 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9299 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9300 }
9301
9302 return abbrev;
9303}
9304
93311388
DE
9305/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9306 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9307 DIE. Any children of the skipped DIEs will also be skipped. */
9308
d521ce57
TT
9309static const gdb_byte *
9310skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9311{
4bb7a0a7
DJ
9312 while (1)
9313 {
685af9cd
TT
9314 unsigned int bytes_read;
9315 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9316
4bb7a0a7
DJ
9317 if (abbrev == NULL)
9318 return info_ptr + bytes_read;
9319 else
dee91e82 9320 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9321 }
9322}
9323
93311388
DE
9324/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9325 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9326 abbrev corresponding to that skipped uleb128 should be passed in
9327 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9328 children. */
9329
d521ce57
TT
9330static const gdb_byte *
9331skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9332 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9333{
9334 unsigned int bytes_read;
9335 struct attribute attr;
dee91e82
DE
9336 bfd *abfd = reader->abfd;
9337 struct dwarf2_cu *cu = reader->cu;
d521ce57 9338 const gdb_byte *buffer = reader->buffer;
f664829e 9339 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9340 unsigned int form, i;
9341
9342 for (i = 0; i < abbrev->num_attrs; i++)
9343 {
9344 /* The only abbrev we care about is DW_AT_sibling. */
9345 if (abbrev->attrs[i].name == DW_AT_sibling)
9346 {
dee91e82 9347 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9348 if (attr.form == DW_FORM_ref_addr)
b98664d3 9349 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9350 else
b9502d3f 9351 {
9c541725
PA
9352 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9353 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9354
9355 if (sibling_ptr < info_ptr)
b98664d3 9356 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9357 else if (sibling_ptr > reader->buffer_end)
9358 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9359 else
9360 return sibling_ptr;
9361 }
4bb7a0a7
DJ
9362 }
9363
9364 /* If it isn't DW_AT_sibling, skip this attribute. */
9365 form = abbrev->attrs[i].form;
9366 skip_attribute:
9367 switch (form)
9368 {
4bb7a0a7 9369 case DW_FORM_ref_addr:
ae411497
TT
9370 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9371 and later it is offset sized. */
9372 if (cu->header.version == 2)
9373 info_ptr += cu->header.addr_size;
9374 else
9375 info_ptr += cu->header.offset_size;
9376 break;
36586728
TT
9377 case DW_FORM_GNU_ref_alt:
9378 info_ptr += cu->header.offset_size;
9379 break;
ae411497 9380 case DW_FORM_addr:
4bb7a0a7
DJ
9381 info_ptr += cu->header.addr_size;
9382 break;
9383 case DW_FORM_data1:
9384 case DW_FORM_ref1:
9385 case DW_FORM_flag:
8fe0f950 9386 case DW_FORM_strx1:
4bb7a0a7
DJ
9387 info_ptr += 1;
9388 break;
2dc7f7b3 9389 case DW_FORM_flag_present:
43988095 9390 case DW_FORM_implicit_const:
2dc7f7b3 9391 break;
4bb7a0a7
DJ
9392 case DW_FORM_data2:
9393 case DW_FORM_ref2:
8fe0f950 9394 case DW_FORM_strx2:
4bb7a0a7
DJ
9395 info_ptr += 2;
9396 break;
8fe0f950
AT
9397 case DW_FORM_strx3:
9398 info_ptr += 3;
9399 break;
4bb7a0a7
DJ
9400 case DW_FORM_data4:
9401 case DW_FORM_ref4:
8fe0f950 9402 case DW_FORM_strx4:
4bb7a0a7
DJ
9403 info_ptr += 4;
9404 break;
9405 case DW_FORM_data8:
9406 case DW_FORM_ref8:
55f1336d 9407 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9408 info_ptr += 8;
9409 break;
0224619f
JK
9410 case DW_FORM_data16:
9411 info_ptr += 16;
9412 break;
4bb7a0a7 9413 case DW_FORM_string:
9b1c24c8 9414 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9415 info_ptr += bytes_read;
9416 break;
2dc7f7b3 9417 case DW_FORM_sec_offset:
4bb7a0a7 9418 case DW_FORM_strp:
36586728 9419 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9420 info_ptr += cu->header.offset_size;
9421 break;
2dc7f7b3 9422 case DW_FORM_exprloc:
4bb7a0a7
DJ
9423 case DW_FORM_block:
9424 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9425 info_ptr += bytes_read;
9426 break;
9427 case DW_FORM_block1:
9428 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9429 break;
9430 case DW_FORM_block2:
9431 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9432 break;
9433 case DW_FORM_block4:
9434 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9435 break;
336d760d 9436 case DW_FORM_addrx:
cf532bd1 9437 case DW_FORM_strx:
4bb7a0a7
DJ
9438 case DW_FORM_sdata:
9439 case DW_FORM_udata:
9440 case DW_FORM_ref_udata:
3019eac3
DE
9441 case DW_FORM_GNU_addr_index:
9442 case DW_FORM_GNU_str_index:
d521ce57 9443 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9444 break;
9445 case DW_FORM_indirect:
9446 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9447 info_ptr += bytes_read;
9448 /* We need to continue parsing from here, so just go back to
9449 the top. */
9450 goto skip_attribute;
9451
9452 default:
3e43a32a
MS
9453 error (_("Dwarf Error: Cannot handle %s "
9454 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9455 dwarf_form_name (form),
9456 bfd_get_filename (abfd));
9457 }
9458 }
9459
9460 if (abbrev->has_children)
dee91e82 9461 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9462 else
9463 return info_ptr;
9464}
9465
93311388 9466/* Locate ORIG_PDI's sibling.
dee91e82 9467 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9468
d521ce57 9469static const gdb_byte *
dee91e82
DE
9470locate_pdi_sibling (const struct die_reader_specs *reader,
9471 struct partial_die_info *orig_pdi,
d521ce57 9472 const gdb_byte *info_ptr)
91c24f0a
DC
9473{
9474 /* Do we know the sibling already? */
72bf9492 9475
91c24f0a
DC
9476 if (orig_pdi->sibling)
9477 return orig_pdi->sibling;
9478
9479 /* Are there any children to deal with? */
9480
9481 if (!orig_pdi->has_children)
9482 return info_ptr;
9483
4bb7a0a7 9484 /* Skip the children the long way. */
91c24f0a 9485
dee91e82 9486 return skip_children (reader, info_ptr);
91c24f0a
DC
9487}
9488
257e7a09 9489/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9490 not NULL. */
c906108c
SS
9491
9492static void
257e7a09
YQ
9493dwarf2_read_symtab (struct partial_symtab *self,
9494 struct objfile *objfile)
c906108c 9495{
ed2dc618
SM
9496 struct dwarf2_per_objfile *dwarf2_per_objfile
9497 = get_dwarf2_per_objfile (objfile);
9498
257e7a09 9499 if (self->readin)
c906108c 9500 {
442e4d9c 9501 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9502 self->filename);
442e4d9c
YQ
9503 }
9504 else
9505 {
9506 if (info_verbose)
c906108c 9507 {
442e4d9c 9508 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9509 self->filename);
442e4d9c 9510 gdb_flush (gdb_stdout);
c906108c 9511 }
c906108c 9512
442e4d9c
YQ
9513 /* If this psymtab is constructed from a debug-only objfile, the
9514 has_section_at_zero flag will not necessarily be correct. We
9515 can get the correct value for this flag by looking at the data
9516 associated with the (presumably stripped) associated objfile. */
9517 if (objfile->separate_debug_objfile_backlink)
9518 {
9519 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9520 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9521
442e4d9c
YQ
9522 dwarf2_per_objfile->has_section_at_zero
9523 = dpo_backlink->has_section_at_zero;
9524 }
b2ab525c 9525
442e4d9c 9526 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9527
257e7a09 9528 psymtab_to_symtab_1 (self);
c906108c 9529
442e4d9c
YQ
9530 /* Finish up the debug error message. */
9531 if (info_verbose)
9532 printf_filtered (_("done.\n"));
c906108c 9533 }
95554aad 9534
ed2dc618 9535 process_cu_includes (dwarf2_per_objfile);
c906108c 9536}
9cdd5dbd
DE
9537\f
9538/* Reading in full CUs. */
c906108c 9539
10b3939b
DJ
9540/* Add PER_CU to the queue. */
9541
9542static void
95554aad
TT
9543queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9544 enum language pretend_language)
10b3939b
DJ
9545{
9546 struct dwarf2_queue_item *item;
9547
9548 per_cu->queued = 1;
8d749320 9549 item = XNEW (struct dwarf2_queue_item);
10b3939b 9550 item->per_cu = per_cu;
95554aad 9551 item->pretend_language = pretend_language;
10b3939b
DJ
9552 item->next = NULL;
9553
9554 if (dwarf2_queue == NULL)
9555 dwarf2_queue = item;
9556 else
9557 dwarf2_queue_tail->next = item;
9558
9559 dwarf2_queue_tail = item;
9560}
9561
89e63ee4
DE
9562/* If PER_CU is not yet queued, add it to the queue.
9563 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9564 dependency.
0907af0c 9565 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9566 meaning either PER_CU is already queued or it is already loaded.
9567
9568 N.B. There is an invariant here that if a CU is queued then it is loaded.
9569 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9570
9571static int
89e63ee4 9572maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9573 struct dwarf2_per_cu_data *per_cu,
9574 enum language pretend_language)
9575{
9576 /* We may arrive here during partial symbol reading, if we need full
9577 DIEs to process an unusual case (e.g. template arguments). Do
9578 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9579 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9580 {
9581 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9582 return 1;
9583 return 0;
9584 }
9585
9586 /* Mark the dependence relation so that we don't flush PER_CU
9587 too early. */
89e63ee4
DE
9588 if (dependent_cu != NULL)
9589 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9590
9591 /* If it's already on the queue, we have nothing to do. */
9592 if (per_cu->queued)
9593 return 0;
9594
9595 /* If the compilation unit is already loaded, just mark it as
9596 used. */
9597 if (per_cu->cu != NULL)
9598 {
9599 per_cu->cu->last_used = 0;
9600 return 0;
9601 }
9602
9603 /* Add it to the queue. */
9604 queue_comp_unit (per_cu, pretend_language);
9605
9606 return 1;
9607}
9608
10b3939b
DJ
9609/* Process the queue. */
9610
9611static void
ed2dc618 9612process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9613{
9614 struct dwarf2_queue_item *item, *next_item;
9615
b4f54984 9616 if (dwarf_read_debug)
45cfd468
DE
9617 {
9618 fprintf_unfiltered (gdb_stdlog,
9619 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9620 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9621 }
9622
03dd20cc
DJ
9623 /* The queue starts out with one item, but following a DIE reference
9624 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9625 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9626 {
cc12ce38
DE
9627 if ((dwarf2_per_objfile->using_index
9628 ? !item->per_cu->v.quick->compunit_symtab
9629 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9630 /* Skip dummy CUs. */
9631 && item->per_cu->cu != NULL)
f4dc4d17
DE
9632 {
9633 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9634 unsigned int debug_print_threshold;
247f5c4f 9635 char buf[100];
f4dc4d17 9636
247f5c4f 9637 if (per_cu->is_debug_types)
f4dc4d17 9638 {
247f5c4f
DE
9639 struct signatured_type *sig_type =
9640 (struct signatured_type *) per_cu;
9641
9d8780f0 9642 sprintf (buf, "TU %s at offset %s",
73be47f5 9643 hex_string (sig_type->signature),
9d8780f0 9644 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9645 /* There can be 100s of TUs.
9646 Only print them in verbose mode. */
9647 debug_print_threshold = 2;
f4dc4d17 9648 }
247f5c4f 9649 else
73be47f5 9650 {
9d8780f0
SM
9651 sprintf (buf, "CU at offset %s",
9652 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9653 debug_print_threshold = 1;
9654 }
247f5c4f 9655
b4f54984 9656 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9657 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9658
9659 if (per_cu->is_debug_types)
9660 process_full_type_unit (per_cu, item->pretend_language);
9661 else
9662 process_full_comp_unit (per_cu, item->pretend_language);
9663
b4f54984 9664 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9665 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9666 }
10b3939b
DJ
9667
9668 item->per_cu->queued = 0;
9669 next_item = item->next;
9670 xfree (item);
9671 }
9672
9673 dwarf2_queue_tail = NULL;
45cfd468 9674
b4f54984 9675 if (dwarf_read_debug)
45cfd468
DE
9676 {
9677 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9678 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9679 }
10b3939b
DJ
9680}
9681
10b3939b
DJ
9682/* Read in full symbols for PST, and anything it depends on. */
9683
c906108c 9684static void
fba45db2 9685psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9686{
10b3939b 9687 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9688 int i;
9689
95554aad
TT
9690 if (pst->readin)
9691 return;
9692
aaa75496 9693 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9694 if (!pst->dependencies[i]->readin
9695 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9696 {
9697 /* Inform about additional files that need to be read in. */
9698 if (info_verbose)
9699 {
a3f17187 9700 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9701 fputs_filtered (" ", gdb_stdout);
9702 wrap_here ("");
9703 fputs_filtered ("and ", gdb_stdout);
9704 wrap_here ("");
9705 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9706 wrap_here (""); /* Flush output. */
aaa75496
JB
9707 gdb_flush (gdb_stdout);
9708 }
9709 psymtab_to_symtab_1 (pst->dependencies[i]);
9710 }
9711
9a3c8263 9712 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9713
9714 if (per_cu == NULL)
aaa75496
JB
9715 {
9716 /* It's an include file, no symbols to read for it.
9717 Everything is in the parent symtab. */
9718 pst->readin = 1;
9719 return;
9720 }
c906108c 9721
58f0c718 9722 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9723}
9724
dee91e82
DE
9725/* Trivial hash function for die_info: the hash value of a DIE
9726 is its offset in .debug_info for this objfile. */
10b3939b 9727
dee91e82
DE
9728static hashval_t
9729die_hash (const void *item)
10b3939b 9730{
9a3c8263 9731 const struct die_info *die = (const struct die_info *) item;
6502dd73 9732
9c541725 9733 return to_underlying (die->sect_off);
dee91e82 9734}
63d06c5c 9735
dee91e82
DE
9736/* Trivial comparison function for die_info structures: two DIEs
9737 are equal if they have the same offset. */
98bfdba5 9738
dee91e82
DE
9739static int
9740die_eq (const void *item_lhs, const void *item_rhs)
9741{
9a3c8263
SM
9742 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9743 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9744
9c541725 9745 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9746}
c906108c 9747
dee91e82
DE
9748/* die_reader_func for load_full_comp_unit.
9749 This is identical to read_signatured_type_reader,
9750 but is kept separate for now. */
c906108c 9751
dee91e82
DE
9752static void
9753load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9754 const gdb_byte *info_ptr,
dee91e82
DE
9755 struct die_info *comp_unit_die,
9756 int has_children,
9757 void *data)
9758{
9759 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9760 enum language *language_ptr = (enum language *) data;
6caca83c 9761
dee91e82
DE
9762 gdb_assert (cu->die_hash == NULL);
9763 cu->die_hash =
9764 htab_create_alloc_ex (cu->header.length / 12,
9765 die_hash,
9766 die_eq,
9767 NULL,
9768 &cu->comp_unit_obstack,
9769 hashtab_obstack_allocate,
9770 dummy_obstack_deallocate);
e142c38c 9771
dee91e82
DE
9772 if (has_children)
9773 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9774 &info_ptr, comp_unit_die);
9775 cu->dies = comp_unit_die;
9776 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9777
9778 /* We try not to read any attributes in this function, because not
9cdd5dbd 9779 all CUs needed for references have been loaded yet, and symbol
10b3939b 9780 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9781 or we won't be able to build types correctly.
9782 Similarly, if we do not read the producer, we can not apply
9783 producer-specific interpretation. */
95554aad 9784 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9785}
10b3939b 9786
dee91e82 9787/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9788
dee91e82 9789static void
95554aad 9790load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9791 bool skip_partial,
95554aad 9792 enum language pretend_language)
dee91e82 9793{
3019eac3 9794 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9795
58f0c718 9796 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9797 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9798}
9799
3da10d80
KS
9800/* Add a DIE to the delayed physname list. */
9801
9802static void
9803add_to_method_list (struct type *type, int fnfield_index, int index,
9804 const char *name, struct die_info *die,
9805 struct dwarf2_cu *cu)
9806{
9807 struct delayed_method_info mi;
9808 mi.type = type;
9809 mi.fnfield_index = fnfield_index;
9810 mi.index = index;
9811 mi.name = name;
9812 mi.die = die;
c89b44cd 9813 cu->method_list.push_back (mi);
3da10d80
KS
9814}
9815
3693fdb3
PA
9816/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9817 "const" / "volatile". If so, decrements LEN by the length of the
9818 modifier and return true. Otherwise return false. */
9819
9820template<size_t N>
9821static bool
9822check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9823{
9824 size_t mod_len = sizeof (mod) - 1;
9825 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9826 {
9827 len -= mod_len;
9828 return true;
9829 }
9830 return false;
9831}
9832
3da10d80
KS
9833/* Compute the physnames of any methods on the CU's method list.
9834
9835 The computation of method physnames is delayed in order to avoid the
9836 (bad) condition that one of the method's formal parameters is of an as yet
9837 incomplete type. */
9838
9839static void
9840compute_delayed_physnames (struct dwarf2_cu *cu)
9841{
3693fdb3 9842 /* Only C++ delays computing physnames. */
c89b44cd 9843 if (cu->method_list.empty ())
3693fdb3
PA
9844 return;
9845 gdb_assert (cu->language == language_cplus);
9846
52941706 9847 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9848 {
1d06ead6 9849 const char *physname;
3da10d80 9850 struct fn_fieldlist *fn_flp
c89b44cd
TT
9851 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9852 physname = dwarf2_physname (mi.name, mi.die, cu);
9853 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9854 = physname ? physname : "";
3693fdb3
PA
9855
9856 /* Since there's no tag to indicate whether a method is a
9857 const/volatile overload, extract that information out of the
9858 demangled name. */
9859 if (physname != NULL)
9860 {
9861 size_t len = strlen (physname);
9862
9863 while (1)
9864 {
9865 if (physname[len] == ')') /* shortcut */
9866 break;
9867 else if (check_modifier (physname, len, " const"))
c89b44cd 9868 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9869 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9870 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9871 else
9872 break;
9873 }
9874 }
3da10d80 9875 }
c89b44cd
TT
9876
9877 /* The list is no longer needed. */
9878 cu->method_list.clear ();
3da10d80
KS
9879}
9880
a766d390
DE
9881/* Go objects should be embedded in a DW_TAG_module DIE,
9882 and it's not clear if/how imported objects will appear.
9883 To keep Go support simple until that's worked out,
9884 go back through what we've read and create something usable.
9885 We could do this while processing each DIE, and feels kinda cleaner,
9886 but that way is more invasive.
9887 This is to, for example, allow the user to type "p var" or "b main"
9888 without having to specify the package name, and allow lookups
9889 of module.object to work in contexts that use the expression
9890 parser. */
9891
9892static void
9893fixup_go_packaging (struct dwarf2_cu *cu)
9894{
421d1616 9895 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9896 struct pending *list;
9897 int i;
9898
c24bdb02 9899 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9900 list != NULL;
9901 list = list->next)
a766d390
DE
9902 {
9903 for (i = 0; i < list->nsyms; ++i)
9904 {
9905 struct symbol *sym = list->symbol[i];
9906
c1b5c1eb 9907 if (sym->language () == language_go
a766d390
DE
9908 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9909 {
421d1616
TT
9910 gdb::unique_xmalloc_ptr<char> this_package_name
9911 (go_symbol_package_name (sym));
a766d390
DE
9912
9913 if (this_package_name == NULL)
9914 continue;
9915 if (package_name == NULL)
421d1616 9916 package_name = std::move (this_package_name);
a766d390
DE
9917 else
9918 {
518817b3
SM
9919 struct objfile *objfile
9920 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9921 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9922 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9923 (symbol_symtab (sym) != NULL
9924 ? symtab_to_filename_for_display
9925 (symbol_symtab (sym))
e3b94546 9926 : objfile_name (objfile)),
421d1616 9927 this_package_name.get (), package_name.get ());
a766d390
DE
9928 }
9929 }
9930 }
9931 }
9932
9933 if (package_name != NULL)
9934 {
518817b3 9935 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9936 const char *saved_package_name
421d1616 9937 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9938 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9939 saved_package_name);
a766d390
DE
9940 struct symbol *sym;
9941
e623cf5d 9942 sym = allocate_symbol (objfile);
d3ecddab 9943 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9944 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9945 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9946 e.g., "main" finds the "main" module and not C's main(). */
9947 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9948 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9949 SYMBOL_TYPE (sym) = type;
9950
c24bdb02 9951 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9952 }
9953}
9954
c9317f21
TT
9955/* Allocate a fully-qualified name consisting of the two parts on the
9956 obstack. */
9957
9958static const char *
9959rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9960{
9961 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9962}
9963
9964/* A helper that allocates a struct discriminant_info to attach to a
9965 union type. */
9966
9967static struct discriminant_info *
9968alloc_discriminant_info (struct type *type, int discriminant_index,
9969 int default_index)
9970{
9971 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9972 gdb_assert (discriminant_index == -1
9973 || (discriminant_index >= 0
9974 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9975 gdb_assert (default_index == -1
c7b15a66 9976 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9977
9978 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9979
9980 struct discriminant_info *disc
9981 = ((struct discriminant_info *)
9982 TYPE_ZALLOC (type,
9983 offsetof (struct discriminant_info, discriminants)
9984 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9985 disc->default_index = default_index;
9986 disc->discriminant_index = discriminant_index;
9987
9988 struct dynamic_prop prop;
9989 prop.kind = PROP_UNDEFINED;
9990 prop.data.baton = disc;
9991
9992 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9993
9994 return disc;
9995}
9996
9997/* Some versions of rustc emitted enums in an unusual way.
9998
9999 Ordinary enums were emitted as unions. The first element of each
10000 structure in the union was named "RUST$ENUM$DISR". This element
10001 held the discriminant.
10002
10003 These versions of Rust also implemented the "non-zero"
10004 optimization. When the enum had two values, and one is empty and
10005 the other holds a pointer that cannot be zero, the pointer is used
10006 as the discriminant, with a zero value meaning the empty variant.
10007 Here, the union's first member is of the form
10008 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10009 where the fieldnos are the indices of the fields that should be
10010 traversed in order to find the field (which may be several fields deep)
10011 and the variantname is the name of the variant of the case when the
10012 field is zero.
10013
10014 This function recognizes whether TYPE is of one of these forms,
10015 and, if so, smashes it to be a variant type. */
10016
10017static void
10018quirk_rust_enum (struct type *type, struct objfile *objfile)
10019{
10020 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10021
10022 /* We don't need to deal with empty enums. */
10023 if (TYPE_NFIELDS (type) == 0)
10024 return;
10025
10026#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10027 if (TYPE_NFIELDS (type) == 1
10028 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10029 {
10030 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10031
10032 /* Decode the field name to find the offset of the
10033 discriminant. */
10034 ULONGEST bit_offset = 0;
10035 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10036 while (name[0] >= '0' && name[0] <= '9')
10037 {
10038 char *tail;
10039 unsigned long index = strtoul (name, &tail, 10);
10040 name = tail;
10041 if (*name != '$'
10042 || index >= TYPE_NFIELDS (field_type)
10043 || (TYPE_FIELD_LOC_KIND (field_type, index)
10044 != FIELD_LOC_KIND_BITPOS))
10045 {
b98664d3 10046 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10047 "[in module %s]"),
10048 TYPE_FIELD_NAME (type, 0),
10049 objfile_name (objfile));
10050 return;
10051 }
10052 ++name;
10053
10054 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10055 field_type = TYPE_FIELD_TYPE (field_type, index);
10056 }
10057
10058 /* Make a union to hold the variants. */
10059 struct type *union_type = alloc_type (objfile);
10060 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10061 TYPE_NFIELDS (union_type) = 3;
10062 TYPE_FIELDS (union_type)
10063 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10064 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10065 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10066
10067 /* Put the discriminant must at index 0. */
10068 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10069 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10070 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10071 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10072
10073 /* The order of fields doesn't really matter, so put the real
10074 field at index 1 and the data-less field at index 2. */
10075 struct discriminant_info *disc
10076 = alloc_discriminant_info (union_type, 0, 1);
10077 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10078 TYPE_FIELD_NAME (union_type, 1)
10079 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10080 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10081 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10082 TYPE_FIELD_NAME (union_type, 1));
10083
10084 const char *dataless_name
10085 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10086 name);
10087 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10088 dataless_name);
10089 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10090 /* NAME points into the original discriminant name, which
10091 already has the correct lifetime. */
10092 TYPE_FIELD_NAME (union_type, 2) = name;
10093 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10094 disc->discriminants[2] = 0;
10095
10096 /* Smash this type to be a structure type. We have to do this
10097 because the type has already been recorded. */
10098 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10099 TYPE_NFIELDS (type) = 1;
10100 TYPE_FIELDS (type)
10101 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10102
10103 /* Install the variant part. */
10104 TYPE_FIELD_TYPE (type, 0) = union_type;
10105 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10106 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10107 }
77c2dba3
TT
10108 /* A union with a single anonymous field is probably an old-style
10109 univariant enum. */
10110 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10111 {
c9317f21
TT
10112 /* Smash this type to be a structure type. We have to do this
10113 because the type has already been recorded. */
10114 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10115
10116 /* Make a union to hold the variants. */
10117 struct type *union_type = alloc_type (objfile);
10118 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10119 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10120 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10121 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10122 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10123
10124 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10125 const char *variant_name
10126 = rust_last_path_segment (TYPE_NAME (field_type));
10127 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10128 TYPE_NAME (field_type)
10129 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10130 TYPE_NAME (type), variant_name);
c9317f21
TT
10131
10132 /* Install the union in the outer struct type. */
10133 TYPE_NFIELDS (type) = 1;
10134 TYPE_FIELDS (type)
10135 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10136 TYPE_FIELD_TYPE (type, 0) = union_type;
10137 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10138 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10139
10140 alloc_discriminant_info (union_type, -1, 0);
10141 }
10142 else
10143 {
10144 struct type *disr_type = nullptr;
10145 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10146 {
10147 disr_type = TYPE_FIELD_TYPE (type, i);
10148
a037790e
TT
10149 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10150 {
10151 /* All fields of a true enum will be structs. */
10152 return;
10153 }
10154 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10155 {
10156 /* Could be data-less variant, so keep going. */
a037790e 10157 disr_type = nullptr;
c9317f21
TT
10158 }
10159 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10160 "RUST$ENUM$DISR") != 0)
10161 {
10162 /* Not a Rust enum. */
10163 return;
10164 }
10165 else
10166 {
10167 /* Found one. */
10168 break;
10169 }
10170 }
10171
10172 /* If we got here without a discriminant, then it's probably
10173 just a union. */
10174 if (disr_type == nullptr)
10175 return;
10176
10177 /* Smash this type to be a structure type. We have to do this
10178 because the type has already been recorded. */
10179 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10180
10181 /* Make a union to hold the variants. */
10182 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10183 struct type *union_type = alloc_type (objfile);
10184 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10185 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10186 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10187 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10188 TYPE_FIELDS (union_type)
10189 = (struct field *) TYPE_ZALLOC (union_type,
10190 (TYPE_NFIELDS (union_type)
10191 * sizeof (struct field)));
10192
10193 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10194 TYPE_NFIELDS (type) * sizeof (struct field));
10195
10196 /* Install the discriminant at index 0 in the union. */
10197 TYPE_FIELD (union_type, 0) = *disr_field;
10198 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10199 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10200
10201 /* Install the union in the outer struct type. */
10202 TYPE_FIELD_TYPE (type, 0) = union_type;
10203 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10204 TYPE_NFIELDS (type) = 1;
10205
10206 /* Set the size and offset of the union type. */
10207 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10208
10209 /* We need a way to find the correct discriminant given a
10210 variant name. For convenience we build a map here. */
10211 struct type *enum_type = FIELD_TYPE (*disr_field);
10212 std::unordered_map<std::string, ULONGEST> discriminant_map;
10213 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10214 {
10215 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10216 {
10217 const char *name
10218 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10219 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10220 }
10221 }
10222
10223 int n_fields = TYPE_NFIELDS (union_type);
10224 struct discriminant_info *disc
10225 = alloc_discriminant_info (union_type, 0, -1);
10226 /* Skip the discriminant here. */
10227 for (int i = 1; i < n_fields; ++i)
10228 {
10229 /* Find the final word in the name of this variant's type.
10230 That name can be used to look up the correct
10231 discriminant. */
10232 const char *variant_name
10233 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10234 i)));
10235
10236 auto iter = discriminant_map.find (variant_name);
10237 if (iter != discriminant_map.end ())
10238 disc->discriminants[i] = iter->second;
10239
bedda9ac 10240 /* Remove the discriminant field, if it exists. */
c9317f21 10241 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10242 if (TYPE_NFIELDS (sub_type) > 0)
10243 {
10244 --TYPE_NFIELDS (sub_type);
10245 ++TYPE_FIELDS (sub_type);
10246 }
c9317f21
TT
10247 TYPE_FIELD_NAME (union_type, i) = variant_name;
10248 TYPE_NAME (sub_type)
10249 = rust_fully_qualify (&objfile->objfile_obstack,
10250 TYPE_NAME (type), variant_name);
10251 }
10252 }
10253}
10254
10255/* Rewrite some Rust unions to be structures with variants parts. */
10256
10257static void
10258rust_union_quirks (struct dwarf2_cu *cu)
10259{
10260 gdb_assert (cu->language == language_rust);
52941706
SM
10261 for (type *type_ : cu->rust_unions)
10262 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10263 /* We don't need this any more. */
10264 cu->rust_unions.clear ();
c9317f21
TT
10265}
10266
95554aad
TT
10267/* Return the symtab for PER_CU. This works properly regardless of
10268 whether we're using the index or psymtabs. */
10269
43f3e411
DE
10270static struct compunit_symtab *
10271get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10272{
ed2dc618 10273 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10274 ? per_cu->v.quick->compunit_symtab
10275 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10276}
10277
10278/* A helper function for computing the list of all symbol tables
10279 included by PER_CU. */
10280
10281static void
4c39bc03 10282recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10283 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10284 struct dwarf2_per_cu_data *per_cu,
43f3e411 10285 struct compunit_symtab *immediate_parent)
95554aad
TT
10286{
10287 void **slot;
43f3e411 10288 struct compunit_symtab *cust;
95554aad
TT
10289
10290 slot = htab_find_slot (all_children, per_cu, INSERT);
10291 if (*slot != NULL)
10292 {
10293 /* This inclusion and its children have been processed. */
10294 return;
10295 }
10296
10297 *slot = per_cu;
10298 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10299 cust = get_compunit_symtab (per_cu);
10300 if (cust != NULL)
ec94af83
DE
10301 {
10302 /* If this is a type unit only add its symbol table if we haven't
10303 seen it yet (type unit per_cu's can share symtabs). */
10304 if (per_cu->is_debug_types)
10305 {
43f3e411 10306 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10307 if (*slot == NULL)
10308 {
43f3e411 10309 *slot = cust;
4c39bc03 10310 result->push_back (cust);
43f3e411
DE
10311 if (cust->user == NULL)
10312 cust->user = immediate_parent;
ec94af83
DE
10313 }
10314 }
10315 else
f9125b6c 10316 {
4c39bc03 10317 result->push_back (cust);
43f3e411
DE
10318 if (cust->user == NULL)
10319 cust->user = immediate_parent;
f9125b6c 10320 }
ec94af83 10321 }
95554aad 10322
ae640021
AB
10323 if (!per_cu->imported_symtabs_empty ())
10324 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10325 {
10326 recursively_compute_inclusions (result, all_children,
10327 all_type_symtabs, ptr, cust);
10328 }
95554aad
TT
10329}
10330
43f3e411 10331/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10332 PER_CU. */
10333
10334static void
43f3e411 10335compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10336{
f4dc4d17
DE
10337 gdb_assert (! per_cu->is_debug_types);
10338
ae640021 10339 if (!per_cu->imported_symtabs_empty ())
95554aad 10340 {
ae640021 10341 int len;
4c39bc03 10342 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10343 htab_t all_children, all_type_symtabs;
43f3e411 10344 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10345
10346 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10347 if (cust == NULL)
95554aad
TT
10348 return;
10349
10350 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10351 NULL, xcalloc, xfree);
ec94af83
DE
10352 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10353 NULL, xcalloc, xfree);
95554aad 10354
ae640021 10355 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10356 {
10357 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10358 all_type_symtabs, ptr, cust);
ec94af83 10359 }
95554aad 10360
ec94af83 10361 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10362 len = result_symtabs.size ();
43f3e411 10363 cust->includes
ed2dc618 10364 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10365 struct compunit_symtab *, len + 1);
4c39bc03
TT
10366 memcpy (cust->includes, result_symtabs.data (),
10367 len * sizeof (compunit_symtab *));
43f3e411 10368 cust->includes[len] = NULL;
95554aad 10369
95554aad 10370 htab_delete (all_children);
ec94af83 10371 htab_delete (all_type_symtabs);
95554aad
TT
10372 }
10373}
10374
10375/* Compute the 'includes' field for the symtabs of all the CUs we just
10376 read. */
10377
10378static void
ed2dc618 10379process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10380{
71b73764 10381 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10382 {
10383 if (! iter->is_debug_types)
43f3e411 10384 compute_compunit_symtab_includes (iter);
f4dc4d17 10385 }
95554aad 10386
c5d0225d 10387 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10388}
10389
9cdd5dbd 10390/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10391 already been loaded into memory. */
10392
10393static void
95554aad
TT
10394process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10395 enum language pretend_language)
10b3939b 10396{
10b3939b 10397 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10398 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10399 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10400 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10401 CORE_ADDR lowpc, highpc;
43f3e411 10402 struct compunit_symtab *cust;
10b3939b 10403 CORE_ADDR baseaddr;
4359dff1 10404 struct block *static_block;
3e29f34a 10405 CORE_ADDR addr;
10b3939b
DJ
10406
10407 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10408
c89b44cd
TT
10409 /* Clear the list here in case something was left over. */
10410 cu->method_list.clear ();
10b3939b 10411
95554aad
TT
10412 cu->language = pretend_language;
10413 cu->language_defn = language_def (cu->language);
10414
c906108c 10415 /* Do line number decoding in read_file_scope () */
10b3939b 10416 process_die (cu->dies, cu);
c906108c 10417
a766d390
DE
10418 /* For now fudge the Go package. */
10419 if (cu->language == language_go)
10420 fixup_go_packaging (cu);
10421
5f48f8f3 10422 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10423 should be complete, and it should now be safe to compute all of the
10424 physnames. */
10425 compute_delayed_physnames (cu);
3da10d80 10426
c9317f21
TT
10427 if (cu->language == language_rust)
10428 rust_union_quirks (cu);
10429
fae299cd
DC
10430 /* Some compilers don't define a DW_AT_high_pc attribute for the
10431 compilation unit. If the DW_AT_high_pc is missing, synthesize
10432 it, by scanning the DIE's below the compilation unit. */
10b3939b 10433 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10434
3e29f34a 10435 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10436 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10437
10438 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10439 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10440 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10441 addrmap to help ensure it has an accurate map of pc values belonging to
10442 this comp unit. */
10443 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10444
c24bdb02 10445 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10446 SECT_OFF_TEXT (objfile),
10447 0);
c906108c 10448
43f3e411 10449 if (cust != NULL)
c906108c 10450 {
df15bd07 10451 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10452
8be455d7
JK
10453 /* Set symtab language to language from DW_AT_language. If the
10454 compilation is from a C file generated by language preprocessors, do
10455 not set the language if it was already deduced by start_subfile. */
43f3e411 10456 if (!(cu->language == language_c
40e3ad0e 10457 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10458 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10459
10460 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10461 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10462 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10463 there were bugs in prologue debug info, fixed later in GCC-4.5
10464 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10465
10466 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10467 needed, it would be wrong due to missing DW_AT_producer there.
10468
10469 Still one can confuse GDB by using non-standard GCC compilation
10470 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10471 */
ab260dad 10472 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10473 cust->locations_valid = 1;
e0d00bc7
JK
10474
10475 if (gcc_4_minor >= 5)
43f3e411 10476 cust->epilogue_unwind_valid = 1;
96408a79 10477
43f3e411 10478 cust->call_site_htab = cu->call_site_htab;
c906108c 10479 }
9291a0cd
TT
10480
10481 if (dwarf2_per_objfile->using_index)
43f3e411 10482 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10483 else
10484 {
10485 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10486 pst->compunit_symtab = cust;
9291a0cd
TT
10487 pst->readin = 1;
10488 }
c906108c 10489
95554aad 10490 /* Push it for inclusion processing later. */
c5d0225d 10491 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10492
10493 /* Not needed any more. */
c24bdb02 10494 cu->reset_builder ();
f4dc4d17 10495}
45cfd468 10496
f4dc4d17
DE
10497/* Generate full symbol information for type unit PER_CU, whose DIEs have
10498 already been loaded into memory. */
10499
10500static void
10501process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10502 enum language pretend_language)
10503{
10504 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10505 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10506 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10507 struct compunit_symtab *cust;
0186c6a7
DE
10508 struct signatured_type *sig_type;
10509
10510 gdb_assert (per_cu->is_debug_types);
10511 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10512
c89b44cd
TT
10513 /* Clear the list here in case something was left over. */
10514 cu->method_list.clear ();
f4dc4d17 10515
f4dc4d17
DE
10516 cu->language = pretend_language;
10517 cu->language_defn = language_def (cu->language);
10518
10519 /* The symbol tables are set up in read_type_unit_scope. */
10520 process_die (cu->dies, cu);
10521
10522 /* For now fudge the Go package. */
10523 if (cu->language == language_go)
10524 fixup_go_packaging (cu);
10525
5f48f8f3 10526 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10527 should be complete, and it should now be safe to compute all of the
10528 physnames. */
10529 compute_delayed_physnames (cu);
f4dc4d17 10530
c9317f21
TT
10531 if (cu->language == language_rust)
10532 rust_union_quirks (cu);
10533
f4dc4d17
DE
10534 /* TUs share symbol tables.
10535 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10536 of it with end_expandable_symtab. Otherwise, complete the addition of
10537 this TU's symbols to the existing symtab. */
43f3e411 10538 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10539 {
c24bdb02
KS
10540 buildsym_compunit *builder = cu->get_builder ();
10541 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10542 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10543
43f3e411 10544 if (cust != NULL)
f4dc4d17
DE
10545 {
10546 /* Set symtab language to language from DW_AT_language. If the
10547 compilation is from a C file generated by language preprocessors,
10548 do not set the language if it was already deduced by
10549 start_subfile. */
43f3e411
DE
10550 if (!(cu->language == language_c
10551 && COMPUNIT_FILETABS (cust)->language != language_c))
10552 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10553 }
10554 }
10555 else
10556 {
c24bdb02 10557 cu->get_builder ()->augment_type_symtab ();
43f3e411 10558 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10559 }
10560
10561 if (dwarf2_per_objfile->using_index)
43f3e411 10562 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10563 else
10564 {
10565 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10566 pst->compunit_symtab = cust;
f4dc4d17 10567 pst->readin = 1;
45cfd468 10568 }
804d2729
TT
10569
10570 /* Not needed any more. */
c24bdb02 10571 cu->reset_builder ();
c906108c
SS
10572}
10573
95554aad
TT
10574/* Process an imported unit DIE. */
10575
10576static void
10577process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10578{
10579 struct attribute *attr;
10580
f4dc4d17
DE
10581 /* For now we don't handle imported units in type units. */
10582 if (cu->per_cu->is_debug_types)
10583 {
10584 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10585 " supported in type units [in module %s]"),
518817b3 10586 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10587 }
10588
95554aad
TT
10589 attr = dwarf2_attr (die, DW_AT_import, cu);
10590 if (attr != NULL)
10591 {
9c541725
PA
10592 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10593 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10594 dwarf2_per_cu_data *per_cu
e3b94546 10595 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10596 cu->per_cu->dwarf2_per_objfile);
95554aad 10597
69d751e3 10598 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10599 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10600 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10601
ae640021 10602 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10603 }
10604}
10605
4c8aa72d
PA
10606/* RAII object that represents a process_die scope: i.e.,
10607 starts/finishes processing a DIE. */
10608class process_die_scope
adde2bff 10609{
4c8aa72d
PA
10610public:
10611 process_die_scope (die_info *die, dwarf2_cu *cu)
10612 : m_die (die), m_cu (cu)
10613 {
10614 /* We should only be processing DIEs not already in process. */
10615 gdb_assert (!m_die->in_process);
10616 m_die->in_process = true;
10617 }
8c3cb9fa 10618
4c8aa72d
PA
10619 ~process_die_scope ()
10620 {
10621 m_die->in_process = false;
10622
10623 /* If we're done processing the DIE for the CU that owns the line
10624 header, we don't need the line header anymore. */
10625 if (m_cu->line_header_die_owner == m_die)
10626 {
10627 delete m_cu->line_header;
10628 m_cu->line_header = NULL;
10629 m_cu->line_header_die_owner = NULL;
10630 }
10631 }
10632
10633private:
10634 die_info *m_die;
10635 dwarf2_cu *m_cu;
10636};
adde2bff 10637
c906108c
SS
10638/* Process a die and its children. */
10639
10640static void
e7c27a73 10641process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10642{
4c8aa72d 10643 process_die_scope scope (die, cu);
adde2bff 10644
c906108c
SS
10645 switch (die->tag)
10646 {
10647 case DW_TAG_padding:
10648 break;
10649 case DW_TAG_compile_unit:
95554aad 10650 case DW_TAG_partial_unit:
e7c27a73 10651 read_file_scope (die, cu);
c906108c 10652 break;
348e048f
DE
10653 case DW_TAG_type_unit:
10654 read_type_unit_scope (die, cu);
10655 break;
c906108c 10656 case DW_TAG_subprogram:
0a4b0913
AB
10657 /* Nested subprograms in Fortran get a prefix. */
10658 if (cu->language == language_fortran
10659 && die->parent != NULL
10660 && die->parent->tag == DW_TAG_subprogram)
10661 cu->processing_has_namespace_info = true;
10662 /* Fall through. */
c906108c 10663 case DW_TAG_inlined_subroutine:
edb3359d 10664 read_func_scope (die, cu);
c906108c
SS
10665 break;
10666 case DW_TAG_lexical_block:
14898363
L
10667 case DW_TAG_try_block:
10668 case DW_TAG_catch_block:
e7c27a73 10669 read_lexical_block_scope (die, cu);
c906108c 10670 break;
216f72a1 10671 case DW_TAG_call_site:
96408a79
SA
10672 case DW_TAG_GNU_call_site:
10673 read_call_site_scope (die, cu);
10674 break;
c906108c 10675 case DW_TAG_class_type:
680b30c7 10676 case DW_TAG_interface_type:
c906108c
SS
10677 case DW_TAG_structure_type:
10678 case DW_TAG_union_type:
134d01f1 10679 process_structure_scope (die, cu);
c906108c
SS
10680 break;
10681 case DW_TAG_enumeration_type:
134d01f1 10682 process_enumeration_scope (die, cu);
c906108c 10683 break;
134d01f1 10684
f792889a
DJ
10685 /* These dies have a type, but processing them does not create
10686 a symbol or recurse to process the children. Therefore we can
10687 read them on-demand through read_type_die. */
c906108c 10688 case DW_TAG_subroutine_type:
72019c9c 10689 case DW_TAG_set_type:
c906108c 10690 case DW_TAG_array_type:
c906108c 10691 case DW_TAG_pointer_type:
c906108c 10692 case DW_TAG_ptr_to_member_type:
c906108c 10693 case DW_TAG_reference_type:
4297a3f0 10694 case DW_TAG_rvalue_reference_type:
c906108c 10695 case DW_TAG_string_type:
c906108c 10696 break;
134d01f1 10697
c906108c 10698 case DW_TAG_base_type:
a02abb62 10699 case DW_TAG_subrange_type:
cb249c71 10700 case DW_TAG_typedef:
134d01f1
DJ
10701 /* Add a typedef symbol for the type definition, if it has a
10702 DW_AT_name. */
f792889a 10703 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10704 break;
c906108c 10705 case DW_TAG_common_block:
e7c27a73 10706 read_common_block (die, cu);
c906108c
SS
10707 break;
10708 case DW_TAG_common_inclusion:
10709 break;
d9fa45fe 10710 case DW_TAG_namespace:
9068261f 10711 cu->processing_has_namespace_info = true;
e7c27a73 10712 read_namespace (die, cu);
d9fa45fe 10713 break;
5d7cb8df 10714 case DW_TAG_module:
9068261f 10715 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10716 read_module (die, cu);
10717 break;
d9fa45fe 10718 case DW_TAG_imported_declaration:
9068261f 10719 cu->processing_has_namespace_info = true;
74921315
KS
10720 if (read_namespace_alias (die, cu))
10721 break;
86a73007
TT
10722 /* The declaration is not a global namespace alias. */
10723 /* Fall through. */
d9fa45fe 10724 case DW_TAG_imported_module:
9068261f 10725 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10726 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10727 || cu->language != language_fortran))
b98664d3 10728 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10729 dwarf_tag_name (die->tag));
10730 read_import_statement (die, cu);
d9fa45fe 10731 break;
95554aad
TT
10732
10733 case DW_TAG_imported_unit:
10734 process_imported_unit_die (die, cu);
10735 break;
10736
71a3c369
TT
10737 case DW_TAG_variable:
10738 read_variable (die, cu);
10739 break;
10740
c906108c 10741 default:
e7c27a73 10742 new_symbol (die, NULL, cu);
c906108c
SS
10743 break;
10744 }
10745}
ca69b9e6
DE
10746\f
10747/* DWARF name computation. */
c906108c 10748
94af9270
KS
10749/* A helper function for dwarf2_compute_name which determines whether DIE
10750 needs to have the name of the scope prepended to the name listed in the
10751 die. */
10752
10753static int
10754die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10755{
1c809c68
TT
10756 struct attribute *attr;
10757
94af9270
KS
10758 switch (die->tag)
10759 {
10760 case DW_TAG_namespace:
10761 case DW_TAG_typedef:
10762 case DW_TAG_class_type:
10763 case DW_TAG_interface_type:
10764 case DW_TAG_structure_type:
10765 case DW_TAG_union_type:
10766 case DW_TAG_enumeration_type:
10767 case DW_TAG_enumerator:
10768 case DW_TAG_subprogram:
08a76f8a 10769 case DW_TAG_inlined_subroutine:
94af9270 10770 case DW_TAG_member:
74921315 10771 case DW_TAG_imported_declaration:
94af9270
KS
10772 return 1;
10773
10774 case DW_TAG_variable:
c2b0a229 10775 case DW_TAG_constant:
94af9270
KS
10776 /* We only need to prefix "globally" visible variables. These include
10777 any variable marked with DW_AT_external or any variable that
10778 lives in a namespace. [Variables in anonymous namespaces
10779 require prefixing, but they are not DW_AT_external.] */
10780
10781 if (dwarf2_attr (die, DW_AT_specification, cu))
10782 {
10783 struct dwarf2_cu *spec_cu = cu;
9a619af0 10784
94af9270
KS
10785 return die_needs_namespace (die_specification (die, &spec_cu),
10786 spec_cu);
10787 }
10788
1c809c68 10789 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10790 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10791 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10792 return 0;
10793 /* A variable in a lexical block of some kind does not need a
10794 namespace, even though in C++ such variables may be external
10795 and have a mangled name. */
10796 if (die->parent->tag == DW_TAG_lexical_block
10797 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10798 || die->parent->tag == DW_TAG_catch_block
10799 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10800 return 0;
10801 return 1;
94af9270
KS
10802
10803 default:
10804 return 0;
10805 }
10806}
10807
73b9be8b
KS
10808/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10809 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10810 defined for the given DIE. */
10811
10812static struct attribute *
10813dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10814{
10815 struct attribute *attr;
10816
10817 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10818 if (attr == NULL)
10819 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10820
10821 return attr;
10822}
10823
10824/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10825 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10826 defined for the given DIE. */
10827
10828static const char *
10829dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10830{
10831 const char *linkage_name;
10832
10833 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10834 if (linkage_name == NULL)
10835 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10836
10837 return linkage_name;
10838}
10839
94af9270 10840/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10841 compute the physname for the object, which include a method's:
9c37b5ae 10842 - formal parameters (C++),
a766d390 10843 - receiver type (Go),
a766d390
DE
10844
10845 The term "physname" is a bit confusing.
10846 For C++, for example, it is the demangled name.
10847 For Go, for example, it's the mangled name.
94af9270 10848
af6b7be1
JB
10849 For Ada, return the DIE's linkage name rather than the fully qualified
10850 name. PHYSNAME is ignored..
10851
94af9270
KS
10852 The result is allocated on the objfile_obstack and canonicalized. */
10853
10854static const char *
15d034d0
TT
10855dwarf2_compute_name (const char *name,
10856 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10857 int physname)
10858{
518817b3 10859 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10860
94af9270
KS
10861 if (name == NULL)
10862 name = dwarf2_name (die, cu);
10863
2ee7123e
DE
10864 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10865 but otherwise compute it by typename_concat inside GDB.
10866 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10867 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10868 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10869 will set the demangled name to the result of dwarf2_full_name, and it is
10870 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10871 if (cu->language == language_ada
10872 || (cu->language == language_fortran && physname))
10873 {
10874 /* For Ada unit, we prefer the linkage name over the name, as
10875 the former contains the exported name, which the user expects
10876 to be able to reference. Ideally, we want the user to be able
10877 to reference this entity using either natural or linkage name,
10878 but we haven't started looking at this enhancement yet. */
73b9be8b 10879 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10880
2ee7123e
DE
10881 if (linkage_name != NULL)
10882 return linkage_name;
f55ee35c
JK
10883 }
10884
94af9270
KS
10885 /* These are the only languages we know how to qualify names in. */
10886 if (name != NULL
9c37b5ae 10887 && (cu->language == language_cplus
c44af4eb
TT
10888 || cu->language == language_fortran || cu->language == language_d
10889 || cu->language == language_rust))
94af9270
KS
10890 {
10891 if (die_needs_namespace (die, cu))
10892 {
0d5cff50 10893 const char *prefix;
34a68019 10894 const char *canonical_name = NULL;
94af9270 10895
d7e74731
PA
10896 string_file buf;
10897
94af9270 10898 prefix = determine_prefix (die, cu);
94af9270
KS
10899 if (*prefix != '\0')
10900 {
43816ebc
TT
10901 gdb::unique_xmalloc_ptr<char> prefixed_name
10902 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10903
43816ebc 10904 buf.puts (prefixed_name.get ());
94af9270
KS
10905 }
10906 else
d7e74731 10907 buf.puts (name);
94af9270 10908
98bfdba5
PA
10909 /* Template parameters may be specified in the DIE's DW_AT_name, or
10910 as children with DW_TAG_template_type_param or
10911 DW_TAG_value_type_param. If the latter, add them to the name
10912 here. If the name already has template parameters, then
10913 skip this step; some versions of GCC emit both, and
10914 it is more efficient to use the pre-computed name.
10915
10916 Something to keep in mind about this process: it is very
10917 unlikely, or in some cases downright impossible, to produce
10918 something that will match the mangled name of a function.
10919 If the definition of the function has the same debug info,
10920 we should be able to match up with it anyway. But fallbacks
10921 using the minimal symbol, for instance to find a method
10922 implemented in a stripped copy of libstdc++, will not work.
10923 If we do not have debug info for the definition, we will have to
10924 match them up some other way.
10925
10926 When we do name matching there is a related problem with function
10927 templates; two instantiated function templates are allowed to
10928 differ only by their return types, which we do not add here. */
10929
10930 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10931 {
10932 struct attribute *attr;
10933 struct die_info *child;
10934 int first = 1;
10935
10936 die->building_fullname = 1;
10937
10938 for (child = die->child; child != NULL; child = child->sibling)
10939 {
10940 struct type *type;
12df843f 10941 LONGEST value;
d521ce57 10942 const gdb_byte *bytes;
98bfdba5
PA
10943 struct dwarf2_locexpr_baton *baton;
10944 struct value *v;
10945
10946 if (child->tag != DW_TAG_template_type_param
10947 && child->tag != DW_TAG_template_value_param)
10948 continue;
10949
10950 if (first)
10951 {
d7e74731 10952 buf.puts ("<");
98bfdba5
PA
10953 first = 0;
10954 }
10955 else
d7e74731 10956 buf.puts (", ");
98bfdba5
PA
10957
10958 attr = dwarf2_attr (child, DW_AT_type, cu);
10959 if (attr == NULL)
10960 {
b98664d3 10961 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10962 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10963 continue;
10964 }
10965 type = die_type (child, cu);
10966
10967 if (child->tag == DW_TAG_template_type_param)
10968 {
c1ec8cea
TT
10969 c_print_type (type, "", &buf, -1, 0, cu->language,
10970 &type_print_raw_options);
98bfdba5
PA
10971 continue;
10972 }
10973
10974 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10975 if (attr == NULL)
10976 {
b98664d3 10977 complaint (_("template parameter missing "
3e43a32a 10978 "DW_AT_const_value"));
d7e74731 10979 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10980 continue;
10981 }
10982
10983 dwarf2_const_value_attr (attr, type, name,
10984 &cu->comp_unit_obstack, cu,
10985 &value, &bytes, &baton);
10986
10987 if (TYPE_NOSIGN (type))
10988 /* GDB prints characters as NUMBER 'CHAR'. If that's
10989 changed, this can use value_print instead. */
d7e74731 10990 c_printchar (value, type, &buf);
98bfdba5
PA
10991 else
10992 {
10993 struct value_print_options opts;
10994
10995 if (baton != NULL)
10996 v = dwarf2_evaluate_loc_desc (type, NULL,
10997 baton->data,
10998 baton->size,
10999 baton->per_cu);
11000 else if (bytes != NULL)
11001 {
11002 v = allocate_value (type);
11003 memcpy (value_contents_writeable (v), bytes,
11004 TYPE_LENGTH (type));
11005 }
11006 else
11007 v = value_from_longest (type, value);
11008
3e43a32a
MS
11009 /* Specify decimal so that we do not depend on
11010 the radix. */
98bfdba5
PA
11011 get_formatted_print_options (&opts, 'd');
11012 opts.raw = 1;
d7e74731 11013 value_print (v, &buf, &opts);
98bfdba5 11014 release_value (v);
98bfdba5
PA
11015 }
11016 }
11017
11018 die->building_fullname = 0;
11019
11020 if (!first)
11021 {
11022 /* Close the argument list, with a space if necessary
11023 (nested templates). */
d7e74731
PA
11024 if (!buf.empty () && buf.string ().back () == '>')
11025 buf.puts (" >");
98bfdba5 11026 else
d7e74731 11027 buf.puts (">");
98bfdba5
PA
11028 }
11029 }
11030
9c37b5ae 11031 /* For C++ methods, append formal parameter type
94af9270 11032 information, if PHYSNAME. */
6e70227d 11033
94af9270 11034 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11035 && cu->language == language_cplus)
94af9270
KS
11036 {
11037 struct type *type = read_type_die (die, cu);
11038
d7e74731 11039 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11040 &type_print_raw_options);
94af9270 11041
9c37b5ae 11042 if (cu->language == language_cplus)
94af9270 11043 {
60430eff
DJ
11044 /* Assume that an artificial first parameter is
11045 "this", but do not crash if it is not. RealView
11046 marks unnamed (and thus unused) parameters as
11047 artificial; there is no way to differentiate
11048 the two cases. */
94af9270
KS
11049 if (TYPE_NFIELDS (type) > 0
11050 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11051 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11052 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11053 0))))
d7e74731 11054 buf.puts (" const");
94af9270
KS
11055 }
11056 }
11057
d7e74731 11058 const std::string &intermediate_name = buf.string ();
94af9270
KS
11059
11060 if (cu->language == language_cplus)
34a68019 11061 canonical_name
322a8516 11062 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11063 &objfile->per_bfd->storage_obstack);
11064
11065 /* If we only computed INTERMEDIATE_NAME, or if
11066 INTERMEDIATE_NAME is already canonical, then we need to
11067 copy it to the appropriate obstack. */
322a8516 11068 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11069 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11070 intermediate_name);
34a68019
TT
11071 else
11072 name = canonical_name;
94af9270
KS
11073 }
11074 }
11075
11076 return name;
11077}
11078
0114d602
DJ
11079/* Return the fully qualified name of DIE, based on its DW_AT_name.
11080 If scope qualifiers are appropriate they will be added. The result
34a68019 11081 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11082 not have a name. NAME may either be from a previous call to
11083 dwarf2_name or NULL.
11084
9c37b5ae 11085 The output string will be canonicalized (if C++). */
0114d602
DJ
11086
11087static const char *
15d034d0 11088dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11089{
94af9270
KS
11090 return dwarf2_compute_name (name, die, cu, 0);
11091}
0114d602 11092
94af9270
KS
11093/* Construct a physname for the given DIE in CU. NAME may either be
11094 from a previous call to dwarf2_name or NULL. The result will be
11095 allocated on the objfile_objstack or NULL if the DIE does not have a
11096 name.
0114d602 11097
9c37b5ae 11098 The output string will be canonicalized (if C++). */
0114d602 11099
94af9270 11100static const char *
15d034d0 11101dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11102{
518817b3 11103 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11104 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11105 int need_copy = 1;
11106
11107 /* In this case dwarf2_compute_name is just a shortcut not building anything
11108 on its own. */
11109 if (!die_needs_namespace (die, cu))
11110 return dwarf2_compute_name (name, die, cu, 1);
11111
73b9be8b 11112 mangled = dw2_linkage_name (die, cu);
900e11f9 11113
e98c9e7c
TT
11114 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11115 See https://github.com/rust-lang/rust/issues/32925. */
11116 if (cu->language == language_rust && mangled != NULL
11117 && strchr (mangled, '{') != NULL)
11118 mangled = NULL;
11119
900e11f9
JK
11120 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11121 has computed. */
791afaa2 11122 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11123 if (mangled != NULL)
900e11f9 11124 {
900e11f9 11125
59cc4834
JB
11126 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11127 {
11128 /* Do nothing (do not demangle the symbol name). */
11129 }
11130 else if (cu->language == language_go)
a766d390 11131 {
5e2db402
TT
11132 /* This is a lie, but we already lie to the caller new_symbol.
11133 new_symbol assumes we return the mangled name.
a766d390 11134 This just undoes that lie until things are cleaned up. */
a766d390
DE
11135 }
11136 else
11137 {
0eb876f5
JB
11138 /* Use DMGL_RET_DROP for C++ template functions to suppress
11139 their return type. It is easier for GDB users to search
11140 for such functions as `name(params)' than `long name(params)'.
11141 In such case the minimal symbol names do not match the full
11142 symbol names but for template functions there is never a need
11143 to look up their definition from their declaration so
11144 the only disadvantage remains the minimal symbol variant
11145 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11146 demangled.reset (gdb_demangle (mangled,
11147 (DMGL_PARAMS | DMGL_ANSI
11148 | DMGL_RET_DROP)));
a766d390 11149 }
900e11f9 11150 if (demangled)
791afaa2 11151 canon = demangled.get ();
900e11f9
JK
11152 else
11153 {
11154 canon = mangled;
11155 need_copy = 0;
11156 }
11157 }
11158
11159 if (canon == NULL || check_physname)
11160 {
11161 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11162
11163 if (canon != NULL && strcmp (physname, canon) != 0)
11164 {
11165 /* It may not mean a bug in GDB. The compiler could also
11166 compute DW_AT_linkage_name incorrectly. But in such case
11167 GDB would need to be bug-to-bug compatible. */
11168
b98664d3 11169 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11170 "(from linkage <%s>) - DIE at %s [in module %s]"),
11171 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11172 objfile_name (objfile));
900e11f9
JK
11173
11174 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11175 is available here - over computed PHYSNAME. It is safer
11176 against both buggy GDB and buggy compilers. */
11177
11178 retval = canon;
11179 }
11180 else
11181 {
11182 retval = physname;
11183 need_copy = 0;
11184 }
11185 }
11186 else
11187 retval = canon;
11188
11189 if (need_copy)
021887d8 11190 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11191
900e11f9 11192 return retval;
0114d602
DJ
11193}
11194
74921315
KS
11195/* Inspect DIE in CU for a namespace alias. If one exists, record
11196 a new symbol for it.
11197
11198 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11199
11200static int
11201read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11202{
11203 struct attribute *attr;
11204
11205 /* If the die does not have a name, this is not a namespace
11206 alias. */
11207 attr = dwarf2_attr (die, DW_AT_name, cu);
11208 if (attr != NULL)
11209 {
11210 int num;
11211 struct die_info *d = die;
11212 struct dwarf2_cu *imported_cu = cu;
11213
11214 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11215 keep inspecting DIEs until we hit the underlying import. */
11216#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11217 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11218 {
11219 attr = dwarf2_attr (d, DW_AT_import, cu);
11220 if (attr == NULL)
11221 break;
11222
11223 d = follow_die_ref (d, attr, &imported_cu);
11224 if (d->tag != DW_TAG_imported_declaration)
11225 break;
11226 }
11227
11228 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11229 {
b98664d3 11230 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11231 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11232 return 0;
11233 }
11234
11235 if (attr != NULL)
11236 {
11237 struct type *type;
9c541725 11238 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11239
9c541725 11240 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11241 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11242 {
11243 /* This declaration is a global namespace alias. Add
11244 a symbol for it whose type is the aliased namespace. */
11245 new_symbol (die, type, cu);
11246 return 1;
11247 }
11248 }
11249 }
11250
11251 return 0;
11252}
11253
22cee43f 11254/* Return the using directives repository (global or local?) to use in the
804d2729 11255 current context for CU.
22cee43f
PMR
11256
11257 For Ada, imported declarations can materialize renamings, which *may* be
11258 global. However it is impossible (for now?) in DWARF to distinguish
11259 "external" imported declarations and "static" ones. As all imported
11260 declarations seem to be static in all other languages, make them all CU-wide
11261 global only in Ada. */
11262
11263static struct using_direct **
804d2729 11264using_directives (struct dwarf2_cu *cu)
22cee43f 11265{
c24bdb02
KS
11266 if (cu->language == language_ada
11267 && cu->get_builder ()->outermost_context_p ())
11268 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11269 else
c24bdb02 11270 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11271}
11272
27aa8d6a
SW
11273/* Read the import statement specified by the given die and record it. */
11274
11275static void
11276read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11277{
518817b3 11278 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11279 struct attribute *import_attr;
32019081 11280 struct die_info *imported_die, *child_die;
de4affc9 11281 struct dwarf2_cu *imported_cu;
27aa8d6a 11282 const char *imported_name;
794684b6 11283 const char *imported_name_prefix;
13387711
SW
11284 const char *canonical_name;
11285 const char *import_alias;
11286 const char *imported_declaration = NULL;
794684b6 11287 const char *import_prefix;
eb1e02fd 11288 std::vector<const char *> excludes;
13387711 11289
27aa8d6a
SW
11290 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11291 if (import_attr == NULL)
11292 {
b98664d3 11293 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11294 dwarf_tag_name (die->tag));
11295 return;
11296 }
11297
de4affc9
CC
11298 imported_cu = cu;
11299 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11300 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11301 if (imported_name == NULL)
11302 {
11303 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11304
11305 The import in the following code:
11306 namespace A
11307 {
11308 typedef int B;
11309 }
11310
11311 int main ()
11312 {
11313 using A::B;
11314 B b;
11315 return b;
11316 }
11317
11318 ...
11319 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11320 <52> DW_AT_decl_file : 1
11321 <53> DW_AT_decl_line : 6
11322 <54> DW_AT_import : <0x75>
11323 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11324 <59> DW_AT_name : B
11325 <5b> DW_AT_decl_file : 1
11326 <5c> DW_AT_decl_line : 2
11327 <5d> DW_AT_type : <0x6e>
11328 ...
11329 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11330 <76> DW_AT_byte_size : 4
11331 <77> DW_AT_encoding : 5 (signed)
11332
11333 imports the wrong die ( 0x75 instead of 0x58 ).
11334 This case will be ignored until the gcc bug is fixed. */
11335 return;
11336 }
11337
82856980
SW
11338 /* Figure out the local name after import. */
11339 import_alias = dwarf2_name (die, cu);
27aa8d6a 11340
794684b6
SW
11341 /* Figure out where the statement is being imported to. */
11342 import_prefix = determine_prefix (die, cu);
11343
11344 /* Figure out what the scope of the imported die is and prepend it
11345 to the name of the imported die. */
de4affc9 11346 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11347
f55ee35c
JK
11348 if (imported_die->tag != DW_TAG_namespace
11349 && imported_die->tag != DW_TAG_module)
794684b6 11350 {
13387711
SW
11351 imported_declaration = imported_name;
11352 canonical_name = imported_name_prefix;
794684b6 11353 }
13387711 11354 else if (strlen (imported_name_prefix) > 0)
12aaed36 11355 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11356 imported_name_prefix,
11357 (cu->language == language_d ? "." : "::"),
11358 imported_name, (char *) NULL);
13387711
SW
11359 else
11360 canonical_name = imported_name;
794684b6 11361
32019081
JK
11362 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11363 for (child_die = die->child; child_die && child_die->tag;
11364 child_die = sibling_die (child_die))
11365 {
11366 /* DWARF-4: A Fortran use statement with a “rename list” may be
11367 represented by an imported module entry with an import attribute
11368 referring to the module and owned entries corresponding to those
11369 entities that are renamed as part of being imported. */
11370
11371 if (child_die->tag != DW_TAG_imported_declaration)
11372 {
b98664d3 11373 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11374 "- DIE at %s [in module %s]"),
11375 sect_offset_str (child_die->sect_off),
11376 objfile_name (objfile));
32019081
JK
11377 continue;
11378 }
11379
11380 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11381 if (import_attr == NULL)
11382 {
b98664d3 11383 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11384 dwarf_tag_name (child_die->tag));
11385 continue;
11386 }
11387
11388 imported_cu = cu;
11389 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11390 &imported_cu);
11391 imported_name = dwarf2_name (imported_die, imported_cu);
11392 if (imported_name == NULL)
11393 {
b98664d3 11394 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11395 "imported name - DIE at %s [in module %s]"),
11396 sect_offset_str (child_die->sect_off),
11397 objfile_name (objfile));
32019081
JK
11398 continue;
11399 }
11400
eb1e02fd 11401 excludes.push_back (imported_name);
32019081
JK
11402
11403 process_die (child_die, cu);
11404 }
11405
804d2729 11406 add_using_directive (using_directives (cu),
22cee43f
PMR
11407 import_prefix,
11408 canonical_name,
11409 import_alias,
11410 imported_declaration,
11411 excludes,
11412 0,
11413 &objfile->objfile_obstack);
27aa8d6a
SW
11414}
11415
5230b05a
WT
11416/* ICC<14 does not output the required DW_AT_declaration on incomplete
11417 types, but gives them a size of zero. Starting with version 14,
11418 ICC is compatible with GCC. */
11419
9068261f 11420static bool
5230b05a
WT
11421producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11422{
11423 if (!cu->checked_producer)
11424 check_producer (cu);
11425
11426 return cu->producer_is_icc_lt_14;
11427}
11428
eb77c9df
AB
11429/* ICC generates a DW_AT_type for C void functions. This was observed on
11430 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11431 which says that void functions should not have a DW_AT_type. */
11432
11433static bool
11434producer_is_icc (struct dwarf2_cu *cu)
11435{
11436 if (!cu->checked_producer)
11437 check_producer (cu);
11438
11439 return cu->producer_is_icc;
11440}
11441
1b80a9fa
JK
11442/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11443 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11444 this, it was first present in GCC release 4.3.0. */
11445
9068261f 11446static bool
1b80a9fa
JK
11447producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11448{
11449 if (!cu->checked_producer)
11450 check_producer (cu);
11451
11452 return cu->producer_is_gcc_lt_4_3;
11453}
11454
d721ba37
PA
11455static file_and_directory
11456find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11457{
d721ba37
PA
11458 file_and_directory res;
11459
9291a0cd
TT
11460 /* Find the filename. Do not use dwarf2_name here, since the filename
11461 is not a source language identifier. */
d721ba37
PA
11462 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11463 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11464
d721ba37
PA
11465 if (res.comp_dir == NULL
11466 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11467 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11468 {
d721ba37
PA
11469 res.comp_dir_storage = ldirname (res.name);
11470 if (!res.comp_dir_storage.empty ())
11471 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11472 }
d721ba37 11473 if (res.comp_dir != NULL)
9291a0cd
TT
11474 {
11475 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11476 directory, get rid of it. */
d721ba37 11477 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11478
d721ba37
PA
11479 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11480 res.comp_dir = cp + 1;
9291a0cd
TT
11481 }
11482
d721ba37
PA
11483 if (res.name == NULL)
11484 res.name = "<unknown>";
11485
11486 return res;
9291a0cd
TT
11487}
11488
f4dc4d17
DE
11489/* Handle DW_AT_stmt_list for a compilation unit.
11490 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11491 COMP_DIR is the compilation directory. LOWPC is passed to
11492 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11493
11494static void
11495handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11496 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11497{
518817b3
SM
11498 struct dwarf2_per_objfile *dwarf2_per_objfile
11499 = cu->per_cu->dwarf2_per_objfile;
527f3840 11500 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11501 struct attribute *attr;
527f3840
JK
11502 struct line_header line_header_local;
11503 hashval_t line_header_local_hash;
527f3840
JK
11504 void **slot;
11505 int decode_mapping;
2ab95328 11506
f4dc4d17
DE
11507 gdb_assert (! cu->per_cu->is_debug_types);
11508
2ab95328 11509 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11510 if (attr == NULL)
11511 return;
11512
9c541725 11513 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11514
11515 /* The line header hash table is only created if needed (it exists to
11516 prevent redundant reading of the line table for partial_units).
11517 If we're given a partial_unit, we'll need it. If we're given a
11518 compile_unit, then use the line header hash table if it's already
11519 created, but don't create one just yet. */
11520
11521 if (dwarf2_per_objfile->line_header_hash == NULL
11522 && die->tag == DW_TAG_partial_unit)
2ab95328 11523 {
527f3840
JK
11524 dwarf2_per_objfile->line_header_hash
11525 = htab_create_alloc_ex (127, line_header_hash_voidp,
11526 line_header_eq_voidp,
11527 free_line_header_voidp,
11528 &objfile->objfile_obstack,
11529 hashtab_obstack_allocate,
11530 dummy_obstack_deallocate);
11531 }
2ab95328 11532
9c541725 11533 line_header_local.sect_off = line_offset;
527f3840
JK
11534 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11535 line_header_local_hash = line_header_hash (&line_header_local);
11536 if (dwarf2_per_objfile->line_header_hash != NULL)
11537 {
11538 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11539 &line_header_local,
11540 line_header_local_hash, NO_INSERT);
11541
11542 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11543 is not present in *SLOT (since if there is something in *SLOT then
11544 it will be for a partial_unit). */
11545 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11546 {
527f3840 11547 gdb_assert (*slot != NULL);
9a3c8263 11548 cu->line_header = (struct line_header *) *slot;
527f3840 11549 return;
dee91e82 11550 }
2ab95328 11551 }
527f3840
JK
11552
11553 /* dwarf_decode_line_header does not yet provide sufficient information.
11554 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11555 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11556 if (lh == NULL)
527f3840 11557 return;
4c8aa72d
PA
11558
11559 cu->line_header = lh.release ();
11560 cu->line_header_die_owner = die;
527f3840
JK
11561
11562 if (dwarf2_per_objfile->line_header_hash == NULL)
11563 slot = NULL;
11564 else
11565 {
11566 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11567 &line_header_local,
11568 line_header_local_hash, INSERT);
11569 gdb_assert (slot != NULL);
11570 }
11571 if (slot != NULL && *slot == NULL)
11572 {
11573 /* This newly decoded line number information unit will be owned
11574 by line_header_hash hash table. */
11575 *slot = cu->line_header;
4c8aa72d 11576 cu->line_header_die_owner = NULL;
527f3840
JK
11577 }
11578 else
11579 {
11580 /* We cannot free any current entry in (*slot) as that struct line_header
11581 may be already used by multiple CUs. Create only temporary decoded
11582 line_header for this CU - it may happen at most once for each line
11583 number information unit. And if we're not using line_header_hash
11584 then this is what we want as well. */
11585 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11586 }
11587 decode_mapping = (die->tag != DW_TAG_partial_unit);
11588 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11589 decode_mapping);
fff8551c 11590
2ab95328
TT
11591}
11592
95554aad 11593/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11594
c906108c 11595static void
e7c27a73 11596read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11597{
518817b3
SM
11598 struct dwarf2_per_objfile *dwarf2_per_objfile
11599 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11600 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11601 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11602 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11603 CORE_ADDR highpc = ((CORE_ADDR) 0);
11604 struct attribute *attr;
c906108c 11605 struct die_info *child_die;
e142c38c 11606 CORE_ADDR baseaddr;
6e70227d 11607
380618d6 11608 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11609 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11610
fae299cd 11611 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11612
11613 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11614 from finish_block. */
2acceee2 11615 if (lowpc == ((CORE_ADDR) -1))
c906108c 11616 lowpc = highpc;
3e29f34a 11617 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11618
d721ba37 11619 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11620
f4b8a18d
KW
11621 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11622 standardised yet. As a workaround for the language detection we fall
11623 back to the DW_AT_producer string. */
11624 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11625 cu->language = language_opencl;
11626
3019eac3
DE
11627 /* Similar hack for Go. */
11628 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11629 set_cu_language (DW_LANG_Go, cu);
11630
c24bdb02 11631 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11632
11633 /* Decode line number information if present. We do this before
11634 processing child DIEs, so that the line header table is available
11635 for DW_AT_decl_file. */
d721ba37 11636 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11637
11638 /* Process all dies in compilation unit. */
11639 if (die->child != NULL)
11640 {
11641 child_die = die->child;
11642 while (child_die && child_die->tag)
11643 {
11644 process_die (child_die, cu);
11645 child_die = sibling_die (child_die);
11646 }
11647 }
11648
11649 /* Decode macro information, if present. Dwarf 2 macro information
11650 refers to information in the line number info statement program
11651 header, so we can only read it if we've read the header
11652 successfully. */
0af92d60
JK
11653 attr = dwarf2_attr (die, DW_AT_macros, cu);
11654 if (attr == NULL)
11655 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11656 if (attr && cu->line_header)
11657 {
11658 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11659 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11660
43f3e411 11661 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11662 }
11663 else
11664 {
11665 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11666 if (attr && cu->line_header)
11667 {
11668 unsigned int macro_offset = DW_UNSND (attr);
11669
43f3e411 11670 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11671 }
11672 }
3019eac3
DE
11673}
11674
c24bdb02
KS
11675void
11676dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11677{
f4dc4d17
DE
11678 struct type_unit_group *tu_group;
11679 int first_time;
3019eac3 11680 struct attribute *attr;
9c541725 11681 unsigned int i;
0186c6a7 11682 struct signatured_type *sig_type;
3019eac3 11683
f4dc4d17 11684 gdb_assert (per_cu->is_debug_types);
0186c6a7 11685 sig_type = (struct signatured_type *) per_cu;
3019eac3 11686
c24bdb02 11687 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11688
f4dc4d17 11689 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11690 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11691 if (sig_type->type_unit_group == NULL)
c24bdb02 11692 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11693 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11694
11695 /* If we've already processed this stmt_list there's no real need to
11696 do it again, we could fake it and just recreate the part we need
11697 (file name,index -> symtab mapping). If data shows this optimization
11698 is useful we can do it then. */
43f3e411 11699 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11700
11701 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11702 debug info. */
fff8551c 11703 line_header_up lh;
f4dc4d17 11704 if (attr != NULL)
3019eac3 11705 {
9c541725 11706 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11707 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11708 }
11709 if (lh == NULL)
11710 {
11711 if (first_time)
c24bdb02 11712 start_symtab ("", NULL, 0);
f4dc4d17
DE
11713 else
11714 {
11715 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11716 gdb_assert (m_builder == nullptr);
804d2729 11717 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11718 m_builder.reset (new struct buildsym_compunit
11719 (COMPUNIT_OBJFILE (cust), "",
11720 COMPUNIT_DIRNAME (cust),
11721 compunit_language (cust),
11722 0, cust));
f4dc4d17 11723 }
f4dc4d17 11724 return;
3019eac3
DE
11725 }
11726
c24bdb02
KS
11727 line_header = lh.release ();
11728 line_header_die_owner = die;
3019eac3 11729
f4dc4d17
DE
11730 if (first_time)
11731 {
c24bdb02 11732 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11733
1fd60fc0
DE
11734 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11735 still initializing it, and our caller (a few levels up)
11736 process_full_type_unit still needs to know if this is the first
11737 time. */
11738
7ba99d21 11739 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11740 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11741 line_header->file_names_size ());
3019eac3 11742
7ba99d21
AT
11743 auto &file_names = line_header->file_names ();
11744 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11745 {
7ba99d21 11746 file_entry &fe = file_names[i];
c24bdb02
KS
11747 dwarf2_start_subfile (this, fe.name,
11748 fe.include_dir (line_header));
11749 buildsym_compunit *b = get_builder ();
11750 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11751 {
4c8aa72d
PA
11752 /* NOTE: start_subfile will recognize when it's been
11753 passed a file it has already seen. So we can't
11754 assume there's a simple mapping from
11755 cu->line_header->file_names to subfiles, plus
11756 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11757 b->get_current_subfile ()->symtab
11758 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11759 }
11760
c24bdb02 11761 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11762 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11763 }
11764 }
11765 else
3019eac3 11766 {
c24bdb02 11767 gdb_assert (m_builder == nullptr);
804d2729 11768 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11769 m_builder.reset (new struct buildsym_compunit
11770 (COMPUNIT_OBJFILE (cust), "",
11771 COMPUNIT_DIRNAME (cust),
11772 compunit_language (cust),
11773 0, cust));
f4dc4d17 11774
7ba99d21
AT
11775 auto &file_names = line_header->file_names ();
11776 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11777 {
7ba99d21 11778 file_entry &fe = file_names[i];
4c8aa72d 11779 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11780 }
3019eac3
DE
11781 }
11782
f4dc4d17
DE
11783 /* The main symtab is allocated last. Type units don't have DW_AT_name
11784 so they don't have a "real" (so to speak) symtab anyway.
11785 There is later code that will assign the main symtab to all symbols
11786 that don't have one. We need to handle the case of a symbol with a
11787 missing symtab (DW_AT_decl_file) anyway. */
11788}
3019eac3 11789
f4dc4d17
DE
11790/* Process DW_TAG_type_unit.
11791 For TUs we want to skip the first top level sibling if it's not the
11792 actual type being defined by this TU. In this case the first top
11793 level sibling is there to provide context only. */
3019eac3 11794
f4dc4d17
DE
11795static void
11796read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11797{
11798 struct die_info *child_die;
3019eac3 11799
f4dc4d17
DE
11800 prepare_one_comp_unit (cu, die, language_minimal);
11801
11802 /* Initialize (or reinitialize) the machinery for building symtabs.
11803 We do this before processing child DIEs, so that the line header table
11804 is available for DW_AT_decl_file. */
c24bdb02 11805 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11806
11807 if (die->child != NULL)
11808 {
11809 child_die = die->child;
11810 while (child_die && child_die->tag)
11811 {
11812 process_die (child_die, cu);
11813 child_die = sibling_die (child_die);
11814 }
11815 }
3019eac3
DE
11816}
11817\f
80626a55
DE
11818/* DWO/DWP files.
11819
11820 http://gcc.gnu.org/wiki/DebugFission
11821 http://gcc.gnu.org/wiki/DebugFissionDWP
11822
11823 To simplify handling of both DWO files ("object" files with the DWARF info)
11824 and DWP files (a file with the DWOs packaged up into one file), we treat
11825 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11826
11827static hashval_t
11828hash_dwo_file (const void *item)
11829{
9a3c8263 11830 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11831 hashval_t hash;
3019eac3 11832
a2ce51a0
DE
11833 hash = htab_hash_string (dwo_file->dwo_name);
11834 if (dwo_file->comp_dir != NULL)
11835 hash += htab_hash_string (dwo_file->comp_dir);
11836 return hash;
3019eac3
DE
11837}
11838
11839static int
11840eq_dwo_file (const void *item_lhs, const void *item_rhs)
11841{
9a3c8263
SM
11842 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11843 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11844
a2ce51a0
DE
11845 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11846 return 0;
11847 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11848 return lhs->comp_dir == rhs->comp_dir;
11849 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11850}
11851
11852/* Allocate a hash table for DWO files. */
11853
51ac9db5 11854static htab_up
ed2dc618 11855allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11856{
51ac9db5
SM
11857 auto delete_dwo_file = [] (void *item)
11858 {
11859 struct dwo_file *dwo_file = (struct dwo_file *) item;
11860
11861 delete dwo_file;
11862 };
11863
11864 return htab_up (htab_create_alloc_ex (41,
11865 hash_dwo_file,
11866 eq_dwo_file,
11867 delete_dwo_file,
11868 &objfile->objfile_obstack,
11869 hashtab_obstack_allocate,
11870 dummy_obstack_deallocate));
3019eac3
DE
11871}
11872
80626a55
DE
11873/* Lookup DWO file DWO_NAME. */
11874
11875static void **
ed2dc618
SM
11876lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11877 const char *dwo_name,
11878 const char *comp_dir)
80626a55
DE
11879{
11880 struct dwo_file find_entry;
11881 void **slot;
11882
11883 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11884 dwarf2_per_objfile->dwo_files
11885 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11886
0ac5b59e
DE
11887 find_entry.dwo_name = dwo_name;
11888 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11889 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11890 INSERT);
80626a55
DE
11891
11892 return slot;
11893}
11894
3019eac3
DE
11895static hashval_t
11896hash_dwo_unit (const void *item)
11897{
9a3c8263 11898 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11899
11900 /* This drops the top 32 bits of the id, but is ok for a hash. */
11901 return dwo_unit->signature;
11902}
11903
11904static int
11905eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11906{
9a3c8263
SM
11907 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11908 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11909
11910 /* The signature is assumed to be unique within the DWO file.
11911 So while object file CU dwo_id's always have the value zero,
11912 that's OK, assuming each object file DWO file has only one CU,
11913 and that's the rule for now. */
11914 return lhs->signature == rhs->signature;
11915}
11916
11917/* Allocate a hash table for DWO CUs,TUs.
11918 There is one of these tables for each of CUs,TUs for each DWO file. */
11919
11920static htab_t
11921allocate_dwo_unit_table (struct objfile *objfile)
11922{
11923 /* Start out with a pretty small number.
11924 Generally DWO files contain only one CU and maybe some TUs. */
11925 return htab_create_alloc_ex (3,
11926 hash_dwo_unit,
11927 eq_dwo_unit,
11928 NULL,
11929 &objfile->objfile_obstack,
11930 hashtab_obstack_allocate,
11931 dummy_obstack_deallocate);
11932}
11933
80626a55 11934/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11935
19c3d4c9 11936struct create_dwo_cu_data
3019eac3
DE
11937{
11938 struct dwo_file *dwo_file;
19c3d4c9 11939 struct dwo_unit dwo_unit;
3019eac3
DE
11940};
11941
19c3d4c9 11942/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11943
11944static void
19c3d4c9
DE
11945create_dwo_cu_reader (const struct die_reader_specs *reader,
11946 const gdb_byte *info_ptr,
11947 struct die_info *comp_unit_die,
11948 int has_children,
11949 void *datap)
3019eac3
DE
11950{
11951 struct dwarf2_cu *cu = reader->cu;
9c541725 11952 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11953 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11954 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11955 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11956 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11957
a084a2a6
AT
11958 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11959 if (!signature.has_value ())
3019eac3 11960 {
b98664d3 11961 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11962 " its dwo_id [in module %s]"),
9d8780f0 11963 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11964 return;
11965 }
11966
3019eac3 11967 dwo_unit->dwo_file = dwo_file;
a084a2a6 11968 dwo_unit->signature = *signature;
8a0459fd 11969 dwo_unit->section = section;
9c541725 11970 dwo_unit->sect_off = sect_off;
3019eac3
DE
11971 dwo_unit->length = cu->per_cu->length;
11972
b4f54984 11973 if (dwarf_read_debug)
9d8780f0
SM
11974 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11975 sect_offset_str (sect_off),
9c541725 11976 hex_string (dwo_unit->signature));
3019eac3
DE
11977}
11978
33c5cd75 11979/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11980 Note: This function processes DWO files only, not DWP files. */
3019eac3 11981
33c5cd75 11982static void
ed2dc618
SM
11983create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11984 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11985 htab_t &cus_htab)
3019eac3
DE
11986{
11987 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11988 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11989
33c5cd75
DB
11990 dwarf2_read_section (objfile, &section);
11991 info_ptr = section.buffer;
3019eac3
DE
11992
11993 if (info_ptr == NULL)
33c5cd75 11994 return;
3019eac3 11995
b4f54984 11996 if (dwarf_read_debug)
19c3d4c9
DE
11997 {
11998 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11999 get_section_name (&section),
12000 get_section_file_name (&section));
19c3d4c9 12001 }
3019eac3 12002
33c5cd75 12003 end_ptr = info_ptr + section.size;
3019eac3
DE
12004 while (info_ptr < end_ptr)
12005 {
12006 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12007 struct create_dwo_cu_data create_dwo_cu_data;
12008 struct dwo_unit *dwo_unit;
12009 void **slot;
12010 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12011
19c3d4c9
DE
12012 memset (&create_dwo_cu_data.dwo_unit, 0,
12013 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12014 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12015 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12016 per_cu.is_debug_types = 0;
33c5cd75
DB
12017 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12018 per_cu.section = &section;
c5ed0576 12019 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12020
12021 init_cutu_and_read_dies_no_follow (
12022 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12023 info_ptr += per_cu.length;
12024
12025 // If the unit could not be parsed, skip it.
12026 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12027 continue;
3019eac3 12028
33c5cd75
DB
12029 if (cus_htab == NULL)
12030 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12031
33c5cd75
DB
12032 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12033 *dwo_unit = create_dwo_cu_data.dwo_unit;
12034 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12035 gdb_assert (slot != NULL);
12036 if (*slot != NULL)
19c3d4c9 12037 {
33c5cd75
DB
12038 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12039 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12040
b98664d3 12041 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12042 " the entry at offset %s, signature %s"),
12043 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12044 hex_string (dwo_unit->signature));
19c3d4c9 12045 }
33c5cd75 12046 *slot = (void *)dwo_unit;
3019eac3 12047 }
3019eac3
DE
12048}
12049
80626a55
DE
12050/* DWP file .debug_{cu,tu}_index section format:
12051 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12052
d2415c6c
DE
12053 DWP Version 1:
12054
80626a55
DE
12055 Both index sections have the same format, and serve to map a 64-bit
12056 signature to a set of section numbers. Each section begins with a header,
12057 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12058 indexes, and a pool of 32-bit section numbers. The index sections will be
12059 aligned at 8-byte boundaries in the file.
12060
d2415c6c
DE
12061 The index section header consists of:
12062
12063 V, 32 bit version number
12064 -, 32 bits unused
12065 N, 32 bit number of compilation units or type units in the index
12066 M, 32 bit number of slots in the hash table
80626a55 12067
d2415c6c 12068 Numbers are recorded using the byte order of the application binary.
80626a55 12069
d2415c6c
DE
12070 The hash table begins at offset 16 in the section, and consists of an array
12071 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12072 order of the application binary). Unused slots in the hash table are 0.
12073 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12074
d2415c6c
DE
12075 The parallel table begins immediately after the hash table
12076 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12077 array of 32-bit indexes (using the byte order of the application binary),
12078 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12079 table contains a 32-bit index into the pool of section numbers. For unused
12080 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12081
73869dc2
DE
12082 The pool of section numbers begins immediately following the hash table
12083 (at offset 16 + 12 * M from the beginning of the section). The pool of
12084 section numbers consists of an array of 32-bit words (using the byte order
12085 of the application binary). Each item in the array is indexed starting
12086 from 0. The hash table entry provides the index of the first section
12087 number in the set. Additional section numbers in the set follow, and the
12088 set is terminated by a 0 entry (section number 0 is not used in ELF).
12089
12090 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12091 section must be the first entry in the set, and the .debug_abbrev.dwo must
12092 be the second entry. Other members of the set may follow in any order.
12093
12094 ---
12095
12096 DWP Version 2:
12097
12098 DWP Version 2 combines all the .debug_info, etc. sections into one,
12099 and the entries in the index tables are now offsets into these sections.
12100 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12101 section.
12102
12103 Index Section Contents:
12104 Header
12105 Hash Table of Signatures dwp_hash_table.hash_table
12106 Parallel Table of Indices dwp_hash_table.unit_table
12107 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12108 Table of Section Sizes dwp_hash_table.v2.sizes
12109
12110 The index section header consists of:
12111
12112 V, 32 bit version number
12113 L, 32 bit number of columns in the table of section offsets
12114 N, 32 bit number of compilation units or type units in the index
12115 M, 32 bit number of slots in the hash table
12116
12117 Numbers are recorded using the byte order of the application binary.
12118
12119 The hash table has the same format as version 1.
12120 The parallel table of indices has the same format as version 1,
12121 except that the entries are origin-1 indices into the table of sections
12122 offsets and the table of section sizes.
12123
12124 The table of offsets begins immediately following the parallel table
12125 (at offset 16 + 12 * M from the beginning of the section). The table is
12126 a two-dimensional array of 32-bit words (using the byte order of the
12127 application binary), with L columns and N+1 rows, in row-major order.
12128 Each row in the array is indexed starting from 0. The first row provides
12129 a key to the remaining rows: each column in this row provides an identifier
12130 for a debug section, and the offsets in the same column of subsequent rows
12131 refer to that section. The section identifiers are:
12132
12133 DW_SECT_INFO 1 .debug_info.dwo
12134 DW_SECT_TYPES 2 .debug_types.dwo
12135 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12136 DW_SECT_LINE 4 .debug_line.dwo
12137 DW_SECT_LOC 5 .debug_loc.dwo
12138 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12139 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12140 DW_SECT_MACRO 8 .debug_macro.dwo
12141
12142 The offsets provided by the CU and TU index sections are the base offsets
12143 for the contributions made by each CU or TU to the corresponding section
12144 in the package file. Each CU and TU header contains an abbrev_offset
12145 field, used to find the abbreviations table for that CU or TU within the
12146 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12147 be interpreted as relative to the base offset given in the index section.
12148 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12149 should be interpreted as relative to the base offset for .debug_line.dwo,
12150 and offsets into other debug sections obtained from DWARF attributes should
12151 also be interpreted as relative to the corresponding base offset.
12152
12153 The table of sizes begins immediately following the table of offsets.
12154 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12155 with L columns and N rows, in row-major order. Each row in the array is
12156 indexed starting from 1 (row 0 is shared by the two tables).
12157
12158 ---
12159
12160 Hash table lookup is handled the same in version 1 and 2:
12161
12162 We assume that N and M will not exceed 2^32 - 1.
12163 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12164
d2415c6c
DE
12165 Given a 64-bit compilation unit signature or a type signature S, an entry
12166 in the hash table is located as follows:
80626a55 12167
d2415c6c
DE
12168 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12169 the low-order k bits all set to 1.
80626a55 12170
d2415c6c 12171 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12172
d2415c6c
DE
12173 3) If the hash table entry at index H matches the signature, use that
12174 entry. If the hash table entry at index H is unused (all zeroes),
12175 terminate the search: the signature is not present in the table.
80626a55 12176
d2415c6c 12177 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12178
d2415c6c 12179 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12180 to stop at an unused slot or find the match. */
80626a55
DE
12181
12182/* Create a hash table to map DWO IDs to their CU/TU entry in
12183 .debug_{info,types}.dwo in DWP_FILE.
12184 Returns NULL if there isn't one.
12185 Note: This function processes DWP files only, not DWO files. */
12186
12187static struct dwp_hash_table *
ed2dc618
SM
12188create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12189 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12190{
12191 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12192 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12193 const gdb_byte *index_ptr, *index_end;
80626a55 12194 struct dwarf2_section_info *index;
73869dc2 12195 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12196 struct dwp_hash_table *htab;
12197
12198 if (is_debug_types)
12199 index = &dwp_file->sections.tu_index;
12200 else
12201 index = &dwp_file->sections.cu_index;
12202
12203 if (dwarf2_section_empty_p (index))
12204 return NULL;
12205 dwarf2_read_section (objfile, index);
12206
12207 index_ptr = index->buffer;
12208 index_end = index_ptr + index->size;
12209
12210 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12211 index_ptr += 4;
12212 if (version == 2)
12213 nr_columns = read_4_bytes (dbfd, index_ptr);
12214 else
12215 nr_columns = 0;
12216 index_ptr += 4;
80626a55
DE
12217 nr_units = read_4_bytes (dbfd, index_ptr);
12218 index_ptr += 4;
12219 nr_slots = read_4_bytes (dbfd, index_ptr);
12220 index_ptr += 4;
12221
73869dc2 12222 if (version != 1 && version != 2)
80626a55 12223 {
21aa081e 12224 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12225 " [in module %s]"),
21aa081e 12226 pulongest (version), dwp_file->name);
80626a55
DE
12227 }
12228 if (nr_slots != (nr_slots & -nr_slots))
12229 {
21aa081e 12230 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12231 " is not power of 2 [in module %s]"),
21aa081e 12232 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12233 }
12234
12235 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12236 htab->version = version;
12237 htab->nr_columns = nr_columns;
80626a55
DE
12238 htab->nr_units = nr_units;
12239 htab->nr_slots = nr_slots;
12240 htab->hash_table = index_ptr;
12241 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12242
12243 /* Exit early if the table is empty. */
12244 if (nr_slots == 0 || nr_units == 0
12245 || (version == 2 && nr_columns == 0))
12246 {
12247 /* All must be zero. */
12248 if (nr_slots != 0 || nr_units != 0
12249 || (version == 2 && nr_columns != 0))
12250 {
b98664d3 12251 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12252 " all zero [in modules %s]"),
12253 dwp_file->name);
12254 }
12255 return htab;
12256 }
12257
12258 if (version == 1)
12259 {
12260 htab->section_pool.v1.indices =
12261 htab->unit_table + sizeof (uint32_t) * nr_slots;
12262 /* It's harder to decide whether the section is too small in v1.
12263 V1 is deprecated anyway so we punt. */
12264 }
12265 else
12266 {
12267 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12268 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12269 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12270 /* Reverse map for error checking. */
12271 int ids_seen[DW_SECT_MAX + 1];
12272 int i;
12273
12274 if (nr_columns < 2)
12275 {
12276 error (_("Dwarf Error: bad DWP hash table, too few columns"
12277 " in section table [in module %s]"),
12278 dwp_file->name);
12279 }
12280 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12281 {
12282 error (_("Dwarf Error: bad DWP hash table, too many columns"
12283 " in section table [in module %s]"),
12284 dwp_file->name);
12285 }
04fd5eed
GB
12286 memset (ids, 255, sizeof_ids);
12287 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12288 for (i = 0; i < nr_columns; ++i)
12289 {
12290 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12291
12292 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12293 {
12294 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12295 " in section table [in module %s]"),
12296 id, dwp_file->name);
12297 }
12298 if (ids_seen[id] != -1)
12299 {
12300 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12301 " id %d in section table [in module %s]"),
12302 id, dwp_file->name);
12303 }
12304 ids_seen[id] = i;
12305 ids[i] = id;
12306 }
12307 /* Must have exactly one info or types section. */
12308 if (((ids_seen[DW_SECT_INFO] != -1)
12309 + (ids_seen[DW_SECT_TYPES] != -1))
12310 != 1)
12311 {
12312 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12313 " DWO info/types section [in module %s]"),
12314 dwp_file->name);
12315 }
12316 /* Must have an abbrev section. */
12317 if (ids_seen[DW_SECT_ABBREV] == -1)
12318 {
12319 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12320 " section [in module %s]"),
12321 dwp_file->name);
12322 }
12323 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12324 htab->section_pool.v2.sizes =
12325 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12326 * nr_units * nr_columns);
12327 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12328 * nr_units * nr_columns))
12329 > index_end)
12330 {
12331 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12332 " [in module %s]"),
12333 dwp_file->name);
12334 }
12335 }
80626a55
DE
12336
12337 return htab;
12338}
12339
12340/* Update SECTIONS with the data from SECTP.
12341
12342 This function is like the other "locate" section routines that are
12343 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12344 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12345
12346 The result is non-zero for success, or zero if an error was found. */
12347
12348static int
73869dc2
DE
12349locate_v1_virtual_dwo_sections (asection *sectp,
12350 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12351{
12352 const struct dwop_section_names *names = &dwop_section_names;
12353
12354 if (section_is_p (sectp->name, &names->abbrev_dwo))
12355 {
12356 /* There can be only one. */
049412e3 12357 if (sections->abbrev.s.section != NULL)
80626a55 12358 return 0;
049412e3 12359 sections->abbrev.s.section = sectp;
fd361982 12360 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12361 }
12362 else if (section_is_p (sectp->name, &names->info_dwo)
12363 || section_is_p (sectp->name, &names->types_dwo))
12364 {
12365 /* There can be only one. */
049412e3 12366 if (sections->info_or_types.s.section != NULL)
80626a55 12367 return 0;
049412e3 12368 sections->info_or_types.s.section = sectp;
fd361982 12369 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12370 }
12371 else if (section_is_p (sectp->name, &names->line_dwo))
12372 {
12373 /* There can be only one. */
049412e3 12374 if (sections->line.s.section != NULL)
80626a55 12375 return 0;
049412e3 12376 sections->line.s.section = sectp;
fd361982 12377 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12378 }
12379 else if (section_is_p (sectp->name, &names->loc_dwo))
12380 {
12381 /* There can be only one. */
049412e3 12382 if (sections->loc.s.section != NULL)
80626a55 12383 return 0;
049412e3 12384 sections->loc.s.section = sectp;
fd361982 12385 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12386 }
12387 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12388 {
12389 /* There can be only one. */
049412e3 12390 if (sections->macinfo.s.section != NULL)
80626a55 12391 return 0;
049412e3 12392 sections->macinfo.s.section = sectp;
fd361982 12393 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12394 }
12395 else if (section_is_p (sectp->name, &names->macro_dwo))
12396 {
12397 /* There can be only one. */
049412e3 12398 if (sections->macro.s.section != NULL)
80626a55 12399 return 0;
049412e3 12400 sections->macro.s.section = sectp;
fd361982 12401 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12402 }
12403 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12404 {
12405 /* There can be only one. */
049412e3 12406 if (sections->str_offsets.s.section != NULL)
80626a55 12407 return 0;
049412e3 12408 sections->str_offsets.s.section = sectp;
fd361982 12409 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12410 }
12411 else
12412 {
12413 /* No other kind of section is valid. */
12414 return 0;
12415 }
12416
12417 return 1;
12418}
12419
73869dc2
DE
12420/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12421 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12422 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12423 This is for DWP version 1 files. */
80626a55
DE
12424
12425static struct dwo_unit *
ed2dc618
SM
12426create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12427 struct dwp_file *dwp_file,
73869dc2
DE
12428 uint32_t unit_index,
12429 const char *comp_dir,
12430 ULONGEST signature, int is_debug_types)
80626a55
DE
12431{
12432 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12433 const struct dwp_hash_table *dwp_htab =
12434 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12435 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12436 const char *kind = is_debug_types ? "TU" : "CU";
12437 struct dwo_file *dwo_file;
12438 struct dwo_unit *dwo_unit;
73869dc2 12439 struct virtual_v1_dwo_sections sections;
80626a55 12440 void **dwo_file_slot;
80626a55
DE
12441 int i;
12442
73869dc2
DE
12443 gdb_assert (dwp_file->version == 1);
12444
b4f54984 12445 if (dwarf_read_debug)
80626a55 12446 {
73869dc2 12447 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12448 kind,
73869dc2 12449 pulongest (unit_index), hex_string (signature),
80626a55
DE
12450 dwp_file->name);
12451 }
12452
19ac8c2e 12453 /* Fetch the sections of this DWO unit.
80626a55
DE
12454 Put a limit on the number of sections we look for so that bad data
12455 doesn't cause us to loop forever. */
12456
73869dc2 12457#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12458 (1 /* .debug_info or .debug_types */ \
12459 + 1 /* .debug_abbrev */ \
12460 + 1 /* .debug_line */ \
12461 + 1 /* .debug_loc */ \
12462 + 1 /* .debug_str_offsets */ \
19ac8c2e 12463 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12464 + 1 /* trailing zero */)
12465
12466 memset (&sections, 0, sizeof (sections));
80626a55 12467
73869dc2 12468 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12469 {
12470 asection *sectp;
12471 uint32_t section_nr =
12472 read_4_bytes (dbfd,
73869dc2
DE
12473 dwp_htab->section_pool.v1.indices
12474 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12475
12476 if (section_nr == 0)
12477 break;
12478 if (section_nr >= dwp_file->num_sections)
12479 {
12480 error (_("Dwarf Error: bad DWP hash table, section number too large"
12481 " [in module %s]"),
12482 dwp_file->name);
12483 }
12484
12485 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12486 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12487 {
12488 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12489 " [in module %s]"),
12490 dwp_file->name);
12491 }
12492 }
12493
12494 if (i < 2
a32a8923
DE
12495 || dwarf2_section_empty_p (&sections.info_or_types)
12496 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12497 {
12498 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12499 " [in module %s]"),
12500 dwp_file->name);
12501 }
73869dc2 12502 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12503 {
12504 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12505 " [in module %s]"),
12506 dwp_file->name);
12507 }
12508
12509 /* It's easier for the rest of the code if we fake a struct dwo_file and
12510 have dwo_unit "live" in that. At least for now.
12511
12512 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12513 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12514 file, we can combine them back into a virtual DWO file to save space
12515 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12516 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12517
791afaa2
TT
12518 std::string virtual_dwo_name =
12519 string_printf ("virtual-dwo/%d-%d-%d-%d",
12520 get_section_id (&sections.abbrev),
12521 get_section_id (&sections.line),
12522 get_section_id (&sections.loc),
12523 get_section_id (&sections.str_offsets));
80626a55 12524 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12525 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12526 virtual_dwo_name.c_str (),
12527 comp_dir);
80626a55
DE
12528 /* Create one if necessary. */
12529 if (*dwo_file_slot == NULL)
12530 {
b4f54984 12531 if (dwarf_read_debug)
80626a55
DE
12532 {
12533 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12534 virtual_dwo_name.c_str ());
80626a55 12535 }
51ac9db5 12536 dwo_file = new struct dwo_file;
efba19b0
TT
12537 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12538 virtual_dwo_name);
0ac5b59e 12539 dwo_file->comp_dir = comp_dir;
80626a55
DE
12540 dwo_file->sections.abbrev = sections.abbrev;
12541 dwo_file->sections.line = sections.line;
12542 dwo_file->sections.loc = sections.loc;
12543 dwo_file->sections.macinfo = sections.macinfo;
12544 dwo_file->sections.macro = sections.macro;
12545 dwo_file->sections.str_offsets = sections.str_offsets;
12546 /* The "str" section is global to the entire DWP file. */
12547 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12548 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12549 there's no need to record it in dwo_file.
12550 Also, we can't simply record type sections in dwo_file because
12551 we record a pointer into the vector in dwo_unit. As we collect more
12552 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12553 for it, invalidating all copies of pointers into the previous
12554 contents. */
80626a55
DE
12555 *dwo_file_slot = dwo_file;
12556 }
12557 else
12558 {
b4f54984 12559 if (dwarf_read_debug)
80626a55
DE
12560 {
12561 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12562 virtual_dwo_name.c_str ());
80626a55 12563 }
9a3c8263 12564 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12565 }
80626a55
DE
12566
12567 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12568 dwo_unit->dwo_file = dwo_file;
12569 dwo_unit->signature = signature;
8d749320
SM
12570 dwo_unit->section =
12571 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12572 *dwo_unit->section = sections.info_or_types;
57d63ce2 12573 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12574
12575 return dwo_unit;
12576}
12577
73869dc2
DE
12578/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12579 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12580 piece within that section used by a TU/CU, return a virtual section
12581 of just that piece. */
12582
12583static struct dwarf2_section_info
ed2dc618
SM
12584create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12585 struct dwarf2_section_info *section,
73869dc2
DE
12586 bfd_size_type offset, bfd_size_type size)
12587{
12588 struct dwarf2_section_info result;
12589 asection *sectp;
12590
12591 gdb_assert (section != NULL);
12592 gdb_assert (!section->is_virtual);
12593
12594 memset (&result, 0, sizeof (result));
12595 result.s.containing_section = section;
dc4ccb6f 12596 result.is_virtual = true;
73869dc2
DE
12597
12598 if (size == 0)
12599 return result;
12600
12601 sectp = get_section_bfd_section (section);
12602
12603 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12604 bounds of the real section. This is a pretty-rare event, so just
12605 flag an error (easier) instead of a warning and trying to cope. */
12606 if (sectp == NULL
fd361982 12607 || offset + size > bfd_section_size (sectp))
73869dc2 12608 {
73869dc2
DE
12609 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12610 " in section %s [in module %s]"),
fd361982 12611 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12612 objfile_name (dwarf2_per_objfile->objfile));
12613 }
12614
12615 result.virtual_offset = offset;
12616 result.size = size;
12617 return result;
12618}
12619
12620/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12621 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12622 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12623 This is for DWP version 2 files. */
12624
12625static struct dwo_unit *
ed2dc618
SM
12626create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12627 struct dwp_file *dwp_file,
73869dc2
DE
12628 uint32_t unit_index,
12629 const char *comp_dir,
12630 ULONGEST signature, int is_debug_types)
12631{
12632 struct objfile *objfile = dwarf2_per_objfile->objfile;
12633 const struct dwp_hash_table *dwp_htab =
12634 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12635 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12636 const char *kind = is_debug_types ? "TU" : "CU";
12637 struct dwo_file *dwo_file;
12638 struct dwo_unit *dwo_unit;
12639 struct virtual_v2_dwo_sections sections;
12640 void **dwo_file_slot;
73869dc2
DE
12641 int i;
12642
12643 gdb_assert (dwp_file->version == 2);
12644
b4f54984 12645 if (dwarf_read_debug)
73869dc2
DE
12646 {
12647 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12648 kind,
12649 pulongest (unit_index), hex_string (signature),
12650 dwp_file->name);
12651 }
12652
12653 /* Fetch the section offsets of this DWO unit. */
12654
12655 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12656
12657 for (i = 0; i < dwp_htab->nr_columns; ++i)
12658 {
12659 uint32_t offset = read_4_bytes (dbfd,
12660 dwp_htab->section_pool.v2.offsets
12661 + (((unit_index - 1) * dwp_htab->nr_columns
12662 + i)
12663 * sizeof (uint32_t)));
12664 uint32_t size = read_4_bytes (dbfd,
12665 dwp_htab->section_pool.v2.sizes
12666 + (((unit_index - 1) * dwp_htab->nr_columns
12667 + i)
12668 * sizeof (uint32_t)));
12669
12670 switch (dwp_htab->section_pool.v2.section_ids[i])
12671 {
12672 case DW_SECT_INFO:
12673 case DW_SECT_TYPES:
12674 sections.info_or_types_offset = offset;
12675 sections.info_or_types_size = size;
12676 break;
12677 case DW_SECT_ABBREV:
12678 sections.abbrev_offset = offset;
12679 sections.abbrev_size = size;
12680 break;
12681 case DW_SECT_LINE:
12682 sections.line_offset = offset;
12683 sections.line_size = size;
12684 break;
12685 case DW_SECT_LOC:
12686 sections.loc_offset = offset;
12687 sections.loc_size = size;
12688 break;
12689 case DW_SECT_STR_OFFSETS:
12690 sections.str_offsets_offset = offset;
12691 sections.str_offsets_size = size;
12692 break;
12693 case DW_SECT_MACINFO:
12694 sections.macinfo_offset = offset;
12695 sections.macinfo_size = size;
12696 break;
12697 case DW_SECT_MACRO:
12698 sections.macro_offset = offset;
12699 sections.macro_size = size;
12700 break;
12701 }
12702 }
12703
12704 /* It's easier for the rest of the code if we fake a struct dwo_file and
12705 have dwo_unit "live" in that. At least for now.
12706
12707 The DWP file can be made up of a random collection of CUs and TUs.
12708 However, for each CU + set of TUs that came from the same original DWO
12709 file, we can combine them back into a virtual DWO file to save space
12710 (fewer struct dwo_file objects to allocate). Remember that for really
12711 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12712
791afaa2
TT
12713 std::string virtual_dwo_name =
12714 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12715 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12716 (long) (sections.line_size ? sections.line_offset : 0),
12717 (long) (sections.loc_size ? sections.loc_offset : 0),
12718 (long) (sections.str_offsets_size
12719 ? sections.str_offsets_offset : 0));
73869dc2 12720 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12721 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12722 virtual_dwo_name.c_str (),
12723 comp_dir);
73869dc2
DE
12724 /* Create one if necessary. */
12725 if (*dwo_file_slot == NULL)
12726 {
b4f54984 12727 if (dwarf_read_debug)
73869dc2
DE
12728 {
12729 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12730 virtual_dwo_name.c_str ());
73869dc2 12731 }
51ac9db5 12732 dwo_file = new struct dwo_file;
efba19b0
TT
12733 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12734 virtual_dwo_name);
73869dc2
DE
12735 dwo_file->comp_dir = comp_dir;
12736 dwo_file->sections.abbrev =
ed2dc618 12737 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12738 sections.abbrev_offset, sections.abbrev_size);
12739 dwo_file->sections.line =
ed2dc618 12740 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12741 sections.line_offset, sections.line_size);
12742 dwo_file->sections.loc =
ed2dc618 12743 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12744 sections.loc_offset, sections.loc_size);
12745 dwo_file->sections.macinfo =
ed2dc618 12746 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12747 sections.macinfo_offset, sections.macinfo_size);
12748 dwo_file->sections.macro =
ed2dc618 12749 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12750 sections.macro_offset, sections.macro_size);
12751 dwo_file->sections.str_offsets =
ed2dc618
SM
12752 create_dwp_v2_section (dwarf2_per_objfile,
12753 &dwp_file->sections.str_offsets,
73869dc2
DE
12754 sections.str_offsets_offset,
12755 sections.str_offsets_size);
12756 /* The "str" section is global to the entire DWP file. */
12757 dwo_file->sections.str = dwp_file->sections.str;
12758 /* The info or types section is assigned below to dwo_unit,
12759 there's no need to record it in dwo_file.
12760 Also, we can't simply record type sections in dwo_file because
12761 we record a pointer into the vector in dwo_unit. As we collect more
12762 types we'll grow the vector and eventually have to reallocate space
12763 for it, invalidating all copies of pointers into the previous
12764 contents. */
12765 *dwo_file_slot = dwo_file;
12766 }
12767 else
12768 {
b4f54984 12769 if (dwarf_read_debug)
73869dc2
DE
12770 {
12771 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12772 virtual_dwo_name.c_str ());
73869dc2 12773 }
9a3c8263 12774 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12775 }
73869dc2
DE
12776
12777 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12778 dwo_unit->dwo_file = dwo_file;
12779 dwo_unit->signature = signature;
8d749320
SM
12780 dwo_unit->section =
12781 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12782 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12783 is_debug_types
73869dc2
DE
12784 ? &dwp_file->sections.types
12785 : &dwp_file->sections.info,
12786 sections.info_or_types_offset,
12787 sections.info_or_types_size);
12788 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12789
12790 return dwo_unit;
12791}
12792
57d63ce2
DE
12793/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12794 Returns NULL if the signature isn't found. */
80626a55
DE
12795
12796static struct dwo_unit *
ed2dc618
SM
12797lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12798 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12799 ULONGEST signature, int is_debug_types)
80626a55 12800{
57d63ce2
DE
12801 const struct dwp_hash_table *dwp_htab =
12802 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12803 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12804 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12805 uint32_t hash = signature & mask;
12806 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12807 unsigned int i;
12808 void **slot;
870f88f7 12809 struct dwo_unit find_dwo_cu;
80626a55
DE
12810
12811 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12812 find_dwo_cu.signature = signature;
19ac8c2e
DE
12813 slot = htab_find_slot (is_debug_types
12814 ? dwp_file->loaded_tus
12815 : dwp_file->loaded_cus,
12816 &find_dwo_cu, INSERT);
80626a55
DE
12817
12818 if (*slot != NULL)
9a3c8263 12819 return (struct dwo_unit *) *slot;
80626a55
DE
12820
12821 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12822 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12823 {
12824 ULONGEST signature_in_table;
12825
12826 signature_in_table =
57d63ce2 12827 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12828 if (signature_in_table == signature)
12829 {
57d63ce2
DE
12830 uint32_t unit_index =
12831 read_4_bytes (dbfd,
12832 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12833
73869dc2
DE
12834 if (dwp_file->version == 1)
12835 {
ed2dc618
SM
12836 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12837 dwp_file, unit_index,
73869dc2
DE
12838 comp_dir, signature,
12839 is_debug_types);
12840 }
12841 else
12842 {
ed2dc618
SM
12843 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12844 dwp_file, unit_index,
73869dc2
DE
12845 comp_dir, signature,
12846 is_debug_types);
12847 }
9a3c8263 12848 return (struct dwo_unit *) *slot;
80626a55
DE
12849 }
12850 if (signature_in_table == 0)
12851 return NULL;
12852 hash = (hash + hash2) & mask;
12853 }
12854
12855 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12856 " [in module %s]"),
12857 dwp_file->name);
12858}
12859
ab5088bf 12860/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12861 Open the file specified by FILE_NAME and hand it off to BFD for
12862 preliminary analysis. Return a newly initialized bfd *, which
12863 includes a canonicalized copy of FILE_NAME.
80626a55 12864 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12865 SEARCH_CWD is true if the current directory is to be searched.
12866 It will be searched before debug-file-directory.
13aaf454
DE
12867 If successful, the file is added to the bfd include table of the
12868 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12869 If unable to find/open the file, return NULL.
3019eac3
DE
12870 NOTE: This function is derived from symfile_bfd_open. */
12871
192b62ce 12872static gdb_bfd_ref_ptr
ed2dc618
SM
12873try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12874 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12875{
24b9144d 12876 int desc;
9c02c129
DE
12877 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12878 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12879 to debug_file_directory. */
e0cc99a6 12880 const char *search_path;
9c02c129
DE
12881 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12882
e0cc99a6 12883 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12884 if (search_cwd)
12885 {
12886 if (*debug_file_directory != '\0')
e0cc99a6
TT
12887 {
12888 search_path_holder.reset (concat (".", dirname_separator_string,
12889 debug_file_directory,
12890 (char *) NULL));
12891 search_path = search_path_holder.get ();
12892 }
6ac97d4c 12893 else
e0cc99a6 12894 search_path = ".";
6ac97d4c 12895 }
9c02c129 12896 else
e0cc99a6 12897 search_path = debug_file_directory;
3019eac3 12898
24b9144d 12899 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12900 if (is_dwp)
12901 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12902
12903 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12904 desc = openp (search_path, flags, file_name,
3019eac3
DE
12905 O_RDONLY | O_BINARY, &absolute_name);
12906 if (desc < 0)
12907 return NULL;
12908
e0cc99a6
TT
12909 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12910 gnutarget, desc));
9c02c129
DE
12911 if (sym_bfd == NULL)
12912 return NULL;
192b62ce 12913 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12914
192b62ce
TT
12915 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12916 return NULL;
3019eac3 12917
13aaf454
DE
12918 /* Success. Record the bfd as having been included by the objfile's bfd.
12919 This is important because things like demangled_names_hash lives in the
12920 objfile's per_bfd space and may have references to things like symbol
12921 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12922 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12923
3019eac3
DE
12924 return sym_bfd;
12925}
12926
ab5088bf 12927/* Try to open DWO file FILE_NAME.
3019eac3
DE
12928 COMP_DIR is the DW_AT_comp_dir attribute.
12929 The result is the bfd handle of the file.
12930 If there is a problem finding or opening the file, return NULL.
12931 Upon success, the canonicalized path of the file is stored in the bfd,
12932 same as symfile_bfd_open. */
12933
192b62ce 12934static gdb_bfd_ref_ptr
ed2dc618
SM
12935open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12936 const char *file_name, const char *comp_dir)
3019eac3 12937{
80626a55 12938 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12939 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12940 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12941
12942 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12943
12944 if (comp_dir != NULL)
12945 {
43816ebc
TT
12946 gdb::unique_xmalloc_ptr<char> path_to_try
12947 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12948
12949 /* NOTE: If comp_dir is a relative path, this will also try the
12950 search path, which seems useful. */
ed2dc618 12951 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12952 path_to_try.get (),
ed2dc618 12953 0 /*is_dwp*/,
192b62ce 12954 1 /*search_cwd*/));
3019eac3
DE
12955 if (abfd != NULL)
12956 return abfd;
12957 }
12958
12959 /* That didn't work, try debug-file-directory, which, despite its name,
12960 is a list of paths. */
12961
12962 if (*debug_file_directory == '\0')
12963 return NULL;
12964
ed2dc618
SM
12965 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12966 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12967}
12968
80626a55
DE
12969/* This function is mapped across the sections and remembers the offset and
12970 size of each of the DWO debugging sections we are interested in. */
12971
12972static void
12973dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12974{
9a3c8263 12975 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12976 const struct dwop_section_names *names = &dwop_section_names;
12977
12978 if (section_is_p (sectp->name, &names->abbrev_dwo))
12979 {
049412e3 12980 dwo_sections->abbrev.s.section = sectp;
fd361982 12981 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12982 }
12983 else if (section_is_p (sectp->name, &names->info_dwo))
12984 {
049412e3 12985 dwo_sections->info.s.section = sectp;
fd361982 12986 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12987 }
12988 else if (section_is_p (sectp->name, &names->line_dwo))
12989 {
049412e3 12990 dwo_sections->line.s.section = sectp;
fd361982 12991 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12992 }
12993 else if (section_is_p (sectp->name, &names->loc_dwo))
12994 {
049412e3 12995 dwo_sections->loc.s.section = sectp;
fd361982 12996 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12997 }
12998 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12999 {
049412e3 13000 dwo_sections->macinfo.s.section = sectp;
fd361982 13001 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
13002 }
13003 else if (section_is_p (sectp->name, &names->macro_dwo))
13004 {
049412e3 13005 dwo_sections->macro.s.section = sectp;
fd361982 13006 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
13007 }
13008 else if (section_is_p (sectp->name, &names->str_dwo))
13009 {
049412e3 13010 dwo_sections->str.s.section = sectp;
fd361982 13011 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
13012 }
13013 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13014 {
049412e3 13015 dwo_sections->str_offsets.s.section = sectp;
fd361982 13016 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
13017 }
13018 else if (section_is_p (sectp->name, &names->types_dwo))
13019 {
13020 struct dwarf2_section_info type_section;
13021
13022 memset (&type_section, 0, sizeof (type_section));
049412e3 13023 type_section.s.section = sectp;
fd361982 13024 type_section.size = bfd_section_size (sectp);
fd5866f6 13025 dwo_sections->types.push_back (type_section);
80626a55
DE
13026 }
13027}
13028
ab5088bf 13029/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13030 by PER_CU. This is for the non-DWP case.
80626a55 13031 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13032
13033static struct dwo_file *
0ac5b59e
DE
13034open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13035 const char *dwo_name, const char *comp_dir)
3019eac3 13036{
ed2dc618 13037 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13038
fb1eb2f9 13039 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13040 if (dbfd == NULL)
13041 {
b4f54984 13042 if (dwarf_read_debug)
80626a55
DE
13043 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13044 return NULL;
13045 }
263db9a1 13046
51ac9db5 13047 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13048 dwo_file->dwo_name = dwo_name;
13049 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13050 dwo_file->dbfd = std::move (dbfd);
3019eac3 13051
fb1eb2f9 13052 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13053 &dwo_file->sections);
3019eac3 13054
ed2dc618
SM
13055 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13056 dwo_file->cus);
3019eac3 13057
263db9a1 13058 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13059 dwo_file->sections.types, dwo_file->tus);
3019eac3 13060
b4f54984 13061 if (dwarf_read_debug)
80626a55
DE
13062 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13063
263db9a1 13064 return dwo_file.release ();
3019eac3
DE
13065}
13066
80626a55 13067/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13068 size of each of the DWP debugging sections common to version 1 and 2 that
13069 we are interested in. */
3019eac3 13070
80626a55 13071static void
73869dc2
DE
13072dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13073 void *dwp_file_ptr)
3019eac3 13074{
9a3c8263 13075 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13076 const struct dwop_section_names *names = &dwop_section_names;
13077 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13078
80626a55 13079 /* Record the ELF section number for later lookup: this is what the
73869dc2 13080 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13081 gdb_assert (elf_section_nr < dwp_file->num_sections);
13082 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13083
80626a55
DE
13084 /* Look for specific sections that we need. */
13085 if (section_is_p (sectp->name, &names->str_dwo))
13086 {
049412e3 13087 dwp_file->sections.str.s.section = sectp;
fd361982 13088 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13089 }
13090 else if (section_is_p (sectp->name, &names->cu_index))
13091 {
049412e3 13092 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13093 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13094 }
13095 else if (section_is_p (sectp->name, &names->tu_index))
13096 {
049412e3 13097 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13098 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13099 }
13100}
3019eac3 13101
73869dc2
DE
13102/* This function is mapped across the sections and remembers the offset and
13103 size of each of the DWP version 2 debugging sections that we are interested
13104 in. This is split into a separate function because we don't know if we
13105 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13106
13107static void
13108dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13109{
9a3c8263 13110 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13111 const struct dwop_section_names *names = &dwop_section_names;
13112 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13113
13114 /* Record the ELF section number for later lookup: this is what the
13115 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13116 gdb_assert (elf_section_nr < dwp_file->num_sections);
13117 dwp_file->elf_sections[elf_section_nr] = sectp;
13118
13119 /* Look for specific sections that we need. */
13120 if (section_is_p (sectp->name, &names->abbrev_dwo))
13121 {
049412e3 13122 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13123 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13124 }
13125 else if (section_is_p (sectp->name, &names->info_dwo))
13126 {
049412e3 13127 dwp_file->sections.info.s.section = sectp;
fd361982 13128 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13129 }
13130 else if (section_is_p (sectp->name, &names->line_dwo))
13131 {
049412e3 13132 dwp_file->sections.line.s.section = sectp;
fd361982 13133 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13134 }
13135 else if (section_is_p (sectp->name, &names->loc_dwo))
13136 {
049412e3 13137 dwp_file->sections.loc.s.section = sectp;
fd361982 13138 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13139 }
13140 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13141 {
049412e3 13142 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13143 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13144 }
13145 else if (section_is_p (sectp->name, &names->macro_dwo))
13146 {
049412e3 13147 dwp_file->sections.macro.s.section = sectp;
fd361982 13148 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13149 }
13150 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13151 {
049412e3 13152 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13153 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13154 }
13155 else if (section_is_p (sectp->name, &names->types_dwo))
13156 {
049412e3 13157 dwp_file->sections.types.s.section = sectp;
fd361982 13158 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13159 }
13160}
13161
80626a55 13162/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13163
80626a55
DE
13164static hashval_t
13165hash_dwp_loaded_cutus (const void *item)
13166{
9a3c8263 13167 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13168
80626a55
DE
13169 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13170 return dwo_unit->signature;
3019eac3
DE
13171}
13172
80626a55 13173/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13174
80626a55
DE
13175static int
13176eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13177{
9a3c8263
SM
13178 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13179 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13180
80626a55
DE
13181 return dua->signature == dub->signature;
13182}
3019eac3 13183
80626a55 13184/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13185
80626a55
DE
13186static htab_t
13187allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13188{
13189 return htab_create_alloc_ex (3,
13190 hash_dwp_loaded_cutus,
13191 eq_dwp_loaded_cutus,
13192 NULL,
13193 &objfile->objfile_obstack,
13194 hashtab_obstack_allocate,
13195 dummy_obstack_deallocate);
13196}
3019eac3 13197
ab5088bf
DE
13198/* Try to open DWP file FILE_NAME.
13199 The result is the bfd handle of the file.
13200 If there is a problem finding or opening the file, return NULL.
13201 Upon success, the canonicalized path of the file is stored in the bfd,
13202 same as symfile_bfd_open. */
13203
192b62ce 13204static gdb_bfd_ref_ptr
ed2dc618
SM
13205open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13206 const char *file_name)
ab5088bf 13207{
ed2dc618
SM
13208 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13209 1 /*is_dwp*/,
192b62ce 13210 1 /*search_cwd*/));
6ac97d4c
DE
13211 if (abfd != NULL)
13212 return abfd;
13213
13214 /* Work around upstream bug 15652.
13215 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13216 [Whether that's a "bug" is debatable, but it is getting in our way.]
13217 We have no real idea where the dwp file is, because gdb's realpath-ing
13218 of the executable's path may have discarded the needed info.
13219 [IWBN if the dwp file name was recorded in the executable, akin to
13220 .gnu_debuglink, but that doesn't exist yet.]
13221 Strip the directory from FILE_NAME and search again. */
13222 if (*debug_file_directory != '\0')
13223 {
13224 /* Don't implicitly search the current directory here.
13225 If the user wants to search "." to handle this case,
13226 it must be added to debug-file-directory. */
ed2dc618
SM
13227 return try_open_dwop_file (dwarf2_per_objfile,
13228 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13229 0 /*search_cwd*/);
13230 }
13231
13232 return NULL;
ab5088bf
DE
13233}
13234
80626a55
DE
13235/* Initialize the use of the DWP file for the current objfile.
13236 By convention the name of the DWP file is ${objfile}.dwp.
13237 The result is NULL if it can't be found. */
a766d390 13238
400174b1 13239static std::unique_ptr<struct dwp_file>
ed2dc618 13240open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13241{
13242 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13243
82bf32bc
JK
13244 /* Try to find first .dwp for the binary file before any symbolic links
13245 resolving. */
6c447423
DE
13246
13247 /* If the objfile is a debug file, find the name of the real binary
13248 file and get the name of dwp file from there. */
d721ba37 13249 std::string dwp_name;
6c447423
DE
13250 if (objfile->separate_debug_objfile_backlink != NULL)
13251 {
13252 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13253 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13254
d721ba37 13255 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13256 }
13257 else
d721ba37
PA
13258 dwp_name = objfile->original_name;
13259
13260 dwp_name += ".dwp";
80626a55 13261
ed2dc618 13262 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13263 if (dbfd == NULL
13264 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13265 {
13266 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13267 dwp_name = objfile_name (objfile);
13268 dwp_name += ".dwp";
ed2dc618 13269 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13270 }
13271
80626a55
DE
13272 if (dbfd == NULL)
13273 {
b4f54984 13274 if (dwarf_read_debug)
d721ba37 13275 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13276 return std::unique_ptr<dwp_file> ();
3019eac3 13277 }
400174b1
TT
13278
13279 const char *name = bfd_get_filename (dbfd.get ());
13280 std::unique_ptr<struct dwp_file> dwp_file
13281 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13282
0a0f4c01 13283 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13284 dwp_file->elf_sections =
13285 OBSTACK_CALLOC (&objfile->objfile_obstack,
13286 dwp_file->num_sections, asection *);
13287
400174b1
TT
13288 bfd_map_over_sections (dwp_file->dbfd.get (),
13289 dwarf2_locate_common_dwp_sections,
13290 dwp_file.get ());
80626a55 13291
400174b1
TT
13292 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13293 0);
80626a55 13294
400174b1
TT
13295 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13296 1);
80626a55 13297
73869dc2 13298 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13299 if (dwp_file->cus && dwp_file->tus
13300 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13301 {
13302 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13303 pretty bizarre. We use pulongest here because that's the established
4d65956b 13304 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13305 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13306 " TU version %s [in DWP file %s]"),
13307 pulongest (dwp_file->cus->version),
d721ba37 13308 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13309 }
08302ed2
DE
13310
13311 if (dwp_file->cus)
13312 dwp_file->version = dwp_file->cus->version;
13313 else if (dwp_file->tus)
13314 dwp_file->version = dwp_file->tus->version;
13315 else
13316 dwp_file->version = 2;
73869dc2
DE
13317
13318 if (dwp_file->version == 2)
400174b1
TT
13319 bfd_map_over_sections (dwp_file->dbfd.get (),
13320 dwarf2_locate_v2_dwp_sections,
13321 dwp_file.get ());
73869dc2 13322
19ac8c2e
DE
13323 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13324 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13325
b4f54984 13326 if (dwarf_read_debug)
80626a55
DE
13327 {
13328 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13329 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13330 " %s CUs, %s TUs\n",
13331 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13332 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13333 }
13334
13335 return dwp_file;
3019eac3 13336}
c906108c 13337
ab5088bf
DE
13338/* Wrapper around open_and_init_dwp_file, only open it once. */
13339
13340static struct dwp_file *
ed2dc618 13341get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13342{
13343 if (! dwarf2_per_objfile->dwp_checked)
13344 {
ed2dc618
SM
13345 dwarf2_per_objfile->dwp_file
13346 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13347 dwarf2_per_objfile->dwp_checked = 1;
13348 }
400174b1 13349 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13350}
13351
80626a55
DE
13352/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13353 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13354 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13355 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13356 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13357
13358 This is called, for example, when wanting to read a variable with a
13359 complex location. Therefore we don't want to do file i/o for every call.
13360 Therefore we don't want to look for a DWO file on every call.
13361 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13362 then we check if we've already seen DWO_NAME, and only THEN do we check
13363 for a DWO file.
13364
1c658ad5 13365 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13366 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13367
3019eac3 13368static struct dwo_unit *
80626a55
DE
13369lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13370 const char *dwo_name, const char *comp_dir,
13371 ULONGEST signature, int is_debug_types)
3019eac3 13372{
ed2dc618 13373 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13374 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13375 const char *kind = is_debug_types ? "TU" : "CU";
13376 void **dwo_file_slot;
3019eac3 13377 struct dwo_file *dwo_file;
80626a55 13378 struct dwp_file *dwp_file;
cb1df416 13379
6a506a2d
DE
13380 /* First see if there's a DWP file.
13381 If we have a DWP file but didn't find the DWO inside it, don't
13382 look for the original DWO file. It makes gdb behave differently
13383 depending on whether one is debugging in the build tree. */
cf2c3c16 13384
ed2dc618 13385 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13386 if (dwp_file != NULL)
cf2c3c16 13387 {
80626a55
DE
13388 const struct dwp_hash_table *dwp_htab =
13389 is_debug_types ? dwp_file->tus : dwp_file->cus;
13390
13391 if (dwp_htab != NULL)
13392 {
13393 struct dwo_unit *dwo_cutu =
ed2dc618 13394 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13395 signature, is_debug_types);
80626a55
DE
13396
13397 if (dwo_cutu != NULL)
13398 {
b4f54984 13399 if (dwarf_read_debug)
80626a55
DE
13400 {
13401 fprintf_unfiltered (gdb_stdlog,
13402 "Virtual DWO %s %s found: @%s\n",
13403 kind, hex_string (signature),
13404 host_address_to_string (dwo_cutu));
13405 }
13406 return dwo_cutu;
13407 }
13408 }
13409 }
6a506a2d 13410 else
80626a55 13411 {
6a506a2d 13412 /* No DWP file, look for the DWO file. */
80626a55 13413
ed2dc618
SM
13414 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13415 dwo_name, comp_dir);
6a506a2d 13416 if (*dwo_file_slot == NULL)
80626a55 13417 {
6a506a2d
DE
13418 /* Read in the file and build a table of the CUs/TUs it contains. */
13419 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13420 }
6a506a2d 13421 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13422 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13423
6a506a2d 13424 if (dwo_file != NULL)
19c3d4c9 13425 {
6a506a2d
DE
13426 struct dwo_unit *dwo_cutu = NULL;
13427
13428 if (is_debug_types && dwo_file->tus)
13429 {
13430 struct dwo_unit find_dwo_cutu;
13431
13432 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13433 find_dwo_cutu.signature = signature;
9a3c8263
SM
13434 dwo_cutu
13435 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13436 }
33c5cd75 13437 else if (!is_debug_types && dwo_file->cus)
80626a55 13438 {
33c5cd75
DB
13439 struct dwo_unit find_dwo_cutu;
13440
13441 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13442 find_dwo_cutu.signature = signature;
13443 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13444 &find_dwo_cutu);
6a506a2d
DE
13445 }
13446
13447 if (dwo_cutu != NULL)
13448 {
b4f54984 13449 if (dwarf_read_debug)
6a506a2d
DE
13450 {
13451 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13452 kind, dwo_name, hex_string (signature),
13453 host_address_to_string (dwo_cutu));
13454 }
13455 return dwo_cutu;
80626a55
DE
13456 }
13457 }
2e276125 13458 }
9cdd5dbd 13459
80626a55
DE
13460 /* We didn't find it. This could mean a dwo_id mismatch, or
13461 someone deleted the DWO/DWP file, or the search path isn't set up
13462 correctly to find the file. */
13463
b4f54984 13464 if (dwarf_read_debug)
80626a55
DE
13465 {
13466 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13467 kind, dwo_name, hex_string (signature));
13468 }
3019eac3 13469
6656a72d
DE
13470 /* This is a warning and not a complaint because it can be caused by
13471 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13472 {
13473 /* Print the name of the DWP file if we looked there, helps the user
13474 better diagnose the problem. */
791afaa2 13475 std::string dwp_text;
43942612
DE
13476
13477 if (dwp_file != NULL)
791afaa2
TT
13478 dwp_text = string_printf (" [in DWP file %s]",
13479 lbasename (dwp_file->name));
43942612 13480
9d8780f0 13481 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13482 " [in module %s]"),
13483 kind, dwo_name, hex_string (signature),
791afaa2 13484 dwp_text.c_str (),
43942612 13485 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13486 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13487 }
3019eac3 13488 return NULL;
5fb290d7
DJ
13489}
13490
80626a55
DE
13491/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13492 See lookup_dwo_cutu_unit for details. */
13493
13494static struct dwo_unit *
13495lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13496 const char *dwo_name, const char *comp_dir,
13497 ULONGEST signature)
13498{
13499 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13500}
13501
13502/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13503 See lookup_dwo_cutu_unit for details. */
13504
13505static struct dwo_unit *
13506lookup_dwo_type_unit (struct signatured_type *this_tu,
13507 const char *dwo_name, const char *comp_dir)
13508{
13509 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13510}
13511
89e63ee4
DE
13512/* Traversal function for queue_and_load_all_dwo_tus. */
13513
13514static int
13515queue_and_load_dwo_tu (void **slot, void *info)
13516{
13517 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13518 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13519 ULONGEST signature = dwo_unit->signature;
13520 struct signatured_type *sig_type =
13521 lookup_dwo_signatured_type (per_cu->cu, signature);
13522
13523 if (sig_type != NULL)
13524 {
13525 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13526
13527 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13528 a real dependency of PER_CU on SIG_TYPE. That is detected later
13529 while processing PER_CU. */
13530 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13531 load_full_type_unit (sig_cu);
ae640021 13532 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13533 }
13534
13535 return 1;
13536}
13537
13538/* Queue all TUs contained in the DWO of PER_CU to be read in.
13539 The DWO may have the only definition of the type, though it may not be
13540 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13541 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13542
13543static void
13544queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13545{
13546 struct dwo_unit *dwo_unit;
13547 struct dwo_file *dwo_file;
13548
13549 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13550 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13551 gdb_assert (per_cu->cu != NULL);
13552
13553 dwo_unit = per_cu->cu->dwo_unit;
13554 gdb_assert (dwo_unit != NULL);
13555
13556 dwo_file = dwo_unit->dwo_file;
13557 if (dwo_file->tus != NULL)
13558 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13559}
13560
3019eac3 13561/* Read in various DIEs. */
348e048f 13562
d389af10 13563/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13564 Inherit only the children of the DW_AT_abstract_origin DIE not being
13565 already referenced by DW_AT_abstract_origin from the children of the
13566 current DIE. */
d389af10
JK
13567
13568static void
13569inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13570{
13571 struct die_info *child_die;
791afaa2 13572 sect_offset *offsetp;
d389af10
JK
13573 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13574 struct die_info *origin_die;
13575 /* Iterator of the ORIGIN_DIE children. */
13576 struct die_info *origin_child_die;
d389af10 13577 struct attribute *attr;
cd02d79d
PA
13578 struct dwarf2_cu *origin_cu;
13579 struct pending **origin_previous_list_in_scope;
d389af10
JK
13580
13581 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13582 if (!attr)
13583 return;
13584
cd02d79d
PA
13585 /* Note that following die references may follow to a die in a
13586 different cu. */
13587
13588 origin_cu = cu;
13589 origin_die = follow_die_ref (die, attr, &origin_cu);
13590
13591 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13592 symbols in. */
13593 origin_previous_list_in_scope = origin_cu->list_in_scope;
13594 origin_cu->list_in_scope = cu->list_in_scope;
13595
edb3359d
DJ
13596 if (die->tag != origin_die->tag
13597 && !(die->tag == DW_TAG_inlined_subroutine
13598 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13599 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13600 sect_offset_str (die->sect_off),
13601 sect_offset_str (origin_die->sect_off));
d389af10 13602
791afaa2 13603 std::vector<sect_offset> offsets;
d389af10 13604
3ea89b92
PMR
13605 for (child_die = die->child;
13606 child_die && child_die->tag;
13607 child_die = sibling_die (child_die))
13608 {
13609 struct die_info *child_origin_die;
13610 struct dwarf2_cu *child_origin_cu;
13611
13612 /* We are trying to process concrete instance entries:
216f72a1 13613 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13614 it's not relevant to our analysis here. i.e. detecting DIEs that are
13615 present in the abstract instance but not referenced in the concrete
13616 one. */
216f72a1
JK
13617 if (child_die->tag == DW_TAG_call_site
13618 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13619 continue;
13620
c38f313d
DJ
13621 /* For each CHILD_DIE, find the corresponding child of
13622 ORIGIN_DIE. If there is more than one layer of
13623 DW_AT_abstract_origin, follow them all; there shouldn't be,
13624 but GCC versions at least through 4.4 generate this (GCC PR
13625 40573). */
3ea89b92
PMR
13626 child_origin_die = child_die;
13627 child_origin_cu = cu;
c38f313d
DJ
13628 while (1)
13629 {
cd02d79d
PA
13630 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13631 child_origin_cu);
c38f313d
DJ
13632 if (attr == NULL)
13633 break;
cd02d79d
PA
13634 child_origin_die = follow_die_ref (child_origin_die, attr,
13635 &child_origin_cu);
c38f313d
DJ
13636 }
13637
d389af10
JK
13638 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13639 counterpart may exist. */
c38f313d 13640 if (child_origin_die != child_die)
d389af10 13641 {
edb3359d
DJ
13642 if (child_die->tag != child_origin_die->tag
13643 && !(child_die->tag == DW_TAG_inlined_subroutine
13644 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13645 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13646 "different tags"),
9d8780f0
SM
13647 sect_offset_str (child_die->sect_off),
13648 sect_offset_str (child_origin_die->sect_off));
c38f313d 13649 if (child_origin_die->parent != origin_die)
b98664d3 13650 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13651 "different parents"),
9d8780f0
SM
13652 sect_offset_str (child_die->sect_off),
13653 sect_offset_str (child_origin_die->sect_off));
c38f313d 13654 else
791afaa2 13655 offsets.push_back (child_origin_die->sect_off);
d389af10 13656 }
d389af10 13657 }
791afaa2
TT
13658 std::sort (offsets.begin (), offsets.end ());
13659 sect_offset *offsets_end = offsets.data () + offsets.size ();
13660 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13661 if (offsetp[-1] == *offsetp)
b98664d3 13662 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13663 "to DIE %s as their abstract origin"),
13664 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13665
791afaa2 13666 offsetp = offsets.data ();
d389af10
JK
13667 origin_child_die = origin_die->child;
13668 while (origin_child_die && origin_child_die->tag)
13669 {
13670 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13671 while (offsetp < offsets_end
9c541725 13672 && *offsetp < origin_child_die->sect_off)
d389af10 13673 offsetp++;
b64f50a1 13674 if (offsetp >= offsets_end
9c541725 13675 || *offsetp > origin_child_die->sect_off)
d389af10 13676 {
adde2bff
DE
13677 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13678 Check whether we're already processing ORIGIN_CHILD_DIE.
13679 This can happen with mutually referenced abstract_origins.
13680 PR 16581. */
13681 if (!origin_child_die->in_process)
13682 process_die (origin_child_die, origin_cu);
d389af10
JK
13683 }
13684 origin_child_die = sibling_die (origin_child_die);
13685 }
cd02d79d 13686 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13687
13688 if (cu != origin_cu)
13689 compute_delayed_physnames (origin_cu);
d389af10
JK
13690}
13691
c906108c 13692static void
e7c27a73 13693read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13694{
518817b3 13695 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13696 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13697 struct context_stack *newobj;
c906108c
SS
13698 CORE_ADDR lowpc;
13699 CORE_ADDR highpc;
13700 struct die_info *child_die;
edb3359d 13701 struct attribute *attr, *call_line, *call_file;
15d034d0 13702 const char *name;
e142c38c 13703 CORE_ADDR baseaddr;
801e3a5b 13704 struct block *block;
edb3359d 13705 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13706 std::vector<struct symbol *> template_args;
34eaf542 13707 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13708
13709 if (inlined_func)
13710 {
13711 /* If we do not have call site information, we can't show the
13712 caller of this inlined function. That's too confusing, so
13713 only use the scope for local variables. */
13714 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13715 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13716 if (call_line == NULL || call_file == NULL)
13717 {
13718 read_lexical_block_scope (die, cu);
13719 return;
13720 }
13721 }
c906108c 13722
e142c38c
DJ
13723 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13724
94af9270 13725 name = dwarf2_name (die, cu);
c906108c 13726
e8d05480
JB
13727 /* Ignore functions with missing or empty names. These are actually
13728 illegal according to the DWARF standard. */
13729 if (name == NULL)
13730 {
b98664d3 13731 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13732 sect_offset_str (die->sect_off));
e8d05480
JB
13733 return;
13734 }
13735
13736 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13737 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13738 <= PC_BOUNDS_INVALID)
e8d05480 13739 {
ae4d0c03
PM
13740 attr = dwarf2_attr (die, DW_AT_external, cu);
13741 if (!attr || !DW_UNSND (attr))
b98664d3 13742 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13743 "for subprogram DIE at %s"),
13744 sect_offset_str (die->sect_off));
e8d05480
JB
13745 return;
13746 }
c906108c 13747
3e29f34a
MR
13748 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13749 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13750
34eaf542
TT
13751 /* If we have any template arguments, then we must allocate a
13752 different sort of symbol. */
13753 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13754 {
13755 if (child_die->tag == DW_TAG_template_type_param
13756 || child_die->tag == DW_TAG_template_value_param)
13757 {
e623cf5d 13758 templ_func = allocate_template_symbol (objfile);
cf724bc9 13759 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13760 break;
13761 }
13762 }
13763
c24bdb02 13764 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13765 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13766 (struct symbol *) templ_func);
4c2df51b 13767
81873cc8 13768 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13769 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13770 cu->language);
13771
4cecd739
DJ
13772 /* If there is a location expression for DW_AT_frame_base, record
13773 it. */
e142c38c 13774 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13775 if (attr != nullptr)
fe978cb0 13776 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13777
63e43d3a
PMR
13778 /* If there is a location for the static link, record it. */
13779 newobj->static_link = NULL;
13780 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13781 if (attr != nullptr)
63e43d3a 13782 {
224c3ddb
SM
13783 newobj->static_link
13784 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13785 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13786 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13787 }
13788
c24bdb02 13789 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13790
639d11d3 13791 if (die->child != NULL)
c906108c 13792 {
639d11d3 13793 child_die = die->child;
c906108c
SS
13794 while (child_die && child_die->tag)
13795 {
34eaf542
TT
13796 if (child_die->tag == DW_TAG_template_type_param
13797 || child_die->tag == DW_TAG_template_value_param)
13798 {
13799 struct symbol *arg = new_symbol (child_die, NULL, cu);
13800
f1078f66 13801 if (arg != NULL)
2f4732b0 13802 template_args.push_back (arg);
34eaf542
TT
13803 }
13804 else
13805 process_die (child_die, cu);
c906108c
SS
13806 child_die = sibling_die (child_die);
13807 }
13808 }
13809
d389af10
JK
13810 inherit_abstract_dies (die, cu);
13811
4a811a97
UW
13812 /* If we have a DW_AT_specification, we might need to import using
13813 directives from the context of the specification DIE. See the
13814 comment in determine_prefix. */
13815 if (cu->language == language_cplus
13816 && dwarf2_attr (die, DW_AT_specification, cu))
13817 {
13818 struct dwarf2_cu *spec_cu = cu;
13819 struct die_info *spec_die = die_specification (die, &spec_cu);
13820
13821 while (spec_die)
13822 {
13823 child_die = spec_die->child;
13824 while (child_die && child_die->tag)
13825 {
13826 if (child_die->tag == DW_TAG_imported_module)
13827 process_die (child_die, spec_cu);
13828 child_die = sibling_die (child_die);
13829 }
13830
13831 /* In some cases, GCC generates specification DIEs that
13832 themselves contain DW_AT_specification attributes. */
13833 spec_die = die_specification (spec_die, &spec_cu);
13834 }
13835 }
13836
c24bdb02 13837 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13838 /* Make a block for the local symbols within. */
c24bdb02 13839 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13840 cstk.static_link, lowpc, highpc);
801e3a5b 13841
df8a16a1 13842 /* For C++, set the block's scope. */
45280282
IB
13843 if ((cu->language == language_cplus
13844 || cu->language == language_fortran
c44af4eb
TT
13845 || cu->language == language_d
13846 || cu->language == language_rust)
4d4ec4e5 13847 && cu->processing_has_namespace_info)
195a3f6c
TT
13848 block_set_scope (block, determine_prefix (die, cu),
13849 &objfile->objfile_obstack);
df8a16a1 13850
801e3a5b
JB
13851 /* If we have address ranges, record them. */
13852 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13853
a60f3166 13854 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13855
34eaf542 13856 /* Attach template arguments to function. */
2f4732b0 13857 if (!template_args.empty ())
34eaf542
TT
13858 {
13859 gdb_assert (templ_func != NULL);
13860
2f4732b0 13861 templ_func->n_template_arguments = template_args.size ();
34eaf542 13862 templ_func->template_arguments
8d749320
SM
13863 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13864 templ_func->n_template_arguments);
34eaf542 13865 memcpy (templ_func->template_arguments,
2f4732b0 13866 template_args.data (),
34eaf542 13867 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13868
13869 /* Make sure that the symtab is set on the new symbols. Even
13870 though they don't appear in this symtab directly, other parts
13871 of gdb assume that symbols do, and this is reasonably
13872 true. */
8634679f 13873 for (symbol *sym : template_args)
3e1d3d8c 13874 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13875 }
13876
208d8187
JB
13877 /* In C++, we can have functions nested inside functions (e.g., when
13878 a function declares a class that has methods). This means that
13879 when we finish processing a function scope, we may need to go
13880 back to building a containing block's symbol lists. */
c24bdb02
KS
13881 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13882 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13883
921e78cf
JB
13884 /* If we've finished processing a top-level function, subsequent
13885 symbols go in the file symbol list. */
c24bdb02
KS
13886 if (cu->get_builder ()->outermost_context_p ())
13887 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13888}
13889
13890/* Process all the DIES contained within a lexical block scope. Start
13891 a new scope, process the dies, and then close the scope. */
13892
13893static void
e7c27a73 13894read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13895{
518817b3 13896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13897 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13898 CORE_ADDR lowpc, highpc;
13899 struct die_info *child_die;
e142c38c
DJ
13900 CORE_ADDR baseaddr;
13901
13902 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13903
13904 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13905 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13906 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13907 be nasty. Might be easier to properly extend generic blocks to
af34e669 13908 describe ranges. */
e385593e
JK
13909 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13910 {
13911 case PC_BOUNDS_NOT_PRESENT:
13912 /* DW_TAG_lexical_block has no attributes, process its children as if
13913 there was no wrapping by that DW_TAG_lexical_block.
13914 GCC does no longer produces such DWARF since GCC r224161. */
13915 for (child_die = die->child;
13916 child_die != NULL && child_die->tag;
13917 child_die = sibling_die (child_die))
13918 process_die (child_die, cu);
13919 return;
13920 case PC_BOUNDS_INVALID:
13921 return;
13922 }
3e29f34a
MR
13923 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13924 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13925
c24bdb02 13926 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13927 if (die->child != NULL)
c906108c 13928 {
639d11d3 13929 child_die = die->child;
c906108c
SS
13930 while (child_die && child_die->tag)
13931 {
e7c27a73 13932 process_die (child_die, cu);
c906108c
SS
13933 child_die = sibling_die (child_die);
13934 }
13935 }
3ea89b92 13936 inherit_abstract_dies (die, cu);
c24bdb02 13937 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13938
c24bdb02
KS
13939 if (*cu->get_builder ()->get_local_symbols () != NULL
13940 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13941 {
801e3a5b 13942 struct block *block
c24bdb02 13943 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13944 cstk.start_addr, highpc);
801e3a5b
JB
13945
13946 /* Note that recording ranges after traversing children, as we
13947 do here, means that recording a parent's ranges entails
13948 walking across all its children's ranges as they appear in
13949 the address map, which is quadratic behavior.
13950
13951 It would be nicer to record the parent's ranges before
13952 traversing its children, simply overriding whatever you find
13953 there. But since we don't even decide whether to create a
13954 block until after we've traversed its children, that's hard
13955 to do. */
13956 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13957 }
c24bdb02
KS
13958 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13959 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13960}
13961
216f72a1 13962/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13963
13964static void
13965read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13966{
518817b3 13967 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13968 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13969 CORE_ADDR pc, baseaddr;
13970 struct attribute *attr;
13971 struct call_site *call_site, call_site_local;
13972 void **slot;
13973 int nparams;
13974 struct die_info *child_die;
13975
13976 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13977
216f72a1
JK
13978 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13979 if (attr == NULL)
13980 {
13981 /* This was a pre-DWARF-5 GNU extension alias
13982 for DW_AT_call_return_pc. */
13983 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13984 }
96408a79
SA
13985 if (!attr)
13986 {
b98664d3 13987 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13988 "DIE %s [in module %s]"),
13989 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13990 return;
13991 }
31aa7e4e 13992 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13993 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13994
13995 if (cu->call_site_htab == NULL)
13996 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13997 NULL, &objfile->objfile_obstack,
13998 hashtab_obstack_allocate, NULL);
13999 call_site_local.pc = pc;
14000 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14001 if (*slot != NULL)
14002 {
b98664d3 14003 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
14004 "DIE %s [in module %s]"),
14005 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 14006 objfile_name (objfile));
96408a79
SA
14007 return;
14008 }
14009
14010 /* Count parameters at the caller. */
14011
14012 nparams = 0;
14013 for (child_die = die->child; child_die && child_die->tag;
14014 child_die = sibling_die (child_die))
14015 {
216f72a1
JK
14016 if (child_die->tag != DW_TAG_call_site_parameter
14017 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 14018 {
b98664d3 14019 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14020 "DW_TAG_call_site child DIE %s [in module %s]"),
14021 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14022 objfile_name (objfile));
96408a79
SA
14023 continue;
14024 }
14025
14026 nparams++;
14027 }
14028
224c3ddb
SM
14029 call_site
14030 = ((struct call_site *)
14031 obstack_alloc (&objfile->objfile_obstack,
14032 sizeof (*call_site)
14033 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14034 *slot = call_site;
14035 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14036 call_site->pc = pc;
14037
216f72a1
JK
14038 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14039 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14040 {
14041 struct die_info *func_die;
14042
14043 /* Skip also over DW_TAG_inlined_subroutine. */
14044 for (func_die = die->parent;
14045 func_die && func_die->tag != DW_TAG_subprogram
14046 && func_die->tag != DW_TAG_subroutine_type;
14047 func_die = func_die->parent);
14048
216f72a1
JK
14049 /* DW_AT_call_all_calls is a superset
14050 of DW_AT_call_all_tail_calls. */
96408a79 14051 if (func_die
216f72a1 14052 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14053 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14054 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14055 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14056 {
14057 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14058 not complete. But keep CALL_SITE for look ups via call_site_htab,
14059 both the initial caller containing the real return address PC and
14060 the final callee containing the current PC of a chain of tail
14061 calls do not need to have the tail call list complete. But any
14062 function candidate for a virtual tail call frame searched via
14063 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14064 determined unambiguously. */
14065 }
14066 else
14067 {
14068 struct type *func_type = NULL;
14069
14070 if (func_die)
14071 func_type = get_die_type (func_die, cu);
14072 if (func_type != NULL)
14073 {
14074 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14075
14076 /* Enlist this call site to the function. */
14077 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14078 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14079 }
14080 else
b98664d3 14081 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14082 "DIE %s [in module %s]"),
14083 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14084 }
14085 }
14086
216f72a1
JK
14087 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14088 if (attr == NULL)
14089 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14090 if (attr == NULL)
14091 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14092 if (attr == NULL)
216f72a1
JK
14093 {
14094 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14095 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14096 }
96408a79
SA
14097 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14098 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14099 /* Keep NULL DWARF_BLOCK. */;
14100 else if (attr_form_is_block (attr))
14101 {
14102 struct dwarf2_locexpr_baton *dlbaton;
14103
8d749320 14104 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14105 dlbaton->data = DW_BLOCK (attr)->data;
14106 dlbaton->size = DW_BLOCK (attr)->size;
14107 dlbaton->per_cu = cu->per_cu;
14108
14109 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14110 }
7771576e 14111 else if (attr_form_is_ref (attr))
96408a79 14112 {
96408a79
SA
14113 struct dwarf2_cu *target_cu = cu;
14114 struct die_info *target_die;
14115
ac9ec31b 14116 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14117 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14118 if (die_is_declaration (target_die, target_cu))
14119 {
7d45c7c3 14120 const char *target_physname;
9112db09
JK
14121
14122 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14123 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14124 if (target_physname == NULL)
9112db09 14125 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14126 if (target_physname == NULL)
b98664d3 14127 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14128 "physname, for referencing DIE %s [in module %s]"),
14129 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14130 else
7d455152 14131 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14132 }
14133 else
14134 {
14135 CORE_ADDR lowpc;
14136
14137 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14138 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14139 <= PC_BOUNDS_INVALID)
b98664d3 14140 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14141 "low pc, for referencing DIE %s [in module %s]"),
14142 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14143 else
3e29f34a
MR
14144 {
14145 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14146 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14147 }
96408a79
SA
14148 }
14149 }
14150 else
b98664d3 14151 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14152 "block nor reference, for DIE %s [in module %s]"),
14153 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14154
14155 call_site->per_cu = cu->per_cu;
14156
14157 for (child_die = die->child;
14158 child_die && child_die->tag;
14159 child_die = sibling_die (child_die))
14160 {
96408a79 14161 struct call_site_parameter *parameter;
1788b2d3 14162 struct attribute *loc, *origin;
96408a79 14163
216f72a1
JK
14164 if (child_die->tag != DW_TAG_call_site_parameter
14165 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14166 {
14167 /* Already printed the complaint above. */
14168 continue;
14169 }
14170
14171 gdb_assert (call_site->parameter_count < nparams);
14172 parameter = &call_site->parameter[call_site->parameter_count];
14173
1788b2d3
JK
14174 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14175 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14176 register is contained in DW_AT_call_value. */
96408a79 14177
24c5c679 14178 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14179 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14180 if (origin == NULL)
14181 {
14182 /* This was a pre-DWARF-5 GNU extension alias
14183 for DW_AT_call_parameter. */
14184 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14185 }
7771576e 14186 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14187 {
1788b2d3 14188 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14189
14190 sect_offset sect_off
14191 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14192 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14193 {
14194 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14195 binding can be done only inside one CU. Such referenced DIE
14196 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14197 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14198 "DW_TAG_call_site child DIE %s [in module %s]"),
14199 sect_offset_str (child_die->sect_off),
9c541725 14200 objfile_name (objfile));
d76b7dbc
JK
14201 continue;
14202 }
9c541725
PA
14203 parameter->u.param_cu_off
14204 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14205 }
14206 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14207 {
b98664d3 14208 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14209 "DW_TAG_call_site child DIE %s [in module %s]"),
14210 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14211 continue;
14212 }
24c5c679 14213 else
96408a79 14214 {
24c5c679
JK
14215 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14216 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14217 if (parameter->u.dwarf_reg != -1)
14218 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14219 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14220 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14221 &parameter->u.fb_offset))
14222 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14223 else
14224 {
b98664d3 14225 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14226 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14227 "DW_TAG_call_site child DIE %s "
24c5c679 14228 "[in module %s]"),
9d8780f0 14229 sect_offset_str (child_die->sect_off),
9c541725 14230 objfile_name (objfile));
24c5c679
JK
14231 continue;
14232 }
96408a79
SA
14233 }
14234
216f72a1
JK
14235 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14236 if (attr == NULL)
14237 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14238 if (!attr_form_is_block (attr))
14239 {
b98664d3 14240 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14241 "DW_TAG_call_site child DIE %s [in module %s]"),
14242 sect_offset_str (child_die->sect_off),
9c541725 14243 objfile_name (objfile));
96408a79
SA
14244 continue;
14245 }
14246 parameter->value = DW_BLOCK (attr)->data;
14247 parameter->value_size = DW_BLOCK (attr)->size;
14248
14249 /* Parameters are not pre-cleared by memset above. */
14250 parameter->data_value = NULL;
14251 parameter->data_value_size = 0;
14252 call_site->parameter_count++;
14253
216f72a1
JK
14254 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14255 if (attr == NULL)
14256 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14257 if (attr != nullptr)
96408a79
SA
14258 {
14259 if (!attr_form_is_block (attr))
b98664d3 14260 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14261 "DW_TAG_call_site child DIE %s [in module %s]"),
14262 sect_offset_str (child_die->sect_off),
9c541725 14263 objfile_name (objfile));
96408a79
SA
14264 else
14265 {
14266 parameter->data_value = DW_BLOCK (attr)->data;
14267 parameter->data_value_size = DW_BLOCK (attr)->size;
14268 }
14269 }
14270 }
14271}
14272
71a3c369
TT
14273/* Helper function for read_variable. If DIE represents a virtual
14274 table, then return the type of the concrete object that is
14275 associated with the virtual table. Otherwise, return NULL. */
14276
14277static struct type *
14278rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14279{
14280 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14281 if (attr == NULL)
14282 return NULL;
14283
14284 /* Find the type DIE. */
14285 struct die_info *type_die = NULL;
14286 struct dwarf2_cu *type_cu = cu;
14287
14288 if (attr_form_is_ref (attr))
14289 type_die = follow_die_ref (die, attr, &type_cu);
14290 if (type_die == NULL)
14291 return NULL;
14292
14293 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14294 return NULL;
14295 return die_containing_type (type_die, type_cu);
14296}
14297
14298/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14299
14300static void
14301read_variable (struct die_info *die, struct dwarf2_cu *cu)
14302{
14303 struct rust_vtable_symbol *storage = NULL;
14304
14305 if (cu->language == language_rust)
14306 {
14307 struct type *containing_type = rust_containing_type (die, cu);
14308
14309 if (containing_type != NULL)
14310 {
518817b3 14311 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 14312
468c0cbb 14313 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
14314 initialize_objfile_symbol (storage);
14315 storage->concrete_type = containing_type;
cf724bc9 14316 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14317 }
14318 }
14319
e4a62c65
TV
14320 struct symbol *res = new_symbol (die, NULL, cu, storage);
14321 struct attribute *abstract_origin
14322 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14323 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14324 if (res == NULL && loc && abstract_origin)
14325 {
14326 /* We have a variable without a name, but with a location and an abstract
14327 origin. This may be a concrete instance of an abstract variable
14328 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14329 later. */
14330 struct dwarf2_cu *origin_cu = cu;
14331 struct die_info *origin_die
14332 = follow_die_ref (die, abstract_origin, &origin_cu);
14333 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14334 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14335 }
71a3c369
TT
14336}
14337
43988095
JK
14338/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14339 reading .debug_rnglists.
14340 Callback's type should be:
14341 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14342 Return true if the attributes are present and valid, otherwise,
14343 return false. */
14344
14345template <typename Callback>
14346static bool
14347dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14348 Callback &&callback)
14349{
ed2dc618 14350 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14351 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14352 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14353 bfd *obfd = objfile->obfd;
43988095
JK
14354 /* Base address selection entry. */
14355 CORE_ADDR base;
14356 int found_base;
43988095 14357 const gdb_byte *buffer;
43988095
JK
14358 CORE_ADDR baseaddr;
14359 bool overflow = false;
14360
14361 found_base = cu->base_known;
14362 base = cu->base_address;
14363
14364 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14365 if (offset >= dwarf2_per_objfile->rnglists.size)
14366 {
b98664d3 14367 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14368 offset);
14369 return false;
14370 }
14371 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14372
14373 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14374
14375 while (1)
14376 {
7814882a
JK
14377 /* Initialize it due to a false compiler warning. */
14378 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14379 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14380 + dwarf2_per_objfile->rnglists.size);
14381 unsigned int bytes_read;
14382
14383 if (buffer == buf_end)
14384 {
14385 overflow = true;
14386 break;
14387 }
14388 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14389 switch (rlet)
14390 {
14391 case DW_RLE_end_of_list:
14392 break;
14393 case DW_RLE_base_address:
14394 if (buffer + cu->header.addr_size > buf_end)
14395 {
14396 overflow = true;
14397 break;
14398 }
14399 base = read_address (obfd, buffer, cu, &bytes_read);
14400 found_base = 1;
14401 buffer += bytes_read;
14402 break;
14403 case DW_RLE_start_length:
14404 if (buffer + cu->header.addr_size > buf_end)
14405 {
14406 overflow = true;
14407 break;
14408 }
14409 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14410 buffer += bytes_read;
14411 range_end = (range_beginning
14412 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14413 buffer += bytes_read;
14414 if (buffer > buf_end)
14415 {
14416 overflow = true;
14417 break;
14418 }
14419 break;
14420 case DW_RLE_offset_pair:
14421 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14422 buffer += bytes_read;
14423 if (buffer > buf_end)
14424 {
14425 overflow = true;
14426 break;
14427 }
14428 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14429 buffer += bytes_read;
14430 if (buffer > buf_end)
14431 {
14432 overflow = true;
14433 break;
14434 }
14435 break;
14436 case DW_RLE_start_end:
14437 if (buffer + 2 * cu->header.addr_size > buf_end)
14438 {
14439 overflow = true;
14440 break;
14441 }
14442 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14443 buffer += bytes_read;
14444 range_end = read_address (obfd, buffer, cu, &bytes_read);
14445 buffer += bytes_read;
14446 break;
14447 default:
b98664d3 14448 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14449 return false;
14450 }
14451 if (rlet == DW_RLE_end_of_list || overflow)
14452 break;
14453 if (rlet == DW_RLE_base_address)
14454 continue;
14455
14456 if (!found_base)
14457 {
14458 /* We have no valid base address for the ranges
14459 data. */
b98664d3 14460 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14461 return false;
14462 }
14463
14464 if (range_beginning > range_end)
14465 {
14466 /* Inverted range entries are invalid. */
b98664d3 14467 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14468 return false;
14469 }
14470
14471 /* Empty range entries have no effect. */
14472 if (range_beginning == range_end)
14473 continue;
14474
14475 range_beginning += base;
14476 range_end += base;
14477
14478 /* A not-uncommon case of bad debug info.
14479 Don't pollute the addrmap with bad data. */
14480 if (range_beginning + baseaddr == 0
14481 && !dwarf2_per_objfile->has_section_at_zero)
14482 {
b98664d3 14483 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14484 " [in module %s]"), objfile_name (objfile));
14485 continue;
14486 }
14487
14488 callback (range_beginning, range_end);
14489 }
14490
14491 if (overflow)
14492 {
b98664d3 14493 complaint (_("Offset %d is not terminated "
43988095
JK
14494 "for DW_AT_ranges attribute"),
14495 offset);
14496 return false;
14497 }
14498
14499 return true;
14500}
14501
14502/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14503 Callback's type should be:
14504 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14505 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14506
43988095 14507template <typename Callback>
43039443 14508static int
5f46c5a5 14509dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14510 Callback &&callback)
43039443 14511{
ed2dc618 14512 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14513 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14514 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14515 struct comp_unit_head *cu_header = &cu->header;
14516 bfd *obfd = objfile->obfd;
14517 unsigned int addr_size = cu_header->addr_size;
14518 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14519 /* Base address selection entry. */
14520 CORE_ADDR base;
14521 int found_base;
14522 unsigned int dummy;
d521ce57 14523 const gdb_byte *buffer;
ff013f42 14524 CORE_ADDR baseaddr;
43039443 14525
43988095
JK
14526 if (cu_header->version >= 5)
14527 return dwarf2_rnglists_process (offset, cu, callback);
14528
d00adf39
DE
14529 found_base = cu->base_known;
14530 base = cu->base_address;
43039443 14531
be391dca 14532 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14533 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14534 {
b98664d3 14535 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14536 offset);
14537 return 0;
14538 }
dce234bc 14539 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14540
e7030f15 14541 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14542
43039443
JK
14543 while (1)
14544 {
14545 CORE_ADDR range_beginning, range_end;
14546
14547 range_beginning = read_address (obfd, buffer, cu, &dummy);
14548 buffer += addr_size;
14549 range_end = read_address (obfd, buffer, cu, &dummy);
14550 buffer += addr_size;
14551 offset += 2 * addr_size;
14552
14553 /* An end of list marker is a pair of zero addresses. */
14554 if (range_beginning == 0 && range_end == 0)
14555 /* Found the end of list entry. */
14556 break;
14557
14558 /* Each base address selection entry is a pair of 2 values.
14559 The first is the largest possible address, the second is
14560 the base address. Check for a base address here. */
14561 if ((range_beginning & mask) == mask)
14562 {
28d2bfb9
AB
14563 /* If we found the largest possible address, then we already
14564 have the base address in range_end. */
14565 base = range_end;
43039443
JK
14566 found_base = 1;
14567 continue;
14568 }
14569
14570 if (!found_base)
14571 {
14572 /* We have no valid base address for the ranges
14573 data. */
b98664d3 14574 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14575 return 0;
14576 }
14577
9277c30c
UW
14578 if (range_beginning > range_end)
14579 {
14580 /* Inverted range entries are invalid. */
b98664d3 14581 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14582 return 0;
14583 }
14584
14585 /* Empty range entries have no effect. */
14586 if (range_beginning == range_end)
14587 continue;
14588
43039443
JK
14589 range_beginning += base;
14590 range_end += base;
14591
01093045
DE
14592 /* A not-uncommon case of bad debug info.
14593 Don't pollute the addrmap with bad data. */
14594 if (range_beginning + baseaddr == 0
14595 && !dwarf2_per_objfile->has_section_at_zero)
14596 {
b98664d3 14597 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14598 " [in module %s]"), objfile_name (objfile));
01093045
DE
14599 continue;
14600 }
14601
5f46c5a5
JK
14602 callback (range_beginning, range_end);
14603 }
14604
14605 return 1;
14606}
14607
14608/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14609 Return 1 if the attributes are present and valid, otherwise, return 0.
14610 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14611
14612static int
14613dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14614 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14615 struct partial_symtab *ranges_pst)
14616{
518817b3 14617 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14618 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14619 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14620 SECT_OFF_TEXT (objfile));
14621 int low_set = 0;
14622 CORE_ADDR low = 0;
14623 CORE_ADDR high = 0;
14624 int retval;
14625
14626 retval = dwarf2_ranges_process (offset, cu,
14627 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14628 {
9277c30c 14629 if (ranges_pst != NULL)
3e29f34a
MR
14630 {
14631 CORE_ADDR lowpc;
14632 CORE_ADDR highpc;
14633
79748972
TT
14634 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14635 range_beginning + baseaddr)
14636 - baseaddr);
14637 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14638 range_end + baseaddr)
14639 - baseaddr);
d320c2b5
TT
14640 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14641 lowpc, highpc - 1, ranges_pst);
3e29f34a 14642 }
ff013f42 14643
43039443
JK
14644 /* FIXME: This is recording everything as a low-high
14645 segment of consecutive addresses. We should have a
14646 data structure for discontiguous block ranges
14647 instead. */
14648 if (! low_set)
14649 {
14650 low = range_beginning;
14651 high = range_end;
14652 low_set = 1;
14653 }
14654 else
14655 {
14656 if (range_beginning < low)
14657 low = range_beginning;
14658 if (range_end > high)
14659 high = range_end;
14660 }
5f46c5a5
JK
14661 });
14662 if (!retval)
14663 return 0;
43039443
JK
14664
14665 if (! low_set)
14666 /* If the first entry is an end-of-list marker, the range
14667 describes an empty scope, i.e. no instructions. */
14668 return 0;
14669
14670 if (low_return)
14671 *low_return = low;
14672 if (high_return)
14673 *high_return = high;
14674 return 1;
14675}
14676
3a2b436a
JK
14677/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14678 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14679 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14680
3a2b436a 14681static enum pc_bounds_kind
af34e669 14682dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14683 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14684 struct partial_symtab *pst)
c906108c 14685{
518817b3
SM
14686 struct dwarf2_per_objfile *dwarf2_per_objfile
14687 = cu->per_cu->dwarf2_per_objfile;
c906108c 14688 struct attribute *attr;
91da1414 14689 struct attribute *attr_high;
af34e669
DJ
14690 CORE_ADDR low = 0;
14691 CORE_ADDR high = 0;
e385593e 14692 enum pc_bounds_kind ret;
c906108c 14693
91da1414
MW
14694 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14695 if (attr_high)
af34e669 14696 {
e142c38c 14697 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14698 if (attr != nullptr)
91da1414 14699 {
31aa7e4e
JB
14700 low = attr_value_as_address (attr);
14701 high = attr_value_as_address (attr_high);
14702 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14703 high += low;
91da1414 14704 }
af34e669
DJ
14705 else
14706 /* Found high w/o low attribute. */
e385593e 14707 return PC_BOUNDS_INVALID;
af34e669
DJ
14708
14709 /* Found consecutive range of addresses. */
3a2b436a 14710 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14711 }
c906108c 14712 else
af34e669 14713 {
e142c38c 14714 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14715 if (attr != NULL)
14716 {
ab435259
DE
14717 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14718 We take advantage of the fact that DW_AT_ranges does not appear
14719 in DW_TAG_compile_unit of DWO files. */
14720 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14721 unsigned int ranges_offset = (DW_UNSND (attr)
14722 + (need_ranges_base
14723 ? cu->ranges_base
14724 : 0));
2e3cf129 14725
af34e669 14726 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14727 .debug_ranges section. */
2e3cf129 14728 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14729 return PC_BOUNDS_INVALID;
43039443 14730 /* Found discontinuous range of addresses. */
3a2b436a 14731 ret = PC_BOUNDS_RANGES;
af34e669 14732 }
e385593e
JK
14733 else
14734 return PC_BOUNDS_NOT_PRESENT;
af34e669 14735 }
c906108c 14736
48fbe735 14737 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14738 if (high <= low)
e385593e 14739 return PC_BOUNDS_INVALID;
c906108c
SS
14740
14741 /* When using the GNU linker, .gnu.linkonce. sections are used to
14742 eliminate duplicate copies of functions and vtables and such.
14743 The linker will arbitrarily choose one and discard the others.
14744 The AT_*_pc values for such functions refer to local labels in
14745 these sections. If the section from that file was discarded, the
14746 labels are not in the output, so the relocs get a value of 0.
14747 If this is a discarded function, mark the pc bounds as invalid,
14748 so that GDB will ignore it. */
72dca2f5 14749 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14750 return PC_BOUNDS_INVALID;
c906108c
SS
14751
14752 *lowpc = low;
96408a79
SA
14753 if (highpc)
14754 *highpc = high;
af34e669 14755 return ret;
c906108c
SS
14756}
14757
b084d499
JB
14758/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14759 its low and high PC addresses. Do nothing if these addresses could not
14760 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14761 and HIGHPC to the high address if greater than HIGHPC. */
14762
14763static void
14764dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14765 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14766 struct dwarf2_cu *cu)
14767{
14768 CORE_ADDR low, high;
14769 struct die_info *child = die->child;
14770
e385593e 14771 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14772 {
325fac50
PA
14773 *lowpc = std::min (*lowpc, low);
14774 *highpc = std::max (*highpc, high);
b084d499
JB
14775 }
14776
14777 /* If the language does not allow nested subprograms (either inside
14778 subprograms or lexical blocks), we're done. */
14779 if (cu->language != language_ada)
14780 return;
6e70227d 14781
b084d499
JB
14782 /* Check all the children of the given DIE. If it contains nested
14783 subprograms, then check their pc bounds. Likewise, we need to
14784 check lexical blocks as well, as they may also contain subprogram
14785 definitions. */
14786 while (child && child->tag)
14787 {
14788 if (child->tag == DW_TAG_subprogram
14789 || child->tag == DW_TAG_lexical_block)
14790 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14791 child = sibling_die (child);
14792 }
14793}
14794
fae299cd
DC
14795/* Get the low and high pc's represented by the scope DIE, and store
14796 them in *LOWPC and *HIGHPC. If the correct values can't be
14797 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14798
14799static void
14800get_scope_pc_bounds (struct die_info *die,
14801 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14802 struct dwarf2_cu *cu)
14803{
14804 CORE_ADDR best_low = (CORE_ADDR) -1;
14805 CORE_ADDR best_high = (CORE_ADDR) 0;
14806 CORE_ADDR current_low, current_high;
14807
3a2b436a 14808 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14809 >= PC_BOUNDS_RANGES)
fae299cd
DC
14810 {
14811 best_low = current_low;
14812 best_high = current_high;
14813 }
14814 else
14815 {
14816 struct die_info *child = die->child;
14817
14818 while (child && child->tag)
14819 {
14820 switch (child->tag) {
14821 case DW_TAG_subprogram:
b084d499 14822 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14823 break;
14824 case DW_TAG_namespace:
f55ee35c 14825 case DW_TAG_module:
fae299cd
DC
14826 /* FIXME: carlton/2004-01-16: Should we do this for
14827 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14828 that current GCC's always emit the DIEs corresponding
14829 to definitions of methods of classes as children of a
14830 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14831 the DIEs giving the declarations, which could be
14832 anywhere). But I don't see any reason why the
14833 standards says that they have to be there. */
14834 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14835
14836 if (current_low != ((CORE_ADDR) -1))
14837 {
325fac50
PA
14838 best_low = std::min (best_low, current_low);
14839 best_high = std::max (best_high, current_high);
fae299cd
DC
14840 }
14841 break;
14842 default:
0963b4bd 14843 /* Ignore. */
fae299cd
DC
14844 break;
14845 }
14846
14847 child = sibling_die (child);
14848 }
14849 }
14850
14851 *lowpc = best_low;
14852 *highpc = best_high;
14853}
14854
801e3a5b
JB
14855/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14856 in DIE. */
380bca97 14857
801e3a5b
JB
14858static void
14859dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14860 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14861{
518817b3 14862 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14863 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14864 struct attribute *attr;
91da1414 14865 struct attribute *attr_high;
801e3a5b 14866
91da1414
MW
14867 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14868 if (attr_high)
801e3a5b 14869 {
801e3a5b 14870 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14871 if (attr != nullptr)
801e3a5b 14872 {
31aa7e4e
JB
14873 CORE_ADDR low = attr_value_as_address (attr);
14874 CORE_ADDR high = attr_value_as_address (attr_high);
14875
14876 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14877 high += low;
9a619af0 14878
3e29f34a
MR
14879 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14880 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14881 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14882 }
14883 }
14884
14885 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14886 if (attr != nullptr)
801e3a5b 14887 {
ab435259
DE
14888 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14889 We take advantage of the fact that DW_AT_ranges does not appear
14890 in DW_TAG_compile_unit of DWO files. */
14891 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14892
14893 /* The value of the DW_AT_ranges attribute is the offset of the
14894 address range list in the .debug_ranges section. */
ab435259
DE
14895 unsigned long offset = (DW_UNSND (attr)
14896 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14897
2d5f09ec 14898 std::vector<blockrange> blockvec;
5f46c5a5
JK
14899 dwarf2_ranges_process (offset, cu,
14900 [&] (CORE_ADDR start, CORE_ADDR end)
14901 {
58fdfd2c
JK
14902 start += baseaddr;
14903 end += baseaddr;
5f46c5a5
JK
14904 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14905 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14906 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14907 blockvec.emplace_back (start, end);
5f46c5a5 14908 });
2d5f09ec
KB
14909
14910 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14911 }
14912}
14913
685b1105
JK
14914/* Check whether the producer field indicates either of GCC < 4.6, or the
14915 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14916
685b1105
JK
14917static void
14918check_producer (struct dwarf2_cu *cu)
60d5a603 14919{
38360086 14920 int major, minor;
60d5a603
JK
14921
14922 if (cu->producer == NULL)
14923 {
14924 /* For unknown compilers expect their behavior is DWARF version
14925 compliant.
14926
14927 GCC started to support .debug_types sections by -gdwarf-4 since
14928 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14929 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14930 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14931 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14932 }
b1ffba5a 14933 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14934 {
38360086
MW
14935 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14936 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14937 }
5230b05a 14938 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14939 {
14940 cu->producer_is_icc = true;
14941 cu->producer_is_icc_lt_14 = major < 14;
14942 }
c258c396
JD
14943 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14944 cu->producer_is_codewarrior = true;
685b1105
JK
14945 else
14946 {
14947 /* For other non-GCC compilers, expect their behavior is DWARF version
14948 compliant. */
60d5a603
JK
14949 }
14950
9068261f 14951 cu->checked_producer = true;
685b1105 14952}
ba919b58 14953
685b1105
JK
14954/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14955 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14956 during 4.6.0 experimental. */
14957
9068261f 14958static bool
685b1105
JK
14959producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14960{
14961 if (!cu->checked_producer)
14962 check_producer (cu);
14963
14964 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14965}
14966
c258c396
JD
14967
14968/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14969 with incorrect is_stmt attributes. */
14970
14971static bool
14972producer_is_codewarrior (struct dwarf2_cu *cu)
14973{
14974 if (!cu->checked_producer)
14975 check_producer (cu);
14976
14977 return cu->producer_is_codewarrior;
14978}
14979
405feb71 14980/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14981 DW_AT_accessibility. */
14982
14983static enum dwarf_access_attribute
14984dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14985{
14986 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14987 {
14988 /* The default DWARF 2 accessibility for members is public, the default
14989 accessibility for inheritance is private. */
14990
14991 if (die->tag != DW_TAG_inheritance)
14992 return DW_ACCESS_public;
14993 else
14994 return DW_ACCESS_private;
14995 }
14996 else
14997 {
14998 /* DWARF 3+ defines the default accessibility a different way. The same
14999 rules apply now for DW_TAG_inheritance as for the members and it only
15000 depends on the container kind. */
15001
15002 if (die->parent->tag == DW_TAG_class_type)
15003 return DW_ACCESS_private;
15004 else
15005 return DW_ACCESS_public;
15006 }
15007}
15008
74ac6d43
TT
15009/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15010 offset. If the attribute was not found return 0, otherwise return
15011 1. If it was found but could not properly be handled, set *OFFSET
15012 to 0. */
15013
15014static int
15015handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15016 LONGEST *offset)
15017{
15018 struct attribute *attr;
15019
15020 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15021 if (attr != NULL)
15022 {
15023 *offset = 0;
15024
15025 /* Note that we do not check for a section offset first here.
15026 This is because DW_AT_data_member_location is new in DWARF 4,
15027 so if we see it, we can assume that a constant form is really
15028 a constant and not a section offset. */
15029 if (attr_form_is_constant (attr))
15030 *offset = dwarf2_get_attr_constant_value (attr, 0);
15031 else if (attr_form_is_section_offset (attr))
15032 dwarf2_complex_location_expr_complaint ();
15033 else if (attr_form_is_block (attr))
15034 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15035 else
15036 dwarf2_complex_location_expr_complaint ();
15037
15038 return 1;
15039 }
15040
15041 return 0;
15042}
15043
c906108c
SS
15044/* Add an aggregate field to the field list. */
15045
15046static void
107d2387 15047dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15048 struct dwarf2_cu *cu)
6e70227d 15049{
518817b3 15050 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15051 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15052 struct nextfield *new_field;
15053 struct attribute *attr;
15054 struct field *fp;
15d034d0 15055 const char *fieldname = "";
c906108c 15056
7d0ccb61
DJ
15057 if (die->tag == DW_TAG_inheritance)
15058 {
be2daae6
TT
15059 fip->baseclasses.emplace_back ();
15060 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15061 }
15062 else
15063 {
be2daae6
TT
15064 fip->fields.emplace_back ();
15065 new_field = &fip->fields.back ();
7d0ccb61 15066 }
be2daae6 15067
c906108c
SS
15068 fip->nfields++;
15069
e142c38c 15070 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15071 if (attr != nullptr)
c906108c 15072 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15073 else
15074 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15075 if (new_field->accessibility != DW_ACCESS_public)
15076 fip->non_public_fields = 1;
60d5a603 15077
e142c38c 15078 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15079 if (attr != nullptr)
c906108c 15080 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15081 else
15082 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15083
15084 fp = &new_field->field;
a9a9bd0f 15085
e142c38c 15086 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15087 {
74ac6d43
TT
15088 LONGEST offset;
15089
a9a9bd0f 15090 /* Data member other than a C++ static data member. */
6e70227d 15091
c906108c 15092 /* Get type of field. */
e7c27a73 15093 fp->type = die_type (die, cu);
c906108c 15094
d6a843b5 15095 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15096
c906108c 15097 /* Get bit size of field (zero if none). */
e142c38c 15098 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15099 if (attr != nullptr)
c906108c
SS
15100 {
15101 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15102 }
15103 else
15104 {
15105 FIELD_BITSIZE (*fp) = 0;
15106 }
15107
15108 /* Get bit offset of field. */
74ac6d43
TT
15109 if (handle_data_member_location (die, cu, &offset))
15110 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15111 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15112 if (attr != nullptr)
c906108c 15113 {
d5a22e77 15114 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15115 {
15116 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15117 additional bit offset from the MSB of the containing
15118 anonymous object to the MSB of the field. We don't
15119 have to do anything special since we don't need to
15120 know the size of the anonymous object. */
f41f5e61 15121 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15122 }
15123 else
15124 {
15125 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15126 MSB of the anonymous object, subtract off the number of
15127 bits from the MSB of the field to the MSB of the
15128 object, and then subtract off the number of bits of
15129 the field itself. The result is the bit offset of
15130 the LSB of the field. */
c906108c
SS
15131 int anonymous_size;
15132 int bit_offset = DW_UNSND (attr);
15133
e142c38c 15134 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15135 if (attr != nullptr)
c906108c
SS
15136 {
15137 /* The size of the anonymous object containing
15138 the bit field is explicit, so use the
15139 indicated size (in bytes). */
15140 anonymous_size = DW_UNSND (attr);
15141 }
15142 else
15143 {
15144 /* The size of the anonymous object containing
15145 the bit field must be inferred from the type
15146 attribute of the data member containing the
15147 bit field. */
15148 anonymous_size = TYPE_LENGTH (fp->type);
15149 }
f41f5e61
PA
15150 SET_FIELD_BITPOS (*fp,
15151 (FIELD_BITPOS (*fp)
15152 + anonymous_size * bits_per_byte
15153 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15154 }
15155 }
da5b30da
AA
15156 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15157 if (attr != NULL)
15158 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15159 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15160
15161 /* Get name of field. */
39cbfefa
DJ
15162 fieldname = dwarf2_name (die, cu);
15163 if (fieldname == NULL)
15164 fieldname = "";
d8151005
DJ
15165
15166 /* The name is already allocated along with this objfile, so we don't
15167 need to duplicate it for the type. */
15168 fp->name = fieldname;
c906108c
SS
15169
15170 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15171 pointer or virtual base class pointer) to private. */
e142c38c 15172 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15173 {
d48cc9dd 15174 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15175 new_field->accessibility = DW_ACCESS_private;
15176 fip->non_public_fields = 1;
15177 }
15178 }
a9a9bd0f 15179 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15180 {
a9a9bd0f
DC
15181 /* C++ static member. */
15182
15183 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15184 is a declaration, but all versions of G++ as of this writing
15185 (so through at least 3.2.1) incorrectly generate
15186 DW_TAG_variable tags. */
6e70227d 15187
ff355380 15188 const char *physname;
c906108c 15189
a9a9bd0f 15190 /* Get name of field. */
39cbfefa
DJ
15191 fieldname = dwarf2_name (die, cu);
15192 if (fieldname == NULL)
c906108c
SS
15193 return;
15194
254e6b9e 15195 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15196 if (attr
15197 /* Only create a symbol if this is an external value.
15198 new_symbol checks this and puts the value in the global symbol
15199 table, which we want. If it is not external, new_symbol
15200 will try to put the value in cu->list_in_scope which is wrong. */
15201 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15202 {
15203 /* A static const member, not much different than an enum as far as
15204 we're concerned, except that we can support more types. */
15205 new_symbol (die, NULL, cu);
15206 }
15207
2df3850c 15208 /* Get physical name. */
ff355380 15209 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15210
d8151005
DJ
15211 /* The name is already allocated along with this objfile, so we don't
15212 need to duplicate it for the type. */
15213 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15214 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15215 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15216 }
15217 else if (die->tag == DW_TAG_inheritance)
15218 {
74ac6d43 15219 LONGEST offset;
d4b96c9a 15220
74ac6d43
TT
15221 /* C++ base class field. */
15222 if (handle_data_member_location (die, cu, &offset))
15223 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15224 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15225 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15226 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15227 }
2ddeaf8a
TT
15228 else if (die->tag == DW_TAG_variant_part)
15229 {
15230 /* process_structure_scope will treat this DIE as a union. */
15231 process_structure_scope (die, cu);
15232
15233 /* The variant part is relative to the start of the enclosing
15234 structure. */
15235 SET_FIELD_BITPOS (*fp, 0);
15236 fp->type = get_die_type (die, cu);
15237 fp->artificial = 1;
15238 fp->name = "<<variant>>";
c8c81635
TT
15239
15240 /* Normally a DW_TAG_variant_part won't have a size, but our
15241 representation requires one, so set it to the maximum of the
489dbda6
TT
15242 child sizes, being sure to account for the offset at which
15243 each child is seen. */
c8c81635
TT
15244 if (TYPE_LENGTH (fp->type) == 0)
15245 {
15246 unsigned max = 0;
15247 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
15248 {
15249 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15250 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15251 if (len > max)
15252 max = len;
15253 }
c8c81635
TT
15254 TYPE_LENGTH (fp->type) = max;
15255 }
2ddeaf8a
TT
15256 }
15257 else
15258 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15259}
15260
883fd55a
KS
15261/* Can the type given by DIE define another type? */
15262
15263static bool
15264type_can_define_types (const struct die_info *die)
15265{
15266 switch (die->tag)
15267 {
15268 case DW_TAG_typedef:
15269 case DW_TAG_class_type:
15270 case DW_TAG_structure_type:
15271 case DW_TAG_union_type:
15272 case DW_TAG_enumeration_type:
15273 return true;
15274
15275 default:
15276 return false;
15277 }
15278}
15279
15280/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15281
15282static void
883fd55a
KS
15283dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15284 struct dwarf2_cu *cu)
6e70227d 15285{
be2daae6
TT
15286 struct decl_field fp;
15287 memset (&fp, 0, sizeof (fp));
98751a41 15288
883fd55a 15289 gdb_assert (type_can_define_types (die));
98751a41 15290
883fd55a 15291 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15292 fp.name = dwarf2_name (die, cu);
15293 fp.type = read_type_die (die, cu);
98751a41 15294
c191a687
KS
15295 /* Save accessibility. */
15296 enum dwarf_access_attribute accessibility;
15297 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15298 if (attr != NULL)
15299 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15300 else
15301 accessibility = dwarf2_default_access_attribute (die, cu);
15302 switch (accessibility)
15303 {
15304 case DW_ACCESS_public:
15305 /* The assumed value if neither private nor protected. */
15306 break;
15307 case DW_ACCESS_private:
be2daae6 15308 fp.is_private = 1;
c191a687
KS
15309 break;
15310 case DW_ACCESS_protected:
be2daae6 15311 fp.is_protected = 1;
c191a687
KS
15312 break;
15313 default:
b98664d3 15314 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15315 }
15316
883fd55a 15317 if (die->tag == DW_TAG_typedef)
be2daae6 15318 fip->typedef_field_list.push_back (fp);
883fd55a 15319 else
be2daae6 15320 fip->nested_types_list.push_back (fp);
98751a41
JK
15321}
15322
c906108c
SS
15323/* Create the vector of fields, and attach it to the type. */
15324
15325static void
fba45db2 15326dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15327 struct dwarf2_cu *cu)
c906108c
SS
15328{
15329 int nfields = fip->nfields;
15330
15331 /* Record the field count, allocate space for the array of fields,
15332 and create blank accessibility bitfields if necessary. */
15333 TYPE_NFIELDS (type) = nfields;
15334 TYPE_FIELDS (type) = (struct field *)
be2daae6 15335 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15336
b4ba55a1 15337 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15338 {
15339 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15340
15341 TYPE_FIELD_PRIVATE_BITS (type) =
15342 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15343 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15344
15345 TYPE_FIELD_PROTECTED_BITS (type) =
15346 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15347 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15348
774b6a14
TT
15349 TYPE_FIELD_IGNORE_BITS (type) =
15350 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15351 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15352 }
15353
15354 /* If the type has baseclasses, allocate and clear a bit vector for
15355 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15356 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15357 {
be2daae6 15358 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15359 unsigned char *pointer;
c906108c
SS
15360
15361 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15362 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15363 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15364 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15365 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15366 }
15367
2ddeaf8a
TT
15368 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15369 {
15370 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15371
be2daae6 15372 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15373 {
be2daae6
TT
15374 struct nextfield &field = fip->fields[index];
15375
15376 if (field.variant.is_discriminant)
2ddeaf8a 15377 di->discriminant_index = index;
be2daae6 15378 else if (field.variant.default_branch)
2ddeaf8a
TT
15379 di->default_index = index;
15380 else
be2daae6 15381 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15382 }
15383 }
15384
be2daae6
TT
15385 /* Copy the saved-up fields into the field vector. */
15386 for (int i = 0; i < nfields; ++i)
c906108c 15387 {
be2daae6
TT
15388 struct nextfield &field
15389 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15390 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15391
be2daae6
TT
15392 TYPE_FIELD (type, i) = field.field;
15393 switch (field.accessibility)
c906108c 15394 {
c5aa993b 15395 case DW_ACCESS_private:
b4ba55a1 15396 if (cu->language != language_ada)
be2daae6 15397 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15398 break;
c906108c 15399
c5aa993b 15400 case DW_ACCESS_protected:
b4ba55a1 15401 if (cu->language != language_ada)
be2daae6 15402 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15403 break;
c906108c 15404
c5aa993b
JM
15405 case DW_ACCESS_public:
15406 break;
c906108c 15407
c5aa993b
JM
15408 default:
15409 /* Unknown accessibility. Complain and treat it as public. */
15410 {
b98664d3 15411 complaint (_("unsupported accessibility %d"),
be2daae6 15412 field.accessibility);
c5aa993b
JM
15413 }
15414 break;
c906108c 15415 }
be2daae6 15416 if (i < fip->baseclasses.size ())
c906108c 15417 {
be2daae6 15418 switch (field.virtuality)
c906108c 15419 {
c5aa993b
JM
15420 case DW_VIRTUALITY_virtual:
15421 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15422 if (cu->language == language_ada)
a73c6dcd 15423 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15424 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15425 break;
c906108c
SS
15426 }
15427 }
c906108c
SS
15428 }
15429}
15430
7d27a96d
TT
15431/* Return true if this member function is a constructor, false
15432 otherwise. */
15433
15434static int
15435dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15436{
15437 const char *fieldname;
fe978cb0 15438 const char *type_name;
7d27a96d
TT
15439 int len;
15440
15441 if (die->parent == NULL)
15442 return 0;
15443
15444 if (die->parent->tag != DW_TAG_structure_type
15445 && die->parent->tag != DW_TAG_union_type
15446 && die->parent->tag != DW_TAG_class_type)
15447 return 0;
15448
15449 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15450 type_name = dwarf2_name (die->parent, cu);
15451 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15452 return 0;
15453
15454 len = strlen (fieldname);
fe978cb0
PA
15455 return (strncmp (fieldname, type_name, len) == 0
15456 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15457}
15458
e35000a7
TBA
15459/* Check if the given VALUE is a recognized enum
15460 dwarf_defaulted_attribute constant according to DWARF5 spec,
15461 Table 7.24. */
15462
15463static bool
15464is_valid_DW_AT_defaulted (ULONGEST value)
15465{
15466 switch (value)
15467 {
15468 case DW_DEFAULTED_no:
15469 case DW_DEFAULTED_in_class:
15470 case DW_DEFAULTED_out_of_class:
15471 return true;
15472 }
15473
3142e908 15474 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15475 return false;
15476}
15477
c906108c
SS
15478/* Add a member function to the proper fieldlist. */
15479
15480static void
107d2387 15481dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15482 struct type *type, struct dwarf2_cu *cu)
c906108c 15483{
518817b3 15484 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15485 struct attribute *attr;
c906108c 15486 int i;
be2daae6 15487 struct fnfieldlist *flp = nullptr;
c906108c 15488 struct fn_field *fnp;
15d034d0 15489 const char *fieldname;
f792889a 15490 struct type *this_type;
60d5a603 15491 enum dwarf_access_attribute accessibility;
c906108c 15492
b4ba55a1 15493 if (cu->language == language_ada)
a73c6dcd 15494 error (_("unexpected member function in Ada type"));
b4ba55a1 15495
2df3850c 15496 /* Get name of member function. */
39cbfefa
DJ
15497 fieldname = dwarf2_name (die, cu);
15498 if (fieldname == NULL)
2df3850c 15499 return;
c906108c 15500
c906108c 15501 /* Look up member function name in fieldlist. */
be2daae6 15502 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15503 {
27bfe10e 15504 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15505 {
15506 flp = &fip->fnfieldlists[i];
15507 break;
15508 }
c906108c
SS
15509 }
15510
be2daae6
TT
15511 /* Create a new fnfieldlist if necessary. */
15512 if (flp == nullptr)
c906108c 15513 {
be2daae6
TT
15514 fip->fnfieldlists.emplace_back ();
15515 flp = &fip->fnfieldlists.back ();
c906108c 15516 flp->name = fieldname;
be2daae6 15517 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15518 }
15519
be2daae6
TT
15520 /* Create a new member function field and add it to the vector of
15521 fnfieldlists. */
15522 flp->fnfields.emplace_back ();
15523 fnp = &flp->fnfields.back ();
3da10d80
KS
15524
15525 /* Delay processing of the physname until later. */
9c37b5ae 15526 if (cu->language == language_cplus)
be2daae6
TT
15527 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15528 die, cu);
3da10d80
KS
15529 else
15530 {
1d06ead6 15531 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15532 fnp->physname = physname ? physname : "";
15533 }
15534
c906108c 15535 fnp->type = alloc_type (objfile);
f792889a
DJ
15536 this_type = read_type_die (die, cu);
15537 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15538 {
f792889a 15539 int nparams = TYPE_NFIELDS (this_type);
c906108c 15540
f792889a 15541 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15542 of the method itself (TYPE_CODE_METHOD). */
15543 smash_to_method_type (fnp->type, type,
f792889a
DJ
15544 TYPE_TARGET_TYPE (this_type),
15545 TYPE_FIELDS (this_type),
15546 TYPE_NFIELDS (this_type),
15547 TYPE_VARARGS (this_type));
c906108c
SS
15548
15549 /* Handle static member functions.
c5aa993b 15550 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15551 member functions. G++ helps GDB by marking the first
15552 parameter for non-static member functions (which is the this
15553 pointer) as artificial. We obtain this information from
15554 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15555 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15556 fnp->voffset = VOFFSET_STATIC;
15557 }
15558 else
b98664d3 15559 complaint (_("member function type missing for '%s'"),
3da10d80 15560 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15561
15562 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15563 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15564 fnp->fcontext = die_containing_type (die, cu);
c906108c 15565
3e43a32a
MS
15566 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15567 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15568
15569 /* Get accessibility. */
e142c38c 15570 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15571 if (attr != nullptr)
aead7601 15572 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15573 else
15574 accessibility = dwarf2_default_access_attribute (die, cu);
15575 switch (accessibility)
c906108c 15576 {
60d5a603
JK
15577 case DW_ACCESS_private:
15578 fnp->is_private = 1;
15579 break;
15580 case DW_ACCESS_protected:
15581 fnp->is_protected = 1;
15582 break;
c906108c
SS
15583 }
15584
b02dede2 15585 /* Check for artificial methods. */
e142c38c 15586 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15587 if (attr && DW_UNSND (attr) != 0)
15588 fnp->is_artificial = 1;
15589
e35000a7
TBA
15590 /* Check for defaulted methods. */
15591 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15592 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15593 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15594
15595 /* Check for deleted methods. */
15596 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15597 if (attr != nullptr && DW_UNSND (attr) != 0)
15598 fnp->is_deleted = 1;
15599
7d27a96d
TT
15600 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15601
0d564a31 15602 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15603 function. For older versions of GCC, this is an offset in the
15604 appropriate virtual table, as specified by DW_AT_containing_type.
15605 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15606 to the object address. */
15607
e142c38c 15608 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15609 if (attr != nullptr)
8e19ed76 15610 {
aec5aa8b 15611 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15612 {
aec5aa8b
TT
15613 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15614 {
15615 /* Old-style GCC. */
15616 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15617 }
15618 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15619 || (DW_BLOCK (attr)->size > 1
15620 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15621 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15622 {
aec5aa8b
TT
15623 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15624 if ((fnp->voffset % cu->header.addr_size) != 0)
15625 dwarf2_complex_location_expr_complaint ();
15626 else
15627 fnp->voffset /= cu->header.addr_size;
15628 fnp->voffset += 2;
15629 }
15630 else
15631 dwarf2_complex_location_expr_complaint ();
15632
15633 if (!fnp->fcontext)
7e993ebf
KS
15634 {
15635 /* If there is no `this' field and no DW_AT_containing_type,
15636 we cannot actually find a base class context for the
15637 vtable! */
15638 if (TYPE_NFIELDS (this_type) == 0
15639 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15640 {
b98664d3 15641 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15642 "function \"%s\" (offset %s)"),
15643 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15644 }
15645 else
15646 {
15647 fnp->fcontext
15648 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15649 }
15650 }
aec5aa8b 15651 }
3690dd37 15652 else if (attr_form_is_section_offset (attr))
8e19ed76 15653 {
4d3c2250 15654 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15655 }
15656 else
15657 {
4d3c2250
KB
15658 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15659 fieldname);
8e19ed76 15660 }
0d564a31 15661 }
d48cc9dd
DJ
15662 else
15663 {
15664 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15665 if (attr && DW_UNSND (attr))
15666 {
15667 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15668 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15669 "but the vtable offset is not specified"),
9d8780f0 15670 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15671 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15672 TYPE_CPLUS_DYNAMIC (type) = 1;
15673 }
15674 }
c906108c
SS
15675}
15676
15677/* Create the vector of member function fields, and attach it to the type. */
15678
15679static void
fba45db2 15680dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15681 struct dwarf2_cu *cu)
c906108c 15682{
b4ba55a1 15683 if (cu->language == language_ada)
a73c6dcd 15684 error (_("unexpected member functions in Ada type"));
b4ba55a1 15685
c906108c
SS
15686 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15687 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15688 TYPE_ALLOC (type,
15689 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15690
be2daae6 15691 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15692 {
be2daae6 15693 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15694 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15695
be2daae6
TT
15696 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15697 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15698 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15699 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15700
15701 for (int k = 0; k < nf.fnfields.size (); ++k)
15702 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15703 }
15704
be2daae6 15705 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15706}
15707
1168df01
JB
15708/* Returns non-zero if NAME is the name of a vtable member in CU's
15709 language, zero otherwise. */
15710static int
15711is_vtable_name (const char *name, struct dwarf2_cu *cu)
15712{
15713 static const char vptr[] = "_vptr";
15714
9c37b5ae
TT
15715 /* Look for the C++ form of the vtable. */
15716 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15717 return 1;
15718
15719 return 0;
15720}
15721
c0dd20ea 15722/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15723 functions, with the ABI-specified layout. If TYPE describes
15724 such a structure, smash it into a member function type.
61049d3b
DJ
15725
15726 GCC shouldn't do this; it should just output pointer to member DIEs.
15727 This is GCC PR debug/28767. */
c0dd20ea 15728
0b92b5bb
TT
15729static void
15730quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15731{
09e2d7c7 15732 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15733
15734 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15735 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15736 return;
c0dd20ea
DJ
15737
15738 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15739 if (TYPE_FIELD_NAME (type, 0) == NULL
15740 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15741 || TYPE_FIELD_NAME (type, 1) == NULL
15742 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15743 return;
c0dd20ea
DJ
15744
15745 /* Find the type of the method. */
0b92b5bb 15746 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15747 if (pfn_type == NULL
15748 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15749 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15750 return;
c0dd20ea
DJ
15751
15752 /* Look for the "this" argument. */
15753 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15754 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15755 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15756 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15757 return;
c0dd20ea 15758
09e2d7c7 15759 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15760 new_type = alloc_type (objfile);
09e2d7c7 15761 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15762 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15763 TYPE_VARARGS (pfn_type));
0b92b5bb 15764 smash_to_methodptr_type (type, new_type);
c0dd20ea 15765}
1168df01 15766
2b4424c3
TT
15767/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15768 appropriate error checking and issuing complaints if there is a
15769 problem. */
15770
15771static ULONGEST
15772get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15773{
15774 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15775
15776 if (attr == nullptr)
15777 return 0;
15778
15779 if (!attr_form_is_constant (attr))
15780 {
b98664d3 15781 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15782 " - DIE at %s [in module %s]"),
15783 sect_offset_str (die->sect_off),
15784 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15785 return 0;
15786 }
15787
15788 ULONGEST align;
15789 if (attr->form == DW_FORM_sdata)
15790 {
15791 LONGEST val = DW_SND (attr);
15792 if (val < 0)
15793 {
b98664d3 15794 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15795 " - DIE at %s [in module %s]"),
15796 sect_offset_str (die->sect_off),
15797 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15798 return 0;
15799 }
15800 align = val;
15801 }
15802 else
15803 align = DW_UNSND (attr);
15804
15805 if (align == 0)
15806 {
b98664d3 15807 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15808 " - DIE at %s [in module %s]"),
15809 sect_offset_str (die->sect_off),
15810 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15811 return 0;
15812 }
15813 if ((align & (align - 1)) != 0)
15814 {
b98664d3 15815 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15816 " - DIE at %s [in module %s]"),
15817 sect_offset_str (die->sect_off),
15818 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15819 return 0;
15820 }
15821
15822 return align;
15823}
15824
15825/* If the DIE has a DW_AT_alignment attribute, use its value to set
15826 the alignment for TYPE. */
15827
15828static void
15829maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15830 struct type *type)
15831{
15832 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15833 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15834 " - DIE at %s [in module %s]"),
15835 sect_offset_str (die->sect_off),
15836 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15837}
685b1105 15838
e35000a7
TBA
15839/* Check if the given VALUE is a valid enum dwarf_calling_convention
15840 constant for a type, according to DWARF5 spec, Table 5.5. */
15841
15842static bool
15843is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15844{
15845 switch (value)
15846 {
15847 case DW_CC_normal:
15848 case DW_CC_pass_by_reference:
15849 case DW_CC_pass_by_value:
15850 return true;
15851
15852 default:
15853 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15854 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15855 return false;
15856 }
15857}
15858
d0922fcf
TBA
15859/* Check if the given VALUE is a valid enum dwarf_calling_convention
15860 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15861 also according to GNU-specific values (see include/dwarf2.h). */
15862
15863static bool
15864is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15865{
15866 switch (value)
15867 {
15868 case DW_CC_normal:
15869 case DW_CC_program:
15870 case DW_CC_nocall:
15871 return true;
15872
15873 case DW_CC_GNU_renesas_sh:
15874 case DW_CC_GNU_borland_fastcall_i386:
15875 case DW_CC_GDB_IBM_OpenCL:
15876 return true;
15877
15878 default:
15879 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15880 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15881 return false;
15882 }
15883}
15884
c906108c 15885/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15886 (definition) to create a type for the structure or union. Fill in
15887 the type's name and general properties; the members will not be
83655187
DE
15888 processed until process_structure_scope. A symbol table entry for
15889 the type will also not be done until process_structure_scope (assuming
15890 the type has a name).
c906108c 15891
c767944b
DJ
15892 NOTE: we need to call these functions regardless of whether or not the
15893 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15894 structure or union. This gets the type entered into our set of
83655187 15895 user defined types. */
c906108c 15896
f792889a 15897static struct type *
134d01f1 15898read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15899{
518817b3 15900 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15901 struct type *type;
15902 struct attribute *attr;
15d034d0 15903 const char *name;
c906108c 15904
348e048f
DE
15905 /* If the definition of this type lives in .debug_types, read that type.
15906 Don't follow DW_AT_specification though, that will take us back up
15907 the chain and we want to go down. */
45e58e77 15908 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15909 if (attr != nullptr)
348e048f 15910 {
ac9ec31b 15911 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15912
ac9ec31b 15913 /* The type's CU may not be the same as CU.
02142a6c 15914 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15915 return set_die_type (die, type, cu);
15916 }
15917
c0dd20ea 15918 type = alloc_type (objfile);
c906108c 15919 INIT_CPLUS_SPECIFIC (type);
93311388 15920
39cbfefa
DJ
15921 name = dwarf2_name (die, cu);
15922 if (name != NULL)
c906108c 15923 {
987504bb 15924 if (cu->language == language_cplus
c44af4eb
TT
15925 || cu->language == language_d
15926 || cu->language == language_rust)
63d06c5c 15927 {
15d034d0 15928 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15929
15930 /* dwarf2_full_name might have already finished building the DIE's
15931 type. If so, there is no need to continue. */
15932 if (get_die_type (die, cu) != NULL)
15933 return get_die_type (die, cu);
15934
e86ca25f 15935 TYPE_NAME (type) = full_name;
63d06c5c
DC
15936 }
15937 else
15938 {
d8151005
DJ
15939 /* The name is already allocated along with this objfile, so
15940 we don't need to duplicate it for the type. */
e86ca25f 15941 TYPE_NAME (type) = name;
63d06c5c 15942 }
c906108c
SS
15943 }
15944
15945 if (die->tag == DW_TAG_structure_type)
15946 {
15947 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15948 }
15949 else if (die->tag == DW_TAG_union_type)
15950 {
15951 TYPE_CODE (type) = TYPE_CODE_UNION;
15952 }
2ddeaf8a
TT
15953 else if (die->tag == DW_TAG_variant_part)
15954 {
15955 TYPE_CODE (type) = TYPE_CODE_UNION;
15956 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15957 }
c906108c
SS
15958 else
15959 {
4753d33b 15960 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15961 }
15962
0cc2414c
TT
15963 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15964 TYPE_DECLARED_CLASS (type) = 1;
15965
e35000a7
TBA
15966 /* Store the calling convention in the type if it's available in
15967 the die. Otherwise the calling convention remains set to
15968 the default value DW_CC_normal. */
15969 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15970 if (attr != nullptr
15971 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15972 {
15973 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15974 TYPE_CPLUS_CALLING_CONVENTION (type)
15975 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15976 }
15977
e142c38c 15978 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15979 if (attr != nullptr)
c906108c 15980 {
155bfbd3
JB
15981 if (attr_form_is_constant (attr))
15982 TYPE_LENGTH (type) = DW_UNSND (attr);
15983 else
15984 {
15985 /* For the moment, dynamic type sizes are not supported
15986 by GDB's struct type. The actual size is determined
15987 on-demand when resolving the type of a given object,
15988 so set the type's length to zero for now. Otherwise,
15989 we record an expression as the length, and that expression
15990 could lead to a very large value, which could eventually
15991 lead to us trying to allocate that much memory when creating
15992 a value of that type. */
15993 TYPE_LENGTH (type) = 0;
15994 }
c906108c
SS
15995 }
15996 else
15997 {
15998 TYPE_LENGTH (type) = 0;
15999 }
16000
2b4424c3
TT
16001 maybe_set_alignment (cu, die, type);
16002
5230b05a 16003 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16004 {
5230b05a
WT
16005 /* ICC<14 does not output the required DW_AT_declaration on
16006 incomplete types, but gives them a size of zero. */
422b1cb0 16007 TYPE_STUB (type) = 1;
685b1105
JK
16008 }
16009 else
16010 TYPE_STUB_SUPPORTED (type) = 1;
16011
dc718098 16012 if (die_is_declaration (die, cu))
876cecd0 16013 TYPE_STUB (type) = 1;
a6c727b2
DJ
16014 else if (attr == NULL && die->child == NULL
16015 && producer_is_realview (cu->producer))
16016 /* RealView does not output the required DW_AT_declaration
16017 on incomplete types. */
16018 TYPE_STUB (type) = 1;
dc718098 16019
c906108c
SS
16020 /* We need to add the type field to the die immediately so we don't
16021 infinitely recurse when dealing with pointers to the structure
0963b4bd 16022 type within the structure itself. */
1c379e20 16023 set_die_type (die, type, cu);
c906108c 16024
7e314c57
JK
16025 /* set_die_type should be already done. */
16026 set_descriptive_type (type, die, cu);
16027
c767944b
DJ
16028 return type;
16029}
16030
2ddeaf8a
TT
16031/* A helper for process_structure_scope that handles a single member
16032 DIE. */
16033
16034static void
16035handle_struct_member_die (struct die_info *child_die, struct type *type,
16036 struct field_info *fi,
16037 std::vector<struct symbol *> *template_args,
16038 struct dwarf2_cu *cu)
16039{
16040 if (child_die->tag == DW_TAG_member
16041 || child_die->tag == DW_TAG_variable
16042 || child_die->tag == DW_TAG_variant_part)
16043 {
16044 /* NOTE: carlton/2002-11-05: A C++ static data member
16045 should be a DW_TAG_member that is a declaration, but
16046 all versions of G++ as of this writing (so through at
16047 least 3.2.1) incorrectly generate DW_TAG_variable
16048 tags for them instead. */
16049 dwarf2_add_field (fi, child_die, cu);
16050 }
16051 else if (child_die->tag == DW_TAG_subprogram)
16052 {
16053 /* Rust doesn't have member functions in the C++ sense.
16054 However, it does emit ordinary functions as children
16055 of a struct DIE. */
16056 if (cu->language == language_rust)
16057 read_func_scope (child_die, cu);
16058 else
16059 {
16060 /* C++ member function. */
16061 dwarf2_add_member_fn (fi, child_die, type, cu);
16062 }
16063 }
16064 else if (child_die->tag == DW_TAG_inheritance)
16065 {
16066 /* C++ base class field. */
16067 dwarf2_add_field (fi, child_die, cu);
16068 }
16069 else if (type_can_define_types (child_die))
16070 dwarf2_add_type_defn (fi, child_die, cu);
16071 else if (child_die->tag == DW_TAG_template_type_param
16072 || child_die->tag == DW_TAG_template_value_param)
16073 {
16074 struct symbol *arg = new_symbol (child_die, NULL, cu);
16075
16076 if (arg != NULL)
16077 template_args->push_back (arg);
16078 }
16079 else if (child_die->tag == DW_TAG_variant)
16080 {
16081 /* In a variant we want to get the discriminant and also add a
16082 field for our sole member child. */
16083 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16084
bde09ab7 16085 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
16086 variant_child != NULL;
16087 variant_child = sibling_die (variant_child))
16088 {
16089 if (variant_child->tag == DW_TAG_member)
16090 {
16091 handle_struct_member_die (variant_child, type, fi,
16092 template_args, cu);
16093 /* Only handle the one. */
16094 break;
16095 }
16096 }
16097
16098 /* We don't handle this but we might as well report it if we see
16099 it. */
16100 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 16101 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
16102 " - DIE at %s [in module %s]"),
16103 sect_offset_str (child_die->sect_off),
16104 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16105
16106 /* The first field was just added, so we can stash the
16107 discriminant there. */
be2daae6 16108 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16109 if (discr == NULL)
be2daae6 16110 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16111 else
be2daae6 16112 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16113 }
16114}
16115
c767944b
DJ
16116/* Finish creating a structure or union type, including filling in
16117 its members and creating a symbol for it. */
16118
16119static void
16120process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16121{
518817b3 16122 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16123 struct die_info *child_die;
c767944b
DJ
16124 struct type *type;
16125
16126 type = get_die_type (die, cu);
16127 if (type == NULL)
16128 type = read_structure_type (die, cu);
16129
2ddeaf8a
TT
16130 /* When reading a DW_TAG_variant_part, we need to notice when we
16131 read the discriminant member, so we can record it later in the
16132 discriminant_info. */
16133 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 16134 sect_offset discr_offset {};
3e1d3d8c 16135 bool has_template_parameters = false;
2ddeaf8a
TT
16136
16137 if (is_variant_part)
16138 {
16139 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16140 if (discr == NULL)
16141 {
16142 /* Maybe it's a univariant form, an extension we support.
16143 In this case arrange not to check the offset. */
16144 is_variant_part = false;
16145 }
16146 else if (attr_form_is_ref (discr))
16147 {
16148 struct dwarf2_cu *target_cu = cu;
16149 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16150
16151 discr_offset = target_die->sect_off;
16152 }
16153 else
16154 {
b98664d3 16155 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16156 " - DIE at %s [in module %s]"),
16157 sect_offset_str (die->sect_off),
16158 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16159 is_variant_part = false;
16160 }
16161 }
16162
e142c38c 16163 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16164 {
16165 struct field_info fi;
2f4732b0 16166 std::vector<struct symbol *> template_args;
c906108c 16167
639d11d3 16168 child_die = die->child;
c906108c
SS
16169
16170 while (child_die && child_die->tag)
16171 {
2ddeaf8a 16172 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16173
2ddeaf8a 16174 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16175 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16176
c906108c
SS
16177 child_die = sibling_die (child_die);
16178 }
16179
34eaf542 16180 /* Attach template arguments to type. */
2f4732b0 16181 if (!template_args.empty ())
34eaf542 16182 {
3e1d3d8c 16183 has_template_parameters = true;
34eaf542 16184 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16185 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16186 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16187 = XOBNEWVEC (&objfile->objfile_obstack,
16188 struct symbol *,
16189 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16190 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16191 template_args.data (),
34eaf542
TT
16192 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16193 * sizeof (struct symbol *)));
34eaf542
TT
16194 }
16195
c906108c
SS
16196 /* Attach fields and member functions to the type. */
16197 if (fi.nfields)
e7c27a73 16198 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16199 if (!fi.fnfieldlists.empty ())
c906108c 16200 {
e7c27a73 16201 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16202
c5aa993b 16203 /* Get the type which refers to the base class (possibly this
c906108c 16204 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16205 class from the DW_AT_containing_type attribute. This use of
16206 DW_AT_containing_type is a GNU extension. */
c906108c 16207
e142c38c 16208 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16209 {
e7c27a73 16210 struct type *t = die_containing_type (die, cu);
c906108c 16211
ae6ae975 16212 set_type_vptr_basetype (type, t);
c906108c
SS
16213 if (type == t)
16214 {
c906108c
SS
16215 int i;
16216
16217 /* Our own class provides vtbl ptr. */
16218 for (i = TYPE_NFIELDS (t) - 1;
16219 i >= TYPE_N_BASECLASSES (t);
16220 --i)
16221 {
0d5cff50 16222 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16223
1168df01 16224 if (is_vtable_name (fieldname, cu))
c906108c 16225 {
ae6ae975 16226 set_type_vptr_fieldno (type, i);
c906108c
SS
16227 break;
16228 }
16229 }
16230
16231 /* Complain if virtual function table field not found. */
16232 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16233 complaint (_("virtual function table pointer "
3e43a32a 16234 "not found when defining class '%s'"),
e86ca25f 16235 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16236 }
16237 else
16238 {
ae6ae975 16239 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16240 }
16241 }
f6235d4c 16242 else if (cu->producer
61012eef 16243 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16244 {
16245 /* The IBM XLC compiler does not provide direct indication
16246 of the containing type, but the vtable pointer is
16247 always named __vfp. */
16248
16249 int i;
16250
16251 for (i = TYPE_NFIELDS (type) - 1;
16252 i >= TYPE_N_BASECLASSES (type);
16253 --i)
16254 {
16255 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16256 {
ae6ae975
DE
16257 set_type_vptr_fieldno (type, i);
16258 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16259 break;
16260 }
16261 }
16262 }
c906108c 16263 }
98751a41
JK
16264
16265 /* Copy fi.typedef_field_list linked list elements content into the
16266 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16267 if (!fi.typedef_field_list.empty ())
98751a41 16268 {
be2daae6 16269 int count = fi.typedef_field_list.size ();
98751a41 16270
a0d7a4ff 16271 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16272 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16273 = ((struct decl_field *)
be2daae6
TT
16274 TYPE_ALLOC (type,
16275 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16276 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16277
be2daae6
TT
16278 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16279 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16280 }
c767944b 16281
883fd55a
KS
16282 /* Copy fi.nested_types_list linked list elements content into the
16283 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16284 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16285 {
be2daae6 16286 int count = fi.nested_types_list.size ();
883fd55a
KS
16287
16288 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16289 TYPE_NESTED_TYPES_ARRAY (type)
16290 = ((struct decl_field *)
be2daae6
TT
16291 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16292 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16293
be2daae6
TT
16294 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16295 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16296 }
c906108c 16297 }
63d06c5c 16298
bb5ed363 16299 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16300 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16301 cu->rust_unions.push_back (type);
0b92b5bb 16302
90aeadfc
DC
16303 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16304 snapshots) has been known to create a die giving a declaration
16305 for a class that has, as a child, a die giving a definition for a
16306 nested class. So we have to process our children even if the
16307 current die is a declaration. Normally, of course, a declaration
16308 won't have any children at all. */
134d01f1 16309
ca040673
DE
16310 child_die = die->child;
16311
90aeadfc
DC
16312 while (child_die != NULL && child_die->tag)
16313 {
16314 if (child_die->tag == DW_TAG_member
16315 || child_die->tag == DW_TAG_variable
34eaf542
TT
16316 || child_die->tag == DW_TAG_inheritance
16317 || child_die->tag == DW_TAG_template_value_param
16318 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16319 {
90aeadfc 16320 /* Do nothing. */
134d01f1 16321 }
90aeadfc
DC
16322 else
16323 process_die (child_die, cu);
134d01f1 16324
90aeadfc 16325 child_die = sibling_die (child_die);
134d01f1
DJ
16326 }
16327
fa4028e9
JB
16328 /* Do not consider external references. According to the DWARF standard,
16329 these DIEs are identified by the fact that they have no byte_size
16330 attribute, and a declaration attribute. */
16331 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16332 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16333 {
16334 struct symbol *sym = new_symbol (die, type, cu);
16335
16336 if (has_template_parameters)
16337 {
a776957c
TT
16338 struct symtab *symtab;
16339 if (sym != nullptr)
16340 symtab = symbol_symtab (sym);
16341 else if (cu->line_header != nullptr)
16342 {
16343 /* Any related symtab will do. */
16344 symtab
7ba99d21 16345 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16346 }
16347 else
16348 {
16349 symtab = nullptr;
16350 complaint (_("could not find suitable "
16351 "symtab for template parameter"
16352 " - DIE at %s [in module %s]"),
16353 sect_offset_str (die->sect_off),
16354 objfile_name (objfile));
16355 }
16356
16357 if (symtab != nullptr)
16358 {
16359 /* Make sure that the symtab is set on the new symbols.
16360 Even though they don't appear in this symtab directly,
16361 other parts of gdb assume that symbols do, and this is
16362 reasonably true. */
16363 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16364 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16365 }
3e1d3d8c
TT
16366 }
16367 }
134d01f1
DJ
16368}
16369
55426c9d
JB
16370/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16371 update TYPE using some information only available in DIE's children. */
16372
16373static void
16374update_enumeration_type_from_children (struct die_info *die,
16375 struct type *type,
16376 struct dwarf2_cu *cu)
16377{
60f7655a 16378 struct die_info *child_die;
55426c9d
JB
16379 int unsigned_enum = 1;
16380 int flag_enum = 1;
16381 ULONGEST mask = 0;
55426c9d 16382
8268c778 16383 auto_obstack obstack;
55426c9d 16384
60f7655a
DE
16385 for (child_die = die->child;
16386 child_die != NULL && child_die->tag;
16387 child_die = sibling_die (child_die))
55426c9d
JB
16388 {
16389 struct attribute *attr;
16390 LONGEST value;
16391 const gdb_byte *bytes;
16392 struct dwarf2_locexpr_baton *baton;
16393 const char *name;
60f7655a 16394
55426c9d
JB
16395 if (child_die->tag != DW_TAG_enumerator)
16396 continue;
16397
16398 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16399 if (attr == NULL)
16400 continue;
16401
16402 name = dwarf2_name (child_die, cu);
16403 if (name == NULL)
16404 name = "<anonymous enumerator>";
16405
16406 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16407 &value, &bytes, &baton);
16408 if (value < 0)
16409 {
16410 unsigned_enum = 0;
16411 flag_enum = 0;
16412 }
16413 else if ((mask & value) != 0)
16414 flag_enum = 0;
16415 else
16416 mask |= value;
16417
16418 /* If we already know that the enum type is neither unsigned, nor
16419 a flag type, no need to look at the rest of the enumerates. */
16420 if (!unsigned_enum && !flag_enum)
16421 break;
55426c9d
JB
16422 }
16423
16424 if (unsigned_enum)
16425 TYPE_UNSIGNED (type) = 1;
16426 if (flag_enum)
16427 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16428}
16429
134d01f1
DJ
16430/* Given a DW_AT_enumeration_type die, set its type. We do not
16431 complete the type's fields yet, or create any symbols. */
c906108c 16432
f792889a 16433static struct type *
134d01f1 16434read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16435{
518817b3 16436 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16437 struct type *type;
c906108c 16438 struct attribute *attr;
0114d602 16439 const char *name;
134d01f1 16440
348e048f
DE
16441 /* If the definition of this type lives in .debug_types, read that type.
16442 Don't follow DW_AT_specification though, that will take us back up
16443 the chain and we want to go down. */
45e58e77 16444 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16445 if (attr != nullptr)
348e048f 16446 {
ac9ec31b 16447 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16448
ac9ec31b 16449 /* The type's CU may not be the same as CU.
02142a6c 16450 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16451 return set_die_type (die, type, cu);
16452 }
16453
c906108c
SS
16454 type = alloc_type (objfile);
16455
16456 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16457 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16458 if (name != NULL)
e86ca25f 16459 TYPE_NAME (type) = name;
c906108c 16460
0626fc76
TT
16461 attr = dwarf2_attr (die, DW_AT_type, cu);
16462 if (attr != NULL)
16463 {
16464 struct type *underlying_type = die_type (die, cu);
16465
16466 TYPE_TARGET_TYPE (type) = underlying_type;
16467 }
16468
e142c38c 16469 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16470 if (attr != nullptr)
c906108c
SS
16471 {
16472 TYPE_LENGTH (type) = DW_UNSND (attr);
16473 }
16474 else
16475 {
16476 TYPE_LENGTH (type) = 0;
16477 }
16478
2b4424c3
TT
16479 maybe_set_alignment (cu, die, type);
16480
137033e9
JB
16481 /* The enumeration DIE can be incomplete. In Ada, any type can be
16482 declared as private in the package spec, and then defined only
16483 inside the package body. Such types are known as Taft Amendment
16484 Types. When another package uses such a type, an incomplete DIE
16485 may be generated by the compiler. */
02eb380e 16486 if (die_is_declaration (die, cu))
876cecd0 16487 TYPE_STUB (type) = 1;
02eb380e 16488
0626fc76
TT
16489 /* Finish the creation of this type by using the enum's children.
16490 We must call this even when the underlying type has been provided
16491 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16492 update_enumeration_type_from_children (die, type, cu);
16493
0626fc76
TT
16494 /* If this type has an underlying type that is not a stub, then we
16495 may use its attributes. We always use the "unsigned" attribute
16496 in this situation, because ordinarily we guess whether the type
16497 is unsigned -- but the guess can be wrong and the underlying type
16498 can tell us the reality. However, we defer to a local size
16499 attribute if one exists, because this lets the compiler override
16500 the underlying type if needed. */
16501 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16502 {
16503 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16504 if (TYPE_LENGTH (type) == 0)
16505 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16506 if (TYPE_RAW_ALIGN (type) == 0
16507 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16508 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16509 }
16510
3d567982
TT
16511 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16512
f792889a 16513 return set_die_type (die, type, cu);
134d01f1
DJ
16514}
16515
16516/* Given a pointer to a die which begins an enumeration, process all
16517 the dies that define the members of the enumeration, and create the
16518 symbol for the enumeration type.
16519
16520 NOTE: We reverse the order of the element list. */
16521
16522static void
16523process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16524{
f792889a 16525 struct type *this_type;
134d01f1 16526
f792889a
DJ
16527 this_type = get_die_type (die, cu);
16528 if (this_type == NULL)
16529 this_type = read_enumeration_type (die, cu);
9dc481d3 16530
639d11d3 16531 if (die->child != NULL)
c906108c 16532 {
9dc481d3
DE
16533 struct die_info *child_die;
16534 struct symbol *sym;
43816ebc 16535 std::vector<struct field> fields;
15d034d0 16536 const char *name;
9dc481d3 16537
639d11d3 16538 child_die = die->child;
c906108c
SS
16539 while (child_die && child_die->tag)
16540 {
16541 if (child_die->tag != DW_TAG_enumerator)
16542 {
e7c27a73 16543 process_die (child_die, cu);
c906108c
SS
16544 }
16545 else
16546 {
39cbfefa
DJ
16547 name = dwarf2_name (child_die, cu);
16548 if (name)
c906108c 16549 {
f792889a 16550 sym = new_symbol (child_die, this_type, cu);
c906108c 16551
43816ebc
TT
16552 fields.emplace_back ();
16553 struct field &field = fields.back ();
c906108c 16554
43816ebc
TT
16555 FIELD_NAME (field) = sym->linkage_name ();
16556 FIELD_TYPE (field) = NULL;
16557 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16558 FIELD_BITSIZE (field) = 0;
c906108c
SS
16559 }
16560 }
16561
16562 child_die = sibling_die (child_die);
16563 }
16564
43816ebc 16565 if (!fields.empty ())
c906108c 16566 {
43816ebc 16567 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 16568 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
16569 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16570 memcpy (TYPE_FIELDS (this_type), fields.data (),
16571 sizeof (struct field) * fields.size ());
c906108c 16572 }
c906108c 16573 }
134d01f1 16574
6c83ed52
TT
16575 /* If we are reading an enum from a .debug_types unit, and the enum
16576 is a declaration, and the enum is not the signatured type in the
16577 unit, then we do not want to add a symbol for it. Adding a
16578 symbol would in some cases obscure the true definition of the
16579 enum, giving users an incomplete type when the definition is
16580 actually available. Note that we do not want to do this for all
16581 enums which are just declarations, because C++0x allows forward
16582 enum declarations. */
3019eac3 16583 if (cu->per_cu->is_debug_types
6c83ed52
TT
16584 && die_is_declaration (die, cu))
16585 {
52dc124a 16586 struct signatured_type *sig_type;
6c83ed52 16587
c0f78cd4 16588 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16589 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16590 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16591 return;
16592 }
16593
f792889a 16594 new_symbol (die, this_type, cu);
c906108c
SS
16595}
16596
16597/* Extract all information from a DW_TAG_array_type DIE and put it in
16598 the DIE's type field. For now, this only handles one dimensional
16599 arrays. */
16600
f792889a 16601static struct type *
e7c27a73 16602read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16603{
518817b3 16604 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16605 struct die_info *child_die;
7e314c57 16606 struct type *type;
c906108c 16607 struct type *element_type, *range_type, *index_type;
c906108c 16608 struct attribute *attr;
15d034d0 16609 const char *name;
a405673c 16610 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16611 unsigned int bit_stride = 0;
c906108c 16612
e7c27a73 16613 element_type = die_type (die, cu);
c906108c 16614
7e314c57
JK
16615 /* The die_type call above may have already set the type for this DIE. */
16616 type = get_die_type (die, cu);
16617 if (type)
16618 return type;
16619
dc53a7ad
JB
16620 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16621 if (attr != NULL)
a405673c
JB
16622 {
16623 int stride_ok;
9a49df9d
AB
16624 struct type *prop_type
16625 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16626
16627 byte_stride_prop
16628 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16629 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16630 prop_type);
a405673c
JB
16631 if (!stride_ok)
16632 {
b98664d3 16633 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16634 " - DIE at %s [in module %s]"),
16635 sect_offset_str (die->sect_off),
518817b3 16636 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16637 /* Ignore this attribute. We will likely not be able to print
16638 arrays of this type correctly, but there is little we can do
16639 to help if we cannot read the attribute's value. */
16640 byte_stride_prop = NULL;
16641 }
16642 }
dc53a7ad
JB
16643
16644 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16645 if (attr != NULL)
16646 bit_stride = DW_UNSND (attr);
16647
c906108c
SS
16648 /* Irix 6.2 native cc creates array types without children for
16649 arrays with unspecified length. */
639d11d3 16650 if (die->child == NULL)
c906108c 16651 {
46bf5051 16652 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16653 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16654 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16655 byte_stride_prop, bit_stride);
f792889a 16656 return set_die_type (die, type, cu);
c906108c
SS
16657 }
16658
791afaa2 16659 std::vector<struct type *> range_types;
639d11d3 16660 child_die = die->child;
c906108c
SS
16661 while (child_die && child_die->tag)
16662 {
16663 if (child_die->tag == DW_TAG_subrange_type)
16664 {
f792889a 16665 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16666
f792889a 16667 if (child_type != NULL)
a02abb62 16668 {
0963b4bd
MS
16669 /* The range type was succesfully read. Save it for the
16670 array type creation. */
791afaa2 16671 range_types.push_back (child_type);
a02abb62 16672 }
c906108c
SS
16673 }
16674 child_die = sibling_die (child_die);
16675 }
16676
16677 /* Dwarf2 dimensions are output from left to right, create the
16678 necessary array types in backwards order. */
7ca2d3a3 16679
c906108c 16680 type = element_type;
7ca2d3a3
DL
16681
16682 if (read_array_order (die, cu) == DW_ORD_col_major)
16683 {
16684 int i = 0;
9a619af0 16685
791afaa2 16686 while (i < range_types.size ())
dc53a7ad 16687 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16688 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16689 }
16690 else
16691 {
791afaa2 16692 size_t ndim = range_types.size ();
7ca2d3a3 16693 while (ndim-- > 0)
dc53a7ad 16694 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16695 byte_stride_prop, bit_stride);
7ca2d3a3 16696 }
c906108c 16697
f5f8a009
EZ
16698 /* Understand Dwarf2 support for vector types (like they occur on
16699 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16700 array type. This is not part of the Dwarf2/3 standard yet, but a
16701 custom vendor extension. The main difference between a regular
16702 array and the vector variant is that vectors are passed by value
16703 to functions. */
e142c38c 16704 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16705 if (attr != nullptr)
ea37ba09 16706 make_vector_type (type);
f5f8a009 16707
dbc98a8b
KW
16708 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16709 implementation may choose to implement triple vectors using this
16710 attribute. */
16711 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16712 if (attr != nullptr)
dbc98a8b
KW
16713 {
16714 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16715 TYPE_LENGTH (type) = DW_UNSND (attr);
16716 else
b98664d3 16717 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16718 "than the total size of elements"));
dbc98a8b
KW
16719 }
16720
39cbfefa
DJ
16721 name = dwarf2_name (die, cu);
16722 if (name)
16723 TYPE_NAME (type) = name;
6e70227d 16724
2b4424c3
TT
16725 maybe_set_alignment (cu, die, type);
16726
0963b4bd 16727 /* Install the type in the die. */
7e314c57
JK
16728 set_die_type (die, type, cu);
16729
16730 /* set_die_type should be already done. */
b4ba55a1
JB
16731 set_descriptive_type (type, die, cu);
16732
7e314c57 16733 return type;
c906108c
SS
16734}
16735
7ca2d3a3 16736static enum dwarf_array_dim_ordering
6e70227d 16737read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16738{
16739 struct attribute *attr;
16740
16741 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16742
435d3d88 16743 if (attr != nullptr)
aead7601 16744 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16745
0963b4bd
MS
16746 /* GNU F77 is a special case, as at 08/2004 array type info is the
16747 opposite order to the dwarf2 specification, but data is still
16748 laid out as per normal fortran.
7ca2d3a3 16749
0963b4bd
MS
16750 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16751 version checking. */
7ca2d3a3 16752
905e0470
PM
16753 if (cu->language == language_fortran
16754 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16755 {
16756 return DW_ORD_row_major;
16757 }
16758
6e70227d 16759 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16760 {
16761 case array_column_major:
16762 return DW_ORD_col_major;
16763 case array_row_major:
16764 default:
16765 return DW_ORD_row_major;
16766 };
16767}
16768
72019c9c 16769/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16770 the DIE's type field. */
72019c9c 16771
f792889a 16772static struct type *
72019c9c
GM
16773read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16774{
7e314c57
JK
16775 struct type *domain_type, *set_type;
16776 struct attribute *attr;
f792889a 16777
7e314c57
JK
16778 domain_type = die_type (die, cu);
16779
16780 /* The die_type call above may have already set the type for this DIE. */
16781 set_type = get_die_type (die, cu);
16782 if (set_type)
16783 return set_type;
16784
16785 set_type = create_set_type (NULL, domain_type);
16786
16787 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16788 if (attr != nullptr)
d09039dd 16789 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16790
2b4424c3
TT
16791 maybe_set_alignment (cu, die, set_type);
16792
f792889a 16793 return set_die_type (die, set_type, cu);
72019c9c 16794}
7ca2d3a3 16795
0971de02
TT
16796/* A helper for read_common_block that creates a locexpr baton.
16797 SYM is the symbol which we are marking as computed.
16798 COMMON_DIE is the DIE for the common block.
16799 COMMON_LOC is the location expression attribute for the common
16800 block itself.
16801 MEMBER_LOC is the location expression attribute for the particular
16802 member of the common block that we are processing.
16803 CU is the CU from which the above come. */
16804
16805static void
16806mark_common_block_symbol_computed (struct symbol *sym,
16807 struct die_info *common_die,
16808 struct attribute *common_loc,
16809 struct attribute *member_loc,
16810 struct dwarf2_cu *cu)
16811{
518817b3
SM
16812 struct dwarf2_per_objfile *dwarf2_per_objfile
16813 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16814 struct objfile *objfile = dwarf2_per_objfile->objfile;
16815 struct dwarf2_locexpr_baton *baton;
16816 gdb_byte *ptr;
16817 unsigned int cu_off;
16818 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16819 LONGEST offset = 0;
16820
16821 gdb_assert (common_loc && member_loc);
16822 gdb_assert (attr_form_is_block (common_loc));
16823 gdb_assert (attr_form_is_block (member_loc)
16824 || attr_form_is_constant (member_loc));
16825
8d749320 16826 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16827 baton->per_cu = cu->per_cu;
16828 gdb_assert (baton->per_cu);
16829
16830 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16831
16832 if (attr_form_is_constant (member_loc))
16833 {
16834 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16835 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16836 }
16837 else
16838 baton->size += DW_BLOCK (member_loc)->size;
16839
224c3ddb 16840 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16841 baton->data = ptr;
16842
16843 *ptr++ = DW_OP_call4;
9c541725 16844 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16845 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16846 ptr += 4;
16847
16848 if (attr_form_is_constant (member_loc))
16849 {
16850 *ptr++ = DW_OP_addr;
16851 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16852 ptr += cu->header.addr_size;
16853 }
16854 else
16855 {
16856 /* We have to copy the data here, because DW_OP_call4 will only
16857 use a DW_AT_location attribute. */
16858 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16859 ptr += DW_BLOCK (member_loc)->size;
16860 }
16861
16862 *ptr++ = DW_OP_plus;
16863 gdb_assert (ptr - baton->data == baton->size);
16864
0971de02 16865 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16866 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16867}
16868
4357ac6c
TT
16869/* Create appropriate locally-scoped variables for all the
16870 DW_TAG_common_block entries. Also create a struct common_block
16871 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16872 is used to separate the common blocks name namespace from regular
4357ac6c 16873 variable names. */
c906108c
SS
16874
16875static void
e7c27a73 16876read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16877{
0971de02
TT
16878 struct attribute *attr;
16879
16880 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16881 if (attr != nullptr)
0971de02
TT
16882 {
16883 /* Support the .debug_loc offsets. */
16884 if (attr_form_is_block (attr))
16885 {
16886 /* Ok. */
16887 }
16888 else if (attr_form_is_section_offset (attr))
16889 {
16890 dwarf2_complex_location_expr_complaint ();
16891 attr = NULL;
16892 }
16893 else
16894 {
16895 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16896 "common block member");
16897 attr = NULL;
16898 }
16899 }
16900
639d11d3 16901 if (die->child != NULL)
c906108c 16902 {
518817b3 16903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16904 struct die_info *child_die;
16905 size_t n_entries = 0, size;
16906 struct common_block *common_block;
16907 struct symbol *sym;
74ac6d43 16908
4357ac6c
TT
16909 for (child_die = die->child;
16910 child_die && child_die->tag;
16911 child_die = sibling_die (child_die))
16912 ++n_entries;
16913
16914 size = (sizeof (struct common_block)
16915 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16916 common_block
16917 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16918 size);
4357ac6c
TT
16919 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16920 common_block->n_entries = 0;
16921
16922 for (child_die = die->child;
16923 child_die && child_die->tag;
16924 child_die = sibling_die (child_die))
16925 {
16926 /* Create the symbol in the DW_TAG_common_block block in the current
16927 symbol scope. */
e7c27a73 16928 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16929 if (sym != NULL)
16930 {
16931 struct attribute *member_loc;
16932
16933 common_block->contents[common_block->n_entries++] = sym;
16934
16935 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16936 cu);
16937 if (member_loc)
16938 {
16939 /* GDB has handled this for a long time, but it is
16940 not specified by DWARF. It seems to have been
16941 emitted by gfortran at least as recently as:
16942 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16943 complaint (_("Variable in common block has "
0971de02 16944 "DW_AT_data_member_location "
9d8780f0
SM
16945 "- DIE at %s [in module %s]"),
16946 sect_offset_str (child_die->sect_off),
518817b3 16947 objfile_name (objfile));
0971de02
TT
16948
16949 if (attr_form_is_section_offset (member_loc))
16950 dwarf2_complex_location_expr_complaint ();
16951 else if (attr_form_is_constant (member_loc)
16952 || attr_form_is_block (member_loc))
16953 {
435d3d88 16954 if (attr != nullptr)
0971de02
TT
16955 mark_common_block_symbol_computed (sym, die, attr,
16956 member_loc, cu);
16957 }
16958 else
16959 dwarf2_complex_location_expr_complaint ();
16960 }
16961 }
c906108c 16962 }
4357ac6c
TT
16963
16964 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16965 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16966 }
16967}
16968
0114d602 16969/* Create a type for a C++ namespace. */
d9fa45fe 16970
0114d602
DJ
16971static struct type *
16972read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16973{
518817b3 16974 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16975 const char *previous_prefix, *name;
9219021c 16976 int is_anonymous;
0114d602
DJ
16977 struct type *type;
16978
16979 /* For extensions, reuse the type of the original namespace. */
16980 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16981 {
16982 struct die_info *ext_die;
16983 struct dwarf2_cu *ext_cu = cu;
9a619af0 16984
0114d602
DJ
16985 ext_die = dwarf2_extension (die, &ext_cu);
16986 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16987
16988 /* EXT_CU may not be the same as CU.
02142a6c 16989 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16990 return set_die_type (die, type, cu);
16991 }
9219021c 16992
e142c38c 16993 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16994
16995 /* Now build the name of the current namespace. */
16996
0114d602
DJ
16997 previous_prefix = determine_prefix (die, cu);
16998 if (previous_prefix[0] != '\0')
16999 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17000 previous_prefix, name, 0, cu);
0114d602
DJ
17001
17002 /* Create the type. */
19f392bc 17003 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17004
60531b24 17005 return set_die_type (die, type, cu);
0114d602
DJ
17006}
17007
22cee43f 17008/* Read a namespace scope. */
0114d602
DJ
17009
17010static void
17011read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17012{
518817b3 17013 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17014 int is_anonymous;
9219021c 17015
5c4e30ca
DC
17016 /* Add a symbol associated to this if we haven't seen the namespace
17017 before. Also, add a using directive if it's an anonymous
17018 namespace. */
9219021c 17019
f2f0e013 17020 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17021 {
17022 struct type *type;
17023
0114d602 17024 type = read_type_die (die, cu);
e7c27a73 17025 new_symbol (die, type, cu);
5c4e30ca 17026
e8e80198 17027 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17028 if (is_anonymous)
0114d602
DJ
17029 {
17030 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17031
eb1e02fd 17032 std::vector<const char *> excludes;
804d2729 17033 add_using_directive (using_directives (cu),
22cee43f 17034 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 17035 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17036 }
5c4e30ca 17037 }
9219021c 17038
639d11d3 17039 if (die->child != NULL)
d9fa45fe 17040 {
639d11d3 17041 struct die_info *child_die = die->child;
6e70227d 17042
d9fa45fe
DC
17043 while (child_die && child_die->tag)
17044 {
e7c27a73 17045 process_die (child_die, cu);
d9fa45fe
DC
17046 child_die = sibling_die (child_die);
17047 }
17048 }
38d518c9
EZ
17049}
17050
f55ee35c
JK
17051/* Read a Fortran module as type. This DIE can be only a declaration used for
17052 imported module. Still we need that type as local Fortran "use ... only"
17053 declaration imports depend on the created type in determine_prefix. */
17054
17055static struct type *
17056read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17057{
518817b3 17058 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 17059 const char *module_name;
f55ee35c
JK
17060 struct type *type;
17061
17062 module_name = dwarf2_name (die, cu);
19f392bc 17063 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17064
f55ee35c
JK
17065 return set_die_type (die, type, cu);
17066}
17067
5d7cb8df
JK
17068/* Read a Fortran module. */
17069
17070static void
17071read_module (struct die_info *die, struct dwarf2_cu *cu)
17072{
17073 struct die_info *child_die = die->child;
530e8392
KB
17074 struct type *type;
17075
17076 type = read_type_die (die, cu);
17077 new_symbol (die, type, cu);
5d7cb8df 17078
5d7cb8df
JK
17079 while (child_die && child_die->tag)
17080 {
17081 process_die (child_die, cu);
17082 child_die = sibling_die (child_die);
17083 }
17084}
17085
38d518c9
EZ
17086/* Return the name of the namespace represented by DIE. Set
17087 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17088 namespace. */
17089
17090static const char *
e142c38c 17091namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17092{
17093 struct die_info *current_die;
17094 const char *name = NULL;
17095
17096 /* Loop through the extensions until we find a name. */
17097
17098 for (current_die = die;
17099 current_die != NULL;
f2f0e013 17100 current_die = dwarf2_extension (die, &cu))
38d518c9 17101 {
96553a0c
DE
17102 /* We don't use dwarf2_name here so that we can detect the absence
17103 of a name -> anonymous namespace. */
7d45c7c3 17104 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17105
38d518c9
EZ
17106 if (name != NULL)
17107 break;
17108 }
17109
17110 /* Is it an anonymous namespace? */
17111
17112 *is_anonymous = (name == NULL);
17113 if (*is_anonymous)
2b1dbab0 17114 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17115
17116 return name;
d9fa45fe
DC
17117}
17118
c906108c
SS
17119/* Extract all information from a DW_TAG_pointer_type DIE and add to
17120 the user defined type vector. */
17121
f792889a 17122static struct type *
e7c27a73 17123read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17124{
518817b3
SM
17125 struct gdbarch *gdbarch
17126 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17127 struct comp_unit_head *cu_header = &cu->header;
c906108c 17128 struct type *type;
8b2dbe47
KB
17129 struct attribute *attr_byte_size;
17130 struct attribute *attr_address_class;
17131 int byte_size, addr_class;
7e314c57
JK
17132 struct type *target_type;
17133
17134 target_type = die_type (die, cu);
c906108c 17135
7e314c57
JK
17136 /* The die_type call above may have already set the type for this DIE. */
17137 type = get_die_type (die, cu);
17138 if (type)
17139 return type;
17140
17141 type = lookup_pointer_type (target_type);
8b2dbe47 17142
e142c38c 17143 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17144 if (attr_byte_size)
17145 byte_size = DW_UNSND (attr_byte_size);
c906108c 17146 else
8b2dbe47
KB
17147 byte_size = cu_header->addr_size;
17148
e142c38c 17149 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17150 if (attr_address_class)
17151 addr_class = DW_UNSND (attr_address_class);
17152 else
17153 addr_class = DW_ADDR_none;
17154
2b4424c3
TT
17155 ULONGEST alignment = get_alignment (cu, die);
17156
17157 /* If the pointer size, alignment, or address class is different
17158 than the default, create a type variant marked as such and set
17159 the length accordingly. */
17160 if (TYPE_LENGTH (type) != byte_size
17161 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17162 && alignment != TYPE_RAW_ALIGN (type))
17163 || addr_class != DW_ADDR_none)
c906108c 17164 {
5e2b427d 17165 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17166 {
17167 int type_flags;
17168
849957d9 17169 type_flags = gdbarch_address_class_type_flags
5e2b427d 17170 (gdbarch, byte_size, addr_class);
876cecd0
TT
17171 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17172 == 0);
8b2dbe47
KB
17173 type = make_type_with_address_space (type, type_flags);
17174 }
17175 else if (TYPE_LENGTH (type) != byte_size)
17176 {
b98664d3 17177 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17178 }
2b4424c3
TT
17179 else if (TYPE_RAW_ALIGN (type) != alignment)
17180 {
b98664d3 17181 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17182 " - DIE at %s [in module %s]"),
17183 sect_offset_str (die->sect_off),
17184 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17185 }
6e70227d 17186 else
9a619af0
MS
17187 {
17188 /* Should we also complain about unhandled address classes? */
17189 }
c906108c 17190 }
8b2dbe47
KB
17191
17192 TYPE_LENGTH (type) = byte_size;
2b4424c3 17193 set_type_align (type, alignment);
f792889a 17194 return set_die_type (die, type, cu);
c906108c
SS
17195}
17196
17197/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17198 the user defined type vector. */
17199
f792889a 17200static struct type *
e7c27a73 17201read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17202{
17203 struct type *type;
17204 struct type *to_type;
17205 struct type *domain;
17206
e7c27a73
DJ
17207 to_type = die_type (die, cu);
17208 domain = die_containing_type (die, cu);
0d5de010 17209
7e314c57
JK
17210 /* The calls above may have already set the type for this DIE. */
17211 type = get_die_type (die, cu);
17212 if (type)
17213 return type;
17214
0d5de010
DJ
17215 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17216 type = lookup_methodptr_type (to_type);
7078baeb
TT
17217 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17218 {
518817b3
SM
17219 struct type *new_type
17220 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17221
17222 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17223 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17224 TYPE_VARARGS (to_type));
17225 type = lookup_methodptr_type (new_type);
17226 }
0d5de010
DJ
17227 else
17228 type = lookup_memberptr_type (to_type, domain);
c906108c 17229
f792889a 17230 return set_die_type (die, type, cu);
c906108c
SS
17231}
17232
4297a3f0 17233/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17234 the user defined type vector. */
17235
f792889a 17236static struct type *
4297a3f0
AV
17237read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17238 enum type_code refcode)
c906108c 17239{
e7c27a73 17240 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17241 struct type *type, *target_type;
c906108c
SS
17242 struct attribute *attr;
17243
4297a3f0
AV
17244 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17245
7e314c57
JK
17246 target_type = die_type (die, cu);
17247
17248 /* The die_type call above may have already set the type for this DIE. */
17249 type = get_die_type (die, cu);
17250 if (type)
17251 return type;
17252
4297a3f0 17253 type = lookup_reference_type (target_type, refcode);
e142c38c 17254 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17255 if (attr != nullptr)
c906108c
SS
17256 {
17257 TYPE_LENGTH (type) = DW_UNSND (attr);
17258 }
17259 else
17260 {
107d2387 17261 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17262 }
2b4424c3 17263 maybe_set_alignment (cu, die, type);
f792889a 17264 return set_die_type (die, type, cu);
c906108c
SS
17265}
17266
cf363f18
MW
17267/* Add the given cv-qualifiers to the element type of the array. GCC
17268 outputs DWARF type qualifiers that apply to an array, not the
17269 element type. But GDB relies on the array element type to carry
17270 the cv-qualifiers. This mimics section 6.7.3 of the C99
17271 specification. */
17272
17273static struct type *
17274add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17275 struct type *base_type, int cnst, int voltl)
17276{
17277 struct type *el_type, *inner_array;
17278
17279 base_type = copy_type (base_type);
17280 inner_array = base_type;
17281
17282 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17283 {
17284 TYPE_TARGET_TYPE (inner_array) =
17285 copy_type (TYPE_TARGET_TYPE (inner_array));
17286 inner_array = TYPE_TARGET_TYPE (inner_array);
17287 }
17288
17289 el_type = TYPE_TARGET_TYPE (inner_array);
17290 cnst |= TYPE_CONST (el_type);
17291 voltl |= TYPE_VOLATILE (el_type);
17292 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17293
17294 return set_die_type (die, base_type, cu);
17295}
17296
f792889a 17297static struct type *
e7c27a73 17298read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17299{
f792889a 17300 struct type *base_type, *cv_type;
c906108c 17301
e7c27a73 17302 base_type = die_type (die, cu);
7e314c57
JK
17303
17304 /* The die_type call above may have already set the type for this DIE. */
17305 cv_type = get_die_type (die, cu);
17306 if (cv_type)
17307 return cv_type;
17308
2f608a3a
KW
17309 /* In case the const qualifier is applied to an array type, the element type
17310 is so qualified, not the array type (section 6.7.3 of C99). */
17311 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17312 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17313
f792889a
DJ
17314 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17315 return set_die_type (die, cv_type, cu);
c906108c
SS
17316}
17317
f792889a 17318static struct type *
e7c27a73 17319read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17320{
f792889a 17321 struct type *base_type, *cv_type;
c906108c 17322
e7c27a73 17323 base_type = die_type (die, cu);
7e314c57
JK
17324
17325 /* The die_type call above may have already set the type for this DIE. */
17326 cv_type = get_die_type (die, cu);
17327 if (cv_type)
17328 return cv_type;
17329
cf363f18
MW
17330 /* In case the volatile qualifier is applied to an array type, the
17331 element type is so qualified, not the array type (section 6.7.3
17332 of C99). */
17333 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17334 return add_array_cv_type (die, cu, base_type, 0, 1);
17335
f792889a
DJ
17336 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17337 return set_die_type (die, cv_type, cu);
c906108c
SS
17338}
17339
06d66ee9
TT
17340/* Handle DW_TAG_restrict_type. */
17341
17342static struct type *
17343read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17344{
17345 struct type *base_type, *cv_type;
17346
17347 base_type = die_type (die, cu);
17348
17349 /* The die_type call above may have already set the type for this DIE. */
17350 cv_type = get_die_type (die, cu);
17351 if (cv_type)
17352 return cv_type;
17353
17354 cv_type = make_restrict_type (base_type);
17355 return set_die_type (die, cv_type, cu);
17356}
17357
a2c2acaf
MW
17358/* Handle DW_TAG_atomic_type. */
17359
17360static struct type *
17361read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17362{
17363 struct type *base_type, *cv_type;
17364
17365 base_type = die_type (die, cu);
17366
17367 /* The die_type call above may have already set the type for this DIE. */
17368 cv_type = get_die_type (die, cu);
17369 if (cv_type)
17370 return cv_type;
17371
17372 cv_type = make_atomic_type (base_type);
17373 return set_die_type (die, cv_type, cu);
17374}
17375
c906108c
SS
17376/* Extract all information from a DW_TAG_string_type DIE and add to
17377 the user defined type vector. It isn't really a user defined type,
17378 but it behaves like one, with other DIE's using an AT_user_def_type
17379 attribute to reference it. */
17380
f792889a 17381static struct type *
e7c27a73 17382read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17383{
518817b3 17384 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17385 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17386 struct type *type, *range_type, *index_type, *char_type;
17387 struct attribute *attr;
216a7e6b
AB
17388 struct dynamic_prop prop;
17389 bool length_is_constant = true;
17390 LONGEST length;
17391
17392 /* There are a couple of places where bit sizes might be made use of
17393 when parsing a DW_TAG_string_type, however, no producer that we know
17394 of make use of these. Handling bit sizes that are a multiple of the
17395 byte size is easy enough, but what about other bit sizes? Lets deal
17396 with that problem when we have to. Warn about these attributes being
17397 unsupported, then parse the type and ignore them like we always
17398 have. */
17399 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17400 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17401 {
17402 static bool warning_printed = false;
17403 if (!warning_printed)
17404 {
17405 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17406 "currently supported on DW_TAG_string_type."));
17407 warning_printed = true;
17408 }
17409 }
c906108c 17410
e142c38c 17411 attr = dwarf2_attr (die, DW_AT_string_length, cu);
216a7e6b
AB
17412 if (attr != nullptr && !attr_form_is_constant (attr))
17413 {
17414 /* The string length describes the location at which the length of
17415 the string can be found. The size of the length field can be
17416 specified with one of the attributes below. */
17417 struct type *prop_type;
17418 struct attribute *len
17419 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17420 if (len == nullptr)
17421 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17422 if (len != nullptr && attr_form_is_constant (len))
17423 {
17424 /* Pass 0 as the default as we know this attribute is constant
17425 and the default value will not be returned. */
17426 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17427 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17428 }
17429 else
17430 {
17431 /* If the size is not specified then we assume it is the size of
17432 an address on this target. */
17433 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17434 }
17435
17436 /* Convert the attribute into a dynamic property. */
17437 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17438 length = 1;
17439 else
17440 length_is_constant = false;
17441 }
17442 else if (attr != nullptr)
17443 {
17444 /* This DW_AT_string_length just contains the length with no
17445 indirection. There's no need to create a dynamic property in this
17446 case. Pass 0 for the default value as we know it will not be
17447 returned in this case. */
17448 length = dwarf2_get_attr_constant_value (attr, 0);
17449 }
17450 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17451 {
216a7e6b
AB
17452 /* We don't currently support non-constant byte sizes for strings. */
17453 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17454 }
17455 else
17456 {
216a7e6b
AB
17457 /* Use 1 as a fallback length if we have nothing else. */
17458 length = 1;
c906108c 17459 }
6ccb9162 17460
46bf5051 17461 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17462 if (length_is_constant)
17463 range_type = create_static_range_type (NULL, index_type, 1, length);
17464 else
17465 {
17466 struct dynamic_prop low_bound;
17467
17468 low_bound.kind = PROP_CONST;
17469 low_bound.data.const_val = 1;
17470 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17471 }
3b7538c0
UW
17472 char_type = language_string_char_type (cu->language_defn, gdbarch);
17473 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17474
f792889a 17475 return set_die_type (die, type, cu);
c906108c
SS
17476}
17477
4d804846
JB
17478/* Assuming that DIE corresponds to a function, returns nonzero
17479 if the function is prototyped. */
17480
17481static int
17482prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17483{
17484 struct attribute *attr;
17485
17486 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17487 if (attr && (DW_UNSND (attr) != 0))
17488 return 1;
17489
17490 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17491 is only meaningful for C, but the concept also extends to other
4d804846
JB
17492 languages that allow unprototyped functions (Eg: Objective C).
17493 For all other languages, assume that functions are always
17494 prototyped. */
17495 if (cu->language != language_c
17496 && cu->language != language_objc
17497 && cu->language != language_opencl)
17498 return 1;
17499
17500 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17501 prototyped and unprototyped functions; default to prototyped,
17502 since that is more common in modern code (and RealView warns
17503 about unprototyped functions). */
17504 if (producer_is_realview (cu->producer))
17505 return 1;
17506
17507 return 0;
17508}
17509
c906108c
SS
17510/* Handle DIES due to C code like:
17511
17512 struct foo
c5aa993b
JM
17513 {
17514 int (*funcp)(int a, long l);
17515 int b;
17516 };
c906108c 17517
0963b4bd 17518 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17519
f792889a 17520static struct type *
e7c27a73 17521read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17522{
518817b3 17523 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17524 struct type *type; /* Type that this function returns. */
17525 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17526 struct attribute *attr;
17527
e7c27a73 17528 type = die_type (die, cu);
7e314c57
JK
17529
17530 /* The die_type call above may have already set the type for this DIE. */
17531 ftype = get_die_type (die, cu);
17532 if (ftype)
17533 return ftype;
17534
0c8b41f1 17535 ftype = lookup_function_type (type);
c906108c 17536
4d804846 17537 if (prototyped_function_p (die, cu))
a6c727b2 17538 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17539
c055b101
CV
17540 /* Store the calling convention in the type if it's available in
17541 the subroutine die. Otherwise set the calling convention to
17542 the default value DW_CC_normal. */
17543 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17544 if (attr != nullptr
17545 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17546 TYPE_CALLING_CONVENTION (ftype)
17547 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17548 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17549 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17550 else
17551 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17552
743649fd
MW
17553 /* Record whether the function returns normally to its caller or not
17554 if the DWARF producer set that information. */
17555 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17556 if (attr && (DW_UNSND (attr) != 0))
17557 TYPE_NO_RETURN (ftype) = 1;
17558
76c10ea2
GM
17559 /* We need to add the subroutine type to the die immediately so
17560 we don't infinitely recurse when dealing with parameters
0963b4bd 17561 declared as the same subroutine type. */
76c10ea2 17562 set_die_type (die, ftype, cu);
6e70227d 17563
639d11d3 17564 if (die->child != NULL)
c906108c 17565 {
bb5ed363 17566 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17567 struct die_info *child_die;
8072405b 17568 int nparams, iparams;
c906108c
SS
17569
17570 /* Count the number of parameters.
17571 FIXME: GDB currently ignores vararg functions, but knows about
17572 vararg member functions. */
8072405b 17573 nparams = 0;
639d11d3 17574 child_die = die->child;
c906108c
SS
17575 while (child_die && child_die->tag)
17576 {
17577 if (child_die->tag == DW_TAG_formal_parameter)
17578 nparams++;
17579 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17580 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17581 child_die = sibling_die (child_die);
17582 }
17583
17584 /* Allocate storage for parameters and fill them in. */
17585 TYPE_NFIELDS (ftype) = nparams;
17586 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17587 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17588
8072405b
JK
17589 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17590 even if we error out during the parameters reading below. */
17591 for (iparams = 0; iparams < nparams; iparams++)
17592 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17593
17594 iparams = 0;
639d11d3 17595 child_die = die->child;
c906108c
SS
17596 while (child_die && child_die->tag)
17597 {
17598 if (child_die->tag == DW_TAG_formal_parameter)
17599 {
3ce3b1ba
PA
17600 struct type *arg_type;
17601
17602 /* DWARF version 2 has no clean way to discern C++
17603 static and non-static member functions. G++ helps
17604 GDB by marking the first parameter for non-static
17605 member functions (which is the this pointer) as
17606 artificial. We pass this information to
17607 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17608
17609 DWARF version 3 added DW_AT_object_pointer, which GCC
17610 4.5 does not yet generate. */
e142c38c 17611 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17612 if (attr != nullptr)
c906108c
SS
17613 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17614 else
9c37b5ae 17615 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17616 arg_type = die_type (child_die, cu);
17617
17618 /* RealView does not mark THIS as const, which the testsuite
17619 expects. GCC marks THIS as const in method definitions,
17620 but not in the class specifications (GCC PR 43053). */
17621 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17622 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17623 {
17624 int is_this = 0;
17625 struct dwarf2_cu *arg_cu = cu;
17626 const char *name = dwarf2_name (child_die, cu);
17627
17628 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17629 if (attr != nullptr)
3ce3b1ba
PA
17630 {
17631 /* If the compiler emits this, use it. */
17632 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17633 is_this = 1;
17634 }
17635 else if (name && strcmp (name, "this") == 0)
17636 /* Function definitions will have the argument names. */
17637 is_this = 1;
17638 else if (name == NULL && iparams == 0)
17639 /* Declarations may not have the names, so like
17640 elsewhere in GDB, assume an artificial first
17641 argument is "this". */
17642 is_this = 1;
17643
17644 if (is_this)
17645 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17646 arg_type, 0);
17647 }
17648
17649 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17650 iparams++;
17651 }
17652 child_die = sibling_die (child_die);
17653 }
17654 }
17655
76c10ea2 17656 return ftype;
c906108c
SS
17657}
17658
f792889a 17659static struct type *
e7c27a73 17660read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17661{
518817b3 17662 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17663 const char *name = NULL;
3c8e0968 17664 struct type *this_type, *target_type;
c906108c 17665
94af9270 17666 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17667 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17668 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17669 set_die_type (die, this_type, cu);
3c8e0968
DE
17670 target_type = die_type (die, cu);
17671 if (target_type != this_type)
17672 TYPE_TARGET_TYPE (this_type) = target_type;
17673 else
17674 {
17675 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17676 spec and cause infinite loops in GDB. */
b98664d3 17677 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17678 "- DIE at %s [in module %s]"),
17679 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17680 TYPE_TARGET_TYPE (this_type) = NULL;
17681 }
f792889a 17682 return this_type;
c906108c
SS
17683}
17684
9b790ce7
UW
17685/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17686 (which may be different from NAME) to the architecture back-end to allow
17687 it to guess the correct format if necessary. */
17688
17689static struct type *
17690dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17691 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17692{
17693 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17694 const struct floatformat **format;
17695 struct type *type;
17696
17697 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17698 if (format)
103a685e 17699 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17700 else
77b7c781 17701 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17702
17703 return type;
17704}
17705
eb77c9df
AB
17706/* Allocate an integer type of size BITS and name NAME. */
17707
17708static struct type *
17709dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17710 int bits, int unsigned_p, const char *name)
17711{
17712 struct type *type;
17713
17714 /* Versions of Intel's C Compiler generate an integer type called "void"
17715 instead of using DW_TAG_unspecified_type. This has been seen on
17716 at least versions 14, 17, and 18. */
35ee2dc2
AB
17717 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17718 && strcmp (name, "void") == 0)
eb77c9df
AB
17719 type = objfile_type (objfile)->builtin_void;
17720 else
17721 type = init_integer_type (objfile, bits, unsigned_p, name);
17722
17723 return type;
17724}
17725
8bdc1658
AB
17726/* Initialise and return a floating point type of size BITS suitable for
17727 use as a component of a complex number. The NAME_HINT is passed through
17728 when initialising the floating point type and is the name of the complex
17729 type.
17730
17731 As DWARF doesn't currently provide an explicit name for the components
17732 of a complex number, but it can be helpful to have these components
17733 named, we try to select a suitable name based on the size of the
17734 component. */
17735static struct type *
17736dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17737 struct objfile *objfile,
103a685e
TT
17738 int bits, const char *name_hint,
17739 enum bfd_endian byte_order)
8bdc1658
AB
17740{
17741 gdbarch *gdbarch = get_objfile_arch (objfile);
17742 struct type *tt = nullptr;
17743
35add35e
AB
17744 /* Try to find a suitable floating point builtin type of size BITS.
17745 We're going to use the name of this type as the name for the complex
17746 target type that we are about to create. */
1db455a7 17747 switch (cu->language)
8bdc1658 17748 {
1db455a7
AB
17749 case language_fortran:
17750 switch (bits)
17751 {
17752 case 32:
17753 tt = builtin_f_type (gdbarch)->builtin_real;
17754 break;
17755 case 64:
17756 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17757 break;
17758 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17759 case 128:
17760 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17761 break;
17762 }
8bdc1658 17763 break;
1db455a7
AB
17764 default:
17765 switch (bits)
17766 {
17767 case 32:
17768 tt = builtin_type (gdbarch)->builtin_float;
17769 break;
17770 case 64:
17771 tt = builtin_type (gdbarch)->builtin_double;
17772 break;
17773 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17774 case 128:
17775 tt = builtin_type (gdbarch)->builtin_long_double;
17776 break;
17777 }
8bdc1658
AB
17778 break;
17779 }
17780
35add35e
AB
17781 /* If the type we found doesn't match the size we were looking for, then
17782 pretend we didn't find a type at all, the complex target type we
17783 create will then be nameless. */
a12e5744 17784 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17785 tt = nullptr;
17786
8bdc1658 17787 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17788 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17789}
17790
c906108c
SS
17791/* Find a representation of a given base type and install
17792 it in the TYPE field of the die. */
17793
f792889a 17794static struct type *
e7c27a73 17795read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17796{
518817b3 17797 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17798 struct type *type;
17799 struct attribute *attr;
19f392bc 17800 int encoding = 0, bits = 0;
15d034d0 17801 const char *name;
34877895 17802 gdbarch *arch;
c906108c 17803
e142c38c 17804 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17805 if (attr != nullptr)
34877895 17806 encoding = DW_UNSND (attr);
e142c38c 17807 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17808 if (attr != nullptr)
34877895 17809 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17810 name = dwarf2_name (die, cu);
6ccb9162 17811 if (!name)
34877895 17812 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17813
17814 arch = get_objfile_arch (objfile);
17815 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17816
34877895
PJ
17817 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17818 if (attr)
103a685e
TT
17819 {
17820 int endianity = DW_UNSND (attr);
17821
17822 switch (endianity)
17823 {
17824 case DW_END_big:
17825 byte_order = BFD_ENDIAN_BIG;
17826 break;
17827 case DW_END_little:
17828 byte_order = BFD_ENDIAN_LITTLE;
17829 break;
17830 default:
17831 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17832 break;
17833 }
17834 }
6ccb9162
UW
17835
17836 switch (encoding)
c906108c 17837 {
6ccb9162
UW
17838 case DW_ATE_address:
17839 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17840 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17841 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17842 break;
17843 case DW_ATE_boolean:
19f392bc 17844 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17845 break;
17846 case DW_ATE_complex_float:
103a685e
TT
17847 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17848 byte_order);
19f392bc 17849 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17850 break;
17851 case DW_ATE_decimal_float:
19f392bc 17852 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17853 break;
17854 case DW_ATE_float:
103a685e 17855 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17856 break;
17857 case DW_ATE_signed:
eb77c9df 17858 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17859 break;
17860 case DW_ATE_unsigned:
3b2b8fea
TT
17861 if (cu->language == language_fortran
17862 && name
61012eef 17863 && startswith (name, "character("))
19f392bc
UW
17864 type = init_character_type (objfile, bits, 1, name);
17865 else
eb77c9df 17866 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17867 break;
17868 case DW_ATE_signed_char:
6e70227d 17869 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17870 || cu->language == language_pascal
17871 || cu->language == language_fortran)
19f392bc
UW
17872 type = init_character_type (objfile, bits, 0, name);
17873 else
eb77c9df 17874 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17875 break;
17876 case DW_ATE_unsigned_char:
868a0084 17877 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17878 || cu->language == language_pascal
c44af4eb
TT
17879 || cu->language == language_fortran
17880 || cu->language == language_rust)
19f392bc
UW
17881 type = init_character_type (objfile, bits, 1, name);
17882 else
eb77c9df 17883 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17884 break;
75079b2b 17885 case DW_ATE_UTF:
53e710ac 17886 {
53e710ac
PA
17887 if (bits == 16)
17888 type = builtin_type (arch)->builtin_char16;
17889 else if (bits == 32)
17890 type = builtin_type (arch)->builtin_char32;
17891 else
17892 {
b98664d3 17893 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17894 bits);
eb77c9df 17895 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17896 }
17897 return set_die_type (die, type, cu);
17898 }
75079b2b
TT
17899 break;
17900
6ccb9162 17901 default:
b98664d3 17902 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17903 dwarf_type_encoding_name (encoding));
77b7c781 17904 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17905 break;
c906108c 17906 }
6ccb9162 17907
0114d602 17908 if (name && strcmp (name, "char") == 0)
876cecd0 17909 TYPE_NOSIGN (type) = 1;
0114d602 17910
2b4424c3
TT
17911 maybe_set_alignment (cu, die, type);
17912
103a685e 17913 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17914
f792889a 17915 return set_die_type (die, type, cu);
c906108c
SS
17916}
17917
80180f79
SA
17918/* Parse dwarf attribute if it's a block, reference or constant and put the
17919 resulting value of the attribute into struct bound_prop.
17920 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17921
17922static int
17923attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17924 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17925 struct type *default_type)
80180f79
SA
17926{
17927 struct dwarf2_property_baton *baton;
518817b3
SM
17928 struct obstack *obstack
17929 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17930
9a49df9d
AB
17931 gdb_assert (default_type != NULL);
17932
80180f79
SA
17933 if (attr == NULL || prop == NULL)
17934 return 0;
17935
17936 if (attr_form_is_block (attr))
17937 {
8d749320 17938 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17939 baton->property_type = default_type;
80180f79
SA
17940 baton->locexpr.per_cu = cu->per_cu;
17941 baton->locexpr.size = DW_BLOCK (attr)->size;
17942 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17943 switch (attr->name)
17944 {
17945 case DW_AT_string_length:
17946 baton->locexpr.is_reference = true;
17947 break;
17948 default:
17949 baton->locexpr.is_reference = false;
17950 break;
17951 }
80180f79
SA
17952 prop->data.baton = baton;
17953 prop->kind = PROP_LOCEXPR;
17954 gdb_assert (prop->data.baton != NULL);
17955 }
17956 else if (attr_form_is_ref (attr))
17957 {
17958 struct dwarf2_cu *target_cu = cu;
17959 struct die_info *target_die;
17960 struct attribute *target_attr;
17961
17962 target_die = follow_die_ref (die, attr, &target_cu);
17963 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17964 if (target_attr == NULL)
17965 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17966 target_cu);
80180f79
SA
17967 if (target_attr == NULL)
17968 return 0;
17969
df25ebbd 17970 switch (target_attr->name)
80180f79 17971 {
df25ebbd
JB
17972 case DW_AT_location:
17973 if (attr_form_is_section_offset (target_attr))
17974 {
8d749320 17975 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17976 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17977 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17978 prop->data.baton = baton;
17979 prop->kind = PROP_LOCLIST;
17980 gdb_assert (prop->data.baton != NULL);
17981 }
17982 else if (attr_form_is_block (target_attr))
17983 {
8d749320 17984 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17985 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17986 baton->locexpr.per_cu = cu->per_cu;
17987 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17988 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17989 baton->locexpr.is_reference = true;
df25ebbd
JB
17990 prop->data.baton = baton;
17991 prop->kind = PROP_LOCEXPR;
17992 gdb_assert (prop->data.baton != NULL);
17993 }
17994 else
17995 {
17996 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17997 "dynamic property");
17998 return 0;
17999 }
18000 break;
18001 case DW_AT_data_member_location:
18002 {
18003 LONGEST offset;
18004
18005 if (!handle_data_member_location (target_die, target_cu,
18006 &offset))
18007 return 0;
18008
8d749320 18009 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18010 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18011 target_cu);
df25ebbd
JB
18012 baton->offset_info.offset = offset;
18013 baton->offset_info.type = die_type (target_die, target_cu);
18014 prop->data.baton = baton;
18015 prop->kind = PROP_ADDR_OFFSET;
18016 break;
18017 }
80180f79
SA
18018 }
18019 }
18020 else if (attr_form_is_constant (attr))
18021 {
18022 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18023 prop->kind = PROP_CONST;
18024 }
18025 else
18026 {
18027 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18028 dwarf2_name (die, cu));
18029 return 0;
18030 }
18031
18032 return 1;
18033}
18034
11a8b164
AB
18035/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18036 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
18037
18038static struct type *
11a8b164
AB
18039dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18040 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
18041{
18042 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
18043 struct type *int_type;
18044
18045 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18046#define TRY_TYPE(F) \
18047 int_type = (unsigned_p \
18048 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18049 : objfile_type (objfile)->builtin_ ## F); \
18050 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18051 return int_type
18052
18053 TRY_TYPE (char);
18054 TRY_TYPE (short);
18055 TRY_TYPE (int);
18056 TRY_TYPE (long);
18057 TRY_TYPE (long_long);
18058
18059#undef TRY_TYPE
18060
18061 gdb_assert_not_reached ("unable to find suitable integer type");
18062}
18063
11a8b164
AB
18064/* Find an integer type the same size as the address size given in the
18065 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18066 is unsigned or not. */
18067
18068static struct type *
18069dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18070 bool unsigned_p)
18071{
18072 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18073 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18074}
18075
b86352cf
AB
18076/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18077 present (which is valid) then compute the default type based on the
18078 compilation units address size. */
18079
18080static struct type *
18081read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18082{
18083 struct type *index_type = die_type (die, cu);
18084
18085 /* Dwarf-2 specifications explicitly allows to create subrange types
18086 without specifying a base type.
18087 In that case, the base type must be set to the type of
18088 the lower bound, upper bound or count, in that order, if any of these
18089 three attributes references an object that has a type.
18090 If no base type is found, the Dwarf-2 specifications say that
18091 a signed integer type of size equal to the size of an address should
18092 be used.
18093 For the following C code: `extern char gdb_int [];'
18094 GCC produces an empty range DIE.
18095 FIXME: muller/2010-05-28: Possible references to object for low bound,
18096 high bound or count are not yet handled by this code. */
18097 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 18098 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
18099
18100 return index_type;
18101}
18102
a02abb62
JB
18103/* Read the given DW_AT_subrange DIE. */
18104
f792889a 18105static struct type *
a02abb62
JB
18106read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18107{
4c9ad8c2 18108 struct type *base_type, *orig_base_type;
a02abb62
JB
18109 struct type *range_type;
18110 struct attribute *attr;
729efb13 18111 struct dynamic_prop low, high;
4fae6e18 18112 int low_default_is_valid;
c451ebe5 18113 int high_bound_is_count = 0;
15d034d0 18114 const char *name;
d359392f 18115 ULONGEST negative_mask;
e77813c8 18116
b86352cf
AB
18117 orig_base_type = read_subrange_index_type (die, cu);
18118
4c9ad8c2
TT
18119 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18120 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18121 creating the range type, but we use the result of check_typedef
18122 when examining properties of the type. */
18123 base_type = check_typedef (orig_base_type);
a02abb62 18124
7e314c57
JK
18125 /* The die_type call above may have already set the type for this DIE. */
18126 range_type = get_die_type (die, cu);
18127 if (range_type)
18128 return range_type;
18129
729efb13
SA
18130 low.kind = PROP_CONST;
18131 high.kind = PROP_CONST;
18132 high.data.const_val = 0;
18133
4fae6e18
JK
18134 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18135 omitting DW_AT_lower_bound. */
18136 switch (cu->language)
6e70227d 18137 {
4fae6e18
JK
18138 case language_c:
18139 case language_cplus:
729efb13 18140 low.data.const_val = 0;
4fae6e18
JK
18141 low_default_is_valid = 1;
18142 break;
18143 case language_fortran:
729efb13 18144 low.data.const_val = 1;
4fae6e18
JK
18145 low_default_is_valid = 1;
18146 break;
18147 case language_d:
4fae6e18 18148 case language_objc:
c44af4eb 18149 case language_rust:
729efb13 18150 low.data.const_val = 0;
4fae6e18
JK
18151 low_default_is_valid = (cu->header.version >= 4);
18152 break;
18153 case language_ada:
18154 case language_m2:
18155 case language_pascal:
729efb13 18156 low.data.const_val = 1;
4fae6e18
JK
18157 low_default_is_valid = (cu->header.version >= 4);
18158 break;
18159 default:
729efb13 18160 low.data.const_val = 0;
4fae6e18
JK
18161 low_default_is_valid = 0;
18162 break;
a02abb62
JB
18163 }
18164
e142c38c 18165 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18166 if (attr != nullptr)
9a49df9d 18167 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18168 else if (!low_default_is_valid)
b98664d3 18169 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18170 "- DIE at %s [in module %s]"),
18171 sect_offset_str (die->sect_off),
518817b3 18172 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18173
506f5c41
TV
18174 struct attribute *attr_ub, *attr_count;
18175 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18176 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18177 {
506f5c41 18178 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18179 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18180 {
c451ebe5
SA
18181 /* If bounds are constant do the final calculation here. */
18182 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18183 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18184 else
18185 high_bound_is_count = 1;
c2ff108b 18186 }
506f5c41
TV
18187 else
18188 {
18189 if (attr_ub != NULL)
18190 complaint (_("Unresolved DW_AT_upper_bound "
18191 "- DIE at %s [in module %s]"),
18192 sect_offset_str (die->sect_off),
18193 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18194 if (attr_count != NULL)
18195 complaint (_("Unresolved DW_AT_count "
18196 "- DIE at %s [in module %s]"),
18197 sect_offset_str (die->sect_off),
18198 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18199 }
e77813c8 18200 }
a02abb62 18201
4e962e74
TT
18202 LONGEST bias = 0;
18203 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18204 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18205 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18206
dbb9c2b1
JB
18207 /* Normally, the DWARF producers are expected to use a signed
18208 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18209 But this is unfortunately not always the case, as witnessed
18210 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18211 is used instead. To work around that ambiguity, we treat
18212 the bounds as signed, and thus sign-extend their values, when
18213 the base type is signed. */
6e70227d 18214 negative_mask =
d359392f 18215 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18216 if (low.kind == PROP_CONST
18217 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18218 low.data.const_val |= negative_mask;
18219 if (high.kind == PROP_CONST
18220 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18221 high.data.const_val |= negative_mask;
43bbcdc2 18222
5bbd8269
AB
18223 /* Check for bit and byte strides. */
18224 struct dynamic_prop byte_stride_prop;
18225 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18226 if (attr_byte_stride != nullptr)
18227 {
18228 struct type *prop_type
18229 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18230 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18231 prop_type);
18232 }
18233
18234 struct dynamic_prop bit_stride_prop;
18235 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18236 if (attr_bit_stride != nullptr)
18237 {
18238 /* It only makes sense to have either a bit or byte stride. */
18239 if (attr_byte_stride != nullptr)
18240 {
18241 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18242 "- DIE at %s [in module %s]"),
18243 sect_offset_str (die->sect_off),
18244 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18245 attr_bit_stride = nullptr;
18246 }
18247 else
18248 {
18249 struct type *prop_type
18250 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18251 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18252 prop_type);
18253 }
18254 }
18255
18256 if (attr_byte_stride != nullptr
18257 || attr_bit_stride != nullptr)
18258 {
18259 bool byte_stride_p = (attr_byte_stride != nullptr);
18260 struct dynamic_prop *stride
18261 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18262
18263 range_type
18264 = create_range_type_with_stride (NULL, orig_base_type, &low,
18265 &high, bias, stride, byte_stride_p);
18266 }
18267 else
18268 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18269
c451ebe5
SA
18270 if (high_bound_is_count)
18271 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18272
c2ff108b
JK
18273 /* Ada expects an empty array on no boundary attributes. */
18274 if (attr == NULL && cu->language != language_ada)
729efb13 18275 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18276
39cbfefa
DJ
18277 name = dwarf2_name (die, cu);
18278 if (name)
18279 TYPE_NAME (range_type) = name;
6e70227d 18280
e142c38c 18281 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18282 if (attr != nullptr)
a02abb62
JB
18283 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18284
2b4424c3
TT
18285 maybe_set_alignment (cu, die, range_type);
18286
7e314c57
JK
18287 set_die_type (die, range_type, cu);
18288
18289 /* set_die_type should be already done. */
b4ba55a1
JB
18290 set_descriptive_type (range_type, die, cu);
18291
7e314c57 18292 return range_type;
a02abb62 18293}
6e70227d 18294
f792889a 18295static struct type *
81a17f79
JB
18296read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18297{
18298 struct type *type;
81a17f79 18299
518817b3
SM
18300 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18301 NULL);
0114d602 18302 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18303
74a2f8ff 18304 /* In Ada, an unspecified type is typically used when the description
85102364 18305 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18306 such a type, we treat it as a stub, and try to resolve it later on,
18307 when needed. */
18308 if (cu->language == language_ada)
18309 TYPE_STUB (type) = 1;
18310
f792889a 18311 return set_die_type (die, type, cu);
81a17f79 18312}
a02abb62 18313
639d11d3
DC
18314/* Read a single die and all its descendents. Set the die's sibling
18315 field to NULL; set other fields in the die correctly, and set all
18316 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18317 location of the info_ptr after reading all of those dies. PARENT
18318 is the parent of the die in question. */
18319
18320static struct die_info *
dee91e82 18321read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18322 const gdb_byte *info_ptr,
18323 const gdb_byte **new_info_ptr,
dee91e82 18324 struct die_info *parent)
639d11d3
DC
18325{
18326 struct die_info *die;
d521ce57 18327 const gdb_byte *cur_ptr;
639d11d3
DC
18328 int has_children;
18329
bf6af496 18330 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18331 if (die == NULL)
18332 {
18333 *new_info_ptr = cur_ptr;
18334 return NULL;
18335 }
93311388 18336 store_in_ref_table (die, reader->cu);
639d11d3
DC
18337
18338 if (has_children)
bf6af496 18339 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18340 else
18341 {
18342 die->child = NULL;
18343 *new_info_ptr = cur_ptr;
18344 }
18345
18346 die->sibling = NULL;
18347 die->parent = parent;
18348 return die;
18349}
18350
18351/* Read a die, all of its descendents, and all of its siblings; set
18352 all of the fields of all of the dies correctly. Arguments are as
18353 in read_die_and_children. */
18354
18355static struct die_info *
bf6af496 18356read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18357 const gdb_byte *info_ptr,
18358 const gdb_byte **new_info_ptr,
bf6af496 18359 struct die_info *parent)
639d11d3
DC
18360{
18361 struct die_info *first_die, *last_sibling;
d521ce57 18362 const gdb_byte *cur_ptr;
639d11d3 18363
c906108c 18364 cur_ptr = info_ptr;
639d11d3
DC
18365 first_die = last_sibling = NULL;
18366
18367 while (1)
c906108c 18368 {
639d11d3 18369 struct die_info *die
dee91e82 18370 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18371
1d325ec1 18372 if (die == NULL)
c906108c 18373 {
639d11d3
DC
18374 *new_info_ptr = cur_ptr;
18375 return first_die;
c906108c 18376 }
1d325ec1
DJ
18377
18378 if (!first_die)
18379 first_die = die;
c906108c 18380 else
1d325ec1
DJ
18381 last_sibling->sibling = die;
18382
18383 last_sibling = die;
c906108c 18384 }
c906108c
SS
18385}
18386
bf6af496
DE
18387/* Read a die, all of its descendents, and all of its siblings; set
18388 all of the fields of all of the dies correctly. Arguments are as
18389 in read_die_and_children.
18390 This the main entry point for reading a DIE and all its children. */
18391
18392static struct die_info *
18393read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18394 const gdb_byte *info_ptr,
18395 const gdb_byte **new_info_ptr,
bf6af496
DE
18396 struct die_info *parent)
18397{
18398 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18399 new_info_ptr, parent);
18400
b4f54984 18401 if (dwarf_die_debug)
bf6af496
DE
18402 {
18403 fprintf_unfiltered (gdb_stdlog,
18404 "Read die from %s@0x%x of %s:\n",
a32a8923 18405 get_section_name (reader->die_section),
bf6af496
DE
18406 (unsigned) (info_ptr - reader->die_section->buffer),
18407 bfd_get_filename (reader->abfd));
b4f54984 18408 dump_die (die, dwarf_die_debug);
bf6af496
DE
18409 }
18410
18411 return die;
18412}
18413
3019eac3
DE
18414/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18415 attributes.
18416 The caller is responsible for filling in the extra attributes
18417 and updating (*DIEP)->num_attrs.
18418 Set DIEP to point to a newly allocated die with its information,
18419 except for its child, sibling, and parent fields.
18420 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18421
d521ce57 18422static const gdb_byte *
3019eac3 18423read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18424 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18425 int *has_children, int num_extra_attrs)
93311388 18426{
b64f50a1 18427 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18428 struct abbrev_info *abbrev;
18429 struct die_info *die;
18430 struct dwarf2_cu *cu = reader->cu;
18431 bfd *abfd = reader->abfd;
18432
9c541725 18433 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18434 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18435 info_ptr += bytes_read;
18436 if (!abbrev_number)
18437 {
18438 *diep = NULL;
18439 *has_children = 0;
18440 return info_ptr;
18441 }
18442
685af9cd 18443 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18444 if (!abbrev)
348e048f
DE
18445 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18446 abbrev_number,
18447 bfd_get_filename (abfd));
18448
3019eac3 18449 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18450 die->sect_off = sect_off;
93311388
DE
18451 die->tag = abbrev->tag;
18452 die->abbrev = abbrev_number;
18453
3019eac3
DE
18454 /* Make the result usable.
18455 The caller needs to update num_attrs after adding the extra
18456 attributes. */
93311388
DE
18457 die->num_attrs = abbrev->num_attrs;
18458
18459 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18460 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18461 info_ptr);
93311388
DE
18462
18463 *diep = die;
18464 *has_children = abbrev->has_children;
18465 return info_ptr;
18466}
18467
3019eac3
DE
18468/* Read a die and all its attributes.
18469 Set DIEP to point to a newly allocated die with its information,
18470 except for its child, sibling, and parent fields.
18471 Set HAS_CHILDREN to tell whether the die has children or not. */
18472
d521ce57 18473static const gdb_byte *
3019eac3 18474read_full_die (const struct die_reader_specs *reader,
d521ce57 18475 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18476 int *has_children)
18477{
d521ce57 18478 const gdb_byte *result;
bf6af496
DE
18479
18480 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18481
b4f54984 18482 if (dwarf_die_debug)
bf6af496
DE
18483 {
18484 fprintf_unfiltered (gdb_stdlog,
18485 "Read die from %s@0x%x of %s:\n",
a32a8923 18486 get_section_name (reader->die_section),
bf6af496
DE
18487 (unsigned) (info_ptr - reader->die_section->buffer),
18488 bfd_get_filename (reader->abfd));
b4f54984 18489 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18490 }
18491
18492 return result;
3019eac3 18493}
433df2d4
DE
18494\f
18495/* Abbreviation tables.
3019eac3 18496
433df2d4 18497 In DWARF version 2, the description of the debugging information is
c906108c
SS
18498 stored in a separate .debug_abbrev section. Before we read any
18499 dies from a section we read in all abbreviations and install them
433df2d4
DE
18500 in a hash table. */
18501
18502/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18503
685af9cd
TT
18504struct abbrev_info *
18505abbrev_table::alloc_abbrev ()
433df2d4
DE
18506{
18507 struct abbrev_info *abbrev;
18508
685af9cd 18509 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18510 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18511
433df2d4
DE
18512 return abbrev;
18513}
18514
18515/* Add an abbreviation to the table. */
c906108c 18516
685af9cd
TT
18517void
18518abbrev_table::add_abbrev (unsigned int abbrev_number,
18519 struct abbrev_info *abbrev)
433df2d4
DE
18520{
18521 unsigned int hash_number;
18522
18523 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18524 abbrev->next = m_abbrevs[hash_number];
18525 m_abbrevs[hash_number] = abbrev;
433df2d4 18526}
dee91e82 18527
433df2d4
DE
18528/* Look up an abbrev in the table.
18529 Returns NULL if the abbrev is not found. */
18530
685af9cd
TT
18531struct abbrev_info *
18532abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18533{
433df2d4
DE
18534 unsigned int hash_number;
18535 struct abbrev_info *abbrev;
18536
18537 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18538 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18539
18540 while (abbrev)
18541 {
18542 if (abbrev->number == abbrev_number)
18543 return abbrev;
18544 abbrev = abbrev->next;
18545 }
18546 return NULL;
18547}
18548
18549/* Read in an abbrev table. */
18550
685af9cd 18551static abbrev_table_up
ed2dc618
SM
18552abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18553 struct dwarf2_section_info *section,
9c541725 18554 sect_offset sect_off)
433df2d4
DE
18555{
18556 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18557 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18558 const gdb_byte *abbrev_ptr;
c906108c
SS
18559 struct abbrev_info *cur_abbrev;
18560 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18561 unsigned int abbrev_form;
f3dd6933
DJ
18562 struct attr_abbrev *cur_attrs;
18563 unsigned int allocated_attrs;
c906108c 18564
685af9cd 18565 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18566
433df2d4 18567 dwarf2_read_section (objfile, section);
9c541725 18568 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18569 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18570 abbrev_ptr += bytes_read;
18571
f3dd6933 18572 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18573 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18574
0963b4bd 18575 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18576 while (abbrev_number)
18577 {
685af9cd 18578 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18579
18580 /* read in abbrev header */
18581 cur_abbrev->number = abbrev_number;
aead7601
SM
18582 cur_abbrev->tag
18583 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18584 abbrev_ptr += bytes_read;
18585 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18586 abbrev_ptr += 1;
18587
18588 /* now read in declarations */
22d2f3ab 18589 for (;;)
c906108c 18590 {
43988095
JK
18591 LONGEST implicit_const;
18592
22d2f3ab
JK
18593 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18594 abbrev_ptr += bytes_read;
18595 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18596 abbrev_ptr += bytes_read;
43988095
JK
18597 if (abbrev_form == DW_FORM_implicit_const)
18598 {
18599 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18600 &bytes_read);
18601 abbrev_ptr += bytes_read;
18602 }
18603 else
18604 {
18605 /* Initialize it due to a false compiler warning. */
18606 implicit_const = -1;
18607 }
22d2f3ab
JK
18608
18609 if (abbrev_name == 0)
18610 break;
18611
f3dd6933 18612 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18613 {
f3dd6933
DJ
18614 allocated_attrs += ATTR_ALLOC_CHUNK;
18615 cur_attrs
224c3ddb 18616 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18617 }
ae038cb0 18618
aead7601
SM
18619 cur_attrs[cur_abbrev->num_attrs].name
18620 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18621 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18622 = (enum dwarf_form) abbrev_form;
43988095 18623 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18624 ++cur_abbrev->num_attrs;
c906108c
SS
18625 }
18626
8d749320
SM
18627 cur_abbrev->attrs =
18628 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18629 cur_abbrev->num_attrs);
f3dd6933
DJ
18630 memcpy (cur_abbrev->attrs, cur_attrs,
18631 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18632
685af9cd 18633 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18634
18635 /* Get next abbreviation.
18636 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18637 always properly terminated with an abbrev number of 0.
18638 Exit loop if we encounter an abbreviation which we have
18639 already read (which means we are about to read the abbreviations
18640 for the next compile unit) or if the end of the abbreviation
18641 table is reached. */
433df2d4 18642 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18643 break;
18644 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18645 abbrev_ptr += bytes_read;
685af9cd 18646 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18647 break;
18648 }
f3dd6933
DJ
18649
18650 xfree (cur_attrs);
433df2d4 18651 return abbrev_table;
c906108c
SS
18652}
18653
72bf9492
DJ
18654/* Returns nonzero if TAG represents a type that we might generate a partial
18655 symbol for. */
18656
18657static int
18658is_type_tag_for_partial (int tag)
18659{
18660 switch (tag)
18661 {
18662#if 0
18663 /* Some types that would be reasonable to generate partial symbols for,
18664 that we don't at present. */
18665 case DW_TAG_array_type:
18666 case DW_TAG_file_type:
18667 case DW_TAG_ptr_to_member_type:
18668 case DW_TAG_set_type:
18669 case DW_TAG_string_type:
18670 case DW_TAG_subroutine_type:
18671#endif
18672 case DW_TAG_base_type:
18673 case DW_TAG_class_type:
680b30c7 18674 case DW_TAG_interface_type:
72bf9492
DJ
18675 case DW_TAG_enumeration_type:
18676 case DW_TAG_structure_type:
18677 case DW_TAG_subrange_type:
18678 case DW_TAG_typedef:
18679 case DW_TAG_union_type:
18680 return 1;
18681 default:
18682 return 0;
18683 }
18684}
18685
18686/* Load all DIEs that are interesting for partial symbols into memory. */
18687
18688static struct partial_die_info *
dee91e82 18689load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18690 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18691{
dee91e82 18692 struct dwarf2_cu *cu = reader->cu;
518817b3 18693 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18694 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18695 unsigned int bytes_read;
5afb4e99 18696 unsigned int load_all = 0;
72bf9492
DJ
18697 int nesting_level = 1;
18698
18699 parent_die = NULL;
18700 last_die = NULL;
18701
7adf1e79
DE
18702 gdb_assert (cu->per_cu != NULL);
18703 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18704 load_all = 1;
18705
72bf9492
DJ
18706 cu->partial_dies
18707 = htab_create_alloc_ex (cu->header.length / 12,
18708 partial_die_hash,
18709 partial_die_eq,
18710 NULL,
18711 &cu->comp_unit_obstack,
18712 hashtab_obstack_allocate,
18713 dummy_obstack_deallocate);
18714
72bf9492
DJ
18715 while (1)
18716 {
685af9cd 18717 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18718
18719 /* A NULL abbrev means the end of a series of children. */
18720 if (abbrev == NULL)
18721 {
18722 if (--nesting_level == 0)
cd9983dd
YQ
18723 return first_die;
18724
72bf9492
DJ
18725 info_ptr += bytes_read;
18726 last_die = parent_die;
18727 parent_die = parent_die->die_parent;
18728 continue;
18729 }
18730
98bfdba5
PA
18731 /* Check for template arguments. We never save these; if
18732 they're seen, we just mark the parent, and go on our way. */
18733 if (parent_die != NULL
18734 && cu->language == language_cplus
18735 && (abbrev->tag == DW_TAG_template_type_param
18736 || abbrev->tag == DW_TAG_template_value_param))
18737 {
18738 parent_die->has_template_arguments = 1;
18739
18740 if (!load_all)
18741 {
18742 /* We don't need a partial DIE for the template argument. */
dee91e82 18743 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18744 continue;
18745 }
18746 }
18747
0d99eb77 18748 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18749 Skip their other children. */
18750 if (!load_all
18751 && cu->language == language_cplus
18752 && parent_die != NULL
18753 && parent_die->tag == DW_TAG_subprogram)
18754 {
dee91e82 18755 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18756 continue;
18757 }
18758
5afb4e99
DJ
18759 /* Check whether this DIE is interesting enough to save. Normally
18760 we would not be interested in members here, but there may be
18761 later variables referencing them via DW_AT_specification (for
18762 static members). */
18763 if (!load_all
18764 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18765 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18766 && abbrev->tag != DW_TAG_enumerator
18767 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18768 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18769 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18770 && abbrev->tag != DW_TAG_variable
5afb4e99 18771 && abbrev->tag != DW_TAG_namespace
f55ee35c 18772 && abbrev->tag != DW_TAG_module
95554aad 18773 && abbrev->tag != DW_TAG_member
74921315
KS
18774 && abbrev->tag != DW_TAG_imported_unit
18775 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18776 {
18777 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18778 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18779 continue;
18780 }
18781
6f06d47b
YQ
18782 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18783 abbrev);
cd9983dd 18784
48fbe735 18785 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18786
18787 /* This two-pass algorithm for processing partial symbols has a
18788 high cost in cache pressure. Thus, handle some simple cases
18789 here which cover the majority of C partial symbols. DIEs
18790 which neither have specification tags in them, nor could have
18791 specification tags elsewhere pointing at them, can simply be
18792 processed and discarded.
18793
18794 This segment is also optional; scan_partial_symbols and
18795 add_partial_symbol will handle these DIEs if we chain
18796 them in normally. When compilers which do not emit large
18797 quantities of duplicate debug information are more common,
18798 this code can probably be removed. */
18799
18800 /* Any complete simple types at the top level (pretty much all
18801 of them, for a language without namespaces), can be processed
18802 directly. */
18803 if (parent_die == NULL
cd9983dd
YQ
18804 && pdi.has_specification == 0
18805 && pdi.is_declaration == 0
18806 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18807 || pdi.tag == DW_TAG_base_type
18808 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18809 {
cd9983dd 18810 if (building_psymtab && pdi.name != NULL)
31edb802 18811 add_psymbol_to_list (pdi.name, false,
79748972 18812 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18813 psymbol_placement::STATIC,
1762568f 18814 0, cu->language, objfile);
cd9983dd 18815 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18816 continue;
18817 }
18818
d8228535
JK
18819 /* The exception for DW_TAG_typedef with has_children above is
18820 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18821 type_name_or_error will error on such types later.
d8228535
JK
18822
18823 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18824 it could not find the child DIEs referenced later, this is checked
18825 above. In correct DWARF DW_TAG_typedef should have no children. */
18826
cd9983dd 18827 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18828 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18829 "- DIE at %s [in module %s]"),
cd9983dd 18830 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18831
72bf9492
DJ
18832 /* If we're at the second level, and we're an enumerator, and
18833 our parent has no specification (meaning possibly lives in a
18834 namespace elsewhere), then we can add the partial symbol now
18835 instead of queueing it. */
cd9983dd 18836 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18837 && parent_die != NULL
18838 && parent_die->die_parent == NULL
18839 && parent_die->tag == DW_TAG_enumeration_type
18840 && parent_die->has_specification == 0)
18841 {
cd9983dd 18842 if (pdi.name == NULL)
b98664d3 18843 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18844 else if (building_psymtab)
31edb802 18845 add_psymbol_to_list (pdi.name, false,
79748972 18846 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18847 cu->language == language_cplus
75aedd27
TT
18848 ? psymbol_placement::GLOBAL
18849 : psymbol_placement::STATIC,
1762568f 18850 0, cu->language, objfile);
72bf9492 18851
cd9983dd 18852 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18853 continue;
18854 }
18855
cd9983dd 18856 struct partial_die_info *part_die
6f06d47b 18857 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18858
72bf9492
DJ
18859 /* We'll save this DIE so link it in. */
18860 part_die->die_parent = parent_die;
18861 part_die->die_sibling = NULL;
18862 part_die->die_child = NULL;
18863
18864 if (last_die && last_die == parent_die)
18865 last_die->die_child = part_die;
18866 else if (last_die)
18867 last_die->die_sibling = part_die;
18868
18869 last_die = part_die;
18870
18871 if (first_die == NULL)
18872 first_die = part_die;
18873
18874 /* Maybe add the DIE to the hash table. Not all DIEs that we
18875 find interesting need to be in the hash table, because we
18876 also have the parent/sibling/child chains; only those that we
18877 might refer to by offset later during partial symbol reading.
18878
18879 For now this means things that might have be the target of a
18880 DW_AT_specification, DW_AT_abstract_origin, or
18881 DW_AT_extension. DW_AT_extension will refer only to
18882 namespaces; DW_AT_abstract_origin refers to functions (and
18883 many things under the function DIE, but we do not recurse
18884 into function DIEs during partial symbol reading) and
18885 possibly variables as well; DW_AT_specification refers to
18886 declarations. Declarations ought to have the DW_AT_declaration
18887 flag. It happens that GCC forgets to put it in sometimes, but
18888 only for functions, not for types.
18889
18890 Adding more things than necessary to the hash table is harmless
18891 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18892 wasted time in find_partial_die, when we reread the compilation
18893 unit with load_all_dies set. */
72bf9492 18894
5afb4e99 18895 if (load_all
72929c62 18896 || abbrev->tag == DW_TAG_constant
5afb4e99 18897 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18898 || abbrev->tag == DW_TAG_variable
18899 || abbrev->tag == DW_TAG_namespace
18900 || part_die->is_declaration)
18901 {
18902 void **slot;
18903
18904 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18905 to_underlying (part_die->sect_off),
18906 INSERT);
72bf9492
DJ
18907 *slot = part_die;
18908 }
18909
72bf9492 18910 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18911 we have no reason to follow the children of structures; for other
98bfdba5
PA
18912 languages we have to, so that we can get at method physnames
18913 to infer fully qualified class names, for DW_AT_specification,
18914 and for C++ template arguments. For C++, we also look one level
18915 inside functions to find template arguments (if the name of the
18916 function does not already contain the template arguments).
bc30ff58 18917
0a4b0913
AB
18918 For Ada and Fortran, we need to scan the children of subprograms
18919 and lexical blocks as well because these languages allow the
18920 definition of nested entities that could be interesting for the
18921 debugger, such as nested subprograms for instance. */
72bf9492 18922 if (last_die->has_children
5afb4e99
DJ
18923 && (load_all
18924 || last_die->tag == DW_TAG_namespace
f55ee35c 18925 || last_die->tag == DW_TAG_module
72bf9492 18926 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18927 || (cu->language == language_cplus
18928 && last_die->tag == DW_TAG_subprogram
18929 && (last_die->name == NULL
18930 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18931 || (cu->language != language_c
18932 && (last_die->tag == DW_TAG_class_type
680b30c7 18933 || last_die->tag == DW_TAG_interface_type
72bf9492 18934 || last_die->tag == DW_TAG_structure_type
bc30ff58 18935 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18936 || ((cu->language == language_ada
18937 || cu->language == language_fortran)
bc30ff58
JB
18938 && (last_die->tag == DW_TAG_subprogram
18939 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18940 {
18941 nesting_level++;
18942 parent_die = last_die;
18943 continue;
18944 }
18945
18946 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18947 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18948
18949 /* Back to the top, do it again. */
18950 }
18951}
18952
6f06d47b
YQ
18953partial_die_info::partial_die_info (sect_offset sect_off_,
18954 struct abbrev_info *abbrev)
18955 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18956{
18957}
18958
35cc7ed7
YQ
18959/* Read a minimal amount of information into the minimal die structure.
18960 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18961
48fbe735
YQ
18962const gdb_byte *
18963partial_die_info::read (const struct die_reader_specs *reader,
18964 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18965{
dee91e82 18966 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18967 struct dwarf2_per_objfile *dwarf2_per_objfile
18968 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18969 unsigned int i;
c5aa993b 18970 int has_low_pc_attr = 0;
c906108c 18971 int has_high_pc_attr = 0;
91da1414 18972 int high_pc_relative = 0;
c906108c 18973
fd0a254f 18974 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18975 {
48fbe735
YQ
18976 struct attribute attr;
18977
fd0a254f 18978 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18979
18980 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18981 partial symbol table. */
c906108c
SS
18982 switch (attr.name)
18983 {
18984 case DW_AT_name:
48fbe735 18985 switch (tag)
71c25dea
TT
18986 {
18987 case DW_TAG_compile_unit:
95554aad 18988 case DW_TAG_partial_unit:
348e048f 18989 case DW_TAG_type_unit:
71c25dea
TT
18990 /* Compilation units have a DW_AT_name that is a filename, not
18991 a source language identifier. */
18992 case DW_TAG_enumeration_type:
18993 case DW_TAG_enumerator:
18994 /* These tags always have simple identifiers already; no need
18995 to canonicalize them. */
48fbe735 18996 name = DW_STRING (&attr);
71c25dea
TT
18997 break;
18998 default:
48fbe735
YQ
18999 {
19000 struct objfile *objfile = dwarf2_per_objfile->objfile;
19001
19002 name
19003 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
19004 &objfile->per_bfd->storage_obstack);
19005 }
71c25dea
TT
19006 break;
19007 }
c906108c 19008 break;
31ef98ae 19009 case DW_AT_linkage_name:
c906108c 19010 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19011 /* Note that both forms of linkage name might appear. We
19012 assume they will be the same, and we only store the last
19013 one we see. */
48fbe735 19014 linkage_name = DW_STRING (&attr);
c906108c
SS
19015 break;
19016 case DW_AT_low_pc:
19017 has_low_pc_attr = 1;
48fbe735 19018 lowpc = attr_value_as_address (&attr);
c906108c
SS
19019 break;
19020 case DW_AT_high_pc:
19021 has_high_pc_attr = 1;
48fbe735 19022 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
19023 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19024 high_pc_relative = 1;
c906108c
SS
19025 break;
19026 case DW_AT_location:
0963b4bd 19027 /* Support the .debug_loc offsets. */
8e19ed76
PS
19028 if (attr_form_is_block (&attr))
19029 {
48fbe735 19030 d.locdesc = DW_BLOCK (&attr);
8e19ed76 19031 }
3690dd37 19032 else if (attr_form_is_section_offset (&attr))
8e19ed76 19033 {
4d3c2250 19034 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
19035 }
19036 else
19037 {
4d3c2250
KB
19038 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19039 "partial symbol information");
8e19ed76 19040 }
c906108c 19041 break;
c906108c 19042 case DW_AT_external:
48fbe735 19043 is_external = DW_UNSND (&attr);
c906108c
SS
19044 break;
19045 case DW_AT_declaration:
48fbe735 19046 is_declaration = DW_UNSND (&attr);
c906108c
SS
19047 break;
19048 case DW_AT_type:
48fbe735 19049 has_type = 1;
c906108c
SS
19050 break;
19051 case DW_AT_abstract_origin:
19052 case DW_AT_specification:
72bf9492 19053 case DW_AT_extension:
48fbe735
YQ
19054 has_specification = 1;
19055 spec_offset = dwarf2_get_ref_die_offset (&attr);
19056 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19057 || cu->per_cu->is_dwz);
c906108c
SS
19058 break;
19059 case DW_AT_sibling:
19060 /* Ignore absolute siblings, they might point outside of
19061 the current compile unit. */
19062 if (attr.form == DW_FORM_ref_addr)
b98664d3 19063 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19064 else
b9502d3f 19065 {
48fbe735 19066 const gdb_byte *buffer = reader->buffer;
9c541725
PA
19067 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19068 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19069
19070 if (sibling_ptr < info_ptr)
b98664d3 19071 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
19072 else if (sibling_ptr > reader->buffer_end)
19073 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19074 else
48fbe735 19075 sibling = sibling_ptr;
b9502d3f 19076 }
c906108c 19077 break;
fa4028e9 19078 case DW_AT_byte_size:
48fbe735 19079 has_byte_size = 1;
fa4028e9 19080 break;
ff908ebf 19081 case DW_AT_const_value:
48fbe735 19082 has_const_value = 1;
ff908ebf 19083 break;
68511cec
CES
19084 case DW_AT_calling_convention:
19085 /* DWARF doesn't provide a way to identify a program's source-level
19086 entry point. DW_AT_calling_convention attributes are only meant
19087 to describe functions' calling conventions.
19088
19089 However, because it's a necessary piece of information in
0c1b455e
TT
19090 Fortran, and before DWARF 4 DW_CC_program was the only
19091 piece of debugging information whose definition refers to
19092 a 'main program' at all, several compilers marked Fortran
19093 main programs with DW_CC_program --- even when those
19094 functions use the standard calling conventions.
19095
19096 Although DWARF now specifies a way to provide this
19097 information, we support this practice for backward
19098 compatibility. */
68511cec 19099 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19100 && cu->language == language_fortran)
48fbe735 19101 main_subprogram = 1;
68511cec 19102 break;
481860b3
GB
19103 case DW_AT_inline:
19104 if (DW_UNSND (&attr) == DW_INL_inlined
19105 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19106 may_be_inlined = 1;
481860b3 19107 break;
95554aad
TT
19108
19109 case DW_AT_import:
48fbe735 19110 if (tag == DW_TAG_imported_unit)
36586728 19111 {
48fbe735
YQ
19112 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19113 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19114 || cu->per_cu->is_dwz);
19115 }
95554aad
TT
19116 break;
19117
0c1b455e 19118 case DW_AT_main_subprogram:
48fbe735 19119 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19120 break;
19121
05caa1d2
TT
19122 case DW_AT_ranges:
19123 {
19124 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19125 but that requires a full DIE, so instead we just
19126 reimplement it. */
19127 int need_ranges_base = tag != DW_TAG_compile_unit;
19128 unsigned int ranges_offset = (DW_UNSND (&attr)
19129 + (need_ranges_base
19130 ? cu->ranges_base
19131 : 0));
19132
19133 /* Value of the DW_AT_ranges attribute is the offset in the
19134 .debug_ranges section. */
19135 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19136 nullptr))
19137 has_pc_info = 1;
19138 }
19139 break;
19140
c906108c
SS
19141 default:
19142 break;
19143 }
19144 }
19145
10d06d82
TT
19146 /* For Ada, if both the name and the linkage name appear, we prefer
19147 the latter. This lets "catch exception" work better, regardless
19148 of the order in which the name and linkage name were emitted.
19149 Really, though, this is just a workaround for the fact that gdb
19150 doesn't store both the name and the linkage name. */
19151 if (cu->language == language_ada && linkage_name != nullptr)
19152 name = linkage_name;
19153
91da1414 19154 if (high_pc_relative)
48fbe735 19155 highpc += lowpc;
91da1414 19156
9373cf26
JK
19157 if (has_low_pc_attr && has_high_pc_attr)
19158 {
19159 /* When using the GNU linker, .gnu.linkonce. sections are used to
19160 eliminate duplicate copies of functions and vtables and such.
19161 The linker will arbitrarily choose one and discard the others.
19162 The AT_*_pc values for such functions refer to local labels in
19163 these sections. If the section from that file was discarded, the
19164 labels are not in the output, so the relocs get a value of 0.
19165 If this is a discarded function, mark the pc bounds as invalid,
19166 so that GDB will ignore it. */
48fbe735 19167 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19168 {
48fbe735 19169 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19170 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19171
b98664d3 19172 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19173 "for DIE at %s [in module %s]"),
48fbe735
YQ
19174 paddress (gdbarch, lowpc),
19175 sect_offset_str (sect_off),
9d8780f0 19176 objfile_name (objfile));
9373cf26
JK
19177 }
19178 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19179 else if (lowpc >= highpc)
9373cf26 19180 {
48fbe735 19181 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19182 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19183
b98664d3 19184 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19185 "for DIE at %s [in module %s]"),
48fbe735
YQ
19186 paddress (gdbarch, lowpc),
19187 paddress (gdbarch, highpc),
19188 sect_offset_str (sect_off),
9c541725 19189 objfile_name (objfile));
9373cf26
JK
19190 }
19191 else
48fbe735 19192 has_pc_info = 1;
9373cf26 19193 }
85cbf3d3 19194
c906108c
SS
19195 return info_ptr;
19196}
19197
72bf9492
DJ
19198/* Find a cached partial DIE at OFFSET in CU. */
19199
d590ff25
YQ
19200struct partial_die_info *
19201dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19202{
19203 struct partial_die_info *lookup_die = NULL;
6f06d47b 19204 struct partial_die_info part_die (sect_off);
72bf9492 19205
9a3c8263 19206 lookup_die = ((struct partial_die_info *)
d590ff25 19207 htab_find_with_hash (partial_dies, &part_die,
9c541725 19208 to_underlying (sect_off)));
72bf9492 19209
72bf9492
DJ
19210 return lookup_die;
19211}
19212
348e048f
DE
19213/* Find a partial DIE at OFFSET, which may or may not be in CU,
19214 except in the case of .debug_types DIEs which do not reference
19215 outside their CU (they do however referencing other types via
55f1336d 19216 DW_FORM_ref_sig8). */
72bf9492 19217
122cf0f2 19218static const struct cu_partial_die_info
9c541725 19219find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19220{
518817b3
SM
19221 struct dwarf2_per_objfile *dwarf2_per_objfile
19222 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19223 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19224 struct dwarf2_per_cu_data *per_cu = NULL;
19225 struct partial_die_info *pd = NULL;
72bf9492 19226
36586728 19227 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19228 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19229 {
d590ff25 19230 pd = cu->find_partial_die (sect_off);
5afb4e99 19231 if (pd != NULL)
fb816e8b 19232 return { cu, pd };
0d99eb77
DE
19233 /* We missed recording what we needed.
19234 Load all dies and try again. */
19235 per_cu = cu->per_cu;
5afb4e99 19236 }
0d99eb77
DE
19237 else
19238 {
19239 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19240 if (cu->per_cu->is_debug_types)
0d99eb77 19241 {
9d8780f0
SM
19242 error (_("Dwarf Error: Type Unit at offset %s contains"
19243 " external reference to offset %s [in module %s].\n"),
19244 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19245 bfd_get_filename (objfile->obfd));
19246 }
9c541725 19247 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19248 dwarf2_per_objfile);
72bf9492 19249
0d99eb77
DE
19250 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19251 load_partial_comp_unit (per_cu);
ae038cb0 19252
0d99eb77 19253 per_cu->cu->last_used = 0;
d590ff25 19254 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19255 }
5afb4e99 19256
dee91e82
DE
19257 /* If we didn't find it, and not all dies have been loaded,
19258 load them all and try again. */
19259
5afb4e99
DJ
19260 if (pd == NULL && per_cu->load_all_dies == 0)
19261 {
5afb4e99 19262 per_cu->load_all_dies = 1;
fd820528
DE
19263
19264 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19265 THIS_CU->cu may already be in use. So we can't just free it and
19266 replace its DIEs with the ones we read in. Instead, we leave those
19267 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19268 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19269 set. */
dee91e82 19270 load_partial_comp_unit (per_cu);
5afb4e99 19271
d590ff25 19272 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19273 }
19274
19275 if (pd == NULL)
19276 internal_error (__FILE__, __LINE__,
9d8780f0 19277 _("could not find partial DIE %s "
3e43a32a 19278 "in cache [from module %s]\n"),
9d8780f0 19279 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19280 return { per_cu->cu, pd };
72bf9492
DJ
19281}
19282
abc72ce4
DE
19283/* See if we can figure out if the class lives in a namespace. We do
19284 this by looking for a member function; its demangled name will
19285 contain namespace info, if there is any. */
19286
19287static void
19288guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19289 struct dwarf2_cu *cu)
19290{
19291 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19292 what template types look like, because the demangler
19293 frequently doesn't give the same name as the debug info. We
19294 could fix this by only using the demangled name to get the
19295 prefix (but see comment in read_structure_type). */
19296
19297 struct partial_die_info *real_pdi;
19298 struct partial_die_info *child_pdi;
19299
19300 /* If this DIE (this DIE's specification, if any) has a parent, then
19301 we should not do this. We'll prepend the parent's fully qualified
19302 name when we create the partial symbol. */
19303
19304 real_pdi = struct_pdi;
19305 while (real_pdi->has_specification)
fb816e8b 19306 {
122cf0f2
AB
19307 auto res = find_partial_die (real_pdi->spec_offset,
19308 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19309 real_pdi = res.pdi;
19310 cu = res.cu;
19311 }
abc72ce4
DE
19312
19313 if (real_pdi->die_parent != NULL)
19314 return;
19315
19316 for (child_pdi = struct_pdi->die_child;
19317 child_pdi != NULL;
19318 child_pdi = child_pdi->die_sibling)
19319 {
19320 if (child_pdi->tag == DW_TAG_subprogram
19321 && child_pdi->linkage_name != NULL)
19322 {
43816ebc
TT
19323 gdb::unique_xmalloc_ptr<char> actual_class_name
19324 (language_class_name_from_physname (cu->language_defn,
19325 child_pdi->linkage_name));
abc72ce4
DE
19326 if (actual_class_name != NULL)
19327 {
518817b3 19328 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19329 struct_pdi->name
021887d8 19330 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 19331 actual_class_name.get ());
abc72ce4
DE
19332 }
19333 break;
19334 }
19335 }
19336}
19337
52356b79
YQ
19338void
19339partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19340{
abc72ce4
DE
19341 /* Once we've fixed up a die, there's no point in doing so again.
19342 This also avoids a memory leak if we were to call
19343 guess_partial_die_structure_name multiple times. */
52356b79 19344 if (fixup_called)
abc72ce4
DE
19345 return;
19346
72bf9492
DJ
19347 /* If we found a reference attribute and the DIE has no name, try
19348 to find a name in the referred to DIE. */
19349
52356b79 19350 if (name == NULL && has_specification)
72bf9492
DJ
19351 {
19352 struct partial_die_info *spec_die;
72bf9492 19353
122cf0f2 19354 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19355 spec_die = res.pdi;
19356 cu = res.cu;
72bf9492 19357
52356b79 19358 spec_die->fixup (cu);
72bf9492
DJ
19359
19360 if (spec_die->name)
19361 {
52356b79 19362 name = spec_die->name;
72bf9492
DJ
19363
19364 /* Copy DW_AT_external attribute if it is set. */
19365 if (spec_die->is_external)
52356b79 19366 is_external = spec_die->is_external;
72bf9492
DJ
19367 }
19368 }
19369
19370 /* Set default names for some unnamed DIEs. */
72bf9492 19371
52356b79
YQ
19372 if (name == NULL && tag == DW_TAG_namespace)
19373 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19374
abc72ce4
DE
19375 /* If there is no parent die to provide a namespace, and there are
19376 children, see if we can determine the namespace from their linkage
122d1940 19377 name. */
abc72ce4 19378 if (cu->language == language_cplus
fd5866f6 19379 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19380 && die_parent == NULL
19381 && has_children
19382 && (tag == DW_TAG_class_type
19383 || tag == DW_TAG_structure_type
19384 || tag == DW_TAG_union_type))
19385 guess_partial_die_structure_name (this, cu);
abc72ce4 19386
53832f31
TT
19387 /* GCC might emit a nameless struct or union that has a linkage
19388 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19389 if (name == NULL
19390 && (tag == DW_TAG_class_type
19391 || tag == DW_TAG_interface_type
19392 || tag == DW_TAG_structure_type
19393 || tag == DW_TAG_union_type)
19394 && linkage_name != NULL)
53832f31 19395 {
43816ebc
TT
19396 gdb::unique_xmalloc_ptr<char> demangled
19397 (gdb_demangle (linkage_name, DMGL_TYPES));
19398 if (demangled != nullptr)
53832f31 19399 {
96408a79
SA
19400 const char *base;
19401
19402 /* Strip any leading namespaces/classes, keep only the base name.
19403 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19404 base = strrchr (demangled.get (), ':');
19405 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19406 base++;
19407 else
43816ebc 19408 base = demangled.get ();
96408a79 19409
518817b3 19410 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19411 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19412 }
19413 }
19414
52356b79 19415 fixup_called = 1;
72bf9492
DJ
19416}
19417
a8329558 19418/* Read an attribute value described by an attribute form. */
c906108c 19419
d521ce57 19420static const gdb_byte *
dee91e82
DE
19421read_attribute_value (const struct die_reader_specs *reader,
19422 struct attribute *attr, unsigned form,
43988095 19423 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19424{
dee91e82 19425 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19426 struct dwarf2_per_objfile *dwarf2_per_objfile
19427 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19428 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19429 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19430 bfd *abfd = reader->abfd;
e7c27a73 19431 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19432 unsigned int bytes_read;
19433 struct dwarf_block *blk;
19434
aead7601 19435 attr->form = (enum dwarf_form) form;
a8329558 19436 switch (form)
c906108c 19437 {
c906108c 19438 case DW_FORM_ref_addr:
ae411497 19439 if (cu->header.version == 2)
4568ecf9 19440 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19441 else
4568ecf9
DE
19442 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19443 &cu->header, &bytes_read);
ae411497
TT
19444 info_ptr += bytes_read;
19445 break;
36586728
TT
19446 case DW_FORM_GNU_ref_alt:
19447 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19448 info_ptr += bytes_read;
19449 break;
ae411497 19450 case DW_FORM_addr:
e7c27a73 19451 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19452 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19453 info_ptr += bytes_read;
c906108c
SS
19454 break;
19455 case DW_FORM_block2:
7b5a2f43 19456 blk = dwarf_alloc_block (cu);
c906108c
SS
19457 blk->size = read_2_bytes (abfd, info_ptr);
19458 info_ptr += 2;
19459 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19460 info_ptr += blk->size;
19461 DW_BLOCK (attr) = blk;
19462 break;
19463 case DW_FORM_block4:
7b5a2f43 19464 blk = dwarf_alloc_block (cu);
c906108c
SS
19465 blk->size = read_4_bytes (abfd, info_ptr);
19466 info_ptr += 4;
19467 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19468 info_ptr += blk->size;
19469 DW_BLOCK (attr) = blk;
19470 break;
19471 case DW_FORM_data2:
19472 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19473 info_ptr += 2;
19474 break;
19475 case DW_FORM_data4:
19476 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19477 info_ptr += 4;
19478 break;
19479 case DW_FORM_data8:
19480 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19481 info_ptr += 8;
19482 break;
0224619f
JK
19483 case DW_FORM_data16:
19484 blk = dwarf_alloc_block (cu);
19485 blk->size = 16;
19486 blk->data = read_n_bytes (abfd, info_ptr, 16);
19487 info_ptr += 16;
19488 DW_BLOCK (attr) = blk;
19489 break;
2dc7f7b3
TT
19490 case DW_FORM_sec_offset:
19491 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19492 info_ptr += bytes_read;
19493 break;
c906108c 19494 case DW_FORM_string:
9b1c24c8 19495 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19496 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19497 info_ptr += bytes_read;
19498 break;
4bdf3d34 19499 case DW_FORM_strp:
36586728
TT
19500 if (!cu->per_cu->is_dwz)
19501 {
ed2dc618
SM
19502 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19503 abfd, info_ptr, cu_header,
36586728
TT
19504 &bytes_read);
19505 DW_STRING_IS_CANONICAL (attr) = 0;
19506 info_ptr += bytes_read;
19507 break;
19508 }
19509 /* FALLTHROUGH */
43988095
JK
19510 case DW_FORM_line_strp:
19511 if (!cu->per_cu->is_dwz)
19512 {
ed2dc618
SM
19513 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19514 abfd, info_ptr,
43988095
JK
19515 cu_header, &bytes_read);
19516 DW_STRING_IS_CANONICAL (attr) = 0;
19517 info_ptr += bytes_read;
19518 break;
19519 }
19520 /* FALLTHROUGH */
36586728
TT
19521 case DW_FORM_GNU_strp_alt:
19522 {
ed2dc618 19523 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19524 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19525 &bytes_read);
19526
ed2dc618
SM
19527 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19528 dwz, str_offset);
36586728
TT
19529 DW_STRING_IS_CANONICAL (attr) = 0;
19530 info_ptr += bytes_read;
19531 }
4bdf3d34 19532 break;
2dc7f7b3 19533 case DW_FORM_exprloc:
c906108c 19534 case DW_FORM_block:
7b5a2f43 19535 blk = dwarf_alloc_block (cu);
c906108c
SS
19536 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19537 info_ptr += bytes_read;
19538 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19539 info_ptr += blk->size;
19540 DW_BLOCK (attr) = blk;
19541 break;
19542 case DW_FORM_block1:
7b5a2f43 19543 blk = dwarf_alloc_block (cu);
c906108c
SS
19544 blk->size = read_1_byte (abfd, info_ptr);
19545 info_ptr += 1;
19546 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19547 info_ptr += blk->size;
19548 DW_BLOCK (attr) = blk;
19549 break;
19550 case DW_FORM_data1:
19551 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19552 info_ptr += 1;
19553 break;
19554 case DW_FORM_flag:
19555 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19556 info_ptr += 1;
19557 break;
2dc7f7b3
TT
19558 case DW_FORM_flag_present:
19559 DW_UNSND (attr) = 1;
19560 break;
c906108c
SS
19561 case DW_FORM_sdata:
19562 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19563 info_ptr += bytes_read;
19564 break;
19565 case DW_FORM_udata:
19566 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19567 info_ptr += bytes_read;
19568 break;
19569 case DW_FORM_ref1:
9c541725 19570 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19571 + read_1_byte (abfd, info_ptr));
c906108c
SS
19572 info_ptr += 1;
19573 break;
19574 case DW_FORM_ref2:
9c541725 19575 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19576 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19577 info_ptr += 2;
19578 break;
19579 case DW_FORM_ref4:
9c541725 19580 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19581 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19582 info_ptr += 4;
19583 break;
613e1657 19584 case DW_FORM_ref8:
9c541725 19585 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19586 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19587 info_ptr += 8;
19588 break;
55f1336d 19589 case DW_FORM_ref_sig8:
ac9ec31b 19590 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19591 info_ptr += 8;
19592 break;
c906108c 19593 case DW_FORM_ref_udata:
9c541725 19594 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19595 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19596 info_ptr += bytes_read;
19597 break;
c906108c 19598 case DW_FORM_indirect:
a8329558
KW
19599 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19600 info_ptr += bytes_read;
43988095
JK
19601 if (form == DW_FORM_implicit_const)
19602 {
19603 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19604 info_ptr += bytes_read;
19605 }
19606 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19607 info_ptr);
19608 break;
19609 case DW_FORM_implicit_const:
19610 DW_SND (attr) = implicit_const;
a8329558 19611 break;
336d760d 19612 case DW_FORM_addrx:
3019eac3
DE
19613 case DW_FORM_GNU_addr_index:
19614 if (reader->dwo_file == NULL)
19615 {
19616 /* For now flag a hard error.
19617 Later we can turn this into a complaint. */
19618 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19619 dwarf_form_name (form),
19620 bfd_get_filename (abfd));
19621 }
19622 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19623 info_ptr += bytes_read;
19624 break;
cf532bd1 19625 case DW_FORM_strx:
15f18d14
AT
19626 case DW_FORM_strx1:
19627 case DW_FORM_strx2:
19628 case DW_FORM_strx3:
19629 case DW_FORM_strx4:
3019eac3
DE
19630 case DW_FORM_GNU_str_index:
19631 if (reader->dwo_file == NULL)
19632 {
19633 /* For now flag a hard error.
19634 Later we can turn this into a complaint if warranted. */
19635 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19636 dwarf_form_name (form),
19637 bfd_get_filename (abfd));
19638 }
19639 {
15f18d14
AT
19640 ULONGEST str_index;
19641 if (form == DW_FORM_strx1)
19642 {
19643 str_index = read_1_byte (abfd, info_ptr);
19644 info_ptr += 1;
19645 }
19646 else if (form == DW_FORM_strx2)
19647 {
19648 str_index = read_2_bytes (abfd, info_ptr);
19649 info_ptr += 2;
19650 }
19651 else if (form == DW_FORM_strx3)
19652 {
19653 str_index = read_3_bytes (abfd, info_ptr);
19654 info_ptr += 3;
19655 }
19656 else if (form == DW_FORM_strx4)
19657 {
19658 str_index = read_4_bytes (abfd, info_ptr);
19659 info_ptr += 4;
19660 }
19661 else
19662 {
19663 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19664 info_ptr += bytes_read;
19665 }
342587c4 19666 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19667 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19668 }
19669 break;
c906108c 19670 default:
8a3fe4f8 19671 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19672 dwarf_form_name (form),
19673 bfd_get_filename (abfd));
c906108c 19674 }
28e94949 19675
36586728 19676 /* Super hack. */
7771576e 19677 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19678 attr->form = DW_FORM_GNU_ref_alt;
19679
28e94949
JB
19680 /* We have seen instances where the compiler tried to emit a byte
19681 size attribute of -1 which ended up being encoded as an unsigned
19682 0xffffffff. Although 0xffffffff is technically a valid size value,
19683 an object of this size seems pretty unlikely so we can relatively
19684 safely treat these cases as if the size attribute was invalid and
19685 treat them as zero by default. */
19686 if (attr->name == DW_AT_byte_size
19687 && form == DW_FORM_data4
19688 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19689 {
19690 complaint
b98664d3 19691 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19692 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19693 DW_UNSND (attr) = 0;
19694 }
28e94949 19695
c906108c
SS
19696 return info_ptr;
19697}
19698
a8329558
KW
19699/* Read an attribute described by an abbreviated attribute. */
19700
d521ce57 19701static const gdb_byte *
dee91e82
DE
19702read_attribute (const struct die_reader_specs *reader,
19703 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19704 const gdb_byte *info_ptr)
a8329558
KW
19705{
19706 attr->name = abbrev->name;
43988095
JK
19707 return read_attribute_value (reader, attr, abbrev->form,
19708 abbrev->implicit_const, info_ptr);
a8329558
KW
19709}
19710
0963b4bd 19711/* Read dwarf information from a buffer. */
c906108c
SS
19712
19713static unsigned int
a1855c1d 19714read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19715{
fe1b8b76 19716 return bfd_get_8 (abfd, buf);
c906108c
SS
19717}
19718
19719static int
a1855c1d 19720read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19721{
fe1b8b76 19722 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19723}
19724
19725static unsigned int
a1855c1d 19726read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19727{
fe1b8b76 19728 return bfd_get_16 (abfd, buf);
c906108c
SS
19729}
19730
21ae7a4d 19731static int
a1855c1d 19732read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19733{
19734 return bfd_get_signed_16 (abfd, buf);
19735}
19736
15f18d14
AT
19737static unsigned int
19738read_3_bytes (bfd *abfd, const gdb_byte *buf)
19739{
19740 unsigned int result = 0;
19741 for (int i = 0; i < 3; ++i)
19742 {
19743 unsigned char byte = bfd_get_8 (abfd, buf);
19744 buf++;
19745 result |= ((unsigned int) byte << (i * 8));
19746 }
19747 return result;
19748}
19749
c906108c 19750static unsigned int
a1855c1d 19751read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19752{
fe1b8b76 19753 return bfd_get_32 (abfd, buf);
c906108c
SS
19754}
19755
21ae7a4d 19756static int
a1855c1d 19757read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19758{
19759 return bfd_get_signed_32 (abfd, buf);
19760}
19761
93311388 19762static ULONGEST
a1855c1d 19763read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19764{
fe1b8b76 19765 return bfd_get_64 (abfd, buf);
c906108c
SS
19766}
19767
19768static CORE_ADDR
d521ce57 19769read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19770 unsigned int *bytes_read)
c906108c 19771{
e7c27a73 19772 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19773 CORE_ADDR retval = 0;
19774
107d2387 19775 if (cu_header->signed_addr_p)
c906108c 19776 {
107d2387
AC
19777 switch (cu_header->addr_size)
19778 {
19779 case 2:
fe1b8b76 19780 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19781 break;
19782 case 4:
fe1b8b76 19783 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19784 break;
19785 case 8:
fe1b8b76 19786 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19787 break;
19788 default:
8e65ff28 19789 internal_error (__FILE__, __LINE__,
e2e0b3e5 19790 _("read_address: bad switch, signed [in module %s]"),
659b0389 19791 bfd_get_filename (abfd));
107d2387
AC
19792 }
19793 }
19794 else
19795 {
19796 switch (cu_header->addr_size)
19797 {
19798 case 2:
fe1b8b76 19799 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19800 break;
19801 case 4:
fe1b8b76 19802 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19803 break;
19804 case 8:
fe1b8b76 19805 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19806 break;
19807 default:
8e65ff28 19808 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19809 _("read_address: bad switch, "
19810 "unsigned [in module %s]"),
659b0389 19811 bfd_get_filename (abfd));
107d2387 19812 }
c906108c 19813 }
64367e0a 19814
107d2387
AC
19815 *bytes_read = cu_header->addr_size;
19816 return retval;
c906108c
SS
19817}
19818
f7ef9339 19819/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19820 specification allows the initial length to take up either 4 bytes
19821 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19822 bytes describe the length and all offsets will be 8 bytes in length
19823 instead of 4.
19824
f7ef9339
KB
19825 An older, non-standard 64-bit format is also handled by this
19826 function. The older format in question stores the initial length
19827 as an 8-byte quantity without an escape value. Lengths greater
19828 than 2^32 aren't very common which means that the initial 4 bytes
19829 is almost always zero. Since a length value of zero doesn't make
19830 sense for the 32-bit format, this initial zero can be considered to
19831 be an escape value which indicates the presence of the older 64-bit
19832 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19833 greater than 4GB. If it becomes necessary to handle lengths
19834 somewhat larger than 4GB, we could allow other small values (such
19835 as the non-sensical values of 1, 2, and 3) to also be used as
19836 escape values indicating the presence of the old format.
f7ef9339 19837
917c78fc
MK
19838 The value returned via bytes_read should be used to increment the
19839 relevant pointer after calling read_initial_length().
c764a876 19840
613e1657
KB
19841 [ Note: read_initial_length() and read_offset() are based on the
19842 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19843 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19844 from:
19845
f7ef9339 19846 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19847
613e1657
KB
19848 This document is only a draft and is subject to change. (So beware.)
19849
f7ef9339 19850 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19851 determined empirically by examining 64-bit ELF files produced by
19852 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19853
19854 - Kevin, July 16, 2002
613e1657
KB
19855 ] */
19856
19857static LONGEST
d521ce57 19858read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19859{
fe1b8b76 19860 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19861
dd373385 19862 if (length == 0xffffffff)
613e1657 19863 {
fe1b8b76 19864 length = bfd_get_64 (abfd, buf + 4);
613e1657 19865 *bytes_read = 12;
613e1657 19866 }
dd373385 19867 else if (length == 0)
f7ef9339 19868 {
dd373385 19869 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19870 length = bfd_get_64 (abfd, buf);
f7ef9339 19871 *bytes_read = 8;
f7ef9339 19872 }
613e1657
KB
19873 else
19874 {
19875 *bytes_read = 4;
613e1657
KB
19876 }
19877
c764a876
DE
19878 return length;
19879}
dd373385 19880
c764a876
DE
19881/* Cover function for read_initial_length.
19882 Returns the length of the object at BUF, and stores the size of the
19883 initial length in *BYTES_READ and stores the size that offsets will be in
19884 *OFFSET_SIZE.
19885 If the initial length size is not equivalent to that specified in
19886 CU_HEADER then issue a complaint.
19887 This is useful when reading non-comp-unit headers. */
dd373385 19888
c764a876 19889static LONGEST
d521ce57 19890read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19891 const struct comp_unit_head *cu_header,
19892 unsigned int *bytes_read,
19893 unsigned int *offset_size)
19894{
19895 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19896
19897 gdb_assert (cu_header->initial_length_size == 4
19898 || cu_header->initial_length_size == 8
19899 || cu_header->initial_length_size == 12);
19900
19901 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19902 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19903
c764a876 19904 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19905 return length;
613e1657
KB
19906}
19907
19908/* Read an offset from the data stream. The size of the offset is
917c78fc 19909 given by cu_header->offset_size. */
613e1657
KB
19910
19911static LONGEST
d521ce57
TT
19912read_offset (bfd *abfd, const gdb_byte *buf,
19913 const struct comp_unit_head *cu_header,
891d2f0b 19914 unsigned int *bytes_read)
c764a876
DE
19915{
19916 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19917
c764a876
DE
19918 *bytes_read = cu_header->offset_size;
19919 return offset;
19920}
19921
19922/* Read an offset from the data stream. */
19923
19924static LONGEST
d521ce57 19925read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19926{
19927 LONGEST retval = 0;
19928
c764a876 19929 switch (offset_size)
613e1657
KB
19930 {
19931 case 4:
fe1b8b76 19932 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19933 break;
19934 case 8:
fe1b8b76 19935 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19936 break;
19937 default:
8e65ff28 19938 internal_error (__FILE__, __LINE__,
c764a876 19939 _("read_offset_1: bad switch [in module %s]"),
659b0389 19940 bfd_get_filename (abfd));
613e1657
KB
19941 }
19942
917c78fc 19943 return retval;
613e1657
KB
19944}
19945
d521ce57
TT
19946static const gdb_byte *
19947read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19948{
19949 /* If the size of a host char is 8 bits, we can return a pointer
19950 to the buffer, otherwise we have to copy the data to a buffer
19951 allocated on the temporary obstack. */
4bdf3d34 19952 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19953 return buf;
c906108c
SS
19954}
19955
d521ce57
TT
19956static const char *
19957read_direct_string (bfd *abfd, const gdb_byte *buf,
19958 unsigned int *bytes_read_ptr)
c906108c
SS
19959{
19960 /* If the size of a host char is 8 bits, we can return a pointer
19961 to the string, otherwise we have to copy the string to a buffer
19962 allocated on the temporary obstack. */
4bdf3d34 19963 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19964 if (*buf == '\0')
19965 {
19966 *bytes_read_ptr = 1;
19967 return NULL;
19968 }
d521ce57
TT
19969 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19970 return (const char *) buf;
4bdf3d34
JJ
19971}
19972
43988095
JK
19973/* Return pointer to string at section SECT offset STR_OFFSET with error
19974 reporting strings FORM_NAME and SECT_NAME. */
19975
d521ce57 19976static const char *
ed2dc618
SM
19977read_indirect_string_at_offset_from (struct objfile *objfile,
19978 bfd *abfd, LONGEST str_offset,
43988095
JK
19979 struct dwarf2_section_info *sect,
19980 const char *form_name,
19981 const char *sect_name)
19982{
ed2dc618 19983 dwarf2_read_section (objfile, sect);
43988095
JK
19984 if (sect->buffer == NULL)
19985 error (_("%s used without %s section [in module %s]"),
19986 form_name, sect_name, bfd_get_filename (abfd));
19987 if (str_offset >= sect->size)
19988 error (_("%s pointing outside of %s section [in module %s]"),
19989 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19990 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19991 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19992 return NULL;
43988095
JK
19993 return (const char *) (sect->buffer + str_offset);
19994}
19995
19996/* Return pointer to string at .debug_str offset STR_OFFSET. */
19997
19998static const char *
ed2dc618
SM
19999read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20000 bfd *abfd, LONGEST str_offset)
43988095 20001{
ed2dc618
SM
20002 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20003 abfd, str_offset,
43988095
JK
20004 &dwarf2_per_objfile->str,
20005 "DW_FORM_strp", ".debug_str");
20006}
20007
20008/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
20009
20010static const char *
ed2dc618
SM
20011read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20012 bfd *abfd, LONGEST str_offset)
43988095 20013{
ed2dc618
SM
20014 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20015 abfd, str_offset,
43988095
JK
20016 &dwarf2_per_objfile->line_str,
20017 "DW_FORM_line_strp",
20018 ".debug_line_str");
c906108c
SS
20019}
20020
36586728
TT
20021/* Read a string at offset STR_OFFSET in the .debug_str section from
20022 the .dwz file DWZ. Throw an error if the offset is too large. If
20023 the string consists of a single NUL byte, return NULL; otherwise
20024 return a pointer to the string. */
20025
d521ce57 20026static const char *
ed2dc618
SM
20027read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20028 LONGEST str_offset)
36586728 20029{
ed2dc618 20030 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
20031
20032 if (dwz->str.buffer == NULL)
20033 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20034 "section [in module %s]"),
00f93c44 20035 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20036 if (str_offset >= dwz->str.size)
20037 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20038 ".debug_str section [in module %s]"),
00f93c44 20039 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20040 gdb_assert (HOST_CHAR_BIT == 8);
20041 if (dwz->str.buffer[str_offset] == '\0')
20042 return NULL;
d521ce57 20043 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
20044}
20045
43988095
JK
20046/* Return pointer to string at .debug_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
d521ce57 20050static const char *
ed2dc618
SM
20051read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20052 const gdb_byte *buf,
cf2c3c16
TT
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 20058 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
20059}
20060
43988095
JK
20061/* Return pointer to string at .debug_line_str offset as read from BUF.
20062 BUF is assumed to be in a compilation unit described by CU_HEADER.
20063 Return *BYTES_READ_PTR count of bytes read from BUF. */
20064
20065static const char *
ed2dc618
SM
20066read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20067 bfd *abfd, const gdb_byte *buf,
43988095
JK
20068 const struct comp_unit_head *cu_header,
20069 unsigned int *bytes_read_ptr)
20070{
20071 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20072
ed2dc618
SM
20073 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20074 str_offset);
43988095
JK
20075}
20076
20077ULONGEST
d521ce57 20078read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 20079 unsigned int *bytes_read_ptr)
c906108c 20080{
12df843f 20081 ULONGEST result;
ce5d95e1 20082 unsigned int num_read;
870f88f7 20083 int shift;
c906108c
SS
20084 unsigned char byte;
20085
20086 result = 0;
20087 shift = 0;
20088 num_read = 0;
c906108c
SS
20089 while (1)
20090 {
fe1b8b76 20091 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20092 buf++;
20093 num_read++;
12df843f 20094 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20095 if ((byte & 128) == 0)
20096 {
20097 break;
20098 }
20099 shift += 7;
20100 }
20101 *bytes_read_ptr = num_read;
20102 return result;
20103}
20104
12df843f 20105static LONGEST
d521ce57
TT
20106read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20107 unsigned int *bytes_read_ptr)
c906108c 20108{
4dd1b460 20109 ULONGEST result;
870f88f7 20110 int shift, num_read;
c906108c
SS
20111 unsigned char byte;
20112
20113 result = 0;
20114 shift = 0;
c906108c 20115 num_read = 0;
c906108c
SS
20116 while (1)
20117 {
fe1b8b76 20118 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20119 buf++;
20120 num_read++;
4dd1b460 20121 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20122 shift += 7;
20123 if ((byte & 128) == 0)
20124 {
20125 break;
20126 }
20127 }
77e0b926 20128 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 20129 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
20130 *bytes_read_ptr = num_read;
20131 return result;
20132}
20133
3019eac3
DE
20134/* Given index ADDR_INDEX in .debug_addr, fetch the value.
20135 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20136 ADDR_SIZE is the size of addresses from the CU header. */
20137
20138static CORE_ADDR
ed2dc618
SM
20139read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20140 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
20141{
20142 struct objfile *objfile = dwarf2_per_objfile->objfile;
20143 bfd *abfd = objfile->obfd;
20144 const gdb_byte *info_ptr;
20145
20146 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20147 if (dwarf2_per_objfile->addr.buffer == NULL)
20148 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20149 objfile_name (objfile));
3019eac3
DE
20150 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20151 error (_("DW_FORM_addr_index pointing outside of "
20152 ".debug_addr section [in module %s]"),
4262abfb 20153 objfile_name (objfile));
3019eac3
DE
20154 info_ptr = (dwarf2_per_objfile->addr.buffer
20155 + addr_base + addr_index * addr_size);
20156 if (addr_size == 4)
20157 return bfd_get_32 (abfd, info_ptr);
20158 else
20159 return bfd_get_64 (abfd, info_ptr);
20160}
20161
20162/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20163
20164static CORE_ADDR
20165read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20166{
518817b3
SM
20167 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20168 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20169}
20170
20171/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20172
20173static CORE_ADDR
d521ce57 20174read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20175 unsigned int *bytes_read)
20176{
518817b3 20177 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20178 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20179
20180 return read_addr_index (cu, addr_index);
20181}
20182
20183/* Data structure to pass results from dwarf2_read_addr_index_reader
20184 back to dwarf2_read_addr_index. */
20185
20186struct dwarf2_read_addr_index_data
20187{
20188 ULONGEST addr_base;
20189 int addr_size;
20190};
20191
20192/* die_reader_func for dwarf2_read_addr_index. */
20193
20194static void
20195dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 20196 const gdb_byte *info_ptr,
3019eac3
DE
20197 struct die_info *comp_unit_die,
20198 int has_children,
20199 void *data)
20200{
20201 struct dwarf2_cu *cu = reader->cu;
20202 struct dwarf2_read_addr_index_data *aidata =
20203 (struct dwarf2_read_addr_index_data *) data;
20204
20205 aidata->addr_base = cu->addr_base;
20206 aidata->addr_size = cu->header.addr_size;
20207}
20208
20209/* Given an index in .debug_addr, fetch the value.
20210 NOTE: This can be called during dwarf expression evaluation,
20211 long after the debug information has been read, and thus per_cu->cu
20212 may no longer exist. */
20213
20214CORE_ADDR
20215dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20216 unsigned int addr_index)
20217{
ed2dc618 20218 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
20219 struct dwarf2_cu *cu = per_cu->cu;
20220 ULONGEST addr_base;
20221 int addr_size;
20222
3019eac3
DE
20223 /* We need addr_base and addr_size.
20224 If we don't have PER_CU->cu, we have to get it.
20225 Nasty, but the alternative is storing the needed info in PER_CU,
20226 which at this point doesn't seem justified: it's not clear how frequently
20227 it would get used and it would increase the size of every PER_CU.
20228 Entry points like dwarf2_per_cu_addr_size do a similar thing
20229 so we're not in uncharted territory here.
20230 Alas we need to be a bit more complicated as addr_base is contained
20231 in the DIE.
20232
20233 We don't need to read the entire CU(/TU).
20234 We just need the header and top level die.
a1b64ce1 20235
3019eac3 20236 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20237 For now we skip this optimization. */
3019eac3
DE
20238
20239 if (cu != NULL)
20240 {
20241 addr_base = cu->addr_base;
20242 addr_size = cu->header.addr_size;
20243 }
20244 else
20245 {
20246 struct dwarf2_read_addr_index_data aidata;
20247
a1b64ce1
DE
20248 /* Note: We can't use init_cutu_and_read_dies_simple here,
20249 we need addr_base. */
58f0c718 20250 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 20251 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20252 addr_base = aidata.addr_base;
20253 addr_size = aidata.addr_size;
20254 }
20255
ed2dc618
SM
20256 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20257 addr_size);
3019eac3
DE
20258}
20259
cf532bd1 20260/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 20261 This is only used by the Fission support. */
3019eac3 20262
d521ce57 20263static const char *
342587c4 20264read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20265{
ed2dc618 20266 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20267 struct dwarf2_per_objfile *dwarf2_per_objfile
20268 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20269 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20270 const char *objf_name = objfile_name (objfile);
3019eac3 20271 bfd *abfd = objfile->obfd;
73869dc2
DE
20272 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20273 struct dwarf2_section_info *str_offsets_section =
20274 &reader->dwo_file->sections.str_offsets;
d521ce57 20275 const gdb_byte *info_ptr;
3019eac3 20276 ULONGEST str_offset;
cf532bd1 20277 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20278
73869dc2
DE
20279 dwarf2_read_section (objfile, str_section);
20280 dwarf2_read_section (objfile, str_offsets_section);
20281 if (str_section->buffer == NULL)
57d63ce2 20282 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20283 " in CU at offset %s [in module %s]"),
20284 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20285 if (str_offsets_section->buffer == NULL)
57d63ce2 20286 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20287 " in CU at offset %s [in module %s]"),
20288 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20289 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20290 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20291 " section in CU at offset %s [in module %s]"),
20292 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20293 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20294 + str_index * cu->header.offset_size);
20295 if (cu->header.offset_size == 4)
20296 str_offset = bfd_get_32 (abfd, info_ptr);
20297 else
20298 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20299 if (str_offset >= str_section->size)
57d63ce2 20300 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20301 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20302 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20303 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20304}
20305
3019eac3
DE
20306/* Return the length of an LEB128 number in BUF. */
20307
20308static int
20309leb128_size (const gdb_byte *buf)
20310{
20311 const gdb_byte *begin = buf;
20312 gdb_byte byte;
20313
20314 while (1)
20315 {
20316 byte = *buf++;
20317 if ((byte & 128) == 0)
20318 return buf - begin;
20319 }
20320}
20321
c906108c 20322static void
e142c38c 20323set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20324{
20325 switch (lang)
20326 {
20327 case DW_LANG_C89:
76bee0cc 20328 case DW_LANG_C99:
0cfd832f 20329 case DW_LANG_C11:
c906108c 20330 case DW_LANG_C:
d1be3247 20331 case DW_LANG_UPC:
e142c38c 20332 cu->language = language_c;
c906108c 20333 break;
9c37b5ae 20334 case DW_LANG_Java:
c906108c 20335 case DW_LANG_C_plus_plus:
0cfd832f
MW
20336 case DW_LANG_C_plus_plus_11:
20337 case DW_LANG_C_plus_plus_14:
e142c38c 20338 cu->language = language_cplus;
c906108c 20339 break;
6aecb9c2
JB
20340 case DW_LANG_D:
20341 cu->language = language_d;
20342 break;
c906108c
SS
20343 case DW_LANG_Fortran77:
20344 case DW_LANG_Fortran90:
b21b22e0 20345 case DW_LANG_Fortran95:
f7de9aab
MW
20346 case DW_LANG_Fortran03:
20347 case DW_LANG_Fortran08:
e142c38c 20348 cu->language = language_fortran;
c906108c 20349 break;
a766d390
DE
20350 case DW_LANG_Go:
20351 cu->language = language_go;
20352 break;
c906108c 20353 case DW_LANG_Mips_Assembler:
e142c38c 20354 cu->language = language_asm;
c906108c
SS
20355 break;
20356 case DW_LANG_Ada83:
8aaf0b47 20357 case DW_LANG_Ada95:
bc5f45f8
JB
20358 cu->language = language_ada;
20359 break;
72019c9c
GM
20360 case DW_LANG_Modula2:
20361 cu->language = language_m2;
20362 break;
fe8e67fd
PM
20363 case DW_LANG_Pascal83:
20364 cu->language = language_pascal;
20365 break;
22566fbd
DJ
20366 case DW_LANG_ObjC:
20367 cu->language = language_objc;
20368 break;
c44af4eb
TT
20369 case DW_LANG_Rust:
20370 case DW_LANG_Rust_old:
20371 cu->language = language_rust;
20372 break;
c906108c
SS
20373 case DW_LANG_Cobol74:
20374 case DW_LANG_Cobol85:
c906108c 20375 default:
e142c38c 20376 cu->language = language_minimal;
c906108c
SS
20377 break;
20378 }
e142c38c 20379 cu->language_defn = language_def (cu->language);
c906108c
SS
20380}
20381
20382/* Return the named attribute or NULL if not there. */
20383
20384static struct attribute *
e142c38c 20385dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20386{
a48e046c 20387 for (;;)
c906108c 20388 {
a48e046c
TT
20389 unsigned int i;
20390 struct attribute *spec = NULL;
20391
20392 for (i = 0; i < die->num_attrs; ++i)
20393 {
20394 if (die->attrs[i].name == name)
20395 return &die->attrs[i];
20396 if (die->attrs[i].name == DW_AT_specification
20397 || die->attrs[i].name == DW_AT_abstract_origin)
20398 spec = &die->attrs[i];
20399 }
20400
20401 if (!spec)
20402 break;
c906108c 20403
f2f0e013 20404 die = follow_die_ref (die, spec, &cu);
f2f0e013 20405 }
c5aa993b 20406
c906108c
SS
20407 return NULL;
20408}
20409
348e048f
DE
20410/* Return the named attribute or NULL if not there,
20411 but do not follow DW_AT_specification, etc.
20412 This is for use in contexts where we're reading .debug_types dies.
20413 Following DW_AT_specification, DW_AT_abstract_origin will take us
20414 back up the chain, and we want to go down. */
20415
20416static struct attribute *
45e58e77 20417dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20418{
20419 unsigned int i;
20420
20421 for (i = 0; i < die->num_attrs; ++i)
20422 if (die->attrs[i].name == name)
20423 return &die->attrs[i];
20424
20425 return NULL;
20426}
20427
7d45c7c3
KB
20428/* Return the string associated with a string-typed attribute, or NULL if it
20429 is either not found or is of an incorrect type. */
20430
20431static const char *
20432dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20433{
20434 struct attribute *attr;
20435 const char *str = NULL;
20436
20437 attr = dwarf2_attr (die, name, cu);
20438
20439 if (attr != NULL)
20440 {
43988095 20441 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20442 || attr->form == DW_FORM_string
cf532bd1 20443 || attr->form == DW_FORM_strx
8fe0f950
AT
20444 || attr->form == DW_FORM_strx1
20445 || attr->form == DW_FORM_strx2
20446 || attr->form == DW_FORM_strx3
20447 || attr->form == DW_FORM_strx4
b3340438 20448 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20449 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20450 str = DW_STRING (attr);
20451 else
b98664d3 20452 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20453 "DIE at %s in module %s"),
20454 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20455 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20456 }
20457
20458 return str;
20459}
20460
a084a2a6 20461/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20462 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20463static const char *
20464dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20465{
20466 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20467 if (dwo_name == nullptr)
20468 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20469 return dwo_name;
20470}
20471
05cf31d1
JB
20472/* Return non-zero iff the attribute NAME is defined for the given DIE,
20473 and holds a non-zero value. This function should only be used for
2dc7f7b3 20474 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20475
20476static int
20477dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20478{
20479 struct attribute *attr = dwarf2_attr (die, name, cu);
20480
20481 return (attr && DW_UNSND (attr));
20482}
20483
3ca72b44 20484static int
e142c38c 20485die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20486{
05cf31d1
JB
20487 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20488 which value is non-zero. However, we have to be careful with
20489 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20490 (via dwarf2_flag_true_p) follows this attribute. So we may
20491 end up accidently finding a declaration attribute that belongs
20492 to a different DIE referenced by the specification attribute,
20493 even though the given DIE does not have a declaration attribute. */
20494 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20495 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20496}
20497
63d06c5c 20498/* Return the die giving the specification for DIE, if there is
f2f0e013 20499 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20500 containing the return value on output. If there is no
20501 specification, but there is an abstract origin, that is
20502 returned. */
63d06c5c
DC
20503
20504static struct die_info *
f2f0e013 20505die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20506{
f2f0e013
DJ
20507 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20508 *spec_cu);
63d06c5c 20509
edb3359d
DJ
20510 if (spec_attr == NULL)
20511 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20512
63d06c5c
DC
20513 if (spec_attr == NULL)
20514 return NULL;
20515 else
f2f0e013 20516 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20517}
c906108c 20518
527f3840
JK
20519/* Stub for free_line_header to match void * callback types. */
20520
20521static void
20522free_line_header_voidp (void *arg)
20523{
9a3c8263 20524 struct line_header *lh = (struct line_header *) arg;
527f3840 20525
fff8551c 20526 delete lh;
527f3840
JK
20527}
20528
fff8551c
PA
20529void
20530line_header::add_include_dir (const char *include_dir)
c906108c 20531{
27e0867f 20532 if (dwarf_line_debug >= 2)
7ba99d21
AT
20533 {
20534 size_t new_size;
20535 if (version >= 5)
20536 new_size = m_include_dirs.size ();
20537 else
20538 new_size = m_include_dirs.size () + 1;
20539 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20540 new_size, include_dir);
20541 }
20542 m_include_dirs.push_back (include_dir);
debd256d 20543}
6e70227d 20544
fff8551c
PA
20545void
20546line_header::add_file_name (const char *name,
ecfb656c 20547 dir_index d_index,
fff8551c
PA
20548 unsigned int mod_time,
20549 unsigned int length)
debd256d 20550{
27e0867f 20551 if (dwarf_line_debug >= 2)
7ba99d21
AT
20552 {
20553 size_t new_size;
20554 if (version >= 5)
20555 new_size = file_names_size ();
20556 else
20557 new_size = file_names_size () + 1;
20558 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20559 new_size, name);
20560 }
20561 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20562}
6e70227d 20563
83769d0b 20564/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20565
20566static struct dwarf2_section_info *
20567get_debug_line_section (struct dwarf2_cu *cu)
20568{
20569 struct dwarf2_section_info *section;
518817b3
SM
20570 struct dwarf2_per_objfile *dwarf2_per_objfile
20571 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20572
20573 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20574 DWO file. */
20575 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20576 section = &cu->dwo_unit->dwo_file->sections.line;
20577 else if (cu->per_cu->is_dwz)
20578 {
ed2dc618 20579 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20580
20581 section = &dwz->line;
20582 }
20583 else
20584 section = &dwarf2_per_objfile->line;
20585
20586 return section;
20587}
20588
43988095
JK
20589/* Read directory or file name entry format, starting with byte of
20590 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20591 entries count and the entries themselves in the described entry
20592 format. */
20593
20594static void
ed2dc618
SM
20595read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20596 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20597 struct line_header *lh,
20598 const struct comp_unit_head *cu_header,
20599 void (*callback) (struct line_header *lh,
20600 const char *name,
ecfb656c 20601 dir_index d_index,
43988095
JK
20602 unsigned int mod_time,
20603 unsigned int length))
20604{
20605 gdb_byte format_count, formati;
20606 ULONGEST data_count, datai;
20607 const gdb_byte *buf = *bufp;
20608 const gdb_byte *format_header_data;
43988095
JK
20609 unsigned int bytes_read;
20610
20611 format_count = read_1_byte (abfd, buf);
20612 buf += 1;
20613 format_header_data = buf;
20614 for (formati = 0; formati < format_count; formati++)
20615 {
20616 read_unsigned_leb128 (abfd, buf, &bytes_read);
20617 buf += bytes_read;
20618 read_unsigned_leb128 (abfd, buf, &bytes_read);
20619 buf += bytes_read;
20620 }
20621
20622 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20623 buf += bytes_read;
20624 for (datai = 0; datai < data_count; datai++)
20625 {
20626 const gdb_byte *format = format_header_data;
20627 struct file_entry fe;
20628
43988095
JK
20629 for (formati = 0; formati < format_count; formati++)
20630 {
ecfb656c 20631 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20632 format += bytes_read;
43988095 20633
ecfb656c 20634 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20635 format += bytes_read;
ecfb656c
PA
20636
20637 gdb::optional<const char *> string;
20638 gdb::optional<unsigned int> uint;
20639
43988095
JK
20640 switch (form)
20641 {
20642 case DW_FORM_string:
ecfb656c 20643 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20644 buf += bytes_read;
20645 break;
20646
20647 case DW_FORM_line_strp:
ed2dc618
SM
20648 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20649 abfd, buf,
ecfb656c
PA
20650 cu_header,
20651 &bytes_read));
43988095
JK
20652 buf += bytes_read;
20653 break;
20654
20655 case DW_FORM_data1:
ecfb656c 20656 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20657 buf += 1;
20658 break;
20659
20660 case DW_FORM_data2:
ecfb656c 20661 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20662 buf += 2;
20663 break;
20664
20665 case DW_FORM_data4:
ecfb656c 20666 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20667 buf += 4;
20668 break;
20669
20670 case DW_FORM_data8:
ecfb656c 20671 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20672 buf += 8;
20673 break;
20674
7ba99d21
AT
20675 case DW_FORM_data16:
20676 /* This is used for MD5, but file_entry does not record MD5s. */
20677 buf += 16;
20678 break;
20679
43988095 20680 case DW_FORM_udata:
ecfb656c 20681 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20682 buf += bytes_read;
20683 break;
20684
20685 case DW_FORM_block:
20686 /* It is valid only for DW_LNCT_timestamp which is ignored by
20687 current GDB. */
20688 break;
20689 }
ecfb656c
PA
20690
20691 switch (content_type)
20692 {
20693 case DW_LNCT_path:
20694 if (string.has_value ())
20695 fe.name = *string;
20696 break;
20697 case DW_LNCT_directory_index:
20698 if (uint.has_value ())
20699 fe.d_index = (dir_index) *uint;
20700 break;
20701 case DW_LNCT_timestamp:
20702 if (uint.has_value ())
20703 fe.mod_time = *uint;
20704 break;
20705 case DW_LNCT_size:
20706 if (uint.has_value ())
20707 fe.length = *uint;
20708 break;
20709 case DW_LNCT_MD5:
20710 break;
20711 default:
b98664d3 20712 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20713 pulongest (content_type));
20714 }
43988095
JK
20715 }
20716
ecfb656c 20717 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20718 }
20719
20720 *bufp = buf;
20721}
20722
debd256d 20723/* Read the statement program header starting at OFFSET in
3019eac3 20724 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20725 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20726 Returns NULL if there is a problem reading the header, e.g., if it
20727 has a version we don't understand.
debd256d
JB
20728
20729 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20730 the returned object point into the dwarf line section buffer,
20731 and must not be freed. */
ae2de4f8 20732
fff8551c 20733static line_header_up
9c541725 20734dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20735{
d521ce57 20736 const gdb_byte *line_ptr;
c764a876 20737 unsigned int bytes_read, offset_size;
debd256d 20738 int i;
d521ce57 20739 const char *cur_dir, *cur_file;
3019eac3
DE
20740 struct dwarf2_section_info *section;
20741 bfd *abfd;
518817b3
SM
20742 struct dwarf2_per_objfile *dwarf2_per_objfile
20743 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20744
36586728 20745 section = get_debug_line_section (cu);
3019eac3
DE
20746 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20747 if (section->buffer == NULL)
debd256d 20748 {
3019eac3 20749 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20750 complaint (_("missing .debug_line.dwo section"));
3019eac3 20751 else
b98664d3 20752 complaint (_("missing .debug_line section"));
debd256d
JB
20753 return 0;
20754 }
20755
fceca515
DE
20756 /* We can't do this until we know the section is non-empty.
20757 Only then do we know we have such a section. */
a32a8923 20758 abfd = get_section_bfd_owner (section);
fceca515 20759
a738430d
MK
20760 /* Make sure that at least there's room for the total_length field.
20761 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20762 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20763 {
4d3c2250 20764 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20765 return 0;
20766 }
20767
fff8551c 20768 line_header_up lh (new line_header ());
debd256d 20769
9c541725 20770 lh->sect_off = sect_off;
527f3840
JK
20771 lh->offset_in_dwz = cu->per_cu->is_dwz;
20772
9c541725 20773 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20774
a738430d 20775 /* Read in the header. */
6e70227d 20776 lh->total_length =
c764a876
DE
20777 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20778 &bytes_read, &offset_size);
debd256d 20779 line_ptr += bytes_read;
7ba99d21
AT
20780
20781 const gdb_byte *start_here = line_ptr;
20782
3019eac3 20783 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20784 {
4d3c2250 20785 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20786 return 0;
20787 }
7ba99d21 20788 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20789 lh->version = read_2_bytes (abfd, line_ptr);
20790 line_ptr += 2;
43988095 20791 if (lh->version > 5)
cd366ee8
DE
20792 {
20793 /* This is a version we don't understand. The format could have
20794 changed in ways we don't handle properly so just punt. */
b98664d3 20795 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20796 return NULL;
20797 }
43988095
JK
20798 if (lh->version >= 5)
20799 {
20800 gdb_byte segment_selector_size;
20801
20802 /* Skip address size. */
20803 read_1_byte (abfd, line_ptr);
20804 line_ptr += 1;
20805
20806 segment_selector_size = read_1_byte (abfd, line_ptr);
20807 line_ptr += 1;
20808 if (segment_selector_size != 0)
20809 {
b98664d3 20810 complaint (_("unsupported segment selector size %u "
43988095
JK
20811 "in .debug_line section"),
20812 segment_selector_size);
20813 return NULL;
20814 }
20815 }
c764a876
DE
20816 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20817 line_ptr += offset_size;
7ba99d21 20818 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20819 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20820 line_ptr += 1;
2dc7f7b3
TT
20821 if (lh->version >= 4)
20822 {
20823 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20824 line_ptr += 1;
20825 }
20826 else
20827 lh->maximum_ops_per_instruction = 1;
20828
20829 if (lh->maximum_ops_per_instruction == 0)
20830 {
20831 lh->maximum_ops_per_instruction = 1;
b98664d3 20832 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20833 "in `.debug_line' section"));
2dc7f7b3
TT
20834 }
20835
debd256d
JB
20836 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20837 line_ptr += 1;
20838 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20839 line_ptr += 1;
20840 lh->line_range = read_1_byte (abfd, line_ptr);
20841 line_ptr += 1;
20842 lh->opcode_base = read_1_byte (abfd, line_ptr);
20843 line_ptr += 1;
fff8551c 20844 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20845
20846 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20847 for (i = 1; i < lh->opcode_base; ++i)
20848 {
20849 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20850 line_ptr += 1;
20851 }
20852
43988095 20853 if (lh->version >= 5)
debd256d 20854 {
43988095 20855 /* Read directory table. */
ed2dc618
SM
20856 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20857 &cu->header,
b926417a 20858 [] (struct line_header *header, const char *name,
ecfb656c 20859 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20860 unsigned int length)
20861 {
b926417a 20862 header->add_include_dir (name);
fff8551c 20863 });
debd256d 20864
43988095 20865 /* Read file name table. */
ed2dc618
SM
20866 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20867 &cu->header,
b926417a 20868 [] (struct line_header *header, const char *name,
ecfb656c 20869 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20870 unsigned int length)
20871 {
b926417a 20872 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20873 });
43988095
JK
20874 }
20875 else
debd256d 20876 {
43988095
JK
20877 /* Read directory table. */
20878 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20879 {
20880 line_ptr += bytes_read;
fff8551c 20881 lh->add_include_dir (cur_dir);
43988095 20882 }
debd256d
JB
20883 line_ptr += bytes_read;
20884
43988095
JK
20885 /* Read file name table. */
20886 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20887 {
ecfb656c
PA
20888 unsigned int mod_time, length;
20889 dir_index d_index;
43988095
JK
20890
20891 line_ptr += bytes_read;
ecfb656c 20892 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20893 line_ptr += bytes_read;
20894 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20895 line_ptr += bytes_read;
20896 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20897 line_ptr += bytes_read;
20898
ecfb656c 20899 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20900 }
20901 line_ptr += bytes_read;
debd256d 20902 }
debd256d 20903
3019eac3 20904 if (line_ptr > (section->buffer + section->size))
b98664d3 20905 complaint (_("line number info header doesn't "
3e43a32a 20906 "fit in `.debug_line' section"));
debd256d 20907
debd256d
JB
20908 return lh;
20909}
c906108c 20910
c6da4cef 20911/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20912 Return the file name of the psymtab for the given file_entry.
c6da4cef 20913 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20914 If space for the result is malloc'd, *NAME_HOLDER will be set.
20915 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20916
d521ce57 20917static const char *
7ba99d21 20918psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
c6da4cef 20919 const struct partial_symtab *pst,
c89b44cd
TT
20920 const char *comp_dir,
20921 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20922{
d521ce57
TT
20923 const char *include_name = fe.name;
20924 const char *include_name_to_compare = include_name;
72b9f47f 20925 const char *pst_filename;
c6da4cef
DE
20926 int file_is_pst;
20927
8c43009f 20928 const char *dir_name = fe.include_dir (lh);
c6da4cef 20929
c89b44cd 20930 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20931 if (!IS_ABSOLUTE_PATH (include_name)
20932 && (dir_name != NULL || comp_dir != NULL))
20933 {
20934 /* Avoid creating a duplicate psymtab for PST.
20935 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20936 Before we do the comparison, however, we need to account
20937 for DIR_NAME and COMP_DIR.
20938 First prepend dir_name (if non-NULL). If we still don't
20939 have an absolute path prepend comp_dir (if non-NULL).
20940 However, the directory we record in the include-file's
20941 psymtab does not contain COMP_DIR (to match the
20942 corresponding symtab(s)).
20943
20944 Example:
20945
20946 bash$ cd /tmp
20947 bash$ gcc -g ./hello.c
20948 include_name = "hello.c"
20949 dir_name = "."
20950 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20951 DW_AT_name = "./hello.c"
20952
20953 */
c6da4cef
DE
20954
20955 if (dir_name != NULL)
20956 {
c89b44cd
TT
20957 name_holder->reset (concat (dir_name, SLASH_STRING,
20958 include_name, (char *) NULL));
20959 include_name = name_holder->get ();
c6da4cef 20960 include_name_to_compare = include_name;
c6da4cef
DE
20961 }
20962 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20963 {
c89b44cd
TT
20964 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20965 include_name, (char *) NULL));
20966 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20967 }
20968 }
20969
20970 pst_filename = pst->filename;
c89b44cd 20971 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20972 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20973 {
c89b44cd
TT
20974 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20975 pst_filename, (char *) NULL));
20976 pst_filename = copied_name.get ();
c6da4cef
DE
20977 }
20978
1e3fad37 20979 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20980
c6da4cef
DE
20981 if (file_is_pst)
20982 return NULL;
20983 return include_name;
20984}
20985
d9b3de22
DE
20986/* State machine to track the state of the line number program. */
20987
6f77053d 20988class lnp_state_machine
d9b3de22 20989{
6f77053d
PA
20990public:
20991 /* Initialize a machine state for the start of a line number
20992 program. */
804d2729
TT
20993 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20994 bool record_lines_p);
6f77053d 20995
8c43009f
PA
20996 file_entry *current_file ()
20997 {
20998 /* lh->file_names is 0-based, but the file name numbers in the
20999 statement program are 1-based. */
6f77053d
PA
21000 return m_line_header->file_name_at (m_file);
21001 }
21002
21003 /* Record the line in the state machine. END_SEQUENCE is true if
21004 we're processing the end of a sequence. */
21005 void record_line (bool end_sequence);
21006
7ab6656f
OJ
21007 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
21008 nop-out rest of the lines in this sequence. */
6f77053d
PA
21009 void check_line_address (struct dwarf2_cu *cu,
21010 const gdb_byte *line_ptr,
7ab6656f 21011 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21012
21013 void handle_set_discriminator (unsigned int discriminator)
21014 {
21015 m_discriminator = discriminator;
21016 m_line_has_non_zero_discriminator |= discriminator != 0;
21017 }
21018
21019 /* Handle DW_LNE_set_address. */
21020 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21021 {
21022 m_op_index = 0;
21023 address += baseaddr;
21024 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21025 }
21026
21027 /* Handle DW_LNS_advance_pc. */
21028 void handle_advance_pc (CORE_ADDR adjust);
21029
21030 /* Handle a special opcode. */
21031 void handle_special_opcode (unsigned char op_code);
21032
21033 /* Handle DW_LNS_advance_line. */
21034 void handle_advance_line (int line_delta)
21035 {
21036 advance_line (line_delta);
21037 }
21038
21039 /* Handle DW_LNS_set_file. */
21040 void handle_set_file (file_name_index file);
21041
21042 /* Handle DW_LNS_negate_stmt. */
21043 void handle_negate_stmt ()
21044 {
21045 m_is_stmt = !m_is_stmt;
21046 }
21047
21048 /* Handle DW_LNS_const_add_pc. */
21049 void handle_const_add_pc ();
21050
21051 /* Handle DW_LNS_fixed_advance_pc. */
21052 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21053 {
21054 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21055 m_op_index = 0;
21056 }
21057
21058 /* Handle DW_LNS_copy. */
21059 void handle_copy ()
21060 {
21061 record_line (false);
21062 m_discriminator = 0;
21063 }
21064
21065 /* Handle DW_LNE_end_sequence. */
21066 void handle_end_sequence ()
21067 {
804d2729 21068 m_currently_recording_lines = true;
6f77053d
PA
21069 }
21070
21071private:
21072 /* Advance the line by LINE_DELTA. */
21073 void advance_line (int line_delta)
21074 {
21075 m_line += line_delta;
21076
21077 if (line_delta != 0)
21078 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21079 }
21080
804d2729
TT
21081 struct dwarf2_cu *m_cu;
21082
6f77053d
PA
21083 gdbarch *m_gdbarch;
21084
21085 /* True if we're recording lines.
21086 Otherwise we're building partial symtabs and are just interested in
21087 finding include files mentioned by the line number program. */
21088 bool m_record_lines_p;
21089
8c43009f 21090 /* The line number header. */
6f77053d 21091 line_header *m_line_header;
8c43009f 21092
6f77053d
PA
21093 /* These are part of the standard DWARF line number state machine,
21094 and initialized according to the DWARF spec. */
d9b3de22 21095
6f77053d 21096 unsigned char m_op_index = 0;
7ba99d21
AT
21097 /* The line table index of the current file. */
21098 file_name_index m_file = 1;
6f77053d
PA
21099 unsigned int m_line = 1;
21100
21101 /* These are initialized in the constructor. */
21102
21103 CORE_ADDR m_address;
21104 bool m_is_stmt;
21105 unsigned int m_discriminator;
d9b3de22
DE
21106
21107 /* Additional bits of state we need to track. */
21108
21109 /* The last file that we called dwarf2_start_subfile for.
21110 This is only used for TLLs. */
6f77053d 21111 unsigned int m_last_file = 0;
d9b3de22 21112 /* The last file a line number was recorded for. */
6f77053d 21113 struct subfile *m_last_subfile = NULL;
d9b3de22 21114
804d2729
TT
21115 /* When true, record the lines we decode. */
21116 bool m_currently_recording_lines = false;
d9b3de22
DE
21117
21118 /* The last line number that was recorded, used to coalesce
21119 consecutive entries for the same line. This can happen, for
21120 example, when discriminators are present. PR 17276. */
6f77053d
PA
21121 unsigned int m_last_line = 0;
21122 bool m_line_has_non_zero_discriminator = false;
8c43009f 21123};
d9b3de22 21124
6f77053d
PA
21125void
21126lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21127{
21128 CORE_ADDR addr_adj = (((m_op_index + adjust)
21129 / m_line_header->maximum_ops_per_instruction)
21130 * m_line_header->minimum_instruction_length);
21131 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21132 m_op_index = ((m_op_index + adjust)
21133 % m_line_header->maximum_ops_per_instruction);
21134}
d9b3de22 21135
6f77053d
PA
21136void
21137lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21138{
6f77053d
PA
21139 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21140 CORE_ADDR addr_adj = (((m_op_index
21141 + (adj_opcode / m_line_header->line_range))
21142 / m_line_header->maximum_ops_per_instruction)
21143 * m_line_header->minimum_instruction_length);
21144 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21145 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21146 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21147
6f77053d
PA
21148 int line_delta = (m_line_header->line_base
21149 + (adj_opcode % m_line_header->line_range));
21150 advance_line (line_delta);
21151 record_line (false);
21152 m_discriminator = 0;
21153}
d9b3de22 21154
6f77053d
PA
21155void
21156lnp_state_machine::handle_set_file (file_name_index file)
21157{
21158 m_file = file;
21159
21160 const file_entry *fe = current_file ();
21161 if (fe == NULL)
21162 dwarf2_debug_line_missing_file_complaint ();
21163 else if (m_record_lines_p)
21164 {
21165 const char *dir = fe->include_dir (m_line_header);
21166
c24bdb02 21167 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21168 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21169 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21170 }
21171}
21172
21173void
21174lnp_state_machine::handle_const_add_pc ()
21175{
21176 CORE_ADDR adjust
21177 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21178
21179 CORE_ADDR addr_adj
21180 = (((m_op_index + adjust)
21181 / m_line_header->maximum_ops_per_instruction)
21182 * m_line_header->minimum_instruction_length);
21183
21184 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21185 m_op_index = ((m_op_index + adjust)
21186 % m_line_header->maximum_ops_per_instruction);
21187}
d9b3de22 21188
a05a36a5
DE
21189/* Return non-zero if we should add LINE to the line number table.
21190 LINE is the line to add, LAST_LINE is the last line that was added,
21191 LAST_SUBFILE is the subfile for LAST_LINE.
21192 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21193 had a non-zero discriminator.
21194
21195 We have to be careful in the presence of discriminators.
21196 E.g., for this line:
21197
21198 for (i = 0; i < 100000; i++);
21199
21200 clang can emit four line number entries for that one line,
21201 each with a different discriminator.
21202 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21203
21204 However, we want gdb to coalesce all four entries into one.
21205 Otherwise the user could stepi into the middle of the line and
21206 gdb would get confused about whether the pc really was in the
21207 middle of the line.
21208
21209 Things are further complicated by the fact that two consecutive
21210 line number entries for the same line is a heuristic used by gcc
21211 to denote the end of the prologue. So we can't just discard duplicate
21212 entries, we have to be selective about it. The heuristic we use is
21213 that we only collapse consecutive entries for the same line if at least
21214 one of those entries has a non-zero discriminator. PR 17276.
21215
21216 Note: Addresses in the line number state machine can never go backwards
21217 within one sequence, thus this coalescing is ok. */
21218
21219static int
804d2729
TT
21220dwarf_record_line_p (struct dwarf2_cu *cu,
21221 unsigned int line, unsigned int last_line,
a05a36a5
DE
21222 int line_has_non_zero_discriminator,
21223 struct subfile *last_subfile)
21224{
c24bdb02 21225 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21226 return 1;
21227 if (line != last_line)
21228 return 1;
21229 /* Same line for the same file that we've seen already.
21230 As a last check, for pr 17276, only record the line if the line
21231 has never had a non-zero discriminator. */
21232 if (!line_has_non_zero_discriminator)
21233 return 1;
21234 return 0;
21235}
21236
804d2729
TT
21237/* Use the CU's builder to record line number LINE beginning at
21238 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21239
21240static void
d9b3de22
DE
21241dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21242 unsigned int line, CORE_ADDR address,
804d2729 21243 struct dwarf2_cu *cu)
252a6764
DE
21244{
21245 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21246
27e0867f
DE
21247 if (dwarf_line_debug)
21248 {
21249 fprintf_unfiltered (gdb_stdlog,
21250 "Recording line %u, file %s, address %s\n",
21251 line, lbasename (subfile->name),
21252 paddress (gdbarch, address));
21253 }
21254
804d2729 21255 if (cu != nullptr)
c24bdb02 21256 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21257}
21258
21259/* Subroutine of dwarf_decode_lines_1 to simplify it.
21260 Mark the end of a set of line number records.
d9b3de22 21261 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21262 If SUBFILE is NULL the request is ignored. */
21263
21264static void
21265dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21266 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21267{
27e0867f
DE
21268 if (subfile == NULL)
21269 return;
21270
21271 if (dwarf_line_debug)
21272 {
21273 fprintf_unfiltered (gdb_stdlog,
21274 "Finishing current line, file %s, address %s\n",
21275 lbasename (subfile->name),
21276 paddress (gdbarch, address));
21277 }
21278
804d2729 21279 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21280}
21281
6f77053d
PA
21282void
21283lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21284{
d9b3de22
DE
21285 if (dwarf_line_debug)
21286 {
21287 fprintf_unfiltered (gdb_stdlog,
21288 "Processing actual line %u: file %u,"
21289 " address %s, is_stmt %u, discrim %u\n",
7ba99d21 21290 m_line, m_file,
6f77053d
PA
21291 paddress (m_gdbarch, m_address),
21292 m_is_stmt, m_discriminator);
d9b3de22
DE
21293 }
21294
6f77053d 21295 file_entry *fe = current_file ();
8c43009f
PA
21296
21297 if (fe == NULL)
d9b3de22
DE
21298 dwarf2_debug_line_missing_file_complaint ();
21299 /* For now we ignore lines not starting on an instruction boundary.
21300 But not when processing end_sequence for compatibility with the
21301 previous version of the code. */
6f77053d 21302 else if (m_op_index == 0 || end_sequence)
d9b3de22 21303 {
8c43009f 21304 fe->included_p = 1;
c258c396 21305 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21306 {
c24bdb02 21307 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21308 || end_sequence)
d9b3de22 21309 {
804d2729
TT
21310 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21311 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21312 }
21313
21314 if (!end_sequence)
21315 {
804d2729 21316 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21317 m_line_has_non_zero_discriminator,
21318 m_last_subfile))
d9b3de22 21319 {
c24bdb02 21320 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21321 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21322 builder->get_current_subfile (),
6f77053d 21323 m_line, m_address,
804d2729 21324 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21325 }
c24bdb02 21326 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21327 m_last_line = m_line;
d9b3de22
DE
21328 }
21329 }
21330 }
21331}
21332
804d2729
TT
21333lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21334 line_header *lh, bool record_lines_p)
d9b3de22 21335{
804d2729 21336 m_cu = cu;
6f77053d
PA
21337 m_gdbarch = arch;
21338 m_record_lines_p = record_lines_p;
21339 m_line_header = lh;
d9b3de22 21340
804d2729 21341 m_currently_recording_lines = true;
d9b3de22 21342
d9b3de22
DE
21343 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21344 was a line entry for it so that the backend has a chance to adjust it
21345 and also record it in case it needs it. This is currently used by MIPS
21346 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21347 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21348 m_is_stmt = lh->default_is_stmt;
21349 m_discriminator = 0;
252a6764
DE
21350}
21351
6f77053d
PA
21352void
21353lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21354 const gdb_byte *line_ptr,
7ab6656f 21355 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21356{
7ab6656f
OJ
21357 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21358 the pc range of the CU. However, we restrict the test to only ADDRESS
21359 values of zero to preserve GDB's previous behaviour which is to handle
21360 the specific case of a function being GC'd by the linker. */
924c2928 21361
7ab6656f 21362 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21363 {
21364 /* This line table is for a function which has been
21365 GCd by the linker. Ignore it. PR gdb/12528 */
21366
518817b3 21367 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21368 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21369
b98664d3 21370 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21371 line_offset, objfile_name (objfile));
804d2729
TT
21372 m_currently_recording_lines = false;
21373 /* Note: m_currently_recording_lines is left as false until we see
21374 DW_LNE_end_sequence. */
924c2928
DE
21375 }
21376}
21377
f3f5162e 21378/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21379 Process the line number information in LH.
21380 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21381 program in order to set included_p for every referenced header. */
debd256d 21382
c906108c 21383static void
43f3e411
DE
21384dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21385 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21386{
d521ce57
TT
21387 const gdb_byte *line_ptr, *extended_end;
21388 const gdb_byte *line_end;
a8c50c1f 21389 unsigned int bytes_read, extended_len;
699ca60a 21390 unsigned char op_code, extended_op;
e142c38c 21391 CORE_ADDR baseaddr;
518817b3 21392 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21393 bfd *abfd = objfile->obfd;
fbf65064 21394 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21395 /* True if we're recording line info (as opposed to building partial
21396 symtabs and just interested in finding include files mentioned by
21397 the line number program). */
21398 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21399
21400 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21401
debd256d
JB
21402 line_ptr = lh->statement_program_start;
21403 line_end = lh->statement_program_end;
c906108c
SS
21404
21405 /* Read the statement sequences until there's nothing left. */
21406 while (line_ptr < line_end)
21407 {
6f77053d
PA
21408 /* The DWARF line number program state machine. Reset the state
21409 machine at the start of each sequence. */
804d2729 21410 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21411 bool end_sequence = false;
d9b3de22 21412
8c43009f 21413 if (record_lines_p)
c906108c 21414 {
8c43009f
PA
21415 /* Start a subfile for the current file of the state
21416 machine. */
21417 const file_entry *fe = state_machine.current_file ();
21418
21419 if (fe != NULL)
804d2729 21420 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21421 }
21422
a738430d 21423 /* Decode the table. */
d9b3de22 21424 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21425 {
21426 op_code = read_1_byte (abfd, line_ptr);
21427 line_ptr += 1;
9aa1fe7e 21428
debd256d 21429 if (op_code >= lh->opcode_base)
6e70227d 21430 {
8e07a239 21431 /* Special opcode. */
6f77053d 21432 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21433 }
21434 else switch (op_code)
c906108c
SS
21435 {
21436 case DW_LNS_extended_op:
3e43a32a
MS
21437 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21438 &bytes_read);
473b7be6 21439 line_ptr += bytes_read;
a8c50c1f 21440 extended_end = line_ptr + extended_len;
c906108c
SS
21441 extended_op = read_1_byte (abfd, line_ptr);
21442 line_ptr += 1;
21443 switch (extended_op)
21444 {
21445 case DW_LNE_end_sequence:
6f77053d
PA
21446 state_machine.handle_end_sequence ();
21447 end_sequence = true;
c906108c
SS
21448 break;
21449 case DW_LNE_set_address:
d9b3de22
DE
21450 {
21451 CORE_ADDR address
21452 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21453 line_ptr += bytes_read;
6f77053d
PA
21454
21455 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21456 lowpc - baseaddr, address);
6f77053d 21457 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21458 }
c906108c
SS
21459 break;
21460 case DW_LNE_define_file:
debd256d 21461 {
d521ce57 21462 const char *cur_file;
ecfb656c
PA
21463 unsigned int mod_time, length;
21464 dir_index dindex;
6e70227d 21465
3e43a32a
MS
21466 cur_file = read_direct_string (abfd, line_ptr,
21467 &bytes_read);
debd256d 21468 line_ptr += bytes_read;
ecfb656c 21469 dindex = (dir_index)
debd256d
JB
21470 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21471 line_ptr += bytes_read;
21472 mod_time =
21473 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21474 line_ptr += bytes_read;
21475 length =
21476 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21477 line_ptr += bytes_read;
ecfb656c 21478 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21479 }
c906108c 21480 break;
d0c6ba3d 21481 case DW_LNE_set_discriminator:
6f77053d
PA
21482 {
21483 /* The discriminator is not interesting to the
21484 debugger; just ignore it. We still need to
21485 check its value though:
21486 if there are consecutive entries for the same
21487 (non-prologue) line we want to coalesce them.
21488 PR 17276. */
21489 unsigned int discr
21490 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21491 line_ptr += bytes_read;
21492
21493 state_machine.handle_set_discriminator (discr);
21494 }
d0c6ba3d 21495 break;
c906108c 21496 default:
b98664d3 21497 complaint (_("mangled .debug_line section"));
debd256d 21498 return;
c906108c 21499 }
a8c50c1f
DJ
21500 /* Make sure that we parsed the extended op correctly. If e.g.
21501 we expected a different address size than the producer used,
21502 we may have read the wrong number of bytes. */
21503 if (line_ptr != extended_end)
21504 {
b98664d3 21505 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21506 return;
21507 }
c906108c
SS
21508 break;
21509 case DW_LNS_copy:
6f77053d 21510 state_machine.handle_copy ();
c906108c
SS
21511 break;
21512 case DW_LNS_advance_pc:
2dc7f7b3
TT
21513 {
21514 CORE_ADDR adjust
21515 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21516 line_ptr += bytes_read;
6f77053d
PA
21517
21518 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21519 }
c906108c
SS
21520 break;
21521 case DW_LNS_advance_line:
a05a36a5
DE
21522 {
21523 int line_delta
21524 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21525 line_ptr += bytes_read;
6f77053d
PA
21526
21527 state_machine.handle_advance_line (line_delta);
a05a36a5 21528 }
c906108c
SS
21529 break;
21530 case DW_LNS_set_file:
d9b3de22 21531 {
6f77053d 21532 file_name_index file
ecfb656c
PA
21533 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21534 &bytes_read);
d9b3de22 21535 line_ptr += bytes_read;
8c43009f 21536
6f77053d 21537 state_machine.handle_set_file (file);
d9b3de22 21538 }
c906108c
SS
21539 break;
21540 case DW_LNS_set_column:
0ad93d4f 21541 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21542 line_ptr += bytes_read;
21543 break;
21544 case DW_LNS_negate_stmt:
6f77053d 21545 state_machine.handle_negate_stmt ();
c906108c
SS
21546 break;
21547 case DW_LNS_set_basic_block:
c906108c 21548 break;
c2c6d25f
JM
21549 /* Add to the address register of the state machine the
21550 address increment value corresponding to special opcode
a738430d
MK
21551 255. I.e., this value is scaled by the minimum
21552 instruction length since special opcode 255 would have
b021a221 21553 scaled the increment. */
c906108c 21554 case DW_LNS_const_add_pc:
6f77053d 21555 state_machine.handle_const_add_pc ();
c906108c
SS
21556 break;
21557 case DW_LNS_fixed_advance_pc:
3e29f34a 21558 {
6f77053d 21559 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21560 line_ptr += 2;
6f77053d
PA
21561
21562 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21563 }
c906108c 21564 break;
9aa1fe7e 21565 default:
a738430d
MK
21566 {
21567 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21568 int i;
a738430d 21569
debd256d 21570 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21571 {
21572 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21573 line_ptr += bytes_read;
21574 }
21575 }
c906108c
SS
21576 }
21577 }
d9b3de22
DE
21578
21579 if (!end_sequence)
21580 dwarf2_debug_line_missing_end_sequence_complaint ();
21581
21582 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21583 in which case we still finish recording the last line). */
6f77053d 21584 state_machine.record_line (true);
c906108c 21585 }
f3f5162e
DE
21586}
21587
21588/* Decode the Line Number Program (LNP) for the given line_header
21589 structure and CU. The actual information extracted and the type
21590 of structures created from the LNP depends on the value of PST.
21591
21592 1. If PST is NULL, then this procedure uses the data from the program
21593 to create all necessary symbol tables, and their linetables.
21594
21595 2. If PST is not NULL, this procedure reads the program to determine
21596 the list of files included by the unit represented by PST, and
21597 builds all the associated partial symbol tables.
21598
21599 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21600 It is used for relative paths in the line table.
21601 NOTE: When processing partial symtabs (pst != NULL),
21602 comp_dir == pst->dirname.
21603
21604 NOTE: It is important that psymtabs have the same file name (via strcmp)
21605 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21606 symtab we don't use it in the name of the psymtabs we create.
21607 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21608 A good testcase for this is mb-inline.exp.
21609
527f3840
JK
21610 LOWPC is the lowest address in CU (or 0 if not known).
21611
21612 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21613 for its PC<->lines mapping information. Otherwise only the filename
21614 table is read in. */
f3f5162e
DE
21615
21616static void
21617dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21618 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21619 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21620{
518817b3 21621 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21622 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21623
527f3840
JK
21624 if (decode_mapping)
21625 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21626
21627 if (decode_for_pst_p)
21628 {
aaa75496
JB
21629 /* Now that we're done scanning the Line Header Program, we can
21630 create the psymtab of each included file. */
7ba99d21
AT
21631 for (auto &file_entry : lh->file_names ())
21632 if (file_entry.included_p == 1)
aaa75496 21633 {
c89b44cd 21634 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21635 const char *include_name =
7ba99d21
AT
21636 psymtab_include_file_name (lh, file_entry, pst,
21637 comp_dir, &name_holder);
c6da4cef 21638 if (include_name != NULL)
aaa75496
JB
21639 dwarf2_create_include_psymtab (include_name, pst, objfile);
21640 }
21641 }
cb1df416
DJ
21642 else
21643 {
21644 /* Make sure a symtab is created for every file, even files
21645 which contain only variables (i.e. no code with associated
21646 line numbers). */
c24bdb02
KS
21647 buildsym_compunit *builder = cu->get_builder ();
21648 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21649
7ba99d21 21650 for (auto &fe : lh->file_names ())
cb1df416 21651 {
804d2729 21652 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21653 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21654 {
c24bdb02 21655 builder->get_current_subfile ()->symtab
804d2729 21656 = allocate_symtab (cust,
c24bdb02 21657 builder->get_current_subfile ()->name);
43f3e411 21658 }
c24bdb02 21659 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21660 }
21661 }
c906108c
SS
21662}
21663
21664/* Start a subfile for DWARF. FILENAME is the name of the file and
21665 DIRNAME the name of the source directory which contains FILENAME
4d663531 21666 or NULL if not known.
c906108c
SS
21667 This routine tries to keep line numbers from identical absolute and
21668 relative file names in a common subfile.
21669
21670 Using the `list' example from the GDB testsuite, which resides in
21671 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21672 of /srcdir/list0.c yields the following debugging information for list0.c:
21673
c5aa993b 21674 DW_AT_name: /srcdir/list0.c
4d663531 21675 DW_AT_comp_dir: /compdir
357e46e7 21676 files.files[0].name: list0.h
c5aa993b 21677 files.files[0].dir: /srcdir
357e46e7 21678 files.files[1].name: list0.c
c5aa993b 21679 files.files[1].dir: /srcdir
c906108c
SS
21680
21681 The line number information for list0.c has to end up in a single
4f1520fb
FR
21682 subfile, so that `break /srcdir/list0.c:1' works as expected.
21683 start_subfile will ensure that this happens provided that we pass the
21684 concatenation of files.files[1].dir and files.files[1].name as the
21685 subfile's name. */
c906108c
SS
21686
21687static void
804d2729
TT
21688dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21689 const char *dirname)
c906108c 21690{
43816ebc 21691 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21692
4d663531 21693 /* In order not to lose the line information directory,
4f1520fb
FR
21694 we concatenate it to the filename when it makes sense.
21695 Note that the Dwarf3 standard says (speaking of filenames in line
21696 information): ``The directory index is ignored for file names
21697 that represent full path names''. Thus ignoring dirname in the
21698 `else' branch below isn't an issue. */
c906108c 21699
d5166ae1 21700 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21701 {
43816ebc
TT
21702 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21703 filename = copy.get ();
d521ce57 21704 }
c906108c 21705
c24bdb02 21706 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21707}
21708
804d2729
TT
21709/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21710 buildsym_compunit constructor. */
f4dc4d17 21711
c24bdb02
KS
21712struct compunit_symtab *
21713dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21714 CORE_ADDR low_pc)
f4dc4d17 21715{
c24bdb02 21716 gdb_assert (m_builder == nullptr);
43f3e411 21717
c24bdb02
KS
21718 m_builder.reset (new struct buildsym_compunit
21719 (per_cu->dwarf2_per_objfile->objfile,
21720 name, comp_dir, language, low_pc));
93b8bea4 21721
c24bdb02 21722 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21723
c24bdb02
KS
21724 get_builder ()->record_debugformat ("DWARF 2");
21725 get_builder ()->record_producer (producer);
f4dc4d17 21726
c24bdb02 21727 processing_has_namespace_info = false;
43f3e411 21728
c24bdb02 21729 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21730}
21731
4c2df51b
DJ
21732static void
21733var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21734 struct dwarf2_cu *cu)
4c2df51b 21735{
518817b3 21736 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21737 struct comp_unit_head *cu_header = &cu->header;
21738
4c2df51b
DJ
21739 /* NOTE drow/2003-01-30: There used to be a comment and some special
21740 code here to turn a symbol with DW_AT_external and a
21741 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21742 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21743 with some versions of binutils) where shared libraries could have
21744 relocations against symbols in their debug information - the
21745 minimal symbol would have the right address, but the debug info
21746 would not. It's no longer necessary, because we will explicitly
21747 apply relocations when we read in the debug information now. */
21748
21749 /* A DW_AT_location attribute with no contents indicates that a
21750 variable has been optimized away. */
21751 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21752 {
f1e6e072 21753 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21754 return;
21755 }
21756
21757 /* Handle one degenerate form of location expression specially, to
21758 preserve GDB's previous behavior when section offsets are
336d760d
AT
21759 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21760 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21761
21762 if (attr_form_is_block (attr)
3019eac3
DE
21763 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21764 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21765 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21766 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21767 && (DW_BLOCK (attr)->size
21768 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21769 {
891d2f0b 21770 unsigned int dummy;
4c2df51b 21771
3019eac3 21772 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21773 SET_SYMBOL_VALUE_ADDRESS (sym,
21774 read_address (objfile->obfd,
21775 DW_BLOCK (attr)->data + 1,
21776 cu, &dummy));
3019eac3 21777 else
38583298
TT
21778 SET_SYMBOL_VALUE_ADDRESS
21779 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21780 &dummy));
f1e6e072 21781 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21782 fixup_symbol_section (sym, objfile);
38583298
TT
21783 SET_SYMBOL_VALUE_ADDRESS (sym,
21784 SYMBOL_VALUE_ADDRESS (sym)
21785 + ANOFFSET (objfile->section_offsets,
21786 SYMBOL_SECTION (sym)));
4c2df51b
DJ
21787 return;
21788 }
21789
21790 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21791 expression evaluator, and use LOC_COMPUTED only when necessary
21792 (i.e. when the value of a register or memory location is
21793 referenced, or a thread-local block, etc.). Then again, it might
21794 not be worthwhile. I'm assuming that it isn't unless performance
21795 or memory numbers show me otherwise. */
21796
f1e6e072 21797 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21798
f1e6e072 21799 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21800 cu->has_loclist = true;
4c2df51b
DJ
21801}
21802
c906108c
SS
21803/* Given a pointer to a DWARF information entry, figure out if we need
21804 to make a symbol table entry for it, and if so, create a new entry
21805 and return a pointer to it.
21806 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21807 used the passed type.
21808 If SPACE is not NULL, use it to hold the new symbol. If it is
21809 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21810
21811static struct symbol *
5e2db402
TT
21812new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21813 struct symbol *space)
c906108c 21814{
518817b3
SM
21815 struct dwarf2_per_objfile *dwarf2_per_objfile
21816 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21817 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21818 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21819 struct symbol *sym = NULL;
15d034d0 21820 const char *name;
c906108c
SS
21821 struct attribute *attr = NULL;
21822 struct attribute *attr2 = NULL;
e142c38c 21823 CORE_ADDR baseaddr;
e37fd15a
SW
21824 struct pending **list_to_add = NULL;
21825
edb3359d 21826 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21827
21828 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21829
94af9270 21830 name = dwarf2_name (die, cu);
c906108c
SS
21831 if (name)
21832 {
94af9270 21833 const char *linkagename;
34eaf542 21834 int suppress_add = 0;
94af9270 21835
34eaf542
TT
21836 if (space)
21837 sym = space;
21838 else
e623cf5d 21839 sym = allocate_symbol (objfile);
c906108c 21840 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21841
21842 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21843 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 21844 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 21845 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 21846
f55ee35c
JK
21847 /* Fortran does not have mangling standard and the mangling does differ
21848 between gfortran, iFort etc. */
21849 if (cu->language == language_fortran
468c0cbb
CB
21850 && symbol_get_demangled_name (sym) == NULL)
21851 symbol_set_demangled_name (sym,
cfc594ee 21852 dwarf2_full_name (name, die, cu),
29df156d 21853 NULL);
f55ee35c 21854
c906108c 21855 /* Default assumptions.
c5aa993b 21856 Use the passed type or decode it from the die. */
176620f1 21857 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21858 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21859 if (type != NULL)
21860 SYMBOL_TYPE (sym) = type;
21861 else
e7c27a73 21862 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21863 attr = dwarf2_attr (die,
21864 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21865 cu);
435d3d88 21866 if (attr != nullptr)
c906108c
SS
21867 {
21868 SYMBOL_LINE (sym) = DW_UNSND (attr);
21869 }
cb1df416 21870
edb3359d
DJ
21871 attr = dwarf2_attr (die,
21872 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21873 cu);
435d3d88 21874 if (attr != nullptr)
cb1df416 21875 {
ecfb656c 21876 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21877 struct file_entry *fe;
9a619af0 21878
ecfb656c
PA
21879 if (cu->line_header != NULL)
21880 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21881 else
21882 fe = NULL;
21883
21884 if (fe == NULL)
b98664d3 21885 complaint (_("file index out of range"));
8c43009f
PA
21886 else
21887 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21888 }
21889
c906108c
SS
21890 switch (die->tag)
21891 {
21892 case DW_TAG_label:
e142c38c 21893 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21894 if (attr != nullptr)
3e29f34a
MR
21895 {
21896 CORE_ADDR addr;
21897
21898 addr = attr_value_as_address (attr);
21899 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21900 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21901 }
0f5238ed
TT
21902 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21903 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21904 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21905 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21906 break;
21907 case DW_TAG_subprogram:
21908 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21909 finish_block. */
f1e6e072 21910 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21911 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21912 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21913 || cu->language == language_ada
21914 || cu->language == language_fortran)
c906108c 21915 {
2cfa0c8d 21916 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21917 Ada and Fortran subprograms, whether marked external or
21918 not, are always stored as a global symbol, because we want
21919 to be able to access them globally. For instance, we want
21920 to be able to break on a nested subprogram without having
21921 to specify the context. */
c24bdb02 21922 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21923 }
21924 else
21925 {
e37fd15a 21926 list_to_add = cu->list_in_scope;
c906108c
SS
21927 }
21928 break;
edb3359d
DJ
21929 case DW_TAG_inlined_subroutine:
21930 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21931 finish_block. */
f1e6e072 21932 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21933 SYMBOL_INLINED (sym) = 1;
481860b3 21934 list_to_add = cu->list_in_scope;
edb3359d 21935 break;
34eaf542
TT
21936 case DW_TAG_template_value_param:
21937 suppress_add = 1;
21938 /* Fall through. */
72929c62 21939 case DW_TAG_constant:
c906108c 21940 case DW_TAG_variable:
254e6b9e 21941 case DW_TAG_member:
0963b4bd
MS
21942 /* Compilation with minimal debug info may result in
21943 variables with missing type entries. Change the
21944 misleading `void' type to something sensible. */
c906108c 21945 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21946 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21947
e142c38c 21948 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21949 /* In the case of DW_TAG_member, we should only be called for
21950 static const members. */
21951 if (die->tag == DW_TAG_member)
21952 {
3863f96c
DE
21953 /* dwarf2_add_field uses die_is_declaration,
21954 so we do the same. */
254e6b9e
DE
21955 gdb_assert (die_is_declaration (die, cu));
21956 gdb_assert (attr);
21957 }
435d3d88 21958 if (attr != nullptr)
c906108c 21959 {
e7c27a73 21960 dwarf2_const_value (attr, sym, cu);
e142c38c 21961 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21962 if (!suppress_add)
34eaf542
TT
21963 {
21964 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21965 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21966 else
e37fd15a 21967 list_to_add = cu->list_in_scope;
34eaf542 21968 }
c906108c
SS
21969 break;
21970 }
e142c38c 21971 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21972 if (attr != nullptr)
c906108c 21973 {
e7c27a73 21974 var_decode_location (attr, sym, cu);
e142c38c 21975 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21976
21977 /* Fortran explicitly imports any global symbols to the local
21978 scope by DW_TAG_common_block. */
21979 if (cu->language == language_fortran && die->parent
21980 && die->parent->tag == DW_TAG_common_block)
21981 attr2 = NULL;
21982
caac4577
JG
21983 if (SYMBOL_CLASS (sym) == LOC_STATIC
21984 && SYMBOL_VALUE_ADDRESS (sym) == 0
21985 && !dwarf2_per_objfile->has_section_at_zero)
21986 {
21987 /* When a static variable is eliminated by the linker,
21988 the corresponding debug information is not stripped
21989 out, but the variable address is set to null;
21990 do not add such variables into symbol table. */
21991 }
21992 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21993 {
4b610737
TT
21994 if (SYMBOL_CLASS (sym) == LOC_STATIC
21995 && (objfile->flags & OBJF_MAINLINE) == 0
21996 && dwarf2_per_objfile->can_copy)
21997 {
21998 /* A global static variable might be subject to
21999 copy relocation. We first check for a local
22000 minsym, though, because maybe the symbol was
22001 marked hidden, in which case this would not
22002 apply. */
22003 bound_minimal_symbol found
22004 = (lookup_minimal_symbol_linkage
987012b8 22005 (sym->linkage_name (), objfile));
4b610737
TT
22006 if (found.minsym != nullptr)
22007 sym->maybe_copied = 1;
22008 }
f55ee35c 22009
1c809c68
TT
22010 /* A variable with DW_AT_external is never static,
22011 but it may be block-scoped. */
804d2729 22012 list_to_add
c24bdb02
KS
22013 = ((cu->list_in_scope
22014 == cu->get_builder ()->get_file_symbols ())
22015 ? cu->get_builder ()->get_global_symbols ()
804d2729 22016 : cu->list_in_scope);
1c809c68 22017 }
c906108c 22018 else
e37fd15a 22019 list_to_add = cu->list_in_scope;
c906108c
SS
22020 }
22021 else
22022 {
22023 /* We do not know the address of this symbol.
c5aa993b
JM
22024 If it is an external symbol and we have type information
22025 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22026 The address of the variable will then be determined from
22027 the minimal symbol table whenever the variable is
22028 referenced. */
e142c38c 22029 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22030
22031 /* Fortran explicitly imports any global symbols to the local
22032 scope by DW_TAG_common_block. */
22033 if (cu->language == language_fortran && die->parent
22034 && die->parent->tag == DW_TAG_common_block)
22035 {
22036 /* SYMBOL_CLASS doesn't matter here because
22037 read_common_block is going to reset it. */
22038 if (!suppress_add)
22039 list_to_add = cu->list_in_scope;
22040 }
22041 else if (attr2 && (DW_UNSND (attr2) != 0)
22042 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22043 {
0fe7935b
DJ
22044 /* A variable with DW_AT_external is never static, but it
22045 may be block-scoped. */
804d2729 22046 list_to_add
c24bdb02
KS
22047 = ((cu->list_in_scope
22048 == cu->get_builder ()->get_file_symbols ())
22049 ? cu->get_builder ()->get_global_symbols ()
804d2729 22050 : cu->list_in_scope);
0fe7935b 22051
f1e6e072 22052 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22053 }
442ddf59
JK
22054 else if (!die_is_declaration (die, cu))
22055 {
22056 /* Use the default LOC_OPTIMIZED_OUT class. */
22057 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22058 if (!suppress_add)
22059 list_to_add = cu->list_in_scope;
442ddf59 22060 }
c906108c
SS
22061 }
22062 break;
22063 case DW_TAG_formal_parameter:
a60f3166
TT
22064 {
22065 /* If we are inside a function, mark this as an argument. If
22066 not, we might be looking at an argument to an inlined function
22067 when we do not have enough information to show inlined frames;
22068 pretend it's a local variable in that case so that the user can
22069 still see it. */
804d2729 22070 struct context_stack *curr
c24bdb02 22071 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22072 if (curr != nullptr && curr->name != nullptr)
22073 SYMBOL_IS_ARGUMENT (sym) = 1;
22074 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22075 if (attr != nullptr)
a60f3166
TT
22076 {
22077 var_decode_location (attr, sym, cu);
22078 }
22079 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22080 if (attr != nullptr)
a60f3166
TT
22081 {
22082 dwarf2_const_value (attr, sym, cu);
22083 }
f346a30d 22084
a60f3166
TT
22085 list_to_add = cu->list_in_scope;
22086 }
c906108c
SS
22087 break;
22088 case DW_TAG_unspecified_parameters:
22089 /* From varargs functions; gdb doesn't seem to have any
22090 interest in this information, so just ignore it for now.
22091 (FIXME?) */
22092 break;
34eaf542
TT
22093 case DW_TAG_template_type_param:
22094 suppress_add = 1;
22095 /* Fall through. */
c906108c 22096 case DW_TAG_class_type:
680b30c7 22097 case DW_TAG_interface_type:
c906108c
SS
22098 case DW_TAG_structure_type:
22099 case DW_TAG_union_type:
72019c9c 22100 case DW_TAG_set_type:
c906108c 22101 case DW_TAG_enumeration_type:
f1e6e072 22102 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22103 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22104
63d06c5c 22105 {
9c37b5ae 22106 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22107 really ever be static objects: otherwise, if you try
22108 to, say, break of a class's method and you're in a file
22109 which doesn't mention that class, it won't work unless
22110 the check for all static symbols in lookup_symbol_aux
22111 saves you. See the OtherFileClass tests in
22112 gdb.c++/namespace.exp. */
22113
e37fd15a 22114 if (!suppress_add)
34eaf542 22115 {
c24bdb02 22116 buildsym_compunit *builder = cu->get_builder ();
804d2729 22117 list_to_add
c24bdb02 22118 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22119 && cu->language == language_cplus
c24bdb02 22120 ? builder->get_global_symbols ()
804d2729 22121 : cu->list_in_scope);
63d06c5c 22122
64382290 22123 /* The semantics of C++ state that "struct foo {
9c37b5ae 22124 ... }" also defines a typedef for "foo". */
64382290 22125 if (cu->language == language_cplus
45280282 22126 || cu->language == language_ada
c44af4eb
TT
22127 || cu->language == language_d
22128 || cu->language == language_rust)
64382290
TT
22129 {
22130 /* The symbol's name is already allocated along
22131 with this objfile, so we don't need to
22132 duplicate it for the type. */
22133 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 22134 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 22135 }
63d06c5c
DC
22136 }
22137 }
c906108c
SS
22138 break;
22139 case DW_TAG_typedef:
f1e6e072 22140 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22141 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22142 list_to_add = cu->list_in_scope;
63d06c5c 22143 break;
c906108c 22144 case DW_TAG_base_type:
a02abb62 22145 case DW_TAG_subrange_type:
f1e6e072 22146 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22147 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22148 list_to_add = cu->list_in_scope;
c906108c
SS
22149 break;
22150 case DW_TAG_enumerator:
e142c38c 22151 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22152 if (attr != nullptr)
c906108c 22153 {
e7c27a73 22154 dwarf2_const_value (attr, sym, cu);
c906108c 22155 }
63d06c5c
DC
22156 {
22157 /* NOTE: carlton/2003-11-10: See comment above in the
22158 DW_TAG_class_type, etc. block. */
22159
804d2729 22160 list_to_add
c24bdb02 22161 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22162 && cu->language == language_cplus
c24bdb02 22163 ? cu->get_builder ()->get_global_symbols ()
804d2729 22164 : cu->list_in_scope);
63d06c5c 22165 }
c906108c 22166 break;
74921315 22167 case DW_TAG_imported_declaration:
5c4e30ca 22168 case DW_TAG_namespace:
f1e6e072 22169 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22170 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22171 break;
530e8392
KB
22172 case DW_TAG_module:
22173 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22174 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22175 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22176 break;
4357ac6c 22177 case DW_TAG_common_block:
f1e6e072 22178 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22179 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22180 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22181 break;
c906108c
SS
22182 default:
22183 /* Not a tag we recognize. Hopefully we aren't processing
22184 trash data, but since we must specifically ignore things
22185 we don't recognize, there is nothing else we should do at
0963b4bd 22186 this point. */
b98664d3 22187 complaint (_("unsupported tag: '%s'"),
4d3c2250 22188 dwarf_tag_name (die->tag));
c906108c
SS
22189 break;
22190 }
df8a16a1 22191
e37fd15a
SW
22192 if (suppress_add)
22193 {
22194 sym->hash_next = objfile->template_symbols;
22195 objfile->template_symbols = sym;
22196 list_to_add = NULL;
22197 }
22198
22199 if (list_to_add != NULL)
d3cb6808 22200 add_symbol_to_list (sym, list_to_add);
e37fd15a 22201
df8a16a1
DJ
22202 /* For the benefit of old versions of GCC, check for anonymous
22203 namespaces based on the demangled name. */
4d4ec4e5 22204 if (!cu->processing_has_namespace_info
94af9270 22205 && cu->language == language_cplus)
c24bdb02 22206 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22207 }
22208 return (sym);
22209}
22210
98bfdba5
PA
22211/* Given an attr with a DW_FORM_dataN value in host byte order,
22212 zero-extend it as appropriate for the symbol's type. The DWARF
22213 standard (v4) is not entirely clear about the meaning of using
22214 DW_FORM_dataN for a constant with a signed type, where the type is
22215 wider than the data. The conclusion of a discussion on the DWARF
22216 list was that this is unspecified. We choose to always zero-extend
22217 because that is the interpretation long in use by GCC. */
c906108c 22218
98bfdba5 22219static gdb_byte *
ff39bb5e 22220dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22221 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22222{
518817b3 22223 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22224 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22225 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22226 LONGEST l = DW_UNSND (attr);
22227
22228 if (bits < sizeof (*value) * 8)
22229 {
22230 l &= ((LONGEST) 1 << bits) - 1;
22231 *value = l;
22232 }
22233 else if (bits == sizeof (*value) * 8)
22234 *value = l;
22235 else
22236 {
224c3ddb 22237 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22238 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22239 return bytes;
22240 }
22241
22242 return NULL;
22243}
22244
22245/* Read a constant value from an attribute. Either set *VALUE, or if
22246 the value does not fit in *VALUE, set *BYTES - either already
22247 allocated on the objfile obstack, or newly allocated on OBSTACK,
22248 or, set *BATON, if we translated the constant to a location
22249 expression. */
22250
22251static void
ff39bb5e 22252dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22253 const char *name, struct obstack *obstack,
22254 struct dwarf2_cu *cu,
d521ce57 22255 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22256 struct dwarf2_locexpr_baton **baton)
22257{
518817b3 22258 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22259 struct comp_unit_head *cu_header = &cu->header;
c906108c 22260 struct dwarf_block *blk;
98bfdba5
PA
22261 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22262 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22263
22264 *value = 0;
22265 *bytes = NULL;
22266 *baton = NULL;
c906108c
SS
22267
22268 switch (attr->form)
22269 {
22270 case DW_FORM_addr:
336d760d 22271 case DW_FORM_addrx:
3019eac3 22272 case DW_FORM_GNU_addr_index:
ac56253d 22273 {
ac56253d
TT
22274 gdb_byte *data;
22275
98bfdba5
PA
22276 if (TYPE_LENGTH (type) != cu_header->addr_size)
22277 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22278 cu_header->addr_size,
98bfdba5 22279 TYPE_LENGTH (type));
ac56253d
TT
22280 /* Symbols of this form are reasonably rare, so we just
22281 piggyback on the existing location code rather than writing
22282 a new implementation of symbol_computed_ops. */
8d749320 22283 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22284 (*baton)->per_cu = cu->per_cu;
22285 gdb_assert ((*baton)->per_cu);
ac56253d 22286
98bfdba5 22287 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22288 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22289 (*baton)->data = data;
ac56253d
TT
22290
22291 data[0] = DW_OP_addr;
22292 store_unsigned_integer (&data[1], cu_header->addr_size,
22293 byte_order, DW_ADDR (attr));
22294 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22295 }
c906108c 22296 break;
4ac36638 22297 case DW_FORM_string:
93b5768b 22298 case DW_FORM_strp:
cf532bd1 22299 case DW_FORM_strx:
3019eac3 22300 case DW_FORM_GNU_str_index:
36586728 22301 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22302 /* DW_STRING is already allocated on the objfile obstack, point
22303 directly to it. */
d521ce57 22304 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22305 break;
c906108c
SS
22306 case DW_FORM_block1:
22307 case DW_FORM_block2:
22308 case DW_FORM_block4:
22309 case DW_FORM_block:
2dc7f7b3 22310 case DW_FORM_exprloc:
0224619f 22311 case DW_FORM_data16:
c906108c 22312 blk = DW_BLOCK (attr);
98bfdba5
PA
22313 if (TYPE_LENGTH (type) != blk->size)
22314 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22315 TYPE_LENGTH (type));
22316 *bytes = blk->data;
c906108c 22317 break;
2df3850c
JM
22318
22319 /* The DW_AT_const_value attributes are supposed to carry the
22320 symbol's value "represented as it would be on the target
22321 architecture." By the time we get here, it's already been
22322 converted to host endianness, so we just need to sign- or
22323 zero-extend it as appropriate. */
22324 case DW_FORM_data1:
3aef2284 22325 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22326 break;
c906108c 22327 case DW_FORM_data2:
3aef2284 22328 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22329 break;
c906108c 22330 case DW_FORM_data4:
3aef2284 22331 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22332 break;
c906108c 22333 case DW_FORM_data8:
3aef2284 22334 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22335 break;
22336
c906108c 22337 case DW_FORM_sdata:
663c44ac 22338 case DW_FORM_implicit_const:
98bfdba5 22339 *value = DW_SND (attr);
2df3850c
JM
22340 break;
22341
c906108c 22342 case DW_FORM_udata:
98bfdba5 22343 *value = DW_UNSND (attr);
c906108c 22344 break;
2df3850c 22345
c906108c 22346 default:
b98664d3 22347 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22348 dwarf_form_name (attr->form));
98bfdba5 22349 *value = 0;
c906108c
SS
22350 break;
22351 }
22352}
22353
2df3850c 22354
98bfdba5
PA
22355/* Copy constant value from an attribute to a symbol. */
22356
2df3850c 22357static void
ff39bb5e 22358dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22359 struct dwarf2_cu *cu)
2df3850c 22360{
518817b3 22361 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22362 LONGEST value;
d521ce57 22363 const gdb_byte *bytes;
98bfdba5 22364 struct dwarf2_locexpr_baton *baton;
2df3850c 22365
98bfdba5 22366 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22367 sym->print_name (),
98bfdba5
PA
22368 &objfile->objfile_obstack, cu,
22369 &value, &bytes, &baton);
2df3850c 22370
98bfdba5
PA
22371 if (baton != NULL)
22372 {
98bfdba5 22373 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22374 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22375 }
22376 else if (bytes != NULL)
22377 {
22378 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22379 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22380 }
22381 else
22382 {
22383 SYMBOL_VALUE (sym) = value;
f1e6e072 22384 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22385 }
2df3850c
JM
22386}
22387
c906108c
SS
22388/* Return the type of the die in question using its DW_AT_type attribute. */
22389
22390static struct type *
e7c27a73 22391die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22392{
c906108c 22393 struct attribute *type_attr;
c906108c 22394
e142c38c 22395 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22396 if (!type_attr)
22397 {
518817b3 22398 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22399 /* A missing DW_AT_type represents a void type. */
518817b3 22400 return objfile_type (objfile)->builtin_void;
c906108c 22401 }
348e048f 22402
673bfd45 22403 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22404}
22405
b4ba55a1
JB
22406/* True iff CU's producer generates GNAT Ada auxiliary information
22407 that allows to find parallel types through that information instead
22408 of having to do expensive parallel lookups by type name. */
22409
22410static int
22411need_gnat_info (struct dwarf2_cu *cu)
22412{
de4cb04a
JB
22413 /* Assume that the Ada compiler was GNAT, which always produces
22414 the auxiliary information. */
22415 return (cu->language == language_ada);
b4ba55a1
JB
22416}
22417
b4ba55a1
JB
22418/* Return the auxiliary type of the die in question using its
22419 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22420 attribute is not present. */
22421
22422static struct type *
22423die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22424{
b4ba55a1 22425 struct attribute *type_attr;
b4ba55a1
JB
22426
22427 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22428 if (!type_attr)
22429 return NULL;
22430
673bfd45 22431 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22432}
22433
22434/* If DIE has a descriptive_type attribute, then set the TYPE's
22435 descriptive type accordingly. */
22436
22437static void
22438set_descriptive_type (struct type *type, struct die_info *die,
22439 struct dwarf2_cu *cu)
22440{
22441 struct type *descriptive_type = die_descriptive_type (die, cu);
22442
22443 if (descriptive_type)
22444 {
22445 ALLOCATE_GNAT_AUX_TYPE (type);
22446 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22447 }
22448}
22449
c906108c
SS
22450/* Return the containing type of the die in question using its
22451 DW_AT_containing_type attribute. */
22452
22453static struct type *
e7c27a73 22454die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22455{
c906108c 22456 struct attribute *type_attr;
518817b3 22457 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22458
e142c38c 22459 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22460 if (!type_attr)
22461 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22462 "[in module %s]"), objfile_name (objfile));
33ac96f0 22463
673bfd45 22464 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22465}
22466
ac9ec31b
DE
22467/* Return an error marker type to use for the ill formed type in DIE/CU. */
22468
22469static struct type *
22470build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22471{
518817b3
SM
22472 struct dwarf2_per_objfile *dwarf2_per_objfile
22473 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22474 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22475 char *saved;
ac9ec31b 22476
528e1572
SM
22477 std::string message
22478 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22479 objfile_name (objfile),
22480 sect_offset_str (cu->header.sect_off),
22481 sect_offset_str (die->sect_off));
efba19b0 22482 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22483
19f392bc 22484 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22485}
22486
673bfd45 22487/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22488 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22489 DW_AT_containing_type.
673bfd45
DE
22490 If there is no type substitute an error marker. */
22491
c906108c 22492static struct type *
ff39bb5e 22493lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22494 struct dwarf2_cu *cu)
c906108c 22495{
518817b3
SM
22496 struct dwarf2_per_objfile *dwarf2_per_objfile
22497 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22498 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22499 struct type *this_type;
22500
ac9ec31b
DE
22501 gdb_assert (attr->name == DW_AT_type
22502 || attr->name == DW_AT_GNAT_descriptive_type
22503 || attr->name == DW_AT_containing_type);
22504
673bfd45
DE
22505 /* First see if we have it cached. */
22506
36586728
TT
22507 if (attr->form == DW_FORM_GNU_ref_alt)
22508 {
22509 struct dwarf2_per_cu_data *per_cu;
9c541725 22510 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22511
ed2dc618
SM
22512 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22513 dwarf2_per_objfile);
9c541725 22514 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22515 }
7771576e 22516 else if (attr_form_is_ref (attr))
673bfd45 22517 {
9c541725 22518 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22519
9c541725 22520 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22521 }
55f1336d 22522 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22523 {
ac9ec31b 22524 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22525
ac9ec31b 22526 return get_signatured_type (die, signature, cu);
673bfd45
DE
22527 }
22528 else
22529 {
b98664d3 22530 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22531 " at %s [in module %s]"),
22532 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22533 objfile_name (objfile));
ac9ec31b 22534 return build_error_marker_type (cu, die);
673bfd45
DE
22535 }
22536
22537 /* If not cached we need to read it in. */
22538
22539 if (this_type == NULL)
22540 {
ac9ec31b 22541 struct die_info *type_die = NULL;
673bfd45
DE
22542 struct dwarf2_cu *type_cu = cu;
22543
7771576e 22544 if (attr_form_is_ref (attr))
ac9ec31b
DE
22545 type_die = follow_die_ref (die, attr, &type_cu);
22546 if (type_die == NULL)
22547 return build_error_marker_type (cu, die);
22548 /* If we find the type now, it's probably because the type came
3019eac3
DE
22549 from an inter-CU reference and the type's CU got expanded before
22550 ours. */
ac9ec31b 22551 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22552 }
22553
22554 /* If we still don't have a type use an error marker. */
22555
22556 if (this_type == NULL)
ac9ec31b 22557 return build_error_marker_type (cu, die);
673bfd45 22558
f792889a 22559 return this_type;
c906108c
SS
22560}
22561
673bfd45
DE
22562/* Return the type in DIE, CU.
22563 Returns NULL for invalid types.
22564
02142a6c 22565 This first does a lookup in die_type_hash,
673bfd45
DE
22566 and only reads the die in if necessary.
22567
22568 NOTE: This can be called when reading in partial or full symbols. */
22569
f792889a 22570static struct type *
e7c27a73 22571read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22572{
f792889a
DJ
22573 struct type *this_type;
22574
22575 this_type = get_die_type (die, cu);
22576 if (this_type)
22577 return this_type;
22578
673bfd45
DE
22579 return read_type_die_1 (die, cu);
22580}
22581
22582/* Read the type in DIE, CU.
22583 Returns NULL for invalid types. */
22584
22585static struct type *
22586read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22587{
22588 struct type *this_type = NULL;
22589
c906108c
SS
22590 switch (die->tag)
22591 {
22592 case DW_TAG_class_type:
680b30c7 22593 case DW_TAG_interface_type:
c906108c
SS
22594 case DW_TAG_structure_type:
22595 case DW_TAG_union_type:
f792889a 22596 this_type = read_structure_type (die, cu);
c906108c
SS
22597 break;
22598 case DW_TAG_enumeration_type:
f792889a 22599 this_type = read_enumeration_type (die, cu);
c906108c
SS
22600 break;
22601 case DW_TAG_subprogram:
22602 case DW_TAG_subroutine_type:
edb3359d 22603 case DW_TAG_inlined_subroutine:
f792889a 22604 this_type = read_subroutine_type (die, cu);
c906108c
SS
22605 break;
22606 case DW_TAG_array_type:
f792889a 22607 this_type = read_array_type (die, cu);
c906108c 22608 break;
72019c9c 22609 case DW_TAG_set_type:
f792889a 22610 this_type = read_set_type (die, cu);
72019c9c 22611 break;
c906108c 22612 case DW_TAG_pointer_type:
f792889a 22613 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22614 break;
22615 case DW_TAG_ptr_to_member_type:
f792889a 22616 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22617 break;
22618 case DW_TAG_reference_type:
4297a3f0
AV
22619 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22620 break;
22621 case DW_TAG_rvalue_reference_type:
22622 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22623 break;
22624 case DW_TAG_const_type:
f792889a 22625 this_type = read_tag_const_type (die, cu);
c906108c
SS
22626 break;
22627 case DW_TAG_volatile_type:
f792889a 22628 this_type = read_tag_volatile_type (die, cu);
c906108c 22629 break;
06d66ee9
TT
22630 case DW_TAG_restrict_type:
22631 this_type = read_tag_restrict_type (die, cu);
22632 break;
c906108c 22633 case DW_TAG_string_type:
f792889a 22634 this_type = read_tag_string_type (die, cu);
c906108c
SS
22635 break;
22636 case DW_TAG_typedef:
f792889a 22637 this_type = read_typedef (die, cu);
c906108c 22638 break;
a02abb62 22639 case DW_TAG_subrange_type:
f792889a 22640 this_type = read_subrange_type (die, cu);
a02abb62 22641 break;
c906108c 22642 case DW_TAG_base_type:
f792889a 22643 this_type = read_base_type (die, cu);
c906108c 22644 break;
81a17f79 22645 case DW_TAG_unspecified_type:
f792889a 22646 this_type = read_unspecified_type (die, cu);
81a17f79 22647 break;
0114d602
DJ
22648 case DW_TAG_namespace:
22649 this_type = read_namespace_type (die, cu);
22650 break;
f55ee35c
JK
22651 case DW_TAG_module:
22652 this_type = read_module_type (die, cu);
22653 break;
a2c2acaf
MW
22654 case DW_TAG_atomic_type:
22655 this_type = read_tag_atomic_type (die, cu);
22656 break;
c906108c 22657 default:
b98664d3 22658 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22659 dwarf_tag_name (die->tag));
c906108c
SS
22660 break;
22661 }
63d06c5c 22662
f792889a 22663 return this_type;
63d06c5c
DC
22664}
22665
abc72ce4
DE
22666/* See if we can figure out if the class lives in a namespace. We do
22667 this by looking for a member function; its demangled name will
22668 contain namespace info, if there is any.
22669 Return the computed name or NULL.
22670 Space for the result is allocated on the objfile's obstack.
22671 This is the full-die version of guess_partial_die_structure_name.
22672 In this case we know DIE has no useful parent. */
22673
43816ebc 22674static const char *
abc72ce4
DE
22675guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22676{
22677 struct die_info *spec_die;
22678 struct dwarf2_cu *spec_cu;
22679 struct die_info *child;
518817b3 22680 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22681
22682 spec_cu = cu;
22683 spec_die = die_specification (die, &spec_cu);
22684 if (spec_die != NULL)
22685 {
22686 die = spec_die;
22687 cu = spec_cu;
22688 }
22689
22690 for (child = die->child;
22691 child != NULL;
22692 child = child->sibling)
22693 {
22694 if (child->tag == DW_TAG_subprogram)
22695 {
73b9be8b 22696 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22697
7d45c7c3 22698 if (linkage_name != NULL)
abc72ce4 22699 {
43816ebc
TT
22700 gdb::unique_xmalloc_ptr<char> actual_name
22701 (language_class_name_from_physname (cu->language_defn,
22702 linkage_name));
22703 const char *name = NULL;
abc72ce4
DE
22704
22705 if (actual_name != NULL)
22706 {
15d034d0 22707 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22708
22709 if (die_name != NULL
43816ebc 22710 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22711 {
22712 /* Strip off the class name from the full name.
22713 We want the prefix. */
22714 int die_name_len = strlen (die_name);
43816ebc
TT
22715 int actual_name_len = strlen (actual_name.get ());
22716 const char *ptr = actual_name.get ();
abc72ce4
DE
22717
22718 /* Test for '::' as a sanity check. */
22719 if (actual_name_len > die_name_len + 2
43816ebc 22720 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22721 name = obstack_strndup (
e3b94546 22722 &objfile->per_bfd->storage_obstack,
43816ebc 22723 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22724 }
22725 }
abc72ce4
DE
22726 return name;
22727 }
22728 }
22729 }
22730
22731 return NULL;
22732}
22733
96408a79
SA
22734/* GCC might emit a nameless typedef that has a linkage name. Determine the
22735 prefix part in such case. See
22736 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22737
a121b7c1 22738static const char *
96408a79
SA
22739anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22740{
22741 struct attribute *attr;
e6a959d6 22742 const char *base;
96408a79
SA
22743
22744 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22745 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22746 return NULL;
22747
7d45c7c3 22748 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22749 return NULL;
22750
73b9be8b 22751 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22752 if (attr == NULL || DW_STRING (attr) == NULL)
22753 return NULL;
22754
22755 /* dwarf2_name had to be already called. */
22756 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22757
22758 /* Strip the base name, keep any leading namespaces/classes. */
22759 base = strrchr (DW_STRING (attr), ':');
22760 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22761 return "";
22762
518817b3 22763 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22764 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22765 DW_STRING (attr),
22766 &base[-1] - DW_STRING (attr));
96408a79
SA
22767}
22768
fdde2d81 22769/* Return the name of the namespace/class that DIE is defined within,
0114d602 22770 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22771
0114d602
DJ
22772 For example, if we're within the method foo() in the following
22773 code:
22774
22775 namespace N {
22776 class C {
22777 void foo () {
22778 }
22779 };
22780 }
22781
22782 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22783
0d5cff50 22784static const char *
e142c38c 22785determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22786{
518817b3
SM
22787 struct dwarf2_per_objfile *dwarf2_per_objfile
22788 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22789 struct die_info *parent, *spec_die;
22790 struct dwarf2_cu *spec_cu;
22791 struct type *parent_type;
a121b7c1 22792 const char *retval;
63d06c5c 22793
9c37b5ae 22794 if (cu->language != language_cplus
c44af4eb
TT
22795 && cu->language != language_fortran && cu->language != language_d
22796 && cu->language != language_rust)
0114d602
DJ
22797 return "";
22798
96408a79
SA
22799 retval = anonymous_struct_prefix (die, cu);
22800 if (retval)
22801 return retval;
22802
0114d602
DJ
22803 /* We have to be careful in the presence of DW_AT_specification.
22804 For example, with GCC 3.4, given the code
22805
22806 namespace N {
22807 void foo() {
22808 // Definition of N::foo.
22809 }
22810 }
22811
22812 then we'll have a tree of DIEs like this:
22813
22814 1: DW_TAG_compile_unit
22815 2: DW_TAG_namespace // N
22816 3: DW_TAG_subprogram // declaration of N::foo
22817 4: DW_TAG_subprogram // definition of N::foo
22818 DW_AT_specification // refers to die #3
22819
22820 Thus, when processing die #4, we have to pretend that we're in
22821 the context of its DW_AT_specification, namely the contex of die
22822 #3. */
22823 spec_cu = cu;
22824 spec_die = die_specification (die, &spec_cu);
22825 if (spec_die == NULL)
22826 parent = die->parent;
22827 else
63d06c5c 22828 {
0114d602
DJ
22829 parent = spec_die->parent;
22830 cu = spec_cu;
63d06c5c 22831 }
0114d602
DJ
22832
22833 if (parent == NULL)
22834 return "";
98bfdba5
PA
22835 else if (parent->building_fullname)
22836 {
22837 const char *name;
22838 const char *parent_name;
22839
22840 /* It has been seen on RealView 2.2 built binaries,
22841 DW_TAG_template_type_param types actually _defined_ as
22842 children of the parent class:
22843
22844 enum E {};
22845 template class <class Enum> Class{};
22846 Class<enum E> class_e;
22847
22848 1: DW_TAG_class_type (Class)
22849 2: DW_TAG_enumeration_type (E)
22850 3: DW_TAG_enumerator (enum1:0)
22851 3: DW_TAG_enumerator (enum2:1)
22852 ...
22853 2: DW_TAG_template_type_param
22854 DW_AT_type DW_FORM_ref_udata (E)
22855
22856 Besides being broken debug info, it can put GDB into an
22857 infinite loop. Consider:
22858
22859 When we're building the full name for Class<E>, we'll start
22860 at Class, and go look over its template type parameters,
22861 finding E. We'll then try to build the full name of E, and
22862 reach here. We're now trying to build the full name of E,
22863 and look over the parent DIE for containing scope. In the
22864 broken case, if we followed the parent DIE of E, we'd again
22865 find Class, and once again go look at its template type
22866 arguments, etc., etc. Simply don't consider such parent die
22867 as source-level parent of this die (it can't be, the language
22868 doesn't allow it), and break the loop here. */
22869 name = dwarf2_name (die, cu);
22870 parent_name = dwarf2_name (parent, cu);
b98664d3 22871 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22872 name ? name : "<unknown>",
22873 parent_name ? parent_name : "<unknown>");
22874 return "";
22875 }
63d06c5c 22876 else
0114d602
DJ
22877 switch (parent->tag)
22878 {
63d06c5c 22879 case DW_TAG_namespace:
0114d602 22880 parent_type = read_type_die (parent, cu);
acebe513
UW
22881 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22882 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22883 Work around this problem here. */
22884 if (cu->language == language_cplus
e86ca25f 22885 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22886 return "";
0114d602 22887 /* We give a name to even anonymous namespaces. */
e86ca25f 22888 return TYPE_NAME (parent_type);
63d06c5c 22889 case DW_TAG_class_type:
680b30c7 22890 case DW_TAG_interface_type:
63d06c5c 22891 case DW_TAG_structure_type:
0114d602 22892 case DW_TAG_union_type:
f55ee35c 22893 case DW_TAG_module:
0114d602 22894 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22895 if (TYPE_NAME (parent_type) != NULL)
22896 return TYPE_NAME (parent_type);
0114d602
DJ
22897 else
22898 /* An anonymous structure is only allowed non-static data
22899 members; no typedefs, no member functions, et cetera.
22900 So it does not need a prefix. */
22901 return "";
abc72ce4 22902 case DW_TAG_compile_unit:
95554aad 22903 case DW_TAG_partial_unit:
abc72ce4
DE
22904 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22905 if (cu->language == language_cplus
fd5866f6 22906 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22907 && die->child != NULL
22908 && (die->tag == DW_TAG_class_type
22909 || die->tag == DW_TAG_structure_type
22910 || die->tag == DW_TAG_union_type))
22911 {
43816ebc 22912 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22913 if (name != NULL)
22914 return name;
22915 }
22916 return "";
0a4b0913
AB
22917 case DW_TAG_subprogram:
22918 /* Nested subroutines in Fortran get a prefix with the name
22919 of the parent's subroutine. */
22920 if (cu->language == language_fortran)
22921 {
22922 if ((die->tag == DW_TAG_subprogram)
22923 && (dwarf2_name (parent, cu) != NULL))
22924 return dwarf2_name (parent, cu);
22925 }
22926 return determine_prefix (parent, cu);
3d567982
TT
22927 case DW_TAG_enumeration_type:
22928 parent_type = read_type_die (parent, cu);
22929 if (TYPE_DECLARED_CLASS (parent_type))
22930 {
e86ca25f
TT
22931 if (TYPE_NAME (parent_type) != NULL)
22932 return TYPE_NAME (parent_type);
3d567982
TT
22933 return "";
22934 }
22935 /* Fall through. */
63d06c5c 22936 default:
8176b9b8 22937 return determine_prefix (parent, cu);
63d06c5c 22938 }
63d06c5c
DC
22939}
22940
3e43a32a
MS
22941/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22942 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22943 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22944 an obconcat, otherwise allocate storage for the result. The CU argument is
22945 used to determine the language and hence, the appropriate separator. */
987504bb 22946
f55ee35c 22947#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22948
22949static char *
f55ee35c
JK
22950typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22951 int physname, struct dwarf2_cu *cu)
63d06c5c 22952{
f55ee35c 22953 const char *lead = "";
5c315b68 22954 const char *sep;
63d06c5c 22955
3e43a32a
MS
22956 if (suffix == NULL || suffix[0] == '\0'
22957 || prefix == NULL || prefix[0] == '\0')
987504bb 22958 sep = "";
45280282
IB
22959 else if (cu->language == language_d)
22960 {
22961 /* For D, the 'main' function could be defined in any module, but it
22962 should never be prefixed. */
22963 if (strcmp (suffix, "D main") == 0)
22964 {
22965 prefix = "";
22966 sep = "";
22967 }
22968 else
22969 sep = ".";
22970 }
f55ee35c
JK
22971 else if (cu->language == language_fortran && physname)
22972 {
22973 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22974 DW_AT_MIPS_linkage_name is preferred and used instead. */
22975
22976 lead = "__";
22977 sep = "_MOD_";
22978 }
987504bb
JJ
22979 else
22980 sep = "::";
63d06c5c 22981
6dd47d34
DE
22982 if (prefix == NULL)
22983 prefix = "";
22984 if (suffix == NULL)
22985 suffix = "";
22986
987504bb
JJ
22987 if (obs == NULL)
22988 {
3e43a32a 22989 char *retval
224c3ddb
SM
22990 = ((char *)
22991 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22992
f55ee35c
JK
22993 strcpy (retval, lead);
22994 strcat (retval, prefix);
6dd47d34
DE
22995 strcat (retval, sep);
22996 strcat (retval, suffix);
63d06c5c
DC
22997 return retval;
22998 }
987504bb
JJ
22999 else
23000 {
23001 /* We have an obstack. */
f55ee35c 23002 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23003 }
63d06c5c
DC
23004}
23005
c906108c
SS
23006/* Return sibling of die, NULL if no sibling. */
23007
f9aca02d 23008static struct die_info *
fba45db2 23009sibling_die (struct die_info *die)
c906108c 23010{
639d11d3 23011 return die->sibling;
c906108c
SS
23012}
23013
71c25dea
TT
23014/* Get name of a die, return NULL if not found. */
23015
15d034d0
TT
23016static const char *
23017dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
23018 struct obstack *obstack)
23019{
23020 if (name && cu->language == language_cplus)
23021 {
2f408ecb 23022 std::string canon_name = cp_canonicalize_string (name);
71c25dea 23023
2f408ecb 23024 if (!canon_name.empty ())
71c25dea 23025 {
2f408ecb 23026 if (canon_name != name)
efba19b0 23027 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
23028 }
23029 }
23030
23031 return name;
c906108c
SS
23032}
23033
96553a0c
DE
23034/* Get name of a die, return NULL if not found.
23035 Anonymous namespaces are converted to their magic string. */
9219021c 23036
15d034d0 23037static const char *
e142c38c 23038dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23039{
23040 struct attribute *attr;
518817b3 23041 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 23042
e142c38c 23043 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 23044 if ((!attr || !DW_STRING (attr))
96553a0c 23045 && die->tag != DW_TAG_namespace
53832f31
TT
23046 && die->tag != DW_TAG_class_type
23047 && die->tag != DW_TAG_interface_type
23048 && die->tag != DW_TAG_structure_type
23049 && die->tag != DW_TAG_union_type)
71c25dea
TT
23050 return NULL;
23051
23052 switch (die->tag)
23053 {
23054 case DW_TAG_compile_unit:
95554aad 23055 case DW_TAG_partial_unit:
71c25dea
TT
23056 /* Compilation units have a DW_AT_name that is a filename, not
23057 a source language identifier. */
23058 case DW_TAG_enumeration_type:
23059 case DW_TAG_enumerator:
23060 /* These tags always have simple identifiers already; no need
23061 to canonicalize them. */
23062 return DW_STRING (attr);
907af001 23063
96553a0c
DE
23064 case DW_TAG_namespace:
23065 if (attr != NULL && DW_STRING (attr) != NULL)
23066 return DW_STRING (attr);
23067 return CP_ANONYMOUS_NAMESPACE_STR;
23068
907af001
UW
23069 case DW_TAG_class_type:
23070 case DW_TAG_interface_type:
23071 case DW_TAG_structure_type:
23072 case DW_TAG_union_type:
23073 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23074 structures or unions. These were of the form "._%d" in GCC 4.1,
23075 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23076 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 23077 if (attr && DW_STRING (attr)
61012eef
GB
23078 && (startswith (DW_STRING (attr), "._")
23079 || startswith (DW_STRING (attr), "<anonymous")))
907af001 23080 return NULL;
53832f31
TT
23081
23082 /* GCC might emit a nameless typedef that has a linkage name. See
23083 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23084 if (!attr || DW_STRING (attr) == NULL)
23085 {
73b9be8b 23086 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
23087 if (attr == NULL || DW_STRING (attr) == NULL)
23088 return NULL;
23089
df5c6c50
JK
23090 /* Avoid demangling DW_STRING (attr) the second time on a second
23091 call for the same DIE. */
23092 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 23093 {
43816ebc
TT
23094 gdb::unique_xmalloc_ptr<char> demangled
23095 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23096
e6a959d6 23097 const char *base;
96408a79 23098
53832f31 23099 /* FIXME: we already did this for the partial symbol... */
34a68019 23100 DW_STRING (attr)
021887d8 23101 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 23102 demangled.get ());
53832f31 23103 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
23104
23105 /* Strip any leading namespaces/classes, keep only the base name.
23106 DW_AT_name for named DIEs does not contain the prefixes. */
23107 base = strrchr (DW_STRING (attr), ':');
23108 if (base && base > DW_STRING (attr) && base[-1] == ':')
23109 return &base[1];
23110 else
23111 return DW_STRING (attr);
53832f31
TT
23112 }
23113 }
907af001
UW
23114 break;
23115
71c25dea 23116 default:
907af001
UW
23117 break;
23118 }
23119
23120 if (!DW_STRING_IS_CANONICAL (attr))
23121 {
23122 DW_STRING (attr)
23123 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 23124 &objfile->per_bfd->storage_obstack);
907af001 23125 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 23126 }
907af001 23127 return DW_STRING (attr);
9219021c
DC
23128}
23129
23130/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23131 is none. *EXT_CU is the CU containing DIE on input, and the CU
23132 containing the return value on output. */
9219021c
DC
23133
23134static struct die_info *
f2f0e013 23135dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23136{
23137 struct attribute *attr;
9219021c 23138
f2f0e013 23139 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23140 if (attr == NULL)
23141 return NULL;
23142
f2f0e013 23143 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23144}
23145
fa9c3fa0
TT
23146/* A convenience function that returns an "unknown" DWARF name,
23147 including the value of V. STR is the name of the entity being
23148 printed, e.g., "TAG". */
23149
23150static const char *
23151dwarf_unknown (const char *str, unsigned v)
23152{
23153 char *cell = get_print_cell ();
23154 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23155 return cell;
23156}
23157
c906108c
SS
23158/* Convert a DIE tag into its string name. */
23159
f39c6ffd 23160static const char *
aa1ee363 23161dwarf_tag_name (unsigned tag)
c906108c 23162{
f39c6ffd
TT
23163 const char *name = get_DW_TAG_name (tag);
23164
23165 if (name == NULL)
fa9c3fa0 23166 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
23167
23168 return name;
c906108c
SS
23169}
23170
23171/* Convert a DWARF attribute code into its string name. */
23172
f39c6ffd 23173static const char *
aa1ee363 23174dwarf_attr_name (unsigned attr)
c906108c 23175{
f39c6ffd
TT
23176 const char *name;
23177
c764a876 23178#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
23179 if (attr == DW_AT_MIPS_fde)
23180 return "DW_AT_MIPS_fde";
23181#else
23182 if (attr == DW_AT_HP_block_index)
23183 return "DW_AT_HP_block_index";
c764a876 23184#endif
f39c6ffd
TT
23185
23186 name = get_DW_AT_name (attr);
23187
23188 if (name == NULL)
fa9c3fa0 23189 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
23190
23191 return name;
c906108c
SS
23192}
23193
a084a2a6
AT
23194/* Convert a unit type to corresponding DW_UT name. */
23195
23196static const char *
23197dwarf_unit_type_name (int unit_type) {
23198 switch (unit_type)
23199 {
23200 case 0x01:
23201 return "DW_UT_compile (0x01)";
23202 case 0x02:
23203 return "DW_UT_type (0x02)";
23204 case 0x03:
23205 return "DW_UT_partial (0x03)";
23206 case 0x04:
23207 return "DW_UT_skeleton (0x04)";
23208 case 0x05:
23209 return "DW_UT_split_compile (0x05)";
23210 case 0x06:
23211 return "DW_UT_split_type (0x06)";
23212 case 0x80:
23213 return "DW_UT_lo_user (0x80)";
23214 case 0xff:
23215 return "DW_UT_hi_user (0xff)";
23216 default:
23217 return nullptr;
23218 }
23219}
23220
c906108c
SS
23221/* Convert a DWARF value form code into its string name. */
23222
f39c6ffd 23223static const char *
aa1ee363 23224dwarf_form_name (unsigned form)
c906108c 23225{
f39c6ffd
TT
23226 const char *name = get_DW_FORM_name (form);
23227
23228 if (name == NULL)
fa9c3fa0 23229 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
23230
23231 return name;
c906108c
SS
23232}
23233
a121b7c1 23234static const char *
fba45db2 23235dwarf_bool_name (unsigned mybool)
c906108c
SS
23236{
23237 if (mybool)
23238 return "TRUE";
23239 else
23240 return "FALSE";
23241}
23242
23243/* Convert a DWARF type code into its string name. */
23244
f39c6ffd 23245static const char *
aa1ee363 23246dwarf_type_encoding_name (unsigned enc)
c906108c 23247{
f39c6ffd 23248 const char *name = get_DW_ATE_name (enc);
c906108c 23249
f39c6ffd 23250 if (name == NULL)
fa9c3fa0 23251 return dwarf_unknown ("ATE", enc);
c906108c 23252
f39c6ffd 23253 return name;
c906108c 23254}
c906108c 23255
f9aca02d 23256static void
d97bc12b 23257dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23258{
23259 unsigned int i;
23260
d97bc12b 23261 print_spaces (indent, f);
9d8780f0 23262 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23263 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23264 sect_offset_str (die->sect_off));
d97bc12b
DE
23265
23266 if (die->parent != NULL)
23267 {
23268 print_spaces (indent, f);
9d8780f0
SM
23269 fprintf_unfiltered (f, " parent at offset: %s\n",
23270 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23271 }
23272
23273 print_spaces (indent, f);
23274 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23275 dwarf_bool_name (die->child != NULL));
c906108c 23276
d97bc12b
DE
23277 print_spaces (indent, f);
23278 fprintf_unfiltered (f, " attributes:\n");
23279
c906108c
SS
23280 for (i = 0; i < die->num_attrs; ++i)
23281 {
d97bc12b
DE
23282 print_spaces (indent, f);
23283 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23284 dwarf_attr_name (die->attrs[i].name),
23285 dwarf_form_name (die->attrs[i].form));
d97bc12b 23286
c906108c
SS
23287 switch (die->attrs[i].form)
23288 {
c906108c 23289 case DW_FORM_addr:
336d760d 23290 case DW_FORM_addrx:
3019eac3 23291 case DW_FORM_GNU_addr_index:
d97bc12b 23292 fprintf_unfiltered (f, "address: ");
5af949e3 23293 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23294 break;
23295 case DW_FORM_block2:
23296 case DW_FORM_block4:
23297 case DW_FORM_block:
23298 case DW_FORM_block1:
56eb65bd
SP
23299 fprintf_unfiltered (f, "block: size %s",
23300 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23301 break;
2dc7f7b3 23302 case DW_FORM_exprloc:
56eb65bd
SP
23303 fprintf_unfiltered (f, "expression: size %s",
23304 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23305 break;
0224619f
JK
23306 case DW_FORM_data16:
23307 fprintf_unfiltered (f, "constant of 16 bytes");
23308 break;
4568ecf9
DE
23309 case DW_FORM_ref_addr:
23310 fprintf_unfiltered (f, "ref address: ");
23311 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23312 break;
36586728
TT
23313 case DW_FORM_GNU_ref_alt:
23314 fprintf_unfiltered (f, "alt ref address: ");
23315 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23316 break;
10b3939b
DJ
23317 case DW_FORM_ref1:
23318 case DW_FORM_ref2:
23319 case DW_FORM_ref4:
4568ecf9
DE
23320 case DW_FORM_ref8:
23321 case DW_FORM_ref_udata:
d97bc12b 23322 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23323 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23324 break;
c906108c
SS
23325 case DW_FORM_data1:
23326 case DW_FORM_data2:
23327 case DW_FORM_data4:
ce5d95e1 23328 case DW_FORM_data8:
c906108c
SS
23329 case DW_FORM_udata:
23330 case DW_FORM_sdata:
43bbcdc2
PH
23331 fprintf_unfiltered (f, "constant: %s",
23332 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23333 break;
2dc7f7b3
TT
23334 case DW_FORM_sec_offset:
23335 fprintf_unfiltered (f, "section offset: %s",
23336 pulongest (DW_UNSND (&die->attrs[i])));
23337 break;
55f1336d 23338 case DW_FORM_ref_sig8:
ac9ec31b
DE
23339 fprintf_unfiltered (f, "signature: %s",
23340 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23341 break;
c906108c 23342 case DW_FORM_string:
4bdf3d34 23343 case DW_FORM_strp:
43988095 23344 case DW_FORM_line_strp:
cf532bd1 23345 case DW_FORM_strx:
3019eac3 23346 case DW_FORM_GNU_str_index:
36586728 23347 case DW_FORM_GNU_strp_alt:
8285870a 23348 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23349 DW_STRING (&die->attrs[i])
8285870a
JK
23350 ? DW_STRING (&die->attrs[i]) : "",
23351 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23352 break;
23353 case DW_FORM_flag:
23354 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23355 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23356 else
d97bc12b 23357 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23358 break;
2dc7f7b3
TT
23359 case DW_FORM_flag_present:
23360 fprintf_unfiltered (f, "flag: TRUE");
23361 break;
a8329558 23362 case DW_FORM_indirect:
0963b4bd
MS
23363 /* The reader will have reduced the indirect form to
23364 the "base form" so this form should not occur. */
5f48f8f3 23365 fprintf_unfiltered (f,
3e43a32a 23366 "unexpected attribute form: DW_FORM_indirect");
a8329558 23367 break;
663c44ac
JK
23368 case DW_FORM_implicit_const:
23369 fprintf_unfiltered (f, "constant: %s",
23370 plongest (DW_SND (&die->attrs[i])));
23371 break;
c906108c 23372 default:
d97bc12b 23373 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23374 die->attrs[i].form);
d97bc12b 23375 break;
c906108c 23376 }
d97bc12b 23377 fprintf_unfiltered (f, "\n");
c906108c
SS
23378 }
23379}
23380
f9aca02d 23381static void
d97bc12b 23382dump_die_for_error (struct die_info *die)
c906108c 23383{
d97bc12b
DE
23384 dump_die_shallow (gdb_stderr, 0, die);
23385}
23386
23387static void
23388dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23389{
23390 int indent = level * 4;
23391
23392 gdb_assert (die != NULL);
23393
23394 if (level >= max_level)
23395 return;
23396
23397 dump_die_shallow (f, indent, die);
23398
23399 if (die->child != NULL)
c906108c 23400 {
d97bc12b
DE
23401 print_spaces (indent, f);
23402 fprintf_unfiltered (f, " Children:");
23403 if (level + 1 < max_level)
23404 {
23405 fprintf_unfiltered (f, "\n");
23406 dump_die_1 (f, level + 1, max_level, die->child);
23407 }
23408 else
23409 {
3e43a32a
MS
23410 fprintf_unfiltered (f,
23411 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23412 }
23413 }
23414
23415 if (die->sibling != NULL && level > 0)
23416 {
23417 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23418 }
23419}
23420
d97bc12b
DE
23421/* This is called from the pdie macro in gdbinit.in.
23422 It's not static so gcc will keep a copy callable from gdb. */
23423
23424void
23425dump_die (struct die_info *die, int max_level)
23426{
23427 dump_die_1 (gdb_stdlog, 0, max_level, die);
23428}
23429
f9aca02d 23430static void
51545339 23431store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23432{
51545339 23433 void **slot;
c906108c 23434
9c541725
PA
23435 slot = htab_find_slot_with_hash (cu->die_hash, die,
23436 to_underlying (die->sect_off),
b64f50a1 23437 INSERT);
51545339
DJ
23438
23439 *slot = die;
c906108c
SS
23440}
23441
b64f50a1
JK
23442/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23443 required kind. */
23444
23445static sect_offset
ff39bb5e 23446dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23447{
7771576e 23448 if (attr_form_is_ref (attr))
9c541725 23449 return (sect_offset) DW_UNSND (attr);
93311388 23450
b98664d3 23451 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23452 dwarf_form_name (attr->form));
9c541725 23453 return {};
c906108c
SS
23454}
23455
43bbcdc2
PH
23456/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23457 * the value held by the attribute is not constant. */
a02abb62 23458
43bbcdc2 23459static LONGEST
ff39bb5e 23460dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23461{
663c44ac 23462 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23463 return DW_SND (attr);
23464 else if (attr->form == DW_FORM_udata
23465 || attr->form == DW_FORM_data1
23466 || attr->form == DW_FORM_data2
23467 || attr->form == DW_FORM_data4
23468 || attr->form == DW_FORM_data8)
23469 return DW_UNSND (attr);
23470 else
23471 {
0224619f 23472 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23473 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23474 dwarf_form_name (attr->form));
23475 return default_value;
23476 }
23477}
23478
348e048f
DE
23479/* Follow reference or signature attribute ATTR of SRC_DIE.
23480 On entry *REF_CU is the CU of SRC_DIE.
23481 On exit *REF_CU is the CU of the result. */
23482
23483static struct die_info *
ff39bb5e 23484follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23485 struct dwarf2_cu **ref_cu)
23486{
23487 struct die_info *die;
23488
7771576e 23489 if (attr_form_is_ref (attr))
348e048f 23490 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23491 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23492 die = follow_die_sig (src_die, attr, ref_cu);
23493 else
23494 {
23495 dump_die_for_error (src_die);
23496 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23497 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23498 }
23499
23500 return die;
03dd20cc
DJ
23501}
23502
5c631832 23503/* Follow reference OFFSET.
673bfd45
DE
23504 On entry *REF_CU is the CU of the source die referencing OFFSET.
23505 On exit *REF_CU is the CU of the result.
23506 Returns NULL if OFFSET is invalid. */
f504f079 23507
f9aca02d 23508static struct die_info *
9c541725 23509follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23510 struct dwarf2_cu **ref_cu)
c906108c 23511{
10b3939b 23512 struct die_info temp_die;
f2f0e013 23513 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23514 struct dwarf2_per_objfile *dwarf2_per_objfile
23515 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23516
348e048f
DE
23517 gdb_assert (cu->per_cu != NULL);
23518
98bfdba5
PA
23519 target_cu = cu;
23520
3019eac3 23521 if (cu->per_cu->is_debug_types)
348e048f
DE
23522 {
23523 /* .debug_types CUs cannot reference anything outside their CU.
23524 If they need to, they have to reference a signatured type via
55f1336d 23525 DW_FORM_ref_sig8. */
9c541725 23526 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23527 return NULL;
348e048f 23528 }
36586728 23529 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23530 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23531 {
23532 struct dwarf2_per_cu_data *per_cu;
9a619af0 23533
9c541725 23534 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23535 dwarf2_per_objfile);
03dd20cc
DJ
23536
23537 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23538 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23539 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23540
10b3939b
DJ
23541 target_cu = per_cu->cu;
23542 }
98bfdba5
PA
23543 else if (cu->dies == NULL)
23544 {
23545 /* We're loading full DIEs during partial symbol reading. */
23546 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23547 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23548 }
c906108c 23549
f2f0e013 23550 *ref_cu = target_cu;
9c541725 23551 temp_die.sect_off = sect_off;
c24bdb02
KS
23552
23553 if (target_cu != cu)
23554 target_cu->ancestor = cu;
23555
9a3c8263 23556 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23557 &temp_die,
23558 to_underlying (sect_off));
5c631832 23559}
10b3939b 23560
5c631832
JK
23561/* Follow reference attribute ATTR of SRC_DIE.
23562 On entry *REF_CU is the CU of SRC_DIE.
23563 On exit *REF_CU is the CU of the result. */
23564
23565static struct die_info *
ff39bb5e 23566follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23567 struct dwarf2_cu **ref_cu)
23568{
9c541725 23569 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23570 struct dwarf2_cu *cu = *ref_cu;
23571 struct die_info *die;
23572
9c541725 23573 die = follow_die_offset (sect_off,
36586728
TT
23574 (attr->form == DW_FORM_GNU_ref_alt
23575 || cu->per_cu->is_dwz),
23576 ref_cu);
5c631832 23577 if (!die)
9d8780f0
SM
23578 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23579 "at %s [in module %s]"),
23580 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23581 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23582
5c631832
JK
23583 return die;
23584}
23585
9c541725 23586/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23587 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23588 dwarf2_locexpr_baton->data has lifetime of
23589 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23590
23591struct dwarf2_locexpr_baton
9c541725 23592dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23593 struct dwarf2_per_cu_data *per_cu,
23594 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23595 void *baton, bool resolve_abstract_p)
5c631832 23596{
918dd910 23597 struct dwarf2_cu *cu;
5c631832
JK
23598 struct die_info *die;
23599 struct attribute *attr;
23600 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23601 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23602 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23603
918dd910 23604 if (per_cu->cu == NULL)
58f0c718 23605 load_cu (per_cu, false);
918dd910 23606 cu = per_cu->cu;
cc12ce38
DE
23607 if (cu == NULL)
23608 {
23609 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23610 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23611 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23612 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23613 }
918dd910 23614
9c541725 23615 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23616 if (!die)
9d8780f0
SM
23617 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23618 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23619
23620 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23621 if (!attr && resolve_abstract_p
3360b6e7 23622 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23623 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23624 {
23625 CORE_ADDR pc = (*get_frame_pc) (baton);
eba4caf2
TV
23626 CORE_ADDR baseaddr
23627 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23628 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23629
3360b6e7
TV
23630 for (const auto &cand_off
23631 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23632 {
3360b6e7
TV
23633 struct dwarf2_cu *cand_cu = cu;
23634 struct die_info *cand
23635 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23636 if (!cand
23637 || !cand->parent
e4a62c65
TV
23638 || cand->parent->tag != DW_TAG_subprogram)
23639 continue;
23640
23641 CORE_ADDR pc_low, pc_high;
23642 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23643 if (pc_low == ((CORE_ADDR) -1))
23644 continue;
23645 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23646 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23647 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23648 continue;
23649
23650 die = cand;
23651 attr = dwarf2_attr (die, DW_AT_location, cu);
23652 break;
23653 }
23654 }
23655
5c631832
JK
23656 if (!attr)
23657 {
e103e986
JK
23658 /* DWARF: "If there is no such attribute, then there is no effect.".
23659 DATA is ignored if SIZE is 0. */
5c631832 23660
e103e986 23661 retval.data = NULL;
5c631832
JK
23662 retval.size = 0;
23663 }
8cf6f0b1
TT
23664 else if (attr_form_is_section_offset (attr))
23665 {
23666 struct dwarf2_loclist_baton loclist_baton;
23667 CORE_ADDR pc = (*get_frame_pc) (baton);
23668 size_t size;
23669
23670 fill_in_loclist_baton (cu, &loclist_baton, attr);
23671
23672 retval.data = dwarf2_find_location_expression (&loclist_baton,
23673 &size, pc);
23674 retval.size = size;
23675 }
5c631832
JK
23676 else
23677 {
23678 if (!attr_form_is_block (attr))
9d8780f0 23679 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23680 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23681 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23682
23683 retval.data = DW_BLOCK (attr)->data;
23684 retval.size = DW_BLOCK (attr)->size;
23685 }
23686 retval.per_cu = cu->per_cu;
918dd910 23687
ed2dc618 23688 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23689
5c631832 23690 return retval;
348e048f
DE
23691}
23692
8b9737bf
TT
23693/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23694 offset. */
23695
23696struct dwarf2_locexpr_baton
23697dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23698 struct dwarf2_per_cu_data *per_cu,
23699 CORE_ADDR (*get_frame_pc) (void *baton),
23700 void *baton)
23701{
9c541725 23702 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23703
9c541725 23704 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23705}
23706
b6807d98
TT
23707/* Write a constant of a given type as target-ordered bytes into
23708 OBSTACK. */
23709
23710static const gdb_byte *
23711write_constant_as_bytes (struct obstack *obstack,
23712 enum bfd_endian byte_order,
23713 struct type *type,
23714 ULONGEST value,
23715 LONGEST *len)
23716{
23717 gdb_byte *result;
23718
23719 *len = TYPE_LENGTH (type);
224c3ddb 23720 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23721 store_unsigned_integer (result, *len, byte_order, value);
23722
23723 return result;
23724}
23725
23726/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23727 pointer to the constant bytes and set LEN to the length of the
23728 data. If memory is needed, allocate it on OBSTACK. If the DIE
23729 does not have a DW_AT_const_value, return NULL. */
23730
23731const gdb_byte *
9c541725 23732dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23733 struct dwarf2_per_cu_data *per_cu,
23734 struct obstack *obstack,
23735 LONGEST *len)
23736{
23737 struct dwarf2_cu *cu;
23738 struct die_info *die;
23739 struct attribute *attr;
23740 const gdb_byte *result = NULL;
23741 struct type *type;
23742 LONGEST value;
23743 enum bfd_endian byte_order;
e3b94546 23744 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23745
b6807d98 23746 if (per_cu->cu == NULL)
58f0c718 23747 load_cu (per_cu, false);
b6807d98 23748 cu = per_cu->cu;
cc12ce38
DE
23749 if (cu == NULL)
23750 {
23751 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23752 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23753 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23754 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23755 }
b6807d98 23756
9c541725 23757 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23758 if (!die)
9d8780f0
SM
23759 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23760 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23761
23762 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23763 if (attr == NULL)
23764 return NULL;
23765
e3b94546 23766 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23767 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23768
23769 switch (attr->form)
23770 {
23771 case DW_FORM_addr:
336d760d 23772 case DW_FORM_addrx:
b6807d98
TT
23773 case DW_FORM_GNU_addr_index:
23774 {
23775 gdb_byte *tem;
23776
23777 *len = cu->header.addr_size;
224c3ddb 23778 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23779 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23780 result = tem;
23781 }
23782 break;
23783 case DW_FORM_string:
23784 case DW_FORM_strp:
cf532bd1 23785 case DW_FORM_strx:
b6807d98
TT
23786 case DW_FORM_GNU_str_index:
23787 case DW_FORM_GNU_strp_alt:
23788 /* DW_STRING is already allocated on the objfile obstack, point
23789 directly to it. */
23790 result = (const gdb_byte *) DW_STRING (attr);
23791 *len = strlen (DW_STRING (attr));
23792 break;
23793 case DW_FORM_block1:
23794 case DW_FORM_block2:
23795 case DW_FORM_block4:
23796 case DW_FORM_block:
23797 case DW_FORM_exprloc:
0224619f 23798 case DW_FORM_data16:
b6807d98
TT
23799 result = DW_BLOCK (attr)->data;
23800 *len = DW_BLOCK (attr)->size;
23801 break;
23802
23803 /* The DW_AT_const_value attributes are supposed to carry the
23804 symbol's value "represented as it would be on the target
23805 architecture." By the time we get here, it's already been
23806 converted to host endianness, so we just need to sign- or
23807 zero-extend it as appropriate. */
23808 case DW_FORM_data1:
23809 type = die_type (die, cu);
23810 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23811 if (result == NULL)
23812 result = write_constant_as_bytes (obstack, byte_order,
23813 type, value, len);
23814 break;
23815 case DW_FORM_data2:
23816 type = die_type (die, cu);
23817 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23818 if (result == NULL)
23819 result = write_constant_as_bytes (obstack, byte_order,
23820 type, value, len);
23821 break;
23822 case DW_FORM_data4:
23823 type = die_type (die, cu);
23824 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23825 if (result == NULL)
23826 result = write_constant_as_bytes (obstack, byte_order,
23827 type, value, len);
23828 break;
23829 case DW_FORM_data8:
23830 type = die_type (die, cu);
23831 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23832 if (result == NULL)
23833 result = write_constant_as_bytes (obstack, byte_order,
23834 type, value, len);
23835 break;
23836
23837 case DW_FORM_sdata:
663c44ac 23838 case DW_FORM_implicit_const:
b6807d98
TT
23839 type = die_type (die, cu);
23840 result = write_constant_as_bytes (obstack, byte_order,
23841 type, DW_SND (attr), len);
23842 break;
23843
23844 case DW_FORM_udata:
23845 type = die_type (die, cu);
23846 result = write_constant_as_bytes (obstack, byte_order,
23847 type, DW_UNSND (attr), len);
23848 break;
23849
23850 default:
b98664d3 23851 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23852 dwarf_form_name (attr->form));
23853 break;
23854 }
23855
23856 return result;
23857}
23858
7942e96e
AA
23859/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23860 valid type for this die is found. */
23861
23862struct type *
9c541725 23863dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23864 struct dwarf2_per_cu_data *per_cu)
23865{
23866 struct dwarf2_cu *cu;
23867 struct die_info *die;
23868
7942e96e 23869 if (per_cu->cu == NULL)
58f0c718 23870 load_cu (per_cu, false);
7942e96e
AA
23871 cu = per_cu->cu;
23872 if (!cu)
23873 return NULL;
23874
9c541725 23875 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23876 if (!die)
23877 return NULL;
23878
23879 return die_type (die, cu);
23880}
23881
8a9b8146
TT
23882/* Return the type of the DIE at DIE_OFFSET in the CU named by
23883 PER_CU. */
23884
23885struct type *
b64f50a1 23886dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23887 struct dwarf2_per_cu_data *per_cu)
23888{
9c541725 23889 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23890 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23891}
23892
ac9ec31b 23893/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23894 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23895 On exit *REF_CU is the CU of the result.
23896 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23897
23898static struct die_info *
ac9ec31b
DE
23899follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23900 struct dwarf2_cu **ref_cu)
348e048f 23901{
348e048f 23902 struct die_info temp_die;
c24bdb02 23903 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23904 struct die_info *die;
23905
ac9ec31b
DE
23906 /* While it might be nice to assert sig_type->type == NULL here,
23907 we can get here for DW_AT_imported_declaration where we need
23908 the DIE not the type. */
348e048f
DE
23909
23910 /* If necessary, add it to the queue and load its DIEs. */
23911
95554aad 23912 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23913 read_signatured_type (sig_type);
348e048f 23914
348e048f 23915 sig_cu = sig_type->per_cu.cu;
69d751e3 23916 gdb_assert (sig_cu != NULL);
9c541725
PA
23917 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23918 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23919 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23920 to_underlying (temp_die.sect_off));
348e048f
DE
23921 if (die)
23922 {
ed2dc618 23923 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23924 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23925
796a7ff8
DE
23926 /* For .gdb_index version 7 keep track of included TUs.
23927 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23928 if (dwarf2_per_objfile->index_table != NULL
23929 && dwarf2_per_objfile->index_table->version <= 7)
23930 {
ae640021 23931 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23932 }
23933
348e048f 23934 *ref_cu = sig_cu;
c24bdb02
KS
23935 if (sig_cu != cu)
23936 sig_cu->ancestor = cu;
23937
348e048f
DE
23938 return die;
23939 }
23940
ac9ec31b
DE
23941 return NULL;
23942}
23943
23944/* Follow signatured type referenced by ATTR in SRC_DIE.
23945 On entry *REF_CU is the CU of SRC_DIE.
23946 On exit *REF_CU is the CU of the result.
23947 The result is the DIE of the type.
23948 If the referenced type cannot be found an error is thrown. */
23949
23950static struct die_info *
ff39bb5e 23951follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23952 struct dwarf2_cu **ref_cu)
23953{
23954 ULONGEST signature = DW_SIGNATURE (attr);
23955 struct signatured_type *sig_type;
23956 struct die_info *die;
23957
23958 gdb_assert (attr->form == DW_FORM_ref_sig8);
23959
a2ce51a0 23960 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23961 /* sig_type will be NULL if the signatured type is missing from
23962 the debug info. */
23963 if (sig_type == NULL)
23964 {
23965 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23966 " from DIE at %s [in module %s]"),
23967 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23968 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23969 }
23970
23971 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23972 if (die == NULL)
23973 {
23974 dump_die_for_error (src_die);
23975 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23976 " from DIE at %s [in module %s]"),
23977 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23978 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23979 }
23980
23981 return die;
23982}
23983
23984/* Get the type specified by SIGNATURE referenced in DIE/CU,
23985 reading in and processing the type unit if necessary. */
23986
23987static struct type *
23988get_signatured_type (struct die_info *die, ULONGEST signature,
23989 struct dwarf2_cu *cu)
23990{
518817b3
SM
23991 struct dwarf2_per_objfile *dwarf2_per_objfile
23992 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23993 struct signatured_type *sig_type;
23994 struct dwarf2_cu *type_cu;
23995 struct die_info *type_die;
23996 struct type *type;
23997
a2ce51a0 23998 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23999 /* sig_type will be NULL if the signatured type is missing from
24000 the debug info. */
24001 if (sig_type == NULL)
24002 {
b98664d3 24003 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24004 " from DIE at %s [in module %s]"),
24005 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24006 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24007 return build_error_marker_type (cu, die);
24008 }
24009
24010 /* If we already know the type we're done. */
24011 if (sig_type->type != NULL)
24012 return sig_type->type;
24013
24014 type_cu = cu;
24015 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24016 if (type_die != NULL)
24017 {
24018 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24019 is created. This is important, for example, because for c++ classes
24020 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24021 type = read_type_die (type_die, type_cu);
24022 if (type == NULL)
24023 {
b98664d3 24024 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24025 " referenced from DIE at %s [in module %s]"),
24026 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24027 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24028 type = build_error_marker_type (cu, die);
24029 }
24030 }
24031 else
24032 {
b98664d3 24033 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24034 " from DIE at %s [in module %s]"),
24035 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24036 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24037 type = build_error_marker_type (cu, die);
24038 }
24039 sig_type->type = type;
24040
24041 return type;
24042}
24043
24044/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24045 reading in and processing the type unit if necessary. */
24046
24047static struct type *
ff39bb5e 24048get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24049 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24050{
24051 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 24052 if (attr_form_is_ref (attr))
ac9ec31b
DE
24053 {
24054 struct dwarf2_cu *type_cu = cu;
24055 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24056
24057 return read_type_die (type_die, type_cu);
24058 }
24059 else if (attr->form == DW_FORM_ref_sig8)
24060 {
24061 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24062 }
24063 else
24064 {
518817b3
SM
24065 struct dwarf2_per_objfile *dwarf2_per_objfile
24066 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24067
b98664d3 24068 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24069 " at %s [in module %s]"),
24070 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 24071 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24072 return build_error_marker_type (cu, die);
24073 }
348e048f
DE
24074}
24075
e5fe5e75 24076/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24077
24078static void
e5fe5e75 24079load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 24080{
52dc124a 24081 struct signatured_type *sig_type;
348e048f 24082
f4dc4d17
DE
24083 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24084 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24085
6721b2ec
DE
24086 /* We have the per_cu, but we need the signatured_type.
24087 Fortunately this is an easy translation. */
24088 gdb_assert (per_cu->is_debug_types);
24089 sig_type = (struct signatured_type *) per_cu;
348e048f 24090
6721b2ec 24091 gdb_assert (per_cu->cu == NULL);
348e048f 24092
52dc124a 24093 read_signatured_type (sig_type);
348e048f 24094
6721b2ec 24095 gdb_assert (per_cu->cu != NULL);
348e048f
DE
24096}
24097
dee91e82
DE
24098/* die_reader_func for read_signatured_type.
24099 This is identical to load_full_comp_unit_reader,
24100 but is kept separate for now. */
348e048f
DE
24101
24102static void
dee91e82 24103read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 24104 const gdb_byte *info_ptr,
dee91e82
DE
24105 struct die_info *comp_unit_die,
24106 int has_children,
24107 void *data)
348e048f 24108{
dee91e82 24109 struct dwarf2_cu *cu = reader->cu;
348e048f 24110
dee91e82
DE
24111 gdb_assert (cu->die_hash == NULL);
24112 cu->die_hash =
24113 htab_create_alloc_ex (cu->header.length / 12,
24114 die_hash,
24115 die_eq,
24116 NULL,
24117 &cu->comp_unit_obstack,
24118 hashtab_obstack_allocate,
24119 dummy_obstack_deallocate);
348e048f 24120
dee91e82
DE
24121 if (has_children)
24122 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24123 &info_ptr, comp_unit_die);
24124 cu->dies = comp_unit_die;
24125 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
24126
24127 /* We try not to read any attributes in this function, because not
9cdd5dbd 24128 all CUs needed for references have been loaded yet, and symbol
348e048f 24129 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
24130 or we won't be able to build types correctly.
24131 Similarly, if we do not read the producer, we can not apply
24132 producer-specific interpretation. */
95554aad 24133 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 24134}
348e048f 24135
3019eac3
DE
24136/* Read in a signatured type and build its CU and DIEs.
24137 If the type is a stub for the real type in a DWO file,
24138 read in the real type from the DWO file as well. */
dee91e82
DE
24139
24140static void
24141read_signatured_type (struct signatured_type *sig_type)
24142{
24143 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24144
3019eac3 24145 gdb_assert (per_cu->is_debug_types);
dee91e82 24146 gdb_assert (per_cu->cu == NULL);
348e048f 24147
58f0c718 24148 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 24149 read_signatured_type_reader, NULL);
7ee85ab1 24150 sig_type->per_cu.tu_read = 1;
c906108c
SS
24151}
24152
c906108c
SS
24153/* Decode simple location descriptions.
24154 Given a pointer to a dwarf block that defines a location, compute
24155 the location and return the value.
24156
4cecd739
DJ
24157 NOTE drow/2003-11-18: This function is called in two situations
24158 now: for the address of static or global variables (partial symbols
24159 only) and for offsets into structures which are expected to be
24160 (more or less) constant. The partial symbol case should go away,
24161 and only the constant case should remain. That will let this
24162 function complain more accurately. A few special modes are allowed
24163 without complaint for global variables (for instance, global
24164 register values and thread-local values).
c906108c
SS
24165
24166 A location description containing no operations indicates that the
4cecd739 24167 object is optimized out. The return value is 0 for that case.
6b992462
DJ
24168 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24169 callers will only want a very basic result and this can become a
21ae7a4d
JK
24170 complaint.
24171
24172 Note that stack[0] is unused except as a default error return. */
c906108c
SS
24173
24174static CORE_ADDR
e7c27a73 24175decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 24176{
518817b3 24177 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
24178 size_t i;
24179 size_t size = blk->size;
d521ce57 24180 const gdb_byte *data = blk->data;
21ae7a4d
JK
24181 CORE_ADDR stack[64];
24182 int stacki;
24183 unsigned int bytes_read, unsnd;
24184 gdb_byte op;
c906108c 24185
21ae7a4d
JK
24186 i = 0;
24187 stacki = 0;
24188 stack[stacki] = 0;
24189 stack[++stacki] = 0;
24190
24191 while (i < size)
24192 {
24193 op = data[i++];
24194 switch (op)
24195 {
24196 case DW_OP_lit0:
24197 case DW_OP_lit1:
24198 case DW_OP_lit2:
24199 case DW_OP_lit3:
24200 case DW_OP_lit4:
24201 case DW_OP_lit5:
24202 case DW_OP_lit6:
24203 case DW_OP_lit7:
24204 case DW_OP_lit8:
24205 case DW_OP_lit9:
24206 case DW_OP_lit10:
24207 case DW_OP_lit11:
24208 case DW_OP_lit12:
24209 case DW_OP_lit13:
24210 case DW_OP_lit14:
24211 case DW_OP_lit15:
24212 case DW_OP_lit16:
24213 case DW_OP_lit17:
24214 case DW_OP_lit18:
24215 case DW_OP_lit19:
24216 case DW_OP_lit20:
24217 case DW_OP_lit21:
24218 case DW_OP_lit22:
24219 case DW_OP_lit23:
24220 case DW_OP_lit24:
24221 case DW_OP_lit25:
24222 case DW_OP_lit26:
24223 case DW_OP_lit27:
24224 case DW_OP_lit28:
24225 case DW_OP_lit29:
24226 case DW_OP_lit30:
24227 case DW_OP_lit31:
24228 stack[++stacki] = op - DW_OP_lit0;
24229 break;
f1bea926 24230
21ae7a4d
JK
24231 case DW_OP_reg0:
24232 case DW_OP_reg1:
24233 case DW_OP_reg2:
24234 case DW_OP_reg3:
24235 case DW_OP_reg4:
24236 case DW_OP_reg5:
24237 case DW_OP_reg6:
24238 case DW_OP_reg7:
24239 case DW_OP_reg8:
24240 case DW_OP_reg9:
24241 case DW_OP_reg10:
24242 case DW_OP_reg11:
24243 case DW_OP_reg12:
24244 case DW_OP_reg13:
24245 case DW_OP_reg14:
24246 case DW_OP_reg15:
24247 case DW_OP_reg16:
24248 case DW_OP_reg17:
24249 case DW_OP_reg18:
24250 case DW_OP_reg19:
24251 case DW_OP_reg20:
24252 case DW_OP_reg21:
24253 case DW_OP_reg22:
24254 case DW_OP_reg23:
24255 case DW_OP_reg24:
24256 case DW_OP_reg25:
24257 case DW_OP_reg26:
24258 case DW_OP_reg27:
24259 case DW_OP_reg28:
24260 case DW_OP_reg29:
24261 case DW_OP_reg30:
24262 case DW_OP_reg31:
24263 stack[++stacki] = op - DW_OP_reg0;
24264 if (i < size)
24265 dwarf2_complex_location_expr_complaint ();
24266 break;
c906108c 24267
21ae7a4d
JK
24268 case DW_OP_regx:
24269 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24270 i += bytes_read;
24271 stack[++stacki] = unsnd;
24272 if (i < size)
24273 dwarf2_complex_location_expr_complaint ();
24274 break;
c906108c 24275
21ae7a4d
JK
24276 case DW_OP_addr:
24277 stack[++stacki] = read_address (objfile->obfd, &data[i],
24278 cu, &bytes_read);
24279 i += bytes_read;
24280 break;
d53d4ac5 24281
21ae7a4d
JK
24282 case DW_OP_const1u:
24283 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24284 i += 1;
24285 break;
24286
24287 case DW_OP_const1s:
24288 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24289 i += 1;
24290 break;
24291
24292 case DW_OP_const2u:
24293 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24294 i += 2;
24295 break;
24296
24297 case DW_OP_const2s:
24298 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24299 i += 2;
24300 break;
d53d4ac5 24301
21ae7a4d
JK
24302 case DW_OP_const4u:
24303 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24304 i += 4;
24305 break;
24306
24307 case DW_OP_const4s:
24308 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24309 i += 4;
24310 break;
24311
585861ea
JK
24312 case DW_OP_const8u:
24313 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24314 i += 8;
24315 break;
24316
21ae7a4d
JK
24317 case DW_OP_constu:
24318 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24319 &bytes_read);
24320 i += bytes_read;
24321 break;
24322
24323 case DW_OP_consts:
24324 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24325 i += bytes_read;
24326 break;
24327
24328 case DW_OP_dup:
24329 stack[stacki + 1] = stack[stacki];
24330 stacki++;
24331 break;
24332
24333 case DW_OP_plus:
24334 stack[stacki - 1] += stack[stacki];
24335 stacki--;
24336 break;
24337
24338 case DW_OP_plus_uconst:
24339 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24340 &bytes_read);
24341 i += bytes_read;
24342 break;
24343
24344 case DW_OP_minus:
24345 stack[stacki - 1] -= stack[stacki];
24346 stacki--;
24347 break;
24348
24349 case DW_OP_deref:
24350 /* If we're not the last op, then we definitely can't encode
24351 this using GDB's address_class enum. This is valid for partial
24352 global symbols, although the variable's address will be bogus
24353 in the psymtab. */
24354 if (i < size)
24355 dwarf2_complex_location_expr_complaint ();
24356 break;
24357
24358 case DW_OP_GNU_push_tls_address:
4aa4e28b 24359 case DW_OP_form_tls_address:
21ae7a4d
JK
24360 /* The top of the stack has the offset from the beginning
24361 of the thread control block at which the variable is located. */
24362 /* Nothing should follow this operator, so the top of stack would
24363 be returned. */
24364 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24365 address will be bogus in the psymtab. Make it always at least
24366 non-zero to not look as a variable garbage collected by linker
24367 which have DW_OP_addr 0. */
21ae7a4d
JK
24368 if (i < size)
24369 dwarf2_complex_location_expr_complaint ();
585861ea 24370 stack[stacki]++;
21ae7a4d
JK
24371 break;
24372
24373 case DW_OP_GNU_uninit:
24374 break;
24375
336d760d 24376 case DW_OP_addrx:
3019eac3 24377 case DW_OP_GNU_addr_index:
49f6c839 24378 case DW_OP_GNU_const_index:
3019eac3
DE
24379 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24380 &bytes_read);
24381 i += bytes_read;
24382 break;
24383
21ae7a4d
JK
24384 default:
24385 {
f39c6ffd 24386 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24387
24388 if (name)
b98664d3 24389 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24390 name);
24391 else
b98664d3 24392 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24393 op);
24394 }
24395
24396 return (stack[stacki]);
d53d4ac5 24397 }
3c6e0cb3 24398
21ae7a4d
JK
24399 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24400 outside of the allocated space. Also enforce minimum>0. */
24401 if (stacki >= ARRAY_SIZE (stack) - 1)
24402 {
b98664d3 24403 complaint (_("location description stack overflow"));
21ae7a4d
JK
24404 return 0;
24405 }
24406
24407 if (stacki <= 0)
24408 {
b98664d3 24409 complaint (_("location description stack underflow"));
21ae7a4d
JK
24410 return 0;
24411 }
24412 }
24413 return (stack[stacki]);
c906108c
SS
24414}
24415
24416/* memory allocation interface */
24417
c906108c 24418static struct dwarf_block *
7b5a2f43 24419dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24420{
8d749320 24421 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24422}
24423
c906108c 24424static struct die_info *
b60c80d6 24425dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24426{
24427 struct die_info *die;
b60c80d6
DJ
24428 size_t size = sizeof (struct die_info);
24429
24430 if (num_attrs > 1)
24431 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24432
b60c80d6 24433 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24434 memset (die, 0, sizeof (struct die_info));
24435 return (die);
24436}
2e276125
JB
24437
24438\f
24439/* Macro support. */
24440
233d95b5
JK
24441/* Return file name relative to the compilation directory of file number I in
24442 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24443 responsible for freeing it. */
233d95b5 24444
2e276125 24445static char *
233d95b5 24446file_file_name (int file, struct line_header *lh)
2e276125 24447{
6a83a1e6
EZ
24448 /* Is the file number a valid index into the line header's file name
24449 table? Remember that file numbers start with one, not zero. */
7ba99d21 24450 if (lh->is_valid_file_index (file))
6a83a1e6 24451 {
7ba99d21 24452 const file_entry *fe = lh->file_name_at (file);
6e70227d 24453
7ba99d21 24454 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24455 {
7ba99d21 24456 const char *dir = fe->include_dir (lh);
8c43009f 24457 if (dir != NULL)
7ba99d21 24458 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24459 }
7ba99d21 24460 return xstrdup (fe->name);
6a83a1e6 24461 }
2e276125
JB
24462 else
24463 {
6a83a1e6
EZ
24464 /* The compiler produced a bogus file number. We can at least
24465 record the macro definitions made in the file, even if we
24466 won't be able to find the file by name. */
24467 char fake_name[80];
9a619af0 24468
8c042590
PM
24469 xsnprintf (fake_name, sizeof (fake_name),
24470 "<bad macro file number %d>", file);
2e276125 24471
b98664d3 24472 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24473 file);
2e276125 24474
6a83a1e6 24475 return xstrdup (fake_name);
2e276125
JB
24476 }
24477}
24478
233d95b5
JK
24479/* Return the full name of file number I in *LH's file name table.
24480 Use COMP_DIR as the name of the current directory of the
24481 compilation. The result is allocated using xmalloc; the caller is
24482 responsible for freeing it. */
24483static char *
24484file_full_name (int file, struct line_header *lh, const char *comp_dir)
24485{
24486 /* Is the file number a valid index into the line header's file name
24487 table? Remember that file numbers start with one, not zero. */
7ba99d21 24488 if (lh->is_valid_file_index (file))
233d95b5
JK
24489 {
24490 char *relative = file_file_name (file, lh);
24491
24492 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24493 return relative;
b36cec19
PA
24494 return reconcat (relative, comp_dir, SLASH_STRING,
24495 relative, (char *) NULL);
233d95b5
JK
24496 }
24497 else
24498 return file_file_name (file, lh);
24499}
24500
2e276125
JB
24501
24502static struct macro_source_file *
804d2729
TT
24503macro_start_file (struct dwarf2_cu *cu,
24504 int file, int line,
2e276125 24505 struct macro_source_file *current_file,
43f3e411 24506 struct line_header *lh)
2e276125 24507{
233d95b5
JK
24508 /* File name relative to the compilation directory of this source file. */
24509 char *file_name = file_file_name (file, lh);
2e276125 24510
2e276125 24511 if (! current_file)
abc9d0dc 24512 {
fc474241
DE
24513 /* Note: We don't create a macro table for this compilation unit
24514 at all until we actually get a filename. */
c24bdb02 24515 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24516
abc9d0dc
TT
24517 /* If we have no current file, then this must be the start_file
24518 directive for the compilation unit's main source file. */
fc474241
DE
24519 current_file = macro_set_main (macro_table, file_name);
24520 macro_define_special (macro_table);
abc9d0dc 24521 }
2e276125 24522 else
233d95b5 24523 current_file = macro_include (current_file, line, file_name);
2e276125 24524
233d95b5 24525 xfree (file_name);
6e70227d 24526
2e276125
JB
24527 return current_file;
24528}
24529
2e276125
JB
24530static const char *
24531consume_improper_spaces (const char *p, const char *body)
24532{
24533 if (*p == ' ')
24534 {
b98664d3 24535 complaint (_("macro definition contains spaces "
3e43a32a 24536 "in formal argument list:\n`%s'"),
4d3c2250 24537 body);
2e276125
JB
24538
24539 while (*p == ' ')
24540 p++;
24541 }
24542
24543 return p;
24544}
24545
24546
24547static void
24548parse_macro_definition (struct macro_source_file *file, int line,
24549 const char *body)
24550{
24551 const char *p;
24552
24553 /* The body string takes one of two forms. For object-like macro
24554 definitions, it should be:
24555
24556 <macro name> " " <definition>
24557
24558 For function-like macro definitions, it should be:
24559
24560 <macro name> "() " <definition>
24561 or
24562 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24563
24564 Spaces may appear only where explicitly indicated, and in the
24565 <definition>.
24566
24567 The Dwarf 2 spec says that an object-like macro's name is always
24568 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24569 the space when the macro's definition is the empty string.
2e276125
JB
24570
24571 The Dwarf 2 spec says that there should be no spaces between the
24572 formal arguments in a function-like macro's formal argument list,
24573 but versions of GCC around March 2002 include spaces after the
24574 commas. */
24575
24576
24577 /* Find the extent of the macro name. The macro name is terminated
24578 by either a space or null character (for an object-like macro) or
24579 an opening paren (for a function-like macro). */
24580 for (p = body; *p; p++)
24581 if (*p == ' ' || *p == '(')
24582 break;
24583
24584 if (*p == ' ' || *p == '\0')
24585 {
24586 /* It's an object-like macro. */
24587 int name_len = p - body;
3f8a7804 24588 char *name = savestring (body, name_len);
2e276125
JB
24589 const char *replacement;
24590
24591 if (*p == ' ')
24592 replacement = body + name_len + 1;
24593 else
24594 {
4d3c2250 24595 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24596 replacement = body + name_len;
24597 }
6e70227d 24598
2e276125
JB
24599 macro_define_object (file, line, name, replacement);
24600
24601 xfree (name);
24602 }
24603 else if (*p == '(')
24604 {
24605 /* It's a function-like macro. */
3f8a7804 24606 char *name = savestring (body, p - body);
2e276125
JB
24607 int argc = 0;
24608 int argv_size = 1;
8d749320 24609 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24610
24611 p++;
24612
24613 p = consume_improper_spaces (p, body);
24614
24615 /* Parse the formal argument list. */
24616 while (*p && *p != ')')
24617 {
24618 /* Find the extent of the current argument name. */
24619 const char *arg_start = p;
24620
24621 while (*p && *p != ',' && *p != ')' && *p != ' ')
24622 p++;
24623
24624 if (! *p || p == arg_start)
4d3c2250 24625 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24626 else
24627 {
24628 /* Make sure argv has room for the new argument. */
24629 if (argc >= argv_size)
24630 {
24631 argv_size *= 2;
224c3ddb 24632 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24633 }
24634
3f8a7804 24635 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24636 }
24637
24638 p = consume_improper_spaces (p, body);
24639
24640 /* Consume the comma, if present. */
24641 if (*p == ',')
24642 {
24643 p++;
24644
24645 p = consume_improper_spaces (p, body);
24646 }
24647 }
24648
24649 if (*p == ')')
24650 {
24651 p++;
24652
24653 if (*p == ' ')
24654 /* Perfectly formed definition, no complaints. */
24655 macro_define_function (file, line, name,
6e70227d 24656 argc, (const char **) argv,
2e276125
JB
24657 p + 1);
24658 else if (*p == '\0')
24659 {
24660 /* Complain, but do define it. */
4d3c2250 24661 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24662 macro_define_function (file, line, name,
6e70227d 24663 argc, (const char **) argv,
2e276125
JB
24664 p);
24665 }
24666 else
24667 /* Just complain. */
4d3c2250 24668 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24669 }
24670 else
24671 /* Just complain. */
4d3c2250 24672 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24673
24674 xfree (name);
24675 {
24676 int i;
24677
24678 for (i = 0; i < argc; i++)
24679 xfree (argv[i]);
24680 }
24681 xfree (argv);
24682 }
24683 else
4d3c2250 24684 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24685}
24686
cf2c3c16
TT
24687/* Skip some bytes from BYTES according to the form given in FORM.
24688 Returns the new pointer. */
2e276125 24689
d521ce57
TT
24690static const gdb_byte *
24691skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24692 enum dwarf_form form,
24693 unsigned int offset_size,
24694 struct dwarf2_section_info *section)
2e276125 24695{
cf2c3c16 24696 unsigned int bytes_read;
2e276125 24697
cf2c3c16 24698 switch (form)
2e276125 24699 {
cf2c3c16
TT
24700 case DW_FORM_data1:
24701 case DW_FORM_flag:
24702 ++bytes;
24703 break;
24704
24705 case DW_FORM_data2:
24706 bytes += 2;
24707 break;
24708
24709 case DW_FORM_data4:
24710 bytes += 4;
24711 break;
24712
24713 case DW_FORM_data8:
24714 bytes += 8;
24715 break;
24716
0224619f
JK
24717 case DW_FORM_data16:
24718 bytes += 16;
24719 break;
24720
cf2c3c16
TT
24721 case DW_FORM_string:
24722 read_direct_string (abfd, bytes, &bytes_read);
24723 bytes += bytes_read;
24724 break;
24725
24726 case DW_FORM_sec_offset:
24727 case DW_FORM_strp:
36586728 24728 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24729 bytes += offset_size;
24730 break;
24731
24732 case DW_FORM_block:
24733 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24734 bytes += bytes_read;
24735 break;
24736
24737 case DW_FORM_block1:
24738 bytes += 1 + read_1_byte (abfd, bytes);
24739 break;
24740 case DW_FORM_block2:
24741 bytes += 2 + read_2_bytes (abfd, bytes);
24742 break;
24743 case DW_FORM_block4:
24744 bytes += 4 + read_4_bytes (abfd, bytes);
24745 break;
24746
336d760d 24747 case DW_FORM_addrx:
cf2c3c16 24748 case DW_FORM_sdata:
cf532bd1 24749 case DW_FORM_strx:
cf2c3c16 24750 case DW_FORM_udata:
3019eac3
DE
24751 case DW_FORM_GNU_addr_index:
24752 case DW_FORM_GNU_str_index:
d521ce57 24753 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24754 if (bytes == NULL)
24755 {
24756 dwarf2_section_buffer_overflow_complaint (section);
24757 return NULL;
24758 }
cf2c3c16
TT
24759 break;
24760
663c44ac
JK
24761 case DW_FORM_implicit_const:
24762 break;
24763
cf2c3c16
TT
24764 default:
24765 {
b98664d3 24766 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24767 form, get_section_name (section));
cf2c3c16
TT
24768 return NULL;
24769 }
2e276125
JB
24770 }
24771
cf2c3c16
TT
24772 return bytes;
24773}
757a13d0 24774
cf2c3c16
TT
24775/* A helper for dwarf_decode_macros that handles skipping an unknown
24776 opcode. Returns an updated pointer to the macro data buffer; or,
24777 on error, issues a complaint and returns NULL. */
757a13d0 24778
d521ce57 24779static const gdb_byte *
cf2c3c16 24780skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24781 const gdb_byte **opcode_definitions,
24782 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24783 bfd *abfd,
24784 unsigned int offset_size,
24785 struct dwarf2_section_info *section)
24786{
24787 unsigned int bytes_read, i;
24788 unsigned long arg;
d521ce57 24789 const gdb_byte *defn;
2e276125 24790
cf2c3c16 24791 if (opcode_definitions[opcode] == NULL)
2e276125 24792 {
b98664d3 24793 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24794 opcode);
24795 return NULL;
24796 }
2e276125 24797
cf2c3c16
TT
24798 defn = opcode_definitions[opcode];
24799 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24800 defn += bytes_read;
2e276125 24801
cf2c3c16
TT
24802 for (i = 0; i < arg; ++i)
24803 {
aead7601
SM
24804 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24805 (enum dwarf_form) defn[i], offset_size,
f664829e 24806 section);
cf2c3c16
TT
24807 if (mac_ptr == NULL)
24808 {
24809 /* skip_form_bytes already issued the complaint. */
24810 return NULL;
24811 }
24812 }
757a13d0 24813
cf2c3c16
TT
24814 return mac_ptr;
24815}
757a13d0 24816
cf2c3c16
TT
24817/* A helper function which parses the header of a macro section.
24818 If the macro section is the extended (for now called "GNU") type,
24819 then this updates *OFFSET_SIZE. Returns a pointer to just after
24820 the header, or issues a complaint and returns NULL on error. */
757a13d0 24821
d521ce57
TT
24822static const gdb_byte *
24823dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24824 bfd *abfd,
d521ce57 24825 const gdb_byte *mac_ptr,
cf2c3c16
TT
24826 unsigned int *offset_size,
24827 int section_is_gnu)
24828{
24829 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24830
cf2c3c16
TT
24831 if (section_is_gnu)
24832 {
24833 unsigned int version, flags;
757a13d0 24834
cf2c3c16 24835 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24836 if (version != 4 && version != 5)
cf2c3c16 24837 {
b98664d3 24838 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24839 version);
24840 return NULL;
24841 }
24842 mac_ptr += 2;
757a13d0 24843
cf2c3c16
TT
24844 flags = read_1_byte (abfd, mac_ptr);
24845 ++mac_ptr;
24846 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24847
cf2c3c16
TT
24848 if ((flags & 2) != 0)
24849 /* We don't need the line table offset. */
24850 mac_ptr += *offset_size;
757a13d0 24851
cf2c3c16
TT
24852 /* Vendor opcode descriptions. */
24853 if ((flags & 4) != 0)
24854 {
24855 unsigned int i, count;
757a13d0 24856
cf2c3c16
TT
24857 count = read_1_byte (abfd, mac_ptr);
24858 ++mac_ptr;
24859 for (i = 0; i < count; ++i)
24860 {
24861 unsigned int opcode, bytes_read;
24862 unsigned long arg;
24863
24864 opcode = read_1_byte (abfd, mac_ptr);
24865 ++mac_ptr;
24866 opcode_definitions[opcode] = mac_ptr;
24867 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24868 mac_ptr += bytes_read;
24869 mac_ptr += arg;
24870 }
757a13d0 24871 }
cf2c3c16 24872 }
757a13d0 24873
cf2c3c16
TT
24874 return mac_ptr;
24875}
757a13d0 24876
cf2c3c16 24877/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24878 including DW_MACRO_import. */
cf2c3c16
TT
24879
24880static void
804d2729 24881dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24882 bfd *abfd,
d521ce57 24883 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24884 struct macro_source_file *current_file,
43f3e411 24885 struct line_header *lh,
cf2c3c16 24886 struct dwarf2_section_info *section,
36586728 24887 int section_is_gnu, int section_is_dwz,
cf2c3c16 24888 unsigned int offset_size,
8fc3fc34 24889 htab_t include_hash)
cf2c3c16 24890{
804d2729
TT
24891 struct dwarf2_per_objfile *dwarf2_per_objfile
24892 = cu->per_cu->dwarf2_per_objfile;
4d663531 24893 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24894 enum dwarf_macro_record_type macinfo_type;
24895 int at_commandline;
d521ce57 24896 const gdb_byte *opcode_definitions[256];
757a13d0 24897
cf2c3c16
TT
24898 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24899 &offset_size, section_is_gnu);
24900 if (mac_ptr == NULL)
24901 {
24902 /* We already issued a complaint. */
24903 return;
24904 }
757a13d0
JK
24905
24906 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24907 GDB is still reading the definitions from command line. First
24908 DW_MACINFO_start_file will need to be ignored as it was already executed
24909 to create CURRENT_FILE for the main source holding also the command line
24910 definitions. On first met DW_MACINFO_start_file this flag is reset to
24911 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24912
24913 at_commandline = 1;
24914
24915 do
24916 {
24917 /* Do we at least have room for a macinfo type byte? */
24918 if (mac_ptr >= mac_end)
24919 {
f664829e 24920 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24921 break;
24922 }
24923
aead7601 24924 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24925 mac_ptr++;
24926
cf2c3c16
TT
24927 /* Note that we rely on the fact that the corresponding GNU and
24928 DWARF constants are the same. */
132448f8
SM
24929 DIAGNOSTIC_PUSH
24930 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24931 switch (macinfo_type)
24932 {
24933 /* A zero macinfo type indicates the end of the macro
24934 information. */
24935 case 0:
24936 break;
2e276125 24937
0af92d60
JK
24938 case DW_MACRO_define:
24939 case DW_MACRO_undef:
24940 case DW_MACRO_define_strp:
24941 case DW_MACRO_undef_strp:
24942 case DW_MACRO_define_sup:
24943 case DW_MACRO_undef_sup:
2e276125 24944 {
891d2f0b 24945 unsigned int bytes_read;
2e276125 24946 int line;
d521ce57 24947 const char *body;
cf2c3c16 24948 int is_define;
2e276125 24949
cf2c3c16
TT
24950 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24951 mac_ptr += bytes_read;
24952
0af92d60
JK
24953 if (macinfo_type == DW_MACRO_define
24954 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24955 {
24956 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24957 mac_ptr += bytes_read;
24958 }
24959 else
24960 {
24961 LONGEST str_offset;
24962
24963 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24964 mac_ptr += offset_size;
2e276125 24965
0af92d60
JK
24966 if (macinfo_type == DW_MACRO_define_sup
24967 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24968 || section_is_dwz)
36586728 24969 {
ed2dc618
SM
24970 struct dwz_file *dwz
24971 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24972
ed2dc618
SM
24973 body = read_indirect_string_from_dwz (objfile,
24974 dwz, str_offset);
36586728
TT
24975 }
24976 else
ed2dc618
SM
24977 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24978 abfd, str_offset);
cf2c3c16
TT
24979 }
24980
0af92d60
JK
24981 is_define = (macinfo_type == DW_MACRO_define
24982 || macinfo_type == DW_MACRO_define_strp
24983 || macinfo_type == DW_MACRO_define_sup);
2e276125 24984 if (! current_file)
757a13d0
JK
24985 {
24986 /* DWARF violation as no main source is present. */
b98664d3 24987 complaint (_("debug info with no main source gives macro %s "
757a13d0 24988 "on line %d: %s"),
cf2c3c16
TT
24989 is_define ? _("definition") : _("undefinition"),
24990 line, body);
757a13d0
JK
24991 break;
24992 }
3e43a32a
MS
24993 if ((line == 0 && !at_commandline)
24994 || (line != 0 && at_commandline))
b98664d3 24995 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24996 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24997 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24998 line == 0 ? _("zero") : _("non-zero"), line, body);
24999
955b06fa 25000 if (body == NULL)
7bede828 25001 {
955b06fa
SDJ
25002 /* Fedora's rpm-build's "debugedit" binary
25003 corrupted .debug_macro sections.
25004
25005 For more info, see
25006 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
25007 complaint (_("debug info gives %s invalid macro %s "
25008 "without body (corrupted?) at line %d "
25009 "on file %s"),
25010 at_commandline ? _("command-line") : _("in-file"),
25011 is_define ? _("definition") : _("undefinition"),
25012 line, current_file->filename);
7bede828 25013 }
955b06fa
SDJ
25014 else if (is_define)
25015 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
25016 else
25017 {
0af92d60
JK
25018 gdb_assert (macinfo_type == DW_MACRO_undef
25019 || macinfo_type == DW_MACRO_undef_strp
25020 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
25021 macro_undef (current_file, line, body);
25022 }
2e276125
JB
25023 }
25024 break;
25025
0af92d60 25026 case DW_MACRO_start_file:
2e276125 25027 {
891d2f0b 25028 unsigned int bytes_read;
2e276125
JB
25029 int line, file;
25030
25031 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25032 mac_ptr += bytes_read;
25033 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25034 mac_ptr += bytes_read;
25035
3e43a32a
MS
25036 if ((line == 0 && !at_commandline)
25037 || (line != 0 && at_commandline))
b98664d3 25038 complaint (_("debug info gives source %d included "
757a13d0
JK
25039 "from %s at %s line %d"),
25040 file, at_commandline ? _("command-line") : _("file"),
25041 line == 0 ? _("zero") : _("non-zero"), line);
25042
25043 if (at_commandline)
25044 {
0af92d60 25045 /* This DW_MACRO_start_file was executed in the
cf2c3c16 25046 pass one. */
757a13d0
JK
25047 at_commandline = 0;
25048 }
25049 else
804d2729
TT
25050 current_file = macro_start_file (cu, file, line, current_file,
25051 lh);
2e276125
JB
25052 }
25053 break;
25054
0af92d60 25055 case DW_MACRO_end_file:
2e276125 25056 if (! current_file)
b98664d3 25057 complaint (_("macro debug info has an unmatched "
3e43a32a 25058 "`close_file' directive"));
2e276125
JB
25059 else
25060 {
25061 current_file = current_file->included_by;
25062 if (! current_file)
25063 {
cf2c3c16 25064 enum dwarf_macro_record_type next_type;
2e276125
JB
25065
25066 /* GCC circa March 2002 doesn't produce the zero
25067 type byte marking the end of the compilation
25068 unit. Complain if it's not there, but exit no
25069 matter what. */
25070
25071 /* Do we at least have room for a macinfo type byte? */
25072 if (mac_ptr >= mac_end)
25073 {
f664829e 25074 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
25075 return;
25076 }
25077
25078 /* We don't increment mac_ptr here, so this is just
25079 a look-ahead. */
aead7601
SM
25080 next_type
25081 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25082 mac_ptr);
2e276125 25083 if (next_type != 0)
b98664d3 25084 complaint (_("no terminating 0-type entry for "
3e43a32a 25085 "macros in `.debug_macinfo' section"));
2e276125
JB
25086
25087 return;
25088 }
25089 }
25090 break;
25091
0af92d60
JK
25092 case DW_MACRO_import:
25093 case DW_MACRO_import_sup:
cf2c3c16
TT
25094 {
25095 LONGEST offset;
8fc3fc34 25096 void **slot;
a036ba48
TT
25097 bfd *include_bfd = abfd;
25098 struct dwarf2_section_info *include_section = section;
d521ce57 25099 const gdb_byte *include_mac_end = mac_end;
a036ba48 25100 int is_dwz = section_is_dwz;
d521ce57 25101 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
25102
25103 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25104 mac_ptr += offset_size;
25105
0af92d60 25106 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 25107 {
ed2dc618 25108 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 25109
4d663531 25110 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 25111
a036ba48 25112 include_section = &dwz->macro;
a32a8923 25113 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
25114 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25115 is_dwz = 1;
25116 }
25117
25118 new_mac_ptr = include_section->buffer + offset;
25119 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25120
8fc3fc34
TT
25121 if (*slot != NULL)
25122 {
25123 /* This has actually happened; see
25124 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 25125 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
25126 ".debug_macro section"));
25127 }
25128 else
25129 {
d521ce57 25130 *slot = (void *) new_mac_ptr;
36586728 25131
804d2729 25132 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 25133 include_mac_end, current_file, lh,
36586728 25134 section, section_is_gnu, is_dwz,
4d663531 25135 offset_size, include_hash);
8fc3fc34 25136
d521ce57 25137 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 25138 }
cf2c3c16
TT
25139 }
25140 break;
25141
2e276125 25142 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
25143 if (!section_is_gnu)
25144 {
25145 unsigned int bytes_read;
2e276125 25146
ac298888
TT
25147 /* This reads the constant, but since we don't recognize
25148 any vendor extensions, we ignore it. */
25149 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
25150 mac_ptr += bytes_read;
25151 read_direct_string (abfd, mac_ptr, &bytes_read);
25152 mac_ptr += bytes_read;
2e276125 25153
cf2c3c16
TT
25154 /* We don't recognize any vendor extensions. */
25155 break;
25156 }
25157 /* FALLTHROUGH */
25158
25159 default:
25160 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25161 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25162 section);
25163 if (mac_ptr == NULL)
25164 return;
25165 break;
2e276125 25166 }
132448f8 25167 DIAGNOSTIC_POP
757a13d0 25168 } while (macinfo_type != 0);
2e276125 25169}
8e19ed76 25170
cf2c3c16 25171static void
09262596 25172dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 25173 int section_is_gnu)
cf2c3c16 25174{
518817b3
SM
25175 struct dwarf2_per_objfile *dwarf2_per_objfile
25176 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25177 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
25178 struct line_header *lh = cu->line_header;
25179 bfd *abfd;
d521ce57 25180 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
25181 struct macro_source_file *current_file = 0;
25182 enum dwarf_macro_record_type macinfo_type;
25183 unsigned int offset_size = cu->header.offset_size;
d521ce57 25184 const gdb_byte *opcode_definitions[256];
8fc3fc34 25185 void **slot;
09262596
DE
25186 struct dwarf2_section_info *section;
25187 const char *section_name;
25188
25189 if (cu->dwo_unit != NULL)
25190 {
25191 if (section_is_gnu)
25192 {
25193 section = &cu->dwo_unit->dwo_file->sections.macro;
25194 section_name = ".debug_macro.dwo";
25195 }
25196 else
25197 {
25198 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25199 section_name = ".debug_macinfo.dwo";
25200 }
25201 }
25202 else
25203 {
25204 if (section_is_gnu)
25205 {
25206 section = &dwarf2_per_objfile->macro;
25207 section_name = ".debug_macro";
25208 }
25209 else
25210 {
25211 section = &dwarf2_per_objfile->macinfo;
25212 section_name = ".debug_macinfo";
25213 }
25214 }
cf2c3c16 25215
bb5ed363 25216 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25217 if (section->buffer == NULL)
25218 {
b98664d3 25219 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
25220 return;
25221 }
a32a8923 25222 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25223
25224 /* First pass: Find the name of the base filename.
25225 This filename is needed in order to process all macros whose definition
25226 (or undefinition) comes from the command line. These macros are defined
25227 before the first DW_MACINFO_start_file entry, and yet still need to be
25228 associated to the base file.
25229
25230 To determine the base file name, we scan the macro definitions until we
25231 reach the first DW_MACINFO_start_file entry. We then initialize
25232 CURRENT_FILE accordingly so that any macro definition found before the
25233 first DW_MACINFO_start_file can still be associated to the base file. */
25234
25235 mac_ptr = section->buffer + offset;
25236 mac_end = section->buffer + section->size;
25237
25238 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25239 &offset_size, section_is_gnu);
25240 if (mac_ptr == NULL)
25241 {
25242 /* We already issued a complaint. */
25243 return;
25244 }
25245
25246 do
25247 {
25248 /* Do we at least have room for a macinfo type byte? */
25249 if (mac_ptr >= mac_end)
25250 {
25251 /* Complaint is printed during the second pass as GDB will probably
25252 stop the first pass earlier upon finding
25253 DW_MACINFO_start_file. */
25254 break;
25255 }
25256
aead7601 25257 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25258 mac_ptr++;
25259
25260 /* Note that we rely on the fact that the corresponding GNU and
25261 DWARF constants are the same. */
132448f8
SM
25262 DIAGNOSTIC_PUSH
25263 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25264 switch (macinfo_type)
25265 {
25266 /* A zero macinfo type indicates the end of the macro
25267 information. */
25268 case 0:
25269 break;
25270
0af92d60
JK
25271 case DW_MACRO_define:
25272 case DW_MACRO_undef:
cf2c3c16
TT
25273 /* Only skip the data by MAC_PTR. */
25274 {
25275 unsigned int bytes_read;
25276
25277 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25278 mac_ptr += bytes_read;
25279 read_direct_string (abfd, mac_ptr, &bytes_read);
25280 mac_ptr += bytes_read;
25281 }
25282 break;
25283
0af92d60 25284 case DW_MACRO_start_file:
cf2c3c16
TT
25285 {
25286 unsigned int bytes_read;
25287 int line, file;
25288
25289 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25290 mac_ptr += bytes_read;
25291 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25292 mac_ptr += bytes_read;
25293
804d2729 25294 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25295 }
25296 break;
25297
0af92d60 25298 case DW_MACRO_end_file:
cf2c3c16
TT
25299 /* No data to skip by MAC_PTR. */
25300 break;
25301
0af92d60
JK
25302 case DW_MACRO_define_strp:
25303 case DW_MACRO_undef_strp:
25304 case DW_MACRO_define_sup:
25305 case DW_MACRO_undef_sup:
cf2c3c16
TT
25306 {
25307 unsigned int bytes_read;
25308
25309 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25310 mac_ptr += bytes_read;
25311 mac_ptr += offset_size;
25312 }
25313 break;
25314
0af92d60
JK
25315 case DW_MACRO_import:
25316 case DW_MACRO_import_sup:
cf2c3c16 25317 /* Note that, according to the spec, a transparent include
0af92d60 25318 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25319 skip this opcode. */
25320 mac_ptr += offset_size;
25321 break;
25322
25323 case DW_MACINFO_vendor_ext:
25324 /* Only skip the data by MAC_PTR. */
25325 if (!section_is_gnu)
25326 {
25327 unsigned int bytes_read;
25328
25329 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25330 mac_ptr += bytes_read;
25331 read_direct_string (abfd, mac_ptr, &bytes_read);
25332 mac_ptr += bytes_read;
25333 }
25334 /* FALLTHROUGH */
25335
25336 default:
25337 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25338 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25339 section);
25340 if (mac_ptr == NULL)
25341 return;
25342 break;
25343 }
132448f8 25344 DIAGNOSTIC_POP
cf2c3c16
TT
25345 } while (macinfo_type != 0 && current_file == NULL);
25346
25347 /* Second pass: Process all entries.
25348
25349 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25350 command-line macro definitions/undefinitions. This flag is unset when we
25351 reach the first DW_MACINFO_start_file entry. */
25352
fc4007c9
TT
25353 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25354 htab_eq_pointer,
25355 NULL, xcalloc, xfree));
8fc3fc34 25356 mac_ptr = section->buffer + offset;
fc4007c9 25357 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25358 *slot = (void *) mac_ptr;
804d2729 25359 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25360 current_file, lh, section,
fc4007c9
TT
25361 section_is_gnu, 0, offset_size,
25362 include_hash.get ());
cf2c3c16
TT
25363}
25364
8e19ed76 25365/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25366 if so return true else false. */
380bca97 25367
8e19ed76 25368static int
6e5a29e1 25369attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25370{
25371 return (attr == NULL ? 0 :
25372 attr->form == DW_FORM_block1
25373 || attr->form == DW_FORM_block2
25374 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25375 || attr->form == DW_FORM_block
25376 || attr->form == DW_FORM_exprloc);
8e19ed76 25377}
4c2df51b 25378
c6a0999f
JB
25379/* Return non-zero if ATTR's value is a section offset --- classes
25380 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25381 You may use DW_UNSND (attr) to retrieve such offsets.
25382
25383 Section 7.5.4, "Attribute Encodings", explains that no attribute
25384 may have a value that belongs to more than one of these classes; it
25385 would be ambiguous if we did, because we use the same forms for all
25386 of them. */
380bca97 25387
3690dd37 25388static int
6e5a29e1 25389attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25390{
25391 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25392 || attr->form == DW_FORM_data8
25393 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25394}
25395
3690dd37
JB
25396/* Return non-zero if ATTR's value falls in the 'constant' class, or
25397 zero otherwise. When this function returns true, you can apply
25398 dwarf2_get_attr_constant_value to it.
25399
25400 However, note that for some attributes you must check
25401 attr_form_is_section_offset before using this test. DW_FORM_data4
25402 and DW_FORM_data8 are members of both the constant class, and of
25403 the classes that contain offsets into other debug sections
25404 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25405 that, if an attribute's can be either a constant or one of the
25406 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25407 taken as section offsets, not constants.
25408
25409 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25410 cannot handle that. */
380bca97 25411
3690dd37 25412static int
6e5a29e1 25413attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25414{
25415 switch (attr->form)
25416 {
25417 case DW_FORM_sdata:
25418 case DW_FORM_udata:
25419 case DW_FORM_data1:
25420 case DW_FORM_data2:
25421 case DW_FORM_data4:
25422 case DW_FORM_data8:
663c44ac 25423 case DW_FORM_implicit_const:
3690dd37
JB
25424 return 1;
25425 default:
25426 return 0;
25427 }
25428}
25429
7771576e
SA
25430
25431/* DW_ADDR is always stored already as sect_offset; despite for the forms
25432 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25433
25434static int
6e5a29e1 25435attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25436{
25437 switch (attr->form)
25438 {
25439 case DW_FORM_ref_addr:
25440 case DW_FORM_ref1:
25441 case DW_FORM_ref2:
25442 case DW_FORM_ref4:
25443 case DW_FORM_ref8:
25444 case DW_FORM_ref_udata:
25445 case DW_FORM_GNU_ref_alt:
25446 return 1;
25447 default:
25448 return 0;
25449 }
25450}
25451
3019eac3
DE
25452/* Return the .debug_loc section to use for CU.
25453 For DWO files use .debug_loc.dwo. */
25454
25455static struct dwarf2_section_info *
25456cu_debug_loc_section (struct dwarf2_cu *cu)
25457{
518817b3
SM
25458 struct dwarf2_per_objfile *dwarf2_per_objfile
25459 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25460
3019eac3 25461 if (cu->dwo_unit)
43988095
JK
25462 {
25463 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25464
43988095
JK
25465 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25466 }
25467 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25468 : &dwarf2_per_objfile->loc);
3019eac3
DE
25469}
25470
8cf6f0b1
TT
25471/* A helper function that fills in a dwarf2_loclist_baton. */
25472
25473static void
25474fill_in_loclist_baton (struct dwarf2_cu *cu,
25475 struct dwarf2_loclist_baton *baton,
ff39bb5e 25476 const struct attribute *attr)
8cf6f0b1 25477{
518817b3
SM
25478 struct dwarf2_per_objfile *dwarf2_per_objfile
25479 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25480 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25481
25482 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25483
25484 baton->per_cu = cu->per_cu;
25485 gdb_assert (baton->per_cu);
25486 /* We don't know how long the location list is, but make sure we
25487 don't run off the edge of the section. */
3019eac3
DE
25488 baton->size = section->size - DW_UNSND (attr);
25489 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25490 baton->base_address = cu->base_address;
f664829e 25491 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25492}
25493
4c2df51b 25494static void
ff39bb5e 25495dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25496 struct dwarf2_cu *cu, int is_block)
4c2df51b 25497{
518817b3
SM
25498 struct dwarf2_per_objfile *dwarf2_per_objfile
25499 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25500 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25501 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25502
3690dd37 25503 if (attr_form_is_section_offset (attr)
3019eac3 25504 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25505 the section. If so, fall through to the complaint in the
25506 other branch. */
3019eac3 25507 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25508 {
0d53c4c4 25509 struct dwarf2_loclist_baton *baton;
4c2df51b 25510
8d749320 25511 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25512
8cf6f0b1 25513 fill_in_loclist_baton (cu, baton, attr);
be391dca 25514
d00adf39 25515 if (cu->base_known == 0)
b98664d3 25516 complaint (_("Location list used without "
3e43a32a 25517 "specifying the CU base address."));
4c2df51b 25518
f1e6e072
TT
25519 SYMBOL_ACLASS_INDEX (sym) = (is_block
25520 ? dwarf2_loclist_block_index
25521 : dwarf2_loclist_index);
0d53c4c4
DJ
25522 SYMBOL_LOCATION_BATON (sym) = baton;
25523 }
25524 else
25525 {
25526 struct dwarf2_locexpr_baton *baton;
25527
8d749320 25528 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25529 baton->per_cu = cu->per_cu;
25530 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25531
25532 if (attr_form_is_block (attr))
25533 {
25534 /* Note that we're just copying the block's data pointer
25535 here, not the actual data. We're still pointing into the
6502dd73
DJ
25536 info_buffer for SYM's objfile; right now we never release
25537 that buffer, but when we do clean up properly this may
25538 need to change. */
0d53c4c4
DJ
25539 baton->size = DW_BLOCK (attr)->size;
25540 baton->data = DW_BLOCK (attr)->data;
25541 }
25542 else
25543 {
25544 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 25545 sym->natural_name ());
0d53c4c4 25546 baton->size = 0;
0d53c4c4 25547 }
6e70227d 25548
f1e6e072
TT
25549 SYMBOL_ACLASS_INDEX (sym) = (is_block
25550 ? dwarf2_locexpr_block_index
25551 : dwarf2_locexpr_index);
0d53c4c4
DJ
25552 SYMBOL_LOCATION_BATON (sym) = baton;
25553 }
4c2df51b 25554}
6502dd73 25555
9aa1f1e3
TT
25556/* Return the OBJFILE associated with the compilation unit CU. If CU
25557 came from a separate debuginfo file, then the master objfile is
25558 returned. */
ae0d2f24
UW
25559
25560struct objfile *
25561dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25562{
e3b94546 25563 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25564
25565 /* Return the master objfile, so that we can report and look up the
25566 correct file containing this variable. */
25567 if (objfile->separate_debug_objfile_backlink)
25568 objfile = objfile->separate_debug_objfile_backlink;
25569
25570 return objfile;
25571}
25572
96408a79
SA
25573/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25574 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25575 CU_HEADERP first. */
25576
25577static const struct comp_unit_head *
25578per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25579 struct dwarf2_per_cu_data *per_cu)
25580{
d521ce57 25581 const gdb_byte *info_ptr;
96408a79
SA
25582
25583 if (per_cu->cu)
25584 return &per_cu->cu->header;
25585
9c541725 25586 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25587
25588 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25589 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25590 rcuh_kind::COMPILE);
96408a79
SA
25591
25592 return cu_headerp;
25593}
25594
ae0d2f24
UW
25595/* Return the address size given in the compilation unit header for CU. */
25596
98714339 25597int
ae0d2f24
UW
25598dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25599{
96408a79
SA
25600 struct comp_unit_head cu_header_local;
25601 const struct comp_unit_head *cu_headerp;
c471e790 25602
96408a79
SA
25603 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25604
25605 return cu_headerp->addr_size;
ae0d2f24
UW
25606}
25607
9eae7c52
TT
25608/* Return the offset size given in the compilation unit header for CU. */
25609
25610int
25611dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25612{
96408a79
SA
25613 struct comp_unit_head cu_header_local;
25614 const struct comp_unit_head *cu_headerp;
9c6c53f7 25615
96408a79
SA
25616 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25617
25618 return cu_headerp->offset_size;
25619}
25620
25621/* See its dwarf2loc.h declaration. */
25622
25623int
25624dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25625{
25626 struct comp_unit_head cu_header_local;
25627 const struct comp_unit_head *cu_headerp;
25628
25629 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25630
25631 if (cu_headerp->version == 2)
25632 return cu_headerp->addr_size;
25633 else
25634 return cu_headerp->offset_size;
181cebd4
JK
25635}
25636
9aa1f1e3
TT
25637/* Return the text offset of the CU. The returned offset comes from
25638 this CU's objfile. If this objfile came from a separate debuginfo
25639 file, then the offset may be different from the corresponding
25640 offset in the parent objfile. */
25641
25642CORE_ADDR
25643dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25644{
e3b94546 25645 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25646
25647 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25648}
25649
9a49df9d
AB
25650/* Return a type that is a generic pointer type, the size of which matches
25651 the address size given in the compilation unit header for PER_CU. */
25652static struct type *
25653dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25654{
25655 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25656 struct type *void_type = objfile_type (objfile)->builtin_void;
25657 struct type *addr_type = lookup_pointer_type (void_type);
25658 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25659
25660 if (TYPE_LENGTH (addr_type) == addr_size)
25661 return addr_type;
25662
25663 addr_type
25664 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25665 return addr_type;
25666}
25667
43988095
JK
25668/* Return DWARF version number of PER_CU. */
25669
25670short
25671dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25672{
25673 return per_cu->dwarf_version;
25674}
25675
348e048f
DE
25676/* Locate the .debug_info compilation unit from CU's objfile which contains
25677 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25678
25679static struct dwarf2_per_cu_data *
9c541725 25680dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25681 unsigned int offset_in_dwz,
ed2dc618 25682 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25683{
25684 struct dwarf2_per_cu_data *this_cu;
25685 int low, high;
25686
ae038cb0 25687 low = 0;
b76e467d 25688 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25689 while (high > low)
25690 {
36586728 25691 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25692 int mid = low + (high - low) / 2;
9a619af0 25693
36586728 25694 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25695 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25696 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25697 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25698 high = mid;
25699 else
25700 low = mid + 1;
25701 }
25702 gdb_assert (low == high);
36586728 25703 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25704 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25705 {
36586728 25706 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25707 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25708 "offset %s [in module %s]"),
25709 sect_offset_str (sect_off),
ed2dc618 25710 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25711
9c541725
PA
25712 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25713 <= sect_off);
ae038cb0
DJ
25714 return dwarf2_per_objfile->all_comp_units[low-1];
25715 }
25716 else
25717 {
b76e467d 25718 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25719 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25720 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25721 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25722 return this_cu;
25723 }
25724}
25725
23745b47 25726/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25727
fcd3b13d
SM
25728dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25729 : per_cu (per_cu_),
9068261f
AB
25730 mark (false),
25731 has_loclist (false),
25732 checked_producer (false),
25733 producer_is_gxx_lt_4_6 (false),
25734 producer_is_gcc_lt_4_3 (false),
eb77c9df 25735 producer_is_icc (false),
9068261f 25736 producer_is_icc_lt_14 (false),
c258c396 25737 producer_is_codewarrior (false),
9068261f 25738 processing_has_namespace_info (false)
93311388 25739{
fcd3b13d
SM
25740 per_cu->cu = this;
25741}
25742
25743/* Destroy a dwarf2_cu. */
25744
25745dwarf2_cu::~dwarf2_cu ()
25746{
25747 per_cu->cu = NULL;
9816fde3
JK
25748}
25749
25750/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25751
25752static void
95554aad
TT
25753prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25754 enum language pretend_language)
9816fde3
JK
25755{
25756 struct attribute *attr;
25757
25758 /* Set the language we're debugging. */
25759 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25760 if (attr != nullptr)
9816fde3
JK
25761 set_cu_language (DW_UNSND (attr), cu);
25762 else
9cded63f 25763 {
95554aad 25764 cu->language = pretend_language;
9cded63f
TT
25765 cu->language_defn = language_def (cu->language);
25766 }
dee91e82 25767
7d45c7c3 25768 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25769}
25770
ae038cb0
DJ
25771/* Increase the age counter on each cached compilation unit, and free
25772 any that are too old. */
25773
25774static void
ed2dc618 25775age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25776{
25777 struct dwarf2_per_cu_data *per_cu, **last_chain;
25778
25779 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25780 per_cu = dwarf2_per_objfile->read_in_chain;
25781 while (per_cu != NULL)
25782 {
25783 per_cu->cu->last_used ++;
b4f54984 25784 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25785 dwarf2_mark (per_cu->cu);
25786 per_cu = per_cu->cu->read_in_chain;
25787 }
25788
25789 per_cu = dwarf2_per_objfile->read_in_chain;
25790 last_chain = &dwarf2_per_objfile->read_in_chain;
25791 while (per_cu != NULL)
25792 {
25793 struct dwarf2_per_cu_data *next_cu;
25794
25795 next_cu = per_cu->cu->read_in_chain;
25796
25797 if (!per_cu->cu->mark)
25798 {
fcd3b13d 25799 delete per_cu->cu;
ae038cb0
DJ
25800 *last_chain = next_cu;
25801 }
25802 else
25803 last_chain = &per_cu->cu->read_in_chain;
25804
25805 per_cu = next_cu;
25806 }
25807}
25808
25809/* Remove a single compilation unit from the cache. */
25810
25811static void
dee91e82 25812free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25813{
25814 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25815 struct dwarf2_per_objfile *dwarf2_per_objfile
25816 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25817
25818 per_cu = dwarf2_per_objfile->read_in_chain;
25819 last_chain = &dwarf2_per_objfile->read_in_chain;
25820 while (per_cu != NULL)
25821 {
25822 struct dwarf2_per_cu_data *next_cu;
25823
25824 next_cu = per_cu->cu->read_in_chain;
25825
dee91e82 25826 if (per_cu == target_per_cu)
ae038cb0 25827 {
fcd3b13d 25828 delete per_cu->cu;
dee91e82 25829 per_cu->cu = NULL;
ae038cb0
DJ
25830 *last_chain = next_cu;
25831 break;
25832 }
25833 else
25834 last_chain = &per_cu->cu->read_in_chain;
25835
25836 per_cu = next_cu;
25837 }
25838}
25839
dee91e82
DE
25840/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25841 We store these in a hash table separate from the DIEs, and preserve them
25842 when the DIEs are flushed out of cache.
25843
25844 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25845 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25846 or the type may come from a DWO file. Furthermore, while it's more logical
25847 to use per_cu->section+offset, with Fission the section with the data is in
25848 the DWO file but we don't know that section at the point we need it.
25849 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25850 because we can enter the lookup routine, get_die_type_at_offset, from
25851 outside this file, and thus won't necessarily have PER_CU->cu.
25852 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25853
dee91e82 25854struct dwarf2_per_cu_offset_and_type
1c379e20 25855{
dee91e82 25856 const struct dwarf2_per_cu_data *per_cu;
9c541725 25857 sect_offset sect_off;
1c379e20
DJ
25858 struct type *type;
25859};
25860
dee91e82 25861/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25862
25863static hashval_t
dee91e82 25864per_cu_offset_and_type_hash (const void *item)
1c379e20 25865{
9a3c8263
SM
25866 const struct dwarf2_per_cu_offset_and_type *ofs
25867 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25868
9c541725 25869 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25870}
25871
dee91e82 25872/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25873
25874static int
dee91e82 25875per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25876{
9a3c8263
SM
25877 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25878 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25879 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25880 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25881
dee91e82 25882 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25883 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25884}
25885
25886/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25887 table if necessary. For convenience, return TYPE.
25888
25889 The DIEs reading must have careful ordering to:
85102364 25890 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25891 reading current DIE.
25892 * Not trying to dereference contents of still incompletely read in types
25893 while reading in other DIEs.
25894 * Enable referencing still incompletely read in types just by a pointer to
25895 the type without accessing its fields.
25896
25897 Therefore caller should follow these rules:
25898 * Try to fetch any prerequisite types we may need to build this DIE type
25899 before building the type and calling set_die_type.
e71ec853 25900 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25901 possible before fetching more types to complete the current type.
25902 * Make the type as complete as possible before fetching more types. */
1c379e20 25903
f792889a 25904static struct type *
1c379e20
DJ
25905set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25906{
518817b3
SM
25907 struct dwarf2_per_objfile *dwarf2_per_objfile
25908 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25909 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25910 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25911 struct attribute *attr;
25912 struct dynamic_prop prop;
1c379e20 25913
b4ba55a1
JB
25914 /* For Ada types, make sure that the gnat-specific data is always
25915 initialized (if not already set). There are a few types where
25916 we should not be doing so, because the type-specific area is
25917 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25918 where the type-specific area is used to store the floatformat).
25919 But this is not a problem, because the gnat-specific information
25920 is actually not needed for these types. */
25921 if (need_gnat_info (cu)
25922 && TYPE_CODE (type) != TYPE_CODE_FUNC
25923 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25924 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25925 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25926 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25927 && !HAVE_GNAT_AUX_INFO (type))
25928 INIT_GNAT_SPECIFIC (type);
25929
3f2f83dd
KB
25930 /* Read DW_AT_allocated and set in type. */
25931 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25932 if (attr_form_is_block (attr))
25933 {
9a49df9d
AB
25934 struct type *prop_type
25935 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25936 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25937 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25938 }
25939 else if (attr != NULL)
25940 {
b98664d3 25941 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25942 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25943 sect_offset_str (die->sect_off));
3f2f83dd
KB
25944 }
25945
25946 /* Read DW_AT_associated and set in type. */
25947 attr = dwarf2_attr (die, DW_AT_associated, cu);
25948 if (attr_form_is_block (attr))
25949 {
9a49df9d
AB
25950 struct type *prop_type
25951 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25952 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25953 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25954 }
25955 else if (attr != NULL)
25956 {
b98664d3 25957 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25958 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25959 sect_offset_str (die->sect_off));
3f2f83dd
KB
25960 }
25961
3cdcd0ce
JB
25962 /* Read DW_AT_data_location and set in type. */
25963 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25964 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25965 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25966 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25967
dee91e82 25968 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25969 {
dee91e82
DE
25970 dwarf2_per_objfile->die_type_hash =
25971 htab_create_alloc_ex (127,
25972 per_cu_offset_and_type_hash,
25973 per_cu_offset_and_type_eq,
25974 NULL,
25975 &objfile->objfile_obstack,
25976 hashtab_obstack_allocate,
25977 dummy_obstack_deallocate);
f792889a 25978 }
1c379e20 25979
dee91e82 25980 ofs.per_cu = cu->per_cu;
9c541725 25981 ofs.sect_off = die->sect_off;
1c379e20 25982 ofs.type = type;
dee91e82
DE
25983 slot = (struct dwarf2_per_cu_offset_and_type **)
25984 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25985 if (*slot)
b98664d3 25986 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25987 sect_offset_str (die->sect_off));
8d749320
SM
25988 *slot = XOBNEW (&objfile->objfile_obstack,
25989 struct dwarf2_per_cu_offset_and_type);
1c379e20 25990 **slot = ofs;
f792889a 25991 return type;
1c379e20
DJ
25992}
25993
9c541725 25994/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25995 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25996
25997static struct type *
9c541725 25998get_die_type_at_offset (sect_offset sect_off,
673bfd45 25999 struct dwarf2_per_cu_data *per_cu)
1c379e20 26000{
dee91e82 26001 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 26002 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 26003
dee91e82 26004 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 26005 return NULL;
1c379e20 26006
dee91e82 26007 ofs.per_cu = per_cu;
9c541725 26008 ofs.sect_off = sect_off;
9a3c8263
SM
26009 slot = ((struct dwarf2_per_cu_offset_and_type *)
26010 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
26011 if (slot)
26012 return slot->type;
26013 else
26014 return NULL;
26015}
26016
02142a6c 26017/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
26018 or return NULL if DIE does not have a saved type. */
26019
26020static struct type *
26021get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26022{
9c541725 26023 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
26024}
26025
10b3939b
DJ
26026/* Add a dependence relationship from CU to REF_PER_CU. */
26027
26028static void
26029dwarf2_add_dependence (struct dwarf2_cu *cu,
26030 struct dwarf2_per_cu_data *ref_per_cu)
26031{
26032 void **slot;
26033
26034 if (cu->dependencies == NULL)
26035 cu->dependencies
26036 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26037 NULL, &cu->comp_unit_obstack,
26038 hashtab_obstack_allocate,
26039 dummy_obstack_deallocate);
26040
26041 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26042 if (*slot == NULL)
26043 *slot = ref_per_cu;
26044}
1c379e20 26045
f504f079
DE
26046/* Subroutine of dwarf2_mark to pass to htab_traverse.
26047 Set the mark field in every compilation unit in the
ae038cb0
DJ
26048 cache that we must keep because we are keeping CU. */
26049
10b3939b
DJ
26050static int
26051dwarf2_mark_helper (void **slot, void *data)
26052{
26053 struct dwarf2_per_cu_data *per_cu;
26054
26055 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
26056
26057 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26058 reading of the chain. As such dependencies remain valid it is not much
26059 useful to track and undo them during QUIT cleanups. */
26060 if (per_cu->cu == NULL)
26061 return 1;
26062
10b3939b
DJ
26063 if (per_cu->cu->mark)
26064 return 1;
9068261f 26065 per_cu->cu->mark = true;
10b3939b
DJ
26066
26067 if (per_cu->cu->dependencies != NULL)
26068 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26069
26070 return 1;
26071}
26072
f504f079
DE
26073/* Set the mark field in CU and in every other compilation unit in the
26074 cache that we must keep because we are keeping CU. */
26075
ae038cb0
DJ
26076static void
26077dwarf2_mark (struct dwarf2_cu *cu)
26078{
26079 if (cu->mark)
26080 return;
9068261f 26081 cu->mark = true;
10b3939b
DJ
26082 if (cu->dependencies != NULL)
26083 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
26084}
26085
26086static void
26087dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26088{
26089 while (per_cu)
26090 {
9068261f 26091 per_cu->cu->mark = false;
ae038cb0
DJ
26092 per_cu = per_cu->cu->read_in_chain;
26093 }
72bf9492
DJ
26094}
26095
72bf9492
DJ
26096/* Trivial hash function for partial_die_info: the hash value of a DIE
26097 is its offset in .debug_info for this objfile. */
26098
26099static hashval_t
26100partial_die_hash (const void *item)
26101{
9a3c8263
SM
26102 const struct partial_die_info *part_die
26103 = (const struct partial_die_info *) item;
9a619af0 26104
9c541725 26105 return to_underlying (part_die->sect_off);
72bf9492
DJ
26106}
26107
26108/* Trivial comparison function for partial_die_info structures: two DIEs
26109 are equal if they have the same offset. */
26110
26111static int
26112partial_die_eq (const void *item_lhs, const void *item_rhs)
26113{
9a3c8263
SM
26114 const struct partial_die_info *part_die_lhs
26115 = (const struct partial_die_info *) item_lhs;
26116 const struct partial_die_info *part_die_rhs
26117 = (const struct partial_die_info *) item_rhs;
9a619af0 26118
9c541725 26119 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
26120}
26121
3c3bb058
AB
26122struct cmd_list_element *set_dwarf_cmdlist;
26123struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
26124
26125static void
981a3fb3 26126set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 26127{
b4f54984 26128 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 26129 gdb_stdout);
ae038cb0
DJ
26130}
26131
26132static void
981a3fb3 26133show_dwarf_cmd (const char *args, int from_tty)
6e70227d 26134{
b4f54984 26135 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
26136}
26137
491144b5 26138bool dwarf_always_disassemble;
437afbb8 26139
437afbb8 26140static void
cd4fb1b2
SM
26141show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26142 struct cmd_list_element *c, const char *value)
9291a0cd 26143{
cd4fb1b2
SM
26144 fprintf_filtered (file,
26145 _("Whether to always disassemble "
26146 "DWARF expressions is %s.\n"),
26147 value);
9291a0cd
TT
26148}
26149
9291a0cd 26150static void
cd4fb1b2
SM
26151show_check_physname (struct ui_file *file, int from_tty,
26152 struct cmd_list_element *c, const char *value)
9291a0cd 26153{
cd4fb1b2
SM
26154 fprintf_filtered (file,
26155 _("Whether to check \"physname\" is %s.\n"),
26156 value);
9291a0cd
TT
26157}
26158
cd4fb1b2
SM
26159void
26160_initialize_dwarf2_read (void)
9291a0cd 26161{
cd4fb1b2
SM
26162 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26163Set DWARF specific variables.\n\
590042fc 26164Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
26165 &set_dwarf_cmdlist, "maintenance set dwarf ",
26166 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 26167
cd4fb1b2 26168 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
26169Show DWARF specific variables.\n\
26170Show DWARF variables such as the cache size."),
cd4fb1b2
SM
26171 &show_dwarf_cmdlist, "maintenance show dwarf ",
26172 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 26173
cd4fb1b2
SM
26174 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26175 &dwarf_max_cache_age, _("\
26176Set the upper bound on the age of cached DWARF compilation units."), _("\
26177Show the upper bound on the age of cached DWARF compilation units."), _("\
26178A higher limit means that cached compilation units will be stored\n\
26179in memory longer, and more total memory will be used. Zero disables\n\
26180caching, which can slow down startup."),
26181 NULL,
26182 show_dwarf_max_cache_age,
26183 &set_dwarf_cmdlist,
26184 &show_dwarf_cmdlist);
156942c7 26185
cd4fb1b2
SM
26186 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26187 &dwarf_always_disassemble, _("\
26188Set whether `info address' always disassembles DWARF expressions."), _("\
26189Show whether `info address' always disassembles DWARF expressions."), _("\
26190When enabled, DWARF expressions are always printed in an assembly-like\n\
26191syntax. When disabled, expressions will be printed in a more\n\
26192conversational style, when possible."),
26193 NULL,
26194 show_dwarf_always_disassemble,
26195 &set_dwarf_cmdlist,
26196 &show_dwarf_cmdlist);
9291a0cd 26197
cd4fb1b2
SM
26198 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26199Set debugging of the DWARF reader."), _("\
26200Show debugging of the DWARF reader."), _("\
26201When enabled (non-zero), debugging messages are printed during DWARF\n\
26202reading and symtab expansion. A value of 1 (one) provides basic\n\
26203information. A value greater than 1 provides more verbose information."),
26204 NULL,
26205 NULL,
26206 &setdebuglist, &showdebuglist);
9291a0cd 26207
cd4fb1b2
SM
26208 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26209Set debugging of the DWARF DIE reader."), _("\
26210Show debugging of the DWARF DIE reader."), _("\
26211When enabled (non-zero), DIEs are dumped after they are read in.\n\
26212The value is the maximum depth to print."),
26213 NULL,
26214 NULL,
26215 &setdebuglist, &showdebuglist);
9291a0cd 26216
cd4fb1b2
SM
26217 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26218Set debugging of the dwarf line reader."), _("\
26219Show debugging of the dwarf line reader."), _("\
26220When enabled (non-zero), line number entries are dumped as they are read in.\n\
26221A value of 1 (one) provides basic information.\n\
26222A value greater than 1 provides more verbose information."),
26223 NULL,
26224 NULL,
26225 &setdebuglist, &showdebuglist);
437afbb8 26226
cd4fb1b2
SM
26227 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26228Set cross-checking of \"physname\" code against demangler."), _("\
26229Show cross-checking of \"physname\" code against demangler."), _("\
26230When enabled, GDB's internal \"physname\" code is checked against\n\
26231the demangler."),
26232 NULL, show_check_physname,
26233 &setdebuglist, &showdebuglist);
900e11f9 26234
e615022a
DE
26235 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26236 no_class, &use_deprecated_index_sections, _("\
26237Set whether to use deprecated gdb_index sections."), _("\
26238Show whether to use deprecated gdb_index sections."), _("\
26239When enabled, deprecated .gdb_index sections are used anyway.\n\
26240Normally they are ignored either because of a missing feature or\n\
26241performance issue.\n\
26242Warning: This option must be enabled before gdb reads the file."),
26243 NULL,
26244 NULL,
26245 &setlist, &showlist);
26246
f1e6e072
TT
26247 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26248 &dwarf2_locexpr_funcs);
26249 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26250 &dwarf2_loclist_funcs);
26251
26252 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26253 &dwarf2_block_frame_base_locexpr_funcs);
26254 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26255 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26256
26257#if GDB_SELF_TEST
26258 selftests::register_test ("dw2_expand_symtabs_matching",
26259 selftests::dw2_expand_symtabs_matching::run_test);
26260#endif
6502dd73 26261}
This page took 4.870479 seconds and 4 git commands to generate.