Change allocate_psymtab to be a constructor
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
4de283e4 32#include "dwarf2read.h"
d55e5aa6
TT
33#include "dwarf-index-cache.h"
34#include "dwarf-index-common.h"
4de283e4
TT
35#include "bfd.h"
36#include "elf-bfd.h"
37#include "symtab.h"
38#include "gdbtypes.h"
39#include "objfiles.h"
d55e5aa6 40#include "dwarf2.h"
4de283e4
TT
41#include "buildsym.h"
42#include "demangle.h"
43#include "gdb-demangle.h"
4de283e4
TT
44#include "filenames.h" /* for DOSish file names */
45#include "macrotab.h"
46#include "language.h"
47#include "complaints.h"
d55e5aa6
TT
48#include "dwarf2expr.h"
49#include "dwarf2loc.h"
4de283e4
TT
50#include "cp-support.h"
51#include "hashtab.h"
52#include "command.h"
d55e5aa6 53#include "gdbcmd.h"
4de283e4
TT
54#include "block.h"
55#include "addrmap.h"
56#include "typeprint.h"
57#include "psympriv.h"
4de283e4 58#include "c-lang.h"
d55e5aa6 59#include "go-lang.h"
4de283e4
TT
60#include "valprint.h"
61#include "gdbcore.h" /* for gnutarget */
62#include "gdb/gdb-index.h"
4de283e4
TT
63#include "gdb_bfd.h"
64#include "f-lang.h"
65#include "source.h"
4de283e4 66#include "build-id.h"
d55e5aa6 67#include "namespace.h"
268a13a5
TT
68#include "gdbsupport/function-view.h"
69#include "gdbsupport/gdb_optional.h"
70#include "gdbsupport/underlying.h"
268a13a5 71#include "gdbsupport/hash_enum.h"
4de283e4 72#include "filename-seen-cache.h"
b32b108a 73#include "producer.h"
4de283e4 74#include <fcntl.h>
4de283e4 75#include <algorithm>
4de283e4 76#include <unordered_map>
268a13a5 77#include "gdbsupport/selftest.h"
c9317f21 78#include "rust-lang.h"
268a13a5 79#include "gdbsupport/pathstuff.h"
437afbb8 80
73be47f5
DE
81/* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
b4f54984
DE
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84static unsigned int dwarf_read_debug = 0;
45cfd468 85
d97bc12b 86/* When non-zero, dump DIEs after they are read in. */
b4f54984 87static unsigned int dwarf_die_debug = 0;
d97bc12b 88
27e0867f
DE
89/* When non-zero, dump line number entries as they are read in. */
90static unsigned int dwarf_line_debug = 0;
91
491144b5
CB
92/* When true, cross-check physname against demangler. */
93static bool check_physname = false;
900e11f9 94
491144b5
CB
95/* When true, do not reject deprecated .gdb_index sections. */
96static bool use_deprecated_index_sections = false;
481860b3 97
5bfd760d 98static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 99
f1e6e072
TT
100/* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102static int dwarf2_locexpr_index;
103static int dwarf2_loclist_index;
104static int dwarf2_locexpr_block_index;
105static int dwarf2_loclist_block_index;
106
3f563c84
PA
107/* An index into a (C++) symbol name component in a symbol name as
108 recorded in the mapped_index's symbol table. For each C++ symbol
109 in the symbol table, we record one entry for the start of each
110 component in the symbol in a table of name components, and then
111 sort the table, in order to be able to binary search symbol names,
112 ignoring leading namespaces, both completion and regular look up.
113 For example, for symbol "A::B::C", we'll have an entry that points
114 to "A::B::C", another that points to "B::C", and another for "C".
115 Note that function symbols in GDB index have no parameter
116 information, just the function/method names. You can convert a
117 name_component to a "const char *" using the
118 'mapped_index::symbol_name_at(offset_type)' method. */
119
120struct name_component
121{
122 /* Offset in the symbol name where the component starts. Stored as
123 a (32-bit) offset instead of a pointer to save memory and improve
124 locality on 64-bit architectures. */
125 offset_type name_offset;
126
127 /* The symbol's index in the symbol and constant pool tables of a
128 mapped_index. */
129 offset_type idx;
130};
131
44ed8f3e
PA
132/* Base class containing bits shared by both .gdb_index and
133 .debug_name indexes. */
134
135struct mapped_index_base
136{
22ca247e
TT
137 mapped_index_base () = default;
138 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
139
44ed8f3e
PA
140 /* The name_component table (a sorted vector). See name_component's
141 description above. */
142 std::vector<name_component> name_components;
143
144 /* How NAME_COMPONENTS is sorted. */
145 enum case_sensitivity name_components_casing;
146
147 /* Return the number of names in the symbol table. */
148 virtual size_t symbol_name_count () const = 0;
149
150 /* Get the name of the symbol at IDX in the symbol table. */
151 virtual const char *symbol_name_at (offset_type idx) const = 0;
152
153 /* Return whether the name at IDX in the symbol table should be
154 ignored. */
155 virtual bool symbol_name_slot_invalid (offset_type idx) const
156 {
157 return false;
158 }
159
160 /* Build the symbol name component sorted vector, if we haven't
161 yet. */
162 void build_name_components ();
163
164 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
165 possible matches for LN_NO_PARAMS in the name component
166 vector. */
167 std::pair<std::vector<name_component>::const_iterator,
168 std::vector<name_component>::const_iterator>
3b00ef10
TT
169 find_name_components_bounds (const lookup_name_info &ln_no_params,
170 enum language lang) const;
44ed8f3e
PA
171
172 /* Prevent deleting/destroying via a base class pointer. */
173protected:
174 ~mapped_index_base() = default;
175};
176
9291a0cd
TT
177/* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
fc898b42 179struct mapped_index final : public mapped_index_base
9291a0cd 180{
f00a2de2
PA
181 /* A slot/bucket in the symbol table hash. */
182 struct symbol_table_slot
183 {
184 const offset_type name;
185 const offset_type vec;
186 };
187
559a7a62 188 /* Index data format version. */
3063847f 189 int version = 0;
559a7a62 190
f00a2de2
PA
191 /* The address table data. */
192 gdb::array_view<const gdb_byte> address_table;
b11b1f88 193
3876f04e 194 /* The symbol table, implemented as a hash table. */
f00a2de2 195 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 196
9291a0cd 197 /* A pointer to the constant pool. */
3063847f 198 const char *constant_pool = nullptr;
3f563c84 199
44ed8f3e
PA
200 bool symbol_name_slot_invalid (offset_type idx) const override
201 {
202 const auto &bucket = this->symbol_table[idx];
9ab08412 203 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 204 }
5c58de74 205
3f563c84
PA
206 /* Convenience method to get at the name of the symbol at IDX in the
207 symbol table. */
44ed8f3e 208 const char *symbol_name_at (offset_type idx) const override
f00a2de2 209 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 210
44ed8f3e
PA
211 size_t symbol_name_count () const override
212 { return this->symbol_table.size (); }
9291a0cd
TT
213};
214
927aa2e7
JK
215/* A description of the mapped .debug_names.
216 Uninitialized map has CU_COUNT 0. */
fc898b42 217struct mapped_debug_names final : public mapped_index_base
927aa2e7 218{
ed2dc618
SM
219 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
220 : dwarf2_per_objfile (dwarf2_per_objfile_)
221 {}
222
223 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
224 bfd_endian dwarf5_byte_order;
225 bool dwarf5_is_dwarf64;
226 bool augmentation_is_gdb;
227 uint8_t offset_size;
228 uint32_t cu_count = 0;
229 uint32_t tu_count, bucket_count, name_count;
230 const gdb_byte *cu_table_reordered, *tu_table_reordered;
231 const uint32_t *bucket_table_reordered, *hash_table_reordered;
232 const gdb_byte *name_table_string_offs_reordered;
233 const gdb_byte *name_table_entry_offs_reordered;
234 const gdb_byte *entry_pool;
235
236 struct index_val
237 {
238 ULONGEST dwarf_tag;
239 struct attr
240 {
241 /* Attribute name DW_IDX_*. */
242 ULONGEST dw_idx;
243
244 /* Attribute form DW_FORM_*. */
245 ULONGEST form;
246
247 /* Value if FORM is DW_FORM_implicit_const. */
248 LONGEST implicit_const;
249 };
250 std::vector<attr> attr_vec;
251 };
252
253 std::unordered_map<ULONGEST, index_val> abbrev_map;
254
255 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
256
257 /* Implementation of the mapped_index_base virtual interface, for
258 the name_components cache. */
259
260 const char *symbol_name_at (offset_type idx) const override
261 { return namei_to_name (idx); }
262
263 size_t symbol_name_count () const override
264 { return this->name_count; }
927aa2e7
JK
265};
266
cd4fb1b2 267/* See dwarf2read.h. */
ed2dc618 268
cd4fb1b2 269dwarf2_per_objfile *
ed2dc618
SM
270get_dwarf2_per_objfile (struct objfile *objfile)
271{
5bfd760d 272 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 273}
c906108c 274
251d32d9 275/* Default names of the debugging sections. */
c906108c 276
233a11ab
CS
277/* Note that if the debugging section has been compressed, it might
278 have a name like .zdebug_info. */
279
9cdd5dbd
DE
280static const struct dwarf2_debug_sections dwarf2_elf_names =
281{
251d32d9
TG
282 { ".debug_info", ".zdebug_info" },
283 { ".debug_abbrev", ".zdebug_abbrev" },
284 { ".debug_line", ".zdebug_line" },
285 { ".debug_loc", ".zdebug_loc" },
43988095 286 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 287 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 288 { ".debug_macro", ".zdebug_macro" },
251d32d9 289 { ".debug_str", ".zdebug_str" },
18a8505e 290 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 291 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 292 { ".debug_ranges", ".zdebug_ranges" },
43988095 293 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 294 { ".debug_types", ".zdebug_types" },
3019eac3 295 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
296 { ".debug_frame", ".zdebug_frame" },
297 { ".eh_frame", NULL },
24d3216f 298 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
299 { ".debug_names", ".zdebug_names" },
300 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 301 23
251d32d9 302};
c906108c 303
80626a55 304/* List of DWO/DWP sections. */
3019eac3 305
80626a55 306static const struct dwop_section_names
3019eac3
DE
307{
308 struct dwarf2_section_names abbrev_dwo;
309 struct dwarf2_section_names info_dwo;
310 struct dwarf2_section_names line_dwo;
311 struct dwarf2_section_names loc_dwo;
43988095 312 struct dwarf2_section_names loclists_dwo;
09262596
DE
313 struct dwarf2_section_names macinfo_dwo;
314 struct dwarf2_section_names macro_dwo;
3019eac3
DE
315 struct dwarf2_section_names str_dwo;
316 struct dwarf2_section_names str_offsets_dwo;
317 struct dwarf2_section_names types_dwo;
80626a55
DE
318 struct dwarf2_section_names cu_index;
319 struct dwarf2_section_names tu_index;
3019eac3 320}
80626a55 321dwop_section_names =
3019eac3
DE
322{
323 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
324 { ".debug_info.dwo", ".zdebug_info.dwo" },
325 { ".debug_line.dwo", ".zdebug_line.dwo" },
326 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 327 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
335};
336
c906108c
SS
337/* local data types */
338
107d2387
AC
339/* The data in a compilation unit header, after target2host
340 translation, looks like this. */
c906108c 341struct comp_unit_head
a738430d 342{
c764a876 343 unsigned int length;
a738430d 344 short version;
a738430d
MK
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
9c541725 347 sect_offset abbrev_sect_off;
57349743 348
a738430d
MK
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
57349743 351
a738430d
MK
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
57349743 354
43988095
JK
355 enum dwarf_unit_type unit_type;
356
a738430d
MK
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
9c541725 359 sect_offset sect_off;
57349743 360
d00adf39
DE
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
9c541725 363 cu_offset first_die_cu_offset;
43988095 364
a084a2a6
AT
365
366 /* 64-bit signature of this unit. For type units, it denotes the signature of
367 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
368 Also used in DWARF 5, to denote the dwo id when the unit type is
369 DW_UT_skeleton or DW_UT_split_compile. */
43988095
JK
370 ULONGEST signature;
371
372 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 373 cu_offset type_cu_offset_in_tu;
a738430d 374};
c906108c 375
3da10d80
KS
376/* Type used for delaying computation of method physnames.
377 See comments for compute_delayed_physnames. */
378struct delayed_method_info
379{
380 /* The type to which the method is attached, i.e., its parent class. */
381 struct type *type;
382
383 /* The index of the method in the type's function fieldlists. */
384 int fnfield_index;
385
386 /* The index of the method in the fieldlist. */
387 int index;
388
389 /* The name of the DIE. */
390 const char *name;
391
392 /* The DIE associated with this method. */
393 struct die_info *die;
394};
395
e7c27a73
DJ
396/* Internal state when decoding a particular compilation unit. */
397struct dwarf2_cu
398{
fcd3b13d
SM
399 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
400 ~dwarf2_cu ();
401
402 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
403
c24bdb02
KS
404 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
405 Create the set of symtabs used by this TU, or if this TU is sharing
406 symtabs with another TU and the symtabs have already been created
407 then restore those symtabs in the line header.
408 We don't need the pc/line-number mapping for type units. */
409 void setup_type_unit_groups (struct die_info *die);
410
411 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
412 buildsym_compunit constructor. */
413 struct compunit_symtab *start_symtab (const char *name,
414 const char *comp_dir,
415 CORE_ADDR low_pc);
416
417 /* Reset the builder. */
418 void reset_builder () { m_builder.reset (); }
419
d00adf39 420 /* The header of the compilation unit. */
fcd3b13d 421 struct comp_unit_head header {};
e142c38c 422
d00adf39 423 /* Base address of this compilation unit. */
fcd3b13d 424 CORE_ADDR base_address = 0;
d00adf39
DE
425
426 /* Non-zero if base_address has been set. */
fcd3b13d 427 int base_known = 0;
d00adf39 428
e142c38c 429 /* The language we are debugging. */
fcd3b13d
SM
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
e142c38c 432
fcd3b13d 433 const char *producer = nullptr;
b0f35d58 434
c24bdb02 435private:
804d2729
TT
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
c24bdb02 438 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 439
c24bdb02 440public:
e142c38c
DJ
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
fcd3b13d 450 struct pending **list_in_scope = nullptr;
e142c38c 451
b64f50a1
JK
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 454 htab_t partial_dies = nullptr;
72bf9492
DJ
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
fcd3b13d 458 auto_obstack comp_unit_obstack;
72bf9492 459
ae038cb0
DJ
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
fcd3b13d 464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 465
69d751e3 466 /* Backlink to our per_cu entry. */
ae038cb0
DJ
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 470 int last_used = 0;
ae038cb0 471
b64f50a1
JK
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
fcd3b13d 474 htab_t die_hash = nullptr;
10b3939b
DJ
475
476 /* Full DIEs if read in. */
fcd3b13d 477 struct die_info *dies = nullptr;
10b3939b
DJ
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
fcd3b13d 482 htab_t dependencies = nullptr;
10b3939b 483
cb1df416 484 /* Header data from the line table, during full symbol processing. */
fcd3b13d 485 struct line_header *line_header = nullptr;
4c8aa72d
PA
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
fcd3b13d 491 die_info *line_header_die_owner = nullptr;
cb1df416 492
3da10d80
KS
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
c89b44cd 495 std::vector<delayed_method_info> method_list;
3da10d80 496
96408a79 497 /* To be copied to symtab->call_site_htab. */
fcd3b13d 498 htab_t call_site_htab = nullptr;
96408a79 499
034e5797
DE
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
18a8505e 504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
fcd3b13d 509 struct dwo_unit *dwo_unit = nullptr;
3019eac3 510
18a8505e 511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 513 gdb::optional<ULONGEST> addr_base;
3019eac3 514
18a8505e 515 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 516 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 517 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 524 ULONGEST ranges_base = 0;
2e3cf129 525
c9317f21
TT
526 /* When reading debug info generated by older versions of rustc, we
527 have to rewrite some union types to be struct types with a
528 variant part. This rewriting must be done after the CU is fully
529 read in, because otherwise at the point of rewriting some struct
530 type might not have been fully processed. So, we keep a list of
531 all such types here and process them after expansion. */
532 std::vector<struct type *> rust_unions;
533
18a8505e
AT
534 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
535 files, the value is implicitly zero. For DWARF 5 version DWO files, the
536 value is often implicit and is the size of the header of
537 .debug_str_offsets section (8 or 4, depending on the address size). */
538 gdb::optional<ULONGEST> str_offsets_base;
539
ae038cb0 540 /* Mark used when releasing cached dies. */
9068261f 541 bool mark : 1;
ae038cb0 542
8be455d7
JK
543 /* This CU references .debug_loc. See the symtab->locations_valid field.
544 This test is imperfect as there may exist optimized debug code not using
545 any location list and still facing inlining issues if handled as
546 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 547 bool has_loclist : 1;
ba919b58 548
9068261f 549 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
550 if all the producer_is_* fields are valid. This information is cached
551 because profiling CU expansion showed excessive time spent in
552 producer_is_gxx_lt_4_6. */
9068261f
AB
553 bool checked_producer : 1;
554 bool producer_is_gxx_lt_4_6 : 1;
555 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 556 bool producer_is_icc : 1;
9068261f 557 bool producer_is_icc_lt_14 : 1;
c258c396 558 bool producer_is_codewarrior : 1;
4d4ec4e5 559
9068261f 560 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
561 debugging info for C++ namespaces. GCC 3.3.x did not produce
562 this information, but later versions do. */
563
9068261f 564 bool processing_has_namespace_info : 1;
d590ff25
YQ
565
566 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
567
568 /* If this CU was inherited by another CU (via specification,
569 abstract_origin, etc), this is the ancestor CU. */
570 dwarf2_cu *ancestor;
571
572 /* Get the buildsym_compunit for this CU. */
573 buildsym_compunit *get_builder ()
574 {
575 /* If this CU has a builder associated with it, use that. */
576 if (m_builder != nullptr)
577 return m_builder.get ();
578
579 /* Otherwise, search ancestors for a valid builder. */
580 if (ancestor != nullptr)
581 return ancestor->get_builder ();
582
583 return nullptr;
584 }
e7c27a73
DJ
585};
586
094b34ac
DE
587/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
588 This includes type_unit_group and quick_file_names. */
589
590struct stmt_list_hash
591{
592 /* The DWO unit this table is from or NULL if there is none. */
593 struct dwo_unit *dwo_unit;
594
595 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 596 sect_offset line_sect_off;
094b34ac
DE
597};
598
f4dc4d17
DE
599/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
600 an object of this type. */
601
602struct type_unit_group
603{
0186c6a7 604 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
605 To simplify things we create an artificial CU that "includes" all the
606 type units using this stmt_list so that the rest of the code still has
607 a "per_cu" handle on the symtab.
608 This PER_CU is recognized by having no section. */
8a0459fd 609#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
610 struct dwarf2_per_cu_data per_cu;
611
0186c6a7
DE
612 /* The TUs that share this DW_AT_stmt_list entry.
613 This is added to while parsing type units to build partial symtabs,
614 and is deleted afterwards and not used again. */
a8b3b8e9 615 std::vector<signatured_type *> *tus;
f4dc4d17 616
43f3e411 617 /* The compunit symtab.
094b34ac 618 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
619 so we create an essentially anonymous symtab as the compunit symtab. */
620 struct compunit_symtab *compunit_symtab;
f4dc4d17 621
094b34ac
DE
622 /* The data used to construct the hash key. */
623 struct stmt_list_hash hash;
f4dc4d17
DE
624
625 /* The number of symtabs from the line header.
626 The value here must match line_header.num_file_names. */
627 unsigned int num_symtabs;
628
629 /* The symbol tables for this TU (obtained from the files listed in
630 DW_AT_stmt_list).
631 WARNING: The order of entries here must match the order of entries
632 in the line header. After the first TU using this type_unit_group, the
633 line header for the subsequent TUs is recreated from this. This is done
634 because we need to use the same symtabs for each TU using the same
635 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
636 there's no guarantee the line header doesn't have duplicate entries. */
637 struct symtab **symtabs;
638};
639
73869dc2 640/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
641
642struct dwo_sections
643{
644 struct dwarf2_section_info abbrev;
3019eac3
DE
645 struct dwarf2_section_info line;
646 struct dwarf2_section_info loc;
43988095 647 struct dwarf2_section_info loclists;
09262596
DE
648 struct dwarf2_section_info macinfo;
649 struct dwarf2_section_info macro;
3019eac3
DE
650 struct dwarf2_section_info str;
651 struct dwarf2_section_info str_offsets;
80626a55
DE
652 /* In the case of a virtual DWO file, these two are unused. */
653 struct dwarf2_section_info info;
fd5866f6 654 std::vector<dwarf2_section_info> types;
3019eac3
DE
655};
656
c88ee1f0 657/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
658
659struct dwo_unit
660{
661 /* Backlink to the containing struct dwo_file. */
662 struct dwo_file *dwo_file;
663
664 /* The "id" that distinguishes this CU/TU.
665 .debug_info calls this "dwo_id", .debug_types calls this "signature".
666 Since signatures came first, we stick with it for consistency. */
667 ULONGEST signature;
668
669 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 670 struct dwarf2_section_info *section;
3019eac3 671
9c541725
PA
672 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
673 sect_offset sect_off;
3019eac3
DE
674 unsigned int length;
675
676 /* For types, offset in the type's DIE of the type defined by this TU. */
677 cu_offset type_offset_in_tu;
678};
679
73869dc2
DE
680/* include/dwarf2.h defines the DWP section codes.
681 It defines a max value but it doesn't define a min value, which we
682 use for error checking, so provide one. */
683
684enum dwp_v2_section_ids
685{
686 DW_SECT_MIN = 1
687};
688
80626a55 689/* Data for one DWO file.
57d63ce2
DE
690
691 This includes virtual DWO files (a virtual DWO file is a DWO file as it
692 appears in a DWP file). DWP files don't really have DWO files per se -
693 comdat folding of types "loses" the DWO file they came from, and from
694 a high level view DWP files appear to contain a mass of random types.
695 However, to maintain consistency with the non-DWP case we pretend DWP
696 files contain virtual DWO files, and we assign each TU with one virtual
697 DWO file (generally based on the line and abbrev section offsets -
698 a heuristic that seems to work in practice). */
3019eac3
DE
699
700struct dwo_file
701{
51ac9db5
SM
702 dwo_file () = default;
703 DISABLE_COPY_AND_ASSIGN (dwo_file);
704
18a8505e 705 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
706 For virtual DWO files the name is constructed from the section offsets
707 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
708 from related CU+TUs. */
51ac9db5 709 const char *dwo_name = nullptr;
0ac5b59e
DE
710
711 /* The DW_AT_comp_dir attribute. */
51ac9db5 712 const char *comp_dir = nullptr;
3019eac3 713
80626a55
DE
714 /* The bfd, when the file is open. Otherwise this is NULL.
715 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 716 gdb_bfd_ref_ptr dbfd;
3019eac3 717
73869dc2
DE
718 /* The sections that make up this DWO file.
719 Remember that for virtual DWO files in DWP V2, these are virtual
720 sections (for lack of a better name). */
51ac9db5 721 struct dwo_sections sections {};
3019eac3 722
33c5cd75
DB
723 /* The CUs in the file.
724 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
725 an extension to handle LLVM's Link Time Optimization output (where
726 multiple source files may be compiled into a single object/dwo pair). */
51ac9db5 727 htab_t cus {};
3019eac3
DE
728
729 /* Table of TUs in the file.
730 Each element is a struct dwo_unit. */
51ac9db5 731 htab_t tus {};
3019eac3
DE
732};
733
80626a55
DE
734/* These sections are what may appear in a DWP file. */
735
736struct dwp_sections
737{
73869dc2 738 /* These are used by both DWP version 1 and 2. */
80626a55
DE
739 struct dwarf2_section_info str;
740 struct dwarf2_section_info cu_index;
741 struct dwarf2_section_info tu_index;
73869dc2
DE
742
743 /* These are only used by DWP version 2 files.
744 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
745 sections are referenced by section number, and are not recorded here.
746 In DWP version 2 there is at most one copy of all these sections, each
747 section being (effectively) comprised of the concatenation of all of the
748 individual sections that exist in the version 1 format.
749 To keep the code simple we treat each of these concatenated pieces as a
750 section itself (a virtual section?). */
751 struct dwarf2_section_info abbrev;
752 struct dwarf2_section_info info;
753 struct dwarf2_section_info line;
754 struct dwarf2_section_info loc;
755 struct dwarf2_section_info macinfo;
756 struct dwarf2_section_info macro;
757 struct dwarf2_section_info str_offsets;
758 struct dwarf2_section_info types;
80626a55
DE
759};
760
73869dc2
DE
761/* These sections are what may appear in a virtual DWO file in DWP version 1.
762 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 763
73869dc2 764struct virtual_v1_dwo_sections
80626a55
DE
765{
766 struct dwarf2_section_info abbrev;
767 struct dwarf2_section_info line;
768 struct dwarf2_section_info loc;
769 struct dwarf2_section_info macinfo;
770 struct dwarf2_section_info macro;
771 struct dwarf2_section_info str_offsets;
772 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 773 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
774 struct dwarf2_section_info info_or_types;
775};
776
73869dc2
DE
777/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
778 In version 2, the sections of the DWO files are concatenated together
779 and stored in one section of that name. Thus each ELF section contains
780 several "virtual" sections. */
781
782struct virtual_v2_dwo_sections
783{
784 bfd_size_type abbrev_offset;
785 bfd_size_type abbrev_size;
786
787 bfd_size_type line_offset;
788 bfd_size_type line_size;
789
790 bfd_size_type loc_offset;
791 bfd_size_type loc_size;
792
793 bfd_size_type macinfo_offset;
794 bfd_size_type macinfo_size;
795
796 bfd_size_type macro_offset;
797 bfd_size_type macro_size;
798
799 bfd_size_type str_offsets_offset;
800 bfd_size_type str_offsets_size;
801
802 /* Each DWP hash table entry records one CU or one TU.
803 That is recorded here, and copied to dwo_unit.section. */
804 bfd_size_type info_or_types_offset;
805 bfd_size_type info_or_types_size;
806};
807
80626a55
DE
808/* Contents of DWP hash tables. */
809
810struct dwp_hash_table
811{
73869dc2 812 uint32_t version, nr_columns;
80626a55 813 uint32_t nr_units, nr_slots;
73869dc2
DE
814 const gdb_byte *hash_table, *unit_table;
815 union
816 {
817 struct
818 {
819 const gdb_byte *indices;
820 } v1;
821 struct
822 {
823 /* This is indexed by column number and gives the id of the section
824 in that column. */
825#define MAX_NR_V2_DWO_SECTIONS \
826 (1 /* .debug_info or .debug_types */ \
827 + 1 /* .debug_abbrev */ \
828 + 1 /* .debug_line */ \
829 + 1 /* .debug_loc */ \
830 + 1 /* .debug_str_offsets */ \
831 + 1 /* .debug_macro or .debug_macinfo */)
832 int section_ids[MAX_NR_V2_DWO_SECTIONS];
833 const gdb_byte *offsets;
834 const gdb_byte *sizes;
835 } v2;
836 } section_pool;
80626a55
DE
837};
838
839/* Data for one DWP file. */
840
841struct dwp_file
842{
400174b1
TT
843 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
844 : name (name_),
845 dbfd (std::move (abfd))
846 {
847 }
848
80626a55
DE
849 /* Name of the file. */
850 const char *name;
851
73869dc2 852 /* File format version. */
400174b1 853 int version = 0;
73869dc2 854
93417882 855 /* The bfd. */
400174b1 856 gdb_bfd_ref_ptr dbfd;
80626a55
DE
857
858 /* Section info for this file. */
400174b1 859 struct dwp_sections sections {};
80626a55 860
57d63ce2 861 /* Table of CUs in the file. */
400174b1 862 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
863
864 /* Table of TUs in the file. */
400174b1 865 const struct dwp_hash_table *tus = nullptr;
80626a55 866
19ac8c2e 867 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
868 htab_t loaded_cus {};
869 htab_t loaded_tus {};
80626a55 870
73869dc2
DE
871 /* Table to map ELF section numbers to their sections.
872 This is only needed for the DWP V1 file format. */
400174b1
TT
873 unsigned int num_sections = 0;
874 asection **elf_sections = nullptr;
80626a55
DE
875};
876
c0ab21c2
TT
877struct abbrev_table;
878typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
879
0963b4bd
MS
880/* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
dee91e82 883 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
884
885struct die_reader_specs
886{
a32a8923 887 /* The bfd of die_section. */
93311388
DE
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
80626a55 893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
894 struct dwo_file *dwo_file;
895
dee91e82 896 /* The section the die comes from.
3019eac3 897 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
d521ce57 901 const gdb_byte *buffer;
f664829e
DE
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
a2ce51a0
DE
905
906 /* The value of the DW_AT_comp_dir attribute. */
907 const char *comp_dir;
685af9cd
TT
908
909 /* The abbreviation table to use when reading the DIEs. */
910 struct abbrev_table *abbrev_table;
93311388
DE
911};
912
c0ab21c2
TT
913/* A subclass of die_reader_specs that holds storage and has complex
914 constructor and destructor behavior. */
915
916class cutu_reader : public die_reader_specs
917{
918public:
919
920 cutu_reader (struct dwarf2_per_cu_data *this_cu,
921 struct abbrev_table *abbrev_table,
922 int use_existing_cu, int keep,
923 bool skip_partial);
924
925 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
926 struct dwarf2_cu *parent_cu = nullptr,
927 struct dwo_file *dwo_file = nullptr);
928
929 ~cutu_reader ();
930
931 DISABLE_COPY_AND_ASSIGN (cutu_reader);
932
933 const gdb_byte *info_ptr = nullptr;
934 struct die_info *comp_unit_die = nullptr;
935 int has_children = 0;
936 bool dummy_p = false;
937
938private:
939 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
940 int use_existing_cu, int keep);
941
942 struct dwarf2_per_cu_data *m_this_cu;
943 int m_keep = 0;
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951};
dee91e82 952
7ba99d21
AT
953/* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
954 later. */
955typedef int dir_index;
ecfb656c 956
7ba99d21
AT
957/* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
958 and later. */
959typedef int file_name_index;
ecfb656c 960
52059ffd
TT
961struct file_entry
962{
fff8551c
PA
963 file_entry () = default;
964
ecfb656c 965 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
966 unsigned int mod_time_, unsigned int length_)
967 : name (name_),
ecfb656c 968 d_index (d_index_),
fff8551c
PA
969 mod_time (mod_time_),
970 length (length_)
971 {}
972
ecfb656c
PA
973 /* Return the include directory at D_INDEX stored in LH. Returns
974 NULL if D_INDEX is out of bounds. */
8c43009f
PA
975 const char *include_dir (const line_header *lh) const;
976
fff8551c
PA
977 /* The file name. Note this is an observing pointer. The memory is
978 owned by debug_line_buffer. */
979 const char *name {};
980
8c43009f 981 /* The directory index (1-based). */
ecfb656c 982 dir_index d_index {};
fff8551c
PA
983
984 unsigned int mod_time {};
985
986 unsigned int length {};
987
988 /* True if referenced by the Line Number Program. */
989 bool included_p {};
990
83769d0b 991 /* The associated symbol table, if any. */
fff8551c 992 struct symtab *symtab {};
52059ffd
TT
993};
994
debd256d
JB
995/* The line number information for a compilation unit (found in the
996 .debug_line section) begins with a "statement program header",
997 which contains the following information. */
998struct line_header
999{
fff8551c
PA
1000 line_header ()
1001 : offset_in_dwz {}
1002 {}
1003
1004 /* Add an entry to the include directory table. */
1005 void add_include_dir (const char *include_dir);
1006
1007 /* Add an entry to the file name table. */
ecfb656c 1008 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1009 unsigned int mod_time, unsigned int length);
1010
7ba99d21
AT
1011 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1012 Returns NULL if INDEX is out of bounds. */
ecfb656c 1013 const char *include_dir_at (dir_index index) const
8c43009f 1014 {
7ba99d21
AT
1015 int vec_index;
1016 if (version >= 5)
1017 vec_index = index;
1018 else
1019 vec_index = index - 1;
1020 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
8c43009f 1021 return NULL;
7ba99d21 1022 return m_include_dirs[vec_index];
8c43009f
PA
1023 }
1024
7ba99d21 1025 bool is_valid_file_index (int file_index)
8c43009f 1026 {
7ba99d21
AT
1027 if (version >= 5)
1028 return 0 <= file_index && file_index < file_names_size ();
1029 return 1 <= file_index && file_index <= file_names_size ();
1030 }
ecfb656c 1031
7ba99d21
AT
1032 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1033 Returns NULL if INDEX is out of bounds. */
1034 file_entry *file_name_at (file_name_index index)
1035 {
1036 int vec_index;
1037 if (version >= 5)
1038 vec_index = index;
1039 else
1040 vec_index = index - 1;
1041 if (vec_index < 0 || vec_index >= m_file_names.size ())
fff8551c 1042 return NULL;
7ba99d21 1043 return &m_file_names[vec_index];
fff8551c
PA
1044 }
1045
7ba99d21
AT
1046 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1047 this method should only be used to iterate through all file entries in an
1048 index-agnostic manner. */
1049 std::vector<file_entry> &file_names ()
1050 { return m_file_names; }
1051
527f3840 1052 /* Offset of line number information in .debug_line section. */
9c541725 1053 sect_offset sect_off {};
527f3840
JK
1054
1055 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1056 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1057
1058 unsigned int total_length {};
1059 unsigned short version {};
1060 unsigned int header_length {};
1061 unsigned char minimum_instruction_length {};
1062 unsigned char maximum_ops_per_instruction {};
1063 unsigned char default_is_stmt {};
1064 int line_base {};
1065 unsigned char line_range {};
1066 unsigned char opcode_base {};
debd256d
JB
1067
1068 /* standard_opcode_lengths[i] is the number of operands for the
1069 standard opcode whose value is i. This means that
1070 standard_opcode_lengths[0] is unused, and the last meaningful
1071 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1072 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1073
7ba99d21
AT
1074 int file_names_size ()
1075 { return m_file_names.size(); }
debd256d
JB
1076
1077 /* The start and end of the statement program following this
6502dd73 1078 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1079 const gdb_byte *statement_program_start {}, *statement_program_end {};
7ba99d21
AT
1080
1081 private:
1082 /* The include_directories table. Note these are observing
1083 pointers. The memory is owned by debug_line_buffer. */
1084 std::vector<const char *> m_include_dirs;
1085
1086 /* The file_names table. This is private because the meaning of indexes
1087 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1088 before, and is 0 in DWARF 5 and later). So the client should use
1089 file_name_at method for access. */
1090 std::vector<file_entry> m_file_names;
debd256d 1091};
c906108c 1092
fff8551c
PA
1093typedef std::unique_ptr<line_header> line_header_up;
1094
8c43009f
PA
1095const char *
1096file_entry::include_dir (const line_header *lh) const
1097{
ecfb656c 1098 return lh->include_dir_at (d_index);
8c43009f
PA
1099}
1100
c906108c 1101/* When we construct a partial symbol table entry we only
0963b4bd 1102 need this much information. */
6f06d47b 1103struct partial_die_info : public allocate_on_obstack
c906108c 1104 {
6f06d47b
YQ
1105 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1106
1107 /* Disable assign but still keep copy ctor, which is needed
1108 load_partial_dies. */
1109 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1110
52356b79
YQ
1111 /* Adjust the partial die before generating a symbol for it. This
1112 function may set the is_external flag or change the DIE's
1113 name. */
1114 void fixup (struct dwarf2_cu *cu);
1115
48fbe735
YQ
1116 /* Read a minimal amount of information into the minimal die
1117 structure. */
1118 const gdb_byte *read (const struct die_reader_specs *reader,
1119 const struct abbrev_info &abbrev,
1120 const gdb_byte *info_ptr);
1121
72bf9492 1122 /* Offset of this DIE. */
6f06d47b 1123 const sect_offset sect_off;
72bf9492
DJ
1124
1125 /* DWARF-2 tag for this DIE. */
6f06d47b 1126 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1127
72bf9492 1128 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1129 const unsigned int has_children : 1;
1130
72bf9492
DJ
1131 unsigned int is_external : 1;
1132 unsigned int is_declaration : 1;
1133 unsigned int has_type : 1;
1134 unsigned int has_specification : 1;
1135 unsigned int has_pc_info : 1;
481860b3 1136 unsigned int may_be_inlined : 1;
72bf9492 1137
0c1b455e
TT
1138 /* This DIE has been marked DW_AT_main_subprogram. */
1139 unsigned int main_subprogram : 1;
1140
72bf9492
DJ
1141 /* Flag set if the SCOPE field of this structure has been
1142 computed. */
1143 unsigned int scope_set : 1;
1144
fa4028e9
JB
1145 /* Flag set if the DIE has a byte_size attribute. */
1146 unsigned int has_byte_size : 1;
1147
ff908ebf
AW
1148 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1149 unsigned int has_const_value : 1;
1150
98bfdba5
PA
1151 /* Flag set if any of the DIE's children are template arguments. */
1152 unsigned int has_template_arguments : 1;
1153
52356b79 1154 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1155 unsigned int fixup_called : 1;
1156
36586728
TT
1157 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1158 unsigned int is_dwz : 1;
1159
1160 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1161 unsigned int spec_is_dwz : 1;
1162
72bf9492 1163 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1164 sometimes a default name for unnamed DIEs. */
6f06d47b 1165 const char *name = nullptr;
72bf9492 1166
abc72ce4 1167 /* The linkage name, if present. */
6f06d47b 1168 const char *linkage_name = nullptr;
abc72ce4 1169
72bf9492
DJ
1170 /* The scope to prepend to our children. This is generally
1171 allocated on the comp_unit_obstack, so will disappear
1172 when this compilation unit leaves the cache. */
6f06d47b 1173 const char *scope = nullptr;
72bf9492 1174
95554aad
TT
1175 /* Some data associated with the partial DIE. The tag determines
1176 which field is live. */
1177 union
1178 {
1179 /* The location description associated with this DIE, if any. */
1180 struct dwarf_block *locdesc;
1181 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1182 sect_offset sect_off;
6f06d47b 1183 } d {};
72bf9492
DJ
1184
1185 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1186 CORE_ADDR lowpc = 0;
1187 CORE_ADDR highpc = 0;
72bf9492 1188
93311388 1189 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1190 DW_AT_sibling, if any. */
48fbe735
YQ
1191 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1192 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1193 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1194
1195 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1196 DW_AT_specification (or DW_AT_abstract_origin or
1197 DW_AT_extension). */
6f06d47b 1198 sect_offset spec_offset {};
72bf9492
DJ
1199
1200 /* Pointers to this DIE's parent, first child, and next sibling,
1201 if any. */
6f06d47b
YQ
1202 struct partial_die_info *die_parent = nullptr;
1203 struct partial_die_info *die_child = nullptr;
1204 struct partial_die_info *die_sibling = nullptr;
1205
1206 friend struct partial_die_info *
1207 dwarf2_cu::find_partial_die (sect_offset sect_off);
1208
1209 private:
1210 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1211 partial_die_info (sect_offset sect_off)
1212 : partial_die_info (sect_off, DW_TAG_padding, 0)
1213 {
1214 }
1215
1216 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1217 int has_children_)
1218 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1219 {
1220 is_external = 0;
1221 is_declaration = 0;
1222 has_type = 0;
1223 has_specification = 0;
1224 has_pc_info = 0;
1225 may_be_inlined = 0;
1226 main_subprogram = 0;
1227 scope_set = 0;
1228 has_byte_size = 0;
1229 has_const_value = 0;
1230 has_template_arguments = 0;
1231 fixup_called = 0;
1232 is_dwz = 0;
1233 spec_is_dwz = 0;
1234 }
c906108c
SS
1235 };
1236
0963b4bd 1237/* This data structure holds the information of an abbrev. */
c906108c
SS
1238struct abbrev_info
1239 {
1240 unsigned int number; /* number identifying abbrev */
1241 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1242 unsigned short has_children; /* boolean */
1243 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1244 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1245 struct abbrev_info *next; /* next in chain */
1246 };
1247
1248struct attr_abbrev
1249 {
9d25dd43
DE
1250 ENUM_BITFIELD(dwarf_attribute) name : 16;
1251 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1252
1253 /* It is valid only if FORM is DW_FORM_implicit_const. */
1254 LONGEST implicit_const;
c906108c
SS
1255 };
1256
433df2d4
DE
1257/* Size of abbrev_table.abbrev_hash_table. */
1258#define ABBREV_HASH_SIZE 121
1259
1260/* Top level data structure to contain an abbreviation table. */
1261
1262struct abbrev_table
1263{
685af9cd
TT
1264 explicit abbrev_table (sect_offset off)
1265 : sect_off (off)
1266 {
4a17f768 1267 m_abbrevs =
685af9cd 1268 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1269 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1270 }
1271
1272 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1273
1274 /* Allocate space for a struct abbrev_info object in
1275 ABBREV_TABLE. */
1276 struct abbrev_info *alloc_abbrev ();
1277
1278 /* Add an abbreviation to the table. */
1279 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1280
1281 /* Look up an abbrev in the table.
1282 Returns NULL if the abbrev is not found. */
1283
1284 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1285
1286
f4dc4d17
DE
1287 /* Where the abbrev table came from.
1288 This is used as a sanity check when the table is used. */
685af9cd 1289 const sect_offset sect_off;
433df2d4
DE
1290
1291 /* Storage for the abbrev table. */
685af9cd 1292 auto_obstack abbrev_obstack;
433df2d4 1293
4a17f768
YQ
1294private:
1295
433df2d4
DE
1296 /* Hash table of abbrevs.
1297 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1298 It could be statically allocated, but the previous code didn't so we
1299 don't either. */
4a17f768 1300 struct abbrev_info **m_abbrevs;
433df2d4
DE
1301};
1302
0963b4bd 1303/* Attributes have a name and a value. */
b60c80d6
DJ
1304struct attribute
1305 {
9d25dd43 1306 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1307 ENUM_BITFIELD(dwarf_form) form : 15;
1308
1309 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1310 field should be in u.str (existing only for DW_STRING) but it is kept
1311 here for better struct attribute alignment. */
1312 unsigned int string_is_canonical : 1;
1313
b60c80d6
DJ
1314 union
1315 {
15d034d0 1316 const char *str;
b60c80d6 1317 struct dwarf_block *blk;
43bbcdc2
PH
1318 ULONGEST unsnd;
1319 LONGEST snd;
b60c80d6 1320 CORE_ADDR addr;
ac9ec31b 1321 ULONGEST signature;
b60c80d6
DJ
1322 }
1323 u;
1324 };
1325
0963b4bd 1326/* This data structure holds a complete die structure. */
c906108c
SS
1327struct die_info
1328 {
76815b17
DE
1329 /* DWARF-2 tag for this DIE. */
1330 ENUM_BITFIELD(dwarf_tag) tag : 16;
1331
1332 /* Number of attributes */
98bfdba5
PA
1333 unsigned char num_attrs;
1334
1335 /* True if we're presently building the full type name for the
1336 type derived from this DIE. */
1337 unsigned char building_fullname : 1;
76815b17 1338
adde2bff
DE
1339 /* True if this die is in process. PR 16581. */
1340 unsigned char in_process : 1;
1341
76815b17
DE
1342 /* Abbrev number */
1343 unsigned int abbrev;
1344
93311388 1345 /* Offset in .debug_info or .debug_types section. */
9c541725 1346 sect_offset sect_off;
78ba4af6
JB
1347
1348 /* The dies in a compilation unit form an n-ary tree. PARENT
1349 points to this die's parent; CHILD points to the first child of
1350 this node; and all the children of a given node are chained
4950bc1c 1351 together via their SIBLING fields. */
639d11d3
DC
1352 struct die_info *child; /* Its first child, if any. */
1353 struct die_info *sibling; /* Its next sibling, if any. */
1354 struct die_info *parent; /* Its parent, if any. */
c906108c 1355
b60c80d6
DJ
1356 /* An array of attributes, with NUM_ATTRS elements. There may be
1357 zero, but it's not common and zero-sized arrays are not
1358 sufficiently portable C. */
1359 struct attribute attrs[1];
c906108c
SS
1360 };
1361
0963b4bd 1362/* Get at parts of an attribute structure. */
c906108c
SS
1363
1364#define DW_STRING(attr) ((attr)->u.str)
8285870a 1365#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1366#define DW_UNSND(attr) ((attr)->u.unsnd)
1367#define DW_BLOCK(attr) ((attr)->u.blk)
1368#define DW_SND(attr) ((attr)->u.snd)
1369#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1370#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1371
0963b4bd 1372/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1373struct dwarf_block
1374 {
56eb65bd 1375 size_t size;
1d6edc3c
JK
1376
1377 /* Valid only if SIZE is not zero. */
d521ce57 1378 const gdb_byte *data;
c906108c
SS
1379 };
1380
c906108c
SS
1381/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1382 but this would require a corresponding change in unpack_field_as_long
1383 and friends. */
1384static int bits_per_byte = 8;
1385
2ddeaf8a
TT
1386/* When reading a variant or variant part, we track a bit more
1387 information about the field, and store it in an object of this
1388 type. */
1389
1390struct variant_field
1391{
1392 /* If we see a DW_TAG_variant, then this will be the discriminant
1393 value. */
1394 ULONGEST discriminant_value;
1395 /* If we see a DW_TAG_variant, then this will be set if this is the
1396 default branch. */
1397 bool default_branch;
1398 /* While reading a DW_TAG_variant_part, this will be set if this
1399 field is the discriminant. */
1400 bool is_discriminant;
1401};
1402
52059ffd
TT
1403struct nextfield
1404{
be2daae6
TT
1405 int accessibility = 0;
1406 int virtuality = 0;
2ddeaf8a 1407 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1408 struct variant_field variant {};
1409 struct field field {};
52059ffd
TT
1410};
1411
1412struct fnfieldlist
1413{
be2daae6
TT
1414 const char *name = nullptr;
1415 std::vector<struct fn_field> fnfields;
52059ffd
TT
1416};
1417
c906108c
SS
1418/* The routines that read and process dies for a C struct or C++ class
1419 pass lists of data member fields and lists of member function fields
1420 in an instance of a field_info structure, as defined below. */
1421struct field_info
c5aa993b 1422 {
0963b4bd 1423 /* List of data member and baseclasses fields. */
be2daae6
TT
1424 std::vector<struct nextfield> fields;
1425 std::vector<struct nextfield> baseclasses;
c906108c 1426
7d0ccb61 1427 /* Number of fields (including baseclasses). */
be2daae6 1428 int nfields = 0;
c906108c 1429
85102364 1430 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1431 int non_public_fields = 0;
c906108c 1432
c5aa993b
JM
1433 /* Member function fieldlist array, contains name of possibly overloaded
1434 member function, number of overloaded member functions and a pointer
1435 to the head of the member function field chain. */
be2daae6 1436 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1437
1438 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1439 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1440 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1441
1442 /* Nested types defined by this class and the number of elements in this
1443 list. */
be2daae6 1444 std::vector<struct decl_field> nested_types_list;
c5aa993b 1445 };
c906108c 1446
10b3939b
DJ
1447/* One item on the queue of compilation units to read in full symbols
1448 for. */
1449struct dwarf2_queue_item
1450{
1451 struct dwarf2_per_cu_data *per_cu;
95554aad 1452 enum language pretend_language;
10b3939b
DJ
1453 struct dwarf2_queue_item *next;
1454};
1455
1456/* The current queue. */
1457static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1458
ae038cb0
DJ
1459/* Loaded secondary compilation units are kept in memory until they
1460 have not been referenced for the processing of this many
1461 compilation units. Set this to zero to disable caching. Cache
1462 sizes of up to at least twenty will improve startup time for
1463 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1464static int dwarf_max_cache_age = 5;
920d2a44 1465static void
b4f54984
DE
1466show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1467 struct cmd_list_element *c, const char *value)
920d2a44 1468{
3e43a32a 1469 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1470 "DWARF compilation units is %s.\n"),
920d2a44
AC
1471 value);
1472}
4390d890 1473\f
c906108c
SS
1474/* local function prototypes */
1475
a32a8923
DE
1476static const char *get_section_name (const struct dwarf2_section_info *);
1477
1478static const char *get_section_file_name (const struct dwarf2_section_info *);
1479
918dd910
JK
1480static void dwarf2_find_base_address (struct die_info *die,
1481 struct dwarf2_cu *cu);
1482
0018ea6f
DE
1483static struct partial_symtab *create_partial_symtab
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
257e7a09
YQ
1516static void dwarf2_read_symtab (struct partial_symtab *,
1517 struct objfile *);
c906108c 1518
a14ed312 1519static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1520
685af9cd 1521static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1522 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1523 sect_offset);
433df2d4 1524
d521ce57 1525static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1526
dee91e82 1527static struct partial_die_info *load_partial_dies
d521ce57 1528 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1529
fb816e8b
TV
1530/* A pair of partial_die_info and compilation unit. */
1531struct cu_partial_die_info
1532{
1533 /* The compilation unit of the partial_die_info. */
1534 struct dwarf2_cu *cu;
1535 /* A partial_die_info. */
1536 struct partial_die_info *pdi;
122cf0f2
AB
1537
1538 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1539 : cu (cu),
1540 pdi (pdi)
405feb71 1541 { /* Nothing. */ }
122cf0f2
AB
1542
1543private:
1544 cu_partial_die_info () = delete;
fb816e8b
TV
1545};
1546
122cf0f2
AB
1547static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1548 struct dwarf2_cu *);
72bf9492 1549
d521ce57
TT
1550static const gdb_byte *read_attribute (const struct die_reader_specs *,
1551 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1552 const gdb_byte *, bool *need_reprocess);
1553
1554static void read_attribute_reprocess (const struct die_reader_specs *reader,
1555 struct attribute *attr);
1556
1557static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1558
a1855c1d 1559static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1560
a1855c1d 1561static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1562
a1855c1d 1563static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1564
15f18d14
AT
1565/* Read the next three bytes (little-endian order) as an unsigned integer. */
1566static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1567
a1855c1d 1568static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1569
a1855c1d 1570static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1571
d521ce57 1572static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1573 unsigned int *);
c906108c 1574
d521ce57 1575static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1576
1577static LONGEST read_checked_initial_length_and_offset
d521ce57 1578 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1579 unsigned int *, unsigned int *);
613e1657 1580
d521ce57
TT
1581static LONGEST read_offset (bfd *, const gdb_byte *,
1582 const struct comp_unit_head *,
c764a876
DE
1583 unsigned int *);
1584
d521ce57 1585static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1586
ed2dc618
SM
1587static sect_offset read_abbrev_offset
1588 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1589 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1590
d521ce57 1591static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1592
d521ce57 1593static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1594
ed2dc618
SM
1595static const char *read_indirect_string
1596 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1597 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1598
ed2dc618
SM
1599static const char *read_indirect_line_string
1600 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1601 const struct comp_unit_head *, unsigned int *);
36586728 1602
ed2dc618
SM
1603static const char *read_indirect_string_at_offset
1604 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1605 LONGEST str_offset);
927aa2e7 1606
ed2dc618
SM
1607static const char *read_indirect_string_from_dwz
1608 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1609
d521ce57 1610static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1611
d521ce57
TT
1612static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1613 const gdb_byte *,
3019eac3
DE
1614 unsigned int *);
1615
18a8505e
AT
1616static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1617 ULONGEST str_index);
1618
1619static const char *read_stub_str_index (struct dwarf2_cu *cu,
1620 ULONGEST str_index);
3019eac3 1621
e142c38c 1622static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1623
e142c38c
DJ
1624static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1625 struct dwarf2_cu *);
c906108c 1626
348e048f 1627static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1628 unsigned int);
348e048f 1629
7d45c7c3
KB
1630static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1631 struct dwarf2_cu *cu);
1632
a084a2a6
AT
1633static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1634
05cf31d1
JB
1635static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1636 struct dwarf2_cu *cu);
1637
e142c38c 1638static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1639
e142c38c 1640static struct die_info *die_specification (struct die_info *die,
f2f0e013 1641 struct dwarf2_cu **);
63d06c5c 1642
9c541725 1643static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1644 struct dwarf2_cu *cu);
debd256d 1645
f3f5162e 1646static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1647 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1648 CORE_ADDR, int decode_mapping);
c906108c 1649
804d2729
TT
1650static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1651 const char *);
c906108c 1652
a14ed312 1653static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1654 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1655
ff39bb5e 1656static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1657 struct dwarf2_cu *);
c906108c 1658
ff39bb5e 1659static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1660 struct type *type,
1661 const char *name,
1662 struct obstack *obstack,
12df843f 1663 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1664 const gdb_byte **bytes,
98bfdba5 1665 struct dwarf2_locexpr_baton **baton);
2df3850c 1666
e7c27a73 1667static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1668
b4ba55a1
JB
1669static int need_gnat_info (struct dwarf2_cu *);
1670
3e43a32a
MS
1671static struct type *die_descriptive_type (struct die_info *,
1672 struct dwarf2_cu *);
b4ba55a1
JB
1673
1674static void set_descriptive_type (struct type *, struct die_info *,
1675 struct dwarf2_cu *);
1676
e7c27a73
DJ
1677static struct type *die_containing_type (struct die_info *,
1678 struct dwarf2_cu *);
c906108c 1679
ff39bb5e 1680static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1681 struct dwarf2_cu *);
c906108c 1682
f792889a 1683static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1684
673bfd45
DE
1685static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1686
0d5cff50 1687static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1688
6e70227d 1689static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1690 const char *suffix, int physname,
1691 struct dwarf2_cu *cu);
63d06c5c 1692
e7c27a73 1693static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1694
348e048f
DE
1695static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1696
e7c27a73 1697static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1698
e7c27a73 1699static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1700
96408a79
SA
1701static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1702
71a3c369
TT
1703static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1704
ff013f42
JK
1705static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1706 struct dwarf2_cu *, struct partial_symtab *);
1707
3a2b436a 1708/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1709 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1710enum pc_bounds_kind
1711{
e385593e 1712 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1713 PC_BOUNDS_NOT_PRESENT,
1714
e385593e
JK
1715 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1716 were present but they do not form a valid range of PC addresses. */
1717 PC_BOUNDS_INVALID,
1718
3a2b436a
JK
1719 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1720 PC_BOUNDS_RANGES,
1721
1722 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1723 PC_BOUNDS_HIGH_LOW,
1724};
1725
1726static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1727 CORE_ADDR *, CORE_ADDR *,
1728 struct dwarf2_cu *,
1729 struct partial_symtab *);
c906108c 1730
fae299cd
DC
1731static void get_scope_pc_bounds (struct die_info *,
1732 CORE_ADDR *, CORE_ADDR *,
1733 struct dwarf2_cu *);
1734
801e3a5b
JB
1735static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1736 CORE_ADDR, struct dwarf2_cu *);
1737
a14ed312 1738static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1739 struct dwarf2_cu *);
c906108c 1740
a14ed312 1741static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1742 struct type *, struct dwarf2_cu *);
c906108c 1743
a14ed312 1744static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1745 struct die_info *, struct type *,
e7c27a73 1746 struct dwarf2_cu *);
c906108c 1747
a14ed312 1748static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1749 struct type *,
1750 struct dwarf2_cu *);
c906108c 1751
134d01f1 1752static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1753
e7c27a73 1754static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1755
e7c27a73 1756static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1757
5d7cb8df
JK
1758static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1759
804d2729 1760static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1761
27aa8d6a
SW
1762static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1763
74921315
KS
1764static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1765
f55ee35c
JK
1766static struct type *read_module_type (struct die_info *die,
1767 struct dwarf2_cu *cu);
1768
38d518c9 1769static const char *namespace_name (struct die_info *die,
e142c38c 1770 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1771
134d01f1 1772static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1773
e7c27a73 1774static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1775
6e70227d 1776static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1777 struct dwarf2_cu *);
1778
bf6af496 1779static struct die_info *read_die_and_siblings_1
d521ce57 1780 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1781 struct die_info *);
639d11d3 1782
dee91e82 1783static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1784 const gdb_byte *info_ptr,
1785 const gdb_byte **new_info_ptr,
639d11d3
DC
1786 struct die_info *parent);
1787
d521ce57
TT
1788static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1789 struct die_info **, const gdb_byte *,
1790 int *, int);
3019eac3 1791
d521ce57
TT
1792static const gdb_byte *read_full_die (const struct die_reader_specs *,
1793 struct die_info **, const gdb_byte *,
1794 int *);
93311388 1795
e7c27a73 1796static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1797
15d034d0
TT
1798static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1799 struct obstack *);
71c25dea 1800
15d034d0 1801static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1802
15d034d0 1803static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1804 struct die_info *die,
1805 struct dwarf2_cu *cu);
1806
ca69b9e6
DE
1807static const char *dwarf2_physname (const char *name, struct die_info *die,
1808 struct dwarf2_cu *cu);
1809
e142c38c 1810static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1811 struct dwarf2_cu **);
9219021c 1812
f39c6ffd 1813static const char *dwarf_tag_name (unsigned int);
c906108c 1814
f39c6ffd 1815static const char *dwarf_attr_name (unsigned int);
c906108c 1816
a084a2a6
AT
1817static const char *dwarf_unit_type_name (int unit_type);
1818
f39c6ffd 1819static const char *dwarf_form_name (unsigned int);
c906108c 1820
a121b7c1 1821static const char *dwarf_bool_name (unsigned int);
c906108c 1822
f39c6ffd 1823static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1824
f9aca02d 1825static struct die_info *sibling_die (struct die_info *);
c906108c 1826
d97bc12b
DE
1827static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1828
1829static void dump_die_for_error (struct die_info *);
1830
1831static void dump_die_1 (struct ui_file *, int level, int max_level,
1832 struct die_info *);
c906108c 1833
d97bc12b 1834/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1835
51545339 1836static void store_in_ref_table (struct die_info *,
10b3939b 1837 struct dwarf2_cu *);
c906108c 1838
ff39bb5e 1839static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1840
ff39bb5e 1841static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1842
348e048f 1843static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1844 const struct attribute *,
348e048f
DE
1845 struct dwarf2_cu **);
1846
10b3939b 1847static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1848 const struct attribute *,
f2f0e013 1849 struct dwarf2_cu **);
c906108c 1850
348e048f 1851static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1852 const struct attribute *,
348e048f
DE
1853 struct dwarf2_cu **);
1854
ac9ec31b
DE
1855static struct type *get_signatured_type (struct die_info *, ULONGEST,
1856 struct dwarf2_cu *);
1857
1858static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1859 const struct attribute *,
ac9ec31b
DE
1860 struct dwarf2_cu *);
1861
e5fe5e75 1862static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1863
52dc124a 1864static void read_signatured_type (struct signatured_type *);
348e048f 1865
63e43d3a
PMR
1866static int attr_to_dynamic_prop (const struct attribute *attr,
1867 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1868 struct dynamic_prop *prop, struct type *type);
63e43d3a 1869
c906108c
SS
1870/* memory allocation interface */
1871
7b5a2f43 1872static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1873
b60c80d6 1874static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1875
43f3e411 1876static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1877
6e5a29e1 1878static int attr_form_is_block (const struct attribute *);
8e19ed76 1879
6e5a29e1 1880static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1881
6e5a29e1 1882static int attr_form_is_constant (const struct attribute *);
3690dd37 1883
6e5a29e1 1884static int attr_form_is_ref (const struct attribute *);
7771576e 1885
8cf6f0b1
TT
1886static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1887 struct dwarf2_loclist_baton *baton,
ff39bb5e 1888 const struct attribute *attr);
8cf6f0b1 1889
ff39bb5e 1890static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1891 struct symbol *sym,
f1e6e072
TT
1892 struct dwarf2_cu *cu,
1893 int is_block);
4c2df51b 1894
d521ce57
TT
1895static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1896 const gdb_byte *info_ptr,
1897 struct abbrev_info *abbrev);
4bb7a0a7 1898
72bf9492
DJ
1899static hashval_t partial_die_hash (const void *item);
1900
1901static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1902
ae038cb0 1903static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1904 (sect_offset sect_off, unsigned int offset_in_dwz,
1905 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1906
9816fde3 1907static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1908 struct die_info *comp_unit_die,
1909 enum language pretend_language);
93311388 1910
ed2dc618 1911static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1912
dee91e82 1913static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1914
f792889a
DJ
1915static struct type *set_die_type (struct die_info *, struct type *,
1916 struct dwarf2_cu *);
1c379e20 1917
ed2dc618 1918static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1919
ed2dc618 1920static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1921
58f0c718 1922static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1923 enum language);
10b3939b 1924
95554aad
TT
1925static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1926 enum language);
10b3939b 1927
f4dc4d17
DE
1928static void process_full_type_unit (struct dwarf2_per_cu_data *,
1929 enum language);
1930
10b3939b
DJ
1931static void dwarf2_add_dependence (struct dwarf2_cu *,
1932 struct dwarf2_per_cu_data *);
1933
ae038cb0
DJ
1934static void dwarf2_mark (struct dwarf2_cu *);
1935
1936static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1937
b64f50a1 1938static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1939 struct dwarf2_per_cu_data *);
673bfd45 1940
f792889a 1941static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1942
95554aad
TT
1943static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1944 enum language pretend_language);
1945
ed2dc618 1946static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1947
9a49df9d
AB
1948static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1949static struct type *dwarf2_per_cu_addr_sized_int_type
1950 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1951static struct type *dwarf2_per_cu_int_type
1952 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1953 bool unsigned_p);
9a49df9d 1954
b303c6f6
AB
1955/* Class, the destructor of which frees all allocated queue entries. This
1956 will only have work to do if an error was thrown while processing the
1957 dwarf. If no error was thrown then the queue entries should have all
1958 been processed, and freed, as we went along. */
1959
1960class dwarf2_queue_guard
1961{
1962public:
1963 dwarf2_queue_guard () = default;
1964
1965 /* Free any entries remaining on the queue. There should only be
1966 entries left if we hit an error while processing the dwarf. */
1967 ~dwarf2_queue_guard ()
1968 {
1969 struct dwarf2_queue_item *item, *last;
1970
1971 item = dwarf2_queue;
1972 while (item)
1973 {
1974 /* Anything still marked queued is likely to be in an
1975 inconsistent state, so discard it. */
1976 if (item->per_cu->queued)
1977 {
1978 if (item->per_cu->cu != NULL)
1979 free_one_cached_comp_unit (item->per_cu);
1980 item->per_cu->queued = 0;
1981 }
1982
1983 last = item;
1984 item = item->next;
1985 xfree (last);
1986 }
1987
1988 dwarf2_queue = dwarf2_queue_tail = NULL;
1989 }
1990};
1991
d721ba37
PA
1992/* The return type of find_file_and_directory. Note, the enclosed
1993 string pointers are only valid while this object is valid. */
1994
1995struct file_and_directory
1996{
1997 /* The filename. This is never NULL. */
1998 const char *name;
1999
2000 /* The compilation directory. NULL if not known. If we needed to
2001 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2002 points directly to the DW_AT_comp_dir string attribute owned by
2003 the obstack that owns the DIE. */
2004 const char *comp_dir;
2005
2006 /* If we needed to build a new string for comp_dir, this is what
2007 owns the storage. */
2008 std::string comp_dir_storage;
2009};
2010
2011static file_and_directory find_file_and_directory (struct die_info *die,
2012 struct dwarf2_cu *cu);
9291a0cd
TT
2013
2014static char *file_full_name (int file, struct line_header *lh,
2015 const char *comp_dir);
2016
43988095
JK
2017/* Expected enum dwarf_unit_type for read_comp_unit_head. */
2018enum class rcuh_kind { COMPILE, TYPE };
2019
d521ce57 2020static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
2021 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2022 struct comp_unit_head *header,
36586728 2023 struct dwarf2_section_info *section,
d521ce57 2024 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2025 rcuh_kind section_kind);
36586728 2026
673bfd45 2027static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2028
3019eac3
DE
2029static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2030
57d63ce2 2031static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2032 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2033 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2034 ULONGEST signature, int is_debug_types);
a2ce51a0 2035
ed2dc618
SM
2036static struct dwp_file *get_dwp_file
2037 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2038
3019eac3 2039static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2040 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2041
2042static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2043 (struct signatured_type *, const char *, const char *);
3019eac3 2044
89e63ee4
DE
2045static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2046
263db9a1
TT
2047/* A unique pointer to a dwo_file. */
2048
51ac9db5 2049typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2050
ed2dc618 2051static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2052
1b80a9fa 2053static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2054
2055static void free_line_header_voidp (void *arg);
4390d890
DE
2056\f
2057/* Various complaints about symbol reading that don't abort the process. */
2058
2059static void
2060dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2061{
b98664d3 2062 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2063}
2064
2065static void
2066dwarf2_debug_line_missing_file_complaint (void)
2067{
b98664d3 2068 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2069}
2070
2071static void
2072dwarf2_debug_line_missing_end_sequence_complaint (void)
2073{
b98664d3 2074 complaint (_(".debug_line section has line "
4390d890
DE
2075 "program sequence without an end"));
2076}
2077
2078static void
2079dwarf2_complex_location_expr_complaint (void)
2080{
b98664d3 2081 complaint (_("location expression too complex"));
4390d890
DE
2082}
2083
2084static void
2085dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2086 int arg3)
2087{
b98664d3 2088 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2089 arg1, arg2, arg3);
2090}
2091
2092static void
2093dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2094{
b98664d3 2095 complaint (_("debug info runs off end of %s section"
4390d890 2096 " [in module %s]"),
a32a8923
DE
2097 get_section_name (section),
2098 get_section_file_name (section));
4390d890 2099}
1b80a9fa 2100
4390d890
DE
2101static void
2102dwarf2_macro_malformed_definition_complaint (const char *arg1)
2103{
b98664d3 2104 complaint (_("macro debug info contains a "
4390d890
DE
2105 "malformed macro definition:\n`%s'"),
2106 arg1);
2107}
2108
2109static void
2110dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2111{
b98664d3 2112 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2113 arg1, arg2);
2114}
527f3840
JK
2115
2116/* Hash function for line_header_hash. */
2117
2118static hashval_t
2119line_header_hash (const struct line_header *ofs)
2120{
9c541725 2121 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2122}
2123
2124/* Hash function for htab_create_alloc_ex for line_header_hash. */
2125
2126static hashval_t
2127line_header_hash_voidp (const void *item)
2128{
9a3c8263 2129 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2130
2131 return line_header_hash (ofs);
2132}
2133
2134/* Equality function for line_header_hash. */
2135
2136static int
2137line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2138{
9a3c8263
SM
2139 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2140 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2141
9c541725 2142 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2143 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2144}
2145
4390d890 2146\f
9291a0cd 2147
31aa7e4e
JB
2148/* Read the given attribute value as an address, taking the attribute's
2149 form into account. */
2150
2151static CORE_ADDR
2152attr_value_as_address (struct attribute *attr)
2153{
2154 CORE_ADDR addr;
2155
336d760d
AT
2156 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2157 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2158 {
2159 /* Aside from a few clearly defined exceptions, attributes that
2160 contain an address must always be in DW_FORM_addr form.
2161 Unfortunately, some compilers happen to be violating this
2162 requirement by encoding addresses using other forms, such
2163 as DW_FORM_data4 for example. For those broken compilers,
2164 we try to do our best, without any guarantee of success,
2165 to interpret the address correctly. It would also be nice
2166 to generate a complaint, but that would require us to maintain
2167 a list of legitimate cases where a non-address form is allowed,
2168 as well as update callers to pass in at least the CU's DWARF
2169 version. This is more overhead than what we're willing to
2170 expand for a pretty rare case. */
2171 addr = DW_UNSND (attr);
2172 }
2173 else
2174 addr = DW_ADDR (attr);
2175
2176 return addr;
2177}
2178
330cdd98
PA
2179/* See declaration. */
2180
2181dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2182 const dwarf2_debug_sections *names,
2183 bool can_copy_)
2184 : objfile (objfile_),
2185 can_copy (can_copy_)
330cdd98
PA
2186{
2187 if (names == NULL)
2188 names = &dwarf2_elf_names;
2189
2190 bfd *obfd = objfile->obfd;
2191
2192 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2193 locate_sections (obfd, sec, *names);
2194}
2195
2196dwarf2_per_objfile::~dwarf2_per_objfile ()
2197{
2198 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2199 free_cached_comp_units ();
2200
2201 if (quick_file_names_table)
2202 htab_delete (quick_file_names_table);
2203
2204 if (line_header_hash)
2205 htab_delete (line_header_hash);
2206
b76e467d 2207 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2208 per_cu->imported_symtabs_free ();
fc8e7e75 2209
b2bdb8cf 2210 for (signatured_type *sig_type : all_type_units)
ae640021 2211 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2212
330cdd98
PA
2213 /* Everything else should be on the objfile obstack. */
2214}
2215
2216/* See declaration. */
2217
2218void
2219dwarf2_per_objfile::free_cached_comp_units ()
2220{
2221 dwarf2_per_cu_data *per_cu = read_in_chain;
2222 dwarf2_per_cu_data **last_chain = &read_in_chain;
2223 while (per_cu != NULL)
2224 {
2225 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2226
fcd3b13d 2227 delete per_cu->cu;
330cdd98
PA
2228 *last_chain = next_cu;
2229 per_cu = next_cu;
2230 }
2231}
2232
11ed8cad
TT
2233/* A helper class that calls free_cached_comp_units on
2234 destruction. */
2235
2236class free_cached_comp_units
2237{
2238public:
2239
2240 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2241 : m_per_objfile (per_objfile)
2242 {
2243 }
2244
2245 ~free_cached_comp_units ()
2246 {
2247 m_per_objfile->free_cached_comp_units ();
2248 }
2249
2250 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2251
2252private:
2253
2254 dwarf2_per_objfile *m_per_objfile;
2255};
2256
c906108c 2257/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2258 information and return true if we have enough to do something.
2259 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2260 ELF names are used. CAN_COPY is true for formats where symbol
2261 interposition is possible and so symbol values must follow copy
2262 relocation rules. */
c906108c
SS
2263
2264int
251d32d9 2265dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2266 const struct dwarf2_debug_sections *names,
2267 bool can_copy)
c906108c 2268{
97cbe998
SDJ
2269 if (objfile->flags & OBJF_READNEVER)
2270 return 0;
2271
ed2dc618
SM
2272 struct dwarf2_per_objfile *dwarf2_per_objfile
2273 = get_dwarf2_per_objfile (objfile);
2274
2275 if (dwarf2_per_objfile == NULL)
5bfd760d 2276 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2277 names,
2278 can_copy);
5bfd760d 2279
73869dc2 2280 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2281 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2282 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2283 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2284}
2285
2286/* Return the containing section of virtual section SECTION. */
2287
2288static struct dwarf2_section_info *
2289get_containing_section (const struct dwarf2_section_info *section)
2290{
2291 gdb_assert (section->is_virtual);
2292 return section->s.containing_section;
c906108c
SS
2293}
2294
a32a8923
DE
2295/* Return the bfd owner of SECTION. */
2296
2297static struct bfd *
2298get_section_bfd_owner (const struct dwarf2_section_info *section)
2299{
73869dc2
DE
2300 if (section->is_virtual)
2301 {
2302 section = get_containing_section (section);
2303 gdb_assert (!section->is_virtual);
2304 }
049412e3 2305 return section->s.section->owner;
a32a8923
DE
2306}
2307
2308/* Return the bfd section of SECTION.
2309 Returns NULL if the section is not present. */
2310
2311static asection *
2312get_section_bfd_section (const struct dwarf2_section_info *section)
2313{
73869dc2
DE
2314 if (section->is_virtual)
2315 {
2316 section = get_containing_section (section);
2317 gdb_assert (!section->is_virtual);
2318 }
049412e3 2319 return section->s.section;
a32a8923
DE
2320}
2321
2322/* Return the name of SECTION. */
2323
2324static const char *
2325get_section_name (const struct dwarf2_section_info *section)
2326{
2327 asection *sectp = get_section_bfd_section (section);
2328
2329 gdb_assert (sectp != NULL);
fd361982 2330 return bfd_section_name (sectp);
a32a8923
DE
2331}
2332
2333/* Return the name of the file SECTION is in. */
2334
2335static const char *
2336get_section_file_name (const struct dwarf2_section_info *section)
2337{
2338 bfd *abfd = get_section_bfd_owner (section);
2339
2340 return bfd_get_filename (abfd);
2341}
2342
2343/* Return the id of SECTION.
2344 Returns 0 if SECTION doesn't exist. */
2345
2346static int
2347get_section_id (const struct dwarf2_section_info *section)
2348{
2349 asection *sectp = get_section_bfd_section (section);
2350
2351 if (sectp == NULL)
2352 return 0;
2353 return sectp->id;
2354}
2355
2356/* Return the flags of SECTION.
73869dc2 2357 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2358
2359static int
2360get_section_flags (const struct dwarf2_section_info *section)
2361{
2362 asection *sectp = get_section_bfd_section (section);
2363
2364 gdb_assert (sectp != NULL);
fd361982 2365 return bfd_section_flags (sectp);
a32a8923
DE
2366}
2367
251d32d9
TG
2368/* When loading sections, we look either for uncompressed section or for
2369 compressed section names. */
233a11ab
CS
2370
2371static int
251d32d9
TG
2372section_is_p (const char *section_name,
2373 const struct dwarf2_section_names *names)
233a11ab 2374{
251d32d9
TG
2375 if (names->normal != NULL
2376 && strcmp (section_name, names->normal) == 0)
2377 return 1;
2378 if (names->compressed != NULL
2379 && strcmp (section_name, names->compressed) == 0)
2380 return 1;
2381 return 0;
233a11ab
CS
2382}
2383
330cdd98 2384/* See declaration. */
c906108c 2385
330cdd98
PA
2386void
2387dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2388 const dwarf2_debug_sections &names)
c906108c 2389{
fd361982 2390 flagword aflag = bfd_section_flags (sectp);
251d32d9 2391
dc7650b8
JK
2392 if ((aflag & SEC_HAS_CONTENTS) == 0)
2393 {
2394 }
950b7495
KS
2395 else if (elf_section_data (sectp)->this_hdr.sh_size
2396 > bfd_get_file_size (abfd))
2397 {
2398 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2399 warning (_("Discarding section %s which has a section size (%s"
2400 ") larger than the file size [in module %s]"),
2401 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2402 bfd_get_filename (abfd));
2403 }
330cdd98 2404 else if (section_is_p (sectp->name, &names.info))
c906108c 2405 {
330cdd98 2406 this->info.s.section = sectp;
fd361982 2407 this->info.size = bfd_section_size (sectp);
c906108c 2408 }
330cdd98 2409 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2410 {
330cdd98 2411 this->abbrev.s.section = sectp;
fd361982 2412 this->abbrev.size = bfd_section_size (sectp);
c906108c 2413 }
330cdd98 2414 else if (section_is_p (sectp->name, &names.line))
c906108c 2415 {
330cdd98 2416 this->line.s.section = sectp;
fd361982 2417 this->line.size = bfd_section_size (sectp);
c906108c 2418 }
330cdd98 2419 else if (section_is_p (sectp->name, &names.loc))
c906108c 2420 {
330cdd98 2421 this->loc.s.section = sectp;
fd361982 2422 this->loc.size = bfd_section_size (sectp);
c906108c 2423 }
330cdd98 2424 else if (section_is_p (sectp->name, &names.loclists))
43988095 2425 {
330cdd98 2426 this->loclists.s.section = sectp;
fd361982 2427 this->loclists.size = bfd_section_size (sectp);
43988095 2428 }
330cdd98 2429 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2430 {
330cdd98 2431 this->macinfo.s.section = sectp;
fd361982 2432 this->macinfo.size = bfd_section_size (sectp);
c906108c 2433 }
330cdd98 2434 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2435 {
330cdd98 2436 this->macro.s.section = sectp;
fd361982 2437 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2438 }
330cdd98 2439 else if (section_is_p (sectp->name, &names.str))
c906108c 2440 {
330cdd98 2441 this->str.s.section = sectp;
fd361982 2442 this->str.size = bfd_section_size (sectp);
c906108c 2443 }
18a8505e
AT
2444 else if (section_is_p (sectp->name, &names.str_offsets))
2445 {
2446 this->str_offsets.s.section = sectp;
2447 this->str_offsets.size = bfd_section_size (sectp);
2448 }
330cdd98 2449 else if (section_is_p (sectp->name, &names.line_str))
43988095 2450 {
330cdd98 2451 this->line_str.s.section = sectp;
fd361982 2452 this->line_str.size = bfd_section_size (sectp);
43988095 2453 }
330cdd98 2454 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2455 {
330cdd98 2456 this->addr.s.section = sectp;
fd361982 2457 this->addr.size = bfd_section_size (sectp);
3019eac3 2458 }
330cdd98 2459 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2460 {
330cdd98 2461 this->frame.s.section = sectp;
fd361982 2462 this->frame.size = bfd_section_size (sectp);
b6af0555 2463 }
330cdd98 2464 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2465 {
330cdd98 2466 this->eh_frame.s.section = sectp;
fd361982 2467 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2468 }
330cdd98 2469 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2470 {
330cdd98 2471 this->ranges.s.section = sectp;
fd361982 2472 this->ranges.size = bfd_section_size (sectp);
af34e669 2473 }
330cdd98 2474 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2475 {
330cdd98 2476 this->rnglists.s.section = sectp;
fd361982 2477 this->rnglists.size = bfd_section_size (sectp);
43988095 2478 }
330cdd98 2479 else if (section_is_p (sectp->name, &names.types))
348e048f 2480 {
8b70b953
TT
2481 struct dwarf2_section_info type_section;
2482
2483 memset (&type_section, 0, sizeof (type_section));
049412e3 2484 type_section.s.section = sectp;
fd361982 2485 type_section.size = bfd_section_size (sectp);
8b70b953 2486
fd5866f6 2487 this->types.push_back (type_section);
348e048f 2488 }
330cdd98 2489 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2490 {
330cdd98 2491 this->gdb_index.s.section = sectp;
fd361982 2492 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2493 }
927aa2e7
JK
2494 else if (section_is_p (sectp->name, &names.debug_names))
2495 {
2496 this->debug_names.s.section = sectp;
fd361982 2497 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2498 }
2499 else if (section_is_p (sectp->name, &names.debug_aranges))
2500 {
2501 this->debug_aranges.s.section = sectp;
fd361982 2502 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2503 }
dce234bc 2504
fd361982
AM
2505 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2506 && bfd_section_vma (sectp) == 0)
330cdd98 2507 this->has_section_at_zero = true;
c906108c
SS
2508}
2509
fceca515
DE
2510/* A helper function that decides whether a section is empty,
2511 or not present. */
9e0ac564
TT
2512
2513static int
19ac8c2e 2514dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2515{
73869dc2
DE
2516 if (section->is_virtual)
2517 return section->size == 0;
049412e3 2518 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2519}
2520
cd4fb1b2 2521/* See dwarf2read.h. */
c906108c 2522
cd4fb1b2
SM
2523void
2524dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2525{
a32a8923 2526 asection *sectp;
3019eac3 2527 bfd *abfd;
dce234bc 2528 gdb_byte *buf, *retbuf;
c906108c 2529
be391dca
TT
2530 if (info->readin)
2531 return;
dce234bc 2532 info->buffer = NULL;
dc4ccb6f 2533 info->readin = true;
188dd5d6 2534
9e0ac564 2535 if (dwarf2_section_empty_p (info))
dce234bc 2536 return;
c906108c 2537
a32a8923 2538 sectp = get_section_bfd_section (info);
3019eac3 2539
73869dc2
DE
2540 /* If this is a virtual section we need to read in the real one first. */
2541 if (info->is_virtual)
2542 {
2543 struct dwarf2_section_info *containing_section =
2544 get_containing_section (info);
2545
2546 gdb_assert (sectp != NULL);
2547 if ((sectp->flags & SEC_RELOC) != 0)
2548 {
2549 error (_("Dwarf Error: DWP format V2 with relocations is not"
2550 " supported in section %s [in module %s]"),
2551 get_section_name (info), get_section_file_name (info));
2552 }
2553 dwarf2_read_section (objfile, containing_section);
2554 /* Other code should have already caught virtual sections that don't
2555 fit. */
2556 gdb_assert (info->virtual_offset + info->size
2557 <= containing_section->size);
2558 /* If the real section is empty or there was a problem reading the
2559 section we shouldn't get here. */
2560 gdb_assert (containing_section->buffer != NULL);
2561 info->buffer = containing_section->buffer + info->virtual_offset;
2562 return;
2563 }
2564
4bf44c1c
TT
2565 /* If the section has relocations, we must read it ourselves.
2566 Otherwise we attach it to the BFD. */
2567 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2568 {
d521ce57 2569 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2570 return;
dce234bc 2571 }
dce234bc 2572
224c3ddb 2573 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2574 info->buffer = buf;
dce234bc
PP
2575
2576 /* When debugging .o files, we may need to apply relocations; see
2577 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2578 We never compress sections in .o files, so we only need to
2579 try this when the section is not compressed. */
ac8035ab 2580 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2581 if (retbuf != NULL)
2582 {
2583 info->buffer = retbuf;
2584 return;
2585 }
2586
a32a8923
DE
2587 abfd = get_section_bfd_owner (info);
2588 gdb_assert (abfd != NULL);
2589
dce234bc
PP
2590 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2591 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2592 {
2593 error (_("Dwarf Error: Can't read DWARF data"
2594 " in section %s [in module %s]"),
fd361982 2595 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2596 }
dce234bc
PP
2597}
2598
9e0ac564
TT
2599/* A helper function that returns the size of a section in a safe way.
2600 If you are positive that the section has been read before using the
2601 size, then it is safe to refer to the dwarf2_section_info object's
2602 "size" field directly. In other cases, you must call this
2603 function, because for compressed sections the size field is not set
2604 correctly until the section has been read. */
2605
2606static bfd_size_type
2607dwarf2_section_size (struct objfile *objfile,
2608 struct dwarf2_section_info *info)
2609{
2610 if (!info->readin)
2611 dwarf2_read_section (objfile, info);
2612 return info->size;
2613}
2614
dce234bc 2615/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2616 SECTION_NAME. */
af34e669 2617
dce234bc 2618void
3017a003
TG
2619dwarf2_get_section_info (struct objfile *objfile,
2620 enum dwarf2_section_enum sect,
d521ce57 2621 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2622 bfd_size_type *sizep)
2623{
5bfd760d 2624 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2625 struct dwarf2_section_info *info;
a3b2a86b
TT
2626
2627 /* We may see an objfile without any DWARF, in which case we just
2628 return nothing. */
2629 if (data == NULL)
2630 {
2631 *sectp = NULL;
2632 *bufp = NULL;
2633 *sizep = 0;
2634 return;
2635 }
3017a003
TG
2636 switch (sect)
2637 {
2638 case DWARF2_DEBUG_FRAME:
2639 info = &data->frame;
2640 break;
2641 case DWARF2_EH_FRAME:
2642 info = &data->eh_frame;
2643 break;
2644 default:
2645 gdb_assert_not_reached ("unexpected section");
2646 }
dce234bc 2647
9e0ac564 2648 dwarf2_read_section (objfile, info);
dce234bc 2649
a32a8923 2650 *sectp = get_section_bfd_section (info);
dce234bc
PP
2651 *bufp = info->buffer;
2652 *sizep = info->size;
2653}
2654
36586728
TT
2655/* A helper function to find the sections for a .dwz file. */
2656
2657static void
2658locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2659{
9a3c8263 2660 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2661
2662 /* Note that we only support the standard ELF names, because .dwz
2663 is ELF-only (at the time of writing). */
2664 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2665 {
049412e3 2666 dwz_file->abbrev.s.section = sectp;
fd361982 2667 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2668 }
2669 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2670 {
049412e3 2671 dwz_file->info.s.section = sectp;
fd361982 2672 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2673 }
2674 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2675 {
049412e3 2676 dwz_file->str.s.section = sectp;
fd361982 2677 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2678 }
2679 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2680 {
049412e3 2681 dwz_file->line.s.section = sectp;
fd361982 2682 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2683 }
2684 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2685 {
049412e3 2686 dwz_file->macro.s.section = sectp;
fd361982 2687 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2688 }
2ec9a5e0
TT
2689 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2690 {
049412e3 2691 dwz_file->gdb_index.s.section = sectp;
fd361982 2692 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2693 }
927aa2e7
JK
2694 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2695 {
2696 dwz_file->debug_names.s.section = sectp;
fd361982 2697 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2698 }
36586728
TT
2699}
2700
c4973306 2701/* See dwarf2read.h. */
36586728 2702
c4973306 2703struct dwz_file *
ed2dc618 2704dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2705{
36586728 2706 const char *filename;
acd13123 2707 bfd_size_type buildid_len_arg;
dc294be5
TT
2708 size_t buildid_len;
2709 bfd_byte *buildid;
36586728
TT
2710
2711 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2712 return dwarf2_per_objfile->dwz_file.get ();
36586728 2713
4db1a1dc 2714 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2715 gdb::unique_xmalloc_ptr<char> data
2716 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2717 &buildid_len_arg, &buildid));
4db1a1dc
TT
2718 if (data == NULL)
2719 {
2720 if (bfd_get_error () == bfd_error_no_error)
2721 return NULL;
2722 error (_("could not read '.gnu_debugaltlink' section: %s"),
2723 bfd_errmsg (bfd_get_error ()));
2724 }
791afaa2
TT
2725
2726 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2727
acd13123
TT
2728 buildid_len = (size_t) buildid_len_arg;
2729
791afaa2 2730 filename = data.get ();
d721ba37
PA
2731
2732 std::string abs_storage;
36586728
TT
2733 if (!IS_ABSOLUTE_PATH (filename))
2734 {
14278e1f
TT
2735 gdb::unique_xmalloc_ptr<char> abs
2736 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2737
14278e1f 2738 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2739 filename = abs_storage.c_str ();
36586728
TT
2740 }
2741
dc294be5
TT
2742 /* First try the file name given in the section. If that doesn't
2743 work, try to use the build-id instead. */
192b62ce 2744 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2745 if (dwz_bfd != NULL)
36586728 2746 {
192b62ce 2747 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2748 dwz_bfd.reset (nullptr);
36586728
TT
2749 }
2750
dc294be5
TT
2751 if (dwz_bfd == NULL)
2752 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2753
2754 if (dwz_bfd == NULL)
2755 error (_("could not find '.gnu_debugaltlink' file for %s"),
2756 objfile_name (dwarf2_per_objfile->objfile));
2757
7ff8cb8c
TT
2758 std::unique_ptr<struct dwz_file> result
2759 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2760
7ff8cb8c
TT
2761 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2762 result.get ());
36586728 2763
7ff8cb8c
TT
2764 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2765 result->dwz_bfd.get ());
2766 dwarf2_per_objfile->dwz_file = std::move (result);
2767 return dwarf2_per_objfile->dwz_file.get ();
36586728 2768}
9291a0cd 2769\f
7b9f3c50
DE
2770/* DWARF quick_symbols_functions support. */
2771
2772/* TUs can share .debug_line entries, and there can be a lot more TUs than
2773 unique line tables, so we maintain a separate table of all .debug_line
2774 derived entries to support the sharing.
2775 All the quick functions need is the list of file names. We discard the
2776 line_header when we're done and don't need to record it here. */
2777struct quick_file_names
2778{
094b34ac
DE
2779 /* The data used to construct the hash key. */
2780 struct stmt_list_hash hash;
7b9f3c50
DE
2781
2782 /* The number of entries in file_names, real_names. */
2783 unsigned int num_file_names;
2784
2785 /* The file names from the line table, after being run through
2786 file_full_name. */
2787 const char **file_names;
2788
2789 /* The file names from the line table after being run through
2790 gdb_realpath. These are computed lazily. */
2791 const char **real_names;
2792};
2793
2794/* When using the index (and thus not using psymtabs), each CU has an
2795 object of this type. This is used to hold information needed by
2796 the various "quick" methods. */
2797struct dwarf2_per_cu_quick_data
2798{
2799 /* The file table. This can be NULL if there was no file table
2800 or it's currently not read in.
2801 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2802 struct quick_file_names *file_names;
2803
2804 /* The corresponding symbol table. This is NULL if symbols for this
2805 CU have not yet been read. */
43f3e411 2806 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2807
2808 /* A temporary mark bit used when iterating over all CUs in
2809 expand_symtabs_matching. */
2810 unsigned int mark : 1;
2811
2812 /* True if we've tried to read the file table and found there isn't one.
2813 There will be no point in trying to read it again next time. */
2814 unsigned int no_file_data : 1;
2815};
2816
094b34ac
DE
2817/* Utility hash function for a stmt_list_hash. */
2818
2819static hashval_t
2820hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2821{
2822 hashval_t v = 0;
2823
2824 if (stmt_list_hash->dwo_unit != NULL)
2825 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2826 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2827 return v;
2828}
2829
2830/* Utility equality function for a stmt_list_hash. */
2831
2832static int
2833eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2834 const struct stmt_list_hash *rhs)
2835{
2836 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2837 return 0;
2838 if (lhs->dwo_unit != NULL
2839 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2840 return 0;
2841
9c541725 2842 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2843}
2844
7b9f3c50
DE
2845/* Hash function for a quick_file_names. */
2846
2847static hashval_t
2848hash_file_name_entry (const void *e)
2849{
9a3c8263
SM
2850 const struct quick_file_names *file_data
2851 = (const struct quick_file_names *) e;
7b9f3c50 2852
094b34ac 2853 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2854}
2855
2856/* Equality function for a quick_file_names. */
2857
2858static int
2859eq_file_name_entry (const void *a, const void *b)
2860{
9a3c8263
SM
2861 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2862 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2863
094b34ac 2864 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2865}
2866
2867/* Delete function for a quick_file_names. */
2868
2869static void
2870delete_file_name_entry (void *e)
2871{
9a3c8263 2872 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2873 int i;
2874
2875 for (i = 0; i < file_data->num_file_names; ++i)
2876 {
2877 xfree ((void*) file_data->file_names[i]);
2878 if (file_data->real_names)
2879 xfree ((void*) file_data->real_names[i]);
2880 }
2881
2882 /* The space for the struct itself lives on objfile_obstack,
2883 so we don't free it here. */
2884}
2885
2886/* Create a quick_file_names hash table. */
2887
2888static htab_t
2889create_quick_file_names_table (unsigned int nr_initial_entries)
2890{
2891 return htab_create_alloc (nr_initial_entries,
2892 hash_file_name_entry, eq_file_name_entry,
2893 delete_file_name_entry, xcalloc, xfree);
2894}
9291a0cd 2895
918dd910
JK
2896/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2897 have to be created afterwards. You should call age_cached_comp_units after
2898 processing PER_CU->CU. dw2_setup must have been already called. */
2899
2900static void
58f0c718 2901load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2902{
3019eac3 2903 if (per_cu->is_debug_types)
e5fe5e75 2904 load_full_type_unit (per_cu);
918dd910 2905 else
58f0c718 2906 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2907
cc12ce38
DE
2908 if (per_cu->cu == NULL)
2909 return; /* Dummy CU. */
2dc860c0
DE
2910
2911 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2912}
2913
a0f42c21 2914/* Read in the symbols for PER_CU. */
2fdf6df6 2915
9291a0cd 2916static void
58f0c718 2917dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2918{
ed2dc618 2919 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2920
f4dc4d17
DE
2921 /* Skip type_unit_groups, reading the type units they contain
2922 is handled elsewhere. */
2923 if (IS_TYPE_UNIT_GROUP (per_cu))
2924 return;
2925
b303c6f6
AB
2926 /* The destructor of dwarf2_queue_guard frees any entries left on
2927 the queue. After this point we're guaranteed to leave this function
2928 with the dwarf queue empty. */
2929 dwarf2_queue_guard q_guard;
9291a0cd 2930
95554aad 2931 if (dwarf2_per_objfile->using_index
43f3e411 2932 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2933 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2934 {
2935 queue_comp_unit (per_cu, language_minimal);
58f0c718 2936 load_cu (per_cu, skip_partial);
89e63ee4
DE
2937
2938 /* If we just loaded a CU from a DWO, and we're working with an index
2939 that may badly handle TUs, load all the TUs in that DWO as well.
2940 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2941 if (!per_cu->is_debug_types
cc12ce38 2942 && per_cu->cu != NULL
89e63ee4
DE
2943 && per_cu->cu->dwo_unit != NULL
2944 && dwarf2_per_objfile->index_table != NULL
2945 && dwarf2_per_objfile->index_table->version <= 7
2946 /* DWP files aren't supported yet. */
ed2dc618 2947 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2948 queue_and_load_all_dwo_tus (per_cu);
95554aad 2949 }
9291a0cd 2950
ed2dc618 2951 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2952
2953 /* Age the cache, releasing compilation units that have not
2954 been used recently. */
ed2dc618 2955 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2956}
2957
2958/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2959 the objfile from which this CU came. Returns the resulting symbol
2960 table. */
2fdf6df6 2961
43f3e411 2962static struct compunit_symtab *
58f0c718 2963dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2964{
ed2dc618
SM
2965 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2966
95554aad 2967 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2968 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2969 {
11ed8cad 2970 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2971 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2972 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2973 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2974 }
f194fefb 2975
43f3e411 2976 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2977}
2978
ff4c9fec 2979/* See declaration. */
f4dc4d17 2980
ff4c9fec
SM
2981dwarf2_per_cu_data *
2982dwarf2_per_objfile::get_cutu (int index)
2983{
b76e467d 2984 if (index >= this->all_comp_units.size ())
ff4c9fec 2985 {
b76e467d 2986 index -= this->all_comp_units.size ();
b2bdb8cf 2987 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2988 return &this->all_type_units[index]->per_cu;
2989 }
f4dc4d17 2990
ff4c9fec
SM
2991 return this->all_comp_units[index];
2992}
f4dc4d17 2993
ff4c9fec 2994/* See declaration. */
2fdf6df6 2995
ff4c9fec
SM
2996dwarf2_per_cu_data *
2997dwarf2_per_objfile::get_cu (int index)
1fd400ff 2998{
b76e467d 2999 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 3000
ff4c9fec 3001 return this->all_comp_units[index];
f4dc4d17
DE
3002}
3003
ff4c9fec 3004/* See declaration. */
f4dc4d17 3005
ff4c9fec
SM
3006signatured_type *
3007dwarf2_per_objfile::get_tu (int index)
f4dc4d17 3008{
b2bdb8cf 3009 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 3010
ff4c9fec 3011 return this->all_type_units[index];
1fd400ff
TT
3012}
3013
4b514bc8
JK
3014/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3015 objfile_obstack, and constructed with the specified field
3016 values. */
3017
3018static dwarf2_per_cu_data *
ed2dc618 3019create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
3020 struct dwarf2_section_info *section,
3021 int is_dwz,
3022 sect_offset sect_off, ULONGEST length)
3023{
ed2dc618 3024 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
3025 dwarf2_per_cu_data *the_cu
3026 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3027 struct dwarf2_per_cu_data);
3028 the_cu->sect_off = sect_off;
3029 the_cu->length = length;
e3b94546 3030 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
3031 the_cu->section = section;
3032 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3033 struct dwarf2_per_cu_quick_data);
3034 the_cu->is_dwz = is_dwz;
3035 return the_cu;
3036}
3037
2ec9a5e0
TT
3038/* A helper for create_cus_from_index that handles a given list of
3039 CUs. */
2fdf6df6 3040
74a0d9f6 3041static void
12359b5e 3042create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3043 const gdb_byte *cu_list, offset_type n_elements,
3044 struct dwarf2_section_info *section,
b76e467d 3045 int is_dwz)
9291a0cd 3046{
12359b5e 3047 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3048 {
74a0d9f6 3049 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3050
3051 sect_offset sect_off
3052 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3053 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3054 cu_list += 2 * 8;
3055
b76e467d 3056 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3057 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3058 sect_off, length);
b76e467d 3059 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3060 }
9291a0cd
TT
3061}
3062
2ec9a5e0 3063/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3064 the CU objects for this objfile. */
2ec9a5e0 3065
74a0d9f6 3066static void
12359b5e 3067create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3068 const gdb_byte *cu_list, offset_type cu_list_elements,
3069 const gdb_byte *dwz_list, offset_type dwz_elements)
3070{
b76e467d
SM
3071 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3072 dwarf2_per_objfile->all_comp_units.reserve
3073 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3074
12359b5e 3075 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3076 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3077
3078 if (dwz_elements == 0)
74a0d9f6 3079 return;
2ec9a5e0 3080
12359b5e
SM
3081 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3082 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3083 &dwz->info, 1);
2ec9a5e0
TT
3084}
3085
1fd400ff 3086/* Create the signatured type hash table from the index. */
673bfd45 3087
74a0d9f6 3088static void
12359b5e
SM
3089create_signatured_type_table_from_index
3090 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3091 struct dwarf2_section_info *section,
3092 const gdb_byte *bytes,
3093 offset_type elements)
1fd400ff 3094{
12359b5e 3095 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3096
b2bdb8cf
SM
3097 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3098 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3099
12359b5e 3100 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3101
12359b5e 3102 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3103 {
52dc124a 3104 struct signatured_type *sig_type;
9c541725 3105 ULONGEST signature;
1fd400ff 3106 void **slot;
9c541725 3107 cu_offset type_offset_in_tu;
1fd400ff 3108
74a0d9f6 3109 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3110 sect_offset sect_off
3111 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3112 type_offset_in_tu
3113 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3114 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3115 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3116 bytes += 3 * 8;
3117
52dc124a 3118 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3119 struct signatured_type);
52dc124a 3120 sig_type->signature = signature;
9c541725 3121 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3122 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3123 sig_type->per_cu.section = section;
9c541725 3124 sig_type->per_cu.sect_off = sect_off;
e3b94546 3125 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3126 sig_type->per_cu.v.quick
1fd400ff
TT
3127 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3128 struct dwarf2_per_cu_quick_data);
3129
52dc124a
DE
3130 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3131 *slot = sig_type;
1fd400ff 3132
b2bdb8cf 3133 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3134 }
3135
673bfd45 3136 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3137}
3138
927aa2e7
JK
3139/* Create the signatured type hash table from .debug_names. */
3140
3141static void
3142create_signatured_type_table_from_debug_names
ed2dc618 3143 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3144 const mapped_debug_names &map,
3145 struct dwarf2_section_info *section,
3146 struct dwarf2_section_info *abbrev_section)
3147{
ed2dc618
SM
3148 struct objfile *objfile = dwarf2_per_objfile->objfile;
3149
927aa2e7
JK
3150 dwarf2_read_section (objfile, section);
3151 dwarf2_read_section (objfile, abbrev_section);
3152
b2bdb8cf
SM
3153 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3154 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3155
3156 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3157
3158 for (uint32_t i = 0; i < map.tu_count; ++i)
3159 {
3160 struct signatured_type *sig_type;
927aa2e7 3161 void **slot;
927aa2e7
JK
3162
3163 sect_offset sect_off
3164 = (sect_offset) (extract_unsigned_integer
3165 (map.tu_table_reordered + i * map.offset_size,
3166 map.offset_size,
3167 map.dwarf5_byte_order));
3168
3169 comp_unit_head cu_header;
ed2dc618
SM
3170 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3171 abbrev_section,
927aa2e7
JK
3172 section->buffer + to_underlying (sect_off),
3173 rcuh_kind::TYPE);
3174
3175 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3176 struct signatured_type);
3177 sig_type->signature = cu_header.signature;
3178 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3179 sig_type->per_cu.is_debug_types = 1;
3180 sig_type->per_cu.section = section;
3181 sig_type->per_cu.sect_off = sect_off;
e3b94546 3182 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3183 sig_type->per_cu.v.quick
3184 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3185 struct dwarf2_per_cu_quick_data);
3186
3187 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3188 *slot = sig_type;
3189
b2bdb8cf 3190 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3191 }
3192
3193 dwarf2_per_objfile->signatured_types = sig_types_hash;
3194}
3195
9291a0cd
TT
3196/* Read the address map data from the mapped index, and use it to
3197 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3198
9291a0cd 3199static void
ed2dc618
SM
3200create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3201 struct mapped_index *index)
9291a0cd 3202{
ed2dc618 3203 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3204 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3205 const gdb_byte *iter, *end;
9291a0cd 3206 struct addrmap *mutable_map;
9291a0cd
TT
3207 CORE_ADDR baseaddr;
3208
8268c778
PA
3209 auto_obstack temp_obstack;
3210
9291a0cd
TT
3211 mutable_map = addrmap_create_mutable (&temp_obstack);
3212
f00a2de2
PA
3213 iter = index->address_table.data ();
3214 end = iter + index->address_table.size ();
9291a0cd 3215
b3b3bada 3216 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
3217
3218 while (iter < end)
3219 {
3220 ULONGEST hi, lo, cu_index;
3221 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3222 iter += 8;
3223 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3224 iter += 8;
3225 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3226 iter += 4;
f652bce2 3227
24a55014 3228 if (lo > hi)
f652bce2 3229 {
b98664d3 3230 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3231 hex_string (lo), hex_string (hi));
24a55014 3232 continue;
f652bce2 3233 }
24a55014 3234
b76e467d 3235 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3236 {
b98664d3 3237 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3238 (unsigned) cu_index);
24a55014 3239 continue;
f652bce2 3240 }
24a55014 3241
79748972
TT
3242 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3243 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3244 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3245 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3246 }
3247
d320c2b5 3248 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3249 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3250}
3251
927aa2e7
JK
3252/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3253 populate the objfile's psymtabs_addrmap. */
3254
3255static void
ed2dc618 3256create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3257 struct dwarf2_section_info *section)
3258{
ed2dc618 3259 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3260 bfd *abfd = objfile->obfd;
3261 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 3262 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
3263
3264 auto_obstack temp_obstack;
3265 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3266
3267 std::unordered_map<sect_offset,
3268 dwarf2_per_cu_data *,
3269 gdb::hash_enum<sect_offset>>
3270 debug_info_offset_to_per_cu;
b76e467d 3271 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3272 {
927aa2e7
JK
3273 const auto insertpair
3274 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3275 if (!insertpair.second)
3276 {
3277 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3278 "debug_info_offset %s, ignoring .debug_aranges."),
3279 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3280 return;
3281 }
3282 }
3283
3284 dwarf2_read_section (objfile, section);
3285
3286 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3287
3288 const gdb_byte *addr = section->buffer;
3289
3290 while (addr < section->buffer + section->size)
3291 {
3292 const gdb_byte *const entry_addr = addr;
3293 unsigned int bytes_read;
3294
3295 const LONGEST entry_length = read_initial_length (abfd, addr,
3296 &bytes_read);
3297 addr += bytes_read;
3298
3299 const gdb_byte *const entry_end = addr + entry_length;
3300 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3301 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3302 if (addr + entry_length > section->buffer + section->size)
3303 {
47e3f474 3304 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3305 "length %s exceeds section length %s, "
3306 "ignoring .debug_aranges."),
47e3f474
TV
3307 objfile_name (objfile),
3308 plongest (entry_addr - section->buffer),
927aa2e7
JK
3309 plongest (bytes_read + entry_length),
3310 pulongest (section->size));
3311 return;
3312 }
3313
3314 /* The version number. */
3315 const uint16_t version = read_2_bytes (abfd, addr);
3316 addr += 2;
3317 if (version != 2)
3318 {
47e3f474 3319 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3320 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3321 objfile_name (objfile),
3322 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3323 return;
3324 }
3325
3326 const uint64_t debug_info_offset
3327 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3328 addr += offset_size;
3329 const auto per_cu_it
3330 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3331 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3332 {
47e3f474 3333 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3334 "debug_info_offset %s does not exists, "
3335 "ignoring .debug_aranges."),
47e3f474
TV
3336 objfile_name (objfile),
3337 plongest (entry_addr - section->buffer),
927aa2e7
JK
3338 pulongest (debug_info_offset));
3339 return;
3340 }
3341 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3342
3343 const uint8_t address_size = *addr++;
3344 if (address_size < 1 || address_size > 8)
3345 {
47e3f474 3346 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3347 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3348 objfile_name (objfile),
3349 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3350 return;
3351 }
3352
3353 const uint8_t segment_selector_size = *addr++;
3354 if (segment_selector_size != 0)
3355 {
47e3f474 3356 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3357 "segment_selector_size %u is not supported, "
3358 "ignoring .debug_aranges."),
47e3f474
TV
3359 objfile_name (objfile),
3360 plongest (entry_addr - section->buffer),
927aa2e7
JK
3361 segment_selector_size);
3362 return;
3363 }
3364
3365 /* Must pad to an alignment boundary that is twice the address
3366 size. It is undocumented by the DWARF standard but GCC does
3367 use it. */
3368 for (size_t padding = ((-(addr - section->buffer))
3369 & (2 * address_size - 1));
3370 padding > 0; padding--)
3371 if (*addr++ != 0)
3372 {
47e3f474 3373 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3374 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3375 objfile_name (objfile),
3376 plongest (entry_addr - section->buffer));
927aa2e7
JK
3377 return;
3378 }
3379
3380 for (;;)
3381 {
3382 if (addr + 2 * address_size > entry_end)
3383 {
47e3f474 3384 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3385 "address list is not properly terminated, "
3386 "ignoring .debug_aranges."),
47e3f474
TV
3387 objfile_name (objfile),
3388 plongest (entry_addr - section->buffer));
927aa2e7
JK
3389 return;
3390 }
3391 ULONGEST start = extract_unsigned_integer (addr, address_size,
3392 dwarf5_byte_order);
3393 addr += address_size;
3394 ULONGEST length = extract_unsigned_integer (addr, address_size,
3395 dwarf5_byte_order);
3396 addr += address_size;
3397 if (start == 0 && length == 0)
3398 break;
3399 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3400 {
3401 /* Symbol was eliminated due to a COMDAT group. */
3402 continue;
3403 }
3404 ULONGEST end = start + length;
79748972
TT
3405 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3406 - baseaddr);
3407 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3408 - baseaddr);
927aa2e7
JK
3409 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3410 }
3411 }
3412
d320c2b5 3413 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3414 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3415}
3416
9291a0cd
TT
3417/* Find a slot in the mapped index INDEX for the object named NAME.
3418 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3419 constant pool and return true. If NAME cannot be found, return
3420 false. */
2fdf6df6 3421
109483d9 3422static bool
9291a0cd
TT
3423find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3424 offset_type **vec_out)
3425{
0cf03b49 3426 offset_type hash;
9291a0cd 3427 offset_type slot, step;
559a7a62 3428 int (*cmp) (const char *, const char *);
9291a0cd 3429
791afaa2 3430 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3431 if (current_language->la_language == language_cplus
45280282
IB
3432 || current_language->la_language == language_fortran
3433 || current_language->la_language == language_d)
0cf03b49
JK
3434 {
3435 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3436 not contain any. */
a8719064 3437
72998fb3 3438 if (strchr (name, '(') != NULL)
0cf03b49 3439 {
109483d9 3440 without_params = cp_remove_params (name);
0cf03b49 3441
72998fb3 3442 if (without_params != NULL)
791afaa2 3443 name = without_params.get ();
0cf03b49
JK
3444 }
3445 }
3446
559a7a62 3447 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3448 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3449 simulate our NAME being searched is also lowercased. */
3450 hash = mapped_index_string_hash ((index->version == 4
3451 && case_sensitivity == case_sensitive_off
3452 ? 5 : index->version),
3453 name);
3454
f00a2de2
PA
3455 slot = hash & (index->symbol_table.size () - 1);
3456 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3457 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3458
3459 for (;;)
3460 {
9291a0cd 3461 const char *str;
f00a2de2
PA
3462
3463 const auto &bucket = index->symbol_table[slot];
3464 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3465 return false;
9291a0cd 3466
f00a2de2 3467 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3468 if (!cmp (name, str))
9291a0cd
TT
3469 {
3470 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3471 + MAYBE_SWAP (bucket.vec));
109483d9 3472 return true;
9291a0cd
TT
3473 }
3474
f00a2de2 3475 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3476 }
3477}
3478
4485a1c1
SM
3479/* A helper function that reads the .gdb_index from BUFFER and fills
3480 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3481 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3482 ok to use deprecated sections.
3483
3484 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3485 out parameters that are filled in with information about the CU and
3486 TU lists in the section.
3487
4485a1c1 3488 Returns true if all went well, false otherwise. */
2fdf6df6 3489
d33bc52e 3490static bool
4485a1c1
SM
3491read_gdb_index_from_buffer (struct objfile *objfile,
3492 const char *filename,
3493 bool deprecated_ok,
3494 gdb::array_view<const gdb_byte> buffer,
3495 struct mapped_index *map,
3496 const gdb_byte **cu_list,
3497 offset_type *cu_list_elements,
3498 const gdb_byte **types_list,
3499 offset_type *types_list_elements)
3500{
3501 const gdb_byte *addr = &buffer[0];
82430852 3502
9291a0cd 3503 /* Version check. */
4485a1c1 3504 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3505 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3506 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3507 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3508 indices. */
831adc1f 3509 if (version < 4)
481860b3
GB
3510 {
3511 static int warning_printed = 0;
3512 if (!warning_printed)
3513 {
3514 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3515 filename);
481860b3
GB
3516 warning_printed = 1;
3517 }
3518 return 0;
3519 }
3520 /* Index version 4 uses a different hash function than index version
3521 5 and later.
3522
3523 Versions earlier than 6 did not emit psymbols for inlined
3524 functions. Using these files will cause GDB not to be able to
3525 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3526 indices unless the user has done
3527 "set use-deprecated-index-sections on". */
2ec9a5e0 3528 if (version < 6 && !deprecated_ok)
481860b3
GB
3529 {
3530 static int warning_printed = 0;
3531 if (!warning_printed)
3532 {
e615022a
DE
3533 warning (_("\
3534Skipping deprecated .gdb_index section in %s.\n\
3535Do \"set use-deprecated-index-sections on\" before the file is read\n\
3536to use the section anyway."),
2ec9a5e0 3537 filename);
481860b3
GB
3538 warning_printed = 1;
3539 }
3540 return 0;
3541 }
796a7ff8 3542 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3543 of the TU (for symbols coming from TUs),
3544 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3545 Plus gold-generated indices can have duplicate entries for global symbols,
3546 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3547 These are just performance bugs, and we can't distinguish gdb-generated
3548 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3549
481860b3 3550 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3551 longer backward compatible. */
796a7ff8 3552 if (version > 8)
594e8718 3553 return 0;
9291a0cd 3554
559a7a62 3555 map->version = version;
9291a0cd 3556
4485a1c1 3557 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3558
4485a1c1 3559 int i = 0;
2ec9a5e0
TT
3560 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3561 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3562 / 8);
1fd400ff
TT
3563 ++i;
3564
2ec9a5e0
TT
3565 *types_list = addr + MAYBE_SWAP (metadata[i]);
3566 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3567 - MAYBE_SWAP (metadata[i]))
3568 / 8);
987d643c 3569 ++i;
1fd400ff 3570
f00a2de2
PA
3571 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3572 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3573 map->address_table
3574 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3575 ++i;
3576
f00a2de2
PA
3577 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3578 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3579 map->symbol_table
3580 = gdb::array_view<mapped_index::symbol_table_slot>
3581 ((mapped_index::symbol_table_slot *) symbol_table,
3582 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3583
f00a2de2 3584 ++i;
f9d83a0b 3585 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3586
2ec9a5e0
TT
3587 return 1;
3588}
3589
4485a1c1
SM
3590/* Callback types for dwarf2_read_gdb_index. */
3591
3592typedef gdb::function_view
3593 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3594 get_gdb_index_contents_ftype;
3595typedef gdb::function_view
3596 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3597 get_gdb_index_contents_dwz_ftype;
3598
927aa2e7 3599/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3600 elements of all the CUs and return 1. Otherwise, return 0. */
3601
3602static int
4485a1c1
SM
3603dwarf2_read_gdb_index
3604 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3605 get_gdb_index_contents_ftype get_gdb_index_contents,
3606 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3607{
2ec9a5e0
TT
3608 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3609 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3610 struct dwz_file *dwz;
12359b5e 3611 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3612
4485a1c1
SM
3613 gdb::array_view<const gdb_byte> main_index_contents
3614 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3615
3616 if (main_index_contents.empty ())
3617 return 0;
3618
3063847f 3619 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3620 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3621 use_deprecated_index_sections,
3622 main_index_contents, map.get (), &cu_list,
3623 &cu_list_elements, &types_list,
3624 &types_list_elements))
2ec9a5e0
TT
3625 return 0;
3626
0fefef59 3627 /* Don't use the index if it's empty. */
3063847f 3628 if (map->symbol_table.empty ())
0fefef59
DE
3629 return 0;
3630
2ec9a5e0
TT
3631 /* If there is a .dwz file, read it so we can get its CU list as
3632 well. */
ed2dc618 3633 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3634 if (dwz != NULL)
2ec9a5e0 3635 {
2ec9a5e0
TT
3636 struct mapped_index dwz_map;
3637 const gdb_byte *dwz_types_ignore;
3638 offset_type dwz_types_elements_ignore;
3639
4485a1c1
SM
3640 gdb::array_view<const gdb_byte> dwz_index_content
3641 = get_gdb_index_contents_dwz (objfile, dwz);
3642
3643 if (dwz_index_content.empty ())
3644 return 0;
3645
3646 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3647 bfd_get_filename (dwz->dwz_bfd.get ()),
3648 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3649 &dwz_list, &dwz_list_elements,
3650 &dwz_types_ignore,
3651 &dwz_types_elements_ignore))
2ec9a5e0
TT
3652 {
3653 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3654 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3655 return 0;
3656 }
3657 }
3658
12359b5e
SM
3659 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3660 dwz_list, dwz_list_elements);
1fd400ff 3661
8b70b953
TT
3662 if (types_list_elements)
3663 {
8b70b953
TT
3664 /* We can only handle a single .debug_types when we have an
3665 index. */
fd5866f6 3666 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3667 return 0;
3668
fd5866f6 3669 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3670
12359b5e
SM
3671 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3672 types_list, types_list_elements);
8b70b953 3673 }
9291a0cd 3674
3063847f 3675 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3676
3063847f 3677 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3678 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3679 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3680 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3681
3682 return 1;
3683}
3684
dee91e82 3685/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3686
dee91e82
DE
3687static void
3688dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3689 const gdb_byte *info_ptr,
dee91e82 3690 struct die_info *comp_unit_die,
c0ab21c2 3691 int has_children)
9291a0cd 3692{
dee91e82 3693 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3694 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3695 struct dwarf2_per_objfile *dwarf2_per_objfile
3696 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3697 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3698 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3699 struct attribute *attr;
7b9f3c50
DE
3700 void **slot;
3701 struct quick_file_names *qfn;
9291a0cd 3702
0186c6a7
DE
3703 gdb_assert (! this_cu->is_debug_types);
3704
07261596
TT
3705 /* Our callers never want to match partial units -- instead they
3706 will match the enclosing full CU. */
3707 if (comp_unit_die->tag == DW_TAG_partial_unit)
3708 {
3709 this_cu->v.quick->no_file_data = 1;
3710 return;
3711 }
3712
0186c6a7 3713 lh_cu = this_cu;
7b9f3c50 3714 slot = NULL;
dee91e82 3715
fff8551c 3716 line_header_up lh;
9c541725 3717 sect_offset line_offset {};
fff8551c 3718
dee91e82 3719 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3720 if (attr != nullptr)
9291a0cd 3721 {
7b9f3c50
DE
3722 struct quick_file_names find_entry;
3723
9c541725 3724 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3725
3726 /* We may have already read in this line header (TU line header sharing).
3727 If we have we're done. */
094b34ac 3728 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3729 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3730 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3731 &find_entry, INSERT);
3732 if (*slot != NULL)
3733 {
9a3c8263 3734 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3735 return;
7b9f3c50
DE
3736 }
3737
3019eac3 3738 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3739 }
3740 if (lh == NULL)
3741 {
094b34ac 3742 lh_cu->v.quick->no_file_data = 1;
dee91e82 3743 return;
9291a0cd
TT
3744 }
3745
8d749320 3746 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3747 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3748 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3749 gdb_assert (slot != NULL);
3750 *slot = qfn;
9291a0cd 3751
d721ba37 3752 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3753
aa391654
TT
3754 int offset = 0;
3755 if (strcmp (fnd.name, "<unknown>") != 0)
3756 ++offset;
3757
7ba99d21 3758 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3759 qfn->file_names =
aa391654
TT
3760 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3761 if (offset != 0)
3762 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3763 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3764 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3765 qfn->real_names = NULL;
9291a0cd 3766
094b34ac 3767 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3768}
3769
3770/* A helper for the "quick" functions which attempts to read the line
3771 table for THIS_CU. */
3772
3773static struct quick_file_names *
e4a48d9d 3774dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3775{
0186c6a7
DE
3776 /* This should never be called for TUs. */
3777 gdb_assert (! this_cu->is_debug_types);
3778 /* Nor type unit groups. */
3779 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3780
dee91e82
DE
3781 if (this_cu->v.quick->file_names != NULL)
3782 return this_cu->v.quick->file_names;
3783 /* If we know there is no line data, no point in looking again. */
3784 if (this_cu->v.quick->no_file_data)
3785 return NULL;
3786
c0ab21c2
TT
3787 cutu_reader reader (this_cu);
3788 if (!reader.dummy_p)
3789 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3790 reader.has_children);
dee91e82
DE
3791
3792 if (this_cu->v.quick->no_file_data)
3793 return NULL;
3794 return this_cu->v.quick->file_names;
9291a0cd
TT
3795}
3796
3797/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3798 real path for a given file name from the line table. */
2fdf6df6 3799
9291a0cd 3800static const char *
7b9f3c50
DE
3801dw2_get_real_path (struct objfile *objfile,
3802 struct quick_file_names *qfn, int index)
9291a0cd 3803{
7b9f3c50
DE
3804 if (qfn->real_names == NULL)
3805 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3806 qfn->num_file_names, const char *);
9291a0cd 3807
7b9f3c50 3808 if (qfn->real_names[index] == NULL)
14278e1f 3809 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3810
7b9f3c50 3811 return qfn->real_names[index];
9291a0cd
TT
3812}
3813
3814static struct symtab *
3815dw2_find_last_source_symtab (struct objfile *objfile)
3816{
ed2dc618
SM
3817 struct dwarf2_per_objfile *dwarf2_per_objfile
3818 = get_dwarf2_per_objfile (objfile);
b76e467d 3819 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3820 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3821
43f3e411
DE
3822 if (cust == NULL)
3823 return NULL;
ed2dc618 3824
43f3e411 3825 return compunit_primary_filetab (cust);
9291a0cd
TT
3826}
3827
7b9f3c50
DE
3828/* Traversal function for dw2_forget_cached_source_info. */
3829
3830static int
3831dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3832{
7b9f3c50 3833 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3834
7b9f3c50 3835 if (file_data->real_names)
9291a0cd 3836 {
7b9f3c50 3837 int i;
9291a0cd 3838
7b9f3c50 3839 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3840 {
7b9f3c50
DE
3841 xfree ((void*) file_data->real_names[i]);
3842 file_data->real_names[i] = NULL;
9291a0cd
TT
3843 }
3844 }
7b9f3c50
DE
3845
3846 return 1;
3847}
3848
3849static void
3850dw2_forget_cached_source_info (struct objfile *objfile)
3851{
ed2dc618
SM
3852 struct dwarf2_per_objfile *dwarf2_per_objfile
3853 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3854
3855 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3856 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3857}
3858
f8eba3c6
TT
3859/* Helper function for dw2_map_symtabs_matching_filename that expands
3860 the symtabs and calls the iterator. */
3861
3862static int
3863dw2_map_expand_apply (struct objfile *objfile,
3864 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3865 const char *name, const char *real_path,
14bc53a8 3866 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3867{
43f3e411 3868 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3869
3870 /* Don't visit already-expanded CUs. */
43f3e411 3871 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3872 return 0;
3873
3874 /* This may expand more than one symtab, and we want to iterate over
3875 all of them. */
58f0c718 3876 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3877
14bc53a8
PA
3878 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3879 last_made, callback);
f8eba3c6
TT
3880}
3881
3882/* Implementation of the map_symtabs_matching_filename method. */
3883
14bc53a8
PA
3884static bool
3885dw2_map_symtabs_matching_filename
3886 (struct objfile *objfile, const char *name, const char *real_path,
3887 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3888{
c011a4f4 3889 const char *name_basename = lbasename (name);
ed2dc618
SM
3890 struct dwarf2_per_objfile *dwarf2_per_objfile
3891 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3892
848e3e78
DE
3893 /* The rule is CUs specify all the files, including those used by
3894 any TU, so there's no need to scan TUs here. */
f4dc4d17 3895
b76e467d 3896 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3897 {
3d7bb9d9 3898 /* We only need to look at symtabs not already expanded. */
43f3e411 3899 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3900 continue;
3901
b76e467d 3902 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3903 if (file_data == NULL)
9291a0cd
TT
3904 continue;
3905
b76e467d 3906 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3907 {
7b9f3c50 3908 const char *this_name = file_data->file_names[j];
da235a7c 3909 const char *this_real_name;
9291a0cd 3910
af529f8f 3911 if (compare_filenames_for_search (this_name, name))
9291a0cd 3912 {
f5b95b50 3913 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3914 callback))
3915 return true;
288e77a7 3916 continue;
4aac40c8 3917 }
9291a0cd 3918
c011a4f4
DE
3919 /* Before we invoke realpath, which can get expensive when many
3920 files are involved, do a quick comparison of the basenames. */
3921 if (! basenames_may_differ
3922 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3923 continue;
3924
da235a7c
JK
3925 this_real_name = dw2_get_real_path (objfile, file_data, j);
3926 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3927 {
da235a7c 3928 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3929 callback))
3930 return true;
288e77a7 3931 continue;
da235a7c 3932 }
9291a0cd 3933
da235a7c
JK
3934 if (real_path != NULL)
3935 {
af529f8f
JK
3936 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3937 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3938 if (this_real_name != NULL
af529f8f 3939 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3940 {
f5b95b50 3941 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3942 callback))
3943 return true;
288e77a7 3944 continue;
9291a0cd
TT
3945 }
3946 }
3947 }
3948 }
3949
14bc53a8 3950 return false;
9291a0cd
TT
3951}
3952
da51c347
DE
3953/* Struct used to manage iterating over all CUs looking for a symbol. */
3954
3955struct dw2_symtab_iterator
9291a0cd 3956{
ed2dc618
SM
3957 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3958 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3959 /* If set, only look for symbols that match that block. Valid values are
3960 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3961 gdb::optional<block_enum> block_index;
da51c347
DE
3962 /* The kind of symbol we're looking for. */
3963 domain_enum domain;
3964 /* The list of CUs from the index entry of the symbol,
3965 or NULL if not found. */
3966 offset_type *vec;
3967 /* The next element in VEC to look at. */
3968 int next;
3969 /* The number of elements in VEC, or zero if there is no match. */
3970 int length;
8943b874
DE
3971 /* Have we seen a global version of the symbol?
3972 If so we can ignore all further global instances.
3973 This is to work around gold/15646, inefficient gold-generated
3974 indices. */
3975 int global_seen;
da51c347 3976};
9291a0cd 3977
2b79f376 3978/* Initialize the index symtab iterator ITER. */
2fdf6df6 3979
9291a0cd 3980static void
da51c347 3981dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3982 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3983 gdb::optional<block_enum> block_index,
da51c347
DE
3984 domain_enum domain,
3985 const char *name)
3986{
ed2dc618 3987 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3988 iter->block_index = block_index;
3989 iter->domain = domain;
3990 iter->next = 0;
8943b874 3991 iter->global_seen = 0;
da51c347 3992
3063847f 3993 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3994
3995 /* index is NULL if OBJF_READNOW. */
3996 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3997 iter->length = MAYBE_SWAP (*iter->vec);
3998 else
3999 {
4000 iter->vec = NULL;
4001 iter->length = 0;
4002 }
4003}
4004
4005/* Return the next matching CU or NULL if there are no more. */
4006
4007static struct dwarf2_per_cu_data *
4008dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4009{
ed2dc618
SM
4010 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4011
da51c347
DE
4012 for ( ; iter->next < iter->length; ++iter->next)
4013 {
4014 offset_type cu_index_and_attrs =
4015 MAYBE_SWAP (iter->vec[iter->next + 1]);
4016 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
4017 gdb_index_symbol_kind symbol_kind =
4018 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4019 /* Only check the symbol attributes if they're present.
4020 Indices prior to version 7 don't record them,
4021 and indices >= 7 may elide them for certain symbols
4022 (gold does this). */
4023 int attrs_valid =
ed2dc618 4024 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
4025 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4026
3190f0c6 4027 /* Don't crash on bad data. */
b76e467d 4028 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4029 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 4030 {
b98664d3 4031 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
4032 " [in module %s]"),
4033 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4034 continue;
4035 }
4036
ff4c9fec 4037 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 4038
da51c347 4039 /* Skip if already read in. */
43f3e411 4040 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4041 continue;
4042
8943b874
DE
4043 /* Check static vs global. */
4044 if (attrs_valid)
4045 {
2b79f376
SM
4046 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4047
4048 if (iter->block_index.has_value ())
4049 {
4050 bool want_static = *iter->block_index == STATIC_BLOCK;
4051
4052 if (is_static != want_static)
4053 continue;
4054 }
4055
8943b874
DE
4056 /* Work around gold/15646. */
4057 if (!is_static && iter->global_seen)
4058 continue;
4059 if (!is_static)
4060 iter->global_seen = 1;
4061 }
da51c347
DE
4062
4063 /* Only check the symbol's kind if it has one. */
4064 if (attrs_valid)
4065 {
4066 switch (iter->domain)
4067 {
4068 case VAR_DOMAIN:
4069 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4070 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4071 /* Some types are also in VAR_DOMAIN. */
4072 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4073 continue;
4074 break;
4075 case STRUCT_DOMAIN:
4076 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4077 continue;
4078 break;
4079 case LABEL_DOMAIN:
4080 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4081 continue;
4082 break;
59c35742
AB
4083 case MODULE_DOMAIN:
4084 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4085 continue;
4086 break;
da51c347
DE
4087 default:
4088 break;
4089 }
4090 }
4091
4092 ++iter->next;
4093 return per_cu;
4094 }
4095
4096 return NULL;
4097}
4098
43f3e411 4099static struct compunit_symtab *
c7f839cb 4100dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4101 const char *name, domain_enum domain)
9291a0cd 4102{
43f3e411 4103 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4104 struct dwarf2_per_objfile *dwarf2_per_objfile
4105 = get_dwarf2_per_objfile (objfile);
9291a0cd 4106
b5ec771e
PA
4107 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4108
ed2dc618
SM
4109 struct dw2_symtab_iterator iter;
4110 struct dwarf2_per_cu_data *per_cu;
da51c347 4111
2b79f376 4112 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4113
ed2dc618
SM
4114 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4115 {
4116 struct symbol *sym, *with_opaque = NULL;
58f0c718 4117 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4118 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4119 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4120
ed2dc618
SM
4121 sym = block_find_symbol (block, name, domain,
4122 block_find_non_opaque_type_preferred,
4123 &with_opaque);
b2e2f908 4124
ed2dc618
SM
4125 /* Some caution must be observed with overloaded functions
4126 and methods, since the index will not contain any overload
4127 information (but NAME might contain it). */
da51c347 4128
ed2dc618
SM
4129 if (sym != NULL
4130 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4131 return stab;
4132 if (with_opaque != NULL
4133 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4134 stab_best = stab;
da51c347 4135
ed2dc618 4136 /* Keep looking through other CUs. */
9291a0cd 4137 }
9291a0cd 4138
da51c347 4139 return stab_best;
9291a0cd
TT
4140}
4141
4142static void
4143dw2_print_stats (struct objfile *objfile)
4144{
ed2dc618
SM
4145 struct dwarf2_per_objfile *dwarf2_per_objfile
4146 = get_dwarf2_per_objfile (objfile);
b76e467d 4147 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4148 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4149 int count = 0;
9291a0cd 4150
ed2dc618 4151 for (int i = 0; i < total; ++i)
9291a0cd 4152 {
ff4c9fec 4153 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4154
43f3e411 4155 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4156 ++count;
4157 }
e4a48d9d 4158 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4159 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4160}
4161
779bd270
DE
4162/* This dumps minimal information about the index.
4163 It is called via "mt print objfiles".
4164 One use is to verify .gdb_index has been loaded by the
4165 gdb.dwarf2/gdb-index.exp testcase. */
4166
9291a0cd
TT
4167static void
4168dw2_dump (struct objfile *objfile)
4169{
ed2dc618
SM
4170 struct dwarf2_per_objfile *dwarf2_per_objfile
4171 = get_dwarf2_per_objfile (objfile);
4172
779bd270
DE
4173 gdb_assert (dwarf2_per_objfile->using_index);
4174 printf_filtered (".gdb_index:");
4175 if (dwarf2_per_objfile->index_table != NULL)
4176 {
4177 printf_filtered (" version %d\n",
4178 dwarf2_per_objfile->index_table->version);
4179 }
4180 else
4181 printf_filtered (" faked for \"readnow\"\n");
4182 printf_filtered ("\n");
9291a0cd
TT
4183}
4184
9291a0cd
TT
4185static void
4186dw2_expand_symtabs_for_function (struct objfile *objfile,
4187 const char *func_name)
4188{
ed2dc618
SM
4189 struct dwarf2_per_objfile *dwarf2_per_objfile
4190 = get_dwarf2_per_objfile (objfile);
da51c347 4191
ed2dc618
SM
4192 struct dw2_symtab_iterator iter;
4193 struct dwarf2_per_cu_data *per_cu;
da51c347 4194
2b79f376 4195 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4196
ed2dc618 4197 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4198 dw2_instantiate_symtab (per_cu, false);
da51c347 4199
9291a0cd
TT
4200}
4201
4202static void
4203dw2_expand_all_symtabs (struct objfile *objfile)
4204{
ed2dc618
SM
4205 struct dwarf2_per_objfile *dwarf2_per_objfile
4206 = get_dwarf2_per_objfile (objfile);
b76e467d 4207 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4208 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4209
ed2dc618 4210 for (int i = 0; i < total_units; ++i)
9291a0cd 4211 {
ff4c9fec 4212 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4213
58f0c718
TT
4214 /* We don't want to directly expand a partial CU, because if we
4215 read it with the wrong language, then assertion failures can
4216 be triggered later on. See PR symtab/23010. So, tell
4217 dw2_instantiate_symtab to skip partial CUs -- any important
4218 partial CU will be read via DW_TAG_imported_unit anyway. */
4219 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4220 }
4221}
4222
4223static void
652a8996
JK
4224dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4225 const char *fullname)
9291a0cd 4226{
ed2dc618
SM
4227 struct dwarf2_per_objfile *dwarf2_per_objfile
4228 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4229
4230 /* We don't need to consider type units here.
4231 This is only called for examining code, e.g. expand_line_sal.
4232 There can be an order of magnitude (or more) more type units
4233 than comp units, and we avoid them if we can. */
4234
b76e467d 4235 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4236 {
3d7bb9d9 4237 /* We only need to look at symtabs not already expanded. */
43f3e411 4238 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4239 continue;
4240
b76e467d 4241 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4242 if (file_data == NULL)
9291a0cd
TT
4243 continue;
4244
b76e467d 4245 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4246 {
652a8996
JK
4247 const char *this_fullname = file_data->file_names[j];
4248
4249 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4250 {
58f0c718 4251 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4252 break;
4253 }
4254 }
4255 }
4256}
4257
9291a0cd 4258static void
199b4314
TT
4259dw2_map_matching_symbols
4260 (struct objfile *objfile,
b054970d 4261 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4262 int global,
4263 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4264 symbol_compare_ftype *ordered_compare)
9291a0cd 4265{
40658b94 4266 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4267 current language is Ada for a non-Ada objfile using GNU index. As Ada
4268 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4269}
4270
e1ef7d7a
PA
4271/* Starting from a search name, return the string that finds the upper
4272 bound of all strings that start with SEARCH_NAME in a sorted name
4273 list. Returns the empty string to indicate that the upper bound is
4274 the end of the list. */
4275
4276static std::string
4277make_sort_after_prefix_name (const char *search_name)
4278{
4279 /* When looking to complete "func", we find the upper bound of all
4280 symbols that start with "func" by looking for where we'd insert
4281 the closest string that would follow "func" in lexicographical
4282 order. Usually, that's "func"-with-last-character-incremented,
4283 i.e. "fund". Mind non-ASCII characters, though. Usually those
4284 will be UTF-8 multi-byte sequences, but we can't be certain.
4285 Especially mind the 0xff character, which is a valid character in
4286 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4287 rule out compilers allowing it in identifiers. Note that
4288 conveniently, strcmp/strcasecmp are specified to compare
4289 characters interpreted as unsigned char. So what we do is treat
4290 the whole string as a base 256 number composed of a sequence of
4291 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4292 to 0, and carries 1 to the following more-significant position.
4293 If the very first character in SEARCH_NAME ends up incremented
4294 and carries/overflows, then the upper bound is the end of the
4295 list. The string after the empty string is also the empty
4296 string.
4297
4298 Some examples of this operation:
4299
4300 SEARCH_NAME => "+1" RESULT
4301
4302 "abc" => "abd"
4303 "ab\xff" => "ac"
4304 "\xff" "a" "\xff" => "\xff" "b"
4305 "\xff" => ""
4306 "\xff\xff" => ""
4307 "" => ""
4308
4309 Then, with these symbols for example:
4310
4311 func
4312 func1
4313 fund
4314
4315 completing "func" looks for symbols between "func" and
4316 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4317 which finds "func" and "func1", but not "fund".
4318
4319 And with:
4320
4321 funcÿ (Latin1 'ÿ' [0xff])
4322 funcÿ1
4323 fund
4324
4325 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4326 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4327
4328 And with:
4329
4330 ÿÿ (Latin1 'ÿ' [0xff])
4331 ÿÿ1
4332
4333 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4334 the end of the list.
4335 */
4336 std::string after = search_name;
4337 while (!after.empty () && (unsigned char) after.back () == 0xff)
4338 after.pop_back ();
4339 if (!after.empty ())
4340 after.back () = (unsigned char) after.back () + 1;
4341 return after;
4342}
4343
5c58de74 4344/* See declaration. */
61d96d7e 4345
5c58de74
PA
4346std::pair<std::vector<name_component>::const_iterator,
4347 std::vector<name_component>::const_iterator>
44ed8f3e 4348mapped_index_base::find_name_components_bounds
3b00ef10 4349 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4350{
5c58de74
PA
4351 auto *name_cmp
4352 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4353
3b00ef10
TT
4354 const char *lang_name
4355 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4356
3f563c84
PA
4357 /* Comparison function object for lower_bound that matches against a
4358 given symbol name. */
4359 auto lookup_compare_lower = [&] (const name_component &elem,
4360 const char *name)
4361 {
5c58de74 4362 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4363 const char *elem_name = elem_qualified + elem.name_offset;
4364 return name_cmp (elem_name, name) < 0;
4365 };
4366
4367 /* Comparison function object for upper_bound that matches against a
4368 given symbol name. */
4369 auto lookup_compare_upper = [&] (const char *name,
4370 const name_component &elem)
4371 {
5c58de74 4372 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4373 const char *elem_name = elem_qualified + elem.name_offset;
4374 return name_cmp (name, elem_name) < 0;
4375 };
4376
5c58de74
PA
4377 auto begin = this->name_components.begin ();
4378 auto end = this->name_components.end ();
3f563c84
PA
4379
4380 /* Find the lower bound. */
4381 auto lower = [&] ()
4382 {
3b00ef10 4383 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4384 return begin;
4385 else
3b00ef10 4386 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4387 } ();
4388
4389 /* Find the upper bound. */
4390 auto upper = [&] ()
4391 {
5c58de74 4392 if (lookup_name_without_params.completion_mode ())
3f563c84 4393 {
e1ef7d7a
PA
4394 /* In completion mode, we want UPPER to point past all
4395 symbols names that have the same prefix. I.e., with
4396 these symbols, and completing "func":
4397
4398 function << lower bound
4399 function1
4400 other_function << upper bound
4401
4402 We find the upper bound by looking for the insertion
4403 point of "func"-with-last-character-incremented,
4404 i.e. "fund". */
3b00ef10 4405 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4406 if (after.empty ())
3f563c84 4407 return end;
e6b2f5ef
PA
4408 return std::lower_bound (lower, end, after.c_str (),
4409 lookup_compare_lower);
3f563c84
PA
4410 }
4411 else
3b00ef10 4412 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4413 } ();
4414
5c58de74
PA
4415 return {lower, upper};
4416}
4417
4418/* See declaration. */
4419
4420void
44ed8f3e 4421mapped_index_base::build_name_components ()
5c58de74
PA
4422{
4423 if (!this->name_components.empty ())
4424 return;
4425
4426 this->name_components_casing = case_sensitivity;
4427 auto *name_cmp
4428 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4429
4430 /* The code below only knows how to break apart components of C++
4431 symbol names (and other languages that use '::' as
3b00ef10 4432 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4433 auto count = this->symbol_name_count ();
4434 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4435 {
44ed8f3e 4436 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4437 continue;
4438
4439 const char *name = this->symbol_name_at (idx);
4440
4441 /* Add each name component to the name component table. */
4442 unsigned int previous_len = 0;
3b00ef10
TT
4443
4444 if (strstr (name, "::") != nullptr)
4445 {
4446 for (unsigned int current_len = cp_find_first_component (name);
4447 name[current_len] != '\0';
4448 current_len += cp_find_first_component (name + current_len))
4449 {
4450 gdb_assert (name[current_len] == ':');
4451 this->name_components.push_back ({previous_len, idx});
4452 /* Skip the '::'. */
4453 current_len += 2;
4454 previous_len = current_len;
4455 }
4456 }
4457 else
5c58de74 4458 {
3b00ef10
TT
4459 /* Handle the Ada encoded (aka mangled) form here. */
4460 for (const char *iter = strstr (name, "__");
4461 iter != nullptr;
4462 iter = strstr (iter, "__"))
4463 {
4464 this->name_components.push_back ({previous_len, idx});
4465 iter += 2;
4466 previous_len = iter - name;
4467 }
5c58de74 4468 }
3b00ef10 4469
5c58de74
PA
4470 this->name_components.push_back ({previous_len, idx});
4471 }
4472
4473 /* Sort name_components elements by name. */
4474 auto name_comp_compare = [&] (const name_component &left,
4475 const name_component &right)
4476 {
4477 const char *left_qualified = this->symbol_name_at (left.idx);
4478 const char *right_qualified = this->symbol_name_at (right.idx);
4479
4480 const char *left_name = left_qualified + left.name_offset;
4481 const char *right_name = right_qualified + right.name_offset;
4482
4483 return name_cmp (left_name, right_name) < 0;
4484 };
4485
4486 std::sort (this->name_components.begin (),
4487 this->name_components.end (),
4488 name_comp_compare);
4489}
4490
4491/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4492 mapped_index_base instead of the containing objfile. This is split
4493 to a separate function in order to be able to unit test the
4494 name_components matching using a mock mapped_index_base. For each
5c58de74 4495 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4496 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4497
4498static void
4499dw2_expand_symtabs_matching_symbol
44ed8f3e 4500 (mapped_index_base &index,
5c58de74
PA
4501 const lookup_name_info &lookup_name_in,
4502 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4503 enum search_domain kind,
3b00ef10 4504 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4505{
4506 lookup_name_info lookup_name_without_params
4507 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4508
4509 /* Build the symbol name component sorted vector, if we haven't
4510 yet. */
4511 index.build_name_components ();
4512
3f563c84
PA
4513 /* The same symbol may appear more than once in the range though.
4514 E.g., if we're looking for symbols that complete "w", and we have
4515 a symbol named "w1::w2", we'll find the two name components for
4516 that same symbol in the range. To be sure we only call the
4517 callback once per symbol, we first collect the symbol name
4518 indexes that matched in a temporary vector and ignore
4519 duplicates. */
4520 std::vector<offset_type> matches;
3f563c84 4521
3b00ef10
TT
4522 struct name_and_matcher
4523 {
4524 symbol_name_matcher_ftype *matcher;
4525 const std::string &name;
4526
4527 bool operator== (const name_and_matcher &other) const
3f563c84 4528 {
3b00ef10
TT
4529 return matcher == other.matcher && name == other.name;
4530 }
4531 };
4532
4533 /* A vector holding all the different symbol name matchers, for all
4534 languages. */
4535 std::vector<name_and_matcher> matchers;
4536
4537 for (int i = 0; i < nr_languages; i++)
4538 {
4539 enum language lang_e = (enum language) i;
4540
4541 const language_defn *lang = language_def (lang_e);
4542 symbol_name_matcher_ftype *name_matcher
4543 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4544
3b00ef10
TT
4545 name_and_matcher key {
4546 name_matcher,
4547 lookup_name_without_params.language_lookup_name (lang_e)
4548 };
4549
4550 /* Don't insert the same comparison routine more than once.
4551 Note that we do this linear walk. This is not a problem in
4552 practice because the number of supported languages is
4553 low. */
4554 if (std::find (matchers.begin (), matchers.end (), key)
4555 != matchers.end ())
9291a0cd 4556 continue;
3b00ef10
TT
4557 matchers.push_back (std::move (key));
4558
4559 auto bounds
4560 = index.find_name_components_bounds (lookup_name_without_params,
4561 lang_e);
4562
4563 /* Now for each symbol name in range, check to see if we have a name
4564 match, and if so, call the MATCH_CALLBACK callback. */
4565
4566 for (; bounds.first != bounds.second; ++bounds.first)
4567 {
4568 const char *qualified = index.symbol_name_at (bounds.first->idx);
4569
4570 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4571 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4572 continue;
9291a0cd 4573
3b00ef10
TT
4574 matches.push_back (bounds.first->idx);
4575 }
3f563c84
PA
4576 }
4577
4578 std::sort (matches.begin (), matches.end ());
4579
4580 /* Finally call the callback, once per match. */
4581 ULONGEST prev = -1;
4582 for (offset_type idx : matches)
4583 {
4584 if (prev != idx)
4585 {
3b00ef10
TT
4586 if (!match_callback (idx))
4587 break;
3f563c84
PA
4588 prev = idx;
4589 }
4590 }
4591
4592 /* Above we use a type wider than idx's for 'prev', since 0 and
4593 (offset_type)-1 are both possible values. */
4594 static_assert (sizeof (prev) > sizeof (offset_type), "");
4595}
4596
c62446b1
PA
4597#if GDB_SELF_TEST
4598
4599namespace selftests { namespace dw2_expand_symtabs_matching {
4600
a3c5fafd
PA
4601/* A mock .gdb_index/.debug_names-like name index table, enough to
4602 exercise dw2_expand_symtabs_matching_symbol, which works with the
4603 mapped_index_base interface. Builds an index from the symbol list
4604 passed as parameter to the constructor. */
4605class mock_mapped_index : public mapped_index_base
c62446b1
PA
4606{
4607public:
a3c5fafd
PA
4608 mock_mapped_index (gdb::array_view<const char *> symbols)
4609 : m_symbol_table (symbols)
c62446b1
PA
4610 {}
4611
a3c5fafd 4612 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4613
a3c5fafd 4614 /* Return the number of names in the symbol table. */
632e107b 4615 size_t symbol_name_count () const override
c62446b1 4616 {
a3c5fafd 4617 return m_symbol_table.size ();
c62446b1
PA
4618 }
4619
a3c5fafd 4620 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4621 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4622 {
4623 return m_symbol_table[idx];
4624 }
c62446b1 4625
a3c5fafd
PA
4626private:
4627 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4628};
4629
4630/* Convenience function that converts a NULL pointer to a "<null>"
4631 string, to pass to print routines. */
4632
4633static const char *
4634string_or_null (const char *str)
4635{
4636 return str != NULL ? str : "<null>";
4637}
4638
4639/* Check if a lookup_name_info built from
4640 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4641 index. EXPECTED_LIST is the list of expected matches, in expected
4642 matching order. If no match expected, then an empty list is
4643 specified. Returns true on success. On failure prints a warning
4644 indicating the file:line that failed, and returns false. */
4645
4646static bool
4647check_match (const char *file, int line,
4648 mock_mapped_index &mock_index,
4649 const char *name, symbol_name_match_type match_type,
4650 bool completion_mode,
4651 std::initializer_list<const char *> expected_list)
4652{
4653 lookup_name_info lookup_name (name, match_type, completion_mode);
4654
4655 bool matched = true;
4656
4657 auto mismatch = [&] (const char *expected_str,
4658 const char *got)
4659 {
4660 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4661 "expected=\"%s\", got=\"%s\"\n"),
4662 file, line,
4663 (match_type == symbol_name_match_type::FULL
4664 ? "FULL" : "WILD"),
4665 name, string_or_null (expected_str), string_or_null (got));
4666 matched = false;
4667 };
4668
4669 auto expected_it = expected_list.begin ();
4670 auto expected_end = expected_list.end ();
4671
a3c5fafd 4672 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4673 NULL, ALL_DOMAIN,
4674 [&] (offset_type idx)
4675 {
a3c5fafd 4676 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4677 const char *expected_str
4678 = expected_it == expected_end ? NULL : *expected_it++;
4679
4680 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4681 mismatch (expected_str, matched_name);
3b00ef10 4682 return true;
c62446b1
PA
4683 });
4684
4685 const char *expected_str
4686 = expected_it == expected_end ? NULL : *expected_it++;
4687 if (expected_str != NULL)
4688 mismatch (expected_str, NULL);
4689
4690 return matched;
4691}
4692
4693/* The symbols added to the mock mapped_index for testing (in
4694 canonical form). */
4695static const char *test_symbols[] = {
4696 "function",
4697 "std::bar",
4698 "std::zfunction",
4699 "std::zfunction2",
4700 "w1::w2",
4701 "ns::foo<char*>",
4702 "ns::foo<int>",
4703 "ns::foo<long>",
a20714ff
PA
4704 "ns2::tmpl<int>::foo2",
4705 "(anonymous namespace)::A::B::C",
c62446b1 4706
e1ef7d7a
PA
4707 /* These are used to check that the increment-last-char in the
4708 matching algorithm for completion doesn't match "t1_fund" when
4709 completing "t1_func". */
4710 "t1_func",
4711 "t1_func1",
4712 "t1_fund",
4713 "t1_fund1",
4714
4715 /* A UTF-8 name with multi-byte sequences to make sure that
4716 cp-name-parser understands this as a single identifier ("função"
4717 is "function" in PT). */
4718 u8"u8função",
4719
4720 /* \377 (0xff) is Latin1 'ÿ'. */
4721 "yfunc\377",
4722
4723 /* \377 (0xff) is Latin1 'ÿ'. */
4724 "\377",
4725 "\377\377123",
4726
c62446b1
PA
4727 /* A name with all sorts of complications. Starts with "z" to make
4728 it easier for the completion tests below. */
4729#define Z_SYM_NAME \
4730 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4731 "::tuple<(anonymous namespace)::ui*, " \
4732 "std::default_delete<(anonymous namespace)::ui>, void>"
4733
4734 Z_SYM_NAME
4735};
4736
a3c5fafd
PA
4737/* Returns true if the mapped_index_base::find_name_component_bounds
4738 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4739 in completion mode. */
5c58de74
PA
4740
4741static bool
a3c5fafd 4742check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4743 const char *search_name,
4744 gdb::array_view<const char *> expected_syms)
4745{
4746 lookup_name_info lookup_name (search_name,
4747 symbol_name_match_type::FULL, true);
4748
3b00ef10
TT
4749 auto bounds = index.find_name_components_bounds (lookup_name,
4750 language_cplus);
5c58de74
PA
4751
4752 size_t distance = std::distance (bounds.first, bounds.second);
4753 if (distance != expected_syms.size ())
4754 return false;
4755
4756 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4757 {
4758 auto nc_elem = bounds.first + exp_elem;
4759 const char *qualified = index.symbol_name_at (nc_elem->idx);
4760 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4761 return false;
4762 }
4763
4764 return true;
4765}
4766
4767/* Test the lower-level mapped_index::find_name_component_bounds
4768 method. */
4769
c62446b1 4770static void
5c58de74
PA
4771test_mapped_index_find_name_component_bounds ()
4772{
4773 mock_mapped_index mock_index (test_symbols);
4774
a3c5fafd 4775 mock_index.build_name_components ();
5c58de74
PA
4776
4777 /* Test the lower-level mapped_index::find_name_component_bounds
4778 method in completion mode. */
4779 {
4780 static const char *expected_syms[] = {
4781 "t1_func",
4782 "t1_func1",
5c58de74
PA
4783 };
4784
a3c5fafd 4785 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4786 "t1_func", expected_syms));
4787 }
4788
4789 /* Check that the increment-last-char in the name matching algorithm
4790 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4791 {
4792 static const char *expected_syms1[] = {
4793 "\377",
4794 "\377\377123",
4795 };
a3c5fafd 4796 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4797 "\377", expected_syms1));
4798
4799 static const char *expected_syms2[] = {
4800 "\377\377123",
4801 };
a3c5fafd 4802 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4803 "\377\377", expected_syms2));
4804 }
4805}
4806
4807/* Test dw2_expand_symtabs_matching_symbol. */
4808
4809static void
4810test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4811{
4812 mock_mapped_index mock_index (test_symbols);
4813
4814 /* We let all tests run until the end even if some fails, for debug
4815 convenience. */
4816 bool any_mismatch = false;
4817
4818 /* Create the expected symbols list (an initializer_list). Needed
4819 because lists have commas, and we need to pass them to CHECK,
4820 which is a macro. */
4821#define EXPECT(...) { __VA_ARGS__ }
4822
4823 /* Wrapper for check_match that passes down the current
4824 __FILE__/__LINE__. */
4825#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4826 any_mismatch |= !check_match (__FILE__, __LINE__, \
4827 mock_index, \
4828 NAME, MATCH_TYPE, COMPLETION_MODE, \
4829 EXPECTED_LIST)
4830
4831 /* Identity checks. */
4832 for (const char *sym : test_symbols)
4833 {
4834 /* Should be able to match all existing symbols. */
4835 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4836 EXPECT (sym));
4837
4838 /* Should be able to match all existing symbols with
4839 parameters. */
4840 std::string with_params = std::string (sym) + "(int)";
4841 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4842 EXPECT (sym));
4843
4844 /* Should be able to match all existing symbols with
4845 parameters and qualifiers. */
4846 with_params = std::string (sym) + " ( int ) const";
4847 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4848 EXPECT (sym));
4849
4850 /* This should really find sym, but cp-name-parser.y doesn't
4851 know about lvalue/rvalue qualifiers yet. */
4852 with_params = std::string (sym) + " ( int ) &&";
4853 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4854 {});
4855 }
4856
e1ef7d7a
PA
4857 /* Check that the name matching algorithm for completion doesn't get
4858 confused with Latin1 'ÿ' / 0xff. */
4859 {
4860 static const char str[] = "\377";
4861 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4862 EXPECT ("\377", "\377\377123"));
4863 }
4864
4865 /* Check that the increment-last-char in the matching algorithm for
4866 completion doesn't match "t1_fund" when completing "t1_func". */
4867 {
4868 static const char str[] = "t1_func";
4869 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4870 EXPECT ("t1_func", "t1_func1"));
4871 }
4872
c62446b1
PA
4873 /* Check that completion mode works at each prefix of the expected
4874 symbol name. */
4875 {
4876 static const char str[] = "function(int)";
4877 size_t len = strlen (str);
4878 std::string lookup;
4879
4880 for (size_t i = 1; i < len; i++)
4881 {
4882 lookup.assign (str, i);
4883 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4884 EXPECT ("function"));
4885 }
4886 }
4887
4888 /* While "w" is a prefix of both components, the match function
4889 should still only be called once. */
4890 {
4891 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4892 EXPECT ("w1::w2"));
a20714ff
PA
4893 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4894 EXPECT ("w1::w2"));
c62446b1
PA
4895 }
4896
4897 /* Same, with a "complicated" symbol. */
4898 {
4899 static const char str[] = Z_SYM_NAME;
4900 size_t len = strlen (str);
4901 std::string lookup;
4902
4903 for (size_t i = 1; i < len; i++)
4904 {
4905 lookup.assign (str, i);
4906 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4907 EXPECT (Z_SYM_NAME));
4908 }
4909 }
4910
4911 /* In FULL mode, an incomplete symbol doesn't match. */
4912 {
4913 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4914 {});
4915 }
4916
4917 /* A complete symbol with parameters matches any overload, since the
4918 index has no overload info. */
4919 {
4920 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4921 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4922 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4923 EXPECT ("std::zfunction", "std::zfunction2"));
4924 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4925 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4926 }
4927
4928 /* Check that whitespace is ignored appropriately. A symbol with a
4929 template argument list. */
4930 {
4931 static const char expected[] = "ns::foo<int>";
4932 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4933 EXPECT (expected));
a20714ff
PA
4934 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4935 EXPECT (expected));
c62446b1
PA
4936 }
4937
4938 /* Check that whitespace is ignored appropriately. A symbol with a
4939 template argument list that includes a pointer. */
4940 {
4941 static const char expected[] = "ns::foo<char*>";
4942 /* Try both completion and non-completion modes. */
4943 static const bool completion_mode[2] = {false, true};
4944 for (size_t i = 0; i < 2; i++)
4945 {
4946 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4947 completion_mode[i], EXPECT (expected));
a20714ff
PA
4948 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4949 completion_mode[i], EXPECT (expected));
c62446b1
PA
4950
4951 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4952 completion_mode[i], EXPECT (expected));
a20714ff
PA
4953 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4954 completion_mode[i], EXPECT (expected));
c62446b1
PA
4955 }
4956 }
4957
4958 {
4959 /* Check method qualifiers are ignored. */
4960 static const char expected[] = "ns::foo<char*>";
4961 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4962 symbol_name_match_type::FULL, true, EXPECT (expected));
4963 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4964 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4965 CHECK_MATCH ("foo < char * > ( int ) const",
4966 symbol_name_match_type::WILD, true, EXPECT (expected));
4967 CHECK_MATCH ("foo < char * > ( int ) &&",
4968 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4969 }
4970
4971 /* Test lookup names that don't match anything. */
4972 {
a20714ff
PA
4973 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4974 {});
4975
c62446b1
PA
4976 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4977 {});
4978 }
4979
a20714ff
PA
4980 /* Some wild matching tests, exercising "(anonymous namespace)",
4981 which should not be confused with a parameter list. */
4982 {
4983 static const char *syms[] = {
4984 "A::B::C",
4985 "B::C",
4986 "C",
4987 "A :: B :: C ( int )",
4988 "B :: C ( int )",
4989 "C ( int )",
4990 };
4991
4992 for (const char *s : syms)
4993 {
4994 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4995 EXPECT ("(anonymous namespace)::A::B::C"));
4996 }
4997 }
4998
4999 {
5000 static const char expected[] = "ns2::tmpl<int>::foo2";
5001 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5002 EXPECT (expected));
5003 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5004 EXPECT (expected));
5005 }
5006
c62446b1
PA
5007 SELF_CHECK (!any_mismatch);
5008
5009#undef EXPECT
5010#undef CHECK_MATCH
5011}
5012
5c58de74
PA
5013static void
5014run_test ()
5015{
5016 test_mapped_index_find_name_component_bounds ();
5017 test_dw2_expand_symtabs_matching_symbol ();
5018}
5019
c62446b1
PA
5020}} // namespace selftests::dw2_expand_symtabs_matching
5021
5022#endif /* GDB_SELF_TEST */
5023
4b514bc8
JK
5024/* If FILE_MATCHER is NULL or if PER_CU has
5025 dwarf2_per_cu_quick_data::MARK set (see
5026 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5027 EXPANSION_NOTIFY on it. */
5028
5029static void
5030dw2_expand_symtabs_matching_one
5031 (struct dwarf2_per_cu_data *per_cu,
5032 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5033 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5034{
5035 if (file_matcher == NULL || per_cu->v.quick->mark)
5036 {
5037 bool symtab_was_null
5038 = (per_cu->v.quick->compunit_symtab == NULL);
5039
58f0c718 5040 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5041
5042 if (expansion_notify != NULL
5043 && symtab_was_null
5044 && per_cu->v.quick->compunit_symtab != NULL)
5045 expansion_notify (per_cu->v.quick->compunit_symtab);
5046 }
5047}
5048
3f563c84
PA
5049/* Helper for dw2_expand_matching symtabs. Called on each symbol
5050 matched, to expand corresponding CUs that were marked. IDX is the
5051 index of the symbol name that matched. */
5052
5053static void
5054dw2_expand_marked_cus
ed2dc618 5055 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5056 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5057 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5058 search_domain kind)
5059{
3f563c84
PA
5060 offset_type *vec, vec_len, vec_idx;
5061 bool global_seen = false;
ed2dc618 5062 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5063
61920122 5064 vec = (offset_type *) (index.constant_pool
f00a2de2 5065 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5066 vec_len = MAYBE_SWAP (vec[0]);
5067 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5068 {
61920122
PA
5069 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5070 /* This value is only valid for index versions >= 7. */
5071 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5072 gdb_index_symbol_kind symbol_kind =
5073 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5074 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5075 /* Only check the symbol attributes if they're present.
5076 Indices prior to version 7 don't record them,
5077 and indices >= 7 may elide them for certain symbols
5078 (gold does this). */
5079 int attrs_valid =
5080 (index.version >= 7
5081 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5082
5083 /* Work around gold/15646. */
5084 if (attrs_valid)
9291a0cd 5085 {
61920122
PA
5086 if (!is_static && global_seen)
5087 continue;
5088 if (!is_static)
5089 global_seen = true;
5090 }
3190f0c6 5091
61920122
PA
5092 /* Only check the symbol's kind if it has one. */
5093 if (attrs_valid)
5094 {
5095 switch (kind)
8943b874 5096 {
61920122
PA
5097 case VARIABLES_DOMAIN:
5098 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5099 continue;
5100 break;
5101 case FUNCTIONS_DOMAIN:
5102 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5103 continue;
61920122
PA
5104 break;
5105 case TYPES_DOMAIN:
5106 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5107 continue;
5108 break;
59c35742
AB
5109 case MODULES_DOMAIN:
5110 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5111 continue;
5112 break;
61920122
PA
5113 default:
5114 break;
8943b874 5115 }
61920122 5116 }
8943b874 5117
61920122 5118 /* Don't crash on bad data. */
b76e467d 5119 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5120 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5121 {
b98664d3 5122 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5123 " [in module %s]"),
5124 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5125 continue;
5126 }
5127
ff4c9fec 5128 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5129 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5130 expansion_notify);
61920122
PA
5131 }
5132}
5133
4b514bc8
JK
5134/* If FILE_MATCHER is non-NULL, set all the
5135 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5136 that match FILE_MATCHER. */
5137
61920122 5138static void
4b514bc8 5139dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5140 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5141 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5142{
4b514bc8 5143 if (file_matcher == NULL)
61920122
PA
5144 return;
5145
4b514bc8
JK
5146 objfile *const objfile = dwarf2_per_objfile->objfile;
5147
5148 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5149 htab_eq_pointer,
5150 NULL, xcalloc, xfree));
5151 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5152 htab_eq_pointer,
5153 NULL, xcalloc, xfree));
61920122 5154
4b514bc8
JK
5155 /* The rule is CUs specify all the files, including those used by
5156 any TU, so there's no need to scan TUs here. */
61920122 5157
b76e467d 5158 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5159 {
927aa2e7
JK
5160 QUIT;
5161
5162 per_cu->v.quick->mark = 0;
5163
5164 /* We only need to look at symtabs not already expanded. */
5165 if (per_cu->v.quick->compunit_symtab)
5166 continue;
5167
b76e467d 5168 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5169 if (file_data == NULL)
5170 continue;
5171
5172 if (htab_find (visited_not_found.get (), file_data) != NULL)
5173 continue;
5174 else if (htab_find (visited_found.get (), file_data) != NULL)
5175 {
5176 per_cu->v.quick->mark = 1;
5177 continue;
5178 }
5179
b76e467d 5180 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5181 {
5182 const char *this_real_name;
5183
5184 if (file_matcher (file_data->file_names[j], false))
5185 {
5186 per_cu->v.quick->mark = 1;
5187 break;
5188 }
5189
5190 /* Before we invoke realpath, which can get expensive when many
5191 files are involved, do a quick comparison of the basenames. */
5192 if (!basenames_may_differ
5193 && !file_matcher (lbasename (file_data->file_names[j]),
5194 true))
5195 continue;
5196
5197 this_real_name = dw2_get_real_path (objfile, file_data, j);
5198 if (file_matcher (this_real_name, false))
5199 {
5200 per_cu->v.quick->mark = 1;
5201 break;
5202 }
5203 }
5204
b76e467d
SM
5205 void **slot = htab_find_slot (per_cu->v.quick->mark
5206 ? visited_found.get ()
5207 : visited_not_found.get (),
5208 file_data, INSERT);
927aa2e7
JK
5209 *slot = file_data;
5210 }
5211}
5212
5213static void
5214dw2_expand_symtabs_matching
5215 (struct objfile *objfile,
5216 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5217 const lookup_name_info &lookup_name,
5218 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5219 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5220 enum search_domain kind)
5221{
ed2dc618
SM
5222 struct dwarf2_per_objfile *dwarf2_per_objfile
5223 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5224
5225 /* index_table is NULL if OBJF_READNOW. */
5226 if (!dwarf2_per_objfile->index_table)
5227 return;
5228
ed2dc618 5229 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5230
5231 mapped_index &index = *dwarf2_per_objfile->index_table;
5232
5233 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5234 symbol_matcher,
5235 kind, [&] (offset_type idx)
5236 {
ed2dc618 5237 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5238 expansion_notify, kind);
3b00ef10 5239 return true;
927aa2e7
JK
5240 });
5241}
5242
5243/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5244 symtab. */
5245
5246static struct compunit_symtab *
5247recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5248 CORE_ADDR pc)
5249{
5250 int i;
5251
5252 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5253 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5254 return cust;
5255
5256 if (cust->includes == NULL)
5257 return NULL;
5258
5259 for (i = 0; cust->includes[i]; ++i)
5260 {
5261 struct compunit_symtab *s = cust->includes[i];
5262
5263 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5264 if (s != NULL)
5265 return s;
5266 }
5267
5268 return NULL;
5269}
5270
5271static struct compunit_symtab *
5272dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5273 struct bound_minimal_symbol msymbol,
5274 CORE_ADDR pc,
5275 struct obj_section *section,
5276 int warn_if_readin)
5277{
5278 struct dwarf2_per_cu_data *data;
5279 struct compunit_symtab *result;
5280
d320c2b5 5281 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5282 return NULL;
5283
b3b3bada 5284 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
5285 data = (struct dwarf2_per_cu_data *) addrmap_find
5286 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5287 if (!data)
5288 return NULL;
5289
5290 if (warn_if_readin && data->v.quick->compunit_symtab)
5291 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5292 paddress (get_objfile_arch (objfile), pc));
5293
5294 result
58f0c718
TT
5295 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5296 false),
927aa2e7
JK
5297 pc);
5298 gdb_assert (result != NULL);
5299 return result;
5300}
5301
5302static void
5303dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5304 void *data, int need_fullname)
5305{
ed2dc618
SM
5306 struct dwarf2_per_objfile *dwarf2_per_objfile
5307 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5308
5309 if (!dwarf2_per_objfile->filenames_cache)
5310 {
5311 dwarf2_per_objfile->filenames_cache.emplace ();
5312
5313 htab_up visited (htab_create_alloc (10,
5314 htab_hash_pointer, htab_eq_pointer,
5315 NULL, xcalloc, xfree));
5316
5317 /* The rule is CUs specify all the files, including those used
5318 by any TU, so there's no need to scan TUs here. We can
5319 ignore file names coming from already-expanded CUs. */
5320
b76e467d 5321 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5322 {
927aa2e7
JK
5323 if (per_cu->v.quick->compunit_symtab)
5324 {
5325 void **slot = htab_find_slot (visited.get (),
5326 per_cu->v.quick->file_names,
5327 INSERT);
5328
5329 *slot = per_cu->v.quick->file_names;
5330 }
5331 }
5332
b76e467d 5333 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5334 {
927aa2e7
JK
5335 /* We only need to look at symtabs not already expanded. */
5336 if (per_cu->v.quick->compunit_symtab)
5337 continue;
5338
b76e467d 5339 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5340 if (file_data == NULL)
5341 continue;
5342
b76e467d 5343 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5344 if (*slot)
5345 {
5346 /* Already visited. */
5347 continue;
5348 }
5349 *slot = file_data;
5350
5351 for (int j = 0; j < file_data->num_file_names; ++j)
5352 {
5353 const char *filename = file_data->file_names[j];
5354 dwarf2_per_objfile->filenames_cache->seen (filename);
5355 }
5356 }
5357 }
5358
5359 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5360 {
5361 gdb::unique_xmalloc_ptr<char> this_real_name;
5362
5363 if (need_fullname)
5364 this_real_name = gdb_realpath (filename);
5365 (*fun) (filename, this_real_name.get (), data);
5366 });
5367}
5368
5369static int
5370dw2_has_symbols (struct objfile *objfile)
5371{
5372 return 1;
5373}
5374
5375const struct quick_symbol_functions dwarf2_gdb_index_functions =
5376{
5377 dw2_has_symbols,
5378 dw2_find_last_source_symtab,
5379 dw2_forget_cached_source_info,
5380 dw2_map_symtabs_matching_filename,
5381 dw2_lookup_symbol,
5382 dw2_print_stats,
5383 dw2_dump,
927aa2e7
JK
5384 dw2_expand_symtabs_for_function,
5385 dw2_expand_all_symtabs,
5386 dw2_expand_symtabs_with_fullname,
5387 dw2_map_matching_symbols,
5388 dw2_expand_symtabs_matching,
5389 dw2_find_pc_sect_compunit_symtab,
5390 NULL,
5391 dw2_map_symbol_filenames
5392};
5393
5394/* DWARF-5 debug_names reader. */
5395
5396/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5397static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5398
5399/* A helper function that reads the .debug_names section in SECTION
5400 and fills in MAP. FILENAME is the name of the file containing the
5401 section; it is used for error reporting.
5402
5403 Returns true if all went well, false otherwise. */
5404
5405static bool
5406read_debug_names_from_section (struct objfile *objfile,
5407 const char *filename,
5408 struct dwarf2_section_info *section,
5409 mapped_debug_names &map)
5410{
5411 if (dwarf2_section_empty_p (section))
5412 return false;
5413
5414 /* Older elfutils strip versions could keep the section in the main
5415 executable while splitting it for the separate debug info file. */
5416 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5417 return false;
5418
5419 dwarf2_read_section (objfile, section);
5420
5421 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5422
5423 const gdb_byte *addr = section->buffer;
5424
5425 bfd *const abfd = get_section_bfd_owner (section);
5426
5427 unsigned int bytes_read;
5428 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5429 addr += bytes_read;
5430
5431 map.dwarf5_is_dwarf64 = bytes_read != 4;
5432 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5433 if (bytes_read + length != section->size)
5434 {
5435 /* There may be multiple per-CU indices. */
5436 warning (_("Section .debug_names in %s length %s does not match "
5437 "section length %s, ignoring .debug_names."),
5438 filename, plongest (bytes_read + length),
5439 pulongest (section->size));
5440 return false;
5441 }
5442
5443 /* The version number. */
5444 uint16_t version = read_2_bytes (abfd, addr);
5445 addr += 2;
5446 if (version != 5)
5447 {
5448 warning (_("Section .debug_names in %s has unsupported version %d, "
5449 "ignoring .debug_names."),
5450 filename, version);
5451 return false;
5452 }
5453
5454 /* Padding. */
5455 uint16_t padding = read_2_bytes (abfd, addr);
5456 addr += 2;
5457 if (padding != 0)
5458 {
5459 warning (_("Section .debug_names in %s has unsupported padding %d, "
5460 "ignoring .debug_names."),
5461 filename, padding);
5462 return false;
5463 }
5464
5465 /* comp_unit_count - The number of CUs in the CU list. */
5466 map.cu_count = read_4_bytes (abfd, addr);
5467 addr += 4;
5468
5469 /* local_type_unit_count - The number of TUs in the local TU
5470 list. */
5471 map.tu_count = read_4_bytes (abfd, addr);
5472 addr += 4;
5473
5474 /* foreign_type_unit_count - The number of TUs in the foreign TU
5475 list. */
5476 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5477 addr += 4;
5478 if (foreign_tu_count != 0)
5479 {
5480 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5481 "ignoring .debug_names."),
5482 filename, static_cast<unsigned long> (foreign_tu_count));
5483 return false;
5484 }
5485
5486 /* bucket_count - The number of hash buckets in the hash lookup
5487 table. */
5488 map.bucket_count = read_4_bytes (abfd, addr);
5489 addr += 4;
5490
5491 /* name_count - The number of unique names in the index. */
5492 map.name_count = read_4_bytes (abfd, addr);
5493 addr += 4;
5494
5495 /* abbrev_table_size - The size in bytes of the abbreviations
5496 table. */
5497 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5498 addr += 4;
5499
5500 /* augmentation_string_size - The size in bytes of the augmentation
5501 string. This value is rounded up to a multiple of 4. */
5502 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5503 addr += 4;
5504 map.augmentation_is_gdb = ((augmentation_string_size
5505 == sizeof (dwarf5_augmentation))
5506 && memcmp (addr, dwarf5_augmentation,
5507 sizeof (dwarf5_augmentation)) == 0);
5508 augmentation_string_size += (-augmentation_string_size) & 3;
5509 addr += augmentation_string_size;
5510
5511 /* List of CUs */
5512 map.cu_table_reordered = addr;
5513 addr += map.cu_count * map.offset_size;
5514
5515 /* List of Local TUs */
5516 map.tu_table_reordered = addr;
5517 addr += map.tu_count * map.offset_size;
5518
5519 /* Hash Lookup Table */
5520 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5521 addr += map.bucket_count * 4;
5522 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5523 addr += map.name_count * 4;
5524
5525 /* Name Table */
5526 map.name_table_string_offs_reordered = addr;
5527 addr += map.name_count * map.offset_size;
5528 map.name_table_entry_offs_reordered = addr;
5529 addr += map.name_count * map.offset_size;
5530
5531 const gdb_byte *abbrev_table_start = addr;
5532 for (;;)
5533 {
927aa2e7
JK
5534 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5535 addr += bytes_read;
5536 if (index_num == 0)
5537 break;
5538
5539 const auto insertpair
5540 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5541 if (!insertpair.second)
5542 {
5543 warning (_("Section .debug_names in %s has duplicate index %s, "
5544 "ignoring .debug_names."),
5545 filename, pulongest (index_num));
5546 return false;
5547 }
5548 mapped_debug_names::index_val &indexval = insertpair.first->second;
5549 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5550 addr += bytes_read;
5551
5552 for (;;)
5553 {
5554 mapped_debug_names::index_val::attr attr;
5555 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5556 addr += bytes_read;
5557 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5558 addr += bytes_read;
5559 if (attr.form == DW_FORM_implicit_const)
5560 {
5561 attr.implicit_const = read_signed_leb128 (abfd, addr,
5562 &bytes_read);
5563 addr += bytes_read;
5564 }
5565 if (attr.dw_idx == 0 && attr.form == 0)
5566 break;
5567 indexval.attr_vec.push_back (std::move (attr));
5568 }
5569 }
5570 if (addr != abbrev_table_start + abbrev_table_size)
5571 {
5572 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5573 "of size %s vs. written as %u, ignoring .debug_names."),
5574 filename, plongest (addr - abbrev_table_start),
5575 abbrev_table_size);
927aa2e7
JK
5576 return false;
5577 }
5578 map.entry_pool = addr;
5579
5580 return true;
5581}
5582
5583/* A helper for create_cus_from_debug_names that handles the MAP's CU
5584 list. */
5585
5586static void
ed2dc618 5587create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5588 const mapped_debug_names &map,
5589 dwarf2_section_info &section,
b76e467d 5590 bool is_dwz)
927aa2e7
JK
5591{
5592 sect_offset sect_off_prev;
5593 for (uint32_t i = 0; i <= map.cu_count; ++i)
5594 {
5595 sect_offset sect_off_next;
5596 if (i < map.cu_count)
5597 {
5598 sect_off_next
5599 = (sect_offset) (extract_unsigned_integer
5600 (map.cu_table_reordered + i * map.offset_size,
5601 map.offset_size,
5602 map.dwarf5_byte_order));
5603 }
5604 else
5605 sect_off_next = (sect_offset) section.size;
5606 if (i >= 1)
5607 {
5608 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5609 dwarf2_per_cu_data *per_cu
ed2dc618 5610 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5611 sect_off_prev, length);
b76e467d 5612 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5613 }
5614 sect_off_prev = sect_off_next;
5615 }
5616}
5617
5618/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5619 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5620
5621static void
ed2dc618 5622create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5623 const mapped_debug_names &map,
5624 const mapped_debug_names &dwz_map)
5625{
b76e467d
SM
5626 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5627 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5628
ed2dc618
SM
5629 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5630 dwarf2_per_objfile->info,
b76e467d 5631 false /* is_dwz */);
927aa2e7
JK
5632
5633 if (dwz_map.cu_count == 0)
5634 return;
5635
ed2dc618
SM
5636 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5637 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5638 true /* is_dwz */);
927aa2e7
JK
5639}
5640
5641/* Read .debug_names. If everything went ok, initialize the "quick"
5642 elements of all the CUs and return true. Otherwise, return false. */
5643
5644static bool
ed2dc618 5645dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5646{
22ca247e
TT
5647 std::unique_ptr<mapped_debug_names> map
5648 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5649 mapped_debug_names dwz_map (dwarf2_per_objfile);
5650 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5651
5652 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5653 &dwarf2_per_objfile->debug_names,
22ca247e 5654 *map))
927aa2e7
JK
5655 return false;
5656
5657 /* Don't use the index if it's empty. */
22ca247e 5658 if (map->name_count == 0)
927aa2e7
JK
5659 return false;
5660
5661 /* If there is a .dwz file, read it so we can get its CU list as
5662 well. */
ed2dc618 5663 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5664 if (dwz != NULL)
5665 {
5666 if (!read_debug_names_from_section (objfile,
00f93c44 5667 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5668 &dwz->debug_names, dwz_map))
5669 {
5670 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5671 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5672 return false;
5673 }
5674 }
5675
22ca247e 5676 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5677
22ca247e 5678 if (map->tu_count != 0)
927aa2e7
JK
5679 {
5680 /* We can only handle a single .debug_types when we have an
5681 index. */
fd5866f6 5682 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5683 return false;
5684
fd5866f6 5685 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5686
5687 create_signatured_type_table_from_debug_names
22ca247e 5688 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5689 }
5690
ed2dc618
SM
5691 create_addrmap_from_aranges (dwarf2_per_objfile,
5692 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5693
22ca247e 5694 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5695 dwarf2_per_objfile->using_index = 1;
5696 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5697 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5698
5699 return true;
5700}
5701
927aa2e7
JK
5702/* Type used to manage iterating over all CUs looking for a symbol for
5703 .debug_names. */
5704
5705class dw2_debug_names_iterator
5706{
5707public:
927aa2e7 5708 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5709 gdb::optional<block_enum> block_index,
5710 domain_enum domain,
927aa2e7 5711 const char *name)
2b79f376 5712 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5713 m_addr (find_vec_in_debug_names (map, name))
5714 {}
5715
5716 dw2_debug_names_iterator (const mapped_debug_names &map,
5717 search_domain search, uint32_t namei)
5718 : m_map (map),
5719 m_search (search),
5720 m_addr (find_vec_in_debug_names (map, namei))
5721 {}
5722
3b00ef10
TT
5723 dw2_debug_names_iterator (const mapped_debug_names &map,
5724 block_enum block_index, domain_enum domain,
5725 uint32_t namei)
5726 : m_map (map), m_block_index (block_index), m_domain (domain),
5727 m_addr (find_vec_in_debug_names (map, namei))
5728 {}
5729
927aa2e7
JK
5730 /* Return the next matching CU or NULL if there are no more. */
5731 dwarf2_per_cu_data *next ();
5732
5733private:
5734 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5735 const char *name);
5736 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5737 uint32_t namei);
5738
5739 /* The internalized form of .debug_names. */
5740 const mapped_debug_names &m_map;
5741
2b79f376
SM
5742 /* If set, only look for symbols that match that block. Valid values are
5743 GLOBAL_BLOCK and STATIC_BLOCK. */
5744 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5745
5746 /* The kind of symbol we're looking for. */
5747 const domain_enum m_domain = UNDEF_DOMAIN;
5748 const search_domain m_search = ALL_DOMAIN;
5749
5750 /* The list of CUs from the index entry of the symbol, or NULL if
5751 not found. */
5752 const gdb_byte *m_addr;
5753};
5754
5755const char *
5756mapped_debug_names::namei_to_name (uint32_t namei) const
5757{
5758 const ULONGEST namei_string_offs
5759 = extract_unsigned_integer ((name_table_string_offs_reordered
5760 + namei * offset_size),
5761 offset_size,
5762 dwarf5_byte_order);
5763 return read_indirect_string_at_offset
ed2dc618 5764 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5765}
5766
5767/* Find a slot in .debug_names for the object named NAME. If NAME is
5768 found, return pointer to its pool data. If NAME cannot be found,
5769 return NULL. */
5770
5771const gdb_byte *
5772dw2_debug_names_iterator::find_vec_in_debug_names
5773 (const mapped_debug_names &map, const char *name)
5774{
5775 int (*cmp) (const char *, const char *);
5776
54ee4252 5777 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5778 if (current_language->la_language == language_cplus
5779 || current_language->la_language == language_fortran
5780 || current_language->la_language == language_d)
5781 {
5782 /* NAME is already canonical. Drop any qualifiers as
5783 .debug_names does not contain any. */
5784
5785 if (strchr (name, '(') != NULL)
5786 {
54ee4252 5787 without_params = cp_remove_params (name);
927aa2e7 5788 if (without_params != NULL)
54ee4252 5789 name = without_params.get ();
927aa2e7
JK
5790 }
5791 }
5792
5793 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5794
5795 const uint32_t full_hash = dwarf5_djb_hash (name);
5796 uint32_t namei
5797 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5798 (map.bucket_table_reordered
5799 + (full_hash % map.bucket_count)), 4,
5800 map.dwarf5_byte_order);
5801 if (namei == 0)
5802 return NULL;
5803 --namei;
5804 if (namei >= map.name_count)
5805 {
b98664d3 5806 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5807 "[in module %s]"),
5808 namei, map.name_count,
ed2dc618 5809 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5810 return NULL;
5811 }
5812
5813 for (;;)
5814 {
5815 const uint32_t namei_full_hash
5816 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5817 (map.hash_table_reordered + namei), 4,
5818 map.dwarf5_byte_order);
5819 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5820 return NULL;
5821
5822 if (full_hash == namei_full_hash)
5823 {
5824 const char *const namei_string = map.namei_to_name (namei);
5825
5826#if 0 /* An expensive sanity check. */
5827 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5828 {
b98664d3 5829 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5830 "[in module %s]"),
5831 namei, objfile_name (dwarf2_per_objfile->objfile));
5832 return NULL;
5833 }
5834#endif
5835
5836 if (cmp (namei_string, name) == 0)
5837 {
5838 const ULONGEST namei_entry_offs
5839 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5840 + namei * map.offset_size),
5841 map.offset_size, map.dwarf5_byte_order);
5842 return map.entry_pool + namei_entry_offs;
5843 }
5844 }
5845
5846 ++namei;
5847 if (namei >= map.name_count)
5848 return NULL;
5849 }
5850}
5851
5852const gdb_byte *
5853dw2_debug_names_iterator::find_vec_in_debug_names
5854 (const mapped_debug_names &map, uint32_t namei)
5855{
5856 if (namei >= map.name_count)
5857 {
b98664d3 5858 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5859 "[in module %s]"),
5860 namei, map.name_count,
ed2dc618 5861 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5862 return NULL;
5863 }
5864
5865 const ULONGEST namei_entry_offs
5866 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5867 + namei * map.offset_size),
5868 map.offset_size, map.dwarf5_byte_order);
5869 return map.entry_pool + namei_entry_offs;
5870}
5871
5872/* See dw2_debug_names_iterator. */
5873
5874dwarf2_per_cu_data *
5875dw2_debug_names_iterator::next ()
5876{
5877 if (m_addr == NULL)
5878 return NULL;
5879
ed2dc618
SM
5880 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5881 struct objfile *objfile = dwarf2_per_objfile->objfile;
5882 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5883
5884 again:
5885
5886 unsigned int bytes_read;
5887 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5888 m_addr += bytes_read;
5889 if (abbrev == 0)
5890 return NULL;
5891
5892 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5893 if (indexval_it == m_map.abbrev_map.cend ())
5894 {
b98664d3 5895 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5896 "[in module %s]"),
ed2dc618 5897 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5898 return NULL;
5899 }
5900 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5901 enum class symbol_linkage {
5902 unknown,
5903 static_,
5904 extern_,
23c13d42 5905 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5906 dwarf2_per_cu_data *per_cu = NULL;
5907 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5908 {
5909 ULONGEST ull;
5910 switch (attr.form)
5911 {
5912 case DW_FORM_implicit_const:
5913 ull = attr.implicit_const;
5914 break;
5915 case DW_FORM_flag_present:
5916 ull = 1;
5917 break;
5918 case DW_FORM_udata:
5919 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5920 m_addr += bytes_read;
5921 break;
5922 default:
b98664d3 5923 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5924 dwarf_form_name (attr.form),
ed2dc618 5925 objfile_name (objfile));
927aa2e7
JK
5926 return NULL;
5927 }
5928 switch (attr.dw_idx)
5929 {
5930 case DW_IDX_compile_unit:
5931 /* Don't crash on bad data. */
b76e467d 5932 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5933 {
b98664d3 5934 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5935 " [in module %s]"),
5936 pulongest (ull),
5937 objfile_name (dwarf2_per_objfile->objfile));
5938 continue;
5939 }
ff4c9fec 5940 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5941 break;
8af5c486
JK
5942 case DW_IDX_type_unit:
5943 /* Don't crash on bad data. */
b2bdb8cf 5944 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5945 {
b98664d3 5946 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5947 " [in module %s]"),
5948 pulongest (ull),
5949 objfile_name (dwarf2_per_objfile->objfile));
5950 continue;
5951 }
ff4c9fec 5952 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5953 break;
927aa2e7
JK
5954 case DW_IDX_GNU_internal:
5955 if (!m_map.augmentation_is_gdb)
5956 break;
23c13d42 5957 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5958 break;
5959 case DW_IDX_GNU_external:
5960 if (!m_map.augmentation_is_gdb)
5961 break;
23c13d42 5962 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5963 break;
5964 }
5965 }
5966
5967 /* Skip if already read in. */
5968 if (per_cu->v.quick->compunit_symtab)
5969 goto again;
5970
5971 /* Check static vs global. */
23c13d42 5972 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5973 {
2b79f376 5974 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5975 const bool symbol_is_static =
5976 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5977 if (want_static != symbol_is_static)
2b79f376 5978 goto again;
927aa2e7
JK
5979 }
5980
5981 /* Match dw2_symtab_iter_next, symbol_kind
5982 and debug_names::psymbol_tag. */
5983 switch (m_domain)
5984 {
5985 case VAR_DOMAIN:
5986 switch (indexval.dwarf_tag)
5987 {
5988 case DW_TAG_variable:
5989 case DW_TAG_subprogram:
5990 /* Some types are also in VAR_DOMAIN. */
5991 case DW_TAG_typedef:
5992 case DW_TAG_structure_type:
5993 break;
5994 default:
5995 goto again;
5996 }
5997 break;
5998 case STRUCT_DOMAIN:
5999 switch (indexval.dwarf_tag)
6000 {
6001 case DW_TAG_typedef:
6002 case DW_TAG_structure_type:
6003 break;
6004 default:
6005 goto again;
6006 }
6007 break;
6008 case LABEL_DOMAIN:
6009 switch (indexval.dwarf_tag)
6010 {
6011 case 0:
6012 case DW_TAG_variable:
6013 break;
6014 default:
6015 goto again;
6016 }
6017 break;
59c35742
AB
6018 case MODULE_DOMAIN:
6019 switch (indexval.dwarf_tag)
6020 {
6021 case DW_TAG_module:
6022 break;
6023 default:
6024 goto again;
6025 }
6026 break;
927aa2e7
JK
6027 default:
6028 break;
6029 }
6030
6031 /* Match dw2_expand_symtabs_matching, symbol_kind and
6032 debug_names::psymbol_tag. */
6033 switch (m_search)
4b514bc8 6034 {
927aa2e7
JK
6035 case VARIABLES_DOMAIN:
6036 switch (indexval.dwarf_tag)
4b514bc8 6037 {
927aa2e7
JK
6038 case DW_TAG_variable:
6039 break;
6040 default:
6041 goto again;
4b514bc8 6042 }
927aa2e7
JK
6043 break;
6044 case FUNCTIONS_DOMAIN:
6045 switch (indexval.dwarf_tag)
4b514bc8 6046 {
927aa2e7
JK
6047 case DW_TAG_subprogram:
6048 break;
6049 default:
6050 goto again;
4b514bc8 6051 }
927aa2e7
JK
6052 break;
6053 case TYPES_DOMAIN:
6054 switch (indexval.dwarf_tag)
6055 {
6056 case DW_TAG_typedef:
6057 case DW_TAG_structure_type:
6058 break;
6059 default:
6060 goto again;
6061 }
6062 break;
59c35742
AB
6063 case MODULES_DOMAIN:
6064 switch (indexval.dwarf_tag)
6065 {
6066 case DW_TAG_module:
6067 break;
6068 default:
6069 goto again;
6070 }
927aa2e7
JK
6071 default:
6072 break;
4b514bc8 6073 }
927aa2e7
JK
6074
6075 return per_cu;
4b514bc8 6076}
61920122 6077
927aa2e7 6078static struct compunit_symtab *
c7f839cb 6079dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6080 const char *name, domain_enum domain)
4b514bc8 6081{
ed2dc618
SM
6082 struct dwarf2_per_objfile *dwarf2_per_objfile
6083 = get_dwarf2_per_objfile (objfile);
61920122 6084
927aa2e7
JK
6085 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6086 if (!mapp)
61920122 6087 {
927aa2e7
JK
6088 /* index is NULL if OBJF_READNOW. */
6089 return NULL;
6090 }
6091 const auto &map = *mapp;
9291a0cd 6092
2b79f376 6093 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6094
927aa2e7
JK
6095 struct compunit_symtab *stab_best = NULL;
6096 struct dwarf2_per_cu_data *per_cu;
6097 while ((per_cu = iter.next ()) != NULL)
6098 {
6099 struct symbol *sym, *with_opaque = NULL;
58f0c718 6100 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6101 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6102 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6103
927aa2e7
JK
6104 sym = block_find_symbol (block, name, domain,
6105 block_find_non_opaque_type_preferred,
6106 &with_opaque);
9703b513 6107
927aa2e7
JK
6108 /* Some caution must be observed with overloaded functions and
6109 methods, since the index will not contain any overload
6110 information (but NAME might contain it). */
a3ec0bb1 6111
927aa2e7 6112 if (sym != NULL
987012b8 6113 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
6114 return stab;
6115 if (with_opaque != NULL
987012b8 6116 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 6117 stab_best = stab;
9703b513 6118
927aa2e7 6119 /* Keep looking through other CUs. */
9703b513
TT
6120 }
6121
927aa2e7 6122 return stab_best;
9703b513
TT
6123}
6124
927aa2e7
JK
6125/* This dumps minimal information about .debug_names. It is called
6126 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6127 uses this to verify that .debug_names has been loaded. */
9291a0cd 6128
927aa2e7
JK
6129static void
6130dw2_debug_names_dump (struct objfile *objfile)
6131{
ed2dc618
SM
6132 struct dwarf2_per_objfile *dwarf2_per_objfile
6133 = get_dwarf2_per_objfile (objfile);
6134
927aa2e7
JK
6135 gdb_assert (dwarf2_per_objfile->using_index);
6136 printf_filtered (".debug_names:");
6137 if (dwarf2_per_objfile->debug_names_table)
6138 printf_filtered (" exists\n");
6139 else
6140 printf_filtered (" faked for \"readnow\"\n");
6141 printf_filtered ("\n");
9291a0cd
TT
6142}
6143
9291a0cd 6144static void
927aa2e7
JK
6145dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6146 const char *func_name)
9291a0cd 6147{
ed2dc618
SM
6148 struct dwarf2_per_objfile *dwarf2_per_objfile
6149 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6150
927aa2e7
JK
6151 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6152 if (dwarf2_per_objfile->debug_names_table)
24c79950 6153 {
927aa2e7 6154 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6155
2b79f376 6156 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6157
927aa2e7
JK
6158 struct dwarf2_per_cu_data *per_cu;
6159 while ((per_cu = iter.next ()) != NULL)
58f0c718 6160 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6161 }
6162}
24c79950 6163
3b00ef10
TT
6164static void
6165dw2_debug_names_map_matching_symbols
6166 (struct objfile *objfile,
6167 const lookup_name_info &name, domain_enum domain,
6168 int global,
6169 gdb::function_view<symbol_found_callback_ftype> callback,
6170 symbol_compare_ftype *ordered_compare)
6171{
6172 struct dwarf2_per_objfile *dwarf2_per_objfile
6173 = get_dwarf2_per_objfile (objfile);
6174
6175 /* debug_names_table is NULL if OBJF_READNOW. */
6176 if (!dwarf2_per_objfile->debug_names_table)
6177 return;
6178
6179 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6180 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6181
6182 const char *match_name = name.ada ().lookup_name ().c_str ();
6183 auto matcher = [&] (const char *symname)
6184 {
6185 if (ordered_compare == nullptr)
6186 return true;
6187 return ordered_compare (symname, match_name) == 0;
6188 };
6189
6190 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6191 [&] (offset_type namei)
6192 {
6193 /* The name was matched, now expand corresponding CUs that were
6194 marked. */
6195 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6196
6197 struct dwarf2_per_cu_data *per_cu;
6198 while ((per_cu = iter.next ()) != NULL)
6199 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6200 return true;
6201 });
6202
6203 /* It's a shame we couldn't do this inside the
6204 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6205 that have already been expanded. Instead, this loop matches what
6206 the psymtab code does. */
6207 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6208 {
6209 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6210 if (cust != nullptr)
6211 {
6212 const struct block *block
6213 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6214 if (!iterate_over_symbols_terminated (block, name,
6215 domain, callback))
6216 break;
6217 }
6218 }
6219}
6220
927aa2e7
JK
6221static void
6222dw2_debug_names_expand_symtabs_matching
6223 (struct objfile *objfile,
6224 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6225 const lookup_name_info &lookup_name,
6226 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6227 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6228 enum search_domain kind)
6229{
ed2dc618
SM
6230 struct dwarf2_per_objfile *dwarf2_per_objfile
6231 = get_dwarf2_per_objfile (objfile);
9291a0cd 6232
927aa2e7
JK
6233 /* debug_names_table is NULL if OBJF_READNOW. */
6234 if (!dwarf2_per_objfile->debug_names_table)
6235 return;
9291a0cd 6236
ed2dc618 6237 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6238
44ed8f3e 6239 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6240
44ed8f3e
PA
6241 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6242 symbol_matcher,
6243 kind, [&] (offset_type namei)
927aa2e7 6244 {
927aa2e7
JK
6245 /* The name was matched, now expand corresponding CUs that were
6246 marked. */
6247 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6248
927aa2e7
JK
6249 struct dwarf2_per_cu_data *per_cu;
6250 while ((per_cu = iter.next ()) != NULL)
6251 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6252 expansion_notify);
3b00ef10 6253 return true;
44ed8f3e 6254 });
9291a0cd
TT
6255}
6256
927aa2e7 6257const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6258{
6259 dw2_has_symbols,
6260 dw2_find_last_source_symtab,
6261 dw2_forget_cached_source_info,
f8eba3c6 6262 dw2_map_symtabs_matching_filename,
927aa2e7 6263 dw2_debug_names_lookup_symbol,
9291a0cd 6264 dw2_print_stats,
927aa2e7 6265 dw2_debug_names_dump,
927aa2e7 6266 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6267 dw2_expand_all_symtabs,
652a8996 6268 dw2_expand_symtabs_with_fullname,
3b00ef10 6269 dw2_debug_names_map_matching_symbols,
927aa2e7 6270 dw2_debug_names_expand_symtabs_matching,
43f3e411 6271 dw2_find_pc_sect_compunit_symtab,
71a3c369 6272 NULL,
9291a0cd
TT
6273 dw2_map_symbol_filenames
6274};
6275
4485a1c1
SM
6276/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6277 to either a dwarf2_per_objfile or dwz_file object. */
6278
6279template <typename T>
6280static gdb::array_view<const gdb_byte>
6281get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6282{
6283 dwarf2_section_info *section = &section_owner->gdb_index;
6284
6285 if (dwarf2_section_empty_p (section))
6286 return {};
6287
6288 /* Older elfutils strip versions could keep the section in the main
6289 executable while splitting it for the separate debug info file. */
6290 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6291 return {};
6292
6293 dwarf2_read_section (obj, section);
6294
8bebfcda
PA
6295 /* dwarf2_section_info::size is a bfd_size_type, while
6296 gdb::array_view works with size_t. On 32-bit hosts, with
6297 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6298 is 32-bit. So we need an explicit narrowing conversion here.
6299 This is fine, because it's impossible to allocate or mmap an
6300 array/buffer larger than what size_t can represent. */
6301 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6302}
6303
87d6a7aa
SM
6304/* Lookup the index cache for the contents of the index associated to
6305 DWARF2_OBJ. */
6306
6307static gdb::array_view<const gdb_byte>
6308get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6309{
6310 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6311 if (build_id == nullptr)
6312 return {};
6313
6314 return global_index_cache.lookup_gdb_index (build_id,
6315 &dwarf2_obj->index_cache_res);
6316}
6317
6318/* Same as the above, but for DWZ. */
6319
6320static gdb::array_view<const gdb_byte>
6321get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6322{
6323 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6324 if (build_id == nullptr)
6325 return {};
6326
6327 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6328}
6329
3c0aa29a 6330/* See symfile.h. */
9291a0cd 6331
3c0aa29a
PA
6332bool
6333dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6334{
ed2dc618
SM
6335 struct dwarf2_per_objfile *dwarf2_per_objfile
6336 = get_dwarf2_per_objfile (objfile);
6337
9291a0cd
TT
6338 /* If we're about to read full symbols, don't bother with the
6339 indices. In this case we also don't care if some other debug
6340 format is making psymtabs, because they are all about to be
6341 expanded anyway. */
6342 if ((objfile->flags & OBJF_READNOW))
6343 {
9291a0cd 6344 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6345 create_all_comp_units (dwarf2_per_objfile);
6346 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6347 dwarf2_per_objfile->quick_file_names_table
6348 = create_quick_file_names_table
6349 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6350
b76e467d 6351 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6352 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6353 {
ff4c9fec 6354 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6355
e254ef6a
DE
6356 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6357 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6358 }
6359
6360 /* Return 1 so that gdb sees the "quick" functions. However,
6361 these functions will be no-ops because we will have expanded
6362 all symtabs. */
3c0aa29a
PA
6363 *index_kind = dw_index_kind::GDB_INDEX;
6364 return true;
9291a0cd
TT
6365 }
6366
ed2dc618 6367 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6368 {
6369 *index_kind = dw_index_kind::DEBUG_NAMES;
6370 return true;
6371 }
927aa2e7 6372
4485a1c1
SM
6373 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6374 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6375 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6376 {
6377 *index_kind = dw_index_kind::GDB_INDEX;
6378 return true;
6379 }
9291a0cd 6380
87d6a7aa
SM
6381 /* ... otherwise, try to find the index in the index cache. */
6382 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6383 get_gdb_index_contents_from_cache,
6384 get_gdb_index_contents_from_cache_dwz))
6385 {
6386 global_index_cache.hit ();
6387 *index_kind = dw_index_kind::GDB_INDEX;
6388 return true;
6389 }
6390
6391 global_index_cache.miss ();
3c0aa29a 6392 return false;
9291a0cd
TT
6393}
6394
6395\f
6396
dce234bc
PP
6397/* Build a partial symbol table. */
6398
6399void
f29dff0a 6400dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6401{
ed2dc618
SM
6402 struct dwarf2_per_objfile *dwarf2_per_objfile
6403 = get_dwarf2_per_objfile (objfile);
c9bf0622 6404
6eee24ce 6405 init_psymbol_list (objfile, 1024);
c906108c 6406
a70b8144 6407 try
c9bf0622
TT
6408 {
6409 /* This isn't really ideal: all the data we allocate on the
6410 objfile's obstack is still uselessly kept around. However,
6411 freeing it seems unsafe. */
906768f9 6412 psymtab_discarder psymtabs (objfile);
ed2dc618 6413 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6414 psymtabs.keep ();
87d6a7aa
SM
6415
6416 /* (maybe) store an index in the cache. */
6417 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6418 }
230d2906 6419 catch (const gdb_exception_error &except)
492d29ea
PA
6420 {
6421 exception_print (gdb_stderr, except);
6422 }
c906108c 6423}
c906108c 6424
1ce1cefd
DE
6425/* Return the total length of the CU described by HEADER. */
6426
6427static unsigned int
6428get_cu_length (const struct comp_unit_head *header)
6429{
6430 return header->initial_length_size + header->length;
6431}
6432
9c541725 6433/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6434
9c541725
PA
6435static inline bool
6436offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6437{
9c541725
PA
6438 sect_offset bottom = cu_header->sect_off;
6439 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6440
9c541725 6441 return sect_off >= bottom && sect_off < top;
45452591
DE
6442}
6443
3b80fe9b
DE
6444/* Find the base address of the compilation unit for range lists and
6445 location lists. It will normally be specified by DW_AT_low_pc.
6446 In DWARF-3 draft 4, the base address could be overridden by
6447 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6448 compilation units with discontinuous ranges. */
6449
6450static void
6451dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6452{
6453 struct attribute *attr;
6454
6455 cu->base_known = 0;
6456 cu->base_address = 0;
6457
6458 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6459 if (attr != nullptr)
3b80fe9b 6460 {
31aa7e4e 6461 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6462 cu->base_known = 1;
6463 }
6464 else
6465 {
6466 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6467 if (attr != nullptr)
3b80fe9b 6468 {
31aa7e4e 6469 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6470 cu->base_known = 1;
6471 }
6472 }
6473}
6474
93311388 6475/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6476 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6477 NOTE: This leaves members offset, first_die_offset to be filled in
6478 by the caller. */
107d2387 6479
d521ce57 6480static const gdb_byte *
107d2387 6481read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6482 const gdb_byte *info_ptr,
6483 struct dwarf2_section_info *section,
6484 rcuh_kind section_kind)
107d2387
AC
6485{
6486 int signed_addr;
891d2f0b 6487 unsigned int bytes_read;
43988095
JK
6488 const char *filename = get_section_file_name (section);
6489 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6490
6491 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6492 cu_header->initial_length_size = bytes_read;
6493 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6494 info_ptr += bytes_read;
107d2387 6495 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6496 if (cu_header->version < 2 || cu_header->version > 5)
6497 error (_("Dwarf Error: wrong version in compilation unit header "
6498 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6499 cu_header->version, filename);
107d2387 6500 info_ptr += 2;
43988095
JK
6501 if (cu_header->version < 5)
6502 switch (section_kind)
6503 {
6504 case rcuh_kind::COMPILE:
6505 cu_header->unit_type = DW_UT_compile;
6506 break;
6507 case rcuh_kind::TYPE:
6508 cu_header->unit_type = DW_UT_type;
6509 break;
6510 default:
6511 internal_error (__FILE__, __LINE__,
6512 _("read_comp_unit_head: invalid section_kind"));
6513 }
6514 else
6515 {
6516 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6517 (read_1_byte (abfd, info_ptr));
6518 info_ptr += 1;
6519 switch (cu_header->unit_type)
6520 {
6521 case DW_UT_compile:
a084a2a6
AT
6522 case DW_UT_partial:
6523 case DW_UT_skeleton:
6524 case DW_UT_split_compile:
43988095
JK
6525 if (section_kind != rcuh_kind::COMPILE)
6526 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6527 "(is %s, should be %s) [in module %s]"),
6528 dwarf_unit_type_name (cu_header->unit_type),
6529 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6530 break;
6531 case DW_UT_type:
a084a2a6 6532 case DW_UT_split_type:
43988095
JK
6533 section_kind = rcuh_kind::TYPE;
6534 break;
6535 default:
6536 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6537 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6538 "[in module %s]"), cu_header->unit_type,
6539 dwarf_unit_type_name (DW_UT_compile),
6540 dwarf_unit_type_name (DW_UT_skeleton),
6541 dwarf_unit_type_name (DW_UT_split_compile),
6542 dwarf_unit_type_name (DW_UT_type),
6543 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6544 }
6545
6546 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6547 info_ptr += 1;
6548 }
9c541725
PA
6549 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6550 cu_header,
6551 &bytes_read);
613e1657 6552 info_ptr += bytes_read;
43988095
JK
6553 if (cu_header->version < 5)
6554 {
6555 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6556 info_ptr += 1;
6557 }
107d2387
AC
6558 signed_addr = bfd_get_sign_extend_vma (abfd);
6559 if (signed_addr < 0)
8e65ff28 6560 internal_error (__FILE__, __LINE__,
e2e0b3e5 6561 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6562 cu_header->signed_addr_p = signed_addr;
c764a876 6563
a084a2a6
AT
6564 bool header_has_signature = section_kind == rcuh_kind::TYPE
6565 || cu_header->unit_type == DW_UT_skeleton
6566 || cu_header->unit_type == DW_UT_split_compile;
43988095 6567
a084a2a6
AT
6568 if (header_has_signature)
6569 {
43988095
JK
6570 cu_header->signature = read_8_bytes (abfd, info_ptr);
6571 info_ptr += 8;
a084a2a6 6572 }
43988095 6573
a084a2a6
AT
6574 if (section_kind == rcuh_kind::TYPE)
6575 {
6576 LONGEST type_offset;
43988095
JK
6577 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6578 info_ptr += bytes_read;
9c541725
PA
6579 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6580 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6581 error (_("Dwarf Error: Too big type_offset in compilation unit "
6582 "header (is %s) [in module %s]"), plongest (type_offset),
6583 filename);
6584 }
6585
107d2387
AC
6586 return info_ptr;
6587}
6588
36586728
TT
6589/* Helper function that returns the proper abbrev section for
6590 THIS_CU. */
6591
6592static struct dwarf2_section_info *
6593get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6594{
6595 struct dwarf2_section_info *abbrev;
ed2dc618 6596 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6597
6598 if (this_cu->is_dwz)
ed2dc618 6599 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6600 else
6601 abbrev = &dwarf2_per_objfile->abbrev;
6602
6603 return abbrev;
6604}
6605
9ff913ba
DE
6606/* Subroutine of read_and_check_comp_unit_head and
6607 read_and_check_type_unit_head to simplify them.
6608 Perform various error checking on the header. */
6609
6610static void
ed2dc618
SM
6611error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6612 struct comp_unit_head *header,
4bdcc0c1
DE
6613 struct dwarf2_section_info *section,
6614 struct dwarf2_section_info *abbrev_section)
9ff913ba 6615{
a32a8923 6616 const char *filename = get_section_file_name (section);
9ff913ba 6617
9c541725 6618 if (to_underlying (header->abbrev_sect_off)
36586728 6619 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6620 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6621 "(offset %s + 6) [in module %s]"),
6622 sect_offset_str (header->abbrev_sect_off),
6623 sect_offset_str (header->sect_off),
9ff913ba
DE
6624 filename);
6625
9c541725 6626 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6627 avoid potential 32-bit overflow. */
9c541725 6628 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6629 > section->size)
9c541725 6630 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6631 "(offset %s + 0) [in module %s]"),
6632 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6633 filename);
6634}
6635
6636/* Read in a CU/TU header and perform some basic error checking.
6637 The contents of the header are stored in HEADER.
6638 The result is a pointer to the start of the first DIE. */
adabb602 6639
d521ce57 6640static const gdb_byte *
ed2dc618
SM
6641read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6642 struct comp_unit_head *header,
9ff913ba 6643 struct dwarf2_section_info *section,
4bdcc0c1 6644 struct dwarf2_section_info *abbrev_section,
d521ce57 6645 const gdb_byte *info_ptr,
43988095 6646 rcuh_kind section_kind)
72bf9492 6647{
d521ce57 6648 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6649
9c541725 6650 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6651
43988095 6652 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6653
9c541725 6654 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6655
ed2dc618
SM
6656 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6657 abbrev_section);
9ff913ba
DE
6658
6659 return info_ptr;
348e048f
DE
6660}
6661
f4dc4d17
DE
6662/* Fetch the abbreviation table offset from a comp or type unit header. */
6663
6664static sect_offset
ed2dc618
SM
6665read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6666 struct dwarf2_section_info *section,
9c541725 6667 sect_offset sect_off)
f4dc4d17 6668{
a32a8923 6669 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6670 const gdb_byte *info_ptr;
ac298888 6671 unsigned int initial_length_size, offset_size;
43988095 6672 uint16_t version;
f4dc4d17
DE
6673
6674 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6675 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6676 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6677 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6678 info_ptr += initial_length_size;
6679
6680 version = read_2_bytes (abfd, info_ptr);
6681 info_ptr += 2;
6682 if (version >= 5)
6683 {
6684 /* Skip unit type and address size. */
6685 info_ptr += 2;
6686 }
6687
9c541725 6688 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6689}
6690
aaa75496
JB
6691/* Allocate a new partial symtab for file named NAME and mark this new
6692 partial symtab as being an include of PST. */
6693
6694static void
d521ce57 6695dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6696 struct objfile *objfile)
6697{
32caafd0 6698 struct partial_symtab *subpst = new partial_symtab (name, objfile);
aaa75496 6699
fbd9ab74
JK
6700 if (!IS_ABSOLUTE_PATH (subpst->filename))
6701 {
6702 /* It shares objfile->objfile_obstack. */
6703 subpst->dirname = pst->dirname;
6704 }
6705
a9342b62 6706 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6707 subpst->dependencies[0] = pst;
6708 subpst->number_of_dependencies = 1;
6709
aaa75496 6710 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6711
6712 /* No private part is necessary for include psymtabs. This property
6713 can be used to differentiate between such include psymtabs and
10b3939b 6714 the regular ones. */
58a9656e 6715 subpst->read_symtab_private = NULL;
aaa75496
JB
6716}
6717
6718/* Read the Line Number Program data and extract the list of files
6719 included by the source file represented by PST. Build an include
d85a05f0 6720 partial symtab for each of these included files. */
aaa75496
JB
6721
6722static void
6723dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6724 struct die_info *die,
6725 struct partial_symtab *pst)
aaa75496 6726{
fff8551c 6727 line_header_up lh;
d85a05f0 6728 struct attribute *attr;
aaa75496 6729
d85a05f0 6730 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6731 if (attr != nullptr)
9c541725 6732 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6733 if (lh == NULL)
6734 return; /* No linetable, so no includes. */
6735
79748972
TT
6736 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6737 that we pass in the raw text_low here; that is ok because we're
6738 only decoding the line table to make include partial symtabs, and
6739 so the addresses aren't really used. */
4ae976d1 6740 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6741 pst->raw_text_low (), 1);
aaa75496
JB
6742}
6743
348e048f 6744static hashval_t
52dc124a 6745hash_signatured_type (const void *item)
348e048f 6746{
9a3c8263
SM
6747 const struct signatured_type *sig_type
6748 = (const struct signatured_type *) item;
9a619af0 6749
348e048f 6750 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6751 return sig_type->signature;
348e048f
DE
6752}
6753
6754static int
52dc124a 6755eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6756{
9a3c8263
SM
6757 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6758 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6759
348e048f
DE
6760 return lhs->signature == rhs->signature;
6761}
6762
1fd400ff
TT
6763/* Allocate a hash table for signatured types. */
6764
6765static htab_t
673bfd45 6766allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6767{
6768 return htab_create_alloc_ex (41,
52dc124a
DE
6769 hash_signatured_type,
6770 eq_signatured_type,
1fd400ff
TT
6771 NULL,
6772 &objfile->objfile_obstack,
6773 hashtab_obstack_allocate,
6774 dummy_obstack_deallocate);
6775}
6776
d467dd73 6777/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6778
6779static int
d467dd73 6780add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6781{
9a3c8263 6782 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6783 std::vector<signatured_type *> *all_type_units
6784 = (std::vector<signatured_type *> *) datum;
1fd400ff 6785
b2bdb8cf 6786 all_type_units->push_back (sigt);
1fd400ff
TT
6787
6788 return 1;
6789}
6790
78d4d2c5 6791/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6792 and fill them into TYPES_HTAB. It will process only type units,
6793 therefore DW_UT_type. */
c88ee1f0 6794
78d4d2c5 6795static void
ed2dc618
SM
6796create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6797 struct dwo_file *dwo_file,
43988095
JK
6798 dwarf2_section_info *section, htab_t &types_htab,
6799 rcuh_kind section_kind)
348e048f 6800{
3019eac3 6801 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6802 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6803 bfd *abfd;
6804 const gdb_byte *info_ptr, *end_ptr;
348e048f 6805
4bdcc0c1
DE
6806 abbrev_section = (dwo_file != NULL
6807 ? &dwo_file->sections.abbrev
6808 : &dwarf2_per_objfile->abbrev);
6809
b4f54984 6810 if (dwarf_read_debug)
43988095
JK
6811 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6812 get_section_name (section),
a32a8923 6813 get_section_file_name (abbrev_section));
09406207 6814
78d4d2c5
JK
6815 dwarf2_read_section (objfile, section);
6816 info_ptr = section->buffer;
348e048f 6817
78d4d2c5
JK
6818 if (info_ptr == NULL)
6819 return;
348e048f 6820
78d4d2c5
JK
6821 /* We can't set abfd until now because the section may be empty or
6822 not present, in which case the bfd is unknown. */
6823 abfd = get_section_bfd_owner (section);
348e048f 6824
c0ab21c2
TT
6825 /* We don't use cutu_reader here because we don't need to read
6826 any dies: the signature is in the header. */
3019eac3 6827
78d4d2c5
JK
6828 end_ptr = info_ptr + section->size;
6829 while (info_ptr < end_ptr)
6830 {
78d4d2c5
JK
6831 struct signatured_type *sig_type;
6832 struct dwo_unit *dwo_tu;
6833 void **slot;
6834 const gdb_byte *ptr = info_ptr;
6835 struct comp_unit_head header;
6836 unsigned int length;
8b70b953 6837
9c541725 6838 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6839
a49dd8dd
JK
6840 /* Initialize it due to a false compiler warning. */
6841 header.signature = -1;
9c541725 6842 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6843
78d4d2c5
JK
6844 /* We need to read the type's signature in order to build the hash
6845 table, but we don't need anything else just yet. */
348e048f 6846
ed2dc618 6847 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6848 abbrev_section, ptr, section_kind);
348e048f 6849
78d4d2c5 6850 length = get_cu_length (&header);
6caca83c 6851
78d4d2c5
JK
6852 /* Skip dummy type units. */
6853 if (ptr >= info_ptr + length
43988095
JK
6854 || peek_abbrev_code (abfd, ptr) == 0
6855 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6856 {
6857 info_ptr += length;
6858 continue;
6859 }
dee91e82 6860
78d4d2c5
JK
6861 if (types_htab == NULL)
6862 {
6863 if (dwo_file)
6864 types_htab = allocate_dwo_unit_table (objfile);
6865 else
6866 types_htab = allocate_signatured_type_table (objfile);
6867 }
8b70b953 6868
78d4d2c5
JK
6869 if (dwo_file)
6870 {
6871 sig_type = NULL;
6872 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6873 struct dwo_unit);
6874 dwo_tu->dwo_file = dwo_file;
43988095 6875 dwo_tu->signature = header.signature;
9c541725 6876 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6877 dwo_tu->section = section;
9c541725 6878 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6879 dwo_tu->length = length;
6880 }
6881 else
6882 {
6883 /* N.B.: type_offset is not usable if this type uses a DWO file.
6884 The real type_offset is in the DWO file. */
6885 dwo_tu = NULL;
6886 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6887 struct signatured_type);
43988095 6888 sig_type->signature = header.signature;
9c541725 6889 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6890 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6891 sig_type->per_cu.is_debug_types = 1;
6892 sig_type->per_cu.section = section;
9c541725 6893 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6894 sig_type->per_cu.length = length;
6895 }
6896
6897 slot = htab_find_slot (types_htab,
6898 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6899 INSERT);
6900 gdb_assert (slot != NULL);
6901 if (*slot != NULL)
6902 {
9c541725 6903 sect_offset dup_sect_off;
0349ea22 6904
3019eac3
DE
6905 if (dwo_file)
6906 {
78d4d2c5
JK
6907 const struct dwo_unit *dup_tu
6908 = (const struct dwo_unit *) *slot;
6909
9c541725 6910 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6911 }
6912 else
6913 {
78d4d2c5
JK
6914 const struct signatured_type *dup_tu
6915 = (const struct signatured_type *) *slot;
6916
9c541725 6917 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6918 }
8b70b953 6919
b98664d3 6920 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6921 " the entry at offset %s, signature %s"),
6922 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6923 hex_string (header.signature));
78d4d2c5
JK
6924 }
6925 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6926
78d4d2c5 6927 if (dwarf_read_debug > 1)
9d8780f0
SM
6928 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6929 sect_offset_str (sect_off),
43988095 6930 hex_string (header.signature));
3019eac3 6931
78d4d2c5
JK
6932 info_ptr += length;
6933 }
6934}
3019eac3 6935
78d4d2c5
JK
6936/* Create the hash table of all entries in the .debug_types
6937 (or .debug_types.dwo) section(s).
6938 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6939 otherwise it is NULL.
b3c8eb43 6940
78d4d2c5 6941 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6942
78d4d2c5 6943 Note: This function processes DWO files only, not DWP files. */
348e048f 6944
78d4d2c5 6945static void
ed2dc618
SM
6946create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6947 struct dwo_file *dwo_file,
fd5866f6 6948 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6949 htab_t &types_htab)
6950{
fd5866f6
SM
6951 for (dwarf2_section_info &section : type_sections)
6952 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6953 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6954}
6955
6956/* Create the hash table of all entries in the .debug_types section,
6957 and initialize all_type_units.
6958 The result is zero if there is an error (e.g. missing .debug_types section),
6959 otherwise non-zero. */
6960
6961static int
ed2dc618 6962create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6963{
78d4d2c5 6964 htab_t types_htab = NULL;
3019eac3 6965
ed2dc618
SM
6966 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6967 &dwarf2_per_objfile->info, types_htab,
43988095 6968 rcuh_kind::COMPILE);
ed2dc618
SM
6969 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6970 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6971 if (types_htab == NULL)
6972 {
6973 dwarf2_per_objfile->signatured_types = NULL;
6974 return 0;
6975 }
6976
348e048f
DE
6977 dwarf2_per_objfile->signatured_types = types_htab;
6978
b2bdb8cf
SM
6979 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6980 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6981
6982 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6983 &dwarf2_per_objfile->all_type_units);
1fd400ff 6984
348e048f
DE
6985 return 1;
6986}
6987
6aa5f3a6
DE
6988/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6989 If SLOT is non-NULL, it is the entry to use in the hash table.
6990 Otherwise we find one. */
6991
6992static struct signatured_type *
ed2dc618
SM
6993add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6994 void **slot)
6aa5f3a6
DE
6995{
6996 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6997
b2bdb8cf
SM
6998 if (dwarf2_per_objfile->all_type_units.size ()
6999 == dwarf2_per_objfile->all_type_units.capacity ())
7000 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 7001
b2bdb8cf
SM
7002 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7003 struct signatured_type);
7004
7005 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
7006 sig_type->signature = sig;
7007 sig_type->per_cu.is_debug_types = 1;
7008 if (dwarf2_per_objfile->using_index)
7009 {
7010 sig_type->per_cu.v.quick =
7011 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7012 struct dwarf2_per_cu_quick_data);
7013 }
7014
7015 if (slot == NULL)
7016 {
7017 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7018 sig_type, INSERT);
7019 }
7020 gdb_assert (*slot == NULL);
7021 *slot = sig_type;
7022 /* The rest of sig_type must be filled in by the caller. */
7023 return sig_type;
7024}
7025
a2ce51a0
DE
7026/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7027 Fill in SIG_ENTRY with DWO_ENTRY. */
7028
7029static void
ed2dc618 7030fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
7031 struct signatured_type *sig_entry,
7032 struct dwo_unit *dwo_entry)
7033{
7ee85ab1 7034 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7035 gdb_assert (! sig_entry->per_cu.queued);
7036 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7037 if (dwarf2_per_objfile->using_index)
7038 {
7039 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7040 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7041 }
7042 else
7043 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7044 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7045 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7046 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7047 gdb_assert (sig_entry->dwo_unit == NULL);
7048
7049 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7050 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7051 sig_entry->per_cu.length = dwo_entry->length;
7052 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7053 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7054 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7055 sig_entry->dwo_unit = dwo_entry;
7056}
7057
7058/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7059 If we haven't read the TU yet, create the signatured_type data structure
7060 for a TU to be read in directly from a DWO file, bypassing the stub.
7061 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7062 using .gdb_index, then when reading a CU we want to stay in the DWO file
7063 containing that CU. Otherwise we could end up reading several other DWO
7064 files (due to comdat folding) to process the transitive closure of all the
7065 mentioned TUs, and that can be slow. The current DWO file will have every
7066 type signature that it needs.
a2ce51a0
DE
7067 We only do this for .gdb_index because in the psymtab case we already have
7068 to read all the DWOs to build the type unit groups. */
7069
7070static struct signatured_type *
7071lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7072{
518817b3
SM
7073 struct dwarf2_per_objfile *dwarf2_per_objfile
7074 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7075 struct objfile *objfile = dwarf2_per_objfile->objfile;
7076 struct dwo_file *dwo_file;
7077 struct dwo_unit find_dwo_entry, *dwo_entry;
7078 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7079 void **slot;
a2ce51a0
DE
7080
7081 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7082
6aa5f3a6
DE
7083 /* If TU skeletons have been removed then we may not have read in any
7084 TUs yet. */
7085 if (dwarf2_per_objfile->signatured_types == NULL)
7086 {
7087 dwarf2_per_objfile->signatured_types
7088 = allocate_signatured_type_table (objfile);
7089 }
a2ce51a0
DE
7090
7091 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7092 Use the global signatured_types array to do our own comdat-folding
7093 of types. If this is the first time we're reading this TU, and
7094 the TU has an entry in .gdb_index, replace the recorded data from
7095 .gdb_index with this TU. */
a2ce51a0 7096
a2ce51a0 7097 find_sig_entry.signature = sig;
6aa5f3a6
DE
7098 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7099 &find_sig_entry, INSERT);
9a3c8263 7100 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7101
7102 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7103 read. Don't reassign the global entry to point to this DWO if that's
7104 the case. Also note that if the TU is already being read, it may not
7105 have come from a DWO, the program may be a mix of Fission-compiled
7106 code and non-Fission-compiled code. */
7107
7108 /* Have we already tried to read this TU?
7109 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7110 needn't exist in the global table yet). */
7111 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7112 return sig_entry;
7113
6aa5f3a6
DE
7114 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7115 dwo_unit of the TU itself. */
7116 dwo_file = cu->dwo_unit->dwo_file;
7117
a2ce51a0
DE
7118 /* Ok, this is the first time we're reading this TU. */
7119 if (dwo_file->tus == NULL)
7120 return NULL;
7121 find_dwo_entry.signature = sig;
9a3c8263 7122 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7123 if (dwo_entry == NULL)
7124 return NULL;
7125
6aa5f3a6
DE
7126 /* If the global table doesn't have an entry for this TU, add one. */
7127 if (sig_entry == NULL)
ed2dc618 7128 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7129
ed2dc618 7130 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7131 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7132 return sig_entry;
7133}
7134
a2ce51a0
DE
7135/* Subroutine of lookup_signatured_type.
7136 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7137 then try the DWP file. If the TU stub (skeleton) has been removed then
7138 it won't be in .gdb_index. */
a2ce51a0
DE
7139
7140static struct signatured_type *
7141lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7142{
518817b3
SM
7143 struct dwarf2_per_objfile *dwarf2_per_objfile
7144 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7145 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7146 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7147 struct dwo_unit *dwo_entry;
7148 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7149 void **slot;
a2ce51a0
DE
7150
7151 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7152 gdb_assert (dwp_file != NULL);
7153
6aa5f3a6
DE
7154 /* If TU skeletons have been removed then we may not have read in any
7155 TUs yet. */
7156 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7157 {
6aa5f3a6
DE
7158 dwarf2_per_objfile->signatured_types
7159 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7160 }
7161
6aa5f3a6
DE
7162 find_sig_entry.signature = sig;
7163 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7164 &find_sig_entry, INSERT);
9a3c8263 7165 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7166
7167 /* Have we already tried to read this TU?
7168 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7169 needn't exist in the global table yet). */
7170 if (sig_entry != NULL)
7171 return sig_entry;
7172
a2ce51a0
DE
7173 if (dwp_file->tus == NULL)
7174 return NULL;
ed2dc618 7175 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7176 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7177 if (dwo_entry == NULL)
7178 return NULL;
7179
ed2dc618
SM
7180 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7181 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7182
a2ce51a0
DE
7183 return sig_entry;
7184}
7185
380bca97 7186/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7187 Returns NULL if signature SIG is not present in the table.
7188 It is up to the caller to complain about this. */
348e048f
DE
7189
7190static struct signatured_type *
a2ce51a0 7191lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7192{
518817b3
SM
7193 struct dwarf2_per_objfile *dwarf2_per_objfile
7194 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7195
a2ce51a0
DE
7196 if (cu->dwo_unit
7197 && dwarf2_per_objfile->using_index)
7198 {
7199 /* We're in a DWO/DWP file, and we're using .gdb_index.
7200 These cases require special processing. */
ed2dc618 7201 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7202 return lookup_dwo_signatured_type (cu, sig);
7203 else
7204 return lookup_dwp_signatured_type (cu, sig);
7205 }
7206 else
7207 {
7208 struct signatured_type find_entry, *entry;
348e048f 7209
a2ce51a0
DE
7210 if (dwarf2_per_objfile->signatured_types == NULL)
7211 return NULL;
7212 find_entry.signature = sig;
9a3c8263
SM
7213 entry = ((struct signatured_type *)
7214 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7215 return entry;
7216 }
348e048f 7217}
18a8505e
AT
7218
7219/* Return the address base of the compile unit, which, if exists, is stored
7220 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
7221static gdb::optional<ULONGEST>
7222lookup_addr_base (struct die_info *comp_unit_die)
7223{
7224 struct attribute *attr;
7225 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
7226 if (attr == nullptr)
7227 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
7228 if (attr == nullptr)
7229 return gdb::optional<ULONGEST> ();
7230 return DW_UNSND (attr);
7231}
7232
7233/* Return range lists base of the compile unit, which, if exists, is stored
7234 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
7235static ULONGEST
7236lookup_ranges_base (struct die_info *comp_unit_die)
7237{
7238 struct attribute *attr;
7239 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
7240 if (attr == nullptr)
7241 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
7242 if (attr == nullptr)
7243 return 0;
7244 return DW_UNSND (attr);
7245}
7246
42e7ad6c 7247/* Low level DIE reading support. */
348e048f 7248
d85a05f0
DJ
7249/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7250
7251static void
7252init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7253 struct dwarf2_cu *cu,
3019eac3 7254 struct dwarf2_section_info *section,
685af9cd
TT
7255 struct dwo_file *dwo_file,
7256 struct abbrev_table *abbrev_table)
d85a05f0 7257{
fceca515 7258 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7259 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7260 reader->cu = cu;
3019eac3 7261 reader->dwo_file = dwo_file;
dee91e82
DE
7262 reader->die_section = section;
7263 reader->buffer = section->buffer;
f664829e 7264 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7265 reader->comp_dir = NULL;
685af9cd 7266 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7267}
7268
c0ab21c2 7269/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7270 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 7271 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
7272 already.
7273
7274 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7275 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7276 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7277 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7278 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7279 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7280 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7281 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7282 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7283 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7284 kept around for at least as long as *RESULT_READER.
7285
b0c7bfa9
DE
7286 The result is non-zero if a valid (non-dummy) DIE was found. */
7287
7288static int
7289read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7290 struct dwo_unit *dwo_unit,
b0c7bfa9 7291 struct die_info *stub_comp_unit_die,
a2ce51a0 7292 const char *stub_comp_dir,
b0c7bfa9 7293 struct die_reader_specs *result_reader,
d521ce57 7294 const gdb_byte **result_info_ptr,
b0c7bfa9 7295 struct die_info **result_comp_unit_die,
685af9cd
TT
7296 int *result_has_children,
7297 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7298{
ed2dc618 7299 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7300 struct objfile *objfile = dwarf2_per_objfile->objfile;
7301 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7302 bfd *abfd;
d521ce57 7303 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7304 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7305 int i,num_extra_attrs;
7306 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
7307 struct die_info *comp_unit_die;
7308
b0aeadb3
DE
7309 /* At most one of these may be provided. */
7310 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7311
b0c7bfa9
DE
7312 /* These attributes aren't processed until later:
7313 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7314 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7315 referenced later. However, these attributes are found in the stub
7316 which we won't have later. In order to not impose this complication
7317 on the rest of the code, we read them here and copy them to the
7318 DWO CU/TU die. */
b0c7bfa9
DE
7319
7320 stmt_list = NULL;
7321 low_pc = NULL;
7322 high_pc = NULL;
7323 ranges = NULL;
7324 comp_dir = NULL;
7325
7326 if (stub_comp_unit_die != NULL)
7327 {
7328 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7329 DWO file. */
7330 if (! this_cu->is_debug_types)
7331 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7332 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7333 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7334 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7335 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7336
18a8505e 7337 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 7338
18a8505e
AT
7339 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
7340 here (if needed). We need the value before we can process
7341 DW_AT_ranges. */
7342 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 7343 }
a2ce51a0
DE
7344 else if (stub_comp_dir != NULL)
7345 {
7346 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7347 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7348 comp_dir->name = DW_AT_comp_dir;
7349 comp_dir->form = DW_FORM_string;
7350 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7351 DW_STRING (comp_dir) = stub_comp_dir;
7352 }
b0c7bfa9
DE
7353
7354 /* Set up for reading the DWO CU/TU. */
7355 cu->dwo_unit = dwo_unit;
685af9cd 7356 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7357 dwarf2_read_section (objfile, section);
a32a8923 7358 abfd = get_section_bfd_owner (section);
9c541725
PA
7359 begin_info_ptr = info_ptr = (section->buffer
7360 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7361 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7362
7363 if (this_cu->is_debug_types)
7364 {
b0c7bfa9
DE
7365 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7366
ed2dc618
SM
7367 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7368 &cu->header, section,
b0c7bfa9 7369 dwo_abbrev_section,
43988095 7370 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7371 /* This is not an assert because it can be caused by bad debug info. */
43988095 7372 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7373 {
7374 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7375 " TU at offset %s [in module %s]"),
a2ce51a0 7376 hex_string (sig_type->signature),
43988095 7377 hex_string (cu->header.signature),
9d8780f0 7378 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7379 bfd_get_filename (abfd));
7380 }
9c541725 7381 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7382 /* For DWOs coming from DWP files, we don't know the CU length
7383 nor the type's offset in the TU until now. */
7384 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7385 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7386
7387 /* Establish the type offset that can be used to lookup the type.
7388 For DWO files, we don't know it until now. */
9c541725
PA
7389 sig_type->type_offset_in_section
7390 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7391 }
7392 else
7393 {
ed2dc618
SM
7394 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7395 &cu->header, section,
b0c7bfa9 7396 dwo_abbrev_section,
43988095 7397 info_ptr, rcuh_kind::COMPILE);
9c541725 7398 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7399 /* For DWOs coming from DWP files, we don't know the CU length
7400 until now. */
7401 dwo_unit->length = get_cu_length (&cu->header);
7402 }
7403
685af9cd
TT
7404 *result_dwo_abbrev_table
7405 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7406 cu->header.abbrev_sect_off);
7407 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7408 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7409
7410 /* Read in the die, but leave space to copy over the attributes
7411 from the stub. This has the benefit of simplifying the rest of
7412 the code - all the work to maintain the illusion of a single
7413 DW_TAG_{compile,type}_unit DIE is done here. */
7414 num_extra_attrs = ((stmt_list != NULL)
7415 + (low_pc != NULL)
7416 + (high_pc != NULL)
7417 + (ranges != NULL)
7418 + (comp_dir != NULL));
7419 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7420 result_has_children, num_extra_attrs);
7421
7422 /* Copy over the attributes from the stub to the DIE we just read in. */
7423 comp_unit_die = *result_comp_unit_die;
7424 i = comp_unit_die->num_attrs;
7425 if (stmt_list != NULL)
7426 comp_unit_die->attrs[i++] = *stmt_list;
7427 if (low_pc != NULL)
7428 comp_unit_die->attrs[i++] = *low_pc;
7429 if (high_pc != NULL)
7430 comp_unit_die->attrs[i++] = *high_pc;
7431 if (ranges != NULL)
7432 comp_unit_die->attrs[i++] = *ranges;
7433 if (comp_dir != NULL)
7434 comp_unit_die->attrs[i++] = *comp_dir;
7435 comp_unit_die->num_attrs += num_extra_attrs;
7436
b4f54984 7437 if (dwarf_die_debug)
bf6af496
DE
7438 {
7439 fprintf_unfiltered (gdb_stdlog,
7440 "Read die from %s@0x%x of %s:\n",
a32a8923 7441 get_section_name (section),
bf6af496
DE
7442 (unsigned) (begin_info_ptr - section->buffer),
7443 bfd_get_filename (abfd));
b4f54984 7444 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7445 }
7446
a2ce51a0
DE
7447 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7448 TUs by skipping the stub and going directly to the entry in the DWO file.
7449 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7450 to get it via circuitous means. Blech. */
7451 if (comp_dir != NULL)
7452 result_reader->comp_dir = DW_STRING (comp_dir);
7453
b0c7bfa9
DE
7454 /* Skip dummy compilation units. */
7455 if (info_ptr >= begin_info_ptr + dwo_unit->length
7456 || peek_abbrev_code (abfd, info_ptr) == 0)
7457 return 0;
7458
7459 *result_info_ptr = info_ptr;
7460 return 1;
7461}
7462
a084a2a6
AT
7463/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7464 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7465 signature is part of the header. */
7466static gdb::optional<ULONGEST>
7467lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7468{
7469 if (cu->header.version >= 5)
7470 return cu->header.signature;
7471 struct attribute *attr;
7472 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7473 if (attr == nullptr)
7474 return gdb::optional<ULONGEST> ();
7475 return DW_UNSND (attr);
7476}
7477
c0ab21c2 7478/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7479 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7480 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7481
7482static struct dwo_unit *
7483lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
7484 struct die_info *comp_unit_die,
7485 const char *dwo_name)
b0c7bfa9
DE
7486{
7487 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7488 struct dwo_unit *dwo_unit;
c0ab21c2 7489 const char *comp_dir;
b0c7bfa9 7490
a2ce51a0
DE
7491 gdb_assert (cu != NULL);
7492
b0c7bfa9 7493 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7494 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7495 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7496
7497 if (this_cu->is_debug_types)
7498 {
7499 struct signatured_type *sig_type;
7500
7501 /* Since this_cu is the first member of struct signatured_type,
7502 we can go from a pointer to one to a pointer to the other. */
7503 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7504 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7505 }
7506 else
7507 {
a084a2a6
AT
7508 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7509 if (!signature.has_value ())
b0c7bfa9
DE
7510 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7511 " [in module %s]"),
e3b94546 7512 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7513 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7514 *signature);
b0c7bfa9
DE
7515 }
7516
b0c7bfa9
DE
7517 return dwo_unit;
7518}
7519
c0ab21c2 7520/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7521 See it for a description of the parameters.
fcd3b13d 7522 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7523
c0ab21c2
TT
7524void
7525cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7526 int use_existing_cu, int keep)
a2ce51a0 7527{
a2ce51a0 7528 struct signatured_type *sig_type;
a2ce51a0 7529 struct die_reader_specs reader;
a2ce51a0
DE
7530
7531 /* Verify we can do the following downcast, and that we have the
7532 data we need. */
7533 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7534 sig_type = (struct signatured_type *) this_cu;
7535 gdb_assert (sig_type->dwo_unit != NULL);
7536
6aa5f3a6
DE
7537 if (use_existing_cu && this_cu->cu != NULL)
7538 {
7539 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7540 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7541 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7542 }
7543 else
7544 {
7545 /* If !use_existing_cu, this_cu->cu must be NULL. */
7546 gdb_assert (this_cu->cu == NULL);
c0ab21c2 7547 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7548 }
7549
7550 /* A future optimization, if needed, would be to use an existing
7551 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7552 could share abbrev tables. */
a2ce51a0
DE
7553
7554 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7555 NULL /* stub_comp_unit_die */,
7556 sig_type->dwo_unit->dwo_file->comp_dir,
7557 &reader, &info_ptr,
685af9cd 7558 &comp_unit_die, &has_children,
c0ab21c2 7559 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7560 {
7561 /* Dummy die. */
c0ab21c2 7562 dummy_p = true;
a2ce51a0 7563 }
a2ce51a0
DE
7564}
7565
fd820528 7566/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7567 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7568
f4dc4d17
DE
7569 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7570 Otherwise the table specified in the comp unit header is read in and used.
7571 This is an optimization for when we already have the abbrev table.
7572
dee91e82
DE
7573 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7574 Otherwise, a new CU is allocated with xmalloc.
7575
7576 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
c0ab21c2
TT
7577 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7578 end. */
aaa75496 7579
c0ab21c2
TT
7580cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7581 struct abbrev_table *abbrev_table,
7582 int use_existing_cu, int keep,
7583 bool skip_partial)
7584 : die_reader_specs {},
7585 m_this_cu (this_cu),
7586 m_keep (keep)
c906108c 7587{
ed2dc618 7588 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7589 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7590 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7591 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7592 struct dwarf2_cu *cu;
c0ab21c2 7593 const gdb_byte *begin_info_ptr;
dee91e82 7594 struct signatured_type *sig_type = NULL;
4bdcc0c1 7595 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7596 /* Non-zero if CU currently points to a DWO file and we need to
7597 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7598 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7599 int rereading_dwo_cu = 0;
c906108c 7600
b4f54984 7601 if (dwarf_die_debug)
9d8780f0 7602 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7603 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7604 sect_offset_str (this_cu->sect_off));
09406207 7605
dee91e82
DE
7606 if (use_existing_cu)
7607 gdb_assert (keep);
23745b47 7608
a2ce51a0
DE
7609 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7610 file (instead of going through the stub), short-circuit all of this. */
7611 if (this_cu->reading_dwo_directly)
7612 {
7613 /* Narrow down the scope of possibilities to have to understand. */
7614 gdb_assert (this_cu->is_debug_types);
7615 gdb_assert (abbrev_table == NULL);
c0ab21c2 7616 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
a2ce51a0
DE
7617 return;
7618 }
7619
dee91e82
DE
7620 /* This is cheap if the section is already read in. */
7621 dwarf2_read_section (objfile, section);
7622
9c541725 7623 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7624
7625 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7626
7627 if (use_existing_cu && this_cu->cu != NULL)
7628 {
7629 cu = this_cu->cu;
42e7ad6c
DE
7630 /* If this CU is from a DWO file we need to start over, we need to
7631 refetch the attributes from the skeleton CU.
7632 This could be optimized by retrieving those attributes from when we
7633 were here the first time: the previous comp_unit_die was stored in
7634 comp_unit_obstack. But there's no data yet that we need this
7635 optimization. */
7636 if (cu->dwo_unit != NULL)
7637 rereading_dwo_cu = 1;
dee91e82
DE
7638 }
7639 else
7640 {
7641 /* If !use_existing_cu, this_cu->cu must be NULL. */
7642 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
7643 m_new_cu.reset (new dwarf2_cu (this_cu));
7644 cu = m_new_cu.get ();
42e7ad6c 7645 }
dee91e82 7646
b0c7bfa9 7647 /* Get the header. */
9c541725 7648 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7649 {
7650 /* We already have the header, there's no need to read it in again. */
9c541725 7651 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7652 }
7653 else
7654 {
3019eac3 7655 if (this_cu->is_debug_types)
dee91e82 7656 {
ed2dc618
SM
7657 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7658 &cu->header, section,
4bdcc0c1 7659 abbrev_section, info_ptr,
43988095 7660 rcuh_kind::TYPE);
dee91e82 7661
42e7ad6c
DE
7662 /* Since per_cu is the first member of struct signatured_type,
7663 we can go from a pointer to one to a pointer to the other. */
7664 sig_type = (struct signatured_type *) this_cu;
43988095 7665 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7666 gdb_assert (sig_type->type_offset_in_tu
7667 == cu->header.type_cu_offset_in_tu);
7668 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7669
42e7ad6c
DE
7670 /* LENGTH has not been set yet for type units if we're
7671 using .gdb_index. */
1ce1cefd 7672 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7673
7674 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7675 sig_type->type_offset_in_section =
7676 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7677
7678 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7679 }
7680 else
7681 {
ed2dc618
SM
7682 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7683 &cu->header, section,
4bdcc0c1 7684 abbrev_section,
43988095
JK
7685 info_ptr,
7686 rcuh_kind::COMPILE);
dee91e82 7687
9c541725 7688 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7689 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7690 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7691 }
7692 }
10b3939b 7693
6caca83c 7694 /* Skip dummy compilation units. */
dee91e82 7695 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7696 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7697 {
7698 dummy_p = true;
7699 return;
7700 }
6caca83c 7701
433df2d4
DE
7702 /* If we don't have them yet, read the abbrevs for this compilation unit.
7703 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7704 done. */
f4dc4d17 7705 if (abbrev_table != NULL)
685af9cd
TT
7706 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7707 else
f4dc4d17 7708 {
c0ab21c2 7709 m_abbrev_table_holder
685af9cd
TT
7710 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7711 cu->header.abbrev_sect_off);
c0ab21c2 7712 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7713 }
af703f96 7714
dee91e82 7715 /* Read the top level CU/TU die. */
c0ab21c2
TT
7716 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7717 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
93311388 7718
58f0c718 7719 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7720 {
7721 dummy_p = true;
7722 return;
7723 }
58f0c718 7724
b0c7bfa9 7725 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7726 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7727 table from the DWO file and pass the ownership over to us. It will be
7728 referenced from READER, so we must make sure to free it after we're done
7729 with READER.
7730
b0c7bfa9
DE
7731 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7732 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7733 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7734 if (dwo_name != nullptr)
3019eac3 7735 {
3019eac3 7736 struct dwo_unit *dwo_unit;
b0c7bfa9 7737 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7738
7739 if (has_children)
6a506a2d 7740 {
b98664d3 7741 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7742 " has children (offset %s) [in module %s]"),
7743 sect_offset_str (this_cu->sect_off),
7744 bfd_get_filename (abfd));
6a506a2d 7745 }
c0ab21c2 7746 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7747 if (dwo_unit != NULL)
3019eac3 7748 {
6a506a2d 7749 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7750 comp_unit_die, NULL,
c0ab21c2 7751 this, &info_ptr,
685af9cd 7752 &dwo_comp_unit_die, &has_children,
c0ab21c2 7753 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7754 {
7755 /* Dummy die. */
c0ab21c2 7756 dummy_p = true;
6a506a2d
DE
7757 return;
7758 }
7759 comp_unit_die = dwo_comp_unit_die;
7760 }
7761 else
7762 {
7763 /* Yikes, we couldn't find the rest of the DIE, we only have
7764 the stub. A complaint has already been logged. There's
7765 not much more we can do except pass on the stub DIE to
7766 die_reader_func. We don't want to throw an error on bad
7767 debug info. */
3019eac3
DE
7768 }
7769 }
c0ab21c2 7770}
3019eac3 7771
c0ab21c2
TT
7772cutu_reader::~cutu_reader ()
7773{
b0c7bfa9 7774 /* Done, clean up. */
c0ab21c2 7775 if (m_new_cu != NULL && m_keep && !dummy_p)
348e048f 7776 {
c0ab21c2
TT
7777 struct dwarf2_per_objfile *dwarf2_per_objfile
7778 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7779 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7780 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7781 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7782 /* The chain owns it now. */
c0ab21c2 7783 m_new_cu.release ();
348e048f 7784 }
dee91e82
DE
7785}
7786
18a8505e
AT
7787/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7788 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7789 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7790
7791 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7792 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7793
7794 We fill in THIS_CU->length.
7795
dee91e82 7796 THIS_CU->cu is always freed when done.
3019eac3 7797 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7798 to care whether it refers to the "main" CU or the DWO CU.
7799
7800 When parent_cu is passed, it is used to provide a default value for
7801 str_offsets_base and addr_base from the parent. */
dee91e82 7802
c0ab21c2
TT
7803cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7804 struct dwarf2_cu *parent_cu,
7805 struct dwo_file *dwo_file)
7806 : die_reader_specs {},
7807 m_this_cu (this_cu)
dee91e82 7808{
ed2dc618 7809 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7810 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7811 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7812 bfd *abfd = get_section_bfd_owner (section);
33e80786 7813 struct dwarf2_section_info *abbrev_section;
d521ce57 7814 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
7815 int has_children;
7816
b4f54984 7817 if (dwarf_die_debug)
9d8780f0 7818 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7819 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7820 sect_offset_str (this_cu->sect_off));
09406207 7821
dee91e82
DE
7822 gdb_assert (this_cu->cu == NULL);
7823
33e80786
DE
7824 abbrev_section = (dwo_file != NULL
7825 ? &dwo_file->sections.abbrev
7826 : get_abbrev_section_for_cu (this_cu));
7827
dee91e82
DE
7828 /* This is cheap if the section is already read in. */
7829 dwarf2_read_section (objfile, section);
7830
c0ab21c2 7831 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7832
9c541725 7833 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7834 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7835 &m_new_cu->header, section,
4bdcc0c1 7836 abbrev_section, info_ptr,
43988095
JK
7837 (this_cu->is_debug_types
7838 ? rcuh_kind::TYPE
7839 : rcuh_kind::COMPILE));
dee91e82 7840
18a8505e
AT
7841 if (parent_cu != nullptr)
7842 {
c0ab21c2
TT
7843 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7844 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7845 }
c0ab21c2 7846 this_cu->length = get_cu_length (&m_new_cu->header);
dee91e82
DE
7847
7848 /* Skip dummy compilation units. */
7849 if (info_ptr >= begin_info_ptr + this_cu->length
7850 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7851 {
7852 dummy_p = true;
7853 return;
7854 }
72bf9492 7855
c0ab21c2 7856 m_abbrev_table_holder
685af9cd 7857 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
c0ab21c2 7858 m_new_cu->header.abbrev_sect_off);
dee91e82 7859
c0ab21c2
TT
7860 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7861 m_abbrev_table_holder.get ());
7862 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
dee91e82
DE
7863}
7864
0018ea6f
DE
7865\f
7866/* Type Unit Groups.
dee91e82 7867
0018ea6f
DE
7868 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7869 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7870 so that all types coming from the same compilation (.o file) are grouped
7871 together. A future step could be to put the types in the same symtab as
7872 the CU the types ultimately came from. */
ff013f42 7873
f4dc4d17
DE
7874static hashval_t
7875hash_type_unit_group (const void *item)
7876{
9a3c8263
SM
7877 const struct type_unit_group *tu_group
7878 = (const struct type_unit_group *) item;
f4dc4d17 7879
094b34ac 7880 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7881}
348e048f
DE
7882
7883static int
f4dc4d17 7884eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7885{
9a3c8263
SM
7886 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7887 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7888
094b34ac 7889 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7890}
348e048f 7891
f4dc4d17
DE
7892/* Allocate a hash table for type unit groups. */
7893
7894static htab_t
ed2dc618 7895allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7896{
7897 return htab_create_alloc_ex (3,
7898 hash_type_unit_group,
7899 eq_type_unit_group,
7900 NULL,
ed2dc618 7901 &objfile->objfile_obstack,
f4dc4d17
DE
7902 hashtab_obstack_allocate,
7903 dummy_obstack_deallocate);
7904}
dee91e82 7905
f4dc4d17
DE
7906/* Type units that don't have DW_AT_stmt_list are grouped into their own
7907 partial symtabs. We combine several TUs per psymtab to not let the size
7908 of any one psymtab grow too big. */
7909#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7910#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7911
094b34ac 7912/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7913 Create the type_unit_group object used to hold one or more TUs. */
7914
7915static struct type_unit_group *
094b34ac 7916create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7917{
518817b3
SM
7918 struct dwarf2_per_objfile *dwarf2_per_objfile
7919 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7920 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7921 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7922 struct type_unit_group *tu_group;
f4dc4d17
DE
7923
7924 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7925 struct type_unit_group);
094b34ac 7926 per_cu = &tu_group->per_cu;
518817b3 7927 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7928
094b34ac
DE
7929 if (dwarf2_per_objfile->using_index)
7930 {
7931 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7932 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7933 }
7934 else
7935 {
9c541725 7936 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7937 struct partial_symtab *pst;
528e1572 7938 std::string name;
094b34ac
DE
7939
7940 /* Give the symtab a useful name for debug purposes. */
7941 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7942 name = string_printf ("<type_units_%d>",
7943 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7944 else
528e1572 7945 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7946
528e1572 7947 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7948 pst->anonymous = true;
094b34ac 7949 }
f4dc4d17 7950
094b34ac 7951 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7952 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7953
7954 return tu_group;
7955}
7956
094b34ac
DE
7957/* Look up the type_unit_group for type unit CU, and create it if necessary.
7958 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7959
7960static struct type_unit_group *
ff39bb5e 7961get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7962{
518817b3
SM
7963 struct dwarf2_per_objfile *dwarf2_per_objfile
7964 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7965 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7966 struct type_unit_group *tu_group;
7967 void **slot;
7968 unsigned int line_offset;
7969 struct type_unit_group type_unit_group_for_lookup;
7970
7971 if (dwarf2_per_objfile->type_unit_groups == NULL)
7972 {
7973 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7974 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7975 }
7976
7977 /* Do we need to create a new group, or can we use an existing one? */
7978
7979 if (stmt_list)
7980 {
7981 line_offset = DW_UNSND (stmt_list);
7982 ++tu_stats->nr_symtab_sharers;
7983 }
7984 else
7985 {
7986 /* Ugh, no stmt_list. Rare, but we have to handle it.
7987 We can do various things here like create one group per TU or
7988 spread them over multiple groups to split up the expansion work.
7989 To avoid worst case scenarios (too many groups or too large groups)
7990 we, umm, group them in bunches. */
7991 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7992 | (tu_stats->nr_stmt_less_type_units
7993 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7994 ++tu_stats->nr_stmt_less_type_units;
7995 }
7996
094b34ac 7997 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7998 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7999 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8000 &type_unit_group_for_lookup, INSERT);
8001 if (*slot != NULL)
8002 {
9a3c8263 8003 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
8004 gdb_assert (tu_group != NULL);
8005 }
8006 else
8007 {
9c541725 8008 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 8009 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
8010 *slot = tu_group;
8011 ++tu_stats->nr_symtabs;
8012 }
8013
8014 return tu_group;
8015}
0018ea6f
DE
8016\f
8017/* Partial symbol tables. */
8018
8019/* Create a psymtab named NAME and assign it to PER_CU.
8020
8021 The caller must fill in the following details:
8022 dirname, textlow, texthigh. */
8023
8024static struct partial_symtab *
8025create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8026{
e3b94546 8027 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8028 struct partial_symtab *pst;
8029
939652a5 8030 pst = start_psymtab_common (objfile, name, 0);
0018ea6f 8031
6d94535f 8032 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
8033
8034 /* This is the glue that links PST into GDB's symbol API. */
8035 pst->read_symtab_private = per_cu;
8036 pst->read_symtab = dwarf2_read_symtab;
8037 per_cu->v.psymtab = pst;
8038
8039 return pst;
8040}
8041
c0ab21c2 8042/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
8043
8044static void
8045process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8046 const gdb_byte *info_ptr,
0018ea6f
DE
8047 struct die_info *comp_unit_die,
8048 int has_children,
c0ab21c2
TT
8049 int want_partial_unit,
8050 enum language pretend_language)
0018ea6f
DE
8051{
8052 struct dwarf2_cu *cu = reader->cu;
518817b3 8053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8054 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8055 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8056 CORE_ADDR baseaddr;
8057 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8058 struct partial_symtab *pst;
3a2b436a 8059 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8060 const char *filename;
0018ea6f 8061
c0ab21c2 8062 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
0018ea6f
DE
8063 return;
8064
8065 gdb_assert (! per_cu->is_debug_types);
8066
c0ab21c2 8067 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 8068
0018ea6f 8069 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8070 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8071 if (filename == NULL)
0018ea6f 8072 filename = "";
0018ea6f
DE
8073
8074 pst = create_partial_symtab (per_cu, filename);
8075
8076 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8077 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 8078
b3b3bada 8079 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
8080
8081 dwarf2_find_base_address (comp_unit_die, cu);
8082
8083 /* Possibly set the default values of LOWPC and HIGHPC from
8084 `DW_AT_ranges'. */
3a2b436a
JK
8085 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8086 &best_highpc, cu, pst);
8087 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8088 {
8089 CORE_ADDR low
8090 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8091 - baseaddr);
8092 CORE_ADDR high
8093 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8094 - baseaddr - 1);
8095 /* Store the contiguous range if it is not empty; it can be
8096 empty for CUs with no code. */
d320c2b5
TT
8097 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8098 low, high, pst);
79748972 8099 }
0018ea6f
DE
8100
8101 /* Check if comp unit has_children.
8102 If so, read the rest of the partial symbols from this comp unit.
8103 If not, there's no more debug_info for this comp unit. */
8104 if (has_children)
8105 {
8106 struct partial_die_info *first_die;
8107 CORE_ADDR lowpc, highpc;
8108
8109 lowpc = ((CORE_ADDR) -1);
8110 highpc = ((CORE_ADDR) 0);
8111
8112 first_die = load_partial_dies (reader, info_ptr, 1);
8113
8114 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8115 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8116
8117 /* If we didn't find a lowpc, set it to highpc to avoid
8118 complaints from `maint check'. */
8119 if (lowpc == ((CORE_ADDR) -1))
8120 lowpc = highpc;
8121
8122 /* If the compilation unit didn't have an explicit address range,
8123 then use the information extracted from its child dies. */
e385593e 8124 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8125 {
8126 best_lowpc = lowpc;
8127 best_highpc = highpc;
8128 }
8129 }
4ae976d1 8130 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8131 best_lowpc + baseaddr)
8132 - baseaddr);
4ae976d1 8133 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8134 best_highpc + baseaddr)
8135 - baseaddr);
0018ea6f 8136
8763cede 8137 end_psymtab_common (objfile, pst);
0018ea6f 8138
ae640021 8139 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8140 {
8141 int i;
ae640021 8142 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8143
8144 /* Fill in 'dependencies' here; we fill in 'users' in a
8145 post-pass. */
8146 pst->number_of_dependencies = len;
a9342b62
TT
8147 pst->dependencies
8148 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8149 for (i = 0; i < len; ++i)
8150 {
8151 pst->dependencies[i]
8152 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8153 }
0018ea6f 8154
ae640021 8155 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8156 }
8157
8158 /* Get the list of files included in the current compilation unit,
8159 and build a psymtab for each of them. */
8160 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8161
b4f54984 8162 if (dwarf_read_debug)
b926417a
TT
8163 fprintf_unfiltered (gdb_stdlog,
8164 "Psymtab for %s unit @%s: %s - %s"
8165 ", %d global, %d static syms\n",
8166 per_cu->is_debug_types ? "type" : "comp",
8167 sect_offset_str (per_cu->sect_off),
8168 paddress (gdbarch, pst->text_low (objfile)),
8169 paddress (gdbarch, pst->text_high (objfile)),
8170 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8171}
8172
8173/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8174 Process compilation unit THIS_CU for a psymtab. */
8175
8176static void
8177process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8178 int want_partial_unit,
8179 enum language pretend_language)
0018ea6f
DE
8180{
8181 /* If this compilation unit was already read in, free the
8182 cached copy in order to read it in again. This is
8183 necessary because we skipped some symbols when we first
8184 read in the compilation unit (see load_partial_dies).
8185 This problem could be avoided, but the benefit is unclear. */
8186 if (this_cu->cu != NULL)
8187 free_one_cached_comp_unit (this_cu);
8188
c0ab21c2
TT
8189 cutu_reader reader (this_cu, NULL, 0, 0, false);
8190
8191 if (reader.dummy_p)
f1902523 8192 {
c0ab21c2 8193 /* Nothing. */
f1902523 8194 }
c0ab21c2
TT
8195 else if (this_cu->is_debug_types)
8196 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
8197 reader.has_children);
8198 else
8199 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
8200 reader.comp_unit_die,
8201 reader.has_children,
8202 want_partial_unit,
8203 pretend_language);
0018ea6f
DE
8204
8205 /* Age out any secondary CUs. */
ed2dc618 8206 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8207}
f4dc4d17
DE
8208
8209/* Reader function for build_type_psymtabs. */
8210
8211static void
8212build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8213 const gdb_byte *info_ptr,
f4dc4d17 8214 struct die_info *type_unit_die,
c0ab21c2 8215 int has_children)
f4dc4d17 8216{
ed2dc618 8217 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8218 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8219 struct objfile *objfile = dwarf2_per_objfile->objfile;
8220 struct dwarf2_cu *cu = reader->cu;
8221 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8222 struct signatured_type *sig_type;
f4dc4d17
DE
8223 struct type_unit_group *tu_group;
8224 struct attribute *attr;
8225 struct partial_die_info *first_die;
8226 CORE_ADDR lowpc, highpc;
8227 struct partial_symtab *pst;
8228
0186c6a7
DE
8229 gdb_assert (per_cu->is_debug_types);
8230 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8231
8232 if (! has_children)
8233 return;
8234
8235 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8236 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8237
df07e2c7 8238 if (tu_group->tus == nullptr)
a8b3b8e9 8239 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8240 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8241
8242 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 8243 pst = create_partial_symtab (per_cu, "");
6d94535f 8244 pst->anonymous = true;
f4dc4d17
DE
8245
8246 first_die = load_partial_dies (reader, info_ptr, 1);
8247
8248 lowpc = (CORE_ADDR) -1;
8249 highpc = (CORE_ADDR) 0;
8250 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8251
8763cede 8252 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8253}
8254
73051182
DE
8255/* Struct used to sort TUs by their abbreviation table offset. */
8256
8257struct tu_abbrev_offset
8258{
b2bdb8cf
SM
8259 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8260 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8261 {}
8262
8263 signatured_type *sig_type;
73051182
DE
8264 sect_offset abbrev_offset;
8265};
8266
484cf504 8267/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8268
484cf504
TT
8269static bool
8270sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8271 const struct tu_abbrev_offset &b)
73051182 8272{
484cf504 8273 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8274}
8275
8276/* Efficiently read all the type units.
8277 This does the bulk of the work for build_type_psymtabs.
8278
8279 The efficiency is because we sort TUs by the abbrev table they use and
8280 only read each abbrev table once. In one program there are 200K TUs
8281 sharing 8K abbrev tables.
8282
8283 The main purpose of this function is to support building the
8284 dwarf2_per_objfile->type_unit_groups table.
8285 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8286 can collapse the search space by grouping them by stmt_list.
8287 The savings can be significant, in the same program from above the 200K TUs
8288 share 8K stmt_list tables.
8289
8290 FUNC is expected to call get_type_unit_group, which will create the
8291 struct type_unit_group if necessary and add it to
8292 dwarf2_per_objfile->type_unit_groups. */
8293
8294static void
ed2dc618 8295build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8296{
73051182 8297 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8298 abbrev_table_up abbrev_table;
73051182 8299 sect_offset abbrev_offset;
73051182
DE
8300
8301 /* It's up to the caller to not call us multiple times. */
8302 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8303
b2bdb8cf 8304 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8305 return;
8306
8307 /* TUs typically share abbrev tables, and there can be way more TUs than
8308 abbrev tables. Sort by abbrev table to reduce the number of times we
8309 read each abbrev table in.
8310 Alternatives are to punt or to maintain a cache of abbrev tables.
8311 This is simpler and efficient enough for now.
8312
8313 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8314 symtab to use). Typically TUs with the same abbrev offset have the same
8315 stmt_list value too so in practice this should work well.
8316
8317 The basic algorithm here is:
8318
8319 sort TUs by abbrev table
8320 for each TU with same abbrev table:
8321 read abbrev table if first user
8322 read TU top level DIE
8323 [IWBN if DWO skeletons had DW_AT_stmt_list]
8324 call FUNC */
8325
b4f54984 8326 if (dwarf_read_debug)
73051182
DE
8327 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8328
8329 /* Sort in a separate table to maintain the order of all_type_units
8330 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8331 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8332 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8333
8334 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8335 sorted_by_abbrev.emplace_back
8336 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8337 sig_type->per_cu.section,
8338 sig_type->per_cu.sect_off));
73051182 8339
484cf504
TT
8340 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8341 sort_tu_by_abbrev_offset);
73051182 8342
9c541725 8343 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8344
b2bdb8cf 8345 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8346 {
73051182
DE
8347 /* Switch to the next abbrev table if necessary. */
8348 if (abbrev_table == NULL
b2bdb8cf 8349 || tu.abbrev_offset != abbrev_offset)
73051182 8350 {
b2bdb8cf 8351 abbrev_offset = tu.abbrev_offset;
73051182 8352 abbrev_table =
ed2dc618
SM
8353 abbrev_table_read_table (dwarf2_per_objfile,
8354 &dwarf2_per_objfile->abbrev,
73051182
DE
8355 abbrev_offset);
8356 ++tu_stats->nr_uniq_abbrev_tables;
8357 }
8358
c0ab21c2
TT
8359 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8360 0, 0, false);
8361 if (!reader.dummy_p)
8362 build_type_psymtabs_reader (&reader, reader.info_ptr,
8363 reader.comp_unit_die,
8364 reader.has_children);
73051182 8365 }
6aa5f3a6 8366}
73051182 8367
6aa5f3a6
DE
8368/* Print collected type unit statistics. */
8369
8370static void
ed2dc618 8371print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8372{
8373 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8374
8375 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8376 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8377 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8378 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8379 tu_stats->nr_uniq_abbrev_tables);
8380 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8381 tu_stats->nr_symtabs);
8382 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8383 tu_stats->nr_symtab_sharers);
8384 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8385 tu_stats->nr_stmt_less_type_units);
8386 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8387 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8388}
8389
f4dc4d17
DE
8390/* Traversal function for build_type_psymtabs. */
8391
8392static int
8393build_type_psymtab_dependencies (void **slot, void *info)
8394{
ed2dc618
SM
8395 struct dwarf2_per_objfile *dwarf2_per_objfile
8396 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8397 struct objfile *objfile = dwarf2_per_objfile->objfile;
8398 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8399 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8400 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8401 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8402 int i;
8403
8404 gdb_assert (len > 0);
0186c6a7 8405 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8406
8407 pst->number_of_dependencies = len;
a9342b62 8408 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8409 for (i = 0; i < len; ++i)
f4dc4d17 8410 {
df07e2c7 8411 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8412 gdb_assert (iter->per_cu.is_debug_types);
8413 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8414 iter->type_unit_group = tu_group;
f4dc4d17
DE
8415 }
8416
df07e2c7
AB
8417 delete tu_group->tus;
8418 tu_group->tus = nullptr;
348e048f
DE
8419
8420 return 1;
8421}
8422
8423/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8424 Build partial symbol tables for the .debug_types comp-units. */
8425
8426static void
ed2dc618 8427build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8428{
ed2dc618 8429 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8430 return;
8431
ed2dc618 8432 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8433}
f4dc4d17 8434
6aa5f3a6
DE
8435/* Traversal function for process_skeletonless_type_unit.
8436 Read a TU in a DWO file and build partial symbols for it. */
8437
8438static int
8439process_skeletonless_type_unit (void **slot, void *info)
8440{
8441 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8442 struct dwarf2_per_objfile *dwarf2_per_objfile
8443 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8444 struct signatured_type find_entry, *entry;
8445
8446 /* If this TU doesn't exist in the global table, add it and read it in. */
8447
8448 if (dwarf2_per_objfile->signatured_types == NULL)
8449 {
8450 dwarf2_per_objfile->signatured_types
ed2dc618 8451 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8452 }
8453
8454 find_entry.signature = dwo_unit->signature;
8455 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8456 INSERT);
8457 /* If we've already seen this type there's nothing to do. What's happening
8458 is we're doing our own version of comdat-folding here. */
8459 if (*slot != NULL)
8460 return 1;
8461
8462 /* This does the job that create_all_type_units would have done for
8463 this TU. */
ed2dc618
SM
8464 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8465 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8466 *slot = entry;
8467
8468 /* This does the job that build_type_psymtabs_1 would have done. */
c0ab21c2
TT
8469 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8470 if (!reader.dummy_p)
8471 build_type_psymtabs_reader (&reader, reader.info_ptr,
8472 reader.comp_unit_die, reader.has_children);
6aa5f3a6
DE
8473
8474 return 1;
8475}
8476
8477/* Traversal function for process_skeletonless_type_units. */
8478
8479static int
8480process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8481{
8482 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8483
8484 if (dwo_file->tus != NULL)
8485 {
8486 htab_traverse_noresize (dwo_file->tus,
8487 process_skeletonless_type_unit, info);
8488 }
8489
8490 return 1;
8491}
8492
8493/* Scan all TUs of DWO files, verifying we've processed them.
8494 This is needed in case a TU was emitted without its skeleton.
8495 Note: This can't be done until we know what all the DWO files are. */
8496
8497static void
ed2dc618 8498process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8499{
8500 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8501 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8502 && dwarf2_per_objfile->dwo_files != NULL)
8503 {
51ac9db5 8504 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8505 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8506 dwarf2_per_objfile);
6aa5f3a6 8507 }
348e048f
DE
8508}
8509
ed2dc618 8510/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8511
8512static void
ed2dc618 8513set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8514{
b76e467d 8515 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8516 {
95554aad 8517 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8518
36586728
TT
8519 if (pst == NULL)
8520 continue;
8521
b76e467d 8522 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8523 {
8524 /* Set the 'user' field only if it is not already set. */
8525 if (pst->dependencies[j]->user == NULL)
8526 pst->dependencies[j]->user = pst;
8527 }
8528 }
8529}
8530
93311388
DE
8531/* Build the partial symbol table by doing a quick pass through the
8532 .debug_info and .debug_abbrev sections. */
72bf9492 8533
93311388 8534static void
ed2dc618 8535dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8536{
ed2dc618 8537 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8538
b4f54984 8539 if (dwarf_read_debug)
45cfd468
DE
8540 {
8541 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8542 objfile_name (objfile));
45cfd468
DE
8543 }
8544
98bfdba5
PA
8545 dwarf2_per_objfile->reading_partial_symbols = 1;
8546
be391dca 8547 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8548
93311388
DE
8549 /* Any cached compilation units will be linked by the per-objfile
8550 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8551 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8552
ed2dc618 8553 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8554
ed2dc618 8555 create_all_comp_units (dwarf2_per_objfile);
c906108c 8556
60606b2c
TT
8557 /* Create a temporary address map on a temporary obstack. We later
8558 copy this to the final obstack. */
8268c778 8559 auto_obstack temp_obstack;
791afaa2
TT
8560
8561 scoped_restore save_psymtabs_addrmap
d320c2b5 8562 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8563 addrmap_create_mutable (&temp_obstack));
72bf9492 8564
b76e467d
SM
8565 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8566 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8567
6aa5f3a6 8568 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8569 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8570
8571 /* Now that all TUs have been processed we can fill in the dependencies. */
8572 if (dwarf2_per_objfile->type_unit_groups != NULL)
8573 {
8574 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8575 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8576 }
8577
b4f54984 8578 if (dwarf_read_debug)
ed2dc618 8579 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8580
ed2dc618 8581 set_partial_user (dwarf2_per_objfile);
95554aad 8582
d320c2b5
TT
8583 objfile->partial_symtabs->psymtabs_addrmap
8584 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8585 objfile->partial_symtabs->obstack ());
791afaa2
TT
8586 /* At this point we want to keep the address map. */
8587 save_psymtabs_addrmap.release ();
ff013f42 8588
b4f54984 8589 if (dwarf_read_debug)
45cfd468 8590 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8591 objfile_name (objfile));
ae038cb0
DJ
8592}
8593
dee91e82
DE
8594/* Load the partial DIEs for a secondary CU into memory.
8595 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8596
dee91e82
DE
8597static void
8598load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8599{
c0ab21c2
TT
8600 cutu_reader reader (this_cu, NULL, 1, 1, false);
8601
8602 if (!reader.dummy_p)
8603 {
8604 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8605 language_minimal);
8606
8607 /* Check if comp unit has_children.
8608 If so, read the rest of the partial symbols from this comp unit.
8609 If not, there's no more debug_info for this comp unit. */
8610 if (reader.has_children)
8611 load_partial_dies (&reader, reader.info_ptr, 0);
8612 }
ae038cb0
DJ
8613}
8614
ae038cb0 8615static void
ed2dc618 8616read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8617 struct dwarf2_section_info *section,
f1902523 8618 struct dwarf2_section_info *abbrev_section,
b76e467d 8619 unsigned int is_dwz)
ae038cb0 8620{
d521ce57 8621 const gdb_byte *info_ptr;
ed2dc618 8622 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8623
b4f54984 8624 if (dwarf_read_debug)
bf6af496 8625 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8626 get_section_name (section),
8627 get_section_file_name (section));
bf6af496 8628
36586728 8629 dwarf2_read_section (objfile, section);
ae038cb0 8630
36586728 8631 info_ptr = section->buffer;
6e70227d 8632
36586728 8633 while (info_ptr < section->buffer + section->size)
ae038cb0 8634 {
ae038cb0 8635 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8636
9c541725 8637 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8638
f1902523 8639 comp_unit_head cu_header;
ed2dc618
SM
8640 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8641 abbrev_section, info_ptr,
8642 rcuh_kind::COMPILE);
ae038cb0
DJ
8643
8644 /* Save the compilation unit for later lookup. */
f1902523
JK
8645 if (cu_header.unit_type != DW_UT_type)
8646 {
8647 this_cu = XOBNEW (&objfile->objfile_obstack,
8648 struct dwarf2_per_cu_data);
8649 memset (this_cu, 0, sizeof (*this_cu));
8650 }
8651 else
8652 {
8653 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8654 struct signatured_type);
8655 memset (sig_type, 0, sizeof (*sig_type));
8656 sig_type->signature = cu_header.signature;
8657 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8658 this_cu = &sig_type->per_cu;
8659 }
8660 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8661 this_cu->sect_off = sect_off;
f1902523 8662 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8663 this_cu->is_dwz = is_dwz;
e3b94546 8664 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8665 this_cu->section = section;
ae038cb0 8666
b76e467d 8667 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8668
8669 info_ptr = info_ptr + this_cu->length;
8670 }
36586728
TT
8671}
8672
8673/* Create a list of all compilation units in OBJFILE.
8674 This is only done for -readnow and building partial symtabs. */
8675
8676static void
ed2dc618 8677create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8678{
b76e467d 8679 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8680 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8681 &dwarf2_per_objfile->abbrev, 0);
36586728 8682
b76e467d 8683 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8684 if (dwz != NULL)
ed2dc618 8685 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8686 1);
c906108c
SS
8687}
8688
5734ee8b 8689/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8690 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8691 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8692 DW_AT_ranges). See the comments of add_partial_subprogram on how
8693 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8694
72bf9492
DJ
8695static void
8696scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8697 CORE_ADDR *highpc, int set_addrmap,
8698 struct dwarf2_cu *cu)
c906108c 8699{
72bf9492 8700 struct partial_die_info *pdi;
c906108c 8701
91c24f0a
DC
8702 /* Now, march along the PDI's, descending into ones which have
8703 interesting children but skipping the children of the other ones,
8704 until we reach the end of the compilation unit. */
c906108c 8705
72bf9492 8706 pdi = first_die;
91c24f0a 8707
72bf9492
DJ
8708 while (pdi != NULL)
8709 {
52356b79 8710 pdi->fixup (cu);
c906108c 8711
f55ee35c 8712 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8713 children, so we need to look at them. Ditto for anonymous
8714 enums. */
933c6fe4 8715
72bf9492 8716 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8717 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8718 || pdi->tag == DW_TAG_imported_unit
8719 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8720 {
72bf9492 8721 switch (pdi->tag)
c906108c
SS
8722 {
8723 case DW_TAG_subprogram:
b1dc1806 8724 case DW_TAG_inlined_subroutine:
cdc07690 8725 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8726 break;
72929c62 8727 case DW_TAG_constant:
c906108c
SS
8728 case DW_TAG_variable:
8729 case DW_TAG_typedef:
91c24f0a 8730 case DW_TAG_union_type:
72bf9492 8731 if (!pdi->is_declaration)
63d06c5c 8732 {
72bf9492 8733 add_partial_symbol (pdi, cu);
63d06c5c
DC
8734 }
8735 break;
c906108c 8736 case DW_TAG_class_type:
680b30c7 8737 case DW_TAG_interface_type:
c906108c 8738 case DW_TAG_structure_type:
72bf9492 8739 if (!pdi->is_declaration)
c906108c 8740 {
72bf9492 8741 add_partial_symbol (pdi, cu);
c906108c 8742 }
b7fee5a3
KS
8743 if ((cu->language == language_rust
8744 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8745 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8746 set_addrmap, cu);
c906108c 8747 break;
91c24f0a 8748 case DW_TAG_enumeration_type:
72bf9492
DJ
8749 if (!pdi->is_declaration)
8750 add_partial_enumeration (pdi, cu);
c906108c
SS
8751 break;
8752 case DW_TAG_base_type:
a02abb62 8753 case DW_TAG_subrange_type:
c906108c 8754 /* File scope base type definitions are added to the partial
c5aa993b 8755 symbol table. */
72bf9492 8756 add_partial_symbol (pdi, cu);
c906108c 8757 break;
d9fa45fe 8758 case DW_TAG_namespace:
cdc07690 8759 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8760 break;
5d7cb8df 8761 case DW_TAG_module:
59c35742
AB
8762 if (!pdi->is_declaration)
8763 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8764 break;
95554aad
TT
8765 case DW_TAG_imported_unit:
8766 {
8767 struct dwarf2_per_cu_data *per_cu;
8768
f4dc4d17
DE
8769 /* For now we don't handle imported units in type units. */
8770 if (cu->per_cu->is_debug_types)
8771 {
8772 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8773 " supported in type units [in module %s]"),
518817b3 8774 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8775 }
8776
e3b94546
SM
8777 per_cu = dwarf2_find_containing_comp_unit
8778 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8779 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8780
8781 /* Go read the partial unit, if needed. */
8782 if (per_cu->v.psymtab == NULL)
b93601f3 8783 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8784
ae640021 8785 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8786 }
8787 break;
74921315
KS
8788 case DW_TAG_imported_declaration:
8789 add_partial_symbol (pdi, cu);
8790 break;
c906108c
SS
8791 default:
8792 break;
8793 }
8794 }
8795
72bf9492
DJ
8796 /* If the die has a sibling, skip to the sibling. */
8797
8798 pdi = pdi->die_sibling;
8799 }
8800}
8801
8802/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8803
72bf9492 8804 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8805 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8806 Enumerators are an exception; they use the scope of their parent
8807 enumeration type, i.e. the name of the enumeration type is not
8808 prepended to the enumerator.
91c24f0a 8809
72bf9492
DJ
8810 There are two complexities. One is DW_AT_specification; in this
8811 case "parent" means the parent of the target of the specification,
8812 instead of the direct parent of the DIE. The other is compilers
8813 which do not emit DW_TAG_namespace; in this case we try to guess
8814 the fully qualified name of structure types from their members'
8815 linkage names. This must be done using the DIE's children rather
8816 than the children of any DW_AT_specification target. We only need
8817 to do this for structures at the top level, i.e. if the target of
8818 any DW_AT_specification (if any; otherwise the DIE itself) does not
8819 have a parent. */
8820
8821/* Compute the scope prefix associated with PDI's parent, in
8822 compilation unit CU. The result will be allocated on CU's
8823 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8824 field. NULL is returned if no prefix is necessary. */
15d034d0 8825static const char *
72bf9492
DJ
8826partial_die_parent_scope (struct partial_die_info *pdi,
8827 struct dwarf2_cu *cu)
8828{
15d034d0 8829 const char *grandparent_scope;
72bf9492 8830 struct partial_die_info *parent, *real_pdi;
91c24f0a 8831
72bf9492
DJ
8832 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8833 then this means the parent of the specification DIE. */
8834
8835 real_pdi = pdi;
72bf9492 8836 while (real_pdi->has_specification)
fb816e8b 8837 {
122cf0f2
AB
8838 auto res = find_partial_die (real_pdi->spec_offset,
8839 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8840 real_pdi = res.pdi;
8841 cu = res.cu;
8842 }
72bf9492
DJ
8843
8844 parent = real_pdi->die_parent;
8845 if (parent == NULL)
8846 return NULL;
8847
8848 if (parent->scope_set)
8849 return parent->scope;
8850
52356b79 8851 parent->fixup (cu);
72bf9492 8852
10b3939b 8853 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8854
acebe513
UW
8855 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8856 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8857 Work around this problem here. */
8858 if (cu->language == language_cplus
6e70227d 8859 && parent->tag == DW_TAG_namespace
acebe513
UW
8860 && strcmp (parent->name, "::") == 0
8861 && grandparent_scope == NULL)
8862 {
8863 parent->scope = NULL;
8864 parent->scope_set = 1;
8865 return NULL;
8866 }
8867
0a4b0913 8868 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8869 if (pdi->tag == DW_TAG_enumerator)
8870 /* Enumerators should not get the name of the enumeration as a prefix. */
8871 parent->scope = grandparent_scope;
8872 else if (parent->tag == DW_TAG_namespace
f55ee35c 8873 || parent->tag == DW_TAG_module
72bf9492
DJ
8874 || parent->tag == DW_TAG_structure_type
8875 || parent->tag == DW_TAG_class_type
680b30c7 8876 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8877 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8878 || parent->tag == DW_TAG_enumeration_type
8879 || (cu->language == language_fortran
8880 && parent->tag == DW_TAG_subprogram
8881 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8882 {
8883 if (grandparent_scope == NULL)
8884 parent->scope = parent->name;
8885 else
3e43a32a
MS
8886 parent->scope = typename_concat (&cu->comp_unit_obstack,
8887 grandparent_scope,
f55ee35c 8888 parent->name, 0, cu);
72bf9492 8889 }
72bf9492
DJ
8890 else
8891 {
8892 /* FIXME drow/2004-04-01: What should we be doing with
8893 function-local names? For partial symbols, we should probably be
8894 ignoring them. */
fa9c3fa0
TT
8895 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8896 dwarf_tag_name (parent->tag),
8897 sect_offset_str (pdi->sect_off));
72bf9492 8898 parent->scope = grandparent_scope;
c906108c
SS
8899 }
8900
72bf9492
DJ
8901 parent->scope_set = 1;
8902 return parent->scope;
8903}
8904
8905/* Return the fully scoped name associated with PDI, from compilation unit
8906 CU. The result will be allocated with malloc. */
4568ecf9 8907
43816ebc 8908static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8909partial_die_full_name (struct partial_die_info *pdi,
8910 struct dwarf2_cu *cu)
8911{
15d034d0 8912 const char *parent_scope;
72bf9492 8913
98bfdba5
PA
8914 /* If this is a template instantiation, we can not work out the
8915 template arguments from partial DIEs. So, unfortunately, we have
8916 to go through the full DIEs. At least any work we do building
8917 types here will be reused if full symbols are loaded later. */
8918 if (pdi->has_template_arguments)
8919 {
52356b79 8920 pdi->fixup (cu);
98bfdba5
PA
8921
8922 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8923 {
8924 struct die_info *die;
8925 struct attribute attr;
8926 struct dwarf2_cu *ref_cu = cu;
8927
b64f50a1 8928 /* DW_FORM_ref_addr is using section offset. */
b4069958 8929 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8930 attr.form = DW_FORM_ref_addr;
9c541725 8931 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8932 die = follow_die_ref (NULL, &attr, &ref_cu);
8933
43816ebc 8934 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8935 }
8936 }
8937
72bf9492
DJ
8938 parent_scope = partial_die_parent_scope (pdi, cu);
8939 if (parent_scope == NULL)
8940 return NULL;
8941 else
43816ebc
TT
8942 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8943 pdi->name, 0, cu));
c906108c
SS
8944}
8945
8946static void
72bf9492 8947add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8948{
518817b3
SM
8949 struct dwarf2_per_objfile *dwarf2_per_objfile
8950 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8951 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8953 CORE_ADDR addr = 0;
15d034d0 8954 const char *actual_name = NULL;
e142c38c
DJ
8955 CORE_ADDR baseaddr;
8956
b3b3bada 8957 baseaddr = objfile->text_section_offset ();
c906108c 8958
43816ebc
TT
8959 gdb::unique_xmalloc_ptr<char> built_actual_name
8960 = partial_die_full_name (pdi, cu);
15d034d0 8961 if (built_actual_name != NULL)
43816ebc 8962 actual_name = built_actual_name.get ();
63d06c5c 8963
72bf9492
DJ
8964 if (actual_name == NULL)
8965 actual_name = pdi->name;
8966
c906108c
SS
8967 switch (pdi->tag)
8968 {
b1dc1806 8969 case DW_TAG_inlined_subroutine:
c906108c 8970 case DW_TAG_subprogram:
79748972
TT
8971 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8972 - baseaddr);
0a4b0913
AB
8973 if (pdi->is_external
8974 || cu->language == language_ada
8975 || (cu->language == language_fortran
8976 && pdi->die_parent != NULL
8977 && pdi->die_parent->tag == DW_TAG_subprogram))
8978 {
8979 /* Normally, only "external" DIEs are part of the global scope.
8980 But in Ada and Fortran, we want to be able to access nested
8981 procedures globally. So all Ada and Fortran subprograms are
8982 stored in the global scope. */
31edb802 8983 add_psymbol_to_list (actual_name,
15d034d0 8984 built_actual_name != NULL,
f47fb265 8985 VAR_DOMAIN, LOC_BLOCK,
79748972 8986 SECT_OFF_TEXT (objfile),
75aedd27 8987 psymbol_placement::GLOBAL,
79748972
TT
8988 addr,
8989 cu->language, objfile);
c906108c
SS
8990 }
8991 else
8992 {
31edb802 8993 add_psymbol_to_list (actual_name,
15d034d0 8994 built_actual_name != NULL,
f47fb265 8995 VAR_DOMAIN, LOC_BLOCK,
79748972 8996 SECT_OFF_TEXT (objfile),
75aedd27 8997 psymbol_placement::STATIC,
1762568f 8998 addr, cu->language, objfile);
c906108c 8999 }
0c1b455e
TT
9000
9001 if (pdi->main_subprogram && actual_name != NULL)
9002 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9003 break;
72929c62 9004 case DW_TAG_constant:
31edb802 9005 add_psymbol_to_list (actual_name,
75aedd27
TT
9006 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9007 -1, (pdi->is_external
9008 ? psymbol_placement::GLOBAL
9009 : psymbol_placement::STATIC),
9010 0, cu->language, objfile);
72929c62 9011 break;
c906108c 9012 case DW_TAG_variable:
95554aad
TT
9013 if (pdi->d.locdesc)
9014 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9015
95554aad 9016 if (pdi->d.locdesc
caac4577
JG
9017 && addr == 0
9018 && !dwarf2_per_objfile->has_section_at_zero)
9019 {
9020 /* A global or static variable may also have been stripped
9021 out by the linker if unused, in which case its address
9022 will be nullified; do not add such variables into partial
9023 symbol table then. */
9024 }
9025 else if (pdi->is_external)
c906108c
SS
9026 {
9027 /* Global Variable.
9028 Don't enter into the minimal symbol tables as there is
9029 a minimal symbol table entry from the ELF symbols already.
9030 Enter into partial symbol table if it has a location
9031 descriptor or a type.
9032 If the location descriptor is missing, new_symbol will create
9033 a LOC_UNRESOLVED symbol, the address of the variable will then
9034 be determined from the minimal symbol table whenever the variable
9035 is referenced.
9036 The address for the partial symbol table entry is not
9037 used by GDB, but it comes in handy for debugging partial symbol
9038 table building. */
9039
95554aad 9040 if (pdi->d.locdesc || pdi->has_type)
31edb802 9041 add_psymbol_to_list (actual_name,
15d034d0 9042 built_actual_name != NULL,
f47fb265 9043 VAR_DOMAIN, LOC_STATIC,
79748972 9044 SECT_OFF_TEXT (objfile),
75aedd27 9045 psymbol_placement::GLOBAL,
79748972 9046 addr, cu->language, objfile);
c906108c
SS
9047 }
9048 else
9049 {
ff908ebf
AW
9050 int has_loc = pdi->d.locdesc != NULL;
9051
9052 /* Static Variable. Skip symbols whose value we cannot know (those
9053 without location descriptors or constant values). */
9054 if (!has_loc && !pdi->has_const_value)
43816ebc 9055 return;
ff908ebf 9056
31edb802 9057 add_psymbol_to_list (actual_name,
15d034d0 9058 built_actual_name != NULL,
f47fb265 9059 VAR_DOMAIN, LOC_STATIC,
79748972 9060 SECT_OFF_TEXT (objfile),
75aedd27 9061 psymbol_placement::STATIC,
79748972 9062 has_loc ? addr : 0,
f47fb265 9063 cu->language, objfile);
c906108c
SS
9064 }
9065 break;
9066 case DW_TAG_typedef:
9067 case DW_TAG_base_type:
a02abb62 9068 case DW_TAG_subrange_type:
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::STATIC,
1762568f 9073 0, cu->language, objfile);
c906108c 9074 break;
74921315 9075 case DW_TAG_imported_declaration:
72bf9492 9076 case DW_TAG_namespace:
31edb802 9077 add_psymbol_to_list (actual_name,
15d034d0 9078 built_actual_name != NULL,
79748972 9079 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9080 psymbol_placement::GLOBAL,
1762568f 9081 0, cu->language, objfile);
72bf9492 9082 break;
530e8392 9083 case DW_TAG_module:
a5fd13a9
BH
9084 /* With Fortran 77 there might be a "BLOCK DATA" module
9085 available without any name. If so, we skip the module as it
9086 doesn't bring any value. */
9087 if (actual_name != nullptr)
31edb802 9088 add_psymbol_to_list (actual_name,
a5fd13a9
BH
9089 built_actual_name != NULL,
9090 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9091 psymbol_placement::GLOBAL,
9092 0, cu->language, objfile);
530e8392 9093 break;
c906108c 9094 case DW_TAG_class_type:
680b30c7 9095 case DW_TAG_interface_type:
c906108c
SS
9096 case DW_TAG_structure_type:
9097 case DW_TAG_union_type:
9098 case DW_TAG_enumeration_type:
fa4028e9
JB
9099 /* Skip external references. The DWARF standard says in the section
9100 about "Structure, Union, and Class Type Entries": "An incomplete
9101 structure, union or class type is represented by a structure,
9102 union or class entry that does not have a byte size attribute
9103 and that has a DW_AT_declaration attribute." */
9104 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 9105 return;
fa4028e9 9106
63d06c5c
DC
9107 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9108 static vs. global. */
31edb802 9109 add_psymbol_to_list (actual_name,
15d034d0 9110 built_actual_name != NULL,
79748972 9111 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9112 cu->language == language_cplus
75aedd27
TT
9113 ? psymbol_placement::GLOBAL
9114 : psymbol_placement::STATIC,
1762568f 9115 0, cu->language, objfile);
c906108c 9116
c906108c
SS
9117 break;
9118 case DW_TAG_enumerator:
31edb802 9119 add_psymbol_to_list (actual_name,
15d034d0 9120 built_actual_name != NULL,
79748972 9121 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9122 cu->language == language_cplus
75aedd27
TT
9123 ? psymbol_placement::GLOBAL
9124 : psymbol_placement::STATIC,
1762568f 9125 0, cu->language, objfile);
c906108c
SS
9126 break;
9127 default:
9128 break;
9129 }
9130}
9131
5c4e30ca
DC
9132/* Read a partial die corresponding to a namespace; also, add a symbol
9133 corresponding to that namespace to the symbol table. NAMESPACE is
9134 the name of the enclosing namespace. */
91c24f0a 9135
72bf9492
DJ
9136static void
9137add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9138 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9139 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9140{
72bf9492 9141 /* Add a symbol for the namespace. */
e7c27a73 9142
72bf9492 9143 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9144
9145 /* Now scan partial symbols in that namespace. */
9146
91c24f0a 9147 if (pdi->has_children)
cdc07690 9148 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9149}
9150
5d7cb8df
JK
9151/* Read a partial die corresponding to a Fortran module. */
9152
9153static void
9154add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9155 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9156{
530e8392
KB
9157 /* Add a symbol for the namespace. */
9158
9159 add_partial_symbol (pdi, cu);
9160
f55ee35c 9161 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9162
9163 if (pdi->has_children)
cdc07690 9164 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9165}
9166
b1dc1806
XR
9167/* Read a partial die corresponding to a subprogram or an inlined
9168 subprogram and create a partial symbol for that subprogram.
9169 When the CU language allows it, this routine also defines a partial
9170 symbol for each nested subprogram that this subprogram contains.
9171 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9172 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9173
cdc07690
YQ
9174 PDI may also be a lexical block, in which case we simply search
9175 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9176 Again, this is only performed when the CU language allows this
9177 type of definitions. */
9178
9179static void
9180add_partial_subprogram (struct partial_die_info *pdi,
9181 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9182 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9183{
b1dc1806 9184 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9185 {
9186 if (pdi->has_pc_info)
9187 {
9188 if (pdi->lowpc < *lowpc)
9189 *lowpc = pdi->lowpc;
9190 if (pdi->highpc > *highpc)
9191 *highpc = pdi->highpc;
cdc07690 9192 if (set_addrmap)
5734ee8b 9193 {
518817b3 9194 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9195 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9196 CORE_ADDR baseaddr;
b926417a
TT
9197 CORE_ADDR this_highpc;
9198 CORE_ADDR this_lowpc;
5734ee8b 9199
b3b3bada 9200 baseaddr = objfile->text_section_offset ();
b926417a
TT
9201 this_lowpc
9202 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9203 pdi->lowpc + baseaddr)
9204 - baseaddr);
9205 this_highpc
9206 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9207 pdi->highpc + baseaddr)
9208 - baseaddr);
d320c2b5 9209 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9210 this_lowpc, this_highpc - 1,
9291a0cd 9211 cu->per_cu->v.psymtab);
5734ee8b 9212 }
481860b3
GB
9213 }
9214
9215 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9216 {
bc30ff58 9217 if (!pdi->is_declaration)
e8d05480
JB
9218 /* Ignore subprogram DIEs that do not have a name, they are
9219 illegal. Do not emit a complaint at this point, we will
9220 do so when we convert this psymtab into a symtab. */
9221 if (pdi->name)
9222 add_partial_symbol (pdi, cu);
bc30ff58
JB
9223 }
9224 }
6e70227d 9225
bc30ff58
JB
9226 if (! pdi->has_children)
9227 return;
9228
0a4b0913 9229 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9230 {
9231 pdi = pdi->die_child;
9232 while (pdi != NULL)
9233 {
52356b79 9234 pdi->fixup (cu);
bc30ff58 9235 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9236 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9237 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9238 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9239 pdi = pdi->die_sibling;
9240 }
9241 }
9242}
9243
91c24f0a
DC
9244/* Read a partial die corresponding to an enumeration type. */
9245
72bf9492
DJ
9246static void
9247add_partial_enumeration (struct partial_die_info *enum_pdi,
9248 struct dwarf2_cu *cu)
91c24f0a 9249{
72bf9492 9250 struct partial_die_info *pdi;
91c24f0a
DC
9251
9252 if (enum_pdi->name != NULL)
72bf9492
DJ
9253 add_partial_symbol (enum_pdi, cu);
9254
9255 pdi = enum_pdi->die_child;
9256 while (pdi)
91c24f0a 9257 {
72bf9492 9258 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9259 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9260 else
72bf9492
DJ
9261 add_partial_symbol (pdi, cu);
9262 pdi = pdi->die_sibling;
91c24f0a 9263 }
91c24f0a
DC
9264}
9265
6caca83c
CC
9266/* Return the initial uleb128 in the die at INFO_PTR. */
9267
9268static unsigned int
d521ce57 9269peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9270{
9271 unsigned int bytes_read;
9272
9273 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9274}
9275
685af9cd
TT
9276/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9277 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9278
4bb7a0a7
DJ
9279 Return the corresponding abbrev, or NULL if the number is zero (indicating
9280 an empty DIE). In either case *BYTES_READ will be set to the length of
9281 the initial number. */
9282
9283static struct abbrev_info *
685af9cd
TT
9284peek_die_abbrev (const die_reader_specs &reader,
9285 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9286{
685af9cd 9287 dwarf2_cu *cu = reader.cu;
518817b3 9288 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9289 unsigned int abbrev_number
9290 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9291
9292 if (abbrev_number == 0)
9293 return NULL;
9294
685af9cd 9295 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9296 if (!abbrev)
9297 {
422b9917 9298 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9299 " at offset %s [in module %s]"),
422b9917 9300 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9301 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9302 }
9303
9304 return abbrev;
9305}
9306
93311388
DE
9307/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9308 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9309 DIE. Any children of the skipped DIEs will also be skipped. */
9310
d521ce57
TT
9311static const gdb_byte *
9312skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9313{
4bb7a0a7
DJ
9314 while (1)
9315 {
685af9cd
TT
9316 unsigned int bytes_read;
9317 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9318
4bb7a0a7
DJ
9319 if (abbrev == NULL)
9320 return info_ptr + bytes_read;
9321 else
dee91e82 9322 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9323 }
9324}
9325
93311388
DE
9326/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9327 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9328 abbrev corresponding to that skipped uleb128 should be passed in
9329 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9330 children. */
9331
d521ce57
TT
9332static const gdb_byte *
9333skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9334 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9335{
9336 unsigned int bytes_read;
9337 struct attribute attr;
dee91e82
DE
9338 bfd *abfd = reader->abfd;
9339 struct dwarf2_cu *cu = reader->cu;
d521ce57 9340 const gdb_byte *buffer = reader->buffer;
f664829e 9341 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9342 unsigned int form, i;
9343
9344 for (i = 0; i < abbrev->num_attrs; i++)
9345 {
9346 /* The only abbrev we care about is DW_AT_sibling. */
9347 if (abbrev->attrs[i].name == DW_AT_sibling)
9348 {
18a8505e
AT
9349 bool ignored;
9350 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9351 &ignored);
4bb7a0a7 9352 if (attr.form == DW_FORM_ref_addr)
b98664d3 9353 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9354 else
b9502d3f 9355 {
9c541725
PA
9356 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9357 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9358
9359 if (sibling_ptr < info_ptr)
b98664d3 9360 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9361 else if (sibling_ptr > reader->buffer_end)
9362 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9363 else
9364 return sibling_ptr;
9365 }
4bb7a0a7
DJ
9366 }
9367
9368 /* If it isn't DW_AT_sibling, skip this attribute. */
9369 form = abbrev->attrs[i].form;
9370 skip_attribute:
9371 switch (form)
9372 {
4bb7a0a7 9373 case DW_FORM_ref_addr:
ae411497
TT
9374 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9375 and later it is offset sized. */
9376 if (cu->header.version == 2)
9377 info_ptr += cu->header.addr_size;
9378 else
9379 info_ptr += cu->header.offset_size;
9380 break;
36586728
TT
9381 case DW_FORM_GNU_ref_alt:
9382 info_ptr += cu->header.offset_size;
9383 break;
ae411497 9384 case DW_FORM_addr:
4bb7a0a7
DJ
9385 info_ptr += cu->header.addr_size;
9386 break;
9387 case DW_FORM_data1:
9388 case DW_FORM_ref1:
9389 case DW_FORM_flag:
8fe0f950 9390 case DW_FORM_strx1:
4bb7a0a7
DJ
9391 info_ptr += 1;
9392 break;
2dc7f7b3 9393 case DW_FORM_flag_present:
43988095 9394 case DW_FORM_implicit_const:
2dc7f7b3 9395 break;
4bb7a0a7
DJ
9396 case DW_FORM_data2:
9397 case DW_FORM_ref2:
8fe0f950 9398 case DW_FORM_strx2:
4bb7a0a7
DJ
9399 info_ptr += 2;
9400 break;
8fe0f950
AT
9401 case DW_FORM_strx3:
9402 info_ptr += 3;
9403 break;
4bb7a0a7
DJ
9404 case DW_FORM_data4:
9405 case DW_FORM_ref4:
8fe0f950 9406 case DW_FORM_strx4:
4bb7a0a7
DJ
9407 info_ptr += 4;
9408 break;
9409 case DW_FORM_data8:
9410 case DW_FORM_ref8:
55f1336d 9411 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9412 info_ptr += 8;
9413 break;
0224619f
JK
9414 case DW_FORM_data16:
9415 info_ptr += 16;
9416 break;
4bb7a0a7 9417 case DW_FORM_string:
9b1c24c8 9418 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9419 info_ptr += bytes_read;
9420 break;
2dc7f7b3 9421 case DW_FORM_sec_offset:
4bb7a0a7 9422 case DW_FORM_strp:
36586728 9423 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9424 info_ptr += cu->header.offset_size;
9425 break;
2dc7f7b3 9426 case DW_FORM_exprloc:
4bb7a0a7
DJ
9427 case DW_FORM_block:
9428 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9429 info_ptr += bytes_read;
9430 break;
9431 case DW_FORM_block1:
9432 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9433 break;
9434 case DW_FORM_block2:
9435 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9436 break;
9437 case DW_FORM_block4:
9438 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9439 break;
336d760d 9440 case DW_FORM_addrx:
cf532bd1 9441 case DW_FORM_strx:
4bb7a0a7
DJ
9442 case DW_FORM_sdata:
9443 case DW_FORM_udata:
9444 case DW_FORM_ref_udata:
3019eac3
DE
9445 case DW_FORM_GNU_addr_index:
9446 case DW_FORM_GNU_str_index:
18a8505e 9447 case DW_FORM_rnglistx:
d521ce57 9448 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9449 break;
9450 case DW_FORM_indirect:
9451 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9452 info_ptr += bytes_read;
9453 /* We need to continue parsing from here, so just go back to
9454 the top. */
9455 goto skip_attribute;
9456
9457 default:
3e43a32a
MS
9458 error (_("Dwarf Error: Cannot handle %s "
9459 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9460 dwarf_form_name (form),
9461 bfd_get_filename (abfd));
9462 }
9463 }
9464
9465 if (abbrev->has_children)
dee91e82 9466 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9467 else
9468 return info_ptr;
9469}
9470
93311388 9471/* Locate ORIG_PDI's sibling.
dee91e82 9472 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9473
d521ce57 9474static const gdb_byte *
dee91e82
DE
9475locate_pdi_sibling (const struct die_reader_specs *reader,
9476 struct partial_die_info *orig_pdi,
d521ce57 9477 const gdb_byte *info_ptr)
91c24f0a
DC
9478{
9479 /* Do we know the sibling already? */
72bf9492 9480
91c24f0a
DC
9481 if (orig_pdi->sibling)
9482 return orig_pdi->sibling;
9483
9484 /* Are there any children to deal with? */
9485
9486 if (!orig_pdi->has_children)
9487 return info_ptr;
9488
4bb7a0a7 9489 /* Skip the children the long way. */
91c24f0a 9490
dee91e82 9491 return skip_children (reader, info_ptr);
91c24f0a
DC
9492}
9493
257e7a09 9494/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9495 not NULL. */
c906108c
SS
9496
9497static void
257e7a09
YQ
9498dwarf2_read_symtab (struct partial_symtab *self,
9499 struct objfile *objfile)
c906108c 9500{
ed2dc618
SM
9501 struct dwarf2_per_objfile *dwarf2_per_objfile
9502 = get_dwarf2_per_objfile (objfile);
9503
257e7a09 9504 if (self->readin)
c906108c 9505 {
442e4d9c 9506 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9507 self->filename);
442e4d9c
YQ
9508 }
9509 else
9510 {
9511 if (info_verbose)
c906108c 9512 {
442e4d9c 9513 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9514 self->filename);
442e4d9c 9515 gdb_flush (gdb_stdout);
c906108c 9516 }
c906108c 9517
442e4d9c
YQ
9518 /* If this psymtab is constructed from a debug-only objfile, the
9519 has_section_at_zero flag will not necessarily be correct. We
9520 can get the correct value for this flag by looking at the data
9521 associated with the (presumably stripped) associated objfile. */
9522 if (objfile->separate_debug_objfile_backlink)
9523 {
9524 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9525 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9526
442e4d9c
YQ
9527 dwarf2_per_objfile->has_section_at_zero
9528 = dpo_backlink->has_section_at_zero;
9529 }
b2ab525c 9530
442e4d9c 9531 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9532
257e7a09 9533 psymtab_to_symtab_1 (self);
c906108c 9534
442e4d9c
YQ
9535 /* Finish up the debug error message. */
9536 if (info_verbose)
9537 printf_filtered (_("done.\n"));
c906108c 9538 }
95554aad 9539
ed2dc618 9540 process_cu_includes (dwarf2_per_objfile);
c906108c 9541}
9cdd5dbd
DE
9542\f
9543/* Reading in full CUs. */
c906108c 9544
10b3939b
DJ
9545/* Add PER_CU to the queue. */
9546
9547static void
95554aad
TT
9548queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9549 enum language pretend_language)
10b3939b
DJ
9550{
9551 struct dwarf2_queue_item *item;
9552
9553 per_cu->queued = 1;
8d749320 9554 item = XNEW (struct dwarf2_queue_item);
10b3939b 9555 item->per_cu = per_cu;
95554aad 9556 item->pretend_language = pretend_language;
10b3939b
DJ
9557 item->next = NULL;
9558
9559 if (dwarf2_queue == NULL)
9560 dwarf2_queue = item;
9561 else
9562 dwarf2_queue_tail->next = item;
9563
9564 dwarf2_queue_tail = item;
9565}
9566
89e63ee4
DE
9567/* If PER_CU is not yet queued, add it to the queue.
9568 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9569 dependency.
0907af0c 9570 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9571 meaning either PER_CU is already queued or it is already loaded.
9572
9573 N.B. There is an invariant here that if a CU is queued then it is loaded.
9574 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9575
9576static int
89e63ee4 9577maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9578 struct dwarf2_per_cu_data *per_cu,
9579 enum language pretend_language)
9580{
9581 /* We may arrive here during partial symbol reading, if we need full
9582 DIEs to process an unusual case (e.g. template arguments). Do
9583 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9584 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9585 {
9586 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9587 return 1;
9588 return 0;
9589 }
9590
9591 /* Mark the dependence relation so that we don't flush PER_CU
9592 too early. */
89e63ee4
DE
9593 if (dependent_cu != NULL)
9594 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9595
9596 /* If it's already on the queue, we have nothing to do. */
9597 if (per_cu->queued)
9598 return 0;
9599
9600 /* If the compilation unit is already loaded, just mark it as
9601 used. */
9602 if (per_cu->cu != NULL)
9603 {
9604 per_cu->cu->last_used = 0;
9605 return 0;
9606 }
9607
9608 /* Add it to the queue. */
9609 queue_comp_unit (per_cu, pretend_language);
9610
9611 return 1;
9612}
9613
10b3939b
DJ
9614/* Process the queue. */
9615
9616static void
ed2dc618 9617process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9618{
9619 struct dwarf2_queue_item *item, *next_item;
9620
b4f54984 9621 if (dwarf_read_debug)
45cfd468
DE
9622 {
9623 fprintf_unfiltered (gdb_stdlog,
9624 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9625 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9626 }
9627
03dd20cc
DJ
9628 /* The queue starts out with one item, but following a DIE reference
9629 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9630 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9631 {
cc12ce38
DE
9632 if ((dwarf2_per_objfile->using_index
9633 ? !item->per_cu->v.quick->compunit_symtab
9634 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9635 /* Skip dummy CUs. */
9636 && item->per_cu->cu != NULL)
f4dc4d17
DE
9637 {
9638 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9639 unsigned int debug_print_threshold;
247f5c4f 9640 char buf[100];
f4dc4d17 9641
247f5c4f 9642 if (per_cu->is_debug_types)
f4dc4d17 9643 {
247f5c4f
DE
9644 struct signatured_type *sig_type =
9645 (struct signatured_type *) per_cu;
9646
9d8780f0 9647 sprintf (buf, "TU %s at offset %s",
73be47f5 9648 hex_string (sig_type->signature),
9d8780f0 9649 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9650 /* There can be 100s of TUs.
9651 Only print them in verbose mode. */
9652 debug_print_threshold = 2;
f4dc4d17 9653 }
247f5c4f 9654 else
73be47f5 9655 {
9d8780f0
SM
9656 sprintf (buf, "CU at offset %s",
9657 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9658 debug_print_threshold = 1;
9659 }
247f5c4f 9660
b4f54984 9661 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9662 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9663
9664 if (per_cu->is_debug_types)
9665 process_full_type_unit (per_cu, item->pretend_language);
9666 else
9667 process_full_comp_unit (per_cu, item->pretend_language);
9668
b4f54984 9669 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9670 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9671 }
10b3939b
DJ
9672
9673 item->per_cu->queued = 0;
9674 next_item = item->next;
9675 xfree (item);
9676 }
9677
9678 dwarf2_queue_tail = NULL;
45cfd468 9679
b4f54984 9680 if (dwarf_read_debug)
45cfd468
DE
9681 {
9682 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9683 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9684 }
10b3939b
DJ
9685}
9686
10b3939b
DJ
9687/* Read in full symbols for PST, and anything it depends on. */
9688
c906108c 9689static void
fba45db2 9690psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9691{
10b3939b 9692 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9693 int i;
9694
95554aad
TT
9695 if (pst->readin)
9696 return;
9697
aaa75496 9698 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9699 if (!pst->dependencies[i]->readin
9700 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9701 {
9702 /* Inform about additional files that need to be read in. */
9703 if (info_verbose)
9704 {
a3f17187 9705 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9706 fputs_filtered (" ", gdb_stdout);
9707 wrap_here ("");
9708 fputs_filtered ("and ", gdb_stdout);
9709 wrap_here ("");
9710 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9711 wrap_here (""); /* Flush output. */
aaa75496
JB
9712 gdb_flush (gdb_stdout);
9713 }
9714 psymtab_to_symtab_1 (pst->dependencies[i]);
9715 }
9716
9a3c8263 9717 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9718
9719 if (per_cu == NULL)
aaa75496
JB
9720 {
9721 /* It's an include file, no symbols to read for it.
9722 Everything is in the parent symtab. */
6d94535f 9723 pst->readin = true;
aaa75496
JB
9724 return;
9725 }
c906108c 9726
58f0c718 9727 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9728}
9729
dee91e82
DE
9730/* Trivial hash function for die_info: the hash value of a DIE
9731 is its offset in .debug_info for this objfile. */
10b3939b 9732
dee91e82
DE
9733static hashval_t
9734die_hash (const void *item)
10b3939b 9735{
9a3c8263 9736 const struct die_info *die = (const struct die_info *) item;
6502dd73 9737
9c541725 9738 return to_underlying (die->sect_off);
dee91e82 9739}
63d06c5c 9740
dee91e82
DE
9741/* Trivial comparison function for die_info structures: two DIEs
9742 are equal if they have the same offset. */
98bfdba5 9743
dee91e82
DE
9744static int
9745die_eq (const void *item_lhs, const void *item_rhs)
9746{
9a3c8263
SM
9747 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9748 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9749
9c541725 9750 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9751}
c906108c 9752
c0ab21c2 9753/* Load the DIEs associated with PER_CU into memory. */
c906108c 9754
dee91e82 9755static void
c0ab21c2
TT
9756load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9757 bool skip_partial,
9758 enum language pretend_language)
dee91e82 9759{
c0ab21c2
TT
9760 gdb_assert (! this_cu->is_debug_types);
9761
9762 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9763 if (reader.dummy_p)
9764 return;
9765
9766 struct dwarf2_cu *cu = reader.cu;
9767 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9768
dee91e82
DE
9769 gdb_assert (cu->die_hash == NULL);
9770 cu->die_hash =
9771 htab_create_alloc_ex (cu->header.length / 12,
9772 die_hash,
9773 die_eq,
9774 NULL,
9775 &cu->comp_unit_obstack,
9776 hashtab_obstack_allocate,
9777 dummy_obstack_deallocate);
e142c38c 9778
c0ab21c2
TT
9779 if (reader.has_children)
9780 reader.comp_unit_die->child
9781 = read_die_and_siblings (&reader, reader.info_ptr,
9782 &info_ptr, reader.comp_unit_die);
9783 cu->dies = reader.comp_unit_die;
dee91e82 9784 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9785
9786 /* We try not to read any attributes in this function, because not
9cdd5dbd 9787 all CUs needed for references have been loaded yet, and symbol
10b3939b 9788 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9789 or we won't be able to build types correctly.
9790 Similarly, if we do not read the producer, we can not apply
9791 producer-specific interpretation. */
c0ab21c2 9792 prepare_one_comp_unit (cu, cu->dies, pretend_language);
10b3939b
DJ
9793}
9794
3da10d80
KS
9795/* Add a DIE to the delayed physname list. */
9796
9797static void
9798add_to_method_list (struct type *type, int fnfield_index, int index,
9799 const char *name, struct die_info *die,
9800 struct dwarf2_cu *cu)
9801{
9802 struct delayed_method_info mi;
9803 mi.type = type;
9804 mi.fnfield_index = fnfield_index;
9805 mi.index = index;
9806 mi.name = name;
9807 mi.die = die;
c89b44cd 9808 cu->method_list.push_back (mi);
3da10d80
KS
9809}
9810
3693fdb3
PA
9811/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9812 "const" / "volatile". If so, decrements LEN by the length of the
9813 modifier and return true. Otherwise return false. */
9814
9815template<size_t N>
9816static bool
9817check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9818{
9819 size_t mod_len = sizeof (mod) - 1;
9820 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9821 {
9822 len -= mod_len;
9823 return true;
9824 }
9825 return false;
9826}
9827
3da10d80
KS
9828/* Compute the physnames of any methods on the CU's method list.
9829
9830 The computation of method physnames is delayed in order to avoid the
9831 (bad) condition that one of the method's formal parameters is of an as yet
9832 incomplete type. */
9833
9834static void
9835compute_delayed_physnames (struct dwarf2_cu *cu)
9836{
3693fdb3 9837 /* Only C++ delays computing physnames. */
c89b44cd 9838 if (cu->method_list.empty ())
3693fdb3
PA
9839 return;
9840 gdb_assert (cu->language == language_cplus);
9841
52941706 9842 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9843 {
1d06ead6 9844 const char *physname;
3da10d80 9845 struct fn_fieldlist *fn_flp
c89b44cd
TT
9846 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9847 physname = dwarf2_physname (mi.name, mi.die, cu);
9848 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9849 = physname ? physname : "";
3693fdb3
PA
9850
9851 /* Since there's no tag to indicate whether a method is a
9852 const/volatile overload, extract that information out of the
9853 demangled name. */
9854 if (physname != NULL)
9855 {
9856 size_t len = strlen (physname);
9857
9858 while (1)
9859 {
9860 if (physname[len] == ')') /* shortcut */
9861 break;
9862 else if (check_modifier (physname, len, " const"))
c89b44cd 9863 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9864 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9865 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9866 else
9867 break;
9868 }
9869 }
3da10d80 9870 }
c89b44cd
TT
9871
9872 /* The list is no longer needed. */
9873 cu->method_list.clear ();
3da10d80
KS
9874}
9875
a766d390
DE
9876/* Go objects should be embedded in a DW_TAG_module DIE,
9877 and it's not clear if/how imported objects will appear.
9878 To keep Go support simple until that's worked out,
9879 go back through what we've read and create something usable.
9880 We could do this while processing each DIE, and feels kinda cleaner,
9881 but that way is more invasive.
9882 This is to, for example, allow the user to type "p var" or "b main"
9883 without having to specify the package name, and allow lookups
9884 of module.object to work in contexts that use the expression
9885 parser. */
9886
9887static void
9888fixup_go_packaging (struct dwarf2_cu *cu)
9889{
421d1616 9890 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9891 struct pending *list;
9892 int i;
9893
c24bdb02 9894 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9895 list != NULL;
9896 list = list->next)
a766d390
DE
9897 {
9898 for (i = 0; i < list->nsyms; ++i)
9899 {
9900 struct symbol *sym = list->symbol[i];
9901
c1b5c1eb 9902 if (sym->language () == language_go
a766d390
DE
9903 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9904 {
421d1616
TT
9905 gdb::unique_xmalloc_ptr<char> this_package_name
9906 (go_symbol_package_name (sym));
a766d390
DE
9907
9908 if (this_package_name == NULL)
9909 continue;
9910 if (package_name == NULL)
421d1616 9911 package_name = std::move (this_package_name);
a766d390
DE
9912 else
9913 {
518817b3
SM
9914 struct objfile *objfile
9915 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9916 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9917 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9918 (symbol_symtab (sym) != NULL
9919 ? symtab_to_filename_for_display
9920 (symbol_symtab (sym))
e3b94546 9921 : objfile_name (objfile)),
421d1616 9922 this_package_name.get (), package_name.get ());
a766d390
DE
9923 }
9924 }
9925 }
9926 }
9927
9928 if (package_name != NULL)
9929 {
518817b3 9930 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9931 const char *saved_package_name
421d1616 9932 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9933 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9934 saved_package_name);
a766d390
DE
9935 struct symbol *sym;
9936
e623cf5d 9937 sym = allocate_symbol (objfile);
d3ecddab 9938 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9939 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9940 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9941 e.g., "main" finds the "main" module and not C's main(). */
9942 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9943 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9944 SYMBOL_TYPE (sym) = type;
9945
c24bdb02 9946 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9947 }
9948}
9949
c9317f21
TT
9950/* Allocate a fully-qualified name consisting of the two parts on the
9951 obstack. */
9952
9953static const char *
9954rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9955{
9956 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9957}
9958
9959/* A helper that allocates a struct discriminant_info to attach to a
9960 union type. */
9961
9962static struct discriminant_info *
9963alloc_discriminant_info (struct type *type, int discriminant_index,
9964 int default_index)
9965{
9966 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9967 gdb_assert (discriminant_index == -1
9968 || (discriminant_index >= 0
9969 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9970 gdb_assert (default_index == -1
c7b15a66 9971 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9972
9973 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9974
9975 struct discriminant_info *disc
9976 = ((struct discriminant_info *)
9977 TYPE_ZALLOC (type,
9978 offsetof (struct discriminant_info, discriminants)
9979 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9980 disc->default_index = default_index;
9981 disc->discriminant_index = discriminant_index;
9982
9983 struct dynamic_prop prop;
9984 prop.kind = PROP_UNDEFINED;
9985 prop.data.baton = disc;
9986
9987 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9988
9989 return disc;
9990}
9991
9992/* Some versions of rustc emitted enums in an unusual way.
9993
9994 Ordinary enums were emitted as unions. The first element of each
9995 structure in the union was named "RUST$ENUM$DISR". This element
9996 held the discriminant.
9997
9998 These versions of Rust also implemented the "non-zero"
9999 optimization. When the enum had two values, and one is empty and
10000 the other holds a pointer that cannot be zero, the pointer is used
10001 as the discriminant, with a zero value meaning the empty variant.
10002 Here, the union's first member is of the form
10003 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10004 where the fieldnos are the indices of the fields that should be
10005 traversed in order to find the field (which may be several fields deep)
10006 and the variantname is the name of the variant of the case when the
10007 field is zero.
10008
10009 This function recognizes whether TYPE is of one of these forms,
10010 and, if so, smashes it to be a variant type. */
10011
10012static void
10013quirk_rust_enum (struct type *type, struct objfile *objfile)
10014{
10015 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10016
10017 /* We don't need to deal with empty enums. */
10018 if (TYPE_NFIELDS (type) == 0)
10019 return;
10020
10021#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10022 if (TYPE_NFIELDS (type) == 1
10023 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10024 {
10025 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10026
10027 /* Decode the field name to find the offset of the
10028 discriminant. */
10029 ULONGEST bit_offset = 0;
10030 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10031 while (name[0] >= '0' && name[0] <= '9')
10032 {
10033 char *tail;
10034 unsigned long index = strtoul (name, &tail, 10);
10035 name = tail;
10036 if (*name != '$'
10037 || index >= TYPE_NFIELDS (field_type)
10038 || (TYPE_FIELD_LOC_KIND (field_type, index)
10039 != FIELD_LOC_KIND_BITPOS))
10040 {
b98664d3 10041 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10042 "[in module %s]"),
10043 TYPE_FIELD_NAME (type, 0),
10044 objfile_name (objfile));
10045 return;
10046 }
10047 ++name;
10048
10049 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10050 field_type = TYPE_FIELD_TYPE (field_type, index);
10051 }
10052
10053 /* Make a union to hold the variants. */
10054 struct type *union_type = alloc_type (objfile);
10055 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10056 TYPE_NFIELDS (union_type) = 3;
10057 TYPE_FIELDS (union_type)
10058 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10059 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10060 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10061
10062 /* Put the discriminant must at index 0. */
10063 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10064 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10065 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10066 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10067
10068 /* The order of fields doesn't really matter, so put the real
10069 field at index 1 and the data-less field at index 2. */
10070 struct discriminant_info *disc
10071 = alloc_discriminant_info (union_type, 0, 1);
10072 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10073 TYPE_FIELD_NAME (union_type, 1)
10074 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10075 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10076 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10077 TYPE_FIELD_NAME (union_type, 1));
10078
10079 const char *dataless_name
10080 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10081 name);
10082 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10083 dataless_name);
10084 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10085 /* NAME points into the original discriminant name, which
10086 already has the correct lifetime. */
10087 TYPE_FIELD_NAME (union_type, 2) = name;
10088 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10089 disc->discriminants[2] = 0;
10090
10091 /* Smash this type to be a structure type. We have to do this
10092 because the type has already been recorded. */
10093 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10094 TYPE_NFIELDS (type) = 1;
10095 TYPE_FIELDS (type)
10096 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10097
10098 /* Install the variant part. */
10099 TYPE_FIELD_TYPE (type, 0) = union_type;
10100 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10101 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10102 }
77c2dba3
TT
10103 /* A union with a single anonymous field is probably an old-style
10104 univariant enum. */
10105 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10106 {
c9317f21
TT
10107 /* Smash this type to be a structure type. We have to do this
10108 because the type has already been recorded. */
10109 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10110
10111 /* Make a union to hold the variants. */
10112 struct type *union_type = alloc_type (objfile);
10113 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10114 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10115 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10116 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10117 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10118
10119 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10120 const char *variant_name
10121 = rust_last_path_segment (TYPE_NAME (field_type));
10122 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10123 TYPE_NAME (field_type)
10124 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10125 TYPE_NAME (type), variant_name);
c9317f21
TT
10126
10127 /* Install the union in the outer struct type. */
10128 TYPE_NFIELDS (type) = 1;
10129 TYPE_FIELDS (type)
10130 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10131 TYPE_FIELD_TYPE (type, 0) = union_type;
10132 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10133 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10134
10135 alloc_discriminant_info (union_type, -1, 0);
10136 }
10137 else
10138 {
10139 struct type *disr_type = nullptr;
10140 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10141 {
10142 disr_type = TYPE_FIELD_TYPE (type, i);
10143
a037790e
TT
10144 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10145 {
10146 /* All fields of a true enum will be structs. */
10147 return;
10148 }
10149 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10150 {
10151 /* Could be data-less variant, so keep going. */
a037790e 10152 disr_type = nullptr;
c9317f21
TT
10153 }
10154 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10155 "RUST$ENUM$DISR") != 0)
10156 {
10157 /* Not a Rust enum. */
10158 return;
10159 }
10160 else
10161 {
10162 /* Found one. */
10163 break;
10164 }
10165 }
10166
10167 /* If we got here without a discriminant, then it's probably
10168 just a union. */
10169 if (disr_type == nullptr)
10170 return;
10171
10172 /* Smash this type to be a structure type. We have to do this
10173 because the type has already been recorded. */
10174 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10175
10176 /* Make a union to hold the variants. */
10177 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10178 struct type *union_type = alloc_type (objfile);
10179 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10180 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10181 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10182 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10183 TYPE_FIELDS (union_type)
10184 = (struct field *) TYPE_ZALLOC (union_type,
10185 (TYPE_NFIELDS (union_type)
10186 * sizeof (struct field)));
10187
10188 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10189 TYPE_NFIELDS (type) * sizeof (struct field));
10190
10191 /* Install the discriminant at index 0 in the union. */
10192 TYPE_FIELD (union_type, 0) = *disr_field;
10193 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10194 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10195
10196 /* Install the union in the outer struct type. */
10197 TYPE_FIELD_TYPE (type, 0) = union_type;
10198 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10199 TYPE_NFIELDS (type) = 1;
10200
10201 /* Set the size and offset of the union type. */
10202 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10203
10204 /* We need a way to find the correct discriminant given a
10205 variant name. For convenience we build a map here. */
10206 struct type *enum_type = FIELD_TYPE (*disr_field);
10207 std::unordered_map<std::string, ULONGEST> discriminant_map;
10208 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10209 {
10210 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10211 {
10212 const char *name
10213 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10214 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10215 }
10216 }
10217
10218 int n_fields = TYPE_NFIELDS (union_type);
10219 struct discriminant_info *disc
10220 = alloc_discriminant_info (union_type, 0, -1);
10221 /* Skip the discriminant here. */
10222 for (int i = 1; i < n_fields; ++i)
10223 {
10224 /* Find the final word in the name of this variant's type.
10225 That name can be used to look up the correct
10226 discriminant. */
10227 const char *variant_name
10228 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10229 i)));
10230
10231 auto iter = discriminant_map.find (variant_name);
10232 if (iter != discriminant_map.end ())
10233 disc->discriminants[i] = iter->second;
10234
bedda9ac 10235 /* Remove the discriminant field, if it exists. */
c9317f21 10236 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10237 if (TYPE_NFIELDS (sub_type) > 0)
10238 {
10239 --TYPE_NFIELDS (sub_type);
10240 ++TYPE_FIELDS (sub_type);
10241 }
c9317f21
TT
10242 TYPE_FIELD_NAME (union_type, i) = variant_name;
10243 TYPE_NAME (sub_type)
10244 = rust_fully_qualify (&objfile->objfile_obstack,
10245 TYPE_NAME (type), variant_name);
10246 }
10247 }
10248}
10249
10250/* Rewrite some Rust unions to be structures with variants parts. */
10251
10252static void
10253rust_union_quirks (struct dwarf2_cu *cu)
10254{
10255 gdb_assert (cu->language == language_rust);
52941706
SM
10256 for (type *type_ : cu->rust_unions)
10257 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10258 /* We don't need this any more. */
10259 cu->rust_unions.clear ();
c9317f21
TT
10260}
10261
95554aad
TT
10262/* Return the symtab for PER_CU. This works properly regardless of
10263 whether we're using the index or psymtabs. */
10264
43f3e411
DE
10265static struct compunit_symtab *
10266get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10267{
ed2dc618 10268 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10269 ? per_cu->v.quick->compunit_symtab
10270 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10271}
10272
10273/* A helper function for computing the list of all symbol tables
10274 included by PER_CU. */
10275
10276static void
4c39bc03 10277recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10278 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10279 struct dwarf2_per_cu_data *per_cu,
43f3e411 10280 struct compunit_symtab *immediate_parent)
95554aad
TT
10281{
10282 void **slot;
43f3e411 10283 struct compunit_symtab *cust;
95554aad
TT
10284
10285 slot = htab_find_slot (all_children, per_cu, INSERT);
10286 if (*slot != NULL)
10287 {
10288 /* This inclusion and its children have been processed. */
10289 return;
10290 }
10291
10292 *slot = per_cu;
10293 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10294 cust = get_compunit_symtab (per_cu);
10295 if (cust != NULL)
ec94af83
DE
10296 {
10297 /* If this is a type unit only add its symbol table if we haven't
10298 seen it yet (type unit per_cu's can share symtabs). */
10299 if (per_cu->is_debug_types)
10300 {
43f3e411 10301 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10302 if (*slot == NULL)
10303 {
43f3e411 10304 *slot = cust;
4c39bc03 10305 result->push_back (cust);
43f3e411
DE
10306 if (cust->user == NULL)
10307 cust->user = immediate_parent;
ec94af83
DE
10308 }
10309 }
10310 else
f9125b6c 10311 {
4c39bc03 10312 result->push_back (cust);
43f3e411
DE
10313 if (cust->user == NULL)
10314 cust->user = immediate_parent;
f9125b6c 10315 }
ec94af83 10316 }
95554aad 10317
ae640021
AB
10318 if (!per_cu->imported_symtabs_empty ())
10319 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10320 {
10321 recursively_compute_inclusions (result, all_children,
10322 all_type_symtabs, ptr, cust);
10323 }
95554aad
TT
10324}
10325
43f3e411 10326/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10327 PER_CU. */
10328
10329static void
43f3e411 10330compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10331{
f4dc4d17
DE
10332 gdb_assert (! per_cu->is_debug_types);
10333
ae640021 10334 if (!per_cu->imported_symtabs_empty ())
95554aad 10335 {
ae640021 10336 int len;
4c39bc03 10337 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10338 htab_t all_children, all_type_symtabs;
43f3e411 10339 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10340
10341 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10342 if (cust == NULL)
95554aad
TT
10343 return;
10344
10345 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10346 NULL, xcalloc, xfree);
ec94af83
DE
10347 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10348 NULL, xcalloc, xfree);
95554aad 10349
ae640021 10350 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10351 {
10352 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10353 all_type_symtabs, ptr, cust);
ec94af83 10354 }
95554aad 10355
ec94af83 10356 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10357 len = result_symtabs.size ();
43f3e411 10358 cust->includes
ed2dc618 10359 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10360 struct compunit_symtab *, len + 1);
4c39bc03
TT
10361 memcpy (cust->includes, result_symtabs.data (),
10362 len * sizeof (compunit_symtab *));
43f3e411 10363 cust->includes[len] = NULL;
95554aad 10364
95554aad 10365 htab_delete (all_children);
ec94af83 10366 htab_delete (all_type_symtabs);
95554aad
TT
10367 }
10368}
10369
10370/* Compute the 'includes' field for the symtabs of all the CUs we just
10371 read. */
10372
10373static void
ed2dc618 10374process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10375{
71b73764 10376 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10377 {
10378 if (! iter->is_debug_types)
43f3e411 10379 compute_compunit_symtab_includes (iter);
f4dc4d17 10380 }
95554aad 10381
c5d0225d 10382 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10383}
10384
9cdd5dbd 10385/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10386 already been loaded into memory. */
10387
10388static void
95554aad
TT
10389process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10390 enum language pretend_language)
10b3939b 10391{
10b3939b 10392 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10393 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10394 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10395 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10396 CORE_ADDR lowpc, highpc;
43f3e411 10397 struct compunit_symtab *cust;
10b3939b 10398 CORE_ADDR baseaddr;
4359dff1 10399 struct block *static_block;
3e29f34a 10400 CORE_ADDR addr;
10b3939b 10401
b3b3bada 10402 baseaddr = objfile->text_section_offset ();
10b3939b 10403
c89b44cd
TT
10404 /* Clear the list here in case something was left over. */
10405 cu->method_list.clear ();
10b3939b 10406
95554aad
TT
10407 cu->language = pretend_language;
10408 cu->language_defn = language_def (cu->language);
10409
c906108c 10410 /* Do line number decoding in read_file_scope () */
10b3939b 10411 process_die (cu->dies, cu);
c906108c 10412
a766d390
DE
10413 /* For now fudge the Go package. */
10414 if (cu->language == language_go)
10415 fixup_go_packaging (cu);
10416
5f48f8f3 10417 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10418 should be complete, and it should now be safe to compute all of the
10419 physnames. */
10420 compute_delayed_physnames (cu);
3da10d80 10421
c9317f21
TT
10422 if (cu->language == language_rust)
10423 rust_union_quirks (cu);
10424
fae299cd
DC
10425 /* Some compilers don't define a DW_AT_high_pc attribute for the
10426 compilation unit. If the DW_AT_high_pc is missing, synthesize
10427 it, by scanning the DIE's below the compilation unit. */
10b3939b 10428 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10429
3e29f34a 10430 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10431 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10432
10433 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10434 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10435 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10436 addrmap to help ensure it has an accurate map of pc values belonging to
10437 this comp unit. */
10438 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10439
c24bdb02 10440 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10441 SECT_OFF_TEXT (objfile),
10442 0);
c906108c 10443
43f3e411 10444 if (cust != NULL)
c906108c 10445 {
df15bd07 10446 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10447
8be455d7
JK
10448 /* Set symtab language to language from DW_AT_language. If the
10449 compilation is from a C file generated by language preprocessors, do
10450 not set the language if it was already deduced by start_subfile. */
43f3e411 10451 if (!(cu->language == language_c
40e3ad0e 10452 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10453 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10454
10455 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10456 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10457 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10458 there were bugs in prologue debug info, fixed later in GCC-4.5
10459 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10460
10461 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10462 needed, it would be wrong due to missing DW_AT_producer there.
10463
10464 Still one can confuse GDB by using non-standard GCC compilation
10465 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10466 */
ab260dad 10467 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10468 cust->locations_valid = 1;
e0d00bc7
JK
10469
10470 if (gcc_4_minor >= 5)
43f3e411 10471 cust->epilogue_unwind_valid = 1;
96408a79 10472
43f3e411 10473 cust->call_site_htab = cu->call_site_htab;
c906108c 10474 }
9291a0cd
TT
10475
10476 if (dwarf2_per_objfile->using_index)
43f3e411 10477 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10478 else
10479 {
10480 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10481 pst->compunit_symtab = cust;
6d94535f 10482 pst->readin = true;
9291a0cd 10483 }
c906108c 10484
95554aad 10485 /* Push it for inclusion processing later. */
c5d0225d 10486 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10487
10488 /* Not needed any more. */
c24bdb02 10489 cu->reset_builder ();
f4dc4d17 10490}
45cfd468 10491
f4dc4d17
DE
10492/* Generate full symbol information for type unit PER_CU, whose DIEs have
10493 already been loaded into memory. */
10494
10495static void
10496process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10497 enum language pretend_language)
10498{
10499 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10500 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10501 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10502 struct compunit_symtab *cust;
0186c6a7
DE
10503 struct signatured_type *sig_type;
10504
10505 gdb_assert (per_cu->is_debug_types);
10506 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10507
c89b44cd
TT
10508 /* Clear the list here in case something was left over. */
10509 cu->method_list.clear ();
f4dc4d17 10510
f4dc4d17
DE
10511 cu->language = pretend_language;
10512 cu->language_defn = language_def (cu->language);
10513
10514 /* The symbol tables are set up in read_type_unit_scope. */
10515 process_die (cu->dies, cu);
10516
10517 /* For now fudge the Go package. */
10518 if (cu->language == language_go)
10519 fixup_go_packaging (cu);
10520
5f48f8f3 10521 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10522 should be complete, and it should now be safe to compute all of the
10523 physnames. */
10524 compute_delayed_physnames (cu);
f4dc4d17 10525
c9317f21
TT
10526 if (cu->language == language_rust)
10527 rust_union_quirks (cu);
10528
f4dc4d17
DE
10529 /* TUs share symbol tables.
10530 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10531 of it with end_expandable_symtab. Otherwise, complete the addition of
10532 this TU's symbols to the existing symtab. */
43f3e411 10533 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10534 {
c24bdb02
KS
10535 buildsym_compunit *builder = cu->get_builder ();
10536 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10537 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10538
43f3e411 10539 if (cust != NULL)
f4dc4d17
DE
10540 {
10541 /* Set symtab language to language from DW_AT_language. If the
10542 compilation is from a C file generated by language preprocessors,
10543 do not set the language if it was already deduced by
10544 start_subfile. */
43f3e411
DE
10545 if (!(cu->language == language_c
10546 && COMPUNIT_FILETABS (cust)->language != language_c))
10547 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10548 }
10549 }
10550 else
10551 {
c24bdb02 10552 cu->get_builder ()->augment_type_symtab ();
43f3e411 10553 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10554 }
10555
10556 if (dwarf2_per_objfile->using_index)
43f3e411 10557 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10558 else
10559 {
10560 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10561 pst->compunit_symtab = cust;
6d94535f 10562 pst->readin = true;
45cfd468 10563 }
804d2729
TT
10564
10565 /* Not needed any more. */
c24bdb02 10566 cu->reset_builder ();
c906108c
SS
10567}
10568
95554aad
TT
10569/* Process an imported unit DIE. */
10570
10571static void
10572process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10573{
10574 struct attribute *attr;
10575
f4dc4d17
DE
10576 /* For now we don't handle imported units in type units. */
10577 if (cu->per_cu->is_debug_types)
10578 {
10579 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10580 " supported in type units [in module %s]"),
518817b3 10581 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10582 }
10583
95554aad
TT
10584 attr = dwarf2_attr (die, DW_AT_import, cu);
10585 if (attr != NULL)
10586 {
9c541725
PA
10587 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10588 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10589 dwarf2_per_cu_data *per_cu
e3b94546 10590 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10591 cu->per_cu->dwarf2_per_objfile);
95554aad 10592
69d751e3 10593 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10594 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10595 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10596
ae640021 10597 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10598 }
10599}
10600
4c8aa72d
PA
10601/* RAII object that represents a process_die scope: i.e.,
10602 starts/finishes processing a DIE. */
10603class process_die_scope
adde2bff 10604{
4c8aa72d
PA
10605public:
10606 process_die_scope (die_info *die, dwarf2_cu *cu)
10607 : m_die (die), m_cu (cu)
10608 {
10609 /* We should only be processing DIEs not already in process. */
10610 gdb_assert (!m_die->in_process);
10611 m_die->in_process = true;
10612 }
8c3cb9fa 10613
4c8aa72d
PA
10614 ~process_die_scope ()
10615 {
10616 m_die->in_process = false;
10617
10618 /* If we're done processing the DIE for the CU that owns the line
10619 header, we don't need the line header anymore. */
10620 if (m_cu->line_header_die_owner == m_die)
10621 {
10622 delete m_cu->line_header;
10623 m_cu->line_header = NULL;
10624 m_cu->line_header_die_owner = NULL;
10625 }
10626 }
10627
10628private:
10629 die_info *m_die;
10630 dwarf2_cu *m_cu;
10631};
adde2bff 10632
c906108c
SS
10633/* Process a die and its children. */
10634
10635static void
e7c27a73 10636process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10637{
4c8aa72d 10638 process_die_scope scope (die, cu);
adde2bff 10639
c906108c
SS
10640 switch (die->tag)
10641 {
10642 case DW_TAG_padding:
10643 break;
10644 case DW_TAG_compile_unit:
95554aad 10645 case DW_TAG_partial_unit:
e7c27a73 10646 read_file_scope (die, cu);
c906108c 10647 break;
348e048f
DE
10648 case DW_TAG_type_unit:
10649 read_type_unit_scope (die, cu);
10650 break;
c906108c 10651 case DW_TAG_subprogram:
0a4b0913
AB
10652 /* Nested subprograms in Fortran get a prefix. */
10653 if (cu->language == language_fortran
10654 && die->parent != NULL
10655 && die->parent->tag == DW_TAG_subprogram)
10656 cu->processing_has_namespace_info = true;
10657 /* Fall through. */
c906108c 10658 case DW_TAG_inlined_subroutine:
edb3359d 10659 read_func_scope (die, cu);
c906108c
SS
10660 break;
10661 case DW_TAG_lexical_block:
14898363
L
10662 case DW_TAG_try_block:
10663 case DW_TAG_catch_block:
e7c27a73 10664 read_lexical_block_scope (die, cu);
c906108c 10665 break;
216f72a1 10666 case DW_TAG_call_site:
96408a79
SA
10667 case DW_TAG_GNU_call_site:
10668 read_call_site_scope (die, cu);
10669 break;
c906108c 10670 case DW_TAG_class_type:
680b30c7 10671 case DW_TAG_interface_type:
c906108c
SS
10672 case DW_TAG_structure_type:
10673 case DW_TAG_union_type:
134d01f1 10674 process_structure_scope (die, cu);
c906108c
SS
10675 break;
10676 case DW_TAG_enumeration_type:
134d01f1 10677 process_enumeration_scope (die, cu);
c906108c 10678 break;
134d01f1 10679
f792889a
DJ
10680 /* These dies have a type, but processing them does not create
10681 a symbol or recurse to process the children. Therefore we can
10682 read them on-demand through read_type_die. */
c906108c 10683 case DW_TAG_subroutine_type:
72019c9c 10684 case DW_TAG_set_type:
c906108c 10685 case DW_TAG_array_type:
c906108c 10686 case DW_TAG_pointer_type:
c906108c 10687 case DW_TAG_ptr_to_member_type:
c906108c 10688 case DW_TAG_reference_type:
4297a3f0 10689 case DW_TAG_rvalue_reference_type:
c906108c 10690 case DW_TAG_string_type:
c906108c 10691 break;
134d01f1 10692
c906108c 10693 case DW_TAG_base_type:
a02abb62 10694 case DW_TAG_subrange_type:
cb249c71 10695 case DW_TAG_typedef:
134d01f1
DJ
10696 /* Add a typedef symbol for the type definition, if it has a
10697 DW_AT_name. */
f792889a 10698 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10699 break;
c906108c 10700 case DW_TAG_common_block:
e7c27a73 10701 read_common_block (die, cu);
c906108c
SS
10702 break;
10703 case DW_TAG_common_inclusion:
10704 break;
d9fa45fe 10705 case DW_TAG_namespace:
9068261f 10706 cu->processing_has_namespace_info = true;
e7c27a73 10707 read_namespace (die, cu);
d9fa45fe 10708 break;
5d7cb8df 10709 case DW_TAG_module:
9068261f 10710 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10711 read_module (die, cu);
10712 break;
d9fa45fe 10713 case DW_TAG_imported_declaration:
9068261f 10714 cu->processing_has_namespace_info = true;
74921315
KS
10715 if (read_namespace_alias (die, cu))
10716 break;
86a73007
TT
10717 /* The declaration is not a global namespace alias. */
10718 /* Fall through. */
d9fa45fe 10719 case DW_TAG_imported_module:
9068261f 10720 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10721 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10722 || cu->language != language_fortran))
b98664d3 10723 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10724 dwarf_tag_name (die->tag));
10725 read_import_statement (die, cu);
d9fa45fe 10726 break;
95554aad
TT
10727
10728 case DW_TAG_imported_unit:
10729 process_imported_unit_die (die, cu);
10730 break;
10731
71a3c369
TT
10732 case DW_TAG_variable:
10733 read_variable (die, cu);
10734 break;
10735
c906108c 10736 default:
e7c27a73 10737 new_symbol (die, NULL, cu);
c906108c
SS
10738 break;
10739 }
10740}
ca69b9e6
DE
10741\f
10742/* DWARF name computation. */
c906108c 10743
94af9270
KS
10744/* A helper function for dwarf2_compute_name which determines whether DIE
10745 needs to have the name of the scope prepended to the name listed in the
10746 die. */
10747
10748static int
10749die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10750{
1c809c68
TT
10751 struct attribute *attr;
10752
94af9270
KS
10753 switch (die->tag)
10754 {
10755 case DW_TAG_namespace:
10756 case DW_TAG_typedef:
10757 case DW_TAG_class_type:
10758 case DW_TAG_interface_type:
10759 case DW_TAG_structure_type:
10760 case DW_TAG_union_type:
10761 case DW_TAG_enumeration_type:
10762 case DW_TAG_enumerator:
10763 case DW_TAG_subprogram:
08a76f8a 10764 case DW_TAG_inlined_subroutine:
94af9270 10765 case DW_TAG_member:
74921315 10766 case DW_TAG_imported_declaration:
94af9270
KS
10767 return 1;
10768
10769 case DW_TAG_variable:
c2b0a229 10770 case DW_TAG_constant:
94af9270
KS
10771 /* We only need to prefix "globally" visible variables. These include
10772 any variable marked with DW_AT_external or any variable that
10773 lives in a namespace. [Variables in anonymous namespaces
10774 require prefixing, but they are not DW_AT_external.] */
10775
10776 if (dwarf2_attr (die, DW_AT_specification, cu))
10777 {
10778 struct dwarf2_cu *spec_cu = cu;
9a619af0 10779
94af9270
KS
10780 return die_needs_namespace (die_specification (die, &spec_cu),
10781 spec_cu);
10782 }
10783
1c809c68 10784 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10785 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10786 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10787 return 0;
10788 /* A variable in a lexical block of some kind does not need a
10789 namespace, even though in C++ such variables may be external
10790 and have a mangled name. */
10791 if (die->parent->tag == DW_TAG_lexical_block
10792 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10793 || die->parent->tag == DW_TAG_catch_block
10794 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10795 return 0;
10796 return 1;
94af9270
KS
10797
10798 default:
10799 return 0;
10800 }
10801}
10802
73b9be8b
KS
10803/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10804 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10805 defined for the given DIE. */
10806
10807static struct attribute *
10808dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10809{
10810 struct attribute *attr;
10811
10812 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10813 if (attr == NULL)
10814 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10815
10816 return attr;
10817}
10818
10819/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10820 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10821 defined for the given DIE. */
10822
10823static const char *
10824dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10825{
10826 const char *linkage_name;
10827
10828 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10829 if (linkage_name == NULL)
10830 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10831
10832 return linkage_name;
10833}
10834
94af9270 10835/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10836 compute the physname for the object, which include a method's:
9c37b5ae 10837 - formal parameters (C++),
a766d390 10838 - receiver type (Go),
a766d390
DE
10839
10840 The term "physname" is a bit confusing.
10841 For C++, for example, it is the demangled name.
10842 For Go, for example, it's the mangled name.
94af9270 10843
af6b7be1
JB
10844 For Ada, return the DIE's linkage name rather than the fully qualified
10845 name. PHYSNAME is ignored..
10846
94af9270
KS
10847 The result is allocated on the objfile_obstack and canonicalized. */
10848
10849static const char *
15d034d0
TT
10850dwarf2_compute_name (const char *name,
10851 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10852 int physname)
10853{
518817b3 10854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10855
94af9270
KS
10856 if (name == NULL)
10857 name = dwarf2_name (die, cu);
10858
2ee7123e
DE
10859 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10860 but otherwise compute it by typename_concat inside GDB.
10861 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10862 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10863 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10864 will set the demangled name to the result of dwarf2_full_name, and it is
10865 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10866 if (cu->language == language_ada
10867 || (cu->language == language_fortran && physname))
10868 {
10869 /* For Ada unit, we prefer the linkage name over the name, as
10870 the former contains the exported name, which the user expects
10871 to be able to reference. Ideally, we want the user to be able
10872 to reference this entity using either natural or linkage name,
10873 but we haven't started looking at this enhancement yet. */
73b9be8b 10874 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10875
2ee7123e
DE
10876 if (linkage_name != NULL)
10877 return linkage_name;
f55ee35c
JK
10878 }
10879
94af9270
KS
10880 /* These are the only languages we know how to qualify names in. */
10881 if (name != NULL
9c37b5ae 10882 && (cu->language == language_cplus
c44af4eb
TT
10883 || cu->language == language_fortran || cu->language == language_d
10884 || cu->language == language_rust))
94af9270
KS
10885 {
10886 if (die_needs_namespace (die, cu))
10887 {
0d5cff50 10888 const char *prefix;
34a68019 10889 const char *canonical_name = NULL;
94af9270 10890
d7e74731
PA
10891 string_file buf;
10892
94af9270 10893 prefix = determine_prefix (die, cu);
94af9270
KS
10894 if (*prefix != '\0')
10895 {
43816ebc
TT
10896 gdb::unique_xmalloc_ptr<char> prefixed_name
10897 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10898
43816ebc 10899 buf.puts (prefixed_name.get ());
94af9270
KS
10900 }
10901 else
d7e74731 10902 buf.puts (name);
94af9270 10903
98bfdba5
PA
10904 /* Template parameters may be specified in the DIE's DW_AT_name, or
10905 as children with DW_TAG_template_type_param or
10906 DW_TAG_value_type_param. If the latter, add them to the name
10907 here. If the name already has template parameters, then
10908 skip this step; some versions of GCC emit both, and
10909 it is more efficient to use the pre-computed name.
10910
10911 Something to keep in mind about this process: it is very
10912 unlikely, or in some cases downright impossible, to produce
10913 something that will match the mangled name of a function.
10914 If the definition of the function has the same debug info,
10915 we should be able to match up with it anyway. But fallbacks
10916 using the minimal symbol, for instance to find a method
10917 implemented in a stripped copy of libstdc++, will not work.
10918 If we do not have debug info for the definition, we will have to
10919 match them up some other way.
10920
10921 When we do name matching there is a related problem with function
10922 templates; two instantiated function templates are allowed to
10923 differ only by their return types, which we do not add here. */
10924
10925 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10926 {
10927 struct attribute *attr;
10928 struct die_info *child;
10929 int first = 1;
10930
10931 die->building_fullname = 1;
10932
10933 for (child = die->child; child != NULL; child = child->sibling)
10934 {
10935 struct type *type;
12df843f 10936 LONGEST value;
d521ce57 10937 const gdb_byte *bytes;
98bfdba5
PA
10938 struct dwarf2_locexpr_baton *baton;
10939 struct value *v;
10940
10941 if (child->tag != DW_TAG_template_type_param
10942 && child->tag != DW_TAG_template_value_param)
10943 continue;
10944
10945 if (first)
10946 {
d7e74731 10947 buf.puts ("<");
98bfdba5
PA
10948 first = 0;
10949 }
10950 else
d7e74731 10951 buf.puts (", ");
98bfdba5
PA
10952
10953 attr = dwarf2_attr (child, DW_AT_type, cu);
10954 if (attr == NULL)
10955 {
b98664d3 10956 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10957 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10958 continue;
10959 }
10960 type = die_type (child, cu);
10961
10962 if (child->tag == DW_TAG_template_type_param)
10963 {
c1ec8cea
TT
10964 c_print_type (type, "", &buf, -1, 0, cu->language,
10965 &type_print_raw_options);
98bfdba5
PA
10966 continue;
10967 }
10968
10969 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10970 if (attr == NULL)
10971 {
b98664d3 10972 complaint (_("template parameter missing "
3e43a32a 10973 "DW_AT_const_value"));
d7e74731 10974 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10975 continue;
10976 }
10977
10978 dwarf2_const_value_attr (attr, type, name,
10979 &cu->comp_unit_obstack, cu,
10980 &value, &bytes, &baton);
10981
10982 if (TYPE_NOSIGN (type))
10983 /* GDB prints characters as NUMBER 'CHAR'. If that's
10984 changed, this can use value_print instead. */
d7e74731 10985 c_printchar (value, type, &buf);
98bfdba5
PA
10986 else
10987 {
10988 struct value_print_options opts;
10989
10990 if (baton != NULL)
10991 v = dwarf2_evaluate_loc_desc (type, NULL,
10992 baton->data,
10993 baton->size,
10994 baton->per_cu);
10995 else if (bytes != NULL)
10996 {
10997 v = allocate_value (type);
10998 memcpy (value_contents_writeable (v), bytes,
10999 TYPE_LENGTH (type));
11000 }
11001 else
11002 v = value_from_longest (type, value);
11003
3e43a32a
MS
11004 /* Specify decimal so that we do not depend on
11005 the radix. */
98bfdba5
PA
11006 get_formatted_print_options (&opts, 'd');
11007 opts.raw = 1;
d7e74731 11008 value_print (v, &buf, &opts);
98bfdba5 11009 release_value (v);
98bfdba5
PA
11010 }
11011 }
11012
11013 die->building_fullname = 0;
11014
11015 if (!first)
11016 {
11017 /* Close the argument list, with a space if necessary
11018 (nested templates). */
d7e74731
PA
11019 if (!buf.empty () && buf.string ().back () == '>')
11020 buf.puts (" >");
98bfdba5 11021 else
d7e74731 11022 buf.puts (">");
98bfdba5
PA
11023 }
11024 }
11025
9c37b5ae 11026 /* For C++ methods, append formal parameter type
94af9270 11027 information, if PHYSNAME. */
6e70227d 11028
94af9270 11029 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11030 && cu->language == language_cplus)
94af9270
KS
11031 {
11032 struct type *type = read_type_die (die, cu);
11033
d7e74731 11034 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11035 &type_print_raw_options);
94af9270 11036
9c37b5ae 11037 if (cu->language == language_cplus)
94af9270 11038 {
60430eff
DJ
11039 /* Assume that an artificial first parameter is
11040 "this", but do not crash if it is not. RealView
11041 marks unnamed (and thus unused) parameters as
11042 artificial; there is no way to differentiate
11043 the two cases. */
94af9270
KS
11044 if (TYPE_NFIELDS (type) > 0
11045 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11046 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11047 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11048 0))))
d7e74731 11049 buf.puts (" const");
94af9270
KS
11050 }
11051 }
11052
d7e74731 11053 const std::string &intermediate_name = buf.string ();
94af9270
KS
11054
11055 if (cu->language == language_cplus)
34a68019 11056 canonical_name
322a8516 11057 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11058 &objfile->per_bfd->storage_obstack);
11059
11060 /* If we only computed INTERMEDIATE_NAME, or if
11061 INTERMEDIATE_NAME is already canonical, then we need to
11062 copy it to the appropriate obstack. */
322a8516 11063 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11064 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11065 intermediate_name);
34a68019
TT
11066 else
11067 name = canonical_name;
94af9270
KS
11068 }
11069 }
11070
11071 return name;
11072}
11073
0114d602
DJ
11074/* Return the fully qualified name of DIE, based on its DW_AT_name.
11075 If scope qualifiers are appropriate they will be added. The result
34a68019 11076 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11077 not have a name. NAME may either be from a previous call to
11078 dwarf2_name or NULL.
11079
9c37b5ae 11080 The output string will be canonicalized (if C++). */
0114d602
DJ
11081
11082static const char *
15d034d0 11083dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11084{
94af9270
KS
11085 return dwarf2_compute_name (name, die, cu, 0);
11086}
0114d602 11087
94af9270
KS
11088/* Construct a physname for the given DIE in CU. NAME may either be
11089 from a previous call to dwarf2_name or NULL. The result will be
11090 allocated on the objfile_objstack or NULL if the DIE does not have a
11091 name.
0114d602 11092
9c37b5ae 11093 The output string will be canonicalized (if C++). */
0114d602 11094
94af9270 11095static const char *
15d034d0 11096dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11097{
518817b3 11098 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11099 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11100 int need_copy = 1;
11101
11102 /* In this case dwarf2_compute_name is just a shortcut not building anything
11103 on its own. */
11104 if (!die_needs_namespace (die, cu))
11105 return dwarf2_compute_name (name, die, cu, 1);
11106
73b9be8b 11107 mangled = dw2_linkage_name (die, cu);
900e11f9 11108
e98c9e7c
TT
11109 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11110 See https://github.com/rust-lang/rust/issues/32925. */
11111 if (cu->language == language_rust && mangled != NULL
11112 && strchr (mangled, '{') != NULL)
11113 mangled = NULL;
11114
900e11f9
JK
11115 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11116 has computed. */
791afaa2 11117 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11118 if (mangled != NULL)
900e11f9 11119 {
900e11f9 11120
59cc4834
JB
11121 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11122 {
11123 /* Do nothing (do not demangle the symbol name). */
11124 }
11125 else if (cu->language == language_go)
a766d390 11126 {
5e2db402
TT
11127 /* This is a lie, but we already lie to the caller new_symbol.
11128 new_symbol assumes we return the mangled name.
a766d390 11129 This just undoes that lie until things are cleaned up. */
a766d390
DE
11130 }
11131 else
11132 {
0eb876f5
JB
11133 /* Use DMGL_RET_DROP for C++ template functions to suppress
11134 their return type. It is easier for GDB users to search
11135 for such functions as `name(params)' than `long name(params)'.
11136 In such case the minimal symbol names do not match the full
11137 symbol names but for template functions there is never a need
11138 to look up their definition from their declaration so
11139 the only disadvantage remains the minimal symbol variant
11140 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11141 demangled.reset (gdb_demangle (mangled,
11142 (DMGL_PARAMS | DMGL_ANSI
11143 | DMGL_RET_DROP)));
a766d390 11144 }
900e11f9 11145 if (demangled)
791afaa2 11146 canon = demangled.get ();
900e11f9
JK
11147 else
11148 {
11149 canon = mangled;
11150 need_copy = 0;
11151 }
11152 }
11153
11154 if (canon == NULL || check_physname)
11155 {
11156 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11157
11158 if (canon != NULL && strcmp (physname, canon) != 0)
11159 {
11160 /* It may not mean a bug in GDB. The compiler could also
11161 compute DW_AT_linkage_name incorrectly. But in such case
11162 GDB would need to be bug-to-bug compatible. */
11163
b98664d3 11164 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11165 "(from linkage <%s>) - DIE at %s [in module %s]"),
11166 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11167 objfile_name (objfile));
900e11f9
JK
11168
11169 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11170 is available here - over computed PHYSNAME. It is safer
11171 against both buggy GDB and buggy compilers. */
11172
11173 retval = canon;
11174 }
11175 else
11176 {
11177 retval = physname;
11178 need_copy = 0;
11179 }
11180 }
11181 else
11182 retval = canon;
11183
11184 if (need_copy)
021887d8 11185 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11186
900e11f9 11187 return retval;
0114d602
DJ
11188}
11189
74921315
KS
11190/* Inspect DIE in CU for a namespace alias. If one exists, record
11191 a new symbol for it.
11192
11193 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11194
11195static int
11196read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11197{
11198 struct attribute *attr;
11199
11200 /* If the die does not have a name, this is not a namespace
11201 alias. */
11202 attr = dwarf2_attr (die, DW_AT_name, cu);
11203 if (attr != NULL)
11204 {
11205 int num;
11206 struct die_info *d = die;
11207 struct dwarf2_cu *imported_cu = cu;
11208
11209 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11210 keep inspecting DIEs until we hit the underlying import. */
11211#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11212 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11213 {
11214 attr = dwarf2_attr (d, DW_AT_import, cu);
11215 if (attr == NULL)
11216 break;
11217
11218 d = follow_die_ref (d, attr, &imported_cu);
11219 if (d->tag != DW_TAG_imported_declaration)
11220 break;
11221 }
11222
11223 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11224 {
b98664d3 11225 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11226 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11227 return 0;
11228 }
11229
11230 if (attr != NULL)
11231 {
11232 struct type *type;
9c541725 11233 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11234
9c541725 11235 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11236 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11237 {
11238 /* This declaration is a global namespace alias. Add
11239 a symbol for it whose type is the aliased namespace. */
11240 new_symbol (die, type, cu);
11241 return 1;
11242 }
11243 }
11244 }
11245
11246 return 0;
11247}
11248
22cee43f 11249/* Return the using directives repository (global or local?) to use in the
804d2729 11250 current context for CU.
22cee43f
PMR
11251
11252 For Ada, imported declarations can materialize renamings, which *may* be
11253 global. However it is impossible (for now?) in DWARF to distinguish
11254 "external" imported declarations and "static" ones. As all imported
11255 declarations seem to be static in all other languages, make them all CU-wide
11256 global only in Ada. */
11257
11258static struct using_direct **
804d2729 11259using_directives (struct dwarf2_cu *cu)
22cee43f 11260{
c24bdb02
KS
11261 if (cu->language == language_ada
11262 && cu->get_builder ()->outermost_context_p ())
11263 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11264 else
c24bdb02 11265 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11266}
11267
27aa8d6a
SW
11268/* Read the import statement specified by the given die and record it. */
11269
11270static void
11271read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11272{
518817b3 11273 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11274 struct attribute *import_attr;
32019081 11275 struct die_info *imported_die, *child_die;
de4affc9 11276 struct dwarf2_cu *imported_cu;
27aa8d6a 11277 const char *imported_name;
794684b6 11278 const char *imported_name_prefix;
13387711
SW
11279 const char *canonical_name;
11280 const char *import_alias;
11281 const char *imported_declaration = NULL;
794684b6 11282 const char *import_prefix;
eb1e02fd 11283 std::vector<const char *> excludes;
13387711 11284
27aa8d6a
SW
11285 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11286 if (import_attr == NULL)
11287 {
b98664d3 11288 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11289 dwarf_tag_name (die->tag));
11290 return;
11291 }
11292
de4affc9
CC
11293 imported_cu = cu;
11294 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11295 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11296 if (imported_name == NULL)
11297 {
11298 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11299
11300 The import in the following code:
11301 namespace A
11302 {
11303 typedef int B;
11304 }
11305
11306 int main ()
11307 {
11308 using A::B;
11309 B b;
11310 return b;
11311 }
11312
11313 ...
11314 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11315 <52> DW_AT_decl_file : 1
11316 <53> DW_AT_decl_line : 6
11317 <54> DW_AT_import : <0x75>
11318 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11319 <59> DW_AT_name : B
11320 <5b> DW_AT_decl_file : 1
11321 <5c> DW_AT_decl_line : 2
11322 <5d> DW_AT_type : <0x6e>
11323 ...
11324 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11325 <76> DW_AT_byte_size : 4
11326 <77> DW_AT_encoding : 5 (signed)
11327
11328 imports the wrong die ( 0x75 instead of 0x58 ).
11329 This case will be ignored until the gcc bug is fixed. */
11330 return;
11331 }
11332
82856980
SW
11333 /* Figure out the local name after import. */
11334 import_alias = dwarf2_name (die, cu);
27aa8d6a 11335
794684b6
SW
11336 /* Figure out where the statement is being imported to. */
11337 import_prefix = determine_prefix (die, cu);
11338
11339 /* Figure out what the scope of the imported die is and prepend it
11340 to the name of the imported die. */
de4affc9 11341 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11342
f55ee35c
JK
11343 if (imported_die->tag != DW_TAG_namespace
11344 && imported_die->tag != DW_TAG_module)
794684b6 11345 {
13387711
SW
11346 imported_declaration = imported_name;
11347 canonical_name = imported_name_prefix;
794684b6 11348 }
13387711 11349 else if (strlen (imported_name_prefix) > 0)
12aaed36 11350 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11351 imported_name_prefix,
11352 (cu->language == language_d ? "." : "::"),
11353 imported_name, (char *) NULL);
13387711
SW
11354 else
11355 canonical_name = imported_name;
794684b6 11356
32019081
JK
11357 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11358 for (child_die = die->child; child_die && child_die->tag;
11359 child_die = sibling_die (child_die))
11360 {
11361 /* DWARF-4: A Fortran use statement with a “rename list” may be
11362 represented by an imported module entry with an import attribute
11363 referring to the module and owned entries corresponding to those
11364 entities that are renamed as part of being imported. */
11365
11366 if (child_die->tag != DW_TAG_imported_declaration)
11367 {
b98664d3 11368 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11369 "- DIE at %s [in module %s]"),
11370 sect_offset_str (child_die->sect_off),
11371 objfile_name (objfile));
32019081
JK
11372 continue;
11373 }
11374
11375 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11376 if (import_attr == NULL)
11377 {
b98664d3 11378 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11379 dwarf_tag_name (child_die->tag));
11380 continue;
11381 }
11382
11383 imported_cu = cu;
11384 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11385 &imported_cu);
11386 imported_name = dwarf2_name (imported_die, imported_cu);
11387 if (imported_name == NULL)
11388 {
b98664d3 11389 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11390 "imported name - DIE at %s [in module %s]"),
11391 sect_offset_str (child_die->sect_off),
11392 objfile_name (objfile));
32019081
JK
11393 continue;
11394 }
11395
eb1e02fd 11396 excludes.push_back (imported_name);
32019081
JK
11397
11398 process_die (child_die, cu);
11399 }
11400
804d2729 11401 add_using_directive (using_directives (cu),
22cee43f
PMR
11402 import_prefix,
11403 canonical_name,
11404 import_alias,
11405 imported_declaration,
11406 excludes,
11407 0,
11408 &objfile->objfile_obstack);
27aa8d6a
SW
11409}
11410
5230b05a
WT
11411/* ICC<14 does not output the required DW_AT_declaration on incomplete
11412 types, but gives them a size of zero. Starting with version 14,
11413 ICC is compatible with GCC. */
11414
9068261f 11415static bool
5230b05a
WT
11416producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11417{
11418 if (!cu->checked_producer)
11419 check_producer (cu);
11420
11421 return cu->producer_is_icc_lt_14;
11422}
11423
eb77c9df
AB
11424/* ICC generates a DW_AT_type for C void functions. This was observed on
11425 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11426 which says that void functions should not have a DW_AT_type. */
11427
11428static bool
11429producer_is_icc (struct dwarf2_cu *cu)
11430{
11431 if (!cu->checked_producer)
11432 check_producer (cu);
11433
11434 return cu->producer_is_icc;
11435}
11436
1b80a9fa
JK
11437/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11438 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11439 this, it was first present in GCC release 4.3.0. */
11440
9068261f 11441static bool
1b80a9fa
JK
11442producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11443{
11444 if (!cu->checked_producer)
11445 check_producer (cu);
11446
11447 return cu->producer_is_gcc_lt_4_3;
11448}
11449
d721ba37
PA
11450static file_and_directory
11451find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11452{
d721ba37
PA
11453 file_and_directory res;
11454
9291a0cd
TT
11455 /* Find the filename. Do not use dwarf2_name here, since the filename
11456 is not a source language identifier. */
d721ba37
PA
11457 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11458 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11459
d721ba37
PA
11460 if (res.comp_dir == NULL
11461 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11462 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11463 {
d721ba37
PA
11464 res.comp_dir_storage = ldirname (res.name);
11465 if (!res.comp_dir_storage.empty ())
11466 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11467 }
d721ba37 11468 if (res.comp_dir != NULL)
9291a0cd
TT
11469 {
11470 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11471 directory, get rid of it. */
d721ba37 11472 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11473
d721ba37
PA
11474 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11475 res.comp_dir = cp + 1;
9291a0cd
TT
11476 }
11477
d721ba37
PA
11478 if (res.name == NULL)
11479 res.name = "<unknown>";
11480
11481 return res;
9291a0cd
TT
11482}
11483
f4dc4d17
DE
11484/* Handle DW_AT_stmt_list for a compilation unit.
11485 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11486 COMP_DIR is the compilation directory. LOWPC is passed to
11487 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11488
11489static void
11490handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11491 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11492{
518817b3
SM
11493 struct dwarf2_per_objfile *dwarf2_per_objfile
11494 = cu->per_cu->dwarf2_per_objfile;
527f3840 11495 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11496 struct attribute *attr;
527f3840
JK
11497 struct line_header line_header_local;
11498 hashval_t line_header_local_hash;
527f3840
JK
11499 void **slot;
11500 int decode_mapping;
2ab95328 11501
f4dc4d17
DE
11502 gdb_assert (! cu->per_cu->is_debug_types);
11503
2ab95328 11504 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11505 if (attr == NULL)
11506 return;
11507
9c541725 11508 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11509
11510 /* The line header hash table is only created if needed (it exists to
11511 prevent redundant reading of the line table for partial_units).
11512 If we're given a partial_unit, we'll need it. If we're given a
11513 compile_unit, then use the line header hash table if it's already
11514 created, but don't create one just yet. */
11515
11516 if (dwarf2_per_objfile->line_header_hash == NULL
11517 && die->tag == DW_TAG_partial_unit)
2ab95328 11518 {
527f3840
JK
11519 dwarf2_per_objfile->line_header_hash
11520 = htab_create_alloc_ex (127, line_header_hash_voidp,
11521 line_header_eq_voidp,
11522 free_line_header_voidp,
11523 &objfile->objfile_obstack,
11524 hashtab_obstack_allocate,
11525 dummy_obstack_deallocate);
11526 }
2ab95328 11527
9c541725 11528 line_header_local.sect_off = line_offset;
527f3840
JK
11529 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11530 line_header_local_hash = line_header_hash (&line_header_local);
11531 if (dwarf2_per_objfile->line_header_hash != NULL)
11532 {
11533 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11534 &line_header_local,
11535 line_header_local_hash, NO_INSERT);
11536
11537 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11538 is not present in *SLOT (since if there is something in *SLOT then
11539 it will be for a partial_unit). */
11540 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11541 {
527f3840 11542 gdb_assert (*slot != NULL);
9a3c8263 11543 cu->line_header = (struct line_header *) *slot;
527f3840 11544 return;
dee91e82 11545 }
2ab95328 11546 }
527f3840
JK
11547
11548 /* dwarf_decode_line_header does not yet provide sufficient information.
11549 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11550 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11551 if (lh == NULL)
527f3840 11552 return;
4c8aa72d
PA
11553
11554 cu->line_header = lh.release ();
11555 cu->line_header_die_owner = die;
527f3840
JK
11556
11557 if (dwarf2_per_objfile->line_header_hash == NULL)
11558 slot = NULL;
11559 else
11560 {
11561 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11562 &line_header_local,
11563 line_header_local_hash, INSERT);
11564 gdb_assert (slot != NULL);
11565 }
11566 if (slot != NULL && *slot == NULL)
11567 {
11568 /* This newly decoded line number information unit will be owned
11569 by line_header_hash hash table. */
11570 *slot = cu->line_header;
4c8aa72d 11571 cu->line_header_die_owner = NULL;
527f3840
JK
11572 }
11573 else
11574 {
11575 /* We cannot free any current entry in (*slot) as that struct line_header
11576 may be already used by multiple CUs. Create only temporary decoded
11577 line_header for this CU - it may happen at most once for each line
11578 number information unit. And if we're not using line_header_hash
11579 then this is what we want as well. */
11580 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11581 }
11582 decode_mapping = (die->tag != DW_TAG_partial_unit);
11583 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11584 decode_mapping);
fff8551c 11585
2ab95328
TT
11586}
11587
95554aad 11588/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11589
c906108c 11590static void
e7c27a73 11591read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11592{
518817b3
SM
11593 struct dwarf2_per_objfile *dwarf2_per_objfile
11594 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11595 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11596 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11597 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11598 CORE_ADDR highpc = ((CORE_ADDR) 0);
11599 struct attribute *attr;
c906108c 11600 struct die_info *child_die;
e142c38c 11601 CORE_ADDR baseaddr;
6e70227d 11602
380618d6 11603 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11604 baseaddr = objfile->text_section_offset ();
c906108c 11605
fae299cd 11606 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11607
11608 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11609 from finish_block. */
2acceee2 11610 if (lowpc == ((CORE_ADDR) -1))
c906108c 11611 lowpc = highpc;
3e29f34a 11612 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11613
d721ba37 11614 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11615
f4b8a18d
KW
11616 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11617 standardised yet. As a workaround for the language detection we fall
11618 back to the DW_AT_producer string. */
11619 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11620 cu->language = language_opencl;
11621
3019eac3
DE
11622 /* Similar hack for Go. */
11623 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11624 set_cu_language (DW_LANG_Go, cu);
11625
c24bdb02 11626 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11627
11628 /* Decode line number information if present. We do this before
11629 processing child DIEs, so that the line header table is available
11630 for DW_AT_decl_file. */
d721ba37 11631 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11632
11633 /* Process all dies in compilation unit. */
11634 if (die->child != NULL)
11635 {
11636 child_die = die->child;
11637 while (child_die && child_die->tag)
11638 {
11639 process_die (child_die, cu);
11640 child_die = sibling_die (child_die);
11641 }
11642 }
11643
11644 /* Decode macro information, if present. Dwarf 2 macro information
11645 refers to information in the line number info statement program
11646 header, so we can only read it if we've read the header
11647 successfully. */
0af92d60
JK
11648 attr = dwarf2_attr (die, DW_AT_macros, cu);
11649 if (attr == NULL)
11650 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11651 if (attr && cu->line_header)
11652 {
11653 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11654 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11655
43f3e411 11656 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11657 }
11658 else
11659 {
11660 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11661 if (attr && cu->line_header)
11662 {
11663 unsigned int macro_offset = DW_UNSND (attr);
11664
43f3e411 11665 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11666 }
11667 }
3019eac3
DE
11668}
11669
c24bdb02
KS
11670void
11671dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11672{
f4dc4d17
DE
11673 struct type_unit_group *tu_group;
11674 int first_time;
3019eac3 11675 struct attribute *attr;
9c541725 11676 unsigned int i;
0186c6a7 11677 struct signatured_type *sig_type;
3019eac3 11678
f4dc4d17 11679 gdb_assert (per_cu->is_debug_types);
0186c6a7 11680 sig_type = (struct signatured_type *) per_cu;
3019eac3 11681
c24bdb02 11682 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11683
f4dc4d17 11684 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11685 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11686 if (sig_type->type_unit_group == NULL)
c24bdb02 11687 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11688 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11689
11690 /* If we've already processed this stmt_list there's no real need to
11691 do it again, we could fake it and just recreate the part we need
11692 (file name,index -> symtab mapping). If data shows this optimization
11693 is useful we can do it then. */
43f3e411 11694 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11695
11696 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11697 debug info. */
fff8551c 11698 line_header_up lh;
f4dc4d17 11699 if (attr != NULL)
3019eac3 11700 {
9c541725 11701 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11702 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11703 }
11704 if (lh == NULL)
11705 {
11706 if (first_time)
c24bdb02 11707 start_symtab ("", NULL, 0);
f4dc4d17
DE
11708 else
11709 {
11710 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11711 gdb_assert (m_builder == nullptr);
804d2729 11712 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11713 m_builder.reset (new struct buildsym_compunit
11714 (COMPUNIT_OBJFILE (cust), "",
11715 COMPUNIT_DIRNAME (cust),
11716 compunit_language (cust),
11717 0, cust));
f4dc4d17 11718 }
f4dc4d17 11719 return;
3019eac3
DE
11720 }
11721
c24bdb02
KS
11722 line_header = lh.release ();
11723 line_header_die_owner = die;
3019eac3 11724
f4dc4d17
DE
11725 if (first_time)
11726 {
c24bdb02 11727 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11728
1fd60fc0
DE
11729 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11730 still initializing it, and our caller (a few levels up)
11731 process_full_type_unit still needs to know if this is the first
11732 time. */
11733
7ba99d21 11734 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11735 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11736 line_header->file_names_size ());
3019eac3 11737
7ba99d21
AT
11738 auto &file_names = line_header->file_names ();
11739 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11740 {
7ba99d21 11741 file_entry &fe = file_names[i];
c24bdb02
KS
11742 dwarf2_start_subfile (this, fe.name,
11743 fe.include_dir (line_header));
11744 buildsym_compunit *b = get_builder ();
11745 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11746 {
4c8aa72d
PA
11747 /* NOTE: start_subfile will recognize when it's been
11748 passed a file it has already seen. So we can't
11749 assume there's a simple mapping from
11750 cu->line_header->file_names to subfiles, plus
11751 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11752 b->get_current_subfile ()->symtab
11753 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11754 }
11755
c24bdb02 11756 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11757 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11758 }
11759 }
11760 else
3019eac3 11761 {
c24bdb02 11762 gdb_assert (m_builder == nullptr);
804d2729 11763 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11764 m_builder.reset (new struct buildsym_compunit
11765 (COMPUNIT_OBJFILE (cust), "",
11766 COMPUNIT_DIRNAME (cust),
11767 compunit_language (cust),
11768 0, cust));
f4dc4d17 11769
7ba99d21
AT
11770 auto &file_names = line_header->file_names ();
11771 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11772 {
7ba99d21 11773 file_entry &fe = file_names[i];
4c8aa72d 11774 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11775 }
3019eac3
DE
11776 }
11777
f4dc4d17
DE
11778 /* The main symtab is allocated last. Type units don't have DW_AT_name
11779 so they don't have a "real" (so to speak) symtab anyway.
11780 There is later code that will assign the main symtab to all symbols
11781 that don't have one. We need to handle the case of a symbol with a
11782 missing symtab (DW_AT_decl_file) anyway. */
11783}
3019eac3 11784
f4dc4d17
DE
11785/* Process DW_TAG_type_unit.
11786 For TUs we want to skip the first top level sibling if it's not the
11787 actual type being defined by this TU. In this case the first top
11788 level sibling is there to provide context only. */
3019eac3 11789
f4dc4d17
DE
11790static void
11791read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11792{
11793 struct die_info *child_die;
3019eac3 11794
f4dc4d17
DE
11795 prepare_one_comp_unit (cu, die, language_minimal);
11796
11797 /* Initialize (or reinitialize) the machinery for building symtabs.
11798 We do this before processing child DIEs, so that the line header table
11799 is available for DW_AT_decl_file. */
c24bdb02 11800 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11801
11802 if (die->child != NULL)
11803 {
11804 child_die = die->child;
11805 while (child_die && child_die->tag)
11806 {
11807 process_die (child_die, cu);
11808 child_die = sibling_die (child_die);
11809 }
11810 }
3019eac3
DE
11811}
11812\f
80626a55
DE
11813/* DWO/DWP files.
11814
11815 http://gcc.gnu.org/wiki/DebugFission
11816 http://gcc.gnu.org/wiki/DebugFissionDWP
11817
11818 To simplify handling of both DWO files ("object" files with the DWARF info)
11819 and DWP files (a file with the DWOs packaged up into one file), we treat
11820 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11821
11822static hashval_t
11823hash_dwo_file (const void *item)
11824{
9a3c8263 11825 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11826 hashval_t hash;
3019eac3 11827
a2ce51a0
DE
11828 hash = htab_hash_string (dwo_file->dwo_name);
11829 if (dwo_file->comp_dir != NULL)
11830 hash += htab_hash_string (dwo_file->comp_dir);
11831 return hash;
3019eac3
DE
11832}
11833
11834static int
11835eq_dwo_file (const void *item_lhs, const void *item_rhs)
11836{
9a3c8263
SM
11837 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11838 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11839
a2ce51a0
DE
11840 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11841 return 0;
11842 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11843 return lhs->comp_dir == rhs->comp_dir;
11844 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11845}
11846
11847/* Allocate a hash table for DWO files. */
11848
51ac9db5 11849static htab_up
ed2dc618 11850allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11851{
51ac9db5
SM
11852 auto delete_dwo_file = [] (void *item)
11853 {
11854 struct dwo_file *dwo_file = (struct dwo_file *) item;
11855
11856 delete dwo_file;
11857 };
11858
11859 return htab_up (htab_create_alloc_ex (41,
11860 hash_dwo_file,
11861 eq_dwo_file,
11862 delete_dwo_file,
11863 &objfile->objfile_obstack,
11864 hashtab_obstack_allocate,
11865 dummy_obstack_deallocate));
3019eac3
DE
11866}
11867
80626a55
DE
11868/* Lookup DWO file DWO_NAME. */
11869
11870static void **
ed2dc618
SM
11871lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11872 const char *dwo_name,
11873 const char *comp_dir)
80626a55
DE
11874{
11875 struct dwo_file find_entry;
11876 void **slot;
11877
11878 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11879 dwarf2_per_objfile->dwo_files
11880 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11881
0ac5b59e
DE
11882 find_entry.dwo_name = dwo_name;
11883 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11884 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11885 INSERT);
80626a55
DE
11886
11887 return slot;
11888}
11889
3019eac3
DE
11890static hashval_t
11891hash_dwo_unit (const void *item)
11892{
9a3c8263 11893 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11894
11895 /* This drops the top 32 bits of the id, but is ok for a hash. */
11896 return dwo_unit->signature;
11897}
11898
11899static int
11900eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11901{
9a3c8263
SM
11902 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11903 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11904
11905 /* The signature is assumed to be unique within the DWO file.
11906 So while object file CU dwo_id's always have the value zero,
11907 that's OK, assuming each object file DWO file has only one CU,
11908 and that's the rule for now. */
11909 return lhs->signature == rhs->signature;
11910}
11911
11912/* Allocate a hash table for DWO CUs,TUs.
11913 There is one of these tables for each of CUs,TUs for each DWO file. */
11914
11915static htab_t
11916allocate_dwo_unit_table (struct objfile *objfile)
11917{
11918 /* Start out with a pretty small number.
11919 Generally DWO files contain only one CU and maybe some TUs. */
11920 return htab_create_alloc_ex (3,
11921 hash_dwo_unit,
11922 eq_dwo_unit,
11923 NULL,
11924 &objfile->objfile_obstack,
11925 hashtab_obstack_allocate,
11926 dummy_obstack_deallocate);
11927}
11928
19c3d4c9 11929/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11930
11931static void
19c3d4c9
DE
11932create_dwo_cu_reader (const struct die_reader_specs *reader,
11933 const gdb_byte *info_ptr,
11934 struct die_info *comp_unit_die,
11935 int has_children,
c0ab21c2
TT
11936 struct dwo_file *dwo_file,
11937 struct dwo_unit *dwo_unit)
3019eac3
DE
11938{
11939 struct dwarf2_cu *cu = reader->cu;
9c541725 11940 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11941 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11942
a084a2a6
AT
11943 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11944 if (!signature.has_value ())
3019eac3 11945 {
b98664d3 11946 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11947 " its dwo_id [in module %s]"),
9d8780f0 11948 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11949 return;
11950 }
11951
3019eac3 11952 dwo_unit->dwo_file = dwo_file;
a084a2a6 11953 dwo_unit->signature = *signature;
8a0459fd 11954 dwo_unit->section = section;
9c541725 11955 dwo_unit->sect_off = sect_off;
3019eac3
DE
11956 dwo_unit->length = cu->per_cu->length;
11957
b4f54984 11958 if (dwarf_read_debug)
9d8780f0
SM
11959 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11960 sect_offset_str (sect_off),
9c541725 11961 hex_string (dwo_unit->signature));
3019eac3
DE
11962}
11963
33c5cd75 11964/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11965 Note: This function processes DWO files only, not DWP files. */
3019eac3 11966
33c5cd75 11967static void
ed2dc618 11968create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
11969 dwarf2_cu *cu, struct dwo_file &dwo_file,
11970 dwarf2_section_info &section, htab_t &cus_htab)
3019eac3
DE
11971{
11972 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11973 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11974
33c5cd75
DB
11975 dwarf2_read_section (objfile, &section);
11976 info_ptr = section.buffer;
3019eac3
DE
11977
11978 if (info_ptr == NULL)
33c5cd75 11979 return;
3019eac3 11980
b4f54984 11981 if (dwarf_read_debug)
19c3d4c9
DE
11982 {
11983 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11984 get_section_name (&section),
11985 get_section_file_name (&section));
19c3d4c9 11986 }
3019eac3 11987
33c5cd75 11988 end_ptr = info_ptr + section.size;
3019eac3
DE
11989 while (info_ptr < end_ptr)
11990 {
11991 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11992 struct dwo_unit read_unit {};
33c5cd75
DB
11993 struct dwo_unit *dwo_unit;
11994 void **slot;
11995 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11996
11997 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11998 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11999 per_cu.is_debug_types = 0;
33c5cd75
DB
12000 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12001 per_cu.section = &section;
12002
c0ab21c2
TT
12003 cutu_reader reader (&per_cu, cu, &dwo_file);
12004 if (!reader.dummy_p)
12005 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
12006 reader.has_children, &dwo_file, &read_unit);
33c5cd75
DB
12007 info_ptr += per_cu.length;
12008
12009 // If the unit could not be parsed, skip it.
c0ab21c2 12010 if (read_unit.dwo_file == NULL)
33c5cd75 12011 continue;
3019eac3 12012
33c5cd75
DB
12013 if (cus_htab == NULL)
12014 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12015
33c5cd75 12016 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 12017 *dwo_unit = read_unit;
33c5cd75
DB
12018 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12019 gdb_assert (slot != NULL);
12020 if (*slot != NULL)
19c3d4c9 12021 {
33c5cd75
DB
12022 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12023 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12024
b98664d3 12025 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12026 " the entry at offset %s, signature %s"),
12027 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12028 hex_string (dwo_unit->signature));
19c3d4c9 12029 }
33c5cd75 12030 *slot = (void *)dwo_unit;
3019eac3 12031 }
3019eac3
DE
12032}
12033
80626a55
DE
12034/* DWP file .debug_{cu,tu}_index section format:
12035 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12036
d2415c6c
DE
12037 DWP Version 1:
12038
80626a55
DE
12039 Both index sections have the same format, and serve to map a 64-bit
12040 signature to a set of section numbers. Each section begins with a header,
12041 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12042 indexes, and a pool of 32-bit section numbers. The index sections will be
12043 aligned at 8-byte boundaries in the file.
12044
d2415c6c
DE
12045 The index section header consists of:
12046
12047 V, 32 bit version number
12048 -, 32 bits unused
12049 N, 32 bit number of compilation units or type units in the index
12050 M, 32 bit number of slots in the hash table
80626a55 12051
d2415c6c 12052 Numbers are recorded using the byte order of the application binary.
80626a55 12053
d2415c6c
DE
12054 The hash table begins at offset 16 in the section, and consists of an array
12055 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12056 order of the application binary). Unused slots in the hash table are 0.
12057 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12058
d2415c6c
DE
12059 The parallel table begins immediately after the hash table
12060 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12061 array of 32-bit indexes (using the byte order of the application binary),
12062 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12063 table contains a 32-bit index into the pool of section numbers. For unused
12064 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12065
73869dc2
DE
12066 The pool of section numbers begins immediately following the hash table
12067 (at offset 16 + 12 * M from the beginning of the section). The pool of
12068 section numbers consists of an array of 32-bit words (using the byte order
12069 of the application binary). Each item in the array is indexed starting
12070 from 0. The hash table entry provides the index of the first section
12071 number in the set. Additional section numbers in the set follow, and the
12072 set is terminated by a 0 entry (section number 0 is not used in ELF).
12073
12074 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12075 section must be the first entry in the set, and the .debug_abbrev.dwo must
12076 be the second entry. Other members of the set may follow in any order.
12077
12078 ---
12079
12080 DWP Version 2:
12081
12082 DWP Version 2 combines all the .debug_info, etc. sections into one,
12083 and the entries in the index tables are now offsets into these sections.
12084 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12085 section.
12086
12087 Index Section Contents:
12088 Header
12089 Hash Table of Signatures dwp_hash_table.hash_table
12090 Parallel Table of Indices dwp_hash_table.unit_table
12091 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12092 Table of Section Sizes dwp_hash_table.v2.sizes
12093
12094 The index section header consists of:
12095
12096 V, 32 bit version number
12097 L, 32 bit number of columns in the table of section offsets
12098 N, 32 bit number of compilation units or type units in the index
12099 M, 32 bit number of slots in the hash table
12100
12101 Numbers are recorded using the byte order of the application binary.
12102
12103 The hash table has the same format as version 1.
12104 The parallel table of indices has the same format as version 1,
12105 except that the entries are origin-1 indices into the table of sections
12106 offsets and the table of section sizes.
12107
12108 The table of offsets begins immediately following the parallel table
12109 (at offset 16 + 12 * M from the beginning of the section). The table is
12110 a two-dimensional array of 32-bit words (using the byte order of the
12111 application binary), with L columns and N+1 rows, in row-major order.
12112 Each row in the array is indexed starting from 0. The first row provides
12113 a key to the remaining rows: each column in this row provides an identifier
12114 for a debug section, and the offsets in the same column of subsequent rows
12115 refer to that section. The section identifiers are:
12116
12117 DW_SECT_INFO 1 .debug_info.dwo
12118 DW_SECT_TYPES 2 .debug_types.dwo
12119 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12120 DW_SECT_LINE 4 .debug_line.dwo
12121 DW_SECT_LOC 5 .debug_loc.dwo
12122 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12123 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12124 DW_SECT_MACRO 8 .debug_macro.dwo
12125
12126 The offsets provided by the CU and TU index sections are the base offsets
12127 for the contributions made by each CU or TU to the corresponding section
12128 in the package file. Each CU and TU header contains an abbrev_offset
12129 field, used to find the abbreviations table for that CU or TU within the
12130 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12131 be interpreted as relative to the base offset given in the index section.
12132 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12133 should be interpreted as relative to the base offset for .debug_line.dwo,
12134 and offsets into other debug sections obtained from DWARF attributes should
12135 also be interpreted as relative to the corresponding base offset.
12136
12137 The table of sizes begins immediately following the table of offsets.
12138 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12139 with L columns and N rows, in row-major order. Each row in the array is
12140 indexed starting from 1 (row 0 is shared by the two tables).
12141
12142 ---
12143
12144 Hash table lookup is handled the same in version 1 and 2:
12145
12146 We assume that N and M will not exceed 2^32 - 1.
12147 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12148
d2415c6c
DE
12149 Given a 64-bit compilation unit signature or a type signature S, an entry
12150 in the hash table is located as follows:
80626a55 12151
d2415c6c
DE
12152 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12153 the low-order k bits all set to 1.
80626a55 12154
d2415c6c 12155 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12156
d2415c6c
DE
12157 3) If the hash table entry at index H matches the signature, use that
12158 entry. If the hash table entry at index H is unused (all zeroes),
12159 terminate the search: the signature is not present in the table.
80626a55 12160
d2415c6c 12161 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12162
d2415c6c 12163 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12164 to stop at an unused slot or find the match. */
80626a55
DE
12165
12166/* Create a hash table to map DWO IDs to their CU/TU entry in
12167 .debug_{info,types}.dwo in DWP_FILE.
12168 Returns NULL if there isn't one.
12169 Note: This function processes DWP files only, not DWO files. */
12170
12171static struct dwp_hash_table *
ed2dc618
SM
12172create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12173 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12174{
12175 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12176 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12177 const gdb_byte *index_ptr, *index_end;
80626a55 12178 struct dwarf2_section_info *index;
73869dc2 12179 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12180 struct dwp_hash_table *htab;
12181
12182 if (is_debug_types)
12183 index = &dwp_file->sections.tu_index;
12184 else
12185 index = &dwp_file->sections.cu_index;
12186
12187 if (dwarf2_section_empty_p (index))
12188 return NULL;
12189 dwarf2_read_section (objfile, index);
12190
12191 index_ptr = index->buffer;
12192 index_end = index_ptr + index->size;
12193
12194 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12195 index_ptr += 4;
12196 if (version == 2)
12197 nr_columns = read_4_bytes (dbfd, index_ptr);
12198 else
12199 nr_columns = 0;
12200 index_ptr += 4;
80626a55
DE
12201 nr_units = read_4_bytes (dbfd, index_ptr);
12202 index_ptr += 4;
12203 nr_slots = read_4_bytes (dbfd, index_ptr);
12204 index_ptr += 4;
12205
73869dc2 12206 if (version != 1 && version != 2)
80626a55 12207 {
21aa081e 12208 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12209 " [in module %s]"),
21aa081e 12210 pulongest (version), dwp_file->name);
80626a55
DE
12211 }
12212 if (nr_slots != (nr_slots & -nr_slots))
12213 {
21aa081e 12214 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12215 " is not power of 2 [in module %s]"),
21aa081e 12216 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12217 }
12218
12219 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12220 htab->version = version;
12221 htab->nr_columns = nr_columns;
80626a55
DE
12222 htab->nr_units = nr_units;
12223 htab->nr_slots = nr_slots;
12224 htab->hash_table = index_ptr;
12225 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12226
12227 /* Exit early if the table is empty. */
12228 if (nr_slots == 0 || nr_units == 0
12229 || (version == 2 && nr_columns == 0))
12230 {
12231 /* All must be zero. */
12232 if (nr_slots != 0 || nr_units != 0
12233 || (version == 2 && nr_columns != 0))
12234 {
b98664d3 12235 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12236 " all zero [in modules %s]"),
12237 dwp_file->name);
12238 }
12239 return htab;
12240 }
12241
12242 if (version == 1)
12243 {
12244 htab->section_pool.v1.indices =
12245 htab->unit_table + sizeof (uint32_t) * nr_slots;
12246 /* It's harder to decide whether the section is too small in v1.
12247 V1 is deprecated anyway so we punt. */
12248 }
12249 else
12250 {
12251 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12252 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12253 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12254 /* Reverse map for error checking. */
12255 int ids_seen[DW_SECT_MAX + 1];
12256 int i;
12257
12258 if (nr_columns < 2)
12259 {
12260 error (_("Dwarf Error: bad DWP hash table, too few columns"
12261 " in section table [in module %s]"),
12262 dwp_file->name);
12263 }
12264 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12265 {
12266 error (_("Dwarf Error: bad DWP hash table, too many columns"
12267 " in section table [in module %s]"),
12268 dwp_file->name);
12269 }
04fd5eed
GB
12270 memset (ids, 255, sizeof_ids);
12271 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12272 for (i = 0; i < nr_columns; ++i)
12273 {
12274 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12275
12276 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12277 {
12278 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12279 " in section table [in module %s]"),
12280 id, dwp_file->name);
12281 }
12282 if (ids_seen[id] != -1)
12283 {
12284 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12285 " id %d in section table [in module %s]"),
12286 id, dwp_file->name);
12287 }
12288 ids_seen[id] = i;
12289 ids[i] = id;
12290 }
12291 /* Must have exactly one info or types section. */
12292 if (((ids_seen[DW_SECT_INFO] != -1)
12293 + (ids_seen[DW_SECT_TYPES] != -1))
12294 != 1)
12295 {
12296 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12297 " DWO info/types section [in module %s]"),
12298 dwp_file->name);
12299 }
12300 /* Must have an abbrev section. */
12301 if (ids_seen[DW_SECT_ABBREV] == -1)
12302 {
12303 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12304 " section [in module %s]"),
12305 dwp_file->name);
12306 }
12307 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12308 htab->section_pool.v2.sizes =
12309 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12310 * nr_units * nr_columns);
12311 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12312 * nr_units * nr_columns))
12313 > index_end)
12314 {
12315 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12316 " [in module %s]"),
12317 dwp_file->name);
12318 }
12319 }
80626a55
DE
12320
12321 return htab;
12322}
12323
12324/* Update SECTIONS with the data from SECTP.
12325
12326 This function is like the other "locate" section routines that are
12327 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12328 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12329
12330 The result is non-zero for success, or zero if an error was found. */
12331
12332static int
73869dc2
DE
12333locate_v1_virtual_dwo_sections (asection *sectp,
12334 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12335{
12336 const struct dwop_section_names *names = &dwop_section_names;
12337
12338 if (section_is_p (sectp->name, &names->abbrev_dwo))
12339 {
12340 /* There can be only one. */
049412e3 12341 if (sections->abbrev.s.section != NULL)
80626a55 12342 return 0;
049412e3 12343 sections->abbrev.s.section = sectp;
fd361982 12344 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12345 }
12346 else if (section_is_p (sectp->name, &names->info_dwo)
12347 || section_is_p (sectp->name, &names->types_dwo))
12348 {
12349 /* There can be only one. */
049412e3 12350 if (sections->info_or_types.s.section != NULL)
80626a55 12351 return 0;
049412e3 12352 sections->info_or_types.s.section = sectp;
fd361982 12353 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12354 }
12355 else if (section_is_p (sectp->name, &names->line_dwo))
12356 {
12357 /* There can be only one. */
049412e3 12358 if (sections->line.s.section != NULL)
80626a55 12359 return 0;
049412e3 12360 sections->line.s.section = sectp;
fd361982 12361 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12362 }
12363 else if (section_is_p (sectp->name, &names->loc_dwo))
12364 {
12365 /* There can be only one. */
049412e3 12366 if (sections->loc.s.section != NULL)
80626a55 12367 return 0;
049412e3 12368 sections->loc.s.section = sectp;
fd361982 12369 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12370 }
12371 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12372 {
12373 /* There can be only one. */
049412e3 12374 if (sections->macinfo.s.section != NULL)
80626a55 12375 return 0;
049412e3 12376 sections->macinfo.s.section = sectp;
fd361982 12377 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12378 }
12379 else if (section_is_p (sectp->name, &names->macro_dwo))
12380 {
12381 /* There can be only one. */
049412e3 12382 if (sections->macro.s.section != NULL)
80626a55 12383 return 0;
049412e3 12384 sections->macro.s.section = sectp;
fd361982 12385 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12386 }
12387 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12388 {
12389 /* There can be only one. */
049412e3 12390 if (sections->str_offsets.s.section != NULL)
80626a55 12391 return 0;
049412e3 12392 sections->str_offsets.s.section = sectp;
fd361982 12393 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12394 }
12395 else
12396 {
12397 /* No other kind of section is valid. */
12398 return 0;
12399 }
12400
12401 return 1;
12402}
12403
73869dc2
DE
12404/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12405 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12406 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12407 This is for DWP version 1 files. */
80626a55
DE
12408
12409static struct dwo_unit *
ed2dc618
SM
12410create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12411 struct dwp_file *dwp_file,
73869dc2
DE
12412 uint32_t unit_index,
12413 const char *comp_dir,
12414 ULONGEST signature, int is_debug_types)
80626a55
DE
12415{
12416 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12417 const struct dwp_hash_table *dwp_htab =
12418 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12419 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12420 const char *kind = is_debug_types ? "TU" : "CU";
12421 struct dwo_file *dwo_file;
12422 struct dwo_unit *dwo_unit;
73869dc2 12423 struct virtual_v1_dwo_sections sections;
80626a55 12424 void **dwo_file_slot;
80626a55
DE
12425 int i;
12426
73869dc2
DE
12427 gdb_assert (dwp_file->version == 1);
12428
b4f54984 12429 if (dwarf_read_debug)
80626a55 12430 {
73869dc2 12431 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12432 kind,
73869dc2 12433 pulongest (unit_index), hex_string (signature),
80626a55
DE
12434 dwp_file->name);
12435 }
12436
19ac8c2e 12437 /* Fetch the sections of this DWO unit.
80626a55
DE
12438 Put a limit on the number of sections we look for so that bad data
12439 doesn't cause us to loop forever. */
12440
73869dc2 12441#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12442 (1 /* .debug_info or .debug_types */ \
12443 + 1 /* .debug_abbrev */ \
12444 + 1 /* .debug_line */ \
12445 + 1 /* .debug_loc */ \
12446 + 1 /* .debug_str_offsets */ \
19ac8c2e 12447 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12448 + 1 /* trailing zero */)
12449
12450 memset (&sections, 0, sizeof (sections));
80626a55 12451
73869dc2 12452 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12453 {
12454 asection *sectp;
12455 uint32_t section_nr =
12456 read_4_bytes (dbfd,
73869dc2
DE
12457 dwp_htab->section_pool.v1.indices
12458 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12459
12460 if (section_nr == 0)
12461 break;
12462 if (section_nr >= dwp_file->num_sections)
12463 {
12464 error (_("Dwarf Error: bad DWP hash table, section number too large"
12465 " [in module %s]"),
12466 dwp_file->name);
12467 }
12468
12469 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12470 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12471 {
12472 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12473 " [in module %s]"),
12474 dwp_file->name);
12475 }
12476 }
12477
12478 if (i < 2
a32a8923
DE
12479 || dwarf2_section_empty_p (&sections.info_or_types)
12480 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12481 {
12482 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12483 " [in module %s]"),
12484 dwp_file->name);
12485 }
73869dc2 12486 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12487 {
12488 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12489 " [in module %s]"),
12490 dwp_file->name);
12491 }
12492
12493 /* It's easier for the rest of the code if we fake a struct dwo_file and
12494 have dwo_unit "live" in that. At least for now.
12495
12496 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12497 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12498 file, we can combine them back into a virtual DWO file to save space
12499 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12500 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12501
791afaa2
TT
12502 std::string virtual_dwo_name =
12503 string_printf ("virtual-dwo/%d-%d-%d-%d",
12504 get_section_id (&sections.abbrev),
12505 get_section_id (&sections.line),
12506 get_section_id (&sections.loc),
12507 get_section_id (&sections.str_offsets));
80626a55 12508 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12509 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12510 virtual_dwo_name.c_str (),
12511 comp_dir);
80626a55
DE
12512 /* Create one if necessary. */
12513 if (*dwo_file_slot == NULL)
12514 {
b4f54984 12515 if (dwarf_read_debug)
80626a55
DE
12516 {
12517 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12518 virtual_dwo_name.c_str ());
80626a55 12519 }
51ac9db5 12520 dwo_file = new struct dwo_file;
efba19b0
TT
12521 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12522 virtual_dwo_name);
0ac5b59e 12523 dwo_file->comp_dir = comp_dir;
80626a55
DE
12524 dwo_file->sections.abbrev = sections.abbrev;
12525 dwo_file->sections.line = sections.line;
12526 dwo_file->sections.loc = sections.loc;
12527 dwo_file->sections.macinfo = sections.macinfo;
12528 dwo_file->sections.macro = sections.macro;
12529 dwo_file->sections.str_offsets = sections.str_offsets;
12530 /* The "str" section is global to the entire DWP file. */
12531 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12532 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12533 there's no need to record it in dwo_file.
12534 Also, we can't simply record type sections in dwo_file because
12535 we record a pointer into the vector in dwo_unit. As we collect more
12536 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12537 for it, invalidating all copies of pointers into the previous
12538 contents. */
80626a55
DE
12539 *dwo_file_slot = dwo_file;
12540 }
12541 else
12542 {
b4f54984 12543 if (dwarf_read_debug)
80626a55
DE
12544 {
12545 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12546 virtual_dwo_name.c_str ());
80626a55 12547 }
9a3c8263 12548 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12549 }
80626a55
DE
12550
12551 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12552 dwo_unit->dwo_file = dwo_file;
12553 dwo_unit->signature = signature;
8d749320
SM
12554 dwo_unit->section =
12555 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12556 *dwo_unit->section = sections.info_or_types;
57d63ce2 12557 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12558
12559 return dwo_unit;
12560}
12561
73869dc2
DE
12562/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12563 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12564 piece within that section used by a TU/CU, return a virtual section
12565 of just that piece. */
12566
12567static struct dwarf2_section_info
ed2dc618
SM
12568create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12569 struct dwarf2_section_info *section,
73869dc2
DE
12570 bfd_size_type offset, bfd_size_type size)
12571{
12572 struct dwarf2_section_info result;
12573 asection *sectp;
12574
12575 gdb_assert (section != NULL);
12576 gdb_assert (!section->is_virtual);
12577
12578 memset (&result, 0, sizeof (result));
12579 result.s.containing_section = section;
dc4ccb6f 12580 result.is_virtual = true;
73869dc2
DE
12581
12582 if (size == 0)
12583 return result;
12584
12585 sectp = get_section_bfd_section (section);
12586
12587 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12588 bounds of the real section. This is a pretty-rare event, so just
12589 flag an error (easier) instead of a warning and trying to cope. */
12590 if (sectp == NULL
fd361982 12591 || offset + size > bfd_section_size (sectp))
73869dc2 12592 {
73869dc2
DE
12593 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12594 " in section %s [in module %s]"),
fd361982 12595 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12596 objfile_name (dwarf2_per_objfile->objfile));
12597 }
12598
12599 result.virtual_offset = offset;
12600 result.size = size;
12601 return result;
12602}
12603
12604/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12605 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12606 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12607 This is for DWP version 2 files. */
12608
12609static struct dwo_unit *
ed2dc618
SM
12610create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12611 struct dwp_file *dwp_file,
73869dc2
DE
12612 uint32_t unit_index,
12613 const char *comp_dir,
12614 ULONGEST signature, int is_debug_types)
12615{
12616 struct objfile *objfile = dwarf2_per_objfile->objfile;
12617 const struct dwp_hash_table *dwp_htab =
12618 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12619 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12620 const char *kind = is_debug_types ? "TU" : "CU";
12621 struct dwo_file *dwo_file;
12622 struct dwo_unit *dwo_unit;
12623 struct virtual_v2_dwo_sections sections;
12624 void **dwo_file_slot;
73869dc2
DE
12625 int i;
12626
12627 gdb_assert (dwp_file->version == 2);
12628
b4f54984 12629 if (dwarf_read_debug)
73869dc2
DE
12630 {
12631 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12632 kind,
12633 pulongest (unit_index), hex_string (signature),
12634 dwp_file->name);
12635 }
12636
12637 /* Fetch the section offsets of this DWO unit. */
12638
12639 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12640
12641 for (i = 0; i < dwp_htab->nr_columns; ++i)
12642 {
12643 uint32_t offset = read_4_bytes (dbfd,
12644 dwp_htab->section_pool.v2.offsets
12645 + (((unit_index - 1) * dwp_htab->nr_columns
12646 + i)
12647 * sizeof (uint32_t)));
12648 uint32_t size = read_4_bytes (dbfd,
12649 dwp_htab->section_pool.v2.sizes
12650 + (((unit_index - 1) * dwp_htab->nr_columns
12651 + i)
12652 * sizeof (uint32_t)));
12653
12654 switch (dwp_htab->section_pool.v2.section_ids[i])
12655 {
12656 case DW_SECT_INFO:
12657 case DW_SECT_TYPES:
12658 sections.info_or_types_offset = offset;
12659 sections.info_or_types_size = size;
12660 break;
12661 case DW_SECT_ABBREV:
12662 sections.abbrev_offset = offset;
12663 sections.abbrev_size = size;
12664 break;
12665 case DW_SECT_LINE:
12666 sections.line_offset = offset;
12667 sections.line_size = size;
12668 break;
12669 case DW_SECT_LOC:
12670 sections.loc_offset = offset;
12671 sections.loc_size = size;
12672 break;
12673 case DW_SECT_STR_OFFSETS:
12674 sections.str_offsets_offset = offset;
12675 sections.str_offsets_size = size;
12676 break;
12677 case DW_SECT_MACINFO:
12678 sections.macinfo_offset = offset;
12679 sections.macinfo_size = size;
12680 break;
12681 case DW_SECT_MACRO:
12682 sections.macro_offset = offset;
12683 sections.macro_size = size;
12684 break;
12685 }
12686 }
12687
12688 /* It's easier for the rest of the code if we fake a struct dwo_file and
12689 have dwo_unit "live" in that. At least for now.
12690
12691 The DWP file can be made up of a random collection of CUs and TUs.
12692 However, for each CU + set of TUs that came from the same original DWO
12693 file, we can combine them back into a virtual DWO file to save space
12694 (fewer struct dwo_file objects to allocate). Remember that for really
12695 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12696
791afaa2
TT
12697 std::string virtual_dwo_name =
12698 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12699 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12700 (long) (sections.line_size ? sections.line_offset : 0),
12701 (long) (sections.loc_size ? sections.loc_offset : 0),
12702 (long) (sections.str_offsets_size
12703 ? sections.str_offsets_offset : 0));
73869dc2 12704 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12705 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12706 virtual_dwo_name.c_str (),
12707 comp_dir);
73869dc2
DE
12708 /* Create one if necessary. */
12709 if (*dwo_file_slot == NULL)
12710 {
b4f54984 12711 if (dwarf_read_debug)
73869dc2
DE
12712 {
12713 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12714 virtual_dwo_name.c_str ());
73869dc2 12715 }
51ac9db5 12716 dwo_file = new struct dwo_file;
efba19b0
TT
12717 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12718 virtual_dwo_name);
73869dc2
DE
12719 dwo_file->comp_dir = comp_dir;
12720 dwo_file->sections.abbrev =
ed2dc618 12721 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12722 sections.abbrev_offset, sections.abbrev_size);
12723 dwo_file->sections.line =
ed2dc618 12724 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12725 sections.line_offset, sections.line_size);
12726 dwo_file->sections.loc =
ed2dc618 12727 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12728 sections.loc_offset, sections.loc_size);
12729 dwo_file->sections.macinfo =
ed2dc618 12730 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12731 sections.macinfo_offset, sections.macinfo_size);
12732 dwo_file->sections.macro =
ed2dc618 12733 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12734 sections.macro_offset, sections.macro_size);
12735 dwo_file->sections.str_offsets =
ed2dc618
SM
12736 create_dwp_v2_section (dwarf2_per_objfile,
12737 &dwp_file->sections.str_offsets,
73869dc2
DE
12738 sections.str_offsets_offset,
12739 sections.str_offsets_size);
12740 /* The "str" section is global to the entire DWP file. */
12741 dwo_file->sections.str = dwp_file->sections.str;
12742 /* The info or types section is assigned below to dwo_unit,
12743 there's no need to record it in dwo_file.
12744 Also, we can't simply record type sections in dwo_file because
12745 we record a pointer into the vector in dwo_unit. As we collect more
12746 types we'll grow the vector and eventually have to reallocate space
12747 for it, invalidating all copies of pointers into the previous
12748 contents. */
12749 *dwo_file_slot = dwo_file;
12750 }
12751 else
12752 {
b4f54984 12753 if (dwarf_read_debug)
73869dc2
DE
12754 {
12755 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12756 virtual_dwo_name.c_str ());
73869dc2 12757 }
9a3c8263 12758 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12759 }
73869dc2
DE
12760
12761 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12762 dwo_unit->dwo_file = dwo_file;
12763 dwo_unit->signature = signature;
8d749320
SM
12764 dwo_unit->section =
12765 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12766 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12767 is_debug_types
73869dc2
DE
12768 ? &dwp_file->sections.types
12769 : &dwp_file->sections.info,
12770 sections.info_or_types_offset,
12771 sections.info_or_types_size);
12772 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12773
12774 return dwo_unit;
12775}
12776
57d63ce2
DE
12777/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12778 Returns NULL if the signature isn't found. */
80626a55
DE
12779
12780static struct dwo_unit *
ed2dc618
SM
12781lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12782 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12783 ULONGEST signature, int is_debug_types)
80626a55 12784{
57d63ce2
DE
12785 const struct dwp_hash_table *dwp_htab =
12786 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12787 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12788 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12789 uint32_t hash = signature & mask;
12790 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12791 unsigned int i;
12792 void **slot;
870f88f7 12793 struct dwo_unit find_dwo_cu;
80626a55
DE
12794
12795 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12796 find_dwo_cu.signature = signature;
19ac8c2e
DE
12797 slot = htab_find_slot (is_debug_types
12798 ? dwp_file->loaded_tus
12799 : dwp_file->loaded_cus,
12800 &find_dwo_cu, INSERT);
80626a55
DE
12801
12802 if (*slot != NULL)
9a3c8263 12803 return (struct dwo_unit *) *slot;
80626a55
DE
12804
12805 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12806 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12807 {
12808 ULONGEST signature_in_table;
12809
12810 signature_in_table =
57d63ce2 12811 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12812 if (signature_in_table == signature)
12813 {
57d63ce2
DE
12814 uint32_t unit_index =
12815 read_4_bytes (dbfd,
12816 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12817
73869dc2
DE
12818 if (dwp_file->version == 1)
12819 {
ed2dc618
SM
12820 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12821 dwp_file, unit_index,
73869dc2
DE
12822 comp_dir, signature,
12823 is_debug_types);
12824 }
12825 else
12826 {
ed2dc618
SM
12827 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12828 dwp_file, unit_index,
73869dc2
DE
12829 comp_dir, signature,
12830 is_debug_types);
12831 }
9a3c8263 12832 return (struct dwo_unit *) *slot;
80626a55
DE
12833 }
12834 if (signature_in_table == 0)
12835 return NULL;
12836 hash = (hash + hash2) & mask;
12837 }
12838
12839 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12840 " [in module %s]"),
12841 dwp_file->name);
12842}
12843
ab5088bf 12844/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12845 Open the file specified by FILE_NAME and hand it off to BFD for
12846 preliminary analysis. Return a newly initialized bfd *, which
12847 includes a canonicalized copy of FILE_NAME.
80626a55 12848 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12849 SEARCH_CWD is true if the current directory is to be searched.
12850 It will be searched before debug-file-directory.
13aaf454
DE
12851 If successful, the file is added to the bfd include table of the
12852 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12853 If unable to find/open the file, return NULL.
3019eac3
DE
12854 NOTE: This function is derived from symfile_bfd_open. */
12855
192b62ce 12856static gdb_bfd_ref_ptr
ed2dc618
SM
12857try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12858 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12859{
24b9144d 12860 int desc;
9c02c129
DE
12861 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12862 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12863 to debug_file_directory. */
e0cc99a6 12864 const char *search_path;
9c02c129
DE
12865 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12866
e0cc99a6 12867 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12868 if (search_cwd)
12869 {
12870 if (*debug_file_directory != '\0')
e0cc99a6
TT
12871 {
12872 search_path_holder.reset (concat (".", dirname_separator_string,
12873 debug_file_directory,
12874 (char *) NULL));
12875 search_path = search_path_holder.get ();
12876 }
6ac97d4c 12877 else
e0cc99a6 12878 search_path = ".";
6ac97d4c 12879 }
9c02c129 12880 else
e0cc99a6 12881 search_path = debug_file_directory;
3019eac3 12882
24b9144d 12883 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12884 if (is_dwp)
12885 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12886
12887 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12888 desc = openp (search_path, flags, file_name,
3019eac3
DE
12889 O_RDONLY | O_BINARY, &absolute_name);
12890 if (desc < 0)
12891 return NULL;
12892
e0cc99a6
TT
12893 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12894 gnutarget, desc));
9c02c129
DE
12895 if (sym_bfd == NULL)
12896 return NULL;
192b62ce 12897 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12898
192b62ce
TT
12899 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12900 return NULL;
3019eac3 12901
13aaf454
DE
12902 /* Success. Record the bfd as having been included by the objfile's bfd.
12903 This is important because things like demangled_names_hash lives in the
12904 objfile's per_bfd space and may have references to things like symbol
12905 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12906 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12907
3019eac3
DE
12908 return sym_bfd;
12909}
12910
ab5088bf 12911/* Try to open DWO file FILE_NAME.
3019eac3
DE
12912 COMP_DIR is the DW_AT_comp_dir attribute.
12913 The result is the bfd handle of the file.
12914 If there is a problem finding or opening the file, return NULL.
12915 Upon success, the canonicalized path of the file is stored in the bfd,
12916 same as symfile_bfd_open. */
12917
192b62ce 12918static gdb_bfd_ref_ptr
ed2dc618
SM
12919open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12920 const char *file_name, const char *comp_dir)
3019eac3 12921{
80626a55 12922 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12923 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12924 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12925
12926 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12927
12928 if (comp_dir != NULL)
12929 {
43816ebc
TT
12930 gdb::unique_xmalloc_ptr<char> path_to_try
12931 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12932
12933 /* NOTE: If comp_dir is a relative path, this will also try the
12934 search path, which seems useful. */
ed2dc618 12935 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12936 path_to_try.get (),
ed2dc618 12937 0 /*is_dwp*/,
192b62ce 12938 1 /*search_cwd*/));
3019eac3
DE
12939 if (abfd != NULL)
12940 return abfd;
12941 }
12942
12943 /* That didn't work, try debug-file-directory, which, despite its name,
12944 is a list of paths. */
12945
12946 if (*debug_file_directory == '\0')
12947 return NULL;
12948
ed2dc618
SM
12949 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12950 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12951}
12952
80626a55
DE
12953/* This function is mapped across the sections and remembers the offset and
12954 size of each of the DWO debugging sections we are interested in. */
12955
12956static void
12957dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12958{
9a3c8263 12959 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12960 const struct dwop_section_names *names = &dwop_section_names;
12961
12962 if (section_is_p (sectp->name, &names->abbrev_dwo))
12963 {
049412e3 12964 dwo_sections->abbrev.s.section = sectp;
fd361982 12965 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12966 }
12967 else if (section_is_p (sectp->name, &names->info_dwo))
12968 {
049412e3 12969 dwo_sections->info.s.section = sectp;
fd361982 12970 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12971 }
12972 else if (section_is_p (sectp->name, &names->line_dwo))
12973 {
049412e3 12974 dwo_sections->line.s.section = sectp;
fd361982 12975 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12976 }
12977 else if (section_is_p (sectp->name, &names->loc_dwo))
12978 {
049412e3 12979 dwo_sections->loc.s.section = sectp;
fd361982 12980 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12981 }
12982 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12983 {
049412e3 12984 dwo_sections->macinfo.s.section = sectp;
fd361982 12985 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12986 }
12987 else if (section_is_p (sectp->name, &names->macro_dwo))
12988 {
049412e3 12989 dwo_sections->macro.s.section = sectp;
fd361982 12990 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12991 }
12992 else if (section_is_p (sectp->name, &names->str_dwo))
12993 {
049412e3 12994 dwo_sections->str.s.section = sectp;
fd361982 12995 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12996 }
12997 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12998 {
049412e3 12999 dwo_sections->str_offsets.s.section = sectp;
fd361982 13000 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
13001 }
13002 else if (section_is_p (sectp->name, &names->types_dwo))
13003 {
13004 struct dwarf2_section_info type_section;
13005
13006 memset (&type_section, 0, sizeof (type_section));
049412e3 13007 type_section.s.section = sectp;
fd361982 13008 type_section.size = bfd_section_size (sectp);
fd5866f6 13009 dwo_sections->types.push_back (type_section);
80626a55
DE
13010 }
13011}
13012
ab5088bf 13013/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13014 by PER_CU. This is for the non-DWP case.
80626a55 13015 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13016
13017static struct dwo_file *
0ac5b59e
DE
13018open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13019 const char *dwo_name, const char *comp_dir)
3019eac3 13020{
ed2dc618 13021 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13022
fb1eb2f9 13023 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13024 if (dbfd == NULL)
13025 {
b4f54984 13026 if (dwarf_read_debug)
80626a55
DE
13027 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13028 return NULL;
13029 }
263db9a1 13030
51ac9db5 13031 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13032 dwo_file->dwo_name = dwo_name;
13033 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13034 dwo_file->dbfd = std::move (dbfd);
3019eac3 13035
fb1eb2f9 13036 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13037 &dwo_file->sections);
3019eac3 13038
18a8505e
AT
13039 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
13040 dwo_file->sections.info, dwo_file->cus);
3019eac3 13041
263db9a1 13042 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13043 dwo_file->sections.types, dwo_file->tus);
3019eac3 13044
b4f54984 13045 if (dwarf_read_debug)
80626a55
DE
13046 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13047
263db9a1 13048 return dwo_file.release ();
3019eac3
DE
13049}
13050
80626a55 13051/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13052 size of each of the DWP debugging sections common to version 1 and 2 that
13053 we are interested in. */
3019eac3 13054
80626a55 13055static void
73869dc2
DE
13056dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13057 void *dwp_file_ptr)
3019eac3 13058{
9a3c8263 13059 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13060 const struct dwop_section_names *names = &dwop_section_names;
13061 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13062
80626a55 13063 /* Record the ELF section number for later lookup: this is what the
73869dc2 13064 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13065 gdb_assert (elf_section_nr < dwp_file->num_sections);
13066 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13067
80626a55
DE
13068 /* Look for specific sections that we need. */
13069 if (section_is_p (sectp->name, &names->str_dwo))
13070 {
049412e3 13071 dwp_file->sections.str.s.section = sectp;
fd361982 13072 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13073 }
13074 else if (section_is_p (sectp->name, &names->cu_index))
13075 {
049412e3 13076 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13077 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13078 }
13079 else if (section_is_p (sectp->name, &names->tu_index))
13080 {
049412e3 13081 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13082 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13083 }
13084}
3019eac3 13085
73869dc2
DE
13086/* This function is mapped across the sections and remembers the offset and
13087 size of each of the DWP version 2 debugging sections that we are interested
13088 in. This is split into a separate function because we don't know if we
13089 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13090
13091static void
13092dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13093{
9a3c8263 13094 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13095 const struct dwop_section_names *names = &dwop_section_names;
13096 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13097
13098 /* Record the ELF section number for later lookup: this is what the
13099 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13100 gdb_assert (elf_section_nr < dwp_file->num_sections);
13101 dwp_file->elf_sections[elf_section_nr] = sectp;
13102
13103 /* Look for specific sections that we need. */
13104 if (section_is_p (sectp->name, &names->abbrev_dwo))
13105 {
049412e3 13106 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13107 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13108 }
13109 else if (section_is_p (sectp->name, &names->info_dwo))
13110 {
049412e3 13111 dwp_file->sections.info.s.section = sectp;
fd361982 13112 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13113 }
13114 else if (section_is_p (sectp->name, &names->line_dwo))
13115 {
049412e3 13116 dwp_file->sections.line.s.section = sectp;
fd361982 13117 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13118 }
13119 else if (section_is_p (sectp->name, &names->loc_dwo))
13120 {
049412e3 13121 dwp_file->sections.loc.s.section = sectp;
fd361982 13122 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13123 }
13124 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13125 {
049412e3 13126 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13127 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13128 }
13129 else if (section_is_p (sectp->name, &names->macro_dwo))
13130 {
049412e3 13131 dwp_file->sections.macro.s.section = sectp;
fd361982 13132 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13133 }
13134 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13135 {
049412e3 13136 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13137 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13138 }
13139 else if (section_is_p (sectp->name, &names->types_dwo))
13140 {
049412e3 13141 dwp_file->sections.types.s.section = sectp;
fd361982 13142 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13143 }
13144}
13145
80626a55 13146/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13147
80626a55
DE
13148static hashval_t
13149hash_dwp_loaded_cutus (const void *item)
13150{
9a3c8263 13151 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13152
80626a55
DE
13153 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13154 return dwo_unit->signature;
3019eac3
DE
13155}
13156
80626a55 13157/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13158
80626a55
DE
13159static int
13160eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13161{
9a3c8263
SM
13162 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13163 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13164
80626a55
DE
13165 return dua->signature == dub->signature;
13166}
3019eac3 13167
80626a55 13168/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13169
80626a55
DE
13170static htab_t
13171allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13172{
13173 return htab_create_alloc_ex (3,
13174 hash_dwp_loaded_cutus,
13175 eq_dwp_loaded_cutus,
13176 NULL,
13177 &objfile->objfile_obstack,
13178 hashtab_obstack_allocate,
13179 dummy_obstack_deallocate);
13180}
3019eac3 13181
ab5088bf
DE
13182/* Try to open DWP file FILE_NAME.
13183 The result is the bfd handle of the file.
13184 If there is a problem finding or opening the file, return NULL.
13185 Upon success, the canonicalized path of the file is stored in the bfd,
13186 same as symfile_bfd_open. */
13187
192b62ce 13188static gdb_bfd_ref_ptr
ed2dc618
SM
13189open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13190 const char *file_name)
ab5088bf 13191{
ed2dc618
SM
13192 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13193 1 /*is_dwp*/,
192b62ce 13194 1 /*search_cwd*/));
6ac97d4c
DE
13195 if (abfd != NULL)
13196 return abfd;
13197
13198 /* Work around upstream bug 15652.
13199 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13200 [Whether that's a "bug" is debatable, but it is getting in our way.]
13201 We have no real idea where the dwp file is, because gdb's realpath-ing
13202 of the executable's path may have discarded the needed info.
13203 [IWBN if the dwp file name was recorded in the executable, akin to
13204 .gnu_debuglink, but that doesn't exist yet.]
13205 Strip the directory from FILE_NAME and search again. */
13206 if (*debug_file_directory != '\0')
13207 {
13208 /* Don't implicitly search the current directory here.
13209 If the user wants to search "." to handle this case,
13210 it must be added to debug-file-directory. */
ed2dc618
SM
13211 return try_open_dwop_file (dwarf2_per_objfile,
13212 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13213 0 /*search_cwd*/);
13214 }
13215
13216 return NULL;
ab5088bf
DE
13217}
13218
80626a55
DE
13219/* Initialize the use of the DWP file for the current objfile.
13220 By convention the name of the DWP file is ${objfile}.dwp.
13221 The result is NULL if it can't be found. */
a766d390 13222
400174b1 13223static std::unique_ptr<struct dwp_file>
ed2dc618 13224open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13225{
13226 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13227
82bf32bc
JK
13228 /* Try to find first .dwp for the binary file before any symbolic links
13229 resolving. */
6c447423
DE
13230
13231 /* If the objfile is a debug file, find the name of the real binary
13232 file and get the name of dwp file from there. */
d721ba37 13233 std::string dwp_name;
6c447423
DE
13234 if (objfile->separate_debug_objfile_backlink != NULL)
13235 {
13236 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13237 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13238
d721ba37 13239 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13240 }
13241 else
d721ba37
PA
13242 dwp_name = objfile->original_name;
13243
13244 dwp_name += ".dwp";
80626a55 13245
ed2dc618 13246 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13247 if (dbfd == NULL
13248 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13249 {
13250 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13251 dwp_name = objfile_name (objfile);
13252 dwp_name += ".dwp";
ed2dc618 13253 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13254 }
13255
80626a55
DE
13256 if (dbfd == NULL)
13257 {
b4f54984 13258 if (dwarf_read_debug)
d721ba37 13259 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13260 return std::unique_ptr<dwp_file> ();
3019eac3 13261 }
400174b1
TT
13262
13263 const char *name = bfd_get_filename (dbfd.get ());
13264 std::unique_ptr<struct dwp_file> dwp_file
13265 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13266
0a0f4c01 13267 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13268 dwp_file->elf_sections =
13269 OBSTACK_CALLOC (&objfile->objfile_obstack,
13270 dwp_file->num_sections, asection *);
13271
400174b1
TT
13272 bfd_map_over_sections (dwp_file->dbfd.get (),
13273 dwarf2_locate_common_dwp_sections,
13274 dwp_file.get ());
80626a55 13275
400174b1
TT
13276 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13277 0);
80626a55 13278
400174b1
TT
13279 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13280 1);
80626a55 13281
73869dc2 13282 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13283 if (dwp_file->cus && dwp_file->tus
13284 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13285 {
13286 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13287 pretty bizarre. We use pulongest here because that's the established
4d65956b 13288 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13289 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13290 " TU version %s [in DWP file %s]"),
13291 pulongest (dwp_file->cus->version),
d721ba37 13292 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13293 }
08302ed2
DE
13294
13295 if (dwp_file->cus)
13296 dwp_file->version = dwp_file->cus->version;
13297 else if (dwp_file->tus)
13298 dwp_file->version = dwp_file->tus->version;
13299 else
13300 dwp_file->version = 2;
73869dc2
DE
13301
13302 if (dwp_file->version == 2)
400174b1
TT
13303 bfd_map_over_sections (dwp_file->dbfd.get (),
13304 dwarf2_locate_v2_dwp_sections,
13305 dwp_file.get ());
73869dc2 13306
19ac8c2e
DE
13307 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13308 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13309
b4f54984 13310 if (dwarf_read_debug)
80626a55
DE
13311 {
13312 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13313 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13314 " %s CUs, %s TUs\n",
13315 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13316 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13317 }
13318
13319 return dwp_file;
3019eac3 13320}
c906108c 13321
ab5088bf
DE
13322/* Wrapper around open_and_init_dwp_file, only open it once. */
13323
13324static struct dwp_file *
ed2dc618 13325get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13326{
13327 if (! dwarf2_per_objfile->dwp_checked)
13328 {
ed2dc618
SM
13329 dwarf2_per_objfile->dwp_file
13330 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13331 dwarf2_per_objfile->dwp_checked = 1;
13332 }
400174b1 13333 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13334}
13335
80626a55
DE
13336/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13337 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13338 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13339 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13340 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13341
13342 This is called, for example, when wanting to read a variable with a
13343 complex location. Therefore we don't want to do file i/o for every call.
13344 Therefore we don't want to look for a DWO file on every call.
13345 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13346 then we check if we've already seen DWO_NAME, and only THEN do we check
13347 for a DWO file.
13348
1c658ad5 13349 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13350 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13351
3019eac3 13352static struct dwo_unit *
80626a55
DE
13353lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13354 const char *dwo_name, const char *comp_dir,
13355 ULONGEST signature, int is_debug_types)
3019eac3 13356{
ed2dc618 13357 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13358 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13359 const char *kind = is_debug_types ? "TU" : "CU";
13360 void **dwo_file_slot;
3019eac3 13361 struct dwo_file *dwo_file;
80626a55 13362 struct dwp_file *dwp_file;
cb1df416 13363
6a506a2d
DE
13364 /* First see if there's a DWP file.
13365 If we have a DWP file but didn't find the DWO inside it, don't
13366 look for the original DWO file. It makes gdb behave differently
13367 depending on whether one is debugging in the build tree. */
cf2c3c16 13368
ed2dc618 13369 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13370 if (dwp_file != NULL)
cf2c3c16 13371 {
80626a55
DE
13372 const struct dwp_hash_table *dwp_htab =
13373 is_debug_types ? dwp_file->tus : dwp_file->cus;
13374
13375 if (dwp_htab != NULL)
13376 {
13377 struct dwo_unit *dwo_cutu =
ed2dc618 13378 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13379 signature, is_debug_types);
80626a55
DE
13380
13381 if (dwo_cutu != NULL)
13382 {
b4f54984 13383 if (dwarf_read_debug)
80626a55
DE
13384 {
13385 fprintf_unfiltered (gdb_stdlog,
13386 "Virtual DWO %s %s found: @%s\n",
13387 kind, hex_string (signature),
13388 host_address_to_string (dwo_cutu));
13389 }
13390 return dwo_cutu;
13391 }
13392 }
13393 }
6a506a2d 13394 else
80626a55 13395 {
6a506a2d 13396 /* No DWP file, look for the DWO file. */
80626a55 13397
ed2dc618
SM
13398 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13399 dwo_name, comp_dir);
6a506a2d 13400 if (*dwo_file_slot == NULL)
80626a55 13401 {
6a506a2d
DE
13402 /* Read in the file and build a table of the CUs/TUs it contains. */
13403 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13404 }
6a506a2d 13405 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13406 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13407
6a506a2d 13408 if (dwo_file != NULL)
19c3d4c9 13409 {
6a506a2d
DE
13410 struct dwo_unit *dwo_cutu = NULL;
13411
13412 if (is_debug_types && dwo_file->tus)
13413 {
13414 struct dwo_unit find_dwo_cutu;
13415
13416 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13417 find_dwo_cutu.signature = signature;
9a3c8263
SM
13418 dwo_cutu
13419 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13420 }
33c5cd75 13421 else if (!is_debug_types && dwo_file->cus)
80626a55 13422 {
33c5cd75
DB
13423 struct dwo_unit find_dwo_cutu;
13424
13425 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13426 find_dwo_cutu.signature = signature;
13427 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13428 &find_dwo_cutu);
6a506a2d
DE
13429 }
13430
13431 if (dwo_cutu != NULL)
13432 {
b4f54984 13433 if (dwarf_read_debug)
6a506a2d
DE
13434 {
13435 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13436 kind, dwo_name, hex_string (signature),
13437 host_address_to_string (dwo_cutu));
13438 }
13439 return dwo_cutu;
80626a55
DE
13440 }
13441 }
2e276125 13442 }
9cdd5dbd 13443
80626a55
DE
13444 /* We didn't find it. This could mean a dwo_id mismatch, or
13445 someone deleted the DWO/DWP file, or the search path isn't set up
13446 correctly to find the file. */
13447
b4f54984 13448 if (dwarf_read_debug)
80626a55
DE
13449 {
13450 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13451 kind, dwo_name, hex_string (signature));
13452 }
3019eac3 13453
6656a72d
DE
13454 /* This is a warning and not a complaint because it can be caused by
13455 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13456 {
13457 /* Print the name of the DWP file if we looked there, helps the user
13458 better diagnose the problem. */
791afaa2 13459 std::string dwp_text;
43942612
DE
13460
13461 if (dwp_file != NULL)
791afaa2
TT
13462 dwp_text = string_printf (" [in DWP file %s]",
13463 lbasename (dwp_file->name));
43942612 13464
9d8780f0 13465 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13466 " [in module %s]"),
13467 kind, dwo_name, hex_string (signature),
791afaa2 13468 dwp_text.c_str (),
43942612 13469 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13470 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13471 }
3019eac3 13472 return NULL;
5fb290d7
DJ
13473}
13474
80626a55
DE
13475/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13476 See lookup_dwo_cutu_unit for details. */
13477
13478static struct dwo_unit *
13479lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13480 const char *dwo_name, const char *comp_dir,
13481 ULONGEST signature)
13482{
13483 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13484}
13485
13486/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13487 See lookup_dwo_cutu_unit for details. */
13488
13489static struct dwo_unit *
13490lookup_dwo_type_unit (struct signatured_type *this_tu,
13491 const char *dwo_name, const char *comp_dir)
13492{
13493 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13494}
13495
89e63ee4
DE
13496/* Traversal function for queue_and_load_all_dwo_tus. */
13497
13498static int
13499queue_and_load_dwo_tu (void **slot, void *info)
13500{
13501 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13502 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13503 ULONGEST signature = dwo_unit->signature;
13504 struct signatured_type *sig_type =
13505 lookup_dwo_signatured_type (per_cu->cu, signature);
13506
13507 if (sig_type != NULL)
13508 {
13509 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13510
13511 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13512 a real dependency of PER_CU on SIG_TYPE. That is detected later
13513 while processing PER_CU. */
13514 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13515 load_full_type_unit (sig_cu);
ae640021 13516 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13517 }
13518
13519 return 1;
13520}
13521
13522/* Queue all TUs contained in the DWO of PER_CU to be read in.
13523 The DWO may have the only definition of the type, though it may not be
13524 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13525 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13526
13527static void
13528queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13529{
13530 struct dwo_unit *dwo_unit;
13531 struct dwo_file *dwo_file;
13532
13533 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13534 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13535 gdb_assert (per_cu->cu != NULL);
13536
13537 dwo_unit = per_cu->cu->dwo_unit;
13538 gdb_assert (dwo_unit != NULL);
13539
13540 dwo_file = dwo_unit->dwo_file;
13541 if (dwo_file->tus != NULL)
13542 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13543}
13544
3019eac3 13545/* Read in various DIEs. */
348e048f 13546
d389af10 13547/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13548 Inherit only the children of the DW_AT_abstract_origin DIE not being
13549 already referenced by DW_AT_abstract_origin from the children of the
13550 current DIE. */
d389af10
JK
13551
13552static void
13553inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13554{
13555 struct die_info *child_die;
791afaa2 13556 sect_offset *offsetp;
d389af10
JK
13557 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13558 struct die_info *origin_die;
13559 /* Iterator of the ORIGIN_DIE children. */
13560 struct die_info *origin_child_die;
d389af10 13561 struct attribute *attr;
cd02d79d
PA
13562 struct dwarf2_cu *origin_cu;
13563 struct pending **origin_previous_list_in_scope;
d389af10
JK
13564
13565 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13566 if (!attr)
13567 return;
13568
cd02d79d
PA
13569 /* Note that following die references may follow to a die in a
13570 different cu. */
13571
13572 origin_cu = cu;
13573 origin_die = follow_die_ref (die, attr, &origin_cu);
13574
13575 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13576 symbols in. */
13577 origin_previous_list_in_scope = origin_cu->list_in_scope;
13578 origin_cu->list_in_scope = cu->list_in_scope;
13579
edb3359d
DJ
13580 if (die->tag != origin_die->tag
13581 && !(die->tag == DW_TAG_inlined_subroutine
13582 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13583 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13584 sect_offset_str (die->sect_off),
13585 sect_offset_str (origin_die->sect_off));
d389af10 13586
791afaa2 13587 std::vector<sect_offset> offsets;
d389af10 13588
3ea89b92
PMR
13589 for (child_die = die->child;
13590 child_die && child_die->tag;
13591 child_die = sibling_die (child_die))
13592 {
13593 struct die_info *child_origin_die;
13594 struct dwarf2_cu *child_origin_cu;
13595
13596 /* We are trying to process concrete instance entries:
216f72a1 13597 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13598 it's not relevant to our analysis here. i.e. detecting DIEs that are
13599 present in the abstract instance but not referenced in the concrete
13600 one. */
216f72a1
JK
13601 if (child_die->tag == DW_TAG_call_site
13602 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13603 continue;
13604
c38f313d
DJ
13605 /* For each CHILD_DIE, find the corresponding child of
13606 ORIGIN_DIE. If there is more than one layer of
13607 DW_AT_abstract_origin, follow them all; there shouldn't be,
13608 but GCC versions at least through 4.4 generate this (GCC PR
13609 40573). */
3ea89b92
PMR
13610 child_origin_die = child_die;
13611 child_origin_cu = cu;
c38f313d
DJ
13612 while (1)
13613 {
cd02d79d
PA
13614 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13615 child_origin_cu);
c38f313d
DJ
13616 if (attr == NULL)
13617 break;
cd02d79d
PA
13618 child_origin_die = follow_die_ref (child_origin_die, attr,
13619 &child_origin_cu);
c38f313d
DJ
13620 }
13621
d389af10
JK
13622 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13623 counterpart may exist. */
c38f313d 13624 if (child_origin_die != child_die)
d389af10 13625 {
edb3359d
DJ
13626 if (child_die->tag != child_origin_die->tag
13627 && !(child_die->tag == DW_TAG_inlined_subroutine
13628 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13629 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13630 "different tags"),
9d8780f0
SM
13631 sect_offset_str (child_die->sect_off),
13632 sect_offset_str (child_origin_die->sect_off));
c38f313d 13633 if (child_origin_die->parent != origin_die)
b98664d3 13634 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13635 "different parents"),
9d8780f0
SM
13636 sect_offset_str (child_die->sect_off),
13637 sect_offset_str (child_origin_die->sect_off));
c38f313d 13638 else
791afaa2 13639 offsets.push_back (child_origin_die->sect_off);
d389af10 13640 }
d389af10 13641 }
791afaa2
TT
13642 std::sort (offsets.begin (), offsets.end ());
13643 sect_offset *offsets_end = offsets.data () + offsets.size ();
13644 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13645 if (offsetp[-1] == *offsetp)
b98664d3 13646 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13647 "to DIE %s as their abstract origin"),
13648 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13649
791afaa2 13650 offsetp = offsets.data ();
d389af10
JK
13651 origin_child_die = origin_die->child;
13652 while (origin_child_die && origin_child_die->tag)
13653 {
13654 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13655 while (offsetp < offsets_end
9c541725 13656 && *offsetp < origin_child_die->sect_off)
d389af10 13657 offsetp++;
b64f50a1 13658 if (offsetp >= offsets_end
9c541725 13659 || *offsetp > origin_child_die->sect_off)
d389af10 13660 {
adde2bff
DE
13661 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13662 Check whether we're already processing ORIGIN_CHILD_DIE.
13663 This can happen with mutually referenced abstract_origins.
13664 PR 16581. */
13665 if (!origin_child_die->in_process)
13666 process_die (origin_child_die, origin_cu);
d389af10
JK
13667 }
13668 origin_child_die = sibling_die (origin_child_die);
13669 }
cd02d79d 13670 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13671
13672 if (cu != origin_cu)
13673 compute_delayed_physnames (origin_cu);
d389af10
JK
13674}
13675
c906108c 13676static void
e7c27a73 13677read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13678{
518817b3 13679 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13680 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13681 struct context_stack *newobj;
c906108c
SS
13682 CORE_ADDR lowpc;
13683 CORE_ADDR highpc;
13684 struct die_info *child_die;
edb3359d 13685 struct attribute *attr, *call_line, *call_file;
15d034d0 13686 const char *name;
e142c38c 13687 CORE_ADDR baseaddr;
801e3a5b 13688 struct block *block;
edb3359d 13689 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13690 std::vector<struct symbol *> template_args;
34eaf542 13691 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13692
13693 if (inlined_func)
13694 {
13695 /* If we do not have call site information, we can't show the
13696 caller of this inlined function. That's too confusing, so
13697 only use the scope for local variables. */
13698 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13699 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13700 if (call_line == NULL || call_file == NULL)
13701 {
13702 read_lexical_block_scope (die, cu);
13703 return;
13704 }
13705 }
c906108c 13706
b3b3bada 13707 baseaddr = objfile->text_section_offset ();
e142c38c 13708
94af9270 13709 name = dwarf2_name (die, cu);
c906108c 13710
e8d05480
JB
13711 /* Ignore functions with missing or empty names. These are actually
13712 illegal according to the DWARF standard. */
13713 if (name == NULL)
13714 {
b98664d3 13715 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13716 sect_offset_str (die->sect_off));
e8d05480
JB
13717 return;
13718 }
13719
13720 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13721 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13722 <= PC_BOUNDS_INVALID)
e8d05480 13723 {
ae4d0c03
PM
13724 attr = dwarf2_attr (die, DW_AT_external, cu);
13725 if (!attr || !DW_UNSND (attr))
b98664d3 13726 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13727 "for subprogram DIE at %s"),
13728 sect_offset_str (die->sect_off));
e8d05480
JB
13729 return;
13730 }
c906108c 13731
3e29f34a
MR
13732 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13733 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13734
34eaf542
TT
13735 /* If we have any template arguments, then we must allocate a
13736 different sort of symbol. */
13737 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13738 {
13739 if (child_die->tag == DW_TAG_template_type_param
13740 || child_die->tag == DW_TAG_template_value_param)
13741 {
e623cf5d 13742 templ_func = allocate_template_symbol (objfile);
cf724bc9 13743 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13744 break;
13745 }
13746 }
13747
c24bdb02 13748 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13749 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13750 (struct symbol *) templ_func);
4c2df51b 13751
81873cc8 13752 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13753 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13754 cu->language);
13755
4cecd739
DJ
13756 /* If there is a location expression for DW_AT_frame_base, record
13757 it. */
e142c38c 13758 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13759 if (attr != nullptr)
fe978cb0 13760 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13761
63e43d3a
PMR
13762 /* If there is a location for the static link, record it. */
13763 newobj->static_link = NULL;
13764 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13765 if (attr != nullptr)
63e43d3a 13766 {
224c3ddb
SM
13767 newobj->static_link
13768 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13769 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13770 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13771 }
13772
c24bdb02 13773 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13774
639d11d3 13775 if (die->child != NULL)
c906108c 13776 {
639d11d3 13777 child_die = die->child;
c906108c
SS
13778 while (child_die && child_die->tag)
13779 {
34eaf542
TT
13780 if (child_die->tag == DW_TAG_template_type_param
13781 || child_die->tag == DW_TAG_template_value_param)
13782 {
13783 struct symbol *arg = new_symbol (child_die, NULL, cu);
13784
f1078f66 13785 if (arg != NULL)
2f4732b0 13786 template_args.push_back (arg);
34eaf542
TT
13787 }
13788 else
13789 process_die (child_die, cu);
c906108c
SS
13790 child_die = sibling_die (child_die);
13791 }
13792 }
13793
d389af10
JK
13794 inherit_abstract_dies (die, cu);
13795
4a811a97
UW
13796 /* If we have a DW_AT_specification, we might need to import using
13797 directives from the context of the specification DIE. See the
13798 comment in determine_prefix. */
13799 if (cu->language == language_cplus
13800 && dwarf2_attr (die, DW_AT_specification, cu))
13801 {
13802 struct dwarf2_cu *spec_cu = cu;
13803 struct die_info *spec_die = die_specification (die, &spec_cu);
13804
13805 while (spec_die)
13806 {
13807 child_die = spec_die->child;
13808 while (child_die && child_die->tag)
13809 {
13810 if (child_die->tag == DW_TAG_imported_module)
13811 process_die (child_die, spec_cu);
13812 child_die = sibling_die (child_die);
13813 }
13814
13815 /* In some cases, GCC generates specification DIEs that
13816 themselves contain DW_AT_specification attributes. */
13817 spec_die = die_specification (spec_die, &spec_cu);
13818 }
13819 }
13820
c24bdb02 13821 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13822 /* Make a block for the local symbols within. */
c24bdb02 13823 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13824 cstk.static_link, lowpc, highpc);
801e3a5b 13825
df8a16a1 13826 /* For C++, set the block's scope. */
45280282
IB
13827 if ((cu->language == language_cplus
13828 || cu->language == language_fortran
c44af4eb
TT
13829 || cu->language == language_d
13830 || cu->language == language_rust)
4d4ec4e5 13831 && cu->processing_has_namespace_info)
195a3f6c
TT
13832 block_set_scope (block, determine_prefix (die, cu),
13833 &objfile->objfile_obstack);
df8a16a1 13834
801e3a5b
JB
13835 /* If we have address ranges, record them. */
13836 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13837
a60f3166 13838 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13839
34eaf542 13840 /* Attach template arguments to function. */
2f4732b0 13841 if (!template_args.empty ())
34eaf542
TT
13842 {
13843 gdb_assert (templ_func != NULL);
13844
2f4732b0 13845 templ_func->n_template_arguments = template_args.size ();
34eaf542 13846 templ_func->template_arguments
8d749320
SM
13847 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13848 templ_func->n_template_arguments);
34eaf542 13849 memcpy (templ_func->template_arguments,
2f4732b0 13850 template_args.data (),
34eaf542 13851 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13852
13853 /* Make sure that the symtab is set on the new symbols. Even
13854 though they don't appear in this symtab directly, other parts
13855 of gdb assume that symbols do, and this is reasonably
13856 true. */
8634679f 13857 for (symbol *sym : template_args)
3e1d3d8c 13858 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13859 }
13860
208d8187
JB
13861 /* In C++, we can have functions nested inside functions (e.g., when
13862 a function declares a class that has methods). This means that
13863 when we finish processing a function scope, we may need to go
13864 back to building a containing block's symbol lists. */
c24bdb02
KS
13865 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13866 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13867
921e78cf
JB
13868 /* If we've finished processing a top-level function, subsequent
13869 symbols go in the file symbol list. */
c24bdb02
KS
13870 if (cu->get_builder ()->outermost_context_p ())
13871 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13872}
13873
13874/* Process all the DIES contained within a lexical block scope. Start
13875 a new scope, process the dies, and then close the scope. */
13876
13877static void
e7c27a73 13878read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13879{
518817b3 13880 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13881 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13882 CORE_ADDR lowpc, highpc;
13883 struct die_info *child_die;
e142c38c
DJ
13884 CORE_ADDR baseaddr;
13885
b3b3bada 13886 baseaddr = objfile->text_section_offset ();
c906108c
SS
13887
13888 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13889 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13890 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13891 be nasty. Might be easier to properly extend generic blocks to
af34e669 13892 describe ranges. */
e385593e
JK
13893 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13894 {
13895 case PC_BOUNDS_NOT_PRESENT:
13896 /* DW_TAG_lexical_block has no attributes, process its children as if
13897 there was no wrapping by that DW_TAG_lexical_block.
13898 GCC does no longer produces such DWARF since GCC r224161. */
13899 for (child_die = die->child;
13900 child_die != NULL && child_die->tag;
13901 child_die = sibling_die (child_die))
13902 process_die (child_die, cu);
13903 return;
13904 case PC_BOUNDS_INVALID:
13905 return;
13906 }
3e29f34a
MR
13907 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13908 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13909
c24bdb02 13910 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13911 if (die->child != NULL)
c906108c 13912 {
639d11d3 13913 child_die = die->child;
c906108c
SS
13914 while (child_die && child_die->tag)
13915 {
e7c27a73 13916 process_die (child_die, cu);
c906108c
SS
13917 child_die = sibling_die (child_die);
13918 }
13919 }
3ea89b92 13920 inherit_abstract_dies (die, cu);
c24bdb02 13921 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13922
c24bdb02
KS
13923 if (*cu->get_builder ()->get_local_symbols () != NULL
13924 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13925 {
801e3a5b 13926 struct block *block
c24bdb02 13927 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13928 cstk.start_addr, highpc);
801e3a5b
JB
13929
13930 /* Note that recording ranges after traversing children, as we
13931 do here, means that recording a parent's ranges entails
13932 walking across all its children's ranges as they appear in
13933 the address map, which is quadratic behavior.
13934
13935 It would be nicer to record the parent's ranges before
13936 traversing its children, simply overriding whatever you find
13937 there. But since we don't even decide whether to create a
13938 block until after we've traversed its children, that's hard
13939 to do. */
13940 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13941 }
c24bdb02
KS
13942 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13943 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13944}
13945
216f72a1 13946/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13947
13948static void
13949read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13950{
518817b3 13951 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13953 CORE_ADDR pc, baseaddr;
13954 struct attribute *attr;
13955 struct call_site *call_site, call_site_local;
13956 void **slot;
13957 int nparams;
13958 struct die_info *child_die;
13959
b3b3bada 13960 baseaddr = objfile->text_section_offset ();
96408a79 13961
216f72a1
JK
13962 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13963 if (attr == NULL)
13964 {
13965 /* This was a pre-DWARF-5 GNU extension alias
13966 for DW_AT_call_return_pc. */
13967 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13968 }
96408a79
SA
13969 if (!attr)
13970 {
b98664d3 13971 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13972 "DIE %s [in module %s]"),
13973 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13974 return;
13975 }
31aa7e4e 13976 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13977 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13978
13979 if (cu->call_site_htab == NULL)
13980 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13981 NULL, &objfile->objfile_obstack,
13982 hashtab_obstack_allocate, NULL);
13983 call_site_local.pc = pc;
13984 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13985 if (*slot != NULL)
13986 {
b98664d3 13987 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13988 "DIE %s [in module %s]"),
13989 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13990 objfile_name (objfile));
96408a79
SA
13991 return;
13992 }
13993
13994 /* Count parameters at the caller. */
13995
13996 nparams = 0;
13997 for (child_die = die->child; child_die && child_die->tag;
13998 child_die = sibling_die (child_die))
13999 {
216f72a1
JK
14000 if (child_die->tag != DW_TAG_call_site_parameter
14001 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 14002 {
b98664d3 14003 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14004 "DW_TAG_call_site child DIE %s [in module %s]"),
14005 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14006 objfile_name (objfile));
96408a79
SA
14007 continue;
14008 }
14009
14010 nparams++;
14011 }
14012
224c3ddb
SM
14013 call_site
14014 = ((struct call_site *)
14015 obstack_alloc (&objfile->objfile_obstack,
14016 sizeof (*call_site)
14017 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14018 *slot = call_site;
14019 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14020 call_site->pc = pc;
14021
216f72a1
JK
14022 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14023 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14024 {
14025 struct die_info *func_die;
14026
14027 /* Skip also over DW_TAG_inlined_subroutine. */
14028 for (func_die = die->parent;
14029 func_die && func_die->tag != DW_TAG_subprogram
14030 && func_die->tag != DW_TAG_subroutine_type;
14031 func_die = func_die->parent);
14032
216f72a1
JK
14033 /* DW_AT_call_all_calls is a superset
14034 of DW_AT_call_all_tail_calls. */
96408a79 14035 if (func_die
216f72a1 14036 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14037 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14038 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14039 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14040 {
14041 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14042 not complete. But keep CALL_SITE for look ups via call_site_htab,
14043 both the initial caller containing the real return address PC and
14044 the final callee containing the current PC of a chain of tail
14045 calls do not need to have the tail call list complete. But any
14046 function candidate for a virtual tail call frame searched via
14047 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14048 determined unambiguously. */
14049 }
14050 else
14051 {
14052 struct type *func_type = NULL;
14053
14054 if (func_die)
14055 func_type = get_die_type (func_die, cu);
14056 if (func_type != NULL)
14057 {
14058 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14059
14060 /* Enlist this call site to the function. */
14061 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14062 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14063 }
14064 else
b98664d3 14065 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14066 "DIE %s [in module %s]"),
14067 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14068 }
14069 }
14070
216f72a1
JK
14071 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14072 if (attr == NULL)
14073 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14074 if (attr == NULL)
14075 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14076 if (attr == NULL)
216f72a1
JK
14077 {
14078 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14079 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14080 }
96408a79
SA
14081 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14082 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14083 /* Keep NULL DWARF_BLOCK. */;
14084 else if (attr_form_is_block (attr))
14085 {
14086 struct dwarf2_locexpr_baton *dlbaton;
14087
8d749320 14088 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14089 dlbaton->data = DW_BLOCK (attr)->data;
14090 dlbaton->size = DW_BLOCK (attr)->size;
14091 dlbaton->per_cu = cu->per_cu;
14092
14093 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14094 }
7771576e 14095 else if (attr_form_is_ref (attr))
96408a79 14096 {
96408a79
SA
14097 struct dwarf2_cu *target_cu = cu;
14098 struct die_info *target_die;
14099
ac9ec31b 14100 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14101 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14102 if (die_is_declaration (target_die, target_cu))
14103 {
7d45c7c3 14104 const char *target_physname;
9112db09
JK
14105
14106 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14107 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14108 if (target_physname == NULL)
9112db09 14109 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14110 if (target_physname == NULL)
b98664d3 14111 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14112 "physname, for referencing DIE %s [in module %s]"),
14113 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14114 else
7d455152 14115 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14116 }
14117 else
14118 {
14119 CORE_ADDR lowpc;
14120
14121 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14122 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14123 <= PC_BOUNDS_INVALID)
b98664d3 14124 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14125 "low pc, for referencing DIE %s [in module %s]"),
14126 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14127 else
3e29f34a
MR
14128 {
14129 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14130 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14131 }
96408a79
SA
14132 }
14133 }
14134 else
b98664d3 14135 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14136 "block nor reference, for DIE %s [in module %s]"),
14137 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14138
14139 call_site->per_cu = cu->per_cu;
14140
14141 for (child_die = die->child;
14142 child_die && child_die->tag;
14143 child_die = sibling_die (child_die))
14144 {
96408a79 14145 struct call_site_parameter *parameter;
1788b2d3 14146 struct attribute *loc, *origin;
96408a79 14147
216f72a1
JK
14148 if (child_die->tag != DW_TAG_call_site_parameter
14149 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14150 {
14151 /* Already printed the complaint above. */
14152 continue;
14153 }
14154
14155 gdb_assert (call_site->parameter_count < nparams);
14156 parameter = &call_site->parameter[call_site->parameter_count];
14157
1788b2d3
JK
14158 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14159 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14160 register is contained in DW_AT_call_value. */
96408a79 14161
24c5c679 14162 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14163 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14164 if (origin == NULL)
14165 {
14166 /* This was a pre-DWARF-5 GNU extension alias
14167 for DW_AT_call_parameter. */
14168 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14169 }
7771576e 14170 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14171 {
1788b2d3 14172 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14173
14174 sect_offset sect_off
14175 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14176 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14177 {
14178 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14179 binding can be done only inside one CU. Such referenced DIE
14180 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14181 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14182 "DW_TAG_call_site child DIE %s [in module %s]"),
14183 sect_offset_str (child_die->sect_off),
9c541725 14184 objfile_name (objfile));
d76b7dbc
JK
14185 continue;
14186 }
9c541725
PA
14187 parameter->u.param_cu_off
14188 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14189 }
14190 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14191 {
b98664d3 14192 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14193 "DW_TAG_call_site child DIE %s [in module %s]"),
14194 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14195 continue;
14196 }
24c5c679 14197 else
96408a79 14198 {
24c5c679
JK
14199 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14200 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14201 if (parameter->u.dwarf_reg != -1)
14202 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14203 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14204 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14205 &parameter->u.fb_offset))
14206 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14207 else
14208 {
b98664d3 14209 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14210 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14211 "DW_TAG_call_site child DIE %s "
24c5c679 14212 "[in module %s]"),
9d8780f0 14213 sect_offset_str (child_die->sect_off),
9c541725 14214 objfile_name (objfile));
24c5c679
JK
14215 continue;
14216 }
96408a79
SA
14217 }
14218
216f72a1
JK
14219 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14220 if (attr == NULL)
14221 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14222 if (!attr_form_is_block (attr))
14223 {
b98664d3 14224 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14225 "DW_TAG_call_site child DIE %s [in module %s]"),
14226 sect_offset_str (child_die->sect_off),
9c541725 14227 objfile_name (objfile));
96408a79
SA
14228 continue;
14229 }
14230 parameter->value = DW_BLOCK (attr)->data;
14231 parameter->value_size = DW_BLOCK (attr)->size;
14232
14233 /* Parameters are not pre-cleared by memset above. */
14234 parameter->data_value = NULL;
14235 parameter->data_value_size = 0;
14236 call_site->parameter_count++;
14237
216f72a1
JK
14238 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14239 if (attr == NULL)
14240 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14241 if (attr != nullptr)
96408a79
SA
14242 {
14243 if (!attr_form_is_block (attr))
b98664d3 14244 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14245 "DW_TAG_call_site child DIE %s [in module %s]"),
14246 sect_offset_str (child_die->sect_off),
9c541725 14247 objfile_name (objfile));
96408a79
SA
14248 else
14249 {
14250 parameter->data_value = DW_BLOCK (attr)->data;
14251 parameter->data_value_size = DW_BLOCK (attr)->size;
14252 }
14253 }
14254 }
14255}
14256
71a3c369
TT
14257/* Helper function for read_variable. If DIE represents a virtual
14258 table, then return the type of the concrete object that is
14259 associated with the virtual table. Otherwise, return NULL. */
14260
14261static struct type *
14262rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14263{
14264 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14265 if (attr == NULL)
14266 return NULL;
14267
14268 /* Find the type DIE. */
14269 struct die_info *type_die = NULL;
14270 struct dwarf2_cu *type_cu = cu;
14271
14272 if (attr_form_is_ref (attr))
14273 type_die = follow_die_ref (die, attr, &type_cu);
14274 if (type_die == NULL)
14275 return NULL;
14276
14277 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14278 return NULL;
14279 return die_containing_type (type_die, type_cu);
14280}
14281
14282/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14283
14284static void
14285read_variable (struct die_info *die, struct dwarf2_cu *cu)
14286{
14287 struct rust_vtable_symbol *storage = NULL;
14288
14289 if (cu->language == language_rust)
14290 {
14291 struct type *containing_type = rust_containing_type (die, cu);
14292
14293 if (containing_type != NULL)
14294 {
518817b3 14295 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 14296
468c0cbb 14297 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
14298 initialize_objfile_symbol (storage);
14299 storage->concrete_type = containing_type;
cf724bc9 14300 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14301 }
14302 }
14303
e4a62c65
TV
14304 struct symbol *res = new_symbol (die, NULL, cu, storage);
14305 struct attribute *abstract_origin
14306 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14307 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14308 if (res == NULL && loc && abstract_origin)
14309 {
14310 /* We have a variable without a name, but with a location and an abstract
14311 origin. This may be a concrete instance of an abstract variable
14312 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14313 later. */
14314 struct dwarf2_cu *origin_cu = cu;
14315 struct die_info *origin_die
14316 = follow_die_ref (die, abstract_origin, &origin_cu);
14317 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14318 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14319 }
71a3c369
TT
14320}
14321
43988095
JK
14322/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14323 reading .debug_rnglists.
14324 Callback's type should be:
14325 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14326 Return true if the attributes are present and valid, otherwise,
14327 return false. */
14328
14329template <typename Callback>
14330static bool
14331dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14332 Callback &&callback)
14333{
ed2dc618 14334 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14335 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14336 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14337 bfd *obfd = objfile->obfd;
43988095
JK
14338 /* Base address selection entry. */
14339 CORE_ADDR base;
14340 int found_base;
43988095 14341 const gdb_byte *buffer;
43988095
JK
14342 CORE_ADDR baseaddr;
14343 bool overflow = false;
14344
14345 found_base = cu->base_known;
14346 base = cu->base_address;
14347
14348 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14349 if (offset >= dwarf2_per_objfile->rnglists.size)
14350 {
b98664d3 14351 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14352 offset);
14353 return false;
14354 }
14355 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14356
b3b3bada 14357 baseaddr = objfile->text_section_offset ();
43988095
JK
14358
14359 while (1)
14360 {
7814882a
JK
14361 /* Initialize it due to a false compiler warning. */
14362 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14363 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14364 + dwarf2_per_objfile->rnglists.size);
14365 unsigned int bytes_read;
14366
14367 if (buffer == buf_end)
14368 {
14369 overflow = true;
14370 break;
14371 }
14372 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14373 switch (rlet)
14374 {
14375 case DW_RLE_end_of_list:
14376 break;
14377 case DW_RLE_base_address:
14378 if (buffer + cu->header.addr_size > buf_end)
14379 {
14380 overflow = true;
14381 break;
14382 }
14383 base = read_address (obfd, buffer, cu, &bytes_read);
14384 found_base = 1;
14385 buffer += bytes_read;
14386 break;
14387 case DW_RLE_start_length:
14388 if (buffer + cu->header.addr_size > buf_end)
14389 {
14390 overflow = true;
14391 break;
14392 }
14393 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14394 buffer += bytes_read;
14395 range_end = (range_beginning
14396 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14397 buffer += bytes_read;
14398 if (buffer > buf_end)
14399 {
14400 overflow = true;
14401 break;
14402 }
14403 break;
14404 case DW_RLE_offset_pair:
14405 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14406 buffer += bytes_read;
14407 if (buffer > buf_end)
14408 {
14409 overflow = true;
14410 break;
14411 }
14412 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14413 buffer += bytes_read;
14414 if (buffer > buf_end)
14415 {
14416 overflow = true;
14417 break;
14418 }
14419 break;
14420 case DW_RLE_start_end:
14421 if (buffer + 2 * cu->header.addr_size > buf_end)
14422 {
14423 overflow = true;
14424 break;
14425 }
14426 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14427 buffer += bytes_read;
14428 range_end = read_address (obfd, buffer, cu, &bytes_read);
14429 buffer += bytes_read;
14430 break;
14431 default:
b98664d3 14432 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14433 return false;
14434 }
14435 if (rlet == DW_RLE_end_of_list || overflow)
14436 break;
14437 if (rlet == DW_RLE_base_address)
14438 continue;
14439
14440 if (!found_base)
14441 {
14442 /* We have no valid base address for the ranges
14443 data. */
b98664d3 14444 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14445 return false;
14446 }
14447
14448 if (range_beginning > range_end)
14449 {
14450 /* Inverted range entries are invalid. */
b98664d3 14451 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14452 return false;
14453 }
14454
14455 /* Empty range entries have no effect. */
14456 if (range_beginning == range_end)
14457 continue;
14458
14459 range_beginning += base;
14460 range_end += base;
14461
14462 /* A not-uncommon case of bad debug info.
14463 Don't pollute the addrmap with bad data. */
14464 if (range_beginning + baseaddr == 0
14465 && !dwarf2_per_objfile->has_section_at_zero)
14466 {
b98664d3 14467 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14468 " [in module %s]"), objfile_name (objfile));
14469 continue;
14470 }
14471
14472 callback (range_beginning, range_end);
14473 }
14474
14475 if (overflow)
14476 {
b98664d3 14477 complaint (_("Offset %d is not terminated "
43988095
JK
14478 "for DW_AT_ranges attribute"),
14479 offset);
14480 return false;
14481 }
14482
14483 return true;
14484}
14485
14486/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14487 Callback's type should be:
14488 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14489 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14490
43988095 14491template <typename Callback>
43039443 14492static int
5f46c5a5 14493dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14494 Callback &&callback)
43039443 14495{
ed2dc618 14496 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14497 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14498 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14499 struct comp_unit_head *cu_header = &cu->header;
14500 bfd *obfd = objfile->obfd;
14501 unsigned int addr_size = cu_header->addr_size;
14502 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14503 /* Base address selection entry. */
14504 CORE_ADDR base;
14505 int found_base;
14506 unsigned int dummy;
d521ce57 14507 const gdb_byte *buffer;
ff013f42 14508 CORE_ADDR baseaddr;
43039443 14509
43988095
JK
14510 if (cu_header->version >= 5)
14511 return dwarf2_rnglists_process (offset, cu, callback);
14512
d00adf39
DE
14513 found_base = cu->base_known;
14514 base = cu->base_address;
43039443 14515
be391dca 14516 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14517 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14518 {
b98664d3 14519 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14520 offset);
14521 return 0;
14522 }
dce234bc 14523 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14524
b3b3bada 14525 baseaddr = objfile->text_section_offset ();
ff013f42 14526
43039443
JK
14527 while (1)
14528 {
14529 CORE_ADDR range_beginning, range_end;
14530
14531 range_beginning = read_address (obfd, buffer, cu, &dummy);
14532 buffer += addr_size;
14533 range_end = read_address (obfd, buffer, cu, &dummy);
14534 buffer += addr_size;
14535 offset += 2 * addr_size;
14536
14537 /* An end of list marker is a pair of zero addresses. */
14538 if (range_beginning == 0 && range_end == 0)
14539 /* Found the end of list entry. */
14540 break;
14541
14542 /* Each base address selection entry is a pair of 2 values.
14543 The first is the largest possible address, the second is
14544 the base address. Check for a base address here. */
14545 if ((range_beginning & mask) == mask)
14546 {
28d2bfb9
AB
14547 /* If we found the largest possible address, then we already
14548 have the base address in range_end. */
14549 base = range_end;
43039443
JK
14550 found_base = 1;
14551 continue;
14552 }
14553
14554 if (!found_base)
14555 {
14556 /* We have no valid base address for the ranges
14557 data. */
b98664d3 14558 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14559 return 0;
14560 }
14561
9277c30c
UW
14562 if (range_beginning > range_end)
14563 {
14564 /* Inverted range entries are invalid. */
b98664d3 14565 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14566 return 0;
14567 }
14568
14569 /* Empty range entries have no effect. */
14570 if (range_beginning == range_end)
14571 continue;
14572
43039443
JK
14573 range_beginning += base;
14574 range_end += base;
14575
01093045
DE
14576 /* A not-uncommon case of bad debug info.
14577 Don't pollute the addrmap with bad data. */
14578 if (range_beginning + baseaddr == 0
14579 && !dwarf2_per_objfile->has_section_at_zero)
14580 {
b98664d3 14581 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14582 " [in module %s]"), objfile_name (objfile));
01093045
DE
14583 continue;
14584 }
14585
5f46c5a5
JK
14586 callback (range_beginning, range_end);
14587 }
14588
14589 return 1;
14590}
14591
14592/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14593 Return 1 if the attributes are present and valid, otherwise, return 0.
14594 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14595
14596static int
14597dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14598 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14599 struct partial_symtab *ranges_pst)
14600{
518817b3 14601 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 14602 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 14603 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14604 int low_set = 0;
14605 CORE_ADDR low = 0;
14606 CORE_ADDR high = 0;
14607 int retval;
14608
14609 retval = dwarf2_ranges_process (offset, cu,
14610 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14611 {
9277c30c 14612 if (ranges_pst != NULL)
3e29f34a
MR
14613 {
14614 CORE_ADDR lowpc;
14615 CORE_ADDR highpc;
14616
79748972
TT
14617 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14618 range_beginning + baseaddr)
14619 - baseaddr);
14620 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14621 range_end + baseaddr)
14622 - baseaddr);
d320c2b5
TT
14623 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14624 lowpc, highpc - 1, ranges_pst);
3e29f34a 14625 }
ff013f42 14626
43039443
JK
14627 /* FIXME: This is recording everything as a low-high
14628 segment of consecutive addresses. We should have a
14629 data structure for discontiguous block ranges
14630 instead. */
14631 if (! low_set)
14632 {
14633 low = range_beginning;
14634 high = range_end;
14635 low_set = 1;
14636 }
14637 else
14638 {
14639 if (range_beginning < low)
14640 low = range_beginning;
14641 if (range_end > high)
14642 high = range_end;
14643 }
5f46c5a5
JK
14644 });
14645 if (!retval)
14646 return 0;
43039443
JK
14647
14648 if (! low_set)
14649 /* If the first entry is an end-of-list marker, the range
14650 describes an empty scope, i.e. no instructions. */
14651 return 0;
14652
14653 if (low_return)
14654 *low_return = low;
14655 if (high_return)
14656 *high_return = high;
14657 return 1;
14658}
14659
3a2b436a
JK
14660/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14661 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14662 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14663
3a2b436a 14664static enum pc_bounds_kind
af34e669 14665dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14666 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14667 struct partial_symtab *pst)
c906108c 14668{
518817b3
SM
14669 struct dwarf2_per_objfile *dwarf2_per_objfile
14670 = cu->per_cu->dwarf2_per_objfile;
c906108c 14671 struct attribute *attr;
91da1414 14672 struct attribute *attr_high;
af34e669
DJ
14673 CORE_ADDR low = 0;
14674 CORE_ADDR high = 0;
e385593e 14675 enum pc_bounds_kind ret;
c906108c 14676
91da1414
MW
14677 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14678 if (attr_high)
af34e669 14679 {
e142c38c 14680 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14681 if (attr != nullptr)
91da1414 14682 {
31aa7e4e
JB
14683 low = attr_value_as_address (attr);
14684 high = attr_value_as_address (attr_high);
14685 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14686 high += low;
91da1414 14687 }
af34e669
DJ
14688 else
14689 /* Found high w/o low attribute. */
e385593e 14690 return PC_BOUNDS_INVALID;
af34e669
DJ
14691
14692 /* Found consecutive range of addresses. */
3a2b436a 14693 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14694 }
c906108c 14695 else
af34e669 14696 {
e142c38c 14697 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14698 if (attr != NULL)
14699 {
18a8505e 14700 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14701 We take advantage of the fact that DW_AT_ranges does not appear
14702 in DW_TAG_compile_unit of DWO files. */
14703 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14704 unsigned int ranges_offset = (DW_UNSND (attr)
14705 + (need_ranges_base
14706 ? cu->ranges_base
14707 : 0));
2e3cf129 14708
af34e669 14709 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14710 .debug_ranges section. */
2e3cf129 14711 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14712 return PC_BOUNDS_INVALID;
43039443 14713 /* Found discontinuous range of addresses. */
3a2b436a 14714 ret = PC_BOUNDS_RANGES;
af34e669 14715 }
e385593e
JK
14716 else
14717 return PC_BOUNDS_NOT_PRESENT;
af34e669 14718 }
c906108c 14719
48fbe735 14720 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14721 if (high <= low)
e385593e 14722 return PC_BOUNDS_INVALID;
c906108c
SS
14723
14724 /* When using the GNU linker, .gnu.linkonce. sections are used to
14725 eliminate duplicate copies of functions and vtables and such.
14726 The linker will arbitrarily choose one and discard the others.
14727 The AT_*_pc values for such functions refer to local labels in
14728 these sections. If the section from that file was discarded, the
14729 labels are not in the output, so the relocs get a value of 0.
14730 If this is a discarded function, mark the pc bounds as invalid,
14731 so that GDB will ignore it. */
72dca2f5 14732 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14733 return PC_BOUNDS_INVALID;
c906108c
SS
14734
14735 *lowpc = low;
96408a79
SA
14736 if (highpc)
14737 *highpc = high;
af34e669 14738 return ret;
c906108c
SS
14739}
14740
b084d499
JB
14741/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14742 its low and high PC addresses. Do nothing if these addresses could not
14743 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14744 and HIGHPC to the high address if greater than HIGHPC. */
14745
14746static void
14747dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14748 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14749 struct dwarf2_cu *cu)
14750{
14751 CORE_ADDR low, high;
14752 struct die_info *child = die->child;
14753
e385593e 14754 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14755 {
325fac50
PA
14756 *lowpc = std::min (*lowpc, low);
14757 *highpc = std::max (*highpc, high);
b084d499
JB
14758 }
14759
14760 /* If the language does not allow nested subprograms (either inside
14761 subprograms or lexical blocks), we're done. */
14762 if (cu->language != language_ada)
14763 return;
6e70227d 14764
b084d499
JB
14765 /* Check all the children of the given DIE. If it contains nested
14766 subprograms, then check their pc bounds. Likewise, we need to
14767 check lexical blocks as well, as they may also contain subprogram
14768 definitions. */
14769 while (child && child->tag)
14770 {
14771 if (child->tag == DW_TAG_subprogram
14772 || child->tag == DW_TAG_lexical_block)
14773 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14774 child = sibling_die (child);
14775 }
14776}
14777
fae299cd
DC
14778/* Get the low and high pc's represented by the scope DIE, and store
14779 them in *LOWPC and *HIGHPC. If the correct values can't be
14780 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14781
14782static void
14783get_scope_pc_bounds (struct die_info *die,
14784 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14785 struct dwarf2_cu *cu)
14786{
14787 CORE_ADDR best_low = (CORE_ADDR) -1;
14788 CORE_ADDR best_high = (CORE_ADDR) 0;
14789 CORE_ADDR current_low, current_high;
14790
3a2b436a 14791 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14792 >= PC_BOUNDS_RANGES)
fae299cd
DC
14793 {
14794 best_low = current_low;
14795 best_high = current_high;
14796 }
14797 else
14798 {
14799 struct die_info *child = die->child;
14800
14801 while (child && child->tag)
14802 {
14803 switch (child->tag) {
14804 case DW_TAG_subprogram:
b084d499 14805 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14806 break;
14807 case DW_TAG_namespace:
f55ee35c 14808 case DW_TAG_module:
fae299cd
DC
14809 /* FIXME: carlton/2004-01-16: Should we do this for
14810 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14811 that current GCC's always emit the DIEs corresponding
14812 to definitions of methods of classes as children of a
14813 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14814 the DIEs giving the declarations, which could be
14815 anywhere). But I don't see any reason why the
14816 standards says that they have to be there. */
14817 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14818
14819 if (current_low != ((CORE_ADDR) -1))
14820 {
325fac50
PA
14821 best_low = std::min (best_low, current_low);
14822 best_high = std::max (best_high, current_high);
fae299cd
DC
14823 }
14824 break;
14825 default:
0963b4bd 14826 /* Ignore. */
fae299cd
DC
14827 break;
14828 }
14829
14830 child = sibling_die (child);
14831 }
14832 }
14833
14834 *lowpc = best_low;
14835 *highpc = best_high;
14836}
14837
801e3a5b
JB
14838/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14839 in DIE. */
380bca97 14840
801e3a5b
JB
14841static void
14842dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14843 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14844{
518817b3 14845 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14846 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14847 struct attribute *attr;
91da1414 14848 struct attribute *attr_high;
801e3a5b 14849
91da1414
MW
14850 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14851 if (attr_high)
801e3a5b 14852 {
801e3a5b 14853 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14854 if (attr != nullptr)
801e3a5b 14855 {
31aa7e4e
JB
14856 CORE_ADDR low = attr_value_as_address (attr);
14857 CORE_ADDR high = attr_value_as_address (attr_high);
14858
14859 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14860 high += low;
9a619af0 14861
3e29f34a
MR
14862 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14863 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14864 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14865 }
14866 }
14867
14868 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14869 if (attr != nullptr)
801e3a5b 14870 {
18a8505e 14871 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14872 We take advantage of the fact that DW_AT_ranges does not appear
14873 in DW_TAG_compile_unit of DWO files. */
14874 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14875
14876 /* The value of the DW_AT_ranges attribute is the offset of the
14877 address range list in the .debug_ranges section. */
ab435259
DE
14878 unsigned long offset = (DW_UNSND (attr)
14879 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14880
2d5f09ec 14881 std::vector<blockrange> blockvec;
5f46c5a5
JK
14882 dwarf2_ranges_process (offset, cu,
14883 [&] (CORE_ADDR start, CORE_ADDR end)
14884 {
58fdfd2c
JK
14885 start += baseaddr;
14886 end += baseaddr;
5f46c5a5
JK
14887 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14888 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14889 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14890 blockvec.emplace_back (start, end);
5f46c5a5 14891 });
2d5f09ec
KB
14892
14893 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14894 }
14895}
14896
685b1105
JK
14897/* Check whether the producer field indicates either of GCC < 4.6, or the
14898 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14899
685b1105
JK
14900static void
14901check_producer (struct dwarf2_cu *cu)
60d5a603 14902{
38360086 14903 int major, minor;
60d5a603
JK
14904
14905 if (cu->producer == NULL)
14906 {
14907 /* For unknown compilers expect their behavior is DWARF version
14908 compliant.
14909
14910 GCC started to support .debug_types sections by -gdwarf-4 since
14911 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14912 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14913 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14914 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14915 }
b1ffba5a 14916 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14917 {
38360086
MW
14918 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14919 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14920 }
5230b05a 14921 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14922 {
14923 cu->producer_is_icc = true;
14924 cu->producer_is_icc_lt_14 = major < 14;
14925 }
c258c396
JD
14926 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14927 cu->producer_is_codewarrior = true;
685b1105
JK
14928 else
14929 {
14930 /* For other non-GCC compilers, expect their behavior is DWARF version
14931 compliant. */
60d5a603
JK
14932 }
14933
9068261f 14934 cu->checked_producer = true;
685b1105 14935}
ba919b58 14936
685b1105
JK
14937/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14938 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14939 during 4.6.0 experimental. */
14940
9068261f 14941static bool
685b1105
JK
14942producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14943{
14944 if (!cu->checked_producer)
14945 check_producer (cu);
14946
14947 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14948}
14949
c258c396
JD
14950
14951/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14952 with incorrect is_stmt attributes. */
14953
14954static bool
14955producer_is_codewarrior (struct dwarf2_cu *cu)
14956{
14957 if (!cu->checked_producer)
14958 check_producer (cu);
14959
14960 return cu->producer_is_codewarrior;
14961}
14962
405feb71 14963/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14964 DW_AT_accessibility. */
14965
14966static enum dwarf_access_attribute
14967dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14968{
14969 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14970 {
14971 /* The default DWARF 2 accessibility for members is public, the default
14972 accessibility for inheritance is private. */
14973
14974 if (die->tag != DW_TAG_inheritance)
14975 return DW_ACCESS_public;
14976 else
14977 return DW_ACCESS_private;
14978 }
14979 else
14980 {
14981 /* DWARF 3+ defines the default accessibility a different way. The same
14982 rules apply now for DW_TAG_inheritance as for the members and it only
14983 depends on the container kind. */
14984
14985 if (die->parent->tag == DW_TAG_class_type)
14986 return DW_ACCESS_private;
14987 else
14988 return DW_ACCESS_public;
14989 }
14990}
14991
74ac6d43
TT
14992/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14993 offset. If the attribute was not found return 0, otherwise return
14994 1. If it was found but could not properly be handled, set *OFFSET
14995 to 0. */
14996
14997static int
14998handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14999 LONGEST *offset)
15000{
15001 struct attribute *attr;
15002
15003 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15004 if (attr != NULL)
15005 {
15006 *offset = 0;
15007
15008 /* Note that we do not check for a section offset first here.
15009 This is because DW_AT_data_member_location is new in DWARF 4,
15010 so if we see it, we can assume that a constant form is really
15011 a constant and not a section offset. */
15012 if (attr_form_is_constant (attr))
15013 *offset = dwarf2_get_attr_constant_value (attr, 0);
15014 else if (attr_form_is_section_offset (attr))
15015 dwarf2_complex_location_expr_complaint ();
15016 else if (attr_form_is_block (attr))
15017 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15018 else
15019 dwarf2_complex_location_expr_complaint ();
15020
15021 return 1;
15022 }
15023
15024 return 0;
15025}
15026
c906108c
SS
15027/* Add an aggregate field to the field list. */
15028
15029static void
107d2387 15030dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15031 struct dwarf2_cu *cu)
6e70227d 15032{
518817b3 15033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15034 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15035 struct nextfield *new_field;
15036 struct attribute *attr;
15037 struct field *fp;
15d034d0 15038 const char *fieldname = "";
c906108c 15039
7d0ccb61
DJ
15040 if (die->tag == DW_TAG_inheritance)
15041 {
be2daae6
TT
15042 fip->baseclasses.emplace_back ();
15043 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15044 }
15045 else
15046 {
be2daae6
TT
15047 fip->fields.emplace_back ();
15048 new_field = &fip->fields.back ();
7d0ccb61 15049 }
be2daae6 15050
c906108c
SS
15051 fip->nfields++;
15052
e142c38c 15053 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15054 if (attr != nullptr)
c906108c 15055 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15056 else
15057 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15058 if (new_field->accessibility != DW_ACCESS_public)
15059 fip->non_public_fields = 1;
60d5a603 15060
e142c38c 15061 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15062 if (attr != nullptr)
c906108c 15063 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15064 else
15065 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15066
15067 fp = &new_field->field;
a9a9bd0f 15068
e142c38c 15069 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15070 {
74ac6d43
TT
15071 LONGEST offset;
15072
a9a9bd0f 15073 /* Data member other than a C++ static data member. */
6e70227d 15074
c906108c 15075 /* Get type of field. */
e7c27a73 15076 fp->type = die_type (die, cu);
c906108c 15077
d6a843b5 15078 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15079
c906108c 15080 /* Get bit size of field (zero if none). */
e142c38c 15081 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15082 if (attr != nullptr)
c906108c
SS
15083 {
15084 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15085 }
15086 else
15087 {
15088 FIELD_BITSIZE (*fp) = 0;
15089 }
15090
15091 /* Get bit offset of field. */
74ac6d43
TT
15092 if (handle_data_member_location (die, cu, &offset))
15093 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15094 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15095 if (attr != nullptr)
c906108c 15096 {
d5a22e77 15097 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15098 {
15099 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15100 additional bit offset from the MSB of the containing
15101 anonymous object to the MSB of the field. We don't
15102 have to do anything special since we don't need to
15103 know the size of the anonymous object. */
f41f5e61 15104 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15105 }
15106 else
15107 {
15108 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15109 MSB of the anonymous object, subtract off the number of
15110 bits from the MSB of the field to the MSB of the
15111 object, and then subtract off the number of bits of
15112 the field itself. The result is the bit offset of
15113 the LSB of the field. */
c906108c
SS
15114 int anonymous_size;
15115 int bit_offset = DW_UNSND (attr);
15116
e142c38c 15117 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15118 if (attr != nullptr)
c906108c
SS
15119 {
15120 /* The size of the anonymous object containing
15121 the bit field is explicit, so use the
15122 indicated size (in bytes). */
15123 anonymous_size = DW_UNSND (attr);
15124 }
15125 else
15126 {
15127 /* The size of the anonymous object containing
15128 the bit field must be inferred from the type
15129 attribute of the data member containing the
15130 bit field. */
15131 anonymous_size = TYPE_LENGTH (fp->type);
15132 }
f41f5e61
PA
15133 SET_FIELD_BITPOS (*fp,
15134 (FIELD_BITPOS (*fp)
15135 + anonymous_size * bits_per_byte
15136 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15137 }
15138 }
da5b30da
AA
15139 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15140 if (attr != NULL)
15141 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15142 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15143
15144 /* Get name of field. */
39cbfefa
DJ
15145 fieldname = dwarf2_name (die, cu);
15146 if (fieldname == NULL)
15147 fieldname = "";
d8151005
DJ
15148
15149 /* The name is already allocated along with this objfile, so we don't
15150 need to duplicate it for the type. */
15151 fp->name = fieldname;
c906108c
SS
15152
15153 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15154 pointer or virtual base class pointer) to private. */
e142c38c 15155 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15156 {
d48cc9dd 15157 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15158 new_field->accessibility = DW_ACCESS_private;
15159 fip->non_public_fields = 1;
15160 }
15161 }
a9a9bd0f 15162 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15163 {
a9a9bd0f
DC
15164 /* C++ static member. */
15165
15166 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15167 is a declaration, but all versions of G++ as of this writing
15168 (so through at least 3.2.1) incorrectly generate
15169 DW_TAG_variable tags. */
6e70227d 15170
ff355380 15171 const char *physname;
c906108c 15172
a9a9bd0f 15173 /* Get name of field. */
39cbfefa
DJ
15174 fieldname = dwarf2_name (die, cu);
15175 if (fieldname == NULL)
c906108c
SS
15176 return;
15177
254e6b9e 15178 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15179 if (attr
15180 /* Only create a symbol if this is an external value.
15181 new_symbol checks this and puts the value in the global symbol
15182 table, which we want. If it is not external, new_symbol
15183 will try to put the value in cu->list_in_scope which is wrong. */
15184 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15185 {
15186 /* A static const member, not much different than an enum as far as
15187 we're concerned, except that we can support more types. */
15188 new_symbol (die, NULL, cu);
15189 }
15190
2df3850c 15191 /* Get physical name. */
ff355380 15192 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15193
d8151005
DJ
15194 /* The name is already allocated along with this objfile, so we don't
15195 need to duplicate it for the type. */
15196 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15197 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15198 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15199 }
15200 else if (die->tag == DW_TAG_inheritance)
15201 {
74ac6d43 15202 LONGEST offset;
d4b96c9a 15203
74ac6d43
TT
15204 /* C++ base class field. */
15205 if (handle_data_member_location (die, cu, &offset))
15206 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15207 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15208 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15209 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15210 }
2ddeaf8a
TT
15211 else if (die->tag == DW_TAG_variant_part)
15212 {
15213 /* process_structure_scope will treat this DIE as a union. */
15214 process_structure_scope (die, cu);
15215
15216 /* The variant part is relative to the start of the enclosing
15217 structure. */
15218 SET_FIELD_BITPOS (*fp, 0);
15219 fp->type = get_die_type (die, cu);
15220 fp->artificial = 1;
15221 fp->name = "<<variant>>";
c8c81635
TT
15222
15223 /* Normally a DW_TAG_variant_part won't have a size, but our
15224 representation requires one, so set it to the maximum of the
489dbda6
TT
15225 child sizes, being sure to account for the offset at which
15226 each child is seen. */
c8c81635
TT
15227 if (TYPE_LENGTH (fp->type) == 0)
15228 {
15229 unsigned max = 0;
15230 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
15231 {
15232 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15233 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15234 if (len > max)
15235 max = len;
15236 }
c8c81635
TT
15237 TYPE_LENGTH (fp->type) = max;
15238 }
2ddeaf8a
TT
15239 }
15240 else
15241 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15242}
15243
883fd55a
KS
15244/* Can the type given by DIE define another type? */
15245
15246static bool
15247type_can_define_types (const struct die_info *die)
15248{
15249 switch (die->tag)
15250 {
15251 case DW_TAG_typedef:
15252 case DW_TAG_class_type:
15253 case DW_TAG_structure_type:
15254 case DW_TAG_union_type:
15255 case DW_TAG_enumeration_type:
15256 return true;
15257
15258 default:
15259 return false;
15260 }
15261}
15262
15263/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15264
15265static void
883fd55a
KS
15266dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15267 struct dwarf2_cu *cu)
6e70227d 15268{
be2daae6
TT
15269 struct decl_field fp;
15270 memset (&fp, 0, sizeof (fp));
98751a41 15271
883fd55a 15272 gdb_assert (type_can_define_types (die));
98751a41 15273
883fd55a 15274 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15275 fp.name = dwarf2_name (die, cu);
15276 fp.type = read_type_die (die, cu);
98751a41 15277
c191a687
KS
15278 /* Save accessibility. */
15279 enum dwarf_access_attribute accessibility;
15280 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15281 if (attr != NULL)
15282 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15283 else
15284 accessibility = dwarf2_default_access_attribute (die, cu);
15285 switch (accessibility)
15286 {
15287 case DW_ACCESS_public:
15288 /* The assumed value if neither private nor protected. */
15289 break;
15290 case DW_ACCESS_private:
be2daae6 15291 fp.is_private = 1;
c191a687
KS
15292 break;
15293 case DW_ACCESS_protected:
be2daae6 15294 fp.is_protected = 1;
c191a687
KS
15295 break;
15296 default:
b98664d3 15297 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15298 }
15299
883fd55a 15300 if (die->tag == DW_TAG_typedef)
be2daae6 15301 fip->typedef_field_list.push_back (fp);
883fd55a 15302 else
be2daae6 15303 fip->nested_types_list.push_back (fp);
98751a41
JK
15304}
15305
c906108c
SS
15306/* Create the vector of fields, and attach it to the type. */
15307
15308static void
fba45db2 15309dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15310 struct dwarf2_cu *cu)
c906108c
SS
15311{
15312 int nfields = fip->nfields;
15313
15314 /* Record the field count, allocate space for the array of fields,
15315 and create blank accessibility bitfields if necessary. */
15316 TYPE_NFIELDS (type) = nfields;
15317 TYPE_FIELDS (type) = (struct field *)
be2daae6 15318 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15319
b4ba55a1 15320 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15321 {
15322 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15323
15324 TYPE_FIELD_PRIVATE_BITS (type) =
15325 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15326 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15327
15328 TYPE_FIELD_PROTECTED_BITS (type) =
15329 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15330 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15331
774b6a14
TT
15332 TYPE_FIELD_IGNORE_BITS (type) =
15333 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15334 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15335 }
15336
15337 /* If the type has baseclasses, allocate and clear a bit vector for
15338 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15339 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15340 {
be2daae6 15341 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15342 unsigned char *pointer;
c906108c
SS
15343
15344 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15345 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15346 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15347 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15348 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15349 }
15350
2ddeaf8a
TT
15351 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15352 {
15353 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15354
be2daae6 15355 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15356 {
be2daae6
TT
15357 struct nextfield &field = fip->fields[index];
15358
15359 if (field.variant.is_discriminant)
2ddeaf8a 15360 di->discriminant_index = index;
be2daae6 15361 else if (field.variant.default_branch)
2ddeaf8a
TT
15362 di->default_index = index;
15363 else
be2daae6 15364 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15365 }
15366 }
15367
be2daae6
TT
15368 /* Copy the saved-up fields into the field vector. */
15369 for (int i = 0; i < nfields; ++i)
c906108c 15370 {
be2daae6
TT
15371 struct nextfield &field
15372 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15373 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15374
be2daae6
TT
15375 TYPE_FIELD (type, i) = field.field;
15376 switch (field.accessibility)
c906108c 15377 {
c5aa993b 15378 case DW_ACCESS_private:
b4ba55a1 15379 if (cu->language != language_ada)
be2daae6 15380 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15381 break;
c906108c 15382
c5aa993b 15383 case DW_ACCESS_protected:
b4ba55a1 15384 if (cu->language != language_ada)
be2daae6 15385 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15386 break;
c906108c 15387
c5aa993b
JM
15388 case DW_ACCESS_public:
15389 break;
c906108c 15390
c5aa993b
JM
15391 default:
15392 /* Unknown accessibility. Complain and treat it as public. */
15393 {
b98664d3 15394 complaint (_("unsupported accessibility %d"),
be2daae6 15395 field.accessibility);
c5aa993b
JM
15396 }
15397 break;
c906108c 15398 }
be2daae6 15399 if (i < fip->baseclasses.size ())
c906108c 15400 {
be2daae6 15401 switch (field.virtuality)
c906108c 15402 {
c5aa993b
JM
15403 case DW_VIRTUALITY_virtual:
15404 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15405 if (cu->language == language_ada)
a73c6dcd 15406 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15407 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15408 break;
c906108c
SS
15409 }
15410 }
c906108c
SS
15411 }
15412}
15413
7d27a96d
TT
15414/* Return true if this member function is a constructor, false
15415 otherwise. */
15416
15417static int
15418dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15419{
15420 const char *fieldname;
fe978cb0 15421 const char *type_name;
7d27a96d
TT
15422 int len;
15423
15424 if (die->parent == NULL)
15425 return 0;
15426
15427 if (die->parent->tag != DW_TAG_structure_type
15428 && die->parent->tag != DW_TAG_union_type
15429 && die->parent->tag != DW_TAG_class_type)
15430 return 0;
15431
15432 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15433 type_name = dwarf2_name (die->parent, cu);
15434 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15435 return 0;
15436
15437 len = strlen (fieldname);
fe978cb0
PA
15438 return (strncmp (fieldname, type_name, len) == 0
15439 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15440}
15441
e35000a7
TBA
15442/* Check if the given VALUE is a recognized enum
15443 dwarf_defaulted_attribute constant according to DWARF5 spec,
15444 Table 7.24. */
15445
15446static bool
15447is_valid_DW_AT_defaulted (ULONGEST value)
15448{
15449 switch (value)
15450 {
15451 case DW_DEFAULTED_no:
15452 case DW_DEFAULTED_in_class:
15453 case DW_DEFAULTED_out_of_class:
15454 return true;
15455 }
15456
3142e908 15457 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15458 return false;
15459}
15460
c906108c
SS
15461/* Add a member function to the proper fieldlist. */
15462
15463static void
107d2387 15464dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15465 struct type *type, struct dwarf2_cu *cu)
c906108c 15466{
518817b3 15467 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15468 struct attribute *attr;
c906108c 15469 int i;
be2daae6 15470 struct fnfieldlist *flp = nullptr;
c906108c 15471 struct fn_field *fnp;
15d034d0 15472 const char *fieldname;
f792889a 15473 struct type *this_type;
60d5a603 15474 enum dwarf_access_attribute accessibility;
c906108c 15475
b4ba55a1 15476 if (cu->language == language_ada)
a73c6dcd 15477 error (_("unexpected member function in Ada type"));
b4ba55a1 15478
2df3850c 15479 /* Get name of member function. */
39cbfefa
DJ
15480 fieldname = dwarf2_name (die, cu);
15481 if (fieldname == NULL)
2df3850c 15482 return;
c906108c 15483
c906108c 15484 /* Look up member function name in fieldlist. */
be2daae6 15485 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15486 {
27bfe10e 15487 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15488 {
15489 flp = &fip->fnfieldlists[i];
15490 break;
15491 }
c906108c
SS
15492 }
15493
be2daae6
TT
15494 /* Create a new fnfieldlist if necessary. */
15495 if (flp == nullptr)
c906108c 15496 {
be2daae6
TT
15497 fip->fnfieldlists.emplace_back ();
15498 flp = &fip->fnfieldlists.back ();
c906108c 15499 flp->name = fieldname;
be2daae6 15500 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15501 }
15502
be2daae6
TT
15503 /* Create a new member function field and add it to the vector of
15504 fnfieldlists. */
15505 flp->fnfields.emplace_back ();
15506 fnp = &flp->fnfields.back ();
3da10d80
KS
15507
15508 /* Delay processing of the physname until later. */
9c37b5ae 15509 if (cu->language == language_cplus)
be2daae6
TT
15510 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15511 die, cu);
3da10d80
KS
15512 else
15513 {
1d06ead6 15514 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15515 fnp->physname = physname ? physname : "";
15516 }
15517
c906108c 15518 fnp->type = alloc_type (objfile);
f792889a
DJ
15519 this_type = read_type_die (die, cu);
15520 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15521 {
f792889a 15522 int nparams = TYPE_NFIELDS (this_type);
c906108c 15523
f792889a 15524 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15525 of the method itself (TYPE_CODE_METHOD). */
15526 smash_to_method_type (fnp->type, type,
f792889a
DJ
15527 TYPE_TARGET_TYPE (this_type),
15528 TYPE_FIELDS (this_type),
15529 TYPE_NFIELDS (this_type),
15530 TYPE_VARARGS (this_type));
c906108c
SS
15531
15532 /* Handle static member functions.
c5aa993b 15533 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15534 member functions. G++ helps GDB by marking the first
15535 parameter for non-static member functions (which is the this
15536 pointer) as artificial. We obtain this information from
15537 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15538 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15539 fnp->voffset = VOFFSET_STATIC;
15540 }
15541 else
b98664d3 15542 complaint (_("member function type missing for '%s'"),
3da10d80 15543 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15544
15545 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15546 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15547 fnp->fcontext = die_containing_type (die, cu);
c906108c 15548
3e43a32a
MS
15549 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15550 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15551
15552 /* Get accessibility. */
e142c38c 15553 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15554 if (attr != nullptr)
aead7601 15555 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15556 else
15557 accessibility = dwarf2_default_access_attribute (die, cu);
15558 switch (accessibility)
c906108c 15559 {
60d5a603
JK
15560 case DW_ACCESS_private:
15561 fnp->is_private = 1;
15562 break;
15563 case DW_ACCESS_protected:
15564 fnp->is_protected = 1;
15565 break;
c906108c
SS
15566 }
15567
b02dede2 15568 /* Check for artificial methods. */
e142c38c 15569 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15570 if (attr && DW_UNSND (attr) != 0)
15571 fnp->is_artificial = 1;
15572
e35000a7
TBA
15573 /* Check for defaulted methods. */
15574 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15575 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15576 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15577
15578 /* Check for deleted methods. */
15579 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15580 if (attr != nullptr && DW_UNSND (attr) != 0)
15581 fnp->is_deleted = 1;
15582
7d27a96d
TT
15583 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15584
0d564a31 15585 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15586 function. For older versions of GCC, this is an offset in the
15587 appropriate virtual table, as specified by DW_AT_containing_type.
15588 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15589 to the object address. */
15590
e142c38c 15591 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15592 if (attr != nullptr)
8e19ed76 15593 {
aec5aa8b 15594 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15595 {
aec5aa8b
TT
15596 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15597 {
15598 /* Old-style GCC. */
15599 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15600 }
15601 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15602 || (DW_BLOCK (attr)->size > 1
15603 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15604 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15605 {
aec5aa8b
TT
15606 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15607 if ((fnp->voffset % cu->header.addr_size) != 0)
15608 dwarf2_complex_location_expr_complaint ();
15609 else
15610 fnp->voffset /= cu->header.addr_size;
15611 fnp->voffset += 2;
15612 }
15613 else
15614 dwarf2_complex_location_expr_complaint ();
15615
15616 if (!fnp->fcontext)
7e993ebf
KS
15617 {
15618 /* If there is no `this' field and no DW_AT_containing_type,
15619 we cannot actually find a base class context for the
15620 vtable! */
15621 if (TYPE_NFIELDS (this_type) == 0
15622 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15623 {
b98664d3 15624 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15625 "function \"%s\" (offset %s)"),
15626 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15627 }
15628 else
15629 {
15630 fnp->fcontext
15631 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15632 }
15633 }
aec5aa8b 15634 }
3690dd37 15635 else if (attr_form_is_section_offset (attr))
8e19ed76 15636 {
4d3c2250 15637 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15638 }
15639 else
15640 {
4d3c2250
KB
15641 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15642 fieldname);
8e19ed76 15643 }
0d564a31 15644 }
d48cc9dd
DJ
15645 else
15646 {
15647 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15648 if (attr && DW_UNSND (attr))
15649 {
15650 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15651 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15652 "but the vtable offset is not specified"),
9d8780f0 15653 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15654 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15655 TYPE_CPLUS_DYNAMIC (type) = 1;
15656 }
15657 }
c906108c
SS
15658}
15659
15660/* Create the vector of member function fields, and attach it to the type. */
15661
15662static void
fba45db2 15663dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15664 struct dwarf2_cu *cu)
c906108c 15665{
b4ba55a1 15666 if (cu->language == language_ada)
a73c6dcd 15667 error (_("unexpected member functions in Ada type"));
b4ba55a1 15668
c906108c
SS
15669 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15670 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15671 TYPE_ALLOC (type,
15672 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15673
be2daae6 15674 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15675 {
be2daae6 15676 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15677 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15678
be2daae6
TT
15679 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15680 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15681 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15682 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15683
15684 for (int k = 0; k < nf.fnfields.size (); ++k)
15685 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15686 }
15687
be2daae6 15688 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15689}
15690
1168df01
JB
15691/* Returns non-zero if NAME is the name of a vtable member in CU's
15692 language, zero otherwise. */
15693static int
15694is_vtable_name (const char *name, struct dwarf2_cu *cu)
15695{
15696 static const char vptr[] = "_vptr";
15697
9c37b5ae
TT
15698 /* Look for the C++ form of the vtable. */
15699 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15700 return 1;
15701
15702 return 0;
15703}
15704
c0dd20ea 15705/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15706 functions, with the ABI-specified layout. If TYPE describes
15707 such a structure, smash it into a member function type.
61049d3b
DJ
15708
15709 GCC shouldn't do this; it should just output pointer to member DIEs.
15710 This is GCC PR debug/28767. */
c0dd20ea 15711
0b92b5bb
TT
15712static void
15713quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15714{
09e2d7c7 15715 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15716
15717 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15718 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15719 return;
c0dd20ea
DJ
15720
15721 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15722 if (TYPE_FIELD_NAME (type, 0) == NULL
15723 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15724 || TYPE_FIELD_NAME (type, 1) == NULL
15725 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15726 return;
c0dd20ea
DJ
15727
15728 /* Find the type of the method. */
0b92b5bb 15729 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15730 if (pfn_type == NULL
15731 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15732 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15733 return;
c0dd20ea
DJ
15734
15735 /* Look for the "this" argument. */
15736 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15737 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15738 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15739 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15740 return;
c0dd20ea 15741
09e2d7c7 15742 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15743 new_type = alloc_type (objfile);
09e2d7c7 15744 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15745 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15746 TYPE_VARARGS (pfn_type));
0b92b5bb 15747 smash_to_methodptr_type (type, new_type);
c0dd20ea 15748}
1168df01 15749
2b4424c3
TT
15750/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15751 appropriate error checking and issuing complaints if there is a
15752 problem. */
15753
15754static ULONGEST
15755get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15756{
15757 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15758
15759 if (attr == nullptr)
15760 return 0;
15761
15762 if (!attr_form_is_constant (attr))
15763 {
b98664d3 15764 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15765 " - DIE at %s [in module %s]"),
15766 sect_offset_str (die->sect_off),
15767 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15768 return 0;
15769 }
15770
15771 ULONGEST align;
15772 if (attr->form == DW_FORM_sdata)
15773 {
15774 LONGEST val = DW_SND (attr);
15775 if (val < 0)
15776 {
b98664d3 15777 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15778 " - DIE at %s [in module %s]"),
15779 sect_offset_str (die->sect_off),
15780 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15781 return 0;
15782 }
15783 align = val;
15784 }
15785 else
15786 align = DW_UNSND (attr);
15787
15788 if (align == 0)
15789 {
b98664d3 15790 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15791 " - DIE at %s [in module %s]"),
15792 sect_offset_str (die->sect_off),
15793 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15794 return 0;
15795 }
15796 if ((align & (align - 1)) != 0)
15797 {
b98664d3 15798 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15799 " - DIE at %s [in module %s]"),
15800 sect_offset_str (die->sect_off),
15801 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15802 return 0;
15803 }
15804
15805 return align;
15806}
15807
15808/* If the DIE has a DW_AT_alignment attribute, use its value to set
15809 the alignment for TYPE. */
15810
15811static void
15812maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15813 struct type *type)
15814{
15815 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15816 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15817 " - DIE at %s [in module %s]"),
15818 sect_offset_str (die->sect_off),
15819 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15820}
685b1105 15821
e35000a7
TBA
15822/* Check if the given VALUE is a valid enum dwarf_calling_convention
15823 constant for a type, according to DWARF5 spec, Table 5.5. */
15824
15825static bool
15826is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15827{
15828 switch (value)
15829 {
15830 case DW_CC_normal:
15831 case DW_CC_pass_by_reference:
15832 case DW_CC_pass_by_value:
15833 return true;
15834
15835 default:
15836 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15837 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15838 return false;
15839 }
15840}
15841
d0922fcf
TBA
15842/* Check if the given VALUE is a valid enum dwarf_calling_convention
15843 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15844 also according to GNU-specific values (see include/dwarf2.h). */
15845
15846static bool
15847is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15848{
15849 switch (value)
15850 {
15851 case DW_CC_normal:
15852 case DW_CC_program:
15853 case DW_CC_nocall:
15854 return true;
15855
15856 case DW_CC_GNU_renesas_sh:
15857 case DW_CC_GNU_borland_fastcall_i386:
15858 case DW_CC_GDB_IBM_OpenCL:
15859 return true;
15860
15861 default:
15862 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15863 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15864 return false;
15865 }
15866}
15867
c906108c 15868/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15869 (definition) to create a type for the structure or union. Fill in
15870 the type's name and general properties; the members will not be
83655187
DE
15871 processed until process_structure_scope. A symbol table entry for
15872 the type will also not be done until process_structure_scope (assuming
15873 the type has a name).
c906108c 15874
c767944b
DJ
15875 NOTE: we need to call these functions regardless of whether or not the
15876 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15877 structure or union. This gets the type entered into our set of
83655187 15878 user defined types. */
c906108c 15879
f792889a 15880static struct type *
134d01f1 15881read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15882{
518817b3 15883 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15884 struct type *type;
15885 struct attribute *attr;
15d034d0 15886 const char *name;
c906108c 15887
348e048f
DE
15888 /* If the definition of this type lives in .debug_types, read that type.
15889 Don't follow DW_AT_specification though, that will take us back up
15890 the chain and we want to go down. */
45e58e77 15891 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15892 if (attr != nullptr)
348e048f 15893 {
ac9ec31b 15894 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15895
ac9ec31b 15896 /* The type's CU may not be the same as CU.
02142a6c 15897 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15898 return set_die_type (die, type, cu);
15899 }
15900
c0dd20ea 15901 type = alloc_type (objfile);
c906108c 15902 INIT_CPLUS_SPECIFIC (type);
93311388 15903
39cbfefa
DJ
15904 name = dwarf2_name (die, cu);
15905 if (name != NULL)
c906108c 15906 {
987504bb 15907 if (cu->language == language_cplus
c44af4eb
TT
15908 || cu->language == language_d
15909 || cu->language == language_rust)
63d06c5c 15910 {
15d034d0 15911 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15912
15913 /* dwarf2_full_name might have already finished building the DIE's
15914 type. If so, there is no need to continue. */
15915 if (get_die_type (die, cu) != NULL)
15916 return get_die_type (die, cu);
15917
e86ca25f 15918 TYPE_NAME (type) = full_name;
63d06c5c
DC
15919 }
15920 else
15921 {
d8151005
DJ
15922 /* The name is already allocated along with this objfile, so
15923 we don't need to duplicate it for the type. */
e86ca25f 15924 TYPE_NAME (type) = name;
63d06c5c 15925 }
c906108c
SS
15926 }
15927
15928 if (die->tag == DW_TAG_structure_type)
15929 {
15930 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15931 }
15932 else if (die->tag == DW_TAG_union_type)
15933 {
15934 TYPE_CODE (type) = TYPE_CODE_UNION;
15935 }
2ddeaf8a
TT
15936 else if (die->tag == DW_TAG_variant_part)
15937 {
15938 TYPE_CODE (type) = TYPE_CODE_UNION;
15939 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15940 }
c906108c
SS
15941 else
15942 {
4753d33b 15943 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15944 }
15945
0cc2414c
TT
15946 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15947 TYPE_DECLARED_CLASS (type) = 1;
15948
e35000a7
TBA
15949 /* Store the calling convention in the type if it's available in
15950 the die. Otherwise the calling convention remains set to
15951 the default value DW_CC_normal. */
15952 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15953 if (attr != nullptr
15954 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15955 {
15956 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15957 TYPE_CPLUS_CALLING_CONVENTION (type)
15958 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15959 }
15960
e142c38c 15961 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15962 if (attr != nullptr)
c906108c 15963 {
155bfbd3
JB
15964 if (attr_form_is_constant (attr))
15965 TYPE_LENGTH (type) = DW_UNSND (attr);
15966 else
15967 {
15968 /* For the moment, dynamic type sizes are not supported
15969 by GDB's struct type. The actual size is determined
15970 on-demand when resolving the type of a given object,
15971 so set the type's length to zero for now. Otherwise,
15972 we record an expression as the length, and that expression
15973 could lead to a very large value, which could eventually
15974 lead to us trying to allocate that much memory when creating
15975 a value of that type. */
15976 TYPE_LENGTH (type) = 0;
15977 }
c906108c
SS
15978 }
15979 else
15980 {
15981 TYPE_LENGTH (type) = 0;
15982 }
15983
2b4424c3
TT
15984 maybe_set_alignment (cu, die, type);
15985
5230b05a 15986 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15987 {
5230b05a
WT
15988 /* ICC<14 does not output the required DW_AT_declaration on
15989 incomplete types, but gives them a size of zero. */
422b1cb0 15990 TYPE_STUB (type) = 1;
685b1105
JK
15991 }
15992 else
15993 TYPE_STUB_SUPPORTED (type) = 1;
15994
dc718098 15995 if (die_is_declaration (die, cu))
876cecd0 15996 TYPE_STUB (type) = 1;
a6c727b2
DJ
15997 else if (attr == NULL && die->child == NULL
15998 && producer_is_realview (cu->producer))
15999 /* RealView does not output the required DW_AT_declaration
16000 on incomplete types. */
16001 TYPE_STUB (type) = 1;
dc718098 16002
c906108c
SS
16003 /* We need to add the type field to the die immediately so we don't
16004 infinitely recurse when dealing with pointers to the structure
0963b4bd 16005 type within the structure itself. */
1c379e20 16006 set_die_type (die, type, cu);
c906108c 16007
7e314c57
JK
16008 /* set_die_type should be already done. */
16009 set_descriptive_type (type, die, cu);
16010
c767944b
DJ
16011 return type;
16012}
16013
2ddeaf8a
TT
16014/* A helper for process_structure_scope that handles a single member
16015 DIE. */
16016
16017static void
16018handle_struct_member_die (struct die_info *child_die, struct type *type,
16019 struct field_info *fi,
16020 std::vector<struct symbol *> *template_args,
16021 struct dwarf2_cu *cu)
16022{
16023 if (child_die->tag == DW_TAG_member
16024 || child_die->tag == DW_TAG_variable
16025 || child_die->tag == DW_TAG_variant_part)
16026 {
16027 /* NOTE: carlton/2002-11-05: A C++ static data member
16028 should be a DW_TAG_member that is a declaration, but
16029 all versions of G++ as of this writing (so through at
16030 least 3.2.1) incorrectly generate DW_TAG_variable
16031 tags for them instead. */
16032 dwarf2_add_field (fi, child_die, cu);
16033 }
16034 else if (child_die->tag == DW_TAG_subprogram)
16035 {
16036 /* Rust doesn't have member functions in the C++ sense.
16037 However, it does emit ordinary functions as children
16038 of a struct DIE. */
16039 if (cu->language == language_rust)
16040 read_func_scope (child_die, cu);
16041 else
16042 {
16043 /* C++ member function. */
16044 dwarf2_add_member_fn (fi, child_die, type, cu);
16045 }
16046 }
16047 else if (child_die->tag == DW_TAG_inheritance)
16048 {
16049 /* C++ base class field. */
16050 dwarf2_add_field (fi, child_die, cu);
16051 }
16052 else if (type_can_define_types (child_die))
16053 dwarf2_add_type_defn (fi, child_die, cu);
16054 else if (child_die->tag == DW_TAG_template_type_param
16055 || child_die->tag == DW_TAG_template_value_param)
16056 {
16057 struct symbol *arg = new_symbol (child_die, NULL, cu);
16058
16059 if (arg != NULL)
16060 template_args->push_back (arg);
16061 }
16062 else if (child_die->tag == DW_TAG_variant)
16063 {
16064 /* In a variant we want to get the discriminant and also add a
16065 field for our sole member child. */
16066 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16067
bde09ab7 16068 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
16069 variant_child != NULL;
16070 variant_child = sibling_die (variant_child))
16071 {
16072 if (variant_child->tag == DW_TAG_member)
16073 {
16074 handle_struct_member_die (variant_child, type, fi,
16075 template_args, cu);
16076 /* Only handle the one. */
16077 break;
16078 }
16079 }
16080
16081 /* We don't handle this but we might as well report it if we see
16082 it. */
16083 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 16084 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
16085 " - DIE at %s [in module %s]"),
16086 sect_offset_str (child_die->sect_off),
16087 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16088
16089 /* The first field was just added, so we can stash the
16090 discriminant there. */
be2daae6 16091 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16092 if (discr == NULL)
be2daae6 16093 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16094 else
be2daae6 16095 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16096 }
16097}
16098
c767944b
DJ
16099/* Finish creating a structure or union type, including filling in
16100 its members and creating a symbol for it. */
16101
16102static void
16103process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16104{
518817b3 16105 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16106 struct die_info *child_die;
c767944b
DJ
16107 struct type *type;
16108
16109 type = get_die_type (die, cu);
16110 if (type == NULL)
16111 type = read_structure_type (die, cu);
16112
2ddeaf8a
TT
16113 /* When reading a DW_TAG_variant_part, we need to notice when we
16114 read the discriminant member, so we can record it later in the
16115 discriminant_info. */
16116 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 16117 sect_offset discr_offset {};
3e1d3d8c 16118 bool has_template_parameters = false;
2ddeaf8a
TT
16119
16120 if (is_variant_part)
16121 {
16122 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16123 if (discr == NULL)
16124 {
16125 /* Maybe it's a univariant form, an extension we support.
16126 In this case arrange not to check the offset. */
16127 is_variant_part = false;
16128 }
16129 else if (attr_form_is_ref (discr))
16130 {
16131 struct dwarf2_cu *target_cu = cu;
16132 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16133
16134 discr_offset = target_die->sect_off;
16135 }
16136 else
16137 {
b98664d3 16138 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16139 " - DIE at %s [in module %s]"),
16140 sect_offset_str (die->sect_off),
16141 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16142 is_variant_part = false;
16143 }
16144 }
16145
e142c38c 16146 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16147 {
16148 struct field_info fi;
2f4732b0 16149 std::vector<struct symbol *> template_args;
c906108c 16150
639d11d3 16151 child_die = die->child;
c906108c
SS
16152
16153 while (child_die && child_die->tag)
16154 {
2ddeaf8a 16155 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16156
2ddeaf8a 16157 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16158 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16159
c906108c
SS
16160 child_die = sibling_die (child_die);
16161 }
16162
34eaf542 16163 /* Attach template arguments to type. */
2f4732b0 16164 if (!template_args.empty ())
34eaf542 16165 {
3e1d3d8c 16166 has_template_parameters = true;
34eaf542 16167 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16168 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16169 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16170 = XOBNEWVEC (&objfile->objfile_obstack,
16171 struct symbol *,
16172 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16173 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16174 template_args.data (),
34eaf542
TT
16175 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16176 * sizeof (struct symbol *)));
34eaf542
TT
16177 }
16178
c906108c
SS
16179 /* Attach fields and member functions to the type. */
16180 if (fi.nfields)
e7c27a73 16181 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16182 if (!fi.fnfieldlists.empty ())
c906108c 16183 {
e7c27a73 16184 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16185
c5aa993b 16186 /* Get the type which refers to the base class (possibly this
c906108c 16187 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16188 class from the DW_AT_containing_type attribute. This use of
16189 DW_AT_containing_type is a GNU extension. */
c906108c 16190
e142c38c 16191 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16192 {
e7c27a73 16193 struct type *t = die_containing_type (die, cu);
c906108c 16194
ae6ae975 16195 set_type_vptr_basetype (type, t);
c906108c
SS
16196 if (type == t)
16197 {
c906108c
SS
16198 int i;
16199
16200 /* Our own class provides vtbl ptr. */
16201 for (i = TYPE_NFIELDS (t) - 1;
16202 i >= TYPE_N_BASECLASSES (t);
16203 --i)
16204 {
0d5cff50 16205 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16206
1168df01 16207 if (is_vtable_name (fieldname, cu))
c906108c 16208 {
ae6ae975 16209 set_type_vptr_fieldno (type, i);
c906108c
SS
16210 break;
16211 }
16212 }
16213
16214 /* Complain if virtual function table field not found. */
16215 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16216 complaint (_("virtual function table pointer "
3e43a32a 16217 "not found when defining class '%s'"),
e86ca25f 16218 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16219 }
16220 else
16221 {
ae6ae975 16222 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16223 }
16224 }
f6235d4c 16225 else if (cu->producer
61012eef 16226 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16227 {
16228 /* The IBM XLC compiler does not provide direct indication
16229 of the containing type, but the vtable pointer is
16230 always named __vfp. */
16231
16232 int i;
16233
16234 for (i = TYPE_NFIELDS (type) - 1;
16235 i >= TYPE_N_BASECLASSES (type);
16236 --i)
16237 {
16238 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16239 {
ae6ae975
DE
16240 set_type_vptr_fieldno (type, i);
16241 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16242 break;
16243 }
16244 }
16245 }
c906108c 16246 }
98751a41
JK
16247
16248 /* Copy fi.typedef_field_list linked list elements content into the
16249 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16250 if (!fi.typedef_field_list.empty ())
98751a41 16251 {
be2daae6 16252 int count = fi.typedef_field_list.size ();
98751a41 16253
a0d7a4ff 16254 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16255 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16256 = ((struct decl_field *)
be2daae6
TT
16257 TYPE_ALLOC (type,
16258 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16259 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16260
be2daae6
TT
16261 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16262 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16263 }
c767944b 16264
883fd55a
KS
16265 /* Copy fi.nested_types_list linked list elements content into the
16266 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16267 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16268 {
be2daae6 16269 int count = fi.nested_types_list.size ();
883fd55a
KS
16270
16271 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16272 TYPE_NESTED_TYPES_ARRAY (type)
16273 = ((struct decl_field *)
be2daae6
TT
16274 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16275 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16276
be2daae6
TT
16277 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16278 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16279 }
c906108c 16280 }
63d06c5c 16281
bb5ed363 16282 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16283 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16284 cu->rust_unions.push_back (type);
0b92b5bb 16285
90aeadfc
DC
16286 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16287 snapshots) has been known to create a die giving a declaration
16288 for a class that has, as a child, a die giving a definition for a
16289 nested class. So we have to process our children even if the
16290 current die is a declaration. Normally, of course, a declaration
16291 won't have any children at all. */
134d01f1 16292
ca040673
DE
16293 child_die = die->child;
16294
90aeadfc
DC
16295 while (child_die != NULL && child_die->tag)
16296 {
16297 if (child_die->tag == DW_TAG_member
16298 || child_die->tag == DW_TAG_variable
34eaf542
TT
16299 || child_die->tag == DW_TAG_inheritance
16300 || child_die->tag == DW_TAG_template_value_param
16301 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16302 {
90aeadfc 16303 /* Do nothing. */
134d01f1 16304 }
90aeadfc
DC
16305 else
16306 process_die (child_die, cu);
134d01f1 16307
90aeadfc 16308 child_die = sibling_die (child_die);
134d01f1
DJ
16309 }
16310
fa4028e9
JB
16311 /* Do not consider external references. According to the DWARF standard,
16312 these DIEs are identified by the fact that they have no byte_size
16313 attribute, and a declaration attribute. */
16314 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16315 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16316 {
16317 struct symbol *sym = new_symbol (die, type, cu);
16318
16319 if (has_template_parameters)
16320 {
a776957c
TT
16321 struct symtab *symtab;
16322 if (sym != nullptr)
16323 symtab = symbol_symtab (sym);
16324 else if (cu->line_header != nullptr)
16325 {
16326 /* Any related symtab will do. */
16327 symtab
7ba99d21 16328 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16329 }
16330 else
16331 {
16332 symtab = nullptr;
16333 complaint (_("could not find suitable "
16334 "symtab for template parameter"
16335 " - DIE at %s [in module %s]"),
16336 sect_offset_str (die->sect_off),
16337 objfile_name (objfile));
16338 }
16339
16340 if (symtab != nullptr)
16341 {
16342 /* Make sure that the symtab is set on the new symbols.
16343 Even though they don't appear in this symtab directly,
16344 other parts of gdb assume that symbols do, and this is
16345 reasonably true. */
16346 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16347 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16348 }
3e1d3d8c
TT
16349 }
16350 }
134d01f1
DJ
16351}
16352
55426c9d
JB
16353/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16354 update TYPE using some information only available in DIE's children. */
16355
16356static void
16357update_enumeration_type_from_children (struct die_info *die,
16358 struct type *type,
16359 struct dwarf2_cu *cu)
16360{
60f7655a 16361 struct die_info *child_die;
55426c9d
JB
16362 int unsigned_enum = 1;
16363 int flag_enum = 1;
16364 ULONGEST mask = 0;
55426c9d 16365
8268c778 16366 auto_obstack obstack;
55426c9d 16367
60f7655a
DE
16368 for (child_die = die->child;
16369 child_die != NULL && child_die->tag;
16370 child_die = sibling_die (child_die))
55426c9d
JB
16371 {
16372 struct attribute *attr;
16373 LONGEST value;
16374 const gdb_byte *bytes;
16375 struct dwarf2_locexpr_baton *baton;
16376 const char *name;
60f7655a 16377
55426c9d
JB
16378 if (child_die->tag != DW_TAG_enumerator)
16379 continue;
16380
16381 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16382 if (attr == NULL)
16383 continue;
16384
16385 name = dwarf2_name (child_die, cu);
16386 if (name == NULL)
16387 name = "<anonymous enumerator>";
16388
16389 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16390 &value, &bytes, &baton);
16391 if (value < 0)
16392 {
16393 unsigned_enum = 0;
16394 flag_enum = 0;
16395 }
16396 else if ((mask & value) != 0)
16397 flag_enum = 0;
16398 else
16399 mask |= value;
16400
16401 /* If we already know that the enum type is neither unsigned, nor
16402 a flag type, no need to look at the rest of the enumerates. */
16403 if (!unsigned_enum && !flag_enum)
16404 break;
55426c9d
JB
16405 }
16406
16407 if (unsigned_enum)
16408 TYPE_UNSIGNED (type) = 1;
16409 if (flag_enum)
16410 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16411}
16412
134d01f1
DJ
16413/* Given a DW_AT_enumeration_type die, set its type. We do not
16414 complete the type's fields yet, or create any symbols. */
c906108c 16415
f792889a 16416static struct type *
134d01f1 16417read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16418{
518817b3 16419 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16420 struct type *type;
c906108c 16421 struct attribute *attr;
0114d602 16422 const char *name;
134d01f1 16423
348e048f
DE
16424 /* If the definition of this type lives in .debug_types, read that type.
16425 Don't follow DW_AT_specification though, that will take us back up
16426 the chain and we want to go down. */
45e58e77 16427 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16428 if (attr != nullptr)
348e048f 16429 {
ac9ec31b 16430 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16431
ac9ec31b 16432 /* The type's CU may not be the same as CU.
02142a6c 16433 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16434 return set_die_type (die, type, cu);
16435 }
16436
c906108c
SS
16437 type = alloc_type (objfile);
16438
16439 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16440 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16441 if (name != NULL)
e86ca25f 16442 TYPE_NAME (type) = name;
c906108c 16443
0626fc76
TT
16444 attr = dwarf2_attr (die, DW_AT_type, cu);
16445 if (attr != NULL)
16446 {
16447 struct type *underlying_type = die_type (die, cu);
16448
16449 TYPE_TARGET_TYPE (type) = underlying_type;
16450 }
16451
e142c38c 16452 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16453 if (attr != nullptr)
c906108c
SS
16454 {
16455 TYPE_LENGTH (type) = DW_UNSND (attr);
16456 }
16457 else
16458 {
16459 TYPE_LENGTH (type) = 0;
16460 }
16461
2b4424c3
TT
16462 maybe_set_alignment (cu, die, type);
16463
137033e9
JB
16464 /* The enumeration DIE can be incomplete. In Ada, any type can be
16465 declared as private in the package spec, and then defined only
16466 inside the package body. Such types are known as Taft Amendment
16467 Types. When another package uses such a type, an incomplete DIE
16468 may be generated by the compiler. */
02eb380e 16469 if (die_is_declaration (die, cu))
876cecd0 16470 TYPE_STUB (type) = 1;
02eb380e 16471
0626fc76
TT
16472 /* Finish the creation of this type by using the enum's children.
16473 We must call this even when the underlying type has been provided
16474 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16475 update_enumeration_type_from_children (die, type, cu);
16476
0626fc76
TT
16477 /* If this type has an underlying type that is not a stub, then we
16478 may use its attributes. We always use the "unsigned" attribute
16479 in this situation, because ordinarily we guess whether the type
16480 is unsigned -- but the guess can be wrong and the underlying type
16481 can tell us the reality. However, we defer to a local size
16482 attribute if one exists, because this lets the compiler override
16483 the underlying type if needed. */
16484 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16485 {
16486 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16487 if (TYPE_LENGTH (type) == 0)
16488 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16489 if (TYPE_RAW_ALIGN (type) == 0
16490 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16491 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16492 }
16493
3d567982
TT
16494 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16495
f792889a 16496 return set_die_type (die, type, cu);
134d01f1
DJ
16497}
16498
16499/* Given a pointer to a die which begins an enumeration, process all
16500 the dies that define the members of the enumeration, and create the
16501 symbol for the enumeration type.
16502
16503 NOTE: We reverse the order of the element list. */
16504
16505static void
16506process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16507{
f792889a 16508 struct type *this_type;
134d01f1 16509
f792889a
DJ
16510 this_type = get_die_type (die, cu);
16511 if (this_type == NULL)
16512 this_type = read_enumeration_type (die, cu);
9dc481d3 16513
639d11d3 16514 if (die->child != NULL)
c906108c 16515 {
9dc481d3
DE
16516 struct die_info *child_die;
16517 struct symbol *sym;
43816ebc 16518 std::vector<struct field> fields;
15d034d0 16519 const char *name;
9dc481d3 16520
639d11d3 16521 child_die = die->child;
c906108c
SS
16522 while (child_die && child_die->tag)
16523 {
16524 if (child_die->tag != DW_TAG_enumerator)
16525 {
e7c27a73 16526 process_die (child_die, cu);
c906108c
SS
16527 }
16528 else
16529 {
39cbfefa
DJ
16530 name = dwarf2_name (child_die, cu);
16531 if (name)
c906108c 16532 {
f792889a 16533 sym = new_symbol (child_die, this_type, cu);
c906108c 16534
43816ebc
TT
16535 fields.emplace_back ();
16536 struct field &field = fields.back ();
c906108c 16537
43816ebc
TT
16538 FIELD_NAME (field) = sym->linkage_name ();
16539 FIELD_TYPE (field) = NULL;
16540 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16541 FIELD_BITSIZE (field) = 0;
c906108c
SS
16542 }
16543 }
16544
16545 child_die = sibling_die (child_die);
16546 }
16547
43816ebc 16548 if (!fields.empty ())
c906108c 16549 {
43816ebc 16550 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 16551 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
16552 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16553 memcpy (TYPE_FIELDS (this_type), fields.data (),
16554 sizeof (struct field) * fields.size ());
c906108c 16555 }
c906108c 16556 }
134d01f1 16557
6c83ed52
TT
16558 /* If we are reading an enum from a .debug_types unit, and the enum
16559 is a declaration, and the enum is not the signatured type in the
16560 unit, then we do not want to add a symbol for it. Adding a
16561 symbol would in some cases obscure the true definition of the
16562 enum, giving users an incomplete type when the definition is
16563 actually available. Note that we do not want to do this for all
16564 enums which are just declarations, because C++0x allows forward
16565 enum declarations. */
3019eac3 16566 if (cu->per_cu->is_debug_types
6c83ed52
TT
16567 && die_is_declaration (die, cu))
16568 {
52dc124a 16569 struct signatured_type *sig_type;
6c83ed52 16570
c0f78cd4 16571 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16572 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16573 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16574 return;
16575 }
16576
f792889a 16577 new_symbol (die, this_type, cu);
c906108c
SS
16578}
16579
16580/* Extract all information from a DW_TAG_array_type DIE and put it in
16581 the DIE's type field. For now, this only handles one dimensional
16582 arrays. */
16583
f792889a 16584static struct type *
e7c27a73 16585read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16586{
518817b3 16587 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16588 struct die_info *child_die;
7e314c57 16589 struct type *type;
c906108c 16590 struct type *element_type, *range_type, *index_type;
c906108c 16591 struct attribute *attr;
15d034d0 16592 const char *name;
a405673c 16593 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16594 unsigned int bit_stride = 0;
c906108c 16595
e7c27a73 16596 element_type = die_type (die, cu);
c906108c 16597
7e314c57
JK
16598 /* The die_type call above may have already set the type for this DIE. */
16599 type = get_die_type (die, cu);
16600 if (type)
16601 return type;
16602
dc53a7ad
JB
16603 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16604 if (attr != NULL)
a405673c
JB
16605 {
16606 int stride_ok;
9a49df9d
AB
16607 struct type *prop_type
16608 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16609
16610 byte_stride_prop
16611 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16612 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16613 prop_type);
a405673c
JB
16614 if (!stride_ok)
16615 {
b98664d3 16616 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16617 " - DIE at %s [in module %s]"),
16618 sect_offset_str (die->sect_off),
518817b3 16619 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16620 /* Ignore this attribute. We will likely not be able to print
16621 arrays of this type correctly, but there is little we can do
16622 to help if we cannot read the attribute's value. */
16623 byte_stride_prop = NULL;
16624 }
16625 }
dc53a7ad
JB
16626
16627 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16628 if (attr != NULL)
16629 bit_stride = DW_UNSND (attr);
16630
c906108c
SS
16631 /* Irix 6.2 native cc creates array types without children for
16632 arrays with unspecified length. */
639d11d3 16633 if (die->child == NULL)
c906108c 16634 {
46bf5051 16635 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16636 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16637 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16638 byte_stride_prop, bit_stride);
f792889a 16639 return set_die_type (die, type, cu);
c906108c
SS
16640 }
16641
791afaa2 16642 std::vector<struct type *> range_types;
639d11d3 16643 child_die = die->child;
c906108c
SS
16644 while (child_die && child_die->tag)
16645 {
16646 if (child_die->tag == DW_TAG_subrange_type)
16647 {
f792889a 16648 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16649
f792889a 16650 if (child_type != NULL)
a02abb62 16651 {
0963b4bd
MS
16652 /* The range type was succesfully read. Save it for the
16653 array type creation. */
791afaa2 16654 range_types.push_back (child_type);
a02abb62 16655 }
c906108c
SS
16656 }
16657 child_die = sibling_die (child_die);
16658 }
16659
16660 /* Dwarf2 dimensions are output from left to right, create the
16661 necessary array types in backwards order. */
7ca2d3a3 16662
c906108c 16663 type = element_type;
7ca2d3a3
DL
16664
16665 if (read_array_order (die, cu) == DW_ORD_col_major)
16666 {
16667 int i = 0;
9a619af0 16668
791afaa2 16669 while (i < range_types.size ())
dc53a7ad 16670 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16671 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16672 }
16673 else
16674 {
791afaa2 16675 size_t ndim = range_types.size ();
7ca2d3a3 16676 while (ndim-- > 0)
dc53a7ad 16677 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16678 byte_stride_prop, bit_stride);
7ca2d3a3 16679 }
c906108c 16680
f5f8a009
EZ
16681 /* Understand Dwarf2 support for vector types (like they occur on
16682 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16683 array type. This is not part of the Dwarf2/3 standard yet, but a
16684 custom vendor extension. The main difference between a regular
16685 array and the vector variant is that vectors are passed by value
16686 to functions. */
e142c38c 16687 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16688 if (attr != nullptr)
ea37ba09 16689 make_vector_type (type);
f5f8a009 16690
dbc98a8b
KW
16691 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16692 implementation may choose to implement triple vectors using this
16693 attribute. */
16694 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16695 if (attr != nullptr)
dbc98a8b
KW
16696 {
16697 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16698 TYPE_LENGTH (type) = DW_UNSND (attr);
16699 else
b98664d3 16700 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16701 "than the total size of elements"));
dbc98a8b
KW
16702 }
16703
39cbfefa
DJ
16704 name = dwarf2_name (die, cu);
16705 if (name)
16706 TYPE_NAME (type) = name;
6e70227d 16707
2b4424c3
TT
16708 maybe_set_alignment (cu, die, type);
16709
0963b4bd 16710 /* Install the type in the die. */
7e314c57
JK
16711 set_die_type (die, type, cu);
16712
16713 /* set_die_type should be already done. */
b4ba55a1
JB
16714 set_descriptive_type (type, die, cu);
16715
7e314c57 16716 return type;
c906108c
SS
16717}
16718
7ca2d3a3 16719static enum dwarf_array_dim_ordering
6e70227d 16720read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16721{
16722 struct attribute *attr;
16723
16724 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16725
435d3d88 16726 if (attr != nullptr)
aead7601 16727 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16728
0963b4bd
MS
16729 /* GNU F77 is a special case, as at 08/2004 array type info is the
16730 opposite order to the dwarf2 specification, but data is still
16731 laid out as per normal fortran.
7ca2d3a3 16732
0963b4bd
MS
16733 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16734 version checking. */
7ca2d3a3 16735
905e0470
PM
16736 if (cu->language == language_fortran
16737 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16738 {
16739 return DW_ORD_row_major;
16740 }
16741
6e70227d 16742 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16743 {
16744 case array_column_major:
16745 return DW_ORD_col_major;
16746 case array_row_major:
16747 default:
16748 return DW_ORD_row_major;
16749 };
16750}
16751
72019c9c 16752/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16753 the DIE's type field. */
72019c9c 16754
f792889a 16755static struct type *
72019c9c
GM
16756read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16757{
7e314c57
JK
16758 struct type *domain_type, *set_type;
16759 struct attribute *attr;
f792889a 16760
7e314c57
JK
16761 domain_type = die_type (die, cu);
16762
16763 /* The die_type call above may have already set the type for this DIE. */
16764 set_type = get_die_type (die, cu);
16765 if (set_type)
16766 return set_type;
16767
16768 set_type = create_set_type (NULL, domain_type);
16769
16770 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16771 if (attr != nullptr)
d09039dd 16772 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16773
2b4424c3
TT
16774 maybe_set_alignment (cu, die, set_type);
16775
f792889a 16776 return set_die_type (die, set_type, cu);
72019c9c 16777}
7ca2d3a3 16778
0971de02
TT
16779/* A helper for read_common_block that creates a locexpr baton.
16780 SYM is the symbol which we are marking as computed.
16781 COMMON_DIE is the DIE for the common block.
16782 COMMON_LOC is the location expression attribute for the common
16783 block itself.
16784 MEMBER_LOC is the location expression attribute for the particular
16785 member of the common block that we are processing.
16786 CU is the CU from which the above come. */
16787
16788static void
16789mark_common_block_symbol_computed (struct symbol *sym,
16790 struct die_info *common_die,
16791 struct attribute *common_loc,
16792 struct attribute *member_loc,
16793 struct dwarf2_cu *cu)
16794{
518817b3
SM
16795 struct dwarf2_per_objfile *dwarf2_per_objfile
16796 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16797 struct objfile *objfile = dwarf2_per_objfile->objfile;
16798 struct dwarf2_locexpr_baton *baton;
16799 gdb_byte *ptr;
16800 unsigned int cu_off;
16801 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16802 LONGEST offset = 0;
16803
16804 gdb_assert (common_loc && member_loc);
16805 gdb_assert (attr_form_is_block (common_loc));
16806 gdb_assert (attr_form_is_block (member_loc)
16807 || attr_form_is_constant (member_loc));
16808
8d749320 16809 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16810 baton->per_cu = cu->per_cu;
16811 gdb_assert (baton->per_cu);
16812
16813 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16814
16815 if (attr_form_is_constant (member_loc))
16816 {
16817 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16818 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16819 }
16820 else
16821 baton->size += DW_BLOCK (member_loc)->size;
16822
224c3ddb 16823 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16824 baton->data = ptr;
16825
16826 *ptr++ = DW_OP_call4;
9c541725 16827 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16828 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16829 ptr += 4;
16830
16831 if (attr_form_is_constant (member_loc))
16832 {
16833 *ptr++ = DW_OP_addr;
16834 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16835 ptr += cu->header.addr_size;
16836 }
16837 else
16838 {
16839 /* We have to copy the data here, because DW_OP_call4 will only
16840 use a DW_AT_location attribute. */
16841 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16842 ptr += DW_BLOCK (member_loc)->size;
16843 }
16844
16845 *ptr++ = DW_OP_plus;
16846 gdb_assert (ptr - baton->data == baton->size);
16847
0971de02 16848 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16849 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16850}
16851
4357ac6c
TT
16852/* Create appropriate locally-scoped variables for all the
16853 DW_TAG_common_block entries. Also create a struct common_block
16854 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16855 is used to separate the common blocks name namespace from regular
4357ac6c 16856 variable names. */
c906108c
SS
16857
16858static void
e7c27a73 16859read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16860{
0971de02
TT
16861 struct attribute *attr;
16862
16863 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16864 if (attr != nullptr)
0971de02
TT
16865 {
16866 /* Support the .debug_loc offsets. */
16867 if (attr_form_is_block (attr))
16868 {
16869 /* Ok. */
16870 }
16871 else if (attr_form_is_section_offset (attr))
16872 {
16873 dwarf2_complex_location_expr_complaint ();
16874 attr = NULL;
16875 }
16876 else
16877 {
16878 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16879 "common block member");
16880 attr = NULL;
16881 }
16882 }
16883
639d11d3 16884 if (die->child != NULL)
c906108c 16885 {
518817b3 16886 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16887 struct die_info *child_die;
16888 size_t n_entries = 0, size;
16889 struct common_block *common_block;
16890 struct symbol *sym;
74ac6d43 16891
4357ac6c
TT
16892 for (child_die = die->child;
16893 child_die && child_die->tag;
16894 child_die = sibling_die (child_die))
16895 ++n_entries;
16896
16897 size = (sizeof (struct common_block)
16898 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16899 common_block
16900 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16901 size);
4357ac6c
TT
16902 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16903 common_block->n_entries = 0;
16904
16905 for (child_die = die->child;
16906 child_die && child_die->tag;
16907 child_die = sibling_die (child_die))
16908 {
16909 /* Create the symbol in the DW_TAG_common_block block in the current
16910 symbol scope. */
e7c27a73 16911 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16912 if (sym != NULL)
16913 {
16914 struct attribute *member_loc;
16915
16916 common_block->contents[common_block->n_entries++] = sym;
16917
16918 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16919 cu);
16920 if (member_loc)
16921 {
16922 /* GDB has handled this for a long time, but it is
16923 not specified by DWARF. It seems to have been
16924 emitted by gfortran at least as recently as:
16925 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16926 complaint (_("Variable in common block has "
0971de02 16927 "DW_AT_data_member_location "
9d8780f0
SM
16928 "- DIE at %s [in module %s]"),
16929 sect_offset_str (child_die->sect_off),
518817b3 16930 objfile_name (objfile));
0971de02
TT
16931
16932 if (attr_form_is_section_offset (member_loc))
16933 dwarf2_complex_location_expr_complaint ();
16934 else if (attr_form_is_constant (member_loc)
16935 || attr_form_is_block (member_loc))
16936 {
435d3d88 16937 if (attr != nullptr)
0971de02
TT
16938 mark_common_block_symbol_computed (sym, die, attr,
16939 member_loc, cu);
16940 }
16941 else
16942 dwarf2_complex_location_expr_complaint ();
16943 }
16944 }
c906108c 16945 }
4357ac6c
TT
16946
16947 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16948 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16949 }
16950}
16951
0114d602 16952/* Create a type for a C++ namespace. */
d9fa45fe 16953
0114d602
DJ
16954static struct type *
16955read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16956{
518817b3 16957 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16958 const char *previous_prefix, *name;
9219021c 16959 int is_anonymous;
0114d602
DJ
16960 struct type *type;
16961
16962 /* For extensions, reuse the type of the original namespace. */
16963 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16964 {
16965 struct die_info *ext_die;
16966 struct dwarf2_cu *ext_cu = cu;
9a619af0 16967
0114d602
DJ
16968 ext_die = dwarf2_extension (die, &ext_cu);
16969 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16970
16971 /* EXT_CU may not be the same as CU.
02142a6c 16972 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16973 return set_die_type (die, type, cu);
16974 }
9219021c 16975
e142c38c 16976 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16977
16978 /* Now build the name of the current namespace. */
16979
0114d602
DJ
16980 previous_prefix = determine_prefix (die, cu);
16981 if (previous_prefix[0] != '\0')
16982 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16983 previous_prefix, name, 0, cu);
0114d602
DJ
16984
16985 /* Create the type. */
19f392bc 16986 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16987
60531b24 16988 return set_die_type (die, type, cu);
0114d602
DJ
16989}
16990
22cee43f 16991/* Read a namespace scope. */
0114d602
DJ
16992
16993static void
16994read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16995{
518817b3 16996 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16997 int is_anonymous;
9219021c 16998
5c4e30ca
DC
16999 /* Add a symbol associated to this if we haven't seen the namespace
17000 before. Also, add a using directive if it's an anonymous
17001 namespace. */
9219021c 17002
f2f0e013 17003 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17004 {
17005 struct type *type;
17006
0114d602 17007 type = read_type_die (die, cu);
e7c27a73 17008 new_symbol (die, type, cu);
5c4e30ca 17009
e8e80198 17010 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17011 if (is_anonymous)
0114d602
DJ
17012 {
17013 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17014
eb1e02fd 17015 std::vector<const char *> excludes;
804d2729 17016 add_using_directive (using_directives (cu),
22cee43f 17017 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 17018 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17019 }
5c4e30ca 17020 }
9219021c 17021
639d11d3 17022 if (die->child != NULL)
d9fa45fe 17023 {
639d11d3 17024 struct die_info *child_die = die->child;
6e70227d 17025
d9fa45fe
DC
17026 while (child_die && child_die->tag)
17027 {
e7c27a73 17028 process_die (child_die, cu);
d9fa45fe
DC
17029 child_die = sibling_die (child_die);
17030 }
17031 }
38d518c9
EZ
17032}
17033
f55ee35c
JK
17034/* Read a Fortran module as type. This DIE can be only a declaration used for
17035 imported module. Still we need that type as local Fortran "use ... only"
17036 declaration imports depend on the created type in determine_prefix. */
17037
17038static struct type *
17039read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17040{
518817b3 17041 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 17042 const char *module_name;
f55ee35c
JK
17043 struct type *type;
17044
17045 module_name = dwarf2_name (die, cu);
19f392bc 17046 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17047
f55ee35c
JK
17048 return set_die_type (die, type, cu);
17049}
17050
5d7cb8df
JK
17051/* Read a Fortran module. */
17052
17053static void
17054read_module (struct die_info *die, struct dwarf2_cu *cu)
17055{
17056 struct die_info *child_die = die->child;
530e8392
KB
17057 struct type *type;
17058
17059 type = read_type_die (die, cu);
17060 new_symbol (die, type, cu);
5d7cb8df 17061
5d7cb8df
JK
17062 while (child_die && child_die->tag)
17063 {
17064 process_die (child_die, cu);
17065 child_die = sibling_die (child_die);
17066 }
17067}
17068
38d518c9
EZ
17069/* Return the name of the namespace represented by DIE. Set
17070 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17071 namespace. */
17072
17073static const char *
e142c38c 17074namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17075{
17076 struct die_info *current_die;
17077 const char *name = NULL;
17078
17079 /* Loop through the extensions until we find a name. */
17080
17081 for (current_die = die;
17082 current_die != NULL;
f2f0e013 17083 current_die = dwarf2_extension (die, &cu))
38d518c9 17084 {
96553a0c
DE
17085 /* We don't use dwarf2_name here so that we can detect the absence
17086 of a name -> anonymous namespace. */
7d45c7c3 17087 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17088
38d518c9
EZ
17089 if (name != NULL)
17090 break;
17091 }
17092
17093 /* Is it an anonymous namespace? */
17094
17095 *is_anonymous = (name == NULL);
17096 if (*is_anonymous)
2b1dbab0 17097 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17098
17099 return name;
d9fa45fe
DC
17100}
17101
c906108c
SS
17102/* Extract all information from a DW_TAG_pointer_type DIE and add to
17103 the user defined type vector. */
17104
f792889a 17105static struct type *
e7c27a73 17106read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17107{
518817b3
SM
17108 struct gdbarch *gdbarch
17109 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17110 struct comp_unit_head *cu_header = &cu->header;
c906108c 17111 struct type *type;
8b2dbe47
KB
17112 struct attribute *attr_byte_size;
17113 struct attribute *attr_address_class;
17114 int byte_size, addr_class;
7e314c57
JK
17115 struct type *target_type;
17116
17117 target_type = die_type (die, cu);
c906108c 17118
7e314c57
JK
17119 /* The die_type call above may have already set the type for this DIE. */
17120 type = get_die_type (die, cu);
17121 if (type)
17122 return type;
17123
17124 type = lookup_pointer_type (target_type);
8b2dbe47 17125
e142c38c 17126 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17127 if (attr_byte_size)
17128 byte_size = DW_UNSND (attr_byte_size);
c906108c 17129 else
8b2dbe47
KB
17130 byte_size = cu_header->addr_size;
17131
e142c38c 17132 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17133 if (attr_address_class)
17134 addr_class = DW_UNSND (attr_address_class);
17135 else
17136 addr_class = DW_ADDR_none;
17137
2b4424c3
TT
17138 ULONGEST alignment = get_alignment (cu, die);
17139
17140 /* If the pointer size, alignment, or address class is different
17141 than the default, create a type variant marked as such and set
17142 the length accordingly. */
17143 if (TYPE_LENGTH (type) != byte_size
17144 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17145 && alignment != TYPE_RAW_ALIGN (type))
17146 || addr_class != DW_ADDR_none)
c906108c 17147 {
5e2b427d 17148 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17149 {
17150 int type_flags;
17151
849957d9 17152 type_flags = gdbarch_address_class_type_flags
5e2b427d 17153 (gdbarch, byte_size, addr_class);
876cecd0
TT
17154 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17155 == 0);
8b2dbe47
KB
17156 type = make_type_with_address_space (type, type_flags);
17157 }
17158 else if (TYPE_LENGTH (type) != byte_size)
17159 {
b98664d3 17160 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17161 }
2b4424c3
TT
17162 else if (TYPE_RAW_ALIGN (type) != alignment)
17163 {
b98664d3 17164 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17165 " - DIE at %s [in module %s]"),
17166 sect_offset_str (die->sect_off),
17167 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17168 }
6e70227d 17169 else
9a619af0
MS
17170 {
17171 /* Should we also complain about unhandled address classes? */
17172 }
c906108c 17173 }
8b2dbe47
KB
17174
17175 TYPE_LENGTH (type) = byte_size;
2b4424c3 17176 set_type_align (type, alignment);
f792889a 17177 return set_die_type (die, type, cu);
c906108c
SS
17178}
17179
17180/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17181 the user defined type vector. */
17182
f792889a 17183static struct type *
e7c27a73 17184read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17185{
17186 struct type *type;
17187 struct type *to_type;
17188 struct type *domain;
17189
e7c27a73
DJ
17190 to_type = die_type (die, cu);
17191 domain = die_containing_type (die, cu);
0d5de010 17192
7e314c57
JK
17193 /* The calls above may have already set the type for this DIE. */
17194 type = get_die_type (die, cu);
17195 if (type)
17196 return type;
17197
0d5de010
DJ
17198 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17199 type = lookup_methodptr_type (to_type);
7078baeb
TT
17200 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17201 {
518817b3
SM
17202 struct type *new_type
17203 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17204
17205 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17206 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17207 TYPE_VARARGS (to_type));
17208 type = lookup_methodptr_type (new_type);
17209 }
0d5de010
DJ
17210 else
17211 type = lookup_memberptr_type (to_type, domain);
c906108c 17212
f792889a 17213 return set_die_type (die, type, cu);
c906108c
SS
17214}
17215
4297a3f0 17216/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17217 the user defined type vector. */
17218
f792889a 17219static struct type *
4297a3f0
AV
17220read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17221 enum type_code refcode)
c906108c 17222{
e7c27a73 17223 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17224 struct type *type, *target_type;
c906108c
SS
17225 struct attribute *attr;
17226
4297a3f0
AV
17227 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17228
7e314c57
JK
17229 target_type = die_type (die, cu);
17230
17231 /* The die_type call above may have already set the type for this DIE. */
17232 type = get_die_type (die, cu);
17233 if (type)
17234 return type;
17235
4297a3f0 17236 type = lookup_reference_type (target_type, refcode);
e142c38c 17237 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17238 if (attr != nullptr)
c906108c
SS
17239 {
17240 TYPE_LENGTH (type) = DW_UNSND (attr);
17241 }
17242 else
17243 {
107d2387 17244 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17245 }
2b4424c3 17246 maybe_set_alignment (cu, die, type);
f792889a 17247 return set_die_type (die, type, cu);
c906108c
SS
17248}
17249
cf363f18
MW
17250/* Add the given cv-qualifiers to the element type of the array. GCC
17251 outputs DWARF type qualifiers that apply to an array, not the
17252 element type. But GDB relies on the array element type to carry
17253 the cv-qualifiers. This mimics section 6.7.3 of the C99
17254 specification. */
17255
17256static struct type *
17257add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17258 struct type *base_type, int cnst, int voltl)
17259{
17260 struct type *el_type, *inner_array;
17261
17262 base_type = copy_type (base_type);
17263 inner_array = base_type;
17264
17265 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17266 {
17267 TYPE_TARGET_TYPE (inner_array) =
17268 copy_type (TYPE_TARGET_TYPE (inner_array));
17269 inner_array = TYPE_TARGET_TYPE (inner_array);
17270 }
17271
17272 el_type = TYPE_TARGET_TYPE (inner_array);
17273 cnst |= TYPE_CONST (el_type);
17274 voltl |= TYPE_VOLATILE (el_type);
17275 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17276
17277 return set_die_type (die, base_type, cu);
17278}
17279
f792889a 17280static struct type *
e7c27a73 17281read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17282{
f792889a 17283 struct type *base_type, *cv_type;
c906108c 17284
e7c27a73 17285 base_type = die_type (die, cu);
7e314c57
JK
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
2f608a3a
KW
17292 /* In case the const qualifier is applied to an array type, the element type
17293 is so qualified, not the array type (section 6.7.3 of C99). */
17294 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17295 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17296
f792889a
DJ
17297 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17298 return set_die_type (die, cv_type, cu);
c906108c
SS
17299}
17300
f792889a 17301static struct type *
e7c27a73 17302read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17303{
f792889a 17304 struct type *base_type, *cv_type;
c906108c 17305
e7c27a73 17306 base_type = die_type (die, cu);
7e314c57
JK
17307
17308 /* The die_type call above may have already set the type for this DIE. */
17309 cv_type = get_die_type (die, cu);
17310 if (cv_type)
17311 return cv_type;
17312
cf363f18
MW
17313 /* In case the volatile qualifier is applied to an array type, the
17314 element type is so qualified, not the array type (section 6.7.3
17315 of C99). */
17316 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17317 return add_array_cv_type (die, cu, base_type, 0, 1);
17318
f792889a
DJ
17319 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17320 return set_die_type (die, cv_type, cu);
c906108c
SS
17321}
17322
06d66ee9
TT
17323/* Handle DW_TAG_restrict_type. */
17324
17325static struct type *
17326read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17327{
17328 struct type *base_type, *cv_type;
17329
17330 base_type = die_type (die, cu);
17331
17332 /* The die_type call above may have already set the type for this DIE. */
17333 cv_type = get_die_type (die, cu);
17334 if (cv_type)
17335 return cv_type;
17336
17337 cv_type = make_restrict_type (base_type);
17338 return set_die_type (die, cv_type, cu);
17339}
17340
a2c2acaf
MW
17341/* Handle DW_TAG_atomic_type. */
17342
17343static struct type *
17344read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17345{
17346 struct type *base_type, *cv_type;
17347
17348 base_type = die_type (die, cu);
17349
17350 /* The die_type call above may have already set the type for this DIE. */
17351 cv_type = get_die_type (die, cu);
17352 if (cv_type)
17353 return cv_type;
17354
17355 cv_type = make_atomic_type (base_type);
17356 return set_die_type (die, cv_type, cu);
17357}
17358
c906108c
SS
17359/* Extract all information from a DW_TAG_string_type DIE and add to
17360 the user defined type vector. It isn't really a user defined type,
17361 but it behaves like one, with other DIE's using an AT_user_def_type
17362 attribute to reference it. */
17363
f792889a 17364static struct type *
e7c27a73 17365read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17366{
518817b3 17367 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17368 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17369 struct type *type, *range_type, *index_type, *char_type;
17370 struct attribute *attr;
216a7e6b
AB
17371 struct dynamic_prop prop;
17372 bool length_is_constant = true;
17373 LONGEST length;
17374
17375 /* There are a couple of places where bit sizes might be made use of
17376 when parsing a DW_TAG_string_type, however, no producer that we know
17377 of make use of these. Handling bit sizes that are a multiple of the
17378 byte size is easy enough, but what about other bit sizes? Lets deal
17379 with that problem when we have to. Warn about these attributes being
17380 unsupported, then parse the type and ignore them like we always
17381 have. */
17382 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17383 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17384 {
17385 static bool warning_printed = false;
17386 if (!warning_printed)
17387 {
17388 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17389 "currently supported on DW_TAG_string_type."));
17390 warning_printed = true;
17391 }
17392 }
c906108c 17393
e142c38c 17394 attr = dwarf2_attr (die, DW_AT_string_length, cu);
216a7e6b
AB
17395 if (attr != nullptr && !attr_form_is_constant (attr))
17396 {
17397 /* The string length describes the location at which the length of
17398 the string can be found. The size of the length field can be
17399 specified with one of the attributes below. */
17400 struct type *prop_type;
17401 struct attribute *len
17402 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17403 if (len == nullptr)
17404 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17405 if (len != nullptr && attr_form_is_constant (len))
17406 {
17407 /* Pass 0 as the default as we know this attribute is constant
17408 and the default value will not be returned. */
17409 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17410 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17411 }
17412 else
17413 {
17414 /* If the size is not specified then we assume it is the size of
17415 an address on this target. */
17416 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17417 }
17418
17419 /* Convert the attribute into a dynamic property. */
17420 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17421 length = 1;
17422 else
17423 length_is_constant = false;
17424 }
17425 else if (attr != nullptr)
17426 {
17427 /* This DW_AT_string_length just contains the length with no
17428 indirection. There's no need to create a dynamic property in this
17429 case. Pass 0 for the default value as we know it will not be
17430 returned in this case. */
17431 length = dwarf2_get_attr_constant_value (attr, 0);
17432 }
17433 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17434 {
216a7e6b
AB
17435 /* We don't currently support non-constant byte sizes for strings. */
17436 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17437 }
17438 else
17439 {
216a7e6b
AB
17440 /* Use 1 as a fallback length if we have nothing else. */
17441 length = 1;
c906108c 17442 }
6ccb9162 17443
46bf5051 17444 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17445 if (length_is_constant)
17446 range_type = create_static_range_type (NULL, index_type, 1, length);
17447 else
17448 {
17449 struct dynamic_prop low_bound;
17450
17451 low_bound.kind = PROP_CONST;
17452 low_bound.data.const_val = 1;
17453 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17454 }
3b7538c0
UW
17455 char_type = language_string_char_type (cu->language_defn, gdbarch);
17456 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17457
f792889a 17458 return set_die_type (die, type, cu);
c906108c
SS
17459}
17460
4d804846
JB
17461/* Assuming that DIE corresponds to a function, returns nonzero
17462 if the function is prototyped. */
17463
17464static int
17465prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17466{
17467 struct attribute *attr;
17468
17469 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17470 if (attr && (DW_UNSND (attr) != 0))
17471 return 1;
17472
17473 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17474 is only meaningful for C, but the concept also extends to other
4d804846
JB
17475 languages that allow unprototyped functions (Eg: Objective C).
17476 For all other languages, assume that functions are always
17477 prototyped. */
17478 if (cu->language != language_c
17479 && cu->language != language_objc
17480 && cu->language != language_opencl)
17481 return 1;
17482
17483 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17484 prototyped and unprototyped functions; default to prototyped,
17485 since that is more common in modern code (and RealView warns
17486 about unprototyped functions). */
17487 if (producer_is_realview (cu->producer))
17488 return 1;
17489
17490 return 0;
17491}
17492
c906108c
SS
17493/* Handle DIES due to C code like:
17494
17495 struct foo
c5aa993b
JM
17496 {
17497 int (*funcp)(int a, long l);
17498 int b;
17499 };
c906108c 17500
0963b4bd 17501 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17502
f792889a 17503static struct type *
e7c27a73 17504read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17505{
518817b3 17506 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17507 struct type *type; /* Type that this function returns. */
17508 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17509 struct attribute *attr;
17510
e7c27a73 17511 type = die_type (die, cu);
7e314c57
JK
17512
17513 /* The die_type call above may have already set the type for this DIE. */
17514 ftype = get_die_type (die, cu);
17515 if (ftype)
17516 return ftype;
17517
0c8b41f1 17518 ftype = lookup_function_type (type);
c906108c 17519
4d804846 17520 if (prototyped_function_p (die, cu))
a6c727b2 17521 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17522
c055b101
CV
17523 /* Store the calling convention in the type if it's available in
17524 the subroutine die. Otherwise set the calling convention to
17525 the default value DW_CC_normal. */
17526 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17527 if (attr != nullptr
17528 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17529 TYPE_CALLING_CONVENTION (ftype)
17530 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17531 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17532 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17533 else
17534 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17535
743649fd
MW
17536 /* Record whether the function returns normally to its caller or not
17537 if the DWARF producer set that information. */
17538 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17539 if (attr && (DW_UNSND (attr) != 0))
17540 TYPE_NO_RETURN (ftype) = 1;
17541
76c10ea2
GM
17542 /* We need to add the subroutine type to the die immediately so
17543 we don't infinitely recurse when dealing with parameters
0963b4bd 17544 declared as the same subroutine type. */
76c10ea2 17545 set_die_type (die, ftype, cu);
6e70227d 17546
639d11d3 17547 if (die->child != NULL)
c906108c 17548 {
bb5ed363 17549 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17550 struct die_info *child_die;
8072405b 17551 int nparams, iparams;
c906108c
SS
17552
17553 /* Count the number of parameters.
17554 FIXME: GDB currently ignores vararg functions, but knows about
17555 vararg member functions. */
8072405b 17556 nparams = 0;
639d11d3 17557 child_die = die->child;
c906108c
SS
17558 while (child_die && child_die->tag)
17559 {
17560 if (child_die->tag == DW_TAG_formal_parameter)
17561 nparams++;
17562 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17563 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17564 child_die = sibling_die (child_die);
17565 }
17566
17567 /* Allocate storage for parameters and fill them in. */
17568 TYPE_NFIELDS (ftype) = nparams;
17569 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17570 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17571
8072405b
JK
17572 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17573 even if we error out during the parameters reading below. */
17574 for (iparams = 0; iparams < nparams; iparams++)
17575 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17576
17577 iparams = 0;
639d11d3 17578 child_die = die->child;
c906108c
SS
17579 while (child_die && child_die->tag)
17580 {
17581 if (child_die->tag == DW_TAG_formal_parameter)
17582 {
3ce3b1ba
PA
17583 struct type *arg_type;
17584
17585 /* DWARF version 2 has no clean way to discern C++
17586 static and non-static member functions. G++ helps
17587 GDB by marking the first parameter for non-static
17588 member functions (which is the this pointer) as
17589 artificial. We pass this information to
17590 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17591
17592 DWARF version 3 added DW_AT_object_pointer, which GCC
17593 4.5 does not yet generate. */
e142c38c 17594 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17595 if (attr != nullptr)
c906108c
SS
17596 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17597 else
9c37b5ae 17598 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17599 arg_type = die_type (child_die, cu);
17600
17601 /* RealView does not mark THIS as const, which the testsuite
17602 expects. GCC marks THIS as const in method definitions,
17603 but not in the class specifications (GCC PR 43053). */
17604 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17605 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17606 {
17607 int is_this = 0;
17608 struct dwarf2_cu *arg_cu = cu;
17609 const char *name = dwarf2_name (child_die, cu);
17610
17611 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17612 if (attr != nullptr)
3ce3b1ba
PA
17613 {
17614 /* If the compiler emits this, use it. */
17615 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17616 is_this = 1;
17617 }
17618 else if (name && strcmp (name, "this") == 0)
17619 /* Function definitions will have the argument names. */
17620 is_this = 1;
17621 else if (name == NULL && iparams == 0)
17622 /* Declarations may not have the names, so like
17623 elsewhere in GDB, assume an artificial first
17624 argument is "this". */
17625 is_this = 1;
17626
17627 if (is_this)
17628 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17629 arg_type, 0);
17630 }
17631
17632 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17633 iparams++;
17634 }
17635 child_die = sibling_die (child_die);
17636 }
17637 }
17638
76c10ea2 17639 return ftype;
c906108c
SS
17640}
17641
f792889a 17642static struct type *
e7c27a73 17643read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17644{
518817b3 17645 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17646 const char *name = NULL;
3c8e0968 17647 struct type *this_type, *target_type;
c906108c 17648
94af9270 17649 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17650 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17651 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17652 set_die_type (die, this_type, cu);
3c8e0968
DE
17653 target_type = die_type (die, cu);
17654 if (target_type != this_type)
17655 TYPE_TARGET_TYPE (this_type) = target_type;
17656 else
17657 {
17658 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17659 spec and cause infinite loops in GDB. */
b98664d3 17660 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17661 "- DIE at %s [in module %s]"),
17662 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17663 TYPE_TARGET_TYPE (this_type) = NULL;
17664 }
f792889a 17665 return this_type;
c906108c
SS
17666}
17667
9b790ce7
UW
17668/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17669 (which may be different from NAME) to the architecture back-end to allow
17670 it to guess the correct format if necessary. */
17671
17672static struct type *
17673dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17674 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17675{
17676 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17677 const struct floatformat **format;
17678 struct type *type;
17679
17680 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17681 if (format)
103a685e 17682 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17683 else
77b7c781 17684 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17685
17686 return type;
17687}
17688
eb77c9df
AB
17689/* Allocate an integer type of size BITS and name NAME. */
17690
17691static struct type *
17692dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17693 int bits, int unsigned_p, const char *name)
17694{
17695 struct type *type;
17696
17697 /* Versions of Intel's C Compiler generate an integer type called "void"
17698 instead of using DW_TAG_unspecified_type. This has been seen on
17699 at least versions 14, 17, and 18. */
35ee2dc2
AB
17700 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17701 && strcmp (name, "void") == 0)
eb77c9df
AB
17702 type = objfile_type (objfile)->builtin_void;
17703 else
17704 type = init_integer_type (objfile, bits, unsigned_p, name);
17705
17706 return type;
17707}
17708
8bdc1658
AB
17709/* Initialise and return a floating point type of size BITS suitable for
17710 use as a component of a complex number. The NAME_HINT is passed through
17711 when initialising the floating point type and is the name of the complex
17712 type.
17713
17714 As DWARF doesn't currently provide an explicit name for the components
17715 of a complex number, but it can be helpful to have these components
17716 named, we try to select a suitable name based on the size of the
17717 component. */
17718static struct type *
17719dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17720 struct objfile *objfile,
103a685e
TT
17721 int bits, const char *name_hint,
17722 enum bfd_endian byte_order)
8bdc1658
AB
17723{
17724 gdbarch *gdbarch = get_objfile_arch (objfile);
17725 struct type *tt = nullptr;
17726
35add35e
AB
17727 /* Try to find a suitable floating point builtin type of size BITS.
17728 We're going to use the name of this type as the name for the complex
17729 target type that we are about to create. */
1db455a7 17730 switch (cu->language)
8bdc1658 17731 {
1db455a7
AB
17732 case language_fortran:
17733 switch (bits)
17734 {
17735 case 32:
17736 tt = builtin_f_type (gdbarch)->builtin_real;
17737 break;
17738 case 64:
17739 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17740 break;
17741 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17742 case 128:
17743 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17744 break;
17745 }
8bdc1658 17746 break;
1db455a7
AB
17747 default:
17748 switch (bits)
17749 {
17750 case 32:
17751 tt = builtin_type (gdbarch)->builtin_float;
17752 break;
17753 case 64:
17754 tt = builtin_type (gdbarch)->builtin_double;
17755 break;
17756 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17757 case 128:
17758 tt = builtin_type (gdbarch)->builtin_long_double;
17759 break;
17760 }
8bdc1658
AB
17761 break;
17762 }
17763
35add35e
AB
17764 /* If the type we found doesn't match the size we were looking for, then
17765 pretend we didn't find a type at all, the complex target type we
17766 create will then be nameless. */
a12e5744 17767 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17768 tt = nullptr;
17769
8bdc1658 17770 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17771 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17772}
17773
c906108c
SS
17774/* Find a representation of a given base type and install
17775 it in the TYPE field of the die. */
17776
f792889a 17777static struct type *
e7c27a73 17778read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17779{
518817b3 17780 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17781 struct type *type;
17782 struct attribute *attr;
19f392bc 17783 int encoding = 0, bits = 0;
15d034d0 17784 const char *name;
34877895 17785 gdbarch *arch;
c906108c 17786
e142c38c 17787 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17788 if (attr != nullptr)
34877895 17789 encoding = DW_UNSND (attr);
e142c38c 17790 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17791 if (attr != nullptr)
34877895 17792 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17793 name = dwarf2_name (die, cu);
6ccb9162 17794 if (!name)
34877895 17795 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17796
17797 arch = get_objfile_arch (objfile);
17798 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17799
34877895
PJ
17800 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17801 if (attr)
103a685e
TT
17802 {
17803 int endianity = DW_UNSND (attr);
17804
17805 switch (endianity)
17806 {
17807 case DW_END_big:
17808 byte_order = BFD_ENDIAN_BIG;
17809 break;
17810 case DW_END_little:
17811 byte_order = BFD_ENDIAN_LITTLE;
17812 break;
17813 default:
17814 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17815 break;
17816 }
17817 }
6ccb9162
UW
17818
17819 switch (encoding)
c906108c 17820 {
6ccb9162
UW
17821 case DW_ATE_address:
17822 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17823 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17824 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17825 break;
17826 case DW_ATE_boolean:
19f392bc 17827 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17828 break;
17829 case DW_ATE_complex_float:
103a685e
TT
17830 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17831 byte_order);
19f392bc 17832 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17833 break;
17834 case DW_ATE_decimal_float:
19f392bc 17835 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17836 break;
17837 case DW_ATE_float:
103a685e 17838 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17839 break;
17840 case DW_ATE_signed:
eb77c9df 17841 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17842 break;
17843 case DW_ATE_unsigned:
3b2b8fea
TT
17844 if (cu->language == language_fortran
17845 && name
61012eef 17846 && startswith (name, "character("))
19f392bc
UW
17847 type = init_character_type (objfile, bits, 1, name);
17848 else
eb77c9df 17849 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17850 break;
17851 case DW_ATE_signed_char:
6e70227d 17852 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17853 || cu->language == language_pascal
17854 || cu->language == language_fortran)
19f392bc
UW
17855 type = init_character_type (objfile, bits, 0, name);
17856 else
eb77c9df 17857 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17858 break;
17859 case DW_ATE_unsigned_char:
868a0084 17860 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17861 || cu->language == language_pascal
c44af4eb
TT
17862 || cu->language == language_fortran
17863 || cu->language == language_rust)
19f392bc
UW
17864 type = init_character_type (objfile, bits, 1, name);
17865 else
eb77c9df 17866 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17867 break;
75079b2b 17868 case DW_ATE_UTF:
53e710ac 17869 {
53e710ac
PA
17870 if (bits == 16)
17871 type = builtin_type (arch)->builtin_char16;
17872 else if (bits == 32)
17873 type = builtin_type (arch)->builtin_char32;
17874 else
17875 {
b98664d3 17876 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17877 bits);
eb77c9df 17878 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17879 }
17880 return set_die_type (die, type, cu);
17881 }
75079b2b
TT
17882 break;
17883
6ccb9162 17884 default:
b98664d3 17885 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17886 dwarf_type_encoding_name (encoding));
77b7c781 17887 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17888 break;
c906108c 17889 }
6ccb9162 17890
0114d602 17891 if (name && strcmp (name, "char") == 0)
876cecd0 17892 TYPE_NOSIGN (type) = 1;
0114d602 17893
2b4424c3
TT
17894 maybe_set_alignment (cu, die, type);
17895
103a685e 17896 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17897
f792889a 17898 return set_die_type (die, type, cu);
c906108c
SS
17899}
17900
80180f79
SA
17901/* Parse dwarf attribute if it's a block, reference or constant and put the
17902 resulting value of the attribute into struct bound_prop.
17903 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17904
17905static int
17906attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17907 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17908 struct type *default_type)
80180f79
SA
17909{
17910 struct dwarf2_property_baton *baton;
518817b3
SM
17911 struct obstack *obstack
17912 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17913
9a49df9d
AB
17914 gdb_assert (default_type != NULL);
17915
80180f79
SA
17916 if (attr == NULL || prop == NULL)
17917 return 0;
17918
17919 if (attr_form_is_block (attr))
17920 {
8d749320 17921 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17922 baton->property_type = default_type;
80180f79
SA
17923 baton->locexpr.per_cu = cu->per_cu;
17924 baton->locexpr.size = DW_BLOCK (attr)->size;
17925 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17926 switch (attr->name)
17927 {
17928 case DW_AT_string_length:
17929 baton->locexpr.is_reference = true;
17930 break;
17931 default:
17932 baton->locexpr.is_reference = false;
17933 break;
17934 }
80180f79
SA
17935 prop->data.baton = baton;
17936 prop->kind = PROP_LOCEXPR;
17937 gdb_assert (prop->data.baton != NULL);
17938 }
17939 else if (attr_form_is_ref (attr))
17940 {
17941 struct dwarf2_cu *target_cu = cu;
17942 struct die_info *target_die;
17943 struct attribute *target_attr;
17944
17945 target_die = follow_die_ref (die, attr, &target_cu);
17946 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17947 if (target_attr == NULL)
17948 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17949 target_cu);
80180f79
SA
17950 if (target_attr == NULL)
17951 return 0;
17952
df25ebbd 17953 switch (target_attr->name)
80180f79 17954 {
df25ebbd
JB
17955 case DW_AT_location:
17956 if (attr_form_is_section_offset (target_attr))
17957 {
8d749320 17958 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17959 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17960 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17961 prop->data.baton = baton;
17962 prop->kind = PROP_LOCLIST;
17963 gdb_assert (prop->data.baton != NULL);
17964 }
17965 else if (attr_form_is_block (target_attr))
17966 {
8d749320 17967 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17968 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17969 baton->locexpr.per_cu = cu->per_cu;
17970 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17971 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17972 baton->locexpr.is_reference = true;
df25ebbd
JB
17973 prop->data.baton = baton;
17974 prop->kind = PROP_LOCEXPR;
17975 gdb_assert (prop->data.baton != NULL);
17976 }
17977 else
17978 {
17979 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17980 "dynamic property");
17981 return 0;
17982 }
17983 break;
17984 case DW_AT_data_member_location:
17985 {
17986 LONGEST offset;
17987
17988 if (!handle_data_member_location (target_die, target_cu,
17989 &offset))
17990 return 0;
17991
8d749320 17992 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17993 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17994 target_cu);
df25ebbd
JB
17995 baton->offset_info.offset = offset;
17996 baton->offset_info.type = die_type (target_die, target_cu);
17997 prop->data.baton = baton;
17998 prop->kind = PROP_ADDR_OFFSET;
17999 break;
18000 }
80180f79
SA
18001 }
18002 }
18003 else if (attr_form_is_constant (attr))
18004 {
18005 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18006 prop->kind = PROP_CONST;
18007 }
18008 else
18009 {
18010 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18011 dwarf2_name (die, cu));
18012 return 0;
18013 }
18014
18015 return 1;
18016}
18017
11a8b164
AB
18018/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18019 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
18020
18021static struct type *
11a8b164
AB
18022dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18023 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
18024{
18025 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
18026 struct type *int_type;
18027
18028 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18029#define TRY_TYPE(F) \
18030 int_type = (unsigned_p \
18031 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18032 : objfile_type (objfile)->builtin_ ## F); \
18033 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18034 return int_type
18035
18036 TRY_TYPE (char);
18037 TRY_TYPE (short);
18038 TRY_TYPE (int);
18039 TRY_TYPE (long);
18040 TRY_TYPE (long_long);
18041
18042#undef TRY_TYPE
18043
18044 gdb_assert_not_reached ("unable to find suitable integer type");
18045}
18046
11a8b164
AB
18047/* Find an integer type the same size as the address size given in the
18048 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18049 is unsigned or not. */
18050
18051static struct type *
18052dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18053 bool unsigned_p)
18054{
18055 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18056 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18057}
18058
b86352cf
AB
18059/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18060 present (which is valid) then compute the default type based on the
18061 compilation units address size. */
18062
18063static struct type *
18064read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18065{
18066 struct type *index_type = die_type (die, cu);
18067
18068 /* Dwarf-2 specifications explicitly allows to create subrange types
18069 without specifying a base type.
18070 In that case, the base type must be set to the type of
18071 the lower bound, upper bound or count, in that order, if any of these
18072 three attributes references an object that has a type.
18073 If no base type is found, the Dwarf-2 specifications say that
18074 a signed integer type of size equal to the size of an address should
18075 be used.
18076 For the following C code: `extern char gdb_int [];'
18077 GCC produces an empty range DIE.
18078 FIXME: muller/2010-05-28: Possible references to object for low bound,
18079 high bound or count are not yet handled by this code. */
18080 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 18081 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
18082
18083 return index_type;
18084}
18085
a02abb62
JB
18086/* Read the given DW_AT_subrange DIE. */
18087
f792889a 18088static struct type *
a02abb62
JB
18089read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18090{
4c9ad8c2 18091 struct type *base_type, *orig_base_type;
a02abb62
JB
18092 struct type *range_type;
18093 struct attribute *attr;
729efb13 18094 struct dynamic_prop low, high;
4fae6e18 18095 int low_default_is_valid;
c451ebe5 18096 int high_bound_is_count = 0;
15d034d0 18097 const char *name;
d359392f 18098 ULONGEST negative_mask;
e77813c8 18099
b86352cf
AB
18100 orig_base_type = read_subrange_index_type (die, cu);
18101
4c9ad8c2
TT
18102 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18103 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18104 creating the range type, but we use the result of check_typedef
18105 when examining properties of the type. */
18106 base_type = check_typedef (orig_base_type);
a02abb62 18107
7e314c57
JK
18108 /* The die_type call above may have already set the type for this DIE. */
18109 range_type = get_die_type (die, cu);
18110 if (range_type)
18111 return range_type;
18112
729efb13
SA
18113 low.kind = PROP_CONST;
18114 high.kind = PROP_CONST;
18115 high.data.const_val = 0;
18116
4fae6e18
JK
18117 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18118 omitting DW_AT_lower_bound. */
18119 switch (cu->language)
6e70227d 18120 {
4fae6e18
JK
18121 case language_c:
18122 case language_cplus:
729efb13 18123 low.data.const_val = 0;
4fae6e18
JK
18124 low_default_is_valid = 1;
18125 break;
18126 case language_fortran:
729efb13 18127 low.data.const_val = 1;
4fae6e18
JK
18128 low_default_is_valid = 1;
18129 break;
18130 case language_d:
4fae6e18 18131 case language_objc:
c44af4eb 18132 case language_rust:
729efb13 18133 low.data.const_val = 0;
4fae6e18
JK
18134 low_default_is_valid = (cu->header.version >= 4);
18135 break;
18136 case language_ada:
18137 case language_m2:
18138 case language_pascal:
729efb13 18139 low.data.const_val = 1;
4fae6e18
JK
18140 low_default_is_valid = (cu->header.version >= 4);
18141 break;
18142 default:
729efb13 18143 low.data.const_val = 0;
4fae6e18
JK
18144 low_default_is_valid = 0;
18145 break;
a02abb62
JB
18146 }
18147
e142c38c 18148 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18149 if (attr != nullptr)
9a49df9d 18150 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18151 else if (!low_default_is_valid)
b98664d3 18152 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18153 "- DIE at %s [in module %s]"),
18154 sect_offset_str (die->sect_off),
518817b3 18155 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18156
506f5c41
TV
18157 struct attribute *attr_ub, *attr_count;
18158 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18159 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18160 {
506f5c41 18161 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18162 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18163 {
c451ebe5
SA
18164 /* If bounds are constant do the final calculation here. */
18165 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18166 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18167 else
18168 high_bound_is_count = 1;
c2ff108b 18169 }
506f5c41
TV
18170 else
18171 {
18172 if (attr_ub != NULL)
18173 complaint (_("Unresolved DW_AT_upper_bound "
18174 "- DIE at %s [in module %s]"),
18175 sect_offset_str (die->sect_off),
18176 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18177 if (attr_count != NULL)
18178 complaint (_("Unresolved DW_AT_count "
18179 "- DIE at %s [in module %s]"),
18180 sect_offset_str (die->sect_off),
18181 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18182 }
e77813c8 18183 }
a02abb62 18184
4e962e74
TT
18185 LONGEST bias = 0;
18186 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18187 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18188 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18189
dbb9c2b1
JB
18190 /* Normally, the DWARF producers are expected to use a signed
18191 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18192 But this is unfortunately not always the case, as witnessed
18193 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18194 is used instead. To work around that ambiguity, we treat
18195 the bounds as signed, and thus sign-extend their values, when
18196 the base type is signed. */
6e70227d 18197 negative_mask =
d359392f 18198 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18199 if (low.kind == PROP_CONST
18200 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18201 low.data.const_val |= negative_mask;
18202 if (high.kind == PROP_CONST
18203 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18204 high.data.const_val |= negative_mask;
43bbcdc2 18205
5bbd8269
AB
18206 /* Check for bit and byte strides. */
18207 struct dynamic_prop byte_stride_prop;
18208 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18209 if (attr_byte_stride != nullptr)
18210 {
18211 struct type *prop_type
18212 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18213 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18214 prop_type);
18215 }
18216
18217 struct dynamic_prop bit_stride_prop;
18218 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18219 if (attr_bit_stride != nullptr)
18220 {
18221 /* It only makes sense to have either a bit or byte stride. */
18222 if (attr_byte_stride != nullptr)
18223 {
18224 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18225 "- DIE at %s [in module %s]"),
18226 sect_offset_str (die->sect_off),
18227 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18228 attr_bit_stride = nullptr;
18229 }
18230 else
18231 {
18232 struct type *prop_type
18233 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18234 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18235 prop_type);
18236 }
18237 }
18238
18239 if (attr_byte_stride != nullptr
18240 || attr_bit_stride != nullptr)
18241 {
18242 bool byte_stride_p = (attr_byte_stride != nullptr);
18243 struct dynamic_prop *stride
18244 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18245
18246 range_type
18247 = create_range_type_with_stride (NULL, orig_base_type, &low,
18248 &high, bias, stride, byte_stride_p);
18249 }
18250 else
18251 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18252
c451ebe5
SA
18253 if (high_bound_is_count)
18254 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18255
c2ff108b
JK
18256 /* Ada expects an empty array on no boundary attributes. */
18257 if (attr == NULL && cu->language != language_ada)
729efb13 18258 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18259
39cbfefa
DJ
18260 name = dwarf2_name (die, cu);
18261 if (name)
18262 TYPE_NAME (range_type) = name;
6e70227d 18263
e142c38c 18264 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18265 if (attr != nullptr)
a02abb62
JB
18266 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18267
2b4424c3
TT
18268 maybe_set_alignment (cu, die, range_type);
18269
7e314c57
JK
18270 set_die_type (die, range_type, cu);
18271
18272 /* set_die_type should be already done. */
b4ba55a1
JB
18273 set_descriptive_type (range_type, die, cu);
18274
7e314c57 18275 return range_type;
a02abb62 18276}
6e70227d 18277
f792889a 18278static struct type *
81a17f79
JB
18279read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18280{
18281 struct type *type;
81a17f79 18282
518817b3
SM
18283 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18284 NULL);
0114d602 18285 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18286
74a2f8ff 18287 /* In Ada, an unspecified type is typically used when the description
85102364 18288 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18289 such a type, we treat it as a stub, and try to resolve it later on,
18290 when needed. */
18291 if (cu->language == language_ada)
18292 TYPE_STUB (type) = 1;
18293
f792889a 18294 return set_die_type (die, type, cu);
81a17f79 18295}
a02abb62 18296
639d11d3
DC
18297/* Read a single die and all its descendents. Set the die's sibling
18298 field to NULL; set other fields in the die correctly, and set all
18299 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18300 location of the info_ptr after reading all of those dies. PARENT
18301 is the parent of the die in question. */
18302
18303static struct die_info *
dee91e82 18304read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18305 const gdb_byte *info_ptr,
18306 const gdb_byte **new_info_ptr,
dee91e82 18307 struct die_info *parent)
639d11d3
DC
18308{
18309 struct die_info *die;
d521ce57 18310 const gdb_byte *cur_ptr;
639d11d3
DC
18311 int has_children;
18312
bf6af496 18313 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18314 if (die == NULL)
18315 {
18316 *new_info_ptr = cur_ptr;
18317 return NULL;
18318 }
93311388 18319 store_in_ref_table (die, reader->cu);
639d11d3
DC
18320
18321 if (has_children)
bf6af496 18322 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18323 else
18324 {
18325 die->child = NULL;
18326 *new_info_ptr = cur_ptr;
18327 }
18328
18329 die->sibling = NULL;
18330 die->parent = parent;
18331 return die;
18332}
18333
18334/* Read a die, all of its descendents, and all of its siblings; set
18335 all of the fields of all of the dies correctly. Arguments are as
18336 in read_die_and_children. */
18337
18338static struct die_info *
bf6af496 18339read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18340 const gdb_byte *info_ptr,
18341 const gdb_byte **new_info_ptr,
bf6af496 18342 struct die_info *parent)
639d11d3
DC
18343{
18344 struct die_info *first_die, *last_sibling;
d521ce57 18345 const gdb_byte *cur_ptr;
639d11d3 18346
c906108c 18347 cur_ptr = info_ptr;
639d11d3
DC
18348 first_die = last_sibling = NULL;
18349
18350 while (1)
c906108c 18351 {
639d11d3 18352 struct die_info *die
dee91e82 18353 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18354
1d325ec1 18355 if (die == NULL)
c906108c 18356 {
639d11d3
DC
18357 *new_info_ptr = cur_ptr;
18358 return first_die;
c906108c 18359 }
1d325ec1
DJ
18360
18361 if (!first_die)
18362 first_die = die;
c906108c 18363 else
1d325ec1
DJ
18364 last_sibling->sibling = die;
18365
18366 last_sibling = die;
c906108c 18367 }
c906108c
SS
18368}
18369
bf6af496
DE
18370/* Read a die, all of its descendents, and all of its siblings; set
18371 all of the fields of all of the dies correctly. Arguments are as
18372 in read_die_and_children.
18373 This the main entry point for reading a DIE and all its children. */
18374
18375static struct die_info *
18376read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18377 const gdb_byte *info_ptr,
18378 const gdb_byte **new_info_ptr,
bf6af496
DE
18379 struct die_info *parent)
18380{
18381 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18382 new_info_ptr, parent);
18383
b4f54984 18384 if (dwarf_die_debug)
bf6af496
DE
18385 {
18386 fprintf_unfiltered (gdb_stdlog,
18387 "Read die from %s@0x%x of %s:\n",
a32a8923 18388 get_section_name (reader->die_section),
bf6af496
DE
18389 (unsigned) (info_ptr - reader->die_section->buffer),
18390 bfd_get_filename (reader->abfd));
b4f54984 18391 dump_die (die, dwarf_die_debug);
bf6af496
DE
18392 }
18393
18394 return die;
18395}
18396
3019eac3
DE
18397/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18398 attributes.
18399 The caller is responsible for filling in the extra attributes
18400 and updating (*DIEP)->num_attrs.
18401 Set DIEP to point to a newly allocated die with its information,
18402 except for its child, sibling, and parent fields.
18403 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18404
d521ce57 18405static const gdb_byte *
3019eac3 18406read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18407 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18408 int *has_children, int num_extra_attrs)
93311388 18409{
b64f50a1 18410 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18411 struct abbrev_info *abbrev;
18412 struct die_info *die;
18413 struct dwarf2_cu *cu = reader->cu;
18414 bfd *abfd = reader->abfd;
18415
9c541725 18416 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18417 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18418 info_ptr += bytes_read;
18419 if (!abbrev_number)
18420 {
18421 *diep = NULL;
18422 *has_children = 0;
18423 return info_ptr;
18424 }
18425
685af9cd 18426 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18427 if (!abbrev)
348e048f
DE
18428 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18429 abbrev_number,
18430 bfd_get_filename (abfd));
18431
3019eac3 18432 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18433 die->sect_off = sect_off;
93311388
DE
18434 die->tag = abbrev->tag;
18435 die->abbrev = abbrev_number;
18436
3019eac3
DE
18437 /* Make the result usable.
18438 The caller needs to update num_attrs after adding the extra
18439 attributes. */
93311388
DE
18440 die->num_attrs = abbrev->num_attrs;
18441
18a8505e 18442 std::vector<int> indexes_that_need_reprocess;
93311388 18443 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18444 {
18445 bool need_reprocess;
18446 info_ptr =
18447 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18448 info_ptr, &need_reprocess);
18449 if (need_reprocess)
18450 indexes_that_need_reprocess.push_back (i);
18451 }
18452
18453 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18454 if (attr != nullptr)
18455 cu->str_offsets_base = DW_UNSND (attr);
93311388 18456
18a8505e
AT
18457 auto maybe_addr_base = lookup_addr_base(die);
18458 if (maybe_addr_base.has_value ())
18459 cu->addr_base = *maybe_addr_base;
18460 for (int index : indexes_that_need_reprocess)
18461 read_attribute_reprocess (reader, &die->attrs[index]);
93311388
DE
18462 *diep = die;
18463 *has_children = abbrev->has_children;
18464 return info_ptr;
18465}
18466
3019eac3
DE
18467/* Read a die and all its attributes.
18468 Set DIEP to point to a newly allocated die with its information,
18469 except for its child, sibling, and parent fields.
18470 Set HAS_CHILDREN to tell whether the die has children or not. */
18471
d521ce57 18472static const gdb_byte *
3019eac3 18473read_full_die (const struct die_reader_specs *reader,
d521ce57 18474 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18475 int *has_children)
18476{
d521ce57 18477 const gdb_byte *result;
bf6af496
DE
18478
18479 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18480
b4f54984 18481 if (dwarf_die_debug)
bf6af496
DE
18482 {
18483 fprintf_unfiltered (gdb_stdlog,
18484 "Read die from %s@0x%x of %s:\n",
a32a8923 18485 get_section_name (reader->die_section),
bf6af496
DE
18486 (unsigned) (info_ptr - reader->die_section->buffer),
18487 bfd_get_filename (reader->abfd));
b4f54984 18488 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18489 }
18490
18491 return result;
3019eac3 18492}
433df2d4
DE
18493\f
18494/* Abbreviation tables.
3019eac3 18495
433df2d4 18496 In DWARF version 2, the description of the debugging information is
c906108c
SS
18497 stored in a separate .debug_abbrev section. Before we read any
18498 dies from a section we read in all abbreviations and install them
433df2d4
DE
18499 in a hash table. */
18500
18501/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18502
685af9cd
TT
18503struct abbrev_info *
18504abbrev_table::alloc_abbrev ()
433df2d4
DE
18505{
18506 struct abbrev_info *abbrev;
18507
685af9cd 18508 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18509 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18510
433df2d4
DE
18511 return abbrev;
18512}
18513
18514/* Add an abbreviation to the table. */
c906108c 18515
685af9cd
TT
18516void
18517abbrev_table::add_abbrev (unsigned int abbrev_number,
18518 struct abbrev_info *abbrev)
433df2d4
DE
18519{
18520 unsigned int hash_number;
18521
18522 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18523 abbrev->next = m_abbrevs[hash_number];
18524 m_abbrevs[hash_number] = abbrev;
433df2d4 18525}
dee91e82 18526
433df2d4
DE
18527/* Look up an abbrev in the table.
18528 Returns NULL if the abbrev is not found. */
18529
685af9cd
TT
18530struct abbrev_info *
18531abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18532{
433df2d4
DE
18533 unsigned int hash_number;
18534 struct abbrev_info *abbrev;
18535
18536 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18537 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18538
18539 while (abbrev)
18540 {
18541 if (abbrev->number == abbrev_number)
18542 return abbrev;
18543 abbrev = abbrev->next;
18544 }
18545 return NULL;
18546}
18547
18548/* Read in an abbrev table. */
18549
685af9cd 18550static abbrev_table_up
ed2dc618
SM
18551abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18552 struct dwarf2_section_info *section,
9c541725 18553 sect_offset sect_off)
433df2d4
DE
18554{
18555 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18556 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18557 const gdb_byte *abbrev_ptr;
c906108c
SS
18558 struct abbrev_info *cur_abbrev;
18559 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18560 unsigned int abbrev_form;
6dfa2fc2 18561 std::vector<struct attr_abbrev> cur_attrs;
c906108c 18562
685af9cd 18563 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18564
433df2d4 18565 dwarf2_read_section (objfile, section);
9c541725 18566 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18567 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18568 abbrev_ptr += bytes_read;
18569
0963b4bd 18570 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18571 while (abbrev_number)
18572 {
6dfa2fc2 18573 cur_attrs.clear ();
685af9cd 18574 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18575
18576 /* read in abbrev header */
18577 cur_abbrev->number = abbrev_number;
aead7601
SM
18578 cur_abbrev->tag
18579 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18580 abbrev_ptr += bytes_read;
18581 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18582 abbrev_ptr += 1;
18583
18584 /* now read in declarations */
22d2f3ab 18585 for (;;)
c906108c 18586 {
43988095
JK
18587 LONGEST implicit_const;
18588
22d2f3ab
JK
18589 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18590 abbrev_ptr += bytes_read;
18591 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18592 abbrev_ptr += bytes_read;
43988095
JK
18593 if (abbrev_form == DW_FORM_implicit_const)
18594 {
18595 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18596 &bytes_read);
18597 abbrev_ptr += bytes_read;
18598 }
18599 else
18600 {
18601 /* Initialize it due to a false compiler warning. */
18602 implicit_const = -1;
18603 }
22d2f3ab
JK
18604
18605 if (abbrev_name == 0)
18606 break;
18607
6dfa2fc2
TT
18608 cur_attrs.emplace_back ();
18609 struct attr_abbrev &cur_attr = cur_attrs.back ();
18610 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18611 cur_attr.form = (enum dwarf_form) abbrev_form;
18612 cur_attr.implicit_const = implicit_const;
22d2f3ab 18613 ++cur_abbrev->num_attrs;
c906108c
SS
18614 }
18615
8d749320
SM
18616 cur_abbrev->attrs =
18617 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18618 cur_abbrev->num_attrs);
6dfa2fc2 18619 memcpy (cur_abbrev->attrs, cur_attrs.data (),
f3dd6933
DJ
18620 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18621
685af9cd 18622 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18623
18624 /* Get next abbreviation.
18625 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18626 always properly terminated with an abbrev number of 0.
18627 Exit loop if we encounter an abbreviation which we have
18628 already read (which means we are about to read the abbreviations
18629 for the next compile unit) or if the end of the abbreviation
18630 table is reached. */
433df2d4 18631 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18632 break;
18633 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18634 abbrev_ptr += bytes_read;
685af9cd 18635 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18636 break;
18637 }
f3dd6933 18638
433df2d4 18639 return abbrev_table;
c906108c
SS
18640}
18641
72bf9492
DJ
18642/* Returns nonzero if TAG represents a type that we might generate a partial
18643 symbol for. */
18644
18645static int
18646is_type_tag_for_partial (int tag)
18647{
18648 switch (tag)
18649 {
18650#if 0
18651 /* Some types that would be reasonable to generate partial symbols for,
18652 that we don't at present. */
18653 case DW_TAG_array_type:
18654 case DW_TAG_file_type:
18655 case DW_TAG_ptr_to_member_type:
18656 case DW_TAG_set_type:
18657 case DW_TAG_string_type:
18658 case DW_TAG_subroutine_type:
18659#endif
18660 case DW_TAG_base_type:
18661 case DW_TAG_class_type:
680b30c7 18662 case DW_TAG_interface_type:
72bf9492
DJ
18663 case DW_TAG_enumeration_type:
18664 case DW_TAG_structure_type:
18665 case DW_TAG_subrange_type:
18666 case DW_TAG_typedef:
18667 case DW_TAG_union_type:
18668 return 1;
18669 default:
18670 return 0;
18671 }
18672}
18673
18674/* Load all DIEs that are interesting for partial symbols into memory. */
18675
18676static struct partial_die_info *
dee91e82 18677load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18678 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18679{
dee91e82 18680 struct dwarf2_cu *cu = reader->cu;
518817b3 18681 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18682 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18683 unsigned int bytes_read;
5afb4e99 18684 unsigned int load_all = 0;
72bf9492
DJ
18685 int nesting_level = 1;
18686
18687 parent_die = NULL;
18688 last_die = NULL;
18689
7adf1e79
DE
18690 gdb_assert (cu->per_cu != NULL);
18691 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18692 load_all = 1;
18693
72bf9492
DJ
18694 cu->partial_dies
18695 = htab_create_alloc_ex (cu->header.length / 12,
18696 partial_die_hash,
18697 partial_die_eq,
18698 NULL,
18699 &cu->comp_unit_obstack,
18700 hashtab_obstack_allocate,
18701 dummy_obstack_deallocate);
18702
72bf9492
DJ
18703 while (1)
18704 {
685af9cd 18705 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18706
18707 /* A NULL abbrev means the end of a series of children. */
18708 if (abbrev == NULL)
18709 {
18710 if (--nesting_level == 0)
cd9983dd
YQ
18711 return first_die;
18712
72bf9492
DJ
18713 info_ptr += bytes_read;
18714 last_die = parent_die;
18715 parent_die = parent_die->die_parent;
18716 continue;
18717 }
18718
98bfdba5
PA
18719 /* Check for template arguments. We never save these; if
18720 they're seen, we just mark the parent, and go on our way. */
18721 if (parent_die != NULL
18722 && cu->language == language_cplus
18723 && (abbrev->tag == DW_TAG_template_type_param
18724 || abbrev->tag == DW_TAG_template_value_param))
18725 {
18726 parent_die->has_template_arguments = 1;
18727
18728 if (!load_all)
18729 {
18730 /* We don't need a partial DIE for the template argument. */
dee91e82 18731 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18732 continue;
18733 }
18734 }
18735
0d99eb77 18736 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18737 Skip their other children. */
18738 if (!load_all
18739 && cu->language == language_cplus
18740 && parent_die != NULL
18741 && parent_die->tag == DW_TAG_subprogram)
18742 {
dee91e82 18743 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18744 continue;
18745 }
18746
5afb4e99
DJ
18747 /* Check whether this DIE is interesting enough to save. Normally
18748 we would not be interested in members here, but there may be
18749 later variables referencing them via DW_AT_specification (for
18750 static members). */
18751 if (!load_all
18752 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18753 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18754 && abbrev->tag != DW_TAG_enumerator
18755 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18756 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18757 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18758 && abbrev->tag != DW_TAG_variable
5afb4e99 18759 && abbrev->tag != DW_TAG_namespace
f55ee35c 18760 && abbrev->tag != DW_TAG_module
95554aad 18761 && abbrev->tag != DW_TAG_member
74921315
KS
18762 && abbrev->tag != DW_TAG_imported_unit
18763 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18764 {
18765 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18766 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18767 continue;
18768 }
18769
6f06d47b
YQ
18770 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18771 abbrev);
cd9983dd 18772
48fbe735 18773 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18774
18775 /* This two-pass algorithm for processing partial symbols has a
18776 high cost in cache pressure. Thus, handle some simple cases
18777 here which cover the majority of C partial symbols. DIEs
18778 which neither have specification tags in them, nor could have
18779 specification tags elsewhere pointing at them, can simply be
18780 processed and discarded.
18781
18782 This segment is also optional; scan_partial_symbols and
18783 add_partial_symbol will handle these DIEs if we chain
18784 them in normally. When compilers which do not emit large
18785 quantities of duplicate debug information are more common,
18786 this code can probably be removed. */
18787
18788 /* Any complete simple types at the top level (pretty much all
18789 of them, for a language without namespaces), can be processed
18790 directly. */
18791 if (parent_die == NULL
cd9983dd
YQ
18792 && pdi.has_specification == 0
18793 && pdi.is_declaration == 0
18794 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18795 || pdi.tag == DW_TAG_base_type
18796 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18797 {
cd9983dd 18798 if (building_psymtab && pdi.name != NULL)
31edb802 18799 add_psymbol_to_list (pdi.name, false,
79748972 18800 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18801 psymbol_placement::STATIC,
1762568f 18802 0, cu->language, objfile);
cd9983dd 18803 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18804 continue;
18805 }
18806
d8228535
JK
18807 /* The exception for DW_TAG_typedef with has_children above is
18808 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18809 type_name_or_error will error on such types later.
d8228535
JK
18810
18811 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18812 it could not find the child DIEs referenced later, this is checked
18813 above. In correct DWARF DW_TAG_typedef should have no children. */
18814
cd9983dd 18815 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18816 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18817 "- DIE at %s [in module %s]"),
cd9983dd 18818 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18819
72bf9492
DJ
18820 /* If we're at the second level, and we're an enumerator, and
18821 our parent has no specification (meaning possibly lives in a
18822 namespace elsewhere), then we can add the partial symbol now
18823 instead of queueing it. */
cd9983dd 18824 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18825 && parent_die != NULL
18826 && parent_die->die_parent == NULL
18827 && parent_die->tag == DW_TAG_enumeration_type
18828 && parent_die->has_specification == 0)
18829 {
cd9983dd 18830 if (pdi.name == NULL)
b98664d3 18831 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18832 else if (building_psymtab)
31edb802 18833 add_psymbol_to_list (pdi.name, false,
79748972 18834 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18835 cu->language == language_cplus
75aedd27
TT
18836 ? psymbol_placement::GLOBAL
18837 : psymbol_placement::STATIC,
1762568f 18838 0, cu->language, objfile);
72bf9492 18839
cd9983dd 18840 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18841 continue;
18842 }
18843
cd9983dd 18844 struct partial_die_info *part_die
6f06d47b 18845 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18846
72bf9492
DJ
18847 /* We'll save this DIE so link it in. */
18848 part_die->die_parent = parent_die;
18849 part_die->die_sibling = NULL;
18850 part_die->die_child = NULL;
18851
18852 if (last_die && last_die == parent_die)
18853 last_die->die_child = part_die;
18854 else if (last_die)
18855 last_die->die_sibling = part_die;
18856
18857 last_die = part_die;
18858
18859 if (first_die == NULL)
18860 first_die = part_die;
18861
18862 /* Maybe add the DIE to the hash table. Not all DIEs that we
18863 find interesting need to be in the hash table, because we
18864 also have the parent/sibling/child chains; only those that we
18865 might refer to by offset later during partial symbol reading.
18866
18867 For now this means things that might have be the target of a
18868 DW_AT_specification, DW_AT_abstract_origin, or
18869 DW_AT_extension. DW_AT_extension will refer only to
18870 namespaces; DW_AT_abstract_origin refers to functions (and
18871 many things under the function DIE, but we do not recurse
18872 into function DIEs during partial symbol reading) and
18873 possibly variables as well; DW_AT_specification refers to
18874 declarations. Declarations ought to have the DW_AT_declaration
18875 flag. It happens that GCC forgets to put it in sometimes, but
18876 only for functions, not for types.
18877
18878 Adding more things than necessary to the hash table is harmless
18879 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18880 wasted time in find_partial_die, when we reread the compilation
18881 unit with load_all_dies set. */
72bf9492 18882
5afb4e99 18883 if (load_all
72929c62 18884 || abbrev->tag == DW_TAG_constant
5afb4e99 18885 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18886 || abbrev->tag == DW_TAG_variable
18887 || abbrev->tag == DW_TAG_namespace
18888 || part_die->is_declaration)
18889 {
18890 void **slot;
18891
18892 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18893 to_underlying (part_die->sect_off),
18894 INSERT);
72bf9492
DJ
18895 *slot = part_die;
18896 }
18897
72bf9492 18898 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18899 we have no reason to follow the children of structures; for other
98bfdba5
PA
18900 languages we have to, so that we can get at method physnames
18901 to infer fully qualified class names, for DW_AT_specification,
18902 and for C++ template arguments. For C++, we also look one level
18903 inside functions to find template arguments (if the name of the
18904 function does not already contain the template arguments).
bc30ff58 18905
0a4b0913
AB
18906 For Ada and Fortran, we need to scan the children of subprograms
18907 and lexical blocks as well because these languages allow the
18908 definition of nested entities that could be interesting for the
18909 debugger, such as nested subprograms for instance. */
72bf9492 18910 if (last_die->has_children
5afb4e99
DJ
18911 && (load_all
18912 || last_die->tag == DW_TAG_namespace
f55ee35c 18913 || last_die->tag == DW_TAG_module
72bf9492 18914 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18915 || (cu->language == language_cplus
18916 && last_die->tag == DW_TAG_subprogram
18917 && (last_die->name == NULL
18918 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18919 || (cu->language != language_c
18920 && (last_die->tag == DW_TAG_class_type
680b30c7 18921 || last_die->tag == DW_TAG_interface_type
72bf9492 18922 || last_die->tag == DW_TAG_structure_type
bc30ff58 18923 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18924 || ((cu->language == language_ada
18925 || cu->language == language_fortran)
bc30ff58
JB
18926 && (last_die->tag == DW_TAG_subprogram
18927 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18928 {
18929 nesting_level++;
18930 parent_die = last_die;
18931 continue;
18932 }
18933
18934 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18935 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18936
18937 /* Back to the top, do it again. */
18938 }
18939}
18940
6f06d47b
YQ
18941partial_die_info::partial_die_info (sect_offset sect_off_,
18942 struct abbrev_info *abbrev)
18943 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18944{
18945}
18946
35cc7ed7
YQ
18947/* Read a minimal amount of information into the minimal die structure.
18948 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18949
48fbe735
YQ
18950const gdb_byte *
18951partial_die_info::read (const struct die_reader_specs *reader,
18952 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18953{
dee91e82 18954 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18955 struct dwarf2_per_objfile *dwarf2_per_objfile
18956 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18957 unsigned int i;
c5aa993b 18958 int has_low_pc_attr = 0;
c906108c 18959 int has_high_pc_attr = 0;
91da1414 18960 int high_pc_relative = 0;
c906108c 18961
18a8505e 18962 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 18963 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18964 {
18a8505e
AT
18965 bool need_reprocess;
18966 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18967 info_ptr, &need_reprocess);
18968 /* String and address offsets that need to do the reprocessing have
18969 already been read at this point, so there is no need to wait until
18970 the loop terminates to do the reprocessing. */
18971 if (need_reprocess)
18972 read_attribute_reprocess (reader, &attr_vec[i]);
18973 attribute &attr = attr_vec[i];
c906108c 18974 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18975 partial symbol table. */
c906108c
SS
18976 switch (attr.name)
18977 {
18978 case DW_AT_name:
48fbe735 18979 switch (tag)
71c25dea
TT
18980 {
18981 case DW_TAG_compile_unit:
95554aad 18982 case DW_TAG_partial_unit:
348e048f 18983 case DW_TAG_type_unit:
71c25dea
TT
18984 /* Compilation units have a DW_AT_name that is a filename, not
18985 a source language identifier. */
18986 case DW_TAG_enumeration_type:
18987 case DW_TAG_enumerator:
18988 /* These tags always have simple identifiers already; no need
18989 to canonicalize them. */
48fbe735 18990 name = DW_STRING (&attr);
71c25dea
TT
18991 break;
18992 default:
48fbe735
YQ
18993 {
18994 struct objfile *objfile = dwarf2_per_objfile->objfile;
18995
18996 name
18997 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18998 &objfile->per_bfd->storage_obstack);
18999 }
71c25dea
TT
19000 break;
19001 }
c906108c 19002 break;
31ef98ae 19003 case DW_AT_linkage_name:
c906108c 19004 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19005 /* Note that both forms of linkage name might appear. We
19006 assume they will be the same, and we only store the last
19007 one we see. */
48fbe735 19008 linkage_name = DW_STRING (&attr);
c906108c
SS
19009 break;
19010 case DW_AT_low_pc:
19011 has_low_pc_attr = 1;
48fbe735 19012 lowpc = attr_value_as_address (&attr);
c906108c
SS
19013 break;
19014 case DW_AT_high_pc:
19015 has_high_pc_attr = 1;
48fbe735 19016 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
19017 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19018 high_pc_relative = 1;
c906108c
SS
19019 break;
19020 case DW_AT_location:
0963b4bd 19021 /* Support the .debug_loc offsets. */
8e19ed76
PS
19022 if (attr_form_is_block (&attr))
19023 {
48fbe735 19024 d.locdesc = DW_BLOCK (&attr);
8e19ed76 19025 }
3690dd37 19026 else if (attr_form_is_section_offset (&attr))
8e19ed76 19027 {
4d3c2250 19028 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
19029 }
19030 else
19031 {
4d3c2250
KB
19032 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19033 "partial symbol information");
8e19ed76 19034 }
c906108c 19035 break;
c906108c 19036 case DW_AT_external:
48fbe735 19037 is_external = DW_UNSND (&attr);
c906108c
SS
19038 break;
19039 case DW_AT_declaration:
48fbe735 19040 is_declaration = DW_UNSND (&attr);
c906108c
SS
19041 break;
19042 case DW_AT_type:
48fbe735 19043 has_type = 1;
c906108c
SS
19044 break;
19045 case DW_AT_abstract_origin:
19046 case DW_AT_specification:
72bf9492 19047 case DW_AT_extension:
48fbe735
YQ
19048 has_specification = 1;
19049 spec_offset = dwarf2_get_ref_die_offset (&attr);
19050 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19051 || cu->per_cu->is_dwz);
c906108c
SS
19052 break;
19053 case DW_AT_sibling:
19054 /* Ignore absolute siblings, they might point outside of
19055 the current compile unit. */
19056 if (attr.form == DW_FORM_ref_addr)
b98664d3 19057 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19058 else
b9502d3f 19059 {
48fbe735 19060 const gdb_byte *buffer = reader->buffer;
9c541725
PA
19061 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19062 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19063
19064 if (sibling_ptr < info_ptr)
b98664d3 19065 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
19066 else if (sibling_ptr > reader->buffer_end)
19067 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19068 else
48fbe735 19069 sibling = sibling_ptr;
b9502d3f 19070 }
c906108c 19071 break;
fa4028e9 19072 case DW_AT_byte_size:
48fbe735 19073 has_byte_size = 1;
fa4028e9 19074 break;
ff908ebf 19075 case DW_AT_const_value:
48fbe735 19076 has_const_value = 1;
ff908ebf 19077 break;
68511cec
CES
19078 case DW_AT_calling_convention:
19079 /* DWARF doesn't provide a way to identify a program's source-level
19080 entry point. DW_AT_calling_convention attributes are only meant
19081 to describe functions' calling conventions.
19082
19083 However, because it's a necessary piece of information in
0c1b455e
TT
19084 Fortran, and before DWARF 4 DW_CC_program was the only
19085 piece of debugging information whose definition refers to
19086 a 'main program' at all, several compilers marked Fortran
19087 main programs with DW_CC_program --- even when those
19088 functions use the standard calling conventions.
19089
19090 Although DWARF now specifies a way to provide this
19091 information, we support this practice for backward
19092 compatibility. */
68511cec 19093 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19094 && cu->language == language_fortran)
48fbe735 19095 main_subprogram = 1;
68511cec 19096 break;
481860b3
GB
19097 case DW_AT_inline:
19098 if (DW_UNSND (&attr) == DW_INL_inlined
19099 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19100 may_be_inlined = 1;
481860b3 19101 break;
95554aad
TT
19102
19103 case DW_AT_import:
48fbe735 19104 if (tag == DW_TAG_imported_unit)
36586728 19105 {
48fbe735
YQ
19106 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19107 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19108 || cu->per_cu->is_dwz);
19109 }
95554aad
TT
19110 break;
19111
0c1b455e 19112 case DW_AT_main_subprogram:
48fbe735 19113 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19114 break;
19115
05caa1d2
TT
19116 case DW_AT_ranges:
19117 {
19118 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19119 but that requires a full DIE, so instead we just
19120 reimplement it. */
19121 int need_ranges_base = tag != DW_TAG_compile_unit;
19122 unsigned int ranges_offset = (DW_UNSND (&attr)
19123 + (need_ranges_base
19124 ? cu->ranges_base
19125 : 0));
19126
19127 /* Value of the DW_AT_ranges attribute is the offset in the
19128 .debug_ranges section. */
19129 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19130 nullptr))
19131 has_pc_info = 1;
19132 }
19133 break;
19134
c906108c
SS
19135 default:
19136 break;
19137 }
19138 }
19139
10d06d82
TT
19140 /* For Ada, if both the name and the linkage name appear, we prefer
19141 the latter. This lets "catch exception" work better, regardless
19142 of the order in which the name and linkage name were emitted.
19143 Really, though, this is just a workaround for the fact that gdb
19144 doesn't store both the name and the linkage name. */
19145 if (cu->language == language_ada && linkage_name != nullptr)
19146 name = linkage_name;
19147
91da1414 19148 if (high_pc_relative)
48fbe735 19149 highpc += lowpc;
91da1414 19150
9373cf26
JK
19151 if (has_low_pc_attr && has_high_pc_attr)
19152 {
19153 /* When using the GNU linker, .gnu.linkonce. sections are used to
19154 eliminate duplicate copies of functions and vtables and such.
19155 The linker will arbitrarily choose one and discard the others.
19156 The AT_*_pc values for such functions refer to local labels in
19157 these sections. If the section from that file was discarded, the
19158 labels are not in the output, so the relocs get a value of 0.
19159 If this is a discarded function, mark the pc bounds as invalid,
19160 so that GDB will ignore it. */
48fbe735 19161 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19162 {
48fbe735 19163 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19164 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19165
b98664d3 19166 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19167 "for DIE at %s [in module %s]"),
48fbe735
YQ
19168 paddress (gdbarch, lowpc),
19169 sect_offset_str (sect_off),
9d8780f0 19170 objfile_name (objfile));
9373cf26
JK
19171 }
19172 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19173 else if (lowpc >= highpc)
9373cf26 19174 {
48fbe735 19175 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19176 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19177
b98664d3 19178 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19179 "for DIE at %s [in module %s]"),
48fbe735
YQ
19180 paddress (gdbarch, lowpc),
19181 paddress (gdbarch, highpc),
19182 sect_offset_str (sect_off),
9c541725 19183 objfile_name (objfile));
9373cf26
JK
19184 }
19185 else
48fbe735 19186 has_pc_info = 1;
9373cf26 19187 }
85cbf3d3 19188
c906108c
SS
19189 return info_ptr;
19190}
19191
72bf9492
DJ
19192/* Find a cached partial DIE at OFFSET in CU. */
19193
d590ff25
YQ
19194struct partial_die_info *
19195dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19196{
19197 struct partial_die_info *lookup_die = NULL;
6f06d47b 19198 struct partial_die_info part_die (sect_off);
72bf9492 19199
9a3c8263 19200 lookup_die = ((struct partial_die_info *)
d590ff25 19201 htab_find_with_hash (partial_dies, &part_die,
9c541725 19202 to_underlying (sect_off)));
72bf9492 19203
72bf9492
DJ
19204 return lookup_die;
19205}
19206
348e048f
DE
19207/* Find a partial DIE at OFFSET, which may or may not be in CU,
19208 except in the case of .debug_types DIEs which do not reference
19209 outside their CU (they do however referencing other types via
55f1336d 19210 DW_FORM_ref_sig8). */
72bf9492 19211
122cf0f2 19212static const struct cu_partial_die_info
9c541725 19213find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19214{
518817b3
SM
19215 struct dwarf2_per_objfile *dwarf2_per_objfile
19216 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19217 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19218 struct dwarf2_per_cu_data *per_cu = NULL;
19219 struct partial_die_info *pd = NULL;
72bf9492 19220
36586728 19221 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19222 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19223 {
d590ff25 19224 pd = cu->find_partial_die (sect_off);
5afb4e99 19225 if (pd != NULL)
fb816e8b 19226 return { cu, pd };
0d99eb77
DE
19227 /* We missed recording what we needed.
19228 Load all dies and try again. */
19229 per_cu = cu->per_cu;
5afb4e99 19230 }
0d99eb77
DE
19231 else
19232 {
19233 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19234 if (cu->per_cu->is_debug_types)
0d99eb77 19235 {
9d8780f0
SM
19236 error (_("Dwarf Error: Type Unit at offset %s contains"
19237 " external reference to offset %s [in module %s].\n"),
19238 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19239 bfd_get_filename (objfile->obfd));
19240 }
9c541725 19241 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19242 dwarf2_per_objfile);
72bf9492 19243
0d99eb77
DE
19244 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19245 load_partial_comp_unit (per_cu);
ae038cb0 19246
0d99eb77 19247 per_cu->cu->last_used = 0;
d590ff25 19248 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19249 }
5afb4e99 19250
dee91e82
DE
19251 /* If we didn't find it, and not all dies have been loaded,
19252 load them all and try again. */
19253
5afb4e99
DJ
19254 if (pd == NULL && per_cu->load_all_dies == 0)
19255 {
5afb4e99 19256 per_cu->load_all_dies = 1;
fd820528
DE
19257
19258 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19259 THIS_CU->cu may already be in use. So we can't just free it and
19260 replace its DIEs with the ones we read in. Instead, we leave those
19261 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19262 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19263 set. */
dee91e82 19264 load_partial_comp_unit (per_cu);
5afb4e99 19265
d590ff25 19266 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19267 }
19268
19269 if (pd == NULL)
19270 internal_error (__FILE__, __LINE__,
9d8780f0 19271 _("could not find partial DIE %s "
3e43a32a 19272 "in cache [from module %s]\n"),
9d8780f0 19273 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19274 return { per_cu->cu, pd };
72bf9492
DJ
19275}
19276
abc72ce4
DE
19277/* See if we can figure out if the class lives in a namespace. We do
19278 this by looking for a member function; its demangled name will
19279 contain namespace info, if there is any. */
19280
19281static void
19282guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19283 struct dwarf2_cu *cu)
19284{
19285 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19286 what template types look like, because the demangler
19287 frequently doesn't give the same name as the debug info. We
19288 could fix this by only using the demangled name to get the
19289 prefix (but see comment in read_structure_type). */
19290
19291 struct partial_die_info *real_pdi;
19292 struct partial_die_info *child_pdi;
19293
19294 /* If this DIE (this DIE's specification, if any) has a parent, then
19295 we should not do this. We'll prepend the parent's fully qualified
19296 name when we create the partial symbol. */
19297
19298 real_pdi = struct_pdi;
19299 while (real_pdi->has_specification)
fb816e8b 19300 {
122cf0f2
AB
19301 auto res = find_partial_die (real_pdi->spec_offset,
19302 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19303 real_pdi = res.pdi;
19304 cu = res.cu;
19305 }
abc72ce4
DE
19306
19307 if (real_pdi->die_parent != NULL)
19308 return;
19309
19310 for (child_pdi = struct_pdi->die_child;
19311 child_pdi != NULL;
19312 child_pdi = child_pdi->die_sibling)
19313 {
19314 if (child_pdi->tag == DW_TAG_subprogram
19315 && child_pdi->linkage_name != NULL)
19316 {
43816ebc
TT
19317 gdb::unique_xmalloc_ptr<char> actual_class_name
19318 (language_class_name_from_physname (cu->language_defn,
19319 child_pdi->linkage_name));
abc72ce4
DE
19320 if (actual_class_name != NULL)
19321 {
518817b3 19322 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19323 struct_pdi->name
021887d8 19324 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 19325 actual_class_name.get ());
abc72ce4
DE
19326 }
19327 break;
19328 }
19329 }
19330}
19331
52356b79
YQ
19332void
19333partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19334{
abc72ce4
DE
19335 /* Once we've fixed up a die, there's no point in doing so again.
19336 This also avoids a memory leak if we were to call
19337 guess_partial_die_structure_name multiple times. */
52356b79 19338 if (fixup_called)
abc72ce4
DE
19339 return;
19340
72bf9492
DJ
19341 /* If we found a reference attribute and the DIE has no name, try
19342 to find a name in the referred to DIE. */
19343
52356b79 19344 if (name == NULL && has_specification)
72bf9492
DJ
19345 {
19346 struct partial_die_info *spec_die;
72bf9492 19347
122cf0f2 19348 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19349 spec_die = res.pdi;
19350 cu = res.cu;
72bf9492 19351
52356b79 19352 spec_die->fixup (cu);
72bf9492
DJ
19353
19354 if (spec_die->name)
19355 {
52356b79 19356 name = spec_die->name;
72bf9492
DJ
19357
19358 /* Copy DW_AT_external attribute if it is set. */
19359 if (spec_die->is_external)
52356b79 19360 is_external = spec_die->is_external;
72bf9492
DJ
19361 }
19362 }
19363
19364 /* Set default names for some unnamed DIEs. */
72bf9492 19365
52356b79
YQ
19366 if (name == NULL && tag == DW_TAG_namespace)
19367 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19368
abc72ce4
DE
19369 /* If there is no parent die to provide a namespace, and there are
19370 children, see if we can determine the namespace from their linkage
122d1940 19371 name. */
abc72ce4 19372 if (cu->language == language_cplus
fd5866f6 19373 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19374 && die_parent == NULL
19375 && has_children
19376 && (tag == DW_TAG_class_type
19377 || tag == DW_TAG_structure_type
19378 || tag == DW_TAG_union_type))
19379 guess_partial_die_structure_name (this, cu);
abc72ce4 19380
53832f31
TT
19381 /* GCC might emit a nameless struct or union that has a linkage
19382 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19383 if (name == NULL
19384 && (tag == DW_TAG_class_type
19385 || tag == DW_TAG_interface_type
19386 || tag == DW_TAG_structure_type
19387 || tag == DW_TAG_union_type)
19388 && linkage_name != NULL)
53832f31 19389 {
43816ebc
TT
19390 gdb::unique_xmalloc_ptr<char> demangled
19391 (gdb_demangle (linkage_name, DMGL_TYPES));
19392 if (demangled != nullptr)
53832f31 19393 {
96408a79
SA
19394 const char *base;
19395
19396 /* Strip any leading namespaces/classes, keep only the base name.
19397 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19398 base = strrchr (demangled.get (), ':');
19399 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19400 base++;
19401 else
43816ebc 19402 base = demangled.get ();
96408a79 19403
518817b3 19404 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19405 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19406 }
19407 }
19408
52356b79 19409 fixup_called = 1;
72bf9492
DJ
19410}
19411
18a8505e
AT
19412/* Process the attributes that had to be skipped in the first round. These
19413 attributes are the ones that need str_offsets_base or addr_base attributes.
19414 They could not have been processed in the first round, because at the time
19415 the values of str_offsets_base or addr_base may not have been known. */
19416void read_attribute_reprocess (const struct die_reader_specs *reader,
19417 struct attribute *attr)
19418{
19419 struct dwarf2_cu *cu = reader->cu;
19420 switch (attr->form)
19421 {
19422 case DW_FORM_addrx:
19423 case DW_FORM_GNU_addr_index:
19424 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19425 break;
19426 case DW_FORM_strx:
19427 case DW_FORM_strx1:
19428 case DW_FORM_strx2:
19429 case DW_FORM_strx3:
19430 case DW_FORM_strx4:
19431 case DW_FORM_GNU_str_index:
19432 {
19433 unsigned int str_index = DW_UNSND (attr);
19434 if (reader->dwo_file != NULL)
19435 {
19436 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19437 DW_STRING_IS_CANONICAL (attr) = 0;
19438 }
19439 else
19440 {
19441 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19442 DW_STRING_IS_CANONICAL (attr) = 0;
19443 }
19444 break;
19445 }
19446 default:
19447 gdb_assert_not_reached (_("Unexpected DWARF form."));
19448 }
19449}
19450
a8329558 19451/* Read an attribute value described by an attribute form. */
c906108c 19452
d521ce57 19453static const gdb_byte *
dee91e82
DE
19454read_attribute_value (const struct die_reader_specs *reader,
19455 struct attribute *attr, unsigned form,
18a8505e
AT
19456 LONGEST implicit_const, const gdb_byte *info_ptr,
19457 bool *need_reprocess)
c906108c 19458{
dee91e82 19459 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19460 struct dwarf2_per_objfile *dwarf2_per_objfile
19461 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19462 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19463 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19464 bfd *abfd = reader->abfd;
e7c27a73 19465 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19466 unsigned int bytes_read;
19467 struct dwarf_block *blk;
18a8505e 19468 *need_reprocess = false;
c906108c 19469
aead7601 19470 attr->form = (enum dwarf_form) form;
a8329558 19471 switch (form)
c906108c 19472 {
c906108c 19473 case DW_FORM_ref_addr:
ae411497 19474 if (cu->header.version == 2)
4568ecf9 19475 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19476 else
4568ecf9
DE
19477 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19478 &cu->header, &bytes_read);
ae411497
TT
19479 info_ptr += bytes_read;
19480 break;
36586728
TT
19481 case DW_FORM_GNU_ref_alt:
19482 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19483 info_ptr += bytes_read;
19484 break;
ae411497 19485 case DW_FORM_addr:
e7c27a73 19486 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19487 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19488 info_ptr += bytes_read;
c906108c
SS
19489 break;
19490 case DW_FORM_block2:
7b5a2f43 19491 blk = dwarf_alloc_block (cu);
c906108c
SS
19492 blk->size = read_2_bytes (abfd, info_ptr);
19493 info_ptr += 2;
19494 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19495 info_ptr += blk->size;
19496 DW_BLOCK (attr) = blk;
19497 break;
19498 case DW_FORM_block4:
7b5a2f43 19499 blk = dwarf_alloc_block (cu);
c906108c
SS
19500 blk->size = read_4_bytes (abfd, info_ptr);
19501 info_ptr += 4;
19502 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19503 info_ptr += blk->size;
19504 DW_BLOCK (attr) = blk;
19505 break;
19506 case DW_FORM_data2:
19507 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19508 info_ptr += 2;
19509 break;
19510 case DW_FORM_data4:
19511 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19512 info_ptr += 4;
19513 break;
19514 case DW_FORM_data8:
19515 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19516 info_ptr += 8;
19517 break;
0224619f
JK
19518 case DW_FORM_data16:
19519 blk = dwarf_alloc_block (cu);
19520 blk->size = 16;
19521 blk->data = read_n_bytes (abfd, info_ptr, 16);
19522 info_ptr += 16;
19523 DW_BLOCK (attr) = blk;
19524 break;
2dc7f7b3
TT
19525 case DW_FORM_sec_offset:
19526 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19527 info_ptr += bytes_read;
19528 break;
c906108c 19529 case DW_FORM_string:
9b1c24c8 19530 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19531 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19532 info_ptr += bytes_read;
19533 break;
4bdf3d34 19534 case DW_FORM_strp:
36586728
TT
19535 if (!cu->per_cu->is_dwz)
19536 {
ed2dc618
SM
19537 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19538 abfd, info_ptr, cu_header,
36586728
TT
19539 &bytes_read);
19540 DW_STRING_IS_CANONICAL (attr) = 0;
19541 info_ptr += bytes_read;
19542 break;
19543 }
19544 /* FALLTHROUGH */
43988095
JK
19545 case DW_FORM_line_strp:
19546 if (!cu->per_cu->is_dwz)
19547 {
ed2dc618
SM
19548 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19549 abfd, info_ptr,
43988095
JK
19550 cu_header, &bytes_read);
19551 DW_STRING_IS_CANONICAL (attr) = 0;
19552 info_ptr += bytes_read;
19553 break;
19554 }
19555 /* FALLTHROUGH */
36586728
TT
19556 case DW_FORM_GNU_strp_alt:
19557 {
ed2dc618 19558 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19559 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19560 &bytes_read);
19561
ed2dc618
SM
19562 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19563 dwz, str_offset);
36586728
TT
19564 DW_STRING_IS_CANONICAL (attr) = 0;
19565 info_ptr += bytes_read;
19566 }
4bdf3d34 19567 break;
2dc7f7b3 19568 case DW_FORM_exprloc:
c906108c 19569 case DW_FORM_block:
7b5a2f43 19570 blk = dwarf_alloc_block (cu);
c906108c
SS
19571 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19572 info_ptr += bytes_read;
19573 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19574 info_ptr += blk->size;
19575 DW_BLOCK (attr) = blk;
19576 break;
19577 case DW_FORM_block1:
7b5a2f43 19578 blk = dwarf_alloc_block (cu);
c906108c
SS
19579 blk->size = read_1_byte (abfd, info_ptr);
19580 info_ptr += 1;
19581 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19582 info_ptr += blk->size;
19583 DW_BLOCK (attr) = blk;
19584 break;
19585 case DW_FORM_data1:
19586 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19587 info_ptr += 1;
19588 break;
19589 case DW_FORM_flag:
19590 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19591 info_ptr += 1;
19592 break;
2dc7f7b3
TT
19593 case DW_FORM_flag_present:
19594 DW_UNSND (attr) = 1;
19595 break;
c906108c
SS
19596 case DW_FORM_sdata:
19597 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19598 info_ptr += bytes_read;
19599 break;
19600 case DW_FORM_udata:
18a8505e 19601 case DW_FORM_rnglistx:
c906108c
SS
19602 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19603 info_ptr += bytes_read;
19604 break;
19605 case DW_FORM_ref1:
9c541725 19606 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19607 + read_1_byte (abfd, info_ptr));
c906108c
SS
19608 info_ptr += 1;
19609 break;
19610 case DW_FORM_ref2:
9c541725 19611 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19612 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19613 info_ptr += 2;
19614 break;
19615 case DW_FORM_ref4:
9c541725 19616 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19617 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19618 info_ptr += 4;
19619 break;
613e1657 19620 case DW_FORM_ref8:
9c541725 19621 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19622 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19623 info_ptr += 8;
19624 break;
55f1336d 19625 case DW_FORM_ref_sig8:
ac9ec31b 19626 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19627 info_ptr += 8;
19628 break;
c906108c 19629 case DW_FORM_ref_udata:
9c541725 19630 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19631 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19632 info_ptr += bytes_read;
19633 break;
c906108c 19634 case DW_FORM_indirect:
a8329558
KW
19635 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19636 info_ptr += bytes_read;
43988095
JK
19637 if (form == DW_FORM_implicit_const)
19638 {
19639 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19640 info_ptr += bytes_read;
19641 }
19642 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19643 info_ptr, need_reprocess);
43988095
JK
19644 break;
19645 case DW_FORM_implicit_const:
19646 DW_SND (attr) = implicit_const;
a8329558 19647 break;
336d760d 19648 case DW_FORM_addrx:
3019eac3 19649 case DW_FORM_GNU_addr_index:
18a8505e
AT
19650 *need_reprocess = true;
19651 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19652 info_ptr += bytes_read;
19653 break;
cf532bd1 19654 case DW_FORM_strx:
15f18d14
AT
19655 case DW_FORM_strx1:
19656 case DW_FORM_strx2:
19657 case DW_FORM_strx3:
19658 case DW_FORM_strx4:
3019eac3 19659 case DW_FORM_GNU_str_index:
3019eac3 19660 {
15f18d14
AT
19661 ULONGEST str_index;
19662 if (form == DW_FORM_strx1)
19663 {
19664 str_index = read_1_byte (abfd, info_ptr);
19665 info_ptr += 1;
19666 }
19667 else if (form == DW_FORM_strx2)
19668 {
19669 str_index = read_2_bytes (abfd, info_ptr);
19670 info_ptr += 2;
19671 }
19672 else if (form == DW_FORM_strx3)
19673 {
19674 str_index = read_3_bytes (abfd, info_ptr);
19675 info_ptr += 3;
19676 }
19677 else if (form == DW_FORM_strx4)
19678 {
19679 str_index = read_4_bytes (abfd, info_ptr);
19680 info_ptr += 4;
19681 }
19682 else
19683 {
19684 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19685 info_ptr += bytes_read;
19686 }
18a8505e
AT
19687 *need_reprocess = true;
19688 DW_UNSND (attr) = str_index;
19689 }
3019eac3 19690 break;
c906108c 19691 default:
8a3fe4f8 19692 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19693 dwarf_form_name (form),
19694 bfd_get_filename (abfd));
c906108c 19695 }
28e94949 19696
36586728 19697 /* Super hack. */
7771576e 19698 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19699 attr->form = DW_FORM_GNU_ref_alt;
19700
28e94949
JB
19701 /* We have seen instances where the compiler tried to emit a byte
19702 size attribute of -1 which ended up being encoded as an unsigned
19703 0xffffffff. Although 0xffffffff is technically a valid size value,
19704 an object of this size seems pretty unlikely so we can relatively
19705 safely treat these cases as if the size attribute was invalid and
19706 treat them as zero by default. */
19707 if (attr->name == DW_AT_byte_size
19708 && form == DW_FORM_data4
19709 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19710 {
19711 complaint
b98664d3 19712 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19713 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19714 DW_UNSND (attr) = 0;
19715 }
28e94949 19716
c906108c
SS
19717 return info_ptr;
19718}
19719
a8329558
KW
19720/* Read an attribute described by an abbreviated attribute. */
19721
d521ce57 19722static const gdb_byte *
dee91e82
DE
19723read_attribute (const struct die_reader_specs *reader,
19724 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19725 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19726{
19727 attr->name = abbrev->name;
43988095 19728 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19729 abbrev->implicit_const, info_ptr,
19730 need_reprocess);
a8329558
KW
19731}
19732
0963b4bd 19733/* Read dwarf information from a buffer. */
c906108c
SS
19734
19735static unsigned int
a1855c1d 19736read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19737{
fe1b8b76 19738 return bfd_get_8 (abfd, buf);
c906108c
SS
19739}
19740
19741static int
a1855c1d 19742read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19743{
fe1b8b76 19744 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19745}
19746
19747static unsigned int
a1855c1d 19748read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19749{
fe1b8b76 19750 return bfd_get_16 (abfd, buf);
c906108c
SS
19751}
19752
21ae7a4d 19753static int
a1855c1d 19754read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19755{
19756 return bfd_get_signed_16 (abfd, buf);
19757}
19758
15f18d14
AT
19759static unsigned int
19760read_3_bytes (bfd *abfd, const gdb_byte *buf)
19761{
19762 unsigned int result = 0;
19763 for (int i = 0; i < 3; ++i)
19764 {
19765 unsigned char byte = bfd_get_8 (abfd, buf);
19766 buf++;
19767 result |= ((unsigned int) byte << (i * 8));
19768 }
19769 return result;
19770}
19771
c906108c 19772static unsigned int
a1855c1d 19773read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19774{
fe1b8b76 19775 return bfd_get_32 (abfd, buf);
c906108c
SS
19776}
19777
21ae7a4d 19778static int
a1855c1d 19779read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19780{
19781 return bfd_get_signed_32 (abfd, buf);
19782}
19783
93311388 19784static ULONGEST
a1855c1d 19785read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19786{
fe1b8b76 19787 return bfd_get_64 (abfd, buf);
c906108c
SS
19788}
19789
19790static CORE_ADDR
d521ce57 19791read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19792 unsigned int *bytes_read)
c906108c 19793{
e7c27a73 19794 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19795 CORE_ADDR retval = 0;
19796
107d2387 19797 if (cu_header->signed_addr_p)
c906108c 19798 {
107d2387
AC
19799 switch (cu_header->addr_size)
19800 {
19801 case 2:
fe1b8b76 19802 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19803 break;
19804 case 4:
fe1b8b76 19805 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19806 break;
19807 case 8:
fe1b8b76 19808 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19809 break;
19810 default:
8e65ff28 19811 internal_error (__FILE__, __LINE__,
e2e0b3e5 19812 _("read_address: bad switch, signed [in module %s]"),
659b0389 19813 bfd_get_filename (abfd));
107d2387
AC
19814 }
19815 }
19816 else
19817 {
19818 switch (cu_header->addr_size)
19819 {
19820 case 2:
fe1b8b76 19821 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19822 break;
19823 case 4:
fe1b8b76 19824 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19825 break;
19826 case 8:
fe1b8b76 19827 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19828 break;
19829 default:
8e65ff28 19830 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19831 _("read_address: bad switch, "
19832 "unsigned [in module %s]"),
659b0389 19833 bfd_get_filename (abfd));
107d2387 19834 }
c906108c 19835 }
64367e0a 19836
107d2387
AC
19837 *bytes_read = cu_header->addr_size;
19838 return retval;
c906108c
SS
19839}
19840
f7ef9339 19841/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19842 specification allows the initial length to take up either 4 bytes
19843 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19844 bytes describe the length and all offsets will be 8 bytes in length
19845 instead of 4.
19846
f7ef9339
KB
19847 An older, non-standard 64-bit format is also handled by this
19848 function. The older format in question stores the initial length
19849 as an 8-byte quantity without an escape value. Lengths greater
19850 than 2^32 aren't very common which means that the initial 4 bytes
19851 is almost always zero. Since a length value of zero doesn't make
19852 sense for the 32-bit format, this initial zero can be considered to
19853 be an escape value which indicates the presence of the older 64-bit
19854 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19855 greater than 4GB. If it becomes necessary to handle lengths
19856 somewhat larger than 4GB, we could allow other small values (such
19857 as the non-sensical values of 1, 2, and 3) to also be used as
19858 escape values indicating the presence of the old format.
f7ef9339 19859
917c78fc
MK
19860 The value returned via bytes_read should be used to increment the
19861 relevant pointer after calling read_initial_length().
c764a876 19862
613e1657
KB
19863 [ Note: read_initial_length() and read_offset() are based on the
19864 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19865 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19866 from:
19867
f7ef9339 19868 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19869
613e1657
KB
19870 This document is only a draft and is subject to change. (So beware.)
19871
f7ef9339 19872 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19873 determined empirically by examining 64-bit ELF files produced by
19874 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19875
19876 - Kevin, July 16, 2002
613e1657
KB
19877 ] */
19878
19879static LONGEST
d521ce57 19880read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19881{
fe1b8b76 19882 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19883
dd373385 19884 if (length == 0xffffffff)
613e1657 19885 {
fe1b8b76 19886 length = bfd_get_64 (abfd, buf + 4);
613e1657 19887 *bytes_read = 12;
613e1657 19888 }
dd373385 19889 else if (length == 0)
f7ef9339 19890 {
dd373385 19891 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19892 length = bfd_get_64 (abfd, buf);
f7ef9339 19893 *bytes_read = 8;
f7ef9339 19894 }
613e1657
KB
19895 else
19896 {
19897 *bytes_read = 4;
613e1657
KB
19898 }
19899
c764a876
DE
19900 return length;
19901}
dd373385 19902
c764a876
DE
19903/* Cover function for read_initial_length.
19904 Returns the length of the object at BUF, and stores the size of the
19905 initial length in *BYTES_READ and stores the size that offsets will be in
19906 *OFFSET_SIZE.
19907 If the initial length size is not equivalent to that specified in
19908 CU_HEADER then issue a complaint.
19909 This is useful when reading non-comp-unit headers. */
dd373385 19910
c764a876 19911static LONGEST
d521ce57 19912read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19913 const struct comp_unit_head *cu_header,
19914 unsigned int *bytes_read,
19915 unsigned int *offset_size)
19916{
19917 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19918
19919 gdb_assert (cu_header->initial_length_size == 4
19920 || cu_header->initial_length_size == 8
19921 || cu_header->initial_length_size == 12);
19922
19923 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19924 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19925
c764a876 19926 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19927 return length;
613e1657
KB
19928}
19929
19930/* Read an offset from the data stream. The size of the offset is
917c78fc 19931 given by cu_header->offset_size. */
613e1657
KB
19932
19933static LONGEST
d521ce57
TT
19934read_offset (bfd *abfd, const gdb_byte *buf,
19935 const struct comp_unit_head *cu_header,
891d2f0b 19936 unsigned int *bytes_read)
c764a876
DE
19937{
19938 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19939
c764a876
DE
19940 *bytes_read = cu_header->offset_size;
19941 return offset;
19942}
19943
19944/* Read an offset from the data stream. */
19945
19946static LONGEST
d521ce57 19947read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19948{
19949 LONGEST retval = 0;
19950
c764a876 19951 switch (offset_size)
613e1657
KB
19952 {
19953 case 4:
fe1b8b76 19954 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19955 break;
19956 case 8:
fe1b8b76 19957 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19958 break;
19959 default:
8e65ff28 19960 internal_error (__FILE__, __LINE__,
c764a876 19961 _("read_offset_1: bad switch [in module %s]"),
659b0389 19962 bfd_get_filename (abfd));
613e1657
KB
19963 }
19964
917c78fc 19965 return retval;
613e1657
KB
19966}
19967
d521ce57
TT
19968static const gdb_byte *
19969read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19970{
19971 /* If the size of a host char is 8 bits, we can return a pointer
19972 to the buffer, otherwise we have to copy the data to a buffer
19973 allocated on the temporary obstack. */
4bdf3d34 19974 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19975 return buf;
c906108c
SS
19976}
19977
d521ce57
TT
19978static const char *
19979read_direct_string (bfd *abfd, const gdb_byte *buf,
19980 unsigned int *bytes_read_ptr)
c906108c
SS
19981{
19982 /* If the size of a host char is 8 bits, we can return a pointer
19983 to the string, otherwise we have to copy the string to a buffer
19984 allocated on the temporary obstack. */
4bdf3d34 19985 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19986 if (*buf == '\0')
19987 {
19988 *bytes_read_ptr = 1;
19989 return NULL;
19990 }
d521ce57
TT
19991 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19992 return (const char *) buf;
4bdf3d34
JJ
19993}
19994
43988095
JK
19995/* Return pointer to string at section SECT offset STR_OFFSET with error
19996 reporting strings FORM_NAME and SECT_NAME. */
19997
d521ce57 19998static const char *
ed2dc618
SM
19999read_indirect_string_at_offset_from (struct objfile *objfile,
20000 bfd *abfd, LONGEST str_offset,
43988095
JK
20001 struct dwarf2_section_info *sect,
20002 const char *form_name,
20003 const char *sect_name)
20004{
ed2dc618 20005 dwarf2_read_section (objfile, sect);
43988095
JK
20006 if (sect->buffer == NULL)
20007 error (_("%s used without %s section [in module %s]"),
20008 form_name, sect_name, bfd_get_filename (abfd));
20009 if (str_offset >= sect->size)
20010 error (_("%s pointing outside of %s section [in module %s]"),
20011 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 20012 gdb_assert (HOST_CHAR_BIT == 8);
43988095 20013 if (sect->buffer[str_offset] == '\0')
4bdf3d34 20014 return NULL;
43988095
JK
20015 return (const char *) (sect->buffer + str_offset);
20016}
20017
20018/* Return pointer to string at .debug_str offset STR_OFFSET. */
20019
20020static const char *
ed2dc618
SM
20021read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20022 bfd *abfd, LONGEST str_offset)
43988095 20023{
ed2dc618
SM
20024 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20025 abfd, str_offset,
43988095
JK
20026 &dwarf2_per_objfile->str,
20027 "DW_FORM_strp", ".debug_str");
20028}
20029
20030/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
20031
20032static const char *
ed2dc618
SM
20033read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20034 bfd *abfd, LONGEST str_offset)
43988095 20035{
ed2dc618
SM
20036 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20037 abfd, str_offset,
43988095
JK
20038 &dwarf2_per_objfile->line_str,
20039 "DW_FORM_line_strp",
20040 ".debug_line_str");
c906108c
SS
20041}
20042
36586728
TT
20043/* Read a string at offset STR_OFFSET in the .debug_str section from
20044 the .dwz file DWZ. Throw an error if the offset is too large. If
20045 the string consists of a single NUL byte, return NULL; otherwise
20046 return a pointer to the string. */
20047
d521ce57 20048static const char *
ed2dc618
SM
20049read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20050 LONGEST str_offset)
36586728 20051{
ed2dc618 20052 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
20053
20054 if (dwz->str.buffer == NULL)
20055 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20056 "section [in module %s]"),
00f93c44 20057 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20058 if (str_offset >= dwz->str.size)
20059 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20060 ".debug_str section [in module %s]"),
00f93c44 20061 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20062 gdb_assert (HOST_CHAR_BIT == 8);
20063 if (dwz->str.buffer[str_offset] == '\0')
20064 return NULL;
d521ce57 20065 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
20066}
20067
43988095
JK
20068/* Return pointer to string at .debug_str offset as read from BUF.
20069 BUF is assumed to be in a compilation unit described by CU_HEADER.
20070 Return *BYTES_READ_PTR count of bytes read from BUF. */
20071
d521ce57 20072static const char *
ed2dc618
SM
20073read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20074 const gdb_byte *buf,
cf2c3c16
TT
20075 const struct comp_unit_head *cu_header,
20076 unsigned int *bytes_read_ptr)
20077{
20078 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20079
ed2dc618 20080 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
20081}
20082
43988095
JK
20083/* Return pointer to string at .debug_line_str offset as read from BUF.
20084 BUF is assumed to be in a compilation unit described by CU_HEADER.
20085 Return *BYTES_READ_PTR count of bytes read from BUF. */
20086
20087static const char *
ed2dc618
SM
20088read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20089 bfd *abfd, const gdb_byte *buf,
43988095
JK
20090 const struct comp_unit_head *cu_header,
20091 unsigned int *bytes_read_ptr)
20092{
20093 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20094
ed2dc618
SM
20095 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20096 str_offset);
43988095
JK
20097}
20098
20099ULONGEST
d521ce57 20100read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 20101 unsigned int *bytes_read_ptr)
c906108c 20102{
12df843f 20103 ULONGEST result;
ce5d95e1 20104 unsigned int num_read;
870f88f7 20105 int shift;
c906108c
SS
20106 unsigned char byte;
20107
20108 result = 0;
20109 shift = 0;
20110 num_read = 0;
c906108c
SS
20111 while (1)
20112 {
fe1b8b76 20113 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20114 buf++;
20115 num_read++;
12df843f 20116 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20117 if ((byte & 128) == 0)
20118 {
20119 break;
20120 }
20121 shift += 7;
20122 }
20123 *bytes_read_ptr = num_read;
20124 return result;
20125}
20126
12df843f 20127static LONGEST
d521ce57
TT
20128read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20129 unsigned int *bytes_read_ptr)
c906108c 20130{
4dd1b460 20131 ULONGEST result;
870f88f7 20132 int shift, num_read;
c906108c
SS
20133 unsigned char byte;
20134
20135 result = 0;
20136 shift = 0;
c906108c 20137 num_read = 0;
c906108c
SS
20138 while (1)
20139 {
fe1b8b76 20140 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20141 buf++;
20142 num_read++;
4dd1b460 20143 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20144 shift += 7;
20145 if ((byte & 128) == 0)
20146 {
20147 break;
20148 }
20149 }
77e0b926 20150 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 20151 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
20152 *bytes_read_ptr = num_read;
20153 return result;
20154}
20155
3019eac3 20156/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20157 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20158 ADDR_SIZE is the size of addresses from the CU header. */
20159
20160static CORE_ADDR
ed2dc618 20161read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
20162 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
20163 int addr_size)
3019eac3
DE
20164{
20165 struct objfile *objfile = dwarf2_per_objfile->objfile;
20166 bfd *abfd = objfile->obfd;
20167 const gdb_byte *info_ptr;
18a8505e 20168 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3
DE
20169
20170 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20171 if (dwarf2_per_objfile->addr.buffer == NULL)
20172 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20173 objfile_name (objfile));
18a8505e
AT
20174 if (addr_base_or_zero + addr_index * addr_size
20175 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
20176 error (_("DW_FORM_addr_index pointing outside of "
20177 ".debug_addr section [in module %s]"),
4262abfb 20178 objfile_name (objfile));
3019eac3 20179 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 20180 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
20181 if (addr_size == 4)
20182 return bfd_get_32 (abfd, info_ptr);
20183 else
20184 return bfd_get_64 (abfd, info_ptr);
20185}
20186
20187/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20188
20189static CORE_ADDR
20190read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20191{
518817b3
SM
20192 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20193 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20194}
20195
20196/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20197
20198static CORE_ADDR
d521ce57 20199read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20200 unsigned int *bytes_read)
20201{
518817b3 20202 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20203 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20204
20205 return read_addr_index (cu, addr_index);
20206}
20207
3019eac3
DE
20208/* Given an index in .debug_addr, fetch the value.
20209 NOTE: This can be called during dwarf expression evaluation,
20210 long after the debug information has been read, and thus per_cu->cu
20211 may no longer exist. */
20212
20213CORE_ADDR
20214dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20215 unsigned int addr_index)
20216{
ed2dc618 20217 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 20218 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 20219 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20220 int addr_size;
20221
3019eac3
DE
20222 /* We need addr_base and addr_size.
20223 If we don't have PER_CU->cu, we have to get it.
20224 Nasty, but the alternative is storing the needed info in PER_CU,
20225 which at this point doesn't seem justified: it's not clear how frequently
20226 it would get used and it would increase the size of every PER_CU.
20227 Entry points like dwarf2_per_cu_addr_size do a similar thing
20228 so we're not in uncharted territory here.
20229 Alas we need to be a bit more complicated as addr_base is contained
20230 in the DIE.
20231
20232 We don't need to read the entire CU(/TU).
20233 We just need the header and top level die.
a1b64ce1 20234
3019eac3 20235 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20236 For now we skip this optimization. */
3019eac3
DE
20237
20238 if (cu != NULL)
20239 {
20240 addr_base = cu->addr_base;
20241 addr_size = cu->header.addr_size;
20242 }
20243 else
20244 {
c0ab21c2
TT
20245 cutu_reader reader (per_cu, NULL, 0, 0, false);
20246 addr_base = reader.cu->addr_base;
20247 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20248 }
20249
ed2dc618
SM
20250 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20251 addr_size);
3019eac3
DE
20252}
20253
18a8505e
AT
20254/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20255 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20256 DWO file. */
3019eac3 20257
d521ce57 20258static const char *
18a8505e
AT
20259read_str_index (struct dwarf2_cu *cu,
20260 struct dwarf2_section_info *str_section,
20261 struct dwarf2_section_info *str_offsets_section,
20262 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20263{
518817b3
SM
20264 struct dwarf2_per_objfile *dwarf2_per_objfile
20265 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20266 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20267 const char *objf_name = objfile_name (objfile);
3019eac3 20268 bfd *abfd = objfile->obfd;
d521ce57 20269 const gdb_byte *info_ptr;
3019eac3 20270 ULONGEST str_offset;
cf532bd1 20271 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20272
73869dc2
DE
20273 dwarf2_read_section (objfile, str_section);
20274 dwarf2_read_section (objfile, str_offsets_section);
20275 if (str_section->buffer == NULL)
18a8505e 20276 error (_("%s used without %s section"
9d8780f0 20277 " in CU at offset %s [in module %s]"),
18a8505e
AT
20278 form_name, get_section_name (str_section),
20279 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20280 if (str_offsets_section->buffer == NULL)
18a8505e 20281 error (_("%s used without %s section"
9d8780f0 20282 " in CU at offset %s [in module %s]"),
18a8505e
AT
20283 form_name, get_section_name (str_section),
20284 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20285 info_ptr = (str_offsets_section->buffer
18a8505e 20286 + str_offsets_base
3019eac3
DE
20287 + str_index * cu->header.offset_size);
20288 if (cu->header.offset_size == 4)
20289 str_offset = bfd_get_32 (abfd, info_ptr);
20290 else
20291 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20292 if (str_offset >= str_section->size)
57d63ce2 20293 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20294 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20295 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20296 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20297}
20298
18a8505e
AT
20299/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20300
20301static const char *
20302read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20303{
20304 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20305 ? reader->cu->header.addr_size : 0;
20306 return read_str_index (reader->cu,
20307 &reader->dwo_file->sections.str,
20308 &reader->dwo_file->sections.str_offsets,
20309 str_offsets_base, str_index);
20310}
20311
20312/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20313
20314static const char *
20315read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20316{
20317 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20318 const char *objf_name = objfile_name (objfile);
20319 static const char form_name[] = "DW_FORM_GNU_str_index";
20320 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20321
20322 if (!cu->str_offsets_base.has_value ())
20323 error (_("%s used in Fission stub without %s"
20324 " in CU at offset 0x%lx [in module %s]"),
20325 form_name, str_offsets_attr_name,
20326 (long) cu->header.offset_size, objf_name);
20327
20328 return read_str_index (cu,
20329 &cu->per_cu->dwarf2_per_objfile->str,
20330 &cu->per_cu->dwarf2_per_objfile->str_offsets,
20331 *cu->str_offsets_base, str_index);
20332}
20333
3019eac3
DE
20334/* Return the length of an LEB128 number in BUF. */
20335
20336static int
20337leb128_size (const gdb_byte *buf)
20338{
20339 const gdb_byte *begin = buf;
20340 gdb_byte byte;
20341
20342 while (1)
20343 {
20344 byte = *buf++;
20345 if ((byte & 128) == 0)
20346 return buf - begin;
20347 }
20348}
20349
c906108c 20350static void
e142c38c 20351set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20352{
20353 switch (lang)
20354 {
20355 case DW_LANG_C89:
76bee0cc 20356 case DW_LANG_C99:
0cfd832f 20357 case DW_LANG_C11:
c906108c 20358 case DW_LANG_C:
d1be3247 20359 case DW_LANG_UPC:
e142c38c 20360 cu->language = language_c;
c906108c 20361 break;
9c37b5ae 20362 case DW_LANG_Java:
c906108c 20363 case DW_LANG_C_plus_plus:
0cfd832f
MW
20364 case DW_LANG_C_plus_plus_11:
20365 case DW_LANG_C_plus_plus_14:
e142c38c 20366 cu->language = language_cplus;
c906108c 20367 break;
6aecb9c2
JB
20368 case DW_LANG_D:
20369 cu->language = language_d;
20370 break;
c906108c
SS
20371 case DW_LANG_Fortran77:
20372 case DW_LANG_Fortran90:
b21b22e0 20373 case DW_LANG_Fortran95:
f7de9aab
MW
20374 case DW_LANG_Fortran03:
20375 case DW_LANG_Fortran08:
e142c38c 20376 cu->language = language_fortran;
c906108c 20377 break;
a766d390
DE
20378 case DW_LANG_Go:
20379 cu->language = language_go;
20380 break;
c906108c 20381 case DW_LANG_Mips_Assembler:
e142c38c 20382 cu->language = language_asm;
c906108c
SS
20383 break;
20384 case DW_LANG_Ada83:
8aaf0b47 20385 case DW_LANG_Ada95:
bc5f45f8
JB
20386 cu->language = language_ada;
20387 break;
72019c9c
GM
20388 case DW_LANG_Modula2:
20389 cu->language = language_m2;
20390 break;
fe8e67fd
PM
20391 case DW_LANG_Pascal83:
20392 cu->language = language_pascal;
20393 break;
22566fbd
DJ
20394 case DW_LANG_ObjC:
20395 cu->language = language_objc;
20396 break;
c44af4eb
TT
20397 case DW_LANG_Rust:
20398 case DW_LANG_Rust_old:
20399 cu->language = language_rust;
20400 break;
c906108c
SS
20401 case DW_LANG_Cobol74:
20402 case DW_LANG_Cobol85:
c906108c 20403 default:
e142c38c 20404 cu->language = language_minimal;
c906108c
SS
20405 break;
20406 }
e142c38c 20407 cu->language_defn = language_def (cu->language);
c906108c
SS
20408}
20409
20410/* Return the named attribute or NULL if not there. */
20411
20412static struct attribute *
e142c38c 20413dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20414{
a48e046c 20415 for (;;)
c906108c 20416 {
a48e046c
TT
20417 unsigned int i;
20418 struct attribute *spec = NULL;
20419
20420 for (i = 0; i < die->num_attrs; ++i)
20421 {
20422 if (die->attrs[i].name == name)
20423 return &die->attrs[i];
20424 if (die->attrs[i].name == DW_AT_specification
20425 || die->attrs[i].name == DW_AT_abstract_origin)
20426 spec = &die->attrs[i];
20427 }
20428
20429 if (!spec)
20430 break;
c906108c 20431
f2f0e013 20432 die = follow_die_ref (die, spec, &cu);
f2f0e013 20433 }
c5aa993b 20434
c906108c
SS
20435 return NULL;
20436}
20437
348e048f
DE
20438/* Return the named attribute or NULL if not there,
20439 but do not follow DW_AT_specification, etc.
20440 This is for use in contexts where we're reading .debug_types dies.
20441 Following DW_AT_specification, DW_AT_abstract_origin will take us
20442 back up the chain, and we want to go down. */
20443
20444static struct attribute *
45e58e77 20445dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20446{
20447 unsigned int i;
20448
20449 for (i = 0; i < die->num_attrs; ++i)
20450 if (die->attrs[i].name == name)
20451 return &die->attrs[i];
20452
20453 return NULL;
20454}
20455
7d45c7c3
KB
20456/* Return the string associated with a string-typed attribute, or NULL if it
20457 is either not found or is of an incorrect type. */
20458
20459static const char *
20460dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20461{
20462 struct attribute *attr;
20463 const char *str = NULL;
20464
20465 attr = dwarf2_attr (die, name, cu);
20466
20467 if (attr != NULL)
20468 {
43988095 20469 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20470 || attr->form == DW_FORM_string
cf532bd1 20471 || attr->form == DW_FORM_strx
8fe0f950
AT
20472 || attr->form == DW_FORM_strx1
20473 || attr->form == DW_FORM_strx2
20474 || attr->form == DW_FORM_strx3
20475 || attr->form == DW_FORM_strx4
b3340438 20476 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20477 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20478 str = DW_STRING (attr);
20479 else
b98664d3 20480 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20481 "DIE at %s in module %s"),
20482 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20483 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20484 }
20485
20486 return str;
20487}
20488
a084a2a6 20489/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20490 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20491static const char *
20492dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20493{
20494 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20495 if (dwo_name == nullptr)
20496 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20497 return dwo_name;
20498}
20499
05cf31d1
JB
20500/* Return non-zero iff the attribute NAME is defined for the given DIE,
20501 and holds a non-zero value. This function should only be used for
2dc7f7b3 20502 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20503
20504static int
20505dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20506{
20507 struct attribute *attr = dwarf2_attr (die, name, cu);
20508
20509 return (attr && DW_UNSND (attr));
20510}
20511
3ca72b44 20512static int
e142c38c 20513die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20514{
05cf31d1
JB
20515 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20516 which value is non-zero. However, we have to be careful with
20517 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20518 (via dwarf2_flag_true_p) follows this attribute. So we may
20519 end up accidently finding a declaration attribute that belongs
20520 to a different DIE referenced by the specification attribute,
20521 even though the given DIE does not have a declaration attribute. */
20522 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20523 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20524}
20525
63d06c5c 20526/* Return the die giving the specification for DIE, if there is
f2f0e013 20527 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20528 containing the return value on output. If there is no
20529 specification, but there is an abstract origin, that is
20530 returned. */
63d06c5c
DC
20531
20532static struct die_info *
f2f0e013 20533die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20534{
f2f0e013
DJ
20535 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20536 *spec_cu);
63d06c5c 20537
edb3359d
DJ
20538 if (spec_attr == NULL)
20539 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20540
63d06c5c
DC
20541 if (spec_attr == NULL)
20542 return NULL;
20543 else
f2f0e013 20544 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20545}
c906108c 20546
527f3840
JK
20547/* Stub for free_line_header to match void * callback types. */
20548
20549static void
20550free_line_header_voidp (void *arg)
20551{
9a3c8263 20552 struct line_header *lh = (struct line_header *) arg;
527f3840 20553
fff8551c 20554 delete lh;
527f3840
JK
20555}
20556
fff8551c
PA
20557void
20558line_header::add_include_dir (const char *include_dir)
c906108c 20559{
27e0867f 20560 if (dwarf_line_debug >= 2)
7ba99d21
AT
20561 {
20562 size_t new_size;
20563 if (version >= 5)
20564 new_size = m_include_dirs.size ();
20565 else
20566 new_size = m_include_dirs.size () + 1;
20567 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20568 new_size, include_dir);
20569 }
20570 m_include_dirs.push_back (include_dir);
debd256d 20571}
6e70227d 20572
fff8551c
PA
20573void
20574line_header::add_file_name (const char *name,
ecfb656c 20575 dir_index d_index,
fff8551c
PA
20576 unsigned int mod_time,
20577 unsigned int length)
debd256d 20578{
27e0867f 20579 if (dwarf_line_debug >= 2)
7ba99d21
AT
20580 {
20581 size_t new_size;
20582 if (version >= 5)
20583 new_size = file_names_size ();
20584 else
20585 new_size = file_names_size () + 1;
20586 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20587 new_size, name);
20588 }
20589 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20590}
6e70227d 20591
83769d0b 20592/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20593
20594static struct dwarf2_section_info *
20595get_debug_line_section (struct dwarf2_cu *cu)
20596{
20597 struct dwarf2_section_info *section;
518817b3
SM
20598 struct dwarf2_per_objfile *dwarf2_per_objfile
20599 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20600
20601 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20602 DWO file. */
20603 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20604 section = &cu->dwo_unit->dwo_file->sections.line;
20605 else if (cu->per_cu->is_dwz)
20606 {
ed2dc618 20607 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20608
20609 section = &dwz->line;
20610 }
20611 else
20612 section = &dwarf2_per_objfile->line;
20613
20614 return section;
20615}
20616
43988095
JK
20617/* Read directory or file name entry format, starting with byte of
20618 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20619 entries count and the entries themselves in the described entry
20620 format. */
20621
20622static void
ed2dc618
SM
20623read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20624 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20625 struct line_header *lh,
20626 const struct comp_unit_head *cu_header,
20627 void (*callback) (struct line_header *lh,
20628 const char *name,
ecfb656c 20629 dir_index d_index,
43988095
JK
20630 unsigned int mod_time,
20631 unsigned int length))
20632{
20633 gdb_byte format_count, formati;
20634 ULONGEST data_count, datai;
20635 const gdb_byte *buf = *bufp;
20636 const gdb_byte *format_header_data;
43988095
JK
20637 unsigned int bytes_read;
20638
20639 format_count = read_1_byte (abfd, buf);
20640 buf += 1;
20641 format_header_data = buf;
20642 for (formati = 0; formati < format_count; formati++)
20643 {
20644 read_unsigned_leb128 (abfd, buf, &bytes_read);
20645 buf += bytes_read;
20646 read_unsigned_leb128 (abfd, buf, &bytes_read);
20647 buf += bytes_read;
20648 }
20649
20650 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20651 buf += bytes_read;
20652 for (datai = 0; datai < data_count; datai++)
20653 {
20654 const gdb_byte *format = format_header_data;
20655 struct file_entry fe;
20656
43988095
JK
20657 for (formati = 0; formati < format_count; formati++)
20658 {
ecfb656c 20659 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20660 format += bytes_read;
43988095 20661
ecfb656c 20662 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20663 format += bytes_read;
ecfb656c
PA
20664
20665 gdb::optional<const char *> string;
20666 gdb::optional<unsigned int> uint;
20667
43988095
JK
20668 switch (form)
20669 {
20670 case DW_FORM_string:
ecfb656c 20671 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20672 buf += bytes_read;
20673 break;
20674
20675 case DW_FORM_line_strp:
ed2dc618
SM
20676 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20677 abfd, buf,
ecfb656c
PA
20678 cu_header,
20679 &bytes_read));
43988095
JK
20680 buf += bytes_read;
20681 break;
20682
20683 case DW_FORM_data1:
ecfb656c 20684 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20685 buf += 1;
20686 break;
20687
20688 case DW_FORM_data2:
ecfb656c 20689 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20690 buf += 2;
20691 break;
20692
20693 case DW_FORM_data4:
ecfb656c 20694 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20695 buf += 4;
20696 break;
20697
20698 case DW_FORM_data8:
ecfb656c 20699 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20700 buf += 8;
20701 break;
20702
7ba99d21
AT
20703 case DW_FORM_data16:
20704 /* This is used for MD5, but file_entry does not record MD5s. */
20705 buf += 16;
20706 break;
20707
43988095 20708 case DW_FORM_udata:
ecfb656c 20709 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20710 buf += bytes_read;
20711 break;
20712
20713 case DW_FORM_block:
20714 /* It is valid only for DW_LNCT_timestamp which is ignored by
20715 current GDB. */
20716 break;
20717 }
ecfb656c
PA
20718
20719 switch (content_type)
20720 {
20721 case DW_LNCT_path:
20722 if (string.has_value ())
20723 fe.name = *string;
20724 break;
20725 case DW_LNCT_directory_index:
20726 if (uint.has_value ())
20727 fe.d_index = (dir_index) *uint;
20728 break;
20729 case DW_LNCT_timestamp:
20730 if (uint.has_value ())
20731 fe.mod_time = *uint;
20732 break;
20733 case DW_LNCT_size:
20734 if (uint.has_value ())
20735 fe.length = *uint;
20736 break;
20737 case DW_LNCT_MD5:
20738 break;
20739 default:
b98664d3 20740 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20741 pulongest (content_type));
20742 }
43988095
JK
20743 }
20744
ecfb656c 20745 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20746 }
20747
20748 *bufp = buf;
20749}
20750
debd256d 20751/* Read the statement program header starting at OFFSET in
3019eac3 20752 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20753 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20754 Returns NULL if there is a problem reading the header, e.g., if it
20755 has a version we don't understand.
debd256d
JB
20756
20757 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20758 the returned object point into the dwarf line section buffer,
20759 and must not be freed. */
ae2de4f8 20760
fff8551c 20761static line_header_up
9c541725 20762dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20763{
d521ce57 20764 const gdb_byte *line_ptr;
c764a876 20765 unsigned int bytes_read, offset_size;
debd256d 20766 int i;
d521ce57 20767 const char *cur_dir, *cur_file;
3019eac3
DE
20768 struct dwarf2_section_info *section;
20769 bfd *abfd;
518817b3
SM
20770 struct dwarf2_per_objfile *dwarf2_per_objfile
20771 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20772
36586728 20773 section = get_debug_line_section (cu);
3019eac3
DE
20774 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20775 if (section->buffer == NULL)
debd256d 20776 {
3019eac3 20777 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20778 complaint (_("missing .debug_line.dwo section"));
3019eac3 20779 else
b98664d3 20780 complaint (_("missing .debug_line section"));
debd256d
JB
20781 return 0;
20782 }
20783
fceca515
DE
20784 /* We can't do this until we know the section is non-empty.
20785 Only then do we know we have such a section. */
a32a8923 20786 abfd = get_section_bfd_owner (section);
fceca515 20787
a738430d
MK
20788 /* Make sure that at least there's room for the total_length field.
20789 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20790 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20791 {
4d3c2250 20792 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20793 return 0;
20794 }
20795
fff8551c 20796 line_header_up lh (new line_header ());
debd256d 20797
9c541725 20798 lh->sect_off = sect_off;
527f3840
JK
20799 lh->offset_in_dwz = cu->per_cu->is_dwz;
20800
9c541725 20801 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20802
a738430d 20803 /* Read in the header. */
6e70227d 20804 lh->total_length =
c764a876
DE
20805 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20806 &bytes_read, &offset_size);
debd256d 20807 line_ptr += bytes_read;
7ba99d21
AT
20808
20809 const gdb_byte *start_here = line_ptr;
20810
3019eac3 20811 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20812 {
4d3c2250 20813 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20814 return 0;
20815 }
7ba99d21 20816 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20817 lh->version = read_2_bytes (abfd, line_ptr);
20818 line_ptr += 2;
43988095 20819 if (lh->version > 5)
cd366ee8
DE
20820 {
20821 /* This is a version we don't understand. The format could have
20822 changed in ways we don't handle properly so just punt. */
b98664d3 20823 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20824 return NULL;
20825 }
43988095
JK
20826 if (lh->version >= 5)
20827 {
20828 gdb_byte segment_selector_size;
20829
20830 /* Skip address size. */
20831 read_1_byte (abfd, line_ptr);
20832 line_ptr += 1;
20833
20834 segment_selector_size = read_1_byte (abfd, line_ptr);
20835 line_ptr += 1;
20836 if (segment_selector_size != 0)
20837 {
b98664d3 20838 complaint (_("unsupported segment selector size %u "
43988095
JK
20839 "in .debug_line section"),
20840 segment_selector_size);
20841 return NULL;
20842 }
20843 }
c764a876
DE
20844 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20845 line_ptr += offset_size;
7ba99d21 20846 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20847 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20848 line_ptr += 1;
2dc7f7b3
TT
20849 if (lh->version >= 4)
20850 {
20851 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20852 line_ptr += 1;
20853 }
20854 else
20855 lh->maximum_ops_per_instruction = 1;
20856
20857 if (lh->maximum_ops_per_instruction == 0)
20858 {
20859 lh->maximum_ops_per_instruction = 1;
b98664d3 20860 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20861 "in `.debug_line' section"));
2dc7f7b3
TT
20862 }
20863
debd256d
JB
20864 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20865 line_ptr += 1;
20866 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20867 line_ptr += 1;
20868 lh->line_range = read_1_byte (abfd, line_ptr);
20869 line_ptr += 1;
20870 lh->opcode_base = read_1_byte (abfd, line_ptr);
20871 line_ptr += 1;
fff8551c 20872 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20873
20874 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20875 for (i = 1; i < lh->opcode_base; ++i)
20876 {
20877 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20878 line_ptr += 1;
20879 }
20880
43988095 20881 if (lh->version >= 5)
debd256d 20882 {
43988095 20883 /* Read directory table. */
ed2dc618
SM
20884 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20885 &cu->header,
b926417a 20886 [] (struct line_header *header, const char *name,
ecfb656c 20887 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20888 unsigned int length)
20889 {
b926417a 20890 header->add_include_dir (name);
fff8551c 20891 });
debd256d 20892
43988095 20893 /* Read file name table. */
ed2dc618
SM
20894 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20895 &cu->header,
b926417a 20896 [] (struct line_header *header, const char *name,
ecfb656c 20897 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20898 unsigned int length)
20899 {
b926417a 20900 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20901 });
43988095
JK
20902 }
20903 else
debd256d 20904 {
43988095
JK
20905 /* Read directory table. */
20906 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20907 {
20908 line_ptr += bytes_read;
fff8551c 20909 lh->add_include_dir (cur_dir);
43988095 20910 }
debd256d
JB
20911 line_ptr += bytes_read;
20912
43988095
JK
20913 /* Read file name table. */
20914 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20915 {
ecfb656c
PA
20916 unsigned int mod_time, length;
20917 dir_index d_index;
43988095
JK
20918
20919 line_ptr += bytes_read;
ecfb656c 20920 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20921 line_ptr += bytes_read;
20922 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20923 line_ptr += bytes_read;
20924 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20925 line_ptr += bytes_read;
20926
ecfb656c 20927 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20928 }
20929 line_ptr += bytes_read;
debd256d 20930 }
debd256d 20931
3019eac3 20932 if (line_ptr > (section->buffer + section->size))
b98664d3 20933 complaint (_("line number info header doesn't "
3e43a32a 20934 "fit in `.debug_line' section"));
debd256d 20935
debd256d
JB
20936 return lh;
20937}
c906108c 20938
c6da4cef 20939/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20940 Return the file name of the psymtab for the given file_entry.
c6da4cef 20941 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20942 If space for the result is malloc'd, *NAME_HOLDER will be set.
20943 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20944
d521ce57 20945static const char *
7ba99d21 20946psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
c6da4cef 20947 const struct partial_symtab *pst,
c89b44cd
TT
20948 const char *comp_dir,
20949 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20950{
d521ce57
TT
20951 const char *include_name = fe.name;
20952 const char *include_name_to_compare = include_name;
72b9f47f 20953 const char *pst_filename;
c6da4cef
DE
20954 int file_is_pst;
20955
8c43009f 20956 const char *dir_name = fe.include_dir (lh);
c6da4cef 20957
c89b44cd 20958 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20959 if (!IS_ABSOLUTE_PATH (include_name)
20960 && (dir_name != NULL || comp_dir != NULL))
20961 {
20962 /* Avoid creating a duplicate psymtab for PST.
20963 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20964 Before we do the comparison, however, we need to account
20965 for DIR_NAME and COMP_DIR.
20966 First prepend dir_name (if non-NULL). If we still don't
20967 have an absolute path prepend comp_dir (if non-NULL).
20968 However, the directory we record in the include-file's
20969 psymtab does not contain COMP_DIR (to match the
20970 corresponding symtab(s)).
20971
20972 Example:
20973
20974 bash$ cd /tmp
20975 bash$ gcc -g ./hello.c
20976 include_name = "hello.c"
20977 dir_name = "."
20978 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20979 DW_AT_name = "./hello.c"
20980
20981 */
c6da4cef
DE
20982
20983 if (dir_name != NULL)
20984 {
c89b44cd
TT
20985 name_holder->reset (concat (dir_name, SLASH_STRING,
20986 include_name, (char *) NULL));
20987 include_name = name_holder->get ();
c6da4cef 20988 include_name_to_compare = include_name;
c6da4cef
DE
20989 }
20990 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20991 {
c89b44cd
TT
20992 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20993 include_name, (char *) NULL));
20994 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20995 }
20996 }
20997
20998 pst_filename = pst->filename;
c89b44cd 20999 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21000 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21001 {
c89b44cd
TT
21002 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21003 pst_filename, (char *) NULL));
21004 pst_filename = copied_name.get ();
c6da4cef
DE
21005 }
21006
1e3fad37 21007 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21008
c6da4cef
DE
21009 if (file_is_pst)
21010 return NULL;
21011 return include_name;
21012}
21013
d9b3de22
DE
21014/* State machine to track the state of the line number program. */
21015
6f77053d 21016class lnp_state_machine
d9b3de22 21017{
6f77053d
PA
21018public:
21019 /* Initialize a machine state for the start of a line number
21020 program. */
804d2729
TT
21021 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21022 bool record_lines_p);
6f77053d 21023
8c43009f
PA
21024 file_entry *current_file ()
21025 {
21026 /* lh->file_names is 0-based, but the file name numbers in the
21027 statement program are 1-based. */
6f77053d
PA
21028 return m_line_header->file_name_at (m_file);
21029 }
21030
21031 /* Record the line in the state machine. END_SEQUENCE is true if
21032 we're processing the end of a sequence. */
21033 void record_line (bool end_sequence);
21034
7ab6656f
OJ
21035 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
21036 nop-out rest of the lines in this sequence. */
6f77053d
PA
21037 void check_line_address (struct dwarf2_cu *cu,
21038 const gdb_byte *line_ptr,
7ab6656f 21039 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21040
21041 void handle_set_discriminator (unsigned int discriminator)
21042 {
21043 m_discriminator = discriminator;
21044 m_line_has_non_zero_discriminator |= discriminator != 0;
21045 }
21046
21047 /* Handle DW_LNE_set_address. */
21048 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21049 {
21050 m_op_index = 0;
21051 address += baseaddr;
21052 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21053 }
21054
21055 /* Handle DW_LNS_advance_pc. */
21056 void handle_advance_pc (CORE_ADDR adjust);
21057
21058 /* Handle a special opcode. */
21059 void handle_special_opcode (unsigned char op_code);
21060
21061 /* Handle DW_LNS_advance_line. */
21062 void handle_advance_line (int line_delta)
21063 {
21064 advance_line (line_delta);
21065 }
21066
21067 /* Handle DW_LNS_set_file. */
21068 void handle_set_file (file_name_index file);
21069
21070 /* Handle DW_LNS_negate_stmt. */
21071 void handle_negate_stmt ()
21072 {
21073 m_is_stmt = !m_is_stmt;
21074 }
21075
21076 /* Handle DW_LNS_const_add_pc. */
21077 void handle_const_add_pc ();
21078
21079 /* Handle DW_LNS_fixed_advance_pc. */
21080 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21081 {
21082 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21083 m_op_index = 0;
21084 }
21085
21086 /* Handle DW_LNS_copy. */
21087 void handle_copy ()
21088 {
21089 record_line (false);
21090 m_discriminator = 0;
21091 }
21092
21093 /* Handle DW_LNE_end_sequence. */
21094 void handle_end_sequence ()
21095 {
804d2729 21096 m_currently_recording_lines = true;
6f77053d
PA
21097 }
21098
21099private:
21100 /* Advance the line by LINE_DELTA. */
21101 void advance_line (int line_delta)
21102 {
21103 m_line += line_delta;
21104
21105 if (line_delta != 0)
21106 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21107 }
21108
804d2729
TT
21109 struct dwarf2_cu *m_cu;
21110
6f77053d
PA
21111 gdbarch *m_gdbarch;
21112
21113 /* True if we're recording lines.
21114 Otherwise we're building partial symtabs and are just interested in
21115 finding include files mentioned by the line number program. */
21116 bool m_record_lines_p;
21117
8c43009f 21118 /* The line number header. */
6f77053d 21119 line_header *m_line_header;
8c43009f 21120
6f77053d
PA
21121 /* These are part of the standard DWARF line number state machine,
21122 and initialized according to the DWARF spec. */
d9b3de22 21123
6f77053d 21124 unsigned char m_op_index = 0;
7ba99d21
AT
21125 /* The line table index of the current file. */
21126 file_name_index m_file = 1;
6f77053d
PA
21127 unsigned int m_line = 1;
21128
21129 /* These are initialized in the constructor. */
21130
21131 CORE_ADDR m_address;
21132 bool m_is_stmt;
21133 unsigned int m_discriminator;
d9b3de22
DE
21134
21135 /* Additional bits of state we need to track. */
21136
21137 /* The last file that we called dwarf2_start_subfile for.
21138 This is only used for TLLs. */
6f77053d 21139 unsigned int m_last_file = 0;
d9b3de22 21140 /* The last file a line number was recorded for. */
6f77053d 21141 struct subfile *m_last_subfile = NULL;
d9b3de22 21142
804d2729
TT
21143 /* When true, record the lines we decode. */
21144 bool m_currently_recording_lines = false;
d9b3de22
DE
21145
21146 /* The last line number that was recorded, used to coalesce
21147 consecutive entries for the same line. This can happen, for
21148 example, when discriminators are present. PR 17276. */
6f77053d
PA
21149 unsigned int m_last_line = 0;
21150 bool m_line_has_non_zero_discriminator = false;
8c43009f 21151};
d9b3de22 21152
6f77053d
PA
21153void
21154lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21155{
21156 CORE_ADDR addr_adj = (((m_op_index + adjust)
21157 / m_line_header->maximum_ops_per_instruction)
21158 * m_line_header->minimum_instruction_length);
21159 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21160 m_op_index = ((m_op_index + adjust)
21161 % m_line_header->maximum_ops_per_instruction);
21162}
d9b3de22 21163
6f77053d
PA
21164void
21165lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21166{
6f77053d
PA
21167 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21168 CORE_ADDR addr_adj = (((m_op_index
21169 + (adj_opcode / m_line_header->line_range))
21170 / m_line_header->maximum_ops_per_instruction)
21171 * m_line_header->minimum_instruction_length);
21172 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21173 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21174 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21175
6f77053d
PA
21176 int line_delta = (m_line_header->line_base
21177 + (adj_opcode % m_line_header->line_range));
21178 advance_line (line_delta);
21179 record_line (false);
21180 m_discriminator = 0;
21181}
d9b3de22 21182
6f77053d
PA
21183void
21184lnp_state_machine::handle_set_file (file_name_index file)
21185{
21186 m_file = file;
21187
21188 const file_entry *fe = current_file ();
21189 if (fe == NULL)
21190 dwarf2_debug_line_missing_file_complaint ();
21191 else if (m_record_lines_p)
21192 {
21193 const char *dir = fe->include_dir (m_line_header);
21194
c24bdb02 21195 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21196 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21197 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21198 }
21199}
21200
21201void
21202lnp_state_machine::handle_const_add_pc ()
21203{
21204 CORE_ADDR adjust
21205 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21206
21207 CORE_ADDR addr_adj
21208 = (((m_op_index + adjust)
21209 / m_line_header->maximum_ops_per_instruction)
21210 * m_line_header->minimum_instruction_length);
21211
21212 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21213 m_op_index = ((m_op_index + adjust)
21214 % m_line_header->maximum_ops_per_instruction);
21215}
d9b3de22 21216
a05a36a5
DE
21217/* Return non-zero if we should add LINE to the line number table.
21218 LINE is the line to add, LAST_LINE is the last line that was added,
21219 LAST_SUBFILE is the subfile for LAST_LINE.
21220 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21221 had a non-zero discriminator.
21222
21223 We have to be careful in the presence of discriminators.
21224 E.g., for this line:
21225
21226 for (i = 0; i < 100000; i++);
21227
21228 clang can emit four line number entries for that one line,
21229 each with a different discriminator.
21230 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21231
21232 However, we want gdb to coalesce all four entries into one.
21233 Otherwise the user could stepi into the middle of the line and
21234 gdb would get confused about whether the pc really was in the
21235 middle of the line.
21236
21237 Things are further complicated by the fact that two consecutive
21238 line number entries for the same line is a heuristic used by gcc
21239 to denote the end of the prologue. So we can't just discard duplicate
21240 entries, we have to be selective about it. The heuristic we use is
21241 that we only collapse consecutive entries for the same line if at least
21242 one of those entries has a non-zero discriminator. PR 17276.
21243
21244 Note: Addresses in the line number state machine can never go backwards
21245 within one sequence, thus this coalescing is ok. */
21246
21247static int
804d2729
TT
21248dwarf_record_line_p (struct dwarf2_cu *cu,
21249 unsigned int line, unsigned int last_line,
a05a36a5
DE
21250 int line_has_non_zero_discriminator,
21251 struct subfile *last_subfile)
21252{
c24bdb02 21253 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21254 return 1;
21255 if (line != last_line)
21256 return 1;
21257 /* Same line for the same file that we've seen already.
21258 As a last check, for pr 17276, only record the line if the line
21259 has never had a non-zero discriminator. */
21260 if (!line_has_non_zero_discriminator)
21261 return 1;
21262 return 0;
21263}
21264
804d2729
TT
21265/* Use the CU's builder to record line number LINE beginning at
21266 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21267
21268static void
d9b3de22
DE
21269dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21270 unsigned int line, CORE_ADDR address,
804d2729 21271 struct dwarf2_cu *cu)
252a6764
DE
21272{
21273 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21274
27e0867f
DE
21275 if (dwarf_line_debug)
21276 {
21277 fprintf_unfiltered (gdb_stdlog,
21278 "Recording line %u, file %s, address %s\n",
21279 line, lbasename (subfile->name),
21280 paddress (gdbarch, address));
21281 }
21282
804d2729 21283 if (cu != nullptr)
c24bdb02 21284 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21285}
21286
21287/* Subroutine of dwarf_decode_lines_1 to simplify it.
21288 Mark the end of a set of line number records.
d9b3de22 21289 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21290 If SUBFILE is NULL the request is ignored. */
21291
21292static void
21293dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21294 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21295{
27e0867f
DE
21296 if (subfile == NULL)
21297 return;
21298
21299 if (dwarf_line_debug)
21300 {
21301 fprintf_unfiltered (gdb_stdlog,
21302 "Finishing current line, file %s, address %s\n",
21303 lbasename (subfile->name),
21304 paddress (gdbarch, address));
21305 }
21306
804d2729 21307 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21308}
21309
6f77053d
PA
21310void
21311lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21312{
d9b3de22
DE
21313 if (dwarf_line_debug)
21314 {
21315 fprintf_unfiltered (gdb_stdlog,
21316 "Processing actual line %u: file %u,"
94a72be7 21317 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21318 m_line, m_file,
6f77053d 21319 paddress (m_gdbarch, m_address),
94a72be7
AB
21320 m_is_stmt, m_discriminator,
21321 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21322 }
21323
6f77053d 21324 file_entry *fe = current_file ();
8c43009f
PA
21325
21326 if (fe == NULL)
d9b3de22
DE
21327 dwarf2_debug_line_missing_file_complaint ();
21328 /* For now we ignore lines not starting on an instruction boundary.
21329 But not when processing end_sequence for compatibility with the
21330 previous version of the code. */
6f77053d 21331 else if (m_op_index == 0 || end_sequence)
d9b3de22 21332 {
8c43009f 21333 fe->included_p = 1;
94a72be7
AB
21334 if (m_record_lines_p
21335 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 21336 {
c24bdb02 21337 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21338 || end_sequence)
d9b3de22 21339 {
804d2729
TT
21340 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21341 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21342 }
21343
21344 if (!end_sequence)
21345 {
804d2729 21346 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21347 m_line_has_non_zero_discriminator,
21348 m_last_subfile))
d9b3de22 21349 {
c24bdb02 21350 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21351 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21352 builder->get_current_subfile (),
6f77053d 21353 m_line, m_address,
804d2729 21354 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21355 }
c24bdb02 21356 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21357 m_last_line = m_line;
d9b3de22
DE
21358 }
21359 }
21360 }
21361}
21362
804d2729
TT
21363lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21364 line_header *lh, bool record_lines_p)
d9b3de22 21365{
804d2729 21366 m_cu = cu;
6f77053d
PA
21367 m_gdbarch = arch;
21368 m_record_lines_p = record_lines_p;
21369 m_line_header = lh;
d9b3de22 21370
804d2729 21371 m_currently_recording_lines = true;
d9b3de22 21372
d9b3de22
DE
21373 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21374 was a line entry for it so that the backend has a chance to adjust it
21375 and also record it in case it needs it. This is currently used by MIPS
21376 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21377 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21378 m_is_stmt = lh->default_is_stmt;
21379 m_discriminator = 0;
252a6764
DE
21380}
21381
6f77053d
PA
21382void
21383lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21384 const gdb_byte *line_ptr,
7ab6656f 21385 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21386{
7ab6656f
OJ
21387 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21388 the pc range of the CU. However, we restrict the test to only ADDRESS
21389 values of zero to preserve GDB's previous behaviour which is to handle
21390 the specific case of a function being GC'd by the linker. */
924c2928 21391
7ab6656f 21392 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21393 {
21394 /* This line table is for a function which has been
21395 GCd by the linker. Ignore it. PR gdb/12528 */
21396
518817b3 21397 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21398 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21399
b98664d3 21400 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21401 line_offset, objfile_name (objfile));
804d2729
TT
21402 m_currently_recording_lines = false;
21403 /* Note: m_currently_recording_lines is left as false until we see
21404 DW_LNE_end_sequence. */
924c2928
DE
21405 }
21406}
21407
f3f5162e 21408/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21409 Process the line number information in LH.
21410 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21411 program in order to set included_p for every referenced header. */
debd256d 21412
c906108c 21413static void
43f3e411
DE
21414dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21415 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21416{
d521ce57
TT
21417 const gdb_byte *line_ptr, *extended_end;
21418 const gdb_byte *line_end;
a8c50c1f 21419 unsigned int bytes_read, extended_len;
699ca60a 21420 unsigned char op_code, extended_op;
e142c38c 21421 CORE_ADDR baseaddr;
518817b3 21422 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21423 bfd *abfd = objfile->obfd;
fbf65064 21424 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21425 /* True if we're recording line info (as opposed to building partial
21426 symtabs and just interested in finding include files mentioned by
21427 the line number program). */
21428 bool record_lines_p = !decode_for_pst_p;
e142c38c 21429
b3b3bada 21430 baseaddr = objfile->text_section_offset ();
c906108c 21431
debd256d
JB
21432 line_ptr = lh->statement_program_start;
21433 line_end = lh->statement_program_end;
c906108c
SS
21434
21435 /* Read the statement sequences until there's nothing left. */
21436 while (line_ptr < line_end)
21437 {
6f77053d
PA
21438 /* The DWARF line number program state machine. Reset the state
21439 machine at the start of each sequence. */
804d2729 21440 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21441 bool end_sequence = false;
d9b3de22 21442
8c43009f 21443 if (record_lines_p)
c906108c 21444 {
8c43009f
PA
21445 /* Start a subfile for the current file of the state
21446 machine. */
21447 const file_entry *fe = state_machine.current_file ();
21448
21449 if (fe != NULL)
804d2729 21450 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21451 }
21452
a738430d 21453 /* Decode the table. */
d9b3de22 21454 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21455 {
21456 op_code = read_1_byte (abfd, line_ptr);
21457 line_ptr += 1;
9aa1fe7e 21458
debd256d 21459 if (op_code >= lh->opcode_base)
6e70227d 21460 {
8e07a239 21461 /* Special opcode. */
6f77053d 21462 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21463 }
21464 else switch (op_code)
c906108c
SS
21465 {
21466 case DW_LNS_extended_op:
3e43a32a
MS
21467 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21468 &bytes_read);
473b7be6 21469 line_ptr += bytes_read;
a8c50c1f 21470 extended_end = line_ptr + extended_len;
c906108c
SS
21471 extended_op = read_1_byte (abfd, line_ptr);
21472 line_ptr += 1;
21473 switch (extended_op)
21474 {
21475 case DW_LNE_end_sequence:
6f77053d
PA
21476 state_machine.handle_end_sequence ();
21477 end_sequence = true;
c906108c
SS
21478 break;
21479 case DW_LNE_set_address:
d9b3de22
DE
21480 {
21481 CORE_ADDR address
21482 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21483 line_ptr += bytes_read;
6f77053d
PA
21484
21485 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21486 lowpc - baseaddr, address);
6f77053d 21487 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21488 }
c906108c
SS
21489 break;
21490 case DW_LNE_define_file:
debd256d 21491 {
d521ce57 21492 const char *cur_file;
ecfb656c
PA
21493 unsigned int mod_time, length;
21494 dir_index dindex;
6e70227d 21495
3e43a32a
MS
21496 cur_file = read_direct_string (abfd, line_ptr,
21497 &bytes_read);
debd256d 21498 line_ptr += bytes_read;
ecfb656c 21499 dindex = (dir_index)
debd256d
JB
21500 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21501 line_ptr += bytes_read;
21502 mod_time =
21503 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21504 line_ptr += bytes_read;
21505 length =
21506 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21507 line_ptr += bytes_read;
ecfb656c 21508 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21509 }
c906108c 21510 break;
d0c6ba3d 21511 case DW_LNE_set_discriminator:
6f77053d
PA
21512 {
21513 /* The discriminator is not interesting to the
21514 debugger; just ignore it. We still need to
21515 check its value though:
21516 if there are consecutive entries for the same
21517 (non-prologue) line we want to coalesce them.
21518 PR 17276. */
21519 unsigned int discr
21520 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21521 line_ptr += bytes_read;
21522
21523 state_machine.handle_set_discriminator (discr);
21524 }
d0c6ba3d 21525 break;
c906108c 21526 default:
b98664d3 21527 complaint (_("mangled .debug_line section"));
debd256d 21528 return;
c906108c 21529 }
a8c50c1f
DJ
21530 /* Make sure that we parsed the extended op correctly. If e.g.
21531 we expected a different address size than the producer used,
21532 we may have read the wrong number of bytes. */
21533 if (line_ptr != extended_end)
21534 {
b98664d3 21535 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21536 return;
21537 }
c906108c
SS
21538 break;
21539 case DW_LNS_copy:
6f77053d 21540 state_machine.handle_copy ();
c906108c
SS
21541 break;
21542 case DW_LNS_advance_pc:
2dc7f7b3
TT
21543 {
21544 CORE_ADDR adjust
21545 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21546 line_ptr += bytes_read;
6f77053d
PA
21547
21548 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21549 }
c906108c
SS
21550 break;
21551 case DW_LNS_advance_line:
a05a36a5
DE
21552 {
21553 int line_delta
21554 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21555 line_ptr += bytes_read;
6f77053d
PA
21556
21557 state_machine.handle_advance_line (line_delta);
a05a36a5 21558 }
c906108c
SS
21559 break;
21560 case DW_LNS_set_file:
d9b3de22 21561 {
6f77053d 21562 file_name_index file
ecfb656c
PA
21563 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21564 &bytes_read);
d9b3de22 21565 line_ptr += bytes_read;
8c43009f 21566
6f77053d 21567 state_machine.handle_set_file (file);
d9b3de22 21568 }
c906108c
SS
21569 break;
21570 case DW_LNS_set_column:
0ad93d4f 21571 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21572 line_ptr += bytes_read;
21573 break;
21574 case DW_LNS_negate_stmt:
6f77053d 21575 state_machine.handle_negate_stmt ();
c906108c
SS
21576 break;
21577 case DW_LNS_set_basic_block:
c906108c 21578 break;
c2c6d25f
JM
21579 /* Add to the address register of the state machine the
21580 address increment value corresponding to special opcode
a738430d
MK
21581 255. I.e., this value is scaled by the minimum
21582 instruction length since special opcode 255 would have
b021a221 21583 scaled the increment. */
c906108c 21584 case DW_LNS_const_add_pc:
6f77053d 21585 state_machine.handle_const_add_pc ();
c906108c
SS
21586 break;
21587 case DW_LNS_fixed_advance_pc:
3e29f34a 21588 {
6f77053d 21589 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21590 line_ptr += 2;
6f77053d
PA
21591
21592 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21593 }
c906108c 21594 break;
9aa1fe7e 21595 default:
a738430d
MK
21596 {
21597 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21598 int i;
a738430d 21599
debd256d 21600 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21601 {
21602 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21603 line_ptr += bytes_read;
21604 }
21605 }
c906108c
SS
21606 }
21607 }
d9b3de22
DE
21608
21609 if (!end_sequence)
21610 dwarf2_debug_line_missing_end_sequence_complaint ();
21611
21612 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21613 in which case we still finish recording the last line). */
6f77053d 21614 state_machine.record_line (true);
c906108c 21615 }
f3f5162e
DE
21616}
21617
21618/* Decode the Line Number Program (LNP) for the given line_header
21619 structure and CU. The actual information extracted and the type
21620 of structures created from the LNP depends on the value of PST.
21621
21622 1. If PST is NULL, then this procedure uses the data from the program
21623 to create all necessary symbol tables, and their linetables.
21624
21625 2. If PST is not NULL, this procedure reads the program to determine
21626 the list of files included by the unit represented by PST, and
21627 builds all the associated partial symbol tables.
21628
21629 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21630 It is used for relative paths in the line table.
21631 NOTE: When processing partial symtabs (pst != NULL),
21632 comp_dir == pst->dirname.
21633
21634 NOTE: It is important that psymtabs have the same file name (via strcmp)
21635 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21636 symtab we don't use it in the name of the psymtabs we create.
21637 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21638 A good testcase for this is mb-inline.exp.
21639
527f3840
JK
21640 LOWPC is the lowest address in CU (or 0 if not known).
21641
21642 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21643 for its PC<->lines mapping information. Otherwise only the filename
21644 table is read in. */
f3f5162e
DE
21645
21646static void
21647dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21648 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21649 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21650{
518817b3 21651 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21652 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21653
527f3840
JK
21654 if (decode_mapping)
21655 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21656
21657 if (decode_for_pst_p)
21658 {
aaa75496
JB
21659 /* Now that we're done scanning the Line Header Program, we can
21660 create the psymtab of each included file. */
7ba99d21
AT
21661 for (auto &file_entry : lh->file_names ())
21662 if (file_entry.included_p == 1)
aaa75496 21663 {
c89b44cd 21664 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21665 const char *include_name =
7ba99d21
AT
21666 psymtab_include_file_name (lh, file_entry, pst,
21667 comp_dir, &name_holder);
c6da4cef 21668 if (include_name != NULL)
aaa75496
JB
21669 dwarf2_create_include_psymtab (include_name, pst, objfile);
21670 }
21671 }
cb1df416
DJ
21672 else
21673 {
21674 /* Make sure a symtab is created for every file, even files
21675 which contain only variables (i.e. no code with associated
21676 line numbers). */
c24bdb02
KS
21677 buildsym_compunit *builder = cu->get_builder ();
21678 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21679
7ba99d21 21680 for (auto &fe : lh->file_names ())
cb1df416 21681 {
804d2729 21682 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21683 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21684 {
c24bdb02 21685 builder->get_current_subfile ()->symtab
804d2729 21686 = allocate_symtab (cust,
c24bdb02 21687 builder->get_current_subfile ()->name);
43f3e411 21688 }
c24bdb02 21689 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21690 }
21691 }
c906108c
SS
21692}
21693
21694/* Start a subfile for DWARF. FILENAME is the name of the file and
21695 DIRNAME the name of the source directory which contains FILENAME
4d663531 21696 or NULL if not known.
c906108c
SS
21697 This routine tries to keep line numbers from identical absolute and
21698 relative file names in a common subfile.
21699
21700 Using the `list' example from the GDB testsuite, which resides in
21701 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21702 of /srcdir/list0.c yields the following debugging information for list0.c:
21703
c5aa993b 21704 DW_AT_name: /srcdir/list0.c
4d663531 21705 DW_AT_comp_dir: /compdir
357e46e7 21706 files.files[0].name: list0.h
c5aa993b 21707 files.files[0].dir: /srcdir
357e46e7 21708 files.files[1].name: list0.c
c5aa993b 21709 files.files[1].dir: /srcdir
c906108c
SS
21710
21711 The line number information for list0.c has to end up in a single
4f1520fb
FR
21712 subfile, so that `break /srcdir/list0.c:1' works as expected.
21713 start_subfile will ensure that this happens provided that we pass the
21714 concatenation of files.files[1].dir and files.files[1].name as the
21715 subfile's name. */
c906108c
SS
21716
21717static void
804d2729
TT
21718dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21719 const char *dirname)
c906108c 21720{
43816ebc 21721 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21722
4d663531 21723 /* In order not to lose the line information directory,
4f1520fb
FR
21724 we concatenate it to the filename when it makes sense.
21725 Note that the Dwarf3 standard says (speaking of filenames in line
21726 information): ``The directory index is ignored for file names
21727 that represent full path names''. Thus ignoring dirname in the
21728 `else' branch below isn't an issue. */
c906108c 21729
d5166ae1 21730 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21731 {
43816ebc
TT
21732 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21733 filename = copy.get ();
d521ce57 21734 }
c906108c 21735
c24bdb02 21736 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21737}
21738
804d2729
TT
21739/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21740 buildsym_compunit constructor. */
f4dc4d17 21741
c24bdb02
KS
21742struct compunit_symtab *
21743dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21744 CORE_ADDR low_pc)
f4dc4d17 21745{
c24bdb02 21746 gdb_assert (m_builder == nullptr);
43f3e411 21747
c24bdb02
KS
21748 m_builder.reset (new struct buildsym_compunit
21749 (per_cu->dwarf2_per_objfile->objfile,
21750 name, comp_dir, language, low_pc));
93b8bea4 21751
c24bdb02 21752 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21753
c24bdb02
KS
21754 get_builder ()->record_debugformat ("DWARF 2");
21755 get_builder ()->record_producer (producer);
f4dc4d17 21756
c24bdb02 21757 processing_has_namespace_info = false;
43f3e411 21758
c24bdb02 21759 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21760}
21761
4c2df51b
DJ
21762static void
21763var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21764 struct dwarf2_cu *cu)
4c2df51b 21765{
518817b3 21766 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21767 struct comp_unit_head *cu_header = &cu->header;
21768
4c2df51b
DJ
21769 /* NOTE drow/2003-01-30: There used to be a comment and some special
21770 code here to turn a symbol with DW_AT_external and a
21771 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21772 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21773 with some versions of binutils) where shared libraries could have
21774 relocations against symbols in their debug information - the
21775 minimal symbol would have the right address, but the debug info
21776 would not. It's no longer necessary, because we will explicitly
21777 apply relocations when we read in the debug information now. */
21778
21779 /* A DW_AT_location attribute with no contents indicates that a
21780 variable has been optimized away. */
21781 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21782 {
f1e6e072 21783 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21784 return;
21785 }
21786
21787 /* Handle one degenerate form of location expression specially, to
21788 preserve GDB's previous behavior when section offsets are
336d760d
AT
21789 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21790 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21791
21792 if (attr_form_is_block (attr)
3019eac3
DE
21793 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21794 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21795 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21796 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21797 && (DW_BLOCK (attr)->size
21798 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21799 {
891d2f0b 21800 unsigned int dummy;
4c2df51b 21801
3019eac3 21802 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21803 SET_SYMBOL_VALUE_ADDRESS (sym,
21804 read_address (objfile->obfd,
21805 DW_BLOCK (attr)->data + 1,
21806 cu, &dummy));
3019eac3 21807 else
38583298
TT
21808 SET_SYMBOL_VALUE_ADDRESS
21809 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21810 &dummy));
f1e6e072 21811 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21812 fixup_symbol_section (sym, objfile);
6a053cb1
TT
21813 SET_SYMBOL_VALUE_ADDRESS
21814 (sym,
21815 SYMBOL_VALUE_ADDRESS (sym)
21816 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
21817 return;
21818 }
21819
21820 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21821 expression evaluator, and use LOC_COMPUTED only when necessary
21822 (i.e. when the value of a register or memory location is
21823 referenced, or a thread-local block, etc.). Then again, it might
21824 not be worthwhile. I'm assuming that it isn't unless performance
21825 or memory numbers show me otherwise. */
21826
f1e6e072 21827 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21828
f1e6e072 21829 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21830 cu->has_loclist = true;
4c2df51b
DJ
21831}
21832
c906108c
SS
21833/* Given a pointer to a DWARF information entry, figure out if we need
21834 to make a symbol table entry for it, and if so, create a new entry
21835 and return a pointer to it.
21836 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21837 used the passed type.
21838 If SPACE is not NULL, use it to hold the new symbol. If it is
21839 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21840
21841static struct symbol *
5e2db402
TT
21842new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21843 struct symbol *space)
c906108c 21844{
518817b3
SM
21845 struct dwarf2_per_objfile *dwarf2_per_objfile
21846 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21847 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21848 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21849 struct symbol *sym = NULL;
15d034d0 21850 const char *name;
c906108c
SS
21851 struct attribute *attr = NULL;
21852 struct attribute *attr2 = NULL;
e142c38c 21853 CORE_ADDR baseaddr;
e37fd15a
SW
21854 struct pending **list_to_add = NULL;
21855
edb3359d 21856 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21857
b3b3bada 21858 baseaddr = objfile->text_section_offset ();
c906108c 21859
94af9270 21860 name = dwarf2_name (die, cu);
c906108c
SS
21861 if (name)
21862 {
94af9270 21863 const char *linkagename;
34eaf542 21864 int suppress_add = 0;
94af9270 21865
34eaf542
TT
21866 if (space)
21867 sym = space;
21868 else
e623cf5d 21869 sym = allocate_symbol (objfile);
c906108c 21870 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21871
21872 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21873 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 21874 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 21875 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 21876
f55ee35c
JK
21877 /* Fortran does not have mangling standard and the mangling does differ
21878 between gfortran, iFort etc. */
21879 if (cu->language == language_fortran
468c0cbb
CB
21880 && symbol_get_demangled_name (sym) == NULL)
21881 symbol_set_demangled_name (sym,
cfc594ee 21882 dwarf2_full_name (name, die, cu),
29df156d 21883 NULL);
f55ee35c 21884
c906108c 21885 /* Default assumptions.
c5aa993b 21886 Use the passed type or decode it from the die. */
176620f1 21887 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21888 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21889 if (type != NULL)
21890 SYMBOL_TYPE (sym) = type;
21891 else
e7c27a73 21892 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21893 attr = dwarf2_attr (die,
21894 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21895 cu);
435d3d88 21896 if (attr != nullptr)
c906108c
SS
21897 {
21898 SYMBOL_LINE (sym) = DW_UNSND (attr);
21899 }
cb1df416 21900
edb3359d
DJ
21901 attr = dwarf2_attr (die,
21902 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21903 cu);
435d3d88 21904 if (attr != nullptr)
cb1df416 21905 {
ecfb656c 21906 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21907 struct file_entry *fe;
9a619af0 21908
ecfb656c
PA
21909 if (cu->line_header != NULL)
21910 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21911 else
21912 fe = NULL;
21913
21914 if (fe == NULL)
b98664d3 21915 complaint (_("file index out of range"));
8c43009f
PA
21916 else
21917 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21918 }
21919
c906108c
SS
21920 switch (die->tag)
21921 {
21922 case DW_TAG_label:
e142c38c 21923 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21924 if (attr != nullptr)
3e29f34a
MR
21925 {
21926 CORE_ADDR addr;
21927
21928 addr = attr_value_as_address (attr);
21929 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21930 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21931 }
0f5238ed
TT
21932 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21933 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21934 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21935 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21936 break;
21937 case DW_TAG_subprogram:
21938 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21939 finish_block. */
f1e6e072 21940 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21941 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21942 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21943 || cu->language == language_ada
21944 || cu->language == language_fortran)
c906108c 21945 {
2cfa0c8d 21946 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21947 Ada and Fortran subprograms, whether marked external or
21948 not, are always stored as a global symbol, because we want
21949 to be able to access them globally. For instance, we want
21950 to be able to break on a nested subprogram without having
21951 to specify the context. */
c24bdb02 21952 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21953 }
21954 else
21955 {
e37fd15a 21956 list_to_add = cu->list_in_scope;
c906108c
SS
21957 }
21958 break;
edb3359d
DJ
21959 case DW_TAG_inlined_subroutine:
21960 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21961 finish_block. */
f1e6e072 21962 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21963 SYMBOL_INLINED (sym) = 1;
481860b3 21964 list_to_add = cu->list_in_scope;
edb3359d 21965 break;
34eaf542
TT
21966 case DW_TAG_template_value_param:
21967 suppress_add = 1;
21968 /* Fall through. */
72929c62 21969 case DW_TAG_constant:
c906108c 21970 case DW_TAG_variable:
254e6b9e 21971 case DW_TAG_member:
0963b4bd
MS
21972 /* Compilation with minimal debug info may result in
21973 variables with missing type entries. Change the
21974 misleading `void' type to something sensible. */
c906108c 21975 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21976 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21977
e142c38c 21978 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21979 /* In the case of DW_TAG_member, we should only be called for
21980 static const members. */
21981 if (die->tag == DW_TAG_member)
21982 {
3863f96c
DE
21983 /* dwarf2_add_field uses die_is_declaration,
21984 so we do the same. */
254e6b9e
DE
21985 gdb_assert (die_is_declaration (die, cu));
21986 gdb_assert (attr);
21987 }
435d3d88 21988 if (attr != nullptr)
c906108c 21989 {
e7c27a73 21990 dwarf2_const_value (attr, sym, cu);
e142c38c 21991 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21992 if (!suppress_add)
34eaf542
TT
21993 {
21994 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21995 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21996 else
e37fd15a 21997 list_to_add = cu->list_in_scope;
34eaf542 21998 }
c906108c
SS
21999 break;
22000 }
e142c38c 22001 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22002 if (attr != nullptr)
c906108c 22003 {
e7c27a73 22004 var_decode_location (attr, sym, cu);
e142c38c 22005 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22006
22007 /* Fortran explicitly imports any global symbols to the local
22008 scope by DW_TAG_common_block. */
22009 if (cu->language == language_fortran && die->parent
22010 && die->parent->tag == DW_TAG_common_block)
22011 attr2 = NULL;
22012
caac4577
JG
22013 if (SYMBOL_CLASS (sym) == LOC_STATIC
22014 && SYMBOL_VALUE_ADDRESS (sym) == 0
22015 && !dwarf2_per_objfile->has_section_at_zero)
22016 {
22017 /* When a static variable is eliminated by the linker,
22018 the corresponding debug information is not stripped
22019 out, but the variable address is set to null;
22020 do not add such variables into symbol table. */
22021 }
22022 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 22023 {
4b610737
TT
22024 if (SYMBOL_CLASS (sym) == LOC_STATIC
22025 && (objfile->flags & OBJF_MAINLINE) == 0
22026 && dwarf2_per_objfile->can_copy)
22027 {
22028 /* A global static variable might be subject to
22029 copy relocation. We first check for a local
22030 minsym, though, because maybe the symbol was
22031 marked hidden, in which case this would not
22032 apply. */
22033 bound_minimal_symbol found
22034 = (lookup_minimal_symbol_linkage
987012b8 22035 (sym->linkage_name (), objfile));
4b610737
TT
22036 if (found.minsym != nullptr)
22037 sym->maybe_copied = 1;
22038 }
f55ee35c 22039
1c809c68
TT
22040 /* A variable with DW_AT_external is never static,
22041 but it may be block-scoped. */
804d2729 22042 list_to_add
c24bdb02
KS
22043 = ((cu->list_in_scope
22044 == cu->get_builder ()->get_file_symbols ())
22045 ? cu->get_builder ()->get_global_symbols ()
804d2729 22046 : cu->list_in_scope);
1c809c68 22047 }
c906108c 22048 else
e37fd15a 22049 list_to_add = cu->list_in_scope;
c906108c
SS
22050 }
22051 else
22052 {
22053 /* We do not know the address of this symbol.
c5aa993b
JM
22054 If it is an external symbol and we have type information
22055 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22056 The address of the variable will then be determined from
22057 the minimal symbol table whenever the variable is
22058 referenced. */
e142c38c 22059 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22060
22061 /* Fortran explicitly imports any global symbols to the local
22062 scope by DW_TAG_common_block. */
22063 if (cu->language == language_fortran && die->parent
22064 && die->parent->tag == DW_TAG_common_block)
22065 {
22066 /* SYMBOL_CLASS doesn't matter here because
22067 read_common_block is going to reset it. */
22068 if (!suppress_add)
22069 list_to_add = cu->list_in_scope;
22070 }
22071 else if (attr2 && (DW_UNSND (attr2) != 0)
22072 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22073 {
0fe7935b
DJ
22074 /* A variable with DW_AT_external is never static, but it
22075 may be block-scoped. */
804d2729 22076 list_to_add
c24bdb02
KS
22077 = ((cu->list_in_scope
22078 == cu->get_builder ()->get_file_symbols ())
22079 ? cu->get_builder ()->get_global_symbols ()
804d2729 22080 : cu->list_in_scope);
0fe7935b 22081
f1e6e072 22082 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22083 }
442ddf59
JK
22084 else if (!die_is_declaration (die, cu))
22085 {
22086 /* Use the default LOC_OPTIMIZED_OUT class. */
22087 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22088 if (!suppress_add)
22089 list_to_add = cu->list_in_scope;
442ddf59 22090 }
c906108c
SS
22091 }
22092 break;
22093 case DW_TAG_formal_parameter:
a60f3166
TT
22094 {
22095 /* If we are inside a function, mark this as an argument. If
22096 not, we might be looking at an argument to an inlined function
22097 when we do not have enough information to show inlined frames;
22098 pretend it's a local variable in that case so that the user can
22099 still see it. */
804d2729 22100 struct context_stack *curr
c24bdb02 22101 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22102 if (curr != nullptr && curr->name != nullptr)
22103 SYMBOL_IS_ARGUMENT (sym) = 1;
22104 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22105 if (attr != nullptr)
a60f3166
TT
22106 {
22107 var_decode_location (attr, sym, cu);
22108 }
22109 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22110 if (attr != nullptr)
a60f3166
TT
22111 {
22112 dwarf2_const_value (attr, sym, cu);
22113 }
f346a30d 22114
a60f3166
TT
22115 list_to_add = cu->list_in_scope;
22116 }
c906108c
SS
22117 break;
22118 case DW_TAG_unspecified_parameters:
22119 /* From varargs functions; gdb doesn't seem to have any
22120 interest in this information, so just ignore it for now.
22121 (FIXME?) */
22122 break;
34eaf542
TT
22123 case DW_TAG_template_type_param:
22124 suppress_add = 1;
22125 /* Fall through. */
c906108c 22126 case DW_TAG_class_type:
680b30c7 22127 case DW_TAG_interface_type:
c906108c
SS
22128 case DW_TAG_structure_type:
22129 case DW_TAG_union_type:
72019c9c 22130 case DW_TAG_set_type:
c906108c 22131 case DW_TAG_enumeration_type:
f1e6e072 22132 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22133 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22134
63d06c5c 22135 {
9c37b5ae 22136 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22137 really ever be static objects: otherwise, if you try
22138 to, say, break of a class's method and you're in a file
22139 which doesn't mention that class, it won't work unless
22140 the check for all static symbols in lookup_symbol_aux
22141 saves you. See the OtherFileClass tests in
22142 gdb.c++/namespace.exp. */
22143
e37fd15a 22144 if (!suppress_add)
34eaf542 22145 {
c24bdb02 22146 buildsym_compunit *builder = cu->get_builder ();
804d2729 22147 list_to_add
c24bdb02 22148 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22149 && cu->language == language_cplus
c24bdb02 22150 ? builder->get_global_symbols ()
804d2729 22151 : cu->list_in_scope);
63d06c5c 22152
64382290 22153 /* The semantics of C++ state that "struct foo {
9c37b5ae 22154 ... }" also defines a typedef for "foo". */
64382290 22155 if (cu->language == language_cplus
45280282 22156 || cu->language == language_ada
c44af4eb
TT
22157 || cu->language == language_d
22158 || cu->language == language_rust)
64382290
TT
22159 {
22160 /* The symbol's name is already allocated along
22161 with this objfile, so we don't need to
22162 duplicate it for the type. */
22163 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 22164 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 22165 }
63d06c5c
DC
22166 }
22167 }
c906108c
SS
22168 break;
22169 case DW_TAG_typedef:
f1e6e072 22170 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22171 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22172 list_to_add = cu->list_in_scope;
63d06c5c 22173 break;
c906108c 22174 case DW_TAG_base_type:
a02abb62 22175 case DW_TAG_subrange_type:
f1e6e072 22176 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22177 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22178 list_to_add = cu->list_in_scope;
c906108c
SS
22179 break;
22180 case DW_TAG_enumerator:
e142c38c 22181 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22182 if (attr != nullptr)
c906108c 22183 {
e7c27a73 22184 dwarf2_const_value (attr, sym, cu);
c906108c 22185 }
63d06c5c
DC
22186 {
22187 /* NOTE: carlton/2003-11-10: See comment above in the
22188 DW_TAG_class_type, etc. block. */
22189
804d2729 22190 list_to_add
c24bdb02 22191 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22192 && cu->language == language_cplus
c24bdb02 22193 ? cu->get_builder ()->get_global_symbols ()
804d2729 22194 : cu->list_in_scope);
63d06c5c 22195 }
c906108c 22196 break;
74921315 22197 case DW_TAG_imported_declaration:
5c4e30ca 22198 case DW_TAG_namespace:
f1e6e072 22199 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22200 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22201 break;
530e8392
KB
22202 case DW_TAG_module:
22203 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22204 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22205 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22206 break;
4357ac6c 22207 case DW_TAG_common_block:
f1e6e072 22208 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22209 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22210 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22211 break;
c906108c
SS
22212 default:
22213 /* Not a tag we recognize. Hopefully we aren't processing
22214 trash data, but since we must specifically ignore things
22215 we don't recognize, there is nothing else we should do at
0963b4bd 22216 this point. */
b98664d3 22217 complaint (_("unsupported tag: '%s'"),
4d3c2250 22218 dwarf_tag_name (die->tag));
c906108c
SS
22219 break;
22220 }
df8a16a1 22221
e37fd15a
SW
22222 if (suppress_add)
22223 {
22224 sym->hash_next = objfile->template_symbols;
22225 objfile->template_symbols = sym;
22226 list_to_add = NULL;
22227 }
22228
22229 if (list_to_add != NULL)
d3cb6808 22230 add_symbol_to_list (sym, list_to_add);
e37fd15a 22231
df8a16a1
DJ
22232 /* For the benefit of old versions of GCC, check for anonymous
22233 namespaces based on the demangled name. */
4d4ec4e5 22234 if (!cu->processing_has_namespace_info
94af9270 22235 && cu->language == language_cplus)
c24bdb02 22236 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22237 }
22238 return (sym);
22239}
22240
98bfdba5
PA
22241/* Given an attr with a DW_FORM_dataN value in host byte order,
22242 zero-extend it as appropriate for the symbol's type. The DWARF
22243 standard (v4) is not entirely clear about the meaning of using
22244 DW_FORM_dataN for a constant with a signed type, where the type is
22245 wider than the data. The conclusion of a discussion on the DWARF
22246 list was that this is unspecified. We choose to always zero-extend
22247 because that is the interpretation long in use by GCC. */
c906108c 22248
98bfdba5 22249static gdb_byte *
ff39bb5e 22250dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22251 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22252{
518817b3 22253 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22254 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22255 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22256 LONGEST l = DW_UNSND (attr);
22257
22258 if (bits < sizeof (*value) * 8)
22259 {
22260 l &= ((LONGEST) 1 << bits) - 1;
22261 *value = l;
22262 }
22263 else if (bits == sizeof (*value) * 8)
22264 *value = l;
22265 else
22266 {
224c3ddb 22267 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22268 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22269 return bytes;
22270 }
22271
22272 return NULL;
22273}
22274
22275/* Read a constant value from an attribute. Either set *VALUE, or if
22276 the value does not fit in *VALUE, set *BYTES - either already
22277 allocated on the objfile obstack, or newly allocated on OBSTACK,
22278 or, set *BATON, if we translated the constant to a location
22279 expression. */
22280
22281static void
ff39bb5e 22282dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22283 const char *name, struct obstack *obstack,
22284 struct dwarf2_cu *cu,
d521ce57 22285 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22286 struct dwarf2_locexpr_baton **baton)
22287{
518817b3 22288 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22289 struct comp_unit_head *cu_header = &cu->header;
c906108c 22290 struct dwarf_block *blk;
98bfdba5
PA
22291 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22292 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22293
22294 *value = 0;
22295 *bytes = NULL;
22296 *baton = NULL;
c906108c
SS
22297
22298 switch (attr->form)
22299 {
22300 case DW_FORM_addr:
336d760d 22301 case DW_FORM_addrx:
3019eac3 22302 case DW_FORM_GNU_addr_index:
ac56253d 22303 {
ac56253d
TT
22304 gdb_byte *data;
22305
98bfdba5
PA
22306 if (TYPE_LENGTH (type) != cu_header->addr_size)
22307 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22308 cu_header->addr_size,
98bfdba5 22309 TYPE_LENGTH (type));
ac56253d
TT
22310 /* Symbols of this form are reasonably rare, so we just
22311 piggyback on the existing location code rather than writing
22312 a new implementation of symbol_computed_ops. */
8d749320 22313 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22314 (*baton)->per_cu = cu->per_cu;
22315 gdb_assert ((*baton)->per_cu);
ac56253d 22316
98bfdba5 22317 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22318 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22319 (*baton)->data = data;
ac56253d
TT
22320
22321 data[0] = DW_OP_addr;
22322 store_unsigned_integer (&data[1], cu_header->addr_size,
22323 byte_order, DW_ADDR (attr));
22324 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22325 }
c906108c 22326 break;
4ac36638 22327 case DW_FORM_string:
93b5768b 22328 case DW_FORM_strp:
cf532bd1 22329 case DW_FORM_strx:
3019eac3 22330 case DW_FORM_GNU_str_index:
36586728 22331 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22332 /* DW_STRING is already allocated on the objfile obstack, point
22333 directly to it. */
d521ce57 22334 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22335 break;
c906108c
SS
22336 case DW_FORM_block1:
22337 case DW_FORM_block2:
22338 case DW_FORM_block4:
22339 case DW_FORM_block:
2dc7f7b3 22340 case DW_FORM_exprloc:
0224619f 22341 case DW_FORM_data16:
c906108c 22342 blk = DW_BLOCK (attr);
98bfdba5
PA
22343 if (TYPE_LENGTH (type) != blk->size)
22344 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22345 TYPE_LENGTH (type));
22346 *bytes = blk->data;
c906108c 22347 break;
2df3850c
JM
22348
22349 /* The DW_AT_const_value attributes are supposed to carry the
22350 symbol's value "represented as it would be on the target
22351 architecture." By the time we get here, it's already been
22352 converted to host endianness, so we just need to sign- or
22353 zero-extend it as appropriate. */
22354 case DW_FORM_data1:
3aef2284 22355 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22356 break;
c906108c 22357 case DW_FORM_data2:
3aef2284 22358 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22359 break;
c906108c 22360 case DW_FORM_data4:
3aef2284 22361 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22362 break;
c906108c 22363 case DW_FORM_data8:
3aef2284 22364 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22365 break;
22366
c906108c 22367 case DW_FORM_sdata:
663c44ac 22368 case DW_FORM_implicit_const:
98bfdba5 22369 *value = DW_SND (attr);
2df3850c
JM
22370 break;
22371
c906108c 22372 case DW_FORM_udata:
98bfdba5 22373 *value = DW_UNSND (attr);
c906108c 22374 break;
2df3850c 22375
c906108c 22376 default:
b98664d3 22377 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22378 dwarf_form_name (attr->form));
98bfdba5 22379 *value = 0;
c906108c
SS
22380 break;
22381 }
22382}
22383
2df3850c 22384
98bfdba5
PA
22385/* Copy constant value from an attribute to a symbol. */
22386
2df3850c 22387static void
ff39bb5e 22388dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22389 struct dwarf2_cu *cu)
2df3850c 22390{
518817b3 22391 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22392 LONGEST value;
d521ce57 22393 const gdb_byte *bytes;
98bfdba5 22394 struct dwarf2_locexpr_baton *baton;
2df3850c 22395
98bfdba5 22396 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22397 sym->print_name (),
98bfdba5
PA
22398 &objfile->objfile_obstack, cu,
22399 &value, &bytes, &baton);
2df3850c 22400
98bfdba5
PA
22401 if (baton != NULL)
22402 {
98bfdba5 22403 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22404 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22405 }
22406 else if (bytes != NULL)
22407 {
22408 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22409 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22410 }
22411 else
22412 {
22413 SYMBOL_VALUE (sym) = value;
f1e6e072 22414 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22415 }
2df3850c
JM
22416}
22417
c906108c
SS
22418/* Return the type of the die in question using its DW_AT_type attribute. */
22419
22420static struct type *
e7c27a73 22421die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22422{
c906108c 22423 struct attribute *type_attr;
c906108c 22424
e142c38c 22425 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22426 if (!type_attr)
22427 {
518817b3 22428 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22429 /* A missing DW_AT_type represents a void type. */
518817b3 22430 return objfile_type (objfile)->builtin_void;
c906108c 22431 }
348e048f 22432
673bfd45 22433 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22434}
22435
b4ba55a1
JB
22436/* True iff CU's producer generates GNAT Ada auxiliary information
22437 that allows to find parallel types through that information instead
22438 of having to do expensive parallel lookups by type name. */
22439
22440static int
22441need_gnat_info (struct dwarf2_cu *cu)
22442{
de4cb04a
JB
22443 /* Assume that the Ada compiler was GNAT, which always produces
22444 the auxiliary information. */
22445 return (cu->language == language_ada);
b4ba55a1
JB
22446}
22447
b4ba55a1
JB
22448/* Return the auxiliary type of the die in question using its
22449 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22450 attribute is not present. */
22451
22452static struct type *
22453die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22454{
b4ba55a1 22455 struct attribute *type_attr;
b4ba55a1
JB
22456
22457 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22458 if (!type_attr)
22459 return NULL;
22460
673bfd45 22461 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22462}
22463
22464/* If DIE has a descriptive_type attribute, then set the TYPE's
22465 descriptive type accordingly. */
22466
22467static void
22468set_descriptive_type (struct type *type, struct die_info *die,
22469 struct dwarf2_cu *cu)
22470{
22471 struct type *descriptive_type = die_descriptive_type (die, cu);
22472
22473 if (descriptive_type)
22474 {
22475 ALLOCATE_GNAT_AUX_TYPE (type);
22476 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22477 }
22478}
22479
c906108c
SS
22480/* Return the containing type of the die in question using its
22481 DW_AT_containing_type attribute. */
22482
22483static struct type *
e7c27a73 22484die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22485{
c906108c 22486 struct attribute *type_attr;
518817b3 22487 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22488
e142c38c 22489 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22490 if (!type_attr)
22491 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22492 "[in module %s]"), objfile_name (objfile));
33ac96f0 22493
673bfd45 22494 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22495}
22496
ac9ec31b
DE
22497/* Return an error marker type to use for the ill formed type in DIE/CU. */
22498
22499static struct type *
22500build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22501{
518817b3
SM
22502 struct dwarf2_per_objfile *dwarf2_per_objfile
22503 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22504 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22505 char *saved;
ac9ec31b 22506
528e1572
SM
22507 std::string message
22508 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22509 objfile_name (objfile),
22510 sect_offset_str (cu->header.sect_off),
22511 sect_offset_str (die->sect_off));
efba19b0 22512 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22513
19f392bc 22514 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22515}
22516
673bfd45 22517/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22518 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22519 DW_AT_containing_type.
673bfd45
DE
22520 If there is no type substitute an error marker. */
22521
c906108c 22522static struct type *
ff39bb5e 22523lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22524 struct dwarf2_cu *cu)
c906108c 22525{
518817b3
SM
22526 struct dwarf2_per_objfile *dwarf2_per_objfile
22527 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22528 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22529 struct type *this_type;
22530
ac9ec31b
DE
22531 gdb_assert (attr->name == DW_AT_type
22532 || attr->name == DW_AT_GNAT_descriptive_type
22533 || attr->name == DW_AT_containing_type);
22534
673bfd45
DE
22535 /* First see if we have it cached. */
22536
36586728
TT
22537 if (attr->form == DW_FORM_GNU_ref_alt)
22538 {
22539 struct dwarf2_per_cu_data *per_cu;
9c541725 22540 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22541
ed2dc618
SM
22542 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22543 dwarf2_per_objfile);
9c541725 22544 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22545 }
7771576e 22546 else if (attr_form_is_ref (attr))
673bfd45 22547 {
9c541725 22548 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22549
9c541725 22550 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22551 }
55f1336d 22552 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22553 {
ac9ec31b 22554 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22555
ac9ec31b 22556 return get_signatured_type (die, signature, cu);
673bfd45
DE
22557 }
22558 else
22559 {
b98664d3 22560 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22561 " at %s [in module %s]"),
22562 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22563 objfile_name (objfile));
ac9ec31b 22564 return build_error_marker_type (cu, die);
673bfd45
DE
22565 }
22566
22567 /* If not cached we need to read it in. */
22568
22569 if (this_type == NULL)
22570 {
ac9ec31b 22571 struct die_info *type_die = NULL;
673bfd45
DE
22572 struct dwarf2_cu *type_cu = cu;
22573
7771576e 22574 if (attr_form_is_ref (attr))
ac9ec31b
DE
22575 type_die = follow_die_ref (die, attr, &type_cu);
22576 if (type_die == NULL)
22577 return build_error_marker_type (cu, die);
22578 /* If we find the type now, it's probably because the type came
3019eac3
DE
22579 from an inter-CU reference and the type's CU got expanded before
22580 ours. */
ac9ec31b 22581 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22582 }
22583
22584 /* If we still don't have a type use an error marker. */
22585
22586 if (this_type == NULL)
ac9ec31b 22587 return build_error_marker_type (cu, die);
673bfd45 22588
f792889a 22589 return this_type;
c906108c
SS
22590}
22591
673bfd45
DE
22592/* Return the type in DIE, CU.
22593 Returns NULL for invalid types.
22594
02142a6c 22595 This first does a lookup in die_type_hash,
673bfd45
DE
22596 and only reads the die in if necessary.
22597
22598 NOTE: This can be called when reading in partial or full symbols. */
22599
f792889a 22600static struct type *
e7c27a73 22601read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22602{
f792889a
DJ
22603 struct type *this_type;
22604
22605 this_type = get_die_type (die, cu);
22606 if (this_type)
22607 return this_type;
22608
673bfd45
DE
22609 return read_type_die_1 (die, cu);
22610}
22611
22612/* Read the type in DIE, CU.
22613 Returns NULL for invalid types. */
22614
22615static struct type *
22616read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22617{
22618 struct type *this_type = NULL;
22619
c906108c
SS
22620 switch (die->tag)
22621 {
22622 case DW_TAG_class_type:
680b30c7 22623 case DW_TAG_interface_type:
c906108c
SS
22624 case DW_TAG_structure_type:
22625 case DW_TAG_union_type:
f792889a 22626 this_type = read_structure_type (die, cu);
c906108c
SS
22627 break;
22628 case DW_TAG_enumeration_type:
f792889a 22629 this_type = read_enumeration_type (die, cu);
c906108c
SS
22630 break;
22631 case DW_TAG_subprogram:
22632 case DW_TAG_subroutine_type:
edb3359d 22633 case DW_TAG_inlined_subroutine:
f792889a 22634 this_type = read_subroutine_type (die, cu);
c906108c
SS
22635 break;
22636 case DW_TAG_array_type:
f792889a 22637 this_type = read_array_type (die, cu);
c906108c 22638 break;
72019c9c 22639 case DW_TAG_set_type:
f792889a 22640 this_type = read_set_type (die, cu);
72019c9c 22641 break;
c906108c 22642 case DW_TAG_pointer_type:
f792889a 22643 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22644 break;
22645 case DW_TAG_ptr_to_member_type:
f792889a 22646 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22647 break;
22648 case DW_TAG_reference_type:
4297a3f0
AV
22649 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22650 break;
22651 case DW_TAG_rvalue_reference_type:
22652 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22653 break;
22654 case DW_TAG_const_type:
f792889a 22655 this_type = read_tag_const_type (die, cu);
c906108c
SS
22656 break;
22657 case DW_TAG_volatile_type:
f792889a 22658 this_type = read_tag_volatile_type (die, cu);
c906108c 22659 break;
06d66ee9
TT
22660 case DW_TAG_restrict_type:
22661 this_type = read_tag_restrict_type (die, cu);
22662 break;
c906108c 22663 case DW_TAG_string_type:
f792889a 22664 this_type = read_tag_string_type (die, cu);
c906108c
SS
22665 break;
22666 case DW_TAG_typedef:
f792889a 22667 this_type = read_typedef (die, cu);
c906108c 22668 break;
a02abb62 22669 case DW_TAG_subrange_type:
f792889a 22670 this_type = read_subrange_type (die, cu);
a02abb62 22671 break;
c906108c 22672 case DW_TAG_base_type:
f792889a 22673 this_type = read_base_type (die, cu);
c906108c 22674 break;
81a17f79 22675 case DW_TAG_unspecified_type:
f792889a 22676 this_type = read_unspecified_type (die, cu);
81a17f79 22677 break;
0114d602
DJ
22678 case DW_TAG_namespace:
22679 this_type = read_namespace_type (die, cu);
22680 break;
f55ee35c
JK
22681 case DW_TAG_module:
22682 this_type = read_module_type (die, cu);
22683 break;
a2c2acaf
MW
22684 case DW_TAG_atomic_type:
22685 this_type = read_tag_atomic_type (die, cu);
22686 break;
c906108c 22687 default:
b98664d3 22688 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22689 dwarf_tag_name (die->tag));
c906108c
SS
22690 break;
22691 }
63d06c5c 22692
f792889a 22693 return this_type;
63d06c5c
DC
22694}
22695
abc72ce4
DE
22696/* See if we can figure out if the class lives in a namespace. We do
22697 this by looking for a member function; its demangled name will
22698 contain namespace info, if there is any.
22699 Return the computed name or NULL.
22700 Space for the result is allocated on the objfile's obstack.
22701 This is the full-die version of guess_partial_die_structure_name.
22702 In this case we know DIE has no useful parent. */
22703
43816ebc 22704static const char *
abc72ce4
DE
22705guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22706{
22707 struct die_info *spec_die;
22708 struct dwarf2_cu *spec_cu;
22709 struct die_info *child;
518817b3 22710 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22711
22712 spec_cu = cu;
22713 spec_die = die_specification (die, &spec_cu);
22714 if (spec_die != NULL)
22715 {
22716 die = spec_die;
22717 cu = spec_cu;
22718 }
22719
22720 for (child = die->child;
22721 child != NULL;
22722 child = child->sibling)
22723 {
22724 if (child->tag == DW_TAG_subprogram)
22725 {
73b9be8b 22726 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22727
7d45c7c3 22728 if (linkage_name != NULL)
abc72ce4 22729 {
43816ebc
TT
22730 gdb::unique_xmalloc_ptr<char> actual_name
22731 (language_class_name_from_physname (cu->language_defn,
22732 linkage_name));
22733 const char *name = NULL;
abc72ce4
DE
22734
22735 if (actual_name != NULL)
22736 {
15d034d0 22737 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22738
22739 if (die_name != NULL
43816ebc 22740 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22741 {
22742 /* Strip off the class name from the full name.
22743 We want the prefix. */
22744 int die_name_len = strlen (die_name);
43816ebc
TT
22745 int actual_name_len = strlen (actual_name.get ());
22746 const char *ptr = actual_name.get ();
abc72ce4
DE
22747
22748 /* Test for '::' as a sanity check. */
22749 if (actual_name_len > die_name_len + 2
43816ebc 22750 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22751 name = obstack_strndup (
e3b94546 22752 &objfile->per_bfd->storage_obstack,
43816ebc 22753 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22754 }
22755 }
abc72ce4
DE
22756 return name;
22757 }
22758 }
22759 }
22760
22761 return NULL;
22762}
22763
96408a79
SA
22764/* GCC might emit a nameless typedef that has a linkage name. Determine the
22765 prefix part in such case. See
22766 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22767
a121b7c1 22768static const char *
96408a79
SA
22769anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22770{
22771 struct attribute *attr;
e6a959d6 22772 const char *base;
96408a79
SA
22773
22774 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22775 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22776 return NULL;
22777
7d45c7c3 22778 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22779 return NULL;
22780
73b9be8b 22781 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22782 if (attr == NULL || DW_STRING (attr) == NULL)
22783 return NULL;
22784
22785 /* dwarf2_name had to be already called. */
22786 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22787
22788 /* Strip the base name, keep any leading namespaces/classes. */
22789 base = strrchr (DW_STRING (attr), ':');
22790 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22791 return "";
22792
518817b3 22793 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22794 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22795 DW_STRING (attr),
22796 &base[-1] - DW_STRING (attr));
96408a79
SA
22797}
22798
fdde2d81 22799/* Return the name of the namespace/class that DIE is defined within,
0114d602 22800 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22801
0114d602
DJ
22802 For example, if we're within the method foo() in the following
22803 code:
22804
22805 namespace N {
22806 class C {
22807 void foo () {
22808 }
22809 };
22810 }
22811
22812 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22813
0d5cff50 22814static const char *
e142c38c 22815determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22816{
518817b3
SM
22817 struct dwarf2_per_objfile *dwarf2_per_objfile
22818 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22819 struct die_info *parent, *spec_die;
22820 struct dwarf2_cu *spec_cu;
22821 struct type *parent_type;
a121b7c1 22822 const char *retval;
63d06c5c 22823
9c37b5ae 22824 if (cu->language != language_cplus
c44af4eb
TT
22825 && cu->language != language_fortran && cu->language != language_d
22826 && cu->language != language_rust)
0114d602
DJ
22827 return "";
22828
96408a79
SA
22829 retval = anonymous_struct_prefix (die, cu);
22830 if (retval)
22831 return retval;
22832
0114d602
DJ
22833 /* We have to be careful in the presence of DW_AT_specification.
22834 For example, with GCC 3.4, given the code
22835
22836 namespace N {
22837 void foo() {
22838 // Definition of N::foo.
22839 }
22840 }
22841
22842 then we'll have a tree of DIEs like this:
22843
22844 1: DW_TAG_compile_unit
22845 2: DW_TAG_namespace // N
22846 3: DW_TAG_subprogram // declaration of N::foo
22847 4: DW_TAG_subprogram // definition of N::foo
22848 DW_AT_specification // refers to die #3
22849
22850 Thus, when processing die #4, we have to pretend that we're in
22851 the context of its DW_AT_specification, namely the contex of die
22852 #3. */
22853 spec_cu = cu;
22854 spec_die = die_specification (die, &spec_cu);
22855 if (spec_die == NULL)
22856 parent = die->parent;
22857 else
63d06c5c 22858 {
0114d602
DJ
22859 parent = spec_die->parent;
22860 cu = spec_cu;
63d06c5c 22861 }
0114d602
DJ
22862
22863 if (parent == NULL)
22864 return "";
98bfdba5
PA
22865 else if (parent->building_fullname)
22866 {
22867 const char *name;
22868 const char *parent_name;
22869
22870 /* It has been seen on RealView 2.2 built binaries,
22871 DW_TAG_template_type_param types actually _defined_ as
22872 children of the parent class:
22873
22874 enum E {};
22875 template class <class Enum> Class{};
22876 Class<enum E> class_e;
22877
22878 1: DW_TAG_class_type (Class)
22879 2: DW_TAG_enumeration_type (E)
22880 3: DW_TAG_enumerator (enum1:0)
22881 3: DW_TAG_enumerator (enum2:1)
22882 ...
22883 2: DW_TAG_template_type_param
22884 DW_AT_type DW_FORM_ref_udata (E)
22885
22886 Besides being broken debug info, it can put GDB into an
22887 infinite loop. Consider:
22888
22889 When we're building the full name for Class<E>, we'll start
22890 at Class, and go look over its template type parameters,
22891 finding E. We'll then try to build the full name of E, and
22892 reach here. We're now trying to build the full name of E,
22893 and look over the parent DIE for containing scope. In the
22894 broken case, if we followed the parent DIE of E, we'd again
22895 find Class, and once again go look at its template type
22896 arguments, etc., etc. Simply don't consider such parent die
22897 as source-level parent of this die (it can't be, the language
22898 doesn't allow it), and break the loop here. */
22899 name = dwarf2_name (die, cu);
22900 parent_name = dwarf2_name (parent, cu);
b98664d3 22901 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22902 name ? name : "<unknown>",
22903 parent_name ? parent_name : "<unknown>");
22904 return "";
22905 }
63d06c5c 22906 else
0114d602
DJ
22907 switch (parent->tag)
22908 {
63d06c5c 22909 case DW_TAG_namespace:
0114d602 22910 parent_type = read_type_die (parent, cu);
acebe513
UW
22911 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22912 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22913 Work around this problem here. */
22914 if (cu->language == language_cplus
e86ca25f 22915 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22916 return "";
0114d602 22917 /* We give a name to even anonymous namespaces. */
e86ca25f 22918 return TYPE_NAME (parent_type);
63d06c5c 22919 case DW_TAG_class_type:
680b30c7 22920 case DW_TAG_interface_type:
63d06c5c 22921 case DW_TAG_structure_type:
0114d602 22922 case DW_TAG_union_type:
f55ee35c 22923 case DW_TAG_module:
0114d602 22924 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22925 if (TYPE_NAME (parent_type) != NULL)
22926 return TYPE_NAME (parent_type);
0114d602
DJ
22927 else
22928 /* An anonymous structure is only allowed non-static data
22929 members; no typedefs, no member functions, et cetera.
22930 So it does not need a prefix. */
22931 return "";
abc72ce4 22932 case DW_TAG_compile_unit:
95554aad 22933 case DW_TAG_partial_unit:
abc72ce4
DE
22934 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22935 if (cu->language == language_cplus
fd5866f6 22936 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22937 && die->child != NULL
22938 && (die->tag == DW_TAG_class_type
22939 || die->tag == DW_TAG_structure_type
22940 || die->tag == DW_TAG_union_type))
22941 {
43816ebc 22942 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22943 if (name != NULL)
22944 return name;
22945 }
22946 return "";
0a4b0913
AB
22947 case DW_TAG_subprogram:
22948 /* Nested subroutines in Fortran get a prefix with the name
22949 of the parent's subroutine. */
22950 if (cu->language == language_fortran)
22951 {
22952 if ((die->tag == DW_TAG_subprogram)
22953 && (dwarf2_name (parent, cu) != NULL))
22954 return dwarf2_name (parent, cu);
22955 }
22956 return determine_prefix (parent, cu);
3d567982
TT
22957 case DW_TAG_enumeration_type:
22958 parent_type = read_type_die (parent, cu);
22959 if (TYPE_DECLARED_CLASS (parent_type))
22960 {
e86ca25f
TT
22961 if (TYPE_NAME (parent_type) != NULL)
22962 return TYPE_NAME (parent_type);
3d567982
TT
22963 return "";
22964 }
22965 /* Fall through. */
63d06c5c 22966 default:
8176b9b8 22967 return determine_prefix (parent, cu);
63d06c5c 22968 }
63d06c5c
DC
22969}
22970
3e43a32a
MS
22971/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22972 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22973 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22974 an obconcat, otherwise allocate storage for the result. The CU argument is
22975 used to determine the language and hence, the appropriate separator. */
987504bb 22976
f55ee35c 22977#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22978
22979static char *
f55ee35c
JK
22980typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22981 int physname, struct dwarf2_cu *cu)
63d06c5c 22982{
f55ee35c 22983 const char *lead = "";
5c315b68 22984 const char *sep;
63d06c5c 22985
3e43a32a
MS
22986 if (suffix == NULL || suffix[0] == '\0'
22987 || prefix == NULL || prefix[0] == '\0')
987504bb 22988 sep = "";
45280282
IB
22989 else if (cu->language == language_d)
22990 {
22991 /* For D, the 'main' function could be defined in any module, but it
22992 should never be prefixed. */
22993 if (strcmp (suffix, "D main") == 0)
22994 {
22995 prefix = "";
22996 sep = "";
22997 }
22998 else
22999 sep = ".";
23000 }
f55ee35c
JK
23001 else if (cu->language == language_fortran && physname)
23002 {
23003 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23004 DW_AT_MIPS_linkage_name is preferred and used instead. */
23005
23006 lead = "__";
23007 sep = "_MOD_";
23008 }
987504bb
JJ
23009 else
23010 sep = "::";
63d06c5c 23011
6dd47d34
DE
23012 if (prefix == NULL)
23013 prefix = "";
23014 if (suffix == NULL)
23015 suffix = "";
23016
987504bb
JJ
23017 if (obs == NULL)
23018 {
3e43a32a 23019 char *retval
224c3ddb
SM
23020 = ((char *)
23021 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23022
f55ee35c
JK
23023 strcpy (retval, lead);
23024 strcat (retval, prefix);
6dd47d34
DE
23025 strcat (retval, sep);
23026 strcat (retval, suffix);
63d06c5c
DC
23027 return retval;
23028 }
987504bb
JJ
23029 else
23030 {
23031 /* We have an obstack. */
f55ee35c 23032 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23033 }
63d06c5c
DC
23034}
23035
c906108c
SS
23036/* Return sibling of die, NULL if no sibling. */
23037
f9aca02d 23038static struct die_info *
fba45db2 23039sibling_die (struct die_info *die)
c906108c 23040{
639d11d3 23041 return die->sibling;
c906108c
SS
23042}
23043
71c25dea
TT
23044/* Get name of a die, return NULL if not found. */
23045
15d034d0
TT
23046static const char *
23047dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
23048 struct obstack *obstack)
23049{
23050 if (name && cu->language == language_cplus)
23051 {
2f408ecb 23052 std::string canon_name = cp_canonicalize_string (name);
71c25dea 23053
2f408ecb 23054 if (!canon_name.empty ())
71c25dea 23055 {
2f408ecb 23056 if (canon_name != name)
efba19b0 23057 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
23058 }
23059 }
23060
23061 return name;
c906108c
SS
23062}
23063
96553a0c
DE
23064/* Get name of a die, return NULL if not found.
23065 Anonymous namespaces are converted to their magic string. */
9219021c 23066
15d034d0 23067static const char *
e142c38c 23068dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23069{
23070 struct attribute *attr;
518817b3 23071 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 23072
e142c38c 23073 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 23074 if ((!attr || !DW_STRING (attr))
96553a0c 23075 && die->tag != DW_TAG_namespace
53832f31
TT
23076 && die->tag != DW_TAG_class_type
23077 && die->tag != DW_TAG_interface_type
23078 && die->tag != DW_TAG_structure_type
23079 && die->tag != DW_TAG_union_type)
71c25dea
TT
23080 return NULL;
23081
23082 switch (die->tag)
23083 {
23084 case DW_TAG_compile_unit:
95554aad 23085 case DW_TAG_partial_unit:
71c25dea
TT
23086 /* Compilation units have a DW_AT_name that is a filename, not
23087 a source language identifier. */
23088 case DW_TAG_enumeration_type:
23089 case DW_TAG_enumerator:
23090 /* These tags always have simple identifiers already; no need
23091 to canonicalize them. */
23092 return DW_STRING (attr);
907af001 23093
96553a0c
DE
23094 case DW_TAG_namespace:
23095 if (attr != NULL && DW_STRING (attr) != NULL)
23096 return DW_STRING (attr);
23097 return CP_ANONYMOUS_NAMESPACE_STR;
23098
907af001
UW
23099 case DW_TAG_class_type:
23100 case DW_TAG_interface_type:
23101 case DW_TAG_structure_type:
23102 case DW_TAG_union_type:
23103 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23104 structures or unions. These were of the form "._%d" in GCC 4.1,
23105 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23106 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 23107 if (attr && DW_STRING (attr)
61012eef
GB
23108 && (startswith (DW_STRING (attr), "._")
23109 || startswith (DW_STRING (attr), "<anonymous")))
907af001 23110 return NULL;
53832f31
TT
23111
23112 /* GCC might emit a nameless typedef that has a linkage name. See
23113 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23114 if (!attr || DW_STRING (attr) == NULL)
23115 {
73b9be8b 23116 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
23117 if (attr == NULL || DW_STRING (attr) == NULL)
23118 return NULL;
23119
df5c6c50
JK
23120 /* Avoid demangling DW_STRING (attr) the second time on a second
23121 call for the same DIE. */
23122 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 23123 {
43816ebc
TT
23124 gdb::unique_xmalloc_ptr<char> demangled
23125 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23126
e6a959d6 23127 const char *base;
96408a79 23128
53832f31 23129 /* FIXME: we already did this for the partial symbol... */
34a68019 23130 DW_STRING (attr)
021887d8 23131 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 23132 demangled.get ());
53832f31 23133 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
23134
23135 /* Strip any leading namespaces/classes, keep only the base name.
23136 DW_AT_name for named DIEs does not contain the prefixes. */
23137 base = strrchr (DW_STRING (attr), ':');
23138 if (base && base > DW_STRING (attr) && base[-1] == ':')
23139 return &base[1];
23140 else
23141 return DW_STRING (attr);
53832f31
TT
23142 }
23143 }
907af001
UW
23144 break;
23145
71c25dea 23146 default:
907af001
UW
23147 break;
23148 }
23149
23150 if (!DW_STRING_IS_CANONICAL (attr))
23151 {
23152 DW_STRING (attr)
23153 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 23154 &objfile->per_bfd->storage_obstack);
907af001 23155 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 23156 }
907af001 23157 return DW_STRING (attr);
9219021c
DC
23158}
23159
23160/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23161 is none. *EXT_CU is the CU containing DIE on input, and the CU
23162 containing the return value on output. */
9219021c
DC
23163
23164static struct die_info *
f2f0e013 23165dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23166{
23167 struct attribute *attr;
9219021c 23168
f2f0e013 23169 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23170 if (attr == NULL)
23171 return NULL;
23172
f2f0e013 23173 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23174}
23175
fa9c3fa0
TT
23176/* A convenience function that returns an "unknown" DWARF name,
23177 including the value of V. STR is the name of the entity being
23178 printed, e.g., "TAG". */
23179
23180static const char *
23181dwarf_unknown (const char *str, unsigned v)
23182{
23183 char *cell = get_print_cell ();
23184 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23185 return cell;
23186}
23187
c906108c
SS
23188/* Convert a DIE tag into its string name. */
23189
f39c6ffd 23190static const char *
aa1ee363 23191dwarf_tag_name (unsigned tag)
c906108c 23192{
f39c6ffd
TT
23193 const char *name = get_DW_TAG_name (tag);
23194
23195 if (name == NULL)
fa9c3fa0 23196 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
23197
23198 return name;
c906108c
SS
23199}
23200
23201/* Convert a DWARF attribute code into its string name. */
23202
f39c6ffd 23203static const char *
aa1ee363 23204dwarf_attr_name (unsigned attr)
c906108c 23205{
f39c6ffd
TT
23206 const char *name;
23207
c764a876 23208#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
23209 if (attr == DW_AT_MIPS_fde)
23210 return "DW_AT_MIPS_fde";
23211#else
23212 if (attr == DW_AT_HP_block_index)
23213 return "DW_AT_HP_block_index";
c764a876 23214#endif
f39c6ffd
TT
23215
23216 name = get_DW_AT_name (attr);
23217
23218 if (name == NULL)
fa9c3fa0 23219 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
23220
23221 return name;
c906108c
SS
23222}
23223
a084a2a6
AT
23224/* Convert a unit type to corresponding DW_UT name. */
23225
23226static const char *
23227dwarf_unit_type_name (int unit_type) {
23228 switch (unit_type)
23229 {
23230 case 0x01:
23231 return "DW_UT_compile (0x01)";
23232 case 0x02:
23233 return "DW_UT_type (0x02)";
23234 case 0x03:
23235 return "DW_UT_partial (0x03)";
23236 case 0x04:
23237 return "DW_UT_skeleton (0x04)";
23238 case 0x05:
23239 return "DW_UT_split_compile (0x05)";
23240 case 0x06:
23241 return "DW_UT_split_type (0x06)";
23242 case 0x80:
23243 return "DW_UT_lo_user (0x80)";
23244 case 0xff:
23245 return "DW_UT_hi_user (0xff)";
23246 default:
23247 return nullptr;
23248 }
23249}
23250
c906108c
SS
23251/* Convert a DWARF value form code into its string name. */
23252
f39c6ffd 23253static const char *
aa1ee363 23254dwarf_form_name (unsigned form)
c906108c 23255{
f39c6ffd
TT
23256 const char *name = get_DW_FORM_name (form);
23257
23258 if (name == NULL)
fa9c3fa0 23259 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
23260
23261 return name;
c906108c
SS
23262}
23263
a121b7c1 23264static const char *
fba45db2 23265dwarf_bool_name (unsigned mybool)
c906108c
SS
23266{
23267 if (mybool)
23268 return "TRUE";
23269 else
23270 return "FALSE";
23271}
23272
23273/* Convert a DWARF type code into its string name. */
23274
f39c6ffd 23275static const char *
aa1ee363 23276dwarf_type_encoding_name (unsigned enc)
c906108c 23277{
f39c6ffd 23278 const char *name = get_DW_ATE_name (enc);
c906108c 23279
f39c6ffd 23280 if (name == NULL)
fa9c3fa0 23281 return dwarf_unknown ("ATE", enc);
c906108c 23282
f39c6ffd 23283 return name;
c906108c 23284}
c906108c 23285
f9aca02d 23286static void
d97bc12b 23287dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23288{
23289 unsigned int i;
23290
d97bc12b 23291 print_spaces (indent, f);
9d8780f0 23292 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23293 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23294 sect_offset_str (die->sect_off));
d97bc12b
DE
23295
23296 if (die->parent != NULL)
23297 {
23298 print_spaces (indent, f);
9d8780f0
SM
23299 fprintf_unfiltered (f, " parent at offset: %s\n",
23300 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23301 }
23302
23303 print_spaces (indent, f);
23304 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23305 dwarf_bool_name (die->child != NULL));
c906108c 23306
d97bc12b
DE
23307 print_spaces (indent, f);
23308 fprintf_unfiltered (f, " attributes:\n");
23309
c906108c
SS
23310 for (i = 0; i < die->num_attrs; ++i)
23311 {
d97bc12b
DE
23312 print_spaces (indent, f);
23313 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23314 dwarf_attr_name (die->attrs[i].name),
23315 dwarf_form_name (die->attrs[i].form));
d97bc12b 23316
c906108c
SS
23317 switch (die->attrs[i].form)
23318 {
c906108c 23319 case DW_FORM_addr:
336d760d 23320 case DW_FORM_addrx:
3019eac3 23321 case DW_FORM_GNU_addr_index:
d97bc12b 23322 fprintf_unfiltered (f, "address: ");
5af949e3 23323 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23324 break;
23325 case DW_FORM_block2:
23326 case DW_FORM_block4:
23327 case DW_FORM_block:
23328 case DW_FORM_block1:
56eb65bd
SP
23329 fprintf_unfiltered (f, "block: size %s",
23330 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23331 break;
2dc7f7b3 23332 case DW_FORM_exprloc:
56eb65bd
SP
23333 fprintf_unfiltered (f, "expression: size %s",
23334 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23335 break;
0224619f
JK
23336 case DW_FORM_data16:
23337 fprintf_unfiltered (f, "constant of 16 bytes");
23338 break;
4568ecf9
DE
23339 case DW_FORM_ref_addr:
23340 fprintf_unfiltered (f, "ref address: ");
23341 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23342 break;
36586728
TT
23343 case DW_FORM_GNU_ref_alt:
23344 fprintf_unfiltered (f, "alt ref address: ");
23345 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23346 break;
10b3939b
DJ
23347 case DW_FORM_ref1:
23348 case DW_FORM_ref2:
23349 case DW_FORM_ref4:
4568ecf9
DE
23350 case DW_FORM_ref8:
23351 case DW_FORM_ref_udata:
d97bc12b 23352 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23353 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23354 break;
c906108c
SS
23355 case DW_FORM_data1:
23356 case DW_FORM_data2:
23357 case DW_FORM_data4:
ce5d95e1 23358 case DW_FORM_data8:
c906108c
SS
23359 case DW_FORM_udata:
23360 case DW_FORM_sdata:
43bbcdc2
PH
23361 fprintf_unfiltered (f, "constant: %s",
23362 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23363 break;
2dc7f7b3
TT
23364 case DW_FORM_sec_offset:
23365 fprintf_unfiltered (f, "section offset: %s",
23366 pulongest (DW_UNSND (&die->attrs[i])));
23367 break;
55f1336d 23368 case DW_FORM_ref_sig8:
ac9ec31b
DE
23369 fprintf_unfiltered (f, "signature: %s",
23370 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23371 break;
c906108c 23372 case DW_FORM_string:
4bdf3d34 23373 case DW_FORM_strp:
43988095 23374 case DW_FORM_line_strp:
cf532bd1 23375 case DW_FORM_strx:
3019eac3 23376 case DW_FORM_GNU_str_index:
36586728 23377 case DW_FORM_GNU_strp_alt:
8285870a 23378 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23379 DW_STRING (&die->attrs[i])
8285870a
JK
23380 ? DW_STRING (&die->attrs[i]) : "",
23381 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23382 break;
23383 case DW_FORM_flag:
23384 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23385 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23386 else
d97bc12b 23387 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23388 break;
2dc7f7b3
TT
23389 case DW_FORM_flag_present:
23390 fprintf_unfiltered (f, "flag: TRUE");
23391 break;
a8329558 23392 case DW_FORM_indirect:
0963b4bd
MS
23393 /* The reader will have reduced the indirect form to
23394 the "base form" so this form should not occur. */
5f48f8f3 23395 fprintf_unfiltered (f,
3e43a32a 23396 "unexpected attribute form: DW_FORM_indirect");
a8329558 23397 break;
663c44ac
JK
23398 case DW_FORM_implicit_const:
23399 fprintf_unfiltered (f, "constant: %s",
23400 plongest (DW_SND (&die->attrs[i])));
23401 break;
c906108c 23402 default:
d97bc12b 23403 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23404 die->attrs[i].form);
d97bc12b 23405 break;
c906108c 23406 }
d97bc12b 23407 fprintf_unfiltered (f, "\n");
c906108c
SS
23408 }
23409}
23410
f9aca02d 23411static void
d97bc12b 23412dump_die_for_error (struct die_info *die)
c906108c 23413{
d97bc12b
DE
23414 dump_die_shallow (gdb_stderr, 0, die);
23415}
23416
23417static void
23418dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23419{
23420 int indent = level * 4;
23421
23422 gdb_assert (die != NULL);
23423
23424 if (level >= max_level)
23425 return;
23426
23427 dump_die_shallow (f, indent, die);
23428
23429 if (die->child != NULL)
c906108c 23430 {
d97bc12b
DE
23431 print_spaces (indent, f);
23432 fprintf_unfiltered (f, " Children:");
23433 if (level + 1 < max_level)
23434 {
23435 fprintf_unfiltered (f, "\n");
23436 dump_die_1 (f, level + 1, max_level, die->child);
23437 }
23438 else
23439 {
3e43a32a
MS
23440 fprintf_unfiltered (f,
23441 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23442 }
23443 }
23444
23445 if (die->sibling != NULL && level > 0)
23446 {
23447 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23448 }
23449}
23450
d97bc12b
DE
23451/* This is called from the pdie macro in gdbinit.in.
23452 It's not static so gcc will keep a copy callable from gdb. */
23453
23454void
23455dump_die (struct die_info *die, int max_level)
23456{
23457 dump_die_1 (gdb_stdlog, 0, max_level, die);
23458}
23459
f9aca02d 23460static void
51545339 23461store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23462{
51545339 23463 void **slot;
c906108c 23464
9c541725
PA
23465 slot = htab_find_slot_with_hash (cu->die_hash, die,
23466 to_underlying (die->sect_off),
b64f50a1 23467 INSERT);
51545339
DJ
23468
23469 *slot = die;
c906108c
SS
23470}
23471
b64f50a1
JK
23472/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23473 required kind. */
23474
23475static sect_offset
ff39bb5e 23476dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23477{
7771576e 23478 if (attr_form_is_ref (attr))
9c541725 23479 return (sect_offset) DW_UNSND (attr);
93311388 23480
b98664d3 23481 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23482 dwarf_form_name (attr->form));
9c541725 23483 return {};
c906108c
SS
23484}
23485
43bbcdc2
PH
23486/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23487 * the value held by the attribute is not constant. */
a02abb62 23488
43bbcdc2 23489static LONGEST
ff39bb5e 23490dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23491{
663c44ac 23492 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23493 return DW_SND (attr);
23494 else if (attr->form == DW_FORM_udata
23495 || attr->form == DW_FORM_data1
23496 || attr->form == DW_FORM_data2
23497 || attr->form == DW_FORM_data4
23498 || attr->form == DW_FORM_data8)
23499 return DW_UNSND (attr);
23500 else
23501 {
0224619f 23502 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23503 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23504 dwarf_form_name (attr->form));
23505 return default_value;
23506 }
23507}
23508
348e048f
DE
23509/* Follow reference or signature attribute ATTR of SRC_DIE.
23510 On entry *REF_CU is the CU of SRC_DIE.
23511 On exit *REF_CU is the CU of the result. */
23512
23513static struct die_info *
ff39bb5e 23514follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23515 struct dwarf2_cu **ref_cu)
23516{
23517 struct die_info *die;
23518
7771576e 23519 if (attr_form_is_ref (attr))
348e048f 23520 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23521 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23522 die = follow_die_sig (src_die, attr, ref_cu);
23523 else
23524 {
23525 dump_die_for_error (src_die);
23526 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23527 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23528 }
23529
23530 return die;
03dd20cc
DJ
23531}
23532
5c631832 23533/* Follow reference OFFSET.
673bfd45
DE
23534 On entry *REF_CU is the CU of the source die referencing OFFSET.
23535 On exit *REF_CU is the CU of the result.
23536 Returns NULL if OFFSET is invalid. */
f504f079 23537
f9aca02d 23538static struct die_info *
9c541725 23539follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23540 struct dwarf2_cu **ref_cu)
c906108c 23541{
10b3939b 23542 struct die_info temp_die;
f2f0e013 23543 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23544 struct dwarf2_per_objfile *dwarf2_per_objfile
23545 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23546
348e048f
DE
23547 gdb_assert (cu->per_cu != NULL);
23548
98bfdba5
PA
23549 target_cu = cu;
23550
3019eac3 23551 if (cu->per_cu->is_debug_types)
348e048f
DE
23552 {
23553 /* .debug_types CUs cannot reference anything outside their CU.
23554 If they need to, they have to reference a signatured type via
55f1336d 23555 DW_FORM_ref_sig8. */
9c541725 23556 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23557 return NULL;
348e048f 23558 }
36586728 23559 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23560 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23561 {
23562 struct dwarf2_per_cu_data *per_cu;
9a619af0 23563
9c541725 23564 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23565 dwarf2_per_objfile);
03dd20cc
DJ
23566
23567 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23568 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23569 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23570
10b3939b
DJ
23571 target_cu = per_cu->cu;
23572 }
98bfdba5
PA
23573 else if (cu->dies == NULL)
23574 {
23575 /* We're loading full DIEs during partial symbol reading. */
23576 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23577 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23578 }
c906108c 23579
f2f0e013 23580 *ref_cu = target_cu;
9c541725 23581 temp_die.sect_off = sect_off;
c24bdb02
KS
23582
23583 if (target_cu != cu)
23584 target_cu->ancestor = cu;
23585
9a3c8263 23586 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23587 &temp_die,
23588 to_underlying (sect_off));
5c631832 23589}
10b3939b 23590
5c631832
JK
23591/* Follow reference attribute ATTR of SRC_DIE.
23592 On entry *REF_CU is the CU of SRC_DIE.
23593 On exit *REF_CU is the CU of the result. */
23594
23595static struct die_info *
ff39bb5e 23596follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23597 struct dwarf2_cu **ref_cu)
23598{
9c541725 23599 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23600 struct dwarf2_cu *cu = *ref_cu;
23601 struct die_info *die;
23602
9c541725 23603 die = follow_die_offset (sect_off,
36586728
TT
23604 (attr->form == DW_FORM_GNU_ref_alt
23605 || cu->per_cu->is_dwz),
23606 ref_cu);
5c631832 23607 if (!die)
9d8780f0
SM
23608 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23609 "at %s [in module %s]"),
23610 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23611 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23612
5c631832
JK
23613 return die;
23614}
23615
9c541725 23616/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23617 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23618 dwarf2_locexpr_baton->data has lifetime of
23619 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23620
23621struct dwarf2_locexpr_baton
9c541725 23622dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23623 struct dwarf2_per_cu_data *per_cu,
23624 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23625 void *baton, bool resolve_abstract_p)
5c631832 23626{
918dd910 23627 struct dwarf2_cu *cu;
5c631832
JK
23628 struct die_info *die;
23629 struct attribute *attr;
23630 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23631 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23632 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23633
918dd910 23634 if (per_cu->cu == NULL)
58f0c718 23635 load_cu (per_cu, false);
918dd910 23636 cu = per_cu->cu;
cc12ce38
DE
23637 if (cu == NULL)
23638 {
23639 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23640 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23641 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23642 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23643 }
918dd910 23644
9c541725 23645 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23646 if (!die)
9d8780f0
SM
23647 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23648 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23649
23650 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23651 if (!attr && resolve_abstract_p
3360b6e7 23652 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23653 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23654 {
23655 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 23656 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 23657 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23658
3360b6e7
TV
23659 for (const auto &cand_off
23660 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23661 {
3360b6e7
TV
23662 struct dwarf2_cu *cand_cu = cu;
23663 struct die_info *cand
23664 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23665 if (!cand
23666 || !cand->parent
e4a62c65
TV
23667 || cand->parent->tag != DW_TAG_subprogram)
23668 continue;
23669
23670 CORE_ADDR pc_low, pc_high;
23671 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23672 if (pc_low == ((CORE_ADDR) -1))
23673 continue;
23674 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23675 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23676 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23677 continue;
23678
23679 die = cand;
23680 attr = dwarf2_attr (die, DW_AT_location, cu);
23681 break;
23682 }
23683 }
23684
5c631832
JK
23685 if (!attr)
23686 {
e103e986
JK
23687 /* DWARF: "If there is no such attribute, then there is no effect.".
23688 DATA is ignored if SIZE is 0. */
5c631832 23689
e103e986 23690 retval.data = NULL;
5c631832
JK
23691 retval.size = 0;
23692 }
8cf6f0b1
TT
23693 else if (attr_form_is_section_offset (attr))
23694 {
23695 struct dwarf2_loclist_baton loclist_baton;
23696 CORE_ADDR pc = (*get_frame_pc) (baton);
23697 size_t size;
23698
23699 fill_in_loclist_baton (cu, &loclist_baton, attr);
23700
23701 retval.data = dwarf2_find_location_expression (&loclist_baton,
23702 &size, pc);
23703 retval.size = size;
23704 }
5c631832
JK
23705 else
23706 {
23707 if (!attr_form_is_block (attr))
9d8780f0 23708 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23709 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23710 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23711
23712 retval.data = DW_BLOCK (attr)->data;
23713 retval.size = DW_BLOCK (attr)->size;
23714 }
23715 retval.per_cu = cu->per_cu;
918dd910 23716
ed2dc618 23717 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23718
5c631832 23719 return retval;
348e048f
DE
23720}
23721
8b9737bf
TT
23722/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23723 offset. */
23724
23725struct dwarf2_locexpr_baton
23726dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23727 struct dwarf2_per_cu_data *per_cu,
23728 CORE_ADDR (*get_frame_pc) (void *baton),
23729 void *baton)
23730{
9c541725 23731 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23732
9c541725 23733 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23734}
23735
b6807d98
TT
23736/* Write a constant of a given type as target-ordered bytes into
23737 OBSTACK. */
23738
23739static const gdb_byte *
23740write_constant_as_bytes (struct obstack *obstack,
23741 enum bfd_endian byte_order,
23742 struct type *type,
23743 ULONGEST value,
23744 LONGEST *len)
23745{
23746 gdb_byte *result;
23747
23748 *len = TYPE_LENGTH (type);
224c3ddb 23749 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23750 store_unsigned_integer (result, *len, byte_order, value);
23751
23752 return result;
23753}
23754
23755/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23756 pointer to the constant bytes and set LEN to the length of the
23757 data. If memory is needed, allocate it on OBSTACK. If the DIE
23758 does not have a DW_AT_const_value, return NULL. */
23759
23760const gdb_byte *
9c541725 23761dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23762 struct dwarf2_per_cu_data *per_cu,
23763 struct obstack *obstack,
23764 LONGEST *len)
23765{
23766 struct dwarf2_cu *cu;
23767 struct die_info *die;
23768 struct attribute *attr;
23769 const gdb_byte *result = NULL;
23770 struct type *type;
23771 LONGEST value;
23772 enum bfd_endian byte_order;
e3b94546 23773 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23774
b6807d98 23775 if (per_cu->cu == NULL)
58f0c718 23776 load_cu (per_cu, false);
b6807d98 23777 cu = per_cu->cu;
cc12ce38
DE
23778 if (cu == NULL)
23779 {
23780 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23781 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23782 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23783 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23784 }
b6807d98 23785
9c541725 23786 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23787 if (!die)
9d8780f0
SM
23788 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23789 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23790
23791 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23792 if (attr == NULL)
23793 return NULL;
23794
e3b94546 23795 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23796 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23797
23798 switch (attr->form)
23799 {
23800 case DW_FORM_addr:
336d760d 23801 case DW_FORM_addrx:
b6807d98
TT
23802 case DW_FORM_GNU_addr_index:
23803 {
23804 gdb_byte *tem;
23805
23806 *len = cu->header.addr_size;
224c3ddb 23807 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23808 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23809 result = tem;
23810 }
23811 break;
23812 case DW_FORM_string:
23813 case DW_FORM_strp:
cf532bd1 23814 case DW_FORM_strx:
b6807d98
TT
23815 case DW_FORM_GNU_str_index:
23816 case DW_FORM_GNU_strp_alt:
23817 /* DW_STRING is already allocated on the objfile obstack, point
23818 directly to it. */
23819 result = (const gdb_byte *) DW_STRING (attr);
23820 *len = strlen (DW_STRING (attr));
23821 break;
23822 case DW_FORM_block1:
23823 case DW_FORM_block2:
23824 case DW_FORM_block4:
23825 case DW_FORM_block:
23826 case DW_FORM_exprloc:
0224619f 23827 case DW_FORM_data16:
b6807d98
TT
23828 result = DW_BLOCK (attr)->data;
23829 *len = DW_BLOCK (attr)->size;
23830 break;
23831
23832 /* The DW_AT_const_value attributes are supposed to carry the
23833 symbol's value "represented as it would be on the target
23834 architecture." By the time we get here, it's already been
23835 converted to host endianness, so we just need to sign- or
23836 zero-extend it as appropriate. */
23837 case DW_FORM_data1:
23838 type = die_type (die, cu);
23839 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23840 if (result == NULL)
23841 result = write_constant_as_bytes (obstack, byte_order,
23842 type, value, len);
23843 break;
23844 case DW_FORM_data2:
23845 type = die_type (die, cu);
23846 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23847 if (result == NULL)
23848 result = write_constant_as_bytes (obstack, byte_order,
23849 type, value, len);
23850 break;
23851 case DW_FORM_data4:
23852 type = die_type (die, cu);
23853 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23854 if (result == NULL)
23855 result = write_constant_as_bytes (obstack, byte_order,
23856 type, value, len);
23857 break;
23858 case DW_FORM_data8:
23859 type = die_type (die, cu);
23860 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23861 if (result == NULL)
23862 result = write_constant_as_bytes (obstack, byte_order,
23863 type, value, len);
23864 break;
23865
23866 case DW_FORM_sdata:
663c44ac 23867 case DW_FORM_implicit_const:
b6807d98
TT
23868 type = die_type (die, cu);
23869 result = write_constant_as_bytes (obstack, byte_order,
23870 type, DW_SND (attr), len);
23871 break;
23872
23873 case DW_FORM_udata:
23874 type = die_type (die, cu);
23875 result = write_constant_as_bytes (obstack, byte_order,
23876 type, DW_UNSND (attr), len);
23877 break;
23878
23879 default:
b98664d3 23880 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23881 dwarf_form_name (attr->form));
23882 break;
23883 }
23884
23885 return result;
23886}
23887
7942e96e
AA
23888/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23889 valid type for this die is found. */
23890
23891struct type *
9c541725 23892dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23893 struct dwarf2_per_cu_data *per_cu)
23894{
23895 struct dwarf2_cu *cu;
23896 struct die_info *die;
23897
7942e96e 23898 if (per_cu->cu == NULL)
58f0c718 23899 load_cu (per_cu, false);
7942e96e
AA
23900 cu = per_cu->cu;
23901 if (!cu)
23902 return NULL;
23903
9c541725 23904 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23905 if (!die)
23906 return NULL;
23907
23908 return die_type (die, cu);
23909}
23910
8a9b8146
TT
23911/* Return the type of the DIE at DIE_OFFSET in the CU named by
23912 PER_CU. */
23913
23914struct type *
b64f50a1 23915dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23916 struct dwarf2_per_cu_data *per_cu)
23917{
9c541725 23918 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23919 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23920}
23921
ac9ec31b 23922/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23923 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23924 On exit *REF_CU is the CU of the result.
23925 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23926
23927static struct die_info *
ac9ec31b
DE
23928follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23929 struct dwarf2_cu **ref_cu)
348e048f 23930{
348e048f 23931 struct die_info temp_die;
c24bdb02 23932 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23933 struct die_info *die;
23934
ac9ec31b
DE
23935 /* While it might be nice to assert sig_type->type == NULL here,
23936 we can get here for DW_AT_imported_declaration where we need
23937 the DIE not the type. */
348e048f
DE
23938
23939 /* If necessary, add it to the queue and load its DIEs. */
23940
95554aad 23941 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23942 read_signatured_type (sig_type);
348e048f 23943
348e048f 23944 sig_cu = sig_type->per_cu.cu;
69d751e3 23945 gdb_assert (sig_cu != NULL);
9c541725
PA
23946 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23947 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23948 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23949 to_underlying (temp_die.sect_off));
348e048f
DE
23950 if (die)
23951 {
ed2dc618 23952 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23953 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23954
796a7ff8
DE
23955 /* For .gdb_index version 7 keep track of included TUs.
23956 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23957 if (dwarf2_per_objfile->index_table != NULL
23958 && dwarf2_per_objfile->index_table->version <= 7)
23959 {
ae640021 23960 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23961 }
23962
348e048f 23963 *ref_cu = sig_cu;
c24bdb02
KS
23964 if (sig_cu != cu)
23965 sig_cu->ancestor = cu;
23966
348e048f
DE
23967 return die;
23968 }
23969
ac9ec31b
DE
23970 return NULL;
23971}
23972
23973/* Follow signatured type referenced by ATTR in SRC_DIE.
23974 On entry *REF_CU is the CU of SRC_DIE.
23975 On exit *REF_CU is the CU of the result.
23976 The result is the DIE of the type.
23977 If the referenced type cannot be found an error is thrown. */
23978
23979static struct die_info *
ff39bb5e 23980follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23981 struct dwarf2_cu **ref_cu)
23982{
23983 ULONGEST signature = DW_SIGNATURE (attr);
23984 struct signatured_type *sig_type;
23985 struct die_info *die;
23986
23987 gdb_assert (attr->form == DW_FORM_ref_sig8);
23988
a2ce51a0 23989 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23990 /* sig_type will be NULL if the signatured type is missing from
23991 the debug info. */
23992 if (sig_type == NULL)
23993 {
23994 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23995 " from DIE at %s [in module %s]"),
23996 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23997 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23998 }
23999
24000 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24001 if (die == NULL)
24002 {
24003 dump_die_for_error (src_die);
24004 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24005 " from DIE at %s [in module %s]"),
24006 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 24007 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
24008 }
24009
24010 return die;
24011}
24012
24013/* Get the type specified by SIGNATURE referenced in DIE/CU,
24014 reading in and processing the type unit if necessary. */
24015
24016static struct type *
24017get_signatured_type (struct die_info *die, ULONGEST signature,
24018 struct dwarf2_cu *cu)
24019{
518817b3
SM
24020 struct dwarf2_per_objfile *dwarf2_per_objfile
24021 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
24022 struct signatured_type *sig_type;
24023 struct dwarf2_cu *type_cu;
24024 struct die_info *type_die;
24025 struct type *type;
24026
a2ce51a0 24027 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24028 /* sig_type will be NULL if the signatured type is missing from
24029 the debug info. */
24030 if (sig_type == NULL)
24031 {
b98664d3 24032 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24033 " from DIE at %s [in module %s]"),
24034 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24035 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24036 return build_error_marker_type (cu, die);
24037 }
24038
24039 /* If we already know the type we're done. */
24040 if (sig_type->type != NULL)
24041 return sig_type->type;
24042
24043 type_cu = cu;
24044 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24045 if (type_die != NULL)
24046 {
24047 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24048 is created. This is important, for example, because for c++ classes
24049 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24050 type = read_type_die (type_die, type_cu);
24051 if (type == NULL)
24052 {
b98664d3 24053 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24054 " referenced from DIE at %s [in module %s]"),
24055 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24056 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24057 type = build_error_marker_type (cu, die);
24058 }
24059 }
24060 else
24061 {
b98664d3 24062 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24063 " from DIE at %s [in module %s]"),
24064 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24065 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24066 type = build_error_marker_type (cu, die);
24067 }
24068 sig_type->type = type;
24069
24070 return type;
24071}
24072
24073/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24074 reading in and processing the type unit if necessary. */
24075
24076static struct type *
ff39bb5e 24077get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24078 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24079{
24080 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 24081 if (attr_form_is_ref (attr))
ac9ec31b
DE
24082 {
24083 struct dwarf2_cu *type_cu = cu;
24084 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24085
24086 return read_type_die (type_die, type_cu);
24087 }
24088 else if (attr->form == DW_FORM_ref_sig8)
24089 {
24090 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24091 }
24092 else
24093 {
518817b3
SM
24094 struct dwarf2_per_objfile *dwarf2_per_objfile
24095 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24096
b98664d3 24097 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24098 " at %s [in module %s]"),
24099 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 24100 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24101 return build_error_marker_type (cu, die);
24102 }
348e048f
DE
24103}
24104
e5fe5e75 24105/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24106
24107static void
e5fe5e75 24108load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 24109{
52dc124a 24110 struct signatured_type *sig_type;
348e048f 24111
f4dc4d17
DE
24112 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24113 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24114
6721b2ec
DE
24115 /* We have the per_cu, but we need the signatured_type.
24116 Fortunately this is an easy translation. */
24117 gdb_assert (per_cu->is_debug_types);
24118 sig_type = (struct signatured_type *) per_cu;
348e048f 24119
6721b2ec 24120 gdb_assert (per_cu->cu == NULL);
348e048f 24121
52dc124a 24122 read_signatured_type (sig_type);
348e048f 24123
6721b2ec 24124 gdb_assert (per_cu->cu != NULL);
348e048f
DE
24125}
24126
3019eac3
DE
24127/* Read in a signatured type and build its CU and DIEs.
24128 If the type is a stub for the real type in a DWO file,
24129 read in the real type from the DWO file as well. */
dee91e82
DE
24130
24131static void
24132read_signatured_type (struct signatured_type *sig_type)
24133{
24134 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24135
3019eac3 24136 gdb_assert (per_cu->is_debug_types);
dee91e82 24137 gdb_assert (per_cu->cu == NULL);
348e048f 24138
c0ab21c2
TT
24139 cutu_reader reader (per_cu, NULL, 0, 1, false);
24140
24141 if (!reader.dummy_p)
24142 {
24143 struct dwarf2_cu *cu = reader.cu;
24144 const gdb_byte *info_ptr = reader.info_ptr;
24145
24146 gdb_assert (cu->die_hash == NULL);
24147 cu->die_hash =
24148 htab_create_alloc_ex (cu->header.length / 12,
24149 die_hash,
24150 die_eq,
24151 NULL,
24152 &cu->comp_unit_obstack,
24153 hashtab_obstack_allocate,
24154 dummy_obstack_deallocate);
24155
24156 if (reader.has_children)
24157 reader.comp_unit_die->child
24158 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24159 reader.comp_unit_die);
24160 cu->dies = reader.comp_unit_die;
24161 /* comp_unit_die is not stored in die_hash, no need. */
24162
24163 /* We try not to read any attributes in this function, because
24164 not all CUs needed for references have been loaded yet, and
24165 symbol table processing isn't initialized. But we have to
24166 set the CU language, or we won't be able to build types
24167 correctly. Similarly, if we do not read the producer, we can
24168 not apply producer-specific interpretation. */
24169 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24170 }
24171
7ee85ab1 24172 sig_type->per_cu.tu_read = 1;
c906108c
SS
24173}
24174
c906108c
SS
24175/* Decode simple location descriptions.
24176 Given a pointer to a dwarf block that defines a location, compute
24177 the location and return the value.
24178
4cecd739
DJ
24179 NOTE drow/2003-11-18: This function is called in two situations
24180 now: for the address of static or global variables (partial symbols
24181 only) and for offsets into structures which are expected to be
24182 (more or less) constant. The partial symbol case should go away,
24183 and only the constant case should remain. That will let this
24184 function complain more accurately. A few special modes are allowed
24185 without complaint for global variables (for instance, global
24186 register values and thread-local values).
c906108c
SS
24187
24188 A location description containing no operations indicates that the
4cecd739 24189 object is optimized out. The return value is 0 for that case.
6b992462
DJ
24190 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24191 callers will only want a very basic result and this can become a
21ae7a4d
JK
24192 complaint.
24193
24194 Note that stack[0] is unused except as a default error return. */
c906108c
SS
24195
24196static CORE_ADDR
e7c27a73 24197decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 24198{
518817b3 24199 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
24200 size_t i;
24201 size_t size = blk->size;
d521ce57 24202 const gdb_byte *data = blk->data;
21ae7a4d
JK
24203 CORE_ADDR stack[64];
24204 int stacki;
24205 unsigned int bytes_read, unsnd;
24206 gdb_byte op;
c906108c 24207
21ae7a4d
JK
24208 i = 0;
24209 stacki = 0;
24210 stack[stacki] = 0;
24211 stack[++stacki] = 0;
24212
24213 while (i < size)
24214 {
24215 op = data[i++];
24216 switch (op)
24217 {
24218 case DW_OP_lit0:
24219 case DW_OP_lit1:
24220 case DW_OP_lit2:
24221 case DW_OP_lit3:
24222 case DW_OP_lit4:
24223 case DW_OP_lit5:
24224 case DW_OP_lit6:
24225 case DW_OP_lit7:
24226 case DW_OP_lit8:
24227 case DW_OP_lit9:
24228 case DW_OP_lit10:
24229 case DW_OP_lit11:
24230 case DW_OP_lit12:
24231 case DW_OP_lit13:
24232 case DW_OP_lit14:
24233 case DW_OP_lit15:
24234 case DW_OP_lit16:
24235 case DW_OP_lit17:
24236 case DW_OP_lit18:
24237 case DW_OP_lit19:
24238 case DW_OP_lit20:
24239 case DW_OP_lit21:
24240 case DW_OP_lit22:
24241 case DW_OP_lit23:
24242 case DW_OP_lit24:
24243 case DW_OP_lit25:
24244 case DW_OP_lit26:
24245 case DW_OP_lit27:
24246 case DW_OP_lit28:
24247 case DW_OP_lit29:
24248 case DW_OP_lit30:
24249 case DW_OP_lit31:
24250 stack[++stacki] = op - DW_OP_lit0;
24251 break;
f1bea926 24252
21ae7a4d
JK
24253 case DW_OP_reg0:
24254 case DW_OP_reg1:
24255 case DW_OP_reg2:
24256 case DW_OP_reg3:
24257 case DW_OP_reg4:
24258 case DW_OP_reg5:
24259 case DW_OP_reg6:
24260 case DW_OP_reg7:
24261 case DW_OP_reg8:
24262 case DW_OP_reg9:
24263 case DW_OP_reg10:
24264 case DW_OP_reg11:
24265 case DW_OP_reg12:
24266 case DW_OP_reg13:
24267 case DW_OP_reg14:
24268 case DW_OP_reg15:
24269 case DW_OP_reg16:
24270 case DW_OP_reg17:
24271 case DW_OP_reg18:
24272 case DW_OP_reg19:
24273 case DW_OP_reg20:
24274 case DW_OP_reg21:
24275 case DW_OP_reg22:
24276 case DW_OP_reg23:
24277 case DW_OP_reg24:
24278 case DW_OP_reg25:
24279 case DW_OP_reg26:
24280 case DW_OP_reg27:
24281 case DW_OP_reg28:
24282 case DW_OP_reg29:
24283 case DW_OP_reg30:
24284 case DW_OP_reg31:
24285 stack[++stacki] = op - DW_OP_reg0;
24286 if (i < size)
24287 dwarf2_complex_location_expr_complaint ();
24288 break;
c906108c 24289
21ae7a4d
JK
24290 case DW_OP_regx:
24291 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24292 i += bytes_read;
24293 stack[++stacki] = unsnd;
24294 if (i < size)
24295 dwarf2_complex_location_expr_complaint ();
24296 break;
c906108c 24297
21ae7a4d
JK
24298 case DW_OP_addr:
24299 stack[++stacki] = read_address (objfile->obfd, &data[i],
24300 cu, &bytes_read);
24301 i += bytes_read;
24302 break;
d53d4ac5 24303
21ae7a4d
JK
24304 case DW_OP_const1u:
24305 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24306 i += 1;
24307 break;
24308
24309 case DW_OP_const1s:
24310 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24311 i += 1;
24312 break;
24313
24314 case DW_OP_const2u:
24315 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24316 i += 2;
24317 break;
24318
24319 case DW_OP_const2s:
24320 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24321 i += 2;
24322 break;
d53d4ac5 24323
21ae7a4d
JK
24324 case DW_OP_const4u:
24325 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24326 i += 4;
24327 break;
24328
24329 case DW_OP_const4s:
24330 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24331 i += 4;
24332 break;
24333
585861ea
JK
24334 case DW_OP_const8u:
24335 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24336 i += 8;
24337 break;
24338
21ae7a4d
JK
24339 case DW_OP_constu:
24340 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24341 &bytes_read);
24342 i += bytes_read;
24343 break;
24344
24345 case DW_OP_consts:
24346 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24347 i += bytes_read;
24348 break;
24349
24350 case DW_OP_dup:
24351 stack[stacki + 1] = stack[stacki];
24352 stacki++;
24353 break;
24354
24355 case DW_OP_plus:
24356 stack[stacki - 1] += stack[stacki];
24357 stacki--;
24358 break;
24359
24360 case DW_OP_plus_uconst:
24361 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24362 &bytes_read);
24363 i += bytes_read;
24364 break;
24365
24366 case DW_OP_minus:
24367 stack[stacki - 1] -= stack[stacki];
24368 stacki--;
24369 break;
24370
24371 case DW_OP_deref:
24372 /* If we're not the last op, then we definitely can't encode
24373 this using GDB's address_class enum. This is valid for partial
24374 global symbols, although the variable's address will be bogus
24375 in the psymtab. */
24376 if (i < size)
24377 dwarf2_complex_location_expr_complaint ();
24378 break;
24379
24380 case DW_OP_GNU_push_tls_address:
4aa4e28b 24381 case DW_OP_form_tls_address:
21ae7a4d
JK
24382 /* The top of the stack has the offset from the beginning
24383 of the thread control block at which the variable is located. */
24384 /* Nothing should follow this operator, so the top of stack would
24385 be returned. */
24386 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24387 address will be bogus in the psymtab. Make it always at least
24388 non-zero to not look as a variable garbage collected by linker
24389 which have DW_OP_addr 0. */
21ae7a4d
JK
24390 if (i < size)
24391 dwarf2_complex_location_expr_complaint ();
585861ea 24392 stack[stacki]++;
21ae7a4d
JK
24393 break;
24394
24395 case DW_OP_GNU_uninit:
24396 break;
24397
336d760d 24398 case DW_OP_addrx:
3019eac3 24399 case DW_OP_GNU_addr_index:
49f6c839 24400 case DW_OP_GNU_const_index:
3019eac3
DE
24401 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24402 &bytes_read);
24403 i += bytes_read;
24404 break;
24405
21ae7a4d
JK
24406 default:
24407 {
f39c6ffd 24408 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24409
24410 if (name)
b98664d3 24411 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24412 name);
24413 else
b98664d3 24414 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24415 op);
24416 }
24417
24418 return (stack[stacki]);
d53d4ac5 24419 }
3c6e0cb3 24420
21ae7a4d
JK
24421 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24422 outside of the allocated space. Also enforce minimum>0. */
24423 if (stacki >= ARRAY_SIZE (stack) - 1)
24424 {
b98664d3 24425 complaint (_("location description stack overflow"));
21ae7a4d
JK
24426 return 0;
24427 }
24428
24429 if (stacki <= 0)
24430 {
b98664d3 24431 complaint (_("location description stack underflow"));
21ae7a4d
JK
24432 return 0;
24433 }
24434 }
24435 return (stack[stacki]);
c906108c
SS
24436}
24437
24438/* memory allocation interface */
24439
c906108c 24440static struct dwarf_block *
7b5a2f43 24441dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24442{
8d749320 24443 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24444}
24445
c906108c 24446static struct die_info *
b60c80d6 24447dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24448{
24449 struct die_info *die;
b60c80d6
DJ
24450 size_t size = sizeof (struct die_info);
24451
24452 if (num_attrs > 1)
24453 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24454
b60c80d6 24455 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24456 memset (die, 0, sizeof (struct die_info));
24457 return (die);
24458}
2e276125
JB
24459
24460\f
24461/* Macro support. */
24462
233d95b5
JK
24463/* Return file name relative to the compilation directory of file number I in
24464 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24465 responsible for freeing it. */
233d95b5 24466
2e276125 24467static char *
233d95b5 24468file_file_name (int file, struct line_header *lh)
2e276125 24469{
6a83a1e6
EZ
24470 /* Is the file number a valid index into the line header's file name
24471 table? Remember that file numbers start with one, not zero. */
7ba99d21 24472 if (lh->is_valid_file_index (file))
6a83a1e6 24473 {
7ba99d21 24474 const file_entry *fe = lh->file_name_at (file);
6e70227d 24475
7ba99d21 24476 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24477 {
7ba99d21 24478 const char *dir = fe->include_dir (lh);
8c43009f 24479 if (dir != NULL)
7ba99d21 24480 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24481 }
7ba99d21 24482 return xstrdup (fe->name);
6a83a1e6 24483 }
2e276125
JB
24484 else
24485 {
6a83a1e6
EZ
24486 /* The compiler produced a bogus file number. We can at least
24487 record the macro definitions made in the file, even if we
24488 won't be able to find the file by name. */
24489 char fake_name[80];
9a619af0 24490
8c042590
PM
24491 xsnprintf (fake_name, sizeof (fake_name),
24492 "<bad macro file number %d>", file);
2e276125 24493
b98664d3 24494 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24495 file);
2e276125 24496
6a83a1e6 24497 return xstrdup (fake_name);
2e276125
JB
24498 }
24499}
24500
233d95b5
JK
24501/* Return the full name of file number I in *LH's file name table.
24502 Use COMP_DIR as the name of the current directory of the
24503 compilation. The result is allocated using xmalloc; the caller is
24504 responsible for freeing it. */
24505static char *
24506file_full_name (int file, struct line_header *lh, const char *comp_dir)
24507{
24508 /* Is the file number a valid index into the line header's file name
24509 table? Remember that file numbers start with one, not zero. */
7ba99d21 24510 if (lh->is_valid_file_index (file))
233d95b5
JK
24511 {
24512 char *relative = file_file_name (file, lh);
24513
24514 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24515 return relative;
b36cec19
PA
24516 return reconcat (relative, comp_dir, SLASH_STRING,
24517 relative, (char *) NULL);
233d95b5
JK
24518 }
24519 else
24520 return file_file_name (file, lh);
24521}
24522
2e276125
JB
24523
24524static struct macro_source_file *
804d2729
TT
24525macro_start_file (struct dwarf2_cu *cu,
24526 int file, int line,
2e276125 24527 struct macro_source_file *current_file,
43f3e411 24528 struct line_header *lh)
2e276125 24529{
233d95b5
JK
24530 /* File name relative to the compilation directory of this source file. */
24531 char *file_name = file_file_name (file, lh);
2e276125 24532
2e276125 24533 if (! current_file)
abc9d0dc 24534 {
fc474241
DE
24535 /* Note: We don't create a macro table for this compilation unit
24536 at all until we actually get a filename. */
c24bdb02 24537 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24538
abc9d0dc
TT
24539 /* If we have no current file, then this must be the start_file
24540 directive for the compilation unit's main source file. */
fc474241
DE
24541 current_file = macro_set_main (macro_table, file_name);
24542 macro_define_special (macro_table);
abc9d0dc 24543 }
2e276125 24544 else
233d95b5 24545 current_file = macro_include (current_file, line, file_name);
2e276125 24546
233d95b5 24547 xfree (file_name);
6e70227d 24548
2e276125
JB
24549 return current_file;
24550}
24551
2e276125
JB
24552static const char *
24553consume_improper_spaces (const char *p, const char *body)
24554{
24555 if (*p == ' ')
24556 {
b98664d3 24557 complaint (_("macro definition contains spaces "
3e43a32a 24558 "in formal argument list:\n`%s'"),
4d3c2250 24559 body);
2e276125
JB
24560
24561 while (*p == ' ')
24562 p++;
24563 }
24564
24565 return p;
24566}
24567
24568
24569static void
24570parse_macro_definition (struct macro_source_file *file, int line,
24571 const char *body)
24572{
24573 const char *p;
24574
24575 /* The body string takes one of two forms. For object-like macro
24576 definitions, it should be:
24577
24578 <macro name> " " <definition>
24579
24580 For function-like macro definitions, it should be:
24581
24582 <macro name> "() " <definition>
24583 or
24584 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24585
24586 Spaces may appear only where explicitly indicated, and in the
24587 <definition>.
24588
24589 The Dwarf 2 spec says that an object-like macro's name is always
24590 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24591 the space when the macro's definition is the empty string.
2e276125
JB
24592
24593 The Dwarf 2 spec says that there should be no spaces between the
24594 formal arguments in a function-like macro's formal argument list,
24595 but versions of GCC around March 2002 include spaces after the
24596 commas. */
24597
24598
24599 /* Find the extent of the macro name. The macro name is terminated
24600 by either a space or null character (for an object-like macro) or
24601 an opening paren (for a function-like macro). */
24602 for (p = body; *p; p++)
24603 if (*p == ' ' || *p == '(')
24604 break;
24605
24606 if (*p == ' ' || *p == '\0')
24607 {
24608 /* It's an object-like macro. */
24609 int name_len = p - body;
456e800a 24610 std::string name (body, name_len);
2e276125
JB
24611 const char *replacement;
24612
24613 if (*p == ' ')
24614 replacement = body + name_len + 1;
24615 else
24616 {
4d3c2250 24617 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24618 replacement = body + name_len;
24619 }
6e70227d 24620
456e800a 24621 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
24622 }
24623 else if (*p == '(')
24624 {
24625 /* It's a function-like macro. */
456e800a 24626 std::string name (body, p - body);
2e276125
JB
24627 int argc = 0;
24628 int argv_size = 1;
8d749320 24629 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24630
24631 p++;
24632
24633 p = consume_improper_spaces (p, body);
24634
24635 /* Parse the formal argument list. */
24636 while (*p && *p != ')')
24637 {
24638 /* Find the extent of the current argument name. */
24639 const char *arg_start = p;
24640
24641 while (*p && *p != ',' && *p != ')' && *p != ' ')
24642 p++;
24643
24644 if (! *p || p == arg_start)
4d3c2250 24645 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24646 else
24647 {
24648 /* Make sure argv has room for the new argument. */
24649 if (argc >= argv_size)
24650 {
24651 argv_size *= 2;
224c3ddb 24652 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24653 }
24654
3f8a7804 24655 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24656 }
24657
24658 p = consume_improper_spaces (p, body);
24659
24660 /* Consume the comma, if present. */
24661 if (*p == ',')
24662 {
24663 p++;
24664
24665 p = consume_improper_spaces (p, body);
24666 }
24667 }
24668
24669 if (*p == ')')
24670 {
24671 p++;
24672
24673 if (*p == ' ')
24674 /* Perfectly formed definition, no complaints. */
456e800a 24675 macro_define_function (file, line, name.c_str (),
6e70227d 24676 argc, (const char **) argv,
2e276125
JB
24677 p + 1);
24678 else if (*p == '\0')
24679 {
24680 /* Complain, but do define it. */
4d3c2250 24681 dwarf2_macro_malformed_definition_complaint (body);
456e800a 24682 macro_define_function (file, line, name.c_str (),
6e70227d 24683 argc, (const char **) argv,
2e276125
JB
24684 p);
24685 }
24686 else
24687 /* Just complain. */
4d3c2250 24688 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24689 }
24690 else
24691 /* Just complain. */
4d3c2250 24692 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24693
2e276125
JB
24694 {
24695 int i;
24696
24697 for (i = 0; i < argc; i++)
24698 xfree (argv[i]);
24699 }
24700 xfree (argv);
24701 }
24702 else
4d3c2250 24703 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24704}
24705
cf2c3c16
TT
24706/* Skip some bytes from BYTES according to the form given in FORM.
24707 Returns the new pointer. */
2e276125 24708
d521ce57
TT
24709static const gdb_byte *
24710skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24711 enum dwarf_form form,
24712 unsigned int offset_size,
24713 struct dwarf2_section_info *section)
2e276125 24714{
cf2c3c16 24715 unsigned int bytes_read;
2e276125 24716
cf2c3c16 24717 switch (form)
2e276125 24718 {
cf2c3c16
TT
24719 case DW_FORM_data1:
24720 case DW_FORM_flag:
24721 ++bytes;
24722 break;
24723
24724 case DW_FORM_data2:
24725 bytes += 2;
24726 break;
24727
24728 case DW_FORM_data4:
24729 bytes += 4;
24730 break;
24731
24732 case DW_FORM_data8:
24733 bytes += 8;
24734 break;
24735
0224619f
JK
24736 case DW_FORM_data16:
24737 bytes += 16;
24738 break;
24739
cf2c3c16
TT
24740 case DW_FORM_string:
24741 read_direct_string (abfd, bytes, &bytes_read);
24742 bytes += bytes_read;
24743 break;
24744
24745 case DW_FORM_sec_offset:
24746 case DW_FORM_strp:
36586728 24747 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24748 bytes += offset_size;
24749 break;
24750
24751 case DW_FORM_block:
24752 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24753 bytes += bytes_read;
24754 break;
24755
24756 case DW_FORM_block1:
24757 bytes += 1 + read_1_byte (abfd, bytes);
24758 break;
24759 case DW_FORM_block2:
24760 bytes += 2 + read_2_bytes (abfd, bytes);
24761 break;
24762 case DW_FORM_block4:
24763 bytes += 4 + read_4_bytes (abfd, bytes);
24764 break;
24765
336d760d 24766 case DW_FORM_addrx:
cf2c3c16 24767 case DW_FORM_sdata:
cf532bd1 24768 case DW_FORM_strx:
cf2c3c16 24769 case DW_FORM_udata:
3019eac3
DE
24770 case DW_FORM_GNU_addr_index:
24771 case DW_FORM_GNU_str_index:
d521ce57 24772 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24773 if (bytes == NULL)
24774 {
24775 dwarf2_section_buffer_overflow_complaint (section);
24776 return NULL;
24777 }
cf2c3c16
TT
24778 break;
24779
663c44ac
JK
24780 case DW_FORM_implicit_const:
24781 break;
24782
cf2c3c16
TT
24783 default:
24784 {
b98664d3 24785 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24786 form, get_section_name (section));
cf2c3c16
TT
24787 return NULL;
24788 }
2e276125
JB
24789 }
24790
cf2c3c16
TT
24791 return bytes;
24792}
757a13d0 24793
cf2c3c16
TT
24794/* A helper for dwarf_decode_macros that handles skipping an unknown
24795 opcode. Returns an updated pointer to the macro data buffer; or,
24796 on error, issues a complaint and returns NULL. */
757a13d0 24797
d521ce57 24798static const gdb_byte *
cf2c3c16 24799skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24800 const gdb_byte **opcode_definitions,
24801 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24802 bfd *abfd,
24803 unsigned int offset_size,
24804 struct dwarf2_section_info *section)
24805{
24806 unsigned int bytes_read, i;
24807 unsigned long arg;
d521ce57 24808 const gdb_byte *defn;
2e276125 24809
cf2c3c16 24810 if (opcode_definitions[opcode] == NULL)
2e276125 24811 {
b98664d3 24812 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24813 opcode);
24814 return NULL;
24815 }
2e276125 24816
cf2c3c16
TT
24817 defn = opcode_definitions[opcode];
24818 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24819 defn += bytes_read;
2e276125 24820
cf2c3c16
TT
24821 for (i = 0; i < arg; ++i)
24822 {
aead7601
SM
24823 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24824 (enum dwarf_form) defn[i], offset_size,
f664829e 24825 section);
cf2c3c16
TT
24826 if (mac_ptr == NULL)
24827 {
24828 /* skip_form_bytes already issued the complaint. */
24829 return NULL;
24830 }
24831 }
757a13d0 24832
cf2c3c16
TT
24833 return mac_ptr;
24834}
757a13d0 24835
cf2c3c16
TT
24836/* A helper function which parses the header of a macro section.
24837 If the macro section is the extended (for now called "GNU") type,
24838 then this updates *OFFSET_SIZE. Returns a pointer to just after
24839 the header, or issues a complaint and returns NULL on error. */
757a13d0 24840
d521ce57
TT
24841static const gdb_byte *
24842dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24843 bfd *abfd,
d521ce57 24844 const gdb_byte *mac_ptr,
cf2c3c16
TT
24845 unsigned int *offset_size,
24846 int section_is_gnu)
24847{
24848 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24849
cf2c3c16
TT
24850 if (section_is_gnu)
24851 {
24852 unsigned int version, flags;
757a13d0 24853
cf2c3c16 24854 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24855 if (version != 4 && version != 5)
cf2c3c16 24856 {
b98664d3 24857 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24858 version);
24859 return NULL;
24860 }
24861 mac_ptr += 2;
757a13d0 24862
cf2c3c16
TT
24863 flags = read_1_byte (abfd, mac_ptr);
24864 ++mac_ptr;
24865 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24866
cf2c3c16
TT
24867 if ((flags & 2) != 0)
24868 /* We don't need the line table offset. */
24869 mac_ptr += *offset_size;
757a13d0 24870
cf2c3c16
TT
24871 /* Vendor opcode descriptions. */
24872 if ((flags & 4) != 0)
24873 {
24874 unsigned int i, count;
757a13d0 24875
cf2c3c16
TT
24876 count = read_1_byte (abfd, mac_ptr);
24877 ++mac_ptr;
24878 for (i = 0; i < count; ++i)
24879 {
24880 unsigned int opcode, bytes_read;
24881 unsigned long arg;
24882
24883 opcode = read_1_byte (abfd, mac_ptr);
24884 ++mac_ptr;
24885 opcode_definitions[opcode] = mac_ptr;
24886 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24887 mac_ptr += bytes_read;
24888 mac_ptr += arg;
24889 }
757a13d0 24890 }
cf2c3c16 24891 }
757a13d0 24892
cf2c3c16
TT
24893 return mac_ptr;
24894}
757a13d0 24895
cf2c3c16 24896/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24897 including DW_MACRO_import. */
cf2c3c16
TT
24898
24899static void
804d2729 24900dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24901 bfd *abfd,
d521ce57 24902 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24903 struct macro_source_file *current_file,
43f3e411 24904 struct line_header *lh,
cf2c3c16 24905 struct dwarf2_section_info *section,
36586728 24906 int section_is_gnu, int section_is_dwz,
cf2c3c16 24907 unsigned int offset_size,
8fc3fc34 24908 htab_t include_hash)
cf2c3c16 24909{
804d2729
TT
24910 struct dwarf2_per_objfile *dwarf2_per_objfile
24911 = cu->per_cu->dwarf2_per_objfile;
4d663531 24912 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24913 enum dwarf_macro_record_type macinfo_type;
24914 int at_commandline;
d521ce57 24915 const gdb_byte *opcode_definitions[256];
757a13d0 24916
cf2c3c16
TT
24917 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24918 &offset_size, section_is_gnu);
24919 if (mac_ptr == NULL)
24920 {
24921 /* We already issued a complaint. */
24922 return;
24923 }
757a13d0
JK
24924
24925 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24926 GDB is still reading the definitions from command line. First
24927 DW_MACINFO_start_file will need to be ignored as it was already executed
24928 to create CURRENT_FILE for the main source holding also the command line
24929 definitions. On first met DW_MACINFO_start_file this flag is reset to
24930 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24931
24932 at_commandline = 1;
24933
24934 do
24935 {
24936 /* Do we at least have room for a macinfo type byte? */
24937 if (mac_ptr >= mac_end)
24938 {
f664829e 24939 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24940 break;
24941 }
24942
aead7601 24943 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24944 mac_ptr++;
24945
cf2c3c16
TT
24946 /* Note that we rely on the fact that the corresponding GNU and
24947 DWARF constants are the same. */
132448f8
SM
24948 DIAGNOSTIC_PUSH
24949 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24950 switch (macinfo_type)
24951 {
24952 /* A zero macinfo type indicates the end of the macro
24953 information. */
24954 case 0:
24955 break;
2e276125 24956
0af92d60
JK
24957 case DW_MACRO_define:
24958 case DW_MACRO_undef:
24959 case DW_MACRO_define_strp:
24960 case DW_MACRO_undef_strp:
24961 case DW_MACRO_define_sup:
24962 case DW_MACRO_undef_sup:
2e276125 24963 {
891d2f0b 24964 unsigned int bytes_read;
2e276125 24965 int line;
d521ce57 24966 const char *body;
cf2c3c16 24967 int is_define;
2e276125 24968
cf2c3c16
TT
24969 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24970 mac_ptr += bytes_read;
24971
0af92d60
JK
24972 if (macinfo_type == DW_MACRO_define
24973 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24974 {
24975 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24976 mac_ptr += bytes_read;
24977 }
24978 else
24979 {
24980 LONGEST str_offset;
24981
24982 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24983 mac_ptr += offset_size;
2e276125 24984
0af92d60
JK
24985 if (macinfo_type == DW_MACRO_define_sup
24986 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24987 || section_is_dwz)
36586728 24988 {
ed2dc618
SM
24989 struct dwz_file *dwz
24990 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24991
ed2dc618
SM
24992 body = read_indirect_string_from_dwz (objfile,
24993 dwz, str_offset);
36586728
TT
24994 }
24995 else
ed2dc618
SM
24996 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24997 abfd, str_offset);
cf2c3c16
TT
24998 }
24999
0af92d60
JK
25000 is_define = (macinfo_type == DW_MACRO_define
25001 || macinfo_type == DW_MACRO_define_strp
25002 || macinfo_type == DW_MACRO_define_sup);
2e276125 25003 if (! current_file)
757a13d0
JK
25004 {
25005 /* DWARF violation as no main source is present. */
b98664d3 25006 complaint (_("debug info with no main source gives macro %s "
757a13d0 25007 "on line %d: %s"),
cf2c3c16
TT
25008 is_define ? _("definition") : _("undefinition"),
25009 line, body);
757a13d0
JK
25010 break;
25011 }
3e43a32a
MS
25012 if ((line == 0 && !at_commandline)
25013 || (line != 0 && at_commandline))
b98664d3 25014 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 25015 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 25016 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
25017 line == 0 ? _("zero") : _("non-zero"), line, body);
25018
955b06fa 25019 if (body == NULL)
7bede828 25020 {
955b06fa
SDJ
25021 /* Fedora's rpm-build's "debugedit" binary
25022 corrupted .debug_macro sections.
25023
25024 For more info, see
25025 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
25026 complaint (_("debug info gives %s invalid macro %s "
25027 "without body (corrupted?) at line %d "
25028 "on file %s"),
25029 at_commandline ? _("command-line") : _("in-file"),
25030 is_define ? _("definition") : _("undefinition"),
25031 line, current_file->filename);
7bede828 25032 }
955b06fa
SDJ
25033 else if (is_define)
25034 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
25035 else
25036 {
0af92d60
JK
25037 gdb_assert (macinfo_type == DW_MACRO_undef
25038 || macinfo_type == DW_MACRO_undef_strp
25039 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
25040 macro_undef (current_file, line, body);
25041 }
2e276125
JB
25042 }
25043 break;
25044
0af92d60 25045 case DW_MACRO_start_file:
2e276125 25046 {
891d2f0b 25047 unsigned int bytes_read;
2e276125
JB
25048 int line, file;
25049
25050 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25051 mac_ptr += bytes_read;
25052 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25053 mac_ptr += bytes_read;
25054
3e43a32a
MS
25055 if ((line == 0 && !at_commandline)
25056 || (line != 0 && at_commandline))
b98664d3 25057 complaint (_("debug info gives source %d included "
757a13d0
JK
25058 "from %s at %s line %d"),
25059 file, at_commandline ? _("command-line") : _("file"),
25060 line == 0 ? _("zero") : _("non-zero"), line);
25061
25062 if (at_commandline)
25063 {
0af92d60 25064 /* This DW_MACRO_start_file was executed in the
cf2c3c16 25065 pass one. */
757a13d0
JK
25066 at_commandline = 0;
25067 }
25068 else
804d2729
TT
25069 current_file = macro_start_file (cu, file, line, current_file,
25070 lh);
2e276125
JB
25071 }
25072 break;
25073
0af92d60 25074 case DW_MACRO_end_file:
2e276125 25075 if (! current_file)
b98664d3 25076 complaint (_("macro debug info has an unmatched "
3e43a32a 25077 "`close_file' directive"));
2e276125
JB
25078 else
25079 {
25080 current_file = current_file->included_by;
25081 if (! current_file)
25082 {
cf2c3c16 25083 enum dwarf_macro_record_type next_type;
2e276125
JB
25084
25085 /* GCC circa March 2002 doesn't produce the zero
25086 type byte marking the end of the compilation
25087 unit. Complain if it's not there, but exit no
25088 matter what. */
25089
25090 /* Do we at least have room for a macinfo type byte? */
25091 if (mac_ptr >= mac_end)
25092 {
f664829e 25093 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
25094 return;
25095 }
25096
25097 /* We don't increment mac_ptr here, so this is just
25098 a look-ahead. */
aead7601
SM
25099 next_type
25100 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25101 mac_ptr);
2e276125 25102 if (next_type != 0)
b98664d3 25103 complaint (_("no terminating 0-type entry for "
3e43a32a 25104 "macros in `.debug_macinfo' section"));
2e276125
JB
25105
25106 return;
25107 }
25108 }
25109 break;
25110
0af92d60
JK
25111 case DW_MACRO_import:
25112 case DW_MACRO_import_sup:
cf2c3c16
TT
25113 {
25114 LONGEST offset;
8fc3fc34 25115 void **slot;
a036ba48
TT
25116 bfd *include_bfd = abfd;
25117 struct dwarf2_section_info *include_section = section;
d521ce57 25118 const gdb_byte *include_mac_end = mac_end;
a036ba48 25119 int is_dwz = section_is_dwz;
d521ce57 25120 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
25121
25122 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25123 mac_ptr += offset_size;
25124
0af92d60 25125 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 25126 {
ed2dc618 25127 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 25128
4d663531 25129 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 25130
a036ba48 25131 include_section = &dwz->macro;
a32a8923 25132 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
25133 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25134 is_dwz = 1;
25135 }
25136
25137 new_mac_ptr = include_section->buffer + offset;
25138 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25139
8fc3fc34
TT
25140 if (*slot != NULL)
25141 {
25142 /* This has actually happened; see
25143 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 25144 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
25145 ".debug_macro section"));
25146 }
25147 else
25148 {
d521ce57 25149 *slot = (void *) new_mac_ptr;
36586728 25150
804d2729 25151 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 25152 include_mac_end, current_file, lh,
36586728 25153 section, section_is_gnu, is_dwz,
4d663531 25154 offset_size, include_hash);
8fc3fc34 25155
d521ce57 25156 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 25157 }
cf2c3c16
TT
25158 }
25159 break;
25160
2e276125 25161 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
25162 if (!section_is_gnu)
25163 {
25164 unsigned int bytes_read;
2e276125 25165
ac298888
TT
25166 /* This reads the constant, but since we don't recognize
25167 any vendor extensions, we ignore it. */
25168 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
25169 mac_ptr += bytes_read;
25170 read_direct_string (abfd, mac_ptr, &bytes_read);
25171 mac_ptr += bytes_read;
2e276125 25172
cf2c3c16
TT
25173 /* We don't recognize any vendor extensions. */
25174 break;
25175 }
25176 /* FALLTHROUGH */
25177
25178 default:
25179 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25180 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25181 section);
25182 if (mac_ptr == NULL)
25183 return;
25184 break;
2e276125 25185 }
132448f8 25186 DIAGNOSTIC_POP
757a13d0 25187 } while (macinfo_type != 0);
2e276125 25188}
8e19ed76 25189
cf2c3c16 25190static void
09262596 25191dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 25192 int section_is_gnu)
cf2c3c16 25193{
518817b3
SM
25194 struct dwarf2_per_objfile *dwarf2_per_objfile
25195 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25196 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
25197 struct line_header *lh = cu->line_header;
25198 bfd *abfd;
d521ce57 25199 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
25200 struct macro_source_file *current_file = 0;
25201 enum dwarf_macro_record_type macinfo_type;
25202 unsigned int offset_size = cu->header.offset_size;
d521ce57 25203 const gdb_byte *opcode_definitions[256];
8fc3fc34 25204 void **slot;
09262596
DE
25205 struct dwarf2_section_info *section;
25206 const char *section_name;
25207
25208 if (cu->dwo_unit != NULL)
25209 {
25210 if (section_is_gnu)
25211 {
25212 section = &cu->dwo_unit->dwo_file->sections.macro;
25213 section_name = ".debug_macro.dwo";
25214 }
25215 else
25216 {
25217 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25218 section_name = ".debug_macinfo.dwo";
25219 }
25220 }
25221 else
25222 {
25223 if (section_is_gnu)
25224 {
25225 section = &dwarf2_per_objfile->macro;
25226 section_name = ".debug_macro";
25227 }
25228 else
25229 {
25230 section = &dwarf2_per_objfile->macinfo;
25231 section_name = ".debug_macinfo";
25232 }
25233 }
cf2c3c16 25234
bb5ed363 25235 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25236 if (section->buffer == NULL)
25237 {
b98664d3 25238 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
25239 return;
25240 }
a32a8923 25241 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25242
25243 /* First pass: Find the name of the base filename.
25244 This filename is needed in order to process all macros whose definition
25245 (or undefinition) comes from the command line. These macros are defined
25246 before the first DW_MACINFO_start_file entry, and yet still need to be
25247 associated to the base file.
25248
25249 To determine the base file name, we scan the macro definitions until we
25250 reach the first DW_MACINFO_start_file entry. We then initialize
25251 CURRENT_FILE accordingly so that any macro definition found before the
25252 first DW_MACINFO_start_file can still be associated to the base file. */
25253
25254 mac_ptr = section->buffer + offset;
25255 mac_end = section->buffer + section->size;
25256
25257 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25258 &offset_size, section_is_gnu);
25259 if (mac_ptr == NULL)
25260 {
25261 /* We already issued a complaint. */
25262 return;
25263 }
25264
25265 do
25266 {
25267 /* Do we at least have room for a macinfo type byte? */
25268 if (mac_ptr >= mac_end)
25269 {
25270 /* Complaint is printed during the second pass as GDB will probably
25271 stop the first pass earlier upon finding
25272 DW_MACINFO_start_file. */
25273 break;
25274 }
25275
aead7601 25276 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25277 mac_ptr++;
25278
25279 /* Note that we rely on the fact that the corresponding GNU and
25280 DWARF constants are the same. */
132448f8
SM
25281 DIAGNOSTIC_PUSH
25282 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25283 switch (macinfo_type)
25284 {
25285 /* A zero macinfo type indicates the end of the macro
25286 information. */
25287 case 0:
25288 break;
25289
0af92d60
JK
25290 case DW_MACRO_define:
25291 case DW_MACRO_undef:
cf2c3c16
TT
25292 /* Only skip the data by MAC_PTR. */
25293 {
25294 unsigned int bytes_read;
25295
25296 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25297 mac_ptr += bytes_read;
25298 read_direct_string (abfd, mac_ptr, &bytes_read);
25299 mac_ptr += bytes_read;
25300 }
25301 break;
25302
0af92d60 25303 case DW_MACRO_start_file:
cf2c3c16
TT
25304 {
25305 unsigned int bytes_read;
25306 int line, file;
25307
25308 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25309 mac_ptr += bytes_read;
25310 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25311 mac_ptr += bytes_read;
25312
804d2729 25313 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25314 }
25315 break;
25316
0af92d60 25317 case DW_MACRO_end_file:
cf2c3c16
TT
25318 /* No data to skip by MAC_PTR. */
25319 break;
25320
0af92d60
JK
25321 case DW_MACRO_define_strp:
25322 case DW_MACRO_undef_strp:
25323 case DW_MACRO_define_sup:
25324 case DW_MACRO_undef_sup:
cf2c3c16
TT
25325 {
25326 unsigned int bytes_read;
25327
25328 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25329 mac_ptr += bytes_read;
25330 mac_ptr += offset_size;
25331 }
25332 break;
25333
0af92d60
JK
25334 case DW_MACRO_import:
25335 case DW_MACRO_import_sup:
cf2c3c16 25336 /* Note that, according to the spec, a transparent include
0af92d60 25337 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25338 skip this opcode. */
25339 mac_ptr += offset_size;
25340 break;
25341
25342 case DW_MACINFO_vendor_ext:
25343 /* Only skip the data by MAC_PTR. */
25344 if (!section_is_gnu)
25345 {
25346 unsigned int bytes_read;
25347
25348 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25349 mac_ptr += bytes_read;
25350 read_direct_string (abfd, mac_ptr, &bytes_read);
25351 mac_ptr += bytes_read;
25352 }
25353 /* FALLTHROUGH */
25354
25355 default:
25356 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25357 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25358 section);
25359 if (mac_ptr == NULL)
25360 return;
25361 break;
25362 }
132448f8 25363 DIAGNOSTIC_POP
cf2c3c16
TT
25364 } while (macinfo_type != 0 && current_file == NULL);
25365
25366 /* Second pass: Process all entries.
25367
25368 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25369 command-line macro definitions/undefinitions. This flag is unset when we
25370 reach the first DW_MACINFO_start_file entry. */
25371
fc4007c9
TT
25372 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25373 htab_eq_pointer,
25374 NULL, xcalloc, xfree));
8fc3fc34 25375 mac_ptr = section->buffer + offset;
fc4007c9 25376 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25377 *slot = (void *) mac_ptr;
804d2729 25378 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25379 current_file, lh, section,
fc4007c9
TT
25380 section_is_gnu, 0, offset_size,
25381 include_hash.get ());
cf2c3c16
TT
25382}
25383
8e19ed76 25384/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25385 if so return true else false. */
380bca97 25386
8e19ed76 25387static int
6e5a29e1 25388attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25389{
25390 return (attr == NULL ? 0 :
25391 attr->form == DW_FORM_block1
25392 || attr->form == DW_FORM_block2
25393 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25394 || attr->form == DW_FORM_block
25395 || attr->form == DW_FORM_exprloc);
8e19ed76 25396}
4c2df51b 25397
c6a0999f
JB
25398/* Return non-zero if ATTR's value is a section offset --- classes
25399 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25400 You may use DW_UNSND (attr) to retrieve such offsets.
25401
25402 Section 7.5.4, "Attribute Encodings", explains that no attribute
25403 may have a value that belongs to more than one of these classes; it
25404 would be ambiguous if we did, because we use the same forms for all
25405 of them. */
380bca97 25406
3690dd37 25407static int
6e5a29e1 25408attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25409{
25410 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25411 || attr->form == DW_FORM_data8
25412 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25413}
25414
3690dd37
JB
25415/* Return non-zero if ATTR's value falls in the 'constant' class, or
25416 zero otherwise. When this function returns true, you can apply
25417 dwarf2_get_attr_constant_value to it.
25418
25419 However, note that for some attributes you must check
25420 attr_form_is_section_offset before using this test. DW_FORM_data4
25421 and DW_FORM_data8 are members of both the constant class, and of
25422 the classes that contain offsets into other debug sections
25423 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25424 that, if an attribute's can be either a constant or one of the
25425 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25426 taken as section offsets, not constants.
25427
25428 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25429 cannot handle that. */
380bca97 25430
3690dd37 25431static int
6e5a29e1 25432attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25433{
25434 switch (attr->form)
25435 {
25436 case DW_FORM_sdata:
25437 case DW_FORM_udata:
25438 case DW_FORM_data1:
25439 case DW_FORM_data2:
25440 case DW_FORM_data4:
25441 case DW_FORM_data8:
663c44ac 25442 case DW_FORM_implicit_const:
3690dd37
JB
25443 return 1;
25444 default:
25445 return 0;
25446 }
25447}
25448
7771576e
SA
25449
25450/* DW_ADDR is always stored already as sect_offset; despite for the forms
25451 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25452
25453static int
6e5a29e1 25454attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25455{
25456 switch (attr->form)
25457 {
25458 case DW_FORM_ref_addr:
25459 case DW_FORM_ref1:
25460 case DW_FORM_ref2:
25461 case DW_FORM_ref4:
25462 case DW_FORM_ref8:
25463 case DW_FORM_ref_udata:
25464 case DW_FORM_GNU_ref_alt:
25465 return 1;
25466 default:
25467 return 0;
25468 }
25469}
25470
3019eac3
DE
25471/* Return the .debug_loc section to use for CU.
25472 For DWO files use .debug_loc.dwo. */
25473
25474static struct dwarf2_section_info *
25475cu_debug_loc_section (struct dwarf2_cu *cu)
25476{
518817b3
SM
25477 struct dwarf2_per_objfile *dwarf2_per_objfile
25478 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25479
3019eac3 25480 if (cu->dwo_unit)
43988095
JK
25481 {
25482 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25483
43988095
JK
25484 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25485 }
25486 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25487 : &dwarf2_per_objfile->loc);
3019eac3
DE
25488}
25489
8cf6f0b1
TT
25490/* A helper function that fills in a dwarf2_loclist_baton. */
25491
25492static void
25493fill_in_loclist_baton (struct dwarf2_cu *cu,
25494 struct dwarf2_loclist_baton *baton,
ff39bb5e 25495 const struct attribute *attr)
8cf6f0b1 25496{
518817b3
SM
25497 struct dwarf2_per_objfile *dwarf2_per_objfile
25498 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25499 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25500
25501 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25502
25503 baton->per_cu = cu->per_cu;
25504 gdb_assert (baton->per_cu);
25505 /* We don't know how long the location list is, but make sure we
25506 don't run off the edge of the section. */
3019eac3
DE
25507 baton->size = section->size - DW_UNSND (attr);
25508 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25509 baton->base_address = cu->base_address;
f664829e 25510 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25511}
25512
4c2df51b 25513static void
ff39bb5e 25514dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25515 struct dwarf2_cu *cu, int is_block)
4c2df51b 25516{
518817b3
SM
25517 struct dwarf2_per_objfile *dwarf2_per_objfile
25518 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25519 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25520 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25521
3690dd37 25522 if (attr_form_is_section_offset (attr)
3019eac3 25523 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25524 the section. If so, fall through to the complaint in the
25525 other branch. */
3019eac3 25526 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25527 {
0d53c4c4 25528 struct dwarf2_loclist_baton *baton;
4c2df51b 25529
8d749320 25530 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25531
8cf6f0b1 25532 fill_in_loclist_baton (cu, baton, attr);
be391dca 25533
d00adf39 25534 if (cu->base_known == 0)
b98664d3 25535 complaint (_("Location list used without "
3e43a32a 25536 "specifying the CU base address."));
4c2df51b 25537
f1e6e072
TT
25538 SYMBOL_ACLASS_INDEX (sym) = (is_block
25539 ? dwarf2_loclist_block_index
25540 : dwarf2_loclist_index);
0d53c4c4
DJ
25541 SYMBOL_LOCATION_BATON (sym) = baton;
25542 }
25543 else
25544 {
25545 struct dwarf2_locexpr_baton *baton;
25546
8d749320 25547 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25548 baton->per_cu = cu->per_cu;
25549 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25550
25551 if (attr_form_is_block (attr))
25552 {
25553 /* Note that we're just copying the block's data pointer
25554 here, not the actual data. We're still pointing into the
6502dd73
DJ
25555 info_buffer for SYM's objfile; right now we never release
25556 that buffer, but when we do clean up properly this may
25557 need to change. */
0d53c4c4
DJ
25558 baton->size = DW_BLOCK (attr)->size;
25559 baton->data = DW_BLOCK (attr)->data;
25560 }
25561 else
25562 {
25563 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 25564 sym->natural_name ());
0d53c4c4 25565 baton->size = 0;
0d53c4c4 25566 }
6e70227d 25567
f1e6e072
TT
25568 SYMBOL_ACLASS_INDEX (sym) = (is_block
25569 ? dwarf2_locexpr_block_index
25570 : dwarf2_locexpr_index);
0d53c4c4
DJ
25571 SYMBOL_LOCATION_BATON (sym) = baton;
25572 }
4c2df51b 25573}
6502dd73 25574
9aa1f1e3
TT
25575/* Return the OBJFILE associated with the compilation unit CU. If CU
25576 came from a separate debuginfo file, then the master objfile is
25577 returned. */
ae0d2f24
UW
25578
25579struct objfile *
25580dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25581{
e3b94546 25582 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25583
25584 /* Return the master objfile, so that we can report and look up the
25585 correct file containing this variable. */
25586 if (objfile->separate_debug_objfile_backlink)
25587 objfile = objfile->separate_debug_objfile_backlink;
25588
25589 return objfile;
25590}
25591
96408a79
SA
25592/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25593 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25594 CU_HEADERP first. */
25595
25596static const struct comp_unit_head *
25597per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25598 struct dwarf2_per_cu_data *per_cu)
25599{
d521ce57 25600 const gdb_byte *info_ptr;
96408a79
SA
25601
25602 if (per_cu->cu)
25603 return &per_cu->cu->header;
25604
9c541725 25605 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25606
25607 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25608 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25609 rcuh_kind::COMPILE);
96408a79
SA
25610
25611 return cu_headerp;
25612}
25613
ae0d2f24
UW
25614/* Return the address size given in the compilation unit header for CU. */
25615
98714339 25616int
ae0d2f24
UW
25617dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25618{
96408a79
SA
25619 struct comp_unit_head cu_header_local;
25620 const struct comp_unit_head *cu_headerp;
c471e790 25621
96408a79
SA
25622 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25623
25624 return cu_headerp->addr_size;
ae0d2f24
UW
25625}
25626
9eae7c52
TT
25627/* Return the offset size given in the compilation unit header for CU. */
25628
25629int
25630dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25631{
96408a79
SA
25632 struct comp_unit_head cu_header_local;
25633 const struct comp_unit_head *cu_headerp;
9c6c53f7 25634
96408a79
SA
25635 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25636
25637 return cu_headerp->offset_size;
25638}
25639
25640/* See its dwarf2loc.h declaration. */
25641
25642int
25643dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25644{
25645 struct comp_unit_head cu_header_local;
25646 const struct comp_unit_head *cu_headerp;
25647
25648 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25649
25650 if (cu_headerp->version == 2)
25651 return cu_headerp->addr_size;
25652 else
25653 return cu_headerp->offset_size;
181cebd4
JK
25654}
25655
9aa1f1e3
TT
25656/* Return the text offset of the CU. The returned offset comes from
25657 this CU's objfile. If this objfile came from a separate debuginfo
25658 file, then the offset may be different from the corresponding
25659 offset in the parent objfile. */
25660
25661CORE_ADDR
25662dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25663{
b3b3bada 25664 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
9aa1f1e3
TT
25665}
25666
9a49df9d
AB
25667/* Return a type that is a generic pointer type, the size of which matches
25668 the address size given in the compilation unit header for PER_CU. */
25669static struct type *
25670dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25671{
25672 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25673 struct type *void_type = objfile_type (objfile)->builtin_void;
25674 struct type *addr_type = lookup_pointer_type (void_type);
25675 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25676
25677 if (TYPE_LENGTH (addr_type) == addr_size)
25678 return addr_type;
25679
25680 addr_type
25681 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25682 return addr_type;
25683}
25684
43988095
JK
25685/* Return DWARF version number of PER_CU. */
25686
25687short
25688dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25689{
25690 return per_cu->dwarf_version;
25691}
25692
348e048f
DE
25693/* Locate the .debug_info compilation unit from CU's objfile which contains
25694 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25695
25696static struct dwarf2_per_cu_data *
9c541725 25697dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25698 unsigned int offset_in_dwz,
ed2dc618 25699 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25700{
25701 struct dwarf2_per_cu_data *this_cu;
25702 int low, high;
25703
ae038cb0 25704 low = 0;
b76e467d 25705 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25706 while (high > low)
25707 {
36586728 25708 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25709 int mid = low + (high - low) / 2;
9a619af0 25710
36586728 25711 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25712 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25713 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25714 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25715 high = mid;
25716 else
25717 low = mid + 1;
25718 }
25719 gdb_assert (low == high);
36586728 25720 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25721 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25722 {
36586728 25723 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25724 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25725 "offset %s [in module %s]"),
25726 sect_offset_str (sect_off),
ed2dc618 25727 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25728
9c541725
PA
25729 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25730 <= sect_off);
ae038cb0
DJ
25731 return dwarf2_per_objfile->all_comp_units[low-1];
25732 }
25733 else
25734 {
b76e467d 25735 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25736 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25737 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25738 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25739 return this_cu;
25740 }
25741}
25742
23745b47 25743/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25744
fcd3b13d
SM
25745dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25746 : per_cu (per_cu_),
9068261f
AB
25747 mark (false),
25748 has_loclist (false),
25749 checked_producer (false),
25750 producer_is_gxx_lt_4_6 (false),
25751 producer_is_gcc_lt_4_3 (false),
eb77c9df 25752 producer_is_icc (false),
9068261f 25753 producer_is_icc_lt_14 (false),
c258c396 25754 producer_is_codewarrior (false),
9068261f 25755 processing_has_namespace_info (false)
93311388 25756{
fcd3b13d
SM
25757 per_cu->cu = this;
25758}
25759
25760/* Destroy a dwarf2_cu. */
25761
25762dwarf2_cu::~dwarf2_cu ()
25763{
25764 per_cu->cu = NULL;
9816fde3
JK
25765}
25766
25767/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25768
25769static void
95554aad
TT
25770prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25771 enum language pretend_language)
9816fde3
JK
25772{
25773 struct attribute *attr;
25774
25775 /* Set the language we're debugging. */
25776 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25777 if (attr != nullptr)
9816fde3
JK
25778 set_cu_language (DW_UNSND (attr), cu);
25779 else
9cded63f 25780 {
95554aad 25781 cu->language = pretend_language;
9cded63f
TT
25782 cu->language_defn = language_def (cu->language);
25783 }
dee91e82 25784
7d45c7c3 25785 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25786}
25787
ae038cb0
DJ
25788/* Increase the age counter on each cached compilation unit, and free
25789 any that are too old. */
25790
25791static void
ed2dc618 25792age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25793{
25794 struct dwarf2_per_cu_data *per_cu, **last_chain;
25795
25796 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25797 per_cu = dwarf2_per_objfile->read_in_chain;
25798 while (per_cu != NULL)
25799 {
25800 per_cu->cu->last_used ++;
b4f54984 25801 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25802 dwarf2_mark (per_cu->cu);
25803 per_cu = per_cu->cu->read_in_chain;
25804 }
25805
25806 per_cu = dwarf2_per_objfile->read_in_chain;
25807 last_chain = &dwarf2_per_objfile->read_in_chain;
25808 while (per_cu != NULL)
25809 {
25810 struct dwarf2_per_cu_data *next_cu;
25811
25812 next_cu = per_cu->cu->read_in_chain;
25813
25814 if (!per_cu->cu->mark)
25815 {
fcd3b13d 25816 delete per_cu->cu;
ae038cb0
DJ
25817 *last_chain = next_cu;
25818 }
25819 else
25820 last_chain = &per_cu->cu->read_in_chain;
25821
25822 per_cu = next_cu;
25823 }
25824}
25825
25826/* Remove a single compilation unit from the cache. */
25827
25828static void
dee91e82 25829free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25830{
25831 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25832 struct dwarf2_per_objfile *dwarf2_per_objfile
25833 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25834
25835 per_cu = dwarf2_per_objfile->read_in_chain;
25836 last_chain = &dwarf2_per_objfile->read_in_chain;
25837 while (per_cu != NULL)
25838 {
25839 struct dwarf2_per_cu_data *next_cu;
25840
25841 next_cu = per_cu->cu->read_in_chain;
25842
dee91e82 25843 if (per_cu == target_per_cu)
ae038cb0 25844 {
fcd3b13d 25845 delete per_cu->cu;
dee91e82 25846 per_cu->cu = NULL;
ae038cb0
DJ
25847 *last_chain = next_cu;
25848 break;
25849 }
25850 else
25851 last_chain = &per_cu->cu->read_in_chain;
25852
25853 per_cu = next_cu;
25854 }
25855}
25856
dee91e82
DE
25857/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25858 We store these in a hash table separate from the DIEs, and preserve them
25859 when the DIEs are flushed out of cache.
25860
25861 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25862 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25863 or the type may come from a DWO file. Furthermore, while it's more logical
25864 to use per_cu->section+offset, with Fission the section with the data is in
25865 the DWO file but we don't know that section at the point we need it.
25866 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25867 because we can enter the lookup routine, get_die_type_at_offset, from
25868 outside this file, and thus won't necessarily have PER_CU->cu.
25869 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25870
dee91e82 25871struct dwarf2_per_cu_offset_and_type
1c379e20 25872{
dee91e82 25873 const struct dwarf2_per_cu_data *per_cu;
9c541725 25874 sect_offset sect_off;
1c379e20
DJ
25875 struct type *type;
25876};
25877
dee91e82 25878/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25879
25880static hashval_t
dee91e82 25881per_cu_offset_and_type_hash (const void *item)
1c379e20 25882{
9a3c8263
SM
25883 const struct dwarf2_per_cu_offset_and_type *ofs
25884 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25885
9c541725 25886 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25887}
25888
dee91e82 25889/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25890
25891static int
dee91e82 25892per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25893{
9a3c8263
SM
25894 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25895 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25896 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25897 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25898
dee91e82 25899 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25900 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25901}
25902
25903/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25904 table if necessary. For convenience, return TYPE.
25905
25906 The DIEs reading must have careful ordering to:
85102364 25907 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25908 reading current DIE.
25909 * Not trying to dereference contents of still incompletely read in types
25910 while reading in other DIEs.
25911 * Enable referencing still incompletely read in types just by a pointer to
25912 the type without accessing its fields.
25913
25914 Therefore caller should follow these rules:
25915 * Try to fetch any prerequisite types we may need to build this DIE type
25916 before building the type and calling set_die_type.
e71ec853 25917 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25918 possible before fetching more types to complete the current type.
25919 * Make the type as complete as possible before fetching more types. */
1c379e20 25920
f792889a 25921static struct type *
1c379e20
DJ
25922set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25923{
518817b3
SM
25924 struct dwarf2_per_objfile *dwarf2_per_objfile
25925 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25926 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25927 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25928 struct attribute *attr;
25929 struct dynamic_prop prop;
1c379e20 25930
b4ba55a1
JB
25931 /* For Ada types, make sure that the gnat-specific data is always
25932 initialized (if not already set). There are a few types where
25933 we should not be doing so, because the type-specific area is
25934 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25935 where the type-specific area is used to store the floatformat).
25936 But this is not a problem, because the gnat-specific information
25937 is actually not needed for these types. */
25938 if (need_gnat_info (cu)
25939 && TYPE_CODE (type) != TYPE_CODE_FUNC
25940 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25941 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25942 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25943 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25944 && !HAVE_GNAT_AUX_INFO (type))
25945 INIT_GNAT_SPECIFIC (type);
25946
3f2f83dd
KB
25947 /* Read DW_AT_allocated and set in type. */
25948 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25949 if (attr_form_is_block (attr))
25950 {
9a49df9d
AB
25951 struct type *prop_type
25952 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25953 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25954 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25955 }
25956 else if (attr != NULL)
25957 {
b98664d3 25958 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25959 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25960 sect_offset_str (die->sect_off));
3f2f83dd
KB
25961 }
25962
25963 /* Read DW_AT_associated and set in type. */
25964 attr = dwarf2_attr (die, DW_AT_associated, cu);
25965 if (attr_form_is_block (attr))
25966 {
9a49df9d
AB
25967 struct type *prop_type
25968 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25969 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25970 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25971 }
25972 else if (attr != NULL)
25973 {
b98664d3 25974 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25975 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25976 sect_offset_str (die->sect_off));
3f2f83dd
KB
25977 }
25978
3cdcd0ce
JB
25979 /* Read DW_AT_data_location and set in type. */
25980 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25981 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25982 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25983 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25984
dee91e82 25985 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25986 {
dee91e82
DE
25987 dwarf2_per_objfile->die_type_hash =
25988 htab_create_alloc_ex (127,
25989 per_cu_offset_and_type_hash,
25990 per_cu_offset_and_type_eq,
25991 NULL,
25992 &objfile->objfile_obstack,
25993 hashtab_obstack_allocate,
25994 dummy_obstack_deallocate);
f792889a 25995 }
1c379e20 25996
dee91e82 25997 ofs.per_cu = cu->per_cu;
9c541725 25998 ofs.sect_off = die->sect_off;
1c379e20 25999 ofs.type = type;
dee91e82
DE
26000 slot = (struct dwarf2_per_cu_offset_and_type **)
26001 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 26002 if (*slot)
b98664d3 26003 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 26004 sect_offset_str (die->sect_off));
8d749320
SM
26005 *slot = XOBNEW (&objfile->objfile_obstack,
26006 struct dwarf2_per_cu_offset_and_type);
1c379e20 26007 **slot = ofs;
f792889a 26008 return type;
1c379e20
DJ
26009}
26010
9c541725 26011/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 26012 or return NULL if the die does not have a saved type. */
1c379e20
DJ
26013
26014static struct type *
9c541725 26015get_die_type_at_offset (sect_offset sect_off,
673bfd45 26016 struct dwarf2_per_cu_data *per_cu)
1c379e20 26017{
dee91e82 26018 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 26019 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 26020
dee91e82 26021 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 26022 return NULL;
1c379e20 26023
dee91e82 26024 ofs.per_cu = per_cu;
9c541725 26025 ofs.sect_off = sect_off;
9a3c8263
SM
26026 slot = ((struct dwarf2_per_cu_offset_and_type *)
26027 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
26028 if (slot)
26029 return slot->type;
26030 else
26031 return NULL;
26032}
26033
02142a6c 26034/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
26035 or return NULL if DIE does not have a saved type. */
26036
26037static struct type *
26038get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26039{
9c541725 26040 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
26041}
26042
10b3939b
DJ
26043/* Add a dependence relationship from CU to REF_PER_CU. */
26044
26045static void
26046dwarf2_add_dependence (struct dwarf2_cu *cu,
26047 struct dwarf2_per_cu_data *ref_per_cu)
26048{
26049 void **slot;
26050
26051 if (cu->dependencies == NULL)
26052 cu->dependencies
26053 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26054 NULL, &cu->comp_unit_obstack,
26055 hashtab_obstack_allocate,
26056 dummy_obstack_deallocate);
26057
26058 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26059 if (*slot == NULL)
26060 *slot = ref_per_cu;
26061}
1c379e20 26062
f504f079
DE
26063/* Subroutine of dwarf2_mark to pass to htab_traverse.
26064 Set the mark field in every compilation unit in the
ae038cb0
DJ
26065 cache that we must keep because we are keeping CU. */
26066
10b3939b
DJ
26067static int
26068dwarf2_mark_helper (void **slot, void *data)
26069{
26070 struct dwarf2_per_cu_data *per_cu;
26071
26072 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
26073
26074 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26075 reading of the chain. As such dependencies remain valid it is not much
26076 useful to track and undo them during QUIT cleanups. */
26077 if (per_cu->cu == NULL)
26078 return 1;
26079
10b3939b
DJ
26080 if (per_cu->cu->mark)
26081 return 1;
9068261f 26082 per_cu->cu->mark = true;
10b3939b
DJ
26083
26084 if (per_cu->cu->dependencies != NULL)
26085 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26086
26087 return 1;
26088}
26089
f504f079
DE
26090/* Set the mark field in CU and in every other compilation unit in the
26091 cache that we must keep because we are keeping CU. */
26092
ae038cb0
DJ
26093static void
26094dwarf2_mark (struct dwarf2_cu *cu)
26095{
26096 if (cu->mark)
26097 return;
9068261f 26098 cu->mark = true;
10b3939b
DJ
26099 if (cu->dependencies != NULL)
26100 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
26101}
26102
26103static void
26104dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26105{
26106 while (per_cu)
26107 {
9068261f 26108 per_cu->cu->mark = false;
ae038cb0
DJ
26109 per_cu = per_cu->cu->read_in_chain;
26110 }
72bf9492
DJ
26111}
26112
72bf9492
DJ
26113/* Trivial hash function for partial_die_info: the hash value of a DIE
26114 is its offset in .debug_info for this objfile. */
26115
26116static hashval_t
26117partial_die_hash (const void *item)
26118{
9a3c8263
SM
26119 const struct partial_die_info *part_die
26120 = (const struct partial_die_info *) item;
9a619af0 26121
9c541725 26122 return to_underlying (part_die->sect_off);
72bf9492
DJ
26123}
26124
26125/* Trivial comparison function for partial_die_info structures: two DIEs
26126 are equal if they have the same offset. */
26127
26128static int
26129partial_die_eq (const void *item_lhs, const void *item_rhs)
26130{
9a3c8263
SM
26131 const struct partial_die_info *part_die_lhs
26132 = (const struct partial_die_info *) item_lhs;
26133 const struct partial_die_info *part_die_rhs
26134 = (const struct partial_die_info *) item_rhs;
9a619af0 26135
9c541725 26136 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
26137}
26138
3c3bb058
AB
26139struct cmd_list_element *set_dwarf_cmdlist;
26140struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
26141
26142static void
981a3fb3 26143set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 26144{
b4f54984 26145 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 26146 gdb_stdout);
ae038cb0
DJ
26147}
26148
26149static void
981a3fb3 26150show_dwarf_cmd (const char *args, int from_tty)
6e70227d 26151{
b4f54984 26152 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
26153}
26154
491144b5 26155bool dwarf_always_disassemble;
437afbb8 26156
437afbb8 26157static void
cd4fb1b2
SM
26158show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26159 struct cmd_list_element *c, const char *value)
9291a0cd 26160{
cd4fb1b2
SM
26161 fprintf_filtered (file,
26162 _("Whether to always disassemble "
26163 "DWARF expressions is %s.\n"),
26164 value);
9291a0cd
TT
26165}
26166
9291a0cd 26167static void
cd4fb1b2
SM
26168show_check_physname (struct ui_file *file, int from_tty,
26169 struct cmd_list_element *c, const char *value)
9291a0cd 26170{
cd4fb1b2
SM
26171 fprintf_filtered (file,
26172 _("Whether to check \"physname\" is %s.\n"),
26173 value);
9291a0cd
TT
26174}
26175
6c265988 26176void _initialize_dwarf2_read ();
cd4fb1b2 26177void
6c265988 26178_initialize_dwarf2_read ()
9291a0cd 26179{
cd4fb1b2
SM
26180 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26181Set DWARF specific variables.\n\
590042fc 26182Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
26183 &set_dwarf_cmdlist, "maintenance set dwarf ",
26184 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 26185
cd4fb1b2 26186 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
26187Show DWARF specific variables.\n\
26188Show DWARF variables such as the cache size."),
cd4fb1b2
SM
26189 &show_dwarf_cmdlist, "maintenance show dwarf ",
26190 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 26191
cd4fb1b2
SM
26192 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26193 &dwarf_max_cache_age, _("\
26194Set the upper bound on the age of cached DWARF compilation units."), _("\
26195Show the upper bound on the age of cached DWARF compilation units."), _("\
26196A higher limit means that cached compilation units will be stored\n\
26197in memory longer, and more total memory will be used. Zero disables\n\
26198caching, which can slow down startup."),
26199 NULL,
26200 show_dwarf_max_cache_age,
26201 &set_dwarf_cmdlist,
26202 &show_dwarf_cmdlist);
156942c7 26203
cd4fb1b2
SM
26204 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26205 &dwarf_always_disassemble, _("\
26206Set whether `info address' always disassembles DWARF expressions."), _("\
26207Show whether `info address' always disassembles DWARF expressions."), _("\
26208When enabled, DWARF expressions are always printed in an assembly-like\n\
26209syntax. When disabled, expressions will be printed in a more\n\
26210conversational style, when possible."),
26211 NULL,
26212 show_dwarf_always_disassemble,
26213 &set_dwarf_cmdlist,
26214 &show_dwarf_cmdlist);
9291a0cd 26215
cd4fb1b2
SM
26216 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26217Set debugging of the DWARF reader."), _("\
26218Show debugging of the DWARF reader."), _("\
26219When enabled (non-zero), debugging messages are printed during DWARF\n\
26220reading and symtab expansion. A value of 1 (one) provides basic\n\
26221information. A value greater than 1 provides more verbose information."),
26222 NULL,
26223 NULL,
26224 &setdebuglist, &showdebuglist);
9291a0cd 26225
cd4fb1b2
SM
26226 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26227Set debugging of the DWARF DIE reader."), _("\
26228Show debugging of the DWARF DIE reader."), _("\
26229When enabled (non-zero), DIEs are dumped after they are read in.\n\
26230The value is the maximum depth to print."),
26231 NULL,
26232 NULL,
26233 &setdebuglist, &showdebuglist);
9291a0cd 26234
cd4fb1b2
SM
26235 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26236Set debugging of the dwarf line reader."), _("\
26237Show debugging of the dwarf line reader."), _("\
26238When enabled (non-zero), line number entries are dumped as they are read in.\n\
26239A value of 1 (one) provides basic information.\n\
26240A value greater than 1 provides more verbose information."),
26241 NULL,
26242 NULL,
26243 &setdebuglist, &showdebuglist);
437afbb8 26244
cd4fb1b2
SM
26245 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26246Set cross-checking of \"physname\" code against demangler."), _("\
26247Show cross-checking of \"physname\" code against demangler."), _("\
26248When enabled, GDB's internal \"physname\" code is checked against\n\
26249the demangler."),
26250 NULL, show_check_physname,
26251 &setdebuglist, &showdebuglist);
900e11f9 26252
e615022a
DE
26253 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26254 no_class, &use_deprecated_index_sections, _("\
26255Set whether to use deprecated gdb_index sections."), _("\
26256Show whether to use deprecated gdb_index sections."), _("\
26257When enabled, deprecated .gdb_index sections are used anyway.\n\
26258Normally they are ignored either because of a missing feature or\n\
26259performance issue.\n\
26260Warning: This option must be enabled before gdb reads the file."),
26261 NULL,
26262 NULL,
26263 &setlist, &showlist);
26264
f1e6e072
TT
26265 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26266 &dwarf2_locexpr_funcs);
26267 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26268 &dwarf2_loclist_funcs);
26269
26270 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26271 &dwarf2_block_frame_base_locexpr_funcs);
26272 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26273 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26274
26275#if GDB_SELF_TEST
26276 selftests::register_test ("dw2_expand_symtabs_matching",
26277 selftests::dw2_expand_symtabs_matching::run_test);
26278#endif
6502dd73 26279}
This page took 5.062978 seconds and 4 git commands to generate.