]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/dwarf2read.c
Consolidate partial symtab dependency reading
[thirdparty/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
685af9cd 1516static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1517 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1518 sect_offset);
433df2d4 1519
d521ce57 1520static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1521
dee91e82 1522static struct partial_die_info *load_partial_dies
d521ce57 1523 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1524
fb816e8b
TV
1525/* A pair of partial_die_info and compilation unit. */
1526struct cu_partial_die_info
1527{
1528 /* The compilation unit of the partial_die_info. */
1529 struct dwarf2_cu *cu;
1530 /* A partial_die_info. */
1531 struct partial_die_info *pdi;
122cf0f2
AB
1532
1533 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1534 : cu (cu),
1535 pdi (pdi)
405feb71 1536 { /* Nothing. */ }
122cf0f2
AB
1537
1538private:
1539 cu_partial_die_info () = delete;
fb816e8b
TV
1540};
1541
122cf0f2
AB
1542static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1543 struct dwarf2_cu *);
72bf9492 1544
d521ce57
TT
1545static const gdb_byte *read_attribute (const struct die_reader_specs *,
1546 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1547 const gdb_byte *, bool *need_reprocess);
1548
1549static void read_attribute_reprocess (const struct die_reader_specs *reader,
1550 struct attribute *attr);
1551
1552static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1553
a1855c1d 1554static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1555
a1855c1d 1556static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1557
a1855c1d 1558static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1559
15f18d14
AT
1560/* Read the next three bytes (little-endian order) as an unsigned integer. */
1561static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1562
a1855c1d 1563static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1564
a1855c1d 1565static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1566
d521ce57 1567static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1568 unsigned int *);
c906108c 1569
d521ce57 1570static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1571
1572static LONGEST read_checked_initial_length_and_offset
d521ce57 1573 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1574 unsigned int *, unsigned int *);
613e1657 1575
d521ce57
TT
1576static LONGEST read_offset (bfd *, const gdb_byte *,
1577 const struct comp_unit_head *,
c764a876
DE
1578 unsigned int *);
1579
d521ce57 1580static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1581
ed2dc618
SM
1582static sect_offset read_abbrev_offset
1583 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1584 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1585
d521ce57 1586static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1587
d521ce57 1588static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1589
ed2dc618
SM
1590static const char *read_indirect_string
1591 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1592 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1593
ed2dc618
SM
1594static const char *read_indirect_line_string
1595 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1596 const struct comp_unit_head *, unsigned int *);
36586728 1597
ed2dc618
SM
1598static const char *read_indirect_string_at_offset
1599 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1600 LONGEST str_offset);
927aa2e7 1601
ed2dc618
SM
1602static const char *read_indirect_string_from_dwz
1603 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1604
d521ce57 1605static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1606
d521ce57
TT
1607static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1608 const gdb_byte *,
3019eac3
DE
1609 unsigned int *);
1610
18a8505e
AT
1611static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1612 ULONGEST str_index);
1613
1614static const char *read_stub_str_index (struct dwarf2_cu *cu,
1615 ULONGEST str_index);
3019eac3 1616
e142c38c 1617static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1618
e142c38c
DJ
1619static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1620 struct dwarf2_cu *);
c906108c 1621
348e048f 1622static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1623 unsigned int);
348e048f 1624
7d45c7c3
KB
1625static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1626 struct dwarf2_cu *cu);
1627
a084a2a6
AT
1628static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1629
05cf31d1
JB
1630static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1631 struct dwarf2_cu *cu);
1632
e142c38c 1633static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1634
e142c38c 1635static struct die_info *die_specification (struct die_info *die,
f2f0e013 1636 struct dwarf2_cu **);
63d06c5c 1637
9c541725 1638static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1639 struct dwarf2_cu *cu);
debd256d 1640
f3f5162e 1641static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1642 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1643 CORE_ADDR, int decode_mapping);
c906108c 1644
804d2729
TT
1645static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1646 const char *);
c906108c 1647
a14ed312 1648static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1649 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1650
ff39bb5e 1651static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1652 struct dwarf2_cu *);
c906108c 1653
ff39bb5e 1654static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1655 struct type *type,
1656 const char *name,
1657 struct obstack *obstack,
12df843f 1658 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1659 const gdb_byte **bytes,
98bfdba5 1660 struct dwarf2_locexpr_baton **baton);
2df3850c 1661
e7c27a73 1662static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1663
b4ba55a1
JB
1664static int need_gnat_info (struct dwarf2_cu *);
1665
3e43a32a
MS
1666static struct type *die_descriptive_type (struct die_info *,
1667 struct dwarf2_cu *);
b4ba55a1
JB
1668
1669static void set_descriptive_type (struct type *, struct die_info *,
1670 struct dwarf2_cu *);
1671
e7c27a73
DJ
1672static struct type *die_containing_type (struct die_info *,
1673 struct dwarf2_cu *);
c906108c 1674
ff39bb5e 1675static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1676 struct dwarf2_cu *);
c906108c 1677
f792889a 1678static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1679
673bfd45
DE
1680static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1681
0d5cff50 1682static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1683
6e70227d 1684static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1685 const char *suffix, int physname,
1686 struct dwarf2_cu *cu);
63d06c5c 1687
e7c27a73 1688static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1689
348e048f
DE
1690static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1691
e7c27a73 1692static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1693
e7c27a73 1694static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1695
96408a79
SA
1696static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1697
71a3c369
TT
1698static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1699
ff013f42 1700static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1701 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1702
3a2b436a 1703/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1704 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1705enum pc_bounds_kind
1706{
e385593e 1707 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1708 PC_BOUNDS_NOT_PRESENT,
1709
e385593e
JK
1710 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1711 were present but they do not form a valid range of PC addresses. */
1712 PC_BOUNDS_INVALID,
1713
3a2b436a
JK
1714 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1715 PC_BOUNDS_RANGES,
1716
1717 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1718 PC_BOUNDS_HIGH_LOW,
1719};
1720
1721static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1722 CORE_ADDR *, CORE_ADDR *,
1723 struct dwarf2_cu *,
891813be 1724 dwarf2_psymtab *);
c906108c 1725
fae299cd
DC
1726static void get_scope_pc_bounds (struct die_info *,
1727 CORE_ADDR *, CORE_ADDR *,
1728 struct dwarf2_cu *);
1729
801e3a5b
JB
1730static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1731 CORE_ADDR, struct dwarf2_cu *);
1732
a14ed312 1733static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1734 struct dwarf2_cu *);
c906108c 1735
a14ed312 1736static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1737 struct type *, struct dwarf2_cu *);
c906108c 1738
a14ed312 1739static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1740 struct die_info *, struct type *,
e7c27a73 1741 struct dwarf2_cu *);
c906108c 1742
a14ed312 1743static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1744 struct type *,
1745 struct dwarf2_cu *);
c906108c 1746
134d01f1 1747static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1748
e7c27a73 1749static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1750
e7c27a73 1751static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1752
5d7cb8df
JK
1753static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1754
804d2729 1755static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1756
27aa8d6a
SW
1757static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1758
74921315
KS
1759static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1760
f55ee35c
JK
1761static struct type *read_module_type (struct die_info *die,
1762 struct dwarf2_cu *cu);
1763
38d518c9 1764static const char *namespace_name (struct die_info *die,
e142c38c 1765 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1766
134d01f1 1767static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1768
e7c27a73 1769static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1770
6e70227d 1771static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1772 struct dwarf2_cu *);
1773
bf6af496 1774static struct die_info *read_die_and_siblings_1
d521ce57 1775 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1776 struct die_info *);
639d11d3 1777
dee91e82 1778static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1779 const gdb_byte *info_ptr,
1780 const gdb_byte **new_info_ptr,
639d11d3
DC
1781 struct die_info *parent);
1782
d521ce57
TT
1783static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1784 struct die_info **, const gdb_byte *,
1785 int *, int);
3019eac3 1786
d521ce57
TT
1787static const gdb_byte *read_full_die (const struct die_reader_specs *,
1788 struct die_info **, const gdb_byte *,
1789 int *);
93311388 1790
e7c27a73 1791static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1792
15d034d0
TT
1793static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1794 struct obstack *);
71c25dea 1795
15d034d0 1796static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1797
15d034d0 1798static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1799 struct die_info *die,
1800 struct dwarf2_cu *cu);
1801
ca69b9e6
DE
1802static const char *dwarf2_physname (const char *name, struct die_info *die,
1803 struct dwarf2_cu *cu);
1804
e142c38c 1805static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1806 struct dwarf2_cu **);
9219021c 1807
f39c6ffd 1808static const char *dwarf_tag_name (unsigned int);
c906108c 1809
f39c6ffd 1810static const char *dwarf_attr_name (unsigned int);
c906108c 1811
a084a2a6
AT
1812static const char *dwarf_unit_type_name (int unit_type);
1813
f39c6ffd 1814static const char *dwarf_form_name (unsigned int);
c906108c 1815
a121b7c1 1816static const char *dwarf_bool_name (unsigned int);
c906108c 1817
f39c6ffd 1818static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1819
f9aca02d 1820static struct die_info *sibling_die (struct die_info *);
c906108c 1821
d97bc12b
DE
1822static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1823
1824static void dump_die_for_error (struct die_info *);
1825
1826static void dump_die_1 (struct ui_file *, int level, int max_level,
1827 struct die_info *);
c906108c 1828
d97bc12b 1829/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1830
51545339 1831static void store_in_ref_table (struct die_info *,
10b3939b 1832 struct dwarf2_cu *);
c906108c 1833
ff39bb5e 1834static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1835
ff39bb5e 1836static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1837
348e048f 1838static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1839 const struct attribute *,
348e048f
DE
1840 struct dwarf2_cu **);
1841
10b3939b 1842static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1843 const struct attribute *,
f2f0e013 1844 struct dwarf2_cu **);
c906108c 1845
348e048f 1846static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1847 const struct attribute *,
348e048f
DE
1848 struct dwarf2_cu **);
1849
ac9ec31b
DE
1850static struct type *get_signatured_type (struct die_info *, ULONGEST,
1851 struct dwarf2_cu *);
1852
1853static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1854 const struct attribute *,
ac9ec31b
DE
1855 struct dwarf2_cu *);
1856
e5fe5e75 1857static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1858
52dc124a 1859static void read_signatured_type (struct signatured_type *);
348e048f 1860
63e43d3a
PMR
1861static int attr_to_dynamic_prop (const struct attribute *attr,
1862 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1863 struct dynamic_prop *prop, struct type *type);
63e43d3a 1864
c906108c
SS
1865/* memory allocation interface */
1866
7b5a2f43 1867static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1868
b60c80d6 1869static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1870
43f3e411 1871static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1872
6e5a29e1 1873static int attr_form_is_block (const struct attribute *);
8e19ed76 1874
6e5a29e1 1875static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1876
6e5a29e1 1877static int attr_form_is_constant (const struct attribute *);
3690dd37 1878
6e5a29e1 1879static int attr_form_is_ref (const struct attribute *);
7771576e 1880
8cf6f0b1
TT
1881static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1882 struct dwarf2_loclist_baton *baton,
ff39bb5e 1883 const struct attribute *attr);
8cf6f0b1 1884
ff39bb5e 1885static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1886 struct symbol *sym,
f1e6e072
TT
1887 struct dwarf2_cu *cu,
1888 int is_block);
4c2df51b 1889
d521ce57
TT
1890static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1891 const gdb_byte *info_ptr,
1892 struct abbrev_info *abbrev);
4bb7a0a7 1893
72bf9492
DJ
1894static hashval_t partial_die_hash (const void *item);
1895
1896static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1897
ae038cb0 1898static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1899 (sect_offset sect_off, unsigned int offset_in_dwz,
1900 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1901
9816fde3 1902static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1903 struct die_info *comp_unit_die,
1904 enum language pretend_language);
93311388 1905
ed2dc618 1906static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1907
dee91e82 1908static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1909
f792889a
DJ
1910static struct type *set_die_type (struct die_info *, struct type *,
1911 struct dwarf2_cu *);
1c379e20 1912
ed2dc618 1913static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1914
ed2dc618 1915static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1916
58f0c718 1917static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1918 enum language);
10b3939b 1919
95554aad
TT
1920static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1921 enum language);
10b3939b 1922
f4dc4d17
DE
1923static void process_full_type_unit (struct dwarf2_per_cu_data *,
1924 enum language);
1925
10b3939b
DJ
1926static void dwarf2_add_dependence (struct dwarf2_cu *,
1927 struct dwarf2_per_cu_data *);
1928
ae038cb0
DJ
1929static void dwarf2_mark (struct dwarf2_cu *);
1930
1931static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1932
b64f50a1 1933static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1934 struct dwarf2_per_cu_data *);
673bfd45 1935
f792889a 1936static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1937
95554aad
TT
1938static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1939 enum language pretend_language);
1940
ed2dc618 1941static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1942
9a49df9d
AB
1943static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1944static struct type *dwarf2_per_cu_addr_sized_int_type
1945 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1946static struct type *dwarf2_per_cu_int_type
1947 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1948 bool unsigned_p);
9a49df9d 1949
b303c6f6
AB
1950/* Class, the destructor of which frees all allocated queue entries. This
1951 will only have work to do if an error was thrown while processing the
1952 dwarf. If no error was thrown then the queue entries should have all
1953 been processed, and freed, as we went along. */
1954
1955class dwarf2_queue_guard
1956{
1957public:
1958 dwarf2_queue_guard () = default;
1959
1960 /* Free any entries remaining on the queue. There should only be
1961 entries left if we hit an error while processing the dwarf. */
1962 ~dwarf2_queue_guard ()
1963 {
1964 struct dwarf2_queue_item *item, *last;
1965
1966 item = dwarf2_queue;
1967 while (item)
1968 {
1969 /* Anything still marked queued is likely to be in an
1970 inconsistent state, so discard it. */
1971 if (item->per_cu->queued)
1972 {
1973 if (item->per_cu->cu != NULL)
1974 free_one_cached_comp_unit (item->per_cu);
1975 item->per_cu->queued = 0;
1976 }
1977
1978 last = item;
1979 item = item->next;
1980 xfree (last);
1981 }
1982
1983 dwarf2_queue = dwarf2_queue_tail = NULL;
1984 }
1985};
1986
d721ba37
PA
1987/* The return type of find_file_and_directory. Note, the enclosed
1988 string pointers are only valid while this object is valid. */
1989
1990struct file_and_directory
1991{
1992 /* The filename. This is never NULL. */
1993 const char *name;
1994
1995 /* The compilation directory. NULL if not known. If we needed to
1996 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1997 points directly to the DW_AT_comp_dir string attribute owned by
1998 the obstack that owns the DIE. */
1999 const char *comp_dir;
2000
2001 /* If we needed to build a new string for comp_dir, this is what
2002 owns the storage. */
2003 std::string comp_dir_storage;
2004};
2005
2006static file_and_directory find_file_and_directory (struct die_info *die,
2007 struct dwarf2_cu *cu);
9291a0cd
TT
2008
2009static char *file_full_name (int file, struct line_header *lh,
2010 const char *comp_dir);
2011
43988095
JK
2012/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2013enum class rcuh_kind { COMPILE, TYPE };
2014
d521ce57 2015static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
2016 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2017 struct comp_unit_head *header,
36586728 2018 struct dwarf2_section_info *section,
d521ce57 2019 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2020 rcuh_kind section_kind);
36586728 2021
673bfd45 2022static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2023
3019eac3
DE
2024static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2025
57d63ce2 2026static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2027 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2028 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2029 ULONGEST signature, int is_debug_types);
a2ce51a0 2030
ed2dc618
SM
2031static struct dwp_file *get_dwp_file
2032 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2033
3019eac3 2034static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2035 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2036
2037static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2038 (struct signatured_type *, const char *, const char *);
3019eac3 2039
89e63ee4
DE
2040static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2041
263db9a1
TT
2042/* A unique pointer to a dwo_file. */
2043
51ac9db5 2044typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2045
ed2dc618 2046static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2047
1b80a9fa 2048static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2049
2050static void free_line_header_voidp (void *arg);
4390d890
DE
2051\f
2052/* Various complaints about symbol reading that don't abort the process. */
2053
2054static void
2055dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2056{
b98664d3 2057 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2058}
2059
2060static void
2061dwarf2_debug_line_missing_file_complaint (void)
2062{
b98664d3 2063 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2064}
2065
2066static void
2067dwarf2_debug_line_missing_end_sequence_complaint (void)
2068{
b98664d3 2069 complaint (_(".debug_line section has line "
4390d890
DE
2070 "program sequence without an end"));
2071}
2072
2073static void
2074dwarf2_complex_location_expr_complaint (void)
2075{
b98664d3 2076 complaint (_("location expression too complex"));
4390d890
DE
2077}
2078
2079static void
2080dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2081 int arg3)
2082{
b98664d3 2083 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2084 arg1, arg2, arg3);
2085}
2086
2087static void
2088dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2089{
b98664d3 2090 complaint (_("debug info runs off end of %s section"
4390d890 2091 " [in module %s]"),
a32a8923
DE
2092 get_section_name (section),
2093 get_section_file_name (section));
4390d890 2094}
1b80a9fa 2095
4390d890
DE
2096static void
2097dwarf2_macro_malformed_definition_complaint (const char *arg1)
2098{
b98664d3 2099 complaint (_("macro debug info contains a "
4390d890
DE
2100 "malformed macro definition:\n`%s'"),
2101 arg1);
2102}
2103
2104static void
2105dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2106{
b98664d3 2107 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2108 arg1, arg2);
2109}
527f3840
JK
2110
2111/* Hash function for line_header_hash. */
2112
2113static hashval_t
2114line_header_hash (const struct line_header *ofs)
2115{
9c541725 2116 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2117}
2118
2119/* Hash function for htab_create_alloc_ex for line_header_hash. */
2120
2121static hashval_t
2122line_header_hash_voidp (const void *item)
2123{
9a3c8263 2124 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2125
2126 return line_header_hash (ofs);
2127}
2128
2129/* Equality function for line_header_hash. */
2130
2131static int
2132line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2133{
9a3c8263
SM
2134 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2135 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2136
9c541725 2137 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2138 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2139}
2140
4390d890 2141\f
9291a0cd 2142
31aa7e4e
JB
2143/* Read the given attribute value as an address, taking the attribute's
2144 form into account. */
2145
2146static CORE_ADDR
2147attr_value_as_address (struct attribute *attr)
2148{
2149 CORE_ADDR addr;
2150
336d760d
AT
2151 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2152 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2153 {
2154 /* Aside from a few clearly defined exceptions, attributes that
2155 contain an address must always be in DW_FORM_addr form.
2156 Unfortunately, some compilers happen to be violating this
2157 requirement by encoding addresses using other forms, such
2158 as DW_FORM_data4 for example. For those broken compilers,
2159 we try to do our best, without any guarantee of success,
2160 to interpret the address correctly. It would also be nice
2161 to generate a complaint, but that would require us to maintain
2162 a list of legitimate cases where a non-address form is allowed,
2163 as well as update callers to pass in at least the CU's DWARF
2164 version. This is more overhead than what we're willing to
2165 expand for a pretty rare case. */
2166 addr = DW_UNSND (attr);
2167 }
2168 else
2169 addr = DW_ADDR (attr);
2170
2171 return addr;
2172}
2173
330cdd98
PA
2174/* See declaration. */
2175
2176dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2177 const dwarf2_debug_sections *names,
2178 bool can_copy_)
2179 : objfile (objfile_),
2180 can_copy (can_copy_)
330cdd98
PA
2181{
2182 if (names == NULL)
2183 names = &dwarf2_elf_names;
2184
2185 bfd *obfd = objfile->obfd;
2186
2187 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2188 locate_sections (obfd, sec, *names);
2189}
2190
2191dwarf2_per_objfile::~dwarf2_per_objfile ()
2192{
2193 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2194 free_cached_comp_units ();
2195
2196 if (quick_file_names_table)
2197 htab_delete (quick_file_names_table);
2198
2199 if (line_header_hash)
2200 htab_delete (line_header_hash);
2201
b76e467d 2202 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2203 per_cu->imported_symtabs_free ();
fc8e7e75 2204
b2bdb8cf 2205 for (signatured_type *sig_type : all_type_units)
ae640021 2206 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2207
330cdd98
PA
2208 /* Everything else should be on the objfile obstack. */
2209}
2210
2211/* See declaration. */
2212
2213void
2214dwarf2_per_objfile::free_cached_comp_units ()
2215{
2216 dwarf2_per_cu_data *per_cu = read_in_chain;
2217 dwarf2_per_cu_data **last_chain = &read_in_chain;
2218 while (per_cu != NULL)
2219 {
2220 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2221
fcd3b13d 2222 delete per_cu->cu;
330cdd98
PA
2223 *last_chain = next_cu;
2224 per_cu = next_cu;
2225 }
2226}
2227
11ed8cad
TT
2228/* A helper class that calls free_cached_comp_units on
2229 destruction. */
2230
2231class free_cached_comp_units
2232{
2233public:
2234
2235 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2236 : m_per_objfile (per_objfile)
2237 {
2238 }
2239
2240 ~free_cached_comp_units ()
2241 {
2242 m_per_objfile->free_cached_comp_units ();
2243 }
2244
2245 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2246
2247private:
2248
2249 dwarf2_per_objfile *m_per_objfile;
2250};
2251
c906108c 2252/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2253 information and return true if we have enough to do something.
2254 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2255 ELF names are used. CAN_COPY is true for formats where symbol
2256 interposition is possible and so symbol values must follow copy
2257 relocation rules. */
c906108c
SS
2258
2259int
251d32d9 2260dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2261 const struct dwarf2_debug_sections *names,
2262 bool can_copy)
c906108c 2263{
97cbe998
SDJ
2264 if (objfile->flags & OBJF_READNEVER)
2265 return 0;
2266
ed2dc618
SM
2267 struct dwarf2_per_objfile *dwarf2_per_objfile
2268 = get_dwarf2_per_objfile (objfile);
2269
2270 if (dwarf2_per_objfile == NULL)
5bfd760d 2271 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2272 names,
2273 can_copy);
5bfd760d 2274
73869dc2 2275 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2276 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2277 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2278 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2279}
2280
2281/* Return the containing section of virtual section SECTION. */
2282
2283static struct dwarf2_section_info *
2284get_containing_section (const struct dwarf2_section_info *section)
2285{
2286 gdb_assert (section->is_virtual);
2287 return section->s.containing_section;
c906108c
SS
2288}
2289
a32a8923
DE
2290/* Return the bfd owner of SECTION. */
2291
2292static struct bfd *
2293get_section_bfd_owner (const struct dwarf2_section_info *section)
2294{
73869dc2
DE
2295 if (section->is_virtual)
2296 {
2297 section = get_containing_section (section);
2298 gdb_assert (!section->is_virtual);
2299 }
049412e3 2300 return section->s.section->owner;
a32a8923
DE
2301}
2302
2303/* Return the bfd section of SECTION.
2304 Returns NULL if the section is not present. */
2305
2306static asection *
2307get_section_bfd_section (const struct dwarf2_section_info *section)
2308{
73869dc2
DE
2309 if (section->is_virtual)
2310 {
2311 section = get_containing_section (section);
2312 gdb_assert (!section->is_virtual);
2313 }
049412e3 2314 return section->s.section;
a32a8923
DE
2315}
2316
2317/* Return the name of SECTION. */
2318
2319static const char *
2320get_section_name (const struct dwarf2_section_info *section)
2321{
2322 asection *sectp = get_section_bfd_section (section);
2323
2324 gdb_assert (sectp != NULL);
fd361982 2325 return bfd_section_name (sectp);
a32a8923
DE
2326}
2327
2328/* Return the name of the file SECTION is in. */
2329
2330static const char *
2331get_section_file_name (const struct dwarf2_section_info *section)
2332{
2333 bfd *abfd = get_section_bfd_owner (section);
2334
2335 return bfd_get_filename (abfd);
2336}
2337
2338/* Return the id of SECTION.
2339 Returns 0 if SECTION doesn't exist. */
2340
2341static int
2342get_section_id (const struct dwarf2_section_info *section)
2343{
2344 asection *sectp = get_section_bfd_section (section);
2345
2346 if (sectp == NULL)
2347 return 0;
2348 return sectp->id;
2349}
2350
2351/* Return the flags of SECTION.
73869dc2 2352 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2353
2354static int
2355get_section_flags (const struct dwarf2_section_info *section)
2356{
2357 asection *sectp = get_section_bfd_section (section);
2358
2359 gdb_assert (sectp != NULL);
fd361982 2360 return bfd_section_flags (sectp);
a32a8923
DE
2361}
2362
251d32d9
TG
2363/* When loading sections, we look either for uncompressed section or for
2364 compressed section names. */
233a11ab
CS
2365
2366static int
251d32d9
TG
2367section_is_p (const char *section_name,
2368 const struct dwarf2_section_names *names)
233a11ab 2369{
251d32d9
TG
2370 if (names->normal != NULL
2371 && strcmp (section_name, names->normal) == 0)
2372 return 1;
2373 if (names->compressed != NULL
2374 && strcmp (section_name, names->compressed) == 0)
2375 return 1;
2376 return 0;
233a11ab
CS
2377}
2378
330cdd98 2379/* See declaration. */
c906108c 2380
330cdd98
PA
2381void
2382dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2383 const dwarf2_debug_sections &names)
c906108c 2384{
fd361982 2385 flagword aflag = bfd_section_flags (sectp);
251d32d9 2386
dc7650b8
JK
2387 if ((aflag & SEC_HAS_CONTENTS) == 0)
2388 {
2389 }
950b7495
KS
2390 else if (elf_section_data (sectp)->this_hdr.sh_size
2391 > bfd_get_file_size (abfd))
2392 {
2393 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2394 warning (_("Discarding section %s which has a section size (%s"
2395 ") larger than the file size [in module %s]"),
2396 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2397 bfd_get_filename (abfd));
2398 }
330cdd98 2399 else if (section_is_p (sectp->name, &names.info))
c906108c 2400 {
330cdd98 2401 this->info.s.section = sectp;
fd361982 2402 this->info.size = bfd_section_size (sectp);
c906108c 2403 }
330cdd98 2404 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2405 {
330cdd98 2406 this->abbrev.s.section = sectp;
fd361982 2407 this->abbrev.size = bfd_section_size (sectp);
c906108c 2408 }
330cdd98 2409 else if (section_is_p (sectp->name, &names.line))
c906108c 2410 {
330cdd98 2411 this->line.s.section = sectp;
fd361982 2412 this->line.size = bfd_section_size (sectp);
c906108c 2413 }
330cdd98 2414 else if (section_is_p (sectp->name, &names.loc))
c906108c 2415 {
330cdd98 2416 this->loc.s.section = sectp;
fd361982 2417 this->loc.size = bfd_section_size (sectp);
c906108c 2418 }
330cdd98 2419 else if (section_is_p (sectp->name, &names.loclists))
43988095 2420 {
330cdd98 2421 this->loclists.s.section = sectp;
fd361982 2422 this->loclists.size = bfd_section_size (sectp);
43988095 2423 }
330cdd98 2424 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2425 {
330cdd98 2426 this->macinfo.s.section = sectp;
fd361982 2427 this->macinfo.size = bfd_section_size (sectp);
c906108c 2428 }
330cdd98 2429 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2430 {
330cdd98 2431 this->macro.s.section = sectp;
fd361982 2432 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2433 }
330cdd98 2434 else if (section_is_p (sectp->name, &names.str))
c906108c 2435 {
330cdd98 2436 this->str.s.section = sectp;
fd361982 2437 this->str.size = bfd_section_size (sectp);
c906108c 2438 }
18a8505e
AT
2439 else if (section_is_p (sectp->name, &names.str_offsets))
2440 {
2441 this->str_offsets.s.section = sectp;
2442 this->str_offsets.size = bfd_section_size (sectp);
2443 }
330cdd98 2444 else if (section_is_p (sectp->name, &names.line_str))
43988095 2445 {
330cdd98 2446 this->line_str.s.section = sectp;
fd361982 2447 this->line_str.size = bfd_section_size (sectp);
43988095 2448 }
330cdd98 2449 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2450 {
330cdd98 2451 this->addr.s.section = sectp;
fd361982 2452 this->addr.size = bfd_section_size (sectp);
3019eac3 2453 }
330cdd98 2454 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2455 {
330cdd98 2456 this->frame.s.section = sectp;
fd361982 2457 this->frame.size = bfd_section_size (sectp);
b6af0555 2458 }
330cdd98 2459 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2460 {
330cdd98 2461 this->eh_frame.s.section = sectp;
fd361982 2462 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2463 }
330cdd98 2464 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2465 {
330cdd98 2466 this->ranges.s.section = sectp;
fd361982 2467 this->ranges.size = bfd_section_size (sectp);
af34e669 2468 }
330cdd98 2469 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2470 {
330cdd98 2471 this->rnglists.s.section = sectp;
fd361982 2472 this->rnglists.size = bfd_section_size (sectp);
43988095 2473 }
330cdd98 2474 else if (section_is_p (sectp->name, &names.types))
348e048f 2475 {
8b70b953
TT
2476 struct dwarf2_section_info type_section;
2477
2478 memset (&type_section, 0, sizeof (type_section));
049412e3 2479 type_section.s.section = sectp;
fd361982 2480 type_section.size = bfd_section_size (sectp);
8b70b953 2481
fd5866f6 2482 this->types.push_back (type_section);
348e048f 2483 }
330cdd98 2484 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2485 {
330cdd98 2486 this->gdb_index.s.section = sectp;
fd361982 2487 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2488 }
927aa2e7
JK
2489 else if (section_is_p (sectp->name, &names.debug_names))
2490 {
2491 this->debug_names.s.section = sectp;
fd361982 2492 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2493 }
2494 else if (section_is_p (sectp->name, &names.debug_aranges))
2495 {
2496 this->debug_aranges.s.section = sectp;
fd361982 2497 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2498 }
dce234bc 2499
fd361982
AM
2500 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2501 && bfd_section_vma (sectp) == 0)
330cdd98 2502 this->has_section_at_zero = true;
c906108c
SS
2503}
2504
fceca515
DE
2505/* A helper function that decides whether a section is empty,
2506 or not present. */
9e0ac564
TT
2507
2508static int
19ac8c2e 2509dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2510{
73869dc2
DE
2511 if (section->is_virtual)
2512 return section->size == 0;
049412e3 2513 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2514}
2515
cd4fb1b2 2516/* See dwarf2read.h. */
c906108c 2517
cd4fb1b2
SM
2518void
2519dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2520{
a32a8923 2521 asection *sectp;
3019eac3 2522 bfd *abfd;
dce234bc 2523 gdb_byte *buf, *retbuf;
c906108c 2524
be391dca
TT
2525 if (info->readin)
2526 return;
dce234bc 2527 info->buffer = NULL;
dc4ccb6f 2528 info->readin = true;
188dd5d6 2529
9e0ac564 2530 if (dwarf2_section_empty_p (info))
dce234bc 2531 return;
c906108c 2532
a32a8923 2533 sectp = get_section_bfd_section (info);
3019eac3 2534
73869dc2
DE
2535 /* If this is a virtual section we need to read in the real one first. */
2536 if (info->is_virtual)
2537 {
2538 struct dwarf2_section_info *containing_section =
2539 get_containing_section (info);
2540
2541 gdb_assert (sectp != NULL);
2542 if ((sectp->flags & SEC_RELOC) != 0)
2543 {
2544 error (_("Dwarf Error: DWP format V2 with relocations is not"
2545 " supported in section %s [in module %s]"),
2546 get_section_name (info), get_section_file_name (info));
2547 }
2548 dwarf2_read_section (objfile, containing_section);
2549 /* Other code should have already caught virtual sections that don't
2550 fit. */
2551 gdb_assert (info->virtual_offset + info->size
2552 <= containing_section->size);
2553 /* If the real section is empty or there was a problem reading the
2554 section we shouldn't get here. */
2555 gdb_assert (containing_section->buffer != NULL);
2556 info->buffer = containing_section->buffer + info->virtual_offset;
2557 return;
2558 }
2559
4bf44c1c
TT
2560 /* If the section has relocations, we must read it ourselves.
2561 Otherwise we attach it to the BFD. */
2562 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2563 {
d521ce57 2564 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2565 return;
dce234bc 2566 }
dce234bc 2567
224c3ddb 2568 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2569 info->buffer = buf;
dce234bc
PP
2570
2571 /* When debugging .o files, we may need to apply relocations; see
2572 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2573 We never compress sections in .o files, so we only need to
2574 try this when the section is not compressed. */
ac8035ab 2575 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2576 if (retbuf != NULL)
2577 {
2578 info->buffer = retbuf;
2579 return;
2580 }
2581
a32a8923
DE
2582 abfd = get_section_bfd_owner (info);
2583 gdb_assert (abfd != NULL);
2584
dce234bc
PP
2585 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2586 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2587 {
2588 error (_("Dwarf Error: Can't read DWARF data"
2589 " in section %s [in module %s]"),
fd361982 2590 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2591 }
dce234bc
PP
2592}
2593
9e0ac564
TT
2594/* A helper function that returns the size of a section in a safe way.
2595 If you are positive that the section has been read before using the
2596 size, then it is safe to refer to the dwarf2_section_info object's
2597 "size" field directly. In other cases, you must call this
2598 function, because for compressed sections the size field is not set
2599 correctly until the section has been read. */
2600
2601static bfd_size_type
2602dwarf2_section_size (struct objfile *objfile,
2603 struct dwarf2_section_info *info)
2604{
2605 if (!info->readin)
2606 dwarf2_read_section (objfile, info);
2607 return info->size;
2608}
2609
dce234bc 2610/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2611 SECTION_NAME. */
af34e669 2612
dce234bc 2613void
3017a003
TG
2614dwarf2_get_section_info (struct objfile *objfile,
2615 enum dwarf2_section_enum sect,
d521ce57 2616 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2617 bfd_size_type *sizep)
2618{
5bfd760d 2619 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2620 struct dwarf2_section_info *info;
a3b2a86b
TT
2621
2622 /* We may see an objfile without any DWARF, in which case we just
2623 return nothing. */
2624 if (data == NULL)
2625 {
2626 *sectp = NULL;
2627 *bufp = NULL;
2628 *sizep = 0;
2629 return;
2630 }
3017a003
TG
2631 switch (sect)
2632 {
2633 case DWARF2_DEBUG_FRAME:
2634 info = &data->frame;
2635 break;
2636 case DWARF2_EH_FRAME:
2637 info = &data->eh_frame;
2638 break;
2639 default:
2640 gdb_assert_not_reached ("unexpected section");
2641 }
dce234bc 2642
9e0ac564 2643 dwarf2_read_section (objfile, info);
dce234bc 2644
a32a8923 2645 *sectp = get_section_bfd_section (info);
dce234bc
PP
2646 *bufp = info->buffer;
2647 *sizep = info->size;
2648}
2649
36586728
TT
2650/* A helper function to find the sections for a .dwz file. */
2651
2652static void
2653locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2654{
9a3c8263 2655 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2656
2657 /* Note that we only support the standard ELF names, because .dwz
2658 is ELF-only (at the time of writing). */
2659 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2660 {
049412e3 2661 dwz_file->abbrev.s.section = sectp;
fd361982 2662 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2663 }
2664 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2665 {
049412e3 2666 dwz_file->info.s.section = sectp;
fd361982 2667 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2668 }
2669 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2670 {
049412e3 2671 dwz_file->str.s.section = sectp;
fd361982 2672 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2673 }
2674 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2675 {
049412e3 2676 dwz_file->line.s.section = sectp;
fd361982 2677 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2678 }
2679 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2680 {
049412e3 2681 dwz_file->macro.s.section = sectp;
fd361982 2682 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2683 }
2ec9a5e0
TT
2684 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2685 {
049412e3 2686 dwz_file->gdb_index.s.section = sectp;
fd361982 2687 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2688 }
927aa2e7
JK
2689 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2690 {
2691 dwz_file->debug_names.s.section = sectp;
fd361982 2692 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2693 }
36586728
TT
2694}
2695
c4973306 2696/* See dwarf2read.h. */
36586728 2697
c4973306 2698struct dwz_file *
ed2dc618 2699dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2700{
36586728 2701 const char *filename;
acd13123 2702 bfd_size_type buildid_len_arg;
dc294be5
TT
2703 size_t buildid_len;
2704 bfd_byte *buildid;
36586728
TT
2705
2706 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2707 return dwarf2_per_objfile->dwz_file.get ();
36586728 2708
4db1a1dc 2709 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2710 gdb::unique_xmalloc_ptr<char> data
2711 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2712 &buildid_len_arg, &buildid));
4db1a1dc
TT
2713 if (data == NULL)
2714 {
2715 if (bfd_get_error () == bfd_error_no_error)
2716 return NULL;
2717 error (_("could not read '.gnu_debugaltlink' section: %s"),
2718 bfd_errmsg (bfd_get_error ()));
2719 }
791afaa2
TT
2720
2721 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2722
acd13123
TT
2723 buildid_len = (size_t) buildid_len_arg;
2724
791afaa2 2725 filename = data.get ();
d721ba37
PA
2726
2727 std::string abs_storage;
36586728
TT
2728 if (!IS_ABSOLUTE_PATH (filename))
2729 {
14278e1f
TT
2730 gdb::unique_xmalloc_ptr<char> abs
2731 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2732
14278e1f 2733 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2734 filename = abs_storage.c_str ();
36586728
TT
2735 }
2736
dc294be5
TT
2737 /* First try the file name given in the section. If that doesn't
2738 work, try to use the build-id instead. */
192b62ce 2739 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2740 if (dwz_bfd != NULL)
36586728 2741 {
192b62ce 2742 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2743 dwz_bfd.reset (nullptr);
36586728
TT
2744 }
2745
dc294be5
TT
2746 if (dwz_bfd == NULL)
2747 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2748
2749 if (dwz_bfd == NULL)
2750 error (_("could not find '.gnu_debugaltlink' file for %s"),
2751 objfile_name (dwarf2_per_objfile->objfile));
2752
7ff8cb8c
TT
2753 std::unique_ptr<struct dwz_file> result
2754 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2755
7ff8cb8c
TT
2756 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2757 result.get ());
36586728 2758
7ff8cb8c
TT
2759 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2760 result->dwz_bfd.get ());
2761 dwarf2_per_objfile->dwz_file = std::move (result);
2762 return dwarf2_per_objfile->dwz_file.get ();
36586728 2763}
9291a0cd 2764\f
7b9f3c50
DE
2765/* DWARF quick_symbols_functions support. */
2766
2767/* TUs can share .debug_line entries, and there can be a lot more TUs than
2768 unique line tables, so we maintain a separate table of all .debug_line
2769 derived entries to support the sharing.
2770 All the quick functions need is the list of file names. We discard the
2771 line_header when we're done and don't need to record it here. */
2772struct quick_file_names
2773{
094b34ac
DE
2774 /* The data used to construct the hash key. */
2775 struct stmt_list_hash hash;
7b9f3c50
DE
2776
2777 /* The number of entries in file_names, real_names. */
2778 unsigned int num_file_names;
2779
2780 /* The file names from the line table, after being run through
2781 file_full_name. */
2782 const char **file_names;
2783
2784 /* The file names from the line table after being run through
2785 gdb_realpath. These are computed lazily. */
2786 const char **real_names;
2787};
2788
2789/* When using the index (and thus not using psymtabs), each CU has an
2790 object of this type. This is used to hold information needed by
2791 the various "quick" methods. */
2792struct dwarf2_per_cu_quick_data
2793{
2794 /* The file table. This can be NULL if there was no file table
2795 or it's currently not read in.
2796 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2797 struct quick_file_names *file_names;
2798
2799 /* The corresponding symbol table. This is NULL if symbols for this
2800 CU have not yet been read. */
43f3e411 2801 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2802
2803 /* A temporary mark bit used when iterating over all CUs in
2804 expand_symtabs_matching. */
2805 unsigned int mark : 1;
2806
2807 /* True if we've tried to read the file table and found there isn't one.
2808 There will be no point in trying to read it again next time. */
2809 unsigned int no_file_data : 1;
2810};
2811
094b34ac
DE
2812/* Utility hash function for a stmt_list_hash. */
2813
2814static hashval_t
2815hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2816{
2817 hashval_t v = 0;
2818
2819 if (stmt_list_hash->dwo_unit != NULL)
2820 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2821 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2822 return v;
2823}
2824
2825/* Utility equality function for a stmt_list_hash. */
2826
2827static int
2828eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2829 const struct stmt_list_hash *rhs)
2830{
2831 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2832 return 0;
2833 if (lhs->dwo_unit != NULL
2834 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2835 return 0;
2836
9c541725 2837 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2838}
2839
7b9f3c50
DE
2840/* Hash function for a quick_file_names. */
2841
2842static hashval_t
2843hash_file_name_entry (const void *e)
2844{
9a3c8263
SM
2845 const struct quick_file_names *file_data
2846 = (const struct quick_file_names *) e;
7b9f3c50 2847
094b34ac 2848 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2849}
2850
2851/* Equality function for a quick_file_names. */
2852
2853static int
2854eq_file_name_entry (const void *a, const void *b)
2855{
9a3c8263
SM
2856 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2857 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2858
094b34ac 2859 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2860}
2861
2862/* Delete function for a quick_file_names. */
2863
2864static void
2865delete_file_name_entry (void *e)
2866{
9a3c8263 2867 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2868 int i;
2869
2870 for (i = 0; i < file_data->num_file_names; ++i)
2871 {
2872 xfree ((void*) file_data->file_names[i]);
2873 if (file_data->real_names)
2874 xfree ((void*) file_data->real_names[i]);
2875 }
2876
2877 /* The space for the struct itself lives on objfile_obstack,
2878 so we don't free it here. */
2879}
2880
2881/* Create a quick_file_names hash table. */
2882
2883static htab_t
2884create_quick_file_names_table (unsigned int nr_initial_entries)
2885{
2886 return htab_create_alloc (nr_initial_entries,
2887 hash_file_name_entry, eq_file_name_entry,
2888 delete_file_name_entry, xcalloc, xfree);
2889}
9291a0cd 2890
918dd910
JK
2891/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2892 have to be created afterwards. You should call age_cached_comp_units after
2893 processing PER_CU->CU. dw2_setup must have been already called. */
2894
2895static void
58f0c718 2896load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2897{
3019eac3 2898 if (per_cu->is_debug_types)
e5fe5e75 2899 load_full_type_unit (per_cu);
918dd910 2900 else
58f0c718 2901 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2902
cc12ce38
DE
2903 if (per_cu->cu == NULL)
2904 return; /* Dummy CU. */
2dc860c0
DE
2905
2906 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2907}
2908
a0f42c21 2909/* Read in the symbols for PER_CU. */
2fdf6df6 2910
9291a0cd 2911static void
58f0c718 2912dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2913{
ed2dc618 2914 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2915
f4dc4d17
DE
2916 /* Skip type_unit_groups, reading the type units they contain
2917 is handled elsewhere. */
2918 if (IS_TYPE_UNIT_GROUP (per_cu))
2919 return;
2920
b303c6f6
AB
2921 /* The destructor of dwarf2_queue_guard frees any entries left on
2922 the queue. After this point we're guaranteed to leave this function
2923 with the dwarf queue empty. */
2924 dwarf2_queue_guard q_guard;
9291a0cd 2925
95554aad 2926 if (dwarf2_per_objfile->using_index
43f3e411 2927 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2928 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2929 {
2930 queue_comp_unit (per_cu, language_minimal);
58f0c718 2931 load_cu (per_cu, skip_partial);
89e63ee4
DE
2932
2933 /* If we just loaded a CU from a DWO, and we're working with an index
2934 that may badly handle TUs, load all the TUs in that DWO as well.
2935 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2936 if (!per_cu->is_debug_types
cc12ce38 2937 && per_cu->cu != NULL
89e63ee4
DE
2938 && per_cu->cu->dwo_unit != NULL
2939 && dwarf2_per_objfile->index_table != NULL
2940 && dwarf2_per_objfile->index_table->version <= 7
2941 /* DWP files aren't supported yet. */
ed2dc618 2942 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2943 queue_and_load_all_dwo_tus (per_cu);
95554aad 2944 }
9291a0cd 2945
ed2dc618 2946 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2947
2948 /* Age the cache, releasing compilation units that have not
2949 been used recently. */
ed2dc618 2950 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2951}
2952
2953/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2954 the objfile from which this CU came. Returns the resulting symbol
2955 table. */
2fdf6df6 2956
43f3e411 2957static struct compunit_symtab *
58f0c718 2958dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2959{
ed2dc618
SM
2960 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2961
95554aad 2962 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2963 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2964 {
11ed8cad 2965 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2966 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2967 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2968 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2969 }
f194fefb 2970
43f3e411 2971 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2972}
2973
ff4c9fec 2974/* See declaration. */
f4dc4d17 2975
ff4c9fec
SM
2976dwarf2_per_cu_data *
2977dwarf2_per_objfile::get_cutu (int index)
2978{
b76e467d 2979 if (index >= this->all_comp_units.size ())
ff4c9fec 2980 {
b76e467d 2981 index -= this->all_comp_units.size ();
b2bdb8cf 2982 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2983 return &this->all_type_units[index]->per_cu;
2984 }
f4dc4d17 2985
ff4c9fec
SM
2986 return this->all_comp_units[index];
2987}
f4dc4d17 2988
ff4c9fec 2989/* See declaration. */
2fdf6df6 2990
ff4c9fec
SM
2991dwarf2_per_cu_data *
2992dwarf2_per_objfile::get_cu (int index)
1fd400ff 2993{
b76e467d 2994 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2995
ff4c9fec 2996 return this->all_comp_units[index];
f4dc4d17
DE
2997}
2998
ff4c9fec 2999/* See declaration. */
f4dc4d17 3000
ff4c9fec
SM
3001signatured_type *
3002dwarf2_per_objfile::get_tu (int index)
f4dc4d17 3003{
b2bdb8cf 3004 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 3005
ff4c9fec 3006 return this->all_type_units[index];
1fd400ff
TT
3007}
3008
4b514bc8
JK
3009/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3010 objfile_obstack, and constructed with the specified field
3011 values. */
3012
3013static dwarf2_per_cu_data *
ed2dc618 3014create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
3015 struct dwarf2_section_info *section,
3016 int is_dwz,
3017 sect_offset sect_off, ULONGEST length)
3018{
ed2dc618 3019 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
3020 dwarf2_per_cu_data *the_cu
3021 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3022 struct dwarf2_per_cu_data);
3023 the_cu->sect_off = sect_off;
3024 the_cu->length = length;
e3b94546 3025 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
3026 the_cu->section = section;
3027 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3028 struct dwarf2_per_cu_quick_data);
3029 the_cu->is_dwz = is_dwz;
3030 return the_cu;
3031}
3032
2ec9a5e0
TT
3033/* A helper for create_cus_from_index that handles a given list of
3034 CUs. */
2fdf6df6 3035
74a0d9f6 3036static void
12359b5e 3037create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3038 const gdb_byte *cu_list, offset_type n_elements,
3039 struct dwarf2_section_info *section,
b76e467d 3040 int is_dwz)
9291a0cd 3041{
12359b5e 3042 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3043 {
74a0d9f6 3044 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3045
3046 sect_offset sect_off
3047 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3048 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3049 cu_list += 2 * 8;
3050
b76e467d 3051 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3052 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3053 sect_off, length);
b76e467d 3054 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3055 }
9291a0cd
TT
3056}
3057
2ec9a5e0 3058/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3059 the CU objects for this objfile. */
2ec9a5e0 3060
74a0d9f6 3061static void
12359b5e 3062create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3063 const gdb_byte *cu_list, offset_type cu_list_elements,
3064 const gdb_byte *dwz_list, offset_type dwz_elements)
3065{
b76e467d
SM
3066 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3067 dwarf2_per_objfile->all_comp_units.reserve
3068 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3069
12359b5e 3070 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3071 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3072
3073 if (dwz_elements == 0)
74a0d9f6 3074 return;
2ec9a5e0 3075
12359b5e
SM
3076 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3077 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3078 &dwz->info, 1);
2ec9a5e0
TT
3079}
3080
1fd400ff 3081/* Create the signatured type hash table from the index. */
673bfd45 3082
74a0d9f6 3083static void
12359b5e
SM
3084create_signatured_type_table_from_index
3085 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3086 struct dwarf2_section_info *section,
3087 const gdb_byte *bytes,
3088 offset_type elements)
1fd400ff 3089{
12359b5e 3090 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3091
b2bdb8cf
SM
3092 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3093 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3094
12359b5e 3095 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3096
12359b5e 3097 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3098 {
52dc124a 3099 struct signatured_type *sig_type;
9c541725 3100 ULONGEST signature;
1fd400ff 3101 void **slot;
9c541725 3102 cu_offset type_offset_in_tu;
1fd400ff 3103
74a0d9f6 3104 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3105 sect_offset sect_off
3106 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3107 type_offset_in_tu
3108 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3109 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3110 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3111 bytes += 3 * 8;
3112
52dc124a 3113 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3114 struct signatured_type);
52dc124a 3115 sig_type->signature = signature;
9c541725 3116 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3117 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3118 sig_type->per_cu.section = section;
9c541725 3119 sig_type->per_cu.sect_off = sect_off;
e3b94546 3120 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3121 sig_type->per_cu.v.quick
1fd400ff
TT
3122 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3123 struct dwarf2_per_cu_quick_data);
3124
52dc124a
DE
3125 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3126 *slot = sig_type;
1fd400ff 3127
b2bdb8cf 3128 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3129 }
3130
673bfd45 3131 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3132}
3133
927aa2e7
JK
3134/* Create the signatured type hash table from .debug_names. */
3135
3136static void
3137create_signatured_type_table_from_debug_names
ed2dc618 3138 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3139 const mapped_debug_names &map,
3140 struct dwarf2_section_info *section,
3141 struct dwarf2_section_info *abbrev_section)
3142{
ed2dc618
SM
3143 struct objfile *objfile = dwarf2_per_objfile->objfile;
3144
927aa2e7
JK
3145 dwarf2_read_section (objfile, section);
3146 dwarf2_read_section (objfile, abbrev_section);
3147
b2bdb8cf
SM
3148 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3149 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3150
3151 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3152
3153 for (uint32_t i = 0; i < map.tu_count; ++i)
3154 {
3155 struct signatured_type *sig_type;
927aa2e7 3156 void **slot;
927aa2e7
JK
3157
3158 sect_offset sect_off
3159 = (sect_offset) (extract_unsigned_integer
3160 (map.tu_table_reordered + i * map.offset_size,
3161 map.offset_size,
3162 map.dwarf5_byte_order));
3163
3164 comp_unit_head cu_header;
ed2dc618
SM
3165 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3166 abbrev_section,
927aa2e7
JK
3167 section->buffer + to_underlying (sect_off),
3168 rcuh_kind::TYPE);
3169
3170 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3171 struct signatured_type);
3172 sig_type->signature = cu_header.signature;
3173 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3174 sig_type->per_cu.is_debug_types = 1;
3175 sig_type->per_cu.section = section;
3176 sig_type->per_cu.sect_off = sect_off;
e3b94546 3177 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3178 sig_type->per_cu.v.quick
3179 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3180 struct dwarf2_per_cu_quick_data);
3181
3182 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3183 *slot = sig_type;
3184
b2bdb8cf 3185 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3186 }
3187
3188 dwarf2_per_objfile->signatured_types = sig_types_hash;
3189}
3190
9291a0cd
TT
3191/* Read the address map data from the mapped index, and use it to
3192 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3193
9291a0cd 3194static void
ed2dc618
SM
3195create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3196 struct mapped_index *index)
9291a0cd 3197{
ed2dc618 3198 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3199 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3200 const gdb_byte *iter, *end;
9291a0cd 3201 struct addrmap *mutable_map;
9291a0cd
TT
3202 CORE_ADDR baseaddr;
3203
8268c778
PA
3204 auto_obstack temp_obstack;
3205
9291a0cd
TT
3206 mutable_map = addrmap_create_mutable (&temp_obstack);
3207
f00a2de2
PA
3208 iter = index->address_table.data ();
3209 end = iter + index->address_table.size ();
9291a0cd 3210
b3b3bada 3211 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
3212
3213 while (iter < end)
3214 {
3215 ULONGEST hi, lo, cu_index;
3216 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3217 iter += 8;
3218 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3219 iter += 8;
3220 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3221 iter += 4;
f652bce2 3222
24a55014 3223 if (lo > hi)
f652bce2 3224 {
b98664d3 3225 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3226 hex_string (lo), hex_string (hi));
24a55014 3227 continue;
f652bce2 3228 }
24a55014 3229
b76e467d 3230 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3231 {
b98664d3 3232 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3233 (unsigned) cu_index);
24a55014 3234 continue;
f652bce2 3235 }
24a55014 3236
79748972
TT
3237 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3238 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3239 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3240 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3241 }
3242
d320c2b5 3243 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3244 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3245}
3246
927aa2e7
JK
3247/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3248 populate the objfile's psymtabs_addrmap. */
3249
3250static void
ed2dc618 3251create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3252 struct dwarf2_section_info *section)
3253{
ed2dc618 3254 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3255 bfd *abfd = objfile->obfd;
3256 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 3257 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
3258
3259 auto_obstack temp_obstack;
3260 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3261
3262 std::unordered_map<sect_offset,
3263 dwarf2_per_cu_data *,
3264 gdb::hash_enum<sect_offset>>
3265 debug_info_offset_to_per_cu;
b76e467d 3266 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3267 {
927aa2e7
JK
3268 const auto insertpair
3269 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3270 if (!insertpair.second)
3271 {
3272 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3273 "debug_info_offset %s, ignoring .debug_aranges."),
3274 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3275 return;
3276 }
3277 }
3278
3279 dwarf2_read_section (objfile, section);
3280
3281 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3282
3283 const gdb_byte *addr = section->buffer;
3284
3285 while (addr < section->buffer + section->size)
3286 {
3287 const gdb_byte *const entry_addr = addr;
3288 unsigned int bytes_read;
3289
3290 const LONGEST entry_length = read_initial_length (abfd, addr,
3291 &bytes_read);
3292 addr += bytes_read;
3293
3294 const gdb_byte *const entry_end = addr + entry_length;
3295 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3296 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3297 if (addr + entry_length > section->buffer + section->size)
3298 {
47e3f474 3299 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3300 "length %s exceeds section length %s, "
3301 "ignoring .debug_aranges."),
47e3f474
TV
3302 objfile_name (objfile),
3303 plongest (entry_addr - section->buffer),
927aa2e7
JK
3304 plongest (bytes_read + entry_length),
3305 pulongest (section->size));
3306 return;
3307 }
3308
3309 /* The version number. */
3310 const uint16_t version = read_2_bytes (abfd, addr);
3311 addr += 2;
3312 if (version != 2)
3313 {
47e3f474 3314 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3315 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3316 objfile_name (objfile),
3317 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3318 return;
3319 }
3320
3321 const uint64_t debug_info_offset
3322 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3323 addr += offset_size;
3324 const auto per_cu_it
3325 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3326 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3327 {
47e3f474 3328 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3329 "debug_info_offset %s does not exists, "
3330 "ignoring .debug_aranges."),
47e3f474
TV
3331 objfile_name (objfile),
3332 plongest (entry_addr - section->buffer),
927aa2e7
JK
3333 pulongest (debug_info_offset));
3334 return;
3335 }
3336 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3337
3338 const uint8_t address_size = *addr++;
3339 if (address_size < 1 || address_size > 8)
3340 {
47e3f474 3341 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3342 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3343 objfile_name (objfile),
3344 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3345 return;
3346 }
3347
3348 const uint8_t segment_selector_size = *addr++;
3349 if (segment_selector_size != 0)
3350 {
47e3f474 3351 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3352 "segment_selector_size %u is not supported, "
3353 "ignoring .debug_aranges."),
47e3f474
TV
3354 objfile_name (objfile),
3355 plongest (entry_addr - section->buffer),
927aa2e7
JK
3356 segment_selector_size);
3357 return;
3358 }
3359
3360 /* Must pad to an alignment boundary that is twice the address
3361 size. It is undocumented by the DWARF standard but GCC does
3362 use it. */
3363 for (size_t padding = ((-(addr - section->buffer))
3364 & (2 * address_size - 1));
3365 padding > 0; padding--)
3366 if (*addr++ != 0)
3367 {
47e3f474 3368 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3369 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3370 objfile_name (objfile),
3371 plongest (entry_addr - section->buffer));
927aa2e7
JK
3372 return;
3373 }
3374
3375 for (;;)
3376 {
3377 if (addr + 2 * address_size > entry_end)
3378 {
47e3f474 3379 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3380 "address list is not properly terminated, "
3381 "ignoring .debug_aranges."),
47e3f474
TV
3382 objfile_name (objfile),
3383 plongest (entry_addr - section->buffer));
927aa2e7
JK
3384 return;
3385 }
3386 ULONGEST start = extract_unsigned_integer (addr, address_size,
3387 dwarf5_byte_order);
3388 addr += address_size;
3389 ULONGEST length = extract_unsigned_integer (addr, address_size,
3390 dwarf5_byte_order);
3391 addr += address_size;
3392 if (start == 0 && length == 0)
3393 break;
3394 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3395 {
3396 /* Symbol was eliminated due to a COMDAT group. */
3397 continue;
3398 }
3399 ULONGEST end = start + length;
79748972
TT
3400 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3401 - baseaddr);
3402 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3403 - baseaddr);
927aa2e7
JK
3404 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3405 }
3406 }
3407
d320c2b5 3408 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3409 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3410}
3411
9291a0cd
TT
3412/* Find a slot in the mapped index INDEX for the object named NAME.
3413 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3414 constant pool and return true. If NAME cannot be found, return
3415 false. */
2fdf6df6 3416
109483d9 3417static bool
9291a0cd
TT
3418find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3419 offset_type **vec_out)
3420{
0cf03b49 3421 offset_type hash;
9291a0cd 3422 offset_type slot, step;
559a7a62 3423 int (*cmp) (const char *, const char *);
9291a0cd 3424
791afaa2 3425 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3426 if (current_language->la_language == language_cplus
45280282
IB
3427 || current_language->la_language == language_fortran
3428 || current_language->la_language == language_d)
0cf03b49
JK
3429 {
3430 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3431 not contain any. */
a8719064 3432
72998fb3 3433 if (strchr (name, '(') != NULL)
0cf03b49 3434 {
109483d9 3435 without_params = cp_remove_params (name);
0cf03b49 3436
72998fb3 3437 if (without_params != NULL)
791afaa2 3438 name = without_params.get ();
0cf03b49
JK
3439 }
3440 }
3441
559a7a62 3442 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3443 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3444 simulate our NAME being searched is also lowercased. */
3445 hash = mapped_index_string_hash ((index->version == 4
3446 && case_sensitivity == case_sensitive_off
3447 ? 5 : index->version),
3448 name);
3449
f00a2de2
PA
3450 slot = hash & (index->symbol_table.size () - 1);
3451 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3452 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3453
3454 for (;;)
3455 {
9291a0cd 3456 const char *str;
f00a2de2
PA
3457
3458 const auto &bucket = index->symbol_table[slot];
3459 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3460 return false;
9291a0cd 3461
f00a2de2 3462 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3463 if (!cmp (name, str))
9291a0cd
TT
3464 {
3465 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3466 + MAYBE_SWAP (bucket.vec));
109483d9 3467 return true;
9291a0cd
TT
3468 }
3469
f00a2de2 3470 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3471 }
3472}
3473
4485a1c1
SM
3474/* A helper function that reads the .gdb_index from BUFFER and fills
3475 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3476 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3477 ok to use deprecated sections.
3478
3479 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3480 out parameters that are filled in with information about the CU and
3481 TU lists in the section.
3482
4485a1c1 3483 Returns true if all went well, false otherwise. */
2fdf6df6 3484
d33bc52e 3485static bool
4485a1c1
SM
3486read_gdb_index_from_buffer (struct objfile *objfile,
3487 const char *filename,
3488 bool deprecated_ok,
3489 gdb::array_view<const gdb_byte> buffer,
3490 struct mapped_index *map,
3491 const gdb_byte **cu_list,
3492 offset_type *cu_list_elements,
3493 const gdb_byte **types_list,
3494 offset_type *types_list_elements)
3495{
3496 const gdb_byte *addr = &buffer[0];
82430852 3497
9291a0cd 3498 /* Version check. */
4485a1c1 3499 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3500 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3501 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3502 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3503 indices. */
831adc1f 3504 if (version < 4)
481860b3
GB
3505 {
3506 static int warning_printed = 0;
3507 if (!warning_printed)
3508 {
3509 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3510 filename);
481860b3
GB
3511 warning_printed = 1;
3512 }
3513 return 0;
3514 }
3515 /* Index version 4 uses a different hash function than index version
3516 5 and later.
3517
3518 Versions earlier than 6 did not emit psymbols for inlined
3519 functions. Using these files will cause GDB not to be able to
3520 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3521 indices unless the user has done
3522 "set use-deprecated-index-sections on". */
2ec9a5e0 3523 if (version < 6 && !deprecated_ok)
481860b3
GB
3524 {
3525 static int warning_printed = 0;
3526 if (!warning_printed)
3527 {
e615022a
DE
3528 warning (_("\
3529Skipping deprecated .gdb_index section in %s.\n\
3530Do \"set use-deprecated-index-sections on\" before the file is read\n\
3531to use the section anyway."),
2ec9a5e0 3532 filename);
481860b3
GB
3533 warning_printed = 1;
3534 }
3535 return 0;
3536 }
796a7ff8 3537 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3538 of the TU (for symbols coming from TUs),
3539 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3540 Plus gold-generated indices can have duplicate entries for global symbols,
3541 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3542 These are just performance bugs, and we can't distinguish gdb-generated
3543 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3544
481860b3 3545 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3546 longer backward compatible. */
796a7ff8 3547 if (version > 8)
594e8718 3548 return 0;
9291a0cd 3549
559a7a62 3550 map->version = version;
9291a0cd 3551
4485a1c1 3552 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3553
4485a1c1 3554 int i = 0;
2ec9a5e0
TT
3555 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3556 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3557 / 8);
1fd400ff
TT
3558 ++i;
3559
2ec9a5e0
TT
3560 *types_list = addr + MAYBE_SWAP (metadata[i]);
3561 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3562 - MAYBE_SWAP (metadata[i]))
3563 / 8);
987d643c 3564 ++i;
1fd400ff 3565
f00a2de2
PA
3566 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3567 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3568 map->address_table
3569 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3570 ++i;
3571
f00a2de2
PA
3572 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3573 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3574 map->symbol_table
3575 = gdb::array_view<mapped_index::symbol_table_slot>
3576 ((mapped_index::symbol_table_slot *) symbol_table,
3577 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3578
f00a2de2 3579 ++i;
f9d83a0b 3580 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3581
2ec9a5e0
TT
3582 return 1;
3583}
3584
4485a1c1
SM
3585/* Callback types for dwarf2_read_gdb_index. */
3586
3587typedef gdb::function_view
3588 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3589 get_gdb_index_contents_ftype;
3590typedef gdb::function_view
3591 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3592 get_gdb_index_contents_dwz_ftype;
3593
927aa2e7 3594/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3595 elements of all the CUs and return 1. Otherwise, return 0. */
3596
3597static int
4485a1c1
SM
3598dwarf2_read_gdb_index
3599 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3600 get_gdb_index_contents_ftype get_gdb_index_contents,
3601 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3602{
2ec9a5e0
TT
3603 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3604 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3605 struct dwz_file *dwz;
12359b5e 3606 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3607
4485a1c1
SM
3608 gdb::array_view<const gdb_byte> main_index_contents
3609 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3610
3611 if (main_index_contents.empty ())
3612 return 0;
3613
3063847f 3614 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3615 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3616 use_deprecated_index_sections,
3617 main_index_contents, map.get (), &cu_list,
3618 &cu_list_elements, &types_list,
3619 &types_list_elements))
2ec9a5e0
TT
3620 return 0;
3621
0fefef59 3622 /* Don't use the index if it's empty. */
3063847f 3623 if (map->symbol_table.empty ())
0fefef59
DE
3624 return 0;
3625
2ec9a5e0
TT
3626 /* If there is a .dwz file, read it so we can get its CU list as
3627 well. */
ed2dc618 3628 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3629 if (dwz != NULL)
2ec9a5e0 3630 {
2ec9a5e0
TT
3631 struct mapped_index dwz_map;
3632 const gdb_byte *dwz_types_ignore;
3633 offset_type dwz_types_elements_ignore;
3634
4485a1c1
SM
3635 gdb::array_view<const gdb_byte> dwz_index_content
3636 = get_gdb_index_contents_dwz (objfile, dwz);
3637
3638 if (dwz_index_content.empty ())
3639 return 0;
3640
3641 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3642 bfd_get_filename (dwz->dwz_bfd.get ()),
3643 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3644 &dwz_list, &dwz_list_elements,
3645 &dwz_types_ignore,
3646 &dwz_types_elements_ignore))
2ec9a5e0
TT
3647 {
3648 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3649 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3650 return 0;
3651 }
3652 }
3653
12359b5e
SM
3654 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3655 dwz_list, dwz_list_elements);
1fd400ff 3656
8b70b953
TT
3657 if (types_list_elements)
3658 {
8b70b953
TT
3659 /* We can only handle a single .debug_types when we have an
3660 index. */
fd5866f6 3661 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3662 return 0;
3663
fd5866f6 3664 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3665
12359b5e
SM
3666 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3667 types_list, types_list_elements);
8b70b953 3668 }
9291a0cd 3669
3063847f 3670 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3671
3063847f 3672 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3673 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3674 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3675 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3676
3677 return 1;
3678}
3679
dee91e82 3680/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3681
dee91e82
DE
3682static void
3683dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3684 const gdb_byte *info_ptr,
dee91e82 3685 struct die_info *comp_unit_die,
c0ab21c2 3686 int has_children)
9291a0cd 3687{
dee91e82 3688 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3689 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3690 struct dwarf2_per_objfile *dwarf2_per_objfile
3691 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3692 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3693 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3694 struct attribute *attr;
7b9f3c50
DE
3695 void **slot;
3696 struct quick_file_names *qfn;
9291a0cd 3697
0186c6a7
DE
3698 gdb_assert (! this_cu->is_debug_types);
3699
07261596
TT
3700 /* Our callers never want to match partial units -- instead they
3701 will match the enclosing full CU. */
3702 if (comp_unit_die->tag == DW_TAG_partial_unit)
3703 {
3704 this_cu->v.quick->no_file_data = 1;
3705 return;
3706 }
3707
0186c6a7 3708 lh_cu = this_cu;
7b9f3c50 3709 slot = NULL;
dee91e82 3710
fff8551c 3711 line_header_up lh;
9c541725 3712 sect_offset line_offset {};
fff8551c 3713
dee91e82 3714 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3715 if (attr != nullptr)
9291a0cd 3716 {
7b9f3c50
DE
3717 struct quick_file_names find_entry;
3718
9c541725 3719 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3720
3721 /* We may have already read in this line header (TU line header sharing).
3722 If we have we're done. */
094b34ac 3723 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3724 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3725 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3726 &find_entry, INSERT);
3727 if (*slot != NULL)
3728 {
9a3c8263 3729 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3730 return;
7b9f3c50
DE
3731 }
3732
3019eac3 3733 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3734 }
3735 if (lh == NULL)
3736 {
094b34ac 3737 lh_cu->v.quick->no_file_data = 1;
dee91e82 3738 return;
9291a0cd
TT
3739 }
3740
8d749320 3741 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3742 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3743 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3744 gdb_assert (slot != NULL);
3745 *slot = qfn;
9291a0cd 3746
d721ba37 3747 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3748
aa391654
TT
3749 int offset = 0;
3750 if (strcmp (fnd.name, "<unknown>") != 0)
3751 ++offset;
3752
7ba99d21 3753 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3754 qfn->file_names =
aa391654
TT
3755 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3756 if (offset != 0)
3757 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3758 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3759 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3760 qfn->real_names = NULL;
9291a0cd 3761
094b34ac 3762 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3763}
3764
3765/* A helper for the "quick" functions which attempts to read the line
3766 table for THIS_CU. */
3767
3768static struct quick_file_names *
e4a48d9d 3769dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3770{
0186c6a7
DE
3771 /* This should never be called for TUs. */
3772 gdb_assert (! this_cu->is_debug_types);
3773 /* Nor type unit groups. */
3774 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3775
dee91e82
DE
3776 if (this_cu->v.quick->file_names != NULL)
3777 return this_cu->v.quick->file_names;
3778 /* If we know there is no line data, no point in looking again. */
3779 if (this_cu->v.quick->no_file_data)
3780 return NULL;
3781
c0ab21c2
TT
3782 cutu_reader reader (this_cu);
3783 if (!reader.dummy_p)
3784 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3785 reader.has_children);
dee91e82
DE
3786
3787 if (this_cu->v.quick->no_file_data)
3788 return NULL;
3789 return this_cu->v.quick->file_names;
9291a0cd
TT
3790}
3791
3792/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3793 real path for a given file name from the line table. */
2fdf6df6 3794
9291a0cd 3795static const char *
7b9f3c50
DE
3796dw2_get_real_path (struct objfile *objfile,
3797 struct quick_file_names *qfn, int index)
9291a0cd 3798{
7b9f3c50
DE
3799 if (qfn->real_names == NULL)
3800 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3801 qfn->num_file_names, const char *);
9291a0cd 3802
7b9f3c50 3803 if (qfn->real_names[index] == NULL)
14278e1f 3804 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3805
7b9f3c50 3806 return qfn->real_names[index];
9291a0cd
TT
3807}
3808
3809static struct symtab *
3810dw2_find_last_source_symtab (struct objfile *objfile)
3811{
ed2dc618
SM
3812 struct dwarf2_per_objfile *dwarf2_per_objfile
3813 = get_dwarf2_per_objfile (objfile);
b76e467d 3814 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3815 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3816
43f3e411
DE
3817 if (cust == NULL)
3818 return NULL;
ed2dc618 3819
43f3e411 3820 return compunit_primary_filetab (cust);
9291a0cd
TT
3821}
3822
7b9f3c50
DE
3823/* Traversal function for dw2_forget_cached_source_info. */
3824
3825static int
3826dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3827{
7b9f3c50 3828 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3829
7b9f3c50 3830 if (file_data->real_names)
9291a0cd 3831 {
7b9f3c50 3832 int i;
9291a0cd 3833
7b9f3c50 3834 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3835 {
7b9f3c50
DE
3836 xfree ((void*) file_data->real_names[i]);
3837 file_data->real_names[i] = NULL;
9291a0cd
TT
3838 }
3839 }
7b9f3c50
DE
3840
3841 return 1;
3842}
3843
3844static void
3845dw2_forget_cached_source_info (struct objfile *objfile)
3846{
ed2dc618
SM
3847 struct dwarf2_per_objfile *dwarf2_per_objfile
3848 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3849
3850 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3851 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3852}
3853
f8eba3c6
TT
3854/* Helper function for dw2_map_symtabs_matching_filename that expands
3855 the symtabs and calls the iterator. */
3856
3857static int
3858dw2_map_expand_apply (struct objfile *objfile,
3859 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3860 const char *name, const char *real_path,
14bc53a8 3861 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3862{
43f3e411 3863 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3864
3865 /* Don't visit already-expanded CUs. */
43f3e411 3866 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3867 return 0;
3868
3869 /* This may expand more than one symtab, and we want to iterate over
3870 all of them. */
58f0c718 3871 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3872
14bc53a8
PA
3873 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3874 last_made, callback);
f8eba3c6
TT
3875}
3876
3877/* Implementation of the map_symtabs_matching_filename method. */
3878
14bc53a8
PA
3879static bool
3880dw2_map_symtabs_matching_filename
3881 (struct objfile *objfile, const char *name, const char *real_path,
3882 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3883{
c011a4f4 3884 const char *name_basename = lbasename (name);
ed2dc618
SM
3885 struct dwarf2_per_objfile *dwarf2_per_objfile
3886 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3887
848e3e78
DE
3888 /* The rule is CUs specify all the files, including those used by
3889 any TU, so there's no need to scan TUs here. */
f4dc4d17 3890
b76e467d 3891 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3892 {
3d7bb9d9 3893 /* We only need to look at symtabs not already expanded. */
43f3e411 3894 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3895 continue;
3896
b76e467d 3897 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3898 if (file_data == NULL)
9291a0cd
TT
3899 continue;
3900
b76e467d 3901 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3902 {
7b9f3c50 3903 const char *this_name = file_data->file_names[j];
da235a7c 3904 const char *this_real_name;
9291a0cd 3905
af529f8f 3906 if (compare_filenames_for_search (this_name, name))
9291a0cd 3907 {
f5b95b50 3908 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3909 callback))
3910 return true;
288e77a7 3911 continue;
4aac40c8 3912 }
9291a0cd 3913
c011a4f4
DE
3914 /* Before we invoke realpath, which can get expensive when many
3915 files are involved, do a quick comparison of the basenames. */
3916 if (! basenames_may_differ
3917 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3918 continue;
3919
da235a7c
JK
3920 this_real_name = dw2_get_real_path (objfile, file_data, j);
3921 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3922 {
da235a7c 3923 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3924 callback))
3925 return true;
288e77a7 3926 continue;
da235a7c 3927 }
9291a0cd 3928
da235a7c
JK
3929 if (real_path != NULL)
3930 {
af529f8f
JK
3931 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3932 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3933 if (this_real_name != NULL
af529f8f 3934 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3935 {
f5b95b50 3936 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3937 callback))
3938 return true;
288e77a7 3939 continue;
9291a0cd
TT
3940 }
3941 }
3942 }
3943 }
3944
14bc53a8 3945 return false;
9291a0cd
TT
3946}
3947
da51c347
DE
3948/* Struct used to manage iterating over all CUs looking for a symbol. */
3949
3950struct dw2_symtab_iterator
9291a0cd 3951{
ed2dc618
SM
3952 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3953 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3954 /* If set, only look for symbols that match that block. Valid values are
3955 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3956 gdb::optional<block_enum> block_index;
da51c347
DE
3957 /* The kind of symbol we're looking for. */
3958 domain_enum domain;
3959 /* The list of CUs from the index entry of the symbol,
3960 or NULL if not found. */
3961 offset_type *vec;
3962 /* The next element in VEC to look at. */
3963 int next;
3964 /* The number of elements in VEC, or zero if there is no match. */
3965 int length;
8943b874
DE
3966 /* Have we seen a global version of the symbol?
3967 If so we can ignore all further global instances.
3968 This is to work around gold/15646, inefficient gold-generated
3969 indices. */
3970 int global_seen;
da51c347 3971};
9291a0cd 3972
2b79f376 3973/* Initialize the index symtab iterator ITER. */
2fdf6df6 3974
9291a0cd 3975static void
da51c347 3976dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3977 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3978 gdb::optional<block_enum> block_index,
da51c347
DE
3979 domain_enum domain,
3980 const char *name)
3981{
ed2dc618 3982 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3983 iter->block_index = block_index;
3984 iter->domain = domain;
3985 iter->next = 0;
8943b874 3986 iter->global_seen = 0;
da51c347 3987
3063847f 3988 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3989
3990 /* index is NULL if OBJF_READNOW. */
3991 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3992 iter->length = MAYBE_SWAP (*iter->vec);
3993 else
3994 {
3995 iter->vec = NULL;
3996 iter->length = 0;
3997 }
3998}
3999
4000/* Return the next matching CU or NULL if there are no more. */
4001
4002static struct dwarf2_per_cu_data *
4003dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4004{
ed2dc618
SM
4005 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4006
da51c347
DE
4007 for ( ; iter->next < iter->length; ++iter->next)
4008 {
4009 offset_type cu_index_and_attrs =
4010 MAYBE_SWAP (iter->vec[iter->next + 1]);
4011 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
4012 gdb_index_symbol_kind symbol_kind =
4013 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4014 /* Only check the symbol attributes if they're present.
4015 Indices prior to version 7 don't record them,
4016 and indices >= 7 may elide them for certain symbols
4017 (gold does this). */
4018 int attrs_valid =
ed2dc618 4019 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
4020 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4021
3190f0c6 4022 /* Don't crash on bad data. */
b76e467d 4023 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4024 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 4025 {
b98664d3 4026 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
4027 " [in module %s]"),
4028 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4029 continue;
4030 }
4031
ff4c9fec 4032 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 4033
da51c347 4034 /* Skip if already read in. */
43f3e411 4035 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4036 continue;
4037
8943b874
DE
4038 /* Check static vs global. */
4039 if (attrs_valid)
4040 {
2b79f376
SM
4041 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4042
4043 if (iter->block_index.has_value ())
4044 {
4045 bool want_static = *iter->block_index == STATIC_BLOCK;
4046
4047 if (is_static != want_static)
4048 continue;
4049 }
4050
8943b874
DE
4051 /* Work around gold/15646. */
4052 if (!is_static && iter->global_seen)
4053 continue;
4054 if (!is_static)
4055 iter->global_seen = 1;
4056 }
da51c347
DE
4057
4058 /* Only check the symbol's kind if it has one. */
4059 if (attrs_valid)
4060 {
4061 switch (iter->domain)
4062 {
4063 case VAR_DOMAIN:
4064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4065 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4066 /* Some types are also in VAR_DOMAIN. */
4067 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4068 continue;
4069 break;
4070 case STRUCT_DOMAIN:
4071 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4072 continue;
4073 break;
4074 case LABEL_DOMAIN:
4075 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4076 continue;
4077 break;
59c35742
AB
4078 case MODULE_DOMAIN:
4079 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4080 continue;
4081 break;
da51c347
DE
4082 default:
4083 break;
4084 }
4085 }
4086
4087 ++iter->next;
4088 return per_cu;
4089 }
4090
4091 return NULL;
4092}
4093
43f3e411 4094static struct compunit_symtab *
c7f839cb 4095dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4096 const char *name, domain_enum domain)
9291a0cd 4097{
43f3e411 4098 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4099 struct dwarf2_per_objfile *dwarf2_per_objfile
4100 = get_dwarf2_per_objfile (objfile);
9291a0cd 4101
b5ec771e
PA
4102 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4103
ed2dc618
SM
4104 struct dw2_symtab_iterator iter;
4105 struct dwarf2_per_cu_data *per_cu;
da51c347 4106
2b79f376 4107 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4108
ed2dc618
SM
4109 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4110 {
4111 struct symbol *sym, *with_opaque = NULL;
58f0c718 4112 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4113 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4114 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4115
ed2dc618
SM
4116 sym = block_find_symbol (block, name, domain,
4117 block_find_non_opaque_type_preferred,
4118 &with_opaque);
b2e2f908 4119
ed2dc618
SM
4120 /* Some caution must be observed with overloaded functions
4121 and methods, since the index will not contain any overload
4122 information (but NAME might contain it). */
da51c347 4123
ed2dc618
SM
4124 if (sym != NULL
4125 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4126 return stab;
4127 if (with_opaque != NULL
4128 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4129 stab_best = stab;
da51c347 4130
ed2dc618 4131 /* Keep looking through other CUs. */
9291a0cd 4132 }
9291a0cd 4133
da51c347 4134 return stab_best;
9291a0cd
TT
4135}
4136
4137static void
4138dw2_print_stats (struct objfile *objfile)
4139{
ed2dc618
SM
4140 struct dwarf2_per_objfile *dwarf2_per_objfile
4141 = get_dwarf2_per_objfile (objfile);
b76e467d 4142 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4143 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4144 int count = 0;
9291a0cd 4145
ed2dc618 4146 for (int i = 0; i < total; ++i)
9291a0cd 4147 {
ff4c9fec 4148 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4149
43f3e411 4150 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4151 ++count;
4152 }
e4a48d9d 4153 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4154 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4155}
4156
779bd270
DE
4157/* This dumps minimal information about the index.
4158 It is called via "mt print objfiles".
4159 One use is to verify .gdb_index has been loaded by the
4160 gdb.dwarf2/gdb-index.exp testcase. */
4161
9291a0cd
TT
4162static void
4163dw2_dump (struct objfile *objfile)
4164{
ed2dc618
SM
4165 struct dwarf2_per_objfile *dwarf2_per_objfile
4166 = get_dwarf2_per_objfile (objfile);
4167
779bd270
DE
4168 gdb_assert (dwarf2_per_objfile->using_index);
4169 printf_filtered (".gdb_index:");
4170 if (dwarf2_per_objfile->index_table != NULL)
4171 {
4172 printf_filtered (" version %d\n",
4173 dwarf2_per_objfile->index_table->version);
4174 }
4175 else
4176 printf_filtered (" faked for \"readnow\"\n");
4177 printf_filtered ("\n");
9291a0cd
TT
4178}
4179
9291a0cd
TT
4180static void
4181dw2_expand_symtabs_for_function (struct objfile *objfile,
4182 const char *func_name)
4183{
ed2dc618
SM
4184 struct dwarf2_per_objfile *dwarf2_per_objfile
4185 = get_dwarf2_per_objfile (objfile);
da51c347 4186
ed2dc618
SM
4187 struct dw2_symtab_iterator iter;
4188 struct dwarf2_per_cu_data *per_cu;
da51c347 4189
2b79f376 4190 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4191
ed2dc618 4192 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4193 dw2_instantiate_symtab (per_cu, false);
da51c347 4194
9291a0cd
TT
4195}
4196
4197static void
4198dw2_expand_all_symtabs (struct objfile *objfile)
4199{
ed2dc618
SM
4200 struct dwarf2_per_objfile *dwarf2_per_objfile
4201 = get_dwarf2_per_objfile (objfile);
b76e467d 4202 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4203 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4204
ed2dc618 4205 for (int i = 0; i < total_units; ++i)
9291a0cd 4206 {
ff4c9fec 4207 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4208
58f0c718
TT
4209 /* We don't want to directly expand a partial CU, because if we
4210 read it with the wrong language, then assertion failures can
4211 be triggered later on. See PR symtab/23010. So, tell
4212 dw2_instantiate_symtab to skip partial CUs -- any important
4213 partial CU will be read via DW_TAG_imported_unit anyway. */
4214 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4215 }
4216}
4217
4218static void
652a8996
JK
4219dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4220 const char *fullname)
9291a0cd 4221{
ed2dc618
SM
4222 struct dwarf2_per_objfile *dwarf2_per_objfile
4223 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4224
4225 /* We don't need to consider type units here.
4226 This is only called for examining code, e.g. expand_line_sal.
4227 There can be an order of magnitude (or more) more type units
4228 than comp units, and we avoid them if we can. */
4229
b76e467d 4230 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4231 {
3d7bb9d9 4232 /* We only need to look at symtabs not already expanded. */
43f3e411 4233 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4234 continue;
4235
b76e467d 4236 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4237 if (file_data == NULL)
9291a0cd
TT
4238 continue;
4239
b76e467d 4240 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4241 {
652a8996
JK
4242 const char *this_fullname = file_data->file_names[j];
4243
4244 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4245 {
58f0c718 4246 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4247 break;
4248 }
4249 }
4250 }
4251}
4252
9291a0cd 4253static void
199b4314
TT
4254dw2_map_matching_symbols
4255 (struct objfile *objfile,
b054970d 4256 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4257 int global,
4258 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4259 symbol_compare_ftype *ordered_compare)
9291a0cd 4260{
40658b94 4261 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4262 current language is Ada for a non-Ada objfile using GNU index. As Ada
4263 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4264}
4265
e1ef7d7a
PA
4266/* Starting from a search name, return the string that finds the upper
4267 bound of all strings that start with SEARCH_NAME in a sorted name
4268 list. Returns the empty string to indicate that the upper bound is
4269 the end of the list. */
4270
4271static std::string
4272make_sort_after_prefix_name (const char *search_name)
4273{
4274 /* When looking to complete "func", we find the upper bound of all
4275 symbols that start with "func" by looking for where we'd insert
4276 the closest string that would follow "func" in lexicographical
4277 order. Usually, that's "func"-with-last-character-incremented,
4278 i.e. "fund". Mind non-ASCII characters, though. Usually those
4279 will be UTF-8 multi-byte sequences, but we can't be certain.
4280 Especially mind the 0xff character, which is a valid character in
4281 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4282 rule out compilers allowing it in identifiers. Note that
4283 conveniently, strcmp/strcasecmp are specified to compare
4284 characters interpreted as unsigned char. So what we do is treat
4285 the whole string as a base 256 number composed of a sequence of
4286 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4287 to 0, and carries 1 to the following more-significant position.
4288 If the very first character in SEARCH_NAME ends up incremented
4289 and carries/overflows, then the upper bound is the end of the
4290 list. The string after the empty string is also the empty
4291 string.
4292
4293 Some examples of this operation:
4294
4295 SEARCH_NAME => "+1" RESULT
4296
4297 "abc" => "abd"
4298 "ab\xff" => "ac"
4299 "\xff" "a" "\xff" => "\xff" "b"
4300 "\xff" => ""
4301 "\xff\xff" => ""
4302 "" => ""
4303
4304 Then, with these symbols for example:
4305
4306 func
4307 func1
4308 fund
4309
4310 completing "func" looks for symbols between "func" and
4311 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4312 which finds "func" and "func1", but not "fund".
4313
4314 And with:
4315
4316 funcÿ (Latin1 'ÿ' [0xff])
4317 funcÿ1
4318 fund
4319
4320 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4321 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4322
4323 And with:
4324
4325 ÿÿ (Latin1 'ÿ' [0xff])
4326 ÿÿ1
4327
4328 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4329 the end of the list.
4330 */
4331 std::string after = search_name;
4332 while (!after.empty () && (unsigned char) after.back () == 0xff)
4333 after.pop_back ();
4334 if (!after.empty ())
4335 after.back () = (unsigned char) after.back () + 1;
4336 return after;
4337}
4338
5c58de74 4339/* See declaration. */
61d96d7e 4340
5c58de74
PA
4341std::pair<std::vector<name_component>::const_iterator,
4342 std::vector<name_component>::const_iterator>
44ed8f3e 4343mapped_index_base::find_name_components_bounds
3b00ef10 4344 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4345{
5c58de74
PA
4346 auto *name_cmp
4347 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4348
3b00ef10
TT
4349 const char *lang_name
4350 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4351
3f563c84
PA
4352 /* Comparison function object for lower_bound that matches against a
4353 given symbol name. */
4354 auto lookup_compare_lower = [&] (const name_component &elem,
4355 const char *name)
4356 {
5c58de74 4357 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4358 const char *elem_name = elem_qualified + elem.name_offset;
4359 return name_cmp (elem_name, name) < 0;
4360 };
4361
4362 /* Comparison function object for upper_bound that matches against a
4363 given symbol name. */
4364 auto lookup_compare_upper = [&] (const char *name,
4365 const name_component &elem)
4366 {
5c58de74 4367 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4368 const char *elem_name = elem_qualified + elem.name_offset;
4369 return name_cmp (name, elem_name) < 0;
4370 };
4371
5c58de74
PA
4372 auto begin = this->name_components.begin ();
4373 auto end = this->name_components.end ();
3f563c84
PA
4374
4375 /* Find the lower bound. */
4376 auto lower = [&] ()
4377 {
3b00ef10 4378 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4379 return begin;
4380 else
3b00ef10 4381 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4382 } ();
4383
4384 /* Find the upper bound. */
4385 auto upper = [&] ()
4386 {
5c58de74 4387 if (lookup_name_without_params.completion_mode ())
3f563c84 4388 {
e1ef7d7a
PA
4389 /* In completion mode, we want UPPER to point past all
4390 symbols names that have the same prefix. I.e., with
4391 these symbols, and completing "func":
4392
4393 function << lower bound
4394 function1
4395 other_function << upper bound
4396
4397 We find the upper bound by looking for the insertion
4398 point of "func"-with-last-character-incremented,
4399 i.e. "fund". */
3b00ef10 4400 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4401 if (after.empty ())
3f563c84 4402 return end;
e6b2f5ef
PA
4403 return std::lower_bound (lower, end, after.c_str (),
4404 lookup_compare_lower);
3f563c84
PA
4405 }
4406 else
3b00ef10 4407 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4408 } ();
4409
5c58de74
PA
4410 return {lower, upper};
4411}
4412
4413/* See declaration. */
4414
4415void
44ed8f3e 4416mapped_index_base::build_name_components ()
5c58de74
PA
4417{
4418 if (!this->name_components.empty ())
4419 return;
4420
4421 this->name_components_casing = case_sensitivity;
4422 auto *name_cmp
4423 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4424
4425 /* The code below only knows how to break apart components of C++
4426 symbol names (and other languages that use '::' as
3b00ef10 4427 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4428 auto count = this->symbol_name_count ();
4429 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4430 {
44ed8f3e 4431 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4432 continue;
4433
4434 const char *name = this->symbol_name_at (idx);
4435
4436 /* Add each name component to the name component table. */
4437 unsigned int previous_len = 0;
3b00ef10
TT
4438
4439 if (strstr (name, "::") != nullptr)
4440 {
4441 for (unsigned int current_len = cp_find_first_component (name);
4442 name[current_len] != '\0';
4443 current_len += cp_find_first_component (name + current_len))
4444 {
4445 gdb_assert (name[current_len] == ':');
4446 this->name_components.push_back ({previous_len, idx});
4447 /* Skip the '::'. */
4448 current_len += 2;
4449 previous_len = current_len;
4450 }
4451 }
4452 else
5c58de74 4453 {
3b00ef10
TT
4454 /* Handle the Ada encoded (aka mangled) form here. */
4455 for (const char *iter = strstr (name, "__");
4456 iter != nullptr;
4457 iter = strstr (iter, "__"))
4458 {
4459 this->name_components.push_back ({previous_len, idx});
4460 iter += 2;
4461 previous_len = iter - name;
4462 }
5c58de74 4463 }
3b00ef10 4464
5c58de74
PA
4465 this->name_components.push_back ({previous_len, idx});
4466 }
4467
4468 /* Sort name_components elements by name. */
4469 auto name_comp_compare = [&] (const name_component &left,
4470 const name_component &right)
4471 {
4472 const char *left_qualified = this->symbol_name_at (left.idx);
4473 const char *right_qualified = this->symbol_name_at (right.idx);
4474
4475 const char *left_name = left_qualified + left.name_offset;
4476 const char *right_name = right_qualified + right.name_offset;
4477
4478 return name_cmp (left_name, right_name) < 0;
4479 };
4480
4481 std::sort (this->name_components.begin (),
4482 this->name_components.end (),
4483 name_comp_compare);
4484}
4485
4486/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4487 mapped_index_base instead of the containing objfile. This is split
4488 to a separate function in order to be able to unit test the
4489 name_components matching using a mock mapped_index_base. For each
5c58de74 4490 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4491 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4492
4493static void
4494dw2_expand_symtabs_matching_symbol
44ed8f3e 4495 (mapped_index_base &index,
5c58de74
PA
4496 const lookup_name_info &lookup_name_in,
4497 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4498 enum search_domain kind,
3b00ef10 4499 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4500{
4501 lookup_name_info lookup_name_without_params
4502 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4503
4504 /* Build the symbol name component sorted vector, if we haven't
4505 yet. */
4506 index.build_name_components ();
4507
3f563c84
PA
4508 /* The same symbol may appear more than once in the range though.
4509 E.g., if we're looking for symbols that complete "w", and we have
4510 a symbol named "w1::w2", we'll find the two name components for
4511 that same symbol in the range. To be sure we only call the
4512 callback once per symbol, we first collect the symbol name
4513 indexes that matched in a temporary vector and ignore
4514 duplicates. */
4515 std::vector<offset_type> matches;
3f563c84 4516
3b00ef10
TT
4517 struct name_and_matcher
4518 {
4519 symbol_name_matcher_ftype *matcher;
4520 const std::string &name;
4521
4522 bool operator== (const name_and_matcher &other) const
3f563c84 4523 {
3b00ef10
TT
4524 return matcher == other.matcher && name == other.name;
4525 }
4526 };
4527
4528 /* A vector holding all the different symbol name matchers, for all
4529 languages. */
4530 std::vector<name_and_matcher> matchers;
4531
4532 for (int i = 0; i < nr_languages; i++)
4533 {
4534 enum language lang_e = (enum language) i;
4535
4536 const language_defn *lang = language_def (lang_e);
4537 symbol_name_matcher_ftype *name_matcher
4538 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4539
3b00ef10
TT
4540 name_and_matcher key {
4541 name_matcher,
4542 lookup_name_without_params.language_lookup_name (lang_e)
4543 };
4544
4545 /* Don't insert the same comparison routine more than once.
4546 Note that we do this linear walk. This is not a problem in
4547 practice because the number of supported languages is
4548 low. */
4549 if (std::find (matchers.begin (), matchers.end (), key)
4550 != matchers.end ())
9291a0cd 4551 continue;
3b00ef10
TT
4552 matchers.push_back (std::move (key));
4553
4554 auto bounds
4555 = index.find_name_components_bounds (lookup_name_without_params,
4556 lang_e);
4557
4558 /* Now for each symbol name in range, check to see if we have a name
4559 match, and if so, call the MATCH_CALLBACK callback. */
4560
4561 for (; bounds.first != bounds.second; ++bounds.first)
4562 {
4563 const char *qualified = index.symbol_name_at (bounds.first->idx);
4564
4565 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4566 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4567 continue;
9291a0cd 4568
3b00ef10
TT
4569 matches.push_back (bounds.first->idx);
4570 }
3f563c84
PA
4571 }
4572
4573 std::sort (matches.begin (), matches.end ());
4574
4575 /* Finally call the callback, once per match. */
4576 ULONGEST prev = -1;
4577 for (offset_type idx : matches)
4578 {
4579 if (prev != idx)
4580 {
3b00ef10
TT
4581 if (!match_callback (idx))
4582 break;
3f563c84
PA
4583 prev = idx;
4584 }
4585 }
4586
4587 /* Above we use a type wider than idx's for 'prev', since 0 and
4588 (offset_type)-1 are both possible values. */
4589 static_assert (sizeof (prev) > sizeof (offset_type), "");
4590}
4591
c62446b1
PA
4592#if GDB_SELF_TEST
4593
4594namespace selftests { namespace dw2_expand_symtabs_matching {
4595
a3c5fafd
PA
4596/* A mock .gdb_index/.debug_names-like name index table, enough to
4597 exercise dw2_expand_symtabs_matching_symbol, which works with the
4598 mapped_index_base interface. Builds an index from the symbol list
4599 passed as parameter to the constructor. */
4600class mock_mapped_index : public mapped_index_base
c62446b1
PA
4601{
4602public:
a3c5fafd
PA
4603 mock_mapped_index (gdb::array_view<const char *> symbols)
4604 : m_symbol_table (symbols)
c62446b1
PA
4605 {}
4606
a3c5fafd 4607 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4608
a3c5fafd 4609 /* Return the number of names in the symbol table. */
632e107b 4610 size_t symbol_name_count () const override
c62446b1 4611 {
a3c5fafd 4612 return m_symbol_table.size ();
c62446b1
PA
4613 }
4614
a3c5fafd 4615 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4616 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4617 {
4618 return m_symbol_table[idx];
4619 }
c62446b1 4620
a3c5fafd
PA
4621private:
4622 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4623};
4624
4625/* Convenience function that converts a NULL pointer to a "<null>"
4626 string, to pass to print routines. */
4627
4628static const char *
4629string_or_null (const char *str)
4630{
4631 return str != NULL ? str : "<null>";
4632}
4633
4634/* Check if a lookup_name_info built from
4635 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4636 index. EXPECTED_LIST is the list of expected matches, in expected
4637 matching order. If no match expected, then an empty list is
4638 specified. Returns true on success. On failure prints a warning
4639 indicating the file:line that failed, and returns false. */
4640
4641static bool
4642check_match (const char *file, int line,
4643 mock_mapped_index &mock_index,
4644 const char *name, symbol_name_match_type match_type,
4645 bool completion_mode,
4646 std::initializer_list<const char *> expected_list)
4647{
4648 lookup_name_info lookup_name (name, match_type, completion_mode);
4649
4650 bool matched = true;
4651
4652 auto mismatch = [&] (const char *expected_str,
4653 const char *got)
4654 {
4655 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4656 "expected=\"%s\", got=\"%s\"\n"),
4657 file, line,
4658 (match_type == symbol_name_match_type::FULL
4659 ? "FULL" : "WILD"),
4660 name, string_or_null (expected_str), string_or_null (got));
4661 matched = false;
4662 };
4663
4664 auto expected_it = expected_list.begin ();
4665 auto expected_end = expected_list.end ();
4666
a3c5fafd 4667 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4668 NULL, ALL_DOMAIN,
4669 [&] (offset_type idx)
4670 {
a3c5fafd 4671 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4672 const char *expected_str
4673 = expected_it == expected_end ? NULL : *expected_it++;
4674
4675 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4676 mismatch (expected_str, matched_name);
3b00ef10 4677 return true;
c62446b1
PA
4678 });
4679
4680 const char *expected_str
4681 = expected_it == expected_end ? NULL : *expected_it++;
4682 if (expected_str != NULL)
4683 mismatch (expected_str, NULL);
4684
4685 return matched;
4686}
4687
4688/* The symbols added to the mock mapped_index for testing (in
4689 canonical form). */
4690static const char *test_symbols[] = {
4691 "function",
4692 "std::bar",
4693 "std::zfunction",
4694 "std::zfunction2",
4695 "w1::w2",
4696 "ns::foo<char*>",
4697 "ns::foo<int>",
4698 "ns::foo<long>",
a20714ff
PA
4699 "ns2::tmpl<int>::foo2",
4700 "(anonymous namespace)::A::B::C",
c62446b1 4701
e1ef7d7a
PA
4702 /* These are used to check that the increment-last-char in the
4703 matching algorithm for completion doesn't match "t1_fund" when
4704 completing "t1_func". */
4705 "t1_func",
4706 "t1_func1",
4707 "t1_fund",
4708 "t1_fund1",
4709
4710 /* A UTF-8 name with multi-byte sequences to make sure that
4711 cp-name-parser understands this as a single identifier ("função"
4712 is "function" in PT). */
4713 u8"u8função",
4714
4715 /* \377 (0xff) is Latin1 'ÿ'. */
4716 "yfunc\377",
4717
4718 /* \377 (0xff) is Latin1 'ÿ'. */
4719 "\377",
4720 "\377\377123",
4721
c62446b1
PA
4722 /* A name with all sorts of complications. Starts with "z" to make
4723 it easier for the completion tests below. */
4724#define Z_SYM_NAME \
4725 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4726 "::tuple<(anonymous namespace)::ui*, " \
4727 "std::default_delete<(anonymous namespace)::ui>, void>"
4728
4729 Z_SYM_NAME
4730};
4731
a3c5fafd
PA
4732/* Returns true if the mapped_index_base::find_name_component_bounds
4733 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4734 in completion mode. */
5c58de74
PA
4735
4736static bool
a3c5fafd 4737check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4738 const char *search_name,
4739 gdb::array_view<const char *> expected_syms)
4740{
4741 lookup_name_info lookup_name (search_name,
4742 symbol_name_match_type::FULL, true);
4743
3b00ef10
TT
4744 auto bounds = index.find_name_components_bounds (lookup_name,
4745 language_cplus);
5c58de74
PA
4746
4747 size_t distance = std::distance (bounds.first, bounds.second);
4748 if (distance != expected_syms.size ())
4749 return false;
4750
4751 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4752 {
4753 auto nc_elem = bounds.first + exp_elem;
4754 const char *qualified = index.symbol_name_at (nc_elem->idx);
4755 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4756 return false;
4757 }
4758
4759 return true;
4760}
4761
4762/* Test the lower-level mapped_index::find_name_component_bounds
4763 method. */
4764
c62446b1 4765static void
5c58de74
PA
4766test_mapped_index_find_name_component_bounds ()
4767{
4768 mock_mapped_index mock_index (test_symbols);
4769
a3c5fafd 4770 mock_index.build_name_components ();
5c58de74
PA
4771
4772 /* Test the lower-level mapped_index::find_name_component_bounds
4773 method in completion mode. */
4774 {
4775 static const char *expected_syms[] = {
4776 "t1_func",
4777 "t1_func1",
5c58de74
PA
4778 };
4779
a3c5fafd 4780 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4781 "t1_func", expected_syms));
4782 }
4783
4784 /* Check that the increment-last-char in the name matching algorithm
4785 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4786 {
4787 static const char *expected_syms1[] = {
4788 "\377",
4789 "\377\377123",
4790 };
a3c5fafd 4791 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4792 "\377", expected_syms1));
4793
4794 static const char *expected_syms2[] = {
4795 "\377\377123",
4796 };
a3c5fafd 4797 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4798 "\377\377", expected_syms2));
4799 }
4800}
4801
4802/* Test dw2_expand_symtabs_matching_symbol. */
4803
4804static void
4805test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4806{
4807 mock_mapped_index mock_index (test_symbols);
4808
4809 /* We let all tests run until the end even if some fails, for debug
4810 convenience. */
4811 bool any_mismatch = false;
4812
4813 /* Create the expected symbols list (an initializer_list). Needed
4814 because lists have commas, and we need to pass them to CHECK,
4815 which is a macro. */
4816#define EXPECT(...) { __VA_ARGS__ }
4817
4818 /* Wrapper for check_match that passes down the current
4819 __FILE__/__LINE__. */
4820#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4821 any_mismatch |= !check_match (__FILE__, __LINE__, \
4822 mock_index, \
4823 NAME, MATCH_TYPE, COMPLETION_MODE, \
4824 EXPECTED_LIST)
4825
4826 /* Identity checks. */
4827 for (const char *sym : test_symbols)
4828 {
4829 /* Should be able to match all existing symbols. */
4830 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4831 EXPECT (sym));
4832
4833 /* Should be able to match all existing symbols with
4834 parameters. */
4835 std::string with_params = std::string (sym) + "(int)";
4836 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4837 EXPECT (sym));
4838
4839 /* Should be able to match all existing symbols with
4840 parameters and qualifiers. */
4841 with_params = std::string (sym) + " ( int ) const";
4842 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4843 EXPECT (sym));
4844
4845 /* This should really find sym, but cp-name-parser.y doesn't
4846 know about lvalue/rvalue qualifiers yet. */
4847 with_params = std::string (sym) + " ( int ) &&";
4848 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4849 {});
4850 }
4851
e1ef7d7a
PA
4852 /* Check that the name matching algorithm for completion doesn't get
4853 confused with Latin1 'ÿ' / 0xff. */
4854 {
4855 static const char str[] = "\377";
4856 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4857 EXPECT ("\377", "\377\377123"));
4858 }
4859
4860 /* Check that the increment-last-char in the matching algorithm for
4861 completion doesn't match "t1_fund" when completing "t1_func". */
4862 {
4863 static const char str[] = "t1_func";
4864 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4865 EXPECT ("t1_func", "t1_func1"));
4866 }
4867
c62446b1
PA
4868 /* Check that completion mode works at each prefix of the expected
4869 symbol name. */
4870 {
4871 static const char str[] = "function(int)";
4872 size_t len = strlen (str);
4873 std::string lookup;
4874
4875 for (size_t i = 1; i < len; i++)
4876 {
4877 lookup.assign (str, i);
4878 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4879 EXPECT ("function"));
4880 }
4881 }
4882
4883 /* While "w" is a prefix of both components, the match function
4884 should still only be called once. */
4885 {
4886 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4887 EXPECT ("w1::w2"));
a20714ff
PA
4888 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4889 EXPECT ("w1::w2"));
c62446b1
PA
4890 }
4891
4892 /* Same, with a "complicated" symbol. */
4893 {
4894 static const char str[] = Z_SYM_NAME;
4895 size_t len = strlen (str);
4896 std::string lookup;
4897
4898 for (size_t i = 1; i < len; i++)
4899 {
4900 lookup.assign (str, i);
4901 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4902 EXPECT (Z_SYM_NAME));
4903 }
4904 }
4905
4906 /* In FULL mode, an incomplete symbol doesn't match. */
4907 {
4908 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4909 {});
4910 }
4911
4912 /* A complete symbol with parameters matches any overload, since the
4913 index has no overload info. */
4914 {
4915 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4916 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4917 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4918 EXPECT ("std::zfunction", "std::zfunction2"));
4919 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4920 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4921 }
4922
4923 /* Check that whitespace is ignored appropriately. A symbol with a
4924 template argument list. */
4925 {
4926 static const char expected[] = "ns::foo<int>";
4927 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4928 EXPECT (expected));
a20714ff
PA
4929 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4930 EXPECT (expected));
c62446b1
PA
4931 }
4932
4933 /* Check that whitespace is ignored appropriately. A symbol with a
4934 template argument list that includes a pointer. */
4935 {
4936 static const char expected[] = "ns::foo<char*>";
4937 /* Try both completion and non-completion modes. */
4938 static const bool completion_mode[2] = {false, true};
4939 for (size_t i = 0; i < 2; i++)
4940 {
4941 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4942 completion_mode[i], EXPECT (expected));
a20714ff
PA
4943 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4944 completion_mode[i], EXPECT (expected));
c62446b1
PA
4945
4946 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4947 completion_mode[i], EXPECT (expected));
a20714ff
PA
4948 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4949 completion_mode[i], EXPECT (expected));
c62446b1
PA
4950 }
4951 }
4952
4953 {
4954 /* Check method qualifiers are ignored. */
4955 static const char expected[] = "ns::foo<char*>";
4956 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4957 symbol_name_match_type::FULL, true, EXPECT (expected));
4958 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4959 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4960 CHECK_MATCH ("foo < char * > ( int ) const",
4961 symbol_name_match_type::WILD, true, EXPECT (expected));
4962 CHECK_MATCH ("foo < char * > ( int ) &&",
4963 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4964 }
4965
4966 /* Test lookup names that don't match anything. */
4967 {
a20714ff
PA
4968 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4969 {});
4970
c62446b1
PA
4971 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4972 {});
4973 }
4974
a20714ff
PA
4975 /* Some wild matching tests, exercising "(anonymous namespace)",
4976 which should not be confused with a parameter list. */
4977 {
4978 static const char *syms[] = {
4979 "A::B::C",
4980 "B::C",
4981 "C",
4982 "A :: B :: C ( int )",
4983 "B :: C ( int )",
4984 "C ( int )",
4985 };
4986
4987 for (const char *s : syms)
4988 {
4989 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4990 EXPECT ("(anonymous namespace)::A::B::C"));
4991 }
4992 }
4993
4994 {
4995 static const char expected[] = "ns2::tmpl<int>::foo2";
4996 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4997 EXPECT (expected));
4998 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4999 EXPECT (expected));
5000 }
5001
c62446b1
PA
5002 SELF_CHECK (!any_mismatch);
5003
5004#undef EXPECT
5005#undef CHECK_MATCH
5006}
5007
5c58de74
PA
5008static void
5009run_test ()
5010{
5011 test_mapped_index_find_name_component_bounds ();
5012 test_dw2_expand_symtabs_matching_symbol ();
5013}
5014
c62446b1
PA
5015}} // namespace selftests::dw2_expand_symtabs_matching
5016
5017#endif /* GDB_SELF_TEST */
5018
4b514bc8
JK
5019/* If FILE_MATCHER is NULL or if PER_CU has
5020 dwarf2_per_cu_quick_data::MARK set (see
5021 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5022 EXPANSION_NOTIFY on it. */
5023
5024static void
5025dw2_expand_symtabs_matching_one
5026 (struct dwarf2_per_cu_data *per_cu,
5027 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5028 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5029{
5030 if (file_matcher == NULL || per_cu->v.quick->mark)
5031 {
5032 bool symtab_was_null
5033 = (per_cu->v.quick->compunit_symtab == NULL);
5034
58f0c718 5035 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5036
5037 if (expansion_notify != NULL
5038 && symtab_was_null
5039 && per_cu->v.quick->compunit_symtab != NULL)
5040 expansion_notify (per_cu->v.quick->compunit_symtab);
5041 }
5042}
5043
3f563c84
PA
5044/* Helper for dw2_expand_matching symtabs. Called on each symbol
5045 matched, to expand corresponding CUs that were marked. IDX is the
5046 index of the symbol name that matched. */
5047
5048static void
5049dw2_expand_marked_cus
ed2dc618 5050 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5051 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5052 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5053 search_domain kind)
5054{
3f563c84
PA
5055 offset_type *vec, vec_len, vec_idx;
5056 bool global_seen = false;
ed2dc618 5057 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5058
61920122 5059 vec = (offset_type *) (index.constant_pool
f00a2de2 5060 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5061 vec_len = MAYBE_SWAP (vec[0]);
5062 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5063 {
61920122
PA
5064 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5065 /* This value is only valid for index versions >= 7. */
5066 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5067 gdb_index_symbol_kind symbol_kind =
5068 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5069 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5070 /* Only check the symbol attributes if they're present.
5071 Indices prior to version 7 don't record them,
5072 and indices >= 7 may elide them for certain symbols
5073 (gold does this). */
5074 int attrs_valid =
5075 (index.version >= 7
5076 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5077
5078 /* Work around gold/15646. */
5079 if (attrs_valid)
9291a0cd 5080 {
61920122
PA
5081 if (!is_static && global_seen)
5082 continue;
5083 if (!is_static)
5084 global_seen = true;
5085 }
3190f0c6 5086
61920122
PA
5087 /* Only check the symbol's kind if it has one. */
5088 if (attrs_valid)
5089 {
5090 switch (kind)
8943b874 5091 {
61920122
PA
5092 case VARIABLES_DOMAIN:
5093 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5094 continue;
5095 break;
5096 case FUNCTIONS_DOMAIN:
5097 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5098 continue;
61920122
PA
5099 break;
5100 case TYPES_DOMAIN:
5101 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5102 continue;
5103 break;
59c35742
AB
5104 case MODULES_DOMAIN:
5105 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5106 continue;
5107 break;
61920122
PA
5108 default:
5109 break;
8943b874 5110 }
61920122 5111 }
8943b874 5112
61920122 5113 /* Don't crash on bad data. */
b76e467d 5114 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5115 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5116 {
b98664d3 5117 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5118 " [in module %s]"),
5119 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5120 continue;
5121 }
5122
ff4c9fec 5123 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5124 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5125 expansion_notify);
61920122
PA
5126 }
5127}
5128
4b514bc8
JK
5129/* If FILE_MATCHER is non-NULL, set all the
5130 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5131 that match FILE_MATCHER. */
5132
61920122 5133static void
4b514bc8 5134dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5135 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5136 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5137{
4b514bc8 5138 if (file_matcher == NULL)
61920122
PA
5139 return;
5140
4b514bc8
JK
5141 objfile *const objfile = dwarf2_per_objfile->objfile;
5142
5143 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5144 htab_eq_pointer,
5145 NULL, xcalloc, xfree));
5146 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5147 htab_eq_pointer,
5148 NULL, xcalloc, xfree));
61920122 5149
4b514bc8
JK
5150 /* The rule is CUs specify all the files, including those used by
5151 any TU, so there's no need to scan TUs here. */
61920122 5152
b76e467d 5153 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5154 {
927aa2e7
JK
5155 QUIT;
5156
5157 per_cu->v.quick->mark = 0;
5158
5159 /* We only need to look at symtabs not already expanded. */
5160 if (per_cu->v.quick->compunit_symtab)
5161 continue;
5162
b76e467d 5163 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5164 if (file_data == NULL)
5165 continue;
5166
5167 if (htab_find (visited_not_found.get (), file_data) != NULL)
5168 continue;
5169 else if (htab_find (visited_found.get (), file_data) != NULL)
5170 {
5171 per_cu->v.quick->mark = 1;
5172 continue;
5173 }
5174
b76e467d 5175 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5176 {
5177 const char *this_real_name;
5178
5179 if (file_matcher (file_data->file_names[j], false))
5180 {
5181 per_cu->v.quick->mark = 1;
5182 break;
5183 }
5184
5185 /* Before we invoke realpath, which can get expensive when many
5186 files are involved, do a quick comparison of the basenames. */
5187 if (!basenames_may_differ
5188 && !file_matcher (lbasename (file_data->file_names[j]),
5189 true))
5190 continue;
5191
5192 this_real_name = dw2_get_real_path (objfile, file_data, j);
5193 if (file_matcher (this_real_name, false))
5194 {
5195 per_cu->v.quick->mark = 1;
5196 break;
5197 }
5198 }
5199
b76e467d
SM
5200 void **slot = htab_find_slot (per_cu->v.quick->mark
5201 ? visited_found.get ()
5202 : visited_not_found.get (),
5203 file_data, INSERT);
927aa2e7
JK
5204 *slot = file_data;
5205 }
5206}
5207
5208static void
5209dw2_expand_symtabs_matching
5210 (struct objfile *objfile,
5211 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5212 const lookup_name_info &lookup_name,
5213 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5214 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5215 enum search_domain kind)
5216{
ed2dc618
SM
5217 struct dwarf2_per_objfile *dwarf2_per_objfile
5218 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5219
5220 /* index_table is NULL if OBJF_READNOW. */
5221 if (!dwarf2_per_objfile->index_table)
5222 return;
5223
ed2dc618 5224 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5225
5226 mapped_index &index = *dwarf2_per_objfile->index_table;
5227
5228 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5229 symbol_matcher,
5230 kind, [&] (offset_type idx)
5231 {
ed2dc618 5232 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5233 expansion_notify, kind);
3b00ef10 5234 return true;
927aa2e7
JK
5235 });
5236}
5237
5238/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5239 symtab. */
5240
5241static struct compunit_symtab *
5242recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5243 CORE_ADDR pc)
5244{
5245 int i;
5246
5247 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5248 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5249 return cust;
5250
5251 if (cust->includes == NULL)
5252 return NULL;
5253
5254 for (i = 0; cust->includes[i]; ++i)
5255 {
5256 struct compunit_symtab *s = cust->includes[i];
5257
5258 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5259 if (s != NULL)
5260 return s;
5261 }
5262
5263 return NULL;
5264}
5265
5266static struct compunit_symtab *
5267dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5268 struct bound_minimal_symbol msymbol,
5269 CORE_ADDR pc,
5270 struct obj_section *section,
5271 int warn_if_readin)
5272{
5273 struct dwarf2_per_cu_data *data;
5274 struct compunit_symtab *result;
5275
d320c2b5 5276 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5277 return NULL;
5278
b3b3bada 5279 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
5280 data = (struct dwarf2_per_cu_data *) addrmap_find
5281 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5282 if (!data)
5283 return NULL;
5284
5285 if (warn_if_readin && data->v.quick->compunit_symtab)
5286 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5287 paddress (get_objfile_arch (objfile), pc));
5288
5289 result
58f0c718
TT
5290 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5291 false),
927aa2e7
JK
5292 pc);
5293 gdb_assert (result != NULL);
5294 return result;
5295}
5296
5297static void
5298dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5299 void *data, int need_fullname)
5300{
ed2dc618
SM
5301 struct dwarf2_per_objfile *dwarf2_per_objfile
5302 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5303
5304 if (!dwarf2_per_objfile->filenames_cache)
5305 {
5306 dwarf2_per_objfile->filenames_cache.emplace ();
5307
5308 htab_up visited (htab_create_alloc (10,
5309 htab_hash_pointer, htab_eq_pointer,
5310 NULL, xcalloc, xfree));
5311
5312 /* The rule is CUs specify all the files, including those used
5313 by any TU, so there's no need to scan TUs here. We can
5314 ignore file names coming from already-expanded CUs. */
5315
b76e467d 5316 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5317 {
927aa2e7
JK
5318 if (per_cu->v.quick->compunit_symtab)
5319 {
5320 void **slot = htab_find_slot (visited.get (),
5321 per_cu->v.quick->file_names,
5322 INSERT);
5323
5324 *slot = per_cu->v.quick->file_names;
5325 }
5326 }
5327
b76e467d 5328 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5329 {
927aa2e7
JK
5330 /* We only need to look at symtabs not already expanded. */
5331 if (per_cu->v.quick->compunit_symtab)
5332 continue;
5333
b76e467d 5334 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5335 if (file_data == NULL)
5336 continue;
5337
b76e467d 5338 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5339 if (*slot)
5340 {
5341 /* Already visited. */
5342 continue;
5343 }
5344 *slot = file_data;
5345
5346 for (int j = 0; j < file_data->num_file_names; ++j)
5347 {
5348 const char *filename = file_data->file_names[j];
5349 dwarf2_per_objfile->filenames_cache->seen (filename);
5350 }
5351 }
5352 }
5353
5354 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5355 {
5356 gdb::unique_xmalloc_ptr<char> this_real_name;
5357
5358 if (need_fullname)
5359 this_real_name = gdb_realpath (filename);
5360 (*fun) (filename, this_real_name.get (), data);
5361 });
5362}
5363
5364static int
5365dw2_has_symbols (struct objfile *objfile)
5366{
5367 return 1;
5368}
5369
5370const struct quick_symbol_functions dwarf2_gdb_index_functions =
5371{
5372 dw2_has_symbols,
5373 dw2_find_last_source_symtab,
5374 dw2_forget_cached_source_info,
5375 dw2_map_symtabs_matching_filename,
5376 dw2_lookup_symbol,
5377 dw2_print_stats,
5378 dw2_dump,
927aa2e7
JK
5379 dw2_expand_symtabs_for_function,
5380 dw2_expand_all_symtabs,
5381 dw2_expand_symtabs_with_fullname,
5382 dw2_map_matching_symbols,
5383 dw2_expand_symtabs_matching,
5384 dw2_find_pc_sect_compunit_symtab,
5385 NULL,
5386 dw2_map_symbol_filenames
5387};
5388
5389/* DWARF-5 debug_names reader. */
5390
5391/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5392static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5393
5394/* A helper function that reads the .debug_names section in SECTION
5395 and fills in MAP. FILENAME is the name of the file containing the
5396 section; it is used for error reporting.
5397
5398 Returns true if all went well, false otherwise. */
5399
5400static bool
5401read_debug_names_from_section (struct objfile *objfile,
5402 const char *filename,
5403 struct dwarf2_section_info *section,
5404 mapped_debug_names &map)
5405{
5406 if (dwarf2_section_empty_p (section))
5407 return false;
5408
5409 /* Older elfutils strip versions could keep the section in the main
5410 executable while splitting it for the separate debug info file. */
5411 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5412 return false;
5413
5414 dwarf2_read_section (objfile, section);
5415
5416 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5417
5418 const gdb_byte *addr = section->buffer;
5419
5420 bfd *const abfd = get_section_bfd_owner (section);
5421
5422 unsigned int bytes_read;
5423 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5424 addr += bytes_read;
5425
5426 map.dwarf5_is_dwarf64 = bytes_read != 4;
5427 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5428 if (bytes_read + length != section->size)
5429 {
5430 /* There may be multiple per-CU indices. */
5431 warning (_("Section .debug_names in %s length %s does not match "
5432 "section length %s, ignoring .debug_names."),
5433 filename, plongest (bytes_read + length),
5434 pulongest (section->size));
5435 return false;
5436 }
5437
5438 /* The version number. */
5439 uint16_t version = read_2_bytes (abfd, addr);
5440 addr += 2;
5441 if (version != 5)
5442 {
5443 warning (_("Section .debug_names in %s has unsupported version %d, "
5444 "ignoring .debug_names."),
5445 filename, version);
5446 return false;
5447 }
5448
5449 /* Padding. */
5450 uint16_t padding = read_2_bytes (abfd, addr);
5451 addr += 2;
5452 if (padding != 0)
5453 {
5454 warning (_("Section .debug_names in %s has unsupported padding %d, "
5455 "ignoring .debug_names."),
5456 filename, padding);
5457 return false;
5458 }
5459
5460 /* comp_unit_count - The number of CUs in the CU list. */
5461 map.cu_count = read_4_bytes (abfd, addr);
5462 addr += 4;
5463
5464 /* local_type_unit_count - The number of TUs in the local TU
5465 list. */
5466 map.tu_count = read_4_bytes (abfd, addr);
5467 addr += 4;
5468
5469 /* foreign_type_unit_count - The number of TUs in the foreign TU
5470 list. */
5471 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5472 addr += 4;
5473 if (foreign_tu_count != 0)
5474 {
5475 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5476 "ignoring .debug_names."),
5477 filename, static_cast<unsigned long> (foreign_tu_count));
5478 return false;
5479 }
5480
5481 /* bucket_count - The number of hash buckets in the hash lookup
5482 table. */
5483 map.bucket_count = read_4_bytes (abfd, addr);
5484 addr += 4;
5485
5486 /* name_count - The number of unique names in the index. */
5487 map.name_count = read_4_bytes (abfd, addr);
5488 addr += 4;
5489
5490 /* abbrev_table_size - The size in bytes of the abbreviations
5491 table. */
5492 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5493 addr += 4;
5494
5495 /* augmentation_string_size - The size in bytes of the augmentation
5496 string. This value is rounded up to a multiple of 4. */
5497 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5498 addr += 4;
5499 map.augmentation_is_gdb = ((augmentation_string_size
5500 == sizeof (dwarf5_augmentation))
5501 && memcmp (addr, dwarf5_augmentation,
5502 sizeof (dwarf5_augmentation)) == 0);
5503 augmentation_string_size += (-augmentation_string_size) & 3;
5504 addr += augmentation_string_size;
5505
5506 /* List of CUs */
5507 map.cu_table_reordered = addr;
5508 addr += map.cu_count * map.offset_size;
5509
5510 /* List of Local TUs */
5511 map.tu_table_reordered = addr;
5512 addr += map.tu_count * map.offset_size;
5513
5514 /* Hash Lookup Table */
5515 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5516 addr += map.bucket_count * 4;
5517 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5518 addr += map.name_count * 4;
5519
5520 /* Name Table */
5521 map.name_table_string_offs_reordered = addr;
5522 addr += map.name_count * map.offset_size;
5523 map.name_table_entry_offs_reordered = addr;
5524 addr += map.name_count * map.offset_size;
5525
5526 const gdb_byte *abbrev_table_start = addr;
5527 for (;;)
5528 {
927aa2e7
JK
5529 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5530 addr += bytes_read;
5531 if (index_num == 0)
5532 break;
5533
5534 const auto insertpair
5535 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5536 if (!insertpair.second)
5537 {
5538 warning (_("Section .debug_names in %s has duplicate index %s, "
5539 "ignoring .debug_names."),
5540 filename, pulongest (index_num));
5541 return false;
5542 }
5543 mapped_debug_names::index_val &indexval = insertpair.first->second;
5544 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5545 addr += bytes_read;
5546
5547 for (;;)
5548 {
5549 mapped_debug_names::index_val::attr attr;
5550 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5551 addr += bytes_read;
5552 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5553 addr += bytes_read;
5554 if (attr.form == DW_FORM_implicit_const)
5555 {
5556 attr.implicit_const = read_signed_leb128 (abfd, addr,
5557 &bytes_read);
5558 addr += bytes_read;
5559 }
5560 if (attr.dw_idx == 0 && attr.form == 0)
5561 break;
5562 indexval.attr_vec.push_back (std::move (attr));
5563 }
5564 }
5565 if (addr != abbrev_table_start + abbrev_table_size)
5566 {
5567 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5568 "of size %s vs. written as %u, ignoring .debug_names."),
5569 filename, plongest (addr - abbrev_table_start),
5570 abbrev_table_size);
927aa2e7
JK
5571 return false;
5572 }
5573 map.entry_pool = addr;
5574
5575 return true;
5576}
5577
5578/* A helper for create_cus_from_debug_names that handles the MAP's CU
5579 list. */
5580
5581static void
ed2dc618 5582create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5583 const mapped_debug_names &map,
5584 dwarf2_section_info &section,
b76e467d 5585 bool is_dwz)
927aa2e7
JK
5586{
5587 sect_offset sect_off_prev;
5588 for (uint32_t i = 0; i <= map.cu_count; ++i)
5589 {
5590 sect_offset sect_off_next;
5591 if (i < map.cu_count)
5592 {
5593 sect_off_next
5594 = (sect_offset) (extract_unsigned_integer
5595 (map.cu_table_reordered + i * map.offset_size,
5596 map.offset_size,
5597 map.dwarf5_byte_order));
5598 }
5599 else
5600 sect_off_next = (sect_offset) section.size;
5601 if (i >= 1)
5602 {
5603 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5604 dwarf2_per_cu_data *per_cu
ed2dc618 5605 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5606 sect_off_prev, length);
b76e467d 5607 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5608 }
5609 sect_off_prev = sect_off_next;
5610 }
5611}
5612
5613/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5614 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5615
5616static void
ed2dc618 5617create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5618 const mapped_debug_names &map,
5619 const mapped_debug_names &dwz_map)
5620{
b76e467d
SM
5621 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5622 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5623
ed2dc618
SM
5624 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5625 dwarf2_per_objfile->info,
b76e467d 5626 false /* is_dwz */);
927aa2e7
JK
5627
5628 if (dwz_map.cu_count == 0)
5629 return;
5630
ed2dc618
SM
5631 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5632 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5633 true /* is_dwz */);
927aa2e7
JK
5634}
5635
5636/* Read .debug_names. If everything went ok, initialize the "quick"
5637 elements of all the CUs and return true. Otherwise, return false. */
5638
5639static bool
ed2dc618 5640dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5641{
22ca247e
TT
5642 std::unique_ptr<mapped_debug_names> map
5643 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5644 mapped_debug_names dwz_map (dwarf2_per_objfile);
5645 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5646
5647 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5648 &dwarf2_per_objfile->debug_names,
22ca247e 5649 *map))
927aa2e7
JK
5650 return false;
5651
5652 /* Don't use the index if it's empty. */
22ca247e 5653 if (map->name_count == 0)
927aa2e7
JK
5654 return false;
5655
5656 /* If there is a .dwz file, read it so we can get its CU list as
5657 well. */
ed2dc618 5658 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5659 if (dwz != NULL)
5660 {
5661 if (!read_debug_names_from_section (objfile,
00f93c44 5662 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5663 &dwz->debug_names, dwz_map))
5664 {
5665 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5666 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5667 return false;
5668 }
5669 }
5670
22ca247e 5671 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5672
22ca247e 5673 if (map->tu_count != 0)
927aa2e7
JK
5674 {
5675 /* We can only handle a single .debug_types when we have an
5676 index. */
fd5866f6 5677 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5678 return false;
5679
fd5866f6 5680 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5681
5682 create_signatured_type_table_from_debug_names
22ca247e 5683 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5684 }
5685
ed2dc618
SM
5686 create_addrmap_from_aranges (dwarf2_per_objfile,
5687 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5688
22ca247e 5689 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5690 dwarf2_per_objfile->using_index = 1;
5691 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5692 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5693
5694 return true;
5695}
5696
927aa2e7
JK
5697/* Type used to manage iterating over all CUs looking for a symbol for
5698 .debug_names. */
5699
5700class dw2_debug_names_iterator
5701{
5702public:
927aa2e7 5703 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5704 gdb::optional<block_enum> block_index,
5705 domain_enum domain,
927aa2e7 5706 const char *name)
2b79f376 5707 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5708 m_addr (find_vec_in_debug_names (map, name))
5709 {}
5710
5711 dw2_debug_names_iterator (const mapped_debug_names &map,
5712 search_domain search, uint32_t namei)
5713 : m_map (map),
5714 m_search (search),
5715 m_addr (find_vec_in_debug_names (map, namei))
5716 {}
5717
3b00ef10
TT
5718 dw2_debug_names_iterator (const mapped_debug_names &map,
5719 block_enum block_index, domain_enum domain,
5720 uint32_t namei)
5721 : m_map (map), m_block_index (block_index), m_domain (domain),
5722 m_addr (find_vec_in_debug_names (map, namei))
5723 {}
5724
927aa2e7
JK
5725 /* Return the next matching CU or NULL if there are no more. */
5726 dwarf2_per_cu_data *next ();
5727
5728private:
5729 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5730 const char *name);
5731 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5732 uint32_t namei);
5733
5734 /* The internalized form of .debug_names. */
5735 const mapped_debug_names &m_map;
5736
2b79f376
SM
5737 /* If set, only look for symbols that match that block. Valid values are
5738 GLOBAL_BLOCK and STATIC_BLOCK. */
5739 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5740
5741 /* The kind of symbol we're looking for. */
5742 const domain_enum m_domain = UNDEF_DOMAIN;
5743 const search_domain m_search = ALL_DOMAIN;
5744
5745 /* The list of CUs from the index entry of the symbol, or NULL if
5746 not found. */
5747 const gdb_byte *m_addr;
5748};
5749
5750const char *
5751mapped_debug_names::namei_to_name (uint32_t namei) const
5752{
5753 const ULONGEST namei_string_offs
5754 = extract_unsigned_integer ((name_table_string_offs_reordered
5755 + namei * offset_size),
5756 offset_size,
5757 dwarf5_byte_order);
5758 return read_indirect_string_at_offset
ed2dc618 5759 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5760}
5761
5762/* Find a slot in .debug_names for the object named NAME. If NAME is
5763 found, return pointer to its pool data. If NAME cannot be found,
5764 return NULL. */
5765
5766const gdb_byte *
5767dw2_debug_names_iterator::find_vec_in_debug_names
5768 (const mapped_debug_names &map, const char *name)
5769{
5770 int (*cmp) (const char *, const char *);
5771
54ee4252 5772 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5773 if (current_language->la_language == language_cplus
5774 || current_language->la_language == language_fortran
5775 || current_language->la_language == language_d)
5776 {
5777 /* NAME is already canonical. Drop any qualifiers as
5778 .debug_names does not contain any. */
5779
5780 if (strchr (name, '(') != NULL)
5781 {
54ee4252 5782 without_params = cp_remove_params (name);
927aa2e7 5783 if (without_params != NULL)
54ee4252 5784 name = without_params.get ();
927aa2e7
JK
5785 }
5786 }
5787
5788 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5789
5790 const uint32_t full_hash = dwarf5_djb_hash (name);
5791 uint32_t namei
5792 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5793 (map.bucket_table_reordered
5794 + (full_hash % map.bucket_count)), 4,
5795 map.dwarf5_byte_order);
5796 if (namei == 0)
5797 return NULL;
5798 --namei;
5799 if (namei >= map.name_count)
5800 {
b98664d3 5801 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5802 "[in module %s]"),
5803 namei, map.name_count,
ed2dc618 5804 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5805 return NULL;
5806 }
5807
5808 for (;;)
5809 {
5810 const uint32_t namei_full_hash
5811 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5812 (map.hash_table_reordered + namei), 4,
5813 map.dwarf5_byte_order);
5814 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5815 return NULL;
5816
5817 if (full_hash == namei_full_hash)
5818 {
5819 const char *const namei_string = map.namei_to_name (namei);
5820
5821#if 0 /* An expensive sanity check. */
5822 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5823 {
b98664d3 5824 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5825 "[in module %s]"),
5826 namei, objfile_name (dwarf2_per_objfile->objfile));
5827 return NULL;
5828 }
5829#endif
5830
5831 if (cmp (namei_string, name) == 0)
5832 {
5833 const ULONGEST namei_entry_offs
5834 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5835 + namei * map.offset_size),
5836 map.offset_size, map.dwarf5_byte_order);
5837 return map.entry_pool + namei_entry_offs;
5838 }
5839 }
5840
5841 ++namei;
5842 if (namei >= map.name_count)
5843 return NULL;
5844 }
5845}
5846
5847const gdb_byte *
5848dw2_debug_names_iterator::find_vec_in_debug_names
5849 (const mapped_debug_names &map, uint32_t namei)
5850{
5851 if (namei >= map.name_count)
5852 {
b98664d3 5853 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5854 "[in module %s]"),
5855 namei, map.name_count,
ed2dc618 5856 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5857 return NULL;
5858 }
5859
5860 const ULONGEST namei_entry_offs
5861 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5862 + namei * map.offset_size),
5863 map.offset_size, map.dwarf5_byte_order);
5864 return map.entry_pool + namei_entry_offs;
5865}
5866
5867/* See dw2_debug_names_iterator. */
5868
5869dwarf2_per_cu_data *
5870dw2_debug_names_iterator::next ()
5871{
5872 if (m_addr == NULL)
5873 return NULL;
5874
ed2dc618
SM
5875 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5876 struct objfile *objfile = dwarf2_per_objfile->objfile;
5877 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5878
5879 again:
5880
5881 unsigned int bytes_read;
5882 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5883 m_addr += bytes_read;
5884 if (abbrev == 0)
5885 return NULL;
5886
5887 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5888 if (indexval_it == m_map.abbrev_map.cend ())
5889 {
b98664d3 5890 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5891 "[in module %s]"),
ed2dc618 5892 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5893 return NULL;
5894 }
5895 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5896 enum class symbol_linkage {
5897 unknown,
5898 static_,
5899 extern_,
23c13d42 5900 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5901 dwarf2_per_cu_data *per_cu = NULL;
5902 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5903 {
5904 ULONGEST ull;
5905 switch (attr.form)
5906 {
5907 case DW_FORM_implicit_const:
5908 ull = attr.implicit_const;
5909 break;
5910 case DW_FORM_flag_present:
5911 ull = 1;
5912 break;
5913 case DW_FORM_udata:
5914 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5915 m_addr += bytes_read;
5916 break;
5917 default:
b98664d3 5918 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5919 dwarf_form_name (attr.form),
ed2dc618 5920 objfile_name (objfile));
927aa2e7
JK
5921 return NULL;
5922 }
5923 switch (attr.dw_idx)
5924 {
5925 case DW_IDX_compile_unit:
5926 /* Don't crash on bad data. */
b76e467d 5927 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5928 {
b98664d3 5929 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5930 " [in module %s]"),
5931 pulongest (ull),
5932 objfile_name (dwarf2_per_objfile->objfile));
5933 continue;
5934 }
ff4c9fec 5935 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5936 break;
8af5c486
JK
5937 case DW_IDX_type_unit:
5938 /* Don't crash on bad data. */
b2bdb8cf 5939 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5940 {
b98664d3 5941 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5942 " [in module %s]"),
5943 pulongest (ull),
5944 objfile_name (dwarf2_per_objfile->objfile));
5945 continue;
5946 }
ff4c9fec 5947 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5948 break;
927aa2e7
JK
5949 case DW_IDX_GNU_internal:
5950 if (!m_map.augmentation_is_gdb)
5951 break;
23c13d42 5952 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5953 break;
5954 case DW_IDX_GNU_external:
5955 if (!m_map.augmentation_is_gdb)
5956 break;
23c13d42 5957 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5958 break;
5959 }
5960 }
5961
5962 /* Skip if already read in. */
5963 if (per_cu->v.quick->compunit_symtab)
5964 goto again;
5965
5966 /* Check static vs global. */
23c13d42 5967 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5968 {
2b79f376 5969 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5970 const bool symbol_is_static =
5971 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5972 if (want_static != symbol_is_static)
2b79f376 5973 goto again;
927aa2e7
JK
5974 }
5975
5976 /* Match dw2_symtab_iter_next, symbol_kind
5977 and debug_names::psymbol_tag. */
5978 switch (m_domain)
5979 {
5980 case VAR_DOMAIN:
5981 switch (indexval.dwarf_tag)
5982 {
5983 case DW_TAG_variable:
5984 case DW_TAG_subprogram:
5985 /* Some types are also in VAR_DOMAIN. */
5986 case DW_TAG_typedef:
5987 case DW_TAG_structure_type:
5988 break;
5989 default:
5990 goto again;
5991 }
5992 break;
5993 case STRUCT_DOMAIN:
5994 switch (indexval.dwarf_tag)
5995 {
5996 case DW_TAG_typedef:
5997 case DW_TAG_structure_type:
5998 break;
5999 default:
6000 goto again;
6001 }
6002 break;
6003 case LABEL_DOMAIN:
6004 switch (indexval.dwarf_tag)
6005 {
6006 case 0:
6007 case DW_TAG_variable:
6008 break;
6009 default:
6010 goto again;
6011 }
6012 break;
59c35742
AB
6013 case MODULE_DOMAIN:
6014 switch (indexval.dwarf_tag)
6015 {
6016 case DW_TAG_module:
6017 break;
6018 default:
6019 goto again;
6020 }
6021 break;
927aa2e7
JK
6022 default:
6023 break;
6024 }
6025
6026 /* Match dw2_expand_symtabs_matching, symbol_kind and
6027 debug_names::psymbol_tag. */
6028 switch (m_search)
4b514bc8 6029 {
927aa2e7
JK
6030 case VARIABLES_DOMAIN:
6031 switch (indexval.dwarf_tag)
4b514bc8 6032 {
927aa2e7
JK
6033 case DW_TAG_variable:
6034 break;
6035 default:
6036 goto again;
4b514bc8 6037 }
927aa2e7
JK
6038 break;
6039 case FUNCTIONS_DOMAIN:
6040 switch (indexval.dwarf_tag)
4b514bc8 6041 {
927aa2e7
JK
6042 case DW_TAG_subprogram:
6043 break;
6044 default:
6045 goto again;
4b514bc8 6046 }
927aa2e7
JK
6047 break;
6048 case TYPES_DOMAIN:
6049 switch (indexval.dwarf_tag)
6050 {
6051 case DW_TAG_typedef:
6052 case DW_TAG_structure_type:
6053 break;
6054 default:
6055 goto again;
6056 }
6057 break;
59c35742
AB
6058 case MODULES_DOMAIN:
6059 switch (indexval.dwarf_tag)
6060 {
6061 case DW_TAG_module:
6062 break;
6063 default:
6064 goto again;
6065 }
927aa2e7
JK
6066 default:
6067 break;
4b514bc8 6068 }
927aa2e7
JK
6069
6070 return per_cu;
4b514bc8 6071}
61920122 6072
927aa2e7 6073static struct compunit_symtab *
c7f839cb 6074dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6075 const char *name, domain_enum domain)
4b514bc8 6076{
ed2dc618
SM
6077 struct dwarf2_per_objfile *dwarf2_per_objfile
6078 = get_dwarf2_per_objfile (objfile);
61920122 6079
927aa2e7
JK
6080 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6081 if (!mapp)
61920122 6082 {
927aa2e7
JK
6083 /* index is NULL if OBJF_READNOW. */
6084 return NULL;
6085 }
6086 const auto &map = *mapp;
9291a0cd 6087
2b79f376 6088 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6089
927aa2e7
JK
6090 struct compunit_symtab *stab_best = NULL;
6091 struct dwarf2_per_cu_data *per_cu;
6092 while ((per_cu = iter.next ()) != NULL)
6093 {
6094 struct symbol *sym, *with_opaque = NULL;
58f0c718 6095 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6096 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6097 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6098
927aa2e7
JK
6099 sym = block_find_symbol (block, name, domain,
6100 block_find_non_opaque_type_preferred,
6101 &with_opaque);
9703b513 6102
927aa2e7
JK
6103 /* Some caution must be observed with overloaded functions and
6104 methods, since the index will not contain any overload
6105 information (but NAME might contain it). */
a3ec0bb1 6106
927aa2e7 6107 if (sym != NULL
987012b8 6108 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
6109 return stab;
6110 if (with_opaque != NULL
987012b8 6111 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 6112 stab_best = stab;
9703b513 6113
927aa2e7 6114 /* Keep looking through other CUs. */
9703b513
TT
6115 }
6116
927aa2e7 6117 return stab_best;
9703b513
TT
6118}
6119
927aa2e7
JK
6120/* This dumps minimal information about .debug_names. It is called
6121 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6122 uses this to verify that .debug_names has been loaded. */
9291a0cd 6123
927aa2e7
JK
6124static void
6125dw2_debug_names_dump (struct objfile *objfile)
6126{
ed2dc618
SM
6127 struct dwarf2_per_objfile *dwarf2_per_objfile
6128 = get_dwarf2_per_objfile (objfile);
6129
927aa2e7
JK
6130 gdb_assert (dwarf2_per_objfile->using_index);
6131 printf_filtered (".debug_names:");
6132 if (dwarf2_per_objfile->debug_names_table)
6133 printf_filtered (" exists\n");
6134 else
6135 printf_filtered (" faked for \"readnow\"\n");
6136 printf_filtered ("\n");
9291a0cd
TT
6137}
6138
9291a0cd 6139static void
927aa2e7
JK
6140dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6141 const char *func_name)
9291a0cd 6142{
ed2dc618
SM
6143 struct dwarf2_per_objfile *dwarf2_per_objfile
6144 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6145
927aa2e7
JK
6146 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6147 if (dwarf2_per_objfile->debug_names_table)
24c79950 6148 {
927aa2e7 6149 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6150
2b79f376 6151 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6152
927aa2e7
JK
6153 struct dwarf2_per_cu_data *per_cu;
6154 while ((per_cu = iter.next ()) != NULL)
58f0c718 6155 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6156 }
6157}
24c79950 6158
3b00ef10
TT
6159static void
6160dw2_debug_names_map_matching_symbols
6161 (struct objfile *objfile,
6162 const lookup_name_info &name, domain_enum domain,
6163 int global,
6164 gdb::function_view<symbol_found_callback_ftype> callback,
6165 symbol_compare_ftype *ordered_compare)
6166{
6167 struct dwarf2_per_objfile *dwarf2_per_objfile
6168 = get_dwarf2_per_objfile (objfile);
6169
6170 /* debug_names_table is NULL if OBJF_READNOW. */
6171 if (!dwarf2_per_objfile->debug_names_table)
6172 return;
6173
6174 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6175 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6176
6177 const char *match_name = name.ada ().lookup_name ().c_str ();
6178 auto matcher = [&] (const char *symname)
6179 {
6180 if (ordered_compare == nullptr)
6181 return true;
6182 return ordered_compare (symname, match_name) == 0;
6183 };
6184
6185 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6186 [&] (offset_type namei)
6187 {
6188 /* The name was matched, now expand corresponding CUs that were
6189 marked. */
6190 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6191
6192 struct dwarf2_per_cu_data *per_cu;
6193 while ((per_cu = iter.next ()) != NULL)
6194 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6195 return true;
6196 });
6197
6198 /* It's a shame we couldn't do this inside the
6199 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6200 that have already been expanded. Instead, this loop matches what
6201 the psymtab code does. */
6202 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6203 {
6204 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6205 if (cust != nullptr)
6206 {
6207 const struct block *block
6208 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6209 if (!iterate_over_symbols_terminated (block, name,
6210 domain, callback))
6211 break;
6212 }
6213 }
6214}
6215
927aa2e7
JK
6216static void
6217dw2_debug_names_expand_symtabs_matching
6218 (struct objfile *objfile,
6219 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6220 const lookup_name_info &lookup_name,
6221 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6222 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6223 enum search_domain kind)
6224{
ed2dc618
SM
6225 struct dwarf2_per_objfile *dwarf2_per_objfile
6226 = get_dwarf2_per_objfile (objfile);
9291a0cd 6227
927aa2e7
JK
6228 /* debug_names_table is NULL if OBJF_READNOW. */
6229 if (!dwarf2_per_objfile->debug_names_table)
6230 return;
9291a0cd 6231
ed2dc618 6232 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6233
44ed8f3e 6234 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6235
44ed8f3e
PA
6236 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6237 symbol_matcher,
6238 kind, [&] (offset_type namei)
927aa2e7 6239 {
927aa2e7
JK
6240 /* The name was matched, now expand corresponding CUs that were
6241 marked. */
6242 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6243
927aa2e7
JK
6244 struct dwarf2_per_cu_data *per_cu;
6245 while ((per_cu = iter.next ()) != NULL)
6246 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6247 expansion_notify);
3b00ef10 6248 return true;
44ed8f3e 6249 });
9291a0cd
TT
6250}
6251
927aa2e7 6252const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6253{
6254 dw2_has_symbols,
6255 dw2_find_last_source_symtab,
6256 dw2_forget_cached_source_info,
f8eba3c6 6257 dw2_map_symtabs_matching_filename,
927aa2e7 6258 dw2_debug_names_lookup_symbol,
9291a0cd 6259 dw2_print_stats,
927aa2e7 6260 dw2_debug_names_dump,
927aa2e7 6261 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6262 dw2_expand_all_symtabs,
652a8996 6263 dw2_expand_symtabs_with_fullname,
3b00ef10 6264 dw2_debug_names_map_matching_symbols,
927aa2e7 6265 dw2_debug_names_expand_symtabs_matching,
43f3e411 6266 dw2_find_pc_sect_compunit_symtab,
71a3c369 6267 NULL,
9291a0cd
TT
6268 dw2_map_symbol_filenames
6269};
6270
4485a1c1
SM
6271/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6272 to either a dwarf2_per_objfile or dwz_file object. */
6273
6274template <typename T>
6275static gdb::array_view<const gdb_byte>
6276get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6277{
6278 dwarf2_section_info *section = &section_owner->gdb_index;
6279
6280 if (dwarf2_section_empty_p (section))
6281 return {};
6282
6283 /* Older elfutils strip versions could keep the section in the main
6284 executable while splitting it for the separate debug info file. */
6285 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6286 return {};
6287
6288 dwarf2_read_section (obj, section);
6289
8bebfcda
PA
6290 /* dwarf2_section_info::size is a bfd_size_type, while
6291 gdb::array_view works with size_t. On 32-bit hosts, with
6292 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6293 is 32-bit. So we need an explicit narrowing conversion here.
6294 This is fine, because it's impossible to allocate or mmap an
6295 array/buffer larger than what size_t can represent. */
6296 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6297}
6298
87d6a7aa
SM
6299/* Lookup the index cache for the contents of the index associated to
6300 DWARF2_OBJ. */
6301
6302static gdb::array_view<const gdb_byte>
6303get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6304{
6305 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6306 if (build_id == nullptr)
6307 return {};
6308
6309 return global_index_cache.lookup_gdb_index (build_id,
6310 &dwarf2_obj->index_cache_res);
6311}
6312
6313/* Same as the above, but for DWZ. */
6314
6315static gdb::array_view<const gdb_byte>
6316get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6317{
6318 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6319 if (build_id == nullptr)
6320 return {};
6321
6322 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6323}
6324
3c0aa29a 6325/* See symfile.h. */
9291a0cd 6326
3c0aa29a
PA
6327bool
6328dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6329{
ed2dc618
SM
6330 struct dwarf2_per_objfile *dwarf2_per_objfile
6331 = get_dwarf2_per_objfile (objfile);
6332
9291a0cd
TT
6333 /* If we're about to read full symbols, don't bother with the
6334 indices. In this case we also don't care if some other debug
6335 format is making psymtabs, because they are all about to be
6336 expanded anyway. */
6337 if ((objfile->flags & OBJF_READNOW))
6338 {
9291a0cd 6339 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6340 create_all_comp_units (dwarf2_per_objfile);
6341 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6342 dwarf2_per_objfile->quick_file_names_table
6343 = create_quick_file_names_table
6344 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6345
b76e467d 6346 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6347 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6348 {
ff4c9fec 6349 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6350
e254ef6a
DE
6351 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6352 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6353 }
6354
6355 /* Return 1 so that gdb sees the "quick" functions. However,
6356 these functions will be no-ops because we will have expanded
6357 all symtabs. */
3c0aa29a
PA
6358 *index_kind = dw_index_kind::GDB_INDEX;
6359 return true;
9291a0cd
TT
6360 }
6361
ed2dc618 6362 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6363 {
6364 *index_kind = dw_index_kind::DEBUG_NAMES;
6365 return true;
6366 }
927aa2e7 6367
4485a1c1
SM
6368 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6369 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6370 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6371 {
6372 *index_kind = dw_index_kind::GDB_INDEX;
6373 return true;
6374 }
9291a0cd 6375
87d6a7aa
SM
6376 /* ... otherwise, try to find the index in the index cache. */
6377 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6378 get_gdb_index_contents_from_cache,
6379 get_gdb_index_contents_from_cache_dwz))
6380 {
6381 global_index_cache.hit ();
6382 *index_kind = dw_index_kind::GDB_INDEX;
6383 return true;
6384 }
6385
6386 global_index_cache.miss ();
3c0aa29a 6387 return false;
9291a0cd
TT
6388}
6389
6390\f
6391
dce234bc
PP
6392/* Build a partial symbol table. */
6393
6394void
f29dff0a 6395dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6396{
ed2dc618
SM
6397 struct dwarf2_per_objfile *dwarf2_per_objfile
6398 = get_dwarf2_per_objfile (objfile);
c9bf0622 6399
6eee24ce 6400 init_psymbol_list (objfile, 1024);
c906108c 6401
a70b8144 6402 try
c9bf0622
TT
6403 {
6404 /* This isn't really ideal: all the data we allocate on the
6405 objfile's obstack is still uselessly kept around. However,
6406 freeing it seems unsafe. */
906768f9 6407 psymtab_discarder psymtabs (objfile);
ed2dc618 6408 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6409 psymtabs.keep ();
87d6a7aa
SM
6410
6411 /* (maybe) store an index in the cache. */
6412 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6413 }
230d2906 6414 catch (const gdb_exception_error &except)
492d29ea
PA
6415 {
6416 exception_print (gdb_stderr, except);
6417 }
c906108c 6418}
c906108c 6419
1ce1cefd
DE
6420/* Return the total length of the CU described by HEADER. */
6421
6422static unsigned int
6423get_cu_length (const struct comp_unit_head *header)
6424{
6425 return header->initial_length_size + header->length;
6426}
6427
9c541725 6428/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6429
9c541725
PA
6430static inline bool
6431offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6432{
9c541725
PA
6433 sect_offset bottom = cu_header->sect_off;
6434 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6435
9c541725 6436 return sect_off >= bottom && sect_off < top;
45452591
DE
6437}
6438
3b80fe9b
DE
6439/* Find the base address of the compilation unit for range lists and
6440 location lists. It will normally be specified by DW_AT_low_pc.
6441 In DWARF-3 draft 4, the base address could be overridden by
6442 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6443 compilation units with discontinuous ranges. */
6444
6445static void
6446dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6447{
6448 struct attribute *attr;
6449
6450 cu->base_known = 0;
6451 cu->base_address = 0;
6452
6453 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6454 if (attr != nullptr)
3b80fe9b 6455 {
31aa7e4e 6456 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6457 cu->base_known = 1;
6458 }
6459 else
6460 {
6461 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6462 if (attr != nullptr)
3b80fe9b 6463 {
31aa7e4e 6464 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6465 cu->base_known = 1;
6466 }
6467 }
6468}
6469
93311388 6470/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6471 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6472 NOTE: This leaves members offset, first_die_offset to be filled in
6473 by the caller. */
107d2387 6474
d521ce57 6475static const gdb_byte *
107d2387 6476read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6477 const gdb_byte *info_ptr,
6478 struct dwarf2_section_info *section,
6479 rcuh_kind section_kind)
107d2387
AC
6480{
6481 int signed_addr;
891d2f0b 6482 unsigned int bytes_read;
43988095
JK
6483 const char *filename = get_section_file_name (section);
6484 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6485
6486 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6487 cu_header->initial_length_size = bytes_read;
6488 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6489 info_ptr += bytes_read;
107d2387 6490 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6491 if (cu_header->version < 2 || cu_header->version > 5)
6492 error (_("Dwarf Error: wrong version in compilation unit header "
6493 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6494 cu_header->version, filename);
107d2387 6495 info_ptr += 2;
43988095
JK
6496 if (cu_header->version < 5)
6497 switch (section_kind)
6498 {
6499 case rcuh_kind::COMPILE:
6500 cu_header->unit_type = DW_UT_compile;
6501 break;
6502 case rcuh_kind::TYPE:
6503 cu_header->unit_type = DW_UT_type;
6504 break;
6505 default:
6506 internal_error (__FILE__, __LINE__,
6507 _("read_comp_unit_head: invalid section_kind"));
6508 }
6509 else
6510 {
6511 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6512 (read_1_byte (abfd, info_ptr));
6513 info_ptr += 1;
6514 switch (cu_header->unit_type)
6515 {
6516 case DW_UT_compile:
a084a2a6
AT
6517 case DW_UT_partial:
6518 case DW_UT_skeleton:
6519 case DW_UT_split_compile:
43988095
JK
6520 if (section_kind != rcuh_kind::COMPILE)
6521 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6522 "(is %s, should be %s) [in module %s]"),
6523 dwarf_unit_type_name (cu_header->unit_type),
6524 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6525 break;
6526 case DW_UT_type:
a084a2a6 6527 case DW_UT_split_type:
43988095
JK
6528 section_kind = rcuh_kind::TYPE;
6529 break;
6530 default:
6531 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6532 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6533 "[in module %s]"), cu_header->unit_type,
6534 dwarf_unit_type_name (DW_UT_compile),
6535 dwarf_unit_type_name (DW_UT_skeleton),
6536 dwarf_unit_type_name (DW_UT_split_compile),
6537 dwarf_unit_type_name (DW_UT_type),
6538 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6539 }
6540
6541 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6542 info_ptr += 1;
6543 }
9c541725
PA
6544 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6545 cu_header,
6546 &bytes_read);
613e1657 6547 info_ptr += bytes_read;
43988095
JK
6548 if (cu_header->version < 5)
6549 {
6550 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6551 info_ptr += 1;
6552 }
107d2387
AC
6553 signed_addr = bfd_get_sign_extend_vma (abfd);
6554 if (signed_addr < 0)
8e65ff28 6555 internal_error (__FILE__, __LINE__,
e2e0b3e5 6556 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6557 cu_header->signed_addr_p = signed_addr;
c764a876 6558
a084a2a6
AT
6559 bool header_has_signature = section_kind == rcuh_kind::TYPE
6560 || cu_header->unit_type == DW_UT_skeleton
6561 || cu_header->unit_type == DW_UT_split_compile;
43988095 6562
a084a2a6
AT
6563 if (header_has_signature)
6564 {
43988095
JK
6565 cu_header->signature = read_8_bytes (abfd, info_ptr);
6566 info_ptr += 8;
a084a2a6 6567 }
43988095 6568
a084a2a6
AT
6569 if (section_kind == rcuh_kind::TYPE)
6570 {
6571 LONGEST type_offset;
43988095
JK
6572 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6573 info_ptr += bytes_read;
9c541725
PA
6574 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6575 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6576 error (_("Dwarf Error: Too big type_offset in compilation unit "
6577 "header (is %s) [in module %s]"), plongest (type_offset),
6578 filename);
6579 }
6580
107d2387
AC
6581 return info_ptr;
6582}
6583
36586728
TT
6584/* Helper function that returns the proper abbrev section for
6585 THIS_CU. */
6586
6587static struct dwarf2_section_info *
6588get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6589{
6590 struct dwarf2_section_info *abbrev;
ed2dc618 6591 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6592
6593 if (this_cu->is_dwz)
ed2dc618 6594 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6595 else
6596 abbrev = &dwarf2_per_objfile->abbrev;
6597
6598 return abbrev;
6599}
6600
9ff913ba
DE
6601/* Subroutine of read_and_check_comp_unit_head and
6602 read_and_check_type_unit_head to simplify them.
6603 Perform various error checking on the header. */
6604
6605static void
ed2dc618
SM
6606error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6607 struct comp_unit_head *header,
4bdcc0c1
DE
6608 struct dwarf2_section_info *section,
6609 struct dwarf2_section_info *abbrev_section)
9ff913ba 6610{
a32a8923 6611 const char *filename = get_section_file_name (section);
9ff913ba 6612
9c541725 6613 if (to_underlying (header->abbrev_sect_off)
36586728 6614 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6615 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6616 "(offset %s + 6) [in module %s]"),
6617 sect_offset_str (header->abbrev_sect_off),
6618 sect_offset_str (header->sect_off),
9ff913ba
DE
6619 filename);
6620
9c541725 6621 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6622 avoid potential 32-bit overflow. */
9c541725 6623 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6624 > section->size)
9c541725 6625 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6626 "(offset %s + 0) [in module %s]"),
6627 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6628 filename);
6629}
6630
6631/* Read in a CU/TU header and perform some basic error checking.
6632 The contents of the header are stored in HEADER.
6633 The result is a pointer to the start of the first DIE. */
adabb602 6634
d521ce57 6635static const gdb_byte *
ed2dc618
SM
6636read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6637 struct comp_unit_head *header,
9ff913ba 6638 struct dwarf2_section_info *section,
4bdcc0c1 6639 struct dwarf2_section_info *abbrev_section,
d521ce57 6640 const gdb_byte *info_ptr,
43988095 6641 rcuh_kind section_kind)
72bf9492 6642{
d521ce57 6643 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6644
9c541725 6645 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6646
43988095 6647 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6648
9c541725 6649 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6650
ed2dc618
SM
6651 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6652 abbrev_section);
9ff913ba
DE
6653
6654 return info_ptr;
348e048f
DE
6655}
6656
f4dc4d17
DE
6657/* Fetch the abbreviation table offset from a comp or type unit header. */
6658
6659static sect_offset
ed2dc618
SM
6660read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6661 struct dwarf2_section_info *section,
9c541725 6662 sect_offset sect_off)
f4dc4d17 6663{
a32a8923 6664 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6665 const gdb_byte *info_ptr;
ac298888 6666 unsigned int initial_length_size, offset_size;
43988095 6667 uint16_t version;
f4dc4d17
DE
6668
6669 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6670 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6671 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6672 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6673 info_ptr += initial_length_size;
6674
6675 version = read_2_bytes (abfd, info_ptr);
6676 info_ptr += 2;
6677 if (version >= 5)
6678 {
6679 /* Skip unit type and address size. */
6680 info_ptr += 2;
6681 }
6682
9c541725 6683 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6684}
6685
aaa75496
JB
6686/* Allocate a new partial symtab for file named NAME and mark this new
6687 partial symtab as being an include of PST. */
6688
6689static void
891813be 6690dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6691 struct objfile *objfile)
6692{
891813be 6693 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
aaa75496 6694
fbd9ab74
JK
6695 if (!IS_ABSOLUTE_PATH (subpst->filename))
6696 {
6697 /* It shares objfile->objfile_obstack. */
6698 subpst->dirname = pst->dirname;
6699 }
6700
a9342b62 6701 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6702 subpst->dependencies[0] = pst;
6703 subpst->number_of_dependencies = 1;
6704
aaa75496
JB
6705 /* No private part is necessary for include psymtabs. This property
6706 can be used to differentiate between such include psymtabs and
10b3939b 6707 the regular ones. */
891813be 6708 subpst->per_cu_data = nullptr;
aaa75496
JB
6709}
6710
6711/* Read the Line Number Program data and extract the list of files
6712 included by the source file represented by PST. Build an include
d85a05f0 6713 partial symtab for each of these included files. */
aaa75496
JB
6714
6715static void
6716dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6717 struct die_info *die,
891813be 6718 dwarf2_psymtab *pst)
aaa75496 6719{
fff8551c 6720 line_header_up lh;
d85a05f0 6721 struct attribute *attr;
aaa75496 6722
d85a05f0 6723 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6724 if (attr != nullptr)
9c541725 6725 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6726 if (lh == NULL)
6727 return; /* No linetable, so no includes. */
6728
79748972
TT
6729 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6730 that we pass in the raw text_low here; that is ok because we're
6731 only decoding the line table to make include partial symtabs, and
6732 so the addresses aren't really used. */
4ae976d1 6733 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6734 pst->raw_text_low (), 1);
aaa75496
JB
6735}
6736
348e048f 6737static hashval_t
52dc124a 6738hash_signatured_type (const void *item)
348e048f 6739{
9a3c8263
SM
6740 const struct signatured_type *sig_type
6741 = (const struct signatured_type *) item;
9a619af0 6742
348e048f 6743 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6744 return sig_type->signature;
348e048f
DE
6745}
6746
6747static int
52dc124a 6748eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6749{
9a3c8263
SM
6750 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6751 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6752
348e048f
DE
6753 return lhs->signature == rhs->signature;
6754}
6755
1fd400ff
TT
6756/* Allocate a hash table for signatured types. */
6757
6758static htab_t
673bfd45 6759allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6760{
6761 return htab_create_alloc_ex (41,
52dc124a
DE
6762 hash_signatured_type,
6763 eq_signatured_type,
1fd400ff
TT
6764 NULL,
6765 &objfile->objfile_obstack,
6766 hashtab_obstack_allocate,
6767 dummy_obstack_deallocate);
6768}
6769
d467dd73 6770/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6771
6772static int
d467dd73 6773add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6774{
9a3c8263 6775 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6776 std::vector<signatured_type *> *all_type_units
6777 = (std::vector<signatured_type *> *) datum;
1fd400ff 6778
b2bdb8cf 6779 all_type_units->push_back (sigt);
1fd400ff
TT
6780
6781 return 1;
6782}
6783
78d4d2c5 6784/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6785 and fill them into TYPES_HTAB. It will process only type units,
6786 therefore DW_UT_type. */
c88ee1f0 6787
78d4d2c5 6788static void
ed2dc618
SM
6789create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6790 struct dwo_file *dwo_file,
43988095
JK
6791 dwarf2_section_info *section, htab_t &types_htab,
6792 rcuh_kind section_kind)
348e048f 6793{
3019eac3 6794 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6795 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6796 bfd *abfd;
6797 const gdb_byte *info_ptr, *end_ptr;
348e048f 6798
4bdcc0c1
DE
6799 abbrev_section = (dwo_file != NULL
6800 ? &dwo_file->sections.abbrev
6801 : &dwarf2_per_objfile->abbrev);
6802
b4f54984 6803 if (dwarf_read_debug)
43988095
JK
6804 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6805 get_section_name (section),
a32a8923 6806 get_section_file_name (abbrev_section));
09406207 6807
78d4d2c5
JK
6808 dwarf2_read_section (objfile, section);
6809 info_ptr = section->buffer;
348e048f 6810
78d4d2c5
JK
6811 if (info_ptr == NULL)
6812 return;
348e048f 6813
78d4d2c5
JK
6814 /* We can't set abfd until now because the section may be empty or
6815 not present, in which case the bfd is unknown. */
6816 abfd = get_section_bfd_owner (section);
348e048f 6817
c0ab21c2
TT
6818 /* We don't use cutu_reader here because we don't need to read
6819 any dies: the signature is in the header. */
3019eac3 6820
78d4d2c5
JK
6821 end_ptr = info_ptr + section->size;
6822 while (info_ptr < end_ptr)
6823 {
78d4d2c5
JK
6824 struct signatured_type *sig_type;
6825 struct dwo_unit *dwo_tu;
6826 void **slot;
6827 const gdb_byte *ptr = info_ptr;
6828 struct comp_unit_head header;
6829 unsigned int length;
8b70b953 6830
9c541725 6831 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6832
a49dd8dd
JK
6833 /* Initialize it due to a false compiler warning. */
6834 header.signature = -1;
9c541725 6835 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6836
78d4d2c5
JK
6837 /* We need to read the type's signature in order to build the hash
6838 table, but we don't need anything else just yet. */
348e048f 6839
ed2dc618 6840 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6841 abbrev_section, ptr, section_kind);
348e048f 6842
78d4d2c5 6843 length = get_cu_length (&header);
6caca83c 6844
78d4d2c5
JK
6845 /* Skip dummy type units. */
6846 if (ptr >= info_ptr + length
43988095
JK
6847 || peek_abbrev_code (abfd, ptr) == 0
6848 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6849 {
6850 info_ptr += length;
6851 continue;
6852 }
dee91e82 6853
78d4d2c5
JK
6854 if (types_htab == NULL)
6855 {
6856 if (dwo_file)
6857 types_htab = allocate_dwo_unit_table (objfile);
6858 else
6859 types_htab = allocate_signatured_type_table (objfile);
6860 }
8b70b953 6861
78d4d2c5
JK
6862 if (dwo_file)
6863 {
6864 sig_type = NULL;
6865 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6866 struct dwo_unit);
6867 dwo_tu->dwo_file = dwo_file;
43988095 6868 dwo_tu->signature = header.signature;
9c541725 6869 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6870 dwo_tu->section = section;
9c541725 6871 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6872 dwo_tu->length = length;
6873 }
6874 else
6875 {
6876 /* N.B.: type_offset is not usable if this type uses a DWO file.
6877 The real type_offset is in the DWO file. */
6878 dwo_tu = NULL;
6879 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6880 struct signatured_type);
43988095 6881 sig_type->signature = header.signature;
9c541725 6882 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6883 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6884 sig_type->per_cu.is_debug_types = 1;
6885 sig_type->per_cu.section = section;
9c541725 6886 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6887 sig_type->per_cu.length = length;
6888 }
6889
6890 slot = htab_find_slot (types_htab,
6891 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6892 INSERT);
6893 gdb_assert (slot != NULL);
6894 if (*slot != NULL)
6895 {
9c541725 6896 sect_offset dup_sect_off;
0349ea22 6897
3019eac3
DE
6898 if (dwo_file)
6899 {
78d4d2c5
JK
6900 const struct dwo_unit *dup_tu
6901 = (const struct dwo_unit *) *slot;
6902
9c541725 6903 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6904 }
6905 else
6906 {
78d4d2c5
JK
6907 const struct signatured_type *dup_tu
6908 = (const struct signatured_type *) *slot;
6909
9c541725 6910 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6911 }
8b70b953 6912
b98664d3 6913 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6914 " the entry at offset %s, signature %s"),
6915 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6916 hex_string (header.signature));
78d4d2c5
JK
6917 }
6918 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6919
78d4d2c5 6920 if (dwarf_read_debug > 1)
9d8780f0
SM
6921 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6922 sect_offset_str (sect_off),
43988095 6923 hex_string (header.signature));
3019eac3 6924
78d4d2c5
JK
6925 info_ptr += length;
6926 }
6927}
3019eac3 6928
78d4d2c5
JK
6929/* Create the hash table of all entries in the .debug_types
6930 (or .debug_types.dwo) section(s).
6931 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6932 otherwise it is NULL.
b3c8eb43 6933
78d4d2c5 6934 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6935
78d4d2c5 6936 Note: This function processes DWO files only, not DWP files. */
348e048f 6937
78d4d2c5 6938static void
ed2dc618
SM
6939create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6940 struct dwo_file *dwo_file,
fd5866f6 6941 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6942 htab_t &types_htab)
6943{
fd5866f6
SM
6944 for (dwarf2_section_info &section : type_sections)
6945 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6946 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6947}
6948
6949/* Create the hash table of all entries in the .debug_types section,
6950 and initialize all_type_units.
6951 The result is zero if there is an error (e.g. missing .debug_types section),
6952 otherwise non-zero. */
6953
6954static int
ed2dc618 6955create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6956{
78d4d2c5 6957 htab_t types_htab = NULL;
3019eac3 6958
ed2dc618
SM
6959 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6960 &dwarf2_per_objfile->info, types_htab,
43988095 6961 rcuh_kind::COMPILE);
ed2dc618
SM
6962 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6963 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6964 if (types_htab == NULL)
6965 {
6966 dwarf2_per_objfile->signatured_types = NULL;
6967 return 0;
6968 }
6969
348e048f
DE
6970 dwarf2_per_objfile->signatured_types = types_htab;
6971
b2bdb8cf
SM
6972 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6973 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6974
6975 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6976 &dwarf2_per_objfile->all_type_units);
1fd400ff 6977
348e048f
DE
6978 return 1;
6979}
6980
6aa5f3a6
DE
6981/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6982 If SLOT is non-NULL, it is the entry to use in the hash table.
6983 Otherwise we find one. */
6984
6985static struct signatured_type *
ed2dc618
SM
6986add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6987 void **slot)
6aa5f3a6
DE
6988{
6989 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6990
b2bdb8cf
SM
6991 if (dwarf2_per_objfile->all_type_units.size ()
6992 == dwarf2_per_objfile->all_type_units.capacity ())
6993 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6994
b2bdb8cf
SM
6995 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6996 struct signatured_type);
6997
6998 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6999 sig_type->signature = sig;
7000 sig_type->per_cu.is_debug_types = 1;
7001 if (dwarf2_per_objfile->using_index)
7002 {
7003 sig_type->per_cu.v.quick =
7004 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7005 struct dwarf2_per_cu_quick_data);
7006 }
7007
7008 if (slot == NULL)
7009 {
7010 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7011 sig_type, INSERT);
7012 }
7013 gdb_assert (*slot == NULL);
7014 *slot = sig_type;
7015 /* The rest of sig_type must be filled in by the caller. */
7016 return sig_type;
7017}
7018
a2ce51a0
DE
7019/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7020 Fill in SIG_ENTRY with DWO_ENTRY. */
7021
7022static void
ed2dc618 7023fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
7024 struct signatured_type *sig_entry,
7025 struct dwo_unit *dwo_entry)
7026{
7ee85ab1 7027 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7028 gdb_assert (! sig_entry->per_cu.queued);
7029 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7030 if (dwarf2_per_objfile->using_index)
7031 {
7032 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7033 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7034 }
7035 else
7036 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7037 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7038 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7039 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7040 gdb_assert (sig_entry->dwo_unit == NULL);
7041
7042 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7043 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7044 sig_entry->per_cu.length = dwo_entry->length;
7045 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7046 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7047 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7048 sig_entry->dwo_unit = dwo_entry;
7049}
7050
7051/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7052 If we haven't read the TU yet, create the signatured_type data structure
7053 for a TU to be read in directly from a DWO file, bypassing the stub.
7054 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7055 using .gdb_index, then when reading a CU we want to stay in the DWO file
7056 containing that CU. Otherwise we could end up reading several other DWO
7057 files (due to comdat folding) to process the transitive closure of all the
7058 mentioned TUs, and that can be slow. The current DWO file will have every
7059 type signature that it needs.
a2ce51a0
DE
7060 We only do this for .gdb_index because in the psymtab case we already have
7061 to read all the DWOs to build the type unit groups. */
7062
7063static struct signatured_type *
7064lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7065{
518817b3
SM
7066 struct dwarf2_per_objfile *dwarf2_per_objfile
7067 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7068 struct objfile *objfile = dwarf2_per_objfile->objfile;
7069 struct dwo_file *dwo_file;
7070 struct dwo_unit find_dwo_entry, *dwo_entry;
7071 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7072 void **slot;
a2ce51a0
DE
7073
7074 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7075
6aa5f3a6
DE
7076 /* If TU skeletons have been removed then we may not have read in any
7077 TUs yet. */
7078 if (dwarf2_per_objfile->signatured_types == NULL)
7079 {
7080 dwarf2_per_objfile->signatured_types
7081 = allocate_signatured_type_table (objfile);
7082 }
a2ce51a0
DE
7083
7084 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7085 Use the global signatured_types array to do our own comdat-folding
7086 of types. If this is the first time we're reading this TU, and
7087 the TU has an entry in .gdb_index, replace the recorded data from
7088 .gdb_index with this TU. */
a2ce51a0 7089
a2ce51a0 7090 find_sig_entry.signature = sig;
6aa5f3a6
DE
7091 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7092 &find_sig_entry, INSERT);
9a3c8263 7093 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7094
7095 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7096 read. Don't reassign the global entry to point to this DWO if that's
7097 the case. Also note that if the TU is already being read, it may not
7098 have come from a DWO, the program may be a mix of Fission-compiled
7099 code and non-Fission-compiled code. */
7100
7101 /* Have we already tried to read this TU?
7102 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7103 needn't exist in the global table yet). */
7104 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7105 return sig_entry;
7106
6aa5f3a6
DE
7107 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7108 dwo_unit of the TU itself. */
7109 dwo_file = cu->dwo_unit->dwo_file;
7110
a2ce51a0
DE
7111 /* Ok, this is the first time we're reading this TU. */
7112 if (dwo_file->tus == NULL)
7113 return NULL;
7114 find_dwo_entry.signature = sig;
9a3c8263 7115 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7116 if (dwo_entry == NULL)
7117 return NULL;
7118
6aa5f3a6
DE
7119 /* If the global table doesn't have an entry for this TU, add one. */
7120 if (sig_entry == NULL)
ed2dc618 7121 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7122
ed2dc618 7123 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7124 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7125 return sig_entry;
7126}
7127
a2ce51a0
DE
7128/* Subroutine of lookup_signatured_type.
7129 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7130 then try the DWP file. If the TU stub (skeleton) has been removed then
7131 it won't be in .gdb_index. */
a2ce51a0
DE
7132
7133static struct signatured_type *
7134lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7135{
518817b3
SM
7136 struct dwarf2_per_objfile *dwarf2_per_objfile
7137 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7138 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7139 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7140 struct dwo_unit *dwo_entry;
7141 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7142 void **slot;
a2ce51a0
DE
7143
7144 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7145 gdb_assert (dwp_file != NULL);
7146
6aa5f3a6
DE
7147 /* If TU skeletons have been removed then we may not have read in any
7148 TUs yet. */
7149 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7150 {
6aa5f3a6
DE
7151 dwarf2_per_objfile->signatured_types
7152 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7153 }
7154
6aa5f3a6
DE
7155 find_sig_entry.signature = sig;
7156 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7157 &find_sig_entry, INSERT);
9a3c8263 7158 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7159
7160 /* Have we already tried to read this TU?
7161 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7162 needn't exist in the global table yet). */
7163 if (sig_entry != NULL)
7164 return sig_entry;
7165
a2ce51a0
DE
7166 if (dwp_file->tus == NULL)
7167 return NULL;
ed2dc618 7168 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7169 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7170 if (dwo_entry == NULL)
7171 return NULL;
7172
ed2dc618
SM
7173 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7174 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7175
a2ce51a0
DE
7176 return sig_entry;
7177}
7178
380bca97 7179/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7180 Returns NULL if signature SIG is not present in the table.
7181 It is up to the caller to complain about this. */
348e048f
DE
7182
7183static struct signatured_type *
a2ce51a0 7184lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7185{
518817b3
SM
7186 struct dwarf2_per_objfile *dwarf2_per_objfile
7187 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7188
a2ce51a0
DE
7189 if (cu->dwo_unit
7190 && dwarf2_per_objfile->using_index)
7191 {
7192 /* We're in a DWO/DWP file, and we're using .gdb_index.
7193 These cases require special processing. */
ed2dc618 7194 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7195 return lookup_dwo_signatured_type (cu, sig);
7196 else
7197 return lookup_dwp_signatured_type (cu, sig);
7198 }
7199 else
7200 {
7201 struct signatured_type find_entry, *entry;
348e048f 7202
a2ce51a0
DE
7203 if (dwarf2_per_objfile->signatured_types == NULL)
7204 return NULL;
7205 find_entry.signature = sig;
9a3c8263
SM
7206 entry = ((struct signatured_type *)
7207 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7208 return entry;
7209 }
348e048f 7210}
18a8505e
AT
7211
7212/* Return the address base of the compile unit, which, if exists, is stored
7213 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
7214static gdb::optional<ULONGEST>
7215lookup_addr_base (struct die_info *comp_unit_die)
7216{
7217 struct attribute *attr;
7218 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
7219 if (attr == nullptr)
7220 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
7221 if (attr == nullptr)
7222 return gdb::optional<ULONGEST> ();
7223 return DW_UNSND (attr);
7224}
7225
7226/* Return range lists base of the compile unit, which, if exists, is stored
7227 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
7228static ULONGEST
7229lookup_ranges_base (struct die_info *comp_unit_die)
7230{
7231 struct attribute *attr;
7232 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
7233 if (attr == nullptr)
7234 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
7235 if (attr == nullptr)
7236 return 0;
7237 return DW_UNSND (attr);
7238}
7239
42e7ad6c 7240/* Low level DIE reading support. */
348e048f 7241
d85a05f0
DJ
7242/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7243
7244static void
7245init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7246 struct dwarf2_cu *cu,
3019eac3 7247 struct dwarf2_section_info *section,
685af9cd
TT
7248 struct dwo_file *dwo_file,
7249 struct abbrev_table *abbrev_table)
d85a05f0 7250{
fceca515 7251 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7252 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7253 reader->cu = cu;
3019eac3 7254 reader->dwo_file = dwo_file;
dee91e82
DE
7255 reader->die_section = section;
7256 reader->buffer = section->buffer;
f664829e 7257 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7258 reader->comp_dir = NULL;
685af9cd 7259 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7260}
7261
c0ab21c2 7262/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7263 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 7264 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
7265 already.
7266
7267 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7268 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7269 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7270 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7271 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7272 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7273 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7274 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7275 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7276 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7277 kept around for at least as long as *RESULT_READER.
7278
b0c7bfa9
DE
7279 The result is non-zero if a valid (non-dummy) DIE was found. */
7280
7281static int
7282read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7283 struct dwo_unit *dwo_unit,
b0c7bfa9 7284 struct die_info *stub_comp_unit_die,
a2ce51a0 7285 const char *stub_comp_dir,
b0c7bfa9 7286 struct die_reader_specs *result_reader,
d521ce57 7287 const gdb_byte **result_info_ptr,
b0c7bfa9 7288 struct die_info **result_comp_unit_die,
685af9cd
TT
7289 int *result_has_children,
7290 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7291{
ed2dc618 7292 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7293 struct objfile *objfile = dwarf2_per_objfile->objfile;
7294 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7295 bfd *abfd;
d521ce57 7296 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7297 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7298 int i,num_extra_attrs;
7299 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
7300 struct die_info *comp_unit_die;
7301
b0aeadb3
DE
7302 /* At most one of these may be provided. */
7303 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7304
b0c7bfa9
DE
7305 /* These attributes aren't processed until later:
7306 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7307 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7308 referenced later. However, these attributes are found in the stub
7309 which we won't have later. In order to not impose this complication
7310 on the rest of the code, we read them here and copy them to the
7311 DWO CU/TU die. */
b0c7bfa9
DE
7312
7313 stmt_list = NULL;
7314 low_pc = NULL;
7315 high_pc = NULL;
7316 ranges = NULL;
7317 comp_dir = NULL;
7318
7319 if (stub_comp_unit_die != NULL)
7320 {
7321 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7322 DWO file. */
7323 if (! this_cu->is_debug_types)
7324 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7325 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7326 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7327 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7328 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7329
18a8505e 7330 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 7331
18a8505e
AT
7332 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
7333 here (if needed). We need the value before we can process
7334 DW_AT_ranges. */
7335 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 7336 }
a2ce51a0
DE
7337 else if (stub_comp_dir != NULL)
7338 {
7339 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7340 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7341 comp_dir->name = DW_AT_comp_dir;
7342 comp_dir->form = DW_FORM_string;
7343 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7344 DW_STRING (comp_dir) = stub_comp_dir;
7345 }
b0c7bfa9
DE
7346
7347 /* Set up for reading the DWO CU/TU. */
7348 cu->dwo_unit = dwo_unit;
685af9cd 7349 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7350 dwarf2_read_section (objfile, section);
a32a8923 7351 abfd = get_section_bfd_owner (section);
9c541725
PA
7352 begin_info_ptr = info_ptr = (section->buffer
7353 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7354 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7355
7356 if (this_cu->is_debug_types)
7357 {
b0c7bfa9
DE
7358 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7359
ed2dc618
SM
7360 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7361 &cu->header, section,
b0c7bfa9 7362 dwo_abbrev_section,
43988095 7363 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7364 /* This is not an assert because it can be caused by bad debug info. */
43988095 7365 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7366 {
7367 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7368 " TU at offset %s [in module %s]"),
a2ce51a0 7369 hex_string (sig_type->signature),
43988095 7370 hex_string (cu->header.signature),
9d8780f0 7371 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7372 bfd_get_filename (abfd));
7373 }
9c541725 7374 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7375 /* For DWOs coming from DWP files, we don't know the CU length
7376 nor the type's offset in the TU until now. */
7377 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7378 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7379
7380 /* Establish the type offset that can be used to lookup the type.
7381 For DWO files, we don't know it until now. */
9c541725
PA
7382 sig_type->type_offset_in_section
7383 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7384 }
7385 else
7386 {
ed2dc618
SM
7387 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7388 &cu->header, section,
b0c7bfa9 7389 dwo_abbrev_section,
43988095 7390 info_ptr, rcuh_kind::COMPILE);
9c541725 7391 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7392 /* For DWOs coming from DWP files, we don't know the CU length
7393 until now. */
7394 dwo_unit->length = get_cu_length (&cu->header);
7395 }
7396
685af9cd
TT
7397 *result_dwo_abbrev_table
7398 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7399 cu->header.abbrev_sect_off);
7400 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7401 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7402
7403 /* Read in the die, but leave space to copy over the attributes
7404 from the stub. This has the benefit of simplifying the rest of
7405 the code - all the work to maintain the illusion of a single
7406 DW_TAG_{compile,type}_unit DIE is done here. */
7407 num_extra_attrs = ((stmt_list != NULL)
7408 + (low_pc != NULL)
7409 + (high_pc != NULL)
7410 + (ranges != NULL)
7411 + (comp_dir != NULL));
7412 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7413 result_has_children, num_extra_attrs);
7414
7415 /* Copy over the attributes from the stub to the DIE we just read in. */
7416 comp_unit_die = *result_comp_unit_die;
7417 i = comp_unit_die->num_attrs;
7418 if (stmt_list != NULL)
7419 comp_unit_die->attrs[i++] = *stmt_list;
7420 if (low_pc != NULL)
7421 comp_unit_die->attrs[i++] = *low_pc;
7422 if (high_pc != NULL)
7423 comp_unit_die->attrs[i++] = *high_pc;
7424 if (ranges != NULL)
7425 comp_unit_die->attrs[i++] = *ranges;
7426 if (comp_dir != NULL)
7427 comp_unit_die->attrs[i++] = *comp_dir;
7428 comp_unit_die->num_attrs += num_extra_attrs;
7429
b4f54984 7430 if (dwarf_die_debug)
bf6af496
DE
7431 {
7432 fprintf_unfiltered (gdb_stdlog,
7433 "Read die from %s@0x%x of %s:\n",
a32a8923 7434 get_section_name (section),
bf6af496
DE
7435 (unsigned) (begin_info_ptr - section->buffer),
7436 bfd_get_filename (abfd));
b4f54984 7437 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7438 }
7439
a2ce51a0
DE
7440 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7441 TUs by skipping the stub and going directly to the entry in the DWO file.
7442 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7443 to get it via circuitous means. Blech. */
7444 if (comp_dir != NULL)
7445 result_reader->comp_dir = DW_STRING (comp_dir);
7446
b0c7bfa9
DE
7447 /* Skip dummy compilation units. */
7448 if (info_ptr >= begin_info_ptr + dwo_unit->length
7449 || peek_abbrev_code (abfd, info_ptr) == 0)
7450 return 0;
7451
7452 *result_info_ptr = info_ptr;
7453 return 1;
7454}
7455
a084a2a6
AT
7456/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7457 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7458 signature is part of the header. */
7459static gdb::optional<ULONGEST>
7460lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7461{
7462 if (cu->header.version >= 5)
7463 return cu->header.signature;
7464 struct attribute *attr;
7465 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7466 if (attr == nullptr)
7467 return gdb::optional<ULONGEST> ();
7468 return DW_UNSND (attr);
7469}
7470
c0ab21c2 7471/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7472 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7473 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7474
7475static struct dwo_unit *
7476lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
7477 struct die_info *comp_unit_die,
7478 const char *dwo_name)
b0c7bfa9
DE
7479{
7480 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7481 struct dwo_unit *dwo_unit;
c0ab21c2 7482 const char *comp_dir;
b0c7bfa9 7483
a2ce51a0
DE
7484 gdb_assert (cu != NULL);
7485
b0c7bfa9 7486 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7487 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7488 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7489
7490 if (this_cu->is_debug_types)
7491 {
7492 struct signatured_type *sig_type;
7493
7494 /* Since this_cu is the first member of struct signatured_type,
7495 we can go from a pointer to one to a pointer to the other. */
7496 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7497 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7498 }
7499 else
7500 {
a084a2a6
AT
7501 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7502 if (!signature.has_value ())
b0c7bfa9
DE
7503 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7504 " [in module %s]"),
e3b94546 7505 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7506 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7507 *signature);
b0c7bfa9
DE
7508 }
7509
b0c7bfa9
DE
7510 return dwo_unit;
7511}
7512
c0ab21c2 7513/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7514 See it for a description of the parameters.
fcd3b13d 7515 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7516
c0ab21c2
TT
7517void
7518cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7519 int use_existing_cu, int keep)
a2ce51a0 7520{
a2ce51a0 7521 struct signatured_type *sig_type;
a2ce51a0 7522 struct die_reader_specs reader;
a2ce51a0
DE
7523
7524 /* Verify we can do the following downcast, and that we have the
7525 data we need. */
7526 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7527 sig_type = (struct signatured_type *) this_cu;
7528 gdb_assert (sig_type->dwo_unit != NULL);
7529
6aa5f3a6
DE
7530 if (use_existing_cu && this_cu->cu != NULL)
7531 {
7532 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7533 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7534 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7535 }
7536 else
7537 {
7538 /* If !use_existing_cu, this_cu->cu must be NULL. */
7539 gdb_assert (this_cu->cu == NULL);
c0ab21c2 7540 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7541 }
7542
7543 /* A future optimization, if needed, would be to use an existing
7544 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7545 could share abbrev tables. */
a2ce51a0
DE
7546
7547 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7548 NULL /* stub_comp_unit_die */,
7549 sig_type->dwo_unit->dwo_file->comp_dir,
7550 &reader, &info_ptr,
685af9cd 7551 &comp_unit_die, &has_children,
c0ab21c2 7552 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7553 {
7554 /* Dummy die. */
c0ab21c2 7555 dummy_p = true;
a2ce51a0 7556 }
a2ce51a0
DE
7557}
7558
fd820528 7559/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7560 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7561
f4dc4d17
DE
7562 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7563 Otherwise the table specified in the comp unit header is read in and used.
7564 This is an optimization for when we already have the abbrev table.
7565
dee91e82
DE
7566 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7567 Otherwise, a new CU is allocated with xmalloc.
7568
7569 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
c0ab21c2
TT
7570 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7571 end. */
aaa75496 7572
c0ab21c2
TT
7573cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7574 struct abbrev_table *abbrev_table,
7575 int use_existing_cu, int keep,
7576 bool skip_partial)
7577 : die_reader_specs {},
7578 m_this_cu (this_cu),
7579 m_keep (keep)
c906108c 7580{
ed2dc618 7581 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7582 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7583 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7584 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7585 struct dwarf2_cu *cu;
c0ab21c2 7586 const gdb_byte *begin_info_ptr;
dee91e82 7587 struct signatured_type *sig_type = NULL;
4bdcc0c1 7588 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7589 /* Non-zero if CU currently points to a DWO file and we need to
7590 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7591 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7592 int rereading_dwo_cu = 0;
c906108c 7593
b4f54984 7594 if (dwarf_die_debug)
9d8780f0 7595 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7596 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7597 sect_offset_str (this_cu->sect_off));
09406207 7598
dee91e82
DE
7599 if (use_existing_cu)
7600 gdb_assert (keep);
23745b47 7601
a2ce51a0
DE
7602 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7603 file (instead of going through the stub), short-circuit all of this. */
7604 if (this_cu->reading_dwo_directly)
7605 {
7606 /* Narrow down the scope of possibilities to have to understand. */
7607 gdb_assert (this_cu->is_debug_types);
7608 gdb_assert (abbrev_table == NULL);
c0ab21c2 7609 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
a2ce51a0
DE
7610 return;
7611 }
7612
dee91e82
DE
7613 /* This is cheap if the section is already read in. */
7614 dwarf2_read_section (objfile, section);
7615
9c541725 7616 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7617
7618 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7619
7620 if (use_existing_cu && this_cu->cu != NULL)
7621 {
7622 cu = this_cu->cu;
42e7ad6c
DE
7623 /* If this CU is from a DWO file we need to start over, we need to
7624 refetch the attributes from the skeleton CU.
7625 This could be optimized by retrieving those attributes from when we
7626 were here the first time: the previous comp_unit_die was stored in
7627 comp_unit_obstack. But there's no data yet that we need this
7628 optimization. */
7629 if (cu->dwo_unit != NULL)
7630 rereading_dwo_cu = 1;
dee91e82
DE
7631 }
7632 else
7633 {
7634 /* If !use_existing_cu, this_cu->cu must be NULL. */
7635 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
7636 m_new_cu.reset (new dwarf2_cu (this_cu));
7637 cu = m_new_cu.get ();
42e7ad6c 7638 }
dee91e82 7639
b0c7bfa9 7640 /* Get the header. */
9c541725 7641 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7642 {
7643 /* We already have the header, there's no need to read it in again. */
9c541725 7644 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7645 }
7646 else
7647 {
3019eac3 7648 if (this_cu->is_debug_types)
dee91e82 7649 {
ed2dc618
SM
7650 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7651 &cu->header, section,
4bdcc0c1 7652 abbrev_section, info_ptr,
43988095 7653 rcuh_kind::TYPE);
dee91e82 7654
42e7ad6c
DE
7655 /* Since per_cu is the first member of struct signatured_type,
7656 we can go from a pointer to one to a pointer to the other. */
7657 sig_type = (struct signatured_type *) this_cu;
43988095 7658 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7659 gdb_assert (sig_type->type_offset_in_tu
7660 == cu->header.type_cu_offset_in_tu);
7661 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7662
42e7ad6c
DE
7663 /* LENGTH has not been set yet for type units if we're
7664 using .gdb_index. */
1ce1cefd 7665 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7666
7667 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7668 sig_type->type_offset_in_section =
7669 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7670
7671 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7672 }
7673 else
7674 {
ed2dc618
SM
7675 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7676 &cu->header, section,
4bdcc0c1 7677 abbrev_section,
43988095
JK
7678 info_ptr,
7679 rcuh_kind::COMPILE);
dee91e82 7680
9c541725 7681 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7682 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7683 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7684 }
7685 }
10b3939b 7686
6caca83c 7687 /* Skip dummy compilation units. */
dee91e82 7688 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7689 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7690 {
7691 dummy_p = true;
7692 return;
7693 }
6caca83c 7694
433df2d4
DE
7695 /* If we don't have them yet, read the abbrevs for this compilation unit.
7696 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7697 done. */
f4dc4d17 7698 if (abbrev_table != NULL)
685af9cd
TT
7699 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7700 else
f4dc4d17 7701 {
c0ab21c2 7702 m_abbrev_table_holder
685af9cd
TT
7703 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7704 cu->header.abbrev_sect_off);
c0ab21c2 7705 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7706 }
af703f96 7707
dee91e82 7708 /* Read the top level CU/TU die. */
c0ab21c2
TT
7709 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7710 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
93311388 7711
58f0c718 7712 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7713 {
7714 dummy_p = true;
7715 return;
7716 }
58f0c718 7717
b0c7bfa9 7718 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7719 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7720 table from the DWO file and pass the ownership over to us. It will be
7721 referenced from READER, so we must make sure to free it after we're done
7722 with READER.
7723
b0c7bfa9
DE
7724 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7725 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7726 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7727 if (dwo_name != nullptr)
3019eac3 7728 {
3019eac3 7729 struct dwo_unit *dwo_unit;
b0c7bfa9 7730 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7731
7732 if (has_children)
6a506a2d 7733 {
b98664d3 7734 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7735 " has children (offset %s) [in module %s]"),
7736 sect_offset_str (this_cu->sect_off),
7737 bfd_get_filename (abfd));
6a506a2d 7738 }
c0ab21c2 7739 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7740 if (dwo_unit != NULL)
3019eac3 7741 {
6a506a2d 7742 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7743 comp_unit_die, NULL,
c0ab21c2 7744 this, &info_ptr,
685af9cd 7745 &dwo_comp_unit_die, &has_children,
c0ab21c2 7746 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7747 {
7748 /* Dummy die. */
c0ab21c2 7749 dummy_p = true;
6a506a2d
DE
7750 return;
7751 }
7752 comp_unit_die = dwo_comp_unit_die;
7753 }
7754 else
7755 {
7756 /* Yikes, we couldn't find the rest of the DIE, we only have
7757 the stub. A complaint has already been logged. There's
7758 not much more we can do except pass on the stub DIE to
7759 die_reader_func. We don't want to throw an error on bad
7760 debug info. */
3019eac3
DE
7761 }
7762 }
c0ab21c2 7763}
3019eac3 7764
c0ab21c2
TT
7765cutu_reader::~cutu_reader ()
7766{
b0c7bfa9 7767 /* Done, clean up. */
c0ab21c2 7768 if (m_new_cu != NULL && m_keep && !dummy_p)
348e048f 7769 {
c0ab21c2
TT
7770 struct dwarf2_per_objfile *dwarf2_per_objfile
7771 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7772 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7773 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7774 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7775 /* The chain owns it now. */
c0ab21c2 7776 m_new_cu.release ();
348e048f 7777 }
dee91e82
DE
7778}
7779
18a8505e
AT
7780/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7781 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7782 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7783
7784 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7785 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7786
7787 We fill in THIS_CU->length.
7788
dee91e82 7789 THIS_CU->cu is always freed when done.
3019eac3 7790 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7791 to care whether it refers to the "main" CU or the DWO CU.
7792
7793 When parent_cu is passed, it is used to provide a default value for
7794 str_offsets_base and addr_base from the parent. */
dee91e82 7795
c0ab21c2
TT
7796cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7797 struct dwarf2_cu *parent_cu,
7798 struct dwo_file *dwo_file)
7799 : die_reader_specs {},
7800 m_this_cu (this_cu)
dee91e82 7801{
ed2dc618 7802 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7803 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7804 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7805 bfd *abfd = get_section_bfd_owner (section);
33e80786 7806 struct dwarf2_section_info *abbrev_section;
d521ce57 7807 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
7808 int has_children;
7809
b4f54984 7810 if (dwarf_die_debug)
9d8780f0 7811 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7812 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7813 sect_offset_str (this_cu->sect_off));
09406207 7814
dee91e82
DE
7815 gdb_assert (this_cu->cu == NULL);
7816
33e80786
DE
7817 abbrev_section = (dwo_file != NULL
7818 ? &dwo_file->sections.abbrev
7819 : get_abbrev_section_for_cu (this_cu));
7820
dee91e82
DE
7821 /* This is cheap if the section is already read in. */
7822 dwarf2_read_section (objfile, section);
7823
c0ab21c2 7824 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7825
9c541725 7826 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7827 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7828 &m_new_cu->header, section,
4bdcc0c1 7829 abbrev_section, info_ptr,
43988095
JK
7830 (this_cu->is_debug_types
7831 ? rcuh_kind::TYPE
7832 : rcuh_kind::COMPILE));
dee91e82 7833
18a8505e
AT
7834 if (parent_cu != nullptr)
7835 {
c0ab21c2
TT
7836 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7837 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7838 }
c0ab21c2 7839 this_cu->length = get_cu_length (&m_new_cu->header);
dee91e82
DE
7840
7841 /* Skip dummy compilation units. */
7842 if (info_ptr >= begin_info_ptr + this_cu->length
7843 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7844 {
7845 dummy_p = true;
7846 return;
7847 }
72bf9492 7848
c0ab21c2 7849 m_abbrev_table_holder
685af9cd 7850 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
c0ab21c2 7851 m_new_cu->header.abbrev_sect_off);
dee91e82 7852
c0ab21c2
TT
7853 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7854 m_abbrev_table_holder.get ());
7855 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
dee91e82
DE
7856}
7857
0018ea6f
DE
7858\f
7859/* Type Unit Groups.
dee91e82 7860
0018ea6f
DE
7861 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7862 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7863 so that all types coming from the same compilation (.o file) are grouped
7864 together. A future step could be to put the types in the same symtab as
7865 the CU the types ultimately came from. */
ff013f42 7866
f4dc4d17
DE
7867static hashval_t
7868hash_type_unit_group (const void *item)
7869{
9a3c8263
SM
7870 const struct type_unit_group *tu_group
7871 = (const struct type_unit_group *) item;
f4dc4d17 7872
094b34ac 7873 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7874}
348e048f
DE
7875
7876static int
f4dc4d17 7877eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7878{
9a3c8263
SM
7879 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7880 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7881
094b34ac 7882 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7883}
348e048f 7884
f4dc4d17
DE
7885/* Allocate a hash table for type unit groups. */
7886
7887static htab_t
ed2dc618 7888allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7889{
7890 return htab_create_alloc_ex (3,
7891 hash_type_unit_group,
7892 eq_type_unit_group,
7893 NULL,
ed2dc618 7894 &objfile->objfile_obstack,
f4dc4d17
DE
7895 hashtab_obstack_allocate,
7896 dummy_obstack_deallocate);
7897}
dee91e82 7898
f4dc4d17
DE
7899/* Type units that don't have DW_AT_stmt_list are grouped into their own
7900 partial symtabs. We combine several TUs per psymtab to not let the size
7901 of any one psymtab grow too big. */
7902#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7903#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7904
094b34ac 7905/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7906 Create the type_unit_group object used to hold one or more TUs. */
7907
7908static struct type_unit_group *
094b34ac 7909create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7910{
518817b3
SM
7911 struct dwarf2_per_objfile *dwarf2_per_objfile
7912 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7913 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7914 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7915 struct type_unit_group *tu_group;
f4dc4d17
DE
7916
7917 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7918 struct type_unit_group);
094b34ac 7919 per_cu = &tu_group->per_cu;
518817b3 7920 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7921
094b34ac
DE
7922 if (dwarf2_per_objfile->using_index)
7923 {
7924 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7925 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7926 }
7927 else
7928 {
9c541725 7929 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7930 dwarf2_psymtab *pst;
528e1572 7931 std::string name;
094b34ac
DE
7932
7933 /* Give the symtab a useful name for debug purposes. */
7934 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7935 name = string_printf ("<type_units_%d>",
7936 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7937 else
528e1572 7938 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7939
528e1572 7940 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7941 pst->anonymous = true;
094b34ac 7942 }
f4dc4d17 7943
094b34ac 7944 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7945 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7946
7947 return tu_group;
7948}
7949
094b34ac
DE
7950/* Look up the type_unit_group for type unit CU, and create it if necessary.
7951 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7952
7953static struct type_unit_group *
ff39bb5e 7954get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7955{
518817b3
SM
7956 struct dwarf2_per_objfile *dwarf2_per_objfile
7957 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7958 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7959 struct type_unit_group *tu_group;
7960 void **slot;
7961 unsigned int line_offset;
7962 struct type_unit_group type_unit_group_for_lookup;
7963
7964 if (dwarf2_per_objfile->type_unit_groups == NULL)
7965 {
7966 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7967 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7968 }
7969
7970 /* Do we need to create a new group, or can we use an existing one? */
7971
7972 if (stmt_list)
7973 {
7974 line_offset = DW_UNSND (stmt_list);
7975 ++tu_stats->nr_symtab_sharers;
7976 }
7977 else
7978 {
7979 /* Ugh, no stmt_list. Rare, but we have to handle it.
7980 We can do various things here like create one group per TU or
7981 spread them over multiple groups to split up the expansion work.
7982 To avoid worst case scenarios (too many groups or too large groups)
7983 we, umm, group them in bunches. */
7984 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7985 | (tu_stats->nr_stmt_less_type_units
7986 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7987 ++tu_stats->nr_stmt_less_type_units;
7988 }
7989
094b34ac 7990 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7991 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7992 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7993 &type_unit_group_for_lookup, INSERT);
7994 if (*slot != NULL)
7995 {
9a3c8263 7996 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7997 gdb_assert (tu_group != NULL);
7998 }
7999 else
8000 {
9c541725 8001 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8002 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8003 *slot = tu_group;
8004 ++tu_stats->nr_symtabs;
8005 }
8006
8007 return tu_group;
8008}
0018ea6f
DE
8009\f
8010/* Partial symbol tables. */
8011
8012/* Create a psymtab named NAME and assign it to PER_CU.
8013
8014 The caller must fill in the following details:
8015 dirname, textlow, texthigh. */
8016
891813be 8017static dwarf2_psymtab *
0018ea6f
DE
8018create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8019{
e3b94546 8020 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 8021 dwarf2_psymtab *pst;
0018ea6f 8022
891813be 8023 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 8024
6d94535f 8025 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
8026
8027 /* This is the glue that links PST into GDB's symbol API. */
891813be 8028 pst->per_cu_data = per_cu;
0018ea6f
DE
8029 per_cu->v.psymtab = pst;
8030
8031 return pst;
8032}
8033
c0ab21c2 8034/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
8035
8036static void
8037process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8038 const gdb_byte *info_ptr,
0018ea6f
DE
8039 struct die_info *comp_unit_die,
8040 int has_children,
c0ab21c2
TT
8041 int want_partial_unit,
8042 enum language pretend_language)
0018ea6f
DE
8043{
8044 struct dwarf2_cu *cu = reader->cu;
518817b3 8045 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8046 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8047 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8048 CORE_ADDR baseaddr;
8049 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 8050 dwarf2_psymtab *pst;
3a2b436a 8051 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8052 const char *filename;
0018ea6f 8053
c0ab21c2 8054 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
0018ea6f
DE
8055 return;
8056
8057 gdb_assert (! per_cu->is_debug_types);
8058
c0ab21c2 8059 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 8060
0018ea6f 8061 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8062 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8063 if (filename == NULL)
0018ea6f 8064 filename = "";
0018ea6f
DE
8065
8066 pst = create_partial_symtab (per_cu, filename);
8067
8068 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8069 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 8070
b3b3bada 8071 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
8072
8073 dwarf2_find_base_address (comp_unit_die, cu);
8074
8075 /* Possibly set the default values of LOWPC and HIGHPC from
8076 `DW_AT_ranges'. */
3a2b436a
JK
8077 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8078 &best_highpc, cu, pst);
8079 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8080 {
8081 CORE_ADDR low
8082 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8083 - baseaddr);
8084 CORE_ADDR high
8085 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8086 - baseaddr - 1);
8087 /* Store the contiguous range if it is not empty; it can be
8088 empty for CUs with no code. */
d320c2b5
TT
8089 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8090 low, high, pst);
79748972 8091 }
0018ea6f
DE
8092
8093 /* Check if comp unit has_children.
8094 If so, read the rest of the partial symbols from this comp unit.
8095 If not, there's no more debug_info for this comp unit. */
8096 if (has_children)
8097 {
8098 struct partial_die_info *first_die;
8099 CORE_ADDR lowpc, highpc;
8100
8101 lowpc = ((CORE_ADDR) -1);
8102 highpc = ((CORE_ADDR) 0);
8103
8104 first_die = load_partial_dies (reader, info_ptr, 1);
8105
8106 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8107 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8108
8109 /* If we didn't find a lowpc, set it to highpc to avoid
8110 complaints from `maint check'. */
8111 if (lowpc == ((CORE_ADDR) -1))
8112 lowpc = highpc;
8113
8114 /* If the compilation unit didn't have an explicit address range,
8115 then use the information extracted from its child dies. */
e385593e 8116 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8117 {
8118 best_lowpc = lowpc;
8119 best_highpc = highpc;
8120 }
8121 }
4ae976d1 8122 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8123 best_lowpc + baseaddr)
8124 - baseaddr);
4ae976d1 8125 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8126 best_highpc + baseaddr)
8127 - baseaddr);
0018ea6f 8128
8763cede 8129 end_psymtab_common (objfile, pst);
0018ea6f 8130
ae640021 8131 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8132 {
8133 int i;
ae640021 8134 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8135
8136 /* Fill in 'dependencies' here; we fill in 'users' in a
8137 post-pass. */
8138 pst->number_of_dependencies = len;
a9342b62
TT
8139 pst->dependencies
8140 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8141 for (i = 0; i < len; ++i)
8142 {
8143 pst->dependencies[i]
8144 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8145 }
0018ea6f 8146
ae640021 8147 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8148 }
8149
8150 /* Get the list of files included in the current compilation unit,
8151 and build a psymtab for each of them. */
8152 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8153
b4f54984 8154 if (dwarf_read_debug)
b926417a
TT
8155 fprintf_unfiltered (gdb_stdlog,
8156 "Psymtab for %s unit @%s: %s - %s"
8157 ", %d global, %d static syms\n",
8158 per_cu->is_debug_types ? "type" : "comp",
8159 sect_offset_str (per_cu->sect_off),
8160 paddress (gdbarch, pst->text_low (objfile)),
8161 paddress (gdbarch, pst->text_high (objfile)),
8162 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8163}
8164
8165/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8166 Process compilation unit THIS_CU for a psymtab. */
8167
8168static void
8169process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8170 int want_partial_unit,
8171 enum language pretend_language)
0018ea6f
DE
8172{
8173 /* If this compilation unit was already read in, free the
8174 cached copy in order to read it in again. This is
8175 necessary because we skipped some symbols when we first
8176 read in the compilation unit (see load_partial_dies).
8177 This problem could be avoided, but the benefit is unclear. */
8178 if (this_cu->cu != NULL)
8179 free_one_cached_comp_unit (this_cu);
8180
c0ab21c2
TT
8181 cutu_reader reader (this_cu, NULL, 0, 0, false);
8182
8183 if (reader.dummy_p)
f1902523 8184 {
c0ab21c2 8185 /* Nothing. */
f1902523 8186 }
c0ab21c2
TT
8187 else if (this_cu->is_debug_types)
8188 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
8189 reader.has_children);
8190 else
8191 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
8192 reader.comp_unit_die,
8193 reader.has_children,
8194 want_partial_unit,
8195 pretend_language);
0018ea6f
DE
8196
8197 /* Age out any secondary CUs. */
ed2dc618 8198 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8199}
f4dc4d17
DE
8200
8201/* Reader function for build_type_psymtabs. */
8202
8203static void
8204build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8205 const gdb_byte *info_ptr,
f4dc4d17 8206 struct die_info *type_unit_die,
c0ab21c2 8207 int has_children)
f4dc4d17 8208{
ed2dc618 8209 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8210 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8211 struct objfile *objfile = dwarf2_per_objfile->objfile;
8212 struct dwarf2_cu *cu = reader->cu;
8213 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8214 struct signatured_type *sig_type;
f4dc4d17
DE
8215 struct type_unit_group *tu_group;
8216 struct attribute *attr;
8217 struct partial_die_info *first_die;
8218 CORE_ADDR lowpc, highpc;
891813be 8219 dwarf2_psymtab *pst;
f4dc4d17 8220
0186c6a7
DE
8221 gdb_assert (per_cu->is_debug_types);
8222 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8223
8224 if (! has_children)
8225 return;
8226
8227 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8228 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8229
df07e2c7 8230 if (tu_group->tus == nullptr)
a8b3b8e9 8231 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8232 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8233
8234 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 8235 pst = create_partial_symtab (per_cu, "");
6d94535f 8236 pst->anonymous = true;
f4dc4d17
DE
8237
8238 first_die = load_partial_dies (reader, info_ptr, 1);
8239
8240 lowpc = (CORE_ADDR) -1;
8241 highpc = (CORE_ADDR) 0;
8242 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8243
8763cede 8244 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8245}
8246
73051182
DE
8247/* Struct used to sort TUs by their abbreviation table offset. */
8248
8249struct tu_abbrev_offset
8250{
b2bdb8cf
SM
8251 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8252 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8253 {}
8254
8255 signatured_type *sig_type;
73051182
DE
8256 sect_offset abbrev_offset;
8257};
8258
484cf504 8259/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8260
484cf504
TT
8261static bool
8262sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8263 const struct tu_abbrev_offset &b)
73051182 8264{
484cf504 8265 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8266}
8267
8268/* Efficiently read all the type units.
8269 This does the bulk of the work for build_type_psymtabs.
8270
8271 The efficiency is because we sort TUs by the abbrev table they use and
8272 only read each abbrev table once. In one program there are 200K TUs
8273 sharing 8K abbrev tables.
8274
8275 The main purpose of this function is to support building the
8276 dwarf2_per_objfile->type_unit_groups table.
8277 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8278 can collapse the search space by grouping them by stmt_list.
8279 The savings can be significant, in the same program from above the 200K TUs
8280 share 8K stmt_list tables.
8281
8282 FUNC is expected to call get_type_unit_group, which will create the
8283 struct type_unit_group if necessary and add it to
8284 dwarf2_per_objfile->type_unit_groups. */
8285
8286static void
ed2dc618 8287build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8288{
73051182 8289 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8290 abbrev_table_up abbrev_table;
73051182 8291 sect_offset abbrev_offset;
73051182
DE
8292
8293 /* It's up to the caller to not call us multiple times. */
8294 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8295
b2bdb8cf 8296 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8297 return;
8298
8299 /* TUs typically share abbrev tables, and there can be way more TUs than
8300 abbrev tables. Sort by abbrev table to reduce the number of times we
8301 read each abbrev table in.
8302 Alternatives are to punt or to maintain a cache of abbrev tables.
8303 This is simpler and efficient enough for now.
8304
8305 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8306 symtab to use). Typically TUs with the same abbrev offset have the same
8307 stmt_list value too so in practice this should work well.
8308
8309 The basic algorithm here is:
8310
8311 sort TUs by abbrev table
8312 for each TU with same abbrev table:
8313 read abbrev table if first user
8314 read TU top level DIE
8315 [IWBN if DWO skeletons had DW_AT_stmt_list]
8316 call FUNC */
8317
b4f54984 8318 if (dwarf_read_debug)
73051182
DE
8319 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8320
8321 /* Sort in a separate table to maintain the order of all_type_units
8322 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8323 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8324 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8325
8326 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8327 sorted_by_abbrev.emplace_back
8328 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8329 sig_type->per_cu.section,
8330 sig_type->per_cu.sect_off));
73051182 8331
484cf504
TT
8332 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8333 sort_tu_by_abbrev_offset);
73051182 8334
9c541725 8335 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8336
b2bdb8cf 8337 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8338 {
73051182
DE
8339 /* Switch to the next abbrev table if necessary. */
8340 if (abbrev_table == NULL
b2bdb8cf 8341 || tu.abbrev_offset != abbrev_offset)
73051182 8342 {
b2bdb8cf 8343 abbrev_offset = tu.abbrev_offset;
73051182 8344 abbrev_table =
ed2dc618
SM
8345 abbrev_table_read_table (dwarf2_per_objfile,
8346 &dwarf2_per_objfile->abbrev,
73051182
DE
8347 abbrev_offset);
8348 ++tu_stats->nr_uniq_abbrev_tables;
8349 }
8350
c0ab21c2
TT
8351 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8352 0, 0, false);
8353 if (!reader.dummy_p)
8354 build_type_psymtabs_reader (&reader, reader.info_ptr,
8355 reader.comp_unit_die,
8356 reader.has_children);
73051182 8357 }
6aa5f3a6 8358}
73051182 8359
6aa5f3a6
DE
8360/* Print collected type unit statistics. */
8361
8362static void
ed2dc618 8363print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8364{
8365 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8366
8367 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8368 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8369 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8370 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8371 tu_stats->nr_uniq_abbrev_tables);
8372 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8373 tu_stats->nr_symtabs);
8374 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8375 tu_stats->nr_symtab_sharers);
8376 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8377 tu_stats->nr_stmt_less_type_units);
8378 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8379 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8380}
8381
f4dc4d17
DE
8382/* Traversal function for build_type_psymtabs. */
8383
8384static int
8385build_type_psymtab_dependencies (void **slot, void *info)
8386{
ed2dc618
SM
8387 struct dwarf2_per_objfile *dwarf2_per_objfile
8388 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8389 struct objfile *objfile = dwarf2_per_objfile->objfile;
8390 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8391 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 8392 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 8393 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8394 int i;
8395
8396 gdb_assert (len > 0);
0186c6a7 8397 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8398
8399 pst->number_of_dependencies = len;
a9342b62 8400 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8401 for (i = 0; i < len; ++i)
f4dc4d17 8402 {
df07e2c7 8403 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8404 gdb_assert (iter->per_cu.is_debug_types);
8405 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8406 iter->type_unit_group = tu_group;
f4dc4d17
DE
8407 }
8408
df07e2c7
AB
8409 delete tu_group->tus;
8410 tu_group->tus = nullptr;
348e048f
DE
8411
8412 return 1;
8413}
8414
8415/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8416 Build partial symbol tables for the .debug_types comp-units. */
8417
8418static void
ed2dc618 8419build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8420{
ed2dc618 8421 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8422 return;
8423
ed2dc618 8424 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8425}
f4dc4d17 8426
6aa5f3a6
DE
8427/* Traversal function for process_skeletonless_type_unit.
8428 Read a TU in a DWO file and build partial symbols for it. */
8429
8430static int
8431process_skeletonless_type_unit (void **slot, void *info)
8432{
8433 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8434 struct dwarf2_per_objfile *dwarf2_per_objfile
8435 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8436 struct signatured_type find_entry, *entry;
8437
8438 /* If this TU doesn't exist in the global table, add it and read it in. */
8439
8440 if (dwarf2_per_objfile->signatured_types == NULL)
8441 {
8442 dwarf2_per_objfile->signatured_types
ed2dc618 8443 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8444 }
8445
8446 find_entry.signature = dwo_unit->signature;
8447 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8448 INSERT);
8449 /* If we've already seen this type there's nothing to do. What's happening
8450 is we're doing our own version of comdat-folding here. */
8451 if (*slot != NULL)
8452 return 1;
8453
8454 /* This does the job that create_all_type_units would have done for
8455 this TU. */
ed2dc618
SM
8456 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8457 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8458 *slot = entry;
8459
8460 /* This does the job that build_type_psymtabs_1 would have done. */
c0ab21c2
TT
8461 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8462 if (!reader.dummy_p)
8463 build_type_psymtabs_reader (&reader, reader.info_ptr,
8464 reader.comp_unit_die, reader.has_children);
6aa5f3a6
DE
8465
8466 return 1;
8467}
8468
8469/* Traversal function for process_skeletonless_type_units. */
8470
8471static int
8472process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8473{
8474 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8475
8476 if (dwo_file->tus != NULL)
8477 {
8478 htab_traverse_noresize (dwo_file->tus,
8479 process_skeletonless_type_unit, info);
8480 }
8481
8482 return 1;
8483}
8484
8485/* Scan all TUs of DWO files, verifying we've processed them.
8486 This is needed in case a TU was emitted without its skeleton.
8487 Note: This can't be done until we know what all the DWO files are. */
8488
8489static void
ed2dc618 8490process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8491{
8492 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8493 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8494 && dwarf2_per_objfile->dwo_files != NULL)
8495 {
51ac9db5 8496 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8497 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8498 dwarf2_per_objfile);
6aa5f3a6 8499 }
348e048f
DE
8500}
8501
ed2dc618 8502/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8503
8504static void
ed2dc618 8505set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8506{
b76e467d 8507 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8508 {
891813be 8509 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8510
36586728
TT
8511 if (pst == NULL)
8512 continue;
8513
b76e467d 8514 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8515 {
8516 /* Set the 'user' field only if it is not already set. */
8517 if (pst->dependencies[j]->user == NULL)
8518 pst->dependencies[j]->user = pst;
8519 }
8520 }
8521}
8522
93311388
DE
8523/* Build the partial symbol table by doing a quick pass through the
8524 .debug_info and .debug_abbrev sections. */
72bf9492 8525
93311388 8526static void
ed2dc618 8527dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8528{
ed2dc618 8529 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8530
b4f54984 8531 if (dwarf_read_debug)
45cfd468
DE
8532 {
8533 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8534 objfile_name (objfile));
45cfd468
DE
8535 }
8536
98bfdba5
PA
8537 dwarf2_per_objfile->reading_partial_symbols = 1;
8538
be391dca 8539 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8540
93311388
DE
8541 /* Any cached compilation units will be linked by the per-objfile
8542 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8543 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8544
ed2dc618 8545 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8546
ed2dc618 8547 create_all_comp_units (dwarf2_per_objfile);
c906108c 8548
60606b2c
TT
8549 /* Create a temporary address map on a temporary obstack. We later
8550 copy this to the final obstack. */
8268c778 8551 auto_obstack temp_obstack;
791afaa2
TT
8552
8553 scoped_restore save_psymtabs_addrmap
d320c2b5 8554 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8555 addrmap_create_mutable (&temp_obstack));
72bf9492 8556
b76e467d
SM
8557 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8558 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8559
6aa5f3a6 8560 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8561 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8562
8563 /* Now that all TUs have been processed we can fill in the dependencies. */
8564 if (dwarf2_per_objfile->type_unit_groups != NULL)
8565 {
8566 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8567 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8568 }
8569
b4f54984 8570 if (dwarf_read_debug)
ed2dc618 8571 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8572
ed2dc618 8573 set_partial_user (dwarf2_per_objfile);
95554aad 8574
d320c2b5
TT
8575 objfile->partial_symtabs->psymtabs_addrmap
8576 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8577 objfile->partial_symtabs->obstack ());
791afaa2
TT
8578 /* At this point we want to keep the address map. */
8579 save_psymtabs_addrmap.release ();
ff013f42 8580
b4f54984 8581 if (dwarf_read_debug)
45cfd468 8582 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8583 objfile_name (objfile));
ae038cb0
DJ
8584}
8585
dee91e82
DE
8586/* Load the partial DIEs for a secondary CU into memory.
8587 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8588
dee91e82
DE
8589static void
8590load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8591{
c0ab21c2
TT
8592 cutu_reader reader (this_cu, NULL, 1, 1, false);
8593
8594 if (!reader.dummy_p)
8595 {
8596 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8597 language_minimal);
8598
8599 /* Check if comp unit has_children.
8600 If so, read the rest of the partial symbols from this comp unit.
8601 If not, there's no more debug_info for this comp unit. */
8602 if (reader.has_children)
8603 load_partial_dies (&reader, reader.info_ptr, 0);
8604 }
ae038cb0
DJ
8605}
8606
ae038cb0 8607static void
ed2dc618 8608read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8609 struct dwarf2_section_info *section,
f1902523 8610 struct dwarf2_section_info *abbrev_section,
b76e467d 8611 unsigned int is_dwz)
ae038cb0 8612{
d521ce57 8613 const gdb_byte *info_ptr;
ed2dc618 8614 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8615
b4f54984 8616 if (dwarf_read_debug)
bf6af496 8617 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8618 get_section_name (section),
8619 get_section_file_name (section));
bf6af496 8620
36586728 8621 dwarf2_read_section (objfile, section);
ae038cb0 8622
36586728 8623 info_ptr = section->buffer;
6e70227d 8624
36586728 8625 while (info_ptr < section->buffer + section->size)
ae038cb0 8626 {
ae038cb0 8627 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8628
9c541725 8629 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8630
f1902523 8631 comp_unit_head cu_header;
ed2dc618
SM
8632 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8633 abbrev_section, info_ptr,
8634 rcuh_kind::COMPILE);
ae038cb0
DJ
8635
8636 /* Save the compilation unit for later lookup. */
f1902523
JK
8637 if (cu_header.unit_type != DW_UT_type)
8638 {
8639 this_cu = XOBNEW (&objfile->objfile_obstack,
8640 struct dwarf2_per_cu_data);
8641 memset (this_cu, 0, sizeof (*this_cu));
8642 }
8643 else
8644 {
8645 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8646 struct signatured_type);
8647 memset (sig_type, 0, sizeof (*sig_type));
8648 sig_type->signature = cu_header.signature;
8649 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8650 this_cu = &sig_type->per_cu;
8651 }
8652 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8653 this_cu->sect_off = sect_off;
f1902523 8654 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8655 this_cu->is_dwz = is_dwz;
e3b94546 8656 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8657 this_cu->section = section;
ae038cb0 8658
b76e467d 8659 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8660
8661 info_ptr = info_ptr + this_cu->length;
8662 }
36586728
TT
8663}
8664
8665/* Create a list of all compilation units in OBJFILE.
8666 This is only done for -readnow and building partial symtabs. */
8667
8668static void
ed2dc618 8669create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8670{
b76e467d 8671 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8672 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8673 &dwarf2_per_objfile->abbrev, 0);
36586728 8674
b76e467d 8675 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8676 if (dwz != NULL)
ed2dc618 8677 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8678 1);
c906108c
SS
8679}
8680
5734ee8b 8681/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8682 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8683 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8684 DW_AT_ranges). See the comments of add_partial_subprogram on how
8685 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8686
72bf9492
DJ
8687static void
8688scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8689 CORE_ADDR *highpc, int set_addrmap,
8690 struct dwarf2_cu *cu)
c906108c 8691{
72bf9492 8692 struct partial_die_info *pdi;
c906108c 8693
91c24f0a
DC
8694 /* Now, march along the PDI's, descending into ones which have
8695 interesting children but skipping the children of the other ones,
8696 until we reach the end of the compilation unit. */
c906108c 8697
72bf9492 8698 pdi = first_die;
91c24f0a 8699
72bf9492
DJ
8700 while (pdi != NULL)
8701 {
52356b79 8702 pdi->fixup (cu);
c906108c 8703
f55ee35c 8704 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8705 children, so we need to look at them. Ditto for anonymous
8706 enums. */
933c6fe4 8707
72bf9492 8708 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8709 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8710 || pdi->tag == DW_TAG_imported_unit
8711 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8712 {
72bf9492 8713 switch (pdi->tag)
c906108c
SS
8714 {
8715 case DW_TAG_subprogram:
b1dc1806 8716 case DW_TAG_inlined_subroutine:
cdc07690 8717 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8718 break;
72929c62 8719 case DW_TAG_constant:
c906108c
SS
8720 case DW_TAG_variable:
8721 case DW_TAG_typedef:
91c24f0a 8722 case DW_TAG_union_type:
72bf9492 8723 if (!pdi->is_declaration)
63d06c5c 8724 {
72bf9492 8725 add_partial_symbol (pdi, cu);
63d06c5c
DC
8726 }
8727 break;
c906108c 8728 case DW_TAG_class_type:
680b30c7 8729 case DW_TAG_interface_type:
c906108c 8730 case DW_TAG_structure_type:
72bf9492 8731 if (!pdi->is_declaration)
c906108c 8732 {
72bf9492 8733 add_partial_symbol (pdi, cu);
c906108c 8734 }
b7fee5a3
KS
8735 if ((cu->language == language_rust
8736 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8737 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8738 set_addrmap, cu);
c906108c 8739 break;
91c24f0a 8740 case DW_TAG_enumeration_type:
72bf9492
DJ
8741 if (!pdi->is_declaration)
8742 add_partial_enumeration (pdi, cu);
c906108c
SS
8743 break;
8744 case DW_TAG_base_type:
a02abb62 8745 case DW_TAG_subrange_type:
c906108c 8746 /* File scope base type definitions are added to the partial
c5aa993b 8747 symbol table. */
72bf9492 8748 add_partial_symbol (pdi, cu);
c906108c 8749 break;
d9fa45fe 8750 case DW_TAG_namespace:
cdc07690 8751 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8752 break;
5d7cb8df 8753 case DW_TAG_module:
59c35742
AB
8754 if (!pdi->is_declaration)
8755 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8756 break;
95554aad
TT
8757 case DW_TAG_imported_unit:
8758 {
8759 struct dwarf2_per_cu_data *per_cu;
8760
f4dc4d17
DE
8761 /* For now we don't handle imported units in type units. */
8762 if (cu->per_cu->is_debug_types)
8763 {
8764 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8765 " supported in type units [in module %s]"),
518817b3 8766 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8767 }
8768
e3b94546
SM
8769 per_cu = dwarf2_find_containing_comp_unit
8770 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8771 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8772
8773 /* Go read the partial unit, if needed. */
8774 if (per_cu->v.psymtab == NULL)
b93601f3 8775 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8776
ae640021 8777 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8778 }
8779 break;
74921315
KS
8780 case DW_TAG_imported_declaration:
8781 add_partial_symbol (pdi, cu);
8782 break;
c906108c
SS
8783 default:
8784 break;
8785 }
8786 }
8787
72bf9492
DJ
8788 /* If the die has a sibling, skip to the sibling. */
8789
8790 pdi = pdi->die_sibling;
8791 }
8792}
8793
8794/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8795
72bf9492 8796 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8797 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8798 Enumerators are an exception; they use the scope of their parent
8799 enumeration type, i.e. the name of the enumeration type is not
8800 prepended to the enumerator.
91c24f0a 8801
72bf9492
DJ
8802 There are two complexities. One is DW_AT_specification; in this
8803 case "parent" means the parent of the target of the specification,
8804 instead of the direct parent of the DIE. The other is compilers
8805 which do not emit DW_TAG_namespace; in this case we try to guess
8806 the fully qualified name of structure types from their members'
8807 linkage names. This must be done using the DIE's children rather
8808 than the children of any DW_AT_specification target. We only need
8809 to do this for structures at the top level, i.e. if the target of
8810 any DW_AT_specification (if any; otherwise the DIE itself) does not
8811 have a parent. */
8812
8813/* Compute the scope prefix associated with PDI's parent, in
8814 compilation unit CU. The result will be allocated on CU's
8815 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8816 field. NULL is returned if no prefix is necessary. */
15d034d0 8817static const char *
72bf9492
DJ
8818partial_die_parent_scope (struct partial_die_info *pdi,
8819 struct dwarf2_cu *cu)
8820{
15d034d0 8821 const char *grandparent_scope;
72bf9492 8822 struct partial_die_info *parent, *real_pdi;
91c24f0a 8823
72bf9492
DJ
8824 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8825 then this means the parent of the specification DIE. */
8826
8827 real_pdi = pdi;
72bf9492 8828 while (real_pdi->has_specification)
fb816e8b 8829 {
122cf0f2
AB
8830 auto res = find_partial_die (real_pdi->spec_offset,
8831 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8832 real_pdi = res.pdi;
8833 cu = res.cu;
8834 }
72bf9492
DJ
8835
8836 parent = real_pdi->die_parent;
8837 if (parent == NULL)
8838 return NULL;
8839
8840 if (parent->scope_set)
8841 return parent->scope;
8842
52356b79 8843 parent->fixup (cu);
72bf9492 8844
10b3939b 8845 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8846
acebe513
UW
8847 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8848 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8849 Work around this problem here. */
8850 if (cu->language == language_cplus
6e70227d 8851 && parent->tag == DW_TAG_namespace
acebe513
UW
8852 && strcmp (parent->name, "::") == 0
8853 && grandparent_scope == NULL)
8854 {
8855 parent->scope = NULL;
8856 parent->scope_set = 1;
8857 return NULL;
8858 }
8859
0a4b0913 8860 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8861 if (pdi->tag == DW_TAG_enumerator)
8862 /* Enumerators should not get the name of the enumeration as a prefix. */
8863 parent->scope = grandparent_scope;
8864 else if (parent->tag == DW_TAG_namespace
f55ee35c 8865 || parent->tag == DW_TAG_module
72bf9492
DJ
8866 || parent->tag == DW_TAG_structure_type
8867 || parent->tag == DW_TAG_class_type
680b30c7 8868 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8869 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8870 || parent->tag == DW_TAG_enumeration_type
8871 || (cu->language == language_fortran
8872 && parent->tag == DW_TAG_subprogram
8873 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8874 {
8875 if (grandparent_scope == NULL)
8876 parent->scope = parent->name;
8877 else
3e43a32a
MS
8878 parent->scope = typename_concat (&cu->comp_unit_obstack,
8879 grandparent_scope,
f55ee35c 8880 parent->name, 0, cu);
72bf9492 8881 }
72bf9492
DJ
8882 else
8883 {
8884 /* FIXME drow/2004-04-01: What should we be doing with
8885 function-local names? For partial symbols, we should probably be
8886 ignoring them. */
fa9c3fa0
TT
8887 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8888 dwarf_tag_name (parent->tag),
8889 sect_offset_str (pdi->sect_off));
72bf9492 8890 parent->scope = grandparent_scope;
c906108c
SS
8891 }
8892
72bf9492
DJ
8893 parent->scope_set = 1;
8894 return parent->scope;
8895}
8896
8897/* Return the fully scoped name associated with PDI, from compilation unit
8898 CU. The result will be allocated with malloc. */
4568ecf9 8899
43816ebc 8900static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8901partial_die_full_name (struct partial_die_info *pdi,
8902 struct dwarf2_cu *cu)
8903{
15d034d0 8904 const char *parent_scope;
72bf9492 8905
98bfdba5
PA
8906 /* If this is a template instantiation, we can not work out the
8907 template arguments from partial DIEs. So, unfortunately, we have
8908 to go through the full DIEs. At least any work we do building
8909 types here will be reused if full symbols are loaded later. */
8910 if (pdi->has_template_arguments)
8911 {
52356b79 8912 pdi->fixup (cu);
98bfdba5
PA
8913
8914 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8915 {
8916 struct die_info *die;
8917 struct attribute attr;
8918 struct dwarf2_cu *ref_cu = cu;
8919
b64f50a1 8920 /* DW_FORM_ref_addr is using section offset. */
b4069958 8921 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8922 attr.form = DW_FORM_ref_addr;
9c541725 8923 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8924 die = follow_die_ref (NULL, &attr, &ref_cu);
8925
43816ebc 8926 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8927 }
8928 }
8929
72bf9492
DJ
8930 parent_scope = partial_die_parent_scope (pdi, cu);
8931 if (parent_scope == NULL)
8932 return NULL;
8933 else
43816ebc
TT
8934 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8935 pdi->name, 0, cu));
c906108c
SS
8936}
8937
8938static void
72bf9492 8939add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8940{
518817b3
SM
8941 struct dwarf2_per_objfile *dwarf2_per_objfile
8942 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8943 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8944 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8945 CORE_ADDR addr = 0;
15d034d0 8946 const char *actual_name = NULL;
e142c38c
DJ
8947 CORE_ADDR baseaddr;
8948
b3b3bada 8949 baseaddr = objfile->text_section_offset ();
c906108c 8950
43816ebc
TT
8951 gdb::unique_xmalloc_ptr<char> built_actual_name
8952 = partial_die_full_name (pdi, cu);
15d034d0 8953 if (built_actual_name != NULL)
43816ebc 8954 actual_name = built_actual_name.get ();
63d06c5c 8955
72bf9492
DJ
8956 if (actual_name == NULL)
8957 actual_name = pdi->name;
8958
c906108c
SS
8959 switch (pdi->tag)
8960 {
b1dc1806 8961 case DW_TAG_inlined_subroutine:
c906108c 8962 case DW_TAG_subprogram:
79748972
TT
8963 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8964 - baseaddr);
0a4b0913
AB
8965 if (pdi->is_external
8966 || cu->language == language_ada
8967 || (cu->language == language_fortran
8968 && pdi->die_parent != NULL
8969 && pdi->die_parent->tag == DW_TAG_subprogram))
8970 {
8971 /* Normally, only "external" DIEs are part of the global scope.
8972 But in Ada and Fortran, we want to be able to access nested
8973 procedures globally. So all Ada and Fortran subprograms are
8974 stored in the global scope. */
31edb802 8975 add_psymbol_to_list (actual_name,
15d034d0 8976 built_actual_name != NULL,
f47fb265 8977 VAR_DOMAIN, LOC_BLOCK,
79748972 8978 SECT_OFF_TEXT (objfile),
75aedd27 8979 psymbol_placement::GLOBAL,
79748972
TT
8980 addr,
8981 cu->language, objfile);
c906108c
SS
8982 }
8983 else
8984 {
31edb802 8985 add_psymbol_to_list (actual_name,
15d034d0 8986 built_actual_name != NULL,
f47fb265 8987 VAR_DOMAIN, LOC_BLOCK,
79748972 8988 SECT_OFF_TEXT (objfile),
75aedd27 8989 psymbol_placement::STATIC,
1762568f 8990 addr, cu->language, objfile);
c906108c 8991 }
0c1b455e
TT
8992
8993 if (pdi->main_subprogram && actual_name != NULL)
8994 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8995 break;
72929c62 8996 case DW_TAG_constant:
31edb802 8997 add_psymbol_to_list (actual_name,
75aedd27
TT
8998 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8999 -1, (pdi->is_external
9000 ? psymbol_placement::GLOBAL
9001 : psymbol_placement::STATIC),
9002 0, cu->language, objfile);
72929c62 9003 break;
c906108c 9004 case DW_TAG_variable:
95554aad
TT
9005 if (pdi->d.locdesc)
9006 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9007
95554aad 9008 if (pdi->d.locdesc
caac4577
JG
9009 && addr == 0
9010 && !dwarf2_per_objfile->has_section_at_zero)
9011 {
9012 /* A global or static variable may also have been stripped
9013 out by the linker if unused, in which case its address
9014 will be nullified; do not add such variables into partial
9015 symbol table then. */
9016 }
9017 else if (pdi->is_external)
c906108c
SS
9018 {
9019 /* Global Variable.
9020 Don't enter into the minimal symbol tables as there is
9021 a minimal symbol table entry from the ELF symbols already.
9022 Enter into partial symbol table if it has a location
9023 descriptor or a type.
9024 If the location descriptor is missing, new_symbol will create
9025 a LOC_UNRESOLVED symbol, the address of the variable will then
9026 be determined from the minimal symbol table whenever the variable
9027 is referenced.
9028 The address for the partial symbol table entry is not
9029 used by GDB, but it comes in handy for debugging partial symbol
9030 table building. */
9031
95554aad 9032 if (pdi->d.locdesc || pdi->has_type)
31edb802 9033 add_psymbol_to_list (actual_name,
15d034d0 9034 built_actual_name != NULL,
f47fb265 9035 VAR_DOMAIN, LOC_STATIC,
79748972 9036 SECT_OFF_TEXT (objfile),
75aedd27 9037 psymbol_placement::GLOBAL,
79748972 9038 addr, cu->language, objfile);
c906108c
SS
9039 }
9040 else
9041 {
ff908ebf
AW
9042 int has_loc = pdi->d.locdesc != NULL;
9043
9044 /* Static Variable. Skip symbols whose value we cannot know (those
9045 without location descriptors or constant values). */
9046 if (!has_loc && !pdi->has_const_value)
43816ebc 9047 return;
ff908ebf 9048
31edb802 9049 add_psymbol_to_list (actual_name,
15d034d0 9050 built_actual_name != NULL,
f47fb265 9051 VAR_DOMAIN, LOC_STATIC,
79748972 9052 SECT_OFF_TEXT (objfile),
75aedd27 9053 psymbol_placement::STATIC,
79748972 9054 has_loc ? addr : 0,
f47fb265 9055 cu->language, objfile);
c906108c
SS
9056 }
9057 break;
9058 case DW_TAG_typedef:
9059 case DW_TAG_base_type:
a02abb62 9060 case DW_TAG_subrange_type:
31edb802 9061 add_psymbol_to_list (actual_name,
15d034d0 9062 built_actual_name != NULL,
79748972 9063 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9064 psymbol_placement::STATIC,
1762568f 9065 0, cu->language, objfile);
c906108c 9066 break;
74921315 9067 case DW_TAG_imported_declaration:
72bf9492 9068 case DW_TAG_namespace:
31edb802 9069 add_psymbol_to_list (actual_name,
15d034d0 9070 built_actual_name != NULL,
79748972 9071 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9072 psymbol_placement::GLOBAL,
1762568f 9073 0, cu->language, objfile);
72bf9492 9074 break;
530e8392 9075 case DW_TAG_module:
a5fd13a9
BH
9076 /* With Fortran 77 there might be a "BLOCK DATA" module
9077 available without any name. If so, we skip the module as it
9078 doesn't bring any value. */
9079 if (actual_name != nullptr)
31edb802 9080 add_psymbol_to_list (actual_name,
a5fd13a9
BH
9081 built_actual_name != NULL,
9082 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9083 psymbol_placement::GLOBAL,
9084 0, cu->language, objfile);
530e8392 9085 break;
c906108c 9086 case DW_TAG_class_type:
680b30c7 9087 case DW_TAG_interface_type:
c906108c
SS
9088 case DW_TAG_structure_type:
9089 case DW_TAG_union_type:
9090 case DW_TAG_enumeration_type:
fa4028e9
JB
9091 /* Skip external references. The DWARF standard says in the section
9092 about "Structure, Union, and Class Type Entries": "An incomplete
9093 structure, union or class type is represented by a structure,
9094 union or class entry that does not have a byte size attribute
9095 and that has a DW_AT_declaration attribute." */
9096 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 9097 return;
fa4028e9 9098
63d06c5c
DC
9099 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9100 static vs. global. */
31edb802 9101 add_psymbol_to_list (actual_name,
15d034d0 9102 built_actual_name != NULL,
79748972 9103 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9104 cu->language == language_cplus
75aedd27
TT
9105 ? psymbol_placement::GLOBAL
9106 : psymbol_placement::STATIC,
1762568f 9107 0, cu->language, objfile);
c906108c 9108
c906108c
SS
9109 break;
9110 case DW_TAG_enumerator:
31edb802 9111 add_psymbol_to_list (actual_name,
15d034d0 9112 built_actual_name != NULL,
79748972 9113 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9114 cu->language == language_cplus
75aedd27
TT
9115 ? psymbol_placement::GLOBAL
9116 : psymbol_placement::STATIC,
1762568f 9117 0, cu->language, objfile);
c906108c
SS
9118 break;
9119 default:
9120 break;
9121 }
9122}
9123
5c4e30ca
DC
9124/* Read a partial die corresponding to a namespace; also, add a symbol
9125 corresponding to that namespace to the symbol table. NAMESPACE is
9126 the name of the enclosing namespace. */
91c24f0a 9127
72bf9492
DJ
9128static void
9129add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9130 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9131 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9132{
72bf9492 9133 /* Add a symbol for the namespace. */
e7c27a73 9134
72bf9492 9135 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9136
9137 /* Now scan partial symbols in that namespace. */
9138
91c24f0a 9139 if (pdi->has_children)
cdc07690 9140 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9141}
9142
5d7cb8df
JK
9143/* Read a partial die corresponding to a Fortran module. */
9144
9145static void
9146add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9147 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9148{
530e8392
KB
9149 /* Add a symbol for the namespace. */
9150
9151 add_partial_symbol (pdi, cu);
9152
f55ee35c 9153 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9154
9155 if (pdi->has_children)
cdc07690 9156 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9157}
9158
b1dc1806
XR
9159/* Read a partial die corresponding to a subprogram or an inlined
9160 subprogram and create a partial symbol for that subprogram.
9161 When the CU language allows it, this routine also defines a partial
9162 symbol for each nested subprogram that this subprogram contains.
9163 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9164 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9165
cdc07690
YQ
9166 PDI may also be a lexical block, in which case we simply search
9167 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9168 Again, this is only performed when the CU language allows this
9169 type of definitions. */
9170
9171static void
9172add_partial_subprogram (struct partial_die_info *pdi,
9173 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9174 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9175{
b1dc1806 9176 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9177 {
9178 if (pdi->has_pc_info)
9179 {
9180 if (pdi->lowpc < *lowpc)
9181 *lowpc = pdi->lowpc;
9182 if (pdi->highpc > *highpc)
9183 *highpc = pdi->highpc;
cdc07690 9184 if (set_addrmap)
5734ee8b 9185 {
518817b3 9186 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9187 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9188 CORE_ADDR baseaddr;
b926417a
TT
9189 CORE_ADDR this_highpc;
9190 CORE_ADDR this_lowpc;
5734ee8b 9191
b3b3bada 9192 baseaddr = objfile->text_section_offset ();
b926417a
TT
9193 this_lowpc
9194 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9195 pdi->lowpc + baseaddr)
9196 - baseaddr);
9197 this_highpc
9198 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9199 pdi->highpc + baseaddr)
9200 - baseaddr);
d320c2b5 9201 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9202 this_lowpc, this_highpc - 1,
9291a0cd 9203 cu->per_cu->v.psymtab);
5734ee8b 9204 }
481860b3
GB
9205 }
9206
9207 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9208 {
bc30ff58 9209 if (!pdi->is_declaration)
e8d05480
JB
9210 /* Ignore subprogram DIEs that do not have a name, they are
9211 illegal. Do not emit a complaint at this point, we will
9212 do so when we convert this psymtab into a symtab. */
9213 if (pdi->name)
9214 add_partial_symbol (pdi, cu);
bc30ff58
JB
9215 }
9216 }
6e70227d 9217
bc30ff58
JB
9218 if (! pdi->has_children)
9219 return;
9220
0a4b0913 9221 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9222 {
9223 pdi = pdi->die_child;
9224 while (pdi != NULL)
9225 {
52356b79 9226 pdi->fixup (cu);
bc30ff58 9227 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9228 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9229 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9230 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9231 pdi = pdi->die_sibling;
9232 }
9233 }
9234}
9235
91c24f0a
DC
9236/* Read a partial die corresponding to an enumeration type. */
9237
72bf9492
DJ
9238static void
9239add_partial_enumeration (struct partial_die_info *enum_pdi,
9240 struct dwarf2_cu *cu)
91c24f0a 9241{
72bf9492 9242 struct partial_die_info *pdi;
91c24f0a
DC
9243
9244 if (enum_pdi->name != NULL)
72bf9492
DJ
9245 add_partial_symbol (enum_pdi, cu);
9246
9247 pdi = enum_pdi->die_child;
9248 while (pdi)
91c24f0a 9249 {
72bf9492 9250 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9251 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9252 else
72bf9492
DJ
9253 add_partial_symbol (pdi, cu);
9254 pdi = pdi->die_sibling;
91c24f0a 9255 }
91c24f0a
DC
9256}
9257
6caca83c
CC
9258/* Return the initial uleb128 in the die at INFO_PTR. */
9259
9260static unsigned int
d521ce57 9261peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9262{
9263 unsigned int bytes_read;
9264
9265 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9266}
9267
685af9cd
TT
9268/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9269 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9270
4bb7a0a7
DJ
9271 Return the corresponding abbrev, or NULL if the number is zero (indicating
9272 an empty DIE). In either case *BYTES_READ will be set to the length of
9273 the initial number. */
9274
9275static struct abbrev_info *
685af9cd
TT
9276peek_die_abbrev (const die_reader_specs &reader,
9277 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9278{
685af9cd 9279 dwarf2_cu *cu = reader.cu;
518817b3 9280 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9281 unsigned int abbrev_number
9282 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9283
9284 if (abbrev_number == 0)
9285 return NULL;
9286
685af9cd 9287 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9288 if (!abbrev)
9289 {
422b9917 9290 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9291 " at offset %s [in module %s]"),
422b9917 9292 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9293 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9294 }
9295
9296 return abbrev;
9297}
9298
93311388
DE
9299/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9300 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9301 DIE. Any children of the skipped DIEs will also be skipped. */
9302
d521ce57
TT
9303static const gdb_byte *
9304skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9305{
4bb7a0a7
DJ
9306 while (1)
9307 {
685af9cd
TT
9308 unsigned int bytes_read;
9309 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9310
4bb7a0a7
DJ
9311 if (abbrev == NULL)
9312 return info_ptr + bytes_read;
9313 else
dee91e82 9314 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9315 }
9316}
9317
93311388
DE
9318/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9319 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9320 abbrev corresponding to that skipped uleb128 should be passed in
9321 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9322 children. */
9323
d521ce57
TT
9324static const gdb_byte *
9325skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9326 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9327{
9328 unsigned int bytes_read;
9329 struct attribute attr;
dee91e82
DE
9330 bfd *abfd = reader->abfd;
9331 struct dwarf2_cu *cu = reader->cu;
d521ce57 9332 const gdb_byte *buffer = reader->buffer;
f664829e 9333 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9334 unsigned int form, i;
9335
9336 for (i = 0; i < abbrev->num_attrs; i++)
9337 {
9338 /* The only abbrev we care about is DW_AT_sibling. */
9339 if (abbrev->attrs[i].name == DW_AT_sibling)
9340 {
18a8505e
AT
9341 bool ignored;
9342 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9343 &ignored);
4bb7a0a7 9344 if (attr.form == DW_FORM_ref_addr)
b98664d3 9345 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9346 else
b9502d3f 9347 {
9c541725
PA
9348 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9349 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9350
9351 if (sibling_ptr < info_ptr)
b98664d3 9352 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9353 else if (sibling_ptr > reader->buffer_end)
9354 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9355 else
9356 return sibling_ptr;
9357 }
4bb7a0a7
DJ
9358 }
9359
9360 /* If it isn't DW_AT_sibling, skip this attribute. */
9361 form = abbrev->attrs[i].form;
9362 skip_attribute:
9363 switch (form)
9364 {
4bb7a0a7 9365 case DW_FORM_ref_addr:
ae411497
TT
9366 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9367 and later it is offset sized. */
9368 if (cu->header.version == 2)
9369 info_ptr += cu->header.addr_size;
9370 else
9371 info_ptr += cu->header.offset_size;
9372 break;
36586728
TT
9373 case DW_FORM_GNU_ref_alt:
9374 info_ptr += cu->header.offset_size;
9375 break;
ae411497 9376 case DW_FORM_addr:
4bb7a0a7
DJ
9377 info_ptr += cu->header.addr_size;
9378 break;
9379 case DW_FORM_data1:
9380 case DW_FORM_ref1:
9381 case DW_FORM_flag:
8fe0f950 9382 case DW_FORM_strx1:
4bb7a0a7
DJ
9383 info_ptr += 1;
9384 break;
2dc7f7b3 9385 case DW_FORM_flag_present:
43988095 9386 case DW_FORM_implicit_const:
2dc7f7b3 9387 break;
4bb7a0a7
DJ
9388 case DW_FORM_data2:
9389 case DW_FORM_ref2:
8fe0f950 9390 case DW_FORM_strx2:
4bb7a0a7
DJ
9391 info_ptr += 2;
9392 break;
8fe0f950
AT
9393 case DW_FORM_strx3:
9394 info_ptr += 3;
9395 break;
4bb7a0a7
DJ
9396 case DW_FORM_data4:
9397 case DW_FORM_ref4:
8fe0f950 9398 case DW_FORM_strx4:
4bb7a0a7
DJ
9399 info_ptr += 4;
9400 break;
9401 case DW_FORM_data8:
9402 case DW_FORM_ref8:
55f1336d 9403 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9404 info_ptr += 8;
9405 break;
0224619f
JK
9406 case DW_FORM_data16:
9407 info_ptr += 16;
9408 break;
4bb7a0a7 9409 case DW_FORM_string:
9b1c24c8 9410 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9411 info_ptr += bytes_read;
9412 break;
2dc7f7b3 9413 case DW_FORM_sec_offset:
4bb7a0a7 9414 case DW_FORM_strp:
36586728 9415 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9416 info_ptr += cu->header.offset_size;
9417 break;
2dc7f7b3 9418 case DW_FORM_exprloc:
4bb7a0a7
DJ
9419 case DW_FORM_block:
9420 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9421 info_ptr += bytes_read;
9422 break;
9423 case DW_FORM_block1:
9424 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9425 break;
9426 case DW_FORM_block2:
9427 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9428 break;
9429 case DW_FORM_block4:
9430 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9431 break;
336d760d 9432 case DW_FORM_addrx:
cf532bd1 9433 case DW_FORM_strx:
4bb7a0a7
DJ
9434 case DW_FORM_sdata:
9435 case DW_FORM_udata:
9436 case DW_FORM_ref_udata:
3019eac3
DE
9437 case DW_FORM_GNU_addr_index:
9438 case DW_FORM_GNU_str_index:
18a8505e 9439 case DW_FORM_rnglistx:
d521ce57 9440 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9441 break;
9442 case DW_FORM_indirect:
9443 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9444 info_ptr += bytes_read;
9445 /* We need to continue parsing from here, so just go back to
9446 the top. */
9447 goto skip_attribute;
9448
9449 default:
3e43a32a
MS
9450 error (_("Dwarf Error: Cannot handle %s "
9451 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9452 dwarf_form_name (form),
9453 bfd_get_filename (abfd));
9454 }
9455 }
9456
9457 if (abbrev->has_children)
dee91e82 9458 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9459 else
9460 return info_ptr;
9461}
9462
93311388 9463/* Locate ORIG_PDI's sibling.
dee91e82 9464 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9465
d521ce57 9466static const gdb_byte *
dee91e82
DE
9467locate_pdi_sibling (const struct die_reader_specs *reader,
9468 struct partial_die_info *orig_pdi,
d521ce57 9469 const gdb_byte *info_ptr)
91c24f0a
DC
9470{
9471 /* Do we know the sibling already? */
72bf9492 9472
91c24f0a
DC
9473 if (orig_pdi->sibling)
9474 return orig_pdi->sibling;
9475
9476 /* Are there any children to deal with? */
9477
9478 if (!orig_pdi->has_children)
9479 return info_ptr;
9480
4bb7a0a7 9481 /* Skip the children the long way. */
91c24f0a 9482
dee91e82 9483 return skip_children (reader, info_ptr);
91c24f0a
DC
9484}
9485
257e7a09 9486/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9487 not NULL. */
c906108c 9488
891813be
TT
9489void
9490dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9491{
ed2dc618
SM
9492 struct dwarf2_per_objfile *dwarf2_per_objfile
9493 = get_dwarf2_per_objfile (objfile);
9494
077cbab2
TT
9495 gdb_assert (!readin);
9496 /* If this psymtab is constructed from a debug-only objfile, the
9497 has_section_at_zero flag will not necessarily be correct. We
9498 can get the correct value for this flag by looking at the data
9499 associated with the (presumably stripped) associated objfile. */
9500 if (objfile->separate_debug_objfile_backlink)
c906108c 9501 {
077cbab2
TT
9502 struct dwarf2_per_objfile *dpo_backlink
9503 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9504
077cbab2
TT
9505 dwarf2_per_objfile->has_section_at_zero
9506 = dpo_backlink->has_section_at_zero;
9507 }
98bfdba5 9508
077cbab2 9509 dwarf2_per_objfile->reading_partial_symbols = 0;
c906108c 9510
8566b89b 9511 expand_psymtab (objfile);
95554aad 9512
ed2dc618 9513 process_cu_includes (dwarf2_per_objfile);
c906108c 9514}
9cdd5dbd
DE
9515\f
9516/* Reading in full CUs. */
c906108c 9517
10b3939b
DJ
9518/* Add PER_CU to the queue. */
9519
9520static void
95554aad
TT
9521queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9522 enum language pretend_language)
10b3939b
DJ
9523{
9524 struct dwarf2_queue_item *item;
9525
9526 per_cu->queued = 1;
8d749320 9527 item = XNEW (struct dwarf2_queue_item);
10b3939b 9528 item->per_cu = per_cu;
95554aad 9529 item->pretend_language = pretend_language;
10b3939b
DJ
9530 item->next = NULL;
9531
9532 if (dwarf2_queue == NULL)
9533 dwarf2_queue = item;
9534 else
9535 dwarf2_queue_tail->next = item;
9536
9537 dwarf2_queue_tail = item;
9538}
9539
89e63ee4
DE
9540/* If PER_CU is not yet queued, add it to the queue.
9541 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9542 dependency.
0907af0c 9543 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9544 meaning either PER_CU is already queued or it is already loaded.
9545
9546 N.B. There is an invariant here that if a CU is queued then it is loaded.
9547 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9548
9549static int
89e63ee4 9550maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9551 struct dwarf2_per_cu_data *per_cu,
9552 enum language pretend_language)
9553{
9554 /* We may arrive here during partial symbol reading, if we need full
9555 DIEs to process an unusual case (e.g. template arguments). Do
9556 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9557 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9558 {
9559 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9560 return 1;
9561 return 0;
9562 }
9563
9564 /* Mark the dependence relation so that we don't flush PER_CU
9565 too early. */
89e63ee4
DE
9566 if (dependent_cu != NULL)
9567 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9568
9569 /* If it's already on the queue, we have nothing to do. */
9570 if (per_cu->queued)
9571 return 0;
9572
9573 /* If the compilation unit is already loaded, just mark it as
9574 used. */
9575 if (per_cu->cu != NULL)
9576 {
9577 per_cu->cu->last_used = 0;
9578 return 0;
9579 }
9580
9581 /* Add it to the queue. */
9582 queue_comp_unit (per_cu, pretend_language);
9583
9584 return 1;
9585}
9586
10b3939b
DJ
9587/* Process the queue. */
9588
9589static void
ed2dc618 9590process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9591{
9592 struct dwarf2_queue_item *item, *next_item;
9593
b4f54984 9594 if (dwarf_read_debug)
45cfd468
DE
9595 {
9596 fprintf_unfiltered (gdb_stdlog,
9597 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9598 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9599 }
9600
03dd20cc
DJ
9601 /* The queue starts out with one item, but following a DIE reference
9602 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9603 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9604 {
cc12ce38
DE
9605 if ((dwarf2_per_objfile->using_index
9606 ? !item->per_cu->v.quick->compunit_symtab
9607 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9608 /* Skip dummy CUs. */
9609 && item->per_cu->cu != NULL)
f4dc4d17
DE
9610 {
9611 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9612 unsigned int debug_print_threshold;
247f5c4f 9613 char buf[100];
f4dc4d17 9614
247f5c4f 9615 if (per_cu->is_debug_types)
f4dc4d17 9616 {
247f5c4f
DE
9617 struct signatured_type *sig_type =
9618 (struct signatured_type *) per_cu;
9619
9d8780f0 9620 sprintf (buf, "TU %s at offset %s",
73be47f5 9621 hex_string (sig_type->signature),
9d8780f0 9622 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9623 /* There can be 100s of TUs.
9624 Only print them in verbose mode. */
9625 debug_print_threshold = 2;
f4dc4d17 9626 }
247f5c4f 9627 else
73be47f5 9628 {
9d8780f0
SM
9629 sprintf (buf, "CU at offset %s",
9630 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9631 debug_print_threshold = 1;
9632 }
247f5c4f 9633
b4f54984 9634 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9635 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9636
9637 if (per_cu->is_debug_types)
9638 process_full_type_unit (per_cu, item->pretend_language);
9639 else
9640 process_full_comp_unit (per_cu, item->pretend_language);
9641
b4f54984 9642 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9643 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9644 }
10b3939b
DJ
9645
9646 item->per_cu->queued = 0;
9647 next_item = item->next;
9648 xfree (item);
9649 }
9650
9651 dwarf2_queue_tail = NULL;
45cfd468 9652
b4f54984 9653 if (dwarf_read_debug)
45cfd468
DE
9654 {
9655 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9656 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9657 }
10b3939b
DJ
9658}
9659
10b3939b
DJ
9660/* Read in full symbols for PST, and anything it depends on. */
9661
8566b89b
TT
9662void
9663dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9664{
10b3939b 9665 struct dwarf2_per_cu_data *per_cu;
aaa75496 9666
8566b89b 9667 if (readin)
95554aad
TT
9668 return;
9669
0494dbec 9670 read_dependencies (objfile);
aaa75496 9671
8566b89b 9672 per_cu = per_cu_data;
10b3939b
DJ
9673
9674 if (per_cu == NULL)
aaa75496
JB
9675 {
9676 /* It's an include file, no symbols to read for it.
9677 Everything is in the parent symtab. */
8566b89b 9678 readin = true;
aaa75496
JB
9679 return;
9680 }
c906108c 9681
58f0c718 9682 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9683}
9684
dee91e82
DE
9685/* Trivial hash function for die_info: the hash value of a DIE
9686 is its offset in .debug_info for this objfile. */
10b3939b 9687
dee91e82
DE
9688static hashval_t
9689die_hash (const void *item)
10b3939b 9690{
9a3c8263 9691 const struct die_info *die = (const struct die_info *) item;
6502dd73 9692
9c541725 9693 return to_underlying (die->sect_off);
dee91e82 9694}
63d06c5c 9695
dee91e82
DE
9696/* Trivial comparison function for die_info structures: two DIEs
9697 are equal if they have the same offset. */
98bfdba5 9698
dee91e82
DE
9699static int
9700die_eq (const void *item_lhs, const void *item_rhs)
9701{
9a3c8263
SM
9702 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9703 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9704
9c541725 9705 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9706}
c906108c 9707
c0ab21c2 9708/* Load the DIEs associated with PER_CU into memory. */
c906108c 9709
dee91e82 9710static void
c0ab21c2
TT
9711load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9712 bool skip_partial,
9713 enum language pretend_language)
dee91e82 9714{
c0ab21c2
TT
9715 gdb_assert (! this_cu->is_debug_types);
9716
9717 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9718 if (reader.dummy_p)
9719 return;
9720
9721 struct dwarf2_cu *cu = reader.cu;
9722 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9723
dee91e82
DE
9724 gdb_assert (cu->die_hash == NULL);
9725 cu->die_hash =
9726 htab_create_alloc_ex (cu->header.length / 12,
9727 die_hash,
9728 die_eq,
9729 NULL,
9730 &cu->comp_unit_obstack,
9731 hashtab_obstack_allocate,
9732 dummy_obstack_deallocate);
e142c38c 9733
c0ab21c2
TT
9734 if (reader.has_children)
9735 reader.comp_unit_die->child
9736 = read_die_and_siblings (&reader, reader.info_ptr,
9737 &info_ptr, reader.comp_unit_die);
9738 cu->dies = reader.comp_unit_die;
dee91e82 9739 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9740
9741 /* We try not to read any attributes in this function, because not
9cdd5dbd 9742 all CUs needed for references have been loaded yet, and symbol
10b3939b 9743 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9744 or we won't be able to build types correctly.
9745 Similarly, if we do not read the producer, we can not apply
9746 producer-specific interpretation. */
c0ab21c2 9747 prepare_one_comp_unit (cu, cu->dies, pretend_language);
10b3939b
DJ
9748}
9749
3da10d80
KS
9750/* Add a DIE to the delayed physname list. */
9751
9752static void
9753add_to_method_list (struct type *type, int fnfield_index, int index,
9754 const char *name, struct die_info *die,
9755 struct dwarf2_cu *cu)
9756{
9757 struct delayed_method_info mi;
9758 mi.type = type;
9759 mi.fnfield_index = fnfield_index;
9760 mi.index = index;
9761 mi.name = name;
9762 mi.die = die;
c89b44cd 9763 cu->method_list.push_back (mi);
3da10d80
KS
9764}
9765
3693fdb3
PA
9766/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9767 "const" / "volatile". If so, decrements LEN by the length of the
9768 modifier and return true. Otherwise return false. */
9769
9770template<size_t N>
9771static bool
9772check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9773{
9774 size_t mod_len = sizeof (mod) - 1;
9775 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9776 {
9777 len -= mod_len;
9778 return true;
9779 }
9780 return false;
9781}
9782
3da10d80
KS
9783/* Compute the physnames of any methods on the CU's method list.
9784
9785 The computation of method physnames is delayed in order to avoid the
9786 (bad) condition that one of the method's formal parameters is of an as yet
9787 incomplete type. */
9788
9789static void
9790compute_delayed_physnames (struct dwarf2_cu *cu)
9791{
3693fdb3 9792 /* Only C++ delays computing physnames. */
c89b44cd 9793 if (cu->method_list.empty ())
3693fdb3
PA
9794 return;
9795 gdb_assert (cu->language == language_cplus);
9796
52941706 9797 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9798 {
1d06ead6 9799 const char *physname;
3da10d80 9800 struct fn_fieldlist *fn_flp
c89b44cd
TT
9801 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9802 physname = dwarf2_physname (mi.name, mi.die, cu);
9803 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9804 = physname ? physname : "";
3693fdb3
PA
9805
9806 /* Since there's no tag to indicate whether a method is a
9807 const/volatile overload, extract that information out of the
9808 demangled name. */
9809 if (physname != NULL)
9810 {
9811 size_t len = strlen (physname);
9812
9813 while (1)
9814 {
9815 if (physname[len] == ')') /* shortcut */
9816 break;
9817 else if (check_modifier (physname, len, " const"))
c89b44cd 9818 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9819 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9820 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9821 else
9822 break;
9823 }
9824 }
3da10d80 9825 }
c89b44cd
TT
9826
9827 /* The list is no longer needed. */
9828 cu->method_list.clear ();
3da10d80
KS
9829}
9830
a766d390
DE
9831/* Go objects should be embedded in a DW_TAG_module DIE,
9832 and it's not clear if/how imported objects will appear.
9833 To keep Go support simple until that's worked out,
9834 go back through what we've read and create something usable.
9835 We could do this while processing each DIE, and feels kinda cleaner,
9836 but that way is more invasive.
9837 This is to, for example, allow the user to type "p var" or "b main"
9838 without having to specify the package name, and allow lookups
9839 of module.object to work in contexts that use the expression
9840 parser. */
9841
9842static void
9843fixup_go_packaging (struct dwarf2_cu *cu)
9844{
421d1616 9845 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9846 struct pending *list;
9847 int i;
9848
c24bdb02 9849 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9850 list != NULL;
9851 list = list->next)
a766d390
DE
9852 {
9853 for (i = 0; i < list->nsyms; ++i)
9854 {
9855 struct symbol *sym = list->symbol[i];
9856
c1b5c1eb 9857 if (sym->language () == language_go
a766d390
DE
9858 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9859 {
421d1616
TT
9860 gdb::unique_xmalloc_ptr<char> this_package_name
9861 (go_symbol_package_name (sym));
a766d390
DE
9862
9863 if (this_package_name == NULL)
9864 continue;
9865 if (package_name == NULL)
421d1616 9866 package_name = std::move (this_package_name);
a766d390
DE
9867 else
9868 {
518817b3
SM
9869 struct objfile *objfile
9870 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9871 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9872 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9873 (symbol_symtab (sym) != NULL
9874 ? symtab_to_filename_for_display
9875 (symbol_symtab (sym))
e3b94546 9876 : objfile_name (objfile)),
421d1616 9877 this_package_name.get (), package_name.get ());
a766d390
DE
9878 }
9879 }
9880 }
9881 }
9882
9883 if (package_name != NULL)
9884 {
518817b3 9885 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9886 const char *saved_package_name
421d1616 9887 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9888 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9889 saved_package_name);
a766d390
DE
9890 struct symbol *sym;
9891
e623cf5d 9892 sym = allocate_symbol (objfile);
d3ecddab 9893 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9894 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9895 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9896 e.g., "main" finds the "main" module and not C's main(). */
9897 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9898 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9899 SYMBOL_TYPE (sym) = type;
9900
c24bdb02 9901 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9902 }
9903}
9904
c9317f21
TT
9905/* Allocate a fully-qualified name consisting of the two parts on the
9906 obstack. */
9907
9908static const char *
9909rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9910{
9911 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9912}
9913
9914/* A helper that allocates a struct discriminant_info to attach to a
9915 union type. */
9916
9917static struct discriminant_info *
9918alloc_discriminant_info (struct type *type, int discriminant_index,
9919 int default_index)
9920{
9921 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9922 gdb_assert (discriminant_index == -1
9923 || (discriminant_index >= 0
9924 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9925 gdb_assert (default_index == -1
c7b15a66 9926 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9927
9928 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9929
9930 struct discriminant_info *disc
9931 = ((struct discriminant_info *)
9932 TYPE_ZALLOC (type,
9933 offsetof (struct discriminant_info, discriminants)
9934 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9935 disc->default_index = default_index;
9936 disc->discriminant_index = discriminant_index;
9937
9938 struct dynamic_prop prop;
9939 prop.kind = PROP_UNDEFINED;
9940 prop.data.baton = disc;
9941
9942 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9943
9944 return disc;
9945}
9946
9947/* Some versions of rustc emitted enums in an unusual way.
9948
9949 Ordinary enums were emitted as unions. The first element of each
9950 structure in the union was named "RUST$ENUM$DISR". This element
9951 held the discriminant.
9952
9953 These versions of Rust also implemented the "non-zero"
9954 optimization. When the enum had two values, and one is empty and
9955 the other holds a pointer that cannot be zero, the pointer is used
9956 as the discriminant, with a zero value meaning the empty variant.
9957 Here, the union's first member is of the form
9958 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9959 where the fieldnos are the indices of the fields that should be
9960 traversed in order to find the field (which may be several fields deep)
9961 and the variantname is the name of the variant of the case when the
9962 field is zero.
9963
9964 This function recognizes whether TYPE is of one of these forms,
9965 and, if so, smashes it to be a variant type. */
9966
9967static void
9968quirk_rust_enum (struct type *type, struct objfile *objfile)
9969{
9970 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9971
9972 /* We don't need to deal with empty enums. */
9973 if (TYPE_NFIELDS (type) == 0)
9974 return;
9975
9976#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9977 if (TYPE_NFIELDS (type) == 1
9978 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9979 {
9980 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9981
9982 /* Decode the field name to find the offset of the
9983 discriminant. */
9984 ULONGEST bit_offset = 0;
9985 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9986 while (name[0] >= '0' && name[0] <= '9')
9987 {
9988 char *tail;
9989 unsigned long index = strtoul (name, &tail, 10);
9990 name = tail;
9991 if (*name != '$'
9992 || index >= TYPE_NFIELDS (field_type)
9993 || (TYPE_FIELD_LOC_KIND (field_type, index)
9994 != FIELD_LOC_KIND_BITPOS))
9995 {
b98664d3 9996 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9997 "[in module %s]"),
9998 TYPE_FIELD_NAME (type, 0),
9999 objfile_name (objfile));
10000 return;
10001 }
10002 ++name;
10003
10004 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10005 field_type = TYPE_FIELD_TYPE (field_type, index);
10006 }
10007
10008 /* Make a union to hold the variants. */
10009 struct type *union_type = alloc_type (objfile);
10010 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10011 TYPE_NFIELDS (union_type) = 3;
10012 TYPE_FIELDS (union_type)
10013 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10014 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10015 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10016
10017 /* Put the discriminant must at index 0. */
10018 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10019 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10020 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10021 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10022
10023 /* The order of fields doesn't really matter, so put the real
10024 field at index 1 and the data-less field at index 2. */
10025 struct discriminant_info *disc
10026 = alloc_discriminant_info (union_type, 0, 1);
10027 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10028 TYPE_FIELD_NAME (union_type, 1)
10029 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10030 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10031 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10032 TYPE_FIELD_NAME (union_type, 1));
10033
10034 const char *dataless_name
10035 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10036 name);
10037 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10038 dataless_name);
10039 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10040 /* NAME points into the original discriminant name, which
10041 already has the correct lifetime. */
10042 TYPE_FIELD_NAME (union_type, 2) = name;
10043 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10044 disc->discriminants[2] = 0;
10045
10046 /* Smash this type to be a structure type. We have to do this
10047 because the type has already been recorded. */
10048 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10049 TYPE_NFIELDS (type) = 1;
10050 TYPE_FIELDS (type)
10051 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10052
10053 /* Install the variant part. */
10054 TYPE_FIELD_TYPE (type, 0) = union_type;
10055 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10056 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10057 }
77c2dba3
TT
10058 /* A union with a single anonymous field is probably an old-style
10059 univariant enum. */
10060 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10061 {
c9317f21
TT
10062 /* Smash this type to be a structure type. We have to do this
10063 because the type has already been recorded. */
10064 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10065
10066 /* Make a union to hold the variants. */
10067 struct type *union_type = alloc_type (objfile);
10068 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10069 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10070 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10071 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10072 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10073
10074 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10075 const char *variant_name
10076 = rust_last_path_segment (TYPE_NAME (field_type));
10077 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10078 TYPE_NAME (field_type)
10079 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10080 TYPE_NAME (type), variant_name);
c9317f21
TT
10081
10082 /* Install the union in the outer struct type. */
10083 TYPE_NFIELDS (type) = 1;
10084 TYPE_FIELDS (type)
10085 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10086 TYPE_FIELD_TYPE (type, 0) = union_type;
10087 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10088 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10089
10090 alloc_discriminant_info (union_type, -1, 0);
10091 }
10092 else
10093 {
10094 struct type *disr_type = nullptr;
10095 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10096 {
10097 disr_type = TYPE_FIELD_TYPE (type, i);
10098
a037790e
TT
10099 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10100 {
10101 /* All fields of a true enum will be structs. */
10102 return;
10103 }
10104 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10105 {
10106 /* Could be data-less variant, so keep going. */
a037790e 10107 disr_type = nullptr;
c9317f21
TT
10108 }
10109 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10110 "RUST$ENUM$DISR") != 0)
10111 {
10112 /* Not a Rust enum. */
10113 return;
10114 }
10115 else
10116 {
10117 /* Found one. */
10118 break;
10119 }
10120 }
10121
10122 /* If we got here without a discriminant, then it's probably
10123 just a union. */
10124 if (disr_type == nullptr)
10125 return;
10126
10127 /* Smash this type to be a structure type. We have to do this
10128 because the type has already been recorded. */
10129 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10130
10131 /* Make a union to hold the variants. */
10132 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10133 struct type *union_type = alloc_type (objfile);
10134 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10135 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10136 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10137 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10138 TYPE_FIELDS (union_type)
10139 = (struct field *) TYPE_ZALLOC (union_type,
10140 (TYPE_NFIELDS (union_type)
10141 * sizeof (struct field)));
10142
10143 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10144 TYPE_NFIELDS (type) * sizeof (struct field));
10145
10146 /* Install the discriminant at index 0 in the union. */
10147 TYPE_FIELD (union_type, 0) = *disr_field;
10148 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10149 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10150
10151 /* Install the union in the outer struct type. */
10152 TYPE_FIELD_TYPE (type, 0) = union_type;
10153 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10154 TYPE_NFIELDS (type) = 1;
10155
10156 /* Set the size and offset of the union type. */
10157 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10158
10159 /* We need a way to find the correct discriminant given a
10160 variant name. For convenience we build a map here. */
10161 struct type *enum_type = FIELD_TYPE (*disr_field);
10162 std::unordered_map<std::string, ULONGEST> discriminant_map;
10163 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10164 {
10165 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10166 {
10167 const char *name
10168 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10169 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10170 }
10171 }
10172
10173 int n_fields = TYPE_NFIELDS (union_type);
10174 struct discriminant_info *disc
10175 = alloc_discriminant_info (union_type, 0, -1);
10176 /* Skip the discriminant here. */
10177 for (int i = 1; i < n_fields; ++i)
10178 {
10179 /* Find the final word in the name of this variant's type.
10180 That name can be used to look up the correct
10181 discriminant. */
10182 const char *variant_name
10183 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10184 i)));
10185
10186 auto iter = discriminant_map.find (variant_name);
10187 if (iter != discriminant_map.end ())
10188 disc->discriminants[i] = iter->second;
10189
bedda9ac 10190 /* Remove the discriminant field, if it exists. */
c9317f21 10191 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10192 if (TYPE_NFIELDS (sub_type) > 0)
10193 {
10194 --TYPE_NFIELDS (sub_type);
10195 ++TYPE_FIELDS (sub_type);
10196 }
c9317f21
TT
10197 TYPE_FIELD_NAME (union_type, i) = variant_name;
10198 TYPE_NAME (sub_type)
10199 = rust_fully_qualify (&objfile->objfile_obstack,
10200 TYPE_NAME (type), variant_name);
10201 }
10202 }
10203}
10204
10205/* Rewrite some Rust unions to be structures with variants parts. */
10206
10207static void
10208rust_union_quirks (struct dwarf2_cu *cu)
10209{
10210 gdb_assert (cu->language == language_rust);
52941706
SM
10211 for (type *type_ : cu->rust_unions)
10212 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10213 /* We don't need this any more. */
10214 cu->rust_unions.clear ();
c9317f21
TT
10215}
10216
95554aad
TT
10217/* Return the symtab for PER_CU. This works properly regardless of
10218 whether we're using the index or psymtabs. */
10219
43f3e411
DE
10220static struct compunit_symtab *
10221get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10222{
ed2dc618 10223 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10224 ? per_cu->v.quick->compunit_symtab
10225 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10226}
10227
10228/* A helper function for computing the list of all symbol tables
10229 included by PER_CU. */
10230
10231static void
4c39bc03 10232recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10233 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10234 struct dwarf2_per_cu_data *per_cu,
43f3e411 10235 struct compunit_symtab *immediate_parent)
95554aad
TT
10236{
10237 void **slot;
43f3e411 10238 struct compunit_symtab *cust;
95554aad
TT
10239
10240 slot = htab_find_slot (all_children, per_cu, INSERT);
10241 if (*slot != NULL)
10242 {
10243 /* This inclusion and its children have been processed. */
10244 return;
10245 }
10246
10247 *slot = per_cu;
10248 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10249 cust = get_compunit_symtab (per_cu);
10250 if (cust != NULL)
ec94af83
DE
10251 {
10252 /* If this is a type unit only add its symbol table if we haven't
10253 seen it yet (type unit per_cu's can share symtabs). */
10254 if (per_cu->is_debug_types)
10255 {
43f3e411 10256 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10257 if (*slot == NULL)
10258 {
43f3e411 10259 *slot = cust;
4c39bc03 10260 result->push_back (cust);
43f3e411
DE
10261 if (cust->user == NULL)
10262 cust->user = immediate_parent;
ec94af83
DE
10263 }
10264 }
10265 else
f9125b6c 10266 {
4c39bc03 10267 result->push_back (cust);
43f3e411
DE
10268 if (cust->user == NULL)
10269 cust->user = immediate_parent;
f9125b6c 10270 }
ec94af83 10271 }
95554aad 10272
ae640021
AB
10273 if (!per_cu->imported_symtabs_empty ())
10274 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10275 {
10276 recursively_compute_inclusions (result, all_children,
10277 all_type_symtabs, ptr, cust);
10278 }
95554aad
TT
10279}
10280
43f3e411 10281/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10282 PER_CU. */
10283
10284static void
43f3e411 10285compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10286{
f4dc4d17
DE
10287 gdb_assert (! per_cu->is_debug_types);
10288
ae640021 10289 if (!per_cu->imported_symtabs_empty ())
95554aad 10290 {
ae640021 10291 int len;
4c39bc03 10292 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10293 htab_t all_children, all_type_symtabs;
43f3e411 10294 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10295
10296 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10297 if (cust == NULL)
95554aad
TT
10298 return;
10299
10300 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10301 NULL, xcalloc, xfree);
ec94af83
DE
10302 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10303 NULL, xcalloc, xfree);
95554aad 10304
ae640021 10305 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10306 {
10307 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10308 all_type_symtabs, ptr, cust);
ec94af83 10309 }
95554aad 10310
ec94af83 10311 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10312 len = result_symtabs.size ();
43f3e411 10313 cust->includes
ed2dc618 10314 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10315 struct compunit_symtab *, len + 1);
4c39bc03
TT
10316 memcpy (cust->includes, result_symtabs.data (),
10317 len * sizeof (compunit_symtab *));
43f3e411 10318 cust->includes[len] = NULL;
95554aad 10319
95554aad 10320 htab_delete (all_children);
ec94af83 10321 htab_delete (all_type_symtabs);
95554aad
TT
10322 }
10323}
10324
10325/* Compute the 'includes' field for the symtabs of all the CUs we just
10326 read. */
10327
10328static void
ed2dc618 10329process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10330{
71b73764 10331 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10332 {
10333 if (! iter->is_debug_types)
43f3e411 10334 compute_compunit_symtab_includes (iter);
f4dc4d17 10335 }
95554aad 10336
c5d0225d 10337 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10338}
10339
9cdd5dbd 10340/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10341 already been loaded into memory. */
10342
10343static void
95554aad
TT
10344process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10345 enum language pretend_language)
10b3939b 10346{
10b3939b 10347 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10348 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10349 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10350 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10351 CORE_ADDR lowpc, highpc;
43f3e411 10352 struct compunit_symtab *cust;
10b3939b 10353 CORE_ADDR baseaddr;
4359dff1 10354 struct block *static_block;
3e29f34a 10355 CORE_ADDR addr;
10b3939b 10356
b3b3bada 10357 baseaddr = objfile->text_section_offset ();
10b3939b 10358
c89b44cd
TT
10359 /* Clear the list here in case something was left over. */
10360 cu->method_list.clear ();
10b3939b 10361
95554aad
TT
10362 cu->language = pretend_language;
10363 cu->language_defn = language_def (cu->language);
10364
c906108c 10365 /* Do line number decoding in read_file_scope () */
10b3939b 10366 process_die (cu->dies, cu);
c906108c 10367
a766d390
DE
10368 /* For now fudge the Go package. */
10369 if (cu->language == language_go)
10370 fixup_go_packaging (cu);
10371
5f48f8f3 10372 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10373 should be complete, and it should now be safe to compute all of the
10374 physnames. */
10375 compute_delayed_physnames (cu);
3da10d80 10376
c9317f21
TT
10377 if (cu->language == language_rust)
10378 rust_union_quirks (cu);
10379
fae299cd
DC
10380 /* Some compilers don't define a DW_AT_high_pc attribute for the
10381 compilation unit. If the DW_AT_high_pc is missing, synthesize
10382 it, by scanning the DIE's below the compilation unit. */
10b3939b 10383 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10384
3e29f34a 10385 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10386 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10387
10388 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10389 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10390 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10391 addrmap to help ensure it has an accurate map of pc values belonging to
10392 this comp unit. */
10393 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10394
c24bdb02 10395 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10396 SECT_OFF_TEXT (objfile),
10397 0);
c906108c 10398
43f3e411 10399 if (cust != NULL)
c906108c 10400 {
df15bd07 10401 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10402
8be455d7
JK
10403 /* Set symtab language to language from DW_AT_language. If the
10404 compilation is from a C file generated by language preprocessors, do
10405 not set the language if it was already deduced by start_subfile. */
43f3e411 10406 if (!(cu->language == language_c
40e3ad0e 10407 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10408 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10409
10410 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10411 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10412 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10413 there were bugs in prologue debug info, fixed later in GCC-4.5
10414 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10415
10416 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10417 needed, it would be wrong due to missing DW_AT_producer there.
10418
10419 Still one can confuse GDB by using non-standard GCC compilation
10420 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10421 */
ab260dad 10422 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10423 cust->locations_valid = 1;
e0d00bc7
JK
10424
10425 if (gcc_4_minor >= 5)
43f3e411 10426 cust->epilogue_unwind_valid = 1;
96408a79 10427
43f3e411 10428 cust->call_site_htab = cu->call_site_htab;
c906108c 10429 }
9291a0cd
TT
10430
10431 if (dwarf2_per_objfile->using_index)
43f3e411 10432 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10433 else
10434 {
891813be 10435 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 10436 pst->compunit_symtab = cust;
6d94535f 10437 pst->readin = true;
9291a0cd 10438 }
c906108c 10439
95554aad 10440 /* Push it for inclusion processing later. */
c5d0225d 10441 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10442
10443 /* Not needed any more. */
c24bdb02 10444 cu->reset_builder ();
f4dc4d17 10445}
45cfd468 10446
f4dc4d17
DE
10447/* Generate full symbol information for type unit PER_CU, whose DIEs have
10448 already been loaded into memory. */
10449
10450static void
10451process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10452 enum language pretend_language)
10453{
10454 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10455 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10456 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10457 struct compunit_symtab *cust;
0186c6a7
DE
10458 struct signatured_type *sig_type;
10459
10460 gdb_assert (per_cu->is_debug_types);
10461 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10462
c89b44cd
TT
10463 /* Clear the list here in case something was left over. */
10464 cu->method_list.clear ();
f4dc4d17 10465
f4dc4d17
DE
10466 cu->language = pretend_language;
10467 cu->language_defn = language_def (cu->language);
10468
10469 /* The symbol tables are set up in read_type_unit_scope. */
10470 process_die (cu->dies, cu);
10471
10472 /* For now fudge the Go package. */
10473 if (cu->language == language_go)
10474 fixup_go_packaging (cu);
10475
5f48f8f3 10476 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10477 should be complete, and it should now be safe to compute all of the
10478 physnames. */
10479 compute_delayed_physnames (cu);
f4dc4d17 10480
c9317f21
TT
10481 if (cu->language == language_rust)
10482 rust_union_quirks (cu);
10483
f4dc4d17
DE
10484 /* TUs share symbol tables.
10485 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10486 of it with end_expandable_symtab. Otherwise, complete the addition of
10487 this TU's symbols to the existing symtab. */
43f3e411 10488 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10489 {
c24bdb02
KS
10490 buildsym_compunit *builder = cu->get_builder ();
10491 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10492 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10493
43f3e411 10494 if (cust != NULL)
f4dc4d17
DE
10495 {
10496 /* Set symtab language to language from DW_AT_language. If the
10497 compilation is from a C file generated by language preprocessors,
10498 do not set the language if it was already deduced by
10499 start_subfile. */
43f3e411
DE
10500 if (!(cu->language == language_c
10501 && COMPUNIT_FILETABS (cust)->language != language_c))
10502 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10503 }
10504 }
10505 else
10506 {
c24bdb02 10507 cu->get_builder ()->augment_type_symtab ();
43f3e411 10508 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10509 }
10510
10511 if (dwarf2_per_objfile->using_index)
43f3e411 10512 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10513 else
10514 {
891813be 10515 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 10516 pst->compunit_symtab = cust;
6d94535f 10517 pst->readin = true;
45cfd468 10518 }
804d2729
TT
10519
10520 /* Not needed any more. */
c24bdb02 10521 cu->reset_builder ();
c906108c
SS
10522}
10523
95554aad
TT
10524/* Process an imported unit DIE. */
10525
10526static void
10527process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10528{
10529 struct attribute *attr;
10530
f4dc4d17
DE
10531 /* For now we don't handle imported units in type units. */
10532 if (cu->per_cu->is_debug_types)
10533 {
10534 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10535 " supported in type units [in module %s]"),
518817b3 10536 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10537 }
10538
95554aad
TT
10539 attr = dwarf2_attr (die, DW_AT_import, cu);
10540 if (attr != NULL)
10541 {
9c541725
PA
10542 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10543 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10544 dwarf2_per_cu_data *per_cu
e3b94546 10545 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10546 cu->per_cu->dwarf2_per_objfile);
95554aad 10547
69d751e3 10548 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10549 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10550 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10551
ae640021 10552 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10553 }
10554}
10555
4c8aa72d
PA
10556/* RAII object that represents a process_die scope: i.e.,
10557 starts/finishes processing a DIE. */
10558class process_die_scope
adde2bff 10559{
4c8aa72d
PA
10560public:
10561 process_die_scope (die_info *die, dwarf2_cu *cu)
10562 : m_die (die), m_cu (cu)
10563 {
10564 /* We should only be processing DIEs not already in process. */
10565 gdb_assert (!m_die->in_process);
10566 m_die->in_process = true;
10567 }
8c3cb9fa 10568
4c8aa72d
PA
10569 ~process_die_scope ()
10570 {
10571 m_die->in_process = false;
10572
10573 /* If we're done processing the DIE for the CU that owns the line
10574 header, we don't need the line header anymore. */
10575 if (m_cu->line_header_die_owner == m_die)
10576 {
10577 delete m_cu->line_header;
10578 m_cu->line_header = NULL;
10579 m_cu->line_header_die_owner = NULL;
10580 }
10581 }
10582
10583private:
10584 die_info *m_die;
10585 dwarf2_cu *m_cu;
10586};
adde2bff 10587
c906108c
SS
10588/* Process a die and its children. */
10589
10590static void
e7c27a73 10591process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10592{
4c8aa72d 10593 process_die_scope scope (die, cu);
adde2bff 10594
c906108c
SS
10595 switch (die->tag)
10596 {
10597 case DW_TAG_padding:
10598 break;
10599 case DW_TAG_compile_unit:
95554aad 10600 case DW_TAG_partial_unit:
e7c27a73 10601 read_file_scope (die, cu);
c906108c 10602 break;
348e048f
DE
10603 case DW_TAG_type_unit:
10604 read_type_unit_scope (die, cu);
10605 break;
c906108c 10606 case DW_TAG_subprogram:
0a4b0913
AB
10607 /* Nested subprograms in Fortran get a prefix. */
10608 if (cu->language == language_fortran
10609 && die->parent != NULL
10610 && die->parent->tag == DW_TAG_subprogram)
10611 cu->processing_has_namespace_info = true;
10612 /* Fall through. */
c906108c 10613 case DW_TAG_inlined_subroutine:
edb3359d 10614 read_func_scope (die, cu);
c906108c
SS
10615 break;
10616 case DW_TAG_lexical_block:
14898363
L
10617 case DW_TAG_try_block:
10618 case DW_TAG_catch_block:
e7c27a73 10619 read_lexical_block_scope (die, cu);
c906108c 10620 break;
216f72a1 10621 case DW_TAG_call_site:
96408a79
SA
10622 case DW_TAG_GNU_call_site:
10623 read_call_site_scope (die, cu);
10624 break;
c906108c 10625 case DW_TAG_class_type:
680b30c7 10626 case DW_TAG_interface_type:
c906108c
SS
10627 case DW_TAG_structure_type:
10628 case DW_TAG_union_type:
134d01f1 10629 process_structure_scope (die, cu);
c906108c
SS
10630 break;
10631 case DW_TAG_enumeration_type:
134d01f1 10632 process_enumeration_scope (die, cu);
c906108c 10633 break;
134d01f1 10634
f792889a
DJ
10635 /* These dies have a type, but processing them does not create
10636 a symbol or recurse to process the children. Therefore we can
10637 read them on-demand through read_type_die. */
c906108c 10638 case DW_TAG_subroutine_type:
72019c9c 10639 case DW_TAG_set_type:
c906108c 10640 case DW_TAG_array_type:
c906108c 10641 case DW_TAG_pointer_type:
c906108c 10642 case DW_TAG_ptr_to_member_type:
c906108c 10643 case DW_TAG_reference_type:
4297a3f0 10644 case DW_TAG_rvalue_reference_type:
c906108c 10645 case DW_TAG_string_type:
c906108c 10646 break;
134d01f1 10647
c906108c 10648 case DW_TAG_base_type:
a02abb62 10649 case DW_TAG_subrange_type:
cb249c71 10650 case DW_TAG_typedef:
134d01f1
DJ
10651 /* Add a typedef symbol for the type definition, if it has a
10652 DW_AT_name. */
f792889a 10653 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10654 break;
c906108c 10655 case DW_TAG_common_block:
e7c27a73 10656 read_common_block (die, cu);
c906108c
SS
10657 break;
10658 case DW_TAG_common_inclusion:
10659 break;
d9fa45fe 10660 case DW_TAG_namespace:
9068261f 10661 cu->processing_has_namespace_info = true;
e7c27a73 10662 read_namespace (die, cu);
d9fa45fe 10663 break;
5d7cb8df 10664 case DW_TAG_module:
9068261f 10665 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10666 read_module (die, cu);
10667 break;
d9fa45fe 10668 case DW_TAG_imported_declaration:
9068261f 10669 cu->processing_has_namespace_info = true;
74921315
KS
10670 if (read_namespace_alias (die, cu))
10671 break;
86a73007
TT
10672 /* The declaration is not a global namespace alias. */
10673 /* Fall through. */
d9fa45fe 10674 case DW_TAG_imported_module:
9068261f 10675 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10676 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10677 || cu->language != language_fortran))
b98664d3 10678 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10679 dwarf_tag_name (die->tag));
10680 read_import_statement (die, cu);
d9fa45fe 10681 break;
95554aad
TT
10682
10683 case DW_TAG_imported_unit:
10684 process_imported_unit_die (die, cu);
10685 break;
10686
71a3c369
TT
10687 case DW_TAG_variable:
10688 read_variable (die, cu);
10689 break;
10690
c906108c 10691 default:
e7c27a73 10692 new_symbol (die, NULL, cu);
c906108c
SS
10693 break;
10694 }
10695}
ca69b9e6
DE
10696\f
10697/* DWARF name computation. */
c906108c 10698
94af9270
KS
10699/* A helper function for dwarf2_compute_name which determines whether DIE
10700 needs to have the name of the scope prepended to the name listed in the
10701 die. */
10702
10703static int
10704die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10705{
1c809c68
TT
10706 struct attribute *attr;
10707
94af9270
KS
10708 switch (die->tag)
10709 {
10710 case DW_TAG_namespace:
10711 case DW_TAG_typedef:
10712 case DW_TAG_class_type:
10713 case DW_TAG_interface_type:
10714 case DW_TAG_structure_type:
10715 case DW_TAG_union_type:
10716 case DW_TAG_enumeration_type:
10717 case DW_TAG_enumerator:
10718 case DW_TAG_subprogram:
08a76f8a 10719 case DW_TAG_inlined_subroutine:
94af9270 10720 case DW_TAG_member:
74921315 10721 case DW_TAG_imported_declaration:
94af9270
KS
10722 return 1;
10723
10724 case DW_TAG_variable:
c2b0a229 10725 case DW_TAG_constant:
94af9270
KS
10726 /* We only need to prefix "globally" visible variables. These include
10727 any variable marked with DW_AT_external or any variable that
10728 lives in a namespace. [Variables in anonymous namespaces
10729 require prefixing, but they are not DW_AT_external.] */
10730
10731 if (dwarf2_attr (die, DW_AT_specification, cu))
10732 {
10733 struct dwarf2_cu *spec_cu = cu;
9a619af0 10734
94af9270
KS
10735 return die_needs_namespace (die_specification (die, &spec_cu),
10736 spec_cu);
10737 }
10738
1c809c68 10739 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10740 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10741 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10742 return 0;
10743 /* A variable in a lexical block of some kind does not need a
10744 namespace, even though in C++ such variables may be external
10745 and have a mangled name. */
10746 if (die->parent->tag == DW_TAG_lexical_block
10747 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10748 || die->parent->tag == DW_TAG_catch_block
10749 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10750 return 0;
10751 return 1;
94af9270
KS
10752
10753 default:
10754 return 0;
10755 }
10756}
10757
73b9be8b
KS
10758/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10759 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10760 defined for the given DIE. */
10761
10762static struct attribute *
10763dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10764{
10765 struct attribute *attr;
10766
10767 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10768 if (attr == NULL)
10769 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10770
10771 return attr;
10772}
10773
10774/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10775 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10776 defined for the given DIE. */
10777
10778static const char *
10779dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10780{
10781 const char *linkage_name;
10782
10783 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10784 if (linkage_name == NULL)
10785 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10786
10787 return linkage_name;
10788}
10789
94af9270 10790/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10791 compute the physname for the object, which include a method's:
9c37b5ae 10792 - formal parameters (C++),
a766d390 10793 - receiver type (Go),
a766d390
DE
10794
10795 The term "physname" is a bit confusing.
10796 For C++, for example, it is the demangled name.
10797 For Go, for example, it's the mangled name.
94af9270 10798
af6b7be1
JB
10799 For Ada, return the DIE's linkage name rather than the fully qualified
10800 name. PHYSNAME is ignored..
10801
94af9270
KS
10802 The result is allocated on the objfile_obstack and canonicalized. */
10803
10804static const char *
15d034d0
TT
10805dwarf2_compute_name (const char *name,
10806 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10807 int physname)
10808{
518817b3 10809 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10810
94af9270
KS
10811 if (name == NULL)
10812 name = dwarf2_name (die, cu);
10813
2ee7123e
DE
10814 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10815 but otherwise compute it by typename_concat inside GDB.
10816 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10817 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10818 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10819 will set the demangled name to the result of dwarf2_full_name, and it is
10820 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10821 if (cu->language == language_ada
10822 || (cu->language == language_fortran && physname))
10823 {
10824 /* For Ada unit, we prefer the linkage name over the name, as
10825 the former contains the exported name, which the user expects
10826 to be able to reference. Ideally, we want the user to be able
10827 to reference this entity using either natural or linkage name,
10828 but we haven't started looking at this enhancement yet. */
73b9be8b 10829 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10830
2ee7123e
DE
10831 if (linkage_name != NULL)
10832 return linkage_name;
f55ee35c
JK
10833 }
10834
94af9270
KS
10835 /* These are the only languages we know how to qualify names in. */
10836 if (name != NULL
9c37b5ae 10837 && (cu->language == language_cplus
c44af4eb
TT
10838 || cu->language == language_fortran || cu->language == language_d
10839 || cu->language == language_rust))
94af9270
KS
10840 {
10841 if (die_needs_namespace (die, cu))
10842 {
0d5cff50 10843 const char *prefix;
34a68019 10844 const char *canonical_name = NULL;
94af9270 10845
d7e74731
PA
10846 string_file buf;
10847
94af9270 10848 prefix = determine_prefix (die, cu);
94af9270
KS
10849 if (*prefix != '\0')
10850 {
43816ebc
TT
10851 gdb::unique_xmalloc_ptr<char> prefixed_name
10852 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10853
43816ebc 10854 buf.puts (prefixed_name.get ());
94af9270
KS
10855 }
10856 else
d7e74731 10857 buf.puts (name);
94af9270 10858
98bfdba5
PA
10859 /* Template parameters may be specified in the DIE's DW_AT_name, or
10860 as children with DW_TAG_template_type_param or
10861 DW_TAG_value_type_param. If the latter, add them to the name
10862 here. If the name already has template parameters, then
10863 skip this step; some versions of GCC emit both, and
10864 it is more efficient to use the pre-computed name.
10865
10866 Something to keep in mind about this process: it is very
10867 unlikely, or in some cases downright impossible, to produce
10868 something that will match the mangled name of a function.
10869 If the definition of the function has the same debug info,
10870 we should be able to match up with it anyway. But fallbacks
10871 using the minimal symbol, for instance to find a method
10872 implemented in a stripped copy of libstdc++, will not work.
10873 If we do not have debug info for the definition, we will have to
10874 match them up some other way.
10875
10876 When we do name matching there is a related problem with function
10877 templates; two instantiated function templates are allowed to
10878 differ only by their return types, which we do not add here. */
10879
10880 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10881 {
10882 struct attribute *attr;
10883 struct die_info *child;
10884 int first = 1;
10885
10886 die->building_fullname = 1;
10887
10888 for (child = die->child; child != NULL; child = child->sibling)
10889 {
10890 struct type *type;
12df843f 10891 LONGEST value;
d521ce57 10892 const gdb_byte *bytes;
98bfdba5
PA
10893 struct dwarf2_locexpr_baton *baton;
10894 struct value *v;
10895
10896 if (child->tag != DW_TAG_template_type_param
10897 && child->tag != DW_TAG_template_value_param)
10898 continue;
10899
10900 if (first)
10901 {
d7e74731 10902 buf.puts ("<");
98bfdba5
PA
10903 first = 0;
10904 }
10905 else
d7e74731 10906 buf.puts (", ");
98bfdba5
PA
10907
10908 attr = dwarf2_attr (child, DW_AT_type, cu);
10909 if (attr == NULL)
10910 {
b98664d3 10911 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10912 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10913 continue;
10914 }
10915 type = die_type (child, cu);
10916
10917 if (child->tag == DW_TAG_template_type_param)
10918 {
c1ec8cea
TT
10919 c_print_type (type, "", &buf, -1, 0, cu->language,
10920 &type_print_raw_options);
98bfdba5
PA
10921 continue;
10922 }
10923
10924 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10925 if (attr == NULL)
10926 {
b98664d3 10927 complaint (_("template parameter missing "
3e43a32a 10928 "DW_AT_const_value"));
d7e74731 10929 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10930 continue;
10931 }
10932
10933 dwarf2_const_value_attr (attr, type, name,
10934 &cu->comp_unit_obstack, cu,
10935 &value, &bytes, &baton);
10936
10937 if (TYPE_NOSIGN (type))
10938 /* GDB prints characters as NUMBER 'CHAR'. If that's
10939 changed, this can use value_print instead. */
d7e74731 10940 c_printchar (value, type, &buf);
98bfdba5
PA
10941 else
10942 {
10943 struct value_print_options opts;
10944
10945 if (baton != NULL)
10946 v = dwarf2_evaluate_loc_desc (type, NULL,
10947 baton->data,
10948 baton->size,
10949 baton->per_cu);
10950 else if (bytes != NULL)
10951 {
10952 v = allocate_value (type);
10953 memcpy (value_contents_writeable (v), bytes,
10954 TYPE_LENGTH (type));
10955 }
10956 else
10957 v = value_from_longest (type, value);
10958
3e43a32a
MS
10959 /* Specify decimal so that we do not depend on
10960 the radix. */
98bfdba5
PA
10961 get_formatted_print_options (&opts, 'd');
10962 opts.raw = 1;
d7e74731 10963 value_print (v, &buf, &opts);
98bfdba5 10964 release_value (v);
98bfdba5
PA
10965 }
10966 }
10967
10968 die->building_fullname = 0;
10969
10970 if (!first)
10971 {
10972 /* Close the argument list, with a space if necessary
10973 (nested templates). */
d7e74731
PA
10974 if (!buf.empty () && buf.string ().back () == '>')
10975 buf.puts (" >");
98bfdba5 10976 else
d7e74731 10977 buf.puts (">");
98bfdba5
PA
10978 }
10979 }
10980
9c37b5ae 10981 /* For C++ methods, append formal parameter type
94af9270 10982 information, if PHYSNAME. */
6e70227d 10983
94af9270 10984 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10985 && cu->language == language_cplus)
94af9270
KS
10986 {
10987 struct type *type = read_type_die (die, cu);
10988
d7e74731 10989 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10990 &type_print_raw_options);
94af9270 10991
9c37b5ae 10992 if (cu->language == language_cplus)
94af9270 10993 {
60430eff
DJ
10994 /* Assume that an artificial first parameter is
10995 "this", but do not crash if it is not. RealView
10996 marks unnamed (and thus unused) parameters as
10997 artificial; there is no way to differentiate
10998 the two cases. */
94af9270
KS
10999 if (TYPE_NFIELDS (type) > 0
11000 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11001 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11002 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11003 0))))
d7e74731 11004 buf.puts (" const");
94af9270
KS
11005 }
11006 }
11007
d7e74731 11008 const std::string &intermediate_name = buf.string ();
94af9270
KS
11009
11010 if (cu->language == language_cplus)
34a68019 11011 canonical_name
322a8516 11012 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11013 &objfile->per_bfd->storage_obstack);
11014
11015 /* If we only computed INTERMEDIATE_NAME, or if
11016 INTERMEDIATE_NAME is already canonical, then we need to
11017 copy it to the appropriate obstack. */
322a8516 11018 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11019 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11020 intermediate_name);
34a68019
TT
11021 else
11022 name = canonical_name;
94af9270
KS
11023 }
11024 }
11025
11026 return name;
11027}
11028
0114d602
DJ
11029/* Return the fully qualified name of DIE, based on its DW_AT_name.
11030 If scope qualifiers are appropriate they will be added. The result
34a68019 11031 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11032 not have a name. NAME may either be from a previous call to
11033 dwarf2_name or NULL.
11034
9c37b5ae 11035 The output string will be canonicalized (if C++). */
0114d602
DJ
11036
11037static const char *
15d034d0 11038dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11039{
94af9270
KS
11040 return dwarf2_compute_name (name, die, cu, 0);
11041}
0114d602 11042
94af9270
KS
11043/* Construct a physname for the given DIE in CU. NAME may either be
11044 from a previous call to dwarf2_name or NULL. The result will be
11045 allocated on the objfile_objstack or NULL if the DIE does not have a
11046 name.
0114d602 11047
9c37b5ae 11048 The output string will be canonicalized (if C++). */
0114d602 11049
94af9270 11050static const char *
15d034d0 11051dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11052{
518817b3 11053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11054 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11055 int need_copy = 1;
11056
11057 /* In this case dwarf2_compute_name is just a shortcut not building anything
11058 on its own. */
11059 if (!die_needs_namespace (die, cu))
11060 return dwarf2_compute_name (name, die, cu, 1);
11061
73b9be8b 11062 mangled = dw2_linkage_name (die, cu);
900e11f9 11063
e98c9e7c
TT
11064 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11065 See https://github.com/rust-lang/rust/issues/32925. */
11066 if (cu->language == language_rust && mangled != NULL
11067 && strchr (mangled, '{') != NULL)
11068 mangled = NULL;
11069
900e11f9
JK
11070 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11071 has computed. */
791afaa2 11072 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11073 if (mangled != NULL)
900e11f9 11074 {
900e11f9 11075
59cc4834
JB
11076 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11077 {
11078 /* Do nothing (do not demangle the symbol name). */
11079 }
11080 else if (cu->language == language_go)
a766d390 11081 {
5e2db402
TT
11082 /* This is a lie, but we already lie to the caller new_symbol.
11083 new_symbol assumes we return the mangled name.
a766d390 11084 This just undoes that lie until things are cleaned up. */
a766d390
DE
11085 }
11086 else
11087 {
0eb876f5
JB
11088 /* Use DMGL_RET_DROP for C++ template functions to suppress
11089 their return type. It is easier for GDB users to search
11090 for such functions as `name(params)' than `long name(params)'.
11091 In such case the minimal symbol names do not match the full
11092 symbol names but for template functions there is never a need
11093 to look up their definition from their declaration so
11094 the only disadvantage remains the minimal symbol variant
11095 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11096 demangled.reset (gdb_demangle (mangled,
11097 (DMGL_PARAMS | DMGL_ANSI
11098 | DMGL_RET_DROP)));
a766d390 11099 }
900e11f9 11100 if (demangled)
791afaa2 11101 canon = demangled.get ();
900e11f9
JK
11102 else
11103 {
11104 canon = mangled;
11105 need_copy = 0;
11106 }
11107 }
11108
11109 if (canon == NULL || check_physname)
11110 {
11111 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11112
11113 if (canon != NULL && strcmp (physname, canon) != 0)
11114 {
11115 /* It may not mean a bug in GDB. The compiler could also
11116 compute DW_AT_linkage_name incorrectly. But in such case
11117 GDB would need to be bug-to-bug compatible. */
11118
b98664d3 11119 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11120 "(from linkage <%s>) - DIE at %s [in module %s]"),
11121 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11122 objfile_name (objfile));
900e11f9
JK
11123
11124 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11125 is available here - over computed PHYSNAME. It is safer
11126 against both buggy GDB and buggy compilers. */
11127
11128 retval = canon;
11129 }
11130 else
11131 {
11132 retval = physname;
11133 need_copy = 0;
11134 }
11135 }
11136 else
11137 retval = canon;
11138
11139 if (need_copy)
021887d8 11140 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11141
900e11f9 11142 return retval;
0114d602
DJ
11143}
11144
74921315
KS
11145/* Inspect DIE in CU for a namespace alias. If one exists, record
11146 a new symbol for it.
11147
11148 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11149
11150static int
11151read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11152{
11153 struct attribute *attr;
11154
11155 /* If the die does not have a name, this is not a namespace
11156 alias. */
11157 attr = dwarf2_attr (die, DW_AT_name, cu);
11158 if (attr != NULL)
11159 {
11160 int num;
11161 struct die_info *d = die;
11162 struct dwarf2_cu *imported_cu = cu;
11163
11164 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11165 keep inspecting DIEs until we hit the underlying import. */
11166#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11167 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11168 {
11169 attr = dwarf2_attr (d, DW_AT_import, cu);
11170 if (attr == NULL)
11171 break;
11172
11173 d = follow_die_ref (d, attr, &imported_cu);
11174 if (d->tag != DW_TAG_imported_declaration)
11175 break;
11176 }
11177
11178 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11179 {
b98664d3 11180 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11181 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11182 return 0;
11183 }
11184
11185 if (attr != NULL)
11186 {
11187 struct type *type;
9c541725 11188 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11189
9c541725 11190 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11191 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11192 {
11193 /* This declaration is a global namespace alias. Add
11194 a symbol for it whose type is the aliased namespace. */
11195 new_symbol (die, type, cu);
11196 return 1;
11197 }
11198 }
11199 }
11200
11201 return 0;
11202}
11203
22cee43f 11204/* Return the using directives repository (global or local?) to use in the
804d2729 11205 current context for CU.
22cee43f
PMR
11206
11207 For Ada, imported declarations can materialize renamings, which *may* be
11208 global. However it is impossible (for now?) in DWARF to distinguish
11209 "external" imported declarations and "static" ones. As all imported
11210 declarations seem to be static in all other languages, make them all CU-wide
11211 global only in Ada. */
11212
11213static struct using_direct **
804d2729 11214using_directives (struct dwarf2_cu *cu)
22cee43f 11215{
c24bdb02
KS
11216 if (cu->language == language_ada
11217 && cu->get_builder ()->outermost_context_p ())
11218 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11219 else
c24bdb02 11220 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11221}
11222
27aa8d6a
SW
11223/* Read the import statement specified by the given die and record it. */
11224
11225static void
11226read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11227{
518817b3 11228 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11229 struct attribute *import_attr;
32019081 11230 struct die_info *imported_die, *child_die;
de4affc9 11231 struct dwarf2_cu *imported_cu;
27aa8d6a 11232 const char *imported_name;
794684b6 11233 const char *imported_name_prefix;
13387711
SW
11234 const char *canonical_name;
11235 const char *import_alias;
11236 const char *imported_declaration = NULL;
794684b6 11237 const char *import_prefix;
eb1e02fd 11238 std::vector<const char *> excludes;
13387711 11239
27aa8d6a
SW
11240 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11241 if (import_attr == NULL)
11242 {
b98664d3 11243 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11244 dwarf_tag_name (die->tag));
11245 return;
11246 }
11247
de4affc9
CC
11248 imported_cu = cu;
11249 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11250 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11251 if (imported_name == NULL)
11252 {
11253 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11254
11255 The import in the following code:
11256 namespace A
11257 {
11258 typedef int B;
11259 }
11260
11261 int main ()
11262 {
11263 using A::B;
11264 B b;
11265 return b;
11266 }
11267
11268 ...
11269 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11270 <52> DW_AT_decl_file : 1
11271 <53> DW_AT_decl_line : 6
11272 <54> DW_AT_import : <0x75>
11273 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11274 <59> DW_AT_name : B
11275 <5b> DW_AT_decl_file : 1
11276 <5c> DW_AT_decl_line : 2
11277 <5d> DW_AT_type : <0x6e>
11278 ...
11279 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11280 <76> DW_AT_byte_size : 4
11281 <77> DW_AT_encoding : 5 (signed)
11282
11283 imports the wrong die ( 0x75 instead of 0x58 ).
11284 This case will be ignored until the gcc bug is fixed. */
11285 return;
11286 }
11287
82856980
SW
11288 /* Figure out the local name after import. */
11289 import_alias = dwarf2_name (die, cu);
27aa8d6a 11290
794684b6
SW
11291 /* Figure out where the statement is being imported to. */
11292 import_prefix = determine_prefix (die, cu);
11293
11294 /* Figure out what the scope of the imported die is and prepend it
11295 to the name of the imported die. */
de4affc9 11296 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11297
f55ee35c
JK
11298 if (imported_die->tag != DW_TAG_namespace
11299 && imported_die->tag != DW_TAG_module)
794684b6 11300 {
13387711
SW
11301 imported_declaration = imported_name;
11302 canonical_name = imported_name_prefix;
794684b6 11303 }
13387711 11304 else if (strlen (imported_name_prefix) > 0)
12aaed36 11305 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11306 imported_name_prefix,
11307 (cu->language == language_d ? "." : "::"),
11308 imported_name, (char *) NULL);
13387711
SW
11309 else
11310 canonical_name = imported_name;
794684b6 11311
32019081
JK
11312 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11313 for (child_die = die->child; child_die && child_die->tag;
11314 child_die = sibling_die (child_die))
11315 {
11316 /* DWARF-4: A Fortran use statement with a “rename list” may be
11317 represented by an imported module entry with an import attribute
11318 referring to the module and owned entries corresponding to those
11319 entities that are renamed as part of being imported. */
11320
11321 if (child_die->tag != DW_TAG_imported_declaration)
11322 {
b98664d3 11323 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11324 "- DIE at %s [in module %s]"),
11325 sect_offset_str (child_die->sect_off),
11326 objfile_name (objfile));
32019081
JK
11327 continue;
11328 }
11329
11330 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11331 if (import_attr == NULL)
11332 {
b98664d3 11333 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11334 dwarf_tag_name (child_die->tag));
11335 continue;
11336 }
11337
11338 imported_cu = cu;
11339 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11340 &imported_cu);
11341 imported_name = dwarf2_name (imported_die, imported_cu);
11342 if (imported_name == NULL)
11343 {
b98664d3 11344 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11345 "imported name - DIE at %s [in module %s]"),
11346 sect_offset_str (child_die->sect_off),
11347 objfile_name (objfile));
32019081
JK
11348 continue;
11349 }
11350
eb1e02fd 11351 excludes.push_back (imported_name);
32019081
JK
11352
11353 process_die (child_die, cu);
11354 }
11355
804d2729 11356 add_using_directive (using_directives (cu),
22cee43f
PMR
11357 import_prefix,
11358 canonical_name,
11359 import_alias,
11360 imported_declaration,
11361 excludes,
11362 0,
11363 &objfile->objfile_obstack);
27aa8d6a
SW
11364}
11365
5230b05a
WT
11366/* ICC<14 does not output the required DW_AT_declaration on incomplete
11367 types, but gives them a size of zero. Starting with version 14,
11368 ICC is compatible with GCC. */
11369
9068261f 11370static bool
5230b05a
WT
11371producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11372{
11373 if (!cu->checked_producer)
11374 check_producer (cu);
11375
11376 return cu->producer_is_icc_lt_14;
11377}
11378
eb77c9df
AB
11379/* ICC generates a DW_AT_type for C void functions. This was observed on
11380 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11381 which says that void functions should not have a DW_AT_type. */
11382
11383static bool
11384producer_is_icc (struct dwarf2_cu *cu)
11385{
11386 if (!cu->checked_producer)
11387 check_producer (cu);
11388
11389 return cu->producer_is_icc;
11390}
11391
1b80a9fa
JK
11392/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11393 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11394 this, it was first present in GCC release 4.3.0. */
11395
9068261f 11396static bool
1b80a9fa
JK
11397producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11398{
11399 if (!cu->checked_producer)
11400 check_producer (cu);
11401
11402 return cu->producer_is_gcc_lt_4_3;
11403}
11404
d721ba37
PA
11405static file_and_directory
11406find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11407{
d721ba37
PA
11408 file_and_directory res;
11409
9291a0cd
TT
11410 /* Find the filename. Do not use dwarf2_name here, since the filename
11411 is not a source language identifier. */
d721ba37
PA
11412 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11413 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11414
d721ba37
PA
11415 if (res.comp_dir == NULL
11416 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11417 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11418 {
d721ba37
PA
11419 res.comp_dir_storage = ldirname (res.name);
11420 if (!res.comp_dir_storage.empty ())
11421 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11422 }
d721ba37 11423 if (res.comp_dir != NULL)
9291a0cd
TT
11424 {
11425 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11426 directory, get rid of it. */
d721ba37 11427 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11428
d721ba37
PA
11429 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11430 res.comp_dir = cp + 1;
9291a0cd
TT
11431 }
11432
d721ba37
PA
11433 if (res.name == NULL)
11434 res.name = "<unknown>";
11435
11436 return res;
9291a0cd
TT
11437}
11438
f4dc4d17
DE
11439/* Handle DW_AT_stmt_list for a compilation unit.
11440 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11441 COMP_DIR is the compilation directory. LOWPC is passed to
11442 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11443
11444static void
11445handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11446 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11447{
518817b3
SM
11448 struct dwarf2_per_objfile *dwarf2_per_objfile
11449 = cu->per_cu->dwarf2_per_objfile;
527f3840 11450 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11451 struct attribute *attr;
527f3840
JK
11452 struct line_header line_header_local;
11453 hashval_t line_header_local_hash;
527f3840
JK
11454 void **slot;
11455 int decode_mapping;
2ab95328 11456
f4dc4d17
DE
11457 gdb_assert (! cu->per_cu->is_debug_types);
11458
2ab95328 11459 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11460 if (attr == NULL)
11461 return;
11462
9c541725 11463 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11464
11465 /* The line header hash table is only created if needed (it exists to
11466 prevent redundant reading of the line table for partial_units).
11467 If we're given a partial_unit, we'll need it. If we're given a
11468 compile_unit, then use the line header hash table if it's already
11469 created, but don't create one just yet. */
11470
11471 if (dwarf2_per_objfile->line_header_hash == NULL
11472 && die->tag == DW_TAG_partial_unit)
2ab95328 11473 {
527f3840
JK
11474 dwarf2_per_objfile->line_header_hash
11475 = htab_create_alloc_ex (127, line_header_hash_voidp,
11476 line_header_eq_voidp,
11477 free_line_header_voidp,
11478 &objfile->objfile_obstack,
11479 hashtab_obstack_allocate,
11480 dummy_obstack_deallocate);
11481 }
2ab95328 11482
9c541725 11483 line_header_local.sect_off = line_offset;
527f3840
JK
11484 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11485 line_header_local_hash = line_header_hash (&line_header_local);
11486 if (dwarf2_per_objfile->line_header_hash != NULL)
11487 {
11488 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11489 &line_header_local,
11490 line_header_local_hash, NO_INSERT);
11491
11492 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11493 is not present in *SLOT (since if there is something in *SLOT then
11494 it will be for a partial_unit). */
11495 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11496 {
527f3840 11497 gdb_assert (*slot != NULL);
9a3c8263 11498 cu->line_header = (struct line_header *) *slot;
527f3840 11499 return;
dee91e82 11500 }
2ab95328 11501 }
527f3840
JK
11502
11503 /* dwarf_decode_line_header does not yet provide sufficient information.
11504 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11505 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11506 if (lh == NULL)
527f3840 11507 return;
4c8aa72d
PA
11508
11509 cu->line_header = lh.release ();
11510 cu->line_header_die_owner = die;
527f3840
JK
11511
11512 if (dwarf2_per_objfile->line_header_hash == NULL)
11513 slot = NULL;
11514 else
11515 {
11516 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11517 &line_header_local,
11518 line_header_local_hash, INSERT);
11519 gdb_assert (slot != NULL);
11520 }
11521 if (slot != NULL && *slot == NULL)
11522 {
11523 /* This newly decoded line number information unit will be owned
11524 by line_header_hash hash table. */
11525 *slot = cu->line_header;
4c8aa72d 11526 cu->line_header_die_owner = NULL;
527f3840
JK
11527 }
11528 else
11529 {
11530 /* We cannot free any current entry in (*slot) as that struct line_header
11531 may be already used by multiple CUs. Create only temporary decoded
11532 line_header for this CU - it may happen at most once for each line
11533 number information unit. And if we're not using line_header_hash
11534 then this is what we want as well. */
11535 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11536 }
11537 decode_mapping = (die->tag != DW_TAG_partial_unit);
11538 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11539 decode_mapping);
fff8551c 11540
2ab95328
TT
11541}
11542
95554aad 11543/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11544
c906108c 11545static void
e7c27a73 11546read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11547{
518817b3
SM
11548 struct dwarf2_per_objfile *dwarf2_per_objfile
11549 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11550 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11551 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11552 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11553 CORE_ADDR highpc = ((CORE_ADDR) 0);
11554 struct attribute *attr;
c906108c 11555 struct die_info *child_die;
e142c38c 11556 CORE_ADDR baseaddr;
6e70227d 11557
380618d6 11558 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11559 baseaddr = objfile->text_section_offset ();
c906108c 11560
fae299cd 11561 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11562
11563 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11564 from finish_block. */
2acceee2 11565 if (lowpc == ((CORE_ADDR) -1))
c906108c 11566 lowpc = highpc;
3e29f34a 11567 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11568
d721ba37 11569 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11570
f4b8a18d
KW
11571 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11572 standardised yet. As a workaround for the language detection we fall
11573 back to the DW_AT_producer string. */
11574 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11575 cu->language = language_opencl;
11576
3019eac3
DE
11577 /* Similar hack for Go. */
11578 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11579 set_cu_language (DW_LANG_Go, cu);
11580
c24bdb02 11581 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11582
11583 /* Decode line number information if present. We do this before
11584 processing child DIEs, so that the line header table is available
11585 for DW_AT_decl_file. */
d721ba37 11586 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11587
11588 /* Process all dies in compilation unit. */
11589 if (die->child != NULL)
11590 {
11591 child_die = die->child;
11592 while (child_die && child_die->tag)
11593 {
11594 process_die (child_die, cu);
11595 child_die = sibling_die (child_die);
11596 }
11597 }
11598
11599 /* Decode macro information, if present. Dwarf 2 macro information
11600 refers to information in the line number info statement program
11601 header, so we can only read it if we've read the header
11602 successfully. */
0af92d60
JK
11603 attr = dwarf2_attr (die, DW_AT_macros, cu);
11604 if (attr == NULL)
11605 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11606 if (attr && cu->line_header)
11607 {
11608 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11609 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11610
43f3e411 11611 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11612 }
11613 else
11614 {
11615 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11616 if (attr && cu->line_header)
11617 {
11618 unsigned int macro_offset = DW_UNSND (attr);
11619
43f3e411 11620 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11621 }
11622 }
3019eac3
DE
11623}
11624
c24bdb02
KS
11625void
11626dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11627{
f4dc4d17
DE
11628 struct type_unit_group *tu_group;
11629 int first_time;
3019eac3 11630 struct attribute *attr;
9c541725 11631 unsigned int i;
0186c6a7 11632 struct signatured_type *sig_type;
3019eac3 11633
f4dc4d17 11634 gdb_assert (per_cu->is_debug_types);
0186c6a7 11635 sig_type = (struct signatured_type *) per_cu;
3019eac3 11636
c24bdb02 11637 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11638
f4dc4d17 11639 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11640 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11641 if (sig_type->type_unit_group == NULL)
c24bdb02 11642 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11643 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11644
11645 /* If we've already processed this stmt_list there's no real need to
11646 do it again, we could fake it and just recreate the part we need
11647 (file name,index -> symtab mapping). If data shows this optimization
11648 is useful we can do it then. */
43f3e411 11649 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11650
11651 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11652 debug info. */
fff8551c 11653 line_header_up lh;
f4dc4d17 11654 if (attr != NULL)
3019eac3 11655 {
9c541725 11656 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11657 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11658 }
11659 if (lh == NULL)
11660 {
11661 if (first_time)
c24bdb02 11662 start_symtab ("", NULL, 0);
f4dc4d17
DE
11663 else
11664 {
11665 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11666 gdb_assert (m_builder == nullptr);
804d2729 11667 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11668 m_builder.reset (new struct buildsym_compunit
11669 (COMPUNIT_OBJFILE (cust), "",
11670 COMPUNIT_DIRNAME (cust),
11671 compunit_language (cust),
11672 0, cust));
f4dc4d17 11673 }
f4dc4d17 11674 return;
3019eac3
DE
11675 }
11676
c24bdb02
KS
11677 line_header = lh.release ();
11678 line_header_die_owner = die;
3019eac3 11679
f4dc4d17
DE
11680 if (first_time)
11681 {
c24bdb02 11682 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11683
1fd60fc0
DE
11684 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11685 still initializing it, and our caller (a few levels up)
11686 process_full_type_unit still needs to know if this is the first
11687 time. */
11688
7ba99d21 11689 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11690 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11691 line_header->file_names_size ());
3019eac3 11692
7ba99d21
AT
11693 auto &file_names = line_header->file_names ();
11694 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11695 {
7ba99d21 11696 file_entry &fe = file_names[i];
c24bdb02
KS
11697 dwarf2_start_subfile (this, fe.name,
11698 fe.include_dir (line_header));
11699 buildsym_compunit *b = get_builder ();
11700 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11701 {
4c8aa72d
PA
11702 /* NOTE: start_subfile will recognize when it's been
11703 passed a file it has already seen. So we can't
11704 assume there's a simple mapping from
11705 cu->line_header->file_names to subfiles, plus
11706 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11707 b->get_current_subfile ()->symtab
11708 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11709 }
11710
c24bdb02 11711 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11712 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11713 }
11714 }
11715 else
3019eac3 11716 {
c24bdb02 11717 gdb_assert (m_builder == nullptr);
804d2729 11718 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11719 m_builder.reset (new struct buildsym_compunit
11720 (COMPUNIT_OBJFILE (cust), "",
11721 COMPUNIT_DIRNAME (cust),
11722 compunit_language (cust),
11723 0, cust));
f4dc4d17 11724
7ba99d21
AT
11725 auto &file_names = line_header->file_names ();
11726 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11727 {
7ba99d21 11728 file_entry &fe = file_names[i];
4c8aa72d 11729 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11730 }
3019eac3
DE
11731 }
11732
f4dc4d17
DE
11733 /* The main symtab is allocated last. Type units don't have DW_AT_name
11734 so they don't have a "real" (so to speak) symtab anyway.
11735 There is later code that will assign the main symtab to all symbols
11736 that don't have one. We need to handle the case of a symbol with a
11737 missing symtab (DW_AT_decl_file) anyway. */
11738}
3019eac3 11739
f4dc4d17
DE
11740/* Process DW_TAG_type_unit.
11741 For TUs we want to skip the first top level sibling if it's not the
11742 actual type being defined by this TU. In this case the first top
11743 level sibling is there to provide context only. */
3019eac3 11744
f4dc4d17
DE
11745static void
11746read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11747{
11748 struct die_info *child_die;
3019eac3 11749
f4dc4d17
DE
11750 prepare_one_comp_unit (cu, die, language_minimal);
11751
11752 /* Initialize (or reinitialize) the machinery for building symtabs.
11753 We do this before processing child DIEs, so that the line header table
11754 is available for DW_AT_decl_file. */
c24bdb02 11755 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11756
11757 if (die->child != NULL)
11758 {
11759 child_die = die->child;
11760 while (child_die && child_die->tag)
11761 {
11762 process_die (child_die, cu);
11763 child_die = sibling_die (child_die);
11764 }
11765 }
3019eac3
DE
11766}
11767\f
80626a55
DE
11768/* DWO/DWP files.
11769
11770 http://gcc.gnu.org/wiki/DebugFission
11771 http://gcc.gnu.org/wiki/DebugFissionDWP
11772
11773 To simplify handling of both DWO files ("object" files with the DWARF info)
11774 and DWP files (a file with the DWOs packaged up into one file), we treat
11775 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11776
11777static hashval_t
11778hash_dwo_file (const void *item)
11779{
9a3c8263 11780 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11781 hashval_t hash;
3019eac3 11782
a2ce51a0
DE
11783 hash = htab_hash_string (dwo_file->dwo_name);
11784 if (dwo_file->comp_dir != NULL)
11785 hash += htab_hash_string (dwo_file->comp_dir);
11786 return hash;
3019eac3
DE
11787}
11788
11789static int
11790eq_dwo_file (const void *item_lhs, const void *item_rhs)
11791{
9a3c8263
SM
11792 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11793 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11794
a2ce51a0
DE
11795 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11796 return 0;
11797 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11798 return lhs->comp_dir == rhs->comp_dir;
11799 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11800}
11801
11802/* Allocate a hash table for DWO files. */
11803
51ac9db5 11804static htab_up
ed2dc618 11805allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11806{
51ac9db5
SM
11807 auto delete_dwo_file = [] (void *item)
11808 {
11809 struct dwo_file *dwo_file = (struct dwo_file *) item;
11810
11811 delete dwo_file;
11812 };
11813
11814 return htab_up (htab_create_alloc_ex (41,
11815 hash_dwo_file,
11816 eq_dwo_file,
11817 delete_dwo_file,
11818 &objfile->objfile_obstack,
11819 hashtab_obstack_allocate,
11820 dummy_obstack_deallocate));
3019eac3
DE
11821}
11822
80626a55
DE
11823/* Lookup DWO file DWO_NAME. */
11824
11825static void **
ed2dc618
SM
11826lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11827 const char *dwo_name,
11828 const char *comp_dir)
80626a55
DE
11829{
11830 struct dwo_file find_entry;
11831 void **slot;
11832
11833 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11834 dwarf2_per_objfile->dwo_files
11835 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11836
0ac5b59e
DE
11837 find_entry.dwo_name = dwo_name;
11838 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11839 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11840 INSERT);
80626a55
DE
11841
11842 return slot;
11843}
11844
3019eac3
DE
11845static hashval_t
11846hash_dwo_unit (const void *item)
11847{
9a3c8263 11848 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11849
11850 /* This drops the top 32 bits of the id, but is ok for a hash. */
11851 return dwo_unit->signature;
11852}
11853
11854static int
11855eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11856{
9a3c8263
SM
11857 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11858 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11859
11860 /* The signature is assumed to be unique within the DWO file.
11861 So while object file CU dwo_id's always have the value zero,
11862 that's OK, assuming each object file DWO file has only one CU,
11863 and that's the rule for now. */
11864 return lhs->signature == rhs->signature;
11865}
11866
11867/* Allocate a hash table for DWO CUs,TUs.
11868 There is one of these tables for each of CUs,TUs for each DWO file. */
11869
11870static htab_t
11871allocate_dwo_unit_table (struct objfile *objfile)
11872{
11873 /* Start out with a pretty small number.
11874 Generally DWO files contain only one CU and maybe some TUs. */
11875 return htab_create_alloc_ex (3,
11876 hash_dwo_unit,
11877 eq_dwo_unit,
11878 NULL,
11879 &objfile->objfile_obstack,
11880 hashtab_obstack_allocate,
11881 dummy_obstack_deallocate);
11882}
11883
19c3d4c9 11884/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11885
11886static void
19c3d4c9
DE
11887create_dwo_cu_reader (const struct die_reader_specs *reader,
11888 const gdb_byte *info_ptr,
11889 struct die_info *comp_unit_die,
11890 int has_children,
c0ab21c2
TT
11891 struct dwo_file *dwo_file,
11892 struct dwo_unit *dwo_unit)
3019eac3
DE
11893{
11894 struct dwarf2_cu *cu = reader->cu;
9c541725 11895 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11896 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11897
a084a2a6
AT
11898 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11899 if (!signature.has_value ())
3019eac3 11900 {
b98664d3 11901 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11902 " its dwo_id [in module %s]"),
9d8780f0 11903 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11904 return;
11905 }
11906
3019eac3 11907 dwo_unit->dwo_file = dwo_file;
a084a2a6 11908 dwo_unit->signature = *signature;
8a0459fd 11909 dwo_unit->section = section;
9c541725 11910 dwo_unit->sect_off = sect_off;
3019eac3
DE
11911 dwo_unit->length = cu->per_cu->length;
11912
b4f54984 11913 if (dwarf_read_debug)
9d8780f0
SM
11914 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11915 sect_offset_str (sect_off),
9c541725 11916 hex_string (dwo_unit->signature));
3019eac3
DE
11917}
11918
33c5cd75 11919/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11920 Note: This function processes DWO files only, not DWP files. */
3019eac3 11921
33c5cd75 11922static void
ed2dc618 11923create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
11924 dwarf2_cu *cu, struct dwo_file &dwo_file,
11925 dwarf2_section_info &section, htab_t &cus_htab)
3019eac3
DE
11926{
11927 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11928 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11929
33c5cd75
DB
11930 dwarf2_read_section (objfile, &section);
11931 info_ptr = section.buffer;
3019eac3
DE
11932
11933 if (info_ptr == NULL)
33c5cd75 11934 return;
3019eac3 11935
b4f54984 11936 if (dwarf_read_debug)
19c3d4c9
DE
11937 {
11938 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11939 get_section_name (&section),
11940 get_section_file_name (&section));
19c3d4c9 11941 }
3019eac3 11942
33c5cd75 11943 end_ptr = info_ptr + section.size;
3019eac3
DE
11944 while (info_ptr < end_ptr)
11945 {
11946 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11947 struct dwo_unit read_unit {};
33c5cd75
DB
11948 struct dwo_unit *dwo_unit;
11949 void **slot;
11950 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11951
11952 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11953 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11954 per_cu.is_debug_types = 0;
33c5cd75
DB
11955 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11956 per_cu.section = &section;
11957
c0ab21c2
TT
11958 cutu_reader reader (&per_cu, cu, &dwo_file);
11959 if (!reader.dummy_p)
11960 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11961 reader.has_children, &dwo_file, &read_unit);
33c5cd75
DB
11962 info_ptr += per_cu.length;
11963
11964 // If the unit could not be parsed, skip it.
c0ab21c2 11965 if (read_unit.dwo_file == NULL)
33c5cd75 11966 continue;
3019eac3 11967
33c5cd75
DB
11968 if (cus_htab == NULL)
11969 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11970
33c5cd75 11971 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11972 *dwo_unit = read_unit;
33c5cd75
DB
11973 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11974 gdb_assert (slot != NULL);
11975 if (*slot != NULL)
19c3d4c9 11976 {
33c5cd75
DB
11977 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11978 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11979
b98664d3 11980 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11981 " the entry at offset %s, signature %s"),
11982 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11983 hex_string (dwo_unit->signature));
19c3d4c9 11984 }
33c5cd75 11985 *slot = (void *)dwo_unit;
3019eac3 11986 }
3019eac3
DE
11987}
11988
80626a55
DE
11989/* DWP file .debug_{cu,tu}_index section format:
11990 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11991
d2415c6c
DE
11992 DWP Version 1:
11993
80626a55
DE
11994 Both index sections have the same format, and serve to map a 64-bit
11995 signature to a set of section numbers. Each section begins with a header,
11996 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11997 indexes, and a pool of 32-bit section numbers. The index sections will be
11998 aligned at 8-byte boundaries in the file.
11999
d2415c6c
DE
12000 The index section header consists of:
12001
12002 V, 32 bit version number
12003 -, 32 bits unused
12004 N, 32 bit number of compilation units or type units in the index
12005 M, 32 bit number of slots in the hash table
80626a55 12006
d2415c6c 12007 Numbers are recorded using the byte order of the application binary.
80626a55 12008
d2415c6c
DE
12009 The hash table begins at offset 16 in the section, and consists of an array
12010 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12011 order of the application binary). Unused slots in the hash table are 0.
12012 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12013
d2415c6c
DE
12014 The parallel table begins immediately after the hash table
12015 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12016 array of 32-bit indexes (using the byte order of the application binary),
12017 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12018 table contains a 32-bit index into the pool of section numbers. For unused
12019 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12020
73869dc2
DE
12021 The pool of section numbers begins immediately following the hash table
12022 (at offset 16 + 12 * M from the beginning of the section). The pool of
12023 section numbers consists of an array of 32-bit words (using the byte order
12024 of the application binary). Each item in the array is indexed starting
12025 from 0. The hash table entry provides the index of the first section
12026 number in the set. Additional section numbers in the set follow, and the
12027 set is terminated by a 0 entry (section number 0 is not used in ELF).
12028
12029 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12030 section must be the first entry in the set, and the .debug_abbrev.dwo must
12031 be the second entry. Other members of the set may follow in any order.
12032
12033 ---
12034
12035 DWP Version 2:
12036
12037 DWP Version 2 combines all the .debug_info, etc. sections into one,
12038 and the entries in the index tables are now offsets into these sections.
12039 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12040 section.
12041
12042 Index Section Contents:
12043 Header
12044 Hash Table of Signatures dwp_hash_table.hash_table
12045 Parallel Table of Indices dwp_hash_table.unit_table
12046 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12047 Table of Section Sizes dwp_hash_table.v2.sizes
12048
12049 The index section header consists of:
12050
12051 V, 32 bit version number
12052 L, 32 bit number of columns in the table of section offsets
12053 N, 32 bit number of compilation units or type units in the index
12054 M, 32 bit number of slots in the hash table
12055
12056 Numbers are recorded using the byte order of the application binary.
12057
12058 The hash table has the same format as version 1.
12059 The parallel table of indices has the same format as version 1,
12060 except that the entries are origin-1 indices into the table of sections
12061 offsets and the table of section sizes.
12062
12063 The table of offsets begins immediately following the parallel table
12064 (at offset 16 + 12 * M from the beginning of the section). The table is
12065 a two-dimensional array of 32-bit words (using the byte order of the
12066 application binary), with L columns and N+1 rows, in row-major order.
12067 Each row in the array is indexed starting from 0. The first row provides
12068 a key to the remaining rows: each column in this row provides an identifier
12069 for a debug section, and the offsets in the same column of subsequent rows
12070 refer to that section. The section identifiers are:
12071
12072 DW_SECT_INFO 1 .debug_info.dwo
12073 DW_SECT_TYPES 2 .debug_types.dwo
12074 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12075 DW_SECT_LINE 4 .debug_line.dwo
12076 DW_SECT_LOC 5 .debug_loc.dwo
12077 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12078 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12079 DW_SECT_MACRO 8 .debug_macro.dwo
12080
12081 The offsets provided by the CU and TU index sections are the base offsets
12082 for the contributions made by each CU or TU to the corresponding section
12083 in the package file. Each CU and TU header contains an abbrev_offset
12084 field, used to find the abbreviations table for that CU or TU within the
12085 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12086 be interpreted as relative to the base offset given in the index section.
12087 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12088 should be interpreted as relative to the base offset for .debug_line.dwo,
12089 and offsets into other debug sections obtained from DWARF attributes should
12090 also be interpreted as relative to the corresponding base offset.
12091
12092 The table of sizes begins immediately following the table of offsets.
12093 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12094 with L columns and N rows, in row-major order. Each row in the array is
12095 indexed starting from 1 (row 0 is shared by the two tables).
12096
12097 ---
12098
12099 Hash table lookup is handled the same in version 1 and 2:
12100
12101 We assume that N and M will not exceed 2^32 - 1.
12102 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12103
d2415c6c
DE
12104 Given a 64-bit compilation unit signature or a type signature S, an entry
12105 in the hash table is located as follows:
80626a55 12106
d2415c6c
DE
12107 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12108 the low-order k bits all set to 1.
80626a55 12109
d2415c6c 12110 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12111
d2415c6c
DE
12112 3) If the hash table entry at index H matches the signature, use that
12113 entry. If the hash table entry at index H is unused (all zeroes),
12114 terminate the search: the signature is not present in the table.
80626a55 12115
d2415c6c 12116 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12117
d2415c6c 12118 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12119 to stop at an unused slot or find the match. */
80626a55
DE
12120
12121/* Create a hash table to map DWO IDs to their CU/TU entry in
12122 .debug_{info,types}.dwo in DWP_FILE.
12123 Returns NULL if there isn't one.
12124 Note: This function processes DWP files only, not DWO files. */
12125
12126static struct dwp_hash_table *
ed2dc618
SM
12127create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12128 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12129{
12130 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12131 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12132 const gdb_byte *index_ptr, *index_end;
80626a55 12133 struct dwarf2_section_info *index;
73869dc2 12134 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12135 struct dwp_hash_table *htab;
12136
12137 if (is_debug_types)
12138 index = &dwp_file->sections.tu_index;
12139 else
12140 index = &dwp_file->sections.cu_index;
12141
12142 if (dwarf2_section_empty_p (index))
12143 return NULL;
12144 dwarf2_read_section (objfile, index);
12145
12146 index_ptr = index->buffer;
12147 index_end = index_ptr + index->size;
12148
12149 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12150 index_ptr += 4;
12151 if (version == 2)
12152 nr_columns = read_4_bytes (dbfd, index_ptr);
12153 else
12154 nr_columns = 0;
12155 index_ptr += 4;
80626a55
DE
12156 nr_units = read_4_bytes (dbfd, index_ptr);
12157 index_ptr += 4;
12158 nr_slots = read_4_bytes (dbfd, index_ptr);
12159 index_ptr += 4;
12160
73869dc2 12161 if (version != 1 && version != 2)
80626a55 12162 {
21aa081e 12163 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12164 " [in module %s]"),
21aa081e 12165 pulongest (version), dwp_file->name);
80626a55
DE
12166 }
12167 if (nr_slots != (nr_slots & -nr_slots))
12168 {
21aa081e 12169 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12170 " is not power of 2 [in module %s]"),
21aa081e 12171 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12172 }
12173
12174 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12175 htab->version = version;
12176 htab->nr_columns = nr_columns;
80626a55
DE
12177 htab->nr_units = nr_units;
12178 htab->nr_slots = nr_slots;
12179 htab->hash_table = index_ptr;
12180 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12181
12182 /* Exit early if the table is empty. */
12183 if (nr_slots == 0 || nr_units == 0
12184 || (version == 2 && nr_columns == 0))
12185 {
12186 /* All must be zero. */
12187 if (nr_slots != 0 || nr_units != 0
12188 || (version == 2 && nr_columns != 0))
12189 {
b98664d3 12190 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12191 " all zero [in modules %s]"),
12192 dwp_file->name);
12193 }
12194 return htab;
12195 }
12196
12197 if (version == 1)
12198 {
12199 htab->section_pool.v1.indices =
12200 htab->unit_table + sizeof (uint32_t) * nr_slots;
12201 /* It's harder to decide whether the section is too small in v1.
12202 V1 is deprecated anyway so we punt. */
12203 }
12204 else
12205 {
12206 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12207 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12208 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12209 /* Reverse map for error checking. */
12210 int ids_seen[DW_SECT_MAX + 1];
12211 int i;
12212
12213 if (nr_columns < 2)
12214 {
12215 error (_("Dwarf Error: bad DWP hash table, too few columns"
12216 " in section table [in module %s]"),
12217 dwp_file->name);
12218 }
12219 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12220 {
12221 error (_("Dwarf Error: bad DWP hash table, too many columns"
12222 " in section table [in module %s]"),
12223 dwp_file->name);
12224 }
04fd5eed
GB
12225 memset (ids, 255, sizeof_ids);
12226 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12227 for (i = 0; i < nr_columns; ++i)
12228 {
12229 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12230
12231 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12232 {
12233 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12234 " in section table [in module %s]"),
12235 id, dwp_file->name);
12236 }
12237 if (ids_seen[id] != -1)
12238 {
12239 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12240 " id %d in section table [in module %s]"),
12241 id, dwp_file->name);
12242 }
12243 ids_seen[id] = i;
12244 ids[i] = id;
12245 }
12246 /* Must have exactly one info or types section. */
12247 if (((ids_seen[DW_SECT_INFO] != -1)
12248 + (ids_seen[DW_SECT_TYPES] != -1))
12249 != 1)
12250 {
12251 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12252 " DWO info/types section [in module %s]"),
12253 dwp_file->name);
12254 }
12255 /* Must have an abbrev section. */
12256 if (ids_seen[DW_SECT_ABBREV] == -1)
12257 {
12258 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12259 " section [in module %s]"),
12260 dwp_file->name);
12261 }
12262 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12263 htab->section_pool.v2.sizes =
12264 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12265 * nr_units * nr_columns);
12266 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12267 * nr_units * nr_columns))
12268 > index_end)
12269 {
12270 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12271 " [in module %s]"),
12272 dwp_file->name);
12273 }
12274 }
80626a55
DE
12275
12276 return htab;
12277}
12278
12279/* Update SECTIONS with the data from SECTP.
12280
12281 This function is like the other "locate" section routines that are
12282 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12283 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12284
12285 The result is non-zero for success, or zero if an error was found. */
12286
12287static int
73869dc2
DE
12288locate_v1_virtual_dwo_sections (asection *sectp,
12289 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12290{
12291 const struct dwop_section_names *names = &dwop_section_names;
12292
12293 if (section_is_p (sectp->name, &names->abbrev_dwo))
12294 {
12295 /* There can be only one. */
049412e3 12296 if (sections->abbrev.s.section != NULL)
80626a55 12297 return 0;
049412e3 12298 sections->abbrev.s.section = sectp;
fd361982 12299 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12300 }
12301 else if (section_is_p (sectp->name, &names->info_dwo)
12302 || section_is_p (sectp->name, &names->types_dwo))
12303 {
12304 /* There can be only one. */
049412e3 12305 if (sections->info_or_types.s.section != NULL)
80626a55 12306 return 0;
049412e3 12307 sections->info_or_types.s.section = sectp;
fd361982 12308 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12309 }
12310 else if (section_is_p (sectp->name, &names->line_dwo))
12311 {
12312 /* There can be only one. */
049412e3 12313 if (sections->line.s.section != NULL)
80626a55 12314 return 0;
049412e3 12315 sections->line.s.section = sectp;
fd361982 12316 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12317 }
12318 else if (section_is_p (sectp->name, &names->loc_dwo))
12319 {
12320 /* There can be only one. */
049412e3 12321 if (sections->loc.s.section != NULL)
80626a55 12322 return 0;
049412e3 12323 sections->loc.s.section = sectp;
fd361982 12324 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12325 }
12326 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12327 {
12328 /* There can be only one. */
049412e3 12329 if (sections->macinfo.s.section != NULL)
80626a55 12330 return 0;
049412e3 12331 sections->macinfo.s.section = sectp;
fd361982 12332 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12333 }
12334 else if (section_is_p (sectp->name, &names->macro_dwo))
12335 {
12336 /* There can be only one. */
049412e3 12337 if (sections->macro.s.section != NULL)
80626a55 12338 return 0;
049412e3 12339 sections->macro.s.section = sectp;
fd361982 12340 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12341 }
12342 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12343 {
12344 /* There can be only one. */
049412e3 12345 if (sections->str_offsets.s.section != NULL)
80626a55 12346 return 0;
049412e3 12347 sections->str_offsets.s.section = sectp;
fd361982 12348 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12349 }
12350 else
12351 {
12352 /* No other kind of section is valid. */
12353 return 0;
12354 }
12355
12356 return 1;
12357}
12358
73869dc2
DE
12359/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12360 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12361 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12362 This is for DWP version 1 files. */
80626a55
DE
12363
12364static struct dwo_unit *
ed2dc618
SM
12365create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12366 struct dwp_file *dwp_file,
73869dc2
DE
12367 uint32_t unit_index,
12368 const char *comp_dir,
12369 ULONGEST signature, int is_debug_types)
80626a55
DE
12370{
12371 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12372 const struct dwp_hash_table *dwp_htab =
12373 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12374 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12375 const char *kind = is_debug_types ? "TU" : "CU";
12376 struct dwo_file *dwo_file;
12377 struct dwo_unit *dwo_unit;
73869dc2 12378 struct virtual_v1_dwo_sections sections;
80626a55 12379 void **dwo_file_slot;
80626a55
DE
12380 int i;
12381
73869dc2
DE
12382 gdb_assert (dwp_file->version == 1);
12383
b4f54984 12384 if (dwarf_read_debug)
80626a55 12385 {
73869dc2 12386 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12387 kind,
73869dc2 12388 pulongest (unit_index), hex_string (signature),
80626a55
DE
12389 dwp_file->name);
12390 }
12391
19ac8c2e 12392 /* Fetch the sections of this DWO unit.
80626a55
DE
12393 Put a limit on the number of sections we look for so that bad data
12394 doesn't cause us to loop forever. */
12395
73869dc2 12396#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12397 (1 /* .debug_info or .debug_types */ \
12398 + 1 /* .debug_abbrev */ \
12399 + 1 /* .debug_line */ \
12400 + 1 /* .debug_loc */ \
12401 + 1 /* .debug_str_offsets */ \
19ac8c2e 12402 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12403 + 1 /* trailing zero */)
12404
12405 memset (&sections, 0, sizeof (sections));
80626a55 12406
73869dc2 12407 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12408 {
12409 asection *sectp;
12410 uint32_t section_nr =
12411 read_4_bytes (dbfd,
73869dc2
DE
12412 dwp_htab->section_pool.v1.indices
12413 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12414
12415 if (section_nr == 0)
12416 break;
12417 if (section_nr >= dwp_file->num_sections)
12418 {
12419 error (_("Dwarf Error: bad DWP hash table, section number too large"
12420 " [in module %s]"),
12421 dwp_file->name);
12422 }
12423
12424 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12425 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12426 {
12427 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12428 " [in module %s]"),
12429 dwp_file->name);
12430 }
12431 }
12432
12433 if (i < 2
a32a8923
DE
12434 || dwarf2_section_empty_p (&sections.info_or_types)
12435 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12436 {
12437 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12438 " [in module %s]"),
12439 dwp_file->name);
12440 }
73869dc2 12441 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12442 {
12443 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12444 " [in module %s]"),
12445 dwp_file->name);
12446 }
12447
12448 /* It's easier for the rest of the code if we fake a struct dwo_file and
12449 have dwo_unit "live" in that. At least for now.
12450
12451 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12452 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12453 file, we can combine them back into a virtual DWO file to save space
12454 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12455 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12456
791afaa2
TT
12457 std::string virtual_dwo_name =
12458 string_printf ("virtual-dwo/%d-%d-%d-%d",
12459 get_section_id (&sections.abbrev),
12460 get_section_id (&sections.line),
12461 get_section_id (&sections.loc),
12462 get_section_id (&sections.str_offsets));
80626a55 12463 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12464 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12465 virtual_dwo_name.c_str (),
12466 comp_dir);
80626a55
DE
12467 /* Create one if necessary. */
12468 if (*dwo_file_slot == NULL)
12469 {
b4f54984 12470 if (dwarf_read_debug)
80626a55
DE
12471 {
12472 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12473 virtual_dwo_name.c_str ());
80626a55 12474 }
51ac9db5 12475 dwo_file = new struct dwo_file;
efba19b0
TT
12476 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12477 virtual_dwo_name);
0ac5b59e 12478 dwo_file->comp_dir = comp_dir;
80626a55
DE
12479 dwo_file->sections.abbrev = sections.abbrev;
12480 dwo_file->sections.line = sections.line;
12481 dwo_file->sections.loc = sections.loc;
12482 dwo_file->sections.macinfo = sections.macinfo;
12483 dwo_file->sections.macro = sections.macro;
12484 dwo_file->sections.str_offsets = sections.str_offsets;
12485 /* The "str" section is global to the entire DWP file. */
12486 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12487 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12488 there's no need to record it in dwo_file.
12489 Also, we can't simply record type sections in dwo_file because
12490 we record a pointer into the vector in dwo_unit. As we collect more
12491 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12492 for it, invalidating all copies of pointers into the previous
12493 contents. */
80626a55
DE
12494 *dwo_file_slot = dwo_file;
12495 }
12496 else
12497 {
b4f54984 12498 if (dwarf_read_debug)
80626a55
DE
12499 {
12500 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12501 virtual_dwo_name.c_str ());
80626a55 12502 }
9a3c8263 12503 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12504 }
80626a55
DE
12505
12506 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12507 dwo_unit->dwo_file = dwo_file;
12508 dwo_unit->signature = signature;
8d749320
SM
12509 dwo_unit->section =
12510 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12511 *dwo_unit->section = sections.info_or_types;
57d63ce2 12512 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12513
12514 return dwo_unit;
12515}
12516
73869dc2
DE
12517/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12518 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12519 piece within that section used by a TU/CU, return a virtual section
12520 of just that piece. */
12521
12522static struct dwarf2_section_info
ed2dc618
SM
12523create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12524 struct dwarf2_section_info *section,
73869dc2
DE
12525 bfd_size_type offset, bfd_size_type size)
12526{
12527 struct dwarf2_section_info result;
12528 asection *sectp;
12529
12530 gdb_assert (section != NULL);
12531 gdb_assert (!section->is_virtual);
12532
12533 memset (&result, 0, sizeof (result));
12534 result.s.containing_section = section;
dc4ccb6f 12535 result.is_virtual = true;
73869dc2
DE
12536
12537 if (size == 0)
12538 return result;
12539
12540 sectp = get_section_bfd_section (section);
12541
12542 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12543 bounds of the real section. This is a pretty-rare event, so just
12544 flag an error (easier) instead of a warning and trying to cope. */
12545 if (sectp == NULL
fd361982 12546 || offset + size > bfd_section_size (sectp))
73869dc2 12547 {
73869dc2
DE
12548 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12549 " in section %s [in module %s]"),
fd361982 12550 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12551 objfile_name (dwarf2_per_objfile->objfile));
12552 }
12553
12554 result.virtual_offset = offset;
12555 result.size = size;
12556 return result;
12557}
12558
12559/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12560 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12561 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12562 This is for DWP version 2 files. */
12563
12564static struct dwo_unit *
ed2dc618
SM
12565create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12566 struct dwp_file *dwp_file,
73869dc2
DE
12567 uint32_t unit_index,
12568 const char *comp_dir,
12569 ULONGEST signature, int is_debug_types)
12570{
12571 struct objfile *objfile = dwarf2_per_objfile->objfile;
12572 const struct dwp_hash_table *dwp_htab =
12573 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12574 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12575 const char *kind = is_debug_types ? "TU" : "CU";
12576 struct dwo_file *dwo_file;
12577 struct dwo_unit *dwo_unit;
12578 struct virtual_v2_dwo_sections sections;
12579 void **dwo_file_slot;
73869dc2
DE
12580 int i;
12581
12582 gdb_assert (dwp_file->version == 2);
12583
b4f54984 12584 if (dwarf_read_debug)
73869dc2
DE
12585 {
12586 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12587 kind,
12588 pulongest (unit_index), hex_string (signature),
12589 dwp_file->name);
12590 }
12591
12592 /* Fetch the section offsets of this DWO unit. */
12593
12594 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12595
12596 for (i = 0; i < dwp_htab->nr_columns; ++i)
12597 {
12598 uint32_t offset = read_4_bytes (dbfd,
12599 dwp_htab->section_pool.v2.offsets
12600 + (((unit_index - 1) * dwp_htab->nr_columns
12601 + i)
12602 * sizeof (uint32_t)));
12603 uint32_t size = read_4_bytes (dbfd,
12604 dwp_htab->section_pool.v2.sizes
12605 + (((unit_index - 1) * dwp_htab->nr_columns
12606 + i)
12607 * sizeof (uint32_t)));
12608
12609 switch (dwp_htab->section_pool.v2.section_ids[i])
12610 {
12611 case DW_SECT_INFO:
12612 case DW_SECT_TYPES:
12613 sections.info_or_types_offset = offset;
12614 sections.info_or_types_size = size;
12615 break;
12616 case DW_SECT_ABBREV:
12617 sections.abbrev_offset = offset;
12618 sections.abbrev_size = size;
12619 break;
12620 case DW_SECT_LINE:
12621 sections.line_offset = offset;
12622 sections.line_size = size;
12623 break;
12624 case DW_SECT_LOC:
12625 sections.loc_offset = offset;
12626 sections.loc_size = size;
12627 break;
12628 case DW_SECT_STR_OFFSETS:
12629 sections.str_offsets_offset = offset;
12630 sections.str_offsets_size = size;
12631 break;
12632 case DW_SECT_MACINFO:
12633 sections.macinfo_offset = offset;
12634 sections.macinfo_size = size;
12635 break;
12636 case DW_SECT_MACRO:
12637 sections.macro_offset = offset;
12638 sections.macro_size = size;
12639 break;
12640 }
12641 }
12642
12643 /* It's easier for the rest of the code if we fake a struct dwo_file and
12644 have dwo_unit "live" in that. At least for now.
12645
12646 The DWP file can be made up of a random collection of CUs and TUs.
12647 However, for each CU + set of TUs that came from the same original DWO
12648 file, we can combine them back into a virtual DWO file to save space
12649 (fewer struct dwo_file objects to allocate). Remember that for really
12650 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12651
791afaa2
TT
12652 std::string virtual_dwo_name =
12653 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12654 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12655 (long) (sections.line_size ? sections.line_offset : 0),
12656 (long) (sections.loc_size ? sections.loc_offset : 0),
12657 (long) (sections.str_offsets_size
12658 ? sections.str_offsets_offset : 0));
73869dc2 12659 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12660 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12661 virtual_dwo_name.c_str (),
12662 comp_dir);
73869dc2
DE
12663 /* Create one if necessary. */
12664 if (*dwo_file_slot == NULL)
12665 {
b4f54984 12666 if (dwarf_read_debug)
73869dc2
DE
12667 {
12668 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12669 virtual_dwo_name.c_str ());
73869dc2 12670 }
51ac9db5 12671 dwo_file = new struct dwo_file;
efba19b0
TT
12672 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12673 virtual_dwo_name);
73869dc2
DE
12674 dwo_file->comp_dir = comp_dir;
12675 dwo_file->sections.abbrev =
ed2dc618 12676 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12677 sections.abbrev_offset, sections.abbrev_size);
12678 dwo_file->sections.line =
ed2dc618 12679 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12680 sections.line_offset, sections.line_size);
12681 dwo_file->sections.loc =
ed2dc618 12682 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12683 sections.loc_offset, sections.loc_size);
12684 dwo_file->sections.macinfo =
ed2dc618 12685 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12686 sections.macinfo_offset, sections.macinfo_size);
12687 dwo_file->sections.macro =
ed2dc618 12688 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12689 sections.macro_offset, sections.macro_size);
12690 dwo_file->sections.str_offsets =
ed2dc618
SM
12691 create_dwp_v2_section (dwarf2_per_objfile,
12692 &dwp_file->sections.str_offsets,
73869dc2
DE
12693 sections.str_offsets_offset,
12694 sections.str_offsets_size);
12695 /* The "str" section is global to the entire DWP file. */
12696 dwo_file->sections.str = dwp_file->sections.str;
12697 /* The info or types section is assigned below to dwo_unit,
12698 there's no need to record it in dwo_file.
12699 Also, we can't simply record type sections in dwo_file because
12700 we record a pointer into the vector in dwo_unit. As we collect more
12701 types we'll grow the vector and eventually have to reallocate space
12702 for it, invalidating all copies of pointers into the previous
12703 contents. */
12704 *dwo_file_slot = dwo_file;
12705 }
12706 else
12707 {
b4f54984 12708 if (dwarf_read_debug)
73869dc2
DE
12709 {
12710 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12711 virtual_dwo_name.c_str ());
73869dc2 12712 }
9a3c8263 12713 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12714 }
73869dc2
DE
12715
12716 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12717 dwo_unit->dwo_file = dwo_file;
12718 dwo_unit->signature = signature;
8d749320
SM
12719 dwo_unit->section =
12720 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12721 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12722 is_debug_types
73869dc2
DE
12723 ? &dwp_file->sections.types
12724 : &dwp_file->sections.info,
12725 sections.info_or_types_offset,
12726 sections.info_or_types_size);
12727 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12728
12729 return dwo_unit;
12730}
12731
57d63ce2
DE
12732/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12733 Returns NULL if the signature isn't found. */
80626a55
DE
12734
12735static struct dwo_unit *
ed2dc618
SM
12736lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12737 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12738 ULONGEST signature, int is_debug_types)
80626a55 12739{
57d63ce2
DE
12740 const struct dwp_hash_table *dwp_htab =
12741 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12742 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12743 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12744 uint32_t hash = signature & mask;
12745 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12746 unsigned int i;
12747 void **slot;
870f88f7 12748 struct dwo_unit find_dwo_cu;
80626a55
DE
12749
12750 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12751 find_dwo_cu.signature = signature;
19ac8c2e
DE
12752 slot = htab_find_slot (is_debug_types
12753 ? dwp_file->loaded_tus
12754 : dwp_file->loaded_cus,
12755 &find_dwo_cu, INSERT);
80626a55
DE
12756
12757 if (*slot != NULL)
9a3c8263 12758 return (struct dwo_unit *) *slot;
80626a55
DE
12759
12760 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12761 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12762 {
12763 ULONGEST signature_in_table;
12764
12765 signature_in_table =
57d63ce2 12766 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12767 if (signature_in_table == signature)
12768 {
57d63ce2
DE
12769 uint32_t unit_index =
12770 read_4_bytes (dbfd,
12771 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12772
73869dc2
DE
12773 if (dwp_file->version == 1)
12774 {
ed2dc618
SM
12775 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12776 dwp_file, unit_index,
73869dc2
DE
12777 comp_dir, signature,
12778 is_debug_types);
12779 }
12780 else
12781 {
ed2dc618
SM
12782 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12783 dwp_file, unit_index,
73869dc2
DE
12784 comp_dir, signature,
12785 is_debug_types);
12786 }
9a3c8263 12787 return (struct dwo_unit *) *slot;
80626a55
DE
12788 }
12789 if (signature_in_table == 0)
12790 return NULL;
12791 hash = (hash + hash2) & mask;
12792 }
12793
12794 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12795 " [in module %s]"),
12796 dwp_file->name);
12797}
12798
ab5088bf 12799/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12800 Open the file specified by FILE_NAME and hand it off to BFD for
12801 preliminary analysis. Return a newly initialized bfd *, which
12802 includes a canonicalized copy of FILE_NAME.
80626a55 12803 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12804 SEARCH_CWD is true if the current directory is to be searched.
12805 It will be searched before debug-file-directory.
13aaf454
DE
12806 If successful, the file is added to the bfd include table of the
12807 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12808 If unable to find/open the file, return NULL.
3019eac3
DE
12809 NOTE: This function is derived from symfile_bfd_open. */
12810
192b62ce 12811static gdb_bfd_ref_ptr
ed2dc618
SM
12812try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12813 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12814{
24b9144d 12815 int desc;
9c02c129
DE
12816 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12817 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12818 to debug_file_directory. */
e0cc99a6 12819 const char *search_path;
9c02c129
DE
12820 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12821
e0cc99a6 12822 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12823 if (search_cwd)
12824 {
12825 if (*debug_file_directory != '\0')
e0cc99a6
TT
12826 {
12827 search_path_holder.reset (concat (".", dirname_separator_string,
12828 debug_file_directory,
12829 (char *) NULL));
12830 search_path = search_path_holder.get ();
12831 }
6ac97d4c 12832 else
e0cc99a6 12833 search_path = ".";
6ac97d4c 12834 }
9c02c129 12835 else
e0cc99a6 12836 search_path = debug_file_directory;
3019eac3 12837
24b9144d 12838 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12839 if (is_dwp)
12840 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12841
12842 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12843 desc = openp (search_path, flags, file_name,
3019eac3
DE
12844 O_RDONLY | O_BINARY, &absolute_name);
12845 if (desc < 0)
12846 return NULL;
12847
e0cc99a6
TT
12848 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12849 gnutarget, desc));
9c02c129
DE
12850 if (sym_bfd == NULL)
12851 return NULL;
192b62ce 12852 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12853
192b62ce
TT
12854 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12855 return NULL;
3019eac3 12856
13aaf454
DE
12857 /* Success. Record the bfd as having been included by the objfile's bfd.
12858 This is important because things like demangled_names_hash lives in the
12859 objfile's per_bfd space and may have references to things like symbol
12860 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12861 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12862
3019eac3
DE
12863 return sym_bfd;
12864}
12865
ab5088bf 12866/* Try to open DWO file FILE_NAME.
3019eac3
DE
12867 COMP_DIR is the DW_AT_comp_dir attribute.
12868 The result is the bfd handle of the file.
12869 If there is a problem finding or opening the file, return NULL.
12870 Upon success, the canonicalized path of the file is stored in the bfd,
12871 same as symfile_bfd_open. */
12872
192b62ce 12873static gdb_bfd_ref_ptr
ed2dc618
SM
12874open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12875 const char *file_name, const char *comp_dir)
3019eac3 12876{
80626a55 12877 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12878 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12879 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12880
12881 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12882
12883 if (comp_dir != NULL)
12884 {
43816ebc
TT
12885 gdb::unique_xmalloc_ptr<char> path_to_try
12886 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12887
12888 /* NOTE: If comp_dir is a relative path, this will also try the
12889 search path, which seems useful. */
ed2dc618 12890 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12891 path_to_try.get (),
ed2dc618 12892 0 /*is_dwp*/,
192b62ce 12893 1 /*search_cwd*/));
3019eac3
DE
12894 if (abfd != NULL)
12895 return abfd;
12896 }
12897
12898 /* That didn't work, try debug-file-directory, which, despite its name,
12899 is a list of paths. */
12900
12901 if (*debug_file_directory == '\0')
12902 return NULL;
12903
ed2dc618
SM
12904 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12905 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12906}
12907
80626a55
DE
12908/* This function is mapped across the sections and remembers the offset and
12909 size of each of the DWO debugging sections we are interested in. */
12910
12911static void
12912dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12913{
9a3c8263 12914 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12915 const struct dwop_section_names *names = &dwop_section_names;
12916
12917 if (section_is_p (sectp->name, &names->abbrev_dwo))
12918 {
049412e3 12919 dwo_sections->abbrev.s.section = sectp;
fd361982 12920 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12921 }
12922 else if (section_is_p (sectp->name, &names->info_dwo))
12923 {
049412e3 12924 dwo_sections->info.s.section = sectp;
fd361982 12925 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12926 }
12927 else if (section_is_p (sectp->name, &names->line_dwo))
12928 {
049412e3 12929 dwo_sections->line.s.section = sectp;
fd361982 12930 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12931 }
12932 else if (section_is_p (sectp->name, &names->loc_dwo))
12933 {
049412e3 12934 dwo_sections->loc.s.section = sectp;
fd361982 12935 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12936 }
12937 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12938 {
049412e3 12939 dwo_sections->macinfo.s.section = sectp;
fd361982 12940 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12941 }
12942 else if (section_is_p (sectp->name, &names->macro_dwo))
12943 {
049412e3 12944 dwo_sections->macro.s.section = sectp;
fd361982 12945 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12946 }
12947 else if (section_is_p (sectp->name, &names->str_dwo))
12948 {
049412e3 12949 dwo_sections->str.s.section = sectp;
fd361982 12950 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12951 }
12952 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12953 {
049412e3 12954 dwo_sections->str_offsets.s.section = sectp;
fd361982 12955 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12956 }
12957 else if (section_is_p (sectp->name, &names->types_dwo))
12958 {
12959 struct dwarf2_section_info type_section;
12960
12961 memset (&type_section, 0, sizeof (type_section));
049412e3 12962 type_section.s.section = sectp;
fd361982 12963 type_section.size = bfd_section_size (sectp);
fd5866f6 12964 dwo_sections->types.push_back (type_section);
80626a55
DE
12965 }
12966}
12967
ab5088bf 12968/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12969 by PER_CU. This is for the non-DWP case.
80626a55 12970 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12971
12972static struct dwo_file *
0ac5b59e
DE
12973open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12974 const char *dwo_name, const char *comp_dir)
3019eac3 12975{
ed2dc618 12976 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12977
fb1eb2f9 12978 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12979 if (dbfd == NULL)
12980 {
b4f54984 12981 if (dwarf_read_debug)
80626a55
DE
12982 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12983 return NULL;
12984 }
263db9a1 12985
51ac9db5 12986 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12987 dwo_file->dwo_name = dwo_name;
12988 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12989 dwo_file->dbfd = std::move (dbfd);
3019eac3 12990
fb1eb2f9 12991 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12992 &dwo_file->sections);
3019eac3 12993
18a8505e
AT
12994 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12995 dwo_file->sections.info, dwo_file->cus);
3019eac3 12996
263db9a1 12997 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12998 dwo_file->sections.types, dwo_file->tus);
3019eac3 12999
b4f54984 13000 if (dwarf_read_debug)
80626a55
DE
13001 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13002
263db9a1 13003 return dwo_file.release ();
3019eac3
DE
13004}
13005
80626a55 13006/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13007 size of each of the DWP debugging sections common to version 1 and 2 that
13008 we are interested in. */
3019eac3 13009
80626a55 13010static void
73869dc2
DE
13011dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13012 void *dwp_file_ptr)
3019eac3 13013{
9a3c8263 13014 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13015 const struct dwop_section_names *names = &dwop_section_names;
13016 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13017
80626a55 13018 /* Record the ELF section number for later lookup: this is what the
73869dc2 13019 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13020 gdb_assert (elf_section_nr < dwp_file->num_sections);
13021 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13022
80626a55
DE
13023 /* Look for specific sections that we need. */
13024 if (section_is_p (sectp->name, &names->str_dwo))
13025 {
049412e3 13026 dwp_file->sections.str.s.section = sectp;
fd361982 13027 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13028 }
13029 else if (section_is_p (sectp->name, &names->cu_index))
13030 {
049412e3 13031 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13032 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13033 }
13034 else if (section_is_p (sectp->name, &names->tu_index))
13035 {
049412e3 13036 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13037 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13038 }
13039}
3019eac3 13040
73869dc2
DE
13041/* This function is mapped across the sections and remembers the offset and
13042 size of each of the DWP version 2 debugging sections that we are interested
13043 in. This is split into a separate function because we don't know if we
13044 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13045
13046static void
13047dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13048{
9a3c8263 13049 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13050 const struct dwop_section_names *names = &dwop_section_names;
13051 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13052
13053 /* Record the ELF section number for later lookup: this is what the
13054 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13055 gdb_assert (elf_section_nr < dwp_file->num_sections);
13056 dwp_file->elf_sections[elf_section_nr] = sectp;
13057
13058 /* Look for specific sections that we need. */
13059 if (section_is_p (sectp->name, &names->abbrev_dwo))
13060 {
049412e3 13061 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13062 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13063 }
13064 else if (section_is_p (sectp->name, &names->info_dwo))
13065 {
049412e3 13066 dwp_file->sections.info.s.section = sectp;
fd361982 13067 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13068 }
13069 else if (section_is_p (sectp->name, &names->line_dwo))
13070 {
049412e3 13071 dwp_file->sections.line.s.section = sectp;
fd361982 13072 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13073 }
13074 else if (section_is_p (sectp->name, &names->loc_dwo))
13075 {
049412e3 13076 dwp_file->sections.loc.s.section = sectp;
fd361982 13077 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13078 }
13079 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13080 {
049412e3 13081 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13082 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13083 }
13084 else if (section_is_p (sectp->name, &names->macro_dwo))
13085 {
049412e3 13086 dwp_file->sections.macro.s.section = sectp;
fd361982 13087 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13088 }
13089 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13090 {
049412e3 13091 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13092 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13093 }
13094 else if (section_is_p (sectp->name, &names->types_dwo))
13095 {
049412e3 13096 dwp_file->sections.types.s.section = sectp;
fd361982 13097 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13098 }
13099}
13100
80626a55 13101/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13102
80626a55
DE
13103static hashval_t
13104hash_dwp_loaded_cutus (const void *item)
13105{
9a3c8263 13106 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13107
80626a55
DE
13108 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13109 return dwo_unit->signature;
3019eac3
DE
13110}
13111
80626a55 13112/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13113
80626a55
DE
13114static int
13115eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13116{
9a3c8263
SM
13117 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13118 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13119
80626a55
DE
13120 return dua->signature == dub->signature;
13121}
3019eac3 13122
80626a55 13123/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13124
80626a55
DE
13125static htab_t
13126allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13127{
13128 return htab_create_alloc_ex (3,
13129 hash_dwp_loaded_cutus,
13130 eq_dwp_loaded_cutus,
13131 NULL,
13132 &objfile->objfile_obstack,
13133 hashtab_obstack_allocate,
13134 dummy_obstack_deallocate);
13135}
3019eac3 13136
ab5088bf
DE
13137/* Try to open DWP file FILE_NAME.
13138 The result is the bfd handle of the file.
13139 If there is a problem finding or opening the file, return NULL.
13140 Upon success, the canonicalized path of the file is stored in the bfd,
13141 same as symfile_bfd_open. */
13142
192b62ce 13143static gdb_bfd_ref_ptr
ed2dc618
SM
13144open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13145 const char *file_name)
ab5088bf 13146{
ed2dc618
SM
13147 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13148 1 /*is_dwp*/,
192b62ce 13149 1 /*search_cwd*/));
6ac97d4c
DE
13150 if (abfd != NULL)
13151 return abfd;
13152
13153 /* Work around upstream bug 15652.
13154 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13155 [Whether that's a "bug" is debatable, but it is getting in our way.]
13156 We have no real idea where the dwp file is, because gdb's realpath-ing
13157 of the executable's path may have discarded the needed info.
13158 [IWBN if the dwp file name was recorded in the executable, akin to
13159 .gnu_debuglink, but that doesn't exist yet.]
13160 Strip the directory from FILE_NAME and search again. */
13161 if (*debug_file_directory != '\0')
13162 {
13163 /* Don't implicitly search the current directory here.
13164 If the user wants to search "." to handle this case,
13165 it must be added to debug-file-directory. */
ed2dc618
SM
13166 return try_open_dwop_file (dwarf2_per_objfile,
13167 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13168 0 /*search_cwd*/);
13169 }
13170
13171 return NULL;
ab5088bf
DE
13172}
13173
80626a55
DE
13174/* Initialize the use of the DWP file for the current objfile.
13175 By convention the name of the DWP file is ${objfile}.dwp.
13176 The result is NULL if it can't be found. */
a766d390 13177
400174b1 13178static std::unique_ptr<struct dwp_file>
ed2dc618 13179open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13180{
13181 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13182
82bf32bc
JK
13183 /* Try to find first .dwp for the binary file before any symbolic links
13184 resolving. */
6c447423
DE
13185
13186 /* If the objfile is a debug file, find the name of the real binary
13187 file and get the name of dwp file from there. */
d721ba37 13188 std::string dwp_name;
6c447423
DE
13189 if (objfile->separate_debug_objfile_backlink != NULL)
13190 {
13191 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13192 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13193
d721ba37 13194 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13195 }
13196 else
d721ba37
PA
13197 dwp_name = objfile->original_name;
13198
13199 dwp_name += ".dwp";
80626a55 13200
ed2dc618 13201 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13202 if (dbfd == NULL
13203 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13204 {
13205 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13206 dwp_name = objfile_name (objfile);
13207 dwp_name += ".dwp";
ed2dc618 13208 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13209 }
13210
80626a55
DE
13211 if (dbfd == NULL)
13212 {
b4f54984 13213 if (dwarf_read_debug)
d721ba37 13214 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13215 return std::unique_ptr<dwp_file> ();
3019eac3 13216 }
400174b1
TT
13217
13218 const char *name = bfd_get_filename (dbfd.get ());
13219 std::unique_ptr<struct dwp_file> dwp_file
13220 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13221
0a0f4c01 13222 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13223 dwp_file->elf_sections =
13224 OBSTACK_CALLOC (&objfile->objfile_obstack,
13225 dwp_file->num_sections, asection *);
13226
400174b1
TT
13227 bfd_map_over_sections (dwp_file->dbfd.get (),
13228 dwarf2_locate_common_dwp_sections,
13229 dwp_file.get ());
80626a55 13230
400174b1
TT
13231 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13232 0);
80626a55 13233
400174b1
TT
13234 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13235 1);
80626a55 13236
73869dc2 13237 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13238 if (dwp_file->cus && dwp_file->tus
13239 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13240 {
13241 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13242 pretty bizarre. We use pulongest here because that's the established
4d65956b 13243 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13244 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13245 " TU version %s [in DWP file %s]"),
13246 pulongest (dwp_file->cus->version),
d721ba37 13247 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13248 }
08302ed2
DE
13249
13250 if (dwp_file->cus)
13251 dwp_file->version = dwp_file->cus->version;
13252 else if (dwp_file->tus)
13253 dwp_file->version = dwp_file->tus->version;
13254 else
13255 dwp_file->version = 2;
73869dc2
DE
13256
13257 if (dwp_file->version == 2)
400174b1
TT
13258 bfd_map_over_sections (dwp_file->dbfd.get (),
13259 dwarf2_locate_v2_dwp_sections,
13260 dwp_file.get ());
73869dc2 13261
19ac8c2e
DE
13262 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13263 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13264
b4f54984 13265 if (dwarf_read_debug)
80626a55
DE
13266 {
13267 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13268 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13269 " %s CUs, %s TUs\n",
13270 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13271 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13272 }
13273
13274 return dwp_file;
3019eac3 13275}
c906108c 13276
ab5088bf
DE
13277/* Wrapper around open_and_init_dwp_file, only open it once. */
13278
13279static struct dwp_file *
ed2dc618 13280get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13281{
13282 if (! dwarf2_per_objfile->dwp_checked)
13283 {
ed2dc618
SM
13284 dwarf2_per_objfile->dwp_file
13285 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13286 dwarf2_per_objfile->dwp_checked = 1;
13287 }
400174b1 13288 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13289}
13290
80626a55
DE
13291/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13292 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13293 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13294 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13295 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13296
13297 This is called, for example, when wanting to read a variable with a
13298 complex location. Therefore we don't want to do file i/o for every call.
13299 Therefore we don't want to look for a DWO file on every call.
13300 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13301 then we check if we've already seen DWO_NAME, and only THEN do we check
13302 for a DWO file.
13303
1c658ad5 13304 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13305 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13306
3019eac3 13307static struct dwo_unit *
80626a55
DE
13308lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13309 const char *dwo_name, const char *comp_dir,
13310 ULONGEST signature, int is_debug_types)
3019eac3 13311{
ed2dc618 13312 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13313 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13314 const char *kind = is_debug_types ? "TU" : "CU";
13315 void **dwo_file_slot;
3019eac3 13316 struct dwo_file *dwo_file;
80626a55 13317 struct dwp_file *dwp_file;
cb1df416 13318
6a506a2d
DE
13319 /* First see if there's a DWP file.
13320 If we have a DWP file but didn't find the DWO inside it, don't
13321 look for the original DWO file. It makes gdb behave differently
13322 depending on whether one is debugging in the build tree. */
cf2c3c16 13323
ed2dc618 13324 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13325 if (dwp_file != NULL)
cf2c3c16 13326 {
80626a55
DE
13327 const struct dwp_hash_table *dwp_htab =
13328 is_debug_types ? dwp_file->tus : dwp_file->cus;
13329
13330 if (dwp_htab != NULL)
13331 {
13332 struct dwo_unit *dwo_cutu =
ed2dc618 13333 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13334 signature, is_debug_types);
80626a55
DE
13335
13336 if (dwo_cutu != NULL)
13337 {
b4f54984 13338 if (dwarf_read_debug)
80626a55
DE
13339 {
13340 fprintf_unfiltered (gdb_stdlog,
13341 "Virtual DWO %s %s found: @%s\n",
13342 kind, hex_string (signature),
13343 host_address_to_string (dwo_cutu));
13344 }
13345 return dwo_cutu;
13346 }
13347 }
13348 }
6a506a2d 13349 else
80626a55 13350 {
6a506a2d 13351 /* No DWP file, look for the DWO file. */
80626a55 13352
ed2dc618
SM
13353 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13354 dwo_name, comp_dir);
6a506a2d 13355 if (*dwo_file_slot == NULL)
80626a55 13356 {
6a506a2d
DE
13357 /* Read in the file and build a table of the CUs/TUs it contains. */
13358 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13359 }
6a506a2d 13360 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13361 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13362
6a506a2d 13363 if (dwo_file != NULL)
19c3d4c9 13364 {
6a506a2d
DE
13365 struct dwo_unit *dwo_cutu = NULL;
13366
13367 if (is_debug_types && dwo_file->tus)
13368 {
13369 struct dwo_unit find_dwo_cutu;
13370
13371 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13372 find_dwo_cutu.signature = signature;
9a3c8263
SM
13373 dwo_cutu
13374 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13375 }
33c5cd75 13376 else if (!is_debug_types && dwo_file->cus)
80626a55 13377 {
33c5cd75
DB
13378 struct dwo_unit find_dwo_cutu;
13379
13380 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13381 find_dwo_cutu.signature = signature;
13382 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13383 &find_dwo_cutu);
6a506a2d
DE
13384 }
13385
13386 if (dwo_cutu != NULL)
13387 {
b4f54984 13388 if (dwarf_read_debug)
6a506a2d
DE
13389 {
13390 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13391 kind, dwo_name, hex_string (signature),
13392 host_address_to_string (dwo_cutu));
13393 }
13394 return dwo_cutu;
80626a55
DE
13395 }
13396 }
2e276125 13397 }
9cdd5dbd 13398
80626a55
DE
13399 /* We didn't find it. This could mean a dwo_id mismatch, or
13400 someone deleted the DWO/DWP file, or the search path isn't set up
13401 correctly to find the file. */
13402
b4f54984 13403 if (dwarf_read_debug)
80626a55
DE
13404 {
13405 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13406 kind, dwo_name, hex_string (signature));
13407 }
3019eac3 13408
6656a72d
DE
13409 /* This is a warning and not a complaint because it can be caused by
13410 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13411 {
13412 /* Print the name of the DWP file if we looked there, helps the user
13413 better diagnose the problem. */
791afaa2 13414 std::string dwp_text;
43942612
DE
13415
13416 if (dwp_file != NULL)
791afaa2
TT
13417 dwp_text = string_printf (" [in DWP file %s]",
13418 lbasename (dwp_file->name));
43942612 13419
9d8780f0 13420 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13421 " [in module %s]"),
13422 kind, dwo_name, hex_string (signature),
791afaa2 13423 dwp_text.c_str (),
43942612 13424 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13425 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13426 }
3019eac3 13427 return NULL;
5fb290d7
DJ
13428}
13429
80626a55
DE
13430/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13431 See lookup_dwo_cutu_unit for details. */
13432
13433static struct dwo_unit *
13434lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13435 const char *dwo_name, const char *comp_dir,
13436 ULONGEST signature)
13437{
13438 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13439}
13440
13441/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13442 See lookup_dwo_cutu_unit for details. */
13443
13444static struct dwo_unit *
13445lookup_dwo_type_unit (struct signatured_type *this_tu,
13446 const char *dwo_name, const char *comp_dir)
13447{
13448 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13449}
13450
89e63ee4
DE
13451/* Traversal function for queue_and_load_all_dwo_tus. */
13452
13453static int
13454queue_and_load_dwo_tu (void **slot, void *info)
13455{
13456 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13457 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13458 ULONGEST signature = dwo_unit->signature;
13459 struct signatured_type *sig_type =
13460 lookup_dwo_signatured_type (per_cu->cu, signature);
13461
13462 if (sig_type != NULL)
13463 {
13464 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13465
13466 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13467 a real dependency of PER_CU on SIG_TYPE. That is detected later
13468 while processing PER_CU. */
13469 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13470 load_full_type_unit (sig_cu);
ae640021 13471 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13472 }
13473
13474 return 1;
13475}
13476
13477/* Queue all TUs contained in the DWO of PER_CU to be read in.
13478 The DWO may have the only definition of the type, though it may not be
13479 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13480 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13481
13482static void
13483queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13484{
13485 struct dwo_unit *dwo_unit;
13486 struct dwo_file *dwo_file;
13487
13488 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13489 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13490 gdb_assert (per_cu->cu != NULL);
13491
13492 dwo_unit = per_cu->cu->dwo_unit;
13493 gdb_assert (dwo_unit != NULL);
13494
13495 dwo_file = dwo_unit->dwo_file;
13496 if (dwo_file->tus != NULL)
13497 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13498}
13499
3019eac3 13500/* Read in various DIEs. */
348e048f 13501
d389af10 13502/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13503 Inherit only the children of the DW_AT_abstract_origin DIE not being
13504 already referenced by DW_AT_abstract_origin from the children of the
13505 current DIE. */
d389af10
JK
13506
13507static void
13508inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13509{
13510 struct die_info *child_die;
791afaa2 13511 sect_offset *offsetp;
d389af10
JK
13512 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13513 struct die_info *origin_die;
13514 /* Iterator of the ORIGIN_DIE children. */
13515 struct die_info *origin_child_die;
d389af10 13516 struct attribute *attr;
cd02d79d
PA
13517 struct dwarf2_cu *origin_cu;
13518 struct pending **origin_previous_list_in_scope;
d389af10
JK
13519
13520 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13521 if (!attr)
13522 return;
13523
cd02d79d
PA
13524 /* Note that following die references may follow to a die in a
13525 different cu. */
13526
13527 origin_cu = cu;
13528 origin_die = follow_die_ref (die, attr, &origin_cu);
13529
13530 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13531 symbols in. */
13532 origin_previous_list_in_scope = origin_cu->list_in_scope;
13533 origin_cu->list_in_scope = cu->list_in_scope;
13534
edb3359d
DJ
13535 if (die->tag != origin_die->tag
13536 && !(die->tag == DW_TAG_inlined_subroutine
13537 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13538 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13539 sect_offset_str (die->sect_off),
13540 sect_offset_str (origin_die->sect_off));
d389af10 13541
791afaa2 13542 std::vector<sect_offset> offsets;
d389af10 13543
3ea89b92
PMR
13544 for (child_die = die->child;
13545 child_die && child_die->tag;
13546 child_die = sibling_die (child_die))
13547 {
13548 struct die_info *child_origin_die;
13549 struct dwarf2_cu *child_origin_cu;
13550
13551 /* We are trying to process concrete instance entries:
216f72a1 13552 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13553 it's not relevant to our analysis here. i.e. detecting DIEs that are
13554 present in the abstract instance but not referenced in the concrete
13555 one. */
216f72a1
JK
13556 if (child_die->tag == DW_TAG_call_site
13557 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13558 continue;
13559
c38f313d
DJ
13560 /* For each CHILD_DIE, find the corresponding child of
13561 ORIGIN_DIE. If there is more than one layer of
13562 DW_AT_abstract_origin, follow them all; there shouldn't be,
13563 but GCC versions at least through 4.4 generate this (GCC PR
13564 40573). */
3ea89b92
PMR
13565 child_origin_die = child_die;
13566 child_origin_cu = cu;
c38f313d
DJ
13567 while (1)
13568 {
cd02d79d
PA
13569 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13570 child_origin_cu);
c38f313d
DJ
13571 if (attr == NULL)
13572 break;
cd02d79d
PA
13573 child_origin_die = follow_die_ref (child_origin_die, attr,
13574 &child_origin_cu);
c38f313d
DJ
13575 }
13576
d389af10
JK
13577 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13578 counterpart may exist. */
c38f313d 13579 if (child_origin_die != child_die)
d389af10 13580 {
edb3359d
DJ
13581 if (child_die->tag != child_origin_die->tag
13582 && !(child_die->tag == DW_TAG_inlined_subroutine
13583 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13584 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13585 "different tags"),
9d8780f0
SM
13586 sect_offset_str (child_die->sect_off),
13587 sect_offset_str (child_origin_die->sect_off));
c38f313d 13588 if (child_origin_die->parent != origin_die)
b98664d3 13589 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13590 "different parents"),
9d8780f0
SM
13591 sect_offset_str (child_die->sect_off),
13592 sect_offset_str (child_origin_die->sect_off));
c38f313d 13593 else
791afaa2 13594 offsets.push_back (child_origin_die->sect_off);
d389af10 13595 }
d389af10 13596 }
791afaa2
TT
13597 std::sort (offsets.begin (), offsets.end ());
13598 sect_offset *offsets_end = offsets.data () + offsets.size ();
13599 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13600 if (offsetp[-1] == *offsetp)
b98664d3 13601 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13602 "to DIE %s as their abstract origin"),
13603 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13604
791afaa2 13605 offsetp = offsets.data ();
d389af10
JK
13606 origin_child_die = origin_die->child;
13607 while (origin_child_die && origin_child_die->tag)
13608 {
13609 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13610 while (offsetp < offsets_end
9c541725 13611 && *offsetp < origin_child_die->sect_off)
d389af10 13612 offsetp++;
b64f50a1 13613 if (offsetp >= offsets_end
9c541725 13614 || *offsetp > origin_child_die->sect_off)
d389af10 13615 {
adde2bff
DE
13616 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13617 Check whether we're already processing ORIGIN_CHILD_DIE.
13618 This can happen with mutually referenced abstract_origins.
13619 PR 16581. */
13620 if (!origin_child_die->in_process)
13621 process_die (origin_child_die, origin_cu);
d389af10
JK
13622 }
13623 origin_child_die = sibling_die (origin_child_die);
13624 }
cd02d79d 13625 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13626
13627 if (cu != origin_cu)
13628 compute_delayed_physnames (origin_cu);
d389af10
JK
13629}
13630
c906108c 13631static void
e7c27a73 13632read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13633{
518817b3 13634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13635 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13636 struct context_stack *newobj;
c906108c
SS
13637 CORE_ADDR lowpc;
13638 CORE_ADDR highpc;
13639 struct die_info *child_die;
edb3359d 13640 struct attribute *attr, *call_line, *call_file;
15d034d0 13641 const char *name;
e142c38c 13642 CORE_ADDR baseaddr;
801e3a5b 13643 struct block *block;
edb3359d 13644 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13645 std::vector<struct symbol *> template_args;
34eaf542 13646 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13647
13648 if (inlined_func)
13649 {
13650 /* If we do not have call site information, we can't show the
13651 caller of this inlined function. That's too confusing, so
13652 only use the scope for local variables. */
13653 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13654 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13655 if (call_line == NULL || call_file == NULL)
13656 {
13657 read_lexical_block_scope (die, cu);
13658 return;
13659 }
13660 }
c906108c 13661
b3b3bada 13662 baseaddr = objfile->text_section_offset ();
e142c38c 13663
94af9270 13664 name = dwarf2_name (die, cu);
c906108c 13665
e8d05480
JB
13666 /* Ignore functions with missing or empty names. These are actually
13667 illegal according to the DWARF standard. */
13668 if (name == NULL)
13669 {
b98664d3 13670 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13671 sect_offset_str (die->sect_off));
e8d05480
JB
13672 return;
13673 }
13674
13675 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13676 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13677 <= PC_BOUNDS_INVALID)
e8d05480 13678 {
ae4d0c03
PM
13679 attr = dwarf2_attr (die, DW_AT_external, cu);
13680 if (!attr || !DW_UNSND (attr))
b98664d3 13681 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13682 "for subprogram DIE at %s"),
13683 sect_offset_str (die->sect_off));
e8d05480
JB
13684 return;
13685 }
c906108c 13686
3e29f34a
MR
13687 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13688 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13689
34eaf542
TT
13690 /* If we have any template arguments, then we must allocate a
13691 different sort of symbol. */
13692 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13693 {
13694 if (child_die->tag == DW_TAG_template_type_param
13695 || child_die->tag == DW_TAG_template_value_param)
13696 {
e623cf5d 13697 templ_func = allocate_template_symbol (objfile);
cf724bc9 13698 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13699 break;
13700 }
13701 }
13702
c24bdb02 13703 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13704 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13705 (struct symbol *) templ_func);
4c2df51b 13706
81873cc8 13707 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13708 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13709 cu->language);
13710
4cecd739
DJ
13711 /* If there is a location expression for DW_AT_frame_base, record
13712 it. */
e142c38c 13713 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13714 if (attr != nullptr)
fe978cb0 13715 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13716
63e43d3a
PMR
13717 /* If there is a location for the static link, record it. */
13718 newobj->static_link = NULL;
13719 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13720 if (attr != nullptr)
63e43d3a 13721 {
224c3ddb
SM
13722 newobj->static_link
13723 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13724 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13725 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13726 }
13727
c24bdb02 13728 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13729
639d11d3 13730 if (die->child != NULL)
c906108c 13731 {
639d11d3 13732 child_die = die->child;
c906108c
SS
13733 while (child_die && child_die->tag)
13734 {
34eaf542
TT
13735 if (child_die->tag == DW_TAG_template_type_param
13736 || child_die->tag == DW_TAG_template_value_param)
13737 {
13738 struct symbol *arg = new_symbol (child_die, NULL, cu);
13739
f1078f66 13740 if (arg != NULL)
2f4732b0 13741 template_args.push_back (arg);
34eaf542
TT
13742 }
13743 else
13744 process_die (child_die, cu);
c906108c
SS
13745 child_die = sibling_die (child_die);
13746 }
13747 }
13748
d389af10
JK
13749 inherit_abstract_dies (die, cu);
13750
4a811a97
UW
13751 /* If we have a DW_AT_specification, we might need to import using
13752 directives from the context of the specification DIE. See the
13753 comment in determine_prefix. */
13754 if (cu->language == language_cplus
13755 && dwarf2_attr (die, DW_AT_specification, cu))
13756 {
13757 struct dwarf2_cu *spec_cu = cu;
13758 struct die_info *spec_die = die_specification (die, &spec_cu);
13759
13760 while (spec_die)
13761 {
13762 child_die = spec_die->child;
13763 while (child_die && child_die->tag)
13764 {
13765 if (child_die->tag == DW_TAG_imported_module)
13766 process_die (child_die, spec_cu);
13767 child_die = sibling_die (child_die);
13768 }
13769
13770 /* In some cases, GCC generates specification DIEs that
13771 themselves contain DW_AT_specification attributes. */
13772 spec_die = die_specification (spec_die, &spec_cu);
13773 }
13774 }
13775
c24bdb02 13776 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13777 /* Make a block for the local symbols within. */
c24bdb02 13778 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13779 cstk.static_link, lowpc, highpc);
801e3a5b 13780
df8a16a1 13781 /* For C++, set the block's scope. */
45280282
IB
13782 if ((cu->language == language_cplus
13783 || cu->language == language_fortran
c44af4eb
TT
13784 || cu->language == language_d
13785 || cu->language == language_rust)
4d4ec4e5 13786 && cu->processing_has_namespace_info)
195a3f6c
TT
13787 block_set_scope (block, determine_prefix (die, cu),
13788 &objfile->objfile_obstack);
df8a16a1 13789
801e3a5b
JB
13790 /* If we have address ranges, record them. */
13791 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13792
a60f3166 13793 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13794
34eaf542 13795 /* Attach template arguments to function. */
2f4732b0 13796 if (!template_args.empty ())
34eaf542
TT
13797 {
13798 gdb_assert (templ_func != NULL);
13799
2f4732b0 13800 templ_func->n_template_arguments = template_args.size ();
34eaf542 13801 templ_func->template_arguments
8d749320
SM
13802 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13803 templ_func->n_template_arguments);
34eaf542 13804 memcpy (templ_func->template_arguments,
2f4732b0 13805 template_args.data (),
34eaf542 13806 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13807
13808 /* Make sure that the symtab is set on the new symbols. Even
13809 though they don't appear in this symtab directly, other parts
13810 of gdb assume that symbols do, and this is reasonably
13811 true. */
8634679f 13812 for (symbol *sym : template_args)
3e1d3d8c 13813 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13814 }
13815
208d8187
JB
13816 /* In C++, we can have functions nested inside functions (e.g., when
13817 a function declares a class that has methods). This means that
13818 when we finish processing a function scope, we may need to go
13819 back to building a containing block's symbol lists. */
c24bdb02
KS
13820 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13821 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13822
921e78cf
JB
13823 /* If we've finished processing a top-level function, subsequent
13824 symbols go in the file symbol list. */
c24bdb02
KS
13825 if (cu->get_builder ()->outermost_context_p ())
13826 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13827}
13828
13829/* Process all the DIES contained within a lexical block scope. Start
13830 a new scope, process the dies, and then close the scope. */
13831
13832static void
e7c27a73 13833read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13834{
518817b3 13835 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13836 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13837 CORE_ADDR lowpc, highpc;
13838 struct die_info *child_die;
e142c38c
DJ
13839 CORE_ADDR baseaddr;
13840
b3b3bada 13841 baseaddr = objfile->text_section_offset ();
c906108c
SS
13842
13843 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13844 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13845 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13846 be nasty. Might be easier to properly extend generic blocks to
af34e669 13847 describe ranges. */
e385593e
JK
13848 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13849 {
13850 case PC_BOUNDS_NOT_PRESENT:
13851 /* DW_TAG_lexical_block has no attributes, process its children as if
13852 there was no wrapping by that DW_TAG_lexical_block.
13853 GCC does no longer produces such DWARF since GCC r224161. */
13854 for (child_die = die->child;
13855 child_die != NULL && child_die->tag;
13856 child_die = sibling_die (child_die))
13857 process_die (child_die, cu);
13858 return;
13859 case PC_BOUNDS_INVALID:
13860 return;
13861 }
3e29f34a
MR
13862 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13863 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13864
c24bdb02 13865 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13866 if (die->child != NULL)
c906108c 13867 {
639d11d3 13868 child_die = die->child;
c906108c
SS
13869 while (child_die && child_die->tag)
13870 {
e7c27a73 13871 process_die (child_die, cu);
c906108c
SS
13872 child_die = sibling_die (child_die);
13873 }
13874 }
3ea89b92 13875 inherit_abstract_dies (die, cu);
c24bdb02 13876 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13877
c24bdb02
KS
13878 if (*cu->get_builder ()->get_local_symbols () != NULL
13879 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13880 {
801e3a5b 13881 struct block *block
c24bdb02 13882 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13883 cstk.start_addr, highpc);
801e3a5b
JB
13884
13885 /* Note that recording ranges after traversing children, as we
13886 do here, means that recording a parent's ranges entails
13887 walking across all its children's ranges as they appear in
13888 the address map, which is quadratic behavior.
13889
13890 It would be nicer to record the parent's ranges before
13891 traversing its children, simply overriding whatever you find
13892 there. But since we don't even decide whether to create a
13893 block until after we've traversed its children, that's hard
13894 to do. */
13895 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13896 }
c24bdb02
KS
13897 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13898 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13899}
13900
216f72a1 13901/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13902
13903static void
13904read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13905{
518817b3 13906 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13908 CORE_ADDR pc, baseaddr;
13909 struct attribute *attr;
13910 struct call_site *call_site, call_site_local;
13911 void **slot;
13912 int nparams;
13913 struct die_info *child_die;
13914
b3b3bada 13915 baseaddr = objfile->text_section_offset ();
96408a79 13916
216f72a1
JK
13917 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13918 if (attr == NULL)
13919 {
13920 /* This was a pre-DWARF-5 GNU extension alias
13921 for DW_AT_call_return_pc. */
13922 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13923 }
96408a79
SA
13924 if (!attr)
13925 {
b98664d3 13926 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13927 "DIE %s [in module %s]"),
13928 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13929 return;
13930 }
31aa7e4e 13931 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13932 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13933
13934 if (cu->call_site_htab == NULL)
13935 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13936 NULL, &objfile->objfile_obstack,
13937 hashtab_obstack_allocate, NULL);
13938 call_site_local.pc = pc;
13939 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13940 if (*slot != NULL)
13941 {
b98664d3 13942 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13943 "DIE %s [in module %s]"),
13944 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13945 objfile_name (objfile));
96408a79
SA
13946 return;
13947 }
13948
13949 /* Count parameters at the caller. */
13950
13951 nparams = 0;
13952 for (child_die = die->child; child_die && child_die->tag;
13953 child_die = sibling_die (child_die))
13954 {
216f72a1
JK
13955 if (child_die->tag != DW_TAG_call_site_parameter
13956 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13957 {
b98664d3 13958 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13959 "DW_TAG_call_site child DIE %s [in module %s]"),
13960 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13961 objfile_name (objfile));
96408a79
SA
13962 continue;
13963 }
13964
13965 nparams++;
13966 }
13967
224c3ddb
SM
13968 call_site
13969 = ((struct call_site *)
13970 obstack_alloc (&objfile->objfile_obstack,
13971 sizeof (*call_site)
13972 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13973 *slot = call_site;
13974 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13975 call_site->pc = pc;
13976
216f72a1
JK
13977 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13978 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13979 {
13980 struct die_info *func_die;
13981
13982 /* Skip also over DW_TAG_inlined_subroutine. */
13983 for (func_die = die->parent;
13984 func_die && func_die->tag != DW_TAG_subprogram
13985 && func_die->tag != DW_TAG_subroutine_type;
13986 func_die = func_die->parent);
13987
216f72a1
JK
13988 /* DW_AT_call_all_calls is a superset
13989 of DW_AT_call_all_tail_calls. */
96408a79 13990 if (func_die
216f72a1 13991 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13992 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13993 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13994 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13995 {
13996 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13997 not complete. But keep CALL_SITE for look ups via call_site_htab,
13998 both the initial caller containing the real return address PC and
13999 the final callee containing the current PC of a chain of tail
14000 calls do not need to have the tail call list complete. But any
14001 function candidate for a virtual tail call frame searched via
14002 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14003 determined unambiguously. */
14004 }
14005 else
14006 {
14007 struct type *func_type = NULL;
14008
14009 if (func_die)
14010 func_type = get_die_type (func_die, cu);
14011 if (func_type != NULL)
14012 {
14013 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14014
14015 /* Enlist this call site to the function. */
14016 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14017 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14018 }
14019 else
b98664d3 14020 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14021 "DIE %s [in module %s]"),
14022 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14023 }
14024 }
14025
216f72a1
JK
14026 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14027 if (attr == NULL)
14028 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14029 if (attr == NULL)
14030 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14031 if (attr == NULL)
216f72a1
JK
14032 {
14033 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14034 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14035 }
96408a79
SA
14036 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14037 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14038 /* Keep NULL DWARF_BLOCK. */;
14039 else if (attr_form_is_block (attr))
14040 {
14041 struct dwarf2_locexpr_baton *dlbaton;
14042
8d749320 14043 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14044 dlbaton->data = DW_BLOCK (attr)->data;
14045 dlbaton->size = DW_BLOCK (attr)->size;
14046 dlbaton->per_cu = cu->per_cu;
14047
14048 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14049 }
7771576e 14050 else if (attr_form_is_ref (attr))
96408a79 14051 {
96408a79
SA
14052 struct dwarf2_cu *target_cu = cu;
14053 struct die_info *target_die;
14054
ac9ec31b 14055 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14056 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14057 if (die_is_declaration (target_die, target_cu))
14058 {
7d45c7c3 14059 const char *target_physname;
9112db09
JK
14060
14061 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14062 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14063 if (target_physname == NULL)
9112db09 14064 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14065 if (target_physname == NULL)
b98664d3 14066 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14067 "physname, for referencing DIE %s [in module %s]"),
14068 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14069 else
7d455152 14070 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14071 }
14072 else
14073 {
14074 CORE_ADDR lowpc;
14075
14076 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14077 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14078 <= PC_BOUNDS_INVALID)
b98664d3 14079 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14080 "low pc, for referencing DIE %s [in module %s]"),
14081 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14082 else
3e29f34a
MR
14083 {
14084 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14085 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14086 }
96408a79
SA
14087 }
14088 }
14089 else
b98664d3 14090 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14091 "block nor reference, for DIE %s [in module %s]"),
14092 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14093
14094 call_site->per_cu = cu->per_cu;
14095
14096 for (child_die = die->child;
14097 child_die && child_die->tag;
14098 child_die = sibling_die (child_die))
14099 {
96408a79 14100 struct call_site_parameter *parameter;
1788b2d3 14101 struct attribute *loc, *origin;
96408a79 14102
216f72a1
JK
14103 if (child_die->tag != DW_TAG_call_site_parameter
14104 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14105 {
14106 /* Already printed the complaint above. */
14107 continue;
14108 }
14109
14110 gdb_assert (call_site->parameter_count < nparams);
14111 parameter = &call_site->parameter[call_site->parameter_count];
14112
1788b2d3
JK
14113 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14114 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14115 register is contained in DW_AT_call_value. */
96408a79 14116
24c5c679 14117 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14118 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14119 if (origin == NULL)
14120 {
14121 /* This was a pre-DWARF-5 GNU extension alias
14122 for DW_AT_call_parameter. */
14123 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14124 }
7771576e 14125 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14126 {
1788b2d3 14127 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14128
14129 sect_offset sect_off
14130 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14131 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14132 {
14133 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14134 binding can be done only inside one CU. Such referenced DIE
14135 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14136 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14137 "DW_TAG_call_site child DIE %s [in module %s]"),
14138 sect_offset_str (child_die->sect_off),
9c541725 14139 objfile_name (objfile));
d76b7dbc
JK
14140 continue;
14141 }
9c541725
PA
14142 parameter->u.param_cu_off
14143 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14144 }
14145 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14146 {
b98664d3 14147 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14148 "DW_TAG_call_site child DIE %s [in module %s]"),
14149 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14150 continue;
14151 }
24c5c679 14152 else
96408a79 14153 {
24c5c679
JK
14154 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14155 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14156 if (parameter->u.dwarf_reg != -1)
14157 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14158 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14159 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14160 &parameter->u.fb_offset))
14161 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14162 else
14163 {
b98664d3 14164 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14165 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14166 "DW_TAG_call_site child DIE %s "
24c5c679 14167 "[in module %s]"),
9d8780f0 14168 sect_offset_str (child_die->sect_off),
9c541725 14169 objfile_name (objfile));
24c5c679
JK
14170 continue;
14171 }
96408a79
SA
14172 }
14173
216f72a1
JK
14174 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14175 if (attr == NULL)
14176 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14177 if (!attr_form_is_block (attr))
14178 {
b98664d3 14179 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14180 "DW_TAG_call_site child DIE %s [in module %s]"),
14181 sect_offset_str (child_die->sect_off),
9c541725 14182 objfile_name (objfile));
96408a79
SA
14183 continue;
14184 }
14185 parameter->value = DW_BLOCK (attr)->data;
14186 parameter->value_size = DW_BLOCK (attr)->size;
14187
14188 /* Parameters are not pre-cleared by memset above. */
14189 parameter->data_value = NULL;
14190 parameter->data_value_size = 0;
14191 call_site->parameter_count++;
14192
216f72a1
JK
14193 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14194 if (attr == NULL)
14195 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14196 if (attr != nullptr)
96408a79
SA
14197 {
14198 if (!attr_form_is_block (attr))
b98664d3 14199 complaint (_("No DW_FORM_block* DW_AT_call_data_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 else
14204 {
14205 parameter->data_value = DW_BLOCK (attr)->data;
14206 parameter->data_value_size = DW_BLOCK (attr)->size;
14207 }
14208 }
14209 }
14210}
14211
71a3c369
TT
14212/* Helper function for read_variable. If DIE represents a virtual
14213 table, then return the type of the concrete object that is
14214 associated with the virtual table. Otherwise, return NULL. */
14215
14216static struct type *
14217rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14218{
14219 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14220 if (attr == NULL)
14221 return NULL;
14222
14223 /* Find the type DIE. */
14224 struct die_info *type_die = NULL;
14225 struct dwarf2_cu *type_cu = cu;
14226
14227 if (attr_form_is_ref (attr))
14228 type_die = follow_die_ref (die, attr, &type_cu);
14229 if (type_die == NULL)
14230 return NULL;
14231
14232 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14233 return NULL;
14234 return die_containing_type (type_die, type_cu);
14235}
14236
14237/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14238
14239static void
14240read_variable (struct die_info *die, struct dwarf2_cu *cu)
14241{
14242 struct rust_vtable_symbol *storage = NULL;
14243
14244 if (cu->language == language_rust)
14245 {
14246 struct type *containing_type = rust_containing_type (die, cu);
14247
14248 if (containing_type != NULL)
14249 {
518817b3 14250 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 14251
468c0cbb 14252 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
14253 initialize_objfile_symbol (storage);
14254 storage->concrete_type = containing_type;
cf724bc9 14255 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14256 }
14257 }
14258
e4a62c65
TV
14259 struct symbol *res = new_symbol (die, NULL, cu, storage);
14260 struct attribute *abstract_origin
14261 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14262 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14263 if (res == NULL && loc && abstract_origin)
14264 {
14265 /* We have a variable without a name, but with a location and an abstract
14266 origin. This may be a concrete instance of an abstract variable
14267 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14268 later. */
14269 struct dwarf2_cu *origin_cu = cu;
14270 struct die_info *origin_die
14271 = follow_die_ref (die, abstract_origin, &origin_cu);
14272 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14273 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14274 }
71a3c369
TT
14275}
14276
43988095
JK
14277/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14278 reading .debug_rnglists.
14279 Callback's type should be:
14280 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14281 Return true if the attributes are present and valid, otherwise,
14282 return false. */
14283
14284template <typename Callback>
14285static bool
14286dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14287 Callback &&callback)
14288{
ed2dc618 14289 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14290 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14291 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14292 bfd *obfd = objfile->obfd;
43988095
JK
14293 /* Base address selection entry. */
14294 CORE_ADDR base;
14295 int found_base;
43988095 14296 const gdb_byte *buffer;
43988095
JK
14297 CORE_ADDR baseaddr;
14298 bool overflow = false;
14299
14300 found_base = cu->base_known;
14301 base = cu->base_address;
14302
14303 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14304 if (offset >= dwarf2_per_objfile->rnglists.size)
14305 {
b98664d3 14306 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14307 offset);
14308 return false;
14309 }
14310 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14311
b3b3bada 14312 baseaddr = objfile->text_section_offset ();
43988095
JK
14313
14314 while (1)
14315 {
7814882a
JK
14316 /* Initialize it due to a false compiler warning. */
14317 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14318 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14319 + dwarf2_per_objfile->rnglists.size);
14320 unsigned int bytes_read;
14321
14322 if (buffer == buf_end)
14323 {
14324 overflow = true;
14325 break;
14326 }
14327 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14328 switch (rlet)
14329 {
14330 case DW_RLE_end_of_list:
14331 break;
14332 case DW_RLE_base_address:
14333 if (buffer + cu->header.addr_size > buf_end)
14334 {
14335 overflow = true;
14336 break;
14337 }
14338 base = read_address (obfd, buffer, cu, &bytes_read);
14339 found_base = 1;
14340 buffer += bytes_read;
14341 break;
14342 case DW_RLE_start_length:
14343 if (buffer + cu->header.addr_size > buf_end)
14344 {
14345 overflow = true;
14346 break;
14347 }
14348 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14349 buffer += bytes_read;
14350 range_end = (range_beginning
14351 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14352 buffer += bytes_read;
14353 if (buffer > buf_end)
14354 {
14355 overflow = true;
14356 break;
14357 }
14358 break;
14359 case DW_RLE_offset_pair:
14360 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14361 buffer += bytes_read;
14362 if (buffer > buf_end)
14363 {
14364 overflow = true;
14365 break;
14366 }
14367 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14368 buffer += bytes_read;
14369 if (buffer > buf_end)
14370 {
14371 overflow = true;
14372 break;
14373 }
14374 break;
14375 case DW_RLE_start_end:
14376 if (buffer + 2 * cu->header.addr_size > buf_end)
14377 {
14378 overflow = true;
14379 break;
14380 }
14381 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14382 buffer += bytes_read;
14383 range_end = read_address (obfd, buffer, cu, &bytes_read);
14384 buffer += bytes_read;
14385 break;
14386 default:
b98664d3 14387 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14388 return false;
14389 }
14390 if (rlet == DW_RLE_end_of_list || overflow)
14391 break;
14392 if (rlet == DW_RLE_base_address)
14393 continue;
14394
14395 if (!found_base)
14396 {
14397 /* We have no valid base address for the ranges
14398 data. */
b98664d3 14399 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14400 return false;
14401 }
14402
14403 if (range_beginning > range_end)
14404 {
14405 /* Inverted range entries are invalid. */
b98664d3 14406 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14407 return false;
14408 }
14409
14410 /* Empty range entries have no effect. */
14411 if (range_beginning == range_end)
14412 continue;
14413
14414 range_beginning += base;
14415 range_end += base;
14416
14417 /* A not-uncommon case of bad debug info.
14418 Don't pollute the addrmap with bad data. */
14419 if (range_beginning + baseaddr == 0
14420 && !dwarf2_per_objfile->has_section_at_zero)
14421 {
b98664d3 14422 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14423 " [in module %s]"), objfile_name (objfile));
14424 continue;
14425 }
14426
14427 callback (range_beginning, range_end);
14428 }
14429
14430 if (overflow)
14431 {
b98664d3 14432 complaint (_("Offset %d is not terminated "
43988095
JK
14433 "for DW_AT_ranges attribute"),
14434 offset);
14435 return false;
14436 }
14437
14438 return true;
14439}
14440
14441/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14442 Callback's type should be:
14443 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14444 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14445
43988095 14446template <typename Callback>
43039443 14447static int
5f46c5a5 14448dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14449 Callback &&callback)
43039443 14450{
ed2dc618 14451 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14452 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14453 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14454 struct comp_unit_head *cu_header = &cu->header;
14455 bfd *obfd = objfile->obfd;
14456 unsigned int addr_size = cu_header->addr_size;
14457 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14458 /* Base address selection entry. */
14459 CORE_ADDR base;
14460 int found_base;
14461 unsigned int dummy;
d521ce57 14462 const gdb_byte *buffer;
ff013f42 14463 CORE_ADDR baseaddr;
43039443 14464
43988095
JK
14465 if (cu_header->version >= 5)
14466 return dwarf2_rnglists_process (offset, cu, callback);
14467
d00adf39
DE
14468 found_base = cu->base_known;
14469 base = cu->base_address;
43039443 14470
be391dca 14471 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14472 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14473 {
b98664d3 14474 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14475 offset);
14476 return 0;
14477 }
dce234bc 14478 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14479
b3b3bada 14480 baseaddr = objfile->text_section_offset ();
ff013f42 14481
43039443
JK
14482 while (1)
14483 {
14484 CORE_ADDR range_beginning, range_end;
14485
14486 range_beginning = read_address (obfd, buffer, cu, &dummy);
14487 buffer += addr_size;
14488 range_end = read_address (obfd, buffer, cu, &dummy);
14489 buffer += addr_size;
14490 offset += 2 * addr_size;
14491
14492 /* An end of list marker is a pair of zero addresses. */
14493 if (range_beginning == 0 && range_end == 0)
14494 /* Found the end of list entry. */
14495 break;
14496
14497 /* Each base address selection entry is a pair of 2 values.
14498 The first is the largest possible address, the second is
14499 the base address. Check for a base address here. */
14500 if ((range_beginning & mask) == mask)
14501 {
28d2bfb9
AB
14502 /* If we found the largest possible address, then we already
14503 have the base address in range_end. */
14504 base = range_end;
43039443
JK
14505 found_base = 1;
14506 continue;
14507 }
14508
14509 if (!found_base)
14510 {
14511 /* We have no valid base address for the ranges
14512 data. */
b98664d3 14513 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14514 return 0;
14515 }
14516
9277c30c
UW
14517 if (range_beginning > range_end)
14518 {
14519 /* Inverted range entries are invalid. */
b98664d3 14520 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14521 return 0;
14522 }
14523
14524 /* Empty range entries have no effect. */
14525 if (range_beginning == range_end)
14526 continue;
14527
43039443
JK
14528 range_beginning += base;
14529 range_end += base;
14530
01093045
DE
14531 /* A not-uncommon case of bad debug info.
14532 Don't pollute the addrmap with bad data. */
14533 if (range_beginning + baseaddr == 0
14534 && !dwarf2_per_objfile->has_section_at_zero)
14535 {
b98664d3 14536 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14537 " [in module %s]"), objfile_name (objfile));
01093045
DE
14538 continue;
14539 }
14540
5f46c5a5
JK
14541 callback (range_beginning, range_end);
14542 }
14543
14544 return 1;
14545}
14546
14547/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14548 Return 1 if the attributes are present and valid, otherwise, return 0.
14549 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14550
14551static int
14552dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14553 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14554 dwarf2_psymtab *ranges_pst)
5f46c5a5 14555{
518817b3 14556 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 14557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 14558 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14559 int low_set = 0;
14560 CORE_ADDR low = 0;
14561 CORE_ADDR high = 0;
14562 int retval;
14563
14564 retval = dwarf2_ranges_process (offset, cu,
14565 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14566 {
9277c30c 14567 if (ranges_pst != NULL)
3e29f34a
MR
14568 {
14569 CORE_ADDR lowpc;
14570 CORE_ADDR highpc;
14571
79748972
TT
14572 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14573 range_beginning + baseaddr)
14574 - baseaddr);
14575 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14576 range_end + baseaddr)
14577 - baseaddr);
d320c2b5
TT
14578 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14579 lowpc, highpc - 1, ranges_pst);
3e29f34a 14580 }
ff013f42 14581
43039443
JK
14582 /* FIXME: This is recording everything as a low-high
14583 segment of consecutive addresses. We should have a
14584 data structure for discontiguous block ranges
14585 instead. */
14586 if (! low_set)
14587 {
14588 low = range_beginning;
14589 high = range_end;
14590 low_set = 1;
14591 }
14592 else
14593 {
14594 if (range_beginning < low)
14595 low = range_beginning;
14596 if (range_end > high)
14597 high = range_end;
14598 }
5f46c5a5
JK
14599 });
14600 if (!retval)
14601 return 0;
43039443
JK
14602
14603 if (! low_set)
14604 /* If the first entry is an end-of-list marker, the range
14605 describes an empty scope, i.e. no instructions. */
14606 return 0;
14607
14608 if (low_return)
14609 *low_return = low;
14610 if (high_return)
14611 *high_return = high;
14612 return 1;
14613}
14614
3a2b436a
JK
14615/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14616 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14617 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14618
3a2b436a 14619static enum pc_bounds_kind
af34e669 14620dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14621 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14622 dwarf2_psymtab *pst)
c906108c 14623{
518817b3
SM
14624 struct dwarf2_per_objfile *dwarf2_per_objfile
14625 = cu->per_cu->dwarf2_per_objfile;
c906108c 14626 struct attribute *attr;
91da1414 14627 struct attribute *attr_high;
af34e669
DJ
14628 CORE_ADDR low = 0;
14629 CORE_ADDR high = 0;
e385593e 14630 enum pc_bounds_kind ret;
c906108c 14631
91da1414
MW
14632 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14633 if (attr_high)
af34e669 14634 {
e142c38c 14635 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14636 if (attr != nullptr)
91da1414 14637 {
31aa7e4e
JB
14638 low = attr_value_as_address (attr);
14639 high = attr_value_as_address (attr_high);
14640 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14641 high += low;
91da1414 14642 }
af34e669
DJ
14643 else
14644 /* Found high w/o low attribute. */
e385593e 14645 return PC_BOUNDS_INVALID;
af34e669
DJ
14646
14647 /* Found consecutive range of addresses. */
3a2b436a 14648 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14649 }
c906108c 14650 else
af34e669 14651 {
e142c38c 14652 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14653 if (attr != NULL)
14654 {
18a8505e 14655 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14656 We take advantage of the fact that DW_AT_ranges does not appear
14657 in DW_TAG_compile_unit of DWO files. */
14658 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14659 unsigned int ranges_offset = (DW_UNSND (attr)
14660 + (need_ranges_base
14661 ? cu->ranges_base
14662 : 0));
2e3cf129 14663
af34e669 14664 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14665 .debug_ranges section. */
2e3cf129 14666 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14667 return PC_BOUNDS_INVALID;
43039443 14668 /* Found discontinuous range of addresses. */
3a2b436a 14669 ret = PC_BOUNDS_RANGES;
af34e669 14670 }
e385593e
JK
14671 else
14672 return PC_BOUNDS_NOT_PRESENT;
af34e669 14673 }
c906108c 14674
48fbe735 14675 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14676 if (high <= low)
e385593e 14677 return PC_BOUNDS_INVALID;
c906108c
SS
14678
14679 /* When using the GNU linker, .gnu.linkonce. sections are used to
14680 eliminate duplicate copies of functions and vtables and such.
14681 The linker will arbitrarily choose one and discard the others.
14682 The AT_*_pc values for such functions refer to local labels in
14683 these sections. If the section from that file was discarded, the
14684 labels are not in the output, so the relocs get a value of 0.
14685 If this is a discarded function, mark the pc bounds as invalid,
14686 so that GDB will ignore it. */
72dca2f5 14687 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14688 return PC_BOUNDS_INVALID;
c906108c
SS
14689
14690 *lowpc = low;
96408a79
SA
14691 if (highpc)
14692 *highpc = high;
af34e669 14693 return ret;
c906108c
SS
14694}
14695
b084d499
JB
14696/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14697 its low and high PC addresses. Do nothing if these addresses could not
14698 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14699 and HIGHPC to the high address if greater than HIGHPC. */
14700
14701static void
14702dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14703 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14704 struct dwarf2_cu *cu)
14705{
14706 CORE_ADDR low, high;
14707 struct die_info *child = die->child;
14708
e385593e 14709 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14710 {
325fac50
PA
14711 *lowpc = std::min (*lowpc, low);
14712 *highpc = std::max (*highpc, high);
b084d499
JB
14713 }
14714
14715 /* If the language does not allow nested subprograms (either inside
14716 subprograms or lexical blocks), we're done. */
14717 if (cu->language != language_ada)
14718 return;
6e70227d 14719
b084d499
JB
14720 /* Check all the children of the given DIE. If it contains nested
14721 subprograms, then check their pc bounds. Likewise, we need to
14722 check lexical blocks as well, as they may also contain subprogram
14723 definitions. */
14724 while (child && child->tag)
14725 {
14726 if (child->tag == DW_TAG_subprogram
14727 || child->tag == DW_TAG_lexical_block)
14728 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14729 child = sibling_die (child);
14730 }
14731}
14732
fae299cd
DC
14733/* Get the low and high pc's represented by the scope DIE, and store
14734 them in *LOWPC and *HIGHPC. If the correct values can't be
14735 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14736
14737static void
14738get_scope_pc_bounds (struct die_info *die,
14739 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14740 struct dwarf2_cu *cu)
14741{
14742 CORE_ADDR best_low = (CORE_ADDR) -1;
14743 CORE_ADDR best_high = (CORE_ADDR) 0;
14744 CORE_ADDR current_low, current_high;
14745
3a2b436a 14746 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14747 >= PC_BOUNDS_RANGES)
fae299cd
DC
14748 {
14749 best_low = current_low;
14750 best_high = current_high;
14751 }
14752 else
14753 {
14754 struct die_info *child = die->child;
14755
14756 while (child && child->tag)
14757 {
14758 switch (child->tag) {
14759 case DW_TAG_subprogram:
b084d499 14760 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14761 break;
14762 case DW_TAG_namespace:
f55ee35c 14763 case DW_TAG_module:
fae299cd
DC
14764 /* FIXME: carlton/2004-01-16: Should we do this for
14765 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14766 that current GCC's always emit the DIEs corresponding
14767 to definitions of methods of classes as children of a
14768 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14769 the DIEs giving the declarations, which could be
14770 anywhere). But I don't see any reason why the
14771 standards says that they have to be there. */
14772 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14773
14774 if (current_low != ((CORE_ADDR) -1))
14775 {
325fac50
PA
14776 best_low = std::min (best_low, current_low);
14777 best_high = std::max (best_high, current_high);
fae299cd
DC
14778 }
14779 break;
14780 default:
0963b4bd 14781 /* Ignore. */
fae299cd
DC
14782 break;
14783 }
14784
14785 child = sibling_die (child);
14786 }
14787 }
14788
14789 *lowpc = best_low;
14790 *highpc = best_high;
14791}
14792
801e3a5b
JB
14793/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14794 in DIE. */
380bca97 14795
801e3a5b
JB
14796static void
14797dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14798 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14799{
518817b3 14800 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14801 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14802 struct attribute *attr;
91da1414 14803 struct attribute *attr_high;
801e3a5b 14804
91da1414
MW
14805 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14806 if (attr_high)
801e3a5b 14807 {
801e3a5b 14808 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14809 if (attr != nullptr)
801e3a5b 14810 {
31aa7e4e
JB
14811 CORE_ADDR low = attr_value_as_address (attr);
14812 CORE_ADDR high = attr_value_as_address (attr_high);
14813
14814 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14815 high += low;
9a619af0 14816
3e29f34a
MR
14817 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14818 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14819 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14820 }
14821 }
14822
14823 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14824 if (attr != nullptr)
801e3a5b 14825 {
18a8505e 14826 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14827 We take advantage of the fact that DW_AT_ranges does not appear
14828 in DW_TAG_compile_unit of DWO files. */
14829 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14830
14831 /* The value of the DW_AT_ranges attribute is the offset of the
14832 address range list in the .debug_ranges section. */
ab435259
DE
14833 unsigned long offset = (DW_UNSND (attr)
14834 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14835
2d5f09ec 14836 std::vector<blockrange> blockvec;
5f46c5a5
JK
14837 dwarf2_ranges_process (offset, cu,
14838 [&] (CORE_ADDR start, CORE_ADDR end)
14839 {
58fdfd2c
JK
14840 start += baseaddr;
14841 end += baseaddr;
5f46c5a5
JK
14842 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14843 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14844 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14845 blockvec.emplace_back (start, end);
5f46c5a5 14846 });
2d5f09ec
KB
14847
14848 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14849 }
14850}
14851
685b1105
JK
14852/* Check whether the producer field indicates either of GCC < 4.6, or the
14853 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14854
685b1105
JK
14855static void
14856check_producer (struct dwarf2_cu *cu)
60d5a603 14857{
38360086 14858 int major, minor;
60d5a603
JK
14859
14860 if (cu->producer == NULL)
14861 {
14862 /* For unknown compilers expect their behavior is DWARF version
14863 compliant.
14864
14865 GCC started to support .debug_types sections by -gdwarf-4 since
14866 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14867 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14868 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14869 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14870 }
b1ffba5a 14871 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14872 {
38360086
MW
14873 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14874 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14875 }
5230b05a 14876 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14877 {
14878 cu->producer_is_icc = true;
14879 cu->producer_is_icc_lt_14 = major < 14;
14880 }
c258c396
JD
14881 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14882 cu->producer_is_codewarrior = true;
685b1105
JK
14883 else
14884 {
14885 /* For other non-GCC compilers, expect their behavior is DWARF version
14886 compliant. */
60d5a603
JK
14887 }
14888
9068261f 14889 cu->checked_producer = true;
685b1105 14890}
ba919b58 14891
685b1105
JK
14892/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14893 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14894 during 4.6.0 experimental. */
14895
9068261f 14896static bool
685b1105
JK
14897producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14898{
14899 if (!cu->checked_producer)
14900 check_producer (cu);
14901
14902 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14903}
14904
c258c396
JD
14905
14906/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14907 with incorrect is_stmt attributes. */
14908
14909static bool
14910producer_is_codewarrior (struct dwarf2_cu *cu)
14911{
14912 if (!cu->checked_producer)
14913 check_producer (cu);
14914
14915 return cu->producer_is_codewarrior;
14916}
14917
405feb71 14918/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14919 DW_AT_accessibility. */
14920
14921static enum dwarf_access_attribute
14922dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14923{
14924 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14925 {
14926 /* The default DWARF 2 accessibility for members is public, the default
14927 accessibility for inheritance is private. */
14928
14929 if (die->tag != DW_TAG_inheritance)
14930 return DW_ACCESS_public;
14931 else
14932 return DW_ACCESS_private;
14933 }
14934 else
14935 {
14936 /* DWARF 3+ defines the default accessibility a different way. The same
14937 rules apply now for DW_TAG_inheritance as for the members and it only
14938 depends on the container kind. */
14939
14940 if (die->parent->tag == DW_TAG_class_type)
14941 return DW_ACCESS_private;
14942 else
14943 return DW_ACCESS_public;
14944 }
14945}
14946
74ac6d43
TT
14947/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14948 offset. If the attribute was not found return 0, otherwise return
14949 1. If it was found but could not properly be handled, set *OFFSET
14950 to 0. */
14951
14952static int
14953handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14954 LONGEST *offset)
14955{
14956 struct attribute *attr;
14957
14958 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14959 if (attr != NULL)
14960 {
14961 *offset = 0;
14962
14963 /* Note that we do not check for a section offset first here.
14964 This is because DW_AT_data_member_location is new in DWARF 4,
14965 so if we see it, we can assume that a constant form is really
14966 a constant and not a section offset. */
14967 if (attr_form_is_constant (attr))
14968 *offset = dwarf2_get_attr_constant_value (attr, 0);
14969 else if (attr_form_is_section_offset (attr))
14970 dwarf2_complex_location_expr_complaint ();
14971 else if (attr_form_is_block (attr))
14972 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14973 else
14974 dwarf2_complex_location_expr_complaint ();
14975
14976 return 1;
14977 }
14978
14979 return 0;
14980}
14981
c906108c
SS
14982/* Add an aggregate field to the field list. */
14983
14984static void
107d2387 14985dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14986 struct dwarf2_cu *cu)
6e70227d 14987{
518817b3 14988 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14989 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14990 struct nextfield *new_field;
14991 struct attribute *attr;
14992 struct field *fp;
15d034d0 14993 const char *fieldname = "";
c906108c 14994
7d0ccb61
DJ
14995 if (die->tag == DW_TAG_inheritance)
14996 {
be2daae6
TT
14997 fip->baseclasses.emplace_back ();
14998 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14999 }
15000 else
15001 {
be2daae6
TT
15002 fip->fields.emplace_back ();
15003 new_field = &fip->fields.back ();
7d0ccb61 15004 }
be2daae6 15005
c906108c
SS
15006 fip->nfields++;
15007
e142c38c 15008 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15009 if (attr != nullptr)
c906108c 15010 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15011 else
15012 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15013 if (new_field->accessibility != DW_ACCESS_public)
15014 fip->non_public_fields = 1;
60d5a603 15015
e142c38c 15016 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15017 if (attr != nullptr)
c906108c 15018 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15019 else
15020 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15021
15022 fp = &new_field->field;
a9a9bd0f 15023
e142c38c 15024 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15025 {
74ac6d43
TT
15026 LONGEST offset;
15027
a9a9bd0f 15028 /* Data member other than a C++ static data member. */
6e70227d 15029
c906108c 15030 /* Get type of field. */
e7c27a73 15031 fp->type = die_type (die, cu);
c906108c 15032
d6a843b5 15033 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15034
c906108c 15035 /* Get bit size of field (zero if none). */
e142c38c 15036 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15037 if (attr != nullptr)
c906108c
SS
15038 {
15039 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15040 }
15041 else
15042 {
15043 FIELD_BITSIZE (*fp) = 0;
15044 }
15045
15046 /* Get bit offset of field. */
74ac6d43
TT
15047 if (handle_data_member_location (die, cu, &offset))
15048 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15049 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15050 if (attr != nullptr)
c906108c 15051 {
d5a22e77 15052 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15053 {
15054 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15055 additional bit offset from the MSB of the containing
15056 anonymous object to the MSB of the field. We don't
15057 have to do anything special since we don't need to
15058 know the size of the anonymous object. */
f41f5e61 15059 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15060 }
15061 else
15062 {
15063 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15064 MSB of the anonymous object, subtract off the number of
15065 bits from the MSB of the field to the MSB of the
15066 object, and then subtract off the number of bits of
15067 the field itself. The result is the bit offset of
15068 the LSB of the field. */
c906108c
SS
15069 int anonymous_size;
15070 int bit_offset = DW_UNSND (attr);
15071
e142c38c 15072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15073 if (attr != nullptr)
c906108c
SS
15074 {
15075 /* The size of the anonymous object containing
15076 the bit field is explicit, so use the
15077 indicated size (in bytes). */
15078 anonymous_size = DW_UNSND (attr);
15079 }
15080 else
15081 {
15082 /* The size of the anonymous object containing
15083 the bit field must be inferred from the type
15084 attribute of the data member containing the
15085 bit field. */
15086 anonymous_size = TYPE_LENGTH (fp->type);
15087 }
f41f5e61
PA
15088 SET_FIELD_BITPOS (*fp,
15089 (FIELD_BITPOS (*fp)
15090 + anonymous_size * bits_per_byte
15091 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15092 }
15093 }
da5b30da
AA
15094 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15095 if (attr != NULL)
15096 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15097 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15098
15099 /* Get name of field. */
39cbfefa
DJ
15100 fieldname = dwarf2_name (die, cu);
15101 if (fieldname == NULL)
15102 fieldname = "";
d8151005
DJ
15103
15104 /* The name is already allocated along with this objfile, so we don't
15105 need to duplicate it for the type. */
15106 fp->name = fieldname;
c906108c
SS
15107
15108 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15109 pointer or virtual base class pointer) to private. */
e142c38c 15110 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15111 {
d48cc9dd 15112 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15113 new_field->accessibility = DW_ACCESS_private;
15114 fip->non_public_fields = 1;
15115 }
15116 }
a9a9bd0f 15117 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15118 {
a9a9bd0f
DC
15119 /* C++ static member. */
15120
15121 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15122 is a declaration, but all versions of G++ as of this writing
15123 (so through at least 3.2.1) incorrectly generate
15124 DW_TAG_variable tags. */
6e70227d 15125
ff355380 15126 const char *physname;
c906108c 15127
a9a9bd0f 15128 /* Get name of field. */
39cbfefa
DJ
15129 fieldname = dwarf2_name (die, cu);
15130 if (fieldname == NULL)
c906108c
SS
15131 return;
15132
254e6b9e 15133 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15134 if (attr
15135 /* Only create a symbol if this is an external value.
15136 new_symbol checks this and puts the value in the global symbol
15137 table, which we want. If it is not external, new_symbol
15138 will try to put the value in cu->list_in_scope which is wrong. */
15139 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15140 {
15141 /* A static const member, not much different than an enum as far as
15142 we're concerned, except that we can support more types. */
15143 new_symbol (die, NULL, cu);
15144 }
15145
2df3850c 15146 /* Get physical name. */
ff355380 15147 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15148
d8151005
DJ
15149 /* The name is already allocated along with this objfile, so we don't
15150 need to duplicate it for the type. */
15151 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15152 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15153 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15154 }
15155 else if (die->tag == DW_TAG_inheritance)
15156 {
74ac6d43 15157 LONGEST offset;
d4b96c9a 15158
74ac6d43
TT
15159 /* C++ base class field. */
15160 if (handle_data_member_location (die, cu, &offset))
15161 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15162 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15163 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15164 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15165 }
2ddeaf8a
TT
15166 else if (die->tag == DW_TAG_variant_part)
15167 {
15168 /* process_structure_scope will treat this DIE as a union. */
15169 process_structure_scope (die, cu);
15170
15171 /* The variant part is relative to the start of the enclosing
15172 structure. */
15173 SET_FIELD_BITPOS (*fp, 0);
15174 fp->type = get_die_type (die, cu);
15175 fp->artificial = 1;
15176 fp->name = "<<variant>>";
c8c81635
TT
15177
15178 /* Normally a DW_TAG_variant_part won't have a size, but our
15179 representation requires one, so set it to the maximum of the
489dbda6
TT
15180 child sizes, being sure to account for the offset at which
15181 each child is seen. */
c8c81635
TT
15182 if (TYPE_LENGTH (fp->type) == 0)
15183 {
15184 unsigned max = 0;
15185 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
15186 {
15187 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15188 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15189 if (len > max)
15190 max = len;
15191 }
c8c81635
TT
15192 TYPE_LENGTH (fp->type) = max;
15193 }
2ddeaf8a
TT
15194 }
15195 else
15196 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15197}
15198
883fd55a
KS
15199/* Can the type given by DIE define another type? */
15200
15201static bool
15202type_can_define_types (const struct die_info *die)
15203{
15204 switch (die->tag)
15205 {
15206 case DW_TAG_typedef:
15207 case DW_TAG_class_type:
15208 case DW_TAG_structure_type:
15209 case DW_TAG_union_type:
15210 case DW_TAG_enumeration_type:
15211 return true;
15212
15213 default:
15214 return false;
15215 }
15216}
15217
15218/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15219
15220static void
883fd55a
KS
15221dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15222 struct dwarf2_cu *cu)
6e70227d 15223{
be2daae6
TT
15224 struct decl_field fp;
15225 memset (&fp, 0, sizeof (fp));
98751a41 15226
883fd55a 15227 gdb_assert (type_can_define_types (die));
98751a41 15228
883fd55a 15229 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15230 fp.name = dwarf2_name (die, cu);
15231 fp.type = read_type_die (die, cu);
98751a41 15232
c191a687
KS
15233 /* Save accessibility. */
15234 enum dwarf_access_attribute accessibility;
15235 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15236 if (attr != NULL)
15237 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15238 else
15239 accessibility = dwarf2_default_access_attribute (die, cu);
15240 switch (accessibility)
15241 {
15242 case DW_ACCESS_public:
15243 /* The assumed value if neither private nor protected. */
15244 break;
15245 case DW_ACCESS_private:
be2daae6 15246 fp.is_private = 1;
c191a687
KS
15247 break;
15248 case DW_ACCESS_protected:
be2daae6 15249 fp.is_protected = 1;
c191a687
KS
15250 break;
15251 default:
b98664d3 15252 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15253 }
15254
883fd55a 15255 if (die->tag == DW_TAG_typedef)
be2daae6 15256 fip->typedef_field_list.push_back (fp);
883fd55a 15257 else
be2daae6 15258 fip->nested_types_list.push_back (fp);
98751a41
JK
15259}
15260
c906108c
SS
15261/* Create the vector of fields, and attach it to the type. */
15262
15263static void
fba45db2 15264dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15265 struct dwarf2_cu *cu)
c906108c
SS
15266{
15267 int nfields = fip->nfields;
15268
15269 /* Record the field count, allocate space for the array of fields,
15270 and create blank accessibility bitfields if necessary. */
15271 TYPE_NFIELDS (type) = nfields;
15272 TYPE_FIELDS (type) = (struct field *)
be2daae6 15273 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15274
b4ba55a1 15275 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15276 {
15277 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15278
15279 TYPE_FIELD_PRIVATE_BITS (type) =
15280 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15281 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15282
15283 TYPE_FIELD_PROTECTED_BITS (type) =
15284 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15285 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15286
774b6a14
TT
15287 TYPE_FIELD_IGNORE_BITS (type) =
15288 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15289 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15290 }
15291
15292 /* If the type has baseclasses, allocate and clear a bit vector for
15293 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15294 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15295 {
be2daae6 15296 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15297 unsigned char *pointer;
c906108c
SS
15298
15299 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15300 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15301 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15302 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15303 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15304 }
15305
2ddeaf8a
TT
15306 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15307 {
15308 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15309
be2daae6 15310 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15311 {
be2daae6
TT
15312 struct nextfield &field = fip->fields[index];
15313
15314 if (field.variant.is_discriminant)
2ddeaf8a 15315 di->discriminant_index = index;
be2daae6 15316 else if (field.variant.default_branch)
2ddeaf8a
TT
15317 di->default_index = index;
15318 else
be2daae6 15319 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15320 }
15321 }
15322
be2daae6
TT
15323 /* Copy the saved-up fields into the field vector. */
15324 for (int i = 0; i < nfields; ++i)
c906108c 15325 {
be2daae6
TT
15326 struct nextfield &field
15327 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15328 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15329
be2daae6
TT
15330 TYPE_FIELD (type, i) = field.field;
15331 switch (field.accessibility)
c906108c 15332 {
c5aa993b 15333 case DW_ACCESS_private:
b4ba55a1 15334 if (cu->language != language_ada)
be2daae6 15335 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15336 break;
c906108c 15337
c5aa993b 15338 case DW_ACCESS_protected:
b4ba55a1 15339 if (cu->language != language_ada)
be2daae6 15340 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15341 break;
c906108c 15342
c5aa993b
JM
15343 case DW_ACCESS_public:
15344 break;
c906108c 15345
c5aa993b
JM
15346 default:
15347 /* Unknown accessibility. Complain and treat it as public. */
15348 {
b98664d3 15349 complaint (_("unsupported accessibility %d"),
be2daae6 15350 field.accessibility);
c5aa993b
JM
15351 }
15352 break;
c906108c 15353 }
be2daae6 15354 if (i < fip->baseclasses.size ())
c906108c 15355 {
be2daae6 15356 switch (field.virtuality)
c906108c 15357 {
c5aa993b
JM
15358 case DW_VIRTUALITY_virtual:
15359 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15360 if (cu->language == language_ada)
a73c6dcd 15361 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15362 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15363 break;
c906108c
SS
15364 }
15365 }
c906108c
SS
15366 }
15367}
15368
7d27a96d
TT
15369/* Return true if this member function is a constructor, false
15370 otherwise. */
15371
15372static int
15373dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15374{
15375 const char *fieldname;
fe978cb0 15376 const char *type_name;
7d27a96d
TT
15377 int len;
15378
15379 if (die->parent == NULL)
15380 return 0;
15381
15382 if (die->parent->tag != DW_TAG_structure_type
15383 && die->parent->tag != DW_TAG_union_type
15384 && die->parent->tag != DW_TAG_class_type)
15385 return 0;
15386
15387 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15388 type_name = dwarf2_name (die->parent, cu);
15389 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15390 return 0;
15391
15392 len = strlen (fieldname);
fe978cb0
PA
15393 return (strncmp (fieldname, type_name, len) == 0
15394 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15395}
15396
e35000a7
TBA
15397/* Check if the given VALUE is a recognized enum
15398 dwarf_defaulted_attribute constant according to DWARF5 spec,
15399 Table 7.24. */
15400
15401static bool
15402is_valid_DW_AT_defaulted (ULONGEST value)
15403{
15404 switch (value)
15405 {
15406 case DW_DEFAULTED_no:
15407 case DW_DEFAULTED_in_class:
15408 case DW_DEFAULTED_out_of_class:
15409 return true;
15410 }
15411
3142e908 15412 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15413 return false;
15414}
15415
c906108c
SS
15416/* Add a member function to the proper fieldlist. */
15417
15418static void
107d2387 15419dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15420 struct type *type, struct dwarf2_cu *cu)
c906108c 15421{
518817b3 15422 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15423 struct attribute *attr;
c906108c 15424 int i;
be2daae6 15425 struct fnfieldlist *flp = nullptr;
c906108c 15426 struct fn_field *fnp;
15d034d0 15427 const char *fieldname;
f792889a 15428 struct type *this_type;
60d5a603 15429 enum dwarf_access_attribute accessibility;
c906108c 15430
b4ba55a1 15431 if (cu->language == language_ada)
a73c6dcd 15432 error (_("unexpected member function in Ada type"));
b4ba55a1 15433
2df3850c 15434 /* Get name of member function. */
39cbfefa
DJ
15435 fieldname = dwarf2_name (die, cu);
15436 if (fieldname == NULL)
2df3850c 15437 return;
c906108c 15438
c906108c 15439 /* Look up member function name in fieldlist. */
be2daae6 15440 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15441 {
27bfe10e 15442 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15443 {
15444 flp = &fip->fnfieldlists[i];
15445 break;
15446 }
c906108c
SS
15447 }
15448
be2daae6
TT
15449 /* Create a new fnfieldlist if necessary. */
15450 if (flp == nullptr)
c906108c 15451 {
be2daae6
TT
15452 fip->fnfieldlists.emplace_back ();
15453 flp = &fip->fnfieldlists.back ();
c906108c 15454 flp->name = fieldname;
be2daae6 15455 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15456 }
15457
be2daae6
TT
15458 /* Create a new member function field and add it to the vector of
15459 fnfieldlists. */
15460 flp->fnfields.emplace_back ();
15461 fnp = &flp->fnfields.back ();
3da10d80
KS
15462
15463 /* Delay processing of the physname until later. */
9c37b5ae 15464 if (cu->language == language_cplus)
be2daae6
TT
15465 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15466 die, cu);
3da10d80
KS
15467 else
15468 {
1d06ead6 15469 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15470 fnp->physname = physname ? physname : "";
15471 }
15472
c906108c 15473 fnp->type = alloc_type (objfile);
f792889a
DJ
15474 this_type = read_type_die (die, cu);
15475 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15476 {
f792889a 15477 int nparams = TYPE_NFIELDS (this_type);
c906108c 15478
f792889a 15479 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15480 of the method itself (TYPE_CODE_METHOD). */
15481 smash_to_method_type (fnp->type, type,
f792889a
DJ
15482 TYPE_TARGET_TYPE (this_type),
15483 TYPE_FIELDS (this_type),
15484 TYPE_NFIELDS (this_type),
15485 TYPE_VARARGS (this_type));
c906108c
SS
15486
15487 /* Handle static member functions.
c5aa993b 15488 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15489 member functions. G++ helps GDB by marking the first
15490 parameter for non-static member functions (which is the this
15491 pointer) as artificial. We obtain this information from
15492 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15493 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15494 fnp->voffset = VOFFSET_STATIC;
15495 }
15496 else
b98664d3 15497 complaint (_("member function type missing for '%s'"),
3da10d80 15498 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15499
15500 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15501 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15502 fnp->fcontext = die_containing_type (die, cu);
c906108c 15503
3e43a32a
MS
15504 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15505 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15506
15507 /* Get accessibility. */
e142c38c 15508 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15509 if (attr != nullptr)
aead7601 15510 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15511 else
15512 accessibility = dwarf2_default_access_attribute (die, cu);
15513 switch (accessibility)
c906108c 15514 {
60d5a603
JK
15515 case DW_ACCESS_private:
15516 fnp->is_private = 1;
15517 break;
15518 case DW_ACCESS_protected:
15519 fnp->is_protected = 1;
15520 break;
c906108c
SS
15521 }
15522
b02dede2 15523 /* Check for artificial methods. */
e142c38c 15524 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15525 if (attr && DW_UNSND (attr) != 0)
15526 fnp->is_artificial = 1;
15527
e35000a7
TBA
15528 /* Check for defaulted methods. */
15529 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15530 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15531 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15532
15533 /* Check for deleted methods. */
15534 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15535 if (attr != nullptr && DW_UNSND (attr) != 0)
15536 fnp->is_deleted = 1;
15537
7d27a96d
TT
15538 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15539
0d564a31 15540 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15541 function. For older versions of GCC, this is an offset in the
15542 appropriate virtual table, as specified by DW_AT_containing_type.
15543 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15544 to the object address. */
15545
e142c38c 15546 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15547 if (attr != nullptr)
8e19ed76 15548 {
aec5aa8b 15549 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15550 {
aec5aa8b
TT
15551 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15552 {
15553 /* Old-style GCC. */
15554 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15555 }
15556 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15557 || (DW_BLOCK (attr)->size > 1
15558 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15559 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15560 {
aec5aa8b
TT
15561 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15562 if ((fnp->voffset % cu->header.addr_size) != 0)
15563 dwarf2_complex_location_expr_complaint ();
15564 else
15565 fnp->voffset /= cu->header.addr_size;
15566 fnp->voffset += 2;
15567 }
15568 else
15569 dwarf2_complex_location_expr_complaint ();
15570
15571 if (!fnp->fcontext)
7e993ebf
KS
15572 {
15573 /* If there is no `this' field and no DW_AT_containing_type,
15574 we cannot actually find a base class context for the
15575 vtable! */
15576 if (TYPE_NFIELDS (this_type) == 0
15577 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15578 {
b98664d3 15579 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15580 "function \"%s\" (offset %s)"),
15581 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15582 }
15583 else
15584 {
15585 fnp->fcontext
15586 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15587 }
15588 }
aec5aa8b 15589 }
3690dd37 15590 else if (attr_form_is_section_offset (attr))
8e19ed76 15591 {
4d3c2250 15592 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15593 }
15594 else
15595 {
4d3c2250
KB
15596 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15597 fieldname);
8e19ed76 15598 }
0d564a31 15599 }
d48cc9dd
DJ
15600 else
15601 {
15602 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15603 if (attr && DW_UNSND (attr))
15604 {
15605 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15606 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15607 "but the vtable offset is not specified"),
9d8780f0 15608 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15609 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15610 TYPE_CPLUS_DYNAMIC (type) = 1;
15611 }
15612 }
c906108c
SS
15613}
15614
15615/* Create the vector of member function fields, and attach it to the type. */
15616
15617static void
fba45db2 15618dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15619 struct dwarf2_cu *cu)
c906108c 15620{
b4ba55a1 15621 if (cu->language == language_ada)
a73c6dcd 15622 error (_("unexpected member functions in Ada type"));
b4ba55a1 15623
c906108c
SS
15624 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15625 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15626 TYPE_ALLOC (type,
15627 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15628
be2daae6 15629 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15630 {
be2daae6 15631 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15632 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15633
be2daae6
TT
15634 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15635 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15636 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15637 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15638
15639 for (int k = 0; k < nf.fnfields.size (); ++k)
15640 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15641 }
15642
be2daae6 15643 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15644}
15645
1168df01
JB
15646/* Returns non-zero if NAME is the name of a vtable member in CU's
15647 language, zero otherwise. */
15648static int
15649is_vtable_name (const char *name, struct dwarf2_cu *cu)
15650{
15651 static const char vptr[] = "_vptr";
15652
9c37b5ae
TT
15653 /* Look for the C++ form of the vtable. */
15654 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15655 return 1;
15656
15657 return 0;
15658}
15659
c0dd20ea 15660/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15661 functions, with the ABI-specified layout. If TYPE describes
15662 such a structure, smash it into a member function type.
61049d3b
DJ
15663
15664 GCC shouldn't do this; it should just output pointer to member DIEs.
15665 This is GCC PR debug/28767. */
c0dd20ea 15666
0b92b5bb
TT
15667static void
15668quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15669{
09e2d7c7 15670 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15671
15672 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15673 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15674 return;
c0dd20ea
DJ
15675
15676 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15677 if (TYPE_FIELD_NAME (type, 0) == NULL
15678 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15679 || TYPE_FIELD_NAME (type, 1) == NULL
15680 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15681 return;
c0dd20ea
DJ
15682
15683 /* Find the type of the method. */
0b92b5bb 15684 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15685 if (pfn_type == NULL
15686 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15687 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15688 return;
c0dd20ea
DJ
15689
15690 /* Look for the "this" argument. */
15691 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15692 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15693 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15694 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15695 return;
c0dd20ea 15696
09e2d7c7 15697 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15698 new_type = alloc_type (objfile);
09e2d7c7 15699 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15700 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15701 TYPE_VARARGS (pfn_type));
0b92b5bb 15702 smash_to_methodptr_type (type, new_type);
c0dd20ea 15703}
1168df01 15704
2b4424c3
TT
15705/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15706 appropriate error checking and issuing complaints if there is a
15707 problem. */
15708
15709static ULONGEST
15710get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15711{
15712 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15713
15714 if (attr == nullptr)
15715 return 0;
15716
15717 if (!attr_form_is_constant (attr))
15718 {
b98664d3 15719 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15720 " - DIE at %s [in module %s]"),
15721 sect_offset_str (die->sect_off),
15722 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15723 return 0;
15724 }
15725
15726 ULONGEST align;
15727 if (attr->form == DW_FORM_sdata)
15728 {
15729 LONGEST val = DW_SND (attr);
15730 if (val < 0)
15731 {
b98664d3 15732 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15733 " - DIE at %s [in module %s]"),
15734 sect_offset_str (die->sect_off),
15735 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15736 return 0;
15737 }
15738 align = val;
15739 }
15740 else
15741 align = DW_UNSND (attr);
15742
15743 if (align == 0)
15744 {
b98664d3 15745 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15746 " - DIE at %s [in module %s]"),
15747 sect_offset_str (die->sect_off),
15748 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15749 return 0;
15750 }
15751 if ((align & (align - 1)) != 0)
15752 {
b98664d3 15753 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15754 " - DIE at %s [in module %s]"),
15755 sect_offset_str (die->sect_off),
15756 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15757 return 0;
15758 }
15759
15760 return align;
15761}
15762
15763/* If the DIE has a DW_AT_alignment attribute, use its value to set
15764 the alignment for TYPE. */
15765
15766static void
15767maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15768 struct type *type)
15769{
15770 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15771 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15772 " - DIE at %s [in module %s]"),
15773 sect_offset_str (die->sect_off),
15774 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15775}
685b1105 15776
e35000a7
TBA
15777/* Check if the given VALUE is a valid enum dwarf_calling_convention
15778 constant for a type, according to DWARF5 spec, Table 5.5. */
15779
15780static bool
15781is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15782{
15783 switch (value)
15784 {
15785 case DW_CC_normal:
15786 case DW_CC_pass_by_reference:
15787 case DW_CC_pass_by_value:
15788 return true;
15789
15790 default:
15791 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15792 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15793 return false;
15794 }
15795}
15796
d0922fcf
TBA
15797/* Check if the given VALUE is a valid enum dwarf_calling_convention
15798 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15799 also according to GNU-specific values (see include/dwarf2.h). */
15800
15801static bool
15802is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15803{
15804 switch (value)
15805 {
15806 case DW_CC_normal:
15807 case DW_CC_program:
15808 case DW_CC_nocall:
15809 return true;
15810
15811 case DW_CC_GNU_renesas_sh:
15812 case DW_CC_GNU_borland_fastcall_i386:
15813 case DW_CC_GDB_IBM_OpenCL:
15814 return true;
15815
15816 default:
15817 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15818 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15819 return false;
15820 }
15821}
15822
c906108c 15823/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15824 (definition) to create a type for the structure or union. Fill in
15825 the type's name and general properties; the members will not be
83655187
DE
15826 processed until process_structure_scope. A symbol table entry for
15827 the type will also not be done until process_structure_scope (assuming
15828 the type has a name).
c906108c 15829
c767944b
DJ
15830 NOTE: we need to call these functions regardless of whether or not the
15831 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15832 structure or union. This gets the type entered into our set of
83655187 15833 user defined types. */
c906108c 15834
f792889a 15835static struct type *
134d01f1 15836read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15837{
518817b3 15838 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15839 struct type *type;
15840 struct attribute *attr;
15d034d0 15841 const char *name;
c906108c 15842
348e048f
DE
15843 /* If the definition of this type lives in .debug_types, read that type.
15844 Don't follow DW_AT_specification though, that will take us back up
15845 the chain and we want to go down. */
45e58e77 15846 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15847 if (attr != nullptr)
348e048f 15848 {
ac9ec31b 15849 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15850
ac9ec31b 15851 /* The type's CU may not be the same as CU.
02142a6c 15852 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15853 return set_die_type (die, type, cu);
15854 }
15855
c0dd20ea 15856 type = alloc_type (objfile);
c906108c 15857 INIT_CPLUS_SPECIFIC (type);
93311388 15858
39cbfefa
DJ
15859 name = dwarf2_name (die, cu);
15860 if (name != NULL)
c906108c 15861 {
987504bb 15862 if (cu->language == language_cplus
c44af4eb
TT
15863 || cu->language == language_d
15864 || cu->language == language_rust)
63d06c5c 15865 {
15d034d0 15866 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15867
15868 /* dwarf2_full_name might have already finished building the DIE's
15869 type. If so, there is no need to continue. */
15870 if (get_die_type (die, cu) != NULL)
15871 return get_die_type (die, cu);
15872
e86ca25f 15873 TYPE_NAME (type) = full_name;
63d06c5c
DC
15874 }
15875 else
15876 {
d8151005
DJ
15877 /* The name is already allocated along with this objfile, so
15878 we don't need to duplicate it for the type. */
e86ca25f 15879 TYPE_NAME (type) = name;
63d06c5c 15880 }
c906108c
SS
15881 }
15882
15883 if (die->tag == DW_TAG_structure_type)
15884 {
15885 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15886 }
15887 else if (die->tag == DW_TAG_union_type)
15888 {
15889 TYPE_CODE (type) = TYPE_CODE_UNION;
15890 }
2ddeaf8a
TT
15891 else if (die->tag == DW_TAG_variant_part)
15892 {
15893 TYPE_CODE (type) = TYPE_CODE_UNION;
15894 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15895 }
c906108c
SS
15896 else
15897 {
4753d33b 15898 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15899 }
15900
0cc2414c
TT
15901 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15902 TYPE_DECLARED_CLASS (type) = 1;
15903
e35000a7
TBA
15904 /* Store the calling convention in the type if it's available in
15905 the die. Otherwise the calling convention remains set to
15906 the default value DW_CC_normal. */
15907 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15908 if (attr != nullptr
15909 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15910 {
15911 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15912 TYPE_CPLUS_CALLING_CONVENTION (type)
15913 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15914 }
15915
e142c38c 15916 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15917 if (attr != nullptr)
c906108c 15918 {
155bfbd3
JB
15919 if (attr_form_is_constant (attr))
15920 TYPE_LENGTH (type) = DW_UNSND (attr);
15921 else
15922 {
15923 /* For the moment, dynamic type sizes are not supported
15924 by GDB's struct type. The actual size is determined
15925 on-demand when resolving the type of a given object,
15926 so set the type's length to zero for now. Otherwise,
15927 we record an expression as the length, and that expression
15928 could lead to a very large value, which could eventually
15929 lead to us trying to allocate that much memory when creating
15930 a value of that type. */
15931 TYPE_LENGTH (type) = 0;
15932 }
c906108c
SS
15933 }
15934 else
15935 {
15936 TYPE_LENGTH (type) = 0;
15937 }
15938
2b4424c3
TT
15939 maybe_set_alignment (cu, die, type);
15940
5230b05a 15941 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15942 {
5230b05a
WT
15943 /* ICC<14 does not output the required DW_AT_declaration on
15944 incomplete types, but gives them a size of zero. */
422b1cb0 15945 TYPE_STUB (type) = 1;
685b1105
JK
15946 }
15947 else
15948 TYPE_STUB_SUPPORTED (type) = 1;
15949
dc718098 15950 if (die_is_declaration (die, cu))
876cecd0 15951 TYPE_STUB (type) = 1;
a6c727b2
DJ
15952 else if (attr == NULL && die->child == NULL
15953 && producer_is_realview (cu->producer))
15954 /* RealView does not output the required DW_AT_declaration
15955 on incomplete types. */
15956 TYPE_STUB (type) = 1;
dc718098 15957
c906108c
SS
15958 /* We need to add the type field to the die immediately so we don't
15959 infinitely recurse when dealing with pointers to the structure
0963b4bd 15960 type within the structure itself. */
1c379e20 15961 set_die_type (die, type, cu);
c906108c 15962
7e314c57
JK
15963 /* set_die_type should be already done. */
15964 set_descriptive_type (type, die, cu);
15965
c767944b
DJ
15966 return type;
15967}
15968
2ddeaf8a
TT
15969/* A helper for process_structure_scope that handles a single member
15970 DIE. */
15971
15972static void
15973handle_struct_member_die (struct die_info *child_die, struct type *type,
15974 struct field_info *fi,
15975 std::vector<struct symbol *> *template_args,
15976 struct dwarf2_cu *cu)
15977{
15978 if (child_die->tag == DW_TAG_member
15979 || child_die->tag == DW_TAG_variable
15980 || child_die->tag == DW_TAG_variant_part)
15981 {
15982 /* NOTE: carlton/2002-11-05: A C++ static data member
15983 should be a DW_TAG_member that is a declaration, but
15984 all versions of G++ as of this writing (so through at
15985 least 3.2.1) incorrectly generate DW_TAG_variable
15986 tags for them instead. */
15987 dwarf2_add_field (fi, child_die, cu);
15988 }
15989 else if (child_die->tag == DW_TAG_subprogram)
15990 {
15991 /* Rust doesn't have member functions in the C++ sense.
15992 However, it does emit ordinary functions as children
15993 of a struct DIE. */
15994 if (cu->language == language_rust)
15995 read_func_scope (child_die, cu);
15996 else
15997 {
15998 /* C++ member function. */
15999 dwarf2_add_member_fn (fi, child_die, type, cu);
16000 }
16001 }
16002 else if (child_die->tag == DW_TAG_inheritance)
16003 {
16004 /* C++ base class field. */
16005 dwarf2_add_field (fi, child_die, cu);
16006 }
16007 else if (type_can_define_types (child_die))
16008 dwarf2_add_type_defn (fi, child_die, cu);
16009 else if (child_die->tag == DW_TAG_template_type_param
16010 || child_die->tag == DW_TAG_template_value_param)
16011 {
16012 struct symbol *arg = new_symbol (child_die, NULL, cu);
16013
16014 if (arg != NULL)
16015 template_args->push_back (arg);
16016 }
16017 else if (child_die->tag == DW_TAG_variant)
16018 {
16019 /* In a variant we want to get the discriminant and also add a
16020 field for our sole member child. */
16021 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16022
bde09ab7 16023 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
16024 variant_child != NULL;
16025 variant_child = sibling_die (variant_child))
16026 {
16027 if (variant_child->tag == DW_TAG_member)
16028 {
16029 handle_struct_member_die (variant_child, type, fi,
16030 template_args, cu);
16031 /* Only handle the one. */
16032 break;
16033 }
16034 }
16035
16036 /* We don't handle this but we might as well report it if we see
16037 it. */
16038 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 16039 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
16040 " - DIE at %s [in module %s]"),
16041 sect_offset_str (child_die->sect_off),
16042 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16043
16044 /* The first field was just added, so we can stash the
16045 discriminant there. */
be2daae6 16046 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16047 if (discr == NULL)
be2daae6 16048 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16049 else
be2daae6 16050 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16051 }
16052}
16053
c767944b
DJ
16054/* Finish creating a structure or union type, including filling in
16055 its members and creating a symbol for it. */
16056
16057static void
16058process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16059{
518817b3 16060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16061 struct die_info *child_die;
c767944b
DJ
16062 struct type *type;
16063
16064 type = get_die_type (die, cu);
16065 if (type == NULL)
16066 type = read_structure_type (die, cu);
16067
2ddeaf8a
TT
16068 /* When reading a DW_TAG_variant_part, we need to notice when we
16069 read the discriminant member, so we can record it later in the
16070 discriminant_info. */
16071 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 16072 sect_offset discr_offset {};
3e1d3d8c 16073 bool has_template_parameters = false;
2ddeaf8a
TT
16074
16075 if (is_variant_part)
16076 {
16077 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16078 if (discr == NULL)
16079 {
16080 /* Maybe it's a univariant form, an extension we support.
16081 In this case arrange not to check the offset. */
16082 is_variant_part = false;
16083 }
16084 else if (attr_form_is_ref (discr))
16085 {
16086 struct dwarf2_cu *target_cu = cu;
16087 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16088
16089 discr_offset = target_die->sect_off;
16090 }
16091 else
16092 {
b98664d3 16093 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16094 " - DIE at %s [in module %s]"),
16095 sect_offset_str (die->sect_off),
16096 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16097 is_variant_part = false;
16098 }
16099 }
16100
e142c38c 16101 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16102 {
16103 struct field_info fi;
2f4732b0 16104 std::vector<struct symbol *> template_args;
c906108c 16105
639d11d3 16106 child_die = die->child;
c906108c
SS
16107
16108 while (child_die && child_die->tag)
16109 {
2ddeaf8a 16110 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16111
2ddeaf8a 16112 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16113 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16114
c906108c
SS
16115 child_die = sibling_die (child_die);
16116 }
16117
34eaf542 16118 /* Attach template arguments to type. */
2f4732b0 16119 if (!template_args.empty ())
34eaf542 16120 {
3e1d3d8c 16121 has_template_parameters = true;
34eaf542 16122 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16123 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16124 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16125 = XOBNEWVEC (&objfile->objfile_obstack,
16126 struct symbol *,
16127 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16128 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16129 template_args.data (),
34eaf542
TT
16130 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16131 * sizeof (struct symbol *)));
34eaf542
TT
16132 }
16133
c906108c
SS
16134 /* Attach fields and member functions to the type. */
16135 if (fi.nfields)
e7c27a73 16136 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16137 if (!fi.fnfieldlists.empty ())
c906108c 16138 {
e7c27a73 16139 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16140
c5aa993b 16141 /* Get the type which refers to the base class (possibly this
c906108c 16142 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16143 class from the DW_AT_containing_type attribute. This use of
16144 DW_AT_containing_type is a GNU extension. */
c906108c 16145
e142c38c 16146 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16147 {
e7c27a73 16148 struct type *t = die_containing_type (die, cu);
c906108c 16149
ae6ae975 16150 set_type_vptr_basetype (type, t);
c906108c
SS
16151 if (type == t)
16152 {
c906108c
SS
16153 int i;
16154
16155 /* Our own class provides vtbl ptr. */
16156 for (i = TYPE_NFIELDS (t) - 1;
16157 i >= TYPE_N_BASECLASSES (t);
16158 --i)
16159 {
0d5cff50 16160 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16161
1168df01 16162 if (is_vtable_name (fieldname, cu))
c906108c 16163 {
ae6ae975 16164 set_type_vptr_fieldno (type, i);
c906108c
SS
16165 break;
16166 }
16167 }
16168
16169 /* Complain if virtual function table field not found. */
16170 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16171 complaint (_("virtual function table pointer "
3e43a32a 16172 "not found when defining class '%s'"),
e86ca25f 16173 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16174 }
16175 else
16176 {
ae6ae975 16177 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16178 }
16179 }
f6235d4c 16180 else if (cu->producer
61012eef 16181 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16182 {
16183 /* The IBM XLC compiler does not provide direct indication
16184 of the containing type, but the vtable pointer is
16185 always named __vfp. */
16186
16187 int i;
16188
16189 for (i = TYPE_NFIELDS (type) - 1;
16190 i >= TYPE_N_BASECLASSES (type);
16191 --i)
16192 {
16193 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16194 {
ae6ae975
DE
16195 set_type_vptr_fieldno (type, i);
16196 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16197 break;
16198 }
16199 }
16200 }
c906108c 16201 }
98751a41
JK
16202
16203 /* Copy fi.typedef_field_list linked list elements content into the
16204 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16205 if (!fi.typedef_field_list.empty ())
98751a41 16206 {
be2daae6 16207 int count = fi.typedef_field_list.size ();
98751a41 16208
a0d7a4ff 16209 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16210 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16211 = ((struct decl_field *)
be2daae6
TT
16212 TYPE_ALLOC (type,
16213 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16214 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16215
be2daae6
TT
16216 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16217 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16218 }
c767944b 16219
883fd55a
KS
16220 /* Copy fi.nested_types_list linked list elements content into the
16221 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16222 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16223 {
be2daae6 16224 int count = fi.nested_types_list.size ();
883fd55a
KS
16225
16226 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16227 TYPE_NESTED_TYPES_ARRAY (type)
16228 = ((struct decl_field *)
be2daae6
TT
16229 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16230 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16231
be2daae6
TT
16232 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16233 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16234 }
c906108c 16235 }
63d06c5c 16236
bb5ed363 16237 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16238 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16239 cu->rust_unions.push_back (type);
0b92b5bb 16240
90aeadfc
DC
16241 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16242 snapshots) has been known to create a die giving a declaration
16243 for a class that has, as a child, a die giving a definition for a
16244 nested class. So we have to process our children even if the
16245 current die is a declaration. Normally, of course, a declaration
16246 won't have any children at all. */
134d01f1 16247
ca040673
DE
16248 child_die = die->child;
16249
90aeadfc
DC
16250 while (child_die != NULL && child_die->tag)
16251 {
16252 if (child_die->tag == DW_TAG_member
16253 || child_die->tag == DW_TAG_variable
34eaf542
TT
16254 || child_die->tag == DW_TAG_inheritance
16255 || child_die->tag == DW_TAG_template_value_param
16256 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16257 {
90aeadfc 16258 /* Do nothing. */
134d01f1 16259 }
90aeadfc
DC
16260 else
16261 process_die (child_die, cu);
134d01f1 16262
90aeadfc 16263 child_die = sibling_die (child_die);
134d01f1
DJ
16264 }
16265
fa4028e9
JB
16266 /* Do not consider external references. According to the DWARF standard,
16267 these DIEs are identified by the fact that they have no byte_size
16268 attribute, and a declaration attribute. */
16269 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16270 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16271 {
16272 struct symbol *sym = new_symbol (die, type, cu);
16273
16274 if (has_template_parameters)
16275 {
a776957c
TT
16276 struct symtab *symtab;
16277 if (sym != nullptr)
16278 symtab = symbol_symtab (sym);
16279 else if (cu->line_header != nullptr)
16280 {
16281 /* Any related symtab will do. */
16282 symtab
7ba99d21 16283 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16284 }
16285 else
16286 {
16287 symtab = nullptr;
16288 complaint (_("could not find suitable "
16289 "symtab for template parameter"
16290 " - DIE at %s [in module %s]"),
16291 sect_offset_str (die->sect_off),
16292 objfile_name (objfile));
16293 }
16294
16295 if (symtab != nullptr)
16296 {
16297 /* Make sure that the symtab is set on the new symbols.
16298 Even though they don't appear in this symtab directly,
16299 other parts of gdb assume that symbols do, and this is
16300 reasonably true. */
16301 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16302 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16303 }
3e1d3d8c
TT
16304 }
16305 }
134d01f1
DJ
16306}
16307
55426c9d
JB
16308/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16309 update TYPE using some information only available in DIE's children. */
16310
16311static void
16312update_enumeration_type_from_children (struct die_info *die,
16313 struct type *type,
16314 struct dwarf2_cu *cu)
16315{
60f7655a 16316 struct die_info *child_die;
55426c9d
JB
16317 int unsigned_enum = 1;
16318 int flag_enum = 1;
16319 ULONGEST mask = 0;
55426c9d 16320
8268c778 16321 auto_obstack obstack;
55426c9d 16322
60f7655a
DE
16323 for (child_die = die->child;
16324 child_die != NULL && child_die->tag;
16325 child_die = sibling_die (child_die))
55426c9d
JB
16326 {
16327 struct attribute *attr;
16328 LONGEST value;
16329 const gdb_byte *bytes;
16330 struct dwarf2_locexpr_baton *baton;
16331 const char *name;
60f7655a 16332
55426c9d
JB
16333 if (child_die->tag != DW_TAG_enumerator)
16334 continue;
16335
16336 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16337 if (attr == NULL)
16338 continue;
16339
16340 name = dwarf2_name (child_die, cu);
16341 if (name == NULL)
16342 name = "<anonymous enumerator>";
16343
16344 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16345 &value, &bytes, &baton);
16346 if (value < 0)
16347 {
16348 unsigned_enum = 0;
16349 flag_enum = 0;
16350 }
16351 else if ((mask & value) != 0)
16352 flag_enum = 0;
16353 else
16354 mask |= value;
16355
16356 /* If we already know that the enum type is neither unsigned, nor
16357 a flag type, no need to look at the rest of the enumerates. */
16358 if (!unsigned_enum && !flag_enum)
16359 break;
55426c9d
JB
16360 }
16361
16362 if (unsigned_enum)
16363 TYPE_UNSIGNED (type) = 1;
16364 if (flag_enum)
16365 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16366}
16367
134d01f1
DJ
16368/* Given a DW_AT_enumeration_type die, set its type. We do not
16369 complete the type's fields yet, or create any symbols. */
c906108c 16370
f792889a 16371static struct type *
134d01f1 16372read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16373{
518817b3 16374 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16375 struct type *type;
c906108c 16376 struct attribute *attr;
0114d602 16377 const char *name;
134d01f1 16378
348e048f
DE
16379 /* If the definition of this type lives in .debug_types, read that type.
16380 Don't follow DW_AT_specification though, that will take us back up
16381 the chain and we want to go down. */
45e58e77 16382 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16383 if (attr != nullptr)
348e048f 16384 {
ac9ec31b 16385 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16386
ac9ec31b 16387 /* The type's CU may not be the same as CU.
02142a6c 16388 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16389 return set_die_type (die, type, cu);
16390 }
16391
c906108c
SS
16392 type = alloc_type (objfile);
16393
16394 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16395 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16396 if (name != NULL)
e86ca25f 16397 TYPE_NAME (type) = name;
c906108c 16398
0626fc76
TT
16399 attr = dwarf2_attr (die, DW_AT_type, cu);
16400 if (attr != NULL)
16401 {
16402 struct type *underlying_type = die_type (die, cu);
16403
16404 TYPE_TARGET_TYPE (type) = underlying_type;
16405 }
16406
e142c38c 16407 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16408 if (attr != nullptr)
c906108c
SS
16409 {
16410 TYPE_LENGTH (type) = DW_UNSND (attr);
16411 }
16412 else
16413 {
16414 TYPE_LENGTH (type) = 0;
16415 }
16416
2b4424c3
TT
16417 maybe_set_alignment (cu, die, type);
16418
137033e9
JB
16419 /* The enumeration DIE can be incomplete. In Ada, any type can be
16420 declared as private in the package spec, and then defined only
16421 inside the package body. Such types are known as Taft Amendment
16422 Types. When another package uses such a type, an incomplete DIE
16423 may be generated by the compiler. */
02eb380e 16424 if (die_is_declaration (die, cu))
876cecd0 16425 TYPE_STUB (type) = 1;
02eb380e 16426
0626fc76
TT
16427 /* Finish the creation of this type by using the enum's children.
16428 We must call this even when the underlying type has been provided
16429 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16430 update_enumeration_type_from_children (die, type, cu);
16431
0626fc76
TT
16432 /* If this type has an underlying type that is not a stub, then we
16433 may use its attributes. We always use the "unsigned" attribute
16434 in this situation, because ordinarily we guess whether the type
16435 is unsigned -- but the guess can be wrong and the underlying type
16436 can tell us the reality. However, we defer to a local size
16437 attribute if one exists, because this lets the compiler override
16438 the underlying type if needed. */
16439 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16440 {
16441 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16442 if (TYPE_LENGTH (type) == 0)
16443 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16444 if (TYPE_RAW_ALIGN (type) == 0
16445 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16446 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16447 }
16448
3d567982
TT
16449 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16450
f792889a 16451 return set_die_type (die, type, cu);
134d01f1
DJ
16452}
16453
16454/* Given a pointer to a die which begins an enumeration, process all
16455 the dies that define the members of the enumeration, and create the
16456 symbol for the enumeration type.
16457
16458 NOTE: We reverse the order of the element list. */
16459
16460static void
16461process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16462{
f792889a 16463 struct type *this_type;
134d01f1 16464
f792889a
DJ
16465 this_type = get_die_type (die, cu);
16466 if (this_type == NULL)
16467 this_type = read_enumeration_type (die, cu);
9dc481d3 16468
639d11d3 16469 if (die->child != NULL)
c906108c 16470 {
9dc481d3
DE
16471 struct die_info *child_die;
16472 struct symbol *sym;
43816ebc 16473 std::vector<struct field> fields;
15d034d0 16474 const char *name;
9dc481d3 16475
639d11d3 16476 child_die = die->child;
c906108c
SS
16477 while (child_die && child_die->tag)
16478 {
16479 if (child_die->tag != DW_TAG_enumerator)
16480 {
e7c27a73 16481 process_die (child_die, cu);
c906108c
SS
16482 }
16483 else
16484 {
39cbfefa
DJ
16485 name = dwarf2_name (child_die, cu);
16486 if (name)
c906108c 16487 {
f792889a 16488 sym = new_symbol (child_die, this_type, cu);
c906108c 16489
43816ebc
TT
16490 fields.emplace_back ();
16491 struct field &field = fields.back ();
c906108c 16492
43816ebc
TT
16493 FIELD_NAME (field) = sym->linkage_name ();
16494 FIELD_TYPE (field) = NULL;
16495 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16496 FIELD_BITSIZE (field) = 0;
c906108c
SS
16497 }
16498 }
16499
16500 child_die = sibling_die (child_die);
16501 }
16502
43816ebc 16503 if (!fields.empty ())
c906108c 16504 {
43816ebc 16505 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 16506 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
16507 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16508 memcpy (TYPE_FIELDS (this_type), fields.data (),
16509 sizeof (struct field) * fields.size ());
c906108c 16510 }
c906108c 16511 }
134d01f1 16512
6c83ed52
TT
16513 /* If we are reading an enum from a .debug_types unit, and the enum
16514 is a declaration, and the enum is not the signatured type in the
16515 unit, then we do not want to add a symbol for it. Adding a
16516 symbol would in some cases obscure the true definition of the
16517 enum, giving users an incomplete type when the definition is
16518 actually available. Note that we do not want to do this for all
16519 enums which are just declarations, because C++0x allows forward
16520 enum declarations. */
3019eac3 16521 if (cu->per_cu->is_debug_types
6c83ed52
TT
16522 && die_is_declaration (die, cu))
16523 {
52dc124a 16524 struct signatured_type *sig_type;
6c83ed52 16525
c0f78cd4 16526 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16527 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16528 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16529 return;
16530 }
16531
f792889a 16532 new_symbol (die, this_type, cu);
c906108c
SS
16533}
16534
16535/* Extract all information from a DW_TAG_array_type DIE and put it in
16536 the DIE's type field. For now, this only handles one dimensional
16537 arrays. */
16538
f792889a 16539static struct type *
e7c27a73 16540read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16541{
518817b3 16542 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16543 struct die_info *child_die;
7e314c57 16544 struct type *type;
c906108c 16545 struct type *element_type, *range_type, *index_type;
c906108c 16546 struct attribute *attr;
15d034d0 16547 const char *name;
a405673c 16548 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16549 unsigned int bit_stride = 0;
c906108c 16550
e7c27a73 16551 element_type = die_type (die, cu);
c906108c 16552
7e314c57
JK
16553 /* The die_type call above may have already set the type for this DIE. */
16554 type = get_die_type (die, cu);
16555 if (type)
16556 return type;
16557
dc53a7ad
JB
16558 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16559 if (attr != NULL)
a405673c
JB
16560 {
16561 int stride_ok;
9a49df9d
AB
16562 struct type *prop_type
16563 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16564
16565 byte_stride_prop
16566 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16567 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16568 prop_type);
a405673c
JB
16569 if (!stride_ok)
16570 {
b98664d3 16571 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16572 " - DIE at %s [in module %s]"),
16573 sect_offset_str (die->sect_off),
518817b3 16574 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16575 /* Ignore this attribute. We will likely not be able to print
16576 arrays of this type correctly, but there is little we can do
16577 to help if we cannot read the attribute's value. */
16578 byte_stride_prop = NULL;
16579 }
16580 }
dc53a7ad
JB
16581
16582 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16583 if (attr != NULL)
16584 bit_stride = DW_UNSND (attr);
16585
c906108c
SS
16586 /* Irix 6.2 native cc creates array types without children for
16587 arrays with unspecified length. */
639d11d3 16588 if (die->child == NULL)
c906108c 16589 {
46bf5051 16590 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16591 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16592 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16593 byte_stride_prop, bit_stride);
f792889a 16594 return set_die_type (die, type, cu);
c906108c
SS
16595 }
16596
791afaa2 16597 std::vector<struct type *> range_types;
639d11d3 16598 child_die = die->child;
c906108c
SS
16599 while (child_die && child_die->tag)
16600 {
16601 if (child_die->tag == DW_TAG_subrange_type)
16602 {
f792889a 16603 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16604
f792889a 16605 if (child_type != NULL)
a02abb62 16606 {
0963b4bd
MS
16607 /* The range type was succesfully read. Save it for the
16608 array type creation. */
791afaa2 16609 range_types.push_back (child_type);
a02abb62 16610 }
c906108c
SS
16611 }
16612 child_die = sibling_die (child_die);
16613 }
16614
16615 /* Dwarf2 dimensions are output from left to right, create the
16616 necessary array types in backwards order. */
7ca2d3a3 16617
c906108c 16618 type = element_type;
7ca2d3a3
DL
16619
16620 if (read_array_order (die, cu) == DW_ORD_col_major)
16621 {
16622 int i = 0;
9a619af0 16623
791afaa2 16624 while (i < range_types.size ())
dc53a7ad 16625 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16626 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16627 }
16628 else
16629 {
791afaa2 16630 size_t ndim = range_types.size ();
7ca2d3a3 16631 while (ndim-- > 0)
dc53a7ad 16632 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16633 byte_stride_prop, bit_stride);
7ca2d3a3 16634 }
c906108c 16635
f5f8a009
EZ
16636 /* Understand Dwarf2 support for vector types (like they occur on
16637 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16638 array type. This is not part of the Dwarf2/3 standard yet, but a
16639 custom vendor extension. The main difference between a regular
16640 array and the vector variant is that vectors are passed by value
16641 to functions. */
e142c38c 16642 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16643 if (attr != nullptr)
ea37ba09 16644 make_vector_type (type);
f5f8a009 16645
dbc98a8b
KW
16646 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16647 implementation may choose to implement triple vectors using this
16648 attribute. */
16649 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16650 if (attr != nullptr)
dbc98a8b
KW
16651 {
16652 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16653 TYPE_LENGTH (type) = DW_UNSND (attr);
16654 else
b98664d3 16655 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16656 "than the total size of elements"));
dbc98a8b
KW
16657 }
16658
39cbfefa
DJ
16659 name = dwarf2_name (die, cu);
16660 if (name)
16661 TYPE_NAME (type) = name;
6e70227d 16662
2b4424c3
TT
16663 maybe_set_alignment (cu, die, type);
16664
0963b4bd 16665 /* Install the type in the die. */
7e314c57
JK
16666 set_die_type (die, type, cu);
16667
16668 /* set_die_type should be already done. */
b4ba55a1
JB
16669 set_descriptive_type (type, die, cu);
16670
7e314c57 16671 return type;
c906108c
SS
16672}
16673
7ca2d3a3 16674static enum dwarf_array_dim_ordering
6e70227d 16675read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16676{
16677 struct attribute *attr;
16678
16679 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16680
435d3d88 16681 if (attr != nullptr)
aead7601 16682 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16683
0963b4bd
MS
16684 /* GNU F77 is a special case, as at 08/2004 array type info is the
16685 opposite order to the dwarf2 specification, but data is still
16686 laid out as per normal fortran.
7ca2d3a3 16687
0963b4bd
MS
16688 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16689 version checking. */
7ca2d3a3 16690
905e0470
PM
16691 if (cu->language == language_fortran
16692 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16693 {
16694 return DW_ORD_row_major;
16695 }
16696
6e70227d 16697 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16698 {
16699 case array_column_major:
16700 return DW_ORD_col_major;
16701 case array_row_major:
16702 default:
16703 return DW_ORD_row_major;
16704 };
16705}
16706
72019c9c 16707/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16708 the DIE's type field. */
72019c9c 16709
f792889a 16710static struct type *
72019c9c
GM
16711read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16712{
7e314c57
JK
16713 struct type *domain_type, *set_type;
16714 struct attribute *attr;
f792889a 16715
7e314c57
JK
16716 domain_type = die_type (die, cu);
16717
16718 /* The die_type call above may have already set the type for this DIE. */
16719 set_type = get_die_type (die, cu);
16720 if (set_type)
16721 return set_type;
16722
16723 set_type = create_set_type (NULL, domain_type);
16724
16725 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16726 if (attr != nullptr)
d09039dd 16727 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16728
2b4424c3
TT
16729 maybe_set_alignment (cu, die, set_type);
16730
f792889a 16731 return set_die_type (die, set_type, cu);
72019c9c 16732}
7ca2d3a3 16733
0971de02
TT
16734/* A helper for read_common_block that creates a locexpr baton.
16735 SYM is the symbol which we are marking as computed.
16736 COMMON_DIE is the DIE for the common block.
16737 COMMON_LOC is the location expression attribute for the common
16738 block itself.
16739 MEMBER_LOC is the location expression attribute for the particular
16740 member of the common block that we are processing.
16741 CU is the CU from which the above come. */
16742
16743static void
16744mark_common_block_symbol_computed (struct symbol *sym,
16745 struct die_info *common_die,
16746 struct attribute *common_loc,
16747 struct attribute *member_loc,
16748 struct dwarf2_cu *cu)
16749{
518817b3
SM
16750 struct dwarf2_per_objfile *dwarf2_per_objfile
16751 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16752 struct objfile *objfile = dwarf2_per_objfile->objfile;
16753 struct dwarf2_locexpr_baton *baton;
16754 gdb_byte *ptr;
16755 unsigned int cu_off;
16756 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16757 LONGEST offset = 0;
16758
16759 gdb_assert (common_loc && member_loc);
16760 gdb_assert (attr_form_is_block (common_loc));
16761 gdb_assert (attr_form_is_block (member_loc)
16762 || attr_form_is_constant (member_loc));
16763
8d749320 16764 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16765 baton->per_cu = cu->per_cu;
16766 gdb_assert (baton->per_cu);
16767
16768 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16769
16770 if (attr_form_is_constant (member_loc))
16771 {
16772 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16773 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16774 }
16775 else
16776 baton->size += DW_BLOCK (member_loc)->size;
16777
224c3ddb 16778 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16779 baton->data = ptr;
16780
16781 *ptr++ = DW_OP_call4;
9c541725 16782 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16783 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16784 ptr += 4;
16785
16786 if (attr_form_is_constant (member_loc))
16787 {
16788 *ptr++ = DW_OP_addr;
16789 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16790 ptr += cu->header.addr_size;
16791 }
16792 else
16793 {
16794 /* We have to copy the data here, because DW_OP_call4 will only
16795 use a DW_AT_location attribute. */
16796 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16797 ptr += DW_BLOCK (member_loc)->size;
16798 }
16799
16800 *ptr++ = DW_OP_plus;
16801 gdb_assert (ptr - baton->data == baton->size);
16802
0971de02 16803 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16804 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16805}
16806
4357ac6c
TT
16807/* Create appropriate locally-scoped variables for all the
16808 DW_TAG_common_block entries. Also create a struct common_block
16809 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16810 is used to separate the common blocks name namespace from regular
4357ac6c 16811 variable names. */
c906108c
SS
16812
16813static void
e7c27a73 16814read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16815{
0971de02
TT
16816 struct attribute *attr;
16817
16818 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16819 if (attr != nullptr)
0971de02
TT
16820 {
16821 /* Support the .debug_loc offsets. */
16822 if (attr_form_is_block (attr))
16823 {
16824 /* Ok. */
16825 }
16826 else if (attr_form_is_section_offset (attr))
16827 {
16828 dwarf2_complex_location_expr_complaint ();
16829 attr = NULL;
16830 }
16831 else
16832 {
16833 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16834 "common block member");
16835 attr = NULL;
16836 }
16837 }
16838
639d11d3 16839 if (die->child != NULL)
c906108c 16840 {
518817b3 16841 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16842 struct die_info *child_die;
16843 size_t n_entries = 0, size;
16844 struct common_block *common_block;
16845 struct symbol *sym;
74ac6d43 16846
4357ac6c
TT
16847 for (child_die = die->child;
16848 child_die && child_die->tag;
16849 child_die = sibling_die (child_die))
16850 ++n_entries;
16851
16852 size = (sizeof (struct common_block)
16853 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16854 common_block
16855 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16856 size);
4357ac6c
TT
16857 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16858 common_block->n_entries = 0;
16859
16860 for (child_die = die->child;
16861 child_die && child_die->tag;
16862 child_die = sibling_die (child_die))
16863 {
16864 /* Create the symbol in the DW_TAG_common_block block in the current
16865 symbol scope. */
e7c27a73 16866 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16867 if (sym != NULL)
16868 {
16869 struct attribute *member_loc;
16870
16871 common_block->contents[common_block->n_entries++] = sym;
16872
16873 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16874 cu);
16875 if (member_loc)
16876 {
16877 /* GDB has handled this for a long time, but it is
16878 not specified by DWARF. It seems to have been
16879 emitted by gfortran at least as recently as:
16880 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16881 complaint (_("Variable in common block has "
0971de02 16882 "DW_AT_data_member_location "
9d8780f0
SM
16883 "- DIE at %s [in module %s]"),
16884 sect_offset_str (child_die->sect_off),
518817b3 16885 objfile_name (objfile));
0971de02
TT
16886
16887 if (attr_form_is_section_offset (member_loc))
16888 dwarf2_complex_location_expr_complaint ();
16889 else if (attr_form_is_constant (member_loc)
16890 || attr_form_is_block (member_loc))
16891 {
435d3d88 16892 if (attr != nullptr)
0971de02
TT
16893 mark_common_block_symbol_computed (sym, die, attr,
16894 member_loc, cu);
16895 }
16896 else
16897 dwarf2_complex_location_expr_complaint ();
16898 }
16899 }
c906108c 16900 }
4357ac6c
TT
16901
16902 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16903 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16904 }
16905}
16906
0114d602 16907/* Create a type for a C++ namespace. */
d9fa45fe 16908
0114d602
DJ
16909static struct type *
16910read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16911{
518817b3 16912 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16913 const char *previous_prefix, *name;
9219021c 16914 int is_anonymous;
0114d602
DJ
16915 struct type *type;
16916
16917 /* For extensions, reuse the type of the original namespace. */
16918 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16919 {
16920 struct die_info *ext_die;
16921 struct dwarf2_cu *ext_cu = cu;
9a619af0 16922
0114d602
DJ
16923 ext_die = dwarf2_extension (die, &ext_cu);
16924 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16925
16926 /* EXT_CU may not be the same as CU.
02142a6c 16927 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16928 return set_die_type (die, type, cu);
16929 }
9219021c 16930
e142c38c 16931 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16932
16933 /* Now build the name of the current namespace. */
16934
0114d602
DJ
16935 previous_prefix = determine_prefix (die, cu);
16936 if (previous_prefix[0] != '\0')
16937 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16938 previous_prefix, name, 0, cu);
0114d602
DJ
16939
16940 /* Create the type. */
19f392bc 16941 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16942
60531b24 16943 return set_die_type (die, type, cu);
0114d602
DJ
16944}
16945
22cee43f 16946/* Read a namespace scope. */
0114d602
DJ
16947
16948static void
16949read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16950{
518817b3 16951 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16952 int is_anonymous;
9219021c 16953
5c4e30ca
DC
16954 /* Add a symbol associated to this if we haven't seen the namespace
16955 before. Also, add a using directive if it's an anonymous
16956 namespace. */
9219021c 16957
f2f0e013 16958 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16959 {
16960 struct type *type;
16961
0114d602 16962 type = read_type_die (die, cu);
e7c27a73 16963 new_symbol (die, type, cu);
5c4e30ca 16964
e8e80198 16965 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16966 if (is_anonymous)
0114d602
DJ
16967 {
16968 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16969
eb1e02fd 16970 std::vector<const char *> excludes;
804d2729 16971 add_using_directive (using_directives (cu),
22cee43f 16972 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16973 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16974 }
5c4e30ca 16975 }
9219021c 16976
639d11d3 16977 if (die->child != NULL)
d9fa45fe 16978 {
639d11d3 16979 struct die_info *child_die = die->child;
6e70227d 16980
d9fa45fe
DC
16981 while (child_die && child_die->tag)
16982 {
e7c27a73 16983 process_die (child_die, cu);
d9fa45fe
DC
16984 child_die = sibling_die (child_die);
16985 }
16986 }
38d518c9
EZ
16987}
16988
f55ee35c
JK
16989/* Read a Fortran module as type. This DIE can be only a declaration used for
16990 imported module. Still we need that type as local Fortran "use ... only"
16991 declaration imports depend on the created type in determine_prefix. */
16992
16993static struct type *
16994read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16995{
518817b3 16996 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16997 const char *module_name;
f55ee35c
JK
16998 struct type *type;
16999
17000 module_name = dwarf2_name (die, cu);
19f392bc 17001 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17002
f55ee35c
JK
17003 return set_die_type (die, type, cu);
17004}
17005
5d7cb8df
JK
17006/* Read a Fortran module. */
17007
17008static void
17009read_module (struct die_info *die, struct dwarf2_cu *cu)
17010{
17011 struct die_info *child_die = die->child;
530e8392
KB
17012 struct type *type;
17013
17014 type = read_type_die (die, cu);
17015 new_symbol (die, type, cu);
5d7cb8df 17016
5d7cb8df
JK
17017 while (child_die && child_die->tag)
17018 {
17019 process_die (child_die, cu);
17020 child_die = sibling_die (child_die);
17021 }
17022}
17023
38d518c9
EZ
17024/* Return the name of the namespace represented by DIE. Set
17025 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17026 namespace. */
17027
17028static const char *
e142c38c 17029namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17030{
17031 struct die_info *current_die;
17032 const char *name = NULL;
17033
17034 /* Loop through the extensions until we find a name. */
17035
17036 for (current_die = die;
17037 current_die != NULL;
f2f0e013 17038 current_die = dwarf2_extension (die, &cu))
38d518c9 17039 {
96553a0c
DE
17040 /* We don't use dwarf2_name here so that we can detect the absence
17041 of a name -> anonymous namespace. */
7d45c7c3 17042 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17043
38d518c9
EZ
17044 if (name != NULL)
17045 break;
17046 }
17047
17048 /* Is it an anonymous namespace? */
17049
17050 *is_anonymous = (name == NULL);
17051 if (*is_anonymous)
2b1dbab0 17052 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17053
17054 return name;
d9fa45fe
DC
17055}
17056
c906108c
SS
17057/* Extract all information from a DW_TAG_pointer_type DIE and add to
17058 the user defined type vector. */
17059
f792889a 17060static struct type *
e7c27a73 17061read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17062{
518817b3
SM
17063 struct gdbarch *gdbarch
17064 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17065 struct comp_unit_head *cu_header = &cu->header;
c906108c 17066 struct type *type;
8b2dbe47
KB
17067 struct attribute *attr_byte_size;
17068 struct attribute *attr_address_class;
17069 int byte_size, addr_class;
7e314c57
JK
17070 struct type *target_type;
17071
17072 target_type = die_type (die, cu);
c906108c 17073
7e314c57
JK
17074 /* The die_type call above may have already set the type for this DIE. */
17075 type = get_die_type (die, cu);
17076 if (type)
17077 return type;
17078
17079 type = lookup_pointer_type (target_type);
8b2dbe47 17080
e142c38c 17081 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17082 if (attr_byte_size)
17083 byte_size = DW_UNSND (attr_byte_size);
c906108c 17084 else
8b2dbe47
KB
17085 byte_size = cu_header->addr_size;
17086
e142c38c 17087 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17088 if (attr_address_class)
17089 addr_class = DW_UNSND (attr_address_class);
17090 else
17091 addr_class = DW_ADDR_none;
17092
2b4424c3
TT
17093 ULONGEST alignment = get_alignment (cu, die);
17094
17095 /* If the pointer size, alignment, or address class is different
17096 than the default, create a type variant marked as such and set
17097 the length accordingly. */
17098 if (TYPE_LENGTH (type) != byte_size
17099 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17100 && alignment != TYPE_RAW_ALIGN (type))
17101 || addr_class != DW_ADDR_none)
c906108c 17102 {
5e2b427d 17103 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17104 {
17105 int type_flags;
17106
849957d9 17107 type_flags = gdbarch_address_class_type_flags
5e2b427d 17108 (gdbarch, byte_size, addr_class);
876cecd0
TT
17109 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17110 == 0);
8b2dbe47
KB
17111 type = make_type_with_address_space (type, type_flags);
17112 }
17113 else if (TYPE_LENGTH (type) != byte_size)
17114 {
b98664d3 17115 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17116 }
2b4424c3
TT
17117 else if (TYPE_RAW_ALIGN (type) != alignment)
17118 {
b98664d3 17119 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17120 " - DIE at %s [in module %s]"),
17121 sect_offset_str (die->sect_off),
17122 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17123 }
6e70227d 17124 else
9a619af0
MS
17125 {
17126 /* Should we also complain about unhandled address classes? */
17127 }
c906108c 17128 }
8b2dbe47
KB
17129
17130 TYPE_LENGTH (type) = byte_size;
2b4424c3 17131 set_type_align (type, alignment);
f792889a 17132 return set_die_type (die, type, cu);
c906108c
SS
17133}
17134
17135/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17136 the user defined type vector. */
17137
f792889a 17138static struct type *
e7c27a73 17139read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17140{
17141 struct type *type;
17142 struct type *to_type;
17143 struct type *domain;
17144
e7c27a73
DJ
17145 to_type = die_type (die, cu);
17146 domain = die_containing_type (die, cu);
0d5de010 17147
7e314c57
JK
17148 /* The calls above may have already set the type for this DIE. */
17149 type = get_die_type (die, cu);
17150 if (type)
17151 return type;
17152
0d5de010
DJ
17153 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17154 type = lookup_methodptr_type (to_type);
7078baeb
TT
17155 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17156 {
518817b3
SM
17157 struct type *new_type
17158 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17159
17160 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17161 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17162 TYPE_VARARGS (to_type));
17163 type = lookup_methodptr_type (new_type);
17164 }
0d5de010
DJ
17165 else
17166 type = lookup_memberptr_type (to_type, domain);
c906108c 17167
f792889a 17168 return set_die_type (die, type, cu);
c906108c
SS
17169}
17170
4297a3f0 17171/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17172 the user defined type vector. */
17173
f792889a 17174static struct type *
4297a3f0
AV
17175read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17176 enum type_code refcode)
c906108c 17177{
e7c27a73 17178 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17179 struct type *type, *target_type;
c906108c
SS
17180 struct attribute *attr;
17181
4297a3f0
AV
17182 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17183
7e314c57
JK
17184 target_type = die_type (die, cu);
17185
17186 /* The die_type call above may have already set the type for this DIE. */
17187 type = get_die_type (die, cu);
17188 if (type)
17189 return type;
17190
4297a3f0 17191 type = lookup_reference_type (target_type, refcode);
e142c38c 17192 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17193 if (attr != nullptr)
c906108c
SS
17194 {
17195 TYPE_LENGTH (type) = DW_UNSND (attr);
17196 }
17197 else
17198 {
107d2387 17199 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17200 }
2b4424c3 17201 maybe_set_alignment (cu, die, type);
f792889a 17202 return set_die_type (die, type, cu);
c906108c
SS
17203}
17204
cf363f18
MW
17205/* Add the given cv-qualifiers to the element type of the array. GCC
17206 outputs DWARF type qualifiers that apply to an array, not the
17207 element type. But GDB relies on the array element type to carry
17208 the cv-qualifiers. This mimics section 6.7.3 of the C99
17209 specification. */
17210
17211static struct type *
17212add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17213 struct type *base_type, int cnst, int voltl)
17214{
17215 struct type *el_type, *inner_array;
17216
17217 base_type = copy_type (base_type);
17218 inner_array = base_type;
17219
17220 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17221 {
17222 TYPE_TARGET_TYPE (inner_array) =
17223 copy_type (TYPE_TARGET_TYPE (inner_array));
17224 inner_array = TYPE_TARGET_TYPE (inner_array);
17225 }
17226
17227 el_type = TYPE_TARGET_TYPE (inner_array);
17228 cnst |= TYPE_CONST (el_type);
17229 voltl |= TYPE_VOLATILE (el_type);
17230 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17231
17232 return set_die_type (die, base_type, cu);
17233}
17234
f792889a 17235static struct type *
e7c27a73 17236read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17237{
f792889a 17238 struct type *base_type, *cv_type;
c906108c 17239
e7c27a73 17240 base_type = die_type (die, cu);
7e314c57
JK
17241
17242 /* The die_type call above may have already set the type for this DIE. */
17243 cv_type = get_die_type (die, cu);
17244 if (cv_type)
17245 return cv_type;
17246
2f608a3a
KW
17247 /* In case the const qualifier is applied to an array type, the element type
17248 is so qualified, not the array type (section 6.7.3 of C99). */
17249 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17250 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17251
f792889a
DJ
17252 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17253 return set_die_type (die, cv_type, cu);
c906108c
SS
17254}
17255
f792889a 17256static struct type *
e7c27a73 17257read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17258{
f792889a 17259 struct type *base_type, *cv_type;
c906108c 17260
e7c27a73 17261 base_type = die_type (die, cu);
7e314c57
JK
17262
17263 /* The die_type call above may have already set the type for this DIE. */
17264 cv_type = get_die_type (die, cu);
17265 if (cv_type)
17266 return cv_type;
17267
cf363f18
MW
17268 /* In case the volatile qualifier is applied to an array type, the
17269 element type is so qualified, not the array type (section 6.7.3
17270 of C99). */
17271 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17272 return add_array_cv_type (die, cu, base_type, 0, 1);
17273
f792889a
DJ
17274 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17275 return set_die_type (die, cv_type, cu);
c906108c
SS
17276}
17277
06d66ee9
TT
17278/* Handle DW_TAG_restrict_type. */
17279
17280static struct type *
17281read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17282{
17283 struct type *base_type, *cv_type;
17284
17285 base_type = die_type (die, cu);
17286
17287 /* The die_type call above may have already set the type for this DIE. */
17288 cv_type = get_die_type (die, cu);
17289 if (cv_type)
17290 return cv_type;
17291
17292 cv_type = make_restrict_type (base_type);
17293 return set_die_type (die, cv_type, cu);
17294}
17295
a2c2acaf
MW
17296/* Handle DW_TAG_atomic_type. */
17297
17298static struct type *
17299read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17300{
17301 struct type *base_type, *cv_type;
17302
17303 base_type = die_type (die, cu);
17304
17305 /* The die_type call above may have already set the type for this DIE. */
17306 cv_type = get_die_type (die, cu);
17307 if (cv_type)
17308 return cv_type;
17309
17310 cv_type = make_atomic_type (base_type);
17311 return set_die_type (die, cv_type, cu);
17312}
17313
c906108c
SS
17314/* Extract all information from a DW_TAG_string_type DIE and add to
17315 the user defined type vector. It isn't really a user defined type,
17316 but it behaves like one, with other DIE's using an AT_user_def_type
17317 attribute to reference it. */
17318
f792889a 17319static struct type *
e7c27a73 17320read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17321{
518817b3 17322 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17323 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17324 struct type *type, *range_type, *index_type, *char_type;
17325 struct attribute *attr;
216a7e6b
AB
17326 struct dynamic_prop prop;
17327 bool length_is_constant = true;
17328 LONGEST length;
17329
17330 /* There are a couple of places where bit sizes might be made use of
17331 when parsing a DW_TAG_string_type, however, no producer that we know
17332 of make use of these. Handling bit sizes that are a multiple of the
17333 byte size is easy enough, but what about other bit sizes? Lets deal
17334 with that problem when we have to. Warn about these attributes being
17335 unsupported, then parse the type and ignore them like we always
17336 have. */
17337 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17338 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17339 {
17340 static bool warning_printed = false;
17341 if (!warning_printed)
17342 {
17343 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17344 "currently supported on DW_TAG_string_type."));
17345 warning_printed = true;
17346 }
17347 }
c906108c 17348
e142c38c 17349 attr = dwarf2_attr (die, DW_AT_string_length, cu);
216a7e6b
AB
17350 if (attr != nullptr && !attr_form_is_constant (attr))
17351 {
17352 /* The string length describes the location at which the length of
17353 the string can be found. The size of the length field can be
17354 specified with one of the attributes below. */
17355 struct type *prop_type;
17356 struct attribute *len
17357 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17358 if (len == nullptr)
17359 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17360 if (len != nullptr && attr_form_is_constant (len))
17361 {
17362 /* Pass 0 as the default as we know this attribute is constant
17363 and the default value will not be returned. */
17364 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17365 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17366 }
17367 else
17368 {
17369 /* If the size is not specified then we assume it is the size of
17370 an address on this target. */
17371 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17372 }
17373
17374 /* Convert the attribute into a dynamic property. */
17375 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17376 length = 1;
17377 else
17378 length_is_constant = false;
17379 }
17380 else if (attr != nullptr)
17381 {
17382 /* This DW_AT_string_length just contains the length with no
17383 indirection. There's no need to create a dynamic property in this
17384 case. Pass 0 for the default value as we know it will not be
17385 returned in this case. */
17386 length = dwarf2_get_attr_constant_value (attr, 0);
17387 }
17388 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17389 {
216a7e6b
AB
17390 /* We don't currently support non-constant byte sizes for strings. */
17391 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17392 }
17393 else
17394 {
216a7e6b
AB
17395 /* Use 1 as a fallback length if we have nothing else. */
17396 length = 1;
c906108c 17397 }
6ccb9162 17398
46bf5051 17399 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17400 if (length_is_constant)
17401 range_type = create_static_range_type (NULL, index_type, 1, length);
17402 else
17403 {
17404 struct dynamic_prop low_bound;
17405
17406 low_bound.kind = PROP_CONST;
17407 low_bound.data.const_val = 1;
17408 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17409 }
3b7538c0
UW
17410 char_type = language_string_char_type (cu->language_defn, gdbarch);
17411 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17412
f792889a 17413 return set_die_type (die, type, cu);
c906108c
SS
17414}
17415
4d804846
JB
17416/* Assuming that DIE corresponds to a function, returns nonzero
17417 if the function is prototyped. */
17418
17419static int
17420prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17421{
17422 struct attribute *attr;
17423
17424 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17425 if (attr && (DW_UNSND (attr) != 0))
17426 return 1;
17427
17428 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17429 is only meaningful for C, but the concept also extends to other
4d804846
JB
17430 languages that allow unprototyped functions (Eg: Objective C).
17431 For all other languages, assume that functions are always
17432 prototyped. */
17433 if (cu->language != language_c
17434 && cu->language != language_objc
17435 && cu->language != language_opencl)
17436 return 1;
17437
17438 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17439 prototyped and unprototyped functions; default to prototyped,
17440 since that is more common in modern code (and RealView warns
17441 about unprototyped functions). */
17442 if (producer_is_realview (cu->producer))
17443 return 1;
17444
17445 return 0;
17446}
17447
c906108c
SS
17448/* Handle DIES due to C code like:
17449
17450 struct foo
c5aa993b
JM
17451 {
17452 int (*funcp)(int a, long l);
17453 int b;
17454 };
c906108c 17455
0963b4bd 17456 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17457
f792889a 17458static struct type *
e7c27a73 17459read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17460{
518817b3 17461 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17462 struct type *type; /* Type that this function returns. */
17463 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17464 struct attribute *attr;
17465
e7c27a73 17466 type = die_type (die, cu);
7e314c57
JK
17467
17468 /* The die_type call above may have already set the type for this DIE. */
17469 ftype = get_die_type (die, cu);
17470 if (ftype)
17471 return ftype;
17472
0c8b41f1 17473 ftype = lookup_function_type (type);
c906108c 17474
4d804846 17475 if (prototyped_function_p (die, cu))
a6c727b2 17476 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17477
c055b101
CV
17478 /* Store the calling convention in the type if it's available in
17479 the subroutine die. Otherwise set the calling convention to
17480 the default value DW_CC_normal. */
17481 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17482 if (attr != nullptr
17483 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17484 TYPE_CALLING_CONVENTION (ftype)
17485 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17486 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17487 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17488 else
17489 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17490
743649fd
MW
17491 /* Record whether the function returns normally to its caller or not
17492 if the DWARF producer set that information. */
17493 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17494 if (attr && (DW_UNSND (attr) != 0))
17495 TYPE_NO_RETURN (ftype) = 1;
17496
76c10ea2
GM
17497 /* We need to add the subroutine type to the die immediately so
17498 we don't infinitely recurse when dealing with parameters
0963b4bd 17499 declared as the same subroutine type. */
76c10ea2 17500 set_die_type (die, ftype, cu);
6e70227d 17501
639d11d3 17502 if (die->child != NULL)
c906108c 17503 {
bb5ed363 17504 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17505 struct die_info *child_die;
8072405b 17506 int nparams, iparams;
c906108c
SS
17507
17508 /* Count the number of parameters.
17509 FIXME: GDB currently ignores vararg functions, but knows about
17510 vararg member functions. */
8072405b 17511 nparams = 0;
639d11d3 17512 child_die = die->child;
c906108c
SS
17513 while (child_die && child_die->tag)
17514 {
17515 if (child_die->tag == DW_TAG_formal_parameter)
17516 nparams++;
17517 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17518 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17519 child_die = sibling_die (child_die);
17520 }
17521
17522 /* Allocate storage for parameters and fill them in. */
17523 TYPE_NFIELDS (ftype) = nparams;
17524 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17525 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17526
8072405b
JK
17527 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17528 even if we error out during the parameters reading below. */
17529 for (iparams = 0; iparams < nparams; iparams++)
17530 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17531
17532 iparams = 0;
639d11d3 17533 child_die = die->child;
c906108c
SS
17534 while (child_die && child_die->tag)
17535 {
17536 if (child_die->tag == DW_TAG_formal_parameter)
17537 {
3ce3b1ba
PA
17538 struct type *arg_type;
17539
17540 /* DWARF version 2 has no clean way to discern C++
17541 static and non-static member functions. G++ helps
17542 GDB by marking the first parameter for non-static
17543 member functions (which is the this pointer) as
17544 artificial. We pass this information to
17545 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17546
17547 DWARF version 3 added DW_AT_object_pointer, which GCC
17548 4.5 does not yet generate. */
e142c38c 17549 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17550 if (attr != nullptr)
c906108c
SS
17551 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17552 else
9c37b5ae 17553 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17554 arg_type = die_type (child_die, cu);
17555
17556 /* RealView does not mark THIS as const, which the testsuite
17557 expects. GCC marks THIS as const in method definitions,
17558 but not in the class specifications (GCC PR 43053). */
17559 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17560 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17561 {
17562 int is_this = 0;
17563 struct dwarf2_cu *arg_cu = cu;
17564 const char *name = dwarf2_name (child_die, cu);
17565
17566 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17567 if (attr != nullptr)
3ce3b1ba
PA
17568 {
17569 /* If the compiler emits this, use it. */
17570 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17571 is_this = 1;
17572 }
17573 else if (name && strcmp (name, "this") == 0)
17574 /* Function definitions will have the argument names. */
17575 is_this = 1;
17576 else if (name == NULL && iparams == 0)
17577 /* Declarations may not have the names, so like
17578 elsewhere in GDB, assume an artificial first
17579 argument is "this". */
17580 is_this = 1;
17581
17582 if (is_this)
17583 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17584 arg_type, 0);
17585 }
17586
17587 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17588 iparams++;
17589 }
17590 child_die = sibling_die (child_die);
17591 }
17592 }
17593
76c10ea2 17594 return ftype;
c906108c
SS
17595}
17596
f792889a 17597static struct type *
e7c27a73 17598read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17599{
518817b3 17600 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17601 const char *name = NULL;
3c8e0968 17602 struct type *this_type, *target_type;
c906108c 17603
94af9270 17604 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17605 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17606 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17607 set_die_type (die, this_type, cu);
3c8e0968
DE
17608 target_type = die_type (die, cu);
17609 if (target_type != this_type)
17610 TYPE_TARGET_TYPE (this_type) = target_type;
17611 else
17612 {
17613 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17614 spec and cause infinite loops in GDB. */
b98664d3 17615 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17616 "- DIE at %s [in module %s]"),
17617 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17618 TYPE_TARGET_TYPE (this_type) = NULL;
17619 }
f792889a 17620 return this_type;
c906108c
SS
17621}
17622
9b790ce7
UW
17623/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17624 (which may be different from NAME) to the architecture back-end to allow
17625 it to guess the correct format if necessary. */
17626
17627static struct type *
17628dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17629 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17630{
17631 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17632 const struct floatformat **format;
17633 struct type *type;
17634
17635 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17636 if (format)
103a685e 17637 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17638 else
77b7c781 17639 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17640
17641 return type;
17642}
17643
eb77c9df
AB
17644/* Allocate an integer type of size BITS and name NAME. */
17645
17646static struct type *
17647dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17648 int bits, int unsigned_p, const char *name)
17649{
17650 struct type *type;
17651
17652 /* Versions of Intel's C Compiler generate an integer type called "void"
17653 instead of using DW_TAG_unspecified_type. This has been seen on
17654 at least versions 14, 17, and 18. */
35ee2dc2
AB
17655 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17656 && strcmp (name, "void") == 0)
eb77c9df
AB
17657 type = objfile_type (objfile)->builtin_void;
17658 else
17659 type = init_integer_type (objfile, bits, unsigned_p, name);
17660
17661 return type;
17662}
17663
8bdc1658
AB
17664/* Initialise and return a floating point type of size BITS suitable for
17665 use as a component of a complex number. The NAME_HINT is passed through
17666 when initialising the floating point type and is the name of the complex
17667 type.
17668
17669 As DWARF doesn't currently provide an explicit name for the components
17670 of a complex number, but it can be helpful to have these components
17671 named, we try to select a suitable name based on the size of the
17672 component. */
17673static struct type *
17674dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17675 struct objfile *objfile,
103a685e
TT
17676 int bits, const char *name_hint,
17677 enum bfd_endian byte_order)
8bdc1658
AB
17678{
17679 gdbarch *gdbarch = get_objfile_arch (objfile);
17680 struct type *tt = nullptr;
17681
35add35e
AB
17682 /* Try to find a suitable floating point builtin type of size BITS.
17683 We're going to use the name of this type as the name for the complex
17684 target type that we are about to create. */
1db455a7 17685 switch (cu->language)
8bdc1658 17686 {
1db455a7
AB
17687 case language_fortran:
17688 switch (bits)
17689 {
17690 case 32:
17691 tt = builtin_f_type (gdbarch)->builtin_real;
17692 break;
17693 case 64:
17694 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17695 break;
17696 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17697 case 128:
17698 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17699 break;
17700 }
8bdc1658 17701 break;
1db455a7
AB
17702 default:
17703 switch (bits)
17704 {
17705 case 32:
17706 tt = builtin_type (gdbarch)->builtin_float;
17707 break;
17708 case 64:
17709 tt = builtin_type (gdbarch)->builtin_double;
17710 break;
17711 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17712 case 128:
17713 tt = builtin_type (gdbarch)->builtin_long_double;
17714 break;
17715 }
8bdc1658
AB
17716 break;
17717 }
17718
35add35e
AB
17719 /* If the type we found doesn't match the size we were looking for, then
17720 pretend we didn't find a type at all, the complex target type we
17721 create will then be nameless. */
a12e5744 17722 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17723 tt = nullptr;
17724
8bdc1658 17725 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17726 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17727}
17728
c906108c
SS
17729/* Find a representation of a given base type and install
17730 it in the TYPE field of the die. */
17731
f792889a 17732static struct type *
e7c27a73 17733read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17734{
518817b3 17735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17736 struct type *type;
17737 struct attribute *attr;
19f392bc 17738 int encoding = 0, bits = 0;
15d034d0 17739 const char *name;
34877895 17740 gdbarch *arch;
c906108c 17741
e142c38c 17742 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17743 if (attr != nullptr)
34877895 17744 encoding = DW_UNSND (attr);
e142c38c 17745 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17746 if (attr != nullptr)
34877895 17747 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17748 name = dwarf2_name (die, cu);
6ccb9162 17749 if (!name)
34877895 17750 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17751
17752 arch = get_objfile_arch (objfile);
17753 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17754
34877895
PJ
17755 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17756 if (attr)
103a685e
TT
17757 {
17758 int endianity = DW_UNSND (attr);
17759
17760 switch (endianity)
17761 {
17762 case DW_END_big:
17763 byte_order = BFD_ENDIAN_BIG;
17764 break;
17765 case DW_END_little:
17766 byte_order = BFD_ENDIAN_LITTLE;
17767 break;
17768 default:
17769 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17770 break;
17771 }
17772 }
6ccb9162
UW
17773
17774 switch (encoding)
c906108c 17775 {
6ccb9162
UW
17776 case DW_ATE_address:
17777 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17778 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17779 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17780 break;
17781 case DW_ATE_boolean:
19f392bc 17782 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17783 break;
17784 case DW_ATE_complex_float:
103a685e
TT
17785 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17786 byte_order);
19f392bc 17787 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17788 break;
17789 case DW_ATE_decimal_float:
19f392bc 17790 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17791 break;
17792 case DW_ATE_float:
103a685e 17793 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17794 break;
17795 case DW_ATE_signed:
eb77c9df 17796 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17797 break;
17798 case DW_ATE_unsigned:
3b2b8fea
TT
17799 if (cu->language == language_fortran
17800 && name
61012eef 17801 && startswith (name, "character("))
19f392bc
UW
17802 type = init_character_type (objfile, bits, 1, name);
17803 else
eb77c9df 17804 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17805 break;
17806 case DW_ATE_signed_char:
6e70227d 17807 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17808 || cu->language == language_pascal
17809 || cu->language == language_fortran)
19f392bc
UW
17810 type = init_character_type (objfile, bits, 0, name);
17811 else
eb77c9df 17812 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17813 break;
17814 case DW_ATE_unsigned_char:
868a0084 17815 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17816 || cu->language == language_pascal
c44af4eb
TT
17817 || cu->language == language_fortran
17818 || cu->language == language_rust)
19f392bc
UW
17819 type = init_character_type (objfile, bits, 1, name);
17820 else
eb77c9df 17821 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17822 break;
75079b2b 17823 case DW_ATE_UTF:
53e710ac 17824 {
53e710ac
PA
17825 if (bits == 16)
17826 type = builtin_type (arch)->builtin_char16;
17827 else if (bits == 32)
17828 type = builtin_type (arch)->builtin_char32;
17829 else
17830 {
b98664d3 17831 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17832 bits);
eb77c9df 17833 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17834 }
17835 return set_die_type (die, type, cu);
17836 }
75079b2b
TT
17837 break;
17838
6ccb9162 17839 default:
b98664d3 17840 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17841 dwarf_type_encoding_name (encoding));
77b7c781 17842 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17843 break;
c906108c 17844 }
6ccb9162 17845
0114d602 17846 if (name && strcmp (name, "char") == 0)
876cecd0 17847 TYPE_NOSIGN (type) = 1;
0114d602 17848
2b4424c3
TT
17849 maybe_set_alignment (cu, die, type);
17850
103a685e 17851 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17852
f792889a 17853 return set_die_type (die, type, cu);
c906108c
SS
17854}
17855
80180f79
SA
17856/* Parse dwarf attribute if it's a block, reference or constant and put the
17857 resulting value of the attribute into struct bound_prop.
17858 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17859
17860static int
17861attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17862 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17863 struct type *default_type)
80180f79
SA
17864{
17865 struct dwarf2_property_baton *baton;
518817b3
SM
17866 struct obstack *obstack
17867 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17868
9a49df9d
AB
17869 gdb_assert (default_type != NULL);
17870
80180f79
SA
17871 if (attr == NULL || prop == NULL)
17872 return 0;
17873
17874 if (attr_form_is_block (attr))
17875 {
8d749320 17876 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17877 baton->property_type = default_type;
80180f79
SA
17878 baton->locexpr.per_cu = cu->per_cu;
17879 baton->locexpr.size = DW_BLOCK (attr)->size;
17880 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17881 switch (attr->name)
17882 {
17883 case DW_AT_string_length:
17884 baton->locexpr.is_reference = true;
17885 break;
17886 default:
17887 baton->locexpr.is_reference = false;
17888 break;
17889 }
80180f79
SA
17890 prop->data.baton = baton;
17891 prop->kind = PROP_LOCEXPR;
17892 gdb_assert (prop->data.baton != NULL);
17893 }
17894 else if (attr_form_is_ref (attr))
17895 {
17896 struct dwarf2_cu *target_cu = cu;
17897 struct die_info *target_die;
17898 struct attribute *target_attr;
17899
17900 target_die = follow_die_ref (die, attr, &target_cu);
17901 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17902 if (target_attr == NULL)
17903 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17904 target_cu);
80180f79
SA
17905 if (target_attr == NULL)
17906 return 0;
17907
df25ebbd 17908 switch (target_attr->name)
80180f79 17909 {
df25ebbd
JB
17910 case DW_AT_location:
17911 if (attr_form_is_section_offset (target_attr))
17912 {
8d749320 17913 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17914 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17915 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17916 prop->data.baton = baton;
17917 prop->kind = PROP_LOCLIST;
17918 gdb_assert (prop->data.baton != NULL);
17919 }
17920 else if (attr_form_is_block (target_attr))
17921 {
8d749320 17922 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17923 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17924 baton->locexpr.per_cu = cu->per_cu;
17925 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17926 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17927 baton->locexpr.is_reference = true;
df25ebbd
JB
17928 prop->data.baton = baton;
17929 prop->kind = PROP_LOCEXPR;
17930 gdb_assert (prop->data.baton != NULL);
17931 }
17932 else
17933 {
17934 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17935 "dynamic property");
17936 return 0;
17937 }
17938 break;
17939 case DW_AT_data_member_location:
17940 {
17941 LONGEST offset;
17942
17943 if (!handle_data_member_location (target_die, target_cu,
17944 &offset))
17945 return 0;
17946
8d749320 17947 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17948 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17949 target_cu);
df25ebbd
JB
17950 baton->offset_info.offset = offset;
17951 baton->offset_info.type = die_type (target_die, target_cu);
17952 prop->data.baton = baton;
17953 prop->kind = PROP_ADDR_OFFSET;
17954 break;
17955 }
80180f79
SA
17956 }
17957 }
17958 else if (attr_form_is_constant (attr))
17959 {
17960 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17961 prop->kind = PROP_CONST;
17962 }
17963 else
17964 {
17965 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17966 dwarf2_name (die, cu));
17967 return 0;
17968 }
17969
17970 return 1;
17971}
17972
11a8b164
AB
17973/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17974 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
17975
17976static struct type *
11a8b164
AB
17977dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17978 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
17979{
17980 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
17981 struct type *int_type;
17982
17983 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17984#define TRY_TYPE(F) \
17985 int_type = (unsigned_p \
17986 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17987 : objfile_type (objfile)->builtin_ ## F); \
17988 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17989 return int_type
17990
17991 TRY_TYPE (char);
17992 TRY_TYPE (short);
17993 TRY_TYPE (int);
17994 TRY_TYPE (long);
17995 TRY_TYPE (long_long);
17996
17997#undef TRY_TYPE
17998
17999 gdb_assert_not_reached ("unable to find suitable integer type");
18000}
18001
11a8b164
AB
18002/* Find an integer type the same size as the address size given in the
18003 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18004 is unsigned or not. */
18005
18006static struct type *
18007dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18008 bool unsigned_p)
18009{
18010 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18011 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18012}
18013
b86352cf
AB
18014/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18015 present (which is valid) then compute the default type based on the
18016 compilation units address size. */
18017
18018static struct type *
18019read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18020{
18021 struct type *index_type = die_type (die, cu);
18022
18023 /* Dwarf-2 specifications explicitly allows to create subrange types
18024 without specifying a base type.
18025 In that case, the base type must be set to the type of
18026 the lower bound, upper bound or count, in that order, if any of these
18027 three attributes references an object that has a type.
18028 If no base type is found, the Dwarf-2 specifications say that
18029 a signed integer type of size equal to the size of an address should
18030 be used.
18031 For the following C code: `extern char gdb_int [];'
18032 GCC produces an empty range DIE.
18033 FIXME: muller/2010-05-28: Possible references to object for low bound,
18034 high bound or count are not yet handled by this code. */
18035 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 18036 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
18037
18038 return index_type;
18039}
18040
a02abb62
JB
18041/* Read the given DW_AT_subrange DIE. */
18042
f792889a 18043static struct type *
a02abb62
JB
18044read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18045{
4c9ad8c2 18046 struct type *base_type, *orig_base_type;
a02abb62
JB
18047 struct type *range_type;
18048 struct attribute *attr;
729efb13 18049 struct dynamic_prop low, high;
4fae6e18 18050 int low_default_is_valid;
c451ebe5 18051 int high_bound_is_count = 0;
15d034d0 18052 const char *name;
d359392f 18053 ULONGEST negative_mask;
e77813c8 18054
b86352cf
AB
18055 orig_base_type = read_subrange_index_type (die, cu);
18056
4c9ad8c2
TT
18057 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18058 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18059 creating the range type, but we use the result of check_typedef
18060 when examining properties of the type. */
18061 base_type = check_typedef (orig_base_type);
a02abb62 18062
7e314c57
JK
18063 /* The die_type call above may have already set the type for this DIE. */
18064 range_type = get_die_type (die, cu);
18065 if (range_type)
18066 return range_type;
18067
729efb13
SA
18068 low.kind = PROP_CONST;
18069 high.kind = PROP_CONST;
18070 high.data.const_val = 0;
18071
4fae6e18
JK
18072 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18073 omitting DW_AT_lower_bound. */
18074 switch (cu->language)
6e70227d 18075 {
4fae6e18
JK
18076 case language_c:
18077 case language_cplus:
729efb13 18078 low.data.const_val = 0;
4fae6e18
JK
18079 low_default_is_valid = 1;
18080 break;
18081 case language_fortran:
729efb13 18082 low.data.const_val = 1;
4fae6e18
JK
18083 low_default_is_valid = 1;
18084 break;
18085 case language_d:
4fae6e18 18086 case language_objc:
c44af4eb 18087 case language_rust:
729efb13 18088 low.data.const_val = 0;
4fae6e18
JK
18089 low_default_is_valid = (cu->header.version >= 4);
18090 break;
18091 case language_ada:
18092 case language_m2:
18093 case language_pascal:
729efb13 18094 low.data.const_val = 1;
4fae6e18
JK
18095 low_default_is_valid = (cu->header.version >= 4);
18096 break;
18097 default:
729efb13 18098 low.data.const_val = 0;
4fae6e18
JK
18099 low_default_is_valid = 0;
18100 break;
a02abb62
JB
18101 }
18102
e142c38c 18103 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18104 if (attr != nullptr)
9a49df9d 18105 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18106 else if (!low_default_is_valid)
b98664d3 18107 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18108 "- DIE at %s [in module %s]"),
18109 sect_offset_str (die->sect_off),
518817b3 18110 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18111
506f5c41
TV
18112 struct attribute *attr_ub, *attr_count;
18113 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18114 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18115 {
506f5c41 18116 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18117 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18118 {
c451ebe5
SA
18119 /* If bounds are constant do the final calculation here. */
18120 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18121 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18122 else
18123 high_bound_is_count = 1;
c2ff108b 18124 }
506f5c41
TV
18125 else
18126 {
18127 if (attr_ub != NULL)
18128 complaint (_("Unresolved DW_AT_upper_bound "
18129 "- DIE at %s [in module %s]"),
18130 sect_offset_str (die->sect_off),
18131 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18132 if (attr_count != NULL)
18133 complaint (_("Unresolved DW_AT_count "
18134 "- DIE at %s [in module %s]"),
18135 sect_offset_str (die->sect_off),
18136 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18137 }
e77813c8 18138 }
a02abb62 18139
4e962e74
TT
18140 LONGEST bias = 0;
18141 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18142 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18143 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18144
dbb9c2b1
JB
18145 /* Normally, the DWARF producers are expected to use a signed
18146 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18147 But this is unfortunately not always the case, as witnessed
18148 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18149 is used instead. To work around that ambiguity, we treat
18150 the bounds as signed, and thus sign-extend their values, when
18151 the base type is signed. */
6e70227d 18152 negative_mask =
d359392f 18153 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18154 if (low.kind == PROP_CONST
18155 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18156 low.data.const_val |= negative_mask;
18157 if (high.kind == PROP_CONST
18158 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18159 high.data.const_val |= negative_mask;
43bbcdc2 18160
5bbd8269
AB
18161 /* Check for bit and byte strides. */
18162 struct dynamic_prop byte_stride_prop;
18163 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18164 if (attr_byte_stride != nullptr)
18165 {
18166 struct type *prop_type
18167 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18168 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18169 prop_type);
18170 }
18171
18172 struct dynamic_prop bit_stride_prop;
18173 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18174 if (attr_bit_stride != nullptr)
18175 {
18176 /* It only makes sense to have either a bit or byte stride. */
18177 if (attr_byte_stride != nullptr)
18178 {
18179 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18180 "- DIE at %s [in module %s]"),
18181 sect_offset_str (die->sect_off),
18182 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18183 attr_bit_stride = nullptr;
18184 }
18185 else
18186 {
18187 struct type *prop_type
18188 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18189 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18190 prop_type);
18191 }
18192 }
18193
18194 if (attr_byte_stride != nullptr
18195 || attr_bit_stride != nullptr)
18196 {
18197 bool byte_stride_p = (attr_byte_stride != nullptr);
18198 struct dynamic_prop *stride
18199 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18200
18201 range_type
18202 = create_range_type_with_stride (NULL, orig_base_type, &low,
18203 &high, bias, stride, byte_stride_p);
18204 }
18205 else
18206 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18207
c451ebe5
SA
18208 if (high_bound_is_count)
18209 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18210
c2ff108b
JK
18211 /* Ada expects an empty array on no boundary attributes. */
18212 if (attr == NULL && cu->language != language_ada)
729efb13 18213 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18214
39cbfefa
DJ
18215 name = dwarf2_name (die, cu);
18216 if (name)
18217 TYPE_NAME (range_type) = name;
6e70227d 18218
e142c38c 18219 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18220 if (attr != nullptr)
a02abb62
JB
18221 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18222
2b4424c3
TT
18223 maybe_set_alignment (cu, die, range_type);
18224
7e314c57
JK
18225 set_die_type (die, range_type, cu);
18226
18227 /* set_die_type should be already done. */
b4ba55a1
JB
18228 set_descriptive_type (range_type, die, cu);
18229
7e314c57 18230 return range_type;
a02abb62 18231}
6e70227d 18232
f792889a 18233static struct type *
81a17f79
JB
18234read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18235{
18236 struct type *type;
81a17f79 18237
518817b3
SM
18238 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18239 NULL);
0114d602 18240 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18241
74a2f8ff 18242 /* In Ada, an unspecified type is typically used when the description
85102364 18243 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18244 such a type, we treat it as a stub, and try to resolve it later on,
18245 when needed. */
18246 if (cu->language == language_ada)
18247 TYPE_STUB (type) = 1;
18248
f792889a 18249 return set_die_type (die, type, cu);
81a17f79 18250}
a02abb62 18251
639d11d3
DC
18252/* Read a single die and all its descendents. Set the die's sibling
18253 field to NULL; set other fields in the die correctly, and set all
18254 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18255 location of the info_ptr after reading all of those dies. PARENT
18256 is the parent of the die in question. */
18257
18258static struct die_info *
dee91e82 18259read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18260 const gdb_byte *info_ptr,
18261 const gdb_byte **new_info_ptr,
dee91e82 18262 struct die_info *parent)
639d11d3
DC
18263{
18264 struct die_info *die;
d521ce57 18265 const gdb_byte *cur_ptr;
639d11d3
DC
18266 int has_children;
18267
bf6af496 18268 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18269 if (die == NULL)
18270 {
18271 *new_info_ptr = cur_ptr;
18272 return NULL;
18273 }
93311388 18274 store_in_ref_table (die, reader->cu);
639d11d3
DC
18275
18276 if (has_children)
bf6af496 18277 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18278 else
18279 {
18280 die->child = NULL;
18281 *new_info_ptr = cur_ptr;
18282 }
18283
18284 die->sibling = NULL;
18285 die->parent = parent;
18286 return die;
18287}
18288
18289/* Read a die, all of its descendents, and all of its siblings; set
18290 all of the fields of all of the dies correctly. Arguments are as
18291 in read_die_and_children. */
18292
18293static struct die_info *
bf6af496 18294read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18295 const gdb_byte *info_ptr,
18296 const gdb_byte **new_info_ptr,
bf6af496 18297 struct die_info *parent)
639d11d3
DC
18298{
18299 struct die_info *first_die, *last_sibling;
d521ce57 18300 const gdb_byte *cur_ptr;
639d11d3 18301
c906108c 18302 cur_ptr = info_ptr;
639d11d3
DC
18303 first_die = last_sibling = NULL;
18304
18305 while (1)
c906108c 18306 {
639d11d3 18307 struct die_info *die
dee91e82 18308 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18309
1d325ec1 18310 if (die == NULL)
c906108c 18311 {
639d11d3
DC
18312 *new_info_ptr = cur_ptr;
18313 return first_die;
c906108c 18314 }
1d325ec1
DJ
18315
18316 if (!first_die)
18317 first_die = die;
c906108c 18318 else
1d325ec1
DJ
18319 last_sibling->sibling = die;
18320
18321 last_sibling = die;
c906108c 18322 }
c906108c
SS
18323}
18324
bf6af496
DE
18325/* Read a die, all of its descendents, and all of its siblings; set
18326 all of the fields of all of the dies correctly. Arguments are as
18327 in read_die_and_children.
18328 This the main entry point for reading a DIE and all its children. */
18329
18330static struct die_info *
18331read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18332 const gdb_byte *info_ptr,
18333 const gdb_byte **new_info_ptr,
bf6af496
DE
18334 struct die_info *parent)
18335{
18336 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18337 new_info_ptr, parent);
18338
b4f54984 18339 if (dwarf_die_debug)
bf6af496
DE
18340 {
18341 fprintf_unfiltered (gdb_stdlog,
18342 "Read die from %s@0x%x of %s:\n",
a32a8923 18343 get_section_name (reader->die_section),
bf6af496
DE
18344 (unsigned) (info_ptr - reader->die_section->buffer),
18345 bfd_get_filename (reader->abfd));
b4f54984 18346 dump_die (die, dwarf_die_debug);
bf6af496
DE
18347 }
18348
18349 return die;
18350}
18351
3019eac3
DE
18352/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18353 attributes.
18354 The caller is responsible for filling in the extra attributes
18355 and updating (*DIEP)->num_attrs.
18356 Set DIEP to point to a newly allocated die with its information,
18357 except for its child, sibling, and parent fields.
18358 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18359
d521ce57 18360static const gdb_byte *
3019eac3 18361read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18362 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18363 int *has_children, int num_extra_attrs)
93311388 18364{
b64f50a1 18365 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18366 struct abbrev_info *abbrev;
18367 struct die_info *die;
18368 struct dwarf2_cu *cu = reader->cu;
18369 bfd *abfd = reader->abfd;
18370
9c541725 18371 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18372 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18373 info_ptr += bytes_read;
18374 if (!abbrev_number)
18375 {
18376 *diep = NULL;
18377 *has_children = 0;
18378 return info_ptr;
18379 }
18380
685af9cd 18381 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18382 if (!abbrev)
348e048f
DE
18383 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18384 abbrev_number,
18385 bfd_get_filename (abfd));
18386
3019eac3 18387 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18388 die->sect_off = sect_off;
93311388
DE
18389 die->tag = abbrev->tag;
18390 die->abbrev = abbrev_number;
18391
3019eac3
DE
18392 /* Make the result usable.
18393 The caller needs to update num_attrs after adding the extra
18394 attributes. */
93311388
DE
18395 die->num_attrs = abbrev->num_attrs;
18396
18a8505e 18397 std::vector<int> indexes_that_need_reprocess;
93311388 18398 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18399 {
18400 bool need_reprocess;
18401 info_ptr =
18402 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18403 info_ptr, &need_reprocess);
18404 if (need_reprocess)
18405 indexes_that_need_reprocess.push_back (i);
18406 }
18407
18408 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18409 if (attr != nullptr)
18410 cu->str_offsets_base = DW_UNSND (attr);
93311388 18411
18a8505e
AT
18412 auto maybe_addr_base = lookup_addr_base(die);
18413 if (maybe_addr_base.has_value ())
18414 cu->addr_base = *maybe_addr_base;
18415 for (int index : indexes_that_need_reprocess)
18416 read_attribute_reprocess (reader, &die->attrs[index]);
93311388
DE
18417 *diep = die;
18418 *has_children = abbrev->has_children;
18419 return info_ptr;
18420}
18421
3019eac3
DE
18422/* Read a die and all its attributes.
18423 Set DIEP to point to a newly allocated die with its information,
18424 except for its child, sibling, and parent fields.
18425 Set HAS_CHILDREN to tell whether the die has children or not. */
18426
d521ce57 18427static const gdb_byte *
3019eac3 18428read_full_die (const struct die_reader_specs *reader,
d521ce57 18429 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18430 int *has_children)
18431{
d521ce57 18432 const gdb_byte *result;
bf6af496
DE
18433
18434 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18435
b4f54984 18436 if (dwarf_die_debug)
bf6af496
DE
18437 {
18438 fprintf_unfiltered (gdb_stdlog,
18439 "Read die from %s@0x%x of %s:\n",
a32a8923 18440 get_section_name (reader->die_section),
bf6af496
DE
18441 (unsigned) (info_ptr - reader->die_section->buffer),
18442 bfd_get_filename (reader->abfd));
b4f54984 18443 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18444 }
18445
18446 return result;
3019eac3 18447}
433df2d4
DE
18448\f
18449/* Abbreviation tables.
3019eac3 18450
433df2d4 18451 In DWARF version 2, the description of the debugging information is
c906108c
SS
18452 stored in a separate .debug_abbrev section. Before we read any
18453 dies from a section we read in all abbreviations and install them
433df2d4
DE
18454 in a hash table. */
18455
18456/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18457
685af9cd
TT
18458struct abbrev_info *
18459abbrev_table::alloc_abbrev ()
433df2d4
DE
18460{
18461 struct abbrev_info *abbrev;
18462
685af9cd 18463 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18464 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18465
433df2d4
DE
18466 return abbrev;
18467}
18468
18469/* Add an abbreviation to the table. */
c906108c 18470
685af9cd
TT
18471void
18472abbrev_table::add_abbrev (unsigned int abbrev_number,
18473 struct abbrev_info *abbrev)
433df2d4
DE
18474{
18475 unsigned int hash_number;
18476
18477 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18478 abbrev->next = m_abbrevs[hash_number];
18479 m_abbrevs[hash_number] = abbrev;
433df2d4 18480}
dee91e82 18481
433df2d4
DE
18482/* Look up an abbrev in the table.
18483 Returns NULL if the abbrev is not found. */
18484
685af9cd
TT
18485struct abbrev_info *
18486abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18487{
433df2d4
DE
18488 unsigned int hash_number;
18489 struct abbrev_info *abbrev;
18490
18491 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18492 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18493
18494 while (abbrev)
18495 {
18496 if (abbrev->number == abbrev_number)
18497 return abbrev;
18498 abbrev = abbrev->next;
18499 }
18500 return NULL;
18501}
18502
18503/* Read in an abbrev table. */
18504
685af9cd 18505static abbrev_table_up
ed2dc618
SM
18506abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18507 struct dwarf2_section_info *section,
9c541725 18508 sect_offset sect_off)
433df2d4
DE
18509{
18510 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18511 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18512 const gdb_byte *abbrev_ptr;
c906108c
SS
18513 struct abbrev_info *cur_abbrev;
18514 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18515 unsigned int abbrev_form;
6dfa2fc2 18516 std::vector<struct attr_abbrev> cur_attrs;
c906108c 18517
685af9cd 18518 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18519
433df2d4 18520 dwarf2_read_section (objfile, section);
9c541725 18521 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18522 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18523 abbrev_ptr += bytes_read;
18524
0963b4bd 18525 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18526 while (abbrev_number)
18527 {
6dfa2fc2 18528 cur_attrs.clear ();
685af9cd 18529 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18530
18531 /* read in abbrev header */
18532 cur_abbrev->number = abbrev_number;
aead7601
SM
18533 cur_abbrev->tag
18534 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18535 abbrev_ptr += bytes_read;
18536 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18537 abbrev_ptr += 1;
18538
18539 /* now read in declarations */
22d2f3ab 18540 for (;;)
c906108c 18541 {
43988095
JK
18542 LONGEST implicit_const;
18543
22d2f3ab
JK
18544 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18545 abbrev_ptr += bytes_read;
18546 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18547 abbrev_ptr += bytes_read;
43988095
JK
18548 if (abbrev_form == DW_FORM_implicit_const)
18549 {
18550 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18551 &bytes_read);
18552 abbrev_ptr += bytes_read;
18553 }
18554 else
18555 {
18556 /* Initialize it due to a false compiler warning. */
18557 implicit_const = -1;
18558 }
22d2f3ab
JK
18559
18560 if (abbrev_name == 0)
18561 break;
18562
6dfa2fc2
TT
18563 cur_attrs.emplace_back ();
18564 struct attr_abbrev &cur_attr = cur_attrs.back ();
18565 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18566 cur_attr.form = (enum dwarf_form) abbrev_form;
18567 cur_attr.implicit_const = implicit_const;
22d2f3ab 18568 ++cur_abbrev->num_attrs;
c906108c
SS
18569 }
18570
8d749320
SM
18571 cur_abbrev->attrs =
18572 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18573 cur_abbrev->num_attrs);
6dfa2fc2 18574 memcpy (cur_abbrev->attrs, cur_attrs.data (),
f3dd6933
DJ
18575 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18576
685af9cd 18577 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18578
18579 /* Get next abbreviation.
18580 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18581 always properly terminated with an abbrev number of 0.
18582 Exit loop if we encounter an abbreviation which we have
18583 already read (which means we are about to read the abbreviations
18584 for the next compile unit) or if the end of the abbreviation
18585 table is reached. */
433df2d4 18586 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18587 break;
18588 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18589 abbrev_ptr += bytes_read;
685af9cd 18590 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18591 break;
18592 }
f3dd6933 18593
433df2d4 18594 return abbrev_table;
c906108c
SS
18595}
18596
72bf9492
DJ
18597/* Returns nonzero if TAG represents a type that we might generate a partial
18598 symbol for. */
18599
18600static int
18601is_type_tag_for_partial (int tag)
18602{
18603 switch (tag)
18604 {
18605#if 0
18606 /* Some types that would be reasonable to generate partial symbols for,
18607 that we don't at present. */
18608 case DW_TAG_array_type:
18609 case DW_TAG_file_type:
18610 case DW_TAG_ptr_to_member_type:
18611 case DW_TAG_set_type:
18612 case DW_TAG_string_type:
18613 case DW_TAG_subroutine_type:
18614#endif
18615 case DW_TAG_base_type:
18616 case DW_TAG_class_type:
680b30c7 18617 case DW_TAG_interface_type:
72bf9492
DJ
18618 case DW_TAG_enumeration_type:
18619 case DW_TAG_structure_type:
18620 case DW_TAG_subrange_type:
18621 case DW_TAG_typedef:
18622 case DW_TAG_union_type:
18623 return 1;
18624 default:
18625 return 0;
18626 }
18627}
18628
18629/* Load all DIEs that are interesting for partial symbols into memory. */
18630
18631static struct partial_die_info *
dee91e82 18632load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18633 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18634{
dee91e82 18635 struct dwarf2_cu *cu = reader->cu;
518817b3 18636 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18637 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18638 unsigned int bytes_read;
5afb4e99 18639 unsigned int load_all = 0;
72bf9492
DJ
18640 int nesting_level = 1;
18641
18642 parent_die = NULL;
18643 last_die = NULL;
18644
7adf1e79
DE
18645 gdb_assert (cu->per_cu != NULL);
18646 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18647 load_all = 1;
18648
72bf9492
DJ
18649 cu->partial_dies
18650 = htab_create_alloc_ex (cu->header.length / 12,
18651 partial_die_hash,
18652 partial_die_eq,
18653 NULL,
18654 &cu->comp_unit_obstack,
18655 hashtab_obstack_allocate,
18656 dummy_obstack_deallocate);
18657
72bf9492
DJ
18658 while (1)
18659 {
685af9cd 18660 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18661
18662 /* A NULL abbrev means the end of a series of children. */
18663 if (abbrev == NULL)
18664 {
18665 if (--nesting_level == 0)
cd9983dd
YQ
18666 return first_die;
18667
72bf9492
DJ
18668 info_ptr += bytes_read;
18669 last_die = parent_die;
18670 parent_die = parent_die->die_parent;
18671 continue;
18672 }
18673
98bfdba5
PA
18674 /* Check for template arguments. We never save these; if
18675 they're seen, we just mark the parent, and go on our way. */
18676 if (parent_die != NULL
18677 && cu->language == language_cplus
18678 && (abbrev->tag == DW_TAG_template_type_param
18679 || abbrev->tag == DW_TAG_template_value_param))
18680 {
18681 parent_die->has_template_arguments = 1;
18682
18683 if (!load_all)
18684 {
18685 /* We don't need a partial DIE for the template argument. */
dee91e82 18686 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18687 continue;
18688 }
18689 }
18690
0d99eb77 18691 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18692 Skip their other children. */
18693 if (!load_all
18694 && cu->language == language_cplus
18695 && parent_die != NULL
18696 && parent_die->tag == DW_TAG_subprogram)
18697 {
dee91e82 18698 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18699 continue;
18700 }
18701
5afb4e99
DJ
18702 /* Check whether this DIE is interesting enough to save. Normally
18703 we would not be interested in members here, but there may be
18704 later variables referencing them via DW_AT_specification (for
18705 static members). */
18706 if (!load_all
18707 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18708 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18709 && abbrev->tag != DW_TAG_enumerator
18710 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18711 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18712 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18713 && abbrev->tag != DW_TAG_variable
5afb4e99 18714 && abbrev->tag != DW_TAG_namespace
f55ee35c 18715 && abbrev->tag != DW_TAG_module
95554aad 18716 && abbrev->tag != DW_TAG_member
74921315
KS
18717 && abbrev->tag != DW_TAG_imported_unit
18718 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18719 {
18720 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18721 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18722 continue;
18723 }
18724
6f06d47b
YQ
18725 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18726 abbrev);
cd9983dd 18727
48fbe735 18728 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18729
18730 /* This two-pass algorithm for processing partial symbols has a
18731 high cost in cache pressure. Thus, handle some simple cases
18732 here which cover the majority of C partial symbols. DIEs
18733 which neither have specification tags in them, nor could have
18734 specification tags elsewhere pointing at them, can simply be
18735 processed and discarded.
18736
18737 This segment is also optional; scan_partial_symbols and
18738 add_partial_symbol will handle these DIEs if we chain
18739 them in normally. When compilers which do not emit large
18740 quantities of duplicate debug information are more common,
18741 this code can probably be removed. */
18742
18743 /* Any complete simple types at the top level (pretty much all
18744 of them, for a language without namespaces), can be processed
18745 directly. */
18746 if (parent_die == NULL
cd9983dd
YQ
18747 && pdi.has_specification == 0
18748 && pdi.is_declaration == 0
18749 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18750 || pdi.tag == DW_TAG_base_type
18751 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18752 {
cd9983dd 18753 if (building_psymtab && pdi.name != NULL)
31edb802 18754 add_psymbol_to_list (pdi.name, false,
79748972 18755 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18756 psymbol_placement::STATIC,
1762568f 18757 0, cu->language, objfile);
cd9983dd 18758 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18759 continue;
18760 }
18761
d8228535
JK
18762 /* The exception for DW_TAG_typedef with has_children above is
18763 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18764 type_name_or_error will error on such types later.
d8228535
JK
18765
18766 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18767 it could not find the child DIEs referenced later, this is checked
18768 above. In correct DWARF DW_TAG_typedef should have no children. */
18769
cd9983dd 18770 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18771 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18772 "- DIE at %s [in module %s]"),
cd9983dd 18773 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18774
72bf9492
DJ
18775 /* If we're at the second level, and we're an enumerator, and
18776 our parent has no specification (meaning possibly lives in a
18777 namespace elsewhere), then we can add the partial symbol now
18778 instead of queueing it. */
cd9983dd 18779 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18780 && parent_die != NULL
18781 && parent_die->die_parent == NULL
18782 && parent_die->tag == DW_TAG_enumeration_type
18783 && parent_die->has_specification == 0)
18784 {
cd9983dd 18785 if (pdi.name == NULL)
b98664d3 18786 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18787 else if (building_psymtab)
31edb802 18788 add_psymbol_to_list (pdi.name, false,
79748972 18789 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18790 cu->language == language_cplus
75aedd27
TT
18791 ? psymbol_placement::GLOBAL
18792 : psymbol_placement::STATIC,
1762568f 18793 0, cu->language, objfile);
72bf9492 18794
cd9983dd 18795 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18796 continue;
18797 }
18798
cd9983dd 18799 struct partial_die_info *part_die
6f06d47b 18800 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18801
72bf9492
DJ
18802 /* We'll save this DIE so link it in. */
18803 part_die->die_parent = parent_die;
18804 part_die->die_sibling = NULL;
18805 part_die->die_child = NULL;
18806
18807 if (last_die && last_die == parent_die)
18808 last_die->die_child = part_die;
18809 else if (last_die)
18810 last_die->die_sibling = part_die;
18811
18812 last_die = part_die;
18813
18814 if (first_die == NULL)
18815 first_die = part_die;
18816
18817 /* Maybe add the DIE to the hash table. Not all DIEs that we
18818 find interesting need to be in the hash table, because we
18819 also have the parent/sibling/child chains; only those that we
18820 might refer to by offset later during partial symbol reading.
18821
18822 For now this means things that might have be the target of a
18823 DW_AT_specification, DW_AT_abstract_origin, or
18824 DW_AT_extension. DW_AT_extension will refer only to
18825 namespaces; DW_AT_abstract_origin refers to functions (and
18826 many things under the function DIE, but we do not recurse
18827 into function DIEs during partial symbol reading) and
18828 possibly variables as well; DW_AT_specification refers to
18829 declarations. Declarations ought to have the DW_AT_declaration
18830 flag. It happens that GCC forgets to put it in sometimes, but
18831 only for functions, not for types.
18832
18833 Adding more things than necessary to the hash table is harmless
18834 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18835 wasted time in find_partial_die, when we reread the compilation
18836 unit with load_all_dies set. */
72bf9492 18837
5afb4e99 18838 if (load_all
72929c62 18839 || abbrev->tag == DW_TAG_constant
5afb4e99 18840 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18841 || abbrev->tag == DW_TAG_variable
18842 || abbrev->tag == DW_TAG_namespace
18843 || part_die->is_declaration)
18844 {
18845 void **slot;
18846
18847 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18848 to_underlying (part_die->sect_off),
18849 INSERT);
72bf9492
DJ
18850 *slot = part_die;
18851 }
18852
72bf9492 18853 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18854 we have no reason to follow the children of structures; for other
98bfdba5
PA
18855 languages we have to, so that we can get at method physnames
18856 to infer fully qualified class names, for DW_AT_specification,
18857 and for C++ template arguments. For C++, we also look one level
18858 inside functions to find template arguments (if the name of the
18859 function does not already contain the template arguments).
bc30ff58 18860
0a4b0913
AB
18861 For Ada and Fortran, we need to scan the children of subprograms
18862 and lexical blocks as well because these languages allow the
18863 definition of nested entities that could be interesting for the
18864 debugger, such as nested subprograms for instance. */
72bf9492 18865 if (last_die->has_children
5afb4e99
DJ
18866 && (load_all
18867 || last_die->tag == DW_TAG_namespace
f55ee35c 18868 || last_die->tag == DW_TAG_module
72bf9492 18869 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18870 || (cu->language == language_cplus
18871 && last_die->tag == DW_TAG_subprogram
18872 && (last_die->name == NULL
18873 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18874 || (cu->language != language_c
18875 && (last_die->tag == DW_TAG_class_type
680b30c7 18876 || last_die->tag == DW_TAG_interface_type
72bf9492 18877 || last_die->tag == DW_TAG_structure_type
bc30ff58 18878 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18879 || ((cu->language == language_ada
18880 || cu->language == language_fortran)
bc30ff58
JB
18881 && (last_die->tag == DW_TAG_subprogram
18882 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18883 {
18884 nesting_level++;
18885 parent_die = last_die;
18886 continue;
18887 }
18888
18889 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18890 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18891
18892 /* Back to the top, do it again. */
18893 }
18894}
18895
6f06d47b
YQ
18896partial_die_info::partial_die_info (sect_offset sect_off_,
18897 struct abbrev_info *abbrev)
18898 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18899{
18900}
18901
35cc7ed7
YQ
18902/* Read a minimal amount of information into the minimal die structure.
18903 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18904
48fbe735
YQ
18905const gdb_byte *
18906partial_die_info::read (const struct die_reader_specs *reader,
18907 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18908{
dee91e82 18909 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18910 struct dwarf2_per_objfile *dwarf2_per_objfile
18911 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18912 unsigned int i;
c5aa993b 18913 int has_low_pc_attr = 0;
c906108c 18914 int has_high_pc_attr = 0;
91da1414 18915 int high_pc_relative = 0;
c906108c 18916
18a8505e 18917 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 18918 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18919 {
18a8505e
AT
18920 bool need_reprocess;
18921 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18922 info_ptr, &need_reprocess);
18923 /* String and address offsets that need to do the reprocessing have
18924 already been read at this point, so there is no need to wait until
18925 the loop terminates to do the reprocessing. */
18926 if (need_reprocess)
18927 read_attribute_reprocess (reader, &attr_vec[i]);
18928 attribute &attr = attr_vec[i];
c906108c 18929 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18930 partial symbol table. */
c906108c
SS
18931 switch (attr.name)
18932 {
18933 case DW_AT_name:
48fbe735 18934 switch (tag)
71c25dea
TT
18935 {
18936 case DW_TAG_compile_unit:
95554aad 18937 case DW_TAG_partial_unit:
348e048f 18938 case DW_TAG_type_unit:
71c25dea
TT
18939 /* Compilation units have a DW_AT_name that is a filename, not
18940 a source language identifier. */
18941 case DW_TAG_enumeration_type:
18942 case DW_TAG_enumerator:
18943 /* These tags always have simple identifiers already; no need
18944 to canonicalize them. */
48fbe735 18945 name = DW_STRING (&attr);
71c25dea
TT
18946 break;
18947 default:
48fbe735
YQ
18948 {
18949 struct objfile *objfile = dwarf2_per_objfile->objfile;
18950
18951 name
18952 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18953 &objfile->per_bfd->storage_obstack);
18954 }
71c25dea
TT
18955 break;
18956 }
c906108c 18957 break;
31ef98ae 18958 case DW_AT_linkage_name:
c906108c 18959 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18960 /* Note that both forms of linkage name might appear. We
18961 assume they will be the same, and we only store the last
18962 one we see. */
48fbe735 18963 linkage_name = DW_STRING (&attr);
c906108c
SS
18964 break;
18965 case DW_AT_low_pc:
18966 has_low_pc_attr = 1;
48fbe735 18967 lowpc = attr_value_as_address (&attr);
c906108c
SS
18968 break;
18969 case DW_AT_high_pc:
18970 has_high_pc_attr = 1;
48fbe735 18971 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18972 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18973 high_pc_relative = 1;
c906108c
SS
18974 break;
18975 case DW_AT_location:
0963b4bd 18976 /* Support the .debug_loc offsets. */
8e19ed76
PS
18977 if (attr_form_is_block (&attr))
18978 {
48fbe735 18979 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18980 }
3690dd37 18981 else if (attr_form_is_section_offset (&attr))
8e19ed76 18982 {
4d3c2250 18983 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18984 }
18985 else
18986 {
4d3c2250
KB
18987 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18988 "partial symbol information");
8e19ed76 18989 }
c906108c 18990 break;
c906108c 18991 case DW_AT_external:
48fbe735 18992 is_external = DW_UNSND (&attr);
c906108c
SS
18993 break;
18994 case DW_AT_declaration:
48fbe735 18995 is_declaration = DW_UNSND (&attr);
c906108c
SS
18996 break;
18997 case DW_AT_type:
48fbe735 18998 has_type = 1;
c906108c
SS
18999 break;
19000 case DW_AT_abstract_origin:
19001 case DW_AT_specification:
72bf9492 19002 case DW_AT_extension:
48fbe735
YQ
19003 has_specification = 1;
19004 spec_offset = dwarf2_get_ref_die_offset (&attr);
19005 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19006 || cu->per_cu->is_dwz);
c906108c
SS
19007 break;
19008 case DW_AT_sibling:
19009 /* Ignore absolute siblings, they might point outside of
19010 the current compile unit. */
19011 if (attr.form == DW_FORM_ref_addr)
b98664d3 19012 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19013 else
b9502d3f 19014 {
48fbe735 19015 const gdb_byte *buffer = reader->buffer;
9c541725
PA
19016 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19017 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19018
19019 if (sibling_ptr < info_ptr)
b98664d3 19020 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
19021 else if (sibling_ptr > reader->buffer_end)
19022 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19023 else
48fbe735 19024 sibling = sibling_ptr;
b9502d3f 19025 }
c906108c 19026 break;
fa4028e9 19027 case DW_AT_byte_size:
48fbe735 19028 has_byte_size = 1;
fa4028e9 19029 break;
ff908ebf 19030 case DW_AT_const_value:
48fbe735 19031 has_const_value = 1;
ff908ebf 19032 break;
68511cec
CES
19033 case DW_AT_calling_convention:
19034 /* DWARF doesn't provide a way to identify a program's source-level
19035 entry point. DW_AT_calling_convention attributes are only meant
19036 to describe functions' calling conventions.
19037
19038 However, because it's a necessary piece of information in
0c1b455e
TT
19039 Fortran, and before DWARF 4 DW_CC_program was the only
19040 piece of debugging information whose definition refers to
19041 a 'main program' at all, several compilers marked Fortran
19042 main programs with DW_CC_program --- even when those
19043 functions use the standard calling conventions.
19044
19045 Although DWARF now specifies a way to provide this
19046 information, we support this practice for backward
19047 compatibility. */
68511cec 19048 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19049 && cu->language == language_fortran)
48fbe735 19050 main_subprogram = 1;
68511cec 19051 break;
481860b3
GB
19052 case DW_AT_inline:
19053 if (DW_UNSND (&attr) == DW_INL_inlined
19054 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19055 may_be_inlined = 1;
481860b3 19056 break;
95554aad
TT
19057
19058 case DW_AT_import:
48fbe735 19059 if (tag == DW_TAG_imported_unit)
36586728 19060 {
48fbe735
YQ
19061 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19062 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19063 || cu->per_cu->is_dwz);
19064 }
95554aad
TT
19065 break;
19066
0c1b455e 19067 case DW_AT_main_subprogram:
48fbe735 19068 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19069 break;
19070
05caa1d2
TT
19071 case DW_AT_ranges:
19072 {
19073 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19074 but that requires a full DIE, so instead we just
19075 reimplement it. */
19076 int need_ranges_base = tag != DW_TAG_compile_unit;
19077 unsigned int ranges_offset = (DW_UNSND (&attr)
19078 + (need_ranges_base
19079 ? cu->ranges_base
19080 : 0));
19081
19082 /* Value of the DW_AT_ranges attribute is the offset in the
19083 .debug_ranges section. */
19084 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19085 nullptr))
19086 has_pc_info = 1;
19087 }
19088 break;
19089
c906108c
SS
19090 default:
19091 break;
19092 }
19093 }
19094
10d06d82
TT
19095 /* For Ada, if both the name and the linkage name appear, we prefer
19096 the latter. This lets "catch exception" work better, regardless
19097 of the order in which the name and linkage name were emitted.
19098 Really, though, this is just a workaround for the fact that gdb
19099 doesn't store both the name and the linkage name. */
19100 if (cu->language == language_ada && linkage_name != nullptr)
19101 name = linkage_name;
19102
91da1414 19103 if (high_pc_relative)
48fbe735 19104 highpc += lowpc;
91da1414 19105
9373cf26
JK
19106 if (has_low_pc_attr && has_high_pc_attr)
19107 {
19108 /* When using the GNU linker, .gnu.linkonce. sections are used to
19109 eliminate duplicate copies of functions and vtables and such.
19110 The linker will arbitrarily choose one and discard the others.
19111 The AT_*_pc values for such functions refer to local labels in
19112 these sections. If the section from that file was discarded, the
19113 labels are not in the output, so the relocs get a value of 0.
19114 If this is a discarded function, mark the pc bounds as invalid,
19115 so that GDB will ignore it. */
48fbe735 19116 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19117 {
48fbe735 19118 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19119 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19120
b98664d3 19121 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19122 "for DIE at %s [in module %s]"),
48fbe735
YQ
19123 paddress (gdbarch, lowpc),
19124 sect_offset_str (sect_off),
9d8780f0 19125 objfile_name (objfile));
9373cf26
JK
19126 }
19127 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19128 else if (lowpc >= highpc)
9373cf26 19129 {
48fbe735 19130 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19131 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19132
b98664d3 19133 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19134 "for DIE at %s [in module %s]"),
48fbe735
YQ
19135 paddress (gdbarch, lowpc),
19136 paddress (gdbarch, highpc),
19137 sect_offset_str (sect_off),
9c541725 19138 objfile_name (objfile));
9373cf26
JK
19139 }
19140 else
48fbe735 19141 has_pc_info = 1;
9373cf26 19142 }
85cbf3d3 19143
c906108c
SS
19144 return info_ptr;
19145}
19146
72bf9492
DJ
19147/* Find a cached partial DIE at OFFSET in CU. */
19148
d590ff25
YQ
19149struct partial_die_info *
19150dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19151{
19152 struct partial_die_info *lookup_die = NULL;
6f06d47b 19153 struct partial_die_info part_die (sect_off);
72bf9492 19154
9a3c8263 19155 lookup_die = ((struct partial_die_info *)
d590ff25 19156 htab_find_with_hash (partial_dies, &part_die,
9c541725 19157 to_underlying (sect_off)));
72bf9492 19158
72bf9492
DJ
19159 return lookup_die;
19160}
19161
348e048f
DE
19162/* Find a partial DIE at OFFSET, which may or may not be in CU,
19163 except in the case of .debug_types DIEs which do not reference
19164 outside their CU (they do however referencing other types via
55f1336d 19165 DW_FORM_ref_sig8). */
72bf9492 19166
122cf0f2 19167static const struct cu_partial_die_info
9c541725 19168find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19169{
518817b3
SM
19170 struct dwarf2_per_objfile *dwarf2_per_objfile
19171 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19172 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19173 struct dwarf2_per_cu_data *per_cu = NULL;
19174 struct partial_die_info *pd = NULL;
72bf9492 19175
36586728 19176 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19177 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19178 {
d590ff25 19179 pd = cu->find_partial_die (sect_off);
5afb4e99 19180 if (pd != NULL)
fb816e8b 19181 return { cu, pd };
0d99eb77
DE
19182 /* We missed recording what we needed.
19183 Load all dies and try again. */
19184 per_cu = cu->per_cu;
5afb4e99 19185 }
0d99eb77
DE
19186 else
19187 {
19188 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19189 if (cu->per_cu->is_debug_types)
0d99eb77 19190 {
9d8780f0
SM
19191 error (_("Dwarf Error: Type Unit at offset %s contains"
19192 " external reference to offset %s [in module %s].\n"),
19193 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19194 bfd_get_filename (objfile->obfd));
19195 }
9c541725 19196 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19197 dwarf2_per_objfile);
72bf9492 19198
0d99eb77
DE
19199 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19200 load_partial_comp_unit (per_cu);
ae038cb0 19201
0d99eb77 19202 per_cu->cu->last_used = 0;
d590ff25 19203 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19204 }
5afb4e99 19205
dee91e82
DE
19206 /* If we didn't find it, and not all dies have been loaded,
19207 load them all and try again. */
19208
5afb4e99
DJ
19209 if (pd == NULL && per_cu->load_all_dies == 0)
19210 {
5afb4e99 19211 per_cu->load_all_dies = 1;
fd820528
DE
19212
19213 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19214 THIS_CU->cu may already be in use. So we can't just free it and
19215 replace its DIEs with the ones we read in. Instead, we leave those
19216 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19217 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19218 set. */
dee91e82 19219 load_partial_comp_unit (per_cu);
5afb4e99 19220
d590ff25 19221 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19222 }
19223
19224 if (pd == NULL)
19225 internal_error (__FILE__, __LINE__,
9d8780f0 19226 _("could not find partial DIE %s "
3e43a32a 19227 "in cache [from module %s]\n"),
9d8780f0 19228 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19229 return { per_cu->cu, pd };
72bf9492
DJ
19230}
19231
abc72ce4
DE
19232/* See if we can figure out if the class lives in a namespace. We do
19233 this by looking for a member function; its demangled name will
19234 contain namespace info, if there is any. */
19235
19236static void
19237guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19238 struct dwarf2_cu *cu)
19239{
19240 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19241 what template types look like, because the demangler
19242 frequently doesn't give the same name as the debug info. We
19243 could fix this by only using the demangled name to get the
19244 prefix (but see comment in read_structure_type). */
19245
19246 struct partial_die_info *real_pdi;
19247 struct partial_die_info *child_pdi;
19248
19249 /* If this DIE (this DIE's specification, if any) has a parent, then
19250 we should not do this. We'll prepend the parent's fully qualified
19251 name when we create the partial symbol. */
19252
19253 real_pdi = struct_pdi;
19254 while (real_pdi->has_specification)
fb816e8b 19255 {
122cf0f2
AB
19256 auto res = find_partial_die (real_pdi->spec_offset,
19257 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19258 real_pdi = res.pdi;
19259 cu = res.cu;
19260 }
abc72ce4
DE
19261
19262 if (real_pdi->die_parent != NULL)
19263 return;
19264
19265 for (child_pdi = struct_pdi->die_child;
19266 child_pdi != NULL;
19267 child_pdi = child_pdi->die_sibling)
19268 {
19269 if (child_pdi->tag == DW_TAG_subprogram
19270 && child_pdi->linkage_name != NULL)
19271 {
43816ebc
TT
19272 gdb::unique_xmalloc_ptr<char> actual_class_name
19273 (language_class_name_from_physname (cu->language_defn,
19274 child_pdi->linkage_name));
abc72ce4
DE
19275 if (actual_class_name != NULL)
19276 {
518817b3 19277 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19278 struct_pdi->name
021887d8 19279 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 19280 actual_class_name.get ());
abc72ce4
DE
19281 }
19282 break;
19283 }
19284 }
19285}
19286
52356b79
YQ
19287void
19288partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19289{
abc72ce4
DE
19290 /* Once we've fixed up a die, there's no point in doing so again.
19291 This also avoids a memory leak if we were to call
19292 guess_partial_die_structure_name multiple times. */
52356b79 19293 if (fixup_called)
abc72ce4
DE
19294 return;
19295
72bf9492
DJ
19296 /* If we found a reference attribute and the DIE has no name, try
19297 to find a name in the referred to DIE. */
19298
52356b79 19299 if (name == NULL && has_specification)
72bf9492
DJ
19300 {
19301 struct partial_die_info *spec_die;
72bf9492 19302
122cf0f2 19303 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19304 spec_die = res.pdi;
19305 cu = res.cu;
72bf9492 19306
52356b79 19307 spec_die->fixup (cu);
72bf9492
DJ
19308
19309 if (spec_die->name)
19310 {
52356b79 19311 name = spec_die->name;
72bf9492
DJ
19312
19313 /* Copy DW_AT_external attribute if it is set. */
19314 if (spec_die->is_external)
52356b79 19315 is_external = spec_die->is_external;
72bf9492
DJ
19316 }
19317 }
19318
19319 /* Set default names for some unnamed DIEs. */
72bf9492 19320
52356b79
YQ
19321 if (name == NULL && tag == DW_TAG_namespace)
19322 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19323
abc72ce4
DE
19324 /* If there is no parent die to provide a namespace, and there are
19325 children, see if we can determine the namespace from their linkage
122d1940 19326 name. */
abc72ce4 19327 if (cu->language == language_cplus
fd5866f6 19328 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19329 && die_parent == NULL
19330 && has_children
19331 && (tag == DW_TAG_class_type
19332 || tag == DW_TAG_structure_type
19333 || tag == DW_TAG_union_type))
19334 guess_partial_die_structure_name (this, cu);
abc72ce4 19335
53832f31
TT
19336 /* GCC might emit a nameless struct or union that has a linkage
19337 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19338 if (name == NULL
19339 && (tag == DW_TAG_class_type
19340 || tag == DW_TAG_interface_type
19341 || tag == DW_TAG_structure_type
19342 || tag == DW_TAG_union_type)
19343 && linkage_name != NULL)
53832f31 19344 {
43816ebc
TT
19345 gdb::unique_xmalloc_ptr<char> demangled
19346 (gdb_demangle (linkage_name, DMGL_TYPES));
19347 if (demangled != nullptr)
53832f31 19348 {
96408a79
SA
19349 const char *base;
19350
19351 /* Strip any leading namespaces/classes, keep only the base name.
19352 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19353 base = strrchr (demangled.get (), ':');
19354 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19355 base++;
19356 else
43816ebc 19357 base = demangled.get ();
96408a79 19358
518817b3 19359 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19360 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19361 }
19362 }
19363
52356b79 19364 fixup_called = 1;
72bf9492
DJ
19365}
19366
18a8505e
AT
19367/* Process the attributes that had to be skipped in the first round. These
19368 attributes are the ones that need str_offsets_base or addr_base attributes.
19369 They could not have been processed in the first round, because at the time
19370 the values of str_offsets_base or addr_base may not have been known. */
19371void read_attribute_reprocess (const struct die_reader_specs *reader,
19372 struct attribute *attr)
19373{
19374 struct dwarf2_cu *cu = reader->cu;
19375 switch (attr->form)
19376 {
19377 case DW_FORM_addrx:
19378 case DW_FORM_GNU_addr_index:
19379 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19380 break;
19381 case DW_FORM_strx:
19382 case DW_FORM_strx1:
19383 case DW_FORM_strx2:
19384 case DW_FORM_strx3:
19385 case DW_FORM_strx4:
19386 case DW_FORM_GNU_str_index:
19387 {
19388 unsigned int str_index = DW_UNSND (attr);
19389 if (reader->dwo_file != NULL)
19390 {
19391 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19392 DW_STRING_IS_CANONICAL (attr) = 0;
19393 }
19394 else
19395 {
19396 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19397 DW_STRING_IS_CANONICAL (attr) = 0;
19398 }
19399 break;
19400 }
19401 default:
19402 gdb_assert_not_reached (_("Unexpected DWARF form."));
19403 }
19404}
19405
a8329558 19406/* Read an attribute value described by an attribute form. */
c906108c 19407
d521ce57 19408static const gdb_byte *
dee91e82
DE
19409read_attribute_value (const struct die_reader_specs *reader,
19410 struct attribute *attr, unsigned form,
18a8505e
AT
19411 LONGEST implicit_const, const gdb_byte *info_ptr,
19412 bool *need_reprocess)
c906108c 19413{
dee91e82 19414 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19415 struct dwarf2_per_objfile *dwarf2_per_objfile
19416 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19417 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19418 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19419 bfd *abfd = reader->abfd;
e7c27a73 19420 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19421 unsigned int bytes_read;
19422 struct dwarf_block *blk;
18a8505e 19423 *need_reprocess = false;
c906108c 19424
aead7601 19425 attr->form = (enum dwarf_form) form;
a8329558 19426 switch (form)
c906108c 19427 {
c906108c 19428 case DW_FORM_ref_addr:
ae411497 19429 if (cu->header.version == 2)
4568ecf9 19430 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19431 else
4568ecf9
DE
19432 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19433 &cu->header, &bytes_read);
ae411497
TT
19434 info_ptr += bytes_read;
19435 break;
36586728
TT
19436 case DW_FORM_GNU_ref_alt:
19437 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19438 info_ptr += bytes_read;
19439 break;
ae411497 19440 case DW_FORM_addr:
e7c27a73 19441 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19442 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19443 info_ptr += bytes_read;
c906108c
SS
19444 break;
19445 case DW_FORM_block2:
7b5a2f43 19446 blk = dwarf_alloc_block (cu);
c906108c
SS
19447 blk->size = read_2_bytes (abfd, info_ptr);
19448 info_ptr += 2;
19449 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19450 info_ptr += blk->size;
19451 DW_BLOCK (attr) = blk;
19452 break;
19453 case DW_FORM_block4:
7b5a2f43 19454 blk = dwarf_alloc_block (cu);
c906108c
SS
19455 blk->size = read_4_bytes (abfd, info_ptr);
19456 info_ptr += 4;
19457 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19458 info_ptr += blk->size;
19459 DW_BLOCK (attr) = blk;
19460 break;
19461 case DW_FORM_data2:
19462 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19463 info_ptr += 2;
19464 break;
19465 case DW_FORM_data4:
19466 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19467 info_ptr += 4;
19468 break;
19469 case DW_FORM_data8:
19470 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19471 info_ptr += 8;
19472 break;
0224619f
JK
19473 case DW_FORM_data16:
19474 blk = dwarf_alloc_block (cu);
19475 blk->size = 16;
19476 blk->data = read_n_bytes (abfd, info_ptr, 16);
19477 info_ptr += 16;
19478 DW_BLOCK (attr) = blk;
19479 break;
2dc7f7b3
TT
19480 case DW_FORM_sec_offset:
19481 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19482 info_ptr += bytes_read;
19483 break;
c906108c 19484 case DW_FORM_string:
9b1c24c8 19485 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19486 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19487 info_ptr += bytes_read;
19488 break;
4bdf3d34 19489 case DW_FORM_strp:
36586728
TT
19490 if (!cu->per_cu->is_dwz)
19491 {
ed2dc618
SM
19492 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19493 abfd, info_ptr, cu_header,
36586728
TT
19494 &bytes_read);
19495 DW_STRING_IS_CANONICAL (attr) = 0;
19496 info_ptr += bytes_read;
19497 break;
19498 }
19499 /* FALLTHROUGH */
43988095
JK
19500 case DW_FORM_line_strp:
19501 if (!cu->per_cu->is_dwz)
19502 {
ed2dc618
SM
19503 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19504 abfd, info_ptr,
43988095
JK
19505 cu_header, &bytes_read);
19506 DW_STRING_IS_CANONICAL (attr) = 0;
19507 info_ptr += bytes_read;
19508 break;
19509 }
19510 /* FALLTHROUGH */
36586728
TT
19511 case DW_FORM_GNU_strp_alt:
19512 {
ed2dc618 19513 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19514 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19515 &bytes_read);
19516
ed2dc618
SM
19517 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19518 dwz, str_offset);
36586728
TT
19519 DW_STRING_IS_CANONICAL (attr) = 0;
19520 info_ptr += bytes_read;
19521 }
4bdf3d34 19522 break;
2dc7f7b3 19523 case DW_FORM_exprloc:
c906108c 19524 case DW_FORM_block:
7b5a2f43 19525 blk = dwarf_alloc_block (cu);
c906108c
SS
19526 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19527 info_ptr += bytes_read;
19528 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19529 info_ptr += blk->size;
19530 DW_BLOCK (attr) = blk;
19531 break;
19532 case DW_FORM_block1:
7b5a2f43 19533 blk = dwarf_alloc_block (cu);
c906108c
SS
19534 blk->size = read_1_byte (abfd, info_ptr);
19535 info_ptr += 1;
19536 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19537 info_ptr += blk->size;
19538 DW_BLOCK (attr) = blk;
19539 break;
19540 case DW_FORM_data1:
19541 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19542 info_ptr += 1;
19543 break;
19544 case DW_FORM_flag:
19545 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19546 info_ptr += 1;
19547 break;
2dc7f7b3
TT
19548 case DW_FORM_flag_present:
19549 DW_UNSND (attr) = 1;
19550 break;
c906108c
SS
19551 case DW_FORM_sdata:
19552 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19553 info_ptr += bytes_read;
19554 break;
19555 case DW_FORM_udata:
18a8505e 19556 case DW_FORM_rnglistx:
c906108c
SS
19557 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19558 info_ptr += bytes_read;
19559 break;
19560 case DW_FORM_ref1:
9c541725 19561 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19562 + read_1_byte (abfd, info_ptr));
c906108c
SS
19563 info_ptr += 1;
19564 break;
19565 case DW_FORM_ref2:
9c541725 19566 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19567 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19568 info_ptr += 2;
19569 break;
19570 case DW_FORM_ref4:
9c541725 19571 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19572 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19573 info_ptr += 4;
19574 break;
613e1657 19575 case DW_FORM_ref8:
9c541725 19576 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19577 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19578 info_ptr += 8;
19579 break;
55f1336d 19580 case DW_FORM_ref_sig8:
ac9ec31b 19581 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19582 info_ptr += 8;
19583 break;
c906108c 19584 case DW_FORM_ref_udata:
9c541725 19585 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19586 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19587 info_ptr += bytes_read;
19588 break;
c906108c 19589 case DW_FORM_indirect:
a8329558
KW
19590 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19591 info_ptr += bytes_read;
43988095
JK
19592 if (form == DW_FORM_implicit_const)
19593 {
19594 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19595 info_ptr += bytes_read;
19596 }
19597 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19598 info_ptr, need_reprocess);
43988095
JK
19599 break;
19600 case DW_FORM_implicit_const:
19601 DW_SND (attr) = implicit_const;
a8329558 19602 break;
336d760d 19603 case DW_FORM_addrx:
3019eac3 19604 case DW_FORM_GNU_addr_index:
18a8505e
AT
19605 *need_reprocess = true;
19606 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19607 info_ptr += bytes_read;
19608 break;
cf532bd1 19609 case DW_FORM_strx:
15f18d14
AT
19610 case DW_FORM_strx1:
19611 case DW_FORM_strx2:
19612 case DW_FORM_strx3:
19613 case DW_FORM_strx4:
3019eac3 19614 case DW_FORM_GNU_str_index:
3019eac3 19615 {
15f18d14
AT
19616 ULONGEST str_index;
19617 if (form == DW_FORM_strx1)
19618 {
19619 str_index = read_1_byte (abfd, info_ptr);
19620 info_ptr += 1;
19621 }
19622 else if (form == DW_FORM_strx2)
19623 {
19624 str_index = read_2_bytes (abfd, info_ptr);
19625 info_ptr += 2;
19626 }
19627 else if (form == DW_FORM_strx3)
19628 {
19629 str_index = read_3_bytes (abfd, info_ptr);
19630 info_ptr += 3;
19631 }
19632 else if (form == DW_FORM_strx4)
19633 {
19634 str_index = read_4_bytes (abfd, info_ptr);
19635 info_ptr += 4;
19636 }
19637 else
19638 {
19639 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19640 info_ptr += bytes_read;
19641 }
18a8505e
AT
19642 *need_reprocess = true;
19643 DW_UNSND (attr) = str_index;
19644 }
3019eac3 19645 break;
c906108c 19646 default:
8a3fe4f8 19647 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19648 dwarf_form_name (form),
19649 bfd_get_filename (abfd));
c906108c 19650 }
28e94949 19651
36586728 19652 /* Super hack. */
7771576e 19653 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19654 attr->form = DW_FORM_GNU_ref_alt;
19655
28e94949
JB
19656 /* We have seen instances where the compiler tried to emit a byte
19657 size attribute of -1 which ended up being encoded as an unsigned
19658 0xffffffff. Although 0xffffffff is technically a valid size value,
19659 an object of this size seems pretty unlikely so we can relatively
19660 safely treat these cases as if the size attribute was invalid and
19661 treat them as zero by default. */
19662 if (attr->name == DW_AT_byte_size
19663 && form == DW_FORM_data4
19664 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19665 {
19666 complaint
b98664d3 19667 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19668 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19669 DW_UNSND (attr) = 0;
19670 }
28e94949 19671
c906108c
SS
19672 return info_ptr;
19673}
19674
a8329558
KW
19675/* Read an attribute described by an abbreviated attribute. */
19676
d521ce57 19677static const gdb_byte *
dee91e82
DE
19678read_attribute (const struct die_reader_specs *reader,
19679 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19680 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19681{
19682 attr->name = abbrev->name;
43988095 19683 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19684 abbrev->implicit_const, info_ptr,
19685 need_reprocess);
a8329558
KW
19686}
19687
0963b4bd 19688/* Read dwarf information from a buffer. */
c906108c
SS
19689
19690static unsigned int
a1855c1d 19691read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19692{
fe1b8b76 19693 return bfd_get_8 (abfd, buf);
c906108c
SS
19694}
19695
19696static int
a1855c1d 19697read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19698{
fe1b8b76 19699 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19700}
19701
19702static unsigned int
a1855c1d 19703read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19704{
fe1b8b76 19705 return bfd_get_16 (abfd, buf);
c906108c
SS
19706}
19707
21ae7a4d 19708static int
a1855c1d 19709read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19710{
19711 return bfd_get_signed_16 (abfd, buf);
19712}
19713
15f18d14
AT
19714static unsigned int
19715read_3_bytes (bfd *abfd, const gdb_byte *buf)
19716{
19717 unsigned int result = 0;
19718 for (int i = 0; i < 3; ++i)
19719 {
19720 unsigned char byte = bfd_get_8 (abfd, buf);
19721 buf++;
19722 result |= ((unsigned int) byte << (i * 8));
19723 }
19724 return result;
19725}
19726
c906108c 19727static unsigned int
a1855c1d 19728read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19729{
fe1b8b76 19730 return bfd_get_32 (abfd, buf);
c906108c
SS
19731}
19732
21ae7a4d 19733static int
a1855c1d 19734read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19735{
19736 return bfd_get_signed_32 (abfd, buf);
19737}
19738
93311388 19739static ULONGEST
a1855c1d 19740read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19741{
fe1b8b76 19742 return bfd_get_64 (abfd, buf);
c906108c
SS
19743}
19744
19745static CORE_ADDR
d521ce57 19746read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19747 unsigned int *bytes_read)
c906108c 19748{
e7c27a73 19749 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19750 CORE_ADDR retval = 0;
19751
107d2387 19752 if (cu_header->signed_addr_p)
c906108c 19753 {
107d2387
AC
19754 switch (cu_header->addr_size)
19755 {
19756 case 2:
fe1b8b76 19757 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19758 break;
19759 case 4:
fe1b8b76 19760 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19761 break;
19762 case 8:
fe1b8b76 19763 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19764 break;
19765 default:
8e65ff28 19766 internal_error (__FILE__, __LINE__,
e2e0b3e5 19767 _("read_address: bad switch, signed [in module %s]"),
659b0389 19768 bfd_get_filename (abfd));
107d2387
AC
19769 }
19770 }
19771 else
19772 {
19773 switch (cu_header->addr_size)
19774 {
19775 case 2:
fe1b8b76 19776 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19777 break;
19778 case 4:
fe1b8b76 19779 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19780 break;
19781 case 8:
fe1b8b76 19782 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19783 break;
19784 default:
8e65ff28 19785 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19786 _("read_address: bad switch, "
19787 "unsigned [in module %s]"),
659b0389 19788 bfd_get_filename (abfd));
107d2387 19789 }
c906108c 19790 }
64367e0a 19791
107d2387
AC
19792 *bytes_read = cu_header->addr_size;
19793 return retval;
c906108c
SS
19794}
19795
f7ef9339 19796/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19797 specification allows the initial length to take up either 4 bytes
19798 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19799 bytes describe the length and all offsets will be 8 bytes in length
19800 instead of 4.
19801
f7ef9339
KB
19802 An older, non-standard 64-bit format is also handled by this
19803 function. The older format in question stores the initial length
19804 as an 8-byte quantity without an escape value. Lengths greater
19805 than 2^32 aren't very common which means that the initial 4 bytes
19806 is almost always zero. Since a length value of zero doesn't make
19807 sense for the 32-bit format, this initial zero can be considered to
19808 be an escape value which indicates the presence of the older 64-bit
19809 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19810 greater than 4GB. If it becomes necessary to handle lengths
19811 somewhat larger than 4GB, we could allow other small values (such
19812 as the non-sensical values of 1, 2, and 3) to also be used as
19813 escape values indicating the presence of the old format.
f7ef9339 19814
917c78fc
MK
19815 The value returned via bytes_read should be used to increment the
19816 relevant pointer after calling read_initial_length().
c764a876 19817
613e1657
KB
19818 [ Note: read_initial_length() and read_offset() are based on the
19819 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19820 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19821 from:
19822
f7ef9339 19823 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19824
613e1657
KB
19825 This document is only a draft and is subject to change. (So beware.)
19826
f7ef9339 19827 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19828 determined empirically by examining 64-bit ELF files produced by
19829 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19830
19831 - Kevin, July 16, 2002
613e1657
KB
19832 ] */
19833
19834static LONGEST
d521ce57 19835read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19836{
fe1b8b76 19837 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19838
dd373385 19839 if (length == 0xffffffff)
613e1657 19840 {
fe1b8b76 19841 length = bfd_get_64 (abfd, buf + 4);
613e1657 19842 *bytes_read = 12;
613e1657 19843 }
dd373385 19844 else if (length == 0)
f7ef9339 19845 {
dd373385 19846 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19847 length = bfd_get_64 (abfd, buf);
f7ef9339 19848 *bytes_read = 8;
f7ef9339 19849 }
613e1657
KB
19850 else
19851 {
19852 *bytes_read = 4;
613e1657
KB
19853 }
19854
c764a876
DE
19855 return length;
19856}
dd373385 19857
c764a876
DE
19858/* Cover function for read_initial_length.
19859 Returns the length of the object at BUF, and stores the size of the
19860 initial length in *BYTES_READ and stores the size that offsets will be in
19861 *OFFSET_SIZE.
19862 If the initial length size is not equivalent to that specified in
19863 CU_HEADER then issue a complaint.
19864 This is useful when reading non-comp-unit headers. */
dd373385 19865
c764a876 19866static LONGEST
d521ce57 19867read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19868 const struct comp_unit_head *cu_header,
19869 unsigned int *bytes_read,
19870 unsigned int *offset_size)
19871{
19872 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19873
19874 gdb_assert (cu_header->initial_length_size == 4
19875 || cu_header->initial_length_size == 8
19876 || cu_header->initial_length_size == 12);
19877
19878 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19879 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19880
c764a876 19881 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19882 return length;
613e1657
KB
19883}
19884
19885/* Read an offset from the data stream. The size of the offset is
917c78fc 19886 given by cu_header->offset_size. */
613e1657
KB
19887
19888static LONGEST
d521ce57
TT
19889read_offset (bfd *abfd, const gdb_byte *buf,
19890 const struct comp_unit_head *cu_header,
891d2f0b 19891 unsigned int *bytes_read)
c764a876
DE
19892{
19893 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19894
c764a876
DE
19895 *bytes_read = cu_header->offset_size;
19896 return offset;
19897}
19898
19899/* Read an offset from the data stream. */
19900
19901static LONGEST
d521ce57 19902read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19903{
19904 LONGEST retval = 0;
19905
c764a876 19906 switch (offset_size)
613e1657
KB
19907 {
19908 case 4:
fe1b8b76 19909 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19910 break;
19911 case 8:
fe1b8b76 19912 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19913 break;
19914 default:
8e65ff28 19915 internal_error (__FILE__, __LINE__,
c764a876 19916 _("read_offset_1: bad switch [in module %s]"),
659b0389 19917 bfd_get_filename (abfd));
613e1657
KB
19918 }
19919
917c78fc 19920 return retval;
613e1657
KB
19921}
19922
d521ce57
TT
19923static const gdb_byte *
19924read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19925{
19926 /* If the size of a host char is 8 bits, we can return a pointer
19927 to the buffer, otherwise we have to copy the data to a buffer
19928 allocated on the temporary obstack. */
4bdf3d34 19929 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19930 return buf;
c906108c
SS
19931}
19932
d521ce57
TT
19933static const char *
19934read_direct_string (bfd *abfd, const gdb_byte *buf,
19935 unsigned int *bytes_read_ptr)
c906108c
SS
19936{
19937 /* If the size of a host char is 8 bits, we can return a pointer
19938 to the string, otherwise we have to copy the string to a buffer
19939 allocated on the temporary obstack. */
4bdf3d34 19940 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19941 if (*buf == '\0')
19942 {
19943 *bytes_read_ptr = 1;
19944 return NULL;
19945 }
d521ce57
TT
19946 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19947 return (const char *) buf;
4bdf3d34
JJ
19948}
19949
43988095
JK
19950/* Return pointer to string at section SECT offset STR_OFFSET with error
19951 reporting strings FORM_NAME and SECT_NAME. */
19952
d521ce57 19953static const char *
ed2dc618
SM
19954read_indirect_string_at_offset_from (struct objfile *objfile,
19955 bfd *abfd, LONGEST str_offset,
43988095
JK
19956 struct dwarf2_section_info *sect,
19957 const char *form_name,
19958 const char *sect_name)
19959{
ed2dc618 19960 dwarf2_read_section (objfile, sect);
43988095
JK
19961 if (sect->buffer == NULL)
19962 error (_("%s used without %s section [in module %s]"),
19963 form_name, sect_name, bfd_get_filename (abfd));
19964 if (str_offset >= sect->size)
19965 error (_("%s pointing outside of %s section [in module %s]"),
19966 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19967 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19968 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19969 return NULL;
43988095
JK
19970 return (const char *) (sect->buffer + str_offset);
19971}
19972
19973/* Return pointer to string at .debug_str offset STR_OFFSET. */
19974
19975static const char *
ed2dc618
SM
19976read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19977 bfd *abfd, LONGEST str_offset)
43988095 19978{
ed2dc618
SM
19979 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19980 abfd, str_offset,
43988095
JK
19981 &dwarf2_per_objfile->str,
19982 "DW_FORM_strp", ".debug_str");
19983}
19984
19985/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19986
19987static const char *
ed2dc618
SM
19988read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19989 bfd *abfd, LONGEST str_offset)
43988095 19990{
ed2dc618
SM
19991 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19992 abfd, str_offset,
43988095
JK
19993 &dwarf2_per_objfile->line_str,
19994 "DW_FORM_line_strp",
19995 ".debug_line_str");
c906108c
SS
19996}
19997
36586728
TT
19998/* Read a string at offset STR_OFFSET in the .debug_str section from
19999 the .dwz file DWZ. Throw an error if the offset is too large. If
20000 the string consists of a single NUL byte, return NULL; otherwise
20001 return a pointer to the string. */
20002
d521ce57 20003static const char *
ed2dc618
SM
20004read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20005 LONGEST str_offset)
36586728 20006{
ed2dc618 20007 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
20008
20009 if (dwz->str.buffer == NULL)
20010 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20011 "section [in module %s]"),
00f93c44 20012 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20013 if (str_offset >= dwz->str.size)
20014 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20015 ".debug_str section [in module %s]"),
00f93c44 20016 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20017 gdb_assert (HOST_CHAR_BIT == 8);
20018 if (dwz->str.buffer[str_offset] == '\0')
20019 return NULL;
d521ce57 20020 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
20021}
20022
43988095
JK
20023/* Return pointer to string at .debug_str offset as read from BUF.
20024 BUF is assumed to be in a compilation unit described by CU_HEADER.
20025 Return *BYTES_READ_PTR count of bytes read from BUF. */
20026
d521ce57 20027static const char *
ed2dc618
SM
20028read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20029 const gdb_byte *buf,
cf2c3c16
TT
20030 const struct comp_unit_head *cu_header,
20031 unsigned int *bytes_read_ptr)
20032{
20033 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20034
ed2dc618 20035 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
20036}
20037
43988095
JK
20038/* Return pointer to string at .debug_line_str offset as read from BUF.
20039 BUF is assumed to be in a compilation unit described by CU_HEADER.
20040 Return *BYTES_READ_PTR count of bytes read from BUF. */
20041
20042static const char *
ed2dc618
SM
20043read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20044 bfd *abfd, const gdb_byte *buf,
43988095
JK
20045 const struct comp_unit_head *cu_header,
20046 unsigned int *bytes_read_ptr)
20047{
20048 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20049
ed2dc618
SM
20050 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20051 str_offset);
43988095
JK
20052}
20053
20054ULONGEST
d521ce57 20055read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 20056 unsigned int *bytes_read_ptr)
c906108c 20057{
12df843f 20058 ULONGEST result;
ce5d95e1 20059 unsigned int num_read;
870f88f7 20060 int shift;
c906108c
SS
20061 unsigned char byte;
20062
20063 result = 0;
20064 shift = 0;
20065 num_read = 0;
c906108c
SS
20066 while (1)
20067 {
fe1b8b76 20068 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20069 buf++;
20070 num_read++;
12df843f 20071 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20072 if ((byte & 128) == 0)
20073 {
20074 break;
20075 }
20076 shift += 7;
20077 }
20078 *bytes_read_ptr = num_read;
20079 return result;
20080}
20081
12df843f 20082static LONGEST
d521ce57
TT
20083read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20084 unsigned int *bytes_read_ptr)
c906108c 20085{
4dd1b460 20086 ULONGEST result;
870f88f7 20087 int shift, num_read;
c906108c
SS
20088 unsigned char byte;
20089
20090 result = 0;
20091 shift = 0;
c906108c 20092 num_read = 0;
c906108c
SS
20093 while (1)
20094 {
fe1b8b76 20095 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20096 buf++;
20097 num_read++;
4dd1b460 20098 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20099 shift += 7;
20100 if ((byte & 128) == 0)
20101 {
20102 break;
20103 }
20104 }
77e0b926 20105 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 20106 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
20107 *bytes_read_ptr = num_read;
20108 return result;
20109}
20110
3019eac3 20111/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20112 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20113 ADDR_SIZE is the size of addresses from the CU header. */
20114
20115static CORE_ADDR
ed2dc618 20116read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
20117 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
20118 int addr_size)
3019eac3
DE
20119{
20120 struct objfile *objfile = dwarf2_per_objfile->objfile;
20121 bfd *abfd = objfile->obfd;
20122 const gdb_byte *info_ptr;
18a8505e 20123 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3
DE
20124
20125 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20126 if (dwarf2_per_objfile->addr.buffer == NULL)
20127 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20128 objfile_name (objfile));
18a8505e
AT
20129 if (addr_base_or_zero + addr_index * addr_size
20130 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
20131 error (_("DW_FORM_addr_index pointing outside of "
20132 ".debug_addr section [in module %s]"),
4262abfb 20133 objfile_name (objfile));
3019eac3 20134 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 20135 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
20136 if (addr_size == 4)
20137 return bfd_get_32 (abfd, info_ptr);
20138 else
20139 return bfd_get_64 (abfd, info_ptr);
20140}
20141
20142/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20143
20144static CORE_ADDR
20145read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20146{
518817b3
SM
20147 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20148 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20149}
20150
20151/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20152
20153static CORE_ADDR
d521ce57 20154read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20155 unsigned int *bytes_read)
20156{
518817b3 20157 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20158 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20159
20160 return read_addr_index (cu, addr_index);
20161}
20162
3019eac3
DE
20163/* Given an index in .debug_addr, fetch the value.
20164 NOTE: This can be called during dwarf expression evaluation,
20165 long after the debug information has been read, and thus per_cu->cu
20166 may no longer exist. */
20167
20168CORE_ADDR
20169dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20170 unsigned int addr_index)
20171{
ed2dc618 20172 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 20173 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 20174 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20175 int addr_size;
20176
3019eac3
DE
20177 /* We need addr_base and addr_size.
20178 If we don't have PER_CU->cu, we have to get it.
20179 Nasty, but the alternative is storing the needed info in PER_CU,
20180 which at this point doesn't seem justified: it's not clear how frequently
20181 it would get used and it would increase the size of every PER_CU.
20182 Entry points like dwarf2_per_cu_addr_size do a similar thing
20183 so we're not in uncharted territory here.
20184 Alas we need to be a bit more complicated as addr_base is contained
20185 in the DIE.
20186
20187 We don't need to read the entire CU(/TU).
20188 We just need the header and top level die.
a1b64ce1 20189
3019eac3 20190 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20191 For now we skip this optimization. */
3019eac3
DE
20192
20193 if (cu != NULL)
20194 {
20195 addr_base = cu->addr_base;
20196 addr_size = cu->header.addr_size;
20197 }
20198 else
20199 {
c0ab21c2
TT
20200 cutu_reader reader (per_cu, NULL, 0, 0, false);
20201 addr_base = reader.cu->addr_base;
20202 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20203 }
20204
ed2dc618
SM
20205 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20206 addr_size);
3019eac3
DE
20207}
20208
18a8505e
AT
20209/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20210 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20211 DWO file. */
3019eac3 20212
d521ce57 20213static const char *
18a8505e
AT
20214read_str_index (struct dwarf2_cu *cu,
20215 struct dwarf2_section_info *str_section,
20216 struct dwarf2_section_info *str_offsets_section,
20217 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20218{
518817b3
SM
20219 struct dwarf2_per_objfile *dwarf2_per_objfile
20220 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20221 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20222 const char *objf_name = objfile_name (objfile);
3019eac3 20223 bfd *abfd = objfile->obfd;
d521ce57 20224 const gdb_byte *info_ptr;
3019eac3 20225 ULONGEST str_offset;
cf532bd1 20226 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20227
73869dc2
DE
20228 dwarf2_read_section (objfile, str_section);
20229 dwarf2_read_section (objfile, str_offsets_section);
20230 if (str_section->buffer == NULL)
18a8505e 20231 error (_("%s used without %s section"
9d8780f0 20232 " in CU at offset %s [in module %s]"),
18a8505e
AT
20233 form_name, get_section_name (str_section),
20234 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20235 if (str_offsets_section->buffer == NULL)
18a8505e 20236 error (_("%s used without %s section"
9d8780f0 20237 " in CU at offset %s [in module %s]"),
18a8505e
AT
20238 form_name, get_section_name (str_section),
20239 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20240 info_ptr = (str_offsets_section->buffer
18a8505e 20241 + str_offsets_base
3019eac3
DE
20242 + str_index * cu->header.offset_size);
20243 if (cu->header.offset_size == 4)
20244 str_offset = bfd_get_32 (abfd, info_ptr);
20245 else
20246 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20247 if (str_offset >= str_section->size)
57d63ce2 20248 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20249 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20250 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20251 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20252}
20253
18a8505e
AT
20254/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20255
20256static const char *
20257read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20258{
20259 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20260 ? reader->cu->header.addr_size : 0;
20261 return read_str_index (reader->cu,
20262 &reader->dwo_file->sections.str,
20263 &reader->dwo_file->sections.str_offsets,
20264 str_offsets_base, str_index);
20265}
20266
20267/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20268
20269static const char *
20270read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20271{
20272 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20273 const char *objf_name = objfile_name (objfile);
20274 static const char form_name[] = "DW_FORM_GNU_str_index";
20275 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20276
20277 if (!cu->str_offsets_base.has_value ())
20278 error (_("%s used in Fission stub without %s"
20279 " in CU at offset 0x%lx [in module %s]"),
20280 form_name, str_offsets_attr_name,
20281 (long) cu->header.offset_size, objf_name);
20282
20283 return read_str_index (cu,
20284 &cu->per_cu->dwarf2_per_objfile->str,
20285 &cu->per_cu->dwarf2_per_objfile->str_offsets,
20286 *cu->str_offsets_base, str_index);
20287}
20288
3019eac3
DE
20289/* Return the length of an LEB128 number in BUF. */
20290
20291static int
20292leb128_size (const gdb_byte *buf)
20293{
20294 const gdb_byte *begin = buf;
20295 gdb_byte byte;
20296
20297 while (1)
20298 {
20299 byte = *buf++;
20300 if ((byte & 128) == 0)
20301 return buf - begin;
20302 }
20303}
20304
c906108c 20305static void
e142c38c 20306set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20307{
20308 switch (lang)
20309 {
20310 case DW_LANG_C89:
76bee0cc 20311 case DW_LANG_C99:
0cfd832f 20312 case DW_LANG_C11:
c906108c 20313 case DW_LANG_C:
d1be3247 20314 case DW_LANG_UPC:
e142c38c 20315 cu->language = language_c;
c906108c 20316 break;
9c37b5ae 20317 case DW_LANG_Java:
c906108c 20318 case DW_LANG_C_plus_plus:
0cfd832f
MW
20319 case DW_LANG_C_plus_plus_11:
20320 case DW_LANG_C_plus_plus_14:
e142c38c 20321 cu->language = language_cplus;
c906108c 20322 break;
6aecb9c2
JB
20323 case DW_LANG_D:
20324 cu->language = language_d;
20325 break;
c906108c
SS
20326 case DW_LANG_Fortran77:
20327 case DW_LANG_Fortran90:
b21b22e0 20328 case DW_LANG_Fortran95:
f7de9aab
MW
20329 case DW_LANG_Fortran03:
20330 case DW_LANG_Fortran08:
e142c38c 20331 cu->language = language_fortran;
c906108c 20332 break;
a766d390
DE
20333 case DW_LANG_Go:
20334 cu->language = language_go;
20335 break;
c906108c 20336 case DW_LANG_Mips_Assembler:
e142c38c 20337 cu->language = language_asm;
c906108c
SS
20338 break;
20339 case DW_LANG_Ada83:
8aaf0b47 20340 case DW_LANG_Ada95:
bc5f45f8
JB
20341 cu->language = language_ada;
20342 break;
72019c9c
GM
20343 case DW_LANG_Modula2:
20344 cu->language = language_m2;
20345 break;
fe8e67fd
PM
20346 case DW_LANG_Pascal83:
20347 cu->language = language_pascal;
20348 break;
22566fbd
DJ
20349 case DW_LANG_ObjC:
20350 cu->language = language_objc;
20351 break;
c44af4eb
TT
20352 case DW_LANG_Rust:
20353 case DW_LANG_Rust_old:
20354 cu->language = language_rust;
20355 break;
c906108c
SS
20356 case DW_LANG_Cobol74:
20357 case DW_LANG_Cobol85:
c906108c 20358 default:
e142c38c 20359 cu->language = language_minimal;
c906108c
SS
20360 break;
20361 }
e142c38c 20362 cu->language_defn = language_def (cu->language);
c906108c
SS
20363}
20364
20365/* Return the named attribute or NULL if not there. */
20366
20367static struct attribute *
e142c38c 20368dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20369{
a48e046c 20370 for (;;)
c906108c 20371 {
a48e046c
TT
20372 unsigned int i;
20373 struct attribute *spec = NULL;
20374
20375 for (i = 0; i < die->num_attrs; ++i)
20376 {
20377 if (die->attrs[i].name == name)
20378 return &die->attrs[i];
20379 if (die->attrs[i].name == DW_AT_specification
20380 || die->attrs[i].name == DW_AT_abstract_origin)
20381 spec = &die->attrs[i];
20382 }
20383
20384 if (!spec)
20385 break;
c906108c 20386
f2f0e013 20387 die = follow_die_ref (die, spec, &cu);
f2f0e013 20388 }
c5aa993b 20389
c906108c
SS
20390 return NULL;
20391}
20392
348e048f
DE
20393/* Return the named attribute or NULL if not there,
20394 but do not follow DW_AT_specification, etc.
20395 This is for use in contexts where we're reading .debug_types dies.
20396 Following DW_AT_specification, DW_AT_abstract_origin will take us
20397 back up the chain, and we want to go down. */
20398
20399static struct attribute *
45e58e77 20400dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20401{
20402 unsigned int i;
20403
20404 for (i = 0; i < die->num_attrs; ++i)
20405 if (die->attrs[i].name == name)
20406 return &die->attrs[i];
20407
20408 return NULL;
20409}
20410
7d45c7c3
KB
20411/* Return the string associated with a string-typed attribute, or NULL if it
20412 is either not found or is of an incorrect type. */
20413
20414static const char *
20415dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20416{
20417 struct attribute *attr;
20418 const char *str = NULL;
20419
20420 attr = dwarf2_attr (die, name, cu);
20421
20422 if (attr != NULL)
20423 {
43988095 20424 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20425 || attr->form == DW_FORM_string
cf532bd1 20426 || attr->form == DW_FORM_strx
8fe0f950
AT
20427 || attr->form == DW_FORM_strx1
20428 || attr->form == DW_FORM_strx2
20429 || attr->form == DW_FORM_strx3
20430 || attr->form == DW_FORM_strx4
b3340438 20431 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20432 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20433 str = DW_STRING (attr);
20434 else
b98664d3 20435 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20436 "DIE at %s in module %s"),
20437 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20438 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20439 }
20440
20441 return str;
20442}
20443
a084a2a6 20444/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20445 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20446static const char *
20447dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20448{
20449 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20450 if (dwo_name == nullptr)
20451 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20452 return dwo_name;
20453}
20454
05cf31d1
JB
20455/* Return non-zero iff the attribute NAME is defined for the given DIE,
20456 and holds a non-zero value. This function should only be used for
2dc7f7b3 20457 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20458
20459static int
20460dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20461{
20462 struct attribute *attr = dwarf2_attr (die, name, cu);
20463
20464 return (attr && DW_UNSND (attr));
20465}
20466
3ca72b44 20467static int
e142c38c 20468die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20469{
05cf31d1
JB
20470 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20471 which value is non-zero. However, we have to be careful with
20472 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20473 (via dwarf2_flag_true_p) follows this attribute. So we may
20474 end up accidently finding a declaration attribute that belongs
20475 to a different DIE referenced by the specification attribute,
20476 even though the given DIE does not have a declaration attribute. */
20477 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20478 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20479}
20480
63d06c5c 20481/* Return the die giving the specification for DIE, if there is
f2f0e013 20482 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20483 containing the return value on output. If there is no
20484 specification, but there is an abstract origin, that is
20485 returned. */
63d06c5c
DC
20486
20487static struct die_info *
f2f0e013 20488die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20489{
f2f0e013
DJ
20490 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20491 *spec_cu);
63d06c5c 20492
edb3359d
DJ
20493 if (spec_attr == NULL)
20494 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20495
63d06c5c
DC
20496 if (spec_attr == NULL)
20497 return NULL;
20498 else
f2f0e013 20499 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20500}
c906108c 20501
527f3840
JK
20502/* Stub for free_line_header to match void * callback types. */
20503
20504static void
20505free_line_header_voidp (void *arg)
20506{
9a3c8263 20507 struct line_header *lh = (struct line_header *) arg;
527f3840 20508
fff8551c 20509 delete lh;
527f3840
JK
20510}
20511
fff8551c
PA
20512void
20513line_header::add_include_dir (const char *include_dir)
c906108c 20514{
27e0867f 20515 if (dwarf_line_debug >= 2)
7ba99d21
AT
20516 {
20517 size_t new_size;
20518 if (version >= 5)
20519 new_size = m_include_dirs.size ();
20520 else
20521 new_size = m_include_dirs.size () + 1;
20522 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20523 new_size, include_dir);
20524 }
20525 m_include_dirs.push_back (include_dir);
debd256d 20526}
6e70227d 20527
fff8551c
PA
20528void
20529line_header::add_file_name (const char *name,
ecfb656c 20530 dir_index d_index,
fff8551c
PA
20531 unsigned int mod_time,
20532 unsigned int length)
debd256d 20533{
27e0867f 20534 if (dwarf_line_debug >= 2)
7ba99d21
AT
20535 {
20536 size_t new_size;
20537 if (version >= 5)
20538 new_size = file_names_size ();
20539 else
20540 new_size = file_names_size () + 1;
20541 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20542 new_size, name);
20543 }
20544 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20545}
6e70227d 20546
83769d0b 20547/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20548
20549static struct dwarf2_section_info *
20550get_debug_line_section (struct dwarf2_cu *cu)
20551{
20552 struct dwarf2_section_info *section;
518817b3
SM
20553 struct dwarf2_per_objfile *dwarf2_per_objfile
20554 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20555
20556 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20557 DWO file. */
20558 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20559 section = &cu->dwo_unit->dwo_file->sections.line;
20560 else if (cu->per_cu->is_dwz)
20561 {
ed2dc618 20562 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20563
20564 section = &dwz->line;
20565 }
20566 else
20567 section = &dwarf2_per_objfile->line;
20568
20569 return section;
20570}
20571
43988095
JK
20572/* Read directory or file name entry format, starting with byte of
20573 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20574 entries count and the entries themselves in the described entry
20575 format. */
20576
20577static void
ed2dc618
SM
20578read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20579 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20580 struct line_header *lh,
20581 const struct comp_unit_head *cu_header,
20582 void (*callback) (struct line_header *lh,
20583 const char *name,
ecfb656c 20584 dir_index d_index,
43988095
JK
20585 unsigned int mod_time,
20586 unsigned int length))
20587{
20588 gdb_byte format_count, formati;
20589 ULONGEST data_count, datai;
20590 const gdb_byte *buf = *bufp;
20591 const gdb_byte *format_header_data;
43988095
JK
20592 unsigned int bytes_read;
20593
20594 format_count = read_1_byte (abfd, buf);
20595 buf += 1;
20596 format_header_data = buf;
20597 for (formati = 0; formati < format_count; formati++)
20598 {
20599 read_unsigned_leb128 (abfd, buf, &bytes_read);
20600 buf += bytes_read;
20601 read_unsigned_leb128 (abfd, buf, &bytes_read);
20602 buf += bytes_read;
20603 }
20604
20605 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20606 buf += bytes_read;
20607 for (datai = 0; datai < data_count; datai++)
20608 {
20609 const gdb_byte *format = format_header_data;
20610 struct file_entry fe;
20611
43988095
JK
20612 for (formati = 0; formati < format_count; formati++)
20613 {
ecfb656c 20614 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20615 format += bytes_read;
43988095 20616
ecfb656c 20617 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20618 format += bytes_read;
ecfb656c
PA
20619
20620 gdb::optional<const char *> string;
20621 gdb::optional<unsigned int> uint;
20622
43988095
JK
20623 switch (form)
20624 {
20625 case DW_FORM_string:
ecfb656c 20626 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20627 buf += bytes_read;
20628 break;
20629
20630 case DW_FORM_line_strp:
ed2dc618
SM
20631 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20632 abfd, buf,
ecfb656c
PA
20633 cu_header,
20634 &bytes_read));
43988095
JK
20635 buf += bytes_read;
20636 break;
20637
20638 case DW_FORM_data1:
ecfb656c 20639 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20640 buf += 1;
20641 break;
20642
20643 case DW_FORM_data2:
ecfb656c 20644 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20645 buf += 2;
20646 break;
20647
20648 case DW_FORM_data4:
ecfb656c 20649 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20650 buf += 4;
20651 break;
20652
20653 case DW_FORM_data8:
ecfb656c 20654 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20655 buf += 8;
20656 break;
20657
7ba99d21
AT
20658 case DW_FORM_data16:
20659 /* This is used for MD5, but file_entry does not record MD5s. */
20660 buf += 16;
20661 break;
20662
43988095 20663 case DW_FORM_udata:
ecfb656c 20664 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20665 buf += bytes_read;
20666 break;
20667
20668 case DW_FORM_block:
20669 /* It is valid only for DW_LNCT_timestamp which is ignored by
20670 current GDB. */
20671 break;
20672 }
ecfb656c
PA
20673
20674 switch (content_type)
20675 {
20676 case DW_LNCT_path:
20677 if (string.has_value ())
20678 fe.name = *string;
20679 break;
20680 case DW_LNCT_directory_index:
20681 if (uint.has_value ())
20682 fe.d_index = (dir_index) *uint;
20683 break;
20684 case DW_LNCT_timestamp:
20685 if (uint.has_value ())
20686 fe.mod_time = *uint;
20687 break;
20688 case DW_LNCT_size:
20689 if (uint.has_value ())
20690 fe.length = *uint;
20691 break;
20692 case DW_LNCT_MD5:
20693 break;
20694 default:
b98664d3 20695 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20696 pulongest (content_type));
20697 }
43988095
JK
20698 }
20699
ecfb656c 20700 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20701 }
20702
20703 *bufp = buf;
20704}
20705
debd256d 20706/* Read the statement program header starting at OFFSET in
3019eac3 20707 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20708 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20709 Returns NULL if there is a problem reading the header, e.g., if it
20710 has a version we don't understand.
debd256d
JB
20711
20712 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20713 the returned object point into the dwarf line section buffer,
20714 and must not be freed. */
ae2de4f8 20715
fff8551c 20716static line_header_up
9c541725 20717dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20718{
d521ce57 20719 const gdb_byte *line_ptr;
c764a876 20720 unsigned int bytes_read, offset_size;
debd256d 20721 int i;
d521ce57 20722 const char *cur_dir, *cur_file;
3019eac3
DE
20723 struct dwarf2_section_info *section;
20724 bfd *abfd;
518817b3
SM
20725 struct dwarf2_per_objfile *dwarf2_per_objfile
20726 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20727
36586728 20728 section = get_debug_line_section (cu);
3019eac3
DE
20729 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20730 if (section->buffer == NULL)
debd256d 20731 {
3019eac3 20732 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20733 complaint (_("missing .debug_line.dwo section"));
3019eac3 20734 else
b98664d3 20735 complaint (_("missing .debug_line section"));
debd256d
JB
20736 return 0;
20737 }
20738
fceca515
DE
20739 /* We can't do this until we know the section is non-empty.
20740 Only then do we know we have such a section. */
a32a8923 20741 abfd = get_section_bfd_owner (section);
fceca515 20742
a738430d
MK
20743 /* Make sure that at least there's room for the total_length field.
20744 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20745 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20746 {
4d3c2250 20747 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20748 return 0;
20749 }
20750
fff8551c 20751 line_header_up lh (new line_header ());
debd256d 20752
9c541725 20753 lh->sect_off = sect_off;
527f3840
JK
20754 lh->offset_in_dwz = cu->per_cu->is_dwz;
20755
9c541725 20756 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20757
a738430d 20758 /* Read in the header. */
6e70227d 20759 lh->total_length =
c764a876
DE
20760 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20761 &bytes_read, &offset_size);
debd256d 20762 line_ptr += bytes_read;
7ba99d21
AT
20763
20764 const gdb_byte *start_here = line_ptr;
20765
3019eac3 20766 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20767 {
4d3c2250 20768 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20769 return 0;
20770 }
7ba99d21 20771 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20772 lh->version = read_2_bytes (abfd, line_ptr);
20773 line_ptr += 2;
43988095 20774 if (lh->version > 5)
cd366ee8
DE
20775 {
20776 /* This is a version we don't understand. The format could have
20777 changed in ways we don't handle properly so just punt. */
b98664d3 20778 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20779 return NULL;
20780 }
43988095
JK
20781 if (lh->version >= 5)
20782 {
20783 gdb_byte segment_selector_size;
20784
20785 /* Skip address size. */
20786 read_1_byte (abfd, line_ptr);
20787 line_ptr += 1;
20788
20789 segment_selector_size = read_1_byte (abfd, line_ptr);
20790 line_ptr += 1;
20791 if (segment_selector_size != 0)
20792 {
b98664d3 20793 complaint (_("unsupported segment selector size %u "
43988095
JK
20794 "in .debug_line section"),
20795 segment_selector_size);
20796 return NULL;
20797 }
20798 }
c764a876
DE
20799 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20800 line_ptr += offset_size;
7ba99d21 20801 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20802 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20803 line_ptr += 1;
2dc7f7b3
TT
20804 if (lh->version >= 4)
20805 {
20806 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20807 line_ptr += 1;
20808 }
20809 else
20810 lh->maximum_ops_per_instruction = 1;
20811
20812 if (lh->maximum_ops_per_instruction == 0)
20813 {
20814 lh->maximum_ops_per_instruction = 1;
b98664d3 20815 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20816 "in `.debug_line' section"));
2dc7f7b3
TT
20817 }
20818
debd256d
JB
20819 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20820 line_ptr += 1;
20821 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20822 line_ptr += 1;
20823 lh->line_range = read_1_byte (abfd, line_ptr);
20824 line_ptr += 1;
20825 lh->opcode_base = read_1_byte (abfd, line_ptr);
20826 line_ptr += 1;
fff8551c 20827 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20828
20829 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20830 for (i = 1; i < lh->opcode_base; ++i)
20831 {
20832 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20833 line_ptr += 1;
20834 }
20835
43988095 20836 if (lh->version >= 5)
debd256d 20837 {
43988095 20838 /* Read directory table. */
ed2dc618
SM
20839 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20840 &cu->header,
b926417a 20841 [] (struct line_header *header, const char *name,
ecfb656c 20842 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20843 unsigned int length)
20844 {
b926417a 20845 header->add_include_dir (name);
fff8551c 20846 });
debd256d 20847
43988095 20848 /* Read file name table. */
ed2dc618
SM
20849 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20850 &cu->header,
b926417a 20851 [] (struct line_header *header, const char *name,
ecfb656c 20852 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20853 unsigned int length)
20854 {
b926417a 20855 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20856 });
43988095
JK
20857 }
20858 else
debd256d 20859 {
43988095
JK
20860 /* Read directory table. */
20861 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20862 {
20863 line_ptr += bytes_read;
fff8551c 20864 lh->add_include_dir (cur_dir);
43988095 20865 }
debd256d
JB
20866 line_ptr += bytes_read;
20867
43988095
JK
20868 /* Read file name table. */
20869 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20870 {
ecfb656c
PA
20871 unsigned int mod_time, length;
20872 dir_index d_index;
43988095
JK
20873
20874 line_ptr += bytes_read;
ecfb656c 20875 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20876 line_ptr += bytes_read;
20877 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20878 line_ptr += bytes_read;
20879 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20880 line_ptr += bytes_read;
20881
ecfb656c 20882 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20883 }
20884 line_ptr += bytes_read;
debd256d 20885 }
debd256d 20886
3019eac3 20887 if (line_ptr > (section->buffer + section->size))
b98664d3 20888 complaint (_("line number info header doesn't "
3e43a32a 20889 "fit in `.debug_line' section"));
debd256d 20890
debd256d
JB
20891 return lh;
20892}
c906108c 20893
c6da4cef 20894/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20895 Return the file name of the psymtab for the given file_entry.
c6da4cef 20896 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20897 If space for the result is malloc'd, *NAME_HOLDER will be set.
20898 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20899
d521ce57 20900static const char *
7ba99d21 20901psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20902 const dwarf2_psymtab *pst,
c89b44cd
TT
20903 const char *comp_dir,
20904 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20905{
d521ce57
TT
20906 const char *include_name = fe.name;
20907 const char *include_name_to_compare = include_name;
72b9f47f 20908 const char *pst_filename;
c6da4cef
DE
20909 int file_is_pst;
20910
8c43009f 20911 const char *dir_name = fe.include_dir (lh);
c6da4cef 20912
c89b44cd 20913 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20914 if (!IS_ABSOLUTE_PATH (include_name)
20915 && (dir_name != NULL || comp_dir != NULL))
20916 {
20917 /* Avoid creating a duplicate psymtab for PST.
20918 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20919 Before we do the comparison, however, we need to account
20920 for DIR_NAME and COMP_DIR.
20921 First prepend dir_name (if non-NULL). If we still don't
20922 have an absolute path prepend comp_dir (if non-NULL).
20923 However, the directory we record in the include-file's
20924 psymtab does not contain COMP_DIR (to match the
20925 corresponding symtab(s)).
20926
20927 Example:
20928
20929 bash$ cd /tmp
20930 bash$ gcc -g ./hello.c
20931 include_name = "hello.c"
20932 dir_name = "."
20933 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20934 DW_AT_name = "./hello.c"
20935
20936 */
c6da4cef
DE
20937
20938 if (dir_name != NULL)
20939 {
c89b44cd
TT
20940 name_holder->reset (concat (dir_name, SLASH_STRING,
20941 include_name, (char *) NULL));
20942 include_name = name_holder->get ();
c6da4cef 20943 include_name_to_compare = include_name;
c6da4cef
DE
20944 }
20945 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20946 {
c89b44cd
TT
20947 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20948 include_name, (char *) NULL));
20949 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20950 }
20951 }
20952
20953 pst_filename = pst->filename;
c89b44cd 20954 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20955 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20956 {
c89b44cd
TT
20957 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20958 pst_filename, (char *) NULL));
20959 pst_filename = copied_name.get ();
c6da4cef
DE
20960 }
20961
1e3fad37 20962 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20963
c6da4cef
DE
20964 if (file_is_pst)
20965 return NULL;
20966 return include_name;
20967}
20968
d9b3de22
DE
20969/* State machine to track the state of the line number program. */
20970
6f77053d 20971class lnp_state_machine
d9b3de22 20972{
6f77053d
PA
20973public:
20974 /* Initialize a machine state for the start of a line number
20975 program. */
804d2729
TT
20976 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20977 bool record_lines_p);
6f77053d 20978
8c43009f
PA
20979 file_entry *current_file ()
20980 {
20981 /* lh->file_names is 0-based, but the file name numbers in the
20982 statement program are 1-based. */
6f77053d
PA
20983 return m_line_header->file_name_at (m_file);
20984 }
20985
20986 /* Record the line in the state machine. END_SEQUENCE is true if
20987 we're processing the end of a sequence. */
20988 void record_line (bool end_sequence);
20989
7ab6656f
OJ
20990 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20991 nop-out rest of the lines in this sequence. */
6f77053d
PA
20992 void check_line_address (struct dwarf2_cu *cu,
20993 const gdb_byte *line_ptr,
7ab6656f 20994 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20995
20996 void handle_set_discriminator (unsigned int discriminator)
20997 {
20998 m_discriminator = discriminator;
20999 m_line_has_non_zero_discriminator |= discriminator != 0;
21000 }
21001
21002 /* Handle DW_LNE_set_address. */
21003 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21004 {
21005 m_op_index = 0;
21006 address += baseaddr;
21007 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21008 }
21009
21010 /* Handle DW_LNS_advance_pc. */
21011 void handle_advance_pc (CORE_ADDR adjust);
21012
21013 /* Handle a special opcode. */
21014 void handle_special_opcode (unsigned char op_code);
21015
21016 /* Handle DW_LNS_advance_line. */
21017 void handle_advance_line (int line_delta)
21018 {
21019 advance_line (line_delta);
21020 }
21021
21022 /* Handle DW_LNS_set_file. */
21023 void handle_set_file (file_name_index file);
21024
21025 /* Handle DW_LNS_negate_stmt. */
21026 void handle_negate_stmt ()
21027 {
21028 m_is_stmt = !m_is_stmt;
21029 }
21030
21031 /* Handle DW_LNS_const_add_pc. */
21032 void handle_const_add_pc ();
21033
21034 /* Handle DW_LNS_fixed_advance_pc. */
21035 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21036 {
21037 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21038 m_op_index = 0;
21039 }
21040
21041 /* Handle DW_LNS_copy. */
21042 void handle_copy ()
21043 {
21044 record_line (false);
21045 m_discriminator = 0;
21046 }
21047
21048 /* Handle DW_LNE_end_sequence. */
21049 void handle_end_sequence ()
21050 {
804d2729 21051 m_currently_recording_lines = true;
6f77053d
PA
21052 }
21053
21054private:
21055 /* Advance the line by LINE_DELTA. */
21056 void advance_line (int line_delta)
21057 {
21058 m_line += line_delta;
21059
21060 if (line_delta != 0)
21061 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21062 }
21063
804d2729
TT
21064 struct dwarf2_cu *m_cu;
21065
6f77053d
PA
21066 gdbarch *m_gdbarch;
21067
21068 /* True if we're recording lines.
21069 Otherwise we're building partial symtabs and are just interested in
21070 finding include files mentioned by the line number program. */
21071 bool m_record_lines_p;
21072
8c43009f 21073 /* The line number header. */
6f77053d 21074 line_header *m_line_header;
8c43009f 21075
6f77053d
PA
21076 /* These are part of the standard DWARF line number state machine,
21077 and initialized according to the DWARF spec. */
d9b3de22 21078
6f77053d 21079 unsigned char m_op_index = 0;
7ba99d21
AT
21080 /* The line table index of the current file. */
21081 file_name_index m_file = 1;
6f77053d
PA
21082 unsigned int m_line = 1;
21083
21084 /* These are initialized in the constructor. */
21085
21086 CORE_ADDR m_address;
21087 bool m_is_stmt;
21088 unsigned int m_discriminator;
d9b3de22
DE
21089
21090 /* Additional bits of state we need to track. */
21091
21092 /* The last file that we called dwarf2_start_subfile for.
21093 This is only used for TLLs. */
6f77053d 21094 unsigned int m_last_file = 0;
d9b3de22 21095 /* The last file a line number was recorded for. */
6f77053d 21096 struct subfile *m_last_subfile = NULL;
d9b3de22 21097
804d2729
TT
21098 /* When true, record the lines we decode. */
21099 bool m_currently_recording_lines = false;
d9b3de22
DE
21100
21101 /* The last line number that was recorded, used to coalesce
21102 consecutive entries for the same line. This can happen, for
21103 example, when discriminators are present. PR 17276. */
6f77053d
PA
21104 unsigned int m_last_line = 0;
21105 bool m_line_has_non_zero_discriminator = false;
8c43009f 21106};
d9b3de22 21107
6f77053d
PA
21108void
21109lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21110{
21111 CORE_ADDR addr_adj = (((m_op_index + adjust)
21112 / m_line_header->maximum_ops_per_instruction)
21113 * m_line_header->minimum_instruction_length);
21114 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21115 m_op_index = ((m_op_index + adjust)
21116 % m_line_header->maximum_ops_per_instruction);
21117}
d9b3de22 21118
6f77053d
PA
21119void
21120lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21121{
6f77053d
PA
21122 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21123 CORE_ADDR addr_adj = (((m_op_index
21124 + (adj_opcode / m_line_header->line_range))
21125 / m_line_header->maximum_ops_per_instruction)
21126 * m_line_header->minimum_instruction_length);
21127 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21128 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21129 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21130
6f77053d
PA
21131 int line_delta = (m_line_header->line_base
21132 + (adj_opcode % m_line_header->line_range));
21133 advance_line (line_delta);
21134 record_line (false);
21135 m_discriminator = 0;
21136}
d9b3de22 21137
6f77053d
PA
21138void
21139lnp_state_machine::handle_set_file (file_name_index file)
21140{
21141 m_file = file;
21142
21143 const file_entry *fe = current_file ();
21144 if (fe == NULL)
21145 dwarf2_debug_line_missing_file_complaint ();
21146 else if (m_record_lines_p)
21147 {
21148 const char *dir = fe->include_dir (m_line_header);
21149
c24bdb02 21150 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21151 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21152 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21153 }
21154}
21155
21156void
21157lnp_state_machine::handle_const_add_pc ()
21158{
21159 CORE_ADDR adjust
21160 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21161
21162 CORE_ADDR addr_adj
21163 = (((m_op_index + adjust)
21164 / m_line_header->maximum_ops_per_instruction)
21165 * m_line_header->minimum_instruction_length);
21166
21167 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21168 m_op_index = ((m_op_index + adjust)
21169 % m_line_header->maximum_ops_per_instruction);
21170}
d9b3de22 21171
a05a36a5
DE
21172/* Return non-zero if we should add LINE to the line number table.
21173 LINE is the line to add, LAST_LINE is the last line that was added,
21174 LAST_SUBFILE is the subfile for LAST_LINE.
21175 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21176 had a non-zero discriminator.
21177
21178 We have to be careful in the presence of discriminators.
21179 E.g., for this line:
21180
21181 for (i = 0; i < 100000; i++);
21182
21183 clang can emit four line number entries for that one line,
21184 each with a different discriminator.
21185 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21186
21187 However, we want gdb to coalesce all four entries into one.
21188 Otherwise the user could stepi into the middle of the line and
21189 gdb would get confused about whether the pc really was in the
21190 middle of the line.
21191
21192 Things are further complicated by the fact that two consecutive
21193 line number entries for the same line is a heuristic used by gcc
21194 to denote the end of the prologue. So we can't just discard duplicate
21195 entries, we have to be selective about it. The heuristic we use is
21196 that we only collapse consecutive entries for the same line if at least
21197 one of those entries has a non-zero discriminator. PR 17276.
21198
21199 Note: Addresses in the line number state machine can never go backwards
21200 within one sequence, thus this coalescing is ok. */
21201
21202static int
804d2729
TT
21203dwarf_record_line_p (struct dwarf2_cu *cu,
21204 unsigned int line, unsigned int last_line,
a05a36a5
DE
21205 int line_has_non_zero_discriminator,
21206 struct subfile *last_subfile)
21207{
c24bdb02 21208 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21209 return 1;
21210 if (line != last_line)
21211 return 1;
21212 /* Same line for the same file that we've seen already.
21213 As a last check, for pr 17276, only record the line if the line
21214 has never had a non-zero discriminator. */
21215 if (!line_has_non_zero_discriminator)
21216 return 1;
21217 return 0;
21218}
21219
804d2729
TT
21220/* Use the CU's builder to record line number LINE beginning at
21221 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21222
21223static void
d9b3de22
DE
21224dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21225 unsigned int line, CORE_ADDR address,
804d2729 21226 struct dwarf2_cu *cu)
252a6764
DE
21227{
21228 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21229
27e0867f
DE
21230 if (dwarf_line_debug)
21231 {
21232 fprintf_unfiltered (gdb_stdlog,
21233 "Recording line %u, file %s, address %s\n",
21234 line, lbasename (subfile->name),
21235 paddress (gdbarch, address));
21236 }
21237
804d2729 21238 if (cu != nullptr)
c24bdb02 21239 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21240}
21241
21242/* Subroutine of dwarf_decode_lines_1 to simplify it.
21243 Mark the end of a set of line number records.
d9b3de22 21244 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21245 If SUBFILE is NULL the request is ignored. */
21246
21247static void
21248dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21249 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21250{
27e0867f
DE
21251 if (subfile == NULL)
21252 return;
21253
21254 if (dwarf_line_debug)
21255 {
21256 fprintf_unfiltered (gdb_stdlog,
21257 "Finishing current line, file %s, address %s\n",
21258 lbasename (subfile->name),
21259 paddress (gdbarch, address));
21260 }
21261
804d2729 21262 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21263}
21264
6f77053d
PA
21265void
21266lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21267{
d9b3de22
DE
21268 if (dwarf_line_debug)
21269 {
21270 fprintf_unfiltered (gdb_stdlog,
21271 "Processing actual line %u: file %u,"
94a72be7 21272 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21273 m_line, m_file,
6f77053d 21274 paddress (m_gdbarch, m_address),
94a72be7
AB
21275 m_is_stmt, m_discriminator,
21276 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21277 }
21278
6f77053d 21279 file_entry *fe = current_file ();
8c43009f
PA
21280
21281 if (fe == NULL)
d9b3de22
DE
21282 dwarf2_debug_line_missing_file_complaint ();
21283 /* For now we ignore lines not starting on an instruction boundary.
21284 But not when processing end_sequence for compatibility with the
21285 previous version of the code. */
6f77053d 21286 else if (m_op_index == 0 || end_sequence)
d9b3de22 21287 {
8c43009f 21288 fe->included_p = 1;
94a72be7
AB
21289 if (m_record_lines_p
21290 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 21291 {
c24bdb02 21292 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21293 || end_sequence)
d9b3de22 21294 {
804d2729
TT
21295 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21296 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21297 }
21298
21299 if (!end_sequence)
21300 {
804d2729 21301 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21302 m_line_has_non_zero_discriminator,
21303 m_last_subfile))
d9b3de22 21304 {
c24bdb02 21305 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21306 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21307 builder->get_current_subfile (),
6f77053d 21308 m_line, m_address,
804d2729 21309 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21310 }
c24bdb02 21311 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21312 m_last_line = m_line;
d9b3de22
DE
21313 }
21314 }
21315 }
21316}
21317
804d2729
TT
21318lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21319 line_header *lh, bool record_lines_p)
d9b3de22 21320{
804d2729 21321 m_cu = cu;
6f77053d
PA
21322 m_gdbarch = arch;
21323 m_record_lines_p = record_lines_p;
21324 m_line_header = lh;
d9b3de22 21325
804d2729 21326 m_currently_recording_lines = true;
d9b3de22 21327
d9b3de22
DE
21328 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21329 was a line entry for it so that the backend has a chance to adjust it
21330 and also record it in case it needs it. This is currently used by MIPS
21331 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21332 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21333 m_is_stmt = lh->default_is_stmt;
21334 m_discriminator = 0;
252a6764
DE
21335}
21336
6f77053d
PA
21337void
21338lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21339 const gdb_byte *line_ptr,
7ab6656f 21340 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21341{
7ab6656f
OJ
21342 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21343 the pc range of the CU. However, we restrict the test to only ADDRESS
21344 values of zero to preserve GDB's previous behaviour which is to handle
21345 the specific case of a function being GC'd by the linker. */
924c2928 21346
7ab6656f 21347 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21348 {
21349 /* This line table is for a function which has been
21350 GCd by the linker. Ignore it. PR gdb/12528 */
21351
518817b3 21352 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21353 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21354
b98664d3 21355 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21356 line_offset, objfile_name (objfile));
804d2729
TT
21357 m_currently_recording_lines = false;
21358 /* Note: m_currently_recording_lines is left as false until we see
21359 DW_LNE_end_sequence. */
924c2928
DE
21360 }
21361}
21362
f3f5162e 21363/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21364 Process the line number information in LH.
21365 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21366 program in order to set included_p for every referenced header. */
debd256d 21367
c906108c 21368static void
43f3e411
DE
21369dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21370 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21371{
d521ce57
TT
21372 const gdb_byte *line_ptr, *extended_end;
21373 const gdb_byte *line_end;
a8c50c1f 21374 unsigned int bytes_read, extended_len;
699ca60a 21375 unsigned char op_code, extended_op;
e142c38c 21376 CORE_ADDR baseaddr;
518817b3 21377 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21378 bfd *abfd = objfile->obfd;
fbf65064 21379 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21380 /* True if we're recording line info (as opposed to building partial
21381 symtabs and just interested in finding include files mentioned by
21382 the line number program). */
21383 bool record_lines_p = !decode_for_pst_p;
e142c38c 21384
b3b3bada 21385 baseaddr = objfile->text_section_offset ();
c906108c 21386
debd256d
JB
21387 line_ptr = lh->statement_program_start;
21388 line_end = lh->statement_program_end;
c906108c
SS
21389
21390 /* Read the statement sequences until there's nothing left. */
21391 while (line_ptr < line_end)
21392 {
6f77053d
PA
21393 /* The DWARF line number program state machine. Reset the state
21394 machine at the start of each sequence. */
804d2729 21395 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21396 bool end_sequence = false;
d9b3de22 21397
8c43009f 21398 if (record_lines_p)
c906108c 21399 {
8c43009f
PA
21400 /* Start a subfile for the current file of the state
21401 machine. */
21402 const file_entry *fe = state_machine.current_file ();
21403
21404 if (fe != NULL)
804d2729 21405 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21406 }
21407
a738430d 21408 /* Decode the table. */
d9b3de22 21409 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21410 {
21411 op_code = read_1_byte (abfd, line_ptr);
21412 line_ptr += 1;
9aa1fe7e 21413
debd256d 21414 if (op_code >= lh->opcode_base)
6e70227d 21415 {
8e07a239 21416 /* Special opcode. */
6f77053d 21417 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21418 }
21419 else switch (op_code)
c906108c
SS
21420 {
21421 case DW_LNS_extended_op:
3e43a32a
MS
21422 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21423 &bytes_read);
473b7be6 21424 line_ptr += bytes_read;
a8c50c1f 21425 extended_end = line_ptr + extended_len;
c906108c
SS
21426 extended_op = read_1_byte (abfd, line_ptr);
21427 line_ptr += 1;
21428 switch (extended_op)
21429 {
21430 case DW_LNE_end_sequence:
6f77053d
PA
21431 state_machine.handle_end_sequence ();
21432 end_sequence = true;
c906108c
SS
21433 break;
21434 case DW_LNE_set_address:
d9b3de22
DE
21435 {
21436 CORE_ADDR address
21437 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21438 line_ptr += bytes_read;
6f77053d
PA
21439
21440 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21441 lowpc - baseaddr, address);
6f77053d 21442 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21443 }
c906108c
SS
21444 break;
21445 case DW_LNE_define_file:
debd256d 21446 {
d521ce57 21447 const char *cur_file;
ecfb656c
PA
21448 unsigned int mod_time, length;
21449 dir_index dindex;
6e70227d 21450
3e43a32a
MS
21451 cur_file = read_direct_string (abfd, line_ptr,
21452 &bytes_read);
debd256d 21453 line_ptr += bytes_read;
ecfb656c 21454 dindex = (dir_index)
debd256d
JB
21455 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21456 line_ptr += bytes_read;
21457 mod_time =
21458 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21459 line_ptr += bytes_read;
21460 length =
21461 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21462 line_ptr += bytes_read;
ecfb656c 21463 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21464 }
c906108c 21465 break;
d0c6ba3d 21466 case DW_LNE_set_discriminator:
6f77053d
PA
21467 {
21468 /* The discriminator is not interesting to the
21469 debugger; just ignore it. We still need to
21470 check its value though:
21471 if there are consecutive entries for the same
21472 (non-prologue) line we want to coalesce them.
21473 PR 17276. */
21474 unsigned int discr
21475 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21476 line_ptr += bytes_read;
21477
21478 state_machine.handle_set_discriminator (discr);
21479 }
d0c6ba3d 21480 break;
c906108c 21481 default:
b98664d3 21482 complaint (_("mangled .debug_line section"));
debd256d 21483 return;
c906108c 21484 }
a8c50c1f
DJ
21485 /* Make sure that we parsed the extended op correctly. If e.g.
21486 we expected a different address size than the producer used,
21487 we may have read the wrong number of bytes. */
21488 if (line_ptr != extended_end)
21489 {
b98664d3 21490 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21491 return;
21492 }
c906108c
SS
21493 break;
21494 case DW_LNS_copy:
6f77053d 21495 state_machine.handle_copy ();
c906108c
SS
21496 break;
21497 case DW_LNS_advance_pc:
2dc7f7b3
TT
21498 {
21499 CORE_ADDR adjust
21500 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21501 line_ptr += bytes_read;
6f77053d
PA
21502
21503 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21504 }
c906108c
SS
21505 break;
21506 case DW_LNS_advance_line:
a05a36a5
DE
21507 {
21508 int line_delta
21509 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21510 line_ptr += bytes_read;
6f77053d
PA
21511
21512 state_machine.handle_advance_line (line_delta);
a05a36a5 21513 }
c906108c
SS
21514 break;
21515 case DW_LNS_set_file:
d9b3de22 21516 {
6f77053d 21517 file_name_index file
ecfb656c
PA
21518 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21519 &bytes_read);
d9b3de22 21520 line_ptr += bytes_read;
8c43009f 21521
6f77053d 21522 state_machine.handle_set_file (file);
d9b3de22 21523 }
c906108c
SS
21524 break;
21525 case DW_LNS_set_column:
0ad93d4f 21526 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21527 line_ptr += bytes_read;
21528 break;
21529 case DW_LNS_negate_stmt:
6f77053d 21530 state_machine.handle_negate_stmt ();
c906108c
SS
21531 break;
21532 case DW_LNS_set_basic_block:
c906108c 21533 break;
c2c6d25f
JM
21534 /* Add to the address register of the state machine the
21535 address increment value corresponding to special opcode
a738430d
MK
21536 255. I.e., this value is scaled by the minimum
21537 instruction length since special opcode 255 would have
b021a221 21538 scaled the increment. */
c906108c 21539 case DW_LNS_const_add_pc:
6f77053d 21540 state_machine.handle_const_add_pc ();
c906108c
SS
21541 break;
21542 case DW_LNS_fixed_advance_pc:
3e29f34a 21543 {
6f77053d 21544 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21545 line_ptr += 2;
6f77053d
PA
21546
21547 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21548 }
c906108c 21549 break;
9aa1fe7e 21550 default:
a738430d
MK
21551 {
21552 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21553 int i;
a738430d 21554
debd256d 21555 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21556 {
21557 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21558 line_ptr += bytes_read;
21559 }
21560 }
c906108c
SS
21561 }
21562 }
d9b3de22
DE
21563
21564 if (!end_sequence)
21565 dwarf2_debug_line_missing_end_sequence_complaint ();
21566
21567 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21568 in which case we still finish recording the last line). */
6f77053d 21569 state_machine.record_line (true);
c906108c 21570 }
f3f5162e
DE
21571}
21572
21573/* Decode the Line Number Program (LNP) for the given line_header
21574 structure and CU. The actual information extracted and the type
21575 of structures created from the LNP depends on the value of PST.
21576
21577 1. If PST is NULL, then this procedure uses the data from the program
21578 to create all necessary symbol tables, and their linetables.
21579
21580 2. If PST is not NULL, this procedure reads the program to determine
21581 the list of files included by the unit represented by PST, and
21582 builds all the associated partial symbol tables.
21583
21584 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21585 It is used for relative paths in the line table.
21586 NOTE: When processing partial symtabs (pst != NULL),
21587 comp_dir == pst->dirname.
21588
21589 NOTE: It is important that psymtabs have the same file name (via strcmp)
21590 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21591 symtab we don't use it in the name of the psymtabs we create.
21592 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21593 A good testcase for this is mb-inline.exp.
21594
527f3840
JK
21595 LOWPC is the lowest address in CU (or 0 if not known).
21596
21597 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21598 for its PC<->lines mapping information. Otherwise only the filename
21599 table is read in. */
f3f5162e
DE
21600
21601static void
21602dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21603 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21604 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21605{
518817b3 21606 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21607 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21608
527f3840
JK
21609 if (decode_mapping)
21610 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21611
21612 if (decode_for_pst_p)
21613 {
aaa75496
JB
21614 /* Now that we're done scanning the Line Header Program, we can
21615 create the psymtab of each included file. */
7ba99d21
AT
21616 for (auto &file_entry : lh->file_names ())
21617 if (file_entry.included_p == 1)
aaa75496 21618 {
c89b44cd 21619 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21620 const char *include_name =
7ba99d21
AT
21621 psymtab_include_file_name (lh, file_entry, pst,
21622 comp_dir, &name_holder);
c6da4cef 21623 if (include_name != NULL)
aaa75496
JB
21624 dwarf2_create_include_psymtab (include_name, pst, objfile);
21625 }
21626 }
cb1df416
DJ
21627 else
21628 {
21629 /* Make sure a symtab is created for every file, even files
21630 which contain only variables (i.e. no code with associated
21631 line numbers). */
c24bdb02
KS
21632 buildsym_compunit *builder = cu->get_builder ();
21633 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21634
7ba99d21 21635 for (auto &fe : lh->file_names ())
cb1df416 21636 {
804d2729 21637 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21638 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21639 {
c24bdb02 21640 builder->get_current_subfile ()->symtab
804d2729 21641 = allocate_symtab (cust,
c24bdb02 21642 builder->get_current_subfile ()->name);
43f3e411 21643 }
c24bdb02 21644 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21645 }
21646 }
c906108c
SS
21647}
21648
21649/* Start a subfile for DWARF. FILENAME is the name of the file and
21650 DIRNAME the name of the source directory which contains FILENAME
4d663531 21651 or NULL if not known.
c906108c
SS
21652 This routine tries to keep line numbers from identical absolute and
21653 relative file names in a common subfile.
21654
21655 Using the `list' example from the GDB testsuite, which resides in
21656 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21657 of /srcdir/list0.c yields the following debugging information for list0.c:
21658
c5aa993b 21659 DW_AT_name: /srcdir/list0.c
4d663531 21660 DW_AT_comp_dir: /compdir
357e46e7 21661 files.files[0].name: list0.h
c5aa993b 21662 files.files[0].dir: /srcdir
357e46e7 21663 files.files[1].name: list0.c
c5aa993b 21664 files.files[1].dir: /srcdir
c906108c
SS
21665
21666 The line number information for list0.c has to end up in a single
4f1520fb
FR
21667 subfile, so that `break /srcdir/list0.c:1' works as expected.
21668 start_subfile will ensure that this happens provided that we pass the
21669 concatenation of files.files[1].dir and files.files[1].name as the
21670 subfile's name. */
c906108c
SS
21671
21672static void
804d2729
TT
21673dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21674 const char *dirname)
c906108c 21675{
43816ebc 21676 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21677
4d663531 21678 /* In order not to lose the line information directory,
4f1520fb
FR
21679 we concatenate it to the filename when it makes sense.
21680 Note that the Dwarf3 standard says (speaking of filenames in line
21681 information): ``The directory index is ignored for file names
21682 that represent full path names''. Thus ignoring dirname in the
21683 `else' branch below isn't an issue. */
c906108c 21684
d5166ae1 21685 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21686 {
43816ebc
TT
21687 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21688 filename = copy.get ();
d521ce57 21689 }
c906108c 21690
c24bdb02 21691 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21692}
21693
804d2729
TT
21694/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21695 buildsym_compunit constructor. */
f4dc4d17 21696
c24bdb02
KS
21697struct compunit_symtab *
21698dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21699 CORE_ADDR low_pc)
f4dc4d17 21700{
c24bdb02 21701 gdb_assert (m_builder == nullptr);
43f3e411 21702
c24bdb02
KS
21703 m_builder.reset (new struct buildsym_compunit
21704 (per_cu->dwarf2_per_objfile->objfile,
21705 name, comp_dir, language, low_pc));
93b8bea4 21706
c24bdb02 21707 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21708
c24bdb02
KS
21709 get_builder ()->record_debugformat ("DWARF 2");
21710 get_builder ()->record_producer (producer);
f4dc4d17 21711
c24bdb02 21712 processing_has_namespace_info = false;
43f3e411 21713
c24bdb02 21714 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21715}
21716
4c2df51b
DJ
21717static void
21718var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21719 struct dwarf2_cu *cu)
4c2df51b 21720{
518817b3 21721 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21722 struct comp_unit_head *cu_header = &cu->header;
21723
4c2df51b
DJ
21724 /* NOTE drow/2003-01-30: There used to be a comment and some special
21725 code here to turn a symbol with DW_AT_external and a
21726 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21727 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21728 with some versions of binutils) where shared libraries could have
21729 relocations against symbols in their debug information - the
21730 minimal symbol would have the right address, but the debug info
21731 would not. It's no longer necessary, because we will explicitly
21732 apply relocations when we read in the debug information now. */
21733
21734 /* A DW_AT_location attribute with no contents indicates that a
21735 variable has been optimized away. */
21736 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21737 {
f1e6e072 21738 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21739 return;
21740 }
21741
21742 /* Handle one degenerate form of location expression specially, to
21743 preserve GDB's previous behavior when section offsets are
336d760d
AT
21744 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21745 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21746
21747 if (attr_form_is_block (attr)
3019eac3
DE
21748 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21749 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21750 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21751 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21752 && (DW_BLOCK (attr)->size
21753 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21754 {
891d2f0b 21755 unsigned int dummy;
4c2df51b 21756
3019eac3 21757 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21758 SET_SYMBOL_VALUE_ADDRESS (sym,
21759 read_address (objfile->obfd,
21760 DW_BLOCK (attr)->data + 1,
21761 cu, &dummy));
3019eac3 21762 else
38583298
TT
21763 SET_SYMBOL_VALUE_ADDRESS
21764 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21765 &dummy));
f1e6e072 21766 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21767 fixup_symbol_section (sym, objfile);
6a053cb1
TT
21768 SET_SYMBOL_VALUE_ADDRESS
21769 (sym,
21770 SYMBOL_VALUE_ADDRESS (sym)
21771 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
21772 return;
21773 }
21774
21775 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21776 expression evaluator, and use LOC_COMPUTED only when necessary
21777 (i.e. when the value of a register or memory location is
21778 referenced, or a thread-local block, etc.). Then again, it might
21779 not be worthwhile. I'm assuming that it isn't unless performance
21780 or memory numbers show me otherwise. */
21781
f1e6e072 21782 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21783
f1e6e072 21784 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21785 cu->has_loclist = true;
4c2df51b
DJ
21786}
21787
c906108c
SS
21788/* Given a pointer to a DWARF information entry, figure out if we need
21789 to make a symbol table entry for it, and if so, create a new entry
21790 and return a pointer to it.
21791 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21792 used the passed type.
21793 If SPACE is not NULL, use it to hold the new symbol. If it is
21794 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21795
21796static struct symbol *
5e2db402
TT
21797new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21798 struct symbol *space)
c906108c 21799{
518817b3
SM
21800 struct dwarf2_per_objfile *dwarf2_per_objfile
21801 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21802 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21803 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21804 struct symbol *sym = NULL;
15d034d0 21805 const char *name;
c906108c
SS
21806 struct attribute *attr = NULL;
21807 struct attribute *attr2 = NULL;
e142c38c 21808 CORE_ADDR baseaddr;
e37fd15a
SW
21809 struct pending **list_to_add = NULL;
21810
edb3359d 21811 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21812
b3b3bada 21813 baseaddr = objfile->text_section_offset ();
c906108c 21814
94af9270 21815 name = dwarf2_name (die, cu);
c906108c
SS
21816 if (name)
21817 {
94af9270 21818 const char *linkagename;
34eaf542 21819 int suppress_add = 0;
94af9270 21820
34eaf542
TT
21821 if (space)
21822 sym = space;
21823 else
e623cf5d 21824 sym = allocate_symbol (objfile);
c906108c 21825 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21826
21827 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21828 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 21829 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 21830 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 21831
f55ee35c
JK
21832 /* Fortran does not have mangling standard and the mangling does differ
21833 between gfortran, iFort etc. */
21834 if (cu->language == language_fortran
468c0cbb
CB
21835 && symbol_get_demangled_name (sym) == NULL)
21836 symbol_set_demangled_name (sym,
cfc594ee 21837 dwarf2_full_name (name, die, cu),
29df156d 21838 NULL);
f55ee35c 21839
c906108c 21840 /* Default assumptions.
c5aa993b 21841 Use the passed type or decode it from the die. */
176620f1 21842 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21843 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21844 if (type != NULL)
21845 SYMBOL_TYPE (sym) = type;
21846 else
e7c27a73 21847 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21848 attr = dwarf2_attr (die,
21849 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21850 cu);
435d3d88 21851 if (attr != nullptr)
c906108c
SS
21852 {
21853 SYMBOL_LINE (sym) = DW_UNSND (attr);
21854 }
cb1df416 21855
edb3359d
DJ
21856 attr = dwarf2_attr (die,
21857 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21858 cu);
435d3d88 21859 if (attr != nullptr)
cb1df416 21860 {
ecfb656c 21861 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21862 struct file_entry *fe;
9a619af0 21863
ecfb656c
PA
21864 if (cu->line_header != NULL)
21865 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21866 else
21867 fe = NULL;
21868
21869 if (fe == NULL)
b98664d3 21870 complaint (_("file index out of range"));
8c43009f
PA
21871 else
21872 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21873 }
21874
c906108c
SS
21875 switch (die->tag)
21876 {
21877 case DW_TAG_label:
e142c38c 21878 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21879 if (attr != nullptr)
3e29f34a
MR
21880 {
21881 CORE_ADDR addr;
21882
21883 addr = attr_value_as_address (attr);
21884 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21885 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21886 }
0f5238ed
TT
21887 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21888 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21889 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21890 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21891 break;
21892 case DW_TAG_subprogram:
21893 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21894 finish_block. */
f1e6e072 21895 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21896 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21897 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21898 || cu->language == language_ada
21899 || cu->language == language_fortran)
c906108c 21900 {
2cfa0c8d 21901 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21902 Ada and Fortran subprograms, whether marked external or
21903 not, are always stored as a global symbol, because we want
21904 to be able to access them globally. For instance, we want
21905 to be able to break on a nested subprogram without having
21906 to specify the context. */
c24bdb02 21907 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21908 }
21909 else
21910 {
e37fd15a 21911 list_to_add = cu->list_in_scope;
c906108c
SS
21912 }
21913 break;
edb3359d
DJ
21914 case DW_TAG_inlined_subroutine:
21915 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21916 finish_block. */
f1e6e072 21917 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21918 SYMBOL_INLINED (sym) = 1;
481860b3 21919 list_to_add = cu->list_in_scope;
edb3359d 21920 break;
34eaf542
TT
21921 case DW_TAG_template_value_param:
21922 suppress_add = 1;
21923 /* Fall through. */
72929c62 21924 case DW_TAG_constant:
c906108c 21925 case DW_TAG_variable:
254e6b9e 21926 case DW_TAG_member:
0963b4bd
MS
21927 /* Compilation with minimal debug info may result in
21928 variables with missing type entries. Change the
21929 misleading `void' type to something sensible. */
c906108c 21930 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21931 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21932
e142c38c 21933 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21934 /* In the case of DW_TAG_member, we should only be called for
21935 static const members. */
21936 if (die->tag == DW_TAG_member)
21937 {
3863f96c
DE
21938 /* dwarf2_add_field uses die_is_declaration,
21939 so we do the same. */
254e6b9e
DE
21940 gdb_assert (die_is_declaration (die, cu));
21941 gdb_assert (attr);
21942 }
435d3d88 21943 if (attr != nullptr)
c906108c 21944 {
e7c27a73 21945 dwarf2_const_value (attr, sym, cu);
e142c38c 21946 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21947 if (!suppress_add)
34eaf542
TT
21948 {
21949 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21950 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21951 else
e37fd15a 21952 list_to_add = cu->list_in_scope;
34eaf542 21953 }
c906108c
SS
21954 break;
21955 }
e142c38c 21956 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21957 if (attr != nullptr)
c906108c 21958 {
e7c27a73 21959 var_decode_location (attr, sym, cu);
e142c38c 21960 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21961
21962 /* Fortran explicitly imports any global symbols to the local
21963 scope by DW_TAG_common_block. */
21964 if (cu->language == language_fortran && die->parent
21965 && die->parent->tag == DW_TAG_common_block)
21966 attr2 = NULL;
21967
caac4577
JG
21968 if (SYMBOL_CLASS (sym) == LOC_STATIC
21969 && SYMBOL_VALUE_ADDRESS (sym) == 0
21970 && !dwarf2_per_objfile->has_section_at_zero)
21971 {
21972 /* When a static variable is eliminated by the linker,
21973 the corresponding debug information is not stripped
21974 out, but the variable address is set to null;
21975 do not add such variables into symbol table. */
21976 }
21977 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21978 {
4b610737
TT
21979 if (SYMBOL_CLASS (sym) == LOC_STATIC
21980 && (objfile->flags & OBJF_MAINLINE) == 0
21981 && dwarf2_per_objfile->can_copy)
21982 {
21983 /* A global static variable might be subject to
21984 copy relocation. We first check for a local
21985 minsym, though, because maybe the symbol was
21986 marked hidden, in which case this would not
21987 apply. */
21988 bound_minimal_symbol found
21989 = (lookup_minimal_symbol_linkage
987012b8 21990 (sym->linkage_name (), objfile));
4b610737
TT
21991 if (found.minsym != nullptr)
21992 sym->maybe_copied = 1;
21993 }
f55ee35c 21994
1c809c68
TT
21995 /* A variable with DW_AT_external is never static,
21996 but it may be block-scoped. */
804d2729 21997 list_to_add
c24bdb02
KS
21998 = ((cu->list_in_scope
21999 == cu->get_builder ()->get_file_symbols ())
22000 ? cu->get_builder ()->get_global_symbols ()
804d2729 22001 : cu->list_in_scope);
1c809c68 22002 }
c906108c 22003 else
e37fd15a 22004 list_to_add = cu->list_in_scope;
c906108c
SS
22005 }
22006 else
22007 {
22008 /* We do not know the address of this symbol.
c5aa993b
JM
22009 If it is an external symbol and we have type information
22010 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22011 The address of the variable will then be determined from
22012 the minimal symbol table whenever the variable is
22013 referenced. */
e142c38c 22014 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22015
22016 /* Fortran explicitly imports any global symbols to the local
22017 scope by DW_TAG_common_block. */
22018 if (cu->language == language_fortran && die->parent
22019 && die->parent->tag == DW_TAG_common_block)
22020 {
22021 /* SYMBOL_CLASS doesn't matter here because
22022 read_common_block is going to reset it. */
22023 if (!suppress_add)
22024 list_to_add = cu->list_in_scope;
22025 }
22026 else if (attr2 && (DW_UNSND (attr2) != 0)
22027 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22028 {
0fe7935b
DJ
22029 /* A variable with DW_AT_external is never static, but it
22030 may be block-scoped. */
804d2729 22031 list_to_add
c24bdb02
KS
22032 = ((cu->list_in_scope
22033 == cu->get_builder ()->get_file_symbols ())
22034 ? cu->get_builder ()->get_global_symbols ()
804d2729 22035 : cu->list_in_scope);
0fe7935b 22036
f1e6e072 22037 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22038 }
442ddf59
JK
22039 else if (!die_is_declaration (die, cu))
22040 {
22041 /* Use the default LOC_OPTIMIZED_OUT class. */
22042 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22043 if (!suppress_add)
22044 list_to_add = cu->list_in_scope;
442ddf59 22045 }
c906108c
SS
22046 }
22047 break;
22048 case DW_TAG_formal_parameter:
a60f3166
TT
22049 {
22050 /* If we are inside a function, mark this as an argument. If
22051 not, we might be looking at an argument to an inlined function
22052 when we do not have enough information to show inlined frames;
22053 pretend it's a local variable in that case so that the user can
22054 still see it. */
804d2729 22055 struct context_stack *curr
c24bdb02 22056 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22057 if (curr != nullptr && curr->name != nullptr)
22058 SYMBOL_IS_ARGUMENT (sym) = 1;
22059 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22060 if (attr != nullptr)
a60f3166
TT
22061 {
22062 var_decode_location (attr, sym, cu);
22063 }
22064 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22065 if (attr != nullptr)
a60f3166
TT
22066 {
22067 dwarf2_const_value (attr, sym, cu);
22068 }
f346a30d 22069
a60f3166
TT
22070 list_to_add = cu->list_in_scope;
22071 }
c906108c
SS
22072 break;
22073 case DW_TAG_unspecified_parameters:
22074 /* From varargs functions; gdb doesn't seem to have any
22075 interest in this information, so just ignore it for now.
22076 (FIXME?) */
22077 break;
34eaf542
TT
22078 case DW_TAG_template_type_param:
22079 suppress_add = 1;
22080 /* Fall through. */
c906108c 22081 case DW_TAG_class_type:
680b30c7 22082 case DW_TAG_interface_type:
c906108c
SS
22083 case DW_TAG_structure_type:
22084 case DW_TAG_union_type:
72019c9c 22085 case DW_TAG_set_type:
c906108c 22086 case DW_TAG_enumeration_type:
f1e6e072 22087 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22088 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22089
63d06c5c 22090 {
9c37b5ae 22091 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22092 really ever be static objects: otherwise, if you try
22093 to, say, break of a class's method and you're in a file
22094 which doesn't mention that class, it won't work unless
22095 the check for all static symbols in lookup_symbol_aux
22096 saves you. See the OtherFileClass tests in
22097 gdb.c++/namespace.exp. */
22098
e37fd15a 22099 if (!suppress_add)
34eaf542 22100 {
c24bdb02 22101 buildsym_compunit *builder = cu->get_builder ();
804d2729 22102 list_to_add
c24bdb02 22103 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22104 && cu->language == language_cplus
c24bdb02 22105 ? builder->get_global_symbols ()
804d2729 22106 : cu->list_in_scope);
63d06c5c 22107
64382290 22108 /* The semantics of C++ state that "struct foo {
9c37b5ae 22109 ... }" also defines a typedef for "foo". */
64382290 22110 if (cu->language == language_cplus
45280282 22111 || cu->language == language_ada
c44af4eb
TT
22112 || cu->language == language_d
22113 || cu->language == language_rust)
64382290
TT
22114 {
22115 /* The symbol's name is already allocated along
22116 with this objfile, so we don't need to
22117 duplicate it for the type. */
22118 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 22119 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 22120 }
63d06c5c
DC
22121 }
22122 }
c906108c
SS
22123 break;
22124 case DW_TAG_typedef:
f1e6e072 22125 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22126 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22127 list_to_add = cu->list_in_scope;
63d06c5c 22128 break;
c906108c 22129 case DW_TAG_base_type:
a02abb62 22130 case DW_TAG_subrange_type:
f1e6e072 22131 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22132 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22133 list_to_add = cu->list_in_scope;
c906108c
SS
22134 break;
22135 case DW_TAG_enumerator:
e142c38c 22136 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22137 if (attr != nullptr)
c906108c 22138 {
e7c27a73 22139 dwarf2_const_value (attr, sym, cu);
c906108c 22140 }
63d06c5c
DC
22141 {
22142 /* NOTE: carlton/2003-11-10: See comment above in the
22143 DW_TAG_class_type, etc. block. */
22144
804d2729 22145 list_to_add
c24bdb02 22146 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22147 && cu->language == language_cplus
c24bdb02 22148 ? cu->get_builder ()->get_global_symbols ()
804d2729 22149 : cu->list_in_scope);
63d06c5c 22150 }
c906108c 22151 break;
74921315 22152 case DW_TAG_imported_declaration:
5c4e30ca 22153 case DW_TAG_namespace:
f1e6e072 22154 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22155 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22156 break;
530e8392
KB
22157 case DW_TAG_module:
22158 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22159 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22160 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22161 break;
4357ac6c 22162 case DW_TAG_common_block:
f1e6e072 22163 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22164 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22165 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22166 break;
c906108c
SS
22167 default:
22168 /* Not a tag we recognize. Hopefully we aren't processing
22169 trash data, but since we must specifically ignore things
22170 we don't recognize, there is nothing else we should do at
0963b4bd 22171 this point. */
b98664d3 22172 complaint (_("unsupported tag: '%s'"),
4d3c2250 22173 dwarf_tag_name (die->tag));
c906108c
SS
22174 break;
22175 }
df8a16a1 22176
e37fd15a
SW
22177 if (suppress_add)
22178 {
22179 sym->hash_next = objfile->template_symbols;
22180 objfile->template_symbols = sym;
22181 list_to_add = NULL;
22182 }
22183
22184 if (list_to_add != NULL)
d3cb6808 22185 add_symbol_to_list (sym, list_to_add);
e37fd15a 22186
df8a16a1
DJ
22187 /* For the benefit of old versions of GCC, check for anonymous
22188 namespaces based on the demangled name. */
4d4ec4e5 22189 if (!cu->processing_has_namespace_info
94af9270 22190 && cu->language == language_cplus)
c24bdb02 22191 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22192 }
22193 return (sym);
22194}
22195
98bfdba5
PA
22196/* Given an attr with a DW_FORM_dataN value in host byte order,
22197 zero-extend it as appropriate for the symbol's type. The DWARF
22198 standard (v4) is not entirely clear about the meaning of using
22199 DW_FORM_dataN for a constant with a signed type, where the type is
22200 wider than the data. The conclusion of a discussion on the DWARF
22201 list was that this is unspecified. We choose to always zero-extend
22202 because that is the interpretation long in use by GCC. */
c906108c 22203
98bfdba5 22204static gdb_byte *
ff39bb5e 22205dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22206 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22207{
518817b3 22208 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22209 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22210 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22211 LONGEST l = DW_UNSND (attr);
22212
22213 if (bits < sizeof (*value) * 8)
22214 {
22215 l &= ((LONGEST) 1 << bits) - 1;
22216 *value = l;
22217 }
22218 else if (bits == sizeof (*value) * 8)
22219 *value = l;
22220 else
22221 {
224c3ddb 22222 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22223 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22224 return bytes;
22225 }
22226
22227 return NULL;
22228}
22229
22230/* Read a constant value from an attribute. Either set *VALUE, or if
22231 the value does not fit in *VALUE, set *BYTES - either already
22232 allocated on the objfile obstack, or newly allocated on OBSTACK,
22233 or, set *BATON, if we translated the constant to a location
22234 expression. */
22235
22236static void
ff39bb5e 22237dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22238 const char *name, struct obstack *obstack,
22239 struct dwarf2_cu *cu,
d521ce57 22240 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22241 struct dwarf2_locexpr_baton **baton)
22242{
518817b3 22243 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22244 struct comp_unit_head *cu_header = &cu->header;
c906108c 22245 struct dwarf_block *blk;
98bfdba5
PA
22246 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22247 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22248
22249 *value = 0;
22250 *bytes = NULL;
22251 *baton = NULL;
c906108c
SS
22252
22253 switch (attr->form)
22254 {
22255 case DW_FORM_addr:
336d760d 22256 case DW_FORM_addrx:
3019eac3 22257 case DW_FORM_GNU_addr_index:
ac56253d 22258 {
ac56253d
TT
22259 gdb_byte *data;
22260
98bfdba5
PA
22261 if (TYPE_LENGTH (type) != cu_header->addr_size)
22262 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22263 cu_header->addr_size,
98bfdba5 22264 TYPE_LENGTH (type));
ac56253d
TT
22265 /* Symbols of this form are reasonably rare, so we just
22266 piggyback on the existing location code rather than writing
22267 a new implementation of symbol_computed_ops. */
8d749320 22268 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22269 (*baton)->per_cu = cu->per_cu;
22270 gdb_assert ((*baton)->per_cu);
ac56253d 22271
98bfdba5 22272 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22273 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22274 (*baton)->data = data;
ac56253d
TT
22275
22276 data[0] = DW_OP_addr;
22277 store_unsigned_integer (&data[1], cu_header->addr_size,
22278 byte_order, DW_ADDR (attr));
22279 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22280 }
c906108c 22281 break;
4ac36638 22282 case DW_FORM_string:
93b5768b 22283 case DW_FORM_strp:
cf532bd1 22284 case DW_FORM_strx:
3019eac3 22285 case DW_FORM_GNU_str_index:
36586728 22286 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22287 /* DW_STRING is already allocated on the objfile obstack, point
22288 directly to it. */
d521ce57 22289 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22290 break;
c906108c
SS
22291 case DW_FORM_block1:
22292 case DW_FORM_block2:
22293 case DW_FORM_block4:
22294 case DW_FORM_block:
2dc7f7b3 22295 case DW_FORM_exprloc:
0224619f 22296 case DW_FORM_data16:
c906108c 22297 blk = DW_BLOCK (attr);
98bfdba5
PA
22298 if (TYPE_LENGTH (type) != blk->size)
22299 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22300 TYPE_LENGTH (type));
22301 *bytes = blk->data;
c906108c 22302 break;
2df3850c
JM
22303
22304 /* The DW_AT_const_value attributes are supposed to carry the
22305 symbol's value "represented as it would be on the target
22306 architecture." By the time we get here, it's already been
22307 converted to host endianness, so we just need to sign- or
22308 zero-extend it as appropriate. */
22309 case DW_FORM_data1:
3aef2284 22310 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22311 break;
c906108c 22312 case DW_FORM_data2:
3aef2284 22313 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22314 break;
c906108c 22315 case DW_FORM_data4:
3aef2284 22316 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22317 break;
c906108c 22318 case DW_FORM_data8:
3aef2284 22319 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22320 break;
22321
c906108c 22322 case DW_FORM_sdata:
663c44ac 22323 case DW_FORM_implicit_const:
98bfdba5 22324 *value = DW_SND (attr);
2df3850c
JM
22325 break;
22326
c906108c 22327 case DW_FORM_udata:
98bfdba5 22328 *value = DW_UNSND (attr);
c906108c 22329 break;
2df3850c 22330
c906108c 22331 default:
b98664d3 22332 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22333 dwarf_form_name (attr->form));
98bfdba5 22334 *value = 0;
c906108c
SS
22335 break;
22336 }
22337}
22338
2df3850c 22339
98bfdba5
PA
22340/* Copy constant value from an attribute to a symbol. */
22341
2df3850c 22342static void
ff39bb5e 22343dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22344 struct dwarf2_cu *cu)
2df3850c 22345{
518817b3 22346 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22347 LONGEST value;
d521ce57 22348 const gdb_byte *bytes;
98bfdba5 22349 struct dwarf2_locexpr_baton *baton;
2df3850c 22350
98bfdba5 22351 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22352 sym->print_name (),
98bfdba5
PA
22353 &objfile->objfile_obstack, cu,
22354 &value, &bytes, &baton);
2df3850c 22355
98bfdba5
PA
22356 if (baton != NULL)
22357 {
98bfdba5 22358 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22359 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22360 }
22361 else if (bytes != NULL)
22362 {
22363 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22364 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22365 }
22366 else
22367 {
22368 SYMBOL_VALUE (sym) = value;
f1e6e072 22369 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22370 }
2df3850c
JM
22371}
22372
c906108c
SS
22373/* Return the type of the die in question using its DW_AT_type attribute. */
22374
22375static struct type *
e7c27a73 22376die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22377{
c906108c 22378 struct attribute *type_attr;
c906108c 22379
e142c38c 22380 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22381 if (!type_attr)
22382 {
518817b3 22383 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22384 /* A missing DW_AT_type represents a void type. */
518817b3 22385 return objfile_type (objfile)->builtin_void;
c906108c 22386 }
348e048f 22387
673bfd45 22388 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22389}
22390
b4ba55a1
JB
22391/* True iff CU's producer generates GNAT Ada auxiliary information
22392 that allows to find parallel types through that information instead
22393 of having to do expensive parallel lookups by type name. */
22394
22395static int
22396need_gnat_info (struct dwarf2_cu *cu)
22397{
de4cb04a
JB
22398 /* Assume that the Ada compiler was GNAT, which always produces
22399 the auxiliary information. */
22400 return (cu->language == language_ada);
b4ba55a1
JB
22401}
22402
b4ba55a1
JB
22403/* Return the auxiliary type of the die in question using its
22404 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22405 attribute is not present. */
22406
22407static struct type *
22408die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22409{
b4ba55a1 22410 struct attribute *type_attr;
b4ba55a1
JB
22411
22412 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22413 if (!type_attr)
22414 return NULL;
22415
673bfd45 22416 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22417}
22418
22419/* If DIE has a descriptive_type attribute, then set the TYPE's
22420 descriptive type accordingly. */
22421
22422static void
22423set_descriptive_type (struct type *type, struct die_info *die,
22424 struct dwarf2_cu *cu)
22425{
22426 struct type *descriptive_type = die_descriptive_type (die, cu);
22427
22428 if (descriptive_type)
22429 {
22430 ALLOCATE_GNAT_AUX_TYPE (type);
22431 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22432 }
22433}
22434
c906108c
SS
22435/* Return the containing type of the die in question using its
22436 DW_AT_containing_type attribute. */
22437
22438static struct type *
e7c27a73 22439die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22440{
c906108c 22441 struct attribute *type_attr;
518817b3 22442 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22443
e142c38c 22444 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22445 if (!type_attr)
22446 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22447 "[in module %s]"), objfile_name (objfile));
33ac96f0 22448
673bfd45 22449 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22450}
22451
ac9ec31b
DE
22452/* Return an error marker type to use for the ill formed type in DIE/CU. */
22453
22454static struct type *
22455build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22456{
518817b3
SM
22457 struct dwarf2_per_objfile *dwarf2_per_objfile
22458 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22459 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22460 char *saved;
ac9ec31b 22461
528e1572
SM
22462 std::string message
22463 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22464 objfile_name (objfile),
22465 sect_offset_str (cu->header.sect_off),
22466 sect_offset_str (die->sect_off));
efba19b0 22467 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22468
19f392bc 22469 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22470}
22471
673bfd45 22472/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22473 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22474 DW_AT_containing_type.
673bfd45
DE
22475 If there is no type substitute an error marker. */
22476
c906108c 22477static struct type *
ff39bb5e 22478lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22479 struct dwarf2_cu *cu)
c906108c 22480{
518817b3
SM
22481 struct dwarf2_per_objfile *dwarf2_per_objfile
22482 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22483 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22484 struct type *this_type;
22485
ac9ec31b
DE
22486 gdb_assert (attr->name == DW_AT_type
22487 || attr->name == DW_AT_GNAT_descriptive_type
22488 || attr->name == DW_AT_containing_type);
22489
673bfd45
DE
22490 /* First see if we have it cached. */
22491
36586728
TT
22492 if (attr->form == DW_FORM_GNU_ref_alt)
22493 {
22494 struct dwarf2_per_cu_data *per_cu;
9c541725 22495 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22496
ed2dc618
SM
22497 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22498 dwarf2_per_objfile);
9c541725 22499 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22500 }
7771576e 22501 else if (attr_form_is_ref (attr))
673bfd45 22502 {
9c541725 22503 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22504
9c541725 22505 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22506 }
55f1336d 22507 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22508 {
ac9ec31b 22509 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22510
ac9ec31b 22511 return get_signatured_type (die, signature, cu);
673bfd45
DE
22512 }
22513 else
22514 {
b98664d3 22515 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22516 " at %s [in module %s]"),
22517 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22518 objfile_name (objfile));
ac9ec31b 22519 return build_error_marker_type (cu, die);
673bfd45
DE
22520 }
22521
22522 /* If not cached we need to read it in. */
22523
22524 if (this_type == NULL)
22525 {
ac9ec31b 22526 struct die_info *type_die = NULL;
673bfd45
DE
22527 struct dwarf2_cu *type_cu = cu;
22528
7771576e 22529 if (attr_form_is_ref (attr))
ac9ec31b
DE
22530 type_die = follow_die_ref (die, attr, &type_cu);
22531 if (type_die == NULL)
22532 return build_error_marker_type (cu, die);
22533 /* If we find the type now, it's probably because the type came
3019eac3
DE
22534 from an inter-CU reference and the type's CU got expanded before
22535 ours. */
ac9ec31b 22536 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22537 }
22538
22539 /* If we still don't have a type use an error marker. */
22540
22541 if (this_type == NULL)
ac9ec31b 22542 return build_error_marker_type (cu, die);
673bfd45 22543
f792889a 22544 return this_type;
c906108c
SS
22545}
22546
673bfd45
DE
22547/* Return the type in DIE, CU.
22548 Returns NULL for invalid types.
22549
02142a6c 22550 This first does a lookup in die_type_hash,
673bfd45
DE
22551 and only reads the die in if necessary.
22552
22553 NOTE: This can be called when reading in partial or full symbols. */
22554
f792889a 22555static struct type *
e7c27a73 22556read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22557{
f792889a
DJ
22558 struct type *this_type;
22559
22560 this_type = get_die_type (die, cu);
22561 if (this_type)
22562 return this_type;
22563
673bfd45
DE
22564 return read_type_die_1 (die, cu);
22565}
22566
22567/* Read the type in DIE, CU.
22568 Returns NULL for invalid types. */
22569
22570static struct type *
22571read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22572{
22573 struct type *this_type = NULL;
22574
c906108c
SS
22575 switch (die->tag)
22576 {
22577 case DW_TAG_class_type:
680b30c7 22578 case DW_TAG_interface_type:
c906108c
SS
22579 case DW_TAG_structure_type:
22580 case DW_TAG_union_type:
f792889a 22581 this_type = read_structure_type (die, cu);
c906108c
SS
22582 break;
22583 case DW_TAG_enumeration_type:
f792889a 22584 this_type = read_enumeration_type (die, cu);
c906108c
SS
22585 break;
22586 case DW_TAG_subprogram:
22587 case DW_TAG_subroutine_type:
edb3359d 22588 case DW_TAG_inlined_subroutine:
f792889a 22589 this_type = read_subroutine_type (die, cu);
c906108c
SS
22590 break;
22591 case DW_TAG_array_type:
f792889a 22592 this_type = read_array_type (die, cu);
c906108c 22593 break;
72019c9c 22594 case DW_TAG_set_type:
f792889a 22595 this_type = read_set_type (die, cu);
72019c9c 22596 break;
c906108c 22597 case DW_TAG_pointer_type:
f792889a 22598 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22599 break;
22600 case DW_TAG_ptr_to_member_type:
f792889a 22601 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22602 break;
22603 case DW_TAG_reference_type:
4297a3f0
AV
22604 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22605 break;
22606 case DW_TAG_rvalue_reference_type:
22607 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22608 break;
22609 case DW_TAG_const_type:
f792889a 22610 this_type = read_tag_const_type (die, cu);
c906108c
SS
22611 break;
22612 case DW_TAG_volatile_type:
f792889a 22613 this_type = read_tag_volatile_type (die, cu);
c906108c 22614 break;
06d66ee9
TT
22615 case DW_TAG_restrict_type:
22616 this_type = read_tag_restrict_type (die, cu);
22617 break;
c906108c 22618 case DW_TAG_string_type:
f792889a 22619 this_type = read_tag_string_type (die, cu);
c906108c
SS
22620 break;
22621 case DW_TAG_typedef:
f792889a 22622 this_type = read_typedef (die, cu);
c906108c 22623 break;
a02abb62 22624 case DW_TAG_subrange_type:
f792889a 22625 this_type = read_subrange_type (die, cu);
a02abb62 22626 break;
c906108c 22627 case DW_TAG_base_type:
f792889a 22628 this_type = read_base_type (die, cu);
c906108c 22629 break;
81a17f79 22630 case DW_TAG_unspecified_type:
f792889a 22631 this_type = read_unspecified_type (die, cu);
81a17f79 22632 break;
0114d602
DJ
22633 case DW_TAG_namespace:
22634 this_type = read_namespace_type (die, cu);
22635 break;
f55ee35c
JK
22636 case DW_TAG_module:
22637 this_type = read_module_type (die, cu);
22638 break;
a2c2acaf
MW
22639 case DW_TAG_atomic_type:
22640 this_type = read_tag_atomic_type (die, cu);
22641 break;
c906108c 22642 default:
b98664d3 22643 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22644 dwarf_tag_name (die->tag));
c906108c
SS
22645 break;
22646 }
63d06c5c 22647
f792889a 22648 return this_type;
63d06c5c
DC
22649}
22650
abc72ce4
DE
22651/* See if we can figure out if the class lives in a namespace. We do
22652 this by looking for a member function; its demangled name will
22653 contain namespace info, if there is any.
22654 Return the computed name or NULL.
22655 Space for the result is allocated on the objfile's obstack.
22656 This is the full-die version of guess_partial_die_structure_name.
22657 In this case we know DIE has no useful parent. */
22658
43816ebc 22659static const char *
abc72ce4
DE
22660guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22661{
22662 struct die_info *spec_die;
22663 struct dwarf2_cu *spec_cu;
22664 struct die_info *child;
518817b3 22665 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22666
22667 spec_cu = cu;
22668 spec_die = die_specification (die, &spec_cu);
22669 if (spec_die != NULL)
22670 {
22671 die = spec_die;
22672 cu = spec_cu;
22673 }
22674
22675 for (child = die->child;
22676 child != NULL;
22677 child = child->sibling)
22678 {
22679 if (child->tag == DW_TAG_subprogram)
22680 {
73b9be8b 22681 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22682
7d45c7c3 22683 if (linkage_name != NULL)
abc72ce4 22684 {
43816ebc
TT
22685 gdb::unique_xmalloc_ptr<char> actual_name
22686 (language_class_name_from_physname (cu->language_defn,
22687 linkage_name));
22688 const char *name = NULL;
abc72ce4
DE
22689
22690 if (actual_name != NULL)
22691 {
15d034d0 22692 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22693
22694 if (die_name != NULL
43816ebc 22695 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22696 {
22697 /* Strip off the class name from the full name.
22698 We want the prefix. */
22699 int die_name_len = strlen (die_name);
43816ebc
TT
22700 int actual_name_len = strlen (actual_name.get ());
22701 const char *ptr = actual_name.get ();
abc72ce4
DE
22702
22703 /* Test for '::' as a sanity check. */
22704 if (actual_name_len > die_name_len + 2
43816ebc 22705 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22706 name = obstack_strndup (
e3b94546 22707 &objfile->per_bfd->storage_obstack,
43816ebc 22708 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22709 }
22710 }
abc72ce4
DE
22711 return name;
22712 }
22713 }
22714 }
22715
22716 return NULL;
22717}
22718
96408a79
SA
22719/* GCC might emit a nameless typedef that has a linkage name. Determine the
22720 prefix part in such case. See
22721 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22722
a121b7c1 22723static const char *
96408a79
SA
22724anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22725{
22726 struct attribute *attr;
e6a959d6 22727 const char *base;
96408a79
SA
22728
22729 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22730 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22731 return NULL;
22732
7d45c7c3 22733 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22734 return NULL;
22735
73b9be8b 22736 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22737 if (attr == NULL || DW_STRING (attr) == NULL)
22738 return NULL;
22739
22740 /* dwarf2_name had to be already called. */
22741 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22742
22743 /* Strip the base name, keep any leading namespaces/classes. */
22744 base = strrchr (DW_STRING (attr), ':');
22745 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22746 return "";
22747
518817b3 22748 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22749 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22750 DW_STRING (attr),
22751 &base[-1] - DW_STRING (attr));
96408a79
SA
22752}
22753
fdde2d81 22754/* Return the name of the namespace/class that DIE is defined within,
0114d602 22755 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22756
0114d602
DJ
22757 For example, if we're within the method foo() in the following
22758 code:
22759
22760 namespace N {
22761 class C {
22762 void foo () {
22763 }
22764 };
22765 }
22766
22767 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22768
0d5cff50 22769static const char *
e142c38c 22770determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22771{
518817b3
SM
22772 struct dwarf2_per_objfile *dwarf2_per_objfile
22773 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22774 struct die_info *parent, *spec_die;
22775 struct dwarf2_cu *spec_cu;
22776 struct type *parent_type;
a121b7c1 22777 const char *retval;
63d06c5c 22778
9c37b5ae 22779 if (cu->language != language_cplus
c44af4eb
TT
22780 && cu->language != language_fortran && cu->language != language_d
22781 && cu->language != language_rust)
0114d602
DJ
22782 return "";
22783
96408a79
SA
22784 retval = anonymous_struct_prefix (die, cu);
22785 if (retval)
22786 return retval;
22787
0114d602
DJ
22788 /* We have to be careful in the presence of DW_AT_specification.
22789 For example, with GCC 3.4, given the code
22790
22791 namespace N {
22792 void foo() {
22793 // Definition of N::foo.
22794 }
22795 }
22796
22797 then we'll have a tree of DIEs like this:
22798
22799 1: DW_TAG_compile_unit
22800 2: DW_TAG_namespace // N
22801 3: DW_TAG_subprogram // declaration of N::foo
22802 4: DW_TAG_subprogram // definition of N::foo
22803 DW_AT_specification // refers to die #3
22804
22805 Thus, when processing die #4, we have to pretend that we're in
22806 the context of its DW_AT_specification, namely the contex of die
22807 #3. */
22808 spec_cu = cu;
22809 spec_die = die_specification (die, &spec_cu);
22810 if (spec_die == NULL)
22811 parent = die->parent;
22812 else
63d06c5c 22813 {
0114d602
DJ
22814 parent = spec_die->parent;
22815 cu = spec_cu;
63d06c5c 22816 }
0114d602
DJ
22817
22818 if (parent == NULL)
22819 return "";
98bfdba5
PA
22820 else if (parent->building_fullname)
22821 {
22822 const char *name;
22823 const char *parent_name;
22824
22825 /* It has been seen on RealView 2.2 built binaries,
22826 DW_TAG_template_type_param types actually _defined_ as
22827 children of the parent class:
22828
22829 enum E {};
22830 template class <class Enum> Class{};
22831 Class<enum E> class_e;
22832
22833 1: DW_TAG_class_type (Class)
22834 2: DW_TAG_enumeration_type (E)
22835 3: DW_TAG_enumerator (enum1:0)
22836 3: DW_TAG_enumerator (enum2:1)
22837 ...
22838 2: DW_TAG_template_type_param
22839 DW_AT_type DW_FORM_ref_udata (E)
22840
22841 Besides being broken debug info, it can put GDB into an
22842 infinite loop. Consider:
22843
22844 When we're building the full name for Class<E>, we'll start
22845 at Class, and go look over its template type parameters,
22846 finding E. We'll then try to build the full name of E, and
22847 reach here. We're now trying to build the full name of E,
22848 and look over the parent DIE for containing scope. In the
22849 broken case, if we followed the parent DIE of E, we'd again
22850 find Class, and once again go look at its template type
22851 arguments, etc., etc. Simply don't consider such parent die
22852 as source-level parent of this die (it can't be, the language
22853 doesn't allow it), and break the loop here. */
22854 name = dwarf2_name (die, cu);
22855 parent_name = dwarf2_name (parent, cu);
b98664d3 22856 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22857 name ? name : "<unknown>",
22858 parent_name ? parent_name : "<unknown>");
22859 return "";
22860 }
63d06c5c 22861 else
0114d602
DJ
22862 switch (parent->tag)
22863 {
63d06c5c 22864 case DW_TAG_namespace:
0114d602 22865 parent_type = read_type_die (parent, cu);
acebe513
UW
22866 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22867 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22868 Work around this problem here. */
22869 if (cu->language == language_cplus
e86ca25f 22870 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22871 return "";
0114d602 22872 /* We give a name to even anonymous namespaces. */
e86ca25f 22873 return TYPE_NAME (parent_type);
63d06c5c 22874 case DW_TAG_class_type:
680b30c7 22875 case DW_TAG_interface_type:
63d06c5c 22876 case DW_TAG_structure_type:
0114d602 22877 case DW_TAG_union_type:
f55ee35c 22878 case DW_TAG_module:
0114d602 22879 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22880 if (TYPE_NAME (parent_type) != NULL)
22881 return TYPE_NAME (parent_type);
0114d602
DJ
22882 else
22883 /* An anonymous structure is only allowed non-static data
22884 members; no typedefs, no member functions, et cetera.
22885 So it does not need a prefix. */
22886 return "";
abc72ce4 22887 case DW_TAG_compile_unit:
95554aad 22888 case DW_TAG_partial_unit:
abc72ce4
DE
22889 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22890 if (cu->language == language_cplus
fd5866f6 22891 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22892 && die->child != NULL
22893 && (die->tag == DW_TAG_class_type
22894 || die->tag == DW_TAG_structure_type
22895 || die->tag == DW_TAG_union_type))
22896 {
43816ebc 22897 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22898 if (name != NULL)
22899 return name;
22900 }
22901 return "";
0a4b0913
AB
22902 case DW_TAG_subprogram:
22903 /* Nested subroutines in Fortran get a prefix with the name
22904 of the parent's subroutine. */
22905 if (cu->language == language_fortran)
22906 {
22907 if ((die->tag == DW_TAG_subprogram)
22908 && (dwarf2_name (parent, cu) != NULL))
22909 return dwarf2_name (parent, cu);
22910 }
22911 return determine_prefix (parent, cu);
3d567982
TT
22912 case DW_TAG_enumeration_type:
22913 parent_type = read_type_die (parent, cu);
22914 if (TYPE_DECLARED_CLASS (parent_type))
22915 {
e86ca25f
TT
22916 if (TYPE_NAME (parent_type) != NULL)
22917 return TYPE_NAME (parent_type);
3d567982
TT
22918 return "";
22919 }
22920 /* Fall through. */
63d06c5c 22921 default:
8176b9b8 22922 return determine_prefix (parent, cu);
63d06c5c 22923 }
63d06c5c
DC
22924}
22925
3e43a32a
MS
22926/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22927 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22928 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22929 an obconcat, otherwise allocate storage for the result. The CU argument is
22930 used to determine the language and hence, the appropriate separator. */
987504bb 22931
f55ee35c 22932#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22933
22934static char *
f55ee35c
JK
22935typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22936 int physname, struct dwarf2_cu *cu)
63d06c5c 22937{
f55ee35c 22938 const char *lead = "";
5c315b68 22939 const char *sep;
63d06c5c 22940
3e43a32a
MS
22941 if (suffix == NULL || suffix[0] == '\0'
22942 || prefix == NULL || prefix[0] == '\0')
987504bb 22943 sep = "";
45280282
IB
22944 else if (cu->language == language_d)
22945 {
22946 /* For D, the 'main' function could be defined in any module, but it
22947 should never be prefixed. */
22948 if (strcmp (suffix, "D main") == 0)
22949 {
22950 prefix = "";
22951 sep = "";
22952 }
22953 else
22954 sep = ".";
22955 }
f55ee35c
JK
22956 else if (cu->language == language_fortran && physname)
22957 {
22958 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22959 DW_AT_MIPS_linkage_name is preferred and used instead. */
22960
22961 lead = "__";
22962 sep = "_MOD_";
22963 }
987504bb
JJ
22964 else
22965 sep = "::";
63d06c5c 22966
6dd47d34
DE
22967 if (prefix == NULL)
22968 prefix = "";
22969 if (suffix == NULL)
22970 suffix = "";
22971
987504bb
JJ
22972 if (obs == NULL)
22973 {
3e43a32a 22974 char *retval
224c3ddb
SM
22975 = ((char *)
22976 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22977
f55ee35c
JK
22978 strcpy (retval, lead);
22979 strcat (retval, prefix);
6dd47d34
DE
22980 strcat (retval, sep);
22981 strcat (retval, suffix);
63d06c5c
DC
22982 return retval;
22983 }
987504bb
JJ
22984 else
22985 {
22986 /* We have an obstack. */
f55ee35c 22987 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22988 }
63d06c5c
DC
22989}
22990
c906108c
SS
22991/* Return sibling of die, NULL if no sibling. */
22992
f9aca02d 22993static struct die_info *
fba45db2 22994sibling_die (struct die_info *die)
c906108c 22995{
639d11d3 22996 return die->sibling;
c906108c
SS
22997}
22998
71c25dea
TT
22999/* Get name of a die, return NULL if not found. */
23000
15d034d0
TT
23001static const char *
23002dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
23003 struct obstack *obstack)
23004{
23005 if (name && cu->language == language_cplus)
23006 {
2f408ecb 23007 std::string canon_name = cp_canonicalize_string (name);
71c25dea 23008
2f408ecb 23009 if (!canon_name.empty ())
71c25dea 23010 {
2f408ecb 23011 if (canon_name != name)
efba19b0 23012 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
23013 }
23014 }
23015
23016 return name;
c906108c
SS
23017}
23018
96553a0c
DE
23019/* Get name of a die, return NULL if not found.
23020 Anonymous namespaces are converted to their magic string. */
9219021c 23021
15d034d0 23022static const char *
e142c38c 23023dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23024{
23025 struct attribute *attr;
518817b3 23026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 23027
e142c38c 23028 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 23029 if ((!attr || !DW_STRING (attr))
96553a0c 23030 && die->tag != DW_TAG_namespace
53832f31
TT
23031 && die->tag != DW_TAG_class_type
23032 && die->tag != DW_TAG_interface_type
23033 && die->tag != DW_TAG_structure_type
23034 && die->tag != DW_TAG_union_type)
71c25dea
TT
23035 return NULL;
23036
23037 switch (die->tag)
23038 {
23039 case DW_TAG_compile_unit:
95554aad 23040 case DW_TAG_partial_unit:
71c25dea
TT
23041 /* Compilation units have a DW_AT_name that is a filename, not
23042 a source language identifier. */
23043 case DW_TAG_enumeration_type:
23044 case DW_TAG_enumerator:
23045 /* These tags always have simple identifiers already; no need
23046 to canonicalize them. */
23047 return DW_STRING (attr);
907af001 23048
96553a0c
DE
23049 case DW_TAG_namespace:
23050 if (attr != NULL && DW_STRING (attr) != NULL)
23051 return DW_STRING (attr);
23052 return CP_ANONYMOUS_NAMESPACE_STR;
23053
907af001
UW
23054 case DW_TAG_class_type:
23055 case DW_TAG_interface_type:
23056 case DW_TAG_structure_type:
23057 case DW_TAG_union_type:
23058 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23059 structures or unions. These were of the form "._%d" in GCC 4.1,
23060 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23061 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 23062 if (attr && DW_STRING (attr)
61012eef
GB
23063 && (startswith (DW_STRING (attr), "._")
23064 || startswith (DW_STRING (attr), "<anonymous")))
907af001 23065 return NULL;
53832f31
TT
23066
23067 /* GCC might emit a nameless typedef that has a linkage name. See
23068 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23069 if (!attr || DW_STRING (attr) == NULL)
23070 {
73b9be8b 23071 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
23072 if (attr == NULL || DW_STRING (attr) == NULL)
23073 return NULL;
23074
df5c6c50
JK
23075 /* Avoid demangling DW_STRING (attr) the second time on a second
23076 call for the same DIE. */
23077 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 23078 {
43816ebc
TT
23079 gdb::unique_xmalloc_ptr<char> demangled
23080 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23081
e6a959d6 23082 const char *base;
96408a79 23083
53832f31 23084 /* FIXME: we already did this for the partial symbol... */
34a68019 23085 DW_STRING (attr)
021887d8 23086 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 23087 demangled.get ());
53832f31 23088 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
23089
23090 /* Strip any leading namespaces/classes, keep only the base name.
23091 DW_AT_name for named DIEs does not contain the prefixes. */
23092 base = strrchr (DW_STRING (attr), ':');
23093 if (base && base > DW_STRING (attr) && base[-1] == ':')
23094 return &base[1];
23095 else
23096 return DW_STRING (attr);
53832f31
TT
23097 }
23098 }
907af001
UW
23099 break;
23100
71c25dea 23101 default:
907af001
UW
23102 break;
23103 }
23104
23105 if (!DW_STRING_IS_CANONICAL (attr))
23106 {
23107 DW_STRING (attr)
23108 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 23109 &objfile->per_bfd->storage_obstack);
907af001 23110 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 23111 }
907af001 23112 return DW_STRING (attr);
9219021c
DC
23113}
23114
23115/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23116 is none. *EXT_CU is the CU containing DIE on input, and the CU
23117 containing the return value on output. */
9219021c
DC
23118
23119static struct die_info *
f2f0e013 23120dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23121{
23122 struct attribute *attr;
9219021c 23123
f2f0e013 23124 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23125 if (attr == NULL)
23126 return NULL;
23127
f2f0e013 23128 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23129}
23130
fa9c3fa0
TT
23131/* A convenience function that returns an "unknown" DWARF name,
23132 including the value of V. STR is the name of the entity being
23133 printed, e.g., "TAG". */
23134
23135static const char *
23136dwarf_unknown (const char *str, unsigned v)
23137{
23138 char *cell = get_print_cell ();
23139 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23140 return cell;
23141}
23142
c906108c
SS
23143/* Convert a DIE tag into its string name. */
23144
f39c6ffd 23145static const char *
aa1ee363 23146dwarf_tag_name (unsigned tag)
c906108c 23147{
f39c6ffd
TT
23148 const char *name = get_DW_TAG_name (tag);
23149
23150 if (name == NULL)
fa9c3fa0 23151 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
23152
23153 return name;
c906108c
SS
23154}
23155
23156/* Convert a DWARF attribute code into its string name. */
23157
f39c6ffd 23158static const char *
aa1ee363 23159dwarf_attr_name (unsigned attr)
c906108c 23160{
f39c6ffd
TT
23161 const char *name;
23162
c764a876 23163#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
23164 if (attr == DW_AT_MIPS_fde)
23165 return "DW_AT_MIPS_fde";
23166#else
23167 if (attr == DW_AT_HP_block_index)
23168 return "DW_AT_HP_block_index";
c764a876 23169#endif
f39c6ffd
TT
23170
23171 name = get_DW_AT_name (attr);
23172
23173 if (name == NULL)
fa9c3fa0 23174 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
23175
23176 return name;
c906108c
SS
23177}
23178
a084a2a6
AT
23179/* Convert a unit type to corresponding DW_UT name. */
23180
23181static const char *
23182dwarf_unit_type_name (int unit_type) {
23183 switch (unit_type)
23184 {
23185 case 0x01:
23186 return "DW_UT_compile (0x01)";
23187 case 0x02:
23188 return "DW_UT_type (0x02)";
23189 case 0x03:
23190 return "DW_UT_partial (0x03)";
23191 case 0x04:
23192 return "DW_UT_skeleton (0x04)";
23193 case 0x05:
23194 return "DW_UT_split_compile (0x05)";
23195 case 0x06:
23196 return "DW_UT_split_type (0x06)";
23197 case 0x80:
23198 return "DW_UT_lo_user (0x80)";
23199 case 0xff:
23200 return "DW_UT_hi_user (0xff)";
23201 default:
23202 return nullptr;
23203 }
23204}
23205
c906108c
SS
23206/* Convert a DWARF value form code into its string name. */
23207
f39c6ffd 23208static const char *
aa1ee363 23209dwarf_form_name (unsigned form)
c906108c 23210{
f39c6ffd
TT
23211 const char *name = get_DW_FORM_name (form);
23212
23213 if (name == NULL)
fa9c3fa0 23214 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
23215
23216 return name;
c906108c
SS
23217}
23218
a121b7c1 23219static const char *
fba45db2 23220dwarf_bool_name (unsigned mybool)
c906108c
SS
23221{
23222 if (mybool)
23223 return "TRUE";
23224 else
23225 return "FALSE";
23226}
23227
23228/* Convert a DWARF type code into its string name. */
23229
f39c6ffd 23230static const char *
aa1ee363 23231dwarf_type_encoding_name (unsigned enc)
c906108c 23232{
f39c6ffd 23233 const char *name = get_DW_ATE_name (enc);
c906108c 23234
f39c6ffd 23235 if (name == NULL)
fa9c3fa0 23236 return dwarf_unknown ("ATE", enc);
c906108c 23237
f39c6ffd 23238 return name;
c906108c 23239}
c906108c 23240
f9aca02d 23241static void
d97bc12b 23242dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23243{
23244 unsigned int i;
23245
d97bc12b 23246 print_spaces (indent, f);
9d8780f0 23247 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23248 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23249 sect_offset_str (die->sect_off));
d97bc12b
DE
23250
23251 if (die->parent != NULL)
23252 {
23253 print_spaces (indent, f);
9d8780f0
SM
23254 fprintf_unfiltered (f, " parent at offset: %s\n",
23255 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23256 }
23257
23258 print_spaces (indent, f);
23259 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23260 dwarf_bool_name (die->child != NULL));
c906108c 23261
d97bc12b
DE
23262 print_spaces (indent, f);
23263 fprintf_unfiltered (f, " attributes:\n");
23264
c906108c
SS
23265 for (i = 0; i < die->num_attrs; ++i)
23266 {
d97bc12b
DE
23267 print_spaces (indent, f);
23268 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23269 dwarf_attr_name (die->attrs[i].name),
23270 dwarf_form_name (die->attrs[i].form));
d97bc12b 23271
c906108c
SS
23272 switch (die->attrs[i].form)
23273 {
c906108c 23274 case DW_FORM_addr:
336d760d 23275 case DW_FORM_addrx:
3019eac3 23276 case DW_FORM_GNU_addr_index:
d97bc12b 23277 fprintf_unfiltered (f, "address: ");
5af949e3 23278 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23279 break;
23280 case DW_FORM_block2:
23281 case DW_FORM_block4:
23282 case DW_FORM_block:
23283 case DW_FORM_block1:
56eb65bd
SP
23284 fprintf_unfiltered (f, "block: size %s",
23285 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23286 break;
2dc7f7b3 23287 case DW_FORM_exprloc:
56eb65bd
SP
23288 fprintf_unfiltered (f, "expression: size %s",
23289 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23290 break;
0224619f
JK
23291 case DW_FORM_data16:
23292 fprintf_unfiltered (f, "constant of 16 bytes");
23293 break;
4568ecf9
DE
23294 case DW_FORM_ref_addr:
23295 fprintf_unfiltered (f, "ref address: ");
23296 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23297 break;
36586728
TT
23298 case DW_FORM_GNU_ref_alt:
23299 fprintf_unfiltered (f, "alt ref address: ");
23300 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23301 break;
10b3939b
DJ
23302 case DW_FORM_ref1:
23303 case DW_FORM_ref2:
23304 case DW_FORM_ref4:
4568ecf9
DE
23305 case DW_FORM_ref8:
23306 case DW_FORM_ref_udata:
d97bc12b 23307 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23308 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23309 break;
c906108c
SS
23310 case DW_FORM_data1:
23311 case DW_FORM_data2:
23312 case DW_FORM_data4:
ce5d95e1 23313 case DW_FORM_data8:
c906108c
SS
23314 case DW_FORM_udata:
23315 case DW_FORM_sdata:
43bbcdc2
PH
23316 fprintf_unfiltered (f, "constant: %s",
23317 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23318 break;
2dc7f7b3
TT
23319 case DW_FORM_sec_offset:
23320 fprintf_unfiltered (f, "section offset: %s",
23321 pulongest (DW_UNSND (&die->attrs[i])));
23322 break;
55f1336d 23323 case DW_FORM_ref_sig8:
ac9ec31b
DE
23324 fprintf_unfiltered (f, "signature: %s",
23325 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23326 break;
c906108c 23327 case DW_FORM_string:
4bdf3d34 23328 case DW_FORM_strp:
43988095 23329 case DW_FORM_line_strp:
cf532bd1 23330 case DW_FORM_strx:
3019eac3 23331 case DW_FORM_GNU_str_index:
36586728 23332 case DW_FORM_GNU_strp_alt:
8285870a 23333 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23334 DW_STRING (&die->attrs[i])
8285870a
JK
23335 ? DW_STRING (&die->attrs[i]) : "",
23336 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23337 break;
23338 case DW_FORM_flag:
23339 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23340 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23341 else
d97bc12b 23342 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23343 break;
2dc7f7b3
TT
23344 case DW_FORM_flag_present:
23345 fprintf_unfiltered (f, "flag: TRUE");
23346 break;
a8329558 23347 case DW_FORM_indirect:
0963b4bd
MS
23348 /* The reader will have reduced the indirect form to
23349 the "base form" so this form should not occur. */
5f48f8f3 23350 fprintf_unfiltered (f,
3e43a32a 23351 "unexpected attribute form: DW_FORM_indirect");
a8329558 23352 break;
663c44ac
JK
23353 case DW_FORM_implicit_const:
23354 fprintf_unfiltered (f, "constant: %s",
23355 plongest (DW_SND (&die->attrs[i])));
23356 break;
c906108c 23357 default:
d97bc12b 23358 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23359 die->attrs[i].form);
d97bc12b 23360 break;
c906108c 23361 }
d97bc12b 23362 fprintf_unfiltered (f, "\n");
c906108c
SS
23363 }
23364}
23365
f9aca02d 23366static void
d97bc12b 23367dump_die_for_error (struct die_info *die)
c906108c 23368{
d97bc12b
DE
23369 dump_die_shallow (gdb_stderr, 0, die);
23370}
23371
23372static void
23373dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23374{
23375 int indent = level * 4;
23376
23377 gdb_assert (die != NULL);
23378
23379 if (level >= max_level)
23380 return;
23381
23382 dump_die_shallow (f, indent, die);
23383
23384 if (die->child != NULL)
c906108c 23385 {
d97bc12b
DE
23386 print_spaces (indent, f);
23387 fprintf_unfiltered (f, " Children:");
23388 if (level + 1 < max_level)
23389 {
23390 fprintf_unfiltered (f, "\n");
23391 dump_die_1 (f, level + 1, max_level, die->child);
23392 }
23393 else
23394 {
3e43a32a
MS
23395 fprintf_unfiltered (f,
23396 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23397 }
23398 }
23399
23400 if (die->sibling != NULL && level > 0)
23401 {
23402 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23403 }
23404}
23405
d97bc12b
DE
23406/* This is called from the pdie macro in gdbinit.in.
23407 It's not static so gcc will keep a copy callable from gdb. */
23408
23409void
23410dump_die (struct die_info *die, int max_level)
23411{
23412 dump_die_1 (gdb_stdlog, 0, max_level, die);
23413}
23414
f9aca02d 23415static void
51545339 23416store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23417{
51545339 23418 void **slot;
c906108c 23419
9c541725
PA
23420 slot = htab_find_slot_with_hash (cu->die_hash, die,
23421 to_underlying (die->sect_off),
b64f50a1 23422 INSERT);
51545339
DJ
23423
23424 *slot = die;
c906108c
SS
23425}
23426
b64f50a1
JK
23427/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23428 required kind. */
23429
23430static sect_offset
ff39bb5e 23431dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23432{
7771576e 23433 if (attr_form_is_ref (attr))
9c541725 23434 return (sect_offset) DW_UNSND (attr);
93311388 23435
b98664d3 23436 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23437 dwarf_form_name (attr->form));
9c541725 23438 return {};
c906108c
SS
23439}
23440
43bbcdc2
PH
23441/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23442 * the value held by the attribute is not constant. */
a02abb62 23443
43bbcdc2 23444static LONGEST
ff39bb5e 23445dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23446{
663c44ac 23447 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23448 return DW_SND (attr);
23449 else if (attr->form == DW_FORM_udata
23450 || attr->form == DW_FORM_data1
23451 || attr->form == DW_FORM_data2
23452 || attr->form == DW_FORM_data4
23453 || attr->form == DW_FORM_data8)
23454 return DW_UNSND (attr);
23455 else
23456 {
0224619f 23457 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23458 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23459 dwarf_form_name (attr->form));
23460 return default_value;
23461 }
23462}
23463
348e048f
DE
23464/* Follow reference or signature attribute ATTR of SRC_DIE.
23465 On entry *REF_CU is the CU of SRC_DIE.
23466 On exit *REF_CU is the CU of the result. */
23467
23468static struct die_info *
ff39bb5e 23469follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23470 struct dwarf2_cu **ref_cu)
23471{
23472 struct die_info *die;
23473
7771576e 23474 if (attr_form_is_ref (attr))
348e048f 23475 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23476 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23477 die = follow_die_sig (src_die, attr, ref_cu);
23478 else
23479 {
23480 dump_die_for_error (src_die);
23481 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23482 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23483 }
23484
23485 return die;
03dd20cc
DJ
23486}
23487
5c631832 23488/* Follow reference OFFSET.
673bfd45
DE
23489 On entry *REF_CU is the CU of the source die referencing OFFSET.
23490 On exit *REF_CU is the CU of the result.
23491 Returns NULL if OFFSET is invalid. */
f504f079 23492
f9aca02d 23493static struct die_info *
9c541725 23494follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23495 struct dwarf2_cu **ref_cu)
c906108c 23496{
10b3939b 23497 struct die_info temp_die;
f2f0e013 23498 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23499 struct dwarf2_per_objfile *dwarf2_per_objfile
23500 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23501
348e048f
DE
23502 gdb_assert (cu->per_cu != NULL);
23503
98bfdba5
PA
23504 target_cu = cu;
23505
3019eac3 23506 if (cu->per_cu->is_debug_types)
348e048f
DE
23507 {
23508 /* .debug_types CUs cannot reference anything outside their CU.
23509 If they need to, they have to reference a signatured type via
55f1336d 23510 DW_FORM_ref_sig8. */
9c541725 23511 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23512 return NULL;
348e048f 23513 }
36586728 23514 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23515 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23516 {
23517 struct dwarf2_per_cu_data *per_cu;
9a619af0 23518
9c541725 23519 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23520 dwarf2_per_objfile);
03dd20cc
DJ
23521
23522 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23523 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23524 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23525
10b3939b
DJ
23526 target_cu = per_cu->cu;
23527 }
98bfdba5
PA
23528 else if (cu->dies == NULL)
23529 {
23530 /* We're loading full DIEs during partial symbol reading. */
23531 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23532 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23533 }
c906108c 23534
f2f0e013 23535 *ref_cu = target_cu;
9c541725 23536 temp_die.sect_off = sect_off;
c24bdb02
KS
23537
23538 if (target_cu != cu)
23539 target_cu->ancestor = cu;
23540
9a3c8263 23541 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23542 &temp_die,
23543 to_underlying (sect_off));
5c631832 23544}
10b3939b 23545
5c631832
JK
23546/* Follow reference attribute ATTR of SRC_DIE.
23547 On entry *REF_CU is the CU of SRC_DIE.
23548 On exit *REF_CU is the CU of the result. */
23549
23550static struct die_info *
ff39bb5e 23551follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23552 struct dwarf2_cu **ref_cu)
23553{
9c541725 23554 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23555 struct dwarf2_cu *cu = *ref_cu;
23556 struct die_info *die;
23557
9c541725 23558 die = follow_die_offset (sect_off,
36586728
TT
23559 (attr->form == DW_FORM_GNU_ref_alt
23560 || cu->per_cu->is_dwz),
23561 ref_cu);
5c631832 23562 if (!die)
9d8780f0
SM
23563 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23564 "at %s [in module %s]"),
23565 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23566 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23567
5c631832
JK
23568 return die;
23569}
23570
9c541725 23571/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23572 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23573 dwarf2_locexpr_baton->data has lifetime of
23574 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23575
23576struct dwarf2_locexpr_baton
9c541725 23577dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23578 struct dwarf2_per_cu_data *per_cu,
23579 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23580 void *baton, bool resolve_abstract_p)
5c631832 23581{
918dd910 23582 struct dwarf2_cu *cu;
5c631832
JK
23583 struct die_info *die;
23584 struct attribute *attr;
23585 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23586 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23587 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23588
918dd910 23589 if (per_cu->cu == NULL)
58f0c718 23590 load_cu (per_cu, false);
918dd910 23591 cu = per_cu->cu;
cc12ce38
DE
23592 if (cu == NULL)
23593 {
23594 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23595 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23596 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23597 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23598 }
918dd910 23599
9c541725 23600 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23601 if (!die)
9d8780f0
SM
23602 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23603 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23604
23605 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23606 if (!attr && resolve_abstract_p
3360b6e7 23607 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23608 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23609 {
23610 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 23611 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 23612 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23613
3360b6e7
TV
23614 for (const auto &cand_off
23615 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23616 {
3360b6e7
TV
23617 struct dwarf2_cu *cand_cu = cu;
23618 struct die_info *cand
23619 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23620 if (!cand
23621 || !cand->parent
e4a62c65
TV
23622 || cand->parent->tag != DW_TAG_subprogram)
23623 continue;
23624
23625 CORE_ADDR pc_low, pc_high;
23626 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23627 if (pc_low == ((CORE_ADDR) -1))
23628 continue;
23629 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23630 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23631 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23632 continue;
23633
23634 die = cand;
23635 attr = dwarf2_attr (die, DW_AT_location, cu);
23636 break;
23637 }
23638 }
23639
5c631832
JK
23640 if (!attr)
23641 {
e103e986
JK
23642 /* DWARF: "If there is no such attribute, then there is no effect.".
23643 DATA is ignored if SIZE is 0. */
5c631832 23644
e103e986 23645 retval.data = NULL;
5c631832
JK
23646 retval.size = 0;
23647 }
8cf6f0b1
TT
23648 else if (attr_form_is_section_offset (attr))
23649 {
23650 struct dwarf2_loclist_baton loclist_baton;
23651 CORE_ADDR pc = (*get_frame_pc) (baton);
23652 size_t size;
23653
23654 fill_in_loclist_baton (cu, &loclist_baton, attr);
23655
23656 retval.data = dwarf2_find_location_expression (&loclist_baton,
23657 &size, pc);
23658 retval.size = size;
23659 }
5c631832
JK
23660 else
23661 {
23662 if (!attr_form_is_block (attr))
9d8780f0 23663 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23664 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23665 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23666
23667 retval.data = DW_BLOCK (attr)->data;
23668 retval.size = DW_BLOCK (attr)->size;
23669 }
23670 retval.per_cu = cu->per_cu;
918dd910 23671
ed2dc618 23672 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23673
5c631832 23674 return retval;
348e048f
DE
23675}
23676
8b9737bf
TT
23677/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23678 offset. */
23679
23680struct dwarf2_locexpr_baton
23681dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23682 struct dwarf2_per_cu_data *per_cu,
23683 CORE_ADDR (*get_frame_pc) (void *baton),
23684 void *baton)
23685{
9c541725 23686 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23687
9c541725 23688 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23689}
23690
b6807d98
TT
23691/* Write a constant of a given type as target-ordered bytes into
23692 OBSTACK. */
23693
23694static const gdb_byte *
23695write_constant_as_bytes (struct obstack *obstack,
23696 enum bfd_endian byte_order,
23697 struct type *type,
23698 ULONGEST value,
23699 LONGEST *len)
23700{
23701 gdb_byte *result;
23702
23703 *len = TYPE_LENGTH (type);
224c3ddb 23704 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23705 store_unsigned_integer (result, *len, byte_order, value);
23706
23707 return result;
23708}
23709
23710/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23711 pointer to the constant bytes and set LEN to the length of the
23712 data. If memory is needed, allocate it on OBSTACK. If the DIE
23713 does not have a DW_AT_const_value, return NULL. */
23714
23715const gdb_byte *
9c541725 23716dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23717 struct dwarf2_per_cu_data *per_cu,
23718 struct obstack *obstack,
23719 LONGEST *len)
23720{
23721 struct dwarf2_cu *cu;
23722 struct die_info *die;
23723 struct attribute *attr;
23724 const gdb_byte *result = NULL;
23725 struct type *type;
23726 LONGEST value;
23727 enum bfd_endian byte_order;
e3b94546 23728 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23729
b6807d98 23730 if (per_cu->cu == NULL)
58f0c718 23731 load_cu (per_cu, false);
b6807d98 23732 cu = per_cu->cu;
cc12ce38
DE
23733 if (cu == NULL)
23734 {
23735 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23736 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23737 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23738 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23739 }
b6807d98 23740
9c541725 23741 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23742 if (!die)
9d8780f0
SM
23743 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23744 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23745
23746 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23747 if (attr == NULL)
23748 return NULL;
23749
e3b94546 23750 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23751 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23752
23753 switch (attr->form)
23754 {
23755 case DW_FORM_addr:
336d760d 23756 case DW_FORM_addrx:
b6807d98
TT
23757 case DW_FORM_GNU_addr_index:
23758 {
23759 gdb_byte *tem;
23760
23761 *len = cu->header.addr_size;
224c3ddb 23762 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23763 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23764 result = tem;
23765 }
23766 break;
23767 case DW_FORM_string:
23768 case DW_FORM_strp:
cf532bd1 23769 case DW_FORM_strx:
b6807d98
TT
23770 case DW_FORM_GNU_str_index:
23771 case DW_FORM_GNU_strp_alt:
23772 /* DW_STRING is already allocated on the objfile obstack, point
23773 directly to it. */
23774 result = (const gdb_byte *) DW_STRING (attr);
23775 *len = strlen (DW_STRING (attr));
23776 break;
23777 case DW_FORM_block1:
23778 case DW_FORM_block2:
23779 case DW_FORM_block4:
23780 case DW_FORM_block:
23781 case DW_FORM_exprloc:
0224619f 23782 case DW_FORM_data16:
b6807d98
TT
23783 result = DW_BLOCK (attr)->data;
23784 *len = DW_BLOCK (attr)->size;
23785 break;
23786
23787 /* The DW_AT_const_value attributes are supposed to carry the
23788 symbol's value "represented as it would be on the target
23789 architecture." By the time we get here, it's already been
23790 converted to host endianness, so we just need to sign- or
23791 zero-extend it as appropriate. */
23792 case DW_FORM_data1:
23793 type = die_type (die, cu);
23794 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23795 if (result == NULL)
23796 result = write_constant_as_bytes (obstack, byte_order,
23797 type, value, len);
23798 break;
23799 case DW_FORM_data2:
23800 type = die_type (die, cu);
23801 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23802 if (result == NULL)
23803 result = write_constant_as_bytes (obstack, byte_order,
23804 type, value, len);
23805 break;
23806 case DW_FORM_data4:
23807 type = die_type (die, cu);
23808 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23809 if (result == NULL)
23810 result = write_constant_as_bytes (obstack, byte_order,
23811 type, value, len);
23812 break;
23813 case DW_FORM_data8:
23814 type = die_type (die, cu);
23815 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23816 if (result == NULL)
23817 result = write_constant_as_bytes (obstack, byte_order,
23818 type, value, len);
23819 break;
23820
23821 case DW_FORM_sdata:
663c44ac 23822 case DW_FORM_implicit_const:
b6807d98
TT
23823 type = die_type (die, cu);
23824 result = write_constant_as_bytes (obstack, byte_order,
23825 type, DW_SND (attr), len);
23826 break;
23827
23828 case DW_FORM_udata:
23829 type = die_type (die, cu);
23830 result = write_constant_as_bytes (obstack, byte_order,
23831 type, DW_UNSND (attr), len);
23832 break;
23833
23834 default:
b98664d3 23835 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23836 dwarf_form_name (attr->form));
23837 break;
23838 }
23839
23840 return result;
23841}
23842
7942e96e
AA
23843/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23844 valid type for this die is found. */
23845
23846struct type *
9c541725 23847dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23848 struct dwarf2_per_cu_data *per_cu)
23849{
23850 struct dwarf2_cu *cu;
23851 struct die_info *die;
23852
7942e96e 23853 if (per_cu->cu == NULL)
58f0c718 23854 load_cu (per_cu, false);
7942e96e
AA
23855 cu = per_cu->cu;
23856 if (!cu)
23857 return NULL;
23858
9c541725 23859 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23860 if (!die)
23861 return NULL;
23862
23863 return die_type (die, cu);
23864}
23865
8a9b8146
TT
23866/* Return the type of the DIE at DIE_OFFSET in the CU named by
23867 PER_CU. */
23868
23869struct type *
b64f50a1 23870dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23871 struct dwarf2_per_cu_data *per_cu)
23872{
9c541725 23873 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23874 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23875}
23876
ac9ec31b 23877/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23878 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23879 On exit *REF_CU is the CU of the result.
23880 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23881
23882static struct die_info *
ac9ec31b
DE
23883follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23884 struct dwarf2_cu **ref_cu)
348e048f 23885{
348e048f 23886 struct die_info temp_die;
c24bdb02 23887 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23888 struct die_info *die;
23889
ac9ec31b
DE
23890 /* While it might be nice to assert sig_type->type == NULL here,
23891 we can get here for DW_AT_imported_declaration where we need
23892 the DIE not the type. */
348e048f
DE
23893
23894 /* If necessary, add it to the queue and load its DIEs. */
23895
95554aad 23896 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23897 read_signatured_type (sig_type);
348e048f 23898
348e048f 23899 sig_cu = sig_type->per_cu.cu;
69d751e3 23900 gdb_assert (sig_cu != NULL);
9c541725
PA
23901 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23902 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23903 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23904 to_underlying (temp_die.sect_off));
348e048f
DE
23905 if (die)
23906 {
ed2dc618 23907 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23908 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23909
796a7ff8
DE
23910 /* For .gdb_index version 7 keep track of included TUs.
23911 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23912 if (dwarf2_per_objfile->index_table != NULL
23913 && dwarf2_per_objfile->index_table->version <= 7)
23914 {
ae640021 23915 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23916 }
23917
348e048f 23918 *ref_cu = sig_cu;
c24bdb02
KS
23919 if (sig_cu != cu)
23920 sig_cu->ancestor = cu;
23921
348e048f
DE
23922 return die;
23923 }
23924
ac9ec31b
DE
23925 return NULL;
23926}
23927
23928/* Follow signatured type referenced by ATTR in SRC_DIE.
23929 On entry *REF_CU is the CU of SRC_DIE.
23930 On exit *REF_CU is the CU of the result.
23931 The result is the DIE of the type.
23932 If the referenced type cannot be found an error is thrown. */
23933
23934static struct die_info *
ff39bb5e 23935follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23936 struct dwarf2_cu **ref_cu)
23937{
23938 ULONGEST signature = DW_SIGNATURE (attr);
23939 struct signatured_type *sig_type;
23940 struct die_info *die;
23941
23942 gdb_assert (attr->form == DW_FORM_ref_sig8);
23943
a2ce51a0 23944 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23945 /* sig_type will be NULL if the signatured type is missing from
23946 the debug info. */
23947 if (sig_type == NULL)
23948 {
23949 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23950 " from DIE at %s [in module %s]"),
23951 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23952 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23953 }
23954
23955 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23956 if (die == NULL)
23957 {
23958 dump_die_for_error (src_die);
23959 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23960 " from DIE at %s [in module %s]"),
23961 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23962 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23963 }
23964
23965 return die;
23966}
23967
23968/* Get the type specified by SIGNATURE referenced in DIE/CU,
23969 reading in and processing the type unit if necessary. */
23970
23971static struct type *
23972get_signatured_type (struct die_info *die, ULONGEST signature,
23973 struct dwarf2_cu *cu)
23974{
518817b3
SM
23975 struct dwarf2_per_objfile *dwarf2_per_objfile
23976 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23977 struct signatured_type *sig_type;
23978 struct dwarf2_cu *type_cu;
23979 struct die_info *type_die;
23980 struct type *type;
23981
a2ce51a0 23982 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23983 /* sig_type will be NULL if the signatured type is missing from
23984 the debug info. */
23985 if (sig_type == NULL)
23986 {
b98664d3 23987 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23988 " from DIE at %s [in module %s]"),
23989 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23990 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23991 return build_error_marker_type (cu, die);
23992 }
23993
23994 /* If we already know the type we're done. */
23995 if (sig_type->type != NULL)
23996 return sig_type->type;
23997
23998 type_cu = cu;
23999 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24000 if (type_die != NULL)
24001 {
24002 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24003 is created. This is important, for example, because for c++ classes
24004 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24005 type = read_type_die (type_die, type_cu);
24006 if (type == NULL)
24007 {
b98664d3 24008 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24009 " referenced from DIE at %s [in module %s]"),
24010 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24011 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24012 type = build_error_marker_type (cu, die);
24013 }
24014 }
24015 else
24016 {
b98664d3 24017 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24018 " from DIE at %s [in module %s]"),
24019 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24020 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24021 type = build_error_marker_type (cu, die);
24022 }
24023 sig_type->type = type;
24024
24025 return type;
24026}
24027
24028/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24029 reading in and processing the type unit if necessary. */
24030
24031static struct type *
ff39bb5e 24032get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24033 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24034{
24035 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 24036 if (attr_form_is_ref (attr))
ac9ec31b
DE
24037 {
24038 struct dwarf2_cu *type_cu = cu;
24039 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24040
24041 return read_type_die (type_die, type_cu);
24042 }
24043 else if (attr->form == DW_FORM_ref_sig8)
24044 {
24045 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24046 }
24047 else
24048 {
518817b3
SM
24049 struct dwarf2_per_objfile *dwarf2_per_objfile
24050 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24051
b98664d3 24052 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24053 " at %s [in module %s]"),
24054 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 24055 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24056 return build_error_marker_type (cu, die);
24057 }
348e048f
DE
24058}
24059
e5fe5e75 24060/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24061
24062static void
e5fe5e75 24063load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 24064{
52dc124a 24065 struct signatured_type *sig_type;
348e048f 24066
f4dc4d17
DE
24067 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24068 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24069
6721b2ec
DE
24070 /* We have the per_cu, but we need the signatured_type.
24071 Fortunately this is an easy translation. */
24072 gdb_assert (per_cu->is_debug_types);
24073 sig_type = (struct signatured_type *) per_cu;
348e048f 24074
6721b2ec 24075 gdb_assert (per_cu->cu == NULL);
348e048f 24076
52dc124a 24077 read_signatured_type (sig_type);
348e048f 24078
6721b2ec 24079 gdb_assert (per_cu->cu != NULL);
348e048f
DE
24080}
24081
3019eac3
DE
24082/* Read in a signatured type and build its CU and DIEs.
24083 If the type is a stub for the real type in a DWO file,
24084 read in the real type from the DWO file as well. */
dee91e82
DE
24085
24086static void
24087read_signatured_type (struct signatured_type *sig_type)
24088{
24089 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24090
3019eac3 24091 gdb_assert (per_cu->is_debug_types);
dee91e82 24092 gdb_assert (per_cu->cu == NULL);
348e048f 24093
c0ab21c2
TT
24094 cutu_reader reader (per_cu, NULL, 0, 1, false);
24095
24096 if (!reader.dummy_p)
24097 {
24098 struct dwarf2_cu *cu = reader.cu;
24099 const gdb_byte *info_ptr = reader.info_ptr;
24100
24101 gdb_assert (cu->die_hash == NULL);
24102 cu->die_hash =
24103 htab_create_alloc_ex (cu->header.length / 12,
24104 die_hash,
24105 die_eq,
24106 NULL,
24107 &cu->comp_unit_obstack,
24108 hashtab_obstack_allocate,
24109 dummy_obstack_deallocate);
24110
24111 if (reader.has_children)
24112 reader.comp_unit_die->child
24113 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24114 reader.comp_unit_die);
24115 cu->dies = reader.comp_unit_die;
24116 /* comp_unit_die is not stored in die_hash, no need. */
24117
24118 /* We try not to read any attributes in this function, because
24119 not all CUs needed for references have been loaded yet, and
24120 symbol table processing isn't initialized. But we have to
24121 set the CU language, or we won't be able to build types
24122 correctly. Similarly, if we do not read the producer, we can
24123 not apply producer-specific interpretation. */
24124 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24125 }
24126
7ee85ab1 24127 sig_type->per_cu.tu_read = 1;
c906108c
SS
24128}
24129
c906108c
SS
24130/* Decode simple location descriptions.
24131 Given a pointer to a dwarf block that defines a location, compute
24132 the location and return the value.
24133
4cecd739
DJ
24134 NOTE drow/2003-11-18: This function is called in two situations
24135 now: for the address of static or global variables (partial symbols
24136 only) and for offsets into structures which are expected to be
24137 (more or less) constant. The partial symbol case should go away,
24138 and only the constant case should remain. That will let this
24139 function complain more accurately. A few special modes are allowed
24140 without complaint for global variables (for instance, global
24141 register values and thread-local values).
c906108c
SS
24142
24143 A location description containing no operations indicates that the
4cecd739 24144 object is optimized out. The return value is 0 for that case.
6b992462
DJ
24145 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24146 callers will only want a very basic result and this can become a
21ae7a4d
JK
24147 complaint.
24148
24149 Note that stack[0] is unused except as a default error return. */
c906108c
SS
24150
24151static CORE_ADDR
e7c27a73 24152decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 24153{
518817b3 24154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
24155 size_t i;
24156 size_t size = blk->size;
d521ce57 24157 const gdb_byte *data = blk->data;
21ae7a4d
JK
24158 CORE_ADDR stack[64];
24159 int stacki;
24160 unsigned int bytes_read, unsnd;
24161 gdb_byte op;
c906108c 24162
21ae7a4d
JK
24163 i = 0;
24164 stacki = 0;
24165 stack[stacki] = 0;
24166 stack[++stacki] = 0;
24167
24168 while (i < size)
24169 {
24170 op = data[i++];
24171 switch (op)
24172 {
24173 case DW_OP_lit0:
24174 case DW_OP_lit1:
24175 case DW_OP_lit2:
24176 case DW_OP_lit3:
24177 case DW_OP_lit4:
24178 case DW_OP_lit5:
24179 case DW_OP_lit6:
24180 case DW_OP_lit7:
24181 case DW_OP_lit8:
24182 case DW_OP_lit9:
24183 case DW_OP_lit10:
24184 case DW_OP_lit11:
24185 case DW_OP_lit12:
24186 case DW_OP_lit13:
24187 case DW_OP_lit14:
24188 case DW_OP_lit15:
24189 case DW_OP_lit16:
24190 case DW_OP_lit17:
24191 case DW_OP_lit18:
24192 case DW_OP_lit19:
24193 case DW_OP_lit20:
24194 case DW_OP_lit21:
24195 case DW_OP_lit22:
24196 case DW_OP_lit23:
24197 case DW_OP_lit24:
24198 case DW_OP_lit25:
24199 case DW_OP_lit26:
24200 case DW_OP_lit27:
24201 case DW_OP_lit28:
24202 case DW_OP_lit29:
24203 case DW_OP_lit30:
24204 case DW_OP_lit31:
24205 stack[++stacki] = op - DW_OP_lit0;
24206 break;
f1bea926 24207
21ae7a4d
JK
24208 case DW_OP_reg0:
24209 case DW_OP_reg1:
24210 case DW_OP_reg2:
24211 case DW_OP_reg3:
24212 case DW_OP_reg4:
24213 case DW_OP_reg5:
24214 case DW_OP_reg6:
24215 case DW_OP_reg7:
24216 case DW_OP_reg8:
24217 case DW_OP_reg9:
24218 case DW_OP_reg10:
24219 case DW_OP_reg11:
24220 case DW_OP_reg12:
24221 case DW_OP_reg13:
24222 case DW_OP_reg14:
24223 case DW_OP_reg15:
24224 case DW_OP_reg16:
24225 case DW_OP_reg17:
24226 case DW_OP_reg18:
24227 case DW_OP_reg19:
24228 case DW_OP_reg20:
24229 case DW_OP_reg21:
24230 case DW_OP_reg22:
24231 case DW_OP_reg23:
24232 case DW_OP_reg24:
24233 case DW_OP_reg25:
24234 case DW_OP_reg26:
24235 case DW_OP_reg27:
24236 case DW_OP_reg28:
24237 case DW_OP_reg29:
24238 case DW_OP_reg30:
24239 case DW_OP_reg31:
24240 stack[++stacki] = op - DW_OP_reg0;
24241 if (i < size)
24242 dwarf2_complex_location_expr_complaint ();
24243 break;
c906108c 24244
21ae7a4d
JK
24245 case DW_OP_regx:
24246 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24247 i += bytes_read;
24248 stack[++stacki] = unsnd;
24249 if (i < size)
24250 dwarf2_complex_location_expr_complaint ();
24251 break;
c906108c 24252
21ae7a4d
JK
24253 case DW_OP_addr:
24254 stack[++stacki] = read_address (objfile->obfd, &data[i],
24255 cu, &bytes_read);
24256 i += bytes_read;
24257 break;
d53d4ac5 24258
21ae7a4d
JK
24259 case DW_OP_const1u:
24260 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24261 i += 1;
24262 break;
24263
24264 case DW_OP_const1s:
24265 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24266 i += 1;
24267 break;
24268
24269 case DW_OP_const2u:
24270 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24271 i += 2;
24272 break;
24273
24274 case DW_OP_const2s:
24275 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24276 i += 2;
24277 break;
d53d4ac5 24278
21ae7a4d
JK
24279 case DW_OP_const4u:
24280 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24281 i += 4;
24282 break;
24283
24284 case DW_OP_const4s:
24285 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24286 i += 4;
24287 break;
24288
585861ea
JK
24289 case DW_OP_const8u:
24290 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24291 i += 8;
24292 break;
24293
21ae7a4d
JK
24294 case DW_OP_constu:
24295 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24296 &bytes_read);
24297 i += bytes_read;
24298 break;
24299
24300 case DW_OP_consts:
24301 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24302 i += bytes_read;
24303 break;
24304
24305 case DW_OP_dup:
24306 stack[stacki + 1] = stack[stacki];
24307 stacki++;
24308 break;
24309
24310 case DW_OP_plus:
24311 stack[stacki - 1] += stack[stacki];
24312 stacki--;
24313 break;
24314
24315 case DW_OP_plus_uconst:
24316 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24317 &bytes_read);
24318 i += bytes_read;
24319 break;
24320
24321 case DW_OP_minus:
24322 stack[stacki - 1] -= stack[stacki];
24323 stacki--;
24324 break;
24325
24326 case DW_OP_deref:
24327 /* If we're not the last op, then we definitely can't encode
24328 this using GDB's address_class enum. This is valid for partial
24329 global symbols, although the variable's address will be bogus
24330 in the psymtab. */
24331 if (i < size)
24332 dwarf2_complex_location_expr_complaint ();
24333 break;
24334
24335 case DW_OP_GNU_push_tls_address:
4aa4e28b 24336 case DW_OP_form_tls_address:
21ae7a4d
JK
24337 /* The top of the stack has the offset from the beginning
24338 of the thread control block at which the variable is located. */
24339 /* Nothing should follow this operator, so the top of stack would
24340 be returned. */
24341 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24342 address will be bogus in the psymtab. Make it always at least
24343 non-zero to not look as a variable garbage collected by linker
24344 which have DW_OP_addr 0. */
21ae7a4d
JK
24345 if (i < size)
24346 dwarf2_complex_location_expr_complaint ();
585861ea 24347 stack[stacki]++;
21ae7a4d
JK
24348 break;
24349
24350 case DW_OP_GNU_uninit:
24351 break;
24352
336d760d 24353 case DW_OP_addrx:
3019eac3 24354 case DW_OP_GNU_addr_index:
49f6c839 24355 case DW_OP_GNU_const_index:
3019eac3
DE
24356 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24357 &bytes_read);
24358 i += bytes_read;
24359 break;
24360
21ae7a4d
JK
24361 default:
24362 {
f39c6ffd 24363 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24364
24365 if (name)
b98664d3 24366 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24367 name);
24368 else
b98664d3 24369 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24370 op);
24371 }
24372
24373 return (stack[stacki]);
d53d4ac5 24374 }
3c6e0cb3 24375
21ae7a4d
JK
24376 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24377 outside of the allocated space. Also enforce minimum>0. */
24378 if (stacki >= ARRAY_SIZE (stack) - 1)
24379 {
b98664d3 24380 complaint (_("location description stack overflow"));
21ae7a4d
JK
24381 return 0;
24382 }
24383
24384 if (stacki <= 0)
24385 {
b98664d3 24386 complaint (_("location description stack underflow"));
21ae7a4d
JK
24387 return 0;
24388 }
24389 }
24390 return (stack[stacki]);
c906108c
SS
24391}
24392
24393/* memory allocation interface */
24394
c906108c 24395static struct dwarf_block *
7b5a2f43 24396dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24397{
8d749320 24398 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24399}
24400
c906108c 24401static struct die_info *
b60c80d6 24402dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24403{
24404 struct die_info *die;
b60c80d6
DJ
24405 size_t size = sizeof (struct die_info);
24406
24407 if (num_attrs > 1)
24408 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24409
b60c80d6 24410 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24411 memset (die, 0, sizeof (struct die_info));
24412 return (die);
24413}
2e276125
JB
24414
24415\f
24416/* Macro support. */
24417
233d95b5
JK
24418/* Return file name relative to the compilation directory of file number I in
24419 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24420 responsible for freeing it. */
233d95b5 24421
2e276125 24422static char *
233d95b5 24423file_file_name (int file, struct line_header *lh)
2e276125 24424{
6a83a1e6
EZ
24425 /* Is the file number a valid index into the line header's file name
24426 table? Remember that file numbers start with one, not zero. */
7ba99d21 24427 if (lh->is_valid_file_index (file))
6a83a1e6 24428 {
7ba99d21 24429 const file_entry *fe = lh->file_name_at (file);
6e70227d 24430
7ba99d21 24431 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24432 {
7ba99d21 24433 const char *dir = fe->include_dir (lh);
8c43009f 24434 if (dir != NULL)
7ba99d21 24435 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24436 }
7ba99d21 24437 return xstrdup (fe->name);
6a83a1e6 24438 }
2e276125
JB
24439 else
24440 {
6a83a1e6
EZ
24441 /* The compiler produced a bogus file number. We can at least
24442 record the macro definitions made in the file, even if we
24443 won't be able to find the file by name. */
24444 char fake_name[80];
9a619af0 24445
8c042590
PM
24446 xsnprintf (fake_name, sizeof (fake_name),
24447 "<bad macro file number %d>", file);
2e276125 24448
b98664d3 24449 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24450 file);
2e276125 24451
6a83a1e6 24452 return xstrdup (fake_name);
2e276125
JB
24453 }
24454}
24455
233d95b5
JK
24456/* Return the full name of file number I in *LH's file name table.
24457 Use COMP_DIR as the name of the current directory of the
24458 compilation. The result is allocated using xmalloc; the caller is
24459 responsible for freeing it. */
24460static char *
24461file_full_name (int file, struct line_header *lh, const char *comp_dir)
24462{
24463 /* Is the file number a valid index into the line header's file name
24464 table? Remember that file numbers start with one, not zero. */
7ba99d21 24465 if (lh->is_valid_file_index (file))
233d95b5
JK
24466 {
24467 char *relative = file_file_name (file, lh);
24468
24469 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24470 return relative;
b36cec19
PA
24471 return reconcat (relative, comp_dir, SLASH_STRING,
24472 relative, (char *) NULL);
233d95b5
JK
24473 }
24474 else
24475 return file_file_name (file, lh);
24476}
24477
2e276125
JB
24478
24479static struct macro_source_file *
804d2729
TT
24480macro_start_file (struct dwarf2_cu *cu,
24481 int file, int line,
2e276125 24482 struct macro_source_file *current_file,
43f3e411 24483 struct line_header *lh)
2e276125 24484{
233d95b5
JK
24485 /* File name relative to the compilation directory of this source file. */
24486 char *file_name = file_file_name (file, lh);
2e276125 24487
2e276125 24488 if (! current_file)
abc9d0dc 24489 {
fc474241
DE
24490 /* Note: We don't create a macro table for this compilation unit
24491 at all until we actually get a filename. */
c24bdb02 24492 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24493
abc9d0dc
TT
24494 /* If we have no current file, then this must be the start_file
24495 directive for the compilation unit's main source file. */
fc474241
DE
24496 current_file = macro_set_main (macro_table, file_name);
24497 macro_define_special (macro_table);
abc9d0dc 24498 }
2e276125 24499 else
233d95b5 24500 current_file = macro_include (current_file, line, file_name);
2e276125 24501
233d95b5 24502 xfree (file_name);
6e70227d 24503
2e276125
JB
24504 return current_file;
24505}
24506
2e276125
JB
24507static const char *
24508consume_improper_spaces (const char *p, const char *body)
24509{
24510 if (*p == ' ')
24511 {
b98664d3 24512 complaint (_("macro definition contains spaces "
3e43a32a 24513 "in formal argument list:\n`%s'"),
4d3c2250 24514 body);
2e276125
JB
24515
24516 while (*p == ' ')
24517 p++;
24518 }
24519
24520 return p;
24521}
24522
24523
24524static void
24525parse_macro_definition (struct macro_source_file *file, int line,
24526 const char *body)
24527{
24528 const char *p;
24529
24530 /* The body string takes one of two forms. For object-like macro
24531 definitions, it should be:
24532
24533 <macro name> " " <definition>
24534
24535 For function-like macro definitions, it should be:
24536
24537 <macro name> "() " <definition>
24538 or
24539 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24540
24541 Spaces may appear only where explicitly indicated, and in the
24542 <definition>.
24543
24544 The Dwarf 2 spec says that an object-like macro's name is always
24545 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24546 the space when the macro's definition is the empty string.
2e276125
JB
24547
24548 The Dwarf 2 spec says that there should be no spaces between the
24549 formal arguments in a function-like macro's formal argument list,
24550 but versions of GCC around March 2002 include spaces after the
24551 commas. */
24552
24553
24554 /* Find the extent of the macro name. The macro name is terminated
24555 by either a space or null character (for an object-like macro) or
24556 an opening paren (for a function-like macro). */
24557 for (p = body; *p; p++)
24558 if (*p == ' ' || *p == '(')
24559 break;
24560
24561 if (*p == ' ' || *p == '\0')
24562 {
24563 /* It's an object-like macro. */
24564 int name_len = p - body;
456e800a 24565 std::string name (body, name_len);
2e276125
JB
24566 const char *replacement;
24567
24568 if (*p == ' ')
24569 replacement = body + name_len + 1;
24570 else
24571 {
4d3c2250 24572 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24573 replacement = body + name_len;
24574 }
6e70227d 24575
456e800a 24576 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
24577 }
24578 else if (*p == '(')
24579 {
24580 /* It's a function-like macro. */
456e800a 24581 std::string name (body, p - body);
2e276125
JB
24582 int argc = 0;
24583 int argv_size = 1;
8d749320 24584 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24585
24586 p++;
24587
24588 p = consume_improper_spaces (p, body);
24589
24590 /* Parse the formal argument list. */
24591 while (*p && *p != ')')
24592 {
24593 /* Find the extent of the current argument name. */
24594 const char *arg_start = p;
24595
24596 while (*p && *p != ',' && *p != ')' && *p != ' ')
24597 p++;
24598
24599 if (! *p || p == arg_start)
4d3c2250 24600 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24601 else
24602 {
24603 /* Make sure argv has room for the new argument. */
24604 if (argc >= argv_size)
24605 {
24606 argv_size *= 2;
224c3ddb 24607 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24608 }
24609
3f8a7804 24610 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24611 }
24612
24613 p = consume_improper_spaces (p, body);
24614
24615 /* Consume the comma, if present. */
24616 if (*p == ',')
24617 {
24618 p++;
24619
24620 p = consume_improper_spaces (p, body);
24621 }
24622 }
24623
24624 if (*p == ')')
24625 {
24626 p++;
24627
24628 if (*p == ' ')
24629 /* Perfectly formed definition, no complaints. */
456e800a 24630 macro_define_function (file, line, name.c_str (),
6e70227d 24631 argc, (const char **) argv,
2e276125
JB
24632 p + 1);
24633 else if (*p == '\0')
24634 {
24635 /* Complain, but do define it. */
4d3c2250 24636 dwarf2_macro_malformed_definition_complaint (body);
456e800a 24637 macro_define_function (file, line, name.c_str (),
6e70227d 24638 argc, (const char **) argv,
2e276125
JB
24639 p);
24640 }
24641 else
24642 /* Just complain. */
4d3c2250 24643 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24644 }
24645 else
24646 /* Just complain. */
4d3c2250 24647 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24648
2e276125
JB
24649 {
24650 int i;
24651
24652 for (i = 0; i < argc; i++)
24653 xfree (argv[i]);
24654 }
24655 xfree (argv);
24656 }
24657 else
4d3c2250 24658 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24659}
24660
cf2c3c16
TT
24661/* Skip some bytes from BYTES according to the form given in FORM.
24662 Returns the new pointer. */
2e276125 24663
d521ce57
TT
24664static const gdb_byte *
24665skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24666 enum dwarf_form form,
24667 unsigned int offset_size,
24668 struct dwarf2_section_info *section)
2e276125 24669{
cf2c3c16 24670 unsigned int bytes_read;
2e276125 24671
cf2c3c16 24672 switch (form)
2e276125 24673 {
cf2c3c16
TT
24674 case DW_FORM_data1:
24675 case DW_FORM_flag:
24676 ++bytes;
24677 break;
24678
24679 case DW_FORM_data2:
24680 bytes += 2;
24681 break;
24682
24683 case DW_FORM_data4:
24684 bytes += 4;
24685 break;
24686
24687 case DW_FORM_data8:
24688 bytes += 8;
24689 break;
24690
0224619f
JK
24691 case DW_FORM_data16:
24692 bytes += 16;
24693 break;
24694
cf2c3c16
TT
24695 case DW_FORM_string:
24696 read_direct_string (abfd, bytes, &bytes_read);
24697 bytes += bytes_read;
24698 break;
24699
24700 case DW_FORM_sec_offset:
24701 case DW_FORM_strp:
36586728 24702 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24703 bytes += offset_size;
24704 break;
24705
24706 case DW_FORM_block:
24707 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24708 bytes += bytes_read;
24709 break;
24710
24711 case DW_FORM_block1:
24712 bytes += 1 + read_1_byte (abfd, bytes);
24713 break;
24714 case DW_FORM_block2:
24715 bytes += 2 + read_2_bytes (abfd, bytes);
24716 break;
24717 case DW_FORM_block4:
24718 bytes += 4 + read_4_bytes (abfd, bytes);
24719 break;
24720
336d760d 24721 case DW_FORM_addrx:
cf2c3c16 24722 case DW_FORM_sdata:
cf532bd1 24723 case DW_FORM_strx:
cf2c3c16 24724 case DW_FORM_udata:
3019eac3
DE
24725 case DW_FORM_GNU_addr_index:
24726 case DW_FORM_GNU_str_index:
d521ce57 24727 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24728 if (bytes == NULL)
24729 {
24730 dwarf2_section_buffer_overflow_complaint (section);
24731 return NULL;
24732 }
cf2c3c16
TT
24733 break;
24734
663c44ac
JK
24735 case DW_FORM_implicit_const:
24736 break;
24737
cf2c3c16
TT
24738 default:
24739 {
b98664d3 24740 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24741 form, get_section_name (section));
cf2c3c16
TT
24742 return NULL;
24743 }
2e276125
JB
24744 }
24745
cf2c3c16
TT
24746 return bytes;
24747}
757a13d0 24748
cf2c3c16
TT
24749/* A helper for dwarf_decode_macros that handles skipping an unknown
24750 opcode. Returns an updated pointer to the macro data buffer; or,
24751 on error, issues a complaint and returns NULL. */
757a13d0 24752
d521ce57 24753static const gdb_byte *
cf2c3c16 24754skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24755 const gdb_byte **opcode_definitions,
24756 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24757 bfd *abfd,
24758 unsigned int offset_size,
24759 struct dwarf2_section_info *section)
24760{
24761 unsigned int bytes_read, i;
24762 unsigned long arg;
d521ce57 24763 const gdb_byte *defn;
2e276125 24764
cf2c3c16 24765 if (opcode_definitions[opcode] == NULL)
2e276125 24766 {
b98664d3 24767 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24768 opcode);
24769 return NULL;
24770 }
2e276125 24771
cf2c3c16
TT
24772 defn = opcode_definitions[opcode];
24773 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24774 defn += bytes_read;
2e276125 24775
cf2c3c16
TT
24776 for (i = 0; i < arg; ++i)
24777 {
aead7601
SM
24778 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24779 (enum dwarf_form) defn[i], offset_size,
f664829e 24780 section);
cf2c3c16
TT
24781 if (mac_ptr == NULL)
24782 {
24783 /* skip_form_bytes already issued the complaint. */
24784 return NULL;
24785 }
24786 }
757a13d0 24787
cf2c3c16
TT
24788 return mac_ptr;
24789}
757a13d0 24790
cf2c3c16
TT
24791/* A helper function which parses the header of a macro section.
24792 If the macro section is the extended (for now called "GNU") type,
24793 then this updates *OFFSET_SIZE. Returns a pointer to just after
24794 the header, or issues a complaint and returns NULL on error. */
757a13d0 24795
d521ce57
TT
24796static const gdb_byte *
24797dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24798 bfd *abfd,
d521ce57 24799 const gdb_byte *mac_ptr,
cf2c3c16
TT
24800 unsigned int *offset_size,
24801 int section_is_gnu)
24802{
24803 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24804
cf2c3c16
TT
24805 if (section_is_gnu)
24806 {
24807 unsigned int version, flags;
757a13d0 24808
cf2c3c16 24809 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24810 if (version != 4 && version != 5)
cf2c3c16 24811 {
b98664d3 24812 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24813 version);
24814 return NULL;
24815 }
24816 mac_ptr += 2;
757a13d0 24817
cf2c3c16
TT
24818 flags = read_1_byte (abfd, mac_ptr);
24819 ++mac_ptr;
24820 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24821
cf2c3c16
TT
24822 if ((flags & 2) != 0)
24823 /* We don't need the line table offset. */
24824 mac_ptr += *offset_size;
757a13d0 24825
cf2c3c16
TT
24826 /* Vendor opcode descriptions. */
24827 if ((flags & 4) != 0)
24828 {
24829 unsigned int i, count;
757a13d0 24830
cf2c3c16
TT
24831 count = read_1_byte (abfd, mac_ptr);
24832 ++mac_ptr;
24833 for (i = 0; i < count; ++i)
24834 {
24835 unsigned int opcode, bytes_read;
24836 unsigned long arg;
24837
24838 opcode = read_1_byte (abfd, mac_ptr);
24839 ++mac_ptr;
24840 opcode_definitions[opcode] = mac_ptr;
24841 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24842 mac_ptr += bytes_read;
24843 mac_ptr += arg;
24844 }
757a13d0 24845 }
cf2c3c16 24846 }
757a13d0 24847
cf2c3c16
TT
24848 return mac_ptr;
24849}
757a13d0 24850
cf2c3c16 24851/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24852 including DW_MACRO_import. */
cf2c3c16
TT
24853
24854static void
804d2729 24855dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24856 bfd *abfd,
d521ce57 24857 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24858 struct macro_source_file *current_file,
43f3e411 24859 struct line_header *lh,
cf2c3c16 24860 struct dwarf2_section_info *section,
36586728 24861 int section_is_gnu, int section_is_dwz,
cf2c3c16 24862 unsigned int offset_size,
8fc3fc34 24863 htab_t include_hash)
cf2c3c16 24864{
804d2729
TT
24865 struct dwarf2_per_objfile *dwarf2_per_objfile
24866 = cu->per_cu->dwarf2_per_objfile;
4d663531 24867 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24868 enum dwarf_macro_record_type macinfo_type;
24869 int at_commandline;
d521ce57 24870 const gdb_byte *opcode_definitions[256];
757a13d0 24871
cf2c3c16
TT
24872 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24873 &offset_size, section_is_gnu);
24874 if (mac_ptr == NULL)
24875 {
24876 /* We already issued a complaint. */
24877 return;
24878 }
757a13d0
JK
24879
24880 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24881 GDB is still reading the definitions from command line. First
24882 DW_MACINFO_start_file will need to be ignored as it was already executed
24883 to create CURRENT_FILE for the main source holding also the command line
24884 definitions. On first met DW_MACINFO_start_file this flag is reset to
24885 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24886
24887 at_commandline = 1;
24888
24889 do
24890 {
24891 /* Do we at least have room for a macinfo type byte? */
24892 if (mac_ptr >= mac_end)
24893 {
f664829e 24894 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24895 break;
24896 }
24897
aead7601 24898 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24899 mac_ptr++;
24900
cf2c3c16
TT
24901 /* Note that we rely on the fact that the corresponding GNU and
24902 DWARF constants are the same. */
132448f8
SM
24903 DIAGNOSTIC_PUSH
24904 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24905 switch (macinfo_type)
24906 {
24907 /* A zero macinfo type indicates the end of the macro
24908 information. */
24909 case 0:
24910 break;
2e276125 24911
0af92d60
JK
24912 case DW_MACRO_define:
24913 case DW_MACRO_undef:
24914 case DW_MACRO_define_strp:
24915 case DW_MACRO_undef_strp:
24916 case DW_MACRO_define_sup:
24917 case DW_MACRO_undef_sup:
2e276125 24918 {
891d2f0b 24919 unsigned int bytes_read;
2e276125 24920 int line;
d521ce57 24921 const char *body;
cf2c3c16 24922 int is_define;
2e276125 24923
cf2c3c16
TT
24924 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24925 mac_ptr += bytes_read;
24926
0af92d60
JK
24927 if (macinfo_type == DW_MACRO_define
24928 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24929 {
24930 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24931 mac_ptr += bytes_read;
24932 }
24933 else
24934 {
24935 LONGEST str_offset;
24936
24937 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24938 mac_ptr += offset_size;
2e276125 24939
0af92d60
JK
24940 if (macinfo_type == DW_MACRO_define_sup
24941 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24942 || section_is_dwz)
36586728 24943 {
ed2dc618
SM
24944 struct dwz_file *dwz
24945 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24946
ed2dc618
SM
24947 body = read_indirect_string_from_dwz (objfile,
24948 dwz, str_offset);
36586728
TT
24949 }
24950 else
ed2dc618
SM
24951 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24952 abfd, str_offset);
cf2c3c16
TT
24953 }
24954
0af92d60
JK
24955 is_define = (macinfo_type == DW_MACRO_define
24956 || macinfo_type == DW_MACRO_define_strp
24957 || macinfo_type == DW_MACRO_define_sup);
2e276125 24958 if (! current_file)
757a13d0
JK
24959 {
24960 /* DWARF violation as no main source is present. */
b98664d3 24961 complaint (_("debug info with no main source gives macro %s "
757a13d0 24962 "on line %d: %s"),
cf2c3c16
TT
24963 is_define ? _("definition") : _("undefinition"),
24964 line, body);
757a13d0
JK
24965 break;
24966 }
3e43a32a
MS
24967 if ((line == 0 && !at_commandline)
24968 || (line != 0 && at_commandline))
b98664d3 24969 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24970 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24971 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24972 line == 0 ? _("zero") : _("non-zero"), line, body);
24973
955b06fa 24974 if (body == NULL)
7bede828 24975 {
955b06fa
SDJ
24976 /* Fedora's rpm-build's "debugedit" binary
24977 corrupted .debug_macro sections.
24978
24979 For more info, see
24980 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24981 complaint (_("debug info gives %s invalid macro %s "
24982 "without body (corrupted?) at line %d "
24983 "on file %s"),
24984 at_commandline ? _("command-line") : _("in-file"),
24985 is_define ? _("definition") : _("undefinition"),
24986 line, current_file->filename);
7bede828 24987 }
955b06fa
SDJ
24988 else if (is_define)
24989 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24990 else
24991 {
0af92d60
JK
24992 gdb_assert (macinfo_type == DW_MACRO_undef
24993 || macinfo_type == DW_MACRO_undef_strp
24994 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24995 macro_undef (current_file, line, body);
24996 }
2e276125
JB
24997 }
24998 break;
24999
0af92d60 25000 case DW_MACRO_start_file:
2e276125 25001 {
891d2f0b 25002 unsigned int bytes_read;
2e276125
JB
25003 int line, file;
25004
25005 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25006 mac_ptr += bytes_read;
25007 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25008 mac_ptr += bytes_read;
25009
3e43a32a
MS
25010 if ((line == 0 && !at_commandline)
25011 || (line != 0 && at_commandline))
b98664d3 25012 complaint (_("debug info gives source %d included "
757a13d0
JK
25013 "from %s at %s line %d"),
25014 file, at_commandline ? _("command-line") : _("file"),
25015 line == 0 ? _("zero") : _("non-zero"), line);
25016
25017 if (at_commandline)
25018 {
0af92d60 25019 /* This DW_MACRO_start_file was executed in the
cf2c3c16 25020 pass one. */
757a13d0
JK
25021 at_commandline = 0;
25022 }
25023 else
804d2729
TT
25024 current_file = macro_start_file (cu, file, line, current_file,
25025 lh);
2e276125
JB
25026 }
25027 break;
25028
0af92d60 25029 case DW_MACRO_end_file:
2e276125 25030 if (! current_file)
b98664d3 25031 complaint (_("macro debug info has an unmatched "
3e43a32a 25032 "`close_file' directive"));
2e276125
JB
25033 else
25034 {
25035 current_file = current_file->included_by;
25036 if (! current_file)
25037 {
cf2c3c16 25038 enum dwarf_macro_record_type next_type;
2e276125
JB
25039
25040 /* GCC circa March 2002 doesn't produce the zero
25041 type byte marking the end of the compilation
25042 unit. Complain if it's not there, but exit no
25043 matter what. */
25044
25045 /* Do we at least have room for a macinfo type byte? */
25046 if (mac_ptr >= mac_end)
25047 {
f664829e 25048 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
25049 return;
25050 }
25051
25052 /* We don't increment mac_ptr here, so this is just
25053 a look-ahead. */
aead7601
SM
25054 next_type
25055 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25056 mac_ptr);
2e276125 25057 if (next_type != 0)
b98664d3 25058 complaint (_("no terminating 0-type entry for "
3e43a32a 25059 "macros in `.debug_macinfo' section"));
2e276125
JB
25060
25061 return;
25062 }
25063 }
25064 break;
25065
0af92d60
JK
25066 case DW_MACRO_import:
25067 case DW_MACRO_import_sup:
cf2c3c16
TT
25068 {
25069 LONGEST offset;
8fc3fc34 25070 void **slot;
a036ba48
TT
25071 bfd *include_bfd = abfd;
25072 struct dwarf2_section_info *include_section = section;
d521ce57 25073 const gdb_byte *include_mac_end = mac_end;
a036ba48 25074 int is_dwz = section_is_dwz;
d521ce57 25075 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
25076
25077 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25078 mac_ptr += offset_size;
25079
0af92d60 25080 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 25081 {
ed2dc618 25082 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 25083
4d663531 25084 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 25085
a036ba48 25086 include_section = &dwz->macro;
a32a8923 25087 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
25088 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25089 is_dwz = 1;
25090 }
25091
25092 new_mac_ptr = include_section->buffer + offset;
25093 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25094
8fc3fc34
TT
25095 if (*slot != NULL)
25096 {
25097 /* This has actually happened; see
25098 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 25099 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
25100 ".debug_macro section"));
25101 }
25102 else
25103 {
d521ce57 25104 *slot = (void *) new_mac_ptr;
36586728 25105
804d2729 25106 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 25107 include_mac_end, current_file, lh,
36586728 25108 section, section_is_gnu, is_dwz,
4d663531 25109 offset_size, include_hash);
8fc3fc34 25110
d521ce57 25111 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 25112 }
cf2c3c16
TT
25113 }
25114 break;
25115
2e276125 25116 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
25117 if (!section_is_gnu)
25118 {
25119 unsigned int bytes_read;
2e276125 25120
ac298888
TT
25121 /* This reads the constant, but since we don't recognize
25122 any vendor extensions, we ignore it. */
25123 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
25124 mac_ptr += bytes_read;
25125 read_direct_string (abfd, mac_ptr, &bytes_read);
25126 mac_ptr += bytes_read;
2e276125 25127
cf2c3c16
TT
25128 /* We don't recognize any vendor extensions. */
25129 break;
25130 }
25131 /* FALLTHROUGH */
25132
25133 default:
25134 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25135 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25136 section);
25137 if (mac_ptr == NULL)
25138 return;
25139 break;
2e276125 25140 }
132448f8 25141 DIAGNOSTIC_POP
757a13d0 25142 } while (macinfo_type != 0);
2e276125 25143}
8e19ed76 25144
cf2c3c16 25145static void
09262596 25146dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 25147 int section_is_gnu)
cf2c3c16 25148{
518817b3
SM
25149 struct dwarf2_per_objfile *dwarf2_per_objfile
25150 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25151 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
25152 struct line_header *lh = cu->line_header;
25153 bfd *abfd;
d521ce57 25154 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
25155 struct macro_source_file *current_file = 0;
25156 enum dwarf_macro_record_type macinfo_type;
25157 unsigned int offset_size = cu->header.offset_size;
d521ce57 25158 const gdb_byte *opcode_definitions[256];
8fc3fc34 25159 void **slot;
09262596
DE
25160 struct dwarf2_section_info *section;
25161 const char *section_name;
25162
25163 if (cu->dwo_unit != NULL)
25164 {
25165 if (section_is_gnu)
25166 {
25167 section = &cu->dwo_unit->dwo_file->sections.macro;
25168 section_name = ".debug_macro.dwo";
25169 }
25170 else
25171 {
25172 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25173 section_name = ".debug_macinfo.dwo";
25174 }
25175 }
25176 else
25177 {
25178 if (section_is_gnu)
25179 {
25180 section = &dwarf2_per_objfile->macro;
25181 section_name = ".debug_macro";
25182 }
25183 else
25184 {
25185 section = &dwarf2_per_objfile->macinfo;
25186 section_name = ".debug_macinfo";
25187 }
25188 }
cf2c3c16 25189
bb5ed363 25190 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25191 if (section->buffer == NULL)
25192 {
b98664d3 25193 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
25194 return;
25195 }
a32a8923 25196 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25197
25198 /* First pass: Find the name of the base filename.
25199 This filename is needed in order to process all macros whose definition
25200 (or undefinition) comes from the command line. These macros are defined
25201 before the first DW_MACINFO_start_file entry, and yet still need to be
25202 associated to the base file.
25203
25204 To determine the base file name, we scan the macro definitions until we
25205 reach the first DW_MACINFO_start_file entry. We then initialize
25206 CURRENT_FILE accordingly so that any macro definition found before the
25207 first DW_MACINFO_start_file can still be associated to the base file. */
25208
25209 mac_ptr = section->buffer + offset;
25210 mac_end = section->buffer + section->size;
25211
25212 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25213 &offset_size, section_is_gnu);
25214 if (mac_ptr == NULL)
25215 {
25216 /* We already issued a complaint. */
25217 return;
25218 }
25219
25220 do
25221 {
25222 /* Do we at least have room for a macinfo type byte? */
25223 if (mac_ptr >= mac_end)
25224 {
25225 /* Complaint is printed during the second pass as GDB will probably
25226 stop the first pass earlier upon finding
25227 DW_MACINFO_start_file. */
25228 break;
25229 }
25230
aead7601 25231 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25232 mac_ptr++;
25233
25234 /* Note that we rely on the fact that the corresponding GNU and
25235 DWARF constants are the same. */
132448f8
SM
25236 DIAGNOSTIC_PUSH
25237 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25238 switch (macinfo_type)
25239 {
25240 /* A zero macinfo type indicates the end of the macro
25241 information. */
25242 case 0:
25243 break;
25244
0af92d60
JK
25245 case DW_MACRO_define:
25246 case DW_MACRO_undef:
cf2c3c16
TT
25247 /* Only skip the data by MAC_PTR. */
25248 {
25249 unsigned int bytes_read;
25250
25251 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25252 mac_ptr += bytes_read;
25253 read_direct_string (abfd, mac_ptr, &bytes_read);
25254 mac_ptr += bytes_read;
25255 }
25256 break;
25257
0af92d60 25258 case DW_MACRO_start_file:
cf2c3c16
TT
25259 {
25260 unsigned int bytes_read;
25261 int line, file;
25262
25263 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25264 mac_ptr += bytes_read;
25265 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25266 mac_ptr += bytes_read;
25267
804d2729 25268 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25269 }
25270 break;
25271
0af92d60 25272 case DW_MACRO_end_file:
cf2c3c16
TT
25273 /* No data to skip by MAC_PTR. */
25274 break;
25275
0af92d60
JK
25276 case DW_MACRO_define_strp:
25277 case DW_MACRO_undef_strp:
25278 case DW_MACRO_define_sup:
25279 case DW_MACRO_undef_sup:
cf2c3c16
TT
25280 {
25281 unsigned int bytes_read;
25282
25283 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25284 mac_ptr += bytes_read;
25285 mac_ptr += offset_size;
25286 }
25287 break;
25288
0af92d60
JK
25289 case DW_MACRO_import:
25290 case DW_MACRO_import_sup:
cf2c3c16 25291 /* Note that, according to the spec, a transparent include
0af92d60 25292 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25293 skip this opcode. */
25294 mac_ptr += offset_size;
25295 break;
25296
25297 case DW_MACINFO_vendor_ext:
25298 /* Only skip the data by MAC_PTR. */
25299 if (!section_is_gnu)
25300 {
25301 unsigned int bytes_read;
25302
25303 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25304 mac_ptr += bytes_read;
25305 read_direct_string (abfd, mac_ptr, &bytes_read);
25306 mac_ptr += bytes_read;
25307 }
25308 /* FALLTHROUGH */
25309
25310 default:
25311 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25312 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25313 section);
25314 if (mac_ptr == NULL)
25315 return;
25316 break;
25317 }
132448f8 25318 DIAGNOSTIC_POP
cf2c3c16
TT
25319 } while (macinfo_type != 0 && current_file == NULL);
25320
25321 /* Second pass: Process all entries.
25322
25323 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25324 command-line macro definitions/undefinitions. This flag is unset when we
25325 reach the first DW_MACINFO_start_file entry. */
25326
fc4007c9
TT
25327 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25328 htab_eq_pointer,
25329 NULL, xcalloc, xfree));
8fc3fc34 25330 mac_ptr = section->buffer + offset;
fc4007c9 25331 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25332 *slot = (void *) mac_ptr;
804d2729 25333 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25334 current_file, lh, section,
fc4007c9
TT
25335 section_is_gnu, 0, offset_size,
25336 include_hash.get ());
cf2c3c16
TT
25337}
25338
8e19ed76 25339/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25340 if so return true else false. */
380bca97 25341
8e19ed76 25342static int
6e5a29e1 25343attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25344{
25345 return (attr == NULL ? 0 :
25346 attr->form == DW_FORM_block1
25347 || attr->form == DW_FORM_block2
25348 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25349 || attr->form == DW_FORM_block
25350 || attr->form == DW_FORM_exprloc);
8e19ed76 25351}
4c2df51b 25352
c6a0999f
JB
25353/* Return non-zero if ATTR's value is a section offset --- classes
25354 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25355 You may use DW_UNSND (attr) to retrieve such offsets.
25356
25357 Section 7.5.4, "Attribute Encodings", explains that no attribute
25358 may have a value that belongs to more than one of these classes; it
25359 would be ambiguous if we did, because we use the same forms for all
25360 of them. */
380bca97 25361
3690dd37 25362static int
6e5a29e1 25363attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25364{
25365 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25366 || attr->form == DW_FORM_data8
25367 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25368}
25369
3690dd37
JB
25370/* Return non-zero if ATTR's value falls in the 'constant' class, or
25371 zero otherwise. When this function returns true, you can apply
25372 dwarf2_get_attr_constant_value to it.
25373
25374 However, note that for some attributes you must check
25375 attr_form_is_section_offset before using this test. DW_FORM_data4
25376 and DW_FORM_data8 are members of both the constant class, and of
25377 the classes that contain offsets into other debug sections
25378 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25379 that, if an attribute's can be either a constant or one of the
25380 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25381 taken as section offsets, not constants.
25382
25383 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25384 cannot handle that. */
380bca97 25385
3690dd37 25386static int
6e5a29e1 25387attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25388{
25389 switch (attr->form)
25390 {
25391 case DW_FORM_sdata:
25392 case DW_FORM_udata:
25393 case DW_FORM_data1:
25394 case DW_FORM_data2:
25395 case DW_FORM_data4:
25396 case DW_FORM_data8:
663c44ac 25397 case DW_FORM_implicit_const:
3690dd37
JB
25398 return 1;
25399 default:
25400 return 0;
25401 }
25402}
25403
7771576e
SA
25404
25405/* DW_ADDR is always stored already as sect_offset; despite for the forms
25406 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25407
25408static int
6e5a29e1 25409attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25410{
25411 switch (attr->form)
25412 {
25413 case DW_FORM_ref_addr:
25414 case DW_FORM_ref1:
25415 case DW_FORM_ref2:
25416 case DW_FORM_ref4:
25417 case DW_FORM_ref8:
25418 case DW_FORM_ref_udata:
25419 case DW_FORM_GNU_ref_alt:
25420 return 1;
25421 default:
25422 return 0;
25423 }
25424}
25425
3019eac3
DE
25426/* Return the .debug_loc section to use for CU.
25427 For DWO files use .debug_loc.dwo. */
25428
25429static struct dwarf2_section_info *
25430cu_debug_loc_section (struct dwarf2_cu *cu)
25431{
518817b3
SM
25432 struct dwarf2_per_objfile *dwarf2_per_objfile
25433 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25434
3019eac3 25435 if (cu->dwo_unit)
43988095
JK
25436 {
25437 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25438
43988095
JK
25439 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25440 }
25441 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25442 : &dwarf2_per_objfile->loc);
3019eac3
DE
25443}
25444
8cf6f0b1
TT
25445/* A helper function that fills in a dwarf2_loclist_baton. */
25446
25447static void
25448fill_in_loclist_baton (struct dwarf2_cu *cu,
25449 struct dwarf2_loclist_baton *baton,
ff39bb5e 25450 const struct attribute *attr)
8cf6f0b1 25451{
518817b3
SM
25452 struct dwarf2_per_objfile *dwarf2_per_objfile
25453 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25454 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25455
25456 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25457
25458 baton->per_cu = cu->per_cu;
25459 gdb_assert (baton->per_cu);
25460 /* We don't know how long the location list is, but make sure we
25461 don't run off the edge of the section. */
3019eac3
DE
25462 baton->size = section->size - DW_UNSND (attr);
25463 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25464 baton->base_address = cu->base_address;
f664829e 25465 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25466}
25467
4c2df51b 25468static void
ff39bb5e 25469dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25470 struct dwarf2_cu *cu, int is_block)
4c2df51b 25471{
518817b3
SM
25472 struct dwarf2_per_objfile *dwarf2_per_objfile
25473 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25474 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25475 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25476
3690dd37 25477 if (attr_form_is_section_offset (attr)
3019eac3 25478 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25479 the section. If so, fall through to the complaint in the
25480 other branch. */
3019eac3 25481 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25482 {
0d53c4c4 25483 struct dwarf2_loclist_baton *baton;
4c2df51b 25484
8d749320 25485 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25486
8cf6f0b1 25487 fill_in_loclist_baton (cu, baton, attr);
be391dca 25488
d00adf39 25489 if (cu->base_known == 0)
b98664d3 25490 complaint (_("Location list used without "
3e43a32a 25491 "specifying the CU base address."));
4c2df51b 25492
f1e6e072
TT
25493 SYMBOL_ACLASS_INDEX (sym) = (is_block
25494 ? dwarf2_loclist_block_index
25495 : dwarf2_loclist_index);
0d53c4c4
DJ
25496 SYMBOL_LOCATION_BATON (sym) = baton;
25497 }
25498 else
25499 {
25500 struct dwarf2_locexpr_baton *baton;
25501
8d749320 25502 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25503 baton->per_cu = cu->per_cu;
25504 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25505
25506 if (attr_form_is_block (attr))
25507 {
25508 /* Note that we're just copying the block's data pointer
25509 here, not the actual data. We're still pointing into the
6502dd73
DJ
25510 info_buffer for SYM's objfile; right now we never release
25511 that buffer, but when we do clean up properly this may
25512 need to change. */
0d53c4c4
DJ
25513 baton->size = DW_BLOCK (attr)->size;
25514 baton->data = DW_BLOCK (attr)->data;
25515 }
25516 else
25517 {
25518 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 25519 sym->natural_name ());
0d53c4c4 25520 baton->size = 0;
0d53c4c4 25521 }
6e70227d 25522
f1e6e072
TT
25523 SYMBOL_ACLASS_INDEX (sym) = (is_block
25524 ? dwarf2_locexpr_block_index
25525 : dwarf2_locexpr_index);
0d53c4c4
DJ
25526 SYMBOL_LOCATION_BATON (sym) = baton;
25527 }
4c2df51b 25528}
6502dd73 25529
9aa1f1e3
TT
25530/* Return the OBJFILE associated with the compilation unit CU. If CU
25531 came from a separate debuginfo file, then the master objfile is
25532 returned. */
ae0d2f24
UW
25533
25534struct objfile *
25535dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25536{
e3b94546 25537 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25538
25539 /* Return the master objfile, so that we can report and look up the
25540 correct file containing this variable. */
25541 if (objfile->separate_debug_objfile_backlink)
25542 objfile = objfile->separate_debug_objfile_backlink;
25543
25544 return objfile;
25545}
25546
96408a79
SA
25547/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25548 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25549 CU_HEADERP first. */
25550
25551static const struct comp_unit_head *
25552per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25553 struct dwarf2_per_cu_data *per_cu)
25554{
d521ce57 25555 const gdb_byte *info_ptr;
96408a79
SA
25556
25557 if (per_cu->cu)
25558 return &per_cu->cu->header;
25559
9c541725 25560 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25561
25562 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25563 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25564 rcuh_kind::COMPILE);
96408a79
SA
25565
25566 return cu_headerp;
25567}
25568
ae0d2f24
UW
25569/* Return the address size given in the compilation unit header for CU. */
25570
98714339 25571int
ae0d2f24
UW
25572dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25573{
96408a79
SA
25574 struct comp_unit_head cu_header_local;
25575 const struct comp_unit_head *cu_headerp;
c471e790 25576
96408a79
SA
25577 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25578
25579 return cu_headerp->addr_size;
ae0d2f24
UW
25580}
25581
9eae7c52
TT
25582/* Return the offset size given in the compilation unit header for CU. */
25583
25584int
25585dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25586{
96408a79
SA
25587 struct comp_unit_head cu_header_local;
25588 const struct comp_unit_head *cu_headerp;
9c6c53f7 25589
96408a79
SA
25590 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25591
25592 return cu_headerp->offset_size;
25593}
25594
25595/* See its dwarf2loc.h declaration. */
25596
25597int
25598dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25599{
25600 struct comp_unit_head cu_header_local;
25601 const struct comp_unit_head *cu_headerp;
25602
25603 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25604
25605 if (cu_headerp->version == 2)
25606 return cu_headerp->addr_size;
25607 else
25608 return cu_headerp->offset_size;
181cebd4
JK
25609}
25610
9aa1f1e3
TT
25611/* Return the text offset of the CU. The returned offset comes from
25612 this CU's objfile. If this objfile came from a separate debuginfo
25613 file, then the offset may be different from the corresponding
25614 offset in the parent objfile. */
25615
25616CORE_ADDR
25617dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25618{
b3b3bada 25619 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
9aa1f1e3
TT
25620}
25621
9a49df9d
AB
25622/* Return a type that is a generic pointer type, the size of which matches
25623 the address size given in the compilation unit header for PER_CU. */
25624static struct type *
25625dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25626{
25627 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25628 struct type *void_type = objfile_type (objfile)->builtin_void;
25629 struct type *addr_type = lookup_pointer_type (void_type);
25630 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25631
25632 if (TYPE_LENGTH (addr_type) == addr_size)
25633 return addr_type;
25634
25635 addr_type
25636 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25637 return addr_type;
25638}
25639
43988095
JK
25640/* Return DWARF version number of PER_CU. */
25641
25642short
25643dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25644{
25645 return per_cu->dwarf_version;
25646}
25647
348e048f
DE
25648/* Locate the .debug_info compilation unit from CU's objfile which contains
25649 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25650
25651static struct dwarf2_per_cu_data *
9c541725 25652dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25653 unsigned int offset_in_dwz,
ed2dc618 25654 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25655{
25656 struct dwarf2_per_cu_data *this_cu;
25657 int low, high;
25658
ae038cb0 25659 low = 0;
b76e467d 25660 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25661 while (high > low)
25662 {
36586728 25663 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25664 int mid = low + (high - low) / 2;
9a619af0 25665
36586728 25666 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25667 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25668 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25669 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25670 high = mid;
25671 else
25672 low = mid + 1;
25673 }
25674 gdb_assert (low == high);
36586728 25675 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25676 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25677 {
36586728 25678 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25679 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25680 "offset %s [in module %s]"),
25681 sect_offset_str (sect_off),
ed2dc618 25682 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25683
9c541725
PA
25684 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25685 <= sect_off);
ae038cb0
DJ
25686 return dwarf2_per_objfile->all_comp_units[low-1];
25687 }
25688 else
25689 {
b76e467d 25690 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25691 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25692 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25693 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25694 return this_cu;
25695 }
25696}
25697
23745b47 25698/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25699
fcd3b13d
SM
25700dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25701 : per_cu (per_cu_),
9068261f
AB
25702 mark (false),
25703 has_loclist (false),
25704 checked_producer (false),
25705 producer_is_gxx_lt_4_6 (false),
25706 producer_is_gcc_lt_4_3 (false),
eb77c9df 25707 producer_is_icc (false),
9068261f 25708 producer_is_icc_lt_14 (false),
c258c396 25709 producer_is_codewarrior (false),
9068261f 25710 processing_has_namespace_info (false)
93311388 25711{
fcd3b13d
SM
25712 per_cu->cu = this;
25713}
25714
25715/* Destroy a dwarf2_cu. */
25716
25717dwarf2_cu::~dwarf2_cu ()
25718{
25719 per_cu->cu = NULL;
9816fde3
JK
25720}
25721
25722/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25723
25724static void
95554aad
TT
25725prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25726 enum language pretend_language)
9816fde3
JK
25727{
25728 struct attribute *attr;
25729
25730 /* Set the language we're debugging. */
25731 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25732 if (attr != nullptr)
9816fde3
JK
25733 set_cu_language (DW_UNSND (attr), cu);
25734 else
9cded63f 25735 {
95554aad 25736 cu->language = pretend_language;
9cded63f
TT
25737 cu->language_defn = language_def (cu->language);
25738 }
dee91e82 25739
7d45c7c3 25740 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25741}
25742
ae038cb0
DJ
25743/* Increase the age counter on each cached compilation unit, and free
25744 any that are too old. */
25745
25746static void
ed2dc618 25747age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25748{
25749 struct dwarf2_per_cu_data *per_cu, **last_chain;
25750
25751 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25752 per_cu = dwarf2_per_objfile->read_in_chain;
25753 while (per_cu != NULL)
25754 {
25755 per_cu->cu->last_used ++;
b4f54984 25756 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25757 dwarf2_mark (per_cu->cu);
25758 per_cu = per_cu->cu->read_in_chain;
25759 }
25760
25761 per_cu = dwarf2_per_objfile->read_in_chain;
25762 last_chain = &dwarf2_per_objfile->read_in_chain;
25763 while (per_cu != NULL)
25764 {
25765 struct dwarf2_per_cu_data *next_cu;
25766
25767 next_cu = per_cu->cu->read_in_chain;
25768
25769 if (!per_cu->cu->mark)
25770 {
fcd3b13d 25771 delete per_cu->cu;
ae038cb0
DJ
25772 *last_chain = next_cu;
25773 }
25774 else
25775 last_chain = &per_cu->cu->read_in_chain;
25776
25777 per_cu = next_cu;
25778 }
25779}
25780
25781/* Remove a single compilation unit from the cache. */
25782
25783static void
dee91e82 25784free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25785{
25786 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25787 struct dwarf2_per_objfile *dwarf2_per_objfile
25788 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25789
25790 per_cu = dwarf2_per_objfile->read_in_chain;
25791 last_chain = &dwarf2_per_objfile->read_in_chain;
25792 while (per_cu != NULL)
25793 {
25794 struct dwarf2_per_cu_data *next_cu;
25795
25796 next_cu = per_cu->cu->read_in_chain;
25797
dee91e82 25798 if (per_cu == target_per_cu)
ae038cb0 25799 {
fcd3b13d 25800 delete per_cu->cu;
dee91e82 25801 per_cu->cu = NULL;
ae038cb0
DJ
25802 *last_chain = next_cu;
25803 break;
25804 }
25805 else
25806 last_chain = &per_cu->cu->read_in_chain;
25807
25808 per_cu = next_cu;
25809 }
25810}
25811
dee91e82
DE
25812/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25813 We store these in a hash table separate from the DIEs, and preserve them
25814 when the DIEs are flushed out of cache.
25815
25816 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25817 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25818 or the type may come from a DWO file. Furthermore, while it's more logical
25819 to use per_cu->section+offset, with Fission the section with the data is in
25820 the DWO file but we don't know that section at the point we need it.
25821 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25822 because we can enter the lookup routine, get_die_type_at_offset, from
25823 outside this file, and thus won't necessarily have PER_CU->cu.
25824 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25825
dee91e82 25826struct dwarf2_per_cu_offset_and_type
1c379e20 25827{
dee91e82 25828 const struct dwarf2_per_cu_data *per_cu;
9c541725 25829 sect_offset sect_off;
1c379e20
DJ
25830 struct type *type;
25831};
25832
dee91e82 25833/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25834
25835static hashval_t
dee91e82 25836per_cu_offset_and_type_hash (const void *item)
1c379e20 25837{
9a3c8263
SM
25838 const struct dwarf2_per_cu_offset_and_type *ofs
25839 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25840
9c541725 25841 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25842}
25843
dee91e82 25844/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25845
25846static int
dee91e82 25847per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25848{
9a3c8263
SM
25849 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25850 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25851 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25852 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25853
dee91e82 25854 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25855 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25856}
25857
25858/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25859 table if necessary. For convenience, return TYPE.
25860
25861 The DIEs reading must have careful ordering to:
85102364 25862 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25863 reading current DIE.
25864 * Not trying to dereference contents of still incompletely read in types
25865 while reading in other DIEs.
25866 * Enable referencing still incompletely read in types just by a pointer to
25867 the type without accessing its fields.
25868
25869 Therefore caller should follow these rules:
25870 * Try to fetch any prerequisite types we may need to build this DIE type
25871 before building the type and calling set_die_type.
e71ec853 25872 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25873 possible before fetching more types to complete the current type.
25874 * Make the type as complete as possible before fetching more types. */
1c379e20 25875
f792889a 25876static struct type *
1c379e20
DJ
25877set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25878{
518817b3
SM
25879 struct dwarf2_per_objfile *dwarf2_per_objfile
25880 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25881 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25882 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25883 struct attribute *attr;
25884 struct dynamic_prop prop;
1c379e20 25885
b4ba55a1
JB
25886 /* For Ada types, make sure that the gnat-specific data is always
25887 initialized (if not already set). There are a few types where
25888 we should not be doing so, because the type-specific area is
25889 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25890 where the type-specific area is used to store the floatformat).
25891 But this is not a problem, because the gnat-specific information
25892 is actually not needed for these types. */
25893 if (need_gnat_info (cu)
25894 && TYPE_CODE (type) != TYPE_CODE_FUNC
25895 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25896 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25897 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25898 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25899 && !HAVE_GNAT_AUX_INFO (type))
25900 INIT_GNAT_SPECIFIC (type);
25901
3f2f83dd
KB
25902 /* Read DW_AT_allocated and set in type. */
25903 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25904 if (attr_form_is_block (attr))
25905 {
9a49df9d
AB
25906 struct type *prop_type
25907 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25908 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25909 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25910 }
25911 else if (attr != NULL)
25912 {
b98664d3 25913 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25914 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25915 sect_offset_str (die->sect_off));
3f2f83dd
KB
25916 }
25917
25918 /* Read DW_AT_associated and set in type. */
25919 attr = dwarf2_attr (die, DW_AT_associated, cu);
25920 if (attr_form_is_block (attr))
25921 {
9a49df9d
AB
25922 struct type *prop_type
25923 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25924 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25925 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25926 }
25927 else if (attr != NULL)
25928 {
b98664d3 25929 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25930 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25931 sect_offset_str (die->sect_off));
3f2f83dd
KB
25932 }
25933
3cdcd0ce
JB
25934 /* Read DW_AT_data_location and set in type. */
25935 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25936 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25937 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25938 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25939
dee91e82 25940 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25941 {
dee91e82
DE
25942 dwarf2_per_objfile->die_type_hash =
25943 htab_create_alloc_ex (127,
25944 per_cu_offset_and_type_hash,
25945 per_cu_offset_and_type_eq,
25946 NULL,
25947 &objfile->objfile_obstack,
25948 hashtab_obstack_allocate,
25949 dummy_obstack_deallocate);
f792889a 25950 }
1c379e20 25951
dee91e82 25952 ofs.per_cu = cu->per_cu;
9c541725 25953 ofs.sect_off = die->sect_off;
1c379e20 25954 ofs.type = type;
dee91e82
DE
25955 slot = (struct dwarf2_per_cu_offset_and_type **)
25956 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25957 if (*slot)
b98664d3 25958 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25959 sect_offset_str (die->sect_off));
8d749320
SM
25960 *slot = XOBNEW (&objfile->objfile_obstack,
25961 struct dwarf2_per_cu_offset_and_type);
1c379e20 25962 **slot = ofs;
f792889a 25963 return type;
1c379e20
DJ
25964}
25965
9c541725 25966/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25967 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25968
25969static struct type *
9c541725 25970get_die_type_at_offset (sect_offset sect_off,
673bfd45 25971 struct dwarf2_per_cu_data *per_cu)
1c379e20 25972{
dee91e82 25973 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25974 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25975
dee91e82 25976 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25977 return NULL;
1c379e20 25978
dee91e82 25979 ofs.per_cu = per_cu;
9c541725 25980 ofs.sect_off = sect_off;
9a3c8263
SM
25981 slot = ((struct dwarf2_per_cu_offset_and_type *)
25982 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25983 if (slot)
25984 return slot->type;
25985 else
25986 return NULL;
25987}
25988
02142a6c 25989/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25990 or return NULL if DIE does not have a saved type. */
25991
25992static struct type *
25993get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25994{
9c541725 25995 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25996}
25997
10b3939b
DJ
25998/* Add a dependence relationship from CU to REF_PER_CU. */
25999
26000static void
26001dwarf2_add_dependence (struct dwarf2_cu *cu,
26002 struct dwarf2_per_cu_data *ref_per_cu)
26003{
26004 void **slot;
26005
26006 if (cu->dependencies == NULL)
26007 cu->dependencies
26008 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26009 NULL, &cu->comp_unit_obstack,
26010 hashtab_obstack_allocate,
26011 dummy_obstack_deallocate);
26012
26013 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26014 if (*slot == NULL)
26015 *slot = ref_per_cu;
26016}
1c379e20 26017
f504f079
DE
26018/* Subroutine of dwarf2_mark to pass to htab_traverse.
26019 Set the mark field in every compilation unit in the
ae038cb0
DJ
26020 cache that we must keep because we are keeping CU. */
26021
10b3939b
DJ
26022static int
26023dwarf2_mark_helper (void **slot, void *data)
26024{
26025 struct dwarf2_per_cu_data *per_cu;
26026
26027 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
26028
26029 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26030 reading of the chain. As such dependencies remain valid it is not much
26031 useful to track and undo them during QUIT cleanups. */
26032 if (per_cu->cu == NULL)
26033 return 1;
26034
10b3939b
DJ
26035 if (per_cu->cu->mark)
26036 return 1;
9068261f 26037 per_cu->cu->mark = true;
10b3939b
DJ
26038
26039 if (per_cu->cu->dependencies != NULL)
26040 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26041
26042 return 1;
26043}
26044
f504f079
DE
26045/* Set the mark field in CU and in every other compilation unit in the
26046 cache that we must keep because we are keeping CU. */
26047
ae038cb0
DJ
26048static void
26049dwarf2_mark (struct dwarf2_cu *cu)
26050{
26051 if (cu->mark)
26052 return;
9068261f 26053 cu->mark = true;
10b3939b
DJ
26054 if (cu->dependencies != NULL)
26055 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
26056}
26057
26058static void
26059dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26060{
26061 while (per_cu)
26062 {
9068261f 26063 per_cu->cu->mark = false;
ae038cb0
DJ
26064 per_cu = per_cu->cu->read_in_chain;
26065 }
72bf9492
DJ
26066}
26067
72bf9492
DJ
26068/* Trivial hash function for partial_die_info: the hash value of a DIE
26069 is its offset in .debug_info for this objfile. */
26070
26071static hashval_t
26072partial_die_hash (const void *item)
26073{
9a3c8263
SM
26074 const struct partial_die_info *part_die
26075 = (const struct partial_die_info *) item;
9a619af0 26076
9c541725 26077 return to_underlying (part_die->sect_off);
72bf9492
DJ
26078}
26079
26080/* Trivial comparison function for partial_die_info structures: two DIEs
26081 are equal if they have the same offset. */
26082
26083static int
26084partial_die_eq (const void *item_lhs, const void *item_rhs)
26085{
9a3c8263
SM
26086 const struct partial_die_info *part_die_lhs
26087 = (const struct partial_die_info *) item_lhs;
26088 const struct partial_die_info *part_die_rhs
26089 = (const struct partial_die_info *) item_rhs;
9a619af0 26090
9c541725 26091 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
26092}
26093
3c3bb058
AB
26094struct cmd_list_element *set_dwarf_cmdlist;
26095struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
26096
26097static void
981a3fb3 26098set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 26099{
b4f54984 26100 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 26101 gdb_stdout);
ae038cb0
DJ
26102}
26103
26104static void
981a3fb3 26105show_dwarf_cmd (const char *args, int from_tty)
6e70227d 26106{
b4f54984 26107 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
26108}
26109
491144b5 26110bool dwarf_always_disassemble;
437afbb8 26111
437afbb8 26112static void
cd4fb1b2
SM
26113show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26114 struct cmd_list_element *c, const char *value)
9291a0cd 26115{
cd4fb1b2
SM
26116 fprintf_filtered (file,
26117 _("Whether to always disassemble "
26118 "DWARF expressions is %s.\n"),
26119 value);
9291a0cd
TT
26120}
26121
9291a0cd 26122static void
cd4fb1b2
SM
26123show_check_physname (struct ui_file *file, int from_tty,
26124 struct cmd_list_element *c, const char *value)
9291a0cd 26125{
cd4fb1b2
SM
26126 fprintf_filtered (file,
26127 _("Whether to check \"physname\" is %s.\n"),
26128 value);
9291a0cd
TT
26129}
26130
6c265988 26131void _initialize_dwarf2_read ();
cd4fb1b2 26132void
6c265988 26133_initialize_dwarf2_read ()
9291a0cd 26134{
cd4fb1b2
SM
26135 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26136Set DWARF specific variables.\n\
590042fc 26137Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
26138 &set_dwarf_cmdlist, "maintenance set dwarf ",
26139 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 26140
cd4fb1b2 26141 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
26142Show DWARF specific variables.\n\
26143Show DWARF variables such as the cache size."),
cd4fb1b2
SM
26144 &show_dwarf_cmdlist, "maintenance show dwarf ",
26145 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 26146
cd4fb1b2
SM
26147 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26148 &dwarf_max_cache_age, _("\
26149Set the upper bound on the age of cached DWARF compilation units."), _("\
26150Show the upper bound on the age of cached DWARF compilation units."), _("\
26151A higher limit means that cached compilation units will be stored\n\
26152in memory longer, and more total memory will be used. Zero disables\n\
26153caching, which can slow down startup."),
26154 NULL,
26155 show_dwarf_max_cache_age,
26156 &set_dwarf_cmdlist,
26157 &show_dwarf_cmdlist);
156942c7 26158
cd4fb1b2
SM
26159 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26160 &dwarf_always_disassemble, _("\
26161Set whether `info address' always disassembles DWARF expressions."), _("\
26162Show whether `info address' always disassembles DWARF expressions."), _("\
26163When enabled, DWARF expressions are always printed in an assembly-like\n\
26164syntax. When disabled, expressions will be printed in a more\n\
26165conversational style, when possible."),
26166 NULL,
26167 show_dwarf_always_disassemble,
26168 &set_dwarf_cmdlist,
26169 &show_dwarf_cmdlist);
9291a0cd 26170
cd4fb1b2
SM
26171 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26172Set debugging of the DWARF reader."), _("\
26173Show debugging of the DWARF reader."), _("\
26174When enabled (non-zero), debugging messages are printed during DWARF\n\
26175reading and symtab expansion. A value of 1 (one) provides basic\n\
26176information. A value greater than 1 provides more verbose information."),
26177 NULL,
26178 NULL,
26179 &setdebuglist, &showdebuglist);
9291a0cd 26180
cd4fb1b2
SM
26181 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26182Set debugging of the DWARF DIE reader."), _("\
26183Show debugging of the DWARF DIE reader."), _("\
26184When enabled (non-zero), DIEs are dumped after they are read in.\n\
26185The value is the maximum depth to print."),
26186 NULL,
26187 NULL,
26188 &setdebuglist, &showdebuglist);
9291a0cd 26189
cd4fb1b2
SM
26190 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26191Set debugging of the dwarf line reader."), _("\
26192Show debugging of the dwarf line reader."), _("\
26193When enabled (non-zero), line number entries are dumped as they are read in.\n\
26194A value of 1 (one) provides basic information.\n\
26195A value greater than 1 provides more verbose information."),
26196 NULL,
26197 NULL,
26198 &setdebuglist, &showdebuglist);
437afbb8 26199
cd4fb1b2
SM
26200 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26201Set cross-checking of \"physname\" code against demangler."), _("\
26202Show cross-checking of \"physname\" code against demangler."), _("\
26203When enabled, GDB's internal \"physname\" code is checked against\n\
26204the demangler."),
26205 NULL, show_check_physname,
26206 &setdebuglist, &showdebuglist);
900e11f9 26207
e615022a
DE
26208 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26209 no_class, &use_deprecated_index_sections, _("\
26210Set whether to use deprecated gdb_index sections."), _("\
26211Show whether to use deprecated gdb_index sections."), _("\
26212When enabled, deprecated .gdb_index sections are used anyway.\n\
26213Normally they are ignored either because of a missing feature or\n\
26214performance issue.\n\
26215Warning: This option must be enabled before gdb reads the file."),
26216 NULL,
26217 NULL,
26218 &setlist, &showlist);
26219
f1e6e072
TT
26220 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26221 &dwarf2_locexpr_funcs);
26222 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26223 &dwarf2_loclist_funcs);
26224
26225 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26226 &dwarf2_block_frame_base_locexpr_funcs);
26227 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26228 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26229
26230#if GDB_SELF_TEST
26231 selftests::register_test ("dw2_expand_symtabs_matching",
26232 selftests::dw2_expand_symtabs_matching::run_test);
26233#endif
6502dd73 26234}