Consolidate psymtab "Reading" messages
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
4de283e4 32#include "dwarf2read.h"
d55e5aa6
TT
33#include "dwarf-index-cache.h"
34#include "dwarf-index-common.h"
4de283e4
TT
35#include "bfd.h"
36#include "elf-bfd.h"
37#include "symtab.h"
38#include "gdbtypes.h"
39#include "objfiles.h"
d55e5aa6 40#include "dwarf2.h"
4de283e4
TT
41#include "buildsym.h"
42#include "demangle.h"
43#include "gdb-demangle.h"
4de283e4
TT
44#include "filenames.h" /* for DOSish file names */
45#include "macrotab.h"
46#include "language.h"
47#include "complaints.h"
d55e5aa6
TT
48#include "dwarf2expr.h"
49#include "dwarf2loc.h"
4de283e4
TT
50#include "cp-support.h"
51#include "hashtab.h"
52#include "command.h"
d55e5aa6 53#include "gdbcmd.h"
4de283e4
TT
54#include "block.h"
55#include "addrmap.h"
56#include "typeprint.h"
57#include "psympriv.h"
4de283e4 58#include "c-lang.h"
d55e5aa6 59#include "go-lang.h"
4de283e4
TT
60#include "valprint.h"
61#include "gdbcore.h" /* for gnutarget */
62#include "gdb/gdb-index.h"
4de283e4
TT
63#include "gdb_bfd.h"
64#include "f-lang.h"
65#include "source.h"
4de283e4 66#include "build-id.h"
d55e5aa6 67#include "namespace.h"
268a13a5
TT
68#include "gdbsupport/function-view.h"
69#include "gdbsupport/gdb_optional.h"
70#include "gdbsupport/underlying.h"
268a13a5 71#include "gdbsupport/hash_enum.h"
4de283e4 72#include "filename-seen-cache.h"
b32b108a 73#include "producer.h"
4de283e4 74#include <fcntl.h>
4de283e4 75#include <algorithm>
4de283e4 76#include <unordered_map>
268a13a5 77#include "gdbsupport/selftest.h"
c9317f21 78#include "rust-lang.h"
268a13a5 79#include "gdbsupport/pathstuff.h"
437afbb8 80
73be47f5
DE
81/* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
b4f54984
DE
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84static unsigned int dwarf_read_debug = 0;
45cfd468 85
d97bc12b 86/* When non-zero, dump DIEs after they are read in. */
b4f54984 87static unsigned int dwarf_die_debug = 0;
d97bc12b 88
27e0867f
DE
89/* When non-zero, dump line number entries as they are read in. */
90static unsigned int dwarf_line_debug = 0;
91
491144b5
CB
92/* When true, cross-check physname against demangler. */
93static bool check_physname = false;
900e11f9 94
491144b5
CB
95/* When true, do not reject deprecated .gdb_index sections. */
96static bool use_deprecated_index_sections = false;
481860b3 97
5bfd760d 98static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 99
f1e6e072
TT
100/* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102static int dwarf2_locexpr_index;
103static int dwarf2_loclist_index;
104static int dwarf2_locexpr_block_index;
105static int dwarf2_loclist_block_index;
106
3f563c84
PA
107/* An index into a (C++) symbol name component in a symbol name as
108 recorded in the mapped_index's symbol table. For each C++ symbol
109 in the symbol table, we record one entry for the start of each
110 component in the symbol in a table of name components, and then
111 sort the table, in order to be able to binary search symbol names,
112 ignoring leading namespaces, both completion and regular look up.
113 For example, for symbol "A::B::C", we'll have an entry that points
114 to "A::B::C", another that points to "B::C", and another for "C".
115 Note that function symbols in GDB index have no parameter
116 information, just the function/method names. You can convert a
117 name_component to a "const char *" using the
118 'mapped_index::symbol_name_at(offset_type)' method. */
119
120struct name_component
121{
122 /* Offset in the symbol name where the component starts. Stored as
123 a (32-bit) offset instead of a pointer to save memory and improve
124 locality on 64-bit architectures. */
125 offset_type name_offset;
126
127 /* The symbol's index in the symbol and constant pool tables of a
128 mapped_index. */
129 offset_type idx;
130};
131
44ed8f3e
PA
132/* Base class containing bits shared by both .gdb_index and
133 .debug_name indexes. */
134
135struct mapped_index_base
136{
22ca247e
TT
137 mapped_index_base () = default;
138 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
139
44ed8f3e
PA
140 /* The name_component table (a sorted vector). See name_component's
141 description above. */
142 std::vector<name_component> name_components;
143
144 /* How NAME_COMPONENTS is sorted. */
145 enum case_sensitivity name_components_casing;
146
147 /* Return the number of names in the symbol table. */
148 virtual size_t symbol_name_count () const = 0;
149
150 /* Get the name of the symbol at IDX in the symbol table. */
151 virtual const char *symbol_name_at (offset_type idx) const = 0;
152
153 /* Return whether the name at IDX in the symbol table should be
154 ignored. */
155 virtual bool symbol_name_slot_invalid (offset_type idx) const
156 {
157 return false;
158 }
159
160 /* Build the symbol name component sorted vector, if we haven't
161 yet. */
162 void build_name_components ();
163
164 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
165 possible matches for LN_NO_PARAMS in the name component
166 vector. */
167 std::pair<std::vector<name_component>::const_iterator,
168 std::vector<name_component>::const_iterator>
3b00ef10
TT
169 find_name_components_bounds (const lookup_name_info &ln_no_params,
170 enum language lang) const;
44ed8f3e
PA
171
172 /* Prevent deleting/destroying via a base class pointer. */
173protected:
174 ~mapped_index_base() = default;
175};
176
9291a0cd
TT
177/* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
fc898b42 179struct mapped_index final : public mapped_index_base
9291a0cd 180{
f00a2de2
PA
181 /* A slot/bucket in the symbol table hash. */
182 struct symbol_table_slot
183 {
184 const offset_type name;
185 const offset_type vec;
186 };
187
559a7a62 188 /* Index data format version. */
3063847f 189 int version = 0;
559a7a62 190
f00a2de2
PA
191 /* The address table data. */
192 gdb::array_view<const gdb_byte> address_table;
b11b1f88 193
3876f04e 194 /* The symbol table, implemented as a hash table. */
f00a2de2 195 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 196
9291a0cd 197 /* A pointer to the constant pool. */
3063847f 198 const char *constant_pool = nullptr;
3f563c84 199
44ed8f3e
PA
200 bool symbol_name_slot_invalid (offset_type idx) const override
201 {
202 const auto &bucket = this->symbol_table[idx];
9ab08412 203 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 204 }
5c58de74 205
3f563c84
PA
206 /* Convenience method to get at the name of the symbol at IDX in the
207 symbol table. */
44ed8f3e 208 const char *symbol_name_at (offset_type idx) const override
f00a2de2 209 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 210
44ed8f3e
PA
211 size_t symbol_name_count () const override
212 { return this->symbol_table.size (); }
9291a0cd
TT
213};
214
927aa2e7
JK
215/* A description of the mapped .debug_names.
216 Uninitialized map has CU_COUNT 0. */
fc898b42 217struct mapped_debug_names final : public mapped_index_base
927aa2e7 218{
ed2dc618
SM
219 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
220 : dwarf2_per_objfile (dwarf2_per_objfile_)
221 {}
222
223 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
224 bfd_endian dwarf5_byte_order;
225 bool dwarf5_is_dwarf64;
226 bool augmentation_is_gdb;
227 uint8_t offset_size;
228 uint32_t cu_count = 0;
229 uint32_t tu_count, bucket_count, name_count;
230 const gdb_byte *cu_table_reordered, *tu_table_reordered;
231 const uint32_t *bucket_table_reordered, *hash_table_reordered;
232 const gdb_byte *name_table_string_offs_reordered;
233 const gdb_byte *name_table_entry_offs_reordered;
234 const gdb_byte *entry_pool;
235
236 struct index_val
237 {
238 ULONGEST dwarf_tag;
239 struct attr
240 {
241 /* Attribute name DW_IDX_*. */
242 ULONGEST dw_idx;
243
244 /* Attribute form DW_FORM_*. */
245 ULONGEST form;
246
247 /* Value if FORM is DW_FORM_implicit_const. */
248 LONGEST implicit_const;
249 };
250 std::vector<attr> attr_vec;
251 };
252
253 std::unordered_map<ULONGEST, index_val> abbrev_map;
254
255 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
256
257 /* Implementation of the mapped_index_base virtual interface, for
258 the name_components cache. */
259
260 const char *symbol_name_at (offset_type idx) const override
261 { return namei_to_name (idx); }
262
263 size_t symbol_name_count () const override
264 { return this->name_count; }
927aa2e7
JK
265};
266
cd4fb1b2 267/* See dwarf2read.h. */
ed2dc618 268
cd4fb1b2 269dwarf2_per_objfile *
ed2dc618
SM
270get_dwarf2_per_objfile (struct objfile *objfile)
271{
5bfd760d 272 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 273}
c906108c 274
251d32d9 275/* Default names of the debugging sections. */
c906108c 276
233a11ab
CS
277/* Note that if the debugging section has been compressed, it might
278 have a name like .zdebug_info. */
279
9cdd5dbd
DE
280static const struct dwarf2_debug_sections dwarf2_elf_names =
281{
251d32d9
TG
282 { ".debug_info", ".zdebug_info" },
283 { ".debug_abbrev", ".zdebug_abbrev" },
284 { ".debug_line", ".zdebug_line" },
285 { ".debug_loc", ".zdebug_loc" },
43988095 286 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 287 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 288 { ".debug_macro", ".zdebug_macro" },
251d32d9 289 { ".debug_str", ".zdebug_str" },
18a8505e 290 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 291 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 292 { ".debug_ranges", ".zdebug_ranges" },
43988095 293 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 294 { ".debug_types", ".zdebug_types" },
3019eac3 295 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
296 { ".debug_frame", ".zdebug_frame" },
297 { ".eh_frame", NULL },
24d3216f 298 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
299 { ".debug_names", ".zdebug_names" },
300 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 301 23
251d32d9 302};
c906108c 303
80626a55 304/* List of DWO/DWP sections. */
3019eac3 305
80626a55 306static const struct dwop_section_names
3019eac3
DE
307{
308 struct dwarf2_section_names abbrev_dwo;
309 struct dwarf2_section_names info_dwo;
310 struct dwarf2_section_names line_dwo;
311 struct dwarf2_section_names loc_dwo;
43988095 312 struct dwarf2_section_names loclists_dwo;
09262596
DE
313 struct dwarf2_section_names macinfo_dwo;
314 struct dwarf2_section_names macro_dwo;
3019eac3
DE
315 struct dwarf2_section_names str_dwo;
316 struct dwarf2_section_names str_offsets_dwo;
317 struct dwarf2_section_names types_dwo;
80626a55
DE
318 struct dwarf2_section_names cu_index;
319 struct dwarf2_section_names tu_index;
3019eac3 320}
80626a55 321dwop_section_names =
3019eac3
DE
322{
323 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
324 { ".debug_info.dwo", ".zdebug_info.dwo" },
325 { ".debug_line.dwo", ".zdebug_line.dwo" },
326 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 327 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
335};
336
c906108c
SS
337/* local data types */
338
107d2387
AC
339/* The data in a compilation unit header, after target2host
340 translation, looks like this. */
c906108c 341struct comp_unit_head
a738430d 342{
c764a876 343 unsigned int length;
a738430d 344 short version;
a738430d
MK
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
9c541725 347 sect_offset abbrev_sect_off;
57349743 348
a738430d
MK
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
57349743 351
a738430d
MK
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
57349743 354
43988095
JK
355 enum dwarf_unit_type unit_type;
356
a738430d
MK
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
9c541725 359 sect_offset sect_off;
57349743 360
d00adf39
DE
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
9c541725 363 cu_offset first_die_cu_offset;
43988095 364
a084a2a6
AT
365
366 /* 64-bit signature of this unit. For type units, it denotes the signature of
367 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
368 Also used in DWARF 5, to denote the dwo id when the unit type is
369 DW_UT_skeleton or DW_UT_split_compile. */
43988095
JK
370 ULONGEST signature;
371
372 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 373 cu_offset type_cu_offset_in_tu;
a738430d 374};
c906108c 375
3da10d80
KS
376/* Type used for delaying computation of method physnames.
377 See comments for compute_delayed_physnames. */
378struct delayed_method_info
379{
380 /* The type to which the method is attached, i.e., its parent class. */
381 struct type *type;
382
383 /* The index of the method in the type's function fieldlists. */
384 int fnfield_index;
385
386 /* The index of the method in the fieldlist. */
387 int index;
388
389 /* The name of the DIE. */
390 const char *name;
391
392 /* The DIE associated with this method. */
393 struct die_info *die;
394};
395
e7c27a73
DJ
396/* Internal state when decoding a particular compilation unit. */
397struct dwarf2_cu
398{
fcd3b13d
SM
399 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
400 ~dwarf2_cu ();
401
402 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
403
c24bdb02
KS
404 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
405 Create the set of symtabs used by this TU, or if this TU is sharing
406 symtabs with another TU and the symtabs have already been created
407 then restore those symtabs in the line header.
408 We don't need the pc/line-number mapping for type units. */
409 void setup_type_unit_groups (struct die_info *die);
410
411 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
412 buildsym_compunit constructor. */
413 struct compunit_symtab *start_symtab (const char *name,
414 const char *comp_dir,
415 CORE_ADDR low_pc);
416
417 /* Reset the builder. */
418 void reset_builder () { m_builder.reset (); }
419
d00adf39 420 /* The header of the compilation unit. */
fcd3b13d 421 struct comp_unit_head header {};
e142c38c 422
d00adf39 423 /* Base address of this compilation unit. */
fcd3b13d 424 CORE_ADDR base_address = 0;
d00adf39
DE
425
426 /* Non-zero if base_address has been set. */
fcd3b13d 427 int base_known = 0;
d00adf39 428
e142c38c 429 /* The language we are debugging. */
fcd3b13d
SM
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
e142c38c 432
fcd3b13d 433 const char *producer = nullptr;
b0f35d58 434
c24bdb02 435private:
804d2729
TT
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
c24bdb02 438 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 439
c24bdb02 440public:
e142c38c
DJ
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
fcd3b13d 450 struct pending **list_in_scope = nullptr;
e142c38c 451
b64f50a1
JK
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 454 htab_t partial_dies = nullptr;
72bf9492
DJ
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
fcd3b13d 458 auto_obstack comp_unit_obstack;
72bf9492 459
ae038cb0
DJ
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
fcd3b13d 464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 465
69d751e3 466 /* Backlink to our per_cu entry. */
ae038cb0
DJ
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 470 int last_used = 0;
ae038cb0 471
b64f50a1
JK
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
fcd3b13d 474 htab_t die_hash = nullptr;
10b3939b
DJ
475
476 /* Full DIEs if read in. */
fcd3b13d 477 struct die_info *dies = nullptr;
10b3939b
DJ
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
fcd3b13d 482 htab_t dependencies = nullptr;
10b3939b 483
cb1df416 484 /* Header data from the line table, during full symbol processing. */
fcd3b13d 485 struct line_header *line_header = nullptr;
4c8aa72d
PA
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
fcd3b13d 491 die_info *line_header_die_owner = nullptr;
cb1df416 492
3da10d80
KS
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
c89b44cd 495 std::vector<delayed_method_info> method_list;
3da10d80 496
96408a79 497 /* To be copied to symtab->call_site_htab. */
fcd3b13d 498 htab_t call_site_htab = nullptr;
96408a79 499
034e5797
DE
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
18a8505e 504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
fcd3b13d 509 struct dwo_unit *dwo_unit = nullptr;
3019eac3 510
18a8505e 511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 513 gdb::optional<ULONGEST> addr_base;
3019eac3 514
18a8505e 515 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 516 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 517 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 524 ULONGEST ranges_base = 0;
2e3cf129 525
c9317f21
TT
526 /* When reading debug info generated by older versions of rustc, we
527 have to rewrite some union types to be struct types with a
528 variant part. This rewriting must be done after the CU is fully
529 read in, because otherwise at the point of rewriting some struct
530 type might not have been fully processed. So, we keep a list of
531 all such types here and process them after expansion. */
532 std::vector<struct type *> rust_unions;
533
18a8505e
AT
534 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
535 files, the value is implicitly zero. For DWARF 5 version DWO files, the
536 value is often implicit and is the size of the header of
537 .debug_str_offsets section (8 or 4, depending on the address size). */
538 gdb::optional<ULONGEST> str_offsets_base;
539
ae038cb0 540 /* Mark used when releasing cached dies. */
9068261f 541 bool mark : 1;
ae038cb0 542
8be455d7
JK
543 /* This CU references .debug_loc. See the symtab->locations_valid field.
544 This test is imperfect as there may exist optimized debug code not using
545 any location list and still facing inlining issues if handled as
546 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 547 bool has_loclist : 1;
ba919b58 548
9068261f 549 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
550 if all the producer_is_* fields are valid. This information is cached
551 because profiling CU expansion showed excessive time spent in
552 producer_is_gxx_lt_4_6. */
9068261f
AB
553 bool checked_producer : 1;
554 bool producer_is_gxx_lt_4_6 : 1;
555 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 556 bool producer_is_icc : 1;
9068261f 557 bool producer_is_icc_lt_14 : 1;
c258c396 558 bool producer_is_codewarrior : 1;
4d4ec4e5 559
9068261f 560 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
561 debugging info for C++ namespaces. GCC 3.3.x did not produce
562 this information, but later versions do. */
563
9068261f 564 bool processing_has_namespace_info : 1;
d590ff25
YQ
565
566 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
567
568 /* If this CU was inherited by another CU (via specification,
569 abstract_origin, etc), this is the ancestor CU. */
570 dwarf2_cu *ancestor;
571
572 /* Get the buildsym_compunit for this CU. */
573 buildsym_compunit *get_builder ()
574 {
575 /* If this CU has a builder associated with it, use that. */
576 if (m_builder != nullptr)
577 return m_builder.get ();
578
579 /* Otherwise, search ancestors for a valid builder. */
580 if (ancestor != nullptr)
581 return ancestor->get_builder ();
582
583 return nullptr;
584 }
e7c27a73
DJ
585};
586
094b34ac
DE
587/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
588 This includes type_unit_group and quick_file_names. */
589
590struct stmt_list_hash
591{
592 /* The DWO unit this table is from or NULL if there is none. */
593 struct dwo_unit *dwo_unit;
594
595 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 596 sect_offset line_sect_off;
094b34ac
DE
597};
598
f4dc4d17
DE
599/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
600 an object of this type. */
601
602struct type_unit_group
603{
0186c6a7 604 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
605 To simplify things we create an artificial CU that "includes" all the
606 type units using this stmt_list so that the rest of the code still has
607 a "per_cu" handle on the symtab.
608 This PER_CU is recognized by having no section. */
8a0459fd 609#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
610 struct dwarf2_per_cu_data per_cu;
611
0186c6a7
DE
612 /* The TUs that share this DW_AT_stmt_list entry.
613 This is added to while parsing type units to build partial symtabs,
614 and is deleted afterwards and not used again. */
a8b3b8e9 615 std::vector<signatured_type *> *tus;
f4dc4d17 616
43f3e411 617 /* The compunit symtab.
094b34ac 618 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
619 so we create an essentially anonymous symtab as the compunit symtab. */
620 struct compunit_symtab *compunit_symtab;
f4dc4d17 621
094b34ac
DE
622 /* The data used to construct the hash key. */
623 struct stmt_list_hash hash;
f4dc4d17
DE
624
625 /* The number of symtabs from the line header.
626 The value here must match line_header.num_file_names. */
627 unsigned int num_symtabs;
628
629 /* The symbol tables for this TU (obtained from the files listed in
630 DW_AT_stmt_list).
631 WARNING: The order of entries here must match the order of entries
632 in the line header. After the first TU using this type_unit_group, the
633 line header for the subsequent TUs is recreated from this. This is done
634 because we need to use the same symtabs for each TU using the same
635 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
636 there's no guarantee the line header doesn't have duplicate entries. */
637 struct symtab **symtabs;
638};
639
73869dc2 640/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
641
642struct dwo_sections
643{
644 struct dwarf2_section_info abbrev;
3019eac3
DE
645 struct dwarf2_section_info line;
646 struct dwarf2_section_info loc;
43988095 647 struct dwarf2_section_info loclists;
09262596
DE
648 struct dwarf2_section_info macinfo;
649 struct dwarf2_section_info macro;
3019eac3
DE
650 struct dwarf2_section_info str;
651 struct dwarf2_section_info str_offsets;
80626a55
DE
652 /* In the case of a virtual DWO file, these two are unused. */
653 struct dwarf2_section_info info;
fd5866f6 654 std::vector<dwarf2_section_info> types;
3019eac3
DE
655};
656
c88ee1f0 657/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
658
659struct dwo_unit
660{
661 /* Backlink to the containing struct dwo_file. */
662 struct dwo_file *dwo_file;
663
664 /* The "id" that distinguishes this CU/TU.
665 .debug_info calls this "dwo_id", .debug_types calls this "signature".
666 Since signatures came first, we stick with it for consistency. */
667 ULONGEST signature;
668
669 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 670 struct dwarf2_section_info *section;
3019eac3 671
9c541725
PA
672 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
673 sect_offset sect_off;
3019eac3
DE
674 unsigned int length;
675
676 /* For types, offset in the type's DIE of the type defined by this TU. */
677 cu_offset type_offset_in_tu;
678};
679
73869dc2
DE
680/* include/dwarf2.h defines the DWP section codes.
681 It defines a max value but it doesn't define a min value, which we
682 use for error checking, so provide one. */
683
684enum dwp_v2_section_ids
685{
686 DW_SECT_MIN = 1
687};
688
80626a55 689/* Data for one DWO file.
57d63ce2
DE
690
691 This includes virtual DWO files (a virtual DWO file is a DWO file as it
692 appears in a DWP file). DWP files don't really have DWO files per se -
693 comdat folding of types "loses" the DWO file they came from, and from
694 a high level view DWP files appear to contain a mass of random types.
695 However, to maintain consistency with the non-DWP case we pretend DWP
696 files contain virtual DWO files, and we assign each TU with one virtual
697 DWO file (generally based on the line and abbrev section offsets -
698 a heuristic that seems to work in practice). */
3019eac3
DE
699
700struct dwo_file
701{
51ac9db5
SM
702 dwo_file () = default;
703 DISABLE_COPY_AND_ASSIGN (dwo_file);
704
18a8505e 705 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
706 For virtual DWO files the name is constructed from the section offsets
707 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
708 from related CU+TUs. */
51ac9db5 709 const char *dwo_name = nullptr;
0ac5b59e
DE
710
711 /* The DW_AT_comp_dir attribute. */
51ac9db5 712 const char *comp_dir = nullptr;
3019eac3 713
80626a55
DE
714 /* The bfd, when the file is open. Otherwise this is NULL.
715 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 716 gdb_bfd_ref_ptr dbfd;
3019eac3 717
73869dc2
DE
718 /* The sections that make up this DWO file.
719 Remember that for virtual DWO files in DWP V2, these are virtual
720 sections (for lack of a better name). */
51ac9db5 721 struct dwo_sections sections {};
3019eac3 722
33c5cd75
DB
723 /* The CUs in the file.
724 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
725 an extension to handle LLVM's Link Time Optimization output (where
726 multiple source files may be compiled into a single object/dwo pair). */
51ac9db5 727 htab_t cus {};
3019eac3
DE
728
729 /* Table of TUs in the file.
730 Each element is a struct dwo_unit. */
51ac9db5 731 htab_t tus {};
3019eac3
DE
732};
733
80626a55
DE
734/* These sections are what may appear in a DWP file. */
735
736struct dwp_sections
737{
73869dc2 738 /* These are used by both DWP version 1 and 2. */
80626a55
DE
739 struct dwarf2_section_info str;
740 struct dwarf2_section_info cu_index;
741 struct dwarf2_section_info tu_index;
73869dc2
DE
742
743 /* These are only used by DWP version 2 files.
744 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
745 sections are referenced by section number, and are not recorded here.
746 In DWP version 2 there is at most one copy of all these sections, each
747 section being (effectively) comprised of the concatenation of all of the
748 individual sections that exist in the version 1 format.
749 To keep the code simple we treat each of these concatenated pieces as a
750 section itself (a virtual section?). */
751 struct dwarf2_section_info abbrev;
752 struct dwarf2_section_info info;
753 struct dwarf2_section_info line;
754 struct dwarf2_section_info loc;
755 struct dwarf2_section_info macinfo;
756 struct dwarf2_section_info macro;
757 struct dwarf2_section_info str_offsets;
758 struct dwarf2_section_info types;
80626a55
DE
759};
760
73869dc2
DE
761/* These sections are what may appear in a virtual DWO file in DWP version 1.
762 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 763
73869dc2 764struct virtual_v1_dwo_sections
80626a55
DE
765{
766 struct dwarf2_section_info abbrev;
767 struct dwarf2_section_info line;
768 struct dwarf2_section_info loc;
769 struct dwarf2_section_info macinfo;
770 struct dwarf2_section_info macro;
771 struct dwarf2_section_info str_offsets;
772 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 773 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
774 struct dwarf2_section_info info_or_types;
775};
776
73869dc2
DE
777/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
778 In version 2, the sections of the DWO files are concatenated together
779 and stored in one section of that name. Thus each ELF section contains
780 several "virtual" sections. */
781
782struct virtual_v2_dwo_sections
783{
784 bfd_size_type abbrev_offset;
785 bfd_size_type abbrev_size;
786
787 bfd_size_type line_offset;
788 bfd_size_type line_size;
789
790 bfd_size_type loc_offset;
791 bfd_size_type loc_size;
792
793 bfd_size_type macinfo_offset;
794 bfd_size_type macinfo_size;
795
796 bfd_size_type macro_offset;
797 bfd_size_type macro_size;
798
799 bfd_size_type str_offsets_offset;
800 bfd_size_type str_offsets_size;
801
802 /* Each DWP hash table entry records one CU or one TU.
803 That is recorded here, and copied to dwo_unit.section. */
804 bfd_size_type info_or_types_offset;
805 bfd_size_type info_or_types_size;
806};
807
80626a55
DE
808/* Contents of DWP hash tables. */
809
810struct dwp_hash_table
811{
73869dc2 812 uint32_t version, nr_columns;
80626a55 813 uint32_t nr_units, nr_slots;
73869dc2
DE
814 const gdb_byte *hash_table, *unit_table;
815 union
816 {
817 struct
818 {
819 const gdb_byte *indices;
820 } v1;
821 struct
822 {
823 /* This is indexed by column number and gives the id of the section
824 in that column. */
825#define MAX_NR_V2_DWO_SECTIONS \
826 (1 /* .debug_info or .debug_types */ \
827 + 1 /* .debug_abbrev */ \
828 + 1 /* .debug_line */ \
829 + 1 /* .debug_loc */ \
830 + 1 /* .debug_str_offsets */ \
831 + 1 /* .debug_macro or .debug_macinfo */)
832 int section_ids[MAX_NR_V2_DWO_SECTIONS];
833 const gdb_byte *offsets;
834 const gdb_byte *sizes;
835 } v2;
836 } section_pool;
80626a55
DE
837};
838
839/* Data for one DWP file. */
840
841struct dwp_file
842{
400174b1
TT
843 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
844 : name (name_),
845 dbfd (std::move (abfd))
846 {
847 }
848
80626a55
DE
849 /* Name of the file. */
850 const char *name;
851
73869dc2 852 /* File format version. */
400174b1 853 int version = 0;
73869dc2 854
93417882 855 /* The bfd. */
400174b1 856 gdb_bfd_ref_ptr dbfd;
80626a55
DE
857
858 /* Section info for this file. */
400174b1 859 struct dwp_sections sections {};
80626a55 860
57d63ce2 861 /* Table of CUs in the file. */
400174b1 862 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
863
864 /* Table of TUs in the file. */
400174b1 865 const struct dwp_hash_table *tus = nullptr;
80626a55 866
19ac8c2e 867 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
868 htab_t loaded_cus {};
869 htab_t loaded_tus {};
80626a55 870
73869dc2
DE
871 /* Table to map ELF section numbers to their sections.
872 This is only needed for the DWP V1 file format. */
400174b1
TT
873 unsigned int num_sections = 0;
874 asection **elf_sections = nullptr;
80626a55
DE
875};
876
c0ab21c2
TT
877struct abbrev_table;
878typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
879
0963b4bd
MS
880/* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
dee91e82 883 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
884
885struct die_reader_specs
886{
a32a8923 887 /* The bfd of die_section. */
93311388
DE
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
80626a55 893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
894 struct dwo_file *dwo_file;
895
dee91e82 896 /* The section the die comes from.
3019eac3 897 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
d521ce57 901 const gdb_byte *buffer;
f664829e
DE
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
a2ce51a0
DE
905
906 /* The value of the DW_AT_comp_dir attribute. */
907 const char *comp_dir;
685af9cd
TT
908
909 /* The abbreviation table to use when reading the DIEs. */
910 struct abbrev_table *abbrev_table;
93311388
DE
911};
912
c0ab21c2
TT
913/* A subclass of die_reader_specs that holds storage and has complex
914 constructor and destructor behavior. */
915
916class cutu_reader : public die_reader_specs
917{
918public:
919
920 cutu_reader (struct dwarf2_per_cu_data *this_cu,
921 struct abbrev_table *abbrev_table,
922 int use_existing_cu, int keep,
923 bool skip_partial);
924
925 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
926 struct dwarf2_cu *parent_cu = nullptr,
927 struct dwo_file *dwo_file = nullptr);
928
929 ~cutu_reader ();
930
931 DISABLE_COPY_AND_ASSIGN (cutu_reader);
932
933 const gdb_byte *info_ptr = nullptr;
934 struct die_info *comp_unit_die = nullptr;
935 int has_children = 0;
936 bool dummy_p = false;
937
938private:
939 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
940 int use_existing_cu, int keep);
941
942 struct dwarf2_per_cu_data *m_this_cu;
943 int m_keep = 0;
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951};
dee91e82 952
7ba99d21
AT
953/* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
954 later. */
955typedef int dir_index;
ecfb656c 956
7ba99d21
AT
957/* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
958 and later. */
959typedef int file_name_index;
ecfb656c 960
52059ffd
TT
961struct file_entry
962{
fff8551c
PA
963 file_entry () = default;
964
ecfb656c 965 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
966 unsigned int mod_time_, unsigned int length_)
967 : name (name_),
ecfb656c 968 d_index (d_index_),
fff8551c
PA
969 mod_time (mod_time_),
970 length (length_)
971 {}
972
ecfb656c
PA
973 /* Return the include directory at D_INDEX stored in LH. Returns
974 NULL if D_INDEX is out of bounds. */
8c43009f
PA
975 const char *include_dir (const line_header *lh) const;
976
fff8551c
PA
977 /* The file name. Note this is an observing pointer. The memory is
978 owned by debug_line_buffer. */
979 const char *name {};
980
8c43009f 981 /* The directory index (1-based). */
ecfb656c 982 dir_index d_index {};
fff8551c
PA
983
984 unsigned int mod_time {};
985
986 unsigned int length {};
987
988 /* True if referenced by the Line Number Program. */
989 bool included_p {};
990
83769d0b 991 /* The associated symbol table, if any. */
fff8551c 992 struct symtab *symtab {};
52059ffd
TT
993};
994
debd256d
JB
995/* The line number information for a compilation unit (found in the
996 .debug_line section) begins with a "statement program header",
997 which contains the following information. */
998struct line_header
999{
fff8551c
PA
1000 line_header ()
1001 : offset_in_dwz {}
1002 {}
1003
1004 /* Add an entry to the include directory table. */
1005 void add_include_dir (const char *include_dir);
1006
1007 /* Add an entry to the file name table. */
ecfb656c 1008 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1009 unsigned int mod_time, unsigned int length);
1010
7ba99d21
AT
1011 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1012 Returns NULL if INDEX is out of bounds. */
ecfb656c 1013 const char *include_dir_at (dir_index index) const
8c43009f 1014 {
7ba99d21
AT
1015 int vec_index;
1016 if (version >= 5)
1017 vec_index = index;
1018 else
1019 vec_index = index - 1;
1020 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
8c43009f 1021 return NULL;
7ba99d21 1022 return m_include_dirs[vec_index];
8c43009f
PA
1023 }
1024
7ba99d21 1025 bool is_valid_file_index (int file_index)
8c43009f 1026 {
7ba99d21
AT
1027 if (version >= 5)
1028 return 0 <= file_index && file_index < file_names_size ();
1029 return 1 <= file_index && file_index <= file_names_size ();
1030 }
ecfb656c 1031
7ba99d21
AT
1032 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1033 Returns NULL if INDEX is out of bounds. */
1034 file_entry *file_name_at (file_name_index index)
1035 {
1036 int vec_index;
1037 if (version >= 5)
1038 vec_index = index;
1039 else
1040 vec_index = index - 1;
1041 if (vec_index < 0 || vec_index >= m_file_names.size ())
fff8551c 1042 return NULL;
7ba99d21 1043 return &m_file_names[vec_index];
fff8551c
PA
1044 }
1045
7ba99d21
AT
1046 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1047 this method should only be used to iterate through all file entries in an
1048 index-agnostic manner. */
1049 std::vector<file_entry> &file_names ()
1050 { return m_file_names; }
1051
527f3840 1052 /* Offset of line number information in .debug_line section. */
9c541725 1053 sect_offset sect_off {};
527f3840
JK
1054
1055 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1056 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1057
1058 unsigned int total_length {};
1059 unsigned short version {};
1060 unsigned int header_length {};
1061 unsigned char minimum_instruction_length {};
1062 unsigned char maximum_ops_per_instruction {};
1063 unsigned char default_is_stmt {};
1064 int line_base {};
1065 unsigned char line_range {};
1066 unsigned char opcode_base {};
debd256d
JB
1067
1068 /* standard_opcode_lengths[i] is the number of operands for the
1069 standard opcode whose value is i. This means that
1070 standard_opcode_lengths[0] is unused, and the last meaningful
1071 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1072 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1073
7ba99d21
AT
1074 int file_names_size ()
1075 { return m_file_names.size(); }
debd256d
JB
1076
1077 /* The start and end of the statement program following this
6502dd73 1078 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1079 const gdb_byte *statement_program_start {}, *statement_program_end {};
7ba99d21
AT
1080
1081 private:
1082 /* The include_directories table. Note these are observing
1083 pointers. The memory is owned by debug_line_buffer. */
1084 std::vector<const char *> m_include_dirs;
1085
1086 /* The file_names table. This is private because the meaning of indexes
1087 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1088 before, and is 0 in DWARF 5 and later). So the client should use
1089 file_name_at method for access. */
1090 std::vector<file_entry> m_file_names;
debd256d 1091};
c906108c 1092
fff8551c
PA
1093typedef std::unique_ptr<line_header> line_header_up;
1094
8c43009f
PA
1095const char *
1096file_entry::include_dir (const line_header *lh) const
1097{
ecfb656c 1098 return lh->include_dir_at (d_index);
8c43009f
PA
1099}
1100
c906108c 1101/* When we construct a partial symbol table entry we only
0963b4bd 1102 need this much information. */
6f06d47b 1103struct partial_die_info : public allocate_on_obstack
c906108c 1104 {
6f06d47b
YQ
1105 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1106
1107 /* Disable assign but still keep copy ctor, which is needed
1108 load_partial_dies. */
1109 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1110
52356b79
YQ
1111 /* Adjust the partial die before generating a symbol for it. This
1112 function may set the is_external flag or change the DIE's
1113 name. */
1114 void fixup (struct dwarf2_cu *cu);
1115
48fbe735
YQ
1116 /* Read a minimal amount of information into the minimal die
1117 structure. */
1118 const gdb_byte *read (const struct die_reader_specs *reader,
1119 const struct abbrev_info &abbrev,
1120 const gdb_byte *info_ptr);
1121
72bf9492 1122 /* Offset of this DIE. */
6f06d47b 1123 const sect_offset sect_off;
72bf9492
DJ
1124
1125 /* DWARF-2 tag for this DIE. */
6f06d47b 1126 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1127
72bf9492 1128 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1129 const unsigned int has_children : 1;
1130
72bf9492
DJ
1131 unsigned int is_external : 1;
1132 unsigned int is_declaration : 1;
1133 unsigned int has_type : 1;
1134 unsigned int has_specification : 1;
1135 unsigned int has_pc_info : 1;
481860b3 1136 unsigned int may_be_inlined : 1;
72bf9492 1137
0c1b455e
TT
1138 /* This DIE has been marked DW_AT_main_subprogram. */
1139 unsigned int main_subprogram : 1;
1140
72bf9492
DJ
1141 /* Flag set if the SCOPE field of this structure has been
1142 computed. */
1143 unsigned int scope_set : 1;
1144
fa4028e9
JB
1145 /* Flag set if the DIE has a byte_size attribute. */
1146 unsigned int has_byte_size : 1;
1147
ff908ebf
AW
1148 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1149 unsigned int has_const_value : 1;
1150
98bfdba5
PA
1151 /* Flag set if any of the DIE's children are template arguments. */
1152 unsigned int has_template_arguments : 1;
1153
52356b79 1154 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1155 unsigned int fixup_called : 1;
1156
36586728
TT
1157 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1158 unsigned int is_dwz : 1;
1159
1160 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1161 unsigned int spec_is_dwz : 1;
1162
72bf9492 1163 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1164 sometimes a default name for unnamed DIEs. */
6f06d47b 1165 const char *name = nullptr;
72bf9492 1166
abc72ce4 1167 /* The linkage name, if present. */
6f06d47b 1168 const char *linkage_name = nullptr;
abc72ce4 1169
72bf9492
DJ
1170 /* The scope to prepend to our children. This is generally
1171 allocated on the comp_unit_obstack, so will disappear
1172 when this compilation unit leaves the cache. */
6f06d47b 1173 const char *scope = nullptr;
72bf9492 1174
95554aad
TT
1175 /* Some data associated with the partial DIE. The tag determines
1176 which field is live. */
1177 union
1178 {
1179 /* The location description associated with this DIE, if any. */
1180 struct dwarf_block *locdesc;
1181 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1182 sect_offset sect_off;
6f06d47b 1183 } d {};
72bf9492
DJ
1184
1185 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1186 CORE_ADDR lowpc = 0;
1187 CORE_ADDR highpc = 0;
72bf9492 1188
93311388 1189 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1190 DW_AT_sibling, if any. */
48fbe735
YQ
1191 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1192 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1193 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1194
1195 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1196 DW_AT_specification (or DW_AT_abstract_origin or
1197 DW_AT_extension). */
6f06d47b 1198 sect_offset spec_offset {};
72bf9492
DJ
1199
1200 /* Pointers to this DIE's parent, first child, and next sibling,
1201 if any. */
6f06d47b
YQ
1202 struct partial_die_info *die_parent = nullptr;
1203 struct partial_die_info *die_child = nullptr;
1204 struct partial_die_info *die_sibling = nullptr;
1205
1206 friend struct partial_die_info *
1207 dwarf2_cu::find_partial_die (sect_offset sect_off);
1208
1209 private:
1210 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1211 partial_die_info (sect_offset sect_off)
1212 : partial_die_info (sect_off, DW_TAG_padding, 0)
1213 {
1214 }
1215
1216 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1217 int has_children_)
1218 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1219 {
1220 is_external = 0;
1221 is_declaration = 0;
1222 has_type = 0;
1223 has_specification = 0;
1224 has_pc_info = 0;
1225 may_be_inlined = 0;
1226 main_subprogram = 0;
1227 scope_set = 0;
1228 has_byte_size = 0;
1229 has_const_value = 0;
1230 has_template_arguments = 0;
1231 fixup_called = 0;
1232 is_dwz = 0;
1233 spec_is_dwz = 0;
1234 }
c906108c
SS
1235 };
1236
0963b4bd 1237/* This data structure holds the information of an abbrev. */
c906108c
SS
1238struct abbrev_info
1239 {
1240 unsigned int number; /* number identifying abbrev */
1241 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1242 unsigned short has_children; /* boolean */
1243 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1244 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1245 struct abbrev_info *next; /* next in chain */
1246 };
1247
1248struct attr_abbrev
1249 {
9d25dd43
DE
1250 ENUM_BITFIELD(dwarf_attribute) name : 16;
1251 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1252
1253 /* It is valid only if FORM is DW_FORM_implicit_const. */
1254 LONGEST implicit_const;
c906108c
SS
1255 };
1256
433df2d4
DE
1257/* Size of abbrev_table.abbrev_hash_table. */
1258#define ABBREV_HASH_SIZE 121
1259
1260/* Top level data structure to contain an abbreviation table. */
1261
1262struct abbrev_table
1263{
685af9cd
TT
1264 explicit abbrev_table (sect_offset off)
1265 : sect_off (off)
1266 {
4a17f768 1267 m_abbrevs =
685af9cd 1268 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1269 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1270 }
1271
1272 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1273
1274 /* Allocate space for a struct abbrev_info object in
1275 ABBREV_TABLE. */
1276 struct abbrev_info *alloc_abbrev ();
1277
1278 /* Add an abbreviation to the table. */
1279 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1280
1281 /* Look up an abbrev in the table.
1282 Returns NULL if the abbrev is not found. */
1283
1284 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1285
1286
f4dc4d17
DE
1287 /* Where the abbrev table came from.
1288 This is used as a sanity check when the table is used. */
685af9cd 1289 const sect_offset sect_off;
433df2d4
DE
1290
1291 /* Storage for the abbrev table. */
685af9cd 1292 auto_obstack abbrev_obstack;
433df2d4 1293
4a17f768
YQ
1294private:
1295
433df2d4
DE
1296 /* Hash table of abbrevs.
1297 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1298 It could be statically allocated, but the previous code didn't so we
1299 don't either. */
4a17f768 1300 struct abbrev_info **m_abbrevs;
433df2d4
DE
1301};
1302
0963b4bd 1303/* Attributes have a name and a value. */
b60c80d6
DJ
1304struct attribute
1305 {
9d25dd43 1306 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1307 ENUM_BITFIELD(dwarf_form) form : 15;
1308
1309 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1310 field should be in u.str (existing only for DW_STRING) but it is kept
1311 here for better struct attribute alignment. */
1312 unsigned int string_is_canonical : 1;
1313
b60c80d6
DJ
1314 union
1315 {
15d034d0 1316 const char *str;
b60c80d6 1317 struct dwarf_block *blk;
43bbcdc2
PH
1318 ULONGEST unsnd;
1319 LONGEST snd;
b60c80d6 1320 CORE_ADDR addr;
ac9ec31b 1321 ULONGEST signature;
b60c80d6
DJ
1322 }
1323 u;
1324 };
1325
0963b4bd 1326/* This data structure holds a complete die structure. */
c906108c
SS
1327struct die_info
1328 {
76815b17
DE
1329 /* DWARF-2 tag for this DIE. */
1330 ENUM_BITFIELD(dwarf_tag) tag : 16;
1331
1332 /* Number of attributes */
98bfdba5
PA
1333 unsigned char num_attrs;
1334
1335 /* True if we're presently building the full type name for the
1336 type derived from this DIE. */
1337 unsigned char building_fullname : 1;
76815b17 1338
adde2bff
DE
1339 /* True if this die is in process. PR 16581. */
1340 unsigned char in_process : 1;
1341
76815b17
DE
1342 /* Abbrev number */
1343 unsigned int abbrev;
1344
93311388 1345 /* Offset in .debug_info or .debug_types section. */
9c541725 1346 sect_offset sect_off;
78ba4af6
JB
1347
1348 /* The dies in a compilation unit form an n-ary tree. PARENT
1349 points to this die's parent; CHILD points to the first child of
1350 this node; and all the children of a given node are chained
4950bc1c 1351 together via their SIBLING fields. */
639d11d3
DC
1352 struct die_info *child; /* Its first child, if any. */
1353 struct die_info *sibling; /* Its next sibling, if any. */
1354 struct die_info *parent; /* Its parent, if any. */
c906108c 1355
b60c80d6
DJ
1356 /* An array of attributes, with NUM_ATTRS elements. There may be
1357 zero, but it's not common and zero-sized arrays are not
1358 sufficiently portable C. */
1359 struct attribute attrs[1];
c906108c
SS
1360 };
1361
0963b4bd 1362/* Get at parts of an attribute structure. */
c906108c
SS
1363
1364#define DW_STRING(attr) ((attr)->u.str)
8285870a 1365#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1366#define DW_UNSND(attr) ((attr)->u.unsnd)
1367#define DW_BLOCK(attr) ((attr)->u.blk)
1368#define DW_SND(attr) ((attr)->u.snd)
1369#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1370#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1371
0963b4bd 1372/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1373struct dwarf_block
1374 {
56eb65bd 1375 size_t size;
1d6edc3c
JK
1376
1377 /* Valid only if SIZE is not zero. */
d521ce57 1378 const gdb_byte *data;
c906108c
SS
1379 };
1380
c906108c
SS
1381/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1382 but this would require a corresponding change in unpack_field_as_long
1383 and friends. */
1384static int bits_per_byte = 8;
1385
2ddeaf8a
TT
1386/* When reading a variant or variant part, we track a bit more
1387 information about the field, and store it in an object of this
1388 type. */
1389
1390struct variant_field
1391{
1392 /* If we see a DW_TAG_variant, then this will be the discriminant
1393 value. */
1394 ULONGEST discriminant_value;
1395 /* If we see a DW_TAG_variant, then this will be set if this is the
1396 default branch. */
1397 bool default_branch;
1398 /* While reading a DW_TAG_variant_part, this will be set if this
1399 field is the discriminant. */
1400 bool is_discriminant;
1401};
1402
52059ffd
TT
1403struct nextfield
1404{
be2daae6
TT
1405 int accessibility = 0;
1406 int virtuality = 0;
2ddeaf8a 1407 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1408 struct variant_field variant {};
1409 struct field field {};
52059ffd
TT
1410};
1411
1412struct fnfieldlist
1413{
be2daae6
TT
1414 const char *name = nullptr;
1415 std::vector<struct fn_field> fnfields;
52059ffd
TT
1416};
1417
c906108c
SS
1418/* The routines that read and process dies for a C struct or C++ class
1419 pass lists of data member fields and lists of member function fields
1420 in an instance of a field_info structure, as defined below. */
1421struct field_info
c5aa993b 1422 {
0963b4bd 1423 /* List of data member and baseclasses fields. */
be2daae6
TT
1424 std::vector<struct nextfield> fields;
1425 std::vector<struct nextfield> baseclasses;
c906108c 1426
7d0ccb61 1427 /* Number of fields (including baseclasses). */
be2daae6 1428 int nfields = 0;
c906108c 1429
85102364 1430 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1431 int non_public_fields = 0;
c906108c 1432
c5aa993b
JM
1433 /* Member function fieldlist array, contains name of possibly overloaded
1434 member function, number of overloaded member functions and a pointer
1435 to the head of the member function field chain. */
be2daae6 1436 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1437
1438 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1439 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1440 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1441
1442 /* Nested types defined by this class and the number of elements in this
1443 list. */
be2daae6 1444 std::vector<struct decl_field> nested_types_list;
c5aa993b 1445 };
c906108c 1446
10b3939b
DJ
1447/* One item on the queue of compilation units to read in full symbols
1448 for. */
1449struct dwarf2_queue_item
1450{
1451 struct dwarf2_per_cu_data *per_cu;
95554aad 1452 enum language pretend_language;
10b3939b
DJ
1453 struct dwarf2_queue_item *next;
1454};
1455
1456/* The current queue. */
1457static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1458
ae038cb0
DJ
1459/* Loaded secondary compilation units are kept in memory until they
1460 have not been referenced for the processing of this many
1461 compilation units. Set this to zero to disable caching. Cache
1462 sizes of up to at least twenty will improve startup time for
1463 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1464static int dwarf_max_cache_age = 5;
920d2a44 1465static void
b4f54984
DE
1466show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1467 struct cmd_list_element *c, const char *value)
920d2a44 1468{
3e43a32a 1469 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1470 "DWARF compilation units is %s.\n"),
920d2a44
AC
1471 value);
1472}
4390d890 1473\f
c906108c
SS
1474/* local function prototypes */
1475
a32a8923
DE
1476static const char *get_section_name (const struct dwarf2_section_info *);
1477
1478static const char *get_section_file_name (const struct dwarf2_section_info *);
1479
918dd910
JK
1480static void dwarf2_find_base_address (struct die_info *die,
1481 struct dwarf2_cu *cu);
1482
891813be 1483static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1484 (struct dwarf2_per_cu_data *per_cu, const char *name);
1485
f1902523
JK
1486static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1487 const gdb_byte *info_ptr,
1488 struct die_info *type_unit_die,
c0ab21c2 1489 int has_children);
f1902523 1490
ed2dc618
SM
1491static void dwarf2_build_psymtabs_hard
1492 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1493
72bf9492
DJ
1494static void scan_partial_symbols (struct partial_die_info *,
1495 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1496 int, struct dwarf2_cu *);
c906108c 1497
72bf9492
DJ
1498static void add_partial_symbol (struct partial_die_info *,
1499 struct dwarf2_cu *);
63d06c5c 1500
72bf9492
DJ
1501static void add_partial_namespace (struct partial_die_info *pdi,
1502 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1503 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1504
5d7cb8df 1505static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1506 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1507 struct dwarf2_cu *cu);
1508
72bf9492
DJ
1509static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1510 struct dwarf2_cu *cu);
91c24f0a 1511
bc30ff58
JB
1512static void add_partial_subprogram (struct partial_die_info *pdi,
1513 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1514 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1515
891813be 1516static void psymtab_to_symtab_1 (dwarf2_psymtab *);
c906108c 1517
685af9cd 1518static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1519 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1520 sect_offset);
433df2d4 1521
d521ce57 1522static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1523
dee91e82 1524static struct partial_die_info *load_partial_dies
d521ce57 1525 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1526
fb816e8b
TV
1527/* A pair of partial_die_info and compilation unit. */
1528struct cu_partial_die_info
1529{
1530 /* The compilation unit of the partial_die_info. */
1531 struct dwarf2_cu *cu;
1532 /* A partial_die_info. */
1533 struct partial_die_info *pdi;
122cf0f2
AB
1534
1535 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1536 : cu (cu),
1537 pdi (pdi)
405feb71 1538 { /* Nothing. */ }
122cf0f2
AB
1539
1540private:
1541 cu_partial_die_info () = delete;
fb816e8b
TV
1542};
1543
122cf0f2
AB
1544static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1545 struct dwarf2_cu *);
72bf9492 1546
d521ce57
TT
1547static const gdb_byte *read_attribute (const struct die_reader_specs *,
1548 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1549 const gdb_byte *, bool *need_reprocess);
1550
1551static void read_attribute_reprocess (const struct die_reader_specs *reader,
1552 struct attribute *attr);
1553
1554static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1555
a1855c1d 1556static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1557
a1855c1d 1558static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1559
a1855c1d 1560static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1561
15f18d14
AT
1562/* Read the next three bytes (little-endian order) as an unsigned integer. */
1563static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1564
a1855c1d 1565static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1566
a1855c1d 1567static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1568
d521ce57 1569static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1570 unsigned int *);
c906108c 1571
d521ce57 1572static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1573
1574static LONGEST read_checked_initial_length_and_offset
d521ce57 1575 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1576 unsigned int *, unsigned int *);
613e1657 1577
d521ce57
TT
1578static LONGEST read_offset (bfd *, const gdb_byte *,
1579 const struct comp_unit_head *,
c764a876
DE
1580 unsigned int *);
1581
d521ce57 1582static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1583
ed2dc618
SM
1584static sect_offset read_abbrev_offset
1585 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1586 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1587
d521ce57 1588static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1589
d521ce57 1590static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1591
ed2dc618
SM
1592static const char *read_indirect_string
1593 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1594 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1595
ed2dc618
SM
1596static const char *read_indirect_line_string
1597 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1598 const struct comp_unit_head *, unsigned int *);
36586728 1599
ed2dc618
SM
1600static const char *read_indirect_string_at_offset
1601 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1602 LONGEST str_offset);
927aa2e7 1603
ed2dc618
SM
1604static const char *read_indirect_string_from_dwz
1605 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1606
d521ce57 1607static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1608
d521ce57
TT
1609static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1610 const gdb_byte *,
3019eac3
DE
1611 unsigned int *);
1612
18a8505e
AT
1613static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1614 ULONGEST str_index);
1615
1616static const char *read_stub_str_index (struct dwarf2_cu *cu,
1617 ULONGEST str_index);
3019eac3 1618
e142c38c 1619static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1620
e142c38c
DJ
1621static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1622 struct dwarf2_cu *);
c906108c 1623
348e048f 1624static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1625 unsigned int);
348e048f 1626
7d45c7c3
KB
1627static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1628 struct dwarf2_cu *cu);
1629
a084a2a6
AT
1630static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1631
05cf31d1
JB
1632static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1633 struct dwarf2_cu *cu);
1634
e142c38c 1635static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1636
e142c38c 1637static struct die_info *die_specification (struct die_info *die,
f2f0e013 1638 struct dwarf2_cu **);
63d06c5c 1639
9c541725 1640static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1641 struct dwarf2_cu *cu);
debd256d 1642
f3f5162e 1643static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1644 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1645 CORE_ADDR, int decode_mapping);
c906108c 1646
804d2729
TT
1647static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1648 const char *);
c906108c 1649
a14ed312 1650static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1651 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1652
ff39bb5e 1653static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1654 struct dwarf2_cu *);
c906108c 1655
ff39bb5e 1656static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1657 struct type *type,
1658 const char *name,
1659 struct obstack *obstack,
12df843f 1660 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1661 const gdb_byte **bytes,
98bfdba5 1662 struct dwarf2_locexpr_baton **baton);
2df3850c 1663
e7c27a73 1664static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1665
b4ba55a1
JB
1666static int need_gnat_info (struct dwarf2_cu *);
1667
3e43a32a
MS
1668static struct type *die_descriptive_type (struct die_info *,
1669 struct dwarf2_cu *);
b4ba55a1
JB
1670
1671static void set_descriptive_type (struct type *, struct die_info *,
1672 struct dwarf2_cu *);
1673
e7c27a73
DJ
1674static struct type *die_containing_type (struct die_info *,
1675 struct dwarf2_cu *);
c906108c 1676
ff39bb5e 1677static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1678 struct dwarf2_cu *);
c906108c 1679
f792889a 1680static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1681
673bfd45
DE
1682static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1683
0d5cff50 1684static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1685
6e70227d 1686static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1687 const char *suffix, int physname,
1688 struct dwarf2_cu *cu);
63d06c5c 1689
e7c27a73 1690static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1691
348e048f
DE
1692static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1693
e7c27a73 1694static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1695
e7c27a73 1696static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1697
96408a79
SA
1698static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1699
71a3c369
TT
1700static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1701
ff013f42 1702static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1703 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1704
3a2b436a 1705/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1706 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1707enum pc_bounds_kind
1708{
e385593e 1709 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1710 PC_BOUNDS_NOT_PRESENT,
1711
e385593e
JK
1712 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1713 were present but they do not form a valid range of PC addresses. */
1714 PC_BOUNDS_INVALID,
1715
3a2b436a
JK
1716 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1717 PC_BOUNDS_RANGES,
1718
1719 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1720 PC_BOUNDS_HIGH_LOW,
1721};
1722
1723static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1724 CORE_ADDR *, CORE_ADDR *,
1725 struct dwarf2_cu *,
891813be 1726 dwarf2_psymtab *);
c906108c 1727
fae299cd
DC
1728static void get_scope_pc_bounds (struct die_info *,
1729 CORE_ADDR *, CORE_ADDR *,
1730 struct dwarf2_cu *);
1731
801e3a5b
JB
1732static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1733 CORE_ADDR, struct dwarf2_cu *);
1734
a14ed312 1735static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1736 struct dwarf2_cu *);
c906108c 1737
a14ed312 1738static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1739 struct type *, struct dwarf2_cu *);
c906108c 1740
a14ed312 1741static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1742 struct die_info *, struct type *,
e7c27a73 1743 struct dwarf2_cu *);
c906108c 1744
a14ed312 1745static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1746 struct type *,
1747 struct dwarf2_cu *);
c906108c 1748
134d01f1 1749static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1750
e7c27a73 1751static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1752
e7c27a73 1753static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1754
5d7cb8df
JK
1755static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1756
804d2729 1757static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1758
27aa8d6a
SW
1759static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1760
74921315
KS
1761static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1762
f55ee35c
JK
1763static struct type *read_module_type (struct die_info *die,
1764 struct dwarf2_cu *cu);
1765
38d518c9 1766static const char *namespace_name (struct die_info *die,
e142c38c 1767 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1768
134d01f1 1769static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1770
e7c27a73 1771static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1772
6e70227d 1773static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1774 struct dwarf2_cu *);
1775
bf6af496 1776static struct die_info *read_die_and_siblings_1
d521ce57 1777 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1778 struct die_info *);
639d11d3 1779
dee91e82 1780static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1781 const gdb_byte *info_ptr,
1782 const gdb_byte **new_info_ptr,
639d11d3
DC
1783 struct die_info *parent);
1784
d521ce57
TT
1785static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1786 struct die_info **, const gdb_byte *,
1787 int *, int);
3019eac3 1788
d521ce57
TT
1789static const gdb_byte *read_full_die (const struct die_reader_specs *,
1790 struct die_info **, const gdb_byte *,
1791 int *);
93311388 1792
e7c27a73 1793static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1794
15d034d0
TT
1795static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1796 struct obstack *);
71c25dea 1797
15d034d0 1798static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1799
15d034d0 1800static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1801 struct die_info *die,
1802 struct dwarf2_cu *cu);
1803
ca69b9e6
DE
1804static const char *dwarf2_physname (const char *name, struct die_info *die,
1805 struct dwarf2_cu *cu);
1806
e142c38c 1807static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1808 struct dwarf2_cu **);
9219021c 1809
f39c6ffd 1810static const char *dwarf_tag_name (unsigned int);
c906108c 1811
f39c6ffd 1812static const char *dwarf_attr_name (unsigned int);
c906108c 1813
a084a2a6
AT
1814static const char *dwarf_unit_type_name (int unit_type);
1815
f39c6ffd 1816static const char *dwarf_form_name (unsigned int);
c906108c 1817
a121b7c1 1818static const char *dwarf_bool_name (unsigned int);
c906108c 1819
f39c6ffd 1820static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1821
f9aca02d 1822static struct die_info *sibling_die (struct die_info *);
c906108c 1823
d97bc12b
DE
1824static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1825
1826static void dump_die_for_error (struct die_info *);
1827
1828static void dump_die_1 (struct ui_file *, int level, int max_level,
1829 struct die_info *);
c906108c 1830
d97bc12b 1831/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1832
51545339 1833static void store_in_ref_table (struct die_info *,
10b3939b 1834 struct dwarf2_cu *);
c906108c 1835
ff39bb5e 1836static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1837
ff39bb5e 1838static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1839
348e048f 1840static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1841 const struct attribute *,
348e048f
DE
1842 struct dwarf2_cu **);
1843
10b3939b 1844static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1845 const struct attribute *,
f2f0e013 1846 struct dwarf2_cu **);
c906108c 1847
348e048f 1848static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1849 const struct attribute *,
348e048f
DE
1850 struct dwarf2_cu **);
1851
ac9ec31b
DE
1852static struct type *get_signatured_type (struct die_info *, ULONGEST,
1853 struct dwarf2_cu *);
1854
1855static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1856 const struct attribute *,
ac9ec31b
DE
1857 struct dwarf2_cu *);
1858
e5fe5e75 1859static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1860
52dc124a 1861static void read_signatured_type (struct signatured_type *);
348e048f 1862
63e43d3a
PMR
1863static int attr_to_dynamic_prop (const struct attribute *attr,
1864 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1865 struct dynamic_prop *prop, struct type *type);
63e43d3a 1866
c906108c
SS
1867/* memory allocation interface */
1868
7b5a2f43 1869static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1870
b60c80d6 1871static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1872
43f3e411 1873static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1874
6e5a29e1 1875static int attr_form_is_block (const struct attribute *);
8e19ed76 1876
6e5a29e1 1877static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1878
6e5a29e1 1879static int attr_form_is_constant (const struct attribute *);
3690dd37 1880
6e5a29e1 1881static int attr_form_is_ref (const struct attribute *);
7771576e 1882
8cf6f0b1
TT
1883static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1884 struct dwarf2_loclist_baton *baton,
ff39bb5e 1885 const struct attribute *attr);
8cf6f0b1 1886
ff39bb5e 1887static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1888 struct symbol *sym,
f1e6e072
TT
1889 struct dwarf2_cu *cu,
1890 int is_block);
4c2df51b 1891
d521ce57
TT
1892static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1893 const gdb_byte *info_ptr,
1894 struct abbrev_info *abbrev);
4bb7a0a7 1895
72bf9492
DJ
1896static hashval_t partial_die_hash (const void *item);
1897
1898static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1899
ae038cb0 1900static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1901 (sect_offset sect_off, unsigned int offset_in_dwz,
1902 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1903
9816fde3 1904static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1905 struct die_info *comp_unit_die,
1906 enum language pretend_language);
93311388 1907
ed2dc618 1908static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1909
dee91e82 1910static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1911
f792889a
DJ
1912static struct type *set_die_type (struct die_info *, struct type *,
1913 struct dwarf2_cu *);
1c379e20 1914
ed2dc618 1915static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1916
ed2dc618 1917static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1918
58f0c718 1919static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1920 enum language);
10b3939b 1921
95554aad
TT
1922static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1923 enum language);
10b3939b 1924
f4dc4d17
DE
1925static void process_full_type_unit (struct dwarf2_per_cu_data *,
1926 enum language);
1927
10b3939b
DJ
1928static void dwarf2_add_dependence (struct dwarf2_cu *,
1929 struct dwarf2_per_cu_data *);
1930
ae038cb0
DJ
1931static void dwarf2_mark (struct dwarf2_cu *);
1932
1933static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1934
b64f50a1 1935static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1936 struct dwarf2_per_cu_data *);
673bfd45 1937
f792889a 1938static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1939
95554aad
TT
1940static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1941 enum language pretend_language);
1942
ed2dc618 1943static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1944
9a49df9d
AB
1945static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1946static struct type *dwarf2_per_cu_addr_sized_int_type
1947 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1948static struct type *dwarf2_per_cu_int_type
1949 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1950 bool unsigned_p);
9a49df9d 1951
b303c6f6
AB
1952/* Class, the destructor of which frees all allocated queue entries. This
1953 will only have work to do if an error was thrown while processing the
1954 dwarf. If no error was thrown then the queue entries should have all
1955 been processed, and freed, as we went along. */
1956
1957class dwarf2_queue_guard
1958{
1959public:
1960 dwarf2_queue_guard () = default;
1961
1962 /* Free any entries remaining on the queue. There should only be
1963 entries left if we hit an error while processing the dwarf. */
1964 ~dwarf2_queue_guard ()
1965 {
1966 struct dwarf2_queue_item *item, *last;
1967
1968 item = dwarf2_queue;
1969 while (item)
1970 {
1971 /* Anything still marked queued is likely to be in an
1972 inconsistent state, so discard it. */
1973 if (item->per_cu->queued)
1974 {
1975 if (item->per_cu->cu != NULL)
1976 free_one_cached_comp_unit (item->per_cu);
1977 item->per_cu->queued = 0;
1978 }
1979
1980 last = item;
1981 item = item->next;
1982 xfree (last);
1983 }
1984
1985 dwarf2_queue = dwarf2_queue_tail = NULL;
1986 }
1987};
1988
d721ba37
PA
1989/* The return type of find_file_and_directory. Note, the enclosed
1990 string pointers are only valid while this object is valid. */
1991
1992struct file_and_directory
1993{
1994 /* The filename. This is never NULL. */
1995 const char *name;
1996
1997 /* The compilation directory. NULL if not known. If we needed to
1998 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1999 points directly to the DW_AT_comp_dir string attribute owned by
2000 the obstack that owns the DIE. */
2001 const char *comp_dir;
2002
2003 /* If we needed to build a new string for comp_dir, this is what
2004 owns the storage. */
2005 std::string comp_dir_storage;
2006};
2007
2008static file_and_directory find_file_and_directory (struct die_info *die,
2009 struct dwarf2_cu *cu);
9291a0cd
TT
2010
2011static char *file_full_name (int file, struct line_header *lh,
2012 const char *comp_dir);
2013
43988095
JK
2014/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2015enum class rcuh_kind { COMPILE, TYPE };
2016
d521ce57 2017static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
2018 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2019 struct comp_unit_head *header,
36586728 2020 struct dwarf2_section_info *section,
d521ce57 2021 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2022 rcuh_kind section_kind);
36586728 2023
673bfd45 2024static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2025
3019eac3
DE
2026static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2027
57d63ce2 2028static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2029 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2030 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2031 ULONGEST signature, int is_debug_types);
a2ce51a0 2032
ed2dc618
SM
2033static struct dwp_file *get_dwp_file
2034 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2035
3019eac3 2036static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2037 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2038
2039static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2040 (struct signatured_type *, const char *, const char *);
3019eac3 2041
89e63ee4
DE
2042static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2043
263db9a1
TT
2044/* A unique pointer to a dwo_file. */
2045
51ac9db5 2046typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2047
ed2dc618 2048static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2049
1b80a9fa 2050static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2051
2052static void free_line_header_voidp (void *arg);
4390d890
DE
2053\f
2054/* Various complaints about symbol reading that don't abort the process. */
2055
2056static void
2057dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2058{
b98664d3 2059 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2060}
2061
2062static void
2063dwarf2_debug_line_missing_file_complaint (void)
2064{
b98664d3 2065 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2066}
2067
2068static void
2069dwarf2_debug_line_missing_end_sequence_complaint (void)
2070{
b98664d3 2071 complaint (_(".debug_line section has line "
4390d890
DE
2072 "program sequence without an end"));
2073}
2074
2075static void
2076dwarf2_complex_location_expr_complaint (void)
2077{
b98664d3 2078 complaint (_("location expression too complex"));
4390d890
DE
2079}
2080
2081static void
2082dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2083 int arg3)
2084{
b98664d3 2085 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2086 arg1, arg2, arg3);
2087}
2088
2089static void
2090dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2091{
b98664d3 2092 complaint (_("debug info runs off end of %s section"
4390d890 2093 " [in module %s]"),
a32a8923
DE
2094 get_section_name (section),
2095 get_section_file_name (section));
4390d890 2096}
1b80a9fa 2097
4390d890
DE
2098static void
2099dwarf2_macro_malformed_definition_complaint (const char *arg1)
2100{
b98664d3 2101 complaint (_("macro debug info contains a "
4390d890
DE
2102 "malformed macro definition:\n`%s'"),
2103 arg1);
2104}
2105
2106static void
2107dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2108{
b98664d3 2109 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2110 arg1, arg2);
2111}
527f3840
JK
2112
2113/* Hash function for line_header_hash. */
2114
2115static hashval_t
2116line_header_hash (const struct line_header *ofs)
2117{
9c541725 2118 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2119}
2120
2121/* Hash function for htab_create_alloc_ex for line_header_hash. */
2122
2123static hashval_t
2124line_header_hash_voidp (const void *item)
2125{
9a3c8263 2126 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2127
2128 return line_header_hash (ofs);
2129}
2130
2131/* Equality function for line_header_hash. */
2132
2133static int
2134line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2135{
9a3c8263
SM
2136 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2137 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2138
9c541725 2139 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2140 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2141}
2142
4390d890 2143\f
9291a0cd 2144
31aa7e4e
JB
2145/* Read the given attribute value as an address, taking the attribute's
2146 form into account. */
2147
2148static CORE_ADDR
2149attr_value_as_address (struct attribute *attr)
2150{
2151 CORE_ADDR addr;
2152
336d760d
AT
2153 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2154 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2155 {
2156 /* Aside from a few clearly defined exceptions, attributes that
2157 contain an address must always be in DW_FORM_addr form.
2158 Unfortunately, some compilers happen to be violating this
2159 requirement by encoding addresses using other forms, such
2160 as DW_FORM_data4 for example. For those broken compilers,
2161 we try to do our best, without any guarantee of success,
2162 to interpret the address correctly. It would also be nice
2163 to generate a complaint, but that would require us to maintain
2164 a list of legitimate cases where a non-address form is allowed,
2165 as well as update callers to pass in at least the CU's DWARF
2166 version. This is more overhead than what we're willing to
2167 expand for a pretty rare case. */
2168 addr = DW_UNSND (attr);
2169 }
2170 else
2171 addr = DW_ADDR (attr);
2172
2173 return addr;
2174}
2175
330cdd98
PA
2176/* See declaration. */
2177
2178dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2179 const dwarf2_debug_sections *names,
2180 bool can_copy_)
2181 : objfile (objfile_),
2182 can_copy (can_copy_)
330cdd98
PA
2183{
2184 if (names == NULL)
2185 names = &dwarf2_elf_names;
2186
2187 bfd *obfd = objfile->obfd;
2188
2189 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2190 locate_sections (obfd, sec, *names);
2191}
2192
2193dwarf2_per_objfile::~dwarf2_per_objfile ()
2194{
2195 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2196 free_cached_comp_units ();
2197
2198 if (quick_file_names_table)
2199 htab_delete (quick_file_names_table);
2200
2201 if (line_header_hash)
2202 htab_delete (line_header_hash);
2203
b76e467d 2204 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2205 per_cu->imported_symtabs_free ();
fc8e7e75 2206
b2bdb8cf 2207 for (signatured_type *sig_type : all_type_units)
ae640021 2208 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2209
330cdd98
PA
2210 /* Everything else should be on the objfile obstack. */
2211}
2212
2213/* See declaration. */
2214
2215void
2216dwarf2_per_objfile::free_cached_comp_units ()
2217{
2218 dwarf2_per_cu_data *per_cu = read_in_chain;
2219 dwarf2_per_cu_data **last_chain = &read_in_chain;
2220 while (per_cu != NULL)
2221 {
2222 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2223
fcd3b13d 2224 delete per_cu->cu;
330cdd98
PA
2225 *last_chain = next_cu;
2226 per_cu = next_cu;
2227 }
2228}
2229
11ed8cad
TT
2230/* A helper class that calls free_cached_comp_units on
2231 destruction. */
2232
2233class free_cached_comp_units
2234{
2235public:
2236
2237 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2238 : m_per_objfile (per_objfile)
2239 {
2240 }
2241
2242 ~free_cached_comp_units ()
2243 {
2244 m_per_objfile->free_cached_comp_units ();
2245 }
2246
2247 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2248
2249private:
2250
2251 dwarf2_per_objfile *m_per_objfile;
2252};
2253
c906108c 2254/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2255 information and return true if we have enough to do something.
2256 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2257 ELF names are used. CAN_COPY is true for formats where symbol
2258 interposition is possible and so symbol values must follow copy
2259 relocation rules. */
c906108c
SS
2260
2261int
251d32d9 2262dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2263 const struct dwarf2_debug_sections *names,
2264 bool can_copy)
c906108c 2265{
97cbe998
SDJ
2266 if (objfile->flags & OBJF_READNEVER)
2267 return 0;
2268
ed2dc618
SM
2269 struct dwarf2_per_objfile *dwarf2_per_objfile
2270 = get_dwarf2_per_objfile (objfile);
2271
2272 if (dwarf2_per_objfile == NULL)
5bfd760d 2273 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2274 names,
2275 can_copy);
5bfd760d 2276
73869dc2 2277 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2278 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2279 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2280 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2281}
2282
2283/* Return the containing section of virtual section SECTION. */
2284
2285static struct dwarf2_section_info *
2286get_containing_section (const struct dwarf2_section_info *section)
2287{
2288 gdb_assert (section->is_virtual);
2289 return section->s.containing_section;
c906108c
SS
2290}
2291
a32a8923
DE
2292/* Return the bfd owner of SECTION. */
2293
2294static struct bfd *
2295get_section_bfd_owner (const struct dwarf2_section_info *section)
2296{
73869dc2
DE
2297 if (section->is_virtual)
2298 {
2299 section = get_containing_section (section);
2300 gdb_assert (!section->is_virtual);
2301 }
049412e3 2302 return section->s.section->owner;
a32a8923
DE
2303}
2304
2305/* Return the bfd section of SECTION.
2306 Returns NULL if the section is not present. */
2307
2308static asection *
2309get_section_bfd_section (const struct dwarf2_section_info *section)
2310{
73869dc2
DE
2311 if (section->is_virtual)
2312 {
2313 section = get_containing_section (section);
2314 gdb_assert (!section->is_virtual);
2315 }
049412e3 2316 return section->s.section;
a32a8923
DE
2317}
2318
2319/* Return the name of SECTION. */
2320
2321static const char *
2322get_section_name (const struct dwarf2_section_info *section)
2323{
2324 asection *sectp = get_section_bfd_section (section);
2325
2326 gdb_assert (sectp != NULL);
fd361982 2327 return bfd_section_name (sectp);
a32a8923
DE
2328}
2329
2330/* Return the name of the file SECTION is in. */
2331
2332static const char *
2333get_section_file_name (const struct dwarf2_section_info *section)
2334{
2335 bfd *abfd = get_section_bfd_owner (section);
2336
2337 return bfd_get_filename (abfd);
2338}
2339
2340/* Return the id of SECTION.
2341 Returns 0 if SECTION doesn't exist. */
2342
2343static int
2344get_section_id (const struct dwarf2_section_info *section)
2345{
2346 asection *sectp = get_section_bfd_section (section);
2347
2348 if (sectp == NULL)
2349 return 0;
2350 return sectp->id;
2351}
2352
2353/* Return the flags of SECTION.
73869dc2 2354 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2355
2356static int
2357get_section_flags (const struct dwarf2_section_info *section)
2358{
2359 asection *sectp = get_section_bfd_section (section);
2360
2361 gdb_assert (sectp != NULL);
fd361982 2362 return bfd_section_flags (sectp);
a32a8923
DE
2363}
2364
251d32d9
TG
2365/* When loading sections, we look either for uncompressed section or for
2366 compressed section names. */
233a11ab
CS
2367
2368static int
251d32d9
TG
2369section_is_p (const char *section_name,
2370 const struct dwarf2_section_names *names)
233a11ab 2371{
251d32d9
TG
2372 if (names->normal != NULL
2373 && strcmp (section_name, names->normal) == 0)
2374 return 1;
2375 if (names->compressed != NULL
2376 && strcmp (section_name, names->compressed) == 0)
2377 return 1;
2378 return 0;
233a11ab
CS
2379}
2380
330cdd98 2381/* See declaration. */
c906108c 2382
330cdd98
PA
2383void
2384dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2385 const dwarf2_debug_sections &names)
c906108c 2386{
fd361982 2387 flagword aflag = bfd_section_flags (sectp);
251d32d9 2388
dc7650b8
JK
2389 if ((aflag & SEC_HAS_CONTENTS) == 0)
2390 {
2391 }
950b7495
KS
2392 else if (elf_section_data (sectp)->this_hdr.sh_size
2393 > bfd_get_file_size (abfd))
2394 {
2395 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2396 warning (_("Discarding section %s which has a section size (%s"
2397 ") larger than the file size [in module %s]"),
2398 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2399 bfd_get_filename (abfd));
2400 }
330cdd98 2401 else if (section_is_p (sectp->name, &names.info))
c906108c 2402 {
330cdd98 2403 this->info.s.section = sectp;
fd361982 2404 this->info.size = bfd_section_size (sectp);
c906108c 2405 }
330cdd98 2406 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2407 {
330cdd98 2408 this->abbrev.s.section = sectp;
fd361982 2409 this->abbrev.size = bfd_section_size (sectp);
c906108c 2410 }
330cdd98 2411 else if (section_is_p (sectp->name, &names.line))
c906108c 2412 {
330cdd98 2413 this->line.s.section = sectp;
fd361982 2414 this->line.size = bfd_section_size (sectp);
c906108c 2415 }
330cdd98 2416 else if (section_is_p (sectp->name, &names.loc))
c906108c 2417 {
330cdd98 2418 this->loc.s.section = sectp;
fd361982 2419 this->loc.size = bfd_section_size (sectp);
c906108c 2420 }
330cdd98 2421 else if (section_is_p (sectp->name, &names.loclists))
43988095 2422 {
330cdd98 2423 this->loclists.s.section = sectp;
fd361982 2424 this->loclists.size = bfd_section_size (sectp);
43988095 2425 }
330cdd98 2426 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2427 {
330cdd98 2428 this->macinfo.s.section = sectp;
fd361982 2429 this->macinfo.size = bfd_section_size (sectp);
c906108c 2430 }
330cdd98 2431 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2432 {
330cdd98 2433 this->macro.s.section = sectp;
fd361982 2434 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2435 }
330cdd98 2436 else if (section_is_p (sectp->name, &names.str))
c906108c 2437 {
330cdd98 2438 this->str.s.section = sectp;
fd361982 2439 this->str.size = bfd_section_size (sectp);
c906108c 2440 }
18a8505e
AT
2441 else if (section_is_p (sectp->name, &names.str_offsets))
2442 {
2443 this->str_offsets.s.section = sectp;
2444 this->str_offsets.size = bfd_section_size (sectp);
2445 }
330cdd98 2446 else if (section_is_p (sectp->name, &names.line_str))
43988095 2447 {
330cdd98 2448 this->line_str.s.section = sectp;
fd361982 2449 this->line_str.size = bfd_section_size (sectp);
43988095 2450 }
330cdd98 2451 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2452 {
330cdd98 2453 this->addr.s.section = sectp;
fd361982 2454 this->addr.size = bfd_section_size (sectp);
3019eac3 2455 }
330cdd98 2456 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2457 {
330cdd98 2458 this->frame.s.section = sectp;
fd361982 2459 this->frame.size = bfd_section_size (sectp);
b6af0555 2460 }
330cdd98 2461 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2462 {
330cdd98 2463 this->eh_frame.s.section = sectp;
fd361982 2464 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2465 }
330cdd98 2466 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2467 {
330cdd98 2468 this->ranges.s.section = sectp;
fd361982 2469 this->ranges.size = bfd_section_size (sectp);
af34e669 2470 }
330cdd98 2471 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2472 {
330cdd98 2473 this->rnglists.s.section = sectp;
fd361982 2474 this->rnglists.size = bfd_section_size (sectp);
43988095 2475 }
330cdd98 2476 else if (section_is_p (sectp->name, &names.types))
348e048f 2477 {
8b70b953
TT
2478 struct dwarf2_section_info type_section;
2479
2480 memset (&type_section, 0, sizeof (type_section));
049412e3 2481 type_section.s.section = sectp;
fd361982 2482 type_section.size = bfd_section_size (sectp);
8b70b953 2483
fd5866f6 2484 this->types.push_back (type_section);
348e048f 2485 }
330cdd98 2486 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2487 {
330cdd98 2488 this->gdb_index.s.section = sectp;
fd361982 2489 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2490 }
927aa2e7
JK
2491 else if (section_is_p (sectp->name, &names.debug_names))
2492 {
2493 this->debug_names.s.section = sectp;
fd361982 2494 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2495 }
2496 else if (section_is_p (sectp->name, &names.debug_aranges))
2497 {
2498 this->debug_aranges.s.section = sectp;
fd361982 2499 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2500 }
dce234bc 2501
fd361982
AM
2502 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2503 && bfd_section_vma (sectp) == 0)
330cdd98 2504 this->has_section_at_zero = true;
c906108c
SS
2505}
2506
fceca515
DE
2507/* A helper function that decides whether a section is empty,
2508 or not present. */
9e0ac564
TT
2509
2510static int
19ac8c2e 2511dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2512{
73869dc2
DE
2513 if (section->is_virtual)
2514 return section->size == 0;
049412e3 2515 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2516}
2517
cd4fb1b2 2518/* See dwarf2read.h. */
c906108c 2519
cd4fb1b2
SM
2520void
2521dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2522{
a32a8923 2523 asection *sectp;
3019eac3 2524 bfd *abfd;
dce234bc 2525 gdb_byte *buf, *retbuf;
c906108c 2526
be391dca
TT
2527 if (info->readin)
2528 return;
dce234bc 2529 info->buffer = NULL;
dc4ccb6f 2530 info->readin = true;
188dd5d6 2531
9e0ac564 2532 if (dwarf2_section_empty_p (info))
dce234bc 2533 return;
c906108c 2534
a32a8923 2535 sectp = get_section_bfd_section (info);
3019eac3 2536
73869dc2
DE
2537 /* If this is a virtual section we need to read in the real one first. */
2538 if (info->is_virtual)
2539 {
2540 struct dwarf2_section_info *containing_section =
2541 get_containing_section (info);
2542
2543 gdb_assert (sectp != NULL);
2544 if ((sectp->flags & SEC_RELOC) != 0)
2545 {
2546 error (_("Dwarf Error: DWP format V2 with relocations is not"
2547 " supported in section %s [in module %s]"),
2548 get_section_name (info), get_section_file_name (info));
2549 }
2550 dwarf2_read_section (objfile, containing_section);
2551 /* Other code should have already caught virtual sections that don't
2552 fit. */
2553 gdb_assert (info->virtual_offset + info->size
2554 <= containing_section->size);
2555 /* If the real section is empty or there was a problem reading the
2556 section we shouldn't get here. */
2557 gdb_assert (containing_section->buffer != NULL);
2558 info->buffer = containing_section->buffer + info->virtual_offset;
2559 return;
2560 }
2561
4bf44c1c
TT
2562 /* If the section has relocations, we must read it ourselves.
2563 Otherwise we attach it to the BFD. */
2564 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2565 {
d521ce57 2566 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2567 return;
dce234bc 2568 }
dce234bc 2569
224c3ddb 2570 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2571 info->buffer = buf;
dce234bc
PP
2572
2573 /* When debugging .o files, we may need to apply relocations; see
2574 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2575 We never compress sections in .o files, so we only need to
2576 try this when the section is not compressed. */
ac8035ab 2577 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2578 if (retbuf != NULL)
2579 {
2580 info->buffer = retbuf;
2581 return;
2582 }
2583
a32a8923
DE
2584 abfd = get_section_bfd_owner (info);
2585 gdb_assert (abfd != NULL);
2586
dce234bc
PP
2587 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2588 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2589 {
2590 error (_("Dwarf Error: Can't read DWARF data"
2591 " in section %s [in module %s]"),
fd361982 2592 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2593 }
dce234bc
PP
2594}
2595
9e0ac564
TT
2596/* A helper function that returns the size of a section in a safe way.
2597 If you are positive that the section has been read before using the
2598 size, then it is safe to refer to the dwarf2_section_info object's
2599 "size" field directly. In other cases, you must call this
2600 function, because for compressed sections the size field is not set
2601 correctly until the section has been read. */
2602
2603static bfd_size_type
2604dwarf2_section_size (struct objfile *objfile,
2605 struct dwarf2_section_info *info)
2606{
2607 if (!info->readin)
2608 dwarf2_read_section (objfile, info);
2609 return info->size;
2610}
2611
dce234bc 2612/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2613 SECTION_NAME. */
af34e669 2614
dce234bc 2615void
3017a003
TG
2616dwarf2_get_section_info (struct objfile *objfile,
2617 enum dwarf2_section_enum sect,
d521ce57 2618 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2619 bfd_size_type *sizep)
2620{
5bfd760d 2621 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2622 struct dwarf2_section_info *info;
a3b2a86b
TT
2623
2624 /* We may see an objfile without any DWARF, in which case we just
2625 return nothing. */
2626 if (data == NULL)
2627 {
2628 *sectp = NULL;
2629 *bufp = NULL;
2630 *sizep = 0;
2631 return;
2632 }
3017a003
TG
2633 switch (sect)
2634 {
2635 case DWARF2_DEBUG_FRAME:
2636 info = &data->frame;
2637 break;
2638 case DWARF2_EH_FRAME:
2639 info = &data->eh_frame;
2640 break;
2641 default:
2642 gdb_assert_not_reached ("unexpected section");
2643 }
dce234bc 2644
9e0ac564 2645 dwarf2_read_section (objfile, info);
dce234bc 2646
a32a8923 2647 *sectp = get_section_bfd_section (info);
dce234bc
PP
2648 *bufp = info->buffer;
2649 *sizep = info->size;
2650}
2651
36586728
TT
2652/* A helper function to find the sections for a .dwz file. */
2653
2654static void
2655locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2656{
9a3c8263 2657 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2658
2659 /* Note that we only support the standard ELF names, because .dwz
2660 is ELF-only (at the time of writing). */
2661 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2662 {
049412e3 2663 dwz_file->abbrev.s.section = sectp;
fd361982 2664 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2665 }
2666 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2667 {
049412e3 2668 dwz_file->info.s.section = sectp;
fd361982 2669 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2670 }
2671 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2672 {
049412e3 2673 dwz_file->str.s.section = sectp;
fd361982 2674 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2675 }
2676 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2677 {
049412e3 2678 dwz_file->line.s.section = sectp;
fd361982 2679 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2680 }
2681 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2682 {
049412e3 2683 dwz_file->macro.s.section = sectp;
fd361982 2684 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2685 }
2ec9a5e0
TT
2686 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2687 {
049412e3 2688 dwz_file->gdb_index.s.section = sectp;
fd361982 2689 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2690 }
927aa2e7
JK
2691 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2692 {
2693 dwz_file->debug_names.s.section = sectp;
fd361982 2694 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2695 }
36586728
TT
2696}
2697
c4973306 2698/* See dwarf2read.h. */
36586728 2699
c4973306 2700struct dwz_file *
ed2dc618 2701dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2702{
36586728 2703 const char *filename;
acd13123 2704 bfd_size_type buildid_len_arg;
dc294be5
TT
2705 size_t buildid_len;
2706 bfd_byte *buildid;
36586728
TT
2707
2708 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2709 return dwarf2_per_objfile->dwz_file.get ();
36586728 2710
4db1a1dc 2711 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2712 gdb::unique_xmalloc_ptr<char> data
2713 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2714 &buildid_len_arg, &buildid));
4db1a1dc
TT
2715 if (data == NULL)
2716 {
2717 if (bfd_get_error () == bfd_error_no_error)
2718 return NULL;
2719 error (_("could not read '.gnu_debugaltlink' section: %s"),
2720 bfd_errmsg (bfd_get_error ()));
2721 }
791afaa2
TT
2722
2723 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2724
acd13123
TT
2725 buildid_len = (size_t) buildid_len_arg;
2726
791afaa2 2727 filename = data.get ();
d721ba37
PA
2728
2729 std::string abs_storage;
36586728
TT
2730 if (!IS_ABSOLUTE_PATH (filename))
2731 {
14278e1f
TT
2732 gdb::unique_xmalloc_ptr<char> abs
2733 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2734
14278e1f 2735 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2736 filename = abs_storage.c_str ();
36586728
TT
2737 }
2738
dc294be5
TT
2739 /* First try the file name given in the section. If that doesn't
2740 work, try to use the build-id instead. */
192b62ce 2741 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2742 if (dwz_bfd != NULL)
36586728 2743 {
192b62ce 2744 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2745 dwz_bfd.reset (nullptr);
36586728
TT
2746 }
2747
dc294be5
TT
2748 if (dwz_bfd == NULL)
2749 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2750
2751 if (dwz_bfd == NULL)
2752 error (_("could not find '.gnu_debugaltlink' file for %s"),
2753 objfile_name (dwarf2_per_objfile->objfile));
2754
7ff8cb8c
TT
2755 std::unique_ptr<struct dwz_file> result
2756 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2757
7ff8cb8c
TT
2758 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2759 result.get ());
36586728 2760
7ff8cb8c
TT
2761 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2762 result->dwz_bfd.get ());
2763 dwarf2_per_objfile->dwz_file = std::move (result);
2764 return dwarf2_per_objfile->dwz_file.get ();
36586728 2765}
9291a0cd 2766\f
7b9f3c50
DE
2767/* DWARF quick_symbols_functions support. */
2768
2769/* TUs can share .debug_line entries, and there can be a lot more TUs than
2770 unique line tables, so we maintain a separate table of all .debug_line
2771 derived entries to support the sharing.
2772 All the quick functions need is the list of file names. We discard the
2773 line_header when we're done and don't need to record it here. */
2774struct quick_file_names
2775{
094b34ac
DE
2776 /* The data used to construct the hash key. */
2777 struct stmt_list_hash hash;
7b9f3c50
DE
2778
2779 /* The number of entries in file_names, real_names. */
2780 unsigned int num_file_names;
2781
2782 /* The file names from the line table, after being run through
2783 file_full_name. */
2784 const char **file_names;
2785
2786 /* The file names from the line table after being run through
2787 gdb_realpath. These are computed lazily. */
2788 const char **real_names;
2789};
2790
2791/* When using the index (and thus not using psymtabs), each CU has an
2792 object of this type. This is used to hold information needed by
2793 the various "quick" methods. */
2794struct dwarf2_per_cu_quick_data
2795{
2796 /* The file table. This can be NULL if there was no file table
2797 or it's currently not read in.
2798 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2799 struct quick_file_names *file_names;
2800
2801 /* The corresponding symbol table. This is NULL if symbols for this
2802 CU have not yet been read. */
43f3e411 2803 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2804
2805 /* A temporary mark bit used when iterating over all CUs in
2806 expand_symtabs_matching. */
2807 unsigned int mark : 1;
2808
2809 /* True if we've tried to read the file table and found there isn't one.
2810 There will be no point in trying to read it again next time. */
2811 unsigned int no_file_data : 1;
2812};
2813
094b34ac
DE
2814/* Utility hash function for a stmt_list_hash. */
2815
2816static hashval_t
2817hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2818{
2819 hashval_t v = 0;
2820
2821 if (stmt_list_hash->dwo_unit != NULL)
2822 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2823 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2824 return v;
2825}
2826
2827/* Utility equality function for a stmt_list_hash. */
2828
2829static int
2830eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2831 const struct stmt_list_hash *rhs)
2832{
2833 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2834 return 0;
2835 if (lhs->dwo_unit != NULL
2836 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2837 return 0;
2838
9c541725 2839 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2840}
2841
7b9f3c50
DE
2842/* Hash function for a quick_file_names. */
2843
2844static hashval_t
2845hash_file_name_entry (const void *e)
2846{
9a3c8263
SM
2847 const struct quick_file_names *file_data
2848 = (const struct quick_file_names *) e;
7b9f3c50 2849
094b34ac 2850 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2851}
2852
2853/* Equality function for a quick_file_names. */
2854
2855static int
2856eq_file_name_entry (const void *a, const void *b)
2857{
9a3c8263
SM
2858 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2859 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2860
094b34ac 2861 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2862}
2863
2864/* Delete function for a quick_file_names. */
2865
2866static void
2867delete_file_name_entry (void *e)
2868{
9a3c8263 2869 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2870 int i;
2871
2872 for (i = 0; i < file_data->num_file_names; ++i)
2873 {
2874 xfree ((void*) file_data->file_names[i]);
2875 if (file_data->real_names)
2876 xfree ((void*) file_data->real_names[i]);
2877 }
2878
2879 /* The space for the struct itself lives on objfile_obstack,
2880 so we don't free it here. */
2881}
2882
2883/* Create a quick_file_names hash table. */
2884
2885static htab_t
2886create_quick_file_names_table (unsigned int nr_initial_entries)
2887{
2888 return htab_create_alloc (nr_initial_entries,
2889 hash_file_name_entry, eq_file_name_entry,
2890 delete_file_name_entry, xcalloc, xfree);
2891}
9291a0cd 2892
918dd910
JK
2893/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2894 have to be created afterwards. You should call age_cached_comp_units after
2895 processing PER_CU->CU. dw2_setup must have been already called. */
2896
2897static void
58f0c718 2898load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2899{
3019eac3 2900 if (per_cu->is_debug_types)
e5fe5e75 2901 load_full_type_unit (per_cu);
918dd910 2902 else
58f0c718 2903 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2904
cc12ce38
DE
2905 if (per_cu->cu == NULL)
2906 return; /* Dummy CU. */
2dc860c0
DE
2907
2908 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2909}
2910
a0f42c21 2911/* Read in the symbols for PER_CU. */
2fdf6df6 2912
9291a0cd 2913static void
58f0c718 2914dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2915{
ed2dc618 2916 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2917
f4dc4d17
DE
2918 /* Skip type_unit_groups, reading the type units they contain
2919 is handled elsewhere. */
2920 if (IS_TYPE_UNIT_GROUP (per_cu))
2921 return;
2922
b303c6f6
AB
2923 /* The destructor of dwarf2_queue_guard frees any entries left on
2924 the queue. After this point we're guaranteed to leave this function
2925 with the dwarf queue empty. */
2926 dwarf2_queue_guard q_guard;
9291a0cd 2927
95554aad 2928 if (dwarf2_per_objfile->using_index
43f3e411 2929 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2930 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2931 {
2932 queue_comp_unit (per_cu, language_minimal);
58f0c718 2933 load_cu (per_cu, skip_partial);
89e63ee4
DE
2934
2935 /* If we just loaded a CU from a DWO, and we're working with an index
2936 that may badly handle TUs, load all the TUs in that DWO as well.
2937 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2938 if (!per_cu->is_debug_types
cc12ce38 2939 && per_cu->cu != NULL
89e63ee4
DE
2940 && per_cu->cu->dwo_unit != NULL
2941 && dwarf2_per_objfile->index_table != NULL
2942 && dwarf2_per_objfile->index_table->version <= 7
2943 /* DWP files aren't supported yet. */
ed2dc618 2944 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2945 queue_and_load_all_dwo_tus (per_cu);
95554aad 2946 }
9291a0cd 2947
ed2dc618 2948 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2949
2950 /* Age the cache, releasing compilation units that have not
2951 been used recently. */
ed2dc618 2952 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2953}
2954
2955/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2956 the objfile from which this CU came. Returns the resulting symbol
2957 table. */
2fdf6df6 2958
43f3e411 2959static struct compunit_symtab *
58f0c718 2960dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2961{
ed2dc618
SM
2962 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2963
95554aad 2964 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2965 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2966 {
11ed8cad 2967 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2968 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2969 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2970 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2971 }
f194fefb 2972
43f3e411 2973 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2974}
2975
ff4c9fec 2976/* See declaration. */
f4dc4d17 2977
ff4c9fec
SM
2978dwarf2_per_cu_data *
2979dwarf2_per_objfile::get_cutu (int index)
2980{
b76e467d 2981 if (index >= this->all_comp_units.size ())
ff4c9fec 2982 {
b76e467d 2983 index -= this->all_comp_units.size ();
b2bdb8cf 2984 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2985 return &this->all_type_units[index]->per_cu;
2986 }
f4dc4d17 2987
ff4c9fec
SM
2988 return this->all_comp_units[index];
2989}
f4dc4d17 2990
ff4c9fec 2991/* See declaration. */
2fdf6df6 2992
ff4c9fec
SM
2993dwarf2_per_cu_data *
2994dwarf2_per_objfile::get_cu (int index)
1fd400ff 2995{
b76e467d 2996 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2997
ff4c9fec 2998 return this->all_comp_units[index];
f4dc4d17
DE
2999}
3000
ff4c9fec 3001/* See declaration. */
f4dc4d17 3002
ff4c9fec
SM
3003signatured_type *
3004dwarf2_per_objfile::get_tu (int index)
f4dc4d17 3005{
b2bdb8cf 3006 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 3007
ff4c9fec 3008 return this->all_type_units[index];
1fd400ff
TT
3009}
3010
4b514bc8
JK
3011/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3012 objfile_obstack, and constructed with the specified field
3013 values. */
3014
3015static dwarf2_per_cu_data *
ed2dc618 3016create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
3017 struct dwarf2_section_info *section,
3018 int is_dwz,
3019 sect_offset sect_off, ULONGEST length)
3020{
ed2dc618 3021 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
3022 dwarf2_per_cu_data *the_cu
3023 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3024 struct dwarf2_per_cu_data);
3025 the_cu->sect_off = sect_off;
3026 the_cu->length = length;
e3b94546 3027 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
3028 the_cu->section = section;
3029 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3030 struct dwarf2_per_cu_quick_data);
3031 the_cu->is_dwz = is_dwz;
3032 return the_cu;
3033}
3034
2ec9a5e0
TT
3035/* A helper for create_cus_from_index that handles a given list of
3036 CUs. */
2fdf6df6 3037
74a0d9f6 3038static void
12359b5e 3039create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3040 const gdb_byte *cu_list, offset_type n_elements,
3041 struct dwarf2_section_info *section,
b76e467d 3042 int is_dwz)
9291a0cd 3043{
12359b5e 3044 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3045 {
74a0d9f6 3046 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3047
3048 sect_offset sect_off
3049 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3050 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3051 cu_list += 2 * 8;
3052
b76e467d 3053 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3054 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3055 sect_off, length);
b76e467d 3056 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3057 }
9291a0cd
TT
3058}
3059
2ec9a5e0 3060/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3061 the CU objects for this objfile. */
2ec9a5e0 3062
74a0d9f6 3063static void
12359b5e 3064create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3065 const gdb_byte *cu_list, offset_type cu_list_elements,
3066 const gdb_byte *dwz_list, offset_type dwz_elements)
3067{
b76e467d
SM
3068 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3069 dwarf2_per_objfile->all_comp_units.reserve
3070 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3071
12359b5e 3072 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3073 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3074
3075 if (dwz_elements == 0)
74a0d9f6 3076 return;
2ec9a5e0 3077
12359b5e
SM
3078 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3079 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3080 &dwz->info, 1);
2ec9a5e0
TT
3081}
3082
1fd400ff 3083/* Create the signatured type hash table from the index. */
673bfd45 3084
74a0d9f6 3085static void
12359b5e
SM
3086create_signatured_type_table_from_index
3087 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3088 struct dwarf2_section_info *section,
3089 const gdb_byte *bytes,
3090 offset_type elements)
1fd400ff 3091{
12359b5e 3092 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3093
b2bdb8cf
SM
3094 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3095 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3096
12359b5e 3097 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3098
12359b5e 3099 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3100 {
52dc124a 3101 struct signatured_type *sig_type;
9c541725 3102 ULONGEST signature;
1fd400ff 3103 void **slot;
9c541725 3104 cu_offset type_offset_in_tu;
1fd400ff 3105
74a0d9f6 3106 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3107 sect_offset sect_off
3108 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3109 type_offset_in_tu
3110 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3111 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3112 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3113 bytes += 3 * 8;
3114
52dc124a 3115 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3116 struct signatured_type);
52dc124a 3117 sig_type->signature = signature;
9c541725 3118 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3119 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3120 sig_type->per_cu.section = section;
9c541725 3121 sig_type->per_cu.sect_off = sect_off;
e3b94546 3122 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3123 sig_type->per_cu.v.quick
1fd400ff
TT
3124 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3125 struct dwarf2_per_cu_quick_data);
3126
52dc124a
DE
3127 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3128 *slot = sig_type;
1fd400ff 3129
b2bdb8cf 3130 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3131 }
3132
673bfd45 3133 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3134}
3135
927aa2e7
JK
3136/* Create the signatured type hash table from .debug_names. */
3137
3138static void
3139create_signatured_type_table_from_debug_names
ed2dc618 3140 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3141 const mapped_debug_names &map,
3142 struct dwarf2_section_info *section,
3143 struct dwarf2_section_info *abbrev_section)
3144{
ed2dc618
SM
3145 struct objfile *objfile = dwarf2_per_objfile->objfile;
3146
927aa2e7
JK
3147 dwarf2_read_section (objfile, section);
3148 dwarf2_read_section (objfile, abbrev_section);
3149
b2bdb8cf
SM
3150 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3151 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3152
3153 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3154
3155 for (uint32_t i = 0; i < map.tu_count; ++i)
3156 {
3157 struct signatured_type *sig_type;
927aa2e7 3158 void **slot;
927aa2e7
JK
3159
3160 sect_offset sect_off
3161 = (sect_offset) (extract_unsigned_integer
3162 (map.tu_table_reordered + i * map.offset_size,
3163 map.offset_size,
3164 map.dwarf5_byte_order));
3165
3166 comp_unit_head cu_header;
ed2dc618
SM
3167 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3168 abbrev_section,
927aa2e7
JK
3169 section->buffer + to_underlying (sect_off),
3170 rcuh_kind::TYPE);
3171
3172 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3173 struct signatured_type);
3174 sig_type->signature = cu_header.signature;
3175 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3176 sig_type->per_cu.is_debug_types = 1;
3177 sig_type->per_cu.section = section;
3178 sig_type->per_cu.sect_off = sect_off;
e3b94546 3179 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3180 sig_type->per_cu.v.quick
3181 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3182 struct dwarf2_per_cu_quick_data);
3183
3184 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3185 *slot = sig_type;
3186
b2bdb8cf 3187 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3188 }
3189
3190 dwarf2_per_objfile->signatured_types = sig_types_hash;
3191}
3192
9291a0cd
TT
3193/* Read the address map data from the mapped index, and use it to
3194 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3195
9291a0cd 3196static void
ed2dc618
SM
3197create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3198 struct mapped_index *index)
9291a0cd 3199{
ed2dc618 3200 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3201 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3202 const gdb_byte *iter, *end;
9291a0cd 3203 struct addrmap *mutable_map;
9291a0cd
TT
3204 CORE_ADDR baseaddr;
3205
8268c778
PA
3206 auto_obstack temp_obstack;
3207
9291a0cd
TT
3208 mutable_map = addrmap_create_mutable (&temp_obstack);
3209
f00a2de2
PA
3210 iter = index->address_table.data ();
3211 end = iter + index->address_table.size ();
9291a0cd 3212
b3b3bada 3213 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
3214
3215 while (iter < end)
3216 {
3217 ULONGEST hi, lo, cu_index;
3218 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3219 iter += 8;
3220 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3221 iter += 8;
3222 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3223 iter += 4;
f652bce2 3224
24a55014 3225 if (lo > hi)
f652bce2 3226 {
b98664d3 3227 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3228 hex_string (lo), hex_string (hi));
24a55014 3229 continue;
f652bce2 3230 }
24a55014 3231
b76e467d 3232 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3233 {
b98664d3 3234 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3235 (unsigned) cu_index);
24a55014 3236 continue;
f652bce2 3237 }
24a55014 3238
79748972
TT
3239 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3240 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3241 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3242 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3243 }
3244
d320c2b5 3245 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3246 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3247}
3248
927aa2e7
JK
3249/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3250 populate the objfile's psymtabs_addrmap. */
3251
3252static void
ed2dc618 3253create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3254 struct dwarf2_section_info *section)
3255{
ed2dc618 3256 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3257 bfd *abfd = objfile->obfd;
3258 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 3259 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
3260
3261 auto_obstack temp_obstack;
3262 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3263
3264 std::unordered_map<sect_offset,
3265 dwarf2_per_cu_data *,
3266 gdb::hash_enum<sect_offset>>
3267 debug_info_offset_to_per_cu;
b76e467d 3268 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3269 {
927aa2e7
JK
3270 const auto insertpair
3271 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3272 if (!insertpair.second)
3273 {
3274 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3275 "debug_info_offset %s, ignoring .debug_aranges."),
3276 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3277 return;
3278 }
3279 }
3280
3281 dwarf2_read_section (objfile, section);
3282
3283 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3284
3285 const gdb_byte *addr = section->buffer;
3286
3287 while (addr < section->buffer + section->size)
3288 {
3289 const gdb_byte *const entry_addr = addr;
3290 unsigned int bytes_read;
3291
3292 const LONGEST entry_length = read_initial_length (abfd, addr,
3293 &bytes_read);
3294 addr += bytes_read;
3295
3296 const gdb_byte *const entry_end = addr + entry_length;
3297 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3298 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3299 if (addr + entry_length > section->buffer + section->size)
3300 {
47e3f474 3301 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3302 "length %s exceeds section length %s, "
3303 "ignoring .debug_aranges."),
47e3f474
TV
3304 objfile_name (objfile),
3305 plongest (entry_addr - section->buffer),
927aa2e7
JK
3306 plongest (bytes_read + entry_length),
3307 pulongest (section->size));
3308 return;
3309 }
3310
3311 /* The version number. */
3312 const uint16_t version = read_2_bytes (abfd, addr);
3313 addr += 2;
3314 if (version != 2)
3315 {
47e3f474 3316 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3317 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3318 objfile_name (objfile),
3319 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3320 return;
3321 }
3322
3323 const uint64_t debug_info_offset
3324 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3325 addr += offset_size;
3326 const auto per_cu_it
3327 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3328 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3329 {
47e3f474 3330 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3331 "debug_info_offset %s does not exists, "
3332 "ignoring .debug_aranges."),
47e3f474
TV
3333 objfile_name (objfile),
3334 plongest (entry_addr - section->buffer),
927aa2e7
JK
3335 pulongest (debug_info_offset));
3336 return;
3337 }
3338 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3339
3340 const uint8_t address_size = *addr++;
3341 if (address_size < 1 || address_size > 8)
3342 {
47e3f474 3343 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3344 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3345 objfile_name (objfile),
3346 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3347 return;
3348 }
3349
3350 const uint8_t segment_selector_size = *addr++;
3351 if (segment_selector_size != 0)
3352 {
47e3f474 3353 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3354 "segment_selector_size %u is not supported, "
3355 "ignoring .debug_aranges."),
47e3f474
TV
3356 objfile_name (objfile),
3357 plongest (entry_addr - section->buffer),
927aa2e7
JK
3358 segment_selector_size);
3359 return;
3360 }
3361
3362 /* Must pad to an alignment boundary that is twice the address
3363 size. It is undocumented by the DWARF standard but GCC does
3364 use it. */
3365 for (size_t padding = ((-(addr - section->buffer))
3366 & (2 * address_size - 1));
3367 padding > 0; padding--)
3368 if (*addr++ != 0)
3369 {
47e3f474 3370 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3371 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3372 objfile_name (objfile),
3373 plongest (entry_addr - section->buffer));
927aa2e7
JK
3374 return;
3375 }
3376
3377 for (;;)
3378 {
3379 if (addr + 2 * address_size > entry_end)
3380 {
47e3f474 3381 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3382 "address list is not properly terminated, "
3383 "ignoring .debug_aranges."),
47e3f474
TV
3384 objfile_name (objfile),
3385 plongest (entry_addr - section->buffer));
927aa2e7
JK
3386 return;
3387 }
3388 ULONGEST start = extract_unsigned_integer (addr, address_size,
3389 dwarf5_byte_order);
3390 addr += address_size;
3391 ULONGEST length = extract_unsigned_integer (addr, address_size,
3392 dwarf5_byte_order);
3393 addr += address_size;
3394 if (start == 0 && length == 0)
3395 break;
3396 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3397 {
3398 /* Symbol was eliminated due to a COMDAT group. */
3399 continue;
3400 }
3401 ULONGEST end = start + length;
79748972
TT
3402 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3403 - baseaddr);
3404 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3405 - baseaddr);
927aa2e7
JK
3406 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3407 }
3408 }
3409
d320c2b5 3410 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3411 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3412}
3413
9291a0cd
TT
3414/* Find a slot in the mapped index INDEX for the object named NAME.
3415 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3416 constant pool and return true. If NAME cannot be found, return
3417 false. */
2fdf6df6 3418
109483d9 3419static bool
9291a0cd
TT
3420find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3421 offset_type **vec_out)
3422{
0cf03b49 3423 offset_type hash;
9291a0cd 3424 offset_type slot, step;
559a7a62 3425 int (*cmp) (const char *, const char *);
9291a0cd 3426
791afaa2 3427 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3428 if (current_language->la_language == language_cplus
45280282
IB
3429 || current_language->la_language == language_fortran
3430 || current_language->la_language == language_d)
0cf03b49
JK
3431 {
3432 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3433 not contain any. */
a8719064 3434
72998fb3 3435 if (strchr (name, '(') != NULL)
0cf03b49 3436 {
109483d9 3437 without_params = cp_remove_params (name);
0cf03b49 3438
72998fb3 3439 if (without_params != NULL)
791afaa2 3440 name = without_params.get ();
0cf03b49
JK
3441 }
3442 }
3443
559a7a62 3444 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3445 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3446 simulate our NAME being searched is also lowercased. */
3447 hash = mapped_index_string_hash ((index->version == 4
3448 && case_sensitivity == case_sensitive_off
3449 ? 5 : index->version),
3450 name);
3451
f00a2de2
PA
3452 slot = hash & (index->symbol_table.size () - 1);
3453 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3454 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3455
3456 for (;;)
3457 {
9291a0cd 3458 const char *str;
f00a2de2
PA
3459
3460 const auto &bucket = index->symbol_table[slot];
3461 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3462 return false;
9291a0cd 3463
f00a2de2 3464 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3465 if (!cmp (name, str))
9291a0cd
TT
3466 {
3467 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3468 + MAYBE_SWAP (bucket.vec));
109483d9 3469 return true;
9291a0cd
TT
3470 }
3471
f00a2de2 3472 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3473 }
3474}
3475
4485a1c1
SM
3476/* A helper function that reads the .gdb_index from BUFFER and fills
3477 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3478 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3479 ok to use deprecated sections.
3480
3481 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3482 out parameters that are filled in with information about the CU and
3483 TU lists in the section.
3484
4485a1c1 3485 Returns true if all went well, false otherwise. */
2fdf6df6 3486
d33bc52e 3487static bool
4485a1c1
SM
3488read_gdb_index_from_buffer (struct objfile *objfile,
3489 const char *filename,
3490 bool deprecated_ok,
3491 gdb::array_view<const gdb_byte> buffer,
3492 struct mapped_index *map,
3493 const gdb_byte **cu_list,
3494 offset_type *cu_list_elements,
3495 const gdb_byte **types_list,
3496 offset_type *types_list_elements)
3497{
3498 const gdb_byte *addr = &buffer[0];
82430852 3499
9291a0cd 3500 /* Version check. */
4485a1c1 3501 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3502 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3503 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3504 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3505 indices. */
831adc1f 3506 if (version < 4)
481860b3
GB
3507 {
3508 static int warning_printed = 0;
3509 if (!warning_printed)
3510 {
3511 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3512 filename);
481860b3
GB
3513 warning_printed = 1;
3514 }
3515 return 0;
3516 }
3517 /* Index version 4 uses a different hash function than index version
3518 5 and later.
3519
3520 Versions earlier than 6 did not emit psymbols for inlined
3521 functions. Using these files will cause GDB not to be able to
3522 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3523 indices unless the user has done
3524 "set use-deprecated-index-sections on". */
2ec9a5e0 3525 if (version < 6 && !deprecated_ok)
481860b3
GB
3526 {
3527 static int warning_printed = 0;
3528 if (!warning_printed)
3529 {
e615022a
DE
3530 warning (_("\
3531Skipping deprecated .gdb_index section in %s.\n\
3532Do \"set use-deprecated-index-sections on\" before the file is read\n\
3533to use the section anyway."),
2ec9a5e0 3534 filename);
481860b3
GB
3535 warning_printed = 1;
3536 }
3537 return 0;
3538 }
796a7ff8 3539 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3540 of the TU (for symbols coming from TUs),
3541 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3542 Plus gold-generated indices can have duplicate entries for global symbols,
3543 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3544 These are just performance bugs, and we can't distinguish gdb-generated
3545 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3546
481860b3 3547 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3548 longer backward compatible. */
796a7ff8 3549 if (version > 8)
594e8718 3550 return 0;
9291a0cd 3551
559a7a62 3552 map->version = version;
9291a0cd 3553
4485a1c1 3554 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3555
4485a1c1 3556 int i = 0;
2ec9a5e0
TT
3557 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3558 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3559 / 8);
1fd400ff
TT
3560 ++i;
3561
2ec9a5e0
TT
3562 *types_list = addr + MAYBE_SWAP (metadata[i]);
3563 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3564 - MAYBE_SWAP (metadata[i]))
3565 / 8);
987d643c 3566 ++i;
1fd400ff 3567
f00a2de2
PA
3568 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3569 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3570 map->address_table
3571 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3572 ++i;
3573
f00a2de2
PA
3574 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3575 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3576 map->symbol_table
3577 = gdb::array_view<mapped_index::symbol_table_slot>
3578 ((mapped_index::symbol_table_slot *) symbol_table,
3579 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3580
f00a2de2 3581 ++i;
f9d83a0b 3582 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3583
2ec9a5e0
TT
3584 return 1;
3585}
3586
4485a1c1
SM
3587/* Callback types for dwarf2_read_gdb_index. */
3588
3589typedef gdb::function_view
3590 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3591 get_gdb_index_contents_ftype;
3592typedef gdb::function_view
3593 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3594 get_gdb_index_contents_dwz_ftype;
3595
927aa2e7 3596/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3597 elements of all the CUs and return 1. Otherwise, return 0. */
3598
3599static int
4485a1c1
SM
3600dwarf2_read_gdb_index
3601 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3602 get_gdb_index_contents_ftype get_gdb_index_contents,
3603 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3604{
2ec9a5e0
TT
3605 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3606 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3607 struct dwz_file *dwz;
12359b5e 3608 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3609
4485a1c1
SM
3610 gdb::array_view<const gdb_byte> main_index_contents
3611 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3612
3613 if (main_index_contents.empty ())
3614 return 0;
3615
3063847f 3616 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3617 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3618 use_deprecated_index_sections,
3619 main_index_contents, map.get (), &cu_list,
3620 &cu_list_elements, &types_list,
3621 &types_list_elements))
2ec9a5e0
TT
3622 return 0;
3623
0fefef59 3624 /* Don't use the index if it's empty. */
3063847f 3625 if (map->symbol_table.empty ())
0fefef59
DE
3626 return 0;
3627
2ec9a5e0
TT
3628 /* If there is a .dwz file, read it so we can get its CU list as
3629 well. */
ed2dc618 3630 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3631 if (dwz != NULL)
2ec9a5e0 3632 {
2ec9a5e0
TT
3633 struct mapped_index dwz_map;
3634 const gdb_byte *dwz_types_ignore;
3635 offset_type dwz_types_elements_ignore;
3636
4485a1c1
SM
3637 gdb::array_view<const gdb_byte> dwz_index_content
3638 = get_gdb_index_contents_dwz (objfile, dwz);
3639
3640 if (dwz_index_content.empty ())
3641 return 0;
3642
3643 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3644 bfd_get_filename (dwz->dwz_bfd.get ()),
3645 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3646 &dwz_list, &dwz_list_elements,
3647 &dwz_types_ignore,
3648 &dwz_types_elements_ignore))
2ec9a5e0
TT
3649 {
3650 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3651 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3652 return 0;
3653 }
3654 }
3655
12359b5e
SM
3656 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3657 dwz_list, dwz_list_elements);
1fd400ff 3658
8b70b953
TT
3659 if (types_list_elements)
3660 {
8b70b953
TT
3661 /* We can only handle a single .debug_types when we have an
3662 index. */
fd5866f6 3663 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3664 return 0;
3665
fd5866f6 3666 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3667
12359b5e
SM
3668 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3669 types_list, types_list_elements);
8b70b953 3670 }
9291a0cd 3671
3063847f 3672 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3673
3063847f 3674 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3675 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3676 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3677 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3678
3679 return 1;
3680}
3681
dee91e82 3682/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3683
dee91e82
DE
3684static void
3685dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3686 const gdb_byte *info_ptr,
dee91e82 3687 struct die_info *comp_unit_die,
c0ab21c2 3688 int has_children)
9291a0cd 3689{
dee91e82 3690 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3691 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3692 struct dwarf2_per_objfile *dwarf2_per_objfile
3693 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3694 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3695 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3696 struct attribute *attr;
7b9f3c50
DE
3697 void **slot;
3698 struct quick_file_names *qfn;
9291a0cd 3699
0186c6a7
DE
3700 gdb_assert (! this_cu->is_debug_types);
3701
07261596
TT
3702 /* Our callers never want to match partial units -- instead they
3703 will match the enclosing full CU. */
3704 if (comp_unit_die->tag == DW_TAG_partial_unit)
3705 {
3706 this_cu->v.quick->no_file_data = 1;
3707 return;
3708 }
3709
0186c6a7 3710 lh_cu = this_cu;
7b9f3c50 3711 slot = NULL;
dee91e82 3712
fff8551c 3713 line_header_up lh;
9c541725 3714 sect_offset line_offset {};
fff8551c 3715
dee91e82 3716 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3717 if (attr != nullptr)
9291a0cd 3718 {
7b9f3c50
DE
3719 struct quick_file_names find_entry;
3720
9c541725 3721 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3722
3723 /* We may have already read in this line header (TU line header sharing).
3724 If we have we're done. */
094b34ac 3725 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3726 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3727 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3728 &find_entry, INSERT);
3729 if (*slot != NULL)
3730 {
9a3c8263 3731 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3732 return;
7b9f3c50
DE
3733 }
3734
3019eac3 3735 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3736 }
3737 if (lh == NULL)
3738 {
094b34ac 3739 lh_cu->v.quick->no_file_data = 1;
dee91e82 3740 return;
9291a0cd
TT
3741 }
3742
8d749320 3743 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3744 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3745 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3746 gdb_assert (slot != NULL);
3747 *slot = qfn;
9291a0cd 3748
d721ba37 3749 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3750
aa391654
TT
3751 int offset = 0;
3752 if (strcmp (fnd.name, "<unknown>") != 0)
3753 ++offset;
3754
7ba99d21 3755 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3756 qfn->file_names =
aa391654
TT
3757 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3758 if (offset != 0)
3759 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3760 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3761 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3762 qfn->real_names = NULL;
9291a0cd 3763
094b34ac 3764 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3765}
3766
3767/* A helper for the "quick" functions which attempts to read the line
3768 table for THIS_CU. */
3769
3770static struct quick_file_names *
e4a48d9d 3771dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3772{
0186c6a7
DE
3773 /* This should never be called for TUs. */
3774 gdb_assert (! this_cu->is_debug_types);
3775 /* Nor type unit groups. */
3776 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3777
dee91e82
DE
3778 if (this_cu->v.quick->file_names != NULL)
3779 return this_cu->v.quick->file_names;
3780 /* If we know there is no line data, no point in looking again. */
3781 if (this_cu->v.quick->no_file_data)
3782 return NULL;
3783
c0ab21c2
TT
3784 cutu_reader reader (this_cu);
3785 if (!reader.dummy_p)
3786 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3787 reader.has_children);
dee91e82
DE
3788
3789 if (this_cu->v.quick->no_file_data)
3790 return NULL;
3791 return this_cu->v.quick->file_names;
9291a0cd
TT
3792}
3793
3794/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3795 real path for a given file name from the line table. */
2fdf6df6 3796
9291a0cd 3797static const char *
7b9f3c50
DE
3798dw2_get_real_path (struct objfile *objfile,
3799 struct quick_file_names *qfn, int index)
9291a0cd 3800{
7b9f3c50
DE
3801 if (qfn->real_names == NULL)
3802 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3803 qfn->num_file_names, const char *);
9291a0cd 3804
7b9f3c50 3805 if (qfn->real_names[index] == NULL)
14278e1f 3806 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3807
7b9f3c50 3808 return qfn->real_names[index];
9291a0cd
TT
3809}
3810
3811static struct symtab *
3812dw2_find_last_source_symtab (struct objfile *objfile)
3813{
ed2dc618
SM
3814 struct dwarf2_per_objfile *dwarf2_per_objfile
3815 = get_dwarf2_per_objfile (objfile);
b76e467d 3816 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3817 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3818
43f3e411
DE
3819 if (cust == NULL)
3820 return NULL;
ed2dc618 3821
43f3e411 3822 return compunit_primary_filetab (cust);
9291a0cd
TT
3823}
3824
7b9f3c50
DE
3825/* Traversal function for dw2_forget_cached_source_info. */
3826
3827static int
3828dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3829{
7b9f3c50 3830 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3831
7b9f3c50 3832 if (file_data->real_names)
9291a0cd 3833 {
7b9f3c50 3834 int i;
9291a0cd 3835
7b9f3c50 3836 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3837 {
7b9f3c50
DE
3838 xfree ((void*) file_data->real_names[i]);
3839 file_data->real_names[i] = NULL;
9291a0cd
TT
3840 }
3841 }
7b9f3c50
DE
3842
3843 return 1;
3844}
3845
3846static void
3847dw2_forget_cached_source_info (struct objfile *objfile)
3848{
ed2dc618
SM
3849 struct dwarf2_per_objfile *dwarf2_per_objfile
3850 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3851
3852 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3853 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3854}
3855
f8eba3c6
TT
3856/* Helper function for dw2_map_symtabs_matching_filename that expands
3857 the symtabs and calls the iterator. */
3858
3859static int
3860dw2_map_expand_apply (struct objfile *objfile,
3861 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3862 const char *name, const char *real_path,
14bc53a8 3863 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3864{
43f3e411 3865 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3866
3867 /* Don't visit already-expanded CUs. */
43f3e411 3868 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3869 return 0;
3870
3871 /* This may expand more than one symtab, and we want to iterate over
3872 all of them. */
58f0c718 3873 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3874
14bc53a8
PA
3875 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3876 last_made, callback);
f8eba3c6
TT
3877}
3878
3879/* Implementation of the map_symtabs_matching_filename method. */
3880
14bc53a8
PA
3881static bool
3882dw2_map_symtabs_matching_filename
3883 (struct objfile *objfile, const char *name, const char *real_path,
3884 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3885{
c011a4f4 3886 const char *name_basename = lbasename (name);
ed2dc618
SM
3887 struct dwarf2_per_objfile *dwarf2_per_objfile
3888 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3889
848e3e78
DE
3890 /* The rule is CUs specify all the files, including those used by
3891 any TU, so there's no need to scan TUs here. */
f4dc4d17 3892
b76e467d 3893 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3894 {
3d7bb9d9 3895 /* We only need to look at symtabs not already expanded. */
43f3e411 3896 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3897 continue;
3898
b76e467d 3899 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3900 if (file_data == NULL)
9291a0cd
TT
3901 continue;
3902
b76e467d 3903 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3904 {
7b9f3c50 3905 const char *this_name = file_data->file_names[j];
da235a7c 3906 const char *this_real_name;
9291a0cd 3907
af529f8f 3908 if (compare_filenames_for_search (this_name, name))
9291a0cd 3909 {
f5b95b50 3910 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3911 callback))
3912 return true;
288e77a7 3913 continue;
4aac40c8 3914 }
9291a0cd 3915
c011a4f4
DE
3916 /* Before we invoke realpath, which can get expensive when many
3917 files are involved, do a quick comparison of the basenames. */
3918 if (! basenames_may_differ
3919 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3920 continue;
3921
da235a7c
JK
3922 this_real_name = dw2_get_real_path (objfile, file_data, j);
3923 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3924 {
da235a7c 3925 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3926 callback))
3927 return true;
288e77a7 3928 continue;
da235a7c 3929 }
9291a0cd 3930
da235a7c
JK
3931 if (real_path != NULL)
3932 {
af529f8f
JK
3933 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3934 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3935 if (this_real_name != NULL
af529f8f 3936 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3937 {
f5b95b50 3938 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3939 callback))
3940 return true;
288e77a7 3941 continue;
9291a0cd
TT
3942 }
3943 }
3944 }
3945 }
3946
14bc53a8 3947 return false;
9291a0cd
TT
3948}
3949
da51c347
DE
3950/* Struct used to manage iterating over all CUs looking for a symbol. */
3951
3952struct dw2_symtab_iterator
9291a0cd 3953{
ed2dc618
SM
3954 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3955 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3956 /* If set, only look for symbols that match that block. Valid values are
3957 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3958 gdb::optional<block_enum> block_index;
da51c347
DE
3959 /* The kind of symbol we're looking for. */
3960 domain_enum domain;
3961 /* The list of CUs from the index entry of the symbol,
3962 or NULL if not found. */
3963 offset_type *vec;
3964 /* The next element in VEC to look at. */
3965 int next;
3966 /* The number of elements in VEC, or zero if there is no match. */
3967 int length;
8943b874
DE
3968 /* Have we seen a global version of the symbol?
3969 If so we can ignore all further global instances.
3970 This is to work around gold/15646, inefficient gold-generated
3971 indices. */
3972 int global_seen;
da51c347 3973};
9291a0cd 3974
2b79f376 3975/* Initialize the index symtab iterator ITER. */
2fdf6df6 3976
9291a0cd 3977static void
da51c347 3978dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3979 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3980 gdb::optional<block_enum> block_index,
da51c347
DE
3981 domain_enum domain,
3982 const char *name)
3983{
ed2dc618 3984 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3985 iter->block_index = block_index;
3986 iter->domain = domain;
3987 iter->next = 0;
8943b874 3988 iter->global_seen = 0;
da51c347 3989
3063847f 3990 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3991
3992 /* index is NULL if OBJF_READNOW. */
3993 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3994 iter->length = MAYBE_SWAP (*iter->vec);
3995 else
3996 {
3997 iter->vec = NULL;
3998 iter->length = 0;
3999 }
4000}
4001
4002/* Return the next matching CU or NULL if there are no more. */
4003
4004static struct dwarf2_per_cu_data *
4005dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4006{
ed2dc618
SM
4007 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4008
da51c347
DE
4009 for ( ; iter->next < iter->length; ++iter->next)
4010 {
4011 offset_type cu_index_and_attrs =
4012 MAYBE_SWAP (iter->vec[iter->next + 1]);
4013 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
4014 gdb_index_symbol_kind symbol_kind =
4015 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4016 /* Only check the symbol attributes if they're present.
4017 Indices prior to version 7 don't record them,
4018 and indices >= 7 may elide them for certain symbols
4019 (gold does this). */
4020 int attrs_valid =
ed2dc618 4021 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
4022 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4023
3190f0c6 4024 /* Don't crash on bad data. */
b76e467d 4025 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4026 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 4027 {
b98664d3 4028 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
4029 " [in module %s]"),
4030 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4031 continue;
4032 }
4033
ff4c9fec 4034 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 4035
da51c347 4036 /* Skip if already read in. */
43f3e411 4037 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4038 continue;
4039
8943b874
DE
4040 /* Check static vs global. */
4041 if (attrs_valid)
4042 {
2b79f376
SM
4043 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4044
4045 if (iter->block_index.has_value ())
4046 {
4047 bool want_static = *iter->block_index == STATIC_BLOCK;
4048
4049 if (is_static != want_static)
4050 continue;
4051 }
4052
8943b874
DE
4053 /* Work around gold/15646. */
4054 if (!is_static && iter->global_seen)
4055 continue;
4056 if (!is_static)
4057 iter->global_seen = 1;
4058 }
da51c347
DE
4059
4060 /* Only check the symbol's kind if it has one. */
4061 if (attrs_valid)
4062 {
4063 switch (iter->domain)
4064 {
4065 case VAR_DOMAIN:
4066 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4067 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4068 /* Some types are also in VAR_DOMAIN. */
4069 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4070 continue;
4071 break;
4072 case STRUCT_DOMAIN:
4073 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4074 continue;
4075 break;
4076 case LABEL_DOMAIN:
4077 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4078 continue;
4079 break;
59c35742
AB
4080 case MODULE_DOMAIN:
4081 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4082 continue;
4083 break;
da51c347
DE
4084 default:
4085 break;
4086 }
4087 }
4088
4089 ++iter->next;
4090 return per_cu;
4091 }
4092
4093 return NULL;
4094}
4095
43f3e411 4096static struct compunit_symtab *
c7f839cb 4097dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4098 const char *name, domain_enum domain)
9291a0cd 4099{
43f3e411 4100 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4101 struct dwarf2_per_objfile *dwarf2_per_objfile
4102 = get_dwarf2_per_objfile (objfile);
9291a0cd 4103
b5ec771e
PA
4104 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4105
ed2dc618
SM
4106 struct dw2_symtab_iterator iter;
4107 struct dwarf2_per_cu_data *per_cu;
da51c347 4108
2b79f376 4109 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4110
ed2dc618
SM
4111 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4112 {
4113 struct symbol *sym, *with_opaque = NULL;
58f0c718 4114 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4115 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4116 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4117
ed2dc618
SM
4118 sym = block_find_symbol (block, name, domain,
4119 block_find_non_opaque_type_preferred,
4120 &with_opaque);
b2e2f908 4121
ed2dc618
SM
4122 /* Some caution must be observed with overloaded functions
4123 and methods, since the index will not contain any overload
4124 information (but NAME might contain it). */
da51c347 4125
ed2dc618
SM
4126 if (sym != NULL
4127 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4128 return stab;
4129 if (with_opaque != NULL
4130 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4131 stab_best = stab;
da51c347 4132
ed2dc618 4133 /* Keep looking through other CUs. */
9291a0cd 4134 }
9291a0cd 4135
da51c347 4136 return stab_best;
9291a0cd
TT
4137}
4138
4139static void
4140dw2_print_stats (struct objfile *objfile)
4141{
ed2dc618
SM
4142 struct dwarf2_per_objfile *dwarf2_per_objfile
4143 = get_dwarf2_per_objfile (objfile);
b76e467d 4144 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4145 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4146 int count = 0;
9291a0cd 4147
ed2dc618 4148 for (int i = 0; i < total; ++i)
9291a0cd 4149 {
ff4c9fec 4150 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4151
43f3e411 4152 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4153 ++count;
4154 }
e4a48d9d 4155 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4156 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4157}
4158
779bd270
DE
4159/* This dumps minimal information about the index.
4160 It is called via "mt print objfiles".
4161 One use is to verify .gdb_index has been loaded by the
4162 gdb.dwarf2/gdb-index.exp testcase. */
4163
9291a0cd
TT
4164static void
4165dw2_dump (struct objfile *objfile)
4166{
ed2dc618
SM
4167 struct dwarf2_per_objfile *dwarf2_per_objfile
4168 = get_dwarf2_per_objfile (objfile);
4169
779bd270
DE
4170 gdb_assert (dwarf2_per_objfile->using_index);
4171 printf_filtered (".gdb_index:");
4172 if (dwarf2_per_objfile->index_table != NULL)
4173 {
4174 printf_filtered (" version %d\n",
4175 dwarf2_per_objfile->index_table->version);
4176 }
4177 else
4178 printf_filtered (" faked for \"readnow\"\n");
4179 printf_filtered ("\n");
9291a0cd
TT
4180}
4181
9291a0cd
TT
4182static void
4183dw2_expand_symtabs_for_function (struct objfile *objfile,
4184 const char *func_name)
4185{
ed2dc618
SM
4186 struct dwarf2_per_objfile *dwarf2_per_objfile
4187 = get_dwarf2_per_objfile (objfile);
da51c347 4188
ed2dc618
SM
4189 struct dw2_symtab_iterator iter;
4190 struct dwarf2_per_cu_data *per_cu;
da51c347 4191
2b79f376 4192 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4193
ed2dc618 4194 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4195 dw2_instantiate_symtab (per_cu, false);
da51c347 4196
9291a0cd
TT
4197}
4198
4199static void
4200dw2_expand_all_symtabs (struct objfile *objfile)
4201{
ed2dc618
SM
4202 struct dwarf2_per_objfile *dwarf2_per_objfile
4203 = get_dwarf2_per_objfile (objfile);
b76e467d 4204 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4205 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4206
ed2dc618 4207 for (int i = 0; i < total_units; ++i)
9291a0cd 4208 {
ff4c9fec 4209 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4210
58f0c718
TT
4211 /* We don't want to directly expand a partial CU, because if we
4212 read it with the wrong language, then assertion failures can
4213 be triggered later on. See PR symtab/23010. So, tell
4214 dw2_instantiate_symtab to skip partial CUs -- any important
4215 partial CU will be read via DW_TAG_imported_unit anyway. */
4216 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4217 }
4218}
4219
4220static void
652a8996
JK
4221dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4222 const char *fullname)
9291a0cd 4223{
ed2dc618
SM
4224 struct dwarf2_per_objfile *dwarf2_per_objfile
4225 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4226
4227 /* We don't need to consider type units here.
4228 This is only called for examining code, e.g. expand_line_sal.
4229 There can be an order of magnitude (or more) more type units
4230 than comp units, and we avoid them if we can. */
4231
b76e467d 4232 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4233 {
3d7bb9d9 4234 /* We only need to look at symtabs not already expanded. */
43f3e411 4235 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4236 continue;
4237
b76e467d 4238 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4239 if (file_data == NULL)
9291a0cd
TT
4240 continue;
4241
b76e467d 4242 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4243 {
652a8996
JK
4244 const char *this_fullname = file_data->file_names[j];
4245
4246 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4247 {
58f0c718 4248 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4249 break;
4250 }
4251 }
4252 }
4253}
4254
9291a0cd 4255static void
199b4314
TT
4256dw2_map_matching_symbols
4257 (struct objfile *objfile,
b054970d 4258 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4259 int global,
4260 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4261 symbol_compare_ftype *ordered_compare)
9291a0cd 4262{
40658b94 4263 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4264 current language is Ada for a non-Ada objfile using GNU index. As Ada
4265 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4266}
4267
e1ef7d7a
PA
4268/* Starting from a search name, return the string that finds the upper
4269 bound of all strings that start with SEARCH_NAME in a sorted name
4270 list. Returns the empty string to indicate that the upper bound is
4271 the end of the list. */
4272
4273static std::string
4274make_sort_after_prefix_name (const char *search_name)
4275{
4276 /* When looking to complete "func", we find the upper bound of all
4277 symbols that start with "func" by looking for where we'd insert
4278 the closest string that would follow "func" in lexicographical
4279 order. Usually, that's "func"-with-last-character-incremented,
4280 i.e. "fund". Mind non-ASCII characters, though. Usually those
4281 will be UTF-8 multi-byte sequences, but we can't be certain.
4282 Especially mind the 0xff character, which is a valid character in
4283 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4284 rule out compilers allowing it in identifiers. Note that
4285 conveniently, strcmp/strcasecmp are specified to compare
4286 characters interpreted as unsigned char. So what we do is treat
4287 the whole string as a base 256 number composed of a sequence of
4288 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4289 to 0, and carries 1 to the following more-significant position.
4290 If the very first character in SEARCH_NAME ends up incremented
4291 and carries/overflows, then the upper bound is the end of the
4292 list. The string after the empty string is also the empty
4293 string.
4294
4295 Some examples of this operation:
4296
4297 SEARCH_NAME => "+1" RESULT
4298
4299 "abc" => "abd"
4300 "ab\xff" => "ac"
4301 "\xff" "a" "\xff" => "\xff" "b"
4302 "\xff" => ""
4303 "\xff\xff" => ""
4304 "" => ""
4305
4306 Then, with these symbols for example:
4307
4308 func
4309 func1
4310 fund
4311
4312 completing "func" looks for symbols between "func" and
4313 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4314 which finds "func" and "func1", but not "fund".
4315
4316 And with:
4317
4318 funcÿ (Latin1 'ÿ' [0xff])
4319 funcÿ1
4320 fund
4321
4322 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4323 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4324
4325 And with:
4326
4327 ÿÿ (Latin1 'ÿ' [0xff])
4328 ÿÿ1
4329
4330 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4331 the end of the list.
4332 */
4333 std::string after = search_name;
4334 while (!after.empty () && (unsigned char) after.back () == 0xff)
4335 after.pop_back ();
4336 if (!after.empty ())
4337 after.back () = (unsigned char) after.back () + 1;
4338 return after;
4339}
4340
5c58de74 4341/* See declaration. */
61d96d7e 4342
5c58de74
PA
4343std::pair<std::vector<name_component>::const_iterator,
4344 std::vector<name_component>::const_iterator>
44ed8f3e 4345mapped_index_base::find_name_components_bounds
3b00ef10 4346 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4347{
5c58de74
PA
4348 auto *name_cmp
4349 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4350
3b00ef10
TT
4351 const char *lang_name
4352 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4353
3f563c84
PA
4354 /* Comparison function object for lower_bound that matches against a
4355 given symbol name. */
4356 auto lookup_compare_lower = [&] (const name_component &elem,
4357 const char *name)
4358 {
5c58de74 4359 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4360 const char *elem_name = elem_qualified + elem.name_offset;
4361 return name_cmp (elem_name, name) < 0;
4362 };
4363
4364 /* Comparison function object for upper_bound that matches against a
4365 given symbol name. */
4366 auto lookup_compare_upper = [&] (const char *name,
4367 const name_component &elem)
4368 {
5c58de74 4369 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4370 const char *elem_name = elem_qualified + elem.name_offset;
4371 return name_cmp (name, elem_name) < 0;
4372 };
4373
5c58de74
PA
4374 auto begin = this->name_components.begin ();
4375 auto end = this->name_components.end ();
3f563c84
PA
4376
4377 /* Find the lower bound. */
4378 auto lower = [&] ()
4379 {
3b00ef10 4380 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4381 return begin;
4382 else
3b00ef10 4383 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4384 } ();
4385
4386 /* Find the upper bound. */
4387 auto upper = [&] ()
4388 {
5c58de74 4389 if (lookup_name_without_params.completion_mode ())
3f563c84 4390 {
e1ef7d7a
PA
4391 /* In completion mode, we want UPPER to point past all
4392 symbols names that have the same prefix. I.e., with
4393 these symbols, and completing "func":
4394
4395 function << lower bound
4396 function1
4397 other_function << upper bound
4398
4399 We find the upper bound by looking for the insertion
4400 point of "func"-with-last-character-incremented,
4401 i.e. "fund". */
3b00ef10 4402 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4403 if (after.empty ())
3f563c84 4404 return end;
e6b2f5ef
PA
4405 return std::lower_bound (lower, end, after.c_str (),
4406 lookup_compare_lower);
3f563c84
PA
4407 }
4408 else
3b00ef10 4409 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4410 } ();
4411
5c58de74
PA
4412 return {lower, upper};
4413}
4414
4415/* See declaration. */
4416
4417void
44ed8f3e 4418mapped_index_base::build_name_components ()
5c58de74
PA
4419{
4420 if (!this->name_components.empty ())
4421 return;
4422
4423 this->name_components_casing = case_sensitivity;
4424 auto *name_cmp
4425 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4426
4427 /* The code below only knows how to break apart components of C++
4428 symbol names (and other languages that use '::' as
3b00ef10 4429 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4430 auto count = this->symbol_name_count ();
4431 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4432 {
44ed8f3e 4433 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4434 continue;
4435
4436 const char *name = this->symbol_name_at (idx);
4437
4438 /* Add each name component to the name component table. */
4439 unsigned int previous_len = 0;
3b00ef10
TT
4440
4441 if (strstr (name, "::") != nullptr)
4442 {
4443 for (unsigned int current_len = cp_find_first_component (name);
4444 name[current_len] != '\0';
4445 current_len += cp_find_first_component (name + current_len))
4446 {
4447 gdb_assert (name[current_len] == ':');
4448 this->name_components.push_back ({previous_len, idx});
4449 /* Skip the '::'. */
4450 current_len += 2;
4451 previous_len = current_len;
4452 }
4453 }
4454 else
5c58de74 4455 {
3b00ef10
TT
4456 /* Handle the Ada encoded (aka mangled) form here. */
4457 for (const char *iter = strstr (name, "__");
4458 iter != nullptr;
4459 iter = strstr (iter, "__"))
4460 {
4461 this->name_components.push_back ({previous_len, idx});
4462 iter += 2;
4463 previous_len = iter - name;
4464 }
5c58de74 4465 }
3b00ef10 4466
5c58de74
PA
4467 this->name_components.push_back ({previous_len, idx});
4468 }
4469
4470 /* Sort name_components elements by name. */
4471 auto name_comp_compare = [&] (const name_component &left,
4472 const name_component &right)
4473 {
4474 const char *left_qualified = this->symbol_name_at (left.idx);
4475 const char *right_qualified = this->symbol_name_at (right.idx);
4476
4477 const char *left_name = left_qualified + left.name_offset;
4478 const char *right_name = right_qualified + right.name_offset;
4479
4480 return name_cmp (left_name, right_name) < 0;
4481 };
4482
4483 std::sort (this->name_components.begin (),
4484 this->name_components.end (),
4485 name_comp_compare);
4486}
4487
4488/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4489 mapped_index_base instead of the containing objfile. This is split
4490 to a separate function in order to be able to unit test the
4491 name_components matching using a mock mapped_index_base. For each
5c58de74 4492 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4493 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4494
4495static void
4496dw2_expand_symtabs_matching_symbol
44ed8f3e 4497 (mapped_index_base &index,
5c58de74
PA
4498 const lookup_name_info &lookup_name_in,
4499 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4500 enum search_domain kind,
3b00ef10 4501 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4502{
4503 lookup_name_info lookup_name_without_params
4504 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4505
4506 /* Build the symbol name component sorted vector, if we haven't
4507 yet. */
4508 index.build_name_components ();
4509
3f563c84
PA
4510 /* The same symbol may appear more than once in the range though.
4511 E.g., if we're looking for symbols that complete "w", and we have
4512 a symbol named "w1::w2", we'll find the two name components for
4513 that same symbol in the range. To be sure we only call the
4514 callback once per symbol, we first collect the symbol name
4515 indexes that matched in a temporary vector and ignore
4516 duplicates. */
4517 std::vector<offset_type> matches;
3f563c84 4518
3b00ef10
TT
4519 struct name_and_matcher
4520 {
4521 symbol_name_matcher_ftype *matcher;
4522 const std::string &name;
4523
4524 bool operator== (const name_and_matcher &other) const
3f563c84 4525 {
3b00ef10
TT
4526 return matcher == other.matcher && name == other.name;
4527 }
4528 };
4529
4530 /* A vector holding all the different symbol name matchers, for all
4531 languages. */
4532 std::vector<name_and_matcher> matchers;
4533
4534 for (int i = 0; i < nr_languages; i++)
4535 {
4536 enum language lang_e = (enum language) i;
4537
4538 const language_defn *lang = language_def (lang_e);
4539 symbol_name_matcher_ftype *name_matcher
4540 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4541
3b00ef10
TT
4542 name_and_matcher key {
4543 name_matcher,
4544 lookup_name_without_params.language_lookup_name (lang_e)
4545 };
4546
4547 /* Don't insert the same comparison routine more than once.
4548 Note that we do this linear walk. This is not a problem in
4549 practice because the number of supported languages is
4550 low. */
4551 if (std::find (matchers.begin (), matchers.end (), key)
4552 != matchers.end ())
9291a0cd 4553 continue;
3b00ef10
TT
4554 matchers.push_back (std::move (key));
4555
4556 auto bounds
4557 = index.find_name_components_bounds (lookup_name_without_params,
4558 lang_e);
4559
4560 /* Now for each symbol name in range, check to see if we have a name
4561 match, and if so, call the MATCH_CALLBACK callback. */
4562
4563 for (; bounds.first != bounds.second; ++bounds.first)
4564 {
4565 const char *qualified = index.symbol_name_at (bounds.first->idx);
4566
4567 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4568 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4569 continue;
9291a0cd 4570
3b00ef10
TT
4571 matches.push_back (bounds.first->idx);
4572 }
3f563c84
PA
4573 }
4574
4575 std::sort (matches.begin (), matches.end ());
4576
4577 /* Finally call the callback, once per match. */
4578 ULONGEST prev = -1;
4579 for (offset_type idx : matches)
4580 {
4581 if (prev != idx)
4582 {
3b00ef10
TT
4583 if (!match_callback (idx))
4584 break;
3f563c84
PA
4585 prev = idx;
4586 }
4587 }
4588
4589 /* Above we use a type wider than idx's for 'prev', since 0 and
4590 (offset_type)-1 are both possible values. */
4591 static_assert (sizeof (prev) > sizeof (offset_type), "");
4592}
4593
c62446b1
PA
4594#if GDB_SELF_TEST
4595
4596namespace selftests { namespace dw2_expand_symtabs_matching {
4597
a3c5fafd
PA
4598/* A mock .gdb_index/.debug_names-like name index table, enough to
4599 exercise dw2_expand_symtabs_matching_symbol, which works with the
4600 mapped_index_base interface. Builds an index from the symbol list
4601 passed as parameter to the constructor. */
4602class mock_mapped_index : public mapped_index_base
c62446b1
PA
4603{
4604public:
a3c5fafd
PA
4605 mock_mapped_index (gdb::array_view<const char *> symbols)
4606 : m_symbol_table (symbols)
c62446b1
PA
4607 {}
4608
a3c5fafd 4609 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4610
a3c5fafd 4611 /* Return the number of names in the symbol table. */
632e107b 4612 size_t symbol_name_count () const override
c62446b1 4613 {
a3c5fafd 4614 return m_symbol_table.size ();
c62446b1
PA
4615 }
4616
a3c5fafd 4617 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4618 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4619 {
4620 return m_symbol_table[idx];
4621 }
c62446b1 4622
a3c5fafd
PA
4623private:
4624 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4625};
4626
4627/* Convenience function that converts a NULL pointer to a "<null>"
4628 string, to pass to print routines. */
4629
4630static const char *
4631string_or_null (const char *str)
4632{
4633 return str != NULL ? str : "<null>";
4634}
4635
4636/* Check if a lookup_name_info built from
4637 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4638 index. EXPECTED_LIST is the list of expected matches, in expected
4639 matching order. If no match expected, then an empty list is
4640 specified. Returns true on success. On failure prints a warning
4641 indicating the file:line that failed, and returns false. */
4642
4643static bool
4644check_match (const char *file, int line,
4645 mock_mapped_index &mock_index,
4646 const char *name, symbol_name_match_type match_type,
4647 bool completion_mode,
4648 std::initializer_list<const char *> expected_list)
4649{
4650 lookup_name_info lookup_name (name, match_type, completion_mode);
4651
4652 bool matched = true;
4653
4654 auto mismatch = [&] (const char *expected_str,
4655 const char *got)
4656 {
4657 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4658 "expected=\"%s\", got=\"%s\"\n"),
4659 file, line,
4660 (match_type == symbol_name_match_type::FULL
4661 ? "FULL" : "WILD"),
4662 name, string_or_null (expected_str), string_or_null (got));
4663 matched = false;
4664 };
4665
4666 auto expected_it = expected_list.begin ();
4667 auto expected_end = expected_list.end ();
4668
a3c5fafd 4669 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4670 NULL, ALL_DOMAIN,
4671 [&] (offset_type idx)
4672 {
a3c5fafd 4673 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4674 const char *expected_str
4675 = expected_it == expected_end ? NULL : *expected_it++;
4676
4677 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4678 mismatch (expected_str, matched_name);
3b00ef10 4679 return true;
c62446b1
PA
4680 });
4681
4682 const char *expected_str
4683 = expected_it == expected_end ? NULL : *expected_it++;
4684 if (expected_str != NULL)
4685 mismatch (expected_str, NULL);
4686
4687 return matched;
4688}
4689
4690/* The symbols added to the mock mapped_index for testing (in
4691 canonical form). */
4692static const char *test_symbols[] = {
4693 "function",
4694 "std::bar",
4695 "std::zfunction",
4696 "std::zfunction2",
4697 "w1::w2",
4698 "ns::foo<char*>",
4699 "ns::foo<int>",
4700 "ns::foo<long>",
a20714ff
PA
4701 "ns2::tmpl<int>::foo2",
4702 "(anonymous namespace)::A::B::C",
c62446b1 4703
e1ef7d7a
PA
4704 /* These are used to check that the increment-last-char in the
4705 matching algorithm for completion doesn't match "t1_fund" when
4706 completing "t1_func". */
4707 "t1_func",
4708 "t1_func1",
4709 "t1_fund",
4710 "t1_fund1",
4711
4712 /* A UTF-8 name with multi-byte sequences to make sure that
4713 cp-name-parser understands this as a single identifier ("função"
4714 is "function" in PT). */
4715 u8"u8função",
4716
4717 /* \377 (0xff) is Latin1 'ÿ'. */
4718 "yfunc\377",
4719
4720 /* \377 (0xff) is Latin1 'ÿ'. */
4721 "\377",
4722 "\377\377123",
4723
c62446b1
PA
4724 /* A name with all sorts of complications. Starts with "z" to make
4725 it easier for the completion tests below. */
4726#define Z_SYM_NAME \
4727 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4728 "::tuple<(anonymous namespace)::ui*, " \
4729 "std::default_delete<(anonymous namespace)::ui>, void>"
4730
4731 Z_SYM_NAME
4732};
4733
a3c5fafd
PA
4734/* Returns true if the mapped_index_base::find_name_component_bounds
4735 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4736 in completion mode. */
5c58de74
PA
4737
4738static bool
a3c5fafd 4739check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4740 const char *search_name,
4741 gdb::array_view<const char *> expected_syms)
4742{
4743 lookup_name_info lookup_name (search_name,
4744 symbol_name_match_type::FULL, true);
4745
3b00ef10
TT
4746 auto bounds = index.find_name_components_bounds (lookup_name,
4747 language_cplus);
5c58de74
PA
4748
4749 size_t distance = std::distance (bounds.first, bounds.second);
4750 if (distance != expected_syms.size ())
4751 return false;
4752
4753 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4754 {
4755 auto nc_elem = bounds.first + exp_elem;
4756 const char *qualified = index.symbol_name_at (nc_elem->idx);
4757 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4758 return false;
4759 }
4760
4761 return true;
4762}
4763
4764/* Test the lower-level mapped_index::find_name_component_bounds
4765 method. */
4766
c62446b1 4767static void
5c58de74
PA
4768test_mapped_index_find_name_component_bounds ()
4769{
4770 mock_mapped_index mock_index (test_symbols);
4771
a3c5fafd 4772 mock_index.build_name_components ();
5c58de74
PA
4773
4774 /* Test the lower-level mapped_index::find_name_component_bounds
4775 method in completion mode. */
4776 {
4777 static const char *expected_syms[] = {
4778 "t1_func",
4779 "t1_func1",
5c58de74
PA
4780 };
4781
a3c5fafd 4782 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4783 "t1_func", expected_syms));
4784 }
4785
4786 /* Check that the increment-last-char in the name matching algorithm
4787 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4788 {
4789 static const char *expected_syms1[] = {
4790 "\377",
4791 "\377\377123",
4792 };
a3c5fafd 4793 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4794 "\377", expected_syms1));
4795
4796 static const char *expected_syms2[] = {
4797 "\377\377123",
4798 };
a3c5fafd 4799 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4800 "\377\377", expected_syms2));
4801 }
4802}
4803
4804/* Test dw2_expand_symtabs_matching_symbol. */
4805
4806static void
4807test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4808{
4809 mock_mapped_index mock_index (test_symbols);
4810
4811 /* We let all tests run until the end even if some fails, for debug
4812 convenience. */
4813 bool any_mismatch = false;
4814
4815 /* Create the expected symbols list (an initializer_list). Needed
4816 because lists have commas, and we need to pass them to CHECK,
4817 which is a macro. */
4818#define EXPECT(...) { __VA_ARGS__ }
4819
4820 /* Wrapper for check_match that passes down the current
4821 __FILE__/__LINE__. */
4822#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4823 any_mismatch |= !check_match (__FILE__, __LINE__, \
4824 mock_index, \
4825 NAME, MATCH_TYPE, COMPLETION_MODE, \
4826 EXPECTED_LIST)
4827
4828 /* Identity checks. */
4829 for (const char *sym : test_symbols)
4830 {
4831 /* Should be able to match all existing symbols. */
4832 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4833 EXPECT (sym));
4834
4835 /* Should be able to match all existing symbols with
4836 parameters. */
4837 std::string with_params = std::string (sym) + "(int)";
4838 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4839 EXPECT (sym));
4840
4841 /* Should be able to match all existing symbols with
4842 parameters and qualifiers. */
4843 with_params = std::string (sym) + " ( int ) const";
4844 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4845 EXPECT (sym));
4846
4847 /* This should really find sym, but cp-name-parser.y doesn't
4848 know about lvalue/rvalue qualifiers yet. */
4849 with_params = std::string (sym) + " ( int ) &&";
4850 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4851 {});
4852 }
4853
e1ef7d7a
PA
4854 /* Check that the name matching algorithm for completion doesn't get
4855 confused with Latin1 'ÿ' / 0xff. */
4856 {
4857 static const char str[] = "\377";
4858 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4859 EXPECT ("\377", "\377\377123"));
4860 }
4861
4862 /* Check that the increment-last-char in the matching algorithm for
4863 completion doesn't match "t1_fund" when completing "t1_func". */
4864 {
4865 static const char str[] = "t1_func";
4866 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4867 EXPECT ("t1_func", "t1_func1"));
4868 }
4869
c62446b1
PA
4870 /* Check that completion mode works at each prefix of the expected
4871 symbol name. */
4872 {
4873 static const char str[] = "function(int)";
4874 size_t len = strlen (str);
4875 std::string lookup;
4876
4877 for (size_t i = 1; i < len; i++)
4878 {
4879 lookup.assign (str, i);
4880 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4881 EXPECT ("function"));
4882 }
4883 }
4884
4885 /* While "w" is a prefix of both components, the match function
4886 should still only be called once. */
4887 {
4888 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4889 EXPECT ("w1::w2"));
a20714ff
PA
4890 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4891 EXPECT ("w1::w2"));
c62446b1
PA
4892 }
4893
4894 /* Same, with a "complicated" symbol. */
4895 {
4896 static const char str[] = Z_SYM_NAME;
4897 size_t len = strlen (str);
4898 std::string lookup;
4899
4900 for (size_t i = 1; i < len; i++)
4901 {
4902 lookup.assign (str, i);
4903 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4904 EXPECT (Z_SYM_NAME));
4905 }
4906 }
4907
4908 /* In FULL mode, an incomplete symbol doesn't match. */
4909 {
4910 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4911 {});
4912 }
4913
4914 /* A complete symbol with parameters matches any overload, since the
4915 index has no overload info. */
4916 {
4917 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4918 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4919 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4920 EXPECT ("std::zfunction", "std::zfunction2"));
4921 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4922 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4923 }
4924
4925 /* Check that whitespace is ignored appropriately. A symbol with a
4926 template argument list. */
4927 {
4928 static const char expected[] = "ns::foo<int>";
4929 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4930 EXPECT (expected));
a20714ff
PA
4931 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4932 EXPECT (expected));
c62446b1
PA
4933 }
4934
4935 /* Check that whitespace is ignored appropriately. A symbol with a
4936 template argument list that includes a pointer. */
4937 {
4938 static const char expected[] = "ns::foo<char*>";
4939 /* Try both completion and non-completion modes. */
4940 static const bool completion_mode[2] = {false, true};
4941 for (size_t i = 0; i < 2; i++)
4942 {
4943 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4944 completion_mode[i], EXPECT (expected));
a20714ff
PA
4945 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4946 completion_mode[i], EXPECT (expected));
c62446b1
PA
4947
4948 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4949 completion_mode[i], EXPECT (expected));
a20714ff
PA
4950 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4951 completion_mode[i], EXPECT (expected));
c62446b1
PA
4952 }
4953 }
4954
4955 {
4956 /* Check method qualifiers are ignored. */
4957 static const char expected[] = "ns::foo<char*>";
4958 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4959 symbol_name_match_type::FULL, true, EXPECT (expected));
4960 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4961 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4962 CHECK_MATCH ("foo < char * > ( int ) const",
4963 symbol_name_match_type::WILD, true, EXPECT (expected));
4964 CHECK_MATCH ("foo < char * > ( int ) &&",
4965 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4966 }
4967
4968 /* Test lookup names that don't match anything. */
4969 {
a20714ff
PA
4970 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4971 {});
4972
c62446b1
PA
4973 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4974 {});
4975 }
4976
a20714ff
PA
4977 /* Some wild matching tests, exercising "(anonymous namespace)",
4978 which should not be confused with a parameter list. */
4979 {
4980 static const char *syms[] = {
4981 "A::B::C",
4982 "B::C",
4983 "C",
4984 "A :: B :: C ( int )",
4985 "B :: C ( int )",
4986 "C ( int )",
4987 };
4988
4989 for (const char *s : syms)
4990 {
4991 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4992 EXPECT ("(anonymous namespace)::A::B::C"));
4993 }
4994 }
4995
4996 {
4997 static const char expected[] = "ns2::tmpl<int>::foo2";
4998 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4999 EXPECT (expected));
5000 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5001 EXPECT (expected));
5002 }
5003
c62446b1
PA
5004 SELF_CHECK (!any_mismatch);
5005
5006#undef EXPECT
5007#undef CHECK_MATCH
5008}
5009
5c58de74
PA
5010static void
5011run_test ()
5012{
5013 test_mapped_index_find_name_component_bounds ();
5014 test_dw2_expand_symtabs_matching_symbol ();
5015}
5016
c62446b1
PA
5017}} // namespace selftests::dw2_expand_symtabs_matching
5018
5019#endif /* GDB_SELF_TEST */
5020
4b514bc8
JK
5021/* If FILE_MATCHER is NULL or if PER_CU has
5022 dwarf2_per_cu_quick_data::MARK set (see
5023 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5024 EXPANSION_NOTIFY on it. */
5025
5026static void
5027dw2_expand_symtabs_matching_one
5028 (struct dwarf2_per_cu_data *per_cu,
5029 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5030 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5031{
5032 if (file_matcher == NULL || per_cu->v.quick->mark)
5033 {
5034 bool symtab_was_null
5035 = (per_cu->v.quick->compunit_symtab == NULL);
5036
58f0c718 5037 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5038
5039 if (expansion_notify != NULL
5040 && symtab_was_null
5041 && per_cu->v.quick->compunit_symtab != NULL)
5042 expansion_notify (per_cu->v.quick->compunit_symtab);
5043 }
5044}
5045
3f563c84
PA
5046/* Helper for dw2_expand_matching symtabs. Called on each symbol
5047 matched, to expand corresponding CUs that were marked. IDX is the
5048 index of the symbol name that matched. */
5049
5050static void
5051dw2_expand_marked_cus
ed2dc618 5052 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5053 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5054 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5055 search_domain kind)
5056{
3f563c84
PA
5057 offset_type *vec, vec_len, vec_idx;
5058 bool global_seen = false;
ed2dc618 5059 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5060
61920122 5061 vec = (offset_type *) (index.constant_pool
f00a2de2 5062 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5063 vec_len = MAYBE_SWAP (vec[0]);
5064 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5065 {
61920122
PA
5066 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5067 /* This value is only valid for index versions >= 7. */
5068 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5069 gdb_index_symbol_kind symbol_kind =
5070 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5071 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5072 /* Only check the symbol attributes if they're present.
5073 Indices prior to version 7 don't record them,
5074 and indices >= 7 may elide them for certain symbols
5075 (gold does this). */
5076 int attrs_valid =
5077 (index.version >= 7
5078 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5079
5080 /* Work around gold/15646. */
5081 if (attrs_valid)
9291a0cd 5082 {
61920122
PA
5083 if (!is_static && global_seen)
5084 continue;
5085 if (!is_static)
5086 global_seen = true;
5087 }
3190f0c6 5088
61920122
PA
5089 /* Only check the symbol's kind if it has one. */
5090 if (attrs_valid)
5091 {
5092 switch (kind)
8943b874 5093 {
61920122
PA
5094 case VARIABLES_DOMAIN:
5095 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5096 continue;
5097 break;
5098 case FUNCTIONS_DOMAIN:
5099 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5100 continue;
61920122
PA
5101 break;
5102 case TYPES_DOMAIN:
5103 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5104 continue;
5105 break;
59c35742
AB
5106 case MODULES_DOMAIN:
5107 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5108 continue;
5109 break;
61920122
PA
5110 default:
5111 break;
8943b874 5112 }
61920122 5113 }
8943b874 5114
61920122 5115 /* Don't crash on bad data. */
b76e467d 5116 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5117 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5118 {
b98664d3 5119 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5120 " [in module %s]"),
5121 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5122 continue;
5123 }
5124
ff4c9fec 5125 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5126 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5127 expansion_notify);
61920122
PA
5128 }
5129}
5130
4b514bc8
JK
5131/* If FILE_MATCHER is non-NULL, set all the
5132 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5133 that match FILE_MATCHER. */
5134
61920122 5135static void
4b514bc8 5136dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5137 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5138 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5139{
4b514bc8 5140 if (file_matcher == NULL)
61920122
PA
5141 return;
5142
4b514bc8
JK
5143 objfile *const objfile = dwarf2_per_objfile->objfile;
5144
5145 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5146 htab_eq_pointer,
5147 NULL, xcalloc, xfree));
5148 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5149 htab_eq_pointer,
5150 NULL, xcalloc, xfree));
61920122 5151
4b514bc8
JK
5152 /* The rule is CUs specify all the files, including those used by
5153 any TU, so there's no need to scan TUs here. */
61920122 5154
b76e467d 5155 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5156 {
927aa2e7
JK
5157 QUIT;
5158
5159 per_cu->v.quick->mark = 0;
5160
5161 /* We only need to look at symtabs not already expanded. */
5162 if (per_cu->v.quick->compunit_symtab)
5163 continue;
5164
b76e467d 5165 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5166 if (file_data == NULL)
5167 continue;
5168
5169 if (htab_find (visited_not_found.get (), file_data) != NULL)
5170 continue;
5171 else if (htab_find (visited_found.get (), file_data) != NULL)
5172 {
5173 per_cu->v.quick->mark = 1;
5174 continue;
5175 }
5176
b76e467d 5177 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5178 {
5179 const char *this_real_name;
5180
5181 if (file_matcher (file_data->file_names[j], false))
5182 {
5183 per_cu->v.quick->mark = 1;
5184 break;
5185 }
5186
5187 /* Before we invoke realpath, which can get expensive when many
5188 files are involved, do a quick comparison of the basenames. */
5189 if (!basenames_may_differ
5190 && !file_matcher (lbasename (file_data->file_names[j]),
5191 true))
5192 continue;
5193
5194 this_real_name = dw2_get_real_path (objfile, file_data, j);
5195 if (file_matcher (this_real_name, false))
5196 {
5197 per_cu->v.quick->mark = 1;
5198 break;
5199 }
5200 }
5201
b76e467d
SM
5202 void **slot = htab_find_slot (per_cu->v.quick->mark
5203 ? visited_found.get ()
5204 : visited_not_found.get (),
5205 file_data, INSERT);
927aa2e7
JK
5206 *slot = file_data;
5207 }
5208}
5209
5210static void
5211dw2_expand_symtabs_matching
5212 (struct objfile *objfile,
5213 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5214 const lookup_name_info &lookup_name,
5215 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5216 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5217 enum search_domain kind)
5218{
ed2dc618
SM
5219 struct dwarf2_per_objfile *dwarf2_per_objfile
5220 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5221
5222 /* index_table is NULL if OBJF_READNOW. */
5223 if (!dwarf2_per_objfile->index_table)
5224 return;
5225
ed2dc618 5226 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5227
5228 mapped_index &index = *dwarf2_per_objfile->index_table;
5229
5230 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5231 symbol_matcher,
5232 kind, [&] (offset_type idx)
5233 {
ed2dc618 5234 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5235 expansion_notify, kind);
3b00ef10 5236 return true;
927aa2e7
JK
5237 });
5238}
5239
5240/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5241 symtab. */
5242
5243static struct compunit_symtab *
5244recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5245 CORE_ADDR pc)
5246{
5247 int i;
5248
5249 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5250 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5251 return cust;
5252
5253 if (cust->includes == NULL)
5254 return NULL;
5255
5256 for (i = 0; cust->includes[i]; ++i)
5257 {
5258 struct compunit_symtab *s = cust->includes[i];
5259
5260 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5261 if (s != NULL)
5262 return s;
5263 }
5264
5265 return NULL;
5266}
5267
5268static struct compunit_symtab *
5269dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5270 struct bound_minimal_symbol msymbol,
5271 CORE_ADDR pc,
5272 struct obj_section *section,
5273 int warn_if_readin)
5274{
5275 struct dwarf2_per_cu_data *data;
5276 struct compunit_symtab *result;
5277
d320c2b5 5278 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5279 return NULL;
5280
b3b3bada 5281 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
5282 data = (struct dwarf2_per_cu_data *) addrmap_find
5283 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5284 if (!data)
5285 return NULL;
5286
5287 if (warn_if_readin && data->v.quick->compunit_symtab)
5288 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5289 paddress (get_objfile_arch (objfile), pc));
5290
5291 result
58f0c718
TT
5292 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5293 false),
927aa2e7
JK
5294 pc);
5295 gdb_assert (result != NULL);
5296 return result;
5297}
5298
5299static void
5300dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5301 void *data, int need_fullname)
5302{
ed2dc618
SM
5303 struct dwarf2_per_objfile *dwarf2_per_objfile
5304 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5305
5306 if (!dwarf2_per_objfile->filenames_cache)
5307 {
5308 dwarf2_per_objfile->filenames_cache.emplace ();
5309
5310 htab_up visited (htab_create_alloc (10,
5311 htab_hash_pointer, htab_eq_pointer,
5312 NULL, xcalloc, xfree));
5313
5314 /* The rule is CUs specify all the files, including those used
5315 by any TU, so there's no need to scan TUs here. We can
5316 ignore file names coming from already-expanded CUs. */
5317
b76e467d 5318 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5319 {
927aa2e7
JK
5320 if (per_cu->v.quick->compunit_symtab)
5321 {
5322 void **slot = htab_find_slot (visited.get (),
5323 per_cu->v.quick->file_names,
5324 INSERT);
5325
5326 *slot = per_cu->v.quick->file_names;
5327 }
5328 }
5329
b76e467d 5330 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5331 {
927aa2e7
JK
5332 /* We only need to look at symtabs not already expanded. */
5333 if (per_cu->v.quick->compunit_symtab)
5334 continue;
5335
b76e467d 5336 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5337 if (file_data == NULL)
5338 continue;
5339
b76e467d 5340 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5341 if (*slot)
5342 {
5343 /* Already visited. */
5344 continue;
5345 }
5346 *slot = file_data;
5347
5348 for (int j = 0; j < file_data->num_file_names; ++j)
5349 {
5350 const char *filename = file_data->file_names[j];
5351 dwarf2_per_objfile->filenames_cache->seen (filename);
5352 }
5353 }
5354 }
5355
5356 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5357 {
5358 gdb::unique_xmalloc_ptr<char> this_real_name;
5359
5360 if (need_fullname)
5361 this_real_name = gdb_realpath (filename);
5362 (*fun) (filename, this_real_name.get (), data);
5363 });
5364}
5365
5366static int
5367dw2_has_symbols (struct objfile *objfile)
5368{
5369 return 1;
5370}
5371
5372const struct quick_symbol_functions dwarf2_gdb_index_functions =
5373{
5374 dw2_has_symbols,
5375 dw2_find_last_source_symtab,
5376 dw2_forget_cached_source_info,
5377 dw2_map_symtabs_matching_filename,
5378 dw2_lookup_symbol,
5379 dw2_print_stats,
5380 dw2_dump,
927aa2e7
JK
5381 dw2_expand_symtabs_for_function,
5382 dw2_expand_all_symtabs,
5383 dw2_expand_symtabs_with_fullname,
5384 dw2_map_matching_symbols,
5385 dw2_expand_symtabs_matching,
5386 dw2_find_pc_sect_compunit_symtab,
5387 NULL,
5388 dw2_map_symbol_filenames
5389};
5390
5391/* DWARF-5 debug_names reader. */
5392
5393/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5394static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5395
5396/* A helper function that reads the .debug_names section in SECTION
5397 and fills in MAP. FILENAME is the name of the file containing the
5398 section; it is used for error reporting.
5399
5400 Returns true if all went well, false otherwise. */
5401
5402static bool
5403read_debug_names_from_section (struct objfile *objfile,
5404 const char *filename,
5405 struct dwarf2_section_info *section,
5406 mapped_debug_names &map)
5407{
5408 if (dwarf2_section_empty_p (section))
5409 return false;
5410
5411 /* Older elfutils strip versions could keep the section in the main
5412 executable while splitting it for the separate debug info file. */
5413 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5414 return false;
5415
5416 dwarf2_read_section (objfile, section);
5417
5418 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5419
5420 const gdb_byte *addr = section->buffer;
5421
5422 bfd *const abfd = get_section_bfd_owner (section);
5423
5424 unsigned int bytes_read;
5425 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5426 addr += bytes_read;
5427
5428 map.dwarf5_is_dwarf64 = bytes_read != 4;
5429 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5430 if (bytes_read + length != section->size)
5431 {
5432 /* There may be multiple per-CU indices. */
5433 warning (_("Section .debug_names in %s length %s does not match "
5434 "section length %s, ignoring .debug_names."),
5435 filename, plongest (bytes_read + length),
5436 pulongest (section->size));
5437 return false;
5438 }
5439
5440 /* The version number. */
5441 uint16_t version = read_2_bytes (abfd, addr);
5442 addr += 2;
5443 if (version != 5)
5444 {
5445 warning (_("Section .debug_names in %s has unsupported version %d, "
5446 "ignoring .debug_names."),
5447 filename, version);
5448 return false;
5449 }
5450
5451 /* Padding. */
5452 uint16_t padding = read_2_bytes (abfd, addr);
5453 addr += 2;
5454 if (padding != 0)
5455 {
5456 warning (_("Section .debug_names in %s has unsupported padding %d, "
5457 "ignoring .debug_names."),
5458 filename, padding);
5459 return false;
5460 }
5461
5462 /* comp_unit_count - The number of CUs in the CU list. */
5463 map.cu_count = read_4_bytes (abfd, addr);
5464 addr += 4;
5465
5466 /* local_type_unit_count - The number of TUs in the local TU
5467 list. */
5468 map.tu_count = read_4_bytes (abfd, addr);
5469 addr += 4;
5470
5471 /* foreign_type_unit_count - The number of TUs in the foreign TU
5472 list. */
5473 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5474 addr += 4;
5475 if (foreign_tu_count != 0)
5476 {
5477 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5478 "ignoring .debug_names."),
5479 filename, static_cast<unsigned long> (foreign_tu_count));
5480 return false;
5481 }
5482
5483 /* bucket_count - The number of hash buckets in the hash lookup
5484 table. */
5485 map.bucket_count = read_4_bytes (abfd, addr);
5486 addr += 4;
5487
5488 /* name_count - The number of unique names in the index. */
5489 map.name_count = read_4_bytes (abfd, addr);
5490 addr += 4;
5491
5492 /* abbrev_table_size - The size in bytes of the abbreviations
5493 table. */
5494 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5495 addr += 4;
5496
5497 /* augmentation_string_size - The size in bytes of the augmentation
5498 string. This value is rounded up to a multiple of 4. */
5499 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5500 addr += 4;
5501 map.augmentation_is_gdb = ((augmentation_string_size
5502 == sizeof (dwarf5_augmentation))
5503 && memcmp (addr, dwarf5_augmentation,
5504 sizeof (dwarf5_augmentation)) == 0);
5505 augmentation_string_size += (-augmentation_string_size) & 3;
5506 addr += augmentation_string_size;
5507
5508 /* List of CUs */
5509 map.cu_table_reordered = addr;
5510 addr += map.cu_count * map.offset_size;
5511
5512 /* List of Local TUs */
5513 map.tu_table_reordered = addr;
5514 addr += map.tu_count * map.offset_size;
5515
5516 /* Hash Lookup Table */
5517 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5518 addr += map.bucket_count * 4;
5519 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5520 addr += map.name_count * 4;
5521
5522 /* Name Table */
5523 map.name_table_string_offs_reordered = addr;
5524 addr += map.name_count * map.offset_size;
5525 map.name_table_entry_offs_reordered = addr;
5526 addr += map.name_count * map.offset_size;
5527
5528 const gdb_byte *abbrev_table_start = addr;
5529 for (;;)
5530 {
927aa2e7
JK
5531 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5532 addr += bytes_read;
5533 if (index_num == 0)
5534 break;
5535
5536 const auto insertpair
5537 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5538 if (!insertpair.second)
5539 {
5540 warning (_("Section .debug_names in %s has duplicate index %s, "
5541 "ignoring .debug_names."),
5542 filename, pulongest (index_num));
5543 return false;
5544 }
5545 mapped_debug_names::index_val &indexval = insertpair.first->second;
5546 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5547 addr += bytes_read;
5548
5549 for (;;)
5550 {
5551 mapped_debug_names::index_val::attr attr;
5552 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5553 addr += bytes_read;
5554 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5555 addr += bytes_read;
5556 if (attr.form == DW_FORM_implicit_const)
5557 {
5558 attr.implicit_const = read_signed_leb128 (abfd, addr,
5559 &bytes_read);
5560 addr += bytes_read;
5561 }
5562 if (attr.dw_idx == 0 && attr.form == 0)
5563 break;
5564 indexval.attr_vec.push_back (std::move (attr));
5565 }
5566 }
5567 if (addr != abbrev_table_start + abbrev_table_size)
5568 {
5569 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5570 "of size %s vs. written as %u, ignoring .debug_names."),
5571 filename, plongest (addr - abbrev_table_start),
5572 abbrev_table_size);
927aa2e7
JK
5573 return false;
5574 }
5575 map.entry_pool = addr;
5576
5577 return true;
5578}
5579
5580/* A helper for create_cus_from_debug_names that handles the MAP's CU
5581 list. */
5582
5583static void
ed2dc618 5584create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5585 const mapped_debug_names &map,
5586 dwarf2_section_info &section,
b76e467d 5587 bool is_dwz)
927aa2e7
JK
5588{
5589 sect_offset sect_off_prev;
5590 for (uint32_t i = 0; i <= map.cu_count; ++i)
5591 {
5592 sect_offset sect_off_next;
5593 if (i < map.cu_count)
5594 {
5595 sect_off_next
5596 = (sect_offset) (extract_unsigned_integer
5597 (map.cu_table_reordered + i * map.offset_size,
5598 map.offset_size,
5599 map.dwarf5_byte_order));
5600 }
5601 else
5602 sect_off_next = (sect_offset) section.size;
5603 if (i >= 1)
5604 {
5605 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5606 dwarf2_per_cu_data *per_cu
ed2dc618 5607 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5608 sect_off_prev, length);
b76e467d 5609 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5610 }
5611 sect_off_prev = sect_off_next;
5612 }
5613}
5614
5615/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5616 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5617
5618static void
ed2dc618 5619create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5620 const mapped_debug_names &map,
5621 const mapped_debug_names &dwz_map)
5622{
b76e467d
SM
5623 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5624 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5625
ed2dc618
SM
5626 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5627 dwarf2_per_objfile->info,
b76e467d 5628 false /* is_dwz */);
927aa2e7
JK
5629
5630 if (dwz_map.cu_count == 0)
5631 return;
5632
ed2dc618
SM
5633 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5634 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5635 true /* is_dwz */);
927aa2e7
JK
5636}
5637
5638/* Read .debug_names. If everything went ok, initialize the "quick"
5639 elements of all the CUs and return true. Otherwise, return false. */
5640
5641static bool
ed2dc618 5642dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5643{
22ca247e
TT
5644 std::unique_ptr<mapped_debug_names> map
5645 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5646 mapped_debug_names dwz_map (dwarf2_per_objfile);
5647 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5648
5649 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5650 &dwarf2_per_objfile->debug_names,
22ca247e 5651 *map))
927aa2e7
JK
5652 return false;
5653
5654 /* Don't use the index if it's empty. */
22ca247e 5655 if (map->name_count == 0)
927aa2e7
JK
5656 return false;
5657
5658 /* If there is a .dwz file, read it so we can get its CU list as
5659 well. */
ed2dc618 5660 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5661 if (dwz != NULL)
5662 {
5663 if (!read_debug_names_from_section (objfile,
00f93c44 5664 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5665 &dwz->debug_names, dwz_map))
5666 {
5667 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5668 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5669 return false;
5670 }
5671 }
5672
22ca247e 5673 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5674
22ca247e 5675 if (map->tu_count != 0)
927aa2e7
JK
5676 {
5677 /* We can only handle a single .debug_types when we have an
5678 index. */
fd5866f6 5679 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5680 return false;
5681
fd5866f6 5682 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5683
5684 create_signatured_type_table_from_debug_names
22ca247e 5685 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5686 }
5687
ed2dc618
SM
5688 create_addrmap_from_aranges (dwarf2_per_objfile,
5689 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5690
22ca247e 5691 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5692 dwarf2_per_objfile->using_index = 1;
5693 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5694 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5695
5696 return true;
5697}
5698
927aa2e7
JK
5699/* Type used to manage iterating over all CUs looking for a symbol for
5700 .debug_names. */
5701
5702class dw2_debug_names_iterator
5703{
5704public:
927aa2e7 5705 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5706 gdb::optional<block_enum> block_index,
5707 domain_enum domain,
927aa2e7 5708 const char *name)
2b79f376 5709 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5710 m_addr (find_vec_in_debug_names (map, name))
5711 {}
5712
5713 dw2_debug_names_iterator (const mapped_debug_names &map,
5714 search_domain search, uint32_t namei)
5715 : m_map (map),
5716 m_search (search),
5717 m_addr (find_vec_in_debug_names (map, namei))
5718 {}
5719
3b00ef10
TT
5720 dw2_debug_names_iterator (const mapped_debug_names &map,
5721 block_enum block_index, domain_enum domain,
5722 uint32_t namei)
5723 : m_map (map), m_block_index (block_index), m_domain (domain),
5724 m_addr (find_vec_in_debug_names (map, namei))
5725 {}
5726
927aa2e7
JK
5727 /* Return the next matching CU or NULL if there are no more. */
5728 dwarf2_per_cu_data *next ();
5729
5730private:
5731 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5732 const char *name);
5733 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5734 uint32_t namei);
5735
5736 /* The internalized form of .debug_names. */
5737 const mapped_debug_names &m_map;
5738
2b79f376
SM
5739 /* If set, only look for symbols that match that block. Valid values are
5740 GLOBAL_BLOCK and STATIC_BLOCK. */
5741 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5742
5743 /* The kind of symbol we're looking for. */
5744 const domain_enum m_domain = UNDEF_DOMAIN;
5745 const search_domain m_search = ALL_DOMAIN;
5746
5747 /* The list of CUs from the index entry of the symbol, or NULL if
5748 not found. */
5749 const gdb_byte *m_addr;
5750};
5751
5752const char *
5753mapped_debug_names::namei_to_name (uint32_t namei) const
5754{
5755 const ULONGEST namei_string_offs
5756 = extract_unsigned_integer ((name_table_string_offs_reordered
5757 + namei * offset_size),
5758 offset_size,
5759 dwarf5_byte_order);
5760 return read_indirect_string_at_offset
ed2dc618 5761 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5762}
5763
5764/* Find a slot in .debug_names for the object named NAME. If NAME is
5765 found, return pointer to its pool data. If NAME cannot be found,
5766 return NULL. */
5767
5768const gdb_byte *
5769dw2_debug_names_iterator::find_vec_in_debug_names
5770 (const mapped_debug_names &map, const char *name)
5771{
5772 int (*cmp) (const char *, const char *);
5773
54ee4252 5774 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5775 if (current_language->la_language == language_cplus
5776 || current_language->la_language == language_fortran
5777 || current_language->la_language == language_d)
5778 {
5779 /* NAME is already canonical. Drop any qualifiers as
5780 .debug_names does not contain any. */
5781
5782 if (strchr (name, '(') != NULL)
5783 {
54ee4252 5784 without_params = cp_remove_params (name);
927aa2e7 5785 if (without_params != NULL)
54ee4252 5786 name = without_params.get ();
927aa2e7
JK
5787 }
5788 }
5789
5790 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5791
5792 const uint32_t full_hash = dwarf5_djb_hash (name);
5793 uint32_t namei
5794 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5795 (map.bucket_table_reordered
5796 + (full_hash % map.bucket_count)), 4,
5797 map.dwarf5_byte_order);
5798 if (namei == 0)
5799 return NULL;
5800 --namei;
5801 if (namei >= map.name_count)
5802 {
b98664d3 5803 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5804 "[in module %s]"),
5805 namei, map.name_count,
ed2dc618 5806 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5807 return NULL;
5808 }
5809
5810 for (;;)
5811 {
5812 const uint32_t namei_full_hash
5813 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5814 (map.hash_table_reordered + namei), 4,
5815 map.dwarf5_byte_order);
5816 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5817 return NULL;
5818
5819 if (full_hash == namei_full_hash)
5820 {
5821 const char *const namei_string = map.namei_to_name (namei);
5822
5823#if 0 /* An expensive sanity check. */
5824 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5825 {
b98664d3 5826 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5827 "[in module %s]"),
5828 namei, objfile_name (dwarf2_per_objfile->objfile));
5829 return NULL;
5830 }
5831#endif
5832
5833 if (cmp (namei_string, name) == 0)
5834 {
5835 const ULONGEST namei_entry_offs
5836 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5837 + namei * map.offset_size),
5838 map.offset_size, map.dwarf5_byte_order);
5839 return map.entry_pool + namei_entry_offs;
5840 }
5841 }
5842
5843 ++namei;
5844 if (namei >= map.name_count)
5845 return NULL;
5846 }
5847}
5848
5849const gdb_byte *
5850dw2_debug_names_iterator::find_vec_in_debug_names
5851 (const mapped_debug_names &map, uint32_t namei)
5852{
5853 if (namei >= map.name_count)
5854 {
b98664d3 5855 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5856 "[in module %s]"),
5857 namei, map.name_count,
ed2dc618 5858 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5859 return NULL;
5860 }
5861
5862 const ULONGEST namei_entry_offs
5863 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5864 + namei * map.offset_size),
5865 map.offset_size, map.dwarf5_byte_order);
5866 return map.entry_pool + namei_entry_offs;
5867}
5868
5869/* See dw2_debug_names_iterator. */
5870
5871dwarf2_per_cu_data *
5872dw2_debug_names_iterator::next ()
5873{
5874 if (m_addr == NULL)
5875 return NULL;
5876
ed2dc618
SM
5877 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5878 struct objfile *objfile = dwarf2_per_objfile->objfile;
5879 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5880
5881 again:
5882
5883 unsigned int bytes_read;
5884 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5885 m_addr += bytes_read;
5886 if (abbrev == 0)
5887 return NULL;
5888
5889 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5890 if (indexval_it == m_map.abbrev_map.cend ())
5891 {
b98664d3 5892 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5893 "[in module %s]"),
ed2dc618 5894 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5895 return NULL;
5896 }
5897 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5898 enum class symbol_linkage {
5899 unknown,
5900 static_,
5901 extern_,
23c13d42 5902 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5903 dwarf2_per_cu_data *per_cu = NULL;
5904 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5905 {
5906 ULONGEST ull;
5907 switch (attr.form)
5908 {
5909 case DW_FORM_implicit_const:
5910 ull = attr.implicit_const;
5911 break;
5912 case DW_FORM_flag_present:
5913 ull = 1;
5914 break;
5915 case DW_FORM_udata:
5916 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5917 m_addr += bytes_read;
5918 break;
5919 default:
b98664d3 5920 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5921 dwarf_form_name (attr.form),
ed2dc618 5922 objfile_name (objfile));
927aa2e7
JK
5923 return NULL;
5924 }
5925 switch (attr.dw_idx)
5926 {
5927 case DW_IDX_compile_unit:
5928 /* Don't crash on bad data. */
b76e467d 5929 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5930 {
b98664d3 5931 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5932 " [in module %s]"),
5933 pulongest (ull),
5934 objfile_name (dwarf2_per_objfile->objfile));
5935 continue;
5936 }
ff4c9fec 5937 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5938 break;
8af5c486
JK
5939 case DW_IDX_type_unit:
5940 /* Don't crash on bad data. */
b2bdb8cf 5941 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5942 {
b98664d3 5943 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5944 " [in module %s]"),
5945 pulongest (ull),
5946 objfile_name (dwarf2_per_objfile->objfile));
5947 continue;
5948 }
ff4c9fec 5949 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5950 break;
927aa2e7
JK
5951 case DW_IDX_GNU_internal:
5952 if (!m_map.augmentation_is_gdb)
5953 break;
23c13d42 5954 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5955 break;
5956 case DW_IDX_GNU_external:
5957 if (!m_map.augmentation_is_gdb)
5958 break;
23c13d42 5959 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5960 break;
5961 }
5962 }
5963
5964 /* Skip if already read in. */
5965 if (per_cu->v.quick->compunit_symtab)
5966 goto again;
5967
5968 /* Check static vs global. */
23c13d42 5969 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5970 {
2b79f376 5971 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5972 const bool symbol_is_static =
5973 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5974 if (want_static != symbol_is_static)
2b79f376 5975 goto again;
927aa2e7
JK
5976 }
5977
5978 /* Match dw2_symtab_iter_next, symbol_kind
5979 and debug_names::psymbol_tag. */
5980 switch (m_domain)
5981 {
5982 case VAR_DOMAIN:
5983 switch (indexval.dwarf_tag)
5984 {
5985 case DW_TAG_variable:
5986 case DW_TAG_subprogram:
5987 /* Some types are also in VAR_DOMAIN. */
5988 case DW_TAG_typedef:
5989 case DW_TAG_structure_type:
5990 break;
5991 default:
5992 goto again;
5993 }
5994 break;
5995 case STRUCT_DOMAIN:
5996 switch (indexval.dwarf_tag)
5997 {
5998 case DW_TAG_typedef:
5999 case DW_TAG_structure_type:
6000 break;
6001 default:
6002 goto again;
6003 }
6004 break;
6005 case LABEL_DOMAIN:
6006 switch (indexval.dwarf_tag)
6007 {
6008 case 0:
6009 case DW_TAG_variable:
6010 break;
6011 default:
6012 goto again;
6013 }
6014 break;
59c35742
AB
6015 case MODULE_DOMAIN:
6016 switch (indexval.dwarf_tag)
6017 {
6018 case DW_TAG_module:
6019 break;
6020 default:
6021 goto again;
6022 }
6023 break;
927aa2e7
JK
6024 default:
6025 break;
6026 }
6027
6028 /* Match dw2_expand_symtabs_matching, symbol_kind and
6029 debug_names::psymbol_tag. */
6030 switch (m_search)
4b514bc8 6031 {
927aa2e7
JK
6032 case VARIABLES_DOMAIN:
6033 switch (indexval.dwarf_tag)
4b514bc8 6034 {
927aa2e7
JK
6035 case DW_TAG_variable:
6036 break;
6037 default:
6038 goto again;
4b514bc8 6039 }
927aa2e7
JK
6040 break;
6041 case FUNCTIONS_DOMAIN:
6042 switch (indexval.dwarf_tag)
4b514bc8 6043 {
927aa2e7
JK
6044 case DW_TAG_subprogram:
6045 break;
6046 default:
6047 goto again;
4b514bc8 6048 }
927aa2e7
JK
6049 break;
6050 case TYPES_DOMAIN:
6051 switch (indexval.dwarf_tag)
6052 {
6053 case DW_TAG_typedef:
6054 case DW_TAG_structure_type:
6055 break;
6056 default:
6057 goto again;
6058 }
6059 break;
59c35742
AB
6060 case MODULES_DOMAIN:
6061 switch (indexval.dwarf_tag)
6062 {
6063 case DW_TAG_module:
6064 break;
6065 default:
6066 goto again;
6067 }
927aa2e7
JK
6068 default:
6069 break;
4b514bc8 6070 }
927aa2e7
JK
6071
6072 return per_cu;
4b514bc8 6073}
61920122 6074
927aa2e7 6075static struct compunit_symtab *
c7f839cb 6076dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6077 const char *name, domain_enum domain)
4b514bc8 6078{
ed2dc618
SM
6079 struct dwarf2_per_objfile *dwarf2_per_objfile
6080 = get_dwarf2_per_objfile (objfile);
61920122 6081
927aa2e7
JK
6082 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6083 if (!mapp)
61920122 6084 {
927aa2e7
JK
6085 /* index is NULL if OBJF_READNOW. */
6086 return NULL;
6087 }
6088 const auto &map = *mapp;
9291a0cd 6089
2b79f376 6090 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6091
927aa2e7
JK
6092 struct compunit_symtab *stab_best = NULL;
6093 struct dwarf2_per_cu_data *per_cu;
6094 while ((per_cu = iter.next ()) != NULL)
6095 {
6096 struct symbol *sym, *with_opaque = NULL;
58f0c718 6097 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6098 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6099 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6100
927aa2e7
JK
6101 sym = block_find_symbol (block, name, domain,
6102 block_find_non_opaque_type_preferred,
6103 &with_opaque);
9703b513 6104
927aa2e7
JK
6105 /* Some caution must be observed with overloaded functions and
6106 methods, since the index will not contain any overload
6107 information (but NAME might contain it). */
a3ec0bb1 6108
927aa2e7 6109 if (sym != NULL
987012b8 6110 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
6111 return stab;
6112 if (with_opaque != NULL
987012b8 6113 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 6114 stab_best = stab;
9703b513 6115
927aa2e7 6116 /* Keep looking through other CUs. */
9703b513
TT
6117 }
6118
927aa2e7 6119 return stab_best;
9703b513
TT
6120}
6121
927aa2e7
JK
6122/* This dumps minimal information about .debug_names. It is called
6123 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6124 uses this to verify that .debug_names has been loaded. */
9291a0cd 6125
927aa2e7
JK
6126static void
6127dw2_debug_names_dump (struct objfile *objfile)
6128{
ed2dc618
SM
6129 struct dwarf2_per_objfile *dwarf2_per_objfile
6130 = get_dwarf2_per_objfile (objfile);
6131
927aa2e7
JK
6132 gdb_assert (dwarf2_per_objfile->using_index);
6133 printf_filtered (".debug_names:");
6134 if (dwarf2_per_objfile->debug_names_table)
6135 printf_filtered (" exists\n");
6136 else
6137 printf_filtered (" faked for \"readnow\"\n");
6138 printf_filtered ("\n");
9291a0cd
TT
6139}
6140
9291a0cd 6141static void
927aa2e7
JK
6142dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6143 const char *func_name)
9291a0cd 6144{
ed2dc618
SM
6145 struct dwarf2_per_objfile *dwarf2_per_objfile
6146 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6147
927aa2e7
JK
6148 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6149 if (dwarf2_per_objfile->debug_names_table)
24c79950 6150 {
927aa2e7 6151 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6152
2b79f376 6153 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6154
927aa2e7
JK
6155 struct dwarf2_per_cu_data *per_cu;
6156 while ((per_cu = iter.next ()) != NULL)
58f0c718 6157 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6158 }
6159}
24c79950 6160
3b00ef10
TT
6161static void
6162dw2_debug_names_map_matching_symbols
6163 (struct objfile *objfile,
6164 const lookup_name_info &name, domain_enum domain,
6165 int global,
6166 gdb::function_view<symbol_found_callback_ftype> callback,
6167 symbol_compare_ftype *ordered_compare)
6168{
6169 struct dwarf2_per_objfile *dwarf2_per_objfile
6170 = get_dwarf2_per_objfile (objfile);
6171
6172 /* debug_names_table is NULL if OBJF_READNOW. */
6173 if (!dwarf2_per_objfile->debug_names_table)
6174 return;
6175
6176 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6177 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6178
6179 const char *match_name = name.ada ().lookup_name ().c_str ();
6180 auto matcher = [&] (const char *symname)
6181 {
6182 if (ordered_compare == nullptr)
6183 return true;
6184 return ordered_compare (symname, match_name) == 0;
6185 };
6186
6187 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6188 [&] (offset_type namei)
6189 {
6190 /* The name was matched, now expand corresponding CUs that were
6191 marked. */
6192 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6193
6194 struct dwarf2_per_cu_data *per_cu;
6195 while ((per_cu = iter.next ()) != NULL)
6196 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6197 return true;
6198 });
6199
6200 /* It's a shame we couldn't do this inside the
6201 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6202 that have already been expanded. Instead, this loop matches what
6203 the psymtab code does. */
6204 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6205 {
6206 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6207 if (cust != nullptr)
6208 {
6209 const struct block *block
6210 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6211 if (!iterate_over_symbols_terminated (block, name,
6212 domain, callback))
6213 break;
6214 }
6215 }
6216}
6217
927aa2e7
JK
6218static void
6219dw2_debug_names_expand_symtabs_matching
6220 (struct objfile *objfile,
6221 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6222 const lookup_name_info &lookup_name,
6223 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6224 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6225 enum search_domain kind)
6226{
ed2dc618
SM
6227 struct dwarf2_per_objfile *dwarf2_per_objfile
6228 = get_dwarf2_per_objfile (objfile);
9291a0cd 6229
927aa2e7
JK
6230 /* debug_names_table is NULL if OBJF_READNOW. */
6231 if (!dwarf2_per_objfile->debug_names_table)
6232 return;
9291a0cd 6233
ed2dc618 6234 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6235
44ed8f3e 6236 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6237
44ed8f3e
PA
6238 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6239 symbol_matcher,
6240 kind, [&] (offset_type namei)
927aa2e7 6241 {
927aa2e7
JK
6242 /* The name was matched, now expand corresponding CUs that were
6243 marked. */
6244 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6245
927aa2e7
JK
6246 struct dwarf2_per_cu_data *per_cu;
6247 while ((per_cu = iter.next ()) != NULL)
6248 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6249 expansion_notify);
3b00ef10 6250 return true;
44ed8f3e 6251 });
9291a0cd
TT
6252}
6253
927aa2e7 6254const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6255{
6256 dw2_has_symbols,
6257 dw2_find_last_source_symtab,
6258 dw2_forget_cached_source_info,
f8eba3c6 6259 dw2_map_symtabs_matching_filename,
927aa2e7 6260 dw2_debug_names_lookup_symbol,
9291a0cd 6261 dw2_print_stats,
927aa2e7 6262 dw2_debug_names_dump,
927aa2e7 6263 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6264 dw2_expand_all_symtabs,
652a8996 6265 dw2_expand_symtabs_with_fullname,
3b00ef10 6266 dw2_debug_names_map_matching_symbols,
927aa2e7 6267 dw2_debug_names_expand_symtabs_matching,
43f3e411 6268 dw2_find_pc_sect_compunit_symtab,
71a3c369 6269 NULL,
9291a0cd
TT
6270 dw2_map_symbol_filenames
6271};
6272
4485a1c1
SM
6273/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6274 to either a dwarf2_per_objfile or dwz_file object. */
6275
6276template <typename T>
6277static gdb::array_view<const gdb_byte>
6278get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6279{
6280 dwarf2_section_info *section = &section_owner->gdb_index;
6281
6282 if (dwarf2_section_empty_p (section))
6283 return {};
6284
6285 /* Older elfutils strip versions could keep the section in the main
6286 executable while splitting it for the separate debug info file. */
6287 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6288 return {};
6289
6290 dwarf2_read_section (obj, section);
6291
8bebfcda
PA
6292 /* dwarf2_section_info::size is a bfd_size_type, while
6293 gdb::array_view works with size_t. On 32-bit hosts, with
6294 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6295 is 32-bit. So we need an explicit narrowing conversion here.
6296 This is fine, because it's impossible to allocate or mmap an
6297 array/buffer larger than what size_t can represent. */
6298 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6299}
6300
87d6a7aa
SM
6301/* Lookup the index cache for the contents of the index associated to
6302 DWARF2_OBJ. */
6303
6304static gdb::array_view<const gdb_byte>
6305get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6306{
6307 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6308 if (build_id == nullptr)
6309 return {};
6310
6311 return global_index_cache.lookup_gdb_index (build_id,
6312 &dwarf2_obj->index_cache_res);
6313}
6314
6315/* Same as the above, but for DWZ. */
6316
6317static gdb::array_view<const gdb_byte>
6318get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6319{
6320 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6321 if (build_id == nullptr)
6322 return {};
6323
6324 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6325}
6326
3c0aa29a 6327/* See symfile.h. */
9291a0cd 6328
3c0aa29a
PA
6329bool
6330dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6331{
ed2dc618
SM
6332 struct dwarf2_per_objfile *dwarf2_per_objfile
6333 = get_dwarf2_per_objfile (objfile);
6334
9291a0cd
TT
6335 /* If we're about to read full symbols, don't bother with the
6336 indices. In this case we also don't care if some other debug
6337 format is making psymtabs, because they are all about to be
6338 expanded anyway. */
6339 if ((objfile->flags & OBJF_READNOW))
6340 {
9291a0cd 6341 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6342 create_all_comp_units (dwarf2_per_objfile);
6343 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6344 dwarf2_per_objfile->quick_file_names_table
6345 = create_quick_file_names_table
6346 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6347
b76e467d 6348 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6349 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6350 {
ff4c9fec 6351 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6352
e254ef6a
DE
6353 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6354 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6355 }
6356
6357 /* Return 1 so that gdb sees the "quick" functions. However,
6358 these functions will be no-ops because we will have expanded
6359 all symtabs. */
3c0aa29a
PA
6360 *index_kind = dw_index_kind::GDB_INDEX;
6361 return true;
9291a0cd
TT
6362 }
6363
ed2dc618 6364 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6365 {
6366 *index_kind = dw_index_kind::DEBUG_NAMES;
6367 return true;
6368 }
927aa2e7 6369
4485a1c1
SM
6370 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6371 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6372 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6373 {
6374 *index_kind = dw_index_kind::GDB_INDEX;
6375 return true;
6376 }
9291a0cd 6377
87d6a7aa
SM
6378 /* ... otherwise, try to find the index in the index cache. */
6379 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6380 get_gdb_index_contents_from_cache,
6381 get_gdb_index_contents_from_cache_dwz))
6382 {
6383 global_index_cache.hit ();
6384 *index_kind = dw_index_kind::GDB_INDEX;
6385 return true;
6386 }
6387
6388 global_index_cache.miss ();
3c0aa29a 6389 return false;
9291a0cd
TT
6390}
6391
6392\f
6393
dce234bc
PP
6394/* Build a partial symbol table. */
6395
6396void
f29dff0a 6397dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6398{
ed2dc618
SM
6399 struct dwarf2_per_objfile *dwarf2_per_objfile
6400 = get_dwarf2_per_objfile (objfile);
c9bf0622 6401
6eee24ce 6402 init_psymbol_list (objfile, 1024);
c906108c 6403
a70b8144 6404 try
c9bf0622
TT
6405 {
6406 /* This isn't really ideal: all the data we allocate on the
6407 objfile's obstack is still uselessly kept around. However,
6408 freeing it seems unsafe. */
906768f9 6409 psymtab_discarder psymtabs (objfile);
ed2dc618 6410 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6411 psymtabs.keep ();
87d6a7aa
SM
6412
6413 /* (maybe) store an index in the cache. */
6414 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6415 }
230d2906 6416 catch (const gdb_exception_error &except)
492d29ea
PA
6417 {
6418 exception_print (gdb_stderr, except);
6419 }
c906108c 6420}
c906108c 6421
1ce1cefd
DE
6422/* Return the total length of the CU described by HEADER. */
6423
6424static unsigned int
6425get_cu_length (const struct comp_unit_head *header)
6426{
6427 return header->initial_length_size + header->length;
6428}
6429
9c541725 6430/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6431
9c541725
PA
6432static inline bool
6433offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6434{
9c541725
PA
6435 sect_offset bottom = cu_header->sect_off;
6436 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6437
9c541725 6438 return sect_off >= bottom && sect_off < top;
45452591
DE
6439}
6440
3b80fe9b
DE
6441/* Find the base address of the compilation unit for range lists and
6442 location lists. It will normally be specified by DW_AT_low_pc.
6443 In DWARF-3 draft 4, the base address could be overridden by
6444 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6445 compilation units with discontinuous ranges. */
6446
6447static void
6448dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6449{
6450 struct attribute *attr;
6451
6452 cu->base_known = 0;
6453 cu->base_address = 0;
6454
6455 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6456 if (attr != nullptr)
3b80fe9b 6457 {
31aa7e4e 6458 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6459 cu->base_known = 1;
6460 }
6461 else
6462 {
6463 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6464 if (attr != nullptr)
3b80fe9b 6465 {
31aa7e4e 6466 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6467 cu->base_known = 1;
6468 }
6469 }
6470}
6471
93311388 6472/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6473 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6474 NOTE: This leaves members offset, first_die_offset to be filled in
6475 by the caller. */
107d2387 6476
d521ce57 6477static const gdb_byte *
107d2387 6478read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6479 const gdb_byte *info_ptr,
6480 struct dwarf2_section_info *section,
6481 rcuh_kind section_kind)
107d2387
AC
6482{
6483 int signed_addr;
891d2f0b 6484 unsigned int bytes_read;
43988095
JK
6485 const char *filename = get_section_file_name (section);
6486 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6487
6488 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6489 cu_header->initial_length_size = bytes_read;
6490 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6491 info_ptr += bytes_read;
107d2387 6492 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6493 if (cu_header->version < 2 || cu_header->version > 5)
6494 error (_("Dwarf Error: wrong version in compilation unit header "
6495 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6496 cu_header->version, filename);
107d2387 6497 info_ptr += 2;
43988095
JK
6498 if (cu_header->version < 5)
6499 switch (section_kind)
6500 {
6501 case rcuh_kind::COMPILE:
6502 cu_header->unit_type = DW_UT_compile;
6503 break;
6504 case rcuh_kind::TYPE:
6505 cu_header->unit_type = DW_UT_type;
6506 break;
6507 default:
6508 internal_error (__FILE__, __LINE__,
6509 _("read_comp_unit_head: invalid section_kind"));
6510 }
6511 else
6512 {
6513 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6514 (read_1_byte (abfd, info_ptr));
6515 info_ptr += 1;
6516 switch (cu_header->unit_type)
6517 {
6518 case DW_UT_compile:
a084a2a6
AT
6519 case DW_UT_partial:
6520 case DW_UT_skeleton:
6521 case DW_UT_split_compile:
43988095
JK
6522 if (section_kind != rcuh_kind::COMPILE)
6523 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6524 "(is %s, should be %s) [in module %s]"),
6525 dwarf_unit_type_name (cu_header->unit_type),
6526 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6527 break;
6528 case DW_UT_type:
a084a2a6 6529 case DW_UT_split_type:
43988095
JK
6530 section_kind = rcuh_kind::TYPE;
6531 break;
6532 default:
6533 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6534 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6535 "[in module %s]"), cu_header->unit_type,
6536 dwarf_unit_type_name (DW_UT_compile),
6537 dwarf_unit_type_name (DW_UT_skeleton),
6538 dwarf_unit_type_name (DW_UT_split_compile),
6539 dwarf_unit_type_name (DW_UT_type),
6540 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6541 }
6542
6543 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6544 info_ptr += 1;
6545 }
9c541725
PA
6546 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6547 cu_header,
6548 &bytes_read);
613e1657 6549 info_ptr += bytes_read;
43988095
JK
6550 if (cu_header->version < 5)
6551 {
6552 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6553 info_ptr += 1;
6554 }
107d2387
AC
6555 signed_addr = bfd_get_sign_extend_vma (abfd);
6556 if (signed_addr < 0)
8e65ff28 6557 internal_error (__FILE__, __LINE__,
e2e0b3e5 6558 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6559 cu_header->signed_addr_p = signed_addr;
c764a876 6560
a084a2a6
AT
6561 bool header_has_signature = section_kind == rcuh_kind::TYPE
6562 || cu_header->unit_type == DW_UT_skeleton
6563 || cu_header->unit_type == DW_UT_split_compile;
43988095 6564
a084a2a6
AT
6565 if (header_has_signature)
6566 {
43988095
JK
6567 cu_header->signature = read_8_bytes (abfd, info_ptr);
6568 info_ptr += 8;
a084a2a6 6569 }
43988095 6570
a084a2a6
AT
6571 if (section_kind == rcuh_kind::TYPE)
6572 {
6573 LONGEST type_offset;
43988095
JK
6574 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6575 info_ptr += bytes_read;
9c541725
PA
6576 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6577 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6578 error (_("Dwarf Error: Too big type_offset in compilation unit "
6579 "header (is %s) [in module %s]"), plongest (type_offset),
6580 filename);
6581 }
6582
107d2387
AC
6583 return info_ptr;
6584}
6585
36586728
TT
6586/* Helper function that returns the proper abbrev section for
6587 THIS_CU. */
6588
6589static struct dwarf2_section_info *
6590get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6591{
6592 struct dwarf2_section_info *abbrev;
ed2dc618 6593 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6594
6595 if (this_cu->is_dwz)
ed2dc618 6596 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6597 else
6598 abbrev = &dwarf2_per_objfile->abbrev;
6599
6600 return abbrev;
6601}
6602
9ff913ba
DE
6603/* Subroutine of read_and_check_comp_unit_head and
6604 read_and_check_type_unit_head to simplify them.
6605 Perform various error checking on the header. */
6606
6607static void
ed2dc618
SM
6608error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6609 struct comp_unit_head *header,
4bdcc0c1
DE
6610 struct dwarf2_section_info *section,
6611 struct dwarf2_section_info *abbrev_section)
9ff913ba 6612{
a32a8923 6613 const char *filename = get_section_file_name (section);
9ff913ba 6614
9c541725 6615 if (to_underlying (header->abbrev_sect_off)
36586728 6616 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6617 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6618 "(offset %s + 6) [in module %s]"),
6619 sect_offset_str (header->abbrev_sect_off),
6620 sect_offset_str (header->sect_off),
9ff913ba
DE
6621 filename);
6622
9c541725 6623 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6624 avoid potential 32-bit overflow. */
9c541725 6625 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6626 > section->size)
9c541725 6627 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6628 "(offset %s + 0) [in module %s]"),
6629 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6630 filename);
6631}
6632
6633/* Read in a CU/TU header and perform some basic error checking.
6634 The contents of the header are stored in HEADER.
6635 The result is a pointer to the start of the first DIE. */
adabb602 6636
d521ce57 6637static const gdb_byte *
ed2dc618
SM
6638read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6639 struct comp_unit_head *header,
9ff913ba 6640 struct dwarf2_section_info *section,
4bdcc0c1 6641 struct dwarf2_section_info *abbrev_section,
d521ce57 6642 const gdb_byte *info_ptr,
43988095 6643 rcuh_kind section_kind)
72bf9492 6644{
d521ce57 6645 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6646
9c541725 6647 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6648
43988095 6649 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6650
9c541725 6651 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6652
ed2dc618
SM
6653 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6654 abbrev_section);
9ff913ba
DE
6655
6656 return info_ptr;
348e048f
DE
6657}
6658
f4dc4d17
DE
6659/* Fetch the abbreviation table offset from a comp or type unit header. */
6660
6661static sect_offset
ed2dc618
SM
6662read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6663 struct dwarf2_section_info *section,
9c541725 6664 sect_offset sect_off)
f4dc4d17 6665{
a32a8923 6666 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6667 const gdb_byte *info_ptr;
ac298888 6668 unsigned int initial_length_size, offset_size;
43988095 6669 uint16_t version;
f4dc4d17
DE
6670
6671 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6672 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6673 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6674 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6675 info_ptr += initial_length_size;
6676
6677 version = read_2_bytes (abfd, info_ptr);
6678 info_ptr += 2;
6679 if (version >= 5)
6680 {
6681 /* Skip unit type and address size. */
6682 info_ptr += 2;
6683 }
6684
9c541725 6685 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6686}
6687
aaa75496
JB
6688/* Allocate a new partial symtab for file named NAME and mark this new
6689 partial symtab as being an include of PST. */
6690
6691static void
891813be 6692dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6693 struct objfile *objfile)
6694{
891813be 6695 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
aaa75496 6696
fbd9ab74
JK
6697 if (!IS_ABSOLUTE_PATH (subpst->filename))
6698 {
6699 /* It shares objfile->objfile_obstack. */
6700 subpst->dirname = pst->dirname;
6701 }
6702
a9342b62 6703 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6704 subpst->dependencies[0] = pst;
6705 subpst->number_of_dependencies = 1;
6706
aaa75496
JB
6707 /* No private part is necessary for include psymtabs. This property
6708 can be used to differentiate between such include psymtabs and
10b3939b 6709 the regular ones. */
891813be 6710 subpst->per_cu_data = nullptr;
aaa75496
JB
6711}
6712
6713/* Read the Line Number Program data and extract the list of files
6714 included by the source file represented by PST. Build an include
d85a05f0 6715 partial symtab for each of these included files. */
aaa75496
JB
6716
6717static void
6718dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6719 struct die_info *die,
891813be 6720 dwarf2_psymtab *pst)
aaa75496 6721{
fff8551c 6722 line_header_up lh;
d85a05f0 6723 struct attribute *attr;
aaa75496 6724
d85a05f0 6725 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6726 if (attr != nullptr)
9c541725 6727 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6728 if (lh == NULL)
6729 return; /* No linetable, so no includes. */
6730
79748972
TT
6731 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6732 that we pass in the raw text_low here; that is ok because we're
6733 only decoding the line table to make include partial symtabs, and
6734 so the addresses aren't really used. */
4ae976d1 6735 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6736 pst->raw_text_low (), 1);
aaa75496
JB
6737}
6738
348e048f 6739static hashval_t
52dc124a 6740hash_signatured_type (const void *item)
348e048f 6741{
9a3c8263
SM
6742 const struct signatured_type *sig_type
6743 = (const struct signatured_type *) item;
9a619af0 6744
348e048f 6745 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6746 return sig_type->signature;
348e048f
DE
6747}
6748
6749static int
52dc124a 6750eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6751{
9a3c8263
SM
6752 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6753 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6754
348e048f
DE
6755 return lhs->signature == rhs->signature;
6756}
6757
1fd400ff
TT
6758/* Allocate a hash table for signatured types. */
6759
6760static htab_t
673bfd45 6761allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6762{
6763 return htab_create_alloc_ex (41,
52dc124a
DE
6764 hash_signatured_type,
6765 eq_signatured_type,
1fd400ff
TT
6766 NULL,
6767 &objfile->objfile_obstack,
6768 hashtab_obstack_allocate,
6769 dummy_obstack_deallocate);
6770}
6771
d467dd73 6772/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6773
6774static int
d467dd73 6775add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6776{
9a3c8263 6777 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6778 std::vector<signatured_type *> *all_type_units
6779 = (std::vector<signatured_type *> *) datum;
1fd400ff 6780
b2bdb8cf 6781 all_type_units->push_back (sigt);
1fd400ff
TT
6782
6783 return 1;
6784}
6785
78d4d2c5 6786/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6787 and fill them into TYPES_HTAB. It will process only type units,
6788 therefore DW_UT_type. */
c88ee1f0 6789
78d4d2c5 6790static void
ed2dc618
SM
6791create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6792 struct dwo_file *dwo_file,
43988095
JK
6793 dwarf2_section_info *section, htab_t &types_htab,
6794 rcuh_kind section_kind)
348e048f 6795{
3019eac3 6796 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6797 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6798 bfd *abfd;
6799 const gdb_byte *info_ptr, *end_ptr;
348e048f 6800
4bdcc0c1
DE
6801 abbrev_section = (dwo_file != NULL
6802 ? &dwo_file->sections.abbrev
6803 : &dwarf2_per_objfile->abbrev);
6804
b4f54984 6805 if (dwarf_read_debug)
43988095
JK
6806 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6807 get_section_name (section),
a32a8923 6808 get_section_file_name (abbrev_section));
09406207 6809
78d4d2c5
JK
6810 dwarf2_read_section (objfile, section);
6811 info_ptr = section->buffer;
348e048f 6812
78d4d2c5
JK
6813 if (info_ptr == NULL)
6814 return;
348e048f 6815
78d4d2c5
JK
6816 /* We can't set abfd until now because the section may be empty or
6817 not present, in which case the bfd is unknown. */
6818 abfd = get_section_bfd_owner (section);
348e048f 6819
c0ab21c2
TT
6820 /* We don't use cutu_reader here because we don't need to read
6821 any dies: the signature is in the header. */
3019eac3 6822
78d4d2c5
JK
6823 end_ptr = info_ptr + section->size;
6824 while (info_ptr < end_ptr)
6825 {
78d4d2c5
JK
6826 struct signatured_type *sig_type;
6827 struct dwo_unit *dwo_tu;
6828 void **slot;
6829 const gdb_byte *ptr = info_ptr;
6830 struct comp_unit_head header;
6831 unsigned int length;
8b70b953 6832
9c541725 6833 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6834
a49dd8dd
JK
6835 /* Initialize it due to a false compiler warning. */
6836 header.signature = -1;
9c541725 6837 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6838
78d4d2c5
JK
6839 /* We need to read the type's signature in order to build the hash
6840 table, but we don't need anything else just yet. */
348e048f 6841
ed2dc618 6842 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6843 abbrev_section, ptr, section_kind);
348e048f 6844
78d4d2c5 6845 length = get_cu_length (&header);
6caca83c 6846
78d4d2c5
JK
6847 /* Skip dummy type units. */
6848 if (ptr >= info_ptr + length
43988095
JK
6849 || peek_abbrev_code (abfd, ptr) == 0
6850 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6851 {
6852 info_ptr += length;
6853 continue;
6854 }
dee91e82 6855
78d4d2c5
JK
6856 if (types_htab == NULL)
6857 {
6858 if (dwo_file)
6859 types_htab = allocate_dwo_unit_table (objfile);
6860 else
6861 types_htab = allocate_signatured_type_table (objfile);
6862 }
8b70b953 6863
78d4d2c5
JK
6864 if (dwo_file)
6865 {
6866 sig_type = NULL;
6867 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6868 struct dwo_unit);
6869 dwo_tu->dwo_file = dwo_file;
43988095 6870 dwo_tu->signature = header.signature;
9c541725 6871 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6872 dwo_tu->section = section;
9c541725 6873 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6874 dwo_tu->length = length;
6875 }
6876 else
6877 {
6878 /* N.B.: type_offset is not usable if this type uses a DWO file.
6879 The real type_offset is in the DWO file. */
6880 dwo_tu = NULL;
6881 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6882 struct signatured_type);
43988095 6883 sig_type->signature = header.signature;
9c541725 6884 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6885 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6886 sig_type->per_cu.is_debug_types = 1;
6887 sig_type->per_cu.section = section;
9c541725 6888 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6889 sig_type->per_cu.length = length;
6890 }
6891
6892 slot = htab_find_slot (types_htab,
6893 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6894 INSERT);
6895 gdb_assert (slot != NULL);
6896 if (*slot != NULL)
6897 {
9c541725 6898 sect_offset dup_sect_off;
0349ea22 6899
3019eac3
DE
6900 if (dwo_file)
6901 {
78d4d2c5
JK
6902 const struct dwo_unit *dup_tu
6903 = (const struct dwo_unit *) *slot;
6904
9c541725 6905 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6906 }
6907 else
6908 {
78d4d2c5
JK
6909 const struct signatured_type *dup_tu
6910 = (const struct signatured_type *) *slot;
6911
9c541725 6912 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6913 }
8b70b953 6914
b98664d3 6915 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6916 " the entry at offset %s, signature %s"),
6917 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6918 hex_string (header.signature));
78d4d2c5
JK
6919 }
6920 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6921
78d4d2c5 6922 if (dwarf_read_debug > 1)
9d8780f0
SM
6923 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6924 sect_offset_str (sect_off),
43988095 6925 hex_string (header.signature));
3019eac3 6926
78d4d2c5
JK
6927 info_ptr += length;
6928 }
6929}
3019eac3 6930
78d4d2c5
JK
6931/* Create the hash table of all entries in the .debug_types
6932 (or .debug_types.dwo) section(s).
6933 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6934 otherwise it is NULL.
b3c8eb43 6935
78d4d2c5 6936 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6937
78d4d2c5 6938 Note: This function processes DWO files only, not DWP files. */
348e048f 6939
78d4d2c5 6940static void
ed2dc618
SM
6941create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6942 struct dwo_file *dwo_file,
fd5866f6 6943 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6944 htab_t &types_htab)
6945{
fd5866f6
SM
6946 for (dwarf2_section_info &section : type_sections)
6947 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6948 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6949}
6950
6951/* Create the hash table of all entries in the .debug_types section,
6952 and initialize all_type_units.
6953 The result is zero if there is an error (e.g. missing .debug_types section),
6954 otherwise non-zero. */
6955
6956static int
ed2dc618 6957create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6958{
78d4d2c5 6959 htab_t types_htab = NULL;
3019eac3 6960
ed2dc618
SM
6961 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6962 &dwarf2_per_objfile->info, types_htab,
43988095 6963 rcuh_kind::COMPILE);
ed2dc618
SM
6964 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6965 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6966 if (types_htab == NULL)
6967 {
6968 dwarf2_per_objfile->signatured_types = NULL;
6969 return 0;
6970 }
6971
348e048f
DE
6972 dwarf2_per_objfile->signatured_types = types_htab;
6973
b2bdb8cf
SM
6974 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6975 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6976
6977 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6978 &dwarf2_per_objfile->all_type_units);
1fd400ff 6979
348e048f
DE
6980 return 1;
6981}
6982
6aa5f3a6
DE
6983/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6984 If SLOT is non-NULL, it is the entry to use in the hash table.
6985 Otherwise we find one. */
6986
6987static struct signatured_type *
ed2dc618
SM
6988add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6989 void **slot)
6aa5f3a6
DE
6990{
6991 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6992
b2bdb8cf
SM
6993 if (dwarf2_per_objfile->all_type_units.size ()
6994 == dwarf2_per_objfile->all_type_units.capacity ())
6995 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6996
b2bdb8cf
SM
6997 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6998 struct signatured_type);
6999
7000 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
7001 sig_type->signature = sig;
7002 sig_type->per_cu.is_debug_types = 1;
7003 if (dwarf2_per_objfile->using_index)
7004 {
7005 sig_type->per_cu.v.quick =
7006 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7007 struct dwarf2_per_cu_quick_data);
7008 }
7009
7010 if (slot == NULL)
7011 {
7012 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7013 sig_type, INSERT);
7014 }
7015 gdb_assert (*slot == NULL);
7016 *slot = sig_type;
7017 /* The rest of sig_type must be filled in by the caller. */
7018 return sig_type;
7019}
7020
a2ce51a0
DE
7021/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7022 Fill in SIG_ENTRY with DWO_ENTRY. */
7023
7024static void
ed2dc618 7025fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
7026 struct signatured_type *sig_entry,
7027 struct dwo_unit *dwo_entry)
7028{
7ee85ab1 7029 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7030 gdb_assert (! sig_entry->per_cu.queued);
7031 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7032 if (dwarf2_per_objfile->using_index)
7033 {
7034 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7035 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7036 }
7037 else
7038 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7039 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7040 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7041 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7042 gdb_assert (sig_entry->dwo_unit == NULL);
7043
7044 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7045 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7046 sig_entry->per_cu.length = dwo_entry->length;
7047 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7048 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7049 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7050 sig_entry->dwo_unit = dwo_entry;
7051}
7052
7053/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7054 If we haven't read the TU yet, create the signatured_type data structure
7055 for a TU to be read in directly from a DWO file, bypassing the stub.
7056 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7057 using .gdb_index, then when reading a CU we want to stay in the DWO file
7058 containing that CU. Otherwise we could end up reading several other DWO
7059 files (due to comdat folding) to process the transitive closure of all the
7060 mentioned TUs, and that can be slow. The current DWO file will have every
7061 type signature that it needs.
a2ce51a0
DE
7062 We only do this for .gdb_index because in the psymtab case we already have
7063 to read all the DWOs to build the type unit groups. */
7064
7065static struct signatured_type *
7066lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7067{
518817b3
SM
7068 struct dwarf2_per_objfile *dwarf2_per_objfile
7069 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7070 struct objfile *objfile = dwarf2_per_objfile->objfile;
7071 struct dwo_file *dwo_file;
7072 struct dwo_unit find_dwo_entry, *dwo_entry;
7073 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7074 void **slot;
a2ce51a0
DE
7075
7076 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7077
6aa5f3a6
DE
7078 /* If TU skeletons have been removed then we may not have read in any
7079 TUs yet. */
7080 if (dwarf2_per_objfile->signatured_types == NULL)
7081 {
7082 dwarf2_per_objfile->signatured_types
7083 = allocate_signatured_type_table (objfile);
7084 }
a2ce51a0
DE
7085
7086 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7087 Use the global signatured_types array to do our own comdat-folding
7088 of types. If this is the first time we're reading this TU, and
7089 the TU has an entry in .gdb_index, replace the recorded data from
7090 .gdb_index with this TU. */
a2ce51a0 7091
a2ce51a0 7092 find_sig_entry.signature = sig;
6aa5f3a6
DE
7093 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7094 &find_sig_entry, INSERT);
9a3c8263 7095 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7096
7097 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7098 read. Don't reassign the global entry to point to this DWO if that's
7099 the case. Also note that if the TU is already being read, it may not
7100 have come from a DWO, the program may be a mix of Fission-compiled
7101 code and non-Fission-compiled code. */
7102
7103 /* Have we already tried to read this TU?
7104 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7105 needn't exist in the global table yet). */
7106 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7107 return sig_entry;
7108
6aa5f3a6
DE
7109 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7110 dwo_unit of the TU itself. */
7111 dwo_file = cu->dwo_unit->dwo_file;
7112
a2ce51a0
DE
7113 /* Ok, this is the first time we're reading this TU. */
7114 if (dwo_file->tus == NULL)
7115 return NULL;
7116 find_dwo_entry.signature = sig;
9a3c8263 7117 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7118 if (dwo_entry == NULL)
7119 return NULL;
7120
6aa5f3a6
DE
7121 /* If the global table doesn't have an entry for this TU, add one. */
7122 if (sig_entry == NULL)
ed2dc618 7123 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7124
ed2dc618 7125 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7126 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7127 return sig_entry;
7128}
7129
a2ce51a0
DE
7130/* Subroutine of lookup_signatured_type.
7131 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7132 then try the DWP file. If the TU stub (skeleton) has been removed then
7133 it won't be in .gdb_index. */
a2ce51a0
DE
7134
7135static struct signatured_type *
7136lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7137{
518817b3
SM
7138 struct dwarf2_per_objfile *dwarf2_per_objfile
7139 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7140 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7141 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7142 struct dwo_unit *dwo_entry;
7143 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7144 void **slot;
a2ce51a0
DE
7145
7146 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7147 gdb_assert (dwp_file != NULL);
7148
6aa5f3a6
DE
7149 /* If TU skeletons have been removed then we may not have read in any
7150 TUs yet. */
7151 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7152 {
6aa5f3a6
DE
7153 dwarf2_per_objfile->signatured_types
7154 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7155 }
7156
6aa5f3a6
DE
7157 find_sig_entry.signature = sig;
7158 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7159 &find_sig_entry, INSERT);
9a3c8263 7160 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7161
7162 /* Have we already tried to read this TU?
7163 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7164 needn't exist in the global table yet). */
7165 if (sig_entry != NULL)
7166 return sig_entry;
7167
a2ce51a0
DE
7168 if (dwp_file->tus == NULL)
7169 return NULL;
ed2dc618 7170 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7171 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7172 if (dwo_entry == NULL)
7173 return NULL;
7174
ed2dc618
SM
7175 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7176 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7177
a2ce51a0
DE
7178 return sig_entry;
7179}
7180
380bca97 7181/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7182 Returns NULL if signature SIG is not present in the table.
7183 It is up to the caller to complain about this. */
348e048f
DE
7184
7185static struct signatured_type *
a2ce51a0 7186lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7187{
518817b3
SM
7188 struct dwarf2_per_objfile *dwarf2_per_objfile
7189 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7190
a2ce51a0
DE
7191 if (cu->dwo_unit
7192 && dwarf2_per_objfile->using_index)
7193 {
7194 /* We're in a DWO/DWP file, and we're using .gdb_index.
7195 These cases require special processing. */
ed2dc618 7196 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7197 return lookup_dwo_signatured_type (cu, sig);
7198 else
7199 return lookup_dwp_signatured_type (cu, sig);
7200 }
7201 else
7202 {
7203 struct signatured_type find_entry, *entry;
348e048f 7204
a2ce51a0
DE
7205 if (dwarf2_per_objfile->signatured_types == NULL)
7206 return NULL;
7207 find_entry.signature = sig;
9a3c8263
SM
7208 entry = ((struct signatured_type *)
7209 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7210 return entry;
7211 }
348e048f 7212}
18a8505e
AT
7213
7214/* Return the address base of the compile unit, which, if exists, is stored
7215 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
7216static gdb::optional<ULONGEST>
7217lookup_addr_base (struct die_info *comp_unit_die)
7218{
7219 struct attribute *attr;
7220 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
7221 if (attr == nullptr)
7222 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
7223 if (attr == nullptr)
7224 return gdb::optional<ULONGEST> ();
7225 return DW_UNSND (attr);
7226}
7227
7228/* Return range lists base of the compile unit, which, if exists, is stored
7229 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
7230static ULONGEST
7231lookup_ranges_base (struct die_info *comp_unit_die)
7232{
7233 struct attribute *attr;
7234 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
7235 if (attr == nullptr)
7236 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
7237 if (attr == nullptr)
7238 return 0;
7239 return DW_UNSND (attr);
7240}
7241
42e7ad6c 7242/* Low level DIE reading support. */
348e048f 7243
d85a05f0
DJ
7244/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7245
7246static void
7247init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7248 struct dwarf2_cu *cu,
3019eac3 7249 struct dwarf2_section_info *section,
685af9cd
TT
7250 struct dwo_file *dwo_file,
7251 struct abbrev_table *abbrev_table)
d85a05f0 7252{
fceca515 7253 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7254 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7255 reader->cu = cu;
3019eac3 7256 reader->dwo_file = dwo_file;
dee91e82
DE
7257 reader->die_section = section;
7258 reader->buffer = section->buffer;
f664829e 7259 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7260 reader->comp_dir = NULL;
685af9cd 7261 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7262}
7263
c0ab21c2 7264/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7265 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 7266 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
7267 already.
7268
7269 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7270 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7271 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7272 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7273 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7274 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7275 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7276 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7277 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7278 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7279 kept around for at least as long as *RESULT_READER.
7280
b0c7bfa9
DE
7281 The result is non-zero if a valid (non-dummy) DIE was found. */
7282
7283static int
7284read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7285 struct dwo_unit *dwo_unit,
b0c7bfa9 7286 struct die_info *stub_comp_unit_die,
a2ce51a0 7287 const char *stub_comp_dir,
b0c7bfa9 7288 struct die_reader_specs *result_reader,
d521ce57 7289 const gdb_byte **result_info_ptr,
b0c7bfa9 7290 struct die_info **result_comp_unit_die,
685af9cd
TT
7291 int *result_has_children,
7292 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7293{
ed2dc618 7294 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7295 struct objfile *objfile = dwarf2_per_objfile->objfile;
7296 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7297 bfd *abfd;
d521ce57 7298 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7299 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7300 int i,num_extra_attrs;
7301 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
7302 struct die_info *comp_unit_die;
7303
b0aeadb3
DE
7304 /* At most one of these may be provided. */
7305 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7306
b0c7bfa9
DE
7307 /* These attributes aren't processed until later:
7308 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7309 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7310 referenced later. However, these attributes are found in the stub
7311 which we won't have later. In order to not impose this complication
7312 on the rest of the code, we read them here and copy them to the
7313 DWO CU/TU die. */
b0c7bfa9
DE
7314
7315 stmt_list = NULL;
7316 low_pc = NULL;
7317 high_pc = NULL;
7318 ranges = NULL;
7319 comp_dir = NULL;
7320
7321 if (stub_comp_unit_die != NULL)
7322 {
7323 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7324 DWO file. */
7325 if (! this_cu->is_debug_types)
7326 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7327 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7328 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7329 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7330 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7331
18a8505e 7332 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 7333
18a8505e
AT
7334 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
7335 here (if needed). We need the value before we can process
7336 DW_AT_ranges. */
7337 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 7338 }
a2ce51a0
DE
7339 else if (stub_comp_dir != NULL)
7340 {
7341 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7342 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7343 comp_dir->name = DW_AT_comp_dir;
7344 comp_dir->form = DW_FORM_string;
7345 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7346 DW_STRING (comp_dir) = stub_comp_dir;
7347 }
b0c7bfa9
DE
7348
7349 /* Set up for reading the DWO CU/TU. */
7350 cu->dwo_unit = dwo_unit;
685af9cd 7351 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7352 dwarf2_read_section (objfile, section);
a32a8923 7353 abfd = get_section_bfd_owner (section);
9c541725
PA
7354 begin_info_ptr = info_ptr = (section->buffer
7355 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7356 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7357
7358 if (this_cu->is_debug_types)
7359 {
b0c7bfa9
DE
7360 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7361
ed2dc618
SM
7362 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7363 &cu->header, section,
b0c7bfa9 7364 dwo_abbrev_section,
43988095 7365 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7366 /* This is not an assert because it can be caused by bad debug info. */
43988095 7367 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7368 {
7369 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7370 " TU at offset %s [in module %s]"),
a2ce51a0 7371 hex_string (sig_type->signature),
43988095 7372 hex_string (cu->header.signature),
9d8780f0 7373 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7374 bfd_get_filename (abfd));
7375 }
9c541725 7376 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7377 /* For DWOs coming from DWP files, we don't know the CU length
7378 nor the type's offset in the TU until now. */
7379 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7380 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7381
7382 /* Establish the type offset that can be used to lookup the type.
7383 For DWO files, we don't know it until now. */
9c541725
PA
7384 sig_type->type_offset_in_section
7385 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7386 }
7387 else
7388 {
ed2dc618
SM
7389 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7390 &cu->header, section,
b0c7bfa9 7391 dwo_abbrev_section,
43988095 7392 info_ptr, rcuh_kind::COMPILE);
9c541725 7393 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7394 /* For DWOs coming from DWP files, we don't know the CU length
7395 until now. */
7396 dwo_unit->length = get_cu_length (&cu->header);
7397 }
7398
685af9cd
TT
7399 *result_dwo_abbrev_table
7400 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7401 cu->header.abbrev_sect_off);
7402 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7403 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7404
7405 /* Read in the die, but leave space to copy over the attributes
7406 from the stub. This has the benefit of simplifying the rest of
7407 the code - all the work to maintain the illusion of a single
7408 DW_TAG_{compile,type}_unit DIE is done here. */
7409 num_extra_attrs = ((stmt_list != NULL)
7410 + (low_pc != NULL)
7411 + (high_pc != NULL)
7412 + (ranges != NULL)
7413 + (comp_dir != NULL));
7414 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7415 result_has_children, num_extra_attrs);
7416
7417 /* Copy over the attributes from the stub to the DIE we just read in. */
7418 comp_unit_die = *result_comp_unit_die;
7419 i = comp_unit_die->num_attrs;
7420 if (stmt_list != NULL)
7421 comp_unit_die->attrs[i++] = *stmt_list;
7422 if (low_pc != NULL)
7423 comp_unit_die->attrs[i++] = *low_pc;
7424 if (high_pc != NULL)
7425 comp_unit_die->attrs[i++] = *high_pc;
7426 if (ranges != NULL)
7427 comp_unit_die->attrs[i++] = *ranges;
7428 if (comp_dir != NULL)
7429 comp_unit_die->attrs[i++] = *comp_dir;
7430 comp_unit_die->num_attrs += num_extra_attrs;
7431
b4f54984 7432 if (dwarf_die_debug)
bf6af496
DE
7433 {
7434 fprintf_unfiltered (gdb_stdlog,
7435 "Read die from %s@0x%x of %s:\n",
a32a8923 7436 get_section_name (section),
bf6af496
DE
7437 (unsigned) (begin_info_ptr - section->buffer),
7438 bfd_get_filename (abfd));
b4f54984 7439 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7440 }
7441
a2ce51a0
DE
7442 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7443 TUs by skipping the stub and going directly to the entry in the DWO file.
7444 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7445 to get it via circuitous means. Blech. */
7446 if (comp_dir != NULL)
7447 result_reader->comp_dir = DW_STRING (comp_dir);
7448
b0c7bfa9
DE
7449 /* Skip dummy compilation units. */
7450 if (info_ptr >= begin_info_ptr + dwo_unit->length
7451 || peek_abbrev_code (abfd, info_ptr) == 0)
7452 return 0;
7453
7454 *result_info_ptr = info_ptr;
7455 return 1;
7456}
7457
a084a2a6
AT
7458/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7459 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7460 signature is part of the header. */
7461static gdb::optional<ULONGEST>
7462lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7463{
7464 if (cu->header.version >= 5)
7465 return cu->header.signature;
7466 struct attribute *attr;
7467 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7468 if (attr == nullptr)
7469 return gdb::optional<ULONGEST> ();
7470 return DW_UNSND (attr);
7471}
7472
c0ab21c2 7473/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7474 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7475 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7476
7477static struct dwo_unit *
7478lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
7479 struct die_info *comp_unit_die,
7480 const char *dwo_name)
b0c7bfa9
DE
7481{
7482 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7483 struct dwo_unit *dwo_unit;
c0ab21c2 7484 const char *comp_dir;
b0c7bfa9 7485
a2ce51a0
DE
7486 gdb_assert (cu != NULL);
7487
b0c7bfa9 7488 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7489 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7490 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7491
7492 if (this_cu->is_debug_types)
7493 {
7494 struct signatured_type *sig_type;
7495
7496 /* Since this_cu is the first member of struct signatured_type,
7497 we can go from a pointer to one to a pointer to the other. */
7498 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7499 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7500 }
7501 else
7502 {
a084a2a6
AT
7503 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7504 if (!signature.has_value ())
b0c7bfa9
DE
7505 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7506 " [in module %s]"),
e3b94546 7507 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7508 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7509 *signature);
b0c7bfa9
DE
7510 }
7511
b0c7bfa9
DE
7512 return dwo_unit;
7513}
7514
c0ab21c2 7515/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7516 See it for a description of the parameters.
fcd3b13d 7517 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7518
c0ab21c2
TT
7519void
7520cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7521 int use_existing_cu, int keep)
a2ce51a0 7522{
a2ce51a0 7523 struct signatured_type *sig_type;
a2ce51a0 7524 struct die_reader_specs reader;
a2ce51a0
DE
7525
7526 /* Verify we can do the following downcast, and that we have the
7527 data we need. */
7528 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7529 sig_type = (struct signatured_type *) this_cu;
7530 gdb_assert (sig_type->dwo_unit != NULL);
7531
6aa5f3a6
DE
7532 if (use_existing_cu && this_cu->cu != NULL)
7533 {
7534 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7535 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7536 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7537 }
7538 else
7539 {
7540 /* If !use_existing_cu, this_cu->cu must be NULL. */
7541 gdb_assert (this_cu->cu == NULL);
c0ab21c2 7542 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7543 }
7544
7545 /* A future optimization, if needed, would be to use an existing
7546 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7547 could share abbrev tables. */
a2ce51a0
DE
7548
7549 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7550 NULL /* stub_comp_unit_die */,
7551 sig_type->dwo_unit->dwo_file->comp_dir,
7552 &reader, &info_ptr,
685af9cd 7553 &comp_unit_die, &has_children,
c0ab21c2 7554 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7555 {
7556 /* Dummy die. */
c0ab21c2 7557 dummy_p = true;
a2ce51a0 7558 }
a2ce51a0
DE
7559}
7560
fd820528 7561/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7562 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7563
f4dc4d17
DE
7564 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7565 Otherwise the table specified in the comp unit header is read in and used.
7566 This is an optimization for when we already have the abbrev table.
7567
dee91e82
DE
7568 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7569 Otherwise, a new CU is allocated with xmalloc.
7570
7571 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
c0ab21c2
TT
7572 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7573 end. */
aaa75496 7574
c0ab21c2
TT
7575cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7576 struct abbrev_table *abbrev_table,
7577 int use_existing_cu, int keep,
7578 bool skip_partial)
7579 : die_reader_specs {},
7580 m_this_cu (this_cu),
7581 m_keep (keep)
c906108c 7582{
ed2dc618 7583 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7584 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7585 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7586 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7587 struct dwarf2_cu *cu;
c0ab21c2 7588 const gdb_byte *begin_info_ptr;
dee91e82 7589 struct signatured_type *sig_type = NULL;
4bdcc0c1 7590 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7591 /* Non-zero if CU currently points to a DWO file and we need to
7592 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7593 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7594 int rereading_dwo_cu = 0;
c906108c 7595
b4f54984 7596 if (dwarf_die_debug)
9d8780f0 7597 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7598 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7599 sect_offset_str (this_cu->sect_off));
09406207 7600
dee91e82
DE
7601 if (use_existing_cu)
7602 gdb_assert (keep);
23745b47 7603
a2ce51a0
DE
7604 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7605 file (instead of going through the stub), short-circuit all of this. */
7606 if (this_cu->reading_dwo_directly)
7607 {
7608 /* Narrow down the scope of possibilities to have to understand. */
7609 gdb_assert (this_cu->is_debug_types);
7610 gdb_assert (abbrev_table == NULL);
c0ab21c2 7611 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
a2ce51a0
DE
7612 return;
7613 }
7614
dee91e82
DE
7615 /* This is cheap if the section is already read in. */
7616 dwarf2_read_section (objfile, section);
7617
9c541725 7618 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7619
7620 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7621
7622 if (use_existing_cu && this_cu->cu != NULL)
7623 {
7624 cu = this_cu->cu;
42e7ad6c
DE
7625 /* If this CU is from a DWO file we need to start over, we need to
7626 refetch the attributes from the skeleton CU.
7627 This could be optimized by retrieving those attributes from when we
7628 were here the first time: the previous comp_unit_die was stored in
7629 comp_unit_obstack. But there's no data yet that we need this
7630 optimization. */
7631 if (cu->dwo_unit != NULL)
7632 rereading_dwo_cu = 1;
dee91e82
DE
7633 }
7634 else
7635 {
7636 /* If !use_existing_cu, this_cu->cu must be NULL. */
7637 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
7638 m_new_cu.reset (new dwarf2_cu (this_cu));
7639 cu = m_new_cu.get ();
42e7ad6c 7640 }
dee91e82 7641
b0c7bfa9 7642 /* Get the header. */
9c541725 7643 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7644 {
7645 /* We already have the header, there's no need to read it in again. */
9c541725 7646 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7647 }
7648 else
7649 {
3019eac3 7650 if (this_cu->is_debug_types)
dee91e82 7651 {
ed2dc618
SM
7652 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7653 &cu->header, section,
4bdcc0c1 7654 abbrev_section, info_ptr,
43988095 7655 rcuh_kind::TYPE);
dee91e82 7656
42e7ad6c
DE
7657 /* Since per_cu is the first member of struct signatured_type,
7658 we can go from a pointer to one to a pointer to the other. */
7659 sig_type = (struct signatured_type *) this_cu;
43988095 7660 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7661 gdb_assert (sig_type->type_offset_in_tu
7662 == cu->header.type_cu_offset_in_tu);
7663 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7664
42e7ad6c
DE
7665 /* LENGTH has not been set yet for type units if we're
7666 using .gdb_index. */
1ce1cefd 7667 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7668
7669 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7670 sig_type->type_offset_in_section =
7671 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7672
7673 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7674 }
7675 else
7676 {
ed2dc618
SM
7677 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7678 &cu->header, section,
4bdcc0c1 7679 abbrev_section,
43988095
JK
7680 info_ptr,
7681 rcuh_kind::COMPILE);
dee91e82 7682
9c541725 7683 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7684 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7685 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7686 }
7687 }
10b3939b 7688
6caca83c 7689 /* Skip dummy compilation units. */
dee91e82 7690 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7691 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7692 {
7693 dummy_p = true;
7694 return;
7695 }
6caca83c 7696
433df2d4
DE
7697 /* If we don't have them yet, read the abbrevs for this compilation unit.
7698 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7699 done. */
f4dc4d17 7700 if (abbrev_table != NULL)
685af9cd
TT
7701 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7702 else
f4dc4d17 7703 {
c0ab21c2 7704 m_abbrev_table_holder
685af9cd
TT
7705 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7706 cu->header.abbrev_sect_off);
c0ab21c2 7707 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7708 }
af703f96 7709
dee91e82 7710 /* Read the top level CU/TU die. */
c0ab21c2
TT
7711 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7712 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
93311388 7713
58f0c718 7714 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7715 {
7716 dummy_p = true;
7717 return;
7718 }
58f0c718 7719
b0c7bfa9 7720 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7721 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7722 table from the DWO file and pass the ownership over to us. It will be
7723 referenced from READER, so we must make sure to free it after we're done
7724 with READER.
7725
b0c7bfa9
DE
7726 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7727 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7728 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7729 if (dwo_name != nullptr)
3019eac3 7730 {
3019eac3 7731 struct dwo_unit *dwo_unit;
b0c7bfa9 7732 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7733
7734 if (has_children)
6a506a2d 7735 {
b98664d3 7736 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7737 " has children (offset %s) [in module %s]"),
7738 sect_offset_str (this_cu->sect_off),
7739 bfd_get_filename (abfd));
6a506a2d 7740 }
c0ab21c2 7741 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7742 if (dwo_unit != NULL)
3019eac3 7743 {
6a506a2d 7744 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7745 comp_unit_die, NULL,
c0ab21c2 7746 this, &info_ptr,
685af9cd 7747 &dwo_comp_unit_die, &has_children,
c0ab21c2 7748 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7749 {
7750 /* Dummy die. */
c0ab21c2 7751 dummy_p = true;
6a506a2d
DE
7752 return;
7753 }
7754 comp_unit_die = dwo_comp_unit_die;
7755 }
7756 else
7757 {
7758 /* Yikes, we couldn't find the rest of the DIE, we only have
7759 the stub. A complaint has already been logged. There's
7760 not much more we can do except pass on the stub DIE to
7761 die_reader_func. We don't want to throw an error on bad
7762 debug info. */
3019eac3
DE
7763 }
7764 }
c0ab21c2 7765}
3019eac3 7766
c0ab21c2
TT
7767cutu_reader::~cutu_reader ()
7768{
b0c7bfa9 7769 /* Done, clean up. */
c0ab21c2 7770 if (m_new_cu != NULL && m_keep && !dummy_p)
348e048f 7771 {
c0ab21c2
TT
7772 struct dwarf2_per_objfile *dwarf2_per_objfile
7773 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7774 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7775 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7776 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7777 /* The chain owns it now. */
c0ab21c2 7778 m_new_cu.release ();
348e048f 7779 }
dee91e82
DE
7780}
7781
18a8505e
AT
7782/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7783 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7784 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7785
7786 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7787 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7788
7789 We fill in THIS_CU->length.
7790
dee91e82 7791 THIS_CU->cu is always freed when done.
3019eac3 7792 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7793 to care whether it refers to the "main" CU or the DWO CU.
7794
7795 When parent_cu is passed, it is used to provide a default value for
7796 str_offsets_base and addr_base from the parent. */
dee91e82 7797
c0ab21c2
TT
7798cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7799 struct dwarf2_cu *parent_cu,
7800 struct dwo_file *dwo_file)
7801 : die_reader_specs {},
7802 m_this_cu (this_cu)
dee91e82 7803{
ed2dc618 7804 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7805 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7806 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7807 bfd *abfd = get_section_bfd_owner (section);
33e80786 7808 struct dwarf2_section_info *abbrev_section;
d521ce57 7809 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
7810 int has_children;
7811
b4f54984 7812 if (dwarf_die_debug)
9d8780f0 7813 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7814 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7815 sect_offset_str (this_cu->sect_off));
09406207 7816
dee91e82
DE
7817 gdb_assert (this_cu->cu == NULL);
7818
33e80786
DE
7819 abbrev_section = (dwo_file != NULL
7820 ? &dwo_file->sections.abbrev
7821 : get_abbrev_section_for_cu (this_cu));
7822
dee91e82
DE
7823 /* This is cheap if the section is already read in. */
7824 dwarf2_read_section (objfile, section);
7825
c0ab21c2 7826 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7827
9c541725 7828 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7829 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7830 &m_new_cu->header, section,
4bdcc0c1 7831 abbrev_section, info_ptr,
43988095
JK
7832 (this_cu->is_debug_types
7833 ? rcuh_kind::TYPE
7834 : rcuh_kind::COMPILE));
dee91e82 7835
18a8505e
AT
7836 if (parent_cu != nullptr)
7837 {
c0ab21c2
TT
7838 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7839 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7840 }
c0ab21c2 7841 this_cu->length = get_cu_length (&m_new_cu->header);
dee91e82
DE
7842
7843 /* Skip dummy compilation units. */
7844 if (info_ptr >= begin_info_ptr + this_cu->length
7845 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7846 {
7847 dummy_p = true;
7848 return;
7849 }
72bf9492 7850
c0ab21c2 7851 m_abbrev_table_holder
685af9cd 7852 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
c0ab21c2 7853 m_new_cu->header.abbrev_sect_off);
dee91e82 7854
c0ab21c2
TT
7855 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7856 m_abbrev_table_holder.get ());
7857 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
dee91e82
DE
7858}
7859
0018ea6f
DE
7860\f
7861/* Type Unit Groups.
dee91e82 7862
0018ea6f
DE
7863 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7864 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7865 so that all types coming from the same compilation (.o file) are grouped
7866 together. A future step could be to put the types in the same symtab as
7867 the CU the types ultimately came from. */
ff013f42 7868
f4dc4d17
DE
7869static hashval_t
7870hash_type_unit_group (const void *item)
7871{
9a3c8263
SM
7872 const struct type_unit_group *tu_group
7873 = (const struct type_unit_group *) item;
f4dc4d17 7874
094b34ac 7875 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7876}
348e048f
DE
7877
7878static int
f4dc4d17 7879eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7880{
9a3c8263
SM
7881 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7882 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7883
094b34ac 7884 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7885}
348e048f 7886
f4dc4d17
DE
7887/* Allocate a hash table for type unit groups. */
7888
7889static htab_t
ed2dc618 7890allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7891{
7892 return htab_create_alloc_ex (3,
7893 hash_type_unit_group,
7894 eq_type_unit_group,
7895 NULL,
ed2dc618 7896 &objfile->objfile_obstack,
f4dc4d17
DE
7897 hashtab_obstack_allocate,
7898 dummy_obstack_deallocate);
7899}
dee91e82 7900
f4dc4d17
DE
7901/* Type units that don't have DW_AT_stmt_list are grouped into their own
7902 partial symtabs. We combine several TUs per psymtab to not let the size
7903 of any one psymtab grow too big. */
7904#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7905#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7906
094b34ac 7907/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7908 Create the type_unit_group object used to hold one or more TUs. */
7909
7910static struct type_unit_group *
094b34ac 7911create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7912{
518817b3
SM
7913 struct dwarf2_per_objfile *dwarf2_per_objfile
7914 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7915 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7916 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7917 struct type_unit_group *tu_group;
f4dc4d17
DE
7918
7919 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7920 struct type_unit_group);
094b34ac 7921 per_cu = &tu_group->per_cu;
518817b3 7922 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7923
094b34ac
DE
7924 if (dwarf2_per_objfile->using_index)
7925 {
7926 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7927 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7928 }
7929 else
7930 {
9c541725 7931 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7932 dwarf2_psymtab *pst;
528e1572 7933 std::string name;
094b34ac
DE
7934
7935 /* Give the symtab a useful name for debug purposes. */
7936 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7937 name = string_printf ("<type_units_%d>",
7938 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7939 else
528e1572 7940 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7941
528e1572 7942 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7943 pst->anonymous = true;
094b34ac 7944 }
f4dc4d17 7945
094b34ac 7946 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7947 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7948
7949 return tu_group;
7950}
7951
094b34ac
DE
7952/* Look up the type_unit_group for type unit CU, and create it if necessary.
7953 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7954
7955static struct type_unit_group *
ff39bb5e 7956get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7957{
518817b3
SM
7958 struct dwarf2_per_objfile *dwarf2_per_objfile
7959 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7960 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7961 struct type_unit_group *tu_group;
7962 void **slot;
7963 unsigned int line_offset;
7964 struct type_unit_group type_unit_group_for_lookup;
7965
7966 if (dwarf2_per_objfile->type_unit_groups == NULL)
7967 {
7968 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7969 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7970 }
7971
7972 /* Do we need to create a new group, or can we use an existing one? */
7973
7974 if (stmt_list)
7975 {
7976 line_offset = DW_UNSND (stmt_list);
7977 ++tu_stats->nr_symtab_sharers;
7978 }
7979 else
7980 {
7981 /* Ugh, no stmt_list. Rare, but we have to handle it.
7982 We can do various things here like create one group per TU or
7983 spread them over multiple groups to split up the expansion work.
7984 To avoid worst case scenarios (too many groups or too large groups)
7985 we, umm, group them in bunches. */
7986 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7987 | (tu_stats->nr_stmt_less_type_units
7988 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7989 ++tu_stats->nr_stmt_less_type_units;
7990 }
7991
094b34ac 7992 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7993 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7994 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7995 &type_unit_group_for_lookup, INSERT);
7996 if (*slot != NULL)
7997 {
9a3c8263 7998 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7999 gdb_assert (tu_group != NULL);
8000 }
8001 else
8002 {
9c541725 8003 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8004 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8005 *slot = tu_group;
8006 ++tu_stats->nr_symtabs;
8007 }
8008
8009 return tu_group;
8010}
0018ea6f
DE
8011\f
8012/* Partial symbol tables. */
8013
8014/* Create a psymtab named NAME and assign it to PER_CU.
8015
8016 The caller must fill in the following details:
8017 dirname, textlow, texthigh. */
8018
891813be 8019static dwarf2_psymtab *
0018ea6f
DE
8020create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8021{
e3b94546 8022 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 8023 dwarf2_psymtab *pst;
0018ea6f 8024
891813be 8025 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 8026
6d94535f 8027 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
8028
8029 /* This is the glue that links PST into GDB's symbol API. */
891813be 8030 pst->per_cu_data = per_cu;
0018ea6f
DE
8031 per_cu->v.psymtab = pst;
8032
8033 return pst;
8034}
8035
c0ab21c2 8036/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
8037
8038static void
8039process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8040 const gdb_byte *info_ptr,
0018ea6f
DE
8041 struct die_info *comp_unit_die,
8042 int has_children,
c0ab21c2
TT
8043 int want_partial_unit,
8044 enum language pretend_language)
0018ea6f
DE
8045{
8046 struct dwarf2_cu *cu = reader->cu;
518817b3 8047 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8048 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8049 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8050 CORE_ADDR baseaddr;
8051 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 8052 dwarf2_psymtab *pst;
3a2b436a 8053 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8054 const char *filename;
0018ea6f 8055
c0ab21c2 8056 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
0018ea6f
DE
8057 return;
8058
8059 gdb_assert (! per_cu->is_debug_types);
8060
c0ab21c2 8061 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 8062
0018ea6f 8063 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8064 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8065 if (filename == NULL)
0018ea6f 8066 filename = "";
0018ea6f
DE
8067
8068 pst = create_partial_symtab (per_cu, filename);
8069
8070 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8071 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 8072
b3b3bada 8073 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
8074
8075 dwarf2_find_base_address (comp_unit_die, cu);
8076
8077 /* Possibly set the default values of LOWPC and HIGHPC from
8078 `DW_AT_ranges'. */
3a2b436a
JK
8079 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8080 &best_highpc, cu, pst);
8081 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8082 {
8083 CORE_ADDR low
8084 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8085 - baseaddr);
8086 CORE_ADDR high
8087 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8088 - baseaddr - 1);
8089 /* Store the contiguous range if it is not empty; it can be
8090 empty for CUs with no code. */
d320c2b5
TT
8091 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8092 low, high, pst);
79748972 8093 }
0018ea6f
DE
8094
8095 /* Check if comp unit has_children.
8096 If so, read the rest of the partial symbols from this comp unit.
8097 If not, there's no more debug_info for this comp unit. */
8098 if (has_children)
8099 {
8100 struct partial_die_info *first_die;
8101 CORE_ADDR lowpc, highpc;
8102
8103 lowpc = ((CORE_ADDR) -1);
8104 highpc = ((CORE_ADDR) 0);
8105
8106 first_die = load_partial_dies (reader, info_ptr, 1);
8107
8108 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8109 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8110
8111 /* If we didn't find a lowpc, set it to highpc to avoid
8112 complaints from `maint check'. */
8113 if (lowpc == ((CORE_ADDR) -1))
8114 lowpc = highpc;
8115
8116 /* If the compilation unit didn't have an explicit address range,
8117 then use the information extracted from its child dies. */
e385593e 8118 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8119 {
8120 best_lowpc = lowpc;
8121 best_highpc = highpc;
8122 }
8123 }
4ae976d1 8124 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8125 best_lowpc + baseaddr)
8126 - baseaddr);
4ae976d1 8127 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8128 best_highpc + baseaddr)
8129 - baseaddr);
0018ea6f 8130
8763cede 8131 end_psymtab_common (objfile, pst);
0018ea6f 8132
ae640021 8133 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8134 {
8135 int i;
ae640021 8136 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8137
8138 /* Fill in 'dependencies' here; we fill in 'users' in a
8139 post-pass. */
8140 pst->number_of_dependencies = len;
a9342b62
TT
8141 pst->dependencies
8142 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8143 for (i = 0; i < len; ++i)
8144 {
8145 pst->dependencies[i]
8146 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8147 }
0018ea6f 8148
ae640021 8149 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8150 }
8151
8152 /* Get the list of files included in the current compilation unit,
8153 and build a psymtab for each of them. */
8154 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8155
b4f54984 8156 if (dwarf_read_debug)
b926417a
TT
8157 fprintf_unfiltered (gdb_stdlog,
8158 "Psymtab for %s unit @%s: %s - %s"
8159 ", %d global, %d static syms\n",
8160 per_cu->is_debug_types ? "type" : "comp",
8161 sect_offset_str (per_cu->sect_off),
8162 paddress (gdbarch, pst->text_low (objfile)),
8163 paddress (gdbarch, pst->text_high (objfile)),
8164 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8165}
8166
8167/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8168 Process compilation unit THIS_CU for a psymtab. */
8169
8170static void
8171process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8172 int want_partial_unit,
8173 enum language pretend_language)
0018ea6f
DE
8174{
8175 /* If this compilation unit was already read in, free the
8176 cached copy in order to read it in again. This is
8177 necessary because we skipped some symbols when we first
8178 read in the compilation unit (see load_partial_dies).
8179 This problem could be avoided, but the benefit is unclear. */
8180 if (this_cu->cu != NULL)
8181 free_one_cached_comp_unit (this_cu);
8182
c0ab21c2
TT
8183 cutu_reader reader (this_cu, NULL, 0, 0, false);
8184
8185 if (reader.dummy_p)
f1902523 8186 {
c0ab21c2 8187 /* Nothing. */
f1902523 8188 }
c0ab21c2
TT
8189 else if (this_cu->is_debug_types)
8190 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
8191 reader.has_children);
8192 else
8193 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
8194 reader.comp_unit_die,
8195 reader.has_children,
8196 want_partial_unit,
8197 pretend_language);
0018ea6f
DE
8198
8199 /* Age out any secondary CUs. */
ed2dc618 8200 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8201}
f4dc4d17
DE
8202
8203/* Reader function for build_type_psymtabs. */
8204
8205static void
8206build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8207 const gdb_byte *info_ptr,
f4dc4d17 8208 struct die_info *type_unit_die,
c0ab21c2 8209 int has_children)
f4dc4d17 8210{
ed2dc618 8211 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8212 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8213 struct objfile *objfile = dwarf2_per_objfile->objfile;
8214 struct dwarf2_cu *cu = reader->cu;
8215 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8216 struct signatured_type *sig_type;
f4dc4d17
DE
8217 struct type_unit_group *tu_group;
8218 struct attribute *attr;
8219 struct partial_die_info *first_die;
8220 CORE_ADDR lowpc, highpc;
891813be 8221 dwarf2_psymtab *pst;
f4dc4d17 8222
0186c6a7
DE
8223 gdb_assert (per_cu->is_debug_types);
8224 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8225
8226 if (! has_children)
8227 return;
8228
8229 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8230 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8231
df07e2c7 8232 if (tu_group->tus == nullptr)
a8b3b8e9 8233 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8234 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8235
8236 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 8237 pst = create_partial_symtab (per_cu, "");
6d94535f 8238 pst->anonymous = true;
f4dc4d17
DE
8239
8240 first_die = load_partial_dies (reader, info_ptr, 1);
8241
8242 lowpc = (CORE_ADDR) -1;
8243 highpc = (CORE_ADDR) 0;
8244 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8245
8763cede 8246 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8247}
8248
73051182
DE
8249/* Struct used to sort TUs by their abbreviation table offset. */
8250
8251struct tu_abbrev_offset
8252{
b2bdb8cf
SM
8253 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8254 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8255 {}
8256
8257 signatured_type *sig_type;
73051182
DE
8258 sect_offset abbrev_offset;
8259};
8260
484cf504 8261/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8262
484cf504
TT
8263static bool
8264sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8265 const struct tu_abbrev_offset &b)
73051182 8266{
484cf504 8267 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8268}
8269
8270/* Efficiently read all the type units.
8271 This does the bulk of the work for build_type_psymtabs.
8272
8273 The efficiency is because we sort TUs by the abbrev table they use and
8274 only read each abbrev table once. In one program there are 200K TUs
8275 sharing 8K abbrev tables.
8276
8277 The main purpose of this function is to support building the
8278 dwarf2_per_objfile->type_unit_groups table.
8279 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8280 can collapse the search space by grouping them by stmt_list.
8281 The savings can be significant, in the same program from above the 200K TUs
8282 share 8K stmt_list tables.
8283
8284 FUNC is expected to call get_type_unit_group, which will create the
8285 struct type_unit_group if necessary and add it to
8286 dwarf2_per_objfile->type_unit_groups. */
8287
8288static void
ed2dc618 8289build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8290{
73051182 8291 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8292 abbrev_table_up abbrev_table;
73051182 8293 sect_offset abbrev_offset;
73051182
DE
8294
8295 /* It's up to the caller to not call us multiple times. */
8296 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8297
b2bdb8cf 8298 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8299 return;
8300
8301 /* TUs typically share abbrev tables, and there can be way more TUs than
8302 abbrev tables. Sort by abbrev table to reduce the number of times we
8303 read each abbrev table in.
8304 Alternatives are to punt or to maintain a cache of abbrev tables.
8305 This is simpler and efficient enough for now.
8306
8307 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8308 symtab to use). Typically TUs with the same abbrev offset have the same
8309 stmt_list value too so in practice this should work well.
8310
8311 The basic algorithm here is:
8312
8313 sort TUs by abbrev table
8314 for each TU with same abbrev table:
8315 read abbrev table if first user
8316 read TU top level DIE
8317 [IWBN if DWO skeletons had DW_AT_stmt_list]
8318 call FUNC */
8319
b4f54984 8320 if (dwarf_read_debug)
73051182
DE
8321 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8322
8323 /* Sort in a separate table to maintain the order of all_type_units
8324 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8325 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8326 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8327
8328 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8329 sorted_by_abbrev.emplace_back
8330 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8331 sig_type->per_cu.section,
8332 sig_type->per_cu.sect_off));
73051182 8333
484cf504
TT
8334 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8335 sort_tu_by_abbrev_offset);
73051182 8336
9c541725 8337 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8338
b2bdb8cf 8339 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8340 {
73051182
DE
8341 /* Switch to the next abbrev table if necessary. */
8342 if (abbrev_table == NULL
b2bdb8cf 8343 || tu.abbrev_offset != abbrev_offset)
73051182 8344 {
b2bdb8cf 8345 abbrev_offset = tu.abbrev_offset;
73051182 8346 abbrev_table =
ed2dc618
SM
8347 abbrev_table_read_table (dwarf2_per_objfile,
8348 &dwarf2_per_objfile->abbrev,
73051182
DE
8349 abbrev_offset);
8350 ++tu_stats->nr_uniq_abbrev_tables;
8351 }
8352
c0ab21c2
TT
8353 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8354 0, 0, false);
8355 if (!reader.dummy_p)
8356 build_type_psymtabs_reader (&reader, reader.info_ptr,
8357 reader.comp_unit_die,
8358 reader.has_children);
73051182 8359 }
6aa5f3a6 8360}
73051182 8361
6aa5f3a6
DE
8362/* Print collected type unit statistics. */
8363
8364static void
ed2dc618 8365print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8366{
8367 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8368
8369 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8370 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8371 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8372 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8373 tu_stats->nr_uniq_abbrev_tables);
8374 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8375 tu_stats->nr_symtabs);
8376 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8377 tu_stats->nr_symtab_sharers);
8378 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8379 tu_stats->nr_stmt_less_type_units);
8380 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8381 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8382}
8383
f4dc4d17
DE
8384/* Traversal function for build_type_psymtabs. */
8385
8386static int
8387build_type_psymtab_dependencies (void **slot, void *info)
8388{
ed2dc618
SM
8389 struct dwarf2_per_objfile *dwarf2_per_objfile
8390 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8391 struct objfile *objfile = dwarf2_per_objfile->objfile;
8392 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8393 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 8394 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 8395 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8396 int i;
8397
8398 gdb_assert (len > 0);
0186c6a7 8399 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8400
8401 pst->number_of_dependencies = len;
a9342b62 8402 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8403 for (i = 0; i < len; ++i)
f4dc4d17 8404 {
df07e2c7 8405 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8406 gdb_assert (iter->per_cu.is_debug_types);
8407 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8408 iter->type_unit_group = tu_group;
f4dc4d17
DE
8409 }
8410
df07e2c7
AB
8411 delete tu_group->tus;
8412 tu_group->tus = nullptr;
348e048f
DE
8413
8414 return 1;
8415}
8416
8417/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8418 Build partial symbol tables for the .debug_types comp-units. */
8419
8420static void
ed2dc618 8421build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8422{
ed2dc618 8423 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8424 return;
8425
ed2dc618 8426 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8427}
f4dc4d17 8428
6aa5f3a6
DE
8429/* Traversal function for process_skeletonless_type_unit.
8430 Read a TU in a DWO file and build partial symbols for it. */
8431
8432static int
8433process_skeletonless_type_unit (void **slot, void *info)
8434{
8435 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8436 struct dwarf2_per_objfile *dwarf2_per_objfile
8437 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8438 struct signatured_type find_entry, *entry;
8439
8440 /* If this TU doesn't exist in the global table, add it and read it in. */
8441
8442 if (dwarf2_per_objfile->signatured_types == NULL)
8443 {
8444 dwarf2_per_objfile->signatured_types
ed2dc618 8445 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8446 }
8447
8448 find_entry.signature = dwo_unit->signature;
8449 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8450 INSERT);
8451 /* If we've already seen this type there's nothing to do. What's happening
8452 is we're doing our own version of comdat-folding here. */
8453 if (*slot != NULL)
8454 return 1;
8455
8456 /* This does the job that create_all_type_units would have done for
8457 this TU. */
ed2dc618
SM
8458 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8459 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8460 *slot = entry;
8461
8462 /* This does the job that build_type_psymtabs_1 would have done. */
c0ab21c2
TT
8463 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8464 if (!reader.dummy_p)
8465 build_type_psymtabs_reader (&reader, reader.info_ptr,
8466 reader.comp_unit_die, reader.has_children);
6aa5f3a6
DE
8467
8468 return 1;
8469}
8470
8471/* Traversal function for process_skeletonless_type_units. */
8472
8473static int
8474process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8475{
8476 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8477
8478 if (dwo_file->tus != NULL)
8479 {
8480 htab_traverse_noresize (dwo_file->tus,
8481 process_skeletonless_type_unit, info);
8482 }
8483
8484 return 1;
8485}
8486
8487/* Scan all TUs of DWO files, verifying we've processed them.
8488 This is needed in case a TU was emitted without its skeleton.
8489 Note: This can't be done until we know what all the DWO files are. */
8490
8491static void
ed2dc618 8492process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8493{
8494 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8495 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8496 && dwarf2_per_objfile->dwo_files != NULL)
8497 {
51ac9db5 8498 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8499 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8500 dwarf2_per_objfile);
6aa5f3a6 8501 }
348e048f
DE
8502}
8503
ed2dc618 8504/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8505
8506static void
ed2dc618 8507set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8508{
b76e467d 8509 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8510 {
891813be 8511 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8512
36586728
TT
8513 if (pst == NULL)
8514 continue;
8515
b76e467d 8516 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8517 {
8518 /* Set the 'user' field only if it is not already set. */
8519 if (pst->dependencies[j]->user == NULL)
8520 pst->dependencies[j]->user = pst;
8521 }
8522 }
8523}
8524
93311388
DE
8525/* Build the partial symbol table by doing a quick pass through the
8526 .debug_info and .debug_abbrev sections. */
72bf9492 8527
93311388 8528static void
ed2dc618 8529dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8530{
ed2dc618 8531 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8532
b4f54984 8533 if (dwarf_read_debug)
45cfd468
DE
8534 {
8535 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8536 objfile_name (objfile));
45cfd468
DE
8537 }
8538
98bfdba5
PA
8539 dwarf2_per_objfile->reading_partial_symbols = 1;
8540
be391dca 8541 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8542
93311388
DE
8543 /* Any cached compilation units will be linked by the per-objfile
8544 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8545 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8546
ed2dc618 8547 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8548
ed2dc618 8549 create_all_comp_units (dwarf2_per_objfile);
c906108c 8550
60606b2c
TT
8551 /* Create a temporary address map on a temporary obstack. We later
8552 copy this to the final obstack. */
8268c778 8553 auto_obstack temp_obstack;
791afaa2
TT
8554
8555 scoped_restore save_psymtabs_addrmap
d320c2b5 8556 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8557 addrmap_create_mutable (&temp_obstack));
72bf9492 8558
b76e467d
SM
8559 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8560 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8561
6aa5f3a6 8562 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8563 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8564
8565 /* Now that all TUs have been processed we can fill in the dependencies. */
8566 if (dwarf2_per_objfile->type_unit_groups != NULL)
8567 {
8568 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8569 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8570 }
8571
b4f54984 8572 if (dwarf_read_debug)
ed2dc618 8573 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8574
ed2dc618 8575 set_partial_user (dwarf2_per_objfile);
95554aad 8576
d320c2b5
TT
8577 objfile->partial_symtabs->psymtabs_addrmap
8578 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8579 objfile->partial_symtabs->obstack ());
791afaa2
TT
8580 /* At this point we want to keep the address map. */
8581 save_psymtabs_addrmap.release ();
ff013f42 8582
b4f54984 8583 if (dwarf_read_debug)
45cfd468 8584 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8585 objfile_name (objfile));
ae038cb0
DJ
8586}
8587
dee91e82
DE
8588/* Load the partial DIEs for a secondary CU into memory.
8589 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8590
dee91e82
DE
8591static void
8592load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8593{
c0ab21c2
TT
8594 cutu_reader reader (this_cu, NULL, 1, 1, false);
8595
8596 if (!reader.dummy_p)
8597 {
8598 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8599 language_minimal);
8600
8601 /* Check if comp unit has_children.
8602 If so, read the rest of the partial symbols from this comp unit.
8603 If not, there's no more debug_info for this comp unit. */
8604 if (reader.has_children)
8605 load_partial_dies (&reader, reader.info_ptr, 0);
8606 }
ae038cb0
DJ
8607}
8608
ae038cb0 8609static void
ed2dc618 8610read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8611 struct dwarf2_section_info *section,
f1902523 8612 struct dwarf2_section_info *abbrev_section,
b76e467d 8613 unsigned int is_dwz)
ae038cb0 8614{
d521ce57 8615 const gdb_byte *info_ptr;
ed2dc618 8616 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8617
b4f54984 8618 if (dwarf_read_debug)
bf6af496 8619 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8620 get_section_name (section),
8621 get_section_file_name (section));
bf6af496 8622
36586728 8623 dwarf2_read_section (objfile, section);
ae038cb0 8624
36586728 8625 info_ptr = section->buffer;
6e70227d 8626
36586728 8627 while (info_ptr < section->buffer + section->size)
ae038cb0 8628 {
ae038cb0 8629 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8630
9c541725 8631 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8632
f1902523 8633 comp_unit_head cu_header;
ed2dc618
SM
8634 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8635 abbrev_section, info_ptr,
8636 rcuh_kind::COMPILE);
ae038cb0
DJ
8637
8638 /* Save the compilation unit for later lookup. */
f1902523
JK
8639 if (cu_header.unit_type != DW_UT_type)
8640 {
8641 this_cu = XOBNEW (&objfile->objfile_obstack,
8642 struct dwarf2_per_cu_data);
8643 memset (this_cu, 0, sizeof (*this_cu));
8644 }
8645 else
8646 {
8647 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8648 struct signatured_type);
8649 memset (sig_type, 0, sizeof (*sig_type));
8650 sig_type->signature = cu_header.signature;
8651 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8652 this_cu = &sig_type->per_cu;
8653 }
8654 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8655 this_cu->sect_off = sect_off;
f1902523 8656 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8657 this_cu->is_dwz = is_dwz;
e3b94546 8658 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8659 this_cu->section = section;
ae038cb0 8660
b76e467d 8661 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8662
8663 info_ptr = info_ptr + this_cu->length;
8664 }
36586728
TT
8665}
8666
8667/* Create a list of all compilation units in OBJFILE.
8668 This is only done for -readnow and building partial symtabs. */
8669
8670static void
ed2dc618 8671create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8672{
b76e467d 8673 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8674 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8675 &dwarf2_per_objfile->abbrev, 0);
36586728 8676
b76e467d 8677 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8678 if (dwz != NULL)
ed2dc618 8679 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8680 1);
c906108c
SS
8681}
8682
5734ee8b 8683/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8684 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8685 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8686 DW_AT_ranges). See the comments of add_partial_subprogram on how
8687 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8688
72bf9492
DJ
8689static void
8690scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8691 CORE_ADDR *highpc, int set_addrmap,
8692 struct dwarf2_cu *cu)
c906108c 8693{
72bf9492 8694 struct partial_die_info *pdi;
c906108c 8695
91c24f0a
DC
8696 /* Now, march along the PDI's, descending into ones which have
8697 interesting children but skipping the children of the other ones,
8698 until we reach the end of the compilation unit. */
c906108c 8699
72bf9492 8700 pdi = first_die;
91c24f0a 8701
72bf9492
DJ
8702 while (pdi != NULL)
8703 {
52356b79 8704 pdi->fixup (cu);
c906108c 8705
f55ee35c 8706 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8707 children, so we need to look at them. Ditto for anonymous
8708 enums. */
933c6fe4 8709
72bf9492 8710 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8711 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8712 || pdi->tag == DW_TAG_imported_unit
8713 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8714 {
72bf9492 8715 switch (pdi->tag)
c906108c
SS
8716 {
8717 case DW_TAG_subprogram:
b1dc1806 8718 case DW_TAG_inlined_subroutine:
cdc07690 8719 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8720 break;
72929c62 8721 case DW_TAG_constant:
c906108c
SS
8722 case DW_TAG_variable:
8723 case DW_TAG_typedef:
91c24f0a 8724 case DW_TAG_union_type:
72bf9492 8725 if (!pdi->is_declaration)
63d06c5c 8726 {
72bf9492 8727 add_partial_symbol (pdi, cu);
63d06c5c
DC
8728 }
8729 break;
c906108c 8730 case DW_TAG_class_type:
680b30c7 8731 case DW_TAG_interface_type:
c906108c 8732 case DW_TAG_structure_type:
72bf9492 8733 if (!pdi->is_declaration)
c906108c 8734 {
72bf9492 8735 add_partial_symbol (pdi, cu);
c906108c 8736 }
b7fee5a3
KS
8737 if ((cu->language == language_rust
8738 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8739 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8740 set_addrmap, cu);
c906108c 8741 break;
91c24f0a 8742 case DW_TAG_enumeration_type:
72bf9492
DJ
8743 if (!pdi->is_declaration)
8744 add_partial_enumeration (pdi, cu);
c906108c
SS
8745 break;
8746 case DW_TAG_base_type:
a02abb62 8747 case DW_TAG_subrange_type:
c906108c 8748 /* File scope base type definitions are added to the partial
c5aa993b 8749 symbol table. */
72bf9492 8750 add_partial_symbol (pdi, cu);
c906108c 8751 break;
d9fa45fe 8752 case DW_TAG_namespace:
cdc07690 8753 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8754 break;
5d7cb8df 8755 case DW_TAG_module:
59c35742
AB
8756 if (!pdi->is_declaration)
8757 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8758 break;
95554aad
TT
8759 case DW_TAG_imported_unit:
8760 {
8761 struct dwarf2_per_cu_data *per_cu;
8762
f4dc4d17
DE
8763 /* For now we don't handle imported units in type units. */
8764 if (cu->per_cu->is_debug_types)
8765 {
8766 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8767 " supported in type units [in module %s]"),
518817b3 8768 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8769 }
8770
e3b94546
SM
8771 per_cu = dwarf2_find_containing_comp_unit
8772 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8773 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8774
8775 /* Go read the partial unit, if needed. */
8776 if (per_cu->v.psymtab == NULL)
b93601f3 8777 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8778
ae640021 8779 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8780 }
8781 break;
74921315
KS
8782 case DW_TAG_imported_declaration:
8783 add_partial_symbol (pdi, cu);
8784 break;
c906108c
SS
8785 default:
8786 break;
8787 }
8788 }
8789
72bf9492
DJ
8790 /* If the die has a sibling, skip to the sibling. */
8791
8792 pdi = pdi->die_sibling;
8793 }
8794}
8795
8796/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8797
72bf9492 8798 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8799 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8800 Enumerators are an exception; they use the scope of their parent
8801 enumeration type, i.e. the name of the enumeration type is not
8802 prepended to the enumerator.
91c24f0a 8803
72bf9492
DJ
8804 There are two complexities. One is DW_AT_specification; in this
8805 case "parent" means the parent of the target of the specification,
8806 instead of the direct parent of the DIE. The other is compilers
8807 which do not emit DW_TAG_namespace; in this case we try to guess
8808 the fully qualified name of structure types from their members'
8809 linkage names. This must be done using the DIE's children rather
8810 than the children of any DW_AT_specification target. We only need
8811 to do this for structures at the top level, i.e. if the target of
8812 any DW_AT_specification (if any; otherwise the DIE itself) does not
8813 have a parent. */
8814
8815/* Compute the scope prefix associated with PDI's parent, in
8816 compilation unit CU. The result will be allocated on CU's
8817 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8818 field. NULL is returned if no prefix is necessary. */
15d034d0 8819static const char *
72bf9492
DJ
8820partial_die_parent_scope (struct partial_die_info *pdi,
8821 struct dwarf2_cu *cu)
8822{
15d034d0 8823 const char *grandparent_scope;
72bf9492 8824 struct partial_die_info *parent, *real_pdi;
91c24f0a 8825
72bf9492
DJ
8826 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8827 then this means the parent of the specification DIE. */
8828
8829 real_pdi = pdi;
72bf9492 8830 while (real_pdi->has_specification)
fb816e8b 8831 {
122cf0f2
AB
8832 auto res = find_partial_die (real_pdi->spec_offset,
8833 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8834 real_pdi = res.pdi;
8835 cu = res.cu;
8836 }
72bf9492
DJ
8837
8838 parent = real_pdi->die_parent;
8839 if (parent == NULL)
8840 return NULL;
8841
8842 if (parent->scope_set)
8843 return parent->scope;
8844
52356b79 8845 parent->fixup (cu);
72bf9492 8846
10b3939b 8847 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8848
acebe513
UW
8849 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8850 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8851 Work around this problem here. */
8852 if (cu->language == language_cplus
6e70227d 8853 && parent->tag == DW_TAG_namespace
acebe513
UW
8854 && strcmp (parent->name, "::") == 0
8855 && grandparent_scope == NULL)
8856 {
8857 parent->scope = NULL;
8858 parent->scope_set = 1;
8859 return NULL;
8860 }
8861
0a4b0913 8862 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8863 if (pdi->tag == DW_TAG_enumerator)
8864 /* Enumerators should not get the name of the enumeration as a prefix. */
8865 parent->scope = grandparent_scope;
8866 else if (parent->tag == DW_TAG_namespace
f55ee35c 8867 || parent->tag == DW_TAG_module
72bf9492
DJ
8868 || parent->tag == DW_TAG_structure_type
8869 || parent->tag == DW_TAG_class_type
680b30c7 8870 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8871 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8872 || parent->tag == DW_TAG_enumeration_type
8873 || (cu->language == language_fortran
8874 && parent->tag == DW_TAG_subprogram
8875 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8876 {
8877 if (grandparent_scope == NULL)
8878 parent->scope = parent->name;
8879 else
3e43a32a
MS
8880 parent->scope = typename_concat (&cu->comp_unit_obstack,
8881 grandparent_scope,
f55ee35c 8882 parent->name, 0, cu);
72bf9492 8883 }
72bf9492
DJ
8884 else
8885 {
8886 /* FIXME drow/2004-04-01: What should we be doing with
8887 function-local names? For partial symbols, we should probably be
8888 ignoring them. */
fa9c3fa0
TT
8889 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8890 dwarf_tag_name (parent->tag),
8891 sect_offset_str (pdi->sect_off));
72bf9492 8892 parent->scope = grandparent_scope;
c906108c
SS
8893 }
8894
72bf9492
DJ
8895 parent->scope_set = 1;
8896 return parent->scope;
8897}
8898
8899/* Return the fully scoped name associated with PDI, from compilation unit
8900 CU. The result will be allocated with malloc. */
4568ecf9 8901
43816ebc 8902static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8903partial_die_full_name (struct partial_die_info *pdi,
8904 struct dwarf2_cu *cu)
8905{
15d034d0 8906 const char *parent_scope;
72bf9492 8907
98bfdba5
PA
8908 /* If this is a template instantiation, we can not work out the
8909 template arguments from partial DIEs. So, unfortunately, we have
8910 to go through the full DIEs. At least any work we do building
8911 types here will be reused if full symbols are loaded later. */
8912 if (pdi->has_template_arguments)
8913 {
52356b79 8914 pdi->fixup (cu);
98bfdba5
PA
8915
8916 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8917 {
8918 struct die_info *die;
8919 struct attribute attr;
8920 struct dwarf2_cu *ref_cu = cu;
8921
b64f50a1 8922 /* DW_FORM_ref_addr is using section offset. */
b4069958 8923 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8924 attr.form = DW_FORM_ref_addr;
9c541725 8925 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8926 die = follow_die_ref (NULL, &attr, &ref_cu);
8927
43816ebc 8928 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8929 }
8930 }
8931
72bf9492
DJ
8932 parent_scope = partial_die_parent_scope (pdi, cu);
8933 if (parent_scope == NULL)
8934 return NULL;
8935 else
43816ebc
TT
8936 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8937 pdi->name, 0, cu));
c906108c
SS
8938}
8939
8940static void
72bf9492 8941add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8942{
518817b3
SM
8943 struct dwarf2_per_objfile *dwarf2_per_objfile
8944 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8945 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8946 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8947 CORE_ADDR addr = 0;
15d034d0 8948 const char *actual_name = NULL;
e142c38c
DJ
8949 CORE_ADDR baseaddr;
8950
b3b3bada 8951 baseaddr = objfile->text_section_offset ();
c906108c 8952
43816ebc
TT
8953 gdb::unique_xmalloc_ptr<char> built_actual_name
8954 = partial_die_full_name (pdi, cu);
15d034d0 8955 if (built_actual_name != NULL)
43816ebc 8956 actual_name = built_actual_name.get ();
63d06c5c 8957
72bf9492
DJ
8958 if (actual_name == NULL)
8959 actual_name = pdi->name;
8960
c906108c
SS
8961 switch (pdi->tag)
8962 {
b1dc1806 8963 case DW_TAG_inlined_subroutine:
c906108c 8964 case DW_TAG_subprogram:
79748972
TT
8965 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8966 - baseaddr);
0a4b0913
AB
8967 if (pdi->is_external
8968 || cu->language == language_ada
8969 || (cu->language == language_fortran
8970 && pdi->die_parent != NULL
8971 && pdi->die_parent->tag == DW_TAG_subprogram))
8972 {
8973 /* Normally, only "external" DIEs are part of the global scope.
8974 But in Ada and Fortran, we want to be able to access nested
8975 procedures globally. So all Ada and Fortran subprograms are
8976 stored in the global scope. */
31edb802 8977 add_psymbol_to_list (actual_name,
15d034d0 8978 built_actual_name != NULL,
f47fb265 8979 VAR_DOMAIN, LOC_BLOCK,
79748972 8980 SECT_OFF_TEXT (objfile),
75aedd27 8981 psymbol_placement::GLOBAL,
79748972
TT
8982 addr,
8983 cu->language, objfile);
c906108c
SS
8984 }
8985 else
8986 {
31edb802 8987 add_psymbol_to_list (actual_name,
15d034d0 8988 built_actual_name != NULL,
f47fb265 8989 VAR_DOMAIN, LOC_BLOCK,
79748972 8990 SECT_OFF_TEXT (objfile),
75aedd27 8991 psymbol_placement::STATIC,
1762568f 8992 addr, cu->language, objfile);
c906108c 8993 }
0c1b455e
TT
8994
8995 if (pdi->main_subprogram && actual_name != NULL)
8996 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8997 break;
72929c62 8998 case DW_TAG_constant:
31edb802 8999 add_psymbol_to_list (actual_name,
75aedd27
TT
9000 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9001 -1, (pdi->is_external
9002 ? psymbol_placement::GLOBAL
9003 : psymbol_placement::STATIC),
9004 0, cu->language, objfile);
72929c62 9005 break;
c906108c 9006 case DW_TAG_variable:
95554aad
TT
9007 if (pdi->d.locdesc)
9008 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9009
95554aad 9010 if (pdi->d.locdesc
caac4577
JG
9011 && addr == 0
9012 && !dwarf2_per_objfile->has_section_at_zero)
9013 {
9014 /* A global or static variable may also have been stripped
9015 out by the linker if unused, in which case its address
9016 will be nullified; do not add such variables into partial
9017 symbol table then. */
9018 }
9019 else if (pdi->is_external)
c906108c
SS
9020 {
9021 /* Global Variable.
9022 Don't enter into the minimal symbol tables as there is
9023 a minimal symbol table entry from the ELF symbols already.
9024 Enter into partial symbol table if it has a location
9025 descriptor or a type.
9026 If the location descriptor is missing, new_symbol will create
9027 a LOC_UNRESOLVED symbol, the address of the variable will then
9028 be determined from the minimal symbol table whenever the variable
9029 is referenced.
9030 The address for the partial symbol table entry is not
9031 used by GDB, but it comes in handy for debugging partial symbol
9032 table building. */
9033
95554aad 9034 if (pdi->d.locdesc || pdi->has_type)
31edb802 9035 add_psymbol_to_list (actual_name,
15d034d0 9036 built_actual_name != NULL,
f47fb265 9037 VAR_DOMAIN, LOC_STATIC,
79748972 9038 SECT_OFF_TEXT (objfile),
75aedd27 9039 psymbol_placement::GLOBAL,
79748972 9040 addr, cu->language, objfile);
c906108c
SS
9041 }
9042 else
9043 {
ff908ebf
AW
9044 int has_loc = pdi->d.locdesc != NULL;
9045
9046 /* Static Variable. Skip symbols whose value we cannot know (those
9047 without location descriptors or constant values). */
9048 if (!has_loc && !pdi->has_const_value)
43816ebc 9049 return;
ff908ebf 9050
31edb802 9051 add_psymbol_to_list (actual_name,
15d034d0 9052 built_actual_name != NULL,
f47fb265 9053 VAR_DOMAIN, LOC_STATIC,
79748972 9054 SECT_OFF_TEXT (objfile),
75aedd27 9055 psymbol_placement::STATIC,
79748972 9056 has_loc ? addr : 0,
f47fb265 9057 cu->language, objfile);
c906108c
SS
9058 }
9059 break;
9060 case DW_TAG_typedef:
9061 case DW_TAG_base_type:
a02abb62 9062 case DW_TAG_subrange_type:
31edb802 9063 add_psymbol_to_list (actual_name,
15d034d0 9064 built_actual_name != NULL,
79748972 9065 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9066 psymbol_placement::STATIC,
1762568f 9067 0, cu->language, objfile);
c906108c 9068 break;
74921315 9069 case DW_TAG_imported_declaration:
72bf9492 9070 case DW_TAG_namespace:
31edb802 9071 add_psymbol_to_list (actual_name,
15d034d0 9072 built_actual_name != NULL,
79748972 9073 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9074 psymbol_placement::GLOBAL,
1762568f 9075 0, cu->language, objfile);
72bf9492 9076 break;
530e8392 9077 case DW_TAG_module:
a5fd13a9
BH
9078 /* With Fortran 77 there might be a "BLOCK DATA" module
9079 available without any name. If so, we skip the module as it
9080 doesn't bring any value. */
9081 if (actual_name != nullptr)
31edb802 9082 add_psymbol_to_list (actual_name,
a5fd13a9
BH
9083 built_actual_name != NULL,
9084 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9085 psymbol_placement::GLOBAL,
9086 0, cu->language, objfile);
530e8392 9087 break;
c906108c 9088 case DW_TAG_class_type:
680b30c7 9089 case DW_TAG_interface_type:
c906108c
SS
9090 case DW_TAG_structure_type:
9091 case DW_TAG_union_type:
9092 case DW_TAG_enumeration_type:
fa4028e9
JB
9093 /* Skip external references. The DWARF standard says in the section
9094 about "Structure, Union, and Class Type Entries": "An incomplete
9095 structure, union or class type is represented by a structure,
9096 union or class entry that does not have a byte size attribute
9097 and that has a DW_AT_declaration attribute." */
9098 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 9099 return;
fa4028e9 9100
63d06c5c
DC
9101 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9102 static vs. global. */
31edb802 9103 add_psymbol_to_list (actual_name,
15d034d0 9104 built_actual_name != NULL,
79748972 9105 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9106 cu->language == language_cplus
75aedd27
TT
9107 ? psymbol_placement::GLOBAL
9108 : psymbol_placement::STATIC,
1762568f 9109 0, cu->language, objfile);
c906108c 9110
c906108c
SS
9111 break;
9112 case DW_TAG_enumerator:
31edb802 9113 add_psymbol_to_list (actual_name,
15d034d0 9114 built_actual_name != NULL,
79748972 9115 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9116 cu->language == language_cplus
75aedd27
TT
9117 ? psymbol_placement::GLOBAL
9118 : psymbol_placement::STATIC,
1762568f 9119 0, cu->language, objfile);
c906108c
SS
9120 break;
9121 default:
9122 break;
9123 }
9124}
9125
5c4e30ca
DC
9126/* Read a partial die corresponding to a namespace; also, add a symbol
9127 corresponding to that namespace to the symbol table. NAMESPACE is
9128 the name of the enclosing namespace. */
91c24f0a 9129
72bf9492
DJ
9130static void
9131add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9132 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9133 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9134{
72bf9492 9135 /* Add a symbol for the namespace. */
e7c27a73 9136
72bf9492 9137 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9138
9139 /* Now scan partial symbols in that namespace. */
9140
91c24f0a 9141 if (pdi->has_children)
cdc07690 9142 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9143}
9144
5d7cb8df
JK
9145/* Read a partial die corresponding to a Fortran module. */
9146
9147static void
9148add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9149 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9150{
530e8392
KB
9151 /* Add a symbol for the namespace. */
9152
9153 add_partial_symbol (pdi, cu);
9154
f55ee35c 9155 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9156
9157 if (pdi->has_children)
cdc07690 9158 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9159}
9160
b1dc1806
XR
9161/* Read a partial die corresponding to a subprogram or an inlined
9162 subprogram and create a partial symbol for that subprogram.
9163 When the CU language allows it, this routine also defines a partial
9164 symbol for each nested subprogram that this subprogram contains.
9165 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9166 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9167
cdc07690
YQ
9168 PDI may also be a lexical block, in which case we simply search
9169 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9170 Again, this is only performed when the CU language allows this
9171 type of definitions. */
9172
9173static void
9174add_partial_subprogram (struct partial_die_info *pdi,
9175 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9176 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9177{
b1dc1806 9178 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9179 {
9180 if (pdi->has_pc_info)
9181 {
9182 if (pdi->lowpc < *lowpc)
9183 *lowpc = pdi->lowpc;
9184 if (pdi->highpc > *highpc)
9185 *highpc = pdi->highpc;
cdc07690 9186 if (set_addrmap)
5734ee8b 9187 {
518817b3 9188 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9189 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9190 CORE_ADDR baseaddr;
b926417a
TT
9191 CORE_ADDR this_highpc;
9192 CORE_ADDR this_lowpc;
5734ee8b 9193
b3b3bada 9194 baseaddr = objfile->text_section_offset ();
b926417a
TT
9195 this_lowpc
9196 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9197 pdi->lowpc + baseaddr)
9198 - baseaddr);
9199 this_highpc
9200 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9201 pdi->highpc + baseaddr)
9202 - baseaddr);
d320c2b5 9203 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9204 this_lowpc, this_highpc - 1,
9291a0cd 9205 cu->per_cu->v.psymtab);
5734ee8b 9206 }
481860b3
GB
9207 }
9208
9209 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9210 {
bc30ff58 9211 if (!pdi->is_declaration)
e8d05480
JB
9212 /* Ignore subprogram DIEs that do not have a name, they are
9213 illegal. Do not emit a complaint at this point, we will
9214 do so when we convert this psymtab into a symtab. */
9215 if (pdi->name)
9216 add_partial_symbol (pdi, cu);
bc30ff58
JB
9217 }
9218 }
6e70227d 9219
bc30ff58
JB
9220 if (! pdi->has_children)
9221 return;
9222
0a4b0913 9223 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9224 {
9225 pdi = pdi->die_child;
9226 while (pdi != NULL)
9227 {
52356b79 9228 pdi->fixup (cu);
bc30ff58 9229 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9230 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9231 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9232 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9233 pdi = pdi->die_sibling;
9234 }
9235 }
9236}
9237
91c24f0a
DC
9238/* Read a partial die corresponding to an enumeration type. */
9239
72bf9492
DJ
9240static void
9241add_partial_enumeration (struct partial_die_info *enum_pdi,
9242 struct dwarf2_cu *cu)
91c24f0a 9243{
72bf9492 9244 struct partial_die_info *pdi;
91c24f0a
DC
9245
9246 if (enum_pdi->name != NULL)
72bf9492
DJ
9247 add_partial_symbol (enum_pdi, cu);
9248
9249 pdi = enum_pdi->die_child;
9250 while (pdi)
91c24f0a 9251 {
72bf9492 9252 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9253 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9254 else
72bf9492
DJ
9255 add_partial_symbol (pdi, cu);
9256 pdi = pdi->die_sibling;
91c24f0a 9257 }
91c24f0a
DC
9258}
9259
6caca83c
CC
9260/* Return the initial uleb128 in the die at INFO_PTR. */
9261
9262static unsigned int
d521ce57 9263peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9264{
9265 unsigned int bytes_read;
9266
9267 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9268}
9269
685af9cd
TT
9270/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9271 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9272
4bb7a0a7
DJ
9273 Return the corresponding abbrev, or NULL if the number is zero (indicating
9274 an empty DIE). In either case *BYTES_READ will be set to the length of
9275 the initial number. */
9276
9277static struct abbrev_info *
685af9cd
TT
9278peek_die_abbrev (const die_reader_specs &reader,
9279 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9280{
685af9cd 9281 dwarf2_cu *cu = reader.cu;
518817b3 9282 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9283 unsigned int abbrev_number
9284 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9285
9286 if (abbrev_number == 0)
9287 return NULL;
9288
685af9cd 9289 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9290 if (!abbrev)
9291 {
422b9917 9292 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9293 " at offset %s [in module %s]"),
422b9917 9294 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9295 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9296 }
9297
9298 return abbrev;
9299}
9300
93311388
DE
9301/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9302 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9303 DIE. Any children of the skipped DIEs will also be skipped. */
9304
d521ce57
TT
9305static const gdb_byte *
9306skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9307{
4bb7a0a7
DJ
9308 while (1)
9309 {
685af9cd
TT
9310 unsigned int bytes_read;
9311 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9312
4bb7a0a7
DJ
9313 if (abbrev == NULL)
9314 return info_ptr + bytes_read;
9315 else
dee91e82 9316 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9317 }
9318}
9319
93311388
DE
9320/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9321 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9322 abbrev corresponding to that skipped uleb128 should be passed in
9323 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9324 children. */
9325
d521ce57
TT
9326static const gdb_byte *
9327skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9328 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9329{
9330 unsigned int bytes_read;
9331 struct attribute attr;
dee91e82
DE
9332 bfd *abfd = reader->abfd;
9333 struct dwarf2_cu *cu = reader->cu;
d521ce57 9334 const gdb_byte *buffer = reader->buffer;
f664829e 9335 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9336 unsigned int form, i;
9337
9338 for (i = 0; i < abbrev->num_attrs; i++)
9339 {
9340 /* The only abbrev we care about is DW_AT_sibling. */
9341 if (abbrev->attrs[i].name == DW_AT_sibling)
9342 {
18a8505e
AT
9343 bool ignored;
9344 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9345 &ignored);
4bb7a0a7 9346 if (attr.form == DW_FORM_ref_addr)
b98664d3 9347 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9348 else
b9502d3f 9349 {
9c541725
PA
9350 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9351 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9352
9353 if (sibling_ptr < info_ptr)
b98664d3 9354 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9355 else if (sibling_ptr > reader->buffer_end)
9356 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9357 else
9358 return sibling_ptr;
9359 }
4bb7a0a7
DJ
9360 }
9361
9362 /* If it isn't DW_AT_sibling, skip this attribute. */
9363 form = abbrev->attrs[i].form;
9364 skip_attribute:
9365 switch (form)
9366 {
4bb7a0a7 9367 case DW_FORM_ref_addr:
ae411497
TT
9368 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9369 and later it is offset sized. */
9370 if (cu->header.version == 2)
9371 info_ptr += cu->header.addr_size;
9372 else
9373 info_ptr += cu->header.offset_size;
9374 break;
36586728
TT
9375 case DW_FORM_GNU_ref_alt:
9376 info_ptr += cu->header.offset_size;
9377 break;
ae411497 9378 case DW_FORM_addr:
4bb7a0a7
DJ
9379 info_ptr += cu->header.addr_size;
9380 break;
9381 case DW_FORM_data1:
9382 case DW_FORM_ref1:
9383 case DW_FORM_flag:
8fe0f950 9384 case DW_FORM_strx1:
4bb7a0a7
DJ
9385 info_ptr += 1;
9386 break;
2dc7f7b3 9387 case DW_FORM_flag_present:
43988095 9388 case DW_FORM_implicit_const:
2dc7f7b3 9389 break;
4bb7a0a7
DJ
9390 case DW_FORM_data2:
9391 case DW_FORM_ref2:
8fe0f950 9392 case DW_FORM_strx2:
4bb7a0a7
DJ
9393 info_ptr += 2;
9394 break;
8fe0f950
AT
9395 case DW_FORM_strx3:
9396 info_ptr += 3;
9397 break;
4bb7a0a7
DJ
9398 case DW_FORM_data4:
9399 case DW_FORM_ref4:
8fe0f950 9400 case DW_FORM_strx4:
4bb7a0a7
DJ
9401 info_ptr += 4;
9402 break;
9403 case DW_FORM_data8:
9404 case DW_FORM_ref8:
55f1336d 9405 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9406 info_ptr += 8;
9407 break;
0224619f
JK
9408 case DW_FORM_data16:
9409 info_ptr += 16;
9410 break;
4bb7a0a7 9411 case DW_FORM_string:
9b1c24c8 9412 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9413 info_ptr += bytes_read;
9414 break;
2dc7f7b3 9415 case DW_FORM_sec_offset:
4bb7a0a7 9416 case DW_FORM_strp:
36586728 9417 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9418 info_ptr += cu->header.offset_size;
9419 break;
2dc7f7b3 9420 case DW_FORM_exprloc:
4bb7a0a7
DJ
9421 case DW_FORM_block:
9422 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9423 info_ptr += bytes_read;
9424 break;
9425 case DW_FORM_block1:
9426 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9427 break;
9428 case DW_FORM_block2:
9429 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9430 break;
9431 case DW_FORM_block4:
9432 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9433 break;
336d760d 9434 case DW_FORM_addrx:
cf532bd1 9435 case DW_FORM_strx:
4bb7a0a7
DJ
9436 case DW_FORM_sdata:
9437 case DW_FORM_udata:
9438 case DW_FORM_ref_udata:
3019eac3
DE
9439 case DW_FORM_GNU_addr_index:
9440 case DW_FORM_GNU_str_index:
18a8505e 9441 case DW_FORM_rnglistx:
d521ce57 9442 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9443 break;
9444 case DW_FORM_indirect:
9445 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9446 info_ptr += bytes_read;
9447 /* We need to continue parsing from here, so just go back to
9448 the top. */
9449 goto skip_attribute;
9450
9451 default:
3e43a32a
MS
9452 error (_("Dwarf Error: Cannot handle %s "
9453 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9454 dwarf_form_name (form),
9455 bfd_get_filename (abfd));
9456 }
9457 }
9458
9459 if (abbrev->has_children)
dee91e82 9460 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9461 else
9462 return info_ptr;
9463}
9464
93311388 9465/* Locate ORIG_PDI's sibling.
dee91e82 9466 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9467
d521ce57 9468static const gdb_byte *
dee91e82
DE
9469locate_pdi_sibling (const struct die_reader_specs *reader,
9470 struct partial_die_info *orig_pdi,
d521ce57 9471 const gdb_byte *info_ptr)
91c24f0a
DC
9472{
9473 /* Do we know the sibling already? */
72bf9492 9474
91c24f0a
DC
9475 if (orig_pdi->sibling)
9476 return orig_pdi->sibling;
9477
9478 /* Are there any children to deal with? */
9479
9480 if (!orig_pdi->has_children)
9481 return info_ptr;
9482
4bb7a0a7 9483 /* Skip the children the long way. */
91c24f0a 9484
dee91e82 9485 return skip_children (reader, info_ptr);
91c24f0a
DC
9486}
9487
257e7a09 9488/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9489 not NULL. */
c906108c 9490
891813be
TT
9491void
9492dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9493{
ed2dc618
SM
9494 struct dwarf2_per_objfile *dwarf2_per_objfile
9495 = get_dwarf2_per_objfile (objfile);
9496
077cbab2
TT
9497 gdb_assert (!readin);
9498 /* If this psymtab is constructed from a debug-only objfile, the
9499 has_section_at_zero flag will not necessarily be correct. We
9500 can get the correct value for this flag by looking at the data
9501 associated with the (presumably stripped) associated objfile. */
9502 if (objfile->separate_debug_objfile_backlink)
c906108c 9503 {
077cbab2
TT
9504 struct dwarf2_per_objfile *dpo_backlink
9505 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9506
077cbab2
TT
9507 dwarf2_per_objfile->has_section_at_zero
9508 = dpo_backlink->has_section_at_zero;
9509 }
98bfdba5 9510
077cbab2 9511 dwarf2_per_objfile->reading_partial_symbols = 0;
c906108c 9512
077cbab2 9513 psymtab_to_symtab_1 (this);
95554aad 9514
ed2dc618 9515 process_cu_includes (dwarf2_per_objfile);
c906108c 9516}
9cdd5dbd
DE
9517\f
9518/* Reading in full CUs. */
c906108c 9519
10b3939b
DJ
9520/* Add PER_CU to the queue. */
9521
9522static void
95554aad
TT
9523queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9524 enum language pretend_language)
10b3939b
DJ
9525{
9526 struct dwarf2_queue_item *item;
9527
9528 per_cu->queued = 1;
8d749320 9529 item = XNEW (struct dwarf2_queue_item);
10b3939b 9530 item->per_cu = per_cu;
95554aad 9531 item->pretend_language = pretend_language;
10b3939b
DJ
9532 item->next = NULL;
9533
9534 if (dwarf2_queue == NULL)
9535 dwarf2_queue = item;
9536 else
9537 dwarf2_queue_tail->next = item;
9538
9539 dwarf2_queue_tail = item;
9540}
9541
89e63ee4
DE
9542/* If PER_CU is not yet queued, add it to the queue.
9543 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9544 dependency.
0907af0c 9545 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9546 meaning either PER_CU is already queued or it is already loaded.
9547
9548 N.B. There is an invariant here that if a CU is queued then it is loaded.
9549 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9550
9551static int
89e63ee4 9552maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9553 struct dwarf2_per_cu_data *per_cu,
9554 enum language pretend_language)
9555{
9556 /* We may arrive here during partial symbol reading, if we need full
9557 DIEs to process an unusual case (e.g. template arguments). Do
9558 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9559 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9560 {
9561 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9562 return 1;
9563 return 0;
9564 }
9565
9566 /* Mark the dependence relation so that we don't flush PER_CU
9567 too early. */
89e63ee4
DE
9568 if (dependent_cu != NULL)
9569 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9570
9571 /* If it's already on the queue, we have nothing to do. */
9572 if (per_cu->queued)
9573 return 0;
9574
9575 /* If the compilation unit is already loaded, just mark it as
9576 used. */
9577 if (per_cu->cu != NULL)
9578 {
9579 per_cu->cu->last_used = 0;
9580 return 0;
9581 }
9582
9583 /* Add it to the queue. */
9584 queue_comp_unit (per_cu, pretend_language);
9585
9586 return 1;
9587}
9588
10b3939b
DJ
9589/* Process the queue. */
9590
9591static void
ed2dc618 9592process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9593{
9594 struct dwarf2_queue_item *item, *next_item;
9595
b4f54984 9596 if (dwarf_read_debug)
45cfd468
DE
9597 {
9598 fprintf_unfiltered (gdb_stdlog,
9599 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9600 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9601 }
9602
03dd20cc
DJ
9603 /* The queue starts out with one item, but following a DIE reference
9604 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9605 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9606 {
cc12ce38
DE
9607 if ((dwarf2_per_objfile->using_index
9608 ? !item->per_cu->v.quick->compunit_symtab
9609 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9610 /* Skip dummy CUs. */
9611 && item->per_cu->cu != NULL)
f4dc4d17
DE
9612 {
9613 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9614 unsigned int debug_print_threshold;
247f5c4f 9615 char buf[100];
f4dc4d17 9616
247f5c4f 9617 if (per_cu->is_debug_types)
f4dc4d17 9618 {
247f5c4f
DE
9619 struct signatured_type *sig_type =
9620 (struct signatured_type *) per_cu;
9621
9d8780f0 9622 sprintf (buf, "TU %s at offset %s",
73be47f5 9623 hex_string (sig_type->signature),
9d8780f0 9624 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9625 /* There can be 100s of TUs.
9626 Only print them in verbose mode. */
9627 debug_print_threshold = 2;
f4dc4d17 9628 }
247f5c4f 9629 else
73be47f5 9630 {
9d8780f0
SM
9631 sprintf (buf, "CU at offset %s",
9632 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9633 debug_print_threshold = 1;
9634 }
247f5c4f 9635
b4f54984 9636 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9637 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9638
9639 if (per_cu->is_debug_types)
9640 process_full_type_unit (per_cu, item->pretend_language);
9641 else
9642 process_full_comp_unit (per_cu, item->pretend_language);
9643
b4f54984 9644 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9645 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9646 }
10b3939b
DJ
9647
9648 item->per_cu->queued = 0;
9649 next_item = item->next;
9650 xfree (item);
9651 }
9652
9653 dwarf2_queue_tail = NULL;
45cfd468 9654
b4f54984 9655 if (dwarf_read_debug)
45cfd468
DE
9656 {
9657 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9658 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9659 }
10b3939b
DJ
9660}
9661
10b3939b
DJ
9662/* Read in full symbols for PST, and anything it depends on. */
9663
c906108c 9664static void
891813be 9665psymtab_to_symtab_1 (dwarf2_psymtab *pst)
c906108c 9666{
10b3939b 9667 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9668 int i;
9669
95554aad
TT
9670 if (pst->readin)
9671 return;
9672
aaa75496 9673 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9674 if (!pst->dependencies[i]->readin
9675 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9676 {
9677 /* Inform about additional files that need to be read in. */
9678 if (info_verbose)
9679 {
a3f17187 9680 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9681 fputs_filtered (" ", gdb_stdout);
9682 wrap_here ("");
9683 fputs_filtered ("and ", gdb_stdout);
9684 wrap_here ("");
9685 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9686 wrap_here (""); /* Flush output. */
aaa75496
JB
9687 gdb_flush (gdb_stdout);
9688 }
891813be 9689 psymtab_to_symtab_1 ((dwarf2_psymtab *) pst->dependencies[i]);
aaa75496
JB
9690 }
9691
891813be 9692 per_cu = pst->per_cu_data;
10b3939b
DJ
9693
9694 if (per_cu == NULL)
aaa75496
JB
9695 {
9696 /* It's an include file, no symbols to read for it.
9697 Everything is in the parent symtab. */
6d94535f 9698 pst->readin = true;
aaa75496
JB
9699 return;
9700 }
c906108c 9701
58f0c718 9702 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9703}
9704
dee91e82
DE
9705/* Trivial hash function for die_info: the hash value of a DIE
9706 is its offset in .debug_info for this objfile. */
10b3939b 9707
dee91e82
DE
9708static hashval_t
9709die_hash (const void *item)
10b3939b 9710{
9a3c8263 9711 const struct die_info *die = (const struct die_info *) item;
6502dd73 9712
9c541725 9713 return to_underlying (die->sect_off);
dee91e82 9714}
63d06c5c 9715
dee91e82
DE
9716/* Trivial comparison function for die_info structures: two DIEs
9717 are equal if they have the same offset. */
98bfdba5 9718
dee91e82
DE
9719static int
9720die_eq (const void *item_lhs, const void *item_rhs)
9721{
9a3c8263
SM
9722 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9723 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9724
9c541725 9725 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9726}
c906108c 9727
c0ab21c2 9728/* Load the DIEs associated with PER_CU into memory. */
c906108c 9729
dee91e82 9730static void
c0ab21c2
TT
9731load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9732 bool skip_partial,
9733 enum language pretend_language)
dee91e82 9734{
c0ab21c2
TT
9735 gdb_assert (! this_cu->is_debug_types);
9736
9737 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9738 if (reader.dummy_p)
9739 return;
9740
9741 struct dwarf2_cu *cu = reader.cu;
9742 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9743
dee91e82
DE
9744 gdb_assert (cu->die_hash == NULL);
9745 cu->die_hash =
9746 htab_create_alloc_ex (cu->header.length / 12,
9747 die_hash,
9748 die_eq,
9749 NULL,
9750 &cu->comp_unit_obstack,
9751 hashtab_obstack_allocate,
9752 dummy_obstack_deallocate);
e142c38c 9753
c0ab21c2
TT
9754 if (reader.has_children)
9755 reader.comp_unit_die->child
9756 = read_die_and_siblings (&reader, reader.info_ptr,
9757 &info_ptr, reader.comp_unit_die);
9758 cu->dies = reader.comp_unit_die;
dee91e82 9759 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9760
9761 /* We try not to read any attributes in this function, because not
9cdd5dbd 9762 all CUs needed for references have been loaded yet, and symbol
10b3939b 9763 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9764 or we won't be able to build types correctly.
9765 Similarly, if we do not read the producer, we can not apply
9766 producer-specific interpretation. */
c0ab21c2 9767 prepare_one_comp_unit (cu, cu->dies, pretend_language);
10b3939b
DJ
9768}
9769
3da10d80
KS
9770/* Add a DIE to the delayed physname list. */
9771
9772static void
9773add_to_method_list (struct type *type, int fnfield_index, int index,
9774 const char *name, struct die_info *die,
9775 struct dwarf2_cu *cu)
9776{
9777 struct delayed_method_info mi;
9778 mi.type = type;
9779 mi.fnfield_index = fnfield_index;
9780 mi.index = index;
9781 mi.name = name;
9782 mi.die = die;
c89b44cd 9783 cu->method_list.push_back (mi);
3da10d80
KS
9784}
9785
3693fdb3
PA
9786/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9787 "const" / "volatile". If so, decrements LEN by the length of the
9788 modifier and return true. Otherwise return false. */
9789
9790template<size_t N>
9791static bool
9792check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9793{
9794 size_t mod_len = sizeof (mod) - 1;
9795 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9796 {
9797 len -= mod_len;
9798 return true;
9799 }
9800 return false;
9801}
9802
3da10d80
KS
9803/* Compute the physnames of any methods on the CU's method list.
9804
9805 The computation of method physnames is delayed in order to avoid the
9806 (bad) condition that one of the method's formal parameters is of an as yet
9807 incomplete type. */
9808
9809static void
9810compute_delayed_physnames (struct dwarf2_cu *cu)
9811{
3693fdb3 9812 /* Only C++ delays computing physnames. */
c89b44cd 9813 if (cu->method_list.empty ())
3693fdb3
PA
9814 return;
9815 gdb_assert (cu->language == language_cplus);
9816
52941706 9817 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9818 {
1d06ead6 9819 const char *physname;
3da10d80 9820 struct fn_fieldlist *fn_flp
c89b44cd
TT
9821 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9822 physname = dwarf2_physname (mi.name, mi.die, cu);
9823 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9824 = physname ? physname : "";
3693fdb3
PA
9825
9826 /* Since there's no tag to indicate whether a method is a
9827 const/volatile overload, extract that information out of the
9828 demangled name. */
9829 if (physname != NULL)
9830 {
9831 size_t len = strlen (physname);
9832
9833 while (1)
9834 {
9835 if (physname[len] == ')') /* shortcut */
9836 break;
9837 else if (check_modifier (physname, len, " const"))
c89b44cd 9838 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9839 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9840 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9841 else
9842 break;
9843 }
9844 }
3da10d80 9845 }
c89b44cd
TT
9846
9847 /* The list is no longer needed. */
9848 cu->method_list.clear ();
3da10d80
KS
9849}
9850
a766d390
DE
9851/* Go objects should be embedded in a DW_TAG_module DIE,
9852 and it's not clear if/how imported objects will appear.
9853 To keep Go support simple until that's worked out,
9854 go back through what we've read and create something usable.
9855 We could do this while processing each DIE, and feels kinda cleaner,
9856 but that way is more invasive.
9857 This is to, for example, allow the user to type "p var" or "b main"
9858 without having to specify the package name, and allow lookups
9859 of module.object to work in contexts that use the expression
9860 parser. */
9861
9862static void
9863fixup_go_packaging (struct dwarf2_cu *cu)
9864{
421d1616 9865 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9866 struct pending *list;
9867 int i;
9868
c24bdb02 9869 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9870 list != NULL;
9871 list = list->next)
a766d390
DE
9872 {
9873 for (i = 0; i < list->nsyms; ++i)
9874 {
9875 struct symbol *sym = list->symbol[i];
9876
c1b5c1eb 9877 if (sym->language () == language_go
a766d390
DE
9878 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9879 {
421d1616
TT
9880 gdb::unique_xmalloc_ptr<char> this_package_name
9881 (go_symbol_package_name (sym));
a766d390
DE
9882
9883 if (this_package_name == NULL)
9884 continue;
9885 if (package_name == NULL)
421d1616 9886 package_name = std::move (this_package_name);
a766d390
DE
9887 else
9888 {
518817b3
SM
9889 struct objfile *objfile
9890 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9891 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9892 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9893 (symbol_symtab (sym) != NULL
9894 ? symtab_to_filename_for_display
9895 (symbol_symtab (sym))
e3b94546 9896 : objfile_name (objfile)),
421d1616 9897 this_package_name.get (), package_name.get ());
a766d390
DE
9898 }
9899 }
9900 }
9901 }
9902
9903 if (package_name != NULL)
9904 {
518817b3 9905 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9906 const char *saved_package_name
421d1616 9907 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9908 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9909 saved_package_name);
a766d390
DE
9910 struct symbol *sym;
9911
e623cf5d 9912 sym = allocate_symbol (objfile);
d3ecddab 9913 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9914 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9915 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9916 e.g., "main" finds the "main" module and not C's main(). */
9917 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9918 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9919 SYMBOL_TYPE (sym) = type;
9920
c24bdb02 9921 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9922 }
9923}
9924
c9317f21
TT
9925/* Allocate a fully-qualified name consisting of the two parts on the
9926 obstack. */
9927
9928static const char *
9929rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9930{
9931 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9932}
9933
9934/* A helper that allocates a struct discriminant_info to attach to a
9935 union type. */
9936
9937static struct discriminant_info *
9938alloc_discriminant_info (struct type *type, int discriminant_index,
9939 int default_index)
9940{
9941 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9942 gdb_assert (discriminant_index == -1
9943 || (discriminant_index >= 0
9944 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9945 gdb_assert (default_index == -1
c7b15a66 9946 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9947
9948 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9949
9950 struct discriminant_info *disc
9951 = ((struct discriminant_info *)
9952 TYPE_ZALLOC (type,
9953 offsetof (struct discriminant_info, discriminants)
9954 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9955 disc->default_index = default_index;
9956 disc->discriminant_index = discriminant_index;
9957
9958 struct dynamic_prop prop;
9959 prop.kind = PROP_UNDEFINED;
9960 prop.data.baton = disc;
9961
9962 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9963
9964 return disc;
9965}
9966
9967/* Some versions of rustc emitted enums in an unusual way.
9968
9969 Ordinary enums were emitted as unions. The first element of each
9970 structure in the union was named "RUST$ENUM$DISR". This element
9971 held the discriminant.
9972
9973 These versions of Rust also implemented the "non-zero"
9974 optimization. When the enum had two values, and one is empty and
9975 the other holds a pointer that cannot be zero, the pointer is used
9976 as the discriminant, with a zero value meaning the empty variant.
9977 Here, the union's first member is of the form
9978 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9979 where the fieldnos are the indices of the fields that should be
9980 traversed in order to find the field (which may be several fields deep)
9981 and the variantname is the name of the variant of the case when the
9982 field is zero.
9983
9984 This function recognizes whether TYPE is of one of these forms,
9985 and, if so, smashes it to be a variant type. */
9986
9987static void
9988quirk_rust_enum (struct type *type, struct objfile *objfile)
9989{
9990 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9991
9992 /* We don't need to deal with empty enums. */
9993 if (TYPE_NFIELDS (type) == 0)
9994 return;
9995
9996#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9997 if (TYPE_NFIELDS (type) == 1
9998 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9999 {
10000 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10001
10002 /* Decode the field name to find the offset of the
10003 discriminant. */
10004 ULONGEST bit_offset = 0;
10005 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10006 while (name[0] >= '0' && name[0] <= '9')
10007 {
10008 char *tail;
10009 unsigned long index = strtoul (name, &tail, 10);
10010 name = tail;
10011 if (*name != '$'
10012 || index >= TYPE_NFIELDS (field_type)
10013 || (TYPE_FIELD_LOC_KIND (field_type, index)
10014 != FIELD_LOC_KIND_BITPOS))
10015 {
b98664d3 10016 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10017 "[in module %s]"),
10018 TYPE_FIELD_NAME (type, 0),
10019 objfile_name (objfile));
10020 return;
10021 }
10022 ++name;
10023
10024 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10025 field_type = TYPE_FIELD_TYPE (field_type, index);
10026 }
10027
10028 /* Make a union to hold the variants. */
10029 struct type *union_type = alloc_type (objfile);
10030 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10031 TYPE_NFIELDS (union_type) = 3;
10032 TYPE_FIELDS (union_type)
10033 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10034 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10035 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10036
10037 /* Put the discriminant must at index 0. */
10038 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10039 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10040 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10041 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10042
10043 /* The order of fields doesn't really matter, so put the real
10044 field at index 1 and the data-less field at index 2. */
10045 struct discriminant_info *disc
10046 = alloc_discriminant_info (union_type, 0, 1);
10047 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10048 TYPE_FIELD_NAME (union_type, 1)
10049 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10050 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10051 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10052 TYPE_FIELD_NAME (union_type, 1));
10053
10054 const char *dataless_name
10055 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10056 name);
10057 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10058 dataless_name);
10059 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10060 /* NAME points into the original discriminant name, which
10061 already has the correct lifetime. */
10062 TYPE_FIELD_NAME (union_type, 2) = name;
10063 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10064 disc->discriminants[2] = 0;
10065
10066 /* Smash this type to be a structure type. We have to do this
10067 because the type has already been recorded. */
10068 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10069 TYPE_NFIELDS (type) = 1;
10070 TYPE_FIELDS (type)
10071 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10072
10073 /* Install the variant part. */
10074 TYPE_FIELD_TYPE (type, 0) = union_type;
10075 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10076 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10077 }
77c2dba3
TT
10078 /* A union with a single anonymous field is probably an old-style
10079 univariant enum. */
10080 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10081 {
c9317f21
TT
10082 /* Smash this type to be a structure type. We have to do this
10083 because the type has already been recorded. */
10084 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10085
10086 /* Make a union to hold the variants. */
10087 struct type *union_type = alloc_type (objfile);
10088 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10089 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10090 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10091 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10092 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10093
10094 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10095 const char *variant_name
10096 = rust_last_path_segment (TYPE_NAME (field_type));
10097 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10098 TYPE_NAME (field_type)
10099 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10100 TYPE_NAME (type), variant_name);
c9317f21
TT
10101
10102 /* Install the union in the outer struct type. */
10103 TYPE_NFIELDS (type) = 1;
10104 TYPE_FIELDS (type)
10105 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10106 TYPE_FIELD_TYPE (type, 0) = union_type;
10107 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10108 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10109
10110 alloc_discriminant_info (union_type, -1, 0);
10111 }
10112 else
10113 {
10114 struct type *disr_type = nullptr;
10115 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10116 {
10117 disr_type = TYPE_FIELD_TYPE (type, i);
10118
a037790e
TT
10119 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10120 {
10121 /* All fields of a true enum will be structs. */
10122 return;
10123 }
10124 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10125 {
10126 /* Could be data-less variant, so keep going. */
a037790e 10127 disr_type = nullptr;
c9317f21
TT
10128 }
10129 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10130 "RUST$ENUM$DISR") != 0)
10131 {
10132 /* Not a Rust enum. */
10133 return;
10134 }
10135 else
10136 {
10137 /* Found one. */
10138 break;
10139 }
10140 }
10141
10142 /* If we got here without a discriminant, then it's probably
10143 just a union. */
10144 if (disr_type == nullptr)
10145 return;
10146
10147 /* Smash this type to be a structure type. We have to do this
10148 because the type has already been recorded. */
10149 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10150
10151 /* Make a union to hold the variants. */
10152 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10153 struct type *union_type = alloc_type (objfile);
10154 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10155 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10156 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10157 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10158 TYPE_FIELDS (union_type)
10159 = (struct field *) TYPE_ZALLOC (union_type,
10160 (TYPE_NFIELDS (union_type)
10161 * sizeof (struct field)));
10162
10163 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10164 TYPE_NFIELDS (type) * sizeof (struct field));
10165
10166 /* Install the discriminant at index 0 in the union. */
10167 TYPE_FIELD (union_type, 0) = *disr_field;
10168 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10169 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10170
10171 /* Install the union in the outer struct type. */
10172 TYPE_FIELD_TYPE (type, 0) = union_type;
10173 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10174 TYPE_NFIELDS (type) = 1;
10175
10176 /* Set the size and offset of the union type. */
10177 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10178
10179 /* We need a way to find the correct discriminant given a
10180 variant name. For convenience we build a map here. */
10181 struct type *enum_type = FIELD_TYPE (*disr_field);
10182 std::unordered_map<std::string, ULONGEST> discriminant_map;
10183 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10184 {
10185 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10186 {
10187 const char *name
10188 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10189 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10190 }
10191 }
10192
10193 int n_fields = TYPE_NFIELDS (union_type);
10194 struct discriminant_info *disc
10195 = alloc_discriminant_info (union_type, 0, -1);
10196 /* Skip the discriminant here. */
10197 for (int i = 1; i < n_fields; ++i)
10198 {
10199 /* Find the final word in the name of this variant's type.
10200 That name can be used to look up the correct
10201 discriminant. */
10202 const char *variant_name
10203 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10204 i)));
10205
10206 auto iter = discriminant_map.find (variant_name);
10207 if (iter != discriminant_map.end ())
10208 disc->discriminants[i] = iter->second;
10209
bedda9ac 10210 /* Remove the discriminant field, if it exists. */
c9317f21 10211 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10212 if (TYPE_NFIELDS (sub_type) > 0)
10213 {
10214 --TYPE_NFIELDS (sub_type);
10215 ++TYPE_FIELDS (sub_type);
10216 }
c9317f21
TT
10217 TYPE_FIELD_NAME (union_type, i) = variant_name;
10218 TYPE_NAME (sub_type)
10219 = rust_fully_qualify (&objfile->objfile_obstack,
10220 TYPE_NAME (type), variant_name);
10221 }
10222 }
10223}
10224
10225/* Rewrite some Rust unions to be structures with variants parts. */
10226
10227static void
10228rust_union_quirks (struct dwarf2_cu *cu)
10229{
10230 gdb_assert (cu->language == language_rust);
52941706
SM
10231 for (type *type_ : cu->rust_unions)
10232 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10233 /* We don't need this any more. */
10234 cu->rust_unions.clear ();
c9317f21
TT
10235}
10236
95554aad
TT
10237/* Return the symtab for PER_CU. This works properly regardless of
10238 whether we're using the index or psymtabs. */
10239
43f3e411
DE
10240static struct compunit_symtab *
10241get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10242{
ed2dc618 10243 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10244 ? per_cu->v.quick->compunit_symtab
10245 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10246}
10247
10248/* A helper function for computing the list of all symbol tables
10249 included by PER_CU. */
10250
10251static void
4c39bc03 10252recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10253 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10254 struct dwarf2_per_cu_data *per_cu,
43f3e411 10255 struct compunit_symtab *immediate_parent)
95554aad
TT
10256{
10257 void **slot;
43f3e411 10258 struct compunit_symtab *cust;
95554aad
TT
10259
10260 slot = htab_find_slot (all_children, per_cu, INSERT);
10261 if (*slot != NULL)
10262 {
10263 /* This inclusion and its children have been processed. */
10264 return;
10265 }
10266
10267 *slot = per_cu;
10268 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10269 cust = get_compunit_symtab (per_cu);
10270 if (cust != NULL)
ec94af83
DE
10271 {
10272 /* If this is a type unit only add its symbol table if we haven't
10273 seen it yet (type unit per_cu's can share symtabs). */
10274 if (per_cu->is_debug_types)
10275 {
43f3e411 10276 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10277 if (*slot == NULL)
10278 {
43f3e411 10279 *slot = cust;
4c39bc03 10280 result->push_back (cust);
43f3e411
DE
10281 if (cust->user == NULL)
10282 cust->user = immediate_parent;
ec94af83
DE
10283 }
10284 }
10285 else
f9125b6c 10286 {
4c39bc03 10287 result->push_back (cust);
43f3e411
DE
10288 if (cust->user == NULL)
10289 cust->user = immediate_parent;
f9125b6c 10290 }
ec94af83 10291 }
95554aad 10292
ae640021
AB
10293 if (!per_cu->imported_symtabs_empty ())
10294 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10295 {
10296 recursively_compute_inclusions (result, all_children,
10297 all_type_symtabs, ptr, cust);
10298 }
95554aad
TT
10299}
10300
43f3e411 10301/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10302 PER_CU. */
10303
10304static void
43f3e411 10305compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10306{
f4dc4d17
DE
10307 gdb_assert (! per_cu->is_debug_types);
10308
ae640021 10309 if (!per_cu->imported_symtabs_empty ())
95554aad 10310 {
ae640021 10311 int len;
4c39bc03 10312 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10313 htab_t all_children, all_type_symtabs;
43f3e411 10314 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10315
10316 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10317 if (cust == NULL)
95554aad
TT
10318 return;
10319
10320 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10321 NULL, xcalloc, xfree);
ec94af83
DE
10322 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10323 NULL, xcalloc, xfree);
95554aad 10324
ae640021 10325 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10326 {
10327 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10328 all_type_symtabs, ptr, cust);
ec94af83 10329 }
95554aad 10330
ec94af83 10331 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10332 len = result_symtabs.size ();
43f3e411 10333 cust->includes
ed2dc618 10334 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10335 struct compunit_symtab *, len + 1);
4c39bc03
TT
10336 memcpy (cust->includes, result_symtabs.data (),
10337 len * sizeof (compunit_symtab *));
43f3e411 10338 cust->includes[len] = NULL;
95554aad 10339
95554aad 10340 htab_delete (all_children);
ec94af83 10341 htab_delete (all_type_symtabs);
95554aad
TT
10342 }
10343}
10344
10345/* Compute the 'includes' field for the symtabs of all the CUs we just
10346 read. */
10347
10348static void
ed2dc618 10349process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10350{
71b73764 10351 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10352 {
10353 if (! iter->is_debug_types)
43f3e411 10354 compute_compunit_symtab_includes (iter);
f4dc4d17 10355 }
95554aad 10356
c5d0225d 10357 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10358}
10359
9cdd5dbd 10360/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10361 already been loaded into memory. */
10362
10363static void
95554aad
TT
10364process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10365 enum language pretend_language)
10b3939b 10366{
10b3939b 10367 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10368 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10369 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10370 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10371 CORE_ADDR lowpc, highpc;
43f3e411 10372 struct compunit_symtab *cust;
10b3939b 10373 CORE_ADDR baseaddr;
4359dff1 10374 struct block *static_block;
3e29f34a 10375 CORE_ADDR addr;
10b3939b 10376
b3b3bada 10377 baseaddr = objfile->text_section_offset ();
10b3939b 10378
c89b44cd
TT
10379 /* Clear the list here in case something was left over. */
10380 cu->method_list.clear ();
10b3939b 10381
95554aad
TT
10382 cu->language = pretend_language;
10383 cu->language_defn = language_def (cu->language);
10384
c906108c 10385 /* Do line number decoding in read_file_scope () */
10b3939b 10386 process_die (cu->dies, cu);
c906108c 10387
a766d390
DE
10388 /* For now fudge the Go package. */
10389 if (cu->language == language_go)
10390 fixup_go_packaging (cu);
10391
5f48f8f3 10392 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10393 should be complete, and it should now be safe to compute all of the
10394 physnames. */
10395 compute_delayed_physnames (cu);
3da10d80 10396
c9317f21
TT
10397 if (cu->language == language_rust)
10398 rust_union_quirks (cu);
10399
fae299cd
DC
10400 /* Some compilers don't define a DW_AT_high_pc attribute for the
10401 compilation unit. If the DW_AT_high_pc is missing, synthesize
10402 it, by scanning the DIE's below the compilation unit. */
10b3939b 10403 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10404
3e29f34a 10405 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10406 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10407
10408 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10409 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10410 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10411 addrmap to help ensure it has an accurate map of pc values belonging to
10412 this comp unit. */
10413 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10414
c24bdb02 10415 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10416 SECT_OFF_TEXT (objfile),
10417 0);
c906108c 10418
43f3e411 10419 if (cust != NULL)
c906108c 10420 {
df15bd07 10421 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10422
8be455d7
JK
10423 /* Set symtab language to language from DW_AT_language. If the
10424 compilation is from a C file generated by language preprocessors, do
10425 not set the language if it was already deduced by start_subfile. */
43f3e411 10426 if (!(cu->language == language_c
40e3ad0e 10427 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10428 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10429
10430 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10431 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10432 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10433 there were bugs in prologue debug info, fixed later in GCC-4.5
10434 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10435
10436 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10437 needed, it would be wrong due to missing DW_AT_producer there.
10438
10439 Still one can confuse GDB by using non-standard GCC compilation
10440 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10441 */
ab260dad 10442 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10443 cust->locations_valid = 1;
e0d00bc7
JK
10444
10445 if (gcc_4_minor >= 5)
43f3e411 10446 cust->epilogue_unwind_valid = 1;
96408a79 10447
43f3e411 10448 cust->call_site_htab = cu->call_site_htab;
c906108c 10449 }
9291a0cd
TT
10450
10451 if (dwarf2_per_objfile->using_index)
43f3e411 10452 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10453 else
10454 {
891813be 10455 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 10456 pst->compunit_symtab = cust;
6d94535f 10457 pst->readin = true;
9291a0cd 10458 }
c906108c 10459
95554aad 10460 /* Push it for inclusion processing later. */
c5d0225d 10461 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10462
10463 /* Not needed any more. */
c24bdb02 10464 cu->reset_builder ();
f4dc4d17 10465}
45cfd468 10466
f4dc4d17
DE
10467/* Generate full symbol information for type unit PER_CU, whose DIEs have
10468 already been loaded into memory. */
10469
10470static void
10471process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10472 enum language pretend_language)
10473{
10474 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10475 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10476 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10477 struct compunit_symtab *cust;
0186c6a7
DE
10478 struct signatured_type *sig_type;
10479
10480 gdb_assert (per_cu->is_debug_types);
10481 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10482
c89b44cd
TT
10483 /* Clear the list here in case something was left over. */
10484 cu->method_list.clear ();
f4dc4d17 10485
f4dc4d17
DE
10486 cu->language = pretend_language;
10487 cu->language_defn = language_def (cu->language);
10488
10489 /* The symbol tables are set up in read_type_unit_scope. */
10490 process_die (cu->dies, cu);
10491
10492 /* For now fudge the Go package. */
10493 if (cu->language == language_go)
10494 fixup_go_packaging (cu);
10495
5f48f8f3 10496 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10497 should be complete, and it should now be safe to compute all of the
10498 physnames. */
10499 compute_delayed_physnames (cu);
f4dc4d17 10500
c9317f21
TT
10501 if (cu->language == language_rust)
10502 rust_union_quirks (cu);
10503
f4dc4d17
DE
10504 /* TUs share symbol tables.
10505 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10506 of it with end_expandable_symtab. Otherwise, complete the addition of
10507 this TU's symbols to the existing symtab. */
43f3e411 10508 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10509 {
c24bdb02
KS
10510 buildsym_compunit *builder = cu->get_builder ();
10511 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10512 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10513
43f3e411 10514 if (cust != NULL)
f4dc4d17
DE
10515 {
10516 /* Set symtab language to language from DW_AT_language. If the
10517 compilation is from a C file generated by language preprocessors,
10518 do not set the language if it was already deduced by
10519 start_subfile. */
43f3e411
DE
10520 if (!(cu->language == language_c
10521 && COMPUNIT_FILETABS (cust)->language != language_c))
10522 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10523 }
10524 }
10525 else
10526 {
c24bdb02 10527 cu->get_builder ()->augment_type_symtab ();
43f3e411 10528 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10529 }
10530
10531 if (dwarf2_per_objfile->using_index)
43f3e411 10532 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10533 else
10534 {
891813be 10535 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 10536 pst->compunit_symtab = cust;
6d94535f 10537 pst->readin = true;
45cfd468 10538 }
804d2729
TT
10539
10540 /* Not needed any more. */
c24bdb02 10541 cu->reset_builder ();
c906108c
SS
10542}
10543
95554aad
TT
10544/* Process an imported unit DIE. */
10545
10546static void
10547process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10548{
10549 struct attribute *attr;
10550
f4dc4d17
DE
10551 /* For now we don't handle imported units in type units. */
10552 if (cu->per_cu->is_debug_types)
10553 {
10554 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10555 " supported in type units [in module %s]"),
518817b3 10556 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10557 }
10558
95554aad
TT
10559 attr = dwarf2_attr (die, DW_AT_import, cu);
10560 if (attr != NULL)
10561 {
9c541725
PA
10562 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10563 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10564 dwarf2_per_cu_data *per_cu
e3b94546 10565 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10566 cu->per_cu->dwarf2_per_objfile);
95554aad 10567
69d751e3 10568 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10569 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10570 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10571
ae640021 10572 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10573 }
10574}
10575
4c8aa72d
PA
10576/* RAII object that represents a process_die scope: i.e.,
10577 starts/finishes processing a DIE. */
10578class process_die_scope
adde2bff 10579{
4c8aa72d
PA
10580public:
10581 process_die_scope (die_info *die, dwarf2_cu *cu)
10582 : m_die (die), m_cu (cu)
10583 {
10584 /* We should only be processing DIEs not already in process. */
10585 gdb_assert (!m_die->in_process);
10586 m_die->in_process = true;
10587 }
8c3cb9fa 10588
4c8aa72d
PA
10589 ~process_die_scope ()
10590 {
10591 m_die->in_process = false;
10592
10593 /* If we're done processing the DIE for the CU that owns the line
10594 header, we don't need the line header anymore. */
10595 if (m_cu->line_header_die_owner == m_die)
10596 {
10597 delete m_cu->line_header;
10598 m_cu->line_header = NULL;
10599 m_cu->line_header_die_owner = NULL;
10600 }
10601 }
10602
10603private:
10604 die_info *m_die;
10605 dwarf2_cu *m_cu;
10606};
adde2bff 10607
c906108c
SS
10608/* Process a die and its children. */
10609
10610static void
e7c27a73 10611process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10612{
4c8aa72d 10613 process_die_scope scope (die, cu);
adde2bff 10614
c906108c
SS
10615 switch (die->tag)
10616 {
10617 case DW_TAG_padding:
10618 break;
10619 case DW_TAG_compile_unit:
95554aad 10620 case DW_TAG_partial_unit:
e7c27a73 10621 read_file_scope (die, cu);
c906108c 10622 break;
348e048f
DE
10623 case DW_TAG_type_unit:
10624 read_type_unit_scope (die, cu);
10625 break;
c906108c 10626 case DW_TAG_subprogram:
0a4b0913
AB
10627 /* Nested subprograms in Fortran get a prefix. */
10628 if (cu->language == language_fortran
10629 && die->parent != NULL
10630 && die->parent->tag == DW_TAG_subprogram)
10631 cu->processing_has_namespace_info = true;
10632 /* Fall through. */
c906108c 10633 case DW_TAG_inlined_subroutine:
edb3359d 10634 read_func_scope (die, cu);
c906108c
SS
10635 break;
10636 case DW_TAG_lexical_block:
14898363
L
10637 case DW_TAG_try_block:
10638 case DW_TAG_catch_block:
e7c27a73 10639 read_lexical_block_scope (die, cu);
c906108c 10640 break;
216f72a1 10641 case DW_TAG_call_site:
96408a79
SA
10642 case DW_TAG_GNU_call_site:
10643 read_call_site_scope (die, cu);
10644 break;
c906108c 10645 case DW_TAG_class_type:
680b30c7 10646 case DW_TAG_interface_type:
c906108c
SS
10647 case DW_TAG_structure_type:
10648 case DW_TAG_union_type:
134d01f1 10649 process_structure_scope (die, cu);
c906108c
SS
10650 break;
10651 case DW_TAG_enumeration_type:
134d01f1 10652 process_enumeration_scope (die, cu);
c906108c 10653 break;
134d01f1 10654
f792889a
DJ
10655 /* These dies have a type, but processing them does not create
10656 a symbol or recurse to process the children. Therefore we can
10657 read them on-demand through read_type_die. */
c906108c 10658 case DW_TAG_subroutine_type:
72019c9c 10659 case DW_TAG_set_type:
c906108c 10660 case DW_TAG_array_type:
c906108c 10661 case DW_TAG_pointer_type:
c906108c 10662 case DW_TAG_ptr_to_member_type:
c906108c 10663 case DW_TAG_reference_type:
4297a3f0 10664 case DW_TAG_rvalue_reference_type:
c906108c 10665 case DW_TAG_string_type:
c906108c 10666 break;
134d01f1 10667
c906108c 10668 case DW_TAG_base_type:
a02abb62 10669 case DW_TAG_subrange_type:
cb249c71 10670 case DW_TAG_typedef:
134d01f1
DJ
10671 /* Add a typedef symbol for the type definition, if it has a
10672 DW_AT_name. */
f792889a 10673 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10674 break;
c906108c 10675 case DW_TAG_common_block:
e7c27a73 10676 read_common_block (die, cu);
c906108c
SS
10677 break;
10678 case DW_TAG_common_inclusion:
10679 break;
d9fa45fe 10680 case DW_TAG_namespace:
9068261f 10681 cu->processing_has_namespace_info = true;
e7c27a73 10682 read_namespace (die, cu);
d9fa45fe 10683 break;
5d7cb8df 10684 case DW_TAG_module:
9068261f 10685 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10686 read_module (die, cu);
10687 break;
d9fa45fe 10688 case DW_TAG_imported_declaration:
9068261f 10689 cu->processing_has_namespace_info = true;
74921315
KS
10690 if (read_namespace_alias (die, cu))
10691 break;
86a73007
TT
10692 /* The declaration is not a global namespace alias. */
10693 /* Fall through. */
d9fa45fe 10694 case DW_TAG_imported_module:
9068261f 10695 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10696 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10697 || cu->language != language_fortran))
b98664d3 10698 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10699 dwarf_tag_name (die->tag));
10700 read_import_statement (die, cu);
d9fa45fe 10701 break;
95554aad
TT
10702
10703 case DW_TAG_imported_unit:
10704 process_imported_unit_die (die, cu);
10705 break;
10706
71a3c369
TT
10707 case DW_TAG_variable:
10708 read_variable (die, cu);
10709 break;
10710
c906108c 10711 default:
e7c27a73 10712 new_symbol (die, NULL, cu);
c906108c
SS
10713 break;
10714 }
10715}
ca69b9e6
DE
10716\f
10717/* DWARF name computation. */
c906108c 10718
94af9270
KS
10719/* A helper function for dwarf2_compute_name which determines whether DIE
10720 needs to have the name of the scope prepended to the name listed in the
10721 die. */
10722
10723static int
10724die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10725{
1c809c68
TT
10726 struct attribute *attr;
10727
94af9270
KS
10728 switch (die->tag)
10729 {
10730 case DW_TAG_namespace:
10731 case DW_TAG_typedef:
10732 case DW_TAG_class_type:
10733 case DW_TAG_interface_type:
10734 case DW_TAG_structure_type:
10735 case DW_TAG_union_type:
10736 case DW_TAG_enumeration_type:
10737 case DW_TAG_enumerator:
10738 case DW_TAG_subprogram:
08a76f8a 10739 case DW_TAG_inlined_subroutine:
94af9270 10740 case DW_TAG_member:
74921315 10741 case DW_TAG_imported_declaration:
94af9270
KS
10742 return 1;
10743
10744 case DW_TAG_variable:
c2b0a229 10745 case DW_TAG_constant:
94af9270
KS
10746 /* We only need to prefix "globally" visible variables. These include
10747 any variable marked with DW_AT_external or any variable that
10748 lives in a namespace. [Variables in anonymous namespaces
10749 require prefixing, but they are not DW_AT_external.] */
10750
10751 if (dwarf2_attr (die, DW_AT_specification, cu))
10752 {
10753 struct dwarf2_cu *spec_cu = cu;
9a619af0 10754
94af9270
KS
10755 return die_needs_namespace (die_specification (die, &spec_cu),
10756 spec_cu);
10757 }
10758
1c809c68 10759 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10760 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10761 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10762 return 0;
10763 /* A variable in a lexical block of some kind does not need a
10764 namespace, even though in C++ such variables may be external
10765 and have a mangled name. */
10766 if (die->parent->tag == DW_TAG_lexical_block
10767 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10768 || die->parent->tag == DW_TAG_catch_block
10769 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10770 return 0;
10771 return 1;
94af9270
KS
10772
10773 default:
10774 return 0;
10775 }
10776}
10777
73b9be8b
KS
10778/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10779 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10780 defined for the given DIE. */
10781
10782static struct attribute *
10783dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10784{
10785 struct attribute *attr;
10786
10787 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10788 if (attr == NULL)
10789 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10790
10791 return attr;
10792}
10793
10794/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10795 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10796 defined for the given DIE. */
10797
10798static const char *
10799dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10800{
10801 const char *linkage_name;
10802
10803 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10804 if (linkage_name == NULL)
10805 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10806
10807 return linkage_name;
10808}
10809
94af9270 10810/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10811 compute the physname for the object, which include a method's:
9c37b5ae 10812 - formal parameters (C++),
a766d390 10813 - receiver type (Go),
a766d390
DE
10814
10815 The term "physname" is a bit confusing.
10816 For C++, for example, it is the demangled name.
10817 For Go, for example, it's the mangled name.
94af9270 10818
af6b7be1
JB
10819 For Ada, return the DIE's linkage name rather than the fully qualified
10820 name. PHYSNAME is ignored..
10821
94af9270
KS
10822 The result is allocated on the objfile_obstack and canonicalized. */
10823
10824static const char *
15d034d0
TT
10825dwarf2_compute_name (const char *name,
10826 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10827 int physname)
10828{
518817b3 10829 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10830
94af9270
KS
10831 if (name == NULL)
10832 name = dwarf2_name (die, cu);
10833
2ee7123e
DE
10834 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10835 but otherwise compute it by typename_concat inside GDB.
10836 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10837 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10838 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10839 will set the demangled name to the result of dwarf2_full_name, and it is
10840 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10841 if (cu->language == language_ada
10842 || (cu->language == language_fortran && physname))
10843 {
10844 /* For Ada unit, we prefer the linkage name over the name, as
10845 the former contains the exported name, which the user expects
10846 to be able to reference. Ideally, we want the user to be able
10847 to reference this entity using either natural or linkage name,
10848 but we haven't started looking at this enhancement yet. */
73b9be8b 10849 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10850
2ee7123e
DE
10851 if (linkage_name != NULL)
10852 return linkage_name;
f55ee35c
JK
10853 }
10854
94af9270
KS
10855 /* These are the only languages we know how to qualify names in. */
10856 if (name != NULL
9c37b5ae 10857 && (cu->language == language_cplus
c44af4eb
TT
10858 || cu->language == language_fortran || cu->language == language_d
10859 || cu->language == language_rust))
94af9270
KS
10860 {
10861 if (die_needs_namespace (die, cu))
10862 {
0d5cff50 10863 const char *prefix;
34a68019 10864 const char *canonical_name = NULL;
94af9270 10865
d7e74731
PA
10866 string_file buf;
10867
94af9270 10868 prefix = determine_prefix (die, cu);
94af9270
KS
10869 if (*prefix != '\0')
10870 {
43816ebc
TT
10871 gdb::unique_xmalloc_ptr<char> prefixed_name
10872 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10873
43816ebc 10874 buf.puts (prefixed_name.get ());
94af9270
KS
10875 }
10876 else
d7e74731 10877 buf.puts (name);
94af9270 10878
98bfdba5
PA
10879 /* Template parameters may be specified in the DIE's DW_AT_name, or
10880 as children with DW_TAG_template_type_param or
10881 DW_TAG_value_type_param. If the latter, add them to the name
10882 here. If the name already has template parameters, then
10883 skip this step; some versions of GCC emit both, and
10884 it is more efficient to use the pre-computed name.
10885
10886 Something to keep in mind about this process: it is very
10887 unlikely, or in some cases downright impossible, to produce
10888 something that will match the mangled name of a function.
10889 If the definition of the function has the same debug info,
10890 we should be able to match up with it anyway. But fallbacks
10891 using the minimal symbol, for instance to find a method
10892 implemented in a stripped copy of libstdc++, will not work.
10893 If we do not have debug info for the definition, we will have to
10894 match them up some other way.
10895
10896 When we do name matching there is a related problem with function
10897 templates; two instantiated function templates are allowed to
10898 differ only by their return types, which we do not add here. */
10899
10900 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10901 {
10902 struct attribute *attr;
10903 struct die_info *child;
10904 int first = 1;
10905
10906 die->building_fullname = 1;
10907
10908 for (child = die->child; child != NULL; child = child->sibling)
10909 {
10910 struct type *type;
12df843f 10911 LONGEST value;
d521ce57 10912 const gdb_byte *bytes;
98bfdba5
PA
10913 struct dwarf2_locexpr_baton *baton;
10914 struct value *v;
10915
10916 if (child->tag != DW_TAG_template_type_param
10917 && child->tag != DW_TAG_template_value_param)
10918 continue;
10919
10920 if (first)
10921 {
d7e74731 10922 buf.puts ("<");
98bfdba5
PA
10923 first = 0;
10924 }
10925 else
d7e74731 10926 buf.puts (", ");
98bfdba5
PA
10927
10928 attr = dwarf2_attr (child, DW_AT_type, cu);
10929 if (attr == NULL)
10930 {
b98664d3 10931 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10932 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10933 continue;
10934 }
10935 type = die_type (child, cu);
10936
10937 if (child->tag == DW_TAG_template_type_param)
10938 {
c1ec8cea
TT
10939 c_print_type (type, "", &buf, -1, 0, cu->language,
10940 &type_print_raw_options);
98bfdba5
PA
10941 continue;
10942 }
10943
10944 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10945 if (attr == NULL)
10946 {
b98664d3 10947 complaint (_("template parameter missing "
3e43a32a 10948 "DW_AT_const_value"));
d7e74731 10949 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10950 continue;
10951 }
10952
10953 dwarf2_const_value_attr (attr, type, name,
10954 &cu->comp_unit_obstack, cu,
10955 &value, &bytes, &baton);
10956
10957 if (TYPE_NOSIGN (type))
10958 /* GDB prints characters as NUMBER 'CHAR'. If that's
10959 changed, this can use value_print instead. */
d7e74731 10960 c_printchar (value, type, &buf);
98bfdba5
PA
10961 else
10962 {
10963 struct value_print_options opts;
10964
10965 if (baton != NULL)
10966 v = dwarf2_evaluate_loc_desc (type, NULL,
10967 baton->data,
10968 baton->size,
10969 baton->per_cu);
10970 else if (bytes != NULL)
10971 {
10972 v = allocate_value (type);
10973 memcpy (value_contents_writeable (v), bytes,
10974 TYPE_LENGTH (type));
10975 }
10976 else
10977 v = value_from_longest (type, value);
10978
3e43a32a
MS
10979 /* Specify decimal so that we do not depend on
10980 the radix. */
98bfdba5
PA
10981 get_formatted_print_options (&opts, 'd');
10982 opts.raw = 1;
d7e74731 10983 value_print (v, &buf, &opts);
98bfdba5 10984 release_value (v);
98bfdba5
PA
10985 }
10986 }
10987
10988 die->building_fullname = 0;
10989
10990 if (!first)
10991 {
10992 /* Close the argument list, with a space if necessary
10993 (nested templates). */
d7e74731
PA
10994 if (!buf.empty () && buf.string ().back () == '>')
10995 buf.puts (" >");
98bfdba5 10996 else
d7e74731 10997 buf.puts (">");
98bfdba5
PA
10998 }
10999 }
11000
9c37b5ae 11001 /* For C++ methods, append formal parameter type
94af9270 11002 information, if PHYSNAME. */
6e70227d 11003
94af9270 11004 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11005 && cu->language == language_cplus)
94af9270
KS
11006 {
11007 struct type *type = read_type_die (die, cu);
11008
d7e74731 11009 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11010 &type_print_raw_options);
94af9270 11011
9c37b5ae 11012 if (cu->language == language_cplus)
94af9270 11013 {
60430eff
DJ
11014 /* Assume that an artificial first parameter is
11015 "this", but do not crash if it is not. RealView
11016 marks unnamed (and thus unused) parameters as
11017 artificial; there is no way to differentiate
11018 the two cases. */
94af9270
KS
11019 if (TYPE_NFIELDS (type) > 0
11020 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11021 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11022 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11023 0))))
d7e74731 11024 buf.puts (" const");
94af9270
KS
11025 }
11026 }
11027
d7e74731 11028 const std::string &intermediate_name = buf.string ();
94af9270
KS
11029
11030 if (cu->language == language_cplus)
34a68019 11031 canonical_name
322a8516 11032 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11033 &objfile->per_bfd->storage_obstack);
11034
11035 /* If we only computed INTERMEDIATE_NAME, or if
11036 INTERMEDIATE_NAME is already canonical, then we need to
11037 copy it to the appropriate obstack. */
322a8516 11038 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11039 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11040 intermediate_name);
34a68019
TT
11041 else
11042 name = canonical_name;
94af9270
KS
11043 }
11044 }
11045
11046 return name;
11047}
11048
0114d602
DJ
11049/* Return the fully qualified name of DIE, based on its DW_AT_name.
11050 If scope qualifiers are appropriate they will be added. The result
34a68019 11051 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11052 not have a name. NAME may either be from a previous call to
11053 dwarf2_name or NULL.
11054
9c37b5ae 11055 The output string will be canonicalized (if C++). */
0114d602
DJ
11056
11057static const char *
15d034d0 11058dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11059{
94af9270
KS
11060 return dwarf2_compute_name (name, die, cu, 0);
11061}
0114d602 11062
94af9270
KS
11063/* Construct a physname for the given DIE in CU. NAME may either be
11064 from a previous call to dwarf2_name or NULL. The result will be
11065 allocated on the objfile_objstack or NULL if the DIE does not have a
11066 name.
0114d602 11067
9c37b5ae 11068 The output string will be canonicalized (if C++). */
0114d602 11069
94af9270 11070static const char *
15d034d0 11071dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11072{
518817b3 11073 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11074 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11075 int need_copy = 1;
11076
11077 /* In this case dwarf2_compute_name is just a shortcut not building anything
11078 on its own. */
11079 if (!die_needs_namespace (die, cu))
11080 return dwarf2_compute_name (name, die, cu, 1);
11081
73b9be8b 11082 mangled = dw2_linkage_name (die, cu);
900e11f9 11083
e98c9e7c
TT
11084 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11085 See https://github.com/rust-lang/rust/issues/32925. */
11086 if (cu->language == language_rust && mangled != NULL
11087 && strchr (mangled, '{') != NULL)
11088 mangled = NULL;
11089
900e11f9
JK
11090 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11091 has computed. */
791afaa2 11092 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11093 if (mangled != NULL)
900e11f9 11094 {
900e11f9 11095
59cc4834
JB
11096 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11097 {
11098 /* Do nothing (do not demangle the symbol name). */
11099 }
11100 else if (cu->language == language_go)
a766d390 11101 {
5e2db402
TT
11102 /* This is a lie, but we already lie to the caller new_symbol.
11103 new_symbol assumes we return the mangled name.
a766d390 11104 This just undoes that lie until things are cleaned up. */
a766d390
DE
11105 }
11106 else
11107 {
0eb876f5
JB
11108 /* Use DMGL_RET_DROP for C++ template functions to suppress
11109 their return type. It is easier for GDB users to search
11110 for such functions as `name(params)' than `long name(params)'.
11111 In such case the minimal symbol names do not match the full
11112 symbol names but for template functions there is never a need
11113 to look up their definition from their declaration so
11114 the only disadvantage remains the minimal symbol variant
11115 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11116 demangled.reset (gdb_demangle (mangled,
11117 (DMGL_PARAMS | DMGL_ANSI
11118 | DMGL_RET_DROP)));
a766d390 11119 }
900e11f9 11120 if (demangled)
791afaa2 11121 canon = demangled.get ();
900e11f9
JK
11122 else
11123 {
11124 canon = mangled;
11125 need_copy = 0;
11126 }
11127 }
11128
11129 if (canon == NULL || check_physname)
11130 {
11131 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11132
11133 if (canon != NULL && strcmp (physname, canon) != 0)
11134 {
11135 /* It may not mean a bug in GDB. The compiler could also
11136 compute DW_AT_linkage_name incorrectly. But in such case
11137 GDB would need to be bug-to-bug compatible. */
11138
b98664d3 11139 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11140 "(from linkage <%s>) - DIE at %s [in module %s]"),
11141 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11142 objfile_name (objfile));
900e11f9
JK
11143
11144 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11145 is available here - over computed PHYSNAME. It is safer
11146 against both buggy GDB and buggy compilers. */
11147
11148 retval = canon;
11149 }
11150 else
11151 {
11152 retval = physname;
11153 need_copy = 0;
11154 }
11155 }
11156 else
11157 retval = canon;
11158
11159 if (need_copy)
021887d8 11160 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11161
900e11f9 11162 return retval;
0114d602
DJ
11163}
11164
74921315
KS
11165/* Inspect DIE in CU for a namespace alias. If one exists, record
11166 a new symbol for it.
11167
11168 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11169
11170static int
11171read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11172{
11173 struct attribute *attr;
11174
11175 /* If the die does not have a name, this is not a namespace
11176 alias. */
11177 attr = dwarf2_attr (die, DW_AT_name, cu);
11178 if (attr != NULL)
11179 {
11180 int num;
11181 struct die_info *d = die;
11182 struct dwarf2_cu *imported_cu = cu;
11183
11184 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11185 keep inspecting DIEs until we hit the underlying import. */
11186#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11187 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11188 {
11189 attr = dwarf2_attr (d, DW_AT_import, cu);
11190 if (attr == NULL)
11191 break;
11192
11193 d = follow_die_ref (d, attr, &imported_cu);
11194 if (d->tag != DW_TAG_imported_declaration)
11195 break;
11196 }
11197
11198 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11199 {
b98664d3 11200 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11201 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11202 return 0;
11203 }
11204
11205 if (attr != NULL)
11206 {
11207 struct type *type;
9c541725 11208 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11209
9c541725 11210 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11211 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11212 {
11213 /* This declaration is a global namespace alias. Add
11214 a symbol for it whose type is the aliased namespace. */
11215 new_symbol (die, type, cu);
11216 return 1;
11217 }
11218 }
11219 }
11220
11221 return 0;
11222}
11223
22cee43f 11224/* Return the using directives repository (global or local?) to use in the
804d2729 11225 current context for CU.
22cee43f
PMR
11226
11227 For Ada, imported declarations can materialize renamings, which *may* be
11228 global. However it is impossible (for now?) in DWARF to distinguish
11229 "external" imported declarations and "static" ones. As all imported
11230 declarations seem to be static in all other languages, make them all CU-wide
11231 global only in Ada. */
11232
11233static struct using_direct **
804d2729 11234using_directives (struct dwarf2_cu *cu)
22cee43f 11235{
c24bdb02
KS
11236 if (cu->language == language_ada
11237 && cu->get_builder ()->outermost_context_p ())
11238 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11239 else
c24bdb02 11240 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11241}
11242
27aa8d6a
SW
11243/* Read the import statement specified by the given die and record it. */
11244
11245static void
11246read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11247{
518817b3 11248 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11249 struct attribute *import_attr;
32019081 11250 struct die_info *imported_die, *child_die;
de4affc9 11251 struct dwarf2_cu *imported_cu;
27aa8d6a 11252 const char *imported_name;
794684b6 11253 const char *imported_name_prefix;
13387711
SW
11254 const char *canonical_name;
11255 const char *import_alias;
11256 const char *imported_declaration = NULL;
794684b6 11257 const char *import_prefix;
eb1e02fd 11258 std::vector<const char *> excludes;
13387711 11259
27aa8d6a
SW
11260 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11261 if (import_attr == NULL)
11262 {
b98664d3 11263 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11264 dwarf_tag_name (die->tag));
11265 return;
11266 }
11267
de4affc9
CC
11268 imported_cu = cu;
11269 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11270 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11271 if (imported_name == NULL)
11272 {
11273 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11274
11275 The import in the following code:
11276 namespace A
11277 {
11278 typedef int B;
11279 }
11280
11281 int main ()
11282 {
11283 using A::B;
11284 B b;
11285 return b;
11286 }
11287
11288 ...
11289 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11290 <52> DW_AT_decl_file : 1
11291 <53> DW_AT_decl_line : 6
11292 <54> DW_AT_import : <0x75>
11293 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11294 <59> DW_AT_name : B
11295 <5b> DW_AT_decl_file : 1
11296 <5c> DW_AT_decl_line : 2
11297 <5d> DW_AT_type : <0x6e>
11298 ...
11299 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11300 <76> DW_AT_byte_size : 4
11301 <77> DW_AT_encoding : 5 (signed)
11302
11303 imports the wrong die ( 0x75 instead of 0x58 ).
11304 This case will be ignored until the gcc bug is fixed. */
11305 return;
11306 }
11307
82856980
SW
11308 /* Figure out the local name after import. */
11309 import_alias = dwarf2_name (die, cu);
27aa8d6a 11310
794684b6
SW
11311 /* Figure out where the statement is being imported to. */
11312 import_prefix = determine_prefix (die, cu);
11313
11314 /* Figure out what the scope of the imported die is and prepend it
11315 to the name of the imported die. */
de4affc9 11316 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11317
f55ee35c
JK
11318 if (imported_die->tag != DW_TAG_namespace
11319 && imported_die->tag != DW_TAG_module)
794684b6 11320 {
13387711
SW
11321 imported_declaration = imported_name;
11322 canonical_name = imported_name_prefix;
794684b6 11323 }
13387711 11324 else if (strlen (imported_name_prefix) > 0)
12aaed36 11325 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11326 imported_name_prefix,
11327 (cu->language == language_d ? "." : "::"),
11328 imported_name, (char *) NULL);
13387711
SW
11329 else
11330 canonical_name = imported_name;
794684b6 11331
32019081
JK
11332 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11333 for (child_die = die->child; child_die && child_die->tag;
11334 child_die = sibling_die (child_die))
11335 {
11336 /* DWARF-4: A Fortran use statement with a “rename list” may be
11337 represented by an imported module entry with an import attribute
11338 referring to the module and owned entries corresponding to those
11339 entities that are renamed as part of being imported. */
11340
11341 if (child_die->tag != DW_TAG_imported_declaration)
11342 {
b98664d3 11343 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11344 "- DIE at %s [in module %s]"),
11345 sect_offset_str (child_die->sect_off),
11346 objfile_name (objfile));
32019081
JK
11347 continue;
11348 }
11349
11350 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11351 if (import_attr == NULL)
11352 {
b98664d3 11353 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11354 dwarf_tag_name (child_die->tag));
11355 continue;
11356 }
11357
11358 imported_cu = cu;
11359 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11360 &imported_cu);
11361 imported_name = dwarf2_name (imported_die, imported_cu);
11362 if (imported_name == NULL)
11363 {
b98664d3 11364 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11365 "imported name - DIE at %s [in module %s]"),
11366 sect_offset_str (child_die->sect_off),
11367 objfile_name (objfile));
32019081
JK
11368 continue;
11369 }
11370
eb1e02fd 11371 excludes.push_back (imported_name);
32019081
JK
11372
11373 process_die (child_die, cu);
11374 }
11375
804d2729 11376 add_using_directive (using_directives (cu),
22cee43f
PMR
11377 import_prefix,
11378 canonical_name,
11379 import_alias,
11380 imported_declaration,
11381 excludes,
11382 0,
11383 &objfile->objfile_obstack);
27aa8d6a
SW
11384}
11385
5230b05a
WT
11386/* ICC<14 does not output the required DW_AT_declaration on incomplete
11387 types, but gives them a size of zero. Starting with version 14,
11388 ICC is compatible with GCC. */
11389
9068261f 11390static bool
5230b05a
WT
11391producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11392{
11393 if (!cu->checked_producer)
11394 check_producer (cu);
11395
11396 return cu->producer_is_icc_lt_14;
11397}
11398
eb77c9df
AB
11399/* ICC generates a DW_AT_type for C void functions. This was observed on
11400 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11401 which says that void functions should not have a DW_AT_type. */
11402
11403static bool
11404producer_is_icc (struct dwarf2_cu *cu)
11405{
11406 if (!cu->checked_producer)
11407 check_producer (cu);
11408
11409 return cu->producer_is_icc;
11410}
11411
1b80a9fa
JK
11412/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11413 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11414 this, it was first present in GCC release 4.3.0. */
11415
9068261f 11416static bool
1b80a9fa
JK
11417producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11418{
11419 if (!cu->checked_producer)
11420 check_producer (cu);
11421
11422 return cu->producer_is_gcc_lt_4_3;
11423}
11424
d721ba37
PA
11425static file_and_directory
11426find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11427{
d721ba37
PA
11428 file_and_directory res;
11429
9291a0cd
TT
11430 /* Find the filename. Do not use dwarf2_name here, since the filename
11431 is not a source language identifier. */
d721ba37
PA
11432 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11433 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11434
d721ba37
PA
11435 if (res.comp_dir == NULL
11436 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11437 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11438 {
d721ba37
PA
11439 res.comp_dir_storage = ldirname (res.name);
11440 if (!res.comp_dir_storage.empty ())
11441 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11442 }
d721ba37 11443 if (res.comp_dir != NULL)
9291a0cd
TT
11444 {
11445 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11446 directory, get rid of it. */
d721ba37 11447 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11448
d721ba37
PA
11449 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11450 res.comp_dir = cp + 1;
9291a0cd
TT
11451 }
11452
d721ba37
PA
11453 if (res.name == NULL)
11454 res.name = "<unknown>";
11455
11456 return res;
9291a0cd
TT
11457}
11458
f4dc4d17
DE
11459/* Handle DW_AT_stmt_list for a compilation unit.
11460 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11461 COMP_DIR is the compilation directory. LOWPC is passed to
11462 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11463
11464static void
11465handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11466 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11467{
518817b3
SM
11468 struct dwarf2_per_objfile *dwarf2_per_objfile
11469 = cu->per_cu->dwarf2_per_objfile;
527f3840 11470 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11471 struct attribute *attr;
527f3840
JK
11472 struct line_header line_header_local;
11473 hashval_t line_header_local_hash;
527f3840
JK
11474 void **slot;
11475 int decode_mapping;
2ab95328 11476
f4dc4d17
DE
11477 gdb_assert (! cu->per_cu->is_debug_types);
11478
2ab95328 11479 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11480 if (attr == NULL)
11481 return;
11482
9c541725 11483 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11484
11485 /* The line header hash table is only created if needed (it exists to
11486 prevent redundant reading of the line table for partial_units).
11487 If we're given a partial_unit, we'll need it. If we're given a
11488 compile_unit, then use the line header hash table if it's already
11489 created, but don't create one just yet. */
11490
11491 if (dwarf2_per_objfile->line_header_hash == NULL
11492 && die->tag == DW_TAG_partial_unit)
2ab95328 11493 {
527f3840
JK
11494 dwarf2_per_objfile->line_header_hash
11495 = htab_create_alloc_ex (127, line_header_hash_voidp,
11496 line_header_eq_voidp,
11497 free_line_header_voidp,
11498 &objfile->objfile_obstack,
11499 hashtab_obstack_allocate,
11500 dummy_obstack_deallocate);
11501 }
2ab95328 11502
9c541725 11503 line_header_local.sect_off = line_offset;
527f3840
JK
11504 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11505 line_header_local_hash = line_header_hash (&line_header_local);
11506 if (dwarf2_per_objfile->line_header_hash != NULL)
11507 {
11508 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11509 &line_header_local,
11510 line_header_local_hash, NO_INSERT);
11511
11512 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11513 is not present in *SLOT (since if there is something in *SLOT then
11514 it will be for a partial_unit). */
11515 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11516 {
527f3840 11517 gdb_assert (*slot != NULL);
9a3c8263 11518 cu->line_header = (struct line_header *) *slot;
527f3840 11519 return;
dee91e82 11520 }
2ab95328 11521 }
527f3840
JK
11522
11523 /* dwarf_decode_line_header does not yet provide sufficient information.
11524 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11525 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11526 if (lh == NULL)
527f3840 11527 return;
4c8aa72d
PA
11528
11529 cu->line_header = lh.release ();
11530 cu->line_header_die_owner = die;
527f3840
JK
11531
11532 if (dwarf2_per_objfile->line_header_hash == NULL)
11533 slot = NULL;
11534 else
11535 {
11536 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11537 &line_header_local,
11538 line_header_local_hash, INSERT);
11539 gdb_assert (slot != NULL);
11540 }
11541 if (slot != NULL && *slot == NULL)
11542 {
11543 /* This newly decoded line number information unit will be owned
11544 by line_header_hash hash table. */
11545 *slot = cu->line_header;
4c8aa72d 11546 cu->line_header_die_owner = NULL;
527f3840
JK
11547 }
11548 else
11549 {
11550 /* We cannot free any current entry in (*slot) as that struct line_header
11551 may be already used by multiple CUs. Create only temporary decoded
11552 line_header for this CU - it may happen at most once for each line
11553 number information unit. And if we're not using line_header_hash
11554 then this is what we want as well. */
11555 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11556 }
11557 decode_mapping = (die->tag != DW_TAG_partial_unit);
11558 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11559 decode_mapping);
fff8551c 11560
2ab95328
TT
11561}
11562
95554aad 11563/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11564
c906108c 11565static void
e7c27a73 11566read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11567{
518817b3
SM
11568 struct dwarf2_per_objfile *dwarf2_per_objfile
11569 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11570 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11571 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11572 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11573 CORE_ADDR highpc = ((CORE_ADDR) 0);
11574 struct attribute *attr;
c906108c 11575 struct die_info *child_die;
e142c38c 11576 CORE_ADDR baseaddr;
6e70227d 11577
380618d6 11578 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11579 baseaddr = objfile->text_section_offset ();
c906108c 11580
fae299cd 11581 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11582
11583 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11584 from finish_block. */
2acceee2 11585 if (lowpc == ((CORE_ADDR) -1))
c906108c 11586 lowpc = highpc;
3e29f34a 11587 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11588
d721ba37 11589 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11590
f4b8a18d
KW
11591 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11592 standardised yet. As a workaround for the language detection we fall
11593 back to the DW_AT_producer string. */
11594 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11595 cu->language = language_opencl;
11596
3019eac3
DE
11597 /* Similar hack for Go. */
11598 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11599 set_cu_language (DW_LANG_Go, cu);
11600
c24bdb02 11601 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11602
11603 /* Decode line number information if present. We do this before
11604 processing child DIEs, so that the line header table is available
11605 for DW_AT_decl_file. */
d721ba37 11606 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11607
11608 /* Process all dies in compilation unit. */
11609 if (die->child != NULL)
11610 {
11611 child_die = die->child;
11612 while (child_die && child_die->tag)
11613 {
11614 process_die (child_die, cu);
11615 child_die = sibling_die (child_die);
11616 }
11617 }
11618
11619 /* Decode macro information, if present. Dwarf 2 macro information
11620 refers to information in the line number info statement program
11621 header, so we can only read it if we've read the header
11622 successfully. */
0af92d60
JK
11623 attr = dwarf2_attr (die, DW_AT_macros, cu);
11624 if (attr == NULL)
11625 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11626 if (attr && cu->line_header)
11627 {
11628 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11629 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11630
43f3e411 11631 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11632 }
11633 else
11634 {
11635 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11636 if (attr && cu->line_header)
11637 {
11638 unsigned int macro_offset = DW_UNSND (attr);
11639
43f3e411 11640 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11641 }
11642 }
3019eac3
DE
11643}
11644
c24bdb02
KS
11645void
11646dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11647{
f4dc4d17
DE
11648 struct type_unit_group *tu_group;
11649 int first_time;
3019eac3 11650 struct attribute *attr;
9c541725 11651 unsigned int i;
0186c6a7 11652 struct signatured_type *sig_type;
3019eac3 11653
f4dc4d17 11654 gdb_assert (per_cu->is_debug_types);
0186c6a7 11655 sig_type = (struct signatured_type *) per_cu;
3019eac3 11656
c24bdb02 11657 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11658
f4dc4d17 11659 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11660 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11661 if (sig_type->type_unit_group == NULL)
c24bdb02 11662 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11663 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11664
11665 /* If we've already processed this stmt_list there's no real need to
11666 do it again, we could fake it and just recreate the part we need
11667 (file name,index -> symtab mapping). If data shows this optimization
11668 is useful we can do it then. */
43f3e411 11669 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11670
11671 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11672 debug info. */
fff8551c 11673 line_header_up lh;
f4dc4d17 11674 if (attr != NULL)
3019eac3 11675 {
9c541725 11676 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11677 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11678 }
11679 if (lh == NULL)
11680 {
11681 if (first_time)
c24bdb02 11682 start_symtab ("", NULL, 0);
f4dc4d17
DE
11683 else
11684 {
11685 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11686 gdb_assert (m_builder == nullptr);
804d2729 11687 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11688 m_builder.reset (new struct buildsym_compunit
11689 (COMPUNIT_OBJFILE (cust), "",
11690 COMPUNIT_DIRNAME (cust),
11691 compunit_language (cust),
11692 0, cust));
f4dc4d17 11693 }
f4dc4d17 11694 return;
3019eac3
DE
11695 }
11696
c24bdb02
KS
11697 line_header = lh.release ();
11698 line_header_die_owner = die;
3019eac3 11699
f4dc4d17
DE
11700 if (first_time)
11701 {
c24bdb02 11702 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11703
1fd60fc0
DE
11704 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11705 still initializing it, and our caller (a few levels up)
11706 process_full_type_unit still needs to know if this is the first
11707 time. */
11708
7ba99d21 11709 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11710 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11711 line_header->file_names_size ());
3019eac3 11712
7ba99d21
AT
11713 auto &file_names = line_header->file_names ();
11714 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11715 {
7ba99d21 11716 file_entry &fe = file_names[i];
c24bdb02
KS
11717 dwarf2_start_subfile (this, fe.name,
11718 fe.include_dir (line_header));
11719 buildsym_compunit *b = get_builder ();
11720 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11721 {
4c8aa72d
PA
11722 /* NOTE: start_subfile will recognize when it's been
11723 passed a file it has already seen. So we can't
11724 assume there's a simple mapping from
11725 cu->line_header->file_names to subfiles, plus
11726 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11727 b->get_current_subfile ()->symtab
11728 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11729 }
11730
c24bdb02 11731 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11732 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11733 }
11734 }
11735 else
3019eac3 11736 {
c24bdb02 11737 gdb_assert (m_builder == nullptr);
804d2729 11738 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11739 m_builder.reset (new struct buildsym_compunit
11740 (COMPUNIT_OBJFILE (cust), "",
11741 COMPUNIT_DIRNAME (cust),
11742 compunit_language (cust),
11743 0, cust));
f4dc4d17 11744
7ba99d21
AT
11745 auto &file_names = line_header->file_names ();
11746 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11747 {
7ba99d21 11748 file_entry &fe = file_names[i];
4c8aa72d 11749 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11750 }
3019eac3
DE
11751 }
11752
f4dc4d17
DE
11753 /* The main symtab is allocated last. Type units don't have DW_AT_name
11754 so they don't have a "real" (so to speak) symtab anyway.
11755 There is later code that will assign the main symtab to all symbols
11756 that don't have one. We need to handle the case of a symbol with a
11757 missing symtab (DW_AT_decl_file) anyway. */
11758}
3019eac3 11759
f4dc4d17
DE
11760/* Process DW_TAG_type_unit.
11761 For TUs we want to skip the first top level sibling if it's not the
11762 actual type being defined by this TU. In this case the first top
11763 level sibling is there to provide context only. */
3019eac3 11764
f4dc4d17
DE
11765static void
11766read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11767{
11768 struct die_info *child_die;
3019eac3 11769
f4dc4d17
DE
11770 prepare_one_comp_unit (cu, die, language_minimal);
11771
11772 /* Initialize (or reinitialize) the machinery for building symtabs.
11773 We do this before processing child DIEs, so that the line header table
11774 is available for DW_AT_decl_file. */
c24bdb02 11775 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11776
11777 if (die->child != NULL)
11778 {
11779 child_die = die->child;
11780 while (child_die && child_die->tag)
11781 {
11782 process_die (child_die, cu);
11783 child_die = sibling_die (child_die);
11784 }
11785 }
3019eac3
DE
11786}
11787\f
80626a55
DE
11788/* DWO/DWP files.
11789
11790 http://gcc.gnu.org/wiki/DebugFission
11791 http://gcc.gnu.org/wiki/DebugFissionDWP
11792
11793 To simplify handling of both DWO files ("object" files with the DWARF info)
11794 and DWP files (a file with the DWOs packaged up into one file), we treat
11795 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11796
11797static hashval_t
11798hash_dwo_file (const void *item)
11799{
9a3c8263 11800 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11801 hashval_t hash;
3019eac3 11802
a2ce51a0
DE
11803 hash = htab_hash_string (dwo_file->dwo_name);
11804 if (dwo_file->comp_dir != NULL)
11805 hash += htab_hash_string (dwo_file->comp_dir);
11806 return hash;
3019eac3
DE
11807}
11808
11809static int
11810eq_dwo_file (const void *item_lhs, const void *item_rhs)
11811{
9a3c8263
SM
11812 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11813 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11814
a2ce51a0
DE
11815 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11816 return 0;
11817 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11818 return lhs->comp_dir == rhs->comp_dir;
11819 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11820}
11821
11822/* Allocate a hash table for DWO files. */
11823
51ac9db5 11824static htab_up
ed2dc618 11825allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11826{
51ac9db5
SM
11827 auto delete_dwo_file = [] (void *item)
11828 {
11829 struct dwo_file *dwo_file = (struct dwo_file *) item;
11830
11831 delete dwo_file;
11832 };
11833
11834 return htab_up (htab_create_alloc_ex (41,
11835 hash_dwo_file,
11836 eq_dwo_file,
11837 delete_dwo_file,
11838 &objfile->objfile_obstack,
11839 hashtab_obstack_allocate,
11840 dummy_obstack_deallocate));
3019eac3
DE
11841}
11842
80626a55
DE
11843/* Lookup DWO file DWO_NAME. */
11844
11845static void **
ed2dc618
SM
11846lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11847 const char *dwo_name,
11848 const char *comp_dir)
80626a55
DE
11849{
11850 struct dwo_file find_entry;
11851 void **slot;
11852
11853 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11854 dwarf2_per_objfile->dwo_files
11855 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11856
0ac5b59e
DE
11857 find_entry.dwo_name = dwo_name;
11858 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11859 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11860 INSERT);
80626a55
DE
11861
11862 return slot;
11863}
11864
3019eac3
DE
11865static hashval_t
11866hash_dwo_unit (const void *item)
11867{
9a3c8263 11868 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11869
11870 /* This drops the top 32 bits of the id, but is ok for a hash. */
11871 return dwo_unit->signature;
11872}
11873
11874static int
11875eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11876{
9a3c8263
SM
11877 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11878 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11879
11880 /* The signature is assumed to be unique within the DWO file.
11881 So while object file CU dwo_id's always have the value zero,
11882 that's OK, assuming each object file DWO file has only one CU,
11883 and that's the rule for now. */
11884 return lhs->signature == rhs->signature;
11885}
11886
11887/* Allocate a hash table for DWO CUs,TUs.
11888 There is one of these tables for each of CUs,TUs for each DWO file. */
11889
11890static htab_t
11891allocate_dwo_unit_table (struct objfile *objfile)
11892{
11893 /* Start out with a pretty small number.
11894 Generally DWO files contain only one CU and maybe some TUs. */
11895 return htab_create_alloc_ex (3,
11896 hash_dwo_unit,
11897 eq_dwo_unit,
11898 NULL,
11899 &objfile->objfile_obstack,
11900 hashtab_obstack_allocate,
11901 dummy_obstack_deallocate);
11902}
11903
19c3d4c9 11904/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11905
11906static void
19c3d4c9
DE
11907create_dwo_cu_reader (const struct die_reader_specs *reader,
11908 const gdb_byte *info_ptr,
11909 struct die_info *comp_unit_die,
11910 int has_children,
c0ab21c2
TT
11911 struct dwo_file *dwo_file,
11912 struct dwo_unit *dwo_unit)
3019eac3
DE
11913{
11914 struct dwarf2_cu *cu = reader->cu;
9c541725 11915 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11916 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11917
a084a2a6
AT
11918 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11919 if (!signature.has_value ())
3019eac3 11920 {
b98664d3 11921 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11922 " its dwo_id [in module %s]"),
9d8780f0 11923 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11924 return;
11925 }
11926
3019eac3 11927 dwo_unit->dwo_file = dwo_file;
a084a2a6 11928 dwo_unit->signature = *signature;
8a0459fd 11929 dwo_unit->section = section;
9c541725 11930 dwo_unit->sect_off = sect_off;
3019eac3
DE
11931 dwo_unit->length = cu->per_cu->length;
11932
b4f54984 11933 if (dwarf_read_debug)
9d8780f0
SM
11934 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11935 sect_offset_str (sect_off),
9c541725 11936 hex_string (dwo_unit->signature));
3019eac3
DE
11937}
11938
33c5cd75 11939/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11940 Note: This function processes DWO files only, not DWP files. */
3019eac3 11941
33c5cd75 11942static void
ed2dc618 11943create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
11944 dwarf2_cu *cu, struct dwo_file &dwo_file,
11945 dwarf2_section_info &section, htab_t &cus_htab)
3019eac3
DE
11946{
11947 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11948 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11949
33c5cd75
DB
11950 dwarf2_read_section (objfile, &section);
11951 info_ptr = section.buffer;
3019eac3
DE
11952
11953 if (info_ptr == NULL)
33c5cd75 11954 return;
3019eac3 11955
b4f54984 11956 if (dwarf_read_debug)
19c3d4c9
DE
11957 {
11958 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11959 get_section_name (&section),
11960 get_section_file_name (&section));
19c3d4c9 11961 }
3019eac3 11962
33c5cd75 11963 end_ptr = info_ptr + section.size;
3019eac3
DE
11964 while (info_ptr < end_ptr)
11965 {
11966 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11967 struct dwo_unit read_unit {};
33c5cd75
DB
11968 struct dwo_unit *dwo_unit;
11969 void **slot;
11970 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11971
11972 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11973 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11974 per_cu.is_debug_types = 0;
33c5cd75
DB
11975 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11976 per_cu.section = &section;
11977
c0ab21c2
TT
11978 cutu_reader reader (&per_cu, cu, &dwo_file);
11979 if (!reader.dummy_p)
11980 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11981 reader.has_children, &dwo_file, &read_unit);
33c5cd75
DB
11982 info_ptr += per_cu.length;
11983
11984 // If the unit could not be parsed, skip it.
c0ab21c2 11985 if (read_unit.dwo_file == NULL)
33c5cd75 11986 continue;
3019eac3 11987
33c5cd75
DB
11988 if (cus_htab == NULL)
11989 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11990
33c5cd75 11991 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11992 *dwo_unit = read_unit;
33c5cd75
DB
11993 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11994 gdb_assert (slot != NULL);
11995 if (*slot != NULL)
19c3d4c9 11996 {
33c5cd75
DB
11997 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11998 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11999
b98664d3 12000 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12001 " the entry at offset %s, signature %s"),
12002 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12003 hex_string (dwo_unit->signature));
19c3d4c9 12004 }
33c5cd75 12005 *slot = (void *)dwo_unit;
3019eac3 12006 }
3019eac3
DE
12007}
12008
80626a55
DE
12009/* DWP file .debug_{cu,tu}_index section format:
12010 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12011
d2415c6c
DE
12012 DWP Version 1:
12013
80626a55
DE
12014 Both index sections have the same format, and serve to map a 64-bit
12015 signature to a set of section numbers. Each section begins with a header,
12016 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12017 indexes, and a pool of 32-bit section numbers. The index sections will be
12018 aligned at 8-byte boundaries in the file.
12019
d2415c6c
DE
12020 The index section header consists of:
12021
12022 V, 32 bit version number
12023 -, 32 bits unused
12024 N, 32 bit number of compilation units or type units in the index
12025 M, 32 bit number of slots in the hash table
80626a55 12026
d2415c6c 12027 Numbers are recorded using the byte order of the application binary.
80626a55 12028
d2415c6c
DE
12029 The hash table begins at offset 16 in the section, and consists of an array
12030 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12031 order of the application binary). Unused slots in the hash table are 0.
12032 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12033
d2415c6c
DE
12034 The parallel table begins immediately after the hash table
12035 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12036 array of 32-bit indexes (using the byte order of the application binary),
12037 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12038 table contains a 32-bit index into the pool of section numbers. For unused
12039 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12040
73869dc2
DE
12041 The pool of section numbers begins immediately following the hash table
12042 (at offset 16 + 12 * M from the beginning of the section). The pool of
12043 section numbers consists of an array of 32-bit words (using the byte order
12044 of the application binary). Each item in the array is indexed starting
12045 from 0. The hash table entry provides the index of the first section
12046 number in the set. Additional section numbers in the set follow, and the
12047 set is terminated by a 0 entry (section number 0 is not used in ELF).
12048
12049 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12050 section must be the first entry in the set, and the .debug_abbrev.dwo must
12051 be the second entry. Other members of the set may follow in any order.
12052
12053 ---
12054
12055 DWP Version 2:
12056
12057 DWP Version 2 combines all the .debug_info, etc. sections into one,
12058 and the entries in the index tables are now offsets into these sections.
12059 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12060 section.
12061
12062 Index Section Contents:
12063 Header
12064 Hash Table of Signatures dwp_hash_table.hash_table
12065 Parallel Table of Indices dwp_hash_table.unit_table
12066 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12067 Table of Section Sizes dwp_hash_table.v2.sizes
12068
12069 The index section header consists of:
12070
12071 V, 32 bit version number
12072 L, 32 bit number of columns in the table of section offsets
12073 N, 32 bit number of compilation units or type units in the index
12074 M, 32 bit number of slots in the hash table
12075
12076 Numbers are recorded using the byte order of the application binary.
12077
12078 The hash table has the same format as version 1.
12079 The parallel table of indices has the same format as version 1,
12080 except that the entries are origin-1 indices into the table of sections
12081 offsets and the table of section sizes.
12082
12083 The table of offsets begins immediately following the parallel table
12084 (at offset 16 + 12 * M from the beginning of the section). The table is
12085 a two-dimensional array of 32-bit words (using the byte order of the
12086 application binary), with L columns and N+1 rows, in row-major order.
12087 Each row in the array is indexed starting from 0. The first row provides
12088 a key to the remaining rows: each column in this row provides an identifier
12089 for a debug section, and the offsets in the same column of subsequent rows
12090 refer to that section. The section identifiers are:
12091
12092 DW_SECT_INFO 1 .debug_info.dwo
12093 DW_SECT_TYPES 2 .debug_types.dwo
12094 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12095 DW_SECT_LINE 4 .debug_line.dwo
12096 DW_SECT_LOC 5 .debug_loc.dwo
12097 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12098 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12099 DW_SECT_MACRO 8 .debug_macro.dwo
12100
12101 The offsets provided by the CU and TU index sections are the base offsets
12102 for the contributions made by each CU or TU to the corresponding section
12103 in the package file. Each CU and TU header contains an abbrev_offset
12104 field, used to find the abbreviations table for that CU or TU within the
12105 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12106 be interpreted as relative to the base offset given in the index section.
12107 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12108 should be interpreted as relative to the base offset for .debug_line.dwo,
12109 and offsets into other debug sections obtained from DWARF attributes should
12110 also be interpreted as relative to the corresponding base offset.
12111
12112 The table of sizes begins immediately following the table of offsets.
12113 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12114 with L columns and N rows, in row-major order. Each row in the array is
12115 indexed starting from 1 (row 0 is shared by the two tables).
12116
12117 ---
12118
12119 Hash table lookup is handled the same in version 1 and 2:
12120
12121 We assume that N and M will not exceed 2^32 - 1.
12122 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12123
d2415c6c
DE
12124 Given a 64-bit compilation unit signature or a type signature S, an entry
12125 in the hash table is located as follows:
80626a55 12126
d2415c6c
DE
12127 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12128 the low-order k bits all set to 1.
80626a55 12129
d2415c6c 12130 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12131
d2415c6c
DE
12132 3) If the hash table entry at index H matches the signature, use that
12133 entry. If the hash table entry at index H is unused (all zeroes),
12134 terminate the search: the signature is not present in the table.
80626a55 12135
d2415c6c 12136 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12137
d2415c6c 12138 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12139 to stop at an unused slot or find the match. */
80626a55
DE
12140
12141/* Create a hash table to map DWO IDs to their CU/TU entry in
12142 .debug_{info,types}.dwo in DWP_FILE.
12143 Returns NULL if there isn't one.
12144 Note: This function processes DWP files only, not DWO files. */
12145
12146static struct dwp_hash_table *
ed2dc618
SM
12147create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12148 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12149{
12150 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12151 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12152 const gdb_byte *index_ptr, *index_end;
80626a55 12153 struct dwarf2_section_info *index;
73869dc2 12154 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12155 struct dwp_hash_table *htab;
12156
12157 if (is_debug_types)
12158 index = &dwp_file->sections.tu_index;
12159 else
12160 index = &dwp_file->sections.cu_index;
12161
12162 if (dwarf2_section_empty_p (index))
12163 return NULL;
12164 dwarf2_read_section (objfile, index);
12165
12166 index_ptr = index->buffer;
12167 index_end = index_ptr + index->size;
12168
12169 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12170 index_ptr += 4;
12171 if (version == 2)
12172 nr_columns = read_4_bytes (dbfd, index_ptr);
12173 else
12174 nr_columns = 0;
12175 index_ptr += 4;
80626a55
DE
12176 nr_units = read_4_bytes (dbfd, index_ptr);
12177 index_ptr += 4;
12178 nr_slots = read_4_bytes (dbfd, index_ptr);
12179 index_ptr += 4;
12180
73869dc2 12181 if (version != 1 && version != 2)
80626a55 12182 {
21aa081e 12183 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12184 " [in module %s]"),
21aa081e 12185 pulongest (version), dwp_file->name);
80626a55
DE
12186 }
12187 if (nr_slots != (nr_slots & -nr_slots))
12188 {
21aa081e 12189 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12190 " is not power of 2 [in module %s]"),
21aa081e 12191 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12192 }
12193
12194 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12195 htab->version = version;
12196 htab->nr_columns = nr_columns;
80626a55
DE
12197 htab->nr_units = nr_units;
12198 htab->nr_slots = nr_slots;
12199 htab->hash_table = index_ptr;
12200 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12201
12202 /* Exit early if the table is empty. */
12203 if (nr_slots == 0 || nr_units == 0
12204 || (version == 2 && nr_columns == 0))
12205 {
12206 /* All must be zero. */
12207 if (nr_slots != 0 || nr_units != 0
12208 || (version == 2 && nr_columns != 0))
12209 {
b98664d3 12210 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12211 " all zero [in modules %s]"),
12212 dwp_file->name);
12213 }
12214 return htab;
12215 }
12216
12217 if (version == 1)
12218 {
12219 htab->section_pool.v1.indices =
12220 htab->unit_table + sizeof (uint32_t) * nr_slots;
12221 /* It's harder to decide whether the section is too small in v1.
12222 V1 is deprecated anyway so we punt. */
12223 }
12224 else
12225 {
12226 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12227 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12228 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12229 /* Reverse map for error checking. */
12230 int ids_seen[DW_SECT_MAX + 1];
12231 int i;
12232
12233 if (nr_columns < 2)
12234 {
12235 error (_("Dwarf Error: bad DWP hash table, too few columns"
12236 " in section table [in module %s]"),
12237 dwp_file->name);
12238 }
12239 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12240 {
12241 error (_("Dwarf Error: bad DWP hash table, too many columns"
12242 " in section table [in module %s]"),
12243 dwp_file->name);
12244 }
04fd5eed
GB
12245 memset (ids, 255, sizeof_ids);
12246 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12247 for (i = 0; i < nr_columns; ++i)
12248 {
12249 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12250
12251 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12252 {
12253 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12254 " in section table [in module %s]"),
12255 id, dwp_file->name);
12256 }
12257 if (ids_seen[id] != -1)
12258 {
12259 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12260 " id %d in section table [in module %s]"),
12261 id, dwp_file->name);
12262 }
12263 ids_seen[id] = i;
12264 ids[i] = id;
12265 }
12266 /* Must have exactly one info or types section. */
12267 if (((ids_seen[DW_SECT_INFO] != -1)
12268 + (ids_seen[DW_SECT_TYPES] != -1))
12269 != 1)
12270 {
12271 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12272 " DWO info/types section [in module %s]"),
12273 dwp_file->name);
12274 }
12275 /* Must have an abbrev section. */
12276 if (ids_seen[DW_SECT_ABBREV] == -1)
12277 {
12278 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12279 " section [in module %s]"),
12280 dwp_file->name);
12281 }
12282 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12283 htab->section_pool.v2.sizes =
12284 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12285 * nr_units * nr_columns);
12286 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12287 * nr_units * nr_columns))
12288 > index_end)
12289 {
12290 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12291 " [in module %s]"),
12292 dwp_file->name);
12293 }
12294 }
80626a55
DE
12295
12296 return htab;
12297}
12298
12299/* Update SECTIONS with the data from SECTP.
12300
12301 This function is like the other "locate" section routines that are
12302 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12303 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12304
12305 The result is non-zero for success, or zero if an error was found. */
12306
12307static int
73869dc2
DE
12308locate_v1_virtual_dwo_sections (asection *sectp,
12309 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12310{
12311 const struct dwop_section_names *names = &dwop_section_names;
12312
12313 if (section_is_p (sectp->name, &names->abbrev_dwo))
12314 {
12315 /* There can be only one. */
049412e3 12316 if (sections->abbrev.s.section != NULL)
80626a55 12317 return 0;
049412e3 12318 sections->abbrev.s.section = sectp;
fd361982 12319 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12320 }
12321 else if (section_is_p (sectp->name, &names->info_dwo)
12322 || section_is_p (sectp->name, &names->types_dwo))
12323 {
12324 /* There can be only one. */
049412e3 12325 if (sections->info_or_types.s.section != NULL)
80626a55 12326 return 0;
049412e3 12327 sections->info_or_types.s.section = sectp;
fd361982 12328 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12329 }
12330 else if (section_is_p (sectp->name, &names->line_dwo))
12331 {
12332 /* There can be only one. */
049412e3 12333 if (sections->line.s.section != NULL)
80626a55 12334 return 0;
049412e3 12335 sections->line.s.section = sectp;
fd361982 12336 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12337 }
12338 else if (section_is_p (sectp->name, &names->loc_dwo))
12339 {
12340 /* There can be only one. */
049412e3 12341 if (sections->loc.s.section != NULL)
80626a55 12342 return 0;
049412e3 12343 sections->loc.s.section = sectp;
fd361982 12344 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12345 }
12346 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12347 {
12348 /* There can be only one. */
049412e3 12349 if (sections->macinfo.s.section != NULL)
80626a55 12350 return 0;
049412e3 12351 sections->macinfo.s.section = sectp;
fd361982 12352 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12353 }
12354 else if (section_is_p (sectp->name, &names->macro_dwo))
12355 {
12356 /* There can be only one. */
049412e3 12357 if (sections->macro.s.section != NULL)
80626a55 12358 return 0;
049412e3 12359 sections->macro.s.section = sectp;
fd361982 12360 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12361 }
12362 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12363 {
12364 /* There can be only one. */
049412e3 12365 if (sections->str_offsets.s.section != NULL)
80626a55 12366 return 0;
049412e3 12367 sections->str_offsets.s.section = sectp;
fd361982 12368 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12369 }
12370 else
12371 {
12372 /* No other kind of section is valid. */
12373 return 0;
12374 }
12375
12376 return 1;
12377}
12378
73869dc2
DE
12379/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12380 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12381 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12382 This is for DWP version 1 files. */
80626a55
DE
12383
12384static struct dwo_unit *
ed2dc618
SM
12385create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12386 struct dwp_file *dwp_file,
73869dc2
DE
12387 uint32_t unit_index,
12388 const char *comp_dir,
12389 ULONGEST signature, int is_debug_types)
80626a55
DE
12390{
12391 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12392 const struct dwp_hash_table *dwp_htab =
12393 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12394 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12395 const char *kind = is_debug_types ? "TU" : "CU";
12396 struct dwo_file *dwo_file;
12397 struct dwo_unit *dwo_unit;
73869dc2 12398 struct virtual_v1_dwo_sections sections;
80626a55 12399 void **dwo_file_slot;
80626a55
DE
12400 int i;
12401
73869dc2
DE
12402 gdb_assert (dwp_file->version == 1);
12403
b4f54984 12404 if (dwarf_read_debug)
80626a55 12405 {
73869dc2 12406 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12407 kind,
73869dc2 12408 pulongest (unit_index), hex_string (signature),
80626a55
DE
12409 dwp_file->name);
12410 }
12411
19ac8c2e 12412 /* Fetch the sections of this DWO unit.
80626a55
DE
12413 Put a limit on the number of sections we look for so that bad data
12414 doesn't cause us to loop forever. */
12415
73869dc2 12416#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12417 (1 /* .debug_info or .debug_types */ \
12418 + 1 /* .debug_abbrev */ \
12419 + 1 /* .debug_line */ \
12420 + 1 /* .debug_loc */ \
12421 + 1 /* .debug_str_offsets */ \
19ac8c2e 12422 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12423 + 1 /* trailing zero */)
12424
12425 memset (&sections, 0, sizeof (sections));
80626a55 12426
73869dc2 12427 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12428 {
12429 asection *sectp;
12430 uint32_t section_nr =
12431 read_4_bytes (dbfd,
73869dc2
DE
12432 dwp_htab->section_pool.v1.indices
12433 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12434
12435 if (section_nr == 0)
12436 break;
12437 if (section_nr >= dwp_file->num_sections)
12438 {
12439 error (_("Dwarf Error: bad DWP hash table, section number too large"
12440 " [in module %s]"),
12441 dwp_file->name);
12442 }
12443
12444 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12445 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12446 {
12447 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12448 " [in module %s]"),
12449 dwp_file->name);
12450 }
12451 }
12452
12453 if (i < 2
a32a8923
DE
12454 || dwarf2_section_empty_p (&sections.info_or_types)
12455 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12456 {
12457 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12458 " [in module %s]"),
12459 dwp_file->name);
12460 }
73869dc2 12461 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12462 {
12463 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12464 " [in module %s]"),
12465 dwp_file->name);
12466 }
12467
12468 /* It's easier for the rest of the code if we fake a struct dwo_file and
12469 have dwo_unit "live" in that. At least for now.
12470
12471 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12472 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12473 file, we can combine them back into a virtual DWO file to save space
12474 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12475 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12476
791afaa2
TT
12477 std::string virtual_dwo_name =
12478 string_printf ("virtual-dwo/%d-%d-%d-%d",
12479 get_section_id (&sections.abbrev),
12480 get_section_id (&sections.line),
12481 get_section_id (&sections.loc),
12482 get_section_id (&sections.str_offsets));
80626a55 12483 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12484 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12485 virtual_dwo_name.c_str (),
12486 comp_dir);
80626a55
DE
12487 /* Create one if necessary. */
12488 if (*dwo_file_slot == NULL)
12489 {
b4f54984 12490 if (dwarf_read_debug)
80626a55
DE
12491 {
12492 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12493 virtual_dwo_name.c_str ());
80626a55 12494 }
51ac9db5 12495 dwo_file = new struct dwo_file;
efba19b0
TT
12496 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12497 virtual_dwo_name);
0ac5b59e 12498 dwo_file->comp_dir = comp_dir;
80626a55
DE
12499 dwo_file->sections.abbrev = sections.abbrev;
12500 dwo_file->sections.line = sections.line;
12501 dwo_file->sections.loc = sections.loc;
12502 dwo_file->sections.macinfo = sections.macinfo;
12503 dwo_file->sections.macro = sections.macro;
12504 dwo_file->sections.str_offsets = sections.str_offsets;
12505 /* The "str" section is global to the entire DWP file. */
12506 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12507 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12508 there's no need to record it in dwo_file.
12509 Also, we can't simply record type sections in dwo_file because
12510 we record a pointer into the vector in dwo_unit. As we collect more
12511 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12512 for it, invalidating all copies of pointers into the previous
12513 contents. */
80626a55
DE
12514 *dwo_file_slot = dwo_file;
12515 }
12516 else
12517 {
b4f54984 12518 if (dwarf_read_debug)
80626a55
DE
12519 {
12520 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12521 virtual_dwo_name.c_str ());
80626a55 12522 }
9a3c8263 12523 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12524 }
80626a55
DE
12525
12526 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12527 dwo_unit->dwo_file = dwo_file;
12528 dwo_unit->signature = signature;
8d749320
SM
12529 dwo_unit->section =
12530 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12531 *dwo_unit->section = sections.info_or_types;
57d63ce2 12532 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12533
12534 return dwo_unit;
12535}
12536
73869dc2
DE
12537/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12538 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12539 piece within that section used by a TU/CU, return a virtual section
12540 of just that piece. */
12541
12542static struct dwarf2_section_info
ed2dc618
SM
12543create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12544 struct dwarf2_section_info *section,
73869dc2
DE
12545 bfd_size_type offset, bfd_size_type size)
12546{
12547 struct dwarf2_section_info result;
12548 asection *sectp;
12549
12550 gdb_assert (section != NULL);
12551 gdb_assert (!section->is_virtual);
12552
12553 memset (&result, 0, sizeof (result));
12554 result.s.containing_section = section;
dc4ccb6f 12555 result.is_virtual = true;
73869dc2
DE
12556
12557 if (size == 0)
12558 return result;
12559
12560 sectp = get_section_bfd_section (section);
12561
12562 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12563 bounds of the real section. This is a pretty-rare event, so just
12564 flag an error (easier) instead of a warning and trying to cope. */
12565 if (sectp == NULL
fd361982 12566 || offset + size > bfd_section_size (sectp))
73869dc2 12567 {
73869dc2
DE
12568 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12569 " in section %s [in module %s]"),
fd361982 12570 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12571 objfile_name (dwarf2_per_objfile->objfile));
12572 }
12573
12574 result.virtual_offset = offset;
12575 result.size = size;
12576 return result;
12577}
12578
12579/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12580 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12581 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12582 This is for DWP version 2 files. */
12583
12584static struct dwo_unit *
ed2dc618
SM
12585create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12586 struct dwp_file *dwp_file,
73869dc2
DE
12587 uint32_t unit_index,
12588 const char *comp_dir,
12589 ULONGEST signature, int is_debug_types)
12590{
12591 struct objfile *objfile = dwarf2_per_objfile->objfile;
12592 const struct dwp_hash_table *dwp_htab =
12593 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12594 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12595 const char *kind = is_debug_types ? "TU" : "CU";
12596 struct dwo_file *dwo_file;
12597 struct dwo_unit *dwo_unit;
12598 struct virtual_v2_dwo_sections sections;
12599 void **dwo_file_slot;
73869dc2
DE
12600 int i;
12601
12602 gdb_assert (dwp_file->version == 2);
12603
b4f54984 12604 if (dwarf_read_debug)
73869dc2
DE
12605 {
12606 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12607 kind,
12608 pulongest (unit_index), hex_string (signature),
12609 dwp_file->name);
12610 }
12611
12612 /* Fetch the section offsets of this DWO unit. */
12613
12614 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12615
12616 for (i = 0; i < dwp_htab->nr_columns; ++i)
12617 {
12618 uint32_t offset = read_4_bytes (dbfd,
12619 dwp_htab->section_pool.v2.offsets
12620 + (((unit_index - 1) * dwp_htab->nr_columns
12621 + i)
12622 * sizeof (uint32_t)));
12623 uint32_t size = read_4_bytes (dbfd,
12624 dwp_htab->section_pool.v2.sizes
12625 + (((unit_index - 1) * dwp_htab->nr_columns
12626 + i)
12627 * sizeof (uint32_t)));
12628
12629 switch (dwp_htab->section_pool.v2.section_ids[i])
12630 {
12631 case DW_SECT_INFO:
12632 case DW_SECT_TYPES:
12633 sections.info_or_types_offset = offset;
12634 sections.info_or_types_size = size;
12635 break;
12636 case DW_SECT_ABBREV:
12637 sections.abbrev_offset = offset;
12638 sections.abbrev_size = size;
12639 break;
12640 case DW_SECT_LINE:
12641 sections.line_offset = offset;
12642 sections.line_size = size;
12643 break;
12644 case DW_SECT_LOC:
12645 sections.loc_offset = offset;
12646 sections.loc_size = size;
12647 break;
12648 case DW_SECT_STR_OFFSETS:
12649 sections.str_offsets_offset = offset;
12650 sections.str_offsets_size = size;
12651 break;
12652 case DW_SECT_MACINFO:
12653 sections.macinfo_offset = offset;
12654 sections.macinfo_size = size;
12655 break;
12656 case DW_SECT_MACRO:
12657 sections.macro_offset = offset;
12658 sections.macro_size = size;
12659 break;
12660 }
12661 }
12662
12663 /* It's easier for the rest of the code if we fake a struct dwo_file and
12664 have dwo_unit "live" in that. At least for now.
12665
12666 The DWP file can be made up of a random collection of CUs and TUs.
12667 However, for each CU + set of TUs that came from the same original DWO
12668 file, we can combine them back into a virtual DWO file to save space
12669 (fewer struct dwo_file objects to allocate). Remember that for really
12670 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12671
791afaa2
TT
12672 std::string virtual_dwo_name =
12673 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12674 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12675 (long) (sections.line_size ? sections.line_offset : 0),
12676 (long) (sections.loc_size ? sections.loc_offset : 0),
12677 (long) (sections.str_offsets_size
12678 ? sections.str_offsets_offset : 0));
73869dc2 12679 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12680 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12681 virtual_dwo_name.c_str (),
12682 comp_dir);
73869dc2
DE
12683 /* Create one if necessary. */
12684 if (*dwo_file_slot == NULL)
12685 {
b4f54984 12686 if (dwarf_read_debug)
73869dc2
DE
12687 {
12688 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12689 virtual_dwo_name.c_str ());
73869dc2 12690 }
51ac9db5 12691 dwo_file = new struct dwo_file;
efba19b0
TT
12692 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12693 virtual_dwo_name);
73869dc2
DE
12694 dwo_file->comp_dir = comp_dir;
12695 dwo_file->sections.abbrev =
ed2dc618 12696 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12697 sections.abbrev_offset, sections.abbrev_size);
12698 dwo_file->sections.line =
ed2dc618 12699 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12700 sections.line_offset, sections.line_size);
12701 dwo_file->sections.loc =
ed2dc618 12702 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12703 sections.loc_offset, sections.loc_size);
12704 dwo_file->sections.macinfo =
ed2dc618 12705 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12706 sections.macinfo_offset, sections.macinfo_size);
12707 dwo_file->sections.macro =
ed2dc618 12708 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12709 sections.macro_offset, sections.macro_size);
12710 dwo_file->sections.str_offsets =
ed2dc618
SM
12711 create_dwp_v2_section (dwarf2_per_objfile,
12712 &dwp_file->sections.str_offsets,
73869dc2
DE
12713 sections.str_offsets_offset,
12714 sections.str_offsets_size);
12715 /* The "str" section is global to the entire DWP file. */
12716 dwo_file->sections.str = dwp_file->sections.str;
12717 /* The info or types section is assigned below to dwo_unit,
12718 there's no need to record it in dwo_file.
12719 Also, we can't simply record type sections in dwo_file because
12720 we record a pointer into the vector in dwo_unit. As we collect more
12721 types we'll grow the vector and eventually have to reallocate space
12722 for it, invalidating all copies of pointers into the previous
12723 contents. */
12724 *dwo_file_slot = dwo_file;
12725 }
12726 else
12727 {
b4f54984 12728 if (dwarf_read_debug)
73869dc2
DE
12729 {
12730 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12731 virtual_dwo_name.c_str ());
73869dc2 12732 }
9a3c8263 12733 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12734 }
73869dc2
DE
12735
12736 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12737 dwo_unit->dwo_file = dwo_file;
12738 dwo_unit->signature = signature;
8d749320
SM
12739 dwo_unit->section =
12740 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12741 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12742 is_debug_types
73869dc2
DE
12743 ? &dwp_file->sections.types
12744 : &dwp_file->sections.info,
12745 sections.info_or_types_offset,
12746 sections.info_or_types_size);
12747 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12748
12749 return dwo_unit;
12750}
12751
57d63ce2
DE
12752/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12753 Returns NULL if the signature isn't found. */
80626a55
DE
12754
12755static struct dwo_unit *
ed2dc618
SM
12756lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12757 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12758 ULONGEST signature, int is_debug_types)
80626a55 12759{
57d63ce2
DE
12760 const struct dwp_hash_table *dwp_htab =
12761 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12762 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12763 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12764 uint32_t hash = signature & mask;
12765 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12766 unsigned int i;
12767 void **slot;
870f88f7 12768 struct dwo_unit find_dwo_cu;
80626a55
DE
12769
12770 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12771 find_dwo_cu.signature = signature;
19ac8c2e
DE
12772 slot = htab_find_slot (is_debug_types
12773 ? dwp_file->loaded_tus
12774 : dwp_file->loaded_cus,
12775 &find_dwo_cu, INSERT);
80626a55
DE
12776
12777 if (*slot != NULL)
9a3c8263 12778 return (struct dwo_unit *) *slot;
80626a55
DE
12779
12780 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12781 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12782 {
12783 ULONGEST signature_in_table;
12784
12785 signature_in_table =
57d63ce2 12786 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12787 if (signature_in_table == signature)
12788 {
57d63ce2
DE
12789 uint32_t unit_index =
12790 read_4_bytes (dbfd,
12791 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12792
73869dc2
DE
12793 if (dwp_file->version == 1)
12794 {
ed2dc618
SM
12795 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12796 dwp_file, unit_index,
73869dc2
DE
12797 comp_dir, signature,
12798 is_debug_types);
12799 }
12800 else
12801 {
ed2dc618
SM
12802 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12803 dwp_file, unit_index,
73869dc2
DE
12804 comp_dir, signature,
12805 is_debug_types);
12806 }
9a3c8263 12807 return (struct dwo_unit *) *slot;
80626a55
DE
12808 }
12809 if (signature_in_table == 0)
12810 return NULL;
12811 hash = (hash + hash2) & mask;
12812 }
12813
12814 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12815 " [in module %s]"),
12816 dwp_file->name);
12817}
12818
ab5088bf 12819/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12820 Open the file specified by FILE_NAME and hand it off to BFD for
12821 preliminary analysis. Return a newly initialized bfd *, which
12822 includes a canonicalized copy of FILE_NAME.
80626a55 12823 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12824 SEARCH_CWD is true if the current directory is to be searched.
12825 It will be searched before debug-file-directory.
13aaf454
DE
12826 If successful, the file is added to the bfd include table of the
12827 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12828 If unable to find/open the file, return NULL.
3019eac3
DE
12829 NOTE: This function is derived from symfile_bfd_open. */
12830
192b62ce 12831static gdb_bfd_ref_ptr
ed2dc618
SM
12832try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12833 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12834{
24b9144d 12835 int desc;
9c02c129
DE
12836 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12837 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12838 to debug_file_directory. */
e0cc99a6 12839 const char *search_path;
9c02c129
DE
12840 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12841
e0cc99a6 12842 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12843 if (search_cwd)
12844 {
12845 if (*debug_file_directory != '\0')
e0cc99a6
TT
12846 {
12847 search_path_holder.reset (concat (".", dirname_separator_string,
12848 debug_file_directory,
12849 (char *) NULL));
12850 search_path = search_path_holder.get ();
12851 }
6ac97d4c 12852 else
e0cc99a6 12853 search_path = ".";
6ac97d4c 12854 }
9c02c129 12855 else
e0cc99a6 12856 search_path = debug_file_directory;
3019eac3 12857
24b9144d 12858 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12859 if (is_dwp)
12860 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12861
12862 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12863 desc = openp (search_path, flags, file_name,
3019eac3
DE
12864 O_RDONLY | O_BINARY, &absolute_name);
12865 if (desc < 0)
12866 return NULL;
12867
e0cc99a6
TT
12868 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12869 gnutarget, desc));
9c02c129
DE
12870 if (sym_bfd == NULL)
12871 return NULL;
192b62ce 12872 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12873
192b62ce
TT
12874 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12875 return NULL;
3019eac3 12876
13aaf454
DE
12877 /* Success. Record the bfd as having been included by the objfile's bfd.
12878 This is important because things like demangled_names_hash lives in the
12879 objfile's per_bfd space and may have references to things like symbol
12880 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12881 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12882
3019eac3
DE
12883 return sym_bfd;
12884}
12885
ab5088bf 12886/* Try to open DWO file FILE_NAME.
3019eac3
DE
12887 COMP_DIR is the DW_AT_comp_dir attribute.
12888 The result is the bfd handle of the file.
12889 If there is a problem finding or opening the file, return NULL.
12890 Upon success, the canonicalized path of the file is stored in the bfd,
12891 same as symfile_bfd_open. */
12892
192b62ce 12893static gdb_bfd_ref_ptr
ed2dc618
SM
12894open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12895 const char *file_name, const char *comp_dir)
3019eac3 12896{
80626a55 12897 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12898 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12899 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12900
12901 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12902
12903 if (comp_dir != NULL)
12904 {
43816ebc
TT
12905 gdb::unique_xmalloc_ptr<char> path_to_try
12906 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12907
12908 /* NOTE: If comp_dir is a relative path, this will also try the
12909 search path, which seems useful. */
ed2dc618 12910 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12911 path_to_try.get (),
ed2dc618 12912 0 /*is_dwp*/,
192b62ce 12913 1 /*search_cwd*/));
3019eac3
DE
12914 if (abfd != NULL)
12915 return abfd;
12916 }
12917
12918 /* That didn't work, try debug-file-directory, which, despite its name,
12919 is a list of paths. */
12920
12921 if (*debug_file_directory == '\0')
12922 return NULL;
12923
ed2dc618
SM
12924 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12925 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12926}
12927
80626a55
DE
12928/* This function is mapped across the sections and remembers the offset and
12929 size of each of the DWO debugging sections we are interested in. */
12930
12931static void
12932dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12933{
9a3c8263 12934 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12935 const struct dwop_section_names *names = &dwop_section_names;
12936
12937 if (section_is_p (sectp->name, &names->abbrev_dwo))
12938 {
049412e3 12939 dwo_sections->abbrev.s.section = sectp;
fd361982 12940 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12941 }
12942 else if (section_is_p (sectp->name, &names->info_dwo))
12943 {
049412e3 12944 dwo_sections->info.s.section = sectp;
fd361982 12945 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12946 }
12947 else if (section_is_p (sectp->name, &names->line_dwo))
12948 {
049412e3 12949 dwo_sections->line.s.section = sectp;
fd361982 12950 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12951 }
12952 else if (section_is_p (sectp->name, &names->loc_dwo))
12953 {
049412e3 12954 dwo_sections->loc.s.section = sectp;
fd361982 12955 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12956 }
12957 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12958 {
049412e3 12959 dwo_sections->macinfo.s.section = sectp;
fd361982 12960 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12961 }
12962 else if (section_is_p (sectp->name, &names->macro_dwo))
12963 {
049412e3 12964 dwo_sections->macro.s.section = sectp;
fd361982 12965 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12966 }
12967 else if (section_is_p (sectp->name, &names->str_dwo))
12968 {
049412e3 12969 dwo_sections->str.s.section = sectp;
fd361982 12970 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12971 }
12972 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12973 {
049412e3 12974 dwo_sections->str_offsets.s.section = sectp;
fd361982 12975 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12976 }
12977 else if (section_is_p (sectp->name, &names->types_dwo))
12978 {
12979 struct dwarf2_section_info type_section;
12980
12981 memset (&type_section, 0, sizeof (type_section));
049412e3 12982 type_section.s.section = sectp;
fd361982 12983 type_section.size = bfd_section_size (sectp);
fd5866f6 12984 dwo_sections->types.push_back (type_section);
80626a55
DE
12985 }
12986}
12987
ab5088bf 12988/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12989 by PER_CU. This is for the non-DWP case.
80626a55 12990 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12991
12992static struct dwo_file *
0ac5b59e
DE
12993open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12994 const char *dwo_name, const char *comp_dir)
3019eac3 12995{
ed2dc618 12996 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12997
fb1eb2f9 12998 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12999 if (dbfd == NULL)
13000 {
b4f54984 13001 if (dwarf_read_debug)
80626a55
DE
13002 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13003 return NULL;
13004 }
263db9a1 13005
51ac9db5 13006 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13007 dwo_file->dwo_name = dwo_name;
13008 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13009 dwo_file->dbfd = std::move (dbfd);
3019eac3 13010
fb1eb2f9 13011 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13012 &dwo_file->sections);
3019eac3 13013
18a8505e
AT
13014 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
13015 dwo_file->sections.info, dwo_file->cus);
3019eac3 13016
263db9a1 13017 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13018 dwo_file->sections.types, dwo_file->tus);
3019eac3 13019
b4f54984 13020 if (dwarf_read_debug)
80626a55
DE
13021 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13022
263db9a1 13023 return dwo_file.release ();
3019eac3
DE
13024}
13025
80626a55 13026/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13027 size of each of the DWP debugging sections common to version 1 and 2 that
13028 we are interested in. */
3019eac3 13029
80626a55 13030static void
73869dc2
DE
13031dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13032 void *dwp_file_ptr)
3019eac3 13033{
9a3c8263 13034 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13035 const struct dwop_section_names *names = &dwop_section_names;
13036 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13037
80626a55 13038 /* Record the ELF section number for later lookup: this is what the
73869dc2 13039 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13040 gdb_assert (elf_section_nr < dwp_file->num_sections);
13041 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13042
80626a55
DE
13043 /* Look for specific sections that we need. */
13044 if (section_is_p (sectp->name, &names->str_dwo))
13045 {
049412e3 13046 dwp_file->sections.str.s.section = sectp;
fd361982 13047 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13048 }
13049 else if (section_is_p (sectp->name, &names->cu_index))
13050 {
049412e3 13051 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13052 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13053 }
13054 else if (section_is_p (sectp->name, &names->tu_index))
13055 {
049412e3 13056 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13057 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13058 }
13059}
3019eac3 13060
73869dc2
DE
13061/* This function is mapped across the sections and remembers the offset and
13062 size of each of the DWP version 2 debugging sections that we are interested
13063 in. This is split into a separate function because we don't know if we
13064 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13065
13066static void
13067dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13068{
9a3c8263 13069 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13070 const struct dwop_section_names *names = &dwop_section_names;
13071 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13072
13073 /* Record the ELF section number for later lookup: this is what the
13074 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13075 gdb_assert (elf_section_nr < dwp_file->num_sections);
13076 dwp_file->elf_sections[elf_section_nr] = sectp;
13077
13078 /* Look for specific sections that we need. */
13079 if (section_is_p (sectp->name, &names->abbrev_dwo))
13080 {
049412e3 13081 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13082 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13083 }
13084 else if (section_is_p (sectp->name, &names->info_dwo))
13085 {
049412e3 13086 dwp_file->sections.info.s.section = sectp;
fd361982 13087 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13088 }
13089 else if (section_is_p (sectp->name, &names->line_dwo))
13090 {
049412e3 13091 dwp_file->sections.line.s.section = sectp;
fd361982 13092 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13093 }
13094 else if (section_is_p (sectp->name, &names->loc_dwo))
13095 {
049412e3 13096 dwp_file->sections.loc.s.section = sectp;
fd361982 13097 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13098 }
13099 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13100 {
049412e3 13101 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13102 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13103 }
13104 else if (section_is_p (sectp->name, &names->macro_dwo))
13105 {
049412e3 13106 dwp_file->sections.macro.s.section = sectp;
fd361982 13107 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13108 }
13109 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13110 {
049412e3 13111 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13112 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13113 }
13114 else if (section_is_p (sectp->name, &names->types_dwo))
13115 {
049412e3 13116 dwp_file->sections.types.s.section = sectp;
fd361982 13117 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13118 }
13119}
13120
80626a55 13121/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13122
80626a55
DE
13123static hashval_t
13124hash_dwp_loaded_cutus (const void *item)
13125{
9a3c8263 13126 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13127
80626a55
DE
13128 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13129 return dwo_unit->signature;
3019eac3
DE
13130}
13131
80626a55 13132/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13133
80626a55
DE
13134static int
13135eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13136{
9a3c8263
SM
13137 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13138 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13139
80626a55
DE
13140 return dua->signature == dub->signature;
13141}
3019eac3 13142
80626a55 13143/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13144
80626a55
DE
13145static htab_t
13146allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13147{
13148 return htab_create_alloc_ex (3,
13149 hash_dwp_loaded_cutus,
13150 eq_dwp_loaded_cutus,
13151 NULL,
13152 &objfile->objfile_obstack,
13153 hashtab_obstack_allocate,
13154 dummy_obstack_deallocate);
13155}
3019eac3 13156
ab5088bf
DE
13157/* Try to open DWP file FILE_NAME.
13158 The result is the bfd handle of the file.
13159 If there is a problem finding or opening the file, return NULL.
13160 Upon success, the canonicalized path of the file is stored in the bfd,
13161 same as symfile_bfd_open. */
13162
192b62ce 13163static gdb_bfd_ref_ptr
ed2dc618
SM
13164open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13165 const char *file_name)
ab5088bf 13166{
ed2dc618
SM
13167 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13168 1 /*is_dwp*/,
192b62ce 13169 1 /*search_cwd*/));
6ac97d4c
DE
13170 if (abfd != NULL)
13171 return abfd;
13172
13173 /* Work around upstream bug 15652.
13174 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13175 [Whether that's a "bug" is debatable, but it is getting in our way.]
13176 We have no real idea where the dwp file is, because gdb's realpath-ing
13177 of the executable's path may have discarded the needed info.
13178 [IWBN if the dwp file name was recorded in the executable, akin to
13179 .gnu_debuglink, but that doesn't exist yet.]
13180 Strip the directory from FILE_NAME and search again. */
13181 if (*debug_file_directory != '\0')
13182 {
13183 /* Don't implicitly search the current directory here.
13184 If the user wants to search "." to handle this case,
13185 it must be added to debug-file-directory. */
ed2dc618
SM
13186 return try_open_dwop_file (dwarf2_per_objfile,
13187 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13188 0 /*search_cwd*/);
13189 }
13190
13191 return NULL;
ab5088bf
DE
13192}
13193
80626a55
DE
13194/* Initialize the use of the DWP file for the current objfile.
13195 By convention the name of the DWP file is ${objfile}.dwp.
13196 The result is NULL if it can't be found. */
a766d390 13197
400174b1 13198static std::unique_ptr<struct dwp_file>
ed2dc618 13199open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13200{
13201 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13202
82bf32bc
JK
13203 /* Try to find first .dwp for the binary file before any symbolic links
13204 resolving. */
6c447423
DE
13205
13206 /* If the objfile is a debug file, find the name of the real binary
13207 file and get the name of dwp file from there. */
d721ba37 13208 std::string dwp_name;
6c447423
DE
13209 if (objfile->separate_debug_objfile_backlink != NULL)
13210 {
13211 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13212 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13213
d721ba37 13214 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13215 }
13216 else
d721ba37
PA
13217 dwp_name = objfile->original_name;
13218
13219 dwp_name += ".dwp";
80626a55 13220
ed2dc618 13221 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13222 if (dbfd == NULL
13223 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13224 {
13225 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13226 dwp_name = objfile_name (objfile);
13227 dwp_name += ".dwp";
ed2dc618 13228 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13229 }
13230
80626a55
DE
13231 if (dbfd == NULL)
13232 {
b4f54984 13233 if (dwarf_read_debug)
d721ba37 13234 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13235 return std::unique_ptr<dwp_file> ();
3019eac3 13236 }
400174b1
TT
13237
13238 const char *name = bfd_get_filename (dbfd.get ());
13239 std::unique_ptr<struct dwp_file> dwp_file
13240 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13241
0a0f4c01 13242 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13243 dwp_file->elf_sections =
13244 OBSTACK_CALLOC (&objfile->objfile_obstack,
13245 dwp_file->num_sections, asection *);
13246
400174b1
TT
13247 bfd_map_over_sections (dwp_file->dbfd.get (),
13248 dwarf2_locate_common_dwp_sections,
13249 dwp_file.get ());
80626a55 13250
400174b1
TT
13251 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13252 0);
80626a55 13253
400174b1
TT
13254 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13255 1);
80626a55 13256
73869dc2 13257 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13258 if (dwp_file->cus && dwp_file->tus
13259 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13260 {
13261 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13262 pretty bizarre. We use pulongest here because that's the established
4d65956b 13263 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13264 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13265 " TU version %s [in DWP file %s]"),
13266 pulongest (dwp_file->cus->version),
d721ba37 13267 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13268 }
08302ed2
DE
13269
13270 if (dwp_file->cus)
13271 dwp_file->version = dwp_file->cus->version;
13272 else if (dwp_file->tus)
13273 dwp_file->version = dwp_file->tus->version;
13274 else
13275 dwp_file->version = 2;
73869dc2
DE
13276
13277 if (dwp_file->version == 2)
400174b1
TT
13278 bfd_map_over_sections (dwp_file->dbfd.get (),
13279 dwarf2_locate_v2_dwp_sections,
13280 dwp_file.get ());
73869dc2 13281
19ac8c2e
DE
13282 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13283 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13284
b4f54984 13285 if (dwarf_read_debug)
80626a55
DE
13286 {
13287 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13288 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13289 " %s CUs, %s TUs\n",
13290 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13291 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13292 }
13293
13294 return dwp_file;
3019eac3 13295}
c906108c 13296
ab5088bf
DE
13297/* Wrapper around open_and_init_dwp_file, only open it once. */
13298
13299static struct dwp_file *
ed2dc618 13300get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13301{
13302 if (! dwarf2_per_objfile->dwp_checked)
13303 {
ed2dc618
SM
13304 dwarf2_per_objfile->dwp_file
13305 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13306 dwarf2_per_objfile->dwp_checked = 1;
13307 }
400174b1 13308 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13309}
13310
80626a55
DE
13311/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13312 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13313 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13314 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13315 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13316
13317 This is called, for example, when wanting to read a variable with a
13318 complex location. Therefore we don't want to do file i/o for every call.
13319 Therefore we don't want to look for a DWO file on every call.
13320 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13321 then we check if we've already seen DWO_NAME, and only THEN do we check
13322 for a DWO file.
13323
1c658ad5 13324 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13325 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13326
3019eac3 13327static struct dwo_unit *
80626a55
DE
13328lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13329 const char *dwo_name, const char *comp_dir,
13330 ULONGEST signature, int is_debug_types)
3019eac3 13331{
ed2dc618 13332 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13333 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13334 const char *kind = is_debug_types ? "TU" : "CU";
13335 void **dwo_file_slot;
3019eac3 13336 struct dwo_file *dwo_file;
80626a55 13337 struct dwp_file *dwp_file;
cb1df416 13338
6a506a2d
DE
13339 /* First see if there's a DWP file.
13340 If we have a DWP file but didn't find the DWO inside it, don't
13341 look for the original DWO file. It makes gdb behave differently
13342 depending on whether one is debugging in the build tree. */
cf2c3c16 13343
ed2dc618 13344 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13345 if (dwp_file != NULL)
cf2c3c16 13346 {
80626a55
DE
13347 const struct dwp_hash_table *dwp_htab =
13348 is_debug_types ? dwp_file->tus : dwp_file->cus;
13349
13350 if (dwp_htab != NULL)
13351 {
13352 struct dwo_unit *dwo_cutu =
ed2dc618 13353 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13354 signature, is_debug_types);
80626a55
DE
13355
13356 if (dwo_cutu != NULL)
13357 {
b4f54984 13358 if (dwarf_read_debug)
80626a55
DE
13359 {
13360 fprintf_unfiltered (gdb_stdlog,
13361 "Virtual DWO %s %s found: @%s\n",
13362 kind, hex_string (signature),
13363 host_address_to_string (dwo_cutu));
13364 }
13365 return dwo_cutu;
13366 }
13367 }
13368 }
6a506a2d 13369 else
80626a55 13370 {
6a506a2d 13371 /* No DWP file, look for the DWO file. */
80626a55 13372
ed2dc618
SM
13373 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13374 dwo_name, comp_dir);
6a506a2d 13375 if (*dwo_file_slot == NULL)
80626a55 13376 {
6a506a2d
DE
13377 /* Read in the file and build a table of the CUs/TUs it contains. */
13378 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13379 }
6a506a2d 13380 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13381 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13382
6a506a2d 13383 if (dwo_file != NULL)
19c3d4c9 13384 {
6a506a2d
DE
13385 struct dwo_unit *dwo_cutu = NULL;
13386
13387 if (is_debug_types && dwo_file->tus)
13388 {
13389 struct dwo_unit find_dwo_cutu;
13390
13391 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13392 find_dwo_cutu.signature = signature;
9a3c8263
SM
13393 dwo_cutu
13394 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13395 }
33c5cd75 13396 else if (!is_debug_types && dwo_file->cus)
80626a55 13397 {
33c5cd75
DB
13398 struct dwo_unit find_dwo_cutu;
13399
13400 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13401 find_dwo_cutu.signature = signature;
13402 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13403 &find_dwo_cutu);
6a506a2d
DE
13404 }
13405
13406 if (dwo_cutu != NULL)
13407 {
b4f54984 13408 if (dwarf_read_debug)
6a506a2d
DE
13409 {
13410 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13411 kind, dwo_name, hex_string (signature),
13412 host_address_to_string (dwo_cutu));
13413 }
13414 return dwo_cutu;
80626a55
DE
13415 }
13416 }
2e276125 13417 }
9cdd5dbd 13418
80626a55
DE
13419 /* We didn't find it. This could mean a dwo_id mismatch, or
13420 someone deleted the DWO/DWP file, or the search path isn't set up
13421 correctly to find the file. */
13422
b4f54984 13423 if (dwarf_read_debug)
80626a55
DE
13424 {
13425 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13426 kind, dwo_name, hex_string (signature));
13427 }
3019eac3 13428
6656a72d
DE
13429 /* This is a warning and not a complaint because it can be caused by
13430 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13431 {
13432 /* Print the name of the DWP file if we looked there, helps the user
13433 better diagnose the problem. */
791afaa2 13434 std::string dwp_text;
43942612
DE
13435
13436 if (dwp_file != NULL)
791afaa2
TT
13437 dwp_text = string_printf (" [in DWP file %s]",
13438 lbasename (dwp_file->name));
43942612 13439
9d8780f0 13440 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13441 " [in module %s]"),
13442 kind, dwo_name, hex_string (signature),
791afaa2 13443 dwp_text.c_str (),
43942612 13444 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13445 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13446 }
3019eac3 13447 return NULL;
5fb290d7
DJ
13448}
13449
80626a55
DE
13450/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13451 See lookup_dwo_cutu_unit for details. */
13452
13453static struct dwo_unit *
13454lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13455 const char *dwo_name, const char *comp_dir,
13456 ULONGEST signature)
13457{
13458 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13459}
13460
13461/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13462 See lookup_dwo_cutu_unit for details. */
13463
13464static struct dwo_unit *
13465lookup_dwo_type_unit (struct signatured_type *this_tu,
13466 const char *dwo_name, const char *comp_dir)
13467{
13468 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13469}
13470
89e63ee4
DE
13471/* Traversal function for queue_and_load_all_dwo_tus. */
13472
13473static int
13474queue_and_load_dwo_tu (void **slot, void *info)
13475{
13476 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13477 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13478 ULONGEST signature = dwo_unit->signature;
13479 struct signatured_type *sig_type =
13480 lookup_dwo_signatured_type (per_cu->cu, signature);
13481
13482 if (sig_type != NULL)
13483 {
13484 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13485
13486 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13487 a real dependency of PER_CU on SIG_TYPE. That is detected later
13488 while processing PER_CU. */
13489 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13490 load_full_type_unit (sig_cu);
ae640021 13491 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13492 }
13493
13494 return 1;
13495}
13496
13497/* Queue all TUs contained in the DWO of PER_CU to be read in.
13498 The DWO may have the only definition of the type, though it may not be
13499 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13500 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13501
13502static void
13503queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13504{
13505 struct dwo_unit *dwo_unit;
13506 struct dwo_file *dwo_file;
13507
13508 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13509 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13510 gdb_assert (per_cu->cu != NULL);
13511
13512 dwo_unit = per_cu->cu->dwo_unit;
13513 gdb_assert (dwo_unit != NULL);
13514
13515 dwo_file = dwo_unit->dwo_file;
13516 if (dwo_file->tus != NULL)
13517 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13518}
13519
3019eac3 13520/* Read in various DIEs. */
348e048f 13521
d389af10 13522/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13523 Inherit only the children of the DW_AT_abstract_origin DIE not being
13524 already referenced by DW_AT_abstract_origin from the children of the
13525 current DIE. */
d389af10
JK
13526
13527static void
13528inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13529{
13530 struct die_info *child_die;
791afaa2 13531 sect_offset *offsetp;
d389af10
JK
13532 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13533 struct die_info *origin_die;
13534 /* Iterator of the ORIGIN_DIE children. */
13535 struct die_info *origin_child_die;
d389af10 13536 struct attribute *attr;
cd02d79d
PA
13537 struct dwarf2_cu *origin_cu;
13538 struct pending **origin_previous_list_in_scope;
d389af10
JK
13539
13540 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13541 if (!attr)
13542 return;
13543
cd02d79d
PA
13544 /* Note that following die references may follow to a die in a
13545 different cu. */
13546
13547 origin_cu = cu;
13548 origin_die = follow_die_ref (die, attr, &origin_cu);
13549
13550 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13551 symbols in. */
13552 origin_previous_list_in_scope = origin_cu->list_in_scope;
13553 origin_cu->list_in_scope = cu->list_in_scope;
13554
edb3359d
DJ
13555 if (die->tag != origin_die->tag
13556 && !(die->tag == DW_TAG_inlined_subroutine
13557 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13558 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13559 sect_offset_str (die->sect_off),
13560 sect_offset_str (origin_die->sect_off));
d389af10 13561
791afaa2 13562 std::vector<sect_offset> offsets;
d389af10 13563
3ea89b92
PMR
13564 for (child_die = die->child;
13565 child_die && child_die->tag;
13566 child_die = sibling_die (child_die))
13567 {
13568 struct die_info *child_origin_die;
13569 struct dwarf2_cu *child_origin_cu;
13570
13571 /* We are trying to process concrete instance entries:
216f72a1 13572 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13573 it's not relevant to our analysis here. i.e. detecting DIEs that are
13574 present in the abstract instance but not referenced in the concrete
13575 one. */
216f72a1
JK
13576 if (child_die->tag == DW_TAG_call_site
13577 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13578 continue;
13579
c38f313d
DJ
13580 /* For each CHILD_DIE, find the corresponding child of
13581 ORIGIN_DIE. If there is more than one layer of
13582 DW_AT_abstract_origin, follow them all; there shouldn't be,
13583 but GCC versions at least through 4.4 generate this (GCC PR
13584 40573). */
3ea89b92
PMR
13585 child_origin_die = child_die;
13586 child_origin_cu = cu;
c38f313d
DJ
13587 while (1)
13588 {
cd02d79d
PA
13589 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13590 child_origin_cu);
c38f313d
DJ
13591 if (attr == NULL)
13592 break;
cd02d79d
PA
13593 child_origin_die = follow_die_ref (child_origin_die, attr,
13594 &child_origin_cu);
c38f313d
DJ
13595 }
13596
d389af10
JK
13597 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13598 counterpart may exist. */
c38f313d 13599 if (child_origin_die != child_die)
d389af10 13600 {
edb3359d
DJ
13601 if (child_die->tag != child_origin_die->tag
13602 && !(child_die->tag == DW_TAG_inlined_subroutine
13603 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13604 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13605 "different tags"),
9d8780f0
SM
13606 sect_offset_str (child_die->sect_off),
13607 sect_offset_str (child_origin_die->sect_off));
c38f313d 13608 if (child_origin_die->parent != origin_die)
b98664d3 13609 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13610 "different parents"),
9d8780f0
SM
13611 sect_offset_str (child_die->sect_off),
13612 sect_offset_str (child_origin_die->sect_off));
c38f313d 13613 else
791afaa2 13614 offsets.push_back (child_origin_die->sect_off);
d389af10 13615 }
d389af10 13616 }
791afaa2
TT
13617 std::sort (offsets.begin (), offsets.end ());
13618 sect_offset *offsets_end = offsets.data () + offsets.size ();
13619 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13620 if (offsetp[-1] == *offsetp)
b98664d3 13621 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13622 "to DIE %s as their abstract origin"),
13623 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13624
791afaa2 13625 offsetp = offsets.data ();
d389af10
JK
13626 origin_child_die = origin_die->child;
13627 while (origin_child_die && origin_child_die->tag)
13628 {
13629 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13630 while (offsetp < offsets_end
9c541725 13631 && *offsetp < origin_child_die->sect_off)
d389af10 13632 offsetp++;
b64f50a1 13633 if (offsetp >= offsets_end
9c541725 13634 || *offsetp > origin_child_die->sect_off)
d389af10 13635 {
adde2bff
DE
13636 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13637 Check whether we're already processing ORIGIN_CHILD_DIE.
13638 This can happen with mutually referenced abstract_origins.
13639 PR 16581. */
13640 if (!origin_child_die->in_process)
13641 process_die (origin_child_die, origin_cu);
d389af10
JK
13642 }
13643 origin_child_die = sibling_die (origin_child_die);
13644 }
cd02d79d 13645 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13646
13647 if (cu != origin_cu)
13648 compute_delayed_physnames (origin_cu);
d389af10
JK
13649}
13650
c906108c 13651static void
e7c27a73 13652read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13653{
518817b3 13654 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13655 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13656 struct context_stack *newobj;
c906108c
SS
13657 CORE_ADDR lowpc;
13658 CORE_ADDR highpc;
13659 struct die_info *child_die;
edb3359d 13660 struct attribute *attr, *call_line, *call_file;
15d034d0 13661 const char *name;
e142c38c 13662 CORE_ADDR baseaddr;
801e3a5b 13663 struct block *block;
edb3359d 13664 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13665 std::vector<struct symbol *> template_args;
34eaf542 13666 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13667
13668 if (inlined_func)
13669 {
13670 /* If we do not have call site information, we can't show the
13671 caller of this inlined function. That's too confusing, so
13672 only use the scope for local variables. */
13673 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13674 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13675 if (call_line == NULL || call_file == NULL)
13676 {
13677 read_lexical_block_scope (die, cu);
13678 return;
13679 }
13680 }
c906108c 13681
b3b3bada 13682 baseaddr = objfile->text_section_offset ();
e142c38c 13683
94af9270 13684 name = dwarf2_name (die, cu);
c906108c 13685
e8d05480
JB
13686 /* Ignore functions with missing or empty names. These are actually
13687 illegal according to the DWARF standard. */
13688 if (name == NULL)
13689 {
b98664d3 13690 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13691 sect_offset_str (die->sect_off));
e8d05480
JB
13692 return;
13693 }
13694
13695 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13696 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13697 <= PC_BOUNDS_INVALID)
e8d05480 13698 {
ae4d0c03
PM
13699 attr = dwarf2_attr (die, DW_AT_external, cu);
13700 if (!attr || !DW_UNSND (attr))
b98664d3 13701 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13702 "for subprogram DIE at %s"),
13703 sect_offset_str (die->sect_off));
e8d05480
JB
13704 return;
13705 }
c906108c 13706
3e29f34a
MR
13707 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13708 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13709
34eaf542
TT
13710 /* If we have any template arguments, then we must allocate a
13711 different sort of symbol. */
13712 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13713 {
13714 if (child_die->tag == DW_TAG_template_type_param
13715 || child_die->tag == DW_TAG_template_value_param)
13716 {
e623cf5d 13717 templ_func = allocate_template_symbol (objfile);
cf724bc9 13718 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13719 break;
13720 }
13721 }
13722
c24bdb02 13723 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13724 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13725 (struct symbol *) templ_func);
4c2df51b 13726
81873cc8 13727 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13728 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13729 cu->language);
13730
4cecd739
DJ
13731 /* If there is a location expression for DW_AT_frame_base, record
13732 it. */
e142c38c 13733 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13734 if (attr != nullptr)
fe978cb0 13735 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13736
63e43d3a
PMR
13737 /* If there is a location for the static link, record it. */
13738 newobj->static_link = NULL;
13739 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13740 if (attr != nullptr)
63e43d3a 13741 {
224c3ddb
SM
13742 newobj->static_link
13743 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13744 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13745 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13746 }
13747
c24bdb02 13748 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13749
639d11d3 13750 if (die->child != NULL)
c906108c 13751 {
639d11d3 13752 child_die = die->child;
c906108c
SS
13753 while (child_die && child_die->tag)
13754 {
34eaf542
TT
13755 if (child_die->tag == DW_TAG_template_type_param
13756 || child_die->tag == DW_TAG_template_value_param)
13757 {
13758 struct symbol *arg = new_symbol (child_die, NULL, cu);
13759
f1078f66 13760 if (arg != NULL)
2f4732b0 13761 template_args.push_back (arg);
34eaf542
TT
13762 }
13763 else
13764 process_die (child_die, cu);
c906108c
SS
13765 child_die = sibling_die (child_die);
13766 }
13767 }
13768
d389af10
JK
13769 inherit_abstract_dies (die, cu);
13770
4a811a97
UW
13771 /* If we have a DW_AT_specification, we might need to import using
13772 directives from the context of the specification DIE. See the
13773 comment in determine_prefix. */
13774 if (cu->language == language_cplus
13775 && dwarf2_attr (die, DW_AT_specification, cu))
13776 {
13777 struct dwarf2_cu *spec_cu = cu;
13778 struct die_info *spec_die = die_specification (die, &spec_cu);
13779
13780 while (spec_die)
13781 {
13782 child_die = spec_die->child;
13783 while (child_die && child_die->tag)
13784 {
13785 if (child_die->tag == DW_TAG_imported_module)
13786 process_die (child_die, spec_cu);
13787 child_die = sibling_die (child_die);
13788 }
13789
13790 /* In some cases, GCC generates specification DIEs that
13791 themselves contain DW_AT_specification attributes. */
13792 spec_die = die_specification (spec_die, &spec_cu);
13793 }
13794 }
13795
c24bdb02 13796 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13797 /* Make a block for the local symbols within. */
c24bdb02 13798 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13799 cstk.static_link, lowpc, highpc);
801e3a5b 13800
df8a16a1 13801 /* For C++, set the block's scope. */
45280282
IB
13802 if ((cu->language == language_cplus
13803 || cu->language == language_fortran
c44af4eb
TT
13804 || cu->language == language_d
13805 || cu->language == language_rust)
4d4ec4e5 13806 && cu->processing_has_namespace_info)
195a3f6c
TT
13807 block_set_scope (block, determine_prefix (die, cu),
13808 &objfile->objfile_obstack);
df8a16a1 13809
801e3a5b
JB
13810 /* If we have address ranges, record them. */
13811 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13812
a60f3166 13813 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13814
34eaf542 13815 /* Attach template arguments to function. */
2f4732b0 13816 if (!template_args.empty ())
34eaf542
TT
13817 {
13818 gdb_assert (templ_func != NULL);
13819
2f4732b0 13820 templ_func->n_template_arguments = template_args.size ();
34eaf542 13821 templ_func->template_arguments
8d749320
SM
13822 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13823 templ_func->n_template_arguments);
34eaf542 13824 memcpy (templ_func->template_arguments,
2f4732b0 13825 template_args.data (),
34eaf542 13826 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13827
13828 /* Make sure that the symtab is set on the new symbols. Even
13829 though they don't appear in this symtab directly, other parts
13830 of gdb assume that symbols do, and this is reasonably
13831 true. */
8634679f 13832 for (symbol *sym : template_args)
3e1d3d8c 13833 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13834 }
13835
208d8187
JB
13836 /* In C++, we can have functions nested inside functions (e.g., when
13837 a function declares a class that has methods). This means that
13838 when we finish processing a function scope, we may need to go
13839 back to building a containing block's symbol lists. */
c24bdb02
KS
13840 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13841 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13842
921e78cf
JB
13843 /* If we've finished processing a top-level function, subsequent
13844 symbols go in the file symbol list. */
c24bdb02
KS
13845 if (cu->get_builder ()->outermost_context_p ())
13846 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13847}
13848
13849/* Process all the DIES contained within a lexical block scope. Start
13850 a new scope, process the dies, and then close the scope. */
13851
13852static void
e7c27a73 13853read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13854{
518817b3 13855 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13856 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13857 CORE_ADDR lowpc, highpc;
13858 struct die_info *child_die;
e142c38c
DJ
13859 CORE_ADDR baseaddr;
13860
b3b3bada 13861 baseaddr = objfile->text_section_offset ();
c906108c
SS
13862
13863 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13864 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13865 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13866 be nasty. Might be easier to properly extend generic blocks to
af34e669 13867 describe ranges. */
e385593e
JK
13868 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13869 {
13870 case PC_BOUNDS_NOT_PRESENT:
13871 /* DW_TAG_lexical_block has no attributes, process its children as if
13872 there was no wrapping by that DW_TAG_lexical_block.
13873 GCC does no longer produces such DWARF since GCC r224161. */
13874 for (child_die = die->child;
13875 child_die != NULL && child_die->tag;
13876 child_die = sibling_die (child_die))
13877 process_die (child_die, cu);
13878 return;
13879 case PC_BOUNDS_INVALID:
13880 return;
13881 }
3e29f34a
MR
13882 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13883 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13884
c24bdb02 13885 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13886 if (die->child != NULL)
c906108c 13887 {
639d11d3 13888 child_die = die->child;
c906108c
SS
13889 while (child_die && child_die->tag)
13890 {
e7c27a73 13891 process_die (child_die, cu);
c906108c
SS
13892 child_die = sibling_die (child_die);
13893 }
13894 }
3ea89b92 13895 inherit_abstract_dies (die, cu);
c24bdb02 13896 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13897
c24bdb02
KS
13898 if (*cu->get_builder ()->get_local_symbols () != NULL
13899 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13900 {
801e3a5b 13901 struct block *block
c24bdb02 13902 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13903 cstk.start_addr, highpc);
801e3a5b
JB
13904
13905 /* Note that recording ranges after traversing children, as we
13906 do here, means that recording a parent's ranges entails
13907 walking across all its children's ranges as they appear in
13908 the address map, which is quadratic behavior.
13909
13910 It would be nicer to record the parent's ranges before
13911 traversing its children, simply overriding whatever you find
13912 there. But since we don't even decide whether to create a
13913 block until after we've traversed its children, that's hard
13914 to do. */
13915 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13916 }
c24bdb02
KS
13917 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13918 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13919}
13920
216f72a1 13921/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13922
13923static void
13924read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13925{
518817b3 13926 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13927 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13928 CORE_ADDR pc, baseaddr;
13929 struct attribute *attr;
13930 struct call_site *call_site, call_site_local;
13931 void **slot;
13932 int nparams;
13933 struct die_info *child_die;
13934
b3b3bada 13935 baseaddr = objfile->text_section_offset ();
96408a79 13936
216f72a1
JK
13937 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13938 if (attr == NULL)
13939 {
13940 /* This was a pre-DWARF-5 GNU extension alias
13941 for DW_AT_call_return_pc. */
13942 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13943 }
96408a79
SA
13944 if (!attr)
13945 {
b98664d3 13946 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13947 "DIE %s [in module %s]"),
13948 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13949 return;
13950 }
31aa7e4e 13951 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13952 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13953
13954 if (cu->call_site_htab == NULL)
13955 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13956 NULL, &objfile->objfile_obstack,
13957 hashtab_obstack_allocate, NULL);
13958 call_site_local.pc = pc;
13959 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13960 if (*slot != NULL)
13961 {
b98664d3 13962 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13963 "DIE %s [in module %s]"),
13964 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13965 objfile_name (objfile));
96408a79
SA
13966 return;
13967 }
13968
13969 /* Count parameters at the caller. */
13970
13971 nparams = 0;
13972 for (child_die = die->child; child_die && child_die->tag;
13973 child_die = sibling_die (child_die))
13974 {
216f72a1
JK
13975 if (child_die->tag != DW_TAG_call_site_parameter
13976 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13977 {
b98664d3 13978 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13979 "DW_TAG_call_site child DIE %s [in module %s]"),
13980 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13981 objfile_name (objfile));
96408a79
SA
13982 continue;
13983 }
13984
13985 nparams++;
13986 }
13987
224c3ddb
SM
13988 call_site
13989 = ((struct call_site *)
13990 obstack_alloc (&objfile->objfile_obstack,
13991 sizeof (*call_site)
13992 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13993 *slot = call_site;
13994 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13995 call_site->pc = pc;
13996
216f72a1
JK
13997 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13998 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13999 {
14000 struct die_info *func_die;
14001
14002 /* Skip also over DW_TAG_inlined_subroutine. */
14003 for (func_die = die->parent;
14004 func_die && func_die->tag != DW_TAG_subprogram
14005 && func_die->tag != DW_TAG_subroutine_type;
14006 func_die = func_die->parent);
14007
216f72a1
JK
14008 /* DW_AT_call_all_calls is a superset
14009 of DW_AT_call_all_tail_calls. */
96408a79 14010 if (func_die
216f72a1 14011 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14012 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14013 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14014 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14015 {
14016 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14017 not complete. But keep CALL_SITE for look ups via call_site_htab,
14018 both the initial caller containing the real return address PC and
14019 the final callee containing the current PC of a chain of tail
14020 calls do not need to have the tail call list complete. But any
14021 function candidate for a virtual tail call frame searched via
14022 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14023 determined unambiguously. */
14024 }
14025 else
14026 {
14027 struct type *func_type = NULL;
14028
14029 if (func_die)
14030 func_type = get_die_type (func_die, cu);
14031 if (func_type != NULL)
14032 {
14033 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14034
14035 /* Enlist this call site to the function. */
14036 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14037 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14038 }
14039 else
b98664d3 14040 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14041 "DIE %s [in module %s]"),
14042 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14043 }
14044 }
14045
216f72a1
JK
14046 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14047 if (attr == NULL)
14048 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14049 if (attr == NULL)
14050 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14051 if (attr == NULL)
216f72a1
JK
14052 {
14053 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14054 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14055 }
96408a79
SA
14056 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14057 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14058 /* Keep NULL DWARF_BLOCK. */;
14059 else if (attr_form_is_block (attr))
14060 {
14061 struct dwarf2_locexpr_baton *dlbaton;
14062
8d749320 14063 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14064 dlbaton->data = DW_BLOCK (attr)->data;
14065 dlbaton->size = DW_BLOCK (attr)->size;
14066 dlbaton->per_cu = cu->per_cu;
14067
14068 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14069 }
7771576e 14070 else if (attr_form_is_ref (attr))
96408a79 14071 {
96408a79
SA
14072 struct dwarf2_cu *target_cu = cu;
14073 struct die_info *target_die;
14074
ac9ec31b 14075 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14076 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14077 if (die_is_declaration (target_die, target_cu))
14078 {
7d45c7c3 14079 const char *target_physname;
9112db09
JK
14080
14081 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14082 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14083 if (target_physname == NULL)
9112db09 14084 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14085 if (target_physname == NULL)
b98664d3 14086 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14087 "physname, for referencing DIE %s [in module %s]"),
14088 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14089 else
7d455152 14090 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14091 }
14092 else
14093 {
14094 CORE_ADDR lowpc;
14095
14096 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14097 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14098 <= PC_BOUNDS_INVALID)
b98664d3 14099 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14100 "low pc, for referencing DIE %s [in module %s]"),
14101 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14102 else
3e29f34a
MR
14103 {
14104 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14105 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14106 }
96408a79
SA
14107 }
14108 }
14109 else
b98664d3 14110 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14111 "block nor reference, for DIE %s [in module %s]"),
14112 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14113
14114 call_site->per_cu = cu->per_cu;
14115
14116 for (child_die = die->child;
14117 child_die && child_die->tag;
14118 child_die = sibling_die (child_die))
14119 {
96408a79 14120 struct call_site_parameter *parameter;
1788b2d3 14121 struct attribute *loc, *origin;
96408a79 14122
216f72a1
JK
14123 if (child_die->tag != DW_TAG_call_site_parameter
14124 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14125 {
14126 /* Already printed the complaint above. */
14127 continue;
14128 }
14129
14130 gdb_assert (call_site->parameter_count < nparams);
14131 parameter = &call_site->parameter[call_site->parameter_count];
14132
1788b2d3
JK
14133 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14134 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14135 register is contained in DW_AT_call_value. */
96408a79 14136
24c5c679 14137 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14138 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14139 if (origin == NULL)
14140 {
14141 /* This was a pre-DWARF-5 GNU extension alias
14142 for DW_AT_call_parameter. */
14143 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14144 }
7771576e 14145 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14146 {
1788b2d3 14147 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14148
14149 sect_offset sect_off
14150 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14151 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14152 {
14153 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14154 binding can be done only inside one CU. Such referenced DIE
14155 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14156 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14157 "DW_TAG_call_site child DIE %s [in module %s]"),
14158 sect_offset_str (child_die->sect_off),
9c541725 14159 objfile_name (objfile));
d76b7dbc
JK
14160 continue;
14161 }
9c541725
PA
14162 parameter->u.param_cu_off
14163 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14164 }
14165 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14166 {
b98664d3 14167 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14168 "DW_TAG_call_site child DIE %s [in module %s]"),
14169 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14170 continue;
14171 }
24c5c679 14172 else
96408a79 14173 {
24c5c679
JK
14174 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14175 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14176 if (parameter->u.dwarf_reg != -1)
14177 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14178 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14179 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14180 &parameter->u.fb_offset))
14181 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14182 else
14183 {
b98664d3 14184 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14185 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14186 "DW_TAG_call_site child DIE %s "
24c5c679 14187 "[in module %s]"),
9d8780f0 14188 sect_offset_str (child_die->sect_off),
9c541725 14189 objfile_name (objfile));
24c5c679
JK
14190 continue;
14191 }
96408a79
SA
14192 }
14193
216f72a1
JK
14194 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14195 if (attr == NULL)
14196 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14197 if (!attr_form_is_block (attr))
14198 {
b98664d3 14199 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14200 "DW_TAG_call_site child DIE %s [in module %s]"),
14201 sect_offset_str (child_die->sect_off),
9c541725 14202 objfile_name (objfile));
96408a79
SA
14203 continue;
14204 }
14205 parameter->value = DW_BLOCK (attr)->data;
14206 parameter->value_size = DW_BLOCK (attr)->size;
14207
14208 /* Parameters are not pre-cleared by memset above. */
14209 parameter->data_value = NULL;
14210 parameter->data_value_size = 0;
14211 call_site->parameter_count++;
14212
216f72a1
JK
14213 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14214 if (attr == NULL)
14215 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14216 if (attr != nullptr)
96408a79
SA
14217 {
14218 if (!attr_form_is_block (attr))
b98664d3 14219 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14220 "DW_TAG_call_site child DIE %s [in module %s]"),
14221 sect_offset_str (child_die->sect_off),
9c541725 14222 objfile_name (objfile));
96408a79
SA
14223 else
14224 {
14225 parameter->data_value = DW_BLOCK (attr)->data;
14226 parameter->data_value_size = DW_BLOCK (attr)->size;
14227 }
14228 }
14229 }
14230}
14231
71a3c369
TT
14232/* Helper function for read_variable. If DIE represents a virtual
14233 table, then return the type of the concrete object that is
14234 associated with the virtual table. Otherwise, return NULL. */
14235
14236static struct type *
14237rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14238{
14239 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14240 if (attr == NULL)
14241 return NULL;
14242
14243 /* Find the type DIE. */
14244 struct die_info *type_die = NULL;
14245 struct dwarf2_cu *type_cu = cu;
14246
14247 if (attr_form_is_ref (attr))
14248 type_die = follow_die_ref (die, attr, &type_cu);
14249 if (type_die == NULL)
14250 return NULL;
14251
14252 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14253 return NULL;
14254 return die_containing_type (type_die, type_cu);
14255}
14256
14257/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14258
14259static void
14260read_variable (struct die_info *die, struct dwarf2_cu *cu)
14261{
14262 struct rust_vtable_symbol *storage = NULL;
14263
14264 if (cu->language == language_rust)
14265 {
14266 struct type *containing_type = rust_containing_type (die, cu);
14267
14268 if (containing_type != NULL)
14269 {
518817b3 14270 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 14271
468c0cbb 14272 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
14273 initialize_objfile_symbol (storage);
14274 storage->concrete_type = containing_type;
cf724bc9 14275 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14276 }
14277 }
14278
e4a62c65
TV
14279 struct symbol *res = new_symbol (die, NULL, cu, storage);
14280 struct attribute *abstract_origin
14281 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14282 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14283 if (res == NULL && loc && abstract_origin)
14284 {
14285 /* We have a variable without a name, but with a location and an abstract
14286 origin. This may be a concrete instance of an abstract variable
14287 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14288 later. */
14289 struct dwarf2_cu *origin_cu = cu;
14290 struct die_info *origin_die
14291 = follow_die_ref (die, abstract_origin, &origin_cu);
14292 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14293 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14294 }
71a3c369
TT
14295}
14296
43988095
JK
14297/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14298 reading .debug_rnglists.
14299 Callback's type should be:
14300 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14301 Return true if the attributes are present and valid, otherwise,
14302 return false. */
14303
14304template <typename Callback>
14305static bool
14306dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14307 Callback &&callback)
14308{
ed2dc618 14309 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14310 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14311 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14312 bfd *obfd = objfile->obfd;
43988095
JK
14313 /* Base address selection entry. */
14314 CORE_ADDR base;
14315 int found_base;
43988095 14316 const gdb_byte *buffer;
43988095
JK
14317 CORE_ADDR baseaddr;
14318 bool overflow = false;
14319
14320 found_base = cu->base_known;
14321 base = cu->base_address;
14322
14323 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14324 if (offset >= dwarf2_per_objfile->rnglists.size)
14325 {
b98664d3 14326 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14327 offset);
14328 return false;
14329 }
14330 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14331
b3b3bada 14332 baseaddr = objfile->text_section_offset ();
43988095
JK
14333
14334 while (1)
14335 {
7814882a
JK
14336 /* Initialize it due to a false compiler warning. */
14337 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14338 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14339 + dwarf2_per_objfile->rnglists.size);
14340 unsigned int bytes_read;
14341
14342 if (buffer == buf_end)
14343 {
14344 overflow = true;
14345 break;
14346 }
14347 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14348 switch (rlet)
14349 {
14350 case DW_RLE_end_of_list:
14351 break;
14352 case DW_RLE_base_address:
14353 if (buffer + cu->header.addr_size > buf_end)
14354 {
14355 overflow = true;
14356 break;
14357 }
14358 base = read_address (obfd, buffer, cu, &bytes_read);
14359 found_base = 1;
14360 buffer += bytes_read;
14361 break;
14362 case DW_RLE_start_length:
14363 if (buffer + cu->header.addr_size > buf_end)
14364 {
14365 overflow = true;
14366 break;
14367 }
14368 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14369 buffer += bytes_read;
14370 range_end = (range_beginning
14371 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14372 buffer += bytes_read;
14373 if (buffer > buf_end)
14374 {
14375 overflow = true;
14376 break;
14377 }
14378 break;
14379 case DW_RLE_offset_pair:
14380 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14381 buffer += bytes_read;
14382 if (buffer > buf_end)
14383 {
14384 overflow = true;
14385 break;
14386 }
14387 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14388 buffer += bytes_read;
14389 if (buffer > buf_end)
14390 {
14391 overflow = true;
14392 break;
14393 }
14394 break;
14395 case DW_RLE_start_end:
14396 if (buffer + 2 * cu->header.addr_size > buf_end)
14397 {
14398 overflow = true;
14399 break;
14400 }
14401 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14402 buffer += bytes_read;
14403 range_end = read_address (obfd, buffer, cu, &bytes_read);
14404 buffer += bytes_read;
14405 break;
14406 default:
b98664d3 14407 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14408 return false;
14409 }
14410 if (rlet == DW_RLE_end_of_list || overflow)
14411 break;
14412 if (rlet == DW_RLE_base_address)
14413 continue;
14414
14415 if (!found_base)
14416 {
14417 /* We have no valid base address for the ranges
14418 data. */
b98664d3 14419 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14420 return false;
14421 }
14422
14423 if (range_beginning > range_end)
14424 {
14425 /* Inverted range entries are invalid. */
b98664d3 14426 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14427 return false;
14428 }
14429
14430 /* Empty range entries have no effect. */
14431 if (range_beginning == range_end)
14432 continue;
14433
14434 range_beginning += base;
14435 range_end += base;
14436
14437 /* A not-uncommon case of bad debug info.
14438 Don't pollute the addrmap with bad data. */
14439 if (range_beginning + baseaddr == 0
14440 && !dwarf2_per_objfile->has_section_at_zero)
14441 {
b98664d3 14442 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14443 " [in module %s]"), objfile_name (objfile));
14444 continue;
14445 }
14446
14447 callback (range_beginning, range_end);
14448 }
14449
14450 if (overflow)
14451 {
b98664d3 14452 complaint (_("Offset %d is not terminated "
43988095
JK
14453 "for DW_AT_ranges attribute"),
14454 offset);
14455 return false;
14456 }
14457
14458 return true;
14459}
14460
14461/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14462 Callback's type should be:
14463 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14464 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14465
43988095 14466template <typename Callback>
43039443 14467static int
5f46c5a5 14468dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14469 Callback &&callback)
43039443 14470{
ed2dc618 14471 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14472 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14473 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14474 struct comp_unit_head *cu_header = &cu->header;
14475 bfd *obfd = objfile->obfd;
14476 unsigned int addr_size = cu_header->addr_size;
14477 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14478 /* Base address selection entry. */
14479 CORE_ADDR base;
14480 int found_base;
14481 unsigned int dummy;
d521ce57 14482 const gdb_byte *buffer;
ff013f42 14483 CORE_ADDR baseaddr;
43039443 14484
43988095
JK
14485 if (cu_header->version >= 5)
14486 return dwarf2_rnglists_process (offset, cu, callback);
14487
d00adf39
DE
14488 found_base = cu->base_known;
14489 base = cu->base_address;
43039443 14490
be391dca 14491 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14492 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14493 {
b98664d3 14494 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14495 offset);
14496 return 0;
14497 }
dce234bc 14498 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14499
b3b3bada 14500 baseaddr = objfile->text_section_offset ();
ff013f42 14501
43039443
JK
14502 while (1)
14503 {
14504 CORE_ADDR range_beginning, range_end;
14505
14506 range_beginning = read_address (obfd, buffer, cu, &dummy);
14507 buffer += addr_size;
14508 range_end = read_address (obfd, buffer, cu, &dummy);
14509 buffer += addr_size;
14510 offset += 2 * addr_size;
14511
14512 /* An end of list marker is a pair of zero addresses. */
14513 if (range_beginning == 0 && range_end == 0)
14514 /* Found the end of list entry. */
14515 break;
14516
14517 /* Each base address selection entry is a pair of 2 values.
14518 The first is the largest possible address, the second is
14519 the base address. Check for a base address here. */
14520 if ((range_beginning & mask) == mask)
14521 {
28d2bfb9
AB
14522 /* If we found the largest possible address, then we already
14523 have the base address in range_end. */
14524 base = range_end;
43039443
JK
14525 found_base = 1;
14526 continue;
14527 }
14528
14529 if (!found_base)
14530 {
14531 /* We have no valid base address for the ranges
14532 data. */
b98664d3 14533 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14534 return 0;
14535 }
14536
9277c30c
UW
14537 if (range_beginning > range_end)
14538 {
14539 /* Inverted range entries are invalid. */
b98664d3 14540 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14541 return 0;
14542 }
14543
14544 /* Empty range entries have no effect. */
14545 if (range_beginning == range_end)
14546 continue;
14547
43039443
JK
14548 range_beginning += base;
14549 range_end += base;
14550
01093045
DE
14551 /* A not-uncommon case of bad debug info.
14552 Don't pollute the addrmap with bad data. */
14553 if (range_beginning + baseaddr == 0
14554 && !dwarf2_per_objfile->has_section_at_zero)
14555 {
b98664d3 14556 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14557 " [in module %s]"), objfile_name (objfile));
01093045
DE
14558 continue;
14559 }
14560
5f46c5a5
JK
14561 callback (range_beginning, range_end);
14562 }
14563
14564 return 1;
14565}
14566
14567/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14568 Return 1 if the attributes are present and valid, otherwise, return 0.
14569 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14570
14571static int
14572dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14573 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14574 dwarf2_psymtab *ranges_pst)
5f46c5a5 14575{
518817b3 14576 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 14577 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 14578 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14579 int low_set = 0;
14580 CORE_ADDR low = 0;
14581 CORE_ADDR high = 0;
14582 int retval;
14583
14584 retval = dwarf2_ranges_process (offset, cu,
14585 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14586 {
9277c30c 14587 if (ranges_pst != NULL)
3e29f34a
MR
14588 {
14589 CORE_ADDR lowpc;
14590 CORE_ADDR highpc;
14591
79748972
TT
14592 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14593 range_beginning + baseaddr)
14594 - baseaddr);
14595 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14596 range_end + baseaddr)
14597 - baseaddr);
d320c2b5
TT
14598 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14599 lowpc, highpc - 1, ranges_pst);
3e29f34a 14600 }
ff013f42 14601
43039443
JK
14602 /* FIXME: This is recording everything as a low-high
14603 segment of consecutive addresses. We should have a
14604 data structure for discontiguous block ranges
14605 instead. */
14606 if (! low_set)
14607 {
14608 low = range_beginning;
14609 high = range_end;
14610 low_set = 1;
14611 }
14612 else
14613 {
14614 if (range_beginning < low)
14615 low = range_beginning;
14616 if (range_end > high)
14617 high = range_end;
14618 }
5f46c5a5
JK
14619 });
14620 if (!retval)
14621 return 0;
43039443
JK
14622
14623 if (! low_set)
14624 /* If the first entry is an end-of-list marker, the range
14625 describes an empty scope, i.e. no instructions. */
14626 return 0;
14627
14628 if (low_return)
14629 *low_return = low;
14630 if (high_return)
14631 *high_return = high;
14632 return 1;
14633}
14634
3a2b436a
JK
14635/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14636 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14637 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14638
3a2b436a 14639static enum pc_bounds_kind
af34e669 14640dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14641 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14642 dwarf2_psymtab *pst)
c906108c 14643{
518817b3
SM
14644 struct dwarf2_per_objfile *dwarf2_per_objfile
14645 = cu->per_cu->dwarf2_per_objfile;
c906108c 14646 struct attribute *attr;
91da1414 14647 struct attribute *attr_high;
af34e669
DJ
14648 CORE_ADDR low = 0;
14649 CORE_ADDR high = 0;
e385593e 14650 enum pc_bounds_kind ret;
c906108c 14651
91da1414
MW
14652 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14653 if (attr_high)
af34e669 14654 {
e142c38c 14655 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14656 if (attr != nullptr)
91da1414 14657 {
31aa7e4e
JB
14658 low = attr_value_as_address (attr);
14659 high = attr_value_as_address (attr_high);
14660 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14661 high += low;
91da1414 14662 }
af34e669
DJ
14663 else
14664 /* Found high w/o low attribute. */
e385593e 14665 return PC_BOUNDS_INVALID;
af34e669
DJ
14666
14667 /* Found consecutive range of addresses. */
3a2b436a 14668 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14669 }
c906108c 14670 else
af34e669 14671 {
e142c38c 14672 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14673 if (attr != NULL)
14674 {
18a8505e 14675 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14676 We take advantage of the fact that DW_AT_ranges does not appear
14677 in DW_TAG_compile_unit of DWO files. */
14678 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14679 unsigned int ranges_offset = (DW_UNSND (attr)
14680 + (need_ranges_base
14681 ? cu->ranges_base
14682 : 0));
2e3cf129 14683
af34e669 14684 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14685 .debug_ranges section. */
2e3cf129 14686 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14687 return PC_BOUNDS_INVALID;
43039443 14688 /* Found discontinuous range of addresses. */
3a2b436a 14689 ret = PC_BOUNDS_RANGES;
af34e669 14690 }
e385593e
JK
14691 else
14692 return PC_BOUNDS_NOT_PRESENT;
af34e669 14693 }
c906108c 14694
48fbe735 14695 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14696 if (high <= low)
e385593e 14697 return PC_BOUNDS_INVALID;
c906108c
SS
14698
14699 /* When using the GNU linker, .gnu.linkonce. sections are used to
14700 eliminate duplicate copies of functions and vtables and such.
14701 The linker will arbitrarily choose one and discard the others.
14702 The AT_*_pc values for such functions refer to local labels in
14703 these sections. If the section from that file was discarded, the
14704 labels are not in the output, so the relocs get a value of 0.
14705 If this is a discarded function, mark the pc bounds as invalid,
14706 so that GDB will ignore it. */
72dca2f5 14707 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14708 return PC_BOUNDS_INVALID;
c906108c
SS
14709
14710 *lowpc = low;
96408a79
SA
14711 if (highpc)
14712 *highpc = high;
af34e669 14713 return ret;
c906108c
SS
14714}
14715
b084d499
JB
14716/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14717 its low and high PC addresses. Do nothing if these addresses could not
14718 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14719 and HIGHPC to the high address if greater than HIGHPC. */
14720
14721static void
14722dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14723 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14724 struct dwarf2_cu *cu)
14725{
14726 CORE_ADDR low, high;
14727 struct die_info *child = die->child;
14728
e385593e 14729 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14730 {
325fac50
PA
14731 *lowpc = std::min (*lowpc, low);
14732 *highpc = std::max (*highpc, high);
b084d499
JB
14733 }
14734
14735 /* If the language does not allow nested subprograms (either inside
14736 subprograms or lexical blocks), we're done. */
14737 if (cu->language != language_ada)
14738 return;
6e70227d 14739
b084d499
JB
14740 /* Check all the children of the given DIE. If it contains nested
14741 subprograms, then check their pc bounds. Likewise, we need to
14742 check lexical blocks as well, as they may also contain subprogram
14743 definitions. */
14744 while (child && child->tag)
14745 {
14746 if (child->tag == DW_TAG_subprogram
14747 || child->tag == DW_TAG_lexical_block)
14748 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14749 child = sibling_die (child);
14750 }
14751}
14752
fae299cd
DC
14753/* Get the low and high pc's represented by the scope DIE, and store
14754 them in *LOWPC and *HIGHPC. If the correct values can't be
14755 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14756
14757static void
14758get_scope_pc_bounds (struct die_info *die,
14759 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14760 struct dwarf2_cu *cu)
14761{
14762 CORE_ADDR best_low = (CORE_ADDR) -1;
14763 CORE_ADDR best_high = (CORE_ADDR) 0;
14764 CORE_ADDR current_low, current_high;
14765
3a2b436a 14766 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14767 >= PC_BOUNDS_RANGES)
fae299cd
DC
14768 {
14769 best_low = current_low;
14770 best_high = current_high;
14771 }
14772 else
14773 {
14774 struct die_info *child = die->child;
14775
14776 while (child && child->tag)
14777 {
14778 switch (child->tag) {
14779 case DW_TAG_subprogram:
b084d499 14780 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14781 break;
14782 case DW_TAG_namespace:
f55ee35c 14783 case DW_TAG_module:
fae299cd
DC
14784 /* FIXME: carlton/2004-01-16: Should we do this for
14785 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14786 that current GCC's always emit the DIEs corresponding
14787 to definitions of methods of classes as children of a
14788 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14789 the DIEs giving the declarations, which could be
14790 anywhere). But I don't see any reason why the
14791 standards says that they have to be there. */
14792 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14793
14794 if (current_low != ((CORE_ADDR) -1))
14795 {
325fac50
PA
14796 best_low = std::min (best_low, current_low);
14797 best_high = std::max (best_high, current_high);
fae299cd
DC
14798 }
14799 break;
14800 default:
0963b4bd 14801 /* Ignore. */
fae299cd
DC
14802 break;
14803 }
14804
14805 child = sibling_die (child);
14806 }
14807 }
14808
14809 *lowpc = best_low;
14810 *highpc = best_high;
14811}
14812
801e3a5b
JB
14813/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14814 in DIE. */
380bca97 14815
801e3a5b
JB
14816static void
14817dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14818 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14819{
518817b3 14820 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14821 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14822 struct attribute *attr;
91da1414 14823 struct attribute *attr_high;
801e3a5b 14824
91da1414
MW
14825 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14826 if (attr_high)
801e3a5b 14827 {
801e3a5b 14828 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14829 if (attr != nullptr)
801e3a5b 14830 {
31aa7e4e
JB
14831 CORE_ADDR low = attr_value_as_address (attr);
14832 CORE_ADDR high = attr_value_as_address (attr_high);
14833
14834 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14835 high += low;
9a619af0 14836
3e29f34a
MR
14837 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14838 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14839 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14840 }
14841 }
14842
14843 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14844 if (attr != nullptr)
801e3a5b 14845 {
18a8505e 14846 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14847 We take advantage of the fact that DW_AT_ranges does not appear
14848 in DW_TAG_compile_unit of DWO files. */
14849 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14850
14851 /* The value of the DW_AT_ranges attribute is the offset of the
14852 address range list in the .debug_ranges section. */
ab435259
DE
14853 unsigned long offset = (DW_UNSND (attr)
14854 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14855
2d5f09ec 14856 std::vector<blockrange> blockvec;
5f46c5a5
JK
14857 dwarf2_ranges_process (offset, cu,
14858 [&] (CORE_ADDR start, CORE_ADDR end)
14859 {
58fdfd2c
JK
14860 start += baseaddr;
14861 end += baseaddr;
5f46c5a5
JK
14862 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14863 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14864 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14865 blockvec.emplace_back (start, end);
5f46c5a5 14866 });
2d5f09ec
KB
14867
14868 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14869 }
14870}
14871
685b1105
JK
14872/* Check whether the producer field indicates either of GCC < 4.6, or the
14873 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14874
685b1105
JK
14875static void
14876check_producer (struct dwarf2_cu *cu)
60d5a603 14877{
38360086 14878 int major, minor;
60d5a603
JK
14879
14880 if (cu->producer == NULL)
14881 {
14882 /* For unknown compilers expect their behavior is DWARF version
14883 compliant.
14884
14885 GCC started to support .debug_types sections by -gdwarf-4 since
14886 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14887 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14888 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14889 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14890 }
b1ffba5a 14891 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14892 {
38360086
MW
14893 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14894 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14895 }
5230b05a 14896 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14897 {
14898 cu->producer_is_icc = true;
14899 cu->producer_is_icc_lt_14 = major < 14;
14900 }
c258c396
JD
14901 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14902 cu->producer_is_codewarrior = true;
685b1105
JK
14903 else
14904 {
14905 /* For other non-GCC compilers, expect their behavior is DWARF version
14906 compliant. */
60d5a603
JK
14907 }
14908
9068261f 14909 cu->checked_producer = true;
685b1105 14910}
ba919b58 14911
685b1105
JK
14912/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14913 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14914 during 4.6.0 experimental. */
14915
9068261f 14916static bool
685b1105
JK
14917producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14918{
14919 if (!cu->checked_producer)
14920 check_producer (cu);
14921
14922 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14923}
14924
c258c396
JD
14925
14926/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14927 with incorrect is_stmt attributes. */
14928
14929static bool
14930producer_is_codewarrior (struct dwarf2_cu *cu)
14931{
14932 if (!cu->checked_producer)
14933 check_producer (cu);
14934
14935 return cu->producer_is_codewarrior;
14936}
14937
405feb71 14938/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14939 DW_AT_accessibility. */
14940
14941static enum dwarf_access_attribute
14942dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14943{
14944 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14945 {
14946 /* The default DWARF 2 accessibility for members is public, the default
14947 accessibility for inheritance is private. */
14948
14949 if (die->tag != DW_TAG_inheritance)
14950 return DW_ACCESS_public;
14951 else
14952 return DW_ACCESS_private;
14953 }
14954 else
14955 {
14956 /* DWARF 3+ defines the default accessibility a different way. The same
14957 rules apply now for DW_TAG_inheritance as for the members and it only
14958 depends on the container kind. */
14959
14960 if (die->parent->tag == DW_TAG_class_type)
14961 return DW_ACCESS_private;
14962 else
14963 return DW_ACCESS_public;
14964 }
14965}
14966
74ac6d43
TT
14967/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14968 offset. If the attribute was not found return 0, otherwise return
14969 1. If it was found but could not properly be handled, set *OFFSET
14970 to 0. */
14971
14972static int
14973handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14974 LONGEST *offset)
14975{
14976 struct attribute *attr;
14977
14978 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14979 if (attr != NULL)
14980 {
14981 *offset = 0;
14982
14983 /* Note that we do not check for a section offset first here.
14984 This is because DW_AT_data_member_location is new in DWARF 4,
14985 so if we see it, we can assume that a constant form is really
14986 a constant and not a section offset. */
14987 if (attr_form_is_constant (attr))
14988 *offset = dwarf2_get_attr_constant_value (attr, 0);
14989 else if (attr_form_is_section_offset (attr))
14990 dwarf2_complex_location_expr_complaint ();
14991 else if (attr_form_is_block (attr))
14992 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14993 else
14994 dwarf2_complex_location_expr_complaint ();
14995
14996 return 1;
14997 }
14998
14999 return 0;
15000}
15001
c906108c
SS
15002/* Add an aggregate field to the field list. */
15003
15004static void
107d2387 15005dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15006 struct dwarf2_cu *cu)
6e70227d 15007{
518817b3 15008 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15009 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15010 struct nextfield *new_field;
15011 struct attribute *attr;
15012 struct field *fp;
15d034d0 15013 const char *fieldname = "";
c906108c 15014
7d0ccb61
DJ
15015 if (die->tag == DW_TAG_inheritance)
15016 {
be2daae6
TT
15017 fip->baseclasses.emplace_back ();
15018 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15019 }
15020 else
15021 {
be2daae6
TT
15022 fip->fields.emplace_back ();
15023 new_field = &fip->fields.back ();
7d0ccb61 15024 }
be2daae6 15025
c906108c
SS
15026 fip->nfields++;
15027
e142c38c 15028 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15029 if (attr != nullptr)
c906108c 15030 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15031 else
15032 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15033 if (new_field->accessibility != DW_ACCESS_public)
15034 fip->non_public_fields = 1;
60d5a603 15035
e142c38c 15036 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15037 if (attr != nullptr)
c906108c 15038 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15039 else
15040 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15041
15042 fp = &new_field->field;
a9a9bd0f 15043
e142c38c 15044 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15045 {
74ac6d43
TT
15046 LONGEST offset;
15047
a9a9bd0f 15048 /* Data member other than a C++ static data member. */
6e70227d 15049
c906108c 15050 /* Get type of field. */
e7c27a73 15051 fp->type = die_type (die, cu);
c906108c 15052
d6a843b5 15053 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15054
c906108c 15055 /* Get bit size of field (zero if none). */
e142c38c 15056 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15057 if (attr != nullptr)
c906108c
SS
15058 {
15059 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15060 }
15061 else
15062 {
15063 FIELD_BITSIZE (*fp) = 0;
15064 }
15065
15066 /* Get bit offset of field. */
74ac6d43
TT
15067 if (handle_data_member_location (die, cu, &offset))
15068 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15069 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15070 if (attr != nullptr)
c906108c 15071 {
d5a22e77 15072 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15073 {
15074 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15075 additional bit offset from the MSB of the containing
15076 anonymous object to the MSB of the field. We don't
15077 have to do anything special since we don't need to
15078 know the size of the anonymous object. */
f41f5e61 15079 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15080 }
15081 else
15082 {
15083 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15084 MSB of the anonymous object, subtract off the number of
15085 bits from the MSB of the field to the MSB of the
15086 object, and then subtract off the number of bits of
15087 the field itself. The result is the bit offset of
15088 the LSB of the field. */
c906108c
SS
15089 int anonymous_size;
15090 int bit_offset = DW_UNSND (attr);
15091
e142c38c 15092 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15093 if (attr != nullptr)
c906108c
SS
15094 {
15095 /* The size of the anonymous object containing
15096 the bit field is explicit, so use the
15097 indicated size (in bytes). */
15098 anonymous_size = DW_UNSND (attr);
15099 }
15100 else
15101 {
15102 /* The size of the anonymous object containing
15103 the bit field must be inferred from the type
15104 attribute of the data member containing the
15105 bit field. */
15106 anonymous_size = TYPE_LENGTH (fp->type);
15107 }
f41f5e61
PA
15108 SET_FIELD_BITPOS (*fp,
15109 (FIELD_BITPOS (*fp)
15110 + anonymous_size * bits_per_byte
15111 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15112 }
15113 }
da5b30da
AA
15114 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15115 if (attr != NULL)
15116 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15117 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15118
15119 /* Get name of field. */
39cbfefa
DJ
15120 fieldname = dwarf2_name (die, cu);
15121 if (fieldname == NULL)
15122 fieldname = "";
d8151005
DJ
15123
15124 /* The name is already allocated along with this objfile, so we don't
15125 need to duplicate it for the type. */
15126 fp->name = fieldname;
c906108c
SS
15127
15128 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15129 pointer or virtual base class pointer) to private. */
e142c38c 15130 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15131 {
d48cc9dd 15132 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15133 new_field->accessibility = DW_ACCESS_private;
15134 fip->non_public_fields = 1;
15135 }
15136 }
a9a9bd0f 15137 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15138 {
a9a9bd0f
DC
15139 /* C++ static member. */
15140
15141 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15142 is a declaration, but all versions of G++ as of this writing
15143 (so through at least 3.2.1) incorrectly generate
15144 DW_TAG_variable tags. */
6e70227d 15145
ff355380 15146 const char *physname;
c906108c 15147
a9a9bd0f 15148 /* Get name of field. */
39cbfefa
DJ
15149 fieldname = dwarf2_name (die, cu);
15150 if (fieldname == NULL)
c906108c
SS
15151 return;
15152
254e6b9e 15153 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15154 if (attr
15155 /* Only create a symbol if this is an external value.
15156 new_symbol checks this and puts the value in the global symbol
15157 table, which we want. If it is not external, new_symbol
15158 will try to put the value in cu->list_in_scope which is wrong. */
15159 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15160 {
15161 /* A static const member, not much different than an enum as far as
15162 we're concerned, except that we can support more types. */
15163 new_symbol (die, NULL, cu);
15164 }
15165
2df3850c 15166 /* Get physical name. */
ff355380 15167 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15168
d8151005
DJ
15169 /* The name is already allocated along with this objfile, so we don't
15170 need to duplicate it for the type. */
15171 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15172 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15173 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15174 }
15175 else if (die->tag == DW_TAG_inheritance)
15176 {
74ac6d43 15177 LONGEST offset;
d4b96c9a 15178
74ac6d43
TT
15179 /* C++ base class field. */
15180 if (handle_data_member_location (die, cu, &offset))
15181 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15182 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15183 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15184 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15185 }
2ddeaf8a
TT
15186 else if (die->tag == DW_TAG_variant_part)
15187 {
15188 /* process_structure_scope will treat this DIE as a union. */
15189 process_structure_scope (die, cu);
15190
15191 /* The variant part is relative to the start of the enclosing
15192 structure. */
15193 SET_FIELD_BITPOS (*fp, 0);
15194 fp->type = get_die_type (die, cu);
15195 fp->artificial = 1;
15196 fp->name = "<<variant>>";
c8c81635
TT
15197
15198 /* Normally a DW_TAG_variant_part won't have a size, but our
15199 representation requires one, so set it to the maximum of the
489dbda6
TT
15200 child sizes, being sure to account for the offset at which
15201 each child is seen. */
c8c81635
TT
15202 if (TYPE_LENGTH (fp->type) == 0)
15203 {
15204 unsigned max = 0;
15205 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
15206 {
15207 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15208 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15209 if (len > max)
15210 max = len;
15211 }
c8c81635
TT
15212 TYPE_LENGTH (fp->type) = max;
15213 }
2ddeaf8a
TT
15214 }
15215 else
15216 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15217}
15218
883fd55a
KS
15219/* Can the type given by DIE define another type? */
15220
15221static bool
15222type_can_define_types (const struct die_info *die)
15223{
15224 switch (die->tag)
15225 {
15226 case DW_TAG_typedef:
15227 case DW_TAG_class_type:
15228 case DW_TAG_structure_type:
15229 case DW_TAG_union_type:
15230 case DW_TAG_enumeration_type:
15231 return true;
15232
15233 default:
15234 return false;
15235 }
15236}
15237
15238/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15239
15240static void
883fd55a
KS
15241dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15242 struct dwarf2_cu *cu)
6e70227d 15243{
be2daae6
TT
15244 struct decl_field fp;
15245 memset (&fp, 0, sizeof (fp));
98751a41 15246
883fd55a 15247 gdb_assert (type_can_define_types (die));
98751a41 15248
883fd55a 15249 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15250 fp.name = dwarf2_name (die, cu);
15251 fp.type = read_type_die (die, cu);
98751a41 15252
c191a687
KS
15253 /* Save accessibility. */
15254 enum dwarf_access_attribute accessibility;
15255 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15256 if (attr != NULL)
15257 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15258 else
15259 accessibility = dwarf2_default_access_attribute (die, cu);
15260 switch (accessibility)
15261 {
15262 case DW_ACCESS_public:
15263 /* The assumed value if neither private nor protected. */
15264 break;
15265 case DW_ACCESS_private:
be2daae6 15266 fp.is_private = 1;
c191a687
KS
15267 break;
15268 case DW_ACCESS_protected:
be2daae6 15269 fp.is_protected = 1;
c191a687
KS
15270 break;
15271 default:
b98664d3 15272 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15273 }
15274
883fd55a 15275 if (die->tag == DW_TAG_typedef)
be2daae6 15276 fip->typedef_field_list.push_back (fp);
883fd55a 15277 else
be2daae6 15278 fip->nested_types_list.push_back (fp);
98751a41
JK
15279}
15280
c906108c
SS
15281/* Create the vector of fields, and attach it to the type. */
15282
15283static void
fba45db2 15284dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15285 struct dwarf2_cu *cu)
c906108c
SS
15286{
15287 int nfields = fip->nfields;
15288
15289 /* Record the field count, allocate space for the array of fields,
15290 and create blank accessibility bitfields if necessary. */
15291 TYPE_NFIELDS (type) = nfields;
15292 TYPE_FIELDS (type) = (struct field *)
be2daae6 15293 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15294
b4ba55a1 15295 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15296 {
15297 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15298
15299 TYPE_FIELD_PRIVATE_BITS (type) =
15300 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15301 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15302
15303 TYPE_FIELD_PROTECTED_BITS (type) =
15304 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15305 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15306
774b6a14
TT
15307 TYPE_FIELD_IGNORE_BITS (type) =
15308 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15309 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15310 }
15311
15312 /* If the type has baseclasses, allocate and clear a bit vector for
15313 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15314 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15315 {
be2daae6 15316 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15317 unsigned char *pointer;
c906108c
SS
15318
15319 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15320 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15321 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15322 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15323 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15324 }
15325
2ddeaf8a
TT
15326 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15327 {
15328 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15329
be2daae6 15330 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15331 {
be2daae6
TT
15332 struct nextfield &field = fip->fields[index];
15333
15334 if (field.variant.is_discriminant)
2ddeaf8a 15335 di->discriminant_index = index;
be2daae6 15336 else if (field.variant.default_branch)
2ddeaf8a
TT
15337 di->default_index = index;
15338 else
be2daae6 15339 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15340 }
15341 }
15342
be2daae6
TT
15343 /* Copy the saved-up fields into the field vector. */
15344 for (int i = 0; i < nfields; ++i)
c906108c 15345 {
be2daae6
TT
15346 struct nextfield &field
15347 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15348 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15349
be2daae6
TT
15350 TYPE_FIELD (type, i) = field.field;
15351 switch (field.accessibility)
c906108c 15352 {
c5aa993b 15353 case DW_ACCESS_private:
b4ba55a1 15354 if (cu->language != language_ada)
be2daae6 15355 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15356 break;
c906108c 15357
c5aa993b 15358 case DW_ACCESS_protected:
b4ba55a1 15359 if (cu->language != language_ada)
be2daae6 15360 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15361 break;
c906108c 15362
c5aa993b
JM
15363 case DW_ACCESS_public:
15364 break;
c906108c 15365
c5aa993b
JM
15366 default:
15367 /* Unknown accessibility. Complain and treat it as public. */
15368 {
b98664d3 15369 complaint (_("unsupported accessibility %d"),
be2daae6 15370 field.accessibility);
c5aa993b
JM
15371 }
15372 break;
c906108c 15373 }
be2daae6 15374 if (i < fip->baseclasses.size ())
c906108c 15375 {
be2daae6 15376 switch (field.virtuality)
c906108c 15377 {
c5aa993b
JM
15378 case DW_VIRTUALITY_virtual:
15379 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15380 if (cu->language == language_ada)
a73c6dcd 15381 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15382 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15383 break;
c906108c
SS
15384 }
15385 }
c906108c
SS
15386 }
15387}
15388
7d27a96d
TT
15389/* Return true if this member function is a constructor, false
15390 otherwise. */
15391
15392static int
15393dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15394{
15395 const char *fieldname;
fe978cb0 15396 const char *type_name;
7d27a96d
TT
15397 int len;
15398
15399 if (die->parent == NULL)
15400 return 0;
15401
15402 if (die->parent->tag != DW_TAG_structure_type
15403 && die->parent->tag != DW_TAG_union_type
15404 && die->parent->tag != DW_TAG_class_type)
15405 return 0;
15406
15407 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15408 type_name = dwarf2_name (die->parent, cu);
15409 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15410 return 0;
15411
15412 len = strlen (fieldname);
fe978cb0
PA
15413 return (strncmp (fieldname, type_name, len) == 0
15414 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15415}
15416
e35000a7
TBA
15417/* Check if the given VALUE is a recognized enum
15418 dwarf_defaulted_attribute constant according to DWARF5 spec,
15419 Table 7.24. */
15420
15421static bool
15422is_valid_DW_AT_defaulted (ULONGEST value)
15423{
15424 switch (value)
15425 {
15426 case DW_DEFAULTED_no:
15427 case DW_DEFAULTED_in_class:
15428 case DW_DEFAULTED_out_of_class:
15429 return true;
15430 }
15431
3142e908 15432 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15433 return false;
15434}
15435
c906108c
SS
15436/* Add a member function to the proper fieldlist. */
15437
15438static void
107d2387 15439dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15440 struct type *type, struct dwarf2_cu *cu)
c906108c 15441{
518817b3 15442 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15443 struct attribute *attr;
c906108c 15444 int i;
be2daae6 15445 struct fnfieldlist *flp = nullptr;
c906108c 15446 struct fn_field *fnp;
15d034d0 15447 const char *fieldname;
f792889a 15448 struct type *this_type;
60d5a603 15449 enum dwarf_access_attribute accessibility;
c906108c 15450
b4ba55a1 15451 if (cu->language == language_ada)
a73c6dcd 15452 error (_("unexpected member function in Ada type"));
b4ba55a1 15453
2df3850c 15454 /* Get name of member function. */
39cbfefa
DJ
15455 fieldname = dwarf2_name (die, cu);
15456 if (fieldname == NULL)
2df3850c 15457 return;
c906108c 15458
c906108c 15459 /* Look up member function name in fieldlist. */
be2daae6 15460 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15461 {
27bfe10e 15462 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15463 {
15464 flp = &fip->fnfieldlists[i];
15465 break;
15466 }
c906108c
SS
15467 }
15468
be2daae6
TT
15469 /* Create a new fnfieldlist if necessary. */
15470 if (flp == nullptr)
c906108c 15471 {
be2daae6
TT
15472 fip->fnfieldlists.emplace_back ();
15473 flp = &fip->fnfieldlists.back ();
c906108c 15474 flp->name = fieldname;
be2daae6 15475 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15476 }
15477
be2daae6
TT
15478 /* Create a new member function field and add it to the vector of
15479 fnfieldlists. */
15480 flp->fnfields.emplace_back ();
15481 fnp = &flp->fnfields.back ();
3da10d80
KS
15482
15483 /* Delay processing of the physname until later. */
9c37b5ae 15484 if (cu->language == language_cplus)
be2daae6
TT
15485 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15486 die, cu);
3da10d80
KS
15487 else
15488 {
1d06ead6 15489 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15490 fnp->physname = physname ? physname : "";
15491 }
15492
c906108c 15493 fnp->type = alloc_type (objfile);
f792889a
DJ
15494 this_type = read_type_die (die, cu);
15495 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15496 {
f792889a 15497 int nparams = TYPE_NFIELDS (this_type);
c906108c 15498
f792889a 15499 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15500 of the method itself (TYPE_CODE_METHOD). */
15501 smash_to_method_type (fnp->type, type,
f792889a
DJ
15502 TYPE_TARGET_TYPE (this_type),
15503 TYPE_FIELDS (this_type),
15504 TYPE_NFIELDS (this_type),
15505 TYPE_VARARGS (this_type));
c906108c
SS
15506
15507 /* Handle static member functions.
c5aa993b 15508 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15509 member functions. G++ helps GDB by marking the first
15510 parameter for non-static member functions (which is the this
15511 pointer) as artificial. We obtain this information from
15512 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15513 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15514 fnp->voffset = VOFFSET_STATIC;
15515 }
15516 else
b98664d3 15517 complaint (_("member function type missing for '%s'"),
3da10d80 15518 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15519
15520 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15521 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15522 fnp->fcontext = die_containing_type (die, cu);
c906108c 15523
3e43a32a
MS
15524 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15525 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15526
15527 /* Get accessibility. */
e142c38c 15528 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15529 if (attr != nullptr)
aead7601 15530 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15531 else
15532 accessibility = dwarf2_default_access_attribute (die, cu);
15533 switch (accessibility)
c906108c 15534 {
60d5a603
JK
15535 case DW_ACCESS_private:
15536 fnp->is_private = 1;
15537 break;
15538 case DW_ACCESS_protected:
15539 fnp->is_protected = 1;
15540 break;
c906108c
SS
15541 }
15542
b02dede2 15543 /* Check for artificial methods. */
e142c38c 15544 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15545 if (attr && DW_UNSND (attr) != 0)
15546 fnp->is_artificial = 1;
15547
e35000a7
TBA
15548 /* Check for defaulted methods. */
15549 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15550 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15551 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15552
15553 /* Check for deleted methods. */
15554 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15555 if (attr != nullptr && DW_UNSND (attr) != 0)
15556 fnp->is_deleted = 1;
15557
7d27a96d
TT
15558 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15559
0d564a31 15560 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15561 function. For older versions of GCC, this is an offset in the
15562 appropriate virtual table, as specified by DW_AT_containing_type.
15563 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15564 to the object address. */
15565
e142c38c 15566 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15567 if (attr != nullptr)
8e19ed76 15568 {
aec5aa8b 15569 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15570 {
aec5aa8b
TT
15571 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15572 {
15573 /* Old-style GCC. */
15574 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15575 }
15576 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15577 || (DW_BLOCK (attr)->size > 1
15578 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15579 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15580 {
aec5aa8b
TT
15581 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15582 if ((fnp->voffset % cu->header.addr_size) != 0)
15583 dwarf2_complex_location_expr_complaint ();
15584 else
15585 fnp->voffset /= cu->header.addr_size;
15586 fnp->voffset += 2;
15587 }
15588 else
15589 dwarf2_complex_location_expr_complaint ();
15590
15591 if (!fnp->fcontext)
7e993ebf
KS
15592 {
15593 /* If there is no `this' field and no DW_AT_containing_type,
15594 we cannot actually find a base class context for the
15595 vtable! */
15596 if (TYPE_NFIELDS (this_type) == 0
15597 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15598 {
b98664d3 15599 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15600 "function \"%s\" (offset %s)"),
15601 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15602 }
15603 else
15604 {
15605 fnp->fcontext
15606 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15607 }
15608 }
aec5aa8b 15609 }
3690dd37 15610 else if (attr_form_is_section_offset (attr))
8e19ed76 15611 {
4d3c2250 15612 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15613 }
15614 else
15615 {
4d3c2250
KB
15616 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15617 fieldname);
8e19ed76 15618 }
0d564a31 15619 }
d48cc9dd
DJ
15620 else
15621 {
15622 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15623 if (attr && DW_UNSND (attr))
15624 {
15625 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15626 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15627 "but the vtable offset is not specified"),
9d8780f0 15628 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15629 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15630 TYPE_CPLUS_DYNAMIC (type) = 1;
15631 }
15632 }
c906108c
SS
15633}
15634
15635/* Create the vector of member function fields, and attach it to the type. */
15636
15637static void
fba45db2 15638dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15639 struct dwarf2_cu *cu)
c906108c 15640{
b4ba55a1 15641 if (cu->language == language_ada)
a73c6dcd 15642 error (_("unexpected member functions in Ada type"));
b4ba55a1 15643
c906108c
SS
15644 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15645 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15646 TYPE_ALLOC (type,
15647 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15648
be2daae6 15649 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15650 {
be2daae6 15651 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15652 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15653
be2daae6
TT
15654 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15655 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15656 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15657 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15658
15659 for (int k = 0; k < nf.fnfields.size (); ++k)
15660 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15661 }
15662
be2daae6 15663 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15664}
15665
1168df01
JB
15666/* Returns non-zero if NAME is the name of a vtable member in CU's
15667 language, zero otherwise. */
15668static int
15669is_vtable_name (const char *name, struct dwarf2_cu *cu)
15670{
15671 static const char vptr[] = "_vptr";
15672
9c37b5ae
TT
15673 /* Look for the C++ form of the vtable. */
15674 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15675 return 1;
15676
15677 return 0;
15678}
15679
c0dd20ea 15680/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15681 functions, with the ABI-specified layout. If TYPE describes
15682 such a structure, smash it into a member function type.
61049d3b
DJ
15683
15684 GCC shouldn't do this; it should just output pointer to member DIEs.
15685 This is GCC PR debug/28767. */
c0dd20ea 15686
0b92b5bb
TT
15687static void
15688quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15689{
09e2d7c7 15690 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15691
15692 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15693 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15694 return;
c0dd20ea
DJ
15695
15696 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15697 if (TYPE_FIELD_NAME (type, 0) == NULL
15698 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15699 || TYPE_FIELD_NAME (type, 1) == NULL
15700 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15701 return;
c0dd20ea
DJ
15702
15703 /* Find the type of the method. */
0b92b5bb 15704 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15705 if (pfn_type == NULL
15706 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15707 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15708 return;
c0dd20ea
DJ
15709
15710 /* Look for the "this" argument. */
15711 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15712 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15713 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15714 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15715 return;
c0dd20ea 15716
09e2d7c7 15717 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15718 new_type = alloc_type (objfile);
09e2d7c7 15719 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15720 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15721 TYPE_VARARGS (pfn_type));
0b92b5bb 15722 smash_to_methodptr_type (type, new_type);
c0dd20ea 15723}
1168df01 15724
2b4424c3
TT
15725/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15726 appropriate error checking and issuing complaints if there is a
15727 problem. */
15728
15729static ULONGEST
15730get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15731{
15732 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15733
15734 if (attr == nullptr)
15735 return 0;
15736
15737 if (!attr_form_is_constant (attr))
15738 {
b98664d3 15739 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15740 " - DIE at %s [in module %s]"),
15741 sect_offset_str (die->sect_off),
15742 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15743 return 0;
15744 }
15745
15746 ULONGEST align;
15747 if (attr->form == DW_FORM_sdata)
15748 {
15749 LONGEST val = DW_SND (attr);
15750 if (val < 0)
15751 {
b98664d3 15752 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15753 " - DIE at %s [in module %s]"),
15754 sect_offset_str (die->sect_off),
15755 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15756 return 0;
15757 }
15758 align = val;
15759 }
15760 else
15761 align = DW_UNSND (attr);
15762
15763 if (align == 0)
15764 {
b98664d3 15765 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15766 " - DIE at %s [in module %s]"),
15767 sect_offset_str (die->sect_off),
15768 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15769 return 0;
15770 }
15771 if ((align & (align - 1)) != 0)
15772 {
b98664d3 15773 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15774 " - DIE at %s [in module %s]"),
15775 sect_offset_str (die->sect_off),
15776 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15777 return 0;
15778 }
15779
15780 return align;
15781}
15782
15783/* If the DIE has a DW_AT_alignment attribute, use its value to set
15784 the alignment for TYPE. */
15785
15786static void
15787maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15788 struct type *type)
15789{
15790 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15791 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15792 " - DIE at %s [in module %s]"),
15793 sect_offset_str (die->sect_off),
15794 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15795}
685b1105 15796
e35000a7
TBA
15797/* Check if the given VALUE is a valid enum dwarf_calling_convention
15798 constant for a type, according to DWARF5 spec, Table 5.5. */
15799
15800static bool
15801is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15802{
15803 switch (value)
15804 {
15805 case DW_CC_normal:
15806 case DW_CC_pass_by_reference:
15807 case DW_CC_pass_by_value:
15808 return true;
15809
15810 default:
15811 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15812 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15813 return false;
15814 }
15815}
15816
d0922fcf
TBA
15817/* Check if the given VALUE is a valid enum dwarf_calling_convention
15818 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15819 also according to GNU-specific values (see include/dwarf2.h). */
15820
15821static bool
15822is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15823{
15824 switch (value)
15825 {
15826 case DW_CC_normal:
15827 case DW_CC_program:
15828 case DW_CC_nocall:
15829 return true;
15830
15831 case DW_CC_GNU_renesas_sh:
15832 case DW_CC_GNU_borland_fastcall_i386:
15833 case DW_CC_GDB_IBM_OpenCL:
15834 return true;
15835
15836 default:
15837 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15838 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15839 return false;
15840 }
15841}
15842
c906108c 15843/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15844 (definition) to create a type for the structure or union. Fill in
15845 the type's name and general properties; the members will not be
83655187
DE
15846 processed until process_structure_scope. A symbol table entry for
15847 the type will also not be done until process_structure_scope (assuming
15848 the type has a name).
c906108c 15849
c767944b
DJ
15850 NOTE: we need to call these functions regardless of whether or not the
15851 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15852 structure or union. This gets the type entered into our set of
83655187 15853 user defined types. */
c906108c 15854
f792889a 15855static struct type *
134d01f1 15856read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15857{
518817b3 15858 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15859 struct type *type;
15860 struct attribute *attr;
15d034d0 15861 const char *name;
c906108c 15862
348e048f
DE
15863 /* If the definition of this type lives in .debug_types, read that type.
15864 Don't follow DW_AT_specification though, that will take us back up
15865 the chain and we want to go down. */
45e58e77 15866 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15867 if (attr != nullptr)
348e048f 15868 {
ac9ec31b 15869 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15870
ac9ec31b 15871 /* The type's CU may not be the same as CU.
02142a6c 15872 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15873 return set_die_type (die, type, cu);
15874 }
15875
c0dd20ea 15876 type = alloc_type (objfile);
c906108c 15877 INIT_CPLUS_SPECIFIC (type);
93311388 15878
39cbfefa
DJ
15879 name = dwarf2_name (die, cu);
15880 if (name != NULL)
c906108c 15881 {
987504bb 15882 if (cu->language == language_cplus
c44af4eb
TT
15883 || cu->language == language_d
15884 || cu->language == language_rust)
63d06c5c 15885 {
15d034d0 15886 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15887
15888 /* dwarf2_full_name might have already finished building the DIE's
15889 type. If so, there is no need to continue. */
15890 if (get_die_type (die, cu) != NULL)
15891 return get_die_type (die, cu);
15892
e86ca25f 15893 TYPE_NAME (type) = full_name;
63d06c5c
DC
15894 }
15895 else
15896 {
d8151005
DJ
15897 /* The name is already allocated along with this objfile, so
15898 we don't need to duplicate it for the type. */
e86ca25f 15899 TYPE_NAME (type) = name;
63d06c5c 15900 }
c906108c
SS
15901 }
15902
15903 if (die->tag == DW_TAG_structure_type)
15904 {
15905 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15906 }
15907 else if (die->tag == DW_TAG_union_type)
15908 {
15909 TYPE_CODE (type) = TYPE_CODE_UNION;
15910 }
2ddeaf8a
TT
15911 else if (die->tag == DW_TAG_variant_part)
15912 {
15913 TYPE_CODE (type) = TYPE_CODE_UNION;
15914 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15915 }
c906108c
SS
15916 else
15917 {
4753d33b 15918 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15919 }
15920
0cc2414c
TT
15921 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15922 TYPE_DECLARED_CLASS (type) = 1;
15923
e35000a7
TBA
15924 /* Store the calling convention in the type if it's available in
15925 the die. Otherwise the calling convention remains set to
15926 the default value DW_CC_normal. */
15927 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15928 if (attr != nullptr
15929 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15930 {
15931 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15932 TYPE_CPLUS_CALLING_CONVENTION (type)
15933 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15934 }
15935
e142c38c 15936 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15937 if (attr != nullptr)
c906108c 15938 {
155bfbd3
JB
15939 if (attr_form_is_constant (attr))
15940 TYPE_LENGTH (type) = DW_UNSND (attr);
15941 else
15942 {
15943 /* For the moment, dynamic type sizes are not supported
15944 by GDB's struct type. The actual size is determined
15945 on-demand when resolving the type of a given object,
15946 so set the type's length to zero for now. Otherwise,
15947 we record an expression as the length, and that expression
15948 could lead to a very large value, which could eventually
15949 lead to us trying to allocate that much memory when creating
15950 a value of that type. */
15951 TYPE_LENGTH (type) = 0;
15952 }
c906108c
SS
15953 }
15954 else
15955 {
15956 TYPE_LENGTH (type) = 0;
15957 }
15958
2b4424c3
TT
15959 maybe_set_alignment (cu, die, type);
15960
5230b05a 15961 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15962 {
5230b05a
WT
15963 /* ICC<14 does not output the required DW_AT_declaration on
15964 incomplete types, but gives them a size of zero. */
422b1cb0 15965 TYPE_STUB (type) = 1;
685b1105
JK
15966 }
15967 else
15968 TYPE_STUB_SUPPORTED (type) = 1;
15969
dc718098 15970 if (die_is_declaration (die, cu))
876cecd0 15971 TYPE_STUB (type) = 1;
a6c727b2
DJ
15972 else if (attr == NULL && die->child == NULL
15973 && producer_is_realview (cu->producer))
15974 /* RealView does not output the required DW_AT_declaration
15975 on incomplete types. */
15976 TYPE_STUB (type) = 1;
dc718098 15977
c906108c
SS
15978 /* We need to add the type field to the die immediately so we don't
15979 infinitely recurse when dealing with pointers to the structure
0963b4bd 15980 type within the structure itself. */
1c379e20 15981 set_die_type (die, type, cu);
c906108c 15982
7e314c57
JK
15983 /* set_die_type should be already done. */
15984 set_descriptive_type (type, die, cu);
15985
c767944b
DJ
15986 return type;
15987}
15988
2ddeaf8a
TT
15989/* A helper for process_structure_scope that handles a single member
15990 DIE. */
15991
15992static void
15993handle_struct_member_die (struct die_info *child_die, struct type *type,
15994 struct field_info *fi,
15995 std::vector<struct symbol *> *template_args,
15996 struct dwarf2_cu *cu)
15997{
15998 if (child_die->tag == DW_TAG_member
15999 || child_die->tag == DW_TAG_variable
16000 || child_die->tag == DW_TAG_variant_part)
16001 {
16002 /* NOTE: carlton/2002-11-05: A C++ static data member
16003 should be a DW_TAG_member that is a declaration, but
16004 all versions of G++ as of this writing (so through at
16005 least 3.2.1) incorrectly generate DW_TAG_variable
16006 tags for them instead. */
16007 dwarf2_add_field (fi, child_die, cu);
16008 }
16009 else if (child_die->tag == DW_TAG_subprogram)
16010 {
16011 /* Rust doesn't have member functions in the C++ sense.
16012 However, it does emit ordinary functions as children
16013 of a struct DIE. */
16014 if (cu->language == language_rust)
16015 read_func_scope (child_die, cu);
16016 else
16017 {
16018 /* C++ member function. */
16019 dwarf2_add_member_fn (fi, child_die, type, cu);
16020 }
16021 }
16022 else if (child_die->tag == DW_TAG_inheritance)
16023 {
16024 /* C++ base class field. */
16025 dwarf2_add_field (fi, child_die, cu);
16026 }
16027 else if (type_can_define_types (child_die))
16028 dwarf2_add_type_defn (fi, child_die, cu);
16029 else if (child_die->tag == DW_TAG_template_type_param
16030 || child_die->tag == DW_TAG_template_value_param)
16031 {
16032 struct symbol *arg = new_symbol (child_die, NULL, cu);
16033
16034 if (arg != NULL)
16035 template_args->push_back (arg);
16036 }
16037 else if (child_die->tag == DW_TAG_variant)
16038 {
16039 /* In a variant we want to get the discriminant and also add a
16040 field for our sole member child. */
16041 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16042
bde09ab7 16043 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
16044 variant_child != NULL;
16045 variant_child = sibling_die (variant_child))
16046 {
16047 if (variant_child->tag == DW_TAG_member)
16048 {
16049 handle_struct_member_die (variant_child, type, fi,
16050 template_args, cu);
16051 /* Only handle the one. */
16052 break;
16053 }
16054 }
16055
16056 /* We don't handle this but we might as well report it if we see
16057 it. */
16058 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 16059 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
16060 " - DIE at %s [in module %s]"),
16061 sect_offset_str (child_die->sect_off),
16062 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16063
16064 /* The first field was just added, so we can stash the
16065 discriminant there. */
be2daae6 16066 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16067 if (discr == NULL)
be2daae6 16068 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16069 else
be2daae6 16070 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16071 }
16072}
16073
c767944b
DJ
16074/* Finish creating a structure or union type, including filling in
16075 its members and creating a symbol for it. */
16076
16077static void
16078process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16079{
518817b3 16080 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16081 struct die_info *child_die;
c767944b
DJ
16082 struct type *type;
16083
16084 type = get_die_type (die, cu);
16085 if (type == NULL)
16086 type = read_structure_type (die, cu);
16087
2ddeaf8a
TT
16088 /* When reading a DW_TAG_variant_part, we need to notice when we
16089 read the discriminant member, so we can record it later in the
16090 discriminant_info. */
16091 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 16092 sect_offset discr_offset {};
3e1d3d8c 16093 bool has_template_parameters = false;
2ddeaf8a
TT
16094
16095 if (is_variant_part)
16096 {
16097 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16098 if (discr == NULL)
16099 {
16100 /* Maybe it's a univariant form, an extension we support.
16101 In this case arrange not to check the offset. */
16102 is_variant_part = false;
16103 }
16104 else if (attr_form_is_ref (discr))
16105 {
16106 struct dwarf2_cu *target_cu = cu;
16107 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16108
16109 discr_offset = target_die->sect_off;
16110 }
16111 else
16112 {
b98664d3 16113 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16114 " - DIE at %s [in module %s]"),
16115 sect_offset_str (die->sect_off),
16116 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16117 is_variant_part = false;
16118 }
16119 }
16120
e142c38c 16121 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16122 {
16123 struct field_info fi;
2f4732b0 16124 std::vector<struct symbol *> template_args;
c906108c 16125
639d11d3 16126 child_die = die->child;
c906108c
SS
16127
16128 while (child_die && child_die->tag)
16129 {
2ddeaf8a 16130 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16131
2ddeaf8a 16132 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16133 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16134
c906108c
SS
16135 child_die = sibling_die (child_die);
16136 }
16137
34eaf542 16138 /* Attach template arguments to type. */
2f4732b0 16139 if (!template_args.empty ())
34eaf542 16140 {
3e1d3d8c 16141 has_template_parameters = true;
34eaf542 16142 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16143 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16144 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16145 = XOBNEWVEC (&objfile->objfile_obstack,
16146 struct symbol *,
16147 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16148 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16149 template_args.data (),
34eaf542
TT
16150 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16151 * sizeof (struct symbol *)));
34eaf542
TT
16152 }
16153
c906108c
SS
16154 /* Attach fields and member functions to the type. */
16155 if (fi.nfields)
e7c27a73 16156 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16157 if (!fi.fnfieldlists.empty ())
c906108c 16158 {
e7c27a73 16159 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16160
c5aa993b 16161 /* Get the type which refers to the base class (possibly this
c906108c 16162 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16163 class from the DW_AT_containing_type attribute. This use of
16164 DW_AT_containing_type is a GNU extension. */
c906108c 16165
e142c38c 16166 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16167 {
e7c27a73 16168 struct type *t = die_containing_type (die, cu);
c906108c 16169
ae6ae975 16170 set_type_vptr_basetype (type, t);
c906108c
SS
16171 if (type == t)
16172 {
c906108c
SS
16173 int i;
16174
16175 /* Our own class provides vtbl ptr. */
16176 for (i = TYPE_NFIELDS (t) - 1;
16177 i >= TYPE_N_BASECLASSES (t);
16178 --i)
16179 {
0d5cff50 16180 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16181
1168df01 16182 if (is_vtable_name (fieldname, cu))
c906108c 16183 {
ae6ae975 16184 set_type_vptr_fieldno (type, i);
c906108c
SS
16185 break;
16186 }
16187 }
16188
16189 /* Complain if virtual function table field not found. */
16190 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16191 complaint (_("virtual function table pointer "
3e43a32a 16192 "not found when defining class '%s'"),
e86ca25f 16193 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16194 }
16195 else
16196 {
ae6ae975 16197 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16198 }
16199 }
f6235d4c 16200 else if (cu->producer
61012eef 16201 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16202 {
16203 /* The IBM XLC compiler does not provide direct indication
16204 of the containing type, but the vtable pointer is
16205 always named __vfp. */
16206
16207 int i;
16208
16209 for (i = TYPE_NFIELDS (type) - 1;
16210 i >= TYPE_N_BASECLASSES (type);
16211 --i)
16212 {
16213 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16214 {
ae6ae975
DE
16215 set_type_vptr_fieldno (type, i);
16216 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16217 break;
16218 }
16219 }
16220 }
c906108c 16221 }
98751a41
JK
16222
16223 /* Copy fi.typedef_field_list linked list elements content into the
16224 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16225 if (!fi.typedef_field_list.empty ())
98751a41 16226 {
be2daae6 16227 int count = fi.typedef_field_list.size ();
98751a41 16228
a0d7a4ff 16229 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16230 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16231 = ((struct decl_field *)
be2daae6
TT
16232 TYPE_ALLOC (type,
16233 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16234 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16235
be2daae6
TT
16236 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16237 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16238 }
c767944b 16239
883fd55a
KS
16240 /* Copy fi.nested_types_list linked list elements content into the
16241 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16242 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16243 {
be2daae6 16244 int count = fi.nested_types_list.size ();
883fd55a
KS
16245
16246 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16247 TYPE_NESTED_TYPES_ARRAY (type)
16248 = ((struct decl_field *)
be2daae6
TT
16249 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16250 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16251
be2daae6
TT
16252 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16253 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16254 }
c906108c 16255 }
63d06c5c 16256
bb5ed363 16257 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16258 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16259 cu->rust_unions.push_back (type);
0b92b5bb 16260
90aeadfc
DC
16261 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16262 snapshots) has been known to create a die giving a declaration
16263 for a class that has, as a child, a die giving a definition for a
16264 nested class. So we have to process our children even if the
16265 current die is a declaration. Normally, of course, a declaration
16266 won't have any children at all. */
134d01f1 16267
ca040673
DE
16268 child_die = die->child;
16269
90aeadfc
DC
16270 while (child_die != NULL && child_die->tag)
16271 {
16272 if (child_die->tag == DW_TAG_member
16273 || child_die->tag == DW_TAG_variable
34eaf542
TT
16274 || child_die->tag == DW_TAG_inheritance
16275 || child_die->tag == DW_TAG_template_value_param
16276 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16277 {
90aeadfc 16278 /* Do nothing. */
134d01f1 16279 }
90aeadfc
DC
16280 else
16281 process_die (child_die, cu);
134d01f1 16282
90aeadfc 16283 child_die = sibling_die (child_die);
134d01f1
DJ
16284 }
16285
fa4028e9
JB
16286 /* Do not consider external references. According to the DWARF standard,
16287 these DIEs are identified by the fact that they have no byte_size
16288 attribute, and a declaration attribute. */
16289 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16290 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16291 {
16292 struct symbol *sym = new_symbol (die, type, cu);
16293
16294 if (has_template_parameters)
16295 {
a776957c
TT
16296 struct symtab *symtab;
16297 if (sym != nullptr)
16298 symtab = symbol_symtab (sym);
16299 else if (cu->line_header != nullptr)
16300 {
16301 /* Any related symtab will do. */
16302 symtab
7ba99d21 16303 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16304 }
16305 else
16306 {
16307 symtab = nullptr;
16308 complaint (_("could not find suitable "
16309 "symtab for template parameter"
16310 " - DIE at %s [in module %s]"),
16311 sect_offset_str (die->sect_off),
16312 objfile_name (objfile));
16313 }
16314
16315 if (symtab != nullptr)
16316 {
16317 /* Make sure that the symtab is set on the new symbols.
16318 Even though they don't appear in this symtab directly,
16319 other parts of gdb assume that symbols do, and this is
16320 reasonably true. */
16321 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16322 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16323 }
3e1d3d8c
TT
16324 }
16325 }
134d01f1
DJ
16326}
16327
55426c9d
JB
16328/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16329 update TYPE using some information only available in DIE's children. */
16330
16331static void
16332update_enumeration_type_from_children (struct die_info *die,
16333 struct type *type,
16334 struct dwarf2_cu *cu)
16335{
60f7655a 16336 struct die_info *child_die;
55426c9d
JB
16337 int unsigned_enum = 1;
16338 int flag_enum = 1;
16339 ULONGEST mask = 0;
55426c9d 16340
8268c778 16341 auto_obstack obstack;
55426c9d 16342
60f7655a
DE
16343 for (child_die = die->child;
16344 child_die != NULL && child_die->tag;
16345 child_die = sibling_die (child_die))
55426c9d
JB
16346 {
16347 struct attribute *attr;
16348 LONGEST value;
16349 const gdb_byte *bytes;
16350 struct dwarf2_locexpr_baton *baton;
16351 const char *name;
60f7655a 16352
55426c9d
JB
16353 if (child_die->tag != DW_TAG_enumerator)
16354 continue;
16355
16356 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16357 if (attr == NULL)
16358 continue;
16359
16360 name = dwarf2_name (child_die, cu);
16361 if (name == NULL)
16362 name = "<anonymous enumerator>";
16363
16364 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16365 &value, &bytes, &baton);
16366 if (value < 0)
16367 {
16368 unsigned_enum = 0;
16369 flag_enum = 0;
16370 }
16371 else if ((mask & value) != 0)
16372 flag_enum = 0;
16373 else
16374 mask |= value;
16375
16376 /* If we already know that the enum type is neither unsigned, nor
16377 a flag type, no need to look at the rest of the enumerates. */
16378 if (!unsigned_enum && !flag_enum)
16379 break;
55426c9d
JB
16380 }
16381
16382 if (unsigned_enum)
16383 TYPE_UNSIGNED (type) = 1;
16384 if (flag_enum)
16385 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16386}
16387
134d01f1
DJ
16388/* Given a DW_AT_enumeration_type die, set its type. We do not
16389 complete the type's fields yet, or create any symbols. */
c906108c 16390
f792889a 16391static struct type *
134d01f1 16392read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16393{
518817b3 16394 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16395 struct type *type;
c906108c 16396 struct attribute *attr;
0114d602 16397 const char *name;
134d01f1 16398
348e048f
DE
16399 /* If the definition of this type lives in .debug_types, read that type.
16400 Don't follow DW_AT_specification though, that will take us back up
16401 the chain and we want to go down. */
45e58e77 16402 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16403 if (attr != nullptr)
348e048f 16404 {
ac9ec31b 16405 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16406
ac9ec31b 16407 /* The type's CU may not be the same as CU.
02142a6c 16408 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16409 return set_die_type (die, type, cu);
16410 }
16411
c906108c
SS
16412 type = alloc_type (objfile);
16413
16414 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16415 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16416 if (name != NULL)
e86ca25f 16417 TYPE_NAME (type) = name;
c906108c 16418
0626fc76
TT
16419 attr = dwarf2_attr (die, DW_AT_type, cu);
16420 if (attr != NULL)
16421 {
16422 struct type *underlying_type = die_type (die, cu);
16423
16424 TYPE_TARGET_TYPE (type) = underlying_type;
16425 }
16426
e142c38c 16427 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16428 if (attr != nullptr)
c906108c
SS
16429 {
16430 TYPE_LENGTH (type) = DW_UNSND (attr);
16431 }
16432 else
16433 {
16434 TYPE_LENGTH (type) = 0;
16435 }
16436
2b4424c3
TT
16437 maybe_set_alignment (cu, die, type);
16438
137033e9
JB
16439 /* The enumeration DIE can be incomplete. In Ada, any type can be
16440 declared as private in the package spec, and then defined only
16441 inside the package body. Such types are known as Taft Amendment
16442 Types. When another package uses such a type, an incomplete DIE
16443 may be generated by the compiler. */
02eb380e 16444 if (die_is_declaration (die, cu))
876cecd0 16445 TYPE_STUB (type) = 1;
02eb380e 16446
0626fc76
TT
16447 /* Finish the creation of this type by using the enum's children.
16448 We must call this even when the underlying type has been provided
16449 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16450 update_enumeration_type_from_children (die, type, cu);
16451
0626fc76
TT
16452 /* If this type has an underlying type that is not a stub, then we
16453 may use its attributes. We always use the "unsigned" attribute
16454 in this situation, because ordinarily we guess whether the type
16455 is unsigned -- but the guess can be wrong and the underlying type
16456 can tell us the reality. However, we defer to a local size
16457 attribute if one exists, because this lets the compiler override
16458 the underlying type if needed. */
16459 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16460 {
16461 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16462 if (TYPE_LENGTH (type) == 0)
16463 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16464 if (TYPE_RAW_ALIGN (type) == 0
16465 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16466 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16467 }
16468
3d567982
TT
16469 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16470
f792889a 16471 return set_die_type (die, type, cu);
134d01f1
DJ
16472}
16473
16474/* Given a pointer to a die which begins an enumeration, process all
16475 the dies that define the members of the enumeration, and create the
16476 symbol for the enumeration type.
16477
16478 NOTE: We reverse the order of the element list. */
16479
16480static void
16481process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16482{
f792889a 16483 struct type *this_type;
134d01f1 16484
f792889a
DJ
16485 this_type = get_die_type (die, cu);
16486 if (this_type == NULL)
16487 this_type = read_enumeration_type (die, cu);
9dc481d3 16488
639d11d3 16489 if (die->child != NULL)
c906108c 16490 {
9dc481d3
DE
16491 struct die_info *child_die;
16492 struct symbol *sym;
43816ebc 16493 std::vector<struct field> fields;
15d034d0 16494 const char *name;
9dc481d3 16495
639d11d3 16496 child_die = die->child;
c906108c
SS
16497 while (child_die && child_die->tag)
16498 {
16499 if (child_die->tag != DW_TAG_enumerator)
16500 {
e7c27a73 16501 process_die (child_die, cu);
c906108c
SS
16502 }
16503 else
16504 {
39cbfefa
DJ
16505 name = dwarf2_name (child_die, cu);
16506 if (name)
c906108c 16507 {
f792889a 16508 sym = new_symbol (child_die, this_type, cu);
c906108c 16509
43816ebc
TT
16510 fields.emplace_back ();
16511 struct field &field = fields.back ();
c906108c 16512
43816ebc
TT
16513 FIELD_NAME (field) = sym->linkage_name ();
16514 FIELD_TYPE (field) = NULL;
16515 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16516 FIELD_BITSIZE (field) = 0;
c906108c
SS
16517 }
16518 }
16519
16520 child_die = sibling_die (child_die);
16521 }
16522
43816ebc 16523 if (!fields.empty ())
c906108c 16524 {
43816ebc 16525 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 16526 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
16527 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16528 memcpy (TYPE_FIELDS (this_type), fields.data (),
16529 sizeof (struct field) * fields.size ());
c906108c 16530 }
c906108c 16531 }
134d01f1 16532
6c83ed52
TT
16533 /* If we are reading an enum from a .debug_types unit, and the enum
16534 is a declaration, and the enum is not the signatured type in the
16535 unit, then we do not want to add a symbol for it. Adding a
16536 symbol would in some cases obscure the true definition of the
16537 enum, giving users an incomplete type when the definition is
16538 actually available. Note that we do not want to do this for all
16539 enums which are just declarations, because C++0x allows forward
16540 enum declarations. */
3019eac3 16541 if (cu->per_cu->is_debug_types
6c83ed52
TT
16542 && die_is_declaration (die, cu))
16543 {
52dc124a 16544 struct signatured_type *sig_type;
6c83ed52 16545
c0f78cd4 16546 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16547 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16548 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16549 return;
16550 }
16551
f792889a 16552 new_symbol (die, this_type, cu);
c906108c
SS
16553}
16554
16555/* Extract all information from a DW_TAG_array_type DIE and put it in
16556 the DIE's type field. For now, this only handles one dimensional
16557 arrays. */
16558
f792889a 16559static struct type *
e7c27a73 16560read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16561{
518817b3 16562 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16563 struct die_info *child_die;
7e314c57 16564 struct type *type;
c906108c 16565 struct type *element_type, *range_type, *index_type;
c906108c 16566 struct attribute *attr;
15d034d0 16567 const char *name;
a405673c 16568 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16569 unsigned int bit_stride = 0;
c906108c 16570
e7c27a73 16571 element_type = die_type (die, cu);
c906108c 16572
7e314c57
JK
16573 /* The die_type call above may have already set the type for this DIE. */
16574 type = get_die_type (die, cu);
16575 if (type)
16576 return type;
16577
dc53a7ad
JB
16578 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16579 if (attr != NULL)
a405673c
JB
16580 {
16581 int stride_ok;
9a49df9d
AB
16582 struct type *prop_type
16583 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16584
16585 byte_stride_prop
16586 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16587 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16588 prop_type);
a405673c
JB
16589 if (!stride_ok)
16590 {
b98664d3 16591 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16592 " - DIE at %s [in module %s]"),
16593 sect_offset_str (die->sect_off),
518817b3 16594 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16595 /* Ignore this attribute. We will likely not be able to print
16596 arrays of this type correctly, but there is little we can do
16597 to help if we cannot read the attribute's value. */
16598 byte_stride_prop = NULL;
16599 }
16600 }
dc53a7ad
JB
16601
16602 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16603 if (attr != NULL)
16604 bit_stride = DW_UNSND (attr);
16605
c906108c
SS
16606 /* Irix 6.2 native cc creates array types without children for
16607 arrays with unspecified length. */
639d11d3 16608 if (die->child == NULL)
c906108c 16609 {
46bf5051 16610 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16611 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16612 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16613 byte_stride_prop, bit_stride);
f792889a 16614 return set_die_type (die, type, cu);
c906108c
SS
16615 }
16616
791afaa2 16617 std::vector<struct type *> range_types;
639d11d3 16618 child_die = die->child;
c906108c
SS
16619 while (child_die && child_die->tag)
16620 {
16621 if (child_die->tag == DW_TAG_subrange_type)
16622 {
f792889a 16623 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16624
f792889a 16625 if (child_type != NULL)
a02abb62 16626 {
0963b4bd
MS
16627 /* The range type was succesfully read. Save it for the
16628 array type creation. */
791afaa2 16629 range_types.push_back (child_type);
a02abb62 16630 }
c906108c
SS
16631 }
16632 child_die = sibling_die (child_die);
16633 }
16634
16635 /* Dwarf2 dimensions are output from left to right, create the
16636 necessary array types in backwards order. */
7ca2d3a3 16637
c906108c 16638 type = element_type;
7ca2d3a3
DL
16639
16640 if (read_array_order (die, cu) == DW_ORD_col_major)
16641 {
16642 int i = 0;
9a619af0 16643
791afaa2 16644 while (i < range_types.size ())
dc53a7ad 16645 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16646 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16647 }
16648 else
16649 {
791afaa2 16650 size_t ndim = range_types.size ();
7ca2d3a3 16651 while (ndim-- > 0)
dc53a7ad 16652 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16653 byte_stride_prop, bit_stride);
7ca2d3a3 16654 }
c906108c 16655
f5f8a009
EZ
16656 /* Understand Dwarf2 support for vector types (like they occur on
16657 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16658 array type. This is not part of the Dwarf2/3 standard yet, but a
16659 custom vendor extension. The main difference between a regular
16660 array and the vector variant is that vectors are passed by value
16661 to functions. */
e142c38c 16662 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16663 if (attr != nullptr)
ea37ba09 16664 make_vector_type (type);
f5f8a009 16665
dbc98a8b
KW
16666 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16667 implementation may choose to implement triple vectors using this
16668 attribute. */
16669 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16670 if (attr != nullptr)
dbc98a8b
KW
16671 {
16672 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16673 TYPE_LENGTH (type) = DW_UNSND (attr);
16674 else
b98664d3 16675 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16676 "than the total size of elements"));
dbc98a8b
KW
16677 }
16678
39cbfefa
DJ
16679 name = dwarf2_name (die, cu);
16680 if (name)
16681 TYPE_NAME (type) = name;
6e70227d 16682
2b4424c3
TT
16683 maybe_set_alignment (cu, die, type);
16684
0963b4bd 16685 /* Install the type in the die. */
7e314c57
JK
16686 set_die_type (die, type, cu);
16687
16688 /* set_die_type should be already done. */
b4ba55a1
JB
16689 set_descriptive_type (type, die, cu);
16690
7e314c57 16691 return type;
c906108c
SS
16692}
16693
7ca2d3a3 16694static enum dwarf_array_dim_ordering
6e70227d 16695read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16696{
16697 struct attribute *attr;
16698
16699 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16700
435d3d88 16701 if (attr != nullptr)
aead7601 16702 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16703
0963b4bd
MS
16704 /* GNU F77 is a special case, as at 08/2004 array type info is the
16705 opposite order to the dwarf2 specification, but data is still
16706 laid out as per normal fortran.
7ca2d3a3 16707
0963b4bd
MS
16708 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16709 version checking. */
7ca2d3a3 16710
905e0470
PM
16711 if (cu->language == language_fortran
16712 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16713 {
16714 return DW_ORD_row_major;
16715 }
16716
6e70227d 16717 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16718 {
16719 case array_column_major:
16720 return DW_ORD_col_major;
16721 case array_row_major:
16722 default:
16723 return DW_ORD_row_major;
16724 };
16725}
16726
72019c9c 16727/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16728 the DIE's type field. */
72019c9c 16729
f792889a 16730static struct type *
72019c9c
GM
16731read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16732{
7e314c57
JK
16733 struct type *domain_type, *set_type;
16734 struct attribute *attr;
f792889a 16735
7e314c57
JK
16736 domain_type = die_type (die, cu);
16737
16738 /* The die_type call above may have already set the type for this DIE. */
16739 set_type = get_die_type (die, cu);
16740 if (set_type)
16741 return set_type;
16742
16743 set_type = create_set_type (NULL, domain_type);
16744
16745 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16746 if (attr != nullptr)
d09039dd 16747 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16748
2b4424c3
TT
16749 maybe_set_alignment (cu, die, set_type);
16750
f792889a 16751 return set_die_type (die, set_type, cu);
72019c9c 16752}
7ca2d3a3 16753
0971de02
TT
16754/* A helper for read_common_block that creates a locexpr baton.
16755 SYM is the symbol which we are marking as computed.
16756 COMMON_DIE is the DIE for the common block.
16757 COMMON_LOC is the location expression attribute for the common
16758 block itself.
16759 MEMBER_LOC is the location expression attribute for the particular
16760 member of the common block that we are processing.
16761 CU is the CU from which the above come. */
16762
16763static void
16764mark_common_block_symbol_computed (struct symbol *sym,
16765 struct die_info *common_die,
16766 struct attribute *common_loc,
16767 struct attribute *member_loc,
16768 struct dwarf2_cu *cu)
16769{
518817b3
SM
16770 struct dwarf2_per_objfile *dwarf2_per_objfile
16771 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16772 struct objfile *objfile = dwarf2_per_objfile->objfile;
16773 struct dwarf2_locexpr_baton *baton;
16774 gdb_byte *ptr;
16775 unsigned int cu_off;
16776 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16777 LONGEST offset = 0;
16778
16779 gdb_assert (common_loc && member_loc);
16780 gdb_assert (attr_form_is_block (common_loc));
16781 gdb_assert (attr_form_is_block (member_loc)
16782 || attr_form_is_constant (member_loc));
16783
8d749320 16784 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16785 baton->per_cu = cu->per_cu;
16786 gdb_assert (baton->per_cu);
16787
16788 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16789
16790 if (attr_form_is_constant (member_loc))
16791 {
16792 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16793 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16794 }
16795 else
16796 baton->size += DW_BLOCK (member_loc)->size;
16797
224c3ddb 16798 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16799 baton->data = ptr;
16800
16801 *ptr++ = DW_OP_call4;
9c541725 16802 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16803 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16804 ptr += 4;
16805
16806 if (attr_form_is_constant (member_loc))
16807 {
16808 *ptr++ = DW_OP_addr;
16809 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16810 ptr += cu->header.addr_size;
16811 }
16812 else
16813 {
16814 /* We have to copy the data here, because DW_OP_call4 will only
16815 use a DW_AT_location attribute. */
16816 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16817 ptr += DW_BLOCK (member_loc)->size;
16818 }
16819
16820 *ptr++ = DW_OP_plus;
16821 gdb_assert (ptr - baton->data == baton->size);
16822
0971de02 16823 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16824 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16825}
16826
4357ac6c
TT
16827/* Create appropriate locally-scoped variables for all the
16828 DW_TAG_common_block entries. Also create a struct common_block
16829 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16830 is used to separate the common blocks name namespace from regular
4357ac6c 16831 variable names. */
c906108c
SS
16832
16833static void
e7c27a73 16834read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16835{
0971de02
TT
16836 struct attribute *attr;
16837
16838 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16839 if (attr != nullptr)
0971de02
TT
16840 {
16841 /* Support the .debug_loc offsets. */
16842 if (attr_form_is_block (attr))
16843 {
16844 /* Ok. */
16845 }
16846 else if (attr_form_is_section_offset (attr))
16847 {
16848 dwarf2_complex_location_expr_complaint ();
16849 attr = NULL;
16850 }
16851 else
16852 {
16853 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16854 "common block member");
16855 attr = NULL;
16856 }
16857 }
16858
639d11d3 16859 if (die->child != NULL)
c906108c 16860 {
518817b3 16861 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16862 struct die_info *child_die;
16863 size_t n_entries = 0, size;
16864 struct common_block *common_block;
16865 struct symbol *sym;
74ac6d43 16866
4357ac6c
TT
16867 for (child_die = die->child;
16868 child_die && child_die->tag;
16869 child_die = sibling_die (child_die))
16870 ++n_entries;
16871
16872 size = (sizeof (struct common_block)
16873 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16874 common_block
16875 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16876 size);
4357ac6c
TT
16877 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16878 common_block->n_entries = 0;
16879
16880 for (child_die = die->child;
16881 child_die && child_die->tag;
16882 child_die = sibling_die (child_die))
16883 {
16884 /* Create the symbol in the DW_TAG_common_block block in the current
16885 symbol scope. */
e7c27a73 16886 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16887 if (sym != NULL)
16888 {
16889 struct attribute *member_loc;
16890
16891 common_block->contents[common_block->n_entries++] = sym;
16892
16893 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16894 cu);
16895 if (member_loc)
16896 {
16897 /* GDB has handled this for a long time, but it is
16898 not specified by DWARF. It seems to have been
16899 emitted by gfortran at least as recently as:
16900 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16901 complaint (_("Variable in common block has "
0971de02 16902 "DW_AT_data_member_location "
9d8780f0
SM
16903 "- DIE at %s [in module %s]"),
16904 sect_offset_str (child_die->sect_off),
518817b3 16905 objfile_name (objfile));
0971de02
TT
16906
16907 if (attr_form_is_section_offset (member_loc))
16908 dwarf2_complex_location_expr_complaint ();
16909 else if (attr_form_is_constant (member_loc)
16910 || attr_form_is_block (member_loc))
16911 {
435d3d88 16912 if (attr != nullptr)
0971de02
TT
16913 mark_common_block_symbol_computed (sym, die, attr,
16914 member_loc, cu);
16915 }
16916 else
16917 dwarf2_complex_location_expr_complaint ();
16918 }
16919 }
c906108c 16920 }
4357ac6c
TT
16921
16922 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16923 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16924 }
16925}
16926
0114d602 16927/* Create a type for a C++ namespace. */
d9fa45fe 16928
0114d602
DJ
16929static struct type *
16930read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16931{
518817b3 16932 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16933 const char *previous_prefix, *name;
9219021c 16934 int is_anonymous;
0114d602
DJ
16935 struct type *type;
16936
16937 /* For extensions, reuse the type of the original namespace. */
16938 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16939 {
16940 struct die_info *ext_die;
16941 struct dwarf2_cu *ext_cu = cu;
9a619af0 16942
0114d602
DJ
16943 ext_die = dwarf2_extension (die, &ext_cu);
16944 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16945
16946 /* EXT_CU may not be the same as CU.
02142a6c 16947 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16948 return set_die_type (die, type, cu);
16949 }
9219021c 16950
e142c38c 16951 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16952
16953 /* Now build the name of the current namespace. */
16954
0114d602
DJ
16955 previous_prefix = determine_prefix (die, cu);
16956 if (previous_prefix[0] != '\0')
16957 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16958 previous_prefix, name, 0, cu);
0114d602
DJ
16959
16960 /* Create the type. */
19f392bc 16961 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16962
60531b24 16963 return set_die_type (die, type, cu);
0114d602
DJ
16964}
16965
22cee43f 16966/* Read a namespace scope. */
0114d602
DJ
16967
16968static void
16969read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16970{
518817b3 16971 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16972 int is_anonymous;
9219021c 16973
5c4e30ca
DC
16974 /* Add a symbol associated to this if we haven't seen the namespace
16975 before. Also, add a using directive if it's an anonymous
16976 namespace. */
9219021c 16977
f2f0e013 16978 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16979 {
16980 struct type *type;
16981
0114d602 16982 type = read_type_die (die, cu);
e7c27a73 16983 new_symbol (die, type, cu);
5c4e30ca 16984
e8e80198 16985 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16986 if (is_anonymous)
0114d602
DJ
16987 {
16988 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16989
eb1e02fd 16990 std::vector<const char *> excludes;
804d2729 16991 add_using_directive (using_directives (cu),
22cee43f 16992 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16993 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16994 }
5c4e30ca 16995 }
9219021c 16996
639d11d3 16997 if (die->child != NULL)
d9fa45fe 16998 {
639d11d3 16999 struct die_info *child_die = die->child;
6e70227d 17000
d9fa45fe
DC
17001 while (child_die && child_die->tag)
17002 {
e7c27a73 17003 process_die (child_die, cu);
d9fa45fe
DC
17004 child_die = sibling_die (child_die);
17005 }
17006 }
38d518c9
EZ
17007}
17008
f55ee35c
JK
17009/* Read a Fortran module as type. This DIE can be only a declaration used for
17010 imported module. Still we need that type as local Fortran "use ... only"
17011 declaration imports depend on the created type in determine_prefix. */
17012
17013static struct type *
17014read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17015{
518817b3 17016 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 17017 const char *module_name;
f55ee35c
JK
17018 struct type *type;
17019
17020 module_name = dwarf2_name (die, cu);
19f392bc 17021 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17022
f55ee35c
JK
17023 return set_die_type (die, type, cu);
17024}
17025
5d7cb8df
JK
17026/* Read a Fortran module. */
17027
17028static void
17029read_module (struct die_info *die, struct dwarf2_cu *cu)
17030{
17031 struct die_info *child_die = die->child;
530e8392
KB
17032 struct type *type;
17033
17034 type = read_type_die (die, cu);
17035 new_symbol (die, type, cu);
5d7cb8df 17036
5d7cb8df
JK
17037 while (child_die && child_die->tag)
17038 {
17039 process_die (child_die, cu);
17040 child_die = sibling_die (child_die);
17041 }
17042}
17043
38d518c9
EZ
17044/* Return the name of the namespace represented by DIE. Set
17045 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17046 namespace. */
17047
17048static const char *
e142c38c 17049namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17050{
17051 struct die_info *current_die;
17052 const char *name = NULL;
17053
17054 /* Loop through the extensions until we find a name. */
17055
17056 for (current_die = die;
17057 current_die != NULL;
f2f0e013 17058 current_die = dwarf2_extension (die, &cu))
38d518c9 17059 {
96553a0c
DE
17060 /* We don't use dwarf2_name here so that we can detect the absence
17061 of a name -> anonymous namespace. */
7d45c7c3 17062 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17063
38d518c9
EZ
17064 if (name != NULL)
17065 break;
17066 }
17067
17068 /* Is it an anonymous namespace? */
17069
17070 *is_anonymous = (name == NULL);
17071 if (*is_anonymous)
2b1dbab0 17072 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17073
17074 return name;
d9fa45fe
DC
17075}
17076
c906108c
SS
17077/* Extract all information from a DW_TAG_pointer_type DIE and add to
17078 the user defined type vector. */
17079
f792889a 17080static struct type *
e7c27a73 17081read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17082{
518817b3
SM
17083 struct gdbarch *gdbarch
17084 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17085 struct comp_unit_head *cu_header = &cu->header;
c906108c 17086 struct type *type;
8b2dbe47
KB
17087 struct attribute *attr_byte_size;
17088 struct attribute *attr_address_class;
17089 int byte_size, addr_class;
7e314c57
JK
17090 struct type *target_type;
17091
17092 target_type = die_type (die, cu);
c906108c 17093
7e314c57
JK
17094 /* The die_type call above may have already set the type for this DIE. */
17095 type = get_die_type (die, cu);
17096 if (type)
17097 return type;
17098
17099 type = lookup_pointer_type (target_type);
8b2dbe47 17100
e142c38c 17101 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17102 if (attr_byte_size)
17103 byte_size = DW_UNSND (attr_byte_size);
c906108c 17104 else
8b2dbe47
KB
17105 byte_size = cu_header->addr_size;
17106
e142c38c 17107 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17108 if (attr_address_class)
17109 addr_class = DW_UNSND (attr_address_class);
17110 else
17111 addr_class = DW_ADDR_none;
17112
2b4424c3
TT
17113 ULONGEST alignment = get_alignment (cu, die);
17114
17115 /* If the pointer size, alignment, or address class is different
17116 than the default, create a type variant marked as such and set
17117 the length accordingly. */
17118 if (TYPE_LENGTH (type) != byte_size
17119 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17120 && alignment != TYPE_RAW_ALIGN (type))
17121 || addr_class != DW_ADDR_none)
c906108c 17122 {
5e2b427d 17123 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17124 {
17125 int type_flags;
17126
849957d9 17127 type_flags = gdbarch_address_class_type_flags
5e2b427d 17128 (gdbarch, byte_size, addr_class);
876cecd0
TT
17129 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17130 == 0);
8b2dbe47
KB
17131 type = make_type_with_address_space (type, type_flags);
17132 }
17133 else if (TYPE_LENGTH (type) != byte_size)
17134 {
b98664d3 17135 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17136 }
2b4424c3
TT
17137 else if (TYPE_RAW_ALIGN (type) != alignment)
17138 {
b98664d3 17139 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17140 " - DIE at %s [in module %s]"),
17141 sect_offset_str (die->sect_off),
17142 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17143 }
6e70227d 17144 else
9a619af0
MS
17145 {
17146 /* Should we also complain about unhandled address classes? */
17147 }
c906108c 17148 }
8b2dbe47
KB
17149
17150 TYPE_LENGTH (type) = byte_size;
2b4424c3 17151 set_type_align (type, alignment);
f792889a 17152 return set_die_type (die, type, cu);
c906108c
SS
17153}
17154
17155/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17156 the user defined type vector. */
17157
f792889a 17158static struct type *
e7c27a73 17159read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17160{
17161 struct type *type;
17162 struct type *to_type;
17163 struct type *domain;
17164
e7c27a73
DJ
17165 to_type = die_type (die, cu);
17166 domain = die_containing_type (die, cu);
0d5de010 17167
7e314c57
JK
17168 /* The calls above may have already set the type for this DIE. */
17169 type = get_die_type (die, cu);
17170 if (type)
17171 return type;
17172
0d5de010
DJ
17173 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17174 type = lookup_methodptr_type (to_type);
7078baeb
TT
17175 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17176 {
518817b3
SM
17177 struct type *new_type
17178 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17179
17180 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17181 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17182 TYPE_VARARGS (to_type));
17183 type = lookup_methodptr_type (new_type);
17184 }
0d5de010
DJ
17185 else
17186 type = lookup_memberptr_type (to_type, domain);
c906108c 17187
f792889a 17188 return set_die_type (die, type, cu);
c906108c
SS
17189}
17190
4297a3f0 17191/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17192 the user defined type vector. */
17193
f792889a 17194static struct type *
4297a3f0
AV
17195read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17196 enum type_code refcode)
c906108c 17197{
e7c27a73 17198 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17199 struct type *type, *target_type;
c906108c
SS
17200 struct attribute *attr;
17201
4297a3f0
AV
17202 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17203
7e314c57
JK
17204 target_type = die_type (die, cu);
17205
17206 /* The die_type call above may have already set the type for this DIE. */
17207 type = get_die_type (die, cu);
17208 if (type)
17209 return type;
17210
4297a3f0 17211 type = lookup_reference_type (target_type, refcode);
e142c38c 17212 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17213 if (attr != nullptr)
c906108c
SS
17214 {
17215 TYPE_LENGTH (type) = DW_UNSND (attr);
17216 }
17217 else
17218 {
107d2387 17219 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17220 }
2b4424c3 17221 maybe_set_alignment (cu, die, type);
f792889a 17222 return set_die_type (die, type, cu);
c906108c
SS
17223}
17224
cf363f18
MW
17225/* Add the given cv-qualifiers to the element type of the array. GCC
17226 outputs DWARF type qualifiers that apply to an array, not the
17227 element type. But GDB relies on the array element type to carry
17228 the cv-qualifiers. This mimics section 6.7.3 of the C99
17229 specification. */
17230
17231static struct type *
17232add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17233 struct type *base_type, int cnst, int voltl)
17234{
17235 struct type *el_type, *inner_array;
17236
17237 base_type = copy_type (base_type);
17238 inner_array = base_type;
17239
17240 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17241 {
17242 TYPE_TARGET_TYPE (inner_array) =
17243 copy_type (TYPE_TARGET_TYPE (inner_array));
17244 inner_array = TYPE_TARGET_TYPE (inner_array);
17245 }
17246
17247 el_type = TYPE_TARGET_TYPE (inner_array);
17248 cnst |= TYPE_CONST (el_type);
17249 voltl |= TYPE_VOLATILE (el_type);
17250 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17251
17252 return set_die_type (die, base_type, cu);
17253}
17254
f792889a 17255static struct type *
e7c27a73 17256read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17257{
f792889a 17258 struct type *base_type, *cv_type;
c906108c 17259
e7c27a73 17260 base_type = die_type (die, cu);
7e314c57
JK
17261
17262 /* The die_type call above may have already set the type for this DIE. */
17263 cv_type = get_die_type (die, cu);
17264 if (cv_type)
17265 return cv_type;
17266
2f608a3a
KW
17267 /* In case the const qualifier is applied to an array type, the element type
17268 is so qualified, not the array type (section 6.7.3 of C99). */
17269 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17270 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17271
f792889a
DJ
17272 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17273 return set_die_type (die, cv_type, cu);
c906108c
SS
17274}
17275
f792889a 17276static struct type *
e7c27a73 17277read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17278{
f792889a 17279 struct type *base_type, *cv_type;
c906108c 17280
e7c27a73 17281 base_type = die_type (die, cu);
7e314c57
JK
17282
17283 /* The die_type call above may have already set the type for this DIE. */
17284 cv_type = get_die_type (die, cu);
17285 if (cv_type)
17286 return cv_type;
17287
cf363f18
MW
17288 /* In case the volatile qualifier is applied to an array type, the
17289 element type is so qualified, not the array type (section 6.7.3
17290 of C99). */
17291 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17292 return add_array_cv_type (die, cu, base_type, 0, 1);
17293
f792889a
DJ
17294 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17295 return set_die_type (die, cv_type, cu);
c906108c
SS
17296}
17297
06d66ee9
TT
17298/* Handle DW_TAG_restrict_type. */
17299
17300static struct type *
17301read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17302{
17303 struct type *base_type, *cv_type;
17304
17305 base_type = die_type (die, cu);
17306
17307 /* The die_type call above may have already set the type for this DIE. */
17308 cv_type = get_die_type (die, cu);
17309 if (cv_type)
17310 return cv_type;
17311
17312 cv_type = make_restrict_type (base_type);
17313 return set_die_type (die, cv_type, cu);
17314}
17315
a2c2acaf
MW
17316/* Handle DW_TAG_atomic_type. */
17317
17318static struct type *
17319read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17320{
17321 struct type *base_type, *cv_type;
17322
17323 base_type = die_type (die, cu);
17324
17325 /* The die_type call above may have already set the type for this DIE. */
17326 cv_type = get_die_type (die, cu);
17327 if (cv_type)
17328 return cv_type;
17329
17330 cv_type = make_atomic_type (base_type);
17331 return set_die_type (die, cv_type, cu);
17332}
17333
c906108c
SS
17334/* Extract all information from a DW_TAG_string_type DIE and add to
17335 the user defined type vector. It isn't really a user defined type,
17336 but it behaves like one, with other DIE's using an AT_user_def_type
17337 attribute to reference it. */
17338
f792889a 17339static struct type *
e7c27a73 17340read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17341{
518817b3 17342 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17343 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17344 struct type *type, *range_type, *index_type, *char_type;
17345 struct attribute *attr;
216a7e6b
AB
17346 struct dynamic_prop prop;
17347 bool length_is_constant = true;
17348 LONGEST length;
17349
17350 /* There are a couple of places where bit sizes might be made use of
17351 when parsing a DW_TAG_string_type, however, no producer that we know
17352 of make use of these. Handling bit sizes that are a multiple of the
17353 byte size is easy enough, but what about other bit sizes? Lets deal
17354 with that problem when we have to. Warn about these attributes being
17355 unsupported, then parse the type and ignore them like we always
17356 have. */
17357 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17358 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17359 {
17360 static bool warning_printed = false;
17361 if (!warning_printed)
17362 {
17363 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17364 "currently supported on DW_TAG_string_type."));
17365 warning_printed = true;
17366 }
17367 }
c906108c 17368
e142c38c 17369 attr = dwarf2_attr (die, DW_AT_string_length, cu);
216a7e6b
AB
17370 if (attr != nullptr && !attr_form_is_constant (attr))
17371 {
17372 /* The string length describes the location at which the length of
17373 the string can be found. The size of the length field can be
17374 specified with one of the attributes below. */
17375 struct type *prop_type;
17376 struct attribute *len
17377 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17378 if (len == nullptr)
17379 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17380 if (len != nullptr && attr_form_is_constant (len))
17381 {
17382 /* Pass 0 as the default as we know this attribute is constant
17383 and the default value will not be returned. */
17384 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17385 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17386 }
17387 else
17388 {
17389 /* If the size is not specified then we assume it is the size of
17390 an address on this target. */
17391 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17392 }
17393
17394 /* Convert the attribute into a dynamic property. */
17395 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17396 length = 1;
17397 else
17398 length_is_constant = false;
17399 }
17400 else if (attr != nullptr)
17401 {
17402 /* This DW_AT_string_length just contains the length with no
17403 indirection. There's no need to create a dynamic property in this
17404 case. Pass 0 for the default value as we know it will not be
17405 returned in this case. */
17406 length = dwarf2_get_attr_constant_value (attr, 0);
17407 }
17408 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17409 {
216a7e6b
AB
17410 /* We don't currently support non-constant byte sizes for strings. */
17411 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17412 }
17413 else
17414 {
216a7e6b
AB
17415 /* Use 1 as a fallback length if we have nothing else. */
17416 length = 1;
c906108c 17417 }
6ccb9162 17418
46bf5051 17419 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17420 if (length_is_constant)
17421 range_type = create_static_range_type (NULL, index_type, 1, length);
17422 else
17423 {
17424 struct dynamic_prop low_bound;
17425
17426 low_bound.kind = PROP_CONST;
17427 low_bound.data.const_val = 1;
17428 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17429 }
3b7538c0
UW
17430 char_type = language_string_char_type (cu->language_defn, gdbarch);
17431 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17432
f792889a 17433 return set_die_type (die, type, cu);
c906108c
SS
17434}
17435
4d804846
JB
17436/* Assuming that DIE corresponds to a function, returns nonzero
17437 if the function is prototyped. */
17438
17439static int
17440prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17441{
17442 struct attribute *attr;
17443
17444 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17445 if (attr && (DW_UNSND (attr) != 0))
17446 return 1;
17447
17448 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17449 is only meaningful for C, but the concept also extends to other
4d804846
JB
17450 languages that allow unprototyped functions (Eg: Objective C).
17451 For all other languages, assume that functions are always
17452 prototyped. */
17453 if (cu->language != language_c
17454 && cu->language != language_objc
17455 && cu->language != language_opencl)
17456 return 1;
17457
17458 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17459 prototyped and unprototyped functions; default to prototyped,
17460 since that is more common in modern code (and RealView warns
17461 about unprototyped functions). */
17462 if (producer_is_realview (cu->producer))
17463 return 1;
17464
17465 return 0;
17466}
17467
c906108c
SS
17468/* Handle DIES due to C code like:
17469
17470 struct foo
c5aa993b
JM
17471 {
17472 int (*funcp)(int a, long l);
17473 int b;
17474 };
c906108c 17475
0963b4bd 17476 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17477
f792889a 17478static struct type *
e7c27a73 17479read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17480{
518817b3 17481 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17482 struct type *type; /* Type that this function returns. */
17483 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17484 struct attribute *attr;
17485
e7c27a73 17486 type = die_type (die, cu);
7e314c57
JK
17487
17488 /* The die_type call above may have already set the type for this DIE. */
17489 ftype = get_die_type (die, cu);
17490 if (ftype)
17491 return ftype;
17492
0c8b41f1 17493 ftype = lookup_function_type (type);
c906108c 17494
4d804846 17495 if (prototyped_function_p (die, cu))
a6c727b2 17496 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17497
c055b101
CV
17498 /* Store the calling convention in the type if it's available in
17499 the subroutine die. Otherwise set the calling convention to
17500 the default value DW_CC_normal. */
17501 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17502 if (attr != nullptr
17503 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17504 TYPE_CALLING_CONVENTION (ftype)
17505 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17506 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17507 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17508 else
17509 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17510
743649fd
MW
17511 /* Record whether the function returns normally to its caller or not
17512 if the DWARF producer set that information. */
17513 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17514 if (attr && (DW_UNSND (attr) != 0))
17515 TYPE_NO_RETURN (ftype) = 1;
17516
76c10ea2
GM
17517 /* We need to add the subroutine type to the die immediately so
17518 we don't infinitely recurse when dealing with parameters
0963b4bd 17519 declared as the same subroutine type. */
76c10ea2 17520 set_die_type (die, ftype, cu);
6e70227d 17521
639d11d3 17522 if (die->child != NULL)
c906108c 17523 {
bb5ed363 17524 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17525 struct die_info *child_die;
8072405b 17526 int nparams, iparams;
c906108c
SS
17527
17528 /* Count the number of parameters.
17529 FIXME: GDB currently ignores vararg functions, but knows about
17530 vararg member functions. */
8072405b 17531 nparams = 0;
639d11d3 17532 child_die = die->child;
c906108c
SS
17533 while (child_die && child_die->tag)
17534 {
17535 if (child_die->tag == DW_TAG_formal_parameter)
17536 nparams++;
17537 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17538 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17539 child_die = sibling_die (child_die);
17540 }
17541
17542 /* Allocate storage for parameters and fill them in. */
17543 TYPE_NFIELDS (ftype) = nparams;
17544 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17545 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17546
8072405b
JK
17547 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17548 even if we error out during the parameters reading below. */
17549 for (iparams = 0; iparams < nparams; iparams++)
17550 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17551
17552 iparams = 0;
639d11d3 17553 child_die = die->child;
c906108c
SS
17554 while (child_die && child_die->tag)
17555 {
17556 if (child_die->tag == DW_TAG_formal_parameter)
17557 {
3ce3b1ba
PA
17558 struct type *arg_type;
17559
17560 /* DWARF version 2 has no clean way to discern C++
17561 static and non-static member functions. G++ helps
17562 GDB by marking the first parameter for non-static
17563 member functions (which is the this pointer) as
17564 artificial. We pass this information to
17565 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17566
17567 DWARF version 3 added DW_AT_object_pointer, which GCC
17568 4.5 does not yet generate. */
e142c38c 17569 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17570 if (attr != nullptr)
c906108c
SS
17571 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17572 else
9c37b5ae 17573 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17574 arg_type = die_type (child_die, cu);
17575
17576 /* RealView does not mark THIS as const, which the testsuite
17577 expects. GCC marks THIS as const in method definitions,
17578 but not in the class specifications (GCC PR 43053). */
17579 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17580 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17581 {
17582 int is_this = 0;
17583 struct dwarf2_cu *arg_cu = cu;
17584 const char *name = dwarf2_name (child_die, cu);
17585
17586 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17587 if (attr != nullptr)
3ce3b1ba
PA
17588 {
17589 /* If the compiler emits this, use it. */
17590 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17591 is_this = 1;
17592 }
17593 else if (name && strcmp (name, "this") == 0)
17594 /* Function definitions will have the argument names. */
17595 is_this = 1;
17596 else if (name == NULL && iparams == 0)
17597 /* Declarations may not have the names, so like
17598 elsewhere in GDB, assume an artificial first
17599 argument is "this". */
17600 is_this = 1;
17601
17602 if (is_this)
17603 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17604 arg_type, 0);
17605 }
17606
17607 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17608 iparams++;
17609 }
17610 child_die = sibling_die (child_die);
17611 }
17612 }
17613
76c10ea2 17614 return ftype;
c906108c
SS
17615}
17616
f792889a 17617static struct type *
e7c27a73 17618read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17619{
518817b3 17620 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17621 const char *name = NULL;
3c8e0968 17622 struct type *this_type, *target_type;
c906108c 17623
94af9270 17624 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17625 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17626 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17627 set_die_type (die, this_type, cu);
3c8e0968
DE
17628 target_type = die_type (die, cu);
17629 if (target_type != this_type)
17630 TYPE_TARGET_TYPE (this_type) = target_type;
17631 else
17632 {
17633 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17634 spec and cause infinite loops in GDB. */
b98664d3 17635 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17636 "- DIE at %s [in module %s]"),
17637 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17638 TYPE_TARGET_TYPE (this_type) = NULL;
17639 }
f792889a 17640 return this_type;
c906108c
SS
17641}
17642
9b790ce7
UW
17643/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17644 (which may be different from NAME) to the architecture back-end to allow
17645 it to guess the correct format if necessary. */
17646
17647static struct type *
17648dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17649 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17650{
17651 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17652 const struct floatformat **format;
17653 struct type *type;
17654
17655 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17656 if (format)
103a685e 17657 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17658 else
77b7c781 17659 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17660
17661 return type;
17662}
17663
eb77c9df
AB
17664/* Allocate an integer type of size BITS and name NAME. */
17665
17666static struct type *
17667dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17668 int bits, int unsigned_p, const char *name)
17669{
17670 struct type *type;
17671
17672 /* Versions of Intel's C Compiler generate an integer type called "void"
17673 instead of using DW_TAG_unspecified_type. This has been seen on
17674 at least versions 14, 17, and 18. */
35ee2dc2
AB
17675 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17676 && strcmp (name, "void") == 0)
eb77c9df
AB
17677 type = objfile_type (objfile)->builtin_void;
17678 else
17679 type = init_integer_type (objfile, bits, unsigned_p, name);
17680
17681 return type;
17682}
17683
8bdc1658
AB
17684/* Initialise and return a floating point type of size BITS suitable for
17685 use as a component of a complex number. The NAME_HINT is passed through
17686 when initialising the floating point type and is the name of the complex
17687 type.
17688
17689 As DWARF doesn't currently provide an explicit name for the components
17690 of a complex number, but it can be helpful to have these components
17691 named, we try to select a suitable name based on the size of the
17692 component. */
17693static struct type *
17694dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17695 struct objfile *objfile,
103a685e
TT
17696 int bits, const char *name_hint,
17697 enum bfd_endian byte_order)
8bdc1658
AB
17698{
17699 gdbarch *gdbarch = get_objfile_arch (objfile);
17700 struct type *tt = nullptr;
17701
35add35e
AB
17702 /* Try to find a suitable floating point builtin type of size BITS.
17703 We're going to use the name of this type as the name for the complex
17704 target type that we are about to create. */
1db455a7 17705 switch (cu->language)
8bdc1658 17706 {
1db455a7
AB
17707 case language_fortran:
17708 switch (bits)
17709 {
17710 case 32:
17711 tt = builtin_f_type (gdbarch)->builtin_real;
17712 break;
17713 case 64:
17714 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17715 break;
17716 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17717 case 128:
17718 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17719 break;
17720 }
8bdc1658 17721 break;
1db455a7
AB
17722 default:
17723 switch (bits)
17724 {
17725 case 32:
17726 tt = builtin_type (gdbarch)->builtin_float;
17727 break;
17728 case 64:
17729 tt = builtin_type (gdbarch)->builtin_double;
17730 break;
17731 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17732 case 128:
17733 tt = builtin_type (gdbarch)->builtin_long_double;
17734 break;
17735 }
8bdc1658
AB
17736 break;
17737 }
17738
35add35e
AB
17739 /* If the type we found doesn't match the size we were looking for, then
17740 pretend we didn't find a type at all, the complex target type we
17741 create will then be nameless. */
a12e5744 17742 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17743 tt = nullptr;
17744
8bdc1658 17745 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17746 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17747}
17748
c906108c
SS
17749/* Find a representation of a given base type and install
17750 it in the TYPE field of the die. */
17751
f792889a 17752static struct type *
e7c27a73 17753read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17754{
518817b3 17755 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17756 struct type *type;
17757 struct attribute *attr;
19f392bc 17758 int encoding = 0, bits = 0;
15d034d0 17759 const char *name;
34877895 17760 gdbarch *arch;
c906108c 17761
e142c38c 17762 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17763 if (attr != nullptr)
34877895 17764 encoding = DW_UNSND (attr);
e142c38c 17765 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17766 if (attr != nullptr)
34877895 17767 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17768 name = dwarf2_name (die, cu);
6ccb9162 17769 if (!name)
34877895 17770 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17771
17772 arch = get_objfile_arch (objfile);
17773 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17774
34877895
PJ
17775 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17776 if (attr)
103a685e
TT
17777 {
17778 int endianity = DW_UNSND (attr);
17779
17780 switch (endianity)
17781 {
17782 case DW_END_big:
17783 byte_order = BFD_ENDIAN_BIG;
17784 break;
17785 case DW_END_little:
17786 byte_order = BFD_ENDIAN_LITTLE;
17787 break;
17788 default:
17789 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17790 break;
17791 }
17792 }
6ccb9162
UW
17793
17794 switch (encoding)
c906108c 17795 {
6ccb9162
UW
17796 case DW_ATE_address:
17797 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17798 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17799 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17800 break;
17801 case DW_ATE_boolean:
19f392bc 17802 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17803 break;
17804 case DW_ATE_complex_float:
103a685e
TT
17805 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17806 byte_order);
19f392bc 17807 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17808 break;
17809 case DW_ATE_decimal_float:
19f392bc 17810 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17811 break;
17812 case DW_ATE_float:
103a685e 17813 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17814 break;
17815 case DW_ATE_signed:
eb77c9df 17816 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17817 break;
17818 case DW_ATE_unsigned:
3b2b8fea
TT
17819 if (cu->language == language_fortran
17820 && name
61012eef 17821 && startswith (name, "character("))
19f392bc
UW
17822 type = init_character_type (objfile, bits, 1, name);
17823 else
eb77c9df 17824 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17825 break;
17826 case DW_ATE_signed_char:
6e70227d 17827 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17828 || cu->language == language_pascal
17829 || cu->language == language_fortran)
19f392bc
UW
17830 type = init_character_type (objfile, bits, 0, name);
17831 else
eb77c9df 17832 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17833 break;
17834 case DW_ATE_unsigned_char:
868a0084 17835 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17836 || cu->language == language_pascal
c44af4eb
TT
17837 || cu->language == language_fortran
17838 || cu->language == language_rust)
19f392bc
UW
17839 type = init_character_type (objfile, bits, 1, name);
17840 else
eb77c9df 17841 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17842 break;
75079b2b 17843 case DW_ATE_UTF:
53e710ac 17844 {
53e710ac
PA
17845 if (bits == 16)
17846 type = builtin_type (arch)->builtin_char16;
17847 else if (bits == 32)
17848 type = builtin_type (arch)->builtin_char32;
17849 else
17850 {
b98664d3 17851 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17852 bits);
eb77c9df 17853 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17854 }
17855 return set_die_type (die, type, cu);
17856 }
75079b2b
TT
17857 break;
17858
6ccb9162 17859 default:
b98664d3 17860 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17861 dwarf_type_encoding_name (encoding));
77b7c781 17862 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17863 break;
c906108c 17864 }
6ccb9162 17865
0114d602 17866 if (name && strcmp (name, "char") == 0)
876cecd0 17867 TYPE_NOSIGN (type) = 1;
0114d602 17868
2b4424c3
TT
17869 maybe_set_alignment (cu, die, type);
17870
103a685e 17871 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17872
f792889a 17873 return set_die_type (die, type, cu);
c906108c
SS
17874}
17875
80180f79
SA
17876/* Parse dwarf attribute if it's a block, reference or constant and put the
17877 resulting value of the attribute into struct bound_prop.
17878 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17879
17880static int
17881attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17882 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17883 struct type *default_type)
80180f79
SA
17884{
17885 struct dwarf2_property_baton *baton;
518817b3
SM
17886 struct obstack *obstack
17887 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17888
9a49df9d
AB
17889 gdb_assert (default_type != NULL);
17890
80180f79
SA
17891 if (attr == NULL || prop == NULL)
17892 return 0;
17893
17894 if (attr_form_is_block (attr))
17895 {
8d749320 17896 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17897 baton->property_type = default_type;
80180f79
SA
17898 baton->locexpr.per_cu = cu->per_cu;
17899 baton->locexpr.size = DW_BLOCK (attr)->size;
17900 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17901 switch (attr->name)
17902 {
17903 case DW_AT_string_length:
17904 baton->locexpr.is_reference = true;
17905 break;
17906 default:
17907 baton->locexpr.is_reference = false;
17908 break;
17909 }
80180f79
SA
17910 prop->data.baton = baton;
17911 prop->kind = PROP_LOCEXPR;
17912 gdb_assert (prop->data.baton != NULL);
17913 }
17914 else if (attr_form_is_ref (attr))
17915 {
17916 struct dwarf2_cu *target_cu = cu;
17917 struct die_info *target_die;
17918 struct attribute *target_attr;
17919
17920 target_die = follow_die_ref (die, attr, &target_cu);
17921 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17922 if (target_attr == NULL)
17923 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17924 target_cu);
80180f79
SA
17925 if (target_attr == NULL)
17926 return 0;
17927
df25ebbd 17928 switch (target_attr->name)
80180f79 17929 {
df25ebbd
JB
17930 case DW_AT_location:
17931 if (attr_form_is_section_offset (target_attr))
17932 {
8d749320 17933 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17934 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17935 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17936 prop->data.baton = baton;
17937 prop->kind = PROP_LOCLIST;
17938 gdb_assert (prop->data.baton != NULL);
17939 }
17940 else if (attr_form_is_block (target_attr))
17941 {
8d749320 17942 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17943 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17944 baton->locexpr.per_cu = cu->per_cu;
17945 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17946 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17947 baton->locexpr.is_reference = true;
df25ebbd
JB
17948 prop->data.baton = baton;
17949 prop->kind = PROP_LOCEXPR;
17950 gdb_assert (prop->data.baton != NULL);
17951 }
17952 else
17953 {
17954 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17955 "dynamic property");
17956 return 0;
17957 }
17958 break;
17959 case DW_AT_data_member_location:
17960 {
17961 LONGEST offset;
17962
17963 if (!handle_data_member_location (target_die, target_cu,
17964 &offset))
17965 return 0;
17966
8d749320 17967 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17968 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17969 target_cu);
df25ebbd
JB
17970 baton->offset_info.offset = offset;
17971 baton->offset_info.type = die_type (target_die, target_cu);
17972 prop->data.baton = baton;
17973 prop->kind = PROP_ADDR_OFFSET;
17974 break;
17975 }
80180f79
SA
17976 }
17977 }
17978 else if (attr_form_is_constant (attr))
17979 {
17980 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17981 prop->kind = PROP_CONST;
17982 }
17983 else
17984 {
17985 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17986 dwarf2_name (die, cu));
17987 return 0;
17988 }
17989
17990 return 1;
17991}
17992
11a8b164
AB
17993/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17994 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
17995
17996static struct type *
11a8b164
AB
17997dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17998 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
17999{
18000 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
18001 struct type *int_type;
18002
18003 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18004#define TRY_TYPE(F) \
18005 int_type = (unsigned_p \
18006 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18007 : objfile_type (objfile)->builtin_ ## F); \
18008 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18009 return int_type
18010
18011 TRY_TYPE (char);
18012 TRY_TYPE (short);
18013 TRY_TYPE (int);
18014 TRY_TYPE (long);
18015 TRY_TYPE (long_long);
18016
18017#undef TRY_TYPE
18018
18019 gdb_assert_not_reached ("unable to find suitable integer type");
18020}
18021
11a8b164
AB
18022/* Find an integer type the same size as the address size given in the
18023 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18024 is unsigned or not. */
18025
18026static struct type *
18027dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18028 bool unsigned_p)
18029{
18030 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18031 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18032}
18033
b86352cf
AB
18034/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18035 present (which is valid) then compute the default type based on the
18036 compilation units address size. */
18037
18038static struct type *
18039read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18040{
18041 struct type *index_type = die_type (die, cu);
18042
18043 /* Dwarf-2 specifications explicitly allows to create subrange types
18044 without specifying a base type.
18045 In that case, the base type must be set to the type of
18046 the lower bound, upper bound or count, in that order, if any of these
18047 three attributes references an object that has a type.
18048 If no base type is found, the Dwarf-2 specifications say that
18049 a signed integer type of size equal to the size of an address should
18050 be used.
18051 For the following C code: `extern char gdb_int [];'
18052 GCC produces an empty range DIE.
18053 FIXME: muller/2010-05-28: Possible references to object for low bound,
18054 high bound or count are not yet handled by this code. */
18055 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 18056 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
18057
18058 return index_type;
18059}
18060
a02abb62
JB
18061/* Read the given DW_AT_subrange DIE. */
18062
f792889a 18063static struct type *
a02abb62
JB
18064read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18065{
4c9ad8c2 18066 struct type *base_type, *orig_base_type;
a02abb62
JB
18067 struct type *range_type;
18068 struct attribute *attr;
729efb13 18069 struct dynamic_prop low, high;
4fae6e18 18070 int low_default_is_valid;
c451ebe5 18071 int high_bound_is_count = 0;
15d034d0 18072 const char *name;
d359392f 18073 ULONGEST negative_mask;
e77813c8 18074
b86352cf
AB
18075 orig_base_type = read_subrange_index_type (die, cu);
18076
4c9ad8c2
TT
18077 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18078 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18079 creating the range type, but we use the result of check_typedef
18080 when examining properties of the type. */
18081 base_type = check_typedef (orig_base_type);
a02abb62 18082
7e314c57
JK
18083 /* The die_type call above may have already set the type for this DIE. */
18084 range_type = get_die_type (die, cu);
18085 if (range_type)
18086 return range_type;
18087
729efb13
SA
18088 low.kind = PROP_CONST;
18089 high.kind = PROP_CONST;
18090 high.data.const_val = 0;
18091
4fae6e18
JK
18092 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18093 omitting DW_AT_lower_bound. */
18094 switch (cu->language)
6e70227d 18095 {
4fae6e18
JK
18096 case language_c:
18097 case language_cplus:
729efb13 18098 low.data.const_val = 0;
4fae6e18
JK
18099 low_default_is_valid = 1;
18100 break;
18101 case language_fortran:
729efb13 18102 low.data.const_val = 1;
4fae6e18
JK
18103 low_default_is_valid = 1;
18104 break;
18105 case language_d:
4fae6e18 18106 case language_objc:
c44af4eb 18107 case language_rust:
729efb13 18108 low.data.const_val = 0;
4fae6e18
JK
18109 low_default_is_valid = (cu->header.version >= 4);
18110 break;
18111 case language_ada:
18112 case language_m2:
18113 case language_pascal:
729efb13 18114 low.data.const_val = 1;
4fae6e18
JK
18115 low_default_is_valid = (cu->header.version >= 4);
18116 break;
18117 default:
729efb13 18118 low.data.const_val = 0;
4fae6e18
JK
18119 low_default_is_valid = 0;
18120 break;
a02abb62
JB
18121 }
18122
e142c38c 18123 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18124 if (attr != nullptr)
9a49df9d 18125 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18126 else if (!low_default_is_valid)
b98664d3 18127 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18128 "- DIE at %s [in module %s]"),
18129 sect_offset_str (die->sect_off),
518817b3 18130 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18131
506f5c41
TV
18132 struct attribute *attr_ub, *attr_count;
18133 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18134 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18135 {
506f5c41 18136 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18137 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18138 {
c451ebe5
SA
18139 /* If bounds are constant do the final calculation here. */
18140 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18141 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18142 else
18143 high_bound_is_count = 1;
c2ff108b 18144 }
506f5c41
TV
18145 else
18146 {
18147 if (attr_ub != NULL)
18148 complaint (_("Unresolved DW_AT_upper_bound "
18149 "- DIE at %s [in module %s]"),
18150 sect_offset_str (die->sect_off),
18151 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18152 if (attr_count != NULL)
18153 complaint (_("Unresolved DW_AT_count "
18154 "- DIE at %s [in module %s]"),
18155 sect_offset_str (die->sect_off),
18156 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18157 }
e77813c8 18158 }
a02abb62 18159
4e962e74
TT
18160 LONGEST bias = 0;
18161 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18162 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18163 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18164
dbb9c2b1
JB
18165 /* Normally, the DWARF producers are expected to use a signed
18166 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18167 But this is unfortunately not always the case, as witnessed
18168 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18169 is used instead. To work around that ambiguity, we treat
18170 the bounds as signed, and thus sign-extend their values, when
18171 the base type is signed. */
6e70227d 18172 negative_mask =
d359392f 18173 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18174 if (low.kind == PROP_CONST
18175 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18176 low.data.const_val |= negative_mask;
18177 if (high.kind == PROP_CONST
18178 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18179 high.data.const_val |= negative_mask;
43bbcdc2 18180
5bbd8269
AB
18181 /* Check for bit and byte strides. */
18182 struct dynamic_prop byte_stride_prop;
18183 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18184 if (attr_byte_stride != nullptr)
18185 {
18186 struct type *prop_type
18187 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18188 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18189 prop_type);
18190 }
18191
18192 struct dynamic_prop bit_stride_prop;
18193 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18194 if (attr_bit_stride != nullptr)
18195 {
18196 /* It only makes sense to have either a bit or byte stride. */
18197 if (attr_byte_stride != nullptr)
18198 {
18199 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18200 "- DIE at %s [in module %s]"),
18201 sect_offset_str (die->sect_off),
18202 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18203 attr_bit_stride = nullptr;
18204 }
18205 else
18206 {
18207 struct type *prop_type
18208 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18209 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18210 prop_type);
18211 }
18212 }
18213
18214 if (attr_byte_stride != nullptr
18215 || attr_bit_stride != nullptr)
18216 {
18217 bool byte_stride_p = (attr_byte_stride != nullptr);
18218 struct dynamic_prop *stride
18219 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18220
18221 range_type
18222 = create_range_type_with_stride (NULL, orig_base_type, &low,
18223 &high, bias, stride, byte_stride_p);
18224 }
18225 else
18226 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18227
c451ebe5
SA
18228 if (high_bound_is_count)
18229 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18230
c2ff108b
JK
18231 /* Ada expects an empty array on no boundary attributes. */
18232 if (attr == NULL && cu->language != language_ada)
729efb13 18233 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18234
39cbfefa
DJ
18235 name = dwarf2_name (die, cu);
18236 if (name)
18237 TYPE_NAME (range_type) = name;
6e70227d 18238
e142c38c 18239 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18240 if (attr != nullptr)
a02abb62
JB
18241 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18242
2b4424c3
TT
18243 maybe_set_alignment (cu, die, range_type);
18244
7e314c57
JK
18245 set_die_type (die, range_type, cu);
18246
18247 /* set_die_type should be already done. */
b4ba55a1
JB
18248 set_descriptive_type (range_type, die, cu);
18249
7e314c57 18250 return range_type;
a02abb62 18251}
6e70227d 18252
f792889a 18253static struct type *
81a17f79
JB
18254read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18255{
18256 struct type *type;
81a17f79 18257
518817b3
SM
18258 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18259 NULL);
0114d602 18260 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18261
74a2f8ff 18262 /* In Ada, an unspecified type is typically used when the description
85102364 18263 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18264 such a type, we treat it as a stub, and try to resolve it later on,
18265 when needed. */
18266 if (cu->language == language_ada)
18267 TYPE_STUB (type) = 1;
18268
f792889a 18269 return set_die_type (die, type, cu);
81a17f79 18270}
a02abb62 18271
639d11d3
DC
18272/* Read a single die and all its descendents. Set the die's sibling
18273 field to NULL; set other fields in the die correctly, and set all
18274 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18275 location of the info_ptr after reading all of those dies. PARENT
18276 is the parent of the die in question. */
18277
18278static struct die_info *
dee91e82 18279read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18280 const gdb_byte *info_ptr,
18281 const gdb_byte **new_info_ptr,
dee91e82 18282 struct die_info *parent)
639d11d3
DC
18283{
18284 struct die_info *die;
d521ce57 18285 const gdb_byte *cur_ptr;
639d11d3
DC
18286 int has_children;
18287
bf6af496 18288 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18289 if (die == NULL)
18290 {
18291 *new_info_ptr = cur_ptr;
18292 return NULL;
18293 }
93311388 18294 store_in_ref_table (die, reader->cu);
639d11d3
DC
18295
18296 if (has_children)
bf6af496 18297 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18298 else
18299 {
18300 die->child = NULL;
18301 *new_info_ptr = cur_ptr;
18302 }
18303
18304 die->sibling = NULL;
18305 die->parent = parent;
18306 return die;
18307}
18308
18309/* Read a die, all of its descendents, and all of its siblings; set
18310 all of the fields of all of the dies correctly. Arguments are as
18311 in read_die_and_children. */
18312
18313static struct die_info *
bf6af496 18314read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18315 const gdb_byte *info_ptr,
18316 const gdb_byte **new_info_ptr,
bf6af496 18317 struct die_info *parent)
639d11d3
DC
18318{
18319 struct die_info *first_die, *last_sibling;
d521ce57 18320 const gdb_byte *cur_ptr;
639d11d3 18321
c906108c 18322 cur_ptr = info_ptr;
639d11d3
DC
18323 first_die = last_sibling = NULL;
18324
18325 while (1)
c906108c 18326 {
639d11d3 18327 struct die_info *die
dee91e82 18328 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18329
1d325ec1 18330 if (die == NULL)
c906108c 18331 {
639d11d3
DC
18332 *new_info_ptr = cur_ptr;
18333 return first_die;
c906108c 18334 }
1d325ec1
DJ
18335
18336 if (!first_die)
18337 first_die = die;
c906108c 18338 else
1d325ec1
DJ
18339 last_sibling->sibling = die;
18340
18341 last_sibling = die;
c906108c 18342 }
c906108c
SS
18343}
18344
bf6af496
DE
18345/* Read a die, all of its descendents, and all of its siblings; set
18346 all of the fields of all of the dies correctly. Arguments are as
18347 in read_die_and_children.
18348 This the main entry point for reading a DIE and all its children. */
18349
18350static struct die_info *
18351read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18352 const gdb_byte *info_ptr,
18353 const gdb_byte **new_info_ptr,
bf6af496
DE
18354 struct die_info *parent)
18355{
18356 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18357 new_info_ptr, parent);
18358
b4f54984 18359 if (dwarf_die_debug)
bf6af496
DE
18360 {
18361 fprintf_unfiltered (gdb_stdlog,
18362 "Read die from %s@0x%x of %s:\n",
a32a8923 18363 get_section_name (reader->die_section),
bf6af496
DE
18364 (unsigned) (info_ptr - reader->die_section->buffer),
18365 bfd_get_filename (reader->abfd));
b4f54984 18366 dump_die (die, dwarf_die_debug);
bf6af496
DE
18367 }
18368
18369 return die;
18370}
18371
3019eac3
DE
18372/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18373 attributes.
18374 The caller is responsible for filling in the extra attributes
18375 and updating (*DIEP)->num_attrs.
18376 Set DIEP to point to a newly allocated die with its information,
18377 except for its child, sibling, and parent fields.
18378 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18379
d521ce57 18380static const gdb_byte *
3019eac3 18381read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18382 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18383 int *has_children, int num_extra_attrs)
93311388 18384{
b64f50a1 18385 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18386 struct abbrev_info *abbrev;
18387 struct die_info *die;
18388 struct dwarf2_cu *cu = reader->cu;
18389 bfd *abfd = reader->abfd;
18390
9c541725 18391 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18392 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18393 info_ptr += bytes_read;
18394 if (!abbrev_number)
18395 {
18396 *diep = NULL;
18397 *has_children = 0;
18398 return info_ptr;
18399 }
18400
685af9cd 18401 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18402 if (!abbrev)
348e048f
DE
18403 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18404 abbrev_number,
18405 bfd_get_filename (abfd));
18406
3019eac3 18407 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18408 die->sect_off = sect_off;
93311388
DE
18409 die->tag = abbrev->tag;
18410 die->abbrev = abbrev_number;
18411
3019eac3
DE
18412 /* Make the result usable.
18413 The caller needs to update num_attrs after adding the extra
18414 attributes. */
93311388
DE
18415 die->num_attrs = abbrev->num_attrs;
18416
18a8505e 18417 std::vector<int> indexes_that_need_reprocess;
93311388 18418 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18419 {
18420 bool need_reprocess;
18421 info_ptr =
18422 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18423 info_ptr, &need_reprocess);
18424 if (need_reprocess)
18425 indexes_that_need_reprocess.push_back (i);
18426 }
18427
18428 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18429 if (attr != nullptr)
18430 cu->str_offsets_base = DW_UNSND (attr);
93311388 18431
18a8505e
AT
18432 auto maybe_addr_base = lookup_addr_base(die);
18433 if (maybe_addr_base.has_value ())
18434 cu->addr_base = *maybe_addr_base;
18435 for (int index : indexes_that_need_reprocess)
18436 read_attribute_reprocess (reader, &die->attrs[index]);
93311388
DE
18437 *diep = die;
18438 *has_children = abbrev->has_children;
18439 return info_ptr;
18440}
18441
3019eac3
DE
18442/* Read a die and all its attributes.
18443 Set DIEP to point to a newly allocated die with its information,
18444 except for its child, sibling, and parent fields.
18445 Set HAS_CHILDREN to tell whether the die has children or not. */
18446
d521ce57 18447static const gdb_byte *
3019eac3 18448read_full_die (const struct die_reader_specs *reader,
d521ce57 18449 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18450 int *has_children)
18451{
d521ce57 18452 const gdb_byte *result;
bf6af496
DE
18453
18454 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18455
b4f54984 18456 if (dwarf_die_debug)
bf6af496
DE
18457 {
18458 fprintf_unfiltered (gdb_stdlog,
18459 "Read die from %s@0x%x of %s:\n",
a32a8923 18460 get_section_name (reader->die_section),
bf6af496
DE
18461 (unsigned) (info_ptr - reader->die_section->buffer),
18462 bfd_get_filename (reader->abfd));
b4f54984 18463 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18464 }
18465
18466 return result;
3019eac3 18467}
433df2d4
DE
18468\f
18469/* Abbreviation tables.
3019eac3 18470
433df2d4 18471 In DWARF version 2, the description of the debugging information is
c906108c
SS
18472 stored in a separate .debug_abbrev section. Before we read any
18473 dies from a section we read in all abbreviations and install them
433df2d4
DE
18474 in a hash table. */
18475
18476/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18477
685af9cd
TT
18478struct abbrev_info *
18479abbrev_table::alloc_abbrev ()
433df2d4
DE
18480{
18481 struct abbrev_info *abbrev;
18482
685af9cd 18483 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18484 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18485
433df2d4
DE
18486 return abbrev;
18487}
18488
18489/* Add an abbreviation to the table. */
c906108c 18490
685af9cd
TT
18491void
18492abbrev_table::add_abbrev (unsigned int abbrev_number,
18493 struct abbrev_info *abbrev)
433df2d4
DE
18494{
18495 unsigned int hash_number;
18496
18497 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18498 abbrev->next = m_abbrevs[hash_number];
18499 m_abbrevs[hash_number] = abbrev;
433df2d4 18500}
dee91e82 18501
433df2d4
DE
18502/* Look up an abbrev in the table.
18503 Returns NULL if the abbrev is not found. */
18504
685af9cd
TT
18505struct abbrev_info *
18506abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18507{
433df2d4
DE
18508 unsigned int hash_number;
18509 struct abbrev_info *abbrev;
18510
18511 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18512 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18513
18514 while (abbrev)
18515 {
18516 if (abbrev->number == abbrev_number)
18517 return abbrev;
18518 abbrev = abbrev->next;
18519 }
18520 return NULL;
18521}
18522
18523/* Read in an abbrev table. */
18524
685af9cd 18525static abbrev_table_up
ed2dc618
SM
18526abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18527 struct dwarf2_section_info *section,
9c541725 18528 sect_offset sect_off)
433df2d4
DE
18529{
18530 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18531 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18532 const gdb_byte *abbrev_ptr;
c906108c
SS
18533 struct abbrev_info *cur_abbrev;
18534 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18535 unsigned int abbrev_form;
6dfa2fc2 18536 std::vector<struct attr_abbrev> cur_attrs;
c906108c 18537
685af9cd 18538 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18539
433df2d4 18540 dwarf2_read_section (objfile, section);
9c541725 18541 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18542 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18543 abbrev_ptr += bytes_read;
18544
0963b4bd 18545 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18546 while (abbrev_number)
18547 {
6dfa2fc2 18548 cur_attrs.clear ();
685af9cd 18549 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18550
18551 /* read in abbrev header */
18552 cur_abbrev->number = abbrev_number;
aead7601
SM
18553 cur_abbrev->tag
18554 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18555 abbrev_ptr += bytes_read;
18556 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18557 abbrev_ptr += 1;
18558
18559 /* now read in declarations */
22d2f3ab 18560 for (;;)
c906108c 18561 {
43988095
JK
18562 LONGEST implicit_const;
18563
22d2f3ab
JK
18564 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18565 abbrev_ptr += bytes_read;
18566 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18567 abbrev_ptr += bytes_read;
43988095
JK
18568 if (abbrev_form == DW_FORM_implicit_const)
18569 {
18570 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18571 &bytes_read);
18572 abbrev_ptr += bytes_read;
18573 }
18574 else
18575 {
18576 /* Initialize it due to a false compiler warning. */
18577 implicit_const = -1;
18578 }
22d2f3ab
JK
18579
18580 if (abbrev_name == 0)
18581 break;
18582
6dfa2fc2
TT
18583 cur_attrs.emplace_back ();
18584 struct attr_abbrev &cur_attr = cur_attrs.back ();
18585 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18586 cur_attr.form = (enum dwarf_form) abbrev_form;
18587 cur_attr.implicit_const = implicit_const;
22d2f3ab 18588 ++cur_abbrev->num_attrs;
c906108c
SS
18589 }
18590
8d749320
SM
18591 cur_abbrev->attrs =
18592 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18593 cur_abbrev->num_attrs);
6dfa2fc2 18594 memcpy (cur_abbrev->attrs, cur_attrs.data (),
f3dd6933
DJ
18595 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18596
685af9cd 18597 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18598
18599 /* Get next abbreviation.
18600 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18601 always properly terminated with an abbrev number of 0.
18602 Exit loop if we encounter an abbreviation which we have
18603 already read (which means we are about to read the abbreviations
18604 for the next compile unit) or if the end of the abbreviation
18605 table is reached. */
433df2d4 18606 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18607 break;
18608 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18609 abbrev_ptr += bytes_read;
685af9cd 18610 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18611 break;
18612 }
f3dd6933 18613
433df2d4 18614 return abbrev_table;
c906108c
SS
18615}
18616
72bf9492
DJ
18617/* Returns nonzero if TAG represents a type that we might generate a partial
18618 symbol for. */
18619
18620static int
18621is_type_tag_for_partial (int tag)
18622{
18623 switch (tag)
18624 {
18625#if 0
18626 /* Some types that would be reasonable to generate partial symbols for,
18627 that we don't at present. */
18628 case DW_TAG_array_type:
18629 case DW_TAG_file_type:
18630 case DW_TAG_ptr_to_member_type:
18631 case DW_TAG_set_type:
18632 case DW_TAG_string_type:
18633 case DW_TAG_subroutine_type:
18634#endif
18635 case DW_TAG_base_type:
18636 case DW_TAG_class_type:
680b30c7 18637 case DW_TAG_interface_type:
72bf9492
DJ
18638 case DW_TAG_enumeration_type:
18639 case DW_TAG_structure_type:
18640 case DW_TAG_subrange_type:
18641 case DW_TAG_typedef:
18642 case DW_TAG_union_type:
18643 return 1;
18644 default:
18645 return 0;
18646 }
18647}
18648
18649/* Load all DIEs that are interesting for partial symbols into memory. */
18650
18651static struct partial_die_info *
dee91e82 18652load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18653 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18654{
dee91e82 18655 struct dwarf2_cu *cu = reader->cu;
518817b3 18656 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18657 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18658 unsigned int bytes_read;
5afb4e99 18659 unsigned int load_all = 0;
72bf9492
DJ
18660 int nesting_level = 1;
18661
18662 parent_die = NULL;
18663 last_die = NULL;
18664
7adf1e79
DE
18665 gdb_assert (cu->per_cu != NULL);
18666 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18667 load_all = 1;
18668
72bf9492
DJ
18669 cu->partial_dies
18670 = htab_create_alloc_ex (cu->header.length / 12,
18671 partial_die_hash,
18672 partial_die_eq,
18673 NULL,
18674 &cu->comp_unit_obstack,
18675 hashtab_obstack_allocate,
18676 dummy_obstack_deallocate);
18677
72bf9492
DJ
18678 while (1)
18679 {
685af9cd 18680 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18681
18682 /* A NULL abbrev means the end of a series of children. */
18683 if (abbrev == NULL)
18684 {
18685 if (--nesting_level == 0)
cd9983dd
YQ
18686 return first_die;
18687
72bf9492
DJ
18688 info_ptr += bytes_read;
18689 last_die = parent_die;
18690 parent_die = parent_die->die_parent;
18691 continue;
18692 }
18693
98bfdba5
PA
18694 /* Check for template arguments. We never save these; if
18695 they're seen, we just mark the parent, and go on our way. */
18696 if (parent_die != NULL
18697 && cu->language == language_cplus
18698 && (abbrev->tag == DW_TAG_template_type_param
18699 || abbrev->tag == DW_TAG_template_value_param))
18700 {
18701 parent_die->has_template_arguments = 1;
18702
18703 if (!load_all)
18704 {
18705 /* We don't need a partial DIE for the template argument. */
dee91e82 18706 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18707 continue;
18708 }
18709 }
18710
0d99eb77 18711 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18712 Skip their other children. */
18713 if (!load_all
18714 && cu->language == language_cplus
18715 && parent_die != NULL
18716 && parent_die->tag == DW_TAG_subprogram)
18717 {
dee91e82 18718 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18719 continue;
18720 }
18721
5afb4e99
DJ
18722 /* Check whether this DIE is interesting enough to save. Normally
18723 we would not be interested in members here, but there may be
18724 later variables referencing them via DW_AT_specification (for
18725 static members). */
18726 if (!load_all
18727 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18728 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18729 && abbrev->tag != DW_TAG_enumerator
18730 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18731 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18732 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18733 && abbrev->tag != DW_TAG_variable
5afb4e99 18734 && abbrev->tag != DW_TAG_namespace
f55ee35c 18735 && abbrev->tag != DW_TAG_module
95554aad 18736 && abbrev->tag != DW_TAG_member
74921315
KS
18737 && abbrev->tag != DW_TAG_imported_unit
18738 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18739 {
18740 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18741 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18742 continue;
18743 }
18744
6f06d47b
YQ
18745 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18746 abbrev);
cd9983dd 18747
48fbe735 18748 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18749
18750 /* This two-pass algorithm for processing partial symbols has a
18751 high cost in cache pressure. Thus, handle some simple cases
18752 here which cover the majority of C partial symbols. DIEs
18753 which neither have specification tags in them, nor could have
18754 specification tags elsewhere pointing at them, can simply be
18755 processed and discarded.
18756
18757 This segment is also optional; scan_partial_symbols and
18758 add_partial_symbol will handle these DIEs if we chain
18759 them in normally. When compilers which do not emit large
18760 quantities of duplicate debug information are more common,
18761 this code can probably be removed. */
18762
18763 /* Any complete simple types at the top level (pretty much all
18764 of them, for a language without namespaces), can be processed
18765 directly. */
18766 if (parent_die == NULL
cd9983dd
YQ
18767 && pdi.has_specification == 0
18768 && pdi.is_declaration == 0
18769 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18770 || pdi.tag == DW_TAG_base_type
18771 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18772 {
cd9983dd 18773 if (building_psymtab && pdi.name != NULL)
31edb802 18774 add_psymbol_to_list (pdi.name, false,
79748972 18775 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18776 psymbol_placement::STATIC,
1762568f 18777 0, cu->language, objfile);
cd9983dd 18778 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18779 continue;
18780 }
18781
d8228535
JK
18782 /* The exception for DW_TAG_typedef with has_children above is
18783 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18784 type_name_or_error will error on such types later.
d8228535
JK
18785
18786 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18787 it could not find the child DIEs referenced later, this is checked
18788 above. In correct DWARF DW_TAG_typedef should have no children. */
18789
cd9983dd 18790 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18791 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18792 "- DIE at %s [in module %s]"),
cd9983dd 18793 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18794
72bf9492
DJ
18795 /* If we're at the second level, and we're an enumerator, and
18796 our parent has no specification (meaning possibly lives in a
18797 namespace elsewhere), then we can add the partial symbol now
18798 instead of queueing it. */
cd9983dd 18799 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18800 && parent_die != NULL
18801 && parent_die->die_parent == NULL
18802 && parent_die->tag == DW_TAG_enumeration_type
18803 && parent_die->has_specification == 0)
18804 {
cd9983dd 18805 if (pdi.name == NULL)
b98664d3 18806 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18807 else if (building_psymtab)
31edb802 18808 add_psymbol_to_list (pdi.name, false,
79748972 18809 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18810 cu->language == language_cplus
75aedd27
TT
18811 ? psymbol_placement::GLOBAL
18812 : psymbol_placement::STATIC,
1762568f 18813 0, cu->language, objfile);
72bf9492 18814
cd9983dd 18815 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18816 continue;
18817 }
18818
cd9983dd 18819 struct partial_die_info *part_die
6f06d47b 18820 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18821
72bf9492
DJ
18822 /* We'll save this DIE so link it in. */
18823 part_die->die_parent = parent_die;
18824 part_die->die_sibling = NULL;
18825 part_die->die_child = NULL;
18826
18827 if (last_die && last_die == parent_die)
18828 last_die->die_child = part_die;
18829 else if (last_die)
18830 last_die->die_sibling = part_die;
18831
18832 last_die = part_die;
18833
18834 if (first_die == NULL)
18835 first_die = part_die;
18836
18837 /* Maybe add the DIE to the hash table. Not all DIEs that we
18838 find interesting need to be in the hash table, because we
18839 also have the parent/sibling/child chains; only those that we
18840 might refer to by offset later during partial symbol reading.
18841
18842 For now this means things that might have be the target of a
18843 DW_AT_specification, DW_AT_abstract_origin, or
18844 DW_AT_extension. DW_AT_extension will refer only to
18845 namespaces; DW_AT_abstract_origin refers to functions (and
18846 many things under the function DIE, but we do not recurse
18847 into function DIEs during partial symbol reading) and
18848 possibly variables as well; DW_AT_specification refers to
18849 declarations. Declarations ought to have the DW_AT_declaration
18850 flag. It happens that GCC forgets to put it in sometimes, but
18851 only for functions, not for types.
18852
18853 Adding more things than necessary to the hash table is harmless
18854 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18855 wasted time in find_partial_die, when we reread the compilation
18856 unit with load_all_dies set. */
72bf9492 18857
5afb4e99 18858 if (load_all
72929c62 18859 || abbrev->tag == DW_TAG_constant
5afb4e99 18860 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18861 || abbrev->tag == DW_TAG_variable
18862 || abbrev->tag == DW_TAG_namespace
18863 || part_die->is_declaration)
18864 {
18865 void **slot;
18866
18867 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18868 to_underlying (part_die->sect_off),
18869 INSERT);
72bf9492
DJ
18870 *slot = part_die;
18871 }
18872
72bf9492 18873 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18874 we have no reason to follow the children of structures; for other
98bfdba5
PA
18875 languages we have to, so that we can get at method physnames
18876 to infer fully qualified class names, for DW_AT_specification,
18877 and for C++ template arguments. For C++, we also look one level
18878 inside functions to find template arguments (if the name of the
18879 function does not already contain the template arguments).
bc30ff58 18880
0a4b0913
AB
18881 For Ada and Fortran, we need to scan the children of subprograms
18882 and lexical blocks as well because these languages allow the
18883 definition of nested entities that could be interesting for the
18884 debugger, such as nested subprograms for instance. */
72bf9492 18885 if (last_die->has_children
5afb4e99
DJ
18886 && (load_all
18887 || last_die->tag == DW_TAG_namespace
f55ee35c 18888 || last_die->tag == DW_TAG_module
72bf9492 18889 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18890 || (cu->language == language_cplus
18891 && last_die->tag == DW_TAG_subprogram
18892 && (last_die->name == NULL
18893 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18894 || (cu->language != language_c
18895 && (last_die->tag == DW_TAG_class_type
680b30c7 18896 || last_die->tag == DW_TAG_interface_type
72bf9492 18897 || last_die->tag == DW_TAG_structure_type
bc30ff58 18898 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18899 || ((cu->language == language_ada
18900 || cu->language == language_fortran)
bc30ff58
JB
18901 && (last_die->tag == DW_TAG_subprogram
18902 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18903 {
18904 nesting_level++;
18905 parent_die = last_die;
18906 continue;
18907 }
18908
18909 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18910 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18911
18912 /* Back to the top, do it again. */
18913 }
18914}
18915
6f06d47b
YQ
18916partial_die_info::partial_die_info (sect_offset sect_off_,
18917 struct abbrev_info *abbrev)
18918 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18919{
18920}
18921
35cc7ed7
YQ
18922/* Read a minimal amount of information into the minimal die structure.
18923 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18924
48fbe735
YQ
18925const gdb_byte *
18926partial_die_info::read (const struct die_reader_specs *reader,
18927 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18928{
dee91e82 18929 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18930 struct dwarf2_per_objfile *dwarf2_per_objfile
18931 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18932 unsigned int i;
c5aa993b 18933 int has_low_pc_attr = 0;
c906108c 18934 int has_high_pc_attr = 0;
91da1414 18935 int high_pc_relative = 0;
c906108c 18936
18a8505e 18937 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 18938 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18939 {
18a8505e
AT
18940 bool need_reprocess;
18941 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18942 info_ptr, &need_reprocess);
18943 /* String and address offsets that need to do the reprocessing have
18944 already been read at this point, so there is no need to wait until
18945 the loop terminates to do the reprocessing. */
18946 if (need_reprocess)
18947 read_attribute_reprocess (reader, &attr_vec[i]);
18948 attribute &attr = attr_vec[i];
c906108c 18949 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18950 partial symbol table. */
c906108c
SS
18951 switch (attr.name)
18952 {
18953 case DW_AT_name:
48fbe735 18954 switch (tag)
71c25dea
TT
18955 {
18956 case DW_TAG_compile_unit:
95554aad 18957 case DW_TAG_partial_unit:
348e048f 18958 case DW_TAG_type_unit:
71c25dea
TT
18959 /* Compilation units have a DW_AT_name that is a filename, not
18960 a source language identifier. */
18961 case DW_TAG_enumeration_type:
18962 case DW_TAG_enumerator:
18963 /* These tags always have simple identifiers already; no need
18964 to canonicalize them. */
48fbe735 18965 name = DW_STRING (&attr);
71c25dea
TT
18966 break;
18967 default:
48fbe735
YQ
18968 {
18969 struct objfile *objfile = dwarf2_per_objfile->objfile;
18970
18971 name
18972 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18973 &objfile->per_bfd->storage_obstack);
18974 }
71c25dea
TT
18975 break;
18976 }
c906108c 18977 break;
31ef98ae 18978 case DW_AT_linkage_name:
c906108c 18979 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18980 /* Note that both forms of linkage name might appear. We
18981 assume they will be the same, and we only store the last
18982 one we see. */
48fbe735 18983 linkage_name = DW_STRING (&attr);
c906108c
SS
18984 break;
18985 case DW_AT_low_pc:
18986 has_low_pc_attr = 1;
48fbe735 18987 lowpc = attr_value_as_address (&attr);
c906108c
SS
18988 break;
18989 case DW_AT_high_pc:
18990 has_high_pc_attr = 1;
48fbe735 18991 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18992 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18993 high_pc_relative = 1;
c906108c
SS
18994 break;
18995 case DW_AT_location:
0963b4bd 18996 /* Support the .debug_loc offsets. */
8e19ed76
PS
18997 if (attr_form_is_block (&attr))
18998 {
48fbe735 18999 d.locdesc = DW_BLOCK (&attr);
8e19ed76 19000 }
3690dd37 19001 else if (attr_form_is_section_offset (&attr))
8e19ed76 19002 {
4d3c2250 19003 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
19004 }
19005 else
19006 {
4d3c2250
KB
19007 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19008 "partial symbol information");
8e19ed76 19009 }
c906108c 19010 break;
c906108c 19011 case DW_AT_external:
48fbe735 19012 is_external = DW_UNSND (&attr);
c906108c
SS
19013 break;
19014 case DW_AT_declaration:
48fbe735 19015 is_declaration = DW_UNSND (&attr);
c906108c
SS
19016 break;
19017 case DW_AT_type:
48fbe735 19018 has_type = 1;
c906108c
SS
19019 break;
19020 case DW_AT_abstract_origin:
19021 case DW_AT_specification:
72bf9492 19022 case DW_AT_extension:
48fbe735
YQ
19023 has_specification = 1;
19024 spec_offset = dwarf2_get_ref_die_offset (&attr);
19025 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19026 || cu->per_cu->is_dwz);
c906108c
SS
19027 break;
19028 case DW_AT_sibling:
19029 /* Ignore absolute siblings, they might point outside of
19030 the current compile unit. */
19031 if (attr.form == DW_FORM_ref_addr)
b98664d3 19032 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19033 else
b9502d3f 19034 {
48fbe735 19035 const gdb_byte *buffer = reader->buffer;
9c541725
PA
19036 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19037 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19038
19039 if (sibling_ptr < info_ptr)
b98664d3 19040 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
19041 else if (sibling_ptr > reader->buffer_end)
19042 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19043 else
48fbe735 19044 sibling = sibling_ptr;
b9502d3f 19045 }
c906108c 19046 break;
fa4028e9 19047 case DW_AT_byte_size:
48fbe735 19048 has_byte_size = 1;
fa4028e9 19049 break;
ff908ebf 19050 case DW_AT_const_value:
48fbe735 19051 has_const_value = 1;
ff908ebf 19052 break;
68511cec
CES
19053 case DW_AT_calling_convention:
19054 /* DWARF doesn't provide a way to identify a program's source-level
19055 entry point. DW_AT_calling_convention attributes are only meant
19056 to describe functions' calling conventions.
19057
19058 However, because it's a necessary piece of information in
0c1b455e
TT
19059 Fortran, and before DWARF 4 DW_CC_program was the only
19060 piece of debugging information whose definition refers to
19061 a 'main program' at all, several compilers marked Fortran
19062 main programs with DW_CC_program --- even when those
19063 functions use the standard calling conventions.
19064
19065 Although DWARF now specifies a way to provide this
19066 information, we support this practice for backward
19067 compatibility. */
68511cec 19068 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19069 && cu->language == language_fortran)
48fbe735 19070 main_subprogram = 1;
68511cec 19071 break;
481860b3
GB
19072 case DW_AT_inline:
19073 if (DW_UNSND (&attr) == DW_INL_inlined
19074 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19075 may_be_inlined = 1;
481860b3 19076 break;
95554aad
TT
19077
19078 case DW_AT_import:
48fbe735 19079 if (tag == DW_TAG_imported_unit)
36586728 19080 {
48fbe735
YQ
19081 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19082 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19083 || cu->per_cu->is_dwz);
19084 }
95554aad
TT
19085 break;
19086
0c1b455e 19087 case DW_AT_main_subprogram:
48fbe735 19088 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19089 break;
19090
05caa1d2
TT
19091 case DW_AT_ranges:
19092 {
19093 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19094 but that requires a full DIE, so instead we just
19095 reimplement it. */
19096 int need_ranges_base = tag != DW_TAG_compile_unit;
19097 unsigned int ranges_offset = (DW_UNSND (&attr)
19098 + (need_ranges_base
19099 ? cu->ranges_base
19100 : 0));
19101
19102 /* Value of the DW_AT_ranges attribute is the offset in the
19103 .debug_ranges section. */
19104 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19105 nullptr))
19106 has_pc_info = 1;
19107 }
19108 break;
19109
c906108c
SS
19110 default:
19111 break;
19112 }
19113 }
19114
10d06d82
TT
19115 /* For Ada, if both the name and the linkage name appear, we prefer
19116 the latter. This lets "catch exception" work better, regardless
19117 of the order in which the name and linkage name were emitted.
19118 Really, though, this is just a workaround for the fact that gdb
19119 doesn't store both the name and the linkage name. */
19120 if (cu->language == language_ada && linkage_name != nullptr)
19121 name = linkage_name;
19122
91da1414 19123 if (high_pc_relative)
48fbe735 19124 highpc += lowpc;
91da1414 19125
9373cf26
JK
19126 if (has_low_pc_attr && has_high_pc_attr)
19127 {
19128 /* When using the GNU linker, .gnu.linkonce. sections are used to
19129 eliminate duplicate copies of functions and vtables and such.
19130 The linker will arbitrarily choose one and discard the others.
19131 The AT_*_pc values for such functions refer to local labels in
19132 these sections. If the section from that file was discarded, the
19133 labels are not in the output, so the relocs get a value of 0.
19134 If this is a discarded function, mark the pc bounds as invalid,
19135 so that GDB will ignore it. */
48fbe735 19136 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19137 {
48fbe735 19138 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19139 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19140
b98664d3 19141 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19142 "for DIE at %s [in module %s]"),
48fbe735
YQ
19143 paddress (gdbarch, lowpc),
19144 sect_offset_str (sect_off),
9d8780f0 19145 objfile_name (objfile));
9373cf26
JK
19146 }
19147 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19148 else if (lowpc >= highpc)
9373cf26 19149 {
48fbe735 19150 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19151 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19152
b98664d3 19153 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19154 "for DIE at %s [in module %s]"),
48fbe735
YQ
19155 paddress (gdbarch, lowpc),
19156 paddress (gdbarch, highpc),
19157 sect_offset_str (sect_off),
9c541725 19158 objfile_name (objfile));
9373cf26
JK
19159 }
19160 else
48fbe735 19161 has_pc_info = 1;
9373cf26 19162 }
85cbf3d3 19163
c906108c
SS
19164 return info_ptr;
19165}
19166
72bf9492
DJ
19167/* Find a cached partial DIE at OFFSET in CU. */
19168
d590ff25
YQ
19169struct partial_die_info *
19170dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19171{
19172 struct partial_die_info *lookup_die = NULL;
6f06d47b 19173 struct partial_die_info part_die (sect_off);
72bf9492 19174
9a3c8263 19175 lookup_die = ((struct partial_die_info *)
d590ff25 19176 htab_find_with_hash (partial_dies, &part_die,
9c541725 19177 to_underlying (sect_off)));
72bf9492 19178
72bf9492
DJ
19179 return lookup_die;
19180}
19181
348e048f
DE
19182/* Find a partial DIE at OFFSET, which may or may not be in CU,
19183 except in the case of .debug_types DIEs which do not reference
19184 outside their CU (they do however referencing other types via
55f1336d 19185 DW_FORM_ref_sig8). */
72bf9492 19186
122cf0f2 19187static const struct cu_partial_die_info
9c541725 19188find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19189{
518817b3
SM
19190 struct dwarf2_per_objfile *dwarf2_per_objfile
19191 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19192 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19193 struct dwarf2_per_cu_data *per_cu = NULL;
19194 struct partial_die_info *pd = NULL;
72bf9492 19195
36586728 19196 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19197 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19198 {
d590ff25 19199 pd = cu->find_partial_die (sect_off);
5afb4e99 19200 if (pd != NULL)
fb816e8b 19201 return { cu, pd };
0d99eb77
DE
19202 /* We missed recording what we needed.
19203 Load all dies and try again. */
19204 per_cu = cu->per_cu;
5afb4e99 19205 }
0d99eb77
DE
19206 else
19207 {
19208 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19209 if (cu->per_cu->is_debug_types)
0d99eb77 19210 {
9d8780f0
SM
19211 error (_("Dwarf Error: Type Unit at offset %s contains"
19212 " external reference to offset %s [in module %s].\n"),
19213 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19214 bfd_get_filename (objfile->obfd));
19215 }
9c541725 19216 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19217 dwarf2_per_objfile);
72bf9492 19218
0d99eb77
DE
19219 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19220 load_partial_comp_unit (per_cu);
ae038cb0 19221
0d99eb77 19222 per_cu->cu->last_used = 0;
d590ff25 19223 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19224 }
5afb4e99 19225
dee91e82
DE
19226 /* If we didn't find it, and not all dies have been loaded,
19227 load them all and try again. */
19228
5afb4e99
DJ
19229 if (pd == NULL && per_cu->load_all_dies == 0)
19230 {
5afb4e99 19231 per_cu->load_all_dies = 1;
fd820528
DE
19232
19233 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19234 THIS_CU->cu may already be in use. So we can't just free it and
19235 replace its DIEs with the ones we read in. Instead, we leave those
19236 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19237 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19238 set. */
dee91e82 19239 load_partial_comp_unit (per_cu);
5afb4e99 19240
d590ff25 19241 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19242 }
19243
19244 if (pd == NULL)
19245 internal_error (__FILE__, __LINE__,
9d8780f0 19246 _("could not find partial DIE %s "
3e43a32a 19247 "in cache [from module %s]\n"),
9d8780f0 19248 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19249 return { per_cu->cu, pd };
72bf9492
DJ
19250}
19251
abc72ce4
DE
19252/* See if we can figure out if the class lives in a namespace. We do
19253 this by looking for a member function; its demangled name will
19254 contain namespace info, if there is any. */
19255
19256static void
19257guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19258 struct dwarf2_cu *cu)
19259{
19260 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19261 what template types look like, because the demangler
19262 frequently doesn't give the same name as the debug info. We
19263 could fix this by only using the demangled name to get the
19264 prefix (but see comment in read_structure_type). */
19265
19266 struct partial_die_info *real_pdi;
19267 struct partial_die_info *child_pdi;
19268
19269 /* If this DIE (this DIE's specification, if any) has a parent, then
19270 we should not do this. We'll prepend the parent's fully qualified
19271 name when we create the partial symbol. */
19272
19273 real_pdi = struct_pdi;
19274 while (real_pdi->has_specification)
fb816e8b 19275 {
122cf0f2
AB
19276 auto res = find_partial_die (real_pdi->spec_offset,
19277 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19278 real_pdi = res.pdi;
19279 cu = res.cu;
19280 }
abc72ce4
DE
19281
19282 if (real_pdi->die_parent != NULL)
19283 return;
19284
19285 for (child_pdi = struct_pdi->die_child;
19286 child_pdi != NULL;
19287 child_pdi = child_pdi->die_sibling)
19288 {
19289 if (child_pdi->tag == DW_TAG_subprogram
19290 && child_pdi->linkage_name != NULL)
19291 {
43816ebc
TT
19292 gdb::unique_xmalloc_ptr<char> actual_class_name
19293 (language_class_name_from_physname (cu->language_defn,
19294 child_pdi->linkage_name));
abc72ce4
DE
19295 if (actual_class_name != NULL)
19296 {
518817b3 19297 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19298 struct_pdi->name
021887d8 19299 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 19300 actual_class_name.get ());
abc72ce4
DE
19301 }
19302 break;
19303 }
19304 }
19305}
19306
52356b79
YQ
19307void
19308partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19309{
abc72ce4
DE
19310 /* Once we've fixed up a die, there's no point in doing so again.
19311 This also avoids a memory leak if we were to call
19312 guess_partial_die_structure_name multiple times. */
52356b79 19313 if (fixup_called)
abc72ce4
DE
19314 return;
19315
72bf9492
DJ
19316 /* If we found a reference attribute and the DIE has no name, try
19317 to find a name in the referred to DIE. */
19318
52356b79 19319 if (name == NULL && has_specification)
72bf9492
DJ
19320 {
19321 struct partial_die_info *spec_die;
72bf9492 19322
122cf0f2 19323 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19324 spec_die = res.pdi;
19325 cu = res.cu;
72bf9492 19326
52356b79 19327 spec_die->fixup (cu);
72bf9492
DJ
19328
19329 if (spec_die->name)
19330 {
52356b79 19331 name = spec_die->name;
72bf9492
DJ
19332
19333 /* Copy DW_AT_external attribute if it is set. */
19334 if (spec_die->is_external)
52356b79 19335 is_external = spec_die->is_external;
72bf9492
DJ
19336 }
19337 }
19338
19339 /* Set default names for some unnamed DIEs. */
72bf9492 19340
52356b79
YQ
19341 if (name == NULL && tag == DW_TAG_namespace)
19342 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19343
abc72ce4
DE
19344 /* If there is no parent die to provide a namespace, and there are
19345 children, see if we can determine the namespace from their linkage
122d1940 19346 name. */
abc72ce4 19347 if (cu->language == language_cplus
fd5866f6 19348 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19349 && die_parent == NULL
19350 && has_children
19351 && (tag == DW_TAG_class_type
19352 || tag == DW_TAG_structure_type
19353 || tag == DW_TAG_union_type))
19354 guess_partial_die_structure_name (this, cu);
abc72ce4 19355
53832f31
TT
19356 /* GCC might emit a nameless struct or union that has a linkage
19357 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19358 if (name == NULL
19359 && (tag == DW_TAG_class_type
19360 || tag == DW_TAG_interface_type
19361 || tag == DW_TAG_structure_type
19362 || tag == DW_TAG_union_type)
19363 && linkage_name != NULL)
53832f31 19364 {
43816ebc
TT
19365 gdb::unique_xmalloc_ptr<char> demangled
19366 (gdb_demangle (linkage_name, DMGL_TYPES));
19367 if (demangled != nullptr)
53832f31 19368 {
96408a79
SA
19369 const char *base;
19370
19371 /* Strip any leading namespaces/classes, keep only the base name.
19372 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19373 base = strrchr (demangled.get (), ':');
19374 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19375 base++;
19376 else
43816ebc 19377 base = demangled.get ();
96408a79 19378
518817b3 19379 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19380 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19381 }
19382 }
19383
52356b79 19384 fixup_called = 1;
72bf9492
DJ
19385}
19386
18a8505e
AT
19387/* Process the attributes that had to be skipped in the first round. These
19388 attributes are the ones that need str_offsets_base or addr_base attributes.
19389 They could not have been processed in the first round, because at the time
19390 the values of str_offsets_base or addr_base may not have been known. */
19391void read_attribute_reprocess (const struct die_reader_specs *reader,
19392 struct attribute *attr)
19393{
19394 struct dwarf2_cu *cu = reader->cu;
19395 switch (attr->form)
19396 {
19397 case DW_FORM_addrx:
19398 case DW_FORM_GNU_addr_index:
19399 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19400 break;
19401 case DW_FORM_strx:
19402 case DW_FORM_strx1:
19403 case DW_FORM_strx2:
19404 case DW_FORM_strx3:
19405 case DW_FORM_strx4:
19406 case DW_FORM_GNU_str_index:
19407 {
19408 unsigned int str_index = DW_UNSND (attr);
19409 if (reader->dwo_file != NULL)
19410 {
19411 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19412 DW_STRING_IS_CANONICAL (attr) = 0;
19413 }
19414 else
19415 {
19416 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19417 DW_STRING_IS_CANONICAL (attr) = 0;
19418 }
19419 break;
19420 }
19421 default:
19422 gdb_assert_not_reached (_("Unexpected DWARF form."));
19423 }
19424}
19425
a8329558 19426/* Read an attribute value described by an attribute form. */
c906108c 19427
d521ce57 19428static const gdb_byte *
dee91e82
DE
19429read_attribute_value (const struct die_reader_specs *reader,
19430 struct attribute *attr, unsigned form,
18a8505e
AT
19431 LONGEST implicit_const, const gdb_byte *info_ptr,
19432 bool *need_reprocess)
c906108c 19433{
dee91e82 19434 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19435 struct dwarf2_per_objfile *dwarf2_per_objfile
19436 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19437 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19438 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19439 bfd *abfd = reader->abfd;
e7c27a73 19440 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19441 unsigned int bytes_read;
19442 struct dwarf_block *blk;
18a8505e 19443 *need_reprocess = false;
c906108c 19444
aead7601 19445 attr->form = (enum dwarf_form) form;
a8329558 19446 switch (form)
c906108c 19447 {
c906108c 19448 case DW_FORM_ref_addr:
ae411497 19449 if (cu->header.version == 2)
4568ecf9 19450 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19451 else
4568ecf9
DE
19452 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19453 &cu->header, &bytes_read);
ae411497
TT
19454 info_ptr += bytes_read;
19455 break;
36586728
TT
19456 case DW_FORM_GNU_ref_alt:
19457 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19458 info_ptr += bytes_read;
19459 break;
ae411497 19460 case DW_FORM_addr:
e7c27a73 19461 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19462 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19463 info_ptr += bytes_read;
c906108c
SS
19464 break;
19465 case DW_FORM_block2:
7b5a2f43 19466 blk = dwarf_alloc_block (cu);
c906108c
SS
19467 blk->size = read_2_bytes (abfd, info_ptr);
19468 info_ptr += 2;
19469 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19470 info_ptr += blk->size;
19471 DW_BLOCK (attr) = blk;
19472 break;
19473 case DW_FORM_block4:
7b5a2f43 19474 blk = dwarf_alloc_block (cu);
c906108c
SS
19475 blk->size = read_4_bytes (abfd, info_ptr);
19476 info_ptr += 4;
19477 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19478 info_ptr += blk->size;
19479 DW_BLOCK (attr) = blk;
19480 break;
19481 case DW_FORM_data2:
19482 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19483 info_ptr += 2;
19484 break;
19485 case DW_FORM_data4:
19486 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19487 info_ptr += 4;
19488 break;
19489 case DW_FORM_data8:
19490 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19491 info_ptr += 8;
19492 break;
0224619f
JK
19493 case DW_FORM_data16:
19494 blk = dwarf_alloc_block (cu);
19495 blk->size = 16;
19496 blk->data = read_n_bytes (abfd, info_ptr, 16);
19497 info_ptr += 16;
19498 DW_BLOCK (attr) = blk;
19499 break;
2dc7f7b3
TT
19500 case DW_FORM_sec_offset:
19501 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19502 info_ptr += bytes_read;
19503 break;
c906108c 19504 case DW_FORM_string:
9b1c24c8 19505 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19506 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19507 info_ptr += bytes_read;
19508 break;
4bdf3d34 19509 case DW_FORM_strp:
36586728
TT
19510 if (!cu->per_cu->is_dwz)
19511 {
ed2dc618
SM
19512 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19513 abfd, info_ptr, cu_header,
36586728
TT
19514 &bytes_read);
19515 DW_STRING_IS_CANONICAL (attr) = 0;
19516 info_ptr += bytes_read;
19517 break;
19518 }
19519 /* FALLTHROUGH */
43988095
JK
19520 case DW_FORM_line_strp:
19521 if (!cu->per_cu->is_dwz)
19522 {
ed2dc618
SM
19523 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19524 abfd, info_ptr,
43988095
JK
19525 cu_header, &bytes_read);
19526 DW_STRING_IS_CANONICAL (attr) = 0;
19527 info_ptr += bytes_read;
19528 break;
19529 }
19530 /* FALLTHROUGH */
36586728
TT
19531 case DW_FORM_GNU_strp_alt:
19532 {
ed2dc618 19533 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19534 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19535 &bytes_read);
19536
ed2dc618
SM
19537 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19538 dwz, str_offset);
36586728
TT
19539 DW_STRING_IS_CANONICAL (attr) = 0;
19540 info_ptr += bytes_read;
19541 }
4bdf3d34 19542 break;
2dc7f7b3 19543 case DW_FORM_exprloc:
c906108c 19544 case DW_FORM_block:
7b5a2f43 19545 blk = dwarf_alloc_block (cu);
c906108c
SS
19546 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19547 info_ptr += bytes_read;
19548 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19549 info_ptr += blk->size;
19550 DW_BLOCK (attr) = blk;
19551 break;
19552 case DW_FORM_block1:
7b5a2f43 19553 blk = dwarf_alloc_block (cu);
c906108c
SS
19554 blk->size = read_1_byte (abfd, info_ptr);
19555 info_ptr += 1;
19556 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19557 info_ptr += blk->size;
19558 DW_BLOCK (attr) = blk;
19559 break;
19560 case DW_FORM_data1:
19561 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19562 info_ptr += 1;
19563 break;
19564 case DW_FORM_flag:
19565 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19566 info_ptr += 1;
19567 break;
2dc7f7b3
TT
19568 case DW_FORM_flag_present:
19569 DW_UNSND (attr) = 1;
19570 break;
c906108c
SS
19571 case DW_FORM_sdata:
19572 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19573 info_ptr += bytes_read;
19574 break;
19575 case DW_FORM_udata:
18a8505e 19576 case DW_FORM_rnglistx:
c906108c
SS
19577 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19578 info_ptr += bytes_read;
19579 break;
19580 case DW_FORM_ref1:
9c541725 19581 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19582 + read_1_byte (abfd, info_ptr));
c906108c
SS
19583 info_ptr += 1;
19584 break;
19585 case DW_FORM_ref2:
9c541725 19586 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19587 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19588 info_ptr += 2;
19589 break;
19590 case DW_FORM_ref4:
9c541725 19591 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19592 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19593 info_ptr += 4;
19594 break;
613e1657 19595 case DW_FORM_ref8:
9c541725 19596 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19597 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19598 info_ptr += 8;
19599 break;
55f1336d 19600 case DW_FORM_ref_sig8:
ac9ec31b 19601 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19602 info_ptr += 8;
19603 break;
c906108c 19604 case DW_FORM_ref_udata:
9c541725 19605 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19606 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19607 info_ptr += bytes_read;
19608 break;
c906108c 19609 case DW_FORM_indirect:
a8329558
KW
19610 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19611 info_ptr += bytes_read;
43988095
JK
19612 if (form == DW_FORM_implicit_const)
19613 {
19614 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19615 info_ptr += bytes_read;
19616 }
19617 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19618 info_ptr, need_reprocess);
43988095
JK
19619 break;
19620 case DW_FORM_implicit_const:
19621 DW_SND (attr) = implicit_const;
a8329558 19622 break;
336d760d 19623 case DW_FORM_addrx:
3019eac3 19624 case DW_FORM_GNU_addr_index:
18a8505e
AT
19625 *need_reprocess = true;
19626 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19627 info_ptr += bytes_read;
19628 break;
cf532bd1 19629 case DW_FORM_strx:
15f18d14
AT
19630 case DW_FORM_strx1:
19631 case DW_FORM_strx2:
19632 case DW_FORM_strx3:
19633 case DW_FORM_strx4:
3019eac3 19634 case DW_FORM_GNU_str_index:
3019eac3 19635 {
15f18d14
AT
19636 ULONGEST str_index;
19637 if (form == DW_FORM_strx1)
19638 {
19639 str_index = read_1_byte (abfd, info_ptr);
19640 info_ptr += 1;
19641 }
19642 else if (form == DW_FORM_strx2)
19643 {
19644 str_index = read_2_bytes (abfd, info_ptr);
19645 info_ptr += 2;
19646 }
19647 else if (form == DW_FORM_strx3)
19648 {
19649 str_index = read_3_bytes (abfd, info_ptr);
19650 info_ptr += 3;
19651 }
19652 else if (form == DW_FORM_strx4)
19653 {
19654 str_index = read_4_bytes (abfd, info_ptr);
19655 info_ptr += 4;
19656 }
19657 else
19658 {
19659 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19660 info_ptr += bytes_read;
19661 }
18a8505e
AT
19662 *need_reprocess = true;
19663 DW_UNSND (attr) = str_index;
19664 }
3019eac3 19665 break;
c906108c 19666 default:
8a3fe4f8 19667 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19668 dwarf_form_name (form),
19669 bfd_get_filename (abfd));
c906108c 19670 }
28e94949 19671
36586728 19672 /* Super hack. */
7771576e 19673 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19674 attr->form = DW_FORM_GNU_ref_alt;
19675
28e94949
JB
19676 /* We have seen instances where the compiler tried to emit a byte
19677 size attribute of -1 which ended up being encoded as an unsigned
19678 0xffffffff. Although 0xffffffff is technically a valid size value,
19679 an object of this size seems pretty unlikely so we can relatively
19680 safely treat these cases as if the size attribute was invalid and
19681 treat them as zero by default. */
19682 if (attr->name == DW_AT_byte_size
19683 && form == DW_FORM_data4
19684 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19685 {
19686 complaint
b98664d3 19687 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19688 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19689 DW_UNSND (attr) = 0;
19690 }
28e94949 19691
c906108c
SS
19692 return info_ptr;
19693}
19694
a8329558
KW
19695/* Read an attribute described by an abbreviated attribute. */
19696
d521ce57 19697static const gdb_byte *
dee91e82
DE
19698read_attribute (const struct die_reader_specs *reader,
19699 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19700 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19701{
19702 attr->name = abbrev->name;
43988095 19703 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19704 abbrev->implicit_const, info_ptr,
19705 need_reprocess);
a8329558
KW
19706}
19707
0963b4bd 19708/* Read dwarf information from a buffer. */
c906108c
SS
19709
19710static unsigned int
a1855c1d 19711read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19712{
fe1b8b76 19713 return bfd_get_8 (abfd, buf);
c906108c
SS
19714}
19715
19716static int
a1855c1d 19717read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19718{
fe1b8b76 19719 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19720}
19721
19722static unsigned int
a1855c1d 19723read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19724{
fe1b8b76 19725 return bfd_get_16 (abfd, buf);
c906108c
SS
19726}
19727
21ae7a4d 19728static int
a1855c1d 19729read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19730{
19731 return bfd_get_signed_16 (abfd, buf);
19732}
19733
15f18d14
AT
19734static unsigned int
19735read_3_bytes (bfd *abfd, const gdb_byte *buf)
19736{
19737 unsigned int result = 0;
19738 for (int i = 0; i < 3; ++i)
19739 {
19740 unsigned char byte = bfd_get_8 (abfd, buf);
19741 buf++;
19742 result |= ((unsigned int) byte << (i * 8));
19743 }
19744 return result;
19745}
19746
c906108c 19747static unsigned int
a1855c1d 19748read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19749{
fe1b8b76 19750 return bfd_get_32 (abfd, buf);
c906108c
SS
19751}
19752
21ae7a4d 19753static int
a1855c1d 19754read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19755{
19756 return bfd_get_signed_32 (abfd, buf);
19757}
19758
93311388 19759static ULONGEST
a1855c1d 19760read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19761{
fe1b8b76 19762 return bfd_get_64 (abfd, buf);
c906108c
SS
19763}
19764
19765static CORE_ADDR
d521ce57 19766read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19767 unsigned int *bytes_read)
c906108c 19768{
e7c27a73 19769 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19770 CORE_ADDR retval = 0;
19771
107d2387 19772 if (cu_header->signed_addr_p)
c906108c 19773 {
107d2387
AC
19774 switch (cu_header->addr_size)
19775 {
19776 case 2:
fe1b8b76 19777 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19778 break;
19779 case 4:
fe1b8b76 19780 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19781 break;
19782 case 8:
fe1b8b76 19783 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19784 break;
19785 default:
8e65ff28 19786 internal_error (__FILE__, __LINE__,
e2e0b3e5 19787 _("read_address: bad switch, signed [in module %s]"),
659b0389 19788 bfd_get_filename (abfd));
107d2387
AC
19789 }
19790 }
19791 else
19792 {
19793 switch (cu_header->addr_size)
19794 {
19795 case 2:
fe1b8b76 19796 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19797 break;
19798 case 4:
fe1b8b76 19799 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19800 break;
19801 case 8:
fe1b8b76 19802 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19803 break;
19804 default:
8e65ff28 19805 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19806 _("read_address: bad switch, "
19807 "unsigned [in module %s]"),
659b0389 19808 bfd_get_filename (abfd));
107d2387 19809 }
c906108c 19810 }
64367e0a 19811
107d2387
AC
19812 *bytes_read = cu_header->addr_size;
19813 return retval;
c906108c
SS
19814}
19815
f7ef9339 19816/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19817 specification allows the initial length to take up either 4 bytes
19818 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19819 bytes describe the length and all offsets will be 8 bytes in length
19820 instead of 4.
19821
f7ef9339
KB
19822 An older, non-standard 64-bit format is also handled by this
19823 function. The older format in question stores the initial length
19824 as an 8-byte quantity without an escape value. Lengths greater
19825 than 2^32 aren't very common which means that the initial 4 bytes
19826 is almost always zero. Since a length value of zero doesn't make
19827 sense for the 32-bit format, this initial zero can be considered to
19828 be an escape value which indicates the presence of the older 64-bit
19829 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19830 greater than 4GB. If it becomes necessary to handle lengths
19831 somewhat larger than 4GB, we could allow other small values (such
19832 as the non-sensical values of 1, 2, and 3) to also be used as
19833 escape values indicating the presence of the old format.
f7ef9339 19834
917c78fc
MK
19835 The value returned via bytes_read should be used to increment the
19836 relevant pointer after calling read_initial_length().
c764a876 19837
613e1657
KB
19838 [ Note: read_initial_length() and read_offset() are based on the
19839 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19840 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19841 from:
19842
f7ef9339 19843 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19844
613e1657
KB
19845 This document is only a draft and is subject to change. (So beware.)
19846
f7ef9339 19847 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19848 determined empirically by examining 64-bit ELF files produced by
19849 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19850
19851 - Kevin, July 16, 2002
613e1657
KB
19852 ] */
19853
19854static LONGEST
d521ce57 19855read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19856{
fe1b8b76 19857 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19858
dd373385 19859 if (length == 0xffffffff)
613e1657 19860 {
fe1b8b76 19861 length = bfd_get_64 (abfd, buf + 4);
613e1657 19862 *bytes_read = 12;
613e1657 19863 }
dd373385 19864 else if (length == 0)
f7ef9339 19865 {
dd373385 19866 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19867 length = bfd_get_64 (abfd, buf);
f7ef9339 19868 *bytes_read = 8;
f7ef9339 19869 }
613e1657
KB
19870 else
19871 {
19872 *bytes_read = 4;
613e1657
KB
19873 }
19874
c764a876
DE
19875 return length;
19876}
dd373385 19877
c764a876
DE
19878/* Cover function for read_initial_length.
19879 Returns the length of the object at BUF, and stores the size of the
19880 initial length in *BYTES_READ and stores the size that offsets will be in
19881 *OFFSET_SIZE.
19882 If the initial length size is not equivalent to that specified in
19883 CU_HEADER then issue a complaint.
19884 This is useful when reading non-comp-unit headers. */
dd373385 19885
c764a876 19886static LONGEST
d521ce57 19887read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19888 const struct comp_unit_head *cu_header,
19889 unsigned int *bytes_read,
19890 unsigned int *offset_size)
19891{
19892 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19893
19894 gdb_assert (cu_header->initial_length_size == 4
19895 || cu_header->initial_length_size == 8
19896 || cu_header->initial_length_size == 12);
19897
19898 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19899 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19900
c764a876 19901 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19902 return length;
613e1657
KB
19903}
19904
19905/* Read an offset from the data stream. The size of the offset is
917c78fc 19906 given by cu_header->offset_size. */
613e1657
KB
19907
19908static LONGEST
d521ce57
TT
19909read_offset (bfd *abfd, const gdb_byte *buf,
19910 const struct comp_unit_head *cu_header,
891d2f0b 19911 unsigned int *bytes_read)
c764a876
DE
19912{
19913 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19914
c764a876
DE
19915 *bytes_read = cu_header->offset_size;
19916 return offset;
19917}
19918
19919/* Read an offset from the data stream. */
19920
19921static LONGEST
d521ce57 19922read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19923{
19924 LONGEST retval = 0;
19925
c764a876 19926 switch (offset_size)
613e1657
KB
19927 {
19928 case 4:
fe1b8b76 19929 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19930 break;
19931 case 8:
fe1b8b76 19932 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19933 break;
19934 default:
8e65ff28 19935 internal_error (__FILE__, __LINE__,
c764a876 19936 _("read_offset_1: bad switch [in module %s]"),
659b0389 19937 bfd_get_filename (abfd));
613e1657
KB
19938 }
19939
917c78fc 19940 return retval;
613e1657
KB
19941}
19942
d521ce57
TT
19943static const gdb_byte *
19944read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19945{
19946 /* If the size of a host char is 8 bits, we can return a pointer
19947 to the buffer, otherwise we have to copy the data to a buffer
19948 allocated on the temporary obstack. */
4bdf3d34 19949 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19950 return buf;
c906108c
SS
19951}
19952
d521ce57
TT
19953static const char *
19954read_direct_string (bfd *abfd, const gdb_byte *buf,
19955 unsigned int *bytes_read_ptr)
c906108c
SS
19956{
19957 /* If the size of a host char is 8 bits, we can return a pointer
19958 to the string, otherwise we have to copy the string to a buffer
19959 allocated on the temporary obstack. */
4bdf3d34 19960 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19961 if (*buf == '\0')
19962 {
19963 *bytes_read_ptr = 1;
19964 return NULL;
19965 }
d521ce57
TT
19966 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19967 return (const char *) buf;
4bdf3d34
JJ
19968}
19969
43988095
JK
19970/* Return pointer to string at section SECT offset STR_OFFSET with error
19971 reporting strings FORM_NAME and SECT_NAME. */
19972
d521ce57 19973static const char *
ed2dc618
SM
19974read_indirect_string_at_offset_from (struct objfile *objfile,
19975 bfd *abfd, LONGEST str_offset,
43988095
JK
19976 struct dwarf2_section_info *sect,
19977 const char *form_name,
19978 const char *sect_name)
19979{
ed2dc618 19980 dwarf2_read_section (objfile, sect);
43988095
JK
19981 if (sect->buffer == NULL)
19982 error (_("%s used without %s section [in module %s]"),
19983 form_name, sect_name, bfd_get_filename (abfd));
19984 if (str_offset >= sect->size)
19985 error (_("%s pointing outside of %s section [in module %s]"),
19986 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19987 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19988 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19989 return NULL;
43988095
JK
19990 return (const char *) (sect->buffer + str_offset);
19991}
19992
19993/* Return pointer to string at .debug_str offset STR_OFFSET. */
19994
19995static const char *
ed2dc618
SM
19996read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19997 bfd *abfd, LONGEST str_offset)
43988095 19998{
ed2dc618
SM
19999 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20000 abfd, str_offset,
43988095
JK
20001 &dwarf2_per_objfile->str,
20002 "DW_FORM_strp", ".debug_str");
20003}
20004
20005/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
20006
20007static const char *
ed2dc618
SM
20008read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20009 bfd *abfd, LONGEST str_offset)
43988095 20010{
ed2dc618
SM
20011 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20012 abfd, str_offset,
43988095
JK
20013 &dwarf2_per_objfile->line_str,
20014 "DW_FORM_line_strp",
20015 ".debug_line_str");
c906108c
SS
20016}
20017
36586728
TT
20018/* Read a string at offset STR_OFFSET in the .debug_str section from
20019 the .dwz file DWZ. Throw an error if the offset is too large. If
20020 the string consists of a single NUL byte, return NULL; otherwise
20021 return a pointer to the string. */
20022
d521ce57 20023static const char *
ed2dc618
SM
20024read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20025 LONGEST str_offset)
36586728 20026{
ed2dc618 20027 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
20028
20029 if (dwz->str.buffer == NULL)
20030 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20031 "section [in module %s]"),
00f93c44 20032 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20033 if (str_offset >= dwz->str.size)
20034 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20035 ".debug_str section [in module %s]"),
00f93c44 20036 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20037 gdb_assert (HOST_CHAR_BIT == 8);
20038 if (dwz->str.buffer[str_offset] == '\0')
20039 return NULL;
d521ce57 20040 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
20041}
20042
43988095
JK
20043/* Return pointer to string at .debug_str offset as read from BUF.
20044 BUF is assumed to be in a compilation unit described by CU_HEADER.
20045 Return *BYTES_READ_PTR count of bytes read from BUF. */
20046
d521ce57 20047static const char *
ed2dc618
SM
20048read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20049 const gdb_byte *buf,
cf2c3c16
TT
20050 const struct comp_unit_head *cu_header,
20051 unsigned int *bytes_read_ptr)
20052{
20053 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20054
ed2dc618 20055 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
20056}
20057
43988095
JK
20058/* Return pointer to string at .debug_line_str offset as read from BUF.
20059 BUF is assumed to be in a compilation unit described by CU_HEADER.
20060 Return *BYTES_READ_PTR count of bytes read from BUF. */
20061
20062static const char *
ed2dc618
SM
20063read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20064 bfd *abfd, const gdb_byte *buf,
43988095
JK
20065 const struct comp_unit_head *cu_header,
20066 unsigned int *bytes_read_ptr)
20067{
20068 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20069
ed2dc618
SM
20070 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20071 str_offset);
43988095
JK
20072}
20073
20074ULONGEST
d521ce57 20075read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 20076 unsigned int *bytes_read_ptr)
c906108c 20077{
12df843f 20078 ULONGEST result;
ce5d95e1 20079 unsigned int num_read;
870f88f7 20080 int shift;
c906108c
SS
20081 unsigned char byte;
20082
20083 result = 0;
20084 shift = 0;
20085 num_read = 0;
c906108c
SS
20086 while (1)
20087 {
fe1b8b76 20088 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20089 buf++;
20090 num_read++;
12df843f 20091 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20092 if ((byte & 128) == 0)
20093 {
20094 break;
20095 }
20096 shift += 7;
20097 }
20098 *bytes_read_ptr = num_read;
20099 return result;
20100}
20101
12df843f 20102static LONGEST
d521ce57
TT
20103read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20104 unsigned int *bytes_read_ptr)
c906108c 20105{
4dd1b460 20106 ULONGEST result;
870f88f7 20107 int shift, num_read;
c906108c
SS
20108 unsigned char byte;
20109
20110 result = 0;
20111 shift = 0;
c906108c 20112 num_read = 0;
c906108c
SS
20113 while (1)
20114 {
fe1b8b76 20115 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20116 buf++;
20117 num_read++;
4dd1b460 20118 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20119 shift += 7;
20120 if ((byte & 128) == 0)
20121 {
20122 break;
20123 }
20124 }
77e0b926 20125 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 20126 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
20127 *bytes_read_ptr = num_read;
20128 return result;
20129}
20130
3019eac3 20131/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20132 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20133 ADDR_SIZE is the size of addresses from the CU header. */
20134
20135static CORE_ADDR
ed2dc618 20136read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
20137 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
20138 int addr_size)
3019eac3
DE
20139{
20140 struct objfile *objfile = dwarf2_per_objfile->objfile;
20141 bfd *abfd = objfile->obfd;
20142 const gdb_byte *info_ptr;
18a8505e 20143 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3
DE
20144
20145 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20146 if (dwarf2_per_objfile->addr.buffer == NULL)
20147 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20148 objfile_name (objfile));
18a8505e
AT
20149 if (addr_base_or_zero + addr_index * addr_size
20150 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
20151 error (_("DW_FORM_addr_index pointing outside of "
20152 ".debug_addr section [in module %s]"),
4262abfb 20153 objfile_name (objfile));
3019eac3 20154 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 20155 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
20156 if (addr_size == 4)
20157 return bfd_get_32 (abfd, info_ptr);
20158 else
20159 return bfd_get_64 (abfd, info_ptr);
20160}
20161
20162/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20163
20164static CORE_ADDR
20165read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20166{
518817b3
SM
20167 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20168 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20169}
20170
20171/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20172
20173static CORE_ADDR
d521ce57 20174read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20175 unsigned int *bytes_read)
20176{
518817b3 20177 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20178 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20179
20180 return read_addr_index (cu, addr_index);
20181}
20182
3019eac3
DE
20183/* Given an index in .debug_addr, fetch the value.
20184 NOTE: This can be called during dwarf expression evaluation,
20185 long after the debug information has been read, and thus per_cu->cu
20186 may no longer exist. */
20187
20188CORE_ADDR
20189dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20190 unsigned int addr_index)
20191{
ed2dc618 20192 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 20193 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 20194 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20195 int addr_size;
20196
3019eac3
DE
20197 /* We need addr_base and addr_size.
20198 If we don't have PER_CU->cu, we have to get it.
20199 Nasty, but the alternative is storing the needed info in PER_CU,
20200 which at this point doesn't seem justified: it's not clear how frequently
20201 it would get used and it would increase the size of every PER_CU.
20202 Entry points like dwarf2_per_cu_addr_size do a similar thing
20203 so we're not in uncharted territory here.
20204 Alas we need to be a bit more complicated as addr_base is contained
20205 in the DIE.
20206
20207 We don't need to read the entire CU(/TU).
20208 We just need the header and top level die.
a1b64ce1 20209
3019eac3 20210 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20211 For now we skip this optimization. */
3019eac3
DE
20212
20213 if (cu != NULL)
20214 {
20215 addr_base = cu->addr_base;
20216 addr_size = cu->header.addr_size;
20217 }
20218 else
20219 {
c0ab21c2
TT
20220 cutu_reader reader (per_cu, NULL, 0, 0, false);
20221 addr_base = reader.cu->addr_base;
20222 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20223 }
20224
ed2dc618
SM
20225 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20226 addr_size);
3019eac3
DE
20227}
20228
18a8505e
AT
20229/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20230 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20231 DWO file. */
3019eac3 20232
d521ce57 20233static const char *
18a8505e
AT
20234read_str_index (struct dwarf2_cu *cu,
20235 struct dwarf2_section_info *str_section,
20236 struct dwarf2_section_info *str_offsets_section,
20237 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20238{
518817b3
SM
20239 struct dwarf2_per_objfile *dwarf2_per_objfile
20240 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20241 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20242 const char *objf_name = objfile_name (objfile);
3019eac3 20243 bfd *abfd = objfile->obfd;
d521ce57 20244 const gdb_byte *info_ptr;
3019eac3 20245 ULONGEST str_offset;
cf532bd1 20246 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20247
73869dc2
DE
20248 dwarf2_read_section (objfile, str_section);
20249 dwarf2_read_section (objfile, str_offsets_section);
20250 if (str_section->buffer == NULL)
18a8505e 20251 error (_("%s used without %s section"
9d8780f0 20252 " in CU at offset %s [in module %s]"),
18a8505e
AT
20253 form_name, get_section_name (str_section),
20254 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20255 if (str_offsets_section->buffer == NULL)
18a8505e 20256 error (_("%s used without %s section"
9d8780f0 20257 " in CU at offset %s [in module %s]"),
18a8505e
AT
20258 form_name, get_section_name (str_section),
20259 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20260 info_ptr = (str_offsets_section->buffer
18a8505e 20261 + str_offsets_base
3019eac3
DE
20262 + str_index * cu->header.offset_size);
20263 if (cu->header.offset_size == 4)
20264 str_offset = bfd_get_32 (abfd, info_ptr);
20265 else
20266 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20267 if (str_offset >= str_section->size)
57d63ce2 20268 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20269 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20270 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20271 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20272}
20273
18a8505e
AT
20274/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20275
20276static const char *
20277read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20278{
20279 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20280 ? reader->cu->header.addr_size : 0;
20281 return read_str_index (reader->cu,
20282 &reader->dwo_file->sections.str,
20283 &reader->dwo_file->sections.str_offsets,
20284 str_offsets_base, str_index);
20285}
20286
20287/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20288
20289static const char *
20290read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20291{
20292 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20293 const char *objf_name = objfile_name (objfile);
20294 static const char form_name[] = "DW_FORM_GNU_str_index";
20295 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20296
20297 if (!cu->str_offsets_base.has_value ())
20298 error (_("%s used in Fission stub without %s"
20299 " in CU at offset 0x%lx [in module %s]"),
20300 form_name, str_offsets_attr_name,
20301 (long) cu->header.offset_size, objf_name);
20302
20303 return read_str_index (cu,
20304 &cu->per_cu->dwarf2_per_objfile->str,
20305 &cu->per_cu->dwarf2_per_objfile->str_offsets,
20306 *cu->str_offsets_base, str_index);
20307}
20308
3019eac3
DE
20309/* Return the length of an LEB128 number in BUF. */
20310
20311static int
20312leb128_size (const gdb_byte *buf)
20313{
20314 const gdb_byte *begin = buf;
20315 gdb_byte byte;
20316
20317 while (1)
20318 {
20319 byte = *buf++;
20320 if ((byte & 128) == 0)
20321 return buf - begin;
20322 }
20323}
20324
c906108c 20325static void
e142c38c 20326set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20327{
20328 switch (lang)
20329 {
20330 case DW_LANG_C89:
76bee0cc 20331 case DW_LANG_C99:
0cfd832f 20332 case DW_LANG_C11:
c906108c 20333 case DW_LANG_C:
d1be3247 20334 case DW_LANG_UPC:
e142c38c 20335 cu->language = language_c;
c906108c 20336 break;
9c37b5ae 20337 case DW_LANG_Java:
c906108c 20338 case DW_LANG_C_plus_plus:
0cfd832f
MW
20339 case DW_LANG_C_plus_plus_11:
20340 case DW_LANG_C_plus_plus_14:
e142c38c 20341 cu->language = language_cplus;
c906108c 20342 break;
6aecb9c2
JB
20343 case DW_LANG_D:
20344 cu->language = language_d;
20345 break;
c906108c
SS
20346 case DW_LANG_Fortran77:
20347 case DW_LANG_Fortran90:
b21b22e0 20348 case DW_LANG_Fortran95:
f7de9aab
MW
20349 case DW_LANG_Fortran03:
20350 case DW_LANG_Fortran08:
e142c38c 20351 cu->language = language_fortran;
c906108c 20352 break;
a766d390
DE
20353 case DW_LANG_Go:
20354 cu->language = language_go;
20355 break;
c906108c 20356 case DW_LANG_Mips_Assembler:
e142c38c 20357 cu->language = language_asm;
c906108c
SS
20358 break;
20359 case DW_LANG_Ada83:
8aaf0b47 20360 case DW_LANG_Ada95:
bc5f45f8
JB
20361 cu->language = language_ada;
20362 break;
72019c9c
GM
20363 case DW_LANG_Modula2:
20364 cu->language = language_m2;
20365 break;
fe8e67fd
PM
20366 case DW_LANG_Pascal83:
20367 cu->language = language_pascal;
20368 break;
22566fbd
DJ
20369 case DW_LANG_ObjC:
20370 cu->language = language_objc;
20371 break;
c44af4eb
TT
20372 case DW_LANG_Rust:
20373 case DW_LANG_Rust_old:
20374 cu->language = language_rust;
20375 break;
c906108c
SS
20376 case DW_LANG_Cobol74:
20377 case DW_LANG_Cobol85:
c906108c 20378 default:
e142c38c 20379 cu->language = language_minimal;
c906108c
SS
20380 break;
20381 }
e142c38c 20382 cu->language_defn = language_def (cu->language);
c906108c
SS
20383}
20384
20385/* Return the named attribute or NULL if not there. */
20386
20387static struct attribute *
e142c38c 20388dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20389{
a48e046c 20390 for (;;)
c906108c 20391 {
a48e046c
TT
20392 unsigned int i;
20393 struct attribute *spec = NULL;
20394
20395 for (i = 0; i < die->num_attrs; ++i)
20396 {
20397 if (die->attrs[i].name == name)
20398 return &die->attrs[i];
20399 if (die->attrs[i].name == DW_AT_specification
20400 || die->attrs[i].name == DW_AT_abstract_origin)
20401 spec = &die->attrs[i];
20402 }
20403
20404 if (!spec)
20405 break;
c906108c 20406
f2f0e013 20407 die = follow_die_ref (die, spec, &cu);
f2f0e013 20408 }
c5aa993b 20409
c906108c
SS
20410 return NULL;
20411}
20412
348e048f
DE
20413/* Return the named attribute or NULL if not there,
20414 but do not follow DW_AT_specification, etc.
20415 This is for use in contexts where we're reading .debug_types dies.
20416 Following DW_AT_specification, DW_AT_abstract_origin will take us
20417 back up the chain, and we want to go down. */
20418
20419static struct attribute *
45e58e77 20420dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20421{
20422 unsigned int i;
20423
20424 for (i = 0; i < die->num_attrs; ++i)
20425 if (die->attrs[i].name == name)
20426 return &die->attrs[i];
20427
20428 return NULL;
20429}
20430
7d45c7c3
KB
20431/* Return the string associated with a string-typed attribute, or NULL if it
20432 is either not found or is of an incorrect type. */
20433
20434static const char *
20435dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20436{
20437 struct attribute *attr;
20438 const char *str = NULL;
20439
20440 attr = dwarf2_attr (die, name, cu);
20441
20442 if (attr != NULL)
20443 {
43988095 20444 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20445 || attr->form == DW_FORM_string
cf532bd1 20446 || attr->form == DW_FORM_strx
8fe0f950
AT
20447 || attr->form == DW_FORM_strx1
20448 || attr->form == DW_FORM_strx2
20449 || attr->form == DW_FORM_strx3
20450 || attr->form == DW_FORM_strx4
b3340438 20451 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20452 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20453 str = DW_STRING (attr);
20454 else
b98664d3 20455 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20456 "DIE at %s in module %s"),
20457 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20458 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20459 }
20460
20461 return str;
20462}
20463
a084a2a6 20464/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20465 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20466static const char *
20467dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20468{
20469 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20470 if (dwo_name == nullptr)
20471 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20472 return dwo_name;
20473}
20474
05cf31d1
JB
20475/* Return non-zero iff the attribute NAME is defined for the given DIE,
20476 and holds a non-zero value. This function should only be used for
2dc7f7b3 20477 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20478
20479static int
20480dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20481{
20482 struct attribute *attr = dwarf2_attr (die, name, cu);
20483
20484 return (attr && DW_UNSND (attr));
20485}
20486
3ca72b44 20487static int
e142c38c 20488die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20489{
05cf31d1
JB
20490 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20491 which value is non-zero. However, we have to be careful with
20492 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20493 (via dwarf2_flag_true_p) follows this attribute. So we may
20494 end up accidently finding a declaration attribute that belongs
20495 to a different DIE referenced by the specification attribute,
20496 even though the given DIE does not have a declaration attribute. */
20497 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20498 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20499}
20500
63d06c5c 20501/* Return the die giving the specification for DIE, if there is
f2f0e013 20502 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20503 containing the return value on output. If there is no
20504 specification, but there is an abstract origin, that is
20505 returned. */
63d06c5c
DC
20506
20507static struct die_info *
f2f0e013 20508die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20509{
f2f0e013
DJ
20510 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20511 *spec_cu);
63d06c5c 20512
edb3359d
DJ
20513 if (spec_attr == NULL)
20514 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20515
63d06c5c
DC
20516 if (spec_attr == NULL)
20517 return NULL;
20518 else
f2f0e013 20519 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20520}
c906108c 20521
527f3840
JK
20522/* Stub for free_line_header to match void * callback types. */
20523
20524static void
20525free_line_header_voidp (void *arg)
20526{
9a3c8263 20527 struct line_header *lh = (struct line_header *) arg;
527f3840 20528
fff8551c 20529 delete lh;
527f3840
JK
20530}
20531
fff8551c
PA
20532void
20533line_header::add_include_dir (const char *include_dir)
c906108c 20534{
27e0867f 20535 if (dwarf_line_debug >= 2)
7ba99d21
AT
20536 {
20537 size_t new_size;
20538 if (version >= 5)
20539 new_size = m_include_dirs.size ();
20540 else
20541 new_size = m_include_dirs.size () + 1;
20542 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20543 new_size, include_dir);
20544 }
20545 m_include_dirs.push_back (include_dir);
debd256d 20546}
6e70227d 20547
fff8551c
PA
20548void
20549line_header::add_file_name (const char *name,
ecfb656c 20550 dir_index d_index,
fff8551c
PA
20551 unsigned int mod_time,
20552 unsigned int length)
debd256d 20553{
27e0867f 20554 if (dwarf_line_debug >= 2)
7ba99d21
AT
20555 {
20556 size_t new_size;
20557 if (version >= 5)
20558 new_size = file_names_size ();
20559 else
20560 new_size = file_names_size () + 1;
20561 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20562 new_size, name);
20563 }
20564 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20565}
6e70227d 20566
83769d0b 20567/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20568
20569static struct dwarf2_section_info *
20570get_debug_line_section (struct dwarf2_cu *cu)
20571{
20572 struct dwarf2_section_info *section;
518817b3
SM
20573 struct dwarf2_per_objfile *dwarf2_per_objfile
20574 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20575
20576 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20577 DWO file. */
20578 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20579 section = &cu->dwo_unit->dwo_file->sections.line;
20580 else if (cu->per_cu->is_dwz)
20581 {
ed2dc618 20582 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20583
20584 section = &dwz->line;
20585 }
20586 else
20587 section = &dwarf2_per_objfile->line;
20588
20589 return section;
20590}
20591
43988095
JK
20592/* Read directory or file name entry format, starting with byte of
20593 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20594 entries count and the entries themselves in the described entry
20595 format. */
20596
20597static void
ed2dc618
SM
20598read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20599 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20600 struct line_header *lh,
20601 const struct comp_unit_head *cu_header,
20602 void (*callback) (struct line_header *lh,
20603 const char *name,
ecfb656c 20604 dir_index d_index,
43988095
JK
20605 unsigned int mod_time,
20606 unsigned int length))
20607{
20608 gdb_byte format_count, formati;
20609 ULONGEST data_count, datai;
20610 const gdb_byte *buf = *bufp;
20611 const gdb_byte *format_header_data;
43988095
JK
20612 unsigned int bytes_read;
20613
20614 format_count = read_1_byte (abfd, buf);
20615 buf += 1;
20616 format_header_data = buf;
20617 for (formati = 0; formati < format_count; formati++)
20618 {
20619 read_unsigned_leb128 (abfd, buf, &bytes_read);
20620 buf += bytes_read;
20621 read_unsigned_leb128 (abfd, buf, &bytes_read);
20622 buf += bytes_read;
20623 }
20624
20625 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20626 buf += bytes_read;
20627 for (datai = 0; datai < data_count; datai++)
20628 {
20629 const gdb_byte *format = format_header_data;
20630 struct file_entry fe;
20631
43988095
JK
20632 for (formati = 0; formati < format_count; formati++)
20633 {
ecfb656c 20634 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20635 format += bytes_read;
43988095 20636
ecfb656c 20637 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20638 format += bytes_read;
ecfb656c
PA
20639
20640 gdb::optional<const char *> string;
20641 gdb::optional<unsigned int> uint;
20642
43988095
JK
20643 switch (form)
20644 {
20645 case DW_FORM_string:
ecfb656c 20646 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20647 buf += bytes_read;
20648 break;
20649
20650 case DW_FORM_line_strp:
ed2dc618
SM
20651 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20652 abfd, buf,
ecfb656c
PA
20653 cu_header,
20654 &bytes_read));
43988095
JK
20655 buf += bytes_read;
20656 break;
20657
20658 case DW_FORM_data1:
ecfb656c 20659 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20660 buf += 1;
20661 break;
20662
20663 case DW_FORM_data2:
ecfb656c 20664 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20665 buf += 2;
20666 break;
20667
20668 case DW_FORM_data4:
ecfb656c 20669 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20670 buf += 4;
20671 break;
20672
20673 case DW_FORM_data8:
ecfb656c 20674 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20675 buf += 8;
20676 break;
20677
7ba99d21
AT
20678 case DW_FORM_data16:
20679 /* This is used for MD5, but file_entry does not record MD5s. */
20680 buf += 16;
20681 break;
20682
43988095 20683 case DW_FORM_udata:
ecfb656c 20684 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20685 buf += bytes_read;
20686 break;
20687
20688 case DW_FORM_block:
20689 /* It is valid only for DW_LNCT_timestamp which is ignored by
20690 current GDB. */
20691 break;
20692 }
ecfb656c
PA
20693
20694 switch (content_type)
20695 {
20696 case DW_LNCT_path:
20697 if (string.has_value ())
20698 fe.name = *string;
20699 break;
20700 case DW_LNCT_directory_index:
20701 if (uint.has_value ())
20702 fe.d_index = (dir_index) *uint;
20703 break;
20704 case DW_LNCT_timestamp:
20705 if (uint.has_value ())
20706 fe.mod_time = *uint;
20707 break;
20708 case DW_LNCT_size:
20709 if (uint.has_value ())
20710 fe.length = *uint;
20711 break;
20712 case DW_LNCT_MD5:
20713 break;
20714 default:
b98664d3 20715 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20716 pulongest (content_type));
20717 }
43988095
JK
20718 }
20719
ecfb656c 20720 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20721 }
20722
20723 *bufp = buf;
20724}
20725
debd256d 20726/* Read the statement program header starting at OFFSET in
3019eac3 20727 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20728 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20729 Returns NULL if there is a problem reading the header, e.g., if it
20730 has a version we don't understand.
debd256d
JB
20731
20732 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20733 the returned object point into the dwarf line section buffer,
20734 and must not be freed. */
ae2de4f8 20735
fff8551c 20736static line_header_up
9c541725 20737dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20738{
d521ce57 20739 const gdb_byte *line_ptr;
c764a876 20740 unsigned int bytes_read, offset_size;
debd256d 20741 int i;
d521ce57 20742 const char *cur_dir, *cur_file;
3019eac3
DE
20743 struct dwarf2_section_info *section;
20744 bfd *abfd;
518817b3
SM
20745 struct dwarf2_per_objfile *dwarf2_per_objfile
20746 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20747
36586728 20748 section = get_debug_line_section (cu);
3019eac3
DE
20749 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20750 if (section->buffer == NULL)
debd256d 20751 {
3019eac3 20752 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20753 complaint (_("missing .debug_line.dwo section"));
3019eac3 20754 else
b98664d3 20755 complaint (_("missing .debug_line section"));
debd256d
JB
20756 return 0;
20757 }
20758
fceca515
DE
20759 /* We can't do this until we know the section is non-empty.
20760 Only then do we know we have such a section. */
a32a8923 20761 abfd = get_section_bfd_owner (section);
fceca515 20762
a738430d
MK
20763 /* Make sure that at least there's room for the total_length field.
20764 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20765 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20766 {
4d3c2250 20767 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20768 return 0;
20769 }
20770
fff8551c 20771 line_header_up lh (new line_header ());
debd256d 20772
9c541725 20773 lh->sect_off = sect_off;
527f3840
JK
20774 lh->offset_in_dwz = cu->per_cu->is_dwz;
20775
9c541725 20776 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20777
a738430d 20778 /* Read in the header. */
6e70227d 20779 lh->total_length =
c764a876
DE
20780 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20781 &bytes_read, &offset_size);
debd256d 20782 line_ptr += bytes_read;
7ba99d21
AT
20783
20784 const gdb_byte *start_here = line_ptr;
20785
3019eac3 20786 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20787 {
4d3c2250 20788 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20789 return 0;
20790 }
7ba99d21 20791 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20792 lh->version = read_2_bytes (abfd, line_ptr);
20793 line_ptr += 2;
43988095 20794 if (lh->version > 5)
cd366ee8
DE
20795 {
20796 /* This is a version we don't understand. The format could have
20797 changed in ways we don't handle properly so just punt. */
b98664d3 20798 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20799 return NULL;
20800 }
43988095
JK
20801 if (lh->version >= 5)
20802 {
20803 gdb_byte segment_selector_size;
20804
20805 /* Skip address size. */
20806 read_1_byte (abfd, line_ptr);
20807 line_ptr += 1;
20808
20809 segment_selector_size = read_1_byte (abfd, line_ptr);
20810 line_ptr += 1;
20811 if (segment_selector_size != 0)
20812 {
b98664d3 20813 complaint (_("unsupported segment selector size %u "
43988095
JK
20814 "in .debug_line section"),
20815 segment_selector_size);
20816 return NULL;
20817 }
20818 }
c764a876
DE
20819 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20820 line_ptr += offset_size;
7ba99d21 20821 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20822 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20823 line_ptr += 1;
2dc7f7b3
TT
20824 if (lh->version >= 4)
20825 {
20826 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20827 line_ptr += 1;
20828 }
20829 else
20830 lh->maximum_ops_per_instruction = 1;
20831
20832 if (lh->maximum_ops_per_instruction == 0)
20833 {
20834 lh->maximum_ops_per_instruction = 1;
b98664d3 20835 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20836 "in `.debug_line' section"));
2dc7f7b3
TT
20837 }
20838
debd256d
JB
20839 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20840 line_ptr += 1;
20841 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20842 line_ptr += 1;
20843 lh->line_range = read_1_byte (abfd, line_ptr);
20844 line_ptr += 1;
20845 lh->opcode_base = read_1_byte (abfd, line_ptr);
20846 line_ptr += 1;
fff8551c 20847 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20848
20849 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20850 for (i = 1; i < lh->opcode_base; ++i)
20851 {
20852 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20853 line_ptr += 1;
20854 }
20855
43988095 20856 if (lh->version >= 5)
debd256d 20857 {
43988095 20858 /* Read directory table. */
ed2dc618
SM
20859 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20860 &cu->header,
b926417a 20861 [] (struct line_header *header, const char *name,
ecfb656c 20862 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20863 unsigned int length)
20864 {
b926417a 20865 header->add_include_dir (name);
fff8551c 20866 });
debd256d 20867
43988095 20868 /* Read file name table. */
ed2dc618
SM
20869 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20870 &cu->header,
b926417a 20871 [] (struct line_header *header, const char *name,
ecfb656c 20872 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20873 unsigned int length)
20874 {
b926417a 20875 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20876 });
43988095
JK
20877 }
20878 else
debd256d 20879 {
43988095
JK
20880 /* Read directory table. */
20881 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20882 {
20883 line_ptr += bytes_read;
fff8551c 20884 lh->add_include_dir (cur_dir);
43988095 20885 }
debd256d
JB
20886 line_ptr += bytes_read;
20887
43988095
JK
20888 /* Read file name table. */
20889 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20890 {
ecfb656c
PA
20891 unsigned int mod_time, length;
20892 dir_index d_index;
43988095
JK
20893
20894 line_ptr += bytes_read;
ecfb656c 20895 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20896 line_ptr += bytes_read;
20897 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20898 line_ptr += bytes_read;
20899 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20900 line_ptr += bytes_read;
20901
ecfb656c 20902 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20903 }
20904 line_ptr += bytes_read;
debd256d 20905 }
debd256d 20906
3019eac3 20907 if (line_ptr > (section->buffer + section->size))
b98664d3 20908 complaint (_("line number info header doesn't "
3e43a32a 20909 "fit in `.debug_line' section"));
debd256d 20910
debd256d
JB
20911 return lh;
20912}
c906108c 20913
c6da4cef 20914/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20915 Return the file name of the psymtab for the given file_entry.
c6da4cef 20916 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20917 If space for the result is malloc'd, *NAME_HOLDER will be set.
20918 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20919
d521ce57 20920static const char *
7ba99d21 20921psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20922 const dwarf2_psymtab *pst,
c89b44cd
TT
20923 const char *comp_dir,
20924 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20925{
d521ce57
TT
20926 const char *include_name = fe.name;
20927 const char *include_name_to_compare = include_name;
72b9f47f 20928 const char *pst_filename;
c6da4cef
DE
20929 int file_is_pst;
20930
8c43009f 20931 const char *dir_name = fe.include_dir (lh);
c6da4cef 20932
c89b44cd 20933 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20934 if (!IS_ABSOLUTE_PATH (include_name)
20935 && (dir_name != NULL || comp_dir != NULL))
20936 {
20937 /* Avoid creating a duplicate psymtab for PST.
20938 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20939 Before we do the comparison, however, we need to account
20940 for DIR_NAME and COMP_DIR.
20941 First prepend dir_name (if non-NULL). If we still don't
20942 have an absolute path prepend comp_dir (if non-NULL).
20943 However, the directory we record in the include-file's
20944 psymtab does not contain COMP_DIR (to match the
20945 corresponding symtab(s)).
20946
20947 Example:
20948
20949 bash$ cd /tmp
20950 bash$ gcc -g ./hello.c
20951 include_name = "hello.c"
20952 dir_name = "."
20953 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20954 DW_AT_name = "./hello.c"
20955
20956 */
c6da4cef
DE
20957
20958 if (dir_name != NULL)
20959 {
c89b44cd
TT
20960 name_holder->reset (concat (dir_name, SLASH_STRING,
20961 include_name, (char *) NULL));
20962 include_name = name_holder->get ();
c6da4cef 20963 include_name_to_compare = include_name;
c6da4cef
DE
20964 }
20965 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20966 {
c89b44cd
TT
20967 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20968 include_name, (char *) NULL));
20969 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20970 }
20971 }
20972
20973 pst_filename = pst->filename;
c89b44cd 20974 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20975 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20976 {
c89b44cd
TT
20977 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20978 pst_filename, (char *) NULL));
20979 pst_filename = copied_name.get ();
c6da4cef
DE
20980 }
20981
1e3fad37 20982 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20983
c6da4cef
DE
20984 if (file_is_pst)
20985 return NULL;
20986 return include_name;
20987}
20988
d9b3de22
DE
20989/* State machine to track the state of the line number program. */
20990
6f77053d 20991class lnp_state_machine
d9b3de22 20992{
6f77053d
PA
20993public:
20994 /* Initialize a machine state for the start of a line number
20995 program. */
804d2729
TT
20996 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20997 bool record_lines_p);
6f77053d 20998
8c43009f
PA
20999 file_entry *current_file ()
21000 {
21001 /* lh->file_names is 0-based, but the file name numbers in the
21002 statement program are 1-based. */
6f77053d
PA
21003 return m_line_header->file_name_at (m_file);
21004 }
21005
21006 /* Record the line in the state machine. END_SEQUENCE is true if
21007 we're processing the end of a sequence. */
21008 void record_line (bool end_sequence);
21009
7ab6656f
OJ
21010 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
21011 nop-out rest of the lines in this sequence. */
6f77053d
PA
21012 void check_line_address (struct dwarf2_cu *cu,
21013 const gdb_byte *line_ptr,
7ab6656f 21014 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21015
21016 void handle_set_discriminator (unsigned int discriminator)
21017 {
21018 m_discriminator = discriminator;
21019 m_line_has_non_zero_discriminator |= discriminator != 0;
21020 }
21021
21022 /* Handle DW_LNE_set_address. */
21023 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21024 {
21025 m_op_index = 0;
21026 address += baseaddr;
21027 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21028 }
21029
21030 /* Handle DW_LNS_advance_pc. */
21031 void handle_advance_pc (CORE_ADDR adjust);
21032
21033 /* Handle a special opcode. */
21034 void handle_special_opcode (unsigned char op_code);
21035
21036 /* Handle DW_LNS_advance_line. */
21037 void handle_advance_line (int line_delta)
21038 {
21039 advance_line (line_delta);
21040 }
21041
21042 /* Handle DW_LNS_set_file. */
21043 void handle_set_file (file_name_index file);
21044
21045 /* Handle DW_LNS_negate_stmt. */
21046 void handle_negate_stmt ()
21047 {
21048 m_is_stmt = !m_is_stmt;
21049 }
21050
21051 /* Handle DW_LNS_const_add_pc. */
21052 void handle_const_add_pc ();
21053
21054 /* Handle DW_LNS_fixed_advance_pc. */
21055 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21056 {
21057 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21058 m_op_index = 0;
21059 }
21060
21061 /* Handle DW_LNS_copy. */
21062 void handle_copy ()
21063 {
21064 record_line (false);
21065 m_discriminator = 0;
21066 }
21067
21068 /* Handle DW_LNE_end_sequence. */
21069 void handle_end_sequence ()
21070 {
804d2729 21071 m_currently_recording_lines = true;
6f77053d
PA
21072 }
21073
21074private:
21075 /* Advance the line by LINE_DELTA. */
21076 void advance_line (int line_delta)
21077 {
21078 m_line += line_delta;
21079
21080 if (line_delta != 0)
21081 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21082 }
21083
804d2729
TT
21084 struct dwarf2_cu *m_cu;
21085
6f77053d
PA
21086 gdbarch *m_gdbarch;
21087
21088 /* True if we're recording lines.
21089 Otherwise we're building partial symtabs and are just interested in
21090 finding include files mentioned by the line number program. */
21091 bool m_record_lines_p;
21092
8c43009f 21093 /* The line number header. */
6f77053d 21094 line_header *m_line_header;
8c43009f 21095
6f77053d
PA
21096 /* These are part of the standard DWARF line number state machine,
21097 and initialized according to the DWARF spec. */
d9b3de22 21098
6f77053d 21099 unsigned char m_op_index = 0;
7ba99d21
AT
21100 /* The line table index of the current file. */
21101 file_name_index m_file = 1;
6f77053d
PA
21102 unsigned int m_line = 1;
21103
21104 /* These are initialized in the constructor. */
21105
21106 CORE_ADDR m_address;
21107 bool m_is_stmt;
21108 unsigned int m_discriminator;
d9b3de22
DE
21109
21110 /* Additional bits of state we need to track. */
21111
21112 /* The last file that we called dwarf2_start_subfile for.
21113 This is only used for TLLs. */
6f77053d 21114 unsigned int m_last_file = 0;
d9b3de22 21115 /* The last file a line number was recorded for. */
6f77053d 21116 struct subfile *m_last_subfile = NULL;
d9b3de22 21117
804d2729
TT
21118 /* When true, record the lines we decode. */
21119 bool m_currently_recording_lines = false;
d9b3de22
DE
21120
21121 /* The last line number that was recorded, used to coalesce
21122 consecutive entries for the same line. This can happen, for
21123 example, when discriminators are present. PR 17276. */
6f77053d
PA
21124 unsigned int m_last_line = 0;
21125 bool m_line_has_non_zero_discriminator = false;
8c43009f 21126};
d9b3de22 21127
6f77053d
PA
21128void
21129lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21130{
21131 CORE_ADDR addr_adj = (((m_op_index + adjust)
21132 / m_line_header->maximum_ops_per_instruction)
21133 * m_line_header->minimum_instruction_length);
21134 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21135 m_op_index = ((m_op_index + adjust)
21136 % m_line_header->maximum_ops_per_instruction);
21137}
d9b3de22 21138
6f77053d
PA
21139void
21140lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21141{
6f77053d
PA
21142 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21143 CORE_ADDR addr_adj = (((m_op_index
21144 + (adj_opcode / m_line_header->line_range))
21145 / m_line_header->maximum_ops_per_instruction)
21146 * m_line_header->minimum_instruction_length);
21147 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21148 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21149 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21150
6f77053d
PA
21151 int line_delta = (m_line_header->line_base
21152 + (adj_opcode % m_line_header->line_range));
21153 advance_line (line_delta);
21154 record_line (false);
21155 m_discriminator = 0;
21156}
d9b3de22 21157
6f77053d
PA
21158void
21159lnp_state_machine::handle_set_file (file_name_index file)
21160{
21161 m_file = file;
21162
21163 const file_entry *fe = current_file ();
21164 if (fe == NULL)
21165 dwarf2_debug_line_missing_file_complaint ();
21166 else if (m_record_lines_p)
21167 {
21168 const char *dir = fe->include_dir (m_line_header);
21169
c24bdb02 21170 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21171 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21172 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21173 }
21174}
21175
21176void
21177lnp_state_machine::handle_const_add_pc ()
21178{
21179 CORE_ADDR adjust
21180 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21181
21182 CORE_ADDR addr_adj
21183 = (((m_op_index + adjust)
21184 / m_line_header->maximum_ops_per_instruction)
21185 * m_line_header->minimum_instruction_length);
21186
21187 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21188 m_op_index = ((m_op_index + adjust)
21189 % m_line_header->maximum_ops_per_instruction);
21190}
d9b3de22 21191
a05a36a5
DE
21192/* Return non-zero if we should add LINE to the line number table.
21193 LINE is the line to add, LAST_LINE is the last line that was added,
21194 LAST_SUBFILE is the subfile for LAST_LINE.
21195 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21196 had a non-zero discriminator.
21197
21198 We have to be careful in the presence of discriminators.
21199 E.g., for this line:
21200
21201 for (i = 0; i < 100000; i++);
21202
21203 clang can emit four line number entries for that one line,
21204 each with a different discriminator.
21205 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21206
21207 However, we want gdb to coalesce all four entries into one.
21208 Otherwise the user could stepi into the middle of the line and
21209 gdb would get confused about whether the pc really was in the
21210 middle of the line.
21211
21212 Things are further complicated by the fact that two consecutive
21213 line number entries for the same line is a heuristic used by gcc
21214 to denote the end of the prologue. So we can't just discard duplicate
21215 entries, we have to be selective about it. The heuristic we use is
21216 that we only collapse consecutive entries for the same line if at least
21217 one of those entries has a non-zero discriminator. PR 17276.
21218
21219 Note: Addresses in the line number state machine can never go backwards
21220 within one sequence, thus this coalescing is ok. */
21221
21222static int
804d2729
TT
21223dwarf_record_line_p (struct dwarf2_cu *cu,
21224 unsigned int line, unsigned int last_line,
a05a36a5
DE
21225 int line_has_non_zero_discriminator,
21226 struct subfile *last_subfile)
21227{
c24bdb02 21228 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21229 return 1;
21230 if (line != last_line)
21231 return 1;
21232 /* Same line for the same file that we've seen already.
21233 As a last check, for pr 17276, only record the line if the line
21234 has never had a non-zero discriminator. */
21235 if (!line_has_non_zero_discriminator)
21236 return 1;
21237 return 0;
21238}
21239
804d2729
TT
21240/* Use the CU's builder to record line number LINE beginning at
21241 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21242
21243static void
d9b3de22
DE
21244dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21245 unsigned int line, CORE_ADDR address,
804d2729 21246 struct dwarf2_cu *cu)
252a6764
DE
21247{
21248 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21249
27e0867f
DE
21250 if (dwarf_line_debug)
21251 {
21252 fprintf_unfiltered (gdb_stdlog,
21253 "Recording line %u, file %s, address %s\n",
21254 line, lbasename (subfile->name),
21255 paddress (gdbarch, address));
21256 }
21257
804d2729 21258 if (cu != nullptr)
c24bdb02 21259 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21260}
21261
21262/* Subroutine of dwarf_decode_lines_1 to simplify it.
21263 Mark the end of a set of line number records.
d9b3de22 21264 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21265 If SUBFILE is NULL the request is ignored. */
21266
21267static void
21268dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21269 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21270{
27e0867f
DE
21271 if (subfile == NULL)
21272 return;
21273
21274 if (dwarf_line_debug)
21275 {
21276 fprintf_unfiltered (gdb_stdlog,
21277 "Finishing current line, file %s, address %s\n",
21278 lbasename (subfile->name),
21279 paddress (gdbarch, address));
21280 }
21281
804d2729 21282 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21283}
21284
6f77053d
PA
21285void
21286lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21287{
d9b3de22
DE
21288 if (dwarf_line_debug)
21289 {
21290 fprintf_unfiltered (gdb_stdlog,
21291 "Processing actual line %u: file %u,"
94a72be7 21292 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21293 m_line, m_file,
6f77053d 21294 paddress (m_gdbarch, m_address),
94a72be7
AB
21295 m_is_stmt, m_discriminator,
21296 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21297 }
21298
6f77053d 21299 file_entry *fe = current_file ();
8c43009f
PA
21300
21301 if (fe == NULL)
d9b3de22
DE
21302 dwarf2_debug_line_missing_file_complaint ();
21303 /* For now we ignore lines not starting on an instruction boundary.
21304 But not when processing end_sequence for compatibility with the
21305 previous version of the code. */
6f77053d 21306 else if (m_op_index == 0 || end_sequence)
d9b3de22 21307 {
8c43009f 21308 fe->included_p = 1;
94a72be7
AB
21309 if (m_record_lines_p
21310 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 21311 {
c24bdb02 21312 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21313 || end_sequence)
d9b3de22 21314 {
804d2729
TT
21315 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21316 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21317 }
21318
21319 if (!end_sequence)
21320 {
804d2729 21321 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21322 m_line_has_non_zero_discriminator,
21323 m_last_subfile))
d9b3de22 21324 {
c24bdb02 21325 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21326 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21327 builder->get_current_subfile (),
6f77053d 21328 m_line, m_address,
804d2729 21329 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21330 }
c24bdb02 21331 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21332 m_last_line = m_line;
d9b3de22
DE
21333 }
21334 }
21335 }
21336}
21337
804d2729
TT
21338lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21339 line_header *lh, bool record_lines_p)
d9b3de22 21340{
804d2729 21341 m_cu = cu;
6f77053d
PA
21342 m_gdbarch = arch;
21343 m_record_lines_p = record_lines_p;
21344 m_line_header = lh;
d9b3de22 21345
804d2729 21346 m_currently_recording_lines = true;
d9b3de22 21347
d9b3de22
DE
21348 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21349 was a line entry for it so that the backend has a chance to adjust it
21350 and also record it in case it needs it. This is currently used by MIPS
21351 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21352 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21353 m_is_stmt = lh->default_is_stmt;
21354 m_discriminator = 0;
252a6764
DE
21355}
21356
6f77053d
PA
21357void
21358lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21359 const gdb_byte *line_ptr,
7ab6656f 21360 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21361{
7ab6656f
OJ
21362 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21363 the pc range of the CU. However, we restrict the test to only ADDRESS
21364 values of zero to preserve GDB's previous behaviour which is to handle
21365 the specific case of a function being GC'd by the linker. */
924c2928 21366
7ab6656f 21367 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21368 {
21369 /* This line table is for a function which has been
21370 GCd by the linker. Ignore it. PR gdb/12528 */
21371
518817b3 21372 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21373 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21374
b98664d3 21375 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21376 line_offset, objfile_name (objfile));
804d2729
TT
21377 m_currently_recording_lines = false;
21378 /* Note: m_currently_recording_lines is left as false until we see
21379 DW_LNE_end_sequence. */
924c2928
DE
21380 }
21381}
21382
f3f5162e 21383/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21384 Process the line number information in LH.
21385 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21386 program in order to set included_p for every referenced header. */
debd256d 21387
c906108c 21388static void
43f3e411
DE
21389dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21390 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21391{
d521ce57
TT
21392 const gdb_byte *line_ptr, *extended_end;
21393 const gdb_byte *line_end;
a8c50c1f 21394 unsigned int bytes_read, extended_len;
699ca60a 21395 unsigned char op_code, extended_op;
e142c38c 21396 CORE_ADDR baseaddr;
518817b3 21397 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21398 bfd *abfd = objfile->obfd;
fbf65064 21399 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21400 /* True if we're recording line info (as opposed to building partial
21401 symtabs and just interested in finding include files mentioned by
21402 the line number program). */
21403 bool record_lines_p = !decode_for_pst_p;
e142c38c 21404
b3b3bada 21405 baseaddr = objfile->text_section_offset ();
c906108c 21406
debd256d
JB
21407 line_ptr = lh->statement_program_start;
21408 line_end = lh->statement_program_end;
c906108c
SS
21409
21410 /* Read the statement sequences until there's nothing left. */
21411 while (line_ptr < line_end)
21412 {
6f77053d
PA
21413 /* The DWARF line number program state machine. Reset the state
21414 machine at the start of each sequence. */
804d2729 21415 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21416 bool end_sequence = false;
d9b3de22 21417
8c43009f 21418 if (record_lines_p)
c906108c 21419 {
8c43009f
PA
21420 /* Start a subfile for the current file of the state
21421 machine. */
21422 const file_entry *fe = state_machine.current_file ();
21423
21424 if (fe != NULL)
804d2729 21425 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21426 }
21427
a738430d 21428 /* Decode the table. */
d9b3de22 21429 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21430 {
21431 op_code = read_1_byte (abfd, line_ptr);
21432 line_ptr += 1;
9aa1fe7e 21433
debd256d 21434 if (op_code >= lh->opcode_base)
6e70227d 21435 {
8e07a239 21436 /* Special opcode. */
6f77053d 21437 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21438 }
21439 else switch (op_code)
c906108c
SS
21440 {
21441 case DW_LNS_extended_op:
3e43a32a
MS
21442 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21443 &bytes_read);
473b7be6 21444 line_ptr += bytes_read;
a8c50c1f 21445 extended_end = line_ptr + extended_len;
c906108c
SS
21446 extended_op = read_1_byte (abfd, line_ptr);
21447 line_ptr += 1;
21448 switch (extended_op)
21449 {
21450 case DW_LNE_end_sequence:
6f77053d
PA
21451 state_machine.handle_end_sequence ();
21452 end_sequence = true;
c906108c
SS
21453 break;
21454 case DW_LNE_set_address:
d9b3de22
DE
21455 {
21456 CORE_ADDR address
21457 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21458 line_ptr += bytes_read;
6f77053d
PA
21459
21460 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21461 lowpc - baseaddr, address);
6f77053d 21462 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21463 }
c906108c
SS
21464 break;
21465 case DW_LNE_define_file:
debd256d 21466 {
d521ce57 21467 const char *cur_file;
ecfb656c
PA
21468 unsigned int mod_time, length;
21469 dir_index dindex;
6e70227d 21470
3e43a32a
MS
21471 cur_file = read_direct_string (abfd, line_ptr,
21472 &bytes_read);
debd256d 21473 line_ptr += bytes_read;
ecfb656c 21474 dindex = (dir_index)
debd256d
JB
21475 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21476 line_ptr += bytes_read;
21477 mod_time =
21478 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21479 line_ptr += bytes_read;
21480 length =
21481 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21482 line_ptr += bytes_read;
ecfb656c 21483 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21484 }
c906108c 21485 break;
d0c6ba3d 21486 case DW_LNE_set_discriminator:
6f77053d
PA
21487 {
21488 /* The discriminator is not interesting to the
21489 debugger; just ignore it. We still need to
21490 check its value though:
21491 if there are consecutive entries for the same
21492 (non-prologue) line we want to coalesce them.
21493 PR 17276. */
21494 unsigned int discr
21495 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21496 line_ptr += bytes_read;
21497
21498 state_machine.handle_set_discriminator (discr);
21499 }
d0c6ba3d 21500 break;
c906108c 21501 default:
b98664d3 21502 complaint (_("mangled .debug_line section"));
debd256d 21503 return;
c906108c 21504 }
a8c50c1f
DJ
21505 /* Make sure that we parsed the extended op correctly. If e.g.
21506 we expected a different address size than the producer used,
21507 we may have read the wrong number of bytes. */
21508 if (line_ptr != extended_end)
21509 {
b98664d3 21510 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21511 return;
21512 }
c906108c
SS
21513 break;
21514 case DW_LNS_copy:
6f77053d 21515 state_machine.handle_copy ();
c906108c
SS
21516 break;
21517 case DW_LNS_advance_pc:
2dc7f7b3
TT
21518 {
21519 CORE_ADDR adjust
21520 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21521 line_ptr += bytes_read;
6f77053d
PA
21522
21523 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21524 }
c906108c
SS
21525 break;
21526 case DW_LNS_advance_line:
a05a36a5
DE
21527 {
21528 int line_delta
21529 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21530 line_ptr += bytes_read;
6f77053d
PA
21531
21532 state_machine.handle_advance_line (line_delta);
a05a36a5 21533 }
c906108c
SS
21534 break;
21535 case DW_LNS_set_file:
d9b3de22 21536 {
6f77053d 21537 file_name_index file
ecfb656c
PA
21538 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21539 &bytes_read);
d9b3de22 21540 line_ptr += bytes_read;
8c43009f 21541
6f77053d 21542 state_machine.handle_set_file (file);
d9b3de22 21543 }
c906108c
SS
21544 break;
21545 case DW_LNS_set_column:
0ad93d4f 21546 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21547 line_ptr += bytes_read;
21548 break;
21549 case DW_LNS_negate_stmt:
6f77053d 21550 state_machine.handle_negate_stmt ();
c906108c
SS
21551 break;
21552 case DW_LNS_set_basic_block:
c906108c 21553 break;
c2c6d25f
JM
21554 /* Add to the address register of the state machine the
21555 address increment value corresponding to special opcode
a738430d
MK
21556 255. I.e., this value is scaled by the minimum
21557 instruction length since special opcode 255 would have
b021a221 21558 scaled the increment. */
c906108c 21559 case DW_LNS_const_add_pc:
6f77053d 21560 state_machine.handle_const_add_pc ();
c906108c
SS
21561 break;
21562 case DW_LNS_fixed_advance_pc:
3e29f34a 21563 {
6f77053d 21564 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21565 line_ptr += 2;
6f77053d
PA
21566
21567 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21568 }
c906108c 21569 break;
9aa1fe7e 21570 default:
a738430d
MK
21571 {
21572 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21573 int i;
a738430d 21574
debd256d 21575 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21576 {
21577 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21578 line_ptr += bytes_read;
21579 }
21580 }
c906108c
SS
21581 }
21582 }
d9b3de22
DE
21583
21584 if (!end_sequence)
21585 dwarf2_debug_line_missing_end_sequence_complaint ();
21586
21587 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21588 in which case we still finish recording the last line). */
6f77053d 21589 state_machine.record_line (true);
c906108c 21590 }
f3f5162e
DE
21591}
21592
21593/* Decode the Line Number Program (LNP) for the given line_header
21594 structure and CU. The actual information extracted and the type
21595 of structures created from the LNP depends on the value of PST.
21596
21597 1. If PST is NULL, then this procedure uses the data from the program
21598 to create all necessary symbol tables, and their linetables.
21599
21600 2. If PST is not NULL, this procedure reads the program to determine
21601 the list of files included by the unit represented by PST, and
21602 builds all the associated partial symbol tables.
21603
21604 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21605 It is used for relative paths in the line table.
21606 NOTE: When processing partial symtabs (pst != NULL),
21607 comp_dir == pst->dirname.
21608
21609 NOTE: It is important that psymtabs have the same file name (via strcmp)
21610 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21611 symtab we don't use it in the name of the psymtabs we create.
21612 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21613 A good testcase for this is mb-inline.exp.
21614
527f3840
JK
21615 LOWPC is the lowest address in CU (or 0 if not known).
21616
21617 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21618 for its PC<->lines mapping information. Otherwise only the filename
21619 table is read in. */
f3f5162e
DE
21620
21621static void
21622dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21623 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21624 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21625{
518817b3 21626 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21627 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21628
527f3840
JK
21629 if (decode_mapping)
21630 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21631
21632 if (decode_for_pst_p)
21633 {
aaa75496
JB
21634 /* Now that we're done scanning the Line Header Program, we can
21635 create the psymtab of each included file. */
7ba99d21
AT
21636 for (auto &file_entry : lh->file_names ())
21637 if (file_entry.included_p == 1)
aaa75496 21638 {
c89b44cd 21639 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21640 const char *include_name =
7ba99d21
AT
21641 psymtab_include_file_name (lh, file_entry, pst,
21642 comp_dir, &name_holder);
c6da4cef 21643 if (include_name != NULL)
aaa75496
JB
21644 dwarf2_create_include_psymtab (include_name, pst, objfile);
21645 }
21646 }
cb1df416
DJ
21647 else
21648 {
21649 /* Make sure a symtab is created for every file, even files
21650 which contain only variables (i.e. no code with associated
21651 line numbers). */
c24bdb02
KS
21652 buildsym_compunit *builder = cu->get_builder ();
21653 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21654
7ba99d21 21655 for (auto &fe : lh->file_names ())
cb1df416 21656 {
804d2729 21657 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21658 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21659 {
c24bdb02 21660 builder->get_current_subfile ()->symtab
804d2729 21661 = allocate_symtab (cust,
c24bdb02 21662 builder->get_current_subfile ()->name);
43f3e411 21663 }
c24bdb02 21664 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21665 }
21666 }
c906108c
SS
21667}
21668
21669/* Start a subfile for DWARF. FILENAME is the name of the file and
21670 DIRNAME the name of the source directory which contains FILENAME
4d663531 21671 or NULL if not known.
c906108c
SS
21672 This routine tries to keep line numbers from identical absolute and
21673 relative file names in a common subfile.
21674
21675 Using the `list' example from the GDB testsuite, which resides in
21676 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21677 of /srcdir/list0.c yields the following debugging information for list0.c:
21678
c5aa993b 21679 DW_AT_name: /srcdir/list0.c
4d663531 21680 DW_AT_comp_dir: /compdir
357e46e7 21681 files.files[0].name: list0.h
c5aa993b 21682 files.files[0].dir: /srcdir
357e46e7 21683 files.files[1].name: list0.c
c5aa993b 21684 files.files[1].dir: /srcdir
c906108c
SS
21685
21686 The line number information for list0.c has to end up in a single
4f1520fb
FR
21687 subfile, so that `break /srcdir/list0.c:1' works as expected.
21688 start_subfile will ensure that this happens provided that we pass the
21689 concatenation of files.files[1].dir and files.files[1].name as the
21690 subfile's name. */
c906108c
SS
21691
21692static void
804d2729
TT
21693dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21694 const char *dirname)
c906108c 21695{
43816ebc 21696 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21697
4d663531 21698 /* In order not to lose the line information directory,
4f1520fb
FR
21699 we concatenate it to the filename when it makes sense.
21700 Note that the Dwarf3 standard says (speaking of filenames in line
21701 information): ``The directory index is ignored for file names
21702 that represent full path names''. Thus ignoring dirname in the
21703 `else' branch below isn't an issue. */
c906108c 21704
d5166ae1 21705 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21706 {
43816ebc
TT
21707 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21708 filename = copy.get ();
d521ce57 21709 }
c906108c 21710
c24bdb02 21711 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21712}
21713
804d2729
TT
21714/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21715 buildsym_compunit constructor. */
f4dc4d17 21716
c24bdb02
KS
21717struct compunit_symtab *
21718dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21719 CORE_ADDR low_pc)
f4dc4d17 21720{
c24bdb02 21721 gdb_assert (m_builder == nullptr);
43f3e411 21722
c24bdb02
KS
21723 m_builder.reset (new struct buildsym_compunit
21724 (per_cu->dwarf2_per_objfile->objfile,
21725 name, comp_dir, language, low_pc));
93b8bea4 21726
c24bdb02 21727 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21728
c24bdb02
KS
21729 get_builder ()->record_debugformat ("DWARF 2");
21730 get_builder ()->record_producer (producer);
f4dc4d17 21731
c24bdb02 21732 processing_has_namespace_info = false;
43f3e411 21733
c24bdb02 21734 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21735}
21736
4c2df51b
DJ
21737static void
21738var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21739 struct dwarf2_cu *cu)
4c2df51b 21740{
518817b3 21741 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21742 struct comp_unit_head *cu_header = &cu->header;
21743
4c2df51b
DJ
21744 /* NOTE drow/2003-01-30: There used to be a comment and some special
21745 code here to turn a symbol with DW_AT_external and a
21746 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21747 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21748 with some versions of binutils) where shared libraries could have
21749 relocations against symbols in their debug information - the
21750 minimal symbol would have the right address, but the debug info
21751 would not. It's no longer necessary, because we will explicitly
21752 apply relocations when we read in the debug information now. */
21753
21754 /* A DW_AT_location attribute with no contents indicates that a
21755 variable has been optimized away. */
21756 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21757 {
f1e6e072 21758 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21759 return;
21760 }
21761
21762 /* Handle one degenerate form of location expression specially, to
21763 preserve GDB's previous behavior when section offsets are
336d760d
AT
21764 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21765 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21766
21767 if (attr_form_is_block (attr)
3019eac3
DE
21768 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21769 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21770 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21771 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21772 && (DW_BLOCK (attr)->size
21773 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21774 {
891d2f0b 21775 unsigned int dummy;
4c2df51b 21776
3019eac3 21777 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21778 SET_SYMBOL_VALUE_ADDRESS (sym,
21779 read_address (objfile->obfd,
21780 DW_BLOCK (attr)->data + 1,
21781 cu, &dummy));
3019eac3 21782 else
38583298
TT
21783 SET_SYMBOL_VALUE_ADDRESS
21784 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21785 &dummy));
f1e6e072 21786 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21787 fixup_symbol_section (sym, objfile);
6a053cb1
TT
21788 SET_SYMBOL_VALUE_ADDRESS
21789 (sym,
21790 SYMBOL_VALUE_ADDRESS (sym)
21791 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
21792 return;
21793 }
21794
21795 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21796 expression evaluator, and use LOC_COMPUTED only when necessary
21797 (i.e. when the value of a register or memory location is
21798 referenced, or a thread-local block, etc.). Then again, it might
21799 not be worthwhile. I'm assuming that it isn't unless performance
21800 or memory numbers show me otherwise. */
21801
f1e6e072 21802 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21803
f1e6e072 21804 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21805 cu->has_loclist = true;
4c2df51b
DJ
21806}
21807
c906108c
SS
21808/* Given a pointer to a DWARF information entry, figure out if we need
21809 to make a symbol table entry for it, and if so, create a new entry
21810 and return a pointer to it.
21811 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21812 used the passed type.
21813 If SPACE is not NULL, use it to hold the new symbol. If it is
21814 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21815
21816static struct symbol *
5e2db402
TT
21817new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21818 struct symbol *space)
c906108c 21819{
518817b3
SM
21820 struct dwarf2_per_objfile *dwarf2_per_objfile
21821 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21822 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21823 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21824 struct symbol *sym = NULL;
15d034d0 21825 const char *name;
c906108c
SS
21826 struct attribute *attr = NULL;
21827 struct attribute *attr2 = NULL;
e142c38c 21828 CORE_ADDR baseaddr;
e37fd15a
SW
21829 struct pending **list_to_add = NULL;
21830
edb3359d 21831 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21832
b3b3bada 21833 baseaddr = objfile->text_section_offset ();
c906108c 21834
94af9270 21835 name = dwarf2_name (die, cu);
c906108c
SS
21836 if (name)
21837 {
94af9270 21838 const char *linkagename;
34eaf542 21839 int suppress_add = 0;
94af9270 21840
34eaf542
TT
21841 if (space)
21842 sym = space;
21843 else
e623cf5d 21844 sym = allocate_symbol (objfile);
c906108c 21845 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21846
21847 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21848 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 21849 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 21850 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 21851
f55ee35c
JK
21852 /* Fortran does not have mangling standard and the mangling does differ
21853 between gfortran, iFort etc. */
21854 if (cu->language == language_fortran
468c0cbb
CB
21855 && symbol_get_demangled_name (sym) == NULL)
21856 symbol_set_demangled_name (sym,
cfc594ee 21857 dwarf2_full_name (name, die, cu),
29df156d 21858 NULL);
f55ee35c 21859
c906108c 21860 /* Default assumptions.
c5aa993b 21861 Use the passed type or decode it from the die. */
176620f1 21862 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21863 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21864 if (type != NULL)
21865 SYMBOL_TYPE (sym) = type;
21866 else
e7c27a73 21867 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21868 attr = dwarf2_attr (die,
21869 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21870 cu);
435d3d88 21871 if (attr != nullptr)
c906108c
SS
21872 {
21873 SYMBOL_LINE (sym) = DW_UNSND (attr);
21874 }
cb1df416 21875
edb3359d
DJ
21876 attr = dwarf2_attr (die,
21877 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21878 cu);
435d3d88 21879 if (attr != nullptr)
cb1df416 21880 {
ecfb656c 21881 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21882 struct file_entry *fe;
9a619af0 21883
ecfb656c
PA
21884 if (cu->line_header != NULL)
21885 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21886 else
21887 fe = NULL;
21888
21889 if (fe == NULL)
b98664d3 21890 complaint (_("file index out of range"));
8c43009f
PA
21891 else
21892 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21893 }
21894
c906108c
SS
21895 switch (die->tag)
21896 {
21897 case DW_TAG_label:
e142c38c 21898 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21899 if (attr != nullptr)
3e29f34a
MR
21900 {
21901 CORE_ADDR addr;
21902
21903 addr = attr_value_as_address (attr);
21904 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21905 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21906 }
0f5238ed
TT
21907 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21908 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21909 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21910 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21911 break;
21912 case DW_TAG_subprogram:
21913 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21914 finish_block. */
f1e6e072 21915 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21916 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21917 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21918 || cu->language == language_ada
21919 || cu->language == language_fortran)
c906108c 21920 {
2cfa0c8d 21921 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21922 Ada and Fortran subprograms, whether marked external or
21923 not, are always stored as a global symbol, because we want
21924 to be able to access them globally. For instance, we want
21925 to be able to break on a nested subprogram without having
21926 to specify the context. */
c24bdb02 21927 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21928 }
21929 else
21930 {
e37fd15a 21931 list_to_add = cu->list_in_scope;
c906108c
SS
21932 }
21933 break;
edb3359d
DJ
21934 case DW_TAG_inlined_subroutine:
21935 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21936 finish_block. */
f1e6e072 21937 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21938 SYMBOL_INLINED (sym) = 1;
481860b3 21939 list_to_add = cu->list_in_scope;
edb3359d 21940 break;
34eaf542
TT
21941 case DW_TAG_template_value_param:
21942 suppress_add = 1;
21943 /* Fall through. */
72929c62 21944 case DW_TAG_constant:
c906108c 21945 case DW_TAG_variable:
254e6b9e 21946 case DW_TAG_member:
0963b4bd
MS
21947 /* Compilation with minimal debug info may result in
21948 variables with missing type entries. Change the
21949 misleading `void' type to something sensible. */
c906108c 21950 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21951 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21952
e142c38c 21953 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21954 /* In the case of DW_TAG_member, we should only be called for
21955 static const members. */
21956 if (die->tag == DW_TAG_member)
21957 {
3863f96c
DE
21958 /* dwarf2_add_field uses die_is_declaration,
21959 so we do the same. */
254e6b9e
DE
21960 gdb_assert (die_is_declaration (die, cu));
21961 gdb_assert (attr);
21962 }
435d3d88 21963 if (attr != nullptr)
c906108c 21964 {
e7c27a73 21965 dwarf2_const_value (attr, sym, cu);
e142c38c 21966 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21967 if (!suppress_add)
34eaf542
TT
21968 {
21969 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21970 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21971 else
e37fd15a 21972 list_to_add = cu->list_in_scope;
34eaf542 21973 }
c906108c
SS
21974 break;
21975 }
e142c38c 21976 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21977 if (attr != nullptr)
c906108c 21978 {
e7c27a73 21979 var_decode_location (attr, sym, cu);
e142c38c 21980 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21981
21982 /* Fortran explicitly imports any global symbols to the local
21983 scope by DW_TAG_common_block. */
21984 if (cu->language == language_fortran && die->parent
21985 && die->parent->tag == DW_TAG_common_block)
21986 attr2 = NULL;
21987
caac4577
JG
21988 if (SYMBOL_CLASS (sym) == LOC_STATIC
21989 && SYMBOL_VALUE_ADDRESS (sym) == 0
21990 && !dwarf2_per_objfile->has_section_at_zero)
21991 {
21992 /* When a static variable is eliminated by the linker,
21993 the corresponding debug information is not stripped
21994 out, but the variable address is set to null;
21995 do not add such variables into symbol table. */
21996 }
21997 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21998 {
4b610737
TT
21999 if (SYMBOL_CLASS (sym) == LOC_STATIC
22000 && (objfile->flags & OBJF_MAINLINE) == 0
22001 && dwarf2_per_objfile->can_copy)
22002 {
22003 /* A global static variable might be subject to
22004 copy relocation. We first check for a local
22005 minsym, though, because maybe the symbol was
22006 marked hidden, in which case this would not
22007 apply. */
22008 bound_minimal_symbol found
22009 = (lookup_minimal_symbol_linkage
987012b8 22010 (sym->linkage_name (), objfile));
4b610737
TT
22011 if (found.minsym != nullptr)
22012 sym->maybe_copied = 1;
22013 }
f55ee35c 22014
1c809c68
TT
22015 /* A variable with DW_AT_external is never static,
22016 but it may be block-scoped. */
804d2729 22017 list_to_add
c24bdb02
KS
22018 = ((cu->list_in_scope
22019 == cu->get_builder ()->get_file_symbols ())
22020 ? cu->get_builder ()->get_global_symbols ()
804d2729 22021 : cu->list_in_scope);
1c809c68 22022 }
c906108c 22023 else
e37fd15a 22024 list_to_add = cu->list_in_scope;
c906108c
SS
22025 }
22026 else
22027 {
22028 /* We do not know the address of this symbol.
c5aa993b
JM
22029 If it is an external symbol and we have type information
22030 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22031 The address of the variable will then be determined from
22032 the minimal symbol table whenever the variable is
22033 referenced. */
e142c38c 22034 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22035
22036 /* Fortran explicitly imports any global symbols to the local
22037 scope by DW_TAG_common_block. */
22038 if (cu->language == language_fortran && die->parent
22039 && die->parent->tag == DW_TAG_common_block)
22040 {
22041 /* SYMBOL_CLASS doesn't matter here because
22042 read_common_block is going to reset it. */
22043 if (!suppress_add)
22044 list_to_add = cu->list_in_scope;
22045 }
22046 else if (attr2 && (DW_UNSND (attr2) != 0)
22047 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22048 {
0fe7935b
DJ
22049 /* A variable with DW_AT_external is never static, but it
22050 may be block-scoped. */
804d2729 22051 list_to_add
c24bdb02
KS
22052 = ((cu->list_in_scope
22053 == cu->get_builder ()->get_file_symbols ())
22054 ? cu->get_builder ()->get_global_symbols ()
804d2729 22055 : cu->list_in_scope);
0fe7935b 22056
f1e6e072 22057 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22058 }
442ddf59
JK
22059 else if (!die_is_declaration (die, cu))
22060 {
22061 /* Use the default LOC_OPTIMIZED_OUT class. */
22062 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22063 if (!suppress_add)
22064 list_to_add = cu->list_in_scope;
442ddf59 22065 }
c906108c
SS
22066 }
22067 break;
22068 case DW_TAG_formal_parameter:
a60f3166
TT
22069 {
22070 /* If we are inside a function, mark this as an argument. If
22071 not, we might be looking at an argument to an inlined function
22072 when we do not have enough information to show inlined frames;
22073 pretend it's a local variable in that case so that the user can
22074 still see it. */
804d2729 22075 struct context_stack *curr
c24bdb02 22076 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22077 if (curr != nullptr && curr->name != nullptr)
22078 SYMBOL_IS_ARGUMENT (sym) = 1;
22079 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22080 if (attr != nullptr)
a60f3166
TT
22081 {
22082 var_decode_location (attr, sym, cu);
22083 }
22084 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22085 if (attr != nullptr)
a60f3166
TT
22086 {
22087 dwarf2_const_value (attr, sym, cu);
22088 }
f346a30d 22089
a60f3166
TT
22090 list_to_add = cu->list_in_scope;
22091 }
c906108c
SS
22092 break;
22093 case DW_TAG_unspecified_parameters:
22094 /* From varargs functions; gdb doesn't seem to have any
22095 interest in this information, so just ignore it for now.
22096 (FIXME?) */
22097 break;
34eaf542
TT
22098 case DW_TAG_template_type_param:
22099 suppress_add = 1;
22100 /* Fall through. */
c906108c 22101 case DW_TAG_class_type:
680b30c7 22102 case DW_TAG_interface_type:
c906108c
SS
22103 case DW_TAG_structure_type:
22104 case DW_TAG_union_type:
72019c9c 22105 case DW_TAG_set_type:
c906108c 22106 case DW_TAG_enumeration_type:
f1e6e072 22107 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22108 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22109
63d06c5c 22110 {
9c37b5ae 22111 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22112 really ever be static objects: otherwise, if you try
22113 to, say, break of a class's method and you're in a file
22114 which doesn't mention that class, it won't work unless
22115 the check for all static symbols in lookup_symbol_aux
22116 saves you. See the OtherFileClass tests in
22117 gdb.c++/namespace.exp. */
22118
e37fd15a 22119 if (!suppress_add)
34eaf542 22120 {
c24bdb02 22121 buildsym_compunit *builder = cu->get_builder ();
804d2729 22122 list_to_add
c24bdb02 22123 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22124 && cu->language == language_cplus
c24bdb02 22125 ? builder->get_global_symbols ()
804d2729 22126 : cu->list_in_scope);
63d06c5c 22127
64382290 22128 /* The semantics of C++ state that "struct foo {
9c37b5ae 22129 ... }" also defines a typedef for "foo". */
64382290 22130 if (cu->language == language_cplus
45280282 22131 || cu->language == language_ada
c44af4eb
TT
22132 || cu->language == language_d
22133 || cu->language == language_rust)
64382290
TT
22134 {
22135 /* The symbol's name is already allocated along
22136 with this objfile, so we don't need to
22137 duplicate it for the type. */
22138 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 22139 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 22140 }
63d06c5c
DC
22141 }
22142 }
c906108c
SS
22143 break;
22144 case DW_TAG_typedef:
f1e6e072 22145 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22146 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22147 list_to_add = cu->list_in_scope;
63d06c5c 22148 break;
c906108c 22149 case DW_TAG_base_type:
a02abb62 22150 case DW_TAG_subrange_type:
f1e6e072 22151 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22152 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22153 list_to_add = cu->list_in_scope;
c906108c
SS
22154 break;
22155 case DW_TAG_enumerator:
e142c38c 22156 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22157 if (attr != nullptr)
c906108c 22158 {
e7c27a73 22159 dwarf2_const_value (attr, sym, cu);
c906108c 22160 }
63d06c5c
DC
22161 {
22162 /* NOTE: carlton/2003-11-10: See comment above in the
22163 DW_TAG_class_type, etc. block. */
22164
804d2729 22165 list_to_add
c24bdb02 22166 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22167 && cu->language == language_cplus
c24bdb02 22168 ? cu->get_builder ()->get_global_symbols ()
804d2729 22169 : cu->list_in_scope);
63d06c5c 22170 }
c906108c 22171 break;
74921315 22172 case DW_TAG_imported_declaration:
5c4e30ca 22173 case DW_TAG_namespace:
f1e6e072 22174 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22175 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22176 break;
530e8392
KB
22177 case DW_TAG_module:
22178 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22179 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22180 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22181 break;
4357ac6c 22182 case DW_TAG_common_block:
f1e6e072 22183 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22184 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22185 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22186 break;
c906108c
SS
22187 default:
22188 /* Not a tag we recognize. Hopefully we aren't processing
22189 trash data, but since we must specifically ignore things
22190 we don't recognize, there is nothing else we should do at
0963b4bd 22191 this point. */
b98664d3 22192 complaint (_("unsupported tag: '%s'"),
4d3c2250 22193 dwarf_tag_name (die->tag));
c906108c
SS
22194 break;
22195 }
df8a16a1 22196
e37fd15a
SW
22197 if (suppress_add)
22198 {
22199 sym->hash_next = objfile->template_symbols;
22200 objfile->template_symbols = sym;
22201 list_to_add = NULL;
22202 }
22203
22204 if (list_to_add != NULL)
d3cb6808 22205 add_symbol_to_list (sym, list_to_add);
e37fd15a 22206
df8a16a1
DJ
22207 /* For the benefit of old versions of GCC, check for anonymous
22208 namespaces based on the demangled name. */
4d4ec4e5 22209 if (!cu->processing_has_namespace_info
94af9270 22210 && cu->language == language_cplus)
c24bdb02 22211 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22212 }
22213 return (sym);
22214}
22215
98bfdba5
PA
22216/* Given an attr with a DW_FORM_dataN value in host byte order,
22217 zero-extend it as appropriate for the symbol's type. The DWARF
22218 standard (v4) is not entirely clear about the meaning of using
22219 DW_FORM_dataN for a constant with a signed type, where the type is
22220 wider than the data. The conclusion of a discussion on the DWARF
22221 list was that this is unspecified. We choose to always zero-extend
22222 because that is the interpretation long in use by GCC. */
c906108c 22223
98bfdba5 22224static gdb_byte *
ff39bb5e 22225dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22226 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22227{
518817b3 22228 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22229 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22230 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22231 LONGEST l = DW_UNSND (attr);
22232
22233 if (bits < sizeof (*value) * 8)
22234 {
22235 l &= ((LONGEST) 1 << bits) - 1;
22236 *value = l;
22237 }
22238 else if (bits == sizeof (*value) * 8)
22239 *value = l;
22240 else
22241 {
224c3ddb 22242 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22243 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22244 return bytes;
22245 }
22246
22247 return NULL;
22248}
22249
22250/* Read a constant value from an attribute. Either set *VALUE, or if
22251 the value does not fit in *VALUE, set *BYTES - either already
22252 allocated on the objfile obstack, or newly allocated on OBSTACK,
22253 or, set *BATON, if we translated the constant to a location
22254 expression. */
22255
22256static void
ff39bb5e 22257dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22258 const char *name, struct obstack *obstack,
22259 struct dwarf2_cu *cu,
d521ce57 22260 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22261 struct dwarf2_locexpr_baton **baton)
22262{
518817b3 22263 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22264 struct comp_unit_head *cu_header = &cu->header;
c906108c 22265 struct dwarf_block *blk;
98bfdba5
PA
22266 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22267 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22268
22269 *value = 0;
22270 *bytes = NULL;
22271 *baton = NULL;
c906108c
SS
22272
22273 switch (attr->form)
22274 {
22275 case DW_FORM_addr:
336d760d 22276 case DW_FORM_addrx:
3019eac3 22277 case DW_FORM_GNU_addr_index:
ac56253d 22278 {
ac56253d
TT
22279 gdb_byte *data;
22280
98bfdba5
PA
22281 if (TYPE_LENGTH (type) != cu_header->addr_size)
22282 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22283 cu_header->addr_size,
98bfdba5 22284 TYPE_LENGTH (type));
ac56253d
TT
22285 /* Symbols of this form are reasonably rare, so we just
22286 piggyback on the existing location code rather than writing
22287 a new implementation of symbol_computed_ops. */
8d749320 22288 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22289 (*baton)->per_cu = cu->per_cu;
22290 gdb_assert ((*baton)->per_cu);
ac56253d 22291
98bfdba5 22292 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22293 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22294 (*baton)->data = data;
ac56253d
TT
22295
22296 data[0] = DW_OP_addr;
22297 store_unsigned_integer (&data[1], cu_header->addr_size,
22298 byte_order, DW_ADDR (attr));
22299 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22300 }
c906108c 22301 break;
4ac36638 22302 case DW_FORM_string:
93b5768b 22303 case DW_FORM_strp:
cf532bd1 22304 case DW_FORM_strx:
3019eac3 22305 case DW_FORM_GNU_str_index:
36586728 22306 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22307 /* DW_STRING is already allocated on the objfile obstack, point
22308 directly to it. */
d521ce57 22309 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22310 break;
c906108c
SS
22311 case DW_FORM_block1:
22312 case DW_FORM_block2:
22313 case DW_FORM_block4:
22314 case DW_FORM_block:
2dc7f7b3 22315 case DW_FORM_exprloc:
0224619f 22316 case DW_FORM_data16:
c906108c 22317 blk = DW_BLOCK (attr);
98bfdba5
PA
22318 if (TYPE_LENGTH (type) != blk->size)
22319 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22320 TYPE_LENGTH (type));
22321 *bytes = blk->data;
c906108c 22322 break;
2df3850c
JM
22323
22324 /* The DW_AT_const_value attributes are supposed to carry the
22325 symbol's value "represented as it would be on the target
22326 architecture." By the time we get here, it's already been
22327 converted to host endianness, so we just need to sign- or
22328 zero-extend it as appropriate. */
22329 case DW_FORM_data1:
3aef2284 22330 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22331 break;
c906108c 22332 case DW_FORM_data2:
3aef2284 22333 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22334 break;
c906108c 22335 case DW_FORM_data4:
3aef2284 22336 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22337 break;
c906108c 22338 case DW_FORM_data8:
3aef2284 22339 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22340 break;
22341
c906108c 22342 case DW_FORM_sdata:
663c44ac 22343 case DW_FORM_implicit_const:
98bfdba5 22344 *value = DW_SND (attr);
2df3850c
JM
22345 break;
22346
c906108c 22347 case DW_FORM_udata:
98bfdba5 22348 *value = DW_UNSND (attr);
c906108c 22349 break;
2df3850c 22350
c906108c 22351 default:
b98664d3 22352 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22353 dwarf_form_name (attr->form));
98bfdba5 22354 *value = 0;
c906108c
SS
22355 break;
22356 }
22357}
22358
2df3850c 22359
98bfdba5
PA
22360/* Copy constant value from an attribute to a symbol. */
22361
2df3850c 22362static void
ff39bb5e 22363dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22364 struct dwarf2_cu *cu)
2df3850c 22365{
518817b3 22366 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22367 LONGEST value;
d521ce57 22368 const gdb_byte *bytes;
98bfdba5 22369 struct dwarf2_locexpr_baton *baton;
2df3850c 22370
98bfdba5 22371 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22372 sym->print_name (),
98bfdba5
PA
22373 &objfile->objfile_obstack, cu,
22374 &value, &bytes, &baton);
2df3850c 22375
98bfdba5
PA
22376 if (baton != NULL)
22377 {
98bfdba5 22378 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22379 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22380 }
22381 else if (bytes != NULL)
22382 {
22383 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22384 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22385 }
22386 else
22387 {
22388 SYMBOL_VALUE (sym) = value;
f1e6e072 22389 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22390 }
2df3850c
JM
22391}
22392
c906108c
SS
22393/* Return the type of the die in question using its DW_AT_type attribute. */
22394
22395static struct type *
e7c27a73 22396die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22397{
c906108c 22398 struct attribute *type_attr;
c906108c 22399
e142c38c 22400 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22401 if (!type_attr)
22402 {
518817b3 22403 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22404 /* A missing DW_AT_type represents a void type. */
518817b3 22405 return objfile_type (objfile)->builtin_void;
c906108c 22406 }
348e048f 22407
673bfd45 22408 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22409}
22410
b4ba55a1
JB
22411/* True iff CU's producer generates GNAT Ada auxiliary information
22412 that allows to find parallel types through that information instead
22413 of having to do expensive parallel lookups by type name. */
22414
22415static int
22416need_gnat_info (struct dwarf2_cu *cu)
22417{
de4cb04a
JB
22418 /* Assume that the Ada compiler was GNAT, which always produces
22419 the auxiliary information. */
22420 return (cu->language == language_ada);
b4ba55a1
JB
22421}
22422
b4ba55a1
JB
22423/* Return the auxiliary type of the die in question using its
22424 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22425 attribute is not present. */
22426
22427static struct type *
22428die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22429{
b4ba55a1 22430 struct attribute *type_attr;
b4ba55a1
JB
22431
22432 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22433 if (!type_attr)
22434 return NULL;
22435
673bfd45 22436 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22437}
22438
22439/* If DIE has a descriptive_type attribute, then set the TYPE's
22440 descriptive type accordingly. */
22441
22442static void
22443set_descriptive_type (struct type *type, struct die_info *die,
22444 struct dwarf2_cu *cu)
22445{
22446 struct type *descriptive_type = die_descriptive_type (die, cu);
22447
22448 if (descriptive_type)
22449 {
22450 ALLOCATE_GNAT_AUX_TYPE (type);
22451 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22452 }
22453}
22454
c906108c
SS
22455/* Return the containing type of the die in question using its
22456 DW_AT_containing_type attribute. */
22457
22458static struct type *
e7c27a73 22459die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22460{
c906108c 22461 struct attribute *type_attr;
518817b3 22462 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22463
e142c38c 22464 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22465 if (!type_attr)
22466 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22467 "[in module %s]"), objfile_name (objfile));
33ac96f0 22468
673bfd45 22469 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22470}
22471
ac9ec31b
DE
22472/* Return an error marker type to use for the ill formed type in DIE/CU. */
22473
22474static struct type *
22475build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22476{
518817b3
SM
22477 struct dwarf2_per_objfile *dwarf2_per_objfile
22478 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22479 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22480 char *saved;
ac9ec31b 22481
528e1572
SM
22482 std::string message
22483 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22484 objfile_name (objfile),
22485 sect_offset_str (cu->header.sect_off),
22486 sect_offset_str (die->sect_off));
efba19b0 22487 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22488
19f392bc 22489 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22490}
22491
673bfd45 22492/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22493 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22494 DW_AT_containing_type.
673bfd45
DE
22495 If there is no type substitute an error marker. */
22496
c906108c 22497static struct type *
ff39bb5e 22498lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22499 struct dwarf2_cu *cu)
c906108c 22500{
518817b3
SM
22501 struct dwarf2_per_objfile *dwarf2_per_objfile
22502 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22503 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22504 struct type *this_type;
22505
ac9ec31b
DE
22506 gdb_assert (attr->name == DW_AT_type
22507 || attr->name == DW_AT_GNAT_descriptive_type
22508 || attr->name == DW_AT_containing_type);
22509
673bfd45
DE
22510 /* First see if we have it cached. */
22511
36586728
TT
22512 if (attr->form == DW_FORM_GNU_ref_alt)
22513 {
22514 struct dwarf2_per_cu_data *per_cu;
9c541725 22515 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22516
ed2dc618
SM
22517 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22518 dwarf2_per_objfile);
9c541725 22519 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22520 }
7771576e 22521 else if (attr_form_is_ref (attr))
673bfd45 22522 {
9c541725 22523 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22524
9c541725 22525 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22526 }
55f1336d 22527 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22528 {
ac9ec31b 22529 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22530
ac9ec31b 22531 return get_signatured_type (die, signature, cu);
673bfd45
DE
22532 }
22533 else
22534 {
b98664d3 22535 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22536 " at %s [in module %s]"),
22537 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22538 objfile_name (objfile));
ac9ec31b 22539 return build_error_marker_type (cu, die);
673bfd45
DE
22540 }
22541
22542 /* If not cached we need to read it in. */
22543
22544 if (this_type == NULL)
22545 {
ac9ec31b 22546 struct die_info *type_die = NULL;
673bfd45
DE
22547 struct dwarf2_cu *type_cu = cu;
22548
7771576e 22549 if (attr_form_is_ref (attr))
ac9ec31b
DE
22550 type_die = follow_die_ref (die, attr, &type_cu);
22551 if (type_die == NULL)
22552 return build_error_marker_type (cu, die);
22553 /* If we find the type now, it's probably because the type came
3019eac3
DE
22554 from an inter-CU reference and the type's CU got expanded before
22555 ours. */
ac9ec31b 22556 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22557 }
22558
22559 /* If we still don't have a type use an error marker. */
22560
22561 if (this_type == NULL)
ac9ec31b 22562 return build_error_marker_type (cu, die);
673bfd45 22563
f792889a 22564 return this_type;
c906108c
SS
22565}
22566
673bfd45
DE
22567/* Return the type in DIE, CU.
22568 Returns NULL for invalid types.
22569
02142a6c 22570 This first does a lookup in die_type_hash,
673bfd45
DE
22571 and only reads the die in if necessary.
22572
22573 NOTE: This can be called when reading in partial or full symbols. */
22574
f792889a 22575static struct type *
e7c27a73 22576read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22577{
f792889a
DJ
22578 struct type *this_type;
22579
22580 this_type = get_die_type (die, cu);
22581 if (this_type)
22582 return this_type;
22583
673bfd45
DE
22584 return read_type_die_1 (die, cu);
22585}
22586
22587/* Read the type in DIE, CU.
22588 Returns NULL for invalid types. */
22589
22590static struct type *
22591read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22592{
22593 struct type *this_type = NULL;
22594
c906108c
SS
22595 switch (die->tag)
22596 {
22597 case DW_TAG_class_type:
680b30c7 22598 case DW_TAG_interface_type:
c906108c
SS
22599 case DW_TAG_structure_type:
22600 case DW_TAG_union_type:
f792889a 22601 this_type = read_structure_type (die, cu);
c906108c
SS
22602 break;
22603 case DW_TAG_enumeration_type:
f792889a 22604 this_type = read_enumeration_type (die, cu);
c906108c
SS
22605 break;
22606 case DW_TAG_subprogram:
22607 case DW_TAG_subroutine_type:
edb3359d 22608 case DW_TAG_inlined_subroutine:
f792889a 22609 this_type = read_subroutine_type (die, cu);
c906108c
SS
22610 break;
22611 case DW_TAG_array_type:
f792889a 22612 this_type = read_array_type (die, cu);
c906108c 22613 break;
72019c9c 22614 case DW_TAG_set_type:
f792889a 22615 this_type = read_set_type (die, cu);
72019c9c 22616 break;
c906108c 22617 case DW_TAG_pointer_type:
f792889a 22618 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22619 break;
22620 case DW_TAG_ptr_to_member_type:
f792889a 22621 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22622 break;
22623 case DW_TAG_reference_type:
4297a3f0
AV
22624 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22625 break;
22626 case DW_TAG_rvalue_reference_type:
22627 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22628 break;
22629 case DW_TAG_const_type:
f792889a 22630 this_type = read_tag_const_type (die, cu);
c906108c
SS
22631 break;
22632 case DW_TAG_volatile_type:
f792889a 22633 this_type = read_tag_volatile_type (die, cu);
c906108c 22634 break;
06d66ee9
TT
22635 case DW_TAG_restrict_type:
22636 this_type = read_tag_restrict_type (die, cu);
22637 break;
c906108c 22638 case DW_TAG_string_type:
f792889a 22639 this_type = read_tag_string_type (die, cu);
c906108c
SS
22640 break;
22641 case DW_TAG_typedef:
f792889a 22642 this_type = read_typedef (die, cu);
c906108c 22643 break;
a02abb62 22644 case DW_TAG_subrange_type:
f792889a 22645 this_type = read_subrange_type (die, cu);
a02abb62 22646 break;
c906108c 22647 case DW_TAG_base_type:
f792889a 22648 this_type = read_base_type (die, cu);
c906108c 22649 break;
81a17f79 22650 case DW_TAG_unspecified_type:
f792889a 22651 this_type = read_unspecified_type (die, cu);
81a17f79 22652 break;
0114d602
DJ
22653 case DW_TAG_namespace:
22654 this_type = read_namespace_type (die, cu);
22655 break;
f55ee35c
JK
22656 case DW_TAG_module:
22657 this_type = read_module_type (die, cu);
22658 break;
a2c2acaf
MW
22659 case DW_TAG_atomic_type:
22660 this_type = read_tag_atomic_type (die, cu);
22661 break;
c906108c 22662 default:
b98664d3 22663 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22664 dwarf_tag_name (die->tag));
c906108c
SS
22665 break;
22666 }
63d06c5c 22667
f792889a 22668 return this_type;
63d06c5c
DC
22669}
22670
abc72ce4
DE
22671/* See if we can figure out if the class lives in a namespace. We do
22672 this by looking for a member function; its demangled name will
22673 contain namespace info, if there is any.
22674 Return the computed name or NULL.
22675 Space for the result is allocated on the objfile's obstack.
22676 This is the full-die version of guess_partial_die_structure_name.
22677 In this case we know DIE has no useful parent. */
22678
43816ebc 22679static const char *
abc72ce4
DE
22680guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22681{
22682 struct die_info *spec_die;
22683 struct dwarf2_cu *spec_cu;
22684 struct die_info *child;
518817b3 22685 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22686
22687 spec_cu = cu;
22688 spec_die = die_specification (die, &spec_cu);
22689 if (spec_die != NULL)
22690 {
22691 die = spec_die;
22692 cu = spec_cu;
22693 }
22694
22695 for (child = die->child;
22696 child != NULL;
22697 child = child->sibling)
22698 {
22699 if (child->tag == DW_TAG_subprogram)
22700 {
73b9be8b 22701 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22702
7d45c7c3 22703 if (linkage_name != NULL)
abc72ce4 22704 {
43816ebc
TT
22705 gdb::unique_xmalloc_ptr<char> actual_name
22706 (language_class_name_from_physname (cu->language_defn,
22707 linkage_name));
22708 const char *name = NULL;
abc72ce4
DE
22709
22710 if (actual_name != NULL)
22711 {
15d034d0 22712 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22713
22714 if (die_name != NULL
43816ebc 22715 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22716 {
22717 /* Strip off the class name from the full name.
22718 We want the prefix. */
22719 int die_name_len = strlen (die_name);
43816ebc
TT
22720 int actual_name_len = strlen (actual_name.get ());
22721 const char *ptr = actual_name.get ();
abc72ce4
DE
22722
22723 /* Test for '::' as a sanity check. */
22724 if (actual_name_len > die_name_len + 2
43816ebc 22725 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22726 name = obstack_strndup (
e3b94546 22727 &objfile->per_bfd->storage_obstack,
43816ebc 22728 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22729 }
22730 }
abc72ce4
DE
22731 return name;
22732 }
22733 }
22734 }
22735
22736 return NULL;
22737}
22738
96408a79
SA
22739/* GCC might emit a nameless typedef that has a linkage name. Determine the
22740 prefix part in such case. See
22741 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22742
a121b7c1 22743static const char *
96408a79
SA
22744anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22745{
22746 struct attribute *attr;
e6a959d6 22747 const char *base;
96408a79
SA
22748
22749 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22750 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22751 return NULL;
22752
7d45c7c3 22753 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22754 return NULL;
22755
73b9be8b 22756 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22757 if (attr == NULL || DW_STRING (attr) == NULL)
22758 return NULL;
22759
22760 /* dwarf2_name had to be already called. */
22761 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22762
22763 /* Strip the base name, keep any leading namespaces/classes. */
22764 base = strrchr (DW_STRING (attr), ':');
22765 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22766 return "";
22767
518817b3 22768 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22769 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22770 DW_STRING (attr),
22771 &base[-1] - DW_STRING (attr));
96408a79
SA
22772}
22773
fdde2d81 22774/* Return the name of the namespace/class that DIE is defined within,
0114d602 22775 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22776
0114d602
DJ
22777 For example, if we're within the method foo() in the following
22778 code:
22779
22780 namespace N {
22781 class C {
22782 void foo () {
22783 }
22784 };
22785 }
22786
22787 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22788
0d5cff50 22789static const char *
e142c38c 22790determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22791{
518817b3
SM
22792 struct dwarf2_per_objfile *dwarf2_per_objfile
22793 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22794 struct die_info *parent, *spec_die;
22795 struct dwarf2_cu *spec_cu;
22796 struct type *parent_type;
a121b7c1 22797 const char *retval;
63d06c5c 22798
9c37b5ae 22799 if (cu->language != language_cplus
c44af4eb
TT
22800 && cu->language != language_fortran && cu->language != language_d
22801 && cu->language != language_rust)
0114d602
DJ
22802 return "";
22803
96408a79
SA
22804 retval = anonymous_struct_prefix (die, cu);
22805 if (retval)
22806 return retval;
22807
0114d602
DJ
22808 /* We have to be careful in the presence of DW_AT_specification.
22809 For example, with GCC 3.4, given the code
22810
22811 namespace N {
22812 void foo() {
22813 // Definition of N::foo.
22814 }
22815 }
22816
22817 then we'll have a tree of DIEs like this:
22818
22819 1: DW_TAG_compile_unit
22820 2: DW_TAG_namespace // N
22821 3: DW_TAG_subprogram // declaration of N::foo
22822 4: DW_TAG_subprogram // definition of N::foo
22823 DW_AT_specification // refers to die #3
22824
22825 Thus, when processing die #4, we have to pretend that we're in
22826 the context of its DW_AT_specification, namely the contex of die
22827 #3. */
22828 spec_cu = cu;
22829 spec_die = die_specification (die, &spec_cu);
22830 if (spec_die == NULL)
22831 parent = die->parent;
22832 else
63d06c5c 22833 {
0114d602
DJ
22834 parent = spec_die->parent;
22835 cu = spec_cu;
63d06c5c 22836 }
0114d602
DJ
22837
22838 if (parent == NULL)
22839 return "";
98bfdba5
PA
22840 else if (parent->building_fullname)
22841 {
22842 const char *name;
22843 const char *parent_name;
22844
22845 /* It has been seen on RealView 2.2 built binaries,
22846 DW_TAG_template_type_param types actually _defined_ as
22847 children of the parent class:
22848
22849 enum E {};
22850 template class <class Enum> Class{};
22851 Class<enum E> class_e;
22852
22853 1: DW_TAG_class_type (Class)
22854 2: DW_TAG_enumeration_type (E)
22855 3: DW_TAG_enumerator (enum1:0)
22856 3: DW_TAG_enumerator (enum2:1)
22857 ...
22858 2: DW_TAG_template_type_param
22859 DW_AT_type DW_FORM_ref_udata (E)
22860
22861 Besides being broken debug info, it can put GDB into an
22862 infinite loop. Consider:
22863
22864 When we're building the full name for Class<E>, we'll start
22865 at Class, and go look over its template type parameters,
22866 finding E. We'll then try to build the full name of E, and
22867 reach here. We're now trying to build the full name of E,
22868 and look over the parent DIE for containing scope. In the
22869 broken case, if we followed the parent DIE of E, we'd again
22870 find Class, and once again go look at its template type
22871 arguments, etc., etc. Simply don't consider such parent die
22872 as source-level parent of this die (it can't be, the language
22873 doesn't allow it), and break the loop here. */
22874 name = dwarf2_name (die, cu);
22875 parent_name = dwarf2_name (parent, cu);
b98664d3 22876 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22877 name ? name : "<unknown>",
22878 parent_name ? parent_name : "<unknown>");
22879 return "";
22880 }
63d06c5c 22881 else
0114d602
DJ
22882 switch (parent->tag)
22883 {
63d06c5c 22884 case DW_TAG_namespace:
0114d602 22885 parent_type = read_type_die (parent, cu);
acebe513
UW
22886 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22887 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22888 Work around this problem here. */
22889 if (cu->language == language_cplus
e86ca25f 22890 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22891 return "";
0114d602 22892 /* We give a name to even anonymous namespaces. */
e86ca25f 22893 return TYPE_NAME (parent_type);
63d06c5c 22894 case DW_TAG_class_type:
680b30c7 22895 case DW_TAG_interface_type:
63d06c5c 22896 case DW_TAG_structure_type:
0114d602 22897 case DW_TAG_union_type:
f55ee35c 22898 case DW_TAG_module:
0114d602 22899 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22900 if (TYPE_NAME (parent_type) != NULL)
22901 return TYPE_NAME (parent_type);
0114d602
DJ
22902 else
22903 /* An anonymous structure is only allowed non-static data
22904 members; no typedefs, no member functions, et cetera.
22905 So it does not need a prefix. */
22906 return "";
abc72ce4 22907 case DW_TAG_compile_unit:
95554aad 22908 case DW_TAG_partial_unit:
abc72ce4
DE
22909 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22910 if (cu->language == language_cplus
fd5866f6 22911 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22912 && die->child != NULL
22913 && (die->tag == DW_TAG_class_type
22914 || die->tag == DW_TAG_structure_type
22915 || die->tag == DW_TAG_union_type))
22916 {
43816ebc 22917 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22918 if (name != NULL)
22919 return name;
22920 }
22921 return "";
0a4b0913
AB
22922 case DW_TAG_subprogram:
22923 /* Nested subroutines in Fortran get a prefix with the name
22924 of the parent's subroutine. */
22925 if (cu->language == language_fortran)
22926 {
22927 if ((die->tag == DW_TAG_subprogram)
22928 && (dwarf2_name (parent, cu) != NULL))
22929 return dwarf2_name (parent, cu);
22930 }
22931 return determine_prefix (parent, cu);
3d567982
TT
22932 case DW_TAG_enumeration_type:
22933 parent_type = read_type_die (parent, cu);
22934 if (TYPE_DECLARED_CLASS (parent_type))
22935 {
e86ca25f
TT
22936 if (TYPE_NAME (parent_type) != NULL)
22937 return TYPE_NAME (parent_type);
3d567982
TT
22938 return "";
22939 }
22940 /* Fall through. */
63d06c5c 22941 default:
8176b9b8 22942 return determine_prefix (parent, cu);
63d06c5c 22943 }
63d06c5c
DC
22944}
22945
3e43a32a
MS
22946/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22947 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22948 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22949 an obconcat, otherwise allocate storage for the result. The CU argument is
22950 used to determine the language and hence, the appropriate separator. */
987504bb 22951
f55ee35c 22952#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22953
22954static char *
f55ee35c
JK
22955typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22956 int physname, struct dwarf2_cu *cu)
63d06c5c 22957{
f55ee35c 22958 const char *lead = "";
5c315b68 22959 const char *sep;
63d06c5c 22960
3e43a32a
MS
22961 if (suffix == NULL || suffix[0] == '\0'
22962 || prefix == NULL || prefix[0] == '\0')
987504bb 22963 sep = "";
45280282
IB
22964 else if (cu->language == language_d)
22965 {
22966 /* For D, the 'main' function could be defined in any module, but it
22967 should never be prefixed. */
22968 if (strcmp (suffix, "D main") == 0)
22969 {
22970 prefix = "";
22971 sep = "";
22972 }
22973 else
22974 sep = ".";
22975 }
f55ee35c
JK
22976 else if (cu->language == language_fortran && physname)
22977 {
22978 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22979 DW_AT_MIPS_linkage_name is preferred and used instead. */
22980
22981 lead = "__";
22982 sep = "_MOD_";
22983 }
987504bb
JJ
22984 else
22985 sep = "::";
63d06c5c 22986
6dd47d34
DE
22987 if (prefix == NULL)
22988 prefix = "";
22989 if (suffix == NULL)
22990 suffix = "";
22991
987504bb
JJ
22992 if (obs == NULL)
22993 {
3e43a32a 22994 char *retval
224c3ddb
SM
22995 = ((char *)
22996 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22997
f55ee35c
JK
22998 strcpy (retval, lead);
22999 strcat (retval, prefix);
6dd47d34
DE
23000 strcat (retval, sep);
23001 strcat (retval, suffix);
63d06c5c
DC
23002 return retval;
23003 }
987504bb
JJ
23004 else
23005 {
23006 /* We have an obstack. */
f55ee35c 23007 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23008 }
63d06c5c
DC
23009}
23010
c906108c
SS
23011/* Return sibling of die, NULL if no sibling. */
23012
f9aca02d 23013static struct die_info *
fba45db2 23014sibling_die (struct die_info *die)
c906108c 23015{
639d11d3 23016 return die->sibling;
c906108c
SS
23017}
23018
71c25dea
TT
23019/* Get name of a die, return NULL if not found. */
23020
15d034d0
TT
23021static const char *
23022dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
23023 struct obstack *obstack)
23024{
23025 if (name && cu->language == language_cplus)
23026 {
2f408ecb 23027 std::string canon_name = cp_canonicalize_string (name);
71c25dea 23028
2f408ecb 23029 if (!canon_name.empty ())
71c25dea 23030 {
2f408ecb 23031 if (canon_name != name)
efba19b0 23032 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
23033 }
23034 }
23035
23036 return name;
c906108c
SS
23037}
23038
96553a0c
DE
23039/* Get name of a die, return NULL if not found.
23040 Anonymous namespaces are converted to their magic string. */
9219021c 23041
15d034d0 23042static const char *
e142c38c 23043dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23044{
23045 struct attribute *attr;
518817b3 23046 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 23047
e142c38c 23048 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 23049 if ((!attr || !DW_STRING (attr))
96553a0c 23050 && die->tag != DW_TAG_namespace
53832f31
TT
23051 && die->tag != DW_TAG_class_type
23052 && die->tag != DW_TAG_interface_type
23053 && die->tag != DW_TAG_structure_type
23054 && die->tag != DW_TAG_union_type)
71c25dea
TT
23055 return NULL;
23056
23057 switch (die->tag)
23058 {
23059 case DW_TAG_compile_unit:
95554aad 23060 case DW_TAG_partial_unit:
71c25dea
TT
23061 /* Compilation units have a DW_AT_name that is a filename, not
23062 a source language identifier. */
23063 case DW_TAG_enumeration_type:
23064 case DW_TAG_enumerator:
23065 /* These tags always have simple identifiers already; no need
23066 to canonicalize them. */
23067 return DW_STRING (attr);
907af001 23068
96553a0c
DE
23069 case DW_TAG_namespace:
23070 if (attr != NULL && DW_STRING (attr) != NULL)
23071 return DW_STRING (attr);
23072 return CP_ANONYMOUS_NAMESPACE_STR;
23073
907af001
UW
23074 case DW_TAG_class_type:
23075 case DW_TAG_interface_type:
23076 case DW_TAG_structure_type:
23077 case DW_TAG_union_type:
23078 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23079 structures or unions. These were of the form "._%d" in GCC 4.1,
23080 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23081 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 23082 if (attr && DW_STRING (attr)
61012eef
GB
23083 && (startswith (DW_STRING (attr), "._")
23084 || startswith (DW_STRING (attr), "<anonymous")))
907af001 23085 return NULL;
53832f31
TT
23086
23087 /* GCC might emit a nameless typedef that has a linkage name. See
23088 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23089 if (!attr || DW_STRING (attr) == NULL)
23090 {
73b9be8b 23091 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
23092 if (attr == NULL || DW_STRING (attr) == NULL)
23093 return NULL;
23094
df5c6c50
JK
23095 /* Avoid demangling DW_STRING (attr) the second time on a second
23096 call for the same DIE. */
23097 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 23098 {
43816ebc
TT
23099 gdb::unique_xmalloc_ptr<char> demangled
23100 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23101
e6a959d6 23102 const char *base;
96408a79 23103
53832f31 23104 /* FIXME: we already did this for the partial symbol... */
34a68019 23105 DW_STRING (attr)
021887d8 23106 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 23107 demangled.get ());
53832f31 23108 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
23109
23110 /* Strip any leading namespaces/classes, keep only the base name.
23111 DW_AT_name for named DIEs does not contain the prefixes. */
23112 base = strrchr (DW_STRING (attr), ':');
23113 if (base && base > DW_STRING (attr) && base[-1] == ':')
23114 return &base[1];
23115 else
23116 return DW_STRING (attr);
53832f31
TT
23117 }
23118 }
907af001
UW
23119 break;
23120
71c25dea 23121 default:
907af001
UW
23122 break;
23123 }
23124
23125 if (!DW_STRING_IS_CANONICAL (attr))
23126 {
23127 DW_STRING (attr)
23128 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 23129 &objfile->per_bfd->storage_obstack);
907af001 23130 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 23131 }
907af001 23132 return DW_STRING (attr);
9219021c
DC
23133}
23134
23135/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23136 is none. *EXT_CU is the CU containing DIE on input, and the CU
23137 containing the return value on output. */
9219021c
DC
23138
23139static struct die_info *
f2f0e013 23140dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23141{
23142 struct attribute *attr;
9219021c 23143
f2f0e013 23144 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23145 if (attr == NULL)
23146 return NULL;
23147
f2f0e013 23148 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23149}
23150
fa9c3fa0
TT
23151/* A convenience function that returns an "unknown" DWARF name,
23152 including the value of V. STR is the name of the entity being
23153 printed, e.g., "TAG". */
23154
23155static const char *
23156dwarf_unknown (const char *str, unsigned v)
23157{
23158 char *cell = get_print_cell ();
23159 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23160 return cell;
23161}
23162
c906108c
SS
23163/* Convert a DIE tag into its string name. */
23164
f39c6ffd 23165static const char *
aa1ee363 23166dwarf_tag_name (unsigned tag)
c906108c 23167{
f39c6ffd
TT
23168 const char *name = get_DW_TAG_name (tag);
23169
23170 if (name == NULL)
fa9c3fa0 23171 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
23172
23173 return name;
c906108c
SS
23174}
23175
23176/* Convert a DWARF attribute code into its string name. */
23177
f39c6ffd 23178static const char *
aa1ee363 23179dwarf_attr_name (unsigned attr)
c906108c 23180{
f39c6ffd
TT
23181 const char *name;
23182
c764a876 23183#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
23184 if (attr == DW_AT_MIPS_fde)
23185 return "DW_AT_MIPS_fde";
23186#else
23187 if (attr == DW_AT_HP_block_index)
23188 return "DW_AT_HP_block_index";
c764a876 23189#endif
f39c6ffd
TT
23190
23191 name = get_DW_AT_name (attr);
23192
23193 if (name == NULL)
fa9c3fa0 23194 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
23195
23196 return name;
c906108c
SS
23197}
23198
a084a2a6
AT
23199/* Convert a unit type to corresponding DW_UT name. */
23200
23201static const char *
23202dwarf_unit_type_name (int unit_type) {
23203 switch (unit_type)
23204 {
23205 case 0x01:
23206 return "DW_UT_compile (0x01)";
23207 case 0x02:
23208 return "DW_UT_type (0x02)";
23209 case 0x03:
23210 return "DW_UT_partial (0x03)";
23211 case 0x04:
23212 return "DW_UT_skeleton (0x04)";
23213 case 0x05:
23214 return "DW_UT_split_compile (0x05)";
23215 case 0x06:
23216 return "DW_UT_split_type (0x06)";
23217 case 0x80:
23218 return "DW_UT_lo_user (0x80)";
23219 case 0xff:
23220 return "DW_UT_hi_user (0xff)";
23221 default:
23222 return nullptr;
23223 }
23224}
23225
c906108c
SS
23226/* Convert a DWARF value form code into its string name. */
23227
f39c6ffd 23228static const char *
aa1ee363 23229dwarf_form_name (unsigned form)
c906108c 23230{
f39c6ffd
TT
23231 const char *name = get_DW_FORM_name (form);
23232
23233 if (name == NULL)
fa9c3fa0 23234 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
23235
23236 return name;
c906108c
SS
23237}
23238
a121b7c1 23239static const char *
fba45db2 23240dwarf_bool_name (unsigned mybool)
c906108c
SS
23241{
23242 if (mybool)
23243 return "TRUE";
23244 else
23245 return "FALSE";
23246}
23247
23248/* Convert a DWARF type code into its string name. */
23249
f39c6ffd 23250static const char *
aa1ee363 23251dwarf_type_encoding_name (unsigned enc)
c906108c 23252{
f39c6ffd 23253 const char *name = get_DW_ATE_name (enc);
c906108c 23254
f39c6ffd 23255 if (name == NULL)
fa9c3fa0 23256 return dwarf_unknown ("ATE", enc);
c906108c 23257
f39c6ffd 23258 return name;
c906108c 23259}
c906108c 23260
f9aca02d 23261static void
d97bc12b 23262dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23263{
23264 unsigned int i;
23265
d97bc12b 23266 print_spaces (indent, f);
9d8780f0 23267 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23268 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23269 sect_offset_str (die->sect_off));
d97bc12b
DE
23270
23271 if (die->parent != NULL)
23272 {
23273 print_spaces (indent, f);
9d8780f0
SM
23274 fprintf_unfiltered (f, " parent at offset: %s\n",
23275 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23276 }
23277
23278 print_spaces (indent, f);
23279 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23280 dwarf_bool_name (die->child != NULL));
c906108c 23281
d97bc12b
DE
23282 print_spaces (indent, f);
23283 fprintf_unfiltered (f, " attributes:\n");
23284
c906108c
SS
23285 for (i = 0; i < die->num_attrs; ++i)
23286 {
d97bc12b
DE
23287 print_spaces (indent, f);
23288 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23289 dwarf_attr_name (die->attrs[i].name),
23290 dwarf_form_name (die->attrs[i].form));
d97bc12b 23291
c906108c
SS
23292 switch (die->attrs[i].form)
23293 {
c906108c 23294 case DW_FORM_addr:
336d760d 23295 case DW_FORM_addrx:
3019eac3 23296 case DW_FORM_GNU_addr_index:
d97bc12b 23297 fprintf_unfiltered (f, "address: ");
5af949e3 23298 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23299 break;
23300 case DW_FORM_block2:
23301 case DW_FORM_block4:
23302 case DW_FORM_block:
23303 case DW_FORM_block1:
56eb65bd
SP
23304 fprintf_unfiltered (f, "block: size %s",
23305 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23306 break;
2dc7f7b3 23307 case DW_FORM_exprloc:
56eb65bd
SP
23308 fprintf_unfiltered (f, "expression: size %s",
23309 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23310 break;
0224619f
JK
23311 case DW_FORM_data16:
23312 fprintf_unfiltered (f, "constant of 16 bytes");
23313 break;
4568ecf9
DE
23314 case DW_FORM_ref_addr:
23315 fprintf_unfiltered (f, "ref address: ");
23316 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23317 break;
36586728
TT
23318 case DW_FORM_GNU_ref_alt:
23319 fprintf_unfiltered (f, "alt ref address: ");
23320 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23321 break;
10b3939b
DJ
23322 case DW_FORM_ref1:
23323 case DW_FORM_ref2:
23324 case DW_FORM_ref4:
4568ecf9
DE
23325 case DW_FORM_ref8:
23326 case DW_FORM_ref_udata:
d97bc12b 23327 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23328 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23329 break;
c906108c
SS
23330 case DW_FORM_data1:
23331 case DW_FORM_data2:
23332 case DW_FORM_data4:
ce5d95e1 23333 case DW_FORM_data8:
c906108c
SS
23334 case DW_FORM_udata:
23335 case DW_FORM_sdata:
43bbcdc2
PH
23336 fprintf_unfiltered (f, "constant: %s",
23337 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23338 break;
2dc7f7b3
TT
23339 case DW_FORM_sec_offset:
23340 fprintf_unfiltered (f, "section offset: %s",
23341 pulongest (DW_UNSND (&die->attrs[i])));
23342 break;
55f1336d 23343 case DW_FORM_ref_sig8:
ac9ec31b
DE
23344 fprintf_unfiltered (f, "signature: %s",
23345 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23346 break;
c906108c 23347 case DW_FORM_string:
4bdf3d34 23348 case DW_FORM_strp:
43988095 23349 case DW_FORM_line_strp:
cf532bd1 23350 case DW_FORM_strx:
3019eac3 23351 case DW_FORM_GNU_str_index:
36586728 23352 case DW_FORM_GNU_strp_alt:
8285870a 23353 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23354 DW_STRING (&die->attrs[i])
8285870a
JK
23355 ? DW_STRING (&die->attrs[i]) : "",
23356 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23357 break;
23358 case DW_FORM_flag:
23359 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23360 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23361 else
d97bc12b 23362 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23363 break;
2dc7f7b3
TT
23364 case DW_FORM_flag_present:
23365 fprintf_unfiltered (f, "flag: TRUE");
23366 break;
a8329558 23367 case DW_FORM_indirect:
0963b4bd
MS
23368 /* The reader will have reduced the indirect form to
23369 the "base form" so this form should not occur. */
5f48f8f3 23370 fprintf_unfiltered (f,
3e43a32a 23371 "unexpected attribute form: DW_FORM_indirect");
a8329558 23372 break;
663c44ac
JK
23373 case DW_FORM_implicit_const:
23374 fprintf_unfiltered (f, "constant: %s",
23375 plongest (DW_SND (&die->attrs[i])));
23376 break;
c906108c 23377 default:
d97bc12b 23378 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23379 die->attrs[i].form);
d97bc12b 23380 break;
c906108c 23381 }
d97bc12b 23382 fprintf_unfiltered (f, "\n");
c906108c
SS
23383 }
23384}
23385
f9aca02d 23386static void
d97bc12b 23387dump_die_for_error (struct die_info *die)
c906108c 23388{
d97bc12b
DE
23389 dump_die_shallow (gdb_stderr, 0, die);
23390}
23391
23392static void
23393dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23394{
23395 int indent = level * 4;
23396
23397 gdb_assert (die != NULL);
23398
23399 if (level >= max_level)
23400 return;
23401
23402 dump_die_shallow (f, indent, die);
23403
23404 if (die->child != NULL)
c906108c 23405 {
d97bc12b
DE
23406 print_spaces (indent, f);
23407 fprintf_unfiltered (f, " Children:");
23408 if (level + 1 < max_level)
23409 {
23410 fprintf_unfiltered (f, "\n");
23411 dump_die_1 (f, level + 1, max_level, die->child);
23412 }
23413 else
23414 {
3e43a32a
MS
23415 fprintf_unfiltered (f,
23416 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23417 }
23418 }
23419
23420 if (die->sibling != NULL && level > 0)
23421 {
23422 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23423 }
23424}
23425
d97bc12b
DE
23426/* This is called from the pdie macro in gdbinit.in.
23427 It's not static so gcc will keep a copy callable from gdb. */
23428
23429void
23430dump_die (struct die_info *die, int max_level)
23431{
23432 dump_die_1 (gdb_stdlog, 0, max_level, die);
23433}
23434
f9aca02d 23435static void
51545339 23436store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23437{
51545339 23438 void **slot;
c906108c 23439
9c541725
PA
23440 slot = htab_find_slot_with_hash (cu->die_hash, die,
23441 to_underlying (die->sect_off),
b64f50a1 23442 INSERT);
51545339
DJ
23443
23444 *slot = die;
c906108c
SS
23445}
23446
b64f50a1
JK
23447/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23448 required kind. */
23449
23450static sect_offset
ff39bb5e 23451dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23452{
7771576e 23453 if (attr_form_is_ref (attr))
9c541725 23454 return (sect_offset) DW_UNSND (attr);
93311388 23455
b98664d3 23456 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23457 dwarf_form_name (attr->form));
9c541725 23458 return {};
c906108c
SS
23459}
23460
43bbcdc2
PH
23461/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23462 * the value held by the attribute is not constant. */
a02abb62 23463
43bbcdc2 23464static LONGEST
ff39bb5e 23465dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23466{
663c44ac 23467 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23468 return DW_SND (attr);
23469 else if (attr->form == DW_FORM_udata
23470 || attr->form == DW_FORM_data1
23471 || attr->form == DW_FORM_data2
23472 || attr->form == DW_FORM_data4
23473 || attr->form == DW_FORM_data8)
23474 return DW_UNSND (attr);
23475 else
23476 {
0224619f 23477 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23478 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23479 dwarf_form_name (attr->form));
23480 return default_value;
23481 }
23482}
23483
348e048f
DE
23484/* Follow reference or signature attribute ATTR of SRC_DIE.
23485 On entry *REF_CU is the CU of SRC_DIE.
23486 On exit *REF_CU is the CU of the result. */
23487
23488static struct die_info *
ff39bb5e 23489follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23490 struct dwarf2_cu **ref_cu)
23491{
23492 struct die_info *die;
23493
7771576e 23494 if (attr_form_is_ref (attr))
348e048f 23495 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23496 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23497 die = follow_die_sig (src_die, attr, ref_cu);
23498 else
23499 {
23500 dump_die_for_error (src_die);
23501 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23502 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23503 }
23504
23505 return die;
03dd20cc
DJ
23506}
23507
5c631832 23508/* Follow reference OFFSET.
673bfd45
DE
23509 On entry *REF_CU is the CU of the source die referencing OFFSET.
23510 On exit *REF_CU is the CU of the result.
23511 Returns NULL if OFFSET is invalid. */
f504f079 23512
f9aca02d 23513static struct die_info *
9c541725 23514follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23515 struct dwarf2_cu **ref_cu)
c906108c 23516{
10b3939b 23517 struct die_info temp_die;
f2f0e013 23518 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23519 struct dwarf2_per_objfile *dwarf2_per_objfile
23520 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23521
348e048f
DE
23522 gdb_assert (cu->per_cu != NULL);
23523
98bfdba5
PA
23524 target_cu = cu;
23525
3019eac3 23526 if (cu->per_cu->is_debug_types)
348e048f
DE
23527 {
23528 /* .debug_types CUs cannot reference anything outside their CU.
23529 If they need to, they have to reference a signatured type via
55f1336d 23530 DW_FORM_ref_sig8. */
9c541725 23531 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23532 return NULL;
348e048f 23533 }
36586728 23534 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23535 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23536 {
23537 struct dwarf2_per_cu_data *per_cu;
9a619af0 23538
9c541725 23539 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23540 dwarf2_per_objfile);
03dd20cc
DJ
23541
23542 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23543 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23544 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23545
10b3939b
DJ
23546 target_cu = per_cu->cu;
23547 }
98bfdba5
PA
23548 else if (cu->dies == NULL)
23549 {
23550 /* We're loading full DIEs during partial symbol reading. */
23551 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23552 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23553 }
c906108c 23554
f2f0e013 23555 *ref_cu = target_cu;
9c541725 23556 temp_die.sect_off = sect_off;
c24bdb02
KS
23557
23558 if (target_cu != cu)
23559 target_cu->ancestor = cu;
23560
9a3c8263 23561 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23562 &temp_die,
23563 to_underlying (sect_off));
5c631832 23564}
10b3939b 23565
5c631832
JK
23566/* Follow reference attribute ATTR of SRC_DIE.
23567 On entry *REF_CU is the CU of SRC_DIE.
23568 On exit *REF_CU is the CU of the result. */
23569
23570static struct die_info *
ff39bb5e 23571follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23572 struct dwarf2_cu **ref_cu)
23573{
9c541725 23574 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23575 struct dwarf2_cu *cu = *ref_cu;
23576 struct die_info *die;
23577
9c541725 23578 die = follow_die_offset (sect_off,
36586728
TT
23579 (attr->form == DW_FORM_GNU_ref_alt
23580 || cu->per_cu->is_dwz),
23581 ref_cu);
5c631832 23582 if (!die)
9d8780f0
SM
23583 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23584 "at %s [in module %s]"),
23585 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23586 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23587
5c631832
JK
23588 return die;
23589}
23590
9c541725 23591/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23592 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23593 dwarf2_locexpr_baton->data has lifetime of
23594 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23595
23596struct dwarf2_locexpr_baton
9c541725 23597dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23598 struct dwarf2_per_cu_data *per_cu,
23599 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23600 void *baton, bool resolve_abstract_p)
5c631832 23601{
918dd910 23602 struct dwarf2_cu *cu;
5c631832
JK
23603 struct die_info *die;
23604 struct attribute *attr;
23605 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23606 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23607 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23608
918dd910 23609 if (per_cu->cu == NULL)
58f0c718 23610 load_cu (per_cu, false);
918dd910 23611 cu = per_cu->cu;
cc12ce38
DE
23612 if (cu == NULL)
23613 {
23614 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23615 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23616 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23617 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23618 }
918dd910 23619
9c541725 23620 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23621 if (!die)
9d8780f0
SM
23622 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23623 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23624
23625 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23626 if (!attr && resolve_abstract_p
3360b6e7 23627 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23628 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23629 {
23630 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 23631 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 23632 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23633
3360b6e7
TV
23634 for (const auto &cand_off
23635 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23636 {
3360b6e7
TV
23637 struct dwarf2_cu *cand_cu = cu;
23638 struct die_info *cand
23639 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23640 if (!cand
23641 || !cand->parent
e4a62c65
TV
23642 || cand->parent->tag != DW_TAG_subprogram)
23643 continue;
23644
23645 CORE_ADDR pc_low, pc_high;
23646 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23647 if (pc_low == ((CORE_ADDR) -1))
23648 continue;
23649 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23650 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23651 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23652 continue;
23653
23654 die = cand;
23655 attr = dwarf2_attr (die, DW_AT_location, cu);
23656 break;
23657 }
23658 }
23659
5c631832
JK
23660 if (!attr)
23661 {
e103e986
JK
23662 /* DWARF: "If there is no such attribute, then there is no effect.".
23663 DATA is ignored if SIZE is 0. */
5c631832 23664
e103e986 23665 retval.data = NULL;
5c631832
JK
23666 retval.size = 0;
23667 }
8cf6f0b1
TT
23668 else if (attr_form_is_section_offset (attr))
23669 {
23670 struct dwarf2_loclist_baton loclist_baton;
23671 CORE_ADDR pc = (*get_frame_pc) (baton);
23672 size_t size;
23673
23674 fill_in_loclist_baton (cu, &loclist_baton, attr);
23675
23676 retval.data = dwarf2_find_location_expression (&loclist_baton,
23677 &size, pc);
23678 retval.size = size;
23679 }
5c631832
JK
23680 else
23681 {
23682 if (!attr_form_is_block (attr))
9d8780f0 23683 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23684 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23685 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23686
23687 retval.data = DW_BLOCK (attr)->data;
23688 retval.size = DW_BLOCK (attr)->size;
23689 }
23690 retval.per_cu = cu->per_cu;
918dd910 23691
ed2dc618 23692 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23693
5c631832 23694 return retval;
348e048f
DE
23695}
23696
8b9737bf
TT
23697/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23698 offset. */
23699
23700struct dwarf2_locexpr_baton
23701dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23702 struct dwarf2_per_cu_data *per_cu,
23703 CORE_ADDR (*get_frame_pc) (void *baton),
23704 void *baton)
23705{
9c541725 23706 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23707
9c541725 23708 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23709}
23710
b6807d98
TT
23711/* Write a constant of a given type as target-ordered bytes into
23712 OBSTACK. */
23713
23714static const gdb_byte *
23715write_constant_as_bytes (struct obstack *obstack,
23716 enum bfd_endian byte_order,
23717 struct type *type,
23718 ULONGEST value,
23719 LONGEST *len)
23720{
23721 gdb_byte *result;
23722
23723 *len = TYPE_LENGTH (type);
224c3ddb 23724 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23725 store_unsigned_integer (result, *len, byte_order, value);
23726
23727 return result;
23728}
23729
23730/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23731 pointer to the constant bytes and set LEN to the length of the
23732 data. If memory is needed, allocate it on OBSTACK. If the DIE
23733 does not have a DW_AT_const_value, return NULL. */
23734
23735const gdb_byte *
9c541725 23736dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23737 struct dwarf2_per_cu_data *per_cu,
23738 struct obstack *obstack,
23739 LONGEST *len)
23740{
23741 struct dwarf2_cu *cu;
23742 struct die_info *die;
23743 struct attribute *attr;
23744 const gdb_byte *result = NULL;
23745 struct type *type;
23746 LONGEST value;
23747 enum bfd_endian byte_order;
e3b94546 23748 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23749
b6807d98 23750 if (per_cu->cu == NULL)
58f0c718 23751 load_cu (per_cu, false);
b6807d98 23752 cu = per_cu->cu;
cc12ce38
DE
23753 if (cu == NULL)
23754 {
23755 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23756 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23757 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23758 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23759 }
b6807d98 23760
9c541725 23761 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23762 if (!die)
9d8780f0
SM
23763 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23764 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23765
23766 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23767 if (attr == NULL)
23768 return NULL;
23769
e3b94546 23770 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23771 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23772
23773 switch (attr->form)
23774 {
23775 case DW_FORM_addr:
336d760d 23776 case DW_FORM_addrx:
b6807d98
TT
23777 case DW_FORM_GNU_addr_index:
23778 {
23779 gdb_byte *tem;
23780
23781 *len = cu->header.addr_size;
224c3ddb 23782 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23783 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23784 result = tem;
23785 }
23786 break;
23787 case DW_FORM_string:
23788 case DW_FORM_strp:
cf532bd1 23789 case DW_FORM_strx:
b6807d98
TT
23790 case DW_FORM_GNU_str_index:
23791 case DW_FORM_GNU_strp_alt:
23792 /* DW_STRING is already allocated on the objfile obstack, point
23793 directly to it. */
23794 result = (const gdb_byte *) DW_STRING (attr);
23795 *len = strlen (DW_STRING (attr));
23796 break;
23797 case DW_FORM_block1:
23798 case DW_FORM_block2:
23799 case DW_FORM_block4:
23800 case DW_FORM_block:
23801 case DW_FORM_exprloc:
0224619f 23802 case DW_FORM_data16:
b6807d98
TT
23803 result = DW_BLOCK (attr)->data;
23804 *len = DW_BLOCK (attr)->size;
23805 break;
23806
23807 /* The DW_AT_const_value attributes are supposed to carry the
23808 symbol's value "represented as it would be on the target
23809 architecture." By the time we get here, it's already been
23810 converted to host endianness, so we just need to sign- or
23811 zero-extend it as appropriate. */
23812 case DW_FORM_data1:
23813 type = die_type (die, cu);
23814 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23815 if (result == NULL)
23816 result = write_constant_as_bytes (obstack, byte_order,
23817 type, value, len);
23818 break;
23819 case DW_FORM_data2:
23820 type = die_type (die, cu);
23821 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23822 if (result == NULL)
23823 result = write_constant_as_bytes (obstack, byte_order,
23824 type, value, len);
23825 break;
23826 case DW_FORM_data4:
23827 type = die_type (die, cu);
23828 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23829 if (result == NULL)
23830 result = write_constant_as_bytes (obstack, byte_order,
23831 type, value, len);
23832 break;
23833 case DW_FORM_data8:
23834 type = die_type (die, cu);
23835 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23836 if (result == NULL)
23837 result = write_constant_as_bytes (obstack, byte_order,
23838 type, value, len);
23839 break;
23840
23841 case DW_FORM_sdata:
663c44ac 23842 case DW_FORM_implicit_const:
b6807d98
TT
23843 type = die_type (die, cu);
23844 result = write_constant_as_bytes (obstack, byte_order,
23845 type, DW_SND (attr), len);
23846 break;
23847
23848 case DW_FORM_udata:
23849 type = die_type (die, cu);
23850 result = write_constant_as_bytes (obstack, byte_order,
23851 type, DW_UNSND (attr), len);
23852 break;
23853
23854 default:
b98664d3 23855 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23856 dwarf_form_name (attr->form));
23857 break;
23858 }
23859
23860 return result;
23861}
23862
7942e96e
AA
23863/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23864 valid type for this die is found. */
23865
23866struct type *
9c541725 23867dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23868 struct dwarf2_per_cu_data *per_cu)
23869{
23870 struct dwarf2_cu *cu;
23871 struct die_info *die;
23872
7942e96e 23873 if (per_cu->cu == NULL)
58f0c718 23874 load_cu (per_cu, false);
7942e96e
AA
23875 cu = per_cu->cu;
23876 if (!cu)
23877 return NULL;
23878
9c541725 23879 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23880 if (!die)
23881 return NULL;
23882
23883 return die_type (die, cu);
23884}
23885
8a9b8146
TT
23886/* Return the type of the DIE at DIE_OFFSET in the CU named by
23887 PER_CU. */
23888
23889struct type *
b64f50a1 23890dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23891 struct dwarf2_per_cu_data *per_cu)
23892{
9c541725 23893 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23894 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23895}
23896
ac9ec31b 23897/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23898 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23899 On exit *REF_CU is the CU of the result.
23900 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23901
23902static struct die_info *
ac9ec31b
DE
23903follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23904 struct dwarf2_cu **ref_cu)
348e048f 23905{
348e048f 23906 struct die_info temp_die;
c24bdb02 23907 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23908 struct die_info *die;
23909
ac9ec31b
DE
23910 /* While it might be nice to assert sig_type->type == NULL here,
23911 we can get here for DW_AT_imported_declaration where we need
23912 the DIE not the type. */
348e048f
DE
23913
23914 /* If necessary, add it to the queue and load its DIEs. */
23915
95554aad 23916 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23917 read_signatured_type (sig_type);
348e048f 23918
348e048f 23919 sig_cu = sig_type->per_cu.cu;
69d751e3 23920 gdb_assert (sig_cu != NULL);
9c541725
PA
23921 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23922 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23923 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23924 to_underlying (temp_die.sect_off));
348e048f
DE
23925 if (die)
23926 {
ed2dc618 23927 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23928 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23929
796a7ff8
DE
23930 /* For .gdb_index version 7 keep track of included TUs.
23931 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23932 if (dwarf2_per_objfile->index_table != NULL
23933 && dwarf2_per_objfile->index_table->version <= 7)
23934 {
ae640021 23935 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23936 }
23937
348e048f 23938 *ref_cu = sig_cu;
c24bdb02
KS
23939 if (sig_cu != cu)
23940 sig_cu->ancestor = cu;
23941
348e048f
DE
23942 return die;
23943 }
23944
ac9ec31b
DE
23945 return NULL;
23946}
23947
23948/* Follow signatured type referenced by ATTR in SRC_DIE.
23949 On entry *REF_CU is the CU of SRC_DIE.
23950 On exit *REF_CU is the CU of the result.
23951 The result is the DIE of the type.
23952 If the referenced type cannot be found an error is thrown. */
23953
23954static struct die_info *
ff39bb5e 23955follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23956 struct dwarf2_cu **ref_cu)
23957{
23958 ULONGEST signature = DW_SIGNATURE (attr);
23959 struct signatured_type *sig_type;
23960 struct die_info *die;
23961
23962 gdb_assert (attr->form == DW_FORM_ref_sig8);
23963
a2ce51a0 23964 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23965 /* sig_type will be NULL if the signatured type is missing from
23966 the debug info. */
23967 if (sig_type == NULL)
23968 {
23969 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23970 " from DIE at %s [in module %s]"),
23971 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23972 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23973 }
23974
23975 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23976 if (die == NULL)
23977 {
23978 dump_die_for_error (src_die);
23979 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23980 " from DIE at %s [in module %s]"),
23981 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23982 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23983 }
23984
23985 return die;
23986}
23987
23988/* Get the type specified by SIGNATURE referenced in DIE/CU,
23989 reading in and processing the type unit if necessary. */
23990
23991static struct type *
23992get_signatured_type (struct die_info *die, ULONGEST signature,
23993 struct dwarf2_cu *cu)
23994{
518817b3
SM
23995 struct dwarf2_per_objfile *dwarf2_per_objfile
23996 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23997 struct signatured_type *sig_type;
23998 struct dwarf2_cu *type_cu;
23999 struct die_info *type_die;
24000 struct type *type;
24001
a2ce51a0 24002 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24003 /* sig_type will be NULL if the signatured type is missing from
24004 the debug info. */
24005 if (sig_type == NULL)
24006 {
b98664d3 24007 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24008 " from DIE at %s [in module %s]"),
24009 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24010 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24011 return build_error_marker_type (cu, die);
24012 }
24013
24014 /* If we already know the type we're done. */
24015 if (sig_type->type != NULL)
24016 return sig_type->type;
24017
24018 type_cu = cu;
24019 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24020 if (type_die != NULL)
24021 {
24022 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24023 is created. This is important, for example, because for c++ classes
24024 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24025 type = read_type_die (type_die, type_cu);
24026 if (type == NULL)
24027 {
b98664d3 24028 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24029 " referenced from DIE at %s [in module %s]"),
24030 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24031 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24032 type = build_error_marker_type (cu, die);
24033 }
24034 }
24035 else
24036 {
b98664d3 24037 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24038 " from DIE at %s [in module %s]"),
24039 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24040 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24041 type = build_error_marker_type (cu, die);
24042 }
24043 sig_type->type = type;
24044
24045 return type;
24046}
24047
24048/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24049 reading in and processing the type unit if necessary. */
24050
24051static struct type *
ff39bb5e 24052get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24053 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24054{
24055 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 24056 if (attr_form_is_ref (attr))
ac9ec31b
DE
24057 {
24058 struct dwarf2_cu *type_cu = cu;
24059 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24060
24061 return read_type_die (type_die, type_cu);
24062 }
24063 else if (attr->form == DW_FORM_ref_sig8)
24064 {
24065 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24066 }
24067 else
24068 {
518817b3
SM
24069 struct dwarf2_per_objfile *dwarf2_per_objfile
24070 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24071
b98664d3 24072 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24073 " at %s [in module %s]"),
24074 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 24075 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24076 return build_error_marker_type (cu, die);
24077 }
348e048f
DE
24078}
24079
e5fe5e75 24080/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24081
24082static void
e5fe5e75 24083load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 24084{
52dc124a 24085 struct signatured_type *sig_type;
348e048f 24086
f4dc4d17
DE
24087 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24088 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24089
6721b2ec
DE
24090 /* We have the per_cu, but we need the signatured_type.
24091 Fortunately this is an easy translation. */
24092 gdb_assert (per_cu->is_debug_types);
24093 sig_type = (struct signatured_type *) per_cu;
348e048f 24094
6721b2ec 24095 gdb_assert (per_cu->cu == NULL);
348e048f 24096
52dc124a 24097 read_signatured_type (sig_type);
348e048f 24098
6721b2ec 24099 gdb_assert (per_cu->cu != NULL);
348e048f
DE
24100}
24101
3019eac3
DE
24102/* Read in a signatured type and build its CU and DIEs.
24103 If the type is a stub for the real type in a DWO file,
24104 read in the real type from the DWO file as well. */
dee91e82
DE
24105
24106static void
24107read_signatured_type (struct signatured_type *sig_type)
24108{
24109 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24110
3019eac3 24111 gdb_assert (per_cu->is_debug_types);
dee91e82 24112 gdb_assert (per_cu->cu == NULL);
348e048f 24113
c0ab21c2
TT
24114 cutu_reader reader (per_cu, NULL, 0, 1, false);
24115
24116 if (!reader.dummy_p)
24117 {
24118 struct dwarf2_cu *cu = reader.cu;
24119 const gdb_byte *info_ptr = reader.info_ptr;
24120
24121 gdb_assert (cu->die_hash == NULL);
24122 cu->die_hash =
24123 htab_create_alloc_ex (cu->header.length / 12,
24124 die_hash,
24125 die_eq,
24126 NULL,
24127 &cu->comp_unit_obstack,
24128 hashtab_obstack_allocate,
24129 dummy_obstack_deallocate);
24130
24131 if (reader.has_children)
24132 reader.comp_unit_die->child
24133 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24134 reader.comp_unit_die);
24135 cu->dies = reader.comp_unit_die;
24136 /* comp_unit_die is not stored in die_hash, no need. */
24137
24138 /* We try not to read any attributes in this function, because
24139 not all CUs needed for references have been loaded yet, and
24140 symbol table processing isn't initialized. But we have to
24141 set the CU language, or we won't be able to build types
24142 correctly. Similarly, if we do not read the producer, we can
24143 not apply producer-specific interpretation. */
24144 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24145 }
24146
7ee85ab1 24147 sig_type->per_cu.tu_read = 1;
c906108c
SS
24148}
24149
c906108c
SS
24150/* Decode simple location descriptions.
24151 Given a pointer to a dwarf block that defines a location, compute
24152 the location and return the value.
24153
4cecd739
DJ
24154 NOTE drow/2003-11-18: This function is called in two situations
24155 now: for the address of static or global variables (partial symbols
24156 only) and for offsets into structures which are expected to be
24157 (more or less) constant. The partial symbol case should go away,
24158 and only the constant case should remain. That will let this
24159 function complain more accurately. A few special modes are allowed
24160 without complaint for global variables (for instance, global
24161 register values and thread-local values).
c906108c
SS
24162
24163 A location description containing no operations indicates that the
4cecd739 24164 object is optimized out. The return value is 0 for that case.
6b992462
DJ
24165 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24166 callers will only want a very basic result and this can become a
21ae7a4d
JK
24167 complaint.
24168
24169 Note that stack[0] is unused except as a default error return. */
c906108c
SS
24170
24171static CORE_ADDR
e7c27a73 24172decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 24173{
518817b3 24174 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
24175 size_t i;
24176 size_t size = blk->size;
d521ce57 24177 const gdb_byte *data = blk->data;
21ae7a4d
JK
24178 CORE_ADDR stack[64];
24179 int stacki;
24180 unsigned int bytes_read, unsnd;
24181 gdb_byte op;
c906108c 24182
21ae7a4d
JK
24183 i = 0;
24184 stacki = 0;
24185 stack[stacki] = 0;
24186 stack[++stacki] = 0;
24187
24188 while (i < size)
24189 {
24190 op = data[i++];
24191 switch (op)
24192 {
24193 case DW_OP_lit0:
24194 case DW_OP_lit1:
24195 case DW_OP_lit2:
24196 case DW_OP_lit3:
24197 case DW_OP_lit4:
24198 case DW_OP_lit5:
24199 case DW_OP_lit6:
24200 case DW_OP_lit7:
24201 case DW_OP_lit8:
24202 case DW_OP_lit9:
24203 case DW_OP_lit10:
24204 case DW_OP_lit11:
24205 case DW_OP_lit12:
24206 case DW_OP_lit13:
24207 case DW_OP_lit14:
24208 case DW_OP_lit15:
24209 case DW_OP_lit16:
24210 case DW_OP_lit17:
24211 case DW_OP_lit18:
24212 case DW_OP_lit19:
24213 case DW_OP_lit20:
24214 case DW_OP_lit21:
24215 case DW_OP_lit22:
24216 case DW_OP_lit23:
24217 case DW_OP_lit24:
24218 case DW_OP_lit25:
24219 case DW_OP_lit26:
24220 case DW_OP_lit27:
24221 case DW_OP_lit28:
24222 case DW_OP_lit29:
24223 case DW_OP_lit30:
24224 case DW_OP_lit31:
24225 stack[++stacki] = op - DW_OP_lit0;
24226 break;
f1bea926 24227
21ae7a4d
JK
24228 case DW_OP_reg0:
24229 case DW_OP_reg1:
24230 case DW_OP_reg2:
24231 case DW_OP_reg3:
24232 case DW_OP_reg4:
24233 case DW_OP_reg5:
24234 case DW_OP_reg6:
24235 case DW_OP_reg7:
24236 case DW_OP_reg8:
24237 case DW_OP_reg9:
24238 case DW_OP_reg10:
24239 case DW_OP_reg11:
24240 case DW_OP_reg12:
24241 case DW_OP_reg13:
24242 case DW_OP_reg14:
24243 case DW_OP_reg15:
24244 case DW_OP_reg16:
24245 case DW_OP_reg17:
24246 case DW_OP_reg18:
24247 case DW_OP_reg19:
24248 case DW_OP_reg20:
24249 case DW_OP_reg21:
24250 case DW_OP_reg22:
24251 case DW_OP_reg23:
24252 case DW_OP_reg24:
24253 case DW_OP_reg25:
24254 case DW_OP_reg26:
24255 case DW_OP_reg27:
24256 case DW_OP_reg28:
24257 case DW_OP_reg29:
24258 case DW_OP_reg30:
24259 case DW_OP_reg31:
24260 stack[++stacki] = op - DW_OP_reg0;
24261 if (i < size)
24262 dwarf2_complex_location_expr_complaint ();
24263 break;
c906108c 24264
21ae7a4d
JK
24265 case DW_OP_regx:
24266 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24267 i += bytes_read;
24268 stack[++stacki] = unsnd;
24269 if (i < size)
24270 dwarf2_complex_location_expr_complaint ();
24271 break;
c906108c 24272
21ae7a4d
JK
24273 case DW_OP_addr:
24274 stack[++stacki] = read_address (objfile->obfd, &data[i],
24275 cu, &bytes_read);
24276 i += bytes_read;
24277 break;
d53d4ac5 24278
21ae7a4d
JK
24279 case DW_OP_const1u:
24280 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24281 i += 1;
24282 break;
24283
24284 case DW_OP_const1s:
24285 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24286 i += 1;
24287 break;
24288
24289 case DW_OP_const2u:
24290 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24291 i += 2;
24292 break;
24293
24294 case DW_OP_const2s:
24295 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24296 i += 2;
24297 break;
d53d4ac5 24298
21ae7a4d
JK
24299 case DW_OP_const4u:
24300 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24301 i += 4;
24302 break;
24303
24304 case DW_OP_const4s:
24305 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24306 i += 4;
24307 break;
24308
585861ea
JK
24309 case DW_OP_const8u:
24310 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24311 i += 8;
24312 break;
24313
21ae7a4d
JK
24314 case DW_OP_constu:
24315 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24316 &bytes_read);
24317 i += bytes_read;
24318 break;
24319
24320 case DW_OP_consts:
24321 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24322 i += bytes_read;
24323 break;
24324
24325 case DW_OP_dup:
24326 stack[stacki + 1] = stack[stacki];
24327 stacki++;
24328 break;
24329
24330 case DW_OP_plus:
24331 stack[stacki - 1] += stack[stacki];
24332 stacki--;
24333 break;
24334
24335 case DW_OP_plus_uconst:
24336 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24337 &bytes_read);
24338 i += bytes_read;
24339 break;
24340
24341 case DW_OP_minus:
24342 stack[stacki - 1] -= stack[stacki];
24343 stacki--;
24344 break;
24345
24346 case DW_OP_deref:
24347 /* If we're not the last op, then we definitely can't encode
24348 this using GDB's address_class enum. This is valid for partial
24349 global symbols, although the variable's address will be bogus
24350 in the psymtab. */
24351 if (i < size)
24352 dwarf2_complex_location_expr_complaint ();
24353 break;
24354
24355 case DW_OP_GNU_push_tls_address:
4aa4e28b 24356 case DW_OP_form_tls_address:
21ae7a4d
JK
24357 /* The top of the stack has the offset from the beginning
24358 of the thread control block at which the variable is located. */
24359 /* Nothing should follow this operator, so the top of stack would
24360 be returned. */
24361 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24362 address will be bogus in the psymtab. Make it always at least
24363 non-zero to not look as a variable garbage collected by linker
24364 which have DW_OP_addr 0. */
21ae7a4d
JK
24365 if (i < size)
24366 dwarf2_complex_location_expr_complaint ();
585861ea 24367 stack[stacki]++;
21ae7a4d
JK
24368 break;
24369
24370 case DW_OP_GNU_uninit:
24371 break;
24372
336d760d 24373 case DW_OP_addrx:
3019eac3 24374 case DW_OP_GNU_addr_index:
49f6c839 24375 case DW_OP_GNU_const_index:
3019eac3
DE
24376 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24377 &bytes_read);
24378 i += bytes_read;
24379 break;
24380
21ae7a4d
JK
24381 default:
24382 {
f39c6ffd 24383 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24384
24385 if (name)
b98664d3 24386 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24387 name);
24388 else
b98664d3 24389 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24390 op);
24391 }
24392
24393 return (stack[stacki]);
d53d4ac5 24394 }
3c6e0cb3 24395
21ae7a4d
JK
24396 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24397 outside of the allocated space. Also enforce minimum>0. */
24398 if (stacki >= ARRAY_SIZE (stack) - 1)
24399 {
b98664d3 24400 complaint (_("location description stack overflow"));
21ae7a4d
JK
24401 return 0;
24402 }
24403
24404 if (stacki <= 0)
24405 {
b98664d3 24406 complaint (_("location description stack underflow"));
21ae7a4d
JK
24407 return 0;
24408 }
24409 }
24410 return (stack[stacki]);
c906108c
SS
24411}
24412
24413/* memory allocation interface */
24414
c906108c 24415static struct dwarf_block *
7b5a2f43 24416dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24417{
8d749320 24418 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24419}
24420
c906108c 24421static struct die_info *
b60c80d6 24422dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24423{
24424 struct die_info *die;
b60c80d6
DJ
24425 size_t size = sizeof (struct die_info);
24426
24427 if (num_attrs > 1)
24428 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24429
b60c80d6 24430 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24431 memset (die, 0, sizeof (struct die_info));
24432 return (die);
24433}
2e276125
JB
24434
24435\f
24436/* Macro support. */
24437
233d95b5
JK
24438/* Return file name relative to the compilation directory of file number I in
24439 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24440 responsible for freeing it. */
233d95b5 24441
2e276125 24442static char *
233d95b5 24443file_file_name (int file, struct line_header *lh)
2e276125 24444{
6a83a1e6
EZ
24445 /* Is the file number a valid index into the line header's file name
24446 table? Remember that file numbers start with one, not zero. */
7ba99d21 24447 if (lh->is_valid_file_index (file))
6a83a1e6 24448 {
7ba99d21 24449 const file_entry *fe = lh->file_name_at (file);
6e70227d 24450
7ba99d21 24451 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24452 {
7ba99d21 24453 const char *dir = fe->include_dir (lh);
8c43009f 24454 if (dir != NULL)
7ba99d21 24455 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24456 }
7ba99d21 24457 return xstrdup (fe->name);
6a83a1e6 24458 }
2e276125
JB
24459 else
24460 {
6a83a1e6
EZ
24461 /* The compiler produced a bogus file number. We can at least
24462 record the macro definitions made in the file, even if we
24463 won't be able to find the file by name. */
24464 char fake_name[80];
9a619af0 24465
8c042590
PM
24466 xsnprintf (fake_name, sizeof (fake_name),
24467 "<bad macro file number %d>", file);
2e276125 24468
b98664d3 24469 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24470 file);
2e276125 24471
6a83a1e6 24472 return xstrdup (fake_name);
2e276125
JB
24473 }
24474}
24475
233d95b5
JK
24476/* Return the full name of file number I in *LH's file name table.
24477 Use COMP_DIR as the name of the current directory of the
24478 compilation. The result is allocated using xmalloc; the caller is
24479 responsible for freeing it. */
24480static char *
24481file_full_name (int file, struct line_header *lh, const char *comp_dir)
24482{
24483 /* Is the file number a valid index into the line header's file name
24484 table? Remember that file numbers start with one, not zero. */
7ba99d21 24485 if (lh->is_valid_file_index (file))
233d95b5
JK
24486 {
24487 char *relative = file_file_name (file, lh);
24488
24489 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24490 return relative;
b36cec19
PA
24491 return reconcat (relative, comp_dir, SLASH_STRING,
24492 relative, (char *) NULL);
233d95b5
JK
24493 }
24494 else
24495 return file_file_name (file, lh);
24496}
24497
2e276125
JB
24498
24499static struct macro_source_file *
804d2729
TT
24500macro_start_file (struct dwarf2_cu *cu,
24501 int file, int line,
2e276125 24502 struct macro_source_file *current_file,
43f3e411 24503 struct line_header *lh)
2e276125 24504{
233d95b5
JK
24505 /* File name relative to the compilation directory of this source file. */
24506 char *file_name = file_file_name (file, lh);
2e276125 24507
2e276125 24508 if (! current_file)
abc9d0dc 24509 {
fc474241
DE
24510 /* Note: We don't create a macro table for this compilation unit
24511 at all until we actually get a filename. */
c24bdb02 24512 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24513
abc9d0dc
TT
24514 /* If we have no current file, then this must be the start_file
24515 directive for the compilation unit's main source file. */
fc474241
DE
24516 current_file = macro_set_main (macro_table, file_name);
24517 macro_define_special (macro_table);
abc9d0dc 24518 }
2e276125 24519 else
233d95b5 24520 current_file = macro_include (current_file, line, file_name);
2e276125 24521
233d95b5 24522 xfree (file_name);
6e70227d 24523
2e276125
JB
24524 return current_file;
24525}
24526
2e276125
JB
24527static const char *
24528consume_improper_spaces (const char *p, const char *body)
24529{
24530 if (*p == ' ')
24531 {
b98664d3 24532 complaint (_("macro definition contains spaces "
3e43a32a 24533 "in formal argument list:\n`%s'"),
4d3c2250 24534 body);
2e276125
JB
24535
24536 while (*p == ' ')
24537 p++;
24538 }
24539
24540 return p;
24541}
24542
24543
24544static void
24545parse_macro_definition (struct macro_source_file *file, int line,
24546 const char *body)
24547{
24548 const char *p;
24549
24550 /* The body string takes one of two forms. For object-like macro
24551 definitions, it should be:
24552
24553 <macro name> " " <definition>
24554
24555 For function-like macro definitions, it should be:
24556
24557 <macro name> "() " <definition>
24558 or
24559 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24560
24561 Spaces may appear only where explicitly indicated, and in the
24562 <definition>.
24563
24564 The Dwarf 2 spec says that an object-like macro's name is always
24565 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24566 the space when the macro's definition is the empty string.
2e276125
JB
24567
24568 The Dwarf 2 spec says that there should be no spaces between the
24569 formal arguments in a function-like macro's formal argument list,
24570 but versions of GCC around March 2002 include spaces after the
24571 commas. */
24572
24573
24574 /* Find the extent of the macro name. The macro name is terminated
24575 by either a space or null character (for an object-like macro) or
24576 an opening paren (for a function-like macro). */
24577 for (p = body; *p; p++)
24578 if (*p == ' ' || *p == '(')
24579 break;
24580
24581 if (*p == ' ' || *p == '\0')
24582 {
24583 /* It's an object-like macro. */
24584 int name_len = p - body;
456e800a 24585 std::string name (body, name_len);
2e276125
JB
24586 const char *replacement;
24587
24588 if (*p == ' ')
24589 replacement = body + name_len + 1;
24590 else
24591 {
4d3c2250 24592 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24593 replacement = body + name_len;
24594 }
6e70227d 24595
456e800a 24596 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
24597 }
24598 else if (*p == '(')
24599 {
24600 /* It's a function-like macro. */
456e800a 24601 std::string name (body, p - body);
2e276125
JB
24602 int argc = 0;
24603 int argv_size = 1;
8d749320 24604 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24605
24606 p++;
24607
24608 p = consume_improper_spaces (p, body);
24609
24610 /* Parse the formal argument list. */
24611 while (*p && *p != ')')
24612 {
24613 /* Find the extent of the current argument name. */
24614 const char *arg_start = p;
24615
24616 while (*p && *p != ',' && *p != ')' && *p != ' ')
24617 p++;
24618
24619 if (! *p || p == arg_start)
4d3c2250 24620 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24621 else
24622 {
24623 /* Make sure argv has room for the new argument. */
24624 if (argc >= argv_size)
24625 {
24626 argv_size *= 2;
224c3ddb 24627 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24628 }
24629
3f8a7804 24630 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24631 }
24632
24633 p = consume_improper_spaces (p, body);
24634
24635 /* Consume the comma, if present. */
24636 if (*p == ',')
24637 {
24638 p++;
24639
24640 p = consume_improper_spaces (p, body);
24641 }
24642 }
24643
24644 if (*p == ')')
24645 {
24646 p++;
24647
24648 if (*p == ' ')
24649 /* Perfectly formed definition, no complaints. */
456e800a 24650 macro_define_function (file, line, name.c_str (),
6e70227d 24651 argc, (const char **) argv,
2e276125
JB
24652 p + 1);
24653 else if (*p == '\0')
24654 {
24655 /* Complain, but do define it. */
4d3c2250 24656 dwarf2_macro_malformed_definition_complaint (body);
456e800a 24657 macro_define_function (file, line, name.c_str (),
6e70227d 24658 argc, (const char **) argv,
2e276125
JB
24659 p);
24660 }
24661 else
24662 /* Just complain. */
4d3c2250 24663 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24664 }
24665 else
24666 /* Just complain. */
4d3c2250 24667 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24668
2e276125
JB
24669 {
24670 int i;
24671
24672 for (i = 0; i < argc; i++)
24673 xfree (argv[i]);
24674 }
24675 xfree (argv);
24676 }
24677 else
4d3c2250 24678 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24679}
24680
cf2c3c16
TT
24681/* Skip some bytes from BYTES according to the form given in FORM.
24682 Returns the new pointer. */
2e276125 24683
d521ce57
TT
24684static const gdb_byte *
24685skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24686 enum dwarf_form form,
24687 unsigned int offset_size,
24688 struct dwarf2_section_info *section)
2e276125 24689{
cf2c3c16 24690 unsigned int bytes_read;
2e276125 24691
cf2c3c16 24692 switch (form)
2e276125 24693 {
cf2c3c16
TT
24694 case DW_FORM_data1:
24695 case DW_FORM_flag:
24696 ++bytes;
24697 break;
24698
24699 case DW_FORM_data2:
24700 bytes += 2;
24701 break;
24702
24703 case DW_FORM_data4:
24704 bytes += 4;
24705 break;
24706
24707 case DW_FORM_data8:
24708 bytes += 8;
24709 break;
24710
0224619f
JK
24711 case DW_FORM_data16:
24712 bytes += 16;
24713 break;
24714
cf2c3c16
TT
24715 case DW_FORM_string:
24716 read_direct_string (abfd, bytes, &bytes_read);
24717 bytes += bytes_read;
24718 break;
24719
24720 case DW_FORM_sec_offset:
24721 case DW_FORM_strp:
36586728 24722 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24723 bytes += offset_size;
24724 break;
24725
24726 case DW_FORM_block:
24727 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24728 bytes += bytes_read;
24729 break;
24730
24731 case DW_FORM_block1:
24732 bytes += 1 + read_1_byte (abfd, bytes);
24733 break;
24734 case DW_FORM_block2:
24735 bytes += 2 + read_2_bytes (abfd, bytes);
24736 break;
24737 case DW_FORM_block4:
24738 bytes += 4 + read_4_bytes (abfd, bytes);
24739 break;
24740
336d760d 24741 case DW_FORM_addrx:
cf2c3c16 24742 case DW_FORM_sdata:
cf532bd1 24743 case DW_FORM_strx:
cf2c3c16 24744 case DW_FORM_udata:
3019eac3
DE
24745 case DW_FORM_GNU_addr_index:
24746 case DW_FORM_GNU_str_index:
d521ce57 24747 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24748 if (bytes == NULL)
24749 {
24750 dwarf2_section_buffer_overflow_complaint (section);
24751 return NULL;
24752 }
cf2c3c16
TT
24753 break;
24754
663c44ac
JK
24755 case DW_FORM_implicit_const:
24756 break;
24757
cf2c3c16
TT
24758 default:
24759 {
b98664d3 24760 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24761 form, get_section_name (section));
cf2c3c16
TT
24762 return NULL;
24763 }
2e276125
JB
24764 }
24765
cf2c3c16
TT
24766 return bytes;
24767}
757a13d0 24768
cf2c3c16
TT
24769/* A helper for dwarf_decode_macros that handles skipping an unknown
24770 opcode. Returns an updated pointer to the macro data buffer; or,
24771 on error, issues a complaint and returns NULL. */
757a13d0 24772
d521ce57 24773static const gdb_byte *
cf2c3c16 24774skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24775 const gdb_byte **opcode_definitions,
24776 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24777 bfd *abfd,
24778 unsigned int offset_size,
24779 struct dwarf2_section_info *section)
24780{
24781 unsigned int bytes_read, i;
24782 unsigned long arg;
d521ce57 24783 const gdb_byte *defn;
2e276125 24784
cf2c3c16 24785 if (opcode_definitions[opcode] == NULL)
2e276125 24786 {
b98664d3 24787 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24788 opcode);
24789 return NULL;
24790 }
2e276125 24791
cf2c3c16
TT
24792 defn = opcode_definitions[opcode];
24793 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24794 defn += bytes_read;
2e276125 24795
cf2c3c16
TT
24796 for (i = 0; i < arg; ++i)
24797 {
aead7601
SM
24798 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24799 (enum dwarf_form) defn[i], offset_size,
f664829e 24800 section);
cf2c3c16
TT
24801 if (mac_ptr == NULL)
24802 {
24803 /* skip_form_bytes already issued the complaint. */
24804 return NULL;
24805 }
24806 }
757a13d0 24807
cf2c3c16
TT
24808 return mac_ptr;
24809}
757a13d0 24810
cf2c3c16
TT
24811/* A helper function which parses the header of a macro section.
24812 If the macro section is the extended (for now called "GNU") type,
24813 then this updates *OFFSET_SIZE. Returns a pointer to just after
24814 the header, or issues a complaint and returns NULL on error. */
757a13d0 24815
d521ce57
TT
24816static const gdb_byte *
24817dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24818 bfd *abfd,
d521ce57 24819 const gdb_byte *mac_ptr,
cf2c3c16
TT
24820 unsigned int *offset_size,
24821 int section_is_gnu)
24822{
24823 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24824
cf2c3c16
TT
24825 if (section_is_gnu)
24826 {
24827 unsigned int version, flags;
757a13d0 24828
cf2c3c16 24829 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24830 if (version != 4 && version != 5)
cf2c3c16 24831 {
b98664d3 24832 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24833 version);
24834 return NULL;
24835 }
24836 mac_ptr += 2;
757a13d0 24837
cf2c3c16
TT
24838 flags = read_1_byte (abfd, mac_ptr);
24839 ++mac_ptr;
24840 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24841
cf2c3c16
TT
24842 if ((flags & 2) != 0)
24843 /* We don't need the line table offset. */
24844 mac_ptr += *offset_size;
757a13d0 24845
cf2c3c16
TT
24846 /* Vendor opcode descriptions. */
24847 if ((flags & 4) != 0)
24848 {
24849 unsigned int i, count;
757a13d0 24850
cf2c3c16
TT
24851 count = read_1_byte (abfd, mac_ptr);
24852 ++mac_ptr;
24853 for (i = 0; i < count; ++i)
24854 {
24855 unsigned int opcode, bytes_read;
24856 unsigned long arg;
24857
24858 opcode = read_1_byte (abfd, mac_ptr);
24859 ++mac_ptr;
24860 opcode_definitions[opcode] = mac_ptr;
24861 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24862 mac_ptr += bytes_read;
24863 mac_ptr += arg;
24864 }
757a13d0 24865 }
cf2c3c16 24866 }
757a13d0 24867
cf2c3c16
TT
24868 return mac_ptr;
24869}
757a13d0 24870
cf2c3c16 24871/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24872 including DW_MACRO_import. */
cf2c3c16
TT
24873
24874static void
804d2729 24875dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24876 bfd *abfd,
d521ce57 24877 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24878 struct macro_source_file *current_file,
43f3e411 24879 struct line_header *lh,
cf2c3c16 24880 struct dwarf2_section_info *section,
36586728 24881 int section_is_gnu, int section_is_dwz,
cf2c3c16 24882 unsigned int offset_size,
8fc3fc34 24883 htab_t include_hash)
cf2c3c16 24884{
804d2729
TT
24885 struct dwarf2_per_objfile *dwarf2_per_objfile
24886 = cu->per_cu->dwarf2_per_objfile;
4d663531 24887 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24888 enum dwarf_macro_record_type macinfo_type;
24889 int at_commandline;
d521ce57 24890 const gdb_byte *opcode_definitions[256];
757a13d0 24891
cf2c3c16
TT
24892 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24893 &offset_size, section_is_gnu);
24894 if (mac_ptr == NULL)
24895 {
24896 /* We already issued a complaint. */
24897 return;
24898 }
757a13d0
JK
24899
24900 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24901 GDB is still reading the definitions from command line. First
24902 DW_MACINFO_start_file will need to be ignored as it was already executed
24903 to create CURRENT_FILE for the main source holding also the command line
24904 definitions. On first met DW_MACINFO_start_file this flag is reset to
24905 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24906
24907 at_commandline = 1;
24908
24909 do
24910 {
24911 /* Do we at least have room for a macinfo type byte? */
24912 if (mac_ptr >= mac_end)
24913 {
f664829e 24914 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24915 break;
24916 }
24917
aead7601 24918 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24919 mac_ptr++;
24920
cf2c3c16
TT
24921 /* Note that we rely on the fact that the corresponding GNU and
24922 DWARF constants are the same. */
132448f8
SM
24923 DIAGNOSTIC_PUSH
24924 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24925 switch (macinfo_type)
24926 {
24927 /* A zero macinfo type indicates the end of the macro
24928 information. */
24929 case 0:
24930 break;
2e276125 24931
0af92d60
JK
24932 case DW_MACRO_define:
24933 case DW_MACRO_undef:
24934 case DW_MACRO_define_strp:
24935 case DW_MACRO_undef_strp:
24936 case DW_MACRO_define_sup:
24937 case DW_MACRO_undef_sup:
2e276125 24938 {
891d2f0b 24939 unsigned int bytes_read;
2e276125 24940 int line;
d521ce57 24941 const char *body;
cf2c3c16 24942 int is_define;
2e276125 24943
cf2c3c16
TT
24944 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24945 mac_ptr += bytes_read;
24946
0af92d60
JK
24947 if (macinfo_type == DW_MACRO_define
24948 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24949 {
24950 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24951 mac_ptr += bytes_read;
24952 }
24953 else
24954 {
24955 LONGEST str_offset;
24956
24957 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24958 mac_ptr += offset_size;
2e276125 24959
0af92d60
JK
24960 if (macinfo_type == DW_MACRO_define_sup
24961 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24962 || section_is_dwz)
36586728 24963 {
ed2dc618
SM
24964 struct dwz_file *dwz
24965 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24966
ed2dc618
SM
24967 body = read_indirect_string_from_dwz (objfile,
24968 dwz, str_offset);
36586728
TT
24969 }
24970 else
ed2dc618
SM
24971 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24972 abfd, str_offset);
cf2c3c16
TT
24973 }
24974
0af92d60
JK
24975 is_define = (macinfo_type == DW_MACRO_define
24976 || macinfo_type == DW_MACRO_define_strp
24977 || macinfo_type == DW_MACRO_define_sup);
2e276125 24978 if (! current_file)
757a13d0
JK
24979 {
24980 /* DWARF violation as no main source is present. */
b98664d3 24981 complaint (_("debug info with no main source gives macro %s "
757a13d0 24982 "on line %d: %s"),
cf2c3c16
TT
24983 is_define ? _("definition") : _("undefinition"),
24984 line, body);
757a13d0
JK
24985 break;
24986 }
3e43a32a
MS
24987 if ((line == 0 && !at_commandline)
24988 || (line != 0 && at_commandline))
b98664d3 24989 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24990 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24991 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24992 line == 0 ? _("zero") : _("non-zero"), line, body);
24993
955b06fa 24994 if (body == NULL)
7bede828 24995 {
955b06fa
SDJ
24996 /* Fedora's rpm-build's "debugedit" binary
24997 corrupted .debug_macro sections.
24998
24999 For more info, see
25000 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
25001 complaint (_("debug info gives %s invalid macro %s "
25002 "without body (corrupted?) at line %d "
25003 "on file %s"),
25004 at_commandline ? _("command-line") : _("in-file"),
25005 is_define ? _("definition") : _("undefinition"),
25006 line, current_file->filename);
7bede828 25007 }
955b06fa
SDJ
25008 else if (is_define)
25009 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
25010 else
25011 {
0af92d60
JK
25012 gdb_assert (macinfo_type == DW_MACRO_undef
25013 || macinfo_type == DW_MACRO_undef_strp
25014 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
25015 macro_undef (current_file, line, body);
25016 }
2e276125
JB
25017 }
25018 break;
25019
0af92d60 25020 case DW_MACRO_start_file:
2e276125 25021 {
891d2f0b 25022 unsigned int bytes_read;
2e276125
JB
25023 int line, file;
25024
25025 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25026 mac_ptr += bytes_read;
25027 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25028 mac_ptr += bytes_read;
25029
3e43a32a
MS
25030 if ((line == 0 && !at_commandline)
25031 || (line != 0 && at_commandline))
b98664d3 25032 complaint (_("debug info gives source %d included "
757a13d0
JK
25033 "from %s at %s line %d"),
25034 file, at_commandline ? _("command-line") : _("file"),
25035 line == 0 ? _("zero") : _("non-zero"), line);
25036
25037 if (at_commandline)
25038 {
0af92d60 25039 /* This DW_MACRO_start_file was executed in the
cf2c3c16 25040 pass one. */
757a13d0
JK
25041 at_commandline = 0;
25042 }
25043 else
804d2729
TT
25044 current_file = macro_start_file (cu, file, line, current_file,
25045 lh);
2e276125
JB
25046 }
25047 break;
25048
0af92d60 25049 case DW_MACRO_end_file:
2e276125 25050 if (! current_file)
b98664d3 25051 complaint (_("macro debug info has an unmatched "
3e43a32a 25052 "`close_file' directive"));
2e276125
JB
25053 else
25054 {
25055 current_file = current_file->included_by;
25056 if (! current_file)
25057 {
cf2c3c16 25058 enum dwarf_macro_record_type next_type;
2e276125
JB
25059
25060 /* GCC circa March 2002 doesn't produce the zero
25061 type byte marking the end of the compilation
25062 unit. Complain if it's not there, but exit no
25063 matter what. */
25064
25065 /* Do we at least have room for a macinfo type byte? */
25066 if (mac_ptr >= mac_end)
25067 {
f664829e 25068 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
25069 return;
25070 }
25071
25072 /* We don't increment mac_ptr here, so this is just
25073 a look-ahead. */
aead7601
SM
25074 next_type
25075 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25076 mac_ptr);
2e276125 25077 if (next_type != 0)
b98664d3 25078 complaint (_("no terminating 0-type entry for "
3e43a32a 25079 "macros in `.debug_macinfo' section"));
2e276125
JB
25080
25081 return;
25082 }
25083 }
25084 break;
25085
0af92d60
JK
25086 case DW_MACRO_import:
25087 case DW_MACRO_import_sup:
cf2c3c16
TT
25088 {
25089 LONGEST offset;
8fc3fc34 25090 void **slot;
a036ba48
TT
25091 bfd *include_bfd = abfd;
25092 struct dwarf2_section_info *include_section = section;
d521ce57 25093 const gdb_byte *include_mac_end = mac_end;
a036ba48 25094 int is_dwz = section_is_dwz;
d521ce57 25095 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
25096
25097 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25098 mac_ptr += offset_size;
25099
0af92d60 25100 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 25101 {
ed2dc618 25102 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 25103
4d663531 25104 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 25105
a036ba48 25106 include_section = &dwz->macro;
a32a8923 25107 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
25108 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25109 is_dwz = 1;
25110 }
25111
25112 new_mac_ptr = include_section->buffer + offset;
25113 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25114
8fc3fc34
TT
25115 if (*slot != NULL)
25116 {
25117 /* This has actually happened; see
25118 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 25119 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
25120 ".debug_macro section"));
25121 }
25122 else
25123 {
d521ce57 25124 *slot = (void *) new_mac_ptr;
36586728 25125
804d2729 25126 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 25127 include_mac_end, current_file, lh,
36586728 25128 section, section_is_gnu, is_dwz,
4d663531 25129 offset_size, include_hash);
8fc3fc34 25130
d521ce57 25131 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 25132 }
cf2c3c16
TT
25133 }
25134 break;
25135
2e276125 25136 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
25137 if (!section_is_gnu)
25138 {
25139 unsigned int bytes_read;
2e276125 25140
ac298888
TT
25141 /* This reads the constant, but since we don't recognize
25142 any vendor extensions, we ignore it. */
25143 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
25144 mac_ptr += bytes_read;
25145 read_direct_string (abfd, mac_ptr, &bytes_read);
25146 mac_ptr += bytes_read;
2e276125 25147
cf2c3c16
TT
25148 /* We don't recognize any vendor extensions. */
25149 break;
25150 }
25151 /* FALLTHROUGH */
25152
25153 default:
25154 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25155 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25156 section);
25157 if (mac_ptr == NULL)
25158 return;
25159 break;
2e276125 25160 }
132448f8 25161 DIAGNOSTIC_POP
757a13d0 25162 } while (macinfo_type != 0);
2e276125 25163}
8e19ed76 25164
cf2c3c16 25165static void
09262596 25166dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 25167 int section_is_gnu)
cf2c3c16 25168{
518817b3
SM
25169 struct dwarf2_per_objfile *dwarf2_per_objfile
25170 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25171 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
25172 struct line_header *lh = cu->line_header;
25173 bfd *abfd;
d521ce57 25174 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
25175 struct macro_source_file *current_file = 0;
25176 enum dwarf_macro_record_type macinfo_type;
25177 unsigned int offset_size = cu->header.offset_size;
d521ce57 25178 const gdb_byte *opcode_definitions[256];
8fc3fc34 25179 void **slot;
09262596
DE
25180 struct dwarf2_section_info *section;
25181 const char *section_name;
25182
25183 if (cu->dwo_unit != NULL)
25184 {
25185 if (section_is_gnu)
25186 {
25187 section = &cu->dwo_unit->dwo_file->sections.macro;
25188 section_name = ".debug_macro.dwo";
25189 }
25190 else
25191 {
25192 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25193 section_name = ".debug_macinfo.dwo";
25194 }
25195 }
25196 else
25197 {
25198 if (section_is_gnu)
25199 {
25200 section = &dwarf2_per_objfile->macro;
25201 section_name = ".debug_macro";
25202 }
25203 else
25204 {
25205 section = &dwarf2_per_objfile->macinfo;
25206 section_name = ".debug_macinfo";
25207 }
25208 }
cf2c3c16 25209
bb5ed363 25210 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25211 if (section->buffer == NULL)
25212 {
b98664d3 25213 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
25214 return;
25215 }
a32a8923 25216 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25217
25218 /* First pass: Find the name of the base filename.
25219 This filename is needed in order to process all macros whose definition
25220 (or undefinition) comes from the command line. These macros are defined
25221 before the first DW_MACINFO_start_file entry, and yet still need to be
25222 associated to the base file.
25223
25224 To determine the base file name, we scan the macro definitions until we
25225 reach the first DW_MACINFO_start_file entry. We then initialize
25226 CURRENT_FILE accordingly so that any macro definition found before the
25227 first DW_MACINFO_start_file can still be associated to the base file. */
25228
25229 mac_ptr = section->buffer + offset;
25230 mac_end = section->buffer + section->size;
25231
25232 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25233 &offset_size, section_is_gnu);
25234 if (mac_ptr == NULL)
25235 {
25236 /* We already issued a complaint. */
25237 return;
25238 }
25239
25240 do
25241 {
25242 /* Do we at least have room for a macinfo type byte? */
25243 if (mac_ptr >= mac_end)
25244 {
25245 /* Complaint is printed during the second pass as GDB will probably
25246 stop the first pass earlier upon finding
25247 DW_MACINFO_start_file. */
25248 break;
25249 }
25250
aead7601 25251 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25252 mac_ptr++;
25253
25254 /* Note that we rely on the fact that the corresponding GNU and
25255 DWARF constants are the same. */
132448f8
SM
25256 DIAGNOSTIC_PUSH
25257 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25258 switch (macinfo_type)
25259 {
25260 /* A zero macinfo type indicates the end of the macro
25261 information. */
25262 case 0:
25263 break;
25264
0af92d60
JK
25265 case DW_MACRO_define:
25266 case DW_MACRO_undef:
cf2c3c16
TT
25267 /* Only skip the data by MAC_PTR. */
25268 {
25269 unsigned int bytes_read;
25270
25271 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25272 mac_ptr += bytes_read;
25273 read_direct_string (abfd, mac_ptr, &bytes_read);
25274 mac_ptr += bytes_read;
25275 }
25276 break;
25277
0af92d60 25278 case DW_MACRO_start_file:
cf2c3c16
TT
25279 {
25280 unsigned int bytes_read;
25281 int line, file;
25282
25283 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25284 mac_ptr += bytes_read;
25285 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25286 mac_ptr += bytes_read;
25287
804d2729 25288 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25289 }
25290 break;
25291
0af92d60 25292 case DW_MACRO_end_file:
cf2c3c16
TT
25293 /* No data to skip by MAC_PTR. */
25294 break;
25295
0af92d60
JK
25296 case DW_MACRO_define_strp:
25297 case DW_MACRO_undef_strp:
25298 case DW_MACRO_define_sup:
25299 case DW_MACRO_undef_sup:
cf2c3c16
TT
25300 {
25301 unsigned int bytes_read;
25302
25303 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25304 mac_ptr += bytes_read;
25305 mac_ptr += offset_size;
25306 }
25307 break;
25308
0af92d60
JK
25309 case DW_MACRO_import:
25310 case DW_MACRO_import_sup:
cf2c3c16 25311 /* Note that, according to the spec, a transparent include
0af92d60 25312 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25313 skip this opcode. */
25314 mac_ptr += offset_size;
25315 break;
25316
25317 case DW_MACINFO_vendor_ext:
25318 /* Only skip the data by MAC_PTR. */
25319 if (!section_is_gnu)
25320 {
25321 unsigned int bytes_read;
25322
25323 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25324 mac_ptr += bytes_read;
25325 read_direct_string (abfd, mac_ptr, &bytes_read);
25326 mac_ptr += bytes_read;
25327 }
25328 /* FALLTHROUGH */
25329
25330 default:
25331 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25332 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25333 section);
25334 if (mac_ptr == NULL)
25335 return;
25336 break;
25337 }
132448f8 25338 DIAGNOSTIC_POP
cf2c3c16
TT
25339 } while (macinfo_type != 0 && current_file == NULL);
25340
25341 /* Second pass: Process all entries.
25342
25343 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25344 command-line macro definitions/undefinitions. This flag is unset when we
25345 reach the first DW_MACINFO_start_file entry. */
25346
fc4007c9
TT
25347 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25348 htab_eq_pointer,
25349 NULL, xcalloc, xfree));
8fc3fc34 25350 mac_ptr = section->buffer + offset;
fc4007c9 25351 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25352 *slot = (void *) mac_ptr;
804d2729 25353 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25354 current_file, lh, section,
fc4007c9
TT
25355 section_is_gnu, 0, offset_size,
25356 include_hash.get ());
cf2c3c16
TT
25357}
25358
8e19ed76 25359/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25360 if so return true else false. */
380bca97 25361
8e19ed76 25362static int
6e5a29e1 25363attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25364{
25365 return (attr == NULL ? 0 :
25366 attr->form == DW_FORM_block1
25367 || attr->form == DW_FORM_block2
25368 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25369 || attr->form == DW_FORM_block
25370 || attr->form == DW_FORM_exprloc);
8e19ed76 25371}
4c2df51b 25372
c6a0999f
JB
25373/* Return non-zero if ATTR's value is a section offset --- classes
25374 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25375 You may use DW_UNSND (attr) to retrieve such offsets.
25376
25377 Section 7.5.4, "Attribute Encodings", explains that no attribute
25378 may have a value that belongs to more than one of these classes; it
25379 would be ambiguous if we did, because we use the same forms for all
25380 of them. */
380bca97 25381
3690dd37 25382static int
6e5a29e1 25383attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25384{
25385 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25386 || attr->form == DW_FORM_data8
25387 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25388}
25389
3690dd37
JB
25390/* Return non-zero if ATTR's value falls in the 'constant' class, or
25391 zero otherwise. When this function returns true, you can apply
25392 dwarf2_get_attr_constant_value to it.
25393
25394 However, note that for some attributes you must check
25395 attr_form_is_section_offset before using this test. DW_FORM_data4
25396 and DW_FORM_data8 are members of both the constant class, and of
25397 the classes that contain offsets into other debug sections
25398 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25399 that, if an attribute's can be either a constant or one of the
25400 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25401 taken as section offsets, not constants.
25402
25403 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25404 cannot handle that. */
380bca97 25405
3690dd37 25406static int
6e5a29e1 25407attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25408{
25409 switch (attr->form)
25410 {
25411 case DW_FORM_sdata:
25412 case DW_FORM_udata:
25413 case DW_FORM_data1:
25414 case DW_FORM_data2:
25415 case DW_FORM_data4:
25416 case DW_FORM_data8:
663c44ac 25417 case DW_FORM_implicit_const:
3690dd37
JB
25418 return 1;
25419 default:
25420 return 0;
25421 }
25422}
25423
7771576e
SA
25424
25425/* DW_ADDR is always stored already as sect_offset; despite for the forms
25426 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25427
25428static int
6e5a29e1 25429attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25430{
25431 switch (attr->form)
25432 {
25433 case DW_FORM_ref_addr:
25434 case DW_FORM_ref1:
25435 case DW_FORM_ref2:
25436 case DW_FORM_ref4:
25437 case DW_FORM_ref8:
25438 case DW_FORM_ref_udata:
25439 case DW_FORM_GNU_ref_alt:
25440 return 1;
25441 default:
25442 return 0;
25443 }
25444}
25445
3019eac3
DE
25446/* Return the .debug_loc section to use for CU.
25447 For DWO files use .debug_loc.dwo. */
25448
25449static struct dwarf2_section_info *
25450cu_debug_loc_section (struct dwarf2_cu *cu)
25451{
518817b3
SM
25452 struct dwarf2_per_objfile *dwarf2_per_objfile
25453 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25454
3019eac3 25455 if (cu->dwo_unit)
43988095
JK
25456 {
25457 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25458
43988095
JK
25459 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25460 }
25461 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25462 : &dwarf2_per_objfile->loc);
3019eac3
DE
25463}
25464
8cf6f0b1
TT
25465/* A helper function that fills in a dwarf2_loclist_baton. */
25466
25467static void
25468fill_in_loclist_baton (struct dwarf2_cu *cu,
25469 struct dwarf2_loclist_baton *baton,
ff39bb5e 25470 const struct attribute *attr)
8cf6f0b1 25471{
518817b3
SM
25472 struct dwarf2_per_objfile *dwarf2_per_objfile
25473 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25474 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25475
25476 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25477
25478 baton->per_cu = cu->per_cu;
25479 gdb_assert (baton->per_cu);
25480 /* We don't know how long the location list is, but make sure we
25481 don't run off the edge of the section. */
3019eac3
DE
25482 baton->size = section->size - DW_UNSND (attr);
25483 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25484 baton->base_address = cu->base_address;
f664829e 25485 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25486}
25487
4c2df51b 25488static void
ff39bb5e 25489dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25490 struct dwarf2_cu *cu, int is_block)
4c2df51b 25491{
518817b3
SM
25492 struct dwarf2_per_objfile *dwarf2_per_objfile
25493 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25494 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25495 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25496
3690dd37 25497 if (attr_form_is_section_offset (attr)
3019eac3 25498 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25499 the section. If so, fall through to the complaint in the
25500 other branch. */
3019eac3 25501 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25502 {
0d53c4c4 25503 struct dwarf2_loclist_baton *baton;
4c2df51b 25504
8d749320 25505 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25506
8cf6f0b1 25507 fill_in_loclist_baton (cu, baton, attr);
be391dca 25508
d00adf39 25509 if (cu->base_known == 0)
b98664d3 25510 complaint (_("Location list used without "
3e43a32a 25511 "specifying the CU base address."));
4c2df51b 25512
f1e6e072
TT
25513 SYMBOL_ACLASS_INDEX (sym) = (is_block
25514 ? dwarf2_loclist_block_index
25515 : dwarf2_loclist_index);
0d53c4c4
DJ
25516 SYMBOL_LOCATION_BATON (sym) = baton;
25517 }
25518 else
25519 {
25520 struct dwarf2_locexpr_baton *baton;
25521
8d749320 25522 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25523 baton->per_cu = cu->per_cu;
25524 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25525
25526 if (attr_form_is_block (attr))
25527 {
25528 /* Note that we're just copying the block's data pointer
25529 here, not the actual data. We're still pointing into the
6502dd73
DJ
25530 info_buffer for SYM's objfile; right now we never release
25531 that buffer, but when we do clean up properly this may
25532 need to change. */
0d53c4c4
DJ
25533 baton->size = DW_BLOCK (attr)->size;
25534 baton->data = DW_BLOCK (attr)->data;
25535 }
25536 else
25537 {
25538 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 25539 sym->natural_name ());
0d53c4c4 25540 baton->size = 0;
0d53c4c4 25541 }
6e70227d 25542
f1e6e072
TT
25543 SYMBOL_ACLASS_INDEX (sym) = (is_block
25544 ? dwarf2_locexpr_block_index
25545 : dwarf2_locexpr_index);
0d53c4c4
DJ
25546 SYMBOL_LOCATION_BATON (sym) = baton;
25547 }
4c2df51b 25548}
6502dd73 25549
9aa1f1e3
TT
25550/* Return the OBJFILE associated with the compilation unit CU. If CU
25551 came from a separate debuginfo file, then the master objfile is
25552 returned. */
ae0d2f24
UW
25553
25554struct objfile *
25555dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25556{
e3b94546 25557 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25558
25559 /* Return the master objfile, so that we can report and look up the
25560 correct file containing this variable. */
25561 if (objfile->separate_debug_objfile_backlink)
25562 objfile = objfile->separate_debug_objfile_backlink;
25563
25564 return objfile;
25565}
25566
96408a79
SA
25567/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25568 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25569 CU_HEADERP first. */
25570
25571static const struct comp_unit_head *
25572per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25573 struct dwarf2_per_cu_data *per_cu)
25574{
d521ce57 25575 const gdb_byte *info_ptr;
96408a79
SA
25576
25577 if (per_cu->cu)
25578 return &per_cu->cu->header;
25579
9c541725 25580 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25581
25582 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25583 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25584 rcuh_kind::COMPILE);
96408a79
SA
25585
25586 return cu_headerp;
25587}
25588
ae0d2f24
UW
25589/* Return the address size given in the compilation unit header for CU. */
25590
98714339 25591int
ae0d2f24
UW
25592dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25593{
96408a79
SA
25594 struct comp_unit_head cu_header_local;
25595 const struct comp_unit_head *cu_headerp;
c471e790 25596
96408a79
SA
25597 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25598
25599 return cu_headerp->addr_size;
ae0d2f24
UW
25600}
25601
9eae7c52
TT
25602/* Return the offset size given in the compilation unit header for CU. */
25603
25604int
25605dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25606{
96408a79
SA
25607 struct comp_unit_head cu_header_local;
25608 const struct comp_unit_head *cu_headerp;
9c6c53f7 25609
96408a79
SA
25610 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25611
25612 return cu_headerp->offset_size;
25613}
25614
25615/* See its dwarf2loc.h declaration. */
25616
25617int
25618dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25619{
25620 struct comp_unit_head cu_header_local;
25621 const struct comp_unit_head *cu_headerp;
25622
25623 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25624
25625 if (cu_headerp->version == 2)
25626 return cu_headerp->addr_size;
25627 else
25628 return cu_headerp->offset_size;
181cebd4
JK
25629}
25630
9aa1f1e3
TT
25631/* Return the text offset of the CU. The returned offset comes from
25632 this CU's objfile. If this objfile came from a separate debuginfo
25633 file, then the offset may be different from the corresponding
25634 offset in the parent objfile. */
25635
25636CORE_ADDR
25637dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25638{
b3b3bada 25639 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
9aa1f1e3
TT
25640}
25641
9a49df9d
AB
25642/* Return a type that is a generic pointer type, the size of which matches
25643 the address size given in the compilation unit header for PER_CU. */
25644static struct type *
25645dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25646{
25647 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25648 struct type *void_type = objfile_type (objfile)->builtin_void;
25649 struct type *addr_type = lookup_pointer_type (void_type);
25650 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25651
25652 if (TYPE_LENGTH (addr_type) == addr_size)
25653 return addr_type;
25654
25655 addr_type
25656 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25657 return addr_type;
25658}
25659
43988095
JK
25660/* Return DWARF version number of PER_CU. */
25661
25662short
25663dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25664{
25665 return per_cu->dwarf_version;
25666}
25667
348e048f
DE
25668/* Locate the .debug_info compilation unit from CU's objfile which contains
25669 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25670
25671static struct dwarf2_per_cu_data *
9c541725 25672dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25673 unsigned int offset_in_dwz,
ed2dc618 25674 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25675{
25676 struct dwarf2_per_cu_data *this_cu;
25677 int low, high;
25678
ae038cb0 25679 low = 0;
b76e467d 25680 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25681 while (high > low)
25682 {
36586728 25683 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25684 int mid = low + (high - low) / 2;
9a619af0 25685
36586728 25686 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25687 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25688 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25689 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25690 high = mid;
25691 else
25692 low = mid + 1;
25693 }
25694 gdb_assert (low == high);
36586728 25695 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25696 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25697 {
36586728 25698 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25699 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25700 "offset %s [in module %s]"),
25701 sect_offset_str (sect_off),
ed2dc618 25702 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25703
9c541725
PA
25704 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25705 <= sect_off);
ae038cb0
DJ
25706 return dwarf2_per_objfile->all_comp_units[low-1];
25707 }
25708 else
25709 {
b76e467d 25710 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25711 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25712 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25713 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25714 return this_cu;
25715 }
25716}
25717
23745b47 25718/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25719
fcd3b13d
SM
25720dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25721 : per_cu (per_cu_),
9068261f
AB
25722 mark (false),
25723 has_loclist (false),
25724 checked_producer (false),
25725 producer_is_gxx_lt_4_6 (false),
25726 producer_is_gcc_lt_4_3 (false),
eb77c9df 25727 producer_is_icc (false),
9068261f 25728 producer_is_icc_lt_14 (false),
c258c396 25729 producer_is_codewarrior (false),
9068261f 25730 processing_has_namespace_info (false)
93311388 25731{
fcd3b13d
SM
25732 per_cu->cu = this;
25733}
25734
25735/* Destroy a dwarf2_cu. */
25736
25737dwarf2_cu::~dwarf2_cu ()
25738{
25739 per_cu->cu = NULL;
9816fde3
JK
25740}
25741
25742/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25743
25744static void
95554aad
TT
25745prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25746 enum language pretend_language)
9816fde3
JK
25747{
25748 struct attribute *attr;
25749
25750 /* Set the language we're debugging. */
25751 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25752 if (attr != nullptr)
9816fde3
JK
25753 set_cu_language (DW_UNSND (attr), cu);
25754 else
9cded63f 25755 {
95554aad 25756 cu->language = pretend_language;
9cded63f
TT
25757 cu->language_defn = language_def (cu->language);
25758 }
dee91e82 25759
7d45c7c3 25760 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25761}
25762
ae038cb0
DJ
25763/* Increase the age counter on each cached compilation unit, and free
25764 any that are too old. */
25765
25766static void
ed2dc618 25767age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25768{
25769 struct dwarf2_per_cu_data *per_cu, **last_chain;
25770
25771 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25772 per_cu = dwarf2_per_objfile->read_in_chain;
25773 while (per_cu != NULL)
25774 {
25775 per_cu->cu->last_used ++;
b4f54984 25776 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25777 dwarf2_mark (per_cu->cu);
25778 per_cu = per_cu->cu->read_in_chain;
25779 }
25780
25781 per_cu = dwarf2_per_objfile->read_in_chain;
25782 last_chain = &dwarf2_per_objfile->read_in_chain;
25783 while (per_cu != NULL)
25784 {
25785 struct dwarf2_per_cu_data *next_cu;
25786
25787 next_cu = per_cu->cu->read_in_chain;
25788
25789 if (!per_cu->cu->mark)
25790 {
fcd3b13d 25791 delete per_cu->cu;
ae038cb0
DJ
25792 *last_chain = next_cu;
25793 }
25794 else
25795 last_chain = &per_cu->cu->read_in_chain;
25796
25797 per_cu = next_cu;
25798 }
25799}
25800
25801/* Remove a single compilation unit from the cache. */
25802
25803static void
dee91e82 25804free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25805{
25806 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25807 struct dwarf2_per_objfile *dwarf2_per_objfile
25808 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25809
25810 per_cu = dwarf2_per_objfile->read_in_chain;
25811 last_chain = &dwarf2_per_objfile->read_in_chain;
25812 while (per_cu != NULL)
25813 {
25814 struct dwarf2_per_cu_data *next_cu;
25815
25816 next_cu = per_cu->cu->read_in_chain;
25817
dee91e82 25818 if (per_cu == target_per_cu)
ae038cb0 25819 {
fcd3b13d 25820 delete per_cu->cu;
dee91e82 25821 per_cu->cu = NULL;
ae038cb0
DJ
25822 *last_chain = next_cu;
25823 break;
25824 }
25825 else
25826 last_chain = &per_cu->cu->read_in_chain;
25827
25828 per_cu = next_cu;
25829 }
25830}
25831
dee91e82
DE
25832/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25833 We store these in a hash table separate from the DIEs, and preserve them
25834 when the DIEs are flushed out of cache.
25835
25836 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25837 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25838 or the type may come from a DWO file. Furthermore, while it's more logical
25839 to use per_cu->section+offset, with Fission the section with the data is in
25840 the DWO file but we don't know that section at the point we need it.
25841 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25842 because we can enter the lookup routine, get_die_type_at_offset, from
25843 outside this file, and thus won't necessarily have PER_CU->cu.
25844 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25845
dee91e82 25846struct dwarf2_per_cu_offset_and_type
1c379e20 25847{
dee91e82 25848 const struct dwarf2_per_cu_data *per_cu;
9c541725 25849 sect_offset sect_off;
1c379e20
DJ
25850 struct type *type;
25851};
25852
dee91e82 25853/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25854
25855static hashval_t
dee91e82 25856per_cu_offset_and_type_hash (const void *item)
1c379e20 25857{
9a3c8263
SM
25858 const struct dwarf2_per_cu_offset_and_type *ofs
25859 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25860
9c541725 25861 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25862}
25863
dee91e82 25864/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25865
25866static int
dee91e82 25867per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25868{
9a3c8263
SM
25869 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25870 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25871 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25872 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25873
dee91e82 25874 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25875 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25876}
25877
25878/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25879 table if necessary. For convenience, return TYPE.
25880
25881 The DIEs reading must have careful ordering to:
85102364 25882 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25883 reading current DIE.
25884 * Not trying to dereference contents of still incompletely read in types
25885 while reading in other DIEs.
25886 * Enable referencing still incompletely read in types just by a pointer to
25887 the type without accessing its fields.
25888
25889 Therefore caller should follow these rules:
25890 * Try to fetch any prerequisite types we may need to build this DIE type
25891 before building the type and calling set_die_type.
e71ec853 25892 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25893 possible before fetching more types to complete the current type.
25894 * Make the type as complete as possible before fetching more types. */
1c379e20 25895
f792889a 25896static struct type *
1c379e20
DJ
25897set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25898{
518817b3
SM
25899 struct dwarf2_per_objfile *dwarf2_per_objfile
25900 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25901 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25902 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25903 struct attribute *attr;
25904 struct dynamic_prop prop;
1c379e20 25905
b4ba55a1
JB
25906 /* For Ada types, make sure that the gnat-specific data is always
25907 initialized (if not already set). There are a few types where
25908 we should not be doing so, because the type-specific area is
25909 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25910 where the type-specific area is used to store the floatformat).
25911 But this is not a problem, because the gnat-specific information
25912 is actually not needed for these types. */
25913 if (need_gnat_info (cu)
25914 && TYPE_CODE (type) != TYPE_CODE_FUNC
25915 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25916 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25917 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25918 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25919 && !HAVE_GNAT_AUX_INFO (type))
25920 INIT_GNAT_SPECIFIC (type);
25921
3f2f83dd
KB
25922 /* Read DW_AT_allocated and set in type. */
25923 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25924 if (attr_form_is_block (attr))
25925 {
9a49df9d
AB
25926 struct type *prop_type
25927 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25928 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25929 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25930 }
25931 else if (attr != NULL)
25932 {
b98664d3 25933 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25934 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25935 sect_offset_str (die->sect_off));
3f2f83dd
KB
25936 }
25937
25938 /* Read DW_AT_associated and set in type. */
25939 attr = dwarf2_attr (die, DW_AT_associated, cu);
25940 if (attr_form_is_block (attr))
25941 {
9a49df9d
AB
25942 struct type *prop_type
25943 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25944 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25945 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25946 }
25947 else if (attr != NULL)
25948 {
b98664d3 25949 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25950 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25951 sect_offset_str (die->sect_off));
3f2f83dd
KB
25952 }
25953
3cdcd0ce
JB
25954 /* Read DW_AT_data_location and set in type. */
25955 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25956 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25957 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25958 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25959
dee91e82 25960 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25961 {
dee91e82
DE
25962 dwarf2_per_objfile->die_type_hash =
25963 htab_create_alloc_ex (127,
25964 per_cu_offset_and_type_hash,
25965 per_cu_offset_and_type_eq,
25966 NULL,
25967 &objfile->objfile_obstack,
25968 hashtab_obstack_allocate,
25969 dummy_obstack_deallocate);
f792889a 25970 }
1c379e20 25971
dee91e82 25972 ofs.per_cu = cu->per_cu;
9c541725 25973 ofs.sect_off = die->sect_off;
1c379e20 25974 ofs.type = type;
dee91e82
DE
25975 slot = (struct dwarf2_per_cu_offset_and_type **)
25976 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25977 if (*slot)
b98664d3 25978 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25979 sect_offset_str (die->sect_off));
8d749320
SM
25980 *slot = XOBNEW (&objfile->objfile_obstack,
25981 struct dwarf2_per_cu_offset_and_type);
1c379e20 25982 **slot = ofs;
f792889a 25983 return type;
1c379e20
DJ
25984}
25985
9c541725 25986/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25987 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25988
25989static struct type *
9c541725 25990get_die_type_at_offset (sect_offset sect_off,
673bfd45 25991 struct dwarf2_per_cu_data *per_cu)
1c379e20 25992{
dee91e82 25993 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25994 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25995
dee91e82 25996 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25997 return NULL;
1c379e20 25998
dee91e82 25999 ofs.per_cu = per_cu;
9c541725 26000 ofs.sect_off = sect_off;
9a3c8263
SM
26001 slot = ((struct dwarf2_per_cu_offset_and_type *)
26002 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
26003 if (slot)
26004 return slot->type;
26005 else
26006 return NULL;
26007}
26008
02142a6c 26009/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
26010 or return NULL if DIE does not have a saved type. */
26011
26012static struct type *
26013get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26014{
9c541725 26015 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
26016}
26017
10b3939b
DJ
26018/* Add a dependence relationship from CU to REF_PER_CU. */
26019
26020static void
26021dwarf2_add_dependence (struct dwarf2_cu *cu,
26022 struct dwarf2_per_cu_data *ref_per_cu)
26023{
26024 void **slot;
26025
26026 if (cu->dependencies == NULL)
26027 cu->dependencies
26028 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26029 NULL, &cu->comp_unit_obstack,
26030 hashtab_obstack_allocate,
26031 dummy_obstack_deallocate);
26032
26033 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26034 if (*slot == NULL)
26035 *slot = ref_per_cu;
26036}
1c379e20 26037
f504f079
DE
26038/* Subroutine of dwarf2_mark to pass to htab_traverse.
26039 Set the mark field in every compilation unit in the
ae038cb0
DJ
26040 cache that we must keep because we are keeping CU. */
26041
10b3939b
DJ
26042static int
26043dwarf2_mark_helper (void **slot, void *data)
26044{
26045 struct dwarf2_per_cu_data *per_cu;
26046
26047 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
26048
26049 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26050 reading of the chain. As such dependencies remain valid it is not much
26051 useful to track and undo them during QUIT cleanups. */
26052 if (per_cu->cu == NULL)
26053 return 1;
26054
10b3939b
DJ
26055 if (per_cu->cu->mark)
26056 return 1;
9068261f 26057 per_cu->cu->mark = true;
10b3939b
DJ
26058
26059 if (per_cu->cu->dependencies != NULL)
26060 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26061
26062 return 1;
26063}
26064
f504f079
DE
26065/* Set the mark field in CU and in every other compilation unit in the
26066 cache that we must keep because we are keeping CU. */
26067
ae038cb0
DJ
26068static void
26069dwarf2_mark (struct dwarf2_cu *cu)
26070{
26071 if (cu->mark)
26072 return;
9068261f 26073 cu->mark = true;
10b3939b
DJ
26074 if (cu->dependencies != NULL)
26075 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
26076}
26077
26078static void
26079dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26080{
26081 while (per_cu)
26082 {
9068261f 26083 per_cu->cu->mark = false;
ae038cb0
DJ
26084 per_cu = per_cu->cu->read_in_chain;
26085 }
72bf9492
DJ
26086}
26087
72bf9492
DJ
26088/* Trivial hash function for partial_die_info: the hash value of a DIE
26089 is its offset in .debug_info for this objfile. */
26090
26091static hashval_t
26092partial_die_hash (const void *item)
26093{
9a3c8263
SM
26094 const struct partial_die_info *part_die
26095 = (const struct partial_die_info *) item;
9a619af0 26096
9c541725 26097 return to_underlying (part_die->sect_off);
72bf9492
DJ
26098}
26099
26100/* Trivial comparison function for partial_die_info structures: two DIEs
26101 are equal if they have the same offset. */
26102
26103static int
26104partial_die_eq (const void *item_lhs, const void *item_rhs)
26105{
9a3c8263
SM
26106 const struct partial_die_info *part_die_lhs
26107 = (const struct partial_die_info *) item_lhs;
26108 const struct partial_die_info *part_die_rhs
26109 = (const struct partial_die_info *) item_rhs;
9a619af0 26110
9c541725 26111 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
26112}
26113
3c3bb058
AB
26114struct cmd_list_element *set_dwarf_cmdlist;
26115struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
26116
26117static void
981a3fb3 26118set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 26119{
b4f54984 26120 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 26121 gdb_stdout);
ae038cb0
DJ
26122}
26123
26124static void
981a3fb3 26125show_dwarf_cmd (const char *args, int from_tty)
6e70227d 26126{
b4f54984 26127 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
26128}
26129
491144b5 26130bool dwarf_always_disassemble;
437afbb8 26131
437afbb8 26132static void
cd4fb1b2
SM
26133show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26134 struct cmd_list_element *c, const char *value)
9291a0cd 26135{
cd4fb1b2
SM
26136 fprintf_filtered (file,
26137 _("Whether to always disassemble "
26138 "DWARF expressions is %s.\n"),
26139 value);
9291a0cd
TT
26140}
26141
9291a0cd 26142static void
cd4fb1b2
SM
26143show_check_physname (struct ui_file *file, int from_tty,
26144 struct cmd_list_element *c, const char *value)
9291a0cd 26145{
cd4fb1b2
SM
26146 fprintf_filtered (file,
26147 _("Whether to check \"physname\" is %s.\n"),
26148 value);
9291a0cd
TT
26149}
26150
6c265988 26151void _initialize_dwarf2_read ();
cd4fb1b2 26152void
6c265988 26153_initialize_dwarf2_read ()
9291a0cd 26154{
cd4fb1b2
SM
26155 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26156Set DWARF specific variables.\n\
590042fc 26157Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
26158 &set_dwarf_cmdlist, "maintenance set dwarf ",
26159 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 26160
cd4fb1b2 26161 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
26162Show DWARF specific variables.\n\
26163Show DWARF variables such as the cache size."),
cd4fb1b2
SM
26164 &show_dwarf_cmdlist, "maintenance show dwarf ",
26165 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 26166
cd4fb1b2
SM
26167 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26168 &dwarf_max_cache_age, _("\
26169Set the upper bound on the age of cached DWARF compilation units."), _("\
26170Show the upper bound on the age of cached DWARF compilation units."), _("\
26171A higher limit means that cached compilation units will be stored\n\
26172in memory longer, and more total memory will be used. Zero disables\n\
26173caching, which can slow down startup."),
26174 NULL,
26175 show_dwarf_max_cache_age,
26176 &set_dwarf_cmdlist,
26177 &show_dwarf_cmdlist);
156942c7 26178
cd4fb1b2
SM
26179 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26180 &dwarf_always_disassemble, _("\
26181Set whether `info address' always disassembles DWARF expressions."), _("\
26182Show whether `info address' always disassembles DWARF expressions."), _("\
26183When enabled, DWARF expressions are always printed in an assembly-like\n\
26184syntax. When disabled, expressions will be printed in a more\n\
26185conversational style, when possible."),
26186 NULL,
26187 show_dwarf_always_disassemble,
26188 &set_dwarf_cmdlist,
26189 &show_dwarf_cmdlist);
9291a0cd 26190
cd4fb1b2
SM
26191 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26192Set debugging of the DWARF reader."), _("\
26193Show debugging of the DWARF reader."), _("\
26194When enabled (non-zero), debugging messages are printed during DWARF\n\
26195reading and symtab expansion. A value of 1 (one) provides basic\n\
26196information. A value greater than 1 provides more verbose information."),
26197 NULL,
26198 NULL,
26199 &setdebuglist, &showdebuglist);
9291a0cd 26200
cd4fb1b2
SM
26201 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26202Set debugging of the DWARF DIE reader."), _("\
26203Show debugging of the DWARF DIE reader."), _("\
26204When enabled (non-zero), DIEs are dumped after they are read in.\n\
26205The value is the maximum depth to print."),
26206 NULL,
26207 NULL,
26208 &setdebuglist, &showdebuglist);
9291a0cd 26209
cd4fb1b2
SM
26210 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26211Set debugging of the dwarf line reader."), _("\
26212Show debugging of the dwarf line reader."), _("\
26213When enabled (non-zero), line number entries are dumped as they are read in.\n\
26214A value of 1 (one) provides basic information.\n\
26215A value greater than 1 provides more verbose information."),
26216 NULL,
26217 NULL,
26218 &setdebuglist, &showdebuglist);
437afbb8 26219
cd4fb1b2
SM
26220 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26221Set cross-checking of \"physname\" code against demangler."), _("\
26222Show cross-checking of \"physname\" code against demangler."), _("\
26223When enabled, GDB's internal \"physname\" code is checked against\n\
26224the demangler."),
26225 NULL, show_check_physname,
26226 &setdebuglist, &showdebuglist);
900e11f9 26227
e615022a
DE
26228 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26229 no_class, &use_deprecated_index_sections, _("\
26230Set whether to use deprecated gdb_index sections."), _("\
26231Show whether to use deprecated gdb_index sections."), _("\
26232When enabled, deprecated .gdb_index sections are used anyway.\n\
26233Normally they are ignored either because of a missing feature or\n\
26234performance issue.\n\
26235Warning: This option must be enabled before gdb reads the file."),
26236 NULL,
26237 NULL,
26238 &setlist, &showlist);
26239
f1e6e072
TT
26240 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26241 &dwarf2_locexpr_funcs);
26242 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26243 &dwarf2_loclist_funcs);
26244
26245 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26246 &dwarf2_block_frame_base_locexpr_funcs);
26247 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26248 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26249
26250#if GDB_SELF_TEST
26251 selftests::register_test ("dw2_expand_symtabs_matching",
26252 selftests::dw2_expand_symtabs_matching::run_test);
26253#endif
6502dd73 26254}
This page took 5.201529 seconds and 4 git commands to generate.