]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/dwarf2read.c
[gdb] Fix typos in comments
[thirdparty/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
42a4f53d 3 Copyright (C) 1994-2019 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
ecfb656c
PA
912/* A 1-based directory index. This is a strong typedef to prevent
913 accidentally using a directory index as a 0-based index into an
914 array/vector. */
915enum class dir_index : unsigned int {};
916
917/* Likewise, a 1-based file name index. */
918enum class file_name_index : unsigned int {};
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
ecfb656c 970 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 971 is out of bounds. */
ecfb656c 972 const char *include_dir_at (dir_index index) const
8c43009f 973 {
ecfb656c
PA
974 /* Convert directory index number (1-based) to vector index
975 (0-based). */
976 size_t vec_index = to_underlying (index) - 1;
977
978 if (vec_index >= include_dirs.size ())
8c43009f 979 return NULL;
ecfb656c 980 return include_dirs[vec_index];
8c43009f
PA
981 }
982
ecfb656c 983 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 984 is out of bounds. */
ecfb656c 985 file_entry *file_name_at (file_name_index index)
8c43009f 986 {
ecfb656c
PA
987 /* Convert file name index number (1-based) to vector index
988 (0-based). */
989 size_t vec_index = to_underlying (index) - 1;
990
991 if (vec_index >= file_names.size ())
fff8551c 992 return NULL;
ecfb656c 993 return &file_names[vec_index];
fff8551c
PA
994 }
995
527f3840 996 /* Offset of line number information in .debug_line section. */
9c541725 997 sect_offset sect_off {};
527f3840
JK
998
999 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1000 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1001
1002 unsigned int total_length {};
1003 unsigned short version {};
1004 unsigned int header_length {};
1005 unsigned char minimum_instruction_length {};
1006 unsigned char maximum_ops_per_instruction {};
1007 unsigned char default_is_stmt {};
1008 int line_base {};
1009 unsigned char line_range {};
1010 unsigned char opcode_base {};
debd256d
JB
1011
1012 /* standard_opcode_lengths[i] is the number of operands for the
1013 standard opcode whose value is i. This means that
1014 standard_opcode_lengths[0] is unused, and the last meaningful
1015 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1016 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1017
fff8551c
PA
1018 /* The include_directories table. Note these are observing
1019 pointers. The memory is owned by debug_line_buffer. */
1020 std::vector<const char *> include_dirs;
debd256d 1021
fff8551c
PA
1022 /* The file_names table. */
1023 std::vector<file_entry> file_names;
debd256d
JB
1024
1025 /* The start and end of the statement program following this
6502dd73 1026 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1027 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1028};
c906108c 1029
fff8551c
PA
1030typedef std::unique_ptr<line_header> line_header_up;
1031
8c43009f
PA
1032const char *
1033file_entry::include_dir (const line_header *lh) const
1034{
ecfb656c 1035 return lh->include_dir_at (d_index);
8c43009f
PA
1036}
1037
c906108c 1038/* When we construct a partial symbol table entry we only
0963b4bd 1039 need this much information. */
6f06d47b 1040struct partial_die_info : public allocate_on_obstack
c906108c 1041 {
6f06d47b
YQ
1042 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1043
1044 /* Disable assign but still keep copy ctor, which is needed
1045 load_partial_dies. */
1046 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1047
52356b79
YQ
1048 /* Adjust the partial die before generating a symbol for it. This
1049 function may set the is_external flag or change the DIE's
1050 name. */
1051 void fixup (struct dwarf2_cu *cu);
1052
48fbe735
YQ
1053 /* Read a minimal amount of information into the minimal die
1054 structure. */
1055 const gdb_byte *read (const struct die_reader_specs *reader,
1056 const struct abbrev_info &abbrev,
1057 const gdb_byte *info_ptr);
1058
72bf9492 1059 /* Offset of this DIE. */
6f06d47b 1060 const sect_offset sect_off;
72bf9492
DJ
1061
1062 /* DWARF-2 tag for this DIE. */
6f06d47b 1063 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1064
72bf9492 1065 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1066 const unsigned int has_children : 1;
1067
72bf9492
DJ
1068 unsigned int is_external : 1;
1069 unsigned int is_declaration : 1;
1070 unsigned int has_type : 1;
1071 unsigned int has_specification : 1;
1072 unsigned int has_pc_info : 1;
481860b3 1073 unsigned int may_be_inlined : 1;
72bf9492 1074
0c1b455e
TT
1075 /* This DIE has been marked DW_AT_main_subprogram. */
1076 unsigned int main_subprogram : 1;
1077
72bf9492
DJ
1078 /* Flag set if the SCOPE field of this structure has been
1079 computed. */
1080 unsigned int scope_set : 1;
1081
fa4028e9
JB
1082 /* Flag set if the DIE has a byte_size attribute. */
1083 unsigned int has_byte_size : 1;
1084
ff908ebf
AW
1085 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1086 unsigned int has_const_value : 1;
1087
98bfdba5
PA
1088 /* Flag set if any of the DIE's children are template arguments. */
1089 unsigned int has_template_arguments : 1;
1090
52356b79 1091 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1092 unsigned int fixup_called : 1;
1093
36586728
TT
1094 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1095 unsigned int is_dwz : 1;
1096
1097 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1098 unsigned int spec_is_dwz : 1;
1099
72bf9492 1100 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1101 sometimes a default name for unnamed DIEs. */
6f06d47b 1102 const char *name = nullptr;
72bf9492 1103
abc72ce4 1104 /* The linkage name, if present. */
6f06d47b 1105 const char *linkage_name = nullptr;
abc72ce4 1106
72bf9492
DJ
1107 /* The scope to prepend to our children. This is generally
1108 allocated on the comp_unit_obstack, so will disappear
1109 when this compilation unit leaves the cache. */
6f06d47b 1110 const char *scope = nullptr;
72bf9492 1111
95554aad
TT
1112 /* Some data associated with the partial DIE. The tag determines
1113 which field is live. */
1114 union
1115 {
1116 /* The location description associated with this DIE, if any. */
1117 struct dwarf_block *locdesc;
1118 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1119 sect_offset sect_off;
6f06d47b 1120 } d {};
72bf9492
DJ
1121
1122 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1123 CORE_ADDR lowpc = 0;
1124 CORE_ADDR highpc = 0;
72bf9492 1125
93311388 1126 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1127 DW_AT_sibling, if any. */
48fbe735
YQ
1128 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1129 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1130 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1131
1132 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1133 DW_AT_specification (or DW_AT_abstract_origin or
1134 DW_AT_extension). */
6f06d47b 1135 sect_offset spec_offset {};
72bf9492
DJ
1136
1137 /* Pointers to this DIE's parent, first child, and next sibling,
1138 if any. */
6f06d47b
YQ
1139 struct partial_die_info *die_parent = nullptr;
1140 struct partial_die_info *die_child = nullptr;
1141 struct partial_die_info *die_sibling = nullptr;
1142
1143 friend struct partial_die_info *
1144 dwarf2_cu::find_partial_die (sect_offset sect_off);
1145
1146 private:
1147 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1148 partial_die_info (sect_offset sect_off)
1149 : partial_die_info (sect_off, DW_TAG_padding, 0)
1150 {
1151 }
1152
1153 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1154 int has_children_)
1155 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1156 {
1157 is_external = 0;
1158 is_declaration = 0;
1159 has_type = 0;
1160 has_specification = 0;
1161 has_pc_info = 0;
1162 may_be_inlined = 0;
1163 main_subprogram = 0;
1164 scope_set = 0;
1165 has_byte_size = 0;
1166 has_const_value = 0;
1167 has_template_arguments = 0;
1168 fixup_called = 0;
1169 is_dwz = 0;
1170 spec_is_dwz = 0;
1171 }
c906108c
SS
1172 };
1173
0963b4bd 1174/* This data structure holds the information of an abbrev. */
c906108c
SS
1175struct abbrev_info
1176 {
1177 unsigned int number; /* number identifying abbrev */
1178 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1179 unsigned short has_children; /* boolean */
1180 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1181 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1182 struct abbrev_info *next; /* next in chain */
1183 };
1184
1185struct attr_abbrev
1186 {
9d25dd43
DE
1187 ENUM_BITFIELD(dwarf_attribute) name : 16;
1188 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1189
1190 /* It is valid only if FORM is DW_FORM_implicit_const. */
1191 LONGEST implicit_const;
c906108c
SS
1192 };
1193
433df2d4
DE
1194/* Size of abbrev_table.abbrev_hash_table. */
1195#define ABBREV_HASH_SIZE 121
1196
1197/* Top level data structure to contain an abbreviation table. */
1198
1199struct abbrev_table
1200{
685af9cd
TT
1201 explicit abbrev_table (sect_offset off)
1202 : sect_off (off)
1203 {
4a17f768 1204 m_abbrevs =
685af9cd 1205 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1206 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1207 }
1208
1209 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1210
1211 /* Allocate space for a struct abbrev_info object in
1212 ABBREV_TABLE. */
1213 struct abbrev_info *alloc_abbrev ();
1214
1215 /* Add an abbreviation to the table. */
1216 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1217
1218 /* Look up an abbrev in the table.
1219 Returns NULL if the abbrev is not found. */
1220
1221 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1222
1223
f4dc4d17
DE
1224 /* Where the abbrev table came from.
1225 This is used as a sanity check when the table is used. */
685af9cd 1226 const sect_offset sect_off;
433df2d4
DE
1227
1228 /* Storage for the abbrev table. */
685af9cd 1229 auto_obstack abbrev_obstack;
433df2d4 1230
4a17f768
YQ
1231private:
1232
433df2d4
DE
1233 /* Hash table of abbrevs.
1234 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1235 It could be statically allocated, but the previous code didn't so we
1236 don't either. */
4a17f768 1237 struct abbrev_info **m_abbrevs;
433df2d4
DE
1238};
1239
685af9cd
TT
1240typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1241
0963b4bd 1242/* Attributes have a name and a value. */
b60c80d6
DJ
1243struct attribute
1244 {
9d25dd43 1245 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1246 ENUM_BITFIELD(dwarf_form) form : 15;
1247
1248 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1249 field should be in u.str (existing only for DW_STRING) but it is kept
1250 here for better struct attribute alignment. */
1251 unsigned int string_is_canonical : 1;
1252
b60c80d6
DJ
1253 union
1254 {
15d034d0 1255 const char *str;
b60c80d6 1256 struct dwarf_block *blk;
43bbcdc2
PH
1257 ULONGEST unsnd;
1258 LONGEST snd;
b60c80d6 1259 CORE_ADDR addr;
ac9ec31b 1260 ULONGEST signature;
b60c80d6
DJ
1261 }
1262 u;
1263 };
1264
0963b4bd 1265/* This data structure holds a complete die structure. */
c906108c
SS
1266struct die_info
1267 {
76815b17
DE
1268 /* DWARF-2 tag for this DIE. */
1269 ENUM_BITFIELD(dwarf_tag) tag : 16;
1270
1271 /* Number of attributes */
98bfdba5
PA
1272 unsigned char num_attrs;
1273
1274 /* True if we're presently building the full type name for the
1275 type derived from this DIE. */
1276 unsigned char building_fullname : 1;
76815b17 1277
adde2bff
DE
1278 /* True if this die is in process. PR 16581. */
1279 unsigned char in_process : 1;
1280
76815b17
DE
1281 /* Abbrev number */
1282 unsigned int abbrev;
1283
93311388 1284 /* Offset in .debug_info or .debug_types section. */
9c541725 1285 sect_offset sect_off;
78ba4af6
JB
1286
1287 /* The dies in a compilation unit form an n-ary tree. PARENT
1288 points to this die's parent; CHILD points to the first child of
1289 this node; and all the children of a given node are chained
4950bc1c 1290 together via their SIBLING fields. */
639d11d3
DC
1291 struct die_info *child; /* Its first child, if any. */
1292 struct die_info *sibling; /* Its next sibling, if any. */
1293 struct die_info *parent; /* Its parent, if any. */
c906108c 1294
b60c80d6
DJ
1295 /* An array of attributes, with NUM_ATTRS elements. There may be
1296 zero, but it's not common and zero-sized arrays are not
1297 sufficiently portable C. */
1298 struct attribute attrs[1];
c906108c
SS
1299 };
1300
0963b4bd 1301/* Get at parts of an attribute structure. */
c906108c
SS
1302
1303#define DW_STRING(attr) ((attr)->u.str)
8285870a 1304#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1305#define DW_UNSND(attr) ((attr)->u.unsnd)
1306#define DW_BLOCK(attr) ((attr)->u.blk)
1307#define DW_SND(attr) ((attr)->u.snd)
1308#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1309#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1310
0963b4bd 1311/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1312struct dwarf_block
1313 {
56eb65bd 1314 size_t size;
1d6edc3c
JK
1315
1316 /* Valid only if SIZE is not zero. */
d521ce57 1317 const gdb_byte *data;
c906108c
SS
1318 };
1319
c906108c
SS
1320#ifndef ATTR_ALLOC_CHUNK
1321#define ATTR_ALLOC_CHUNK 4
1322#endif
1323
c906108c
SS
1324/* Allocate fields for structs, unions and enums in this size. */
1325#ifndef DW_FIELD_ALLOC_CHUNK
1326#define DW_FIELD_ALLOC_CHUNK 4
1327#endif
1328
c906108c
SS
1329/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1330 but this would require a corresponding change in unpack_field_as_long
1331 and friends. */
1332static int bits_per_byte = 8;
1333
2ddeaf8a
TT
1334/* When reading a variant or variant part, we track a bit more
1335 information about the field, and store it in an object of this
1336 type. */
1337
1338struct variant_field
1339{
1340 /* If we see a DW_TAG_variant, then this will be the discriminant
1341 value. */
1342 ULONGEST discriminant_value;
1343 /* If we see a DW_TAG_variant, then this will be set if this is the
1344 default branch. */
1345 bool default_branch;
1346 /* While reading a DW_TAG_variant_part, this will be set if this
1347 field is the discriminant. */
1348 bool is_discriminant;
1349};
1350
52059ffd
TT
1351struct nextfield
1352{
be2daae6
TT
1353 int accessibility = 0;
1354 int virtuality = 0;
2ddeaf8a 1355 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1356 struct variant_field variant {};
1357 struct field field {};
52059ffd
TT
1358};
1359
1360struct fnfieldlist
1361{
be2daae6
TT
1362 const char *name = nullptr;
1363 std::vector<struct fn_field> fnfields;
52059ffd
TT
1364};
1365
c906108c
SS
1366/* The routines that read and process dies for a C struct or C++ class
1367 pass lists of data member fields and lists of member function fields
1368 in an instance of a field_info structure, as defined below. */
1369struct field_info
c5aa993b 1370 {
0963b4bd 1371 /* List of data member and baseclasses fields. */
be2daae6
TT
1372 std::vector<struct nextfield> fields;
1373 std::vector<struct nextfield> baseclasses;
c906108c 1374
7d0ccb61 1375 /* Number of fields (including baseclasses). */
be2daae6 1376 int nfields = 0;
c906108c 1377
c5aa993b 1378 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1379 int non_public_fields = 0;
c906108c 1380
c5aa993b
JM
1381 /* Member function fieldlist array, contains name of possibly overloaded
1382 member function, number of overloaded member functions and a pointer
1383 to the head of the member function field chain. */
be2daae6 1384 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1385
1386 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1387 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1388 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1389
1390 /* Nested types defined by this class and the number of elements in this
1391 list. */
be2daae6 1392 std::vector<struct decl_field> nested_types_list;
c5aa993b 1393 };
c906108c 1394
10b3939b
DJ
1395/* One item on the queue of compilation units to read in full symbols
1396 for. */
1397struct dwarf2_queue_item
1398{
1399 struct dwarf2_per_cu_data *per_cu;
95554aad 1400 enum language pretend_language;
10b3939b
DJ
1401 struct dwarf2_queue_item *next;
1402};
1403
1404/* The current queue. */
1405static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1406
ae038cb0
DJ
1407/* Loaded secondary compilation units are kept in memory until they
1408 have not been referenced for the processing of this many
1409 compilation units. Set this to zero to disable caching. Cache
1410 sizes of up to at least twenty will improve startup time for
1411 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1412static int dwarf_max_cache_age = 5;
920d2a44 1413static void
b4f54984
DE
1414show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1415 struct cmd_list_element *c, const char *value)
920d2a44 1416{
3e43a32a 1417 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1418 "DWARF compilation units is %s.\n"),
920d2a44
AC
1419 value);
1420}
4390d890 1421\f
c906108c
SS
1422/* local function prototypes */
1423
a32a8923
DE
1424static const char *get_section_name (const struct dwarf2_section_info *);
1425
1426static const char *get_section_file_name (const struct dwarf2_section_info *);
1427
918dd910
JK
1428static void dwarf2_find_base_address (struct die_info *die,
1429 struct dwarf2_cu *cu);
1430
0018ea6f
DE
1431static struct partial_symtab *create_partial_symtab
1432 (struct dwarf2_per_cu_data *per_cu, const char *name);
1433
f1902523
JK
1434static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1435 const gdb_byte *info_ptr,
1436 struct die_info *type_unit_die,
1437 int has_children, void *data);
1438
ed2dc618
SM
1439static void dwarf2_build_psymtabs_hard
1440 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1441
72bf9492
DJ
1442static void scan_partial_symbols (struct partial_die_info *,
1443 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1444 int, struct dwarf2_cu *);
c906108c 1445
72bf9492
DJ
1446static void add_partial_symbol (struct partial_die_info *,
1447 struct dwarf2_cu *);
63d06c5c 1448
72bf9492
DJ
1449static void add_partial_namespace (struct partial_die_info *pdi,
1450 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1451 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1452
5d7cb8df 1453static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1454 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1455 struct dwarf2_cu *cu);
1456
72bf9492
DJ
1457static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1458 struct dwarf2_cu *cu);
91c24f0a 1459
bc30ff58
JB
1460static void add_partial_subprogram (struct partial_die_info *pdi,
1461 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1462 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1463
257e7a09
YQ
1464static void dwarf2_read_symtab (struct partial_symtab *,
1465 struct objfile *);
c906108c 1466
a14ed312 1467static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1468
685af9cd 1469static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1470 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1471 sect_offset);
433df2d4 1472
d521ce57 1473static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1474
dee91e82 1475static struct partial_die_info *load_partial_dies
d521ce57 1476 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1477
fb816e8b
TV
1478/* A pair of partial_die_info and compilation unit. */
1479struct cu_partial_die_info
1480{
1481 /* The compilation unit of the partial_die_info. */
1482 struct dwarf2_cu *cu;
1483 /* A partial_die_info. */
1484 struct partial_die_info *pdi;
122cf0f2
AB
1485
1486 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1487 : cu (cu),
1488 pdi (pdi)
405feb71 1489 { /* Nothing. */ }
122cf0f2
AB
1490
1491private:
1492 cu_partial_die_info () = delete;
fb816e8b
TV
1493};
1494
122cf0f2
AB
1495static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1496 struct dwarf2_cu *);
72bf9492 1497
d521ce57
TT
1498static const gdb_byte *read_attribute (const struct die_reader_specs *,
1499 struct attribute *, struct attr_abbrev *,
1500 const gdb_byte *);
a8329558 1501
a1855c1d 1502static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1503
a1855c1d 1504static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1505
a1855c1d 1506static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1507
15f18d14
AT
1508/* Read the next three bytes (little-endian order) as an unsigned integer. */
1509static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1510
a1855c1d 1511static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1512
a1855c1d 1513static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1514
d521ce57 1515static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1516 unsigned int *);
c906108c 1517
d521ce57 1518static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1519
1520static LONGEST read_checked_initial_length_and_offset
d521ce57 1521 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1522 unsigned int *, unsigned int *);
613e1657 1523
d521ce57
TT
1524static LONGEST read_offset (bfd *, const gdb_byte *,
1525 const struct comp_unit_head *,
c764a876
DE
1526 unsigned int *);
1527
d521ce57 1528static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1529
ed2dc618
SM
1530static sect_offset read_abbrev_offset
1531 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1532 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1533
d521ce57 1534static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1535
d521ce57 1536static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1537
ed2dc618
SM
1538static const char *read_indirect_string
1539 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1540 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1541
ed2dc618
SM
1542static const char *read_indirect_line_string
1543 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1544 const struct comp_unit_head *, unsigned int *);
36586728 1545
ed2dc618
SM
1546static const char *read_indirect_string_at_offset
1547 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1548 LONGEST str_offset);
927aa2e7 1549
ed2dc618
SM
1550static const char *read_indirect_string_from_dwz
1551 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1552
d521ce57 1553static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1554
d521ce57
TT
1555static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1556 const gdb_byte *,
3019eac3
DE
1557 unsigned int *);
1558
d521ce57 1559static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1560 ULONGEST str_index);
3019eac3 1561
e142c38c 1562static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1563
e142c38c
DJ
1564static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1565 struct dwarf2_cu *);
c906108c 1566
348e048f 1567static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1568 unsigned int);
348e048f 1569
7d45c7c3
KB
1570static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1571 struct dwarf2_cu *cu);
1572
a084a2a6
AT
1573static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1574
05cf31d1
JB
1575static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1576 struct dwarf2_cu *cu);
1577
e142c38c 1578static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1579
e142c38c 1580static struct die_info *die_specification (struct die_info *die,
f2f0e013 1581 struct dwarf2_cu **);
63d06c5c 1582
9c541725 1583static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1584 struct dwarf2_cu *cu);
debd256d 1585
f3f5162e 1586static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1587 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1588 CORE_ADDR, int decode_mapping);
c906108c 1589
804d2729
TT
1590static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1591 const char *);
c906108c 1592
a14ed312 1593static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1594 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1595
ff39bb5e 1596static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1597 struct dwarf2_cu *);
c906108c 1598
ff39bb5e 1599static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1600 struct type *type,
1601 const char *name,
1602 struct obstack *obstack,
12df843f 1603 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1604 const gdb_byte **bytes,
98bfdba5 1605 struct dwarf2_locexpr_baton **baton);
2df3850c 1606
e7c27a73 1607static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1608
b4ba55a1
JB
1609static int need_gnat_info (struct dwarf2_cu *);
1610
3e43a32a
MS
1611static struct type *die_descriptive_type (struct die_info *,
1612 struct dwarf2_cu *);
b4ba55a1
JB
1613
1614static void set_descriptive_type (struct type *, struct die_info *,
1615 struct dwarf2_cu *);
1616
e7c27a73
DJ
1617static struct type *die_containing_type (struct die_info *,
1618 struct dwarf2_cu *);
c906108c 1619
ff39bb5e 1620static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1621 struct dwarf2_cu *);
c906108c 1622
f792889a 1623static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1624
673bfd45
DE
1625static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1626
0d5cff50 1627static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1628
6e70227d 1629static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1630 const char *suffix, int physname,
1631 struct dwarf2_cu *cu);
63d06c5c 1632
e7c27a73 1633static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1634
348e048f
DE
1635static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1636
e7c27a73 1637static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1638
e7c27a73 1639static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1640
96408a79
SA
1641static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1642
71a3c369
TT
1643static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1644
ff013f42
JK
1645static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1646 struct dwarf2_cu *, struct partial_symtab *);
1647
3a2b436a 1648/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1649 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1650enum pc_bounds_kind
1651{
e385593e 1652 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1653 PC_BOUNDS_NOT_PRESENT,
1654
e385593e
JK
1655 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1656 were present but they do not form a valid range of PC addresses. */
1657 PC_BOUNDS_INVALID,
1658
3a2b436a
JK
1659 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1660 PC_BOUNDS_RANGES,
1661
1662 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1663 PC_BOUNDS_HIGH_LOW,
1664};
1665
1666static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1667 CORE_ADDR *, CORE_ADDR *,
1668 struct dwarf2_cu *,
1669 struct partial_symtab *);
c906108c 1670
fae299cd
DC
1671static void get_scope_pc_bounds (struct die_info *,
1672 CORE_ADDR *, CORE_ADDR *,
1673 struct dwarf2_cu *);
1674
801e3a5b
JB
1675static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1676 CORE_ADDR, struct dwarf2_cu *);
1677
a14ed312 1678static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1679 struct dwarf2_cu *);
c906108c 1680
a14ed312 1681static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1682 struct type *, struct dwarf2_cu *);
c906108c 1683
a14ed312 1684static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1685 struct die_info *, struct type *,
e7c27a73 1686 struct dwarf2_cu *);
c906108c 1687
a14ed312 1688static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1689 struct type *,
1690 struct dwarf2_cu *);
c906108c 1691
134d01f1 1692static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1693
e7c27a73 1694static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1695
e7c27a73 1696static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1697
5d7cb8df
JK
1698static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1699
804d2729 1700static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1701
27aa8d6a
SW
1702static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1703
74921315
KS
1704static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1705
f55ee35c
JK
1706static struct type *read_module_type (struct die_info *die,
1707 struct dwarf2_cu *cu);
1708
38d518c9 1709static const char *namespace_name (struct die_info *die,
e142c38c 1710 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1711
134d01f1 1712static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1713
e7c27a73 1714static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1715
6e70227d 1716static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1717 struct dwarf2_cu *);
1718
bf6af496 1719static struct die_info *read_die_and_siblings_1
d521ce57 1720 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1721 struct die_info *);
639d11d3 1722
dee91e82 1723static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1724 const gdb_byte *info_ptr,
1725 const gdb_byte **new_info_ptr,
639d11d3
DC
1726 struct die_info *parent);
1727
d521ce57
TT
1728static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1729 struct die_info **, const gdb_byte *,
1730 int *, int);
3019eac3 1731
d521ce57
TT
1732static const gdb_byte *read_full_die (const struct die_reader_specs *,
1733 struct die_info **, const gdb_byte *,
1734 int *);
93311388 1735
e7c27a73 1736static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1737
15d034d0
TT
1738static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1739 struct obstack *);
71c25dea 1740
15d034d0 1741static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1742
15d034d0 1743static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1744 struct die_info *die,
1745 struct dwarf2_cu *cu);
1746
ca69b9e6
DE
1747static const char *dwarf2_physname (const char *name, struct die_info *die,
1748 struct dwarf2_cu *cu);
1749
e142c38c 1750static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1751 struct dwarf2_cu **);
9219021c 1752
f39c6ffd 1753static const char *dwarf_tag_name (unsigned int);
c906108c 1754
f39c6ffd 1755static const char *dwarf_attr_name (unsigned int);
c906108c 1756
a084a2a6
AT
1757static const char *dwarf_unit_type_name (int unit_type);
1758
f39c6ffd 1759static const char *dwarf_form_name (unsigned int);
c906108c 1760
a121b7c1 1761static const char *dwarf_bool_name (unsigned int);
c906108c 1762
f39c6ffd 1763static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1764
f9aca02d 1765static struct die_info *sibling_die (struct die_info *);
c906108c 1766
d97bc12b
DE
1767static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1768
1769static void dump_die_for_error (struct die_info *);
1770
1771static void dump_die_1 (struct ui_file *, int level, int max_level,
1772 struct die_info *);
c906108c 1773
d97bc12b 1774/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1775
51545339 1776static void store_in_ref_table (struct die_info *,
10b3939b 1777 struct dwarf2_cu *);
c906108c 1778
ff39bb5e 1779static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1780
ff39bb5e 1781static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1782
348e048f 1783static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1784 const struct attribute *,
348e048f
DE
1785 struct dwarf2_cu **);
1786
10b3939b 1787static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1788 const struct attribute *,
f2f0e013 1789 struct dwarf2_cu **);
c906108c 1790
348e048f 1791static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1792 const struct attribute *,
348e048f
DE
1793 struct dwarf2_cu **);
1794
ac9ec31b
DE
1795static struct type *get_signatured_type (struct die_info *, ULONGEST,
1796 struct dwarf2_cu *);
1797
1798static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1799 const struct attribute *,
ac9ec31b
DE
1800 struct dwarf2_cu *);
1801
e5fe5e75 1802static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1803
52dc124a 1804static void read_signatured_type (struct signatured_type *);
348e048f 1805
63e43d3a
PMR
1806static int attr_to_dynamic_prop (const struct attribute *attr,
1807 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1808 struct dynamic_prop *prop, struct type *type);
63e43d3a 1809
c906108c
SS
1810/* memory allocation interface */
1811
7b5a2f43 1812static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1813
b60c80d6 1814static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1815
43f3e411 1816static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1817
6e5a29e1 1818static int attr_form_is_block (const struct attribute *);
8e19ed76 1819
6e5a29e1 1820static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1821
6e5a29e1 1822static int attr_form_is_constant (const struct attribute *);
3690dd37 1823
6e5a29e1 1824static int attr_form_is_ref (const struct attribute *);
7771576e 1825
8cf6f0b1
TT
1826static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1827 struct dwarf2_loclist_baton *baton,
ff39bb5e 1828 const struct attribute *attr);
8cf6f0b1 1829
ff39bb5e 1830static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1831 struct symbol *sym,
f1e6e072
TT
1832 struct dwarf2_cu *cu,
1833 int is_block);
4c2df51b 1834
d521ce57
TT
1835static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1836 const gdb_byte *info_ptr,
1837 struct abbrev_info *abbrev);
4bb7a0a7 1838
72bf9492
DJ
1839static hashval_t partial_die_hash (const void *item);
1840
1841static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1842
ae038cb0 1843static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1844 (sect_offset sect_off, unsigned int offset_in_dwz,
1845 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1846
9816fde3 1847static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1848 struct die_info *comp_unit_die,
1849 enum language pretend_language);
93311388 1850
ed2dc618 1851static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1852
dee91e82 1853static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1854
f792889a
DJ
1855static struct type *set_die_type (struct die_info *, struct type *,
1856 struct dwarf2_cu *);
1c379e20 1857
ed2dc618 1858static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1859
ed2dc618 1860static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1861
58f0c718 1862static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1863 enum language);
10b3939b 1864
95554aad
TT
1865static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1866 enum language);
10b3939b 1867
f4dc4d17
DE
1868static void process_full_type_unit (struct dwarf2_per_cu_data *,
1869 enum language);
1870
10b3939b
DJ
1871static void dwarf2_add_dependence (struct dwarf2_cu *,
1872 struct dwarf2_per_cu_data *);
1873
ae038cb0
DJ
1874static void dwarf2_mark (struct dwarf2_cu *);
1875
1876static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1877
b64f50a1 1878static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1879 struct dwarf2_per_cu_data *);
673bfd45 1880
f792889a 1881static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1882
95554aad
TT
1883static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1884 enum language pretend_language);
1885
ed2dc618 1886static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1887
9a49df9d
AB
1888static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1889static struct type *dwarf2_per_cu_addr_sized_int_type
1890 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1891
b303c6f6
AB
1892/* Class, the destructor of which frees all allocated queue entries. This
1893 will only have work to do if an error was thrown while processing the
1894 dwarf. If no error was thrown then the queue entries should have all
1895 been processed, and freed, as we went along. */
1896
1897class dwarf2_queue_guard
1898{
1899public:
1900 dwarf2_queue_guard () = default;
1901
1902 /* Free any entries remaining on the queue. There should only be
1903 entries left if we hit an error while processing the dwarf. */
1904 ~dwarf2_queue_guard ()
1905 {
1906 struct dwarf2_queue_item *item, *last;
1907
1908 item = dwarf2_queue;
1909 while (item)
1910 {
1911 /* Anything still marked queued is likely to be in an
1912 inconsistent state, so discard it. */
1913 if (item->per_cu->queued)
1914 {
1915 if (item->per_cu->cu != NULL)
1916 free_one_cached_comp_unit (item->per_cu);
1917 item->per_cu->queued = 0;
1918 }
1919
1920 last = item;
1921 item = item->next;
1922 xfree (last);
1923 }
1924
1925 dwarf2_queue = dwarf2_queue_tail = NULL;
1926 }
1927};
1928
d721ba37
PA
1929/* The return type of find_file_and_directory. Note, the enclosed
1930 string pointers are only valid while this object is valid. */
1931
1932struct file_and_directory
1933{
1934 /* The filename. This is never NULL. */
1935 const char *name;
1936
1937 /* The compilation directory. NULL if not known. If we needed to
1938 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1939 points directly to the DW_AT_comp_dir string attribute owned by
1940 the obstack that owns the DIE. */
1941 const char *comp_dir;
1942
1943 /* If we needed to build a new string for comp_dir, this is what
1944 owns the storage. */
1945 std::string comp_dir_storage;
1946};
1947
1948static file_and_directory find_file_and_directory (struct die_info *die,
1949 struct dwarf2_cu *cu);
9291a0cd
TT
1950
1951static char *file_full_name (int file, struct line_header *lh,
1952 const char *comp_dir);
1953
43988095
JK
1954/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1955enum class rcuh_kind { COMPILE, TYPE };
1956
d521ce57 1957static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1958 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1959 struct comp_unit_head *header,
36586728 1960 struct dwarf2_section_info *section,
d521ce57 1961 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1962 rcuh_kind section_kind);
36586728 1963
fd820528 1964static void init_cutu_and_read_dies
f4dc4d17 1965 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1966 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1967 die_reader_func_ftype *die_reader_func, void *data);
1968
dee91e82
DE
1969static void init_cutu_and_read_dies_simple
1970 (struct dwarf2_per_cu_data *this_cu,
1971 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1972
673bfd45 1973static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1974
3019eac3
DE
1975static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1976
57d63ce2 1977static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1978 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1979 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1980 ULONGEST signature, int is_debug_types);
a2ce51a0 1981
ed2dc618
SM
1982static struct dwp_file *get_dwp_file
1983 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1984
3019eac3 1985static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1986 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1987
1988static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1989 (struct signatured_type *, const char *, const char *);
3019eac3 1990
89e63ee4
DE
1991static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1992
263db9a1
TT
1993/* A unique pointer to a dwo_file. */
1994
51ac9db5 1995typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1996
ed2dc618 1997static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1998
1b80a9fa 1999static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2000
2001static void free_line_header_voidp (void *arg);
4390d890
DE
2002\f
2003/* Various complaints about symbol reading that don't abort the process. */
2004
2005static void
2006dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2007{
b98664d3 2008 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2009}
2010
2011static void
2012dwarf2_debug_line_missing_file_complaint (void)
2013{
b98664d3 2014 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2015}
2016
2017static void
2018dwarf2_debug_line_missing_end_sequence_complaint (void)
2019{
b98664d3 2020 complaint (_(".debug_line section has line "
4390d890
DE
2021 "program sequence without an end"));
2022}
2023
2024static void
2025dwarf2_complex_location_expr_complaint (void)
2026{
b98664d3 2027 complaint (_("location expression too complex"));
4390d890
DE
2028}
2029
2030static void
2031dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2032 int arg3)
2033{
b98664d3 2034 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2035 arg1, arg2, arg3);
2036}
2037
2038static void
2039dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2040{
b98664d3 2041 complaint (_("debug info runs off end of %s section"
4390d890 2042 " [in module %s]"),
a32a8923
DE
2043 get_section_name (section),
2044 get_section_file_name (section));
4390d890 2045}
1b80a9fa 2046
4390d890
DE
2047static void
2048dwarf2_macro_malformed_definition_complaint (const char *arg1)
2049{
b98664d3 2050 complaint (_("macro debug info contains a "
4390d890
DE
2051 "malformed macro definition:\n`%s'"),
2052 arg1);
2053}
2054
2055static void
2056dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2057{
b98664d3 2058 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2059 arg1, arg2);
2060}
527f3840
JK
2061
2062/* Hash function for line_header_hash. */
2063
2064static hashval_t
2065line_header_hash (const struct line_header *ofs)
2066{
9c541725 2067 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2068}
2069
2070/* Hash function for htab_create_alloc_ex for line_header_hash. */
2071
2072static hashval_t
2073line_header_hash_voidp (const void *item)
2074{
9a3c8263 2075 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2076
2077 return line_header_hash (ofs);
2078}
2079
2080/* Equality function for line_header_hash. */
2081
2082static int
2083line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2084{
9a3c8263
SM
2085 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2086 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2087
9c541725 2088 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2089 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2090}
2091
4390d890 2092\f
9291a0cd 2093
31aa7e4e
JB
2094/* Read the given attribute value as an address, taking the attribute's
2095 form into account. */
2096
2097static CORE_ADDR
2098attr_value_as_address (struct attribute *attr)
2099{
2100 CORE_ADDR addr;
2101
336d760d
AT
2102 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2103 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2104 {
2105 /* Aside from a few clearly defined exceptions, attributes that
2106 contain an address must always be in DW_FORM_addr form.
2107 Unfortunately, some compilers happen to be violating this
2108 requirement by encoding addresses using other forms, such
2109 as DW_FORM_data4 for example. For those broken compilers,
2110 we try to do our best, without any guarantee of success,
2111 to interpret the address correctly. It would also be nice
2112 to generate a complaint, but that would require us to maintain
2113 a list of legitimate cases where a non-address form is allowed,
2114 as well as update callers to pass in at least the CU's DWARF
2115 version. This is more overhead than what we're willing to
2116 expand for a pretty rare case. */
2117 addr = DW_UNSND (attr);
2118 }
2119 else
2120 addr = DW_ADDR (attr);
2121
2122 return addr;
2123}
2124
330cdd98
PA
2125/* See declaration. */
2126
2127dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2128 const dwarf2_debug_sections *names,
2129 bool can_copy_)
2130 : objfile (objfile_),
2131 can_copy (can_copy_)
330cdd98
PA
2132{
2133 if (names == NULL)
2134 names = &dwarf2_elf_names;
2135
2136 bfd *obfd = objfile->obfd;
2137
2138 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2139 locate_sections (obfd, sec, *names);
2140}
2141
2142dwarf2_per_objfile::~dwarf2_per_objfile ()
2143{
2144 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2145 free_cached_comp_units ();
2146
2147 if (quick_file_names_table)
2148 htab_delete (quick_file_names_table);
2149
2150 if (line_header_hash)
2151 htab_delete (line_header_hash);
2152
b76e467d 2153 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2154 per_cu->imported_symtabs_free ();
fc8e7e75 2155
b2bdb8cf 2156 for (signatured_type *sig_type : all_type_units)
ae640021 2157 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2158
330cdd98
PA
2159 /* Everything else should be on the objfile obstack. */
2160}
2161
2162/* See declaration. */
2163
2164void
2165dwarf2_per_objfile::free_cached_comp_units ()
2166{
2167 dwarf2_per_cu_data *per_cu = read_in_chain;
2168 dwarf2_per_cu_data **last_chain = &read_in_chain;
2169 while (per_cu != NULL)
2170 {
2171 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2172
fcd3b13d 2173 delete per_cu->cu;
330cdd98
PA
2174 *last_chain = next_cu;
2175 per_cu = next_cu;
2176 }
2177}
2178
11ed8cad
TT
2179/* A helper class that calls free_cached_comp_units on
2180 destruction. */
2181
2182class free_cached_comp_units
2183{
2184public:
2185
2186 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2187 : m_per_objfile (per_objfile)
2188 {
2189 }
2190
2191 ~free_cached_comp_units ()
2192 {
2193 m_per_objfile->free_cached_comp_units ();
2194 }
2195
2196 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2197
2198private:
2199
2200 dwarf2_per_objfile *m_per_objfile;
2201};
2202
c906108c 2203/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2204 information and return true if we have enough to do something.
2205 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2206 ELF names are used. CAN_COPY is true for formats where symbol
2207 interposition is possible and so symbol values must follow copy
2208 relocation rules. */
c906108c
SS
2209
2210int
251d32d9 2211dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2212 const struct dwarf2_debug_sections *names,
2213 bool can_copy)
c906108c 2214{
97cbe998
SDJ
2215 if (objfile->flags & OBJF_READNEVER)
2216 return 0;
2217
ed2dc618
SM
2218 struct dwarf2_per_objfile *dwarf2_per_objfile
2219 = get_dwarf2_per_objfile (objfile);
2220
2221 if (dwarf2_per_objfile == NULL)
5bfd760d 2222 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2223 names,
2224 can_copy);
5bfd760d 2225
73869dc2 2226 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2227 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2228 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2229 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2230}
2231
2232/* Return the containing section of virtual section SECTION. */
2233
2234static struct dwarf2_section_info *
2235get_containing_section (const struct dwarf2_section_info *section)
2236{
2237 gdb_assert (section->is_virtual);
2238 return section->s.containing_section;
c906108c
SS
2239}
2240
a32a8923
DE
2241/* Return the bfd owner of SECTION. */
2242
2243static struct bfd *
2244get_section_bfd_owner (const struct dwarf2_section_info *section)
2245{
73869dc2
DE
2246 if (section->is_virtual)
2247 {
2248 section = get_containing_section (section);
2249 gdb_assert (!section->is_virtual);
2250 }
049412e3 2251 return section->s.section->owner;
a32a8923
DE
2252}
2253
2254/* Return the bfd section of SECTION.
2255 Returns NULL if the section is not present. */
2256
2257static asection *
2258get_section_bfd_section (const struct dwarf2_section_info *section)
2259{
73869dc2
DE
2260 if (section->is_virtual)
2261 {
2262 section = get_containing_section (section);
2263 gdb_assert (!section->is_virtual);
2264 }
049412e3 2265 return section->s.section;
a32a8923
DE
2266}
2267
2268/* Return the name of SECTION. */
2269
2270static const char *
2271get_section_name (const struct dwarf2_section_info *section)
2272{
2273 asection *sectp = get_section_bfd_section (section);
2274
2275 gdb_assert (sectp != NULL);
fd361982 2276 return bfd_section_name (sectp);
a32a8923
DE
2277}
2278
2279/* Return the name of the file SECTION is in. */
2280
2281static const char *
2282get_section_file_name (const struct dwarf2_section_info *section)
2283{
2284 bfd *abfd = get_section_bfd_owner (section);
2285
2286 return bfd_get_filename (abfd);
2287}
2288
2289/* Return the id of SECTION.
2290 Returns 0 if SECTION doesn't exist. */
2291
2292static int
2293get_section_id (const struct dwarf2_section_info *section)
2294{
2295 asection *sectp = get_section_bfd_section (section);
2296
2297 if (sectp == NULL)
2298 return 0;
2299 return sectp->id;
2300}
2301
2302/* Return the flags of SECTION.
73869dc2 2303 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2304
2305static int
2306get_section_flags (const struct dwarf2_section_info *section)
2307{
2308 asection *sectp = get_section_bfd_section (section);
2309
2310 gdb_assert (sectp != NULL);
fd361982 2311 return bfd_section_flags (sectp);
a32a8923
DE
2312}
2313
251d32d9
TG
2314/* When loading sections, we look either for uncompressed section or for
2315 compressed section names. */
233a11ab
CS
2316
2317static int
251d32d9
TG
2318section_is_p (const char *section_name,
2319 const struct dwarf2_section_names *names)
233a11ab 2320{
251d32d9
TG
2321 if (names->normal != NULL
2322 && strcmp (section_name, names->normal) == 0)
2323 return 1;
2324 if (names->compressed != NULL
2325 && strcmp (section_name, names->compressed) == 0)
2326 return 1;
2327 return 0;
233a11ab
CS
2328}
2329
330cdd98 2330/* See declaration. */
c906108c 2331
330cdd98
PA
2332void
2333dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2334 const dwarf2_debug_sections &names)
c906108c 2335{
fd361982 2336 flagword aflag = bfd_section_flags (sectp);
251d32d9 2337
dc7650b8
JK
2338 if ((aflag & SEC_HAS_CONTENTS) == 0)
2339 {
2340 }
950b7495
KS
2341 else if (elf_section_data (sectp)->this_hdr.sh_size
2342 > bfd_get_file_size (abfd))
2343 {
2344 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2345 warning (_("Discarding section %s which has a section size (%s"
2346 ") larger than the file size [in module %s]"),
2347 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2348 bfd_get_filename (abfd));
2349 }
330cdd98 2350 else if (section_is_p (sectp->name, &names.info))
c906108c 2351 {
330cdd98 2352 this->info.s.section = sectp;
fd361982 2353 this->info.size = bfd_section_size (sectp);
c906108c 2354 }
330cdd98 2355 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2356 {
330cdd98 2357 this->abbrev.s.section = sectp;
fd361982 2358 this->abbrev.size = bfd_section_size (sectp);
c906108c 2359 }
330cdd98 2360 else if (section_is_p (sectp->name, &names.line))
c906108c 2361 {
330cdd98 2362 this->line.s.section = sectp;
fd361982 2363 this->line.size = bfd_section_size (sectp);
c906108c 2364 }
330cdd98 2365 else if (section_is_p (sectp->name, &names.loc))
c906108c 2366 {
330cdd98 2367 this->loc.s.section = sectp;
fd361982 2368 this->loc.size = bfd_section_size (sectp);
c906108c 2369 }
330cdd98 2370 else if (section_is_p (sectp->name, &names.loclists))
43988095 2371 {
330cdd98 2372 this->loclists.s.section = sectp;
fd361982 2373 this->loclists.size = bfd_section_size (sectp);
43988095 2374 }
330cdd98 2375 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2376 {
330cdd98 2377 this->macinfo.s.section = sectp;
fd361982 2378 this->macinfo.size = bfd_section_size (sectp);
c906108c 2379 }
330cdd98 2380 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2381 {
330cdd98 2382 this->macro.s.section = sectp;
fd361982 2383 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2384 }
330cdd98 2385 else if (section_is_p (sectp->name, &names.str))
c906108c 2386 {
330cdd98 2387 this->str.s.section = sectp;
fd361982 2388 this->str.size = bfd_section_size (sectp);
c906108c 2389 }
330cdd98 2390 else if (section_is_p (sectp->name, &names.line_str))
43988095 2391 {
330cdd98 2392 this->line_str.s.section = sectp;
fd361982 2393 this->line_str.size = bfd_section_size (sectp);
43988095 2394 }
330cdd98 2395 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2396 {
330cdd98 2397 this->addr.s.section = sectp;
fd361982 2398 this->addr.size = bfd_section_size (sectp);
3019eac3 2399 }
330cdd98 2400 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2401 {
330cdd98 2402 this->frame.s.section = sectp;
fd361982 2403 this->frame.size = bfd_section_size (sectp);
b6af0555 2404 }
330cdd98 2405 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2406 {
330cdd98 2407 this->eh_frame.s.section = sectp;
fd361982 2408 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2409 }
330cdd98 2410 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2411 {
330cdd98 2412 this->ranges.s.section = sectp;
fd361982 2413 this->ranges.size = bfd_section_size (sectp);
af34e669 2414 }
330cdd98 2415 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2416 {
330cdd98 2417 this->rnglists.s.section = sectp;
fd361982 2418 this->rnglists.size = bfd_section_size (sectp);
43988095 2419 }
330cdd98 2420 else if (section_is_p (sectp->name, &names.types))
348e048f 2421 {
8b70b953
TT
2422 struct dwarf2_section_info type_section;
2423
2424 memset (&type_section, 0, sizeof (type_section));
049412e3 2425 type_section.s.section = sectp;
fd361982 2426 type_section.size = bfd_section_size (sectp);
8b70b953 2427
fd5866f6 2428 this->types.push_back (type_section);
348e048f 2429 }
330cdd98 2430 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2431 {
330cdd98 2432 this->gdb_index.s.section = sectp;
fd361982 2433 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2434 }
927aa2e7
JK
2435 else if (section_is_p (sectp->name, &names.debug_names))
2436 {
2437 this->debug_names.s.section = sectp;
fd361982 2438 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2439 }
2440 else if (section_is_p (sectp->name, &names.debug_aranges))
2441 {
2442 this->debug_aranges.s.section = sectp;
fd361982 2443 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2444 }
dce234bc 2445
fd361982
AM
2446 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2447 && bfd_section_vma (sectp) == 0)
330cdd98 2448 this->has_section_at_zero = true;
c906108c
SS
2449}
2450
fceca515
DE
2451/* A helper function that decides whether a section is empty,
2452 or not present. */
9e0ac564
TT
2453
2454static int
19ac8c2e 2455dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2456{
73869dc2
DE
2457 if (section->is_virtual)
2458 return section->size == 0;
049412e3 2459 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2460}
2461
cd4fb1b2 2462/* See dwarf2read.h. */
c906108c 2463
cd4fb1b2
SM
2464void
2465dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2466{
a32a8923 2467 asection *sectp;
3019eac3 2468 bfd *abfd;
dce234bc 2469 gdb_byte *buf, *retbuf;
c906108c 2470
be391dca
TT
2471 if (info->readin)
2472 return;
dce234bc 2473 info->buffer = NULL;
dc4ccb6f 2474 info->readin = true;
188dd5d6 2475
9e0ac564 2476 if (dwarf2_section_empty_p (info))
dce234bc 2477 return;
c906108c 2478
a32a8923 2479 sectp = get_section_bfd_section (info);
3019eac3 2480
73869dc2
DE
2481 /* If this is a virtual section we need to read in the real one first. */
2482 if (info->is_virtual)
2483 {
2484 struct dwarf2_section_info *containing_section =
2485 get_containing_section (info);
2486
2487 gdb_assert (sectp != NULL);
2488 if ((sectp->flags & SEC_RELOC) != 0)
2489 {
2490 error (_("Dwarf Error: DWP format V2 with relocations is not"
2491 " supported in section %s [in module %s]"),
2492 get_section_name (info), get_section_file_name (info));
2493 }
2494 dwarf2_read_section (objfile, containing_section);
2495 /* Other code should have already caught virtual sections that don't
2496 fit. */
2497 gdb_assert (info->virtual_offset + info->size
2498 <= containing_section->size);
2499 /* If the real section is empty or there was a problem reading the
2500 section we shouldn't get here. */
2501 gdb_assert (containing_section->buffer != NULL);
2502 info->buffer = containing_section->buffer + info->virtual_offset;
2503 return;
2504 }
2505
4bf44c1c
TT
2506 /* If the section has relocations, we must read it ourselves.
2507 Otherwise we attach it to the BFD. */
2508 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2509 {
d521ce57 2510 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2511 return;
dce234bc 2512 }
dce234bc 2513
224c3ddb 2514 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2515 info->buffer = buf;
dce234bc
PP
2516
2517 /* When debugging .o files, we may need to apply relocations; see
2518 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2519 We never compress sections in .o files, so we only need to
2520 try this when the section is not compressed. */
ac8035ab 2521 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2522 if (retbuf != NULL)
2523 {
2524 info->buffer = retbuf;
2525 return;
2526 }
2527
a32a8923
DE
2528 abfd = get_section_bfd_owner (info);
2529 gdb_assert (abfd != NULL);
2530
dce234bc
PP
2531 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2532 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2533 {
2534 error (_("Dwarf Error: Can't read DWARF data"
2535 " in section %s [in module %s]"),
fd361982 2536 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2537 }
dce234bc
PP
2538}
2539
9e0ac564
TT
2540/* A helper function that returns the size of a section in a safe way.
2541 If you are positive that the section has been read before using the
2542 size, then it is safe to refer to the dwarf2_section_info object's
2543 "size" field directly. In other cases, you must call this
2544 function, because for compressed sections the size field is not set
2545 correctly until the section has been read. */
2546
2547static bfd_size_type
2548dwarf2_section_size (struct objfile *objfile,
2549 struct dwarf2_section_info *info)
2550{
2551 if (!info->readin)
2552 dwarf2_read_section (objfile, info);
2553 return info->size;
2554}
2555
dce234bc 2556/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2557 SECTION_NAME. */
af34e669 2558
dce234bc 2559void
3017a003
TG
2560dwarf2_get_section_info (struct objfile *objfile,
2561 enum dwarf2_section_enum sect,
d521ce57 2562 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2563 bfd_size_type *sizep)
2564{
5bfd760d 2565 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2566 struct dwarf2_section_info *info;
a3b2a86b
TT
2567
2568 /* We may see an objfile without any DWARF, in which case we just
2569 return nothing. */
2570 if (data == NULL)
2571 {
2572 *sectp = NULL;
2573 *bufp = NULL;
2574 *sizep = 0;
2575 return;
2576 }
3017a003
TG
2577 switch (sect)
2578 {
2579 case DWARF2_DEBUG_FRAME:
2580 info = &data->frame;
2581 break;
2582 case DWARF2_EH_FRAME:
2583 info = &data->eh_frame;
2584 break;
2585 default:
2586 gdb_assert_not_reached ("unexpected section");
2587 }
dce234bc 2588
9e0ac564 2589 dwarf2_read_section (objfile, info);
dce234bc 2590
a32a8923 2591 *sectp = get_section_bfd_section (info);
dce234bc
PP
2592 *bufp = info->buffer;
2593 *sizep = info->size;
2594}
2595
36586728
TT
2596/* A helper function to find the sections for a .dwz file. */
2597
2598static void
2599locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2600{
9a3c8263 2601 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2602
2603 /* Note that we only support the standard ELF names, because .dwz
2604 is ELF-only (at the time of writing). */
2605 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2606 {
049412e3 2607 dwz_file->abbrev.s.section = sectp;
fd361982 2608 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2609 }
2610 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2611 {
049412e3 2612 dwz_file->info.s.section = sectp;
fd361982 2613 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2614 }
2615 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2616 {
049412e3 2617 dwz_file->str.s.section = sectp;
fd361982 2618 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2619 }
2620 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2621 {
049412e3 2622 dwz_file->line.s.section = sectp;
fd361982 2623 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2624 }
2625 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2626 {
049412e3 2627 dwz_file->macro.s.section = sectp;
fd361982 2628 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2629 }
2ec9a5e0
TT
2630 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2631 {
049412e3 2632 dwz_file->gdb_index.s.section = sectp;
fd361982 2633 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2634 }
927aa2e7
JK
2635 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2636 {
2637 dwz_file->debug_names.s.section = sectp;
fd361982 2638 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2639 }
36586728
TT
2640}
2641
c4973306 2642/* See dwarf2read.h. */
36586728 2643
c4973306 2644struct dwz_file *
ed2dc618 2645dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2646{
36586728 2647 const char *filename;
acd13123 2648 bfd_size_type buildid_len_arg;
dc294be5
TT
2649 size_t buildid_len;
2650 bfd_byte *buildid;
36586728
TT
2651
2652 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2653 return dwarf2_per_objfile->dwz_file.get ();
36586728 2654
4db1a1dc 2655 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2656 gdb::unique_xmalloc_ptr<char> data
2657 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2658 &buildid_len_arg, &buildid));
4db1a1dc
TT
2659 if (data == NULL)
2660 {
2661 if (bfd_get_error () == bfd_error_no_error)
2662 return NULL;
2663 error (_("could not read '.gnu_debugaltlink' section: %s"),
2664 bfd_errmsg (bfd_get_error ()));
2665 }
791afaa2
TT
2666
2667 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2668
acd13123
TT
2669 buildid_len = (size_t) buildid_len_arg;
2670
791afaa2 2671 filename = data.get ();
d721ba37
PA
2672
2673 std::string abs_storage;
36586728
TT
2674 if (!IS_ABSOLUTE_PATH (filename))
2675 {
14278e1f
TT
2676 gdb::unique_xmalloc_ptr<char> abs
2677 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2678
14278e1f 2679 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2680 filename = abs_storage.c_str ();
36586728
TT
2681 }
2682
dc294be5
TT
2683 /* First try the file name given in the section. If that doesn't
2684 work, try to use the build-id instead. */
192b62ce 2685 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2686 if (dwz_bfd != NULL)
36586728 2687 {
192b62ce 2688 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2689 dwz_bfd.reset (nullptr);
36586728
TT
2690 }
2691
dc294be5
TT
2692 if (dwz_bfd == NULL)
2693 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2694
2695 if (dwz_bfd == NULL)
2696 error (_("could not find '.gnu_debugaltlink' file for %s"),
2697 objfile_name (dwarf2_per_objfile->objfile));
2698
7ff8cb8c
TT
2699 std::unique_ptr<struct dwz_file> result
2700 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2701
7ff8cb8c
TT
2702 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2703 result.get ());
36586728 2704
7ff8cb8c
TT
2705 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2706 result->dwz_bfd.get ());
2707 dwarf2_per_objfile->dwz_file = std::move (result);
2708 return dwarf2_per_objfile->dwz_file.get ();
36586728 2709}
9291a0cd 2710\f
7b9f3c50
DE
2711/* DWARF quick_symbols_functions support. */
2712
2713/* TUs can share .debug_line entries, and there can be a lot more TUs than
2714 unique line tables, so we maintain a separate table of all .debug_line
2715 derived entries to support the sharing.
2716 All the quick functions need is the list of file names. We discard the
2717 line_header when we're done and don't need to record it here. */
2718struct quick_file_names
2719{
094b34ac
DE
2720 /* The data used to construct the hash key. */
2721 struct stmt_list_hash hash;
7b9f3c50
DE
2722
2723 /* The number of entries in file_names, real_names. */
2724 unsigned int num_file_names;
2725
2726 /* The file names from the line table, after being run through
2727 file_full_name. */
2728 const char **file_names;
2729
2730 /* The file names from the line table after being run through
2731 gdb_realpath. These are computed lazily. */
2732 const char **real_names;
2733};
2734
2735/* When using the index (and thus not using psymtabs), each CU has an
2736 object of this type. This is used to hold information needed by
2737 the various "quick" methods. */
2738struct dwarf2_per_cu_quick_data
2739{
2740 /* The file table. This can be NULL if there was no file table
2741 or it's currently not read in.
2742 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2743 struct quick_file_names *file_names;
2744
2745 /* The corresponding symbol table. This is NULL if symbols for this
2746 CU have not yet been read. */
43f3e411 2747 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2748
2749 /* A temporary mark bit used when iterating over all CUs in
2750 expand_symtabs_matching. */
2751 unsigned int mark : 1;
2752
2753 /* True if we've tried to read the file table and found there isn't one.
2754 There will be no point in trying to read it again next time. */
2755 unsigned int no_file_data : 1;
2756};
2757
094b34ac
DE
2758/* Utility hash function for a stmt_list_hash. */
2759
2760static hashval_t
2761hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2762{
2763 hashval_t v = 0;
2764
2765 if (stmt_list_hash->dwo_unit != NULL)
2766 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2767 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2768 return v;
2769}
2770
2771/* Utility equality function for a stmt_list_hash. */
2772
2773static int
2774eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2775 const struct stmt_list_hash *rhs)
2776{
2777 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2778 return 0;
2779 if (lhs->dwo_unit != NULL
2780 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2781 return 0;
2782
9c541725 2783 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2784}
2785
7b9f3c50
DE
2786/* Hash function for a quick_file_names. */
2787
2788static hashval_t
2789hash_file_name_entry (const void *e)
2790{
9a3c8263
SM
2791 const struct quick_file_names *file_data
2792 = (const struct quick_file_names *) e;
7b9f3c50 2793
094b34ac 2794 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2795}
2796
2797/* Equality function for a quick_file_names. */
2798
2799static int
2800eq_file_name_entry (const void *a, const void *b)
2801{
9a3c8263
SM
2802 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2803 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2804
094b34ac 2805 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2806}
2807
2808/* Delete function for a quick_file_names. */
2809
2810static void
2811delete_file_name_entry (void *e)
2812{
9a3c8263 2813 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2814 int i;
2815
2816 for (i = 0; i < file_data->num_file_names; ++i)
2817 {
2818 xfree ((void*) file_data->file_names[i]);
2819 if (file_data->real_names)
2820 xfree ((void*) file_data->real_names[i]);
2821 }
2822
2823 /* The space for the struct itself lives on objfile_obstack,
2824 so we don't free it here. */
2825}
2826
2827/* Create a quick_file_names hash table. */
2828
2829static htab_t
2830create_quick_file_names_table (unsigned int nr_initial_entries)
2831{
2832 return htab_create_alloc (nr_initial_entries,
2833 hash_file_name_entry, eq_file_name_entry,
2834 delete_file_name_entry, xcalloc, xfree);
2835}
9291a0cd 2836
918dd910
JK
2837/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2838 have to be created afterwards. You should call age_cached_comp_units after
2839 processing PER_CU->CU. dw2_setup must have been already called. */
2840
2841static void
58f0c718 2842load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2843{
3019eac3 2844 if (per_cu->is_debug_types)
e5fe5e75 2845 load_full_type_unit (per_cu);
918dd910 2846 else
58f0c718 2847 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2848
cc12ce38
DE
2849 if (per_cu->cu == NULL)
2850 return; /* Dummy CU. */
2dc860c0
DE
2851
2852 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2853}
2854
a0f42c21 2855/* Read in the symbols for PER_CU. */
2fdf6df6 2856
9291a0cd 2857static void
58f0c718 2858dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2859{
ed2dc618 2860 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2861
f4dc4d17
DE
2862 /* Skip type_unit_groups, reading the type units they contain
2863 is handled elsewhere. */
2864 if (IS_TYPE_UNIT_GROUP (per_cu))
2865 return;
2866
b303c6f6
AB
2867 /* The destructor of dwarf2_queue_guard frees any entries left on
2868 the queue. After this point we're guaranteed to leave this function
2869 with the dwarf queue empty. */
2870 dwarf2_queue_guard q_guard;
9291a0cd 2871
95554aad 2872 if (dwarf2_per_objfile->using_index
43f3e411 2873 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2874 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2875 {
2876 queue_comp_unit (per_cu, language_minimal);
58f0c718 2877 load_cu (per_cu, skip_partial);
89e63ee4
DE
2878
2879 /* If we just loaded a CU from a DWO, and we're working with an index
2880 that may badly handle TUs, load all the TUs in that DWO as well.
2881 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2882 if (!per_cu->is_debug_types
cc12ce38 2883 && per_cu->cu != NULL
89e63ee4
DE
2884 && per_cu->cu->dwo_unit != NULL
2885 && dwarf2_per_objfile->index_table != NULL
2886 && dwarf2_per_objfile->index_table->version <= 7
2887 /* DWP files aren't supported yet. */
ed2dc618 2888 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2889 queue_and_load_all_dwo_tus (per_cu);
95554aad 2890 }
9291a0cd 2891
ed2dc618 2892 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2893
2894 /* Age the cache, releasing compilation units that have not
2895 been used recently. */
ed2dc618 2896 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2897}
2898
2899/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2900 the objfile from which this CU came. Returns the resulting symbol
2901 table. */
2fdf6df6 2902
43f3e411 2903static struct compunit_symtab *
58f0c718 2904dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2905{
ed2dc618
SM
2906 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2907
95554aad 2908 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2909 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2910 {
11ed8cad 2911 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2912 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2913 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2914 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2915 }
f194fefb 2916
43f3e411 2917 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2918}
2919
ff4c9fec 2920/* See declaration. */
f4dc4d17 2921
ff4c9fec
SM
2922dwarf2_per_cu_data *
2923dwarf2_per_objfile::get_cutu (int index)
2924{
b76e467d 2925 if (index >= this->all_comp_units.size ())
ff4c9fec 2926 {
b76e467d 2927 index -= this->all_comp_units.size ();
b2bdb8cf 2928 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2929 return &this->all_type_units[index]->per_cu;
2930 }
f4dc4d17 2931
ff4c9fec
SM
2932 return this->all_comp_units[index];
2933}
f4dc4d17 2934
ff4c9fec 2935/* See declaration. */
2fdf6df6 2936
ff4c9fec
SM
2937dwarf2_per_cu_data *
2938dwarf2_per_objfile::get_cu (int index)
1fd400ff 2939{
b76e467d 2940 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2941
ff4c9fec 2942 return this->all_comp_units[index];
f4dc4d17
DE
2943}
2944
ff4c9fec 2945/* See declaration. */
f4dc4d17 2946
ff4c9fec
SM
2947signatured_type *
2948dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2949{
b2bdb8cf 2950 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2951
ff4c9fec 2952 return this->all_type_units[index];
1fd400ff
TT
2953}
2954
4b514bc8
JK
2955/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2956 objfile_obstack, and constructed with the specified field
2957 values. */
2958
2959static dwarf2_per_cu_data *
ed2dc618 2960create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2961 struct dwarf2_section_info *section,
2962 int is_dwz,
2963 sect_offset sect_off, ULONGEST length)
2964{
ed2dc618 2965 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2966 dwarf2_per_cu_data *the_cu
2967 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2968 struct dwarf2_per_cu_data);
2969 the_cu->sect_off = sect_off;
2970 the_cu->length = length;
e3b94546 2971 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2972 the_cu->section = section;
2973 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2974 struct dwarf2_per_cu_quick_data);
2975 the_cu->is_dwz = is_dwz;
2976 return the_cu;
2977}
2978
2ec9a5e0
TT
2979/* A helper for create_cus_from_index that handles a given list of
2980 CUs. */
2fdf6df6 2981
74a0d9f6 2982static void
12359b5e 2983create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2984 const gdb_byte *cu_list, offset_type n_elements,
2985 struct dwarf2_section_info *section,
b76e467d 2986 int is_dwz)
9291a0cd 2987{
12359b5e 2988 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2989 {
74a0d9f6 2990 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2991
2992 sect_offset sect_off
2993 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2994 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2995 cu_list += 2 * 8;
2996
b76e467d 2997 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2998 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2999 sect_off, length);
b76e467d 3000 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3001 }
9291a0cd
TT
3002}
3003
2ec9a5e0 3004/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3005 the CU objects for this objfile. */
2ec9a5e0 3006
74a0d9f6 3007static void
12359b5e 3008create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3009 const gdb_byte *cu_list, offset_type cu_list_elements,
3010 const gdb_byte *dwz_list, offset_type dwz_elements)
3011{
b76e467d
SM
3012 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3013 dwarf2_per_objfile->all_comp_units.reserve
3014 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3015
12359b5e 3016 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3017 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3018
3019 if (dwz_elements == 0)
74a0d9f6 3020 return;
2ec9a5e0 3021
12359b5e
SM
3022 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3023 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3024 &dwz->info, 1);
2ec9a5e0
TT
3025}
3026
1fd400ff 3027/* Create the signatured type hash table from the index. */
673bfd45 3028
74a0d9f6 3029static void
12359b5e
SM
3030create_signatured_type_table_from_index
3031 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3032 struct dwarf2_section_info *section,
3033 const gdb_byte *bytes,
3034 offset_type elements)
1fd400ff 3035{
12359b5e 3036 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3037
b2bdb8cf
SM
3038 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3039 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3040
12359b5e 3041 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3042
12359b5e 3043 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3044 {
52dc124a 3045 struct signatured_type *sig_type;
9c541725 3046 ULONGEST signature;
1fd400ff 3047 void **slot;
9c541725 3048 cu_offset type_offset_in_tu;
1fd400ff 3049
74a0d9f6 3050 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3051 sect_offset sect_off
3052 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3053 type_offset_in_tu
3054 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3055 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3056 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3057 bytes += 3 * 8;
3058
52dc124a 3059 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3060 struct signatured_type);
52dc124a 3061 sig_type->signature = signature;
9c541725 3062 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3063 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3064 sig_type->per_cu.section = section;
9c541725 3065 sig_type->per_cu.sect_off = sect_off;
e3b94546 3066 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3067 sig_type->per_cu.v.quick
1fd400ff
TT
3068 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3069 struct dwarf2_per_cu_quick_data);
3070
52dc124a
DE
3071 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3072 *slot = sig_type;
1fd400ff 3073
b2bdb8cf 3074 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3075 }
3076
673bfd45 3077 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3078}
3079
927aa2e7
JK
3080/* Create the signatured type hash table from .debug_names. */
3081
3082static void
3083create_signatured_type_table_from_debug_names
ed2dc618 3084 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3085 const mapped_debug_names &map,
3086 struct dwarf2_section_info *section,
3087 struct dwarf2_section_info *abbrev_section)
3088{
ed2dc618
SM
3089 struct objfile *objfile = dwarf2_per_objfile->objfile;
3090
927aa2e7
JK
3091 dwarf2_read_section (objfile, section);
3092 dwarf2_read_section (objfile, abbrev_section);
3093
b2bdb8cf
SM
3094 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3095 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3096
3097 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3098
3099 for (uint32_t i = 0; i < map.tu_count; ++i)
3100 {
3101 struct signatured_type *sig_type;
927aa2e7 3102 void **slot;
927aa2e7
JK
3103
3104 sect_offset sect_off
3105 = (sect_offset) (extract_unsigned_integer
3106 (map.tu_table_reordered + i * map.offset_size,
3107 map.offset_size,
3108 map.dwarf5_byte_order));
3109
3110 comp_unit_head cu_header;
ed2dc618
SM
3111 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3112 abbrev_section,
927aa2e7
JK
3113 section->buffer + to_underlying (sect_off),
3114 rcuh_kind::TYPE);
3115
3116 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3117 struct signatured_type);
3118 sig_type->signature = cu_header.signature;
3119 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3120 sig_type->per_cu.is_debug_types = 1;
3121 sig_type->per_cu.section = section;
3122 sig_type->per_cu.sect_off = sect_off;
e3b94546 3123 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3124 sig_type->per_cu.v.quick
3125 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3126 struct dwarf2_per_cu_quick_data);
3127
3128 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3129 *slot = sig_type;
3130
b2bdb8cf 3131 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3132 }
3133
3134 dwarf2_per_objfile->signatured_types = sig_types_hash;
3135}
3136
9291a0cd
TT
3137/* Read the address map data from the mapped index, and use it to
3138 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3139
9291a0cd 3140static void
ed2dc618
SM
3141create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3142 struct mapped_index *index)
9291a0cd 3143{
ed2dc618 3144 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3145 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3146 const gdb_byte *iter, *end;
9291a0cd 3147 struct addrmap *mutable_map;
9291a0cd
TT
3148 CORE_ADDR baseaddr;
3149
8268c778
PA
3150 auto_obstack temp_obstack;
3151
9291a0cd
TT
3152 mutable_map = addrmap_create_mutable (&temp_obstack);
3153
f00a2de2
PA
3154 iter = index->address_table.data ();
3155 end = iter + index->address_table.size ();
9291a0cd
TT
3156
3157 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3158
3159 while (iter < end)
3160 {
3161 ULONGEST hi, lo, cu_index;
3162 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3163 iter += 8;
3164 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3165 iter += 8;
3166 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3167 iter += 4;
f652bce2 3168
24a55014 3169 if (lo > hi)
f652bce2 3170 {
b98664d3 3171 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3172 hex_string (lo), hex_string (hi));
24a55014 3173 continue;
f652bce2 3174 }
24a55014 3175
b76e467d 3176 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3177 {
b98664d3 3178 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3179 (unsigned) cu_index);
24a55014 3180 continue;
f652bce2 3181 }
24a55014 3182
79748972
TT
3183 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3184 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3185 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3186 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3187 }
3188
d320c2b5 3189 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3190 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3191}
3192
927aa2e7
JK
3193/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3194 populate the objfile's psymtabs_addrmap. */
3195
3196static void
ed2dc618 3197create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3198 struct dwarf2_section_info *section)
3199{
ed2dc618 3200 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3201 bfd *abfd = objfile->obfd;
3202 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3203 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3204 SECT_OFF_TEXT (objfile));
3205
3206 auto_obstack temp_obstack;
3207 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3208
3209 std::unordered_map<sect_offset,
3210 dwarf2_per_cu_data *,
3211 gdb::hash_enum<sect_offset>>
3212 debug_info_offset_to_per_cu;
b76e467d 3213 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3214 {
927aa2e7
JK
3215 const auto insertpair
3216 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3217 if (!insertpair.second)
3218 {
3219 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3220 "debug_info_offset %s, ignoring .debug_aranges."),
3221 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3222 return;
3223 }
3224 }
3225
3226 dwarf2_read_section (objfile, section);
3227
3228 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3229
3230 const gdb_byte *addr = section->buffer;
3231
3232 while (addr < section->buffer + section->size)
3233 {
3234 const gdb_byte *const entry_addr = addr;
3235 unsigned int bytes_read;
3236
3237 const LONGEST entry_length = read_initial_length (abfd, addr,
3238 &bytes_read);
3239 addr += bytes_read;
3240
3241 const gdb_byte *const entry_end = addr + entry_length;
3242 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3243 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3244 if (addr + entry_length > section->buffer + section->size)
3245 {
47e3f474 3246 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3247 "length %s exceeds section length %s, "
3248 "ignoring .debug_aranges."),
47e3f474
TV
3249 objfile_name (objfile),
3250 plongest (entry_addr - section->buffer),
927aa2e7
JK
3251 plongest (bytes_read + entry_length),
3252 pulongest (section->size));
3253 return;
3254 }
3255
3256 /* The version number. */
3257 const uint16_t version = read_2_bytes (abfd, addr);
3258 addr += 2;
3259 if (version != 2)
3260 {
47e3f474 3261 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3262 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3263 objfile_name (objfile),
3264 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3265 return;
3266 }
3267
3268 const uint64_t debug_info_offset
3269 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3270 addr += offset_size;
3271 const auto per_cu_it
3272 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3273 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3274 {
47e3f474 3275 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3276 "debug_info_offset %s does not exists, "
3277 "ignoring .debug_aranges."),
47e3f474
TV
3278 objfile_name (objfile),
3279 plongest (entry_addr - section->buffer),
927aa2e7
JK
3280 pulongest (debug_info_offset));
3281 return;
3282 }
3283 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3284
3285 const uint8_t address_size = *addr++;
3286 if (address_size < 1 || address_size > 8)
3287 {
47e3f474 3288 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3289 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3290 objfile_name (objfile),
3291 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3292 return;
3293 }
3294
3295 const uint8_t segment_selector_size = *addr++;
3296 if (segment_selector_size != 0)
3297 {
47e3f474 3298 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3299 "segment_selector_size %u is not supported, "
3300 "ignoring .debug_aranges."),
47e3f474
TV
3301 objfile_name (objfile),
3302 plongest (entry_addr - section->buffer),
927aa2e7
JK
3303 segment_selector_size);
3304 return;
3305 }
3306
3307 /* Must pad to an alignment boundary that is twice the address
3308 size. It is undocumented by the DWARF standard but GCC does
3309 use it. */
3310 for (size_t padding = ((-(addr - section->buffer))
3311 & (2 * address_size - 1));
3312 padding > 0; padding--)
3313 if (*addr++ != 0)
3314 {
47e3f474 3315 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3316 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3317 objfile_name (objfile),
3318 plongest (entry_addr - section->buffer));
927aa2e7
JK
3319 return;
3320 }
3321
3322 for (;;)
3323 {
3324 if (addr + 2 * address_size > entry_end)
3325 {
47e3f474 3326 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3327 "address list is not properly terminated, "
3328 "ignoring .debug_aranges."),
47e3f474
TV
3329 objfile_name (objfile),
3330 plongest (entry_addr - section->buffer));
927aa2e7
JK
3331 return;
3332 }
3333 ULONGEST start = extract_unsigned_integer (addr, address_size,
3334 dwarf5_byte_order);
3335 addr += address_size;
3336 ULONGEST length = extract_unsigned_integer (addr, address_size,
3337 dwarf5_byte_order);
3338 addr += address_size;
3339 if (start == 0 && length == 0)
3340 break;
3341 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3342 {
3343 /* Symbol was eliminated due to a COMDAT group. */
3344 continue;
3345 }
3346 ULONGEST end = start + length;
79748972
TT
3347 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3348 - baseaddr);
3349 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3350 - baseaddr);
927aa2e7
JK
3351 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3352 }
3353 }
3354
d320c2b5 3355 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3356 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3357}
3358
9291a0cd
TT
3359/* Find a slot in the mapped index INDEX for the object named NAME.
3360 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3361 constant pool and return true. If NAME cannot be found, return
3362 false. */
2fdf6df6 3363
109483d9 3364static bool
9291a0cd
TT
3365find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3366 offset_type **vec_out)
3367{
0cf03b49 3368 offset_type hash;
9291a0cd 3369 offset_type slot, step;
559a7a62 3370 int (*cmp) (const char *, const char *);
9291a0cd 3371
791afaa2 3372 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3373 if (current_language->la_language == language_cplus
45280282
IB
3374 || current_language->la_language == language_fortran
3375 || current_language->la_language == language_d)
0cf03b49
JK
3376 {
3377 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3378 not contain any. */
a8719064 3379
72998fb3 3380 if (strchr (name, '(') != NULL)
0cf03b49 3381 {
109483d9 3382 without_params = cp_remove_params (name);
0cf03b49 3383
72998fb3 3384 if (without_params != NULL)
791afaa2 3385 name = without_params.get ();
0cf03b49
JK
3386 }
3387 }
3388
559a7a62 3389 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3390 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3391 simulate our NAME being searched is also lowercased. */
3392 hash = mapped_index_string_hash ((index->version == 4
3393 && case_sensitivity == case_sensitive_off
3394 ? 5 : index->version),
3395 name);
3396
f00a2de2
PA
3397 slot = hash & (index->symbol_table.size () - 1);
3398 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3399 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3400
3401 for (;;)
3402 {
9291a0cd 3403 const char *str;
f00a2de2
PA
3404
3405 const auto &bucket = index->symbol_table[slot];
3406 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3407 return false;
9291a0cd 3408
f00a2de2 3409 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3410 if (!cmp (name, str))
9291a0cd
TT
3411 {
3412 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3413 + MAYBE_SWAP (bucket.vec));
109483d9 3414 return true;
9291a0cd
TT
3415 }
3416
f00a2de2 3417 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3418 }
3419}
3420
4485a1c1
SM
3421/* A helper function that reads the .gdb_index from BUFFER and fills
3422 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3423 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3424 ok to use deprecated sections.
3425
3426 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3427 out parameters that are filled in with information about the CU and
3428 TU lists in the section.
3429
4485a1c1 3430 Returns true if all went well, false otherwise. */
2fdf6df6 3431
d33bc52e 3432static bool
4485a1c1
SM
3433read_gdb_index_from_buffer (struct objfile *objfile,
3434 const char *filename,
3435 bool deprecated_ok,
3436 gdb::array_view<const gdb_byte> buffer,
3437 struct mapped_index *map,
3438 const gdb_byte **cu_list,
3439 offset_type *cu_list_elements,
3440 const gdb_byte **types_list,
3441 offset_type *types_list_elements)
3442{
3443 const gdb_byte *addr = &buffer[0];
82430852 3444
9291a0cd 3445 /* Version check. */
4485a1c1 3446 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3447 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3448 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3449 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3450 indices. */
831adc1f 3451 if (version < 4)
481860b3
GB
3452 {
3453 static int warning_printed = 0;
3454 if (!warning_printed)
3455 {
3456 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3457 filename);
481860b3
GB
3458 warning_printed = 1;
3459 }
3460 return 0;
3461 }
3462 /* Index version 4 uses a different hash function than index version
3463 5 and later.
3464
3465 Versions earlier than 6 did not emit psymbols for inlined
3466 functions. Using these files will cause GDB not to be able to
3467 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3468 indices unless the user has done
3469 "set use-deprecated-index-sections on". */
2ec9a5e0 3470 if (version < 6 && !deprecated_ok)
481860b3
GB
3471 {
3472 static int warning_printed = 0;
3473 if (!warning_printed)
3474 {
e615022a
DE
3475 warning (_("\
3476Skipping deprecated .gdb_index section in %s.\n\
3477Do \"set use-deprecated-index-sections on\" before the file is read\n\
3478to use the section anyway."),
2ec9a5e0 3479 filename);
481860b3
GB
3480 warning_printed = 1;
3481 }
3482 return 0;
3483 }
796a7ff8 3484 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3485 of the TU (for symbols coming from TUs),
3486 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3487 Plus gold-generated indices can have duplicate entries for global symbols,
3488 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3489 These are just performance bugs, and we can't distinguish gdb-generated
3490 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3491
481860b3 3492 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3493 longer backward compatible. */
796a7ff8 3494 if (version > 8)
594e8718 3495 return 0;
9291a0cd 3496
559a7a62 3497 map->version = version;
9291a0cd 3498
4485a1c1 3499 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3500
4485a1c1 3501 int i = 0;
2ec9a5e0
TT
3502 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3503 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3504 / 8);
1fd400ff
TT
3505 ++i;
3506
2ec9a5e0
TT
3507 *types_list = addr + MAYBE_SWAP (metadata[i]);
3508 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3509 - MAYBE_SWAP (metadata[i]))
3510 / 8);
987d643c 3511 ++i;
1fd400ff 3512
f00a2de2
PA
3513 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3514 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3515 map->address_table
3516 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3517 ++i;
3518
f00a2de2
PA
3519 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3520 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3521 map->symbol_table
3522 = gdb::array_view<mapped_index::symbol_table_slot>
3523 ((mapped_index::symbol_table_slot *) symbol_table,
3524 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3525
f00a2de2 3526 ++i;
f9d83a0b 3527 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3528
2ec9a5e0
TT
3529 return 1;
3530}
3531
4485a1c1
SM
3532/* Callback types for dwarf2_read_gdb_index. */
3533
3534typedef gdb::function_view
3535 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3536 get_gdb_index_contents_ftype;
3537typedef gdb::function_view
3538 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3539 get_gdb_index_contents_dwz_ftype;
3540
927aa2e7 3541/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3542 elements of all the CUs and return 1. Otherwise, return 0. */
3543
3544static int
4485a1c1
SM
3545dwarf2_read_gdb_index
3546 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3547 get_gdb_index_contents_ftype get_gdb_index_contents,
3548 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3549{
2ec9a5e0
TT
3550 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3551 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3552 struct dwz_file *dwz;
12359b5e 3553 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3554
4485a1c1
SM
3555 gdb::array_view<const gdb_byte> main_index_contents
3556 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3557
3558 if (main_index_contents.empty ())
3559 return 0;
3560
3063847f 3561 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3562 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3563 use_deprecated_index_sections,
3564 main_index_contents, map.get (), &cu_list,
3565 &cu_list_elements, &types_list,
3566 &types_list_elements))
2ec9a5e0
TT
3567 return 0;
3568
0fefef59 3569 /* Don't use the index if it's empty. */
3063847f 3570 if (map->symbol_table.empty ())
0fefef59
DE
3571 return 0;
3572
2ec9a5e0
TT
3573 /* If there is a .dwz file, read it so we can get its CU list as
3574 well. */
ed2dc618 3575 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3576 if (dwz != NULL)
2ec9a5e0 3577 {
2ec9a5e0
TT
3578 struct mapped_index dwz_map;
3579 const gdb_byte *dwz_types_ignore;
3580 offset_type dwz_types_elements_ignore;
3581
4485a1c1
SM
3582 gdb::array_view<const gdb_byte> dwz_index_content
3583 = get_gdb_index_contents_dwz (objfile, dwz);
3584
3585 if (dwz_index_content.empty ())
3586 return 0;
3587
3588 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3589 bfd_get_filename (dwz->dwz_bfd.get ()),
3590 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3591 &dwz_list, &dwz_list_elements,
3592 &dwz_types_ignore,
3593 &dwz_types_elements_ignore))
2ec9a5e0
TT
3594 {
3595 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3596 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3597 return 0;
3598 }
3599 }
3600
12359b5e
SM
3601 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3602 dwz_list, dwz_list_elements);
1fd400ff 3603
8b70b953
TT
3604 if (types_list_elements)
3605 {
8b70b953
TT
3606 /* We can only handle a single .debug_types when we have an
3607 index. */
fd5866f6 3608 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3609 return 0;
3610
fd5866f6 3611 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3612
12359b5e
SM
3613 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3614 types_list, types_list_elements);
8b70b953 3615 }
9291a0cd 3616
3063847f 3617 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3618
3063847f 3619 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3620 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3621 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3622 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3623
3624 return 1;
3625}
3626
dee91e82 3627/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3628
dee91e82
DE
3629static void
3630dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3631 const gdb_byte *info_ptr,
dee91e82
DE
3632 struct die_info *comp_unit_die,
3633 int has_children,
3634 void *data)
9291a0cd 3635{
dee91e82 3636 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3637 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3638 struct dwarf2_per_objfile *dwarf2_per_objfile
3639 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3640 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3641 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3642 struct attribute *attr;
dee91e82 3643 int i;
7b9f3c50
DE
3644 void **slot;
3645 struct quick_file_names *qfn;
9291a0cd 3646
0186c6a7
DE
3647 gdb_assert (! this_cu->is_debug_types);
3648
07261596
TT
3649 /* Our callers never want to match partial units -- instead they
3650 will match the enclosing full CU. */
3651 if (comp_unit_die->tag == DW_TAG_partial_unit)
3652 {
3653 this_cu->v.quick->no_file_data = 1;
3654 return;
3655 }
3656
0186c6a7 3657 lh_cu = this_cu;
7b9f3c50 3658 slot = NULL;
dee91e82 3659
fff8551c 3660 line_header_up lh;
9c541725 3661 sect_offset line_offset {};
fff8551c 3662
dee91e82 3663 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3664 if (attr)
3665 {
7b9f3c50
DE
3666 struct quick_file_names find_entry;
3667
9c541725 3668 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3669
3670 /* We may have already read in this line header (TU line header sharing).
3671 If we have we're done. */
094b34ac 3672 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3673 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3674 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3675 &find_entry, INSERT);
3676 if (*slot != NULL)
3677 {
9a3c8263 3678 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3679 return;
7b9f3c50
DE
3680 }
3681
3019eac3 3682 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3683 }
3684 if (lh == NULL)
3685 {
094b34ac 3686 lh_cu->v.quick->no_file_data = 1;
dee91e82 3687 return;
9291a0cd
TT
3688 }
3689
8d749320 3690 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3691 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3692 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3693 gdb_assert (slot != NULL);
3694 *slot = qfn;
9291a0cd 3695
d721ba37 3696 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3697
aa391654
TT
3698 int offset = 0;
3699 if (strcmp (fnd.name, "<unknown>") != 0)
3700 ++offset;
3701
3702 qfn->num_file_names = offset + lh->file_names.size ();
8d749320 3703 qfn->file_names =
aa391654
TT
3704 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3705 if (offset != 0)
3706 qfn->file_names[0] = xstrdup (fnd.name);
fff8551c 3707 for (i = 0; i < lh->file_names.size (); ++i)
aa391654 3708 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3709 qfn->real_names = NULL;
9291a0cd 3710
094b34ac 3711 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3712}
3713
3714/* A helper for the "quick" functions which attempts to read the line
3715 table for THIS_CU. */
3716
3717static struct quick_file_names *
e4a48d9d 3718dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3719{
0186c6a7
DE
3720 /* This should never be called for TUs. */
3721 gdb_assert (! this_cu->is_debug_types);
3722 /* Nor type unit groups. */
3723 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3724
dee91e82
DE
3725 if (this_cu->v.quick->file_names != NULL)
3726 return this_cu->v.quick->file_names;
3727 /* If we know there is no line data, no point in looking again. */
3728 if (this_cu->v.quick->no_file_data)
3729 return NULL;
3730
0186c6a7 3731 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3732
3733 if (this_cu->v.quick->no_file_data)
3734 return NULL;
3735 return this_cu->v.quick->file_names;
9291a0cd
TT
3736}
3737
3738/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3739 real path for a given file name from the line table. */
2fdf6df6 3740
9291a0cd 3741static const char *
7b9f3c50
DE
3742dw2_get_real_path (struct objfile *objfile,
3743 struct quick_file_names *qfn, int index)
9291a0cd 3744{
7b9f3c50
DE
3745 if (qfn->real_names == NULL)
3746 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3747 qfn->num_file_names, const char *);
9291a0cd 3748
7b9f3c50 3749 if (qfn->real_names[index] == NULL)
14278e1f 3750 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3751
7b9f3c50 3752 return qfn->real_names[index];
9291a0cd
TT
3753}
3754
3755static struct symtab *
3756dw2_find_last_source_symtab (struct objfile *objfile)
3757{
ed2dc618
SM
3758 struct dwarf2_per_objfile *dwarf2_per_objfile
3759 = get_dwarf2_per_objfile (objfile);
b76e467d 3760 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3761 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3762
43f3e411
DE
3763 if (cust == NULL)
3764 return NULL;
ed2dc618 3765
43f3e411 3766 return compunit_primary_filetab (cust);
9291a0cd
TT
3767}
3768
7b9f3c50
DE
3769/* Traversal function for dw2_forget_cached_source_info. */
3770
3771static int
3772dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3773{
7b9f3c50 3774 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3775
7b9f3c50 3776 if (file_data->real_names)
9291a0cd 3777 {
7b9f3c50 3778 int i;
9291a0cd 3779
7b9f3c50 3780 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3781 {
7b9f3c50
DE
3782 xfree ((void*) file_data->real_names[i]);
3783 file_data->real_names[i] = NULL;
9291a0cd
TT
3784 }
3785 }
7b9f3c50
DE
3786
3787 return 1;
3788}
3789
3790static void
3791dw2_forget_cached_source_info (struct objfile *objfile)
3792{
ed2dc618
SM
3793 struct dwarf2_per_objfile *dwarf2_per_objfile
3794 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3795
3796 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3797 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3798}
3799
f8eba3c6
TT
3800/* Helper function for dw2_map_symtabs_matching_filename that expands
3801 the symtabs and calls the iterator. */
3802
3803static int
3804dw2_map_expand_apply (struct objfile *objfile,
3805 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3806 const char *name, const char *real_path,
14bc53a8 3807 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3808{
43f3e411 3809 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3810
3811 /* Don't visit already-expanded CUs. */
43f3e411 3812 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3813 return 0;
3814
3815 /* This may expand more than one symtab, and we want to iterate over
3816 all of them. */
58f0c718 3817 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3818
14bc53a8
PA
3819 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3820 last_made, callback);
f8eba3c6
TT
3821}
3822
3823/* Implementation of the map_symtabs_matching_filename method. */
3824
14bc53a8
PA
3825static bool
3826dw2_map_symtabs_matching_filename
3827 (struct objfile *objfile, const char *name, const char *real_path,
3828 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3829{
c011a4f4 3830 const char *name_basename = lbasename (name);
ed2dc618
SM
3831 struct dwarf2_per_objfile *dwarf2_per_objfile
3832 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3833
848e3e78
DE
3834 /* The rule is CUs specify all the files, including those used by
3835 any TU, so there's no need to scan TUs here. */
f4dc4d17 3836
b76e467d 3837 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3838 {
3d7bb9d9 3839 /* We only need to look at symtabs not already expanded. */
43f3e411 3840 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3841 continue;
3842
b76e467d 3843 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3844 if (file_data == NULL)
9291a0cd
TT
3845 continue;
3846
b76e467d 3847 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3848 {
7b9f3c50 3849 const char *this_name = file_data->file_names[j];
da235a7c 3850 const char *this_real_name;
9291a0cd 3851
af529f8f 3852 if (compare_filenames_for_search (this_name, name))
9291a0cd 3853 {
f5b95b50 3854 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3855 callback))
3856 return true;
288e77a7 3857 continue;
4aac40c8 3858 }
9291a0cd 3859
c011a4f4
DE
3860 /* Before we invoke realpath, which can get expensive when many
3861 files are involved, do a quick comparison of the basenames. */
3862 if (! basenames_may_differ
3863 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3864 continue;
3865
da235a7c
JK
3866 this_real_name = dw2_get_real_path (objfile, file_data, j);
3867 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3868 {
da235a7c 3869 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3870 callback))
3871 return true;
288e77a7 3872 continue;
da235a7c 3873 }
9291a0cd 3874
da235a7c
JK
3875 if (real_path != NULL)
3876 {
af529f8f
JK
3877 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3878 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3879 if (this_real_name != NULL
af529f8f 3880 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3881 {
f5b95b50 3882 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3883 callback))
3884 return true;
288e77a7 3885 continue;
9291a0cd
TT
3886 }
3887 }
3888 }
3889 }
3890
14bc53a8 3891 return false;
9291a0cd
TT
3892}
3893
da51c347
DE
3894/* Struct used to manage iterating over all CUs looking for a symbol. */
3895
3896struct dw2_symtab_iterator
9291a0cd 3897{
ed2dc618
SM
3898 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3899 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3900 /* If set, only look for symbols that match that block. Valid values are
3901 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3902 gdb::optional<block_enum> block_index;
da51c347
DE
3903 /* The kind of symbol we're looking for. */
3904 domain_enum domain;
3905 /* The list of CUs from the index entry of the symbol,
3906 or NULL if not found. */
3907 offset_type *vec;
3908 /* The next element in VEC to look at. */
3909 int next;
3910 /* The number of elements in VEC, or zero if there is no match. */
3911 int length;
8943b874
DE
3912 /* Have we seen a global version of the symbol?
3913 If so we can ignore all further global instances.
3914 This is to work around gold/15646, inefficient gold-generated
3915 indices. */
3916 int global_seen;
da51c347 3917};
9291a0cd 3918
2b79f376 3919/* Initialize the index symtab iterator ITER. */
2fdf6df6 3920
9291a0cd 3921static void
da51c347 3922dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3923 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3924 gdb::optional<block_enum> block_index,
da51c347
DE
3925 domain_enum domain,
3926 const char *name)
3927{
ed2dc618 3928 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3929 iter->block_index = block_index;
3930 iter->domain = domain;
3931 iter->next = 0;
8943b874 3932 iter->global_seen = 0;
da51c347 3933
3063847f 3934 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3935
3936 /* index is NULL if OBJF_READNOW. */
3937 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3938 iter->length = MAYBE_SWAP (*iter->vec);
3939 else
3940 {
3941 iter->vec = NULL;
3942 iter->length = 0;
3943 }
3944}
3945
3946/* Return the next matching CU or NULL if there are no more. */
3947
3948static struct dwarf2_per_cu_data *
3949dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3950{
ed2dc618
SM
3951 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3952
da51c347
DE
3953 for ( ; iter->next < iter->length; ++iter->next)
3954 {
3955 offset_type cu_index_and_attrs =
3956 MAYBE_SWAP (iter->vec[iter->next + 1]);
3957 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3958 gdb_index_symbol_kind symbol_kind =
3959 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3960 /* Only check the symbol attributes if they're present.
3961 Indices prior to version 7 don't record them,
3962 and indices >= 7 may elide them for certain symbols
3963 (gold does this). */
3964 int attrs_valid =
ed2dc618 3965 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3966 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3967
3190f0c6 3968 /* Don't crash on bad data. */
b76e467d 3969 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3970 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3971 {
b98664d3 3972 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3973 " [in module %s]"),
3974 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3975 continue;
3976 }
3977
ff4c9fec 3978 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3979
da51c347 3980 /* Skip if already read in. */
43f3e411 3981 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3982 continue;
3983
8943b874
DE
3984 /* Check static vs global. */
3985 if (attrs_valid)
3986 {
2b79f376
SM
3987 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3988
3989 if (iter->block_index.has_value ())
3990 {
3991 bool want_static = *iter->block_index == STATIC_BLOCK;
3992
3993 if (is_static != want_static)
3994 continue;
3995 }
3996
8943b874
DE
3997 /* Work around gold/15646. */
3998 if (!is_static && iter->global_seen)
3999 continue;
4000 if (!is_static)
4001 iter->global_seen = 1;
4002 }
da51c347
DE
4003
4004 /* Only check the symbol's kind if it has one. */
4005 if (attrs_valid)
4006 {
4007 switch (iter->domain)
4008 {
4009 case VAR_DOMAIN:
4010 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4011 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4012 /* Some types are also in VAR_DOMAIN. */
4013 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4014 continue;
4015 break;
4016 case STRUCT_DOMAIN:
4017 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4018 continue;
4019 break;
4020 case LABEL_DOMAIN:
4021 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4022 continue;
4023 break;
4024 default:
4025 break;
4026 }
4027 }
4028
4029 ++iter->next;
4030 return per_cu;
4031 }
4032
4033 return NULL;
4034}
4035
43f3e411 4036static struct compunit_symtab *
c7f839cb 4037dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4038 const char *name, domain_enum domain)
9291a0cd 4039{
43f3e411 4040 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4041 struct dwarf2_per_objfile *dwarf2_per_objfile
4042 = get_dwarf2_per_objfile (objfile);
9291a0cd 4043
b5ec771e
PA
4044 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4045
ed2dc618
SM
4046 struct dw2_symtab_iterator iter;
4047 struct dwarf2_per_cu_data *per_cu;
da51c347 4048
2b79f376 4049 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4050
ed2dc618
SM
4051 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4052 {
4053 struct symbol *sym, *with_opaque = NULL;
58f0c718 4054 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4055 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4056 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4057
ed2dc618
SM
4058 sym = block_find_symbol (block, name, domain,
4059 block_find_non_opaque_type_preferred,
4060 &with_opaque);
b2e2f908 4061
ed2dc618
SM
4062 /* Some caution must be observed with overloaded functions
4063 and methods, since the index will not contain any overload
4064 information (but NAME might contain it). */
da51c347 4065
ed2dc618
SM
4066 if (sym != NULL
4067 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4068 return stab;
4069 if (with_opaque != NULL
4070 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4071 stab_best = stab;
da51c347 4072
ed2dc618 4073 /* Keep looking through other CUs. */
9291a0cd 4074 }
9291a0cd 4075
da51c347 4076 return stab_best;
9291a0cd
TT
4077}
4078
4079static void
4080dw2_print_stats (struct objfile *objfile)
4081{
ed2dc618
SM
4082 struct dwarf2_per_objfile *dwarf2_per_objfile
4083 = get_dwarf2_per_objfile (objfile);
b76e467d 4084 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4085 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4086 int count = 0;
9291a0cd 4087
ed2dc618 4088 for (int i = 0; i < total; ++i)
9291a0cd 4089 {
ff4c9fec 4090 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4091
43f3e411 4092 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4093 ++count;
4094 }
e4a48d9d 4095 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4096 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4097}
4098
779bd270
DE
4099/* This dumps minimal information about the index.
4100 It is called via "mt print objfiles".
4101 One use is to verify .gdb_index has been loaded by the
4102 gdb.dwarf2/gdb-index.exp testcase. */
4103
9291a0cd
TT
4104static void
4105dw2_dump (struct objfile *objfile)
4106{
ed2dc618
SM
4107 struct dwarf2_per_objfile *dwarf2_per_objfile
4108 = get_dwarf2_per_objfile (objfile);
4109
779bd270
DE
4110 gdb_assert (dwarf2_per_objfile->using_index);
4111 printf_filtered (".gdb_index:");
4112 if (dwarf2_per_objfile->index_table != NULL)
4113 {
4114 printf_filtered (" version %d\n",
4115 dwarf2_per_objfile->index_table->version);
4116 }
4117 else
4118 printf_filtered (" faked for \"readnow\"\n");
4119 printf_filtered ("\n");
9291a0cd
TT
4120}
4121
9291a0cd
TT
4122static void
4123dw2_expand_symtabs_for_function (struct objfile *objfile,
4124 const char *func_name)
4125{
ed2dc618
SM
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
da51c347 4128
ed2dc618
SM
4129 struct dw2_symtab_iterator iter;
4130 struct dwarf2_per_cu_data *per_cu;
da51c347 4131
2b79f376 4132 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4133
ed2dc618 4134 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4135 dw2_instantiate_symtab (per_cu, false);
da51c347 4136
9291a0cd
TT
4137}
4138
4139static void
4140dw2_expand_all_symtabs (struct objfile *objfile)
4141{
ed2dc618
SM
4142 struct dwarf2_per_objfile *dwarf2_per_objfile
4143 = get_dwarf2_per_objfile (objfile);
b76e467d 4144 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4145 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4146
ed2dc618 4147 for (int i = 0; i < total_units; ++i)
9291a0cd 4148 {
ff4c9fec 4149 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4150
58f0c718
TT
4151 /* We don't want to directly expand a partial CU, because if we
4152 read it with the wrong language, then assertion failures can
4153 be triggered later on. See PR symtab/23010. So, tell
4154 dw2_instantiate_symtab to skip partial CUs -- any important
4155 partial CU will be read via DW_TAG_imported_unit anyway. */
4156 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4157 }
4158}
4159
4160static void
652a8996
JK
4161dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4162 const char *fullname)
9291a0cd 4163{
ed2dc618
SM
4164 struct dwarf2_per_objfile *dwarf2_per_objfile
4165 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4166
4167 /* We don't need to consider type units here.
4168 This is only called for examining code, e.g. expand_line_sal.
4169 There can be an order of magnitude (or more) more type units
4170 than comp units, and we avoid them if we can. */
4171
b76e467d 4172 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4173 {
3d7bb9d9 4174 /* We only need to look at symtabs not already expanded. */
43f3e411 4175 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4176 continue;
4177
b76e467d 4178 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4179 if (file_data == NULL)
9291a0cd
TT
4180 continue;
4181
b76e467d 4182 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4183 {
652a8996
JK
4184 const char *this_fullname = file_data->file_names[j];
4185
4186 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4187 {
58f0c718 4188 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4189 break;
4190 }
4191 }
4192 }
4193}
4194
9291a0cd 4195static void
199b4314
TT
4196dw2_map_matching_symbols
4197 (struct objfile *objfile,
b054970d 4198 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4199 int global,
4200 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4201 symbol_compare_ftype *ordered_compare)
9291a0cd 4202{
40658b94 4203 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4204 current language is Ada for a non-Ada objfile using GNU index. As Ada
4205 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4206}
4207
e1ef7d7a
PA
4208/* Starting from a search name, return the string that finds the upper
4209 bound of all strings that start with SEARCH_NAME in a sorted name
4210 list. Returns the empty string to indicate that the upper bound is
4211 the end of the list. */
4212
4213static std::string
4214make_sort_after_prefix_name (const char *search_name)
4215{
4216 /* When looking to complete "func", we find the upper bound of all
4217 symbols that start with "func" by looking for where we'd insert
4218 the closest string that would follow "func" in lexicographical
4219 order. Usually, that's "func"-with-last-character-incremented,
4220 i.e. "fund". Mind non-ASCII characters, though. Usually those
4221 will be UTF-8 multi-byte sequences, but we can't be certain.
4222 Especially mind the 0xff character, which is a valid character in
4223 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4224 rule out compilers allowing it in identifiers. Note that
4225 conveniently, strcmp/strcasecmp are specified to compare
4226 characters interpreted as unsigned char. So what we do is treat
4227 the whole string as a base 256 number composed of a sequence of
4228 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4229 to 0, and carries 1 to the following more-significant position.
4230 If the very first character in SEARCH_NAME ends up incremented
4231 and carries/overflows, then the upper bound is the end of the
4232 list. The string after the empty string is also the empty
4233 string.
4234
4235 Some examples of this operation:
4236
4237 SEARCH_NAME => "+1" RESULT
4238
4239 "abc" => "abd"
4240 "ab\xff" => "ac"
4241 "\xff" "a" "\xff" => "\xff" "b"
4242 "\xff" => ""
4243 "\xff\xff" => ""
4244 "" => ""
4245
4246 Then, with these symbols for example:
4247
4248 func
4249 func1
4250 fund
4251
4252 completing "func" looks for symbols between "func" and
4253 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4254 which finds "func" and "func1", but not "fund".
4255
4256 And with:
4257
4258 funcÿ (Latin1 'ÿ' [0xff])
4259 funcÿ1
4260 fund
4261
4262 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4263 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4264
4265 And with:
4266
4267 ÿÿ (Latin1 'ÿ' [0xff])
4268 ÿÿ1
4269
4270 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4271 the end of the list.
4272 */
4273 std::string after = search_name;
4274 while (!after.empty () && (unsigned char) after.back () == 0xff)
4275 after.pop_back ();
4276 if (!after.empty ())
4277 after.back () = (unsigned char) after.back () + 1;
4278 return after;
4279}
4280
5c58de74 4281/* See declaration. */
61d96d7e 4282
5c58de74
PA
4283std::pair<std::vector<name_component>::const_iterator,
4284 std::vector<name_component>::const_iterator>
44ed8f3e 4285mapped_index_base::find_name_components_bounds
3b00ef10 4286 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4287{
5c58de74
PA
4288 auto *name_cmp
4289 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4290
3b00ef10
TT
4291 const char *lang_name
4292 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4293
3f563c84
PA
4294 /* Comparison function object for lower_bound that matches against a
4295 given symbol name. */
4296 auto lookup_compare_lower = [&] (const name_component &elem,
4297 const char *name)
4298 {
5c58de74 4299 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4300 const char *elem_name = elem_qualified + elem.name_offset;
4301 return name_cmp (elem_name, name) < 0;
4302 };
4303
4304 /* Comparison function object for upper_bound that matches against a
4305 given symbol name. */
4306 auto lookup_compare_upper = [&] (const char *name,
4307 const name_component &elem)
4308 {
5c58de74 4309 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4310 const char *elem_name = elem_qualified + elem.name_offset;
4311 return name_cmp (name, elem_name) < 0;
4312 };
4313
5c58de74
PA
4314 auto begin = this->name_components.begin ();
4315 auto end = this->name_components.end ();
3f563c84
PA
4316
4317 /* Find the lower bound. */
4318 auto lower = [&] ()
4319 {
3b00ef10 4320 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4321 return begin;
4322 else
3b00ef10 4323 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4324 } ();
4325
4326 /* Find the upper bound. */
4327 auto upper = [&] ()
4328 {
5c58de74 4329 if (lookup_name_without_params.completion_mode ())
3f563c84 4330 {
e1ef7d7a
PA
4331 /* In completion mode, we want UPPER to point past all
4332 symbols names that have the same prefix. I.e., with
4333 these symbols, and completing "func":
4334
4335 function << lower bound
4336 function1
4337 other_function << upper bound
4338
4339 We find the upper bound by looking for the insertion
4340 point of "func"-with-last-character-incremented,
4341 i.e. "fund". */
3b00ef10 4342 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4343 if (after.empty ())
3f563c84 4344 return end;
e6b2f5ef
PA
4345 return std::lower_bound (lower, end, after.c_str (),
4346 lookup_compare_lower);
3f563c84
PA
4347 }
4348 else
3b00ef10 4349 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4350 } ();
4351
5c58de74
PA
4352 return {lower, upper};
4353}
4354
4355/* See declaration. */
4356
4357void
44ed8f3e 4358mapped_index_base::build_name_components ()
5c58de74
PA
4359{
4360 if (!this->name_components.empty ())
4361 return;
4362
4363 this->name_components_casing = case_sensitivity;
4364 auto *name_cmp
4365 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4366
4367 /* The code below only knows how to break apart components of C++
4368 symbol names (and other languages that use '::' as
3b00ef10 4369 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4370 auto count = this->symbol_name_count ();
4371 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4372 {
44ed8f3e 4373 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4374 continue;
4375
4376 const char *name = this->symbol_name_at (idx);
4377
4378 /* Add each name component to the name component table. */
4379 unsigned int previous_len = 0;
3b00ef10
TT
4380
4381 if (strstr (name, "::") != nullptr)
4382 {
4383 for (unsigned int current_len = cp_find_first_component (name);
4384 name[current_len] != '\0';
4385 current_len += cp_find_first_component (name + current_len))
4386 {
4387 gdb_assert (name[current_len] == ':');
4388 this->name_components.push_back ({previous_len, idx});
4389 /* Skip the '::'. */
4390 current_len += 2;
4391 previous_len = current_len;
4392 }
4393 }
4394 else
5c58de74 4395 {
3b00ef10
TT
4396 /* Handle the Ada encoded (aka mangled) form here. */
4397 for (const char *iter = strstr (name, "__");
4398 iter != nullptr;
4399 iter = strstr (iter, "__"))
4400 {
4401 this->name_components.push_back ({previous_len, idx});
4402 iter += 2;
4403 previous_len = iter - name;
4404 }
5c58de74 4405 }
3b00ef10 4406
5c58de74
PA
4407 this->name_components.push_back ({previous_len, idx});
4408 }
4409
4410 /* Sort name_components elements by name. */
4411 auto name_comp_compare = [&] (const name_component &left,
4412 const name_component &right)
4413 {
4414 const char *left_qualified = this->symbol_name_at (left.idx);
4415 const char *right_qualified = this->symbol_name_at (right.idx);
4416
4417 const char *left_name = left_qualified + left.name_offset;
4418 const char *right_name = right_qualified + right.name_offset;
4419
4420 return name_cmp (left_name, right_name) < 0;
4421 };
4422
4423 std::sort (this->name_components.begin (),
4424 this->name_components.end (),
4425 name_comp_compare);
4426}
4427
4428/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4429 mapped_index_base instead of the containing objfile. This is split
4430 to a separate function in order to be able to unit test the
4431 name_components matching using a mock mapped_index_base. For each
5c58de74 4432 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4433 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4434
4435static void
4436dw2_expand_symtabs_matching_symbol
44ed8f3e 4437 (mapped_index_base &index,
5c58de74
PA
4438 const lookup_name_info &lookup_name_in,
4439 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4440 enum search_domain kind,
3b00ef10 4441 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4442{
4443 lookup_name_info lookup_name_without_params
4444 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4445
4446 /* Build the symbol name component sorted vector, if we haven't
4447 yet. */
4448 index.build_name_components ();
4449
3f563c84
PA
4450 /* The same symbol may appear more than once in the range though.
4451 E.g., if we're looking for symbols that complete "w", and we have
4452 a symbol named "w1::w2", we'll find the two name components for
4453 that same symbol in the range. To be sure we only call the
4454 callback once per symbol, we first collect the symbol name
4455 indexes that matched in a temporary vector and ignore
4456 duplicates. */
4457 std::vector<offset_type> matches;
3f563c84 4458
3b00ef10
TT
4459 struct name_and_matcher
4460 {
4461 symbol_name_matcher_ftype *matcher;
4462 const std::string &name;
4463
4464 bool operator== (const name_and_matcher &other) const
3f563c84 4465 {
3b00ef10
TT
4466 return matcher == other.matcher && name == other.name;
4467 }
4468 };
4469
4470 /* A vector holding all the different symbol name matchers, for all
4471 languages. */
4472 std::vector<name_and_matcher> matchers;
4473
4474 for (int i = 0; i < nr_languages; i++)
4475 {
4476 enum language lang_e = (enum language) i;
4477
4478 const language_defn *lang = language_def (lang_e);
4479 symbol_name_matcher_ftype *name_matcher
4480 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4481
3b00ef10
TT
4482 name_and_matcher key {
4483 name_matcher,
4484 lookup_name_without_params.language_lookup_name (lang_e)
4485 };
4486
4487 /* Don't insert the same comparison routine more than once.
4488 Note that we do this linear walk. This is not a problem in
4489 practice because the number of supported languages is
4490 low. */
4491 if (std::find (matchers.begin (), matchers.end (), key)
4492 != matchers.end ())
9291a0cd 4493 continue;
3b00ef10
TT
4494 matchers.push_back (std::move (key));
4495
4496 auto bounds
4497 = index.find_name_components_bounds (lookup_name_without_params,
4498 lang_e);
4499
4500 /* Now for each symbol name in range, check to see if we have a name
4501 match, and if so, call the MATCH_CALLBACK callback. */
4502
4503 for (; bounds.first != bounds.second; ++bounds.first)
4504 {
4505 const char *qualified = index.symbol_name_at (bounds.first->idx);
4506
4507 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4508 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4509 continue;
9291a0cd 4510
3b00ef10
TT
4511 matches.push_back (bounds.first->idx);
4512 }
3f563c84
PA
4513 }
4514
4515 std::sort (matches.begin (), matches.end ());
4516
4517 /* Finally call the callback, once per match. */
4518 ULONGEST prev = -1;
4519 for (offset_type idx : matches)
4520 {
4521 if (prev != idx)
4522 {
3b00ef10
TT
4523 if (!match_callback (idx))
4524 break;
3f563c84
PA
4525 prev = idx;
4526 }
4527 }
4528
4529 /* Above we use a type wider than idx's for 'prev', since 0 and
4530 (offset_type)-1 are both possible values. */
4531 static_assert (sizeof (prev) > sizeof (offset_type), "");
4532}
4533
c62446b1
PA
4534#if GDB_SELF_TEST
4535
4536namespace selftests { namespace dw2_expand_symtabs_matching {
4537
a3c5fafd
PA
4538/* A mock .gdb_index/.debug_names-like name index table, enough to
4539 exercise dw2_expand_symtabs_matching_symbol, which works with the
4540 mapped_index_base interface. Builds an index from the symbol list
4541 passed as parameter to the constructor. */
4542class mock_mapped_index : public mapped_index_base
c62446b1
PA
4543{
4544public:
a3c5fafd
PA
4545 mock_mapped_index (gdb::array_view<const char *> symbols)
4546 : m_symbol_table (symbols)
c62446b1
PA
4547 {}
4548
a3c5fafd 4549 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4550
a3c5fafd 4551 /* Return the number of names in the symbol table. */
632e107b 4552 size_t symbol_name_count () const override
c62446b1 4553 {
a3c5fafd 4554 return m_symbol_table.size ();
c62446b1
PA
4555 }
4556
a3c5fafd 4557 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4558 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4559 {
4560 return m_symbol_table[idx];
4561 }
c62446b1 4562
a3c5fafd
PA
4563private:
4564 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4565};
4566
4567/* Convenience function that converts a NULL pointer to a "<null>"
4568 string, to pass to print routines. */
4569
4570static const char *
4571string_or_null (const char *str)
4572{
4573 return str != NULL ? str : "<null>";
4574}
4575
4576/* Check if a lookup_name_info built from
4577 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4578 index. EXPECTED_LIST is the list of expected matches, in expected
4579 matching order. If no match expected, then an empty list is
4580 specified. Returns true on success. On failure prints a warning
4581 indicating the file:line that failed, and returns false. */
4582
4583static bool
4584check_match (const char *file, int line,
4585 mock_mapped_index &mock_index,
4586 const char *name, symbol_name_match_type match_type,
4587 bool completion_mode,
4588 std::initializer_list<const char *> expected_list)
4589{
4590 lookup_name_info lookup_name (name, match_type, completion_mode);
4591
4592 bool matched = true;
4593
4594 auto mismatch = [&] (const char *expected_str,
4595 const char *got)
4596 {
4597 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4598 "expected=\"%s\", got=\"%s\"\n"),
4599 file, line,
4600 (match_type == symbol_name_match_type::FULL
4601 ? "FULL" : "WILD"),
4602 name, string_or_null (expected_str), string_or_null (got));
4603 matched = false;
4604 };
4605
4606 auto expected_it = expected_list.begin ();
4607 auto expected_end = expected_list.end ();
4608
a3c5fafd 4609 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4610 NULL, ALL_DOMAIN,
4611 [&] (offset_type idx)
4612 {
a3c5fafd 4613 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4614 const char *expected_str
4615 = expected_it == expected_end ? NULL : *expected_it++;
4616
4617 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4618 mismatch (expected_str, matched_name);
3b00ef10 4619 return true;
c62446b1
PA
4620 });
4621
4622 const char *expected_str
4623 = expected_it == expected_end ? NULL : *expected_it++;
4624 if (expected_str != NULL)
4625 mismatch (expected_str, NULL);
4626
4627 return matched;
4628}
4629
4630/* The symbols added to the mock mapped_index for testing (in
4631 canonical form). */
4632static const char *test_symbols[] = {
4633 "function",
4634 "std::bar",
4635 "std::zfunction",
4636 "std::zfunction2",
4637 "w1::w2",
4638 "ns::foo<char*>",
4639 "ns::foo<int>",
4640 "ns::foo<long>",
a20714ff
PA
4641 "ns2::tmpl<int>::foo2",
4642 "(anonymous namespace)::A::B::C",
c62446b1 4643
e1ef7d7a
PA
4644 /* These are used to check that the increment-last-char in the
4645 matching algorithm for completion doesn't match "t1_fund" when
4646 completing "t1_func". */
4647 "t1_func",
4648 "t1_func1",
4649 "t1_fund",
4650 "t1_fund1",
4651
4652 /* A UTF-8 name with multi-byte sequences to make sure that
4653 cp-name-parser understands this as a single identifier ("função"
4654 is "function" in PT). */
4655 u8"u8função",
4656
4657 /* \377 (0xff) is Latin1 'ÿ'. */
4658 "yfunc\377",
4659
4660 /* \377 (0xff) is Latin1 'ÿ'. */
4661 "\377",
4662 "\377\377123",
4663
c62446b1
PA
4664 /* A name with all sorts of complications. Starts with "z" to make
4665 it easier for the completion tests below. */
4666#define Z_SYM_NAME \
4667 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4668 "::tuple<(anonymous namespace)::ui*, " \
4669 "std::default_delete<(anonymous namespace)::ui>, void>"
4670
4671 Z_SYM_NAME
4672};
4673
a3c5fafd
PA
4674/* Returns true if the mapped_index_base::find_name_component_bounds
4675 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4676 in completion mode. */
5c58de74
PA
4677
4678static bool
a3c5fafd 4679check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4680 const char *search_name,
4681 gdb::array_view<const char *> expected_syms)
4682{
4683 lookup_name_info lookup_name (search_name,
4684 symbol_name_match_type::FULL, true);
4685
3b00ef10
TT
4686 auto bounds = index.find_name_components_bounds (lookup_name,
4687 language_cplus);
5c58de74
PA
4688
4689 size_t distance = std::distance (bounds.first, bounds.second);
4690 if (distance != expected_syms.size ())
4691 return false;
4692
4693 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4694 {
4695 auto nc_elem = bounds.first + exp_elem;
4696 const char *qualified = index.symbol_name_at (nc_elem->idx);
4697 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4698 return false;
4699 }
4700
4701 return true;
4702}
4703
4704/* Test the lower-level mapped_index::find_name_component_bounds
4705 method. */
4706
c62446b1 4707static void
5c58de74
PA
4708test_mapped_index_find_name_component_bounds ()
4709{
4710 mock_mapped_index mock_index (test_symbols);
4711
a3c5fafd 4712 mock_index.build_name_components ();
5c58de74
PA
4713
4714 /* Test the lower-level mapped_index::find_name_component_bounds
4715 method in completion mode. */
4716 {
4717 static const char *expected_syms[] = {
4718 "t1_func",
4719 "t1_func1",
5c58de74
PA
4720 };
4721
a3c5fafd 4722 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4723 "t1_func", expected_syms));
4724 }
4725
4726 /* Check that the increment-last-char in the name matching algorithm
4727 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4728 {
4729 static const char *expected_syms1[] = {
4730 "\377",
4731 "\377\377123",
4732 };
a3c5fafd 4733 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4734 "\377", expected_syms1));
4735
4736 static const char *expected_syms2[] = {
4737 "\377\377123",
4738 };
a3c5fafd 4739 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4740 "\377\377", expected_syms2));
4741 }
4742}
4743
4744/* Test dw2_expand_symtabs_matching_symbol. */
4745
4746static void
4747test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4748{
4749 mock_mapped_index mock_index (test_symbols);
4750
4751 /* We let all tests run until the end even if some fails, for debug
4752 convenience. */
4753 bool any_mismatch = false;
4754
4755 /* Create the expected symbols list (an initializer_list). Needed
4756 because lists have commas, and we need to pass them to CHECK,
4757 which is a macro. */
4758#define EXPECT(...) { __VA_ARGS__ }
4759
4760 /* Wrapper for check_match that passes down the current
4761 __FILE__/__LINE__. */
4762#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4763 any_mismatch |= !check_match (__FILE__, __LINE__, \
4764 mock_index, \
4765 NAME, MATCH_TYPE, COMPLETION_MODE, \
4766 EXPECTED_LIST)
4767
4768 /* Identity checks. */
4769 for (const char *sym : test_symbols)
4770 {
4771 /* Should be able to match all existing symbols. */
4772 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4773 EXPECT (sym));
4774
4775 /* Should be able to match all existing symbols with
4776 parameters. */
4777 std::string with_params = std::string (sym) + "(int)";
4778 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4779 EXPECT (sym));
4780
4781 /* Should be able to match all existing symbols with
4782 parameters and qualifiers. */
4783 with_params = std::string (sym) + " ( int ) const";
4784 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4785 EXPECT (sym));
4786
4787 /* This should really find sym, but cp-name-parser.y doesn't
4788 know about lvalue/rvalue qualifiers yet. */
4789 with_params = std::string (sym) + " ( int ) &&";
4790 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4791 {});
4792 }
4793
e1ef7d7a
PA
4794 /* Check that the name matching algorithm for completion doesn't get
4795 confused with Latin1 'ÿ' / 0xff. */
4796 {
4797 static const char str[] = "\377";
4798 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4799 EXPECT ("\377", "\377\377123"));
4800 }
4801
4802 /* Check that the increment-last-char in the matching algorithm for
4803 completion doesn't match "t1_fund" when completing "t1_func". */
4804 {
4805 static const char str[] = "t1_func";
4806 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4807 EXPECT ("t1_func", "t1_func1"));
4808 }
4809
c62446b1
PA
4810 /* Check that completion mode works at each prefix of the expected
4811 symbol name. */
4812 {
4813 static const char str[] = "function(int)";
4814 size_t len = strlen (str);
4815 std::string lookup;
4816
4817 for (size_t i = 1; i < len; i++)
4818 {
4819 lookup.assign (str, i);
4820 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4821 EXPECT ("function"));
4822 }
4823 }
4824
4825 /* While "w" is a prefix of both components, the match function
4826 should still only be called once. */
4827 {
4828 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4829 EXPECT ("w1::w2"));
a20714ff
PA
4830 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4831 EXPECT ("w1::w2"));
c62446b1
PA
4832 }
4833
4834 /* Same, with a "complicated" symbol. */
4835 {
4836 static const char str[] = Z_SYM_NAME;
4837 size_t len = strlen (str);
4838 std::string lookup;
4839
4840 for (size_t i = 1; i < len; i++)
4841 {
4842 lookup.assign (str, i);
4843 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4844 EXPECT (Z_SYM_NAME));
4845 }
4846 }
4847
4848 /* In FULL mode, an incomplete symbol doesn't match. */
4849 {
4850 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4851 {});
4852 }
4853
4854 /* A complete symbol with parameters matches any overload, since the
4855 index has no overload info. */
4856 {
4857 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4858 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4859 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4860 EXPECT ("std::zfunction", "std::zfunction2"));
4861 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4862 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4863 }
4864
4865 /* Check that whitespace is ignored appropriately. A symbol with a
4866 template argument list. */
4867 {
4868 static const char expected[] = "ns::foo<int>";
4869 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4870 EXPECT (expected));
a20714ff
PA
4871 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4872 EXPECT (expected));
c62446b1
PA
4873 }
4874
4875 /* Check that whitespace is ignored appropriately. A symbol with a
4876 template argument list that includes a pointer. */
4877 {
4878 static const char expected[] = "ns::foo<char*>";
4879 /* Try both completion and non-completion modes. */
4880 static const bool completion_mode[2] = {false, true};
4881 for (size_t i = 0; i < 2; i++)
4882 {
4883 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4884 completion_mode[i], EXPECT (expected));
a20714ff
PA
4885 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4886 completion_mode[i], EXPECT (expected));
c62446b1
PA
4887
4888 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4889 completion_mode[i], EXPECT (expected));
a20714ff
PA
4890 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4891 completion_mode[i], EXPECT (expected));
c62446b1
PA
4892 }
4893 }
4894
4895 {
4896 /* Check method qualifiers are ignored. */
4897 static const char expected[] = "ns::foo<char*>";
4898 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4899 symbol_name_match_type::FULL, true, EXPECT (expected));
4900 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4901 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4902 CHECK_MATCH ("foo < char * > ( int ) const",
4903 symbol_name_match_type::WILD, true, EXPECT (expected));
4904 CHECK_MATCH ("foo < char * > ( int ) &&",
4905 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4906 }
4907
4908 /* Test lookup names that don't match anything. */
4909 {
a20714ff
PA
4910 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4911 {});
4912
c62446b1
PA
4913 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4914 {});
4915 }
4916
a20714ff
PA
4917 /* Some wild matching tests, exercising "(anonymous namespace)",
4918 which should not be confused with a parameter list. */
4919 {
4920 static const char *syms[] = {
4921 "A::B::C",
4922 "B::C",
4923 "C",
4924 "A :: B :: C ( int )",
4925 "B :: C ( int )",
4926 "C ( int )",
4927 };
4928
4929 for (const char *s : syms)
4930 {
4931 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4932 EXPECT ("(anonymous namespace)::A::B::C"));
4933 }
4934 }
4935
4936 {
4937 static const char expected[] = "ns2::tmpl<int>::foo2";
4938 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4939 EXPECT (expected));
4940 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4941 EXPECT (expected));
4942 }
4943
c62446b1
PA
4944 SELF_CHECK (!any_mismatch);
4945
4946#undef EXPECT
4947#undef CHECK_MATCH
4948}
4949
5c58de74
PA
4950static void
4951run_test ()
4952{
4953 test_mapped_index_find_name_component_bounds ();
4954 test_dw2_expand_symtabs_matching_symbol ();
4955}
4956
c62446b1
PA
4957}} // namespace selftests::dw2_expand_symtabs_matching
4958
4959#endif /* GDB_SELF_TEST */
4960
4b514bc8
JK
4961/* If FILE_MATCHER is NULL or if PER_CU has
4962 dwarf2_per_cu_quick_data::MARK set (see
4963 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4964 EXPANSION_NOTIFY on it. */
4965
4966static void
4967dw2_expand_symtabs_matching_one
4968 (struct dwarf2_per_cu_data *per_cu,
4969 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4970 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4971{
4972 if (file_matcher == NULL || per_cu->v.quick->mark)
4973 {
4974 bool symtab_was_null
4975 = (per_cu->v.quick->compunit_symtab == NULL);
4976
58f0c718 4977 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4978
4979 if (expansion_notify != NULL
4980 && symtab_was_null
4981 && per_cu->v.quick->compunit_symtab != NULL)
4982 expansion_notify (per_cu->v.quick->compunit_symtab);
4983 }
4984}
4985
3f563c84
PA
4986/* Helper for dw2_expand_matching symtabs. Called on each symbol
4987 matched, to expand corresponding CUs that were marked. IDX is the
4988 index of the symbol name that matched. */
4989
4990static void
4991dw2_expand_marked_cus
ed2dc618 4992 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4993 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4994 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4995 search_domain kind)
4996{
3f563c84
PA
4997 offset_type *vec, vec_len, vec_idx;
4998 bool global_seen = false;
ed2dc618 4999 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5000
61920122 5001 vec = (offset_type *) (index.constant_pool
f00a2de2 5002 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5003 vec_len = MAYBE_SWAP (vec[0]);
5004 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5005 {
61920122
PA
5006 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5007 /* This value is only valid for index versions >= 7. */
5008 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5009 gdb_index_symbol_kind symbol_kind =
5010 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5011 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5012 /* Only check the symbol attributes if they're present.
5013 Indices prior to version 7 don't record them,
5014 and indices >= 7 may elide them for certain symbols
5015 (gold does this). */
5016 int attrs_valid =
5017 (index.version >= 7
5018 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5019
5020 /* Work around gold/15646. */
5021 if (attrs_valid)
9291a0cd 5022 {
61920122
PA
5023 if (!is_static && global_seen)
5024 continue;
5025 if (!is_static)
5026 global_seen = true;
5027 }
3190f0c6 5028
61920122
PA
5029 /* Only check the symbol's kind if it has one. */
5030 if (attrs_valid)
5031 {
5032 switch (kind)
8943b874 5033 {
61920122
PA
5034 case VARIABLES_DOMAIN:
5035 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5036 continue;
5037 break;
5038 case FUNCTIONS_DOMAIN:
5039 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5040 continue;
61920122
PA
5041 break;
5042 case TYPES_DOMAIN:
5043 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5044 continue;
5045 break;
5046 default:
5047 break;
8943b874 5048 }
61920122 5049 }
8943b874 5050
61920122 5051 /* Don't crash on bad data. */
b76e467d 5052 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5053 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5054 {
b98664d3 5055 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5056 " [in module %s]"),
5057 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5058 continue;
5059 }
5060
ff4c9fec 5061 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5062 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5063 expansion_notify);
61920122
PA
5064 }
5065}
5066
4b514bc8
JK
5067/* If FILE_MATCHER is non-NULL, set all the
5068 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5069 that match FILE_MATCHER. */
5070
61920122 5071static void
4b514bc8 5072dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5073 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5074 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5075{
4b514bc8 5076 if (file_matcher == NULL)
61920122
PA
5077 return;
5078
4b514bc8
JK
5079 objfile *const objfile = dwarf2_per_objfile->objfile;
5080
5081 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5082 htab_eq_pointer,
5083 NULL, xcalloc, xfree));
5084 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5085 htab_eq_pointer,
5086 NULL, xcalloc, xfree));
61920122 5087
4b514bc8
JK
5088 /* The rule is CUs specify all the files, including those used by
5089 any TU, so there's no need to scan TUs here. */
61920122 5090
b76e467d 5091 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5092 {
927aa2e7
JK
5093 QUIT;
5094
5095 per_cu->v.quick->mark = 0;
5096
5097 /* We only need to look at symtabs not already expanded. */
5098 if (per_cu->v.quick->compunit_symtab)
5099 continue;
5100
b76e467d 5101 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5102 if (file_data == NULL)
5103 continue;
5104
5105 if (htab_find (visited_not_found.get (), file_data) != NULL)
5106 continue;
5107 else if (htab_find (visited_found.get (), file_data) != NULL)
5108 {
5109 per_cu->v.quick->mark = 1;
5110 continue;
5111 }
5112
b76e467d 5113 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5114 {
5115 const char *this_real_name;
5116
5117 if (file_matcher (file_data->file_names[j], false))
5118 {
5119 per_cu->v.quick->mark = 1;
5120 break;
5121 }
5122
5123 /* Before we invoke realpath, which can get expensive when many
5124 files are involved, do a quick comparison of the basenames. */
5125 if (!basenames_may_differ
5126 && !file_matcher (lbasename (file_data->file_names[j]),
5127 true))
5128 continue;
5129
5130 this_real_name = dw2_get_real_path (objfile, file_data, j);
5131 if (file_matcher (this_real_name, false))
5132 {
5133 per_cu->v.quick->mark = 1;
5134 break;
5135 }
5136 }
5137
b76e467d
SM
5138 void **slot = htab_find_slot (per_cu->v.quick->mark
5139 ? visited_found.get ()
5140 : visited_not_found.get (),
5141 file_data, INSERT);
927aa2e7
JK
5142 *slot = file_data;
5143 }
5144}
5145
5146static void
5147dw2_expand_symtabs_matching
5148 (struct objfile *objfile,
5149 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5150 const lookup_name_info &lookup_name,
5151 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5152 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5153 enum search_domain kind)
5154{
ed2dc618
SM
5155 struct dwarf2_per_objfile *dwarf2_per_objfile
5156 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5157
5158 /* index_table is NULL if OBJF_READNOW. */
5159 if (!dwarf2_per_objfile->index_table)
5160 return;
5161
ed2dc618 5162 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5163
5164 mapped_index &index = *dwarf2_per_objfile->index_table;
5165
5166 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5167 symbol_matcher,
5168 kind, [&] (offset_type idx)
5169 {
ed2dc618 5170 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5171 expansion_notify, kind);
3b00ef10 5172 return true;
927aa2e7
JK
5173 });
5174}
5175
5176/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5177 symtab. */
5178
5179static struct compunit_symtab *
5180recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5181 CORE_ADDR pc)
5182{
5183 int i;
5184
5185 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5186 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5187 return cust;
5188
5189 if (cust->includes == NULL)
5190 return NULL;
5191
5192 for (i = 0; cust->includes[i]; ++i)
5193 {
5194 struct compunit_symtab *s = cust->includes[i];
5195
5196 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5197 if (s != NULL)
5198 return s;
5199 }
5200
5201 return NULL;
5202}
5203
5204static struct compunit_symtab *
5205dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5206 struct bound_minimal_symbol msymbol,
5207 CORE_ADDR pc,
5208 struct obj_section *section,
5209 int warn_if_readin)
5210{
5211 struct dwarf2_per_cu_data *data;
5212 struct compunit_symtab *result;
5213
d320c2b5 5214 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5215 return NULL;
5216
79748972
TT
5217 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5218 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5219 data = (struct dwarf2_per_cu_data *) addrmap_find
5220 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5221 if (!data)
5222 return NULL;
5223
5224 if (warn_if_readin && data->v.quick->compunit_symtab)
5225 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5226 paddress (get_objfile_arch (objfile), pc));
5227
5228 result
58f0c718
TT
5229 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5230 false),
927aa2e7
JK
5231 pc);
5232 gdb_assert (result != NULL);
5233 return result;
5234}
5235
5236static void
5237dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5238 void *data, int need_fullname)
5239{
ed2dc618
SM
5240 struct dwarf2_per_objfile *dwarf2_per_objfile
5241 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5242
5243 if (!dwarf2_per_objfile->filenames_cache)
5244 {
5245 dwarf2_per_objfile->filenames_cache.emplace ();
5246
5247 htab_up visited (htab_create_alloc (10,
5248 htab_hash_pointer, htab_eq_pointer,
5249 NULL, xcalloc, xfree));
5250
5251 /* The rule is CUs specify all the files, including those used
5252 by any TU, so there's no need to scan TUs here. We can
5253 ignore file names coming from already-expanded CUs. */
5254
b76e467d 5255 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5256 {
927aa2e7
JK
5257 if (per_cu->v.quick->compunit_symtab)
5258 {
5259 void **slot = htab_find_slot (visited.get (),
5260 per_cu->v.quick->file_names,
5261 INSERT);
5262
5263 *slot = per_cu->v.quick->file_names;
5264 }
5265 }
5266
b76e467d 5267 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5268 {
927aa2e7
JK
5269 /* We only need to look at symtabs not already expanded. */
5270 if (per_cu->v.quick->compunit_symtab)
5271 continue;
5272
b76e467d 5273 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5274 if (file_data == NULL)
5275 continue;
5276
b76e467d 5277 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5278 if (*slot)
5279 {
5280 /* Already visited. */
5281 continue;
5282 }
5283 *slot = file_data;
5284
5285 for (int j = 0; j < file_data->num_file_names; ++j)
5286 {
5287 const char *filename = file_data->file_names[j];
5288 dwarf2_per_objfile->filenames_cache->seen (filename);
5289 }
5290 }
5291 }
5292
5293 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5294 {
5295 gdb::unique_xmalloc_ptr<char> this_real_name;
5296
5297 if (need_fullname)
5298 this_real_name = gdb_realpath (filename);
5299 (*fun) (filename, this_real_name.get (), data);
5300 });
5301}
5302
5303static int
5304dw2_has_symbols (struct objfile *objfile)
5305{
5306 return 1;
5307}
5308
5309const struct quick_symbol_functions dwarf2_gdb_index_functions =
5310{
5311 dw2_has_symbols,
5312 dw2_find_last_source_symtab,
5313 dw2_forget_cached_source_info,
5314 dw2_map_symtabs_matching_filename,
5315 dw2_lookup_symbol,
5316 dw2_print_stats,
5317 dw2_dump,
927aa2e7
JK
5318 dw2_expand_symtabs_for_function,
5319 dw2_expand_all_symtabs,
5320 dw2_expand_symtabs_with_fullname,
5321 dw2_map_matching_symbols,
5322 dw2_expand_symtabs_matching,
5323 dw2_find_pc_sect_compunit_symtab,
5324 NULL,
5325 dw2_map_symbol_filenames
5326};
5327
5328/* DWARF-5 debug_names reader. */
5329
5330/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5331static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5332
5333/* A helper function that reads the .debug_names section in SECTION
5334 and fills in MAP. FILENAME is the name of the file containing the
5335 section; it is used for error reporting.
5336
5337 Returns true if all went well, false otherwise. */
5338
5339static bool
5340read_debug_names_from_section (struct objfile *objfile,
5341 const char *filename,
5342 struct dwarf2_section_info *section,
5343 mapped_debug_names &map)
5344{
5345 if (dwarf2_section_empty_p (section))
5346 return false;
5347
5348 /* Older elfutils strip versions could keep the section in the main
5349 executable while splitting it for the separate debug info file. */
5350 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5351 return false;
5352
5353 dwarf2_read_section (objfile, section);
5354
5355 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5356
5357 const gdb_byte *addr = section->buffer;
5358
5359 bfd *const abfd = get_section_bfd_owner (section);
5360
5361 unsigned int bytes_read;
5362 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5363 addr += bytes_read;
5364
5365 map.dwarf5_is_dwarf64 = bytes_read != 4;
5366 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5367 if (bytes_read + length != section->size)
5368 {
5369 /* There may be multiple per-CU indices. */
5370 warning (_("Section .debug_names in %s length %s does not match "
5371 "section length %s, ignoring .debug_names."),
5372 filename, plongest (bytes_read + length),
5373 pulongest (section->size));
5374 return false;
5375 }
5376
5377 /* The version number. */
5378 uint16_t version = read_2_bytes (abfd, addr);
5379 addr += 2;
5380 if (version != 5)
5381 {
5382 warning (_("Section .debug_names in %s has unsupported version %d, "
5383 "ignoring .debug_names."),
5384 filename, version);
5385 return false;
5386 }
5387
5388 /* Padding. */
5389 uint16_t padding = read_2_bytes (abfd, addr);
5390 addr += 2;
5391 if (padding != 0)
5392 {
5393 warning (_("Section .debug_names in %s has unsupported padding %d, "
5394 "ignoring .debug_names."),
5395 filename, padding);
5396 return false;
5397 }
5398
5399 /* comp_unit_count - The number of CUs in the CU list. */
5400 map.cu_count = read_4_bytes (abfd, addr);
5401 addr += 4;
5402
5403 /* local_type_unit_count - The number of TUs in the local TU
5404 list. */
5405 map.tu_count = read_4_bytes (abfd, addr);
5406 addr += 4;
5407
5408 /* foreign_type_unit_count - The number of TUs in the foreign TU
5409 list. */
5410 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5411 addr += 4;
5412 if (foreign_tu_count != 0)
5413 {
5414 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5415 "ignoring .debug_names."),
5416 filename, static_cast<unsigned long> (foreign_tu_count));
5417 return false;
5418 }
5419
5420 /* bucket_count - The number of hash buckets in the hash lookup
5421 table. */
5422 map.bucket_count = read_4_bytes (abfd, addr);
5423 addr += 4;
5424
5425 /* name_count - The number of unique names in the index. */
5426 map.name_count = read_4_bytes (abfd, addr);
5427 addr += 4;
5428
5429 /* abbrev_table_size - The size in bytes of the abbreviations
5430 table. */
5431 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5432 addr += 4;
5433
5434 /* augmentation_string_size - The size in bytes of the augmentation
5435 string. This value is rounded up to a multiple of 4. */
5436 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5437 addr += 4;
5438 map.augmentation_is_gdb = ((augmentation_string_size
5439 == sizeof (dwarf5_augmentation))
5440 && memcmp (addr, dwarf5_augmentation,
5441 sizeof (dwarf5_augmentation)) == 0);
5442 augmentation_string_size += (-augmentation_string_size) & 3;
5443 addr += augmentation_string_size;
5444
5445 /* List of CUs */
5446 map.cu_table_reordered = addr;
5447 addr += map.cu_count * map.offset_size;
5448
5449 /* List of Local TUs */
5450 map.tu_table_reordered = addr;
5451 addr += map.tu_count * map.offset_size;
5452
5453 /* Hash Lookup Table */
5454 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5455 addr += map.bucket_count * 4;
5456 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5457 addr += map.name_count * 4;
5458
5459 /* Name Table */
5460 map.name_table_string_offs_reordered = addr;
5461 addr += map.name_count * map.offset_size;
5462 map.name_table_entry_offs_reordered = addr;
5463 addr += map.name_count * map.offset_size;
5464
5465 const gdb_byte *abbrev_table_start = addr;
5466 for (;;)
5467 {
927aa2e7
JK
5468 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5469 addr += bytes_read;
5470 if (index_num == 0)
5471 break;
5472
5473 const auto insertpair
5474 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5475 if (!insertpair.second)
5476 {
5477 warning (_("Section .debug_names in %s has duplicate index %s, "
5478 "ignoring .debug_names."),
5479 filename, pulongest (index_num));
5480 return false;
5481 }
5482 mapped_debug_names::index_val &indexval = insertpair.first->second;
5483 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5484 addr += bytes_read;
5485
5486 for (;;)
5487 {
5488 mapped_debug_names::index_val::attr attr;
5489 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5490 addr += bytes_read;
5491 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5492 addr += bytes_read;
5493 if (attr.form == DW_FORM_implicit_const)
5494 {
5495 attr.implicit_const = read_signed_leb128 (abfd, addr,
5496 &bytes_read);
5497 addr += bytes_read;
5498 }
5499 if (attr.dw_idx == 0 && attr.form == 0)
5500 break;
5501 indexval.attr_vec.push_back (std::move (attr));
5502 }
5503 }
5504 if (addr != abbrev_table_start + abbrev_table_size)
5505 {
5506 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5507 "of size %s vs. written as %u, ignoring .debug_names."),
5508 filename, plongest (addr - abbrev_table_start),
5509 abbrev_table_size);
927aa2e7
JK
5510 return false;
5511 }
5512 map.entry_pool = addr;
5513
5514 return true;
5515}
5516
5517/* A helper for create_cus_from_debug_names that handles the MAP's CU
5518 list. */
5519
5520static void
ed2dc618 5521create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5522 const mapped_debug_names &map,
5523 dwarf2_section_info &section,
b76e467d 5524 bool is_dwz)
927aa2e7
JK
5525{
5526 sect_offset sect_off_prev;
5527 for (uint32_t i = 0; i <= map.cu_count; ++i)
5528 {
5529 sect_offset sect_off_next;
5530 if (i < map.cu_count)
5531 {
5532 sect_off_next
5533 = (sect_offset) (extract_unsigned_integer
5534 (map.cu_table_reordered + i * map.offset_size,
5535 map.offset_size,
5536 map.dwarf5_byte_order));
5537 }
5538 else
5539 sect_off_next = (sect_offset) section.size;
5540 if (i >= 1)
5541 {
5542 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5543 dwarf2_per_cu_data *per_cu
ed2dc618 5544 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5545 sect_off_prev, length);
b76e467d 5546 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5547 }
5548 sect_off_prev = sect_off_next;
5549 }
5550}
5551
5552/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5553 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5554
5555static void
ed2dc618 5556create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5557 const mapped_debug_names &map,
5558 const mapped_debug_names &dwz_map)
5559{
b76e467d
SM
5560 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5561 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5562
ed2dc618
SM
5563 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5564 dwarf2_per_objfile->info,
b76e467d 5565 false /* is_dwz */);
927aa2e7
JK
5566
5567 if (dwz_map.cu_count == 0)
5568 return;
5569
ed2dc618
SM
5570 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5571 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5572 true /* is_dwz */);
927aa2e7
JK
5573}
5574
5575/* Read .debug_names. If everything went ok, initialize the "quick"
5576 elements of all the CUs and return true. Otherwise, return false. */
5577
5578static bool
ed2dc618 5579dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5580{
22ca247e
TT
5581 std::unique_ptr<mapped_debug_names> map
5582 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5583 mapped_debug_names dwz_map (dwarf2_per_objfile);
5584 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5585
5586 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5587 &dwarf2_per_objfile->debug_names,
22ca247e 5588 *map))
927aa2e7
JK
5589 return false;
5590
5591 /* Don't use the index if it's empty. */
22ca247e 5592 if (map->name_count == 0)
927aa2e7
JK
5593 return false;
5594
5595 /* If there is a .dwz file, read it so we can get its CU list as
5596 well. */
ed2dc618 5597 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5598 if (dwz != NULL)
5599 {
5600 if (!read_debug_names_from_section (objfile,
00f93c44 5601 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5602 &dwz->debug_names, dwz_map))
5603 {
5604 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5605 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5606 return false;
5607 }
5608 }
5609
22ca247e 5610 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5611
22ca247e 5612 if (map->tu_count != 0)
927aa2e7
JK
5613 {
5614 /* We can only handle a single .debug_types when we have an
5615 index. */
fd5866f6 5616 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5617 return false;
5618
fd5866f6 5619 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5620
5621 create_signatured_type_table_from_debug_names
22ca247e 5622 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5623 }
5624
ed2dc618
SM
5625 create_addrmap_from_aranges (dwarf2_per_objfile,
5626 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5627
22ca247e 5628 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5629 dwarf2_per_objfile->using_index = 1;
5630 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5631 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5632
5633 return true;
5634}
5635
927aa2e7
JK
5636/* Type used to manage iterating over all CUs looking for a symbol for
5637 .debug_names. */
5638
5639class dw2_debug_names_iterator
5640{
5641public:
927aa2e7 5642 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5643 gdb::optional<block_enum> block_index,
5644 domain_enum domain,
927aa2e7 5645 const char *name)
2b79f376 5646 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5647 m_addr (find_vec_in_debug_names (map, name))
5648 {}
5649
5650 dw2_debug_names_iterator (const mapped_debug_names &map,
5651 search_domain search, uint32_t namei)
5652 : m_map (map),
5653 m_search (search),
5654 m_addr (find_vec_in_debug_names (map, namei))
5655 {}
5656
3b00ef10
TT
5657 dw2_debug_names_iterator (const mapped_debug_names &map,
5658 block_enum block_index, domain_enum domain,
5659 uint32_t namei)
5660 : m_map (map), m_block_index (block_index), m_domain (domain),
5661 m_addr (find_vec_in_debug_names (map, namei))
5662 {}
5663
927aa2e7
JK
5664 /* Return the next matching CU or NULL if there are no more. */
5665 dwarf2_per_cu_data *next ();
5666
5667private:
5668 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5669 const char *name);
5670 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5671 uint32_t namei);
5672
5673 /* The internalized form of .debug_names. */
5674 const mapped_debug_names &m_map;
5675
2b79f376
SM
5676 /* If set, only look for symbols that match that block. Valid values are
5677 GLOBAL_BLOCK and STATIC_BLOCK. */
5678 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5679
5680 /* The kind of symbol we're looking for. */
5681 const domain_enum m_domain = UNDEF_DOMAIN;
5682 const search_domain m_search = ALL_DOMAIN;
5683
5684 /* The list of CUs from the index entry of the symbol, or NULL if
5685 not found. */
5686 const gdb_byte *m_addr;
5687};
5688
5689const char *
5690mapped_debug_names::namei_to_name (uint32_t namei) const
5691{
5692 const ULONGEST namei_string_offs
5693 = extract_unsigned_integer ((name_table_string_offs_reordered
5694 + namei * offset_size),
5695 offset_size,
5696 dwarf5_byte_order);
5697 return read_indirect_string_at_offset
ed2dc618 5698 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5699}
5700
5701/* Find a slot in .debug_names for the object named NAME. If NAME is
5702 found, return pointer to its pool data. If NAME cannot be found,
5703 return NULL. */
5704
5705const gdb_byte *
5706dw2_debug_names_iterator::find_vec_in_debug_names
5707 (const mapped_debug_names &map, const char *name)
5708{
5709 int (*cmp) (const char *, const char *);
5710
54ee4252 5711 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5712 if (current_language->la_language == language_cplus
5713 || current_language->la_language == language_fortran
5714 || current_language->la_language == language_d)
5715 {
5716 /* NAME is already canonical. Drop any qualifiers as
5717 .debug_names does not contain any. */
5718
5719 if (strchr (name, '(') != NULL)
5720 {
54ee4252 5721 without_params = cp_remove_params (name);
927aa2e7 5722 if (without_params != NULL)
54ee4252 5723 name = without_params.get ();
927aa2e7
JK
5724 }
5725 }
5726
5727 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5728
5729 const uint32_t full_hash = dwarf5_djb_hash (name);
5730 uint32_t namei
5731 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5732 (map.bucket_table_reordered
5733 + (full_hash % map.bucket_count)), 4,
5734 map.dwarf5_byte_order);
5735 if (namei == 0)
5736 return NULL;
5737 --namei;
5738 if (namei >= map.name_count)
5739 {
b98664d3 5740 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5741 "[in module %s]"),
5742 namei, map.name_count,
ed2dc618 5743 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5744 return NULL;
5745 }
5746
5747 for (;;)
5748 {
5749 const uint32_t namei_full_hash
5750 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5751 (map.hash_table_reordered + namei), 4,
5752 map.dwarf5_byte_order);
5753 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5754 return NULL;
5755
5756 if (full_hash == namei_full_hash)
5757 {
5758 const char *const namei_string = map.namei_to_name (namei);
5759
5760#if 0 /* An expensive sanity check. */
5761 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5762 {
b98664d3 5763 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5764 "[in module %s]"),
5765 namei, objfile_name (dwarf2_per_objfile->objfile));
5766 return NULL;
5767 }
5768#endif
5769
5770 if (cmp (namei_string, name) == 0)
5771 {
5772 const ULONGEST namei_entry_offs
5773 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5774 + namei * map.offset_size),
5775 map.offset_size, map.dwarf5_byte_order);
5776 return map.entry_pool + namei_entry_offs;
5777 }
5778 }
5779
5780 ++namei;
5781 if (namei >= map.name_count)
5782 return NULL;
5783 }
5784}
5785
5786const gdb_byte *
5787dw2_debug_names_iterator::find_vec_in_debug_names
5788 (const mapped_debug_names &map, uint32_t namei)
5789{
5790 if (namei >= map.name_count)
5791 {
b98664d3 5792 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5793 "[in module %s]"),
5794 namei, map.name_count,
ed2dc618 5795 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5796 return NULL;
5797 }
5798
5799 const ULONGEST namei_entry_offs
5800 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5801 + namei * map.offset_size),
5802 map.offset_size, map.dwarf5_byte_order);
5803 return map.entry_pool + namei_entry_offs;
5804}
5805
5806/* See dw2_debug_names_iterator. */
5807
5808dwarf2_per_cu_data *
5809dw2_debug_names_iterator::next ()
5810{
5811 if (m_addr == NULL)
5812 return NULL;
5813
ed2dc618
SM
5814 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5815 struct objfile *objfile = dwarf2_per_objfile->objfile;
5816 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5817
5818 again:
5819
5820 unsigned int bytes_read;
5821 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5822 m_addr += bytes_read;
5823 if (abbrev == 0)
5824 return NULL;
5825
5826 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5827 if (indexval_it == m_map.abbrev_map.cend ())
5828 {
b98664d3 5829 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5830 "[in module %s]"),
ed2dc618 5831 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5832 return NULL;
5833 }
5834 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5835 enum class symbol_linkage {
5836 unknown,
5837 static_,
5838 extern_,
23c13d42 5839 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5840 dwarf2_per_cu_data *per_cu = NULL;
5841 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5842 {
5843 ULONGEST ull;
5844 switch (attr.form)
5845 {
5846 case DW_FORM_implicit_const:
5847 ull = attr.implicit_const;
5848 break;
5849 case DW_FORM_flag_present:
5850 ull = 1;
5851 break;
5852 case DW_FORM_udata:
5853 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5854 m_addr += bytes_read;
5855 break;
5856 default:
b98664d3 5857 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5858 dwarf_form_name (attr.form),
ed2dc618 5859 objfile_name (objfile));
927aa2e7
JK
5860 return NULL;
5861 }
5862 switch (attr.dw_idx)
5863 {
5864 case DW_IDX_compile_unit:
5865 /* Don't crash on bad data. */
b76e467d 5866 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5867 {
b98664d3 5868 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5869 " [in module %s]"),
5870 pulongest (ull),
5871 objfile_name (dwarf2_per_objfile->objfile));
5872 continue;
5873 }
ff4c9fec 5874 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5875 break;
8af5c486
JK
5876 case DW_IDX_type_unit:
5877 /* Don't crash on bad data. */
b2bdb8cf 5878 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5879 {
b98664d3 5880 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5881 " [in module %s]"),
5882 pulongest (ull),
5883 objfile_name (dwarf2_per_objfile->objfile));
5884 continue;
5885 }
ff4c9fec 5886 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5887 break;
927aa2e7
JK
5888 case DW_IDX_GNU_internal:
5889 if (!m_map.augmentation_is_gdb)
5890 break;
23c13d42 5891 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5892 break;
5893 case DW_IDX_GNU_external:
5894 if (!m_map.augmentation_is_gdb)
5895 break;
23c13d42 5896 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5897 break;
5898 }
5899 }
5900
5901 /* Skip if already read in. */
5902 if (per_cu->v.quick->compunit_symtab)
5903 goto again;
5904
5905 /* Check static vs global. */
23c13d42 5906 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5907 {
2b79f376 5908 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5909 const bool symbol_is_static =
5910 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5911 if (want_static != symbol_is_static)
2b79f376 5912 goto again;
927aa2e7
JK
5913 }
5914
5915 /* Match dw2_symtab_iter_next, symbol_kind
5916 and debug_names::psymbol_tag. */
5917 switch (m_domain)
5918 {
5919 case VAR_DOMAIN:
5920 switch (indexval.dwarf_tag)
5921 {
5922 case DW_TAG_variable:
5923 case DW_TAG_subprogram:
5924 /* Some types are also in VAR_DOMAIN. */
5925 case DW_TAG_typedef:
5926 case DW_TAG_structure_type:
5927 break;
5928 default:
5929 goto again;
5930 }
5931 break;
5932 case STRUCT_DOMAIN:
5933 switch (indexval.dwarf_tag)
5934 {
5935 case DW_TAG_typedef:
5936 case DW_TAG_structure_type:
5937 break;
5938 default:
5939 goto again;
5940 }
5941 break;
5942 case LABEL_DOMAIN:
5943 switch (indexval.dwarf_tag)
5944 {
5945 case 0:
5946 case DW_TAG_variable:
5947 break;
5948 default:
5949 goto again;
5950 }
5951 break;
5952 default:
5953 break;
5954 }
5955
5956 /* Match dw2_expand_symtabs_matching, symbol_kind and
5957 debug_names::psymbol_tag. */
5958 switch (m_search)
4b514bc8 5959 {
927aa2e7
JK
5960 case VARIABLES_DOMAIN:
5961 switch (indexval.dwarf_tag)
4b514bc8 5962 {
927aa2e7
JK
5963 case DW_TAG_variable:
5964 break;
5965 default:
5966 goto again;
4b514bc8 5967 }
927aa2e7
JK
5968 break;
5969 case FUNCTIONS_DOMAIN:
5970 switch (indexval.dwarf_tag)
4b514bc8 5971 {
927aa2e7
JK
5972 case DW_TAG_subprogram:
5973 break;
5974 default:
5975 goto again;
4b514bc8 5976 }
927aa2e7
JK
5977 break;
5978 case TYPES_DOMAIN:
5979 switch (indexval.dwarf_tag)
5980 {
5981 case DW_TAG_typedef:
5982 case DW_TAG_structure_type:
5983 break;
5984 default:
5985 goto again;
5986 }
5987 break;
5988 default:
5989 break;
4b514bc8 5990 }
927aa2e7
JK
5991
5992 return per_cu;
4b514bc8 5993}
61920122 5994
927aa2e7 5995static struct compunit_symtab *
c7f839cb 5996dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5997 const char *name, domain_enum domain)
4b514bc8 5998{
ed2dc618
SM
5999 struct dwarf2_per_objfile *dwarf2_per_objfile
6000 = get_dwarf2_per_objfile (objfile);
61920122 6001
927aa2e7
JK
6002 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6003 if (!mapp)
61920122 6004 {
927aa2e7
JK
6005 /* index is NULL if OBJF_READNOW. */
6006 return NULL;
6007 }
6008 const auto &map = *mapp;
9291a0cd 6009
2b79f376 6010 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6011
927aa2e7
JK
6012 struct compunit_symtab *stab_best = NULL;
6013 struct dwarf2_per_cu_data *per_cu;
6014 while ((per_cu = iter.next ()) != NULL)
6015 {
6016 struct symbol *sym, *with_opaque = NULL;
58f0c718 6017 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6018 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6019 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6020
927aa2e7
JK
6021 sym = block_find_symbol (block, name, domain,
6022 block_find_non_opaque_type_preferred,
6023 &with_opaque);
9703b513 6024
927aa2e7
JK
6025 /* Some caution must be observed with overloaded functions and
6026 methods, since the index will not contain any overload
6027 information (but NAME might contain it). */
a3ec0bb1 6028
927aa2e7
JK
6029 if (sym != NULL
6030 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6031 return stab;
6032 if (with_opaque != NULL
6033 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6034 stab_best = stab;
9703b513 6035
927aa2e7 6036 /* Keep looking through other CUs. */
9703b513
TT
6037 }
6038
927aa2e7 6039 return stab_best;
9703b513
TT
6040}
6041
927aa2e7
JK
6042/* This dumps minimal information about .debug_names. It is called
6043 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6044 uses this to verify that .debug_names has been loaded. */
9291a0cd 6045
927aa2e7
JK
6046static void
6047dw2_debug_names_dump (struct objfile *objfile)
6048{
ed2dc618
SM
6049 struct dwarf2_per_objfile *dwarf2_per_objfile
6050 = get_dwarf2_per_objfile (objfile);
6051
927aa2e7
JK
6052 gdb_assert (dwarf2_per_objfile->using_index);
6053 printf_filtered (".debug_names:");
6054 if (dwarf2_per_objfile->debug_names_table)
6055 printf_filtered (" exists\n");
6056 else
6057 printf_filtered (" faked for \"readnow\"\n");
6058 printf_filtered ("\n");
9291a0cd
TT
6059}
6060
9291a0cd 6061static void
927aa2e7
JK
6062dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6063 const char *func_name)
9291a0cd 6064{
ed2dc618
SM
6065 struct dwarf2_per_objfile *dwarf2_per_objfile
6066 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6067
927aa2e7
JK
6068 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6069 if (dwarf2_per_objfile->debug_names_table)
24c79950 6070 {
927aa2e7 6071 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6072
2b79f376 6073 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6074
927aa2e7
JK
6075 struct dwarf2_per_cu_data *per_cu;
6076 while ((per_cu = iter.next ()) != NULL)
58f0c718 6077 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6078 }
6079}
24c79950 6080
3b00ef10
TT
6081static void
6082dw2_debug_names_map_matching_symbols
6083 (struct objfile *objfile,
6084 const lookup_name_info &name, domain_enum domain,
6085 int global,
6086 gdb::function_view<symbol_found_callback_ftype> callback,
6087 symbol_compare_ftype *ordered_compare)
6088{
6089 struct dwarf2_per_objfile *dwarf2_per_objfile
6090 = get_dwarf2_per_objfile (objfile);
6091
6092 /* debug_names_table is NULL if OBJF_READNOW. */
6093 if (!dwarf2_per_objfile->debug_names_table)
6094 return;
6095
6096 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6097 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6098
6099 const char *match_name = name.ada ().lookup_name ().c_str ();
6100 auto matcher = [&] (const char *symname)
6101 {
6102 if (ordered_compare == nullptr)
6103 return true;
6104 return ordered_compare (symname, match_name) == 0;
6105 };
6106
6107 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6108 [&] (offset_type namei)
6109 {
6110 /* The name was matched, now expand corresponding CUs that were
6111 marked. */
6112 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6113
6114 struct dwarf2_per_cu_data *per_cu;
6115 while ((per_cu = iter.next ()) != NULL)
6116 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6117 return true;
6118 });
6119
6120 /* It's a shame we couldn't do this inside the
6121 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6122 that have already been expanded. Instead, this loop matches what
6123 the psymtab code does. */
6124 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6125 {
6126 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6127 if (cust != nullptr)
6128 {
6129 const struct block *block
6130 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6131 if (!iterate_over_symbols_terminated (block, name,
6132 domain, callback))
6133 break;
6134 }
6135 }
6136}
6137
927aa2e7
JK
6138static void
6139dw2_debug_names_expand_symtabs_matching
6140 (struct objfile *objfile,
6141 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6142 const lookup_name_info &lookup_name,
6143 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6144 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6145 enum search_domain kind)
6146{
ed2dc618
SM
6147 struct dwarf2_per_objfile *dwarf2_per_objfile
6148 = get_dwarf2_per_objfile (objfile);
9291a0cd 6149
927aa2e7
JK
6150 /* debug_names_table is NULL if OBJF_READNOW. */
6151 if (!dwarf2_per_objfile->debug_names_table)
6152 return;
9291a0cd 6153
ed2dc618 6154 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6155
44ed8f3e 6156 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6157
44ed8f3e
PA
6158 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6159 symbol_matcher,
6160 kind, [&] (offset_type namei)
927aa2e7 6161 {
927aa2e7
JK
6162 /* The name was matched, now expand corresponding CUs that were
6163 marked. */
6164 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6165
927aa2e7
JK
6166 struct dwarf2_per_cu_data *per_cu;
6167 while ((per_cu = iter.next ()) != NULL)
6168 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6169 expansion_notify);
3b00ef10 6170 return true;
44ed8f3e 6171 });
9291a0cd
TT
6172}
6173
927aa2e7 6174const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6175{
6176 dw2_has_symbols,
6177 dw2_find_last_source_symtab,
6178 dw2_forget_cached_source_info,
f8eba3c6 6179 dw2_map_symtabs_matching_filename,
927aa2e7 6180 dw2_debug_names_lookup_symbol,
9291a0cd 6181 dw2_print_stats,
927aa2e7 6182 dw2_debug_names_dump,
927aa2e7 6183 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6184 dw2_expand_all_symtabs,
652a8996 6185 dw2_expand_symtabs_with_fullname,
3b00ef10 6186 dw2_debug_names_map_matching_symbols,
927aa2e7 6187 dw2_debug_names_expand_symtabs_matching,
43f3e411 6188 dw2_find_pc_sect_compunit_symtab,
71a3c369 6189 NULL,
9291a0cd
TT
6190 dw2_map_symbol_filenames
6191};
6192
4485a1c1
SM
6193/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6194 to either a dwarf2_per_objfile or dwz_file object. */
6195
6196template <typename T>
6197static gdb::array_view<const gdb_byte>
6198get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6199{
6200 dwarf2_section_info *section = &section_owner->gdb_index;
6201
6202 if (dwarf2_section_empty_p (section))
6203 return {};
6204
6205 /* Older elfutils strip versions could keep the section in the main
6206 executable while splitting it for the separate debug info file. */
6207 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6208 return {};
6209
6210 dwarf2_read_section (obj, section);
6211
8bebfcda
PA
6212 /* dwarf2_section_info::size is a bfd_size_type, while
6213 gdb::array_view works with size_t. On 32-bit hosts, with
6214 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6215 is 32-bit. So we need an explicit narrowing conversion here.
6216 This is fine, because it's impossible to allocate or mmap an
6217 array/buffer larger than what size_t can represent. */
6218 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6219}
6220
87d6a7aa
SM
6221/* Lookup the index cache for the contents of the index associated to
6222 DWARF2_OBJ. */
6223
6224static gdb::array_view<const gdb_byte>
6225get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6226{
6227 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6228 if (build_id == nullptr)
6229 return {};
6230
6231 return global_index_cache.lookup_gdb_index (build_id,
6232 &dwarf2_obj->index_cache_res);
6233}
6234
6235/* Same as the above, but for DWZ. */
6236
6237static gdb::array_view<const gdb_byte>
6238get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6239{
6240 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6241 if (build_id == nullptr)
6242 return {};
6243
6244 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6245}
6246
3c0aa29a 6247/* See symfile.h. */
9291a0cd 6248
3c0aa29a
PA
6249bool
6250dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6251{
ed2dc618
SM
6252 struct dwarf2_per_objfile *dwarf2_per_objfile
6253 = get_dwarf2_per_objfile (objfile);
6254
9291a0cd
TT
6255 /* If we're about to read full symbols, don't bother with the
6256 indices. In this case we also don't care if some other debug
6257 format is making psymtabs, because they are all about to be
6258 expanded anyway. */
6259 if ((objfile->flags & OBJF_READNOW))
6260 {
9291a0cd 6261 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6262 create_all_comp_units (dwarf2_per_objfile);
6263 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6264 dwarf2_per_objfile->quick_file_names_table
6265 = create_quick_file_names_table
6266 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6267
b76e467d 6268 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6269 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6270 {
ff4c9fec 6271 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6272
e254ef6a
DE
6273 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6274 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6275 }
6276
6277 /* Return 1 so that gdb sees the "quick" functions. However,
6278 these functions will be no-ops because we will have expanded
6279 all symtabs. */
3c0aa29a
PA
6280 *index_kind = dw_index_kind::GDB_INDEX;
6281 return true;
9291a0cd
TT
6282 }
6283
ed2dc618 6284 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6285 {
6286 *index_kind = dw_index_kind::DEBUG_NAMES;
6287 return true;
6288 }
927aa2e7 6289
4485a1c1
SM
6290 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6291 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6292 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6293 {
6294 *index_kind = dw_index_kind::GDB_INDEX;
6295 return true;
6296 }
9291a0cd 6297
87d6a7aa
SM
6298 /* ... otherwise, try to find the index in the index cache. */
6299 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6300 get_gdb_index_contents_from_cache,
6301 get_gdb_index_contents_from_cache_dwz))
6302 {
6303 global_index_cache.hit ();
6304 *index_kind = dw_index_kind::GDB_INDEX;
6305 return true;
6306 }
6307
6308 global_index_cache.miss ();
3c0aa29a 6309 return false;
9291a0cd
TT
6310}
6311
6312\f
6313
dce234bc
PP
6314/* Build a partial symbol table. */
6315
6316void
f29dff0a 6317dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6318{
ed2dc618
SM
6319 struct dwarf2_per_objfile *dwarf2_per_objfile
6320 = get_dwarf2_per_objfile (objfile);
c9bf0622 6321
6eee24ce 6322 init_psymbol_list (objfile, 1024);
c906108c 6323
a70b8144 6324 try
c9bf0622
TT
6325 {
6326 /* This isn't really ideal: all the data we allocate on the
6327 objfile's obstack is still uselessly kept around. However,
6328 freeing it seems unsafe. */
906768f9 6329 psymtab_discarder psymtabs (objfile);
ed2dc618 6330 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6331 psymtabs.keep ();
87d6a7aa
SM
6332
6333 /* (maybe) store an index in the cache. */
6334 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6335 }
230d2906 6336 catch (const gdb_exception_error &except)
492d29ea
PA
6337 {
6338 exception_print (gdb_stderr, except);
6339 }
c906108c 6340}
c906108c 6341
1ce1cefd
DE
6342/* Return the total length of the CU described by HEADER. */
6343
6344static unsigned int
6345get_cu_length (const struct comp_unit_head *header)
6346{
6347 return header->initial_length_size + header->length;
6348}
6349
9c541725 6350/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6351
9c541725
PA
6352static inline bool
6353offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6354{
9c541725
PA
6355 sect_offset bottom = cu_header->sect_off;
6356 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6357
9c541725 6358 return sect_off >= bottom && sect_off < top;
45452591
DE
6359}
6360
3b80fe9b
DE
6361/* Find the base address of the compilation unit for range lists and
6362 location lists. It will normally be specified by DW_AT_low_pc.
6363 In DWARF-3 draft 4, the base address could be overridden by
6364 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6365 compilation units with discontinuous ranges. */
6366
6367static void
6368dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6369{
6370 struct attribute *attr;
6371
6372 cu->base_known = 0;
6373 cu->base_address = 0;
6374
6375 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6376 if (attr)
6377 {
31aa7e4e 6378 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6379 cu->base_known = 1;
6380 }
6381 else
6382 {
6383 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6384 if (attr)
6385 {
31aa7e4e 6386 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6387 cu->base_known = 1;
6388 }
6389 }
6390}
6391
93311388 6392/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6393 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6394 NOTE: This leaves members offset, first_die_offset to be filled in
6395 by the caller. */
107d2387 6396
d521ce57 6397static const gdb_byte *
107d2387 6398read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6399 const gdb_byte *info_ptr,
6400 struct dwarf2_section_info *section,
6401 rcuh_kind section_kind)
107d2387
AC
6402{
6403 int signed_addr;
891d2f0b 6404 unsigned int bytes_read;
43988095
JK
6405 const char *filename = get_section_file_name (section);
6406 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6407
6408 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6409 cu_header->initial_length_size = bytes_read;
6410 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6411 info_ptr += bytes_read;
107d2387 6412 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6413 if (cu_header->version < 2 || cu_header->version > 5)
6414 error (_("Dwarf Error: wrong version in compilation unit header "
6415 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6416 cu_header->version, filename);
107d2387 6417 info_ptr += 2;
43988095
JK
6418 if (cu_header->version < 5)
6419 switch (section_kind)
6420 {
6421 case rcuh_kind::COMPILE:
6422 cu_header->unit_type = DW_UT_compile;
6423 break;
6424 case rcuh_kind::TYPE:
6425 cu_header->unit_type = DW_UT_type;
6426 break;
6427 default:
6428 internal_error (__FILE__, __LINE__,
6429 _("read_comp_unit_head: invalid section_kind"));
6430 }
6431 else
6432 {
6433 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6434 (read_1_byte (abfd, info_ptr));
6435 info_ptr += 1;
6436 switch (cu_header->unit_type)
6437 {
6438 case DW_UT_compile:
a084a2a6
AT
6439 case DW_UT_partial:
6440 case DW_UT_skeleton:
6441 case DW_UT_split_compile:
43988095
JK
6442 if (section_kind != rcuh_kind::COMPILE)
6443 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6444 "(is %s, should be %s) [in module %s]"),
6445 dwarf_unit_type_name (cu_header->unit_type),
6446 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6447 break;
6448 case DW_UT_type:
a084a2a6 6449 case DW_UT_split_type:
43988095
JK
6450 section_kind = rcuh_kind::TYPE;
6451 break;
6452 default:
6453 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6454 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6455 "[in module %s]"), cu_header->unit_type,
6456 dwarf_unit_type_name (DW_UT_compile),
6457 dwarf_unit_type_name (DW_UT_skeleton),
6458 dwarf_unit_type_name (DW_UT_split_compile),
6459 dwarf_unit_type_name (DW_UT_type),
6460 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6461 }
6462
6463 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6464 info_ptr += 1;
6465 }
9c541725
PA
6466 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6467 cu_header,
6468 &bytes_read);
613e1657 6469 info_ptr += bytes_read;
43988095
JK
6470 if (cu_header->version < 5)
6471 {
6472 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6473 info_ptr += 1;
6474 }
107d2387
AC
6475 signed_addr = bfd_get_sign_extend_vma (abfd);
6476 if (signed_addr < 0)
8e65ff28 6477 internal_error (__FILE__, __LINE__,
e2e0b3e5 6478 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6479 cu_header->signed_addr_p = signed_addr;
c764a876 6480
a084a2a6
AT
6481 bool header_has_signature = section_kind == rcuh_kind::TYPE
6482 || cu_header->unit_type == DW_UT_skeleton
6483 || cu_header->unit_type == DW_UT_split_compile;
43988095 6484
a084a2a6
AT
6485 if (header_has_signature)
6486 {
43988095
JK
6487 cu_header->signature = read_8_bytes (abfd, info_ptr);
6488 info_ptr += 8;
a084a2a6 6489 }
43988095 6490
a084a2a6
AT
6491 if (section_kind == rcuh_kind::TYPE)
6492 {
6493 LONGEST type_offset;
43988095
JK
6494 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6495 info_ptr += bytes_read;
9c541725
PA
6496 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6497 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6498 error (_("Dwarf Error: Too big type_offset in compilation unit "
6499 "header (is %s) [in module %s]"), plongest (type_offset),
6500 filename);
6501 }
6502
107d2387
AC
6503 return info_ptr;
6504}
6505
36586728
TT
6506/* Helper function that returns the proper abbrev section for
6507 THIS_CU. */
6508
6509static struct dwarf2_section_info *
6510get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6511{
6512 struct dwarf2_section_info *abbrev;
ed2dc618 6513 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6514
6515 if (this_cu->is_dwz)
ed2dc618 6516 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6517 else
6518 abbrev = &dwarf2_per_objfile->abbrev;
6519
6520 return abbrev;
6521}
6522
9ff913ba
DE
6523/* Subroutine of read_and_check_comp_unit_head and
6524 read_and_check_type_unit_head to simplify them.
6525 Perform various error checking on the header. */
6526
6527static void
ed2dc618
SM
6528error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6529 struct comp_unit_head *header,
4bdcc0c1
DE
6530 struct dwarf2_section_info *section,
6531 struct dwarf2_section_info *abbrev_section)
9ff913ba 6532{
a32a8923 6533 const char *filename = get_section_file_name (section);
9ff913ba 6534
9c541725 6535 if (to_underlying (header->abbrev_sect_off)
36586728 6536 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6537 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6538 "(offset %s + 6) [in module %s]"),
6539 sect_offset_str (header->abbrev_sect_off),
6540 sect_offset_str (header->sect_off),
9ff913ba
DE
6541 filename);
6542
9c541725 6543 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6544 avoid potential 32-bit overflow. */
9c541725 6545 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6546 > section->size)
9c541725 6547 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6548 "(offset %s + 0) [in module %s]"),
6549 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6550 filename);
6551}
6552
6553/* Read in a CU/TU header and perform some basic error checking.
6554 The contents of the header are stored in HEADER.
6555 The result is a pointer to the start of the first DIE. */
adabb602 6556
d521ce57 6557static const gdb_byte *
ed2dc618
SM
6558read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6559 struct comp_unit_head *header,
9ff913ba 6560 struct dwarf2_section_info *section,
4bdcc0c1 6561 struct dwarf2_section_info *abbrev_section,
d521ce57 6562 const gdb_byte *info_ptr,
43988095 6563 rcuh_kind section_kind)
72bf9492 6564{
d521ce57 6565 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6566
9c541725 6567 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6568
43988095 6569 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6570
9c541725 6571 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6572
ed2dc618
SM
6573 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6574 abbrev_section);
9ff913ba
DE
6575
6576 return info_ptr;
348e048f
DE
6577}
6578
f4dc4d17
DE
6579/* Fetch the abbreviation table offset from a comp or type unit header. */
6580
6581static sect_offset
ed2dc618
SM
6582read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6583 struct dwarf2_section_info *section,
9c541725 6584 sect_offset sect_off)
f4dc4d17 6585{
a32a8923 6586 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6587 const gdb_byte *info_ptr;
ac298888 6588 unsigned int initial_length_size, offset_size;
43988095 6589 uint16_t version;
f4dc4d17
DE
6590
6591 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6592 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6593 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6594 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6595 info_ptr += initial_length_size;
6596
6597 version = read_2_bytes (abfd, info_ptr);
6598 info_ptr += 2;
6599 if (version >= 5)
6600 {
6601 /* Skip unit type and address size. */
6602 info_ptr += 2;
6603 }
6604
9c541725 6605 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6606}
6607
aaa75496
JB
6608/* Allocate a new partial symtab for file named NAME and mark this new
6609 partial symtab as being an include of PST. */
6610
6611static void
d521ce57 6612dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6613 struct objfile *objfile)
6614{
6615 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6616
fbd9ab74
JK
6617 if (!IS_ABSOLUTE_PATH (subpst->filename))
6618 {
6619 /* It shares objfile->objfile_obstack. */
6620 subpst->dirname = pst->dirname;
6621 }
6622
a9342b62 6623 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6624 subpst->dependencies[0] = pst;
6625 subpst->number_of_dependencies = 1;
6626
aaa75496 6627 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6628
6629 /* No private part is necessary for include psymtabs. This property
6630 can be used to differentiate between such include psymtabs and
10b3939b 6631 the regular ones. */
58a9656e 6632 subpst->read_symtab_private = NULL;
aaa75496
JB
6633}
6634
6635/* Read the Line Number Program data and extract the list of files
6636 included by the source file represented by PST. Build an include
d85a05f0 6637 partial symtab for each of these included files. */
aaa75496
JB
6638
6639static void
6640dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6641 struct die_info *die,
6642 struct partial_symtab *pst)
aaa75496 6643{
fff8551c 6644 line_header_up lh;
d85a05f0 6645 struct attribute *attr;
aaa75496 6646
d85a05f0
DJ
6647 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6648 if (attr)
9c541725 6649 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6650 if (lh == NULL)
6651 return; /* No linetable, so no includes. */
6652
79748972
TT
6653 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6654 that we pass in the raw text_low here; that is ok because we're
6655 only decoding the line table to make include partial symtabs, and
6656 so the addresses aren't really used. */
4ae976d1 6657 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6658 pst->raw_text_low (), 1);
aaa75496
JB
6659}
6660
348e048f 6661static hashval_t
52dc124a 6662hash_signatured_type (const void *item)
348e048f 6663{
9a3c8263
SM
6664 const struct signatured_type *sig_type
6665 = (const struct signatured_type *) item;
9a619af0 6666
348e048f 6667 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6668 return sig_type->signature;
348e048f
DE
6669}
6670
6671static int
52dc124a 6672eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6673{
9a3c8263
SM
6674 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6675 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6676
348e048f
DE
6677 return lhs->signature == rhs->signature;
6678}
6679
1fd400ff
TT
6680/* Allocate a hash table for signatured types. */
6681
6682static htab_t
673bfd45 6683allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6684{
6685 return htab_create_alloc_ex (41,
52dc124a
DE
6686 hash_signatured_type,
6687 eq_signatured_type,
1fd400ff
TT
6688 NULL,
6689 &objfile->objfile_obstack,
6690 hashtab_obstack_allocate,
6691 dummy_obstack_deallocate);
6692}
6693
d467dd73 6694/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6695
6696static int
d467dd73 6697add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6698{
9a3c8263 6699 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6700 std::vector<signatured_type *> *all_type_units
6701 = (std::vector<signatured_type *> *) datum;
1fd400ff 6702
b2bdb8cf 6703 all_type_units->push_back (sigt);
1fd400ff
TT
6704
6705 return 1;
6706}
6707
78d4d2c5 6708/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6709 and fill them into TYPES_HTAB. It will process only type units,
6710 therefore DW_UT_type. */
c88ee1f0 6711
78d4d2c5 6712static void
ed2dc618
SM
6713create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6714 struct dwo_file *dwo_file,
43988095
JK
6715 dwarf2_section_info *section, htab_t &types_htab,
6716 rcuh_kind section_kind)
348e048f 6717{
3019eac3 6718 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6719 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6720 bfd *abfd;
6721 const gdb_byte *info_ptr, *end_ptr;
348e048f 6722
4bdcc0c1
DE
6723 abbrev_section = (dwo_file != NULL
6724 ? &dwo_file->sections.abbrev
6725 : &dwarf2_per_objfile->abbrev);
6726
b4f54984 6727 if (dwarf_read_debug)
43988095
JK
6728 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6729 get_section_name (section),
a32a8923 6730 get_section_file_name (abbrev_section));
09406207 6731
78d4d2c5
JK
6732 dwarf2_read_section (objfile, section);
6733 info_ptr = section->buffer;
348e048f 6734
78d4d2c5
JK
6735 if (info_ptr == NULL)
6736 return;
348e048f 6737
78d4d2c5
JK
6738 /* We can't set abfd until now because the section may be empty or
6739 not present, in which case the bfd is unknown. */
6740 abfd = get_section_bfd_owner (section);
348e048f 6741
78d4d2c5
JK
6742 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6743 because we don't need to read any dies: the signature is in the
6744 header. */
3019eac3 6745
78d4d2c5
JK
6746 end_ptr = info_ptr + section->size;
6747 while (info_ptr < end_ptr)
6748 {
78d4d2c5
JK
6749 struct signatured_type *sig_type;
6750 struct dwo_unit *dwo_tu;
6751 void **slot;
6752 const gdb_byte *ptr = info_ptr;
6753 struct comp_unit_head header;
6754 unsigned int length;
8b70b953 6755
9c541725 6756 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6757
a49dd8dd
JK
6758 /* Initialize it due to a false compiler warning. */
6759 header.signature = -1;
9c541725 6760 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6761
78d4d2c5
JK
6762 /* We need to read the type's signature in order to build the hash
6763 table, but we don't need anything else just yet. */
348e048f 6764
ed2dc618 6765 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6766 abbrev_section, ptr, section_kind);
348e048f 6767
78d4d2c5 6768 length = get_cu_length (&header);
6caca83c 6769
78d4d2c5
JK
6770 /* Skip dummy type units. */
6771 if (ptr >= info_ptr + length
43988095
JK
6772 || peek_abbrev_code (abfd, ptr) == 0
6773 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6774 {
6775 info_ptr += length;
6776 continue;
6777 }
dee91e82 6778
78d4d2c5
JK
6779 if (types_htab == NULL)
6780 {
6781 if (dwo_file)
6782 types_htab = allocate_dwo_unit_table (objfile);
6783 else
6784 types_htab = allocate_signatured_type_table (objfile);
6785 }
8b70b953 6786
78d4d2c5
JK
6787 if (dwo_file)
6788 {
6789 sig_type = NULL;
6790 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6791 struct dwo_unit);
6792 dwo_tu->dwo_file = dwo_file;
43988095 6793 dwo_tu->signature = header.signature;
9c541725 6794 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6795 dwo_tu->section = section;
9c541725 6796 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6797 dwo_tu->length = length;
6798 }
6799 else
6800 {
6801 /* N.B.: type_offset is not usable if this type uses a DWO file.
6802 The real type_offset is in the DWO file. */
6803 dwo_tu = NULL;
6804 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6805 struct signatured_type);
43988095 6806 sig_type->signature = header.signature;
9c541725 6807 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6808 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6809 sig_type->per_cu.is_debug_types = 1;
6810 sig_type->per_cu.section = section;
9c541725 6811 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6812 sig_type->per_cu.length = length;
6813 }
6814
6815 slot = htab_find_slot (types_htab,
6816 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6817 INSERT);
6818 gdb_assert (slot != NULL);
6819 if (*slot != NULL)
6820 {
9c541725 6821 sect_offset dup_sect_off;
0349ea22 6822
3019eac3
DE
6823 if (dwo_file)
6824 {
78d4d2c5
JK
6825 const struct dwo_unit *dup_tu
6826 = (const struct dwo_unit *) *slot;
6827
9c541725 6828 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6829 }
6830 else
6831 {
78d4d2c5
JK
6832 const struct signatured_type *dup_tu
6833 = (const struct signatured_type *) *slot;
6834
9c541725 6835 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6836 }
8b70b953 6837
b98664d3 6838 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6839 " the entry at offset %s, signature %s"),
6840 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6841 hex_string (header.signature));
78d4d2c5
JK
6842 }
6843 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6844
78d4d2c5 6845 if (dwarf_read_debug > 1)
9d8780f0
SM
6846 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6847 sect_offset_str (sect_off),
43988095 6848 hex_string (header.signature));
3019eac3 6849
78d4d2c5
JK
6850 info_ptr += length;
6851 }
6852}
3019eac3 6853
78d4d2c5
JK
6854/* Create the hash table of all entries in the .debug_types
6855 (or .debug_types.dwo) section(s).
6856 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6857 otherwise it is NULL.
b3c8eb43 6858
78d4d2c5 6859 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6860
78d4d2c5 6861 Note: This function processes DWO files only, not DWP files. */
348e048f 6862
78d4d2c5 6863static void
ed2dc618
SM
6864create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6865 struct dwo_file *dwo_file,
fd5866f6 6866 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6867 htab_t &types_htab)
6868{
fd5866f6
SM
6869 for (dwarf2_section_info &section : type_sections)
6870 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6871 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6872}
6873
6874/* Create the hash table of all entries in the .debug_types section,
6875 and initialize all_type_units.
6876 The result is zero if there is an error (e.g. missing .debug_types section),
6877 otherwise non-zero. */
6878
6879static int
ed2dc618 6880create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6881{
78d4d2c5 6882 htab_t types_htab = NULL;
3019eac3 6883
ed2dc618
SM
6884 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6885 &dwarf2_per_objfile->info, types_htab,
43988095 6886 rcuh_kind::COMPILE);
ed2dc618
SM
6887 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6888 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6889 if (types_htab == NULL)
6890 {
6891 dwarf2_per_objfile->signatured_types = NULL;
6892 return 0;
6893 }
6894
348e048f
DE
6895 dwarf2_per_objfile->signatured_types = types_htab;
6896
b2bdb8cf
SM
6897 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6898 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6899
6900 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6901 &dwarf2_per_objfile->all_type_units);
1fd400ff 6902
348e048f
DE
6903 return 1;
6904}
6905
6aa5f3a6
DE
6906/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6907 If SLOT is non-NULL, it is the entry to use in the hash table.
6908 Otherwise we find one. */
6909
6910static struct signatured_type *
ed2dc618
SM
6911add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6912 void **slot)
6aa5f3a6
DE
6913{
6914 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6915
b2bdb8cf
SM
6916 if (dwarf2_per_objfile->all_type_units.size ()
6917 == dwarf2_per_objfile->all_type_units.capacity ())
6918 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6919
b2bdb8cf
SM
6920 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6921 struct signatured_type);
6922
6923 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6924 sig_type->signature = sig;
6925 sig_type->per_cu.is_debug_types = 1;
6926 if (dwarf2_per_objfile->using_index)
6927 {
6928 sig_type->per_cu.v.quick =
6929 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6930 struct dwarf2_per_cu_quick_data);
6931 }
6932
6933 if (slot == NULL)
6934 {
6935 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6936 sig_type, INSERT);
6937 }
6938 gdb_assert (*slot == NULL);
6939 *slot = sig_type;
6940 /* The rest of sig_type must be filled in by the caller. */
6941 return sig_type;
6942}
6943
a2ce51a0
DE
6944/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6945 Fill in SIG_ENTRY with DWO_ENTRY. */
6946
6947static void
ed2dc618 6948fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6949 struct signatured_type *sig_entry,
6950 struct dwo_unit *dwo_entry)
6951{
7ee85ab1 6952 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6953 gdb_assert (! sig_entry->per_cu.queued);
6954 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6955 if (dwarf2_per_objfile->using_index)
6956 {
6957 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6958 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6959 }
6960 else
6961 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6962 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6963 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6964 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6965 gdb_assert (sig_entry->dwo_unit == NULL);
6966
6967 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6968 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6969 sig_entry->per_cu.length = dwo_entry->length;
6970 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6971 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6972 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6973 sig_entry->dwo_unit = dwo_entry;
6974}
6975
6976/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6977 If we haven't read the TU yet, create the signatured_type data structure
6978 for a TU to be read in directly from a DWO file, bypassing the stub.
6979 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6980 using .gdb_index, then when reading a CU we want to stay in the DWO file
6981 containing that CU. Otherwise we could end up reading several other DWO
6982 files (due to comdat folding) to process the transitive closure of all the
6983 mentioned TUs, and that can be slow. The current DWO file will have every
6984 type signature that it needs.
a2ce51a0
DE
6985 We only do this for .gdb_index because in the psymtab case we already have
6986 to read all the DWOs to build the type unit groups. */
6987
6988static struct signatured_type *
6989lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6990{
518817b3
SM
6991 struct dwarf2_per_objfile *dwarf2_per_objfile
6992 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6993 struct objfile *objfile = dwarf2_per_objfile->objfile;
6994 struct dwo_file *dwo_file;
6995 struct dwo_unit find_dwo_entry, *dwo_entry;
6996 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6997 void **slot;
a2ce51a0
DE
6998
6999 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7000
6aa5f3a6
DE
7001 /* If TU skeletons have been removed then we may not have read in any
7002 TUs yet. */
7003 if (dwarf2_per_objfile->signatured_types == NULL)
7004 {
7005 dwarf2_per_objfile->signatured_types
7006 = allocate_signatured_type_table (objfile);
7007 }
a2ce51a0
DE
7008
7009 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7010 Use the global signatured_types array to do our own comdat-folding
7011 of types. If this is the first time we're reading this TU, and
7012 the TU has an entry in .gdb_index, replace the recorded data from
7013 .gdb_index with this TU. */
a2ce51a0 7014
a2ce51a0 7015 find_sig_entry.signature = sig;
6aa5f3a6
DE
7016 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7017 &find_sig_entry, INSERT);
9a3c8263 7018 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7019
7020 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7021 read. Don't reassign the global entry to point to this DWO if that's
7022 the case. Also note that if the TU is already being read, it may not
7023 have come from a DWO, the program may be a mix of Fission-compiled
7024 code and non-Fission-compiled code. */
7025
7026 /* Have we already tried to read this TU?
7027 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7028 needn't exist in the global table yet). */
7029 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7030 return sig_entry;
7031
6aa5f3a6
DE
7032 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7033 dwo_unit of the TU itself. */
7034 dwo_file = cu->dwo_unit->dwo_file;
7035
a2ce51a0
DE
7036 /* Ok, this is the first time we're reading this TU. */
7037 if (dwo_file->tus == NULL)
7038 return NULL;
7039 find_dwo_entry.signature = sig;
9a3c8263 7040 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7041 if (dwo_entry == NULL)
7042 return NULL;
7043
6aa5f3a6
DE
7044 /* If the global table doesn't have an entry for this TU, add one. */
7045 if (sig_entry == NULL)
ed2dc618 7046 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7047
ed2dc618 7048 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7049 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7050 return sig_entry;
7051}
7052
a2ce51a0
DE
7053/* Subroutine of lookup_signatured_type.
7054 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7055 then try the DWP file. If the TU stub (skeleton) has been removed then
7056 it won't be in .gdb_index. */
a2ce51a0
DE
7057
7058static struct signatured_type *
7059lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7060{
518817b3
SM
7061 struct dwarf2_per_objfile *dwarf2_per_objfile
7062 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7063 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7064 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7065 struct dwo_unit *dwo_entry;
7066 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7067 void **slot;
a2ce51a0
DE
7068
7069 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7070 gdb_assert (dwp_file != NULL);
7071
6aa5f3a6
DE
7072 /* If TU skeletons have been removed then we may not have read in any
7073 TUs yet. */
7074 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7075 {
6aa5f3a6
DE
7076 dwarf2_per_objfile->signatured_types
7077 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7078 }
7079
6aa5f3a6
DE
7080 find_sig_entry.signature = sig;
7081 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7082 &find_sig_entry, INSERT);
9a3c8263 7083 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7084
7085 /* Have we already tried to read this TU?
7086 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7087 needn't exist in the global table yet). */
7088 if (sig_entry != NULL)
7089 return sig_entry;
7090
a2ce51a0
DE
7091 if (dwp_file->tus == NULL)
7092 return NULL;
ed2dc618 7093 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7094 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7095 if (dwo_entry == NULL)
7096 return NULL;
7097
ed2dc618
SM
7098 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7099 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7100
a2ce51a0
DE
7101 return sig_entry;
7102}
7103
380bca97 7104/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7105 Returns NULL if signature SIG is not present in the table.
7106 It is up to the caller to complain about this. */
348e048f
DE
7107
7108static struct signatured_type *
a2ce51a0 7109lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7110{
518817b3
SM
7111 struct dwarf2_per_objfile *dwarf2_per_objfile
7112 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7113
a2ce51a0
DE
7114 if (cu->dwo_unit
7115 && dwarf2_per_objfile->using_index)
7116 {
7117 /* We're in a DWO/DWP file, and we're using .gdb_index.
7118 These cases require special processing. */
ed2dc618 7119 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7120 return lookup_dwo_signatured_type (cu, sig);
7121 else
7122 return lookup_dwp_signatured_type (cu, sig);
7123 }
7124 else
7125 {
7126 struct signatured_type find_entry, *entry;
348e048f 7127
a2ce51a0
DE
7128 if (dwarf2_per_objfile->signatured_types == NULL)
7129 return NULL;
7130 find_entry.signature = sig;
9a3c8263
SM
7131 entry = ((struct signatured_type *)
7132 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7133 return entry;
7134 }
348e048f 7135}
42e7ad6c
DE
7136\f
7137/* Low level DIE reading support. */
348e048f 7138
d85a05f0
DJ
7139/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7140
7141static void
7142init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7143 struct dwarf2_cu *cu,
3019eac3 7144 struct dwarf2_section_info *section,
685af9cd
TT
7145 struct dwo_file *dwo_file,
7146 struct abbrev_table *abbrev_table)
d85a05f0 7147{
fceca515 7148 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7149 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7150 reader->cu = cu;
3019eac3 7151 reader->dwo_file = dwo_file;
dee91e82
DE
7152 reader->die_section = section;
7153 reader->buffer = section->buffer;
f664829e 7154 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7155 reader->comp_dir = NULL;
685af9cd 7156 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7157}
7158
b0c7bfa9
DE
7159/* Subroutine of init_cutu_and_read_dies to simplify it.
7160 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7161 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7162 already.
7163
7164 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7165 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7166 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7167 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7168 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7169 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7170 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7171 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7172 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7173 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7174 kept around for at least as long as *RESULT_READER.
7175
b0c7bfa9
DE
7176 The result is non-zero if a valid (non-dummy) DIE was found. */
7177
7178static int
7179read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7180 struct dwo_unit *dwo_unit,
b0c7bfa9 7181 struct die_info *stub_comp_unit_die,
a2ce51a0 7182 const char *stub_comp_dir,
b0c7bfa9 7183 struct die_reader_specs *result_reader,
d521ce57 7184 const gdb_byte **result_info_ptr,
b0c7bfa9 7185 struct die_info **result_comp_unit_die,
685af9cd
TT
7186 int *result_has_children,
7187 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7188{
ed2dc618 7189 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7190 struct objfile *objfile = dwarf2_per_objfile->objfile;
7191 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7192 bfd *abfd;
d521ce57 7193 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7194 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7195 int i,num_extra_attrs;
7196 struct dwarf2_section_info *dwo_abbrev_section;
7197 struct attribute *attr;
7198 struct die_info *comp_unit_die;
7199
b0aeadb3
DE
7200 /* At most one of these may be provided. */
7201 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7202
b0c7bfa9
DE
7203 /* These attributes aren't processed until later:
7204 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7205 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7206 referenced later. However, these attributes are found in the stub
7207 which we won't have later. In order to not impose this complication
7208 on the rest of the code, we read them here and copy them to the
7209 DWO CU/TU die. */
b0c7bfa9
DE
7210
7211 stmt_list = NULL;
7212 low_pc = NULL;
7213 high_pc = NULL;
7214 ranges = NULL;
7215 comp_dir = NULL;
7216
7217 if (stub_comp_unit_die != NULL)
7218 {
7219 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7220 DWO file. */
7221 if (! this_cu->is_debug_types)
7222 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7223 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7224 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7225 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7226 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7227
7228 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7229 We need the value before we can process DW_FORM_GNU_addr_index
7230 or DW_FORM_addrx. */
b0c7bfa9
DE
7231 cu->addr_base = 0;
7232 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7233 if (attr)
7234 cu->addr_base = DW_UNSND (attr);
7235
7236 /* There should be a DW_AT_ranges_base attribute here (if needed).
7237 We need the value before we can process DW_AT_ranges. */
7238 cu->ranges_base = 0;
7239 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7240 if (attr)
7241 cu->ranges_base = DW_UNSND (attr);
7242 }
a2ce51a0
DE
7243 else if (stub_comp_dir != NULL)
7244 {
7245 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7246 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7247 comp_dir->name = DW_AT_comp_dir;
7248 comp_dir->form = DW_FORM_string;
7249 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7250 DW_STRING (comp_dir) = stub_comp_dir;
7251 }
b0c7bfa9
DE
7252
7253 /* Set up for reading the DWO CU/TU. */
7254 cu->dwo_unit = dwo_unit;
685af9cd 7255 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7256 dwarf2_read_section (objfile, section);
a32a8923 7257 abfd = get_section_bfd_owner (section);
9c541725
PA
7258 begin_info_ptr = info_ptr = (section->buffer
7259 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7260 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7261
7262 if (this_cu->is_debug_types)
7263 {
b0c7bfa9
DE
7264 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7265
ed2dc618
SM
7266 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7267 &cu->header, section,
b0c7bfa9 7268 dwo_abbrev_section,
43988095 7269 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7270 /* This is not an assert because it can be caused by bad debug info. */
43988095 7271 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7272 {
7273 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7274 " TU at offset %s [in module %s]"),
a2ce51a0 7275 hex_string (sig_type->signature),
43988095 7276 hex_string (cu->header.signature),
9d8780f0 7277 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7278 bfd_get_filename (abfd));
7279 }
9c541725 7280 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7281 /* For DWOs coming from DWP files, we don't know the CU length
7282 nor the type's offset in the TU until now. */
7283 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7284 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7285
7286 /* Establish the type offset that can be used to lookup the type.
7287 For DWO files, we don't know it until now. */
9c541725
PA
7288 sig_type->type_offset_in_section
7289 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7290 }
7291 else
7292 {
ed2dc618
SM
7293 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7294 &cu->header, section,
b0c7bfa9 7295 dwo_abbrev_section,
43988095 7296 info_ptr, rcuh_kind::COMPILE);
9c541725 7297 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7298 /* For DWOs coming from DWP files, we don't know the CU length
7299 until now. */
7300 dwo_unit->length = get_cu_length (&cu->header);
7301 }
7302
685af9cd
TT
7303 *result_dwo_abbrev_table
7304 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7305 cu->header.abbrev_sect_off);
7306 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7307 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7308
7309 /* Read in the die, but leave space to copy over the attributes
7310 from the stub. This has the benefit of simplifying the rest of
7311 the code - all the work to maintain the illusion of a single
7312 DW_TAG_{compile,type}_unit DIE is done here. */
7313 num_extra_attrs = ((stmt_list != NULL)
7314 + (low_pc != NULL)
7315 + (high_pc != NULL)
7316 + (ranges != NULL)
7317 + (comp_dir != NULL));
7318 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7319 result_has_children, num_extra_attrs);
7320
7321 /* Copy over the attributes from the stub to the DIE we just read in. */
7322 comp_unit_die = *result_comp_unit_die;
7323 i = comp_unit_die->num_attrs;
7324 if (stmt_list != NULL)
7325 comp_unit_die->attrs[i++] = *stmt_list;
7326 if (low_pc != NULL)
7327 comp_unit_die->attrs[i++] = *low_pc;
7328 if (high_pc != NULL)
7329 comp_unit_die->attrs[i++] = *high_pc;
7330 if (ranges != NULL)
7331 comp_unit_die->attrs[i++] = *ranges;
7332 if (comp_dir != NULL)
7333 comp_unit_die->attrs[i++] = *comp_dir;
7334 comp_unit_die->num_attrs += num_extra_attrs;
7335
b4f54984 7336 if (dwarf_die_debug)
bf6af496
DE
7337 {
7338 fprintf_unfiltered (gdb_stdlog,
7339 "Read die from %s@0x%x of %s:\n",
a32a8923 7340 get_section_name (section),
bf6af496
DE
7341 (unsigned) (begin_info_ptr - section->buffer),
7342 bfd_get_filename (abfd));
b4f54984 7343 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7344 }
7345
a2ce51a0
DE
7346 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7347 TUs by skipping the stub and going directly to the entry in the DWO file.
7348 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7349 to get it via circuitous means. Blech. */
7350 if (comp_dir != NULL)
7351 result_reader->comp_dir = DW_STRING (comp_dir);
7352
b0c7bfa9
DE
7353 /* Skip dummy compilation units. */
7354 if (info_ptr >= begin_info_ptr + dwo_unit->length
7355 || peek_abbrev_code (abfd, info_ptr) == 0)
7356 return 0;
7357
7358 *result_info_ptr = info_ptr;
7359 return 1;
7360}
7361
a084a2a6
AT
7362/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7363 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7364 signature is part of the header. */
7365static gdb::optional<ULONGEST>
7366lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7367{
7368 if (cu->header.version >= 5)
7369 return cu->header.signature;
7370 struct attribute *attr;
7371 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7372 if (attr == nullptr)
7373 return gdb::optional<ULONGEST> ();
7374 return DW_UNSND (attr);
7375}
7376
b0c7bfa9
DE
7377/* Subroutine of init_cutu_and_read_dies to simplify it.
7378 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7379 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7380
7381static struct dwo_unit *
7382lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7383 struct die_info *comp_unit_die)
7384{
7385 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7386 struct dwo_unit *dwo_unit;
7387 const char *comp_dir, *dwo_name;
7388
a2ce51a0
DE
7389 gdb_assert (cu != NULL);
7390
b0c7bfa9 7391 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7392 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7393 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7394
7395 if (this_cu->is_debug_types)
7396 {
7397 struct signatured_type *sig_type;
7398
7399 /* Since this_cu is the first member of struct signatured_type,
7400 we can go from a pointer to one to a pointer to the other. */
7401 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7402 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7403 }
7404 else
7405 {
a084a2a6
AT
7406 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7407 if (!signature.has_value ())
b0c7bfa9
DE
7408 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7409 " [in module %s]"),
e3b94546 7410 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7411 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7412 *signature);
b0c7bfa9
DE
7413 }
7414
b0c7bfa9
DE
7415 return dwo_unit;
7416}
7417
a2ce51a0 7418/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7419 See it for a description of the parameters.
fcd3b13d 7420 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7421
7422static void
6aa5f3a6
DE
7423init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7424 int use_existing_cu, int keep,
a2ce51a0
DE
7425 die_reader_func_ftype *die_reader_func,
7426 void *data)
7427{
fcd3b13d 7428 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7429 struct signatured_type *sig_type;
a2ce51a0
DE
7430 struct die_reader_specs reader;
7431 const gdb_byte *info_ptr;
7432 struct die_info *comp_unit_die;
7433 int has_children;
ed2dc618 7434 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7435
7436 /* Verify we can do the following downcast, and that we have the
7437 data we need. */
7438 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7439 sig_type = (struct signatured_type *) this_cu;
7440 gdb_assert (sig_type->dwo_unit != NULL);
7441
6aa5f3a6
DE
7442 if (use_existing_cu && this_cu->cu != NULL)
7443 {
7444 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7445 /* There's no need to do the rereading_dwo_cu handling that
7446 init_cutu_and_read_dies does since we don't read the stub. */
7447 }
7448 else
7449 {
7450 /* If !use_existing_cu, this_cu->cu must be NULL. */
7451 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7452 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7453 }
7454
7455 /* A future optimization, if needed, would be to use an existing
7456 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7457 could share abbrev tables. */
a2ce51a0 7458
685af9cd
TT
7459 /* The abbreviation table used by READER, this must live at least as long as
7460 READER. */
7461 abbrev_table_up dwo_abbrev_table;
7462
a2ce51a0 7463 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7464 NULL /* stub_comp_unit_die */,
7465 sig_type->dwo_unit->dwo_file->comp_dir,
7466 &reader, &info_ptr,
685af9cd
TT
7467 &comp_unit_die, &has_children,
7468 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7469 {
7470 /* Dummy die. */
a2ce51a0
DE
7471 return;
7472 }
7473
7474 /* All the "real" work is done here. */
7475 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7476
6aa5f3a6 7477 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7478 but the alternative is making the latter more complex.
7479 This function is only for the special case of using DWO files directly:
7480 no point in overly complicating the general case just to handle this. */
fcd3b13d 7481 if (new_cu != NULL && keep)
a2ce51a0 7482 {
fcd3b13d
SM
7483 /* Link this CU into read_in_chain. */
7484 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7485 dwarf2_per_objfile->read_in_chain = this_cu;
7486 /* The chain owns it now. */
7487 new_cu.release ();
a2ce51a0 7488 }
a2ce51a0
DE
7489}
7490
fd820528 7491/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7492 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7493
f4dc4d17
DE
7494 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7495 Otherwise the table specified in the comp unit header is read in and used.
7496 This is an optimization for when we already have the abbrev table.
7497
dee91e82
DE
7498 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7499 Otherwise, a new CU is allocated with xmalloc.
7500
7501 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7502 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7503
7504 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7505 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7506
70221824 7507static void
fd820528 7508init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7509 struct abbrev_table *abbrev_table,
fd820528 7510 int use_existing_cu, int keep,
58f0c718 7511 bool skip_partial,
fd820528
DE
7512 die_reader_func_ftype *die_reader_func,
7513 void *data)
c906108c 7514{
ed2dc618 7515 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7516 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7517 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7518 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7519 struct dwarf2_cu *cu;
d521ce57 7520 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7521 struct die_reader_specs reader;
d85a05f0 7522 struct die_info *comp_unit_die;
dee91e82 7523 int has_children;
dee91e82 7524 struct signatured_type *sig_type = NULL;
4bdcc0c1 7525 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7526 /* Non-zero if CU currently points to a DWO file and we need to
7527 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7528 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7529 int rereading_dwo_cu = 0;
c906108c 7530
b4f54984 7531 if (dwarf_die_debug)
9d8780f0 7532 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7533 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7534 sect_offset_str (this_cu->sect_off));
09406207 7535
dee91e82
DE
7536 if (use_existing_cu)
7537 gdb_assert (keep);
23745b47 7538
a2ce51a0
DE
7539 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7540 file (instead of going through the stub), short-circuit all of this. */
7541 if (this_cu->reading_dwo_directly)
7542 {
7543 /* Narrow down the scope of possibilities to have to understand. */
7544 gdb_assert (this_cu->is_debug_types);
7545 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7546 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7547 die_reader_func, data);
a2ce51a0
DE
7548 return;
7549 }
7550
dee91e82
DE
7551 /* This is cheap if the section is already read in. */
7552 dwarf2_read_section (objfile, section);
7553
9c541725 7554 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7555
7556 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7557
fcd3b13d 7558 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7559 if (use_existing_cu && this_cu->cu != NULL)
7560 {
7561 cu = this_cu->cu;
42e7ad6c
DE
7562 /* If this CU is from a DWO file we need to start over, we need to
7563 refetch the attributes from the skeleton CU.
7564 This could be optimized by retrieving those attributes from when we
7565 were here the first time: the previous comp_unit_die was stored in
7566 comp_unit_obstack. But there's no data yet that we need this
7567 optimization. */
7568 if (cu->dwo_unit != NULL)
7569 rereading_dwo_cu = 1;
dee91e82
DE
7570 }
7571 else
7572 {
7573 /* If !use_existing_cu, this_cu->cu must be NULL. */
7574 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7575 new_cu.reset (new dwarf2_cu (this_cu));
7576 cu = new_cu.get ();
42e7ad6c 7577 }
dee91e82 7578
b0c7bfa9 7579 /* Get the header. */
9c541725 7580 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7581 {
7582 /* We already have the header, there's no need to read it in again. */
9c541725 7583 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7584 }
7585 else
7586 {
3019eac3 7587 if (this_cu->is_debug_types)
dee91e82 7588 {
ed2dc618
SM
7589 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7590 &cu->header, section,
4bdcc0c1 7591 abbrev_section, info_ptr,
43988095 7592 rcuh_kind::TYPE);
dee91e82 7593
42e7ad6c
DE
7594 /* Since per_cu is the first member of struct signatured_type,
7595 we can go from a pointer to one to a pointer to the other. */
7596 sig_type = (struct signatured_type *) this_cu;
43988095 7597 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7598 gdb_assert (sig_type->type_offset_in_tu
7599 == cu->header.type_cu_offset_in_tu);
7600 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7601
42e7ad6c
DE
7602 /* LENGTH has not been set yet for type units if we're
7603 using .gdb_index. */
1ce1cefd 7604 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7605
7606 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7607 sig_type->type_offset_in_section =
7608 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7609
7610 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7611 }
7612 else
7613 {
ed2dc618
SM
7614 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7615 &cu->header, section,
4bdcc0c1 7616 abbrev_section,
43988095
JK
7617 info_ptr,
7618 rcuh_kind::COMPILE);
dee91e82 7619
9c541725 7620 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7621 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7622 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7623 }
7624 }
10b3939b 7625
6caca83c 7626 /* Skip dummy compilation units. */
dee91e82 7627 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7628 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7629 return;
6caca83c 7630
433df2d4
DE
7631 /* If we don't have them yet, read the abbrevs for this compilation unit.
7632 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7633 done (own the table through ABBREV_TABLE_HOLDER). */
7634 abbrev_table_up abbrev_table_holder;
f4dc4d17 7635 if (abbrev_table != NULL)
685af9cd
TT
7636 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7637 else
f4dc4d17 7638 {
685af9cd
TT
7639 abbrev_table_holder
7640 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7641 cu->header.abbrev_sect_off);
7642 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7643 }
af703f96 7644
dee91e82 7645 /* Read the top level CU/TU die. */
685af9cd 7646 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7647 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7648
58f0c718
TT
7649 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7650 return;
7651
b0c7bfa9 7652 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7653 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7654 table from the DWO file and pass the ownership over to us. It will be
7655 referenced from READER, so we must make sure to free it after we're done
7656 with READER.
7657
b0c7bfa9
DE
7658 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7659 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7660 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7661 abbrev_table_up dwo_abbrev_table;
a084a2a6 7662 if (dwo_name != nullptr)
3019eac3 7663 {
3019eac3 7664 struct dwo_unit *dwo_unit;
b0c7bfa9 7665 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7666
7667 if (has_children)
6a506a2d 7668 {
b98664d3 7669 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7670 " has children (offset %s) [in module %s]"),
7671 sect_offset_str (this_cu->sect_off),
7672 bfd_get_filename (abfd));
6a506a2d 7673 }
b0c7bfa9 7674 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7675 if (dwo_unit != NULL)
3019eac3 7676 {
6a506a2d 7677 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7678 comp_unit_die, NULL,
6a506a2d 7679 &reader, &info_ptr,
685af9cd
TT
7680 &dwo_comp_unit_die, &has_children,
7681 &dwo_abbrev_table) == 0)
6a506a2d
DE
7682 {
7683 /* Dummy die. */
6a506a2d
DE
7684 return;
7685 }
7686 comp_unit_die = dwo_comp_unit_die;
7687 }
7688 else
7689 {
7690 /* Yikes, we couldn't find the rest of the DIE, we only have
7691 the stub. A complaint has already been logged. There's
7692 not much more we can do except pass on the stub DIE to
7693 die_reader_func. We don't want to throw an error on bad
7694 debug info. */
3019eac3
DE
7695 }
7696 }
7697
b0c7bfa9 7698 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7699 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7700
b0c7bfa9 7701 /* Done, clean up. */
fcd3b13d 7702 if (new_cu != NULL && keep)
348e048f 7703 {
fcd3b13d
SM
7704 /* Link this CU into read_in_chain. */
7705 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7706 dwarf2_per_objfile->read_in_chain = this_cu;
7707 /* The chain owns it now. */
7708 new_cu.release ();
348e048f 7709 }
dee91e82
DE
7710}
7711
33e80786
DE
7712/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7713 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7714 to have already done the lookup to find the DWO file).
dee91e82
DE
7715
7716 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7717 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7718
7719 We fill in THIS_CU->length.
7720
7721 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7722 linker) then DIE_READER_FUNC will not get called.
7723
7724 THIS_CU->cu is always freed when done.
3019eac3
DE
7725 This is done in order to not leave THIS_CU->cu in a state where we have
7726 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7727
7728static void
7729init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7730 struct dwo_file *dwo_file,
dee91e82
DE
7731 die_reader_func_ftype *die_reader_func,
7732 void *data)
7733{
ed2dc618 7734 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7735 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7736 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7737 bfd *abfd = get_section_bfd_owner (section);
33e80786 7738 struct dwarf2_section_info *abbrev_section;
d521ce57 7739 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7740 struct die_reader_specs reader;
dee91e82
DE
7741 struct die_info *comp_unit_die;
7742 int has_children;
7743
b4f54984 7744 if (dwarf_die_debug)
9d8780f0 7745 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7746 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7747 sect_offset_str (this_cu->sect_off));
09406207 7748
dee91e82
DE
7749 gdb_assert (this_cu->cu == NULL);
7750
33e80786
DE
7751 abbrev_section = (dwo_file != NULL
7752 ? &dwo_file->sections.abbrev
7753 : get_abbrev_section_for_cu (this_cu));
7754
dee91e82
DE
7755 /* This is cheap if the section is already read in. */
7756 dwarf2_read_section (objfile, section);
7757
fcd3b13d 7758 struct dwarf2_cu cu (this_cu);
dee91e82 7759
9c541725 7760 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7761 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7762 &cu.header, section,
4bdcc0c1 7763 abbrev_section, info_ptr,
43988095
JK
7764 (this_cu->is_debug_types
7765 ? rcuh_kind::TYPE
7766 : rcuh_kind::COMPILE));
dee91e82 7767
1ce1cefd 7768 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7769
7770 /* Skip dummy compilation units. */
7771 if (info_ptr >= begin_info_ptr + this_cu->length
7772 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7773 return;
72bf9492 7774
685af9cd
TT
7775 abbrev_table_up abbrev_table
7776 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7777 cu.header.abbrev_sect_off);
dee91e82 7778
685af9cd 7779 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7780 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7781
7782 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7783}
7784
3019eac3
DE
7785/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7786 does not lookup the specified DWO file.
7787 This cannot be used to read DWO files.
dee91e82
DE
7788
7789 THIS_CU->cu is always freed when done.
3019eac3
DE
7790 This is done in order to not leave THIS_CU->cu in a state where we have
7791 to care whether it refers to the "main" CU or the DWO CU.
7792 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7793
7794static void
7795init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7796 die_reader_func_ftype *die_reader_func,
7797 void *data)
7798{
33e80786 7799 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7800}
0018ea6f
DE
7801\f
7802/* Type Unit Groups.
dee91e82 7803
0018ea6f
DE
7804 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7805 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7806 so that all types coming from the same compilation (.o file) are grouped
7807 together. A future step could be to put the types in the same symtab as
7808 the CU the types ultimately came from. */
ff013f42 7809
f4dc4d17
DE
7810static hashval_t
7811hash_type_unit_group (const void *item)
7812{
9a3c8263
SM
7813 const struct type_unit_group *tu_group
7814 = (const struct type_unit_group *) item;
f4dc4d17 7815
094b34ac 7816 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7817}
348e048f
DE
7818
7819static int
f4dc4d17 7820eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7821{
9a3c8263
SM
7822 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7823 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7824
094b34ac 7825 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7826}
348e048f 7827
f4dc4d17
DE
7828/* Allocate a hash table for type unit groups. */
7829
7830static htab_t
ed2dc618 7831allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7832{
7833 return htab_create_alloc_ex (3,
7834 hash_type_unit_group,
7835 eq_type_unit_group,
7836 NULL,
ed2dc618 7837 &objfile->objfile_obstack,
f4dc4d17
DE
7838 hashtab_obstack_allocate,
7839 dummy_obstack_deallocate);
7840}
dee91e82 7841
f4dc4d17
DE
7842/* Type units that don't have DW_AT_stmt_list are grouped into their own
7843 partial symtabs. We combine several TUs per psymtab to not let the size
7844 of any one psymtab grow too big. */
7845#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7846#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7847
094b34ac 7848/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7849 Create the type_unit_group object used to hold one or more TUs. */
7850
7851static struct type_unit_group *
094b34ac 7852create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7853{
518817b3
SM
7854 struct dwarf2_per_objfile *dwarf2_per_objfile
7855 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7856 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7857 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7858 struct type_unit_group *tu_group;
f4dc4d17
DE
7859
7860 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7861 struct type_unit_group);
094b34ac 7862 per_cu = &tu_group->per_cu;
518817b3 7863 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7864
094b34ac
DE
7865 if (dwarf2_per_objfile->using_index)
7866 {
7867 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7868 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7869 }
7870 else
7871 {
9c541725 7872 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7873 struct partial_symtab *pst;
528e1572 7874 std::string name;
094b34ac
DE
7875
7876 /* Give the symtab a useful name for debug purposes. */
7877 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7878 name = string_printf ("<type_units_%d>",
7879 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7880 else
528e1572 7881 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7882
528e1572 7883 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7884 pst->anonymous = 1;
094b34ac 7885 }
f4dc4d17 7886
094b34ac 7887 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7888 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7889
7890 return tu_group;
7891}
7892
094b34ac
DE
7893/* Look up the type_unit_group for type unit CU, and create it if necessary.
7894 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7895
7896static struct type_unit_group *
ff39bb5e 7897get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7898{
518817b3
SM
7899 struct dwarf2_per_objfile *dwarf2_per_objfile
7900 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7901 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7902 struct type_unit_group *tu_group;
7903 void **slot;
7904 unsigned int line_offset;
7905 struct type_unit_group type_unit_group_for_lookup;
7906
7907 if (dwarf2_per_objfile->type_unit_groups == NULL)
7908 {
7909 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7910 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7911 }
7912
7913 /* Do we need to create a new group, or can we use an existing one? */
7914
7915 if (stmt_list)
7916 {
7917 line_offset = DW_UNSND (stmt_list);
7918 ++tu_stats->nr_symtab_sharers;
7919 }
7920 else
7921 {
7922 /* Ugh, no stmt_list. Rare, but we have to handle it.
7923 We can do various things here like create one group per TU or
7924 spread them over multiple groups to split up the expansion work.
7925 To avoid worst case scenarios (too many groups or too large groups)
7926 we, umm, group them in bunches. */
7927 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7928 | (tu_stats->nr_stmt_less_type_units
7929 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7930 ++tu_stats->nr_stmt_less_type_units;
7931 }
7932
094b34ac 7933 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7934 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7935 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7936 &type_unit_group_for_lookup, INSERT);
7937 if (*slot != NULL)
7938 {
9a3c8263 7939 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7940 gdb_assert (tu_group != NULL);
7941 }
7942 else
7943 {
9c541725 7944 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7945 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7946 *slot = tu_group;
7947 ++tu_stats->nr_symtabs;
7948 }
7949
7950 return tu_group;
7951}
0018ea6f
DE
7952\f
7953/* Partial symbol tables. */
7954
7955/* Create a psymtab named NAME and assign it to PER_CU.
7956
7957 The caller must fill in the following details:
7958 dirname, textlow, texthigh. */
7959
7960static struct partial_symtab *
7961create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7962{
e3b94546 7963 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7964 struct partial_symtab *pst;
7965
939652a5 7966 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
7967
7968 pst->psymtabs_addrmap_supported = 1;
7969
7970 /* This is the glue that links PST into GDB's symbol API. */
7971 pst->read_symtab_private = per_cu;
7972 pst->read_symtab = dwarf2_read_symtab;
7973 per_cu->v.psymtab = pst;
7974
7975 return pst;
7976}
7977
b93601f3
TT
7978/* The DATA object passed to process_psymtab_comp_unit_reader has this
7979 type. */
7980
7981struct process_psymtab_comp_unit_data
7982{
7983 /* True if we are reading a DW_TAG_partial_unit. */
7984
7985 int want_partial_unit;
7986
7987 /* The "pretend" language that is used if the CU doesn't declare a
7988 language. */
7989
7990 enum language pretend_language;
7991};
7992
0018ea6f
DE
7993/* die_reader_func for process_psymtab_comp_unit. */
7994
7995static void
7996process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7997 const gdb_byte *info_ptr,
0018ea6f
DE
7998 struct die_info *comp_unit_die,
7999 int has_children,
8000 void *data)
8001{
8002 struct dwarf2_cu *cu = reader->cu;
518817b3 8003 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8004 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8005 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8006 CORE_ADDR baseaddr;
8007 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8008 struct partial_symtab *pst;
3a2b436a 8009 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8010 const char *filename;
9a3c8263
SM
8011 struct process_psymtab_comp_unit_data *info
8012 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8013
b93601f3 8014 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8015 return;
8016
8017 gdb_assert (! per_cu->is_debug_types);
8018
b93601f3 8019 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8020
0018ea6f 8021 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8022 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8023 if (filename == NULL)
0018ea6f 8024 filename = "";
0018ea6f
DE
8025
8026 pst = create_partial_symtab (per_cu, filename);
8027
8028 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8029 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8030
8031 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8032
8033 dwarf2_find_base_address (comp_unit_die, cu);
8034
8035 /* Possibly set the default values of LOWPC and HIGHPC from
8036 `DW_AT_ranges'. */
3a2b436a
JK
8037 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8038 &best_highpc, cu, pst);
8039 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8040 {
8041 CORE_ADDR low
8042 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8043 - baseaddr);
8044 CORE_ADDR high
8045 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8046 - baseaddr - 1);
8047 /* Store the contiguous range if it is not empty; it can be
8048 empty for CUs with no code. */
d320c2b5
TT
8049 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8050 low, high, pst);
79748972 8051 }
0018ea6f
DE
8052
8053 /* Check if comp unit has_children.
8054 If so, read the rest of the partial symbols from this comp unit.
8055 If not, there's no more debug_info for this comp unit. */
8056 if (has_children)
8057 {
8058 struct partial_die_info *first_die;
8059 CORE_ADDR lowpc, highpc;
8060
8061 lowpc = ((CORE_ADDR) -1);
8062 highpc = ((CORE_ADDR) 0);
8063
8064 first_die = load_partial_dies (reader, info_ptr, 1);
8065
8066 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8067 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8068
8069 /* If we didn't find a lowpc, set it to highpc to avoid
8070 complaints from `maint check'. */
8071 if (lowpc == ((CORE_ADDR) -1))
8072 lowpc = highpc;
8073
8074 /* If the compilation unit didn't have an explicit address range,
8075 then use the information extracted from its child dies. */
e385593e 8076 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8077 {
8078 best_lowpc = lowpc;
8079 best_highpc = highpc;
8080 }
8081 }
4ae976d1 8082 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8083 best_lowpc + baseaddr)
8084 - baseaddr);
4ae976d1 8085 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8086 best_highpc + baseaddr)
8087 - baseaddr);
0018ea6f 8088
8763cede 8089 end_psymtab_common (objfile, pst);
0018ea6f 8090
ae640021 8091 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8092 {
8093 int i;
ae640021 8094 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8095
8096 /* Fill in 'dependencies' here; we fill in 'users' in a
8097 post-pass. */
8098 pst->number_of_dependencies = len;
a9342b62
TT
8099 pst->dependencies
8100 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8101 for (i = 0; i < len; ++i)
8102 {
8103 pst->dependencies[i]
8104 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8105 }
0018ea6f 8106
ae640021 8107 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8108 }
8109
8110 /* Get the list of files included in the current compilation unit,
8111 and build a psymtab for each of them. */
8112 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8113
b4f54984 8114 if (dwarf_read_debug)
b926417a
TT
8115 fprintf_unfiltered (gdb_stdlog,
8116 "Psymtab for %s unit @%s: %s - %s"
8117 ", %d global, %d static syms\n",
8118 per_cu->is_debug_types ? "type" : "comp",
8119 sect_offset_str (per_cu->sect_off),
8120 paddress (gdbarch, pst->text_low (objfile)),
8121 paddress (gdbarch, pst->text_high (objfile)),
8122 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8123}
8124
8125/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8126 Process compilation unit THIS_CU for a psymtab. */
8127
8128static void
8129process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8130 int want_partial_unit,
8131 enum language pretend_language)
0018ea6f
DE
8132{
8133 /* If this compilation unit was already read in, free the
8134 cached copy in order to read it in again. This is
8135 necessary because we skipped some symbols when we first
8136 read in the compilation unit (see load_partial_dies).
8137 This problem could be avoided, but the benefit is unclear. */
8138 if (this_cu->cu != NULL)
8139 free_one_cached_comp_unit (this_cu);
8140
f1902523 8141 if (this_cu->is_debug_types)
58f0c718
TT
8142 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8143 build_type_psymtabs_reader, NULL);
f1902523
JK
8144 else
8145 {
8146 process_psymtab_comp_unit_data info;
8147 info.want_partial_unit = want_partial_unit;
8148 info.pretend_language = pretend_language;
58f0c718 8149 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8150 process_psymtab_comp_unit_reader, &info);
8151 }
0018ea6f
DE
8152
8153 /* Age out any secondary CUs. */
ed2dc618 8154 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8155}
f4dc4d17
DE
8156
8157/* Reader function for build_type_psymtabs. */
8158
8159static void
8160build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8161 const gdb_byte *info_ptr,
f4dc4d17
DE
8162 struct die_info *type_unit_die,
8163 int has_children,
8164 void *data)
8165{
ed2dc618 8166 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8167 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8168 struct objfile *objfile = dwarf2_per_objfile->objfile;
8169 struct dwarf2_cu *cu = reader->cu;
8170 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8171 struct signatured_type *sig_type;
f4dc4d17
DE
8172 struct type_unit_group *tu_group;
8173 struct attribute *attr;
8174 struct partial_die_info *first_die;
8175 CORE_ADDR lowpc, highpc;
8176 struct partial_symtab *pst;
8177
8178 gdb_assert (data == NULL);
0186c6a7
DE
8179 gdb_assert (per_cu->is_debug_types);
8180 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8181
8182 if (! has_children)
8183 return;
8184
8185 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8186 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8187
df07e2c7 8188 if (tu_group->tus == nullptr)
a8b3b8e9 8189 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8190 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8191
8192 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8193 pst = create_partial_symtab (per_cu, "");
8194 pst->anonymous = 1;
8195
8196 first_die = load_partial_dies (reader, info_ptr, 1);
8197
8198 lowpc = (CORE_ADDR) -1;
8199 highpc = (CORE_ADDR) 0;
8200 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8201
8763cede 8202 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8203}
8204
73051182
DE
8205/* Struct used to sort TUs by their abbreviation table offset. */
8206
8207struct tu_abbrev_offset
8208{
b2bdb8cf
SM
8209 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8210 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8211 {}
8212
8213 signatured_type *sig_type;
73051182
DE
8214 sect_offset abbrev_offset;
8215};
8216
484cf504 8217/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8218
484cf504
TT
8219static bool
8220sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8221 const struct tu_abbrev_offset &b)
73051182 8222{
484cf504 8223 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8224}
8225
8226/* Efficiently read all the type units.
8227 This does the bulk of the work for build_type_psymtabs.
8228
8229 The efficiency is because we sort TUs by the abbrev table they use and
8230 only read each abbrev table once. In one program there are 200K TUs
8231 sharing 8K abbrev tables.
8232
8233 The main purpose of this function is to support building the
8234 dwarf2_per_objfile->type_unit_groups table.
8235 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8236 can collapse the search space by grouping them by stmt_list.
8237 The savings can be significant, in the same program from above the 200K TUs
8238 share 8K stmt_list tables.
8239
8240 FUNC is expected to call get_type_unit_group, which will create the
8241 struct type_unit_group if necessary and add it to
8242 dwarf2_per_objfile->type_unit_groups. */
8243
8244static void
ed2dc618 8245build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8246{
73051182 8247 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8248 abbrev_table_up abbrev_table;
73051182 8249 sect_offset abbrev_offset;
73051182
DE
8250
8251 /* It's up to the caller to not call us multiple times. */
8252 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8253
b2bdb8cf 8254 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8255 return;
8256
8257 /* TUs typically share abbrev tables, and there can be way more TUs than
8258 abbrev tables. Sort by abbrev table to reduce the number of times we
8259 read each abbrev table in.
8260 Alternatives are to punt or to maintain a cache of abbrev tables.
8261 This is simpler and efficient enough for now.
8262
8263 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8264 symtab to use). Typically TUs with the same abbrev offset have the same
8265 stmt_list value too so in practice this should work well.
8266
8267 The basic algorithm here is:
8268
8269 sort TUs by abbrev table
8270 for each TU with same abbrev table:
8271 read abbrev table if first user
8272 read TU top level DIE
8273 [IWBN if DWO skeletons had DW_AT_stmt_list]
8274 call FUNC */
8275
b4f54984 8276 if (dwarf_read_debug)
73051182
DE
8277 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8278
8279 /* Sort in a separate table to maintain the order of all_type_units
8280 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8281 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8282 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8283
8284 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8285 sorted_by_abbrev.emplace_back
8286 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8287 sig_type->per_cu.section,
8288 sig_type->per_cu.sect_off));
73051182 8289
484cf504
TT
8290 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8291 sort_tu_by_abbrev_offset);
73051182 8292
9c541725 8293 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8294
b2bdb8cf 8295 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8296 {
73051182
DE
8297 /* Switch to the next abbrev table if necessary. */
8298 if (abbrev_table == NULL
b2bdb8cf 8299 || tu.abbrev_offset != abbrev_offset)
73051182 8300 {
b2bdb8cf 8301 abbrev_offset = tu.abbrev_offset;
73051182 8302 abbrev_table =
ed2dc618
SM
8303 abbrev_table_read_table (dwarf2_per_objfile,
8304 &dwarf2_per_objfile->abbrev,
73051182
DE
8305 abbrev_offset);
8306 ++tu_stats->nr_uniq_abbrev_tables;
8307 }
8308
b2bdb8cf 8309 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8310 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8311 }
6aa5f3a6 8312}
73051182 8313
6aa5f3a6
DE
8314/* Print collected type unit statistics. */
8315
8316static void
ed2dc618 8317print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8318{
8319 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8320
8321 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8322 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8323 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8324 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8325 tu_stats->nr_uniq_abbrev_tables);
8326 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8327 tu_stats->nr_symtabs);
8328 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8329 tu_stats->nr_symtab_sharers);
8330 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8331 tu_stats->nr_stmt_less_type_units);
8332 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8333 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8334}
8335
f4dc4d17
DE
8336/* Traversal function for build_type_psymtabs. */
8337
8338static int
8339build_type_psymtab_dependencies (void **slot, void *info)
8340{
ed2dc618
SM
8341 struct dwarf2_per_objfile *dwarf2_per_objfile
8342 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8343 struct objfile *objfile = dwarf2_per_objfile->objfile;
8344 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8345 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8346 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8347 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8348 int i;
8349
8350 gdb_assert (len > 0);
0186c6a7 8351 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8352
8353 pst->number_of_dependencies = len;
a9342b62 8354 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8355 for (i = 0; i < len; ++i)
f4dc4d17 8356 {
df07e2c7 8357 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8358 gdb_assert (iter->per_cu.is_debug_types);
8359 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8360 iter->type_unit_group = tu_group;
f4dc4d17
DE
8361 }
8362
df07e2c7
AB
8363 delete tu_group->tus;
8364 tu_group->tus = nullptr;
348e048f
DE
8365
8366 return 1;
8367}
8368
8369/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8370 Build partial symbol tables for the .debug_types comp-units. */
8371
8372static void
ed2dc618 8373build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8374{
ed2dc618 8375 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8376 return;
8377
ed2dc618 8378 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8379}
f4dc4d17 8380
6aa5f3a6
DE
8381/* Traversal function for process_skeletonless_type_unit.
8382 Read a TU in a DWO file and build partial symbols for it. */
8383
8384static int
8385process_skeletonless_type_unit (void **slot, void *info)
8386{
8387 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8388 struct dwarf2_per_objfile *dwarf2_per_objfile
8389 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8390 struct signatured_type find_entry, *entry;
8391
8392 /* If this TU doesn't exist in the global table, add it and read it in. */
8393
8394 if (dwarf2_per_objfile->signatured_types == NULL)
8395 {
8396 dwarf2_per_objfile->signatured_types
ed2dc618 8397 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8398 }
8399
8400 find_entry.signature = dwo_unit->signature;
8401 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8402 INSERT);
8403 /* If we've already seen this type there's nothing to do. What's happening
8404 is we're doing our own version of comdat-folding here. */
8405 if (*slot != NULL)
8406 return 1;
8407
8408 /* This does the job that create_all_type_units would have done for
8409 this TU. */
ed2dc618
SM
8410 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8411 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8412 *slot = entry;
8413
8414 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8415 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8416 build_type_psymtabs_reader, NULL);
8417
8418 return 1;
8419}
8420
8421/* Traversal function for process_skeletonless_type_units. */
8422
8423static int
8424process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8425{
8426 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8427
8428 if (dwo_file->tus != NULL)
8429 {
8430 htab_traverse_noresize (dwo_file->tus,
8431 process_skeletonless_type_unit, info);
8432 }
8433
8434 return 1;
8435}
8436
8437/* Scan all TUs of DWO files, verifying we've processed them.
8438 This is needed in case a TU was emitted without its skeleton.
8439 Note: This can't be done until we know what all the DWO files are. */
8440
8441static void
ed2dc618 8442process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8443{
8444 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8445 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8446 && dwarf2_per_objfile->dwo_files != NULL)
8447 {
51ac9db5 8448 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8449 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8450 dwarf2_per_objfile);
6aa5f3a6 8451 }
348e048f
DE
8452}
8453
ed2dc618 8454/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8455
8456static void
ed2dc618 8457set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8458{
b76e467d 8459 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8460 {
95554aad 8461 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8462
36586728
TT
8463 if (pst == NULL)
8464 continue;
8465
b76e467d 8466 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8467 {
8468 /* Set the 'user' field only if it is not already set. */
8469 if (pst->dependencies[j]->user == NULL)
8470 pst->dependencies[j]->user = pst;
8471 }
8472 }
8473}
8474
93311388
DE
8475/* Build the partial symbol table by doing a quick pass through the
8476 .debug_info and .debug_abbrev sections. */
72bf9492 8477
93311388 8478static void
ed2dc618 8479dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8480{
ed2dc618 8481 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8482
b4f54984 8483 if (dwarf_read_debug)
45cfd468
DE
8484 {
8485 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8486 objfile_name (objfile));
45cfd468
DE
8487 }
8488
98bfdba5
PA
8489 dwarf2_per_objfile->reading_partial_symbols = 1;
8490
be391dca 8491 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8492
93311388
DE
8493 /* Any cached compilation units will be linked by the per-objfile
8494 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8495 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8496
ed2dc618 8497 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8498
ed2dc618 8499 create_all_comp_units (dwarf2_per_objfile);
c906108c 8500
60606b2c
TT
8501 /* Create a temporary address map on a temporary obstack. We later
8502 copy this to the final obstack. */
8268c778 8503 auto_obstack temp_obstack;
791afaa2
TT
8504
8505 scoped_restore save_psymtabs_addrmap
d320c2b5 8506 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8507 addrmap_create_mutable (&temp_obstack));
72bf9492 8508
b76e467d
SM
8509 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8510 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8511
6aa5f3a6 8512 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8513 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8514
8515 /* Now that all TUs have been processed we can fill in the dependencies. */
8516 if (dwarf2_per_objfile->type_unit_groups != NULL)
8517 {
8518 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8519 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8520 }
8521
b4f54984 8522 if (dwarf_read_debug)
ed2dc618 8523 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8524
ed2dc618 8525 set_partial_user (dwarf2_per_objfile);
95554aad 8526
d320c2b5
TT
8527 objfile->partial_symtabs->psymtabs_addrmap
8528 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8529 objfile->partial_symtabs->obstack ());
791afaa2
TT
8530 /* At this point we want to keep the address map. */
8531 save_psymtabs_addrmap.release ();
ff013f42 8532
b4f54984 8533 if (dwarf_read_debug)
45cfd468 8534 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8535 objfile_name (objfile));
ae038cb0
DJ
8536}
8537
3019eac3 8538/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8539
8540static void
dee91e82 8541load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8542 const gdb_byte *info_ptr,
dee91e82
DE
8543 struct die_info *comp_unit_die,
8544 int has_children,
8545 void *data)
ae038cb0 8546{
dee91e82 8547 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8548
95554aad 8549 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8550
ae038cb0
DJ
8551 /* Check if comp unit has_children.
8552 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8553 If not, there's no more debug_info for this comp unit. */
d85a05f0 8554 if (has_children)
dee91e82
DE
8555 load_partial_dies (reader, info_ptr, 0);
8556}
98bfdba5 8557
dee91e82
DE
8558/* Load the partial DIEs for a secondary CU into memory.
8559 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8560
dee91e82
DE
8561static void
8562load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8563{
58f0c718 8564 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8565 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8566}
8567
ae038cb0 8568static void
ed2dc618 8569read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8570 struct dwarf2_section_info *section,
f1902523 8571 struct dwarf2_section_info *abbrev_section,
b76e467d 8572 unsigned int is_dwz)
ae038cb0 8573{
d521ce57 8574 const gdb_byte *info_ptr;
ed2dc618 8575 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8576
b4f54984 8577 if (dwarf_read_debug)
bf6af496 8578 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8579 get_section_name (section),
8580 get_section_file_name (section));
bf6af496 8581
36586728 8582 dwarf2_read_section (objfile, section);
ae038cb0 8583
36586728 8584 info_ptr = section->buffer;
6e70227d 8585
36586728 8586 while (info_ptr < section->buffer + section->size)
ae038cb0 8587 {
ae038cb0 8588 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8589
9c541725 8590 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8591
f1902523 8592 comp_unit_head cu_header;
ed2dc618
SM
8593 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8594 abbrev_section, info_ptr,
8595 rcuh_kind::COMPILE);
ae038cb0
DJ
8596
8597 /* Save the compilation unit for later lookup. */
f1902523
JK
8598 if (cu_header.unit_type != DW_UT_type)
8599 {
8600 this_cu = XOBNEW (&objfile->objfile_obstack,
8601 struct dwarf2_per_cu_data);
8602 memset (this_cu, 0, sizeof (*this_cu));
8603 }
8604 else
8605 {
8606 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8607 struct signatured_type);
8608 memset (sig_type, 0, sizeof (*sig_type));
8609 sig_type->signature = cu_header.signature;
8610 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8611 this_cu = &sig_type->per_cu;
8612 }
8613 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8614 this_cu->sect_off = sect_off;
f1902523 8615 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8616 this_cu->is_dwz = is_dwz;
e3b94546 8617 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8618 this_cu->section = section;
ae038cb0 8619
b76e467d 8620 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8621
8622 info_ptr = info_ptr + this_cu->length;
8623 }
36586728
TT
8624}
8625
8626/* Create a list of all compilation units in OBJFILE.
8627 This is only done for -readnow and building partial symtabs. */
8628
8629static void
ed2dc618 8630create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8631{
b76e467d 8632 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8633 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8634 &dwarf2_per_objfile->abbrev, 0);
36586728 8635
b76e467d 8636 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8637 if (dwz != NULL)
ed2dc618 8638 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8639 1);
c906108c
SS
8640}
8641
5734ee8b 8642/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8643 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8644 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8645 DW_AT_ranges). See the comments of add_partial_subprogram on how
8646 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8647
72bf9492
DJ
8648static void
8649scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8650 CORE_ADDR *highpc, int set_addrmap,
8651 struct dwarf2_cu *cu)
c906108c 8652{
72bf9492 8653 struct partial_die_info *pdi;
c906108c 8654
91c24f0a
DC
8655 /* Now, march along the PDI's, descending into ones which have
8656 interesting children but skipping the children of the other ones,
8657 until we reach the end of the compilation unit. */
c906108c 8658
72bf9492 8659 pdi = first_die;
91c24f0a 8660
72bf9492
DJ
8661 while (pdi != NULL)
8662 {
52356b79 8663 pdi->fixup (cu);
c906108c 8664
f55ee35c 8665 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8666 children, so we need to look at them. Ditto for anonymous
8667 enums. */
933c6fe4 8668
72bf9492 8669 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8670 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8671 || pdi->tag == DW_TAG_imported_unit
8672 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8673 {
72bf9492 8674 switch (pdi->tag)
c906108c
SS
8675 {
8676 case DW_TAG_subprogram:
b1dc1806 8677 case DW_TAG_inlined_subroutine:
cdc07690 8678 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8679 break;
72929c62 8680 case DW_TAG_constant:
c906108c
SS
8681 case DW_TAG_variable:
8682 case DW_TAG_typedef:
91c24f0a 8683 case DW_TAG_union_type:
72bf9492 8684 if (!pdi->is_declaration)
63d06c5c 8685 {
72bf9492 8686 add_partial_symbol (pdi, cu);
63d06c5c
DC
8687 }
8688 break;
c906108c 8689 case DW_TAG_class_type:
680b30c7 8690 case DW_TAG_interface_type:
c906108c 8691 case DW_TAG_structure_type:
72bf9492 8692 if (!pdi->is_declaration)
c906108c 8693 {
72bf9492 8694 add_partial_symbol (pdi, cu);
c906108c 8695 }
b7fee5a3
KS
8696 if ((cu->language == language_rust
8697 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8698 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8699 set_addrmap, cu);
c906108c 8700 break;
91c24f0a 8701 case DW_TAG_enumeration_type:
72bf9492
DJ
8702 if (!pdi->is_declaration)
8703 add_partial_enumeration (pdi, cu);
c906108c
SS
8704 break;
8705 case DW_TAG_base_type:
a02abb62 8706 case DW_TAG_subrange_type:
c906108c 8707 /* File scope base type definitions are added to the partial
c5aa993b 8708 symbol table. */
72bf9492 8709 add_partial_symbol (pdi, cu);
c906108c 8710 break;
d9fa45fe 8711 case DW_TAG_namespace:
cdc07690 8712 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8713 break;
5d7cb8df 8714 case DW_TAG_module:
cdc07690 8715 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8716 break;
95554aad
TT
8717 case DW_TAG_imported_unit:
8718 {
8719 struct dwarf2_per_cu_data *per_cu;
8720
f4dc4d17
DE
8721 /* For now we don't handle imported units in type units. */
8722 if (cu->per_cu->is_debug_types)
8723 {
8724 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8725 " supported in type units [in module %s]"),
518817b3 8726 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8727 }
8728
e3b94546
SM
8729 per_cu = dwarf2_find_containing_comp_unit
8730 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8731 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8732
8733 /* Go read the partial unit, if needed. */
8734 if (per_cu->v.psymtab == NULL)
b93601f3 8735 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8736
ae640021 8737 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8738 }
8739 break;
74921315
KS
8740 case DW_TAG_imported_declaration:
8741 add_partial_symbol (pdi, cu);
8742 break;
c906108c
SS
8743 default:
8744 break;
8745 }
8746 }
8747
72bf9492
DJ
8748 /* If the die has a sibling, skip to the sibling. */
8749
8750 pdi = pdi->die_sibling;
8751 }
8752}
8753
8754/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8755
72bf9492 8756 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8757 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8758 Enumerators are an exception; they use the scope of their parent
8759 enumeration type, i.e. the name of the enumeration type is not
8760 prepended to the enumerator.
91c24f0a 8761
72bf9492
DJ
8762 There are two complexities. One is DW_AT_specification; in this
8763 case "parent" means the parent of the target of the specification,
8764 instead of the direct parent of the DIE. The other is compilers
8765 which do not emit DW_TAG_namespace; in this case we try to guess
8766 the fully qualified name of structure types from their members'
8767 linkage names. This must be done using the DIE's children rather
8768 than the children of any DW_AT_specification target. We only need
8769 to do this for structures at the top level, i.e. if the target of
8770 any DW_AT_specification (if any; otherwise the DIE itself) does not
8771 have a parent. */
8772
8773/* Compute the scope prefix associated with PDI's parent, in
8774 compilation unit CU. The result will be allocated on CU's
8775 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8776 field. NULL is returned if no prefix is necessary. */
15d034d0 8777static const char *
72bf9492
DJ
8778partial_die_parent_scope (struct partial_die_info *pdi,
8779 struct dwarf2_cu *cu)
8780{
15d034d0 8781 const char *grandparent_scope;
72bf9492 8782 struct partial_die_info *parent, *real_pdi;
91c24f0a 8783
72bf9492
DJ
8784 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8785 then this means the parent of the specification DIE. */
8786
8787 real_pdi = pdi;
72bf9492 8788 while (real_pdi->has_specification)
fb816e8b 8789 {
122cf0f2
AB
8790 auto res = find_partial_die (real_pdi->spec_offset,
8791 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8792 real_pdi = res.pdi;
8793 cu = res.cu;
8794 }
72bf9492
DJ
8795
8796 parent = real_pdi->die_parent;
8797 if (parent == NULL)
8798 return NULL;
8799
8800 if (parent->scope_set)
8801 return parent->scope;
8802
52356b79 8803 parent->fixup (cu);
72bf9492 8804
10b3939b 8805 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8806
acebe513
UW
8807 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8808 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8809 Work around this problem here. */
8810 if (cu->language == language_cplus
6e70227d 8811 && parent->tag == DW_TAG_namespace
acebe513
UW
8812 && strcmp (parent->name, "::") == 0
8813 && grandparent_scope == NULL)
8814 {
8815 parent->scope = NULL;
8816 parent->scope_set = 1;
8817 return NULL;
8818 }
8819
0a4b0913 8820 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8821 if (pdi->tag == DW_TAG_enumerator)
8822 /* Enumerators should not get the name of the enumeration as a prefix. */
8823 parent->scope = grandparent_scope;
8824 else if (parent->tag == DW_TAG_namespace
f55ee35c 8825 || parent->tag == DW_TAG_module
72bf9492
DJ
8826 || parent->tag == DW_TAG_structure_type
8827 || parent->tag == DW_TAG_class_type
680b30c7 8828 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8829 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8830 || parent->tag == DW_TAG_enumeration_type
8831 || (cu->language == language_fortran
8832 && parent->tag == DW_TAG_subprogram
8833 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8834 {
8835 if (grandparent_scope == NULL)
8836 parent->scope = parent->name;
8837 else
3e43a32a
MS
8838 parent->scope = typename_concat (&cu->comp_unit_obstack,
8839 grandparent_scope,
f55ee35c 8840 parent->name, 0, cu);
72bf9492 8841 }
72bf9492
DJ
8842 else
8843 {
8844 /* FIXME drow/2004-04-01: What should we be doing with
8845 function-local names? For partial symbols, we should probably be
8846 ignoring them. */
fa9c3fa0
TT
8847 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8848 dwarf_tag_name (parent->tag),
8849 sect_offset_str (pdi->sect_off));
72bf9492 8850 parent->scope = grandparent_scope;
c906108c
SS
8851 }
8852
72bf9492
DJ
8853 parent->scope_set = 1;
8854 return parent->scope;
8855}
8856
8857/* Return the fully scoped name associated with PDI, from compilation unit
8858 CU. The result will be allocated with malloc. */
4568ecf9 8859
72bf9492
DJ
8860static char *
8861partial_die_full_name (struct partial_die_info *pdi,
8862 struct dwarf2_cu *cu)
8863{
15d034d0 8864 const char *parent_scope;
72bf9492 8865
98bfdba5
PA
8866 /* If this is a template instantiation, we can not work out the
8867 template arguments from partial DIEs. So, unfortunately, we have
8868 to go through the full DIEs. At least any work we do building
8869 types here will be reused if full symbols are loaded later. */
8870 if (pdi->has_template_arguments)
8871 {
52356b79 8872 pdi->fixup (cu);
98bfdba5
PA
8873
8874 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8875 {
8876 struct die_info *die;
8877 struct attribute attr;
8878 struct dwarf2_cu *ref_cu = cu;
8879
b64f50a1 8880 /* DW_FORM_ref_addr is using section offset. */
b4069958 8881 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8882 attr.form = DW_FORM_ref_addr;
9c541725 8883 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8884 die = follow_die_ref (NULL, &attr, &ref_cu);
8885
8886 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8887 }
8888 }
8889
72bf9492
DJ
8890 parent_scope = partial_die_parent_scope (pdi, cu);
8891 if (parent_scope == NULL)
8892 return NULL;
8893 else
f55ee35c 8894 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8895}
8896
8897static void
72bf9492 8898add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8899{
518817b3
SM
8900 struct dwarf2_per_objfile *dwarf2_per_objfile
8901 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8902 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8903 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8904 CORE_ADDR addr = 0;
15d034d0 8905 const char *actual_name = NULL;
e142c38c 8906 CORE_ADDR baseaddr;
15d034d0 8907 char *built_actual_name;
e142c38c
DJ
8908
8909 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8910
15d034d0
TT
8911 built_actual_name = partial_die_full_name (pdi, cu);
8912 if (built_actual_name != NULL)
8913 actual_name = built_actual_name;
63d06c5c 8914
72bf9492
DJ
8915 if (actual_name == NULL)
8916 actual_name = pdi->name;
8917
c906108c
SS
8918 switch (pdi->tag)
8919 {
b1dc1806 8920 case DW_TAG_inlined_subroutine:
c906108c 8921 case DW_TAG_subprogram:
79748972
TT
8922 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8923 - baseaddr);
0a4b0913
AB
8924 if (pdi->is_external
8925 || cu->language == language_ada
8926 || (cu->language == language_fortran
8927 && pdi->die_parent != NULL
8928 && pdi->die_parent->tag == DW_TAG_subprogram))
8929 {
8930 /* Normally, only "external" DIEs are part of the global scope.
8931 But in Ada and Fortran, we want to be able to access nested
8932 procedures globally. So all Ada and Fortran subprograms are
8933 stored in the global scope. */
f47fb265 8934 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8935 built_actual_name != NULL,
f47fb265 8936 VAR_DOMAIN, LOC_BLOCK,
79748972 8937 SECT_OFF_TEXT (objfile),
75aedd27 8938 psymbol_placement::GLOBAL,
79748972
TT
8939 addr,
8940 cu->language, objfile);
c906108c
SS
8941 }
8942 else
8943 {
f47fb265 8944 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8945 built_actual_name != NULL,
f47fb265 8946 VAR_DOMAIN, LOC_BLOCK,
79748972 8947 SECT_OFF_TEXT (objfile),
75aedd27 8948 psymbol_placement::STATIC,
1762568f 8949 addr, cu->language, objfile);
c906108c 8950 }
0c1b455e
TT
8951
8952 if (pdi->main_subprogram && actual_name != NULL)
8953 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8954 break;
72929c62 8955 case DW_TAG_constant:
75aedd27
TT
8956 add_psymbol_to_list (actual_name, strlen (actual_name),
8957 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8958 -1, (pdi->is_external
8959 ? psymbol_placement::GLOBAL
8960 : psymbol_placement::STATIC),
8961 0, cu->language, objfile);
72929c62 8962 break;
c906108c 8963 case DW_TAG_variable:
95554aad
TT
8964 if (pdi->d.locdesc)
8965 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8966
95554aad 8967 if (pdi->d.locdesc
caac4577
JG
8968 && addr == 0
8969 && !dwarf2_per_objfile->has_section_at_zero)
8970 {
8971 /* A global or static variable may also have been stripped
8972 out by the linker if unused, in which case its address
8973 will be nullified; do not add such variables into partial
8974 symbol table then. */
8975 }
8976 else if (pdi->is_external)
c906108c
SS
8977 {
8978 /* Global Variable.
8979 Don't enter into the minimal symbol tables as there is
8980 a minimal symbol table entry from the ELF symbols already.
8981 Enter into partial symbol table if it has a location
8982 descriptor or a type.
8983 If the location descriptor is missing, new_symbol will create
8984 a LOC_UNRESOLVED symbol, the address of the variable will then
8985 be determined from the minimal symbol table whenever the variable
8986 is referenced.
8987 The address for the partial symbol table entry is not
8988 used by GDB, but it comes in handy for debugging partial symbol
8989 table building. */
8990
95554aad 8991 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8992 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8993 built_actual_name != NULL,
f47fb265 8994 VAR_DOMAIN, LOC_STATIC,
79748972 8995 SECT_OFF_TEXT (objfile),
75aedd27 8996 psymbol_placement::GLOBAL,
79748972 8997 addr, cu->language, objfile);
c906108c
SS
8998 }
8999 else
9000 {
ff908ebf
AW
9001 int has_loc = pdi->d.locdesc != NULL;
9002
9003 /* Static Variable. Skip symbols whose value we cannot know (those
9004 without location descriptors or constant values). */
9005 if (!has_loc && !pdi->has_const_value)
decbce07 9006 {
15d034d0 9007 xfree (built_actual_name);
decbce07
MS
9008 return;
9009 }
ff908ebf 9010
f47fb265 9011 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9012 built_actual_name != NULL,
f47fb265 9013 VAR_DOMAIN, LOC_STATIC,
79748972 9014 SECT_OFF_TEXT (objfile),
75aedd27 9015 psymbol_placement::STATIC,
79748972 9016 has_loc ? addr : 0,
f47fb265 9017 cu->language, objfile);
c906108c
SS
9018 }
9019 break;
9020 case DW_TAG_typedef:
9021 case DW_TAG_base_type:
a02abb62 9022 case DW_TAG_subrange_type:
38d518c9 9023 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9024 built_actual_name != NULL,
79748972 9025 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9026 psymbol_placement::STATIC,
1762568f 9027 0, cu->language, objfile);
c906108c 9028 break;
74921315 9029 case DW_TAG_imported_declaration:
72bf9492
DJ
9030 case DW_TAG_namespace:
9031 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9032 built_actual_name != NULL,
79748972 9033 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9034 psymbol_placement::GLOBAL,
1762568f 9035 0, cu->language, objfile);
72bf9492 9036 break;
530e8392 9037 case DW_TAG_module:
a5fd13a9
BH
9038 /* With Fortran 77 there might be a "BLOCK DATA" module
9039 available without any name. If so, we skip the module as it
9040 doesn't bring any value. */
9041 if (actual_name != nullptr)
9042 add_psymbol_to_list (actual_name, strlen (actual_name),
9043 built_actual_name != NULL,
9044 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9045 psymbol_placement::GLOBAL,
9046 0, cu->language, objfile);
530e8392 9047 break;
c906108c 9048 case DW_TAG_class_type:
680b30c7 9049 case DW_TAG_interface_type:
c906108c
SS
9050 case DW_TAG_structure_type:
9051 case DW_TAG_union_type:
9052 case DW_TAG_enumeration_type:
fa4028e9
JB
9053 /* Skip external references. The DWARF standard says in the section
9054 about "Structure, Union, and Class Type Entries": "An incomplete
9055 structure, union or class type is represented by a structure,
9056 union or class entry that does not have a byte size attribute
9057 and that has a DW_AT_declaration attribute." */
9058 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9059 {
15d034d0 9060 xfree (built_actual_name);
decbce07
MS
9061 return;
9062 }
fa4028e9 9063
63d06c5c
DC
9064 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9065 static vs. global. */
38d518c9 9066 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9067 built_actual_name != NULL,
79748972 9068 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9069 cu->language == language_cplus
75aedd27
TT
9070 ? psymbol_placement::GLOBAL
9071 : psymbol_placement::STATIC,
1762568f 9072 0, cu->language, objfile);
c906108c 9073
c906108c
SS
9074 break;
9075 case DW_TAG_enumerator:
38d518c9 9076 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9077 built_actual_name != NULL,
79748972 9078 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9079 cu->language == language_cplus
75aedd27
TT
9080 ? psymbol_placement::GLOBAL
9081 : psymbol_placement::STATIC,
1762568f 9082 0, cu->language, objfile);
c906108c
SS
9083 break;
9084 default:
9085 break;
9086 }
5c4e30ca 9087
15d034d0 9088 xfree (built_actual_name);
c906108c
SS
9089}
9090
5c4e30ca
DC
9091/* Read a partial die corresponding to a namespace; also, add a symbol
9092 corresponding to that namespace to the symbol table. NAMESPACE is
9093 the name of the enclosing namespace. */
91c24f0a 9094
72bf9492
DJ
9095static void
9096add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9097 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9098 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9099{
72bf9492 9100 /* Add a symbol for the namespace. */
e7c27a73 9101
72bf9492 9102 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9103
9104 /* Now scan partial symbols in that namespace. */
9105
91c24f0a 9106 if (pdi->has_children)
cdc07690 9107 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9108}
9109
5d7cb8df
JK
9110/* Read a partial die corresponding to a Fortran module. */
9111
9112static void
9113add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9114 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9115{
530e8392
KB
9116 /* Add a symbol for the namespace. */
9117
9118 add_partial_symbol (pdi, cu);
9119
f55ee35c 9120 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9121
9122 if (pdi->has_children)
cdc07690 9123 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9124}
9125
b1dc1806
XR
9126/* Read a partial die corresponding to a subprogram or an inlined
9127 subprogram and create a partial symbol for that subprogram.
9128 When the CU language allows it, this routine also defines a partial
9129 symbol for each nested subprogram that this subprogram contains.
9130 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9131 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9132
cdc07690
YQ
9133 PDI may also be a lexical block, in which case we simply search
9134 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9135 Again, this is only performed when the CU language allows this
9136 type of definitions. */
9137
9138static void
9139add_partial_subprogram (struct partial_die_info *pdi,
9140 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9141 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9142{
b1dc1806 9143 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9144 {
9145 if (pdi->has_pc_info)
9146 {
9147 if (pdi->lowpc < *lowpc)
9148 *lowpc = pdi->lowpc;
9149 if (pdi->highpc > *highpc)
9150 *highpc = pdi->highpc;
cdc07690 9151 if (set_addrmap)
5734ee8b 9152 {
518817b3 9153 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9154 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9155 CORE_ADDR baseaddr;
b926417a
TT
9156 CORE_ADDR this_highpc;
9157 CORE_ADDR this_lowpc;
5734ee8b
DJ
9158
9159 baseaddr = ANOFFSET (objfile->section_offsets,
9160 SECT_OFF_TEXT (objfile));
b926417a
TT
9161 this_lowpc
9162 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9163 pdi->lowpc + baseaddr)
9164 - baseaddr);
9165 this_highpc
9166 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9167 pdi->highpc + baseaddr)
9168 - baseaddr);
d320c2b5 9169 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9170 this_lowpc, this_highpc - 1,
9291a0cd 9171 cu->per_cu->v.psymtab);
5734ee8b 9172 }
481860b3
GB
9173 }
9174
9175 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9176 {
bc30ff58 9177 if (!pdi->is_declaration)
e8d05480
JB
9178 /* Ignore subprogram DIEs that do not have a name, they are
9179 illegal. Do not emit a complaint at this point, we will
9180 do so when we convert this psymtab into a symtab. */
9181 if (pdi->name)
9182 add_partial_symbol (pdi, cu);
bc30ff58
JB
9183 }
9184 }
6e70227d 9185
bc30ff58
JB
9186 if (! pdi->has_children)
9187 return;
9188
0a4b0913 9189 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9190 {
9191 pdi = pdi->die_child;
9192 while (pdi != NULL)
9193 {
52356b79 9194 pdi->fixup (cu);
bc30ff58 9195 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9196 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9197 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9198 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9199 pdi = pdi->die_sibling;
9200 }
9201 }
9202}
9203
91c24f0a
DC
9204/* Read a partial die corresponding to an enumeration type. */
9205
72bf9492
DJ
9206static void
9207add_partial_enumeration (struct partial_die_info *enum_pdi,
9208 struct dwarf2_cu *cu)
91c24f0a 9209{
72bf9492 9210 struct partial_die_info *pdi;
91c24f0a
DC
9211
9212 if (enum_pdi->name != NULL)
72bf9492
DJ
9213 add_partial_symbol (enum_pdi, cu);
9214
9215 pdi = enum_pdi->die_child;
9216 while (pdi)
91c24f0a 9217 {
72bf9492 9218 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9219 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9220 else
72bf9492
DJ
9221 add_partial_symbol (pdi, cu);
9222 pdi = pdi->die_sibling;
91c24f0a 9223 }
91c24f0a
DC
9224}
9225
6caca83c
CC
9226/* Return the initial uleb128 in the die at INFO_PTR. */
9227
9228static unsigned int
d521ce57 9229peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9230{
9231 unsigned int bytes_read;
9232
9233 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9234}
9235
685af9cd
TT
9236/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9237 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9238
4bb7a0a7
DJ
9239 Return the corresponding abbrev, or NULL if the number is zero (indicating
9240 an empty DIE). In either case *BYTES_READ will be set to the length of
9241 the initial number. */
9242
9243static struct abbrev_info *
685af9cd
TT
9244peek_die_abbrev (const die_reader_specs &reader,
9245 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9246{
685af9cd 9247 dwarf2_cu *cu = reader.cu;
518817b3 9248 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9249 unsigned int abbrev_number
9250 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9251
9252 if (abbrev_number == 0)
9253 return NULL;
9254
685af9cd 9255 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9256 if (!abbrev)
9257 {
422b9917 9258 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9259 " at offset %s [in module %s]"),
422b9917 9260 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9261 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9262 }
9263
9264 return abbrev;
9265}
9266
93311388
DE
9267/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9268 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9269 DIE. Any children of the skipped DIEs will also be skipped. */
9270
d521ce57
TT
9271static const gdb_byte *
9272skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9273{
4bb7a0a7
DJ
9274 while (1)
9275 {
685af9cd
TT
9276 unsigned int bytes_read;
9277 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9278
4bb7a0a7
DJ
9279 if (abbrev == NULL)
9280 return info_ptr + bytes_read;
9281 else
dee91e82 9282 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9283 }
9284}
9285
93311388
DE
9286/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9287 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9288 abbrev corresponding to that skipped uleb128 should be passed in
9289 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9290 children. */
9291
d521ce57
TT
9292static const gdb_byte *
9293skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9294 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9295{
9296 unsigned int bytes_read;
9297 struct attribute attr;
dee91e82
DE
9298 bfd *abfd = reader->abfd;
9299 struct dwarf2_cu *cu = reader->cu;
d521ce57 9300 const gdb_byte *buffer = reader->buffer;
f664829e 9301 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9302 unsigned int form, i;
9303
9304 for (i = 0; i < abbrev->num_attrs; i++)
9305 {
9306 /* The only abbrev we care about is DW_AT_sibling. */
9307 if (abbrev->attrs[i].name == DW_AT_sibling)
9308 {
dee91e82 9309 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9310 if (attr.form == DW_FORM_ref_addr)
b98664d3 9311 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9312 else
b9502d3f 9313 {
9c541725
PA
9314 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9315 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9316
9317 if (sibling_ptr < info_ptr)
b98664d3 9318 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9319 else if (sibling_ptr > reader->buffer_end)
9320 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9321 else
9322 return sibling_ptr;
9323 }
4bb7a0a7
DJ
9324 }
9325
9326 /* If it isn't DW_AT_sibling, skip this attribute. */
9327 form = abbrev->attrs[i].form;
9328 skip_attribute:
9329 switch (form)
9330 {
4bb7a0a7 9331 case DW_FORM_ref_addr:
ae411497
TT
9332 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9333 and later it is offset sized. */
9334 if (cu->header.version == 2)
9335 info_ptr += cu->header.addr_size;
9336 else
9337 info_ptr += cu->header.offset_size;
9338 break;
36586728
TT
9339 case DW_FORM_GNU_ref_alt:
9340 info_ptr += cu->header.offset_size;
9341 break;
ae411497 9342 case DW_FORM_addr:
4bb7a0a7
DJ
9343 info_ptr += cu->header.addr_size;
9344 break;
9345 case DW_FORM_data1:
9346 case DW_FORM_ref1:
9347 case DW_FORM_flag:
8fe0f950 9348 case DW_FORM_strx1:
4bb7a0a7
DJ
9349 info_ptr += 1;
9350 break;
2dc7f7b3 9351 case DW_FORM_flag_present:
43988095 9352 case DW_FORM_implicit_const:
2dc7f7b3 9353 break;
4bb7a0a7
DJ
9354 case DW_FORM_data2:
9355 case DW_FORM_ref2:
8fe0f950 9356 case DW_FORM_strx2:
4bb7a0a7
DJ
9357 info_ptr += 2;
9358 break;
8fe0f950
AT
9359 case DW_FORM_strx3:
9360 info_ptr += 3;
9361 break;
4bb7a0a7
DJ
9362 case DW_FORM_data4:
9363 case DW_FORM_ref4:
8fe0f950 9364 case DW_FORM_strx4:
4bb7a0a7
DJ
9365 info_ptr += 4;
9366 break;
9367 case DW_FORM_data8:
9368 case DW_FORM_ref8:
55f1336d 9369 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9370 info_ptr += 8;
9371 break;
0224619f
JK
9372 case DW_FORM_data16:
9373 info_ptr += 16;
9374 break;
4bb7a0a7 9375 case DW_FORM_string:
9b1c24c8 9376 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9377 info_ptr += bytes_read;
9378 break;
2dc7f7b3 9379 case DW_FORM_sec_offset:
4bb7a0a7 9380 case DW_FORM_strp:
36586728 9381 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9382 info_ptr += cu->header.offset_size;
9383 break;
2dc7f7b3 9384 case DW_FORM_exprloc:
4bb7a0a7
DJ
9385 case DW_FORM_block:
9386 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9387 info_ptr += bytes_read;
9388 break;
9389 case DW_FORM_block1:
9390 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9391 break;
9392 case DW_FORM_block2:
9393 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9394 break;
9395 case DW_FORM_block4:
9396 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9397 break;
336d760d 9398 case DW_FORM_addrx:
cf532bd1 9399 case DW_FORM_strx:
4bb7a0a7
DJ
9400 case DW_FORM_sdata:
9401 case DW_FORM_udata:
9402 case DW_FORM_ref_udata:
3019eac3
DE
9403 case DW_FORM_GNU_addr_index:
9404 case DW_FORM_GNU_str_index:
d521ce57 9405 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9406 break;
9407 case DW_FORM_indirect:
9408 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9409 info_ptr += bytes_read;
9410 /* We need to continue parsing from here, so just go back to
9411 the top. */
9412 goto skip_attribute;
9413
9414 default:
3e43a32a
MS
9415 error (_("Dwarf Error: Cannot handle %s "
9416 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9417 dwarf_form_name (form),
9418 bfd_get_filename (abfd));
9419 }
9420 }
9421
9422 if (abbrev->has_children)
dee91e82 9423 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9424 else
9425 return info_ptr;
9426}
9427
93311388 9428/* Locate ORIG_PDI's sibling.
dee91e82 9429 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9430
d521ce57 9431static const gdb_byte *
dee91e82
DE
9432locate_pdi_sibling (const struct die_reader_specs *reader,
9433 struct partial_die_info *orig_pdi,
d521ce57 9434 const gdb_byte *info_ptr)
91c24f0a
DC
9435{
9436 /* Do we know the sibling already? */
72bf9492 9437
91c24f0a
DC
9438 if (orig_pdi->sibling)
9439 return orig_pdi->sibling;
9440
9441 /* Are there any children to deal with? */
9442
9443 if (!orig_pdi->has_children)
9444 return info_ptr;
9445
4bb7a0a7 9446 /* Skip the children the long way. */
91c24f0a 9447
dee91e82 9448 return skip_children (reader, info_ptr);
91c24f0a
DC
9449}
9450
257e7a09 9451/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9452 not NULL. */
c906108c
SS
9453
9454static void
257e7a09
YQ
9455dwarf2_read_symtab (struct partial_symtab *self,
9456 struct objfile *objfile)
c906108c 9457{
ed2dc618
SM
9458 struct dwarf2_per_objfile *dwarf2_per_objfile
9459 = get_dwarf2_per_objfile (objfile);
9460
257e7a09 9461 if (self->readin)
c906108c 9462 {
442e4d9c 9463 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9464 self->filename);
442e4d9c
YQ
9465 }
9466 else
9467 {
9468 if (info_verbose)
c906108c 9469 {
442e4d9c 9470 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9471 self->filename);
442e4d9c 9472 gdb_flush (gdb_stdout);
c906108c 9473 }
c906108c 9474
442e4d9c
YQ
9475 /* If this psymtab is constructed from a debug-only objfile, the
9476 has_section_at_zero flag will not necessarily be correct. We
9477 can get the correct value for this flag by looking at the data
9478 associated with the (presumably stripped) associated objfile. */
9479 if (objfile->separate_debug_objfile_backlink)
9480 {
9481 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9482 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9483
442e4d9c
YQ
9484 dwarf2_per_objfile->has_section_at_zero
9485 = dpo_backlink->has_section_at_zero;
9486 }
b2ab525c 9487
442e4d9c 9488 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9489
257e7a09 9490 psymtab_to_symtab_1 (self);
c906108c 9491
442e4d9c
YQ
9492 /* Finish up the debug error message. */
9493 if (info_verbose)
9494 printf_filtered (_("done.\n"));
c906108c 9495 }
95554aad 9496
ed2dc618 9497 process_cu_includes (dwarf2_per_objfile);
c906108c 9498}
9cdd5dbd
DE
9499\f
9500/* Reading in full CUs. */
c906108c 9501
10b3939b
DJ
9502/* Add PER_CU to the queue. */
9503
9504static void
95554aad
TT
9505queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9506 enum language pretend_language)
10b3939b
DJ
9507{
9508 struct dwarf2_queue_item *item;
9509
9510 per_cu->queued = 1;
8d749320 9511 item = XNEW (struct dwarf2_queue_item);
10b3939b 9512 item->per_cu = per_cu;
95554aad 9513 item->pretend_language = pretend_language;
10b3939b
DJ
9514 item->next = NULL;
9515
9516 if (dwarf2_queue == NULL)
9517 dwarf2_queue = item;
9518 else
9519 dwarf2_queue_tail->next = item;
9520
9521 dwarf2_queue_tail = item;
9522}
9523
89e63ee4
DE
9524/* If PER_CU is not yet queued, add it to the queue.
9525 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9526 dependency.
0907af0c 9527 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9528 meaning either PER_CU is already queued or it is already loaded.
9529
9530 N.B. There is an invariant here that if a CU is queued then it is loaded.
9531 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9532
9533static int
89e63ee4 9534maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9535 struct dwarf2_per_cu_data *per_cu,
9536 enum language pretend_language)
9537{
9538 /* We may arrive here during partial symbol reading, if we need full
9539 DIEs to process an unusual case (e.g. template arguments). Do
9540 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9541 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9542 {
9543 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9544 return 1;
9545 return 0;
9546 }
9547
9548 /* Mark the dependence relation so that we don't flush PER_CU
9549 too early. */
89e63ee4
DE
9550 if (dependent_cu != NULL)
9551 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9552
9553 /* If it's already on the queue, we have nothing to do. */
9554 if (per_cu->queued)
9555 return 0;
9556
9557 /* If the compilation unit is already loaded, just mark it as
9558 used. */
9559 if (per_cu->cu != NULL)
9560 {
9561 per_cu->cu->last_used = 0;
9562 return 0;
9563 }
9564
9565 /* Add it to the queue. */
9566 queue_comp_unit (per_cu, pretend_language);
9567
9568 return 1;
9569}
9570
10b3939b
DJ
9571/* Process the queue. */
9572
9573static void
ed2dc618 9574process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9575{
9576 struct dwarf2_queue_item *item, *next_item;
9577
b4f54984 9578 if (dwarf_read_debug)
45cfd468
DE
9579 {
9580 fprintf_unfiltered (gdb_stdlog,
9581 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9582 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9583 }
9584
03dd20cc
DJ
9585 /* The queue starts out with one item, but following a DIE reference
9586 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9587 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9588 {
cc12ce38
DE
9589 if ((dwarf2_per_objfile->using_index
9590 ? !item->per_cu->v.quick->compunit_symtab
9591 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9592 /* Skip dummy CUs. */
9593 && item->per_cu->cu != NULL)
f4dc4d17
DE
9594 {
9595 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9596 unsigned int debug_print_threshold;
247f5c4f 9597 char buf[100];
f4dc4d17 9598
247f5c4f 9599 if (per_cu->is_debug_types)
f4dc4d17 9600 {
247f5c4f
DE
9601 struct signatured_type *sig_type =
9602 (struct signatured_type *) per_cu;
9603
9d8780f0 9604 sprintf (buf, "TU %s at offset %s",
73be47f5 9605 hex_string (sig_type->signature),
9d8780f0 9606 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9607 /* There can be 100s of TUs.
9608 Only print them in verbose mode. */
9609 debug_print_threshold = 2;
f4dc4d17 9610 }
247f5c4f 9611 else
73be47f5 9612 {
9d8780f0
SM
9613 sprintf (buf, "CU at offset %s",
9614 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9615 debug_print_threshold = 1;
9616 }
247f5c4f 9617
b4f54984 9618 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9619 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9620
9621 if (per_cu->is_debug_types)
9622 process_full_type_unit (per_cu, item->pretend_language);
9623 else
9624 process_full_comp_unit (per_cu, item->pretend_language);
9625
b4f54984 9626 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9627 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9628 }
10b3939b
DJ
9629
9630 item->per_cu->queued = 0;
9631 next_item = item->next;
9632 xfree (item);
9633 }
9634
9635 dwarf2_queue_tail = NULL;
45cfd468 9636
b4f54984 9637 if (dwarf_read_debug)
45cfd468
DE
9638 {
9639 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9640 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9641 }
10b3939b
DJ
9642}
9643
10b3939b
DJ
9644/* Read in full symbols for PST, and anything it depends on. */
9645
c906108c 9646static void
fba45db2 9647psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9648{
10b3939b 9649 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9650 int i;
9651
95554aad
TT
9652 if (pst->readin)
9653 return;
9654
aaa75496 9655 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9656 if (!pst->dependencies[i]->readin
9657 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9658 {
9659 /* Inform about additional files that need to be read in. */
9660 if (info_verbose)
9661 {
a3f17187 9662 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9663 fputs_filtered (" ", gdb_stdout);
9664 wrap_here ("");
9665 fputs_filtered ("and ", gdb_stdout);
9666 wrap_here ("");
9667 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9668 wrap_here (""); /* Flush output. */
aaa75496
JB
9669 gdb_flush (gdb_stdout);
9670 }
9671 psymtab_to_symtab_1 (pst->dependencies[i]);
9672 }
9673
9a3c8263 9674 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9675
9676 if (per_cu == NULL)
aaa75496
JB
9677 {
9678 /* It's an include file, no symbols to read for it.
9679 Everything is in the parent symtab. */
9680 pst->readin = 1;
9681 return;
9682 }
c906108c 9683
58f0c718 9684 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9685}
9686
dee91e82
DE
9687/* Trivial hash function for die_info: the hash value of a DIE
9688 is its offset in .debug_info for this objfile. */
10b3939b 9689
dee91e82
DE
9690static hashval_t
9691die_hash (const void *item)
10b3939b 9692{
9a3c8263 9693 const struct die_info *die = (const struct die_info *) item;
6502dd73 9694
9c541725 9695 return to_underlying (die->sect_off);
dee91e82 9696}
63d06c5c 9697
dee91e82
DE
9698/* Trivial comparison function for die_info structures: two DIEs
9699 are equal if they have the same offset. */
98bfdba5 9700
dee91e82
DE
9701static int
9702die_eq (const void *item_lhs, const void *item_rhs)
9703{
9a3c8263
SM
9704 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9705 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9706
9c541725 9707 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9708}
c906108c 9709
dee91e82
DE
9710/* die_reader_func for load_full_comp_unit.
9711 This is identical to read_signatured_type_reader,
9712 but is kept separate for now. */
c906108c 9713
dee91e82
DE
9714static void
9715load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9716 const gdb_byte *info_ptr,
dee91e82
DE
9717 struct die_info *comp_unit_die,
9718 int has_children,
9719 void *data)
9720{
9721 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9722 enum language *language_ptr = (enum language *) data;
6caca83c 9723
dee91e82
DE
9724 gdb_assert (cu->die_hash == NULL);
9725 cu->die_hash =
9726 htab_create_alloc_ex (cu->header.length / 12,
9727 die_hash,
9728 die_eq,
9729 NULL,
9730 &cu->comp_unit_obstack,
9731 hashtab_obstack_allocate,
9732 dummy_obstack_deallocate);
e142c38c 9733
dee91e82
DE
9734 if (has_children)
9735 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9736 &info_ptr, comp_unit_die);
9737 cu->dies = comp_unit_die;
9738 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9739
9740 /* We try not to read any attributes in this function, because not
9cdd5dbd 9741 all CUs needed for references have been loaded yet, and symbol
10b3939b 9742 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9743 or we won't be able to build types correctly.
9744 Similarly, if we do not read the producer, we can not apply
9745 producer-specific interpretation. */
95554aad 9746 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9747}
10b3939b 9748
dee91e82 9749/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9750
dee91e82 9751static void
95554aad 9752load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9753 bool skip_partial,
95554aad 9754 enum language pretend_language)
dee91e82 9755{
3019eac3 9756 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9757
58f0c718 9758 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9759 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9760}
9761
3da10d80
KS
9762/* Add a DIE to the delayed physname list. */
9763
9764static void
9765add_to_method_list (struct type *type, int fnfield_index, int index,
9766 const char *name, struct die_info *die,
9767 struct dwarf2_cu *cu)
9768{
9769 struct delayed_method_info mi;
9770 mi.type = type;
9771 mi.fnfield_index = fnfield_index;
9772 mi.index = index;
9773 mi.name = name;
9774 mi.die = die;
c89b44cd 9775 cu->method_list.push_back (mi);
3da10d80
KS
9776}
9777
3693fdb3
PA
9778/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9779 "const" / "volatile". If so, decrements LEN by the length of the
9780 modifier and return true. Otherwise return false. */
9781
9782template<size_t N>
9783static bool
9784check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9785{
9786 size_t mod_len = sizeof (mod) - 1;
9787 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9788 {
9789 len -= mod_len;
9790 return true;
9791 }
9792 return false;
9793}
9794
3da10d80
KS
9795/* Compute the physnames of any methods on the CU's method list.
9796
9797 The computation of method physnames is delayed in order to avoid the
9798 (bad) condition that one of the method's formal parameters is of an as yet
9799 incomplete type. */
9800
9801static void
9802compute_delayed_physnames (struct dwarf2_cu *cu)
9803{
3693fdb3 9804 /* Only C++ delays computing physnames. */
c89b44cd 9805 if (cu->method_list.empty ())
3693fdb3
PA
9806 return;
9807 gdb_assert (cu->language == language_cplus);
9808
52941706 9809 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9810 {
1d06ead6 9811 const char *physname;
3da10d80 9812 struct fn_fieldlist *fn_flp
c89b44cd
TT
9813 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9814 physname = dwarf2_physname (mi.name, mi.die, cu);
9815 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9816 = physname ? physname : "";
3693fdb3
PA
9817
9818 /* Since there's no tag to indicate whether a method is a
9819 const/volatile overload, extract that information out of the
9820 demangled name. */
9821 if (physname != NULL)
9822 {
9823 size_t len = strlen (physname);
9824
9825 while (1)
9826 {
9827 if (physname[len] == ')') /* shortcut */
9828 break;
9829 else if (check_modifier (physname, len, " const"))
c89b44cd 9830 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9831 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9832 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9833 else
9834 break;
9835 }
9836 }
3da10d80 9837 }
c89b44cd
TT
9838
9839 /* The list is no longer needed. */
9840 cu->method_list.clear ();
3da10d80
KS
9841}
9842
a766d390
DE
9843/* Go objects should be embedded in a DW_TAG_module DIE,
9844 and it's not clear if/how imported objects will appear.
9845 To keep Go support simple until that's worked out,
9846 go back through what we've read and create something usable.
9847 We could do this while processing each DIE, and feels kinda cleaner,
9848 but that way is more invasive.
9849 This is to, for example, allow the user to type "p var" or "b main"
9850 without having to specify the package name, and allow lookups
9851 of module.object to work in contexts that use the expression
9852 parser. */
9853
9854static void
9855fixup_go_packaging (struct dwarf2_cu *cu)
9856{
9857 char *package_name = NULL;
9858 struct pending *list;
9859 int i;
9860
c24bdb02 9861 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9862 list != NULL;
9863 list = list->next)
a766d390
DE
9864 {
9865 for (i = 0; i < list->nsyms; ++i)
9866 {
9867 struct symbol *sym = list->symbol[i];
9868
9869 if (SYMBOL_LANGUAGE (sym) == language_go
9870 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9871 {
9872 char *this_package_name = go_symbol_package_name (sym);
9873
9874 if (this_package_name == NULL)
9875 continue;
9876 if (package_name == NULL)
9877 package_name = this_package_name;
9878 else
9879 {
518817b3
SM
9880 struct objfile *objfile
9881 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9882 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9883 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9884 (symbol_symtab (sym) != NULL
9885 ? symtab_to_filename_for_display
9886 (symbol_symtab (sym))
e3b94546 9887 : objfile_name (objfile)),
a766d390
DE
9888 this_package_name, package_name);
9889 xfree (this_package_name);
9890 }
9891 }
9892 }
9893 }
9894
9895 if (package_name != NULL)
9896 {
518817b3 9897 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9898 const char *saved_package_name
021887d8 9899 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
19f392bc
UW
9900 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9901 saved_package_name);
a766d390
DE
9902 struct symbol *sym;
9903
e623cf5d 9904 sym = allocate_symbol (objfile);
f85f34ed 9905 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9906 SYMBOL_SET_NAMES (sym, saved_package_name,
9907 strlen (saved_package_name), 0, objfile);
a766d390
DE
9908 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9909 e.g., "main" finds the "main" module and not C's main(). */
9910 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9911 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9912 SYMBOL_TYPE (sym) = type;
9913
c24bdb02 9914 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9915
9916 xfree (package_name);
9917 }
9918}
9919
c9317f21
TT
9920/* Allocate a fully-qualified name consisting of the two parts on the
9921 obstack. */
9922
9923static const char *
9924rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9925{
9926 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9927}
9928
9929/* A helper that allocates a struct discriminant_info to attach to a
9930 union type. */
9931
9932static struct discriminant_info *
9933alloc_discriminant_info (struct type *type, int discriminant_index,
9934 int default_index)
9935{
9936 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9937 gdb_assert (discriminant_index == -1
9938 || (discriminant_index >= 0
9939 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9940 gdb_assert (default_index == -1
c7b15a66 9941 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9942
9943 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9944
9945 struct discriminant_info *disc
9946 = ((struct discriminant_info *)
9947 TYPE_ZALLOC (type,
9948 offsetof (struct discriminant_info, discriminants)
9949 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9950 disc->default_index = default_index;
9951 disc->discriminant_index = discriminant_index;
9952
9953 struct dynamic_prop prop;
9954 prop.kind = PROP_UNDEFINED;
9955 prop.data.baton = disc;
9956
9957 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9958
9959 return disc;
9960}
9961
9962/* Some versions of rustc emitted enums in an unusual way.
9963
9964 Ordinary enums were emitted as unions. The first element of each
9965 structure in the union was named "RUST$ENUM$DISR". This element
9966 held the discriminant.
9967
9968 These versions of Rust also implemented the "non-zero"
9969 optimization. When the enum had two values, and one is empty and
9970 the other holds a pointer that cannot be zero, the pointer is used
9971 as the discriminant, with a zero value meaning the empty variant.
9972 Here, the union's first member is of the form
9973 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9974 where the fieldnos are the indices of the fields that should be
9975 traversed in order to find the field (which may be several fields deep)
9976 and the variantname is the name of the variant of the case when the
9977 field is zero.
9978
9979 This function recognizes whether TYPE is of one of these forms,
9980 and, if so, smashes it to be a variant type. */
9981
9982static void
9983quirk_rust_enum (struct type *type, struct objfile *objfile)
9984{
9985 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9986
9987 /* We don't need to deal with empty enums. */
9988 if (TYPE_NFIELDS (type) == 0)
9989 return;
9990
9991#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9992 if (TYPE_NFIELDS (type) == 1
9993 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9994 {
9995 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9996
9997 /* Decode the field name to find the offset of the
9998 discriminant. */
9999 ULONGEST bit_offset = 0;
10000 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10001 while (name[0] >= '0' && name[0] <= '9')
10002 {
10003 char *tail;
10004 unsigned long index = strtoul (name, &tail, 10);
10005 name = tail;
10006 if (*name != '$'
10007 || index >= TYPE_NFIELDS (field_type)
10008 || (TYPE_FIELD_LOC_KIND (field_type, index)
10009 != FIELD_LOC_KIND_BITPOS))
10010 {
b98664d3 10011 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10012 "[in module %s]"),
10013 TYPE_FIELD_NAME (type, 0),
10014 objfile_name (objfile));
10015 return;
10016 }
10017 ++name;
10018
10019 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10020 field_type = TYPE_FIELD_TYPE (field_type, index);
10021 }
10022
10023 /* Make a union to hold the variants. */
10024 struct type *union_type = alloc_type (objfile);
10025 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10026 TYPE_NFIELDS (union_type) = 3;
10027 TYPE_FIELDS (union_type)
10028 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10029 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10030 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10031
10032 /* Put the discriminant must at index 0. */
10033 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10034 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10035 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10036 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10037
10038 /* The order of fields doesn't really matter, so put the real
10039 field at index 1 and the data-less field at index 2. */
10040 struct discriminant_info *disc
10041 = alloc_discriminant_info (union_type, 0, 1);
10042 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10043 TYPE_FIELD_NAME (union_type, 1)
10044 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10045 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10046 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10047 TYPE_FIELD_NAME (union_type, 1));
10048
10049 const char *dataless_name
10050 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10051 name);
10052 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10053 dataless_name);
10054 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10055 /* NAME points into the original discriminant name, which
10056 already has the correct lifetime. */
10057 TYPE_FIELD_NAME (union_type, 2) = name;
10058 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10059 disc->discriminants[2] = 0;
10060
10061 /* Smash this type to be a structure type. We have to do this
10062 because the type has already been recorded. */
10063 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10064 TYPE_NFIELDS (type) = 1;
10065 TYPE_FIELDS (type)
10066 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10067
10068 /* Install the variant part. */
10069 TYPE_FIELD_TYPE (type, 0) = union_type;
10070 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10071 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10072 }
77c2dba3
TT
10073 /* A union with a single anonymous field is probably an old-style
10074 univariant enum. */
10075 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10076 {
c9317f21
TT
10077 /* Smash this type to be a structure type. We have to do this
10078 because the type has already been recorded. */
10079 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10080
10081 /* Make a union to hold the variants. */
10082 struct type *union_type = alloc_type (objfile);
10083 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10084 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10085 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10086 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10087 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10088
10089 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10090 const char *variant_name
10091 = rust_last_path_segment (TYPE_NAME (field_type));
10092 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10093 TYPE_NAME (field_type)
10094 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10095 TYPE_NAME (type), variant_name);
c9317f21
TT
10096
10097 /* Install the union in the outer struct type. */
10098 TYPE_NFIELDS (type) = 1;
10099 TYPE_FIELDS (type)
10100 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10101 TYPE_FIELD_TYPE (type, 0) = union_type;
10102 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10103 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10104
10105 alloc_discriminant_info (union_type, -1, 0);
10106 }
10107 else
10108 {
10109 struct type *disr_type = nullptr;
10110 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10111 {
10112 disr_type = TYPE_FIELD_TYPE (type, i);
10113
a037790e
TT
10114 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10115 {
10116 /* All fields of a true enum will be structs. */
10117 return;
10118 }
10119 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10120 {
10121 /* Could be data-less variant, so keep going. */
a037790e 10122 disr_type = nullptr;
c9317f21
TT
10123 }
10124 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10125 "RUST$ENUM$DISR") != 0)
10126 {
10127 /* Not a Rust enum. */
10128 return;
10129 }
10130 else
10131 {
10132 /* Found one. */
10133 break;
10134 }
10135 }
10136
10137 /* If we got here without a discriminant, then it's probably
10138 just a union. */
10139 if (disr_type == nullptr)
10140 return;
10141
10142 /* Smash this type to be a structure type. We have to do this
10143 because the type has already been recorded. */
10144 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10145
10146 /* Make a union to hold the variants. */
10147 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10148 struct type *union_type = alloc_type (objfile);
10149 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10150 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10151 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10152 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10153 TYPE_FIELDS (union_type)
10154 = (struct field *) TYPE_ZALLOC (union_type,
10155 (TYPE_NFIELDS (union_type)
10156 * sizeof (struct field)));
10157
10158 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10159 TYPE_NFIELDS (type) * sizeof (struct field));
10160
10161 /* Install the discriminant at index 0 in the union. */
10162 TYPE_FIELD (union_type, 0) = *disr_field;
10163 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10164 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10165
10166 /* Install the union in the outer struct type. */
10167 TYPE_FIELD_TYPE (type, 0) = union_type;
10168 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10169 TYPE_NFIELDS (type) = 1;
10170
10171 /* Set the size and offset of the union type. */
10172 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10173
10174 /* We need a way to find the correct discriminant given a
10175 variant name. For convenience we build a map here. */
10176 struct type *enum_type = FIELD_TYPE (*disr_field);
10177 std::unordered_map<std::string, ULONGEST> discriminant_map;
10178 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10179 {
10180 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10181 {
10182 const char *name
10183 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10184 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10185 }
10186 }
10187
10188 int n_fields = TYPE_NFIELDS (union_type);
10189 struct discriminant_info *disc
10190 = alloc_discriminant_info (union_type, 0, -1);
10191 /* Skip the discriminant here. */
10192 for (int i = 1; i < n_fields; ++i)
10193 {
10194 /* Find the final word in the name of this variant's type.
10195 That name can be used to look up the correct
10196 discriminant. */
10197 const char *variant_name
10198 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10199 i)));
10200
10201 auto iter = discriminant_map.find (variant_name);
10202 if (iter != discriminant_map.end ())
10203 disc->discriminants[i] = iter->second;
10204
bedda9ac 10205 /* Remove the discriminant field, if it exists. */
c9317f21 10206 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10207 if (TYPE_NFIELDS (sub_type) > 0)
10208 {
10209 --TYPE_NFIELDS (sub_type);
10210 ++TYPE_FIELDS (sub_type);
10211 }
c9317f21
TT
10212 TYPE_FIELD_NAME (union_type, i) = variant_name;
10213 TYPE_NAME (sub_type)
10214 = rust_fully_qualify (&objfile->objfile_obstack,
10215 TYPE_NAME (type), variant_name);
10216 }
10217 }
10218}
10219
10220/* Rewrite some Rust unions to be structures with variants parts. */
10221
10222static void
10223rust_union_quirks (struct dwarf2_cu *cu)
10224{
10225 gdb_assert (cu->language == language_rust);
52941706
SM
10226 for (type *type_ : cu->rust_unions)
10227 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10228 /* We don't need this any more. */
10229 cu->rust_unions.clear ();
c9317f21
TT
10230}
10231
95554aad
TT
10232/* Return the symtab for PER_CU. This works properly regardless of
10233 whether we're using the index or psymtabs. */
10234
43f3e411
DE
10235static struct compunit_symtab *
10236get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10237{
ed2dc618 10238 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10239 ? per_cu->v.quick->compunit_symtab
10240 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10241}
10242
10243/* A helper function for computing the list of all symbol tables
10244 included by PER_CU. */
10245
10246static void
4c39bc03 10247recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10248 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10249 struct dwarf2_per_cu_data *per_cu,
43f3e411 10250 struct compunit_symtab *immediate_parent)
95554aad
TT
10251{
10252 void **slot;
43f3e411 10253 struct compunit_symtab *cust;
95554aad
TT
10254
10255 slot = htab_find_slot (all_children, per_cu, INSERT);
10256 if (*slot != NULL)
10257 {
10258 /* This inclusion and its children have been processed. */
10259 return;
10260 }
10261
10262 *slot = per_cu;
10263 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10264 cust = get_compunit_symtab (per_cu);
10265 if (cust != NULL)
ec94af83
DE
10266 {
10267 /* If this is a type unit only add its symbol table if we haven't
10268 seen it yet (type unit per_cu's can share symtabs). */
10269 if (per_cu->is_debug_types)
10270 {
43f3e411 10271 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10272 if (*slot == NULL)
10273 {
43f3e411 10274 *slot = cust;
4c39bc03 10275 result->push_back (cust);
43f3e411
DE
10276 if (cust->user == NULL)
10277 cust->user = immediate_parent;
ec94af83
DE
10278 }
10279 }
10280 else
f9125b6c 10281 {
4c39bc03 10282 result->push_back (cust);
43f3e411
DE
10283 if (cust->user == NULL)
10284 cust->user = immediate_parent;
f9125b6c 10285 }
ec94af83 10286 }
95554aad 10287
ae640021
AB
10288 if (!per_cu->imported_symtabs_empty ())
10289 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10290 {
10291 recursively_compute_inclusions (result, all_children,
10292 all_type_symtabs, ptr, cust);
10293 }
95554aad
TT
10294}
10295
43f3e411 10296/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10297 PER_CU. */
10298
10299static void
43f3e411 10300compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10301{
f4dc4d17
DE
10302 gdb_assert (! per_cu->is_debug_types);
10303
ae640021 10304 if (!per_cu->imported_symtabs_empty ())
95554aad 10305 {
ae640021 10306 int len;
4c39bc03 10307 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10308 htab_t all_children, all_type_symtabs;
43f3e411 10309 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10310
10311 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10312 if (cust == NULL)
95554aad
TT
10313 return;
10314
10315 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10316 NULL, xcalloc, xfree);
ec94af83
DE
10317 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10318 NULL, xcalloc, xfree);
95554aad 10319
ae640021 10320 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10321 {
10322 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10323 all_type_symtabs, ptr, cust);
ec94af83 10324 }
95554aad 10325
ec94af83 10326 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10327 len = result_symtabs.size ();
43f3e411 10328 cust->includes
ed2dc618 10329 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10330 struct compunit_symtab *, len + 1);
4c39bc03
TT
10331 memcpy (cust->includes, result_symtabs.data (),
10332 len * sizeof (compunit_symtab *));
43f3e411 10333 cust->includes[len] = NULL;
95554aad 10334
95554aad 10335 htab_delete (all_children);
ec94af83 10336 htab_delete (all_type_symtabs);
95554aad
TT
10337 }
10338}
10339
10340/* Compute the 'includes' field for the symtabs of all the CUs we just
10341 read. */
10342
10343static void
ed2dc618 10344process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10345{
71b73764 10346 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10347 {
10348 if (! iter->is_debug_types)
43f3e411 10349 compute_compunit_symtab_includes (iter);
f4dc4d17 10350 }
95554aad 10351
c5d0225d 10352 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10353}
10354
9cdd5dbd 10355/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10356 already been loaded into memory. */
10357
10358static void
95554aad
TT
10359process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10360 enum language pretend_language)
10b3939b 10361{
10b3939b 10362 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10363 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10364 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10365 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10366 CORE_ADDR lowpc, highpc;
43f3e411 10367 struct compunit_symtab *cust;
10b3939b 10368 CORE_ADDR baseaddr;
4359dff1 10369 struct block *static_block;
3e29f34a 10370 CORE_ADDR addr;
10b3939b
DJ
10371
10372 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10373
c89b44cd
TT
10374 /* Clear the list here in case something was left over. */
10375 cu->method_list.clear ();
10b3939b 10376
95554aad
TT
10377 cu->language = pretend_language;
10378 cu->language_defn = language_def (cu->language);
10379
c906108c 10380 /* Do line number decoding in read_file_scope () */
10b3939b 10381 process_die (cu->dies, cu);
c906108c 10382
a766d390
DE
10383 /* For now fudge the Go package. */
10384 if (cu->language == language_go)
10385 fixup_go_packaging (cu);
10386
5f48f8f3 10387 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10388 should be complete, and it should now be safe to compute all of the
10389 physnames. */
10390 compute_delayed_physnames (cu);
3da10d80 10391
c9317f21
TT
10392 if (cu->language == language_rust)
10393 rust_union_quirks (cu);
10394
fae299cd
DC
10395 /* Some compilers don't define a DW_AT_high_pc attribute for the
10396 compilation unit. If the DW_AT_high_pc is missing, synthesize
10397 it, by scanning the DIE's below the compilation unit. */
10b3939b 10398 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10399
3e29f34a 10400 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10401 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10402
10403 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10404 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10405 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10406 addrmap to help ensure it has an accurate map of pc values belonging to
10407 this comp unit. */
10408 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10409
c24bdb02 10410 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10411 SECT_OFF_TEXT (objfile),
10412 0);
c906108c 10413
43f3e411 10414 if (cust != NULL)
c906108c 10415 {
df15bd07 10416 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10417
8be455d7
JK
10418 /* Set symtab language to language from DW_AT_language. If the
10419 compilation is from a C file generated by language preprocessors, do
10420 not set the language if it was already deduced by start_subfile. */
43f3e411 10421 if (!(cu->language == language_c
40e3ad0e 10422 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10423 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10424
10425 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10426 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10427 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10428 there were bugs in prologue debug info, fixed later in GCC-4.5
10429 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10430
10431 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10432 needed, it would be wrong due to missing DW_AT_producer there.
10433
10434 Still one can confuse GDB by using non-standard GCC compilation
10435 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10436 */
ab260dad 10437 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10438 cust->locations_valid = 1;
e0d00bc7
JK
10439
10440 if (gcc_4_minor >= 5)
43f3e411 10441 cust->epilogue_unwind_valid = 1;
96408a79 10442
43f3e411 10443 cust->call_site_htab = cu->call_site_htab;
c906108c 10444 }
9291a0cd
TT
10445
10446 if (dwarf2_per_objfile->using_index)
43f3e411 10447 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10448 else
10449 {
10450 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10451 pst->compunit_symtab = cust;
9291a0cd
TT
10452 pst->readin = 1;
10453 }
c906108c 10454
95554aad 10455 /* Push it for inclusion processing later. */
c5d0225d 10456 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10457
10458 /* Not needed any more. */
c24bdb02 10459 cu->reset_builder ();
f4dc4d17 10460}
45cfd468 10461
f4dc4d17
DE
10462/* Generate full symbol information for type unit PER_CU, whose DIEs have
10463 already been loaded into memory. */
10464
10465static void
10466process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10467 enum language pretend_language)
10468{
10469 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10470 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10471 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10472 struct compunit_symtab *cust;
0186c6a7
DE
10473 struct signatured_type *sig_type;
10474
10475 gdb_assert (per_cu->is_debug_types);
10476 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10477
c89b44cd
TT
10478 /* Clear the list here in case something was left over. */
10479 cu->method_list.clear ();
f4dc4d17 10480
f4dc4d17
DE
10481 cu->language = pretend_language;
10482 cu->language_defn = language_def (cu->language);
10483
10484 /* The symbol tables are set up in read_type_unit_scope. */
10485 process_die (cu->dies, cu);
10486
10487 /* For now fudge the Go package. */
10488 if (cu->language == language_go)
10489 fixup_go_packaging (cu);
10490
5f48f8f3 10491 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10492 should be complete, and it should now be safe to compute all of the
10493 physnames. */
10494 compute_delayed_physnames (cu);
f4dc4d17 10495
c9317f21
TT
10496 if (cu->language == language_rust)
10497 rust_union_quirks (cu);
10498
f4dc4d17
DE
10499 /* TUs share symbol tables.
10500 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10501 of it with end_expandable_symtab. Otherwise, complete the addition of
10502 this TU's symbols to the existing symtab. */
43f3e411 10503 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10504 {
c24bdb02
KS
10505 buildsym_compunit *builder = cu->get_builder ();
10506 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10507 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10508
43f3e411 10509 if (cust != NULL)
f4dc4d17
DE
10510 {
10511 /* Set symtab language to language from DW_AT_language. If the
10512 compilation is from a C file generated by language preprocessors,
10513 do not set the language if it was already deduced by
10514 start_subfile. */
43f3e411
DE
10515 if (!(cu->language == language_c
10516 && COMPUNIT_FILETABS (cust)->language != language_c))
10517 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10518 }
10519 }
10520 else
10521 {
c24bdb02 10522 cu->get_builder ()->augment_type_symtab ();
43f3e411 10523 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10524 }
10525
10526 if (dwarf2_per_objfile->using_index)
43f3e411 10527 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10528 else
10529 {
10530 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10531 pst->compunit_symtab = cust;
f4dc4d17 10532 pst->readin = 1;
45cfd468 10533 }
804d2729
TT
10534
10535 /* Not needed any more. */
c24bdb02 10536 cu->reset_builder ();
c906108c
SS
10537}
10538
95554aad
TT
10539/* Process an imported unit DIE. */
10540
10541static void
10542process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10543{
10544 struct attribute *attr;
10545
f4dc4d17
DE
10546 /* For now we don't handle imported units in type units. */
10547 if (cu->per_cu->is_debug_types)
10548 {
10549 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10550 " supported in type units [in module %s]"),
518817b3 10551 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10552 }
10553
95554aad
TT
10554 attr = dwarf2_attr (die, DW_AT_import, cu);
10555 if (attr != NULL)
10556 {
9c541725
PA
10557 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10558 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10559 dwarf2_per_cu_data *per_cu
e3b94546 10560 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10561 cu->per_cu->dwarf2_per_objfile);
95554aad 10562
69d751e3 10563 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10564 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10565 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10566
ae640021 10567 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10568 }
10569}
10570
4c8aa72d
PA
10571/* RAII object that represents a process_die scope: i.e.,
10572 starts/finishes processing a DIE. */
10573class process_die_scope
adde2bff 10574{
4c8aa72d
PA
10575public:
10576 process_die_scope (die_info *die, dwarf2_cu *cu)
10577 : m_die (die), m_cu (cu)
10578 {
10579 /* We should only be processing DIEs not already in process. */
10580 gdb_assert (!m_die->in_process);
10581 m_die->in_process = true;
10582 }
8c3cb9fa 10583
4c8aa72d
PA
10584 ~process_die_scope ()
10585 {
10586 m_die->in_process = false;
10587
10588 /* If we're done processing the DIE for the CU that owns the line
10589 header, we don't need the line header anymore. */
10590 if (m_cu->line_header_die_owner == m_die)
10591 {
10592 delete m_cu->line_header;
10593 m_cu->line_header = NULL;
10594 m_cu->line_header_die_owner = NULL;
10595 }
10596 }
10597
10598private:
10599 die_info *m_die;
10600 dwarf2_cu *m_cu;
10601};
adde2bff 10602
c906108c
SS
10603/* Process a die and its children. */
10604
10605static void
e7c27a73 10606process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10607{
4c8aa72d 10608 process_die_scope scope (die, cu);
adde2bff 10609
c906108c
SS
10610 switch (die->tag)
10611 {
10612 case DW_TAG_padding:
10613 break;
10614 case DW_TAG_compile_unit:
95554aad 10615 case DW_TAG_partial_unit:
e7c27a73 10616 read_file_scope (die, cu);
c906108c 10617 break;
348e048f
DE
10618 case DW_TAG_type_unit:
10619 read_type_unit_scope (die, cu);
10620 break;
c906108c 10621 case DW_TAG_subprogram:
0a4b0913
AB
10622 /* Nested subprograms in Fortran get a prefix. */
10623 if (cu->language == language_fortran
10624 && die->parent != NULL
10625 && die->parent->tag == DW_TAG_subprogram)
10626 cu->processing_has_namespace_info = true;
10627 /* Fall through. */
c906108c 10628 case DW_TAG_inlined_subroutine:
edb3359d 10629 read_func_scope (die, cu);
c906108c
SS
10630 break;
10631 case DW_TAG_lexical_block:
14898363
L
10632 case DW_TAG_try_block:
10633 case DW_TAG_catch_block:
e7c27a73 10634 read_lexical_block_scope (die, cu);
c906108c 10635 break;
216f72a1 10636 case DW_TAG_call_site:
96408a79
SA
10637 case DW_TAG_GNU_call_site:
10638 read_call_site_scope (die, cu);
10639 break;
c906108c 10640 case DW_TAG_class_type:
680b30c7 10641 case DW_TAG_interface_type:
c906108c
SS
10642 case DW_TAG_structure_type:
10643 case DW_TAG_union_type:
134d01f1 10644 process_structure_scope (die, cu);
c906108c
SS
10645 break;
10646 case DW_TAG_enumeration_type:
134d01f1 10647 process_enumeration_scope (die, cu);
c906108c 10648 break;
134d01f1 10649
f792889a
DJ
10650 /* These dies have a type, but processing them does not create
10651 a symbol or recurse to process the children. Therefore we can
10652 read them on-demand through read_type_die. */
c906108c 10653 case DW_TAG_subroutine_type:
72019c9c 10654 case DW_TAG_set_type:
c906108c 10655 case DW_TAG_array_type:
c906108c 10656 case DW_TAG_pointer_type:
c906108c 10657 case DW_TAG_ptr_to_member_type:
c906108c 10658 case DW_TAG_reference_type:
4297a3f0 10659 case DW_TAG_rvalue_reference_type:
c906108c 10660 case DW_TAG_string_type:
c906108c 10661 break;
134d01f1 10662
c906108c 10663 case DW_TAG_base_type:
a02abb62 10664 case DW_TAG_subrange_type:
cb249c71 10665 case DW_TAG_typedef:
134d01f1
DJ
10666 /* Add a typedef symbol for the type definition, if it has a
10667 DW_AT_name. */
f792889a 10668 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10669 break;
c906108c 10670 case DW_TAG_common_block:
e7c27a73 10671 read_common_block (die, cu);
c906108c
SS
10672 break;
10673 case DW_TAG_common_inclusion:
10674 break;
d9fa45fe 10675 case DW_TAG_namespace:
9068261f 10676 cu->processing_has_namespace_info = true;
e7c27a73 10677 read_namespace (die, cu);
d9fa45fe 10678 break;
5d7cb8df 10679 case DW_TAG_module:
9068261f 10680 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10681 read_module (die, cu);
10682 break;
d9fa45fe 10683 case DW_TAG_imported_declaration:
9068261f 10684 cu->processing_has_namespace_info = true;
74921315
KS
10685 if (read_namespace_alias (die, cu))
10686 break;
86a73007
TT
10687 /* The declaration is not a global namespace alias. */
10688 /* Fall through. */
d9fa45fe 10689 case DW_TAG_imported_module:
9068261f 10690 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10691 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10692 || cu->language != language_fortran))
b98664d3 10693 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10694 dwarf_tag_name (die->tag));
10695 read_import_statement (die, cu);
d9fa45fe 10696 break;
95554aad
TT
10697
10698 case DW_TAG_imported_unit:
10699 process_imported_unit_die (die, cu);
10700 break;
10701
71a3c369
TT
10702 case DW_TAG_variable:
10703 read_variable (die, cu);
10704 break;
10705
c906108c 10706 default:
e7c27a73 10707 new_symbol (die, NULL, cu);
c906108c
SS
10708 break;
10709 }
10710}
ca69b9e6
DE
10711\f
10712/* DWARF name computation. */
c906108c 10713
94af9270
KS
10714/* A helper function for dwarf2_compute_name which determines whether DIE
10715 needs to have the name of the scope prepended to the name listed in the
10716 die. */
10717
10718static int
10719die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10720{
1c809c68
TT
10721 struct attribute *attr;
10722
94af9270
KS
10723 switch (die->tag)
10724 {
10725 case DW_TAG_namespace:
10726 case DW_TAG_typedef:
10727 case DW_TAG_class_type:
10728 case DW_TAG_interface_type:
10729 case DW_TAG_structure_type:
10730 case DW_TAG_union_type:
10731 case DW_TAG_enumeration_type:
10732 case DW_TAG_enumerator:
10733 case DW_TAG_subprogram:
08a76f8a 10734 case DW_TAG_inlined_subroutine:
94af9270 10735 case DW_TAG_member:
74921315 10736 case DW_TAG_imported_declaration:
94af9270
KS
10737 return 1;
10738
10739 case DW_TAG_variable:
c2b0a229 10740 case DW_TAG_constant:
94af9270
KS
10741 /* We only need to prefix "globally" visible variables. These include
10742 any variable marked with DW_AT_external or any variable that
10743 lives in a namespace. [Variables in anonymous namespaces
10744 require prefixing, but they are not DW_AT_external.] */
10745
10746 if (dwarf2_attr (die, DW_AT_specification, cu))
10747 {
10748 struct dwarf2_cu *spec_cu = cu;
9a619af0 10749
94af9270
KS
10750 return die_needs_namespace (die_specification (die, &spec_cu),
10751 spec_cu);
10752 }
10753
1c809c68 10754 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10755 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10756 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10757 return 0;
10758 /* A variable in a lexical block of some kind does not need a
10759 namespace, even though in C++ such variables may be external
10760 and have a mangled name. */
10761 if (die->parent->tag == DW_TAG_lexical_block
10762 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10763 || die->parent->tag == DW_TAG_catch_block
10764 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10765 return 0;
10766 return 1;
94af9270
KS
10767
10768 default:
10769 return 0;
10770 }
10771}
10772
73b9be8b
KS
10773/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10774 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10775 defined for the given DIE. */
10776
10777static struct attribute *
10778dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10779{
10780 struct attribute *attr;
10781
10782 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10783 if (attr == NULL)
10784 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10785
10786 return attr;
10787}
10788
10789/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10790 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10791 defined for the given DIE. */
10792
10793static const char *
10794dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10795{
10796 const char *linkage_name;
10797
10798 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10799 if (linkage_name == NULL)
10800 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10801
10802 return linkage_name;
10803}
10804
94af9270 10805/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10806 compute the physname for the object, which include a method's:
9c37b5ae 10807 - formal parameters (C++),
a766d390 10808 - receiver type (Go),
a766d390
DE
10809
10810 The term "physname" is a bit confusing.
10811 For C++, for example, it is the demangled name.
10812 For Go, for example, it's the mangled name.
94af9270 10813
af6b7be1
JB
10814 For Ada, return the DIE's linkage name rather than the fully qualified
10815 name. PHYSNAME is ignored..
10816
94af9270
KS
10817 The result is allocated on the objfile_obstack and canonicalized. */
10818
10819static const char *
15d034d0
TT
10820dwarf2_compute_name (const char *name,
10821 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10822 int physname)
10823{
518817b3 10824 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10825
94af9270
KS
10826 if (name == NULL)
10827 name = dwarf2_name (die, cu);
10828
2ee7123e
DE
10829 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10830 but otherwise compute it by typename_concat inside GDB.
10831 FIXME: Actually this is not really true, or at least not always true.
10832 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10833 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10834 will set the demangled name to the result of dwarf2_full_name, and it is
10835 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10836 if (cu->language == language_ada
10837 || (cu->language == language_fortran && physname))
10838 {
10839 /* For Ada unit, we prefer the linkage name over the name, as
10840 the former contains the exported name, which the user expects
10841 to be able to reference. Ideally, we want the user to be able
10842 to reference this entity using either natural or linkage name,
10843 but we haven't started looking at this enhancement yet. */
73b9be8b 10844 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10845
2ee7123e
DE
10846 if (linkage_name != NULL)
10847 return linkage_name;
f55ee35c
JK
10848 }
10849
94af9270
KS
10850 /* These are the only languages we know how to qualify names in. */
10851 if (name != NULL
9c37b5ae 10852 && (cu->language == language_cplus
c44af4eb
TT
10853 || cu->language == language_fortran || cu->language == language_d
10854 || cu->language == language_rust))
94af9270
KS
10855 {
10856 if (die_needs_namespace (die, cu))
10857 {
0d5cff50 10858 const char *prefix;
34a68019 10859 const char *canonical_name = NULL;
94af9270 10860
d7e74731
PA
10861 string_file buf;
10862
94af9270 10863 prefix = determine_prefix (die, cu);
94af9270
KS
10864 if (*prefix != '\0')
10865 {
f55ee35c
JK
10866 char *prefixed_name = typename_concat (NULL, prefix, name,
10867 physname, cu);
9a619af0 10868
d7e74731 10869 buf.puts (prefixed_name);
94af9270
KS
10870 xfree (prefixed_name);
10871 }
10872 else
d7e74731 10873 buf.puts (name);
94af9270 10874
98bfdba5
PA
10875 /* Template parameters may be specified in the DIE's DW_AT_name, or
10876 as children with DW_TAG_template_type_param or
10877 DW_TAG_value_type_param. If the latter, add them to the name
10878 here. If the name already has template parameters, then
10879 skip this step; some versions of GCC emit both, and
10880 it is more efficient to use the pre-computed name.
10881
10882 Something to keep in mind about this process: it is very
10883 unlikely, or in some cases downright impossible, to produce
10884 something that will match the mangled name of a function.
10885 If the definition of the function has the same debug info,
10886 we should be able to match up with it anyway. But fallbacks
10887 using the minimal symbol, for instance to find a method
10888 implemented in a stripped copy of libstdc++, will not work.
10889 If we do not have debug info for the definition, we will have to
10890 match them up some other way.
10891
10892 When we do name matching there is a related problem with function
10893 templates; two instantiated function templates are allowed to
10894 differ only by their return types, which we do not add here. */
10895
10896 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10897 {
10898 struct attribute *attr;
10899 struct die_info *child;
10900 int first = 1;
10901
10902 die->building_fullname = 1;
10903
10904 for (child = die->child; child != NULL; child = child->sibling)
10905 {
10906 struct type *type;
12df843f 10907 LONGEST value;
d521ce57 10908 const gdb_byte *bytes;
98bfdba5
PA
10909 struct dwarf2_locexpr_baton *baton;
10910 struct value *v;
10911
10912 if (child->tag != DW_TAG_template_type_param
10913 && child->tag != DW_TAG_template_value_param)
10914 continue;
10915
10916 if (first)
10917 {
d7e74731 10918 buf.puts ("<");
98bfdba5
PA
10919 first = 0;
10920 }
10921 else
d7e74731 10922 buf.puts (", ");
98bfdba5
PA
10923
10924 attr = dwarf2_attr (child, DW_AT_type, cu);
10925 if (attr == NULL)
10926 {
b98664d3 10927 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10928 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10929 continue;
10930 }
10931 type = die_type (child, cu);
10932
10933 if (child->tag == DW_TAG_template_type_param)
10934 {
c1ec8cea
TT
10935 c_print_type (type, "", &buf, -1, 0, cu->language,
10936 &type_print_raw_options);
98bfdba5
PA
10937 continue;
10938 }
10939
10940 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10941 if (attr == NULL)
10942 {
b98664d3 10943 complaint (_("template parameter missing "
3e43a32a 10944 "DW_AT_const_value"));
d7e74731 10945 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10946 continue;
10947 }
10948
10949 dwarf2_const_value_attr (attr, type, name,
10950 &cu->comp_unit_obstack, cu,
10951 &value, &bytes, &baton);
10952
10953 if (TYPE_NOSIGN (type))
10954 /* GDB prints characters as NUMBER 'CHAR'. If that's
10955 changed, this can use value_print instead. */
d7e74731 10956 c_printchar (value, type, &buf);
98bfdba5
PA
10957 else
10958 {
10959 struct value_print_options opts;
10960
10961 if (baton != NULL)
10962 v = dwarf2_evaluate_loc_desc (type, NULL,
10963 baton->data,
10964 baton->size,
10965 baton->per_cu);
10966 else if (bytes != NULL)
10967 {
10968 v = allocate_value (type);
10969 memcpy (value_contents_writeable (v), bytes,
10970 TYPE_LENGTH (type));
10971 }
10972 else
10973 v = value_from_longest (type, value);
10974
3e43a32a
MS
10975 /* Specify decimal so that we do not depend on
10976 the radix. */
98bfdba5
PA
10977 get_formatted_print_options (&opts, 'd');
10978 opts.raw = 1;
d7e74731 10979 value_print (v, &buf, &opts);
98bfdba5 10980 release_value (v);
98bfdba5
PA
10981 }
10982 }
10983
10984 die->building_fullname = 0;
10985
10986 if (!first)
10987 {
10988 /* Close the argument list, with a space if necessary
10989 (nested templates). */
d7e74731
PA
10990 if (!buf.empty () && buf.string ().back () == '>')
10991 buf.puts (" >");
98bfdba5 10992 else
d7e74731 10993 buf.puts (">");
98bfdba5
PA
10994 }
10995 }
10996
9c37b5ae 10997 /* For C++ methods, append formal parameter type
94af9270 10998 information, if PHYSNAME. */
6e70227d 10999
94af9270 11000 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11001 && cu->language == language_cplus)
94af9270
KS
11002 {
11003 struct type *type = read_type_die (die, cu);
11004
d7e74731 11005 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11006 &type_print_raw_options);
94af9270 11007
9c37b5ae 11008 if (cu->language == language_cplus)
94af9270 11009 {
60430eff
DJ
11010 /* Assume that an artificial first parameter is
11011 "this", but do not crash if it is not. RealView
11012 marks unnamed (and thus unused) parameters as
11013 artificial; there is no way to differentiate
11014 the two cases. */
94af9270
KS
11015 if (TYPE_NFIELDS (type) > 0
11016 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11017 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11018 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11019 0))))
d7e74731 11020 buf.puts (" const");
94af9270
KS
11021 }
11022 }
11023
d7e74731 11024 const std::string &intermediate_name = buf.string ();
94af9270
KS
11025
11026 if (cu->language == language_cplus)
34a68019 11027 canonical_name
322a8516 11028 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11029 &objfile->per_bfd->storage_obstack);
11030
11031 /* If we only computed INTERMEDIATE_NAME, or if
11032 INTERMEDIATE_NAME is already canonical, then we need to
11033 copy it to the appropriate obstack. */
322a8516 11034 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11035 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11036 intermediate_name);
34a68019
TT
11037 else
11038 name = canonical_name;
94af9270
KS
11039 }
11040 }
11041
11042 return name;
11043}
11044
0114d602
DJ
11045/* Return the fully qualified name of DIE, based on its DW_AT_name.
11046 If scope qualifiers are appropriate they will be added. The result
34a68019 11047 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11048 not have a name. NAME may either be from a previous call to
11049 dwarf2_name or NULL.
11050
9c37b5ae 11051 The output string will be canonicalized (if C++). */
0114d602
DJ
11052
11053static const char *
15d034d0 11054dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11055{
94af9270
KS
11056 return dwarf2_compute_name (name, die, cu, 0);
11057}
0114d602 11058
94af9270
KS
11059/* Construct a physname for the given DIE in CU. NAME may either be
11060 from a previous call to dwarf2_name or NULL. The result will be
11061 allocated on the objfile_objstack or NULL if the DIE does not have a
11062 name.
0114d602 11063
9c37b5ae 11064 The output string will be canonicalized (if C++). */
0114d602 11065
94af9270 11066static const char *
15d034d0 11067dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11068{
518817b3 11069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11070 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11071 int need_copy = 1;
11072
11073 /* In this case dwarf2_compute_name is just a shortcut not building anything
11074 on its own. */
11075 if (!die_needs_namespace (die, cu))
11076 return dwarf2_compute_name (name, die, cu, 1);
11077
73b9be8b 11078 mangled = dw2_linkage_name (die, cu);
900e11f9 11079
e98c9e7c
TT
11080 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11081 See https://github.com/rust-lang/rust/issues/32925. */
11082 if (cu->language == language_rust && mangled != NULL
11083 && strchr (mangled, '{') != NULL)
11084 mangled = NULL;
11085
900e11f9
JK
11086 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11087 has computed. */
791afaa2 11088 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11089 if (mangled != NULL)
900e11f9 11090 {
900e11f9 11091
59cc4834
JB
11092 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11093 {
11094 /* Do nothing (do not demangle the symbol name). */
11095 }
11096 else if (cu->language == language_go)
a766d390 11097 {
5e2db402
TT
11098 /* This is a lie, but we already lie to the caller new_symbol.
11099 new_symbol assumes we return the mangled name.
a766d390 11100 This just undoes that lie until things are cleaned up. */
a766d390
DE
11101 }
11102 else
11103 {
0eb876f5
JB
11104 /* Use DMGL_RET_DROP for C++ template functions to suppress
11105 their return type. It is easier for GDB users to search
11106 for such functions as `name(params)' than `long name(params)'.
11107 In such case the minimal symbol names do not match the full
11108 symbol names but for template functions there is never a need
11109 to look up their definition from their declaration so
11110 the only disadvantage remains the minimal symbol variant
11111 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11112 demangled.reset (gdb_demangle (mangled,
11113 (DMGL_PARAMS | DMGL_ANSI
11114 | DMGL_RET_DROP)));
a766d390 11115 }
900e11f9 11116 if (demangled)
791afaa2 11117 canon = demangled.get ();
900e11f9
JK
11118 else
11119 {
11120 canon = mangled;
11121 need_copy = 0;
11122 }
11123 }
11124
11125 if (canon == NULL || check_physname)
11126 {
11127 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11128
11129 if (canon != NULL && strcmp (physname, canon) != 0)
11130 {
11131 /* It may not mean a bug in GDB. The compiler could also
11132 compute DW_AT_linkage_name incorrectly. But in such case
11133 GDB would need to be bug-to-bug compatible. */
11134
b98664d3 11135 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11136 "(from linkage <%s>) - DIE at %s [in module %s]"),
11137 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11138 objfile_name (objfile));
900e11f9
JK
11139
11140 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11141 is available here - over computed PHYSNAME. It is safer
11142 against both buggy GDB and buggy compilers. */
11143
11144 retval = canon;
11145 }
11146 else
11147 {
11148 retval = physname;
11149 need_copy = 0;
11150 }
11151 }
11152 else
11153 retval = canon;
11154
11155 if (need_copy)
021887d8 11156 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11157
900e11f9 11158 return retval;
0114d602
DJ
11159}
11160
74921315
KS
11161/* Inspect DIE in CU for a namespace alias. If one exists, record
11162 a new symbol for it.
11163
11164 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11165
11166static int
11167read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11168{
11169 struct attribute *attr;
11170
11171 /* If the die does not have a name, this is not a namespace
11172 alias. */
11173 attr = dwarf2_attr (die, DW_AT_name, cu);
11174 if (attr != NULL)
11175 {
11176 int num;
11177 struct die_info *d = die;
11178 struct dwarf2_cu *imported_cu = cu;
11179
11180 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11181 keep inspecting DIEs until we hit the underlying import. */
11182#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11183 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11184 {
11185 attr = dwarf2_attr (d, DW_AT_import, cu);
11186 if (attr == NULL)
11187 break;
11188
11189 d = follow_die_ref (d, attr, &imported_cu);
11190 if (d->tag != DW_TAG_imported_declaration)
11191 break;
11192 }
11193
11194 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11195 {
b98664d3 11196 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11197 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11198 return 0;
11199 }
11200
11201 if (attr != NULL)
11202 {
11203 struct type *type;
9c541725 11204 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11205
9c541725 11206 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11207 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11208 {
11209 /* This declaration is a global namespace alias. Add
11210 a symbol for it whose type is the aliased namespace. */
11211 new_symbol (die, type, cu);
11212 return 1;
11213 }
11214 }
11215 }
11216
11217 return 0;
11218}
11219
22cee43f 11220/* Return the using directives repository (global or local?) to use in the
804d2729 11221 current context for CU.
22cee43f
PMR
11222
11223 For Ada, imported declarations can materialize renamings, which *may* be
11224 global. However it is impossible (for now?) in DWARF to distinguish
11225 "external" imported declarations and "static" ones. As all imported
11226 declarations seem to be static in all other languages, make them all CU-wide
11227 global only in Ada. */
11228
11229static struct using_direct **
804d2729 11230using_directives (struct dwarf2_cu *cu)
22cee43f 11231{
c24bdb02
KS
11232 if (cu->language == language_ada
11233 && cu->get_builder ()->outermost_context_p ())
11234 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11235 else
c24bdb02 11236 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11237}
11238
27aa8d6a
SW
11239/* Read the import statement specified by the given die and record it. */
11240
11241static void
11242read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11243{
518817b3 11244 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11245 struct attribute *import_attr;
32019081 11246 struct die_info *imported_die, *child_die;
de4affc9 11247 struct dwarf2_cu *imported_cu;
27aa8d6a 11248 const char *imported_name;
794684b6 11249 const char *imported_name_prefix;
13387711
SW
11250 const char *canonical_name;
11251 const char *import_alias;
11252 const char *imported_declaration = NULL;
794684b6 11253 const char *import_prefix;
eb1e02fd 11254 std::vector<const char *> excludes;
13387711 11255
27aa8d6a
SW
11256 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11257 if (import_attr == NULL)
11258 {
b98664d3 11259 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11260 dwarf_tag_name (die->tag));
11261 return;
11262 }
11263
de4affc9
CC
11264 imported_cu = cu;
11265 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11266 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11267 if (imported_name == NULL)
11268 {
11269 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11270
11271 The import in the following code:
11272 namespace A
11273 {
11274 typedef int B;
11275 }
11276
11277 int main ()
11278 {
11279 using A::B;
11280 B b;
11281 return b;
11282 }
11283
11284 ...
11285 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11286 <52> DW_AT_decl_file : 1
11287 <53> DW_AT_decl_line : 6
11288 <54> DW_AT_import : <0x75>
11289 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11290 <59> DW_AT_name : B
11291 <5b> DW_AT_decl_file : 1
11292 <5c> DW_AT_decl_line : 2
11293 <5d> DW_AT_type : <0x6e>
11294 ...
11295 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11296 <76> DW_AT_byte_size : 4
11297 <77> DW_AT_encoding : 5 (signed)
11298
11299 imports the wrong die ( 0x75 instead of 0x58 ).
11300 This case will be ignored until the gcc bug is fixed. */
11301 return;
11302 }
11303
82856980
SW
11304 /* Figure out the local name after import. */
11305 import_alias = dwarf2_name (die, cu);
27aa8d6a 11306
794684b6
SW
11307 /* Figure out where the statement is being imported to. */
11308 import_prefix = determine_prefix (die, cu);
11309
11310 /* Figure out what the scope of the imported die is and prepend it
11311 to the name of the imported die. */
de4affc9 11312 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11313
f55ee35c
JK
11314 if (imported_die->tag != DW_TAG_namespace
11315 && imported_die->tag != DW_TAG_module)
794684b6 11316 {
13387711
SW
11317 imported_declaration = imported_name;
11318 canonical_name = imported_name_prefix;
794684b6 11319 }
13387711 11320 else if (strlen (imported_name_prefix) > 0)
12aaed36 11321 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11322 imported_name_prefix,
11323 (cu->language == language_d ? "." : "::"),
11324 imported_name, (char *) NULL);
13387711
SW
11325 else
11326 canonical_name = imported_name;
794684b6 11327
32019081
JK
11328 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11329 for (child_die = die->child; child_die && child_die->tag;
11330 child_die = sibling_die (child_die))
11331 {
11332 /* DWARF-4: A Fortran use statement with a “rename list” may be
11333 represented by an imported module entry with an import attribute
11334 referring to the module and owned entries corresponding to those
11335 entities that are renamed as part of being imported. */
11336
11337 if (child_die->tag != DW_TAG_imported_declaration)
11338 {
b98664d3 11339 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11340 "- DIE at %s [in module %s]"),
11341 sect_offset_str (child_die->sect_off),
11342 objfile_name (objfile));
32019081
JK
11343 continue;
11344 }
11345
11346 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11347 if (import_attr == NULL)
11348 {
b98664d3 11349 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11350 dwarf_tag_name (child_die->tag));
11351 continue;
11352 }
11353
11354 imported_cu = cu;
11355 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11356 &imported_cu);
11357 imported_name = dwarf2_name (imported_die, imported_cu);
11358 if (imported_name == NULL)
11359 {
b98664d3 11360 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11361 "imported name - DIE at %s [in module %s]"),
11362 sect_offset_str (child_die->sect_off),
11363 objfile_name (objfile));
32019081
JK
11364 continue;
11365 }
11366
eb1e02fd 11367 excludes.push_back (imported_name);
32019081
JK
11368
11369 process_die (child_die, cu);
11370 }
11371
804d2729 11372 add_using_directive (using_directives (cu),
22cee43f
PMR
11373 import_prefix,
11374 canonical_name,
11375 import_alias,
11376 imported_declaration,
11377 excludes,
11378 0,
11379 &objfile->objfile_obstack);
27aa8d6a
SW
11380}
11381
5230b05a
WT
11382/* ICC<14 does not output the required DW_AT_declaration on incomplete
11383 types, but gives them a size of zero. Starting with version 14,
11384 ICC is compatible with GCC. */
11385
9068261f 11386static bool
5230b05a
WT
11387producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11388{
11389 if (!cu->checked_producer)
11390 check_producer (cu);
11391
11392 return cu->producer_is_icc_lt_14;
11393}
11394
eb77c9df
AB
11395/* ICC generates a DW_AT_type for C void functions. This was observed on
11396 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11397 which says that void functions should not have a DW_AT_type. */
11398
11399static bool
11400producer_is_icc (struct dwarf2_cu *cu)
11401{
11402 if (!cu->checked_producer)
11403 check_producer (cu);
11404
11405 return cu->producer_is_icc;
11406}
11407
1b80a9fa
JK
11408/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11409 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11410 this, it was first present in GCC release 4.3.0. */
11411
9068261f 11412static bool
1b80a9fa
JK
11413producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11414{
11415 if (!cu->checked_producer)
11416 check_producer (cu);
11417
11418 return cu->producer_is_gcc_lt_4_3;
11419}
11420
d721ba37
PA
11421static file_and_directory
11422find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11423{
d721ba37
PA
11424 file_and_directory res;
11425
9291a0cd
TT
11426 /* Find the filename. Do not use dwarf2_name here, since the filename
11427 is not a source language identifier. */
d721ba37
PA
11428 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11429 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11430
d721ba37
PA
11431 if (res.comp_dir == NULL
11432 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11433 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11434 {
d721ba37
PA
11435 res.comp_dir_storage = ldirname (res.name);
11436 if (!res.comp_dir_storage.empty ())
11437 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11438 }
d721ba37 11439 if (res.comp_dir != NULL)
9291a0cd
TT
11440 {
11441 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11442 directory, get rid of it. */
d721ba37 11443 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11444
d721ba37
PA
11445 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11446 res.comp_dir = cp + 1;
9291a0cd
TT
11447 }
11448
d721ba37
PA
11449 if (res.name == NULL)
11450 res.name = "<unknown>";
11451
11452 return res;
9291a0cd
TT
11453}
11454
f4dc4d17
DE
11455/* Handle DW_AT_stmt_list for a compilation unit.
11456 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11457 COMP_DIR is the compilation directory. LOWPC is passed to
11458 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11459
11460static void
11461handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11462 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11463{
518817b3
SM
11464 struct dwarf2_per_objfile *dwarf2_per_objfile
11465 = cu->per_cu->dwarf2_per_objfile;
527f3840 11466 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11467 struct attribute *attr;
527f3840
JK
11468 struct line_header line_header_local;
11469 hashval_t line_header_local_hash;
527f3840
JK
11470 void **slot;
11471 int decode_mapping;
2ab95328 11472
f4dc4d17
DE
11473 gdb_assert (! cu->per_cu->is_debug_types);
11474
2ab95328 11475 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11476 if (attr == NULL)
11477 return;
11478
9c541725 11479 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11480
11481 /* The line header hash table is only created if needed (it exists to
11482 prevent redundant reading of the line table for partial_units).
11483 If we're given a partial_unit, we'll need it. If we're given a
11484 compile_unit, then use the line header hash table if it's already
11485 created, but don't create one just yet. */
11486
11487 if (dwarf2_per_objfile->line_header_hash == NULL
11488 && die->tag == DW_TAG_partial_unit)
2ab95328 11489 {
527f3840
JK
11490 dwarf2_per_objfile->line_header_hash
11491 = htab_create_alloc_ex (127, line_header_hash_voidp,
11492 line_header_eq_voidp,
11493 free_line_header_voidp,
11494 &objfile->objfile_obstack,
11495 hashtab_obstack_allocate,
11496 dummy_obstack_deallocate);
11497 }
2ab95328 11498
9c541725 11499 line_header_local.sect_off = line_offset;
527f3840
JK
11500 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11501 line_header_local_hash = line_header_hash (&line_header_local);
11502 if (dwarf2_per_objfile->line_header_hash != NULL)
11503 {
11504 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11505 &line_header_local,
11506 line_header_local_hash, NO_INSERT);
11507
11508 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11509 is not present in *SLOT (since if there is something in *SLOT then
11510 it will be for a partial_unit). */
11511 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11512 {
527f3840 11513 gdb_assert (*slot != NULL);
9a3c8263 11514 cu->line_header = (struct line_header *) *slot;
527f3840 11515 return;
dee91e82 11516 }
2ab95328 11517 }
527f3840
JK
11518
11519 /* dwarf_decode_line_header does not yet provide sufficient information.
11520 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11521 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11522 if (lh == NULL)
527f3840 11523 return;
4c8aa72d
PA
11524
11525 cu->line_header = lh.release ();
11526 cu->line_header_die_owner = die;
527f3840
JK
11527
11528 if (dwarf2_per_objfile->line_header_hash == NULL)
11529 slot = NULL;
11530 else
11531 {
11532 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11533 &line_header_local,
11534 line_header_local_hash, INSERT);
11535 gdb_assert (slot != NULL);
11536 }
11537 if (slot != NULL && *slot == NULL)
11538 {
11539 /* This newly decoded line number information unit will be owned
11540 by line_header_hash hash table. */
11541 *slot = cu->line_header;
4c8aa72d 11542 cu->line_header_die_owner = NULL;
527f3840
JK
11543 }
11544 else
11545 {
11546 /* We cannot free any current entry in (*slot) as that struct line_header
11547 may be already used by multiple CUs. Create only temporary decoded
11548 line_header for this CU - it may happen at most once for each line
11549 number information unit. And if we're not using line_header_hash
11550 then this is what we want as well. */
11551 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11552 }
11553 decode_mapping = (die->tag != DW_TAG_partial_unit);
11554 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11555 decode_mapping);
fff8551c 11556
2ab95328
TT
11557}
11558
95554aad 11559/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11560
c906108c 11561static void
e7c27a73 11562read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11563{
518817b3
SM
11564 struct dwarf2_per_objfile *dwarf2_per_objfile
11565 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11566 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11567 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11568 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11569 CORE_ADDR highpc = ((CORE_ADDR) 0);
11570 struct attribute *attr;
c906108c 11571 struct die_info *child_die;
e142c38c 11572 CORE_ADDR baseaddr;
6e70227d 11573
380618d6 11574 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11575 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11576
fae299cd 11577 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11578
11579 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11580 from finish_block. */
2acceee2 11581 if (lowpc == ((CORE_ADDR) -1))
c906108c 11582 lowpc = highpc;
3e29f34a 11583 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11584
d721ba37 11585 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11586
f4b8a18d
KW
11587 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11588 standardised yet. As a workaround for the language detection we fall
11589 back to the DW_AT_producer string. */
11590 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11591 cu->language = language_opencl;
11592
3019eac3
DE
11593 /* Similar hack for Go. */
11594 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11595 set_cu_language (DW_LANG_Go, cu);
11596
c24bdb02 11597 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11598
11599 /* Decode line number information if present. We do this before
11600 processing child DIEs, so that the line header table is available
11601 for DW_AT_decl_file. */
d721ba37 11602 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11603
11604 /* Process all dies in compilation unit. */
11605 if (die->child != NULL)
11606 {
11607 child_die = die->child;
11608 while (child_die && child_die->tag)
11609 {
11610 process_die (child_die, cu);
11611 child_die = sibling_die (child_die);
11612 }
11613 }
11614
11615 /* Decode macro information, if present. Dwarf 2 macro information
11616 refers to information in the line number info statement program
11617 header, so we can only read it if we've read the header
11618 successfully. */
0af92d60
JK
11619 attr = dwarf2_attr (die, DW_AT_macros, cu);
11620 if (attr == NULL)
11621 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11622 if (attr && cu->line_header)
11623 {
11624 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11625 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11626
43f3e411 11627 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11628 }
11629 else
11630 {
11631 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11632 if (attr && cu->line_header)
11633 {
11634 unsigned int macro_offset = DW_UNSND (attr);
11635
43f3e411 11636 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11637 }
11638 }
3019eac3
DE
11639}
11640
c24bdb02
KS
11641void
11642dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11643{
f4dc4d17
DE
11644 struct type_unit_group *tu_group;
11645 int first_time;
3019eac3 11646 struct attribute *attr;
9c541725 11647 unsigned int i;
0186c6a7 11648 struct signatured_type *sig_type;
3019eac3 11649
f4dc4d17 11650 gdb_assert (per_cu->is_debug_types);
0186c6a7 11651 sig_type = (struct signatured_type *) per_cu;
3019eac3 11652
c24bdb02 11653 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11654
f4dc4d17 11655 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11656 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11657 if (sig_type->type_unit_group == NULL)
c24bdb02 11658 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11659 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11660
11661 /* If we've already processed this stmt_list there's no real need to
11662 do it again, we could fake it and just recreate the part we need
11663 (file name,index -> symtab mapping). If data shows this optimization
11664 is useful we can do it then. */
43f3e411 11665 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11666
11667 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11668 debug info. */
fff8551c 11669 line_header_up lh;
f4dc4d17 11670 if (attr != NULL)
3019eac3 11671 {
9c541725 11672 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11673 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11674 }
11675 if (lh == NULL)
11676 {
11677 if (first_time)
c24bdb02 11678 start_symtab ("", NULL, 0);
f4dc4d17
DE
11679 else
11680 {
11681 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11682 gdb_assert (m_builder == nullptr);
804d2729 11683 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11684 m_builder.reset (new struct buildsym_compunit
11685 (COMPUNIT_OBJFILE (cust), "",
11686 COMPUNIT_DIRNAME (cust),
11687 compunit_language (cust),
11688 0, cust));
f4dc4d17 11689 }
f4dc4d17 11690 return;
3019eac3
DE
11691 }
11692
c24bdb02
KS
11693 line_header = lh.release ();
11694 line_header_die_owner = die;
3019eac3 11695
f4dc4d17
DE
11696 if (first_time)
11697 {
c24bdb02 11698 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11699
1fd60fc0
DE
11700 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11701 still initializing it, and our caller (a few levels up)
11702 process_full_type_unit still needs to know if this is the first
11703 time. */
11704
c24bdb02 11705 tu_group->num_symtabs = line_header->file_names.size ();
4c8aa72d 11706 tu_group->symtabs = XNEWVEC (struct symtab *,
c24bdb02 11707 line_header->file_names.size ());
3019eac3 11708
c24bdb02 11709 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11710 {
c24bdb02 11711 file_entry &fe = line_header->file_names[i];
3019eac3 11712
c24bdb02
KS
11713 dwarf2_start_subfile (this, fe.name,
11714 fe.include_dir (line_header));
11715 buildsym_compunit *b = get_builder ();
11716 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11717 {
4c8aa72d
PA
11718 /* NOTE: start_subfile will recognize when it's been
11719 passed a file it has already seen. So we can't
11720 assume there's a simple mapping from
11721 cu->line_header->file_names to subfiles, plus
11722 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11723 b->get_current_subfile ()->symtab
11724 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11725 }
11726
c24bdb02 11727 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11728 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11729 }
11730 }
11731 else
3019eac3 11732 {
c24bdb02 11733 gdb_assert (m_builder == nullptr);
804d2729 11734 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11735 m_builder.reset (new struct buildsym_compunit
11736 (COMPUNIT_OBJFILE (cust), "",
11737 COMPUNIT_DIRNAME (cust),
11738 compunit_language (cust),
11739 0, cust));
f4dc4d17 11740
c24bdb02 11741 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11742 {
c24bdb02 11743 file_entry &fe = line_header->file_names[i];
f4dc4d17 11744
4c8aa72d 11745 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11746 }
3019eac3
DE
11747 }
11748
f4dc4d17
DE
11749 /* The main symtab is allocated last. Type units don't have DW_AT_name
11750 so they don't have a "real" (so to speak) symtab anyway.
11751 There is later code that will assign the main symtab to all symbols
11752 that don't have one. We need to handle the case of a symbol with a
11753 missing symtab (DW_AT_decl_file) anyway. */
11754}
3019eac3 11755
f4dc4d17
DE
11756/* Process DW_TAG_type_unit.
11757 For TUs we want to skip the first top level sibling if it's not the
11758 actual type being defined by this TU. In this case the first top
11759 level sibling is there to provide context only. */
3019eac3 11760
f4dc4d17
DE
11761static void
11762read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11763{
11764 struct die_info *child_die;
3019eac3 11765
f4dc4d17
DE
11766 prepare_one_comp_unit (cu, die, language_minimal);
11767
11768 /* Initialize (or reinitialize) the machinery for building symtabs.
11769 We do this before processing child DIEs, so that the line header table
11770 is available for DW_AT_decl_file. */
c24bdb02 11771 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11772
11773 if (die->child != NULL)
11774 {
11775 child_die = die->child;
11776 while (child_die && child_die->tag)
11777 {
11778 process_die (child_die, cu);
11779 child_die = sibling_die (child_die);
11780 }
11781 }
3019eac3
DE
11782}
11783\f
80626a55
DE
11784/* DWO/DWP files.
11785
11786 http://gcc.gnu.org/wiki/DebugFission
11787 http://gcc.gnu.org/wiki/DebugFissionDWP
11788
11789 To simplify handling of both DWO files ("object" files with the DWARF info)
11790 and DWP files (a file with the DWOs packaged up into one file), we treat
11791 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11792
11793static hashval_t
11794hash_dwo_file (const void *item)
11795{
9a3c8263 11796 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11797 hashval_t hash;
3019eac3 11798
a2ce51a0
DE
11799 hash = htab_hash_string (dwo_file->dwo_name);
11800 if (dwo_file->comp_dir != NULL)
11801 hash += htab_hash_string (dwo_file->comp_dir);
11802 return hash;
3019eac3
DE
11803}
11804
11805static int
11806eq_dwo_file (const void *item_lhs, const void *item_rhs)
11807{
9a3c8263
SM
11808 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11809 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11810
a2ce51a0
DE
11811 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11812 return 0;
11813 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11814 return lhs->comp_dir == rhs->comp_dir;
11815 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11816}
11817
11818/* Allocate a hash table for DWO files. */
11819
51ac9db5 11820static htab_up
ed2dc618 11821allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11822{
51ac9db5
SM
11823 auto delete_dwo_file = [] (void *item)
11824 {
11825 struct dwo_file *dwo_file = (struct dwo_file *) item;
11826
11827 delete dwo_file;
11828 };
11829
11830 return htab_up (htab_create_alloc_ex (41,
11831 hash_dwo_file,
11832 eq_dwo_file,
11833 delete_dwo_file,
11834 &objfile->objfile_obstack,
11835 hashtab_obstack_allocate,
11836 dummy_obstack_deallocate));
3019eac3
DE
11837}
11838
80626a55
DE
11839/* Lookup DWO file DWO_NAME. */
11840
11841static void **
ed2dc618
SM
11842lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11843 const char *dwo_name,
11844 const char *comp_dir)
80626a55
DE
11845{
11846 struct dwo_file find_entry;
11847 void **slot;
11848
11849 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11850 dwarf2_per_objfile->dwo_files
11851 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11852
0ac5b59e
DE
11853 find_entry.dwo_name = dwo_name;
11854 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11855 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11856 INSERT);
80626a55
DE
11857
11858 return slot;
11859}
11860
3019eac3
DE
11861static hashval_t
11862hash_dwo_unit (const void *item)
11863{
9a3c8263 11864 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11865
11866 /* This drops the top 32 bits of the id, but is ok for a hash. */
11867 return dwo_unit->signature;
11868}
11869
11870static int
11871eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11872{
9a3c8263
SM
11873 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11874 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11875
11876 /* The signature is assumed to be unique within the DWO file.
11877 So while object file CU dwo_id's always have the value zero,
11878 that's OK, assuming each object file DWO file has only one CU,
11879 and that's the rule for now. */
11880 return lhs->signature == rhs->signature;
11881}
11882
11883/* Allocate a hash table for DWO CUs,TUs.
11884 There is one of these tables for each of CUs,TUs for each DWO file. */
11885
11886static htab_t
11887allocate_dwo_unit_table (struct objfile *objfile)
11888{
11889 /* Start out with a pretty small number.
11890 Generally DWO files contain only one CU and maybe some TUs. */
11891 return htab_create_alloc_ex (3,
11892 hash_dwo_unit,
11893 eq_dwo_unit,
11894 NULL,
11895 &objfile->objfile_obstack,
11896 hashtab_obstack_allocate,
11897 dummy_obstack_deallocate);
11898}
11899
80626a55 11900/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11901
19c3d4c9 11902struct create_dwo_cu_data
3019eac3
DE
11903{
11904 struct dwo_file *dwo_file;
19c3d4c9 11905 struct dwo_unit dwo_unit;
3019eac3
DE
11906};
11907
19c3d4c9 11908/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11909
11910static void
19c3d4c9
DE
11911create_dwo_cu_reader (const struct die_reader_specs *reader,
11912 const gdb_byte *info_ptr,
11913 struct die_info *comp_unit_die,
11914 int has_children,
11915 void *datap)
3019eac3
DE
11916{
11917 struct dwarf2_cu *cu = reader->cu;
9c541725 11918 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11919 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11920 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11921 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11922 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11923
a084a2a6
AT
11924 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11925 if (!signature.has_value ())
3019eac3 11926 {
b98664d3 11927 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11928 " its dwo_id [in module %s]"),
9d8780f0 11929 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11930 return;
11931 }
11932
3019eac3 11933 dwo_unit->dwo_file = dwo_file;
a084a2a6 11934 dwo_unit->signature = *signature;
8a0459fd 11935 dwo_unit->section = section;
9c541725 11936 dwo_unit->sect_off = sect_off;
3019eac3
DE
11937 dwo_unit->length = cu->per_cu->length;
11938
b4f54984 11939 if (dwarf_read_debug)
9d8780f0
SM
11940 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11941 sect_offset_str (sect_off),
9c541725 11942 hex_string (dwo_unit->signature));
3019eac3
DE
11943}
11944
33c5cd75 11945/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11946 Note: This function processes DWO files only, not DWP files. */
3019eac3 11947
33c5cd75 11948static void
ed2dc618
SM
11949create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11950 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11951 htab_t &cus_htab)
3019eac3
DE
11952{
11953 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11954 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11955
33c5cd75
DB
11956 dwarf2_read_section (objfile, &section);
11957 info_ptr = section.buffer;
3019eac3
DE
11958
11959 if (info_ptr == NULL)
33c5cd75 11960 return;
3019eac3 11961
b4f54984 11962 if (dwarf_read_debug)
19c3d4c9
DE
11963 {
11964 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11965 get_section_name (&section),
11966 get_section_file_name (&section));
19c3d4c9 11967 }
3019eac3 11968
33c5cd75 11969 end_ptr = info_ptr + section.size;
3019eac3
DE
11970 while (info_ptr < end_ptr)
11971 {
11972 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11973 struct create_dwo_cu_data create_dwo_cu_data;
11974 struct dwo_unit *dwo_unit;
11975 void **slot;
11976 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11977
19c3d4c9
DE
11978 memset (&create_dwo_cu_data.dwo_unit, 0,
11979 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11980 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11981 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11982 per_cu.is_debug_types = 0;
33c5cd75
DB
11983 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11984 per_cu.section = &section;
c5ed0576 11985 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11986
11987 init_cutu_and_read_dies_no_follow (
11988 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11989 info_ptr += per_cu.length;
11990
11991 // If the unit could not be parsed, skip it.
11992 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11993 continue;
3019eac3 11994
33c5cd75
DB
11995 if (cus_htab == NULL)
11996 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11997
33c5cd75
DB
11998 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11999 *dwo_unit = create_dwo_cu_data.dwo_unit;
12000 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12001 gdb_assert (slot != NULL);
12002 if (*slot != NULL)
19c3d4c9 12003 {
33c5cd75
DB
12004 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12005 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12006
b98664d3 12007 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12008 " the entry at offset %s, signature %s"),
12009 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12010 hex_string (dwo_unit->signature));
19c3d4c9 12011 }
33c5cd75 12012 *slot = (void *)dwo_unit;
3019eac3 12013 }
3019eac3
DE
12014}
12015
80626a55
DE
12016/* DWP file .debug_{cu,tu}_index section format:
12017 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12018
d2415c6c
DE
12019 DWP Version 1:
12020
80626a55
DE
12021 Both index sections have the same format, and serve to map a 64-bit
12022 signature to a set of section numbers. Each section begins with a header,
12023 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12024 indexes, and a pool of 32-bit section numbers. The index sections will be
12025 aligned at 8-byte boundaries in the file.
12026
d2415c6c
DE
12027 The index section header consists of:
12028
12029 V, 32 bit version number
12030 -, 32 bits unused
12031 N, 32 bit number of compilation units or type units in the index
12032 M, 32 bit number of slots in the hash table
80626a55 12033
d2415c6c 12034 Numbers are recorded using the byte order of the application binary.
80626a55 12035
d2415c6c
DE
12036 The hash table begins at offset 16 in the section, and consists of an array
12037 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12038 order of the application binary). Unused slots in the hash table are 0.
12039 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12040
d2415c6c
DE
12041 The parallel table begins immediately after the hash table
12042 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12043 array of 32-bit indexes (using the byte order of the application binary),
12044 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12045 table contains a 32-bit index into the pool of section numbers. For unused
12046 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12047
73869dc2
DE
12048 The pool of section numbers begins immediately following the hash table
12049 (at offset 16 + 12 * M from the beginning of the section). The pool of
12050 section numbers consists of an array of 32-bit words (using the byte order
12051 of the application binary). Each item in the array is indexed starting
12052 from 0. The hash table entry provides the index of the first section
12053 number in the set. Additional section numbers in the set follow, and the
12054 set is terminated by a 0 entry (section number 0 is not used in ELF).
12055
12056 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12057 section must be the first entry in the set, and the .debug_abbrev.dwo must
12058 be the second entry. Other members of the set may follow in any order.
12059
12060 ---
12061
12062 DWP Version 2:
12063
12064 DWP Version 2 combines all the .debug_info, etc. sections into one,
12065 and the entries in the index tables are now offsets into these sections.
12066 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12067 section.
12068
12069 Index Section Contents:
12070 Header
12071 Hash Table of Signatures dwp_hash_table.hash_table
12072 Parallel Table of Indices dwp_hash_table.unit_table
12073 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12074 Table of Section Sizes dwp_hash_table.v2.sizes
12075
12076 The index section header consists of:
12077
12078 V, 32 bit version number
12079 L, 32 bit number of columns in the table of section offsets
12080 N, 32 bit number of compilation units or type units in the index
12081 M, 32 bit number of slots in the hash table
12082
12083 Numbers are recorded using the byte order of the application binary.
12084
12085 The hash table has the same format as version 1.
12086 The parallel table of indices has the same format as version 1,
12087 except that the entries are origin-1 indices into the table of sections
12088 offsets and the table of section sizes.
12089
12090 The table of offsets begins immediately following the parallel table
12091 (at offset 16 + 12 * M from the beginning of the section). The table is
12092 a two-dimensional array of 32-bit words (using the byte order of the
12093 application binary), with L columns and N+1 rows, in row-major order.
12094 Each row in the array is indexed starting from 0. The first row provides
12095 a key to the remaining rows: each column in this row provides an identifier
12096 for a debug section, and the offsets in the same column of subsequent rows
12097 refer to that section. The section identifiers are:
12098
12099 DW_SECT_INFO 1 .debug_info.dwo
12100 DW_SECT_TYPES 2 .debug_types.dwo
12101 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12102 DW_SECT_LINE 4 .debug_line.dwo
12103 DW_SECT_LOC 5 .debug_loc.dwo
12104 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12105 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12106 DW_SECT_MACRO 8 .debug_macro.dwo
12107
12108 The offsets provided by the CU and TU index sections are the base offsets
12109 for the contributions made by each CU or TU to the corresponding section
12110 in the package file. Each CU and TU header contains an abbrev_offset
12111 field, used to find the abbreviations table for that CU or TU within the
12112 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12113 be interpreted as relative to the base offset given in the index section.
12114 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12115 should be interpreted as relative to the base offset for .debug_line.dwo,
12116 and offsets into other debug sections obtained from DWARF attributes should
12117 also be interpreted as relative to the corresponding base offset.
12118
12119 The table of sizes begins immediately following the table of offsets.
12120 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12121 with L columns and N rows, in row-major order. Each row in the array is
12122 indexed starting from 1 (row 0 is shared by the two tables).
12123
12124 ---
12125
12126 Hash table lookup is handled the same in version 1 and 2:
12127
12128 We assume that N and M will not exceed 2^32 - 1.
12129 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12130
d2415c6c
DE
12131 Given a 64-bit compilation unit signature or a type signature S, an entry
12132 in the hash table is located as follows:
80626a55 12133
d2415c6c
DE
12134 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12135 the low-order k bits all set to 1.
80626a55 12136
d2415c6c 12137 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12138
d2415c6c
DE
12139 3) If the hash table entry at index H matches the signature, use that
12140 entry. If the hash table entry at index H is unused (all zeroes),
12141 terminate the search: the signature is not present in the table.
80626a55 12142
d2415c6c 12143 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12144
d2415c6c 12145 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12146 to stop at an unused slot or find the match. */
80626a55
DE
12147
12148/* Create a hash table to map DWO IDs to their CU/TU entry in
12149 .debug_{info,types}.dwo in DWP_FILE.
12150 Returns NULL if there isn't one.
12151 Note: This function processes DWP files only, not DWO files. */
12152
12153static struct dwp_hash_table *
ed2dc618
SM
12154create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12155 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12156{
12157 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12158 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12159 const gdb_byte *index_ptr, *index_end;
80626a55 12160 struct dwarf2_section_info *index;
73869dc2 12161 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12162 struct dwp_hash_table *htab;
12163
12164 if (is_debug_types)
12165 index = &dwp_file->sections.tu_index;
12166 else
12167 index = &dwp_file->sections.cu_index;
12168
12169 if (dwarf2_section_empty_p (index))
12170 return NULL;
12171 dwarf2_read_section (objfile, index);
12172
12173 index_ptr = index->buffer;
12174 index_end = index_ptr + index->size;
12175
12176 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12177 index_ptr += 4;
12178 if (version == 2)
12179 nr_columns = read_4_bytes (dbfd, index_ptr);
12180 else
12181 nr_columns = 0;
12182 index_ptr += 4;
80626a55
DE
12183 nr_units = read_4_bytes (dbfd, index_ptr);
12184 index_ptr += 4;
12185 nr_slots = read_4_bytes (dbfd, index_ptr);
12186 index_ptr += 4;
12187
73869dc2 12188 if (version != 1 && version != 2)
80626a55 12189 {
21aa081e 12190 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12191 " [in module %s]"),
21aa081e 12192 pulongest (version), dwp_file->name);
80626a55
DE
12193 }
12194 if (nr_slots != (nr_slots & -nr_slots))
12195 {
21aa081e 12196 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12197 " is not power of 2 [in module %s]"),
21aa081e 12198 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12199 }
12200
12201 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12202 htab->version = version;
12203 htab->nr_columns = nr_columns;
80626a55
DE
12204 htab->nr_units = nr_units;
12205 htab->nr_slots = nr_slots;
12206 htab->hash_table = index_ptr;
12207 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12208
12209 /* Exit early if the table is empty. */
12210 if (nr_slots == 0 || nr_units == 0
12211 || (version == 2 && nr_columns == 0))
12212 {
12213 /* All must be zero. */
12214 if (nr_slots != 0 || nr_units != 0
12215 || (version == 2 && nr_columns != 0))
12216 {
b98664d3 12217 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12218 " all zero [in modules %s]"),
12219 dwp_file->name);
12220 }
12221 return htab;
12222 }
12223
12224 if (version == 1)
12225 {
12226 htab->section_pool.v1.indices =
12227 htab->unit_table + sizeof (uint32_t) * nr_slots;
12228 /* It's harder to decide whether the section is too small in v1.
12229 V1 is deprecated anyway so we punt. */
12230 }
12231 else
12232 {
12233 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12234 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12235 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12236 /* Reverse map for error checking. */
12237 int ids_seen[DW_SECT_MAX + 1];
12238 int i;
12239
12240 if (nr_columns < 2)
12241 {
12242 error (_("Dwarf Error: bad DWP hash table, too few columns"
12243 " in section table [in module %s]"),
12244 dwp_file->name);
12245 }
12246 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12247 {
12248 error (_("Dwarf Error: bad DWP hash table, too many columns"
12249 " in section table [in module %s]"),
12250 dwp_file->name);
12251 }
04fd5eed
GB
12252 memset (ids, 255, sizeof_ids);
12253 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12254 for (i = 0; i < nr_columns; ++i)
12255 {
12256 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12257
12258 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12259 {
12260 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12261 " in section table [in module %s]"),
12262 id, dwp_file->name);
12263 }
12264 if (ids_seen[id] != -1)
12265 {
12266 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12267 " id %d in section table [in module %s]"),
12268 id, dwp_file->name);
12269 }
12270 ids_seen[id] = i;
12271 ids[i] = id;
12272 }
12273 /* Must have exactly one info or types section. */
12274 if (((ids_seen[DW_SECT_INFO] != -1)
12275 + (ids_seen[DW_SECT_TYPES] != -1))
12276 != 1)
12277 {
12278 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12279 " DWO info/types section [in module %s]"),
12280 dwp_file->name);
12281 }
12282 /* Must have an abbrev section. */
12283 if (ids_seen[DW_SECT_ABBREV] == -1)
12284 {
12285 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12286 " section [in module %s]"),
12287 dwp_file->name);
12288 }
12289 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12290 htab->section_pool.v2.sizes =
12291 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12292 * nr_units * nr_columns);
12293 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12294 * nr_units * nr_columns))
12295 > index_end)
12296 {
12297 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12298 " [in module %s]"),
12299 dwp_file->name);
12300 }
12301 }
80626a55
DE
12302
12303 return htab;
12304}
12305
12306/* Update SECTIONS with the data from SECTP.
12307
12308 This function is like the other "locate" section routines that are
12309 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12310 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12311
12312 The result is non-zero for success, or zero if an error was found. */
12313
12314static int
73869dc2
DE
12315locate_v1_virtual_dwo_sections (asection *sectp,
12316 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12317{
12318 const struct dwop_section_names *names = &dwop_section_names;
12319
12320 if (section_is_p (sectp->name, &names->abbrev_dwo))
12321 {
12322 /* There can be only one. */
049412e3 12323 if (sections->abbrev.s.section != NULL)
80626a55 12324 return 0;
049412e3 12325 sections->abbrev.s.section = sectp;
fd361982 12326 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12327 }
12328 else if (section_is_p (sectp->name, &names->info_dwo)
12329 || section_is_p (sectp->name, &names->types_dwo))
12330 {
12331 /* There can be only one. */
049412e3 12332 if (sections->info_or_types.s.section != NULL)
80626a55 12333 return 0;
049412e3 12334 sections->info_or_types.s.section = sectp;
fd361982 12335 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12336 }
12337 else if (section_is_p (sectp->name, &names->line_dwo))
12338 {
12339 /* There can be only one. */
049412e3 12340 if (sections->line.s.section != NULL)
80626a55 12341 return 0;
049412e3 12342 sections->line.s.section = sectp;
fd361982 12343 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12344 }
12345 else if (section_is_p (sectp->name, &names->loc_dwo))
12346 {
12347 /* There can be only one. */
049412e3 12348 if (sections->loc.s.section != NULL)
80626a55 12349 return 0;
049412e3 12350 sections->loc.s.section = sectp;
fd361982 12351 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12352 }
12353 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12354 {
12355 /* There can be only one. */
049412e3 12356 if (sections->macinfo.s.section != NULL)
80626a55 12357 return 0;
049412e3 12358 sections->macinfo.s.section = sectp;
fd361982 12359 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12360 }
12361 else if (section_is_p (sectp->name, &names->macro_dwo))
12362 {
12363 /* There can be only one. */
049412e3 12364 if (sections->macro.s.section != NULL)
80626a55 12365 return 0;
049412e3 12366 sections->macro.s.section = sectp;
fd361982 12367 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12368 }
12369 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12370 {
12371 /* There can be only one. */
049412e3 12372 if (sections->str_offsets.s.section != NULL)
80626a55 12373 return 0;
049412e3 12374 sections->str_offsets.s.section = sectp;
fd361982 12375 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12376 }
12377 else
12378 {
12379 /* No other kind of section is valid. */
12380 return 0;
12381 }
12382
12383 return 1;
12384}
12385
73869dc2
DE
12386/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12387 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12388 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12389 This is for DWP version 1 files. */
80626a55
DE
12390
12391static struct dwo_unit *
ed2dc618
SM
12392create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12393 struct dwp_file *dwp_file,
73869dc2
DE
12394 uint32_t unit_index,
12395 const char *comp_dir,
12396 ULONGEST signature, int is_debug_types)
80626a55
DE
12397{
12398 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12399 const struct dwp_hash_table *dwp_htab =
12400 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12401 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12402 const char *kind = is_debug_types ? "TU" : "CU";
12403 struct dwo_file *dwo_file;
12404 struct dwo_unit *dwo_unit;
73869dc2 12405 struct virtual_v1_dwo_sections sections;
80626a55 12406 void **dwo_file_slot;
80626a55
DE
12407 int i;
12408
73869dc2
DE
12409 gdb_assert (dwp_file->version == 1);
12410
b4f54984 12411 if (dwarf_read_debug)
80626a55 12412 {
73869dc2 12413 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12414 kind,
73869dc2 12415 pulongest (unit_index), hex_string (signature),
80626a55
DE
12416 dwp_file->name);
12417 }
12418
19ac8c2e 12419 /* Fetch the sections of this DWO unit.
80626a55
DE
12420 Put a limit on the number of sections we look for so that bad data
12421 doesn't cause us to loop forever. */
12422
73869dc2 12423#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12424 (1 /* .debug_info or .debug_types */ \
12425 + 1 /* .debug_abbrev */ \
12426 + 1 /* .debug_line */ \
12427 + 1 /* .debug_loc */ \
12428 + 1 /* .debug_str_offsets */ \
19ac8c2e 12429 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12430 + 1 /* trailing zero */)
12431
12432 memset (&sections, 0, sizeof (sections));
80626a55 12433
73869dc2 12434 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12435 {
12436 asection *sectp;
12437 uint32_t section_nr =
12438 read_4_bytes (dbfd,
73869dc2
DE
12439 dwp_htab->section_pool.v1.indices
12440 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12441
12442 if (section_nr == 0)
12443 break;
12444 if (section_nr >= dwp_file->num_sections)
12445 {
12446 error (_("Dwarf Error: bad DWP hash table, section number too large"
12447 " [in module %s]"),
12448 dwp_file->name);
12449 }
12450
12451 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12452 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12453 {
12454 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12455 " [in module %s]"),
12456 dwp_file->name);
12457 }
12458 }
12459
12460 if (i < 2
a32a8923
DE
12461 || dwarf2_section_empty_p (&sections.info_or_types)
12462 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12463 {
12464 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12465 " [in module %s]"),
12466 dwp_file->name);
12467 }
73869dc2 12468 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12469 {
12470 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12471 " [in module %s]"),
12472 dwp_file->name);
12473 }
12474
12475 /* It's easier for the rest of the code if we fake a struct dwo_file and
12476 have dwo_unit "live" in that. At least for now.
12477
12478 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12479 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12480 file, we can combine them back into a virtual DWO file to save space
12481 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12482 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12483
791afaa2
TT
12484 std::string virtual_dwo_name =
12485 string_printf ("virtual-dwo/%d-%d-%d-%d",
12486 get_section_id (&sections.abbrev),
12487 get_section_id (&sections.line),
12488 get_section_id (&sections.loc),
12489 get_section_id (&sections.str_offsets));
80626a55 12490 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12491 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12492 virtual_dwo_name.c_str (),
12493 comp_dir);
80626a55
DE
12494 /* Create one if necessary. */
12495 if (*dwo_file_slot == NULL)
12496 {
b4f54984 12497 if (dwarf_read_debug)
80626a55
DE
12498 {
12499 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12500 virtual_dwo_name.c_str ());
80626a55 12501 }
51ac9db5 12502 dwo_file = new struct dwo_file;
efba19b0
TT
12503 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12504 virtual_dwo_name);
0ac5b59e 12505 dwo_file->comp_dir = comp_dir;
80626a55
DE
12506 dwo_file->sections.abbrev = sections.abbrev;
12507 dwo_file->sections.line = sections.line;
12508 dwo_file->sections.loc = sections.loc;
12509 dwo_file->sections.macinfo = sections.macinfo;
12510 dwo_file->sections.macro = sections.macro;
12511 dwo_file->sections.str_offsets = sections.str_offsets;
12512 /* The "str" section is global to the entire DWP file. */
12513 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12514 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12515 there's no need to record it in dwo_file.
12516 Also, we can't simply record type sections in dwo_file because
12517 we record a pointer into the vector in dwo_unit. As we collect more
12518 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12519 for it, invalidating all copies of pointers into the previous
12520 contents. */
80626a55
DE
12521 *dwo_file_slot = dwo_file;
12522 }
12523 else
12524 {
b4f54984 12525 if (dwarf_read_debug)
80626a55
DE
12526 {
12527 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12528 virtual_dwo_name.c_str ());
80626a55 12529 }
9a3c8263 12530 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12531 }
80626a55
DE
12532
12533 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12534 dwo_unit->dwo_file = dwo_file;
12535 dwo_unit->signature = signature;
8d749320
SM
12536 dwo_unit->section =
12537 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12538 *dwo_unit->section = sections.info_or_types;
57d63ce2 12539 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12540
12541 return dwo_unit;
12542}
12543
73869dc2
DE
12544/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12545 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12546 piece within that section used by a TU/CU, return a virtual section
12547 of just that piece. */
12548
12549static struct dwarf2_section_info
ed2dc618
SM
12550create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12551 struct dwarf2_section_info *section,
73869dc2
DE
12552 bfd_size_type offset, bfd_size_type size)
12553{
12554 struct dwarf2_section_info result;
12555 asection *sectp;
12556
12557 gdb_assert (section != NULL);
12558 gdb_assert (!section->is_virtual);
12559
12560 memset (&result, 0, sizeof (result));
12561 result.s.containing_section = section;
dc4ccb6f 12562 result.is_virtual = true;
73869dc2
DE
12563
12564 if (size == 0)
12565 return result;
12566
12567 sectp = get_section_bfd_section (section);
12568
12569 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12570 bounds of the real section. This is a pretty-rare event, so just
12571 flag an error (easier) instead of a warning and trying to cope. */
12572 if (sectp == NULL
fd361982 12573 || offset + size > bfd_section_size (sectp))
73869dc2 12574 {
73869dc2
DE
12575 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12576 " in section %s [in module %s]"),
fd361982 12577 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12578 objfile_name (dwarf2_per_objfile->objfile));
12579 }
12580
12581 result.virtual_offset = offset;
12582 result.size = size;
12583 return result;
12584}
12585
12586/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12587 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12588 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12589 This is for DWP version 2 files. */
12590
12591static struct dwo_unit *
ed2dc618
SM
12592create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12593 struct dwp_file *dwp_file,
73869dc2
DE
12594 uint32_t unit_index,
12595 const char *comp_dir,
12596 ULONGEST signature, int is_debug_types)
12597{
12598 struct objfile *objfile = dwarf2_per_objfile->objfile;
12599 const struct dwp_hash_table *dwp_htab =
12600 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12601 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12602 const char *kind = is_debug_types ? "TU" : "CU";
12603 struct dwo_file *dwo_file;
12604 struct dwo_unit *dwo_unit;
12605 struct virtual_v2_dwo_sections sections;
12606 void **dwo_file_slot;
73869dc2
DE
12607 int i;
12608
12609 gdb_assert (dwp_file->version == 2);
12610
b4f54984 12611 if (dwarf_read_debug)
73869dc2
DE
12612 {
12613 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12614 kind,
12615 pulongest (unit_index), hex_string (signature),
12616 dwp_file->name);
12617 }
12618
12619 /* Fetch the section offsets of this DWO unit. */
12620
12621 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12622
12623 for (i = 0; i < dwp_htab->nr_columns; ++i)
12624 {
12625 uint32_t offset = read_4_bytes (dbfd,
12626 dwp_htab->section_pool.v2.offsets
12627 + (((unit_index - 1) * dwp_htab->nr_columns
12628 + i)
12629 * sizeof (uint32_t)));
12630 uint32_t size = read_4_bytes (dbfd,
12631 dwp_htab->section_pool.v2.sizes
12632 + (((unit_index - 1) * dwp_htab->nr_columns
12633 + i)
12634 * sizeof (uint32_t)));
12635
12636 switch (dwp_htab->section_pool.v2.section_ids[i])
12637 {
12638 case DW_SECT_INFO:
12639 case DW_SECT_TYPES:
12640 sections.info_or_types_offset = offset;
12641 sections.info_or_types_size = size;
12642 break;
12643 case DW_SECT_ABBREV:
12644 sections.abbrev_offset = offset;
12645 sections.abbrev_size = size;
12646 break;
12647 case DW_SECT_LINE:
12648 sections.line_offset = offset;
12649 sections.line_size = size;
12650 break;
12651 case DW_SECT_LOC:
12652 sections.loc_offset = offset;
12653 sections.loc_size = size;
12654 break;
12655 case DW_SECT_STR_OFFSETS:
12656 sections.str_offsets_offset = offset;
12657 sections.str_offsets_size = size;
12658 break;
12659 case DW_SECT_MACINFO:
12660 sections.macinfo_offset = offset;
12661 sections.macinfo_size = size;
12662 break;
12663 case DW_SECT_MACRO:
12664 sections.macro_offset = offset;
12665 sections.macro_size = size;
12666 break;
12667 }
12668 }
12669
12670 /* It's easier for the rest of the code if we fake a struct dwo_file and
12671 have dwo_unit "live" in that. At least for now.
12672
12673 The DWP file can be made up of a random collection of CUs and TUs.
12674 However, for each CU + set of TUs that came from the same original DWO
12675 file, we can combine them back into a virtual DWO file to save space
12676 (fewer struct dwo_file objects to allocate). Remember that for really
12677 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12678
791afaa2
TT
12679 std::string virtual_dwo_name =
12680 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12681 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12682 (long) (sections.line_size ? sections.line_offset : 0),
12683 (long) (sections.loc_size ? sections.loc_offset : 0),
12684 (long) (sections.str_offsets_size
12685 ? sections.str_offsets_offset : 0));
73869dc2 12686 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12687 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12688 virtual_dwo_name.c_str (),
12689 comp_dir);
73869dc2
DE
12690 /* Create one if necessary. */
12691 if (*dwo_file_slot == NULL)
12692 {
b4f54984 12693 if (dwarf_read_debug)
73869dc2
DE
12694 {
12695 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12696 virtual_dwo_name.c_str ());
73869dc2 12697 }
51ac9db5 12698 dwo_file = new struct dwo_file;
efba19b0
TT
12699 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12700 virtual_dwo_name);
73869dc2
DE
12701 dwo_file->comp_dir = comp_dir;
12702 dwo_file->sections.abbrev =
ed2dc618 12703 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12704 sections.abbrev_offset, sections.abbrev_size);
12705 dwo_file->sections.line =
ed2dc618 12706 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12707 sections.line_offset, sections.line_size);
12708 dwo_file->sections.loc =
ed2dc618 12709 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12710 sections.loc_offset, sections.loc_size);
12711 dwo_file->sections.macinfo =
ed2dc618 12712 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12713 sections.macinfo_offset, sections.macinfo_size);
12714 dwo_file->sections.macro =
ed2dc618 12715 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12716 sections.macro_offset, sections.macro_size);
12717 dwo_file->sections.str_offsets =
ed2dc618
SM
12718 create_dwp_v2_section (dwarf2_per_objfile,
12719 &dwp_file->sections.str_offsets,
73869dc2
DE
12720 sections.str_offsets_offset,
12721 sections.str_offsets_size);
12722 /* The "str" section is global to the entire DWP file. */
12723 dwo_file->sections.str = dwp_file->sections.str;
12724 /* The info or types section is assigned below to dwo_unit,
12725 there's no need to record it in dwo_file.
12726 Also, we can't simply record type sections in dwo_file because
12727 we record a pointer into the vector in dwo_unit. As we collect more
12728 types we'll grow the vector and eventually have to reallocate space
12729 for it, invalidating all copies of pointers into the previous
12730 contents. */
12731 *dwo_file_slot = dwo_file;
12732 }
12733 else
12734 {
b4f54984 12735 if (dwarf_read_debug)
73869dc2
DE
12736 {
12737 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12738 virtual_dwo_name.c_str ());
73869dc2 12739 }
9a3c8263 12740 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12741 }
73869dc2
DE
12742
12743 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12744 dwo_unit->dwo_file = dwo_file;
12745 dwo_unit->signature = signature;
8d749320
SM
12746 dwo_unit->section =
12747 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12748 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12749 is_debug_types
73869dc2
DE
12750 ? &dwp_file->sections.types
12751 : &dwp_file->sections.info,
12752 sections.info_or_types_offset,
12753 sections.info_or_types_size);
12754 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12755
12756 return dwo_unit;
12757}
12758
57d63ce2
DE
12759/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12760 Returns NULL if the signature isn't found. */
80626a55
DE
12761
12762static struct dwo_unit *
ed2dc618
SM
12763lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12764 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12765 ULONGEST signature, int is_debug_types)
80626a55 12766{
57d63ce2
DE
12767 const struct dwp_hash_table *dwp_htab =
12768 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12769 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12770 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12771 uint32_t hash = signature & mask;
12772 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12773 unsigned int i;
12774 void **slot;
870f88f7 12775 struct dwo_unit find_dwo_cu;
80626a55
DE
12776
12777 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12778 find_dwo_cu.signature = signature;
19ac8c2e
DE
12779 slot = htab_find_slot (is_debug_types
12780 ? dwp_file->loaded_tus
12781 : dwp_file->loaded_cus,
12782 &find_dwo_cu, INSERT);
80626a55
DE
12783
12784 if (*slot != NULL)
9a3c8263 12785 return (struct dwo_unit *) *slot;
80626a55
DE
12786
12787 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12788 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12789 {
12790 ULONGEST signature_in_table;
12791
12792 signature_in_table =
57d63ce2 12793 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12794 if (signature_in_table == signature)
12795 {
57d63ce2
DE
12796 uint32_t unit_index =
12797 read_4_bytes (dbfd,
12798 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12799
73869dc2
DE
12800 if (dwp_file->version == 1)
12801 {
ed2dc618
SM
12802 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12803 dwp_file, unit_index,
73869dc2
DE
12804 comp_dir, signature,
12805 is_debug_types);
12806 }
12807 else
12808 {
ed2dc618
SM
12809 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12810 dwp_file, unit_index,
73869dc2
DE
12811 comp_dir, signature,
12812 is_debug_types);
12813 }
9a3c8263 12814 return (struct dwo_unit *) *slot;
80626a55
DE
12815 }
12816 if (signature_in_table == 0)
12817 return NULL;
12818 hash = (hash + hash2) & mask;
12819 }
12820
12821 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12822 " [in module %s]"),
12823 dwp_file->name);
12824}
12825
ab5088bf 12826/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12827 Open the file specified by FILE_NAME and hand it off to BFD for
12828 preliminary analysis. Return a newly initialized bfd *, which
12829 includes a canonicalized copy of FILE_NAME.
80626a55 12830 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12831 SEARCH_CWD is true if the current directory is to be searched.
12832 It will be searched before debug-file-directory.
13aaf454
DE
12833 If successful, the file is added to the bfd include table of the
12834 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12835 If unable to find/open the file, return NULL.
3019eac3
DE
12836 NOTE: This function is derived from symfile_bfd_open. */
12837
192b62ce 12838static gdb_bfd_ref_ptr
ed2dc618
SM
12839try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12840 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12841{
24b9144d 12842 int desc;
9c02c129
DE
12843 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12844 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12845 to debug_file_directory. */
e0cc99a6 12846 const char *search_path;
9c02c129
DE
12847 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12848
e0cc99a6 12849 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12850 if (search_cwd)
12851 {
12852 if (*debug_file_directory != '\0')
e0cc99a6
TT
12853 {
12854 search_path_holder.reset (concat (".", dirname_separator_string,
12855 debug_file_directory,
12856 (char *) NULL));
12857 search_path = search_path_holder.get ();
12858 }
6ac97d4c 12859 else
e0cc99a6 12860 search_path = ".";
6ac97d4c 12861 }
9c02c129 12862 else
e0cc99a6 12863 search_path = debug_file_directory;
3019eac3 12864
24b9144d 12865 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12866 if (is_dwp)
12867 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12868
12869 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12870 desc = openp (search_path, flags, file_name,
3019eac3
DE
12871 O_RDONLY | O_BINARY, &absolute_name);
12872 if (desc < 0)
12873 return NULL;
12874
e0cc99a6
TT
12875 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12876 gnutarget, desc));
9c02c129
DE
12877 if (sym_bfd == NULL)
12878 return NULL;
192b62ce 12879 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12880
192b62ce
TT
12881 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12882 return NULL;
3019eac3 12883
13aaf454
DE
12884 /* Success. Record the bfd as having been included by the objfile's bfd.
12885 This is important because things like demangled_names_hash lives in the
12886 objfile's per_bfd space and may have references to things like symbol
12887 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12888 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12889
3019eac3
DE
12890 return sym_bfd;
12891}
12892
ab5088bf 12893/* Try to open DWO file FILE_NAME.
3019eac3
DE
12894 COMP_DIR is the DW_AT_comp_dir attribute.
12895 The result is the bfd handle of the file.
12896 If there is a problem finding or opening the file, return NULL.
12897 Upon success, the canonicalized path of the file is stored in the bfd,
12898 same as symfile_bfd_open. */
12899
192b62ce 12900static gdb_bfd_ref_ptr
ed2dc618
SM
12901open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12902 const char *file_name, const char *comp_dir)
3019eac3 12903{
80626a55 12904 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12905 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12906 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12907
12908 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12909
12910 if (comp_dir != NULL)
12911 {
b36cec19
PA
12912 char *path_to_try = concat (comp_dir, SLASH_STRING,
12913 file_name, (char *) NULL);
3019eac3
DE
12914
12915 /* NOTE: If comp_dir is a relative path, this will also try the
12916 search path, which seems useful. */
ed2dc618
SM
12917 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12918 path_to_try,
12919 0 /*is_dwp*/,
192b62ce 12920 1 /*search_cwd*/));
3019eac3
DE
12921 xfree (path_to_try);
12922 if (abfd != NULL)
12923 return abfd;
12924 }
12925
12926 /* That didn't work, try debug-file-directory, which, despite its name,
12927 is a list of paths. */
12928
12929 if (*debug_file_directory == '\0')
12930 return NULL;
12931
ed2dc618
SM
12932 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12933 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12934}
12935
80626a55
DE
12936/* This function is mapped across the sections and remembers the offset and
12937 size of each of the DWO debugging sections we are interested in. */
12938
12939static void
12940dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12941{
9a3c8263 12942 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12943 const struct dwop_section_names *names = &dwop_section_names;
12944
12945 if (section_is_p (sectp->name, &names->abbrev_dwo))
12946 {
049412e3 12947 dwo_sections->abbrev.s.section = sectp;
fd361982 12948 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12949 }
12950 else if (section_is_p (sectp->name, &names->info_dwo))
12951 {
049412e3 12952 dwo_sections->info.s.section = sectp;
fd361982 12953 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12954 }
12955 else if (section_is_p (sectp->name, &names->line_dwo))
12956 {
049412e3 12957 dwo_sections->line.s.section = sectp;
fd361982 12958 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12959 }
12960 else if (section_is_p (sectp->name, &names->loc_dwo))
12961 {
049412e3 12962 dwo_sections->loc.s.section = sectp;
fd361982 12963 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12964 }
12965 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12966 {
049412e3 12967 dwo_sections->macinfo.s.section = sectp;
fd361982 12968 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12969 }
12970 else if (section_is_p (sectp->name, &names->macro_dwo))
12971 {
049412e3 12972 dwo_sections->macro.s.section = sectp;
fd361982 12973 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12974 }
12975 else if (section_is_p (sectp->name, &names->str_dwo))
12976 {
049412e3 12977 dwo_sections->str.s.section = sectp;
fd361982 12978 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12979 }
12980 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12981 {
049412e3 12982 dwo_sections->str_offsets.s.section = sectp;
fd361982 12983 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12984 }
12985 else if (section_is_p (sectp->name, &names->types_dwo))
12986 {
12987 struct dwarf2_section_info type_section;
12988
12989 memset (&type_section, 0, sizeof (type_section));
049412e3 12990 type_section.s.section = sectp;
fd361982 12991 type_section.size = bfd_section_size (sectp);
fd5866f6 12992 dwo_sections->types.push_back (type_section);
80626a55
DE
12993 }
12994}
12995
ab5088bf 12996/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12997 by PER_CU. This is for the non-DWP case.
80626a55 12998 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12999
13000static struct dwo_file *
0ac5b59e
DE
13001open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13002 const char *dwo_name, const char *comp_dir)
3019eac3 13003{
ed2dc618 13004 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13005
fb1eb2f9 13006 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13007 if (dbfd == NULL)
13008 {
b4f54984 13009 if (dwarf_read_debug)
80626a55
DE
13010 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13011 return NULL;
13012 }
263db9a1 13013
51ac9db5 13014 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13015 dwo_file->dwo_name = dwo_name;
13016 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13017 dwo_file->dbfd = std::move (dbfd);
3019eac3 13018
fb1eb2f9 13019 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13020 &dwo_file->sections);
3019eac3 13021
ed2dc618
SM
13022 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13023 dwo_file->cus);
3019eac3 13024
263db9a1 13025 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13026 dwo_file->sections.types, dwo_file->tus);
3019eac3 13027
b4f54984 13028 if (dwarf_read_debug)
80626a55
DE
13029 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13030
263db9a1 13031 return dwo_file.release ();
3019eac3
DE
13032}
13033
80626a55 13034/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13035 size of each of the DWP debugging sections common to version 1 and 2 that
13036 we are interested in. */
3019eac3 13037
80626a55 13038static void
73869dc2
DE
13039dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13040 void *dwp_file_ptr)
3019eac3 13041{
9a3c8263 13042 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13043 const struct dwop_section_names *names = &dwop_section_names;
13044 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13045
80626a55 13046 /* Record the ELF section number for later lookup: this is what the
73869dc2 13047 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13048 gdb_assert (elf_section_nr < dwp_file->num_sections);
13049 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13050
80626a55
DE
13051 /* Look for specific sections that we need. */
13052 if (section_is_p (sectp->name, &names->str_dwo))
13053 {
049412e3 13054 dwp_file->sections.str.s.section = sectp;
fd361982 13055 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13056 }
13057 else if (section_is_p (sectp->name, &names->cu_index))
13058 {
049412e3 13059 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13060 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13061 }
13062 else if (section_is_p (sectp->name, &names->tu_index))
13063 {
049412e3 13064 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13065 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13066 }
13067}
3019eac3 13068
73869dc2
DE
13069/* This function is mapped across the sections and remembers the offset and
13070 size of each of the DWP version 2 debugging sections that we are interested
13071 in. This is split into a separate function because we don't know if we
13072 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13073
13074static void
13075dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13076{
9a3c8263 13077 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13078 const struct dwop_section_names *names = &dwop_section_names;
13079 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13080
13081 /* Record the ELF section number for later lookup: this is what the
13082 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13083 gdb_assert (elf_section_nr < dwp_file->num_sections);
13084 dwp_file->elf_sections[elf_section_nr] = sectp;
13085
13086 /* Look for specific sections that we need. */
13087 if (section_is_p (sectp->name, &names->abbrev_dwo))
13088 {
049412e3 13089 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13090 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13091 }
13092 else if (section_is_p (sectp->name, &names->info_dwo))
13093 {
049412e3 13094 dwp_file->sections.info.s.section = sectp;
fd361982 13095 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13096 }
13097 else if (section_is_p (sectp->name, &names->line_dwo))
13098 {
049412e3 13099 dwp_file->sections.line.s.section = sectp;
fd361982 13100 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13101 }
13102 else if (section_is_p (sectp->name, &names->loc_dwo))
13103 {
049412e3 13104 dwp_file->sections.loc.s.section = sectp;
fd361982 13105 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13106 }
13107 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13108 {
049412e3 13109 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13110 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13111 }
13112 else if (section_is_p (sectp->name, &names->macro_dwo))
13113 {
049412e3 13114 dwp_file->sections.macro.s.section = sectp;
fd361982 13115 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13116 }
13117 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13118 {
049412e3 13119 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13120 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13121 }
13122 else if (section_is_p (sectp->name, &names->types_dwo))
13123 {
049412e3 13124 dwp_file->sections.types.s.section = sectp;
fd361982 13125 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13126 }
13127}
13128
80626a55 13129/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13130
80626a55
DE
13131static hashval_t
13132hash_dwp_loaded_cutus (const void *item)
13133{
9a3c8263 13134 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13135
80626a55
DE
13136 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13137 return dwo_unit->signature;
3019eac3
DE
13138}
13139
80626a55 13140/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13141
80626a55
DE
13142static int
13143eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13144{
9a3c8263
SM
13145 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13146 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13147
80626a55
DE
13148 return dua->signature == dub->signature;
13149}
3019eac3 13150
80626a55 13151/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13152
80626a55
DE
13153static htab_t
13154allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13155{
13156 return htab_create_alloc_ex (3,
13157 hash_dwp_loaded_cutus,
13158 eq_dwp_loaded_cutus,
13159 NULL,
13160 &objfile->objfile_obstack,
13161 hashtab_obstack_allocate,
13162 dummy_obstack_deallocate);
13163}
3019eac3 13164
ab5088bf
DE
13165/* Try to open DWP file FILE_NAME.
13166 The result is the bfd handle of the file.
13167 If there is a problem finding or opening the file, return NULL.
13168 Upon success, the canonicalized path of the file is stored in the bfd,
13169 same as symfile_bfd_open. */
13170
192b62ce 13171static gdb_bfd_ref_ptr
ed2dc618
SM
13172open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13173 const char *file_name)
ab5088bf 13174{
ed2dc618
SM
13175 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13176 1 /*is_dwp*/,
192b62ce 13177 1 /*search_cwd*/));
6ac97d4c
DE
13178 if (abfd != NULL)
13179 return abfd;
13180
13181 /* Work around upstream bug 15652.
13182 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13183 [Whether that's a "bug" is debatable, but it is getting in our way.]
13184 We have no real idea where the dwp file is, because gdb's realpath-ing
13185 of the executable's path may have discarded the needed info.
13186 [IWBN if the dwp file name was recorded in the executable, akin to
13187 .gnu_debuglink, but that doesn't exist yet.]
13188 Strip the directory from FILE_NAME and search again. */
13189 if (*debug_file_directory != '\0')
13190 {
13191 /* Don't implicitly search the current directory here.
13192 If the user wants to search "." to handle this case,
13193 it must be added to debug-file-directory. */
ed2dc618
SM
13194 return try_open_dwop_file (dwarf2_per_objfile,
13195 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13196 0 /*search_cwd*/);
13197 }
13198
13199 return NULL;
ab5088bf
DE
13200}
13201
80626a55
DE
13202/* Initialize the use of the DWP file for the current objfile.
13203 By convention the name of the DWP file is ${objfile}.dwp.
13204 The result is NULL if it can't be found. */
a766d390 13205
400174b1 13206static std::unique_ptr<struct dwp_file>
ed2dc618 13207open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13208{
13209 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13210
82bf32bc
JK
13211 /* Try to find first .dwp for the binary file before any symbolic links
13212 resolving. */
6c447423
DE
13213
13214 /* If the objfile is a debug file, find the name of the real binary
13215 file and get the name of dwp file from there. */
d721ba37 13216 std::string dwp_name;
6c447423
DE
13217 if (objfile->separate_debug_objfile_backlink != NULL)
13218 {
13219 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13220 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13221
d721ba37 13222 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13223 }
13224 else
d721ba37
PA
13225 dwp_name = objfile->original_name;
13226
13227 dwp_name += ".dwp";
80626a55 13228
ed2dc618 13229 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13230 if (dbfd == NULL
13231 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13232 {
13233 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13234 dwp_name = objfile_name (objfile);
13235 dwp_name += ".dwp";
ed2dc618 13236 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13237 }
13238
80626a55
DE
13239 if (dbfd == NULL)
13240 {
b4f54984 13241 if (dwarf_read_debug)
d721ba37 13242 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13243 return std::unique_ptr<dwp_file> ();
3019eac3 13244 }
400174b1
TT
13245
13246 const char *name = bfd_get_filename (dbfd.get ());
13247 std::unique_ptr<struct dwp_file> dwp_file
13248 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13249
0a0f4c01 13250 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13251 dwp_file->elf_sections =
13252 OBSTACK_CALLOC (&objfile->objfile_obstack,
13253 dwp_file->num_sections, asection *);
13254
400174b1
TT
13255 bfd_map_over_sections (dwp_file->dbfd.get (),
13256 dwarf2_locate_common_dwp_sections,
13257 dwp_file.get ());
80626a55 13258
400174b1
TT
13259 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13260 0);
80626a55 13261
400174b1
TT
13262 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13263 1);
80626a55 13264
73869dc2 13265 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13266 if (dwp_file->cus && dwp_file->tus
13267 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13268 {
13269 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13270 pretty bizarre. We use pulongest here because that's the established
4d65956b 13271 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13272 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13273 " TU version %s [in DWP file %s]"),
13274 pulongest (dwp_file->cus->version),
d721ba37 13275 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13276 }
08302ed2
DE
13277
13278 if (dwp_file->cus)
13279 dwp_file->version = dwp_file->cus->version;
13280 else if (dwp_file->tus)
13281 dwp_file->version = dwp_file->tus->version;
13282 else
13283 dwp_file->version = 2;
73869dc2
DE
13284
13285 if (dwp_file->version == 2)
400174b1
TT
13286 bfd_map_over_sections (dwp_file->dbfd.get (),
13287 dwarf2_locate_v2_dwp_sections,
13288 dwp_file.get ());
73869dc2 13289
19ac8c2e
DE
13290 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13291 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13292
b4f54984 13293 if (dwarf_read_debug)
80626a55
DE
13294 {
13295 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13296 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13297 " %s CUs, %s TUs\n",
13298 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13299 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13300 }
13301
13302 return dwp_file;
3019eac3 13303}
c906108c 13304
ab5088bf
DE
13305/* Wrapper around open_and_init_dwp_file, only open it once. */
13306
13307static struct dwp_file *
ed2dc618 13308get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13309{
13310 if (! dwarf2_per_objfile->dwp_checked)
13311 {
ed2dc618
SM
13312 dwarf2_per_objfile->dwp_file
13313 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13314 dwarf2_per_objfile->dwp_checked = 1;
13315 }
400174b1 13316 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13317}
13318
80626a55
DE
13319/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13320 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13321 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13322 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13323 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13324
13325 This is called, for example, when wanting to read a variable with a
13326 complex location. Therefore we don't want to do file i/o for every call.
13327 Therefore we don't want to look for a DWO file on every call.
13328 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13329 then we check if we've already seen DWO_NAME, and only THEN do we check
13330 for a DWO file.
13331
1c658ad5 13332 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13333 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13334
3019eac3 13335static struct dwo_unit *
80626a55
DE
13336lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13337 const char *dwo_name, const char *comp_dir,
13338 ULONGEST signature, int is_debug_types)
3019eac3 13339{
ed2dc618 13340 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13341 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13342 const char *kind = is_debug_types ? "TU" : "CU";
13343 void **dwo_file_slot;
3019eac3 13344 struct dwo_file *dwo_file;
80626a55 13345 struct dwp_file *dwp_file;
cb1df416 13346
6a506a2d
DE
13347 /* First see if there's a DWP file.
13348 If we have a DWP file but didn't find the DWO inside it, don't
13349 look for the original DWO file. It makes gdb behave differently
13350 depending on whether one is debugging in the build tree. */
cf2c3c16 13351
ed2dc618 13352 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13353 if (dwp_file != NULL)
cf2c3c16 13354 {
80626a55
DE
13355 const struct dwp_hash_table *dwp_htab =
13356 is_debug_types ? dwp_file->tus : dwp_file->cus;
13357
13358 if (dwp_htab != NULL)
13359 {
13360 struct dwo_unit *dwo_cutu =
ed2dc618 13361 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13362 signature, is_debug_types);
80626a55
DE
13363
13364 if (dwo_cutu != NULL)
13365 {
b4f54984 13366 if (dwarf_read_debug)
80626a55
DE
13367 {
13368 fprintf_unfiltered (gdb_stdlog,
13369 "Virtual DWO %s %s found: @%s\n",
13370 kind, hex_string (signature),
13371 host_address_to_string (dwo_cutu));
13372 }
13373 return dwo_cutu;
13374 }
13375 }
13376 }
6a506a2d 13377 else
80626a55 13378 {
6a506a2d 13379 /* No DWP file, look for the DWO file. */
80626a55 13380
ed2dc618
SM
13381 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13382 dwo_name, comp_dir);
6a506a2d 13383 if (*dwo_file_slot == NULL)
80626a55 13384 {
6a506a2d
DE
13385 /* Read in the file and build a table of the CUs/TUs it contains. */
13386 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13387 }
6a506a2d 13388 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13389 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13390
6a506a2d 13391 if (dwo_file != NULL)
19c3d4c9 13392 {
6a506a2d
DE
13393 struct dwo_unit *dwo_cutu = NULL;
13394
13395 if (is_debug_types && dwo_file->tus)
13396 {
13397 struct dwo_unit find_dwo_cutu;
13398
13399 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13400 find_dwo_cutu.signature = signature;
9a3c8263
SM
13401 dwo_cutu
13402 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13403 }
33c5cd75 13404 else if (!is_debug_types && dwo_file->cus)
80626a55 13405 {
33c5cd75
DB
13406 struct dwo_unit find_dwo_cutu;
13407
13408 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13409 find_dwo_cutu.signature = signature;
13410 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13411 &find_dwo_cutu);
6a506a2d
DE
13412 }
13413
13414 if (dwo_cutu != NULL)
13415 {
b4f54984 13416 if (dwarf_read_debug)
6a506a2d
DE
13417 {
13418 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13419 kind, dwo_name, hex_string (signature),
13420 host_address_to_string (dwo_cutu));
13421 }
13422 return dwo_cutu;
80626a55
DE
13423 }
13424 }
2e276125 13425 }
9cdd5dbd 13426
80626a55
DE
13427 /* We didn't find it. This could mean a dwo_id mismatch, or
13428 someone deleted the DWO/DWP file, or the search path isn't set up
13429 correctly to find the file. */
13430
b4f54984 13431 if (dwarf_read_debug)
80626a55
DE
13432 {
13433 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13434 kind, dwo_name, hex_string (signature));
13435 }
3019eac3 13436
6656a72d
DE
13437 /* This is a warning and not a complaint because it can be caused by
13438 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13439 {
13440 /* Print the name of the DWP file if we looked there, helps the user
13441 better diagnose the problem. */
791afaa2 13442 std::string dwp_text;
43942612
DE
13443
13444 if (dwp_file != NULL)
791afaa2
TT
13445 dwp_text = string_printf (" [in DWP file %s]",
13446 lbasename (dwp_file->name));
43942612 13447
9d8780f0 13448 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13449 " [in module %s]"),
13450 kind, dwo_name, hex_string (signature),
791afaa2 13451 dwp_text.c_str (),
43942612 13452 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13453 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13454 }
3019eac3 13455 return NULL;
5fb290d7
DJ
13456}
13457
80626a55
DE
13458/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13459 See lookup_dwo_cutu_unit for details. */
13460
13461static struct dwo_unit *
13462lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13463 const char *dwo_name, const char *comp_dir,
13464 ULONGEST signature)
13465{
13466 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13467}
13468
13469/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13470 See lookup_dwo_cutu_unit for details. */
13471
13472static struct dwo_unit *
13473lookup_dwo_type_unit (struct signatured_type *this_tu,
13474 const char *dwo_name, const char *comp_dir)
13475{
13476 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13477}
13478
89e63ee4
DE
13479/* Traversal function for queue_and_load_all_dwo_tus. */
13480
13481static int
13482queue_and_load_dwo_tu (void **slot, void *info)
13483{
13484 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13485 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13486 ULONGEST signature = dwo_unit->signature;
13487 struct signatured_type *sig_type =
13488 lookup_dwo_signatured_type (per_cu->cu, signature);
13489
13490 if (sig_type != NULL)
13491 {
13492 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13493
13494 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13495 a real dependency of PER_CU on SIG_TYPE. That is detected later
13496 while processing PER_CU. */
13497 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13498 load_full_type_unit (sig_cu);
ae640021 13499 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13500 }
13501
13502 return 1;
13503}
13504
13505/* Queue all TUs contained in the DWO of PER_CU to be read in.
13506 The DWO may have the only definition of the type, though it may not be
13507 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13508 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13509
13510static void
13511queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13512{
13513 struct dwo_unit *dwo_unit;
13514 struct dwo_file *dwo_file;
13515
13516 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13517 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13518 gdb_assert (per_cu->cu != NULL);
13519
13520 dwo_unit = per_cu->cu->dwo_unit;
13521 gdb_assert (dwo_unit != NULL);
13522
13523 dwo_file = dwo_unit->dwo_file;
13524 if (dwo_file->tus != NULL)
13525 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13526}
13527
3019eac3 13528/* Read in various DIEs. */
348e048f 13529
d389af10 13530/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13531 Inherit only the children of the DW_AT_abstract_origin DIE not being
13532 already referenced by DW_AT_abstract_origin from the children of the
13533 current DIE. */
d389af10
JK
13534
13535static void
13536inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13537{
13538 struct die_info *child_die;
791afaa2 13539 sect_offset *offsetp;
d389af10
JK
13540 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13541 struct die_info *origin_die;
13542 /* Iterator of the ORIGIN_DIE children. */
13543 struct die_info *origin_child_die;
d389af10 13544 struct attribute *attr;
cd02d79d
PA
13545 struct dwarf2_cu *origin_cu;
13546 struct pending **origin_previous_list_in_scope;
d389af10
JK
13547
13548 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13549 if (!attr)
13550 return;
13551
cd02d79d
PA
13552 /* Note that following die references may follow to a die in a
13553 different cu. */
13554
13555 origin_cu = cu;
13556 origin_die = follow_die_ref (die, attr, &origin_cu);
13557
13558 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13559 symbols in. */
13560 origin_previous_list_in_scope = origin_cu->list_in_scope;
13561 origin_cu->list_in_scope = cu->list_in_scope;
13562
edb3359d
DJ
13563 if (die->tag != origin_die->tag
13564 && !(die->tag == DW_TAG_inlined_subroutine
13565 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13566 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13567 sect_offset_str (die->sect_off),
13568 sect_offset_str (origin_die->sect_off));
d389af10 13569
791afaa2 13570 std::vector<sect_offset> offsets;
d389af10 13571
3ea89b92
PMR
13572 for (child_die = die->child;
13573 child_die && child_die->tag;
13574 child_die = sibling_die (child_die))
13575 {
13576 struct die_info *child_origin_die;
13577 struct dwarf2_cu *child_origin_cu;
13578
13579 /* We are trying to process concrete instance entries:
216f72a1 13580 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13581 it's not relevant to our analysis here. i.e. detecting DIEs that are
13582 present in the abstract instance but not referenced in the concrete
13583 one. */
216f72a1
JK
13584 if (child_die->tag == DW_TAG_call_site
13585 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13586 continue;
13587
c38f313d
DJ
13588 /* For each CHILD_DIE, find the corresponding child of
13589 ORIGIN_DIE. If there is more than one layer of
13590 DW_AT_abstract_origin, follow them all; there shouldn't be,
13591 but GCC versions at least through 4.4 generate this (GCC PR
13592 40573). */
3ea89b92
PMR
13593 child_origin_die = child_die;
13594 child_origin_cu = cu;
c38f313d
DJ
13595 while (1)
13596 {
cd02d79d
PA
13597 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13598 child_origin_cu);
c38f313d
DJ
13599 if (attr == NULL)
13600 break;
cd02d79d
PA
13601 child_origin_die = follow_die_ref (child_origin_die, attr,
13602 &child_origin_cu);
c38f313d
DJ
13603 }
13604
d389af10
JK
13605 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13606 counterpart may exist. */
c38f313d 13607 if (child_origin_die != child_die)
d389af10 13608 {
edb3359d
DJ
13609 if (child_die->tag != child_origin_die->tag
13610 && !(child_die->tag == DW_TAG_inlined_subroutine
13611 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13612 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13613 "different tags"),
9d8780f0
SM
13614 sect_offset_str (child_die->sect_off),
13615 sect_offset_str (child_origin_die->sect_off));
c38f313d 13616 if (child_origin_die->parent != origin_die)
b98664d3 13617 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13618 "different parents"),
9d8780f0
SM
13619 sect_offset_str (child_die->sect_off),
13620 sect_offset_str (child_origin_die->sect_off));
c38f313d 13621 else
791afaa2 13622 offsets.push_back (child_origin_die->sect_off);
d389af10 13623 }
d389af10 13624 }
791afaa2
TT
13625 std::sort (offsets.begin (), offsets.end ());
13626 sect_offset *offsets_end = offsets.data () + offsets.size ();
13627 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13628 if (offsetp[-1] == *offsetp)
b98664d3 13629 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13630 "to DIE %s as their abstract origin"),
13631 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13632
791afaa2 13633 offsetp = offsets.data ();
d389af10
JK
13634 origin_child_die = origin_die->child;
13635 while (origin_child_die && origin_child_die->tag)
13636 {
13637 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13638 while (offsetp < offsets_end
9c541725 13639 && *offsetp < origin_child_die->sect_off)
d389af10 13640 offsetp++;
b64f50a1 13641 if (offsetp >= offsets_end
9c541725 13642 || *offsetp > origin_child_die->sect_off)
d389af10 13643 {
adde2bff
DE
13644 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13645 Check whether we're already processing ORIGIN_CHILD_DIE.
13646 This can happen with mutually referenced abstract_origins.
13647 PR 16581. */
13648 if (!origin_child_die->in_process)
13649 process_die (origin_child_die, origin_cu);
d389af10
JK
13650 }
13651 origin_child_die = sibling_die (origin_child_die);
13652 }
cd02d79d 13653 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13654}
13655
c906108c 13656static void
e7c27a73 13657read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13658{
518817b3 13659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13661 struct context_stack *newobj;
c906108c
SS
13662 CORE_ADDR lowpc;
13663 CORE_ADDR highpc;
13664 struct die_info *child_die;
edb3359d 13665 struct attribute *attr, *call_line, *call_file;
15d034d0 13666 const char *name;
e142c38c 13667 CORE_ADDR baseaddr;
801e3a5b 13668 struct block *block;
edb3359d 13669 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13670 std::vector<struct symbol *> template_args;
34eaf542 13671 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13672
13673 if (inlined_func)
13674 {
13675 /* If we do not have call site information, we can't show the
13676 caller of this inlined function. That's too confusing, so
13677 only use the scope for local variables. */
13678 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13679 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13680 if (call_line == NULL || call_file == NULL)
13681 {
13682 read_lexical_block_scope (die, cu);
13683 return;
13684 }
13685 }
c906108c 13686
e142c38c
DJ
13687 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13688
94af9270 13689 name = dwarf2_name (die, cu);
c906108c 13690
e8d05480
JB
13691 /* Ignore functions with missing or empty names. These are actually
13692 illegal according to the DWARF standard. */
13693 if (name == NULL)
13694 {
b98664d3 13695 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13696 sect_offset_str (die->sect_off));
e8d05480
JB
13697 return;
13698 }
13699
13700 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13701 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13702 <= PC_BOUNDS_INVALID)
e8d05480 13703 {
ae4d0c03
PM
13704 attr = dwarf2_attr (die, DW_AT_external, cu);
13705 if (!attr || !DW_UNSND (attr))
b98664d3 13706 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13707 "for subprogram DIE at %s"),
13708 sect_offset_str (die->sect_off));
e8d05480
JB
13709 return;
13710 }
c906108c 13711
3e29f34a
MR
13712 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13713 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13714
34eaf542
TT
13715 /* If we have any template arguments, then we must allocate a
13716 different sort of symbol. */
13717 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13718 {
13719 if (child_die->tag == DW_TAG_template_type_param
13720 || child_die->tag == DW_TAG_template_value_param)
13721 {
e623cf5d 13722 templ_func = allocate_template_symbol (objfile);
cf724bc9 13723 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13724 break;
13725 }
13726 }
13727
c24bdb02 13728 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13729 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13730 (struct symbol *) templ_func);
4c2df51b 13731
81873cc8
TV
13732 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13733 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13734 cu->language);
13735
4cecd739
DJ
13736 /* If there is a location expression for DW_AT_frame_base, record
13737 it. */
e142c38c 13738 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13739 if (attr)
fe978cb0 13740 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13741
63e43d3a
PMR
13742 /* If there is a location for the static link, record it. */
13743 newobj->static_link = NULL;
13744 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13745 if (attr)
13746 {
224c3ddb
SM
13747 newobj->static_link
13748 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13749 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13750 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13751 }
13752
c24bdb02 13753 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13754
639d11d3 13755 if (die->child != NULL)
c906108c 13756 {
639d11d3 13757 child_die = die->child;
c906108c
SS
13758 while (child_die && child_die->tag)
13759 {
34eaf542
TT
13760 if (child_die->tag == DW_TAG_template_type_param
13761 || child_die->tag == DW_TAG_template_value_param)
13762 {
13763 struct symbol *arg = new_symbol (child_die, NULL, cu);
13764
f1078f66 13765 if (arg != NULL)
2f4732b0 13766 template_args.push_back (arg);
34eaf542
TT
13767 }
13768 else
13769 process_die (child_die, cu);
c906108c
SS
13770 child_die = sibling_die (child_die);
13771 }
13772 }
13773
d389af10
JK
13774 inherit_abstract_dies (die, cu);
13775
4a811a97
UW
13776 /* If we have a DW_AT_specification, we might need to import using
13777 directives from the context of the specification DIE. See the
13778 comment in determine_prefix. */
13779 if (cu->language == language_cplus
13780 && dwarf2_attr (die, DW_AT_specification, cu))
13781 {
13782 struct dwarf2_cu *spec_cu = cu;
13783 struct die_info *spec_die = die_specification (die, &spec_cu);
13784
13785 while (spec_die)
13786 {
13787 child_die = spec_die->child;
13788 while (child_die && child_die->tag)
13789 {
13790 if (child_die->tag == DW_TAG_imported_module)
13791 process_die (child_die, spec_cu);
13792 child_die = sibling_die (child_die);
13793 }
13794
13795 /* In some cases, GCC generates specification DIEs that
13796 themselves contain DW_AT_specification attributes. */
13797 spec_die = die_specification (spec_die, &spec_cu);
13798 }
13799 }
13800
c24bdb02 13801 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13802 /* Make a block for the local symbols within. */
c24bdb02 13803 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13804 cstk.static_link, lowpc, highpc);
801e3a5b 13805
df8a16a1 13806 /* For C++, set the block's scope. */
45280282
IB
13807 if ((cu->language == language_cplus
13808 || cu->language == language_fortran
c44af4eb
TT
13809 || cu->language == language_d
13810 || cu->language == language_rust)
4d4ec4e5 13811 && cu->processing_has_namespace_info)
195a3f6c
TT
13812 block_set_scope (block, determine_prefix (die, cu),
13813 &objfile->objfile_obstack);
df8a16a1 13814
801e3a5b
JB
13815 /* If we have address ranges, record them. */
13816 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13817
a60f3166 13818 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13819
34eaf542 13820 /* Attach template arguments to function. */
2f4732b0 13821 if (!template_args.empty ())
34eaf542
TT
13822 {
13823 gdb_assert (templ_func != NULL);
13824
2f4732b0 13825 templ_func->n_template_arguments = template_args.size ();
34eaf542 13826 templ_func->template_arguments
8d749320
SM
13827 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13828 templ_func->n_template_arguments);
34eaf542 13829 memcpy (templ_func->template_arguments,
2f4732b0 13830 template_args.data (),
34eaf542 13831 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13832
13833 /* Make sure that the symtab is set on the new symbols. Even
13834 though they don't appear in this symtab directly, other parts
13835 of gdb assume that symbols do, and this is reasonably
13836 true. */
8634679f 13837 for (symbol *sym : template_args)
3e1d3d8c 13838 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13839 }
13840
208d8187
JB
13841 /* In C++, we can have functions nested inside functions (e.g., when
13842 a function declares a class that has methods). This means that
13843 when we finish processing a function scope, we may need to go
13844 back to building a containing block's symbol lists. */
c24bdb02
KS
13845 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13846 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13847
921e78cf
JB
13848 /* If we've finished processing a top-level function, subsequent
13849 symbols go in the file symbol list. */
c24bdb02
KS
13850 if (cu->get_builder ()->outermost_context_p ())
13851 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13852}
13853
13854/* Process all the DIES contained within a lexical block scope. Start
13855 a new scope, process the dies, and then close the scope. */
13856
13857static void
e7c27a73 13858read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13859{
518817b3 13860 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13861 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13862 CORE_ADDR lowpc, highpc;
13863 struct die_info *child_die;
e142c38c
DJ
13864 CORE_ADDR baseaddr;
13865
13866 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13867
13868 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13869 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13870 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13871 be nasty. Might be easier to properly extend generic blocks to
af34e669 13872 describe ranges. */
e385593e
JK
13873 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13874 {
13875 case PC_BOUNDS_NOT_PRESENT:
13876 /* DW_TAG_lexical_block has no attributes, process its children as if
13877 there was no wrapping by that DW_TAG_lexical_block.
13878 GCC does no longer produces such DWARF since GCC r224161. */
13879 for (child_die = die->child;
13880 child_die != NULL && child_die->tag;
13881 child_die = sibling_die (child_die))
13882 process_die (child_die, cu);
13883 return;
13884 case PC_BOUNDS_INVALID:
13885 return;
13886 }
3e29f34a
MR
13887 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13888 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13889
c24bdb02 13890 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13891 if (die->child != NULL)
c906108c 13892 {
639d11d3 13893 child_die = die->child;
c906108c
SS
13894 while (child_die && child_die->tag)
13895 {
e7c27a73 13896 process_die (child_die, cu);
c906108c
SS
13897 child_die = sibling_die (child_die);
13898 }
13899 }
3ea89b92 13900 inherit_abstract_dies (die, cu);
c24bdb02 13901 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13902
c24bdb02
KS
13903 if (*cu->get_builder ()->get_local_symbols () != NULL
13904 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13905 {
801e3a5b 13906 struct block *block
c24bdb02 13907 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13908 cstk.start_addr, highpc);
801e3a5b
JB
13909
13910 /* Note that recording ranges after traversing children, as we
13911 do here, means that recording a parent's ranges entails
13912 walking across all its children's ranges as they appear in
13913 the address map, which is quadratic behavior.
13914
13915 It would be nicer to record the parent's ranges before
13916 traversing its children, simply overriding whatever you find
13917 there. But since we don't even decide whether to create a
13918 block until after we've traversed its children, that's hard
13919 to do. */
13920 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13921 }
c24bdb02
KS
13922 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13923 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13924}
13925
216f72a1 13926/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13927
13928static void
13929read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13930{
518817b3 13931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13932 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13933 CORE_ADDR pc, baseaddr;
13934 struct attribute *attr;
13935 struct call_site *call_site, call_site_local;
13936 void **slot;
13937 int nparams;
13938 struct die_info *child_die;
13939
13940 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13941
216f72a1
JK
13942 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13943 if (attr == NULL)
13944 {
13945 /* This was a pre-DWARF-5 GNU extension alias
13946 for DW_AT_call_return_pc. */
13947 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13948 }
96408a79
SA
13949 if (!attr)
13950 {
b98664d3 13951 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13952 "DIE %s [in module %s]"),
13953 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13954 return;
13955 }
31aa7e4e 13956 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13957 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13958
13959 if (cu->call_site_htab == NULL)
13960 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13961 NULL, &objfile->objfile_obstack,
13962 hashtab_obstack_allocate, NULL);
13963 call_site_local.pc = pc;
13964 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13965 if (*slot != NULL)
13966 {
b98664d3 13967 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13968 "DIE %s [in module %s]"),
13969 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13970 objfile_name (objfile));
96408a79
SA
13971 return;
13972 }
13973
13974 /* Count parameters at the caller. */
13975
13976 nparams = 0;
13977 for (child_die = die->child; child_die && child_die->tag;
13978 child_die = sibling_die (child_die))
13979 {
216f72a1
JK
13980 if (child_die->tag != DW_TAG_call_site_parameter
13981 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13982 {
b98664d3 13983 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13984 "DW_TAG_call_site child DIE %s [in module %s]"),
13985 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13986 objfile_name (objfile));
96408a79
SA
13987 continue;
13988 }
13989
13990 nparams++;
13991 }
13992
224c3ddb
SM
13993 call_site
13994 = ((struct call_site *)
13995 obstack_alloc (&objfile->objfile_obstack,
13996 sizeof (*call_site)
13997 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13998 *slot = call_site;
13999 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14000 call_site->pc = pc;
14001
216f72a1
JK
14002 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14003 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14004 {
14005 struct die_info *func_die;
14006
14007 /* Skip also over DW_TAG_inlined_subroutine. */
14008 for (func_die = die->parent;
14009 func_die && func_die->tag != DW_TAG_subprogram
14010 && func_die->tag != DW_TAG_subroutine_type;
14011 func_die = func_die->parent);
14012
216f72a1
JK
14013 /* DW_AT_call_all_calls is a superset
14014 of DW_AT_call_all_tail_calls. */
96408a79 14015 if (func_die
216f72a1 14016 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14017 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14018 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14019 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14020 {
14021 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14022 not complete. But keep CALL_SITE for look ups via call_site_htab,
14023 both the initial caller containing the real return address PC and
14024 the final callee containing the current PC of a chain of tail
14025 calls do not need to have the tail call list complete. But any
14026 function candidate for a virtual tail call frame searched via
14027 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14028 determined unambiguously. */
14029 }
14030 else
14031 {
14032 struct type *func_type = NULL;
14033
14034 if (func_die)
14035 func_type = get_die_type (func_die, cu);
14036 if (func_type != NULL)
14037 {
14038 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14039
14040 /* Enlist this call site to the function. */
14041 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14042 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14043 }
14044 else
b98664d3 14045 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14046 "DIE %s [in module %s]"),
14047 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14048 }
14049 }
14050
216f72a1
JK
14051 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14052 if (attr == NULL)
14053 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14054 if (attr == NULL)
14055 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14056 if (attr == NULL)
216f72a1
JK
14057 {
14058 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14059 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14060 }
96408a79
SA
14061 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14062 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14063 /* Keep NULL DWARF_BLOCK. */;
14064 else if (attr_form_is_block (attr))
14065 {
14066 struct dwarf2_locexpr_baton *dlbaton;
14067
8d749320 14068 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14069 dlbaton->data = DW_BLOCK (attr)->data;
14070 dlbaton->size = DW_BLOCK (attr)->size;
14071 dlbaton->per_cu = cu->per_cu;
14072
14073 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14074 }
7771576e 14075 else if (attr_form_is_ref (attr))
96408a79 14076 {
96408a79
SA
14077 struct dwarf2_cu *target_cu = cu;
14078 struct die_info *target_die;
14079
ac9ec31b 14080 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14081 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14082 if (die_is_declaration (target_die, target_cu))
14083 {
7d45c7c3 14084 const char *target_physname;
9112db09
JK
14085
14086 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14087 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14088 if (target_physname == NULL)
9112db09 14089 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14090 if (target_physname == NULL)
b98664d3 14091 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14092 "physname, for referencing DIE %s [in module %s]"),
14093 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14094 else
7d455152 14095 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14096 }
14097 else
14098 {
14099 CORE_ADDR lowpc;
14100
14101 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14102 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14103 <= PC_BOUNDS_INVALID)
b98664d3 14104 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14105 "low pc, for referencing DIE %s [in module %s]"),
14106 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14107 else
3e29f34a
MR
14108 {
14109 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14110 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14111 }
96408a79
SA
14112 }
14113 }
14114 else
b98664d3 14115 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14116 "block nor reference, for DIE %s [in module %s]"),
14117 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14118
14119 call_site->per_cu = cu->per_cu;
14120
14121 for (child_die = die->child;
14122 child_die && child_die->tag;
14123 child_die = sibling_die (child_die))
14124 {
96408a79 14125 struct call_site_parameter *parameter;
1788b2d3 14126 struct attribute *loc, *origin;
96408a79 14127
216f72a1
JK
14128 if (child_die->tag != DW_TAG_call_site_parameter
14129 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14130 {
14131 /* Already printed the complaint above. */
14132 continue;
14133 }
14134
14135 gdb_assert (call_site->parameter_count < nparams);
14136 parameter = &call_site->parameter[call_site->parameter_count];
14137
1788b2d3
JK
14138 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14139 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14140 register is contained in DW_AT_call_value. */
96408a79 14141
24c5c679 14142 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14143 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14144 if (origin == NULL)
14145 {
14146 /* This was a pre-DWARF-5 GNU extension alias
14147 for DW_AT_call_parameter. */
14148 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14149 }
7771576e 14150 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14151 {
1788b2d3 14152 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14153
14154 sect_offset sect_off
14155 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14156 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14157 {
14158 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14159 binding can be done only inside one CU. Such referenced DIE
14160 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14161 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14162 "DW_TAG_call_site child DIE %s [in module %s]"),
14163 sect_offset_str (child_die->sect_off),
9c541725 14164 objfile_name (objfile));
d76b7dbc
JK
14165 continue;
14166 }
9c541725
PA
14167 parameter->u.param_cu_off
14168 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14169 }
14170 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14171 {
b98664d3 14172 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14173 "DW_TAG_call_site child DIE %s [in module %s]"),
14174 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14175 continue;
14176 }
24c5c679 14177 else
96408a79 14178 {
24c5c679
JK
14179 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14180 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14181 if (parameter->u.dwarf_reg != -1)
14182 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14183 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14184 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14185 &parameter->u.fb_offset))
14186 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14187 else
14188 {
b98664d3 14189 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14190 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14191 "DW_TAG_call_site child DIE %s "
24c5c679 14192 "[in module %s]"),
9d8780f0 14193 sect_offset_str (child_die->sect_off),
9c541725 14194 objfile_name (objfile));
24c5c679
JK
14195 continue;
14196 }
96408a79
SA
14197 }
14198
216f72a1
JK
14199 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14200 if (attr == NULL)
14201 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14202 if (!attr_form_is_block (attr))
14203 {
b98664d3 14204 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14205 "DW_TAG_call_site child DIE %s [in module %s]"),
14206 sect_offset_str (child_die->sect_off),
9c541725 14207 objfile_name (objfile));
96408a79
SA
14208 continue;
14209 }
14210 parameter->value = DW_BLOCK (attr)->data;
14211 parameter->value_size = DW_BLOCK (attr)->size;
14212
14213 /* Parameters are not pre-cleared by memset above. */
14214 parameter->data_value = NULL;
14215 parameter->data_value_size = 0;
14216 call_site->parameter_count++;
14217
216f72a1
JK
14218 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14219 if (attr == NULL)
14220 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14221 if (attr)
14222 {
14223 if (!attr_form_is_block (attr))
b98664d3 14224 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14225 "DW_TAG_call_site child DIE %s [in module %s]"),
14226 sect_offset_str (child_die->sect_off),
9c541725 14227 objfile_name (objfile));
96408a79
SA
14228 else
14229 {
14230 parameter->data_value = DW_BLOCK (attr)->data;
14231 parameter->data_value_size = DW_BLOCK (attr)->size;
14232 }
14233 }
14234 }
14235}
14236
71a3c369
TT
14237/* Helper function for read_variable. If DIE represents a virtual
14238 table, then return the type of the concrete object that is
14239 associated with the virtual table. Otherwise, return NULL. */
14240
14241static struct type *
14242rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14243{
14244 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14245 if (attr == NULL)
14246 return NULL;
14247
14248 /* Find the type DIE. */
14249 struct die_info *type_die = NULL;
14250 struct dwarf2_cu *type_cu = cu;
14251
14252 if (attr_form_is_ref (attr))
14253 type_die = follow_die_ref (die, attr, &type_cu);
14254 if (type_die == NULL)
14255 return NULL;
14256
14257 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14258 return NULL;
14259 return die_containing_type (type_die, type_cu);
14260}
14261
14262/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14263
14264static void
14265read_variable (struct die_info *die, struct dwarf2_cu *cu)
14266{
14267 struct rust_vtable_symbol *storage = NULL;
14268
14269 if (cu->language == language_rust)
14270 {
14271 struct type *containing_type = rust_containing_type (die, cu);
14272
14273 if (containing_type != NULL)
14274 {
518817b3 14275 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14276
14277 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14278 struct rust_vtable_symbol);
14279 initialize_objfile_symbol (storage);
14280 storage->concrete_type = containing_type;
cf724bc9 14281 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14282 }
14283 }
14284
e4a62c65
TV
14285 struct symbol *res = new_symbol (die, NULL, cu, storage);
14286 struct attribute *abstract_origin
14287 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14288 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14289 if (res == NULL && loc && abstract_origin)
14290 {
14291 /* We have a variable without a name, but with a location and an abstract
14292 origin. This may be a concrete instance of an abstract variable
14293 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14294 later. */
14295 struct dwarf2_cu *origin_cu = cu;
14296 struct die_info *origin_die
14297 = follow_die_ref (die, abstract_origin, &origin_cu);
14298 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14299 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14300 }
71a3c369
TT
14301}
14302
43988095
JK
14303/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14304 reading .debug_rnglists.
14305 Callback's type should be:
14306 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14307 Return true if the attributes are present and valid, otherwise,
14308 return false. */
14309
14310template <typename Callback>
14311static bool
14312dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14313 Callback &&callback)
14314{
ed2dc618 14315 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14316 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14317 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14318 bfd *obfd = objfile->obfd;
43988095
JK
14319 /* Base address selection entry. */
14320 CORE_ADDR base;
14321 int found_base;
43988095 14322 const gdb_byte *buffer;
43988095
JK
14323 CORE_ADDR baseaddr;
14324 bool overflow = false;
14325
14326 found_base = cu->base_known;
14327 base = cu->base_address;
14328
14329 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14330 if (offset >= dwarf2_per_objfile->rnglists.size)
14331 {
b98664d3 14332 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14333 offset);
14334 return false;
14335 }
14336 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14337
14338 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14339
14340 while (1)
14341 {
7814882a
JK
14342 /* Initialize it due to a false compiler warning. */
14343 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14344 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14345 + dwarf2_per_objfile->rnglists.size);
14346 unsigned int bytes_read;
14347
14348 if (buffer == buf_end)
14349 {
14350 overflow = true;
14351 break;
14352 }
14353 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14354 switch (rlet)
14355 {
14356 case DW_RLE_end_of_list:
14357 break;
14358 case DW_RLE_base_address:
14359 if (buffer + cu->header.addr_size > buf_end)
14360 {
14361 overflow = true;
14362 break;
14363 }
14364 base = read_address (obfd, buffer, cu, &bytes_read);
14365 found_base = 1;
14366 buffer += bytes_read;
14367 break;
14368 case DW_RLE_start_length:
14369 if (buffer + cu->header.addr_size > buf_end)
14370 {
14371 overflow = true;
14372 break;
14373 }
14374 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14375 buffer += bytes_read;
14376 range_end = (range_beginning
14377 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14378 buffer += bytes_read;
14379 if (buffer > buf_end)
14380 {
14381 overflow = true;
14382 break;
14383 }
14384 break;
14385 case DW_RLE_offset_pair:
14386 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14387 buffer += bytes_read;
14388 if (buffer > buf_end)
14389 {
14390 overflow = true;
14391 break;
14392 }
14393 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14394 buffer += bytes_read;
14395 if (buffer > buf_end)
14396 {
14397 overflow = true;
14398 break;
14399 }
14400 break;
14401 case DW_RLE_start_end:
14402 if (buffer + 2 * cu->header.addr_size > buf_end)
14403 {
14404 overflow = true;
14405 break;
14406 }
14407 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14408 buffer += bytes_read;
14409 range_end = read_address (obfd, buffer, cu, &bytes_read);
14410 buffer += bytes_read;
14411 break;
14412 default:
b98664d3 14413 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14414 return false;
14415 }
14416 if (rlet == DW_RLE_end_of_list || overflow)
14417 break;
14418 if (rlet == DW_RLE_base_address)
14419 continue;
14420
14421 if (!found_base)
14422 {
14423 /* We have no valid base address for the ranges
14424 data. */
b98664d3 14425 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14426 return false;
14427 }
14428
14429 if (range_beginning > range_end)
14430 {
14431 /* Inverted range entries are invalid. */
b98664d3 14432 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14433 return false;
14434 }
14435
14436 /* Empty range entries have no effect. */
14437 if (range_beginning == range_end)
14438 continue;
14439
14440 range_beginning += base;
14441 range_end += base;
14442
14443 /* A not-uncommon case of bad debug info.
14444 Don't pollute the addrmap with bad data. */
14445 if (range_beginning + baseaddr == 0
14446 && !dwarf2_per_objfile->has_section_at_zero)
14447 {
b98664d3 14448 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14449 " [in module %s]"), objfile_name (objfile));
14450 continue;
14451 }
14452
14453 callback (range_beginning, range_end);
14454 }
14455
14456 if (overflow)
14457 {
b98664d3 14458 complaint (_("Offset %d is not terminated "
43988095
JK
14459 "for DW_AT_ranges attribute"),
14460 offset);
14461 return false;
14462 }
14463
14464 return true;
14465}
14466
14467/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14468 Callback's type should be:
14469 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14470 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14471
43988095 14472template <typename Callback>
43039443 14473static int
5f46c5a5 14474dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14475 Callback &&callback)
43039443 14476{
ed2dc618 14477 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14478 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14479 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14480 struct comp_unit_head *cu_header = &cu->header;
14481 bfd *obfd = objfile->obfd;
14482 unsigned int addr_size = cu_header->addr_size;
14483 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14484 /* Base address selection entry. */
14485 CORE_ADDR base;
14486 int found_base;
14487 unsigned int dummy;
d521ce57 14488 const gdb_byte *buffer;
ff013f42 14489 CORE_ADDR baseaddr;
43039443 14490
43988095
JK
14491 if (cu_header->version >= 5)
14492 return dwarf2_rnglists_process (offset, cu, callback);
14493
d00adf39
DE
14494 found_base = cu->base_known;
14495 base = cu->base_address;
43039443 14496
be391dca 14497 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14498 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14499 {
b98664d3 14500 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14501 offset);
14502 return 0;
14503 }
dce234bc 14504 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14505
e7030f15 14506 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14507
43039443
JK
14508 while (1)
14509 {
14510 CORE_ADDR range_beginning, range_end;
14511
14512 range_beginning = read_address (obfd, buffer, cu, &dummy);
14513 buffer += addr_size;
14514 range_end = read_address (obfd, buffer, cu, &dummy);
14515 buffer += addr_size;
14516 offset += 2 * addr_size;
14517
14518 /* An end of list marker is a pair of zero addresses. */
14519 if (range_beginning == 0 && range_end == 0)
14520 /* Found the end of list entry. */
14521 break;
14522
14523 /* Each base address selection entry is a pair of 2 values.
14524 The first is the largest possible address, the second is
14525 the base address. Check for a base address here. */
14526 if ((range_beginning & mask) == mask)
14527 {
28d2bfb9
AB
14528 /* If we found the largest possible address, then we already
14529 have the base address in range_end. */
14530 base = range_end;
43039443
JK
14531 found_base = 1;
14532 continue;
14533 }
14534
14535 if (!found_base)
14536 {
14537 /* We have no valid base address for the ranges
14538 data. */
b98664d3 14539 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14540 return 0;
14541 }
14542
9277c30c
UW
14543 if (range_beginning > range_end)
14544 {
14545 /* Inverted range entries are invalid. */
b98664d3 14546 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14547 return 0;
14548 }
14549
14550 /* Empty range entries have no effect. */
14551 if (range_beginning == range_end)
14552 continue;
14553
43039443
JK
14554 range_beginning += base;
14555 range_end += base;
14556
01093045
DE
14557 /* A not-uncommon case of bad debug info.
14558 Don't pollute the addrmap with bad data. */
14559 if (range_beginning + baseaddr == 0
14560 && !dwarf2_per_objfile->has_section_at_zero)
14561 {
b98664d3 14562 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14563 " [in module %s]"), objfile_name (objfile));
01093045
DE
14564 continue;
14565 }
14566
5f46c5a5
JK
14567 callback (range_beginning, range_end);
14568 }
14569
14570 return 1;
14571}
14572
14573/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14574 Return 1 if the attributes are present and valid, otherwise, return 0.
14575 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14576
14577static int
14578dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14579 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14580 struct partial_symtab *ranges_pst)
14581{
518817b3 14582 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14583 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14584 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14585 SECT_OFF_TEXT (objfile));
14586 int low_set = 0;
14587 CORE_ADDR low = 0;
14588 CORE_ADDR high = 0;
14589 int retval;
14590
14591 retval = dwarf2_ranges_process (offset, cu,
14592 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14593 {
9277c30c 14594 if (ranges_pst != NULL)
3e29f34a
MR
14595 {
14596 CORE_ADDR lowpc;
14597 CORE_ADDR highpc;
14598
79748972
TT
14599 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14600 range_beginning + baseaddr)
14601 - baseaddr);
14602 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14603 range_end + baseaddr)
14604 - baseaddr);
d320c2b5
TT
14605 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14606 lowpc, highpc - 1, ranges_pst);
3e29f34a 14607 }
ff013f42 14608
43039443
JK
14609 /* FIXME: This is recording everything as a low-high
14610 segment of consecutive addresses. We should have a
14611 data structure for discontiguous block ranges
14612 instead. */
14613 if (! low_set)
14614 {
14615 low = range_beginning;
14616 high = range_end;
14617 low_set = 1;
14618 }
14619 else
14620 {
14621 if (range_beginning < low)
14622 low = range_beginning;
14623 if (range_end > high)
14624 high = range_end;
14625 }
5f46c5a5
JK
14626 });
14627 if (!retval)
14628 return 0;
43039443
JK
14629
14630 if (! low_set)
14631 /* If the first entry is an end-of-list marker, the range
14632 describes an empty scope, i.e. no instructions. */
14633 return 0;
14634
14635 if (low_return)
14636 *low_return = low;
14637 if (high_return)
14638 *high_return = high;
14639 return 1;
14640}
14641
3a2b436a
JK
14642/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14643 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14644 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14645
3a2b436a 14646static enum pc_bounds_kind
af34e669 14647dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14648 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14649 struct partial_symtab *pst)
c906108c 14650{
518817b3
SM
14651 struct dwarf2_per_objfile *dwarf2_per_objfile
14652 = cu->per_cu->dwarf2_per_objfile;
c906108c 14653 struct attribute *attr;
91da1414 14654 struct attribute *attr_high;
af34e669
DJ
14655 CORE_ADDR low = 0;
14656 CORE_ADDR high = 0;
e385593e 14657 enum pc_bounds_kind ret;
c906108c 14658
91da1414
MW
14659 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14660 if (attr_high)
af34e669 14661 {
e142c38c 14662 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14663 if (attr)
91da1414 14664 {
31aa7e4e
JB
14665 low = attr_value_as_address (attr);
14666 high = attr_value_as_address (attr_high);
14667 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14668 high += low;
91da1414 14669 }
af34e669
DJ
14670 else
14671 /* Found high w/o low attribute. */
e385593e 14672 return PC_BOUNDS_INVALID;
af34e669
DJ
14673
14674 /* Found consecutive range of addresses. */
3a2b436a 14675 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14676 }
c906108c 14677 else
af34e669 14678 {
e142c38c 14679 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14680 if (attr != NULL)
14681 {
ab435259
DE
14682 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14683 We take advantage of the fact that DW_AT_ranges does not appear
14684 in DW_TAG_compile_unit of DWO files. */
14685 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14686 unsigned int ranges_offset = (DW_UNSND (attr)
14687 + (need_ranges_base
14688 ? cu->ranges_base
14689 : 0));
2e3cf129 14690
af34e669 14691 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14692 .debug_ranges section. */
2e3cf129 14693 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14694 return PC_BOUNDS_INVALID;
43039443 14695 /* Found discontinuous range of addresses. */
3a2b436a 14696 ret = PC_BOUNDS_RANGES;
af34e669 14697 }
e385593e
JK
14698 else
14699 return PC_BOUNDS_NOT_PRESENT;
af34e669 14700 }
c906108c 14701
48fbe735 14702 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14703 if (high <= low)
e385593e 14704 return PC_BOUNDS_INVALID;
c906108c
SS
14705
14706 /* When using the GNU linker, .gnu.linkonce. sections are used to
14707 eliminate duplicate copies of functions and vtables and such.
14708 The linker will arbitrarily choose one and discard the others.
14709 The AT_*_pc values for such functions refer to local labels in
14710 these sections. If the section from that file was discarded, the
14711 labels are not in the output, so the relocs get a value of 0.
14712 If this is a discarded function, mark the pc bounds as invalid,
14713 so that GDB will ignore it. */
72dca2f5 14714 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14715 return PC_BOUNDS_INVALID;
c906108c
SS
14716
14717 *lowpc = low;
96408a79
SA
14718 if (highpc)
14719 *highpc = high;
af34e669 14720 return ret;
c906108c
SS
14721}
14722
b084d499
JB
14723/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14724 its low and high PC addresses. Do nothing if these addresses could not
14725 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14726 and HIGHPC to the high address if greater than HIGHPC. */
14727
14728static void
14729dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14730 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14731 struct dwarf2_cu *cu)
14732{
14733 CORE_ADDR low, high;
14734 struct die_info *child = die->child;
14735
e385593e 14736 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14737 {
325fac50
PA
14738 *lowpc = std::min (*lowpc, low);
14739 *highpc = std::max (*highpc, high);
b084d499
JB
14740 }
14741
14742 /* If the language does not allow nested subprograms (either inside
14743 subprograms or lexical blocks), we're done. */
14744 if (cu->language != language_ada)
14745 return;
6e70227d 14746
b084d499
JB
14747 /* Check all the children of the given DIE. If it contains nested
14748 subprograms, then check their pc bounds. Likewise, we need to
14749 check lexical blocks as well, as they may also contain subprogram
14750 definitions. */
14751 while (child && child->tag)
14752 {
14753 if (child->tag == DW_TAG_subprogram
14754 || child->tag == DW_TAG_lexical_block)
14755 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14756 child = sibling_die (child);
14757 }
14758}
14759
fae299cd
DC
14760/* Get the low and high pc's represented by the scope DIE, and store
14761 them in *LOWPC and *HIGHPC. If the correct values can't be
14762 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14763
14764static void
14765get_scope_pc_bounds (struct die_info *die,
14766 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14767 struct dwarf2_cu *cu)
14768{
14769 CORE_ADDR best_low = (CORE_ADDR) -1;
14770 CORE_ADDR best_high = (CORE_ADDR) 0;
14771 CORE_ADDR current_low, current_high;
14772
3a2b436a 14773 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14774 >= PC_BOUNDS_RANGES)
fae299cd
DC
14775 {
14776 best_low = current_low;
14777 best_high = current_high;
14778 }
14779 else
14780 {
14781 struct die_info *child = die->child;
14782
14783 while (child && child->tag)
14784 {
14785 switch (child->tag) {
14786 case DW_TAG_subprogram:
b084d499 14787 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14788 break;
14789 case DW_TAG_namespace:
f55ee35c 14790 case DW_TAG_module:
fae299cd
DC
14791 /* FIXME: carlton/2004-01-16: Should we do this for
14792 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14793 that current GCC's always emit the DIEs corresponding
14794 to definitions of methods of classes as children of a
14795 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14796 the DIEs giving the declarations, which could be
14797 anywhere). But I don't see any reason why the
14798 standards says that they have to be there. */
14799 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14800
14801 if (current_low != ((CORE_ADDR) -1))
14802 {
325fac50
PA
14803 best_low = std::min (best_low, current_low);
14804 best_high = std::max (best_high, current_high);
fae299cd
DC
14805 }
14806 break;
14807 default:
0963b4bd 14808 /* Ignore. */
fae299cd
DC
14809 break;
14810 }
14811
14812 child = sibling_die (child);
14813 }
14814 }
14815
14816 *lowpc = best_low;
14817 *highpc = best_high;
14818}
14819
801e3a5b
JB
14820/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14821 in DIE. */
380bca97 14822
801e3a5b
JB
14823static void
14824dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14825 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14826{
518817b3 14827 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14828 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14829 struct attribute *attr;
91da1414 14830 struct attribute *attr_high;
801e3a5b 14831
91da1414
MW
14832 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14833 if (attr_high)
801e3a5b 14834 {
801e3a5b
JB
14835 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14836 if (attr)
14837 {
31aa7e4e
JB
14838 CORE_ADDR low = attr_value_as_address (attr);
14839 CORE_ADDR high = attr_value_as_address (attr_high);
14840
14841 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14842 high += low;
9a619af0 14843
3e29f34a
MR
14844 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14845 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14846 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14847 }
14848 }
14849
14850 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14851 if (attr)
14852 {
ab435259
DE
14853 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14854 We take advantage of the fact that DW_AT_ranges does not appear
14855 in DW_TAG_compile_unit of DWO files. */
14856 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14857
14858 /* The value of the DW_AT_ranges attribute is the offset of the
14859 address range list in the .debug_ranges section. */
ab435259
DE
14860 unsigned long offset = (DW_UNSND (attr)
14861 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14862
2d5f09ec 14863 std::vector<blockrange> blockvec;
5f46c5a5
JK
14864 dwarf2_ranges_process (offset, cu,
14865 [&] (CORE_ADDR start, CORE_ADDR end)
14866 {
58fdfd2c
JK
14867 start += baseaddr;
14868 end += baseaddr;
5f46c5a5
JK
14869 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14870 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14871 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14872 blockvec.emplace_back (start, end);
5f46c5a5 14873 });
2d5f09ec
KB
14874
14875 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14876 }
14877}
14878
685b1105
JK
14879/* Check whether the producer field indicates either of GCC < 4.6, or the
14880 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14881
685b1105
JK
14882static void
14883check_producer (struct dwarf2_cu *cu)
60d5a603 14884{
38360086 14885 int major, minor;
60d5a603
JK
14886
14887 if (cu->producer == NULL)
14888 {
14889 /* For unknown compilers expect their behavior is DWARF version
14890 compliant.
14891
14892 GCC started to support .debug_types sections by -gdwarf-4 since
14893 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14894 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14895 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14896 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14897 }
b1ffba5a 14898 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14899 {
38360086
MW
14900 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14901 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14902 }
5230b05a 14903 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14904 {
14905 cu->producer_is_icc = true;
14906 cu->producer_is_icc_lt_14 = major < 14;
14907 }
c258c396
JD
14908 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14909 cu->producer_is_codewarrior = true;
685b1105
JK
14910 else
14911 {
14912 /* For other non-GCC compilers, expect their behavior is DWARF version
14913 compliant. */
60d5a603
JK
14914 }
14915
9068261f 14916 cu->checked_producer = true;
685b1105 14917}
ba919b58 14918
685b1105
JK
14919/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14920 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14921 during 4.6.0 experimental. */
14922
9068261f 14923static bool
685b1105
JK
14924producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14925{
14926 if (!cu->checked_producer)
14927 check_producer (cu);
14928
14929 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14930}
14931
c258c396
JD
14932
14933/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14934 with incorrect is_stmt attributes. */
14935
14936static bool
14937producer_is_codewarrior (struct dwarf2_cu *cu)
14938{
14939 if (!cu->checked_producer)
14940 check_producer (cu);
14941
14942 return cu->producer_is_codewarrior;
14943}
14944
405feb71 14945/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14946 DW_AT_accessibility. */
14947
14948static enum dwarf_access_attribute
14949dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14950{
14951 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14952 {
14953 /* The default DWARF 2 accessibility for members is public, the default
14954 accessibility for inheritance is private. */
14955
14956 if (die->tag != DW_TAG_inheritance)
14957 return DW_ACCESS_public;
14958 else
14959 return DW_ACCESS_private;
14960 }
14961 else
14962 {
14963 /* DWARF 3+ defines the default accessibility a different way. The same
14964 rules apply now for DW_TAG_inheritance as for the members and it only
14965 depends on the container kind. */
14966
14967 if (die->parent->tag == DW_TAG_class_type)
14968 return DW_ACCESS_private;
14969 else
14970 return DW_ACCESS_public;
14971 }
14972}
14973
74ac6d43
TT
14974/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14975 offset. If the attribute was not found return 0, otherwise return
14976 1. If it was found but could not properly be handled, set *OFFSET
14977 to 0. */
14978
14979static int
14980handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14981 LONGEST *offset)
14982{
14983 struct attribute *attr;
14984
14985 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14986 if (attr != NULL)
14987 {
14988 *offset = 0;
14989
14990 /* Note that we do not check for a section offset first here.
14991 This is because DW_AT_data_member_location is new in DWARF 4,
14992 so if we see it, we can assume that a constant form is really
14993 a constant and not a section offset. */
14994 if (attr_form_is_constant (attr))
14995 *offset = dwarf2_get_attr_constant_value (attr, 0);
14996 else if (attr_form_is_section_offset (attr))
14997 dwarf2_complex_location_expr_complaint ();
14998 else if (attr_form_is_block (attr))
14999 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15000 else
15001 dwarf2_complex_location_expr_complaint ();
15002
15003 return 1;
15004 }
15005
15006 return 0;
15007}
15008
c906108c
SS
15009/* Add an aggregate field to the field list. */
15010
15011static void
107d2387 15012dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15013 struct dwarf2_cu *cu)
6e70227d 15014{
518817b3 15015 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15016 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15017 struct nextfield *new_field;
15018 struct attribute *attr;
15019 struct field *fp;
15d034d0 15020 const char *fieldname = "";
c906108c 15021
7d0ccb61
DJ
15022 if (die->tag == DW_TAG_inheritance)
15023 {
be2daae6
TT
15024 fip->baseclasses.emplace_back ();
15025 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15026 }
15027 else
15028 {
be2daae6
TT
15029 fip->fields.emplace_back ();
15030 new_field = &fip->fields.back ();
7d0ccb61 15031 }
be2daae6 15032
c906108c
SS
15033 fip->nfields++;
15034
e142c38c 15035 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15036 if (attr)
15037 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15038 else
15039 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15040 if (new_field->accessibility != DW_ACCESS_public)
15041 fip->non_public_fields = 1;
60d5a603 15042
e142c38c 15043 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15044 if (attr)
15045 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15046 else
15047 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15048
15049 fp = &new_field->field;
a9a9bd0f 15050
e142c38c 15051 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15052 {
74ac6d43
TT
15053 LONGEST offset;
15054
a9a9bd0f 15055 /* Data member other than a C++ static data member. */
6e70227d 15056
c906108c 15057 /* Get type of field. */
e7c27a73 15058 fp->type = die_type (die, cu);
c906108c 15059
d6a843b5 15060 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15061
c906108c 15062 /* Get bit size of field (zero if none). */
e142c38c 15063 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15064 if (attr)
15065 {
15066 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15067 }
15068 else
15069 {
15070 FIELD_BITSIZE (*fp) = 0;
15071 }
15072
15073 /* Get bit offset of field. */
74ac6d43
TT
15074 if (handle_data_member_location (die, cu, &offset))
15075 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15076 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15077 if (attr)
15078 {
5e2b427d 15079 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15080 {
15081 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15082 additional bit offset from the MSB of the containing
15083 anonymous object to the MSB of the field. We don't
15084 have to do anything special since we don't need to
15085 know the size of the anonymous object. */
f41f5e61 15086 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15087 }
15088 else
15089 {
15090 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15091 MSB of the anonymous object, subtract off the number of
15092 bits from the MSB of the field to the MSB of the
15093 object, and then subtract off the number of bits of
15094 the field itself. The result is the bit offset of
15095 the LSB of the field. */
c906108c
SS
15096 int anonymous_size;
15097 int bit_offset = DW_UNSND (attr);
15098
e142c38c 15099 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15100 if (attr)
15101 {
15102 /* The size of the anonymous object containing
15103 the bit field is explicit, so use the
15104 indicated size (in bytes). */
15105 anonymous_size = DW_UNSND (attr);
15106 }
15107 else
15108 {
15109 /* The size of the anonymous object containing
15110 the bit field must be inferred from the type
15111 attribute of the data member containing the
15112 bit field. */
15113 anonymous_size = TYPE_LENGTH (fp->type);
15114 }
f41f5e61
PA
15115 SET_FIELD_BITPOS (*fp,
15116 (FIELD_BITPOS (*fp)
15117 + anonymous_size * bits_per_byte
15118 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15119 }
15120 }
da5b30da
AA
15121 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15122 if (attr != NULL)
15123 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15124 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15125
15126 /* Get name of field. */
39cbfefa
DJ
15127 fieldname = dwarf2_name (die, cu);
15128 if (fieldname == NULL)
15129 fieldname = "";
d8151005
DJ
15130
15131 /* The name is already allocated along with this objfile, so we don't
15132 need to duplicate it for the type. */
15133 fp->name = fieldname;
c906108c
SS
15134
15135 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15136 pointer or virtual base class pointer) to private. */
e142c38c 15137 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15138 {
d48cc9dd 15139 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15140 new_field->accessibility = DW_ACCESS_private;
15141 fip->non_public_fields = 1;
15142 }
15143 }
a9a9bd0f 15144 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15145 {
a9a9bd0f
DC
15146 /* C++ static member. */
15147
15148 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15149 is a declaration, but all versions of G++ as of this writing
15150 (so through at least 3.2.1) incorrectly generate
15151 DW_TAG_variable tags. */
6e70227d 15152
ff355380 15153 const char *physname;
c906108c 15154
a9a9bd0f 15155 /* Get name of field. */
39cbfefa
DJ
15156 fieldname = dwarf2_name (die, cu);
15157 if (fieldname == NULL)
c906108c
SS
15158 return;
15159
254e6b9e 15160 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15161 if (attr
15162 /* Only create a symbol if this is an external value.
15163 new_symbol checks this and puts the value in the global symbol
15164 table, which we want. If it is not external, new_symbol
15165 will try to put the value in cu->list_in_scope which is wrong. */
15166 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15167 {
15168 /* A static const member, not much different than an enum as far as
15169 we're concerned, except that we can support more types. */
15170 new_symbol (die, NULL, cu);
15171 }
15172
2df3850c 15173 /* Get physical name. */
ff355380 15174 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15175
d8151005
DJ
15176 /* The name is already allocated along with this objfile, so we don't
15177 need to duplicate it for the type. */
15178 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15179 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15180 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15181 }
15182 else if (die->tag == DW_TAG_inheritance)
15183 {
74ac6d43 15184 LONGEST offset;
d4b96c9a 15185
74ac6d43
TT
15186 /* C++ base class field. */
15187 if (handle_data_member_location (die, cu, &offset))
15188 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15189 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15190 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15191 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15192 }
2ddeaf8a
TT
15193 else if (die->tag == DW_TAG_variant_part)
15194 {
15195 /* process_structure_scope will treat this DIE as a union. */
15196 process_structure_scope (die, cu);
15197
15198 /* The variant part is relative to the start of the enclosing
15199 structure. */
15200 SET_FIELD_BITPOS (*fp, 0);
15201 fp->type = get_die_type (die, cu);
15202 fp->artificial = 1;
15203 fp->name = "<<variant>>";
c8c81635
TT
15204
15205 /* Normally a DW_TAG_variant_part won't have a size, but our
15206 representation requires one, so set it to the maximum of the
15207 child sizes. */
15208 if (TYPE_LENGTH (fp->type) == 0)
15209 {
15210 unsigned max = 0;
15211 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15212 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15213 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15214 TYPE_LENGTH (fp->type) = max;
15215 }
2ddeaf8a
TT
15216 }
15217 else
15218 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15219}
15220
883fd55a
KS
15221/* Can the type given by DIE define another type? */
15222
15223static bool
15224type_can_define_types (const struct die_info *die)
15225{
15226 switch (die->tag)
15227 {
15228 case DW_TAG_typedef:
15229 case DW_TAG_class_type:
15230 case DW_TAG_structure_type:
15231 case DW_TAG_union_type:
15232 case DW_TAG_enumeration_type:
15233 return true;
15234
15235 default:
15236 return false;
15237 }
15238}
15239
15240/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15241
15242static void
883fd55a
KS
15243dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15244 struct dwarf2_cu *cu)
6e70227d 15245{
be2daae6
TT
15246 struct decl_field fp;
15247 memset (&fp, 0, sizeof (fp));
98751a41 15248
883fd55a 15249 gdb_assert (type_can_define_types (die));
98751a41 15250
883fd55a 15251 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15252 fp.name = dwarf2_name (die, cu);
15253 fp.type = read_type_die (die, cu);
98751a41 15254
c191a687
KS
15255 /* Save accessibility. */
15256 enum dwarf_access_attribute accessibility;
15257 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15258 if (attr != NULL)
15259 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15260 else
15261 accessibility = dwarf2_default_access_attribute (die, cu);
15262 switch (accessibility)
15263 {
15264 case DW_ACCESS_public:
15265 /* The assumed value if neither private nor protected. */
15266 break;
15267 case DW_ACCESS_private:
be2daae6 15268 fp.is_private = 1;
c191a687
KS
15269 break;
15270 case DW_ACCESS_protected:
be2daae6 15271 fp.is_protected = 1;
c191a687
KS
15272 break;
15273 default:
b98664d3 15274 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15275 }
15276
883fd55a 15277 if (die->tag == DW_TAG_typedef)
be2daae6 15278 fip->typedef_field_list.push_back (fp);
883fd55a 15279 else
be2daae6 15280 fip->nested_types_list.push_back (fp);
98751a41
JK
15281}
15282
c906108c
SS
15283/* Create the vector of fields, and attach it to the type. */
15284
15285static void
fba45db2 15286dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15287 struct dwarf2_cu *cu)
c906108c
SS
15288{
15289 int nfields = fip->nfields;
15290
15291 /* Record the field count, allocate space for the array of fields,
15292 and create blank accessibility bitfields if necessary. */
15293 TYPE_NFIELDS (type) = nfields;
15294 TYPE_FIELDS (type) = (struct field *)
be2daae6 15295 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15296
b4ba55a1 15297 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15298 {
15299 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15300
15301 TYPE_FIELD_PRIVATE_BITS (type) =
15302 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15303 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15304
15305 TYPE_FIELD_PROTECTED_BITS (type) =
15306 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15307 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15308
774b6a14
TT
15309 TYPE_FIELD_IGNORE_BITS (type) =
15310 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15311 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15312 }
15313
15314 /* If the type has baseclasses, allocate and clear a bit vector for
15315 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15316 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15317 {
be2daae6 15318 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15319 unsigned char *pointer;
c906108c
SS
15320
15321 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15322 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15323 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15324 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15325 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15326 }
15327
2ddeaf8a
TT
15328 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15329 {
15330 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15331
be2daae6 15332 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15333 {
be2daae6
TT
15334 struct nextfield &field = fip->fields[index];
15335
15336 if (field.variant.is_discriminant)
2ddeaf8a 15337 di->discriminant_index = index;
be2daae6 15338 else if (field.variant.default_branch)
2ddeaf8a
TT
15339 di->default_index = index;
15340 else
be2daae6 15341 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15342 }
15343 }
15344
be2daae6
TT
15345 /* Copy the saved-up fields into the field vector. */
15346 for (int i = 0; i < nfields; ++i)
c906108c 15347 {
be2daae6
TT
15348 struct nextfield &field
15349 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15350 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15351
be2daae6
TT
15352 TYPE_FIELD (type, i) = field.field;
15353 switch (field.accessibility)
c906108c 15354 {
c5aa993b 15355 case DW_ACCESS_private:
b4ba55a1 15356 if (cu->language != language_ada)
be2daae6 15357 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15358 break;
c906108c 15359
c5aa993b 15360 case DW_ACCESS_protected:
b4ba55a1 15361 if (cu->language != language_ada)
be2daae6 15362 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15363 break;
c906108c 15364
c5aa993b
JM
15365 case DW_ACCESS_public:
15366 break;
c906108c 15367
c5aa993b
JM
15368 default:
15369 /* Unknown accessibility. Complain and treat it as public. */
15370 {
b98664d3 15371 complaint (_("unsupported accessibility %d"),
be2daae6 15372 field.accessibility);
c5aa993b
JM
15373 }
15374 break;
c906108c 15375 }
be2daae6 15376 if (i < fip->baseclasses.size ())
c906108c 15377 {
be2daae6 15378 switch (field.virtuality)
c906108c 15379 {
c5aa993b
JM
15380 case DW_VIRTUALITY_virtual:
15381 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15382 if (cu->language == language_ada)
a73c6dcd 15383 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15384 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15385 break;
c906108c
SS
15386 }
15387 }
c906108c
SS
15388 }
15389}
15390
7d27a96d
TT
15391/* Return true if this member function is a constructor, false
15392 otherwise. */
15393
15394static int
15395dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15396{
15397 const char *fieldname;
fe978cb0 15398 const char *type_name;
7d27a96d
TT
15399 int len;
15400
15401 if (die->parent == NULL)
15402 return 0;
15403
15404 if (die->parent->tag != DW_TAG_structure_type
15405 && die->parent->tag != DW_TAG_union_type
15406 && die->parent->tag != DW_TAG_class_type)
15407 return 0;
15408
15409 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15410 type_name = dwarf2_name (die->parent, cu);
15411 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15412 return 0;
15413
15414 len = strlen (fieldname);
fe978cb0
PA
15415 return (strncmp (fieldname, type_name, len) == 0
15416 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15417}
15418
c906108c
SS
15419/* Add a member function to the proper fieldlist. */
15420
15421static void
107d2387 15422dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15423 struct type *type, struct dwarf2_cu *cu)
c906108c 15424{
518817b3 15425 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15426 struct attribute *attr;
c906108c 15427 int i;
be2daae6 15428 struct fnfieldlist *flp = nullptr;
c906108c 15429 struct fn_field *fnp;
15d034d0 15430 const char *fieldname;
f792889a 15431 struct type *this_type;
60d5a603 15432 enum dwarf_access_attribute accessibility;
c906108c 15433
b4ba55a1 15434 if (cu->language == language_ada)
a73c6dcd 15435 error (_("unexpected member function in Ada type"));
b4ba55a1 15436
2df3850c 15437 /* Get name of member function. */
39cbfefa
DJ
15438 fieldname = dwarf2_name (die, cu);
15439 if (fieldname == NULL)
2df3850c 15440 return;
c906108c 15441
c906108c 15442 /* Look up member function name in fieldlist. */
be2daae6 15443 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15444 {
27bfe10e 15445 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15446 {
15447 flp = &fip->fnfieldlists[i];
15448 break;
15449 }
c906108c
SS
15450 }
15451
be2daae6
TT
15452 /* Create a new fnfieldlist if necessary. */
15453 if (flp == nullptr)
c906108c 15454 {
be2daae6
TT
15455 fip->fnfieldlists.emplace_back ();
15456 flp = &fip->fnfieldlists.back ();
c906108c 15457 flp->name = fieldname;
be2daae6 15458 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15459 }
15460
be2daae6
TT
15461 /* Create a new member function field and add it to the vector of
15462 fnfieldlists. */
15463 flp->fnfields.emplace_back ();
15464 fnp = &flp->fnfields.back ();
3da10d80
KS
15465
15466 /* Delay processing of the physname until later. */
9c37b5ae 15467 if (cu->language == language_cplus)
be2daae6
TT
15468 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15469 die, cu);
3da10d80
KS
15470 else
15471 {
1d06ead6 15472 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15473 fnp->physname = physname ? physname : "";
15474 }
15475
c906108c 15476 fnp->type = alloc_type (objfile);
f792889a
DJ
15477 this_type = read_type_die (die, cu);
15478 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15479 {
f792889a 15480 int nparams = TYPE_NFIELDS (this_type);
c906108c 15481
f792889a 15482 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15483 of the method itself (TYPE_CODE_METHOD). */
15484 smash_to_method_type (fnp->type, type,
f792889a
DJ
15485 TYPE_TARGET_TYPE (this_type),
15486 TYPE_FIELDS (this_type),
15487 TYPE_NFIELDS (this_type),
15488 TYPE_VARARGS (this_type));
c906108c
SS
15489
15490 /* Handle static member functions.
c5aa993b 15491 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15492 member functions. G++ helps GDB by marking the first
15493 parameter for non-static member functions (which is the this
15494 pointer) as artificial. We obtain this information from
15495 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15496 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15497 fnp->voffset = VOFFSET_STATIC;
15498 }
15499 else
b98664d3 15500 complaint (_("member function type missing for '%s'"),
3da10d80 15501 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15502
15503 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15504 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15505 fnp->fcontext = die_containing_type (die, cu);
c906108c 15506
3e43a32a
MS
15507 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15508 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15509
15510 /* Get accessibility. */
e142c38c 15511 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15512 if (attr)
aead7601 15513 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15514 else
15515 accessibility = dwarf2_default_access_attribute (die, cu);
15516 switch (accessibility)
c906108c 15517 {
60d5a603
JK
15518 case DW_ACCESS_private:
15519 fnp->is_private = 1;
15520 break;
15521 case DW_ACCESS_protected:
15522 fnp->is_protected = 1;
15523 break;
c906108c
SS
15524 }
15525
b02dede2 15526 /* Check for artificial methods. */
e142c38c 15527 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15528 if (attr && DW_UNSND (attr) != 0)
15529 fnp->is_artificial = 1;
15530
7d27a96d
TT
15531 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15532
0d564a31 15533 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15534 function. For older versions of GCC, this is an offset in the
15535 appropriate virtual table, as specified by DW_AT_containing_type.
15536 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15537 to the object address. */
15538
e142c38c 15539 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15540 if (attr)
8e19ed76 15541 {
aec5aa8b 15542 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15543 {
aec5aa8b
TT
15544 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15545 {
15546 /* Old-style GCC. */
15547 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15548 }
15549 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15550 || (DW_BLOCK (attr)->size > 1
15551 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15552 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15553 {
aec5aa8b
TT
15554 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15555 if ((fnp->voffset % cu->header.addr_size) != 0)
15556 dwarf2_complex_location_expr_complaint ();
15557 else
15558 fnp->voffset /= cu->header.addr_size;
15559 fnp->voffset += 2;
15560 }
15561 else
15562 dwarf2_complex_location_expr_complaint ();
15563
15564 if (!fnp->fcontext)
7e993ebf
KS
15565 {
15566 /* If there is no `this' field and no DW_AT_containing_type,
15567 we cannot actually find a base class context for the
15568 vtable! */
15569 if (TYPE_NFIELDS (this_type) == 0
15570 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15571 {
b98664d3 15572 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15573 "function \"%s\" (offset %s)"),
15574 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15575 }
15576 else
15577 {
15578 fnp->fcontext
15579 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15580 }
15581 }
aec5aa8b 15582 }
3690dd37 15583 else if (attr_form_is_section_offset (attr))
8e19ed76 15584 {
4d3c2250 15585 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15586 }
15587 else
15588 {
4d3c2250
KB
15589 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15590 fieldname);
8e19ed76 15591 }
0d564a31 15592 }
d48cc9dd
DJ
15593 else
15594 {
15595 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15596 if (attr && DW_UNSND (attr))
15597 {
15598 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15599 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15600 "but the vtable offset is not specified"),
9d8780f0 15601 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15602 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15603 TYPE_CPLUS_DYNAMIC (type) = 1;
15604 }
15605 }
c906108c
SS
15606}
15607
15608/* Create the vector of member function fields, and attach it to the type. */
15609
15610static void
fba45db2 15611dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15612 struct dwarf2_cu *cu)
c906108c 15613{
b4ba55a1 15614 if (cu->language == language_ada)
a73c6dcd 15615 error (_("unexpected member functions in Ada type"));
b4ba55a1 15616
c906108c
SS
15617 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15618 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15619 TYPE_ALLOC (type,
15620 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15621
be2daae6 15622 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15623 {
be2daae6 15624 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15625 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15626
be2daae6
TT
15627 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15628 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15629 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15630 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15631
15632 for (int k = 0; k < nf.fnfields.size (); ++k)
15633 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15634 }
15635
be2daae6 15636 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15637}
15638
1168df01
JB
15639/* Returns non-zero if NAME is the name of a vtable member in CU's
15640 language, zero otherwise. */
15641static int
15642is_vtable_name (const char *name, struct dwarf2_cu *cu)
15643{
15644 static const char vptr[] = "_vptr";
15645
9c37b5ae
TT
15646 /* Look for the C++ form of the vtable. */
15647 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15648 return 1;
15649
15650 return 0;
15651}
15652
c0dd20ea 15653/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15654 functions, with the ABI-specified layout. If TYPE describes
15655 such a structure, smash it into a member function type.
61049d3b
DJ
15656
15657 GCC shouldn't do this; it should just output pointer to member DIEs.
15658 This is GCC PR debug/28767. */
c0dd20ea 15659
0b92b5bb
TT
15660static void
15661quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15662{
09e2d7c7 15663 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15664
15665 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15666 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15667 return;
c0dd20ea
DJ
15668
15669 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15670 if (TYPE_FIELD_NAME (type, 0) == NULL
15671 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15672 || TYPE_FIELD_NAME (type, 1) == NULL
15673 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15674 return;
c0dd20ea
DJ
15675
15676 /* Find the type of the method. */
0b92b5bb 15677 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15678 if (pfn_type == NULL
15679 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15680 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15681 return;
c0dd20ea
DJ
15682
15683 /* Look for the "this" argument. */
15684 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15685 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15686 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15687 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15688 return;
c0dd20ea 15689
09e2d7c7 15690 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15691 new_type = alloc_type (objfile);
09e2d7c7 15692 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15693 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15694 TYPE_VARARGS (pfn_type));
0b92b5bb 15695 smash_to_methodptr_type (type, new_type);
c0dd20ea 15696}
1168df01 15697
2b4424c3
TT
15698/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15699 appropriate error checking and issuing complaints if there is a
15700 problem. */
15701
15702static ULONGEST
15703get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15704{
15705 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15706
15707 if (attr == nullptr)
15708 return 0;
15709
15710 if (!attr_form_is_constant (attr))
15711 {
b98664d3 15712 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15713 " - DIE at %s [in module %s]"),
15714 sect_offset_str (die->sect_off),
15715 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15716 return 0;
15717 }
15718
15719 ULONGEST align;
15720 if (attr->form == DW_FORM_sdata)
15721 {
15722 LONGEST val = DW_SND (attr);
15723 if (val < 0)
15724 {
b98664d3 15725 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15726 " - DIE at %s [in module %s]"),
15727 sect_offset_str (die->sect_off),
15728 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15729 return 0;
15730 }
15731 align = val;
15732 }
15733 else
15734 align = DW_UNSND (attr);
15735
15736 if (align == 0)
15737 {
b98664d3 15738 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15739 " - DIE at %s [in module %s]"),
15740 sect_offset_str (die->sect_off),
15741 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15742 return 0;
15743 }
15744 if ((align & (align - 1)) != 0)
15745 {
b98664d3 15746 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15747 " - DIE at %s [in module %s]"),
15748 sect_offset_str (die->sect_off),
15749 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15750 return 0;
15751 }
15752
15753 return align;
15754}
15755
15756/* If the DIE has a DW_AT_alignment attribute, use its value to set
15757 the alignment for TYPE. */
15758
15759static void
15760maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15761 struct type *type)
15762{
15763 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15764 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15765 " - DIE at %s [in module %s]"),
15766 sect_offset_str (die->sect_off),
15767 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15768}
685b1105 15769
c906108c 15770/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15771 (definition) to create a type for the structure or union. Fill in
15772 the type's name and general properties; the members will not be
83655187
DE
15773 processed until process_structure_scope. A symbol table entry for
15774 the type will also not be done until process_structure_scope (assuming
15775 the type has a name).
c906108c 15776
c767944b
DJ
15777 NOTE: we need to call these functions regardless of whether or not the
15778 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15779 structure or union. This gets the type entered into our set of
83655187 15780 user defined types. */
c906108c 15781
f792889a 15782static struct type *
134d01f1 15783read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15784{
518817b3 15785 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15786 struct type *type;
15787 struct attribute *attr;
15d034d0 15788 const char *name;
c906108c 15789
348e048f
DE
15790 /* If the definition of this type lives in .debug_types, read that type.
15791 Don't follow DW_AT_specification though, that will take us back up
15792 the chain and we want to go down. */
45e58e77 15793 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15794 if (attr)
15795 {
ac9ec31b 15796 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15797
ac9ec31b 15798 /* The type's CU may not be the same as CU.
02142a6c 15799 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15800 return set_die_type (die, type, cu);
15801 }
15802
c0dd20ea 15803 type = alloc_type (objfile);
c906108c 15804 INIT_CPLUS_SPECIFIC (type);
93311388 15805
39cbfefa
DJ
15806 name = dwarf2_name (die, cu);
15807 if (name != NULL)
c906108c 15808 {
987504bb 15809 if (cu->language == language_cplus
c44af4eb
TT
15810 || cu->language == language_d
15811 || cu->language == language_rust)
63d06c5c 15812 {
15d034d0 15813 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15814
15815 /* dwarf2_full_name might have already finished building the DIE's
15816 type. If so, there is no need to continue. */
15817 if (get_die_type (die, cu) != NULL)
15818 return get_die_type (die, cu);
15819
e86ca25f 15820 TYPE_NAME (type) = full_name;
63d06c5c
DC
15821 }
15822 else
15823 {
d8151005
DJ
15824 /* The name is already allocated along with this objfile, so
15825 we don't need to duplicate it for the type. */
e86ca25f 15826 TYPE_NAME (type) = name;
63d06c5c 15827 }
c906108c
SS
15828 }
15829
15830 if (die->tag == DW_TAG_structure_type)
15831 {
15832 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15833 }
15834 else if (die->tag == DW_TAG_union_type)
15835 {
15836 TYPE_CODE (type) = TYPE_CODE_UNION;
15837 }
2ddeaf8a
TT
15838 else if (die->tag == DW_TAG_variant_part)
15839 {
15840 TYPE_CODE (type) = TYPE_CODE_UNION;
15841 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15842 }
c906108c
SS
15843 else
15844 {
4753d33b 15845 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15846 }
15847
0cc2414c
TT
15848 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15849 TYPE_DECLARED_CLASS (type) = 1;
15850
e142c38c 15851 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15852 if (attr)
15853 {
155bfbd3
JB
15854 if (attr_form_is_constant (attr))
15855 TYPE_LENGTH (type) = DW_UNSND (attr);
15856 else
15857 {
15858 /* For the moment, dynamic type sizes are not supported
15859 by GDB's struct type. The actual size is determined
15860 on-demand when resolving the type of a given object,
15861 so set the type's length to zero for now. Otherwise,
15862 we record an expression as the length, and that expression
15863 could lead to a very large value, which could eventually
15864 lead to us trying to allocate that much memory when creating
15865 a value of that type. */
15866 TYPE_LENGTH (type) = 0;
15867 }
c906108c
SS
15868 }
15869 else
15870 {
15871 TYPE_LENGTH (type) = 0;
15872 }
15873
2b4424c3
TT
15874 maybe_set_alignment (cu, die, type);
15875
5230b05a 15876 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15877 {
5230b05a
WT
15878 /* ICC<14 does not output the required DW_AT_declaration on
15879 incomplete types, but gives them a size of zero. */
422b1cb0 15880 TYPE_STUB (type) = 1;
685b1105
JK
15881 }
15882 else
15883 TYPE_STUB_SUPPORTED (type) = 1;
15884
dc718098 15885 if (die_is_declaration (die, cu))
876cecd0 15886 TYPE_STUB (type) = 1;
a6c727b2
DJ
15887 else if (attr == NULL && die->child == NULL
15888 && producer_is_realview (cu->producer))
15889 /* RealView does not output the required DW_AT_declaration
15890 on incomplete types. */
15891 TYPE_STUB (type) = 1;
dc718098 15892
c906108c
SS
15893 /* We need to add the type field to the die immediately so we don't
15894 infinitely recurse when dealing with pointers to the structure
0963b4bd 15895 type within the structure itself. */
1c379e20 15896 set_die_type (die, type, cu);
c906108c 15897
7e314c57
JK
15898 /* set_die_type should be already done. */
15899 set_descriptive_type (type, die, cu);
15900
c767944b
DJ
15901 return type;
15902}
15903
2ddeaf8a
TT
15904/* A helper for process_structure_scope that handles a single member
15905 DIE. */
15906
15907static void
15908handle_struct_member_die (struct die_info *child_die, struct type *type,
15909 struct field_info *fi,
15910 std::vector<struct symbol *> *template_args,
15911 struct dwarf2_cu *cu)
15912{
15913 if (child_die->tag == DW_TAG_member
15914 || child_die->tag == DW_TAG_variable
15915 || child_die->tag == DW_TAG_variant_part)
15916 {
15917 /* NOTE: carlton/2002-11-05: A C++ static data member
15918 should be a DW_TAG_member that is a declaration, but
15919 all versions of G++ as of this writing (so through at
15920 least 3.2.1) incorrectly generate DW_TAG_variable
15921 tags for them instead. */
15922 dwarf2_add_field (fi, child_die, cu);
15923 }
15924 else if (child_die->tag == DW_TAG_subprogram)
15925 {
15926 /* Rust doesn't have member functions in the C++ sense.
15927 However, it does emit ordinary functions as children
15928 of a struct DIE. */
15929 if (cu->language == language_rust)
15930 read_func_scope (child_die, cu);
15931 else
15932 {
15933 /* C++ member function. */
15934 dwarf2_add_member_fn (fi, child_die, type, cu);
15935 }
15936 }
15937 else if (child_die->tag == DW_TAG_inheritance)
15938 {
15939 /* C++ base class field. */
15940 dwarf2_add_field (fi, child_die, cu);
15941 }
15942 else if (type_can_define_types (child_die))
15943 dwarf2_add_type_defn (fi, child_die, cu);
15944 else if (child_die->tag == DW_TAG_template_type_param
15945 || child_die->tag == DW_TAG_template_value_param)
15946 {
15947 struct symbol *arg = new_symbol (child_die, NULL, cu);
15948
15949 if (arg != NULL)
15950 template_args->push_back (arg);
15951 }
15952 else if (child_die->tag == DW_TAG_variant)
15953 {
15954 /* In a variant we want to get the discriminant and also add a
15955 field for our sole member child. */
15956 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15957
bde09ab7 15958 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15959 variant_child != NULL;
15960 variant_child = sibling_die (variant_child))
15961 {
15962 if (variant_child->tag == DW_TAG_member)
15963 {
15964 handle_struct_member_die (variant_child, type, fi,
15965 template_args, cu);
15966 /* Only handle the one. */
15967 break;
15968 }
15969 }
15970
15971 /* We don't handle this but we might as well report it if we see
15972 it. */
15973 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15974 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15975 " - DIE at %s [in module %s]"),
15976 sect_offset_str (child_die->sect_off),
15977 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15978
15979 /* The first field was just added, so we can stash the
15980 discriminant there. */
be2daae6 15981 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15982 if (discr == NULL)
be2daae6 15983 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15984 else
be2daae6 15985 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15986 }
15987}
15988
c767944b
DJ
15989/* Finish creating a structure or union type, including filling in
15990 its members and creating a symbol for it. */
15991
15992static void
15993process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15994{
518817b3 15995 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15996 struct die_info *child_die;
c767944b
DJ
15997 struct type *type;
15998
15999 type = get_die_type (die, cu);
16000 if (type == NULL)
16001 type = read_structure_type (die, cu);
16002
2ddeaf8a
TT
16003 /* When reading a DW_TAG_variant_part, we need to notice when we
16004 read the discriminant member, so we can record it later in the
16005 discriminant_info. */
16006 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16007 sect_offset discr_offset;
3e1d3d8c 16008 bool has_template_parameters = false;
2ddeaf8a
TT
16009
16010 if (is_variant_part)
16011 {
16012 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16013 if (discr == NULL)
16014 {
16015 /* Maybe it's a univariant form, an extension we support.
16016 In this case arrange not to check the offset. */
16017 is_variant_part = false;
16018 }
16019 else if (attr_form_is_ref (discr))
16020 {
16021 struct dwarf2_cu *target_cu = cu;
16022 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16023
16024 discr_offset = target_die->sect_off;
16025 }
16026 else
16027 {
b98664d3 16028 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16029 " - DIE at %s [in module %s]"),
16030 sect_offset_str (die->sect_off),
16031 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16032 is_variant_part = false;
16033 }
16034 }
16035
e142c38c 16036 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16037 {
16038 struct field_info fi;
2f4732b0 16039 std::vector<struct symbol *> template_args;
c906108c 16040
639d11d3 16041 child_die = die->child;
c906108c
SS
16042
16043 while (child_die && child_die->tag)
16044 {
2ddeaf8a 16045 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16046
2ddeaf8a 16047 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16048 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16049
c906108c
SS
16050 child_die = sibling_die (child_die);
16051 }
16052
34eaf542 16053 /* Attach template arguments to type. */
2f4732b0 16054 if (!template_args.empty ())
34eaf542 16055 {
3e1d3d8c 16056 has_template_parameters = true;
34eaf542 16057 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16058 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16059 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16060 = XOBNEWVEC (&objfile->objfile_obstack,
16061 struct symbol *,
16062 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16063 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16064 template_args.data (),
34eaf542
TT
16065 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16066 * sizeof (struct symbol *)));
34eaf542
TT
16067 }
16068
c906108c
SS
16069 /* Attach fields and member functions to the type. */
16070 if (fi.nfields)
e7c27a73 16071 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16072 if (!fi.fnfieldlists.empty ())
c906108c 16073 {
e7c27a73 16074 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16075
c5aa993b 16076 /* Get the type which refers to the base class (possibly this
c906108c 16077 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16078 class from the DW_AT_containing_type attribute. This use of
16079 DW_AT_containing_type is a GNU extension. */
c906108c 16080
e142c38c 16081 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16082 {
e7c27a73 16083 struct type *t = die_containing_type (die, cu);
c906108c 16084
ae6ae975 16085 set_type_vptr_basetype (type, t);
c906108c
SS
16086 if (type == t)
16087 {
c906108c
SS
16088 int i;
16089
16090 /* Our own class provides vtbl ptr. */
16091 for (i = TYPE_NFIELDS (t) - 1;
16092 i >= TYPE_N_BASECLASSES (t);
16093 --i)
16094 {
0d5cff50 16095 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16096
1168df01 16097 if (is_vtable_name (fieldname, cu))
c906108c 16098 {
ae6ae975 16099 set_type_vptr_fieldno (type, i);
c906108c
SS
16100 break;
16101 }
16102 }
16103
16104 /* Complain if virtual function table field not found. */
16105 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16106 complaint (_("virtual function table pointer "
3e43a32a 16107 "not found when defining class '%s'"),
e86ca25f 16108 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16109 }
16110 else
16111 {
ae6ae975 16112 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16113 }
16114 }
f6235d4c 16115 else if (cu->producer
61012eef 16116 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16117 {
16118 /* The IBM XLC compiler does not provide direct indication
16119 of the containing type, but the vtable pointer is
16120 always named __vfp. */
16121
16122 int i;
16123
16124 for (i = TYPE_NFIELDS (type) - 1;
16125 i >= TYPE_N_BASECLASSES (type);
16126 --i)
16127 {
16128 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16129 {
ae6ae975
DE
16130 set_type_vptr_fieldno (type, i);
16131 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16132 break;
16133 }
16134 }
16135 }
c906108c 16136 }
98751a41
JK
16137
16138 /* Copy fi.typedef_field_list linked list elements content into the
16139 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16140 if (!fi.typedef_field_list.empty ())
98751a41 16141 {
be2daae6 16142 int count = fi.typedef_field_list.size ();
98751a41 16143
a0d7a4ff 16144 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16145 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16146 = ((struct decl_field *)
be2daae6
TT
16147 TYPE_ALLOC (type,
16148 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16149 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16150
be2daae6
TT
16151 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16152 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16153 }
c767944b 16154
883fd55a
KS
16155 /* Copy fi.nested_types_list linked list elements content into the
16156 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16157 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16158 {
be2daae6 16159 int count = fi.nested_types_list.size ();
883fd55a
KS
16160
16161 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16162 TYPE_NESTED_TYPES_ARRAY (type)
16163 = ((struct decl_field *)
be2daae6
TT
16164 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16165 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16166
be2daae6
TT
16167 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16168 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16169 }
c906108c 16170 }
63d06c5c 16171
bb5ed363 16172 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16173 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16174 cu->rust_unions.push_back (type);
0b92b5bb 16175
90aeadfc
DC
16176 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16177 snapshots) has been known to create a die giving a declaration
16178 for a class that has, as a child, a die giving a definition for a
16179 nested class. So we have to process our children even if the
16180 current die is a declaration. Normally, of course, a declaration
16181 won't have any children at all. */
134d01f1 16182
ca040673
DE
16183 child_die = die->child;
16184
90aeadfc
DC
16185 while (child_die != NULL && child_die->tag)
16186 {
16187 if (child_die->tag == DW_TAG_member
16188 || child_die->tag == DW_TAG_variable
34eaf542
TT
16189 || child_die->tag == DW_TAG_inheritance
16190 || child_die->tag == DW_TAG_template_value_param
16191 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16192 {
90aeadfc 16193 /* Do nothing. */
134d01f1 16194 }
90aeadfc
DC
16195 else
16196 process_die (child_die, cu);
134d01f1 16197
90aeadfc 16198 child_die = sibling_die (child_die);
134d01f1
DJ
16199 }
16200
fa4028e9
JB
16201 /* Do not consider external references. According to the DWARF standard,
16202 these DIEs are identified by the fact that they have no byte_size
16203 attribute, and a declaration attribute. */
16204 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16205 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16206 {
16207 struct symbol *sym = new_symbol (die, type, cu);
16208
16209 if (has_template_parameters)
16210 {
a776957c
TT
16211 struct symtab *symtab;
16212 if (sym != nullptr)
16213 symtab = symbol_symtab (sym);
16214 else if (cu->line_header != nullptr)
16215 {
16216 /* Any related symtab will do. */
16217 symtab
16218 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16219 }
16220 else
16221 {
16222 symtab = nullptr;
16223 complaint (_("could not find suitable "
16224 "symtab for template parameter"
16225 " - DIE at %s [in module %s]"),
16226 sect_offset_str (die->sect_off),
16227 objfile_name (objfile));
16228 }
16229
16230 if (symtab != nullptr)
16231 {
16232 /* Make sure that the symtab is set on the new symbols.
16233 Even though they don't appear in this symtab directly,
16234 other parts of gdb assume that symbols do, and this is
16235 reasonably true. */
16236 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16237 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16238 }
3e1d3d8c
TT
16239 }
16240 }
134d01f1
DJ
16241}
16242
55426c9d
JB
16243/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16244 update TYPE using some information only available in DIE's children. */
16245
16246static void
16247update_enumeration_type_from_children (struct die_info *die,
16248 struct type *type,
16249 struct dwarf2_cu *cu)
16250{
60f7655a 16251 struct die_info *child_die;
55426c9d
JB
16252 int unsigned_enum = 1;
16253 int flag_enum = 1;
16254 ULONGEST mask = 0;
55426c9d 16255
8268c778 16256 auto_obstack obstack;
55426c9d 16257
60f7655a
DE
16258 for (child_die = die->child;
16259 child_die != NULL && child_die->tag;
16260 child_die = sibling_die (child_die))
55426c9d
JB
16261 {
16262 struct attribute *attr;
16263 LONGEST value;
16264 const gdb_byte *bytes;
16265 struct dwarf2_locexpr_baton *baton;
16266 const char *name;
60f7655a 16267
55426c9d
JB
16268 if (child_die->tag != DW_TAG_enumerator)
16269 continue;
16270
16271 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16272 if (attr == NULL)
16273 continue;
16274
16275 name = dwarf2_name (child_die, cu);
16276 if (name == NULL)
16277 name = "<anonymous enumerator>";
16278
16279 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16280 &value, &bytes, &baton);
16281 if (value < 0)
16282 {
16283 unsigned_enum = 0;
16284 flag_enum = 0;
16285 }
16286 else if ((mask & value) != 0)
16287 flag_enum = 0;
16288 else
16289 mask |= value;
16290
16291 /* If we already know that the enum type is neither unsigned, nor
16292 a flag type, no need to look at the rest of the enumerates. */
16293 if (!unsigned_enum && !flag_enum)
16294 break;
55426c9d
JB
16295 }
16296
16297 if (unsigned_enum)
16298 TYPE_UNSIGNED (type) = 1;
16299 if (flag_enum)
16300 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16301}
16302
134d01f1
DJ
16303/* Given a DW_AT_enumeration_type die, set its type. We do not
16304 complete the type's fields yet, or create any symbols. */
c906108c 16305
f792889a 16306static struct type *
134d01f1 16307read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16308{
518817b3 16309 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16310 struct type *type;
c906108c 16311 struct attribute *attr;
0114d602 16312 const char *name;
134d01f1 16313
348e048f
DE
16314 /* If the definition of this type lives in .debug_types, read that type.
16315 Don't follow DW_AT_specification though, that will take us back up
16316 the chain and we want to go down. */
45e58e77 16317 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16318 if (attr)
16319 {
ac9ec31b 16320 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16321
ac9ec31b 16322 /* The type's CU may not be the same as CU.
02142a6c 16323 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16324 return set_die_type (die, type, cu);
16325 }
16326
c906108c
SS
16327 type = alloc_type (objfile);
16328
16329 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16330 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16331 if (name != NULL)
e86ca25f 16332 TYPE_NAME (type) = name;
c906108c 16333
0626fc76
TT
16334 attr = dwarf2_attr (die, DW_AT_type, cu);
16335 if (attr != NULL)
16336 {
16337 struct type *underlying_type = die_type (die, cu);
16338
16339 TYPE_TARGET_TYPE (type) = underlying_type;
16340 }
16341
e142c38c 16342 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16343 if (attr)
16344 {
16345 TYPE_LENGTH (type) = DW_UNSND (attr);
16346 }
16347 else
16348 {
16349 TYPE_LENGTH (type) = 0;
16350 }
16351
2b4424c3
TT
16352 maybe_set_alignment (cu, die, type);
16353
137033e9
JB
16354 /* The enumeration DIE can be incomplete. In Ada, any type can be
16355 declared as private in the package spec, and then defined only
16356 inside the package body. Such types are known as Taft Amendment
16357 Types. When another package uses such a type, an incomplete DIE
16358 may be generated by the compiler. */
02eb380e 16359 if (die_is_declaration (die, cu))
876cecd0 16360 TYPE_STUB (type) = 1;
02eb380e 16361
0626fc76
TT
16362 /* Finish the creation of this type by using the enum's children.
16363 We must call this even when the underlying type has been provided
16364 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16365 update_enumeration_type_from_children (die, type, cu);
16366
0626fc76
TT
16367 /* If this type has an underlying type that is not a stub, then we
16368 may use its attributes. We always use the "unsigned" attribute
16369 in this situation, because ordinarily we guess whether the type
16370 is unsigned -- but the guess can be wrong and the underlying type
16371 can tell us the reality. However, we defer to a local size
16372 attribute if one exists, because this lets the compiler override
16373 the underlying type if needed. */
16374 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16375 {
16376 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16377 if (TYPE_LENGTH (type) == 0)
16378 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16379 if (TYPE_RAW_ALIGN (type) == 0
16380 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16381 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16382 }
16383
3d567982
TT
16384 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16385
f792889a 16386 return set_die_type (die, type, cu);
134d01f1
DJ
16387}
16388
16389/* Given a pointer to a die which begins an enumeration, process all
16390 the dies that define the members of the enumeration, and create the
16391 symbol for the enumeration type.
16392
16393 NOTE: We reverse the order of the element list. */
16394
16395static void
16396process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16397{
f792889a 16398 struct type *this_type;
134d01f1 16399
f792889a
DJ
16400 this_type = get_die_type (die, cu);
16401 if (this_type == NULL)
16402 this_type = read_enumeration_type (die, cu);
9dc481d3 16403
639d11d3 16404 if (die->child != NULL)
c906108c 16405 {
9dc481d3
DE
16406 struct die_info *child_die;
16407 struct symbol *sym;
16408 struct field *fields = NULL;
16409 int num_fields = 0;
15d034d0 16410 const char *name;
9dc481d3 16411
639d11d3 16412 child_die = die->child;
c906108c
SS
16413 while (child_die && child_die->tag)
16414 {
16415 if (child_die->tag != DW_TAG_enumerator)
16416 {
e7c27a73 16417 process_die (child_die, cu);
c906108c
SS
16418 }
16419 else
16420 {
39cbfefa
DJ
16421 name = dwarf2_name (child_die, cu);
16422 if (name)
c906108c 16423 {
f792889a 16424 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16425
16426 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16427 {
16428 fields = (struct field *)
16429 xrealloc (fields,
16430 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16431 * sizeof (struct field));
c906108c
SS
16432 }
16433
3567439c 16434 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16435 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16436 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16437 FIELD_BITSIZE (fields[num_fields]) = 0;
16438
16439 num_fields++;
16440 }
16441 }
16442
16443 child_die = sibling_die (child_die);
16444 }
16445
16446 if (num_fields)
16447 {
f792889a
DJ
16448 TYPE_NFIELDS (this_type) = num_fields;
16449 TYPE_FIELDS (this_type) = (struct field *)
16450 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16451 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16452 sizeof (struct field) * num_fields);
b8c9b27d 16453 xfree (fields);
c906108c 16454 }
c906108c 16455 }
134d01f1 16456
6c83ed52
TT
16457 /* If we are reading an enum from a .debug_types unit, and the enum
16458 is a declaration, and the enum is not the signatured type in the
16459 unit, then we do not want to add a symbol for it. Adding a
16460 symbol would in some cases obscure the true definition of the
16461 enum, giving users an incomplete type when the definition is
16462 actually available. Note that we do not want to do this for all
16463 enums which are just declarations, because C++0x allows forward
16464 enum declarations. */
3019eac3 16465 if (cu->per_cu->is_debug_types
6c83ed52
TT
16466 && die_is_declaration (die, cu))
16467 {
52dc124a 16468 struct signatured_type *sig_type;
6c83ed52 16469
c0f78cd4 16470 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16471 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16472 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16473 return;
16474 }
16475
f792889a 16476 new_symbol (die, this_type, cu);
c906108c
SS
16477}
16478
16479/* Extract all information from a DW_TAG_array_type DIE and put it in
16480 the DIE's type field. For now, this only handles one dimensional
16481 arrays. */
16482
f792889a 16483static struct type *
e7c27a73 16484read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16485{
518817b3 16486 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16487 struct die_info *child_die;
7e314c57 16488 struct type *type;
c906108c 16489 struct type *element_type, *range_type, *index_type;
c906108c 16490 struct attribute *attr;
15d034d0 16491 const char *name;
a405673c 16492 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16493 unsigned int bit_stride = 0;
c906108c 16494
e7c27a73 16495 element_type = die_type (die, cu);
c906108c 16496
7e314c57
JK
16497 /* The die_type call above may have already set the type for this DIE. */
16498 type = get_die_type (die, cu);
16499 if (type)
16500 return type;
16501
dc53a7ad
JB
16502 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16503 if (attr != NULL)
a405673c
JB
16504 {
16505 int stride_ok;
9a49df9d
AB
16506 struct type *prop_type
16507 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16508
16509 byte_stride_prop
16510 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16511 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16512 prop_type);
a405673c
JB
16513 if (!stride_ok)
16514 {
b98664d3 16515 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16516 " - DIE at %s [in module %s]"),
16517 sect_offset_str (die->sect_off),
518817b3 16518 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16519 /* Ignore this attribute. We will likely not be able to print
16520 arrays of this type correctly, but there is little we can do
16521 to help if we cannot read the attribute's value. */
16522 byte_stride_prop = NULL;
16523 }
16524 }
dc53a7ad
JB
16525
16526 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16527 if (attr != NULL)
16528 bit_stride = DW_UNSND (attr);
16529
c906108c
SS
16530 /* Irix 6.2 native cc creates array types without children for
16531 arrays with unspecified length. */
639d11d3 16532 if (die->child == NULL)
c906108c 16533 {
46bf5051 16534 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16535 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16536 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16537 byte_stride_prop, bit_stride);
f792889a 16538 return set_die_type (die, type, cu);
c906108c
SS
16539 }
16540
791afaa2 16541 std::vector<struct type *> range_types;
639d11d3 16542 child_die = die->child;
c906108c
SS
16543 while (child_die && child_die->tag)
16544 {
16545 if (child_die->tag == DW_TAG_subrange_type)
16546 {
f792889a 16547 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16548
f792889a 16549 if (child_type != NULL)
a02abb62 16550 {
0963b4bd
MS
16551 /* The range type was succesfully read. Save it for the
16552 array type creation. */
791afaa2 16553 range_types.push_back (child_type);
a02abb62 16554 }
c906108c
SS
16555 }
16556 child_die = sibling_die (child_die);
16557 }
16558
16559 /* Dwarf2 dimensions are output from left to right, create the
16560 necessary array types in backwards order. */
7ca2d3a3 16561
c906108c 16562 type = element_type;
7ca2d3a3
DL
16563
16564 if (read_array_order (die, cu) == DW_ORD_col_major)
16565 {
16566 int i = 0;
9a619af0 16567
791afaa2 16568 while (i < range_types.size ())
dc53a7ad 16569 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16570 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16571 }
16572 else
16573 {
791afaa2 16574 size_t ndim = range_types.size ();
7ca2d3a3 16575 while (ndim-- > 0)
dc53a7ad 16576 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16577 byte_stride_prop, bit_stride);
7ca2d3a3 16578 }
c906108c 16579
f5f8a009
EZ
16580 /* Understand Dwarf2 support for vector types (like they occur on
16581 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16582 array type. This is not part of the Dwarf2/3 standard yet, but a
16583 custom vendor extension. The main difference between a regular
16584 array and the vector variant is that vectors are passed by value
16585 to functions. */
e142c38c 16586 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16587 if (attr)
ea37ba09 16588 make_vector_type (type);
f5f8a009 16589
dbc98a8b
KW
16590 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16591 implementation may choose to implement triple vectors using this
16592 attribute. */
16593 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16594 if (attr)
16595 {
16596 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16597 TYPE_LENGTH (type) = DW_UNSND (attr);
16598 else
b98664d3 16599 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16600 "than the total size of elements"));
dbc98a8b
KW
16601 }
16602
39cbfefa
DJ
16603 name = dwarf2_name (die, cu);
16604 if (name)
16605 TYPE_NAME (type) = name;
6e70227d 16606
2b4424c3
TT
16607 maybe_set_alignment (cu, die, type);
16608
0963b4bd 16609 /* Install the type in the die. */
7e314c57
JK
16610 set_die_type (die, type, cu);
16611
16612 /* set_die_type should be already done. */
b4ba55a1
JB
16613 set_descriptive_type (type, die, cu);
16614
7e314c57 16615 return type;
c906108c
SS
16616}
16617
7ca2d3a3 16618static enum dwarf_array_dim_ordering
6e70227d 16619read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16620{
16621 struct attribute *attr;
16622
16623 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16624
aead7601
SM
16625 if (attr)
16626 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16627
0963b4bd
MS
16628 /* GNU F77 is a special case, as at 08/2004 array type info is the
16629 opposite order to the dwarf2 specification, but data is still
16630 laid out as per normal fortran.
7ca2d3a3 16631
0963b4bd
MS
16632 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16633 version checking. */
7ca2d3a3 16634
905e0470
PM
16635 if (cu->language == language_fortran
16636 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16637 {
16638 return DW_ORD_row_major;
16639 }
16640
6e70227d 16641 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16642 {
16643 case array_column_major:
16644 return DW_ORD_col_major;
16645 case array_row_major:
16646 default:
16647 return DW_ORD_row_major;
16648 };
16649}
16650
72019c9c 16651/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16652 the DIE's type field. */
72019c9c 16653
f792889a 16654static struct type *
72019c9c
GM
16655read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16656{
7e314c57
JK
16657 struct type *domain_type, *set_type;
16658 struct attribute *attr;
f792889a 16659
7e314c57
JK
16660 domain_type = die_type (die, cu);
16661
16662 /* The die_type call above may have already set the type for this DIE. */
16663 set_type = get_die_type (die, cu);
16664 if (set_type)
16665 return set_type;
16666
16667 set_type = create_set_type (NULL, domain_type);
16668
16669 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16670 if (attr)
16671 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16672
2b4424c3
TT
16673 maybe_set_alignment (cu, die, set_type);
16674
f792889a 16675 return set_die_type (die, set_type, cu);
72019c9c 16676}
7ca2d3a3 16677
0971de02
TT
16678/* A helper for read_common_block that creates a locexpr baton.
16679 SYM is the symbol which we are marking as computed.
16680 COMMON_DIE is the DIE for the common block.
16681 COMMON_LOC is the location expression attribute for the common
16682 block itself.
16683 MEMBER_LOC is the location expression attribute for the particular
16684 member of the common block that we are processing.
16685 CU is the CU from which the above come. */
16686
16687static void
16688mark_common_block_symbol_computed (struct symbol *sym,
16689 struct die_info *common_die,
16690 struct attribute *common_loc,
16691 struct attribute *member_loc,
16692 struct dwarf2_cu *cu)
16693{
518817b3
SM
16694 struct dwarf2_per_objfile *dwarf2_per_objfile
16695 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16696 struct objfile *objfile = dwarf2_per_objfile->objfile;
16697 struct dwarf2_locexpr_baton *baton;
16698 gdb_byte *ptr;
16699 unsigned int cu_off;
16700 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16701 LONGEST offset = 0;
16702
16703 gdb_assert (common_loc && member_loc);
16704 gdb_assert (attr_form_is_block (common_loc));
16705 gdb_assert (attr_form_is_block (member_loc)
16706 || attr_form_is_constant (member_loc));
16707
8d749320 16708 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16709 baton->per_cu = cu->per_cu;
16710 gdb_assert (baton->per_cu);
16711
16712 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16713
16714 if (attr_form_is_constant (member_loc))
16715 {
16716 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16717 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16718 }
16719 else
16720 baton->size += DW_BLOCK (member_loc)->size;
16721
224c3ddb 16722 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16723 baton->data = ptr;
16724
16725 *ptr++ = DW_OP_call4;
9c541725 16726 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16727 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16728 ptr += 4;
16729
16730 if (attr_form_is_constant (member_loc))
16731 {
16732 *ptr++ = DW_OP_addr;
16733 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16734 ptr += cu->header.addr_size;
16735 }
16736 else
16737 {
16738 /* We have to copy the data here, because DW_OP_call4 will only
16739 use a DW_AT_location attribute. */
16740 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16741 ptr += DW_BLOCK (member_loc)->size;
16742 }
16743
16744 *ptr++ = DW_OP_plus;
16745 gdb_assert (ptr - baton->data == baton->size);
16746
0971de02 16747 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16748 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16749}
16750
4357ac6c
TT
16751/* Create appropriate locally-scoped variables for all the
16752 DW_TAG_common_block entries. Also create a struct common_block
16753 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16754 is used to sepate the common blocks name namespace from regular
16755 variable names. */
c906108c
SS
16756
16757static void
e7c27a73 16758read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16759{
0971de02
TT
16760 struct attribute *attr;
16761
16762 attr = dwarf2_attr (die, DW_AT_location, cu);
16763 if (attr)
16764 {
16765 /* Support the .debug_loc offsets. */
16766 if (attr_form_is_block (attr))
16767 {
16768 /* Ok. */
16769 }
16770 else if (attr_form_is_section_offset (attr))
16771 {
16772 dwarf2_complex_location_expr_complaint ();
16773 attr = NULL;
16774 }
16775 else
16776 {
16777 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16778 "common block member");
16779 attr = NULL;
16780 }
16781 }
16782
639d11d3 16783 if (die->child != NULL)
c906108c 16784 {
518817b3 16785 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16786 struct die_info *child_die;
16787 size_t n_entries = 0, size;
16788 struct common_block *common_block;
16789 struct symbol *sym;
74ac6d43 16790
4357ac6c
TT
16791 for (child_die = die->child;
16792 child_die && child_die->tag;
16793 child_die = sibling_die (child_die))
16794 ++n_entries;
16795
16796 size = (sizeof (struct common_block)
16797 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16798 common_block
16799 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16800 size);
4357ac6c
TT
16801 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16802 common_block->n_entries = 0;
16803
16804 for (child_die = die->child;
16805 child_die && child_die->tag;
16806 child_die = sibling_die (child_die))
16807 {
16808 /* Create the symbol in the DW_TAG_common_block block in the current
16809 symbol scope. */
e7c27a73 16810 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16811 if (sym != NULL)
16812 {
16813 struct attribute *member_loc;
16814
16815 common_block->contents[common_block->n_entries++] = sym;
16816
16817 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16818 cu);
16819 if (member_loc)
16820 {
16821 /* GDB has handled this for a long time, but it is
16822 not specified by DWARF. It seems to have been
16823 emitted by gfortran at least as recently as:
16824 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16825 complaint (_("Variable in common block has "
0971de02 16826 "DW_AT_data_member_location "
9d8780f0
SM
16827 "- DIE at %s [in module %s]"),
16828 sect_offset_str (child_die->sect_off),
518817b3 16829 objfile_name (objfile));
0971de02
TT
16830
16831 if (attr_form_is_section_offset (member_loc))
16832 dwarf2_complex_location_expr_complaint ();
16833 else if (attr_form_is_constant (member_loc)
16834 || attr_form_is_block (member_loc))
16835 {
16836 if (attr)
16837 mark_common_block_symbol_computed (sym, die, attr,
16838 member_loc, cu);
16839 }
16840 else
16841 dwarf2_complex_location_expr_complaint ();
16842 }
16843 }
c906108c 16844 }
4357ac6c
TT
16845
16846 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16847 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16848 }
16849}
16850
0114d602 16851/* Create a type for a C++ namespace. */
d9fa45fe 16852
0114d602
DJ
16853static struct type *
16854read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16855{
518817b3 16856 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16857 const char *previous_prefix, *name;
9219021c 16858 int is_anonymous;
0114d602
DJ
16859 struct type *type;
16860
16861 /* For extensions, reuse the type of the original namespace. */
16862 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16863 {
16864 struct die_info *ext_die;
16865 struct dwarf2_cu *ext_cu = cu;
9a619af0 16866
0114d602
DJ
16867 ext_die = dwarf2_extension (die, &ext_cu);
16868 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16869
16870 /* EXT_CU may not be the same as CU.
02142a6c 16871 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16872 return set_die_type (die, type, cu);
16873 }
9219021c 16874
e142c38c 16875 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16876
16877 /* Now build the name of the current namespace. */
16878
0114d602
DJ
16879 previous_prefix = determine_prefix (die, cu);
16880 if (previous_prefix[0] != '\0')
16881 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16882 previous_prefix, name, 0, cu);
0114d602
DJ
16883
16884 /* Create the type. */
19f392bc 16885 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16886
60531b24 16887 return set_die_type (die, type, cu);
0114d602
DJ
16888}
16889
22cee43f 16890/* Read a namespace scope. */
0114d602
DJ
16891
16892static void
16893read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16894{
518817b3 16895 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16896 int is_anonymous;
9219021c 16897
5c4e30ca
DC
16898 /* Add a symbol associated to this if we haven't seen the namespace
16899 before. Also, add a using directive if it's an anonymous
16900 namespace. */
9219021c 16901
f2f0e013 16902 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16903 {
16904 struct type *type;
16905
0114d602 16906 type = read_type_die (die, cu);
e7c27a73 16907 new_symbol (die, type, cu);
5c4e30ca 16908
e8e80198 16909 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16910 if (is_anonymous)
0114d602
DJ
16911 {
16912 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16913
eb1e02fd 16914 std::vector<const char *> excludes;
804d2729 16915 add_using_directive (using_directives (cu),
22cee43f 16916 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16917 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16918 }
5c4e30ca 16919 }
9219021c 16920
639d11d3 16921 if (die->child != NULL)
d9fa45fe 16922 {
639d11d3 16923 struct die_info *child_die = die->child;
6e70227d 16924
d9fa45fe
DC
16925 while (child_die && child_die->tag)
16926 {
e7c27a73 16927 process_die (child_die, cu);
d9fa45fe
DC
16928 child_die = sibling_die (child_die);
16929 }
16930 }
38d518c9
EZ
16931}
16932
f55ee35c
JK
16933/* Read a Fortran module as type. This DIE can be only a declaration used for
16934 imported module. Still we need that type as local Fortran "use ... only"
16935 declaration imports depend on the created type in determine_prefix. */
16936
16937static struct type *
16938read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16939{
518817b3 16940 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16941 const char *module_name;
f55ee35c
JK
16942 struct type *type;
16943
16944 module_name = dwarf2_name (die, cu);
19f392bc 16945 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16946
f55ee35c
JK
16947 return set_die_type (die, type, cu);
16948}
16949
5d7cb8df
JK
16950/* Read a Fortran module. */
16951
16952static void
16953read_module (struct die_info *die, struct dwarf2_cu *cu)
16954{
16955 struct die_info *child_die = die->child;
530e8392
KB
16956 struct type *type;
16957
16958 type = read_type_die (die, cu);
16959 new_symbol (die, type, cu);
5d7cb8df 16960
5d7cb8df
JK
16961 while (child_die && child_die->tag)
16962 {
16963 process_die (child_die, cu);
16964 child_die = sibling_die (child_die);
16965 }
16966}
16967
38d518c9
EZ
16968/* Return the name of the namespace represented by DIE. Set
16969 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16970 namespace. */
16971
16972static const char *
e142c38c 16973namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16974{
16975 struct die_info *current_die;
16976 const char *name = NULL;
16977
16978 /* Loop through the extensions until we find a name. */
16979
16980 for (current_die = die;
16981 current_die != NULL;
f2f0e013 16982 current_die = dwarf2_extension (die, &cu))
38d518c9 16983 {
96553a0c
DE
16984 /* We don't use dwarf2_name here so that we can detect the absence
16985 of a name -> anonymous namespace. */
7d45c7c3 16986 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16987
38d518c9
EZ
16988 if (name != NULL)
16989 break;
16990 }
16991
16992 /* Is it an anonymous namespace? */
16993
16994 *is_anonymous = (name == NULL);
16995 if (*is_anonymous)
2b1dbab0 16996 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16997
16998 return name;
d9fa45fe
DC
16999}
17000
c906108c
SS
17001/* Extract all information from a DW_TAG_pointer_type DIE and add to
17002 the user defined type vector. */
17003
f792889a 17004static struct type *
e7c27a73 17005read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17006{
518817b3
SM
17007 struct gdbarch *gdbarch
17008 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17009 struct comp_unit_head *cu_header = &cu->header;
c906108c 17010 struct type *type;
8b2dbe47
KB
17011 struct attribute *attr_byte_size;
17012 struct attribute *attr_address_class;
17013 int byte_size, addr_class;
7e314c57
JK
17014 struct type *target_type;
17015
17016 target_type = die_type (die, cu);
c906108c 17017
7e314c57
JK
17018 /* The die_type call above may have already set the type for this DIE. */
17019 type = get_die_type (die, cu);
17020 if (type)
17021 return type;
17022
17023 type = lookup_pointer_type (target_type);
8b2dbe47 17024
e142c38c 17025 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17026 if (attr_byte_size)
17027 byte_size = DW_UNSND (attr_byte_size);
c906108c 17028 else
8b2dbe47
KB
17029 byte_size = cu_header->addr_size;
17030
e142c38c 17031 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17032 if (attr_address_class)
17033 addr_class = DW_UNSND (attr_address_class);
17034 else
17035 addr_class = DW_ADDR_none;
17036
2b4424c3
TT
17037 ULONGEST alignment = get_alignment (cu, die);
17038
17039 /* If the pointer size, alignment, or address class is different
17040 than the default, create a type variant marked as such and set
17041 the length accordingly. */
17042 if (TYPE_LENGTH (type) != byte_size
17043 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17044 && alignment != TYPE_RAW_ALIGN (type))
17045 || addr_class != DW_ADDR_none)
c906108c 17046 {
5e2b427d 17047 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17048 {
17049 int type_flags;
17050
849957d9 17051 type_flags = gdbarch_address_class_type_flags
5e2b427d 17052 (gdbarch, byte_size, addr_class);
876cecd0
TT
17053 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17054 == 0);
8b2dbe47
KB
17055 type = make_type_with_address_space (type, type_flags);
17056 }
17057 else if (TYPE_LENGTH (type) != byte_size)
17058 {
b98664d3 17059 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17060 }
2b4424c3
TT
17061 else if (TYPE_RAW_ALIGN (type) != alignment)
17062 {
b98664d3 17063 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17064 " - DIE at %s [in module %s]"),
17065 sect_offset_str (die->sect_off),
17066 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17067 }
6e70227d 17068 else
9a619af0
MS
17069 {
17070 /* Should we also complain about unhandled address classes? */
17071 }
c906108c 17072 }
8b2dbe47
KB
17073
17074 TYPE_LENGTH (type) = byte_size;
2b4424c3 17075 set_type_align (type, alignment);
f792889a 17076 return set_die_type (die, type, cu);
c906108c
SS
17077}
17078
17079/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17080 the user defined type vector. */
17081
f792889a 17082static struct type *
e7c27a73 17083read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17084{
17085 struct type *type;
17086 struct type *to_type;
17087 struct type *domain;
17088
e7c27a73
DJ
17089 to_type = die_type (die, cu);
17090 domain = die_containing_type (die, cu);
0d5de010 17091
7e314c57
JK
17092 /* The calls above may have already set the type for this DIE. */
17093 type = get_die_type (die, cu);
17094 if (type)
17095 return type;
17096
0d5de010
DJ
17097 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17098 type = lookup_methodptr_type (to_type);
7078baeb
TT
17099 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17100 {
518817b3
SM
17101 struct type *new_type
17102 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17103
17104 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17105 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17106 TYPE_VARARGS (to_type));
17107 type = lookup_methodptr_type (new_type);
17108 }
0d5de010
DJ
17109 else
17110 type = lookup_memberptr_type (to_type, domain);
c906108c 17111
f792889a 17112 return set_die_type (die, type, cu);
c906108c
SS
17113}
17114
4297a3f0 17115/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17116 the user defined type vector. */
17117
f792889a 17118static struct type *
4297a3f0
AV
17119read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17120 enum type_code refcode)
c906108c 17121{
e7c27a73 17122 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17123 struct type *type, *target_type;
c906108c
SS
17124 struct attribute *attr;
17125
4297a3f0
AV
17126 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17127
7e314c57
JK
17128 target_type = die_type (die, cu);
17129
17130 /* The die_type call above may have already set the type for this DIE. */
17131 type = get_die_type (die, cu);
17132 if (type)
17133 return type;
17134
4297a3f0 17135 type = lookup_reference_type (target_type, refcode);
e142c38c 17136 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17137 if (attr)
17138 {
17139 TYPE_LENGTH (type) = DW_UNSND (attr);
17140 }
17141 else
17142 {
107d2387 17143 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17144 }
2b4424c3 17145 maybe_set_alignment (cu, die, type);
f792889a 17146 return set_die_type (die, type, cu);
c906108c
SS
17147}
17148
cf363f18
MW
17149/* Add the given cv-qualifiers to the element type of the array. GCC
17150 outputs DWARF type qualifiers that apply to an array, not the
17151 element type. But GDB relies on the array element type to carry
17152 the cv-qualifiers. This mimics section 6.7.3 of the C99
17153 specification. */
17154
17155static struct type *
17156add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17157 struct type *base_type, int cnst, int voltl)
17158{
17159 struct type *el_type, *inner_array;
17160
17161 base_type = copy_type (base_type);
17162 inner_array = base_type;
17163
17164 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17165 {
17166 TYPE_TARGET_TYPE (inner_array) =
17167 copy_type (TYPE_TARGET_TYPE (inner_array));
17168 inner_array = TYPE_TARGET_TYPE (inner_array);
17169 }
17170
17171 el_type = TYPE_TARGET_TYPE (inner_array);
17172 cnst |= TYPE_CONST (el_type);
17173 voltl |= TYPE_VOLATILE (el_type);
17174 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17175
17176 return set_die_type (die, base_type, cu);
17177}
17178
f792889a 17179static struct type *
e7c27a73 17180read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17181{
f792889a 17182 struct type *base_type, *cv_type;
c906108c 17183
e7c27a73 17184 base_type = die_type (die, cu);
7e314c57
JK
17185
17186 /* The die_type call above may have already set the type for this DIE. */
17187 cv_type = get_die_type (die, cu);
17188 if (cv_type)
17189 return cv_type;
17190
2f608a3a
KW
17191 /* In case the const qualifier is applied to an array type, the element type
17192 is so qualified, not the array type (section 6.7.3 of C99). */
17193 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17194 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17195
f792889a
DJ
17196 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17197 return set_die_type (die, cv_type, cu);
c906108c
SS
17198}
17199
f792889a 17200static struct type *
e7c27a73 17201read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17202{
f792889a 17203 struct type *base_type, *cv_type;
c906108c 17204
e7c27a73 17205 base_type = die_type (die, cu);
7e314c57
JK
17206
17207 /* The die_type call above may have already set the type for this DIE. */
17208 cv_type = get_die_type (die, cu);
17209 if (cv_type)
17210 return cv_type;
17211
cf363f18
MW
17212 /* In case the volatile qualifier is applied to an array type, the
17213 element type is so qualified, not the array type (section 6.7.3
17214 of C99). */
17215 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17216 return add_array_cv_type (die, cu, base_type, 0, 1);
17217
f792889a
DJ
17218 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17219 return set_die_type (die, cv_type, cu);
c906108c
SS
17220}
17221
06d66ee9
TT
17222/* Handle DW_TAG_restrict_type. */
17223
17224static struct type *
17225read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17226{
17227 struct type *base_type, *cv_type;
17228
17229 base_type = die_type (die, cu);
17230
17231 /* The die_type call above may have already set the type for this DIE. */
17232 cv_type = get_die_type (die, cu);
17233 if (cv_type)
17234 return cv_type;
17235
17236 cv_type = make_restrict_type (base_type);
17237 return set_die_type (die, cv_type, cu);
17238}
17239
a2c2acaf
MW
17240/* Handle DW_TAG_atomic_type. */
17241
17242static struct type *
17243read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17244{
17245 struct type *base_type, *cv_type;
17246
17247 base_type = die_type (die, cu);
17248
17249 /* The die_type call above may have already set the type for this DIE. */
17250 cv_type = get_die_type (die, cu);
17251 if (cv_type)
17252 return cv_type;
17253
17254 cv_type = make_atomic_type (base_type);
17255 return set_die_type (die, cv_type, cu);
17256}
17257
c906108c
SS
17258/* Extract all information from a DW_TAG_string_type DIE and add to
17259 the user defined type vector. It isn't really a user defined type,
17260 but it behaves like one, with other DIE's using an AT_user_def_type
17261 attribute to reference it. */
17262
f792889a 17263static struct type *
e7c27a73 17264read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17265{
518817b3 17266 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17267 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17268 struct type *type, *range_type, *index_type, *char_type;
17269 struct attribute *attr;
17270 unsigned int length;
17271
e142c38c 17272 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17273 if (attr)
17274 {
17275 length = DW_UNSND (attr);
17276 }
17277 else
17278 {
0963b4bd 17279 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17280 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17281 if (attr)
17282 {
17283 length = DW_UNSND (attr);
17284 }
17285 else
17286 {
17287 length = 1;
17288 }
c906108c 17289 }
6ccb9162 17290
46bf5051 17291 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17292 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17293 char_type = language_string_char_type (cu->language_defn, gdbarch);
17294 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17295
f792889a 17296 return set_die_type (die, type, cu);
c906108c
SS
17297}
17298
4d804846
JB
17299/* Assuming that DIE corresponds to a function, returns nonzero
17300 if the function is prototyped. */
17301
17302static int
17303prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17304{
17305 struct attribute *attr;
17306
17307 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17308 if (attr && (DW_UNSND (attr) != 0))
17309 return 1;
17310
17311 /* The DWARF standard implies that the DW_AT_prototyped attribute
17312 is only meaninful for C, but the concept also extends to other
17313 languages that allow unprototyped functions (Eg: Objective C).
17314 For all other languages, assume that functions are always
17315 prototyped. */
17316 if (cu->language != language_c
17317 && cu->language != language_objc
17318 && cu->language != language_opencl)
17319 return 1;
17320
17321 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17322 prototyped and unprototyped functions; default to prototyped,
17323 since that is more common in modern code (and RealView warns
17324 about unprototyped functions). */
17325 if (producer_is_realview (cu->producer))
17326 return 1;
17327
17328 return 0;
17329}
17330
c906108c
SS
17331/* Handle DIES due to C code like:
17332
17333 struct foo
c5aa993b
JM
17334 {
17335 int (*funcp)(int a, long l);
17336 int b;
17337 };
c906108c 17338
0963b4bd 17339 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17340
f792889a 17341static struct type *
e7c27a73 17342read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17343{
518817b3 17344 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17345 struct type *type; /* Type that this function returns. */
17346 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17347 struct attribute *attr;
17348
e7c27a73 17349 type = die_type (die, cu);
7e314c57
JK
17350
17351 /* The die_type call above may have already set the type for this DIE. */
17352 ftype = get_die_type (die, cu);
17353 if (ftype)
17354 return ftype;
17355
0c8b41f1 17356 ftype = lookup_function_type (type);
c906108c 17357
4d804846 17358 if (prototyped_function_p (die, cu))
a6c727b2 17359 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17360
c055b101
CV
17361 /* Store the calling convention in the type if it's available in
17362 the subroutine die. Otherwise set the calling convention to
17363 the default value DW_CC_normal. */
17364 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17365 if (attr)
17366 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17367 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17368 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17369 else
17370 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17371
743649fd
MW
17372 /* Record whether the function returns normally to its caller or not
17373 if the DWARF producer set that information. */
17374 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17375 if (attr && (DW_UNSND (attr) != 0))
17376 TYPE_NO_RETURN (ftype) = 1;
17377
76c10ea2
GM
17378 /* We need to add the subroutine type to the die immediately so
17379 we don't infinitely recurse when dealing with parameters
0963b4bd 17380 declared as the same subroutine type. */
76c10ea2 17381 set_die_type (die, ftype, cu);
6e70227d 17382
639d11d3 17383 if (die->child != NULL)
c906108c 17384 {
bb5ed363 17385 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17386 struct die_info *child_die;
8072405b 17387 int nparams, iparams;
c906108c
SS
17388
17389 /* Count the number of parameters.
17390 FIXME: GDB currently ignores vararg functions, but knows about
17391 vararg member functions. */
8072405b 17392 nparams = 0;
639d11d3 17393 child_die = die->child;
c906108c
SS
17394 while (child_die && child_die->tag)
17395 {
17396 if (child_die->tag == DW_TAG_formal_parameter)
17397 nparams++;
17398 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17399 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17400 child_die = sibling_die (child_die);
17401 }
17402
17403 /* Allocate storage for parameters and fill them in. */
17404 TYPE_NFIELDS (ftype) = nparams;
17405 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17406 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17407
8072405b
JK
17408 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17409 even if we error out during the parameters reading below. */
17410 for (iparams = 0; iparams < nparams; iparams++)
17411 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17412
17413 iparams = 0;
639d11d3 17414 child_die = die->child;
c906108c
SS
17415 while (child_die && child_die->tag)
17416 {
17417 if (child_die->tag == DW_TAG_formal_parameter)
17418 {
3ce3b1ba
PA
17419 struct type *arg_type;
17420
17421 /* DWARF version 2 has no clean way to discern C++
17422 static and non-static member functions. G++ helps
17423 GDB by marking the first parameter for non-static
17424 member functions (which is the this pointer) as
17425 artificial. We pass this information to
17426 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17427
17428 DWARF version 3 added DW_AT_object_pointer, which GCC
17429 4.5 does not yet generate. */
e142c38c 17430 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17431 if (attr)
17432 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17433 else
9c37b5ae 17434 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17435 arg_type = die_type (child_die, cu);
17436
17437 /* RealView does not mark THIS as const, which the testsuite
17438 expects. GCC marks THIS as const in method definitions,
17439 but not in the class specifications (GCC PR 43053). */
17440 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17441 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17442 {
17443 int is_this = 0;
17444 struct dwarf2_cu *arg_cu = cu;
17445 const char *name = dwarf2_name (child_die, cu);
17446
17447 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17448 if (attr)
17449 {
17450 /* If the compiler emits this, use it. */
17451 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17452 is_this = 1;
17453 }
17454 else if (name && strcmp (name, "this") == 0)
17455 /* Function definitions will have the argument names. */
17456 is_this = 1;
17457 else if (name == NULL && iparams == 0)
17458 /* Declarations may not have the names, so like
17459 elsewhere in GDB, assume an artificial first
17460 argument is "this". */
17461 is_this = 1;
17462
17463 if (is_this)
17464 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17465 arg_type, 0);
17466 }
17467
17468 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17469 iparams++;
17470 }
17471 child_die = sibling_die (child_die);
17472 }
17473 }
17474
76c10ea2 17475 return ftype;
c906108c
SS
17476}
17477
f792889a 17478static struct type *
e7c27a73 17479read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17480{
518817b3 17481 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17482 const char *name = NULL;
3c8e0968 17483 struct type *this_type, *target_type;
c906108c 17484
94af9270 17485 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17486 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17487 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17488 set_die_type (die, this_type, cu);
3c8e0968
DE
17489 target_type = die_type (die, cu);
17490 if (target_type != this_type)
17491 TYPE_TARGET_TYPE (this_type) = target_type;
17492 else
17493 {
17494 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17495 spec and cause infinite loops in GDB. */
b98664d3 17496 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17497 "- DIE at %s [in module %s]"),
17498 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17499 TYPE_TARGET_TYPE (this_type) = NULL;
17500 }
f792889a 17501 return this_type;
c906108c
SS
17502}
17503
9b790ce7
UW
17504/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17505 (which may be different from NAME) to the architecture back-end to allow
17506 it to guess the correct format if necessary. */
17507
17508static struct type *
17509dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17510 const char *name_hint)
17511{
17512 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17513 const struct floatformat **format;
17514 struct type *type;
17515
17516 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17517 if (format)
17518 type = init_float_type (objfile, bits, name, format);
17519 else
77b7c781 17520 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17521
17522 return type;
17523}
17524
eb77c9df
AB
17525/* Allocate an integer type of size BITS and name NAME. */
17526
17527static struct type *
17528dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17529 int bits, int unsigned_p, const char *name)
17530{
17531 struct type *type;
17532
17533 /* Versions of Intel's C Compiler generate an integer type called "void"
17534 instead of using DW_TAG_unspecified_type. This has been seen on
17535 at least versions 14, 17, and 18. */
35ee2dc2
AB
17536 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17537 && strcmp (name, "void") == 0)
eb77c9df
AB
17538 type = objfile_type (objfile)->builtin_void;
17539 else
17540 type = init_integer_type (objfile, bits, unsigned_p, name);
17541
17542 return type;
17543}
17544
8bdc1658
AB
17545/* Initialise and return a floating point type of size BITS suitable for
17546 use as a component of a complex number. The NAME_HINT is passed through
17547 when initialising the floating point type and is the name of the complex
17548 type.
17549
17550 As DWARF doesn't currently provide an explicit name for the components
17551 of a complex number, but it can be helpful to have these components
17552 named, we try to select a suitable name based on the size of the
17553 component. */
17554static struct type *
17555dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17556 struct objfile *objfile,
17557 int bits, const char *name_hint)
17558{
17559 gdbarch *gdbarch = get_objfile_arch (objfile);
17560 struct type *tt = nullptr;
17561
35add35e
AB
17562 /* Try to find a suitable floating point builtin type of size BITS.
17563 We're going to use the name of this type as the name for the complex
17564 target type that we are about to create. */
1db455a7 17565 switch (cu->language)
8bdc1658 17566 {
1db455a7
AB
17567 case language_fortran:
17568 switch (bits)
17569 {
17570 case 32:
17571 tt = builtin_f_type (gdbarch)->builtin_real;
17572 break;
17573 case 64:
17574 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17575 break;
17576 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17577 case 128:
17578 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17579 break;
17580 }
8bdc1658 17581 break;
1db455a7
AB
17582 default:
17583 switch (bits)
17584 {
17585 case 32:
17586 tt = builtin_type (gdbarch)->builtin_float;
17587 break;
17588 case 64:
17589 tt = builtin_type (gdbarch)->builtin_double;
17590 break;
17591 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17592 case 128:
17593 tt = builtin_type (gdbarch)->builtin_long_double;
17594 break;
17595 }
8bdc1658
AB
17596 break;
17597 }
17598
35add35e
AB
17599 /* If the type we found doesn't match the size we were looking for, then
17600 pretend we didn't find a type at all, the complex target type we
17601 create will then be nameless. */
a12e5744 17602 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17603 tt = nullptr;
17604
8bdc1658
AB
17605 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17606 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17607}
17608
c906108c
SS
17609/* Find a representation of a given base type and install
17610 it in the TYPE field of the die. */
17611
f792889a 17612static struct type *
e7c27a73 17613read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17614{
518817b3 17615 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17616 struct type *type;
17617 struct attribute *attr;
19f392bc 17618 int encoding = 0, bits = 0;
15d034d0 17619 const char *name;
c906108c 17620
e142c38c 17621 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17622 if (attr)
17623 {
17624 encoding = DW_UNSND (attr);
17625 }
e142c38c 17626 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17627 if (attr)
17628 {
19f392bc 17629 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17630 }
39cbfefa 17631 name = dwarf2_name (die, cu);
6ccb9162 17632 if (!name)
c906108c 17633 {
b98664d3 17634 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17635 }
6ccb9162
UW
17636
17637 switch (encoding)
c906108c 17638 {
6ccb9162
UW
17639 case DW_ATE_address:
17640 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17641 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17642 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17643 break;
17644 case DW_ATE_boolean:
19f392bc 17645 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17646 break;
17647 case DW_ATE_complex_float:
8bdc1658 17648 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
19f392bc 17649 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17650 break;
17651 case DW_ATE_decimal_float:
19f392bc 17652 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17653 break;
17654 case DW_ATE_float:
9b790ce7 17655 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17656 break;
17657 case DW_ATE_signed:
eb77c9df 17658 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17659 break;
17660 case DW_ATE_unsigned:
3b2b8fea
TT
17661 if (cu->language == language_fortran
17662 && name
61012eef 17663 && startswith (name, "character("))
19f392bc
UW
17664 type = init_character_type (objfile, bits, 1, name);
17665 else
eb77c9df 17666 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17667 break;
17668 case DW_ATE_signed_char:
6e70227d 17669 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17670 || cu->language == language_pascal
17671 || cu->language == language_fortran)
19f392bc
UW
17672 type = init_character_type (objfile, bits, 0, name);
17673 else
eb77c9df 17674 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17675 break;
17676 case DW_ATE_unsigned_char:
868a0084 17677 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17678 || cu->language == language_pascal
c44af4eb
TT
17679 || cu->language == language_fortran
17680 || cu->language == language_rust)
19f392bc
UW
17681 type = init_character_type (objfile, bits, 1, name);
17682 else
eb77c9df 17683 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17684 break;
75079b2b 17685 case DW_ATE_UTF:
53e710ac
PA
17686 {
17687 gdbarch *arch = get_objfile_arch (objfile);
17688
17689 if (bits == 16)
17690 type = builtin_type (arch)->builtin_char16;
17691 else if (bits == 32)
17692 type = builtin_type (arch)->builtin_char32;
17693 else
17694 {
b98664d3 17695 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17696 bits);
eb77c9df 17697 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17698 }
17699 return set_die_type (die, type, cu);
17700 }
75079b2b
TT
17701 break;
17702
6ccb9162 17703 default:
b98664d3 17704 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17705 dwarf_type_encoding_name (encoding));
77b7c781 17706 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17707 break;
c906108c 17708 }
6ccb9162 17709
0114d602 17710 if (name && strcmp (name, "char") == 0)
876cecd0 17711 TYPE_NOSIGN (type) = 1;
0114d602 17712
2b4424c3
TT
17713 maybe_set_alignment (cu, die, type);
17714
f792889a 17715 return set_die_type (die, type, cu);
c906108c
SS
17716}
17717
80180f79
SA
17718/* Parse dwarf attribute if it's a block, reference or constant and put the
17719 resulting value of the attribute into struct bound_prop.
17720 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17721
17722static int
17723attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17724 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17725 struct type *default_type)
80180f79
SA
17726{
17727 struct dwarf2_property_baton *baton;
518817b3
SM
17728 struct obstack *obstack
17729 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17730
9a49df9d
AB
17731 gdb_assert (default_type != NULL);
17732
80180f79
SA
17733 if (attr == NULL || prop == NULL)
17734 return 0;
17735
17736 if (attr_form_is_block (attr))
17737 {
8d749320 17738 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17739 baton->property_type = default_type;
80180f79
SA
17740 baton->locexpr.per_cu = cu->per_cu;
17741 baton->locexpr.size = DW_BLOCK (attr)->size;
17742 baton->locexpr.data = DW_BLOCK (attr)->data;
9a49df9d 17743 baton->locexpr.is_reference = false;
80180f79
SA
17744 prop->data.baton = baton;
17745 prop->kind = PROP_LOCEXPR;
17746 gdb_assert (prop->data.baton != NULL);
17747 }
17748 else if (attr_form_is_ref (attr))
17749 {
17750 struct dwarf2_cu *target_cu = cu;
17751 struct die_info *target_die;
17752 struct attribute *target_attr;
17753
17754 target_die = follow_die_ref (die, attr, &target_cu);
17755 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17756 if (target_attr == NULL)
17757 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17758 target_cu);
80180f79
SA
17759 if (target_attr == NULL)
17760 return 0;
17761
df25ebbd 17762 switch (target_attr->name)
80180f79 17763 {
df25ebbd
JB
17764 case DW_AT_location:
17765 if (attr_form_is_section_offset (target_attr))
17766 {
8d749320 17767 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17768 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17769 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17770 prop->data.baton = baton;
17771 prop->kind = PROP_LOCLIST;
17772 gdb_assert (prop->data.baton != NULL);
17773 }
17774 else if (attr_form_is_block (target_attr))
17775 {
8d749320 17776 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17777 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17778 baton->locexpr.per_cu = cu->per_cu;
17779 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17780 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17781 baton->locexpr.is_reference = true;
df25ebbd
JB
17782 prop->data.baton = baton;
17783 prop->kind = PROP_LOCEXPR;
17784 gdb_assert (prop->data.baton != NULL);
17785 }
17786 else
17787 {
17788 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17789 "dynamic property");
17790 return 0;
17791 }
17792 break;
17793 case DW_AT_data_member_location:
17794 {
17795 LONGEST offset;
17796
17797 if (!handle_data_member_location (target_die, target_cu,
17798 &offset))
17799 return 0;
17800
8d749320 17801 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17802 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17803 target_cu);
df25ebbd
JB
17804 baton->offset_info.offset = offset;
17805 baton->offset_info.type = die_type (target_die, target_cu);
17806 prop->data.baton = baton;
17807 prop->kind = PROP_ADDR_OFFSET;
17808 break;
17809 }
80180f79
SA
17810 }
17811 }
17812 else if (attr_form_is_constant (attr))
17813 {
17814 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17815 prop->kind = PROP_CONST;
17816 }
17817 else
17818 {
17819 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17820 dwarf2_name (die, cu));
17821 return 0;
17822 }
17823
17824 return 1;
17825}
17826
9a49df9d
AB
17827/* Find an integer type the same size as the address size given in the
17828 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17829 is unsigned or not. */
17830
17831static struct type *
17832dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17833 bool unsigned_p)
17834{
17835 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17836 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17837 struct type *int_type;
17838
17839 /* Helper macro to examine the various builtin types. */
17840#define TRY_TYPE(F) \
17841 int_type = (unsigned_p \
17842 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17843 : objfile_type (objfile)->builtin_ ## F); \
17844 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17845 return int_type
17846
17847 TRY_TYPE (char);
17848 TRY_TYPE (short);
17849 TRY_TYPE (int);
17850 TRY_TYPE (long);
17851 TRY_TYPE (long_long);
17852
17853#undef TRY_TYPE
17854
17855 gdb_assert_not_reached ("unable to find suitable integer type");
17856}
17857
b86352cf
AB
17858/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17859 present (which is valid) then compute the default type based on the
17860 compilation units address size. */
17861
17862static struct type *
17863read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17864{
17865 struct type *index_type = die_type (die, cu);
17866
17867 /* Dwarf-2 specifications explicitly allows to create subrange types
17868 without specifying a base type.
17869 In that case, the base type must be set to the type of
17870 the lower bound, upper bound or count, in that order, if any of these
17871 three attributes references an object that has a type.
17872 If no base type is found, the Dwarf-2 specifications say that
17873 a signed integer type of size equal to the size of an address should
17874 be used.
17875 For the following C code: `extern char gdb_int [];'
17876 GCC produces an empty range DIE.
17877 FIXME: muller/2010-05-28: Possible references to object for low bound,
17878 high bound or count are not yet handled by this code. */
17879 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 17880 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
17881
17882 return index_type;
17883}
17884
a02abb62
JB
17885/* Read the given DW_AT_subrange DIE. */
17886
f792889a 17887static struct type *
a02abb62
JB
17888read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17889{
4c9ad8c2 17890 struct type *base_type, *orig_base_type;
a02abb62
JB
17891 struct type *range_type;
17892 struct attribute *attr;
729efb13 17893 struct dynamic_prop low, high;
4fae6e18 17894 int low_default_is_valid;
c451ebe5 17895 int high_bound_is_count = 0;
15d034d0 17896 const char *name;
d359392f 17897 ULONGEST negative_mask;
e77813c8 17898
b86352cf
AB
17899 orig_base_type = read_subrange_index_type (die, cu);
17900
4c9ad8c2
TT
17901 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17902 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17903 creating the range type, but we use the result of check_typedef
17904 when examining properties of the type. */
17905 base_type = check_typedef (orig_base_type);
a02abb62 17906
7e314c57
JK
17907 /* The die_type call above may have already set the type for this DIE. */
17908 range_type = get_die_type (die, cu);
17909 if (range_type)
17910 return range_type;
17911
729efb13
SA
17912 low.kind = PROP_CONST;
17913 high.kind = PROP_CONST;
17914 high.data.const_val = 0;
17915
4fae6e18
JK
17916 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17917 omitting DW_AT_lower_bound. */
17918 switch (cu->language)
6e70227d 17919 {
4fae6e18
JK
17920 case language_c:
17921 case language_cplus:
729efb13 17922 low.data.const_val = 0;
4fae6e18
JK
17923 low_default_is_valid = 1;
17924 break;
17925 case language_fortran:
729efb13 17926 low.data.const_val = 1;
4fae6e18
JK
17927 low_default_is_valid = 1;
17928 break;
17929 case language_d:
4fae6e18 17930 case language_objc:
c44af4eb 17931 case language_rust:
729efb13 17932 low.data.const_val = 0;
4fae6e18
JK
17933 low_default_is_valid = (cu->header.version >= 4);
17934 break;
17935 case language_ada:
17936 case language_m2:
17937 case language_pascal:
729efb13 17938 low.data.const_val = 1;
4fae6e18
JK
17939 low_default_is_valid = (cu->header.version >= 4);
17940 break;
17941 default:
729efb13 17942 low.data.const_val = 0;
4fae6e18
JK
17943 low_default_is_valid = 0;
17944 break;
a02abb62
JB
17945 }
17946
e142c38c 17947 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17948 if (attr)
9a49df9d 17949 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17950 else if (!low_default_is_valid)
b98664d3 17951 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17952 "- DIE at %s [in module %s]"),
17953 sect_offset_str (die->sect_off),
518817b3 17954 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17955
506f5c41
TV
17956 struct attribute *attr_ub, *attr_count;
17957 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17958 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17959 {
506f5c41 17960 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17961 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17962 {
c451ebe5
SA
17963 /* If bounds are constant do the final calculation here. */
17964 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17965 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17966 else
17967 high_bound_is_count = 1;
c2ff108b 17968 }
506f5c41
TV
17969 else
17970 {
17971 if (attr_ub != NULL)
17972 complaint (_("Unresolved DW_AT_upper_bound "
17973 "- DIE at %s [in module %s]"),
17974 sect_offset_str (die->sect_off),
17975 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17976 if (attr_count != NULL)
17977 complaint (_("Unresolved DW_AT_count "
17978 "- DIE at %s [in module %s]"),
17979 sect_offset_str (die->sect_off),
17980 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17981 }
e77813c8 17982 }
a02abb62 17983
4e962e74
TT
17984 LONGEST bias = 0;
17985 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17986 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
17987 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17988
dbb9c2b1
JB
17989 /* Normally, the DWARF producers are expected to use a signed
17990 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17991 But this is unfortunately not always the case, as witnessed
17992 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17993 is used instead. To work around that ambiguity, we treat
17994 the bounds as signed, and thus sign-extend their values, when
17995 the base type is signed. */
6e70227d 17996 negative_mask =
d359392f 17997 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17998 if (low.kind == PROP_CONST
17999 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18000 low.data.const_val |= negative_mask;
18001 if (high.kind == PROP_CONST
18002 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18003 high.data.const_val |= negative_mask;
43bbcdc2 18004
4e962e74 18005 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18006
c451ebe5
SA
18007 if (high_bound_is_count)
18008 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18009
c2ff108b
JK
18010 /* Ada expects an empty array on no boundary attributes. */
18011 if (attr == NULL && cu->language != language_ada)
729efb13 18012 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18013
39cbfefa
DJ
18014 name = dwarf2_name (die, cu);
18015 if (name)
18016 TYPE_NAME (range_type) = name;
6e70227d 18017
e142c38c 18018 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
18019 if (attr)
18020 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18021
2b4424c3
TT
18022 maybe_set_alignment (cu, die, range_type);
18023
7e314c57
JK
18024 set_die_type (die, range_type, cu);
18025
18026 /* set_die_type should be already done. */
b4ba55a1
JB
18027 set_descriptive_type (range_type, die, cu);
18028
7e314c57 18029 return range_type;
a02abb62 18030}
6e70227d 18031
f792889a 18032static struct type *
81a17f79
JB
18033read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18034{
18035 struct type *type;
81a17f79 18036
518817b3
SM
18037 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18038 NULL);
0114d602 18039 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18040
74a2f8ff
JB
18041 /* In Ada, an unspecified type is typically used when the description
18042 of the type is defered to a different unit. When encountering
18043 such a type, we treat it as a stub, and try to resolve it later on,
18044 when needed. */
18045 if (cu->language == language_ada)
18046 TYPE_STUB (type) = 1;
18047
f792889a 18048 return set_die_type (die, type, cu);
81a17f79 18049}
a02abb62 18050
639d11d3
DC
18051/* Read a single die and all its descendents. Set the die's sibling
18052 field to NULL; set other fields in the die correctly, and set all
18053 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18054 location of the info_ptr after reading all of those dies. PARENT
18055 is the parent of the die in question. */
18056
18057static struct die_info *
dee91e82 18058read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18059 const gdb_byte *info_ptr,
18060 const gdb_byte **new_info_ptr,
dee91e82 18061 struct die_info *parent)
639d11d3
DC
18062{
18063 struct die_info *die;
d521ce57 18064 const gdb_byte *cur_ptr;
639d11d3
DC
18065 int has_children;
18066
bf6af496 18067 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18068 if (die == NULL)
18069 {
18070 *new_info_ptr = cur_ptr;
18071 return NULL;
18072 }
93311388 18073 store_in_ref_table (die, reader->cu);
639d11d3
DC
18074
18075 if (has_children)
bf6af496 18076 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18077 else
18078 {
18079 die->child = NULL;
18080 *new_info_ptr = cur_ptr;
18081 }
18082
18083 die->sibling = NULL;
18084 die->parent = parent;
18085 return die;
18086}
18087
18088/* Read a die, all of its descendents, and all of its siblings; set
18089 all of the fields of all of the dies correctly. Arguments are as
18090 in read_die_and_children. */
18091
18092static struct die_info *
bf6af496 18093read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18094 const gdb_byte *info_ptr,
18095 const gdb_byte **new_info_ptr,
bf6af496 18096 struct die_info *parent)
639d11d3
DC
18097{
18098 struct die_info *first_die, *last_sibling;
d521ce57 18099 const gdb_byte *cur_ptr;
639d11d3 18100
c906108c 18101 cur_ptr = info_ptr;
639d11d3
DC
18102 first_die = last_sibling = NULL;
18103
18104 while (1)
c906108c 18105 {
639d11d3 18106 struct die_info *die
dee91e82 18107 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18108
1d325ec1 18109 if (die == NULL)
c906108c 18110 {
639d11d3
DC
18111 *new_info_ptr = cur_ptr;
18112 return first_die;
c906108c 18113 }
1d325ec1
DJ
18114
18115 if (!first_die)
18116 first_die = die;
c906108c 18117 else
1d325ec1
DJ
18118 last_sibling->sibling = die;
18119
18120 last_sibling = die;
c906108c 18121 }
c906108c
SS
18122}
18123
bf6af496
DE
18124/* Read a die, all of its descendents, and all of its siblings; set
18125 all of the fields of all of the dies correctly. Arguments are as
18126 in read_die_and_children.
18127 This the main entry point for reading a DIE and all its children. */
18128
18129static struct die_info *
18130read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18131 const gdb_byte *info_ptr,
18132 const gdb_byte **new_info_ptr,
bf6af496
DE
18133 struct die_info *parent)
18134{
18135 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18136 new_info_ptr, parent);
18137
b4f54984 18138 if (dwarf_die_debug)
bf6af496
DE
18139 {
18140 fprintf_unfiltered (gdb_stdlog,
18141 "Read die from %s@0x%x of %s:\n",
a32a8923 18142 get_section_name (reader->die_section),
bf6af496
DE
18143 (unsigned) (info_ptr - reader->die_section->buffer),
18144 bfd_get_filename (reader->abfd));
b4f54984 18145 dump_die (die, dwarf_die_debug);
bf6af496
DE
18146 }
18147
18148 return die;
18149}
18150
3019eac3
DE
18151/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18152 attributes.
18153 The caller is responsible for filling in the extra attributes
18154 and updating (*DIEP)->num_attrs.
18155 Set DIEP to point to a newly allocated die with its information,
18156 except for its child, sibling, and parent fields.
18157 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18158
d521ce57 18159static const gdb_byte *
3019eac3 18160read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18161 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18162 int *has_children, int num_extra_attrs)
93311388 18163{
b64f50a1 18164 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18165 struct abbrev_info *abbrev;
18166 struct die_info *die;
18167 struct dwarf2_cu *cu = reader->cu;
18168 bfd *abfd = reader->abfd;
18169
9c541725 18170 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18171 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18172 info_ptr += bytes_read;
18173 if (!abbrev_number)
18174 {
18175 *diep = NULL;
18176 *has_children = 0;
18177 return info_ptr;
18178 }
18179
685af9cd 18180 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18181 if (!abbrev)
348e048f
DE
18182 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18183 abbrev_number,
18184 bfd_get_filename (abfd));
18185
3019eac3 18186 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18187 die->sect_off = sect_off;
93311388
DE
18188 die->tag = abbrev->tag;
18189 die->abbrev = abbrev_number;
18190
3019eac3
DE
18191 /* Make the result usable.
18192 The caller needs to update num_attrs after adding the extra
18193 attributes. */
93311388
DE
18194 die->num_attrs = abbrev->num_attrs;
18195
18196 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18197 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18198 info_ptr);
93311388
DE
18199
18200 *diep = die;
18201 *has_children = abbrev->has_children;
18202 return info_ptr;
18203}
18204
3019eac3
DE
18205/* Read a die and all its attributes.
18206 Set DIEP to point to a newly allocated die with its information,
18207 except for its child, sibling, and parent fields.
18208 Set HAS_CHILDREN to tell whether the die has children or not. */
18209
d521ce57 18210static const gdb_byte *
3019eac3 18211read_full_die (const struct die_reader_specs *reader,
d521ce57 18212 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18213 int *has_children)
18214{
d521ce57 18215 const gdb_byte *result;
bf6af496
DE
18216
18217 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18218
b4f54984 18219 if (dwarf_die_debug)
bf6af496
DE
18220 {
18221 fprintf_unfiltered (gdb_stdlog,
18222 "Read die from %s@0x%x of %s:\n",
a32a8923 18223 get_section_name (reader->die_section),
bf6af496
DE
18224 (unsigned) (info_ptr - reader->die_section->buffer),
18225 bfd_get_filename (reader->abfd));
b4f54984 18226 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18227 }
18228
18229 return result;
3019eac3 18230}
433df2d4
DE
18231\f
18232/* Abbreviation tables.
3019eac3 18233
433df2d4 18234 In DWARF version 2, the description of the debugging information is
c906108c
SS
18235 stored in a separate .debug_abbrev section. Before we read any
18236 dies from a section we read in all abbreviations and install them
433df2d4
DE
18237 in a hash table. */
18238
18239/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18240
685af9cd
TT
18241struct abbrev_info *
18242abbrev_table::alloc_abbrev ()
433df2d4
DE
18243{
18244 struct abbrev_info *abbrev;
18245
685af9cd 18246 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18247 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18248
433df2d4
DE
18249 return abbrev;
18250}
18251
18252/* Add an abbreviation to the table. */
c906108c 18253
685af9cd
TT
18254void
18255abbrev_table::add_abbrev (unsigned int abbrev_number,
18256 struct abbrev_info *abbrev)
433df2d4
DE
18257{
18258 unsigned int hash_number;
18259
18260 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18261 abbrev->next = m_abbrevs[hash_number];
18262 m_abbrevs[hash_number] = abbrev;
433df2d4 18263}
dee91e82 18264
433df2d4
DE
18265/* Look up an abbrev in the table.
18266 Returns NULL if the abbrev is not found. */
18267
685af9cd
TT
18268struct abbrev_info *
18269abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18270{
433df2d4
DE
18271 unsigned int hash_number;
18272 struct abbrev_info *abbrev;
18273
18274 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18275 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18276
18277 while (abbrev)
18278 {
18279 if (abbrev->number == abbrev_number)
18280 return abbrev;
18281 abbrev = abbrev->next;
18282 }
18283 return NULL;
18284}
18285
18286/* Read in an abbrev table. */
18287
685af9cd 18288static abbrev_table_up
ed2dc618
SM
18289abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18290 struct dwarf2_section_info *section,
9c541725 18291 sect_offset sect_off)
433df2d4
DE
18292{
18293 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18294 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18295 const gdb_byte *abbrev_ptr;
c906108c
SS
18296 struct abbrev_info *cur_abbrev;
18297 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18298 unsigned int abbrev_form;
f3dd6933
DJ
18299 struct attr_abbrev *cur_attrs;
18300 unsigned int allocated_attrs;
c906108c 18301
685af9cd 18302 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18303
433df2d4 18304 dwarf2_read_section (objfile, section);
9c541725 18305 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18306 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18307 abbrev_ptr += bytes_read;
18308
f3dd6933 18309 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18310 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18311
0963b4bd 18312 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18313 while (abbrev_number)
18314 {
685af9cd 18315 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18316
18317 /* read in abbrev header */
18318 cur_abbrev->number = abbrev_number;
aead7601
SM
18319 cur_abbrev->tag
18320 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18321 abbrev_ptr += bytes_read;
18322 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18323 abbrev_ptr += 1;
18324
18325 /* now read in declarations */
22d2f3ab 18326 for (;;)
c906108c 18327 {
43988095
JK
18328 LONGEST implicit_const;
18329
22d2f3ab
JK
18330 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18331 abbrev_ptr += bytes_read;
18332 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18333 abbrev_ptr += bytes_read;
43988095
JK
18334 if (abbrev_form == DW_FORM_implicit_const)
18335 {
18336 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18337 &bytes_read);
18338 abbrev_ptr += bytes_read;
18339 }
18340 else
18341 {
18342 /* Initialize it due to a false compiler warning. */
18343 implicit_const = -1;
18344 }
22d2f3ab
JK
18345
18346 if (abbrev_name == 0)
18347 break;
18348
f3dd6933 18349 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18350 {
f3dd6933
DJ
18351 allocated_attrs += ATTR_ALLOC_CHUNK;
18352 cur_attrs
224c3ddb 18353 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18354 }
ae038cb0 18355
aead7601
SM
18356 cur_attrs[cur_abbrev->num_attrs].name
18357 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18358 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18359 = (enum dwarf_form) abbrev_form;
43988095 18360 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18361 ++cur_abbrev->num_attrs;
c906108c
SS
18362 }
18363
8d749320
SM
18364 cur_abbrev->attrs =
18365 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18366 cur_abbrev->num_attrs);
f3dd6933
DJ
18367 memcpy (cur_abbrev->attrs, cur_attrs,
18368 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18369
685af9cd 18370 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18371
18372 /* Get next abbreviation.
18373 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18374 always properly terminated with an abbrev number of 0.
18375 Exit loop if we encounter an abbreviation which we have
18376 already read (which means we are about to read the abbreviations
18377 for the next compile unit) or if the end of the abbreviation
18378 table is reached. */
433df2d4 18379 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18380 break;
18381 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18382 abbrev_ptr += bytes_read;
685af9cd 18383 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18384 break;
18385 }
f3dd6933
DJ
18386
18387 xfree (cur_attrs);
433df2d4 18388 return abbrev_table;
c906108c
SS
18389}
18390
72bf9492
DJ
18391/* Returns nonzero if TAG represents a type that we might generate a partial
18392 symbol for. */
18393
18394static int
18395is_type_tag_for_partial (int tag)
18396{
18397 switch (tag)
18398 {
18399#if 0
18400 /* Some types that would be reasonable to generate partial symbols for,
18401 that we don't at present. */
18402 case DW_TAG_array_type:
18403 case DW_TAG_file_type:
18404 case DW_TAG_ptr_to_member_type:
18405 case DW_TAG_set_type:
18406 case DW_TAG_string_type:
18407 case DW_TAG_subroutine_type:
18408#endif
18409 case DW_TAG_base_type:
18410 case DW_TAG_class_type:
680b30c7 18411 case DW_TAG_interface_type:
72bf9492
DJ
18412 case DW_TAG_enumeration_type:
18413 case DW_TAG_structure_type:
18414 case DW_TAG_subrange_type:
18415 case DW_TAG_typedef:
18416 case DW_TAG_union_type:
18417 return 1;
18418 default:
18419 return 0;
18420 }
18421}
18422
18423/* Load all DIEs that are interesting for partial symbols into memory. */
18424
18425static struct partial_die_info *
dee91e82 18426load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18427 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18428{
dee91e82 18429 struct dwarf2_cu *cu = reader->cu;
518817b3 18430 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18431 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18432 unsigned int bytes_read;
5afb4e99 18433 unsigned int load_all = 0;
72bf9492
DJ
18434 int nesting_level = 1;
18435
18436 parent_die = NULL;
18437 last_die = NULL;
18438
7adf1e79
DE
18439 gdb_assert (cu->per_cu != NULL);
18440 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18441 load_all = 1;
18442
72bf9492
DJ
18443 cu->partial_dies
18444 = htab_create_alloc_ex (cu->header.length / 12,
18445 partial_die_hash,
18446 partial_die_eq,
18447 NULL,
18448 &cu->comp_unit_obstack,
18449 hashtab_obstack_allocate,
18450 dummy_obstack_deallocate);
18451
72bf9492
DJ
18452 while (1)
18453 {
685af9cd 18454 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18455
18456 /* A NULL abbrev means the end of a series of children. */
18457 if (abbrev == NULL)
18458 {
18459 if (--nesting_level == 0)
cd9983dd
YQ
18460 return first_die;
18461
72bf9492
DJ
18462 info_ptr += bytes_read;
18463 last_die = parent_die;
18464 parent_die = parent_die->die_parent;
18465 continue;
18466 }
18467
98bfdba5
PA
18468 /* Check for template arguments. We never save these; if
18469 they're seen, we just mark the parent, and go on our way. */
18470 if (parent_die != NULL
18471 && cu->language == language_cplus
18472 && (abbrev->tag == DW_TAG_template_type_param
18473 || abbrev->tag == DW_TAG_template_value_param))
18474 {
18475 parent_die->has_template_arguments = 1;
18476
18477 if (!load_all)
18478 {
18479 /* We don't need a partial DIE for the template argument. */
dee91e82 18480 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18481 continue;
18482 }
18483 }
18484
0d99eb77 18485 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18486 Skip their other children. */
18487 if (!load_all
18488 && cu->language == language_cplus
18489 && parent_die != NULL
18490 && parent_die->tag == DW_TAG_subprogram)
18491 {
dee91e82 18492 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18493 continue;
18494 }
18495
5afb4e99
DJ
18496 /* Check whether this DIE is interesting enough to save. Normally
18497 we would not be interested in members here, but there may be
18498 later variables referencing them via DW_AT_specification (for
18499 static members). */
18500 if (!load_all
18501 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18502 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18503 && abbrev->tag != DW_TAG_enumerator
18504 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18505 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18506 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18507 && abbrev->tag != DW_TAG_variable
5afb4e99 18508 && abbrev->tag != DW_TAG_namespace
f55ee35c 18509 && abbrev->tag != DW_TAG_module
95554aad 18510 && abbrev->tag != DW_TAG_member
74921315
KS
18511 && abbrev->tag != DW_TAG_imported_unit
18512 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18513 {
18514 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18515 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18516 continue;
18517 }
18518
6f06d47b
YQ
18519 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18520 abbrev);
cd9983dd 18521
48fbe735 18522 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18523
18524 /* This two-pass algorithm for processing partial symbols has a
18525 high cost in cache pressure. Thus, handle some simple cases
18526 here which cover the majority of C partial symbols. DIEs
18527 which neither have specification tags in them, nor could have
18528 specification tags elsewhere pointing at them, can simply be
18529 processed and discarded.
18530
18531 This segment is also optional; scan_partial_symbols and
18532 add_partial_symbol will handle these DIEs if we chain
18533 them in normally. When compilers which do not emit large
18534 quantities of duplicate debug information are more common,
18535 this code can probably be removed. */
18536
18537 /* Any complete simple types at the top level (pretty much all
18538 of them, for a language without namespaces), can be processed
18539 directly. */
18540 if (parent_die == NULL
cd9983dd
YQ
18541 && pdi.has_specification == 0
18542 && pdi.is_declaration == 0
18543 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18544 || pdi.tag == DW_TAG_base_type
18545 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18546 {
cd9983dd 18547 if (building_psymtab && pdi.name != NULL)
67547d89 18548 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
79748972 18549 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18550 psymbol_placement::STATIC,
1762568f 18551 0, cu->language, objfile);
cd9983dd 18552 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18553 continue;
18554 }
18555
d8228535
JK
18556 /* The exception for DW_TAG_typedef with has_children above is
18557 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18558 type_name_or_error will error on such types later.
d8228535
JK
18559
18560 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18561 it could not find the child DIEs referenced later, this is checked
18562 above. In correct DWARF DW_TAG_typedef should have no children. */
18563
cd9983dd 18564 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18565 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18566 "- DIE at %s [in module %s]"),
cd9983dd 18567 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18568
72bf9492
DJ
18569 /* If we're at the second level, and we're an enumerator, and
18570 our parent has no specification (meaning possibly lives in a
18571 namespace elsewhere), then we can add the partial symbol now
18572 instead of queueing it. */
cd9983dd 18573 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18574 && parent_die != NULL
18575 && parent_die->die_parent == NULL
18576 && parent_die->tag == DW_TAG_enumeration_type
18577 && parent_die->has_specification == 0)
18578 {
cd9983dd 18579 if (pdi.name == NULL)
b98664d3 18580 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18581 else if (building_psymtab)
67547d89 18582 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
79748972 18583 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18584 cu->language == language_cplus
75aedd27
TT
18585 ? psymbol_placement::GLOBAL
18586 : psymbol_placement::STATIC,
1762568f 18587 0, cu->language, objfile);
72bf9492 18588
cd9983dd 18589 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18590 continue;
18591 }
18592
cd9983dd 18593 struct partial_die_info *part_die
6f06d47b 18594 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18595
72bf9492
DJ
18596 /* We'll save this DIE so link it in. */
18597 part_die->die_parent = parent_die;
18598 part_die->die_sibling = NULL;
18599 part_die->die_child = NULL;
18600
18601 if (last_die && last_die == parent_die)
18602 last_die->die_child = part_die;
18603 else if (last_die)
18604 last_die->die_sibling = part_die;
18605
18606 last_die = part_die;
18607
18608 if (first_die == NULL)
18609 first_die = part_die;
18610
18611 /* Maybe add the DIE to the hash table. Not all DIEs that we
18612 find interesting need to be in the hash table, because we
18613 also have the parent/sibling/child chains; only those that we
18614 might refer to by offset later during partial symbol reading.
18615
18616 For now this means things that might have be the target of a
18617 DW_AT_specification, DW_AT_abstract_origin, or
18618 DW_AT_extension. DW_AT_extension will refer only to
18619 namespaces; DW_AT_abstract_origin refers to functions (and
18620 many things under the function DIE, but we do not recurse
18621 into function DIEs during partial symbol reading) and
18622 possibly variables as well; DW_AT_specification refers to
18623 declarations. Declarations ought to have the DW_AT_declaration
18624 flag. It happens that GCC forgets to put it in sometimes, but
18625 only for functions, not for types.
18626
18627 Adding more things than necessary to the hash table is harmless
18628 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18629 wasted time in find_partial_die, when we reread the compilation
18630 unit with load_all_dies set. */
72bf9492 18631
5afb4e99 18632 if (load_all
72929c62 18633 || abbrev->tag == DW_TAG_constant
5afb4e99 18634 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18635 || abbrev->tag == DW_TAG_variable
18636 || abbrev->tag == DW_TAG_namespace
18637 || part_die->is_declaration)
18638 {
18639 void **slot;
18640
18641 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18642 to_underlying (part_die->sect_off),
18643 INSERT);
72bf9492
DJ
18644 *slot = part_die;
18645 }
18646
72bf9492 18647 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18648 we have no reason to follow the children of structures; for other
98bfdba5
PA
18649 languages we have to, so that we can get at method physnames
18650 to infer fully qualified class names, for DW_AT_specification,
18651 and for C++ template arguments. For C++, we also look one level
18652 inside functions to find template arguments (if the name of the
18653 function does not already contain the template arguments).
bc30ff58 18654
0a4b0913
AB
18655 For Ada and Fortran, we need to scan the children of subprograms
18656 and lexical blocks as well because these languages allow the
18657 definition of nested entities that could be interesting for the
18658 debugger, such as nested subprograms for instance. */
72bf9492 18659 if (last_die->has_children
5afb4e99
DJ
18660 && (load_all
18661 || last_die->tag == DW_TAG_namespace
f55ee35c 18662 || last_die->tag == DW_TAG_module
72bf9492 18663 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18664 || (cu->language == language_cplus
18665 && last_die->tag == DW_TAG_subprogram
18666 && (last_die->name == NULL
18667 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18668 || (cu->language != language_c
18669 && (last_die->tag == DW_TAG_class_type
680b30c7 18670 || last_die->tag == DW_TAG_interface_type
72bf9492 18671 || last_die->tag == DW_TAG_structure_type
bc30ff58 18672 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18673 || ((cu->language == language_ada
18674 || cu->language == language_fortran)
bc30ff58
JB
18675 && (last_die->tag == DW_TAG_subprogram
18676 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18677 {
18678 nesting_level++;
18679 parent_die = last_die;
18680 continue;
18681 }
18682
18683 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18684 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18685
18686 /* Back to the top, do it again. */
18687 }
18688}
18689
6f06d47b
YQ
18690partial_die_info::partial_die_info (sect_offset sect_off_,
18691 struct abbrev_info *abbrev)
18692 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18693{
18694}
18695
35cc7ed7
YQ
18696/* Read a minimal amount of information into the minimal die structure.
18697 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18698
48fbe735
YQ
18699const gdb_byte *
18700partial_die_info::read (const struct die_reader_specs *reader,
18701 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18702{
dee91e82 18703 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18704 struct dwarf2_per_objfile *dwarf2_per_objfile
18705 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18706 unsigned int i;
c5aa993b 18707 int has_low_pc_attr = 0;
c906108c 18708 int has_high_pc_attr = 0;
91da1414 18709 int high_pc_relative = 0;
c906108c 18710
fd0a254f 18711 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18712 {
48fbe735
YQ
18713 struct attribute attr;
18714
fd0a254f 18715 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18716
18717 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18718 partial symbol table. */
c906108c
SS
18719 switch (attr.name)
18720 {
18721 case DW_AT_name:
48fbe735 18722 switch (tag)
71c25dea
TT
18723 {
18724 case DW_TAG_compile_unit:
95554aad 18725 case DW_TAG_partial_unit:
348e048f 18726 case DW_TAG_type_unit:
71c25dea
TT
18727 /* Compilation units have a DW_AT_name that is a filename, not
18728 a source language identifier. */
18729 case DW_TAG_enumeration_type:
18730 case DW_TAG_enumerator:
18731 /* These tags always have simple identifiers already; no need
18732 to canonicalize them. */
48fbe735 18733 name = DW_STRING (&attr);
71c25dea
TT
18734 break;
18735 default:
48fbe735
YQ
18736 {
18737 struct objfile *objfile = dwarf2_per_objfile->objfile;
18738
18739 name
18740 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18741 &objfile->per_bfd->storage_obstack);
18742 }
71c25dea
TT
18743 break;
18744 }
c906108c 18745 break;
31ef98ae 18746 case DW_AT_linkage_name:
c906108c 18747 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18748 /* Note that both forms of linkage name might appear. We
18749 assume they will be the same, and we only store the last
18750 one we see. */
48fbe735 18751 linkage_name = DW_STRING (&attr);
c906108c
SS
18752 break;
18753 case DW_AT_low_pc:
18754 has_low_pc_attr = 1;
48fbe735 18755 lowpc = attr_value_as_address (&attr);
c906108c
SS
18756 break;
18757 case DW_AT_high_pc:
18758 has_high_pc_attr = 1;
48fbe735 18759 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18760 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18761 high_pc_relative = 1;
c906108c
SS
18762 break;
18763 case DW_AT_location:
0963b4bd 18764 /* Support the .debug_loc offsets. */
8e19ed76
PS
18765 if (attr_form_is_block (&attr))
18766 {
48fbe735 18767 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18768 }
3690dd37 18769 else if (attr_form_is_section_offset (&attr))
8e19ed76 18770 {
4d3c2250 18771 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18772 }
18773 else
18774 {
4d3c2250
KB
18775 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18776 "partial symbol information");
8e19ed76 18777 }
c906108c 18778 break;
c906108c 18779 case DW_AT_external:
48fbe735 18780 is_external = DW_UNSND (&attr);
c906108c
SS
18781 break;
18782 case DW_AT_declaration:
48fbe735 18783 is_declaration = DW_UNSND (&attr);
c906108c
SS
18784 break;
18785 case DW_AT_type:
48fbe735 18786 has_type = 1;
c906108c
SS
18787 break;
18788 case DW_AT_abstract_origin:
18789 case DW_AT_specification:
72bf9492 18790 case DW_AT_extension:
48fbe735
YQ
18791 has_specification = 1;
18792 spec_offset = dwarf2_get_ref_die_offset (&attr);
18793 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18794 || cu->per_cu->is_dwz);
c906108c
SS
18795 break;
18796 case DW_AT_sibling:
18797 /* Ignore absolute siblings, they might point outside of
18798 the current compile unit. */
18799 if (attr.form == DW_FORM_ref_addr)
b98664d3 18800 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18801 else
b9502d3f 18802 {
48fbe735 18803 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18804 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18805 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18806
18807 if (sibling_ptr < info_ptr)
b98664d3 18808 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18809 else if (sibling_ptr > reader->buffer_end)
18810 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18811 else
48fbe735 18812 sibling = sibling_ptr;
b9502d3f 18813 }
c906108c 18814 break;
fa4028e9 18815 case DW_AT_byte_size:
48fbe735 18816 has_byte_size = 1;
fa4028e9 18817 break;
ff908ebf 18818 case DW_AT_const_value:
48fbe735 18819 has_const_value = 1;
ff908ebf 18820 break;
68511cec
CES
18821 case DW_AT_calling_convention:
18822 /* DWARF doesn't provide a way to identify a program's source-level
18823 entry point. DW_AT_calling_convention attributes are only meant
18824 to describe functions' calling conventions.
18825
18826 However, because it's a necessary piece of information in
0c1b455e
TT
18827 Fortran, and before DWARF 4 DW_CC_program was the only
18828 piece of debugging information whose definition refers to
18829 a 'main program' at all, several compilers marked Fortran
18830 main programs with DW_CC_program --- even when those
18831 functions use the standard calling conventions.
18832
18833 Although DWARF now specifies a way to provide this
18834 information, we support this practice for backward
18835 compatibility. */
68511cec 18836 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18837 && cu->language == language_fortran)
48fbe735 18838 main_subprogram = 1;
68511cec 18839 break;
481860b3
GB
18840 case DW_AT_inline:
18841 if (DW_UNSND (&attr) == DW_INL_inlined
18842 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18843 may_be_inlined = 1;
481860b3 18844 break;
95554aad
TT
18845
18846 case DW_AT_import:
48fbe735 18847 if (tag == DW_TAG_imported_unit)
36586728 18848 {
48fbe735
YQ
18849 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18850 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18851 || cu->per_cu->is_dwz);
18852 }
95554aad
TT
18853 break;
18854
0c1b455e 18855 case DW_AT_main_subprogram:
48fbe735 18856 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18857 break;
18858
05caa1d2
TT
18859 case DW_AT_ranges:
18860 {
18861 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18862 but that requires a full DIE, so instead we just
18863 reimplement it. */
18864 int need_ranges_base = tag != DW_TAG_compile_unit;
18865 unsigned int ranges_offset = (DW_UNSND (&attr)
18866 + (need_ranges_base
18867 ? cu->ranges_base
18868 : 0));
18869
18870 /* Value of the DW_AT_ranges attribute is the offset in the
18871 .debug_ranges section. */
18872 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18873 nullptr))
18874 has_pc_info = 1;
18875 }
18876 break;
18877
c906108c
SS
18878 default:
18879 break;
18880 }
18881 }
18882
10d06d82
TT
18883 /* For Ada, if both the name and the linkage name appear, we prefer
18884 the latter. This lets "catch exception" work better, regardless
18885 of the order in which the name and linkage name were emitted.
18886 Really, though, this is just a workaround for the fact that gdb
18887 doesn't store both the name and the linkage name. */
18888 if (cu->language == language_ada && linkage_name != nullptr)
18889 name = linkage_name;
18890
91da1414 18891 if (high_pc_relative)
48fbe735 18892 highpc += lowpc;
91da1414 18893
9373cf26
JK
18894 if (has_low_pc_attr && has_high_pc_attr)
18895 {
18896 /* When using the GNU linker, .gnu.linkonce. sections are used to
18897 eliminate duplicate copies of functions and vtables and such.
18898 The linker will arbitrarily choose one and discard the others.
18899 The AT_*_pc values for such functions refer to local labels in
18900 these sections. If the section from that file was discarded, the
18901 labels are not in the output, so the relocs get a value of 0.
18902 If this is a discarded function, mark the pc bounds as invalid,
18903 so that GDB will ignore it. */
48fbe735 18904 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18905 {
48fbe735 18906 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18908
b98664d3 18909 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18910 "for DIE at %s [in module %s]"),
48fbe735
YQ
18911 paddress (gdbarch, lowpc),
18912 sect_offset_str (sect_off),
9d8780f0 18913 objfile_name (objfile));
9373cf26
JK
18914 }
18915 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18916 else if (lowpc >= highpc)
9373cf26 18917 {
48fbe735 18918 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18919 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18920
b98664d3 18921 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18922 "for DIE at %s [in module %s]"),
48fbe735
YQ
18923 paddress (gdbarch, lowpc),
18924 paddress (gdbarch, highpc),
18925 sect_offset_str (sect_off),
9c541725 18926 objfile_name (objfile));
9373cf26
JK
18927 }
18928 else
48fbe735 18929 has_pc_info = 1;
9373cf26 18930 }
85cbf3d3 18931
c906108c
SS
18932 return info_ptr;
18933}
18934
72bf9492
DJ
18935/* Find a cached partial DIE at OFFSET in CU. */
18936
d590ff25
YQ
18937struct partial_die_info *
18938dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18939{
18940 struct partial_die_info *lookup_die = NULL;
6f06d47b 18941 struct partial_die_info part_die (sect_off);
72bf9492 18942
9a3c8263 18943 lookup_die = ((struct partial_die_info *)
d590ff25 18944 htab_find_with_hash (partial_dies, &part_die,
9c541725 18945 to_underlying (sect_off)));
72bf9492 18946
72bf9492
DJ
18947 return lookup_die;
18948}
18949
348e048f
DE
18950/* Find a partial DIE at OFFSET, which may or may not be in CU,
18951 except in the case of .debug_types DIEs which do not reference
18952 outside their CU (they do however referencing other types via
55f1336d 18953 DW_FORM_ref_sig8). */
72bf9492 18954
122cf0f2 18955static const struct cu_partial_die_info
9c541725 18956find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18957{
518817b3
SM
18958 struct dwarf2_per_objfile *dwarf2_per_objfile
18959 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18960 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18961 struct dwarf2_per_cu_data *per_cu = NULL;
18962 struct partial_die_info *pd = NULL;
72bf9492 18963
36586728 18964 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18965 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18966 {
d590ff25 18967 pd = cu->find_partial_die (sect_off);
5afb4e99 18968 if (pd != NULL)
fb816e8b 18969 return { cu, pd };
0d99eb77
DE
18970 /* We missed recording what we needed.
18971 Load all dies and try again. */
18972 per_cu = cu->per_cu;
5afb4e99 18973 }
0d99eb77
DE
18974 else
18975 {
18976 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18977 if (cu->per_cu->is_debug_types)
0d99eb77 18978 {
9d8780f0
SM
18979 error (_("Dwarf Error: Type Unit at offset %s contains"
18980 " external reference to offset %s [in module %s].\n"),
18981 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18982 bfd_get_filename (objfile->obfd));
18983 }
9c541725 18984 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18985 dwarf2_per_objfile);
72bf9492 18986
0d99eb77
DE
18987 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18988 load_partial_comp_unit (per_cu);
ae038cb0 18989
0d99eb77 18990 per_cu->cu->last_used = 0;
d590ff25 18991 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18992 }
5afb4e99 18993
dee91e82
DE
18994 /* If we didn't find it, and not all dies have been loaded,
18995 load them all and try again. */
18996
5afb4e99
DJ
18997 if (pd == NULL && per_cu->load_all_dies == 0)
18998 {
5afb4e99 18999 per_cu->load_all_dies = 1;
fd820528
DE
19000
19001 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19002 THIS_CU->cu may already be in use. So we can't just free it and
19003 replace its DIEs with the ones we read in. Instead, we leave those
19004 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19005 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19006 set. */
dee91e82 19007 load_partial_comp_unit (per_cu);
5afb4e99 19008
d590ff25 19009 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19010 }
19011
19012 if (pd == NULL)
19013 internal_error (__FILE__, __LINE__,
9d8780f0 19014 _("could not find partial DIE %s "
3e43a32a 19015 "in cache [from module %s]\n"),
9d8780f0 19016 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19017 return { per_cu->cu, pd };
72bf9492
DJ
19018}
19019
abc72ce4
DE
19020/* See if we can figure out if the class lives in a namespace. We do
19021 this by looking for a member function; its demangled name will
19022 contain namespace info, if there is any. */
19023
19024static void
19025guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19026 struct dwarf2_cu *cu)
19027{
19028 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19029 what template types look like, because the demangler
19030 frequently doesn't give the same name as the debug info. We
19031 could fix this by only using the demangled name to get the
19032 prefix (but see comment in read_structure_type). */
19033
19034 struct partial_die_info *real_pdi;
19035 struct partial_die_info *child_pdi;
19036
19037 /* If this DIE (this DIE's specification, if any) has a parent, then
19038 we should not do this. We'll prepend the parent's fully qualified
19039 name when we create the partial symbol. */
19040
19041 real_pdi = struct_pdi;
19042 while (real_pdi->has_specification)
fb816e8b 19043 {
122cf0f2
AB
19044 auto res = find_partial_die (real_pdi->spec_offset,
19045 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19046 real_pdi = res.pdi;
19047 cu = res.cu;
19048 }
abc72ce4
DE
19049
19050 if (real_pdi->die_parent != NULL)
19051 return;
19052
19053 for (child_pdi = struct_pdi->die_child;
19054 child_pdi != NULL;
19055 child_pdi = child_pdi->die_sibling)
19056 {
19057 if (child_pdi->tag == DW_TAG_subprogram
19058 && child_pdi->linkage_name != NULL)
19059 {
19060 char *actual_class_name
19061 = language_class_name_from_physname (cu->language_defn,
19062 child_pdi->linkage_name);
19063 if (actual_class_name != NULL)
19064 {
518817b3 19065 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19066 struct_pdi->name
021887d8
TT
19067 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19068 actual_class_name);
abc72ce4
DE
19069 xfree (actual_class_name);
19070 }
19071 break;
19072 }
19073 }
19074}
19075
52356b79
YQ
19076void
19077partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19078{
abc72ce4
DE
19079 /* Once we've fixed up a die, there's no point in doing so again.
19080 This also avoids a memory leak if we were to call
19081 guess_partial_die_structure_name multiple times. */
52356b79 19082 if (fixup_called)
abc72ce4
DE
19083 return;
19084
72bf9492
DJ
19085 /* If we found a reference attribute and the DIE has no name, try
19086 to find a name in the referred to DIE. */
19087
52356b79 19088 if (name == NULL && has_specification)
72bf9492
DJ
19089 {
19090 struct partial_die_info *spec_die;
72bf9492 19091
122cf0f2 19092 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19093 spec_die = res.pdi;
19094 cu = res.cu;
72bf9492 19095
52356b79 19096 spec_die->fixup (cu);
72bf9492
DJ
19097
19098 if (spec_die->name)
19099 {
52356b79 19100 name = spec_die->name;
72bf9492
DJ
19101
19102 /* Copy DW_AT_external attribute if it is set. */
19103 if (spec_die->is_external)
52356b79 19104 is_external = spec_die->is_external;
72bf9492
DJ
19105 }
19106 }
19107
19108 /* Set default names for some unnamed DIEs. */
72bf9492 19109
52356b79
YQ
19110 if (name == NULL && tag == DW_TAG_namespace)
19111 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19112
abc72ce4
DE
19113 /* If there is no parent die to provide a namespace, and there are
19114 children, see if we can determine the namespace from their linkage
122d1940 19115 name. */
abc72ce4 19116 if (cu->language == language_cplus
fd5866f6 19117 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19118 && die_parent == NULL
19119 && has_children
19120 && (tag == DW_TAG_class_type
19121 || tag == DW_TAG_structure_type
19122 || tag == DW_TAG_union_type))
19123 guess_partial_die_structure_name (this, cu);
abc72ce4 19124
53832f31
TT
19125 /* GCC might emit a nameless struct or union that has a linkage
19126 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19127 if (name == NULL
19128 && (tag == DW_TAG_class_type
19129 || tag == DW_TAG_interface_type
19130 || tag == DW_TAG_structure_type
19131 || tag == DW_TAG_union_type)
19132 && linkage_name != NULL)
53832f31
TT
19133 {
19134 char *demangled;
19135
52356b79 19136 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19137 if (demangled)
19138 {
96408a79
SA
19139 const char *base;
19140
19141 /* Strip any leading namespaces/classes, keep only the base name.
19142 DW_AT_name for named DIEs does not contain the prefixes. */
19143 base = strrchr (demangled, ':');
19144 if (base && base > demangled && base[-1] == ':')
19145 base++;
19146 else
19147 base = demangled;
19148
518817b3 19149 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19150 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19151 xfree (demangled);
19152 }
19153 }
19154
52356b79 19155 fixup_called = 1;
72bf9492
DJ
19156}
19157
a8329558 19158/* Read an attribute value described by an attribute form. */
c906108c 19159
d521ce57 19160static const gdb_byte *
dee91e82
DE
19161read_attribute_value (const struct die_reader_specs *reader,
19162 struct attribute *attr, unsigned form,
43988095 19163 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19164{
dee91e82 19165 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19166 struct dwarf2_per_objfile *dwarf2_per_objfile
19167 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19168 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19169 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19170 bfd *abfd = reader->abfd;
e7c27a73 19171 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19172 unsigned int bytes_read;
19173 struct dwarf_block *blk;
19174
aead7601 19175 attr->form = (enum dwarf_form) form;
a8329558 19176 switch (form)
c906108c 19177 {
c906108c 19178 case DW_FORM_ref_addr:
ae411497 19179 if (cu->header.version == 2)
4568ecf9 19180 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19181 else
4568ecf9
DE
19182 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19183 &cu->header, &bytes_read);
ae411497
TT
19184 info_ptr += bytes_read;
19185 break;
36586728
TT
19186 case DW_FORM_GNU_ref_alt:
19187 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19188 info_ptr += bytes_read;
19189 break;
ae411497 19190 case DW_FORM_addr:
e7c27a73 19191 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19192 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19193 info_ptr += bytes_read;
c906108c
SS
19194 break;
19195 case DW_FORM_block2:
7b5a2f43 19196 blk = dwarf_alloc_block (cu);
c906108c
SS
19197 blk->size = read_2_bytes (abfd, info_ptr);
19198 info_ptr += 2;
19199 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19200 info_ptr += blk->size;
19201 DW_BLOCK (attr) = blk;
19202 break;
19203 case DW_FORM_block4:
7b5a2f43 19204 blk = dwarf_alloc_block (cu);
c906108c
SS
19205 blk->size = read_4_bytes (abfd, info_ptr);
19206 info_ptr += 4;
19207 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19208 info_ptr += blk->size;
19209 DW_BLOCK (attr) = blk;
19210 break;
19211 case DW_FORM_data2:
19212 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19213 info_ptr += 2;
19214 break;
19215 case DW_FORM_data4:
19216 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19217 info_ptr += 4;
19218 break;
19219 case DW_FORM_data8:
19220 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19221 info_ptr += 8;
19222 break;
0224619f
JK
19223 case DW_FORM_data16:
19224 blk = dwarf_alloc_block (cu);
19225 blk->size = 16;
19226 blk->data = read_n_bytes (abfd, info_ptr, 16);
19227 info_ptr += 16;
19228 DW_BLOCK (attr) = blk;
19229 break;
2dc7f7b3
TT
19230 case DW_FORM_sec_offset:
19231 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19232 info_ptr += bytes_read;
19233 break;
c906108c 19234 case DW_FORM_string:
9b1c24c8 19235 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19236 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19237 info_ptr += bytes_read;
19238 break;
4bdf3d34 19239 case DW_FORM_strp:
36586728
TT
19240 if (!cu->per_cu->is_dwz)
19241 {
ed2dc618
SM
19242 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19243 abfd, info_ptr, cu_header,
36586728
TT
19244 &bytes_read);
19245 DW_STRING_IS_CANONICAL (attr) = 0;
19246 info_ptr += bytes_read;
19247 break;
19248 }
19249 /* FALLTHROUGH */
43988095
JK
19250 case DW_FORM_line_strp:
19251 if (!cu->per_cu->is_dwz)
19252 {
ed2dc618
SM
19253 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19254 abfd, info_ptr,
43988095
JK
19255 cu_header, &bytes_read);
19256 DW_STRING_IS_CANONICAL (attr) = 0;
19257 info_ptr += bytes_read;
19258 break;
19259 }
19260 /* FALLTHROUGH */
36586728
TT
19261 case DW_FORM_GNU_strp_alt:
19262 {
ed2dc618 19263 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19264 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19265 &bytes_read);
19266
ed2dc618
SM
19267 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19268 dwz, str_offset);
36586728
TT
19269 DW_STRING_IS_CANONICAL (attr) = 0;
19270 info_ptr += bytes_read;
19271 }
4bdf3d34 19272 break;
2dc7f7b3 19273 case DW_FORM_exprloc:
c906108c 19274 case DW_FORM_block:
7b5a2f43 19275 blk = dwarf_alloc_block (cu);
c906108c
SS
19276 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19277 info_ptr += bytes_read;
19278 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19279 info_ptr += blk->size;
19280 DW_BLOCK (attr) = blk;
19281 break;
19282 case DW_FORM_block1:
7b5a2f43 19283 blk = dwarf_alloc_block (cu);
c906108c
SS
19284 blk->size = read_1_byte (abfd, info_ptr);
19285 info_ptr += 1;
19286 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19287 info_ptr += blk->size;
19288 DW_BLOCK (attr) = blk;
19289 break;
19290 case DW_FORM_data1:
19291 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19292 info_ptr += 1;
19293 break;
19294 case DW_FORM_flag:
19295 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19296 info_ptr += 1;
19297 break;
2dc7f7b3
TT
19298 case DW_FORM_flag_present:
19299 DW_UNSND (attr) = 1;
19300 break;
c906108c
SS
19301 case DW_FORM_sdata:
19302 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19303 info_ptr += bytes_read;
19304 break;
19305 case DW_FORM_udata:
19306 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19307 info_ptr += bytes_read;
19308 break;
19309 case DW_FORM_ref1:
9c541725 19310 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19311 + read_1_byte (abfd, info_ptr));
c906108c
SS
19312 info_ptr += 1;
19313 break;
19314 case DW_FORM_ref2:
9c541725 19315 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19316 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19317 info_ptr += 2;
19318 break;
19319 case DW_FORM_ref4:
9c541725 19320 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19321 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19322 info_ptr += 4;
19323 break;
613e1657 19324 case DW_FORM_ref8:
9c541725 19325 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19326 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19327 info_ptr += 8;
19328 break;
55f1336d 19329 case DW_FORM_ref_sig8:
ac9ec31b 19330 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19331 info_ptr += 8;
19332 break;
c906108c 19333 case DW_FORM_ref_udata:
9c541725 19334 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19335 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19336 info_ptr += bytes_read;
19337 break;
c906108c 19338 case DW_FORM_indirect:
a8329558
KW
19339 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19340 info_ptr += bytes_read;
43988095
JK
19341 if (form == DW_FORM_implicit_const)
19342 {
19343 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19344 info_ptr += bytes_read;
19345 }
19346 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19347 info_ptr);
19348 break;
19349 case DW_FORM_implicit_const:
19350 DW_SND (attr) = implicit_const;
a8329558 19351 break;
336d760d 19352 case DW_FORM_addrx:
3019eac3
DE
19353 case DW_FORM_GNU_addr_index:
19354 if (reader->dwo_file == NULL)
19355 {
19356 /* For now flag a hard error.
19357 Later we can turn this into a complaint. */
19358 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19359 dwarf_form_name (form),
19360 bfd_get_filename (abfd));
19361 }
19362 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19363 info_ptr += bytes_read;
19364 break;
cf532bd1 19365 case DW_FORM_strx:
15f18d14
AT
19366 case DW_FORM_strx1:
19367 case DW_FORM_strx2:
19368 case DW_FORM_strx3:
19369 case DW_FORM_strx4:
3019eac3
DE
19370 case DW_FORM_GNU_str_index:
19371 if (reader->dwo_file == NULL)
19372 {
19373 /* For now flag a hard error.
19374 Later we can turn this into a complaint if warranted. */
19375 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19376 dwarf_form_name (form),
19377 bfd_get_filename (abfd));
19378 }
19379 {
15f18d14
AT
19380 ULONGEST str_index;
19381 if (form == DW_FORM_strx1)
19382 {
19383 str_index = read_1_byte (abfd, info_ptr);
19384 info_ptr += 1;
19385 }
19386 else if (form == DW_FORM_strx2)
19387 {
19388 str_index = read_2_bytes (abfd, info_ptr);
19389 info_ptr += 2;
19390 }
19391 else if (form == DW_FORM_strx3)
19392 {
19393 str_index = read_3_bytes (abfd, info_ptr);
19394 info_ptr += 3;
19395 }
19396 else if (form == DW_FORM_strx4)
19397 {
19398 str_index = read_4_bytes (abfd, info_ptr);
19399 info_ptr += 4;
19400 }
19401 else
19402 {
19403 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19404 info_ptr += bytes_read;
19405 }
342587c4 19406 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19407 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19408 }
19409 break;
c906108c 19410 default:
8a3fe4f8 19411 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19412 dwarf_form_name (form),
19413 bfd_get_filename (abfd));
c906108c 19414 }
28e94949 19415
36586728 19416 /* Super hack. */
7771576e 19417 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19418 attr->form = DW_FORM_GNU_ref_alt;
19419
28e94949
JB
19420 /* We have seen instances where the compiler tried to emit a byte
19421 size attribute of -1 which ended up being encoded as an unsigned
19422 0xffffffff. Although 0xffffffff is technically a valid size value,
19423 an object of this size seems pretty unlikely so we can relatively
19424 safely treat these cases as if the size attribute was invalid and
19425 treat them as zero by default. */
19426 if (attr->name == DW_AT_byte_size
19427 && form == DW_FORM_data4
19428 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19429 {
19430 complaint
b98664d3 19431 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19432 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19433 DW_UNSND (attr) = 0;
19434 }
28e94949 19435
c906108c
SS
19436 return info_ptr;
19437}
19438
a8329558
KW
19439/* Read an attribute described by an abbreviated attribute. */
19440
d521ce57 19441static const gdb_byte *
dee91e82
DE
19442read_attribute (const struct die_reader_specs *reader,
19443 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19444 const gdb_byte *info_ptr)
a8329558
KW
19445{
19446 attr->name = abbrev->name;
43988095
JK
19447 return read_attribute_value (reader, attr, abbrev->form,
19448 abbrev->implicit_const, info_ptr);
a8329558
KW
19449}
19450
0963b4bd 19451/* Read dwarf information from a buffer. */
c906108c
SS
19452
19453static unsigned int
a1855c1d 19454read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19455{
fe1b8b76 19456 return bfd_get_8 (abfd, buf);
c906108c
SS
19457}
19458
19459static int
a1855c1d 19460read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19461{
fe1b8b76 19462 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19463}
19464
19465static unsigned int
a1855c1d 19466read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19467{
fe1b8b76 19468 return bfd_get_16 (abfd, buf);
c906108c
SS
19469}
19470
21ae7a4d 19471static int
a1855c1d 19472read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19473{
19474 return bfd_get_signed_16 (abfd, buf);
19475}
19476
15f18d14
AT
19477static unsigned int
19478read_3_bytes (bfd *abfd, const gdb_byte *buf)
19479{
19480 unsigned int result = 0;
19481 for (int i = 0; i < 3; ++i)
19482 {
19483 unsigned char byte = bfd_get_8 (abfd, buf);
19484 buf++;
19485 result |= ((unsigned int) byte << (i * 8));
19486 }
19487 return result;
19488}
19489
c906108c 19490static unsigned int
a1855c1d 19491read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19492{
fe1b8b76 19493 return bfd_get_32 (abfd, buf);
c906108c
SS
19494}
19495
21ae7a4d 19496static int
a1855c1d 19497read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19498{
19499 return bfd_get_signed_32 (abfd, buf);
19500}
19501
93311388 19502static ULONGEST
a1855c1d 19503read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19504{
fe1b8b76 19505 return bfd_get_64 (abfd, buf);
c906108c
SS
19506}
19507
19508static CORE_ADDR
d521ce57 19509read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19510 unsigned int *bytes_read)
c906108c 19511{
e7c27a73 19512 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19513 CORE_ADDR retval = 0;
19514
107d2387 19515 if (cu_header->signed_addr_p)
c906108c 19516 {
107d2387
AC
19517 switch (cu_header->addr_size)
19518 {
19519 case 2:
fe1b8b76 19520 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19521 break;
19522 case 4:
fe1b8b76 19523 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19524 break;
19525 case 8:
fe1b8b76 19526 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19527 break;
19528 default:
8e65ff28 19529 internal_error (__FILE__, __LINE__,
e2e0b3e5 19530 _("read_address: bad switch, signed [in module %s]"),
659b0389 19531 bfd_get_filename (abfd));
107d2387
AC
19532 }
19533 }
19534 else
19535 {
19536 switch (cu_header->addr_size)
19537 {
19538 case 2:
fe1b8b76 19539 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19540 break;
19541 case 4:
fe1b8b76 19542 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19543 break;
19544 case 8:
fe1b8b76 19545 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19546 break;
19547 default:
8e65ff28 19548 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19549 _("read_address: bad switch, "
19550 "unsigned [in module %s]"),
659b0389 19551 bfd_get_filename (abfd));
107d2387 19552 }
c906108c 19553 }
64367e0a 19554
107d2387
AC
19555 *bytes_read = cu_header->addr_size;
19556 return retval;
c906108c
SS
19557}
19558
f7ef9339 19559/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19560 specification allows the initial length to take up either 4 bytes
19561 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19562 bytes describe the length and all offsets will be 8 bytes in length
19563 instead of 4.
19564
f7ef9339
KB
19565 An older, non-standard 64-bit format is also handled by this
19566 function. The older format in question stores the initial length
19567 as an 8-byte quantity without an escape value. Lengths greater
19568 than 2^32 aren't very common which means that the initial 4 bytes
19569 is almost always zero. Since a length value of zero doesn't make
19570 sense for the 32-bit format, this initial zero can be considered to
19571 be an escape value which indicates the presence of the older 64-bit
19572 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19573 greater than 4GB. If it becomes necessary to handle lengths
19574 somewhat larger than 4GB, we could allow other small values (such
19575 as the non-sensical values of 1, 2, and 3) to also be used as
19576 escape values indicating the presence of the old format.
f7ef9339 19577
917c78fc
MK
19578 The value returned via bytes_read should be used to increment the
19579 relevant pointer after calling read_initial_length().
c764a876 19580
613e1657
KB
19581 [ Note: read_initial_length() and read_offset() are based on the
19582 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19583 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19584 from:
19585
f7ef9339 19586 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19587
613e1657
KB
19588 This document is only a draft and is subject to change. (So beware.)
19589
f7ef9339 19590 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19591 determined empirically by examining 64-bit ELF files produced by
19592 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19593
19594 - Kevin, July 16, 2002
613e1657
KB
19595 ] */
19596
19597static LONGEST
d521ce57 19598read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19599{
fe1b8b76 19600 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19601
dd373385 19602 if (length == 0xffffffff)
613e1657 19603 {
fe1b8b76 19604 length = bfd_get_64 (abfd, buf + 4);
613e1657 19605 *bytes_read = 12;
613e1657 19606 }
dd373385 19607 else if (length == 0)
f7ef9339 19608 {
dd373385 19609 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19610 length = bfd_get_64 (abfd, buf);
f7ef9339 19611 *bytes_read = 8;
f7ef9339 19612 }
613e1657
KB
19613 else
19614 {
19615 *bytes_read = 4;
613e1657
KB
19616 }
19617
c764a876
DE
19618 return length;
19619}
dd373385 19620
c764a876
DE
19621/* Cover function for read_initial_length.
19622 Returns the length of the object at BUF, and stores the size of the
19623 initial length in *BYTES_READ and stores the size that offsets will be in
19624 *OFFSET_SIZE.
19625 If the initial length size is not equivalent to that specified in
19626 CU_HEADER then issue a complaint.
19627 This is useful when reading non-comp-unit headers. */
dd373385 19628
c764a876 19629static LONGEST
d521ce57 19630read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19631 const struct comp_unit_head *cu_header,
19632 unsigned int *bytes_read,
19633 unsigned int *offset_size)
19634{
19635 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19636
19637 gdb_assert (cu_header->initial_length_size == 4
19638 || cu_header->initial_length_size == 8
19639 || cu_header->initial_length_size == 12);
19640
19641 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19642 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19643
c764a876 19644 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19645 return length;
613e1657
KB
19646}
19647
19648/* Read an offset from the data stream. The size of the offset is
917c78fc 19649 given by cu_header->offset_size. */
613e1657
KB
19650
19651static LONGEST
d521ce57
TT
19652read_offset (bfd *abfd, const gdb_byte *buf,
19653 const struct comp_unit_head *cu_header,
891d2f0b 19654 unsigned int *bytes_read)
c764a876
DE
19655{
19656 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19657
c764a876
DE
19658 *bytes_read = cu_header->offset_size;
19659 return offset;
19660}
19661
19662/* Read an offset from the data stream. */
19663
19664static LONGEST
d521ce57 19665read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19666{
19667 LONGEST retval = 0;
19668
c764a876 19669 switch (offset_size)
613e1657
KB
19670 {
19671 case 4:
fe1b8b76 19672 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19673 break;
19674 case 8:
fe1b8b76 19675 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19676 break;
19677 default:
8e65ff28 19678 internal_error (__FILE__, __LINE__,
c764a876 19679 _("read_offset_1: bad switch [in module %s]"),
659b0389 19680 bfd_get_filename (abfd));
613e1657
KB
19681 }
19682
917c78fc 19683 return retval;
613e1657
KB
19684}
19685
d521ce57
TT
19686static const gdb_byte *
19687read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19688{
19689 /* If the size of a host char is 8 bits, we can return a pointer
19690 to the buffer, otherwise we have to copy the data to a buffer
19691 allocated on the temporary obstack. */
4bdf3d34 19692 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19693 return buf;
c906108c
SS
19694}
19695
d521ce57
TT
19696static const char *
19697read_direct_string (bfd *abfd, const gdb_byte *buf,
19698 unsigned int *bytes_read_ptr)
c906108c
SS
19699{
19700 /* If the size of a host char is 8 bits, we can return a pointer
19701 to the string, otherwise we have to copy the string to a buffer
19702 allocated on the temporary obstack. */
4bdf3d34 19703 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19704 if (*buf == '\0')
19705 {
19706 *bytes_read_ptr = 1;
19707 return NULL;
19708 }
d521ce57
TT
19709 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19710 return (const char *) buf;
4bdf3d34
JJ
19711}
19712
43988095
JK
19713/* Return pointer to string at section SECT offset STR_OFFSET with error
19714 reporting strings FORM_NAME and SECT_NAME. */
19715
d521ce57 19716static const char *
ed2dc618
SM
19717read_indirect_string_at_offset_from (struct objfile *objfile,
19718 bfd *abfd, LONGEST str_offset,
43988095
JK
19719 struct dwarf2_section_info *sect,
19720 const char *form_name,
19721 const char *sect_name)
19722{
ed2dc618 19723 dwarf2_read_section (objfile, sect);
43988095
JK
19724 if (sect->buffer == NULL)
19725 error (_("%s used without %s section [in module %s]"),
19726 form_name, sect_name, bfd_get_filename (abfd));
19727 if (str_offset >= sect->size)
19728 error (_("%s pointing outside of %s section [in module %s]"),
19729 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19730 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19731 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19732 return NULL;
43988095
JK
19733 return (const char *) (sect->buffer + str_offset);
19734}
19735
19736/* Return pointer to string at .debug_str offset STR_OFFSET. */
19737
19738static const char *
ed2dc618
SM
19739read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19740 bfd *abfd, LONGEST str_offset)
43988095 19741{
ed2dc618
SM
19742 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19743 abfd, str_offset,
43988095
JK
19744 &dwarf2_per_objfile->str,
19745 "DW_FORM_strp", ".debug_str");
19746}
19747
19748/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19749
19750static const char *
ed2dc618
SM
19751read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19752 bfd *abfd, LONGEST str_offset)
43988095 19753{
ed2dc618
SM
19754 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19755 abfd, str_offset,
43988095
JK
19756 &dwarf2_per_objfile->line_str,
19757 "DW_FORM_line_strp",
19758 ".debug_line_str");
c906108c
SS
19759}
19760
36586728
TT
19761/* Read a string at offset STR_OFFSET in the .debug_str section from
19762 the .dwz file DWZ. Throw an error if the offset is too large. If
19763 the string consists of a single NUL byte, return NULL; otherwise
19764 return a pointer to the string. */
19765
d521ce57 19766static const char *
ed2dc618
SM
19767read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19768 LONGEST str_offset)
36586728 19769{
ed2dc618 19770 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19771
19772 if (dwz->str.buffer == NULL)
19773 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19774 "section [in module %s]"),
00f93c44 19775 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19776 if (str_offset >= dwz->str.size)
19777 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19778 ".debug_str section [in module %s]"),
00f93c44 19779 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19780 gdb_assert (HOST_CHAR_BIT == 8);
19781 if (dwz->str.buffer[str_offset] == '\0')
19782 return NULL;
d521ce57 19783 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19784}
19785
43988095
JK
19786/* Return pointer to string at .debug_str offset as read from BUF.
19787 BUF is assumed to be in a compilation unit described by CU_HEADER.
19788 Return *BYTES_READ_PTR count of bytes read from BUF. */
19789
d521ce57 19790static const char *
ed2dc618
SM
19791read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19792 const gdb_byte *buf,
cf2c3c16
TT
19793 const struct comp_unit_head *cu_header,
19794 unsigned int *bytes_read_ptr)
19795{
19796 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19797
ed2dc618 19798 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19799}
19800
43988095
JK
19801/* Return pointer to string at .debug_line_str offset as read from BUF.
19802 BUF is assumed to be in a compilation unit described by CU_HEADER.
19803 Return *BYTES_READ_PTR count of bytes read from BUF. */
19804
19805static const char *
ed2dc618
SM
19806read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19807 bfd *abfd, const gdb_byte *buf,
43988095
JK
19808 const struct comp_unit_head *cu_header,
19809 unsigned int *bytes_read_ptr)
19810{
19811 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19812
ed2dc618
SM
19813 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19814 str_offset);
43988095
JK
19815}
19816
19817ULONGEST
d521ce57 19818read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19819 unsigned int *bytes_read_ptr)
c906108c 19820{
12df843f 19821 ULONGEST result;
ce5d95e1 19822 unsigned int num_read;
870f88f7 19823 int shift;
c906108c
SS
19824 unsigned char byte;
19825
19826 result = 0;
19827 shift = 0;
19828 num_read = 0;
c906108c
SS
19829 while (1)
19830 {
fe1b8b76 19831 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19832 buf++;
19833 num_read++;
12df843f 19834 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19835 if ((byte & 128) == 0)
19836 {
19837 break;
19838 }
19839 shift += 7;
19840 }
19841 *bytes_read_ptr = num_read;
19842 return result;
19843}
19844
12df843f 19845static LONGEST
d521ce57
TT
19846read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19847 unsigned int *bytes_read_ptr)
c906108c 19848{
4dd1b460 19849 ULONGEST result;
870f88f7 19850 int shift, num_read;
c906108c
SS
19851 unsigned char byte;
19852
19853 result = 0;
19854 shift = 0;
c906108c 19855 num_read = 0;
c906108c
SS
19856 while (1)
19857 {
fe1b8b76 19858 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19859 buf++;
19860 num_read++;
4dd1b460 19861 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19862 shift += 7;
19863 if ((byte & 128) == 0)
19864 {
19865 break;
19866 }
19867 }
77e0b926 19868 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 19869 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
19870 *bytes_read_ptr = num_read;
19871 return result;
19872}
19873
3019eac3
DE
19874/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19875 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19876 ADDR_SIZE is the size of addresses from the CU header. */
19877
19878static CORE_ADDR
ed2dc618
SM
19879read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19880 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19881{
19882 struct objfile *objfile = dwarf2_per_objfile->objfile;
19883 bfd *abfd = objfile->obfd;
19884 const gdb_byte *info_ptr;
19885
19886 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19887 if (dwarf2_per_objfile->addr.buffer == NULL)
19888 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19889 objfile_name (objfile));
3019eac3
DE
19890 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19891 error (_("DW_FORM_addr_index pointing outside of "
19892 ".debug_addr section [in module %s]"),
4262abfb 19893 objfile_name (objfile));
3019eac3
DE
19894 info_ptr = (dwarf2_per_objfile->addr.buffer
19895 + addr_base + addr_index * addr_size);
19896 if (addr_size == 4)
19897 return bfd_get_32 (abfd, info_ptr);
19898 else
19899 return bfd_get_64 (abfd, info_ptr);
19900}
19901
19902/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19903
19904static CORE_ADDR
19905read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19906{
518817b3
SM
19907 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19908 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19909}
19910
19911/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19912
19913static CORE_ADDR
d521ce57 19914read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19915 unsigned int *bytes_read)
19916{
518817b3 19917 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19918 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19919
19920 return read_addr_index (cu, addr_index);
19921}
19922
19923/* Data structure to pass results from dwarf2_read_addr_index_reader
19924 back to dwarf2_read_addr_index. */
19925
19926struct dwarf2_read_addr_index_data
19927{
19928 ULONGEST addr_base;
19929 int addr_size;
19930};
19931
19932/* die_reader_func for dwarf2_read_addr_index. */
19933
19934static void
19935dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19936 const gdb_byte *info_ptr,
3019eac3
DE
19937 struct die_info *comp_unit_die,
19938 int has_children,
19939 void *data)
19940{
19941 struct dwarf2_cu *cu = reader->cu;
19942 struct dwarf2_read_addr_index_data *aidata =
19943 (struct dwarf2_read_addr_index_data *) data;
19944
19945 aidata->addr_base = cu->addr_base;
19946 aidata->addr_size = cu->header.addr_size;
19947}
19948
19949/* Given an index in .debug_addr, fetch the value.
19950 NOTE: This can be called during dwarf expression evaluation,
19951 long after the debug information has been read, and thus per_cu->cu
19952 may no longer exist. */
19953
19954CORE_ADDR
19955dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19956 unsigned int addr_index)
19957{
ed2dc618 19958 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19959 struct dwarf2_cu *cu = per_cu->cu;
19960 ULONGEST addr_base;
19961 int addr_size;
19962
3019eac3
DE
19963 /* We need addr_base and addr_size.
19964 If we don't have PER_CU->cu, we have to get it.
19965 Nasty, but the alternative is storing the needed info in PER_CU,
19966 which at this point doesn't seem justified: it's not clear how frequently
19967 it would get used and it would increase the size of every PER_CU.
19968 Entry points like dwarf2_per_cu_addr_size do a similar thing
19969 so we're not in uncharted territory here.
19970 Alas we need to be a bit more complicated as addr_base is contained
19971 in the DIE.
19972
19973 We don't need to read the entire CU(/TU).
19974 We just need the header and top level die.
a1b64ce1 19975
3019eac3 19976 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19977 For now we skip this optimization. */
3019eac3
DE
19978
19979 if (cu != NULL)
19980 {
19981 addr_base = cu->addr_base;
19982 addr_size = cu->header.addr_size;
19983 }
19984 else
19985 {
19986 struct dwarf2_read_addr_index_data aidata;
19987
a1b64ce1
DE
19988 /* Note: We can't use init_cutu_and_read_dies_simple here,
19989 we need addr_base. */
58f0c718 19990 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 19991 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19992 addr_base = aidata.addr_base;
19993 addr_size = aidata.addr_size;
19994 }
19995
ed2dc618
SM
19996 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19997 addr_size);
3019eac3
DE
19998}
19999
cf532bd1 20000/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 20001 This is only used by the Fission support. */
3019eac3 20002
d521ce57 20003static const char *
342587c4 20004read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20005{
ed2dc618 20006 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20007 struct dwarf2_per_objfile *dwarf2_per_objfile
20008 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20009 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20010 const char *objf_name = objfile_name (objfile);
3019eac3 20011 bfd *abfd = objfile->obfd;
73869dc2
DE
20012 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20013 struct dwarf2_section_info *str_offsets_section =
20014 &reader->dwo_file->sections.str_offsets;
d521ce57 20015 const gdb_byte *info_ptr;
3019eac3 20016 ULONGEST str_offset;
cf532bd1 20017 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20018
73869dc2
DE
20019 dwarf2_read_section (objfile, str_section);
20020 dwarf2_read_section (objfile, str_offsets_section);
20021 if (str_section->buffer == NULL)
57d63ce2 20022 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20023 " in CU at offset %s [in module %s]"),
20024 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20025 if (str_offsets_section->buffer == NULL)
57d63ce2 20026 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20027 " in CU at offset %s [in module %s]"),
20028 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20029 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20030 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20031 " section in CU at offset %s [in module %s]"),
20032 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20033 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20034 + str_index * cu->header.offset_size);
20035 if (cu->header.offset_size == 4)
20036 str_offset = bfd_get_32 (abfd, info_ptr);
20037 else
20038 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20039 if (str_offset >= str_section->size)
57d63ce2 20040 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20041 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20042 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20043 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20044}
20045
3019eac3
DE
20046/* Return the length of an LEB128 number in BUF. */
20047
20048static int
20049leb128_size (const gdb_byte *buf)
20050{
20051 const gdb_byte *begin = buf;
20052 gdb_byte byte;
20053
20054 while (1)
20055 {
20056 byte = *buf++;
20057 if ((byte & 128) == 0)
20058 return buf - begin;
20059 }
20060}
20061
c906108c 20062static void
e142c38c 20063set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20064{
20065 switch (lang)
20066 {
20067 case DW_LANG_C89:
76bee0cc 20068 case DW_LANG_C99:
0cfd832f 20069 case DW_LANG_C11:
c906108c 20070 case DW_LANG_C:
d1be3247 20071 case DW_LANG_UPC:
e142c38c 20072 cu->language = language_c;
c906108c 20073 break;
9c37b5ae 20074 case DW_LANG_Java:
c906108c 20075 case DW_LANG_C_plus_plus:
0cfd832f
MW
20076 case DW_LANG_C_plus_plus_11:
20077 case DW_LANG_C_plus_plus_14:
e142c38c 20078 cu->language = language_cplus;
c906108c 20079 break;
6aecb9c2
JB
20080 case DW_LANG_D:
20081 cu->language = language_d;
20082 break;
c906108c
SS
20083 case DW_LANG_Fortran77:
20084 case DW_LANG_Fortran90:
b21b22e0 20085 case DW_LANG_Fortran95:
f7de9aab
MW
20086 case DW_LANG_Fortran03:
20087 case DW_LANG_Fortran08:
e142c38c 20088 cu->language = language_fortran;
c906108c 20089 break;
a766d390
DE
20090 case DW_LANG_Go:
20091 cu->language = language_go;
20092 break;
c906108c 20093 case DW_LANG_Mips_Assembler:
e142c38c 20094 cu->language = language_asm;
c906108c
SS
20095 break;
20096 case DW_LANG_Ada83:
8aaf0b47 20097 case DW_LANG_Ada95:
bc5f45f8
JB
20098 cu->language = language_ada;
20099 break;
72019c9c
GM
20100 case DW_LANG_Modula2:
20101 cu->language = language_m2;
20102 break;
fe8e67fd
PM
20103 case DW_LANG_Pascal83:
20104 cu->language = language_pascal;
20105 break;
22566fbd
DJ
20106 case DW_LANG_ObjC:
20107 cu->language = language_objc;
20108 break;
c44af4eb
TT
20109 case DW_LANG_Rust:
20110 case DW_LANG_Rust_old:
20111 cu->language = language_rust;
20112 break;
c906108c
SS
20113 case DW_LANG_Cobol74:
20114 case DW_LANG_Cobol85:
c906108c 20115 default:
e142c38c 20116 cu->language = language_minimal;
c906108c
SS
20117 break;
20118 }
e142c38c 20119 cu->language_defn = language_def (cu->language);
c906108c
SS
20120}
20121
20122/* Return the named attribute or NULL if not there. */
20123
20124static struct attribute *
e142c38c 20125dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20126{
a48e046c 20127 for (;;)
c906108c 20128 {
a48e046c
TT
20129 unsigned int i;
20130 struct attribute *spec = NULL;
20131
20132 for (i = 0; i < die->num_attrs; ++i)
20133 {
20134 if (die->attrs[i].name == name)
20135 return &die->attrs[i];
20136 if (die->attrs[i].name == DW_AT_specification
20137 || die->attrs[i].name == DW_AT_abstract_origin)
20138 spec = &die->attrs[i];
20139 }
20140
20141 if (!spec)
20142 break;
c906108c 20143
f2f0e013 20144 die = follow_die_ref (die, spec, &cu);
f2f0e013 20145 }
c5aa993b 20146
c906108c
SS
20147 return NULL;
20148}
20149
348e048f
DE
20150/* Return the named attribute or NULL if not there,
20151 but do not follow DW_AT_specification, etc.
20152 This is for use in contexts where we're reading .debug_types dies.
20153 Following DW_AT_specification, DW_AT_abstract_origin will take us
20154 back up the chain, and we want to go down. */
20155
20156static struct attribute *
45e58e77 20157dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20158{
20159 unsigned int i;
20160
20161 for (i = 0; i < die->num_attrs; ++i)
20162 if (die->attrs[i].name == name)
20163 return &die->attrs[i];
20164
20165 return NULL;
20166}
20167
7d45c7c3
KB
20168/* Return the string associated with a string-typed attribute, or NULL if it
20169 is either not found or is of an incorrect type. */
20170
20171static const char *
20172dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20173{
20174 struct attribute *attr;
20175 const char *str = NULL;
20176
20177 attr = dwarf2_attr (die, name, cu);
20178
20179 if (attr != NULL)
20180 {
43988095 20181 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20182 || attr->form == DW_FORM_string
cf532bd1 20183 || attr->form == DW_FORM_strx
8fe0f950
AT
20184 || attr->form == DW_FORM_strx1
20185 || attr->form == DW_FORM_strx2
20186 || attr->form == DW_FORM_strx3
20187 || attr->form == DW_FORM_strx4
b3340438 20188 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20189 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20190 str = DW_STRING (attr);
20191 else
b98664d3 20192 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20193 "DIE at %s in module %s"),
20194 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20195 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20196 }
20197
20198 return str;
20199}
20200
a084a2a6
AT
20201/* Return the dwo name or NULL if not present. If present, it is in either
20202 DW_AT_GNU_dwo_name or DW_AT_dwo_name atrribute. */
20203static const char *
20204dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20205{
20206 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20207 if (dwo_name == nullptr)
20208 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20209 return dwo_name;
20210}
20211
05cf31d1
JB
20212/* Return non-zero iff the attribute NAME is defined for the given DIE,
20213 and holds a non-zero value. This function should only be used for
2dc7f7b3 20214 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20215
20216static int
20217dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20218{
20219 struct attribute *attr = dwarf2_attr (die, name, cu);
20220
20221 return (attr && DW_UNSND (attr));
20222}
20223
3ca72b44 20224static int
e142c38c 20225die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20226{
05cf31d1
JB
20227 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20228 which value is non-zero. However, we have to be careful with
20229 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20230 (via dwarf2_flag_true_p) follows this attribute. So we may
20231 end up accidently finding a declaration attribute that belongs
20232 to a different DIE referenced by the specification attribute,
20233 even though the given DIE does not have a declaration attribute. */
20234 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20235 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20236}
20237
63d06c5c 20238/* Return the die giving the specification for DIE, if there is
f2f0e013 20239 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20240 containing the return value on output. If there is no
20241 specification, but there is an abstract origin, that is
20242 returned. */
63d06c5c
DC
20243
20244static struct die_info *
f2f0e013 20245die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20246{
f2f0e013
DJ
20247 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20248 *spec_cu);
63d06c5c 20249
edb3359d
DJ
20250 if (spec_attr == NULL)
20251 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20252
63d06c5c
DC
20253 if (spec_attr == NULL)
20254 return NULL;
20255 else
f2f0e013 20256 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20257}
c906108c 20258
527f3840
JK
20259/* Stub for free_line_header to match void * callback types. */
20260
20261static void
20262free_line_header_voidp (void *arg)
20263{
9a3c8263 20264 struct line_header *lh = (struct line_header *) arg;
527f3840 20265
fff8551c 20266 delete lh;
527f3840
JK
20267}
20268
fff8551c
PA
20269void
20270line_header::add_include_dir (const char *include_dir)
c906108c 20271{
27e0867f 20272 if (dwarf_line_debug >= 2)
fff8551c
PA
20273 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20274 include_dirs.size () + 1, include_dir);
27e0867f 20275
fff8551c 20276 include_dirs.push_back (include_dir);
debd256d 20277}
6e70227d 20278
fff8551c
PA
20279void
20280line_header::add_file_name (const char *name,
ecfb656c 20281 dir_index d_index,
fff8551c
PA
20282 unsigned int mod_time,
20283 unsigned int length)
debd256d 20284{
27e0867f
DE
20285 if (dwarf_line_debug >= 2)
20286 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20287 (unsigned) file_names.size () + 1, name);
27e0867f 20288
ecfb656c 20289 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20290}
6e70227d 20291
83769d0b 20292/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20293
20294static struct dwarf2_section_info *
20295get_debug_line_section (struct dwarf2_cu *cu)
20296{
20297 struct dwarf2_section_info *section;
518817b3
SM
20298 struct dwarf2_per_objfile *dwarf2_per_objfile
20299 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20300
20301 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20302 DWO file. */
20303 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20304 section = &cu->dwo_unit->dwo_file->sections.line;
20305 else if (cu->per_cu->is_dwz)
20306 {
ed2dc618 20307 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20308
20309 section = &dwz->line;
20310 }
20311 else
20312 section = &dwarf2_per_objfile->line;
20313
20314 return section;
20315}
20316
43988095
JK
20317/* Read directory or file name entry format, starting with byte of
20318 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20319 entries count and the entries themselves in the described entry
20320 format. */
20321
20322static void
ed2dc618
SM
20323read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20324 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20325 struct line_header *lh,
20326 const struct comp_unit_head *cu_header,
20327 void (*callback) (struct line_header *lh,
20328 const char *name,
ecfb656c 20329 dir_index d_index,
43988095
JK
20330 unsigned int mod_time,
20331 unsigned int length))
20332{
20333 gdb_byte format_count, formati;
20334 ULONGEST data_count, datai;
20335 const gdb_byte *buf = *bufp;
20336 const gdb_byte *format_header_data;
43988095
JK
20337 unsigned int bytes_read;
20338
20339 format_count = read_1_byte (abfd, buf);
20340 buf += 1;
20341 format_header_data = buf;
20342 for (formati = 0; formati < format_count; formati++)
20343 {
20344 read_unsigned_leb128 (abfd, buf, &bytes_read);
20345 buf += bytes_read;
20346 read_unsigned_leb128 (abfd, buf, &bytes_read);
20347 buf += bytes_read;
20348 }
20349
20350 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20351 buf += bytes_read;
20352 for (datai = 0; datai < data_count; datai++)
20353 {
20354 const gdb_byte *format = format_header_data;
20355 struct file_entry fe;
20356
43988095
JK
20357 for (formati = 0; formati < format_count; formati++)
20358 {
ecfb656c 20359 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20360 format += bytes_read;
43988095 20361
ecfb656c 20362 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20363 format += bytes_read;
ecfb656c
PA
20364
20365 gdb::optional<const char *> string;
20366 gdb::optional<unsigned int> uint;
20367
43988095
JK
20368 switch (form)
20369 {
20370 case DW_FORM_string:
ecfb656c 20371 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20372 buf += bytes_read;
20373 break;
20374
20375 case DW_FORM_line_strp:
ed2dc618
SM
20376 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20377 abfd, buf,
ecfb656c
PA
20378 cu_header,
20379 &bytes_read));
43988095
JK
20380 buf += bytes_read;
20381 break;
20382
20383 case DW_FORM_data1:
ecfb656c 20384 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20385 buf += 1;
20386 break;
20387
20388 case DW_FORM_data2:
ecfb656c 20389 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20390 buf += 2;
20391 break;
20392
20393 case DW_FORM_data4:
ecfb656c 20394 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20395 buf += 4;
20396 break;
20397
20398 case DW_FORM_data8:
ecfb656c 20399 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20400 buf += 8;
20401 break;
20402
20403 case DW_FORM_udata:
ecfb656c 20404 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20405 buf += bytes_read;
20406 break;
20407
20408 case DW_FORM_block:
20409 /* It is valid only for DW_LNCT_timestamp which is ignored by
20410 current GDB. */
20411 break;
20412 }
ecfb656c
PA
20413
20414 switch (content_type)
20415 {
20416 case DW_LNCT_path:
20417 if (string.has_value ())
20418 fe.name = *string;
20419 break;
20420 case DW_LNCT_directory_index:
20421 if (uint.has_value ())
20422 fe.d_index = (dir_index) *uint;
20423 break;
20424 case DW_LNCT_timestamp:
20425 if (uint.has_value ())
20426 fe.mod_time = *uint;
20427 break;
20428 case DW_LNCT_size:
20429 if (uint.has_value ())
20430 fe.length = *uint;
20431 break;
20432 case DW_LNCT_MD5:
20433 break;
20434 default:
b98664d3 20435 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20436 pulongest (content_type));
20437 }
43988095
JK
20438 }
20439
ecfb656c 20440 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20441 }
20442
20443 *bufp = buf;
20444}
20445
debd256d 20446/* Read the statement program header starting at OFFSET in
3019eac3 20447 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20448 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20449 Returns NULL if there is a problem reading the header, e.g., if it
20450 has a version we don't understand.
debd256d
JB
20451
20452 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20453 the returned object point into the dwarf line section buffer,
20454 and must not be freed. */
ae2de4f8 20455
fff8551c 20456static line_header_up
9c541725 20457dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20458{
d521ce57 20459 const gdb_byte *line_ptr;
c764a876 20460 unsigned int bytes_read, offset_size;
debd256d 20461 int i;
d521ce57 20462 const char *cur_dir, *cur_file;
3019eac3
DE
20463 struct dwarf2_section_info *section;
20464 bfd *abfd;
518817b3
SM
20465 struct dwarf2_per_objfile *dwarf2_per_objfile
20466 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20467
36586728 20468 section = get_debug_line_section (cu);
3019eac3
DE
20469 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20470 if (section->buffer == NULL)
debd256d 20471 {
3019eac3 20472 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20473 complaint (_("missing .debug_line.dwo section"));
3019eac3 20474 else
b98664d3 20475 complaint (_("missing .debug_line section"));
debd256d
JB
20476 return 0;
20477 }
20478
fceca515
DE
20479 /* We can't do this until we know the section is non-empty.
20480 Only then do we know we have such a section. */
a32a8923 20481 abfd = get_section_bfd_owner (section);
fceca515 20482
a738430d
MK
20483 /* Make sure that at least there's room for the total_length field.
20484 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20485 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20486 {
4d3c2250 20487 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20488 return 0;
20489 }
20490
fff8551c 20491 line_header_up lh (new line_header ());
debd256d 20492
9c541725 20493 lh->sect_off = sect_off;
527f3840
JK
20494 lh->offset_in_dwz = cu->per_cu->is_dwz;
20495
9c541725 20496 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20497
a738430d 20498 /* Read in the header. */
6e70227d 20499 lh->total_length =
c764a876
DE
20500 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20501 &bytes_read, &offset_size);
debd256d 20502 line_ptr += bytes_read;
3019eac3 20503 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20504 {
4d3c2250 20505 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20506 return 0;
20507 }
20508 lh->statement_program_end = line_ptr + lh->total_length;
20509 lh->version = read_2_bytes (abfd, line_ptr);
20510 line_ptr += 2;
43988095 20511 if (lh->version > 5)
cd366ee8
DE
20512 {
20513 /* This is a version we don't understand. The format could have
20514 changed in ways we don't handle properly so just punt. */
b98664d3 20515 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20516 return NULL;
20517 }
43988095
JK
20518 if (lh->version >= 5)
20519 {
20520 gdb_byte segment_selector_size;
20521
20522 /* Skip address size. */
20523 read_1_byte (abfd, line_ptr);
20524 line_ptr += 1;
20525
20526 segment_selector_size = read_1_byte (abfd, line_ptr);
20527 line_ptr += 1;
20528 if (segment_selector_size != 0)
20529 {
b98664d3 20530 complaint (_("unsupported segment selector size %u "
43988095
JK
20531 "in .debug_line section"),
20532 segment_selector_size);
20533 return NULL;
20534 }
20535 }
c764a876
DE
20536 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20537 line_ptr += offset_size;
debd256d
JB
20538 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20539 line_ptr += 1;
2dc7f7b3
TT
20540 if (lh->version >= 4)
20541 {
20542 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20543 line_ptr += 1;
20544 }
20545 else
20546 lh->maximum_ops_per_instruction = 1;
20547
20548 if (lh->maximum_ops_per_instruction == 0)
20549 {
20550 lh->maximum_ops_per_instruction = 1;
b98664d3 20551 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20552 "in `.debug_line' section"));
2dc7f7b3
TT
20553 }
20554
debd256d
JB
20555 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20556 line_ptr += 1;
20557 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20558 line_ptr += 1;
20559 lh->line_range = read_1_byte (abfd, line_ptr);
20560 line_ptr += 1;
20561 lh->opcode_base = read_1_byte (abfd, line_ptr);
20562 line_ptr += 1;
fff8551c 20563 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20564
20565 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20566 for (i = 1; i < lh->opcode_base; ++i)
20567 {
20568 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20569 line_ptr += 1;
20570 }
20571
43988095 20572 if (lh->version >= 5)
debd256d 20573 {
43988095 20574 /* Read directory table. */
ed2dc618
SM
20575 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20576 &cu->header,
b926417a 20577 [] (struct line_header *header, const char *name,
ecfb656c 20578 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20579 unsigned int length)
20580 {
b926417a 20581 header->add_include_dir (name);
fff8551c 20582 });
debd256d 20583
43988095 20584 /* Read file name table. */
ed2dc618
SM
20585 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20586 &cu->header,
b926417a 20587 [] (struct line_header *header, const char *name,
ecfb656c 20588 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20589 unsigned int length)
20590 {
b926417a 20591 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20592 });
43988095
JK
20593 }
20594 else
debd256d 20595 {
43988095
JK
20596 /* Read directory table. */
20597 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20598 {
20599 line_ptr += bytes_read;
fff8551c 20600 lh->add_include_dir (cur_dir);
43988095 20601 }
debd256d
JB
20602 line_ptr += bytes_read;
20603
43988095
JK
20604 /* Read file name table. */
20605 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20606 {
ecfb656c
PA
20607 unsigned int mod_time, length;
20608 dir_index d_index;
43988095
JK
20609
20610 line_ptr += bytes_read;
ecfb656c 20611 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20612 line_ptr += bytes_read;
20613 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20614 line_ptr += bytes_read;
20615 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20616 line_ptr += bytes_read;
20617
ecfb656c 20618 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20619 }
20620 line_ptr += bytes_read;
debd256d 20621 }
6e70227d 20622 lh->statement_program_start = line_ptr;
debd256d 20623
3019eac3 20624 if (line_ptr > (section->buffer + section->size))
b98664d3 20625 complaint (_("line number info header doesn't "
3e43a32a 20626 "fit in `.debug_line' section"));
debd256d 20627
debd256d
JB
20628 return lh;
20629}
c906108c 20630
c6da4cef
DE
20631/* Subroutine of dwarf_decode_lines to simplify it.
20632 Return the file name of the psymtab for included file FILE_INDEX
20633 in line header LH of PST.
20634 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20635 If space for the result is malloc'd, *NAME_HOLDER will be set.
20636 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20637
d521ce57 20638static const char *
c6da4cef
DE
20639psymtab_include_file_name (const struct line_header *lh, int file_index,
20640 const struct partial_symtab *pst,
c89b44cd
TT
20641 const char *comp_dir,
20642 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20643{
8c43009f 20644 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20645 const char *include_name = fe.name;
20646 const char *include_name_to_compare = include_name;
72b9f47f 20647 const char *pst_filename;
c6da4cef
DE
20648 int file_is_pst;
20649
8c43009f 20650 const char *dir_name = fe.include_dir (lh);
c6da4cef 20651
c89b44cd 20652 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20653 if (!IS_ABSOLUTE_PATH (include_name)
20654 && (dir_name != NULL || comp_dir != NULL))
20655 {
20656 /* Avoid creating a duplicate psymtab for PST.
20657 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20658 Before we do the comparison, however, we need to account
20659 for DIR_NAME and COMP_DIR.
20660 First prepend dir_name (if non-NULL). If we still don't
20661 have an absolute path prepend comp_dir (if non-NULL).
20662 However, the directory we record in the include-file's
20663 psymtab does not contain COMP_DIR (to match the
20664 corresponding symtab(s)).
20665
20666 Example:
20667
20668 bash$ cd /tmp
20669 bash$ gcc -g ./hello.c
20670 include_name = "hello.c"
20671 dir_name = "."
20672 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20673 DW_AT_name = "./hello.c"
20674
20675 */
c6da4cef
DE
20676
20677 if (dir_name != NULL)
20678 {
c89b44cd
TT
20679 name_holder->reset (concat (dir_name, SLASH_STRING,
20680 include_name, (char *) NULL));
20681 include_name = name_holder->get ();
c6da4cef 20682 include_name_to_compare = include_name;
c6da4cef
DE
20683 }
20684 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20685 {
c89b44cd
TT
20686 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20687 include_name, (char *) NULL));
20688 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20689 }
20690 }
20691
20692 pst_filename = pst->filename;
c89b44cd 20693 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20694 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20695 {
c89b44cd
TT
20696 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20697 pst_filename, (char *) NULL));
20698 pst_filename = copied_name.get ();
c6da4cef
DE
20699 }
20700
1e3fad37 20701 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20702
c6da4cef
DE
20703 if (file_is_pst)
20704 return NULL;
20705 return include_name;
20706}
20707
d9b3de22
DE
20708/* State machine to track the state of the line number program. */
20709
6f77053d 20710class lnp_state_machine
d9b3de22 20711{
6f77053d
PA
20712public:
20713 /* Initialize a machine state for the start of a line number
20714 program. */
804d2729
TT
20715 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20716 bool record_lines_p);
6f77053d 20717
8c43009f
PA
20718 file_entry *current_file ()
20719 {
20720 /* lh->file_names is 0-based, but the file name numbers in the
20721 statement program are 1-based. */
6f77053d
PA
20722 return m_line_header->file_name_at (m_file);
20723 }
20724
20725 /* Record the line in the state machine. END_SEQUENCE is true if
20726 we're processing the end of a sequence. */
20727 void record_line (bool end_sequence);
20728
7ab6656f
OJ
20729 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20730 nop-out rest of the lines in this sequence. */
6f77053d
PA
20731 void check_line_address (struct dwarf2_cu *cu,
20732 const gdb_byte *line_ptr,
7ab6656f 20733 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20734
20735 void handle_set_discriminator (unsigned int discriminator)
20736 {
20737 m_discriminator = discriminator;
20738 m_line_has_non_zero_discriminator |= discriminator != 0;
20739 }
20740
20741 /* Handle DW_LNE_set_address. */
20742 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20743 {
20744 m_op_index = 0;
20745 address += baseaddr;
20746 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20747 }
20748
20749 /* Handle DW_LNS_advance_pc. */
20750 void handle_advance_pc (CORE_ADDR adjust);
20751
20752 /* Handle a special opcode. */
20753 void handle_special_opcode (unsigned char op_code);
20754
20755 /* Handle DW_LNS_advance_line. */
20756 void handle_advance_line (int line_delta)
20757 {
20758 advance_line (line_delta);
20759 }
20760
20761 /* Handle DW_LNS_set_file. */
20762 void handle_set_file (file_name_index file);
20763
20764 /* Handle DW_LNS_negate_stmt. */
20765 void handle_negate_stmt ()
20766 {
20767 m_is_stmt = !m_is_stmt;
20768 }
20769
20770 /* Handle DW_LNS_const_add_pc. */
20771 void handle_const_add_pc ();
20772
20773 /* Handle DW_LNS_fixed_advance_pc. */
20774 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20775 {
20776 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20777 m_op_index = 0;
20778 }
20779
20780 /* Handle DW_LNS_copy. */
20781 void handle_copy ()
20782 {
20783 record_line (false);
20784 m_discriminator = 0;
20785 }
20786
20787 /* Handle DW_LNE_end_sequence. */
20788 void handle_end_sequence ()
20789 {
804d2729 20790 m_currently_recording_lines = true;
6f77053d
PA
20791 }
20792
20793private:
20794 /* Advance the line by LINE_DELTA. */
20795 void advance_line (int line_delta)
20796 {
20797 m_line += line_delta;
20798
20799 if (line_delta != 0)
20800 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20801 }
20802
804d2729
TT
20803 struct dwarf2_cu *m_cu;
20804
6f77053d
PA
20805 gdbarch *m_gdbarch;
20806
20807 /* True if we're recording lines.
20808 Otherwise we're building partial symtabs and are just interested in
20809 finding include files mentioned by the line number program. */
20810 bool m_record_lines_p;
20811
8c43009f 20812 /* The line number header. */
6f77053d 20813 line_header *m_line_header;
8c43009f 20814
6f77053d
PA
20815 /* These are part of the standard DWARF line number state machine,
20816 and initialized according to the DWARF spec. */
d9b3de22 20817
6f77053d 20818 unsigned char m_op_index = 0;
8c43009f 20819 /* The line table index (1-based) of the current file. */
6f77053d
PA
20820 file_name_index m_file = (file_name_index) 1;
20821 unsigned int m_line = 1;
20822
20823 /* These are initialized in the constructor. */
20824
20825 CORE_ADDR m_address;
20826 bool m_is_stmt;
20827 unsigned int m_discriminator;
d9b3de22
DE
20828
20829 /* Additional bits of state we need to track. */
20830
20831 /* The last file that we called dwarf2_start_subfile for.
20832 This is only used for TLLs. */
6f77053d 20833 unsigned int m_last_file = 0;
d9b3de22 20834 /* The last file a line number was recorded for. */
6f77053d 20835 struct subfile *m_last_subfile = NULL;
d9b3de22 20836
804d2729
TT
20837 /* When true, record the lines we decode. */
20838 bool m_currently_recording_lines = false;
d9b3de22
DE
20839
20840 /* The last line number that was recorded, used to coalesce
20841 consecutive entries for the same line. This can happen, for
20842 example, when discriminators are present. PR 17276. */
6f77053d
PA
20843 unsigned int m_last_line = 0;
20844 bool m_line_has_non_zero_discriminator = false;
8c43009f 20845};
d9b3de22 20846
6f77053d
PA
20847void
20848lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20849{
20850 CORE_ADDR addr_adj = (((m_op_index + adjust)
20851 / m_line_header->maximum_ops_per_instruction)
20852 * m_line_header->minimum_instruction_length);
20853 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20854 m_op_index = ((m_op_index + adjust)
20855 % m_line_header->maximum_ops_per_instruction);
20856}
d9b3de22 20857
6f77053d
PA
20858void
20859lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20860{
6f77053d
PA
20861 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20862 CORE_ADDR addr_adj = (((m_op_index
20863 + (adj_opcode / m_line_header->line_range))
20864 / m_line_header->maximum_ops_per_instruction)
20865 * m_line_header->minimum_instruction_length);
20866 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20867 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20868 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20869
6f77053d
PA
20870 int line_delta = (m_line_header->line_base
20871 + (adj_opcode % m_line_header->line_range));
20872 advance_line (line_delta);
20873 record_line (false);
20874 m_discriminator = 0;
20875}
d9b3de22 20876
6f77053d
PA
20877void
20878lnp_state_machine::handle_set_file (file_name_index file)
20879{
20880 m_file = file;
20881
20882 const file_entry *fe = current_file ();
20883 if (fe == NULL)
20884 dwarf2_debug_line_missing_file_complaint ();
20885 else if (m_record_lines_p)
20886 {
20887 const char *dir = fe->include_dir (m_line_header);
20888
c24bdb02 20889 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20890 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20891 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20892 }
20893}
20894
20895void
20896lnp_state_machine::handle_const_add_pc ()
20897{
20898 CORE_ADDR adjust
20899 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20900
20901 CORE_ADDR addr_adj
20902 = (((m_op_index + adjust)
20903 / m_line_header->maximum_ops_per_instruction)
20904 * m_line_header->minimum_instruction_length);
20905
20906 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20907 m_op_index = ((m_op_index + adjust)
20908 % m_line_header->maximum_ops_per_instruction);
20909}
d9b3de22 20910
a05a36a5
DE
20911/* Return non-zero if we should add LINE to the line number table.
20912 LINE is the line to add, LAST_LINE is the last line that was added,
20913 LAST_SUBFILE is the subfile for LAST_LINE.
20914 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20915 had a non-zero discriminator.
20916
20917 We have to be careful in the presence of discriminators.
20918 E.g., for this line:
20919
20920 for (i = 0; i < 100000; i++);
20921
20922 clang can emit four line number entries for that one line,
20923 each with a different discriminator.
20924 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20925
20926 However, we want gdb to coalesce all four entries into one.
20927 Otherwise the user could stepi into the middle of the line and
20928 gdb would get confused about whether the pc really was in the
20929 middle of the line.
20930
20931 Things are further complicated by the fact that two consecutive
20932 line number entries for the same line is a heuristic used by gcc
20933 to denote the end of the prologue. So we can't just discard duplicate
20934 entries, we have to be selective about it. The heuristic we use is
20935 that we only collapse consecutive entries for the same line if at least
20936 one of those entries has a non-zero discriminator. PR 17276.
20937
20938 Note: Addresses in the line number state machine can never go backwards
20939 within one sequence, thus this coalescing is ok. */
20940
20941static int
804d2729
TT
20942dwarf_record_line_p (struct dwarf2_cu *cu,
20943 unsigned int line, unsigned int last_line,
a05a36a5
DE
20944 int line_has_non_zero_discriminator,
20945 struct subfile *last_subfile)
20946{
c24bdb02 20947 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20948 return 1;
20949 if (line != last_line)
20950 return 1;
20951 /* Same line for the same file that we've seen already.
20952 As a last check, for pr 17276, only record the line if the line
20953 has never had a non-zero discriminator. */
20954 if (!line_has_non_zero_discriminator)
20955 return 1;
20956 return 0;
20957}
20958
804d2729
TT
20959/* Use the CU's builder to record line number LINE beginning at
20960 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20961
20962static void
d9b3de22
DE
20963dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20964 unsigned int line, CORE_ADDR address,
804d2729 20965 struct dwarf2_cu *cu)
252a6764
DE
20966{
20967 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20968
27e0867f
DE
20969 if (dwarf_line_debug)
20970 {
20971 fprintf_unfiltered (gdb_stdlog,
20972 "Recording line %u, file %s, address %s\n",
20973 line, lbasename (subfile->name),
20974 paddress (gdbarch, address));
20975 }
20976
804d2729 20977 if (cu != nullptr)
c24bdb02 20978 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
20979}
20980
20981/* Subroutine of dwarf_decode_lines_1 to simplify it.
20982 Mark the end of a set of line number records.
d9b3de22 20983 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20984 If SUBFILE is NULL the request is ignored. */
20985
20986static void
20987dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20988 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20989{
27e0867f
DE
20990 if (subfile == NULL)
20991 return;
20992
20993 if (dwarf_line_debug)
20994 {
20995 fprintf_unfiltered (gdb_stdlog,
20996 "Finishing current line, file %s, address %s\n",
20997 lbasename (subfile->name),
20998 paddress (gdbarch, address));
20999 }
21000
804d2729 21001 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21002}
21003
6f77053d
PA
21004void
21005lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21006{
d9b3de22
DE
21007 if (dwarf_line_debug)
21008 {
21009 fprintf_unfiltered (gdb_stdlog,
21010 "Processing actual line %u: file %u,"
21011 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
21012 m_line, to_underlying (m_file),
21013 paddress (m_gdbarch, m_address),
21014 m_is_stmt, m_discriminator);
d9b3de22
DE
21015 }
21016
6f77053d 21017 file_entry *fe = current_file ();
8c43009f
PA
21018
21019 if (fe == NULL)
d9b3de22
DE
21020 dwarf2_debug_line_missing_file_complaint ();
21021 /* For now we ignore lines not starting on an instruction boundary.
21022 But not when processing end_sequence for compatibility with the
21023 previous version of the code. */
6f77053d 21024 else if (m_op_index == 0 || end_sequence)
d9b3de22 21025 {
8c43009f 21026 fe->included_p = 1;
c258c396 21027 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21028 {
c24bdb02 21029 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21030 || end_sequence)
d9b3de22 21031 {
804d2729
TT
21032 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21033 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21034 }
21035
21036 if (!end_sequence)
21037 {
804d2729 21038 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21039 m_line_has_non_zero_discriminator,
21040 m_last_subfile))
d9b3de22 21041 {
c24bdb02 21042 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21043 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21044 builder->get_current_subfile (),
6f77053d 21045 m_line, m_address,
804d2729 21046 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21047 }
c24bdb02 21048 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21049 m_last_line = m_line;
d9b3de22
DE
21050 }
21051 }
21052 }
21053}
21054
804d2729
TT
21055lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21056 line_header *lh, bool record_lines_p)
d9b3de22 21057{
804d2729 21058 m_cu = cu;
6f77053d
PA
21059 m_gdbarch = arch;
21060 m_record_lines_p = record_lines_p;
21061 m_line_header = lh;
d9b3de22 21062
804d2729 21063 m_currently_recording_lines = true;
d9b3de22 21064
d9b3de22
DE
21065 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21066 was a line entry for it so that the backend has a chance to adjust it
21067 and also record it in case it needs it. This is currently used by MIPS
21068 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21069 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21070 m_is_stmt = lh->default_is_stmt;
21071 m_discriminator = 0;
252a6764
DE
21072}
21073
6f77053d
PA
21074void
21075lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21076 const gdb_byte *line_ptr,
7ab6656f 21077 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21078{
7ab6656f
OJ
21079 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21080 the pc range of the CU. However, we restrict the test to only ADDRESS
21081 values of zero to preserve GDB's previous behaviour which is to handle
21082 the specific case of a function being GC'd by the linker. */
924c2928 21083
7ab6656f 21084 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21085 {
21086 /* This line table is for a function which has been
21087 GCd by the linker. Ignore it. PR gdb/12528 */
21088
518817b3 21089 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21090 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21091
b98664d3 21092 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21093 line_offset, objfile_name (objfile));
804d2729
TT
21094 m_currently_recording_lines = false;
21095 /* Note: m_currently_recording_lines is left as false until we see
21096 DW_LNE_end_sequence. */
924c2928
DE
21097 }
21098}
21099
f3f5162e 21100/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21101 Process the line number information in LH.
21102 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21103 program in order to set included_p for every referenced header. */
debd256d 21104
c906108c 21105static void
43f3e411
DE
21106dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21107 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21108{
d521ce57
TT
21109 const gdb_byte *line_ptr, *extended_end;
21110 const gdb_byte *line_end;
a8c50c1f 21111 unsigned int bytes_read, extended_len;
699ca60a 21112 unsigned char op_code, extended_op;
e142c38c 21113 CORE_ADDR baseaddr;
518817b3 21114 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21115 bfd *abfd = objfile->obfd;
fbf65064 21116 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21117 /* True if we're recording line info (as opposed to building partial
21118 symtabs and just interested in finding include files mentioned by
21119 the line number program). */
21120 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21121
21122 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21123
debd256d
JB
21124 line_ptr = lh->statement_program_start;
21125 line_end = lh->statement_program_end;
c906108c
SS
21126
21127 /* Read the statement sequences until there's nothing left. */
21128 while (line_ptr < line_end)
21129 {
6f77053d
PA
21130 /* The DWARF line number program state machine. Reset the state
21131 machine at the start of each sequence. */
804d2729 21132 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21133 bool end_sequence = false;
d9b3de22 21134
8c43009f 21135 if (record_lines_p)
c906108c 21136 {
8c43009f
PA
21137 /* Start a subfile for the current file of the state
21138 machine. */
21139 const file_entry *fe = state_machine.current_file ();
21140
21141 if (fe != NULL)
804d2729 21142 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21143 }
21144
a738430d 21145 /* Decode the table. */
d9b3de22 21146 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21147 {
21148 op_code = read_1_byte (abfd, line_ptr);
21149 line_ptr += 1;
9aa1fe7e 21150
debd256d 21151 if (op_code >= lh->opcode_base)
6e70227d 21152 {
8e07a239 21153 /* Special opcode. */
6f77053d 21154 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21155 }
21156 else switch (op_code)
c906108c
SS
21157 {
21158 case DW_LNS_extended_op:
3e43a32a
MS
21159 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21160 &bytes_read);
473b7be6 21161 line_ptr += bytes_read;
a8c50c1f 21162 extended_end = line_ptr + extended_len;
c906108c
SS
21163 extended_op = read_1_byte (abfd, line_ptr);
21164 line_ptr += 1;
21165 switch (extended_op)
21166 {
21167 case DW_LNE_end_sequence:
6f77053d
PA
21168 state_machine.handle_end_sequence ();
21169 end_sequence = true;
c906108c
SS
21170 break;
21171 case DW_LNE_set_address:
d9b3de22
DE
21172 {
21173 CORE_ADDR address
21174 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21175 line_ptr += bytes_read;
6f77053d
PA
21176
21177 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21178 lowpc - baseaddr, address);
6f77053d 21179 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21180 }
c906108c
SS
21181 break;
21182 case DW_LNE_define_file:
debd256d 21183 {
d521ce57 21184 const char *cur_file;
ecfb656c
PA
21185 unsigned int mod_time, length;
21186 dir_index dindex;
6e70227d 21187
3e43a32a
MS
21188 cur_file = read_direct_string (abfd, line_ptr,
21189 &bytes_read);
debd256d 21190 line_ptr += bytes_read;
ecfb656c 21191 dindex = (dir_index)
debd256d
JB
21192 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21193 line_ptr += bytes_read;
21194 mod_time =
21195 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21196 line_ptr += bytes_read;
21197 length =
21198 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21199 line_ptr += bytes_read;
ecfb656c 21200 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21201 }
c906108c 21202 break;
d0c6ba3d 21203 case DW_LNE_set_discriminator:
6f77053d
PA
21204 {
21205 /* The discriminator is not interesting to the
21206 debugger; just ignore it. We still need to
21207 check its value though:
21208 if there are consecutive entries for the same
21209 (non-prologue) line we want to coalesce them.
21210 PR 17276. */
21211 unsigned int discr
21212 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21213 line_ptr += bytes_read;
21214
21215 state_machine.handle_set_discriminator (discr);
21216 }
d0c6ba3d 21217 break;
c906108c 21218 default:
b98664d3 21219 complaint (_("mangled .debug_line section"));
debd256d 21220 return;
c906108c 21221 }
a8c50c1f
DJ
21222 /* Make sure that we parsed the extended op correctly. If e.g.
21223 we expected a different address size than the producer used,
21224 we may have read the wrong number of bytes. */
21225 if (line_ptr != extended_end)
21226 {
b98664d3 21227 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21228 return;
21229 }
c906108c
SS
21230 break;
21231 case DW_LNS_copy:
6f77053d 21232 state_machine.handle_copy ();
c906108c
SS
21233 break;
21234 case DW_LNS_advance_pc:
2dc7f7b3
TT
21235 {
21236 CORE_ADDR adjust
21237 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21238 line_ptr += bytes_read;
6f77053d
PA
21239
21240 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21241 }
c906108c
SS
21242 break;
21243 case DW_LNS_advance_line:
a05a36a5
DE
21244 {
21245 int line_delta
21246 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21247 line_ptr += bytes_read;
6f77053d
PA
21248
21249 state_machine.handle_advance_line (line_delta);
a05a36a5 21250 }
c906108c
SS
21251 break;
21252 case DW_LNS_set_file:
d9b3de22 21253 {
6f77053d 21254 file_name_index file
ecfb656c
PA
21255 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21256 &bytes_read);
d9b3de22 21257 line_ptr += bytes_read;
8c43009f 21258
6f77053d 21259 state_machine.handle_set_file (file);
d9b3de22 21260 }
c906108c
SS
21261 break;
21262 case DW_LNS_set_column:
0ad93d4f 21263 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21264 line_ptr += bytes_read;
21265 break;
21266 case DW_LNS_negate_stmt:
6f77053d 21267 state_machine.handle_negate_stmt ();
c906108c
SS
21268 break;
21269 case DW_LNS_set_basic_block:
c906108c 21270 break;
c2c6d25f
JM
21271 /* Add to the address register of the state machine the
21272 address increment value corresponding to special opcode
a738430d
MK
21273 255. I.e., this value is scaled by the minimum
21274 instruction length since special opcode 255 would have
b021a221 21275 scaled the increment. */
c906108c 21276 case DW_LNS_const_add_pc:
6f77053d 21277 state_machine.handle_const_add_pc ();
c906108c
SS
21278 break;
21279 case DW_LNS_fixed_advance_pc:
3e29f34a 21280 {
6f77053d 21281 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21282 line_ptr += 2;
6f77053d
PA
21283
21284 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21285 }
c906108c 21286 break;
9aa1fe7e 21287 default:
a738430d
MK
21288 {
21289 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21290 int i;
a738430d 21291
debd256d 21292 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21293 {
21294 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21295 line_ptr += bytes_read;
21296 }
21297 }
c906108c
SS
21298 }
21299 }
d9b3de22
DE
21300
21301 if (!end_sequence)
21302 dwarf2_debug_line_missing_end_sequence_complaint ();
21303
21304 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21305 in which case we still finish recording the last line). */
6f77053d 21306 state_machine.record_line (true);
c906108c 21307 }
f3f5162e
DE
21308}
21309
21310/* Decode the Line Number Program (LNP) for the given line_header
21311 structure and CU. The actual information extracted and the type
21312 of structures created from the LNP depends on the value of PST.
21313
21314 1. If PST is NULL, then this procedure uses the data from the program
21315 to create all necessary symbol tables, and their linetables.
21316
21317 2. If PST is not NULL, this procedure reads the program to determine
21318 the list of files included by the unit represented by PST, and
21319 builds all the associated partial symbol tables.
21320
21321 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21322 It is used for relative paths in the line table.
21323 NOTE: When processing partial symtabs (pst != NULL),
21324 comp_dir == pst->dirname.
21325
21326 NOTE: It is important that psymtabs have the same file name (via strcmp)
21327 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21328 symtab we don't use it in the name of the psymtabs we create.
21329 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21330 A good testcase for this is mb-inline.exp.
21331
527f3840
JK
21332 LOWPC is the lowest address in CU (or 0 if not known).
21333
21334 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21335 for its PC<->lines mapping information. Otherwise only the filename
21336 table is read in. */
f3f5162e
DE
21337
21338static void
21339dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21340 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21341 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21342{
518817b3 21343 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21344 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21345
527f3840
JK
21346 if (decode_mapping)
21347 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21348
21349 if (decode_for_pst_p)
21350 {
21351 int file_index;
21352
21353 /* Now that we're done scanning the Line Header Program, we can
21354 create the psymtab of each included file. */
fff8551c 21355 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21356 if (lh->file_names[file_index].included_p == 1)
21357 {
c89b44cd 21358 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21359 const char *include_name =
c89b44cd
TT
21360 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21361 &name_holder);
c6da4cef 21362 if (include_name != NULL)
aaa75496
JB
21363 dwarf2_create_include_psymtab (include_name, pst, objfile);
21364 }
21365 }
cb1df416
DJ
21366 else
21367 {
21368 /* Make sure a symtab is created for every file, even files
21369 which contain only variables (i.e. no code with associated
21370 line numbers). */
c24bdb02
KS
21371 buildsym_compunit *builder = cu->get_builder ();
21372 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21373 int i;
cb1df416 21374
fff8551c 21375 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21376 {
8c43009f 21377 file_entry &fe = lh->file_names[i];
9a619af0 21378
804d2729 21379 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
cb1df416 21380
c24bdb02 21381 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21382 {
c24bdb02 21383 builder->get_current_subfile ()->symtab
804d2729 21384 = allocate_symtab (cust,
c24bdb02 21385 builder->get_current_subfile ()->name);
43f3e411 21386 }
c24bdb02 21387 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21388 }
21389 }
c906108c
SS
21390}
21391
21392/* Start a subfile for DWARF. FILENAME is the name of the file and
21393 DIRNAME the name of the source directory which contains FILENAME
4d663531 21394 or NULL if not known.
c906108c
SS
21395 This routine tries to keep line numbers from identical absolute and
21396 relative file names in a common subfile.
21397
21398 Using the `list' example from the GDB testsuite, which resides in
21399 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21400 of /srcdir/list0.c yields the following debugging information for list0.c:
21401
c5aa993b 21402 DW_AT_name: /srcdir/list0.c
4d663531 21403 DW_AT_comp_dir: /compdir
357e46e7 21404 files.files[0].name: list0.h
c5aa993b 21405 files.files[0].dir: /srcdir
357e46e7 21406 files.files[1].name: list0.c
c5aa993b 21407 files.files[1].dir: /srcdir
c906108c
SS
21408
21409 The line number information for list0.c has to end up in a single
4f1520fb
FR
21410 subfile, so that `break /srcdir/list0.c:1' works as expected.
21411 start_subfile will ensure that this happens provided that we pass the
21412 concatenation of files.files[1].dir and files.files[1].name as the
21413 subfile's name. */
c906108c
SS
21414
21415static void
804d2729
TT
21416dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21417 const char *dirname)
c906108c 21418{
d521ce57 21419 char *copy = NULL;
4f1520fb 21420
4d663531 21421 /* In order not to lose the line information directory,
4f1520fb
FR
21422 we concatenate it to the filename when it makes sense.
21423 Note that the Dwarf3 standard says (speaking of filenames in line
21424 information): ``The directory index is ignored for file names
21425 that represent full path names''. Thus ignoring dirname in the
21426 `else' branch below isn't an issue. */
c906108c 21427
d5166ae1 21428 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21429 {
21430 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21431 filename = copy;
21432 }
c906108c 21433
c24bdb02 21434 cu->get_builder ()->start_subfile (filename);
4f1520fb 21435
d521ce57
TT
21436 if (copy != NULL)
21437 xfree (copy);
c906108c
SS
21438}
21439
804d2729
TT
21440/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21441 buildsym_compunit constructor. */
f4dc4d17 21442
c24bdb02
KS
21443struct compunit_symtab *
21444dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21445 CORE_ADDR low_pc)
f4dc4d17 21446{
c24bdb02 21447 gdb_assert (m_builder == nullptr);
43f3e411 21448
c24bdb02
KS
21449 m_builder.reset (new struct buildsym_compunit
21450 (per_cu->dwarf2_per_objfile->objfile,
21451 name, comp_dir, language, low_pc));
93b8bea4 21452
c24bdb02 21453 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21454
c24bdb02
KS
21455 get_builder ()->record_debugformat ("DWARF 2");
21456 get_builder ()->record_producer (producer);
f4dc4d17 21457
c24bdb02 21458 processing_has_namespace_info = false;
43f3e411 21459
c24bdb02 21460 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21461}
21462
4c2df51b
DJ
21463static void
21464var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21465 struct dwarf2_cu *cu)
4c2df51b 21466{
518817b3 21467 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21468 struct comp_unit_head *cu_header = &cu->header;
21469
4c2df51b
DJ
21470 /* NOTE drow/2003-01-30: There used to be a comment and some special
21471 code here to turn a symbol with DW_AT_external and a
21472 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21473 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21474 with some versions of binutils) where shared libraries could have
21475 relocations against symbols in their debug information - the
21476 minimal symbol would have the right address, but the debug info
21477 would not. It's no longer necessary, because we will explicitly
21478 apply relocations when we read in the debug information now. */
21479
21480 /* A DW_AT_location attribute with no contents indicates that a
21481 variable has been optimized away. */
21482 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21483 {
f1e6e072 21484 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21485 return;
21486 }
21487
21488 /* Handle one degenerate form of location expression specially, to
21489 preserve GDB's previous behavior when section offsets are
336d760d
AT
21490 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21491 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21492
21493 if (attr_form_is_block (attr)
3019eac3
DE
21494 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21495 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21496 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21497 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21498 && (DW_BLOCK (attr)->size
21499 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21500 {
891d2f0b 21501 unsigned int dummy;
4c2df51b 21502
3019eac3 21503 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21504 SET_SYMBOL_VALUE_ADDRESS (sym,
21505 read_address (objfile->obfd,
21506 DW_BLOCK (attr)->data + 1,
21507 cu, &dummy));
3019eac3 21508 else
38583298
TT
21509 SET_SYMBOL_VALUE_ADDRESS
21510 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21511 &dummy));
f1e6e072 21512 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21513 fixup_symbol_section (sym, objfile);
38583298
TT
21514 SET_SYMBOL_VALUE_ADDRESS (sym,
21515 SYMBOL_VALUE_ADDRESS (sym)
21516 + ANOFFSET (objfile->section_offsets,
21517 SYMBOL_SECTION (sym)));
4c2df51b
DJ
21518 return;
21519 }
21520
21521 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21522 expression evaluator, and use LOC_COMPUTED only when necessary
21523 (i.e. when the value of a register or memory location is
21524 referenced, or a thread-local block, etc.). Then again, it might
21525 not be worthwhile. I'm assuming that it isn't unless performance
21526 or memory numbers show me otherwise. */
21527
f1e6e072 21528 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21529
f1e6e072 21530 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21531 cu->has_loclist = true;
4c2df51b
DJ
21532}
21533
c906108c
SS
21534/* Given a pointer to a DWARF information entry, figure out if we need
21535 to make a symbol table entry for it, and if so, create a new entry
21536 and return a pointer to it.
21537 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21538 used the passed type.
21539 If SPACE is not NULL, use it to hold the new symbol. If it is
21540 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21541
21542static struct symbol *
5e2db402
TT
21543new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21544 struct symbol *space)
c906108c 21545{
518817b3
SM
21546 struct dwarf2_per_objfile *dwarf2_per_objfile
21547 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21548 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21549 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21550 struct symbol *sym = NULL;
15d034d0 21551 const char *name;
c906108c
SS
21552 struct attribute *attr = NULL;
21553 struct attribute *attr2 = NULL;
e142c38c 21554 CORE_ADDR baseaddr;
e37fd15a
SW
21555 struct pending **list_to_add = NULL;
21556
edb3359d 21557 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21558
21559 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21560
94af9270 21561 name = dwarf2_name (die, cu);
c906108c
SS
21562 if (name)
21563 {
94af9270 21564 const char *linkagename;
34eaf542 21565 int suppress_add = 0;
94af9270 21566
34eaf542
TT
21567 if (space)
21568 sym = space;
21569 else
e623cf5d 21570 sym = allocate_symbol (objfile);
c906108c 21571 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21572
21573 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21574 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21575 linkagename = dwarf2_physname (name, die, cu);
21576 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21577
f55ee35c
JK
21578 /* Fortran does not have mangling standard and the mangling does differ
21579 between gfortran, iFort etc. */
21580 if (cu->language == language_fortran
b250c185 21581 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21582 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21583 dwarf2_full_name (name, die, cu),
29df156d 21584 NULL);
f55ee35c 21585
c906108c 21586 /* Default assumptions.
c5aa993b 21587 Use the passed type or decode it from the die. */
176620f1 21588 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21589 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21590 if (type != NULL)
21591 SYMBOL_TYPE (sym) = type;
21592 else
e7c27a73 21593 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21594 attr = dwarf2_attr (die,
21595 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21596 cu);
c906108c
SS
21597 if (attr)
21598 {
21599 SYMBOL_LINE (sym) = DW_UNSND (attr);
21600 }
cb1df416 21601
edb3359d
DJ
21602 attr = dwarf2_attr (die,
21603 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21604 cu);
cb1df416
DJ
21605 if (attr)
21606 {
ecfb656c 21607 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21608 struct file_entry *fe;
9a619af0 21609
ecfb656c
PA
21610 if (cu->line_header != NULL)
21611 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21612 else
21613 fe = NULL;
21614
21615 if (fe == NULL)
b98664d3 21616 complaint (_("file index out of range"));
8c43009f
PA
21617 else
21618 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21619 }
21620
c906108c
SS
21621 switch (die->tag)
21622 {
21623 case DW_TAG_label:
e142c38c 21624 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21625 if (attr)
3e29f34a
MR
21626 {
21627 CORE_ADDR addr;
21628
21629 addr = attr_value_as_address (attr);
21630 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21631 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21632 }
0f5238ed
TT
21633 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21634 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21635 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21636 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21637 break;
21638 case DW_TAG_subprogram:
21639 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21640 finish_block. */
f1e6e072 21641 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21642 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21643 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21644 || cu->language == language_ada
21645 || cu->language == language_fortran)
c906108c 21646 {
2cfa0c8d 21647 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21648 Ada and Fortran subprograms, whether marked external or
21649 not, are always stored as a global symbol, because we want
21650 to be able to access them globally. For instance, we want
21651 to be able to break on a nested subprogram without having
21652 to specify the context. */
c24bdb02 21653 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21654 }
21655 else
21656 {
e37fd15a 21657 list_to_add = cu->list_in_scope;
c906108c
SS
21658 }
21659 break;
edb3359d
DJ
21660 case DW_TAG_inlined_subroutine:
21661 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21662 finish_block. */
f1e6e072 21663 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21664 SYMBOL_INLINED (sym) = 1;
481860b3 21665 list_to_add = cu->list_in_scope;
edb3359d 21666 break;
34eaf542
TT
21667 case DW_TAG_template_value_param:
21668 suppress_add = 1;
21669 /* Fall through. */
72929c62 21670 case DW_TAG_constant:
c906108c 21671 case DW_TAG_variable:
254e6b9e 21672 case DW_TAG_member:
0963b4bd
MS
21673 /* Compilation with minimal debug info may result in
21674 variables with missing type entries. Change the
21675 misleading `void' type to something sensible. */
c906108c 21676 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21677 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21678
e142c38c 21679 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21680 /* In the case of DW_TAG_member, we should only be called for
21681 static const members. */
21682 if (die->tag == DW_TAG_member)
21683 {
3863f96c
DE
21684 /* dwarf2_add_field uses die_is_declaration,
21685 so we do the same. */
254e6b9e
DE
21686 gdb_assert (die_is_declaration (die, cu));
21687 gdb_assert (attr);
21688 }
c906108c
SS
21689 if (attr)
21690 {
e7c27a73 21691 dwarf2_const_value (attr, sym, cu);
e142c38c 21692 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21693 if (!suppress_add)
34eaf542
TT
21694 {
21695 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21696 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21697 else
e37fd15a 21698 list_to_add = cu->list_in_scope;
34eaf542 21699 }
c906108c
SS
21700 break;
21701 }
e142c38c 21702 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21703 if (attr)
21704 {
e7c27a73 21705 var_decode_location (attr, sym, cu);
e142c38c 21706 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21707
21708 /* Fortran explicitly imports any global symbols to the local
21709 scope by DW_TAG_common_block. */
21710 if (cu->language == language_fortran && die->parent
21711 && die->parent->tag == DW_TAG_common_block)
21712 attr2 = NULL;
21713
caac4577
JG
21714 if (SYMBOL_CLASS (sym) == LOC_STATIC
21715 && SYMBOL_VALUE_ADDRESS (sym) == 0
21716 && !dwarf2_per_objfile->has_section_at_zero)
21717 {
21718 /* When a static variable is eliminated by the linker,
21719 the corresponding debug information is not stripped
21720 out, but the variable address is set to null;
21721 do not add such variables into symbol table. */
21722 }
21723 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21724 {
4b610737
TT
21725 if (SYMBOL_CLASS (sym) == LOC_STATIC
21726 && (objfile->flags & OBJF_MAINLINE) == 0
21727 && dwarf2_per_objfile->can_copy)
21728 {
21729 /* A global static variable might be subject to
21730 copy relocation. We first check for a local
21731 minsym, though, because maybe the symbol was
21732 marked hidden, in which case this would not
21733 apply. */
21734 bound_minimal_symbol found
21735 = (lookup_minimal_symbol_linkage
21736 (SYMBOL_LINKAGE_NAME (sym), objfile));
21737 if (found.minsym != nullptr)
21738 sym->maybe_copied = 1;
21739 }
f55ee35c 21740
1c809c68
TT
21741 /* A variable with DW_AT_external is never static,
21742 but it may be block-scoped. */
804d2729 21743 list_to_add
c24bdb02
KS
21744 = ((cu->list_in_scope
21745 == cu->get_builder ()->get_file_symbols ())
21746 ? cu->get_builder ()->get_global_symbols ()
804d2729 21747 : cu->list_in_scope);
1c809c68 21748 }
c906108c 21749 else
e37fd15a 21750 list_to_add = cu->list_in_scope;
c906108c
SS
21751 }
21752 else
21753 {
21754 /* We do not know the address of this symbol.
c5aa993b
JM
21755 If it is an external symbol and we have type information
21756 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21757 The address of the variable will then be determined from
21758 the minimal symbol table whenever the variable is
21759 referenced. */
e142c38c 21760 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21761
21762 /* Fortran explicitly imports any global symbols to the local
21763 scope by DW_TAG_common_block. */
21764 if (cu->language == language_fortran && die->parent
21765 && die->parent->tag == DW_TAG_common_block)
21766 {
21767 /* SYMBOL_CLASS doesn't matter here because
21768 read_common_block is going to reset it. */
21769 if (!suppress_add)
21770 list_to_add = cu->list_in_scope;
21771 }
21772 else if (attr2 && (DW_UNSND (attr2) != 0)
21773 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21774 {
0fe7935b
DJ
21775 /* A variable with DW_AT_external is never static, but it
21776 may be block-scoped. */
804d2729 21777 list_to_add
c24bdb02
KS
21778 = ((cu->list_in_scope
21779 == cu->get_builder ()->get_file_symbols ())
21780 ? cu->get_builder ()->get_global_symbols ()
804d2729 21781 : cu->list_in_scope);
0fe7935b 21782
f1e6e072 21783 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21784 }
442ddf59
JK
21785 else if (!die_is_declaration (die, cu))
21786 {
21787 /* Use the default LOC_OPTIMIZED_OUT class. */
21788 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21789 if (!suppress_add)
21790 list_to_add = cu->list_in_scope;
442ddf59 21791 }
c906108c
SS
21792 }
21793 break;
21794 case DW_TAG_formal_parameter:
a60f3166
TT
21795 {
21796 /* If we are inside a function, mark this as an argument. If
21797 not, we might be looking at an argument to an inlined function
21798 when we do not have enough information to show inlined frames;
21799 pretend it's a local variable in that case so that the user can
21800 still see it. */
804d2729 21801 struct context_stack *curr
c24bdb02 21802 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21803 if (curr != nullptr && curr->name != nullptr)
21804 SYMBOL_IS_ARGUMENT (sym) = 1;
21805 attr = dwarf2_attr (die, DW_AT_location, cu);
21806 if (attr)
21807 {
21808 var_decode_location (attr, sym, cu);
21809 }
21810 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21811 if (attr)
21812 {
21813 dwarf2_const_value (attr, sym, cu);
21814 }
f346a30d 21815
a60f3166
TT
21816 list_to_add = cu->list_in_scope;
21817 }
c906108c
SS
21818 break;
21819 case DW_TAG_unspecified_parameters:
21820 /* From varargs functions; gdb doesn't seem to have any
21821 interest in this information, so just ignore it for now.
21822 (FIXME?) */
21823 break;
34eaf542
TT
21824 case DW_TAG_template_type_param:
21825 suppress_add = 1;
21826 /* Fall through. */
c906108c 21827 case DW_TAG_class_type:
680b30c7 21828 case DW_TAG_interface_type:
c906108c
SS
21829 case DW_TAG_structure_type:
21830 case DW_TAG_union_type:
72019c9c 21831 case DW_TAG_set_type:
c906108c 21832 case DW_TAG_enumeration_type:
f1e6e072 21833 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21834 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21835
63d06c5c 21836 {
9c37b5ae 21837 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21838 really ever be static objects: otherwise, if you try
21839 to, say, break of a class's method and you're in a file
21840 which doesn't mention that class, it won't work unless
21841 the check for all static symbols in lookup_symbol_aux
21842 saves you. See the OtherFileClass tests in
21843 gdb.c++/namespace.exp. */
21844
e37fd15a 21845 if (!suppress_add)
34eaf542 21846 {
c24bdb02 21847 buildsym_compunit *builder = cu->get_builder ();
804d2729 21848 list_to_add
c24bdb02 21849 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21850 && cu->language == language_cplus
c24bdb02 21851 ? builder->get_global_symbols ()
804d2729 21852 : cu->list_in_scope);
63d06c5c 21853
64382290 21854 /* The semantics of C++ state that "struct foo {
9c37b5ae 21855 ... }" also defines a typedef for "foo". */
64382290 21856 if (cu->language == language_cplus
45280282 21857 || cu->language == language_ada
c44af4eb
TT
21858 || cu->language == language_d
21859 || cu->language == language_rust)
64382290
TT
21860 {
21861 /* The symbol's name is already allocated along
21862 with this objfile, so we don't need to
21863 duplicate it for the type. */
21864 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21865 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21866 }
63d06c5c
DC
21867 }
21868 }
c906108c
SS
21869 break;
21870 case DW_TAG_typedef:
f1e6e072 21871 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21872 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21873 list_to_add = cu->list_in_scope;
63d06c5c 21874 break;
c906108c 21875 case DW_TAG_base_type:
a02abb62 21876 case DW_TAG_subrange_type:
f1e6e072 21877 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21878 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21879 list_to_add = cu->list_in_scope;
c906108c
SS
21880 break;
21881 case DW_TAG_enumerator:
e142c38c 21882 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21883 if (attr)
21884 {
e7c27a73 21885 dwarf2_const_value (attr, sym, cu);
c906108c 21886 }
63d06c5c
DC
21887 {
21888 /* NOTE: carlton/2003-11-10: See comment above in the
21889 DW_TAG_class_type, etc. block. */
21890
804d2729 21891 list_to_add
c24bdb02 21892 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21893 && cu->language == language_cplus
c24bdb02 21894 ? cu->get_builder ()->get_global_symbols ()
804d2729 21895 : cu->list_in_scope);
63d06c5c 21896 }
c906108c 21897 break;
74921315 21898 case DW_TAG_imported_declaration:
5c4e30ca 21899 case DW_TAG_namespace:
f1e6e072 21900 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21901 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21902 break;
530e8392
KB
21903 case DW_TAG_module:
21904 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21905 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21906 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21907 break;
4357ac6c 21908 case DW_TAG_common_block:
f1e6e072 21909 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21910 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21911 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21912 break;
c906108c
SS
21913 default:
21914 /* Not a tag we recognize. Hopefully we aren't processing
21915 trash data, but since we must specifically ignore things
21916 we don't recognize, there is nothing else we should do at
0963b4bd 21917 this point. */
b98664d3 21918 complaint (_("unsupported tag: '%s'"),
4d3c2250 21919 dwarf_tag_name (die->tag));
c906108c
SS
21920 break;
21921 }
df8a16a1 21922
e37fd15a
SW
21923 if (suppress_add)
21924 {
21925 sym->hash_next = objfile->template_symbols;
21926 objfile->template_symbols = sym;
21927 list_to_add = NULL;
21928 }
21929
21930 if (list_to_add != NULL)
d3cb6808 21931 add_symbol_to_list (sym, list_to_add);
e37fd15a 21932
df8a16a1
DJ
21933 /* For the benefit of old versions of GCC, check for anonymous
21934 namespaces based on the demangled name. */
4d4ec4e5 21935 if (!cu->processing_has_namespace_info
94af9270 21936 && cu->language == language_cplus)
c24bdb02 21937 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21938 }
21939 return (sym);
21940}
21941
98bfdba5
PA
21942/* Given an attr with a DW_FORM_dataN value in host byte order,
21943 zero-extend it as appropriate for the symbol's type. The DWARF
21944 standard (v4) is not entirely clear about the meaning of using
21945 DW_FORM_dataN for a constant with a signed type, where the type is
21946 wider than the data. The conclusion of a discussion on the DWARF
21947 list was that this is unspecified. We choose to always zero-extend
21948 because that is the interpretation long in use by GCC. */
c906108c 21949
98bfdba5 21950static gdb_byte *
ff39bb5e 21951dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21952 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21953{
518817b3 21954 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21955 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21956 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21957 LONGEST l = DW_UNSND (attr);
21958
21959 if (bits < sizeof (*value) * 8)
21960 {
21961 l &= ((LONGEST) 1 << bits) - 1;
21962 *value = l;
21963 }
21964 else if (bits == sizeof (*value) * 8)
21965 *value = l;
21966 else
21967 {
224c3ddb 21968 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21969 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21970 return bytes;
21971 }
21972
21973 return NULL;
21974}
21975
21976/* Read a constant value from an attribute. Either set *VALUE, or if
21977 the value does not fit in *VALUE, set *BYTES - either already
21978 allocated on the objfile obstack, or newly allocated on OBSTACK,
21979 or, set *BATON, if we translated the constant to a location
21980 expression. */
21981
21982static void
ff39bb5e 21983dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21984 const char *name, struct obstack *obstack,
21985 struct dwarf2_cu *cu,
d521ce57 21986 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21987 struct dwarf2_locexpr_baton **baton)
21988{
518817b3 21989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21990 struct comp_unit_head *cu_header = &cu->header;
c906108c 21991 struct dwarf_block *blk;
98bfdba5
PA
21992 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21993 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21994
21995 *value = 0;
21996 *bytes = NULL;
21997 *baton = NULL;
c906108c
SS
21998
21999 switch (attr->form)
22000 {
22001 case DW_FORM_addr:
336d760d 22002 case DW_FORM_addrx:
3019eac3 22003 case DW_FORM_GNU_addr_index:
ac56253d 22004 {
ac56253d
TT
22005 gdb_byte *data;
22006
98bfdba5
PA
22007 if (TYPE_LENGTH (type) != cu_header->addr_size)
22008 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22009 cu_header->addr_size,
98bfdba5 22010 TYPE_LENGTH (type));
ac56253d
TT
22011 /* Symbols of this form are reasonably rare, so we just
22012 piggyback on the existing location code rather than writing
22013 a new implementation of symbol_computed_ops. */
8d749320 22014 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22015 (*baton)->per_cu = cu->per_cu;
22016 gdb_assert ((*baton)->per_cu);
ac56253d 22017
98bfdba5 22018 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22019 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22020 (*baton)->data = data;
ac56253d
TT
22021
22022 data[0] = DW_OP_addr;
22023 store_unsigned_integer (&data[1], cu_header->addr_size,
22024 byte_order, DW_ADDR (attr));
22025 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22026 }
c906108c 22027 break;
4ac36638 22028 case DW_FORM_string:
93b5768b 22029 case DW_FORM_strp:
cf532bd1 22030 case DW_FORM_strx:
3019eac3 22031 case DW_FORM_GNU_str_index:
36586728 22032 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22033 /* DW_STRING is already allocated on the objfile obstack, point
22034 directly to it. */
d521ce57 22035 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22036 break;
c906108c
SS
22037 case DW_FORM_block1:
22038 case DW_FORM_block2:
22039 case DW_FORM_block4:
22040 case DW_FORM_block:
2dc7f7b3 22041 case DW_FORM_exprloc:
0224619f 22042 case DW_FORM_data16:
c906108c 22043 blk = DW_BLOCK (attr);
98bfdba5
PA
22044 if (TYPE_LENGTH (type) != blk->size)
22045 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22046 TYPE_LENGTH (type));
22047 *bytes = blk->data;
c906108c 22048 break;
2df3850c
JM
22049
22050 /* The DW_AT_const_value attributes are supposed to carry the
22051 symbol's value "represented as it would be on the target
22052 architecture." By the time we get here, it's already been
22053 converted to host endianness, so we just need to sign- or
22054 zero-extend it as appropriate. */
22055 case DW_FORM_data1:
3aef2284 22056 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22057 break;
c906108c 22058 case DW_FORM_data2:
3aef2284 22059 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22060 break;
c906108c 22061 case DW_FORM_data4:
3aef2284 22062 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22063 break;
c906108c 22064 case DW_FORM_data8:
3aef2284 22065 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22066 break;
22067
c906108c 22068 case DW_FORM_sdata:
663c44ac 22069 case DW_FORM_implicit_const:
98bfdba5 22070 *value = DW_SND (attr);
2df3850c
JM
22071 break;
22072
c906108c 22073 case DW_FORM_udata:
98bfdba5 22074 *value = DW_UNSND (attr);
c906108c 22075 break;
2df3850c 22076
c906108c 22077 default:
b98664d3 22078 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22079 dwarf_form_name (attr->form));
98bfdba5 22080 *value = 0;
c906108c
SS
22081 break;
22082 }
22083}
22084
2df3850c 22085
98bfdba5
PA
22086/* Copy constant value from an attribute to a symbol. */
22087
2df3850c 22088static void
ff39bb5e 22089dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22090 struct dwarf2_cu *cu)
2df3850c 22091{
518817b3 22092 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22093 LONGEST value;
d521ce57 22094 const gdb_byte *bytes;
98bfdba5 22095 struct dwarf2_locexpr_baton *baton;
2df3850c 22096
98bfdba5
PA
22097 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22098 SYMBOL_PRINT_NAME (sym),
22099 &objfile->objfile_obstack, cu,
22100 &value, &bytes, &baton);
2df3850c 22101
98bfdba5
PA
22102 if (baton != NULL)
22103 {
98bfdba5 22104 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22105 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22106 }
22107 else if (bytes != NULL)
22108 {
22109 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22110 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22111 }
22112 else
22113 {
22114 SYMBOL_VALUE (sym) = value;
f1e6e072 22115 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22116 }
2df3850c
JM
22117}
22118
c906108c
SS
22119/* Return the type of the die in question using its DW_AT_type attribute. */
22120
22121static struct type *
e7c27a73 22122die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22123{
c906108c 22124 struct attribute *type_attr;
c906108c 22125
e142c38c 22126 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22127 if (!type_attr)
22128 {
518817b3 22129 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22130 /* A missing DW_AT_type represents a void type. */
518817b3 22131 return objfile_type (objfile)->builtin_void;
c906108c 22132 }
348e048f 22133
673bfd45 22134 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22135}
22136
b4ba55a1
JB
22137/* True iff CU's producer generates GNAT Ada auxiliary information
22138 that allows to find parallel types through that information instead
22139 of having to do expensive parallel lookups by type name. */
22140
22141static int
22142need_gnat_info (struct dwarf2_cu *cu)
22143{
de4cb04a
JB
22144 /* Assume that the Ada compiler was GNAT, which always produces
22145 the auxiliary information. */
22146 return (cu->language == language_ada);
b4ba55a1
JB
22147}
22148
b4ba55a1
JB
22149/* Return the auxiliary type of the die in question using its
22150 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22151 attribute is not present. */
22152
22153static struct type *
22154die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22155{
b4ba55a1 22156 struct attribute *type_attr;
b4ba55a1
JB
22157
22158 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22159 if (!type_attr)
22160 return NULL;
22161
673bfd45 22162 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22163}
22164
22165/* If DIE has a descriptive_type attribute, then set the TYPE's
22166 descriptive type accordingly. */
22167
22168static void
22169set_descriptive_type (struct type *type, struct die_info *die,
22170 struct dwarf2_cu *cu)
22171{
22172 struct type *descriptive_type = die_descriptive_type (die, cu);
22173
22174 if (descriptive_type)
22175 {
22176 ALLOCATE_GNAT_AUX_TYPE (type);
22177 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22178 }
22179}
22180
c906108c
SS
22181/* Return the containing type of the die in question using its
22182 DW_AT_containing_type attribute. */
22183
22184static struct type *
e7c27a73 22185die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22186{
c906108c 22187 struct attribute *type_attr;
518817b3 22188 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22189
e142c38c 22190 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22191 if (!type_attr)
22192 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22193 "[in module %s]"), objfile_name (objfile));
33ac96f0 22194
673bfd45 22195 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22196}
22197
ac9ec31b
DE
22198/* Return an error marker type to use for the ill formed type in DIE/CU. */
22199
22200static struct type *
22201build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22202{
518817b3
SM
22203 struct dwarf2_per_objfile *dwarf2_per_objfile
22204 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22205 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22206 char *saved;
ac9ec31b 22207
528e1572
SM
22208 std::string message
22209 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22210 objfile_name (objfile),
22211 sect_offset_str (cu->header.sect_off),
22212 sect_offset_str (die->sect_off));
efba19b0 22213 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22214
19f392bc 22215 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22216}
22217
673bfd45 22218/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22219 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22220 DW_AT_containing_type.
673bfd45
DE
22221 If there is no type substitute an error marker. */
22222
c906108c 22223static struct type *
ff39bb5e 22224lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22225 struct dwarf2_cu *cu)
c906108c 22226{
518817b3
SM
22227 struct dwarf2_per_objfile *dwarf2_per_objfile
22228 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22229 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22230 struct type *this_type;
22231
ac9ec31b
DE
22232 gdb_assert (attr->name == DW_AT_type
22233 || attr->name == DW_AT_GNAT_descriptive_type
22234 || attr->name == DW_AT_containing_type);
22235
673bfd45
DE
22236 /* First see if we have it cached. */
22237
36586728
TT
22238 if (attr->form == DW_FORM_GNU_ref_alt)
22239 {
22240 struct dwarf2_per_cu_data *per_cu;
9c541725 22241 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22242
ed2dc618
SM
22243 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22244 dwarf2_per_objfile);
9c541725 22245 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22246 }
7771576e 22247 else if (attr_form_is_ref (attr))
673bfd45 22248 {
9c541725 22249 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22250
9c541725 22251 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22252 }
55f1336d 22253 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22254 {
ac9ec31b 22255 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22256
ac9ec31b 22257 return get_signatured_type (die, signature, cu);
673bfd45
DE
22258 }
22259 else
22260 {
b98664d3 22261 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22262 " at %s [in module %s]"),
22263 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22264 objfile_name (objfile));
ac9ec31b 22265 return build_error_marker_type (cu, die);
673bfd45
DE
22266 }
22267
22268 /* If not cached we need to read it in. */
22269
22270 if (this_type == NULL)
22271 {
ac9ec31b 22272 struct die_info *type_die = NULL;
673bfd45
DE
22273 struct dwarf2_cu *type_cu = cu;
22274
7771576e 22275 if (attr_form_is_ref (attr))
ac9ec31b
DE
22276 type_die = follow_die_ref (die, attr, &type_cu);
22277 if (type_die == NULL)
22278 return build_error_marker_type (cu, die);
22279 /* If we find the type now, it's probably because the type came
3019eac3
DE
22280 from an inter-CU reference and the type's CU got expanded before
22281 ours. */
ac9ec31b 22282 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22283 }
22284
22285 /* If we still don't have a type use an error marker. */
22286
22287 if (this_type == NULL)
ac9ec31b 22288 return build_error_marker_type (cu, die);
673bfd45 22289
f792889a 22290 return this_type;
c906108c
SS
22291}
22292
673bfd45
DE
22293/* Return the type in DIE, CU.
22294 Returns NULL for invalid types.
22295
02142a6c 22296 This first does a lookup in die_type_hash,
673bfd45
DE
22297 and only reads the die in if necessary.
22298
22299 NOTE: This can be called when reading in partial or full symbols. */
22300
f792889a 22301static struct type *
e7c27a73 22302read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22303{
f792889a
DJ
22304 struct type *this_type;
22305
22306 this_type = get_die_type (die, cu);
22307 if (this_type)
22308 return this_type;
22309
673bfd45
DE
22310 return read_type_die_1 (die, cu);
22311}
22312
22313/* Read the type in DIE, CU.
22314 Returns NULL for invalid types. */
22315
22316static struct type *
22317read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22318{
22319 struct type *this_type = NULL;
22320
c906108c
SS
22321 switch (die->tag)
22322 {
22323 case DW_TAG_class_type:
680b30c7 22324 case DW_TAG_interface_type:
c906108c
SS
22325 case DW_TAG_structure_type:
22326 case DW_TAG_union_type:
f792889a 22327 this_type = read_structure_type (die, cu);
c906108c
SS
22328 break;
22329 case DW_TAG_enumeration_type:
f792889a 22330 this_type = read_enumeration_type (die, cu);
c906108c
SS
22331 break;
22332 case DW_TAG_subprogram:
22333 case DW_TAG_subroutine_type:
edb3359d 22334 case DW_TAG_inlined_subroutine:
f792889a 22335 this_type = read_subroutine_type (die, cu);
c906108c
SS
22336 break;
22337 case DW_TAG_array_type:
f792889a 22338 this_type = read_array_type (die, cu);
c906108c 22339 break;
72019c9c 22340 case DW_TAG_set_type:
f792889a 22341 this_type = read_set_type (die, cu);
72019c9c 22342 break;
c906108c 22343 case DW_TAG_pointer_type:
f792889a 22344 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22345 break;
22346 case DW_TAG_ptr_to_member_type:
f792889a 22347 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22348 break;
22349 case DW_TAG_reference_type:
4297a3f0
AV
22350 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22351 break;
22352 case DW_TAG_rvalue_reference_type:
22353 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22354 break;
22355 case DW_TAG_const_type:
f792889a 22356 this_type = read_tag_const_type (die, cu);
c906108c
SS
22357 break;
22358 case DW_TAG_volatile_type:
f792889a 22359 this_type = read_tag_volatile_type (die, cu);
c906108c 22360 break;
06d66ee9
TT
22361 case DW_TAG_restrict_type:
22362 this_type = read_tag_restrict_type (die, cu);
22363 break;
c906108c 22364 case DW_TAG_string_type:
f792889a 22365 this_type = read_tag_string_type (die, cu);
c906108c
SS
22366 break;
22367 case DW_TAG_typedef:
f792889a 22368 this_type = read_typedef (die, cu);
c906108c 22369 break;
a02abb62 22370 case DW_TAG_subrange_type:
f792889a 22371 this_type = read_subrange_type (die, cu);
a02abb62 22372 break;
c906108c 22373 case DW_TAG_base_type:
f792889a 22374 this_type = read_base_type (die, cu);
c906108c 22375 break;
81a17f79 22376 case DW_TAG_unspecified_type:
f792889a 22377 this_type = read_unspecified_type (die, cu);
81a17f79 22378 break;
0114d602
DJ
22379 case DW_TAG_namespace:
22380 this_type = read_namespace_type (die, cu);
22381 break;
f55ee35c
JK
22382 case DW_TAG_module:
22383 this_type = read_module_type (die, cu);
22384 break;
a2c2acaf
MW
22385 case DW_TAG_atomic_type:
22386 this_type = read_tag_atomic_type (die, cu);
22387 break;
c906108c 22388 default:
b98664d3 22389 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22390 dwarf_tag_name (die->tag));
c906108c
SS
22391 break;
22392 }
63d06c5c 22393
f792889a 22394 return this_type;
63d06c5c
DC
22395}
22396
abc72ce4
DE
22397/* See if we can figure out if the class lives in a namespace. We do
22398 this by looking for a member function; its demangled name will
22399 contain namespace info, if there is any.
22400 Return the computed name or NULL.
22401 Space for the result is allocated on the objfile's obstack.
22402 This is the full-die version of guess_partial_die_structure_name.
22403 In this case we know DIE has no useful parent. */
22404
22405static char *
22406guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22407{
22408 struct die_info *spec_die;
22409 struct dwarf2_cu *spec_cu;
22410 struct die_info *child;
518817b3 22411 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22412
22413 spec_cu = cu;
22414 spec_die = die_specification (die, &spec_cu);
22415 if (spec_die != NULL)
22416 {
22417 die = spec_die;
22418 cu = spec_cu;
22419 }
22420
22421 for (child = die->child;
22422 child != NULL;
22423 child = child->sibling)
22424 {
22425 if (child->tag == DW_TAG_subprogram)
22426 {
73b9be8b 22427 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22428
7d45c7c3 22429 if (linkage_name != NULL)
abc72ce4
DE
22430 {
22431 char *actual_name
22432 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22433 linkage_name);
abc72ce4
DE
22434 char *name = NULL;
22435
22436 if (actual_name != NULL)
22437 {
15d034d0 22438 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22439
22440 if (die_name != NULL
22441 && strcmp (die_name, actual_name) != 0)
22442 {
22443 /* Strip off the class name from the full name.
22444 We want the prefix. */
22445 int die_name_len = strlen (die_name);
22446 int actual_name_len = strlen (actual_name);
22447
22448 /* Test for '::' as a sanity check. */
22449 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22450 && actual_name[actual_name_len
22451 - die_name_len - 1] == ':')
0cf9feb9 22452 name = obstack_strndup (
e3b94546 22453 &objfile->per_bfd->storage_obstack,
224c3ddb 22454 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22455 }
22456 }
22457 xfree (actual_name);
22458 return name;
22459 }
22460 }
22461 }
22462
22463 return NULL;
22464}
22465
96408a79
SA
22466/* GCC might emit a nameless typedef that has a linkage name. Determine the
22467 prefix part in such case. See
22468 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22469
a121b7c1 22470static const char *
96408a79
SA
22471anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22472{
22473 struct attribute *attr;
e6a959d6 22474 const char *base;
96408a79
SA
22475
22476 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22477 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22478 return NULL;
22479
7d45c7c3 22480 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22481 return NULL;
22482
73b9be8b 22483 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22484 if (attr == NULL || DW_STRING (attr) == NULL)
22485 return NULL;
22486
22487 /* dwarf2_name had to be already called. */
22488 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22489
22490 /* Strip the base name, keep any leading namespaces/classes. */
22491 base = strrchr (DW_STRING (attr), ':');
22492 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22493 return "";
22494
518817b3 22495 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22496 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22497 DW_STRING (attr),
22498 &base[-1] - DW_STRING (attr));
96408a79
SA
22499}
22500
fdde2d81 22501/* Return the name of the namespace/class that DIE is defined within,
0114d602 22502 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22503
0114d602
DJ
22504 For example, if we're within the method foo() in the following
22505 code:
22506
22507 namespace N {
22508 class C {
22509 void foo () {
22510 }
22511 };
22512 }
22513
22514 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22515
0d5cff50 22516static const char *
e142c38c 22517determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22518{
518817b3
SM
22519 struct dwarf2_per_objfile *dwarf2_per_objfile
22520 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22521 struct die_info *parent, *spec_die;
22522 struct dwarf2_cu *spec_cu;
22523 struct type *parent_type;
a121b7c1 22524 const char *retval;
63d06c5c 22525
9c37b5ae 22526 if (cu->language != language_cplus
c44af4eb
TT
22527 && cu->language != language_fortran && cu->language != language_d
22528 && cu->language != language_rust)
0114d602
DJ
22529 return "";
22530
96408a79
SA
22531 retval = anonymous_struct_prefix (die, cu);
22532 if (retval)
22533 return retval;
22534
0114d602
DJ
22535 /* We have to be careful in the presence of DW_AT_specification.
22536 For example, with GCC 3.4, given the code
22537
22538 namespace N {
22539 void foo() {
22540 // Definition of N::foo.
22541 }
22542 }
22543
22544 then we'll have a tree of DIEs like this:
22545
22546 1: DW_TAG_compile_unit
22547 2: DW_TAG_namespace // N
22548 3: DW_TAG_subprogram // declaration of N::foo
22549 4: DW_TAG_subprogram // definition of N::foo
22550 DW_AT_specification // refers to die #3
22551
22552 Thus, when processing die #4, we have to pretend that we're in
22553 the context of its DW_AT_specification, namely the contex of die
22554 #3. */
22555 spec_cu = cu;
22556 spec_die = die_specification (die, &spec_cu);
22557 if (spec_die == NULL)
22558 parent = die->parent;
22559 else
63d06c5c 22560 {
0114d602
DJ
22561 parent = spec_die->parent;
22562 cu = spec_cu;
63d06c5c 22563 }
0114d602
DJ
22564
22565 if (parent == NULL)
22566 return "";
98bfdba5
PA
22567 else if (parent->building_fullname)
22568 {
22569 const char *name;
22570 const char *parent_name;
22571
22572 /* It has been seen on RealView 2.2 built binaries,
22573 DW_TAG_template_type_param types actually _defined_ as
22574 children of the parent class:
22575
22576 enum E {};
22577 template class <class Enum> Class{};
22578 Class<enum E> class_e;
22579
22580 1: DW_TAG_class_type (Class)
22581 2: DW_TAG_enumeration_type (E)
22582 3: DW_TAG_enumerator (enum1:0)
22583 3: DW_TAG_enumerator (enum2:1)
22584 ...
22585 2: DW_TAG_template_type_param
22586 DW_AT_type DW_FORM_ref_udata (E)
22587
22588 Besides being broken debug info, it can put GDB into an
22589 infinite loop. Consider:
22590
22591 When we're building the full name for Class<E>, we'll start
22592 at Class, and go look over its template type parameters,
22593 finding E. We'll then try to build the full name of E, and
22594 reach here. We're now trying to build the full name of E,
22595 and look over the parent DIE for containing scope. In the
22596 broken case, if we followed the parent DIE of E, we'd again
22597 find Class, and once again go look at its template type
22598 arguments, etc., etc. Simply don't consider such parent die
22599 as source-level parent of this die (it can't be, the language
22600 doesn't allow it), and break the loop here. */
22601 name = dwarf2_name (die, cu);
22602 parent_name = dwarf2_name (parent, cu);
b98664d3 22603 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22604 name ? name : "<unknown>",
22605 parent_name ? parent_name : "<unknown>");
22606 return "";
22607 }
63d06c5c 22608 else
0114d602
DJ
22609 switch (parent->tag)
22610 {
63d06c5c 22611 case DW_TAG_namespace:
0114d602 22612 parent_type = read_type_die (parent, cu);
acebe513
UW
22613 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22614 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22615 Work around this problem here. */
22616 if (cu->language == language_cplus
e86ca25f 22617 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22618 return "";
0114d602 22619 /* We give a name to even anonymous namespaces. */
e86ca25f 22620 return TYPE_NAME (parent_type);
63d06c5c 22621 case DW_TAG_class_type:
680b30c7 22622 case DW_TAG_interface_type:
63d06c5c 22623 case DW_TAG_structure_type:
0114d602 22624 case DW_TAG_union_type:
f55ee35c 22625 case DW_TAG_module:
0114d602 22626 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22627 if (TYPE_NAME (parent_type) != NULL)
22628 return TYPE_NAME (parent_type);
0114d602
DJ
22629 else
22630 /* An anonymous structure is only allowed non-static data
22631 members; no typedefs, no member functions, et cetera.
22632 So it does not need a prefix. */
22633 return "";
abc72ce4 22634 case DW_TAG_compile_unit:
95554aad 22635 case DW_TAG_partial_unit:
abc72ce4
DE
22636 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22637 if (cu->language == language_cplus
fd5866f6 22638 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22639 && die->child != NULL
22640 && (die->tag == DW_TAG_class_type
22641 || die->tag == DW_TAG_structure_type
22642 || die->tag == DW_TAG_union_type))
22643 {
22644 char *name = guess_full_die_structure_name (die, cu);
22645 if (name != NULL)
22646 return name;
22647 }
22648 return "";
0a4b0913
AB
22649 case DW_TAG_subprogram:
22650 /* Nested subroutines in Fortran get a prefix with the name
22651 of the parent's subroutine. */
22652 if (cu->language == language_fortran)
22653 {
22654 if ((die->tag == DW_TAG_subprogram)
22655 && (dwarf2_name (parent, cu) != NULL))
22656 return dwarf2_name (parent, cu);
22657 }
22658 return determine_prefix (parent, cu);
3d567982
TT
22659 case DW_TAG_enumeration_type:
22660 parent_type = read_type_die (parent, cu);
22661 if (TYPE_DECLARED_CLASS (parent_type))
22662 {
e86ca25f
TT
22663 if (TYPE_NAME (parent_type) != NULL)
22664 return TYPE_NAME (parent_type);
3d567982
TT
22665 return "";
22666 }
22667 /* Fall through. */
63d06c5c 22668 default:
8176b9b8 22669 return determine_prefix (parent, cu);
63d06c5c 22670 }
63d06c5c
DC
22671}
22672
3e43a32a
MS
22673/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22674 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22675 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22676 an obconcat, otherwise allocate storage for the result. The CU argument is
22677 used to determine the language and hence, the appropriate separator. */
987504bb 22678
f55ee35c 22679#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22680
22681static char *
f55ee35c
JK
22682typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22683 int physname, struct dwarf2_cu *cu)
63d06c5c 22684{
f55ee35c 22685 const char *lead = "";
5c315b68 22686 const char *sep;
63d06c5c 22687
3e43a32a
MS
22688 if (suffix == NULL || suffix[0] == '\0'
22689 || prefix == NULL || prefix[0] == '\0')
987504bb 22690 sep = "";
45280282
IB
22691 else if (cu->language == language_d)
22692 {
22693 /* For D, the 'main' function could be defined in any module, but it
22694 should never be prefixed. */
22695 if (strcmp (suffix, "D main") == 0)
22696 {
22697 prefix = "";
22698 sep = "";
22699 }
22700 else
22701 sep = ".";
22702 }
f55ee35c
JK
22703 else if (cu->language == language_fortran && physname)
22704 {
22705 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22706 DW_AT_MIPS_linkage_name is preferred and used instead. */
22707
22708 lead = "__";
22709 sep = "_MOD_";
22710 }
987504bb
JJ
22711 else
22712 sep = "::";
63d06c5c 22713
6dd47d34
DE
22714 if (prefix == NULL)
22715 prefix = "";
22716 if (suffix == NULL)
22717 suffix = "";
22718
987504bb
JJ
22719 if (obs == NULL)
22720 {
3e43a32a 22721 char *retval
224c3ddb
SM
22722 = ((char *)
22723 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22724
f55ee35c
JK
22725 strcpy (retval, lead);
22726 strcat (retval, prefix);
6dd47d34
DE
22727 strcat (retval, sep);
22728 strcat (retval, suffix);
63d06c5c
DC
22729 return retval;
22730 }
987504bb
JJ
22731 else
22732 {
22733 /* We have an obstack. */
f55ee35c 22734 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22735 }
63d06c5c
DC
22736}
22737
c906108c
SS
22738/* Return sibling of die, NULL if no sibling. */
22739
f9aca02d 22740static struct die_info *
fba45db2 22741sibling_die (struct die_info *die)
c906108c 22742{
639d11d3 22743 return die->sibling;
c906108c
SS
22744}
22745
71c25dea
TT
22746/* Get name of a die, return NULL if not found. */
22747
15d034d0
TT
22748static const char *
22749dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22750 struct obstack *obstack)
22751{
22752 if (name && cu->language == language_cplus)
22753 {
2f408ecb 22754 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22755
2f408ecb 22756 if (!canon_name.empty ())
71c25dea 22757 {
2f408ecb 22758 if (canon_name != name)
efba19b0 22759 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22760 }
22761 }
22762
22763 return name;
c906108c
SS
22764}
22765
96553a0c
DE
22766/* Get name of a die, return NULL if not found.
22767 Anonymous namespaces are converted to their magic string. */
9219021c 22768
15d034d0 22769static const char *
e142c38c 22770dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22771{
22772 struct attribute *attr;
518817b3 22773 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22774
e142c38c 22775 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22776 if ((!attr || !DW_STRING (attr))
96553a0c 22777 && die->tag != DW_TAG_namespace
53832f31
TT
22778 && die->tag != DW_TAG_class_type
22779 && die->tag != DW_TAG_interface_type
22780 && die->tag != DW_TAG_structure_type
22781 && die->tag != DW_TAG_union_type)
71c25dea
TT
22782 return NULL;
22783
22784 switch (die->tag)
22785 {
22786 case DW_TAG_compile_unit:
95554aad 22787 case DW_TAG_partial_unit:
71c25dea
TT
22788 /* Compilation units have a DW_AT_name that is a filename, not
22789 a source language identifier. */
22790 case DW_TAG_enumeration_type:
22791 case DW_TAG_enumerator:
22792 /* These tags always have simple identifiers already; no need
22793 to canonicalize them. */
22794 return DW_STRING (attr);
907af001 22795
96553a0c
DE
22796 case DW_TAG_namespace:
22797 if (attr != NULL && DW_STRING (attr) != NULL)
22798 return DW_STRING (attr);
22799 return CP_ANONYMOUS_NAMESPACE_STR;
22800
907af001
UW
22801 case DW_TAG_class_type:
22802 case DW_TAG_interface_type:
22803 case DW_TAG_structure_type:
22804 case DW_TAG_union_type:
22805 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22806 structures or unions. These were of the form "._%d" in GCC 4.1,
22807 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22808 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22809 if (attr && DW_STRING (attr)
61012eef
GB
22810 && (startswith (DW_STRING (attr), "._")
22811 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22812 return NULL;
53832f31
TT
22813
22814 /* GCC might emit a nameless typedef that has a linkage name. See
22815 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22816 if (!attr || DW_STRING (attr) == NULL)
22817 {
df5c6c50 22818 char *demangled = NULL;
53832f31 22819
73b9be8b 22820 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22821 if (attr == NULL || DW_STRING (attr) == NULL)
22822 return NULL;
22823
df5c6c50
JK
22824 /* Avoid demangling DW_STRING (attr) the second time on a second
22825 call for the same DIE. */
22826 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22827 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22828
22829 if (demangled)
22830 {
e6a959d6 22831 const char *base;
96408a79 22832
53832f31 22833 /* FIXME: we already did this for the partial symbol... */
34a68019 22834 DW_STRING (attr)
021887d8
TT
22835 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22836 demangled);
53832f31
TT
22837 DW_STRING_IS_CANONICAL (attr) = 1;
22838 xfree (demangled);
96408a79
SA
22839
22840 /* Strip any leading namespaces/classes, keep only the base name.
22841 DW_AT_name for named DIEs does not contain the prefixes. */
22842 base = strrchr (DW_STRING (attr), ':');
22843 if (base && base > DW_STRING (attr) && base[-1] == ':')
22844 return &base[1];
22845 else
22846 return DW_STRING (attr);
53832f31
TT
22847 }
22848 }
907af001
UW
22849 break;
22850
71c25dea 22851 default:
907af001
UW
22852 break;
22853 }
22854
22855 if (!DW_STRING_IS_CANONICAL (attr))
22856 {
22857 DW_STRING (attr)
22858 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22859 &objfile->per_bfd->storage_obstack);
907af001 22860 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22861 }
907af001 22862 return DW_STRING (attr);
9219021c
DC
22863}
22864
22865/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22866 is none. *EXT_CU is the CU containing DIE on input, and the CU
22867 containing the return value on output. */
9219021c
DC
22868
22869static struct die_info *
f2f0e013 22870dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22871{
22872 struct attribute *attr;
9219021c 22873
f2f0e013 22874 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22875 if (attr == NULL)
22876 return NULL;
22877
f2f0e013 22878 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22879}
22880
fa9c3fa0
TT
22881/* A convenience function that returns an "unknown" DWARF name,
22882 including the value of V. STR is the name of the entity being
22883 printed, e.g., "TAG". */
22884
22885static const char *
22886dwarf_unknown (const char *str, unsigned v)
22887{
22888 char *cell = get_print_cell ();
22889 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22890 return cell;
22891}
22892
c906108c
SS
22893/* Convert a DIE tag into its string name. */
22894
f39c6ffd 22895static const char *
aa1ee363 22896dwarf_tag_name (unsigned tag)
c906108c 22897{
f39c6ffd
TT
22898 const char *name = get_DW_TAG_name (tag);
22899
22900 if (name == NULL)
fa9c3fa0 22901 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
22902
22903 return name;
c906108c
SS
22904}
22905
22906/* Convert a DWARF attribute code into its string name. */
22907
f39c6ffd 22908static const char *
aa1ee363 22909dwarf_attr_name (unsigned attr)
c906108c 22910{
f39c6ffd
TT
22911 const char *name;
22912
c764a876 22913#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22914 if (attr == DW_AT_MIPS_fde)
22915 return "DW_AT_MIPS_fde";
22916#else
22917 if (attr == DW_AT_HP_block_index)
22918 return "DW_AT_HP_block_index";
c764a876 22919#endif
f39c6ffd
TT
22920
22921 name = get_DW_AT_name (attr);
22922
22923 if (name == NULL)
fa9c3fa0 22924 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
22925
22926 return name;
c906108c
SS
22927}
22928
a084a2a6
AT
22929/* Convert a unit type to corresponding DW_UT name. */
22930
22931static const char *
22932dwarf_unit_type_name (int unit_type) {
22933 switch (unit_type)
22934 {
22935 case 0x01:
22936 return "DW_UT_compile (0x01)";
22937 case 0x02:
22938 return "DW_UT_type (0x02)";
22939 case 0x03:
22940 return "DW_UT_partial (0x03)";
22941 case 0x04:
22942 return "DW_UT_skeleton (0x04)";
22943 case 0x05:
22944 return "DW_UT_split_compile (0x05)";
22945 case 0x06:
22946 return "DW_UT_split_type (0x06)";
22947 case 0x80:
22948 return "DW_UT_lo_user (0x80)";
22949 case 0xff:
22950 return "DW_UT_hi_user (0xff)";
22951 default:
22952 return nullptr;
22953 }
22954}
22955
c906108c
SS
22956/* Convert a DWARF value form code into its string name. */
22957
f39c6ffd 22958static const char *
aa1ee363 22959dwarf_form_name (unsigned form)
c906108c 22960{
f39c6ffd
TT
22961 const char *name = get_DW_FORM_name (form);
22962
22963 if (name == NULL)
fa9c3fa0 22964 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
22965
22966 return name;
c906108c
SS
22967}
22968
a121b7c1 22969static const char *
fba45db2 22970dwarf_bool_name (unsigned mybool)
c906108c
SS
22971{
22972 if (mybool)
22973 return "TRUE";
22974 else
22975 return "FALSE";
22976}
22977
22978/* Convert a DWARF type code into its string name. */
22979
f39c6ffd 22980static const char *
aa1ee363 22981dwarf_type_encoding_name (unsigned enc)
c906108c 22982{
f39c6ffd 22983 const char *name = get_DW_ATE_name (enc);
c906108c 22984
f39c6ffd 22985 if (name == NULL)
fa9c3fa0 22986 return dwarf_unknown ("ATE", enc);
c906108c 22987
f39c6ffd 22988 return name;
c906108c 22989}
c906108c 22990
f9aca02d 22991static void
d97bc12b 22992dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22993{
22994 unsigned int i;
22995
d97bc12b 22996 print_spaces (indent, f);
9d8780f0 22997 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22998 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22999 sect_offset_str (die->sect_off));
d97bc12b
DE
23000
23001 if (die->parent != NULL)
23002 {
23003 print_spaces (indent, f);
9d8780f0
SM
23004 fprintf_unfiltered (f, " parent at offset: %s\n",
23005 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23006 }
23007
23008 print_spaces (indent, f);
23009 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23010 dwarf_bool_name (die->child != NULL));
c906108c 23011
d97bc12b
DE
23012 print_spaces (indent, f);
23013 fprintf_unfiltered (f, " attributes:\n");
23014
c906108c
SS
23015 for (i = 0; i < die->num_attrs; ++i)
23016 {
d97bc12b
DE
23017 print_spaces (indent, f);
23018 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23019 dwarf_attr_name (die->attrs[i].name),
23020 dwarf_form_name (die->attrs[i].form));
d97bc12b 23021
c906108c
SS
23022 switch (die->attrs[i].form)
23023 {
c906108c 23024 case DW_FORM_addr:
336d760d 23025 case DW_FORM_addrx:
3019eac3 23026 case DW_FORM_GNU_addr_index:
d97bc12b 23027 fprintf_unfiltered (f, "address: ");
5af949e3 23028 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23029 break;
23030 case DW_FORM_block2:
23031 case DW_FORM_block4:
23032 case DW_FORM_block:
23033 case DW_FORM_block1:
56eb65bd
SP
23034 fprintf_unfiltered (f, "block: size %s",
23035 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23036 break;
2dc7f7b3 23037 case DW_FORM_exprloc:
56eb65bd
SP
23038 fprintf_unfiltered (f, "expression: size %s",
23039 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23040 break;
0224619f
JK
23041 case DW_FORM_data16:
23042 fprintf_unfiltered (f, "constant of 16 bytes");
23043 break;
4568ecf9
DE
23044 case DW_FORM_ref_addr:
23045 fprintf_unfiltered (f, "ref address: ");
23046 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23047 break;
36586728
TT
23048 case DW_FORM_GNU_ref_alt:
23049 fprintf_unfiltered (f, "alt ref address: ");
23050 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23051 break;
10b3939b
DJ
23052 case DW_FORM_ref1:
23053 case DW_FORM_ref2:
23054 case DW_FORM_ref4:
4568ecf9
DE
23055 case DW_FORM_ref8:
23056 case DW_FORM_ref_udata:
d97bc12b 23057 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23058 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23059 break;
c906108c
SS
23060 case DW_FORM_data1:
23061 case DW_FORM_data2:
23062 case DW_FORM_data4:
ce5d95e1 23063 case DW_FORM_data8:
c906108c
SS
23064 case DW_FORM_udata:
23065 case DW_FORM_sdata:
43bbcdc2
PH
23066 fprintf_unfiltered (f, "constant: %s",
23067 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23068 break;
2dc7f7b3
TT
23069 case DW_FORM_sec_offset:
23070 fprintf_unfiltered (f, "section offset: %s",
23071 pulongest (DW_UNSND (&die->attrs[i])));
23072 break;
55f1336d 23073 case DW_FORM_ref_sig8:
ac9ec31b
DE
23074 fprintf_unfiltered (f, "signature: %s",
23075 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23076 break;
c906108c 23077 case DW_FORM_string:
4bdf3d34 23078 case DW_FORM_strp:
43988095 23079 case DW_FORM_line_strp:
cf532bd1 23080 case DW_FORM_strx:
3019eac3 23081 case DW_FORM_GNU_str_index:
36586728 23082 case DW_FORM_GNU_strp_alt:
8285870a 23083 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23084 DW_STRING (&die->attrs[i])
8285870a
JK
23085 ? DW_STRING (&die->attrs[i]) : "",
23086 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23087 break;
23088 case DW_FORM_flag:
23089 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23090 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23091 else
d97bc12b 23092 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23093 break;
2dc7f7b3
TT
23094 case DW_FORM_flag_present:
23095 fprintf_unfiltered (f, "flag: TRUE");
23096 break;
a8329558 23097 case DW_FORM_indirect:
0963b4bd
MS
23098 /* The reader will have reduced the indirect form to
23099 the "base form" so this form should not occur. */
5f48f8f3 23100 fprintf_unfiltered (f,
3e43a32a 23101 "unexpected attribute form: DW_FORM_indirect");
a8329558 23102 break;
663c44ac
JK
23103 case DW_FORM_implicit_const:
23104 fprintf_unfiltered (f, "constant: %s",
23105 plongest (DW_SND (&die->attrs[i])));
23106 break;
c906108c 23107 default:
d97bc12b 23108 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23109 die->attrs[i].form);
d97bc12b 23110 break;
c906108c 23111 }
d97bc12b 23112 fprintf_unfiltered (f, "\n");
c906108c
SS
23113 }
23114}
23115
f9aca02d 23116static void
d97bc12b 23117dump_die_for_error (struct die_info *die)
c906108c 23118{
d97bc12b
DE
23119 dump_die_shallow (gdb_stderr, 0, die);
23120}
23121
23122static void
23123dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23124{
23125 int indent = level * 4;
23126
23127 gdb_assert (die != NULL);
23128
23129 if (level >= max_level)
23130 return;
23131
23132 dump_die_shallow (f, indent, die);
23133
23134 if (die->child != NULL)
c906108c 23135 {
d97bc12b
DE
23136 print_spaces (indent, f);
23137 fprintf_unfiltered (f, " Children:");
23138 if (level + 1 < max_level)
23139 {
23140 fprintf_unfiltered (f, "\n");
23141 dump_die_1 (f, level + 1, max_level, die->child);
23142 }
23143 else
23144 {
3e43a32a
MS
23145 fprintf_unfiltered (f,
23146 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23147 }
23148 }
23149
23150 if (die->sibling != NULL && level > 0)
23151 {
23152 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23153 }
23154}
23155
d97bc12b
DE
23156/* This is called from the pdie macro in gdbinit.in.
23157 It's not static so gcc will keep a copy callable from gdb. */
23158
23159void
23160dump_die (struct die_info *die, int max_level)
23161{
23162 dump_die_1 (gdb_stdlog, 0, max_level, die);
23163}
23164
f9aca02d 23165static void
51545339 23166store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23167{
51545339 23168 void **slot;
c906108c 23169
9c541725
PA
23170 slot = htab_find_slot_with_hash (cu->die_hash, die,
23171 to_underlying (die->sect_off),
b64f50a1 23172 INSERT);
51545339
DJ
23173
23174 *slot = die;
c906108c
SS
23175}
23176
b64f50a1
JK
23177/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23178 required kind. */
23179
23180static sect_offset
ff39bb5e 23181dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23182{
7771576e 23183 if (attr_form_is_ref (attr))
9c541725 23184 return (sect_offset) DW_UNSND (attr);
93311388 23185
b98664d3 23186 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23187 dwarf_form_name (attr->form));
9c541725 23188 return {};
c906108c
SS
23189}
23190
43bbcdc2
PH
23191/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23192 * the value held by the attribute is not constant. */
a02abb62 23193
43bbcdc2 23194static LONGEST
ff39bb5e 23195dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23196{
663c44ac 23197 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23198 return DW_SND (attr);
23199 else if (attr->form == DW_FORM_udata
23200 || attr->form == DW_FORM_data1
23201 || attr->form == DW_FORM_data2
23202 || attr->form == DW_FORM_data4
23203 || attr->form == DW_FORM_data8)
23204 return DW_UNSND (attr);
23205 else
23206 {
0224619f 23207 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23208 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23209 dwarf_form_name (attr->form));
23210 return default_value;
23211 }
23212}
23213
348e048f
DE
23214/* Follow reference or signature attribute ATTR of SRC_DIE.
23215 On entry *REF_CU is the CU of SRC_DIE.
23216 On exit *REF_CU is the CU of the result. */
23217
23218static struct die_info *
ff39bb5e 23219follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23220 struct dwarf2_cu **ref_cu)
23221{
23222 struct die_info *die;
23223
7771576e 23224 if (attr_form_is_ref (attr))
348e048f 23225 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23226 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23227 die = follow_die_sig (src_die, attr, ref_cu);
23228 else
23229 {
23230 dump_die_for_error (src_die);
23231 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23232 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23233 }
23234
23235 return die;
03dd20cc
DJ
23236}
23237
5c631832 23238/* Follow reference OFFSET.
673bfd45
DE
23239 On entry *REF_CU is the CU of the source die referencing OFFSET.
23240 On exit *REF_CU is the CU of the result.
23241 Returns NULL if OFFSET is invalid. */
f504f079 23242
f9aca02d 23243static struct die_info *
9c541725 23244follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23245 struct dwarf2_cu **ref_cu)
c906108c 23246{
10b3939b 23247 struct die_info temp_die;
f2f0e013 23248 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23249 struct dwarf2_per_objfile *dwarf2_per_objfile
23250 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23251
348e048f
DE
23252 gdb_assert (cu->per_cu != NULL);
23253
98bfdba5
PA
23254 target_cu = cu;
23255
3019eac3 23256 if (cu->per_cu->is_debug_types)
348e048f
DE
23257 {
23258 /* .debug_types CUs cannot reference anything outside their CU.
23259 If they need to, they have to reference a signatured type via
55f1336d 23260 DW_FORM_ref_sig8. */
9c541725 23261 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23262 return NULL;
348e048f 23263 }
36586728 23264 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23265 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23266 {
23267 struct dwarf2_per_cu_data *per_cu;
9a619af0 23268
9c541725 23269 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23270 dwarf2_per_objfile);
03dd20cc
DJ
23271
23272 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23273 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23274 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23275
10b3939b
DJ
23276 target_cu = per_cu->cu;
23277 }
98bfdba5
PA
23278 else if (cu->dies == NULL)
23279 {
23280 /* We're loading full DIEs during partial symbol reading. */
23281 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23282 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23283 }
c906108c 23284
f2f0e013 23285 *ref_cu = target_cu;
9c541725 23286 temp_die.sect_off = sect_off;
c24bdb02
KS
23287
23288 if (target_cu != cu)
23289 target_cu->ancestor = cu;
23290
9a3c8263 23291 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23292 &temp_die,
23293 to_underlying (sect_off));
5c631832 23294}
10b3939b 23295
5c631832
JK
23296/* Follow reference attribute ATTR of SRC_DIE.
23297 On entry *REF_CU is the CU of SRC_DIE.
23298 On exit *REF_CU is the CU of the result. */
23299
23300static struct die_info *
ff39bb5e 23301follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23302 struct dwarf2_cu **ref_cu)
23303{
9c541725 23304 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23305 struct dwarf2_cu *cu = *ref_cu;
23306 struct die_info *die;
23307
9c541725 23308 die = follow_die_offset (sect_off,
36586728
TT
23309 (attr->form == DW_FORM_GNU_ref_alt
23310 || cu->per_cu->is_dwz),
23311 ref_cu);
5c631832 23312 if (!die)
9d8780f0
SM
23313 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23314 "at %s [in module %s]"),
23315 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23316 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23317
5c631832
JK
23318 return die;
23319}
23320
9c541725 23321/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23322 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23323 dwarf2_locexpr_baton->data has lifetime of
23324 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23325
23326struct dwarf2_locexpr_baton
9c541725 23327dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23328 struct dwarf2_per_cu_data *per_cu,
23329 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23330 void *baton, bool resolve_abstract_p)
5c631832 23331{
918dd910 23332 struct dwarf2_cu *cu;
5c631832
JK
23333 struct die_info *die;
23334 struct attribute *attr;
23335 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23336 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23337 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23338
918dd910 23339 if (per_cu->cu == NULL)
58f0c718 23340 load_cu (per_cu, false);
918dd910 23341 cu = per_cu->cu;
cc12ce38
DE
23342 if (cu == NULL)
23343 {
23344 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23345 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23346 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23347 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23348 }
918dd910 23349
9c541725 23350 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23351 if (!die)
9d8780f0
SM
23352 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23353 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23354
23355 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23356 if (!attr && resolve_abstract_p
3360b6e7 23357 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23358 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23359 {
23360 CORE_ADDR pc = (*get_frame_pc) (baton);
eba4caf2
TV
23361 CORE_ADDR baseaddr
23362 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23363 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23364
3360b6e7
TV
23365 for (const auto &cand_off
23366 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23367 {
3360b6e7
TV
23368 struct dwarf2_cu *cand_cu = cu;
23369 struct die_info *cand
23370 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23371 if (!cand
23372 || !cand->parent
e4a62c65
TV
23373 || cand->parent->tag != DW_TAG_subprogram)
23374 continue;
23375
23376 CORE_ADDR pc_low, pc_high;
23377 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23378 if (pc_low == ((CORE_ADDR) -1))
23379 continue;
23380 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23381 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23382 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23383 continue;
23384
23385 die = cand;
23386 attr = dwarf2_attr (die, DW_AT_location, cu);
23387 break;
23388 }
23389 }
23390
5c631832
JK
23391 if (!attr)
23392 {
e103e986
JK
23393 /* DWARF: "If there is no such attribute, then there is no effect.".
23394 DATA is ignored if SIZE is 0. */
5c631832 23395
e103e986 23396 retval.data = NULL;
5c631832
JK
23397 retval.size = 0;
23398 }
8cf6f0b1
TT
23399 else if (attr_form_is_section_offset (attr))
23400 {
23401 struct dwarf2_loclist_baton loclist_baton;
23402 CORE_ADDR pc = (*get_frame_pc) (baton);
23403 size_t size;
23404
23405 fill_in_loclist_baton (cu, &loclist_baton, attr);
23406
23407 retval.data = dwarf2_find_location_expression (&loclist_baton,
23408 &size, pc);
23409 retval.size = size;
23410 }
5c631832
JK
23411 else
23412 {
23413 if (!attr_form_is_block (attr))
9d8780f0 23414 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23415 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23416 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23417
23418 retval.data = DW_BLOCK (attr)->data;
23419 retval.size = DW_BLOCK (attr)->size;
23420 }
23421 retval.per_cu = cu->per_cu;
918dd910 23422
ed2dc618 23423 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23424
5c631832 23425 return retval;
348e048f
DE
23426}
23427
8b9737bf
TT
23428/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23429 offset. */
23430
23431struct dwarf2_locexpr_baton
23432dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23433 struct dwarf2_per_cu_data *per_cu,
23434 CORE_ADDR (*get_frame_pc) (void *baton),
23435 void *baton)
23436{
9c541725 23437 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23438
9c541725 23439 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23440}
23441
b6807d98
TT
23442/* Write a constant of a given type as target-ordered bytes into
23443 OBSTACK. */
23444
23445static const gdb_byte *
23446write_constant_as_bytes (struct obstack *obstack,
23447 enum bfd_endian byte_order,
23448 struct type *type,
23449 ULONGEST value,
23450 LONGEST *len)
23451{
23452 gdb_byte *result;
23453
23454 *len = TYPE_LENGTH (type);
224c3ddb 23455 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23456 store_unsigned_integer (result, *len, byte_order, value);
23457
23458 return result;
23459}
23460
23461/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23462 pointer to the constant bytes and set LEN to the length of the
23463 data. If memory is needed, allocate it on OBSTACK. If the DIE
23464 does not have a DW_AT_const_value, return NULL. */
23465
23466const gdb_byte *
9c541725 23467dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23468 struct dwarf2_per_cu_data *per_cu,
23469 struct obstack *obstack,
23470 LONGEST *len)
23471{
23472 struct dwarf2_cu *cu;
23473 struct die_info *die;
23474 struct attribute *attr;
23475 const gdb_byte *result = NULL;
23476 struct type *type;
23477 LONGEST value;
23478 enum bfd_endian byte_order;
e3b94546 23479 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23480
b6807d98 23481 if (per_cu->cu == NULL)
58f0c718 23482 load_cu (per_cu, false);
b6807d98 23483 cu = per_cu->cu;
cc12ce38
DE
23484 if (cu == NULL)
23485 {
23486 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23487 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23488 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23489 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23490 }
b6807d98 23491
9c541725 23492 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23493 if (!die)
9d8780f0
SM
23494 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23495 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23496
23497 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23498 if (attr == NULL)
23499 return NULL;
23500
e3b94546 23501 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23502 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23503
23504 switch (attr->form)
23505 {
23506 case DW_FORM_addr:
336d760d 23507 case DW_FORM_addrx:
b6807d98
TT
23508 case DW_FORM_GNU_addr_index:
23509 {
23510 gdb_byte *tem;
23511
23512 *len = cu->header.addr_size;
224c3ddb 23513 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23514 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23515 result = tem;
23516 }
23517 break;
23518 case DW_FORM_string:
23519 case DW_FORM_strp:
cf532bd1 23520 case DW_FORM_strx:
b6807d98
TT
23521 case DW_FORM_GNU_str_index:
23522 case DW_FORM_GNU_strp_alt:
23523 /* DW_STRING is already allocated on the objfile obstack, point
23524 directly to it. */
23525 result = (const gdb_byte *) DW_STRING (attr);
23526 *len = strlen (DW_STRING (attr));
23527 break;
23528 case DW_FORM_block1:
23529 case DW_FORM_block2:
23530 case DW_FORM_block4:
23531 case DW_FORM_block:
23532 case DW_FORM_exprloc:
0224619f 23533 case DW_FORM_data16:
b6807d98
TT
23534 result = DW_BLOCK (attr)->data;
23535 *len = DW_BLOCK (attr)->size;
23536 break;
23537
23538 /* The DW_AT_const_value attributes are supposed to carry the
23539 symbol's value "represented as it would be on the target
23540 architecture." By the time we get here, it's already been
23541 converted to host endianness, so we just need to sign- or
23542 zero-extend it as appropriate. */
23543 case DW_FORM_data1:
23544 type = die_type (die, cu);
23545 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23546 if (result == NULL)
23547 result = write_constant_as_bytes (obstack, byte_order,
23548 type, value, len);
23549 break;
23550 case DW_FORM_data2:
23551 type = die_type (die, cu);
23552 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23553 if (result == NULL)
23554 result = write_constant_as_bytes (obstack, byte_order,
23555 type, value, len);
23556 break;
23557 case DW_FORM_data4:
23558 type = die_type (die, cu);
23559 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23560 if (result == NULL)
23561 result = write_constant_as_bytes (obstack, byte_order,
23562 type, value, len);
23563 break;
23564 case DW_FORM_data8:
23565 type = die_type (die, cu);
23566 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23567 if (result == NULL)
23568 result = write_constant_as_bytes (obstack, byte_order,
23569 type, value, len);
23570 break;
23571
23572 case DW_FORM_sdata:
663c44ac 23573 case DW_FORM_implicit_const:
b6807d98
TT
23574 type = die_type (die, cu);
23575 result = write_constant_as_bytes (obstack, byte_order,
23576 type, DW_SND (attr), len);
23577 break;
23578
23579 case DW_FORM_udata:
23580 type = die_type (die, cu);
23581 result = write_constant_as_bytes (obstack, byte_order,
23582 type, DW_UNSND (attr), len);
23583 break;
23584
23585 default:
b98664d3 23586 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23587 dwarf_form_name (attr->form));
23588 break;
23589 }
23590
23591 return result;
23592}
23593
7942e96e
AA
23594/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23595 valid type for this die is found. */
23596
23597struct type *
9c541725 23598dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23599 struct dwarf2_per_cu_data *per_cu)
23600{
23601 struct dwarf2_cu *cu;
23602 struct die_info *die;
23603
7942e96e 23604 if (per_cu->cu == NULL)
58f0c718 23605 load_cu (per_cu, false);
7942e96e
AA
23606 cu = per_cu->cu;
23607 if (!cu)
23608 return NULL;
23609
9c541725 23610 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23611 if (!die)
23612 return NULL;
23613
23614 return die_type (die, cu);
23615}
23616
8a9b8146
TT
23617/* Return the type of the DIE at DIE_OFFSET in the CU named by
23618 PER_CU. */
23619
23620struct type *
b64f50a1 23621dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23622 struct dwarf2_per_cu_data *per_cu)
23623{
9c541725 23624 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23625 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23626}
23627
ac9ec31b 23628/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23629 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23630 On exit *REF_CU is the CU of the result.
23631 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23632
23633static struct die_info *
ac9ec31b
DE
23634follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23635 struct dwarf2_cu **ref_cu)
348e048f 23636{
348e048f 23637 struct die_info temp_die;
c24bdb02 23638 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23639 struct die_info *die;
23640
ac9ec31b
DE
23641 /* While it might be nice to assert sig_type->type == NULL here,
23642 we can get here for DW_AT_imported_declaration where we need
23643 the DIE not the type. */
348e048f
DE
23644
23645 /* If necessary, add it to the queue and load its DIEs. */
23646
95554aad 23647 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23648 read_signatured_type (sig_type);
348e048f 23649
348e048f 23650 sig_cu = sig_type->per_cu.cu;
69d751e3 23651 gdb_assert (sig_cu != NULL);
9c541725
PA
23652 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23653 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23654 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23655 to_underlying (temp_die.sect_off));
348e048f
DE
23656 if (die)
23657 {
ed2dc618 23658 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23659 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23660
796a7ff8
DE
23661 /* For .gdb_index version 7 keep track of included TUs.
23662 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23663 if (dwarf2_per_objfile->index_table != NULL
23664 && dwarf2_per_objfile->index_table->version <= 7)
23665 {
ae640021 23666 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23667 }
23668
348e048f 23669 *ref_cu = sig_cu;
c24bdb02
KS
23670 if (sig_cu != cu)
23671 sig_cu->ancestor = cu;
23672
348e048f
DE
23673 return die;
23674 }
23675
ac9ec31b
DE
23676 return NULL;
23677}
23678
23679/* Follow signatured type referenced by ATTR in SRC_DIE.
23680 On entry *REF_CU is the CU of SRC_DIE.
23681 On exit *REF_CU is the CU of the result.
23682 The result is the DIE of the type.
23683 If the referenced type cannot be found an error is thrown. */
23684
23685static struct die_info *
ff39bb5e 23686follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23687 struct dwarf2_cu **ref_cu)
23688{
23689 ULONGEST signature = DW_SIGNATURE (attr);
23690 struct signatured_type *sig_type;
23691 struct die_info *die;
23692
23693 gdb_assert (attr->form == DW_FORM_ref_sig8);
23694
a2ce51a0 23695 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23696 /* sig_type will be NULL if the signatured type is missing from
23697 the debug info. */
23698 if (sig_type == NULL)
23699 {
23700 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23701 " from DIE at %s [in module %s]"),
23702 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23703 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23704 }
23705
23706 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23707 if (die == NULL)
23708 {
23709 dump_die_for_error (src_die);
23710 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23711 " from DIE at %s [in module %s]"),
23712 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23713 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23714 }
23715
23716 return die;
23717}
23718
23719/* Get the type specified by SIGNATURE referenced in DIE/CU,
23720 reading in and processing the type unit if necessary. */
23721
23722static struct type *
23723get_signatured_type (struct die_info *die, ULONGEST signature,
23724 struct dwarf2_cu *cu)
23725{
518817b3
SM
23726 struct dwarf2_per_objfile *dwarf2_per_objfile
23727 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23728 struct signatured_type *sig_type;
23729 struct dwarf2_cu *type_cu;
23730 struct die_info *type_die;
23731 struct type *type;
23732
a2ce51a0 23733 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23734 /* sig_type will be NULL if the signatured type is missing from
23735 the debug info. */
23736 if (sig_type == NULL)
23737 {
b98664d3 23738 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23739 " from DIE at %s [in module %s]"),
23740 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23741 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23742 return build_error_marker_type (cu, die);
23743 }
23744
23745 /* If we already know the type we're done. */
23746 if (sig_type->type != NULL)
23747 return sig_type->type;
23748
23749 type_cu = cu;
23750 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23751 if (type_die != NULL)
23752 {
23753 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23754 is created. This is important, for example, because for c++ classes
23755 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23756 type = read_type_die (type_die, type_cu);
23757 if (type == NULL)
23758 {
b98664d3 23759 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23760 " referenced from DIE at %s [in module %s]"),
23761 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23762 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23763 type = build_error_marker_type (cu, die);
23764 }
23765 }
23766 else
23767 {
b98664d3 23768 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23769 " from DIE at %s [in module %s]"),
23770 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23771 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23772 type = build_error_marker_type (cu, die);
23773 }
23774 sig_type->type = type;
23775
23776 return type;
23777}
23778
23779/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23780 reading in and processing the type unit if necessary. */
23781
23782static struct type *
ff39bb5e 23783get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23784 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23785{
23786 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23787 if (attr_form_is_ref (attr))
ac9ec31b
DE
23788 {
23789 struct dwarf2_cu *type_cu = cu;
23790 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23791
23792 return read_type_die (type_die, type_cu);
23793 }
23794 else if (attr->form == DW_FORM_ref_sig8)
23795 {
23796 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23797 }
23798 else
23799 {
518817b3
SM
23800 struct dwarf2_per_objfile *dwarf2_per_objfile
23801 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23802
b98664d3 23803 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23804 " at %s [in module %s]"),
23805 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23806 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23807 return build_error_marker_type (cu, die);
23808 }
348e048f
DE
23809}
23810
e5fe5e75 23811/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23812
23813static void
e5fe5e75 23814load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23815{
52dc124a 23816 struct signatured_type *sig_type;
348e048f 23817
f4dc4d17
DE
23818 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23819 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23820
6721b2ec
DE
23821 /* We have the per_cu, but we need the signatured_type.
23822 Fortunately this is an easy translation. */
23823 gdb_assert (per_cu->is_debug_types);
23824 sig_type = (struct signatured_type *) per_cu;
348e048f 23825
6721b2ec 23826 gdb_assert (per_cu->cu == NULL);
348e048f 23827
52dc124a 23828 read_signatured_type (sig_type);
348e048f 23829
6721b2ec 23830 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23831}
23832
dee91e82
DE
23833/* die_reader_func for read_signatured_type.
23834 This is identical to load_full_comp_unit_reader,
23835 but is kept separate for now. */
348e048f
DE
23836
23837static void
dee91e82 23838read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23839 const gdb_byte *info_ptr,
dee91e82
DE
23840 struct die_info *comp_unit_die,
23841 int has_children,
23842 void *data)
348e048f 23843{
dee91e82 23844 struct dwarf2_cu *cu = reader->cu;
348e048f 23845
dee91e82
DE
23846 gdb_assert (cu->die_hash == NULL);
23847 cu->die_hash =
23848 htab_create_alloc_ex (cu->header.length / 12,
23849 die_hash,
23850 die_eq,
23851 NULL,
23852 &cu->comp_unit_obstack,
23853 hashtab_obstack_allocate,
23854 dummy_obstack_deallocate);
348e048f 23855
dee91e82
DE
23856 if (has_children)
23857 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23858 &info_ptr, comp_unit_die);
23859 cu->dies = comp_unit_die;
23860 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23861
23862 /* We try not to read any attributes in this function, because not
9cdd5dbd 23863 all CUs needed for references have been loaded yet, and symbol
348e048f 23864 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23865 or we won't be able to build types correctly.
23866 Similarly, if we do not read the producer, we can not apply
23867 producer-specific interpretation. */
95554aad 23868 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23869}
348e048f 23870
3019eac3
DE
23871/* Read in a signatured type and build its CU and DIEs.
23872 If the type is a stub for the real type in a DWO file,
23873 read in the real type from the DWO file as well. */
dee91e82
DE
23874
23875static void
23876read_signatured_type (struct signatured_type *sig_type)
23877{
23878 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23879
3019eac3 23880 gdb_assert (per_cu->is_debug_types);
dee91e82 23881 gdb_assert (per_cu->cu == NULL);
348e048f 23882
58f0c718 23883 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23884 read_signatured_type_reader, NULL);
7ee85ab1 23885 sig_type->per_cu.tu_read = 1;
c906108c
SS
23886}
23887
c906108c
SS
23888/* Decode simple location descriptions.
23889 Given a pointer to a dwarf block that defines a location, compute
23890 the location and return the value.
23891
4cecd739
DJ
23892 NOTE drow/2003-11-18: This function is called in two situations
23893 now: for the address of static or global variables (partial symbols
23894 only) and for offsets into structures which are expected to be
23895 (more or less) constant. The partial symbol case should go away,
23896 and only the constant case should remain. That will let this
23897 function complain more accurately. A few special modes are allowed
23898 without complaint for global variables (for instance, global
23899 register values and thread-local values).
c906108c
SS
23900
23901 A location description containing no operations indicates that the
4cecd739 23902 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23903 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23904 callers will only want a very basic result and this can become a
21ae7a4d
JK
23905 complaint.
23906
23907 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23908
23909static CORE_ADDR
e7c27a73 23910decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23911{
518817b3 23912 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23913 size_t i;
23914 size_t size = blk->size;
d521ce57 23915 const gdb_byte *data = blk->data;
21ae7a4d
JK
23916 CORE_ADDR stack[64];
23917 int stacki;
23918 unsigned int bytes_read, unsnd;
23919 gdb_byte op;
c906108c 23920
21ae7a4d
JK
23921 i = 0;
23922 stacki = 0;
23923 stack[stacki] = 0;
23924 stack[++stacki] = 0;
23925
23926 while (i < size)
23927 {
23928 op = data[i++];
23929 switch (op)
23930 {
23931 case DW_OP_lit0:
23932 case DW_OP_lit1:
23933 case DW_OP_lit2:
23934 case DW_OP_lit3:
23935 case DW_OP_lit4:
23936 case DW_OP_lit5:
23937 case DW_OP_lit6:
23938 case DW_OP_lit7:
23939 case DW_OP_lit8:
23940 case DW_OP_lit9:
23941 case DW_OP_lit10:
23942 case DW_OP_lit11:
23943 case DW_OP_lit12:
23944 case DW_OP_lit13:
23945 case DW_OP_lit14:
23946 case DW_OP_lit15:
23947 case DW_OP_lit16:
23948 case DW_OP_lit17:
23949 case DW_OP_lit18:
23950 case DW_OP_lit19:
23951 case DW_OP_lit20:
23952 case DW_OP_lit21:
23953 case DW_OP_lit22:
23954 case DW_OP_lit23:
23955 case DW_OP_lit24:
23956 case DW_OP_lit25:
23957 case DW_OP_lit26:
23958 case DW_OP_lit27:
23959 case DW_OP_lit28:
23960 case DW_OP_lit29:
23961 case DW_OP_lit30:
23962 case DW_OP_lit31:
23963 stack[++stacki] = op - DW_OP_lit0;
23964 break;
f1bea926 23965
21ae7a4d
JK
23966 case DW_OP_reg0:
23967 case DW_OP_reg1:
23968 case DW_OP_reg2:
23969 case DW_OP_reg3:
23970 case DW_OP_reg4:
23971 case DW_OP_reg5:
23972 case DW_OP_reg6:
23973 case DW_OP_reg7:
23974 case DW_OP_reg8:
23975 case DW_OP_reg9:
23976 case DW_OP_reg10:
23977 case DW_OP_reg11:
23978 case DW_OP_reg12:
23979 case DW_OP_reg13:
23980 case DW_OP_reg14:
23981 case DW_OP_reg15:
23982 case DW_OP_reg16:
23983 case DW_OP_reg17:
23984 case DW_OP_reg18:
23985 case DW_OP_reg19:
23986 case DW_OP_reg20:
23987 case DW_OP_reg21:
23988 case DW_OP_reg22:
23989 case DW_OP_reg23:
23990 case DW_OP_reg24:
23991 case DW_OP_reg25:
23992 case DW_OP_reg26:
23993 case DW_OP_reg27:
23994 case DW_OP_reg28:
23995 case DW_OP_reg29:
23996 case DW_OP_reg30:
23997 case DW_OP_reg31:
23998 stack[++stacki] = op - DW_OP_reg0;
23999 if (i < size)
24000 dwarf2_complex_location_expr_complaint ();
24001 break;
c906108c 24002
21ae7a4d
JK
24003 case DW_OP_regx:
24004 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24005 i += bytes_read;
24006 stack[++stacki] = unsnd;
24007 if (i < size)
24008 dwarf2_complex_location_expr_complaint ();
24009 break;
c906108c 24010
21ae7a4d
JK
24011 case DW_OP_addr:
24012 stack[++stacki] = read_address (objfile->obfd, &data[i],
24013 cu, &bytes_read);
24014 i += bytes_read;
24015 break;
d53d4ac5 24016
21ae7a4d
JK
24017 case DW_OP_const1u:
24018 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24019 i += 1;
24020 break;
24021
24022 case DW_OP_const1s:
24023 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24024 i += 1;
24025 break;
24026
24027 case DW_OP_const2u:
24028 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24029 i += 2;
24030 break;
24031
24032 case DW_OP_const2s:
24033 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24034 i += 2;
24035 break;
d53d4ac5 24036
21ae7a4d
JK
24037 case DW_OP_const4u:
24038 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24039 i += 4;
24040 break;
24041
24042 case DW_OP_const4s:
24043 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24044 i += 4;
24045 break;
24046
585861ea
JK
24047 case DW_OP_const8u:
24048 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24049 i += 8;
24050 break;
24051
21ae7a4d
JK
24052 case DW_OP_constu:
24053 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24054 &bytes_read);
24055 i += bytes_read;
24056 break;
24057
24058 case DW_OP_consts:
24059 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24060 i += bytes_read;
24061 break;
24062
24063 case DW_OP_dup:
24064 stack[stacki + 1] = stack[stacki];
24065 stacki++;
24066 break;
24067
24068 case DW_OP_plus:
24069 stack[stacki - 1] += stack[stacki];
24070 stacki--;
24071 break;
24072
24073 case DW_OP_plus_uconst:
24074 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24075 &bytes_read);
24076 i += bytes_read;
24077 break;
24078
24079 case DW_OP_minus:
24080 stack[stacki - 1] -= stack[stacki];
24081 stacki--;
24082 break;
24083
24084 case DW_OP_deref:
24085 /* If we're not the last op, then we definitely can't encode
24086 this using GDB's address_class enum. This is valid for partial
24087 global symbols, although the variable's address will be bogus
24088 in the psymtab. */
24089 if (i < size)
24090 dwarf2_complex_location_expr_complaint ();
24091 break;
24092
24093 case DW_OP_GNU_push_tls_address:
4aa4e28b 24094 case DW_OP_form_tls_address:
21ae7a4d
JK
24095 /* The top of the stack has the offset from the beginning
24096 of the thread control block at which the variable is located. */
24097 /* Nothing should follow this operator, so the top of stack would
24098 be returned. */
24099 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24100 address will be bogus in the psymtab. Make it always at least
24101 non-zero to not look as a variable garbage collected by linker
24102 which have DW_OP_addr 0. */
21ae7a4d
JK
24103 if (i < size)
24104 dwarf2_complex_location_expr_complaint ();
585861ea 24105 stack[stacki]++;
21ae7a4d
JK
24106 break;
24107
24108 case DW_OP_GNU_uninit:
24109 break;
24110
336d760d 24111 case DW_OP_addrx:
3019eac3 24112 case DW_OP_GNU_addr_index:
49f6c839 24113 case DW_OP_GNU_const_index:
3019eac3
DE
24114 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24115 &bytes_read);
24116 i += bytes_read;
24117 break;
24118
21ae7a4d
JK
24119 default:
24120 {
f39c6ffd 24121 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24122
24123 if (name)
b98664d3 24124 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24125 name);
24126 else
b98664d3 24127 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24128 op);
24129 }
24130
24131 return (stack[stacki]);
d53d4ac5 24132 }
3c6e0cb3 24133
21ae7a4d
JK
24134 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24135 outside of the allocated space. Also enforce minimum>0. */
24136 if (stacki >= ARRAY_SIZE (stack) - 1)
24137 {
b98664d3 24138 complaint (_("location description stack overflow"));
21ae7a4d
JK
24139 return 0;
24140 }
24141
24142 if (stacki <= 0)
24143 {
b98664d3 24144 complaint (_("location description stack underflow"));
21ae7a4d
JK
24145 return 0;
24146 }
24147 }
24148 return (stack[stacki]);
c906108c
SS
24149}
24150
24151/* memory allocation interface */
24152
c906108c 24153static struct dwarf_block *
7b5a2f43 24154dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24155{
8d749320 24156 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24157}
24158
c906108c 24159static struct die_info *
b60c80d6 24160dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24161{
24162 struct die_info *die;
b60c80d6
DJ
24163 size_t size = sizeof (struct die_info);
24164
24165 if (num_attrs > 1)
24166 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24167
b60c80d6 24168 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24169 memset (die, 0, sizeof (struct die_info));
24170 return (die);
24171}
2e276125
JB
24172
24173\f
24174/* Macro support. */
24175
233d95b5
JK
24176/* Return file name relative to the compilation directory of file number I in
24177 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24178 responsible for freeing it. */
233d95b5 24179
2e276125 24180static char *
233d95b5 24181file_file_name (int file, struct line_header *lh)
2e276125 24182{
6a83a1e6
EZ
24183 /* Is the file number a valid index into the line header's file name
24184 table? Remember that file numbers start with one, not zero. */
fff8551c 24185 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 24186 {
8c43009f 24187 const file_entry &fe = lh->file_names[file - 1];
6e70227d 24188
8c43009f
PA
24189 if (!IS_ABSOLUTE_PATH (fe.name))
24190 {
24191 const char *dir = fe.include_dir (lh);
24192 if (dir != NULL)
24193 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24194 }
24195 return xstrdup (fe.name);
6a83a1e6 24196 }
2e276125
JB
24197 else
24198 {
6a83a1e6
EZ
24199 /* The compiler produced a bogus file number. We can at least
24200 record the macro definitions made in the file, even if we
24201 won't be able to find the file by name. */
24202 char fake_name[80];
9a619af0 24203
8c042590
PM
24204 xsnprintf (fake_name, sizeof (fake_name),
24205 "<bad macro file number %d>", file);
2e276125 24206
b98664d3 24207 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24208 file);
2e276125 24209
6a83a1e6 24210 return xstrdup (fake_name);
2e276125
JB
24211 }
24212}
24213
233d95b5
JK
24214/* Return the full name of file number I in *LH's file name table.
24215 Use COMP_DIR as the name of the current directory of the
24216 compilation. The result is allocated using xmalloc; the caller is
24217 responsible for freeing it. */
24218static char *
24219file_full_name (int file, struct line_header *lh, const char *comp_dir)
24220{
24221 /* Is the file number a valid index into the line header's file name
24222 table? Remember that file numbers start with one, not zero. */
fff8551c 24223 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
24224 {
24225 char *relative = file_file_name (file, lh);
24226
24227 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24228 return relative;
b36cec19
PA
24229 return reconcat (relative, comp_dir, SLASH_STRING,
24230 relative, (char *) NULL);
233d95b5
JK
24231 }
24232 else
24233 return file_file_name (file, lh);
24234}
24235
2e276125
JB
24236
24237static struct macro_source_file *
804d2729
TT
24238macro_start_file (struct dwarf2_cu *cu,
24239 int file, int line,
2e276125 24240 struct macro_source_file *current_file,
43f3e411 24241 struct line_header *lh)
2e276125 24242{
233d95b5
JK
24243 /* File name relative to the compilation directory of this source file. */
24244 char *file_name = file_file_name (file, lh);
2e276125 24245
2e276125 24246 if (! current_file)
abc9d0dc 24247 {
fc474241
DE
24248 /* Note: We don't create a macro table for this compilation unit
24249 at all until we actually get a filename. */
c24bdb02 24250 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24251
abc9d0dc
TT
24252 /* If we have no current file, then this must be the start_file
24253 directive for the compilation unit's main source file. */
fc474241
DE
24254 current_file = macro_set_main (macro_table, file_name);
24255 macro_define_special (macro_table);
abc9d0dc 24256 }
2e276125 24257 else
233d95b5 24258 current_file = macro_include (current_file, line, file_name);
2e276125 24259
233d95b5 24260 xfree (file_name);
6e70227d 24261
2e276125
JB
24262 return current_file;
24263}
24264
2e276125
JB
24265static const char *
24266consume_improper_spaces (const char *p, const char *body)
24267{
24268 if (*p == ' ')
24269 {
b98664d3 24270 complaint (_("macro definition contains spaces "
3e43a32a 24271 "in formal argument list:\n`%s'"),
4d3c2250 24272 body);
2e276125
JB
24273
24274 while (*p == ' ')
24275 p++;
24276 }
24277
24278 return p;
24279}
24280
24281
24282static void
24283parse_macro_definition (struct macro_source_file *file, int line,
24284 const char *body)
24285{
24286 const char *p;
24287
24288 /* The body string takes one of two forms. For object-like macro
24289 definitions, it should be:
24290
24291 <macro name> " " <definition>
24292
24293 For function-like macro definitions, it should be:
24294
24295 <macro name> "() " <definition>
24296 or
24297 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24298
24299 Spaces may appear only where explicitly indicated, and in the
24300 <definition>.
24301
24302 The Dwarf 2 spec says that an object-like macro's name is always
24303 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24304 the space when the macro's definition is the empty string.
2e276125
JB
24305
24306 The Dwarf 2 spec says that there should be no spaces between the
24307 formal arguments in a function-like macro's formal argument list,
24308 but versions of GCC around March 2002 include spaces after the
24309 commas. */
24310
24311
24312 /* Find the extent of the macro name. The macro name is terminated
24313 by either a space or null character (for an object-like macro) or
24314 an opening paren (for a function-like macro). */
24315 for (p = body; *p; p++)
24316 if (*p == ' ' || *p == '(')
24317 break;
24318
24319 if (*p == ' ' || *p == '\0')
24320 {
24321 /* It's an object-like macro. */
24322 int name_len = p - body;
3f8a7804 24323 char *name = savestring (body, name_len);
2e276125
JB
24324 const char *replacement;
24325
24326 if (*p == ' ')
24327 replacement = body + name_len + 1;
24328 else
24329 {
4d3c2250 24330 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24331 replacement = body + name_len;
24332 }
6e70227d 24333
2e276125
JB
24334 macro_define_object (file, line, name, replacement);
24335
24336 xfree (name);
24337 }
24338 else if (*p == '(')
24339 {
24340 /* It's a function-like macro. */
3f8a7804 24341 char *name = savestring (body, p - body);
2e276125
JB
24342 int argc = 0;
24343 int argv_size = 1;
8d749320 24344 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24345
24346 p++;
24347
24348 p = consume_improper_spaces (p, body);
24349
24350 /* Parse the formal argument list. */
24351 while (*p && *p != ')')
24352 {
24353 /* Find the extent of the current argument name. */
24354 const char *arg_start = p;
24355
24356 while (*p && *p != ',' && *p != ')' && *p != ' ')
24357 p++;
24358
24359 if (! *p || p == arg_start)
4d3c2250 24360 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24361 else
24362 {
24363 /* Make sure argv has room for the new argument. */
24364 if (argc >= argv_size)
24365 {
24366 argv_size *= 2;
224c3ddb 24367 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24368 }
24369
3f8a7804 24370 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24371 }
24372
24373 p = consume_improper_spaces (p, body);
24374
24375 /* Consume the comma, if present. */
24376 if (*p == ',')
24377 {
24378 p++;
24379
24380 p = consume_improper_spaces (p, body);
24381 }
24382 }
24383
24384 if (*p == ')')
24385 {
24386 p++;
24387
24388 if (*p == ' ')
24389 /* Perfectly formed definition, no complaints. */
24390 macro_define_function (file, line, name,
6e70227d 24391 argc, (const char **) argv,
2e276125
JB
24392 p + 1);
24393 else if (*p == '\0')
24394 {
24395 /* Complain, but do define it. */
4d3c2250 24396 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24397 macro_define_function (file, line, name,
6e70227d 24398 argc, (const char **) argv,
2e276125
JB
24399 p);
24400 }
24401 else
24402 /* Just complain. */
4d3c2250 24403 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24404 }
24405 else
24406 /* Just complain. */
4d3c2250 24407 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24408
24409 xfree (name);
24410 {
24411 int i;
24412
24413 for (i = 0; i < argc; i++)
24414 xfree (argv[i]);
24415 }
24416 xfree (argv);
24417 }
24418 else
4d3c2250 24419 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24420}
24421
cf2c3c16
TT
24422/* Skip some bytes from BYTES according to the form given in FORM.
24423 Returns the new pointer. */
2e276125 24424
d521ce57
TT
24425static const gdb_byte *
24426skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24427 enum dwarf_form form,
24428 unsigned int offset_size,
24429 struct dwarf2_section_info *section)
2e276125 24430{
cf2c3c16 24431 unsigned int bytes_read;
2e276125 24432
cf2c3c16 24433 switch (form)
2e276125 24434 {
cf2c3c16
TT
24435 case DW_FORM_data1:
24436 case DW_FORM_flag:
24437 ++bytes;
24438 break;
24439
24440 case DW_FORM_data2:
24441 bytes += 2;
24442 break;
24443
24444 case DW_FORM_data4:
24445 bytes += 4;
24446 break;
24447
24448 case DW_FORM_data8:
24449 bytes += 8;
24450 break;
24451
0224619f
JK
24452 case DW_FORM_data16:
24453 bytes += 16;
24454 break;
24455
cf2c3c16
TT
24456 case DW_FORM_string:
24457 read_direct_string (abfd, bytes, &bytes_read);
24458 bytes += bytes_read;
24459 break;
24460
24461 case DW_FORM_sec_offset:
24462 case DW_FORM_strp:
36586728 24463 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24464 bytes += offset_size;
24465 break;
24466
24467 case DW_FORM_block:
24468 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24469 bytes += bytes_read;
24470 break;
24471
24472 case DW_FORM_block1:
24473 bytes += 1 + read_1_byte (abfd, bytes);
24474 break;
24475 case DW_FORM_block2:
24476 bytes += 2 + read_2_bytes (abfd, bytes);
24477 break;
24478 case DW_FORM_block4:
24479 bytes += 4 + read_4_bytes (abfd, bytes);
24480 break;
24481
336d760d 24482 case DW_FORM_addrx:
cf2c3c16 24483 case DW_FORM_sdata:
cf532bd1 24484 case DW_FORM_strx:
cf2c3c16 24485 case DW_FORM_udata:
3019eac3
DE
24486 case DW_FORM_GNU_addr_index:
24487 case DW_FORM_GNU_str_index:
d521ce57 24488 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24489 if (bytes == NULL)
24490 {
24491 dwarf2_section_buffer_overflow_complaint (section);
24492 return NULL;
24493 }
cf2c3c16
TT
24494 break;
24495
663c44ac
JK
24496 case DW_FORM_implicit_const:
24497 break;
24498
cf2c3c16
TT
24499 default:
24500 {
b98664d3 24501 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24502 form, get_section_name (section));
cf2c3c16
TT
24503 return NULL;
24504 }
2e276125
JB
24505 }
24506
cf2c3c16
TT
24507 return bytes;
24508}
757a13d0 24509
cf2c3c16
TT
24510/* A helper for dwarf_decode_macros that handles skipping an unknown
24511 opcode. Returns an updated pointer to the macro data buffer; or,
24512 on error, issues a complaint and returns NULL. */
757a13d0 24513
d521ce57 24514static const gdb_byte *
cf2c3c16 24515skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24516 const gdb_byte **opcode_definitions,
24517 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24518 bfd *abfd,
24519 unsigned int offset_size,
24520 struct dwarf2_section_info *section)
24521{
24522 unsigned int bytes_read, i;
24523 unsigned long arg;
d521ce57 24524 const gdb_byte *defn;
2e276125 24525
cf2c3c16 24526 if (opcode_definitions[opcode] == NULL)
2e276125 24527 {
b98664d3 24528 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24529 opcode);
24530 return NULL;
24531 }
2e276125 24532
cf2c3c16
TT
24533 defn = opcode_definitions[opcode];
24534 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24535 defn += bytes_read;
2e276125 24536
cf2c3c16
TT
24537 for (i = 0; i < arg; ++i)
24538 {
aead7601
SM
24539 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24540 (enum dwarf_form) defn[i], offset_size,
f664829e 24541 section);
cf2c3c16
TT
24542 if (mac_ptr == NULL)
24543 {
24544 /* skip_form_bytes already issued the complaint. */
24545 return NULL;
24546 }
24547 }
757a13d0 24548
cf2c3c16
TT
24549 return mac_ptr;
24550}
757a13d0 24551
cf2c3c16
TT
24552/* A helper function which parses the header of a macro section.
24553 If the macro section is the extended (for now called "GNU") type,
24554 then this updates *OFFSET_SIZE. Returns a pointer to just after
24555 the header, or issues a complaint and returns NULL on error. */
757a13d0 24556
d521ce57
TT
24557static const gdb_byte *
24558dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24559 bfd *abfd,
d521ce57 24560 const gdb_byte *mac_ptr,
cf2c3c16
TT
24561 unsigned int *offset_size,
24562 int section_is_gnu)
24563{
24564 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24565
cf2c3c16
TT
24566 if (section_is_gnu)
24567 {
24568 unsigned int version, flags;
757a13d0 24569
cf2c3c16 24570 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24571 if (version != 4 && version != 5)
cf2c3c16 24572 {
b98664d3 24573 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24574 version);
24575 return NULL;
24576 }
24577 mac_ptr += 2;
757a13d0 24578
cf2c3c16
TT
24579 flags = read_1_byte (abfd, mac_ptr);
24580 ++mac_ptr;
24581 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24582
cf2c3c16
TT
24583 if ((flags & 2) != 0)
24584 /* We don't need the line table offset. */
24585 mac_ptr += *offset_size;
757a13d0 24586
cf2c3c16
TT
24587 /* Vendor opcode descriptions. */
24588 if ((flags & 4) != 0)
24589 {
24590 unsigned int i, count;
757a13d0 24591
cf2c3c16
TT
24592 count = read_1_byte (abfd, mac_ptr);
24593 ++mac_ptr;
24594 for (i = 0; i < count; ++i)
24595 {
24596 unsigned int opcode, bytes_read;
24597 unsigned long arg;
24598
24599 opcode = read_1_byte (abfd, mac_ptr);
24600 ++mac_ptr;
24601 opcode_definitions[opcode] = mac_ptr;
24602 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24603 mac_ptr += bytes_read;
24604 mac_ptr += arg;
24605 }
757a13d0 24606 }
cf2c3c16 24607 }
757a13d0 24608
cf2c3c16
TT
24609 return mac_ptr;
24610}
757a13d0 24611
cf2c3c16 24612/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24613 including DW_MACRO_import. */
cf2c3c16
TT
24614
24615static void
804d2729 24616dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24617 bfd *abfd,
d521ce57 24618 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24619 struct macro_source_file *current_file,
43f3e411 24620 struct line_header *lh,
cf2c3c16 24621 struct dwarf2_section_info *section,
36586728 24622 int section_is_gnu, int section_is_dwz,
cf2c3c16 24623 unsigned int offset_size,
8fc3fc34 24624 htab_t include_hash)
cf2c3c16 24625{
804d2729
TT
24626 struct dwarf2_per_objfile *dwarf2_per_objfile
24627 = cu->per_cu->dwarf2_per_objfile;
4d663531 24628 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24629 enum dwarf_macro_record_type macinfo_type;
24630 int at_commandline;
d521ce57 24631 const gdb_byte *opcode_definitions[256];
757a13d0 24632
cf2c3c16
TT
24633 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24634 &offset_size, section_is_gnu);
24635 if (mac_ptr == NULL)
24636 {
24637 /* We already issued a complaint. */
24638 return;
24639 }
757a13d0
JK
24640
24641 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24642 GDB is still reading the definitions from command line. First
24643 DW_MACINFO_start_file will need to be ignored as it was already executed
24644 to create CURRENT_FILE for the main source holding also the command line
24645 definitions. On first met DW_MACINFO_start_file this flag is reset to
24646 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24647
24648 at_commandline = 1;
24649
24650 do
24651 {
24652 /* Do we at least have room for a macinfo type byte? */
24653 if (mac_ptr >= mac_end)
24654 {
f664829e 24655 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24656 break;
24657 }
24658
aead7601 24659 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24660 mac_ptr++;
24661
cf2c3c16
TT
24662 /* Note that we rely on the fact that the corresponding GNU and
24663 DWARF constants are the same. */
132448f8
SM
24664 DIAGNOSTIC_PUSH
24665 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24666 switch (macinfo_type)
24667 {
24668 /* A zero macinfo type indicates the end of the macro
24669 information. */
24670 case 0:
24671 break;
2e276125 24672
0af92d60
JK
24673 case DW_MACRO_define:
24674 case DW_MACRO_undef:
24675 case DW_MACRO_define_strp:
24676 case DW_MACRO_undef_strp:
24677 case DW_MACRO_define_sup:
24678 case DW_MACRO_undef_sup:
2e276125 24679 {
891d2f0b 24680 unsigned int bytes_read;
2e276125 24681 int line;
d521ce57 24682 const char *body;
cf2c3c16 24683 int is_define;
2e276125 24684
cf2c3c16
TT
24685 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24686 mac_ptr += bytes_read;
24687
0af92d60
JK
24688 if (macinfo_type == DW_MACRO_define
24689 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24690 {
24691 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24692 mac_ptr += bytes_read;
24693 }
24694 else
24695 {
24696 LONGEST str_offset;
24697
24698 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24699 mac_ptr += offset_size;
2e276125 24700
0af92d60
JK
24701 if (macinfo_type == DW_MACRO_define_sup
24702 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24703 || section_is_dwz)
36586728 24704 {
ed2dc618
SM
24705 struct dwz_file *dwz
24706 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24707
ed2dc618
SM
24708 body = read_indirect_string_from_dwz (objfile,
24709 dwz, str_offset);
36586728
TT
24710 }
24711 else
ed2dc618
SM
24712 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24713 abfd, str_offset);
cf2c3c16
TT
24714 }
24715
0af92d60
JK
24716 is_define = (macinfo_type == DW_MACRO_define
24717 || macinfo_type == DW_MACRO_define_strp
24718 || macinfo_type == DW_MACRO_define_sup);
2e276125 24719 if (! current_file)
757a13d0
JK
24720 {
24721 /* DWARF violation as no main source is present. */
b98664d3 24722 complaint (_("debug info with no main source gives macro %s "
757a13d0 24723 "on line %d: %s"),
cf2c3c16
TT
24724 is_define ? _("definition") : _("undefinition"),
24725 line, body);
757a13d0
JK
24726 break;
24727 }
3e43a32a
MS
24728 if ((line == 0 && !at_commandline)
24729 || (line != 0 && at_commandline))
b98664d3 24730 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24731 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24732 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24733 line == 0 ? _("zero") : _("non-zero"), line, body);
24734
955b06fa 24735 if (body == NULL)
7bede828 24736 {
955b06fa
SDJ
24737 /* Fedora's rpm-build's "debugedit" binary
24738 corrupted .debug_macro sections.
24739
24740 For more info, see
24741 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24742 complaint (_("debug info gives %s invalid macro %s "
24743 "without body (corrupted?) at line %d "
24744 "on file %s"),
24745 at_commandline ? _("command-line") : _("in-file"),
24746 is_define ? _("definition") : _("undefinition"),
24747 line, current_file->filename);
7bede828 24748 }
955b06fa
SDJ
24749 else if (is_define)
24750 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24751 else
24752 {
0af92d60
JK
24753 gdb_assert (macinfo_type == DW_MACRO_undef
24754 || macinfo_type == DW_MACRO_undef_strp
24755 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24756 macro_undef (current_file, line, body);
24757 }
2e276125
JB
24758 }
24759 break;
24760
0af92d60 24761 case DW_MACRO_start_file:
2e276125 24762 {
891d2f0b 24763 unsigned int bytes_read;
2e276125
JB
24764 int line, file;
24765
24766 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24767 mac_ptr += bytes_read;
24768 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24769 mac_ptr += bytes_read;
24770
3e43a32a
MS
24771 if ((line == 0 && !at_commandline)
24772 || (line != 0 && at_commandline))
b98664d3 24773 complaint (_("debug info gives source %d included "
757a13d0
JK
24774 "from %s at %s line %d"),
24775 file, at_commandline ? _("command-line") : _("file"),
24776 line == 0 ? _("zero") : _("non-zero"), line);
24777
24778 if (at_commandline)
24779 {
0af92d60 24780 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24781 pass one. */
757a13d0
JK
24782 at_commandline = 0;
24783 }
24784 else
804d2729
TT
24785 current_file = macro_start_file (cu, file, line, current_file,
24786 lh);
2e276125
JB
24787 }
24788 break;
24789
0af92d60 24790 case DW_MACRO_end_file:
2e276125 24791 if (! current_file)
b98664d3 24792 complaint (_("macro debug info has an unmatched "
3e43a32a 24793 "`close_file' directive"));
2e276125
JB
24794 else
24795 {
24796 current_file = current_file->included_by;
24797 if (! current_file)
24798 {
cf2c3c16 24799 enum dwarf_macro_record_type next_type;
2e276125
JB
24800
24801 /* GCC circa March 2002 doesn't produce the zero
24802 type byte marking the end of the compilation
24803 unit. Complain if it's not there, but exit no
24804 matter what. */
24805
24806 /* Do we at least have room for a macinfo type byte? */
24807 if (mac_ptr >= mac_end)
24808 {
f664829e 24809 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24810 return;
24811 }
24812
24813 /* We don't increment mac_ptr here, so this is just
24814 a look-ahead. */
aead7601
SM
24815 next_type
24816 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24817 mac_ptr);
2e276125 24818 if (next_type != 0)
b98664d3 24819 complaint (_("no terminating 0-type entry for "
3e43a32a 24820 "macros in `.debug_macinfo' section"));
2e276125
JB
24821
24822 return;
24823 }
24824 }
24825 break;
24826
0af92d60
JK
24827 case DW_MACRO_import:
24828 case DW_MACRO_import_sup:
cf2c3c16
TT
24829 {
24830 LONGEST offset;
8fc3fc34 24831 void **slot;
a036ba48
TT
24832 bfd *include_bfd = abfd;
24833 struct dwarf2_section_info *include_section = section;
d521ce57 24834 const gdb_byte *include_mac_end = mac_end;
a036ba48 24835 int is_dwz = section_is_dwz;
d521ce57 24836 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24837
24838 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24839 mac_ptr += offset_size;
24840
0af92d60 24841 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24842 {
ed2dc618 24843 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24844
4d663531 24845 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24846
a036ba48 24847 include_section = &dwz->macro;
a32a8923 24848 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24849 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24850 is_dwz = 1;
24851 }
24852
24853 new_mac_ptr = include_section->buffer + offset;
24854 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24855
8fc3fc34
TT
24856 if (*slot != NULL)
24857 {
24858 /* This has actually happened; see
24859 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24860 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24861 ".debug_macro section"));
24862 }
24863 else
24864 {
d521ce57 24865 *slot = (void *) new_mac_ptr;
36586728 24866
804d2729 24867 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24868 include_mac_end, current_file, lh,
36586728 24869 section, section_is_gnu, is_dwz,
4d663531 24870 offset_size, include_hash);
8fc3fc34 24871
d521ce57 24872 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24873 }
cf2c3c16
TT
24874 }
24875 break;
24876
2e276125 24877 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24878 if (!section_is_gnu)
24879 {
24880 unsigned int bytes_read;
2e276125 24881
ac298888
TT
24882 /* This reads the constant, but since we don't recognize
24883 any vendor extensions, we ignore it. */
24884 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24885 mac_ptr += bytes_read;
24886 read_direct_string (abfd, mac_ptr, &bytes_read);
24887 mac_ptr += bytes_read;
2e276125 24888
cf2c3c16
TT
24889 /* We don't recognize any vendor extensions. */
24890 break;
24891 }
24892 /* FALLTHROUGH */
24893
24894 default:
24895 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24896 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24897 section);
24898 if (mac_ptr == NULL)
24899 return;
24900 break;
2e276125 24901 }
132448f8 24902 DIAGNOSTIC_POP
757a13d0 24903 } while (macinfo_type != 0);
2e276125 24904}
8e19ed76 24905
cf2c3c16 24906static void
09262596 24907dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24908 int section_is_gnu)
cf2c3c16 24909{
518817b3
SM
24910 struct dwarf2_per_objfile *dwarf2_per_objfile
24911 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24912 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24913 struct line_header *lh = cu->line_header;
24914 bfd *abfd;
d521ce57 24915 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24916 struct macro_source_file *current_file = 0;
24917 enum dwarf_macro_record_type macinfo_type;
24918 unsigned int offset_size = cu->header.offset_size;
d521ce57 24919 const gdb_byte *opcode_definitions[256];
8fc3fc34 24920 void **slot;
09262596
DE
24921 struct dwarf2_section_info *section;
24922 const char *section_name;
24923
24924 if (cu->dwo_unit != NULL)
24925 {
24926 if (section_is_gnu)
24927 {
24928 section = &cu->dwo_unit->dwo_file->sections.macro;
24929 section_name = ".debug_macro.dwo";
24930 }
24931 else
24932 {
24933 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24934 section_name = ".debug_macinfo.dwo";
24935 }
24936 }
24937 else
24938 {
24939 if (section_is_gnu)
24940 {
24941 section = &dwarf2_per_objfile->macro;
24942 section_name = ".debug_macro";
24943 }
24944 else
24945 {
24946 section = &dwarf2_per_objfile->macinfo;
24947 section_name = ".debug_macinfo";
24948 }
24949 }
cf2c3c16 24950
bb5ed363 24951 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24952 if (section->buffer == NULL)
24953 {
b98664d3 24954 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24955 return;
24956 }
a32a8923 24957 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24958
24959 /* First pass: Find the name of the base filename.
24960 This filename is needed in order to process all macros whose definition
24961 (or undefinition) comes from the command line. These macros are defined
24962 before the first DW_MACINFO_start_file entry, and yet still need to be
24963 associated to the base file.
24964
24965 To determine the base file name, we scan the macro definitions until we
24966 reach the first DW_MACINFO_start_file entry. We then initialize
24967 CURRENT_FILE accordingly so that any macro definition found before the
24968 first DW_MACINFO_start_file can still be associated to the base file. */
24969
24970 mac_ptr = section->buffer + offset;
24971 mac_end = section->buffer + section->size;
24972
24973 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24974 &offset_size, section_is_gnu);
24975 if (mac_ptr == NULL)
24976 {
24977 /* We already issued a complaint. */
24978 return;
24979 }
24980
24981 do
24982 {
24983 /* Do we at least have room for a macinfo type byte? */
24984 if (mac_ptr >= mac_end)
24985 {
24986 /* Complaint is printed during the second pass as GDB will probably
24987 stop the first pass earlier upon finding
24988 DW_MACINFO_start_file. */
24989 break;
24990 }
24991
aead7601 24992 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24993 mac_ptr++;
24994
24995 /* Note that we rely on the fact that the corresponding GNU and
24996 DWARF constants are the same. */
132448f8
SM
24997 DIAGNOSTIC_PUSH
24998 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24999 switch (macinfo_type)
25000 {
25001 /* A zero macinfo type indicates the end of the macro
25002 information. */
25003 case 0:
25004 break;
25005
0af92d60
JK
25006 case DW_MACRO_define:
25007 case DW_MACRO_undef:
cf2c3c16
TT
25008 /* Only skip the data by MAC_PTR. */
25009 {
25010 unsigned int bytes_read;
25011
25012 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25013 mac_ptr += bytes_read;
25014 read_direct_string (abfd, mac_ptr, &bytes_read);
25015 mac_ptr += bytes_read;
25016 }
25017 break;
25018
0af92d60 25019 case DW_MACRO_start_file:
cf2c3c16
TT
25020 {
25021 unsigned int bytes_read;
25022 int line, file;
25023
25024 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25025 mac_ptr += bytes_read;
25026 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25027 mac_ptr += bytes_read;
25028
804d2729 25029 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25030 }
25031 break;
25032
0af92d60 25033 case DW_MACRO_end_file:
cf2c3c16
TT
25034 /* No data to skip by MAC_PTR. */
25035 break;
25036
0af92d60
JK
25037 case DW_MACRO_define_strp:
25038 case DW_MACRO_undef_strp:
25039 case DW_MACRO_define_sup:
25040 case DW_MACRO_undef_sup:
cf2c3c16
TT
25041 {
25042 unsigned int bytes_read;
25043
25044 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25045 mac_ptr += bytes_read;
25046 mac_ptr += offset_size;
25047 }
25048 break;
25049
0af92d60
JK
25050 case DW_MACRO_import:
25051 case DW_MACRO_import_sup:
cf2c3c16 25052 /* Note that, according to the spec, a transparent include
0af92d60 25053 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25054 skip this opcode. */
25055 mac_ptr += offset_size;
25056 break;
25057
25058 case DW_MACINFO_vendor_ext:
25059 /* Only skip the data by MAC_PTR. */
25060 if (!section_is_gnu)
25061 {
25062 unsigned int bytes_read;
25063
25064 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25065 mac_ptr += bytes_read;
25066 read_direct_string (abfd, mac_ptr, &bytes_read);
25067 mac_ptr += bytes_read;
25068 }
25069 /* FALLTHROUGH */
25070
25071 default:
25072 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25073 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25074 section);
25075 if (mac_ptr == NULL)
25076 return;
25077 break;
25078 }
132448f8 25079 DIAGNOSTIC_POP
cf2c3c16
TT
25080 } while (macinfo_type != 0 && current_file == NULL);
25081
25082 /* Second pass: Process all entries.
25083
25084 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25085 command-line macro definitions/undefinitions. This flag is unset when we
25086 reach the first DW_MACINFO_start_file entry. */
25087
fc4007c9
TT
25088 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25089 htab_eq_pointer,
25090 NULL, xcalloc, xfree));
8fc3fc34 25091 mac_ptr = section->buffer + offset;
fc4007c9 25092 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25093 *slot = (void *) mac_ptr;
804d2729 25094 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25095 current_file, lh, section,
fc4007c9
TT
25096 section_is_gnu, 0, offset_size,
25097 include_hash.get ());
cf2c3c16
TT
25098}
25099
8e19ed76 25100/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25101 if so return true else false. */
380bca97 25102
8e19ed76 25103static int
6e5a29e1 25104attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25105{
25106 return (attr == NULL ? 0 :
25107 attr->form == DW_FORM_block1
25108 || attr->form == DW_FORM_block2
25109 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25110 || attr->form == DW_FORM_block
25111 || attr->form == DW_FORM_exprloc);
8e19ed76 25112}
4c2df51b 25113
c6a0999f
JB
25114/* Return non-zero if ATTR's value is a section offset --- classes
25115 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25116 You may use DW_UNSND (attr) to retrieve such offsets.
25117
25118 Section 7.5.4, "Attribute Encodings", explains that no attribute
25119 may have a value that belongs to more than one of these classes; it
25120 would be ambiguous if we did, because we use the same forms for all
25121 of them. */
380bca97 25122
3690dd37 25123static int
6e5a29e1 25124attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25125{
25126 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25127 || attr->form == DW_FORM_data8
25128 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25129}
25130
3690dd37
JB
25131/* Return non-zero if ATTR's value falls in the 'constant' class, or
25132 zero otherwise. When this function returns true, you can apply
25133 dwarf2_get_attr_constant_value to it.
25134
25135 However, note that for some attributes you must check
25136 attr_form_is_section_offset before using this test. DW_FORM_data4
25137 and DW_FORM_data8 are members of both the constant class, and of
25138 the classes that contain offsets into other debug sections
25139 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25140 that, if an attribute's can be either a constant or one of the
25141 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25142 taken as section offsets, not constants.
25143
25144 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25145 cannot handle that. */
380bca97 25146
3690dd37 25147static int
6e5a29e1 25148attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25149{
25150 switch (attr->form)
25151 {
25152 case DW_FORM_sdata:
25153 case DW_FORM_udata:
25154 case DW_FORM_data1:
25155 case DW_FORM_data2:
25156 case DW_FORM_data4:
25157 case DW_FORM_data8:
663c44ac 25158 case DW_FORM_implicit_const:
3690dd37
JB
25159 return 1;
25160 default:
25161 return 0;
25162 }
25163}
25164
7771576e
SA
25165
25166/* DW_ADDR is always stored already as sect_offset; despite for the forms
25167 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25168
25169static int
6e5a29e1 25170attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25171{
25172 switch (attr->form)
25173 {
25174 case DW_FORM_ref_addr:
25175 case DW_FORM_ref1:
25176 case DW_FORM_ref2:
25177 case DW_FORM_ref4:
25178 case DW_FORM_ref8:
25179 case DW_FORM_ref_udata:
25180 case DW_FORM_GNU_ref_alt:
25181 return 1;
25182 default:
25183 return 0;
25184 }
25185}
25186
3019eac3
DE
25187/* Return the .debug_loc section to use for CU.
25188 For DWO files use .debug_loc.dwo. */
25189
25190static struct dwarf2_section_info *
25191cu_debug_loc_section (struct dwarf2_cu *cu)
25192{
518817b3
SM
25193 struct dwarf2_per_objfile *dwarf2_per_objfile
25194 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25195
3019eac3 25196 if (cu->dwo_unit)
43988095
JK
25197 {
25198 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25199
43988095
JK
25200 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25201 }
25202 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25203 : &dwarf2_per_objfile->loc);
3019eac3
DE
25204}
25205
8cf6f0b1
TT
25206/* A helper function that fills in a dwarf2_loclist_baton. */
25207
25208static void
25209fill_in_loclist_baton (struct dwarf2_cu *cu,
25210 struct dwarf2_loclist_baton *baton,
ff39bb5e 25211 const struct attribute *attr)
8cf6f0b1 25212{
518817b3
SM
25213 struct dwarf2_per_objfile *dwarf2_per_objfile
25214 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25215 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25216
25217 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25218
25219 baton->per_cu = cu->per_cu;
25220 gdb_assert (baton->per_cu);
25221 /* We don't know how long the location list is, but make sure we
25222 don't run off the edge of the section. */
3019eac3
DE
25223 baton->size = section->size - DW_UNSND (attr);
25224 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25225 baton->base_address = cu->base_address;
f664829e 25226 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25227}
25228
4c2df51b 25229static void
ff39bb5e 25230dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25231 struct dwarf2_cu *cu, int is_block)
4c2df51b 25232{
518817b3
SM
25233 struct dwarf2_per_objfile *dwarf2_per_objfile
25234 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25235 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25236 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25237
3690dd37 25238 if (attr_form_is_section_offset (attr)
3019eac3 25239 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25240 the section. If so, fall through to the complaint in the
25241 other branch. */
3019eac3 25242 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25243 {
0d53c4c4 25244 struct dwarf2_loclist_baton *baton;
4c2df51b 25245
8d749320 25246 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25247
8cf6f0b1 25248 fill_in_loclist_baton (cu, baton, attr);
be391dca 25249
d00adf39 25250 if (cu->base_known == 0)
b98664d3 25251 complaint (_("Location list used without "
3e43a32a 25252 "specifying the CU base address."));
4c2df51b 25253
f1e6e072
TT
25254 SYMBOL_ACLASS_INDEX (sym) = (is_block
25255 ? dwarf2_loclist_block_index
25256 : dwarf2_loclist_index);
0d53c4c4
DJ
25257 SYMBOL_LOCATION_BATON (sym) = baton;
25258 }
25259 else
25260 {
25261 struct dwarf2_locexpr_baton *baton;
25262
8d749320 25263 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25264 baton->per_cu = cu->per_cu;
25265 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25266
25267 if (attr_form_is_block (attr))
25268 {
25269 /* Note that we're just copying the block's data pointer
25270 here, not the actual data. We're still pointing into the
6502dd73
DJ
25271 info_buffer for SYM's objfile; right now we never release
25272 that buffer, but when we do clean up properly this may
25273 need to change. */
0d53c4c4
DJ
25274 baton->size = DW_BLOCK (attr)->size;
25275 baton->data = DW_BLOCK (attr)->data;
25276 }
25277 else
25278 {
25279 dwarf2_invalid_attrib_class_complaint ("location description",
25280 SYMBOL_NATURAL_NAME (sym));
25281 baton->size = 0;
0d53c4c4 25282 }
6e70227d 25283
f1e6e072
TT
25284 SYMBOL_ACLASS_INDEX (sym) = (is_block
25285 ? dwarf2_locexpr_block_index
25286 : dwarf2_locexpr_index);
0d53c4c4
DJ
25287 SYMBOL_LOCATION_BATON (sym) = baton;
25288 }
4c2df51b 25289}
6502dd73 25290
9aa1f1e3
TT
25291/* Return the OBJFILE associated with the compilation unit CU. If CU
25292 came from a separate debuginfo file, then the master objfile is
25293 returned. */
ae0d2f24
UW
25294
25295struct objfile *
25296dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25297{
e3b94546 25298 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25299
25300 /* Return the master objfile, so that we can report and look up the
25301 correct file containing this variable. */
25302 if (objfile->separate_debug_objfile_backlink)
25303 objfile = objfile->separate_debug_objfile_backlink;
25304
25305 return objfile;
25306}
25307
96408a79
SA
25308/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25309 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25310 CU_HEADERP first. */
25311
25312static const struct comp_unit_head *
25313per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25314 struct dwarf2_per_cu_data *per_cu)
25315{
d521ce57 25316 const gdb_byte *info_ptr;
96408a79
SA
25317
25318 if (per_cu->cu)
25319 return &per_cu->cu->header;
25320
9c541725 25321 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25322
25323 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25324 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25325 rcuh_kind::COMPILE);
96408a79
SA
25326
25327 return cu_headerp;
25328}
25329
ae0d2f24
UW
25330/* Return the address size given in the compilation unit header for CU. */
25331
98714339 25332int
ae0d2f24
UW
25333dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25334{
96408a79
SA
25335 struct comp_unit_head cu_header_local;
25336 const struct comp_unit_head *cu_headerp;
c471e790 25337
96408a79
SA
25338 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25339
25340 return cu_headerp->addr_size;
ae0d2f24
UW
25341}
25342
9eae7c52
TT
25343/* Return the offset size given in the compilation unit header for CU. */
25344
25345int
25346dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25347{
96408a79
SA
25348 struct comp_unit_head cu_header_local;
25349 const struct comp_unit_head *cu_headerp;
9c6c53f7 25350
96408a79
SA
25351 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25352
25353 return cu_headerp->offset_size;
25354}
25355
25356/* See its dwarf2loc.h declaration. */
25357
25358int
25359dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25360{
25361 struct comp_unit_head cu_header_local;
25362 const struct comp_unit_head *cu_headerp;
25363
25364 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25365
25366 if (cu_headerp->version == 2)
25367 return cu_headerp->addr_size;
25368 else
25369 return cu_headerp->offset_size;
181cebd4
JK
25370}
25371
9aa1f1e3
TT
25372/* Return the text offset of the CU. The returned offset comes from
25373 this CU's objfile. If this objfile came from a separate debuginfo
25374 file, then the offset may be different from the corresponding
25375 offset in the parent objfile. */
25376
25377CORE_ADDR
25378dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25379{
e3b94546 25380 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25381
25382 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25383}
25384
9a49df9d
AB
25385/* Return a type that is a generic pointer type, the size of which matches
25386 the address size given in the compilation unit header for PER_CU. */
25387static struct type *
25388dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25389{
25390 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25391 struct type *void_type = objfile_type (objfile)->builtin_void;
25392 struct type *addr_type = lookup_pointer_type (void_type);
25393 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25394
25395 if (TYPE_LENGTH (addr_type) == addr_size)
25396 return addr_type;
25397
25398 addr_type
25399 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25400 return addr_type;
25401}
25402
43988095
JK
25403/* Return DWARF version number of PER_CU. */
25404
25405short
25406dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25407{
25408 return per_cu->dwarf_version;
25409}
25410
348e048f
DE
25411/* Locate the .debug_info compilation unit from CU's objfile which contains
25412 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25413
25414static struct dwarf2_per_cu_data *
9c541725 25415dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25416 unsigned int offset_in_dwz,
ed2dc618 25417 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25418{
25419 struct dwarf2_per_cu_data *this_cu;
25420 int low, high;
25421
ae038cb0 25422 low = 0;
b76e467d 25423 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25424 while (high > low)
25425 {
36586728 25426 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25427 int mid = low + (high - low) / 2;
9a619af0 25428
36586728 25429 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25430 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25431 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25432 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25433 high = mid;
25434 else
25435 low = mid + 1;
25436 }
25437 gdb_assert (low == high);
36586728 25438 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25439 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25440 {
36586728 25441 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25442 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25443 "offset %s [in module %s]"),
25444 sect_offset_str (sect_off),
ed2dc618 25445 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25446
9c541725
PA
25447 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25448 <= sect_off);
ae038cb0
DJ
25449 return dwarf2_per_objfile->all_comp_units[low-1];
25450 }
25451 else
25452 {
b76e467d 25453 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25454 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25455 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25456 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25457 return this_cu;
25458 }
25459}
25460
23745b47 25461/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25462
fcd3b13d
SM
25463dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25464 : per_cu (per_cu_),
9068261f
AB
25465 mark (false),
25466 has_loclist (false),
25467 checked_producer (false),
25468 producer_is_gxx_lt_4_6 (false),
25469 producer_is_gcc_lt_4_3 (false),
eb77c9df 25470 producer_is_icc (false),
9068261f 25471 producer_is_icc_lt_14 (false),
c258c396 25472 producer_is_codewarrior (false),
9068261f 25473 processing_has_namespace_info (false)
93311388 25474{
fcd3b13d
SM
25475 per_cu->cu = this;
25476}
25477
25478/* Destroy a dwarf2_cu. */
25479
25480dwarf2_cu::~dwarf2_cu ()
25481{
25482 per_cu->cu = NULL;
9816fde3
JK
25483}
25484
25485/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25486
25487static void
95554aad
TT
25488prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25489 enum language pretend_language)
9816fde3
JK
25490{
25491 struct attribute *attr;
25492
25493 /* Set the language we're debugging. */
25494 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25495 if (attr)
25496 set_cu_language (DW_UNSND (attr), cu);
25497 else
9cded63f 25498 {
95554aad 25499 cu->language = pretend_language;
9cded63f
TT
25500 cu->language_defn = language_def (cu->language);
25501 }
dee91e82 25502
7d45c7c3 25503 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25504}
25505
ae038cb0
DJ
25506/* Increase the age counter on each cached compilation unit, and free
25507 any that are too old. */
25508
25509static void
ed2dc618 25510age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25511{
25512 struct dwarf2_per_cu_data *per_cu, **last_chain;
25513
25514 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25515 per_cu = dwarf2_per_objfile->read_in_chain;
25516 while (per_cu != NULL)
25517 {
25518 per_cu->cu->last_used ++;
b4f54984 25519 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25520 dwarf2_mark (per_cu->cu);
25521 per_cu = per_cu->cu->read_in_chain;
25522 }
25523
25524 per_cu = dwarf2_per_objfile->read_in_chain;
25525 last_chain = &dwarf2_per_objfile->read_in_chain;
25526 while (per_cu != NULL)
25527 {
25528 struct dwarf2_per_cu_data *next_cu;
25529
25530 next_cu = per_cu->cu->read_in_chain;
25531
25532 if (!per_cu->cu->mark)
25533 {
fcd3b13d 25534 delete per_cu->cu;
ae038cb0
DJ
25535 *last_chain = next_cu;
25536 }
25537 else
25538 last_chain = &per_cu->cu->read_in_chain;
25539
25540 per_cu = next_cu;
25541 }
25542}
25543
25544/* Remove a single compilation unit from the cache. */
25545
25546static void
dee91e82 25547free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25548{
25549 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25550 struct dwarf2_per_objfile *dwarf2_per_objfile
25551 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25552
25553 per_cu = dwarf2_per_objfile->read_in_chain;
25554 last_chain = &dwarf2_per_objfile->read_in_chain;
25555 while (per_cu != NULL)
25556 {
25557 struct dwarf2_per_cu_data *next_cu;
25558
25559 next_cu = per_cu->cu->read_in_chain;
25560
dee91e82 25561 if (per_cu == target_per_cu)
ae038cb0 25562 {
fcd3b13d 25563 delete per_cu->cu;
dee91e82 25564 per_cu->cu = NULL;
ae038cb0
DJ
25565 *last_chain = next_cu;
25566 break;
25567 }
25568 else
25569 last_chain = &per_cu->cu->read_in_chain;
25570
25571 per_cu = next_cu;
25572 }
25573}
25574
dee91e82
DE
25575/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25576 We store these in a hash table separate from the DIEs, and preserve them
25577 when the DIEs are flushed out of cache.
25578
25579 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25580 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25581 or the type may come from a DWO file. Furthermore, while it's more logical
25582 to use per_cu->section+offset, with Fission the section with the data is in
25583 the DWO file but we don't know that section at the point we need it.
25584 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25585 because we can enter the lookup routine, get_die_type_at_offset, from
25586 outside this file, and thus won't necessarily have PER_CU->cu.
25587 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25588
dee91e82 25589struct dwarf2_per_cu_offset_and_type
1c379e20 25590{
dee91e82 25591 const struct dwarf2_per_cu_data *per_cu;
9c541725 25592 sect_offset sect_off;
1c379e20
DJ
25593 struct type *type;
25594};
25595
dee91e82 25596/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25597
25598static hashval_t
dee91e82 25599per_cu_offset_and_type_hash (const void *item)
1c379e20 25600{
9a3c8263
SM
25601 const struct dwarf2_per_cu_offset_and_type *ofs
25602 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25603
9c541725 25604 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25605}
25606
dee91e82 25607/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25608
25609static int
dee91e82 25610per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25611{
9a3c8263
SM
25612 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25613 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25614 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25615 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25616
dee91e82 25617 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25618 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25619}
25620
25621/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25622 table if necessary. For convenience, return TYPE.
25623
25624 The DIEs reading must have careful ordering to:
25625 * Not cause infite loops trying to read in DIEs as a prerequisite for
25626 reading current DIE.
25627 * Not trying to dereference contents of still incompletely read in types
25628 while reading in other DIEs.
25629 * Enable referencing still incompletely read in types just by a pointer to
25630 the type without accessing its fields.
25631
25632 Therefore caller should follow these rules:
25633 * Try to fetch any prerequisite types we may need to build this DIE type
25634 before building the type and calling set_die_type.
e71ec853 25635 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25636 possible before fetching more types to complete the current type.
25637 * Make the type as complete as possible before fetching more types. */
1c379e20 25638
f792889a 25639static struct type *
1c379e20
DJ
25640set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25641{
518817b3
SM
25642 struct dwarf2_per_objfile *dwarf2_per_objfile
25643 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25644 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25645 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25646 struct attribute *attr;
25647 struct dynamic_prop prop;
1c379e20 25648
b4ba55a1
JB
25649 /* For Ada types, make sure that the gnat-specific data is always
25650 initialized (if not already set). There are a few types where
25651 we should not be doing so, because the type-specific area is
25652 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25653 where the type-specific area is used to store the floatformat).
25654 But this is not a problem, because the gnat-specific information
25655 is actually not needed for these types. */
25656 if (need_gnat_info (cu)
25657 && TYPE_CODE (type) != TYPE_CODE_FUNC
25658 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25659 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25660 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25661 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25662 && !HAVE_GNAT_AUX_INFO (type))
25663 INIT_GNAT_SPECIFIC (type);
25664
3f2f83dd
KB
25665 /* Read DW_AT_allocated and set in type. */
25666 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25667 if (attr_form_is_block (attr))
25668 {
9a49df9d
AB
25669 struct type *prop_type
25670 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25671 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25672 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25673 }
25674 else if (attr != NULL)
25675 {
b98664d3 25676 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25677 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25678 sect_offset_str (die->sect_off));
3f2f83dd
KB
25679 }
25680
25681 /* Read DW_AT_associated and set in type. */
25682 attr = dwarf2_attr (die, DW_AT_associated, cu);
25683 if (attr_form_is_block (attr))
25684 {
9a49df9d
AB
25685 struct type *prop_type
25686 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25687 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25688 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25689 }
25690 else if (attr != NULL)
25691 {
b98664d3 25692 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25693 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25694 sect_offset_str (die->sect_off));
3f2f83dd
KB
25695 }
25696
3cdcd0ce
JB
25697 /* Read DW_AT_data_location and set in type. */
25698 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25699 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25700 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25701 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25702
dee91e82 25703 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25704 {
dee91e82
DE
25705 dwarf2_per_objfile->die_type_hash =
25706 htab_create_alloc_ex (127,
25707 per_cu_offset_and_type_hash,
25708 per_cu_offset_and_type_eq,
25709 NULL,
25710 &objfile->objfile_obstack,
25711 hashtab_obstack_allocate,
25712 dummy_obstack_deallocate);
f792889a 25713 }
1c379e20 25714
dee91e82 25715 ofs.per_cu = cu->per_cu;
9c541725 25716 ofs.sect_off = die->sect_off;
1c379e20 25717 ofs.type = type;
dee91e82
DE
25718 slot = (struct dwarf2_per_cu_offset_and_type **)
25719 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25720 if (*slot)
b98664d3 25721 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25722 sect_offset_str (die->sect_off));
8d749320
SM
25723 *slot = XOBNEW (&objfile->objfile_obstack,
25724 struct dwarf2_per_cu_offset_and_type);
1c379e20 25725 **slot = ofs;
f792889a 25726 return type;
1c379e20
DJ
25727}
25728
9c541725 25729/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25730 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25731
25732static struct type *
9c541725 25733get_die_type_at_offset (sect_offset sect_off,
673bfd45 25734 struct dwarf2_per_cu_data *per_cu)
1c379e20 25735{
dee91e82 25736 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25737 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25738
dee91e82 25739 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25740 return NULL;
1c379e20 25741
dee91e82 25742 ofs.per_cu = per_cu;
9c541725 25743 ofs.sect_off = sect_off;
9a3c8263
SM
25744 slot = ((struct dwarf2_per_cu_offset_and_type *)
25745 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25746 if (slot)
25747 return slot->type;
25748 else
25749 return NULL;
25750}
25751
02142a6c 25752/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25753 or return NULL if DIE does not have a saved type. */
25754
25755static struct type *
25756get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25757{
9c541725 25758 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25759}
25760
10b3939b
DJ
25761/* Add a dependence relationship from CU to REF_PER_CU. */
25762
25763static void
25764dwarf2_add_dependence (struct dwarf2_cu *cu,
25765 struct dwarf2_per_cu_data *ref_per_cu)
25766{
25767 void **slot;
25768
25769 if (cu->dependencies == NULL)
25770 cu->dependencies
25771 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25772 NULL, &cu->comp_unit_obstack,
25773 hashtab_obstack_allocate,
25774 dummy_obstack_deallocate);
25775
25776 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25777 if (*slot == NULL)
25778 *slot = ref_per_cu;
25779}
1c379e20 25780
f504f079
DE
25781/* Subroutine of dwarf2_mark to pass to htab_traverse.
25782 Set the mark field in every compilation unit in the
ae038cb0
DJ
25783 cache that we must keep because we are keeping CU. */
25784
10b3939b
DJ
25785static int
25786dwarf2_mark_helper (void **slot, void *data)
25787{
25788 struct dwarf2_per_cu_data *per_cu;
25789
25790 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25791
25792 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25793 reading of the chain. As such dependencies remain valid it is not much
25794 useful to track and undo them during QUIT cleanups. */
25795 if (per_cu->cu == NULL)
25796 return 1;
25797
10b3939b
DJ
25798 if (per_cu->cu->mark)
25799 return 1;
9068261f 25800 per_cu->cu->mark = true;
10b3939b
DJ
25801
25802 if (per_cu->cu->dependencies != NULL)
25803 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25804
25805 return 1;
25806}
25807
f504f079
DE
25808/* Set the mark field in CU and in every other compilation unit in the
25809 cache that we must keep because we are keeping CU. */
25810
ae038cb0
DJ
25811static void
25812dwarf2_mark (struct dwarf2_cu *cu)
25813{
25814 if (cu->mark)
25815 return;
9068261f 25816 cu->mark = true;
10b3939b
DJ
25817 if (cu->dependencies != NULL)
25818 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25819}
25820
25821static void
25822dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25823{
25824 while (per_cu)
25825 {
9068261f 25826 per_cu->cu->mark = false;
ae038cb0
DJ
25827 per_cu = per_cu->cu->read_in_chain;
25828 }
72bf9492
DJ
25829}
25830
72bf9492
DJ
25831/* Trivial hash function for partial_die_info: the hash value of a DIE
25832 is its offset in .debug_info for this objfile. */
25833
25834static hashval_t
25835partial_die_hash (const void *item)
25836{
9a3c8263
SM
25837 const struct partial_die_info *part_die
25838 = (const struct partial_die_info *) item;
9a619af0 25839
9c541725 25840 return to_underlying (part_die->sect_off);
72bf9492
DJ
25841}
25842
25843/* Trivial comparison function for partial_die_info structures: two DIEs
25844 are equal if they have the same offset. */
25845
25846static int
25847partial_die_eq (const void *item_lhs, const void *item_rhs)
25848{
9a3c8263
SM
25849 const struct partial_die_info *part_die_lhs
25850 = (const struct partial_die_info *) item_lhs;
25851 const struct partial_die_info *part_die_rhs
25852 = (const struct partial_die_info *) item_rhs;
9a619af0 25853
9c541725 25854 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25855}
25856
3c3bb058
AB
25857struct cmd_list_element *set_dwarf_cmdlist;
25858struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25859
25860static void
981a3fb3 25861set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25862{
b4f54984 25863 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25864 gdb_stdout);
ae038cb0
DJ
25865}
25866
25867static void
981a3fb3 25868show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25869{
b4f54984 25870 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25871}
25872
491144b5 25873bool dwarf_always_disassemble;
437afbb8 25874
437afbb8 25875static void
cd4fb1b2
SM
25876show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25877 struct cmd_list_element *c, const char *value)
9291a0cd 25878{
cd4fb1b2
SM
25879 fprintf_filtered (file,
25880 _("Whether to always disassemble "
25881 "DWARF expressions is %s.\n"),
25882 value);
9291a0cd
TT
25883}
25884
9291a0cd 25885static void
cd4fb1b2
SM
25886show_check_physname (struct ui_file *file, int from_tty,
25887 struct cmd_list_element *c, const char *value)
9291a0cd 25888{
cd4fb1b2
SM
25889 fprintf_filtered (file,
25890 _("Whether to check \"physname\" is %s.\n"),
25891 value);
9291a0cd
TT
25892}
25893
cd4fb1b2
SM
25894void
25895_initialize_dwarf2_read (void)
9291a0cd 25896{
cd4fb1b2
SM
25897 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25898Set DWARF specific variables.\n\
590042fc 25899Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
25900 &set_dwarf_cmdlist, "maintenance set dwarf ",
25901 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25902
cd4fb1b2 25903 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
25904Show DWARF specific variables.\n\
25905Show DWARF variables such as the cache size."),
cd4fb1b2
SM
25906 &show_dwarf_cmdlist, "maintenance show dwarf ",
25907 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25908
cd4fb1b2
SM
25909 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25910 &dwarf_max_cache_age, _("\
25911Set the upper bound on the age of cached DWARF compilation units."), _("\
25912Show the upper bound on the age of cached DWARF compilation units."), _("\
25913A higher limit means that cached compilation units will be stored\n\
25914in memory longer, and more total memory will be used. Zero disables\n\
25915caching, which can slow down startup."),
25916 NULL,
25917 show_dwarf_max_cache_age,
25918 &set_dwarf_cmdlist,
25919 &show_dwarf_cmdlist);
156942c7 25920
cd4fb1b2
SM
25921 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25922 &dwarf_always_disassemble, _("\
25923Set whether `info address' always disassembles DWARF expressions."), _("\
25924Show whether `info address' always disassembles DWARF expressions."), _("\
25925When enabled, DWARF expressions are always printed in an assembly-like\n\
25926syntax. When disabled, expressions will be printed in a more\n\
25927conversational style, when possible."),
25928 NULL,
25929 show_dwarf_always_disassemble,
25930 &set_dwarf_cmdlist,
25931 &show_dwarf_cmdlist);
9291a0cd 25932
cd4fb1b2
SM
25933 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25934Set debugging of the DWARF reader."), _("\
25935Show debugging of the DWARF reader."), _("\
25936When enabled (non-zero), debugging messages are printed during DWARF\n\
25937reading and symtab expansion. A value of 1 (one) provides basic\n\
25938information. A value greater than 1 provides more verbose information."),
25939 NULL,
25940 NULL,
25941 &setdebuglist, &showdebuglist);
9291a0cd 25942
cd4fb1b2
SM
25943 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25944Set debugging of the DWARF DIE reader."), _("\
25945Show debugging of the DWARF DIE reader."), _("\
25946When enabled (non-zero), DIEs are dumped after they are read in.\n\
25947The value is the maximum depth to print."),
25948 NULL,
25949 NULL,
25950 &setdebuglist, &showdebuglist);
9291a0cd 25951
cd4fb1b2
SM
25952 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25953Set debugging of the dwarf line reader."), _("\
25954Show debugging of the dwarf line reader."), _("\
25955When enabled (non-zero), line number entries are dumped as they are read in.\n\
25956A value of 1 (one) provides basic information.\n\
25957A value greater than 1 provides more verbose information."),
25958 NULL,
25959 NULL,
25960 &setdebuglist, &showdebuglist);
437afbb8 25961
cd4fb1b2
SM
25962 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25963Set cross-checking of \"physname\" code against demangler."), _("\
25964Show cross-checking of \"physname\" code against demangler."), _("\
25965When enabled, GDB's internal \"physname\" code is checked against\n\
25966the demangler."),
25967 NULL, show_check_physname,
25968 &setdebuglist, &showdebuglist);
900e11f9 25969
e615022a
DE
25970 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25971 no_class, &use_deprecated_index_sections, _("\
25972Set whether to use deprecated gdb_index sections."), _("\
25973Show whether to use deprecated gdb_index sections."), _("\
25974When enabled, deprecated .gdb_index sections are used anyway.\n\
25975Normally they are ignored either because of a missing feature or\n\
25976performance issue.\n\
25977Warning: This option must be enabled before gdb reads the file."),
25978 NULL,
25979 NULL,
25980 &setlist, &showlist);
25981
f1e6e072
TT
25982 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25983 &dwarf2_locexpr_funcs);
25984 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25985 &dwarf2_loclist_funcs);
25986
25987 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25988 &dwarf2_block_frame_base_locexpr_funcs);
25989 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25990 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25991
25992#if GDB_SELF_TEST
25993 selftests::register_test ("dw2_expand_symtabs_matching",
25994 selftests::dw2_expand_symtabs_matching::run_test);
25995#endif
6502dd73 25996}