Cast the log10 argument to double to disambiguate it
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
42a4f53d 3 Copyright (C) 1994-2019 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
4de283e4 32#include "dwarf2read.h"
d55e5aa6
TT
33#include "dwarf-index-cache.h"
34#include "dwarf-index-common.h"
4de283e4
TT
35#include "bfd.h"
36#include "elf-bfd.h"
37#include "symtab.h"
38#include "gdbtypes.h"
39#include "objfiles.h"
d55e5aa6 40#include "dwarf2.h"
4de283e4
TT
41#include "buildsym.h"
42#include "demangle.h"
43#include "gdb-demangle.h"
4de283e4
TT
44#include "filenames.h" /* for DOSish file names */
45#include "macrotab.h"
46#include "language.h"
47#include "complaints.h"
d55e5aa6
TT
48#include "dwarf2expr.h"
49#include "dwarf2loc.h"
4de283e4
TT
50#include "cp-support.h"
51#include "hashtab.h"
52#include "command.h"
d55e5aa6 53#include "gdbcmd.h"
4de283e4
TT
54#include "block.h"
55#include "addrmap.h"
56#include "typeprint.h"
57#include "psympriv.h"
4de283e4 58#include "c-lang.h"
d55e5aa6 59#include "go-lang.h"
4de283e4
TT
60#include "valprint.h"
61#include "gdbcore.h" /* for gnutarget */
62#include "gdb/gdb-index.h"
4de283e4
TT
63#include "gdb_bfd.h"
64#include "f-lang.h"
65#include "source.h"
4de283e4 66#include "build-id.h"
d55e5aa6 67#include "namespace.h"
268a13a5
TT
68#include "gdbsupport/function-view.h"
69#include "gdbsupport/gdb_optional.h"
70#include "gdbsupport/underlying.h"
268a13a5 71#include "gdbsupport/hash_enum.h"
4de283e4 72#include "filename-seen-cache.h"
b32b108a 73#include "producer.h"
4de283e4 74#include <fcntl.h>
4de283e4 75#include <algorithm>
4de283e4 76#include <unordered_map>
268a13a5 77#include "gdbsupport/selftest.h"
c9317f21 78#include "rust-lang.h"
268a13a5 79#include "gdbsupport/pathstuff.h"
437afbb8 80
73be47f5
DE
81/* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
b4f54984
DE
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84static unsigned int dwarf_read_debug = 0;
45cfd468 85
d97bc12b 86/* When non-zero, dump DIEs after they are read in. */
b4f54984 87static unsigned int dwarf_die_debug = 0;
d97bc12b 88
27e0867f
DE
89/* When non-zero, dump line number entries as they are read in. */
90static unsigned int dwarf_line_debug = 0;
91
491144b5
CB
92/* When true, cross-check physname against demangler. */
93static bool check_physname = false;
900e11f9 94
491144b5
CB
95/* When true, do not reject deprecated .gdb_index sections. */
96static bool use_deprecated_index_sections = false;
481860b3 97
5bfd760d 98static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 99
f1e6e072
TT
100/* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102static int dwarf2_locexpr_index;
103static int dwarf2_loclist_index;
104static int dwarf2_locexpr_block_index;
105static int dwarf2_loclist_block_index;
106
3f563c84
PA
107/* An index into a (C++) symbol name component in a symbol name as
108 recorded in the mapped_index's symbol table. For each C++ symbol
109 in the symbol table, we record one entry for the start of each
110 component in the symbol in a table of name components, and then
111 sort the table, in order to be able to binary search symbol names,
112 ignoring leading namespaces, both completion and regular look up.
113 For example, for symbol "A::B::C", we'll have an entry that points
114 to "A::B::C", another that points to "B::C", and another for "C".
115 Note that function symbols in GDB index have no parameter
116 information, just the function/method names. You can convert a
117 name_component to a "const char *" using the
118 'mapped_index::symbol_name_at(offset_type)' method. */
119
120struct name_component
121{
122 /* Offset in the symbol name where the component starts. Stored as
123 a (32-bit) offset instead of a pointer to save memory and improve
124 locality on 64-bit architectures. */
125 offset_type name_offset;
126
127 /* The symbol's index in the symbol and constant pool tables of a
128 mapped_index. */
129 offset_type idx;
130};
131
44ed8f3e
PA
132/* Base class containing bits shared by both .gdb_index and
133 .debug_name indexes. */
134
135struct mapped_index_base
136{
22ca247e
TT
137 mapped_index_base () = default;
138 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
139
44ed8f3e
PA
140 /* The name_component table (a sorted vector). See name_component's
141 description above. */
142 std::vector<name_component> name_components;
143
144 /* How NAME_COMPONENTS is sorted. */
145 enum case_sensitivity name_components_casing;
146
147 /* Return the number of names in the symbol table. */
148 virtual size_t symbol_name_count () const = 0;
149
150 /* Get the name of the symbol at IDX in the symbol table. */
151 virtual const char *symbol_name_at (offset_type idx) const = 0;
152
153 /* Return whether the name at IDX in the symbol table should be
154 ignored. */
155 virtual bool symbol_name_slot_invalid (offset_type idx) const
156 {
157 return false;
158 }
159
160 /* Build the symbol name component sorted vector, if we haven't
161 yet. */
162 void build_name_components ();
163
164 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
165 possible matches for LN_NO_PARAMS in the name component
166 vector. */
167 std::pair<std::vector<name_component>::const_iterator,
168 std::vector<name_component>::const_iterator>
3b00ef10
TT
169 find_name_components_bounds (const lookup_name_info &ln_no_params,
170 enum language lang) const;
44ed8f3e
PA
171
172 /* Prevent deleting/destroying via a base class pointer. */
173protected:
174 ~mapped_index_base() = default;
175};
176
9291a0cd
TT
177/* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
fc898b42 179struct mapped_index final : public mapped_index_base
9291a0cd 180{
f00a2de2
PA
181 /* A slot/bucket in the symbol table hash. */
182 struct symbol_table_slot
183 {
184 const offset_type name;
185 const offset_type vec;
186 };
187
559a7a62 188 /* Index data format version. */
3063847f 189 int version = 0;
559a7a62 190
f00a2de2
PA
191 /* The address table data. */
192 gdb::array_view<const gdb_byte> address_table;
b11b1f88 193
3876f04e 194 /* The symbol table, implemented as a hash table. */
f00a2de2 195 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 196
9291a0cd 197 /* A pointer to the constant pool. */
3063847f 198 const char *constant_pool = nullptr;
3f563c84 199
44ed8f3e
PA
200 bool symbol_name_slot_invalid (offset_type idx) const override
201 {
202 const auto &bucket = this->symbol_table[idx];
9ab08412 203 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 204 }
5c58de74 205
3f563c84
PA
206 /* Convenience method to get at the name of the symbol at IDX in the
207 symbol table. */
44ed8f3e 208 const char *symbol_name_at (offset_type idx) const override
f00a2de2 209 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 210
44ed8f3e
PA
211 size_t symbol_name_count () const override
212 { return this->symbol_table.size (); }
9291a0cd
TT
213};
214
927aa2e7
JK
215/* A description of the mapped .debug_names.
216 Uninitialized map has CU_COUNT 0. */
fc898b42 217struct mapped_debug_names final : public mapped_index_base
927aa2e7 218{
ed2dc618
SM
219 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
220 : dwarf2_per_objfile (dwarf2_per_objfile_)
221 {}
222
223 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
224 bfd_endian dwarf5_byte_order;
225 bool dwarf5_is_dwarf64;
226 bool augmentation_is_gdb;
227 uint8_t offset_size;
228 uint32_t cu_count = 0;
229 uint32_t tu_count, bucket_count, name_count;
230 const gdb_byte *cu_table_reordered, *tu_table_reordered;
231 const uint32_t *bucket_table_reordered, *hash_table_reordered;
232 const gdb_byte *name_table_string_offs_reordered;
233 const gdb_byte *name_table_entry_offs_reordered;
234 const gdb_byte *entry_pool;
235
236 struct index_val
237 {
238 ULONGEST dwarf_tag;
239 struct attr
240 {
241 /* Attribute name DW_IDX_*. */
242 ULONGEST dw_idx;
243
244 /* Attribute form DW_FORM_*. */
245 ULONGEST form;
246
247 /* Value if FORM is DW_FORM_implicit_const. */
248 LONGEST implicit_const;
249 };
250 std::vector<attr> attr_vec;
251 };
252
253 std::unordered_map<ULONGEST, index_val> abbrev_map;
254
255 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
256
257 /* Implementation of the mapped_index_base virtual interface, for
258 the name_components cache. */
259
260 const char *symbol_name_at (offset_type idx) const override
261 { return namei_to_name (idx); }
262
263 size_t symbol_name_count () const override
264 { return this->name_count; }
927aa2e7
JK
265};
266
cd4fb1b2 267/* See dwarf2read.h. */
ed2dc618 268
cd4fb1b2 269dwarf2_per_objfile *
ed2dc618
SM
270get_dwarf2_per_objfile (struct objfile *objfile)
271{
5bfd760d 272 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 273}
c906108c 274
251d32d9 275/* Default names of the debugging sections. */
c906108c 276
233a11ab
CS
277/* Note that if the debugging section has been compressed, it might
278 have a name like .zdebug_info. */
279
9cdd5dbd
DE
280static const struct dwarf2_debug_sections dwarf2_elf_names =
281{
251d32d9
TG
282 { ".debug_info", ".zdebug_info" },
283 { ".debug_abbrev", ".zdebug_abbrev" },
284 { ".debug_line", ".zdebug_line" },
285 { ".debug_loc", ".zdebug_loc" },
43988095 286 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 287 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 288 { ".debug_macro", ".zdebug_macro" },
251d32d9 289 { ".debug_str", ".zdebug_str" },
43988095 290 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 291 { ".debug_ranges", ".zdebug_ranges" },
43988095 292 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 293 { ".debug_types", ".zdebug_types" },
3019eac3 294 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
295 { ".debug_frame", ".zdebug_frame" },
296 { ".eh_frame", NULL },
24d3216f 297 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
298 { ".debug_names", ".zdebug_names" },
299 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 300 23
251d32d9 301};
c906108c 302
80626a55 303/* List of DWO/DWP sections. */
3019eac3 304
80626a55 305static const struct dwop_section_names
3019eac3
DE
306{
307 struct dwarf2_section_names abbrev_dwo;
308 struct dwarf2_section_names info_dwo;
309 struct dwarf2_section_names line_dwo;
310 struct dwarf2_section_names loc_dwo;
43988095 311 struct dwarf2_section_names loclists_dwo;
09262596
DE
312 struct dwarf2_section_names macinfo_dwo;
313 struct dwarf2_section_names macro_dwo;
3019eac3
DE
314 struct dwarf2_section_names str_dwo;
315 struct dwarf2_section_names str_offsets_dwo;
316 struct dwarf2_section_names types_dwo;
80626a55
DE
317 struct dwarf2_section_names cu_index;
318 struct dwarf2_section_names tu_index;
3019eac3 319}
80626a55 320dwop_section_names =
3019eac3
DE
321{
322 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
323 { ".debug_info.dwo", ".zdebug_info.dwo" },
324 { ".debug_line.dwo", ".zdebug_line.dwo" },
325 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 326 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
327 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
328 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
329 { ".debug_str.dwo", ".zdebug_str.dwo" },
330 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
331 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
332 { ".debug_cu_index", ".zdebug_cu_index" },
333 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
334};
335
c906108c
SS
336/* local data types */
337
107d2387
AC
338/* The data in a compilation unit header, after target2host
339 translation, looks like this. */
c906108c 340struct comp_unit_head
a738430d 341{
c764a876 342 unsigned int length;
a738430d 343 short version;
a738430d
MK
344 unsigned char addr_size;
345 unsigned char signed_addr_p;
9c541725 346 sect_offset abbrev_sect_off;
57349743 347
a738430d
MK
348 /* Size of file offsets; either 4 or 8. */
349 unsigned int offset_size;
57349743 350
a738430d
MK
351 /* Size of the length field; either 4 or 12. */
352 unsigned int initial_length_size;
57349743 353
43988095
JK
354 enum dwarf_unit_type unit_type;
355
a738430d
MK
356 /* Offset to the first byte of this compilation unit header in the
357 .debug_info section, for resolving relative reference dies. */
9c541725 358 sect_offset sect_off;
57349743 359
d00adf39
DE
360 /* Offset to first die in this cu from the start of the cu.
361 This will be the first byte following the compilation unit header. */
9c541725 362 cu_offset first_die_cu_offset;
43988095 363
a084a2a6
AT
364
365 /* 64-bit signature of this unit. For type units, it denotes the signature of
366 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
367 Also used in DWARF 5, to denote the dwo id when the unit type is
368 DW_UT_skeleton or DW_UT_split_compile. */
43988095
JK
369 ULONGEST signature;
370
371 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 372 cu_offset type_cu_offset_in_tu;
a738430d 373};
c906108c 374
3da10d80
KS
375/* Type used for delaying computation of method physnames.
376 See comments for compute_delayed_physnames. */
377struct delayed_method_info
378{
379 /* The type to which the method is attached, i.e., its parent class. */
380 struct type *type;
381
382 /* The index of the method in the type's function fieldlists. */
383 int fnfield_index;
384
385 /* The index of the method in the fieldlist. */
386 int index;
387
388 /* The name of the DIE. */
389 const char *name;
390
391 /* The DIE associated with this method. */
392 struct die_info *die;
393};
394
e7c27a73
DJ
395/* Internal state when decoding a particular compilation unit. */
396struct dwarf2_cu
397{
fcd3b13d
SM
398 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
399 ~dwarf2_cu ();
400
401 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
402
c24bdb02
KS
403 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
404 Create the set of symtabs used by this TU, or if this TU is sharing
405 symtabs with another TU and the symtabs have already been created
406 then restore those symtabs in the line header.
407 We don't need the pc/line-number mapping for type units. */
408 void setup_type_unit_groups (struct die_info *die);
409
410 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
411 buildsym_compunit constructor. */
412 struct compunit_symtab *start_symtab (const char *name,
413 const char *comp_dir,
414 CORE_ADDR low_pc);
415
416 /* Reset the builder. */
417 void reset_builder () { m_builder.reset (); }
418
d00adf39 419 /* The header of the compilation unit. */
fcd3b13d 420 struct comp_unit_head header {};
e142c38c 421
d00adf39 422 /* Base address of this compilation unit. */
fcd3b13d 423 CORE_ADDR base_address = 0;
d00adf39
DE
424
425 /* Non-zero if base_address has been set. */
fcd3b13d 426 int base_known = 0;
d00adf39 427
e142c38c 428 /* The language we are debugging. */
fcd3b13d
SM
429 enum language language = language_unknown;
430 const struct language_defn *language_defn = nullptr;
e142c38c 431
fcd3b13d 432 const char *producer = nullptr;
b0f35d58 433
c24bdb02 434private:
804d2729
TT
435 /* The symtab builder for this CU. This is only non-NULL when full
436 symbols are being read. */
c24bdb02 437 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 438
c24bdb02 439public:
e142c38c
DJ
440 /* The generic symbol table building routines have separate lists for
441 file scope symbols and all all other scopes (local scopes). So
442 we need to select the right one to pass to add_symbol_to_list().
443 We do it by keeping a pointer to the correct list in list_in_scope.
444
445 FIXME: The original dwarf code just treated the file scope as the
446 first local scope, and all other local scopes as nested local
447 scopes, and worked fine. Check to see if we really need to
448 distinguish these in buildsym.c. */
fcd3b13d 449 struct pending **list_in_scope = nullptr;
e142c38c 450
b64f50a1
JK
451 /* Hash table holding all the loaded partial DIEs
452 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 453 htab_t partial_dies = nullptr;
72bf9492
DJ
454
455 /* Storage for things with the same lifetime as this read-in compilation
456 unit, including partial DIEs. */
fcd3b13d 457 auto_obstack comp_unit_obstack;
72bf9492 458
ae038cb0
DJ
459 /* When multiple dwarf2_cu structures are living in memory, this field
460 chains them all together, so that they can be released efficiently.
461 We will probably also want a generation counter so that most-recently-used
462 compilation units are cached... */
fcd3b13d 463 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 464
69d751e3 465 /* Backlink to our per_cu entry. */
ae038cb0
DJ
466 struct dwarf2_per_cu_data *per_cu;
467
468 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 469 int last_used = 0;
ae038cb0 470
b64f50a1
JK
471 /* A hash table of DIE cu_offset for following references with
472 die_info->offset.sect_off as hash. */
fcd3b13d 473 htab_t die_hash = nullptr;
10b3939b
DJ
474
475 /* Full DIEs if read in. */
fcd3b13d 476 struct die_info *dies = nullptr;
10b3939b
DJ
477
478 /* A set of pointers to dwarf2_per_cu_data objects for compilation
479 units referenced by this one. Only set during full symbol processing;
480 partial symbol tables do not have dependencies. */
fcd3b13d 481 htab_t dependencies = nullptr;
10b3939b 482
cb1df416 483 /* Header data from the line table, during full symbol processing. */
fcd3b13d 484 struct line_header *line_header = nullptr;
4c8aa72d
PA
485 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
486 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
487 this is the DW_TAG_compile_unit die for this CU. We'll hold on
488 to the line header as long as this DIE is being processed. See
489 process_die_scope. */
fcd3b13d 490 die_info *line_header_die_owner = nullptr;
cb1df416 491
3da10d80
KS
492 /* A list of methods which need to have physnames computed
493 after all type information has been read. */
c89b44cd 494 std::vector<delayed_method_info> method_list;
3da10d80 495
96408a79 496 /* To be copied to symtab->call_site_htab. */
fcd3b13d 497 htab_t call_site_htab = nullptr;
96408a79 498
034e5797
DE
499 /* Non-NULL if this CU came from a DWO file.
500 There is an invariant here that is important to remember:
501 Except for attributes copied from the top level DIE in the "main"
502 (or "stub") file in preparation for reading the DWO file
503 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
504 Either there isn't a DWO file (in which case this is NULL and the point
505 is moot), or there is and either we're not going to read it (in which
506 case this is NULL) or there is and we are reading it (in which case this
507 is non-NULL). */
fcd3b13d 508 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
509
510 /* The DW_AT_addr_base attribute if present, zero otherwise
511 (zero is a valid value though).
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 513 ULONGEST addr_base = 0;
3019eac3 514
2e3cf129
DE
515 /* The DW_AT_ranges_base attribute if present, zero otherwise
516 (zero is a valid value though).
1dbab08b 517 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 518 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
519 be used without needing to know whether DWO files are in use or not.
520 N.B. This does not apply to DW_AT_ranges appearing in
521 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
522 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
523 DW_AT_ranges_base *would* have to be applied, and we'd have to care
524 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 525 ULONGEST ranges_base = 0;
2e3cf129 526
c9317f21
TT
527 /* When reading debug info generated by older versions of rustc, we
528 have to rewrite some union types to be struct types with a
529 variant part. This rewriting must be done after the CU is fully
530 read in, because otherwise at the point of rewriting some struct
531 type might not have been fully processed. So, we keep a list of
532 all such types here and process them after expansion. */
533 std::vector<struct type *> rust_unions;
534
ae038cb0 535 /* Mark used when releasing cached dies. */
9068261f 536 bool mark : 1;
ae038cb0 537
8be455d7
JK
538 /* This CU references .debug_loc. See the symtab->locations_valid field.
539 This test is imperfect as there may exist optimized debug code not using
540 any location list and still facing inlining issues if handled as
541 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 542 bool has_loclist : 1;
ba919b58 543
9068261f 544 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
545 if all the producer_is_* fields are valid. This information is cached
546 because profiling CU expansion showed excessive time spent in
547 producer_is_gxx_lt_4_6. */
9068261f
AB
548 bool checked_producer : 1;
549 bool producer_is_gxx_lt_4_6 : 1;
550 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 551 bool producer_is_icc : 1;
9068261f 552 bool producer_is_icc_lt_14 : 1;
c258c396 553 bool producer_is_codewarrior : 1;
4d4ec4e5 554
9068261f 555 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
556 debugging info for C++ namespaces. GCC 3.3.x did not produce
557 this information, but later versions do. */
558
9068261f 559 bool processing_has_namespace_info : 1;
d590ff25
YQ
560
561 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
562
563 /* If this CU was inherited by another CU (via specification,
564 abstract_origin, etc), this is the ancestor CU. */
565 dwarf2_cu *ancestor;
566
567 /* Get the buildsym_compunit for this CU. */
568 buildsym_compunit *get_builder ()
569 {
570 /* If this CU has a builder associated with it, use that. */
571 if (m_builder != nullptr)
572 return m_builder.get ();
573
574 /* Otherwise, search ancestors for a valid builder. */
575 if (ancestor != nullptr)
576 return ancestor->get_builder ();
577
578 return nullptr;
579 }
e7c27a73
DJ
580};
581
094b34ac
DE
582/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
583 This includes type_unit_group and quick_file_names. */
584
585struct stmt_list_hash
586{
587 /* The DWO unit this table is from or NULL if there is none. */
588 struct dwo_unit *dwo_unit;
589
590 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 591 sect_offset line_sect_off;
094b34ac
DE
592};
593
f4dc4d17
DE
594/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
595 an object of this type. */
596
597struct type_unit_group
598{
0186c6a7 599 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
600 To simplify things we create an artificial CU that "includes" all the
601 type units using this stmt_list so that the rest of the code still has
602 a "per_cu" handle on the symtab.
603 This PER_CU is recognized by having no section. */
8a0459fd 604#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
605 struct dwarf2_per_cu_data per_cu;
606
0186c6a7
DE
607 /* The TUs that share this DW_AT_stmt_list entry.
608 This is added to while parsing type units to build partial symtabs,
609 and is deleted afterwards and not used again. */
a8b3b8e9 610 std::vector<signatured_type *> *tus;
f4dc4d17 611
43f3e411 612 /* The compunit symtab.
094b34ac 613 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
614 so we create an essentially anonymous symtab as the compunit symtab. */
615 struct compunit_symtab *compunit_symtab;
f4dc4d17 616
094b34ac
DE
617 /* The data used to construct the hash key. */
618 struct stmt_list_hash hash;
f4dc4d17
DE
619
620 /* The number of symtabs from the line header.
621 The value here must match line_header.num_file_names. */
622 unsigned int num_symtabs;
623
624 /* The symbol tables for this TU (obtained from the files listed in
625 DW_AT_stmt_list).
626 WARNING: The order of entries here must match the order of entries
627 in the line header. After the first TU using this type_unit_group, the
628 line header for the subsequent TUs is recreated from this. This is done
629 because we need to use the same symtabs for each TU using the same
630 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
631 there's no guarantee the line header doesn't have duplicate entries. */
632 struct symtab **symtabs;
633};
634
73869dc2 635/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
636
637struct dwo_sections
638{
639 struct dwarf2_section_info abbrev;
3019eac3
DE
640 struct dwarf2_section_info line;
641 struct dwarf2_section_info loc;
43988095 642 struct dwarf2_section_info loclists;
09262596
DE
643 struct dwarf2_section_info macinfo;
644 struct dwarf2_section_info macro;
3019eac3
DE
645 struct dwarf2_section_info str;
646 struct dwarf2_section_info str_offsets;
80626a55
DE
647 /* In the case of a virtual DWO file, these two are unused. */
648 struct dwarf2_section_info info;
fd5866f6 649 std::vector<dwarf2_section_info> types;
3019eac3
DE
650};
651
c88ee1f0 652/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
653
654struct dwo_unit
655{
656 /* Backlink to the containing struct dwo_file. */
657 struct dwo_file *dwo_file;
658
659 /* The "id" that distinguishes this CU/TU.
660 .debug_info calls this "dwo_id", .debug_types calls this "signature".
661 Since signatures came first, we stick with it for consistency. */
662 ULONGEST signature;
663
664 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 665 struct dwarf2_section_info *section;
3019eac3 666
9c541725
PA
667 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
668 sect_offset sect_off;
3019eac3
DE
669 unsigned int length;
670
671 /* For types, offset in the type's DIE of the type defined by this TU. */
672 cu_offset type_offset_in_tu;
673};
674
73869dc2
DE
675/* include/dwarf2.h defines the DWP section codes.
676 It defines a max value but it doesn't define a min value, which we
677 use for error checking, so provide one. */
678
679enum dwp_v2_section_ids
680{
681 DW_SECT_MIN = 1
682};
683
80626a55 684/* Data for one DWO file.
57d63ce2
DE
685
686 This includes virtual DWO files (a virtual DWO file is a DWO file as it
687 appears in a DWP file). DWP files don't really have DWO files per se -
688 comdat folding of types "loses" the DWO file they came from, and from
689 a high level view DWP files appear to contain a mass of random types.
690 However, to maintain consistency with the non-DWP case we pretend DWP
691 files contain virtual DWO files, and we assign each TU with one virtual
692 DWO file (generally based on the line and abbrev section offsets -
693 a heuristic that seems to work in practice). */
3019eac3
DE
694
695struct dwo_file
696{
51ac9db5
SM
697 dwo_file () = default;
698 DISABLE_COPY_AND_ASSIGN (dwo_file);
699
0ac5b59e 700 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
701 For virtual DWO files the name is constructed from the section offsets
702 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
703 from related CU+TUs. */
51ac9db5 704 const char *dwo_name = nullptr;
0ac5b59e
DE
705
706 /* The DW_AT_comp_dir attribute. */
51ac9db5 707 const char *comp_dir = nullptr;
3019eac3 708
80626a55
DE
709 /* The bfd, when the file is open. Otherwise this is NULL.
710 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 711 gdb_bfd_ref_ptr dbfd;
3019eac3 712
73869dc2
DE
713 /* The sections that make up this DWO file.
714 Remember that for virtual DWO files in DWP V2, these are virtual
715 sections (for lack of a better name). */
51ac9db5 716 struct dwo_sections sections {};
3019eac3 717
33c5cd75
DB
718 /* The CUs in the file.
719 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
720 an extension to handle LLVM's Link Time Optimization output (where
721 multiple source files may be compiled into a single object/dwo pair). */
51ac9db5 722 htab_t cus {};
3019eac3
DE
723
724 /* Table of TUs in the file.
725 Each element is a struct dwo_unit. */
51ac9db5 726 htab_t tus {};
3019eac3
DE
727};
728
80626a55
DE
729/* These sections are what may appear in a DWP file. */
730
731struct dwp_sections
732{
73869dc2 733 /* These are used by both DWP version 1 and 2. */
80626a55
DE
734 struct dwarf2_section_info str;
735 struct dwarf2_section_info cu_index;
736 struct dwarf2_section_info tu_index;
73869dc2
DE
737
738 /* These are only used by DWP version 2 files.
739 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
740 sections are referenced by section number, and are not recorded here.
741 In DWP version 2 there is at most one copy of all these sections, each
742 section being (effectively) comprised of the concatenation of all of the
743 individual sections that exist in the version 1 format.
744 To keep the code simple we treat each of these concatenated pieces as a
745 section itself (a virtual section?). */
746 struct dwarf2_section_info abbrev;
747 struct dwarf2_section_info info;
748 struct dwarf2_section_info line;
749 struct dwarf2_section_info loc;
750 struct dwarf2_section_info macinfo;
751 struct dwarf2_section_info macro;
752 struct dwarf2_section_info str_offsets;
753 struct dwarf2_section_info types;
80626a55
DE
754};
755
73869dc2
DE
756/* These sections are what may appear in a virtual DWO file in DWP version 1.
757 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 758
73869dc2 759struct virtual_v1_dwo_sections
80626a55
DE
760{
761 struct dwarf2_section_info abbrev;
762 struct dwarf2_section_info line;
763 struct dwarf2_section_info loc;
764 struct dwarf2_section_info macinfo;
765 struct dwarf2_section_info macro;
766 struct dwarf2_section_info str_offsets;
767 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 768 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
769 struct dwarf2_section_info info_or_types;
770};
771
73869dc2
DE
772/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
773 In version 2, the sections of the DWO files are concatenated together
774 and stored in one section of that name. Thus each ELF section contains
775 several "virtual" sections. */
776
777struct virtual_v2_dwo_sections
778{
779 bfd_size_type abbrev_offset;
780 bfd_size_type abbrev_size;
781
782 bfd_size_type line_offset;
783 bfd_size_type line_size;
784
785 bfd_size_type loc_offset;
786 bfd_size_type loc_size;
787
788 bfd_size_type macinfo_offset;
789 bfd_size_type macinfo_size;
790
791 bfd_size_type macro_offset;
792 bfd_size_type macro_size;
793
794 bfd_size_type str_offsets_offset;
795 bfd_size_type str_offsets_size;
796
797 /* Each DWP hash table entry records one CU or one TU.
798 That is recorded here, and copied to dwo_unit.section. */
799 bfd_size_type info_or_types_offset;
800 bfd_size_type info_or_types_size;
801};
802
80626a55
DE
803/* Contents of DWP hash tables. */
804
805struct dwp_hash_table
806{
73869dc2 807 uint32_t version, nr_columns;
80626a55 808 uint32_t nr_units, nr_slots;
73869dc2
DE
809 const gdb_byte *hash_table, *unit_table;
810 union
811 {
812 struct
813 {
814 const gdb_byte *indices;
815 } v1;
816 struct
817 {
818 /* This is indexed by column number and gives the id of the section
819 in that column. */
820#define MAX_NR_V2_DWO_SECTIONS \
821 (1 /* .debug_info or .debug_types */ \
822 + 1 /* .debug_abbrev */ \
823 + 1 /* .debug_line */ \
824 + 1 /* .debug_loc */ \
825 + 1 /* .debug_str_offsets */ \
826 + 1 /* .debug_macro or .debug_macinfo */)
827 int section_ids[MAX_NR_V2_DWO_SECTIONS];
828 const gdb_byte *offsets;
829 const gdb_byte *sizes;
830 } v2;
831 } section_pool;
80626a55
DE
832};
833
834/* Data for one DWP file. */
835
836struct dwp_file
837{
400174b1
TT
838 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
839 : name (name_),
840 dbfd (std::move (abfd))
841 {
842 }
843
80626a55
DE
844 /* Name of the file. */
845 const char *name;
846
73869dc2 847 /* File format version. */
400174b1 848 int version = 0;
73869dc2 849
93417882 850 /* The bfd. */
400174b1 851 gdb_bfd_ref_ptr dbfd;
80626a55
DE
852
853 /* Section info for this file. */
400174b1 854 struct dwp_sections sections {};
80626a55 855
57d63ce2 856 /* Table of CUs in the file. */
400174b1 857 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
858
859 /* Table of TUs in the file. */
400174b1 860 const struct dwp_hash_table *tus = nullptr;
80626a55 861
19ac8c2e 862 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
863 htab_t loaded_cus {};
864 htab_t loaded_tus {};
80626a55 865
73869dc2
DE
866 /* Table to map ELF section numbers to their sections.
867 This is only needed for the DWP V1 file format. */
400174b1
TT
868 unsigned int num_sections = 0;
869 asection **elf_sections = nullptr;
80626a55
DE
870};
871
0963b4bd
MS
872/* Struct used to pass misc. parameters to read_die_and_children, et
873 al. which are used for both .debug_info and .debug_types dies.
874 All parameters here are unchanging for the life of the call. This
dee91e82 875 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
876
877struct die_reader_specs
878{
a32a8923 879 /* The bfd of die_section. */
93311388
DE
880 bfd* abfd;
881
882 /* The CU of the DIE we are parsing. */
883 struct dwarf2_cu *cu;
884
80626a55 885 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
886 struct dwo_file *dwo_file;
887
dee91e82 888 /* The section the die comes from.
3019eac3 889 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
890 struct dwarf2_section_info *die_section;
891
892 /* die_section->buffer. */
d521ce57 893 const gdb_byte *buffer;
f664829e
DE
894
895 /* The end of the buffer. */
896 const gdb_byte *buffer_end;
a2ce51a0
DE
897
898 /* The value of the DW_AT_comp_dir attribute. */
899 const char *comp_dir;
685af9cd
TT
900
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
93311388
DE
903};
904
fd820528 905/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 906typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 907 const gdb_byte *info_ptr,
dee91e82
DE
908 struct die_info *comp_unit_die,
909 int has_children,
910 void *data);
911
7ba99d21
AT
912/* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
913 later. */
914typedef int dir_index;
ecfb656c 915
7ba99d21
AT
916/* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
917 and later. */
918typedef int file_name_index;
ecfb656c 919
52059ffd
TT
920struct file_entry
921{
fff8551c
PA
922 file_entry () = default;
923
ecfb656c 924 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
925 unsigned int mod_time_, unsigned int length_)
926 : name (name_),
ecfb656c 927 d_index (d_index_),
fff8551c
PA
928 mod_time (mod_time_),
929 length (length_)
930 {}
931
ecfb656c
PA
932 /* Return the include directory at D_INDEX stored in LH. Returns
933 NULL if D_INDEX is out of bounds. */
8c43009f
PA
934 const char *include_dir (const line_header *lh) const;
935
fff8551c
PA
936 /* The file name. Note this is an observing pointer. The memory is
937 owned by debug_line_buffer. */
938 const char *name {};
939
8c43009f 940 /* The directory index (1-based). */
ecfb656c 941 dir_index d_index {};
fff8551c
PA
942
943 unsigned int mod_time {};
944
945 unsigned int length {};
946
947 /* True if referenced by the Line Number Program. */
948 bool included_p {};
949
83769d0b 950 /* The associated symbol table, if any. */
fff8551c 951 struct symtab *symtab {};
52059ffd
TT
952};
953
debd256d
JB
954/* The line number information for a compilation unit (found in the
955 .debug_line section) begins with a "statement program header",
956 which contains the following information. */
957struct line_header
958{
fff8551c
PA
959 line_header ()
960 : offset_in_dwz {}
961 {}
962
963 /* Add an entry to the include directory table. */
964 void add_include_dir (const char *include_dir);
965
966 /* Add an entry to the file name table. */
ecfb656c 967 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
968 unsigned int mod_time, unsigned int length);
969
7ba99d21
AT
970 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
971 Returns NULL if INDEX is out of bounds. */
ecfb656c 972 const char *include_dir_at (dir_index index) const
8c43009f 973 {
7ba99d21
AT
974 int vec_index;
975 if (version >= 5)
976 vec_index = index;
977 else
978 vec_index = index - 1;
979 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
8c43009f 980 return NULL;
7ba99d21 981 return m_include_dirs[vec_index];
8c43009f
PA
982 }
983
7ba99d21 984 bool is_valid_file_index (int file_index)
8c43009f 985 {
7ba99d21
AT
986 if (version >= 5)
987 return 0 <= file_index && file_index < file_names_size ();
988 return 1 <= file_index && file_index <= file_names_size ();
989 }
ecfb656c 990
7ba99d21
AT
991 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
992 Returns NULL if INDEX is out of bounds. */
993 file_entry *file_name_at (file_name_index index)
994 {
995 int vec_index;
996 if (version >= 5)
997 vec_index = index;
998 else
999 vec_index = index - 1;
1000 if (vec_index < 0 || vec_index >= m_file_names.size ())
fff8551c 1001 return NULL;
7ba99d21 1002 return &m_file_names[vec_index];
fff8551c
PA
1003 }
1004
7ba99d21
AT
1005 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1006 this method should only be used to iterate through all file entries in an
1007 index-agnostic manner. */
1008 std::vector<file_entry> &file_names ()
1009 { return m_file_names; }
1010
527f3840 1011 /* Offset of line number information in .debug_line section. */
9c541725 1012 sect_offset sect_off {};
527f3840
JK
1013
1014 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1015 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1016
1017 unsigned int total_length {};
1018 unsigned short version {};
1019 unsigned int header_length {};
1020 unsigned char minimum_instruction_length {};
1021 unsigned char maximum_ops_per_instruction {};
1022 unsigned char default_is_stmt {};
1023 int line_base {};
1024 unsigned char line_range {};
1025 unsigned char opcode_base {};
debd256d
JB
1026
1027 /* standard_opcode_lengths[i] is the number of operands for the
1028 standard opcode whose value is i. This means that
1029 standard_opcode_lengths[0] is unused, and the last meaningful
1030 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1031 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1032
7ba99d21
AT
1033 int file_names_size ()
1034 { return m_file_names.size(); }
debd256d
JB
1035
1036 /* The start and end of the statement program following this
6502dd73 1037 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1038 const gdb_byte *statement_program_start {}, *statement_program_end {};
7ba99d21
AT
1039
1040 private:
1041 /* The include_directories table. Note these are observing
1042 pointers. The memory is owned by debug_line_buffer. */
1043 std::vector<const char *> m_include_dirs;
1044
1045 /* The file_names table. This is private because the meaning of indexes
1046 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1047 before, and is 0 in DWARF 5 and later). So the client should use
1048 file_name_at method for access. */
1049 std::vector<file_entry> m_file_names;
debd256d 1050};
c906108c 1051
fff8551c
PA
1052typedef std::unique_ptr<line_header> line_header_up;
1053
8c43009f
PA
1054const char *
1055file_entry::include_dir (const line_header *lh) const
1056{
ecfb656c 1057 return lh->include_dir_at (d_index);
8c43009f
PA
1058}
1059
c906108c 1060/* When we construct a partial symbol table entry we only
0963b4bd 1061 need this much information. */
6f06d47b 1062struct partial_die_info : public allocate_on_obstack
c906108c 1063 {
6f06d47b
YQ
1064 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1065
1066 /* Disable assign but still keep copy ctor, which is needed
1067 load_partial_dies. */
1068 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1069
52356b79
YQ
1070 /* Adjust the partial die before generating a symbol for it. This
1071 function may set the is_external flag or change the DIE's
1072 name. */
1073 void fixup (struct dwarf2_cu *cu);
1074
48fbe735
YQ
1075 /* Read a minimal amount of information into the minimal die
1076 structure. */
1077 const gdb_byte *read (const struct die_reader_specs *reader,
1078 const struct abbrev_info &abbrev,
1079 const gdb_byte *info_ptr);
1080
72bf9492 1081 /* Offset of this DIE. */
6f06d47b 1082 const sect_offset sect_off;
72bf9492
DJ
1083
1084 /* DWARF-2 tag for this DIE. */
6f06d47b 1085 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1086
72bf9492 1087 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1088 const unsigned int has_children : 1;
1089
72bf9492
DJ
1090 unsigned int is_external : 1;
1091 unsigned int is_declaration : 1;
1092 unsigned int has_type : 1;
1093 unsigned int has_specification : 1;
1094 unsigned int has_pc_info : 1;
481860b3 1095 unsigned int may_be_inlined : 1;
72bf9492 1096
0c1b455e
TT
1097 /* This DIE has been marked DW_AT_main_subprogram. */
1098 unsigned int main_subprogram : 1;
1099
72bf9492
DJ
1100 /* Flag set if the SCOPE field of this structure has been
1101 computed. */
1102 unsigned int scope_set : 1;
1103
fa4028e9
JB
1104 /* Flag set if the DIE has a byte_size attribute. */
1105 unsigned int has_byte_size : 1;
1106
ff908ebf
AW
1107 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1108 unsigned int has_const_value : 1;
1109
98bfdba5
PA
1110 /* Flag set if any of the DIE's children are template arguments. */
1111 unsigned int has_template_arguments : 1;
1112
52356b79 1113 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1114 unsigned int fixup_called : 1;
1115
36586728
TT
1116 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1117 unsigned int is_dwz : 1;
1118
1119 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1120 unsigned int spec_is_dwz : 1;
1121
72bf9492 1122 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1123 sometimes a default name for unnamed DIEs. */
6f06d47b 1124 const char *name = nullptr;
72bf9492 1125
abc72ce4 1126 /* The linkage name, if present. */
6f06d47b 1127 const char *linkage_name = nullptr;
abc72ce4 1128
72bf9492
DJ
1129 /* The scope to prepend to our children. This is generally
1130 allocated on the comp_unit_obstack, so will disappear
1131 when this compilation unit leaves the cache. */
6f06d47b 1132 const char *scope = nullptr;
72bf9492 1133
95554aad
TT
1134 /* Some data associated with the partial DIE. The tag determines
1135 which field is live. */
1136 union
1137 {
1138 /* The location description associated with this DIE, if any. */
1139 struct dwarf_block *locdesc;
1140 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1141 sect_offset sect_off;
6f06d47b 1142 } d {};
72bf9492
DJ
1143
1144 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1145 CORE_ADDR lowpc = 0;
1146 CORE_ADDR highpc = 0;
72bf9492 1147
93311388 1148 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1149 DW_AT_sibling, if any. */
48fbe735
YQ
1150 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1151 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1152 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1153
1154 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1155 DW_AT_specification (or DW_AT_abstract_origin or
1156 DW_AT_extension). */
6f06d47b 1157 sect_offset spec_offset {};
72bf9492
DJ
1158
1159 /* Pointers to this DIE's parent, first child, and next sibling,
1160 if any. */
6f06d47b
YQ
1161 struct partial_die_info *die_parent = nullptr;
1162 struct partial_die_info *die_child = nullptr;
1163 struct partial_die_info *die_sibling = nullptr;
1164
1165 friend struct partial_die_info *
1166 dwarf2_cu::find_partial_die (sect_offset sect_off);
1167
1168 private:
1169 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1170 partial_die_info (sect_offset sect_off)
1171 : partial_die_info (sect_off, DW_TAG_padding, 0)
1172 {
1173 }
1174
1175 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1176 int has_children_)
1177 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1178 {
1179 is_external = 0;
1180 is_declaration = 0;
1181 has_type = 0;
1182 has_specification = 0;
1183 has_pc_info = 0;
1184 may_be_inlined = 0;
1185 main_subprogram = 0;
1186 scope_set = 0;
1187 has_byte_size = 0;
1188 has_const_value = 0;
1189 has_template_arguments = 0;
1190 fixup_called = 0;
1191 is_dwz = 0;
1192 spec_is_dwz = 0;
1193 }
c906108c
SS
1194 };
1195
0963b4bd 1196/* This data structure holds the information of an abbrev. */
c906108c
SS
1197struct abbrev_info
1198 {
1199 unsigned int number; /* number identifying abbrev */
1200 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1201 unsigned short has_children; /* boolean */
1202 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1203 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1204 struct abbrev_info *next; /* next in chain */
1205 };
1206
1207struct attr_abbrev
1208 {
9d25dd43
DE
1209 ENUM_BITFIELD(dwarf_attribute) name : 16;
1210 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1211
1212 /* It is valid only if FORM is DW_FORM_implicit_const. */
1213 LONGEST implicit_const;
c906108c
SS
1214 };
1215
433df2d4
DE
1216/* Size of abbrev_table.abbrev_hash_table. */
1217#define ABBREV_HASH_SIZE 121
1218
1219/* Top level data structure to contain an abbreviation table. */
1220
1221struct abbrev_table
1222{
685af9cd
TT
1223 explicit abbrev_table (sect_offset off)
1224 : sect_off (off)
1225 {
4a17f768 1226 m_abbrevs =
685af9cd 1227 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1228 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1229 }
1230
1231 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1232
1233 /* Allocate space for a struct abbrev_info object in
1234 ABBREV_TABLE. */
1235 struct abbrev_info *alloc_abbrev ();
1236
1237 /* Add an abbreviation to the table. */
1238 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1239
1240 /* Look up an abbrev in the table.
1241 Returns NULL if the abbrev is not found. */
1242
1243 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1244
1245
f4dc4d17
DE
1246 /* Where the abbrev table came from.
1247 This is used as a sanity check when the table is used. */
685af9cd 1248 const sect_offset sect_off;
433df2d4
DE
1249
1250 /* Storage for the abbrev table. */
685af9cd 1251 auto_obstack abbrev_obstack;
433df2d4 1252
4a17f768
YQ
1253private:
1254
433df2d4
DE
1255 /* Hash table of abbrevs.
1256 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1257 It could be statically allocated, but the previous code didn't so we
1258 don't either. */
4a17f768 1259 struct abbrev_info **m_abbrevs;
433df2d4
DE
1260};
1261
685af9cd
TT
1262typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1263
0963b4bd 1264/* Attributes have a name and a value. */
b60c80d6
DJ
1265struct attribute
1266 {
9d25dd43 1267 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1268 ENUM_BITFIELD(dwarf_form) form : 15;
1269
1270 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1271 field should be in u.str (existing only for DW_STRING) but it is kept
1272 here for better struct attribute alignment. */
1273 unsigned int string_is_canonical : 1;
1274
b60c80d6
DJ
1275 union
1276 {
15d034d0 1277 const char *str;
b60c80d6 1278 struct dwarf_block *blk;
43bbcdc2
PH
1279 ULONGEST unsnd;
1280 LONGEST snd;
b60c80d6 1281 CORE_ADDR addr;
ac9ec31b 1282 ULONGEST signature;
b60c80d6
DJ
1283 }
1284 u;
1285 };
1286
0963b4bd 1287/* This data structure holds a complete die structure. */
c906108c
SS
1288struct die_info
1289 {
76815b17
DE
1290 /* DWARF-2 tag for this DIE. */
1291 ENUM_BITFIELD(dwarf_tag) tag : 16;
1292
1293 /* Number of attributes */
98bfdba5
PA
1294 unsigned char num_attrs;
1295
1296 /* True if we're presently building the full type name for the
1297 type derived from this DIE. */
1298 unsigned char building_fullname : 1;
76815b17 1299
adde2bff
DE
1300 /* True if this die is in process. PR 16581. */
1301 unsigned char in_process : 1;
1302
76815b17
DE
1303 /* Abbrev number */
1304 unsigned int abbrev;
1305
93311388 1306 /* Offset in .debug_info or .debug_types section. */
9c541725 1307 sect_offset sect_off;
78ba4af6
JB
1308
1309 /* The dies in a compilation unit form an n-ary tree. PARENT
1310 points to this die's parent; CHILD points to the first child of
1311 this node; and all the children of a given node are chained
4950bc1c 1312 together via their SIBLING fields. */
639d11d3
DC
1313 struct die_info *child; /* Its first child, if any. */
1314 struct die_info *sibling; /* Its next sibling, if any. */
1315 struct die_info *parent; /* Its parent, if any. */
c906108c 1316
b60c80d6
DJ
1317 /* An array of attributes, with NUM_ATTRS elements. There may be
1318 zero, but it's not common and zero-sized arrays are not
1319 sufficiently portable C. */
1320 struct attribute attrs[1];
c906108c
SS
1321 };
1322
0963b4bd 1323/* Get at parts of an attribute structure. */
c906108c
SS
1324
1325#define DW_STRING(attr) ((attr)->u.str)
8285870a 1326#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1327#define DW_UNSND(attr) ((attr)->u.unsnd)
1328#define DW_BLOCK(attr) ((attr)->u.blk)
1329#define DW_SND(attr) ((attr)->u.snd)
1330#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1331#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1332
0963b4bd 1333/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1334struct dwarf_block
1335 {
56eb65bd 1336 size_t size;
1d6edc3c
JK
1337
1338 /* Valid only if SIZE is not zero. */
d521ce57 1339 const gdb_byte *data;
c906108c
SS
1340 };
1341
c906108c
SS
1342#ifndef ATTR_ALLOC_CHUNK
1343#define ATTR_ALLOC_CHUNK 4
1344#endif
1345
c906108c
SS
1346/* Allocate fields for structs, unions and enums in this size. */
1347#ifndef DW_FIELD_ALLOC_CHUNK
1348#define DW_FIELD_ALLOC_CHUNK 4
1349#endif
1350
c906108c
SS
1351/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1352 but this would require a corresponding change in unpack_field_as_long
1353 and friends. */
1354static int bits_per_byte = 8;
1355
2ddeaf8a
TT
1356/* When reading a variant or variant part, we track a bit more
1357 information about the field, and store it in an object of this
1358 type. */
1359
1360struct variant_field
1361{
1362 /* If we see a DW_TAG_variant, then this will be the discriminant
1363 value. */
1364 ULONGEST discriminant_value;
1365 /* If we see a DW_TAG_variant, then this will be set if this is the
1366 default branch. */
1367 bool default_branch;
1368 /* While reading a DW_TAG_variant_part, this will be set if this
1369 field is the discriminant. */
1370 bool is_discriminant;
1371};
1372
52059ffd
TT
1373struct nextfield
1374{
be2daae6
TT
1375 int accessibility = 0;
1376 int virtuality = 0;
2ddeaf8a 1377 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1378 struct variant_field variant {};
1379 struct field field {};
52059ffd
TT
1380};
1381
1382struct fnfieldlist
1383{
be2daae6
TT
1384 const char *name = nullptr;
1385 std::vector<struct fn_field> fnfields;
52059ffd
TT
1386};
1387
c906108c
SS
1388/* The routines that read and process dies for a C struct or C++ class
1389 pass lists of data member fields and lists of member function fields
1390 in an instance of a field_info structure, as defined below. */
1391struct field_info
c5aa993b 1392 {
0963b4bd 1393 /* List of data member and baseclasses fields. */
be2daae6
TT
1394 std::vector<struct nextfield> fields;
1395 std::vector<struct nextfield> baseclasses;
c906108c 1396
7d0ccb61 1397 /* Number of fields (including baseclasses). */
be2daae6 1398 int nfields = 0;
c906108c 1399
85102364 1400 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1401 int non_public_fields = 0;
c906108c 1402
c5aa993b
JM
1403 /* Member function fieldlist array, contains name of possibly overloaded
1404 member function, number of overloaded member functions and a pointer
1405 to the head of the member function field chain. */
be2daae6 1406 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1407
1408 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1409 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1410 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1411
1412 /* Nested types defined by this class and the number of elements in this
1413 list. */
be2daae6 1414 std::vector<struct decl_field> nested_types_list;
c5aa993b 1415 };
c906108c 1416
10b3939b
DJ
1417/* One item on the queue of compilation units to read in full symbols
1418 for. */
1419struct dwarf2_queue_item
1420{
1421 struct dwarf2_per_cu_data *per_cu;
95554aad 1422 enum language pretend_language;
10b3939b
DJ
1423 struct dwarf2_queue_item *next;
1424};
1425
1426/* The current queue. */
1427static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1428
ae038cb0
DJ
1429/* Loaded secondary compilation units are kept in memory until they
1430 have not been referenced for the processing of this many
1431 compilation units. Set this to zero to disable caching. Cache
1432 sizes of up to at least twenty will improve startup time for
1433 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1434static int dwarf_max_cache_age = 5;
920d2a44 1435static void
b4f54984
DE
1436show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1437 struct cmd_list_element *c, const char *value)
920d2a44 1438{
3e43a32a 1439 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1440 "DWARF compilation units is %s.\n"),
920d2a44
AC
1441 value);
1442}
4390d890 1443\f
c906108c
SS
1444/* local function prototypes */
1445
a32a8923
DE
1446static const char *get_section_name (const struct dwarf2_section_info *);
1447
1448static const char *get_section_file_name (const struct dwarf2_section_info *);
1449
918dd910
JK
1450static void dwarf2_find_base_address (struct die_info *die,
1451 struct dwarf2_cu *cu);
1452
0018ea6f
DE
1453static struct partial_symtab *create_partial_symtab
1454 (struct dwarf2_per_cu_data *per_cu, const char *name);
1455
f1902523
JK
1456static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1457 const gdb_byte *info_ptr,
1458 struct die_info *type_unit_die,
1459 int has_children, void *data);
1460
ed2dc618
SM
1461static void dwarf2_build_psymtabs_hard
1462 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1463
72bf9492
DJ
1464static void scan_partial_symbols (struct partial_die_info *,
1465 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1466 int, struct dwarf2_cu *);
c906108c 1467
72bf9492
DJ
1468static void add_partial_symbol (struct partial_die_info *,
1469 struct dwarf2_cu *);
63d06c5c 1470
72bf9492
DJ
1471static void add_partial_namespace (struct partial_die_info *pdi,
1472 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1473 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1474
5d7cb8df 1475static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1476 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1477 struct dwarf2_cu *cu);
1478
72bf9492
DJ
1479static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1480 struct dwarf2_cu *cu);
91c24f0a 1481
bc30ff58
JB
1482static void add_partial_subprogram (struct partial_die_info *pdi,
1483 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1484 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1485
257e7a09
YQ
1486static void dwarf2_read_symtab (struct partial_symtab *,
1487 struct objfile *);
c906108c 1488
a14ed312 1489static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1490
685af9cd 1491static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1492 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1493 sect_offset);
433df2d4 1494
d521ce57 1495static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1496
dee91e82 1497static struct partial_die_info *load_partial_dies
d521ce57 1498 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1499
fb816e8b
TV
1500/* A pair of partial_die_info and compilation unit. */
1501struct cu_partial_die_info
1502{
1503 /* The compilation unit of the partial_die_info. */
1504 struct dwarf2_cu *cu;
1505 /* A partial_die_info. */
1506 struct partial_die_info *pdi;
122cf0f2
AB
1507
1508 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1509 : cu (cu),
1510 pdi (pdi)
405feb71 1511 { /* Nothing. */ }
122cf0f2
AB
1512
1513private:
1514 cu_partial_die_info () = delete;
fb816e8b
TV
1515};
1516
122cf0f2
AB
1517static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1518 struct dwarf2_cu *);
72bf9492 1519
d521ce57
TT
1520static const gdb_byte *read_attribute (const struct die_reader_specs *,
1521 struct attribute *, struct attr_abbrev *,
1522 const gdb_byte *);
a8329558 1523
a1855c1d 1524static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1525
a1855c1d 1526static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1527
a1855c1d 1528static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1529
15f18d14
AT
1530/* Read the next three bytes (little-endian order) as an unsigned integer. */
1531static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1532
a1855c1d 1533static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1534
a1855c1d 1535static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1536
d521ce57 1537static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1538 unsigned int *);
c906108c 1539
d521ce57 1540static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1541
1542static LONGEST read_checked_initial_length_and_offset
d521ce57 1543 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1544 unsigned int *, unsigned int *);
613e1657 1545
d521ce57
TT
1546static LONGEST read_offset (bfd *, const gdb_byte *,
1547 const struct comp_unit_head *,
c764a876
DE
1548 unsigned int *);
1549
d521ce57 1550static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1551
ed2dc618
SM
1552static sect_offset read_abbrev_offset
1553 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1554 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1555
d521ce57 1556static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1557
d521ce57 1558static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1559
ed2dc618
SM
1560static const char *read_indirect_string
1561 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1562 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1563
ed2dc618
SM
1564static const char *read_indirect_line_string
1565 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1566 const struct comp_unit_head *, unsigned int *);
36586728 1567
ed2dc618
SM
1568static const char *read_indirect_string_at_offset
1569 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1570 LONGEST str_offset);
927aa2e7 1571
ed2dc618
SM
1572static const char *read_indirect_string_from_dwz
1573 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1574
d521ce57 1575static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1576
d521ce57
TT
1577static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1578 const gdb_byte *,
3019eac3
DE
1579 unsigned int *);
1580
d521ce57 1581static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1582 ULONGEST str_index);
3019eac3 1583
e142c38c 1584static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1585
e142c38c
DJ
1586static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1587 struct dwarf2_cu *);
c906108c 1588
348e048f 1589static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1590 unsigned int);
348e048f 1591
7d45c7c3
KB
1592static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1593 struct dwarf2_cu *cu);
1594
a084a2a6
AT
1595static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1596
05cf31d1
JB
1597static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1598 struct dwarf2_cu *cu);
1599
e142c38c 1600static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1601
e142c38c 1602static struct die_info *die_specification (struct die_info *die,
f2f0e013 1603 struct dwarf2_cu **);
63d06c5c 1604
9c541725 1605static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1606 struct dwarf2_cu *cu);
debd256d 1607
f3f5162e 1608static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1609 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1610 CORE_ADDR, int decode_mapping);
c906108c 1611
804d2729
TT
1612static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1613 const char *);
c906108c 1614
a14ed312 1615static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1616 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1617
ff39bb5e 1618static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1619 struct dwarf2_cu *);
c906108c 1620
ff39bb5e 1621static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1622 struct type *type,
1623 const char *name,
1624 struct obstack *obstack,
12df843f 1625 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1626 const gdb_byte **bytes,
98bfdba5 1627 struct dwarf2_locexpr_baton **baton);
2df3850c 1628
e7c27a73 1629static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1630
b4ba55a1
JB
1631static int need_gnat_info (struct dwarf2_cu *);
1632
3e43a32a
MS
1633static struct type *die_descriptive_type (struct die_info *,
1634 struct dwarf2_cu *);
b4ba55a1
JB
1635
1636static void set_descriptive_type (struct type *, struct die_info *,
1637 struct dwarf2_cu *);
1638
e7c27a73
DJ
1639static struct type *die_containing_type (struct die_info *,
1640 struct dwarf2_cu *);
c906108c 1641
ff39bb5e 1642static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1643 struct dwarf2_cu *);
c906108c 1644
f792889a 1645static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1646
673bfd45
DE
1647static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1648
0d5cff50 1649static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1650
6e70227d 1651static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1652 const char *suffix, int physname,
1653 struct dwarf2_cu *cu);
63d06c5c 1654
e7c27a73 1655static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1656
348e048f
DE
1657static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1658
e7c27a73 1659static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1660
e7c27a73 1661static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1662
96408a79
SA
1663static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1664
71a3c369
TT
1665static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1666
ff013f42
JK
1667static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1668 struct dwarf2_cu *, struct partial_symtab *);
1669
3a2b436a 1670/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1671 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1672enum pc_bounds_kind
1673{
e385593e 1674 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1675 PC_BOUNDS_NOT_PRESENT,
1676
e385593e
JK
1677 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1678 were present but they do not form a valid range of PC addresses. */
1679 PC_BOUNDS_INVALID,
1680
3a2b436a
JK
1681 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1682 PC_BOUNDS_RANGES,
1683
1684 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1685 PC_BOUNDS_HIGH_LOW,
1686};
1687
1688static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1689 CORE_ADDR *, CORE_ADDR *,
1690 struct dwarf2_cu *,
1691 struct partial_symtab *);
c906108c 1692
fae299cd
DC
1693static void get_scope_pc_bounds (struct die_info *,
1694 CORE_ADDR *, CORE_ADDR *,
1695 struct dwarf2_cu *);
1696
801e3a5b
JB
1697static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1698 CORE_ADDR, struct dwarf2_cu *);
1699
a14ed312 1700static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1701 struct dwarf2_cu *);
c906108c 1702
a14ed312 1703static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1704 struct type *, struct dwarf2_cu *);
c906108c 1705
a14ed312 1706static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1707 struct die_info *, struct type *,
e7c27a73 1708 struct dwarf2_cu *);
c906108c 1709
a14ed312 1710static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1711 struct type *,
1712 struct dwarf2_cu *);
c906108c 1713
134d01f1 1714static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1715
e7c27a73 1716static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1717
e7c27a73 1718static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1719
5d7cb8df
JK
1720static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1721
804d2729 1722static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1723
27aa8d6a
SW
1724static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1725
74921315
KS
1726static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1727
f55ee35c
JK
1728static struct type *read_module_type (struct die_info *die,
1729 struct dwarf2_cu *cu);
1730
38d518c9 1731static const char *namespace_name (struct die_info *die,
e142c38c 1732 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1733
134d01f1 1734static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1735
e7c27a73 1736static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1737
6e70227d 1738static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1739 struct dwarf2_cu *);
1740
bf6af496 1741static struct die_info *read_die_and_siblings_1
d521ce57 1742 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1743 struct die_info *);
639d11d3 1744
dee91e82 1745static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1746 const gdb_byte *info_ptr,
1747 const gdb_byte **new_info_ptr,
639d11d3
DC
1748 struct die_info *parent);
1749
d521ce57
TT
1750static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1751 struct die_info **, const gdb_byte *,
1752 int *, int);
3019eac3 1753
d521ce57
TT
1754static const gdb_byte *read_full_die (const struct die_reader_specs *,
1755 struct die_info **, const gdb_byte *,
1756 int *);
93311388 1757
e7c27a73 1758static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1759
15d034d0
TT
1760static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1761 struct obstack *);
71c25dea 1762
15d034d0 1763static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1764
15d034d0 1765static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1766 struct die_info *die,
1767 struct dwarf2_cu *cu);
1768
ca69b9e6
DE
1769static const char *dwarf2_physname (const char *name, struct die_info *die,
1770 struct dwarf2_cu *cu);
1771
e142c38c 1772static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1773 struct dwarf2_cu **);
9219021c 1774
f39c6ffd 1775static const char *dwarf_tag_name (unsigned int);
c906108c 1776
f39c6ffd 1777static const char *dwarf_attr_name (unsigned int);
c906108c 1778
a084a2a6
AT
1779static const char *dwarf_unit_type_name (int unit_type);
1780
f39c6ffd 1781static const char *dwarf_form_name (unsigned int);
c906108c 1782
a121b7c1 1783static const char *dwarf_bool_name (unsigned int);
c906108c 1784
f39c6ffd 1785static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1786
f9aca02d 1787static struct die_info *sibling_die (struct die_info *);
c906108c 1788
d97bc12b
DE
1789static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1790
1791static void dump_die_for_error (struct die_info *);
1792
1793static void dump_die_1 (struct ui_file *, int level, int max_level,
1794 struct die_info *);
c906108c 1795
d97bc12b 1796/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1797
51545339 1798static void store_in_ref_table (struct die_info *,
10b3939b 1799 struct dwarf2_cu *);
c906108c 1800
ff39bb5e 1801static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1802
ff39bb5e 1803static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1804
348e048f 1805static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1806 const struct attribute *,
348e048f
DE
1807 struct dwarf2_cu **);
1808
10b3939b 1809static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1810 const struct attribute *,
f2f0e013 1811 struct dwarf2_cu **);
c906108c 1812
348e048f 1813static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1814 const struct attribute *,
348e048f
DE
1815 struct dwarf2_cu **);
1816
ac9ec31b
DE
1817static struct type *get_signatured_type (struct die_info *, ULONGEST,
1818 struct dwarf2_cu *);
1819
1820static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1821 const struct attribute *,
ac9ec31b
DE
1822 struct dwarf2_cu *);
1823
e5fe5e75 1824static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1825
52dc124a 1826static void read_signatured_type (struct signatured_type *);
348e048f 1827
63e43d3a
PMR
1828static int attr_to_dynamic_prop (const struct attribute *attr,
1829 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1830 struct dynamic_prop *prop, struct type *type);
63e43d3a 1831
c906108c
SS
1832/* memory allocation interface */
1833
7b5a2f43 1834static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1835
b60c80d6 1836static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1837
43f3e411 1838static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1839
6e5a29e1 1840static int attr_form_is_block (const struct attribute *);
8e19ed76 1841
6e5a29e1 1842static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1843
6e5a29e1 1844static int attr_form_is_constant (const struct attribute *);
3690dd37 1845
6e5a29e1 1846static int attr_form_is_ref (const struct attribute *);
7771576e 1847
8cf6f0b1
TT
1848static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1849 struct dwarf2_loclist_baton *baton,
ff39bb5e 1850 const struct attribute *attr);
8cf6f0b1 1851
ff39bb5e 1852static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1853 struct symbol *sym,
f1e6e072
TT
1854 struct dwarf2_cu *cu,
1855 int is_block);
4c2df51b 1856
d521ce57
TT
1857static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1858 const gdb_byte *info_ptr,
1859 struct abbrev_info *abbrev);
4bb7a0a7 1860
72bf9492
DJ
1861static hashval_t partial_die_hash (const void *item);
1862
1863static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1864
ae038cb0 1865static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1866 (sect_offset sect_off, unsigned int offset_in_dwz,
1867 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1868
9816fde3 1869static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1870 struct die_info *comp_unit_die,
1871 enum language pretend_language);
93311388 1872
ed2dc618 1873static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1874
dee91e82 1875static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1876
f792889a
DJ
1877static struct type *set_die_type (struct die_info *, struct type *,
1878 struct dwarf2_cu *);
1c379e20 1879
ed2dc618 1880static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1881
ed2dc618 1882static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1883
58f0c718 1884static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1885 enum language);
10b3939b 1886
95554aad
TT
1887static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1888 enum language);
10b3939b 1889
f4dc4d17
DE
1890static void process_full_type_unit (struct dwarf2_per_cu_data *,
1891 enum language);
1892
10b3939b
DJ
1893static void dwarf2_add_dependence (struct dwarf2_cu *,
1894 struct dwarf2_per_cu_data *);
1895
ae038cb0
DJ
1896static void dwarf2_mark (struct dwarf2_cu *);
1897
1898static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1899
b64f50a1 1900static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1901 struct dwarf2_per_cu_data *);
673bfd45 1902
f792889a 1903static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1904
95554aad
TT
1905static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1906 enum language pretend_language);
1907
ed2dc618 1908static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1909
9a49df9d
AB
1910static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1911static struct type *dwarf2_per_cu_addr_sized_int_type
1912 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1913static struct type *dwarf2_per_cu_int_type
1914 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1915 bool unsigned_p);
9a49df9d 1916
b303c6f6
AB
1917/* Class, the destructor of which frees all allocated queue entries. This
1918 will only have work to do if an error was thrown while processing the
1919 dwarf. If no error was thrown then the queue entries should have all
1920 been processed, and freed, as we went along. */
1921
1922class dwarf2_queue_guard
1923{
1924public:
1925 dwarf2_queue_guard () = default;
1926
1927 /* Free any entries remaining on the queue. There should only be
1928 entries left if we hit an error while processing the dwarf. */
1929 ~dwarf2_queue_guard ()
1930 {
1931 struct dwarf2_queue_item *item, *last;
1932
1933 item = dwarf2_queue;
1934 while (item)
1935 {
1936 /* Anything still marked queued is likely to be in an
1937 inconsistent state, so discard it. */
1938 if (item->per_cu->queued)
1939 {
1940 if (item->per_cu->cu != NULL)
1941 free_one_cached_comp_unit (item->per_cu);
1942 item->per_cu->queued = 0;
1943 }
1944
1945 last = item;
1946 item = item->next;
1947 xfree (last);
1948 }
1949
1950 dwarf2_queue = dwarf2_queue_tail = NULL;
1951 }
1952};
1953
d721ba37
PA
1954/* The return type of find_file_and_directory. Note, the enclosed
1955 string pointers are only valid while this object is valid. */
1956
1957struct file_and_directory
1958{
1959 /* The filename. This is never NULL. */
1960 const char *name;
1961
1962 /* The compilation directory. NULL if not known. If we needed to
1963 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1964 points directly to the DW_AT_comp_dir string attribute owned by
1965 the obstack that owns the DIE. */
1966 const char *comp_dir;
1967
1968 /* If we needed to build a new string for comp_dir, this is what
1969 owns the storage. */
1970 std::string comp_dir_storage;
1971};
1972
1973static file_and_directory find_file_and_directory (struct die_info *die,
1974 struct dwarf2_cu *cu);
9291a0cd
TT
1975
1976static char *file_full_name (int file, struct line_header *lh,
1977 const char *comp_dir);
1978
43988095
JK
1979/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1980enum class rcuh_kind { COMPILE, TYPE };
1981
d521ce57 1982static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1983 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1984 struct comp_unit_head *header,
36586728 1985 struct dwarf2_section_info *section,
d521ce57 1986 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1987 rcuh_kind section_kind);
36586728 1988
fd820528 1989static void init_cutu_and_read_dies
f4dc4d17 1990 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1991 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1992 die_reader_func_ftype *die_reader_func, void *data);
1993
dee91e82
DE
1994static void init_cutu_and_read_dies_simple
1995 (struct dwarf2_per_cu_data *this_cu,
1996 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1997
673bfd45 1998static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1999
3019eac3
DE
2000static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2001
57d63ce2 2002static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2003 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2004 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2005 ULONGEST signature, int is_debug_types);
a2ce51a0 2006
ed2dc618
SM
2007static struct dwp_file *get_dwp_file
2008 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2009
3019eac3 2010static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2011 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2012
2013static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2014 (struct signatured_type *, const char *, const char *);
3019eac3 2015
89e63ee4
DE
2016static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2017
263db9a1
TT
2018/* A unique pointer to a dwo_file. */
2019
51ac9db5 2020typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2021
ed2dc618 2022static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2023
1b80a9fa 2024static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2025
2026static void free_line_header_voidp (void *arg);
4390d890
DE
2027\f
2028/* Various complaints about symbol reading that don't abort the process. */
2029
2030static void
2031dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2032{
b98664d3 2033 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2034}
2035
2036static void
2037dwarf2_debug_line_missing_file_complaint (void)
2038{
b98664d3 2039 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2040}
2041
2042static void
2043dwarf2_debug_line_missing_end_sequence_complaint (void)
2044{
b98664d3 2045 complaint (_(".debug_line section has line "
4390d890
DE
2046 "program sequence without an end"));
2047}
2048
2049static void
2050dwarf2_complex_location_expr_complaint (void)
2051{
b98664d3 2052 complaint (_("location expression too complex"));
4390d890
DE
2053}
2054
2055static void
2056dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2057 int arg3)
2058{
b98664d3 2059 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2060 arg1, arg2, arg3);
2061}
2062
2063static void
2064dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2065{
b98664d3 2066 complaint (_("debug info runs off end of %s section"
4390d890 2067 " [in module %s]"),
a32a8923
DE
2068 get_section_name (section),
2069 get_section_file_name (section));
4390d890 2070}
1b80a9fa 2071
4390d890
DE
2072static void
2073dwarf2_macro_malformed_definition_complaint (const char *arg1)
2074{
b98664d3 2075 complaint (_("macro debug info contains a "
4390d890
DE
2076 "malformed macro definition:\n`%s'"),
2077 arg1);
2078}
2079
2080static void
2081dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2082{
b98664d3 2083 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2084 arg1, arg2);
2085}
527f3840
JK
2086
2087/* Hash function for line_header_hash. */
2088
2089static hashval_t
2090line_header_hash (const struct line_header *ofs)
2091{
9c541725 2092 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2093}
2094
2095/* Hash function for htab_create_alloc_ex for line_header_hash. */
2096
2097static hashval_t
2098line_header_hash_voidp (const void *item)
2099{
9a3c8263 2100 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2101
2102 return line_header_hash (ofs);
2103}
2104
2105/* Equality function for line_header_hash. */
2106
2107static int
2108line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2109{
9a3c8263
SM
2110 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2111 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2112
9c541725 2113 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2114 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2115}
2116
4390d890 2117\f
9291a0cd 2118
31aa7e4e
JB
2119/* Read the given attribute value as an address, taking the attribute's
2120 form into account. */
2121
2122static CORE_ADDR
2123attr_value_as_address (struct attribute *attr)
2124{
2125 CORE_ADDR addr;
2126
336d760d
AT
2127 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2128 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2129 {
2130 /* Aside from a few clearly defined exceptions, attributes that
2131 contain an address must always be in DW_FORM_addr form.
2132 Unfortunately, some compilers happen to be violating this
2133 requirement by encoding addresses using other forms, such
2134 as DW_FORM_data4 for example. For those broken compilers,
2135 we try to do our best, without any guarantee of success,
2136 to interpret the address correctly. It would also be nice
2137 to generate a complaint, but that would require us to maintain
2138 a list of legitimate cases where a non-address form is allowed,
2139 as well as update callers to pass in at least the CU's DWARF
2140 version. This is more overhead than what we're willing to
2141 expand for a pretty rare case. */
2142 addr = DW_UNSND (attr);
2143 }
2144 else
2145 addr = DW_ADDR (attr);
2146
2147 return addr;
2148}
2149
330cdd98
PA
2150/* See declaration. */
2151
2152dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2153 const dwarf2_debug_sections *names,
2154 bool can_copy_)
2155 : objfile (objfile_),
2156 can_copy (can_copy_)
330cdd98
PA
2157{
2158 if (names == NULL)
2159 names = &dwarf2_elf_names;
2160
2161 bfd *obfd = objfile->obfd;
2162
2163 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2164 locate_sections (obfd, sec, *names);
2165}
2166
2167dwarf2_per_objfile::~dwarf2_per_objfile ()
2168{
2169 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2170 free_cached_comp_units ();
2171
2172 if (quick_file_names_table)
2173 htab_delete (quick_file_names_table);
2174
2175 if (line_header_hash)
2176 htab_delete (line_header_hash);
2177
b76e467d 2178 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2179 per_cu->imported_symtabs_free ();
fc8e7e75 2180
b2bdb8cf 2181 for (signatured_type *sig_type : all_type_units)
ae640021 2182 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2183
330cdd98
PA
2184 /* Everything else should be on the objfile obstack. */
2185}
2186
2187/* See declaration. */
2188
2189void
2190dwarf2_per_objfile::free_cached_comp_units ()
2191{
2192 dwarf2_per_cu_data *per_cu = read_in_chain;
2193 dwarf2_per_cu_data **last_chain = &read_in_chain;
2194 while (per_cu != NULL)
2195 {
2196 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2197
fcd3b13d 2198 delete per_cu->cu;
330cdd98
PA
2199 *last_chain = next_cu;
2200 per_cu = next_cu;
2201 }
2202}
2203
11ed8cad
TT
2204/* A helper class that calls free_cached_comp_units on
2205 destruction. */
2206
2207class free_cached_comp_units
2208{
2209public:
2210
2211 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2212 : m_per_objfile (per_objfile)
2213 {
2214 }
2215
2216 ~free_cached_comp_units ()
2217 {
2218 m_per_objfile->free_cached_comp_units ();
2219 }
2220
2221 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2222
2223private:
2224
2225 dwarf2_per_objfile *m_per_objfile;
2226};
2227
c906108c 2228/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2229 information and return true if we have enough to do something.
2230 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2231 ELF names are used. CAN_COPY is true for formats where symbol
2232 interposition is possible and so symbol values must follow copy
2233 relocation rules. */
c906108c
SS
2234
2235int
251d32d9 2236dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2237 const struct dwarf2_debug_sections *names,
2238 bool can_copy)
c906108c 2239{
97cbe998
SDJ
2240 if (objfile->flags & OBJF_READNEVER)
2241 return 0;
2242
ed2dc618
SM
2243 struct dwarf2_per_objfile *dwarf2_per_objfile
2244 = get_dwarf2_per_objfile (objfile);
2245
2246 if (dwarf2_per_objfile == NULL)
5bfd760d 2247 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2248 names,
2249 can_copy);
5bfd760d 2250
73869dc2 2251 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2252 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2253 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2254 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2255}
2256
2257/* Return the containing section of virtual section SECTION. */
2258
2259static struct dwarf2_section_info *
2260get_containing_section (const struct dwarf2_section_info *section)
2261{
2262 gdb_assert (section->is_virtual);
2263 return section->s.containing_section;
c906108c
SS
2264}
2265
a32a8923
DE
2266/* Return the bfd owner of SECTION. */
2267
2268static struct bfd *
2269get_section_bfd_owner (const struct dwarf2_section_info *section)
2270{
73869dc2
DE
2271 if (section->is_virtual)
2272 {
2273 section = get_containing_section (section);
2274 gdb_assert (!section->is_virtual);
2275 }
049412e3 2276 return section->s.section->owner;
a32a8923
DE
2277}
2278
2279/* Return the bfd section of SECTION.
2280 Returns NULL if the section is not present. */
2281
2282static asection *
2283get_section_bfd_section (const struct dwarf2_section_info *section)
2284{
73869dc2
DE
2285 if (section->is_virtual)
2286 {
2287 section = get_containing_section (section);
2288 gdb_assert (!section->is_virtual);
2289 }
049412e3 2290 return section->s.section;
a32a8923
DE
2291}
2292
2293/* Return the name of SECTION. */
2294
2295static const char *
2296get_section_name (const struct dwarf2_section_info *section)
2297{
2298 asection *sectp = get_section_bfd_section (section);
2299
2300 gdb_assert (sectp != NULL);
fd361982 2301 return bfd_section_name (sectp);
a32a8923
DE
2302}
2303
2304/* Return the name of the file SECTION is in. */
2305
2306static const char *
2307get_section_file_name (const struct dwarf2_section_info *section)
2308{
2309 bfd *abfd = get_section_bfd_owner (section);
2310
2311 return bfd_get_filename (abfd);
2312}
2313
2314/* Return the id of SECTION.
2315 Returns 0 if SECTION doesn't exist. */
2316
2317static int
2318get_section_id (const struct dwarf2_section_info *section)
2319{
2320 asection *sectp = get_section_bfd_section (section);
2321
2322 if (sectp == NULL)
2323 return 0;
2324 return sectp->id;
2325}
2326
2327/* Return the flags of SECTION.
73869dc2 2328 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2329
2330static int
2331get_section_flags (const struct dwarf2_section_info *section)
2332{
2333 asection *sectp = get_section_bfd_section (section);
2334
2335 gdb_assert (sectp != NULL);
fd361982 2336 return bfd_section_flags (sectp);
a32a8923
DE
2337}
2338
251d32d9
TG
2339/* When loading sections, we look either for uncompressed section or for
2340 compressed section names. */
233a11ab
CS
2341
2342static int
251d32d9
TG
2343section_is_p (const char *section_name,
2344 const struct dwarf2_section_names *names)
233a11ab 2345{
251d32d9
TG
2346 if (names->normal != NULL
2347 && strcmp (section_name, names->normal) == 0)
2348 return 1;
2349 if (names->compressed != NULL
2350 && strcmp (section_name, names->compressed) == 0)
2351 return 1;
2352 return 0;
233a11ab
CS
2353}
2354
330cdd98 2355/* See declaration. */
c906108c 2356
330cdd98
PA
2357void
2358dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2359 const dwarf2_debug_sections &names)
c906108c 2360{
fd361982 2361 flagword aflag = bfd_section_flags (sectp);
251d32d9 2362
dc7650b8
JK
2363 if ((aflag & SEC_HAS_CONTENTS) == 0)
2364 {
2365 }
950b7495
KS
2366 else if (elf_section_data (sectp)->this_hdr.sh_size
2367 > bfd_get_file_size (abfd))
2368 {
2369 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2370 warning (_("Discarding section %s which has a section size (%s"
2371 ") larger than the file size [in module %s]"),
2372 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2373 bfd_get_filename (abfd));
2374 }
330cdd98 2375 else if (section_is_p (sectp->name, &names.info))
c906108c 2376 {
330cdd98 2377 this->info.s.section = sectp;
fd361982 2378 this->info.size = bfd_section_size (sectp);
c906108c 2379 }
330cdd98 2380 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2381 {
330cdd98 2382 this->abbrev.s.section = sectp;
fd361982 2383 this->abbrev.size = bfd_section_size (sectp);
c906108c 2384 }
330cdd98 2385 else if (section_is_p (sectp->name, &names.line))
c906108c 2386 {
330cdd98 2387 this->line.s.section = sectp;
fd361982 2388 this->line.size = bfd_section_size (sectp);
c906108c 2389 }
330cdd98 2390 else if (section_is_p (sectp->name, &names.loc))
c906108c 2391 {
330cdd98 2392 this->loc.s.section = sectp;
fd361982 2393 this->loc.size = bfd_section_size (sectp);
c906108c 2394 }
330cdd98 2395 else if (section_is_p (sectp->name, &names.loclists))
43988095 2396 {
330cdd98 2397 this->loclists.s.section = sectp;
fd361982 2398 this->loclists.size = bfd_section_size (sectp);
43988095 2399 }
330cdd98 2400 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2401 {
330cdd98 2402 this->macinfo.s.section = sectp;
fd361982 2403 this->macinfo.size = bfd_section_size (sectp);
c906108c 2404 }
330cdd98 2405 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2406 {
330cdd98 2407 this->macro.s.section = sectp;
fd361982 2408 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2409 }
330cdd98 2410 else if (section_is_p (sectp->name, &names.str))
c906108c 2411 {
330cdd98 2412 this->str.s.section = sectp;
fd361982 2413 this->str.size = bfd_section_size (sectp);
c906108c 2414 }
330cdd98 2415 else if (section_is_p (sectp->name, &names.line_str))
43988095 2416 {
330cdd98 2417 this->line_str.s.section = sectp;
fd361982 2418 this->line_str.size = bfd_section_size (sectp);
43988095 2419 }
330cdd98 2420 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2421 {
330cdd98 2422 this->addr.s.section = sectp;
fd361982 2423 this->addr.size = bfd_section_size (sectp);
3019eac3 2424 }
330cdd98 2425 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2426 {
330cdd98 2427 this->frame.s.section = sectp;
fd361982 2428 this->frame.size = bfd_section_size (sectp);
b6af0555 2429 }
330cdd98 2430 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2431 {
330cdd98 2432 this->eh_frame.s.section = sectp;
fd361982 2433 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2434 }
330cdd98 2435 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2436 {
330cdd98 2437 this->ranges.s.section = sectp;
fd361982 2438 this->ranges.size = bfd_section_size (sectp);
af34e669 2439 }
330cdd98 2440 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2441 {
330cdd98 2442 this->rnglists.s.section = sectp;
fd361982 2443 this->rnglists.size = bfd_section_size (sectp);
43988095 2444 }
330cdd98 2445 else if (section_is_p (sectp->name, &names.types))
348e048f 2446 {
8b70b953
TT
2447 struct dwarf2_section_info type_section;
2448
2449 memset (&type_section, 0, sizeof (type_section));
049412e3 2450 type_section.s.section = sectp;
fd361982 2451 type_section.size = bfd_section_size (sectp);
8b70b953 2452
fd5866f6 2453 this->types.push_back (type_section);
348e048f 2454 }
330cdd98 2455 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2456 {
330cdd98 2457 this->gdb_index.s.section = sectp;
fd361982 2458 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2459 }
927aa2e7
JK
2460 else if (section_is_p (sectp->name, &names.debug_names))
2461 {
2462 this->debug_names.s.section = sectp;
fd361982 2463 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2464 }
2465 else if (section_is_p (sectp->name, &names.debug_aranges))
2466 {
2467 this->debug_aranges.s.section = sectp;
fd361982 2468 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2469 }
dce234bc 2470
fd361982
AM
2471 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2472 && bfd_section_vma (sectp) == 0)
330cdd98 2473 this->has_section_at_zero = true;
c906108c
SS
2474}
2475
fceca515
DE
2476/* A helper function that decides whether a section is empty,
2477 or not present. */
9e0ac564
TT
2478
2479static int
19ac8c2e 2480dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2481{
73869dc2
DE
2482 if (section->is_virtual)
2483 return section->size == 0;
049412e3 2484 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2485}
2486
cd4fb1b2 2487/* See dwarf2read.h. */
c906108c 2488
cd4fb1b2
SM
2489void
2490dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2491{
a32a8923 2492 asection *sectp;
3019eac3 2493 bfd *abfd;
dce234bc 2494 gdb_byte *buf, *retbuf;
c906108c 2495
be391dca
TT
2496 if (info->readin)
2497 return;
dce234bc 2498 info->buffer = NULL;
dc4ccb6f 2499 info->readin = true;
188dd5d6 2500
9e0ac564 2501 if (dwarf2_section_empty_p (info))
dce234bc 2502 return;
c906108c 2503
a32a8923 2504 sectp = get_section_bfd_section (info);
3019eac3 2505
73869dc2
DE
2506 /* If this is a virtual section we need to read in the real one first. */
2507 if (info->is_virtual)
2508 {
2509 struct dwarf2_section_info *containing_section =
2510 get_containing_section (info);
2511
2512 gdb_assert (sectp != NULL);
2513 if ((sectp->flags & SEC_RELOC) != 0)
2514 {
2515 error (_("Dwarf Error: DWP format V2 with relocations is not"
2516 " supported in section %s [in module %s]"),
2517 get_section_name (info), get_section_file_name (info));
2518 }
2519 dwarf2_read_section (objfile, containing_section);
2520 /* Other code should have already caught virtual sections that don't
2521 fit. */
2522 gdb_assert (info->virtual_offset + info->size
2523 <= containing_section->size);
2524 /* If the real section is empty or there was a problem reading the
2525 section we shouldn't get here. */
2526 gdb_assert (containing_section->buffer != NULL);
2527 info->buffer = containing_section->buffer + info->virtual_offset;
2528 return;
2529 }
2530
4bf44c1c
TT
2531 /* If the section has relocations, we must read it ourselves.
2532 Otherwise we attach it to the BFD. */
2533 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2534 {
d521ce57 2535 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2536 return;
dce234bc 2537 }
dce234bc 2538
224c3ddb 2539 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2540 info->buffer = buf;
dce234bc
PP
2541
2542 /* When debugging .o files, we may need to apply relocations; see
2543 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2544 We never compress sections in .o files, so we only need to
2545 try this when the section is not compressed. */
ac8035ab 2546 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2547 if (retbuf != NULL)
2548 {
2549 info->buffer = retbuf;
2550 return;
2551 }
2552
a32a8923
DE
2553 abfd = get_section_bfd_owner (info);
2554 gdb_assert (abfd != NULL);
2555
dce234bc
PP
2556 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2557 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2558 {
2559 error (_("Dwarf Error: Can't read DWARF data"
2560 " in section %s [in module %s]"),
fd361982 2561 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2562 }
dce234bc
PP
2563}
2564
9e0ac564
TT
2565/* A helper function that returns the size of a section in a safe way.
2566 If you are positive that the section has been read before using the
2567 size, then it is safe to refer to the dwarf2_section_info object's
2568 "size" field directly. In other cases, you must call this
2569 function, because for compressed sections the size field is not set
2570 correctly until the section has been read. */
2571
2572static bfd_size_type
2573dwarf2_section_size (struct objfile *objfile,
2574 struct dwarf2_section_info *info)
2575{
2576 if (!info->readin)
2577 dwarf2_read_section (objfile, info);
2578 return info->size;
2579}
2580
dce234bc 2581/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2582 SECTION_NAME. */
af34e669 2583
dce234bc 2584void
3017a003
TG
2585dwarf2_get_section_info (struct objfile *objfile,
2586 enum dwarf2_section_enum sect,
d521ce57 2587 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2588 bfd_size_type *sizep)
2589{
5bfd760d 2590 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2591 struct dwarf2_section_info *info;
a3b2a86b
TT
2592
2593 /* We may see an objfile without any DWARF, in which case we just
2594 return nothing. */
2595 if (data == NULL)
2596 {
2597 *sectp = NULL;
2598 *bufp = NULL;
2599 *sizep = 0;
2600 return;
2601 }
3017a003
TG
2602 switch (sect)
2603 {
2604 case DWARF2_DEBUG_FRAME:
2605 info = &data->frame;
2606 break;
2607 case DWARF2_EH_FRAME:
2608 info = &data->eh_frame;
2609 break;
2610 default:
2611 gdb_assert_not_reached ("unexpected section");
2612 }
dce234bc 2613
9e0ac564 2614 dwarf2_read_section (objfile, info);
dce234bc 2615
a32a8923 2616 *sectp = get_section_bfd_section (info);
dce234bc
PP
2617 *bufp = info->buffer;
2618 *sizep = info->size;
2619}
2620
36586728
TT
2621/* A helper function to find the sections for a .dwz file. */
2622
2623static void
2624locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2625{
9a3c8263 2626 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2627
2628 /* Note that we only support the standard ELF names, because .dwz
2629 is ELF-only (at the time of writing). */
2630 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2631 {
049412e3 2632 dwz_file->abbrev.s.section = sectp;
fd361982 2633 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2634 }
2635 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2636 {
049412e3 2637 dwz_file->info.s.section = sectp;
fd361982 2638 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2639 }
2640 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2641 {
049412e3 2642 dwz_file->str.s.section = sectp;
fd361982 2643 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2644 }
2645 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2646 {
049412e3 2647 dwz_file->line.s.section = sectp;
fd361982 2648 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2649 }
2650 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2651 {
049412e3 2652 dwz_file->macro.s.section = sectp;
fd361982 2653 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2654 }
2ec9a5e0
TT
2655 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2656 {
049412e3 2657 dwz_file->gdb_index.s.section = sectp;
fd361982 2658 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2659 }
927aa2e7
JK
2660 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2661 {
2662 dwz_file->debug_names.s.section = sectp;
fd361982 2663 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2664 }
36586728
TT
2665}
2666
c4973306 2667/* See dwarf2read.h. */
36586728 2668
c4973306 2669struct dwz_file *
ed2dc618 2670dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2671{
36586728 2672 const char *filename;
acd13123 2673 bfd_size_type buildid_len_arg;
dc294be5
TT
2674 size_t buildid_len;
2675 bfd_byte *buildid;
36586728
TT
2676
2677 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2678 return dwarf2_per_objfile->dwz_file.get ();
36586728 2679
4db1a1dc 2680 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2681 gdb::unique_xmalloc_ptr<char> data
2682 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2683 &buildid_len_arg, &buildid));
4db1a1dc
TT
2684 if (data == NULL)
2685 {
2686 if (bfd_get_error () == bfd_error_no_error)
2687 return NULL;
2688 error (_("could not read '.gnu_debugaltlink' section: %s"),
2689 bfd_errmsg (bfd_get_error ()));
2690 }
791afaa2
TT
2691
2692 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2693
acd13123
TT
2694 buildid_len = (size_t) buildid_len_arg;
2695
791afaa2 2696 filename = data.get ();
d721ba37
PA
2697
2698 std::string abs_storage;
36586728
TT
2699 if (!IS_ABSOLUTE_PATH (filename))
2700 {
14278e1f
TT
2701 gdb::unique_xmalloc_ptr<char> abs
2702 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2703
14278e1f 2704 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2705 filename = abs_storage.c_str ();
36586728
TT
2706 }
2707
dc294be5
TT
2708 /* First try the file name given in the section. If that doesn't
2709 work, try to use the build-id instead. */
192b62ce 2710 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2711 if (dwz_bfd != NULL)
36586728 2712 {
192b62ce 2713 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2714 dwz_bfd.reset (nullptr);
36586728
TT
2715 }
2716
dc294be5
TT
2717 if (dwz_bfd == NULL)
2718 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2719
2720 if (dwz_bfd == NULL)
2721 error (_("could not find '.gnu_debugaltlink' file for %s"),
2722 objfile_name (dwarf2_per_objfile->objfile));
2723
7ff8cb8c
TT
2724 std::unique_ptr<struct dwz_file> result
2725 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2726
7ff8cb8c
TT
2727 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2728 result.get ());
36586728 2729
7ff8cb8c
TT
2730 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2731 result->dwz_bfd.get ());
2732 dwarf2_per_objfile->dwz_file = std::move (result);
2733 return dwarf2_per_objfile->dwz_file.get ();
36586728 2734}
9291a0cd 2735\f
7b9f3c50
DE
2736/* DWARF quick_symbols_functions support. */
2737
2738/* TUs can share .debug_line entries, and there can be a lot more TUs than
2739 unique line tables, so we maintain a separate table of all .debug_line
2740 derived entries to support the sharing.
2741 All the quick functions need is the list of file names. We discard the
2742 line_header when we're done and don't need to record it here. */
2743struct quick_file_names
2744{
094b34ac
DE
2745 /* The data used to construct the hash key. */
2746 struct stmt_list_hash hash;
7b9f3c50
DE
2747
2748 /* The number of entries in file_names, real_names. */
2749 unsigned int num_file_names;
2750
2751 /* The file names from the line table, after being run through
2752 file_full_name. */
2753 const char **file_names;
2754
2755 /* The file names from the line table after being run through
2756 gdb_realpath. These are computed lazily. */
2757 const char **real_names;
2758};
2759
2760/* When using the index (and thus not using psymtabs), each CU has an
2761 object of this type. This is used to hold information needed by
2762 the various "quick" methods. */
2763struct dwarf2_per_cu_quick_data
2764{
2765 /* The file table. This can be NULL if there was no file table
2766 or it's currently not read in.
2767 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2768 struct quick_file_names *file_names;
2769
2770 /* The corresponding symbol table. This is NULL if symbols for this
2771 CU have not yet been read. */
43f3e411 2772 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2773
2774 /* A temporary mark bit used when iterating over all CUs in
2775 expand_symtabs_matching. */
2776 unsigned int mark : 1;
2777
2778 /* True if we've tried to read the file table and found there isn't one.
2779 There will be no point in trying to read it again next time. */
2780 unsigned int no_file_data : 1;
2781};
2782
094b34ac
DE
2783/* Utility hash function for a stmt_list_hash. */
2784
2785static hashval_t
2786hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2787{
2788 hashval_t v = 0;
2789
2790 if (stmt_list_hash->dwo_unit != NULL)
2791 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2792 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2793 return v;
2794}
2795
2796/* Utility equality function for a stmt_list_hash. */
2797
2798static int
2799eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2800 const struct stmt_list_hash *rhs)
2801{
2802 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2803 return 0;
2804 if (lhs->dwo_unit != NULL
2805 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2806 return 0;
2807
9c541725 2808 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2809}
2810
7b9f3c50
DE
2811/* Hash function for a quick_file_names. */
2812
2813static hashval_t
2814hash_file_name_entry (const void *e)
2815{
9a3c8263
SM
2816 const struct quick_file_names *file_data
2817 = (const struct quick_file_names *) e;
7b9f3c50 2818
094b34ac 2819 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2820}
2821
2822/* Equality function for a quick_file_names. */
2823
2824static int
2825eq_file_name_entry (const void *a, const void *b)
2826{
9a3c8263
SM
2827 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2828 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2829
094b34ac 2830 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2831}
2832
2833/* Delete function for a quick_file_names. */
2834
2835static void
2836delete_file_name_entry (void *e)
2837{
9a3c8263 2838 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2839 int i;
2840
2841 for (i = 0; i < file_data->num_file_names; ++i)
2842 {
2843 xfree ((void*) file_data->file_names[i]);
2844 if (file_data->real_names)
2845 xfree ((void*) file_data->real_names[i]);
2846 }
2847
2848 /* The space for the struct itself lives on objfile_obstack,
2849 so we don't free it here. */
2850}
2851
2852/* Create a quick_file_names hash table. */
2853
2854static htab_t
2855create_quick_file_names_table (unsigned int nr_initial_entries)
2856{
2857 return htab_create_alloc (nr_initial_entries,
2858 hash_file_name_entry, eq_file_name_entry,
2859 delete_file_name_entry, xcalloc, xfree);
2860}
9291a0cd 2861
918dd910
JK
2862/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2863 have to be created afterwards. You should call age_cached_comp_units after
2864 processing PER_CU->CU. dw2_setup must have been already called. */
2865
2866static void
58f0c718 2867load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2868{
3019eac3 2869 if (per_cu->is_debug_types)
e5fe5e75 2870 load_full_type_unit (per_cu);
918dd910 2871 else
58f0c718 2872 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2873
cc12ce38
DE
2874 if (per_cu->cu == NULL)
2875 return; /* Dummy CU. */
2dc860c0
DE
2876
2877 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2878}
2879
a0f42c21 2880/* Read in the symbols for PER_CU. */
2fdf6df6 2881
9291a0cd 2882static void
58f0c718 2883dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2884{
ed2dc618 2885 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2886
f4dc4d17
DE
2887 /* Skip type_unit_groups, reading the type units they contain
2888 is handled elsewhere. */
2889 if (IS_TYPE_UNIT_GROUP (per_cu))
2890 return;
2891
b303c6f6
AB
2892 /* The destructor of dwarf2_queue_guard frees any entries left on
2893 the queue. After this point we're guaranteed to leave this function
2894 with the dwarf queue empty. */
2895 dwarf2_queue_guard q_guard;
9291a0cd 2896
95554aad 2897 if (dwarf2_per_objfile->using_index
43f3e411 2898 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2899 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2900 {
2901 queue_comp_unit (per_cu, language_minimal);
58f0c718 2902 load_cu (per_cu, skip_partial);
89e63ee4
DE
2903
2904 /* If we just loaded a CU from a DWO, and we're working with an index
2905 that may badly handle TUs, load all the TUs in that DWO as well.
2906 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2907 if (!per_cu->is_debug_types
cc12ce38 2908 && per_cu->cu != NULL
89e63ee4
DE
2909 && per_cu->cu->dwo_unit != NULL
2910 && dwarf2_per_objfile->index_table != NULL
2911 && dwarf2_per_objfile->index_table->version <= 7
2912 /* DWP files aren't supported yet. */
ed2dc618 2913 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2914 queue_and_load_all_dwo_tus (per_cu);
95554aad 2915 }
9291a0cd 2916
ed2dc618 2917 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2918
2919 /* Age the cache, releasing compilation units that have not
2920 been used recently. */
ed2dc618 2921 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2922}
2923
2924/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2925 the objfile from which this CU came. Returns the resulting symbol
2926 table. */
2fdf6df6 2927
43f3e411 2928static struct compunit_symtab *
58f0c718 2929dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2930{
ed2dc618
SM
2931 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2932
95554aad 2933 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2934 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2935 {
11ed8cad 2936 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2937 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2938 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2939 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2940 }
f194fefb 2941
43f3e411 2942 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2943}
2944
ff4c9fec 2945/* See declaration. */
f4dc4d17 2946
ff4c9fec
SM
2947dwarf2_per_cu_data *
2948dwarf2_per_objfile::get_cutu (int index)
2949{
b76e467d 2950 if (index >= this->all_comp_units.size ())
ff4c9fec 2951 {
b76e467d 2952 index -= this->all_comp_units.size ();
b2bdb8cf 2953 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2954 return &this->all_type_units[index]->per_cu;
2955 }
f4dc4d17 2956
ff4c9fec
SM
2957 return this->all_comp_units[index];
2958}
f4dc4d17 2959
ff4c9fec 2960/* See declaration. */
2fdf6df6 2961
ff4c9fec
SM
2962dwarf2_per_cu_data *
2963dwarf2_per_objfile::get_cu (int index)
1fd400ff 2964{
b76e467d 2965 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2966
ff4c9fec 2967 return this->all_comp_units[index];
f4dc4d17
DE
2968}
2969
ff4c9fec 2970/* See declaration. */
f4dc4d17 2971
ff4c9fec
SM
2972signatured_type *
2973dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2974{
b2bdb8cf 2975 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2976
ff4c9fec 2977 return this->all_type_units[index];
1fd400ff
TT
2978}
2979
4b514bc8
JK
2980/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2981 objfile_obstack, and constructed with the specified field
2982 values. */
2983
2984static dwarf2_per_cu_data *
ed2dc618 2985create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2986 struct dwarf2_section_info *section,
2987 int is_dwz,
2988 sect_offset sect_off, ULONGEST length)
2989{
ed2dc618 2990 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2991 dwarf2_per_cu_data *the_cu
2992 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2993 struct dwarf2_per_cu_data);
2994 the_cu->sect_off = sect_off;
2995 the_cu->length = length;
e3b94546 2996 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2997 the_cu->section = section;
2998 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2999 struct dwarf2_per_cu_quick_data);
3000 the_cu->is_dwz = is_dwz;
3001 return the_cu;
3002}
3003
2ec9a5e0
TT
3004/* A helper for create_cus_from_index that handles a given list of
3005 CUs. */
2fdf6df6 3006
74a0d9f6 3007static void
12359b5e 3008create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3009 const gdb_byte *cu_list, offset_type n_elements,
3010 struct dwarf2_section_info *section,
b76e467d 3011 int is_dwz)
9291a0cd 3012{
12359b5e 3013 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3014 {
74a0d9f6 3015 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3016
3017 sect_offset sect_off
3018 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3019 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3020 cu_list += 2 * 8;
3021
b76e467d 3022 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3023 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3024 sect_off, length);
b76e467d 3025 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3026 }
9291a0cd
TT
3027}
3028
2ec9a5e0 3029/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3030 the CU objects for this objfile. */
2ec9a5e0 3031
74a0d9f6 3032static void
12359b5e 3033create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3034 const gdb_byte *cu_list, offset_type cu_list_elements,
3035 const gdb_byte *dwz_list, offset_type dwz_elements)
3036{
b76e467d
SM
3037 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3038 dwarf2_per_objfile->all_comp_units.reserve
3039 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3040
12359b5e 3041 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3042 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3043
3044 if (dwz_elements == 0)
74a0d9f6 3045 return;
2ec9a5e0 3046
12359b5e
SM
3047 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3048 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3049 &dwz->info, 1);
2ec9a5e0
TT
3050}
3051
1fd400ff 3052/* Create the signatured type hash table from the index. */
673bfd45 3053
74a0d9f6 3054static void
12359b5e
SM
3055create_signatured_type_table_from_index
3056 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3057 struct dwarf2_section_info *section,
3058 const gdb_byte *bytes,
3059 offset_type elements)
1fd400ff 3060{
12359b5e 3061 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3062
b2bdb8cf
SM
3063 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3064 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3065
12359b5e 3066 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3067
12359b5e 3068 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3069 {
52dc124a 3070 struct signatured_type *sig_type;
9c541725 3071 ULONGEST signature;
1fd400ff 3072 void **slot;
9c541725 3073 cu_offset type_offset_in_tu;
1fd400ff 3074
74a0d9f6 3075 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3076 sect_offset sect_off
3077 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3078 type_offset_in_tu
3079 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3080 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3081 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3082 bytes += 3 * 8;
3083
52dc124a 3084 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3085 struct signatured_type);
52dc124a 3086 sig_type->signature = signature;
9c541725 3087 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3088 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3089 sig_type->per_cu.section = section;
9c541725 3090 sig_type->per_cu.sect_off = sect_off;
e3b94546 3091 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3092 sig_type->per_cu.v.quick
1fd400ff
TT
3093 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3094 struct dwarf2_per_cu_quick_data);
3095
52dc124a
DE
3096 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3097 *slot = sig_type;
1fd400ff 3098
b2bdb8cf 3099 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3100 }
3101
673bfd45 3102 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3103}
3104
927aa2e7
JK
3105/* Create the signatured type hash table from .debug_names. */
3106
3107static void
3108create_signatured_type_table_from_debug_names
ed2dc618 3109 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3110 const mapped_debug_names &map,
3111 struct dwarf2_section_info *section,
3112 struct dwarf2_section_info *abbrev_section)
3113{
ed2dc618
SM
3114 struct objfile *objfile = dwarf2_per_objfile->objfile;
3115
927aa2e7
JK
3116 dwarf2_read_section (objfile, section);
3117 dwarf2_read_section (objfile, abbrev_section);
3118
b2bdb8cf
SM
3119 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3120 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3121
3122 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3123
3124 for (uint32_t i = 0; i < map.tu_count; ++i)
3125 {
3126 struct signatured_type *sig_type;
927aa2e7 3127 void **slot;
927aa2e7
JK
3128
3129 sect_offset sect_off
3130 = (sect_offset) (extract_unsigned_integer
3131 (map.tu_table_reordered + i * map.offset_size,
3132 map.offset_size,
3133 map.dwarf5_byte_order));
3134
3135 comp_unit_head cu_header;
ed2dc618
SM
3136 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3137 abbrev_section,
927aa2e7
JK
3138 section->buffer + to_underlying (sect_off),
3139 rcuh_kind::TYPE);
3140
3141 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3142 struct signatured_type);
3143 sig_type->signature = cu_header.signature;
3144 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3145 sig_type->per_cu.is_debug_types = 1;
3146 sig_type->per_cu.section = section;
3147 sig_type->per_cu.sect_off = sect_off;
e3b94546 3148 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3149 sig_type->per_cu.v.quick
3150 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3151 struct dwarf2_per_cu_quick_data);
3152
3153 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3154 *slot = sig_type;
3155
b2bdb8cf 3156 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3157 }
3158
3159 dwarf2_per_objfile->signatured_types = sig_types_hash;
3160}
3161
9291a0cd
TT
3162/* Read the address map data from the mapped index, and use it to
3163 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3164
9291a0cd 3165static void
ed2dc618
SM
3166create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3167 struct mapped_index *index)
9291a0cd 3168{
ed2dc618 3169 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3170 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3171 const gdb_byte *iter, *end;
9291a0cd 3172 struct addrmap *mutable_map;
9291a0cd
TT
3173 CORE_ADDR baseaddr;
3174
8268c778
PA
3175 auto_obstack temp_obstack;
3176
9291a0cd
TT
3177 mutable_map = addrmap_create_mutable (&temp_obstack);
3178
f00a2de2
PA
3179 iter = index->address_table.data ();
3180 end = iter + index->address_table.size ();
9291a0cd
TT
3181
3182 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3183
3184 while (iter < end)
3185 {
3186 ULONGEST hi, lo, cu_index;
3187 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3188 iter += 8;
3189 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3190 iter += 8;
3191 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3192 iter += 4;
f652bce2 3193
24a55014 3194 if (lo > hi)
f652bce2 3195 {
b98664d3 3196 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3197 hex_string (lo), hex_string (hi));
24a55014 3198 continue;
f652bce2 3199 }
24a55014 3200
b76e467d 3201 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3202 {
b98664d3 3203 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3204 (unsigned) cu_index);
24a55014 3205 continue;
f652bce2 3206 }
24a55014 3207
79748972
TT
3208 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3209 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3210 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3211 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3212 }
3213
d320c2b5 3214 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3215 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3216}
3217
927aa2e7
JK
3218/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3219 populate the objfile's psymtabs_addrmap. */
3220
3221static void
ed2dc618 3222create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3223 struct dwarf2_section_info *section)
3224{
ed2dc618 3225 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3226 bfd *abfd = objfile->obfd;
3227 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3228 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3229 SECT_OFF_TEXT (objfile));
3230
3231 auto_obstack temp_obstack;
3232 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3233
3234 std::unordered_map<sect_offset,
3235 dwarf2_per_cu_data *,
3236 gdb::hash_enum<sect_offset>>
3237 debug_info_offset_to_per_cu;
b76e467d 3238 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3239 {
927aa2e7
JK
3240 const auto insertpair
3241 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3242 if (!insertpair.second)
3243 {
3244 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3245 "debug_info_offset %s, ignoring .debug_aranges."),
3246 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3247 return;
3248 }
3249 }
3250
3251 dwarf2_read_section (objfile, section);
3252
3253 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3254
3255 const gdb_byte *addr = section->buffer;
3256
3257 while (addr < section->buffer + section->size)
3258 {
3259 const gdb_byte *const entry_addr = addr;
3260 unsigned int bytes_read;
3261
3262 const LONGEST entry_length = read_initial_length (abfd, addr,
3263 &bytes_read);
3264 addr += bytes_read;
3265
3266 const gdb_byte *const entry_end = addr + entry_length;
3267 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3268 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3269 if (addr + entry_length > section->buffer + section->size)
3270 {
47e3f474 3271 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3272 "length %s exceeds section length %s, "
3273 "ignoring .debug_aranges."),
47e3f474
TV
3274 objfile_name (objfile),
3275 plongest (entry_addr - section->buffer),
927aa2e7
JK
3276 plongest (bytes_read + entry_length),
3277 pulongest (section->size));
3278 return;
3279 }
3280
3281 /* The version number. */
3282 const uint16_t version = read_2_bytes (abfd, addr);
3283 addr += 2;
3284 if (version != 2)
3285 {
47e3f474 3286 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3287 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3288 objfile_name (objfile),
3289 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3290 return;
3291 }
3292
3293 const uint64_t debug_info_offset
3294 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3295 addr += offset_size;
3296 const auto per_cu_it
3297 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3298 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3299 {
47e3f474 3300 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3301 "debug_info_offset %s does not exists, "
3302 "ignoring .debug_aranges."),
47e3f474
TV
3303 objfile_name (objfile),
3304 plongest (entry_addr - section->buffer),
927aa2e7
JK
3305 pulongest (debug_info_offset));
3306 return;
3307 }
3308 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3309
3310 const uint8_t address_size = *addr++;
3311 if (address_size < 1 || address_size > 8)
3312 {
47e3f474 3313 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3314 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3315 objfile_name (objfile),
3316 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3317 return;
3318 }
3319
3320 const uint8_t segment_selector_size = *addr++;
3321 if (segment_selector_size != 0)
3322 {
47e3f474 3323 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3324 "segment_selector_size %u is not supported, "
3325 "ignoring .debug_aranges."),
47e3f474
TV
3326 objfile_name (objfile),
3327 plongest (entry_addr - section->buffer),
927aa2e7
JK
3328 segment_selector_size);
3329 return;
3330 }
3331
3332 /* Must pad to an alignment boundary that is twice the address
3333 size. It is undocumented by the DWARF standard but GCC does
3334 use it. */
3335 for (size_t padding = ((-(addr - section->buffer))
3336 & (2 * address_size - 1));
3337 padding > 0; padding--)
3338 if (*addr++ != 0)
3339 {
47e3f474 3340 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3341 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3342 objfile_name (objfile),
3343 plongest (entry_addr - section->buffer));
927aa2e7
JK
3344 return;
3345 }
3346
3347 for (;;)
3348 {
3349 if (addr + 2 * address_size > entry_end)
3350 {
47e3f474 3351 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3352 "address list is not properly terminated, "
3353 "ignoring .debug_aranges."),
47e3f474
TV
3354 objfile_name (objfile),
3355 plongest (entry_addr - section->buffer));
927aa2e7
JK
3356 return;
3357 }
3358 ULONGEST start = extract_unsigned_integer (addr, address_size,
3359 dwarf5_byte_order);
3360 addr += address_size;
3361 ULONGEST length = extract_unsigned_integer (addr, address_size,
3362 dwarf5_byte_order);
3363 addr += address_size;
3364 if (start == 0 && length == 0)
3365 break;
3366 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3367 {
3368 /* Symbol was eliminated due to a COMDAT group. */
3369 continue;
3370 }
3371 ULONGEST end = start + length;
79748972
TT
3372 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3373 - baseaddr);
3374 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3375 - baseaddr);
927aa2e7
JK
3376 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3377 }
3378 }
3379
d320c2b5 3380 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3381 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3382}
3383
9291a0cd
TT
3384/* Find a slot in the mapped index INDEX for the object named NAME.
3385 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3386 constant pool and return true. If NAME cannot be found, return
3387 false. */
2fdf6df6 3388
109483d9 3389static bool
9291a0cd
TT
3390find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3391 offset_type **vec_out)
3392{
0cf03b49 3393 offset_type hash;
9291a0cd 3394 offset_type slot, step;
559a7a62 3395 int (*cmp) (const char *, const char *);
9291a0cd 3396
791afaa2 3397 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3398 if (current_language->la_language == language_cplus
45280282
IB
3399 || current_language->la_language == language_fortran
3400 || current_language->la_language == language_d)
0cf03b49
JK
3401 {
3402 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3403 not contain any. */
a8719064 3404
72998fb3 3405 if (strchr (name, '(') != NULL)
0cf03b49 3406 {
109483d9 3407 without_params = cp_remove_params (name);
0cf03b49 3408
72998fb3 3409 if (without_params != NULL)
791afaa2 3410 name = without_params.get ();
0cf03b49
JK
3411 }
3412 }
3413
559a7a62 3414 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3415 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3416 simulate our NAME being searched is also lowercased. */
3417 hash = mapped_index_string_hash ((index->version == 4
3418 && case_sensitivity == case_sensitive_off
3419 ? 5 : index->version),
3420 name);
3421
f00a2de2
PA
3422 slot = hash & (index->symbol_table.size () - 1);
3423 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3424 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3425
3426 for (;;)
3427 {
9291a0cd 3428 const char *str;
f00a2de2
PA
3429
3430 const auto &bucket = index->symbol_table[slot];
3431 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3432 return false;
9291a0cd 3433
f00a2de2 3434 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3435 if (!cmp (name, str))
9291a0cd
TT
3436 {
3437 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3438 + MAYBE_SWAP (bucket.vec));
109483d9 3439 return true;
9291a0cd
TT
3440 }
3441
f00a2de2 3442 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3443 }
3444}
3445
4485a1c1
SM
3446/* A helper function that reads the .gdb_index from BUFFER and fills
3447 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3448 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3449 ok to use deprecated sections.
3450
3451 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3452 out parameters that are filled in with information about the CU and
3453 TU lists in the section.
3454
4485a1c1 3455 Returns true if all went well, false otherwise. */
2fdf6df6 3456
d33bc52e 3457static bool
4485a1c1
SM
3458read_gdb_index_from_buffer (struct objfile *objfile,
3459 const char *filename,
3460 bool deprecated_ok,
3461 gdb::array_view<const gdb_byte> buffer,
3462 struct mapped_index *map,
3463 const gdb_byte **cu_list,
3464 offset_type *cu_list_elements,
3465 const gdb_byte **types_list,
3466 offset_type *types_list_elements)
3467{
3468 const gdb_byte *addr = &buffer[0];
82430852 3469
9291a0cd 3470 /* Version check. */
4485a1c1 3471 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3472 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3473 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3474 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3475 indices. */
831adc1f 3476 if (version < 4)
481860b3
GB
3477 {
3478 static int warning_printed = 0;
3479 if (!warning_printed)
3480 {
3481 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3482 filename);
481860b3
GB
3483 warning_printed = 1;
3484 }
3485 return 0;
3486 }
3487 /* Index version 4 uses a different hash function than index version
3488 5 and later.
3489
3490 Versions earlier than 6 did not emit psymbols for inlined
3491 functions. Using these files will cause GDB not to be able to
3492 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3493 indices unless the user has done
3494 "set use-deprecated-index-sections on". */
2ec9a5e0 3495 if (version < 6 && !deprecated_ok)
481860b3
GB
3496 {
3497 static int warning_printed = 0;
3498 if (!warning_printed)
3499 {
e615022a
DE
3500 warning (_("\
3501Skipping deprecated .gdb_index section in %s.\n\
3502Do \"set use-deprecated-index-sections on\" before the file is read\n\
3503to use the section anyway."),
2ec9a5e0 3504 filename);
481860b3
GB
3505 warning_printed = 1;
3506 }
3507 return 0;
3508 }
796a7ff8 3509 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3510 of the TU (for symbols coming from TUs),
3511 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3512 Plus gold-generated indices can have duplicate entries for global symbols,
3513 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3514 These are just performance bugs, and we can't distinguish gdb-generated
3515 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3516
481860b3 3517 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3518 longer backward compatible. */
796a7ff8 3519 if (version > 8)
594e8718 3520 return 0;
9291a0cd 3521
559a7a62 3522 map->version = version;
9291a0cd 3523
4485a1c1 3524 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3525
4485a1c1 3526 int i = 0;
2ec9a5e0
TT
3527 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3528 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3529 / 8);
1fd400ff
TT
3530 ++i;
3531
2ec9a5e0
TT
3532 *types_list = addr + MAYBE_SWAP (metadata[i]);
3533 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3534 - MAYBE_SWAP (metadata[i]))
3535 / 8);
987d643c 3536 ++i;
1fd400ff 3537
f00a2de2
PA
3538 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3539 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3540 map->address_table
3541 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3542 ++i;
3543
f00a2de2
PA
3544 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3545 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3546 map->symbol_table
3547 = gdb::array_view<mapped_index::symbol_table_slot>
3548 ((mapped_index::symbol_table_slot *) symbol_table,
3549 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3550
f00a2de2 3551 ++i;
f9d83a0b 3552 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3553
2ec9a5e0
TT
3554 return 1;
3555}
3556
4485a1c1
SM
3557/* Callback types for dwarf2_read_gdb_index. */
3558
3559typedef gdb::function_view
3560 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3561 get_gdb_index_contents_ftype;
3562typedef gdb::function_view
3563 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3564 get_gdb_index_contents_dwz_ftype;
3565
927aa2e7 3566/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3567 elements of all the CUs and return 1. Otherwise, return 0. */
3568
3569static int
4485a1c1
SM
3570dwarf2_read_gdb_index
3571 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3572 get_gdb_index_contents_ftype get_gdb_index_contents,
3573 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3574{
2ec9a5e0
TT
3575 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3576 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3577 struct dwz_file *dwz;
12359b5e 3578 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3579
4485a1c1
SM
3580 gdb::array_view<const gdb_byte> main_index_contents
3581 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3582
3583 if (main_index_contents.empty ())
3584 return 0;
3585
3063847f 3586 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3587 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3588 use_deprecated_index_sections,
3589 main_index_contents, map.get (), &cu_list,
3590 &cu_list_elements, &types_list,
3591 &types_list_elements))
2ec9a5e0
TT
3592 return 0;
3593
0fefef59 3594 /* Don't use the index if it's empty. */
3063847f 3595 if (map->symbol_table.empty ())
0fefef59
DE
3596 return 0;
3597
2ec9a5e0
TT
3598 /* If there is a .dwz file, read it so we can get its CU list as
3599 well. */
ed2dc618 3600 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3601 if (dwz != NULL)
2ec9a5e0 3602 {
2ec9a5e0
TT
3603 struct mapped_index dwz_map;
3604 const gdb_byte *dwz_types_ignore;
3605 offset_type dwz_types_elements_ignore;
3606
4485a1c1
SM
3607 gdb::array_view<const gdb_byte> dwz_index_content
3608 = get_gdb_index_contents_dwz (objfile, dwz);
3609
3610 if (dwz_index_content.empty ())
3611 return 0;
3612
3613 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3614 bfd_get_filename (dwz->dwz_bfd.get ()),
3615 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3616 &dwz_list, &dwz_list_elements,
3617 &dwz_types_ignore,
3618 &dwz_types_elements_ignore))
2ec9a5e0
TT
3619 {
3620 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3621 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3622 return 0;
3623 }
3624 }
3625
12359b5e
SM
3626 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3627 dwz_list, dwz_list_elements);
1fd400ff 3628
8b70b953
TT
3629 if (types_list_elements)
3630 {
8b70b953
TT
3631 /* We can only handle a single .debug_types when we have an
3632 index. */
fd5866f6 3633 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3634 return 0;
3635
fd5866f6 3636 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3637
12359b5e
SM
3638 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3639 types_list, types_list_elements);
8b70b953 3640 }
9291a0cd 3641
3063847f 3642 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3643
3063847f 3644 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3645 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3646 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3647 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3648
3649 return 1;
3650}
3651
dee91e82 3652/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3653
dee91e82
DE
3654static void
3655dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3656 const gdb_byte *info_ptr,
dee91e82
DE
3657 struct die_info *comp_unit_die,
3658 int has_children,
3659 void *data)
9291a0cd 3660{
dee91e82 3661 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3662 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3663 struct dwarf2_per_objfile *dwarf2_per_objfile
3664 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3665 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3666 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3667 struct attribute *attr;
7b9f3c50
DE
3668 void **slot;
3669 struct quick_file_names *qfn;
9291a0cd 3670
0186c6a7
DE
3671 gdb_assert (! this_cu->is_debug_types);
3672
07261596
TT
3673 /* Our callers never want to match partial units -- instead they
3674 will match the enclosing full CU. */
3675 if (comp_unit_die->tag == DW_TAG_partial_unit)
3676 {
3677 this_cu->v.quick->no_file_data = 1;
3678 return;
3679 }
3680
0186c6a7 3681 lh_cu = this_cu;
7b9f3c50 3682 slot = NULL;
dee91e82 3683
fff8551c 3684 line_header_up lh;
9c541725 3685 sect_offset line_offset {};
fff8551c 3686
dee91e82 3687 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3688 if (attr != nullptr)
9291a0cd 3689 {
7b9f3c50
DE
3690 struct quick_file_names find_entry;
3691
9c541725 3692 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3693
3694 /* We may have already read in this line header (TU line header sharing).
3695 If we have we're done. */
094b34ac 3696 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3697 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3698 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3699 &find_entry, INSERT);
3700 if (*slot != NULL)
3701 {
9a3c8263 3702 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3703 return;
7b9f3c50
DE
3704 }
3705
3019eac3 3706 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3707 }
3708 if (lh == NULL)
3709 {
094b34ac 3710 lh_cu->v.quick->no_file_data = 1;
dee91e82 3711 return;
9291a0cd
TT
3712 }
3713
8d749320 3714 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3715 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3716 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3717 gdb_assert (slot != NULL);
3718 *slot = qfn;
9291a0cd 3719
d721ba37 3720 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3721
aa391654
TT
3722 int offset = 0;
3723 if (strcmp (fnd.name, "<unknown>") != 0)
3724 ++offset;
3725
7ba99d21 3726 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3727 qfn->file_names =
aa391654
TT
3728 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3729 if (offset != 0)
3730 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3731 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3732 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3733 qfn->real_names = NULL;
9291a0cd 3734
094b34ac 3735 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3736}
3737
3738/* A helper for the "quick" functions which attempts to read the line
3739 table for THIS_CU. */
3740
3741static struct quick_file_names *
e4a48d9d 3742dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3743{
0186c6a7
DE
3744 /* This should never be called for TUs. */
3745 gdb_assert (! this_cu->is_debug_types);
3746 /* Nor type unit groups. */
3747 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3748
dee91e82
DE
3749 if (this_cu->v.quick->file_names != NULL)
3750 return this_cu->v.quick->file_names;
3751 /* If we know there is no line data, no point in looking again. */
3752 if (this_cu->v.quick->no_file_data)
3753 return NULL;
3754
0186c6a7 3755 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3756
3757 if (this_cu->v.quick->no_file_data)
3758 return NULL;
3759 return this_cu->v.quick->file_names;
9291a0cd
TT
3760}
3761
3762/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3763 real path for a given file name from the line table. */
2fdf6df6 3764
9291a0cd 3765static const char *
7b9f3c50
DE
3766dw2_get_real_path (struct objfile *objfile,
3767 struct quick_file_names *qfn, int index)
9291a0cd 3768{
7b9f3c50
DE
3769 if (qfn->real_names == NULL)
3770 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3771 qfn->num_file_names, const char *);
9291a0cd 3772
7b9f3c50 3773 if (qfn->real_names[index] == NULL)
14278e1f 3774 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3775
7b9f3c50 3776 return qfn->real_names[index];
9291a0cd
TT
3777}
3778
3779static struct symtab *
3780dw2_find_last_source_symtab (struct objfile *objfile)
3781{
ed2dc618
SM
3782 struct dwarf2_per_objfile *dwarf2_per_objfile
3783 = get_dwarf2_per_objfile (objfile);
b76e467d 3784 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3785 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3786
43f3e411
DE
3787 if (cust == NULL)
3788 return NULL;
ed2dc618 3789
43f3e411 3790 return compunit_primary_filetab (cust);
9291a0cd
TT
3791}
3792
7b9f3c50
DE
3793/* Traversal function for dw2_forget_cached_source_info. */
3794
3795static int
3796dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3797{
7b9f3c50 3798 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3799
7b9f3c50 3800 if (file_data->real_names)
9291a0cd 3801 {
7b9f3c50 3802 int i;
9291a0cd 3803
7b9f3c50 3804 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3805 {
7b9f3c50
DE
3806 xfree ((void*) file_data->real_names[i]);
3807 file_data->real_names[i] = NULL;
9291a0cd
TT
3808 }
3809 }
7b9f3c50
DE
3810
3811 return 1;
3812}
3813
3814static void
3815dw2_forget_cached_source_info (struct objfile *objfile)
3816{
ed2dc618
SM
3817 struct dwarf2_per_objfile *dwarf2_per_objfile
3818 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3819
3820 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3821 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3822}
3823
f8eba3c6
TT
3824/* Helper function for dw2_map_symtabs_matching_filename that expands
3825 the symtabs and calls the iterator. */
3826
3827static int
3828dw2_map_expand_apply (struct objfile *objfile,
3829 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3830 const char *name, const char *real_path,
14bc53a8 3831 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3832{
43f3e411 3833 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3834
3835 /* Don't visit already-expanded CUs. */
43f3e411 3836 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3837 return 0;
3838
3839 /* This may expand more than one symtab, and we want to iterate over
3840 all of them. */
58f0c718 3841 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3842
14bc53a8
PA
3843 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3844 last_made, callback);
f8eba3c6
TT
3845}
3846
3847/* Implementation of the map_symtabs_matching_filename method. */
3848
14bc53a8
PA
3849static bool
3850dw2_map_symtabs_matching_filename
3851 (struct objfile *objfile, const char *name, const char *real_path,
3852 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3853{
c011a4f4 3854 const char *name_basename = lbasename (name);
ed2dc618
SM
3855 struct dwarf2_per_objfile *dwarf2_per_objfile
3856 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3857
848e3e78
DE
3858 /* The rule is CUs specify all the files, including those used by
3859 any TU, so there's no need to scan TUs here. */
f4dc4d17 3860
b76e467d 3861 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3862 {
3d7bb9d9 3863 /* We only need to look at symtabs not already expanded. */
43f3e411 3864 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3865 continue;
3866
b76e467d 3867 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3868 if (file_data == NULL)
9291a0cd
TT
3869 continue;
3870
b76e467d 3871 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3872 {
7b9f3c50 3873 const char *this_name = file_data->file_names[j];
da235a7c 3874 const char *this_real_name;
9291a0cd 3875
af529f8f 3876 if (compare_filenames_for_search (this_name, name))
9291a0cd 3877 {
f5b95b50 3878 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3879 callback))
3880 return true;
288e77a7 3881 continue;
4aac40c8 3882 }
9291a0cd 3883
c011a4f4
DE
3884 /* Before we invoke realpath, which can get expensive when many
3885 files are involved, do a quick comparison of the basenames. */
3886 if (! basenames_may_differ
3887 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3888 continue;
3889
da235a7c
JK
3890 this_real_name = dw2_get_real_path (objfile, file_data, j);
3891 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3892 {
da235a7c 3893 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3894 callback))
3895 return true;
288e77a7 3896 continue;
da235a7c 3897 }
9291a0cd 3898
da235a7c
JK
3899 if (real_path != NULL)
3900 {
af529f8f
JK
3901 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3902 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3903 if (this_real_name != NULL
af529f8f 3904 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3905 {
f5b95b50 3906 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3907 callback))
3908 return true;
288e77a7 3909 continue;
9291a0cd
TT
3910 }
3911 }
3912 }
3913 }
3914
14bc53a8 3915 return false;
9291a0cd
TT
3916}
3917
da51c347
DE
3918/* Struct used to manage iterating over all CUs looking for a symbol. */
3919
3920struct dw2_symtab_iterator
9291a0cd 3921{
ed2dc618
SM
3922 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3923 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3924 /* If set, only look for symbols that match that block. Valid values are
3925 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3926 gdb::optional<block_enum> block_index;
da51c347
DE
3927 /* The kind of symbol we're looking for. */
3928 domain_enum domain;
3929 /* The list of CUs from the index entry of the symbol,
3930 or NULL if not found. */
3931 offset_type *vec;
3932 /* The next element in VEC to look at. */
3933 int next;
3934 /* The number of elements in VEC, or zero if there is no match. */
3935 int length;
8943b874
DE
3936 /* Have we seen a global version of the symbol?
3937 If so we can ignore all further global instances.
3938 This is to work around gold/15646, inefficient gold-generated
3939 indices. */
3940 int global_seen;
da51c347 3941};
9291a0cd 3942
2b79f376 3943/* Initialize the index symtab iterator ITER. */
2fdf6df6 3944
9291a0cd 3945static void
da51c347 3946dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3947 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3948 gdb::optional<block_enum> block_index,
da51c347
DE
3949 domain_enum domain,
3950 const char *name)
3951{
ed2dc618 3952 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3953 iter->block_index = block_index;
3954 iter->domain = domain;
3955 iter->next = 0;
8943b874 3956 iter->global_seen = 0;
da51c347 3957
3063847f 3958 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3959
3960 /* index is NULL if OBJF_READNOW. */
3961 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3962 iter->length = MAYBE_SWAP (*iter->vec);
3963 else
3964 {
3965 iter->vec = NULL;
3966 iter->length = 0;
3967 }
3968}
3969
3970/* Return the next matching CU or NULL if there are no more. */
3971
3972static struct dwarf2_per_cu_data *
3973dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3974{
ed2dc618
SM
3975 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3976
da51c347
DE
3977 for ( ; iter->next < iter->length; ++iter->next)
3978 {
3979 offset_type cu_index_and_attrs =
3980 MAYBE_SWAP (iter->vec[iter->next + 1]);
3981 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3982 gdb_index_symbol_kind symbol_kind =
3983 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3984 /* Only check the symbol attributes if they're present.
3985 Indices prior to version 7 don't record them,
3986 and indices >= 7 may elide them for certain symbols
3987 (gold does this). */
3988 int attrs_valid =
ed2dc618 3989 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3990 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3991
3190f0c6 3992 /* Don't crash on bad data. */
b76e467d 3993 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3994 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3995 {
b98664d3 3996 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3997 " [in module %s]"),
3998 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3999 continue;
4000 }
4001
ff4c9fec 4002 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 4003
da51c347 4004 /* Skip if already read in. */
43f3e411 4005 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4006 continue;
4007
8943b874
DE
4008 /* Check static vs global. */
4009 if (attrs_valid)
4010 {
2b79f376
SM
4011 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4012
4013 if (iter->block_index.has_value ())
4014 {
4015 bool want_static = *iter->block_index == STATIC_BLOCK;
4016
4017 if (is_static != want_static)
4018 continue;
4019 }
4020
8943b874
DE
4021 /* Work around gold/15646. */
4022 if (!is_static && iter->global_seen)
4023 continue;
4024 if (!is_static)
4025 iter->global_seen = 1;
4026 }
da51c347
DE
4027
4028 /* Only check the symbol's kind if it has one. */
4029 if (attrs_valid)
4030 {
4031 switch (iter->domain)
4032 {
4033 case VAR_DOMAIN:
4034 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4035 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4036 /* Some types are also in VAR_DOMAIN. */
4037 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4038 continue;
4039 break;
4040 case STRUCT_DOMAIN:
4041 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4042 continue;
4043 break;
4044 case LABEL_DOMAIN:
4045 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4046 continue;
4047 break;
59c35742
AB
4048 case MODULE_DOMAIN:
4049 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4050 continue;
4051 break;
da51c347
DE
4052 default:
4053 break;
4054 }
4055 }
4056
4057 ++iter->next;
4058 return per_cu;
4059 }
4060
4061 return NULL;
4062}
4063
43f3e411 4064static struct compunit_symtab *
c7f839cb 4065dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4066 const char *name, domain_enum domain)
9291a0cd 4067{
43f3e411 4068 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4069 struct dwarf2_per_objfile *dwarf2_per_objfile
4070 = get_dwarf2_per_objfile (objfile);
9291a0cd 4071
b5ec771e
PA
4072 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4073
ed2dc618
SM
4074 struct dw2_symtab_iterator iter;
4075 struct dwarf2_per_cu_data *per_cu;
da51c347 4076
2b79f376 4077 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4078
ed2dc618
SM
4079 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4080 {
4081 struct symbol *sym, *with_opaque = NULL;
58f0c718 4082 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4083 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4084 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4085
ed2dc618
SM
4086 sym = block_find_symbol (block, name, domain,
4087 block_find_non_opaque_type_preferred,
4088 &with_opaque);
b2e2f908 4089
ed2dc618
SM
4090 /* Some caution must be observed with overloaded functions
4091 and methods, since the index will not contain any overload
4092 information (but NAME might contain it). */
da51c347 4093
ed2dc618
SM
4094 if (sym != NULL
4095 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4096 return stab;
4097 if (with_opaque != NULL
4098 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4099 stab_best = stab;
da51c347 4100
ed2dc618 4101 /* Keep looking through other CUs. */
9291a0cd 4102 }
9291a0cd 4103
da51c347 4104 return stab_best;
9291a0cd
TT
4105}
4106
4107static void
4108dw2_print_stats (struct objfile *objfile)
4109{
ed2dc618
SM
4110 struct dwarf2_per_objfile *dwarf2_per_objfile
4111 = get_dwarf2_per_objfile (objfile);
b76e467d 4112 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4113 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4114 int count = 0;
9291a0cd 4115
ed2dc618 4116 for (int i = 0; i < total; ++i)
9291a0cd 4117 {
ff4c9fec 4118 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4119
43f3e411 4120 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4121 ++count;
4122 }
e4a48d9d 4123 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4124 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4125}
4126
779bd270
DE
4127/* This dumps minimal information about the index.
4128 It is called via "mt print objfiles".
4129 One use is to verify .gdb_index has been loaded by the
4130 gdb.dwarf2/gdb-index.exp testcase. */
4131
9291a0cd
TT
4132static void
4133dw2_dump (struct objfile *objfile)
4134{
ed2dc618
SM
4135 struct dwarf2_per_objfile *dwarf2_per_objfile
4136 = get_dwarf2_per_objfile (objfile);
4137
779bd270
DE
4138 gdb_assert (dwarf2_per_objfile->using_index);
4139 printf_filtered (".gdb_index:");
4140 if (dwarf2_per_objfile->index_table != NULL)
4141 {
4142 printf_filtered (" version %d\n",
4143 dwarf2_per_objfile->index_table->version);
4144 }
4145 else
4146 printf_filtered (" faked for \"readnow\"\n");
4147 printf_filtered ("\n");
9291a0cd
TT
4148}
4149
9291a0cd
TT
4150static void
4151dw2_expand_symtabs_for_function (struct objfile *objfile,
4152 const char *func_name)
4153{
ed2dc618
SM
4154 struct dwarf2_per_objfile *dwarf2_per_objfile
4155 = get_dwarf2_per_objfile (objfile);
da51c347 4156
ed2dc618
SM
4157 struct dw2_symtab_iterator iter;
4158 struct dwarf2_per_cu_data *per_cu;
da51c347 4159
2b79f376 4160 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4161
ed2dc618 4162 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4163 dw2_instantiate_symtab (per_cu, false);
da51c347 4164
9291a0cd
TT
4165}
4166
4167static void
4168dw2_expand_all_symtabs (struct objfile *objfile)
4169{
ed2dc618
SM
4170 struct dwarf2_per_objfile *dwarf2_per_objfile
4171 = get_dwarf2_per_objfile (objfile);
b76e467d 4172 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4173 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4174
ed2dc618 4175 for (int i = 0; i < total_units; ++i)
9291a0cd 4176 {
ff4c9fec 4177 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4178
58f0c718
TT
4179 /* We don't want to directly expand a partial CU, because if we
4180 read it with the wrong language, then assertion failures can
4181 be triggered later on. See PR symtab/23010. So, tell
4182 dw2_instantiate_symtab to skip partial CUs -- any important
4183 partial CU will be read via DW_TAG_imported_unit anyway. */
4184 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4185 }
4186}
4187
4188static void
652a8996
JK
4189dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4190 const char *fullname)
9291a0cd 4191{
ed2dc618
SM
4192 struct dwarf2_per_objfile *dwarf2_per_objfile
4193 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4194
4195 /* We don't need to consider type units here.
4196 This is only called for examining code, e.g. expand_line_sal.
4197 There can be an order of magnitude (or more) more type units
4198 than comp units, and we avoid them if we can. */
4199
b76e467d 4200 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4201 {
3d7bb9d9 4202 /* We only need to look at symtabs not already expanded. */
43f3e411 4203 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4204 continue;
4205
b76e467d 4206 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4207 if (file_data == NULL)
9291a0cd
TT
4208 continue;
4209
b76e467d 4210 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4211 {
652a8996
JK
4212 const char *this_fullname = file_data->file_names[j];
4213
4214 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4215 {
58f0c718 4216 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4217 break;
4218 }
4219 }
4220 }
4221}
4222
9291a0cd 4223static void
199b4314
TT
4224dw2_map_matching_symbols
4225 (struct objfile *objfile,
b054970d 4226 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4227 int global,
4228 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4229 symbol_compare_ftype *ordered_compare)
9291a0cd 4230{
40658b94 4231 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4232 current language is Ada for a non-Ada objfile using GNU index. As Ada
4233 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4234}
4235
e1ef7d7a
PA
4236/* Starting from a search name, return the string that finds the upper
4237 bound of all strings that start with SEARCH_NAME in a sorted name
4238 list. Returns the empty string to indicate that the upper bound is
4239 the end of the list. */
4240
4241static std::string
4242make_sort_after_prefix_name (const char *search_name)
4243{
4244 /* When looking to complete "func", we find the upper bound of all
4245 symbols that start with "func" by looking for where we'd insert
4246 the closest string that would follow "func" in lexicographical
4247 order. Usually, that's "func"-with-last-character-incremented,
4248 i.e. "fund". Mind non-ASCII characters, though. Usually those
4249 will be UTF-8 multi-byte sequences, but we can't be certain.
4250 Especially mind the 0xff character, which is a valid character in
4251 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4252 rule out compilers allowing it in identifiers. Note that
4253 conveniently, strcmp/strcasecmp are specified to compare
4254 characters interpreted as unsigned char. So what we do is treat
4255 the whole string as a base 256 number composed of a sequence of
4256 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4257 to 0, and carries 1 to the following more-significant position.
4258 If the very first character in SEARCH_NAME ends up incremented
4259 and carries/overflows, then the upper bound is the end of the
4260 list. The string after the empty string is also the empty
4261 string.
4262
4263 Some examples of this operation:
4264
4265 SEARCH_NAME => "+1" RESULT
4266
4267 "abc" => "abd"
4268 "ab\xff" => "ac"
4269 "\xff" "a" "\xff" => "\xff" "b"
4270 "\xff" => ""
4271 "\xff\xff" => ""
4272 "" => ""
4273
4274 Then, with these symbols for example:
4275
4276 func
4277 func1
4278 fund
4279
4280 completing "func" looks for symbols between "func" and
4281 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4282 which finds "func" and "func1", but not "fund".
4283
4284 And with:
4285
4286 funcÿ (Latin1 'ÿ' [0xff])
4287 funcÿ1
4288 fund
4289
4290 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4291 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4292
4293 And with:
4294
4295 ÿÿ (Latin1 'ÿ' [0xff])
4296 ÿÿ1
4297
4298 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4299 the end of the list.
4300 */
4301 std::string after = search_name;
4302 while (!after.empty () && (unsigned char) after.back () == 0xff)
4303 after.pop_back ();
4304 if (!after.empty ())
4305 after.back () = (unsigned char) after.back () + 1;
4306 return after;
4307}
4308
5c58de74 4309/* See declaration. */
61d96d7e 4310
5c58de74
PA
4311std::pair<std::vector<name_component>::const_iterator,
4312 std::vector<name_component>::const_iterator>
44ed8f3e 4313mapped_index_base::find_name_components_bounds
3b00ef10 4314 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4315{
5c58de74
PA
4316 auto *name_cmp
4317 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4318
3b00ef10
TT
4319 const char *lang_name
4320 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4321
3f563c84
PA
4322 /* Comparison function object for lower_bound that matches against a
4323 given symbol name. */
4324 auto lookup_compare_lower = [&] (const name_component &elem,
4325 const char *name)
4326 {
5c58de74 4327 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4328 const char *elem_name = elem_qualified + elem.name_offset;
4329 return name_cmp (elem_name, name) < 0;
4330 };
4331
4332 /* Comparison function object for upper_bound that matches against a
4333 given symbol name. */
4334 auto lookup_compare_upper = [&] (const char *name,
4335 const name_component &elem)
4336 {
5c58de74 4337 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4338 const char *elem_name = elem_qualified + elem.name_offset;
4339 return name_cmp (name, elem_name) < 0;
4340 };
4341
5c58de74
PA
4342 auto begin = this->name_components.begin ();
4343 auto end = this->name_components.end ();
3f563c84
PA
4344
4345 /* Find the lower bound. */
4346 auto lower = [&] ()
4347 {
3b00ef10 4348 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4349 return begin;
4350 else
3b00ef10 4351 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4352 } ();
4353
4354 /* Find the upper bound. */
4355 auto upper = [&] ()
4356 {
5c58de74 4357 if (lookup_name_without_params.completion_mode ())
3f563c84 4358 {
e1ef7d7a
PA
4359 /* In completion mode, we want UPPER to point past all
4360 symbols names that have the same prefix. I.e., with
4361 these symbols, and completing "func":
4362
4363 function << lower bound
4364 function1
4365 other_function << upper bound
4366
4367 We find the upper bound by looking for the insertion
4368 point of "func"-with-last-character-incremented,
4369 i.e. "fund". */
3b00ef10 4370 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4371 if (after.empty ())
3f563c84 4372 return end;
e6b2f5ef
PA
4373 return std::lower_bound (lower, end, after.c_str (),
4374 lookup_compare_lower);
3f563c84
PA
4375 }
4376 else
3b00ef10 4377 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4378 } ();
4379
5c58de74
PA
4380 return {lower, upper};
4381}
4382
4383/* See declaration. */
4384
4385void
44ed8f3e 4386mapped_index_base::build_name_components ()
5c58de74
PA
4387{
4388 if (!this->name_components.empty ())
4389 return;
4390
4391 this->name_components_casing = case_sensitivity;
4392 auto *name_cmp
4393 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4394
4395 /* The code below only knows how to break apart components of C++
4396 symbol names (and other languages that use '::' as
3b00ef10 4397 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4398 auto count = this->symbol_name_count ();
4399 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4400 {
44ed8f3e 4401 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4402 continue;
4403
4404 const char *name = this->symbol_name_at (idx);
4405
4406 /* Add each name component to the name component table. */
4407 unsigned int previous_len = 0;
3b00ef10
TT
4408
4409 if (strstr (name, "::") != nullptr)
4410 {
4411 for (unsigned int current_len = cp_find_first_component (name);
4412 name[current_len] != '\0';
4413 current_len += cp_find_first_component (name + current_len))
4414 {
4415 gdb_assert (name[current_len] == ':');
4416 this->name_components.push_back ({previous_len, idx});
4417 /* Skip the '::'. */
4418 current_len += 2;
4419 previous_len = current_len;
4420 }
4421 }
4422 else
5c58de74 4423 {
3b00ef10
TT
4424 /* Handle the Ada encoded (aka mangled) form here. */
4425 for (const char *iter = strstr (name, "__");
4426 iter != nullptr;
4427 iter = strstr (iter, "__"))
4428 {
4429 this->name_components.push_back ({previous_len, idx});
4430 iter += 2;
4431 previous_len = iter - name;
4432 }
5c58de74 4433 }
3b00ef10 4434
5c58de74
PA
4435 this->name_components.push_back ({previous_len, idx});
4436 }
4437
4438 /* Sort name_components elements by name. */
4439 auto name_comp_compare = [&] (const name_component &left,
4440 const name_component &right)
4441 {
4442 const char *left_qualified = this->symbol_name_at (left.idx);
4443 const char *right_qualified = this->symbol_name_at (right.idx);
4444
4445 const char *left_name = left_qualified + left.name_offset;
4446 const char *right_name = right_qualified + right.name_offset;
4447
4448 return name_cmp (left_name, right_name) < 0;
4449 };
4450
4451 std::sort (this->name_components.begin (),
4452 this->name_components.end (),
4453 name_comp_compare);
4454}
4455
4456/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4457 mapped_index_base instead of the containing objfile. This is split
4458 to a separate function in order to be able to unit test the
4459 name_components matching using a mock mapped_index_base. For each
5c58de74 4460 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4461 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4462
4463static void
4464dw2_expand_symtabs_matching_symbol
44ed8f3e 4465 (mapped_index_base &index,
5c58de74
PA
4466 const lookup_name_info &lookup_name_in,
4467 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4468 enum search_domain kind,
3b00ef10 4469 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4470{
4471 lookup_name_info lookup_name_without_params
4472 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4473
4474 /* Build the symbol name component sorted vector, if we haven't
4475 yet. */
4476 index.build_name_components ();
4477
3f563c84
PA
4478 /* The same symbol may appear more than once in the range though.
4479 E.g., if we're looking for symbols that complete "w", and we have
4480 a symbol named "w1::w2", we'll find the two name components for
4481 that same symbol in the range. To be sure we only call the
4482 callback once per symbol, we first collect the symbol name
4483 indexes that matched in a temporary vector and ignore
4484 duplicates. */
4485 std::vector<offset_type> matches;
3f563c84 4486
3b00ef10
TT
4487 struct name_and_matcher
4488 {
4489 symbol_name_matcher_ftype *matcher;
4490 const std::string &name;
4491
4492 bool operator== (const name_and_matcher &other) const
3f563c84 4493 {
3b00ef10
TT
4494 return matcher == other.matcher && name == other.name;
4495 }
4496 };
4497
4498 /* A vector holding all the different symbol name matchers, for all
4499 languages. */
4500 std::vector<name_and_matcher> matchers;
4501
4502 for (int i = 0; i < nr_languages; i++)
4503 {
4504 enum language lang_e = (enum language) i;
4505
4506 const language_defn *lang = language_def (lang_e);
4507 symbol_name_matcher_ftype *name_matcher
4508 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4509
3b00ef10
TT
4510 name_and_matcher key {
4511 name_matcher,
4512 lookup_name_without_params.language_lookup_name (lang_e)
4513 };
4514
4515 /* Don't insert the same comparison routine more than once.
4516 Note that we do this linear walk. This is not a problem in
4517 practice because the number of supported languages is
4518 low. */
4519 if (std::find (matchers.begin (), matchers.end (), key)
4520 != matchers.end ())
9291a0cd 4521 continue;
3b00ef10
TT
4522 matchers.push_back (std::move (key));
4523
4524 auto bounds
4525 = index.find_name_components_bounds (lookup_name_without_params,
4526 lang_e);
4527
4528 /* Now for each symbol name in range, check to see if we have a name
4529 match, and if so, call the MATCH_CALLBACK callback. */
4530
4531 for (; bounds.first != bounds.second; ++bounds.first)
4532 {
4533 const char *qualified = index.symbol_name_at (bounds.first->idx);
4534
4535 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4536 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4537 continue;
9291a0cd 4538
3b00ef10
TT
4539 matches.push_back (bounds.first->idx);
4540 }
3f563c84
PA
4541 }
4542
4543 std::sort (matches.begin (), matches.end ());
4544
4545 /* Finally call the callback, once per match. */
4546 ULONGEST prev = -1;
4547 for (offset_type idx : matches)
4548 {
4549 if (prev != idx)
4550 {
3b00ef10
TT
4551 if (!match_callback (idx))
4552 break;
3f563c84
PA
4553 prev = idx;
4554 }
4555 }
4556
4557 /* Above we use a type wider than idx's for 'prev', since 0 and
4558 (offset_type)-1 are both possible values. */
4559 static_assert (sizeof (prev) > sizeof (offset_type), "");
4560}
4561
c62446b1
PA
4562#if GDB_SELF_TEST
4563
4564namespace selftests { namespace dw2_expand_symtabs_matching {
4565
a3c5fafd
PA
4566/* A mock .gdb_index/.debug_names-like name index table, enough to
4567 exercise dw2_expand_symtabs_matching_symbol, which works with the
4568 mapped_index_base interface. Builds an index from the symbol list
4569 passed as parameter to the constructor. */
4570class mock_mapped_index : public mapped_index_base
c62446b1
PA
4571{
4572public:
a3c5fafd
PA
4573 mock_mapped_index (gdb::array_view<const char *> symbols)
4574 : m_symbol_table (symbols)
c62446b1
PA
4575 {}
4576
a3c5fafd 4577 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4578
a3c5fafd 4579 /* Return the number of names in the symbol table. */
632e107b 4580 size_t symbol_name_count () const override
c62446b1 4581 {
a3c5fafd 4582 return m_symbol_table.size ();
c62446b1
PA
4583 }
4584
a3c5fafd 4585 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4586 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4587 {
4588 return m_symbol_table[idx];
4589 }
c62446b1 4590
a3c5fafd
PA
4591private:
4592 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4593};
4594
4595/* Convenience function that converts a NULL pointer to a "<null>"
4596 string, to pass to print routines. */
4597
4598static const char *
4599string_or_null (const char *str)
4600{
4601 return str != NULL ? str : "<null>";
4602}
4603
4604/* Check if a lookup_name_info built from
4605 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4606 index. EXPECTED_LIST is the list of expected matches, in expected
4607 matching order. If no match expected, then an empty list is
4608 specified. Returns true on success. On failure prints a warning
4609 indicating the file:line that failed, and returns false. */
4610
4611static bool
4612check_match (const char *file, int line,
4613 mock_mapped_index &mock_index,
4614 const char *name, symbol_name_match_type match_type,
4615 bool completion_mode,
4616 std::initializer_list<const char *> expected_list)
4617{
4618 lookup_name_info lookup_name (name, match_type, completion_mode);
4619
4620 bool matched = true;
4621
4622 auto mismatch = [&] (const char *expected_str,
4623 const char *got)
4624 {
4625 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4626 "expected=\"%s\", got=\"%s\"\n"),
4627 file, line,
4628 (match_type == symbol_name_match_type::FULL
4629 ? "FULL" : "WILD"),
4630 name, string_or_null (expected_str), string_or_null (got));
4631 matched = false;
4632 };
4633
4634 auto expected_it = expected_list.begin ();
4635 auto expected_end = expected_list.end ();
4636
a3c5fafd 4637 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4638 NULL, ALL_DOMAIN,
4639 [&] (offset_type idx)
4640 {
a3c5fafd 4641 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4642 const char *expected_str
4643 = expected_it == expected_end ? NULL : *expected_it++;
4644
4645 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4646 mismatch (expected_str, matched_name);
3b00ef10 4647 return true;
c62446b1
PA
4648 });
4649
4650 const char *expected_str
4651 = expected_it == expected_end ? NULL : *expected_it++;
4652 if (expected_str != NULL)
4653 mismatch (expected_str, NULL);
4654
4655 return matched;
4656}
4657
4658/* The symbols added to the mock mapped_index for testing (in
4659 canonical form). */
4660static const char *test_symbols[] = {
4661 "function",
4662 "std::bar",
4663 "std::zfunction",
4664 "std::zfunction2",
4665 "w1::w2",
4666 "ns::foo<char*>",
4667 "ns::foo<int>",
4668 "ns::foo<long>",
a20714ff
PA
4669 "ns2::tmpl<int>::foo2",
4670 "(anonymous namespace)::A::B::C",
c62446b1 4671
e1ef7d7a
PA
4672 /* These are used to check that the increment-last-char in the
4673 matching algorithm for completion doesn't match "t1_fund" when
4674 completing "t1_func". */
4675 "t1_func",
4676 "t1_func1",
4677 "t1_fund",
4678 "t1_fund1",
4679
4680 /* A UTF-8 name with multi-byte sequences to make sure that
4681 cp-name-parser understands this as a single identifier ("função"
4682 is "function" in PT). */
4683 u8"u8função",
4684
4685 /* \377 (0xff) is Latin1 'ÿ'. */
4686 "yfunc\377",
4687
4688 /* \377 (0xff) is Latin1 'ÿ'. */
4689 "\377",
4690 "\377\377123",
4691
c62446b1
PA
4692 /* A name with all sorts of complications. Starts with "z" to make
4693 it easier for the completion tests below. */
4694#define Z_SYM_NAME \
4695 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4696 "::tuple<(anonymous namespace)::ui*, " \
4697 "std::default_delete<(anonymous namespace)::ui>, void>"
4698
4699 Z_SYM_NAME
4700};
4701
a3c5fafd
PA
4702/* Returns true if the mapped_index_base::find_name_component_bounds
4703 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4704 in completion mode. */
5c58de74
PA
4705
4706static bool
a3c5fafd 4707check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4708 const char *search_name,
4709 gdb::array_view<const char *> expected_syms)
4710{
4711 lookup_name_info lookup_name (search_name,
4712 symbol_name_match_type::FULL, true);
4713
3b00ef10
TT
4714 auto bounds = index.find_name_components_bounds (lookup_name,
4715 language_cplus);
5c58de74
PA
4716
4717 size_t distance = std::distance (bounds.first, bounds.second);
4718 if (distance != expected_syms.size ())
4719 return false;
4720
4721 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4722 {
4723 auto nc_elem = bounds.first + exp_elem;
4724 const char *qualified = index.symbol_name_at (nc_elem->idx);
4725 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4726 return false;
4727 }
4728
4729 return true;
4730}
4731
4732/* Test the lower-level mapped_index::find_name_component_bounds
4733 method. */
4734
c62446b1 4735static void
5c58de74
PA
4736test_mapped_index_find_name_component_bounds ()
4737{
4738 mock_mapped_index mock_index (test_symbols);
4739
a3c5fafd 4740 mock_index.build_name_components ();
5c58de74
PA
4741
4742 /* Test the lower-level mapped_index::find_name_component_bounds
4743 method in completion mode. */
4744 {
4745 static const char *expected_syms[] = {
4746 "t1_func",
4747 "t1_func1",
5c58de74
PA
4748 };
4749
a3c5fafd 4750 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4751 "t1_func", expected_syms));
4752 }
4753
4754 /* Check that the increment-last-char in the name matching algorithm
4755 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4756 {
4757 static const char *expected_syms1[] = {
4758 "\377",
4759 "\377\377123",
4760 };
a3c5fafd 4761 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4762 "\377", expected_syms1));
4763
4764 static const char *expected_syms2[] = {
4765 "\377\377123",
4766 };
a3c5fafd 4767 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4768 "\377\377", expected_syms2));
4769 }
4770}
4771
4772/* Test dw2_expand_symtabs_matching_symbol. */
4773
4774static void
4775test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4776{
4777 mock_mapped_index mock_index (test_symbols);
4778
4779 /* We let all tests run until the end even if some fails, for debug
4780 convenience. */
4781 bool any_mismatch = false;
4782
4783 /* Create the expected symbols list (an initializer_list). Needed
4784 because lists have commas, and we need to pass them to CHECK,
4785 which is a macro. */
4786#define EXPECT(...) { __VA_ARGS__ }
4787
4788 /* Wrapper for check_match that passes down the current
4789 __FILE__/__LINE__. */
4790#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4791 any_mismatch |= !check_match (__FILE__, __LINE__, \
4792 mock_index, \
4793 NAME, MATCH_TYPE, COMPLETION_MODE, \
4794 EXPECTED_LIST)
4795
4796 /* Identity checks. */
4797 for (const char *sym : test_symbols)
4798 {
4799 /* Should be able to match all existing symbols. */
4800 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4801 EXPECT (sym));
4802
4803 /* Should be able to match all existing symbols with
4804 parameters. */
4805 std::string with_params = std::string (sym) + "(int)";
4806 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4807 EXPECT (sym));
4808
4809 /* Should be able to match all existing symbols with
4810 parameters and qualifiers. */
4811 with_params = std::string (sym) + " ( int ) const";
4812 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4813 EXPECT (sym));
4814
4815 /* This should really find sym, but cp-name-parser.y doesn't
4816 know about lvalue/rvalue qualifiers yet. */
4817 with_params = std::string (sym) + " ( int ) &&";
4818 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4819 {});
4820 }
4821
e1ef7d7a
PA
4822 /* Check that the name matching algorithm for completion doesn't get
4823 confused with Latin1 'ÿ' / 0xff. */
4824 {
4825 static const char str[] = "\377";
4826 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4827 EXPECT ("\377", "\377\377123"));
4828 }
4829
4830 /* Check that the increment-last-char in the matching algorithm for
4831 completion doesn't match "t1_fund" when completing "t1_func". */
4832 {
4833 static const char str[] = "t1_func";
4834 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4835 EXPECT ("t1_func", "t1_func1"));
4836 }
4837
c62446b1
PA
4838 /* Check that completion mode works at each prefix of the expected
4839 symbol name. */
4840 {
4841 static const char str[] = "function(int)";
4842 size_t len = strlen (str);
4843 std::string lookup;
4844
4845 for (size_t i = 1; i < len; i++)
4846 {
4847 lookup.assign (str, i);
4848 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4849 EXPECT ("function"));
4850 }
4851 }
4852
4853 /* While "w" is a prefix of both components, the match function
4854 should still only be called once. */
4855 {
4856 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4857 EXPECT ("w1::w2"));
a20714ff
PA
4858 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4859 EXPECT ("w1::w2"));
c62446b1
PA
4860 }
4861
4862 /* Same, with a "complicated" symbol. */
4863 {
4864 static const char str[] = Z_SYM_NAME;
4865 size_t len = strlen (str);
4866 std::string lookup;
4867
4868 for (size_t i = 1; i < len; i++)
4869 {
4870 lookup.assign (str, i);
4871 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4872 EXPECT (Z_SYM_NAME));
4873 }
4874 }
4875
4876 /* In FULL mode, an incomplete symbol doesn't match. */
4877 {
4878 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4879 {});
4880 }
4881
4882 /* A complete symbol with parameters matches any overload, since the
4883 index has no overload info. */
4884 {
4885 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4886 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4887 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4888 EXPECT ("std::zfunction", "std::zfunction2"));
4889 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4890 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4891 }
4892
4893 /* Check that whitespace is ignored appropriately. A symbol with a
4894 template argument list. */
4895 {
4896 static const char expected[] = "ns::foo<int>";
4897 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4898 EXPECT (expected));
a20714ff
PA
4899 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4900 EXPECT (expected));
c62446b1
PA
4901 }
4902
4903 /* Check that whitespace is ignored appropriately. A symbol with a
4904 template argument list that includes a pointer. */
4905 {
4906 static const char expected[] = "ns::foo<char*>";
4907 /* Try both completion and non-completion modes. */
4908 static const bool completion_mode[2] = {false, true};
4909 for (size_t i = 0; i < 2; i++)
4910 {
4911 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4912 completion_mode[i], EXPECT (expected));
a20714ff
PA
4913 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4914 completion_mode[i], EXPECT (expected));
c62446b1
PA
4915
4916 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4917 completion_mode[i], EXPECT (expected));
a20714ff
PA
4918 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4919 completion_mode[i], EXPECT (expected));
c62446b1
PA
4920 }
4921 }
4922
4923 {
4924 /* Check method qualifiers are ignored. */
4925 static const char expected[] = "ns::foo<char*>";
4926 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4927 symbol_name_match_type::FULL, true, EXPECT (expected));
4928 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4929 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4930 CHECK_MATCH ("foo < char * > ( int ) const",
4931 symbol_name_match_type::WILD, true, EXPECT (expected));
4932 CHECK_MATCH ("foo < char * > ( int ) &&",
4933 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4934 }
4935
4936 /* Test lookup names that don't match anything. */
4937 {
a20714ff
PA
4938 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4939 {});
4940
c62446b1
PA
4941 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4942 {});
4943 }
4944
a20714ff
PA
4945 /* Some wild matching tests, exercising "(anonymous namespace)",
4946 which should not be confused with a parameter list. */
4947 {
4948 static const char *syms[] = {
4949 "A::B::C",
4950 "B::C",
4951 "C",
4952 "A :: B :: C ( int )",
4953 "B :: C ( int )",
4954 "C ( int )",
4955 };
4956
4957 for (const char *s : syms)
4958 {
4959 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4960 EXPECT ("(anonymous namespace)::A::B::C"));
4961 }
4962 }
4963
4964 {
4965 static const char expected[] = "ns2::tmpl<int>::foo2";
4966 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4967 EXPECT (expected));
4968 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4969 EXPECT (expected));
4970 }
4971
c62446b1
PA
4972 SELF_CHECK (!any_mismatch);
4973
4974#undef EXPECT
4975#undef CHECK_MATCH
4976}
4977
5c58de74
PA
4978static void
4979run_test ()
4980{
4981 test_mapped_index_find_name_component_bounds ();
4982 test_dw2_expand_symtabs_matching_symbol ();
4983}
4984
c62446b1
PA
4985}} // namespace selftests::dw2_expand_symtabs_matching
4986
4987#endif /* GDB_SELF_TEST */
4988
4b514bc8
JK
4989/* If FILE_MATCHER is NULL or if PER_CU has
4990 dwarf2_per_cu_quick_data::MARK set (see
4991 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4992 EXPANSION_NOTIFY on it. */
4993
4994static void
4995dw2_expand_symtabs_matching_one
4996 (struct dwarf2_per_cu_data *per_cu,
4997 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4998 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4999{
5000 if (file_matcher == NULL || per_cu->v.quick->mark)
5001 {
5002 bool symtab_was_null
5003 = (per_cu->v.quick->compunit_symtab == NULL);
5004
58f0c718 5005 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5006
5007 if (expansion_notify != NULL
5008 && symtab_was_null
5009 && per_cu->v.quick->compunit_symtab != NULL)
5010 expansion_notify (per_cu->v.quick->compunit_symtab);
5011 }
5012}
5013
3f563c84
PA
5014/* Helper for dw2_expand_matching symtabs. Called on each symbol
5015 matched, to expand corresponding CUs that were marked. IDX is the
5016 index of the symbol name that matched. */
5017
5018static void
5019dw2_expand_marked_cus
ed2dc618 5020 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5021 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5022 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5023 search_domain kind)
5024{
3f563c84
PA
5025 offset_type *vec, vec_len, vec_idx;
5026 bool global_seen = false;
ed2dc618 5027 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5028
61920122 5029 vec = (offset_type *) (index.constant_pool
f00a2de2 5030 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5031 vec_len = MAYBE_SWAP (vec[0]);
5032 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5033 {
61920122
PA
5034 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5035 /* This value is only valid for index versions >= 7. */
5036 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5037 gdb_index_symbol_kind symbol_kind =
5038 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5039 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5040 /* Only check the symbol attributes if they're present.
5041 Indices prior to version 7 don't record them,
5042 and indices >= 7 may elide them for certain symbols
5043 (gold does this). */
5044 int attrs_valid =
5045 (index.version >= 7
5046 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5047
5048 /* Work around gold/15646. */
5049 if (attrs_valid)
9291a0cd 5050 {
61920122
PA
5051 if (!is_static && global_seen)
5052 continue;
5053 if (!is_static)
5054 global_seen = true;
5055 }
3190f0c6 5056
61920122
PA
5057 /* Only check the symbol's kind if it has one. */
5058 if (attrs_valid)
5059 {
5060 switch (kind)
8943b874 5061 {
61920122
PA
5062 case VARIABLES_DOMAIN:
5063 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5064 continue;
5065 break;
5066 case FUNCTIONS_DOMAIN:
5067 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5068 continue;
61920122
PA
5069 break;
5070 case TYPES_DOMAIN:
5071 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5072 continue;
5073 break;
59c35742
AB
5074 case MODULES_DOMAIN:
5075 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5076 continue;
5077 break;
61920122
PA
5078 default:
5079 break;
8943b874 5080 }
61920122 5081 }
8943b874 5082
61920122 5083 /* Don't crash on bad data. */
b76e467d 5084 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5085 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5086 {
b98664d3 5087 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5088 " [in module %s]"),
5089 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5090 continue;
5091 }
5092
ff4c9fec 5093 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5094 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5095 expansion_notify);
61920122
PA
5096 }
5097}
5098
4b514bc8
JK
5099/* If FILE_MATCHER is non-NULL, set all the
5100 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5101 that match FILE_MATCHER. */
5102
61920122 5103static void
4b514bc8 5104dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5105 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5106 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5107{
4b514bc8 5108 if (file_matcher == NULL)
61920122
PA
5109 return;
5110
4b514bc8
JK
5111 objfile *const objfile = dwarf2_per_objfile->objfile;
5112
5113 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5114 htab_eq_pointer,
5115 NULL, xcalloc, xfree));
5116 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5117 htab_eq_pointer,
5118 NULL, xcalloc, xfree));
61920122 5119
4b514bc8
JK
5120 /* The rule is CUs specify all the files, including those used by
5121 any TU, so there's no need to scan TUs here. */
61920122 5122
b76e467d 5123 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5124 {
927aa2e7
JK
5125 QUIT;
5126
5127 per_cu->v.quick->mark = 0;
5128
5129 /* We only need to look at symtabs not already expanded. */
5130 if (per_cu->v.quick->compunit_symtab)
5131 continue;
5132
b76e467d 5133 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5134 if (file_data == NULL)
5135 continue;
5136
5137 if (htab_find (visited_not_found.get (), file_data) != NULL)
5138 continue;
5139 else if (htab_find (visited_found.get (), file_data) != NULL)
5140 {
5141 per_cu->v.quick->mark = 1;
5142 continue;
5143 }
5144
b76e467d 5145 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5146 {
5147 const char *this_real_name;
5148
5149 if (file_matcher (file_data->file_names[j], false))
5150 {
5151 per_cu->v.quick->mark = 1;
5152 break;
5153 }
5154
5155 /* Before we invoke realpath, which can get expensive when many
5156 files are involved, do a quick comparison of the basenames. */
5157 if (!basenames_may_differ
5158 && !file_matcher (lbasename (file_data->file_names[j]),
5159 true))
5160 continue;
5161
5162 this_real_name = dw2_get_real_path (objfile, file_data, j);
5163 if (file_matcher (this_real_name, false))
5164 {
5165 per_cu->v.quick->mark = 1;
5166 break;
5167 }
5168 }
5169
b76e467d
SM
5170 void **slot = htab_find_slot (per_cu->v.quick->mark
5171 ? visited_found.get ()
5172 : visited_not_found.get (),
5173 file_data, INSERT);
927aa2e7
JK
5174 *slot = file_data;
5175 }
5176}
5177
5178static void
5179dw2_expand_symtabs_matching
5180 (struct objfile *objfile,
5181 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5182 const lookup_name_info &lookup_name,
5183 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5184 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5185 enum search_domain kind)
5186{
ed2dc618
SM
5187 struct dwarf2_per_objfile *dwarf2_per_objfile
5188 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5189
5190 /* index_table is NULL if OBJF_READNOW. */
5191 if (!dwarf2_per_objfile->index_table)
5192 return;
5193
ed2dc618 5194 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5195
5196 mapped_index &index = *dwarf2_per_objfile->index_table;
5197
5198 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5199 symbol_matcher,
5200 kind, [&] (offset_type idx)
5201 {
ed2dc618 5202 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5203 expansion_notify, kind);
3b00ef10 5204 return true;
927aa2e7
JK
5205 });
5206}
5207
5208/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5209 symtab. */
5210
5211static struct compunit_symtab *
5212recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5213 CORE_ADDR pc)
5214{
5215 int i;
5216
5217 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5218 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5219 return cust;
5220
5221 if (cust->includes == NULL)
5222 return NULL;
5223
5224 for (i = 0; cust->includes[i]; ++i)
5225 {
5226 struct compunit_symtab *s = cust->includes[i];
5227
5228 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5229 if (s != NULL)
5230 return s;
5231 }
5232
5233 return NULL;
5234}
5235
5236static struct compunit_symtab *
5237dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5238 struct bound_minimal_symbol msymbol,
5239 CORE_ADDR pc,
5240 struct obj_section *section,
5241 int warn_if_readin)
5242{
5243 struct dwarf2_per_cu_data *data;
5244 struct compunit_symtab *result;
5245
d320c2b5 5246 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5247 return NULL;
5248
79748972
TT
5249 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5250 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5251 data = (struct dwarf2_per_cu_data *) addrmap_find
5252 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5253 if (!data)
5254 return NULL;
5255
5256 if (warn_if_readin && data->v.quick->compunit_symtab)
5257 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5258 paddress (get_objfile_arch (objfile), pc));
5259
5260 result
58f0c718
TT
5261 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5262 false),
927aa2e7
JK
5263 pc);
5264 gdb_assert (result != NULL);
5265 return result;
5266}
5267
5268static void
5269dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5270 void *data, int need_fullname)
5271{
ed2dc618
SM
5272 struct dwarf2_per_objfile *dwarf2_per_objfile
5273 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5274
5275 if (!dwarf2_per_objfile->filenames_cache)
5276 {
5277 dwarf2_per_objfile->filenames_cache.emplace ();
5278
5279 htab_up visited (htab_create_alloc (10,
5280 htab_hash_pointer, htab_eq_pointer,
5281 NULL, xcalloc, xfree));
5282
5283 /* The rule is CUs specify all the files, including those used
5284 by any TU, so there's no need to scan TUs here. We can
5285 ignore file names coming from already-expanded CUs. */
5286
b76e467d 5287 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5288 {
927aa2e7
JK
5289 if (per_cu->v.quick->compunit_symtab)
5290 {
5291 void **slot = htab_find_slot (visited.get (),
5292 per_cu->v.quick->file_names,
5293 INSERT);
5294
5295 *slot = per_cu->v.quick->file_names;
5296 }
5297 }
5298
b76e467d 5299 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5300 {
927aa2e7
JK
5301 /* We only need to look at symtabs not already expanded. */
5302 if (per_cu->v.quick->compunit_symtab)
5303 continue;
5304
b76e467d 5305 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5306 if (file_data == NULL)
5307 continue;
5308
b76e467d 5309 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5310 if (*slot)
5311 {
5312 /* Already visited. */
5313 continue;
5314 }
5315 *slot = file_data;
5316
5317 for (int j = 0; j < file_data->num_file_names; ++j)
5318 {
5319 const char *filename = file_data->file_names[j];
5320 dwarf2_per_objfile->filenames_cache->seen (filename);
5321 }
5322 }
5323 }
5324
5325 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5326 {
5327 gdb::unique_xmalloc_ptr<char> this_real_name;
5328
5329 if (need_fullname)
5330 this_real_name = gdb_realpath (filename);
5331 (*fun) (filename, this_real_name.get (), data);
5332 });
5333}
5334
5335static int
5336dw2_has_symbols (struct objfile *objfile)
5337{
5338 return 1;
5339}
5340
5341const struct quick_symbol_functions dwarf2_gdb_index_functions =
5342{
5343 dw2_has_symbols,
5344 dw2_find_last_source_symtab,
5345 dw2_forget_cached_source_info,
5346 dw2_map_symtabs_matching_filename,
5347 dw2_lookup_symbol,
5348 dw2_print_stats,
5349 dw2_dump,
927aa2e7
JK
5350 dw2_expand_symtabs_for_function,
5351 dw2_expand_all_symtabs,
5352 dw2_expand_symtabs_with_fullname,
5353 dw2_map_matching_symbols,
5354 dw2_expand_symtabs_matching,
5355 dw2_find_pc_sect_compunit_symtab,
5356 NULL,
5357 dw2_map_symbol_filenames
5358};
5359
5360/* DWARF-5 debug_names reader. */
5361
5362/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5363static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5364
5365/* A helper function that reads the .debug_names section in SECTION
5366 and fills in MAP. FILENAME is the name of the file containing the
5367 section; it is used for error reporting.
5368
5369 Returns true if all went well, false otherwise. */
5370
5371static bool
5372read_debug_names_from_section (struct objfile *objfile,
5373 const char *filename,
5374 struct dwarf2_section_info *section,
5375 mapped_debug_names &map)
5376{
5377 if (dwarf2_section_empty_p (section))
5378 return false;
5379
5380 /* Older elfutils strip versions could keep the section in the main
5381 executable while splitting it for the separate debug info file. */
5382 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5383 return false;
5384
5385 dwarf2_read_section (objfile, section);
5386
5387 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5388
5389 const gdb_byte *addr = section->buffer;
5390
5391 bfd *const abfd = get_section_bfd_owner (section);
5392
5393 unsigned int bytes_read;
5394 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5395 addr += bytes_read;
5396
5397 map.dwarf5_is_dwarf64 = bytes_read != 4;
5398 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5399 if (bytes_read + length != section->size)
5400 {
5401 /* There may be multiple per-CU indices. */
5402 warning (_("Section .debug_names in %s length %s does not match "
5403 "section length %s, ignoring .debug_names."),
5404 filename, plongest (bytes_read + length),
5405 pulongest (section->size));
5406 return false;
5407 }
5408
5409 /* The version number. */
5410 uint16_t version = read_2_bytes (abfd, addr);
5411 addr += 2;
5412 if (version != 5)
5413 {
5414 warning (_("Section .debug_names in %s has unsupported version %d, "
5415 "ignoring .debug_names."),
5416 filename, version);
5417 return false;
5418 }
5419
5420 /* Padding. */
5421 uint16_t padding = read_2_bytes (abfd, addr);
5422 addr += 2;
5423 if (padding != 0)
5424 {
5425 warning (_("Section .debug_names in %s has unsupported padding %d, "
5426 "ignoring .debug_names."),
5427 filename, padding);
5428 return false;
5429 }
5430
5431 /* comp_unit_count - The number of CUs in the CU list. */
5432 map.cu_count = read_4_bytes (abfd, addr);
5433 addr += 4;
5434
5435 /* local_type_unit_count - The number of TUs in the local TU
5436 list. */
5437 map.tu_count = read_4_bytes (abfd, addr);
5438 addr += 4;
5439
5440 /* foreign_type_unit_count - The number of TUs in the foreign TU
5441 list. */
5442 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5443 addr += 4;
5444 if (foreign_tu_count != 0)
5445 {
5446 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5447 "ignoring .debug_names."),
5448 filename, static_cast<unsigned long> (foreign_tu_count));
5449 return false;
5450 }
5451
5452 /* bucket_count - The number of hash buckets in the hash lookup
5453 table. */
5454 map.bucket_count = read_4_bytes (abfd, addr);
5455 addr += 4;
5456
5457 /* name_count - The number of unique names in the index. */
5458 map.name_count = read_4_bytes (abfd, addr);
5459 addr += 4;
5460
5461 /* abbrev_table_size - The size in bytes of the abbreviations
5462 table. */
5463 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5464 addr += 4;
5465
5466 /* augmentation_string_size - The size in bytes of the augmentation
5467 string. This value is rounded up to a multiple of 4. */
5468 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5469 addr += 4;
5470 map.augmentation_is_gdb = ((augmentation_string_size
5471 == sizeof (dwarf5_augmentation))
5472 && memcmp (addr, dwarf5_augmentation,
5473 sizeof (dwarf5_augmentation)) == 0);
5474 augmentation_string_size += (-augmentation_string_size) & 3;
5475 addr += augmentation_string_size;
5476
5477 /* List of CUs */
5478 map.cu_table_reordered = addr;
5479 addr += map.cu_count * map.offset_size;
5480
5481 /* List of Local TUs */
5482 map.tu_table_reordered = addr;
5483 addr += map.tu_count * map.offset_size;
5484
5485 /* Hash Lookup Table */
5486 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5487 addr += map.bucket_count * 4;
5488 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5489 addr += map.name_count * 4;
5490
5491 /* Name Table */
5492 map.name_table_string_offs_reordered = addr;
5493 addr += map.name_count * map.offset_size;
5494 map.name_table_entry_offs_reordered = addr;
5495 addr += map.name_count * map.offset_size;
5496
5497 const gdb_byte *abbrev_table_start = addr;
5498 for (;;)
5499 {
927aa2e7
JK
5500 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5501 addr += bytes_read;
5502 if (index_num == 0)
5503 break;
5504
5505 const auto insertpair
5506 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5507 if (!insertpair.second)
5508 {
5509 warning (_("Section .debug_names in %s has duplicate index %s, "
5510 "ignoring .debug_names."),
5511 filename, pulongest (index_num));
5512 return false;
5513 }
5514 mapped_debug_names::index_val &indexval = insertpair.first->second;
5515 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5516 addr += bytes_read;
5517
5518 for (;;)
5519 {
5520 mapped_debug_names::index_val::attr attr;
5521 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5522 addr += bytes_read;
5523 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5524 addr += bytes_read;
5525 if (attr.form == DW_FORM_implicit_const)
5526 {
5527 attr.implicit_const = read_signed_leb128 (abfd, addr,
5528 &bytes_read);
5529 addr += bytes_read;
5530 }
5531 if (attr.dw_idx == 0 && attr.form == 0)
5532 break;
5533 indexval.attr_vec.push_back (std::move (attr));
5534 }
5535 }
5536 if (addr != abbrev_table_start + abbrev_table_size)
5537 {
5538 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5539 "of size %s vs. written as %u, ignoring .debug_names."),
5540 filename, plongest (addr - abbrev_table_start),
5541 abbrev_table_size);
927aa2e7
JK
5542 return false;
5543 }
5544 map.entry_pool = addr;
5545
5546 return true;
5547}
5548
5549/* A helper for create_cus_from_debug_names that handles the MAP's CU
5550 list. */
5551
5552static void
ed2dc618 5553create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5554 const mapped_debug_names &map,
5555 dwarf2_section_info &section,
b76e467d 5556 bool is_dwz)
927aa2e7
JK
5557{
5558 sect_offset sect_off_prev;
5559 for (uint32_t i = 0; i <= map.cu_count; ++i)
5560 {
5561 sect_offset sect_off_next;
5562 if (i < map.cu_count)
5563 {
5564 sect_off_next
5565 = (sect_offset) (extract_unsigned_integer
5566 (map.cu_table_reordered + i * map.offset_size,
5567 map.offset_size,
5568 map.dwarf5_byte_order));
5569 }
5570 else
5571 sect_off_next = (sect_offset) section.size;
5572 if (i >= 1)
5573 {
5574 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5575 dwarf2_per_cu_data *per_cu
ed2dc618 5576 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5577 sect_off_prev, length);
b76e467d 5578 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5579 }
5580 sect_off_prev = sect_off_next;
5581 }
5582}
5583
5584/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5585 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5586
5587static void
ed2dc618 5588create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5589 const mapped_debug_names &map,
5590 const mapped_debug_names &dwz_map)
5591{
b76e467d
SM
5592 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5593 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5594
ed2dc618
SM
5595 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5596 dwarf2_per_objfile->info,
b76e467d 5597 false /* is_dwz */);
927aa2e7
JK
5598
5599 if (dwz_map.cu_count == 0)
5600 return;
5601
ed2dc618
SM
5602 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5603 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5604 true /* is_dwz */);
927aa2e7
JK
5605}
5606
5607/* Read .debug_names. If everything went ok, initialize the "quick"
5608 elements of all the CUs and return true. Otherwise, return false. */
5609
5610static bool
ed2dc618 5611dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5612{
22ca247e
TT
5613 std::unique_ptr<mapped_debug_names> map
5614 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5615 mapped_debug_names dwz_map (dwarf2_per_objfile);
5616 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5617
5618 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5619 &dwarf2_per_objfile->debug_names,
22ca247e 5620 *map))
927aa2e7
JK
5621 return false;
5622
5623 /* Don't use the index if it's empty. */
22ca247e 5624 if (map->name_count == 0)
927aa2e7
JK
5625 return false;
5626
5627 /* If there is a .dwz file, read it so we can get its CU list as
5628 well. */
ed2dc618 5629 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5630 if (dwz != NULL)
5631 {
5632 if (!read_debug_names_from_section (objfile,
00f93c44 5633 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5634 &dwz->debug_names, dwz_map))
5635 {
5636 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5637 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5638 return false;
5639 }
5640 }
5641
22ca247e 5642 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5643
22ca247e 5644 if (map->tu_count != 0)
927aa2e7
JK
5645 {
5646 /* We can only handle a single .debug_types when we have an
5647 index. */
fd5866f6 5648 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5649 return false;
5650
fd5866f6 5651 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5652
5653 create_signatured_type_table_from_debug_names
22ca247e 5654 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5655 }
5656
ed2dc618
SM
5657 create_addrmap_from_aranges (dwarf2_per_objfile,
5658 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5659
22ca247e 5660 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5661 dwarf2_per_objfile->using_index = 1;
5662 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5663 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5664
5665 return true;
5666}
5667
927aa2e7
JK
5668/* Type used to manage iterating over all CUs looking for a symbol for
5669 .debug_names. */
5670
5671class dw2_debug_names_iterator
5672{
5673public:
927aa2e7 5674 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5675 gdb::optional<block_enum> block_index,
5676 domain_enum domain,
927aa2e7 5677 const char *name)
2b79f376 5678 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5679 m_addr (find_vec_in_debug_names (map, name))
5680 {}
5681
5682 dw2_debug_names_iterator (const mapped_debug_names &map,
5683 search_domain search, uint32_t namei)
5684 : m_map (map),
5685 m_search (search),
5686 m_addr (find_vec_in_debug_names (map, namei))
5687 {}
5688
3b00ef10
TT
5689 dw2_debug_names_iterator (const mapped_debug_names &map,
5690 block_enum block_index, domain_enum domain,
5691 uint32_t namei)
5692 : m_map (map), m_block_index (block_index), m_domain (domain),
5693 m_addr (find_vec_in_debug_names (map, namei))
5694 {}
5695
927aa2e7
JK
5696 /* Return the next matching CU or NULL if there are no more. */
5697 dwarf2_per_cu_data *next ();
5698
5699private:
5700 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5701 const char *name);
5702 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5703 uint32_t namei);
5704
5705 /* The internalized form of .debug_names. */
5706 const mapped_debug_names &m_map;
5707
2b79f376
SM
5708 /* If set, only look for symbols that match that block. Valid values are
5709 GLOBAL_BLOCK and STATIC_BLOCK. */
5710 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5711
5712 /* The kind of symbol we're looking for. */
5713 const domain_enum m_domain = UNDEF_DOMAIN;
5714 const search_domain m_search = ALL_DOMAIN;
5715
5716 /* The list of CUs from the index entry of the symbol, or NULL if
5717 not found. */
5718 const gdb_byte *m_addr;
5719};
5720
5721const char *
5722mapped_debug_names::namei_to_name (uint32_t namei) const
5723{
5724 const ULONGEST namei_string_offs
5725 = extract_unsigned_integer ((name_table_string_offs_reordered
5726 + namei * offset_size),
5727 offset_size,
5728 dwarf5_byte_order);
5729 return read_indirect_string_at_offset
ed2dc618 5730 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5731}
5732
5733/* Find a slot in .debug_names for the object named NAME. If NAME is
5734 found, return pointer to its pool data. If NAME cannot be found,
5735 return NULL. */
5736
5737const gdb_byte *
5738dw2_debug_names_iterator::find_vec_in_debug_names
5739 (const mapped_debug_names &map, const char *name)
5740{
5741 int (*cmp) (const char *, const char *);
5742
54ee4252 5743 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5744 if (current_language->la_language == language_cplus
5745 || current_language->la_language == language_fortran
5746 || current_language->la_language == language_d)
5747 {
5748 /* NAME is already canonical. Drop any qualifiers as
5749 .debug_names does not contain any. */
5750
5751 if (strchr (name, '(') != NULL)
5752 {
54ee4252 5753 without_params = cp_remove_params (name);
927aa2e7 5754 if (without_params != NULL)
54ee4252 5755 name = without_params.get ();
927aa2e7
JK
5756 }
5757 }
5758
5759 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5760
5761 const uint32_t full_hash = dwarf5_djb_hash (name);
5762 uint32_t namei
5763 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5764 (map.bucket_table_reordered
5765 + (full_hash % map.bucket_count)), 4,
5766 map.dwarf5_byte_order);
5767 if (namei == 0)
5768 return NULL;
5769 --namei;
5770 if (namei >= map.name_count)
5771 {
b98664d3 5772 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5773 "[in module %s]"),
5774 namei, map.name_count,
ed2dc618 5775 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5776 return NULL;
5777 }
5778
5779 for (;;)
5780 {
5781 const uint32_t namei_full_hash
5782 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5783 (map.hash_table_reordered + namei), 4,
5784 map.dwarf5_byte_order);
5785 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5786 return NULL;
5787
5788 if (full_hash == namei_full_hash)
5789 {
5790 const char *const namei_string = map.namei_to_name (namei);
5791
5792#if 0 /* An expensive sanity check. */
5793 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5794 {
b98664d3 5795 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5796 "[in module %s]"),
5797 namei, objfile_name (dwarf2_per_objfile->objfile));
5798 return NULL;
5799 }
5800#endif
5801
5802 if (cmp (namei_string, name) == 0)
5803 {
5804 const ULONGEST namei_entry_offs
5805 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5806 + namei * map.offset_size),
5807 map.offset_size, map.dwarf5_byte_order);
5808 return map.entry_pool + namei_entry_offs;
5809 }
5810 }
5811
5812 ++namei;
5813 if (namei >= map.name_count)
5814 return NULL;
5815 }
5816}
5817
5818const gdb_byte *
5819dw2_debug_names_iterator::find_vec_in_debug_names
5820 (const mapped_debug_names &map, uint32_t namei)
5821{
5822 if (namei >= map.name_count)
5823 {
b98664d3 5824 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5825 "[in module %s]"),
5826 namei, map.name_count,
ed2dc618 5827 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5828 return NULL;
5829 }
5830
5831 const ULONGEST namei_entry_offs
5832 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5833 + namei * map.offset_size),
5834 map.offset_size, map.dwarf5_byte_order);
5835 return map.entry_pool + namei_entry_offs;
5836}
5837
5838/* See dw2_debug_names_iterator. */
5839
5840dwarf2_per_cu_data *
5841dw2_debug_names_iterator::next ()
5842{
5843 if (m_addr == NULL)
5844 return NULL;
5845
ed2dc618
SM
5846 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5847 struct objfile *objfile = dwarf2_per_objfile->objfile;
5848 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5849
5850 again:
5851
5852 unsigned int bytes_read;
5853 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5854 m_addr += bytes_read;
5855 if (abbrev == 0)
5856 return NULL;
5857
5858 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5859 if (indexval_it == m_map.abbrev_map.cend ())
5860 {
b98664d3 5861 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5862 "[in module %s]"),
ed2dc618 5863 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5864 return NULL;
5865 }
5866 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5867 enum class symbol_linkage {
5868 unknown,
5869 static_,
5870 extern_,
23c13d42 5871 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5872 dwarf2_per_cu_data *per_cu = NULL;
5873 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5874 {
5875 ULONGEST ull;
5876 switch (attr.form)
5877 {
5878 case DW_FORM_implicit_const:
5879 ull = attr.implicit_const;
5880 break;
5881 case DW_FORM_flag_present:
5882 ull = 1;
5883 break;
5884 case DW_FORM_udata:
5885 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5886 m_addr += bytes_read;
5887 break;
5888 default:
b98664d3 5889 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5890 dwarf_form_name (attr.form),
ed2dc618 5891 objfile_name (objfile));
927aa2e7
JK
5892 return NULL;
5893 }
5894 switch (attr.dw_idx)
5895 {
5896 case DW_IDX_compile_unit:
5897 /* Don't crash on bad data. */
b76e467d 5898 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5899 {
b98664d3 5900 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5901 " [in module %s]"),
5902 pulongest (ull),
5903 objfile_name (dwarf2_per_objfile->objfile));
5904 continue;
5905 }
ff4c9fec 5906 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5907 break;
8af5c486
JK
5908 case DW_IDX_type_unit:
5909 /* Don't crash on bad data. */
b2bdb8cf 5910 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5911 {
b98664d3 5912 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5913 " [in module %s]"),
5914 pulongest (ull),
5915 objfile_name (dwarf2_per_objfile->objfile));
5916 continue;
5917 }
ff4c9fec 5918 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5919 break;
927aa2e7
JK
5920 case DW_IDX_GNU_internal:
5921 if (!m_map.augmentation_is_gdb)
5922 break;
23c13d42 5923 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5924 break;
5925 case DW_IDX_GNU_external:
5926 if (!m_map.augmentation_is_gdb)
5927 break;
23c13d42 5928 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5929 break;
5930 }
5931 }
5932
5933 /* Skip if already read in. */
5934 if (per_cu->v.quick->compunit_symtab)
5935 goto again;
5936
5937 /* Check static vs global. */
23c13d42 5938 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5939 {
2b79f376 5940 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5941 const bool symbol_is_static =
5942 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5943 if (want_static != symbol_is_static)
2b79f376 5944 goto again;
927aa2e7
JK
5945 }
5946
5947 /* Match dw2_symtab_iter_next, symbol_kind
5948 and debug_names::psymbol_tag. */
5949 switch (m_domain)
5950 {
5951 case VAR_DOMAIN:
5952 switch (indexval.dwarf_tag)
5953 {
5954 case DW_TAG_variable:
5955 case DW_TAG_subprogram:
5956 /* Some types are also in VAR_DOMAIN. */
5957 case DW_TAG_typedef:
5958 case DW_TAG_structure_type:
5959 break;
5960 default:
5961 goto again;
5962 }
5963 break;
5964 case STRUCT_DOMAIN:
5965 switch (indexval.dwarf_tag)
5966 {
5967 case DW_TAG_typedef:
5968 case DW_TAG_structure_type:
5969 break;
5970 default:
5971 goto again;
5972 }
5973 break;
5974 case LABEL_DOMAIN:
5975 switch (indexval.dwarf_tag)
5976 {
5977 case 0:
5978 case DW_TAG_variable:
5979 break;
5980 default:
5981 goto again;
5982 }
5983 break;
59c35742
AB
5984 case MODULE_DOMAIN:
5985 switch (indexval.dwarf_tag)
5986 {
5987 case DW_TAG_module:
5988 break;
5989 default:
5990 goto again;
5991 }
5992 break;
927aa2e7
JK
5993 default:
5994 break;
5995 }
5996
5997 /* Match dw2_expand_symtabs_matching, symbol_kind and
5998 debug_names::psymbol_tag. */
5999 switch (m_search)
4b514bc8 6000 {
927aa2e7
JK
6001 case VARIABLES_DOMAIN:
6002 switch (indexval.dwarf_tag)
4b514bc8 6003 {
927aa2e7
JK
6004 case DW_TAG_variable:
6005 break;
6006 default:
6007 goto again;
4b514bc8 6008 }
927aa2e7
JK
6009 break;
6010 case FUNCTIONS_DOMAIN:
6011 switch (indexval.dwarf_tag)
4b514bc8 6012 {
927aa2e7
JK
6013 case DW_TAG_subprogram:
6014 break;
6015 default:
6016 goto again;
4b514bc8 6017 }
927aa2e7
JK
6018 break;
6019 case TYPES_DOMAIN:
6020 switch (indexval.dwarf_tag)
6021 {
6022 case DW_TAG_typedef:
6023 case DW_TAG_structure_type:
6024 break;
6025 default:
6026 goto again;
6027 }
6028 break;
59c35742
AB
6029 case MODULES_DOMAIN:
6030 switch (indexval.dwarf_tag)
6031 {
6032 case DW_TAG_module:
6033 break;
6034 default:
6035 goto again;
6036 }
927aa2e7
JK
6037 default:
6038 break;
4b514bc8 6039 }
927aa2e7
JK
6040
6041 return per_cu;
4b514bc8 6042}
61920122 6043
927aa2e7 6044static struct compunit_symtab *
c7f839cb 6045dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6046 const char *name, domain_enum domain)
4b514bc8 6047{
ed2dc618
SM
6048 struct dwarf2_per_objfile *dwarf2_per_objfile
6049 = get_dwarf2_per_objfile (objfile);
61920122 6050
927aa2e7
JK
6051 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6052 if (!mapp)
61920122 6053 {
927aa2e7
JK
6054 /* index is NULL if OBJF_READNOW. */
6055 return NULL;
6056 }
6057 const auto &map = *mapp;
9291a0cd 6058
2b79f376 6059 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6060
927aa2e7
JK
6061 struct compunit_symtab *stab_best = NULL;
6062 struct dwarf2_per_cu_data *per_cu;
6063 while ((per_cu = iter.next ()) != NULL)
6064 {
6065 struct symbol *sym, *with_opaque = NULL;
58f0c718 6066 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6067 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6068 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6069
927aa2e7
JK
6070 sym = block_find_symbol (block, name, domain,
6071 block_find_non_opaque_type_preferred,
6072 &with_opaque);
9703b513 6073
927aa2e7
JK
6074 /* Some caution must be observed with overloaded functions and
6075 methods, since the index will not contain any overload
6076 information (but NAME might contain it). */
a3ec0bb1 6077
927aa2e7 6078 if (sym != NULL
987012b8 6079 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
6080 return stab;
6081 if (with_opaque != NULL
987012b8 6082 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 6083 stab_best = stab;
9703b513 6084
927aa2e7 6085 /* Keep looking through other CUs. */
9703b513
TT
6086 }
6087
927aa2e7 6088 return stab_best;
9703b513
TT
6089}
6090
927aa2e7
JK
6091/* This dumps minimal information about .debug_names. It is called
6092 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6093 uses this to verify that .debug_names has been loaded. */
9291a0cd 6094
927aa2e7
JK
6095static void
6096dw2_debug_names_dump (struct objfile *objfile)
6097{
ed2dc618
SM
6098 struct dwarf2_per_objfile *dwarf2_per_objfile
6099 = get_dwarf2_per_objfile (objfile);
6100
927aa2e7
JK
6101 gdb_assert (dwarf2_per_objfile->using_index);
6102 printf_filtered (".debug_names:");
6103 if (dwarf2_per_objfile->debug_names_table)
6104 printf_filtered (" exists\n");
6105 else
6106 printf_filtered (" faked for \"readnow\"\n");
6107 printf_filtered ("\n");
9291a0cd
TT
6108}
6109
9291a0cd 6110static void
927aa2e7
JK
6111dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6112 const char *func_name)
9291a0cd 6113{
ed2dc618
SM
6114 struct dwarf2_per_objfile *dwarf2_per_objfile
6115 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6116
927aa2e7
JK
6117 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6118 if (dwarf2_per_objfile->debug_names_table)
24c79950 6119 {
927aa2e7 6120 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6121
2b79f376 6122 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6123
927aa2e7
JK
6124 struct dwarf2_per_cu_data *per_cu;
6125 while ((per_cu = iter.next ()) != NULL)
58f0c718 6126 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6127 }
6128}
24c79950 6129
3b00ef10
TT
6130static void
6131dw2_debug_names_map_matching_symbols
6132 (struct objfile *objfile,
6133 const lookup_name_info &name, domain_enum domain,
6134 int global,
6135 gdb::function_view<symbol_found_callback_ftype> callback,
6136 symbol_compare_ftype *ordered_compare)
6137{
6138 struct dwarf2_per_objfile *dwarf2_per_objfile
6139 = get_dwarf2_per_objfile (objfile);
6140
6141 /* debug_names_table is NULL if OBJF_READNOW. */
6142 if (!dwarf2_per_objfile->debug_names_table)
6143 return;
6144
6145 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6146 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6147
6148 const char *match_name = name.ada ().lookup_name ().c_str ();
6149 auto matcher = [&] (const char *symname)
6150 {
6151 if (ordered_compare == nullptr)
6152 return true;
6153 return ordered_compare (symname, match_name) == 0;
6154 };
6155
6156 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6157 [&] (offset_type namei)
6158 {
6159 /* The name was matched, now expand corresponding CUs that were
6160 marked. */
6161 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6162
6163 struct dwarf2_per_cu_data *per_cu;
6164 while ((per_cu = iter.next ()) != NULL)
6165 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6166 return true;
6167 });
6168
6169 /* It's a shame we couldn't do this inside the
6170 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6171 that have already been expanded. Instead, this loop matches what
6172 the psymtab code does. */
6173 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6174 {
6175 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6176 if (cust != nullptr)
6177 {
6178 const struct block *block
6179 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6180 if (!iterate_over_symbols_terminated (block, name,
6181 domain, callback))
6182 break;
6183 }
6184 }
6185}
6186
927aa2e7
JK
6187static void
6188dw2_debug_names_expand_symtabs_matching
6189 (struct objfile *objfile,
6190 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6191 const lookup_name_info &lookup_name,
6192 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6193 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6194 enum search_domain kind)
6195{
ed2dc618
SM
6196 struct dwarf2_per_objfile *dwarf2_per_objfile
6197 = get_dwarf2_per_objfile (objfile);
9291a0cd 6198
927aa2e7
JK
6199 /* debug_names_table is NULL if OBJF_READNOW. */
6200 if (!dwarf2_per_objfile->debug_names_table)
6201 return;
9291a0cd 6202
ed2dc618 6203 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6204
44ed8f3e 6205 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6206
44ed8f3e
PA
6207 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6208 symbol_matcher,
6209 kind, [&] (offset_type namei)
927aa2e7 6210 {
927aa2e7
JK
6211 /* The name was matched, now expand corresponding CUs that were
6212 marked. */
6213 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6214
927aa2e7
JK
6215 struct dwarf2_per_cu_data *per_cu;
6216 while ((per_cu = iter.next ()) != NULL)
6217 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6218 expansion_notify);
3b00ef10 6219 return true;
44ed8f3e 6220 });
9291a0cd
TT
6221}
6222
927aa2e7 6223const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6224{
6225 dw2_has_symbols,
6226 dw2_find_last_source_symtab,
6227 dw2_forget_cached_source_info,
f8eba3c6 6228 dw2_map_symtabs_matching_filename,
927aa2e7 6229 dw2_debug_names_lookup_symbol,
9291a0cd 6230 dw2_print_stats,
927aa2e7 6231 dw2_debug_names_dump,
927aa2e7 6232 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6233 dw2_expand_all_symtabs,
652a8996 6234 dw2_expand_symtabs_with_fullname,
3b00ef10 6235 dw2_debug_names_map_matching_symbols,
927aa2e7 6236 dw2_debug_names_expand_symtabs_matching,
43f3e411 6237 dw2_find_pc_sect_compunit_symtab,
71a3c369 6238 NULL,
9291a0cd
TT
6239 dw2_map_symbol_filenames
6240};
6241
4485a1c1
SM
6242/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6243 to either a dwarf2_per_objfile or dwz_file object. */
6244
6245template <typename T>
6246static gdb::array_view<const gdb_byte>
6247get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6248{
6249 dwarf2_section_info *section = &section_owner->gdb_index;
6250
6251 if (dwarf2_section_empty_p (section))
6252 return {};
6253
6254 /* Older elfutils strip versions could keep the section in the main
6255 executable while splitting it for the separate debug info file. */
6256 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6257 return {};
6258
6259 dwarf2_read_section (obj, section);
6260
8bebfcda
PA
6261 /* dwarf2_section_info::size is a bfd_size_type, while
6262 gdb::array_view works with size_t. On 32-bit hosts, with
6263 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6264 is 32-bit. So we need an explicit narrowing conversion here.
6265 This is fine, because it's impossible to allocate or mmap an
6266 array/buffer larger than what size_t can represent. */
6267 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6268}
6269
87d6a7aa
SM
6270/* Lookup the index cache for the contents of the index associated to
6271 DWARF2_OBJ. */
6272
6273static gdb::array_view<const gdb_byte>
6274get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6275{
6276 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6277 if (build_id == nullptr)
6278 return {};
6279
6280 return global_index_cache.lookup_gdb_index (build_id,
6281 &dwarf2_obj->index_cache_res);
6282}
6283
6284/* Same as the above, but for DWZ. */
6285
6286static gdb::array_view<const gdb_byte>
6287get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6288{
6289 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6290 if (build_id == nullptr)
6291 return {};
6292
6293 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6294}
6295
3c0aa29a 6296/* See symfile.h. */
9291a0cd 6297
3c0aa29a
PA
6298bool
6299dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6300{
ed2dc618
SM
6301 struct dwarf2_per_objfile *dwarf2_per_objfile
6302 = get_dwarf2_per_objfile (objfile);
6303
9291a0cd
TT
6304 /* If we're about to read full symbols, don't bother with the
6305 indices. In this case we also don't care if some other debug
6306 format is making psymtabs, because they are all about to be
6307 expanded anyway. */
6308 if ((objfile->flags & OBJF_READNOW))
6309 {
9291a0cd 6310 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6311 create_all_comp_units (dwarf2_per_objfile);
6312 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6313 dwarf2_per_objfile->quick_file_names_table
6314 = create_quick_file_names_table
6315 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6316
b76e467d 6317 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6318 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6319 {
ff4c9fec 6320 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6321
e254ef6a
DE
6322 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6323 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6324 }
6325
6326 /* Return 1 so that gdb sees the "quick" functions. However,
6327 these functions will be no-ops because we will have expanded
6328 all symtabs. */
3c0aa29a
PA
6329 *index_kind = dw_index_kind::GDB_INDEX;
6330 return true;
9291a0cd
TT
6331 }
6332
ed2dc618 6333 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6334 {
6335 *index_kind = dw_index_kind::DEBUG_NAMES;
6336 return true;
6337 }
927aa2e7 6338
4485a1c1
SM
6339 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6340 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6341 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6342 {
6343 *index_kind = dw_index_kind::GDB_INDEX;
6344 return true;
6345 }
9291a0cd 6346
87d6a7aa
SM
6347 /* ... otherwise, try to find the index in the index cache. */
6348 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6349 get_gdb_index_contents_from_cache,
6350 get_gdb_index_contents_from_cache_dwz))
6351 {
6352 global_index_cache.hit ();
6353 *index_kind = dw_index_kind::GDB_INDEX;
6354 return true;
6355 }
6356
6357 global_index_cache.miss ();
3c0aa29a 6358 return false;
9291a0cd
TT
6359}
6360
6361\f
6362
dce234bc
PP
6363/* Build a partial symbol table. */
6364
6365void
f29dff0a 6366dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6367{
ed2dc618
SM
6368 struct dwarf2_per_objfile *dwarf2_per_objfile
6369 = get_dwarf2_per_objfile (objfile);
c9bf0622 6370
6eee24ce 6371 init_psymbol_list (objfile, 1024);
c906108c 6372
a70b8144 6373 try
c9bf0622
TT
6374 {
6375 /* This isn't really ideal: all the data we allocate on the
6376 objfile's obstack is still uselessly kept around. However,
6377 freeing it seems unsafe. */
906768f9 6378 psymtab_discarder psymtabs (objfile);
ed2dc618 6379 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6380 psymtabs.keep ();
87d6a7aa
SM
6381
6382 /* (maybe) store an index in the cache. */
6383 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6384 }
230d2906 6385 catch (const gdb_exception_error &except)
492d29ea
PA
6386 {
6387 exception_print (gdb_stderr, except);
6388 }
c906108c 6389}
c906108c 6390
1ce1cefd
DE
6391/* Return the total length of the CU described by HEADER. */
6392
6393static unsigned int
6394get_cu_length (const struct comp_unit_head *header)
6395{
6396 return header->initial_length_size + header->length;
6397}
6398
9c541725 6399/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6400
9c541725
PA
6401static inline bool
6402offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6403{
9c541725
PA
6404 sect_offset bottom = cu_header->sect_off;
6405 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6406
9c541725 6407 return sect_off >= bottom && sect_off < top;
45452591
DE
6408}
6409
3b80fe9b
DE
6410/* Find the base address of the compilation unit for range lists and
6411 location lists. It will normally be specified by DW_AT_low_pc.
6412 In DWARF-3 draft 4, the base address could be overridden by
6413 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6414 compilation units with discontinuous ranges. */
6415
6416static void
6417dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6418{
6419 struct attribute *attr;
6420
6421 cu->base_known = 0;
6422 cu->base_address = 0;
6423
6424 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6425 if (attr != nullptr)
3b80fe9b 6426 {
31aa7e4e 6427 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6428 cu->base_known = 1;
6429 }
6430 else
6431 {
6432 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6433 if (attr != nullptr)
3b80fe9b 6434 {
31aa7e4e 6435 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6436 cu->base_known = 1;
6437 }
6438 }
6439}
6440
93311388 6441/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6442 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6443 NOTE: This leaves members offset, first_die_offset to be filled in
6444 by the caller. */
107d2387 6445
d521ce57 6446static const gdb_byte *
107d2387 6447read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6448 const gdb_byte *info_ptr,
6449 struct dwarf2_section_info *section,
6450 rcuh_kind section_kind)
107d2387
AC
6451{
6452 int signed_addr;
891d2f0b 6453 unsigned int bytes_read;
43988095
JK
6454 const char *filename = get_section_file_name (section);
6455 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6456
6457 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6458 cu_header->initial_length_size = bytes_read;
6459 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6460 info_ptr += bytes_read;
107d2387 6461 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6462 if (cu_header->version < 2 || cu_header->version > 5)
6463 error (_("Dwarf Error: wrong version in compilation unit header "
6464 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6465 cu_header->version, filename);
107d2387 6466 info_ptr += 2;
43988095
JK
6467 if (cu_header->version < 5)
6468 switch (section_kind)
6469 {
6470 case rcuh_kind::COMPILE:
6471 cu_header->unit_type = DW_UT_compile;
6472 break;
6473 case rcuh_kind::TYPE:
6474 cu_header->unit_type = DW_UT_type;
6475 break;
6476 default:
6477 internal_error (__FILE__, __LINE__,
6478 _("read_comp_unit_head: invalid section_kind"));
6479 }
6480 else
6481 {
6482 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6483 (read_1_byte (abfd, info_ptr));
6484 info_ptr += 1;
6485 switch (cu_header->unit_type)
6486 {
6487 case DW_UT_compile:
a084a2a6
AT
6488 case DW_UT_partial:
6489 case DW_UT_skeleton:
6490 case DW_UT_split_compile:
43988095
JK
6491 if (section_kind != rcuh_kind::COMPILE)
6492 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6493 "(is %s, should be %s) [in module %s]"),
6494 dwarf_unit_type_name (cu_header->unit_type),
6495 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6496 break;
6497 case DW_UT_type:
a084a2a6 6498 case DW_UT_split_type:
43988095
JK
6499 section_kind = rcuh_kind::TYPE;
6500 break;
6501 default:
6502 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6503 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6504 "[in module %s]"), cu_header->unit_type,
6505 dwarf_unit_type_name (DW_UT_compile),
6506 dwarf_unit_type_name (DW_UT_skeleton),
6507 dwarf_unit_type_name (DW_UT_split_compile),
6508 dwarf_unit_type_name (DW_UT_type),
6509 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6510 }
6511
6512 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6513 info_ptr += 1;
6514 }
9c541725
PA
6515 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6516 cu_header,
6517 &bytes_read);
613e1657 6518 info_ptr += bytes_read;
43988095
JK
6519 if (cu_header->version < 5)
6520 {
6521 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6522 info_ptr += 1;
6523 }
107d2387
AC
6524 signed_addr = bfd_get_sign_extend_vma (abfd);
6525 if (signed_addr < 0)
8e65ff28 6526 internal_error (__FILE__, __LINE__,
e2e0b3e5 6527 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6528 cu_header->signed_addr_p = signed_addr;
c764a876 6529
a084a2a6
AT
6530 bool header_has_signature = section_kind == rcuh_kind::TYPE
6531 || cu_header->unit_type == DW_UT_skeleton
6532 || cu_header->unit_type == DW_UT_split_compile;
43988095 6533
a084a2a6
AT
6534 if (header_has_signature)
6535 {
43988095
JK
6536 cu_header->signature = read_8_bytes (abfd, info_ptr);
6537 info_ptr += 8;
a084a2a6 6538 }
43988095 6539
a084a2a6
AT
6540 if (section_kind == rcuh_kind::TYPE)
6541 {
6542 LONGEST type_offset;
43988095
JK
6543 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6544 info_ptr += bytes_read;
9c541725
PA
6545 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6546 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6547 error (_("Dwarf Error: Too big type_offset in compilation unit "
6548 "header (is %s) [in module %s]"), plongest (type_offset),
6549 filename);
6550 }
6551
107d2387
AC
6552 return info_ptr;
6553}
6554
36586728
TT
6555/* Helper function that returns the proper abbrev section for
6556 THIS_CU. */
6557
6558static struct dwarf2_section_info *
6559get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6560{
6561 struct dwarf2_section_info *abbrev;
ed2dc618 6562 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6563
6564 if (this_cu->is_dwz)
ed2dc618 6565 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6566 else
6567 abbrev = &dwarf2_per_objfile->abbrev;
6568
6569 return abbrev;
6570}
6571
9ff913ba
DE
6572/* Subroutine of read_and_check_comp_unit_head and
6573 read_and_check_type_unit_head to simplify them.
6574 Perform various error checking on the header. */
6575
6576static void
ed2dc618
SM
6577error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6578 struct comp_unit_head *header,
4bdcc0c1
DE
6579 struct dwarf2_section_info *section,
6580 struct dwarf2_section_info *abbrev_section)
9ff913ba 6581{
a32a8923 6582 const char *filename = get_section_file_name (section);
9ff913ba 6583
9c541725 6584 if (to_underlying (header->abbrev_sect_off)
36586728 6585 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6586 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6587 "(offset %s + 6) [in module %s]"),
6588 sect_offset_str (header->abbrev_sect_off),
6589 sect_offset_str (header->sect_off),
9ff913ba
DE
6590 filename);
6591
9c541725 6592 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6593 avoid potential 32-bit overflow. */
9c541725 6594 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6595 > section->size)
9c541725 6596 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6597 "(offset %s + 0) [in module %s]"),
6598 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6599 filename);
6600}
6601
6602/* Read in a CU/TU header and perform some basic error checking.
6603 The contents of the header are stored in HEADER.
6604 The result is a pointer to the start of the first DIE. */
adabb602 6605
d521ce57 6606static const gdb_byte *
ed2dc618
SM
6607read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6608 struct comp_unit_head *header,
9ff913ba 6609 struct dwarf2_section_info *section,
4bdcc0c1 6610 struct dwarf2_section_info *abbrev_section,
d521ce57 6611 const gdb_byte *info_ptr,
43988095 6612 rcuh_kind section_kind)
72bf9492 6613{
d521ce57 6614 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6615
9c541725 6616 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6617
43988095 6618 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6619
9c541725 6620 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6621
ed2dc618
SM
6622 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6623 abbrev_section);
9ff913ba
DE
6624
6625 return info_ptr;
348e048f
DE
6626}
6627
f4dc4d17
DE
6628/* Fetch the abbreviation table offset from a comp or type unit header. */
6629
6630static sect_offset
ed2dc618
SM
6631read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6632 struct dwarf2_section_info *section,
9c541725 6633 sect_offset sect_off)
f4dc4d17 6634{
a32a8923 6635 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6636 const gdb_byte *info_ptr;
ac298888 6637 unsigned int initial_length_size, offset_size;
43988095 6638 uint16_t version;
f4dc4d17
DE
6639
6640 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6641 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6642 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6643 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6644 info_ptr += initial_length_size;
6645
6646 version = read_2_bytes (abfd, info_ptr);
6647 info_ptr += 2;
6648 if (version >= 5)
6649 {
6650 /* Skip unit type and address size. */
6651 info_ptr += 2;
6652 }
6653
9c541725 6654 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6655}
6656
aaa75496
JB
6657/* Allocate a new partial symtab for file named NAME and mark this new
6658 partial symtab as being an include of PST. */
6659
6660static void
d521ce57 6661dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6662 struct objfile *objfile)
6663{
6664 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6665
fbd9ab74
JK
6666 if (!IS_ABSOLUTE_PATH (subpst->filename))
6667 {
6668 /* It shares objfile->objfile_obstack. */
6669 subpst->dirname = pst->dirname;
6670 }
6671
a9342b62 6672 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6673 subpst->dependencies[0] = pst;
6674 subpst->number_of_dependencies = 1;
6675
aaa75496 6676 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6677
6678 /* No private part is necessary for include psymtabs. This property
6679 can be used to differentiate between such include psymtabs and
10b3939b 6680 the regular ones. */
58a9656e 6681 subpst->read_symtab_private = NULL;
aaa75496
JB
6682}
6683
6684/* Read the Line Number Program data and extract the list of files
6685 included by the source file represented by PST. Build an include
d85a05f0 6686 partial symtab for each of these included files. */
aaa75496
JB
6687
6688static void
6689dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6690 struct die_info *die,
6691 struct partial_symtab *pst)
aaa75496 6692{
fff8551c 6693 line_header_up lh;
d85a05f0 6694 struct attribute *attr;
aaa75496 6695
d85a05f0 6696 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6697 if (attr != nullptr)
9c541725 6698 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6699 if (lh == NULL)
6700 return; /* No linetable, so no includes. */
6701
79748972
TT
6702 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6703 that we pass in the raw text_low here; that is ok because we're
6704 only decoding the line table to make include partial symtabs, and
6705 so the addresses aren't really used. */
4ae976d1 6706 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6707 pst->raw_text_low (), 1);
aaa75496
JB
6708}
6709
348e048f 6710static hashval_t
52dc124a 6711hash_signatured_type (const void *item)
348e048f 6712{
9a3c8263
SM
6713 const struct signatured_type *sig_type
6714 = (const struct signatured_type *) item;
9a619af0 6715
348e048f 6716 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6717 return sig_type->signature;
348e048f
DE
6718}
6719
6720static int
52dc124a 6721eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6722{
9a3c8263
SM
6723 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6724 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6725
348e048f
DE
6726 return lhs->signature == rhs->signature;
6727}
6728
1fd400ff
TT
6729/* Allocate a hash table for signatured types. */
6730
6731static htab_t
673bfd45 6732allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6733{
6734 return htab_create_alloc_ex (41,
52dc124a
DE
6735 hash_signatured_type,
6736 eq_signatured_type,
1fd400ff
TT
6737 NULL,
6738 &objfile->objfile_obstack,
6739 hashtab_obstack_allocate,
6740 dummy_obstack_deallocate);
6741}
6742
d467dd73 6743/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6744
6745static int
d467dd73 6746add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6747{
9a3c8263 6748 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6749 std::vector<signatured_type *> *all_type_units
6750 = (std::vector<signatured_type *> *) datum;
1fd400ff 6751
b2bdb8cf 6752 all_type_units->push_back (sigt);
1fd400ff
TT
6753
6754 return 1;
6755}
6756
78d4d2c5 6757/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6758 and fill them into TYPES_HTAB. It will process only type units,
6759 therefore DW_UT_type. */
c88ee1f0 6760
78d4d2c5 6761static void
ed2dc618
SM
6762create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6763 struct dwo_file *dwo_file,
43988095
JK
6764 dwarf2_section_info *section, htab_t &types_htab,
6765 rcuh_kind section_kind)
348e048f 6766{
3019eac3 6767 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6768 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6769 bfd *abfd;
6770 const gdb_byte *info_ptr, *end_ptr;
348e048f 6771
4bdcc0c1
DE
6772 abbrev_section = (dwo_file != NULL
6773 ? &dwo_file->sections.abbrev
6774 : &dwarf2_per_objfile->abbrev);
6775
b4f54984 6776 if (dwarf_read_debug)
43988095
JK
6777 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6778 get_section_name (section),
a32a8923 6779 get_section_file_name (abbrev_section));
09406207 6780
78d4d2c5
JK
6781 dwarf2_read_section (objfile, section);
6782 info_ptr = section->buffer;
348e048f 6783
78d4d2c5
JK
6784 if (info_ptr == NULL)
6785 return;
348e048f 6786
78d4d2c5
JK
6787 /* We can't set abfd until now because the section may be empty or
6788 not present, in which case the bfd is unknown. */
6789 abfd = get_section_bfd_owner (section);
348e048f 6790
78d4d2c5
JK
6791 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6792 because we don't need to read any dies: the signature is in the
6793 header. */
3019eac3 6794
78d4d2c5
JK
6795 end_ptr = info_ptr + section->size;
6796 while (info_ptr < end_ptr)
6797 {
78d4d2c5
JK
6798 struct signatured_type *sig_type;
6799 struct dwo_unit *dwo_tu;
6800 void **slot;
6801 const gdb_byte *ptr = info_ptr;
6802 struct comp_unit_head header;
6803 unsigned int length;
8b70b953 6804
9c541725 6805 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6806
a49dd8dd
JK
6807 /* Initialize it due to a false compiler warning. */
6808 header.signature = -1;
9c541725 6809 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6810
78d4d2c5
JK
6811 /* We need to read the type's signature in order to build the hash
6812 table, but we don't need anything else just yet. */
348e048f 6813
ed2dc618 6814 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6815 abbrev_section, ptr, section_kind);
348e048f 6816
78d4d2c5 6817 length = get_cu_length (&header);
6caca83c 6818
78d4d2c5
JK
6819 /* Skip dummy type units. */
6820 if (ptr >= info_ptr + length
43988095
JK
6821 || peek_abbrev_code (abfd, ptr) == 0
6822 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6823 {
6824 info_ptr += length;
6825 continue;
6826 }
dee91e82 6827
78d4d2c5
JK
6828 if (types_htab == NULL)
6829 {
6830 if (dwo_file)
6831 types_htab = allocate_dwo_unit_table (objfile);
6832 else
6833 types_htab = allocate_signatured_type_table (objfile);
6834 }
8b70b953 6835
78d4d2c5
JK
6836 if (dwo_file)
6837 {
6838 sig_type = NULL;
6839 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6840 struct dwo_unit);
6841 dwo_tu->dwo_file = dwo_file;
43988095 6842 dwo_tu->signature = header.signature;
9c541725 6843 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6844 dwo_tu->section = section;
9c541725 6845 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6846 dwo_tu->length = length;
6847 }
6848 else
6849 {
6850 /* N.B.: type_offset is not usable if this type uses a DWO file.
6851 The real type_offset is in the DWO file. */
6852 dwo_tu = NULL;
6853 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6854 struct signatured_type);
43988095 6855 sig_type->signature = header.signature;
9c541725 6856 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6857 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6858 sig_type->per_cu.is_debug_types = 1;
6859 sig_type->per_cu.section = section;
9c541725 6860 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6861 sig_type->per_cu.length = length;
6862 }
6863
6864 slot = htab_find_slot (types_htab,
6865 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6866 INSERT);
6867 gdb_assert (slot != NULL);
6868 if (*slot != NULL)
6869 {
9c541725 6870 sect_offset dup_sect_off;
0349ea22 6871
3019eac3
DE
6872 if (dwo_file)
6873 {
78d4d2c5
JK
6874 const struct dwo_unit *dup_tu
6875 = (const struct dwo_unit *) *slot;
6876
9c541725 6877 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6878 }
6879 else
6880 {
78d4d2c5
JK
6881 const struct signatured_type *dup_tu
6882 = (const struct signatured_type *) *slot;
6883
9c541725 6884 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6885 }
8b70b953 6886
b98664d3 6887 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6888 " the entry at offset %s, signature %s"),
6889 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6890 hex_string (header.signature));
78d4d2c5
JK
6891 }
6892 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6893
78d4d2c5 6894 if (dwarf_read_debug > 1)
9d8780f0
SM
6895 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6896 sect_offset_str (sect_off),
43988095 6897 hex_string (header.signature));
3019eac3 6898
78d4d2c5
JK
6899 info_ptr += length;
6900 }
6901}
3019eac3 6902
78d4d2c5
JK
6903/* Create the hash table of all entries in the .debug_types
6904 (or .debug_types.dwo) section(s).
6905 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6906 otherwise it is NULL.
b3c8eb43 6907
78d4d2c5 6908 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6909
78d4d2c5 6910 Note: This function processes DWO files only, not DWP files. */
348e048f 6911
78d4d2c5 6912static void
ed2dc618
SM
6913create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6914 struct dwo_file *dwo_file,
fd5866f6 6915 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6916 htab_t &types_htab)
6917{
fd5866f6
SM
6918 for (dwarf2_section_info &section : type_sections)
6919 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6920 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6921}
6922
6923/* Create the hash table of all entries in the .debug_types section,
6924 and initialize all_type_units.
6925 The result is zero if there is an error (e.g. missing .debug_types section),
6926 otherwise non-zero. */
6927
6928static int
ed2dc618 6929create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6930{
78d4d2c5 6931 htab_t types_htab = NULL;
3019eac3 6932
ed2dc618
SM
6933 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6934 &dwarf2_per_objfile->info, types_htab,
43988095 6935 rcuh_kind::COMPILE);
ed2dc618
SM
6936 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6937 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6938 if (types_htab == NULL)
6939 {
6940 dwarf2_per_objfile->signatured_types = NULL;
6941 return 0;
6942 }
6943
348e048f
DE
6944 dwarf2_per_objfile->signatured_types = types_htab;
6945
b2bdb8cf
SM
6946 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6947 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6948
6949 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6950 &dwarf2_per_objfile->all_type_units);
1fd400ff 6951
348e048f
DE
6952 return 1;
6953}
6954
6aa5f3a6
DE
6955/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6956 If SLOT is non-NULL, it is the entry to use in the hash table.
6957 Otherwise we find one. */
6958
6959static struct signatured_type *
ed2dc618
SM
6960add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6961 void **slot)
6aa5f3a6
DE
6962{
6963 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6964
b2bdb8cf
SM
6965 if (dwarf2_per_objfile->all_type_units.size ()
6966 == dwarf2_per_objfile->all_type_units.capacity ())
6967 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6968
b2bdb8cf
SM
6969 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6970 struct signatured_type);
6971
6972 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6973 sig_type->signature = sig;
6974 sig_type->per_cu.is_debug_types = 1;
6975 if (dwarf2_per_objfile->using_index)
6976 {
6977 sig_type->per_cu.v.quick =
6978 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6979 struct dwarf2_per_cu_quick_data);
6980 }
6981
6982 if (slot == NULL)
6983 {
6984 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6985 sig_type, INSERT);
6986 }
6987 gdb_assert (*slot == NULL);
6988 *slot = sig_type;
6989 /* The rest of sig_type must be filled in by the caller. */
6990 return sig_type;
6991}
6992
a2ce51a0
DE
6993/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6994 Fill in SIG_ENTRY with DWO_ENTRY. */
6995
6996static void
ed2dc618 6997fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6998 struct signatured_type *sig_entry,
6999 struct dwo_unit *dwo_entry)
7000{
7ee85ab1 7001 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7002 gdb_assert (! sig_entry->per_cu.queued);
7003 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7004 if (dwarf2_per_objfile->using_index)
7005 {
7006 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7007 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7008 }
7009 else
7010 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7011 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7012 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7013 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7014 gdb_assert (sig_entry->dwo_unit == NULL);
7015
7016 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7017 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7018 sig_entry->per_cu.length = dwo_entry->length;
7019 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7020 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7021 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7022 sig_entry->dwo_unit = dwo_entry;
7023}
7024
7025/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7026 If we haven't read the TU yet, create the signatured_type data structure
7027 for a TU to be read in directly from a DWO file, bypassing the stub.
7028 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7029 using .gdb_index, then when reading a CU we want to stay in the DWO file
7030 containing that CU. Otherwise we could end up reading several other DWO
7031 files (due to comdat folding) to process the transitive closure of all the
7032 mentioned TUs, and that can be slow. The current DWO file will have every
7033 type signature that it needs.
a2ce51a0
DE
7034 We only do this for .gdb_index because in the psymtab case we already have
7035 to read all the DWOs to build the type unit groups. */
7036
7037static struct signatured_type *
7038lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7039{
518817b3
SM
7040 struct dwarf2_per_objfile *dwarf2_per_objfile
7041 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7042 struct objfile *objfile = dwarf2_per_objfile->objfile;
7043 struct dwo_file *dwo_file;
7044 struct dwo_unit find_dwo_entry, *dwo_entry;
7045 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7046 void **slot;
a2ce51a0
DE
7047
7048 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7049
6aa5f3a6
DE
7050 /* If TU skeletons have been removed then we may not have read in any
7051 TUs yet. */
7052 if (dwarf2_per_objfile->signatured_types == NULL)
7053 {
7054 dwarf2_per_objfile->signatured_types
7055 = allocate_signatured_type_table (objfile);
7056 }
a2ce51a0
DE
7057
7058 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7059 Use the global signatured_types array to do our own comdat-folding
7060 of types. If this is the first time we're reading this TU, and
7061 the TU has an entry in .gdb_index, replace the recorded data from
7062 .gdb_index with this TU. */
a2ce51a0 7063
a2ce51a0 7064 find_sig_entry.signature = sig;
6aa5f3a6
DE
7065 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7066 &find_sig_entry, INSERT);
9a3c8263 7067 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7068
7069 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7070 read. Don't reassign the global entry to point to this DWO if that's
7071 the case. Also note that if the TU is already being read, it may not
7072 have come from a DWO, the program may be a mix of Fission-compiled
7073 code and non-Fission-compiled code. */
7074
7075 /* Have we already tried to read this TU?
7076 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7077 needn't exist in the global table yet). */
7078 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7079 return sig_entry;
7080
6aa5f3a6
DE
7081 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7082 dwo_unit of the TU itself. */
7083 dwo_file = cu->dwo_unit->dwo_file;
7084
a2ce51a0
DE
7085 /* Ok, this is the first time we're reading this TU. */
7086 if (dwo_file->tus == NULL)
7087 return NULL;
7088 find_dwo_entry.signature = sig;
9a3c8263 7089 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7090 if (dwo_entry == NULL)
7091 return NULL;
7092
6aa5f3a6
DE
7093 /* If the global table doesn't have an entry for this TU, add one. */
7094 if (sig_entry == NULL)
ed2dc618 7095 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7096
ed2dc618 7097 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7098 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7099 return sig_entry;
7100}
7101
a2ce51a0
DE
7102/* Subroutine of lookup_signatured_type.
7103 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7104 then try the DWP file. If the TU stub (skeleton) has been removed then
7105 it won't be in .gdb_index. */
a2ce51a0
DE
7106
7107static struct signatured_type *
7108lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7109{
518817b3
SM
7110 struct dwarf2_per_objfile *dwarf2_per_objfile
7111 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7112 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7113 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7114 struct dwo_unit *dwo_entry;
7115 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7116 void **slot;
a2ce51a0
DE
7117
7118 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7119 gdb_assert (dwp_file != NULL);
7120
6aa5f3a6
DE
7121 /* If TU skeletons have been removed then we may not have read in any
7122 TUs yet. */
7123 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7124 {
6aa5f3a6
DE
7125 dwarf2_per_objfile->signatured_types
7126 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7127 }
7128
6aa5f3a6
DE
7129 find_sig_entry.signature = sig;
7130 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7131 &find_sig_entry, INSERT);
9a3c8263 7132 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7133
7134 /* Have we already tried to read this TU?
7135 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7136 needn't exist in the global table yet). */
7137 if (sig_entry != NULL)
7138 return sig_entry;
7139
a2ce51a0
DE
7140 if (dwp_file->tus == NULL)
7141 return NULL;
ed2dc618 7142 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7143 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7144 if (dwo_entry == NULL)
7145 return NULL;
7146
ed2dc618
SM
7147 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7148 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7149
a2ce51a0
DE
7150 return sig_entry;
7151}
7152
380bca97 7153/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7154 Returns NULL if signature SIG is not present in the table.
7155 It is up to the caller to complain about this. */
348e048f
DE
7156
7157static struct signatured_type *
a2ce51a0 7158lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7159{
518817b3
SM
7160 struct dwarf2_per_objfile *dwarf2_per_objfile
7161 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7162
a2ce51a0
DE
7163 if (cu->dwo_unit
7164 && dwarf2_per_objfile->using_index)
7165 {
7166 /* We're in a DWO/DWP file, and we're using .gdb_index.
7167 These cases require special processing. */
ed2dc618 7168 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7169 return lookup_dwo_signatured_type (cu, sig);
7170 else
7171 return lookup_dwp_signatured_type (cu, sig);
7172 }
7173 else
7174 {
7175 struct signatured_type find_entry, *entry;
348e048f 7176
a2ce51a0
DE
7177 if (dwarf2_per_objfile->signatured_types == NULL)
7178 return NULL;
7179 find_entry.signature = sig;
9a3c8263
SM
7180 entry = ((struct signatured_type *)
7181 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7182 return entry;
7183 }
348e048f 7184}
42e7ad6c
DE
7185\f
7186/* Low level DIE reading support. */
348e048f 7187
d85a05f0
DJ
7188/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7189
7190static void
7191init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7192 struct dwarf2_cu *cu,
3019eac3 7193 struct dwarf2_section_info *section,
685af9cd
TT
7194 struct dwo_file *dwo_file,
7195 struct abbrev_table *abbrev_table)
d85a05f0 7196{
fceca515 7197 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7198 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7199 reader->cu = cu;
3019eac3 7200 reader->dwo_file = dwo_file;
dee91e82
DE
7201 reader->die_section = section;
7202 reader->buffer = section->buffer;
f664829e 7203 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7204 reader->comp_dir = NULL;
685af9cd 7205 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7206}
7207
b0c7bfa9
DE
7208/* Subroutine of init_cutu_and_read_dies to simplify it.
7209 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7210 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7211 already.
7212
7213 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7214 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7215 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7216 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7217 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7218 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7219 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7220 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7221 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7222 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7223 kept around for at least as long as *RESULT_READER.
7224
b0c7bfa9
DE
7225 The result is non-zero if a valid (non-dummy) DIE was found. */
7226
7227static int
7228read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7229 struct dwo_unit *dwo_unit,
b0c7bfa9 7230 struct die_info *stub_comp_unit_die,
a2ce51a0 7231 const char *stub_comp_dir,
b0c7bfa9 7232 struct die_reader_specs *result_reader,
d521ce57 7233 const gdb_byte **result_info_ptr,
b0c7bfa9 7234 struct die_info **result_comp_unit_die,
685af9cd
TT
7235 int *result_has_children,
7236 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7237{
ed2dc618 7238 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7239 struct objfile *objfile = dwarf2_per_objfile->objfile;
7240 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7241 bfd *abfd;
d521ce57 7242 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7243 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7244 int i,num_extra_attrs;
7245 struct dwarf2_section_info *dwo_abbrev_section;
7246 struct attribute *attr;
7247 struct die_info *comp_unit_die;
7248
b0aeadb3
DE
7249 /* At most one of these may be provided. */
7250 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7251
b0c7bfa9
DE
7252 /* These attributes aren't processed until later:
7253 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7254 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7255 referenced later. However, these attributes are found in the stub
7256 which we won't have later. In order to not impose this complication
7257 on the rest of the code, we read them here and copy them to the
7258 DWO CU/TU die. */
b0c7bfa9
DE
7259
7260 stmt_list = NULL;
7261 low_pc = NULL;
7262 high_pc = NULL;
7263 ranges = NULL;
7264 comp_dir = NULL;
7265
7266 if (stub_comp_unit_die != NULL)
7267 {
7268 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7269 DWO file. */
7270 if (! this_cu->is_debug_types)
7271 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7272 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7273 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7274 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7275 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7276
7277 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7278 We need the value before we can process DW_FORM_GNU_addr_index
7279 or DW_FORM_addrx. */
b0c7bfa9
DE
7280 cu->addr_base = 0;
7281 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
435d3d88 7282 if (attr != nullptr)
b0c7bfa9
DE
7283 cu->addr_base = DW_UNSND (attr);
7284
7285 /* There should be a DW_AT_ranges_base attribute here (if needed).
7286 We need the value before we can process DW_AT_ranges. */
7287 cu->ranges_base = 0;
7288 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
435d3d88 7289 if (attr != nullptr)
b0c7bfa9
DE
7290 cu->ranges_base = DW_UNSND (attr);
7291 }
a2ce51a0
DE
7292 else if (stub_comp_dir != NULL)
7293 {
7294 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7295 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7296 comp_dir->name = DW_AT_comp_dir;
7297 comp_dir->form = DW_FORM_string;
7298 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7299 DW_STRING (comp_dir) = stub_comp_dir;
7300 }
b0c7bfa9
DE
7301
7302 /* Set up for reading the DWO CU/TU. */
7303 cu->dwo_unit = dwo_unit;
685af9cd 7304 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7305 dwarf2_read_section (objfile, section);
a32a8923 7306 abfd = get_section_bfd_owner (section);
9c541725
PA
7307 begin_info_ptr = info_ptr = (section->buffer
7308 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7309 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7310
7311 if (this_cu->is_debug_types)
7312 {
b0c7bfa9
DE
7313 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7314
ed2dc618
SM
7315 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7316 &cu->header, section,
b0c7bfa9 7317 dwo_abbrev_section,
43988095 7318 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7319 /* This is not an assert because it can be caused by bad debug info. */
43988095 7320 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7321 {
7322 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7323 " TU at offset %s [in module %s]"),
a2ce51a0 7324 hex_string (sig_type->signature),
43988095 7325 hex_string (cu->header.signature),
9d8780f0 7326 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7327 bfd_get_filename (abfd));
7328 }
9c541725 7329 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7330 /* For DWOs coming from DWP files, we don't know the CU length
7331 nor the type's offset in the TU until now. */
7332 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7333 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7334
7335 /* Establish the type offset that can be used to lookup the type.
7336 For DWO files, we don't know it until now. */
9c541725
PA
7337 sig_type->type_offset_in_section
7338 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7339 }
7340 else
7341 {
ed2dc618
SM
7342 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7343 &cu->header, section,
b0c7bfa9 7344 dwo_abbrev_section,
43988095 7345 info_ptr, rcuh_kind::COMPILE);
9c541725 7346 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7347 /* For DWOs coming from DWP files, we don't know the CU length
7348 until now. */
7349 dwo_unit->length = get_cu_length (&cu->header);
7350 }
7351
685af9cd
TT
7352 *result_dwo_abbrev_table
7353 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7354 cu->header.abbrev_sect_off);
7355 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7356 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7357
7358 /* Read in the die, but leave space to copy over the attributes
7359 from the stub. This has the benefit of simplifying the rest of
7360 the code - all the work to maintain the illusion of a single
7361 DW_TAG_{compile,type}_unit DIE is done here. */
7362 num_extra_attrs = ((stmt_list != NULL)
7363 + (low_pc != NULL)
7364 + (high_pc != NULL)
7365 + (ranges != NULL)
7366 + (comp_dir != NULL));
7367 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7368 result_has_children, num_extra_attrs);
7369
7370 /* Copy over the attributes from the stub to the DIE we just read in. */
7371 comp_unit_die = *result_comp_unit_die;
7372 i = comp_unit_die->num_attrs;
7373 if (stmt_list != NULL)
7374 comp_unit_die->attrs[i++] = *stmt_list;
7375 if (low_pc != NULL)
7376 comp_unit_die->attrs[i++] = *low_pc;
7377 if (high_pc != NULL)
7378 comp_unit_die->attrs[i++] = *high_pc;
7379 if (ranges != NULL)
7380 comp_unit_die->attrs[i++] = *ranges;
7381 if (comp_dir != NULL)
7382 comp_unit_die->attrs[i++] = *comp_dir;
7383 comp_unit_die->num_attrs += num_extra_attrs;
7384
b4f54984 7385 if (dwarf_die_debug)
bf6af496
DE
7386 {
7387 fprintf_unfiltered (gdb_stdlog,
7388 "Read die from %s@0x%x of %s:\n",
a32a8923 7389 get_section_name (section),
bf6af496
DE
7390 (unsigned) (begin_info_ptr - section->buffer),
7391 bfd_get_filename (abfd));
b4f54984 7392 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7393 }
7394
a2ce51a0
DE
7395 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7396 TUs by skipping the stub and going directly to the entry in the DWO file.
7397 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7398 to get it via circuitous means. Blech. */
7399 if (comp_dir != NULL)
7400 result_reader->comp_dir = DW_STRING (comp_dir);
7401
b0c7bfa9
DE
7402 /* Skip dummy compilation units. */
7403 if (info_ptr >= begin_info_ptr + dwo_unit->length
7404 || peek_abbrev_code (abfd, info_ptr) == 0)
7405 return 0;
7406
7407 *result_info_ptr = info_ptr;
7408 return 1;
7409}
7410
a084a2a6
AT
7411/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7412 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7413 signature is part of the header. */
7414static gdb::optional<ULONGEST>
7415lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7416{
7417 if (cu->header.version >= 5)
7418 return cu->header.signature;
7419 struct attribute *attr;
7420 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7421 if (attr == nullptr)
7422 return gdb::optional<ULONGEST> ();
7423 return DW_UNSND (attr);
7424}
7425
b0c7bfa9
DE
7426/* Subroutine of init_cutu_and_read_dies to simplify it.
7427 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7428 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7429
7430static struct dwo_unit *
7431lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7432 struct die_info *comp_unit_die)
7433{
7434 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7435 struct dwo_unit *dwo_unit;
7436 const char *comp_dir, *dwo_name;
7437
a2ce51a0
DE
7438 gdb_assert (cu != NULL);
7439
b0c7bfa9 7440 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7441 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7442 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7443
7444 if (this_cu->is_debug_types)
7445 {
7446 struct signatured_type *sig_type;
7447
7448 /* Since this_cu is the first member of struct signatured_type,
7449 we can go from a pointer to one to a pointer to the other. */
7450 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7451 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7452 }
7453 else
7454 {
a084a2a6
AT
7455 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7456 if (!signature.has_value ())
b0c7bfa9
DE
7457 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7458 " [in module %s]"),
e3b94546 7459 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7460 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7461 *signature);
b0c7bfa9
DE
7462 }
7463
b0c7bfa9
DE
7464 return dwo_unit;
7465}
7466
a2ce51a0 7467/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7468 See it for a description of the parameters.
fcd3b13d 7469 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7470
7471static void
6aa5f3a6
DE
7472init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7473 int use_existing_cu, int keep,
a2ce51a0
DE
7474 die_reader_func_ftype *die_reader_func,
7475 void *data)
7476{
fcd3b13d 7477 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7478 struct signatured_type *sig_type;
a2ce51a0
DE
7479 struct die_reader_specs reader;
7480 const gdb_byte *info_ptr;
7481 struct die_info *comp_unit_die;
7482 int has_children;
ed2dc618 7483 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7484
7485 /* Verify we can do the following downcast, and that we have the
7486 data we need. */
7487 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7488 sig_type = (struct signatured_type *) this_cu;
7489 gdb_assert (sig_type->dwo_unit != NULL);
7490
6aa5f3a6
DE
7491 if (use_existing_cu && this_cu->cu != NULL)
7492 {
7493 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7494 /* There's no need to do the rereading_dwo_cu handling that
7495 init_cutu_and_read_dies does since we don't read the stub. */
7496 }
7497 else
7498 {
7499 /* If !use_existing_cu, this_cu->cu must be NULL. */
7500 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7501 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7502 }
7503
7504 /* A future optimization, if needed, would be to use an existing
7505 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7506 could share abbrev tables. */
a2ce51a0 7507
685af9cd
TT
7508 /* The abbreviation table used by READER, this must live at least as long as
7509 READER. */
7510 abbrev_table_up dwo_abbrev_table;
7511
a2ce51a0 7512 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7513 NULL /* stub_comp_unit_die */,
7514 sig_type->dwo_unit->dwo_file->comp_dir,
7515 &reader, &info_ptr,
685af9cd
TT
7516 &comp_unit_die, &has_children,
7517 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7518 {
7519 /* Dummy die. */
a2ce51a0
DE
7520 return;
7521 }
7522
7523 /* All the "real" work is done here. */
7524 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7525
6aa5f3a6 7526 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7527 but the alternative is making the latter more complex.
7528 This function is only for the special case of using DWO files directly:
7529 no point in overly complicating the general case just to handle this. */
fcd3b13d 7530 if (new_cu != NULL && keep)
a2ce51a0 7531 {
fcd3b13d
SM
7532 /* Link this CU into read_in_chain. */
7533 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7534 dwarf2_per_objfile->read_in_chain = this_cu;
7535 /* The chain owns it now. */
7536 new_cu.release ();
a2ce51a0 7537 }
a2ce51a0
DE
7538}
7539
fd820528 7540/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7541 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7542
f4dc4d17
DE
7543 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7544 Otherwise the table specified in the comp unit header is read in and used.
7545 This is an optimization for when we already have the abbrev table.
7546
dee91e82
DE
7547 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7548 Otherwise, a new CU is allocated with xmalloc.
7549
7550 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7551 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7552
7553 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7554 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7555
70221824 7556static void
fd820528 7557init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7558 struct abbrev_table *abbrev_table,
fd820528 7559 int use_existing_cu, int keep,
58f0c718 7560 bool skip_partial,
fd820528
DE
7561 die_reader_func_ftype *die_reader_func,
7562 void *data)
c906108c 7563{
ed2dc618 7564 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7565 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7566 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7567 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7568 struct dwarf2_cu *cu;
d521ce57 7569 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7570 struct die_reader_specs reader;
d85a05f0 7571 struct die_info *comp_unit_die;
dee91e82 7572 int has_children;
dee91e82 7573 struct signatured_type *sig_type = NULL;
4bdcc0c1 7574 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7575 /* Non-zero if CU currently points to a DWO file and we need to
7576 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7577 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7578 int rereading_dwo_cu = 0;
c906108c 7579
b4f54984 7580 if (dwarf_die_debug)
9d8780f0 7581 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7582 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7583 sect_offset_str (this_cu->sect_off));
09406207 7584
dee91e82
DE
7585 if (use_existing_cu)
7586 gdb_assert (keep);
23745b47 7587
a2ce51a0
DE
7588 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7589 file (instead of going through the stub), short-circuit all of this. */
7590 if (this_cu->reading_dwo_directly)
7591 {
7592 /* Narrow down the scope of possibilities to have to understand. */
7593 gdb_assert (this_cu->is_debug_types);
7594 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7595 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7596 die_reader_func, data);
a2ce51a0
DE
7597 return;
7598 }
7599
dee91e82
DE
7600 /* This is cheap if the section is already read in. */
7601 dwarf2_read_section (objfile, section);
7602
9c541725 7603 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7604
7605 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7606
fcd3b13d 7607 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7608 if (use_existing_cu && this_cu->cu != NULL)
7609 {
7610 cu = this_cu->cu;
42e7ad6c
DE
7611 /* If this CU is from a DWO file we need to start over, we need to
7612 refetch the attributes from the skeleton CU.
7613 This could be optimized by retrieving those attributes from when we
7614 were here the first time: the previous comp_unit_die was stored in
7615 comp_unit_obstack. But there's no data yet that we need this
7616 optimization. */
7617 if (cu->dwo_unit != NULL)
7618 rereading_dwo_cu = 1;
dee91e82
DE
7619 }
7620 else
7621 {
7622 /* If !use_existing_cu, this_cu->cu must be NULL. */
7623 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7624 new_cu.reset (new dwarf2_cu (this_cu));
7625 cu = new_cu.get ();
42e7ad6c 7626 }
dee91e82 7627
b0c7bfa9 7628 /* Get the header. */
9c541725 7629 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7630 {
7631 /* We already have the header, there's no need to read it in again. */
9c541725 7632 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7633 }
7634 else
7635 {
3019eac3 7636 if (this_cu->is_debug_types)
dee91e82 7637 {
ed2dc618
SM
7638 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7639 &cu->header, section,
4bdcc0c1 7640 abbrev_section, info_ptr,
43988095 7641 rcuh_kind::TYPE);
dee91e82 7642
42e7ad6c
DE
7643 /* Since per_cu is the first member of struct signatured_type,
7644 we can go from a pointer to one to a pointer to the other. */
7645 sig_type = (struct signatured_type *) this_cu;
43988095 7646 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7647 gdb_assert (sig_type->type_offset_in_tu
7648 == cu->header.type_cu_offset_in_tu);
7649 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7650
42e7ad6c
DE
7651 /* LENGTH has not been set yet for type units if we're
7652 using .gdb_index. */
1ce1cefd 7653 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7654
7655 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7656 sig_type->type_offset_in_section =
7657 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7658
7659 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7660 }
7661 else
7662 {
ed2dc618
SM
7663 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7664 &cu->header, section,
4bdcc0c1 7665 abbrev_section,
43988095
JK
7666 info_ptr,
7667 rcuh_kind::COMPILE);
dee91e82 7668
9c541725 7669 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7670 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7671 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7672 }
7673 }
10b3939b 7674
6caca83c 7675 /* Skip dummy compilation units. */
dee91e82 7676 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7677 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7678 return;
6caca83c 7679
433df2d4
DE
7680 /* If we don't have them yet, read the abbrevs for this compilation unit.
7681 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7682 done (own the table through ABBREV_TABLE_HOLDER). */
7683 abbrev_table_up abbrev_table_holder;
f4dc4d17 7684 if (abbrev_table != NULL)
685af9cd
TT
7685 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7686 else
f4dc4d17 7687 {
685af9cd
TT
7688 abbrev_table_holder
7689 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7690 cu->header.abbrev_sect_off);
7691 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7692 }
af703f96 7693
dee91e82 7694 /* Read the top level CU/TU die. */
685af9cd 7695 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7696 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7697
58f0c718
TT
7698 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7699 return;
7700
b0c7bfa9 7701 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7702 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7703 table from the DWO file and pass the ownership over to us. It will be
7704 referenced from READER, so we must make sure to free it after we're done
7705 with READER.
7706
b0c7bfa9
DE
7707 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7708 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7709 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7710 abbrev_table_up dwo_abbrev_table;
a084a2a6 7711 if (dwo_name != nullptr)
3019eac3 7712 {
3019eac3 7713 struct dwo_unit *dwo_unit;
b0c7bfa9 7714 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7715
7716 if (has_children)
6a506a2d 7717 {
b98664d3 7718 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7719 " has children (offset %s) [in module %s]"),
7720 sect_offset_str (this_cu->sect_off),
7721 bfd_get_filename (abfd));
6a506a2d 7722 }
b0c7bfa9 7723 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7724 if (dwo_unit != NULL)
3019eac3 7725 {
6a506a2d 7726 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7727 comp_unit_die, NULL,
6a506a2d 7728 &reader, &info_ptr,
685af9cd
TT
7729 &dwo_comp_unit_die, &has_children,
7730 &dwo_abbrev_table) == 0)
6a506a2d
DE
7731 {
7732 /* Dummy die. */
6a506a2d
DE
7733 return;
7734 }
7735 comp_unit_die = dwo_comp_unit_die;
7736 }
7737 else
7738 {
7739 /* Yikes, we couldn't find the rest of the DIE, we only have
7740 the stub. A complaint has already been logged. There's
7741 not much more we can do except pass on the stub DIE to
7742 die_reader_func. We don't want to throw an error on bad
7743 debug info. */
3019eac3
DE
7744 }
7745 }
7746
b0c7bfa9 7747 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7748 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7749
b0c7bfa9 7750 /* Done, clean up. */
fcd3b13d 7751 if (new_cu != NULL && keep)
348e048f 7752 {
fcd3b13d
SM
7753 /* Link this CU into read_in_chain. */
7754 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7755 dwarf2_per_objfile->read_in_chain = this_cu;
7756 /* The chain owns it now. */
7757 new_cu.release ();
348e048f 7758 }
dee91e82
DE
7759}
7760
33e80786
DE
7761/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7762 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7763 to have already done the lookup to find the DWO file).
dee91e82
DE
7764
7765 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7766 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7767
7768 We fill in THIS_CU->length.
7769
7770 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7771 linker) then DIE_READER_FUNC will not get called.
7772
7773 THIS_CU->cu is always freed when done.
3019eac3
DE
7774 This is done in order to not leave THIS_CU->cu in a state where we have
7775 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7776
7777static void
7778init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7779 struct dwo_file *dwo_file,
dee91e82
DE
7780 die_reader_func_ftype *die_reader_func,
7781 void *data)
7782{
ed2dc618 7783 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7784 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7785 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7786 bfd *abfd = get_section_bfd_owner (section);
33e80786 7787 struct dwarf2_section_info *abbrev_section;
d521ce57 7788 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7789 struct die_reader_specs reader;
dee91e82
DE
7790 struct die_info *comp_unit_die;
7791 int has_children;
7792
b4f54984 7793 if (dwarf_die_debug)
9d8780f0 7794 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7795 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7796 sect_offset_str (this_cu->sect_off));
09406207 7797
dee91e82
DE
7798 gdb_assert (this_cu->cu == NULL);
7799
33e80786
DE
7800 abbrev_section = (dwo_file != NULL
7801 ? &dwo_file->sections.abbrev
7802 : get_abbrev_section_for_cu (this_cu));
7803
dee91e82
DE
7804 /* This is cheap if the section is already read in. */
7805 dwarf2_read_section (objfile, section);
7806
fcd3b13d 7807 struct dwarf2_cu cu (this_cu);
dee91e82 7808
9c541725 7809 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7810 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7811 &cu.header, section,
4bdcc0c1 7812 abbrev_section, info_ptr,
43988095
JK
7813 (this_cu->is_debug_types
7814 ? rcuh_kind::TYPE
7815 : rcuh_kind::COMPILE));
dee91e82 7816
1ce1cefd 7817 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7818
7819 /* Skip dummy compilation units. */
7820 if (info_ptr >= begin_info_ptr + this_cu->length
7821 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7822 return;
72bf9492 7823
685af9cd
TT
7824 abbrev_table_up abbrev_table
7825 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7826 cu.header.abbrev_sect_off);
dee91e82 7827
685af9cd 7828 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7829 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7830
7831 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7832}
7833
3019eac3
DE
7834/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7835 does not lookup the specified DWO file.
7836 This cannot be used to read DWO files.
dee91e82
DE
7837
7838 THIS_CU->cu is always freed when done.
3019eac3
DE
7839 This is done in order to not leave THIS_CU->cu in a state where we have
7840 to care whether it refers to the "main" CU or the DWO CU.
7841 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7842
7843static void
7844init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7845 die_reader_func_ftype *die_reader_func,
7846 void *data)
7847{
33e80786 7848 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7849}
0018ea6f
DE
7850\f
7851/* Type Unit Groups.
dee91e82 7852
0018ea6f
DE
7853 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7854 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7855 so that all types coming from the same compilation (.o file) are grouped
7856 together. A future step could be to put the types in the same symtab as
7857 the CU the types ultimately came from. */
ff013f42 7858
f4dc4d17
DE
7859static hashval_t
7860hash_type_unit_group (const void *item)
7861{
9a3c8263
SM
7862 const struct type_unit_group *tu_group
7863 = (const struct type_unit_group *) item;
f4dc4d17 7864
094b34ac 7865 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7866}
348e048f
DE
7867
7868static int
f4dc4d17 7869eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7870{
9a3c8263
SM
7871 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7872 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7873
094b34ac 7874 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7875}
348e048f 7876
f4dc4d17
DE
7877/* Allocate a hash table for type unit groups. */
7878
7879static htab_t
ed2dc618 7880allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7881{
7882 return htab_create_alloc_ex (3,
7883 hash_type_unit_group,
7884 eq_type_unit_group,
7885 NULL,
ed2dc618 7886 &objfile->objfile_obstack,
f4dc4d17
DE
7887 hashtab_obstack_allocate,
7888 dummy_obstack_deallocate);
7889}
dee91e82 7890
f4dc4d17
DE
7891/* Type units that don't have DW_AT_stmt_list are grouped into their own
7892 partial symtabs. We combine several TUs per psymtab to not let the size
7893 of any one psymtab grow too big. */
7894#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7895#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7896
094b34ac 7897/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7898 Create the type_unit_group object used to hold one or more TUs. */
7899
7900static struct type_unit_group *
094b34ac 7901create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7902{
518817b3
SM
7903 struct dwarf2_per_objfile *dwarf2_per_objfile
7904 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7905 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7906 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7907 struct type_unit_group *tu_group;
f4dc4d17
DE
7908
7909 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7910 struct type_unit_group);
094b34ac 7911 per_cu = &tu_group->per_cu;
518817b3 7912 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7913
094b34ac
DE
7914 if (dwarf2_per_objfile->using_index)
7915 {
7916 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7917 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7918 }
7919 else
7920 {
9c541725 7921 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7922 struct partial_symtab *pst;
528e1572 7923 std::string name;
094b34ac
DE
7924
7925 /* Give the symtab a useful name for debug purposes. */
7926 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7927 name = string_printf ("<type_units_%d>",
7928 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7929 else
528e1572 7930 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7931
528e1572 7932 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7933 pst->anonymous = 1;
094b34ac 7934 }
f4dc4d17 7935
094b34ac 7936 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7937 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7938
7939 return tu_group;
7940}
7941
094b34ac
DE
7942/* Look up the type_unit_group for type unit CU, and create it if necessary.
7943 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7944
7945static struct type_unit_group *
ff39bb5e 7946get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7947{
518817b3
SM
7948 struct dwarf2_per_objfile *dwarf2_per_objfile
7949 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7950 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7951 struct type_unit_group *tu_group;
7952 void **slot;
7953 unsigned int line_offset;
7954 struct type_unit_group type_unit_group_for_lookup;
7955
7956 if (dwarf2_per_objfile->type_unit_groups == NULL)
7957 {
7958 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7959 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7960 }
7961
7962 /* Do we need to create a new group, or can we use an existing one? */
7963
7964 if (stmt_list)
7965 {
7966 line_offset = DW_UNSND (stmt_list);
7967 ++tu_stats->nr_symtab_sharers;
7968 }
7969 else
7970 {
7971 /* Ugh, no stmt_list. Rare, but we have to handle it.
7972 We can do various things here like create one group per TU or
7973 spread them over multiple groups to split up the expansion work.
7974 To avoid worst case scenarios (too many groups or too large groups)
7975 we, umm, group them in bunches. */
7976 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7977 | (tu_stats->nr_stmt_less_type_units
7978 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7979 ++tu_stats->nr_stmt_less_type_units;
7980 }
7981
094b34ac 7982 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7983 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7984 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7985 &type_unit_group_for_lookup, INSERT);
7986 if (*slot != NULL)
7987 {
9a3c8263 7988 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7989 gdb_assert (tu_group != NULL);
7990 }
7991 else
7992 {
9c541725 7993 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7994 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7995 *slot = tu_group;
7996 ++tu_stats->nr_symtabs;
7997 }
7998
7999 return tu_group;
8000}
0018ea6f
DE
8001\f
8002/* Partial symbol tables. */
8003
8004/* Create a psymtab named NAME and assign it to PER_CU.
8005
8006 The caller must fill in the following details:
8007 dirname, textlow, texthigh. */
8008
8009static struct partial_symtab *
8010create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8011{
e3b94546 8012 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8013 struct partial_symtab *pst;
8014
939652a5 8015 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
8016
8017 pst->psymtabs_addrmap_supported = 1;
8018
8019 /* This is the glue that links PST into GDB's symbol API. */
8020 pst->read_symtab_private = per_cu;
8021 pst->read_symtab = dwarf2_read_symtab;
8022 per_cu->v.psymtab = pst;
8023
8024 return pst;
8025}
8026
b93601f3
TT
8027/* The DATA object passed to process_psymtab_comp_unit_reader has this
8028 type. */
8029
8030struct process_psymtab_comp_unit_data
8031{
8032 /* True if we are reading a DW_TAG_partial_unit. */
8033
8034 int want_partial_unit;
8035
8036 /* The "pretend" language that is used if the CU doesn't declare a
8037 language. */
8038
8039 enum language pretend_language;
8040};
8041
0018ea6f
DE
8042/* die_reader_func for process_psymtab_comp_unit. */
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,
8049 void *data)
8050{
8051 struct dwarf2_cu *cu = reader->cu;
518817b3 8052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8053 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8054 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8055 CORE_ADDR baseaddr;
8056 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8057 struct partial_symtab *pst;
3a2b436a 8058 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8059 const char *filename;
9a3c8263
SM
8060 struct process_psymtab_comp_unit_data *info
8061 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8062
b93601f3 8063 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8064 return;
8065
8066 gdb_assert (! per_cu->is_debug_types);
8067
b93601f3 8068 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8069
0018ea6f 8070 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8071 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8072 if (filename == NULL)
0018ea6f 8073 filename = "";
0018ea6f
DE
8074
8075 pst = create_partial_symtab (per_cu, filename);
8076
8077 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8078 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8079
8080 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8081
8082 dwarf2_find_base_address (comp_unit_die, cu);
8083
8084 /* Possibly set the default values of LOWPC and HIGHPC from
8085 `DW_AT_ranges'. */
3a2b436a
JK
8086 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8087 &best_highpc, cu, pst);
8088 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8089 {
8090 CORE_ADDR low
8091 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8092 - baseaddr);
8093 CORE_ADDR high
8094 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8095 - baseaddr - 1);
8096 /* Store the contiguous range if it is not empty; it can be
8097 empty for CUs with no code. */
d320c2b5
TT
8098 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8099 low, high, pst);
79748972 8100 }
0018ea6f
DE
8101
8102 /* Check if comp unit has_children.
8103 If so, read the rest of the partial symbols from this comp unit.
8104 If not, there's no more debug_info for this comp unit. */
8105 if (has_children)
8106 {
8107 struct partial_die_info *first_die;
8108 CORE_ADDR lowpc, highpc;
8109
8110 lowpc = ((CORE_ADDR) -1);
8111 highpc = ((CORE_ADDR) 0);
8112
8113 first_die = load_partial_dies (reader, info_ptr, 1);
8114
8115 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8116 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8117
8118 /* If we didn't find a lowpc, set it to highpc to avoid
8119 complaints from `maint check'. */
8120 if (lowpc == ((CORE_ADDR) -1))
8121 lowpc = highpc;
8122
8123 /* If the compilation unit didn't have an explicit address range,
8124 then use the information extracted from its child dies. */
e385593e 8125 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8126 {
8127 best_lowpc = lowpc;
8128 best_highpc = highpc;
8129 }
8130 }
4ae976d1 8131 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8132 best_lowpc + baseaddr)
8133 - baseaddr);
4ae976d1 8134 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8135 best_highpc + baseaddr)
8136 - baseaddr);
0018ea6f 8137
8763cede 8138 end_psymtab_common (objfile, pst);
0018ea6f 8139
ae640021 8140 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8141 {
8142 int i;
ae640021 8143 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8144
8145 /* Fill in 'dependencies' here; we fill in 'users' in a
8146 post-pass. */
8147 pst->number_of_dependencies = len;
a9342b62
TT
8148 pst->dependencies
8149 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8150 for (i = 0; i < len; ++i)
8151 {
8152 pst->dependencies[i]
8153 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8154 }
0018ea6f 8155
ae640021 8156 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8157 }
8158
8159 /* Get the list of files included in the current compilation unit,
8160 and build a psymtab for each of them. */
8161 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8162
b4f54984 8163 if (dwarf_read_debug)
b926417a
TT
8164 fprintf_unfiltered (gdb_stdlog,
8165 "Psymtab for %s unit @%s: %s - %s"
8166 ", %d global, %d static syms\n",
8167 per_cu->is_debug_types ? "type" : "comp",
8168 sect_offset_str (per_cu->sect_off),
8169 paddress (gdbarch, pst->text_low (objfile)),
8170 paddress (gdbarch, pst->text_high (objfile)),
8171 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8172}
8173
8174/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8175 Process compilation unit THIS_CU for a psymtab. */
8176
8177static void
8178process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8179 int want_partial_unit,
8180 enum language pretend_language)
0018ea6f
DE
8181{
8182 /* If this compilation unit was already read in, free the
8183 cached copy in order to read it in again. This is
8184 necessary because we skipped some symbols when we first
8185 read in the compilation unit (see load_partial_dies).
8186 This problem could be avoided, but the benefit is unclear. */
8187 if (this_cu->cu != NULL)
8188 free_one_cached_comp_unit (this_cu);
8189
f1902523 8190 if (this_cu->is_debug_types)
58f0c718
TT
8191 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8192 build_type_psymtabs_reader, NULL);
f1902523
JK
8193 else
8194 {
8195 process_psymtab_comp_unit_data info;
8196 info.want_partial_unit = want_partial_unit;
8197 info.pretend_language = pretend_language;
58f0c718 8198 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8199 process_psymtab_comp_unit_reader, &info);
8200 }
0018ea6f
DE
8201
8202 /* Age out any secondary CUs. */
ed2dc618 8203 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8204}
f4dc4d17
DE
8205
8206/* Reader function for build_type_psymtabs. */
8207
8208static void
8209build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8210 const gdb_byte *info_ptr,
f4dc4d17
DE
8211 struct die_info *type_unit_die,
8212 int has_children,
8213 void *data)
8214{
ed2dc618 8215 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8216 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8217 struct objfile *objfile = dwarf2_per_objfile->objfile;
8218 struct dwarf2_cu *cu = reader->cu;
8219 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8220 struct signatured_type *sig_type;
f4dc4d17
DE
8221 struct type_unit_group *tu_group;
8222 struct attribute *attr;
8223 struct partial_die_info *first_die;
8224 CORE_ADDR lowpc, highpc;
8225 struct partial_symtab *pst;
8226
8227 gdb_assert (data == NULL);
0186c6a7
DE
8228 gdb_assert (per_cu->is_debug_types);
8229 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8230
8231 if (! has_children)
8232 return;
8233
8234 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8235 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8236
df07e2c7 8237 if (tu_group->tus == nullptr)
a8b3b8e9 8238 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8239 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8240
8241 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8242 pst = create_partial_symtab (per_cu, "");
8243 pst->anonymous = 1;
8244
8245 first_die = load_partial_dies (reader, info_ptr, 1);
8246
8247 lowpc = (CORE_ADDR) -1;
8248 highpc = (CORE_ADDR) 0;
8249 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8250
8763cede 8251 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8252}
8253
73051182
DE
8254/* Struct used to sort TUs by their abbreviation table offset. */
8255
8256struct tu_abbrev_offset
8257{
b2bdb8cf
SM
8258 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8259 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8260 {}
8261
8262 signatured_type *sig_type;
73051182
DE
8263 sect_offset abbrev_offset;
8264};
8265
484cf504 8266/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8267
484cf504
TT
8268static bool
8269sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8270 const struct tu_abbrev_offset &b)
73051182 8271{
484cf504 8272 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8273}
8274
8275/* Efficiently read all the type units.
8276 This does the bulk of the work for build_type_psymtabs.
8277
8278 The efficiency is because we sort TUs by the abbrev table they use and
8279 only read each abbrev table once. In one program there are 200K TUs
8280 sharing 8K abbrev tables.
8281
8282 The main purpose of this function is to support building the
8283 dwarf2_per_objfile->type_unit_groups table.
8284 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8285 can collapse the search space by grouping them by stmt_list.
8286 The savings can be significant, in the same program from above the 200K TUs
8287 share 8K stmt_list tables.
8288
8289 FUNC is expected to call get_type_unit_group, which will create the
8290 struct type_unit_group if necessary and add it to
8291 dwarf2_per_objfile->type_unit_groups. */
8292
8293static void
ed2dc618 8294build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8295{
73051182 8296 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8297 abbrev_table_up abbrev_table;
73051182 8298 sect_offset abbrev_offset;
73051182
DE
8299
8300 /* It's up to the caller to not call us multiple times. */
8301 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8302
b2bdb8cf 8303 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8304 return;
8305
8306 /* TUs typically share abbrev tables, and there can be way more TUs than
8307 abbrev tables. Sort by abbrev table to reduce the number of times we
8308 read each abbrev table in.
8309 Alternatives are to punt or to maintain a cache of abbrev tables.
8310 This is simpler and efficient enough for now.
8311
8312 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8313 symtab to use). Typically TUs with the same abbrev offset have the same
8314 stmt_list value too so in practice this should work well.
8315
8316 The basic algorithm here is:
8317
8318 sort TUs by abbrev table
8319 for each TU with same abbrev table:
8320 read abbrev table if first user
8321 read TU top level DIE
8322 [IWBN if DWO skeletons had DW_AT_stmt_list]
8323 call FUNC */
8324
b4f54984 8325 if (dwarf_read_debug)
73051182
DE
8326 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8327
8328 /* Sort in a separate table to maintain the order of all_type_units
8329 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8330 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8331 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8332
8333 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8334 sorted_by_abbrev.emplace_back
8335 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8336 sig_type->per_cu.section,
8337 sig_type->per_cu.sect_off));
73051182 8338
484cf504
TT
8339 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8340 sort_tu_by_abbrev_offset);
73051182 8341
9c541725 8342 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8343
b2bdb8cf 8344 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8345 {
73051182
DE
8346 /* Switch to the next abbrev table if necessary. */
8347 if (abbrev_table == NULL
b2bdb8cf 8348 || tu.abbrev_offset != abbrev_offset)
73051182 8349 {
b2bdb8cf 8350 abbrev_offset = tu.abbrev_offset;
73051182 8351 abbrev_table =
ed2dc618
SM
8352 abbrev_table_read_table (dwarf2_per_objfile,
8353 &dwarf2_per_objfile->abbrev,
73051182
DE
8354 abbrev_offset);
8355 ++tu_stats->nr_uniq_abbrev_tables;
8356 }
8357
b2bdb8cf 8358 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8359 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8360 }
6aa5f3a6 8361}
73051182 8362
6aa5f3a6
DE
8363/* Print collected type unit statistics. */
8364
8365static void
ed2dc618 8366print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8367{
8368 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8369
8370 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8371 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8372 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8373 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8374 tu_stats->nr_uniq_abbrev_tables);
8375 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8376 tu_stats->nr_symtabs);
8377 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8378 tu_stats->nr_symtab_sharers);
8379 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8380 tu_stats->nr_stmt_less_type_units);
8381 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8382 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8383}
8384
f4dc4d17
DE
8385/* Traversal function for build_type_psymtabs. */
8386
8387static int
8388build_type_psymtab_dependencies (void **slot, void *info)
8389{
ed2dc618
SM
8390 struct dwarf2_per_objfile *dwarf2_per_objfile
8391 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8392 struct objfile *objfile = dwarf2_per_objfile->objfile;
8393 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8394 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8395 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8396 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8397 int i;
8398
8399 gdb_assert (len > 0);
0186c6a7 8400 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8401
8402 pst->number_of_dependencies = len;
a9342b62 8403 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8404 for (i = 0; i < len; ++i)
f4dc4d17 8405 {
df07e2c7 8406 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8407 gdb_assert (iter->per_cu.is_debug_types);
8408 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8409 iter->type_unit_group = tu_group;
f4dc4d17
DE
8410 }
8411
df07e2c7
AB
8412 delete tu_group->tus;
8413 tu_group->tus = nullptr;
348e048f
DE
8414
8415 return 1;
8416}
8417
8418/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8419 Build partial symbol tables for the .debug_types comp-units. */
8420
8421static void
ed2dc618 8422build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8423{
ed2dc618 8424 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8425 return;
8426
ed2dc618 8427 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8428}
f4dc4d17 8429
6aa5f3a6
DE
8430/* Traversal function for process_skeletonless_type_unit.
8431 Read a TU in a DWO file and build partial symbols for it. */
8432
8433static int
8434process_skeletonless_type_unit (void **slot, void *info)
8435{
8436 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8437 struct dwarf2_per_objfile *dwarf2_per_objfile
8438 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8439 struct signatured_type find_entry, *entry;
8440
8441 /* If this TU doesn't exist in the global table, add it and read it in. */
8442
8443 if (dwarf2_per_objfile->signatured_types == NULL)
8444 {
8445 dwarf2_per_objfile->signatured_types
ed2dc618 8446 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8447 }
8448
8449 find_entry.signature = dwo_unit->signature;
8450 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8451 INSERT);
8452 /* If we've already seen this type there's nothing to do. What's happening
8453 is we're doing our own version of comdat-folding here. */
8454 if (*slot != NULL)
8455 return 1;
8456
8457 /* This does the job that create_all_type_units would have done for
8458 this TU. */
ed2dc618
SM
8459 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8460 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8461 *slot = entry;
8462
8463 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8464 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8465 build_type_psymtabs_reader, NULL);
8466
8467 return 1;
8468}
8469
8470/* Traversal function for process_skeletonless_type_units. */
8471
8472static int
8473process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8474{
8475 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8476
8477 if (dwo_file->tus != NULL)
8478 {
8479 htab_traverse_noresize (dwo_file->tus,
8480 process_skeletonless_type_unit, info);
8481 }
8482
8483 return 1;
8484}
8485
8486/* Scan all TUs of DWO files, verifying we've processed them.
8487 This is needed in case a TU was emitted without its skeleton.
8488 Note: This can't be done until we know what all the DWO files are. */
8489
8490static void
ed2dc618 8491process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8492{
8493 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8494 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8495 && dwarf2_per_objfile->dwo_files != NULL)
8496 {
51ac9db5 8497 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8498 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8499 dwarf2_per_objfile);
6aa5f3a6 8500 }
348e048f
DE
8501}
8502
ed2dc618 8503/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8504
8505static void
ed2dc618 8506set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8507{
b76e467d 8508 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8509 {
95554aad 8510 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8511
36586728
TT
8512 if (pst == NULL)
8513 continue;
8514
b76e467d 8515 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8516 {
8517 /* Set the 'user' field only if it is not already set. */
8518 if (pst->dependencies[j]->user == NULL)
8519 pst->dependencies[j]->user = pst;
8520 }
8521 }
8522}
8523
93311388
DE
8524/* Build the partial symbol table by doing a quick pass through the
8525 .debug_info and .debug_abbrev sections. */
72bf9492 8526
93311388 8527static void
ed2dc618 8528dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8529{
ed2dc618 8530 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8531
b4f54984 8532 if (dwarf_read_debug)
45cfd468
DE
8533 {
8534 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8535 objfile_name (objfile));
45cfd468
DE
8536 }
8537
98bfdba5
PA
8538 dwarf2_per_objfile->reading_partial_symbols = 1;
8539
be391dca 8540 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8541
93311388
DE
8542 /* Any cached compilation units will be linked by the per-objfile
8543 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8544 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8545
ed2dc618 8546 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8547
ed2dc618 8548 create_all_comp_units (dwarf2_per_objfile);
c906108c 8549
60606b2c
TT
8550 /* Create a temporary address map on a temporary obstack. We later
8551 copy this to the final obstack. */
8268c778 8552 auto_obstack temp_obstack;
791afaa2
TT
8553
8554 scoped_restore save_psymtabs_addrmap
d320c2b5 8555 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8556 addrmap_create_mutable (&temp_obstack));
72bf9492 8557
b76e467d
SM
8558 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8559 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8560
6aa5f3a6 8561 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8562 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8563
8564 /* Now that all TUs have been processed we can fill in the dependencies. */
8565 if (dwarf2_per_objfile->type_unit_groups != NULL)
8566 {
8567 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8568 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8569 }
8570
b4f54984 8571 if (dwarf_read_debug)
ed2dc618 8572 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8573
ed2dc618 8574 set_partial_user (dwarf2_per_objfile);
95554aad 8575
d320c2b5
TT
8576 objfile->partial_symtabs->psymtabs_addrmap
8577 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8578 objfile->partial_symtabs->obstack ());
791afaa2
TT
8579 /* At this point we want to keep the address map. */
8580 save_psymtabs_addrmap.release ();
ff013f42 8581
b4f54984 8582 if (dwarf_read_debug)
45cfd468 8583 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8584 objfile_name (objfile));
ae038cb0
DJ
8585}
8586
3019eac3 8587/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8588
8589static void
dee91e82 8590load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8591 const gdb_byte *info_ptr,
dee91e82
DE
8592 struct die_info *comp_unit_die,
8593 int has_children,
8594 void *data)
ae038cb0 8595{
dee91e82 8596 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8597
95554aad 8598 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8599
ae038cb0
DJ
8600 /* Check if comp unit has_children.
8601 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8602 If not, there's no more debug_info for this comp unit. */
d85a05f0 8603 if (has_children)
dee91e82
DE
8604 load_partial_dies (reader, info_ptr, 0);
8605}
98bfdba5 8606
dee91e82
DE
8607/* Load the partial DIEs for a secondary CU into memory.
8608 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8609
dee91e82
DE
8610static void
8611load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8612{
58f0c718 8613 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8614 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8615}
8616
ae038cb0 8617static void
ed2dc618 8618read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8619 struct dwarf2_section_info *section,
f1902523 8620 struct dwarf2_section_info *abbrev_section,
b76e467d 8621 unsigned int is_dwz)
ae038cb0 8622{
d521ce57 8623 const gdb_byte *info_ptr;
ed2dc618 8624 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8625
b4f54984 8626 if (dwarf_read_debug)
bf6af496 8627 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8628 get_section_name (section),
8629 get_section_file_name (section));
bf6af496 8630
36586728 8631 dwarf2_read_section (objfile, section);
ae038cb0 8632
36586728 8633 info_ptr = section->buffer;
6e70227d 8634
36586728 8635 while (info_ptr < section->buffer + section->size)
ae038cb0 8636 {
ae038cb0 8637 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8638
9c541725 8639 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8640
f1902523 8641 comp_unit_head cu_header;
ed2dc618
SM
8642 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8643 abbrev_section, info_ptr,
8644 rcuh_kind::COMPILE);
ae038cb0
DJ
8645
8646 /* Save the compilation unit for later lookup. */
f1902523
JK
8647 if (cu_header.unit_type != DW_UT_type)
8648 {
8649 this_cu = XOBNEW (&objfile->objfile_obstack,
8650 struct dwarf2_per_cu_data);
8651 memset (this_cu, 0, sizeof (*this_cu));
8652 }
8653 else
8654 {
8655 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8656 struct signatured_type);
8657 memset (sig_type, 0, sizeof (*sig_type));
8658 sig_type->signature = cu_header.signature;
8659 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8660 this_cu = &sig_type->per_cu;
8661 }
8662 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8663 this_cu->sect_off = sect_off;
f1902523 8664 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8665 this_cu->is_dwz = is_dwz;
e3b94546 8666 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8667 this_cu->section = section;
ae038cb0 8668
b76e467d 8669 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8670
8671 info_ptr = info_ptr + this_cu->length;
8672 }
36586728
TT
8673}
8674
8675/* Create a list of all compilation units in OBJFILE.
8676 This is only done for -readnow and building partial symtabs. */
8677
8678static void
ed2dc618 8679create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8680{
b76e467d 8681 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8682 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8683 &dwarf2_per_objfile->abbrev, 0);
36586728 8684
b76e467d 8685 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8686 if (dwz != NULL)
ed2dc618 8687 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8688 1);
c906108c
SS
8689}
8690
5734ee8b 8691/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8692 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8693 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8694 DW_AT_ranges). See the comments of add_partial_subprogram on how
8695 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8696
72bf9492
DJ
8697static void
8698scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8699 CORE_ADDR *highpc, int set_addrmap,
8700 struct dwarf2_cu *cu)
c906108c 8701{
72bf9492 8702 struct partial_die_info *pdi;
c906108c 8703
91c24f0a
DC
8704 /* Now, march along the PDI's, descending into ones which have
8705 interesting children but skipping the children of the other ones,
8706 until we reach the end of the compilation unit. */
c906108c 8707
72bf9492 8708 pdi = first_die;
91c24f0a 8709
72bf9492
DJ
8710 while (pdi != NULL)
8711 {
52356b79 8712 pdi->fixup (cu);
c906108c 8713
f55ee35c 8714 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8715 children, so we need to look at them. Ditto for anonymous
8716 enums. */
933c6fe4 8717
72bf9492 8718 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8719 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8720 || pdi->tag == DW_TAG_imported_unit
8721 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8722 {
72bf9492 8723 switch (pdi->tag)
c906108c
SS
8724 {
8725 case DW_TAG_subprogram:
b1dc1806 8726 case DW_TAG_inlined_subroutine:
cdc07690 8727 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8728 break;
72929c62 8729 case DW_TAG_constant:
c906108c
SS
8730 case DW_TAG_variable:
8731 case DW_TAG_typedef:
91c24f0a 8732 case DW_TAG_union_type:
72bf9492 8733 if (!pdi->is_declaration)
63d06c5c 8734 {
72bf9492 8735 add_partial_symbol (pdi, cu);
63d06c5c
DC
8736 }
8737 break;
c906108c 8738 case DW_TAG_class_type:
680b30c7 8739 case DW_TAG_interface_type:
c906108c 8740 case DW_TAG_structure_type:
72bf9492 8741 if (!pdi->is_declaration)
c906108c 8742 {
72bf9492 8743 add_partial_symbol (pdi, cu);
c906108c 8744 }
b7fee5a3
KS
8745 if ((cu->language == language_rust
8746 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8747 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8748 set_addrmap, cu);
c906108c 8749 break;
91c24f0a 8750 case DW_TAG_enumeration_type:
72bf9492
DJ
8751 if (!pdi->is_declaration)
8752 add_partial_enumeration (pdi, cu);
c906108c
SS
8753 break;
8754 case DW_TAG_base_type:
a02abb62 8755 case DW_TAG_subrange_type:
c906108c 8756 /* File scope base type definitions are added to the partial
c5aa993b 8757 symbol table. */
72bf9492 8758 add_partial_symbol (pdi, cu);
c906108c 8759 break;
d9fa45fe 8760 case DW_TAG_namespace:
cdc07690 8761 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8762 break;
5d7cb8df 8763 case DW_TAG_module:
59c35742
AB
8764 if (!pdi->is_declaration)
8765 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8766 break;
95554aad
TT
8767 case DW_TAG_imported_unit:
8768 {
8769 struct dwarf2_per_cu_data *per_cu;
8770
f4dc4d17
DE
8771 /* For now we don't handle imported units in type units. */
8772 if (cu->per_cu->is_debug_types)
8773 {
8774 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8775 " supported in type units [in module %s]"),
518817b3 8776 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8777 }
8778
e3b94546
SM
8779 per_cu = dwarf2_find_containing_comp_unit
8780 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8781 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8782
8783 /* Go read the partial unit, if needed. */
8784 if (per_cu->v.psymtab == NULL)
b93601f3 8785 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8786
ae640021 8787 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8788 }
8789 break;
74921315
KS
8790 case DW_TAG_imported_declaration:
8791 add_partial_symbol (pdi, cu);
8792 break;
c906108c
SS
8793 default:
8794 break;
8795 }
8796 }
8797
72bf9492
DJ
8798 /* If the die has a sibling, skip to the sibling. */
8799
8800 pdi = pdi->die_sibling;
8801 }
8802}
8803
8804/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8805
72bf9492 8806 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8807 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8808 Enumerators are an exception; they use the scope of their parent
8809 enumeration type, i.e. the name of the enumeration type is not
8810 prepended to the enumerator.
91c24f0a 8811
72bf9492
DJ
8812 There are two complexities. One is DW_AT_specification; in this
8813 case "parent" means the parent of the target of the specification,
8814 instead of the direct parent of the DIE. The other is compilers
8815 which do not emit DW_TAG_namespace; in this case we try to guess
8816 the fully qualified name of structure types from their members'
8817 linkage names. This must be done using the DIE's children rather
8818 than the children of any DW_AT_specification target. We only need
8819 to do this for structures at the top level, i.e. if the target of
8820 any DW_AT_specification (if any; otherwise the DIE itself) does not
8821 have a parent. */
8822
8823/* Compute the scope prefix associated with PDI's parent, in
8824 compilation unit CU. The result will be allocated on CU's
8825 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8826 field. NULL is returned if no prefix is necessary. */
15d034d0 8827static const char *
72bf9492
DJ
8828partial_die_parent_scope (struct partial_die_info *pdi,
8829 struct dwarf2_cu *cu)
8830{
15d034d0 8831 const char *grandparent_scope;
72bf9492 8832 struct partial_die_info *parent, *real_pdi;
91c24f0a 8833
72bf9492
DJ
8834 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8835 then this means the parent of the specification DIE. */
8836
8837 real_pdi = pdi;
72bf9492 8838 while (real_pdi->has_specification)
fb816e8b 8839 {
122cf0f2
AB
8840 auto res = find_partial_die (real_pdi->spec_offset,
8841 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8842 real_pdi = res.pdi;
8843 cu = res.cu;
8844 }
72bf9492
DJ
8845
8846 parent = real_pdi->die_parent;
8847 if (parent == NULL)
8848 return NULL;
8849
8850 if (parent->scope_set)
8851 return parent->scope;
8852
52356b79 8853 parent->fixup (cu);
72bf9492 8854
10b3939b 8855 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8856
acebe513
UW
8857 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8858 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8859 Work around this problem here. */
8860 if (cu->language == language_cplus
6e70227d 8861 && parent->tag == DW_TAG_namespace
acebe513
UW
8862 && strcmp (parent->name, "::") == 0
8863 && grandparent_scope == NULL)
8864 {
8865 parent->scope = NULL;
8866 parent->scope_set = 1;
8867 return NULL;
8868 }
8869
0a4b0913 8870 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8871 if (pdi->tag == DW_TAG_enumerator)
8872 /* Enumerators should not get the name of the enumeration as a prefix. */
8873 parent->scope = grandparent_scope;
8874 else if (parent->tag == DW_TAG_namespace
f55ee35c 8875 || parent->tag == DW_TAG_module
72bf9492
DJ
8876 || parent->tag == DW_TAG_structure_type
8877 || parent->tag == DW_TAG_class_type
680b30c7 8878 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8879 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8880 || parent->tag == DW_TAG_enumeration_type
8881 || (cu->language == language_fortran
8882 && parent->tag == DW_TAG_subprogram
8883 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8884 {
8885 if (grandparent_scope == NULL)
8886 parent->scope = parent->name;
8887 else
3e43a32a
MS
8888 parent->scope = typename_concat (&cu->comp_unit_obstack,
8889 grandparent_scope,
f55ee35c 8890 parent->name, 0, cu);
72bf9492 8891 }
72bf9492
DJ
8892 else
8893 {
8894 /* FIXME drow/2004-04-01: What should we be doing with
8895 function-local names? For partial symbols, we should probably be
8896 ignoring them. */
fa9c3fa0
TT
8897 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8898 dwarf_tag_name (parent->tag),
8899 sect_offset_str (pdi->sect_off));
72bf9492 8900 parent->scope = grandparent_scope;
c906108c
SS
8901 }
8902
72bf9492
DJ
8903 parent->scope_set = 1;
8904 return parent->scope;
8905}
8906
8907/* Return the fully scoped name associated with PDI, from compilation unit
8908 CU. The result will be allocated with malloc. */
4568ecf9 8909
72bf9492
DJ
8910static char *
8911partial_die_full_name (struct partial_die_info *pdi,
8912 struct dwarf2_cu *cu)
8913{
15d034d0 8914 const char *parent_scope;
72bf9492 8915
98bfdba5
PA
8916 /* If this is a template instantiation, we can not work out the
8917 template arguments from partial DIEs. So, unfortunately, we have
8918 to go through the full DIEs. At least any work we do building
8919 types here will be reused if full symbols are loaded later. */
8920 if (pdi->has_template_arguments)
8921 {
52356b79 8922 pdi->fixup (cu);
98bfdba5
PA
8923
8924 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8925 {
8926 struct die_info *die;
8927 struct attribute attr;
8928 struct dwarf2_cu *ref_cu = cu;
8929
b64f50a1 8930 /* DW_FORM_ref_addr is using section offset. */
b4069958 8931 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8932 attr.form = DW_FORM_ref_addr;
9c541725 8933 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8934 die = follow_die_ref (NULL, &attr, &ref_cu);
8935
8936 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8937 }
8938 }
8939
72bf9492
DJ
8940 parent_scope = partial_die_parent_scope (pdi, cu);
8941 if (parent_scope == NULL)
8942 return NULL;
8943 else
f55ee35c 8944 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8945}
8946
8947static void
72bf9492 8948add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8949{
518817b3
SM
8950 struct dwarf2_per_objfile *dwarf2_per_objfile
8951 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8952 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8953 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8954 CORE_ADDR addr = 0;
15d034d0 8955 const char *actual_name = NULL;
e142c38c 8956 CORE_ADDR baseaddr;
15d034d0 8957 char *built_actual_name;
e142c38c
DJ
8958
8959 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8960
15d034d0
TT
8961 built_actual_name = partial_die_full_name (pdi, cu);
8962 if (built_actual_name != NULL)
8963 actual_name = built_actual_name;
63d06c5c 8964
72bf9492
DJ
8965 if (actual_name == NULL)
8966 actual_name = pdi->name;
8967
c906108c
SS
8968 switch (pdi->tag)
8969 {
b1dc1806 8970 case DW_TAG_inlined_subroutine:
c906108c 8971 case DW_TAG_subprogram:
79748972
TT
8972 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8973 - baseaddr);
0a4b0913
AB
8974 if (pdi->is_external
8975 || cu->language == language_ada
8976 || (cu->language == language_fortran
8977 && pdi->die_parent != NULL
8978 && pdi->die_parent->tag == DW_TAG_subprogram))
8979 {
8980 /* Normally, only "external" DIEs are part of the global scope.
8981 But in Ada and Fortran, we want to be able to access nested
8982 procedures globally. So all Ada and Fortran subprograms are
8983 stored in the global scope. */
31edb802 8984 add_psymbol_to_list (actual_name,
15d034d0 8985 built_actual_name != NULL,
f47fb265 8986 VAR_DOMAIN, LOC_BLOCK,
79748972 8987 SECT_OFF_TEXT (objfile),
75aedd27 8988 psymbol_placement::GLOBAL,
79748972
TT
8989 addr,
8990 cu->language, objfile);
c906108c
SS
8991 }
8992 else
8993 {
31edb802 8994 add_psymbol_to_list (actual_name,
15d034d0 8995 built_actual_name != NULL,
f47fb265 8996 VAR_DOMAIN, LOC_BLOCK,
79748972 8997 SECT_OFF_TEXT (objfile),
75aedd27 8998 psymbol_placement::STATIC,
1762568f 8999 addr, cu->language, objfile);
c906108c 9000 }
0c1b455e
TT
9001
9002 if (pdi->main_subprogram && actual_name != NULL)
9003 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9004 break;
72929c62 9005 case DW_TAG_constant:
31edb802 9006 add_psymbol_to_list (actual_name,
75aedd27
TT
9007 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9008 -1, (pdi->is_external
9009 ? psymbol_placement::GLOBAL
9010 : psymbol_placement::STATIC),
9011 0, cu->language, objfile);
72929c62 9012 break;
c906108c 9013 case DW_TAG_variable:
95554aad
TT
9014 if (pdi->d.locdesc)
9015 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9016
95554aad 9017 if (pdi->d.locdesc
caac4577
JG
9018 && addr == 0
9019 && !dwarf2_per_objfile->has_section_at_zero)
9020 {
9021 /* A global or static variable may also have been stripped
9022 out by the linker if unused, in which case its address
9023 will be nullified; do not add such variables into partial
9024 symbol table then. */
9025 }
9026 else if (pdi->is_external)
c906108c
SS
9027 {
9028 /* Global Variable.
9029 Don't enter into the minimal symbol tables as there is
9030 a minimal symbol table entry from the ELF symbols already.
9031 Enter into partial symbol table if it has a location
9032 descriptor or a type.
9033 If the location descriptor is missing, new_symbol will create
9034 a LOC_UNRESOLVED symbol, the address of the variable will then
9035 be determined from the minimal symbol table whenever the variable
9036 is referenced.
9037 The address for the partial symbol table entry is not
9038 used by GDB, but it comes in handy for debugging partial symbol
9039 table building. */
9040
95554aad 9041 if (pdi->d.locdesc || pdi->has_type)
31edb802 9042 add_psymbol_to_list (actual_name,
15d034d0 9043 built_actual_name != NULL,
f47fb265 9044 VAR_DOMAIN, LOC_STATIC,
79748972 9045 SECT_OFF_TEXT (objfile),
75aedd27 9046 psymbol_placement::GLOBAL,
79748972 9047 addr, cu->language, objfile);
c906108c
SS
9048 }
9049 else
9050 {
ff908ebf
AW
9051 int has_loc = pdi->d.locdesc != NULL;
9052
9053 /* Static Variable. Skip symbols whose value we cannot know (those
9054 without location descriptors or constant values). */
9055 if (!has_loc && !pdi->has_const_value)
decbce07 9056 {
15d034d0 9057 xfree (built_actual_name);
decbce07
MS
9058 return;
9059 }
ff908ebf 9060
31edb802 9061 add_psymbol_to_list (actual_name,
15d034d0 9062 built_actual_name != NULL,
f47fb265 9063 VAR_DOMAIN, LOC_STATIC,
79748972 9064 SECT_OFF_TEXT (objfile),
75aedd27 9065 psymbol_placement::STATIC,
79748972 9066 has_loc ? addr : 0,
f47fb265 9067 cu->language, objfile);
c906108c
SS
9068 }
9069 break;
9070 case DW_TAG_typedef:
9071 case DW_TAG_base_type:
a02abb62 9072 case DW_TAG_subrange_type:
31edb802 9073 add_psymbol_to_list (actual_name,
15d034d0 9074 built_actual_name != NULL,
79748972 9075 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9076 psymbol_placement::STATIC,
1762568f 9077 0, cu->language, objfile);
c906108c 9078 break;
74921315 9079 case DW_TAG_imported_declaration:
72bf9492 9080 case DW_TAG_namespace:
31edb802 9081 add_psymbol_to_list (actual_name,
15d034d0 9082 built_actual_name != NULL,
79748972 9083 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9084 psymbol_placement::GLOBAL,
1762568f 9085 0, cu->language, objfile);
72bf9492 9086 break;
530e8392 9087 case DW_TAG_module:
a5fd13a9
BH
9088 /* With Fortran 77 there might be a "BLOCK DATA" module
9089 available without any name. If so, we skip the module as it
9090 doesn't bring any value. */
9091 if (actual_name != nullptr)
31edb802 9092 add_psymbol_to_list (actual_name,
a5fd13a9
BH
9093 built_actual_name != NULL,
9094 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9095 psymbol_placement::GLOBAL,
9096 0, cu->language, objfile);
530e8392 9097 break;
c906108c 9098 case DW_TAG_class_type:
680b30c7 9099 case DW_TAG_interface_type:
c906108c
SS
9100 case DW_TAG_structure_type:
9101 case DW_TAG_union_type:
9102 case DW_TAG_enumeration_type:
fa4028e9
JB
9103 /* Skip external references. The DWARF standard says in the section
9104 about "Structure, Union, and Class Type Entries": "An incomplete
9105 structure, union or class type is represented by a structure,
9106 union or class entry that does not have a byte size attribute
9107 and that has a DW_AT_declaration attribute." */
9108 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9109 {
15d034d0 9110 xfree (built_actual_name);
decbce07
MS
9111 return;
9112 }
fa4028e9 9113
63d06c5c
DC
9114 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9115 static vs. global. */
31edb802 9116 add_psymbol_to_list (actual_name,
15d034d0 9117 built_actual_name != NULL,
79748972 9118 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9119 cu->language == language_cplus
75aedd27
TT
9120 ? psymbol_placement::GLOBAL
9121 : psymbol_placement::STATIC,
1762568f 9122 0, cu->language, objfile);
c906108c 9123
c906108c
SS
9124 break;
9125 case DW_TAG_enumerator:
31edb802 9126 add_psymbol_to_list (actual_name,
15d034d0 9127 built_actual_name != NULL,
79748972 9128 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9129 cu->language == language_cplus
75aedd27
TT
9130 ? psymbol_placement::GLOBAL
9131 : psymbol_placement::STATIC,
1762568f 9132 0, cu->language, objfile);
c906108c
SS
9133 break;
9134 default:
9135 break;
9136 }
5c4e30ca 9137
15d034d0 9138 xfree (built_actual_name);
c906108c
SS
9139}
9140
5c4e30ca
DC
9141/* Read a partial die corresponding to a namespace; also, add a symbol
9142 corresponding to that namespace to the symbol table. NAMESPACE is
9143 the name of the enclosing namespace. */
91c24f0a 9144
72bf9492
DJ
9145static void
9146add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9147 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9148 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9149{
72bf9492 9150 /* Add a symbol for the namespace. */
e7c27a73 9151
72bf9492 9152 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9153
9154 /* Now scan partial symbols in that namespace. */
9155
91c24f0a 9156 if (pdi->has_children)
cdc07690 9157 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9158}
9159
5d7cb8df
JK
9160/* Read a partial die corresponding to a Fortran module. */
9161
9162static void
9163add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9164 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9165{
530e8392
KB
9166 /* Add a symbol for the namespace. */
9167
9168 add_partial_symbol (pdi, cu);
9169
f55ee35c 9170 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9171
9172 if (pdi->has_children)
cdc07690 9173 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9174}
9175
b1dc1806
XR
9176/* Read a partial die corresponding to a subprogram or an inlined
9177 subprogram and create a partial symbol for that subprogram.
9178 When the CU language allows it, this routine also defines a partial
9179 symbol for each nested subprogram that this subprogram contains.
9180 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9181 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9182
cdc07690
YQ
9183 PDI may also be a lexical block, in which case we simply search
9184 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9185 Again, this is only performed when the CU language allows this
9186 type of definitions. */
9187
9188static void
9189add_partial_subprogram (struct partial_die_info *pdi,
9190 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9191 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9192{
b1dc1806 9193 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9194 {
9195 if (pdi->has_pc_info)
9196 {
9197 if (pdi->lowpc < *lowpc)
9198 *lowpc = pdi->lowpc;
9199 if (pdi->highpc > *highpc)
9200 *highpc = pdi->highpc;
cdc07690 9201 if (set_addrmap)
5734ee8b 9202 {
518817b3 9203 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9204 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9205 CORE_ADDR baseaddr;
b926417a
TT
9206 CORE_ADDR this_highpc;
9207 CORE_ADDR this_lowpc;
5734ee8b
DJ
9208
9209 baseaddr = ANOFFSET (objfile->section_offsets,
9210 SECT_OFF_TEXT (objfile));
b926417a
TT
9211 this_lowpc
9212 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9213 pdi->lowpc + baseaddr)
9214 - baseaddr);
9215 this_highpc
9216 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9217 pdi->highpc + baseaddr)
9218 - baseaddr);
d320c2b5 9219 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9220 this_lowpc, this_highpc - 1,
9291a0cd 9221 cu->per_cu->v.psymtab);
5734ee8b 9222 }
481860b3
GB
9223 }
9224
9225 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9226 {
bc30ff58 9227 if (!pdi->is_declaration)
e8d05480
JB
9228 /* Ignore subprogram DIEs that do not have a name, they are
9229 illegal. Do not emit a complaint at this point, we will
9230 do so when we convert this psymtab into a symtab. */
9231 if (pdi->name)
9232 add_partial_symbol (pdi, cu);
bc30ff58
JB
9233 }
9234 }
6e70227d 9235
bc30ff58
JB
9236 if (! pdi->has_children)
9237 return;
9238
0a4b0913 9239 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9240 {
9241 pdi = pdi->die_child;
9242 while (pdi != NULL)
9243 {
52356b79 9244 pdi->fixup (cu);
bc30ff58 9245 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9246 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9247 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9248 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9249 pdi = pdi->die_sibling;
9250 }
9251 }
9252}
9253
91c24f0a
DC
9254/* Read a partial die corresponding to an enumeration type. */
9255
72bf9492
DJ
9256static void
9257add_partial_enumeration (struct partial_die_info *enum_pdi,
9258 struct dwarf2_cu *cu)
91c24f0a 9259{
72bf9492 9260 struct partial_die_info *pdi;
91c24f0a
DC
9261
9262 if (enum_pdi->name != NULL)
72bf9492
DJ
9263 add_partial_symbol (enum_pdi, cu);
9264
9265 pdi = enum_pdi->die_child;
9266 while (pdi)
91c24f0a 9267 {
72bf9492 9268 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9269 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9270 else
72bf9492
DJ
9271 add_partial_symbol (pdi, cu);
9272 pdi = pdi->die_sibling;
91c24f0a 9273 }
91c24f0a
DC
9274}
9275
6caca83c
CC
9276/* Return the initial uleb128 in the die at INFO_PTR. */
9277
9278static unsigned int
d521ce57 9279peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9280{
9281 unsigned int bytes_read;
9282
9283 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9284}
9285
685af9cd
TT
9286/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9287 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9288
4bb7a0a7
DJ
9289 Return the corresponding abbrev, or NULL if the number is zero (indicating
9290 an empty DIE). In either case *BYTES_READ will be set to the length of
9291 the initial number. */
9292
9293static struct abbrev_info *
685af9cd
TT
9294peek_die_abbrev (const die_reader_specs &reader,
9295 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9296{
685af9cd 9297 dwarf2_cu *cu = reader.cu;
518817b3 9298 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9299 unsigned int abbrev_number
9300 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9301
9302 if (abbrev_number == 0)
9303 return NULL;
9304
685af9cd 9305 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9306 if (!abbrev)
9307 {
422b9917 9308 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9309 " at offset %s [in module %s]"),
422b9917 9310 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9311 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9312 }
9313
9314 return abbrev;
9315}
9316
93311388
DE
9317/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9318 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9319 DIE. Any children of the skipped DIEs will also be skipped. */
9320
d521ce57
TT
9321static const gdb_byte *
9322skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9323{
4bb7a0a7
DJ
9324 while (1)
9325 {
685af9cd
TT
9326 unsigned int bytes_read;
9327 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9328
4bb7a0a7
DJ
9329 if (abbrev == NULL)
9330 return info_ptr + bytes_read;
9331 else
dee91e82 9332 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9333 }
9334}
9335
93311388
DE
9336/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9337 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9338 abbrev corresponding to that skipped uleb128 should be passed in
9339 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9340 children. */
9341
d521ce57
TT
9342static const gdb_byte *
9343skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9344 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9345{
9346 unsigned int bytes_read;
9347 struct attribute attr;
dee91e82
DE
9348 bfd *abfd = reader->abfd;
9349 struct dwarf2_cu *cu = reader->cu;
d521ce57 9350 const gdb_byte *buffer = reader->buffer;
f664829e 9351 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9352 unsigned int form, i;
9353
9354 for (i = 0; i < abbrev->num_attrs; i++)
9355 {
9356 /* The only abbrev we care about is DW_AT_sibling. */
9357 if (abbrev->attrs[i].name == DW_AT_sibling)
9358 {
dee91e82 9359 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9360 if (attr.form == DW_FORM_ref_addr)
b98664d3 9361 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9362 else
b9502d3f 9363 {
9c541725
PA
9364 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9365 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9366
9367 if (sibling_ptr < info_ptr)
b98664d3 9368 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9369 else if (sibling_ptr > reader->buffer_end)
9370 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9371 else
9372 return sibling_ptr;
9373 }
4bb7a0a7
DJ
9374 }
9375
9376 /* If it isn't DW_AT_sibling, skip this attribute. */
9377 form = abbrev->attrs[i].form;
9378 skip_attribute:
9379 switch (form)
9380 {
4bb7a0a7 9381 case DW_FORM_ref_addr:
ae411497
TT
9382 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9383 and later it is offset sized. */
9384 if (cu->header.version == 2)
9385 info_ptr += cu->header.addr_size;
9386 else
9387 info_ptr += cu->header.offset_size;
9388 break;
36586728
TT
9389 case DW_FORM_GNU_ref_alt:
9390 info_ptr += cu->header.offset_size;
9391 break;
ae411497 9392 case DW_FORM_addr:
4bb7a0a7
DJ
9393 info_ptr += cu->header.addr_size;
9394 break;
9395 case DW_FORM_data1:
9396 case DW_FORM_ref1:
9397 case DW_FORM_flag:
8fe0f950 9398 case DW_FORM_strx1:
4bb7a0a7
DJ
9399 info_ptr += 1;
9400 break;
2dc7f7b3 9401 case DW_FORM_flag_present:
43988095 9402 case DW_FORM_implicit_const:
2dc7f7b3 9403 break;
4bb7a0a7
DJ
9404 case DW_FORM_data2:
9405 case DW_FORM_ref2:
8fe0f950 9406 case DW_FORM_strx2:
4bb7a0a7
DJ
9407 info_ptr += 2;
9408 break;
8fe0f950
AT
9409 case DW_FORM_strx3:
9410 info_ptr += 3;
9411 break;
4bb7a0a7
DJ
9412 case DW_FORM_data4:
9413 case DW_FORM_ref4:
8fe0f950 9414 case DW_FORM_strx4:
4bb7a0a7
DJ
9415 info_ptr += 4;
9416 break;
9417 case DW_FORM_data8:
9418 case DW_FORM_ref8:
55f1336d 9419 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9420 info_ptr += 8;
9421 break;
0224619f
JK
9422 case DW_FORM_data16:
9423 info_ptr += 16;
9424 break;
4bb7a0a7 9425 case DW_FORM_string:
9b1c24c8 9426 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9427 info_ptr += bytes_read;
9428 break;
2dc7f7b3 9429 case DW_FORM_sec_offset:
4bb7a0a7 9430 case DW_FORM_strp:
36586728 9431 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9432 info_ptr += cu->header.offset_size;
9433 break;
2dc7f7b3 9434 case DW_FORM_exprloc:
4bb7a0a7
DJ
9435 case DW_FORM_block:
9436 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9437 info_ptr += bytes_read;
9438 break;
9439 case DW_FORM_block1:
9440 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9441 break;
9442 case DW_FORM_block2:
9443 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9444 break;
9445 case DW_FORM_block4:
9446 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9447 break;
336d760d 9448 case DW_FORM_addrx:
cf532bd1 9449 case DW_FORM_strx:
4bb7a0a7
DJ
9450 case DW_FORM_sdata:
9451 case DW_FORM_udata:
9452 case DW_FORM_ref_udata:
3019eac3
DE
9453 case DW_FORM_GNU_addr_index:
9454 case DW_FORM_GNU_str_index:
d521ce57 9455 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9456 break;
9457 case DW_FORM_indirect:
9458 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9459 info_ptr += bytes_read;
9460 /* We need to continue parsing from here, so just go back to
9461 the top. */
9462 goto skip_attribute;
9463
9464 default:
3e43a32a
MS
9465 error (_("Dwarf Error: Cannot handle %s "
9466 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9467 dwarf_form_name (form),
9468 bfd_get_filename (abfd));
9469 }
9470 }
9471
9472 if (abbrev->has_children)
dee91e82 9473 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9474 else
9475 return info_ptr;
9476}
9477
93311388 9478/* Locate ORIG_PDI's sibling.
dee91e82 9479 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9480
d521ce57 9481static const gdb_byte *
dee91e82
DE
9482locate_pdi_sibling (const struct die_reader_specs *reader,
9483 struct partial_die_info *orig_pdi,
d521ce57 9484 const gdb_byte *info_ptr)
91c24f0a
DC
9485{
9486 /* Do we know the sibling already? */
72bf9492 9487
91c24f0a
DC
9488 if (orig_pdi->sibling)
9489 return orig_pdi->sibling;
9490
9491 /* Are there any children to deal with? */
9492
9493 if (!orig_pdi->has_children)
9494 return info_ptr;
9495
4bb7a0a7 9496 /* Skip the children the long way. */
91c24f0a 9497
dee91e82 9498 return skip_children (reader, info_ptr);
91c24f0a
DC
9499}
9500
257e7a09 9501/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9502 not NULL. */
c906108c
SS
9503
9504static void
257e7a09
YQ
9505dwarf2_read_symtab (struct partial_symtab *self,
9506 struct objfile *objfile)
c906108c 9507{
ed2dc618
SM
9508 struct dwarf2_per_objfile *dwarf2_per_objfile
9509 = get_dwarf2_per_objfile (objfile);
9510
257e7a09 9511 if (self->readin)
c906108c 9512 {
442e4d9c 9513 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9514 self->filename);
442e4d9c
YQ
9515 }
9516 else
9517 {
9518 if (info_verbose)
c906108c 9519 {
442e4d9c 9520 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9521 self->filename);
442e4d9c 9522 gdb_flush (gdb_stdout);
c906108c 9523 }
c906108c 9524
442e4d9c
YQ
9525 /* If this psymtab is constructed from a debug-only objfile, the
9526 has_section_at_zero flag will not necessarily be correct. We
9527 can get the correct value for this flag by looking at the data
9528 associated with the (presumably stripped) associated objfile. */
9529 if (objfile->separate_debug_objfile_backlink)
9530 {
9531 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9532 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9533
442e4d9c
YQ
9534 dwarf2_per_objfile->has_section_at_zero
9535 = dpo_backlink->has_section_at_zero;
9536 }
b2ab525c 9537
442e4d9c 9538 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9539
257e7a09 9540 psymtab_to_symtab_1 (self);
c906108c 9541
442e4d9c
YQ
9542 /* Finish up the debug error message. */
9543 if (info_verbose)
9544 printf_filtered (_("done.\n"));
c906108c 9545 }
95554aad 9546
ed2dc618 9547 process_cu_includes (dwarf2_per_objfile);
c906108c 9548}
9cdd5dbd
DE
9549\f
9550/* Reading in full CUs. */
c906108c 9551
10b3939b
DJ
9552/* Add PER_CU to the queue. */
9553
9554static void
95554aad
TT
9555queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9556 enum language pretend_language)
10b3939b
DJ
9557{
9558 struct dwarf2_queue_item *item;
9559
9560 per_cu->queued = 1;
8d749320 9561 item = XNEW (struct dwarf2_queue_item);
10b3939b 9562 item->per_cu = per_cu;
95554aad 9563 item->pretend_language = pretend_language;
10b3939b
DJ
9564 item->next = NULL;
9565
9566 if (dwarf2_queue == NULL)
9567 dwarf2_queue = item;
9568 else
9569 dwarf2_queue_tail->next = item;
9570
9571 dwarf2_queue_tail = item;
9572}
9573
89e63ee4
DE
9574/* If PER_CU is not yet queued, add it to the queue.
9575 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9576 dependency.
0907af0c 9577 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9578 meaning either PER_CU is already queued or it is already loaded.
9579
9580 N.B. There is an invariant here that if a CU is queued then it is loaded.
9581 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9582
9583static int
89e63ee4 9584maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9585 struct dwarf2_per_cu_data *per_cu,
9586 enum language pretend_language)
9587{
9588 /* We may arrive here during partial symbol reading, if we need full
9589 DIEs to process an unusual case (e.g. template arguments). Do
9590 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9591 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9592 {
9593 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9594 return 1;
9595 return 0;
9596 }
9597
9598 /* Mark the dependence relation so that we don't flush PER_CU
9599 too early. */
89e63ee4
DE
9600 if (dependent_cu != NULL)
9601 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9602
9603 /* If it's already on the queue, we have nothing to do. */
9604 if (per_cu->queued)
9605 return 0;
9606
9607 /* If the compilation unit is already loaded, just mark it as
9608 used. */
9609 if (per_cu->cu != NULL)
9610 {
9611 per_cu->cu->last_used = 0;
9612 return 0;
9613 }
9614
9615 /* Add it to the queue. */
9616 queue_comp_unit (per_cu, pretend_language);
9617
9618 return 1;
9619}
9620
10b3939b
DJ
9621/* Process the queue. */
9622
9623static void
ed2dc618 9624process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9625{
9626 struct dwarf2_queue_item *item, *next_item;
9627
b4f54984 9628 if (dwarf_read_debug)
45cfd468
DE
9629 {
9630 fprintf_unfiltered (gdb_stdlog,
9631 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9632 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9633 }
9634
03dd20cc
DJ
9635 /* The queue starts out with one item, but following a DIE reference
9636 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9637 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9638 {
cc12ce38
DE
9639 if ((dwarf2_per_objfile->using_index
9640 ? !item->per_cu->v.quick->compunit_symtab
9641 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9642 /* Skip dummy CUs. */
9643 && item->per_cu->cu != NULL)
f4dc4d17
DE
9644 {
9645 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9646 unsigned int debug_print_threshold;
247f5c4f 9647 char buf[100];
f4dc4d17 9648
247f5c4f 9649 if (per_cu->is_debug_types)
f4dc4d17 9650 {
247f5c4f
DE
9651 struct signatured_type *sig_type =
9652 (struct signatured_type *) per_cu;
9653
9d8780f0 9654 sprintf (buf, "TU %s at offset %s",
73be47f5 9655 hex_string (sig_type->signature),
9d8780f0 9656 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9657 /* There can be 100s of TUs.
9658 Only print them in verbose mode. */
9659 debug_print_threshold = 2;
f4dc4d17 9660 }
247f5c4f 9661 else
73be47f5 9662 {
9d8780f0
SM
9663 sprintf (buf, "CU at offset %s",
9664 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9665 debug_print_threshold = 1;
9666 }
247f5c4f 9667
b4f54984 9668 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9669 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9670
9671 if (per_cu->is_debug_types)
9672 process_full_type_unit (per_cu, item->pretend_language);
9673 else
9674 process_full_comp_unit (per_cu, item->pretend_language);
9675
b4f54984 9676 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9677 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9678 }
10b3939b
DJ
9679
9680 item->per_cu->queued = 0;
9681 next_item = item->next;
9682 xfree (item);
9683 }
9684
9685 dwarf2_queue_tail = NULL;
45cfd468 9686
b4f54984 9687 if (dwarf_read_debug)
45cfd468
DE
9688 {
9689 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9690 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9691 }
10b3939b
DJ
9692}
9693
10b3939b
DJ
9694/* Read in full symbols for PST, and anything it depends on. */
9695
c906108c 9696static void
fba45db2 9697psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9698{
10b3939b 9699 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9700 int i;
9701
95554aad
TT
9702 if (pst->readin)
9703 return;
9704
aaa75496 9705 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9706 if (!pst->dependencies[i]->readin
9707 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9708 {
9709 /* Inform about additional files that need to be read in. */
9710 if (info_verbose)
9711 {
a3f17187 9712 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9713 fputs_filtered (" ", gdb_stdout);
9714 wrap_here ("");
9715 fputs_filtered ("and ", gdb_stdout);
9716 wrap_here ("");
9717 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9718 wrap_here (""); /* Flush output. */
aaa75496
JB
9719 gdb_flush (gdb_stdout);
9720 }
9721 psymtab_to_symtab_1 (pst->dependencies[i]);
9722 }
9723
9a3c8263 9724 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9725
9726 if (per_cu == NULL)
aaa75496
JB
9727 {
9728 /* It's an include file, no symbols to read for it.
9729 Everything is in the parent symtab. */
9730 pst->readin = 1;
9731 return;
9732 }
c906108c 9733
58f0c718 9734 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9735}
9736
dee91e82
DE
9737/* Trivial hash function for die_info: the hash value of a DIE
9738 is its offset in .debug_info for this objfile. */
10b3939b 9739
dee91e82
DE
9740static hashval_t
9741die_hash (const void *item)
10b3939b 9742{
9a3c8263 9743 const struct die_info *die = (const struct die_info *) item;
6502dd73 9744
9c541725 9745 return to_underlying (die->sect_off);
dee91e82 9746}
63d06c5c 9747
dee91e82
DE
9748/* Trivial comparison function for die_info structures: two DIEs
9749 are equal if they have the same offset. */
98bfdba5 9750
dee91e82
DE
9751static int
9752die_eq (const void *item_lhs, const void *item_rhs)
9753{
9a3c8263
SM
9754 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9755 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9756
9c541725 9757 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9758}
c906108c 9759
dee91e82
DE
9760/* die_reader_func for load_full_comp_unit.
9761 This is identical to read_signatured_type_reader,
9762 but is kept separate for now. */
c906108c 9763
dee91e82
DE
9764static void
9765load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9766 const gdb_byte *info_ptr,
dee91e82
DE
9767 struct die_info *comp_unit_die,
9768 int has_children,
9769 void *data)
9770{
9771 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9772 enum language *language_ptr = (enum language *) data;
6caca83c 9773
dee91e82
DE
9774 gdb_assert (cu->die_hash == NULL);
9775 cu->die_hash =
9776 htab_create_alloc_ex (cu->header.length / 12,
9777 die_hash,
9778 die_eq,
9779 NULL,
9780 &cu->comp_unit_obstack,
9781 hashtab_obstack_allocate,
9782 dummy_obstack_deallocate);
e142c38c 9783
dee91e82
DE
9784 if (has_children)
9785 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9786 &info_ptr, comp_unit_die);
9787 cu->dies = comp_unit_die;
9788 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9789
9790 /* We try not to read any attributes in this function, because not
9cdd5dbd 9791 all CUs needed for references have been loaded yet, and symbol
10b3939b 9792 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9793 or we won't be able to build types correctly.
9794 Similarly, if we do not read the producer, we can not apply
9795 producer-specific interpretation. */
95554aad 9796 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9797}
10b3939b 9798
dee91e82 9799/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9800
dee91e82 9801static void
95554aad 9802load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9803 bool skip_partial,
95554aad 9804 enum language pretend_language)
dee91e82 9805{
3019eac3 9806 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9807
58f0c718 9808 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9809 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9810}
9811
3da10d80
KS
9812/* Add a DIE to the delayed physname list. */
9813
9814static void
9815add_to_method_list (struct type *type, int fnfield_index, int index,
9816 const char *name, struct die_info *die,
9817 struct dwarf2_cu *cu)
9818{
9819 struct delayed_method_info mi;
9820 mi.type = type;
9821 mi.fnfield_index = fnfield_index;
9822 mi.index = index;
9823 mi.name = name;
9824 mi.die = die;
c89b44cd 9825 cu->method_list.push_back (mi);
3da10d80
KS
9826}
9827
3693fdb3
PA
9828/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9829 "const" / "volatile". If so, decrements LEN by the length of the
9830 modifier and return true. Otherwise return false. */
9831
9832template<size_t N>
9833static bool
9834check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9835{
9836 size_t mod_len = sizeof (mod) - 1;
9837 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9838 {
9839 len -= mod_len;
9840 return true;
9841 }
9842 return false;
9843}
9844
3da10d80
KS
9845/* Compute the physnames of any methods on the CU's method list.
9846
9847 The computation of method physnames is delayed in order to avoid the
9848 (bad) condition that one of the method's formal parameters is of an as yet
9849 incomplete type. */
9850
9851static void
9852compute_delayed_physnames (struct dwarf2_cu *cu)
9853{
3693fdb3 9854 /* Only C++ delays computing physnames. */
c89b44cd 9855 if (cu->method_list.empty ())
3693fdb3
PA
9856 return;
9857 gdb_assert (cu->language == language_cplus);
9858
52941706 9859 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9860 {
1d06ead6 9861 const char *physname;
3da10d80 9862 struct fn_fieldlist *fn_flp
c89b44cd
TT
9863 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9864 physname = dwarf2_physname (mi.name, mi.die, cu);
9865 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9866 = physname ? physname : "";
3693fdb3
PA
9867
9868 /* Since there's no tag to indicate whether a method is a
9869 const/volatile overload, extract that information out of the
9870 demangled name. */
9871 if (physname != NULL)
9872 {
9873 size_t len = strlen (physname);
9874
9875 while (1)
9876 {
9877 if (physname[len] == ')') /* shortcut */
9878 break;
9879 else if (check_modifier (physname, len, " const"))
c89b44cd 9880 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9881 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9882 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9883 else
9884 break;
9885 }
9886 }
3da10d80 9887 }
c89b44cd
TT
9888
9889 /* The list is no longer needed. */
9890 cu->method_list.clear ();
3da10d80
KS
9891}
9892
a766d390
DE
9893/* Go objects should be embedded in a DW_TAG_module DIE,
9894 and it's not clear if/how imported objects will appear.
9895 To keep Go support simple until that's worked out,
9896 go back through what we've read and create something usable.
9897 We could do this while processing each DIE, and feels kinda cleaner,
9898 but that way is more invasive.
9899 This is to, for example, allow the user to type "p var" or "b main"
9900 without having to specify the package name, and allow lookups
9901 of module.object to work in contexts that use the expression
9902 parser. */
9903
9904static void
9905fixup_go_packaging (struct dwarf2_cu *cu)
9906{
9907 char *package_name = NULL;
9908 struct pending *list;
9909 int i;
9910
c24bdb02 9911 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9912 list != NULL;
9913 list = list->next)
a766d390
DE
9914 {
9915 for (i = 0; i < list->nsyms; ++i)
9916 {
9917 struct symbol *sym = list->symbol[i];
9918
c1b5c1eb 9919 if (sym->language () == language_go
a766d390
DE
9920 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9921 {
9922 char *this_package_name = go_symbol_package_name (sym);
9923
9924 if (this_package_name == NULL)
9925 continue;
9926 if (package_name == NULL)
9927 package_name = this_package_name;
9928 else
9929 {
518817b3
SM
9930 struct objfile *objfile
9931 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9932 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9933 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9934 (symbol_symtab (sym) != NULL
9935 ? symtab_to_filename_for_display
9936 (symbol_symtab (sym))
e3b94546 9937 : objfile_name (objfile)),
a766d390
DE
9938 this_package_name, package_name);
9939 xfree (this_package_name);
9940 }
9941 }
9942 }
9943 }
9944
9945 if (package_name != NULL)
9946 {
518817b3 9947 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9948 const char *saved_package_name
021887d8 9949 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
19f392bc
UW
9950 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9951 saved_package_name);
a766d390
DE
9952 struct symbol *sym;
9953
e623cf5d 9954 sym = allocate_symbol (objfile);
d3ecddab 9955 sym->set_language (language_go, &objfile->objfile_obstack);
31edb802 9956 SYMBOL_SET_NAMES (sym, saved_package_name, false, objfile);
a766d390
DE
9957 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9958 e.g., "main" finds the "main" module and not C's main(). */
9959 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9960 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9961 SYMBOL_TYPE (sym) = type;
9962
c24bdb02 9963 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9964
9965 xfree (package_name);
9966 }
9967}
9968
c9317f21
TT
9969/* Allocate a fully-qualified name consisting of the two parts on the
9970 obstack. */
9971
9972static const char *
9973rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9974{
9975 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9976}
9977
9978/* A helper that allocates a struct discriminant_info to attach to a
9979 union type. */
9980
9981static struct discriminant_info *
9982alloc_discriminant_info (struct type *type, int discriminant_index,
9983 int default_index)
9984{
9985 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9986 gdb_assert (discriminant_index == -1
9987 || (discriminant_index >= 0
9988 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9989 gdb_assert (default_index == -1
c7b15a66 9990 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9991
9992 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9993
9994 struct discriminant_info *disc
9995 = ((struct discriminant_info *)
9996 TYPE_ZALLOC (type,
9997 offsetof (struct discriminant_info, discriminants)
9998 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9999 disc->default_index = default_index;
10000 disc->discriminant_index = discriminant_index;
10001
10002 struct dynamic_prop prop;
10003 prop.kind = PROP_UNDEFINED;
10004 prop.data.baton = disc;
10005
10006 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10007
10008 return disc;
10009}
10010
10011/* Some versions of rustc emitted enums in an unusual way.
10012
10013 Ordinary enums were emitted as unions. The first element of each
10014 structure in the union was named "RUST$ENUM$DISR". This element
10015 held the discriminant.
10016
10017 These versions of Rust also implemented the "non-zero"
10018 optimization. When the enum had two values, and one is empty and
10019 the other holds a pointer that cannot be zero, the pointer is used
10020 as the discriminant, with a zero value meaning the empty variant.
10021 Here, the union's first member is of the form
10022 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10023 where the fieldnos are the indices of the fields that should be
10024 traversed in order to find the field (which may be several fields deep)
10025 and the variantname is the name of the variant of the case when the
10026 field is zero.
10027
10028 This function recognizes whether TYPE is of one of these forms,
10029 and, if so, smashes it to be a variant type. */
10030
10031static void
10032quirk_rust_enum (struct type *type, struct objfile *objfile)
10033{
10034 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10035
10036 /* We don't need to deal with empty enums. */
10037 if (TYPE_NFIELDS (type) == 0)
10038 return;
10039
10040#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10041 if (TYPE_NFIELDS (type) == 1
10042 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10043 {
10044 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10045
10046 /* Decode the field name to find the offset of the
10047 discriminant. */
10048 ULONGEST bit_offset = 0;
10049 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10050 while (name[0] >= '0' && name[0] <= '9')
10051 {
10052 char *tail;
10053 unsigned long index = strtoul (name, &tail, 10);
10054 name = tail;
10055 if (*name != '$'
10056 || index >= TYPE_NFIELDS (field_type)
10057 || (TYPE_FIELD_LOC_KIND (field_type, index)
10058 != FIELD_LOC_KIND_BITPOS))
10059 {
b98664d3 10060 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10061 "[in module %s]"),
10062 TYPE_FIELD_NAME (type, 0),
10063 objfile_name (objfile));
10064 return;
10065 }
10066 ++name;
10067
10068 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10069 field_type = TYPE_FIELD_TYPE (field_type, index);
10070 }
10071
10072 /* Make a union to hold the variants. */
10073 struct type *union_type = alloc_type (objfile);
10074 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10075 TYPE_NFIELDS (union_type) = 3;
10076 TYPE_FIELDS (union_type)
10077 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10078 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10079 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10080
10081 /* Put the discriminant must at index 0. */
10082 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10083 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10084 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10085 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10086
10087 /* The order of fields doesn't really matter, so put the real
10088 field at index 1 and the data-less field at index 2. */
10089 struct discriminant_info *disc
10090 = alloc_discriminant_info (union_type, 0, 1);
10091 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10092 TYPE_FIELD_NAME (union_type, 1)
10093 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10094 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10095 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10096 TYPE_FIELD_NAME (union_type, 1));
10097
10098 const char *dataless_name
10099 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10100 name);
10101 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10102 dataless_name);
10103 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10104 /* NAME points into the original discriminant name, which
10105 already has the correct lifetime. */
10106 TYPE_FIELD_NAME (union_type, 2) = name;
10107 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10108 disc->discriminants[2] = 0;
10109
10110 /* Smash this type to be a structure type. We have to do this
10111 because the type has already been recorded. */
10112 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10113 TYPE_NFIELDS (type) = 1;
10114 TYPE_FIELDS (type)
10115 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10116
10117 /* Install the variant part. */
10118 TYPE_FIELD_TYPE (type, 0) = union_type;
10119 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10120 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10121 }
77c2dba3
TT
10122 /* A union with a single anonymous field is probably an old-style
10123 univariant enum. */
10124 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10125 {
c9317f21
TT
10126 /* Smash this type to be a structure type. We have to do this
10127 because the type has already been recorded. */
10128 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10129
10130 /* Make a union to hold the variants. */
10131 struct type *union_type = alloc_type (objfile);
10132 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10133 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10134 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10135 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10136 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10137
10138 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10139 const char *variant_name
10140 = rust_last_path_segment (TYPE_NAME (field_type));
10141 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10142 TYPE_NAME (field_type)
10143 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10144 TYPE_NAME (type), variant_name);
c9317f21
TT
10145
10146 /* Install the union in the outer struct type. */
10147 TYPE_NFIELDS (type) = 1;
10148 TYPE_FIELDS (type)
10149 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10150 TYPE_FIELD_TYPE (type, 0) = union_type;
10151 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10152 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10153
10154 alloc_discriminant_info (union_type, -1, 0);
10155 }
10156 else
10157 {
10158 struct type *disr_type = nullptr;
10159 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10160 {
10161 disr_type = TYPE_FIELD_TYPE (type, i);
10162
a037790e
TT
10163 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10164 {
10165 /* All fields of a true enum will be structs. */
10166 return;
10167 }
10168 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10169 {
10170 /* Could be data-less variant, so keep going. */
a037790e 10171 disr_type = nullptr;
c9317f21
TT
10172 }
10173 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10174 "RUST$ENUM$DISR") != 0)
10175 {
10176 /* Not a Rust enum. */
10177 return;
10178 }
10179 else
10180 {
10181 /* Found one. */
10182 break;
10183 }
10184 }
10185
10186 /* If we got here without a discriminant, then it's probably
10187 just a union. */
10188 if (disr_type == nullptr)
10189 return;
10190
10191 /* Smash this type to be a structure type. We have to do this
10192 because the type has already been recorded. */
10193 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10194
10195 /* Make a union to hold the variants. */
10196 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10197 struct type *union_type = alloc_type (objfile);
10198 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10199 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10200 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10201 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10202 TYPE_FIELDS (union_type)
10203 = (struct field *) TYPE_ZALLOC (union_type,
10204 (TYPE_NFIELDS (union_type)
10205 * sizeof (struct field)));
10206
10207 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10208 TYPE_NFIELDS (type) * sizeof (struct field));
10209
10210 /* Install the discriminant at index 0 in the union. */
10211 TYPE_FIELD (union_type, 0) = *disr_field;
10212 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10213 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10214
10215 /* Install the union in the outer struct type. */
10216 TYPE_FIELD_TYPE (type, 0) = union_type;
10217 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10218 TYPE_NFIELDS (type) = 1;
10219
10220 /* Set the size and offset of the union type. */
10221 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10222
10223 /* We need a way to find the correct discriminant given a
10224 variant name. For convenience we build a map here. */
10225 struct type *enum_type = FIELD_TYPE (*disr_field);
10226 std::unordered_map<std::string, ULONGEST> discriminant_map;
10227 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10228 {
10229 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10230 {
10231 const char *name
10232 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10233 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10234 }
10235 }
10236
10237 int n_fields = TYPE_NFIELDS (union_type);
10238 struct discriminant_info *disc
10239 = alloc_discriminant_info (union_type, 0, -1);
10240 /* Skip the discriminant here. */
10241 for (int i = 1; i < n_fields; ++i)
10242 {
10243 /* Find the final word in the name of this variant's type.
10244 That name can be used to look up the correct
10245 discriminant. */
10246 const char *variant_name
10247 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10248 i)));
10249
10250 auto iter = discriminant_map.find (variant_name);
10251 if (iter != discriminant_map.end ())
10252 disc->discriminants[i] = iter->second;
10253
bedda9ac 10254 /* Remove the discriminant field, if it exists. */
c9317f21 10255 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10256 if (TYPE_NFIELDS (sub_type) > 0)
10257 {
10258 --TYPE_NFIELDS (sub_type);
10259 ++TYPE_FIELDS (sub_type);
10260 }
c9317f21
TT
10261 TYPE_FIELD_NAME (union_type, i) = variant_name;
10262 TYPE_NAME (sub_type)
10263 = rust_fully_qualify (&objfile->objfile_obstack,
10264 TYPE_NAME (type), variant_name);
10265 }
10266 }
10267}
10268
10269/* Rewrite some Rust unions to be structures with variants parts. */
10270
10271static void
10272rust_union_quirks (struct dwarf2_cu *cu)
10273{
10274 gdb_assert (cu->language == language_rust);
52941706
SM
10275 for (type *type_ : cu->rust_unions)
10276 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10277 /* We don't need this any more. */
10278 cu->rust_unions.clear ();
c9317f21
TT
10279}
10280
95554aad
TT
10281/* Return the symtab for PER_CU. This works properly regardless of
10282 whether we're using the index or psymtabs. */
10283
43f3e411
DE
10284static struct compunit_symtab *
10285get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10286{
ed2dc618 10287 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10288 ? per_cu->v.quick->compunit_symtab
10289 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10290}
10291
10292/* A helper function for computing the list of all symbol tables
10293 included by PER_CU. */
10294
10295static void
4c39bc03 10296recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10297 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10298 struct dwarf2_per_cu_data *per_cu,
43f3e411 10299 struct compunit_symtab *immediate_parent)
95554aad
TT
10300{
10301 void **slot;
43f3e411 10302 struct compunit_symtab *cust;
95554aad
TT
10303
10304 slot = htab_find_slot (all_children, per_cu, INSERT);
10305 if (*slot != NULL)
10306 {
10307 /* This inclusion and its children have been processed. */
10308 return;
10309 }
10310
10311 *slot = per_cu;
10312 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10313 cust = get_compunit_symtab (per_cu);
10314 if (cust != NULL)
ec94af83
DE
10315 {
10316 /* If this is a type unit only add its symbol table if we haven't
10317 seen it yet (type unit per_cu's can share symtabs). */
10318 if (per_cu->is_debug_types)
10319 {
43f3e411 10320 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10321 if (*slot == NULL)
10322 {
43f3e411 10323 *slot = cust;
4c39bc03 10324 result->push_back (cust);
43f3e411
DE
10325 if (cust->user == NULL)
10326 cust->user = immediate_parent;
ec94af83
DE
10327 }
10328 }
10329 else
f9125b6c 10330 {
4c39bc03 10331 result->push_back (cust);
43f3e411
DE
10332 if (cust->user == NULL)
10333 cust->user = immediate_parent;
f9125b6c 10334 }
ec94af83 10335 }
95554aad 10336
ae640021
AB
10337 if (!per_cu->imported_symtabs_empty ())
10338 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10339 {
10340 recursively_compute_inclusions (result, all_children,
10341 all_type_symtabs, ptr, cust);
10342 }
95554aad
TT
10343}
10344
43f3e411 10345/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10346 PER_CU. */
10347
10348static void
43f3e411 10349compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10350{
f4dc4d17
DE
10351 gdb_assert (! per_cu->is_debug_types);
10352
ae640021 10353 if (!per_cu->imported_symtabs_empty ())
95554aad 10354 {
ae640021 10355 int len;
4c39bc03 10356 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10357 htab_t all_children, all_type_symtabs;
43f3e411 10358 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10359
10360 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10361 if (cust == NULL)
95554aad
TT
10362 return;
10363
10364 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10365 NULL, xcalloc, xfree);
ec94af83
DE
10366 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10367 NULL, xcalloc, xfree);
95554aad 10368
ae640021 10369 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10370 {
10371 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10372 all_type_symtabs, ptr, cust);
ec94af83 10373 }
95554aad 10374
ec94af83 10375 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10376 len = result_symtabs.size ();
43f3e411 10377 cust->includes
ed2dc618 10378 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10379 struct compunit_symtab *, len + 1);
4c39bc03
TT
10380 memcpy (cust->includes, result_symtabs.data (),
10381 len * sizeof (compunit_symtab *));
43f3e411 10382 cust->includes[len] = NULL;
95554aad 10383
95554aad 10384 htab_delete (all_children);
ec94af83 10385 htab_delete (all_type_symtabs);
95554aad
TT
10386 }
10387}
10388
10389/* Compute the 'includes' field for the symtabs of all the CUs we just
10390 read. */
10391
10392static void
ed2dc618 10393process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10394{
71b73764 10395 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10396 {
10397 if (! iter->is_debug_types)
43f3e411 10398 compute_compunit_symtab_includes (iter);
f4dc4d17 10399 }
95554aad 10400
c5d0225d 10401 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10402}
10403
9cdd5dbd 10404/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10405 already been loaded into memory. */
10406
10407static void
95554aad
TT
10408process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10409 enum language pretend_language)
10b3939b 10410{
10b3939b 10411 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10412 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10413 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10414 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10415 CORE_ADDR lowpc, highpc;
43f3e411 10416 struct compunit_symtab *cust;
10b3939b 10417 CORE_ADDR baseaddr;
4359dff1 10418 struct block *static_block;
3e29f34a 10419 CORE_ADDR addr;
10b3939b
DJ
10420
10421 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10422
c89b44cd
TT
10423 /* Clear the list here in case something was left over. */
10424 cu->method_list.clear ();
10b3939b 10425
95554aad
TT
10426 cu->language = pretend_language;
10427 cu->language_defn = language_def (cu->language);
10428
c906108c 10429 /* Do line number decoding in read_file_scope () */
10b3939b 10430 process_die (cu->dies, cu);
c906108c 10431
a766d390
DE
10432 /* For now fudge the Go package. */
10433 if (cu->language == language_go)
10434 fixup_go_packaging (cu);
10435
5f48f8f3 10436 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10437 should be complete, and it should now be safe to compute all of the
10438 physnames. */
10439 compute_delayed_physnames (cu);
3da10d80 10440
c9317f21
TT
10441 if (cu->language == language_rust)
10442 rust_union_quirks (cu);
10443
fae299cd
DC
10444 /* Some compilers don't define a DW_AT_high_pc attribute for the
10445 compilation unit. If the DW_AT_high_pc is missing, synthesize
10446 it, by scanning the DIE's below the compilation unit. */
10b3939b 10447 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10448
3e29f34a 10449 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10450 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10451
10452 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10453 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10454 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10455 addrmap to help ensure it has an accurate map of pc values belonging to
10456 this comp unit. */
10457 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10458
c24bdb02 10459 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10460 SECT_OFF_TEXT (objfile),
10461 0);
c906108c 10462
43f3e411 10463 if (cust != NULL)
c906108c 10464 {
df15bd07 10465 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10466
8be455d7
JK
10467 /* Set symtab language to language from DW_AT_language. If the
10468 compilation is from a C file generated by language preprocessors, do
10469 not set the language if it was already deduced by start_subfile. */
43f3e411 10470 if (!(cu->language == language_c
40e3ad0e 10471 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10472 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10473
10474 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10475 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10476 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10477 there were bugs in prologue debug info, fixed later in GCC-4.5
10478 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10479
10480 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10481 needed, it would be wrong due to missing DW_AT_producer there.
10482
10483 Still one can confuse GDB by using non-standard GCC compilation
10484 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10485 */
ab260dad 10486 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10487 cust->locations_valid = 1;
e0d00bc7
JK
10488
10489 if (gcc_4_minor >= 5)
43f3e411 10490 cust->epilogue_unwind_valid = 1;
96408a79 10491
43f3e411 10492 cust->call_site_htab = cu->call_site_htab;
c906108c 10493 }
9291a0cd
TT
10494
10495 if (dwarf2_per_objfile->using_index)
43f3e411 10496 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10497 else
10498 {
10499 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10500 pst->compunit_symtab = cust;
9291a0cd
TT
10501 pst->readin = 1;
10502 }
c906108c 10503
95554aad 10504 /* Push it for inclusion processing later. */
c5d0225d 10505 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10506
10507 /* Not needed any more. */
c24bdb02 10508 cu->reset_builder ();
f4dc4d17 10509}
45cfd468 10510
f4dc4d17
DE
10511/* Generate full symbol information for type unit PER_CU, whose DIEs have
10512 already been loaded into memory. */
10513
10514static void
10515process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10516 enum language pretend_language)
10517{
10518 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10519 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10520 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10521 struct compunit_symtab *cust;
0186c6a7
DE
10522 struct signatured_type *sig_type;
10523
10524 gdb_assert (per_cu->is_debug_types);
10525 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10526
c89b44cd
TT
10527 /* Clear the list here in case something was left over. */
10528 cu->method_list.clear ();
f4dc4d17 10529
f4dc4d17
DE
10530 cu->language = pretend_language;
10531 cu->language_defn = language_def (cu->language);
10532
10533 /* The symbol tables are set up in read_type_unit_scope. */
10534 process_die (cu->dies, cu);
10535
10536 /* For now fudge the Go package. */
10537 if (cu->language == language_go)
10538 fixup_go_packaging (cu);
10539
5f48f8f3 10540 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10541 should be complete, and it should now be safe to compute all of the
10542 physnames. */
10543 compute_delayed_physnames (cu);
f4dc4d17 10544
c9317f21
TT
10545 if (cu->language == language_rust)
10546 rust_union_quirks (cu);
10547
f4dc4d17
DE
10548 /* TUs share symbol tables.
10549 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10550 of it with end_expandable_symtab. Otherwise, complete the addition of
10551 this TU's symbols to the existing symtab. */
43f3e411 10552 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10553 {
c24bdb02
KS
10554 buildsym_compunit *builder = cu->get_builder ();
10555 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10556 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10557
43f3e411 10558 if (cust != NULL)
f4dc4d17
DE
10559 {
10560 /* Set symtab language to language from DW_AT_language. If the
10561 compilation is from a C file generated by language preprocessors,
10562 do not set the language if it was already deduced by
10563 start_subfile. */
43f3e411
DE
10564 if (!(cu->language == language_c
10565 && COMPUNIT_FILETABS (cust)->language != language_c))
10566 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10567 }
10568 }
10569 else
10570 {
c24bdb02 10571 cu->get_builder ()->augment_type_symtab ();
43f3e411 10572 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10573 }
10574
10575 if (dwarf2_per_objfile->using_index)
43f3e411 10576 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10577 else
10578 {
10579 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10580 pst->compunit_symtab = cust;
f4dc4d17 10581 pst->readin = 1;
45cfd468 10582 }
804d2729
TT
10583
10584 /* Not needed any more. */
c24bdb02 10585 cu->reset_builder ();
c906108c
SS
10586}
10587
95554aad
TT
10588/* Process an imported unit DIE. */
10589
10590static void
10591process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10592{
10593 struct attribute *attr;
10594
f4dc4d17
DE
10595 /* For now we don't handle imported units in type units. */
10596 if (cu->per_cu->is_debug_types)
10597 {
10598 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10599 " supported in type units [in module %s]"),
518817b3 10600 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10601 }
10602
95554aad
TT
10603 attr = dwarf2_attr (die, DW_AT_import, cu);
10604 if (attr != NULL)
10605 {
9c541725
PA
10606 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10607 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10608 dwarf2_per_cu_data *per_cu
e3b94546 10609 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10610 cu->per_cu->dwarf2_per_objfile);
95554aad 10611
69d751e3 10612 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10613 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10614 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10615
ae640021 10616 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10617 }
10618}
10619
4c8aa72d
PA
10620/* RAII object that represents a process_die scope: i.e.,
10621 starts/finishes processing a DIE. */
10622class process_die_scope
adde2bff 10623{
4c8aa72d
PA
10624public:
10625 process_die_scope (die_info *die, dwarf2_cu *cu)
10626 : m_die (die), m_cu (cu)
10627 {
10628 /* We should only be processing DIEs not already in process. */
10629 gdb_assert (!m_die->in_process);
10630 m_die->in_process = true;
10631 }
8c3cb9fa 10632
4c8aa72d
PA
10633 ~process_die_scope ()
10634 {
10635 m_die->in_process = false;
10636
10637 /* If we're done processing the DIE for the CU that owns the line
10638 header, we don't need the line header anymore. */
10639 if (m_cu->line_header_die_owner == m_die)
10640 {
10641 delete m_cu->line_header;
10642 m_cu->line_header = NULL;
10643 m_cu->line_header_die_owner = NULL;
10644 }
10645 }
10646
10647private:
10648 die_info *m_die;
10649 dwarf2_cu *m_cu;
10650};
adde2bff 10651
c906108c
SS
10652/* Process a die and its children. */
10653
10654static void
e7c27a73 10655process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10656{
4c8aa72d 10657 process_die_scope scope (die, cu);
adde2bff 10658
c906108c
SS
10659 switch (die->tag)
10660 {
10661 case DW_TAG_padding:
10662 break;
10663 case DW_TAG_compile_unit:
95554aad 10664 case DW_TAG_partial_unit:
e7c27a73 10665 read_file_scope (die, cu);
c906108c 10666 break;
348e048f
DE
10667 case DW_TAG_type_unit:
10668 read_type_unit_scope (die, cu);
10669 break;
c906108c 10670 case DW_TAG_subprogram:
0a4b0913
AB
10671 /* Nested subprograms in Fortran get a prefix. */
10672 if (cu->language == language_fortran
10673 && die->parent != NULL
10674 && die->parent->tag == DW_TAG_subprogram)
10675 cu->processing_has_namespace_info = true;
10676 /* Fall through. */
c906108c 10677 case DW_TAG_inlined_subroutine:
edb3359d 10678 read_func_scope (die, cu);
c906108c
SS
10679 break;
10680 case DW_TAG_lexical_block:
14898363
L
10681 case DW_TAG_try_block:
10682 case DW_TAG_catch_block:
e7c27a73 10683 read_lexical_block_scope (die, cu);
c906108c 10684 break;
216f72a1 10685 case DW_TAG_call_site:
96408a79
SA
10686 case DW_TAG_GNU_call_site:
10687 read_call_site_scope (die, cu);
10688 break;
c906108c 10689 case DW_TAG_class_type:
680b30c7 10690 case DW_TAG_interface_type:
c906108c
SS
10691 case DW_TAG_structure_type:
10692 case DW_TAG_union_type:
134d01f1 10693 process_structure_scope (die, cu);
c906108c
SS
10694 break;
10695 case DW_TAG_enumeration_type:
134d01f1 10696 process_enumeration_scope (die, cu);
c906108c 10697 break;
134d01f1 10698
f792889a
DJ
10699 /* These dies have a type, but processing them does not create
10700 a symbol or recurse to process the children. Therefore we can
10701 read them on-demand through read_type_die. */
c906108c 10702 case DW_TAG_subroutine_type:
72019c9c 10703 case DW_TAG_set_type:
c906108c 10704 case DW_TAG_array_type:
c906108c 10705 case DW_TAG_pointer_type:
c906108c 10706 case DW_TAG_ptr_to_member_type:
c906108c 10707 case DW_TAG_reference_type:
4297a3f0 10708 case DW_TAG_rvalue_reference_type:
c906108c 10709 case DW_TAG_string_type:
c906108c 10710 break;
134d01f1 10711
c906108c 10712 case DW_TAG_base_type:
a02abb62 10713 case DW_TAG_subrange_type:
cb249c71 10714 case DW_TAG_typedef:
134d01f1
DJ
10715 /* Add a typedef symbol for the type definition, if it has a
10716 DW_AT_name. */
f792889a 10717 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10718 break;
c906108c 10719 case DW_TAG_common_block:
e7c27a73 10720 read_common_block (die, cu);
c906108c
SS
10721 break;
10722 case DW_TAG_common_inclusion:
10723 break;
d9fa45fe 10724 case DW_TAG_namespace:
9068261f 10725 cu->processing_has_namespace_info = true;
e7c27a73 10726 read_namespace (die, cu);
d9fa45fe 10727 break;
5d7cb8df 10728 case DW_TAG_module:
9068261f 10729 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10730 read_module (die, cu);
10731 break;
d9fa45fe 10732 case DW_TAG_imported_declaration:
9068261f 10733 cu->processing_has_namespace_info = true;
74921315
KS
10734 if (read_namespace_alias (die, cu))
10735 break;
86a73007
TT
10736 /* The declaration is not a global namespace alias. */
10737 /* Fall through. */
d9fa45fe 10738 case DW_TAG_imported_module:
9068261f 10739 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10740 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10741 || cu->language != language_fortran))
b98664d3 10742 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10743 dwarf_tag_name (die->tag));
10744 read_import_statement (die, cu);
d9fa45fe 10745 break;
95554aad
TT
10746
10747 case DW_TAG_imported_unit:
10748 process_imported_unit_die (die, cu);
10749 break;
10750
71a3c369
TT
10751 case DW_TAG_variable:
10752 read_variable (die, cu);
10753 break;
10754
c906108c 10755 default:
e7c27a73 10756 new_symbol (die, NULL, cu);
c906108c
SS
10757 break;
10758 }
10759}
ca69b9e6
DE
10760\f
10761/* DWARF name computation. */
c906108c 10762
94af9270
KS
10763/* A helper function for dwarf2_compute_name which determines whether DIE
10764 needs to have the name of the scope prepended to the name listed in the
10765 die. */
10766
10767static int
10768die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10769{
1c809c68
TT
10770 struct attribute *attr;
10771
94af9270
KS
10772 switch (die->tag)
10773 {
10774 case DW_TAG_namespace:
10775 case DW_TAG_typedef:
10776 case DW_TAG_class_type:
10777 case DW_TAG_interface_type:
10778 case DW_TAG_structure_type:
10779 case DW_TAG_union_type:
10780 case DW_TAG_enumeration_type:
10781 case DW_TAG_enumerator:
10782 case DW_TAG_subprogram:
08a76f8a 10783 case DW_TAG_inlined_subroutine:
94af9270 10784 case DW_TAG_member:
74921315 10785 case DW_TAG_imported_declaration:
94af9270
KS
10786 return 1;
10787
10788 case DW_TAG_variable:
c2b0a229 10789 case DW_TAG_constant:
94af9270
KS
10790 /* We only need to prefix "globally" visible variables. These include
10791 any variable marked with DW_AT_external or any variable that
10792 lives in a namespace. [Variables in anonymous namespaces
10793 require prefixing, but they are not DW_AT_external.] */
10794
10795 if (dwarf2_attr (die, DW_AT_specification, cu))
10796 {
10797 struct dwarf2_cu *spec_cu = cu;
9a619af0 10798
94af9270
KS
10799 return die_needs_namespace (die_specification (die, &spec_cu),
10800 spec_cu);
10801 }
10802
1c809c68 10803 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10804 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10805 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10806 return 0;
10807 /* A variable in a lexical block of some kind does not need a
10808 namespace, even though in C++ such variables may be external
10809 and have a mangled name. */
10810 if (die->parent->tag == DW_TAG_lexical_block
10811 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10812 || die->parent->tag == DW_TAG_catch_block
10813 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10814 return 0;
10815 return 1;
94af9270
KS
10816
10817 default:
10818 return 0;
10819 }
10820}
10821
73b9be8b
KS
10822/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10823 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10824 defined for the given DIE. */
10825
10826static struct attribute *
10827dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10828{
10829 struct attribute *attr;
10830
10831 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10832 if (attr == NULL)
10833 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10834
10835 return attr;
10836}
10837
10838/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10839 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10840 defined for the given DIE. */
10841
10842static const char *
10843dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10844{
10845 const char *linkage_name;
10846
10847 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10848 if (linkage_name == NULL)
10849 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10850
10851 return linkage_name;
10852}
10853
94af9270 10854/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10855 compute the physname for the object, which include a method's:
9c37b5ae 10856 - formal parameters (C++),
a766d390 10857 - receiver type (Go),
a766d390
DE
10858
10859 The term "physname" is a bit confusing.
10860 For C++, for example, it is the demangled name.
10861 For Go, for example, it's the mangled name.
94af9270 10862
af6b7be1
JB
10863 For Ada, return the DIE's linkage name rather than the fully qualified
10864 name. PHYSNAME is ignored..
10865
94af9270
KS
10866 The result is allocated on the objfile_obstack and canonicalized. */
10867
10868static const char *
15d034d0
TT
10869dwarf2_compute_name (const char *name,
10870 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10871 int physname)
10872{
518817b3 10873 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10874
94af9270
KS
10875 if (name == NULL)
10876 name = dwarf2_name (die, cu);
10877
2ee7123e
DE
10878 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10879 but otherwise compute it by typename_concat inside GDB.
10880 FIXME: Actually this is not really true, or at least not always true.
10881 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10882 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10883 will set the demangled name to the result of dwarf2_full_name, and it is
10884 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10885 if (cu->language == language_ada
10886 || (cu->language == language_fortran && physname))
10887 {
10888 /* For Ada unit, we prefer the linkage name over the name, as
10889 the former contains the exported name, which the user expects
10890 to be able to reference. Ideally, we want the user to be able
10891 to reference this entity using either natural or linkage name,
10892 but we haven't started looking at this enhancement yet. */
73b9be8b 10893 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10894
2ee7123e
DE
10895 if (linkage_name != NULL)
10896 return linkage_name;
f55ee35c
JK
10897 }
10898
94af9270
KS
10899 /* These are the only languages we know how to qualify names in. */
10900 if (name != NULL
9c37b5ae 10901 && (cu->language == language_cplus
c44af4eb
TT
10902 || cu->language == language_fortran || cu->language == language_d
10903 || cu->language == language_rust))
94af9270
KS
10904 {
10905 if (die_needs_namespace (die, cu))
10906 {
0d5cff50 10907 const char *prefix;
34a68019 10908 const char *canonical_name = NULL;
94af9270 10909
d7e74731
PA
10910 string_file buf;
10911
94af9270 10912 prefix = determine_prefix (die, cu);
94af9270
KS
10913 if (*prefix != '\0')
10914 {
f55ee35c
JK
10915 char *prefixed_name = typename_concat (NULL, prefix, name,
10916 physname, cu);
9a619af0 10917
d7e74731 10918 buf.puts (prefixed_name);
94af9270
KS
10919 xfree (prefixed_name);
10920 }
10921 else
d7e74731 10922 buf.puts (name);
94af9270 10923
98bfdba5
PA
10924 /* Template parameters may be specified in the DIE's DW_AT_name, or
10925 as children with DW_TAG_template_type_param or
10926 DW_TAG_value_type_param. If the latter, add them to the name
10927 here. If the name already has template parameters, then
10928 skip this step; some versions of GCC emit both, and
10929 it is more efficient to use the pre-computed name.
10930
10931 Something to keep in mind about this process: it is very
10932 unlikely, or in some cases downright impossible, to produce
10933 something that will match the mangled name of a function.
10934 If the definition of the function has the same debug info,
10935 we should be able to match up with it anyway. But fallbacks
10936 using the minimal symbol, for instance to find a method
10937 implemented in a stripped copy of libstdc++, will not work.
10938 If we do not have debug info for the definition, we will have to
10939 match them up some other way.
10940
10941 When we do name matching there is a related problem with function
10942 templates; two instantiated function templates are allowed to
10943 differ only by their return types, which we do not add here. */
10944
10945 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10946 {
10947 struct attribute *attr;
10948 struct die_info *child;
10949 int first = 1;
10950
10951 die->building_fullname = 1;
10952
10953 for (child = die->child; child != NULL; child = child->sibling)
10954 {
10955 struct type *type;
12df843f 10956 LONGEST value;
d521ce57 10957 const gdb_byte *bytes;
98bfdba5
PA
10958 struct dwarf2_locexpr_baton *baton;
10959 struct value *v;
10960
10961 if (child->tag != DW_TAG_template_type_param
10962 && child->tag != DW_TAG_template_value_param)
10963 continue;
10964
10965 if (first)
10966 {
d7e74731 10967 buf.puts ("<");
98bfdba5
PA
10968 first = 0;
10969 }
10970 else
d7e74731 10971 buf.puts (", ");
98bfdba5
PA
10972
10973 attr = dwarf2_attr (child, DW_AT_type, cu);
10974 if (attr == NULL)
10975 {
b98664d3 10976 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10977 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10978 continue;
10979 }
10980 type = die_type (child, cu);
10981
10982 if (child->tag == DW_TAG_template_type_param)
10983 {
c1ec8cea
TT
10984 c_print_type (type, "", &buf, -1, 0, cu->language,
10985 &type_print_raw_options);
98bfdba5
PA
10986 continue;
10987 }
10988
10989 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10990 if (attr == NULL)
10991 {
b98664d3 10992 complaint (_("template parameter missing "
3e43a32a 10993 "DW_AT_const_value"));
d7e74731 10994 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10995 continue;
10996 }
10997
10998 dwarf2_const_value_attr (attr, type, name,
10999 &cu->comp_unit_obstack, cu,
11000 &value, &bytes, &baton);
11001
11002 if (TYPE_NOSIGN (type))
11003 /* GDB prints characters as NUMBER 'CHAR'. If that's
11004 changed, this can use value_print instead. */
d7e74731 11005 c_printchar (value, type, &buf);
98bfdba5
PA
11006 else
11007 {
11008 struct value_print_options opts;
11009
11010 if (baton != NULL)
11011 v = dwarf2_evaluate_loc_desc (type, NULL,
11012 baton->data,
11013 baton->size,
11014 baton->per_cu);
11015 else if (bytes != NULL)
11016 {
11017 v = allocate_value (type);
11018 memcpy (value_contents_writeable (v), bytes,
11019 TYPE_LENGTH (type));
11020 }
11021 else
11022 v = value_from_longest (type, value);
11023
3e43a32a
MS
11024 /* Specify decimal so that we do not depend on
11025 the radix. */
98bfdba5
PA
11026 get_formatted_print_options (&opts, 'd');
11027 opts.raw = 1;
d7e74731 11028 value_print (v, &buf, &opts);
98bfdba5 11029 release_value (v);
98bfdba5
PA
11030 }
11031 }
11032
11033 die->building_fullname = 0;
11034
11035 if (!first)
11036 {
11037 /* Close the argument list, with a space if necessary
11038 (nested templates). */
d7e74731
PA
11039 if (!buf.empty () && buf.string ().back () == '>')
11040 buf.puts (" >");
98bfdba5 11041 else
d7e74731 11042 buf.puts (">");
98bfdba5
PA
11043 }
11044 }
11045
9c37b5ae 11046 /* For C++ methods, append formal parameter type
94af9270 11047 information, if PHYSNAME. */
6e70227d 11048
94af9270 11049 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11050 && cu->language == language_cplus)
94af9270
KS
11051 {
11052 struct type *type = read_type_die (die, cu);
11053
d7e74731 11054 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11055 &type_print_raw_options);
94af9270 11056
9c37b5ae 11057 if (cu->language == language_cplus)
94af9270 11058 {
60430eff
DJ
11059 /* Assume that an artificial first parameter is
11060 "this", but do not crash if it is not. RealView
11061 marks unnamed (and thus unused) parameters as
11062 artificial; there is no way to differentiate
11063 the two cases. */
94af9270
KS
11064 if (TYPE_NFIELDS (type) > 0
11065 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11066 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11067 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11068 0))))
d7e74731 11069 buf.puts (" const");
94af9270
KS
11070 }
11071 }
11072
d7e74731 11073 const std::string &intermediate_name = buf.string ();
94af9270
KS
11074
11075 if (cu->language == language_cplus)
34a68019 11076 canonical_name
322a8516 11077 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11078 &objfile->per_bfd->storage_obstack);
11079
11080 /* If we only computed INTERMEDIATE_NAME, or if
11081 INTERMEDIATE_NAME is already canonical, then we need to
11082 copy it to the appropriate obstack. */
322a8516 11083 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11084 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11085 intermediate_name);
34a68019
TT
11086 else
11087 name = canonical_name;
94af9270
KS
11088 }
11089 }
11090
11091 return name;
11092}
11093
0114d602
DJ
11094/* Return the fully qualified name of DIE, based on its DW_AT_name.
11095 If scope qualifiers are appropriate they will be added. The result
34a68019 11096 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11097 not have a name. NAME may either be from a previous call to
11098 dwarf2_name or NULL.
11099
9c37b5ae 11100 The output string will be canonicalized (if C++). */
0114d602
DJ
11101
11102static const char *
15d034d0 11103dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11104{
94af9270
KS
11105 return dwarf2_compute_name (name, die, cu, 0);
11106}
0114d602 11107
94af9270
KS
11108/* Construct a physname for the given DIE in CU. NAME may either be
11109 from a previous call to dwarf2_name or NULL. The result will be
11110 allocated on the objfile_objstack or NULL if the DIE does not have a
11111 name.
0114d602 11112
9c37b5ae 11113 The output string will be canonicalized (if C++). */
0114d602 11114
94af9270 11115static const char *
15d034d0 11116dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11117{
518817b3 11118 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11119 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11120 int need_copy = 1;
11121
11122 /* In this case dwarf2_compute_name is just a shortcut not building anything
11123 on its own. */
11124 if (!die_needs_namespace (die, cu))
11125 return dwarf2_compute_name (name, die, cu, 1);
11126
73b9be8b 11127 mangled = dw2_linkage_name (die, cu);
900e11f9 11128
e98c9e7c
TT
11129 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11130 See https://github.com/rust-lang/rust/issues/32925. */
11131 if (cu->language == language_rust && mangled != NULL
11132 && strchr (mangled, '{') != NULL)
11133 mangled = NULL;
11134
900e11f9
JK
11135 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11136 has computed. */
791afaa2 11137 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11138 if (mangled != NULL)
900e11f9 11139 {
900e11f9 11140
59cc4834
JB
11141 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11142 {
11143 /* Do nothing (do not demangle the symbol name). */
11144 }
11145 else if (cu->language == language_go)
a766d390 11146 {
5e2db402
TT
11147 /* This is a lie, but we already lie to the caller new_symbol.
11148 new_symbol assumes we return the mangled name.
a766d390 11149 This just undoes that lie until things are cleaned up. */
a766d390
DE
11150 }
11151 else
11152 {
0eb876f5
JB
11153 /* Use DMGL_RET_DROP for C++ template functions to suppress
11154 their return type. It is easier for GDB users to search
11155 for such functions as `name(params)' than `long name(params)'.
11156 In such case the minimal symbol names do not match the full
11157 symbol names but for template functions there is never a need
11158 to look up their definition from their declaration so
11159 the only disadvantage remains the minimal symbol variant
11160 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11161 demangled.reset (gdb_demangle (mangled,
11162 (DMGL_PARAMS | DMGL_ANSI
11163 | DMGL_RET_DROP)));
a766d390 11164 }
900e11f9 11165 if (demangled)
791afaa2 11166 canon = demangled.get ();
900e11f9
JK
11167 else
11168 {
11169 canon = mangled;
11170 need_copy = 0;
11171 }
11172 }
11173
11174 if (canon == NULL || check_physname)
11175 {
11176 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11177
11178 if (canon != NULL && strcmp (physname, canon) != 0)
11179 {
11180 /* It may not mean a bug in GDB. The compiler could also
11181 compute DW_AT_linkage_name incorrectly. But in such case
11182 GDB would need to be bug-to-bug compatible. */
11183
b98664d3 11184 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11185 "(from linkage <%s>) - DIE at %s [in module %s]"),
11186 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11187 objfile_name (objfile));
900e11f9
JK
11188
11189 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11190 is available here - over computed PHYSNAME. It is safer
11191 against both buggy GDB and buggy compilers. */
11192
11193 retval = canon;
11194 }
11195 else
11196 {
11197 retval = physname;
11198 need_copy = 0;
11199 }
11200 }
11201 else
11202 retval = canon;
11203
11204 if (need_copy)
021887d8 11205 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11206
900e11f9 11207 return retval;
0114d602
DJ
11208}
11209
74921315
KS
11210/* Inspect DIE in CU for a namespace alias. If one exists, record
11211 a new symbol for it.
11212
11213 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11214
11215static int
11216read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11217{
11218 struct attribute *attr;
11219
11220 /* If the die does not have a name, this is not a namespace
11221 alias. */
11222 attr = dwarf2_attr (die, DW_AT_name, cu);
11223 if (attr != NULL)
11224 {
11225 int num;
11226 struct die_info *d = die;
11227 struct dwarf2_cu *imported_cu = cu;
11228
11229 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11230 keep inspecting DIEs until we hit the underlying import. */
11231#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11232 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11233 {
11234 attr = dwarf2_attr (d, DW_AT_import, cu);
11235 if (attr == NULL)
11236 break;
11237
11238 d = follow_die_ref (d, attr, &imported_cu);
11239 if (d->tag != DW_TAG_imported_declaration)
11240 break;
11241 }
11242
11243 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11244 {
b98664d3 11245 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11246 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11247 return 0;
11248 }
11249
11250 if (attr != NULL)
11251 {
11252 struct type *type;
9c541725 11253 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11254
9c541725 11255 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11256 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11257 {
11258 /* This declaration is a global namespace alias. Add
11259 a symbol for it whose type is the aliased namespace. */
11260 new_symbol (die, type, cu);
11261 return 1;
11262 }
11263 }
11264 }
11265
11266 return 0;
11267}
11268
22cee43f 11269/* Return the using directives repository (global or local?) to use in the
804d2729 11270 current context for CU.
22cee43f
PMR
11271
11272 For Ada, imported declarations can materialize renamings, which *may* be
11273 global. However it is impossible (for now?) in DWARF to distinguish
11274 "external" imported declarations and "static" ones. As all imported
11275 declarations seem to be static in all other languages, make them all CU-wide
11276 global only in Ada. */
11277
11278static struct using_direct **
804d2729 11279using_directives (struct dwarf2_cu *cu)
22cee43f 11280{
c24bdb02
KS
11281 if (cu->language == language_ada
11282 && cu->get_builder ()->outermost_context_p ())
11283 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11284 else
c24bdb02 11285 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11286}
11287
27aa8d6a
SW
11288/* Read the import statement specified by the given die and record it. */
11289
11290static void
11291read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11292{
518817b3 11293 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11294 struct attribute *import_attr;
32019081 11295 struct die_info *imported_die, *child_die;
de4affc9 11296 struct dwarf2_cu *imported_cu;
27aa8d6a 11297 const char *imported_name;
794684b6 11298 const char *imported_name_prefix;
13387711
SW
11299 const char *canonical_name;
11300 const char *import_alias;
11301 const char *imported_declaration = NULL;
794684b6 11302 const char *import_prefix;
eb1e02fd 11303 std::vector<const char *> excludes;
13387711 11304
27aa8d6a
SW
11305 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11306 if (import_attr == NULL)
11307 {
b98664d3 11308 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11309 dwarf_tag_name (die->tag));
11310 return;
11311 }
11312
de4affc9
CC
11313 imported_cu = cu;
11314 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11315 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11316 if (imported_name == NULL)
11317 {
11318 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11319
11320 The import in the following code:
11321 namespace A
11322 {
11323 typedef int B;
11324 }
11325
11326 int main ()
11327 {
11328 using A::B;
11329 B b;
11330 return b;
11331 }
11332
11333 ...
11334 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11335 <52> DW_AT_decl_file : 1
11336 <53> DW_AT_decl_line : 6
11337 <54> DW_AT_import : <0x75>
11338 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11339 <59> DW_AT_name : B
11340 <5b> DW_AT_decl_file : 1
11341 <5c> DW_AT_decl_line : 2
11342 <5d> DW_AT_type : <0x6e>
11343 ...
11344 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11345 <76> DW_AT_byte_size : 4
11346 <77> DW_AT_encoding : 5 (signed)
11347
11348 imports the wrong die ( 0x75 instead of 0x58 ).
11349 This case will be ignored until the gcc bug is fixed. */
11350 return;
11351 }
11352
82856980
SW
11353 /* Figure out the local name after import. */
11354 import_alias = dwarf2_name (die, cu);
27aa8d6a 11355
794684b6
SW
11356 /* Figure out where the statement is being imported to. */
11357 import_prefix = determine_prefix (die, cu);
11358
11359 /* Figure out what the scope of the imported die is and prepend it
11360 to the name of the imported die. */
de4affc9 11361 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11362
f55ee35c
JK
11363 if (imported_die->tag != DW_TAG_namespace
11364 && imported_die->tag != DW_TAG_module)
794684b6 11365 {
13387711
SW
11366 imported_declaration = imported_name;
11367 canonical_name = imported_name_prefix;
794684b6 11368 }
13387711 11369 else if (strlen (imported_name_prefix) > 0)
12aaed36 11370 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11371 imported_name_prefix,
11372 (cu->language == language_d ? "." : "::"),
11373 imported_name, (char *) NULL);
13387711
SW
11374 else
11375 canonical_name = imported_name;
794684b6 11376
32019081
JK
11377 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11378 for (child_die = die->child; child_die && child_die->tag;
11379 child_die = sibling_die (child_die))
11380 {
11381 /* DWARF-4: A Fortran use statement with a “rename list” may be
11382 represented by an imported module entry with an import attribute
11383 referring to the module and owned entries corresponding to those
11384 entities that are renamed as part of being imported. */
11385
11386 if (child_die->tag != DW_TAG_imported_declaration)
11387 {
b98664d3 11388 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11389 "- DIE at %s [in module %s]"),
11390 sect_offset_str (child_die->sect_off),
11391 objfile_name (objfile));
32019081
JK
11392 continue;
11393 }
11394
11395 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11396 if (import_attr == NULL)
11397 {
b98664d3 11398 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11399 dwarf_tag_name (child_die->tag));
11400 continue;
11401 }
11402
11403 imported_cu = cu;
11404 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11405 &imported_cu);
11406 imported_name = dwarf2_name (imported_die, imported_cu);
11407 if (imported_name == NULL)
11408 {
b98664d3 11409 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11410 "imported name - DIE at %s [in module %s]"),
11411 sect_offset_str (child_die->sect_off),
11412 objfile_name (objfile));
32019081
JK
11413 continue;
11414 }
11415
eb1e02fd 11416 excludes.push_back (imported_name);
32019081
JK
11417
11418 process_die (child_die, cu);
11419 }
11420
804d2729 11421 add_using_directive (using_directives (cu),
22cee43f
PMR
11422 import_prefix,
11423 canonical_name,
11424 import_alias,
11425 imported_declaration,
11426 excludes,
11427 0,
11428 &objfile->objfile_obstack);
27aa8d6a
SW
11429}
11430
5230b05a
WT
11431/* ICC<14 does not output the required DW_AT_declaration on incomplete
11432 types, but gives them a size of zero. Starting with version 14,
11433 ICC is compatible with GCC. */
11434
9068261f 11435static bool
5230b05a
WT
11436producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11437{
11438 if (!cu->checked_producer)
11439 check_producer (cu);
11440
11441 return cu->producer_is_icc_lt_14;
11442}
11443
eb77c9df
AB
11444/* ICC generates a DW_AT_type for C void functions. This was observed on
11445 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11446 which says that void functions should not have a DW_AT_type. */
11447
11448static bool
11449producer_is_icc (struct dwarf2_cu *cu)
11450{
11451 if (!cu->checked_producer)
11452 check_producer (cu);
11453
11454 return cu->producer_is_icc;
11455}
11456
1b80a9fa
JK
11457/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11458 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11459 this, it was first present in GCC release 4.3.0. */
11460
9068261f 11461static bool
1b80a9fa
JK
11462producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11463{
11464 if (!cu->checked_producer)
11465 check_producer (cu);
11466
11467 return cu->producer_is_gcc_lt_4_3;
11468}
11469
d721ba37
PA
11470static file_and_directory
11471find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11472{
d721ba37
PA
11473 file_and_directory res;
11474
9291a0cd
TT
11475 /* Find the filename. Do not use dwarf2_name here, since the filename
11476 is not a source language identifier. */
d721ba37
PA
11477 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11478 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11479
d721ba37
PA
11480 if (res.comp_dir == NULL
11481 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11482 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11483 {
d721ba37
PA
11484 res.comp_dir_storage = ldirname (res.name);
11485 if (!res.comp_dir_storage.empty ())
11486 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11487 }
d721ba37 11488 if (res.comp_dir != NULL)
9291a0cd
TT
11489 {
11490 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11491 directory, get rid of it. */
d721ba37 11492 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11493
d721ba37
PA
11494 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11495 res.comp_dir = cp + 1;
9291a0cd
TT
11496 }
11497
d721ba37
PA
11498 if (res.name == NULL)
11499 res.name = "<unknown>";
11500
11501 return res;
9291a0cd
TT
11502}
11503
f4dc4d17
DE
11504/* Handle DW_AT_stmt_list for a compilation unit.
11505 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11506 COMP_DIR is the compilation directory. LOWPC is passed to
11507 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11508
11509static void
11510handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11511 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11512{
518817b3
SM
11513 struct dwarf2_per_objfile *dwarf2_per_objfile
11514 = cu->per_cu->dwarf2_per_objfile;
527f3840 11515 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11516 struct attribute *attr;
527f3840
JK
11517 struct line_header line_header_local;
11518 hashval_t line_header_local_hash;
527f3840
JK
11519 void **slot;
11520 int decode_mapping;
2ab95328 11521
f4dc4d17
DE
11522 gdb_assert (! cu->per_cu->is_debug_types);
11523
2ab95328 11524 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11525 if (attr == NULL)
11526 return;
11527
9c541725 11528 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11529
11530 /* The line header hash table is only created if needed (it exists to
11531 prevent redundant reading of the line table for partial_units).
11532 If we're given a partial_unit, we'll need it. If we're given a
11533 compile_unit, then use the line header hash table if it's already
11534 created, but don't create one just yet. */
11535
11536 if (dwarf2_per_objfile->line_header_hash == NULL
11537 && die->tag == DW_TAG_partial_unit)
2ab95328 11538 {
527f3840
JK
11539 dwarf2_per_objfile->line_header_hash
11540 = htab_create_alloc_ex (127, line_header_hash_voidp,
11541 line_header_eq_voidp,
11542 free_line_header_voidp,
11543 &objfile->objfile_obstack,
11544 hashtab_obstack_allocate,
11545 dummy_obstack_deallocate);
11546 }
2ab95328 11547
9c541725 11548 line_header_local.sect_off = line_offset;
527f3840
JK
11549 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11550 line_header_local_hash = line_header_hash (&line_header_local);
11551 if (dwarf2_per_objfile->line_header_hash != NULL)
11552 {
11553 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11554 &line_header_local,
11555 line_header_local_hash, NO_INSERT);
11556
11557 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11558 is not present in *SLOT (since if there is something in *SLOT then
11559 it will be for a partial_unit). */
11560 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11561 {
527f3840 11562 gdb_assert (*slot != NULL);
9a3c8263 11563 cu->line_header = (struct line_header *) *slot;
527f3840 11564 return;
dee91e82 11565 }
2ab95328 11566 }
527f3840
JK
11567
11568 /* dwarf_decode_line_header does not yet provide sufficient information.
11569 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11570 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11571 if (lh == NULL)
527f3840 11572 return;
4c8aa72d
PA
11573
11574 cu->line_header = lh.release ();
11575 cu->line_header_die_owner = die;
527f3840
JK
11576
11577 if (dwarf2_per_objfile->line_header_hash == NULL)
11578 slot = NULL;
11579 else
11580 {
11581 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11582 &line_header_local,
11583 line_header_local_hash, INSERT);
11584 gdb_assert (slot != NULL);
11585 }
11586 if (slot != NULL && *slot == NULL)
11587 {
11588 /* This newly decoded line number information unit will be owned
11589 by line_header_hash hash table. */
11590 *slot = cu->line_header;
4c8aa72d 11591 cu->line_header_die_owner = NULL;
527f3840
JK
11592 }
11593 else
11594 {
11595 /* We cannot free any current entry in (*slot) as that struct line_header
11596 may be already used by multiple CUs. Create only temporary decoded
11597 line_header for this CU - it may happen at most once for each line
11598 number information unit. And if we're not using line_header_hash
11599 then this is what we want as well. */
11600 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11601 }
11602 decode_mapping = (die->tag != DW_TAG_partial_unit);
11603 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11604 decode_mapping);
fff8551c 11605
2ab95328
TT
11606}
11607
95554aad 11608/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11609
c906108c 11610static void
e7c27a73 11611read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11612{
518817b3
SM
11613 struct dwarf2_per_objfile *dwarf2_per_objfile
11614 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11615 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11616 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11617 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11618 CORE_ADDR highpc = ((CORE_ADDR) 0);
11619 struct attribute *attr;
c906108c 11620 struct die_info *child_die;
e142c38c 11621 CORE_ADDR baseaddr;
6e70227d 11622
380618d6 11623 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11624 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11625
fae299cd 11626 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11627
11628 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11629 from finish_block. */
2acceee2 11630 if (lowpc == ((CORE_ADDR) -1))
c906108c 11631 lowpc = highpc;
3e29f34a 11632 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11633
d721ba37 11634 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11635
f4b8a18d
KW
11636 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11637 standardised yet. As a workaround for the language detection we fall
11638 back to the DW_AT_producer string. */
11639 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11640 cu->language = language_opencl;
11641
3019eac3
DE
11642 /* Similar hack for Go. */
11643 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11644 set_cu_language (DW_LANG_Go, cu);
11645
c24bdb02 11646 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11647
11648 /* Decode line number information if present. We do this before
11649 processing child DIEs, so that the line header table is available
11650 for DW_AT_decl_file. */
d721ba37 11651 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11652
11653 /* Process all dies in compilation unit. */
11654 if (die->child != NULL)
11655 {
11656 child_die = die->child;
11657 while (child_die && child_die->tag)
11658 {
11659 process_die (child_die, cu);
11660 child_die = sibling_die (child_die);
11661 }
11662 }
11663
11664 /* Decode macro information, if present. Dwarf 2 macro information
11665 refers to information in the line number info statement program
11666 header, so we can only read it if we've read the header
11667 successfully. */
0af92d60
JK
11668 attr = dwarf2_attr (die, DW_AT_macros, cu);
11669 if (attr == NULL)
11670 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11671 if (attr && cu->line_header)
11672 {
11673 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11674 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11675
43f3e411 11676 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11677 }
11678 else
11679 {
11680 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11681 if (attr && cu->line_header)
11682 {
11683 unsigned int macro_offset = DW_UNSND (attr);
11684
43f3e411 11685 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11686 }
11687 }
3019eac3
DE
11688}
11689
c24bdb02
KS
11690void
11691dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11692{
f4dc4d17
DE
11693 struct type_unit_group *tu_group;
11694 int first_time;
3019eac3 11695 struct attribute *attr;
9c541725 11696 unsigned int i;
0186c6a7 11697 struct signatured_type *sig_type;
3019eac3 11698
f4dc4d17 11699 gdb_assert (per_cu->is_debug_types);
0186c6a7 11700 sig_type = (struct signatured_type *) per_cu;
3019eac3 11701
c24bdb02 11702 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11703
f4dc4d17 11704 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11705 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11706 if (sig_type->type_unit_group == NULL)
c24bdb02 11707 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11708 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11709
11710 /* If we've already processed this stmt_list there's no real need to
11711 do it again, we could fake it and just recreate the part we need
11712 (file name,index -> symtab mapping). If data shows this optimization
11713 is useful we can do it then. */
43f3e411 11714 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11715
11716 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11717 debug info. */
fff8551c 11718 line_header_up lh;
f4dc4d17 11719 if (attr != NULL)
3019eac3 11720 {
9c541725 11721 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11722 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11723 }
11724 if (lh == NULL)
11725 {
11726 if (first_time)
c24bdb02 11727 start_symtab ("", NULL, 0);
f4dc4d17
DE
11728 else
11729 {
11730 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11731 gdb_assert (m_builder == nullptr);
804d2729 11732 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11733 m_builder.reset (new struct buildsym_compunit
11734 (COMPUNIT_OBJFILE (cust), "",
11735 COMPUNIT_DIRNAME (cust),
11736 compunit_language (cust),
11737 0, cust));
f4dc4d17 11738 }
f4dc4d17 11739 return;
3019eac3
DE
11740 }
11741
c24bdb02
KS
11742 line_header = lh.release ();
11743 line_header_die_owner = die;
3019eac3 11744
f4dc4d17
DE
11745 if (first_time)
11746 {
c24bdb02 11747 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11748
1fd60fc0
DE
11749 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11750 still initializing it, and our caller (a few levels up)
11751 process_full_type_unit still needs to know if this is the first
11752 time. */
11753
7ba99d21 11754 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11755 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11756 line_header->file_names_size ());
3019eac3 11757
7ba99d21
AT
11758 auto &file_names = line_header->file_names ();
11759 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11760 {
7ba99d21 11761 file_entry &fe = file_names[i];
c24bdb02
KS
11762 dwarf2_start_subfile (this, fe.name,
11763 fe.include_dir (line_header));
11764 buildsym_compunit *b = get_builder ();
11765 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11766 {
4c8aa72d
PA
11767 /* NOTE: start_subfile will recognize when it's been
11768 passed a file it has already seen. So we can't
11769 assume there's a simple mapping from
11770 cu->line_header->file_names to subfiles, plus
11771 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11772 b->get_current_subfile ()->symtab
11773 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11774 }
11775
c24bdb02 11776 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11777 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11778 }
11779 }
11780 else
3019eac3 11781 {
c24bdb02 11782 gdb_assert (m_builder == nullptr);
804d2729 11783 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11784 m_builder.reset (new struct buildsym_compunit
11785 (COMPUNIT_OBJFILE (cust), "",
11786 COMPUNIT_DIRNAME (cust),
11787 compunit_language (cust),
11788 0, cust));
f4dc4d17 11789
7ba99d21
AT
11790 auto &file_names = line_header->file_names ();
11791 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11792 {
7ba99d21 11793 file_entry &fe = file_names[i];
4c8aa72d 11794 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11795 }
3019eac3
DE
11796 }
11797
f4dc4d17
DE
11798 /* The main symtab is allocated last. Type units don't have DW_AT_name
11799 so they don't have a "real" (so to speak) symtab anyway.
11800 There is later code that will assign the main symtab to all symbols
11801 that don't have one. We need to handle the case of a symbol with a
11802 missing symtab (DW_AT_decl_file) anyway. */
11803}
3019eac3 11804
f4dc4d17
DE
11805/* Process DW_TAG_type_unit.
11806 For TUs we want to skip the first top level sibling if it's not the
11807 actual type being defined by this TU. In this case the first top
11808 level sibling is there to provide context only. */
3019eac3 11809
f4dc4d17
DE
11810static void
11811read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11812{
11813 struct die_info *child_die;
3019eac3 11814
f4dc4d17
DE
11815 prepare_one_comp_unit (cu, die, language_minimal);
11816
11817 /* Initialize (or reinitialize) the machinery for building symtabs.
11818 We do this before processing child DIEs, so that the line header table
11819 is available for DW_AT_decl_file. */
c24bdb02 11820 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11821
11822 if (die->child != NULL)
11823 {
11824 child_die = die->child;
11825 while (child_die && child_die->tag)
11826 {
11827 process_die (child_die, cu);
11828 child_die = sibling_die (child_die);
11829 }
11830 }
3019eac3
DE
11831}
11832\f
80626a55
DE
11833/* DWO/DWP files.
11834
11835 http://gcc.gnu.org/wiki/DebugFission
11836 http://gcc.gnu.org/wiki/DebugFissionDWP
11837
11838 To simplify handling of both DWO files ("object" files with the DWARF info)
11839 and DWP files (a file with the DWOs packaged up into one file), we treat
11840 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11841
11842static hashval_t
11843hash_dwo_file (const void *item)
11844{
9a3c8263 11845 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11846 hashval_t hash;
3019eac3 11847
a2ce51a0
DE
11848 hash = htab_hash_string (dwo_file->dwo_name);
11849 if (dwo_file->comp_dir != NULL)
11850 hash += htab_hash_string (dwo_file->comp_dir);
11851 return hash;
3019eac3
DE
11852}
11853
11854static int
11855eq_dwo_file (const void *item_lhs, const void *item_rhs)
11856{
9a3c8263
SM
11857 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11858 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11859
a2ce51a0
DE
11860 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11861 return 0;
11862 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11863 return lhs->comp_dir == rhs->comp_dir;
11864 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11865}
11866
11867/* Allocate a hash table for DWO files. */
11868
51ac9db5 11869static htab_up
ed2dc618 11870allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11871{
51ac9db5
SM
11872 auto delete_dwo_file = [] (void *item)
11873 {
11874 struct dwo_file *dwo_file = (struct dwo_file *) item;
11875
11876 delete dwo_file;
11877 };
11878
11879 return htab_up (htab_create_alloc_ex (41,
11880 hash_dwo_file,
11881 eq_dwo_file,
11882 delete_dwo_file,
11883 &objfile->objfile_obstack,
11884 hashtab_obstack_allocate,
11885 dummy_obstack_deallocate));
3019eac3
DE
11886}
11887
80626a55
DE
11888/* Lookup DWO file DWO_NAME. */
11889
11890static void **
ed2dc618
SM
11891lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11892 const char *dwo_name,
11893 const char *comp_dir)
80626a55
DE
11894{
11895 struct dwo_file find_entry;
11896 void **slot;
11897
11898 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11899 dwarf2_per_objfile->dwo_files
11900 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11901
0ac5b59e
DE
11902 find_entry.dwo_name = dwo_name;
11903 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11904 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11905 INSERT);
80626a55
DE
11906
11907 return slot;
11908}
11909
3019eac3
DE
11910static hashval_t
11911hash_dwo_unit (const void *item)
11912{
9a3c8263 11913 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11914
11915 /* This drops the top 32 bits of the id, but is ok for a hash. */
11916 return dwo_unit->signature;
11917}
11918
11919static int
11920eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11921{
9a3c8263
SM
11922 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11923 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11924
11925 /* The signature is assumed to be unique within the DWO file.
11926 So while object file CU dwo_id's always have the value zero,
11927 that's OK, assuming each object file DWO file has only one CU,
11928 and that's the rule for now. */
11929 return lhs->signature == rhs->signature;
11930}
11931
11932/* Allocate a hash table for DWO CUs,TUs.
11933 There is one of these tables for each of CUs,TUs for each DWO file. */
11934
11935static htab_t
11936allocate_dwo_unit_table (struct objfile *objfile)
11937{
11938 /* Start out with a pretty small number.
11939 Generally DWO files contain only one CU and maybe some TUs. */
11940 return htab_create_alloc_ex (3,
11941 hash_dwo_unit,
11942 eq_dwo_unit,
11943 NULL,
11944 &objfile->objfile_obstack,
11945 hashtab_obstack_allocate,
11946 dummy_obstack_deallocate);
11947}
11948
80626a55 11949/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11950
19c3d4c9 11951struct create_dwo_cu_data
3019eac3
DE
11952{
11953 struct dwo_file *dwo_file;
19c3d4c9 11954 struct dwo_unit dwo_unit;
3019eac3
DE
11955};
11956
19c3d4c9 11957/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11958
11959static void
19c3d4c9
DE
11960create_dwo_cu_reader (const struct die_reader_specs *reader,
11961 const gdb_byte *info_ptr,
11962 struct die_info *comp_unit_die,
11963 int has_children,
11964 void *datap)
3019eac3
DE
11965{
11966 struct dwarf2_cu *cu = reader->cu;
9c541725 11967 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11968 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11969 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11970 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11971 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11972
a084a2a6
AT
11973 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11974 if (!signature.has_value ())
3019eac3 11975 {
b98664d3 11976 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11977 " its dwo_id [in module %s]"),
9d8780f0 11978 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11979 return;
11980 }
11981
3019eac3 11982 dwo_unit->dwo_file = dwo_file;
a084a2a6 11983 dwo_unit->signature = *signature;
8a0459fd 11984 dwo_unit->section = section;
9c541725 11985 dwo_unit->sect_off = sect_off;
3019eac3
DE
11986 dwo_unit->length = cu->per_cu->length;
11987
b4f54984 11988 if (dwarf_read_debug)
9d8780f0
SM
11989 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11990 sect_offset_str (sect_off),
9c541725 11991 hex_string (dwo_unit->signature));
3019eac3
DE
11992}
11993
33c5cd75 11994/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11995 Note: This function processes DWO files only, not DWP files. */
3019eac3 11996
33c5cd75 11997static void
ed2dc618
SM
11998create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11999 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 12000 htab_t &cus_htab)
3019eac3
DE
12001{
12002 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 12003 const gdb_byte *info_ptr, *end_ptr;
3019eac3 12004
33c5cd75
DB
12005 dwarf2_read_section (objfile, &section);
12006 info_ptr = section.buffer;
3019eac3
DE
12007
12008 if (info_ptr == NULL)
33c5cd75 12009 return;
3019eac3 12010
b4f54984 12011 if (dwarf_read_debug)
19c3d4c9
DE
12012 {
12013 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
12014 get_section_name (&section),
12015 get_section_file_name (&section));
19c3d4c9 12016 }
3019eac3 12017
33c5cd75 12018 end_ptr = info_ptr + section.size;
3019eac3
DE
12019 while (info_ptr < end_ptr)
12020 {
12021 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12022 struct create_dwo_cu_data create_dwo_cu_data;
12023 struct dwo_unit *dwo_unit;
12024 void **slot;
12025 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12026
19c3d4c9
DE
12027 memset (&create_dwo_cu_data.dwo_unit, 0,
12028 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12029 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12030 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12031 per_cu.is_debug_types = 0;
33c5cd75
DB
12032 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12033 per_cu.section = &section;
c5ed0576 12034 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12035
12036 init_cutu_and_read_dies_no_follow (
12037 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12038 info_ptr += per_cu.length;
12039
12040 // If the unit could not be parsed, skip it.
12041 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12042 continue;
3019eac3 12043
33c5cd75
DB
12044 if (cus_htab == NULL)
12045 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12046
33c5cd75
DB
12047 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12048 *dwo_unit = create_dwo_cu_data.dwo_unit;
12049 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12050 gdb_assert (slot != NULL);
12051 if (*slot != NULL)
19c3d4c9 12052 {
33c5cd75
DB
12053 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12054 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12055
b98664d3 12056 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12057 " the entry at offset %s, signature %s"),
12058 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12059 hex_string (dwo_unit->signature));
19c3d4c9 12060 }
33c5cd75 12061 *slot = (void *)dwo_unit;
3019eac3 12062 }
3019eac3
DE
12063}
12064
80626a55
DE
12065/* DWP file .debug_{cu,tu}_index section format:
12066 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12067
d2415c6c
DE
12068 DWP Version 1:
12069
80626a55
DE
12070 Both index sections have the same format, and serve to map a 64-bit
12071 signature to a set of section numbers. Each section begins with a header,
12072 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12073 indexes, and a pool of 32-bit section numbers. The index sections will be
12074 aligned at 8-byte boundaries in the file.
12075
d2415c6c
DE
12076 The index section header consists of:
12077
12078 V, 32 bit version number
12079 -, 32 bits unused
12080 N, 32 bit number of compilation units or type units in the index
12081 M, 32 bit number of slots in the hash table
80626a55 12082
d2415c6c 12083 Numbers are recorded using the byte order of the application binary.
80626a55 12084
d2415c6c
DE
12085 The hash table begins at offset 16 in the section, and consists of an array
12086 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12087 order of the application binary). Unused slots in the hash table are 0.
12088 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12089
d2415c6c
DE
12090 The parallel table begins immediately after the hash table
12091 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12092 array of 32-bit indexes (using the byte order of the application binary),
12093 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12094 table contains a 32-bit index into the pool of section numbers. For unused
12095 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12096
73869dc2
DE
12097 The pool of section numbers begins immediately following the hash table
12098 (at offset 16 + 12 * M from the beginning of the section). The pool of
12099 section numbers consists of an array of 32-bit words (using the byte order
12100 of the application binary). Each item in the array is indexed starting
12101 from 0. The hash table entry provides the index of the first section
12102 number in the set. Additional section numbers in the set follow, and the
12103 set is terminated by a 0 entry (section number 0 is not used in ELF).
12104
12105 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12106 section must be the first entry in the set, and the .debug_abbrev.dwo must
12107 be the second entry. Other members of the set may follow in any order.
12108
12109 ---
12110
12111 DWP Version 2:
12112
12113 DWP Version 2 combines all the .debug_info, etc. sections into one,
12114 and the entries in the index tables are now offsets into these sections.
12115 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12116 section.
12117
12118 Index Section Contents:
12119 Header
12120 Hash Table of Signatures dwp_hash_table.hash_table
12121 Parallel Table of Indices dwp_hash_table.unit_table
12122 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12123 Table of Section Sizes dwp_hash_table.v2.sizes
12124
12125 The index section header consists of:
12126
12127 V, 32 bit version number
12128 L, 32 bit number of columns in the table of section offsets
12129 N, 32 bit number of compilation units or type units in the index
12130 M, 32 bit number of slots in the hash table
12131
12132 Numbers are recorded using the byte order of the application binary.
12133
12134 The hash table has the same format as version 1.
12135 The parallel table of indices has the same format as version 1,
12136 except that the entries are origin-1 indices into the table of sections
12137 offsets and the table of section sizes.
12138
12139 The table of offsets begins immediately following the parallel table
12140 (at offset 16 + 12 * M from the beginning of the section). The table is
12141 a two-dimensional array of 32-bit words (using the byte order of the
12142 application binary), with L columns and N+1 rows, in row-major order.
12143 Each row in the array is indexed starting from 0. The first row provides
12144 a key to the remaining rows: each column in this row provides an identifier
12145 for a debug section, and the offsets in the same column of subsequent rows
12146 refer to that section. The section identifiers are:
12147
12148 DW_SECT_INFO 1 .debug_info.dwo
12149 DW_SECT_TYPES 2 .debug_types.dwo
12150 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12151 DW_SECT_LINE 4 .debug_line.dwo
12152 DW_SECT_LOC 5 .debug_loc.dwo
12153 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12154 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12155 DW_SECT_MACRO 8 .debug_macro.dwo
12156
12157 The offsets provided by the CU and TU index sections are the base offsets
12158 for the contributions made by each CU or TU to the corresponding section
12159 in the package file. Each CU and TU header contains an abbrev_offset
12160 field, used to find the abbreviations table for that CU or TU within the
12161 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12162 be interpreted as relative to the base offset given in the index section.
12163 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12164 should be interpreted as relative to the base offset for .debug_line.dwo,
12165 and offsets into other debug sections obtained from DWARF attributes should
12166 also be interpreted as relative to the corresponding base offset.
12167
12168 The table of sizes begins immediately following the table of offsets.
12169 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12170 with L columns and N rows, in row-major order. Each row in the array is
12171 indexed starting from 1 (row 0 is shared by the two tables).
12172
12173 ---
12174
12175 Hash table lookup is handled the same in version 1 and 2:
12176
12177 We assume that N and M will not exceed 2^32 - 1.
12178 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12179
d2415c6c
DE
12180 Given a 64-bit compilation unit signature or a type signature S, an entry
12181 in the hash table is located as follows:
80626a55 12182
d2415c6c
DE
12183 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12184 the low-order k bits all set to 1.
80626a55 12185
d2415c6c 12186 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12187
d2415c6c
DE
12188 3) If the hash table entry at index H matches the signature, use that
12189 entry. If the hash table entry at index H is unused (all zeroes),
12190 terminate the search: the signature is not present in the table.
80626a55 12191
d2415c6c 12192 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12193
d2415c6c 12194 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12195 to stop at an unused slot or find the match. */
80626a55
DE
12196
12197/* Create a hash table to map DWO IDs to their CU/TU entry in
12198 .debug_{info,types}.dwo in DWP_FILE.
12199 Returns NULL if there isn't one.
12200 Note: This function processes DWP files only, not DWO files. */
12201
12202static struct dwp_hash_table *
ed2dc618
SM
12203create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12204 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12205{
12206 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12207 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12208 const gdb_byte *index_ptr, *index_end;
80626a55 12209 struct dwarf2_section_info *index;
73869dc2 12210 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12211 struct dwp_hash_table *htab;
12212
12213 if (is_debug_types)
12214 index = &dwp_file->sections.tu_index;
12215 else
12216 index = &dwp_file->sections.cu_index;
12217
12218 if (dwarf2_section_empty_p (index))
12219 return NULL;
12220 dwarf2_read_section (objfile, index);
12221
12222 index_ptr = index->buffer;
12223 index_end = index_ptr + index->size;
12224
12225 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12226 index_ptr += 4;
12227 if (version == 2)
12228 nr_columns = read_4_bytes (dbfd, index_ptr);
12229 else
12230 nr_columns = 0;
12231 index_ptr += 4;
80626a55
DE
12232 nr_units = read_4_bytes (dbfd, index_ptr);
12233 index_ptr += 4;
12234 nr_slots = read_4_bytes (dbfd, index_ptr);
12235 index_ptr += 4;
12236
73869dc2 12237 if (version != 1 && version != 2)
80626a55 12238 {
21aa081e 12239 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12240 " [in module %s]"),
21aa081e 12241 pulongest (version), dwp_file->name);
80626a55
DE
12242 }
12243 if (nr_slots != (nr_slots & -nr_slots))
12244 {
21aa081e 12245 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12246 " is not power of 2 [in module %s]"),
21aa081e 12247 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12248 }
12249
12250 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12251 htab->version = version;
12252 htab->nr_columns = nr_columns;
80626a55
DE
12253 htab->nr_units = nr_units;
12254 htab->nr_slots = nr_slots;
12255 htab->hash_table = index_ptr;
12256 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12257
12258 /* Exit early if the table is empty. */
12259 if (nr_slots == 0 || nr_units == 0
12260 || (version == 2 && nr_columns == 0))
12261 {
12262 /* All must be zero. */
12263 if (nr_slots != 0 || nr_units != 0
12264 || (version == 2 && nr_columns != 0))
12265 {
b98664d3 12266 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12267 " all zero [in modules %s]"),
12268 dwp_file->name);
12269 }
12270 return htab;
12271 }
12272
12273 if (version == 1)
12274 {
12275 htab->section_pool.v1.indices =
12276 htab->unit_table + sizeof (uint32_t) * nr_slots;
12277 /* It's harder to decide whether the section is too small in v1.
12278 V1 is deprecated anyway so we punt. */
12279 }
12280 else
12281 {
12282 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12283 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12284 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12285 /* Reverse map for error checking. */
12286 int ids_seen[DW_SECT_MAX + 1];
12287 int i;
12288
12289 if (nr_columns < 2)
12290 {
12291 error (_("Dwarf Error: bad DWP hash table, too few columns"
12292 " in section table [in module %s]"),
12293 dwp_file->name);
12294 }
12295 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12296 {
12297 error (_("Dwarf Error: bad DWP hash table, too many columns"
12298 " in section table [in module %s]"),
12299 dwp_file->name);
12300 }
04fd5eed
GB
12301 memset (ids, 255, sizeof_ids);
12302 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12303 for (i = 0; i < nr_columns; ++i)
12304 {
12305 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12306
12307 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12308 {
12309 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12310 " in section table [in module %s]"),
12311 id, dwp_file->name);
12312 }
12313 if (ids_seen[id] != -1)
12314 {
12315 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12316 " id %d in section table [in module %s]"),
12317 id, dwp_file->name);
12318 }
12319 ids_seen[id] = i;
12320 ids[i] = id;
12321 }
12322 /* Must have exactly one info or types section. */
12323 if (((ids_seen[DW_SECT_INFO] != -1)
12324 + (ids_seen[DW_SECT_TYPES] != -1))
12325 != 1)
12326 {
12327 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12328 " DWO info/types section [in module %s]"),
12329 dwp_file->name);
12330 }
12331 /* Must have an abbrev section. */
12332 if (ids_seen[DW_SECT_ABBREV] == -1)
12333 {
12334 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12335 " section [in module %s]"),
12336 dwp_file->name);
12337 }
12338 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12339 htab->section_pool.v2.sizes =
12340 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12341 * nr_units * nr_columns);
12342 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12343 * nr_units * nr_columns))
12344 > index_end)
12345 {
12346 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12347 " [in module %s]"),
12348 dwp_file->name);
12349 }
12350 }
80626a55
DE
12351
12352 return htab;
12353}
12354
12355/* Update SECTIONS with the data from SECTP.
12356
12357 This function is like the other "locate" section routines that are
12358 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12359 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12360
12361 The result is non-zero for success, or zero if an error was found. */
12362
12363static int
73869dc2
DE
12364locate_v1_virtual_dwo_sections (asection *sectp,
12365 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12366{
12367 const struct dwop_section_names *names = &dwop_section_names;
12368
12369 if (section_is_p (sectp->name, &names->abbrev_dwo))
12370 {
12371 /* There can be only one. */
049412e3 12372 if (sections->abbrev.s.section != NULL)
80626a55 12373 return 0;
049412e3 12374 sections->abbrev.s.section = sectp;
fd361982 12375 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12376 }
12377 else if (section_is_p (sectp->name, &names->info_dwo)
12378 || section_is_p (sectp->name, &names->types_dwo))
12379 {
12380 /* There can be only one. */
049412e3 12381 if (sections->info_or_types.s.section != NULL)
80626a55 12382 return 0;
049412e3 12383 sections->info_or_types.s.section = sectp;
fd361982 12384 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12385 }
12386 else if (section_is_p (sectp->name, &names->line_dwo))
12387 {
12388 /* There can be only one. */
049412e3 12389 if (sections->line.s.section != NULL)
80626a55 12390 return 0;
049412e3 12391 sections->line.s.section = sectp;
fd361982 12392 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12393 }
12394 else if (section_is_p (sectp->name, &names->loc_dwo))
12395 {
12396 /* There can be only one. */
049412e3 12397 if (sections->loc.s.section != NULL)
80626a55 12398 return 0;
049412e3 12399 sections->loc.s.section = sectp;
fd361982 12400 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12401 }
12402 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12403 {
12404 /* There can be only one. */
049412e3 12405 if (sections->macinfo.s.section != NULL)
80626a55 12406 return 0;
049412e3 12407 sections->macinfo.s.section = sectp;
fd361982 12408 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12409 }
12410 else if (section_is_p (sectp->name, &names->macro_dwo))
12411 {
12412 /* There can be only one. */
049412e3 12413 if (sections->macro.s.section != NULL)
80626a55 12414 return 0;
049412e3 12415 sections->macro.s.section = sectp;
fd361982 12416 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12417 }
12418 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12419 {
12420 /* There can be only one. */
049412e3 12421 if (sections->str_offsets.s.section != NULL)
80626a55 12422 return 0;
049412e3 12423 sections->str_offsets.s.section = sectp;
fd361982 12424 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12425 }
12426 else
12427 {
12428 /* No other kind of section is valid. */
12429 return 0;
12430 }
12431
12432 return 1;
12433}
12434
73869dc2
DE
12435/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12436 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12437 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12438 This is for DWP version 1 files. */
80626a55
DE
12439
12440static struct dwo_unit *
ed2dc618
SM
12441create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12442 struct dwp_file *dwp_file,
73869dc2
DE
12443 uint32_t unit_index,
12444 const char *comp_dir,
12445 ULONGEST signature, int is_debug_types)
80626a55
DE
12446{
12447 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12448 const struct dwp_hash_table *dwp_htab =
12449 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12450 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12451 const char *kind = is_debug_types ? "TU" : "CU";
12452 struct dwo_file *dwo_file;
12453 struct dwo_unit *dwo_unit;
73869dc2 12454 struct virtual_v1_dwo_sections sections;
80626a55 12455 void **dwo_file_slot;
80626a55
DE
12456 int i;
12457
73869dc2
DE
12458 gdb_assert (dwp_file->version == 1);
12459
b4f54984 12460 if (dwarf_read_debug)
80626a55 12461 {
73869dc2 12462 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12463 kind,
73869dc2 12464 pulongest (unit_index), hex_string (signature),
80626a55
DE
12465 dwp_file->name);
12466 }
12467
19ac8c2e 12468 /* Fetch the sections of this DWO unit.
80626a55
DE
12469 Put a limit on the number of sections we look for so that bad data
12470 doesn't cause us to loop forever. */
12471
73869dc2 12472#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12473 (1 /* .debug_info or .debug_types */ \
12474 + 1 /* .debug_abbrev */ \
12475 + 1 /* .debug_line */ \
12476 + 1 /* .debug_loc */ \
12477 + 1 /* .debug_str_offsets */ \
19ac8c2e 12478 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12479 + 1 /* trailing zero */)
12480
12481 memset (&sections, 0, sizeof (sections));
80626a55 12482
73869dc2 12483 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12484 {
12485 asection *sectp;
12486 uint32_t section_nr =
12487 read_4_bytes (dbfd,
73869dc2
DE
12488 dwp_htab->section_pool.v1.indices
12489 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12490
12491 if (section_nr == 0)
12492 break;
12493 if (section_nr >= dwp_file->num_sections)
12494 {
12495 error (_("Dwarf Error: bad DWP hash table, section number too large"
12496 " [in module %s]"),
12497 dwp_file->name);
12498 }
12499
12500 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12501 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12502 {
12503 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12504 " [in module %s]"),
12505 dwp_file->name);
12506 }
12507 }
12508
12509 if (i < 2
a32a8923
DE
12510 || dwarf2_section_empty_p (&sections.info_or_types)
12511 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12512 {
12513 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12514 " [in module %s]"),
12515 dwp_file->name);
12516 }
73869dc2 12517 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12518 {
12519 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12520 " [in module %s]"),
12521 dwp_file->name);
12522 }
12523
12524 /* It's easier for the rest of the code if we fake a struct dwo_file and
12525 have dwo_unit "live" in that. At least for now.
12526
12527 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12528 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12529 file, we can combine them back into a virtual DWO file to save space
12530 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12531 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12532
791afaa2
TT
12533 std::string virtual_dwo_name =
12534 string_printf ("virtual-dwo/%d-%d-%d-%d",
12535 get_section_id (&sections.abbrev),
12536 get_section_id (&sections.line),
12537 get_section_id (&sections.loc),
12538 get_section_id (&sections.str_offsets));
80626a55 12539 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12540 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12541 virtual_dwo_name.c_str (),
12542 comp_dir);
80626a55
DE
12543 /* Create one if necessary. */
12544 if (*dwo_file_slot == NULL)
12545 {
b4f54984 12546 if (dwarf_read_debug)
80626a55
DE
12547 {
12548 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12549 virtual_dwo_name.c_str ());
80626a55 12550 }
51ac9db5 12551 dwo_file = new struct dwo_file;
efba19b0
TT
12552 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12553 virtual_dwo_name);
0ac5b59e 12554 dwo_file->comp_dir = comp_dir;
80626a55
DE
12555 dwo_file->sections.abbrev = sections.abbrev;
12556 dwo_file->sections.line = sections.line;
12557 dwo_file->sections.loc = sections.loc;
12558 dwo_file->sections.macinfo = sections.macinfo;
12559 dwo_file->sections.macro = sections.macro;
12560 dwo_file->sections.str_offsets = sections.str_offsets;
12561 /* The "str" section is global to the entire DWP file. */
12562 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12563 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12564 there's no need to record it in dwo_file.
12565 Also, we can't simply record type sections in dwo_file because
12566 we record a pointer into the vector in dwo_unit. As we collect more
12567 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12568 for it, invalidating all copies of pointers into the previous
12569 contents. */
80626a55
DE
12570 *dwo_file_slot = dwo_file;
12571 }
12572 else
12573 {
b4f54984 12574 if (dwarf_read_debug)
80626a55
DE
12575 {
12576 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12577 virtual_dwo_name.c_str ());
80626a55 12578 }
9a3c8263 12579 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12580 }
80626a55
DE
12581
12582 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12583 dwo_unit->dwo_file = dwo_file;
12584 dwo_unit->signature = signature;
8d749320
SM
12585 dwo_unit->section =
12586 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12587 *dwo_unit->section = sections.info_or_types;
57d63ce2 12588 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12589
12590 return dwo_unit;
12591}
12592
73869dc2
DE
12593/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12594 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12595 piece within that section used by a TU/CU, return a virtual section
12596 of just that piece. */
12597
12598static struct dwarf2_section_info
ed2dc618
SM
12599create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12600 struct dwarf2_section_info *section,
73869dc2
DE
12601 bfd_size_type offset, bfd_size_type size)
12602{
12603 struct dwarf2_section_info result;
12604 asection *sectp;
12605
12606 gdb_assert (section != NULL);
12607 gdb_assert (!section->is_virtual);
12608
12609 memset (&result, 0, sizeof (result));
12610 result.s.containing_section = section;
dc4ccb6f 12611 result.is_virtual = true;
73869dc2
DE
12612
12613 if (size == 0)
12614 return result;
12615
12616 sectp = get_section_bfd_section (section);
12617
12618 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12619 bounds of the real section. This is a pretty-rare event, so just
12620 flag an error (easier) instead of a warning and trying to cope. */
12621 if (sectp == NULL
fd361982 12622 || offset + size > bfd_section_size (sectp))
73869dc2 12623 {
73869dc2
DE
12624 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12625 " in section %s [in module %s]"),
fd361982 12626 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12627 objfile_name (dwarf2_per_objfile->objfile));
12628 }
12629
12630 result.virtual_offset = offset;
12631 result.size = size;
12632 return result;
12633}
12634
12635/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12636 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12637 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12638 This is for DWP version 2 files. */
12639
12640static struct dwo_unit *
ed2dc618
SM
12641create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12642 struct dwp_file *dwp_file,
73869dc2
DE
12643 uint32_t unit_index,
12644 const char *comp_dir,
12645 ULONGEST signature, int is_debug_types)
12646{
12647 struct objfile *objfile = dwarf2_per_objfile->objfile;
12648 const struct dwp_hash_table *dwp_htab =
12649 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12650 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12651 const char *kind = is_debug_types ? "TU" : "CU";
12652 struct dwo_file *dwo_file;
12653 struct dwo_unit *dwo_unit;
12654 struct virtual_v2_dwo_sections sections;
12655 void **dwo_file_slot;
73869dc2
DE
12656 int i;
12657
12658 gdb_assert (dwp_file->version == 2);
12659
b4f54984 12660 if (dwarf_read_debug)
73869dc2
DE
12661 {
12662 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12663 kind,
12664 pulongest (unit_index), hex_string (signature),
12665 dwp_file->name);
12666 }
12667
12668 /* Fetch the section offsets of this DWO unit. */
12669
12670 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12671
12672 for (i = 0; i < dwp_htab->nr_columns; ++i)
12673 {
12674 uint32_t offset = read_4_bytes (dbfd,
12675 dwp_htab->section_pool.v2.offsets
12676 + (((unit_index - 1) * dwp_htab->nr_columns
12677 + i)
12678 * sizeof (uint32_t)));
12679 uint32_t size = read_4_bytes (dbfd,
12680 dwp_htab->section_pool.v2.sizes
12681 + (((unit_index - 1) * dwp_htab->nr_columns
12682 + i)
12683 * sizeof (uint32_t)));
12684
12685 switch (dwp_htab->section_pool.v2.section_ids[i])
12686 {
12687 case DW_SECT_INFO:
12688 case DW_SECT_TYPES:
12689 sections.info_or_types_offset = offset;
12690 sections.info_or_types_size = size;
12691 break;
12692 case DW_SECT_ABBREV:
12693 sections.abbrev_offset = offset;
12694 sections.abbrev_size = size;
12695 break;
12696 case DW_SECT_LINE:
12697 sections.line_offset = offset;
12698 sections.line_size = size;
12699 break;
12700 case DW_SECT_LOC:
12701 sections.loc_offset = offset;
12702 sections.loc_size = size;
12703 break;
12704 case DW_SECT_STR_OFFSETS:
12705 sections.str_offsets_offset = offset;
12706 sections.str_offsets_size = size;
12707 break;
12708 case DW_SECT_MACINFO:
12709 sections.macinfo_offset = offset;
12710 sections.macinfo_size = size;
12711 break;
12712 case DW_SECT_MACRO:
12713 sections.macro_offset = offset;
12714 sections.macro_size = size;
12715 break;
12716 }
12717 }
12718
12719 /* It's easier for the rest of the code if we fake a struct dwo_file and
12720 have dwo_unit "live" in that. At least for now.
12721
12722 The DWP file can be made up of a random collection of CUs and TUs.
12723 However, for each CU + set of TUs that came from the same original DWO
12724 file, we can combine them back into a virtual DWO file to save space
12725 (fewer struct dwo_file objects to allocate). Remember that for really
12726 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12727
791afaa2
TT
12728 std::string virtual_dwo_name =
12729 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12730 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12731 (long) (sections.line_size ? sections.line_offset : 0),
12732 (long) (sections.loc_size ? sections.loc_offset : 0),
12733 (long) (sections.str_offsets_size
12734 ? sections.str_offsets_offset : 0));
73869dc2 12735 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12736 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12737 virtual_dwo_name.c_str (),
12738 comp_dir);
73869dc2
DE
12739 /* Create one if necessary. */
12740 if (*dwo_file_slot == NULL)
12741 {
b4f54984 12742 if (dwarf_read_debug)
73869dc2
DE
12743 {
12744 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12745 virtual_dwo_name.c_str ());
73869dc2 12746 }
51ac9db5 12747 dwo_file = new struct dwo_file;
efba19b0
TT
12748 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12749 virtual_dwo_name);
73869dc2
DE
12750 dwo_file->comp_dir = comp_dir;
12751 dwo_file->sections.abbrev =
ed2dc618 12752 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12753 sections.abbrev_offset, sections.abbrev_size);
12754 dwo_file->sections.line =
ed2dc618 12755 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12756 sections.line_offset, sections.line_size);
12757 dwo_file->sections.loc =
ed2dc618 12758 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12759 sections.loc_offset, sections.loc_size);
12760 dwo_file->sections.macinfo =
ed2dc618 12761 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12762 sections.macinfo_offset, sections.macinfo_size);
12763 dwo_file->sections.macro =
ed2dc618 12764 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12765 sections.macro_offset, sections.macro_size);
12766 dwo_file->sections.str_offsets =
ed2dc618
SM
12767 create_dwp_v2_section (dwarf2_per_objfile,
12768 &dwp_file->sections.str_offsets,
73869dc2
DE
12769 sections.str_offsets_offset,
12770 sections.str_offsets_size);
12771 /* The "str" section is global to the entire DWP file. */
12772 dwo_file->sections.str = dwp_file->sections.str;
12773 /* The info or types section is assigned below to dwo_unit,
12774 there's no need to record it in dwo_file.
12775 Also, we can't simply record type sections in dwo_file because
12776 we record a pointer into the vector in dwo_unit. As we collect more
12777 types we'll grow the vector and eventually have to reallocate space
12778 for it, invalidating all copies of pointers into the previous
12779 contents. */
12780 *dwo_file_slot = dwo_file;
12781 }
12782 else
12783 {
b4f54984 12784 if (dwarf_read_debug)
73869dc2
DE
12785 {
12786 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12787 virtual_dwo_name.c_str ());
73869dc2 12788 }
9a3c8263 12789 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12790 }
73869dc2
DE
12791
12792 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12793 dwo_unit->dwo_file = dwo_file;
12794 dwo_unit->signature = signature;
8d749320
SM
12795 dwo_unit->section =
12796 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12797 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12798 is_debug_types
73869dc2
DE
12799 ? &dwp_file->sections.types
12800 : &dwp_file->sections.info,
12801 sections.info_or_types_offset,
12802 sections.info_or_types_size);
12803 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12804
12805 return dwo_unit;
12806}
12807
57d63ce2
DE
12808/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12809 Returns NULL if the signature isn't found. */
80626a55
DE
12810
12811static struct dwo_unit *
ed2dc618
SM
12812lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12813 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12814 ULONGEST signature, int is_debug_types)
80626a55 12815{
57d63ce2
DE
12816 const struct dwp_hash_table *dwp_htab =
12817 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12818 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12819 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12820 uint32_t hash = signature & mask;
12821 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12822 unsigned int i;
12823 void **slot;
870f88f7 12824 struct dwo_unit find_dwo_cu;
80626a55
DE
12825
12826 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12827 find_dwo_cu.signature = signature;
19ac8c2e
DE
12828 slot = htab_find_slot (is_debug_types
12829 ? dwp_file->loaded_tus
12830 : dwp_file->loaded_cus,
12831 &find_dwo_cu, INSERT);
80626a55
DE
12832
12833 if (*slot != NULL)
9a3c8263 12834 return (struct dwo_unit *) *slot;
80626a55
DE
12835
12836 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12837 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12838 {
12839 ULONGEST signature_in_table;
12840
12841 signature_in_table =
57d63ce2 12842 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12843 if (signature_in_table == signature)
12844 {
57d63ce2
DE
12845 uint32_t unit_index =
12846 read_4_bytes (dbfd,
12847 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12848
73869dc2
DE
12849 if (dwp_file->version == 1)
12850 {
ed2dc618
SM
12851 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12852 dwp_file, unit_index,
73869dc2
DE
12853 comp_dir, signature,
12854 is_debug_types);
12855 }
12856 else
12857 {
ed2dc618
SM
12858 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12859 dwp_file, unit_index,
73869dc2
DE
12860 comp_dir, signature,
12861 is_debug_types);
12862 }
9a3c8263 12863 return (struct dwo_unit *) *slot;
80626a55
DE
12864 }
12865 if (signature_in_table == 0)
12866 return NULL;
12867 hash = (hash + hash2) & mask;
12868 }
12869
12870 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12871 " [in module %s]"),
12872 dwp_file->name);
12873}
12874
ab5088bf 12875/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12876 Open the file specified by FILE_NAME and hand it off to BFD for
12877 preliminary analysis. Return a newly initialized bfd *, which
12878 includes a canonicalized copy of FILE_NAME.
80626a55 12879 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12880 SEARCH_CWD is true if the current directory is to be searched.
12881 It will be searched before debug-file-directory.
13aaf454
DE
12882 If successful, the file is added to the bfd include table of the
12883 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12884 If unable to find/open the file, return NULL.
3019eac3
DE
12885 NOTE: This function is derived from symfile_bfd_open. */
12886
192b62ce 12887static gdb_bfd_ref_ptr
ed2dc618
SM
12888try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12889 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12890{
24b9144d 12891 int desc;
9c02c129
DE
12892 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12893 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12894 to debug_file_directory. */
e0cc99a6 12895 const char *search_path;
9c02c129
DE
12896 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12897
e0cc99a6 12898 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12899 if (search_cwd)
12900 {
12901 if (*debug_file_directory != '\0')
e0cc99a6
TT
12902 {
12903 search_path_holder.reset (concat (".", dirname_separator_string,
12904 debug_file_directory,
12905 (char *) NULL));
12906 search_path = search_path_holder.get ();
12907 }
6ac97d4c 12908 else
e0cc99a6 12909 search_path = ".";
6ac97d4c 12910 }
9c02c129 12911 else
e0cc99a6 12912 search_path = debug_file_directory;
3019eac3 12913
24b9144d 12914 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12915 if (is_dwp)
12916 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12917
12918 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12919 desc = openp (search_path, flags, file_name,
3019eac3
DE
12920 O_RDONLY | O_BINARY, &absolute_name);
12921 if (desc < 0)
12922 return NULL;
12923
e0cc99a6
TT
12924 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12925 gnutarget, desc));
9c02c129
DE
12926 if (sym_bfd == NULL)
12927 return NULL;
192b62ce 12928 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12929
192b62ce
TT
12930 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12931 return NULL;
3019eac3 12932
13aaf454
DE
12933 /* Success. Record the bfd as having been included by the objfile's bfd.
12934 This is important because things like demangled_names_hash lives in the
12935 objfile's per_bfd space and may have references to things like symbol
12936 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12937 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12938
3019eac3
DE
12939 return sym_bfd;
12940}
12941
ab5088bf 12942/* Try to open DWO file FILE_NAME.
3019eac3
DE
12943 COMP_DIR is the DW_AT_comp_dir attribute.
12944 The result is the bfd handle of the file.
12945 If there is a problem finding or opening the file, return NULL.
12946 Upon success, the canonicalized path of the file is stored in the bfd,
12947 same as symfile_bfd_open. */
12948
192b62ce 12949static gdb_bfd_ref_ptr
ed2dc618
SM
12950open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12951 const char *file_name, const char *comp_dir)
3019eac3 12952{
80626a55 12953 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12954 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12955 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12956
12957 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12958
12959 if (comp_dir != NULL)
12960 {
b36cec19
PA
12961 char *path_to_try = concat (comp_dir, SLASH_STRING,
12962 file_name, (char *) NULL);
3019eac3
DE
12963
12964 /* NOTE: If comp_dir is a relative path, this will also try the
12965 search path, which seems useful. */
ed2dc618
SM
12966 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12967 path_to_try,
12968 0 /*is_dwp*/,
192b62ce 12969 1 /*search_cwd*/));
3019eac3
DE
12970 xfree (path_to_try);
12971 if (abfd != NULL)
12972 return abfd;
12973 }
12974
12975 /* That didn't work, try debug-file-directory, which, despite its name,
12976 is a list of paths. */
12977
12978 if (*debug_file_directory == '\0')
12979 return NULL;
12980
ed2dc618
SM
12981 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12982 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12983}
12984
80626a55
DE
12985/* This function is mapped across the sections and remembers the offset and
12986 size of each of the DWO debugging sections we are interested in. */
12987
12988static void
12989dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12990{
9a3c8263 12991 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12992 const struct dwop_section_names *names = &dwop_section_names;
12993
12994 if (section_is_p (sectp->name, &names->abbrev_dwo))
12995 {
049412e3 12996 dwo_sections->abbrev.s.section = sectp;
fd361982 12997 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12998 }
12999 else if (section_is_p (sectp->name, &names->info_dwo))
13000 {
049412e3 13001 dwo_sections->info.s.section = sectp;
fd361982 13002 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
13003 }
13004 else if (section_is_p (sectp->name, &names->line_dwo))
13005 {
049412e3 13006 dwo_sections->line.s.section = sectp;
fd361982 13007 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
13008 }
13009 else if (section_is_p (sectp->name, &names->loc_dwo))
13010 {
049412e3 13011 dwo_sections->loc.s.section = sectp;
fd361982 13012 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
13013 }
13014 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13015 {
049412e3 13016 dwo_sections->macinfo.s.section = sectp;
fd361982 13017 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
13018 }
13019 else if (section_is_p (sectp->name, &names->macro_dwo))
13020 {
049412e3 13021 dwo_sections->macro.s.section = sectp;
fd361982 13022 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
13023 }
13024 else if (section_is_p (sectp->name, &names->str_dwo))
13025 {
049412e3 13026 dwo_sections->str.s.section = sectp;
fd361982 13027 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
13028 }
13029 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13030 {
049412e3 13031 dwo_sections->str_offsets.s.section = sectp;
fd361982 13032 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
13033 }
13034 else if (section_is_p (sectp->name, &names->types_dwo))
13035 {
13036 struct dwarf2_section_info type_section;
13037
13038 memset (&type_section, 0, sizeof (type_section));
049412e3 13039 type_section.s.section = sectp;
fd361982 13040 type_section.size = bfd_section_size (sectp);
fd5866f6 13041 dwo_sections->types.push_back (type_section);
80626a55
DE
13042 }
13043}
13044
ab5088bf 13045/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13046 by PER_CU. This is for the non-DWP case.
80626a55 13047 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13048
13049static struct dwo_file *
0ac5b59e
DE
13050open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13051 const char *dwo_name, const char *comp_dir)
3019eac3 13052{
ed2dc618 13053 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13054
fb1eb2f9 13055 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13056 if (dbfd == NULL)
13057 {
b4f54984 13058 if (dwarf_read_debug)
80626a55
DE
13059 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13060 return NULL;
13061 }
263db9a1 13062
51ac9db5 13063 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13064 dwo_file->dwo_name = dwo_name;
13065 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13066 dwo_file->dbfd = std::move (dbfd);
3019eac3 13067
fb1eb2f9 13068 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13069 &dwo_file->sections);
3019eac3 13070
ed2dc618
SM
13071 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13072 dwo_file->cus);
3019eac3 13073
263db9a1 13074 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13075 dwo_file->sections.types, dwo_file->tus);
3019eac3 13076
b4f54984 13077 if (dwarf_read_debug)
80626a55
DE
13078 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13079
263db9a1 13080 return dwo_file.release ();
3019eac3
DE
13081}
13082
80626a55 13083/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13084 size of each of the DWP debugging sections common to version 1 and 2 that
13085 we are interested in. */
3019eac3 13086
80626a55 13087static void
73869dc2
DE
13088dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13089 void *dwp_file_ptr)
3019eac3 13090{
9a3c8263 13091 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13092 const struct dwop_section_names *names = &dwop_section_names;
13093 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13094
80626a55 13095 /* Record the ELF section number for later lookup: this is what the
73869dc2 13096 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13097 gdb_assert (elf_section_nr < dwp_file->num_sections);
13098 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13099
80626a55
DE
13100 /* Look for specific sections that we need. */
13101 if (section_is_p (sectp->name, &names->str_dwo))
13102 {
049412e3 13103 dwp_file->sections.str.s.section = sectp;
fd361982 13104 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13105 }
13106 else if (section_is_p (sectp->name, &names->cu_index))
13107 {
049412e3 13108 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13109 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13110 }
13111 else if (section_is_p (sectp->name, &names->tu_index))
13112 {
049412e3 13113 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13114 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13115 }
13116}
3019eac3 13117
73869dc2
DE
13118/* This function is mapped across the sections and remembers the offset and
13119 size of each of the DWP version 2 debugging sections that we are interested
13120 in. This is split into a separate function because we don't know if we
13121 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13122
13123static void
13124dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13125{
9a3c8263 13126 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13127 const struct dwop_section_names *names = &dwop_section_names;
13128 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13129
13130 /* Record the ELF section number for later lookup: this is what the
13131 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13132 gdb_assert (elf_section_nr < dwp_file->num_sections);
13133 dwp_file->elf_sections[elf_section_nr] = sectp;
13134
13135 /* Look for specific sections that we need. */
13136 if (section_is_p (sectp->name, &names->abbrev_dwo))
13137 {
049412e3 13138 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13139 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13140 }
13141 else if (section_is_p (sectp->name, &names->info_dwo))
13142 {
049412e3 13143 dwp_file->sections.info.s.section = sectp;
fd361982 13144 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13145 }
13146 else if (section_is_p (sectp->name, &names->line_dwo))
13147 {
049412e3 13148 dwp_file->sections.line.s.section = sectp;
fd361982 13149 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13150 }
13151 else if (section_is_p (sectp->name, &names->loc_dwo))
13152 {
049412e3 13153 dwp_file->sections.loc.s.section = sectp;
fd361982 13154 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13155 }
13156 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13157 {
049412e3 13158 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13159 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13160 }
13161 else if (section_is_p (sectp->name, &names->macro_dwo))
13162 {
049412e3 13163 dwp_file->sections.macro.s.section = sectp;
fd361982 13164 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13165 }
13166 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13167 {
049412e3 13168 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13169 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13170 }
13171 else if (section_is_p (sectp->name, &names->types_dwo))
13172 {
049412e3 13173 dwp_file->sections.types.s.section = sectp;
fd361982 13174 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13175 }
13176}
13177
80626a55 13178/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13179
80626a55
DE
13180static hashval_t
13181hash_dwp_loaded_cutus (const void *item)
13182{
9a3c8263 13183 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13184
80626a55
DE
13185 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13186 return dwo_unit->signature;
3019eac3
DE
13187}
13188
80626a55 13189/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13190
80626a55
DE
13191static int
13192eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13193{
9a3c8263
SM
13194 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13195 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13196
80626a55
DE
13197 return dua->signature == dub->signature;
13198}
3019eac3 13199
80626a55 13200/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13201
80626a55
DE
13202static htab_t
13203allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13204{
13205 return htab_create_alloc_ex (3,
13206 hash_dwp_loaded_cutus,
13207 eq_dwp_loaded_cutus,
13208 NULL,
13209 &objfile->objfile_obstack,
13210 hashtab_obstack_allocate,
13211 dummy_obstack_deallocate);
13212}
3019eac3 13213
ab5088bf
DE
13214/* Try to open DWP file FILE_NAME.
13215 The result is the bfd handle of the file.
13216 If there is a problem finding or opening the file, return NULL.
13217 Upon success, the canonicalized path of the file is stored in the bfd,
13218 same as symfile_bfd_open. */
13219
192b62ce 13220static gdb_bfd_ref_ptr
ed2dc618
SM
13221open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13222 const char *file_name)
ab5088bf 13223{
ed2dc618
SM
13224 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13225 1 /*is_dwp*/,
192b62ce 13226 1 /*search_cwd*/));
6ac97d4c
DE
13227 if (abfd != NULL)
13228 return abfd;
13229
13230 /* Work around upstream bug 15652.
13231 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13232 [Whether that's a "bug" is debatable, but it is getting in our way.]
13233 We have no real idea where the dwp file is, because gdb's realpath-ing
13234 of the executable's path may have discarded the needed info.
13235 [IWBN if the dwp file name was recorded in the executable, akin to
13236 .gnu_debuglink, but that doesn't exist yet.]
13237 Strip the directory from FILE_NAME and search again. */
13238 if (*debug_file_directory != '\0')
13239 {
13240 /* Don't implicitly search the current directory here.
13241 If the user wants to search "." to handle this case,
13242 it must be added to debug-file-directory. */
ed2dc618
SM
13243 return try_open_dwop_file (dwarf2_per_objfile,
13244 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13245 0 /*search_cwd*/);
13246 }
13247
13248 return NULL;
ab5088bf
DE
13249}
13250
80626a55
DE
13251/* Initialize the use of the DWP file for the current objfile.
13252 By convention the name of the DWP file is ${objfile}.dwp.
13253 The result is NULL if it can't be found. */
a766d390 13254
400174b1 13255static std::unique_ptr<struct dwp_file>
ed2dc618 13256open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13257{
13258 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13259
82bf32bc
JK
13260 /* Try to find first .dwp for the binary file before any symbolic links
13261 resolving. */
6c447423
DE
13262
13263 /* If the objfile is a debug file, find the name of the real binary
13264 file and get the name of dwp file from there. */
d721ba37 13265 std::string dwp_name;
6c447423
DE
13266 if (objfile->separate_debug_objfile_backlink != NULL)
13267 {
13268 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13269 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13270
d721ba37 13271 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13272 }
13273 else
d721ba37
PA
13274 dwp_name = objfile->original_name;
13275
13276 dwp_name += ".dwp";
80626a55 13277
ed2dc618 13278 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13279 if (dbfd == NULL
13280 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13281 {
13282 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13283 dwp_name = objfile_name (objfile);
13284 dwp_name += ".dwp";
ed2dc618 13285 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13286 }
13287
80626a55
DE
13288 if (dbfd == NULL)
13289 {
b4f54984 13290 if (dwarf_read_debug)
d721ba37 13291 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13292 return std::unique_ptr<dwp_file> ();
3019eac3 13293 }
400174b1
TT
13294
13295 const char *name = bfd_get_filename (dbfd.get ());
13296 std::unique_ptr<struct dwp_file> dwp_file
13297 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13298
0a0f4c01 13299 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13300 dwp_file->elf_sections =
13301 OBSTACK_CALLOC (&objfile->objfile_obstack,
13302 dwp_file->num_sections, asection *);
13303
400174b1
TT
13304 bfd_map_over_sections (dwp_file->dbfd.get (),
13305 dwarf2_locate_common_dwp_sections,
13306 dwp_file.get ());
80626a55 13307
400174b1
TT
13308 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13309 0);
80626a55 13310
400174b1
TT
13311 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13312 1);
80626a55 13313
73869dc2 13314 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13315 if (dwp_file->cus && dwp_file->tus
13316 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13317 {
13318 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13319 pretty bizarre. We use pulongest here because that's the established
4d65956b 13320 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13321 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13322 " TU version %s [in DWP file %s]"),
13323 pulongest (dwp_file->cus->version),
d721ba37 13324 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13325 }
08302ed2
DE
13326
13327 if (dwp_file->cus)
13328 dwp_file->version = dwp_file->cus->version;
13329 else if (dwp_file->tus)
13330 dwp_file->version = dwp_file->tus->version;
13331 else
13332 dwp_file->version = 2;
73869dc2
DE
13333
13334 if (dwp_file->version == 2)
400174b1
TT
13335 bfd_map_over_sections (dwp_file->dbfd.get (),
13336 dwarf2_locate_v2_dwp_sections,
13337 dwp_file.get ());
73869dc2 13338
19ac8c2e
DE
13339 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13340 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13341
b4f54984 13342 if (dwarf_read_debug)
80626a55
DE
13343 {
13344 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13345 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13346 " %s CUs, %s TUs\n",
13347 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13348 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13349 }
13350
13351 return dwp_file;
3019eac3 13352}
c906108c 13353
ab5088bf
DE
13354/* Wrapper around open_and_init_dwp_file, only open it once. */
13355
13356static struct dwp_file *
ed2dc618 13357get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13358{
13359 if (! dwarf2_per_objfile->dwp_checked)
13360 {
ed2dc618
SM
13361 dwarf2_per_objfile->dwp_file
13362 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13363 dwarf2_per_objfile->dwp_checked = 1;
13364 }
400174b1 13365 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13366}
13367
80626a55
DE
13368/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13369 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13370 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13371 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13372 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13373
13374 This is called, for example, when wanting to read a variable with a
13375 complex location. Therefore we don't want to do file i/o for every call.
13376 Therefore we don't want to look for a DWO file on every call.
13377 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13378 then we check if we've already seen DWO_NAME, and only THEN do we check
13379 for a DWO file.
13380
1c658ad5 13381 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13382 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13383
3019eac3 13384static struct dwo_unit *
80626a55
DE
13385lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13386 const char *dwo_name, const char *comp_dir,
13387 ULONGEST signature, int is_debug_types)
3019eac3 13388{
ed2dc618 13389 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13390 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13391 const char *kind = is_debug_types ? "TU" : "CU";
13392 void **dwo_file_slot;
3019eac3 13393 struct dwo_file *dwo_file;
80626a55 13394 struct dwp_file *dwp_file;
cb1df416 13395
6a506a2d
DE
13396 /* First see if there's a DWP file.
13397 If we have a DWP file but didn't find the DWO inside it, don't
13398 look for the original DWO file. It makes gdb behave differently
13399 depending on whether one is debugging in the build tree. */
cf2c3c16 13400
ed2dc618 13401 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13402 if (dwp_file != NULL)
cf2c3c16 13403 {
80626a55
DE
13404 const struct dwp_hash_table *dwp_htab =
13405 is_debug_types ? dwp_file->tus : dwp_file->cus;
13406
13407 if (dwp_htab != NULL)
13408 {
13409 struct dwo_unit *dwo_cutu =
ed2dc618 13410 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13411 signature, is_debug_types);
80626a55
DE
13412
13413 if (dwo_cutu != NULL)
13414 {
b4f54984 13415 if (dwarf_read_debug)
80626a55
DE
13416 {
13417 fprintf_unfiltered (gdb_stdlog,
13418 "Virtual DWO %s %s found: @%s\n",
13419 kind, hex_string (signature),
13420 host_address_to_string (dwo_cutu));
13421 }
13422 return dwo_cutu;
13423 }
13424 }
13425 }
6a506a2d 13426 else
80626a55 13427 {
6a506a2d 13428 /* No DWP file, look for the DWO file. */
80626a55 13429
ed2dc618
SM
13430 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13431 dwo_name, comp_dir);
6a506a2d 13432 if (*dwo_file_slot == NULL)
80626a55 13433 {
6a506a2d
DE
13434 /* Read in the file and build a table of the CUs/TUs it contains. */
13435 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13436 }
6a506a2d 13437 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13438 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13439
6a506a2d 13440 if (dwo_file != NULL)
19c3d4c9 13441 {
6a506a2d
DE
13442 struct dwo_unit *dwo_cutu = NULL;
13443
13444 if (is_debug_types && dwo_file->tus)
13445 {
13446 struct dwo_unit find_dwo_cutu;
13447
13448 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13449 find_dwo_cutu.signature = signature;
9a3c8263
SM
13450 dwo_cutu
13451 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13452 }
33c5cd75 13453 else if (!is_debug_types && dwo_file->cus)
80626a55 13454 {
33c5cd75
DB
13455 struct dwo_unit find_dwo_cutu;
13456
13457 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13458 find_dwo_cutu.signature = signature;
13459 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13460 &find_dwo_cutu);
6a506a2d
DE
13461 }
13462
13463 if (dwo_cutu != NULL)
13464 {
b4f54984 13465 if (dwarf_read_debug)
6a506a2d
DE
13466 {
13467 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13468 kind, dwo_name, hex_string (signature),
13469 host_address_to_string (dwo_cutu));
13470 }
13471 return dwo_cutu;
80626a55
DE
13472 }
13473 }
2e276125 13474 }
9cdd5dbd 13475
80626a55
DE
13476 /* We didn't find it. This could mean a dwo_id mismatch, or
13477 someone deleted the DWO/DWP file, or the search path isn't set up
13478 correctly to find the file. */
13479
b4f54984 13480 if (dwarf_read_debug)
80626a55
DE
13481 {
13482 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13483 kind, dwo_name, hex_string (signature));
13484 }
3019eac3 13485
6656a72d
DE
13486 /* This is a warning and not a complaint because it can be caused by
13487 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13488 {
13489 /* Print the name of the DWP file if we looked there, helps the user
13490 better diagnose the problem. */
791afaa2 13491 std::string dwp_text;
43942612
DE
13492
13493 if (dwp_file != NULL)
791afaa2
TT
13494 dwp_text = string_printf (" [in DWP file %s]",
13495 lbasename (dwp_file->name));
43942612 13496
9d8780f0 13497 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13498 " [in module %s]"),
13499 kind, dwo_name, hex_string (signature),
791afaa2 13500 dwp_text.c_str (),
43942612 13501 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13502 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13503 }
3019eac3 13504 return NULL;
5fb290d7
DJ
13505}
13506
80626a55
DE
13507/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13508 See lookup_dwo_cutu_unit for details. */
13509
13510static struct dwo_unit *
13511lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13512 const char *dwo_name, const char *comp_dir,
13513 ULONGEST signature)
13514{
13515 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13516}
13517
13518/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13519 See lookup_dwo_cutu_unit for details. */
13520
13521static struct dwo_unit *
13522lookup_dwo_type_unit (struct signatured_type *this_tu,
13523 const char *dwo_name, const char *comp_dir)
13524{
13525 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13526}
13527
89e63ee4
DE
13528/* Traversal function for queue_and_load_all_dwo_tus. */
13529
13530static int
13531queue_and_load_dwo_tu (void **slot, void *info)
13532{
13533 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13534 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13535 ULONGEST signature = dwo_unit->signature;
13536 struct signatured_type *sig_type =
13537 lookup_dwo_signatured_type (per_cu->cu, signature);
13538
13539 if (sig_type != NULL)
13540 {
13541 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13542
13543 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13544 a real dependency of PER_CU on SIG_TYPE. That is detected later
13545 while processing PER_CU. */
13546 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13547 load_full_type_unit (sig_cu);
ae640021 13548 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13549 }
13550
13551 return 1;
13552}
13553
13554/* Queue all TUs contained in the DWO of PER_CU to be read in.
13555 The DWO may have the only definition of the type, though it may not be
13556 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13557 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13558
13559static void
13560queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13561{
13562 struct dwo_unit *dwo_unit;
13563 struct dwo_file *dwo_file;
13564
13565 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13566 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13567 gdb_assert (per_cu->cu != NULL);
13568
13569 dwo_unit = per_cu->cu->dwo_unit;
13570 gdb_assert (dwo_unit != NULL);
13571
13572 dwo_file = dwo_unit->dwo_file;
13573 if (dwo_file->tus != NULL)
13574 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13575}
13576
3019eac3 13577/* Read in various DIEs. */
348e048f 13578
d389af10 13579/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13580 Inherit only the children of the DW_AT_abstract_origin DIE not being
13581 already referenced by DW_AT_abstract_origin from the children of the
13582 current DIE. */
d389af10
JK
13583
13584static void
13585inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13586{
13587 struct die_info *child_die;
791afaa2 13588 sect_offset *offsetp;
d389af10
JK
13589 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13590 struct die_info *origin_die;
13591 /* Iterator of the ORIGIN_DIE children. */
13592 struct die_info *origin_child_die;
d389af10 13593 struct attribute *attr;
cd02d79d
PA
13594 struct dwarf2_cu *origin_cu;
13595 struct pending **origin_previous_list_in_scope;
d389af10
JK
13596
13597 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13598 if (!attr)
13599 return;
13600
cd02d79d
PA
13601 /* Note that following die references may follow to a die in a
13602 different cu. */
13603
13604 origin_cu = cu;
13605 origin_die = follow_die_ref (die, attr, &origin_cu);
13606
13607 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13608 symbols in. */
13609 origin_previous_list_in_scope = origin_cu->list_in_scope;
13610 origin_cu->list_in_scope = cu->list_in_scope;
13611
edb3359d
DJ
13612 if (die->tag != origin_die->tag
13613 && !(die->tag == DW_TAG_inlined_subroutine
13614 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13615 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13616 sect_offset_str (die->sect_off),
13617 sect_offset_str (origin_die->sect_off));
d389af10 13618
791afaa2 13619 std::vector<sect_offset> offsets;
d389af10 13620
3ea89b92
PMR
13621 for (child_die = die->child;
13622 child_die && child_die->tag;
13623 child_die = sibling_die (child_die))
13624 {
13625 struct die_info *child_origin_die;
13626 struct dwarf2_cu *child_origin_cu;
13627
13628 /* We are trying to process concrete instance entries:
216f72a1 13629 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13630 it's not relevant to our analysis here. i.e. detecting DIEs that are
13631 present in the abstract instance but not referenced in the concrete
13632 one. */
216f72a1
JK
13633 if (child_die->tag == DW_TAG_call_site
13634 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13635 continue;
13636
c38f313d
DJ
13637 /* For each CHILD_DIE, find the corresponding child of
13638 ORIGIN_DIE. If there is more than one layer of
13639 DW_AT_abstract_origin, follow them all; there shouldn't be,
13640 but GCC versions at least through 4.4 generate this (GCC PR
13641 40573). */
3ea89b92
PMR
13642 child_origin_die = child_die;
13643 child_origin_cu = cu;
c38f313d
DJ
13644 while (1)
13645 {
cd02d79d
PA
13646 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13647 child_origin_cu);
c38f313d
DJ
13648 if (attr == NULL)
13649 break;
cd02d79d
PA
13650 child_origin_die = follow_die_ref (child_origin_die, attr,
13651 &child_origin_cu);
c38f313d
DJ
13652 }
13653
d389af10
JK
13654 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13655 counterpart may exist. */
c38f313d 13656 if (child_origin_die != child_die)
d389af10 13657 {
edb3359d
DJ
13658 if (child_die->tag != child_origin_die->tag
13659 && !(child_die->tag == DW_TAG_inlined_subroutine
13660 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13661 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13662 "different tags"),
9d8780f0
SM
13663 sect_offset_str (child_die->sect_off),
13664 sect_offset_str (child_origin_die->sect_off));
c38f313d 13665 if (child_origin_die->parent != origin_die)
b98664d3 13666 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13667 "different parents"),
9d8780f0
SM
13668 sect_offset_str (child_die->sect_off),
13669 sect_offset_str (child_origin_die->sect_off));
c38f313d 13670 else
791afaa2 13671 offsets.push_back (child_origin_die->sect_off);
d389af10 13672 }
d389af10 13673 }
791afaa2
TT
13674 std::sort (offsets.begin (), offsets.end ());
13675 sect_offset *offsets_end = offsets.data () + offsets.size ();
13676 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13677 if (offsetp[-1] == *offsetp)
b98664d3 13678 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13679 "to DIE %s as their abstract origin"),
13680 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13681
791afaa2 13682 offsetp = offsets.data ();
d389af10
JK
13683 origin_child_die = origin_die->child;
13684 while (origin_child_die && origin_child_die->tag)
13685 {
13686 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13687 while (offsetp < offsets_end
9c541725 13688 && *offsetp < origin_child_die->sect_off)
d389af10 13689 offsetp++;
b64f50a1 13690 if (offsetp >= offsets_end
9c541725 13691 || *offsetp > origin_child_die->sect_off)
d389af10 13692 {
adde2bff
DE
13693 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13694 Check whether we're already processing ORIGIN_CHILD_DIE.
13695 This can happen with mutually referenced abstract_origins.
13696 PR 16581. */
13697 if (!origin_child_die->in_process)
13698 process_die (origin_child_die, origin_cu);
d389af10
JK
13699 }
13700 origin_child_die = sibling_die (origin_child_die);
13701 }
cd02d79d 13702 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13703
13704 if (cu != origin_cu)
13705 compute_delayed_physnames (origin_cu);
d389af10
JK
13706}
13707
c906108c 13708static void
e7c27a73 13709read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13710{
518817b3 13711 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13712 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13713 struct context_stack *newobj;
c906108c
SS
13714 CORE_ADDR lowpc;
13715 CORE_ADDR highpc;
13716 struct die_info *child_die;
edb3359d 13717 struct attribute *attr, *call_line, *call_file;
15d034d0 13718 const char *name;
e142c38c 13719 CORE_ADDR baseaddr;
801e3a5b 13720 struct block *block;
edb3359d 13721 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13722 std::vector<struct symbol *> template_args;
34eaf542 13723 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13724
13725 if (inlined_func)
13726 {
13727 /* If we do not have call site information, we can't show the
13728 caller of this inlined function. That's too confusing, so
13729 only use the scope for local variables. */
13730 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13731 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13732 if (call_line == NULL || call_file == NULL)
13733 {
13734 read_lexical_block_scope (die, cu);
13735 return;
13736 }
13737 }
c906108c 13738
e142c38c
DJ
13739 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13740
94af9270 13741 name = dwarf2_name (die, cu);
c906108c 13742
e8d05480
JB
13743 /* Ignore functions with missing or empty names. These are actually
13744 illegal according to the DWARF standard. */
13745 if (name == NULL)
13746 {
b98664d3 13747 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13748 sect_offset_str (die->sect_off));
e8d05480
JB
13749 return;
13750 }
13751
13752 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13753 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13754 <= PC_BOUNDS_INVALID)
e8d05480 13755 {
ae4d0c03
PM
13756 attr = dwarf2_attr (die, DW_AT_external, cu);
13757 if (!attr || !DW_UNSND (attr))
b98664d3 13758 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13759 "for subprogram DIE at %s"),
13760 sect_offset_str (die->sect_off));
e8d05480
JB
13761 return;
13762 }
c906108c 13763
3e29f34a
MR
13764 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13765 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13766
34eaf542
TT
13767 /* If we have any template arguments, then we must allocate a
13768 different sort of symbol. */
13769 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13770 {
13771 if (child_die->tag == DW_TAG_template_type_param
13772 || child_die->tag == DW_TAG_template_value_param)
13773 {
e623cf5d 13774 templ_func = allocate_template_symbol (objfile);
cf724bc9 13775 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13776 break;
13777 }
13778 }
13779
c24bdb02 13780 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13781 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13782 (struct symbol *) templ_func);
4c2df51b 13783
81873cc8 13784 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13785 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13786 cu->language);
13787
4cecd739
DJ
13788 /* If there is a location expression for DW_AT_frame_base, record
13789 it. */
e142c38c 13790 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13791 if (attr != nullptr)
fe978cb0 13792 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13793
63e43d3a
PMR
13794 /* If there is a location for the static link, record it. */
13795 newobj->static_link = NULL;
13796 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13797 if (attr != nullptr)
63e43d3a 13798 {
224c3ddb
SM
13799 newobj->static_link
13800 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13801 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13802 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13803 }
13804
c24bdb02 13805 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13806
639d11d3 13807 if (die->child != NULL)
c906108c 13808 {
639d11d3 13809 child_die = die->child;
c906108c
SS
13810 while (child_die && child_die->tag)
13811 {
34eaf542
TT
13812 if (child_die->tag == DW_TAG_template_type_param
13813 || child_die->tag == DW_TAG_template_value_param)
13814 {
13815 struct symbol *arg = new_symbol (child_die, NULL, cu);
13816
f1078f66 13817 if (arg != NULL)
2f4732b0 13818 template_args.push_back (arg);
34eaf542
TT
13819 }
13820 else
13821 process_die (child_die, cu);
c906108c
SS
13822 child_die = sibling_die (child_die);
13823 }
13824 }
13825
d389af10
JK
13826 inherit_abstract_dies (die, cu);
13827
4a811a97
UW
13828 /* If we have a DW_AT_specification, we might need to import using
13829 directives from the context of the specification DIE. See the
13830 comment in determine_prefix. */
13831 if (cu->language == language_cplus
13832 && dwarf2_attr (die, DW_AT_specification, cu))
13833 {
13834 struct dwarf2_cu *spec_cu = cu;
13835 struct die_info *spec_die = die_specification (die, &spec_cu);
13836
13837 while (spec_die)
13838 {
13839 child_die = spec_die->child;
13840 while (child_die && child_die->tag)
13841 {
13842 if (child_die->tag == DW_TAG_imported_module)
13843 process_die (child_die, spec_cu);
13844 child_die = sibling_die (child_die);
13845 }
13846
13847 /* In some cases, GCC generates specification DIEs that
13848 themselves contain DW_AT_specification attributes. */
13849 spec_die = die_specification (spec_die, &spec_cu);
13850 }
13851 }
13852
c24bdb02 13853 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13854 /* Make a block for the local symbols within. */
c24bdb02 13855 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13856 cstk.static_link, lowpc, highpc);
801e3a5b 13857
df8a16a1 13858 /* For C++, set the block's scope. */
45280282
IB
13859 if ((cu->language == language_cplus
13860 || cu->language == language_fortran
c44af4eb
TT
13861 || cu->language == language_d
13862 || cu->language == language_rust)
4d4ec4e5 13863 && cu->processing_has_namespace_info)
195a3f6c
TT
13864 block_set_scope (block, determine_prefix (die, cu),
13865 &objfile->objfile_obstack);
df8a16a1 13866
801e3a5b
JB
13867 /* If we have address ranges, record them. */
13868 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13869
a60f3166 13870 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13871
34eaf542 13872 /* Attach template arguments to function. */
2f4732b0 13873 if (!template_args.empty ())
34eaf542
TT
13874 {
13875 gdb_assert (templ_func != NULL);
13876
2f4732b0 13877 templ_func->n_template_arguments = template_args.size ();
34eaf542 13878 templ_func->template_arguments
8d749320
SM
13879 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13880 templ_func->n_template_arguments);
34eaf542 13881 memcpy (templ_func->template_arguments,
2f4732b0 13882 template_args.data (),
34eaf542 13883 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13884
13885 /* Make sure that the symtab is set on the new symbols. Even
13886 though they don't appear in this symtab directly, other parts
13887 of gdb assume that symbols do, and this is reasonably
13888 true. */
8634679f 13889 for (symbol *sym : template_args)
3e1d3d8c 13890 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13891 }
13892
208d8187
JB
13893 /* In C++, we can have functions nested inside functions (e.g., when
13894 a function declares a class that has methods). This means that
13895 when we finish processing a function scope, we may need to go
13896 back to building a containing block's symbol lists. */
c24bdb02
KS
13897 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13898 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13899
921e78cf
JB
13900 /* If we've finished processing a top-level function, subsequent
13901 symbols go in the file symbol list. */
c24bdb02
KS
13902 if (cu->get_builder ()->outermost_context_p ())
13903 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13904}
13905
13906/* Process all the DIES contained within a lexical block scope. Start
13907 a new scope, process the dies, and then close the scope. */
13908
13909static void
e7c27a73 13910read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13911{
518817b3 13912 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13913 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13914 CORE_ADDR lowpc, highpc;
13915 struct die_info *child_die;
e142c38c
DJ
13916 CORE_ADDR baseaddr;
13917
13918 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13919
13920 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13921 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13922 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13923 be nasty. Might be easier to properly extend generic blocks to
af34e669 13924 describe ranges. */
e385593e
JK
13925 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13926 {
13927 case PC_BOUNDS_NOT_PRESENT:
13928 /* DW_TAG_lexical_block has no attributes, process its children as if
13929 there was no wrapping by that DW_TAG_lexical_block.
13930 GCC does no longer produces such DWARF since GCC r224161. */
13931 for (child_die = die->child;
13932 child_die != NULL && child_die->tag;
13933 child_die = sibling_die (child_die))
13934 process_die (child_die, cu);
13935 return;
13936 case PC_BOUNDS_INVALID:
13937 return;
13938 }
3e29f34a
MR
13939 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13940 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13941
c24bdb02 13942 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13943 if (die->child != NULL)
c906108c 13944 {
639d11d3 13945 child_die = die->child;
c906108c
SS
13946 while (child_die && child_die->tag)
13947 {
e7c27a73 13948 process_die (child_die, cu);
c906108c
SS
13949 child_die = sibling_die (child_die);
13950 }
13951 }
3ea89b92 13952 inherit_abstract_dies (die, cu);
c24bdb02 13953 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13954
c24bdb02
KS
13955 if (*cu->get_builder ()->get_local_symbols () != NULL
13956 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13957 {
801e3a5b 13958 struct block *block
c24bdb02 13959 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13960 cstk.start_addr, highpc);
801e3a5b
JB
13961
13962 /* Note that recording ranges after traversing children, as we
13963 do here, means that recording a parent's ranges entails
13964 walking across all its children's ranges as they appear in
13965 the address map, which is quadratic behavior.
13966
13967 It would be nicer to record the parent's ranges before
13968 traversing its children, simply overriding whatever you find
13969 there. But since we don't even decide whether to create a
13970 block until after we've traversed its children, that's hard
13971 to do. */
13972 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13973 }
c24bdb02
KS
13974 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13975 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13976}
13977
216f72a1 13978/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13979
13980static void
13981read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13982{
518817b3 13983 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13984 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13985 CORE_ADDR pc, baseaddr;
13986 struct attribute *attr;
13987 struct call_site *call_site, call_site_local;
13988 void **slot;
13989 int nparams;
13990 struct die_info *child_die;
13991
13992 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13993
216f72a1
JK
13994 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13995 if (attr == NULL)
13996 {
13997 /* This was a pre-DWARF-5 GNU extension alias
13998 for DW_AT_call_return_pc. */
13999 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14000 }
96408a79
SA
14001 if (!attr)
14002 {
b98664d3 14003 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
14004 "DIE %s [in module %s]"),
14005 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14006 return;
14007 }
31aa7e4e 14008 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 14009 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
14010
14011 if (cu->call_site_htab == NULL)
14012 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14013 NULL, &objfile->objfile_obstack,
14014 hashtab_obstack_allocate, NULL);
14015 call_site_local.pc = pc;
14016 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14017 if (*slot != NULL)
14018 {
b98664d3 14019 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
14020 "DIE %s [in module %s]"),
14021 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 14022 objfile_name (objfile));
96408a79
SA
14023 return;
14024 }
14025
14026 /* Count parameters at the caller. */
14027
14028 nparams = 0;
14029 for (child_die = die->child; child_die && child_die->tag;
14030 child_die = sibling_die (child_die))
14031 {
216f72a1
JK
14032 if (child_die->tag != DW_TAG_call_site_parameter
14033 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 14034 {
b98664d3 14035 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14036 "DW_TAG_call_site child DIE %s [in module %s]"),
14037 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14038 objfile_name (objfile));
96408a79
SA
14039 continue;
14040 }
14041
14042 nparams++;
14043 }
14044
224c3ddb
SM
14045 call_site
14046 = ((struct call_site *)
14047 obstack_alloc (&objfile->objfile_obstack,
14048 sizeof (*call_site)
14049 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14050 *slot = call_site;
14051 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14052 call_site->pc = pc;
14053
216f72a1
JK
14054 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14055 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14056 {
14057 struct die_info *func_die;
14058
14059 /* Skip also over DW_TAG_inlined_subroutine. */
14060 for (func_die = die->parent;
14061 func_die && func_die->tag != DW_TAG_subprogram
14062 && func_die->tag != DW_TAG_subroutine_type;
14063 func_die = func_die->parent);
14064
216f72a1
JK
14065 /* DW_AT_call_all_calls is a superset
14066 of DW_AT_call_all_tail_calls. */
96408a79 14067 if (func_die
216f72a1 14068 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14069 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14070 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14071 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14072 {
14073 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14074 not complete. But keep CALL_SITE for look ups via call_site_htab,
14075 both the initial caller containing the real return address PC and
14076 the final callee containing the current PC of a chain of tail
14077 calls do not need to have the tail call list complete. But any
14078 function candidate for a virtual tail call frame searched via
14079 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14080 determined unambiguously. */
14081 }
14082 else
14083 {
14084 struct type *func_type = NULL;
14085
14086 if (func_die)
14087 func_type = get_die_type (func_die, cu);
14088 if (func_type != NULL)
14089 {
14090 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14091
14092 /* Enlist this call site to the function. */
14093 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14094 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14095 }
14096 else
b98664d3 14097 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14098 "DIE %s [in module %s]"),
14099 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14100 }
14101 }
14102
216f72a1
JK
14103 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14104 if (attr == NULL)
14105 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14106 if (attr == NULL)
14107 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14108 if (attr == NULL)
216f72a1
JK
14109 {
14110 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14111 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14112 }
96408a79
SA
14113 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14114 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14115 /* Keep NULL DWARF_BLOCK. */;
14116 else if (attr_form_is_block (attr))
14117 {
14118 struct dwarf2_locexpr_baton *dlbaton;
14119
8d749320 14120 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14121 dlbaton->data = DW_BLOCK (attr)->data;
14122 dlbaton->size = DW_BLOCK (attr)->size;
14123 dlbaton->per_cu = cu->per_cu;
14124
14125 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14126 }
7771576e 14127 else if (attr_form_is_ref (attr))
96408a79 14128 {
96408a79
SA
14129 struct dwarf2_cu *target_cu = cu;
14130 struct die_info *target_die;
14131
ac9ec31b 14132 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14133 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14134 if (die_is_declaration (target_die, target_cu))
14135 {
7d45c7c3 14136 const char *target_physname;
9112db09
JK
14137
14138 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14139 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14140 if (target_physname == NULL)
9112db09 14141 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14142 if (target_physname == NULL)
b98664d3 14143 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14144 "physname, for referencing DIE %s [in module %s]"),
14145 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14146 else
7d455152 14147 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14148 }
14149 else
14150 {
14151 CORE_ADDR lowpc;
14152
14153 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14154 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14155 <= PC_BOUNDS_INVALID)
b98664d3 14156 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14157 "low pc, for referencing DIE %s [in module %s]"),
14158 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14159 else
3e29f34a
MR
14160 {
14161 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14162 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14163 }
96408a79
SA
14164 }
14165 }
14166 else
b98664d3 14167 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14168 "block nor reference, for DIE %s [in module %s]"),
14169 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14170
14171 call_site->per_cu = cu->per_cu;
14172
14173 for (child_die = die->child;
14174 child_die && child_die->tag;
14175 child_die = sibling_die (child_die))
14176 {
96408a79 14177 struct call_site_parameter *parameter;
1788b2d3 14178 struct attribute *loc, *origin;
96408a79 14179
216f72a1
JK
14180 if (child_die->tag != DW_TAG_call_site_parameter
14181 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14182 {
14183 /* Already printed the complaint above. */
14184 continue;
14185 }
14186
14187 gdb_assert (call_site->parameter_count < nparams);
14188 parameter = &call_site->parameter[call_site->parameter_count];
14189
1788b2d3
JK
14190 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14191 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14192 register is contained in DW_AT_call_value. */
96408a79 14193
24c5c679 14194 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14195 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14196 if (origin == NULL)
14197 {
14198 /* This was a pre-DWARF-5 GNU extension alias
14199 for DW_AT_call_parameter. */
14200 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14201 }
7771576e 14202 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14203 {
1788b2d3 14204 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14205
14206 sect_offset sect_off
14207 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14208 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14209 {
14210 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14211 binding can be done only inside one CU. Such referenced DIE
14212 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14213 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14214 "DW_TAG_call_site child DIE %s [in module %s]"),
14215 sect_offset_str (child_die->sect_off),
9c541725 14216 objfile_name (objfile));
d76b7dbc
JK
14217 continue;
14218 }
9c541725
PA
14219 parameter->u.param_cu_off
14220 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14221 }
14222 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14223 {
b98664d3 14224 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14225 "DW_TAG_call_site child DIE %s [in module %s]"),
14226 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14227 continue;
14228 }
24c5c679 14229 else
96408a79 14230 {
24c5c679
JK
14231 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14232 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14233 if (parameter->u.dwarf_reg != -1)
14234 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14235 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14236 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14237 &parameter->u.fb_offset))
14238 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14239 else
14240 {
b98664d3 14241 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14242 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14243 "DW_TAG_call_site child DIE %s "
24c5c679 14244 "[in module %s]"),
9d8780f0 14245 sect_offset_str (child_die->sect_off),
9c541725 14246 objfile_name (objfile));
24c5c679
JK
14247 continue;
14248 }
96408a79
SA
14249 }
14250
216f72a1
JK
14251 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14252 if (attr == NULL)
14253 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14254 if (!attr_form_is_block (attr))
14255 {
b98664d3 14256 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14257 "DW_TAG_call_site child DIE %s [in module %s]"),
14258 sect_offset_str (child_die->sect_off),
9c541725 14259 objfile_name (objfile));
96408a79
SA
14260 continue;
14261 }
14262 parameter->value = DW_BLOCK (attr)->data;
14263 parameter->value_size = DW_BLOCK (attr)->size;
14264
14265 /* Parameters are not pre-cleared by memset above. */
14266 parameter->data_value = NULL;
14267 parameter->data_value_size = 0;
14268 call_site->parameter_count++;
14269
216f72a1
JK
14270 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14271 if (attr == NULL)
14272 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14273 if (attr != nullptr)
96408a79
SA
14274 {
14275 if (!attr_form_is_block (attr))
b98664d3 14276 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14277 "DW_TAG_call_site child DIE %s [in module %s]"),
14278 sect_offset_str (child_die->sect_off),
9c541725 14279 objfile_name (objfile));
96408a79
SA
14280 else
14281 {
14282 parameter->data_value = DW_BLOCK (attr)->data;
14283 parameter->data_value_size = DW_BLOCK (attr)->size;
14284 }
14285 }
14286 }
14287}
14288
71a3c369
TT
14289/* Helper function for read_variable. If DIE represents a virtual
14290 table, then return the type of the concrete object that is
14291 associated with the virtual table. Otherwise, return NULL. */
14292
14293static struct type *
14294rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14295{
14296 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14297 if (attr == NULL)
14298 return NULL;
14299
14300 /* Find the type DIE. */
14301 struct die_info *type_die = NULL;
14302 struct dwarf2_cu *type_cu = cu;
14303
14304 if (attr_form_is_ref (attr))
14305 type_die = follow_die_ref (die, attr, &type_cu);
14306 if (type_die == NULL)
14307 return NULL;
14308
14309 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14310 return NULL;
14311 return die_containing_type (type_die, type_cu);
14312}
14313
14314/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14315
14316static void
14317read_variable (struct die_info *die, struct dwarf2_cu *cu)
14318{
14319 struct rust_vtable_symbol *storage = NULL;
14320
14321 if (cu->language == language_rust)
14322 {
14323 struct type *containing_type = rust_containing_type (die, cu);
14324
14325 if (containing_type != NULL)
14326 {
518817b3 14327 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 14328
468c0cbb 14329 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
14330 initialize_objfile_symbol (storage);
14331 storage->concrete_type = containing_type;
cf724bc9 14332 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14333 }
14334 }
14335
e4a62c65
TV
14336 struct symbol *res = new_symbol (die, NULL, cu, storage);
14337 struct attribute *abstract_origin
14338 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14339 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14340 if (res == NULL && loc && abstract_origin)
14341 {
14342 /* We have a variable without a name, but with a location and an abstract
14343 origin. This may be a concrete instance of an abstract variable
14344 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14345 later. */
14346 struct dwarf2_cu *origin_cu = cu;
14347 struct die_info *origin_die
14348 = follow_die_ref (die, abstract_origin, &origin_cu);
14349 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14350 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14351 }
71a3c369
TT
14352}
14353
43988095
JK
14354/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14355 reading .debug_rnglists.
14356 Callback's type should be:
14357 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14358 Return true if the attributes are present and valid, otherwise,
14359 return false. */
14360
14361template <typename Callback>
14362static bool
14363dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14364 Callback &&callback)
14365{
ed2dc618 14366 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14367 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14368 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14369 bfd *obfd = objfile->obfd;
43988095
JK
14370 /* Base address selection entry. */
14371 CORE_ADDR base;
14372 int found_base;
43988095 14373 const gdb_byte *buffer;
43988095
JK
14374 CORE_ADDR baseaddr;
14375 bool overflow = false;
14376
14377 found_base = cu->base_known;
14378 base = cu->base_address;
14379
14380 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14381 if (offset >= dwarf2_per_objfile->rnglists.size)
14382 {
b98664d3 14383 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14384 offset);
14385 return false;
14386 }
14387 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14388
14389 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14390
14391 while (1)
14392 {
7814882a
JK
14393 /* Initialize it due to a false compiler warning. */
14394 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14395 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14396 + dwarf2_per_objfile->rnglists.size);
14397 unsigned int bytes_read;
14398
14399 if (buffer == buf_end)
14400 {
14401 overflow = true;
14402 break;
14403 }
14404 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14405 switch (rlet)
14406 {
14407 case DW_RLE_end_of_list:
14408 break;
14409 case DW_RLE_base_address:
14410 if (buffer + cu->header.addr_size > buf_end)
14411 {
14412 overflow = true;
14413 break;
14414 }
14415 base = read_address (obfd, buffer, cu, &bytes_read);
14416 found_base = 1;
14417 buffer += bytes_read;
14418 break;
14419 case DW_RLE_start_length:
14420 if (buffer + cu->header.addr_size > buf_end)
14421 {
14422 overflow = true;
14423 break;
14424 }
14425 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14426 buffer += bytes_read;
14427 range_end = (range_beginning
14428 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14429 buffer += bytes_read;
14430 if (buffer > buf_end)
14431 {
14432 overflow = true;
14433 break;
14434 }
14435 break;
14436 case DW_RLE_offset_pair:
14437 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14438 buffer += bytes_read;
14439 if (buffer > buf_end)
14440 {
14441 overflow = true;
14442 break;
14443 }
14444 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14445 buffer += bytes_read;
14446 if (buffer > buf_end)
14447 {
14448 overflow = true;
14449 break;
14450 }
14451 break;
14452 case DW_RLE_start_end:
14453 if (buffer + 2 * cu->header.addr_size > buf_end)
14454 {
14455 overflow = true;
14456 break;
14457 }
14458 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14459 buffer += bytes_read;
14460 range_end = read_address (obfd, buffer, cu, &bytes_read);
14461 buffer += bytes_read;
14462 break;
14463 default:
b98664d3 14464 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14465 return false;
14466 }
14467 if (rlet == DW_RLE_end_of_list || overflow)
14468 break;
14469 if (rlet == DW_RLE_base_address)
14470 continue;
14471
14472 if (!found_base)
14473 {
14474 /* We have no valid base address for the ranges
14475 data. */
b98664d3 14476 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14477 return false;
14478 }
14479
14480 if (range_beginning > range_end)
14481 {
14482 /* Inverted range entries are invalid. */
b98664d3 14483 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14484 return false;
14485 }
14486
14487 /* Empty range entries have no effect. */
14488 if (range_beginning == range_end)
14489 continue;
14490
14491 range_beginning += base;
14492 range_end += base;
14493
14494 /* A not-uncommon case of bad debug info.
14495 Don't pollute the addrmap with bad data. */
14496 if (range_beginning + baseaddr == 0
14497 && !dwarf2_per_objfile->has_section_at_zero)
14498 {
b98664d3 14499 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14500 " [in module %s]"), objfile_name (objfile));
14501 continue;
14502 }
14503
14504 callback (range_beginning, range_end);
14505 }
14506
14507 if (overflow)
14508 {
b98664d3 14509 complaint (_("Offset %d is not terminated "
43988095
JK
14510 "for DW_AT_ranges attribute"),
14511 offset);
14512 return false;
14513 }
14514
14515 return true;
14516}
14517
14518/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14519 Callback's type should be:
14520 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14521 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14522
43988095 14523template <typename Callback>
43039443 14524static int
5f46c5a5 14525dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14526 Callback &&callback)
43039443 14527{
ed2dc618 14528 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14529 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14530 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14531 struct comp_unit_head *cu_header = &cu->header;
14532 bfd *obfd = objfile->obfd;
14533 unsigned int addr_size = cu_header->addr_size;
14534 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14535 /* Base address selection entry. */
14536 CORE_ADDR base;
14537 int found_base;
14538 unsigned int dummy;
d521ce57 14539 const gdb_byte *buffer;
ff013f42 14540 CORE_ADDR baseaddr;
43039443 14541
43988095
JK
14542 if (cu_header->version >= 5)
14543 return dwarf2_rnglists_process (offset, cu, callback);
14544
d00adf39
DE
14545 found_base = cu->base_known;
14546 base = cu->base_address;
43039443 14547
be391dca 14548 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14549 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14550 {
b98664d3 14551 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14552 offset);
14553 return 0;
14554 }
dce234bc 14555 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14556
e7030f15 14557 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14558
43039443
JK
14559 while (1)
14560 {
14561 CORE_ADDR range_beginning, range_end;
14562
14563 range_beginning = read_address (obfd, buffer, cu, &dummy);
14564 buffer += addr_size;
14565 range_end = read_address (obfd, buffer, cu, &dummy);
14566 buffer += addr_size;
14567 offset += 2 * addr_size;
14568
14569 /* An end of list marker is a pair of zero addresses. */
14570 if (range_beginning == 0 && range_end == 0)
14571 /* Found the end of list entry. */
14572 break;
14573
14574 /* Each base address selection entry is a pair of 2 values.
14575 The first is the largest possible address, the second is
14576 the base address. Check for a base address here. */
14577 if ((range_beginning & mask) == mask)
14578 {
28d2bfb9
AB
14579 /* If we found the largest possible address, then we already
14580 have the base address in range_end. */
14581 base = range_end;
43039443
JK
14582 found_base = 1;
14583 continue;
14584 }
14585
14586 if (!found_base)
14587 {
14588 /* We have no valid base address for the ranges
14589 data. */
b98664d3 14590 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14591 return 0;
14592 }
14593
9277c30c
UW
14594 if (range_beginning > range_end)
14595 {
14596 /* Inverted range entries are invalid. */
b98664d3 14597 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14598 return 0;
14599 }
14600
14601 /* Empty range entries have no effect. */
14602 if (range_beginning == range_end)
14603 continue;
14604
43039443
JK
14605 range_beginning += base;
14606 range_end += base;
14607
01093045
DE
14608 /* A not-uncommon case of bad debug info.
14609 Don't pollute the addrmap with bad data. */
14610 if (range_beginning + baseaddr == 0
14611 && !dwarf2_per_objfile->has_section_at_zero)
14612 {
b98664d3 14613 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14614 " [in module %s]"), objfile_name (objfile));
01093045
DE
14615 continue;
14616 }
14617
5f46c5a5
JK
14618 callback (range_beginning, range_end);
14619 }
14620
14621 return 1;
14622}
14623
14624/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14625 Return 1 if the attributes are present and valid, otherwise, return 0.
14626 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14627
14628static int
14629dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14630 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14631 struct partial_symtab *ranges_pst)
14632{
518817b3 14633 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14634 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14635 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14636 SECT_OFF_TEXT (objfile));
14637 int low_set = 0;
14638 CORE_ADDR low = 0;
14639 CORE_ADDR high = 0;
14640 int retval;
14641
14642 retval = dwarf2_ranges_process (offset, cu,
14643 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14644 {
9277c30c 14645 if (ranges_pst != NULL)
3e29f34a
MR
14646 {
14647 CORE_ADDR lowpc;
14648 CORE_ADDR highpc;
14649
79748972
TT
14650 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14651 range_beginning + baseaddr)
14652 - baseaddr);
14653 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14654 range_end + baseaddr)
14655 - baseaddr);
d320c2b5
TT
14656 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14657 lowpc, highpc - 1, ranges_pst);
3e29f34a 14658 }
ff013f42 14659
43039443
JK
14660 /* FIXME: This is recording everything as a low-high
14661 segment of consecutive addresses. We should have a
14662 data structure for discontiguous block ranges
14663 instead. */
14664 if (! low_set)
14665 {
14666 low = range_beginning;
14667 high = range_end;
14668 low_set = 1;
14669 }
14670 else
14671 {
14672 if (range_beginning < low)
14673 low = range_beginning;
14674 if (range_end > high)
14675 high = range_end;
14676 }
5f46c5a5
JK
14677 });
14678 if (!retval)
14679 return 0;
43039443
JK
14680
14681 if (! low_set)
14682 /* If the first entry is an end-of-list marker, the range
14683 describes an empty scope, i.e. no instructions. */
14684 return 0;
14685
14686 if (low_return)
14687 *low_return = low;
14688 if (high_return)
14689 *high_return = high;
14690 return 1;
14691}
14692
3a2b436a
JK
14693/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14694 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14695 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14696
3a2b436a 14697static enum pc_bounds_kind
af34e669 14698dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14699 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14700 struct partial_symtab *pst)
c906108c 14701{
518817b3
SM
14702 struct dwarf2_per_objfile *dwarf2_per_objfile
14703 = cu->per_cu->dwarf2_per_objfile;
c906108c 14704 struct attribute *attr;
91da1414 14705 struct attribute *attr_high;
af34e669
DJ
14706 CORE_ADDR low = 0;
14707 CORE_ADDR high = 0;
e385593e 14708 enum pc_bounds_kind ret;
c906108c 14709
91da1414
MW
14710 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14711 if (attr_high)
af34e669 14712 {
e142c38c 14713 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14714 if (attr != nullptr)
91da1414 14715 {
31aa7e4e
JB
14716 low = attr_value_as_address (attr);
14717 high = attr_value_as_address (attr_high);
14718 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14719 high += low;
91da1414 14720 }
af34e669
DJ
14721 else
14722 /* Found high w/o low attribute. */
e385593e 14723 return PC_BOUNDS_INVALID;
af34e669
DJ
14724
14725 /* Found consecutive range of addresses. */
3a2b436a 14726 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14727 }
c906108c 14728 else
af34e669 14729 {
e142c38c 14730 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14731 if (attr != NULL)
14732 {
ab435259
DE
14733 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14734 We take advantage of the fact that DW_AT_ranges does not appear
14735 in DW_TAG_compile_unit of DWO files. */
14736 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14737 unsigned int ranges_offset = (DW_UNSND (attr)
14738 + (need_ranges_base
14739 ? cu->ranges_base
14740 : 0));
2e3cf129 14741
af34e669 14742 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14743 .debug_ranges section. */
2e3cf129 14744 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14745 return PC_BOUNDS_INVALID;
43039443 14746 /* Found discontinuous range of addresses. */
3a2b436a 14747 ret = PC_BOUNDS_RANGES;
af34e669 14748 }
e385593e
JK
14749 else
14750 return PC_BOUNDS_NOT_PRESENT;
af34e669 14751 }
c906108c 14752
48fbe735 14753 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14754 if (high <= low)
e385593e 14755 return PC_BOUNDS_INVALID;
c906108c
SS
14756
14757 /* When using the GNU linker, .gnu.linkonce. sections are used to
14758 eliminate duplicate copies of functions and vtables and such.
14759 The linker will arbitrarily choose one and discard the others.
14760 The AT_*_pc values for such functions refer to local labels in
14761 these sections. If the section from that file was discarded, the
14762 labels are not in the output, so the relocs get a value of 0.
14763 If this is a discarded function, mark the pc bounds as invalid,
14764 so that GDB will ignore it. */
72dca2f5 14765 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14766 return PC_BOUNDS_INVALID;
c906108c
SS
14767
14768 *lowpc = low;
96408a79
SA
14769 if (highpc)
14770 *highpc = high;
af34e669 14771 return ret;
c906108c
SS
14772}
14773
b084d499
JB
14774/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14775 its low and high PC addresses. Do nothing if these addresses could not
14776 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14777 and HIGHPC to the high address if greater than HIGHPC. */
14778
14779static void
14780dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14781 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14782 struct dwarf2_cu *cu)
14783{
14784 CORE_ADDR low, high;
14785 struct die_info *child = die->child;
14786
e385593e 14787 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14788 {
325fac50
PA
14789 *lowpc = std::min (*lowpc, low);
14790 *highpc = std::max (*highpc, high);
b084d499
JB
14791 }
14792
14793 /* If the language does not allow nested subprograms (either inside
14794 subprograms or lexical blocks), we're done. */
14795 if (cu->language != language_ada)
14796 return;
6e70227d 14797
b084d499
JB
14798 /* Check all the children of the given DIE. If it contains nested
14799 subprograms, then check their pc bounds. Likewise, we need to
14800 check lexical blocks as well, as they may also contain subprogram
14801 definitions. */
14802 while (child && child->tag)
14803 {
14804 if (child->tag == DW_TAG_subprogram
14805 || child->tag == DW_TAG_lexical_block)
14806 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14807 child = sibling_die (child);
14808 }
14809}
14810
fae299cd
DC
14811/* Get the low and high pc's represented by the scope DIE, and store
14812 them in *LOWPC and *HIGHPC. If the correct values can't be
14813 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14814
14815static void
14816get_scope_pc_bounds (struct die_info *die,
14817 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14818 struct dwarf2_cu *cu)
14819{
14820 CORE_ADDR best_low = (CORE_ADDR) -1;
14821 CORE_ADDR best_high = (CORE_ADDR) 0;
14822 CORE_ADDR current_low, current_high;
14823
3a2b436a 14824 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14825 >= PC_BOUNDS_RANGES)
fae299cd
DC
14826 {
14827 best_low = current_low;
14828 best_high = current_high;
14829 }
14830 else
14831 {
14832 struct die_info *child = die->child;
14833
14834 while (child && child->tag)
14835 {
14836 switch (child->tag) {
14837 case DW_TAG_subprogram:
b084d499 14838 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14839 break;
14840 case DW_TAG_namespace:
f55ee35c 14841 case DW_TAG_module:
fae299cd
DC
14842 /* FIXME: carlton/2004-01-16: Should we do this for
14843 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14844 that current GCC's always emit the DIEs corresponding
14845 to definitions of methods of classes as children of a
14846 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14847 the DIEs giving the declarations, which could be
14848 anywhere). But I don't see any reason why the
14849 standards says that they have to be there. */
14850 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14851
14852 if (current_low != ((CORE_ADDR) -1))
14853 {
325fac50
PA
14854 best_low = std::min (best_low, current_low);
14855 best_high = std::max (best_high, current_high);
fae299cd
DC
14856 }
14857 break;
14858 default:
0963b4bd 14859 /* Ignore. */
fae299cd
DC
14860 break;
14861 }
14862
14863 child = sibling_die (child);
14864 }
14865 }
14866
14867 *lowpc = best_low;
14868 *highpc = best_high;
14869}
14870
801e3a5b
JB
14871/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14872 in DIE. */
380bca97 14873
801e3a5b
JB
14874static void
14875dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14876 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14877{
518817b3 14878 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14879 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14880 struct attribute *attr;
91da1414 14881 struct attribute *attr_high;
801e3a5b 14882
91da1414
MW
14883 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14884 if (attr_high)
801e3a5b 14885 {
801e3a5b 14886 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14887 if (attr != nullptr)
801e3a5b 14888 {
31aa7e4e
JB
14889 CORE_ADDR low = attr_value_as_address (attr);
14890 CORE_ADDR high = attr_value_as_address (attr_high);
14891
14892 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14893 high += low;
9a619af0 14894
3e29f34a
MR
14895 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14896 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14897 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14898 }
14899 }
14900
14901 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14902 if (attr != nullptr)
801e3a5b 14903 {
ab435259
DE
14904 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14905 We take advantage of the fact that DW_AT_ranges does not appear
14906 in DW_TAG_compile_unit of DWO files. */
14907 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14908
14909 /* The value of the DW_AT_ranges attribute is the offset of the
14910 address range list in the .debug_ranges section. */
ab435259
DE
14911 unsigned long offset = (DW_UNSND (attr)
14912 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14913
2d5f09ec 14914 std::vector<blockrange> blockvec;
5f46c5a5
JK
14915 dwarf2_ranges_process (offset, cu,
14916 [&] (CORE_ADDR start, CORE_ADDR end)
14917 {
58fdfd2c
JK
14918 start += baseaddr;
14919 end += baseaddr;
5f46c5a5
JK
14920 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14921 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14922 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14923 blockvec.emplace_back (start, end);
5f46c5a5 14924 });
2d5f09ec
KB
14925
14926 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14927 }
14928}
14929
685b1105
JK
14930/* Check whether the producer field indicates either of GCC < 4.6, or the
14931 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14932
685b1105
JK
14933static void
14934check_producer (struct dwarf2_cu *cu)
60d5a603 14935{
38360086 14936 int major, minor;
60d5a603
JK
14937
14938 if (cu->producer == NULL)
14939 {
14940 /* For unknown compilers expect their behavior is DWARF version
14941 compliant.
14942
14943 GCC started to support .debug_types sections by -gdwarf-4 since
14944 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14945 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14946 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14947 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14948 }
b1ffba5a 14949 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14950 {
38360086
MW
14951 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14952 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14953 }
5230b05a 14954 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14955 {
14956 cu->producer_is_icc = true;
14957 cu->producer_is_icc_lt_14 = major < 14;
14958 }
c258c396
JD
14959 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14960 cu->producer_is_codewarrior = true;
685b1105
JK
14961 else
14962 {
14963 /* For other non-GCC compilers, expect their behavior is DWARF version
14964 compliant. */
60d5a603
JK
14965 }
14966
9068261f 14967 cu->checked_producer = true;
685b1105 14968}
ba919b58 14969
685b1105
JK
14970/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14971 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14972 during 4.6.0 experimental. */
14973
9068261f 14974static bool
685b1105
JK
14975producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14976{
14977 if (!cu->checked_producer)
14978 check_producer (cu);
14979
14980 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14981}
14982
c258c396
JD
14983
14984/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14985 with incorrect is_stmt attributes. */
14986
14987static bool
14988producer_is_codewarrior (struct dwarf2_cu *cu)
14989{
14990 if (!cu->checked_producer)
14991 check_producer (cu);
14992
14993 return cu->producer_is_codewarrior;
14994}
14995
405feb71 14996/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14997 DW_AT_accessibility. */
14998
14999static enum dwarf_access_attribute
15000dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15001{
15002 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15003 {
15004 /* The default DWARF 2 accessibility for members is public, the default
15005 accessibility for inheritance is private. */
15006
15007 if (die->tag != DW_TAG_inheritance)
15008 return DW_ACCESS_public;
15009 else
15010 return DW_ACCESS_private;
15011 }
15012 else
15013 {
15014 /* DWARF 3+ defines the default accessibility a different way. The same
15015 rules apply now for DW_TAG_inheritance as for the members and it only
15016 depends on the container kind. */
15017
15018 if (die->parent->tag == DW_TAG_class_type)
15019 return DW_ACCESS_private;
15020 else
15021 return DW_ACCESS_public;
15022 }
15023}
15024
74ac6d43
TT
15025/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15026 offset. If the attribute was not found return 0, otherwise return
15027 1. If it was found but could not properly be handled, set *OFFSET
15028 to 0. */
15029
15030static int
15031handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15032 LONGEST *offset)
15033{
15034 struct attribute *attr;
15035
15036 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15037 if (attr != NULL)
15038 {
15039 *offset = 0;
15040
15041 /* Note that we do not check for a section offset first here.
15042 This is because DW_AT_data_member_location is new in DWARF 4,
15043 so if we see it, we can assume that a constant form is really
15044 a constant and not a section offset. */
15045 if (attr_form_is_constant (attr))
15046 *offset = dwarf2_get_attr_constant_value (attr, 0);
15047 else if (attr_form_is_section_offset (attr))
15048 dwarf2_complex_location_expr_complaint ();
15049 else if (attr_form_is_block (attr))
15050 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15051 else
15052 dwarf2_complex_location_expr_complaint ();
15053
15054 return 1;
15055 }
15056
15057 return 0;
15058}
15059
c906108c
SS
15060/* Add an aggregate field to the field list. */
15061
15062static void
107d2387 15063dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15064 struct dwarf2_cu *cu)
6e70227d 15065{
518817b3 15066 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15067 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15068 struct nextfield *new_field;
15069 struct attribute *attr;
15070 struct field *fp;
15d034d0 15071 const char *fieldname = "";
c906108c 15072
7d0ccb61
DJ
15073 if (die->tag == DW_TAG_inheritance)
15074 {
be2daae6
TT
15075 fip->baseclasses.emplace_back ();
15076 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15077 }
15078 else
15079 {
be2daae6
TT
15080 fip->fields.emplace_back ();
15081 new_field = &fip->fields.back ();
7d0ccb61 15082 }
be2daae6 15083
c906108c
SS
15084 fip->nfields++;
15085
e142c38c 15086 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15087 if (attr != nullptr)
c906108c 15088 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15089 else
15090 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15091 if (new_field->accessibility != DW_ACCESS_public)
15092 fip->non_public_fields = 1;
60d5a603 15093
e142c38c 15094 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15095 if (attr != nullptr)
c906108c 15096 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15097 else
15098 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15099
15100 fp = &new_field->field;
a9a9bd0f 15101
e142c38c 15102 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15103 {
74ac6d43
TT
15104 LONGEST offset;
15105
a9a9bd0f 15106 /* Data member other than a C++ static data member. */
6e70227d 15107
c906108c 15108 /* Get type of field. */
e7c27a73 15109 fp->type = die_type (die, cu);
c906108c 15110
d6a843b5 15111 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15112
c906108c 15113 /* Get bit size of field (zero if none). */
e142c38c 15114 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15115 if (attr != nullptr)
c906108c
SS
15116 {
15117 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15118 }
15119 else
15120 {
15121 FIELD_BITSIZE (*fp) = 0;
15122 }
15123
15124 /* Get bit offset of field. */
74ac6d43
TT
15125 if (handle_data_member_location (die, cu, &offset))
15126 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15127 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15128 if (attr != nullptr)
c906108c 15129 {
d5a22e77 15130 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15131 {
15132 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15133 additional bit offset from the MSB of the containing
15134 anonymous object to the MSB of the field. We don't
15135 have to do anything special since we don't need to
15136 know the size of the anonymous object. */
f41f5e61 15137 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15138 }
15139 else
15140 {
15141 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15142 MSB of the anonymous object, subtract off the number of
15143 bits from the MSB of the field to the MSB of the
15144 object, and then subtract off the number of bits of
15145 the field itself. The result is the bit offset of
15146 the LSB of the field. */
c906108c
SS
15147 int anonymous_size;
15148 int bit_offset = DW_UNSND (attr);
15149
e142c38c 15150 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15151 if (attr != nullptr)
c906108c
SS
15152 {
15153 /* The size of the anonymous object containing
15154 the bit field is explicit, so use the
15155 indicated size (in bytes). */
15156 anonymous_size = DW_UNSND (attr);
15157 }
15158 else
15159 {
15160 /* The size of the anonymous object containing
15161 the bit field must be inferred from the type
15162 attribute of the data member containing the
15163 bit field. */
15164 anonymous_size = TYPE_LENGTH (fp->type);
15165 }
f41f5e61
PA
15166 SET_FIELD_BITPOS (*fp,
15167 (FIELD_BITPOS (*fp)
15168 + anonymous_size * bits_per_byte
15169 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15170 }
15171 }
da5b30da
AA
15172 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15173 if (attr != NULL)
15174 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15175 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15176
15177 /* Get name of field. */
39cbfefa
DJ
15178 fieldname = dwarf2_name (die, cu);
15179 if (fieldname == NULL)
15180 fieldname = "";
d8151005
DJ
15181
15182 /* The name is already allocated along with this objfile, so we don't
15183 need to duplicate it for the type. */
15184 fp->name = fieldname;
c906108c
SS
15185
15186 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15187 pointer or virtual base class pointer) to private. */
e142c38c 15188 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15189 {
d48cc9dd 15190 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15191 new_field->accessibility = DW_ACCESS_private;
15192 fip->non_public_fields = 1;
15193 }
15194 }
a9a9bd0f 15195 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15196 {
a9a9bd0f
DC
15197 /* C++ static member. */
15198
15199 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15200 is a declaration, but all versions of G++ as of this writing
15201 (so through at least 3.2.1) incorrectly generate
15202 DW_TAG_variable tags. */
6e70227d 15203
ff355380 15204 const char *physname;
c906108c 15205
a9a9bd0f 15206 /* Get name of field. */
39cbfefa
DJ
15207 fieldname = dwarf2_name (die, cu);
15208 if (fieldname == NULL)
c906108c
SS
15209 return;
15210
254e6b9e 15211 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15212 if (attr
15213 /* Only create a symbol if this is an external value.
15214 new_symbol checks this and puts the value in the global symbol
15215 table, which we want. If it is not external, new_symbol
15216 will try to put the value in cu->list_in_scope which is wrong. */
15217 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15218 {
15219 /* A static const member, not much different than an enum as far as
15220 we're concerned, except that we can support more types. */
15221 new_symbol (die, NULL, cu);
15222 }
15223
2df3850c 15224 /* Get physical name. */
ff355380 15225 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15226
d8151005
DJ
15227 /* The name is already allocated along with this objfile, so we don't
15228 need to duplicate it for the type. */
15229 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15230 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15231 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15232 }
15233 else if (die->tag == DW_TAG_inheritance)
15234 {
74ac6d43 15235 LONGEST offset;
d4b96c9a 15236
74ac6d43
TT
15237 /* C++ base class field. */
15238 if (handle_data_member_location (die, cu, &offset))
15239 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15240 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15241 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15242 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15243 }
2ddeaf8a
TT
15244 else if (die->tag == DW_TAG_variant_part)
15245 {
15246 /* process_structure_scope will treat this DIE as a union. */
15247 process_structure_scope (die, cu);
15248
15249 /* The variant part is relative to the start of the enclosing
15250 structure. */
15251 SET_FIELD_BITPOS (*fp, 0);
15252 fp->type = get_die_type (die, cu);
15253 fp->artificial = 1;
15254 fp->name = "<<variant>>";
c8c81635
TT
15255
15256 /* Normally a DW_TAG_variant_part won't have a size, but our
15257 representation requires one, so set it to the maximum of the
489dbda6
TT
15258 child sizes, being sure to account for the offset at which
15259 each child is seen. */
c8c81635
TT
15260 if (TYPE_LENGTH (fp->type) == 0)
15261 {
15262 unsigned max = 0;
15263 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
15264 {
15265 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15266 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15267 if (len > max)
15268 max = len;
15269 }
c8c81635
TT
15270 TYPE_LENGTH (fp->type) = max;
15271 }
2ddeaf8a
TT
15272 }
15273 else
15274 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15275}
15276
883fd55a
KS
15277/* Can the type given by DIE define another type? */
15278
15279static bool
15280type_can_define_types (const struct die_info *die)
15281{
15282 switch (die->tag)
15283 {
15284 case DW_TAG_typedef:
15285 case DW_TAG_class_type:
15286 case DW_TAG_structure_type:
15287 case DW_TAG_union_type:
15288 case DW_TAG_enumeration_type:
15289 return true;
15290
15291 default:
15292 return false;
15293 }
15294}
15295
15296/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15297
15298static void
883fd55a
KS
15299dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15300 struct dwarf2_cu *cu)
6e70227d 15301{
be2daae6
TT
15302 struct decl_field fp;
15303 memset (&fp, 0, sizeof (fp));
98751a41 15304
883fd55a 15305 gdb_assert (type_can_define_types (die));
98751a41 15306
883fd55a 15307 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15308 fp.name = dwarf2_name (die, cu);
15309 fp.type = read_type_die (die, cu);
98751a41 15310
c191a687
KS
15311 /* Save accessibility. */
15312 enum dwarf_access_attribute accessibility;
15313 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15314 if (attr != NULL)
15315 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15316 else
15317 accessibility = dwarf2_default_access_attribute (die, cu);
15318 switch (accessibility)
15319 {
15320 case DW_ACCESS_public:
15321 /* The assumed value if neither private nor protected. */
15322 break;
15323 case DW_ACCESS_private:
be2daae6 15324 fp.is_private = 1;
c191a687
KS
15325 break;
15326 case DW_ACCESS_protected:
be2daae6 15327 fp.is_protected = 1;
c191a687
KS
15328 break;
15329 default:
b98664d3 15330 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15331 }
15332
883fd55a 15333 if (die->tag == DW_TAG_typedef)
be2daae6 15334 fip->typedef_field_list.push_back (fp);
883fd55a 15335 else
be2daae6 15336 fip->nested_types_list.push_back (fp);
98751a41
JK
15337}
15338
c906108c
SS
15339/* Create the vector of fields, and attach it to the type. */
15340
15341static void
fba45db2 15342dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15343 struct dwarf2_cu *cu)
c906108c
SS
15344{
15345 int nfields = fip->nfields;
15346
15347 /* Record the field count, allocate space for the array of fields,
15348 and create blank accessibility bitfields if necessary. */
15349 TYPE_NFIELDS (type) = nfields;
15350 TYPE_FIELDS (type) = (struct field *)
be2daae6 15351 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15352
b4ba55a1 15353 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15354 {
15355 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15356
15357 TYPE_FIELD_PRIVATE_BITS (type) =
15358 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15359 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15360
15361 TYPE_FIELD_PROTECTED_BITS (type) =
15362 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15363 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15364
774b6a14
TT
15365 TYPE_FIELD_IGNORE_BITS (type) =
15366 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15367 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15368 }
15369
15370 /* If the type has baseclasses, allocate and clear a bit vector for
15371 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15372 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15373 {
be2daae6 15374 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15375 unsigned char *pointer;
c906108c
SS
15376
15377 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15378 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15379 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15380 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15381 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15382 }
15383
2ddeaf8a
TT
15384 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15385 {
15386 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15387
be2daae6 15388 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15389 {
be2daae6
TT
15390 struct nextfield &field = fip->fields[index];
15391
15392 if (field.variant.is_discriminant)
2ddeaf8a 15393 di->discriminant_index = index;
be2daae6 15394 else if (field.variant.default_branch)
2ddeaf8a
TT
15395 di->default_index = index;
15396 else
be2daae6 15397 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15398 }
15399 }
15400
be2daae6
TT
15401 /* Copy the saved-up fields into the field vector. */
15402 for (int i = 0; i < nfields; ++i)
c906108c 15403 {
be2daae6
TT
15404 struct nextfield &field
15405 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15406 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15407
be2daae6
TT
15408 TYPE_FIELD (type, i) = field.field;
15409 switch (field.accessibility)
c906108c 15410 {
c5aa993b 15411 case DW_ACCESS_private:
b4ba55a1 15412 if (cu->language != language_ada)
be2daae6 15413 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15414 break;
c906108c 15415
c5aa993b 15416 case DW_ACCESS_protected:
b4ba55a1 15417 if (cu->language != language_ada)
be2daae6 15418 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15419 break;
c906108c 15420
c5aa993b
JM
15421 case DW_ACCESS_public:
15422 break;
c906108c 15423
c5aa993b
JM
15424 default:
15425 /* Unknown accessibility. Complain and treat it as public. */
15426 {
b98664d3 15427 complaint (_("unsupported accessibility %d"),
be2daae6 15428 field.accessibility);
c5aa993b
JM
15429 }
15430 break;
c906108c 15431 }
be2daae6 15432 if (i < fip->baseclasses.size ())
c906108c 15433 {
be2daae6 15434 switch (field.virtuality)
c906108c 15435 {
c5aa993b
JM
15436 case DW_VIRTUALITY_virtual:
15437 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15438 if (cu->language == language_ada)
a73c6dcd 15439 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15440 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15441 break;
c906108c
SS
15442 }
15443 }
c906108c
SS
15444 }
15445}
15446
7d27a96d
TT
15447/* Return true if this member function is a constructor, false
15448 otherwise. */
15449
15450static int
15451dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15452{
15453 const char *fieldname;
fe978cb0 15454 const char *type_name;
7d27a96d
TT
15455 int len;
15456
15457 if (die->parent == NULL)
15458 return 0;
15459
15460 if (die->parent->tag != DW_TAG_structure_type
15461 && die->parent->tag != DW_TAG_union_type
15462 && die->parent->tag != DW_TAG_class_type)
15463 return 0;
15464
15465 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15466 type_name = dwarf2_name (die->parent, cu);
15467 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15468 return 0;
15469
15470 len = strlen (fieldname);
fe978cb0
PA
15471 return (strncmp (fieldname, type_name, len) == 0
15472 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15473}
15474
c906108c
SS
15475/* Add a member function to the proper fieldlist. */
15476
15477static void
107d2387 15478dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15479 struct type *type, struct dwarf2_cu *cu)
c906108c 15480{
518817b3 15481 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15482 struct attribute *attr;
c906108c 15483 int i;
be2daae6 15484 struct fnfieldlist *flp = nullptr;
c906108c 15485 struct fn_field *fnp;
15d034d0 15486 const char *fieldname;
f792889a 15487 struct type *this_type;
60d5a603 15488 enum dwarf_access_attribute accessibility;
c906108c 15489
b4ba55a1 15490 if (cu->language == language_ada)
a73c6dcd 15491 error (_("unexpected member function in Ada type"));
b4ba55a1 15492
2df3850c 15493 /* Get name of member function. */
39cbfefa
DJ
15494 fieldname = dwarf2_name (die, cu);
15495 if (fieldname == NULL)
2df3850c 15496 return;
c906108c 15497
c906108c 15498 /* Look up member function name in fieldlist. */
be2daae6 15499 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15500 {
27bfe10e 15501 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15502 {
15503 flp = &fip->fnfieldlists[i];
15504 break;
15505 }
c906108c
SS
15506 }
15507
be2daae6
TT
15508 /* Create a new fnfieldlist if necessary. */
15509 if (flp == nullptr)
c906108c 15510 {
be2daae6
TT
15511 fip->fnfieldlists.emplace_back ();
15512 flp = &fip->fnfieldlists.back ();
c906108c 15513 flp->name = fieldname;
be2daae6 15514 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15515 }
15516
be2daae6
TT
15517 /* Create a new member function field and add it to the vector of
15518 fnfieldlists. */
15519 flp->fnfields.emplace_back ();
15520 fnp = &flp->fnfields.back ();
3da10d80
KS
15521
15522 /* Delay processing of the physname until later. */
9c37b5ae 15523 if (cu->language == language_cplus)
be2daae6
TT
15524 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15525 die, cu);
3da10d80
KS
15526 else
15527 {
1d06ead6 15528 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15529 fnp->physname = physname ? physname : "";
15530 }
15531
c906108c 15532 fnp->type = alloc_type (objfile);
f792889a
DJ
15533 this_type = read_type_die (die, cu);
15534 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15535 {
f792889a 15536 int nparams = TYPE_NFIELDS (this_type);
c906108c 15537
f792889a 15538 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15539 of the method itself (TYPE_CODE_METHOD). */
15540 smash_to_method_type (fnp->type, type,
f792889a
DJ
15541 TYPE_TARGET_TYPE (this_type),
15542 TYPE_FIELDS (this_type),
15543 TYPE_NFIELDS (this_type),
15544 TYPE_VARARGS (this_type));
c906108c
SS
15545
15546 /* Handle static member functions.
c5aa993b 15547 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15548 member functions. G++ helps GDB by marking the first
15549 parameter for non-static member functions (which is the this
15550 pointer) as artificial. We obtain this information from
15551 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15552 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15553 fnp->voffset = VOFFSET_STATIC;
15554 }
15555 else
b98664d3 15556 complaint (_("member function type missing for '%s'"),
3da10d80 15557 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15558
15559 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15560 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15561 fnp->fcontext = die_containing_type (die, cu);
c906108c 15562
3e43a32a
MS
15563 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15564 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15565
15566 /* Get accessibility. */
e142c38c 15567 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15568 if (attr != nullptr)
aead7601 15569 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15570 else
15571 accessibility = dwarf2_default_access_attribute (die, cu);
15572 switch (accessibility)
c906108c 15573 {
60d5a603
JK
15574 case DW_ACCESS_private:
15575 fnp->is_private = 1;
15576 break;
15577 case DW_ACCESS_protected:
15578 fnp->is_protected = 1;
15579 break;
c906108c
SS
15580 }
15581
b02dede2 15582 /* Check for artificial methods. */
e142c38c 15583 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15584 if (attr && DW_UNSND (attr) != 0)
15585 fnp->is_artificial = 1;
15586
7d27a96d
TT
15587 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15588
0d564a31 15589 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15590 function. For older versions of GCC, this is an offset in the
15591 appropriate virtual table, as specified by DW_AT_containing_type.
15592 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15593 to the object address. */
15594
e142c38c 15595 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15596 if (attr != nullptr)
8e19ed76 15597 {
aec5aa8b 15598 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15599 {
aec5aa8b
TT
15600 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15601 {
15602 /* Old-style GCC. */
15603 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15604 }
15605 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15606 || (DW_BLOCK (attr)->size > 1
15607 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15608 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15609 {
aec5aa8b
TT
15610 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15611 if ((fnp->voffset % cu->header.addr_size) != 0)
15612 dwarf2_complex_location_expr_complaint ();
15613 else
15614 fnp->voffset /= cu->header.addr_size;
15615 fnp->voffset += 2;
15616 }
15617 else
15618 dwarf2_complex_location_expr_complaint ();
15619
15620 if (!fnp->fcontext)
7e993ebf
KS
15621 {
15622 /* If there is no `this' field and no DW_AT_containing_type,
15623 we cannot actually find a base class context for the
15624 vtable! */
15625 if (TYPE_NFIELDS (this_type) == 0
15626 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15627 {
b98664d3 15628 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15629 "function \"%s\" (offset %s)"),
15630 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15631 }
15632 else
15633 {
15634 fnp->fcontext
15635 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15636 }
15637 }
aec5aa8b 15638 }
3690dd37 15639 else if (attr_form_is_section_offset (attr))
8e19ed76 15640 {
4d3c2250 15641 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15642 }
15643 else
15644 {
4d3c2250
KB
15645 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15646 fieldname);
8e19ed76 15647 }
0d564a31 15648 }
d48cc9dd
DJ
15649 else
15650 {
15651 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15652 if (attr && DW_UNSND (attr))
15653 {
15654 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15655 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15656 "but the vtable offset is not specified"),
9d8780f0 15657 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15658 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15659 TYPE_CPLUS_DYNAMIC (type) = 1;
15660 }
15661 }
c906108c
SS
15662}
15663
15664/* Create the vector of member function fields, and attach it to the type. */
15665
15666static void
fba45db2 15667dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15668 struct dwarf2_cu *cu)
c906108c 15669{
b4ba55a1 15670 if (cu->language == language_ada)
a73c6dcd 15671 error (_("unexpected member functions in Ada type"));
b4ba55a1 15672
c906108c
SS
15673 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15674 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15675 TYPE_ALLOC (type,
15676 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15677
be2daae6 15678 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15679 {
be2daae6 15680 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15681 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15682
be2daae6
TT
15683 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15684 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15685 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15686 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15687
15688 for (int k = 0; k < nf.fnfields.size (); ++k)
15689 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15690 }
15691
be2daae6 15692 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15693}
15694
1168df01
JB
15695/* Returns non-zero if NAME is the name of a vtable member in CU's
15696 language, zero otherwise. */
15697static int
15698is_vtable_name (const char *name, struct dwarf2_cu *cu)
15699{
15700 static const char vptr[] = "_vptr";
15701
9c37b5ae
TT
15702 /* Look for the C++ form of the vtable. */
15703 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15704 return 1;
15705
15706 return 0;
15707}
15708
c0dd20ea 15709/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15710 functions, with the ABI-specified layout. If TYPE describes
15711 such a structure, smash it into a member function type.
61049d3b
DJ
15712
15713 GCC shouldn't do this; it should just output pointer to member DIEs.
15714 This is GCC PR debug/28767. */
c0dd20ea 15715
0b92b5bb
TT
15716static void
15717quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15718{
09e2d7c7 15719 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15720
15721 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15722 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15723 return;
c0dd20ea
DJ
15724
15725 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15726 if (TYPE_FIELD_NAME (type, 0) == NULL
15727 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15728 || TYPE_FIELD_NAME (type, 1) == NULL
15729 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15730 return;
c0dd20ea
DJ
15731
15732 /* Find the type of the method. */
0b92b5bb 15733 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15734 if (pfn_type == NULL
15735 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15736 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15737 return;
c0dd20ea
DJ
15738
15739 /* Look for the "this" argument. */
15740 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15741 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15742 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15743 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15744 return;
c0dd20ea 15745
09e2d7c7 15746 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15747 new_type = alloc_type (objfile);
09e2d7c7 15748 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15749 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15750 TYPE_VARARGS (pfn_type));
0b92b5bb 15751 smash_to_methodptr_type (type, new_type);
c0dd20ea 15752}
1168df01 15753
2b4424c3
TT
15754/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15755 appropriate error checking and issuing complaints if there is a
15756 problem. */
15757
15758static ULONGEST
15759get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15760{
15761 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15762
15763 if (attr == nullptr)
15764 return 0;
15765
15766 if (!attr_form_is_constant (attr))
15767 {
b98664d3 15768 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15769 " - DIE at %s [in module %s]"),
15770 sect_offset_str (die->sect_off),
15771 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15772 return 0;
15773 }
15774
15775 ULONGEST align;
15776 if (attr->form == DW_FORM_sdata)
15777 {
15778 LONGEST val = DW_SND (attr);
15779 if (val < 0)
15780 {
b98664d3 15781 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15782 " - DIE at %s [in module %s]"),
15783 sect_offset_str (die->sect_off),
15784 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15785 return 0;
15786 }
15787 align = val;
15788 }
15789 else
15790 align = DW_UNSND (attr);
15791
15792 if (align == 0)
15793 {
b98664d3 15794 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15795 " - DIE at %s [in module %s]"),
15796 sect_offset_str (die->sect_off),
15797 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15798 return 0;
15799 }
15800 if ((align & (align - 1)) != 0)
15801 {
b98664d3 15802 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15803 " - DIE at %s [in module %s]"),
15804 sect_offset_str (die->sect_off),
15805 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15806 return 0;
15807 }
15808
15809 return align;
15810}
15811
15812/* If the DIE has a DW_AT_alignment attribute, use its value to set
15813 the alignment for TYPE. */
15814
15815static void
15816maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15817 struct type *type)
15818{
15819 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15820 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15821 " - DIE at %s [in module %s]"),
15822 sect_offset_str (die->sect_off),
15823 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15824}
685b1105 15825
c906108c 15826/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15827 (definition) to create a type for the structure or union. Fill in
15828 the type's name and general properties; the members will not be
83655187
DE
15829 processed until process_structure_scope. A symbol table entry for
15830 the type will also not be done until process_structure_scope (assuming
15831 the type has a name).
c906108c 15832
c767944b
DJ
15833 NOTE: we need to call these functions regardless of whether or not the
15834 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15835 structure or union. This gets the type entered into our set of
83655187 15836 user defined types. */
c906108c 15837
f792889a 15838static struct type *
134d01f1 15839read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15840{
518817b3 15841 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15842 struct type *type;
15843 struct attribute *attr;
15d034d0 15844 const char *name;
c906108c 15845
348e048f
DE
15846 /* If the definition of this type lives in .debug_types, read that type.
15847 Don't follow DW_AT_specification though, that will take us back up
15848 the chain and we want to go down. */
45e58e77 15849 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15850 if (attr != nullptr)
348e048f 15851 {
ac9ec31b 15852 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15853
ac9ec31b 15854 /* The type's CU may not be the same as CU.
02142a6c 15855 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15856 return set_die_type (die, type, cu);
15857 }
15858
c0dd20ea 15859 type = alloc_type (objfile);
c906108c 15860 INIT_CPLUS_SPECIFIC (type);
93311388 15861
39cbfefa
DJ
15862 name = dwarf2_name (die, cu);
15863 if (name != NULL)
c906108c 15864 {
987504bb 15865 if (cu->language == language_cplus
c44af4eb
TT
15866 || cu->language == language_d
15867 || cu->language == language_rust)
63d06c5c 15868 {
15d034d0 15869 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15870
15871 /* dwarf2_full_name might have already finished building the DIE's
15872 type. If so, there is no need to continue. */
15873 if (get_die_type (die, cu) != NULL)
15874 return get_die_type (die, cu);
15875
e86ca25f 15876 TYPE_NAME (type) = full_name;
63d06c5c
DC
15877 }
15878 else
15879 {
d8151005
DJ
15880 /* The name is already allocated along with this objfile, so
15881 we don't need to duplicate it for the type. */
e86ca25f 15882 TYPE_NAME (type) = name;
63d06c5c 15883 }
c906108c
SS
15884 }
15885
15886 if (die->tag == DW_TAG_structure_type)
15887 {
15888 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15889 }
15890 else if (die->tag == DW_TAG_union_type)
15891 {
15892 TYPE_CODE (type) = TYPE_CODE_UNION;
15893 }
2ddeaf8a
TT
15894 else if (die->tag == DW_TAG_variant_part)
15895 {
15896 TYPE_CODE (type) = TYPE_CODE_UNION;
15897 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15898 }
c906108c
SS
15899 else
15900 {
4753d33b 15901 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15902 }
15903
0cc2414c
TT
15904 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15905 TYPE_DECLARED_CLASS (type) = 1;
15906
e142c38c 15907 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15908 if (attr != nullptr)
c906108c 15909 {
155bfbd3
JB
15910 if (attr_form_is_constant (attr))
15911 TYPE_LENGTH (type) = DW_UNSND (attr);
15912 else
15913 {
15914 /* For the moment, dynamic type sizes are not supported
15915 by GDB's struct type. The actual size is determined
15916 on-demand when resolving the type of a given object,
15917 so set the type's length to zero for now. Otherwise,
15918 we record an expression as the length, and that expression
15919 could lead to a very large value, which could eventually
15920 lead to us trying to allocate that much memory when creating
15921 a value of that type. */
15922 TYPE_LENGTH (type) = 0;
15923 }
c906108c
SS
15924 }
15925 else
15926 {
15927 TYPE_LENGTH (type) = 0;
15928 }
15929
2b4424c3
TT
15930 maybe_set_alignment (cu, die, type);
15931
5230b05a 15932 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15933 {
5230b05a
WT
15934 /* ICC<14 does not output the required DW_AT_declaration on
15935 incomplete types, but gives them a size of zero. */
422b1cb0 15936 TYPE_STUB (type) = 1;
685b1105
JK
15937 }
15938 else
15939 TYPE_STUB_SUPPORTED (type) = 1;
15940
dc718098 15941 if (die_is_declaration (die, cu))
876cecd0 15942 TYPE_STUB (type) = 1;
a6c727b2
DJ
15943 else if (attr == NULL && die->child == NULL
15944 && producer_is_realview (cu->producer))
15945 /* RealView does not output the required DW_AT_declaration
15946 on incomplete types. */
15947 TYPE_STUB (type) = 1;
dc718098 15948
c906108c
SS
15949 /* We need to add the type field to the die immediately so we don't
15950 infinitely recurse when dealing with pointers to the structure
0963b4bd 15951 type within the structure itself. */
1c379e20 15952 set_die_type (die, type, cu);
c906108c 15953
7e314c57
JK
15954 /* set_die_type should be already done. */
15955 set_descriptive_type (type, die, cu);
15956
c767944b
DJ
15957 return type;
15958}
15959
2ddeaf8a
TT
15960/* A helper for process_structure_scope that handles a single member
15961 DIE. */
15962
15963static void
15964handle_struct_member_die (struct die_info *child_die, struct type *type,
15965 struct field_info *fi,
15966 std::vector<struct symbol *> *template_args,
15967 struct dwarf2_cu *cu)
15968{
15969 if (child_die->tag == DW_TAG_member
15970 || child_die->tag == DW_TAG_variable
15971 || child_die->tag == DW_TAG_variant_part)
15972 {
15973 /* NOTE: carlton/2002-11-05: A C++ static data member
15974 should be a DW_TAG_member that is a declaration, but
15975 all versions of G++ as of this writing (so through at
15976 least 3.2.1) incorrectly generate DW_TAG_variable
15977 tags for them instead. */
15978 dwarf2_add_field (fi, child_die, cu);
15979 }
15980 else if (child_die->tag == DW_TAG_subprogram)
15981 {
15982 /* Rust doesn't have member functions in the C++ sense.
15983 However, it does emit ordinary functions as children
15984 of a struct DIE. */
15985 if (cu->language == language_rust)
15986 read_func_scope (child_die, cu);
15987 else
15988 {
15989 /* C++ member function. */
15990 dwarf2_add_member_fn (fi, child_die, type, cu);
15991 }
15992 }
15993 else if (child_die->tag == DW_TAG_inheritance)
15994 {
15995 /* C++ base class field. */
15996 dwarf2_add_field (fi, child_die, cu);
15997 }
15998 else if (type_can_define_types (child_die))
15999 dwarf2_add_type_defn (fi, child_die, cu);
16000 else if (child_die->tag == DW_TAG_template_type_param
16001 || child_die->tag == DW_TAG_template_value_param)
16002 {
16003 struct symbol *arg = new_symbol (child_die, NULL, cu);
16004
16005 if (arg != NULL)
16006 template_args->push_back (arg);
16007 }
16008 else if (child_die->tag == DW_TAG_variant)
16009 {
16010 /* In a variant we want to get the discriminant and also add a
16011 field for our sole member child. */
16012 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16013
bde09ab7 16014 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
16015 variant_child != NULL;
16016 variant_child = sibling_die (variant_child))
16017 {
16018 if (variant_child->tag == DW_TAG_member)
16019 {
16020 handle_struct_member_die (variant_child, type, fi,
16021 template_args, cu);
16022 /* Only handle the one. */
16023 break;
16024 }
16025 }
16026
16027 /* We don't handle this but we might as well report it if we see
16028 it. */
16029 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 16030 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
16031 " - DIE at %s [in module %s]"),
16032 sect_offset_str (child_die->sect_off),
16033 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16034
16035 /* The first field was just added, so we can stash the
16036 discriminant there. */
be2daae6 16037 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16038 if (discr == NULL)
be2daae6 16039 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16040 else
be2daae6 16041 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16042 }
16043}
16044
c767944b
DJ
16045/* Finish creating a structure or union type, including filling in
16046 its members and creating a symbol for it. */
16047
16048static void
16049process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16050{
518817b3 16051 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16052 struct die_info *child_die;
c767944b
DJ
16053 struct type *type;
16054
16055 type = get_die_type (die, cu);
16056 if (type == NULL)
16057 type = read_structure_type (die, cu);
16058
2ddeaf8a
TT
16059 /* When reading a DW_TAG_variant_part, we need to notice when we
16060 read the discriminant member, so we can record it later in the
16061 discriminant_info. */
16062 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 16063 sect_offset discr_offset {};
3e1d3d8c 16064 bool has_template_parameters = false;
2ddeaf8a
TT
16065
16066 if (is_variant_part)
16067 {
16068 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16069 if (discr == NULL)
16070 {
16071 /* Maybe it's a univariant form, an extension we support.
16072 In this case arrange not to check the offset. */
16073 is_variant_part = false;
16074 }
16075 else if (attr_form_is_ref (discr))
16076 {
16077 struct dwarf2_cu *target_cu = cu;
16078 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16079
16080 discr_offset = target_die->sect_off;
16081 }
16082 else
16083 {
b98664d3 16084 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16085 " - DIE at %s [in module %s]"),
16086 sect_offset_str (die->sect_off),
16087 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16088 is_variant_part = false;
16089 }
16090 }
16091
e142c38c 16092 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16093 {
16094 struct field_info fi;
2f4732b0 16095 std::vector<struct symbol *> template_args;
c906108c 16096
639d11d3 16097 child_die = die->child;
c906108c
SS
16098
16099 while (child_die && child_die->tag)
16100 {
2ddeaf8a 16101 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16102
2ddeaf8a 16103 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16104 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16105
c906108c
SS
16106 child_die = sibling_die (child_die);
16107 }
16108
34eaf542 16109 /* Attach template arguments to type. */
2f4732b0 16110 if (!template_args.empty ())
34eaf542 16111 {
3e1d3d8c 16112 has_template_parameters = true;
34eaf542 16113 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16114 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16115 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16116 = XOBNEWVEC (&objfile->objfile_obstack,
16117 struct symbol *,
16118 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16119 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16120 template_args.data (),
34eaf542
TT
16121 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16122 * sizeof (struct symbol *)));
34eaf542
TT
16123 }
16124
c906108c
SS
16125 /* Attach fields and member functions to the type. */
16126 if (fi.nfields)
e7c27a73 16127 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16128 if (!fi.fnfieldlists.empty ())
c906108c 16129 {
e7c27a73 16130 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16131
c5aa993b 16132 /* Get the type which refers to the base class (possibly this
c906108c 16133 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16134 class from the DW_AT_containing_type attribute. This use of
16135 DW_AT_containing_type is a GNU extension. */
c906108c 16136
e142c38c 16137 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16138 {
e7c27a73 16139 struct type *t = die_containing_type (die, cu);
c906108c 16140
ae6ae975 16141 set_type_vptr_basetype (type, t);
c906108c
SS
16142 if (type == t)
16143 {
c906108c
SS
16144 int i;
16145
16146 /* Our own class provides vtbl ptr. */
16147 for (i = TYPE_NFIELDS (t) - 1;
16148 i >= TYPE_N_BASECLASSES (t);
16149 --i)
16150 {
0d5cff50 16151 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16152
1168df01 16153 if (is_vtable_name (fieldname, cu))
c906108c 16154 {
ae6ae975 16155 set_type_vptr_fieldno (type, i);
c906108c
SS
16156 break;
16157 }
16158 }
16159
16160 /* Complain if virtual function table field not found. */
16161 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16162 complaint (_("virtual function table pointer "
3e43a32a 16163 "not found when defining class '%s'"),
e86ca25f 16164 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16165 }
16166 else
16167 {
ae6ae975 16168 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16169 }
16170 }
f6235d4c 16171 else if (cu->producer
61012eef 16172 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16173 {
16174 /* The IBM XLC compiler does not provide direct indication
16175 of the containing type, but the vtable pointer is
16176 always named __vfp. */
16177
16178 int i;
16179
16180 for (i = TYPE_NFIELDS (type) - 1;
16181 i >= TYPE_N_BASECLASSES (type);
16182 --i)
16183 {
16184 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16185 {
ae6ae975
DE
16186 set_type_vptr_fieldno (type, i);
16187 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16188 break;
16189 }
16190 }
16191 }
c906108c 16192 }
98751a41
JK
16193
16194 /* Copy fi.typedef_field_list linked list elements content into the
16195 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16196 if (!fi.typedef_field_list.empty ())
98751a41 16197 {
be2daae6 16198 int count = fi.typedef_field_list.size ();
98751a41 16199
a0d7a4ff 16200 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16201 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16202 = ((struct decl_field *)
be2daae6
TT
16203 TYPE_ALLOC (type,
16204 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16205 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16206
be2daae6
TT
16207 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16208 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16209 }
c767944b 16210
883fd55a
KS
16211 /* Copy fi.nested_types_list linked list elements content into the
16212 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16213 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16214 {
be2daae6 16215 int count = fi.nested_types_list.size ();
883fd55a
KS
16216
16217 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16218 TYPE_NESTED_TYPES_ARRAY (type)
16219 = ((struct decl_field *)
be2daae6
TT
16220 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16221 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16222
be2daae6
TT
16223 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16224 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16225 }
c906108c 16226 }
63d06c5c 16227
bb5ed363 16228 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16229 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16230 cu->rust_unions.push_back (type);
0b92b5bb 16231
90aeadfc
DC
16232 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16233 snapshots) has been known to create a die giving a declaration
16234 for a class that has, as a child, a die giving a definition for a
16235 nested class. So we have to process our children even if the
16236 current die is a declaration. Normally, of course, a declaration
16237 won't have any children at all. */
134d01f1 16238
ca040673
DE
16239 child_die = die->child;
16240
90aeadfc
DC
16241 while (child_die != NULL && child_die->tag)
16242 {
16243 if (child_die->tag == DW_TAG_member
16244 || child_die->tag == DW_TAG_variable
34eaf542
TT
16245 || child_die->tag == DW_TAG_inheritance
16246 || child_die->tag == DW_TAG_template_value_param
16247 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16248 {
90aeadfc 16249 /* Do nothing. */
134d01f1 16250 }
90aeadfc
DC
16251 else
16252 process_die (child_die, cu);
134d01f1 16253
90aeadfc 16254 child_die = sibling_die (child_die);
134d01f1
DJ
16255 }
16256
fa4028e9
JB
16257 /* Do not consider external references. According to the DWARF standard,
16258 these DIEs are identified by the fact that they have no byte_size
16259 attribute, and a declaration attribute. */
16260 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16261 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16262 {
16263 struct symbol *sym = new_symbol (die, type, cu);
16264
16265 if (has_template_parameters)
16266 {
a776957c
TT
16267 struct symtab *symtab;
16268 if (sym != nullptr)
16269 symtab = symbol_symtab (sym);
16270 else if (cu->line_header != nullptr)
16271 {
16272 /* Any related symtab will do. */
16273 symtab
7ba99d21 16274 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16275 }
16276 else
16277 {
16278 symtab = nullptr;
16279 complaint (_("could not find suitable "
16280 "symtab for template parameter"
16281 " - DIE at %s [in module %s]"),
16282 sect_offset_str (die->sect_off),
16283 objfile_name (objfile));
16284 }
16285
16286 if (symtab != nullptr)
16287 {
16288 /* Make sure that the symtab is set on the new symbols.
16289 Even though they don't appear in this symtab directly,
16290 other parts of gdb assume that symbols do, and this is
16291 reasonably true. */
16292 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16293 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16294 }
3e1d3d8c
TT
16295 }
16296 }
134d01f1
DJ
16297}
16298
55426c9d
JB
16299/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16300 update TYPE using some information only available in DIE's children. */
16301
16302static void
16303update_enumeration_type_from_children (struct die_info *die,
16304 struct type *type,
16305 struct dwarf2_cu *cu)
16306{
60f7655a 16307 struct die_info *child_die;
55426c9d
JB
16308 int unsigned_enum = 1;
16309 int flag_enum = 1;
16310 ULONGEST mask = 0;
55426c9d 16311
8268c778 16312 auto_obstack obstack;
55426c9d 16313
60f7655a
DE
16314 for (child_die = die->child;
16315 child_die != NULL && child_die->tag;
16316 child_die = sibling_die (child_die))
55426c9d
JB
16317 {
16318 struct attribute *attr;
16319 LONGEST value;
16320 const gdb_byte *bytes;
16321 struct dwarf2_locexpr_baton *baton;
16322 const char *name;
60f7655a 16323
55426c9d
JB
16324 if (child_die->tag != DW_TAG_enumerator)
16325 continue;
16326
16327 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16328 if (attr == NULL)
16329 continue;
16330
16331 name = dwarf2_name (child_die, cu);
16332 if (name == NULL)
16333 name = "<anonymous enumerator>";
16334
16335 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16336 &value, &bytes, &baton);
16337 if (value < 0)
16338 {
16339 unsigned_enum = 0;
16340 flag_enum = 0;
16341 }
16342 else if ((mask & value) != 0)
16343 flag_enum = 0;
16344 else
16345 mask |= value;
16346
16347 /* If we already know that the enum type is neither unsigned, nor
16348 a flag type, no need to look at the rest of the enumerates. */
16349 if (!unsigned_enum && !flag_enum)
16350 break;
55426c9d
JB
16351 }
16352
16353 if (unsigned_enum)
16354 TYPE_UNSIGNED (type) = 1;
16355 if (flag_enum)
16356 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16357}
16358
134d01f1
DJ
16359/* Given a DW_AT_enumeration_type die, set its type. We do not
16360 complete the type's fields yet, or create any symbols. */
c906108c 16361
f792889a 16362static struct type *
134d01f1 16363read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16364{
518817b3 16365 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16366 struct type *type;
c906108c 16367 struct attribute *attr;
0114d602 16368 const char *name;
134d01f1 16369
348e048f
DE
16370 /* If the definition of this type lives in .debug_types, read that type.
16371 Don't follow DW_AT_specification though, that will take us back up
16372 the chain and we want to go down. */
45e58e77 16373 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16374 if (attr != nullptr)
348e048f 16375 {
ac9ec31b 16376 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16377
ac9ec31b 16378 /* The type's CU may not be the same as CU.
02142a6c 16379 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16380 return set_die_type (die, type, cu);
16381 }
16382
c906108c
SS
16383 type = alloc_type (objfile);
16384
16385 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16386 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16387 if (name != NULL)
e86ca25f 16388 TYPE_NAME (type) = name;
c906108c 16389
0626fc76
TT
16390 attr = dwarf2_attr (die, DW_AT_type, cu);
16391 if (attr != NULL)
16392 {
16393 struct type *underlying_type = die_type (die, cu);
16394
16395 TYPE_TARGET_TYPE (type) = underlying_type;
16396 }
16397
e142c38c 16398 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16399 if (attr != nullptr)
c906108c
SS
16400 {
16401 TYPE_LENGTH (type) = DW_UNSND (attr);
16402 }
16403 else
16404 {
16405 TYPE_LENGTH (type) = 0;
16406 }
16407
2b4424c3
TT
16408 maybe_set_alignment (cu, die, type);
16409
137033e9
JB
16410 /* The enumeration DIE can be incomplete. In Ada, any type can be
16411 declared as private in the package spec, and then defined only
16412 inside the package body. Such types are known as Taft Amendment
16413 Types. When another package uses such a type, an incomplete DIE
16414 may be generated by the compiler. */
02eb380e 16415 if (die_is_declaration (die, cu))
876cecd0 16416 TYPE_STUB (type) = 1;
02eb380e 16417
0626fc76
TT
16418 /* Finish the creation of this type by using the enum's children.
16419 We must call this even when the underlying type has been provided
16420 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16421 update_enumeration_type_from_children (die, type, cu);
16422
0626fc76
TT
16423 /* If this type has an underlying type that is not a stub, then we
16424 may use its attributes. We always use the "unsigned" attribute
16425 in this situation, because ordinarily we guess whether the type
16426 is unsigned -- but the guess can be wrong and the underlying type
16427 can tell us the reality. However, we defer to a local size
16428 attribute if one exists, because this lets the compiler override
16429 the underlying type if needed. */
16430 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16431 {
16432 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16433 if (TYPE_LENGTH (type) == 0)
16434 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16435 if (TYPE_RAW_ALIGN (type) == 0
16436 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16437 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16438 }
16439
3d567982
TT
16440 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16441
f792889a 16442 return set_die_type (die, type, cu);
134d01f1
DJ
16443}
16444
16445/* Given a pointer to a die which begins an enumeration, process all
16446 the dies that define the members of the enumeration, and create the
16447 symbol for the enumeration type.
16448
16449 NOTE: We reverse the order of the element list. */
16450
16451static void
16452process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16453{
f792889a 16454 struct type *this_type;
134d01f1 16455
f792889a
DJ
16456 this_type = get_die_type (die, cu);
16457 if (this_type == NULL)
16458 this_type = read_enumeration_type (die, cu);
9dc481d3 16459
639d11d3 16460 if (die->child != NULL)
c906108c 16461 {
9dc481d3
DE
16462 struct die_info *child_die;
16463 struct symbol *sym;
16464 struct field *fields = NULL;
16465 int num_fields = 0;
15d034d0 16466 const char *name;
9dc481d3 16467
639d11d3 16468 child_die = die->child;
c906108c
SS
16469 while (child_die && child_die->tag)
16470 {
16471 if (child_die->tag != DW_TAG_enumerator)
16472 {
e7c27a73 16473 process_die (child_die, cu);
c906108c
SS
16474 }
16475 else
16476 {
39cbfefa
DJ
16477 name = dwarf2_name (child_die, cu);
16478 if (name)
c906108c 16479 {
f792889a 16480 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16481
16482 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16483 {
16484 fields = (struct field *)
16485 xrealloc (fields,
16486 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16487 * sizeof (struct field));
c906108c
SS
16488 }
16489
987012b8 16490 FIELD_NAME (fields[num_fields]) = sym->linkage_name ();
c906108c 16491 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16492 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16493 FIELD_BITSIZE (fields[num_fields]) = 0;
16494
16495 num_fields++;
16496 }
16497 }
16498
16499 child_die = sibling_die (child_die);
16500 }
16501
16502 if (num_fields)
16503 {
f792889a
DJ
16504 TYPE_NFIELDS (this_type) = num_fields;
16505 TYPE_FIELDS (this_type) = (struct field *)
16506 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16507 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16508 sizeof (struct field) * num_fields);
b8c9b27d 16509 xfree (fields);
c906108c 16510 }
c906108c 16511 }
134d01f1 16512
6c83ed52
TT
16513 /* If we are reading an enum from a .debug_types unit, and the enum
16514 is a declaration, and the enum is not the signatured type in the
16515 unit, then we do not want to add a symbol for it. Adding a
16516 symbol would in some cases obscure the true definition of the
16517 enum, giving users an incomplete type when the definition is
16518 actually available. Note that we do not want to do this for all
16519 enums which are just declarations, because C++0x allows forward
16520 enum declarations. */
3019eac3 16521 if (cu->per_cu->is_debug_types
6c83ed52
TT
16522 && die_is_declaration (die, cu))
16523 {
52dc124a 16524 struct signatured_type *sig_type;
6c83ed52 16525
c0f78cd4 16526 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16527 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16528 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16529 return;
16530 }
16531
f792889a 16532 new_symbol (die, this_type, cu);
c906108c
SS
16533}
16534
16535/* Extract all information from a DW_TAG_array_type DIE and put it in
16536 the DIE's type field. For now, this only handles one dimensional
16537 arrays. */
16538
f792889a 16539static struct type *
e7c27a73 16540read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16541{
518817b3 16542 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16543 struct die_info *child_die;
7e314c57 16544 struct type *type;
c906108c 16545 struct type *element_type, *range_type, *index_type;
c906108c 16546 struct attribute *attr;
15d034d0 16547 const char *name;
a405673c 16548 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16549 unsigned int bit_stride = 0;
c906108c 16550
e7c27a73 16551 element_type = die_type (die, cu);
c906108c 16552
7e314c57
JK
16553 /* The die_type call above may have already set the type for this DIE. */
16554 type = get_die_type (die, cu);
16555 if (type)
16556 return type;
16557
dc53a7ad
JB
16558 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16559 if (attr != NULL)
a405673c
JB
16560 {
16561 int stride_ok;
9a49df9d
AB
16562 struct type *prop_type
16563 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16564
16565 byte_stride_prop
16566 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16567 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16568 prop_type);
a405673c
JB
16569 if (!stride_ok)
16570 {
b98664d3 16571 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16572 " - DIE at %s [in module %s]"),
16573 sect_offset_str (die->sect_off),
518817b3 16574 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16575 /* Ignore this attribute. We will likely not be able to print
16576 arrays of this type correctly, but there is little we can do
16577 to help if we cannot read the attribute's value. */
16578 byte_stride_prop = NULL;
16579 }
16580 }
dc53a7ad
JB
16581
16582 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16583 if (attr != NULL)
16584 bit_stride = DW_UNSND (attr);
16585
c906108c
SS
16586 /* Irix 6.2 native cc creates array types without children for
16587 arrays with unspecified length. */
639d11d3 16588 if (die->child == NULL)
c906108c 16589 {
46bf5051 16590 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16591 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16592 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16593 byte_stride_prop, bit_stride);
f792889a 16594 return set_die_type (die, type, cu);
c906108c
SS
16595 }
16596
791afaa2 16597 std::vector<struct type *> range_types;
639d11d3 16598 child_die = die->child;
c906108c
SS
16599 while (child_die && child_die->tag)
16600 {
16601 if (child_die->tag == DW_TAG_subrange_type)
16602 {
f792889a 16603 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16604
f792889a 16605 if (child_type != NULL)
a02abb62 16606 {
0963b4bd
MS
16607 /* The range type was succesfully read. Save it for the
16608 array type creation. */
791afaa2 16609 range_types.push_back (child_type);
a02abb62 16610 }
c906108c
SS
16611 }
16612 child_die = sibling_die (child_die);
16613 }
16614
16615 /* Dwarf2 dimensions are output from left to right, create the
16616 necessary array types in backwards order. */
7ca2d3a3 16617
c906108c 16618 type = element_type;
7ca2d3a3
DL
16619
16620 if (read_array_order (die, cu) == DW_ORD_col_major)
16621 {
16622 int i = 0;
9a619af0 16623
791afaa2 16624 while (i < range_types.size ())
dc53a7ad 16625 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16626 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16627 }
16628 else
16629 {
791afaa2 16630 size_t ndim = range_types.size ();
7ca2d3a3 16631 while (ndim-- > 0)
dc53a7ad 16632 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16633 byte_stride_prop, bit_stride);
7ca2d3a3 16634 }
c906108c 16635
f5f8a009
EZ
16636 /* Understand Dwarf2 support for vector types (like they occur on
16637 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16638 array type. This is not part of the Dwarf2/3 standard yet, but a
16639 custom vendor extension. The main difference between a regular
16640 array and the vector variant is that vectors are passed by value
16641 to functions. */
e142c38c 16642 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16643 if (attr != nullptr)
ea37ba09 16644 make_vector_type (type);
f5f8a009 16645
dbc98a8b
KW
16646 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16647 implementation may choose to implement triple vectors using this
16648 attribute. */
16649 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16650 if (attr != nullptr)
dbc98a8b
KW
16651 {
16652 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16653 TYPE_LENGTH (type) = DW_UNSND (attr);
16654 else
b98664d3 16655 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16656 "than the total size of elements"));
dbc98a8b
KW
16657 }
16658
39cbfefa
DJ
16659 name = dwarf2_name (die, cu);
16660 if (name)
16661 TYPE_NAME (type) = name;
6e70227d 16662
2b4424c3
TT
16663 maybe_set_alignment (cu, die, type);
16664
0963b4bd 16665 /* Install the type in the die. */
7e314c57
JK
16666 set_die_type (die, type, cu);
16667
16668 /* set_die_type should be already done. */
b4ba55a1
JB
16669 set_descriptive_type (type, die, cu);
16670
7e314c57 16671 return type;
c906108c
SS
16672}
16673
7ca2d3a3 16674static enum dwarf_array_dim_ordering
6e70227d 16675read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16676{
16677 struct attribute *attr;
16678
16679 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16680
435d3d88 16681 if (attr != nullptr)
aead7601 16682 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16683
0963b4bd
MS
16684 /* GNU F77 is a special case, as at 08/2004 array type info is the
16685 opposite order to the dwarf2 specification, but data is still
16686 laid out as per normal fortran.
7ca2d3a3 16687
0963b4bd
MS
16688 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16689 version checking. */
7ca2d3a3 16690
905e0470
PM
16691 if (cu->language == language_fortran
16692 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16693 {
16694 return DW_ORD_row_major;
16695 }
16696
6e70227d 16697 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16698 {
16699 case array_column_major:
16700 return DW_ORD_col_major;
16701 case array_row_major:
16702 default:
16703 return DW_ORD_row_major;
16704 };
16705}
16706
72019c9c 16707/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16708 the DIE's type field. */
72019c9c 16709
f792889a 16710static struct type *
72019c9c
GM
16711read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16712{
7e314c57
JK
16713 struct type *domain_type, *set_type;
16714 struct attribute *attr;
f792889a 16715
7e314c57
JK
16716 domain_type = die_type (die, cu);
16717
16718 /* The die_type call above may have already set the type for this DIE. */
16719 set_type = get_die_type (die, cu);
16720 if (set_type)
16721 return set_type;
16722
16723 set_type = create_set_type (NULL, domain_type);
16724
16725 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16726 if (attr != nullptr)
d09039dd 16727 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16728
2b4424c3
TT
16729 maybe_set_alignment (cu, die, set_type);
16730
f792889a 16731 return set_die_type (die, set_type, cu);
72019c9c 16732}
7ca2d3a3 16733
0971de02
TT
16734/* A helper for read_common_block that creates a locexpr baton.
16735 SYM is the symbol which we are marking as computed.
16736 COMMON_DIE is the DIE for the common block.
16737 COMMON_LOC is the location expression attribute for the common
16738 block itself.
16739 MEMBER_LOC is the location expression attribute for the particular
16740 member of the common block that we are processing.
16741 CU is the CU from which the above come. */
16742
16743static void
16744mark_common_block_symbol_computed (struct symbol *sym,
16745 struct die_info *common_die,
16746 struct attribute *common_loc,
16747 struct attribute *member_loc,
16748 struct dwarf2_cu *cu)
16749{
518817b3
SM
16750 struct dwarf2_per_objfile *dwarf2_per_objfile
16751 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16752 struct objfile *objfile = dwarf2_per_objfile->objfile;
16753 struct dwarf2_locexpr_baton *baton;
16754 gdb_byte *ptr;
16755 unsigned int cu_off;
16756 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16757 LONGEST offset = 0;
16758
16759 gdb_assert (common_loc && member_loc);
16760 gdb_assert (attr_form_is_block (common_loc));
16761 gdb_assert (attr_form_is_block (member_loc)
16762 || attr_form_is_constant (member_loc));
16763
8d749320 16764 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16765 baton->per_cu = cu->per_cu;
16766 gdb_assert (baton->per_cu);
16767
16768 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16769
16770 if (attr_form_is_constant (member_loc))
16771 {
16772 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16773 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16774 }
16775 else
16776 baton->size += DW_BLOCK (member_loc)->size;
16777
224c3ddb 16778 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16779 baton->data = ptr;
16780
16781 *ptr++ = DW_OP_call4;
9c541725 16782 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16783 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16784 ptr += 4;
16785
16786 if (attr_form_is_constant (member_loc))
16787 {
16788 *ptr++ = DW_OP_addr;
16789 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16790 ptr += cu->header.addr_size;
16791 }
16792 else
16793 {
16794 /* We have to copy the data here, because DW_OP_call4 will only
16795 use a DW_AT_location attribute. */
16796 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16797 ptr += DW_BLOCK (member_loc)->size;
16798 }
16799
16800 *ptr++ = DW_OP_plus;
16801 gdb_assert (ptr - baton->data == baton->size);
16802
0971de02 16803 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16804 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16805}
16806
4357ac6c
TT
16807/* Create appropriate locally-scoped variables for all the
16808 DW_TAG_common_block entries. Also create a struct common_block
16809 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16810 is used to separate the common blocks name namespace from regular
4357ac6c 16811 variable names. */
c906108c
SS
16812
16813static void
e7c27a73 16814read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16815{
0971de02
TT
16816 struct attribute *attr;
16817
16818 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16819 if (attr != nullptr)
0971de02
TT
16820 {
16821 /* Support the .debug_loc offsets. */
16822 if (attr_form_is_block (attr))
16823 {
16824 /* Ok. */
16825 }
16826 else if (attr_form_is_section_offset (attr))
16827 {
16828 dwarf2_complex_location_expr_complaint ();
16829 attr = NULL;
16830 }
16831 else
16832 {
16833 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16834 "common block member");
16835 attr = NULL;
16836 }
16837 }
16838
639d11d3 16839 if (die->child != NULL)
c906108c 16840 {
518817b3 16841 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16842 struct die_info *child_die;
16843 size_t n_entries = 0, size;
16844 struct common_block *common_block;
16845 struct symbol *sym;
74ac6d43 16846
4357ac6c
TT
16847 for (child_die = die->child;
16848 child_die && child_die->tag;
16849 child_die = sibling_die (child_die))
16850 ++n_entries;
16851
16852 size = (sizeof (struct common_block)
16853 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16854 common_block
16855 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16856 size);
4357ac6c
TT
16857 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16858 common_block->n_entries = 0;
16859
16860 for (child_die = die->child;
16861 child_die && child_die->tag;
16862 child_die = sibling_die (child_die))
16863 {
16864 /* Create the symbol in the DW_TAG_common_block block in the current
16865 symbol scope. */
e7c27a73 16866 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16867 if (sym != NULL)
16868 {
16869 struct attribute *member_loc;
16870
16871 common_block->contents[common_block->n_entries++] = sym;
16872
16873 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16874 cu);
16875 if (member_loc)
16876 {
16877 /* GDB has handled this for a long time, but it is
16878 not specified by DWARF. It seems to have been
16879 emitted by gfortran at least as recently as:
16880 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16881 complaint (_("Variable in common block has "
0971de02 16882 "DW_AT_data_member_location "
9d8780f0
SM
16883 "- DIE at %s [in module %s]"),
16884 sect_offset_str (child_die->sect_off),
518817b3 16885 objfile_name (objfile));
0971de02
TT
16886
16887 if (attr_form_is_section_offset (member_loc))
16888 dwarf2_complex_location_expr_complaint ();
16889 else if (attr_form_is_constant (member_loc)
16890 || attr_form_is_block (member_loc))
16891 {
435d3d88 16892 if (attr != nullptr)
0971de02
TT
16893 mark_common_block_symbol_computed (sym, die, attr,
16894 member_loc, cu);
16895 }
16896 else
16897 dwarf2_complex_location_expr_complaint ();
16898 }
16899 }
c906108c 16900 }
4357ac6c
TT
16901
16902 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16903 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16904 }
16905}
16906
0114d602 16907/* Create a type for a C++ namespace. */
d9fa45fe 16908
0114d602
DJ
16909static struct type *
16910read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16911{
518817b3 16912 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16913 const char *previous_prefix, *name;
9219021c 16914 int is_anonymous;
0114d602
DJ
16915 struct type *type;
16916
16917 /* For extensions, reuse the type of the original namespace. */
16918 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16919 {
16920 struct die_info *ext_die;
16921 struct dwarf2_cu *ext_cu = cu;
9a619af0 16922
0114d602
DJ
16923 ext_die = dwarf2_extension (die, &ext_cu);
16924 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16925
16926 /* EXT_CU may not be the same as CU.
02142a6c 16927 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16928 return set_die_type (die, type, cu);
16929 }
9219021c 16930
e142c38c 16931 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16932
16933 /* Now build the name of the current namespace. */
16934
0114d602
DJ
16935 previous_prefix = determine_prefix (die, cu);
16936 if (previous_prefix[0] != '\0')
16937 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16938 previous_prefix, name, 0, cu);
0114d602
DJ
16939
16940 /* Create the type. */
19f392bc 16941 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16942
60531b24 16943 return set_die_type (die, type, cu);
0114d602
DJ
16944}
16945
22cee43f 16946/* Read a namespace scope. */
0114d602
DJ
16947
16948static void
16949read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16950{
518817b3 16951 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16952 int is_anonymous;
9219021c 16953
5c4e30ca
DC
16954 /* Add a symbol associated to this if we haven't seen the namespace
16955 before. Also, add a using directive if it's an anonymous
16956 namespace. */
9219021c 16957
f2f0e013 16958 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16959 {
16960 struct type *type;
16961
0114d602 16962 type = read_type_die (die, cu);
e7c27a73 16963 new_symbol (die, type, cu);
5c4e30ca 16964
e8e80198 16965 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16966 if (is_anonymous)
0114d602
DJ
16967 {
16968 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16969
eb1e02fd 16970 std::vector<const char *> excludes;
804d2729 16971 add_using_directive (using_directives (cu),
22cee43f 16972 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16973 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16974 }
5c4e30ca 16975 }
9219021c 16976
639d11d3 16977 if (die->child != NULL)
d9fa45fe 16978 {
639d11d3 16979 struct die_info *child_die = die->child;
6e70227d 16980
d9fa45fe
DC
16981 while (child_die && child_die->tag)
16982 {
e7c27a73 16983 process_die (child_die, cu);
d9fa45fe
DC
16984 child_die = sibling_die (child_die);
16985 }
16986 }
38d518c9
EZ
16987}
16988
f55ee35c
JK
16989/* Read a Fortran module as type. This DIE can be only a declaration used for
16990 imported module. Still we need that type as local Fortran "use ... only"
16991 declaration imports depend on the created type in determine_prefix. */
16992
16993static struct type *
16994read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16995{
518817b3 16996 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16997 const char *module_name;
f55ee35c
JK
16998 struct type *type;
16999
17000 module_name = dwarf2_name (die, cu);
19f392bc 17001 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17002
f55ee35c
JK
17003 return set_die_type (die, type, cu);
17004}
17005
5d7cb8df
JK
17006/* Read a Fortran module. */
17007
17008static void
17009read_module (struct die_info *die, struct dwarf2_cu *cu)
17010{
17011 struct die_info *child_die = die->child;
530e8392
KB
17012 struct type *type;
17013
17014 type = read_type_die (die, cu);
17015 new_symbol (die, type, cu);
5d7cb8df 17016
5d7cb8df
JK
17017 while (child_die && child_die->tag)
17018 {
17019 process_die (child_die, cu);
17020 child_die = sibling_die (child_die);
17021 }
17022}
17023
38d518c9
EZ
17024/* Return the name of the namespace represented by DIE. Set
17025 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17026 namespace. */
17027
17028static const char *
e142c38c 17029namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17030{
17031 struct die_info *current_die;
17032 const char *name = NULL;
17033
17034 /* Loop through the extensions until we find a name. */
17035
17036 for (current_die = die;
17037 current_die != NULL;
f2f0e013 17038 current_die = dwarf2_extension (die, &cu))
38d518c9 17039 {
96553a0c
DE
17040 /* We don't use dwarf2_name here so that we can detect the absence
17041 of a name -> anonymous namespace. */
7d45c7c3 17042 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17043
38d518c9
EZ
17044 if (name != NULL)
17045 break;
17046 }
17047
17048 /* Is it an anonymous namespace? */
17049
17050 *is_anonymous = (name == NULL);
17051 if (*is_anonymous)
2b1dbab0 17052 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17053
17054 return name;
d9fa45fe
DC
17055}
17056
c906108c
SS
17057/* Extract all information from a DW_TAG_pointer_type DIE and add to
17058 the user defined type vector. */
17059
f792889a 17060static struct type *
e7c27a73 17061read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17062{
518817b3
SM
17063 struct gdbarch *gdbarch
17064 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17065 struct comp_unit_head *cu_header = &cu->header;
c906108c 17066 struct type *type;
8b2dbe47
KB
17067 struct attribute *attr_byte_size;
17068 struct attribute *attr_address_class;
17069 int byte_size, addr_class;
7e314c57
JK
17070 struct type *target_type;
17071
17072 target_type = die_type (die, cu);
c906108c 17073
7e314c57
JK
17074 /* The die_type call above may have already set the type for this DIE. */
17075 type = get_die_type (die, cu);
17076 if (type)
17077 return type;
17078
17079 type = lookup_pointer_type (target_type);
8b2dbe47 17080
e142c38c 17081 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17082 if (attr_byte_size)
17083 byte_size = DW_UNSND (attr_byte_size);
c906108c 17084 else
8b2dbe47
KB
17085 byte_size = cu_header->addr_size;
17086
e142c38c 17087 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17088 if (attr_address_class)
17089 addr_class = DW_UNSND (attr_address_class);
17090 else
17091 addr_class = DW_ADDR_none;
17092
2b4424c3
TT
17093 ULONGEST alignment = get_alignment (cu, die);
17094
17095 /* If the pointer size, alignment, or address class is different
17096 than the default, create a type variant marked as such and set
17097 the length accordingly. */
17098 if (TYPE_LENGTH (type) != byte_size
17099 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17100 && alignment != TYPE_RAW_ALIGN (type))
17101 || addr_class != DW_ADDR_none)
c906108c 17102 {
5e2b427d 17103 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17104 {
17105 int type_flags;
17106
849957d9 17107 type_flags = gdbarch_address_class_type_flags
5e2b427d 17108 (gdbarch, byte_size, addr_class);
876cecd0
TT
17109 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17110 == 0);
8b2dbe47
KB
17111 type = make_type_with_address_space (type, type_flags);
17112 }
17113 else if (TYPE_LENGTH (type) != byte_size)
17114 {
b98664d3 17115 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17116 }
2b4424c3
TT
17117 else if (TYPE_RAW_ALIGN (type) != alignment)
17118 {
b98664d3 17119 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17120 " - DIE at %s [in module %s]"),
17121 sect_offset_str (die->sect_off),
17122 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17123 }
6e70227d 17124 else
9a619af0
MS
17125 {
17126 /* Should we also complain about unhandled address classes? */
17127 }
c906108c 17128 }
8b2dbe47
KB
17129
17130 TYPE_LENGTH (type) = byte_size;
2b4424c3 17131 set_type_align (type, alignment);
f792889a 17132 return set_die_type (die, type, cu);
c906108c
SS
17133}
17134
17135/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17136 the user defined type vector. */
17137
f792889a 17138static struct type *
e7c27a73 17139read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17140{
17141 struct type *type;
17142 struct type *to_type;
17143 struct type *domain;
17144
e7c27a73
DJ
17145 to_type = die_type (die, cu);
17146 domain = die_containing_type (die, cu);
0d5de010 17147
7e314c57
JK
17148 /* The calls above may have already set the type for this DIE. */
17149 type = get_die_type (die, cu);
17150 if (type)
17151 return type;
17152
0d5de010
DJ
17153 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17154 type = lookup_methodptr_type (to_type);
7078baeb
TT
17155 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17156 {
518817b3
SM
17157 struct type *new_type
17158 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17159
17160 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17161 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17162 TYPE_VARARGS (to_type));
17163 type = lookup_methodptr_type (new_type);
17164 }
0d5de010
DJ
17165 else
17166 type = lookup_memberptr_type (to_type, domain);
c906108c 17167
f792889a 17168 return set_die_type (die, type, cu);
c906108c
SS
17169}
17170
4297a3f0 17171/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17172 the user defined type vector. */
17173
f792889a 17174static struct type *
4297a3f0
AV
17175read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17176 enum type_code refcode)
c906108c 17177{
e7c27a73 17178 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17179 struct type *type, *target_type;
c906108c
SS
17180 struct attribute *attr;
17181
4297a3f0
AV
17182 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17183
7e314c57
JK
17184 target_type = die_type (die, cu);
17185
17186 /* The die_type call above may have already set the type for this DIE. */
17187 type = get_die_type (die, cu);
17188 if (type)
17189 return type;
17190
4297a3f0 17191 type = lookup_reference_type (target_type, refcode);
e142c38c 17192 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17193 if (attr != nullptr)
c906108c
SS
17194 {
17195 TYPE_LENGTH (type) = DW_UNSND (attr);
17196 }
17197 else
17198 {
107d2387 17199 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17200 }
2b4424c3 17201 maybe_set_alignment (cu, die, type);
f792889a 17202 return set_die_type (die, type, cu);
c906108c
SS
17203}
17204
cf363f18
MW
17205/* Add the given cv-qualifiers to the element type of the array. GCC
17206 outputs DWARF type qualifiers that apply to an array, not the
17207 element type. But GDB relies on the array element type to carry
17208 the cv-qualifiers. This mimics section 6.7.3 of the C99
17209 specification. */
17210
17211static struct type *
17212add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17213 struct type *base_type, int cnst, int voltl)
17214{
17215 struct type *el_type, *inner_array;
17216
17217 base_type = copy_type (base_type);
17218 inner_array = base_type;
17219
17220 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17221 {
17222 TYPE_TARGET_TYPE (inner_array) =
17223 copy_type (TYPE_TARGET_TYPE (inner_array));
17224 inner_array = TYPE_TARGET_TYPE (inner_array);
17225 }
17226
17227 el_type = TYPE_TARGET_TYPE (inner_array);
17228 cnst |= TYPE_CONST (el_type);
17229 voltl |= TYPE_VOLATILE (el_type);
17230 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17231
17232 return set_die_type (die, base_type, cu);
17233}
17234
f792889a 17235static struct type *
e7c27a73 17236read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17237{
f792889a 17238 struct type *base_type, *cv_type;
c906108c 17239
e7c27a73 17240 base_type = die_type (die, cu);
7e314c57
JK
17241
17242 /* The die_type call above may have already set the type for this DIE. */
17243 cv_type = get_die_type (die, cu);
17244 if (cv_type)
17245 return cv_type;
17246
2f608a3a
KW
17247 /* In case the const qualifier is applied to an array type, the element type
17248 is so qualified, not the array type (section 6.7.3 of C99). */
17249 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17250 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17251
f792889a
DJ
17252 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17253 return set_die_type (die, cv_type, cu);
c906108c
SS
17254}
17255
f792889a 17256static struct type *
e7c27a73 17257read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17258{
f792889a 17259 struct type *base_type, *cv_type;
c906108c 17260
e7c27a73 17261 base_type = die_type (die, cu);
7e314c57
JK
17262
17263 /* The die_type call above may have already set the type for this DIE. */
17264 cv_type = get_die_type (die, cu);
17265 if (cv_type)
17266 return cv_type;
17267
cf363f18
MW
17268 /* In case the volatile qualifier is applied to an array type, the
17269 element type is so qualified, not the array type (section 6.7.3
17270 of C99). */
17271 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17272 return add_array_cv_type (die, cu, base_type, 0, 1);
17273
f792889a
DJ
17274 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17275 return set_die_type (die, cv_type, cu);
c906108c
SS
17276}
17277
06d66ee9
TT
17278/* Handle DW_TAG_restrict_type. */
17279
17280static struct type *
17281read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17282{
17283 struct type *base_type, *cv_type;
17284
17285 base_type = die_type (die, cu);
17286
17287 /* The die_type call above may have already set the type for this DIE. */
17288 cv_type = get_die_type (die, cu);
17289 if (cv_type)
17290 return cv_type;
17291
17292 cv_type = make_restrict_type (base_type);
17293 return set_die_type (die, cv_type, cu);
17294}
17295
a2c2acaf
MW
17296/* Handle DW_TAG_atomic_type. */
17297
17298static struct type *
17299read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17300{
17301 struct type *base_type, *cv_type;
17302
17303 base_type = die_type (die, cu);
17304
17305 /* The die_type call above may have already set the type for this DIE. */
17306 cv_type = get_die_type (die, cu);
17307 if (cv_type)
17308 return cv_type;
17309
17310 cv_type = make_atomic_type (base_type);
17311 return set_die_type (die, cv_type, cu);
17312}
17313
c906108c
SS
17314/* Extract all information from a DW_TAG_string_type DIE and add to
17315 the user defined type vector. It isn't really a user defined type,
17316 but it behaves like one, with other DIE's using an AT_user_def_type
17317 attribute to reference it. */
17318
f792889a 17319static struct type *
e7c27a73 17320read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17321{
518817b3 17322 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17323 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17324 struct type *type, *range_type, *index_type, *char_type;
17325 struct attribute *attr;
216a7e6b
AB
17326 struct dynamic_prop prop;
17327 bool length_is_constant = true;
17328 LONGEST length;
17329
17330 /* There are a couple of places where bit sizes might be made use of
17331 when parsing a DW_TAG_string_type, however, no producer that we know
17332 of make use of these. Handling bit sizes that are a multiple of the
17333 byte size is easy enough, but what about other bit sizes? Lets deal
17334 with that problem when we have to. Warn about these attributes being
17335 unsupported, then parse the type and ignore them like we always
17336 have. */
17337 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17338 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17339 {
17340 static bool warning_printed = false;
17341 if (!warning_printed)
17342 {
17343 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17344 "currently supported on DW_TAG_string_type."));
17345 warning_printed = true;
17346 }
17347 }
c906108c 17348
e142c38c 17349 attr = dwarf2_attr (die, DW_AT_string_length, cu);
216a7e6b
AB
17350 if (attr != nullptr && !attr_form_is_constant (attr))
17351 {
17352 /* The string length describes the location at which the length of
17353 the string can be found. The size of the length field can be
17354 specified with one of the attributes below. */
17355 struct type *prop_type;
17356 struct attribute *len
17357 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17358 if (len == nullptr)
17359 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17360 if (len != nullptr && attr_form_is_constant (len))
17361 {
17362 /* Pass 0 as the default as we know this attribute is constant
17363 and the default value will not be returned. */
17364 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17365 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17366 }
17367 else
17368 {
17369 /* If the size is not specified then we assume it is the size of
17370 an address on this target. */
17371 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17372 }
17373
17374 /* Convert the attribute into a dynamic property. */
17375 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17376 length = 1;
17377 else
17378 length_is_constant = false;
17379 }
17380 else if (attr != nullptr)
17381 {
17382 /* This DW_AT_string_length just contains the length with no
17383 indirection. There's no need to create a dynamic property in this
17384 case. Pass 0 for the default value as we know it will not be
17385 returned in this case. */
17386 length = dwarf2_get_attr_constant_value (attr, 0);
17387 }
17388 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17389 {
216a7e6b
AB
17390 /* We don't currently support non-constant byte sizes for strings. */
17391 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17392 }
17393 else
17394 {
216a7e6b
AB
17395 /* Use 1 as a fallback length if we have nothing else. */
17396 length = 1;
c906108c 17397 }
6ccb9162 17398
46bf5051 17399 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17400 if (length_is_constant)
17401 range_type = create_static_range_type (NULL, index_type, 1, length);
17402 else
17403 {
17404 struct dynamic_prop low_bound;
17405
17406 low_bound.kind = PROP_CONST;
17407 low_bound.data.const_val = 1;
17408 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17409 }
3b7538c0
UW
17410 char_type = language_string_char_type (cu->language_defn, gdbarch);
17411 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17412
f792889a 17413 return set_die_type (die, type, cu);
c906108c
SS
17414}
17415
4d804846
JB
17416/* Assuming that DIE corresponds to a function, returns nonzero
17417 if the function is prototyped. */
17418
17419static int
17420prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17421{
17422 struct attribute *attr;
17423
17424 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17425 if (attr && (DW_UNSND (attr) != 0))
17426 return 1;
17427
17428 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17429 is only meaningful for C, but the concept also extends to other
4d804846
JB
17430 languages that allow unprototyped functions (Eg: Objective C).
17431 For all other languages, assume that functions are always
17432 prototyped. */
17433 if (cu->language != language_c
17434 && cu->language != language_objc
17435 && cu->language != language_opencl)
17436 return 1;
17437
17438 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17439 prototyped and unprototyped functions; default to prototyped,
17440 since that is more common in modern code (and RealView warns
17441 about unprototyped functions). */
17442 if (producer_is_realview (cu->producer))
17443 return 1;
17444
17445 return 0;
17446}
17447
c906108c
SS
17448/* Handle DIES due to C code like:
17449
17450 struct foo
c5aa993b
JM
17451 {
17452 int (*funcp)(int a, long l);
17453 int b;
17454 };
c906108c 17455
0963b4bd 17456 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17457
f792889a 17458static struct type *
e7c27a73 17459read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17460{
518817b3 17461 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17462 struct type *type; /* Type that this function returns. */
17463 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17464 struct attribute *attr;
17465
e7c27a73 17466 type = die_type (die, cu);
7e314c57
JK
17467
17468 /* The die_type call above may have already set the type for this DIE. */
17469 ftype = get_die_type (die, cu);
17470 if (ftype)
17471 return ftype;
17472
0c8b41f1 17473 ftype = lookup_function_type (type);
c906108c 17474
4d804846 17475 if (prototyped_function_p (die, cu))
a6c727b2 17476 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17477
c055b101
CV
17478 /* Store the calling convention in the type if it's available in
17479 the subroutine die. Otherwise set the calling convention to
17480 the default value DW_CC_normal. */
17481 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
435d3d88 17482 if (attr != nullptr)
54fcddd0
UW
17483 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17484 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17485 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17486 else
17487 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17488
743649fd
MW
17489 /* Record whether the function returns normally to its caller or not
17490 if the DWARF producer set that information. */
17491 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17492 if (attr && (DW_UNSND (attr) != 0))
17493 TYPE_NO_RETURN (ftype) = 1;
17494
76c10ea2
GM
17495 /* We need to add the subroutine type to the die immediately so
17496 we don't infinitely recurse when dealing with parameters
0963b4bd 17497 declared as the same subroutine type. */
76c10ea2 17498 set_die_type (die, ftype, cu);
6e70227d 17499
639d11d3 17500 if (die->child != NULL)
c906108c 17501 {
bb5ed363 17502 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17503 struct die_info *child_die;
8072405b 17504 int nparams, iparams;
c906108c
SS
17505
17506 /* Count the number of parameters.
17507 FIXME: GDB currently ignores vararg functions, but knows about
17508 vararg member functions. */
8072405b 17509 nparams = 0;
639d11d3 17510 child_die = die->child;
c906108c
SS
17511 while (child_die && child_die->tag)
17512 {
17513 if (child_die->tag == DW_TAG_formal_parameter)
17514 nparams++;
17515 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17516 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17517 child_die = sibling_die (child_die);
17518 }
17519
17520 /* Allocate storage for parameters and fill them in. */
17521 TYPE_NFIELDS (ftype) = nparams;
17522 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17523 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17524
8072405b
JK
17525 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17526 even if we error out during the parameters reading below. */
17527 for (iparams = 0; iparams < nparams; iparams++)
17528 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17529
17530 iparams = 0;
639d11d3 17531 child_die = die->child;
c906108c
SS
17532 while (child_die && child_die->tag)
17533 {
17534 if (child_die->tag == DW_TAG_formal_parameter)
17535 {
3ce3b1ba
PA
17536 struct type *arg_type;
17537
17538 /* DWARF version 2 has no clean way to discern C++
17539 static and non-static member functions. G++ helps
17540 GDB by marking the first parameter for non-static
17541 member functions (which is the this pointer) as
17542 artificial. We pass this information to
17543 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17544
17545 DWARF version 3 added DW_AT_object_pointer, which GCC
17546 4.5 does not yet generate. */
e142c38c 17547 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17548 if (attr != nullptr)
c906108c
SS
17549 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17550 else
9c37b5ae 17551 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17552 arg_type = die_type (child_die, cu);
17553
17554 /* RealView does not mark THIS as const, which the testsuite
17555 expects. GCC marks THIS as const in method definitions,
17556 but not in the class specifications (GCC PR 43053). */
17557 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17558 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17559 {
17560 int is_this = 0;
17561 struct dwarf2_cu *arg_cu = cu;
17562 const char *name = dwarf2_name (child_die, cu);
17563
17564 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17565 if (attr != nullptr)
3ce3b1ba
PA
17566 {
17567 /* If the compiler emits this, use it. */
17568 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17569 is_this = 1;
17570 }
17571 else if (name && strcmp (name, "this") == 0)
17572 /* Function definitions will have the argument names. */
17573 is_this = 1;
17574 else if (name == NULL && iparams == 0)
17575 /* Declarations may not have the names, so like
17576 elsewhere in GDB, assume an artificial first
17577 argument is "this". */
17578 is_this = 1;
17579
17580 if (is_this)
17581 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17582 arg_type, 0);
17583 }
17584
17585 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17586 iparams++;
17587 }
17588 child_die = sibling_die (child_die);
17589 }
17590 }
17591
76c10ea2 17592 return ftype;
c906108c
SS
17593}
17594
f792889a 17595static struct type *
e7c27a73 17596read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17597{
518817b3 17598 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17599 const char *name = NULL;
3c8e0968 17600 struct type *this_type, *target_type;
c906108c 17601
94af9270 17602 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17603 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17604 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17605 set_die_type (die, this_type, cu);
3c8e0968
DE
17606 target_type = die_type (die, cu);
17607 if (target_type != this_type)
17608 TYPE_TARGET_TYPE (this_type) = target_type;
17609 else
17610 {
17611 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17612 spec and cause infinite loops in GDB. */
b98664d3 17613 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17614 "- DIE at %s [in module %s]"),
17615 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17616 TYPE_TARGET_TYPE (this_type) = NULL;
17617 }
f792889a 17618 return this_type;
c906108c
SS
17619}
17620
9b790ce7
UW
17621/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17622 (which may be different from NAME) to the architecture back-end to allow
17623 it to guess the correct format if necessary. */
17624
17625static struct type *
17626dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17627 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17628{
17629 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17630 const struct floatformat **format;
17631 struct type *type;
17632
17633 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17634 if (format)
103a685e 17635 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17636 else
77b7c781 17637 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17638
17639 return type;
17640}
17641
eb77c9df
AB
17642/* Allocate an integer type of size BITS and name NAME. */
17643
17644static struct type *
17645dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17646 int bits, int unsigned_p, const char *name)
17647{
17648 struct type *type;
17649
17650 /* Versions of Intel's C Compiler generate an integer type called "void"
17651 instead of using DW_TAG_unspecified_type. This has been seen on
17652 at least versions 14, 17, and 18. */
35ee2dc2
AB
17653 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17654 && strcmp (name, "void") == 0)
eb77c9df
AB
17655 type = objfile_type (objfile)->builtin_void;
17656 else
17657 type = init_integer_type (objfile, bits, unsigned_p, name);
17658
17659 return type;
17660}
17661
8bdc1658
AB
17662/* Initialise and return a floating point type of size BITS suitable for
17663 use as a component of a complex number. The NAME_HINT is passed through
17664 when initialising the floating point type and is the name of the complex
17665 type.
17666
17667 As DWARF doesn't currently provide an explicit name for the components
17668 of a complex number, but it can be helpful to have these components
17669 named, we try to select a suitable name based on the size of the
17670 component. */
17671static struct type *
17672dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17673 struct objfile *objfile,
103a685e
TT
17674 int bits, const char *name_hint,
17675 enum bfd_endian byte_order)
8bdc1658
AB
17676{
17677 gdbarch *gdbarch = get_objfile_arch (objfile);
17678 struct type *tt = nullptr;
17679
35add35e
AB
17680 /* Try to find a suitable floating point builtin type of size BITS.
17681 We're going to use the name of this type as the name for the complex
17682 target type that we are about to create. */
1db455a7 17683 switch (cu->language)
8bdc1658 17684 {
1db455a7
AB
17685 case language_fortran:
17686 switch (bits)
17687 {
17688 case 32:
17689 tt = builtin_f_type (gdbarch)->builtin_real;
17690 break;
17691 case 64:
17692 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17693 break;
17694 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17695 case 128:
17696 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17697 break;
17698 }
8bdc1658 17699 break;
1db455a7
AB
17700 default:
17701 switch (bits)
17702 {
17703 case 32:
17704 tt = builtin_type (gdbarch)->builtin_float;
17705 break;
17706 case 64:
17707 tt = builtin_type (gdbarch)->builtin_double;
17708 break;
17709 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17710 case 128:
17711 tt = builtin_type (gdbarch)->builtin_long_double;
17712 break;
17713 }
8bdc1658
AB
17714 break;
17715 }
17716
35add35e
AB
17717 /* If the type we found doesn't match the size we were looking for, then
17718 pretend we didn't find a type at all, the complex target type we
17719 create will then be nameless. */
a12e5744 17720 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17721 tt = nullptr;
17722
8bdc1658 17723 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17724 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17725}
17726
c906108c
SS
17727/* Find a representation of a given base type and install
17728 it in the TYPE field of the die. */
17729
f792889a 17730static struct type *
e7c27a73 17731read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17732{
518817b3 17733 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17734 struct type *type;
17735 struct attribute *attr;
19f392bc 17736 int encoding = 0, bits = 0;
15d034d0 17737 const char *name;
34877895 17738 gdbarch *arch;
c906108c 17739
e142c38c 17740 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17741 if (attr != nullptr)
34877895 17742 encoding = DW_UNSND (attr);
e142c38c 17743 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17744 if (attr != nullptr)
34877895 17745 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17746 name = dwarf2_name (die, cu);
6ccb9162 17747 if (!name)
34877895 17748 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17749
17750 arch = get_objfile_arch (objfile);
17751 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17752
34877895
PJ
17753 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17754 if (attr)
103a685e
TT
17755 {
17756 int endianity = DW_UNSND (attr);
17757
17758 switch (endianity)
17759 {
17760 case DW_END_big:
17761 byte_order = BFD_ENDIAN_BIG;
17762 break;
17763 case DW_END_little:
17764 byte_order = BFD_ENDIAN_LITTLE;
17765 break;
17766 default:
17767 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17768 break;
17769 }
17770 }
6ccb9162
UW
17771
17772 switch (encoding)
c906108c 17773 {
6ccb9162
UW
17774 case DW_ATE_address:
17775 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17776 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17777 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17778 break;
17779 case DW_ATE_boolean:
19f392bc 17780 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17781 break;
17782 case DW_ATE_complex_float:
103a685e
TT
17783 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17784 byte_order);
19f392bc 17785 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17786 break;
17787 case DW_ATE_decimal_float:
19f392bc 17788 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17789 break;
17790 case DW_ATE_float:
103a685e 17791 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17792 break;
17793 case DW_ATE_signed:
eb77c9df 17794 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17795 break;
17796 case DW_ATE_unsigned:
3b2b8fea
TT
17797 if (cu->language == language_fortran
17798 && name
61012eef 17799 && startswith (name, "character("))
19f392bc
UW
17800 type = init_character_type (objfile, bits, 1, name);
17801 else
eb77c9df 17802 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17803 break;
17804 case DW_ATE_signed_char:
6e70227d 17805 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17806 || cu->language == language_pascal
17807 || cu->language == language_fortran)
19f392bc
UW
17808 type = init_character_type (objfile, bits, 0, name);
17809 else
eb77c9df 17810 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17811 break;
17812 case DW_ATE_unsigned_char:
868a0084 17813 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17814 || cu->language == language_pascal
c44af4eb
TT
17815 || cu->language == language_fortran
17816 || cu->language == language_rust)
19f392bc
UW
17817 type = init_character_type (objfile, bits, 1, name);
17818 else
eb77c9df 17819 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17820 break;
75079b2b 17821 case DW_ATE_UTF:
53e710ac 17822 {
53e710ac
PA
17823 if (bits == 16)
17824 type = builtin_type (arch)->builtin_char16;
17825 else if (bits == 32)
17826 type = builtin_type (arch)->builtin_char32;
17827 else
17828 {
b98664d3 17829 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17830 bits);
eb77c9df 17831 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17832 }
17833 return set_die_type (die, type, cu);
17834 }
75079b2b
TT
17835 break;
17836
6ccb9162 17837 default:
b98664d3 17838 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17839 dwarf_type_encoding_name (encoding));
77b7c781 17840 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17841 break;
c906108c 17842 }
6ccb9162 17843
0114d602 17844 if (name && strcmp (name, "char") == 0)
876cecd0 17845 TYPE_NOSIGN (type) = 1;
0114d602 17846
2b4424c3
TT
17847 maybe_set_alignment (cu, die, type);
17848
103a685e 17849 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17850
f792889a 17851 return set_die_type (die, type, cu);
c906108c
SS
17852}
17853
80180f79
SA
17854/* Parse dwarf attribute if it's a block, reference or constant and put the
17855 resulting value of the attribute into struct bound_prop.
17856 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17857
17858static int
17859attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17860 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17861 struct type *default_type)
80180f79
SA
17862{
17863 struct dwarf2_property_baton *baton;
518817b3
SM
17864 struct obstack *obstack
17865 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17866
9a49df9d
AB
17867 gdb_assert (default_type != NULL);
17868
80180f79
SA
17869 if (attr == NULL || prop == NULL)
17870 return 0;
17871
17872 if (attr_form_is_block (attr))
17873 {
8d749320 17874 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17875 baton->property_type = default_type;
80180f79
SA
17876 baton->locexpr.per_cu = cu->per_cu;
17877 baton->locexpr.size = DW_BLOCK (attr)->size;
17878 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17879 switch (attr->name)
17880 {
17881 case DW_AT_string_length:
17882 baton->locexpr.is_reference = true;
17883 break;
17884 default:
17885 baton->locexpr.is_reference = false;
17886 break;
17887 }
80180f79
SA
17888 prop->data.baton = baton;
17889 prop->kind = PROP_LOCEXPR;
17890 gdb_assert (prop->data.baton != NULL);
17891 }
17892 else if (attr_form_is_ref (attr))
17893 {
17894 struct dwarf2_cu *target_cu = cu;
17895 struct die_info *target_die;
17896 struct attribute *target_attr;
17897
17898 target_die = follow_die_ref (die, attr, &target_cu);
17899 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17900 if (target_attr == NULL)
17901 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17902 target_cu);
80180f79
SA
17903 if (target_attr == NULL)
17904 return 0;
17905
df25ebbd 17906 switch (target_attr->name)
80180f79 17907 {
df25ebbd
JB
17908 case DW_AT_location:
17909 if (attr_form_is_section_offset (target_attr))
17910 {
8d749320 17911 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17912 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17913 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17914 prop->data.baton = baton;
17915 prop->kind = PROP_LOCLIST;
17916 gdb_assert (prop->data.baton != NULL);
17917 }
17918 else if (attr_form_is_block (target_attr))
17919 {
8d749320 17920 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17921 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17922 baton->locexpr.per_cu = cu->per_cu;
17923 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17924 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17925 baton->locexpr.is_reference = true;
df25ebbd
JB
17926 prop->data.baton = baton;
17927 prop->kind = PROP_LOCEXPR;
17928 gdb_assert (prop->data.baton != NULL);
17929 }
17930 else
17931 {
17932 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17933 "dynamic property");
17934 return 0;
17935 }
17936 break;
17937 case DW_AT_data_member_location:
17938 {
17939 LONGEST offset;
17940
17941 if (!handle_data_member_location (target_die, target_cu,
17942 &offset))
17943 return 0;
17944
8d749320 17945 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17946 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17947 target_cu);
df25ebbd
JB
17948 baton->offset_info.offset = offset;
17949 baton->offset_info.type = die_type (target_die, target_cu);
17950 prop->data.baton = baton;
17951 prop->kind = PROP_ADDR_OFFSET;
17952 break;
17953 }
80180f79
SA
17954 }
17955 }
17956 else if (attr_form_is_constant (attr))
17957 {
17958 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17959 prop->kind = PROP_CONST;
17960 }
17961 else
17962 {
17963 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17964 dwarf2_name (die, cu));
17965 return 0;
17966 }
17967
17968 return 1;
17969}
17970
11a8b164
AB
17971/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17972 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
17973
17974static struct type *
11a8b164
AB
17975dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17976 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
17977{
17978 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
17979 struct type *int_type;
17980
17981 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17982#define TRY_TYPE(F) \
17983 int_type = (unsigned_p \
17984 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17985 : objfile_type (objfile)->builtin_ ## F); \
17986 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17987 return int_type
17988
17989 TRY_TYPE (char);
17990 TRY_TYPE (short);
17991 TRY_TYPE (int);
17992 TRY_TYPE (long);
17993 TRY_TYPE (long_long);
17994
17995#undef TRY_TYPE
17996
17997 gdb_assert_not_reached ("unable to find suitable integer type");
17998}
17999
11a8b164
AB
18000/* Find an integer type the same size as the address size given in the
18001 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18002 is unsigned or not. */
18003
18004static struct type *
18005dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18006 bool unsigned_p)
18007{
18008 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18009 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18010}
18011
b86352cf
AB
18012/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18013 present (which is valid) then compute the default type based on the
18014 compilation units address size. */
18015
18016static struct type *
18017read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18018{
18019 struct type *index_type = die_type (die, cu);
18020
18021 /* Dwarf-2 specifications explicitly allows to create subrange types
18022 without specifying a base type.
18023 In that case, the base type must be set to the type of
18024 the lower bound, upper bound or count, in that order, if any of these
18025 three attributes references an object that has a type.
18026 If no base type is found, the Dwarf-2 specifications say that
18027 a signed integer type of size equal to the size of an address should
18028 be used.
18029 For the following C code: `extern char gdb_int [];'
18030 GCC produces an empty range DIE.
18031 FIXME: muller/2010-05-28: Possible references to object for low bound,
18032 high bound or count are not yet handled by this code. */
18033 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 18034 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
18035
18036 return index_type;
18037}
18038
a02abb62
JB
18039/* Read the given DW_AT_subrange DIE. */
18040
f792889a 18041static struct type *
a02abb62
JB
18042read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18043{
4c9ad8c2 18044 struct type *base_type, *orig_base_type;
a02abb62
JB
18045 struct type *range_type;
18046 struct attribute *attr;
729efb13 18047 struct dynamic_prop low, high;
4fae6e18 18048 int low_default_is_valid;
c451ebe5 18049 int high_bound_is_count = 0;
15d034d0 18050 const char *name;
d359392f 18051 ULONGEST negative_mask;
e77813c8 18052
b86352cf
AB
18053 orig_base_type = read_subrange_index_type (die, cu);
18054
4c9ad8c2
TT
18055 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18056 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18057 creating the range type, but we use the result of check_typedef
18058 when examining properties of the type. */
18059 base_type = check_typedef (orig_base_type);
a02abb62 18060
7e314c57
JK
18061 /* The die_type call above may have already set the type for this DIE. */
18062 range_type = get_die_type (die, cu);
18063 if (range_type)
18064 return range_type;
18065
729efb13
SA
18066 low.kind = PROP_CONST;
18067 high.kind = PROP_CONST;
18068 high.data.const_val = 0;
18069
4fae6e18
JK
18070 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18071 omitting DW_AT_lower_bound. */
18072 switch (cu->language)
6e70227d 18073 {
4fae6e18
JK
18074 case language_c:
18075 case language_cplus:
729efb13 18076 low.data.const_val = 0;
4fae6e18
JK
18077 low_default_is_valid = 1;
18078 break;
18079 case language_fortran:
729efb13 18080 low.data.const_val = 1;
4fae6e18
JK
18081 low_default_is_valid = 1;
18082 break;
18083 case language_d:
4fae6e18 18084 case language_objc:
c44af4eb 18085 case language_rust:
729efb13 18086 low.data.const_val = 0;
4fae6e18
JK
18087 low_default_is_valid = (cu->header.version >= 4);
18088 break;
18089 case language_ada:
18090 case language_m2:
18091 case language_pascal:
729efb13 18092 low.data.const_val = 1;
4fae6e18
JK
18093 low_default_is_valid = (cu->header.version >= 4);
18094 break;
18095 default:
729efb13 18096 low.data.const_val = 0;
4fae6e18
JK
18097 low_default_is_valid = 0;
18098 break;
a02abb62
JB
18099 }
18100
e142c38c 18101 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18102 if (attr != nullptr)
9a49df9d 18103 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18104 else if (!low_default_is_valid)
b98664d3 18105 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18106 "- DIE at %s [in module %s]"),
18107 sect_offset_str (die->sect_off),
518817b3 18108 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18109
506f5c41
TV
18110 struct attribute *attr_ub, *attr_count;
18111 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18112 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18113 {
506f5c41 18114 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18115 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18116 {
c451ebe5
SA
18117 /* If bounds are constant do the final calculation here. */
18118 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18119 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18120 else
18121 high_bound_is_count = 1;
c2ff108b 18122 }
506f5c41
TV
18123 else
18124 {
18125 if (attr_ub != NULL)
18126 complaint (_("Unresolved DW_AT_upper_bound "
18127 "- DIE at %s [in module %s]"),
18128 sect_offset_str (die->sect_off),
18129 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18130 if (attr_count != NULL)
18131 complaint (_("Unresolved DW_AT_count "
18132 "- DIE at %s [in module %s]"),
18133 sect_offset_str (die->sect_off),
18134 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18135 }
e77813c8 18136 }
a02abb62 18137
4e962e74
TT
18138 LONGEST bias = 0;
18139 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18140 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18141 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18142
dbb9c2b1
JB
18143 /* Normally, the DWARF producers are expected to use a signed
18144 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18145 But this is unfortunately not always the case, as witnessed
18146 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18147 is used instead. To work around that ambiguity, we treat
18148 the bounds as signed, and thus sign-extend their values, when
18149 the base type is signed. */
6e70227d 18150 negative_mask =
d359392f 18151 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18152 if (low.kind == PROP_CONST
18153 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18154 low.data.const_val |= negative_mask;
18155 if (high.kind == PROP_CONST
18156 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18157 high.data.const_val |= negative_mask;
43bbcdc2 18158
5bbd8269
AB
18159 /* Check for bit and byte strides. */
18160 struct dynamic_prop byte_stride_prop;
18161 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18162 if (attr_byte_stride != nullptr)
18163 {
18164 struct type *prop_type
18165 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18166 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18167 prop_type);
18168 }
18169
18170 struct dynamic_prop bit_stride_prop;
18171 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18172 if (attr_bit_stride != nullptr)
18173 {
18174 /* It only makes sense to have either a bit or byte stride. */
18175 if (attr_byte_stride != nullptr)
18176 {
18177 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18178 "- DIE at %s [in module %s]"),
18179 sect_offset_str (die->sect_off),
18180 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18181 attr_bit_stride = nullptr;
18182 }
18183 else
18184 {
18185 struct type *prop_type
18186 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18187 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18188 prop_type);
18189 }
18190 }
18191
18192 if (attr_byte_stride != nullptr
18193 || attr_bit_stride != nullptr)
18194 {
18195 bool byte_stride_p = (attr_byte_stride != nullptr);
18196 struct dynamic_prop *stride
18197 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18198
18199 range_type
18200 = create_range_type_with_stride (NULL, orig_base_type, &low,
18201 &high, bias, stride, byte_stride_p);
18202 }
18203 else
18204 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18205
c451ebe5
SA
18206 if (high_bound_is_count)
18207 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18208
c2ff108b
JK
18209 /* Ada expects an empty array on no boundary attributes. */
18210 if (attr == NULL && cu->language != language_ada)
729efb13 18211 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18212
39cbfefa
DJ
18213 name = dwarf2_name (die, cu);
18214 if (name)
18215 TYPE_NAME (range_type) = name;
6e70227d 18216
e142c38c 18217 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18218 if (attr != nullptr)
a02abb62
JB
18219 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18220
2b4424c3
TT
18221 maybe_set_alignment (cu, die, range_type);
18222
7e314c57
JK
18223 set_die_type (die, range_type, cu);
18224
18225 /* set_die_type should be already done. */
b4ba55a1
JB
18226 set_descriptive_type (range_type, die, cu);
18227
7e314c57 18228 return range_type;
a02abb62 18229}
6e70227d 18230
f792889a 18231static struct type *
81a17f79
JB
18232read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18233{
18234 struct type *type;
81a17f79 18235
518817b3
SM
18236 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18237 NULL);
0114d602 18238 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18239
74a2f8ff 18240 /* In Ada, an unspecified type is typically used when the description
85102364 18241 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18242 such a type, we treat it as a stub, and try to resolve it later on,
18243 when needed. */
18244 if (cu->language == language_ada)
18245 TYPE_STUB (type) = 1;
18246
f792889a 18247 return set_die_type (die, type, cu);
81a17f79 18248}
a02abb62 18249
639d11d3
DC
18250/* Read a single die and all its descendents. Set the die's sibling
18251 field to NULL; set other fields in the die correctly, and set all
18252 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18253 location of the info_ptr after reading all of those dies. PARENT
18254 is the parent of the die in question. */
18255
18256static struct die_info *
dee91e82 18257read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18258 const gdb_byte *info_ptr,
18259 const gdb_byte **new_info_ptr,
dee91e82 18260 struct die_info *parent)
639d11d3
DC
18261{
18262 struct die_info *die;
d521ce57 18263 const gdb_byte *cur_ptr;
639d11d3
DC
18264 int has_children;
18265
bf6af496 18266 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18267 if (die == NULL)
18268 {
18269 *new_info_ptr = cur_ptr;
18270 return NULL;
18271 }
93311388 18272 store_in_ref_table (die, reader->cu);
639d11d3
DC
18273
18274 if (has_children)
bf6af496 18275 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18276 else
18277 {
18278 die->child = NULL;
18279 *new_info_ptr = cur_ptr;
18280 }
18281
18282 die->sibling = NULL;
18283 die->parent = parent;
18284 return die;
18285}
18286
18287/* Read a die, all of its descendents, and all of its siblings; set
18288 all of the fields of all of the dies correctly. Arguments are as
18289 in read_die_and_children. */
18290
18291static struct die_info *
bf6af496 18292read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18293 const gdb_byte *info_ptr,
18294 const gdb_byte **new_info_ptr,
bf6af496 18295 struct die_info *parent)
639d11d3
DC
18296{
18297 struct die_info *first_die, *last_sibling;
d521ce57 18298 const gdb_byte *cur_ptr;
639d11d3 18299
c906108c 18300 cur_ptr = info_ptr;
639d11d3
DC
18301 first_die = last_sibling = NULL;
18302
18303 while (1)
c906108c 18304 {
639d11d3 18305 struct die_info *die
dee91e82 18306 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18307
1d325ec1 18308 if (die == NULL)
c906108c 18309 {
639d11d3
DC
18310 *new_info_ptr = cur_ptr;
18311 return first_die;
c906108c 18312 }
1d325ec1
DJ
18313
18314 if (!first_die)
18315 first_die = die;
c906108c 18316 else
1d325ec1
DJ
18317 last_sibling->sibling = die;
18318
18319 last_sibling = die;
c906108c 18320 }
c906108c
SS
18321}
18322
bf6af496
DE
18323/* Read a die, all of its descendents, and all of its siblings; set
18324 all of the fields of all of the dies correctly. Arguments are as
18325 in read_die_and_children.
18326 This the main entry point for reading a DIE and all its children. */
18327
18328static struct die_info *
18329read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18330 const gdb_byte *info_ptr,
18331 const gdb_byte **new_info_ptr,
bf6af496
DE
18332 struct die_info *parent)
18333{
18334 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18335 new_info_ptr, parent);
18336
b4f54984 18337 if (dwarf_die_debug)
bf6af496
DE
18338 {
18339 fprintf_unfiltered (gdb_stdlog,
18340 "Read die from %s@0x%x of %s:\n",
a32a8923 18341 get_section_name (reader->die_section),
bf6af496
DE
18342 (unsigned) (info_ptr - reader->die_section->buffer),
18343 bfd_get_filename (reader->abfd));
b4f54984 18344 dump_die (die, dwarf_die_debug);
bf6af496
DE
18345 }
18346
18347 return die;
18348}
18349
3019eac3
DE
18350/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18351 attributes.
18352 The caller is responsible for filling in the extra attributes
18353 and updating (*DIEP)->num_attrs.
18354 Set DIEP to point to a newly allocated die with its information,
18355 except for its child, sibling, and parent fields.
18356 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18357
d521ce57 18358static const gdb_byte *
3019eac3 18359read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18360 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18361 int *has_children, int num_extra_attrs)
93311388 18362{
b64f50a1 18363 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18364 struct abbrev_info *abbrev;
18365 struct die_info *die;
18366 struct dwarf2_cu *cu = reader->cu;
18367 bfd *abfd = reader->abfd;
18368
9c541725 18369 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18370 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18371 info_ptr += bytes_read;
18372 if (!abbrev_number)
18373 {
18374 *diep = NULL;
18375 *has_children = 0;
18376 return info_ptr;
18377 }
18378
685af9cd 18379 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18380 if (!abbrev)
348e048f
DE
18381 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18382 abbrev_number,
18383 bfd_get_filename (abfd));
18384
3019eac3 18385 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18386 die->sect_off = sect_off;
93311388
DE
18387 die->tag = abbrev->tag;
18388 die->abbrev = abbrev_number;
18389
3019eac3
DE
18390 /* Make the result usable.
18391 The caller needs to update num_attrs after adding the extra
18392 attributes. */
93311388
DE
18393 die->num_attrs = abbrev->num_attrs;
18394
18395 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18396 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18397 info_ptr);
93311388
DE
18398
18399 *diep = die;
18400 *has_children = abbrev->has_children;
18401 return info_ptr;
18402}
18403
3019eac3
DE
18404/* Read a die and all its attributes.
18405 Set DIEP to point to a newly allocated die with its information,
18406 except for its child, sibling, and parent fields.
18407 Set HAS_CHILDREN to tell whether the die has children or not. */
18408
d521ce57 18409static const gdb_byte *
3019eac3 18410read_full_die (const struct die_reader_specs *reader,
d521ce57 18411 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18412 int *has_children)
18413{
d521ce57 18414 const gdb_byte *result;
bf6af496
DE
18415
18416 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18417
b4f54984 18418 if (dwarf_die_debug)
bf6af496
DE
18419 {
18420 fprintf_unfiltered (gdb_stdlog,
18421 "Read die from %s@0x%x of %s:\n",
a32a8923 18422 get_section_name (reader->die_section),
bf6af496
DE
18423 (unsigned) (info_ptr - reader->die_section->buffer),
18424 bfd_get_filename (reader->abfd));
b4f54984 18425 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18426 }
18427
18428 return result;
3019eac3 18429}
433df2d4
DE
18430\f
18431/* Abbreviation tables.
3019eac3 18432
433df2d4 18433 In DWARF version 2, the description of the debugging information is
c906108c
SS
18434 stored in a separate .debug_abbrev section. Before we read any
18435 dies from a section we read in all abbreviations and install them
433df2d4
DE
18436 in a hash table. */
18437
18438/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18439
685af9cd
TT
18440struct abbrev_info *
18441abbrev_table::alloc_abbrev ()
433df2d4
DE
18442{
18443 struct abbrev_info *abbrev;
18444
685af9cd 18445 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18446 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18447
433df2d4
DE
18448 return abbrev;
18449}
18450
18451/* Add an abbreviation to the table. */
c906108c 18452
685af9cd
TT
18453void
18454abbrev_table::add_abbrev (unsigned int abbrev_number,
18455 struct abbrev_info *abbrev)
433df2d4
DE
18456{
18457 unsigned int hash_number;
18458
18459 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18460 abbrev->next = m_abbrevs[hash_number];
18461 m_abbrevs[hash_number] = abbrev;
433df2d4 18462}
dee91e82 18463
433df2d4
DE
18464/* Look up an abbrev in the table.
18465 Returns NULL if the abbrev is not found. */
18466
685af9cd
TT
18467struct abbrev_info *
18468abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18469{
433df2d4
DE
18470 unsigned int hash_number;
18471 struct abbrev_info *abbrev;
18472
18473 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18474 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18475
18476 while (abbrev)
18477 {
18478 if (abbrev->number == abbrev_number)
18479 return abbrev;
18480 abbrev = abbrev->next;
18481 }
18482 return NULL;
18483}
18484
18485/* Read in an abbrev table. */
18486
685af9cd 18487static abbrev_table_up
ed2dc618
SM
18488abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18489 struct dwarf2_section_info *section,
9c541725 18490 sect_offset sect_off)
433df2d4
DE
18491{
18492 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18493 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18494 const gdb_byte *abbrev_ptr;
c906108c
SS
18495 struct abbrev_info *cur_abbrev;
18496 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18497 unsigned int abbrev_form;
f3dd6933
DJ
18498 struct attr_abbrev *cur_attrs;
18499 unsigned int allocated_attrs;
c906108c 18500
685af9cd 18501 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18502
433df2d4 18503 dwarf2_read_section (objfile, section);
9c541725 18504 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18505 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18506 abbrev_ptr += bytes_read;
18507
f3dd6933 18508 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18509 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18510
0963b4bd 18511 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18512 while (abbrev_number)
18513 {
685af9cd 18514 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18515
18516 /* read in abbrev header */
18517 cur_abbrev->number = abbrev_number;
aead7601
SM
18518 cur_abbrev->tag
18519 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18520 abbrev_ptr += bytes_read;
18521 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18522 abbrev_ptr += 1;
18523
18524 /* now read in declarations */
22d2f3ab 18525 for (;;)
c906108c 18526 {
43988095
JK
18527 LONGEST implicit_const;
18528
22d2f3ab
JK
18529 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18530 abbrev_ptr += bytes_read;
18531 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18532 abbrev_ptr += bytes_read;
43988095
JK
18533 if (abbrev_form == DW_FORM_implicit_const)
18534 {
18535 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18536 &bytes_read);
18537 abbrev_ptr += bytes_read;
18538 }
18539 else
18540 {
18541 /* Initialize it due to a false compiler warning. */
18542 implicit_const = -1;
18543 }
22d2f3ab
JK
18544
18545 if (abbrev_name == 0)
18546 break;
18547
f3dd6933 18548 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18549 {
f3dd6933
DJ
18550 allocated_attrs += ATTR_ALLOC_CHUNK;
18551 cur_attrs
224c3ddb 18552 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18553 }
ae038cb0 18554
aead7601
SM
18555 cur_attrs[cur_abbrev->num_attrs].name
18556 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18557 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18558 = (enum dwarf_form) abbrev_form;
43988095 18559 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18560 ++cur_abbrev->num_attrs;
c906108c
SS
18561 }
18562
8d749320
SM
18563 cur_abbrev->attrs =
18564 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18565 cur_abbrev->num_attrs);
f3dd6933
DJ
18566 memcpy (cur_abbrev->attrs, cur_attrs,
18567 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18568
685af9cd 18569 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18570
18571 /* Get next abbreviation.
18572 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18573 always properly terminated with an abbrev number of 0.
18574 Exit loop if we encounter an abbreviation which we have
18575 already read (which means we are about to read the abbreviations
18576 for the next compile unit) or if the end of the abbreviation
18577 table is reached. */
433df2d4 18578 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18579 break;
18580 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18581 abbrev_ptr += bytes_read;
685af9cd 18582 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18583 break;
18584 }
f3dd6933
DJ
18585
18586 xfree (cur_attrs);
433df2d4 18587 return abbrev_table;
c906108c
SS
18588}
18589
72bf9492
DJ
18590/* Returns nonzero if TAG represents a type that we might generate a partial
18591 symbol for. */
18592
18593static int
18594is_type_tag_for_partial (int tag)
18595{
18596 switch (tag)
18597 {
18598#if 0
18599 /* Some types that would be reasonable to generate partial symbols for,
18600 that we don't at present. */
18601 case DW_TAG_array_type:
18602 case DW_TAG_file_type:
18603 case DW_TAG_ptr_to_member_type:
18604 case DW_TAG_set_type:
18605 case DW_TAG_string_type:
18606 case DW_TAG_subroutine_type:
18607#endif
18608 case DW_TAG_base_type:
18609 case DW_TAG_class_type:
680b30c7 18610 case DW_TAG_interface_type:
72bf9492
DJ
18611 case DW_TAG_enumeration_type:
18612 case DW_TAG_structure_type:
18613 case DW_TAG_subrange_type:
18614 case DW_TAG_typedef:
18615 case DW_TAG_union_type:
18616 return 1;
18617 default:
18618 return 0;
18619 }
18620}
18621
18622/* Load all DIEs that are interesting for partial symbols into memory. */
18623
18624static struct partial_die_info *
dee91e82 18625load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18626 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18627{
dee91e82 18628 struct dwarf2_cu *cu = reader->cu;
518817b3 18629 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18630 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18631 unsigned int bytes_read;
5afb4e99 18632 unsigned int load_all = 0;
72bf9492
DJ
18633 int nesting_level = 1;
18634
18635 parent_die = NULL;
18636 last_die = NULL;
18637
7adf1e79
DE
18638 gdb_assert (cu->per_cu != NULL);
18639 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18640 load_all = 1;
18641
72bf9492
DJ
18642 cu->partial_dies
18643 = htab_create_alloc_ex (cu->header.length / 12,
18644 partial_die_hash,
18645 partial_die_eq,
18646 NULL,
18647 &cu->comp_unit_obstack,
18648 hashtab_obstack_allocate,
18649 dummy_obstack_deallocate);
18650
72bf9492
DJ
18651 while (1)
18652 {
685af9cd 18653 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18654
18655 /* A NULL abbrev means the end of a series of children. */
18656 if (abbrev == NULL)
18657 {
18658 if (--nesting_level == 0)
cd9983dd
YQ
18659 return first_die;
18660
72bf9492
DJ
18661 info_ptr += bytes_read;
18662 last_die = parent_die;
18663 parent_die = parent_die->die_parent;
18664 continue;
18665 }
18666
98bfdba5
PA
18667 /* Check for template arguments. We never save these; if
18668 they're seen, we just mark the parent, and go on our way. */
18669 if (parent_die != NULL
18670 && cu->language == language_cplus
18671 && (abbrev->tag == DW_TAG_template_type_param
18672 || abbrev->tag == DW_TAG_template_value_param))
18673 {
18674 parent_die->has_template_arguments = 1;
18675
18676 if (!load_all)
18677 {
18678 /* We don't need a partial DIE for the template argument. */
dee91e82 18679 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18680 continue;
18681 }
18682 }
18683
0d99eb77 18684 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18685 Skip their other children. */
18686 if (!load_all
18687 && cu->language == language_cplus
18688 && parent_die != NULL
18689 && parent_die->tag == DW_TAG_subprogram)
18690 {
dee91e82 18691 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18692 continue;
18693 }
18694
5afb4e99
DJ
18695 /* Check whether this DIE is interesting enough to save. Normally
18696 we would not be interested in members here, but there may be
18697 later variables referencing them via DW_AT_specification (for
18698 static members). */
18699 if (!load_all
18700 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18701 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18702 && abbrev->tag != DW_TAG_enumerator
18703 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18704 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18705 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18706 && abbrev->tag != DW_TAG_variable
5afb4e99 18707 && abbrev->tag != DW_TAG_namespace
f55ee35c 18708 && abbrev->tag != DW_TAG_module
95554aad 18709 && abbrev->tag != DW_TAG_member
74921315
KS
18710 && abbrev->tag != DW_TAG_imported_unit
18711 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18712 {
18713 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18714 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18715 continue;
18716 }
18717
6f06d47b
YQ
18718 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18719 abbrev);
cd9983dd 18720
48fbe735 18721 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18722
18723 /* This two-pass algorithm for processing partial symbols has a
18724 high cost in cache pressure. Thus, handle some simple cases
18725 here which cover the majority of C partial symbols. DIEs
18726 which neither have specification tags in them, nor could have
18727 specification tags elsewhere pointing at them, can simply be
18728 processed and discarded.
18729
18730 This segment is also optional; scan_partial_symbols and
18731 add_partial_symbol will handle these DIEs if we chain
18732 them in normally. When compilers which do not emit large
18733 quantities of duplicate debug information are more common,
18734 this code can probably be removed. */
18735
18736 /* Any complete simple types at the top level (pretty much all
18737 of them, for a language without namespaces), can be processed
18738 directly. */
18739 if (parent_die == NULL
cd9983dd
YQ
18740 && pdi.has_specification == 0
18741 && pdi.is_declaration == 0
18742 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18743 || pdi.tag == DW_TAG_base_type
18744 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18745 {
cd9983dd 18746 if (building_psymtab && pdi.name != NULL)
31edb802 18747 add_psymbol_to_list (pdi.name, false,
79748972 18748 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18749 psymbol_placement::STATIC,
1762568f 18750 0, cu->language, objfile);
cd9983dd 18751 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18752 continue;
18753 }
18754
d8228535
JK
18755 /* The exception for DW_TAG_typedef with has_children above is
18756 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18757 type_name_or_error will error on such types later.
d8228535
JK
18758
18759 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18760 it could not find the child DIEs referenced later, this is checked
18761 above. In correct DWARF DW_TAG_typedef should have no children. */
18762
cd9983dd 18763 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18764 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18765 "- DIE at %s [in module %s]"),
cd9983dd 18766 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18767
72bf9492
DJ
18768 /* If we're at the second level, and we're an enumerator, and
18769 our parent has no specification (meaning possibly lives in a
18770 namespace elsewhere), then we can add the partial symbol now
18771 instead of queueing it. */
cd9983dd 18772 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18773 && parent_die != NULL
18774 && parent_die->die_parent == NULL
18775 && parent_die->tag == DW_TAG_enumeration_type
18776 && parent_die->has_specification == 0)
18777 {
cd9983dd 18778 if (pdi.name == NULL)
b98664d3 18779 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18780 else if (building_psymtab)
31edb802 18781 add_psymbol_to_list (pdi.name, false,
79748972 18782 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18783 cu->language == language_cplus
75aedd27
TT
18784 ? psymbol_placement::GLOBAL
18785 : psymbol_placement::STATIC,
1762568f 18786 0, cu->language, objfile);
72bf9492 18787
cd9983dd 18788 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18789 continue;
18790 }
18791
cd9983dd 18792 struct partial_die_info *part_die
6f06d47b 18793 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18794
72bf9492
DJ
18795 /* We'll save this DIE so link it in. */
18796 part_die->die_parent = parent_die;
18797 part_die->die_sibling = NULL;
18798 part_die->die_child = NULL;
18799
18800 if (last_die && last_die == parent_die)
18801 last_die->die_child = part_die;
18802 else if (last_die)
18803 last_die->die_sibling = part_die;
18804
18805 last_die = part_die;
18806
18807 if (first_die == NULL)
18808 first_die = part_die;
18809
18810 /* Maybe add the DIE to the hash table. Not all DIEs that we
18811 find interesting need to be in the hash table, because we
18812 also have the parent/sibling/child chains; only those that we
18813 might refer to by offset later during partial symbol reading.
18814
18815 For now this means things that might have be the target of a
18816 DW_AT_specification, DW_AT_abstract_origin, or
18817 DW_AT_extension. DW_AT_extension will refer only to
18818 namespaces; DW_AT_abstract_origin refers to functions (and
18819 many things under the function DIE, but we do not recurse
18820 into function DIEs during partial symbol reading) and
18821 possibly variables as well; DW_AT_specification refers to
18822 declarations. Declarations ought to have the DW_AT_declaration
18823 flag. It happens that GCC forgets to put it in sometimes, but
18824 only for functions, not for types.
18825
18826 Adding more things than necessary to the hash table is harmless
18827 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18828 wasted time in find_partial_die, when we reread the compilation
18829 unit with load_all_dies set. */
72bf9492 18830
5afb4e99 18831 if (load_all
72929c62 18832 || abbrev->tag == DW_TAG_constant
5afb4e99 18833 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18834 || abbrev->tag == DW_TAG_variable
18835 || abbrev->tag == DW_TAG_namespace
18836 || part_die->is_declaration)
18837 {
18838 void **slot;
18839
18840 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18841 to_underlying (part_die->sect_off),
18842 INSERT);
72bf9492
DJ
18843 *slot = part_die;
18844 }
18845
72bf9492 18846 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18847 we have no reason to follow the children of structures; for other
98bfdba5
PA
18848 languages we have to, so that we can get at method physnames
18849 to infer fully qualified class names, for DW_AT_specification,
18850 and for C++ template arguments. For C++, we also look one level
18851 inside functions to find template arguments (if the name of the
18852 function does not already contain the template arguments).
bc30ff58 18853
0a4b0913
AB
18854 For Ada and Fortran, we need to scan the children of subprograms
18855 and lexical blocks as well because these languages allow the
18856 definition of nested entities that could be interesting for the
18857 debugger, such as nested subprograms for instance. */
72bf9492 18858 if (last_die->has_children
5afb4e99
DJ
18859 && (load_all
18860 || last_die->tag == DW_TAG_namespace
f55ee35c 18861 || last_die->tag == DW_TAG_module
72bf9492 18862 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18863 || (cu->language == language_cplus
18864 && last_die->tag == DW_TAG_subprogram
18865 && (last_die->name == NULL
18866 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18867 || (cu->language != language_c
18868 && (last_die->tag == DW_TAG_class_type
680b30c7 18869 || last_die->tag == DW_TAG_interface_type
72bf9492 18870 || last_die->tag == DW_TAG_structure_type
bc30ff58 18871 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18872 || ((cu->language == language_ada
18873 || cu->language == language_fortran)
bc30ff58
JB
18874 && (last_die->tag == DW_TAG_subprogram
18875 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18876 {
18877 nesting_level++;
18878 parent_die = last_die;
18879 continue;
18880 }
18881
18882 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18883 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18884
18885 /* Back to the top, do it again. */
18886 }
18887}
18888
6f06d47b
YQ
18889partial_die_info::partial_die_info (sect_offset sect_off_,
18890 struct abbrev_info *abbrev)
18891 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18892{
18893}
18894
35cc7ed7
YQ
18895/* Read a minimal amount of information into the minimal die structure.
18896 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18897
48fbe735
YQ
18898const gdb_byte *
18899partial_die_info::read (const struct die_reader_specs *reader,
18900 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18901{
dee91e82 18902 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18903 struct dwarf2_per_objfile *dwarf2_per_objfile
18904 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18905 unsigned int i;
c5aa993b 18906 int has_low_pc_attr = 0;
c906108c 18907 int has_high_pc_attr = 0;
91da1414 18908 int high_pc_relative = 0;
c906108c 18909
fd0a254f 18910 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18911 {
48fbe735
YQ
18912 struct attribute attr;
18913
fd0a254f 18914 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18915
18916 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18917 partial symbol table. */
c906108c
SS
18918 switch (attr.name)
18919 {
18920 case DW_AT_name:
48fbe735 18921 switch (tag)
71c25dea
TT
18922 {
18923 case DW_TAG_compile_unit:
95554aad 18924 case DW_TAG_partial_unit:
348e048f 18925 case DW_TAG_type_unit:
71c25dea
TT
18926 /* Compilation units have a DW_AT_name that is a filename, not
18927 a source language identifier. */
18928 case DW_TAG_enumeration_type:
18929 case DW_TAG_enumerator:
18930 /* These tags always have simple identifiers already; no need
18931 to canonicalize them. */
48fbe735 18932 name = DW_STRING (&attr);
71c25dea
TT
18933 break;
18934 default:
48fbe735
YQ
18935 {
18936 struct objfile *objfile = dwarf2_per_objfile->objfile;
18937
18938 name
18939 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18940 &objfile->per_bfd->storage_obstack);
18941 }
71c25dea
TT
18942 break;
18943 }
c906108c 18944 break;
31ef98ae 18945 case DW_AT_linkage_name:
c906108c 18946 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18947 /* Note that both forms of linkage name might appear. We
18948 assume they will be the same, and we only store the last
18949 one we see. */
48fbe735 18950 linkage_name = DW_STRING (&attr);
c906108c
SS
18951 break;
18952 case DW_AT_low_pc:
18953 has_low_pc_attr = 1;
48fbe735 18954 lowpc = attr_value_as_address (&attr);
c906108c
SS
18955 break;
18956 case DW_AT_high_pc:
18957 has_high_pc_attr = 1;
48fbe735 18958 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18959 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18960 high_pc_relative = 1;
c906108c
SS
18961 break;
18962 case DW_AT_location:
0963b4bd 18963 /* Support the .debug_loc offsets. */
8e19ed76
PS
18964 if (attr_form_is_block (&attr))
18965 {
48fbe735 18966 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18967 }
3690dd37 18968 else if (attr_form_is_section_offset (&attr))
8e19ed76 18969 {
4d3c2250 18970 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18971 }
18972 else
18973 {
4d3c2250
KB
18974 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18975 "partial symbol information");
8e19ed76 18976 }
c906108c 18977 break;
c906108c 18978 case DW_AT_external:
48fbe735 18979 is_external = DW_UNSND (&attr);
c906108c
SS
18980 break;
18981 case DW_AT_declaration:
48fbe735 18982 is_declaration = DW_UNSND (&attr);
c906108c
SS
18983 break;
18984 case DW_AT_type:
48fbe735 18985 has_type = 1;
c906108c
SS
18986 break;
18987 case DW_AT_abstract_origin:
18988 case DW_AT_specification:
72bf9492 18989 case DW_AT_extension:
48fbe735
YQ
18990 has_specification = 1;
18991 spec_offset = dwarf2_get_ref_die_offset (&attr);
18992 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18993 || cu->per_cu->is_dwz);
c906108c
SS
18994 break;
18995 case DW_AT_sibling:
18996 /* Ignore absolute siblings, they might point outside of
18997 the current compile unit. */
18998 if (attr.form == DW_FORM_ref_addr)
b98664d3 18999 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19000 else
b9502d3f 19001 {
48fbe735 19002 const gdb_byte *buffer = reader->buffer;
9c541725
PA
19003 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19004 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19005
19006 if (sibling_ptr < info_ptr)
b98664d3 19007 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
19008 else if (sibling_ptr > reader->buffer_end)
19009 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19010 else
48fbe735 19011 sibling = sibling_ptr;
b9502d3f 19012 }
c906108c 19013 break;
fa4028e9 19014 case DW_AT_byte_size:
48fbe735 19015 has_byte_size = 1;
fa4028e9 19016 break;
ff908ebf 19017 case DW_AT_const_value:
48fbe735 19018 has_const_value = 1;
ff908ebf 19019 break;
68511cec
CES
19020 case DW_AT_calling_convention:
19021 /* DWARF doesn't provide a way to identify a program's source-level
19022 entry point. DW_AT_calling_convention attributes are only meant
19023 to describe functions' calling conventions.
19024
19025 However, because it's a necessary piece of information in
0c1b455e
TT
19026 Fortran, and before DWARF 4 DW_CC_program was the only
19027 piece of debugging information whose definition refers to
19028 a 'main program' at all, several compilers marked Fortran
19029 main programs with DW_CC_program --- even when those
19030 functions use the standard calling conventions.
19031
19032 Although DWARF now specifies a way to provide this
19033 information, we support this practice for backward
19034 compatibility. */
68511cec 19035 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19036 && cu->language == language_fortran)
48fbe735 19037 main_subprogram = 1;
68511cec 19038 break;
481860b3
GB
19039 case DW_AT_inline:
19040 if (DW_UNSND (&attr) == DW_INL_inlined
19041 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19042 may_be_inlined = 1;
481860b3 19043 break;
95554aad
TT
19044
19045 case DW_AT_import:
48fbe735 19046 if (tag == DW_TAG_imported_unit)
36586728 19047 {
48fbe735
YQ
19048 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19049 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19050 || cu->per_cu->is_dwz);
19051 }
95554aad
TT
19052 break;
19053
0c1b455e 19054 case DW_AT_main_subprogram:
48fbe735 19055 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19056 break;
19057
05caa1d2
TT
19058 case DW_AT_ranges:
19059 {
19060 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19061 but that requires a full DIE, so instead we just
19062 reimplement it. */
19063 int need_ranges_base = tag != DW_TAG_compile_unit;
19064 unsigned int ranges_offset = (DW_UNSND (&attr)
19065 + (need_ranges_base
19066 ? cu->ranges_base
19067 : 0));
19068
19069 /* Value of the DW_AT_ranges attribute is the offset in the
19070 .debug_ranges section. */
19071 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19072 nullptr))
19073 has_pc_info = 1;
19074 }
19075 break;
19076
c906108c
SS
19077 default:
19078 break;
19079 }
19080 }
19081
10d06d82
TT
19082 /* For Ada, if both the name and the linkage name appear, we prefer
19083 the latter. This lets "catch exception" work better, regardless
19084 of the order in which the name and linkage name were emitted.
19085 Really, though, this is just a workaround for the fact that gdb
19086 doesn't store both the name and the linkage name. */
19087 if (cu->language == language_ada && linkage_name != nullptr)
19088 name = linkage_name;
19089
91da1414 19090 if (high_pc_relative)
48fbe735 19091 highpc += lowpc;
91da1414 19092
9373cf26
JK
19093 if (has_low_pc_attr && has_high_pc_attr)
19094 {
19095 /* When using the GNU linker, .gnu.linkonce. sections are used to
19096 eliminate duplicate copies of functions and vtables and such.
19097 The linker will arbitrarily choose one and discard the others.
19098 The AT_*_pc values for such functions refer to local labels in
19099 these sections. If the section from that file was discarded, the
19100 labels are not in the output, so the relocs get a value of 0.
19101 If this is a discarded function, mark the pc bounds as invalid,
19102 so that GDB will ignore it. */
48fbe735 19103 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19104 {
48fbe735 19105 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19106 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19107
b98664d3 19108 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19109 "for DIE at %s [in module %s]"),
48fbe735
YQ
19110 paddress (gdbarch, lowpc),
19111 sect_offset_str (sect_off),
9d8780f0 19112 objfile_name (objfile));
9373cf26
JK
19113 }
19114 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19115 else if (lowpc >= highpc)
9373cf26 19116 {
48fbe735 19117 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19118 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19119
b98664d3 19120 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19121 "for DIE at %s [in module %s]"),
48fbe735
YQ
19122 paddress (gdbarch, lowpc),
19123 paddress (gdbarch, highpc),
19124 sect_offset_str (sect_off),
9c541725 19125 objfile_name (objfile));
9373cf26
JK
19126 }
19127 else
48fbe735 19128 has_pc_info = 1;
9373cf26 19129 }
85cbf3d3 19130
c906108c
SS
19131 return info_ptr;
19132}
19133
72bf9492
DJ
19134/* Find a cached partial DIE at OFFSET in CU. */
19135
d590ff25
YQ
19136struct partial_die_info *
19137dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19138{
19139 struct partial_die_info *lookup_die = NULL;
6f06d47b 19140 struct partial_die_info part_die (sect_off);
72bf9492 19141
9a3c8263 19142 lookup_die = ((struct partial_die_info *)
d590ff25 19143 htab_find_with_hash (partial_dies, &part_die,
9c541725 19144 to_underlying (sect_off)));
72bf9492 19145
72bf9492
DJ
19146 return lookup_die;
19147}
19148
348e048f
DE
19149/* Find a partial DIE at OFFSET, which may or may not be in CU,
19150 except in the case of .debug_types DIEs which do not reference
19151 outside their CU (they do however referencing other types via
55f1336d 19152 DW_FORM_ref_sig8). */
72bf9492 19153
122cf0f2 19154static const struct cu_partial_die_info
9c541725 19155find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19156{
518817b3
SM
19157 struct dwarf2_per_objfile *dwarf2_per_objfile
19158 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19159 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19160 struct dwarf2_per_cu_data *per_cu = NULL;
19161 struct partial_die_info *pd = NULL;
72bf9492 19162
36586728 19163 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19164 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19165 {
d590ff25 19166 pd = cu->find_partial_die (sect_off);
5afb4e99 19167 if (pd != NULL)
fb816e8b 19168 return { cu, pd };
0d99eb77
DE
19169 /* We missed recording what we needed.
19170 Load all dies and try again. */
19171 per_cu = cu->per_cu;
5afb4e99 19172 }
0d99eb77
DE
19173 else
19174 {
19175 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19176 if (cu->per_cu->is_debug_types)
0d99eb77 19177 {
9d8780f0
SM
19178 error (_("Dwarf Error: Type Unit at offset %s contains"
19179 " external reference to offset %s [in module %s].\n"),
19180 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19181 bfd_get_filename (objfile->obfd));
19182 }
9c541725 19183 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19184 dwarf2_per_objfile);
72bf9492 19185
0d99eb77
DE
19186 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19187 load_partial_comp_unit (per_cu);
ae038cb0 19188
0d99eb77 19189 per_cu->cu->last_used = 0;
d590ff25 19190 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19191 }
5afb4e99 19192
dee91e82
DE
19193 /* If we didn't find it, and not all dies have been loaded,
19194 load them all and try again. */
19195
5afb4e99
DJ
19196 if (pd == NULL && per_cu->load_all_dies == 0)
19197 {
5afb4e99 19198 per_cu->load_all_dies = 1;
fd820528
DE
19199
19200 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19201 THIS_CU->cu may already be in use. So we can't just free it and
19202 replace its DIEs with the ones we read in. Instead, we leave those
19203 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19204 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19205 set. */
dee91e82 19206 load_partial_comp_unit (per_cu);
5afb4e99 19207
d590ff25 19208 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19209 }
19210
19211 if (pd == NULL)
19212 internal_error (__FILE__, __LINE__,
9d8780f0 19213 _("could not find partial DIE %s "
3e43a32a 19214 "in cache [from module %s]\n"),
9d8780f0 19215 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19216 return { per_cu->cu, pd };
72bf9492
DJ
19217}
19218
abc72ce4
DE
19219/* See if we can figure out if the class lives in a namespace. We do
19220 this by looking for a member function; its demangled name will
19221 contain namespace info, if there is any. */
19222
19223static void
19224guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19225 struct dwarf2_cu *cu)
19226{
19227 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19228 what template types look like, because the demangler
19229 frequently doesn't give the same name as the debug info. We
19230 could fix this by only using the demangled name to get the
19231 prefix (but see comment in read_structure_type). */
19232
19233 struct partial_die_info *real_pdi;
19234 struct partial_die_info *child_pdi;
19235
19236 /* If this DIE (this DIE's specification, if any) has a parent, then
19237 we should not do this. We'll prepend the parent's fully qualified
19238 name when we create the partial symbol. */
19239
19240 real_pdi = struct_pdi;
19241 while (real_pdi->has_specification)
fb816e8b 19242 {
122cf0f2
AB
19243 auto res = find_partial_die (real_pdi->spec_offset,
19244 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19245 real_pdi = res.pdi;
19246 cu = res.cu;
19247 }
abc72ce4
DE
19248
19249 if (real_pdi->die_parent != NULL)
19250 return;
19251
19252 for (child_pdi = struct_pdi->die_child;
19253 child_pdi != NULL;
19254 child_pdi = child_pdi->die_sibling)
19255 {
19256 if (child_pdi->tag == DW_TAG_subprogram
19257 && child_pdi->linkage_name != NULL)
19258 {
19259 char *actual_class_name
19260 = language_class_name_from_physname (cu->language_defn,
19261 child_pdi->linkage_name);
19262 if (actual_class_name != NULL)
19263 {
518817b3 19264 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19265 struct_pdi->name
021887d8
TT
19266 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19267 actual_class_name);
abc72ce4
DE
19268 xfree (actual_class_name);
19269 }
19270 break;
19271 }
19272 }
19273}
19274
52356b79
YQ
19275void
19276partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19277{
abc72ce4
DE
19278 /* Once we've fixed up a die, there's no point in doing so again.
19279 This also avoids a memory leak if we were to call
19280 guess_partial_die_structure_name multiple times. */
52356b79 19281 if (fixup_called)
abc72ce4
DE
19282 return;
19283
72bf9492
DJ
19284 /* If we found a reference attribute and the DIE has no name, try
19285 to find a name in the referred to DIE. */
19286
52356b79 19287 if (name == NULL && has_specification)
72bf9492
DJ
19288 {
19289 struct partial_die_info *spec_die;
72bf9492 19290
122cf0f2 19291 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19292 spec_die = res.pdi;
19293 cu = res.cu;
72bf9492 19294
52356b79 19295 spec_die->fixup (cu);
72bf9492
DJ
19296
19297 if (spec_die->name)
19298 {
52356b79 19299 name = spec_die->name;
72bf9492
DJ
19300
19301 /* Copy DW_AT_external attribute if it is set. */
19302 if (spec_die->is_external)
52356b79 19303 is_external = spec_die->is_external;
72bf9492
DJ
19304 }
19305 }
19306
19307 /* Set default names for some unnamed DIEs. */
72bf9492 19308
52356b79
YQ
19309 if (name == NULL && tag == DW_TAG_namespace)
19310 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19311
abc72ce4
DE
19312 /* If there is no parent die to provide a namespace, and there are
19313 children, see if we can determine the namespace from their linkage
122d1940 19314 name. */
abc72ce4 19315 if (cu->language == language_cplus
fd5866f6 19316 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19317 && die_parent == NULL
19318 && has_children
19319 && (tag == DW_TAG_class_type
19320 || tag == DW_TAG_structure_type
19321 || tag == DW_TAG_union_type))
19322 guess_partial_die_structure_name (this, cu);
abc72ce4 19323
53832f31
TT
19324 /* GCC might emit a nameless struct or union that has a linkage
19325 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19326 if (name == NULL
19327 && (tag == DW_TAG_class_type
19328 || tag == DW_TAG_interface_type
19329 || tag == DW_TAG_structure_type
19330 || tag == DW_TAG_union_type)
19331 && linkage_name != NULL)
53832f31
TT
19332 {
19333 char *demangled;
19334
52356b79 19335 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19336 if (demangled)
19337 {
96408a79
SA
19338 const char *base;
19339
19340 /* Strip any leading namespaces/classes, keep only the base name.
19341 DW_AT_name for named DIEs does not contain the prefixes. */
19342 base = strrchr (demangled, ':');
19343 if (base && base > demangled && base[-1] == ':')
19344 base++;
19345 else
19346 base = demangled;
19347
518817b3 19348 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19349 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19350 xfree (demangled);
19351 }
19352 }
19353
52356b79 19354 fixup_called = 1;
72bf9492
DJ
19355}
19356
a8329558 19357/* Read an attribute value described by an attribute form. */
c906108c 19358
d521ce57 19359static const gdb_byte *
dee91e82
DE
19360read_attribute_value (const struct die_reader_specs *reader,
19361 struct attribute *attr, unsigned form,
43988095 19362 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19363{
dee91e82 19364 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19365 struct dwarf2_per_objfile *dwarf2_per_objfile
19366 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19367 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19368 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19369 bfd *abfd = reader->abfd;
e7c27a73 19370 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19371 unsigned int bytes_read;
19372 struct dwarf_block *blk;
19373
aead7601 19374 attr->form = (enum dwarf_form) form;
a8329558 19375 switch (form)
c906108c 19376 {
c906108c 19377 case DW_FORM_ref_addr:
ae411497 19378 if (cu->header.version == 2)
4568ecf9 19379 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19380 else
4568ecf9
DE
19381 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19382 &cu->header, &bytes_read);
ae411497
TT
19383 info_ptr += bytes_read;
19384 break;
36586728
TT
19385 case DW_FORM_GNU_ref_alt:
19386 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19387 info_ptr += bytes_read;
19388 break;
ae411497 19389 case DW_FORM_addr:
e7c27a73 19390 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19391 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19392 info_ptr += bytes_read;
c906108c
SS
19393 break;
19394 case DW_FORM_block2:
7b5a2f43 19395 blk = dwarf_alloc_block (cu);
c906108c
SS
19396 blk->size = read_2_bytes (abfd, info_ptr);
19397 info_ptr += 2;
19398 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19399 info_ptr += blk->size;
19400 DW_BLOCK (attr) = blk;
19401 break;
19402 case DW_FORM_block4:
7b5a2f43 19403 blk = dwarf_alloc_block (cu);
c906108c
SS
19404 blk->size = read_4_bytes (abfd, info_ptr);
19405 info_ptr += 4;
19406 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19407 info_ptr += blk->size;
19408 DW_BLOCK (attr) = blk;
19409 break;
19410 case DW_FORM_data2:
19411 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19412 info_ptr += 2;
19413 break;
19414 case DW_FORM_data4:
19415 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19416 info_ptr += 4;
19417 break;
19418 case DW_FORM_data8:
19419 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19420 info_ptr += 8;
19421 break;
0224619f
JK
19422 case DW_FORM_data16:
19423 blk = dwarf_alloc_block (cu);
19424 blk->size = 16;
19425 blk->data = read_n_bytes (abfd, info_ptr, 16);
19426 info_ptr += 16;
19427 DW_BLOCK (attr) = blk;
19428 break;
2dc7f7b3
TT
19429 case DW_FORM_sec_offset:
19430 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19431 info_ptr += bytes_read;
19432 break;
c906108c 19433 case DW_FORM_string:
9b1c24c8 19434 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19435 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19436 info_ptr += bytes_read;
19437 break;
4bdf3d34 19438 case DW_FORM_strp:
36586728
TT
19439 if (!cu->per_cu->is_dwz)
19440 {
ed2dc618
SM
19441 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19442 abfd, info_ptr, cu_header,
36586728
TT
19443 &bytes_read);
19444 DW_STRING_IS_CANONICAL (attr) = 0;
19445 info_ptr += bytes_read;
19446 break;
19447 }
19448 /* FALLTHROUGH */
43988095
JK
19449 case DW_FORM_line_strp:
19450 if (!cu->per_cu->is_dwz)
19451 {
ed2dc618
SM
19452 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19453 abfd, info_ptr,
43988095
JK
19454 cu_header, &bytes_read);
19455 DW_STRING_IS_CANONICAL (attr) = 0;
19456 info_ptr += bytes_read;
19457 break;
19458 }
19459 /* FALLTHROUGH */
36586728
TT
19460 case DW_FORM_GNU_strp_alt:
19461 {
ed2dc618 19462 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19463 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19464 &bytes_read);
19465
ed2dc618
SM
19466 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19467 dwz, str_offset);
36586728
TT
19468 DW_STRING_IS_CANONICAL (attr) = 0;
19469 info_ptr += bytes_read;
19470 }
4bdf3d34 19471 break;
2dc7f7b3 19472 case DW_FORM_exprloc:
c906108c 19473 case DW_FORM_block:
7b5a2f43 19474 blk = dwarf_alloc_block (cu);
c906108c
SS
19475 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19476 info_ptr += bytes_read;
19477 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19478 info_ptr += blk->size;
19479 DW_BLOCK (attr) = blk;
19480 break;
19481 case DW_FORM_block1:
7b5a2f43 19482 blk = dwarf_alloc_block (cu);
c906108c
SS
19483 blk->size = read_1_byte (abfd, info_ptr);
19484 info_ptr += 1;
19485 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19486 info_ptr += blk->size;
19487 DW_BLOCK (attr) = blk;
19488 break;
19489 case DW_FORM_data1:
19490 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19491 info_ptr += 1;
19492 break;
19493 case DW_FORM_flag:
19494 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19495 info_ptr += 1;
19496 break;
2dc7f7b3
TT
19497 case DW_FORM_flag_present:
19498 DW_UNSND (attr) = 1;
19499 break;
c906108c
SS
19500 case DW_FORM_sdata:
19501 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19502 info_ptr += bytes_read;
19503 break;
19504 case DW_FORM_udata:
19505 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19506 info_ptr += bytes_read;
19507 break;
19508 case DW_FORM_ref1:
9c541725 19509 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19510 + read_1_byte (abfd, info_ptr));
c906108c
SS
19511 info_ptr += 1;
19512 break;
19513 case DW_FORM_ref2:
9c541725 19514 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19515 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19516 info_ptr += 2;
19517 break;
19518 case DW_FORM_ref4:
9c541725 19519 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19520 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19521 info_ptr += 4;
19522 break;
613e1657 19523 case DW_FORM_ref8:
9c541725 19524 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19525 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19526 info_ptr += 8;
19527 break;
55f1336d 19528 case DW_FORM_ref_sig8:
ac9ec31b 19529 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19530 info_ptr += 8;
19531 break;
c906108c 19532 case DW_FORM_ref_udata:
9c541725 19533 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19534 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19535 info_ptr += bytes_read;
19536 break;
c906108c 19537 case DW_FORM_indirect:
a8329558
KW
19538 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19539 info_ptr += bytes_read;
43988095
JK
19540 if (form == DW_FORM_implicit_const)
19541 {
19542 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19543 info_ptr += bytes_read;
19544 }
19545 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19546 info_ptr);
19547 break;
19548 case DW_FORM_implicit_const:
19549 DW_SND (attr) = implicit_const;
a8329558 19550 break;
336d760d 19551 case DW_FORM_addrx:
3019eac3
DE
19552 case DW_FORM_GNU_addr_index:
19553 if (reader->dwo_file == NULL)
19554 {
19555 /* For now flag a hard error.
19556 Later we can turn this into a complaint. */
19557 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19558 dwarf_form_name (form),
19559 bfd_get_filename (abfd));
19560 }
19561 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19562 info_ptr += bytes_read;
19563 break;
cf532bd1 19564 case DW_FORM_strx:
15f18d14
AT
19565 case DW_FORM_strx1:
19566 case DW_FORM_strx2:
19567 case DW_FORM_strx3:
19568 case DW_FORM_strx4:
3019eac3
DE
19569 case DW_FORM_GNU_str_index:
19570 if (reader->dwo_file == NULL)
19571 {
19572 /* For now flag a hard error.
19573 Later we can turn this into a complaint if warranted. */
19574 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19575 dwarf_form_name (form),
19576 bfd_get_filename (abfd));
19577 }
19578 {
15f18d14
AT
19579 ULONGEST str_index;
19580 if (form == DW_FORM_strx1)
19581 {
19582 str_index = read_1_byte (abfd, info_ptr);
19583 info_ptr += 1;
19584 }
19585 else if (form == DW_FORM_strx2)
19586 {
19587 str_index = read_2_bytes (abfd, info_ptr);
19588 info_ptr += 2;
19589 }
19590 else if (form == DW_FORM_strx3)
19591 {
19592 str_index = read_3_bytes (abfd, info_ptr);
19593 info_ptr += 3;
19594 }
19595 else if (form == DW_FORM_strx4)
19596 {
19597 str_index = read_4_bytes (abfd, info_ptr);
19598 info_ptr += 4;
19599 }
19600 else
19601 {
19602 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19603 info_ptr += bytes_read;
19604 }
342587c4 19605 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19606 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19607 }
19608 break;
c906108c 19609 default:
8a3fe4f8 19610 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19611 dwarf_form_name (form),
19612 bfd_get_filename (abfd));
c906108c 19613 }
28e94949 19614
36586728 19615 /* Super hack. */
7771576e 19616 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19617 attr->form = DW_FORM_GNU_ref_alt;
19618
28e94949
JB
19619 /* We have seen instances where the compiler tried to emit a byte
19620 size attribute of -1 which ended up being encoded as an unsigned
19621 0xffffffff. Although 0xffffffff is technically a valid size value,
19622 an object of this size seems pretty unlikely so we can relatively
19623 safely treat these cases as if the size attribute was invalid and
19624 treat them as zero by default. */
19625 if (attr->name == DW_AT_byte_size
19626 && form == DW_FORM_data4
19627 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19628 {
19629 complaint
b98664d3 19630 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19631 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19632 DW_UNSND (attr) = 0;
19633 }
28e94949 19634
c906108c
SS
19635 return info_ptr;
19636}
19637
a8329558
KW
19638/* Read an attribute described by an abbreviated attribute. */
19639
d521ce57 19640static const gdb_byte *
dee91e82
DE
19641read_attribute (const struct die_reader_specs *reader,
19642 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19643 const gdb_byte *info_ptr)
a8329558
KW
19644{
19645 attr->name = abbrev->name;
43988095
JK
19646 return read_attribute_value (reader, attr, abbrev->form,
19647 abbrev->implicit_const, info_ptr);
a8329558
KW
19648}
19649
0963b4bd 19650/* Read dwarf information from a buffer. */
c906108c
SS
19651
19652static unsigned int
a1855c1d 19653read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19654{
fe1b8b76 19655 return bfd_get_8 (abfd, buf);
c906108c
SS
19656}
19657
19658static int
a1855c1d 19659read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19660{
fe1b8b76 19661 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19662}
19663
19664static unsigned int
a1855c1d 19665read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19666{
fe1b8b76 19667 return bfd_get_16 (abfd, buf);
c906108c
SS
19668}
19669
21ae7a4d 19670static int
a1855c1d 19671read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19672{
19673 return bfd_get_signed_16 (abfd, buf);
19674}
19675
15f18d14
AT
19676static unsigned int
19677read_3_bytes (bfd *abfd, const gdb_byte *buf)
19678{
19679 unsigned int result = 0;
19680 for (int i = 0; i < 3; ++i)
19681 {
19682 unsigned char byte = bfd_get_8 (abfd, buf);
19683 buf++;
19684 result |= ((unsigned int) byte << (i * 8));
19685 }
19686 return result;
19687}
19688
c906108c 19689static unsigned int
a1855c1d 19690read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19691{
fe1b8b76 19692 return bfd_get_32 (abfd, buf);
c906108c
SS
19693}
19694
21ae7a4d 19695static int
a1855c1d 19696read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19697{
19698 return bfd_get_signed_32 (abfd, buf);
19699}
19700
93311388 19701static ULONGEST
a1855c1d 19702read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19703{
fe1b8b76 19704 return bfd_get_64 (abfd, buf);
c906108c
SS
19705}
19706
19707static CORE_ADDR
d521ce57 19708read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19709 unsigned int *bytes_read)
c906108c 19710{
e7c27a73 19711 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19712 CORE_ADDR retval = 0;
19713
107d2387 19714 if (cu_header->signed_addr_p)
c906108c 19715 {
107d2387
AC
19716 switch (cu_header->addr_size)
19717 {
19718 case 2:
fe1b8b76 19719 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19720 break;
19721 case 4:
fe1b8b76 19722 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19723 break;
19724 case 8:
fe1b8b76 19725 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19726 break;
19727 default:
8e65ff28 19728 internal_error (__FILE__, __LINE__,
e2e0b3e5 19729 _("read_address: bad switch, signed [in module %s]"),
659b0389 19730 bfd_get_filename (abfd));
107d2387
AC
19731 }
19732 }
19733 else
19734 {
19735 switch (cu_header->addr_size)
19736 {
19737 case 2:
fe1b8b76 19738 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19739 break;
19740 case 4:
fe1b8b76 19741 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19742 break;
19743 case 8:
fe1b8b76 19744 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19745 break;
19746 default:
8e65ff28 19747 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19748 _("read_address: bad switch, "
19749 "unsigned [in module %s]"),
659b0389 19750 bfd_get_filename (abfd));
107d2387 19751 }
c906108c 19752 }
64367e0a 19753
107d2387
AC
19754 *bytes_read = cu_header->addr_size;
19755 return retval;
c906108c
SS
19756}
19757
f7ef9339 19758/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19759 specification allows the initial length to take up either 4 bytes
19760 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19761 bytes describe the length and all offsets will be 8 bytes in length
19762 instead of 4.
19763
f7ef9339
KB
19764 An older, non-standard 64-bit format is also handled by this
19765 function. The older format in question stores the initial length
19766 as an 8-byte quantity without an escape value. Lengths greater
19767 than 2^32 aren't very common which means that the initial 4 bytes
19768 is almost always zero. Since a length value of zero doesn't make
19769 sense for the 32-bit format, this initial zero can be considered to
19770 be an escape value which indicates the presence of the older 64-bit
19771 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19772 greater than 4GB. If it becomes necessary to handle lengths
19773 somewhat larger than 4GB, we could allow other small values (such
19774 as the non-sensical values of 1, 2, and 3) to also be used as
19775 escape values indicating the presence of the old format.
f7ef9339 19776
917c78fc
MK
19777 The value returned via bytes_read should be used to increment the
19778 relevant pointer after calling read_initial_length().
c764a876 19779
613e1657
KB
19780 [ Note: read_initial_length() and read_offset() are based on the
19781 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19782 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19783 from:
19784
f7ef9339 19785 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19786
613e1657
KB
19787 This document is only a draft and is subject to change. (So beware.)
19788
f7ef9339 19789 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19790 determined empirically by examining 64-bit ELF files produced by
19791 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19792
19793 - Kevin, July 16, 2002
613e1657
KB
19794 ] */
19795
19796static LONGEST
d521ce57 19797read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19798{
fe1b8b76 19799 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19800
dd373385 19801 if (length == 0xffffffff)
613e1657 19802 {
fe1b8b76 19803 length = bfd_get_64 (abfd, buf + 4);
613e1657 19804 *bytes_read = 12;
613e1657 19805 }
dd373385 19806 else if (length == 0)
f7ef9339 19807 {
dd373385 19808 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19809 length = bfd_get_64 (abfd, buf);
f7ef9339 19810 *bytes_read = 8;
f7ef9339 19811 }
613e1657
KB
19812 else
19813 {
19814 *bytes_read = 4;
613e1657
KB
19815 }
19816
c764a876
DE
19817 return length;
19818}
dd373385 19819
c764a876
DE
19820/* Cover function for read_initial_length.
19821 Returns the length of the object at BUF, and stores the size of the
19822 initial length in *BYTES_READ and stores the size that offsets will be in
19823 *OFFSET_SIZE.
19824 If the initial length size is not equivalent to that specified in
19825 CU_HEADER then issue a complaint.
19826 This is useful when reading non-comp-unit headers. */
dd373385 19827
c764a876 19828static LONGEST
d521ce57 19829read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19830 const struct comp_unit_head *cu_header,
19831 unsigned int *bytes_read,
19832 unsigned int *offset_size)
19833{
19834 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19835
19836 gdb_assert (cu_header->initial_length_size == 4
19837 || cu_header->initial_length_size == 8
19838 || cu_header->initial_length_size == 12);
19839
19840 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19841 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19842
c764a876 19843 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19844 return length;
613e1657
KB
19845}
19846
19847/* Read an offset from the data stream. The size of the offset is
917c78fc 19848 given by cu_header->offset_size. */
613e1657
KB
19849
19850static LONGEST
d521ce57
TT
19851read_offset (bfd *abfd, const gdb_byte *buf,
19852 const struct comp_unit_head *cu_header,
891d2f0b 19853 unsigned int *bytes_read)
c764a876
DE
19854{
19855 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19856
c764a876
DE
19857 *bytes_read = cu_header->offset_size;
19858 return offset;
19859}
19860
19861/* Read an offset from the data stream. */
19862
19863static LONGEST
d521ce57 19864read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19865{
19866 LONGEST retval = 0;
19867
c764a876 19868 switch (offset_size)
613e1657
KB
19869 {
19870 case 4:
fe1b8b76 19871 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19872 break;
19873 case 8:
fe1b8b76 19874 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19875 break;
19876 default:
8e65ff28 19877 internal_error (__FILE__, __LINE__,
c764a876 19878 _("read_offset_1: bad switch [in module %s]"),
659b0389 19879 bfd_get_filename (abfd));
613e1657
KB
19880 }
19881
917c78fc 19882 return retval;
613e1657
KB
19883}
19884
d521ce57
TT
19885static const gdb_byte *
19886read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19887{
19888 /* If the size of a host char is 8 bits, we can return a pointer
19889 to the buffer, otherwise we have to copy the data to a buffer
19890 allocated on the temporary obstack. */
4bdf3d34 19891 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19892 return buf;
c906108c
SS
19893}
19894
d521ce57
TT
19895static const char *
19896read_direct_string (bfd *abfd, const gdb_byte *buf,
19897 unsigned int *bytes_read_ptr)
c906108c
SS
19898{
19899 /* If the size of a host char is 8 bits, we can return a pointer
19900 to the string, otherwise we have to copy the string to a buffer
19901 allocated on the temporary obstack. */
4bdf3d34 19902 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19903 if (*buf == '\0')
19904 {
19905 *bytes_read_ptr = 1;
19906 return NULL;
19907 }
d521ce57
TT
19908 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19909 return (const char *) buf;
4bdf3d34
JJ
19910}
19911
43988095
JK
19912/* Return pointer to string at section SECT offset STR_OFFSET with error
19913 reporting strings FORM_NAME and SECT_NAME. */
19914
d521ce57 19915static const char *
ed2dc618
SM
19916read_indirect_string_at_offset_from (struct objfile *objfile,
19917 bfd *abfd, LONGEST str_offset,
43988095
JK
19918 struct dwarf2_section_info *sect,
19919 const char *form_name,
19920 const char *sect_name)
19921{
ed2dc618 19922 dwarf2_read_section (objfile, sect);
43988095
JK
19923 if (sect->buffer == NULL)
19924 error (_("%s used without %s section [in module %s]"),
19925 form_name, sect_name, bfd_get_filename (abfd));
19926 if (str_offset >= sect->size)
19927 error (_("%s pointing outside of %s section [in module %s]"),
19928 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19929 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19930 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19931 return NULL;
43988095
JK
19932 return (const char *) (sect->buffer + str_offset);
19933}
19934
19935/* Return pointer to string at .debug_str offset STR_OFFSET. */
19936
19937static const char *
ed2dc618
SM
19938read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19939 bfd *abfd, LONGEST str_offset)
43988095 19940{
ed2dc618
SM
19941 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19942 abfd, str_offset,
43988095
JK
19943 &dwarf2_per_objfile->str,
19944 "DW_FORM_strp", ".debug_str");
19945}
19946
19947/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19948
19949static const char *
ed2dc618
SM
19950read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19951 bfd *abfd, LONGEST str_offset)
43988095 19952{
ed2dc618
SM
19953 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19954 abfd, str_offset,
43988095
JK
19955 &dwarf2_per_objfile->line_str,
19956 "DW_FORM_line_strp",
19957 ".debug_line_str");
c906108c
SS
19958}
19959
36586728
TT
19960/* Read a string at offset STR_OFFSET in the .debug_str section from
19961 the .dwz file DWZ. Throw an error if the offset is too large. If
19962 the string consists of a single NUL byte, return NULL; otherwise
19963 return a pointer to the string. */
19964
d521ce57 19965static const char *
ed2dc618
SM
19966read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19967 LONGEST str_offset)
36586728 19968{
ed2dc618 19969 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19970
19971 if (dwz->str.buffer == NULL)
19972 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19973 "section [in module %s]"),
00f93c44 19974 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19975 if (str_offset >= dwz->str.size)
19976 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19977 ".debug_str section [in module %s]"),
00f93c44 19978 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19979 gdb_assert (HOST_CHAR_BIT == 8);
19980 if (dwz->str.buffer[str_offset] == '\0')
19981 return NULL;
d521ce57 19982 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19983}
19984
43988095
JK
19985/* Return pointer to string at .debug_str offset as read from BUF.
19986 BUF is assumed to be in a compilation unit described by CU_HEADER.
19987 Return *BYTES_READ_PTR count of bytes read from BUF. */
19988
d521ce57 19989static const char *
ed2dc618
SM
19990read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19991 const gdb_byte *buf,
cf2c3c16
TT
19992 const struct comp_unit_head *cu_header,
19993 unsigned int *bytes_read_ptr)
19994{
19995 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19996
ed2dc618 19997 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19998}
19999
43988095
JK
20000/* Return pointer to string at .debug_line_str offset as read from BUF.
20001 BUF is assumed to be in a compilation unit described by CU_HEADER.
20002 Return *BYTES_READ_PTR count of bytes read from BUF. */
20003
20004static const char *
ed2dc618
SM
20005read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20006 bfd *abfd, const gdb_byte *buf,
43988095
JK
20007 const struct comp_unit_head *cu_header,
20008 unsigned int *bytes_read_ptr)
20009{
20010 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20011
ed2dc618
SM
20012 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20013 str_offset);
43988095
JK
20014}
20015
20016ULONGEST
d521ce57 20017read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 20018 unsigned int *bytes_read_ptr)
c906108c 20019{
12df843f 20020 ULONGEST result;
ce5d95e1 20021 unsigned int num_read;
870f88f7 20022 int shift;
c906108c
SS
20023 unsigned char byte;
20024
20025 result = 0;
20026 shift = 0;
20027 num_read = 0;
c906108c
SS
20028 while (1)
20029 {
fe1b8b76 20030 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20031 buf++;
20032 num_read++;
12df843f 20033 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20034 if ((byte & 128) == 0)
20035 {
20036 break;
20037 }
20038 shift += 7;
20039 }
20040 *bytes_read_ptr = num_read;
20041 return result;
20042}
20043
12df843f 20044static LONGEST
d521ce57
TT
20045read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20046 unsigned int *bytes_read_ptr)
c906108c 20047{
4dd1b460 20048 ULONGEST result;
870f88f7 20049 int shift, num_read;
c906108c
SS
20050 unsigned char byte;
20051
20052 result = 0;
20053 shift = 0;
c906108c 20054 num_read = 0;
c906108c
SS
20055 while (1)
20056 {
fe1b8b76 20057 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20058 buf++;
20059 num_read++;
4dd1b460 20060 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20061 shift += 7;
20062 if ((byte & 128) == 0)
20063 {
20064 break;
20065 }
20066 }
77e0b926 20067 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 20068 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
20069 *bytes_read_ptr = num_read;
20070 return result;
20071}
20072
3019eac3
DE
20073/* Given index ADDR_INDEX in .debug_addr, fetch the value.
20074 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20075 ADDR_SIZE is the size of addresses from the CU header. */
20076
20077static CORE_ADDR
ed2dc618
SM
20078read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20079 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
20080{
20081 struct objfile *objfile = dwarf2_per_objfile->objfile;
20082 bfd *abfd = objfile->obfd;
20083 const gdb_byte *info_ptr;
20084
20085 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20086 if (dwarf2_per_objfile->addr.buffer == NULL)
20087 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20088 objfile_name (objfile));
3019eac3
DE
20089 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20090 error (_("DW_FORM_addr_index pointing outside of "
20091 ".debug_addr section [in module %s]"),
4262abfb 20092 objfile_name (objfile));
3019eac3
DE
20093 info_ptr = (dwarf2_per_objfile->addr.buffer
20094 + addr_base + addr_index * addr_size);
20095 if (addr_size == 4)
20096 return bfd_get_32 (abfd, info_ptr);
20097 else
20098 return bfd_get_64 (abfd, info_ptr);
20099}
20100
20101/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20102
20103static CORE_ADDR
20104read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20105{
518817b3
SM
20106 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20107 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20108}
20109
20110/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20111
20112static CORE_ADDR
d521ce57 20113read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20114 unsigned int *bytes_read)
20115{
518817b3 20116 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20117 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20118
20119 return read_addr_index (cu, addr_index);
20120}
20121
20122/* Data structure to pass results from dwarf2_read_addr_index_reader
20123 back to dwarf2_read_addr_index. */
20124
20125struct dwarf2_read_addr_index_data
20126{
20127 ULONGEST addr_base;
20128 int addr_size;
20129};
20130
20131/* die_reader_func for dwarf2_read_addr_index. */
20132
20133static void
20134dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 20135 const gdb_byte *info_ptr,
3019eac3
DE
20136 struct die_info *comp_unit_die,
20137 int has_children,
20138 void *data)
20139{
20140 struct dwarf2_cu *cu = reader->cu;
20141 struct dwarf2_read_addr_index_data *aidata =
20142 (struct dwarf2_read_addr_index_data *) data;
20143
20144 aidata->addr_base = cu->addr_base;
20145 aidata->addr_size = cu->header.addr_size;
20146}
20147
20148/* Given an index in .debug_addr, fetch the value.
20149 NOTE: This can be called during dwarf expression evaluation,
20150 long after the debug information has been read, and thus per_cu->cu
20151 may no longer exist. */
20152
20153CORE_ADDR
20154dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20155 unsigned int addr_index)
20156{
ed2dc618 20157 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
20158 struct dwarf2_cu *cu = per_cu->cu;
20159 ULONGEST addr_base;
20160 int addr_size;
20161
3019eac3
DE
20162 /* We need addr_base and addr_size.
20163 If we don't have PER_CU->cu, we have to get it.
20164 Nasty, but the alternative is storing the needed info in PER_CU,
20165 which at this point doesn't seem justified: it's not clear how frequently
20166 it would get used and it would increase the size of every PER_CU.
20167 Entry points like dwarf2_per_cu_addr_size do a similar thing
20168 so we're not in uncharted territory here.
20169 Alas we need to be a bit more complicated as addr_base is contained
20170 in the DIE.
20171
20172 We don't need to read the entire CU(/TU).
20173 We just need the header and top level die.
a1b64ce1 20174
3019eac3 20175 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20176 For now we skip this optimization. */
3019eac3
DE
20177
20178 if (cu != NULL)
20179 {
20180 addr_base = cu->addr_base;
20181 addr_size = cu->header.addr_size;
20182 }
20183 else
20184 {
20185 struct dwarf2_read_addr_index_data aidata;
20186
a1b64ce1
DE
20187 /* Note: We can't use init_cutu_and_read_dies_simple here,
20188 we need addr_base. */
58f0c718 20189 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 20190 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20191 addr_base = aidata.addr_base;
20192 addr_size = aidata.addr_size;
20193 }
20194
ed2dc618
SM
20195 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20196 addr_size);
3019eac3
DE
20197}
20198
cf532bd1 20199/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 20200 This is only used by the Fission support. */
3019eac3 20201
d521ce57 20202static const char *
342587c4 20203read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20204{
ed2dc618 20205 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20206 struct dwarf2_per_objfile *dwarf2_per_objfile
20207 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20208 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20209 const char *objf_name = objfile_name (objfile);
3019eac3 20210 bfd *abfd = objfile->obfd;
73869dc2
DE
20211 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20212 struct dwarf2_section_info *str_offsets_section =
20213 &reader->dwo_file->sections.str_offsets;
d521ce57 20214 const gdb_byte *info_ptr;
3019eac3 20215 ULONGEST str_offset;
cf532bd1 20216 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20217
73869dc2
DE
20218 dwarf2_read_section (objfile, str_section);
20219 dwarf2_read_section (objfile, str_offsets_section);
20220 if (str_section->buffer == NULL)
57d63ce2 20221 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20222 " in CU at offset %s [in module %s]"),
20223 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20224 if (str_offsets_section->buffer == NULL)
57d63ce2 20225 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20226 " in CU at offset %s [in module %s]"),
20227 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20228 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20229 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20230 " section in CU at offset %s [in module %s]"),
20231 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20232 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20233 + str_index * cu->header.offset_size);
20234 if (cu->header.offset_size == 4)
20235 str_offset = bfd_get_32 (abfd, info_ptr);
20236 else
20237 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20238 if (str_offset >= str_section->size)
57d63ce2 20239 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20240 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20241 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20242 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20243}
20244
3019eac3
DE
20245/* Return the length of an LEB128 number in BUF. */
20246
20247static int
20248leb128_size (const gdb_byte *buf)
20249{
20250 const gdb_byte *begin = buf;
20251 gdb_byte byte;
20252
20253 while (1)
20254 {
20255 byte = *buf++;
20256 if ((byte & 128) == 0)
20257 return buf - begin;
20258 }
20259}
20260
c906108c 20261static void
e142c38c 20262set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20263{
20264 switch (lang)
20265 {
20266 case DW_LANG_C89:
76bee0cc 20267 case DW_LANG_C99:
0cfd832f 20268 case DW_LANG_C11:
c906108c 20269 case DW_LANG_C:
d1be3247 20270 case DW_LANG_UPC:
e142c38c 20271 cu->language = language_c;
c906108c 20272 break;
9c37b5ae 20273 case DW_LANG_Java:
c906108c 20274 case DW_LANG_C_plus_plus:
0cfd832f
MW
20275 case DW_LANG_C_plus_plus_11:
20276 case DW_LANG_C_plus_plus_14:
e142c38c 20277 cu->language = language_cplus;
c906108c 20278 break;
6aecb9c2
JB
20279 case DW_LANG_D:
20280 cu->language = language_d;
20281 break;
c906108c
SS
20282 case DW_LANG_Fortran77:
20283 case DW_LANG_Fortran90:
b21b22e0 20284 case DW_LANG_Fortran95:
f7de9aab
MW
20285 case DW_LANG_Fortran03:
20286 case DW_LANG_Fortran08:
e142c38c 20287 cu->language = language_fortran;
c906108c 20288 break;
a766d390
DE
20289 case DW_LANG_Go:
20290 cu->language = language_go;
20291 break;
c906108c 20292 case DW_LANG_Mips_Assembler:
e142c38c 20293 cu->language = language_asm;
c906108c
SS
20294 break;
20295 case DW_LANG_Ada83:
8aaf0b47 20296 case DW_LANG_Ada95:
bc5f45f8
JB
20297 cu->language = language_ada;
20298 break;
72019c9c
GM
20299 case DW_LANG_Modula2:
20300 cu->language = language_m2;
20301 break;
fe8e67fd
PM
20302 case DW_LANG_Pascal83:
20303 cu->language = language_pascal;
20304 break;
22566fbd
DJ
20305 case DW_LANG_ObjC:
20306 cu->language = language_objc;
20307 break;
c44af4eb
TT
20308 case DW_LANG_Rust:
20309 case DW_LANG_Rust_old:
20310 cu->language = language_rust;
20311 break;
c906108c
SS
20312 case DW_LANG_Cobol74:
20313 case DW_LANG_Cobol85:
c906108c 20314 default:
e142c38c 20315 cu->language = language_minimal;
c906108c
SS
20316 break;
20317 }
e142c38c 20318 cu->language_defn = language_def (cu->language);
c906108c
SS
20319}
20320
20321/* Return the named attribute or NULL if not there. */
20322
20323static struct attribute *
e142c38c 20324dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20325{
a48e046c 20326 for (;;)
c906108c 20327 {
a48e046c
TT
20328 unsigned int i;
20329 struct attribute *spec = NULL;
20330
20331 for (i = 0; i < die->num_attrs; ++i)
20332 {
20333 if (die->attrs[i].name == name)
20334 return &die->attrs[i];
20335 if (die->attrs[i].name == DW_AT_specification
20336 || die->attrs[i].name == DW_AT_abstract_origin)
20337 spec = &die->attrs[i];
20338 }
20339
20340 if (!spec)
20341 break;
c906108c 20342
f2f0e013 20343 die = follow_die_ref (die, spec, &cu);
f2f0e013 20344 }
c5aa993b 20345
c906108c
SS
20346 return NULL;
20347}
20348
348e048f
DE
20349/* Return the named attribute or NULL if not there,
20350 but do not follow DW_AT_specification, etc.
20351 This is for use in contexts where we're reading .debug_types dies.
20352 Following DW_AT_specification, DW_AT_abstract_origin will take us
20353 back up the chain, and we want to go down. */
20354
20355static struct attribute *
45e58e77 20356dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20357{
20358 unsigned int i;
20359
20360 for (i = 0; i < die->num_attrs; ++i)
20361 if (die->attrs[i].name == name)
20362 return &die->attrs[i];
20363
20364 return NULL;
20365}
20366
7d45c7c3
KB
20367/* Return the string associated with a string-typed attribute, or NULL if it
20368 is either not found or is of an incorrect type. */
20369
20370static const char *
20371dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20372{
20373 struct attribute *attr;
20374 const char *str = NULL;
20375
20376 attr = dwarf2_attr (die, name, cu);
20377
20378 if (attr != NULL)
20379 {
43988095 20380 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20381 || attr->form == DW_FORM_string
cf532bd1 20382 || attr->form == DW_FORM_strx
8fe0f950
AT
20383 || attr->form == DW_FORM_strx1
20384 || attr->form == DW_FORM_strx2
20385 || attr->form == DW_FORM_strx3
20386 || attr->form == DW_FORM_strx4
b3340438 20387 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20388 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20389 str = DW_STRING (attr);
20390 else
b98664d3 20391 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20392 "DIE at %s in module %s"),
20393 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20394 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20395 }
20396
20397 return str;
20398}
20399
a084a2a6 20400/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20401 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20402static const char *
20403dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20404{
20405 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20406 if (dwo_name == nullptr)
20407 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20408 return dwo_name;
20409}
20410
05cf31d1
JB
20411/* Return non-zero iff the attribute NAME is defined for the given DIE,
20412 and holds a non-zero value. This function should only be used for
2dc7f7b3 20413 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20414
20415static int
20416dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20417{
20418 struct attribute *attr = dwarf2_attr (die, name, cu);
20419
20420 return (attr && DW_UNSND (attr));
20421}
20422
3ca72b44 20423static int
e142c38c 20424die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20425{
05cf31d1
JB
20426 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20427 which value is non-zero. However, we have to be careful with
20428 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20429 (via dwarf2_flag_true_p) follows this attribute. So we may
20430 end up accidently finding a declaration attribute that belongs
20431 to a different DIE referenced by the specification attribute,
20432 even though the given DIE does not have a declaration attribute. */
20433 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20434 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20435}
20436
63d06c5c 20437/* Return the die giving the specification for DIE, if there is
f2f0e013 20438 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20439 containing the return value on output. If there is no
20440 specification, but there is an abstract origin, that is
20441 returned. */
63d06c5c
DC
20442
20443static struct die_info *
f2f0e013 20444die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20445{
f2f0e013
DJ
20446 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20447 *spec_cu);
63d06c5c 20448
edb3359d
DJ
20449 if (spec_attr == NULL)
20450 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20451
63d06c5c
DC
20452 if (spec_attr == NULL)
20453 return NULL;
20454 else
f2f0e013 20455 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20456}
c906108c 20457
527f3840
JK
20458/* Stub for free_line_header to match void * callback types. */
20459
20460static void
20461free_line_header_voidp (void *arg)
20462{
9a3c8263 20463 struct line_header *lh = (struct line_header *) arg;
527f3840 20464
fff8551c 20465 delete lh;
527f3840
JK
20466}
20467
fff8551c
PA
20468void
20469line_header::add_include_dir (const char *include_dir)
c906108c 20470{
27e0867f 20471 if (dwarf_line_debug >= 2)
7ba99d21
AT
20472 {
20473 size_t new_size;
20474 if (version >= 5)
20475 new_size = m_include_dirs.size ();
20476 else
20477 new_size = m_include_dirs.size () + 1;
20478 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20479 new_size, include_dir);
20480 }
20481 m_include_dirs.push_back (include_dir);
debd256d 20482}
6e70227d 20483
fff8551c
PA
20484void
20485line_header::add_file_name (const char *name,
ecfb656c 20486 dir_index d_index,
fff8551c
PA
20487 unsigned int mod_time,
20488 unsigned int length)
debd256d 20489{
27e0867f 20490 if (dwarf_line_debug >= 2)
7ba99d21
AT
20491 {
20492 size_t new_size;
20493 if (version >= 5)
20494 new_size = file_names_size ();
20495 else
20496 new_size = file_names_size () + 1;
20497 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20498 new_size, name);
20499 }
20500 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20501}
6e70227d 20502
83769d0b 20503/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20504
20505static struct dwarf2_section_info *
20506get_debug_line_section (struct dwarf2_cu *cu)
20507{
20508 struct dwarf2_section_info *section;
518817b3
SM
20509 struct dwarf2_per_objfile *dwarf2_per_objfile
20510 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20511
20512 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20513 DWO file. */
20514 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20515 section = &cu->dwo_unit->dwo_file->sections.line;
20516 else if (cu->per_cu->is_dwz)
20517 {
ed2dc618 20518 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20519
20520 section = &dwz->line;
20521 }
20522 else
20523 section = &dwarf2_per_objfile->line;
20524
20525 return section;
20526}
20527
43988095
JK
20528/* Read directory or file name entry format, starting with byte of
20529 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20530 entries count and the entries themselves in the described entry
20531 format. */
20532
20533static void
ed2dc618
SM
20534read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20535 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20536 struct line_header *lh,
20537 const struct comp_unit_head *cu_header,
20538 void (*callback) (struct line_header *lh,
20539 const char *name,
ecfb656c 20540 dir_index d_index,
43988095
JK
20541 unsigned int mod_time,
20542 unsigned int length))
20543{
20544 gdb_byte format_count, formati;
20545 ULONGEST data_count, datai;
20546 const gdb_byte *buf = *bufp;
20547 const gdb_byte *format_header_data;
43988095
JK
20548 unsigned int bytes_read;
20549
20550 format_count = read_1_byte (abfd, buf);
20551 buf += 1;
20552 format_header_data = buf;
20553 for (formati = 0; formati < format_count; formati++)
20554 {
20555 read_unsigned_leb128 (abfd, buf, &bytes_read);
20556 buf += bytes_read;
20557 read_unsigned_leb128 (abfd, buf, &bytes_read);
20558 buf += bytes_read;
20559 }
20560
20561 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20562 buf += bytes_read;
20563 for (datai = 0; datai < data_count; datai++)
20564 {
20565 const gdb_byte *format = format_header_data;
20566 struct file_entry fe;
20567
43988095
JK
20568 for (formati = 0; formati < format_count; formati++)
20569 {
ecfb656c 20570 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20571 format += bytes_read;
43988095 20572
ecfb656c 20573 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20574 format += bytes_read;
ecfb656c
PA
20575
20576 gdb::optional<const char *> string;
20577 gdb::optional<unsigned int> uint;
20578
43988095
JK
20579 switch (form)
20580 {
20581 case DW_FORM_string:
ecfb656c 20582 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20583 buf += bytes_read;
20584 break;
20585
20586 case DW_FORM_line_strp:
ed2dc618
SM
20587 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20588 abfd, buf,
ecfb656c
PA
20589 cu_header,
20590 &bytes_read));
43988095
JK
20591 buf += bytes_read;
20592 break;
20593
20594 case DW_FORM_data1:
ecfb656c 20595 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20596 buf += 1;
20597 break;
20598
20599 case DW_FORM_data2:
ecfb656c 20600 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20601 buf += 2;
20602 break;
20603
20604 case DW_FORM_data4:
ecfb656c 20605 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20606 buf += 4;
20607 break;
20608
20609 case DW_FORM_data8:
ecfb656c 20610 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20611 buf += 8;
20612 break;
20613
7ba99d21
AT
20614 case DW_FORM_data16:
20615 /* This is used for MD5, but file_entry does not record MD5s. */
20616 buf += 16;
20617 break;
20618
43988095 20619 case DW_FORM_udata:
ecfb656c 20620 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20621 buf += bytes_read;
20622 break;
20623
20624 case DW_FORM_block:
20625 /* It is valid only for DW_LNCT_timestamp which is ignored by
20626 current GDB. */
20627 break;
20628 }
ecfb656c
PA
20629
20630 switch (content_type)
20631 {
20632 case DW_LNCT_path:
20633 if (string.has_value ())
20634 fe.name = *string;
20635 break;
20636 case DW_LNCT_directory_index:
20637 if (uint.has_value ())
20638 fe.d_index = (dir_index) *uint;
20639 break;
20640 case DW_LNCT_timestamp:
20641 if (uint.has_value ())
20642 fe.mod_time = *uint;
20643 break;
20644 case DW_LNCT_size:
20645 if (uint.has_value ())
20646 fe.length = *uint;
20647 break;
20648 case DW_LNCT_MD5:
20649 break;
20650 default:
b98664d3 20651 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20652 pulongest (content_type));
20653 }
43988095
JK
20654 }
20655
ecfb656c 20656 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20657 }
20658
20659 *bufp = buf;
20660}
20661
debd256d 20662/* Read the statement program header starting at OFFSET in
3019eac3 20663 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20664 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20665 Returns NULL if there is a problem reading the header, e.g., if it
20666 has a version we don't understand.
debd256d
JB
20667
20668 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20669 the returned object point into the dwarf line section buffer,
20670 and must not be freed. */
ae2de4f8 20671
fff8551c 20672static line_header_up
9c541725 20673dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20674{
d521ce57 20675 const gdb_byte *line_ptr;
c764a876 20676 unsigned int bytes_read, offset_size;
debd256d 20677 int i;
d521ce57 20678 const char *cur_dir, *cur_file;
3019eac3
DE
20679 struct dwarf2_section_info *section;
20680 bfd *abfd;
518817b3
SM
20681 struct dwarf2_per_objfile *dwarf2_per_objfile
20682 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20683
36586728 20684 section = get_debug_line_section (cu);
3019eac3
DE
20685 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20686 if (section->buffer == NULL)
debd256d 20687 {
3019eac3 20688 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20689 complaint (_("missing .debug_line.dwo section"));
3019eac3 20690 else
b98664d3 20691 complaint (_("missing .debug_line section"));
debd256d
JB
20692 return 0;
20693 }
20694
fceca515
DE
20695 /* We can't do this until we know the section is non-empty.
20696 Only then do we know we have such a section. */
a32a8923 20697 abfd = get_section_bfd_owner (section);
fceca515 20698
a738430d
MK
20699 /* Make sure that at least there's room for the total_length field.
20700 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20701 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20702 {
4d3c2250 20703 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20704 return 0;
20705 }
20706
fff8551c 20707 line_header_up lh (new line_header ());
debd256d 20708
9c541725 20709 lh->sect_off = sect_off;
527f3840
JK
20710 lh->offset_in_dwz = cu->per_cu->is_dwz;
20711
9c541725 20712 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20713
a738430d 20714 /* Read in the header. */
6e70227d 20715 lh->total_length =
c764a876
DE
20716 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20717 &bytes_read, &offset_size);
debd256d 20718 line_ptr += bytes_read;
7ba99d21
AT
20719
20720 const gdb_byte *start_here = line_ptr;
20721
3019eac3 20722 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20723 {
4d3c2250 20724 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20725 return 0;
20726 }
7ba99d21 20727 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20728 lh->version = read_2_bytes (abfd, line_ptr);
20729 line_ptr += 2;
43988095 20730 if (lh->version > 5)
cd366ee8
DE
20731 {
20732 /* This is a version we don't understand. The format could have
20733 changed in ways we don't handle properly so just punt. */
b98664d3 20734 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20735 return NULL;
20736 }
43988095
JK
20737 if (lh->version >= 5)
20738 {
20739 gdb_byte segment_selector_size;
20740
20741 /* Skip address size. */
20742 read_1_byte (abfd, line_ptr);
20743 line_ptr += 1;
20744
20745 segment_selector_size = read_1_byte (abfd, line_ptr);
20746 line_ptr += 1;
20747 if (segment_selector_size != 0)
20748 {
b98664d3 20749 complaint (_("unsupported segment selector size %u "
43988095
JK
20750 "in .debug_line section"),
20751 segment_selector_size);
20752 return NULL;
20753 }
20754 }
c764a876
DE
20755 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20756 line_ptr += offset_size;
7ba99d21 20757 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20758 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20759 line_ptr += 1;
2dc7f7b3
TT
20760 if (lh->version >= 4)
20761 {
20762 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20763 line_ptr += 1;
20764 }
20765 else
20766 lh->maximum_ops_per_instruction = 1;
20767
20768 if (lh->maximum_ops_per_instruction == 0)
20769 {
20770 lh->maximum_ops_per_instruction = 1;
b98664d3 20771 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20772 "in `.debug_line' section"));
2dc7f7b3
TT
20773 }
20774
debd256d
JB
20775 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20776 line_ptr += 1;
20777 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20778 line_ptr += 1;
20779 lh->line_range = read_1_byte (abfd, line_ptr);
20780 line_ptr += 1;
20781 lh->opcode_base = read_1_byte (abfd, line_ptr);
20782 line_ptr += 1;
fff8551c 20783 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20784
20785 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20786 for (i = 1; i < lh->opcode_base; ++i)
20787 {
20788 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20789 line_ptr += 1;
20790 }
20791
43988095 20792 if (lh->version >= 5)
debd256d 20793 {
43988095 20794 /* Read directory table. */
ed2dc618
SM
20795 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20796 &cu->header,
b926417a 20797 [] (struct line_header *header, const char *name,
ecfb656c 20798 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20799 unsigned int length)
20800 {
b926417a 20801 header->add_include_dir (name);
fff8551c 20802 });
debd256d 20803
43988095 20804 /* Read file name table. */
ed2dc618
SM
20805 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20806 &cu->header,
b926417a 20807 [] (struct line_header *header, const char *name,
ecfb656c 20808 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20809 unsigned int length)
20810 {
b926417a 20811 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20812 });
43988095
JK
20813 }
20814 else
debd256d 20815 {
43988095
JK
20816 /* Read directory table. */
20817 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20818 {
20819 line_ptr += bytes_read;
fff8551c 20820 lh->add_include_dir (cur_dir);
43988095 20821 }
debd256d
JB
20822 line_ptr += bytes_read;
20823
43988095
JK
20824 /* Read file name table. */
20825 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20826 {
ecfb656c
PA
20827 unsigned int mod_time, length;
20828 dir_index d_index;
43988095
JK
20829
20830 line_ptr += bytes_read;
ecfb656c 20831 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20832 line_ptr += bytes_read;
20833 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20834 line_ptr += bytes_read;
20835 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20836 line_ptr += bytes_read;
20837
ecfb656c 20838 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20839 }
20840 line_ptr += bytes_read;
debd256d 20841 }
debd256d 20842
3019eac3 20843 if (line_ptr > (section->buffer + section->size))
b98664d3 20844 complaint (_("line number info header doesn't "
3e43a32a 20845 "fit in `.debug_line' section"));
debd256d 20846
debd256d
JB
20847 return lh;
20848}
c906108c 20849
c6da4cef 20850/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20851 Return the file name of the psymtab for the given file_entry.
c6da4cef 20852 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20853 If space for the result is malloc'd, *NAME_HOLDER will be set.
20854 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20855
d521ce57 20856static const char *
7ba99d21 20857psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
c6da4cef 20858 const struct partial_symtab *pst,
c89b44cd
TT
20859 const char *comp_dir,
20860 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20861{
d521ce57
TT
20862 const char *include_name = fe.name;
20863 const char *include_name_to_compare = include_name;
72b9f47f 20864 const char *pst_filename;
c6da4cef
DE
20865 int file_is_pst;
20866
8c43009f 20867 const char *dir_name = fe.include_dir (lh);
c6da4cef 20868
c89b44cd 20869 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20870 if (!IS_ABSOLUTE_PATH (include_name)
20871 && (dir_name != NULL || comp_dir != NULL))
20872 {
20873 /* Avoid creating a duplicate psymtab for PST.
20874 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20875 Before we do the comparison, however, we need to account
20876 for DIR_NAME and COMP_DIR.
20877 First prepend dir_name (if non-NULL). If we still don't
20878 have an absolute path prepend comp_dir (if non-NULL).
20879 However, the directory we record in the include-file's
20880 psymtab does not contain COMP_DIR (to match the
20881 corresponding symtab(s)).
20882
20883 Example:
20884
20885 bash$ cd /tmp
20886 bash$ gcc -g ./hello.c
20887 include_name = "hello.c"
20888 dir_name = "."
20889 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20890 DW_AT_name = "./hello.c"
20891
20892 */
c6da4cef
DE
20893
20894 if (dir_name != NULL)
20895 {
c89b44cd
TT
20896 name_holder->reset (concat (dir_name, SLASH_STRING,
20897 include_name, (char *) NULL));
20898 include_name = name_holder->get ();
c6da4cef 20899 include_name_to_compare = include_name;
c6da4cef
DE
20900 }
20901 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20902 {
c89b44cd
TT
20903 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20904 include_name, (char *) NULL));
20905 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20906 }
20907 }
20908
20909 pst_filename = pst->filename;
c89b44cd 20910 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20911 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20912 {
c89b44cd
TT
20913 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20914 pst_filename, (char *) NULL));
20915 pst_filename = copied_name.get ();
c6da4cef
DE
20916 }
20917
1e3fad37 20918 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20919
c6da4cef
DE
20920 if (file_is_pst)
20921 return NULL;
20922 return include_name;
20923}
20924
d9b3de22
DE
20925/* State machine to track the state of the line number program. */
20926
6f77053d 20927class lnp_state_machine
d9b3de22 20928{
6f77053d
PA
20929public:
20930 /* Initialize a machine state for the start of a line number
20931 program. */
804d2729
TT
20932 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20933 bool record_lines_p);
6f77053d 20934
8c43009f
PA
20935 file_entry *current_file ()
20936 {
20937 /* lh->file_names is 0-based, but the file name numbers in the
20938 statement program are 1-based. */
6f77053d
PA
20939 return m_line_header->file_name_at (m_file);
20940 }
20941
20942 /* Record the line in the state machine. END_SEQUENCE is true if
20943 we're processing the end of a sequence. */
20944 void record_line (bool end_sequence);
20945
7ab6656f
OJ
20946 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20947 nop-out rest of the lines in this sequence. */
6f77053d
PA
20948 void check_line_address (struct dwarf2_cu *cu,
20949 const gdb_byte *line_ptr,
7ab6656f 20950 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20951
20952 void handle_set_discriminator (unsigned int discriminator)
20953 {
20954 m_discriminator = discriminator;
20955 m_line_has_non_zero_discriminator |= discriminator != 0;
20956 }
20957
20958 /* Handle DW_LNE_set_address. */
20959 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20960 {
20961 m_op_index = 0;
20962 address += baseaddr;
20963 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20964 }
20965
20966 /* Handle DW_LNS_advance_pc. */
20967 void handle_advance_pc (CORE_ADDR adjust);
20968
20969 /* Handle a special opcode. */
20970 void handle_special_opcode (unsigned char op_code);
20971
20972 /* Handle DW_LNS_advance_line. */
20973 void handle_advance_line (int line_delta)
20974 {
20975 advance_line (line_delta);
20976 }
20977
20978 /* Handle DW_LNS_set_file. */
20979 void handle_set_file (file_name_index file);
20980
20981 /* Handle DW_LNS_negate_stmt. */
20982 void handle_negate_stmt ()
20983 {
20984 m_is_stmt = !m_is_stmt;
20985 }
20986
20987 /* Handle DW_LNS_const_add_pc. */
20988 void handle_const_add_pc ();
20989
20990 /* Handle DW_LNS_fixed_advance_pc. */
20991 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20992 {
20993 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20994 m_op_index = 0;
20995 }
20996
20997 /* Handle DW_LNS_copy. */
20998 void handle_copy ()
20999 {
21000 record_line (false);
21001 m_discriminator = 0;
21002 }
21003
21004 /* Handle DW_LNE_end_sequence. */
21005 void handle_end_sequence ()
21006 {
804d2729 21007 m_currently_recording_lines = true;
6f77053d
PA
21008 }
21009
21010private:
21011 /* Advance the line by LINE_DELTA. */
21012 void advance_line (int line_delta)
21013 {
21014 m_line += line_delta;
21015
21016 if (line_delta != 0)
21017 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21018 }
21019
804d2729
TT
21020 struct dwarf2_cu *m_cu;
21021
6f77053d
PA
21022 gdbarch *m_gdbarch;
21023
21024 /* True if we're recording lines.
21025 Otherwise we're building partial symtabs and are just interested in
21026 finding include files mentioned by the line number program. */
21027 bool m_record_lines_p;
21028
8c43009f 21029 /* The line number header. */
6f77053d 21030 line_header *m_line_header;
8c43009f 21031
6f77053d
PA
21032 /* These are part of the standard DWARF line number state machine,
21033 and initialized according to the DWARF spec. */
d9b3de22 21034
6f77053d 21035 unsigned char m_op_index = 0;
7ba99d21
AT
21036 /* The line table index of the current file. */
21037 file_name_index m_file = 1;
6f77053d
PA
21038 unsigned int m_line = 1;
21039
21040 /* These are initialized in the constructor. */
21041
21042 CORE_ADDR m_address;
21043 bool m_is_stmt;
21044 unsigned int m_discriminator;
d9b3de22
DE
21045
21046 /* Additional bits of state we need to track. */
21047
21048 /* The last file that we called dwarf2_start_subfile for.
21049 This is only used for TLLs. */
6f77053d 21050 unsigned int m_last_file = 0;
d9b3de22 21051 /* The last file a line number was recorded for. */
6f77053d 21052 struct subfile *m_last_subfile = NULL;
d9b3de22 21053
804d2729
TT
21054 /* When true, record the lines we decode. */
21055 bool m_currently_recording_lines = false;
d9b3de22
DE
21056
21057 /* The last line number that was recorded, used to coalesce
21058 consecutive entries for the same line. This can happen, for
21059 example, when discriminators are present. PR 17276. */
6f77053d
PA
21060 unsigned int m_last_line = 0;
21061 bool m_line_has_non_zero_discriminator = false;
8c43009f 21062};
d9b3de22 21063
6f77053d
PA
21064void
21065lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21066{
21067 CORE_ADDR addr_adj = (((m_op_index + adjust)
21068 / m_line_header->maximum_ops_per_instruction)
21069 * m_line_header->minimum_instruction_length);
21070 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21071 m_op_index = ((m_op_index + adjust)
21072 % m_line_header->maximum_ops_per_instruction);
21073}
d9b3de22 21074
6f77053d
PA
21075void
21076lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21077{
6f77053d
PA
21078 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21079 CORE_ADDR addr_adj = (((m_op_index
21080 + (adj_opcode / m_line_header->line_range))
21081 / m_line_header->maximum_ops_per_instruction)
21082 * m_line_header->minimum_instruction_length);
21083 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21084 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21085 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21086
6f77053d
PA
21087 int line_delta = (m_line_header->line_base
21088 + (adj_opcode % m_line_header->line_range));
21089 advance_line (line_delta);
21090 record_line (false);
21091 m_discriminator = 0;
21092}
d9b3de22 21093
6f77053d
PA
21094void
21095lnp_state_machine::handle_set_file (file_name_index file)
21096{
21097 m_file = file;
21098
21099 const file_entry *fe = current_file ();
21100 if (fe == NULL)
21101 dwarf2_debug_line_missing_file_complaint ();
21102 else if (m_record_lines_p)
21103 {
21104 const char *dir = fe->include_dir (m_line_header);
21105
c24bdb02 21106 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21107 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21108 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21109 }
21110}
21111
21112void
21113lnp_state_machine::handle_const_add_pc ()
21114{
21115 CORE_ADDR adjust
21116 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21117
21118 CORE_ADDR addr_adj
21119 = (((m_op_index + adjust)
21120 / m_line_header->maximum_ops_per_instruction)
21121 * m_line_header->minimum_instruction_length);
21122
21123 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21124 m_op_index = ((m_op_index + adjust)
21125 % m_line_header->maximum_ops_per_instruction);
21126}
d9b3de22 21127
a05a36a5
DE
21128/* Return non-zero if we should add LINE to the line number table.
21129 LINE is the line to add, LAST_LINE is the last line that was added,
21130 LAST_SUBFILE is the subfile for LAST_LINE.
21131 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21132 had a non-zero discriminator.
21133
21134 We have to be careful in the presence of discriminators.
21135 E.g., for this line:
21136
21137 for (i = 0; i < 100000; i++);
21138
21139 clang can emit four line number entries for that one line,
21140 each with a different discriminator.
21141 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21142
21143 However, we want gdb to coalesce all four entries into one.
21144 Otherwise the user could stepi into the middle of the line and
21145 gdb would get confused about whether the pc really was in the
21146 middle of the line.
21147
21148 Things are further complicated by the fact that two consecutive
21149 line number entries for the same line is a heuristic used by gcc
21150 to denote the end of the prologue. So we can't just discard duplicate
21151 entries, we have to be selective about it. The heuristic we use is
21152 that we only collapse consecutive entries for the same line if at least
21153 one of those entries has a non-zero discriminator. PR 17276.
21154
21155 Note: Addresses in the line number state machine can never go backwards
21156 within one sequence, thus this coalescing is ok. */
21157
21158static int
804d2729
TT
21159dwarf_record_line_p (struct dwarf2_cu *cu,
21160 unsigned int line, unsigned int last_line,
a05a36a5
DE
21161 int line_has_non_zero_discriminator,
21162 struct subfile *last_subfile)
21163{
c24bdb02 21164 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21165 return 1;
21166 if (line != last_line)
21167 return 1;
21168 /* Same line for the same file that we've seen already.
21169 As a last check, for pr 17276, only record the line if the line
21170 has never had a non-zero discriminator. */
21171 if (!line_has_non_zero_discriminator)
21172 return 1;
21173 return 0;
21174}
21175
804d2729
TT
21176/* Use the CU's builder to record line number LINE beginning at
21177 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21178
21179static void
d9b3de22
DE
21180dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21181 unsigned int line, CORE_ADDR address,
804d2729 21182 struct dwarf2_cu *cu)
252a6764
DE
21183{
21184 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21185
27e0867f
DE
21186 if (dwarf_line_debug)
21187 {
21188 fprintf_unfiltered (gdb_stdlog,
21189 "Recording line %u, file %s, address %s\n",
21190 line, lbasename (subfile->name),
21191 paddress (gdbarch, address));
21192 }
21193
804d2729 21194 if (cu != nullptr)
c24bdb02 21195 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21196}
21197
21198/* Subroutine of dwarf_decode_lines_1 to simplify it.
21199 Mark the end of a set of line number records.
d9b3de22 21200 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21201 If SUBFILE is NULL the request is ignored. */
21202
21203static void
21204dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21205 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21206{
27e0867f
DE
21207 if (subfile == NULL)
21208 return;
21209
21210 if (dwarf_line_debug)
21211 {
21212 fprintf_unfiltered (gdb_stdlog,
21213 "Finishing current line, file %s, address %s\n",
21214 lbasename (subfile->name),
21215 paddress (gdbarch, address));
21216 }
21217
804d2729 21218 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21219}
21220
6f77053d
PA
21221void
21222lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21223{
d9b3de22
DE
21224 if (dwarf_line_debug)
21225 {
21226 fprintf_unfiltered (gdb_stdlog,
21227 "Processing actual line %u: file %u,"
21228 " address %s, is_stmt %u, discrim %u\n",
7ba99d21 21229 m_line, m_file,
6f77053d
PA
21230 paddress (m_gdbarch, m_address),
21231 m_is_stmt, m_discriminator);
d9b3de22
DE
21232 }
21233
6f77053d 21234 file_entry *fe = current_file ();
8c43009f
PA
21235
21236 if (fe == NULL)
d9b3de22
DE
21237 dwarf2_debug_line_missing_file_complaint ();
21238 /* For now we ignore lines not starting on an instruction boundary.
21239 But not when processing end_sequence for compatibility with the
21240 previous version of the code. */
6f77053d 21241 else if (m_op_index == 0 || end_sequence)
d9b3de22 21242 {
8c43009f 21243 fe->included_p = 1;
c258c396 21244 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21245 {
c24bdb02 21246 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21247 || end_sequence)
d9b3de22 21248 {
804d2729
TT
21249 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21250 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21251 }
21252
21253 if (!end_sequence)
21254 {
804d2729 21255 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21256 m_line_has_non_zero_discriminator,
21257 m_last_subfile))
d9b3de22 21258 {
c24bdb02 21259 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21260 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21261 builder->get_current_subfile (),
6f77053d 21262 m_line, m_address,
804d2729 21263 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21264 }
c24bdb02 21265 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21266 m_last_line = m_line;
d9b3de22
DE
21267 }
21268 }
21269 }
21270}
21271
804d2729
TT
21272lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21273 line_header *lh, bool record_lines_p)
d9b3de22 21274{
804d2729 21275 m_cu = cu;
6f77053d
PA
21276 m_gdbarch = arch;
21277 m_record_lines_p = record_lines_p;
21278 m_line_header = lh;
d9b3de22 21279
804d2729 21280 m_currently_recording_lines = true;
d9b3de22 21281
d9b3de22
DE
21282 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21283 was a line entry for it so that the backend has a chance to adjust it
21284 and also record it in case it needs it. This is currently used by MIPS
21285 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21286 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21287 m_is_stmt = lh->default_is_stmt;
21288 m_discriminator = 0;
252a6764
DE
21289}
21290
6f77053d
PA
21291void
21292lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21293 const gdb_byte *line_ptr,
7ab6656f 21294 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21295{
7ab6656f
OJ
21296 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21297 the pc range of the CU. However, we restrict the test to only ADDRESS
21298 values of zero to preserve GDB's previous behaviour which is to handle
21299 the specific case of a function being GC'd by the linker. */
924c2928 21300
7ab6656f 21301 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21302 {
21303 /* This line table is for a function which has been
21304 GCd by the linker. Ignore it. PR gdb/12528 */
21305
518817b3 21306 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21307 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21308
b98664d3 21309 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21310 line_offset, objfile_name (objfile));
804d2729
TT
21311 m_currently_recording_lines = false;
21312 /* Note: m_currently_recording_lines is left as false until we see
21313 DW_LNE_end_sequence. */
924c2928
DE
21314 }
21315}
21316
f3f5162e 21317/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21318 Process the line number information in LH.
21319 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21320 program in order to set included_p for every referenced header. */
debd256d 21321
c906108c 21322static void
43f3e411
DE
21323dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21324 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21325{
d521ce57
TT
21326 const gdb_byte *line_ptr, *extended_end;
21327 const gdb_byte *line_end;
a8c50c1f 21328 unsigned int bytes_read, extended_len;
699ca60a 21329 unsigned char op_code, extended_op;
e142c38c 21330 CORE_ADDR baseaddr;
518817b3 21331 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21332 bfd *abfd = objfile->obfd;
fbf65064 21333 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21334 /* True if we're recording line info (as opposed to building partial
21335 symtabs and just interested in finding include files mentioned by
21336 the line number program). */
21337 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21338
21339 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21340
debd256d
JB
21341 line_ptr = lh->statement_program_start;
21342 line_end = lh->statement_program_end;
c906108c
SS
21343
21344 /* Read the statement sequences until there's nothing left. */
21345 while (line_ptr < line_end)
21346 {
6f77053d
PA
21347 /* The DWARF line number program state machine. Reset the state
21348 machine at the start of each sequence. */
804d2729 21349 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21350 bool end_sequence = false;
d9b3de22 21351
8c43009f 21352 if (record_lines_p)
c906108c 21353 {
8c43009f
PA
21354 /* Start a subfile for the current file of the state
21355 machine. */
21356 const file_entry *fe = state_machine.current_file ();
21357
21358 if (fe != NULL)
804d2729 21359 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21360 }
21361
a738430d 21362 /* Decode the table. */
d9b3de22 21363 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21364 {
21365 op_code = read_1_byte (abfd, line_ptr);
21366 line_ptr += 1;
9aa1fe7e 21367
debd256d 21368 if (op_code >= lh->opcode_base)
6e70227d 21369 {
8e07a239 21370 /* Special opcode. */
6f77053d 21371 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21372 }
21373 else switch (op_code)
c906108c
SS
21374 {
21375 case DW_LNS_extended_op:
3e43a32a
MS
21376 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21377 &bytes_read);
473b7be6 21378 line_ptr += bytes_read;
a8c50c1f 21379 extended_end = line_ptr + extended_len;
c906108c
SS
21380 extended_op = read_1_byte (abfd, line_ptr);
21381 line_ptr += 1;
21382 switch (extended_op)
21383 {
21384 case DW_LNE_end_sequence:
6f77053d
PA
21385 state_machine.handle_end_sequence ();
21386 end_sequence = true;
c906108c
SS
21387 break;
21388 case DW_LNE_set_address:
d9b3de22
DE
21389 {
21390 CORE_ADDR address
21391 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21392 line_ptr += bytes_read;
6f77053d
PA
21393
21394 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21395 lowpc - baseaddr, address);
6f77053d 21396 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21397 }
c906108c
SS
21398 break;
21399 case DW_LNE_define_file:
debd256d 21400 {
d521ce57 21401 const char *cur_file;
ecfb656c
PA
21402 unsigned int mod_time, length;
21403 dir_index dindex;
6e70227d 21404
3e43a32a
MS
21405 cur_file = read_direct_string (abfd, line_ptr,
21406 &bytes_read);
debd256d 21407 line_ptr += bytes_read;
ecfb656c 21408 dindex = (dir_index)
debd256d
JB
21409 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21410 line_ptr += bytes_read;
21411 mod_time =
21412 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21413 line_ptr += bytes_read;
21414 length =
21415 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21416 line_ptr += bytes_read;
ecfb656c 21417 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21418 }
c906108c 21419 break;
d0c6ba3d 21420 case DW_LNE_set_discriminator:
6f77053d
PA
21421 {
21422 /* The discriminator is not interesting to the
21423 debugger; just ignore it. We still need to
21424 check its value though:
21425 if there are consecutive entries for the same
21426 (non-prologue) line we want to coalesce them.
21427 PR 17276. */
21428 unsigned int discr
21429 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21430 line_ptr += bytes_read;
21431
21432 state_machine.handle_set_discriminator (discr);
21433 }
d0c6ba3d 21434 break;
c906108c 21435 default:
b98664d3 21436 complaint (_("mangled .debug_line section"));
debd256d 21437 return;
c906108c 21438 }
a8c50c1f
DJ
21439 /* Make sure that we parsed the extended op correctly. If e.g.
21440 we expected a different address size than the producer used,
21441 we may have read the wrong number of bytes. */
21442 if (line_ptr != extended_end)
21443 {
b98664d3 21444 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21445 return;
21446 }
c906108c
SS
21447 break;
21448 case DW_LNS_copy:
6f77053d 21449 state_machine.handle_copy ();
c906108c
SS
21450 break;
21451 case DW_LNS_advance_pc:
2dc7f7b3
TT
21452 {
21453 CORE_ADDR adjust
21454 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21455 line_ptr += bytes_read;
6f77053d
PA
21456
21457 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21458 }
c906108c
SS
21459 break;
21460 case DW_LNS_advance_line:
a05a36a5
DE
21461 {
21462 int line_delta
21463 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21464 line_ptr += bytes_read;
6f77053d
PA
21465
21466 state_machine.handle_advance_line (line_delta);
a05a36a5 21467 }
c906108c
SS
21468 break;
21469 case DW_LNS_set_file:
d9b3de22 21470 {
6f77053d 21471 file_name_index file
ecfb656c
PA
21472 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21473 &bytes_read);
d9b3de22 21474 line_ptr += bytes_read;
8c43009f 21475
6f77053d 21476 state_machine.handle_set_file (file);
d9b3de22 21477 }
c906108c
SS
21478 break;
21479 case DW_LNS_set_column:
0ad93d4f 21480 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21481 line_ptr += bytes_read;
21482 break;
21483 case DW_LNS_negate_stmt:
6f77053d 21484 state_machine.handle_negate_stmt ();
c906108c
SS
21485 break;
21486 case DW_LNS_set_basic_block:
c906108c 21487 break;
c2c6d25f
JM
21488 /* Add to the address register of the state machine the
21489 address increment value corresponding to special opcode
a738430d
MK
21490 255. I.e., this value is scaled by the minimum
21491 instruction length since special opcode 255 would have
b021a221 21492 scaled the increment. */
c906108c 21493 case DW_LNS_const_add_pc:
6f77053d 21494 state_machine.handle_const_add_pc ();
c906108c
SS
21495 break;
21496 case DW_LNS_fixed_advance_pc:
3e29f34a 21497 {
6f77053d 21498 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21499 line_ptr += 2;
6f77053d
PA
21500
21501 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21502 }
c906108c 21503 break;
9aa1fe7e 21504 default:
a738430d
MK
21505 {
21506 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21507 int i;
a738430d 21508
debd256d 21509 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21510 {
21511 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21512 line_ptr += bytes_read;
21513 }
21514 }
c906108c
SS
21515 }
21516 }
d9b3de22
DE
21517
21518 if (!end_sequence)
21519 dwarf2_debug_line_missing_end_sequence_complaint ();
21520
21521 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21522 in which case we still finish recording the last line). */
6f77053d 21523 state_machine.record_line (true);
c906108c 21524 }
f3f5162e
DE
21525}
21526
21527/* Decode the Line Number Program (LNP) for the given line_header
21528 structure and CU. The actual information extracted and the type
21529 of structures created from the LNP depends on the value of PST.
21530
21531 1. If PST is NULL, then this procedure uses the data from the program
21532 to create all necessary symbol tables, and their linetables.
21533
21534 2. If PST is not NULL, this procedure reads the program to determine
21535 the list of files included by the unit represented by PST, and
21536 builds all the associated partial symbol tables.
21537
21538 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21539 It is used for relative paths in the line table.
21540 NOTE: When processing partial symtabs (pst != NULL),
21541 comp_dir == pst->dirname.
21542
21543 NOTE: It is important that psymtabs have the same file name (via strcmp)
21544 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21545 symtab we don't use it in the name of the psymtabs we create.
21546 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21547 A good testcase for this is mb-inline.exp.
21548
527f3840
JK
21549 LOWPC is the lowest address in CU (or 0 if not known).
21550
21551 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21552 for its PC<->lines mapping information. Otherwise only the filename
21553 table is read in. */
f3f5162e
DE
21554
21555static void
21556dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21557 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21558 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21559{
518817b3 21560 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21561 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21562
527f3840
JK
21563 if (decode_mapping)
21564 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21565
21566 if (decode_for_pst_p)
21567 {
aaa75496
JB
21568 /* Now that we're done scanning the Line Header Program, we can
21569 create the psymtab of each included file. */
7ba99d21
AT
21570 for (auto &file_entry : lh->file_names ())
21571 if (file_entry.included_p == 1)
aaa75496 21572 {
c89b44cd 21573 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21574 const char *include_name =
7ba99d21
AT
21575 psymtab_include_file_name (lh, file_entry, pst,
21576 comp_dir, &name_holder);
c6da4cef 21577 if (include_name != NULL)
aaa75496
JB
21578 dwarf2_create_include_psymtab (include_name, pst, objfile);
21579 }
21580 }
cb1df416
DJ
21581 else
21582 {
21583 /* Make sure a symtab is created for every file, even files
21584 which contain only variables (i.e. no code with associated
21585 line numbers). */
c24bdb02
KS
21586 buildsym_compunit *builder = cu->get_builder ();
21587 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21588
7ba99d21 21589 for (auto &fe : lh->file_names ())
cb1df416 21590 {
804d2729 21591 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21592 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21593 {
c24bdb02 21594 builder->get_current_subfile ()->symtab
804d2729 21595 = allocate_symtab (cust,
c24bdb02 21596 builder->get_current_subfile ()->name);
43f3e411 21597 }
c24bdb02 21598 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21599 }
21600 }
c906108c
SS
21601}
21602
21603/* Start a subfile for DWARF. FILENAME is the name of the file and
21604 DIRNAME the name of the source directory which contains FILENAME
4d663531 21605 or NULL if not known.
c906108c
SS
21606 This routine tries to keep line numbers from identical absolute and
21607 relative file names in a common subfile.
21608
21609 Using the `list' example from the GDB testsuite, which resides in
21610 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21611 of /srcdir/list0.c yields the following debugging information for list0.c:
21612
c5aa993b 21613 DW_AT_name: /srcdir/list0.c
4d663531 21614 DW_AT_comp_dir: /compdir
357e46e7 21615 files.files[0].name: list0.h
c5aa993b 21616 files.files[0].dir: /srcdir
357e46e7 21617 files.files[1].name: list0.c
c5aa993b 21618 files.files[1].dir: /srcdir
c906108c
SS
21619
21620 The line number information for list0.c has to end up in a single
4f1520fb
FR
21621 subfile, so that `break /srcdir/list0.c:1' works as expected.
21622 start_subfile will ensure that this happens provided that we pass the
21623 concatenation of files.files[1].dir and files.files[1].name as the
21624 subfile's name. */
c906108c
SS
21625
21626static void
804d2729
TT
21627dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21628 const char *dirname)
c906108c 21629{
d521ce57 21630 char *copy = NULL;
4f1520fb 21631
4d663531 21632 /* In order not to lose the line information directory,
4f1520fb
FR
21633 we concatenate it to the filename when it makes sense.
21634 Note that the Dwarf3 standard says (speaking of filenames in line
21635 information): ``The directory index is ignored for file names
21636 that represent full path names''. Thus ignoring dirname in the
21637 `else' branch below isn't an issue. */
c906108c 21638
d5166ae1 21639 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21640 {
21641 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21642 filename = copy;
21643 }
c906108c 21644
c24bdb02 21645 cu->get_builder ()->start_subfile (filename);
4f1520fb 21646
d521ce57
TT
21647 if (copy != NULL)
21648 xfree (copy);
c906108c
SS
21649}
21650
804d2729
TT
21651/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21652 buildsym_compunit constructor. */
f4dc4d17 21653
c24bdb02
KS
21654struct compunit_symtab *
21655dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21656 CORE_ADDR low_pc)
f4dc4d17 21657{
c24bdb02 21658 gdb_assert (m_builder == nullptr);
43f3e411 21659
c24bdb02
KS
21660 m_builder.reset (new struct buildsym_compunit
21661 (per_cu->dwarf2_per_objfile->objfile,
21662 name, comp_dir, language, low_pc));
93b8bea4 21663
c24bdb02 21664 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21665
c24bdb02
KS
21666 get_builder ()->record_debugformat ("DWARF 2");
21667 get_builder ()->record_producer (producer);
f4dc4d17 21668
c24bdb02 21669 processing_has_namespace_info = false;
43f3e411 21670
c24bdb02 21671 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21672}
21673
4c2df51b
DJ
21674static void
21675var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21676 struct dwarf2_cu *cu)
4c2df51b 21677{
518817b3 21678 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21679 struct comp_unit_head *cu_header = &cu->header;
21680
4c2df51b
DJ
21681 /* NOTE drow/2003-01-30: There used to be a comment and some special
21682 code here to turn a symbol with DW_AT_external and a
21683 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21684 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21685 with some versions of binutils) where shared libraries could have
21686 relocations against symbols in their debug information - the
21687 minimal symbol would have the right address, but the debug info
21688 would not. It's no longer necessary, because we will explicitly
21689 apply relocations when we read in the debug information now. */
21690
21691 /* A DW_AT_location attribute with no contents indicates that a
21692 variable has been optimized away. */
21693 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21694 {
f1e6e072 21695 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21696 return;
21697 }
21698
21699 /* Handle one degenerate form of location expression specially, to
21700 preserve GDB's previous behavior when section offsets are
336d760d
AT
21701 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21702 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21703
21704 if (attr_form_is_block (attr)
3019eac3
DE
21705 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21706 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21707 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21708 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21709 && (DW_BLOCK (attr)->size
21710 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21711 {
891d2f0b 21712 unsigned int dummy;
4c2df51b 21713
3019eac3 21714 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21715 SET_SYMBOL_VALUE_ADDRESS (sym,
21716 read_address (objfile->obfd,
21717 DW_BLOCK (attr)->data + 1,
21718 cu, &dummy));
3019eac3 21719 else
38583298
TT
21720 SET_SYMBOL_VALUE_ADDRESS
21721 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21722 &dummy));
f1e6e072 21723 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21724 fixup_symbol_section (sym, objfile);
38583298
TT
21725 SET_SYMBOL_VALUE_ADDRESS (sym,
21726 SYMBOL_VALUE_ADDRESS (sym)
21727 + ANOFFSET (objfile->section_offsets,
21728 SYMBOL_SECTION (sym)));
4c2df51b
DJ
21729 return;
21730 }
21731
21732 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21733 expression evaluator, and use LOC_COMPUTED only when necessary
21734 (i.e. when the value of a register or memory location is
21735 referenced, or a thread-local block, etc.). Then again, it might
21736 not be worthwhile. I'm assuming that it isn't unless performance
21737 or memory numbers show me otherwise. */
21738
f1e6e072 21739 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21740
f1e6e072 21741 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21742 cu->has_loclist = true;
4c2df51b
DJ
21743}
21744
c906108c
SS
21745/* Given a pointer to a DWARF information entry, figure out if we need
21746 to make a symbol table entry for it, and if so, create a new entry
21747 and return a pointer to it.
21748 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21749 used the passed type.
21750 If SPACE is not NULL, use it to hold the new symbol. If it is
21751 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21752
21753static struct symbol *
5e2db402
TT
21754new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21755 struct symbol *space)
c906108c 21756{
518817b3
SM
21757 struct dwarf2_per_objfile *dwarf2_per_objfile
21758 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21759 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21760 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21761 struct symbol *sym = NULL;
15d034d0 21762 const char *name;
c906108c
SS
21763 struct attribute *attr = NULL;
21764 struct attribute *attr2 = NULL;
e142c38c 21765 CORE_ADDR baseaddr;
e37fd15a
SW
21766 struct pending **list_to_add = NULL;
21767
edb3359d 21768 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21769
21770 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21771
94af9270 21772 name = dwarf2_name (die, cu);
c906108c
SS
21773 if (name)
21774 {
94af9270 21775 const char *linkagename;
34eaf542 21776 int suppress_add = 0;
94af9270 21777
34eaf542
TT
21778 if (space)
21779 sym = space;
21780 else
e623cf5d 21781 sym = allocate_symbol (objfile);
c906108c 21782 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21783
21784 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21785 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 21786 linkagename = dwarf2_physname (name, die, cu);
31edb802 21787 SYMBOL_SET_NAMES (sym, linkagename, false, objfile);
c906108c 21788
f55ee35c
JK
21789 /* Fortran does not have mangling standard and the mangling does differ
21790 between gfortran, iFort etc. */
21791 if (cu->language == language_fortran
468c0cbb
CB
21792 && symbol_get_demangled_name (sym) == NULL)
21793 symbol_set_demangled_name (sym,
cfc594ee 21794 dwarf2_full_name (name, die, cu),
29df156d 21795 NULL);
f55ee35c 21796
c906108c 21797 /* Default assumptions.
c5aa993b 21798 Use the passed type or decode it from the die. */
176620f1 21799 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21800 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21801 if (type != NULL)
21802 SYMBOL_TYPE (sym) = type;
21803 else
e7c27a73 21804 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21805 attr = dwarf2_attr (die,
21806 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21807 cu);
435d3d88 21808 if (attr != nullptr)
c906108c
SS
21809 {
21810 SYMBOL_LINE (sym) = DW_UNSND (attr);
21811 }
cb1df416 21812
edb3359d
DJ
21813 attr = dwarf2_attr (die,
21814 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21815 cu);
435d3d88 21816 if (attr != nullptr)
cb1df416 21817 {
ecfb656c 21818 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21819 struct file_entry *fe;
9a619af0 21820
ecfb656c
PA
21821 if (cu->line_header != NULL)
21822 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21823 else
21824 fe = NULL;
21825
21826 if (fe == NULL)
b98664d3 21827 complaint (_("file index out of range"));
8c43009f
PA
21828 else
21829 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21830 }
21831
c906108c
SS
21832 switch (die->tag)
21833 {
21834 case DW_TAG_label:
e142c38c 21835 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21836 if (attr != nullptr)
3e29f34a
MR
21837 {
21838 CORE_ADDR addr;
21839
21840 addr = attr_value_as_address (attr);
21841 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21842 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21843 }
0f5238ed
TT
21844 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21845 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21846 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21847 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21848 break;
21849 case DW_TAG_subprogram:
21850 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21851 finish_block. */
f1e6e072 21852 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21853 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21854 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21855 || cu->language == language_ada
21856 || cu->language == language_fortran)
c906108c 21857 {
2cfa0c8d 21858 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21859 Ada and Fortran subprograms, whether marked external or
21860 not, are always stored as a global symbol, because we want
21861 to be able to access them globally. For instance, we want
21862 to be able to break on a nested subprogram without having
21863 to specify the context. */
c24bdb02 21864 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21865 }
21866 else
21867 {
e37fd15a 21868 list_to_add = cu->list_in_scope;
c906108c
SS
21869 }
21870 break;
edb3359d
DJ
21871 case DW_TAG_inlined_subroutine:
21872 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21873 finish_block. */
f1e6e072 21874 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21875 SYMBOL_INLINED (sym) = 1;
481860b3 21876 list_to_add = cu->list_in_scope;
edb3359d 21877 break;
34eaf542
TT
21878 case DW_TAG_template_value_param:
21879 suppress_add = 1;
21880 /* Fall through. */
72929c62 21881 case DW_TAG_constant:
c906108c 21882 case DW_TAG_variable:
254e6b9e 21883 case DW_TAG_member:
0963b4bd
MS
21884 /* Compilation with minimal debug info may result in
21885 variables with missing type entries. Change the
21886 misleading `void' type to something sensible. */
c906108c 21887 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21888 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21889
e142c38c 21890 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21891 /* In the case of DW_TAG_member, we should only be called for
21892 static const members. */
21893 if (die->tag == DW_TAG_member)
21894 {
3863f96c
DE
21895 /* dwarf2_add_field uses die_is_declaration,
21896 so we do the same. */
254e6b9e
DE
21897 gdb_assert (die_is_declaration (die, cu));
21898 gdb_assert (attr);
21899 }
435d3d88 21900 if (attr != nullptr)
c906108c 21901 {
e7c27a73 21902 dwarf2_const_value (attr, sym, cu);
e142c38c 21903 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21904 if (!suppress_add)
34eaf542
TT
21905 {
21906 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21907 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21908 else
e37fd15a 21909 list_to_add = cu->list_in_scope;
34eaf542 21910 }
c906108c
SS
21911 break;
21912 }
e142c38c 21913 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21914 if (attr != nullptr)
c906108c 21915 {
e7c27a73 21916 var_decode_location (attr, sym, cu);
e142c38c 21917 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21918
21919 /* Fortran explicitly imports any global symbols to the local
21920 scope by DW_TAG_common_block. */
21921 if (cu->language == language_fortran && die->parent
21922 && die->parent->tag == DW_TAG_common_block)
21923 attr2 = NULL;
21924
caac4577
JG
21925 if (SYMBOL_CLASS (sym) == LOC_STATIC
21926 && SYMBOL_VALUE_ADDRESS (sym) == 0
21927 && !dwarf2_per_objfile->has_section_at_zero)
21928 {
21929 /* When a static variable is eliminated by the linker,
21930 the corresponding debug information is not stripped
21931 out, but the variable address is set to null;
21932 do not add such variables into symbol table. */
21933 }
21934 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21935 {
4b610737
TT
21936 if (SYMBOL_CLASS (sym) == LOC_STATIC
21937 && (objfile->flags & OBJF_MAINLINE) == 0
21938 && dwarf2_per_objfile->can_copy)
21939 {
21940 /* A global static variable might be subject to
21941 copy relocation. We first check for a local
21942 minsym, though, because maybe the symbol was
21943 marked hidden, in which case this would not
21944 apply. */
21945 bound_minimal_symbol found
21946 = (lookup_minimal_symbol_linkage
987012b8 21947 (sym->linkage_name (), objfile));
4b610737
TT
21948 if (found.minsym != nullptr)
21949 sym->maybe_copied = 1;
21950 }
f55ee35c 21951
1c809c68
TT
21952 /* A variable with DW_AT_external is never static,
21953 but it may be block-scoped. */
804d2729 21954 list_to_add
c24bdb02
KS
21955 = ((cu->list_in_scope
21956 == cu->get_builder ()->get_file_symbols ())
21957 ? cu->get_builder ()->get_global_symbols ()
804d2729 21958 : cu->list_in_scope);
1c809c68 21959 }
c906108c 21960 else
e37fd15a 21961 list_to_add = cu->list_in_scope;
c906108c
SS
21962 }
21963 else
21964 {
21965 /* We do not know the address of this symbol.
c5aa993b
JM
21966 If it is an external symbol and we have type information
21967 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21968 The address of the variable will then be determined from
21969 the minimal symbol table whenever the variable is
21970 referenced. */
e142c38c 21971 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21972
21973 /* Fortran explicitly imports any global symbols to the local
21974 scope by DW_TAG_common_block. */
21975 if (cu->language == language_fortran && die->parent
21976 && die->parent->tag == DW_TAG_common_block)
21977 {
21978 /* SYMBOL_CLASS doesn't matter here because
21979 read_common_block is going to reset it. */
21980 if (!suppress_add)
21981 list_to_add = cu->list_in_scope;
21982 }
21983 else if (attr2 && (DW_UNSND (attr2) != 0)
21984 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21985 {
0fe7935b
DJ
21986 /* A variable with DW_AT_external is never static, but it
21987 may be block-scoped. */
804d2729 21988 list_to_add
c24bdb02
KS
21989 = ((cu->list_in_scope
21990 == cu->get_builder ()->get_file_symbols ())
21991 ? cu->get_builder ()->get_global_symbols ()
804d2729 21992 : cu->list_in_scope);
0fe7935b 21993
f1e6e072 21994 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21995 }
442ddf59
JK
21996 else if (!die_is_declaration (die, cu))
21997 {
21998 /* Use the default LOC_OPTIMIZED_OUT class. */
21999 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22000 if (!suppress_add)
22001 list_to_add = cu->list_in_scope;
442ddf59 22002 }
c906108c
SS
22003 }
22004 break;
22005 case DW_TAG_formal_parameter:
a60f3166
TT
22006 {
22007 /* If we are inside a function, mark this as an argument. If
22008 not, we might be looking at an argument to an inlined function
22009 when we do not have enough information to show inlined frames;
22010 pretend it's a local variable in that case so that the user can
22011 still see it. */
804d2729 22012 struct context_stack *curr
c24bdb02 22013 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22014 if (curr != nullptr && curr->name != nullptr)
22015 SYMBOL_IS_ARGUMENT (sym) = 1;
22016 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22017 if (attr != nullptr)
a60f3166
TT
22018 {
22019 var_decode_location (attr, sym, cu);
22020 }
22021 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22022 if (attr != nullptr)
a60f3166
TT
22023 {
22024 dwarf2_const_value (attr, sym, cu);
22025 }
f346a30d 22026
a60f3166
TT
22027 list_to_add = cu->list_in_scope;
22028 }
c906108c
SS
22029 break;
22030 case DW_TAG_unspecified_parameters:
22031 /* From varargs functions; gdb doesn't seem to have any
22032 interest in this information, so just ignore it for now.
22033 (FIXME?) */
22034 break;
34eaf542
TT
22035 case DW_TAG_template_type_param:
22036 suppress_add = 1;
22037 /* Fall through. */
c906108c 22038 case DW_TAG_class_type:
680b30c7 22039 case DW_TAG_interface_type:
c906108c
SS
22040 case DW_TAG_structure_type:
22041 case DW_TAG_union_type:
72019c9c 22042 case DW_TAG_set_type:
c906108c 22043 case DW_TAG_enumeration_type:
f1e6e072 22044 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22045 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22046
63d06c5c 22047 {
9c37b5ae 22048 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22049 really ever be static objects: otherwise, if you try
22050 to, say, break of a class's method and you're in a file
22051 which doesn't mention that class, it won't work unless
22052 the check for all static symbols in lookup_symbol_aux
22053 saves you. See the OtherFileClass tests in
22054 gdb.c++/namespace.exp. */
22055
e37fd15a 22056 if (!suppress_add)
34eaf542 22057 {
c24bdb02 22058 buildsym_compunit *builder = cu->get_builder ();
804d2729 22059 list_to_add
c24bdb02 22060 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22061 && cu->language == language_cplus
c24bdb02 22062 ? builder->get_global_symbols ()
804d2729 22063 : cu->list_in_scope);
63d06c5c 22064
64382290 22065 /* The semantics of C++ state that "struct foo {
9c37b5ae 22066 ... }" also defines a typedef for "foo". */
64382290 22067 if (cu->language == language_cplus
45280282 22068 || cu->language == language_ada
c44af4eb
TT
22069 || cu->language == language_d
22070 || cu->language == language_rust)
64382290
TT
22071 {
22072 /* The symbol's name is already allocated along
22073 with this objfile, so we don't need to
22074 duplicate it for the type. */
22075 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 22076 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 22077 }
63d06c5c
DC
22078 }
22079 }
c906108c
SS
22080 break;
22081 case DW_TAG_typedef:
f1e6e072 22082 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22083 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22084 list_to_add = cu->list_in_scope;
63d06c5c 22085 break;
c906108c 22086 case DW_TAG_base_type:
a02abb62 22087 case DW_TAG_subrange_type:
f1e6e072 22088 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22089 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22090 list_to_add = cu->list_in_scope;
c906108c
SS
22091 break;
22092 case DW_TAG_enumerator:
e142c38c 22093 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22094 if (attr != nullptr)
c906108c 22095 {
e7c27a73 22096 dwarf2_const_value (attr, sym, cu);
c906108c 22097 }
63d06c5c
DC
22098 {
22099 /* NOTE: carlton/2003-11-10: See comment above in the
22100 DW_TAG_class_type, etc. block. */
22101
804d2729 22102 list_to_add
c24bdb02 22103 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22104 && cu->language == language_cplus
c24bdb02 22105 ? cu->get_builder ()->get_global_symbols ()
804d2729 22106 : cu->list_in_scope);
63d06c5c 22107 }
c906108c 22108 break;
74921315 22109 case DW_TAG_imported_declaration:
5c4e30ca 22110 case DW_TAG_namespace:
f1e6e072 22111 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22112 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22113 break;
530e8392
KB
22114 case DW_TAG_module:
22115 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22116 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22117 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22118 break;
4357ac6c 22119 case DW_TAG_common_block:
f1e6e072 22120 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22121 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22122 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22123 break;
c906108c
SS
22124 default:
22125 /* Not a tag we recognize. Hopefully we aren't processing
22126 trash data, but since we must specifically ignore things
22127 we don't recognize, there is nothing else we should do at
0963b4bd 22128 this point. */
b98664d3 22129 complaint (_("unsupported tag: '%s'"),
4d3c2250 22130 dwarf_tag_name (die->tag));
c906108c
SS
22131 break;
22132 }
df8a16a1 22133
e37fd15a
SW
22134 if (suppress_add)
22135 {
22136 sym->hash_next = objfile->template_symbols;
22137 objfile->template_symbols = sym;
22138 list_to_add = NULL;
22139 }
22140
22141 if (list_to_add != NULL)
d3cb6808 22142 add_symbol_to_list (sym, list_to_add);
e37fd15a 22143
df8a16a1
DJ
22144 /* For the benefit of old versions of GCC, check for anonymous
22145 namespaces based on the demangled name. */
4d4ec4e5 22146 if (!cu->processing_has_namespace_info
94af9270 22147 && cu->language == language_cplus)
c24bdb02 22148 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22149 }
22150 return (sym);
22151}
22152
98bfdba5
PA
22153/* Given an attr with a DW_FORM_dataN value in host byte order,
22154 zero-extend it as appropriate for the symbol's type. The DWARF
22155 standard (v4) is not entirely clear about the meaning of using
22156 DW_FORM_dataN for a constant with a signed type, where the type is
22157 wider than the data. The conclusion of a discussion on the DWARF
22158 list was that this is unspecified. We choose to always zero-extend
22159 because that is the interpretation long in use by GCC. */
c906108c 22160
98bfdba5 22161static gdb_byte *
ff39bb5e 22162dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22163 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22164{
518817b3 22165 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22166 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22167 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22168 LONGEST l = DW_UNSND (attr);
22169
22170 if (bits < sizeof (*value) * 8)
22171 {
22172 l &= ((LONGEST) 1 << bits) - 1;
22173 *value = l;
22174 }
22175 else if (bits == sizeof (*value) * 8)
22176 *value = l;
22177 else
22178 {
224c3ddb 22179 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22180 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22181 return bytes;
22182 }
22183
22184 return NULL;
22185}
22186
22187/* Read a constant value from an attribute. Either set *VALUE, or if
22188 the value does not fit in *VALUE, set *BYTES - either already
22189 allocated on the objfile obstack, or newly allocated on OBSTACK,
22190 or, set *BATON, if we translated the constant to a location
22191 expression. */
22192
22193static void
ff39bb5e 22194dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22195 const char *name, struct obstack *obstack,
22196 struct dwarf2_cu *cu,
d521ce57 22197 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22198 struct dwarf2_locexpr_baton **baton)
22199{
518817b3 22200 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22201 struct comp_unit_head *cu_header = &cu->header;
c906108c 22202 struct dwarf_block *blk;
98bfdba5
PA
22203 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22204 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22205
22206 *value = 0;
22207 *bytes = NULL;
22208 *baton = NULL;
c906108c
SS
22209
22210 switch (attr->form)
22211 {
22212 case DW_FORM_addr:
336d760d 22213 case DW_FORM_addrx:
3019eac3 22214 case DW_FORM_GNU_addr_index:
ac56253d 22215 {
ac56253d
TT
22216 gdb_byte *data;
22217
98bfdba5
PA
22218 if (TYPE_LENGTH (type) != cu_header->addr_size)
22219 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22220 cu_header->addr_size,
98bfdba5 22221 TYPE_LENGTH (type));
ac56253d
TT
22222 /* Symbols of this form are reasonably rare, so we just
22223 piggyback on the existing location code rather than writing
22224 a new implementation of symbol_computed_ops. */
8d749320 22225 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22226 (*baton)->per_cu = cu->per_cu;
22227 gdb_assert ((*baton)->per_cu);
ac56253d 22228
98bfdba5 22229 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22230 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22231 (*baton)->data = data;
ac56253d
TT
22232
22233 data[0] = DW_OP_addr;
22234 store_unsigned_integer (&data[1], cu_header->addr_size,
22235 byte_order, DW_ADDR (attr));
22236 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22237 }
c906108c 22238 break;
4ac36638 22239 case DW_FORM_string:
93b5768b 22240 case DW_FORM_strp:
cf532bd1 22241 case DW_FORM_strx:
3019eac3 22242 case DW_FORM_GNU_str_index:
36586728 22243 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22244 /* DW_STRING is already allocated on the objfile obstack, point
22245 directly to it. */
d521ce57 22246 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22247 break;
c906108c
SS
22248 case DW_FORM_block1:
22249 case DW_FORM_block2:
22250 case DW_FORM_block4:
22251 case DW_FORM_block:
2dc7f7b3 22252 case DW_FORM_exprloc:
0224619f 22253 case DW_FORM_data16:
c906108c 22254 blk = DW_BLOCK (attr);
98bfdba5
PA
22255 if (TYPE_LENGTH (type) != blk->size)
22256 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22257 TYPE_LENGTH (type));
22258 *bytes = blk->data;
c906108c 22259 break;
2df3850c
JM
22260
22261 /* The DW_AT_const_value attributes are supposed to carry the
22262 symbol's value "represented as it would be on the target
22263 architecture." By the time we get here, it's already been
22264 converted to host endianness, so we just need to sign- or
22265 zero-extend it as appropriate. */
22266 case DW_FORM_data1:
3aef2284 22267 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22268 break;
c906108c 22269 case DW_FORM_data2:
3aef2284 22270 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22271 break;
c906108c 22272 case DW_FORM_data4:
3aef2284 22273 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22274 break;
c906108c 22275 case DW_FORM_data8:
3aef2284 22276 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22277 break;
22278
c906108c 22279 case DW_FORM_sdata:
663c44ac 22280 case DW_FORM_implicit_const:
98bfdba5 22281 *value = DW_SND (attr);
2df3850c
JM
22282 break;
22283
c906108c 22284 case DW_FORM_udata:
98bfdba5 22285 *value = DW_UNSND (attr);
c906108c 22286 break;
2df3850c 22287
c906108c 22288 default:
b98664d3 22289 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22290 dwarf_form_name (attr->form));
98bfdba5 22291 *value = 0;
c906108c
SS
22292 break;
22293 }
22294}
22295
2df3850c 22296
98bfdba5
PA
22297/* Copy constant value from an attribute to a symbol. */
22298
2df3850c 22299static void
ff39bb5e 22300dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22301 struct dwarf2_cu *cu)
2df3850c 22302{
518817b3 22303 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22304 LONGEST value;
d521ce57 22305 const gdb_byte *bytes;
98bfdba5 22306 struct dwarf2_locexpr_baton *baton;
2df3850c 22307
98bfdba5 22308 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22309 sym->print_name (),
98bfdba5
PA
22310 &objfile->objfile_obstack, cu,
22311 &value, &bytes, &baton);
2df3850c 22312
98bfdba5
PA
22313 if (baton != NULL)
22314 {
98bfdba5 22315 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22316 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22317 }
22318 else if (bytes != NULL)
22319 {
22320 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22321 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22322 }
22323 else
22324 {
22325 SYMBOL_VALUE (sym) = value;
f1e6e072 22326 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22327 }
2df3850c
JM
22328}
22329
c906108c
SS
22330/* Return the type of the die in question using its DW_AT_type attribute. */
22331
22332static struct type *
e7c27a73 22333die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22334{
c906108c 22335 struct attribute *type_attr;
c906108c 22336
e142c38c 22337 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22338 if (!type_attr)
22339 {
518817b3 22340 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22341 /* A missing DW_AT_type represents a void type. */
518817b3 22342 return objfile_type (objfile)->builtin_void;
c906108c 22343 }
348e048f 22344
673bfd45 22345 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22346}
22347
b4ba55a1
JB
22348/* True iff CU's producer generates GNAT Ada auxiliary information
22349 that allows to find parallel types through that information instead
22350 of having to do expensive parallel lookups by type name. */
22351
22352static int
22353need_gnat_info (struct dwarf2_cu *cu)
22354{
de4cb04a
JB
22355 /* Assume that the Ada compiler was GNAT, which always produces
22356 the auxiliary information. */
22357 return (cu->language == language_ada);
b4ba55a1
JB
22358}
22359
b4ba55a1
JB
22360/* Return the auxiliary type of the die in question using its
22361 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22362 attribute is not present. */
22363
22364static struct type *
22365die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22366{
b4ba55a1 22367 struct attribute *type_attr;
b4ba55a1
JB
22368
22369 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22370 if (!type_attr)
22371 return NULL;
22372
673bfd45 22373 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22374}
22375
22376/* If DIE has a descriptive_type attribute, then set the TYPE's
22377 descriptive type accordingly. */
22378
22379static void
22380set_descriptive_type (struct type *type, struct die_info *die,
22381 struct dwarf2_cu *cu)
22382{
22383 struct type *descriptive_type = die_descriptive_type (die, cu);
22384
22385 if (descriptive_type)
22386 {
22387 ALLOCATE_GNAT_AUX_TYPE (type);
22388 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22389 }
22390}
22391
c906108c
SS
22392/* Return the containing type of the die in question using its
22393 DW_AT_containing_type attribute. */
22394
22395static struct type *
e7c27a73 22396die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22397{
c906108c 22398 struct attribute *type_attr;
518817b3 22399 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22400
e142c38c 22401 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22402 if (!type_attr)
22403 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22404 "[in module %s]"), objfile_name (objfile));
33ac96f0 22405
673bfd45 22406 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22407}
22408
ac9ec31b
DE
22409/* Return an error marker type to use for the ill formed type in DIE/CU. */
22410
22411static struct type *
22412build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22413{
518817b3
SM
22414 struct dwarf2_per_objfile *dwarf2_per_objfile
22415 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22416 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22417 char *saved;
ac9ec31b 22418
528e1572
SM
22419 std::string message
22420 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22421 objfile_name (objfile),
22422 sect_offset_str (cu->header.sect_off),
22423 sect_offset_str (die->sect_off));
efba19b0 22424 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22425
19f392bc 22426 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22427}
22428
673bfd45 22429/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22430 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22431 DW_AT_containing_type.
673bfd45
DE
22432 If there is no type substitute an error marker. */
22433
c906108c 22434static struct type *
ff39bb5e 22435lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22436 struct dwarf2_cu *cu)
c906108c 22437{
518817b3
SM
22438 struct dwarf2_per_objfile *dwarf2_per_objfile
22439 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22440 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22441 struct type *this_type;
22442
ac9ec31b
DE
22443 gdb_assert (attr->name == DW_AT_type
22444 || attr->name == DW_AT_GNAT_descriptive_type
22445 || attr->name == DW_AT_containing_type);
22446
673bfd45
DE
22447 /* First see if we have it cached. */
22448
36586728
TT
22449 if (attr->form == DW_FORM_GNU_ref_alt)
22450 {
22451 struct dwarf2_per_cu_data *per_cu;
9c541725 22452 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22453
ed2dc618
SM
22454 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22455 dwarf2_per_objfile);
9c541725 22456 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22457 }
7771576e 22458 else if (attr_form_is_ref (attr))
673bfd45 22459 {
9c541725 22460 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22461
9c541725 22462 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22463 }
55f1336d 22464 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22465 {
ac9ec31b 22466 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22467
ac9ec31b 22468 return get_signatured_type (die, signature, cu);
673bfd45
DE
22469 }
22470 else
22471 {
b98664d3 22472 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22473 " at %s [in module %s]"),
22474 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22475 objfile_name (objfile));
ac9ec31b 22476 return build_error_marker_type (cu, die);
673bfd45
DE
22477 }
22478
22479 /* If not cached we need to read it in. */
22480
22481 if (this_type == NULL)
22482 {
ac9ec31b 22483 struct die_info *type_die = NULL;
673bfd45
DE
22484 struct dwarf2_cu *type_cu = cu;
22485
7771576e 22486 if (attr_form_is_ref (attr))
ac9ec31b
DE
22487 type_die = follow_die_ref (die, attr, &type_cu);
22488 if (type_die == NULL)
22489 return build_error_marker_type (cu, die);
22490 /* If we find the type now, it's probably because the type came
3019eac3
DE
22491 from an inter-CU reference and the type's CU got expanded before
22492 ours. */
ac9ec31b 22493 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22494 }
22495
22496 /* If we still don't have a type use an error marker. */
22497
22498 if (this_type == NULL)
ac9ec31b 22499 return build_error_marker_type (cu, die);
673bfd45 22500
f792889a 22501 return this_type;
c906108c
SS
22502}
22503
673bfd45
DE
22504/* Return the type in DIE, CU.
22505 Returns NULL for invalid types.
22506
02142a6c 22507 This first does a lookup in die_type_hash,
673bfd45
DE
22508 and only reads the die in if necessary.
22509
22510 NOTE: This can be called when reading in partial or full symbols. */
22511
f792889a 22512static struct type *
e7c27a73 22513read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22514{
f792889a
DJ
22515 struct type *this_type;
22516
22517 this_type = get_die_type (die, cu);
22518 if (this_type)
22519 return this_type;
22520
673bfd45
DE
22521 return read_type_die_1 (die, cu);
22522}
22523
22524/* Read the type in DIE, CU.
22525 Returns NULL for invalid types. */
22526
22527static struct type *
22528read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22529{
22530 struct type *this_type = NULL;
22531
c906108c
SS
22532 switch (die->tag)
22533 {
22534 case DW_TAG_class_type:
680b30c7 22535 case DW_TAG_interface_type:
c906108c
SS
22536 case DW_TAG_structure_type:
22537 case DW_TAG_union_type:
f792889a 22538 this_type = read_structure_type (die, cu);
c906108c
SS
22539 break;
22540 case DW_TAG_enumeration_type:
f792889a 22541 this_type = read_enumeration_type (die, cu);
c906108c
SS
22542 break;
22543 case DW_TAG_subprogram:
22544 case DW_TAG_subroutine_type:
edb3359d 22545 case DW_TAG_inlined_subroutine:
f792889a 22546 this_type = read_subroutine_type (die, cu);
c906108c
SS
22547 break;
22548 case DW_TAG_array_type:
f792889a 22549 this_type = read_array_type (die, cu);
c906108c 22550 break;
72019c9c 22551 case DW_TAG_set_type:
f792889a 22552 this_type = read_set_type (die, cu);
72019c9c 22553 break;
c906108c 22554 case DW_TAG_pointer_type:
f792889a 22555 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22556 break;
22557 case DW_TAG_ptr_to_member_type:
f792889a 22558 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22559 break;
22560 case DW_TAG_reference_type:
4297a3f0
AV
22561 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22562 break;
22563 case DW_TAG_rvalue_reference_type:
22564 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22565 break;
22566 case DW_TAG_const_type:
f792889a 22567 this_type = read_tag_const_type (die, cu);
c906108c
SS
22568 break;
22569 case DW_TAG_volatile_type:
f792889a 22570 this_type = read_tag_volatile_type (die, cu);
c906108c 22571 break;
06d66ee9
TT
22572 case DW_TAG_restrict_type:
22573 this_type = read_tag_restrict_type (die, cu);
22574 break;
c906108c 22575 case DW_TAG_string_type:
f792889a 22576 this_type = read_tag_string_type (die, cu);
c906108c
SS
22577 break;
22578 case DW_TAG_typedef:
f792889a 22579 this_type = read_typedef (die, cu);
c906108c 22580 break;
a02abb62 22581 case DW_TAG_subrange_type:
f792889a 22582 this_type = read_subrange_type (die, cu);
a02abb62 22583 break;
c906108c 22584 case DW_TAG_base_type:
f792889a 22585 this_type = read_base_type (die, cu);
c906108c 22586 break;
81a17f79 22587 case DW_TAG_unspecified_type:
f792889a 22588 this_type = read_unspecified_type (die, cu);
81a17f79 22589 break;
0114d602
DJ
22590 case DW_TAG_namespace:
22591 this_type = read_namespace_type (die, cu);
22592 break;
f55ee35c
JK
22593 case DW_TAG_module:
22594 this_type = read_module_type (die, cu);
22595 break;
a2c2acaf
MW
22596 case DW_TAG_atomic_type:
22597 this_type = read_tag_atomic_type (die, cu);
22598 break;
c906108c 22599 default:
b98664d3 22600 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22601 dwarf_tag_name (die->tag));
c906108c
SS
22602 break;
22603 }
63d06c5c 22604
f792889a 22605 return this_type;
63d06c5c
DC
22606}
22607
abc72ce4
DE
22608/* See if we can figure out if the class lives in a namespace. We do
22609 this by looking for a member function; its demangled name will
22610 contain namespace info, if there is any.
22611 Return the computed name or NULL.
22612 Space for the result is allocated on the objfile's obstack.
22613 This is the full-die version of guess_partial_die_structure_name.
22614 In this case we know DIE has no useful parent. */
22615
22616static char *
22617guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22618{
22619 struct die_info *spec_die;
22620 struct dwarf2_cu *spec_cu;
22621 struct die_info *child;
518817b3 22622 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22623
22624 spec_cu = cu;
22625 spec_die = die_specification (die, &spec_cu);
22626 if (spec_die != NULL)
22627 {
22628 die = spec_die;
22629 cu = spec_cu;
22630 }
22631
22632 for (child = die->child;
22633 child != NULL;
22634 child = child->sibling)
22635 {
22636 if (child->tag == DW_TAG_subprogram)
22637 {
73b9be8b 22638 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22639
7d45c7c3 22640 if (linkage_name != NULL)
abc72ce4
DE
22641 {
22642 char *actual_name
22643 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22644 linkage_name);
abc72ce4
DE
22645 char *name = NULL;
22646
22647 if (actual_name != NULL)
22648 {
15d034d0 22649 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22650
22651 if (die_name != NULL
22652 && strcmp (die_name, actual_name) != 0)
22653 {
22654 /* Strip off the class name from the full name.
22655 We want the prefix. */
22656 int die_name_len = strlen (die_name);
22657 int actual_name_len = strlen (actual_name);
22658
22659 /* Test for '::' as a sanity check. */
22660 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22661 && actual_name[actual_name_len
22662 - die_name_len - 1] == ':')
0cf9feb9 22663 name = obstack_strndup (
e3b94546 22664 &objfile->per_bfd->storage_obstack,
224c3ddb 22665 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22666 }
22667 }
22668 xfree (actual_name);
22669 return name;
22670 }
22671 }
22672 }
22673
22674 return NULL;
22675}
22676
96408a79
SA
22677/* GCC might emit a nameless typedef that has a linkage name. Determine the
22678 prefix part in such case. See
22679 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22680
a121b7c1 22681static const char *
96408a79
SA
22682anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22683{
22684 struct attribute *attr;
e6a959d6 22685 const char *base;
96408a79
SA
22686
22687 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22688 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22689 return NULL;
22690
7d45c7c3 22691 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22692 return NULL;
22693
73b9be8b 22694 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22695 if (attr == NULL || DW_STRING (attr) == NULL)
22696 return NULL;
22697
22698 /* dwarf2_name had to be already called. */
22699 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22700
22701 /* Strip the base name, keep any leading namespaces/classes. */
22702 base = strrchr (DW_STRING (attr), ':');
22703 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22704 return "";
22705
518817b3 22706 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22707 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22708 DW_STRING (attr),
22709 &base[-1] - DW_STRING (attr));
96408a79
SA
22710}
22711
fdde2d81 22712/* Return the name of the namespace/class that DIE is defined within,
0114d602 22713 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22714
0114d602
DJ
22715 For example, if we're within the method foo() in the following
22716 code:
22717
22718 namespace N {
22719 class C {
22720 void foo () {
22721 }
22722 };
22723 }
22724
22725 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22726
0d5cff50 22727static const char *
e142c38c 22728determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22729{
518817b3
SM
22730 struct dwarf2_per_objfile *dwarf2_per_objfile
22731 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22732 struct die_info *parent, *spec_die;
22733 struct dwarf2_cu *spec_cu;
22734 struct type *parent_type;
a121b7c1 22735 const char *retval;
63d06c5c 22736
9c37b5ae 22737 if (cu->language != language_cplus
c44af4eb
TT
22738 && cu->language != language_fortran && cu->language != language_d
22739 && cu->language != language_rust)
0114d602
DJ
22740 return "";
22741
96408a79
SA
22742 retval = anonymous_struct_prefix (die, cu);
22743 if (retval)
22744 return retval;
22745
0114d602
DJ
22746 /* We have to be careful in the presence of DW_AT_specification.
22747 For example, with GCC 3.4, given the code
22748
22749 namespace N {
22750 void foo() {
22751 // Definition of N::foo.
22752 }
22753 }
22754
22755 then we'll have a tree of DIEs like this:
22756
22757 1: DW_TAG_compile_unit
22758 2: DW_TAG_namespace // N
22759 3: DW_TAG_subprogram // declaration of N::foo
22760 4: DW_TAG_subprogram // definition of N::foo
22761 DW_AT_specification // refers to die #3
22762
22763 Thus, when processing die #4, we have to pretend that we're in
22764 the context of its DW_AT_specification, namely the contex of die
22765 #3. */
22766 spec_cu = cu;
22767 spec_die = die_specification (die, &spec_cu);
22768 if (spec_die == NULL)
22769 parent = die->parent;
22770 else
63d06c5c 22771 {
0114d602
DJ
22772 parent = spec_die->parent;
22773 cu = spec_cu;
63d06c5c 22774 }
0114d602
DJ
22775
22776 if (parent == NULL)
22777 return "";
98bfdba5
PA
22778 else if (parent->building_fullname)
22779 {
22780 const char *name;
22781 const char *parent_name;
22782
22783 /* It has been seen on RealView 2.2 built binaries,
22784 DW_TAG_template_type_param types actually _defined_ as
22785 children of the parent class:
22786
22787 enum E {};
22788 template class <class Enum> Class{};
22789 Class<enum E> class_e;
22790
22791 1: DW_TAG_class_type (Class)
22792 2: DW_TAG_enumeration_type (E)
22793 3: DW_TAG_enumerator (enum1:0)
22794 3: DW_TAG_enumerator (enum2:1)
22795 ...
22796 2: DW_TAG_template_type_param
22797 DW_AT_type DW_FORM_ref_udata (E)
22798
22799 Besides being broken debug info, it can put GDB into an
22800 infinite loop. Consider:
22801
22802 When we're building the full name for Class<E>, we'll start
22803 at Class, and go look over its template type parameters,
22804 finding E. We'll then try to build the full name of E, and
22805 reach here. We're now trying to build the full name of E,
22806 and look over the parent DIE for containing scope. In the
22807 broken case, if we followed the parent DIE of E, we'd again
22808 find Class, and once again go look at its template type
22809 arguments, etc., etc. Simply don't consider such parent die
22810 as source-level parent of this die (it can't be, the language
22811 doesn't allow it), and break the loop here. */
22812 name = dwarf2_name (die, cu);
22813 parent_name = dwarf2_name (parent, cu);
b98664d3 22814 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22815 name ? name : "<unknown>",
22816 parent_name ? parent_name : "<unknown>");
22817 return "";
22818 }
63d06c5c 22819 else
0114d602
DJ
22820 switch (parent->tag)
22821 {
63d06c5c 22822 case DW_TAG_namespace:
0114d602 22823 parent_type = read_type_die (parent, cu);
acebe513
UW
22824 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22825 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22826 Work around this problem here. */
22827 if (cu->language == language_cplus
e86ca25f 22828 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22829 return "";
0114d602 22830 /* We give a name to even anonymous namespaces. */
e86ca25f 22831 return TYPE_NAME (parent_type);
63d06c5c 22832 case DW_TAG_class_type:
680b30c7 22833 case DW_TAG_interface_type:
63d06c5c 22834 case DW_TAG_structure_type:
0114d602 22835 case DW_TAG_union_type:
f55ee35c 22836 case DW_TAG_module:
0114d602 22837 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22838 if (TYPE_NAME (parent_type) != NULL)
22839 return TYPE_NAME (parent_type);
0114d602
DJ
22840 else
22841 /* An anonymous structure is only allowed non-static data
22842 members; no typedefs, no member functions, et cetera.
22843 So it does not need a prefix. */
22844 return "";
abc72ce4 22845 case DW_TAG_compile_unit:
95554aad 22846 case DW_TAG_partial_unit:
abc72ce4
DE
22847 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22848 if (cu->language == language_cplus
fd5866f6 22849 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22850 && die->child != NULL
22851 && (die->tag == DW_TAG_class_type
22852 || die->tag == DW_TAG_structure_type
22853 || die->tag == DW_TAG_union_type))
22854 {
22855 char *name = guess_full_die_structure_name (die, cu);
22856 if (name != NULL)
22857 return name;
22858 }
22859 return "";
0a4b0913
AB
22860 case DW_TAG_subprogram:
22861 /* Nested subroutines in Fortran get a prefix with the name
22862 of the parent's subroutine. */
22863 if (cu->language == language_fortran)
22864 {
22865 if ((die->tag == DW_TAG_subprogram)
22866 && (dwarf2_name (parent, cu) != NULL))
22867 return dwarf2_name (parent, cu);
22868 }
22869 return determine_prefix (parent, cu);
3d567982
TT
22870 case DW_TAG_enumeration_type:
22871 parent_type = read_type_die (parent, cu);
22872 if (TYPE_DECLARED_CLASS (parent_type))
22873 {
e86ca25f
TT
22874 if (TYPE_NAME (parent_type) != NULL)
22875 return TYPE_NAME (parent_type);
3d567982
TT
22876 return "";
22877 }
22878 /* Fall through. */
63d06c5c 22879 default:
8176b9b8 22880 return determine_prefix (parent, cu);
63d06c5c 22881 }
63d06c5c
DC
22882}
22883
3e43a32a
MS
22884/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22885 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22886 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22887 an obconcat, otherwise allocate storage for the result. The CU argument is
22888 used to determine the language and hence, the appropriate separator. */
987504bb 22889
f55ee35c 22890#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22891
22892static char *
f55ee35c
JK
22893typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22894 int physname, struct dwarf2_cu *cu)
63d06c5c 22895{
f55ee35c 22896 const char *lead = "";
5c315b68 22897 const char *sep;
63d06c5c 22898
3e43a32a
MS
22899 if (suffix == NULL || suffix[0] == '\0'
22900 || prefix == NULL || prefix[0] == '\0')
987504bb 22901 sep = "";
45280282
IB
22902 else if (cu->language == language_d)
22903 {
22904 /* For D, the 'main' function could be defined in any module, but it
22905 should never be prefixed. */
22906 if (strcmp (suffix, "D main") == 0)
22907 {
22908 prefix = "";
22909 sep = "";
22910 }
22911 else
22912 sep = ".";
22913 }
f55ee35c
JK
22914 else if (cu->language == language_fortran && physname)
22915 {
22916 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22917 DW_AT_MIPS_linkage_name is preferred and used instead. */
22918
22919 lead = "__";
22920 sep = "_MOD_";
22921 }
987504bb
JJ
22922 else
22923 sep = "::";
63d06c5c 22924
6dd47d34
DE
22925 if (prefix == NULL)
22926 prefix = "";
22927 if (suffix == NULL)
22928 suffix = "";
22929
987504bb
JJ
22930 if (obs == NULL)
22931 {
3e43a32a 22932 char *retval
224c3ddb
SM
22933 = ((char *)
22934 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22935
f55ee35c
JK
22936 strcpy (retval, lead);
22937 strcat (retval, prefix);
6dd47d34
DE
22938 strcat (retval, sep);
22939 strcat (retval, suffix);
63d06c5c
DC
22940 return retval;
22941 }
987504bb
JJ
22942 else
22943 {
22944 /* We have an obstack. */
f55ee35c 22945 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22946 }
63d06c5c
DC
22947}
22948
c906108c
SS
22949/* Return sibling of die, NULL if no sibling. */
22950
f9aca02d 22951static struct die_info *
fba45db2 22952sibling_die (struct die_info *die)
c906108c 22953{
639d11d3 22954 return die->sibling;
c906108c
SS
22955}
22956
71c25dea
TT
22957/* Get name of a die, return NULL if not found. */
22958
15d034d0
TT
22959static const char *
22960dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22961 struct obstack *obstack)
22962{
22963 if (name && cu->language == language_cplus)
22964 {
2f408ecb 22965 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22966
2f408ecb 22967 if (!canon_name.empty ())
71c25dea 22968 {
2f408ecb 22969 if (canon_name != name)
efba19b0 22970 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22971 }
22972 }
22973
22974 return name;
c906108c
SS
22975}
22976
96553a0c
DE
22977/* Get name of a die, return NULL if not found.
22978 Anonymous namespaces are converted to their magic string. */
9219021c 22979
15d034d0 22980static const char *
e142c38c 22981dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22982{
22983 struct attribute *attr;
518817b3 22984 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22985
e142c38c 22986 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22987 if ((!attr || !DW_STRING (attr))
96553a0c 22988 && die->tag != DW_TAG_namespace
53832f31
TT
22989 && die->tag != DW_TAG_class_type
22990 && die->tag != DW_TAG_interface_type
22991 && die->tag != DW_TAG_structure_type
22992 && die->tag != DW_TAG_union_type)
71c25dea
TT
22993 return NULL;
22994
22995 switch (die->tag)
22996 {
22997 case DW_TAG_compile_unit:
95554aad 22998 case DW_TAG_partial_unit:
71c25dea
TT
22999 /* Compilation units have a DW_AT_name that is a filename, not
23000 a source language identifier. */
23001 case DW_TAG_enumeration_type:
23002 case DW_TAG_enumerator:
23003 /* These tags always have simple identifiers already; no need
23004 to canonicalize them. */
23005 return DW_STRING (attr);
907af001 23006
96553a0c
DE
23007 case DW_TAG_namespace:
23008 if (attr != NULL && DW_STRING (attr) != NULL)
23009 return DW_STRING (attr);
23010 return CP_ANONYMOUS_NAMESPACE_STR;
23011
907af001
UW
23012 case DW_TAG_class_type:
23013 case DW_TAG_interface_type:
23014 case DW_TAG_structure_type:
23015 case DW_TAG_union_type:
23016 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23017 structures or unions. These were of the form "._%d" in GCC 4.1,
23018 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23019 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 23020 if (attr && DW_STRING (attr)
61012eef
GB
23021 && (startswith (DW_STRING (attr), "._")
23022 || startswith (DW_STRING (attr), "<anonymous")))
907af001 23023 return NULL;
53832f31
TT
23024
23025 /* GCC might emit a nameless typedef that has a linkage name. See
23026 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23027 if (!attr || DW_STRING (attr) == NULL)
23028 {
df5c6c50 23029 char *demangled = NULL;
53832f31 23030
73b9be8b 23031 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
23032 if (attr == NULL || DW_STRING (attr) == NULL)
23033 return NULL;
23034
df5c6c50
JK
23035 /* Avoid demangling DW_STRING (attr) the second time on a second
23036 call for the same DIE. */
23037 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 23038 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
23039
23040 if (demangled)
23041 {
e6a959d6 23042 const char *base;
96408a79 23043
53832f31 23044 /* FIXME: we already did this for the partial symbol... */
34a68019 23045 DW_STRING (attr)
021887d8
TT
23046 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23047 demangled);
53832f31
TT
23048 DW_STRING_IS_CANONICAL (attr) = 1;
23049 xfree (demangled);
96408a79
SA
23050
23051 /* Strip any leading namespaces/classes, keep only the base name.
23052 DW_AT_name for named DIEs does not contain the prefixes. */
23053 base = strrchr (DW_STRING (attr), ':');
23054 if (base && base > DW_STRING (attr) && base[-1] == ':')
23055 return &base[1];
23056 else
23057 return DW_STRING (attr);
53832f31
TT
23058 }
23059 }
907af001
UW
23060 break;
23061
71c25dea 23062 default:
907af001
UW
23063 break;
23064 }
23065
23066 if (!DW_STRING_IS_CANONICAL (attr))
23067 {
23068 DW_STRING (attr)
23069 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 23070 &objfile->per_bfd->storage_obstack);
907af001 23071 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 23072 }
907af001 23073 return DW_STRING (attr);
9219021c
DC
23074}
23075
23076/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23077 is none. *EXT_CU is the CU containing DIE on input, and the CU
23078 containing the return value on output. */
9219021c
DC
23079
23080static struct die_info *
f2f0e013 23081dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23082{
23083 struct attribute *attr;
9219021c 23084
f2f0e013 23085 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23086 if (attr == NULL)
23087 return NULL;
23088
f2f0e013 23089 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23090}
23091
fa9c3fa0
TT
23092/* A convenience function that returns an "unknown" DWARF name,
23093 including the value of V. STR is the name of the entity being
23094 printed, e.g., "TAG". */
23095
23096static const char *
23097dwarf_unknown (const char *str, unsigned v)
23098{
23099 char *cell = get_print_cell ();
23100 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23101 return cell;
23102}
23103
c906108c
SS
23104/* Convert a DIE tag into its string name. */
23105
f39c6ffd 23106static const char *
aa1ee363 23107dwarf_tag_name (unsigned tag)
c906108c 23108{
f39c6ffd
TT
23109 const char *name = get_DW_TAG_name (tag);
23110
23111 if (name == NULL)
fa9c3fa0 23112 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
23113
23114 return name;
c906108c
SS
23115}
23116
23117/* Convert a DWARF attribute code into its string name. */
23118
f39c6ffd 23119static const char *
aa1ee363 23120dwarf_attr_name (unsigned attr)
c906108c 23121{
f39c6ffd
TT
23122 const char *name;
23123
c764a876 23124#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
23125 if (attr == DW_AT_MIPS_fde)
23126 return "DW_AT_MIPS_fde";
23127#else
23128 if (attr == DW_AT_HP_block_index)
23129 return "DW_AT_HP_block_index";
c764a876 23130#endif
f39c6ffd
TT
23131
23132 name = get_DW_AT_name (attr);
23133
23134 if (name == NULL)
fa9c3fa0 23135 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
23136
23137 return name;
c906108c
SS
23138}
23139
a084a2a6
AT
23140/* Convert a unit type to corresponding DW_UT name. */
23141
23142static const char *
23143dwarf_unit_type_name (int unit_type) {
23144 switch (unit_type)
23145 {
23146 case 0x01:
23147 return "DW_UT_compile (0x01)";
23148 case 0x02:
23149 return "DW_UT_type (0x02)";
23150 case 0x03:
23151 return "DW_UT_partial (0x03)";
23152 case 0x04:
23153 return "DW_UT_skeleton (0x04)";
23154 case 0x05:
23155 return "DW_UT_split_compile (0x05)";
23156 case 0x06:
23157 return "DW_UT_split_type (0x06)";
23158 case 0x80:
23159 return "DW_UT_lo_user (0x80)";
23160 case 0xff:
23161 return "DW_UT_hi_user (0xff)";
23162 default:
23163 return nullptr;
23164 }
23165}
23166
c906108c
SS
23167/* Convert a DWARF value form code into its string name. */
23168
f39c6ffd 23169static const char *
aa1ee363 23170dwarf_form_name (unsigned form)
c906108c 23171{
f39c6ffd
TT
23172 const char *name = get_DW_FORM_name (form);
23173
23174 if (name == NULL)
fa9c3fa0 23175 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
23176
23177 return name;
c906108c
SS
23178}
23179
a121b7c1 23180static const char *
fba45db2 23181dwarf_bool_name (unsigned mybool)
c906108c
SS
23182{
23183 if (mybool)
23184 return "TRUE";
23185 else
23186 return "FALSE";
23187}
23188
23189/* Convert a DWARF type code into its string name. */
23190
f39c6ffd 23191static const char *
aa1ee363 23192dwarf_type_encoding_name (unsigned enc)
c906108c 23193{
f39c6ffd 23194 const char *name = get_DW_ATE_name (enc);
c906108c 23195
f39c6ffd 23196 if (name == NULL)
fa9c3fa0 23197 return dwarf_unknown ("ATE", enc);
c906108c 23198
f39c6ffd 23199 return name;
c906108c 23200}
c906108c 23201
f9aca02d 23202static void
d97bc12b 23203dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23204{
23205 unsigned int i;
23206
d97bc12b 23207 print_spaces (indent, f);
9d8780f0 23208 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23209 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23210 sect_offset_str (die->sect_off));
d97bc12b
DE
23211
23212 if (die->parent != NULL)
23213 {
23214 print_spaces (indent, f);
9d8780f0
SM
23215 fprintf_unfiltered (f, " parent at offset: %s\n",
23216 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23217 }
23218
23219 print_spaces (indent, f);
23220 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23221 dwarf_bool_name (die->child != NULL));
c906108c 23222
d97bc12b
DE
23223 print_spaces (indent, f);
23224 fprintf_unfiltered (f, " attributes:\n");
23225
c906108c
SS
23226 for (i = 0; i < die->num_attrs; ++i)
23227 {
d97bc12b
DE
23228 print_spaces (indent, f);
23229 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23230 dwarf_attr_name (die->attrs[i].name),
23231 dwarf_form_name (die->attrs[i].form));
d97bc12b 23232
c906108c
SS
23233 switch (die->attrs[i].form)
23234 {
c906108c 23235 case DW_FORM_addr:
336d760d 23236 case DW_FORM_addrx:
3019eac3 23237 case DW_FORM_GNU_addr_index:
d97bc12b 23238 fprintf_unfiltered (f, "address: ");
5af949e3 23239 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23240 break;
23241 case DW_FORM_block2:
23242 case DW_FORM_block4:
23243 case DW_FORM_block:
23244 case DW_FORM_block1:
56eb65bd
SP
23245 fprintf_unfiltered (f, "block: size %s",
23246 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23247 break;
2dc7f7b3 23248 case DW_FORM_exprloc:
56eb65bd
SP
23249 fprintf_unfiltered (f, "expression: size %s",
23250 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23251 break;
0224619f
JK
23252 case DW_FORM_data16:
23253 fprintf_unfiltered (f, "constant of 16 bytes");
23254 break;
4568ecf9
DE
23255 case DW_FORM_ref_addr:
23256 fprintf_unfiltered (f, "ref address: ");
23257 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23258 break;
36586728
TT
23259 case DW_FORM_GNU_ref_alt:
23260 fprintf_unfiltered (f, "alt ref address: ");
23261 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23262 break;
10b3939b
DJ
23263 case DW_FORM_ref1:
23264 case DW_FORM_ref2:
23265 case DW_FORM_ref4:
4568ecf9
DE
23266 case DW_FORM_ref8:
23267 case DW_FORM_ref_udata:
d97bc12b 23268 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23269 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23270 break;
c906108c
SS
23271 case DW_FORM_data1:
23272 case DW_FORM_data2:
23273 case DW_FORM_data4:
ce5d95e1 23274 case DW_FORM_data8:
c906108c
SS
23275 case DW_FORM_udata:
23276 case DW_FORM_sdata:
43bbcdc2
PH
23277 fprintf_unfiltered (f, "constant: %s",
23278 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23279 break;
2dc7f7b3
TT
23280 case DW_FORM_sec_offset:
23281 fprintf_unfiltered (f, "section offset: %s",
23282 pulongest (DW_UNSND (&die->attrs[i])));
23283 break;
55f1336d 23284 case DW_FORM_ref_sig8:
ac9ec31b
DE
23285 fprintf_unfiltered (f, "signature: %s",
23286 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23287 break;
c906108c 23288 case DW_FORM_string:
4bdf3d34 23289 case DW_FORM_strp:
43988095 23290 case DW_FORM_line_strp:
cf532bd1 23291 case DW_FORM_strx:
3019eac3 23292 case DW_FORM_GNU_str_index:
36586728 23293 case DW_FORM_GNU_strp_alt:
8285870a 23294 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23295 DW_STRING (&die->attrs[i])
8285870a
JK
23296 ? DW_STRING (&die->attrs[i]) : "",
23297 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23298 break;
23299 case DW_FORM_flag:
23300 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23301 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23302 else
d97bc12b 23303 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23304 break;
2dc7f7b3
TT
23305 case DW_FORM_flag_present:
23306 fprintf_unfiltered (f, "flag: TRUE");
23307 break;
a8329558 23308 case DW_FORM_indirect:
0963b4bd
MS
23309 /* The reader will have reduced the indirect form to
23310 the "base form" so this form should not occur. */
5f48f8f3 23311 fprintf_unfiltered (f,
3e43a32a 23312 "unexpected attribute form: DW_FORM_indirect");
a8329558 23313 break;
663c44ac
JK
23314 case DW_FORM_implicit_const:
23315 fprintf_unfiltered (f, "constant: %s",
23316 plongest (DW_SND (&die->attrs[i])));
23317 break;
c906108c 23318 default:
d97bc12b 23319 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23320 die->attrs[i].form);
d97bc12b 23321 break;
c906108c 23322 }
d97bc12b 23323 fprintf_unfiltered (f, "\n");
c906108c
SS
23324 }
23325}
23326
f9aca02d 23327static void
d97bc12b 23328dump_die_for_error (struct die_info *die)
c906108c 23329{
d97bc12b
DE
23330 dump_die_shallow (gdb_stderr, 0, die);
23331}
23332
23333static void
23334dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23335{
23336 int indent = level * 4;
23337
23338 gdb_assert (die != NULL);
23339
23340 if (level >= max_level)
23341 return;
23342
23343 dump_die_shallow (f, indent, die);
23344
23345 if (die->child != NULL)
c906108c 23346 {
d97bc12b
DE
23347 print_spaces (indent, f);
23348 fprintf_unfiltered (f, " Children:");
23349 if (level + 1 < max_level)
23350 {
23351 fprintf_unfiltered (f, "\n");
23352 dump_die_1 (f, level + 1, max_level, die->child);
23353 }
23354 else
23355 {
3e43a32a
MS
23356 fprintf_unfiltered (f,
23357 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23358 }
23359 }
23360
23361 if (die->sibling != NULL && level > 0)
23362 {
23363 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23364 }
23365}
23366
d97bc12b
DE
23367/* This is called from the pdie macro in gdbinit.in.
23368 It's not static so gcc will keep a copy callable from gdb. */
23369
23370void
23371dump_die (struct die_info *die, int max_level)
23372{
23373 dump_die_1 (gdb_stdlog, 0, max_level, die);
23374}
23375
f9aca02d 23376static void
51545339 23377store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23378{
51545339 23379 void **slot;
c906108c 23380
9c541725
PA
23381 slot = htab_find_slot_with_hash (cu->die_hash, die,
23382 to_underlying (die->sect_off),
b64f50a1 23383 INSERT);
51545339
DJ
23384
23385 *slot = die;
c906108c
SS
23386}
23387
b64f50a1
JK
23388/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23389 required kind. */
23390
23391static sect_offset
ff39bb5e 23392dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23393{
7771576e 23394 if (attr_form_is_ref (attr))
9c541725 23395 return (sect_offset) DW_UNSND (attr);
93311388 23396
b98664d3 23397 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23398 dwarf_form_name (attr->form));
9c541725 23399 return {};
c906108c
SS
23400}
23401
43bbcdc2
PH
23402/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23403 * the value held by the attribute is not constant. */
a02abb62 23404
43bbcdc2 23405static LONGEST
ff39bb5e 23406dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23407{
663c44ac 23408 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23409 return DW_SND (attr);
23410 else if (attr->form == DW_FORM_udata
23411 || attr->form == DW_FORM_data1
23412 || attr->form == DW_FORM_data2
23413 || attr->form == DW_FORM_data4
23414 || attr->form == DW_FORM_data8)
23415 return DW_UNSND (attr);
23416 else
23417 {
0224619f 23418 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23419 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23420 dwarf_form_name (attr->form));
23421 return default_value;
23422 }
23423}
23424
348e048f
DE
23425/* Follow reference or signature attribute ATTR of SRC_DIE.
23426 On entry *REF_CU is the CU of SRC_DIE.
23427 On exit *REF_CU is the CU of the result. */
23428
23429static struct die_info *
ff39bb5e 23430follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23431 struct dwarf2_cu **ref_cu)
23432{
23433 struct die_info *die;
23434
7771576e 23435 if (attr_form_is_ref (attr))
348e048f 23436 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23437 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23438 die = follow_die_sig (src_die, attr, ref_cu);
23439 else
23440 {
23441 dump_die_for_error (src_die);
23442 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23443 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23444 }
23445
23446 return die;
03dd20cc
DJ
23447}
23448
5c631832 23449/* Follow reference OFFSET.
673bfd45
DE
23450 On entry *REF_CU is the CU of the source die referencing OFFSET.
23451 On exit *REF_CU is the CU of the result.
23452 Returns NULL if OFFSET is invalid. */
f504f079 23453
f9aca02d 23454static struct die_info *
9c541725 23455follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23456 struct dwarf2_cu **ref_cu)
c906108c 23457{
10b3939b 23458 struct die_info temp_die;
f2f0e013 23459 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23460 struct dwarf2_per_objfile *dwarf2_per_objfile
23461 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23462
348e048f
DE
23463 gdb_assert (cu->per_cu != NULL);
23464
98bfdba5
PA
23465 target_cu = cu;
23466
3019eac3 23467 if (cu->per_cu->is_debug_types)
348e048f
DE
23468 {
23469 /* .debug_types CUs cannot reference anything outside their CU.
23470 If they need to, they have to reference a signatured type via
55f1336d 23471 DW_FORM_ref_sig8. */
9c541725 23472 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23473 return NULL;
348e048f 23474 }
36586728 23475 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23476 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23477 {
23478 struct dwarf2_per_cu_data *per_cu;
9a619af0 23479
9c541725 23480 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23481 dwarf2_per_objfile);
03dd20cc
DJ
23482
23483 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23484 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23485 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23486
10b3939b
DJ
23487 target_cu = per_cu->cu;
23488 }
98bfdba5
PA
23489 else if (cu->dies == NULL)
23490 {
23491 /* We're loading full DIEs during partial symbol reading. */
23492 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23493 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23494 }
c906108c 23495
f2f0e013 23496 *ref_cu = target_cu;
9c541725 23497 temp_die.sect_off = sect_off;
c24bdb02
KS
23498
23499 if (target_cu != cu)
23500 target_cu->ancestor = cu;
23501
9a3c8263 23502 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23503 &temp_die,
23504 to_underlying (sect_off));
5c631832 23505}
10b3939b 23506
5c631832
JK
23507/* Follow reference attribute ATTR of SRC_DIE.
23508 On entry *REF_CU is the CU of SRC_DIE.
23509 On exit *REF_CU is the CU of the result. */
23510
23511static struct die_info *
ff39bb5e 23512follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23513 struct dwarf2_cu **ref_cu)
23514{
9c541725 23515 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23516 struct dwarf2_cu *cu = *ref_cu;
23517 struct die_info *die;
23518
9c541725 23519 die = follow_die_offset (sect_off,
36586728
TT
23520 (attr->form == DW_FORM_GNU_ref_alt
23521 || cu->per_cu->is_dwz),
23522 ref_cu);
5c631832 23523 if (!die)
9d8780f0
SM
23524 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23525 "at %s [in module %s]"),
23526 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23527 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23528
5c631832
JK
23529 return die;
23530}
23531
9c541725 23532/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23533 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23534 dwarf2_locexpr_baton->data has lifetime of
23535 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23536
23537struct dwarf2_locexpr_baton
9c541725 23538dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23539 struct dwarf2_per_cu_data *per_cu,
23540 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23541 void *baton, bool resolve_abstract_p)
5c631832 23542{
918dd910 23543 struct dwarf2_cu *cu;
5c631832
JK
23544 struct die_info *die;
23545 struct attribute *attr;
23546 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23547 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23548 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23549
918dd910 23550 if (per_cu->cu == NULL)
58f0c718 23551 load_cu (per_cu, false);
918dd910 23552 cu = per_cu->cu;
cc12ce38
DE
23553 if (cu == NULL)
23554 {
23555 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23556 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23557 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23558 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23559 }
918dd910 23560
9c541725 23561 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23562 if (!die)
9d8780f0
SM
23563 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23564 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23565
23566 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23567 if (!attr && resolve_abstract_p
3360b6e7 23568 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23569 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23570 {
23571 CORE_ADDR pc = (*get_frame_pc) (baton);
eba4caf2
TV
23572 CORE_ADDR baseaddr
23573 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23574 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23575
3360b6e7
TV
23576 for (const auto &cand_off
23577 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23578 {
3360b6e7
TV
23579 struct dwarf2_cu *cand_cu = cu;
23580 struct die_info *cand
23581 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23582 if (!cand
23583 || !cand->parent
e4a62c65
TV
23584 || cand->parent->tag != DW_TAG_subprogram)
23585 continue;
23586
23587 CORE_ADDR pc_low, pc_high;
23588 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23589 if (pc_low == ((CORE_ADDR) -1))
23590 continue;
23591 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23592 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23593 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23594 continue;
23595
23596 die = cand;
23597 attr = dwarf2_attr (die, DW_AT_location, cu);
23598 break;
23599 }
23600 }
23601
5c631832
JK
23602 if (!attr)
23603 {
e103e986
JK
23604 /* DWARF: "If there is no such attribute, then there is no effect.".
23605 DATA is ignored if SIZE is 0. */
5c631832 23606
e103e986 23607 retval.data = NULL;
5c631832
JK
23608 retval.size = 0;
23609 }
8cf6f0b1
TT
23610 else if (attr_form_is_section_offset (attr))
23611 {
23612 struct dwarf2_loclist_baton loclist_baton;
23613 CORE_ADDR pc = (*get_frame_pc) (baton);
23614 size_t size;
23615
23616 fill_in_loclist_baton (cu, &loclist_baton, attr);
23617
23618 retval.data = dwarf2_find_location_expression (&loclist_baton,
23619 &size, pc);
23620 retval.size = size;
23621 }
5c631832
JK
23622 else
23623 {
23624 if (!attr_form_is_block (attr))
9d8780f0 23625 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23626 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23627 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23628
23629 retval.data = DW_BLOCK (attr)->data;
23630 retval.size = DW_BLOCK (attr)->size;
23631 }
23632 retval.per_cu = cu->per_cu;
918dd910 23633
ed2dc618 23634 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23635
5c631832 23636 return retval;
348e048f
DE
23637}
23638
8b9737bf
TT
23639/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23640 offset. */
23641
23642struct dwarf2_locexpr_baton
23643dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23644 struct dwarf2_per_cu_data *per_cu,
23645 CORE_ADDR (*get_frame_pc) (void *baton),
23646 void *baton)
23647{
9c541725 23648 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23649
9c541725 23650 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23651}
23652
b6807d98
TT
23653/* Write a constant of a given type as target-ordered bytes into
23654 OBSTACK. */
23655
23656static const gdb_byte *
23657write_constant_as_bytes (struct obstack *obstack,
23658 enum bfd_endian byte_order,
23659 struct type *type,
23660 ULONGEST value,
23661 LONGEST *len)
23662{
23663 gdb_byte *result;
23664
23665 *len = TYPE_LENGTH (type);
224c3ddb 23666 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23667 store_unsigned_integer (result, *len, byte_order, value);
23668
23669 return result;
23670}
23671
23672/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23673 pointer to the constant bytes and set LEN to the length of the
23674 data. If memory is needed, allocate it on OBSTACK. If the DIE
23675 does not have a DW_AT_const_value, return NULL. */
23676
23677const gdb_byte *
9c541725 23678dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23679 struct dwarf2_per_cu_data *per_cu,
23680 struct obstack *obstack,
23681 LONGEST *len)
23682{
23683 struct dwarf2_cu *cu;
23684 struct die_info *die;
23685 struct attribute *attr;
23686 const gdb_byte *result = NULL;
23687 struct type *type;
23688 LONGEST value;
23689 enum bfd_endian byte_order;
e3b94546 23690 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23691
b6807d98 23692 if (per_cu->cu == NULL)
58f0c718 23693 load_cu (per_cu, false);
b6807d98 23694 cu = per_cu->cu;
cc12ce38
DE
23695 if (cu == NULL)
23696 {
23697 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23698 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23699 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23700 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23701 }
b6807d98 23702
9c541725 23703 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23704 if (!die)
9d8780f0
SM
23705 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23706 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23707
23708 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23709 if (attr == NULL)
23710 return NULL;
23711
e3b94546 23712 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23713 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23714
23715 switch (attr->form)
23716 {
23717 case DW_FORM_addr:
336d760d 23718 case DW_FORM_addrx:
b6807d98
TT
23719 case DW_FORM_GNU_addr_index:
23720 {
23721 gdb_byte *tem;
23722
23723 *len = cu->header.addr_size;
224c3ddb 23724 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23725 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23726 result = tem;
23727 }
23728 break;
23729 case DW_FORM_string:
23730 case DW_FORM_strp:
cf532bd1 23731 case DW_FORM_strx:
b6807d98
TT
23732 case DW_FORM_GNU_str_index:
23733 case DW_FORM_GNU_strp_alt:
23734 /* DW_STRING is already allocated on the objfile obstack, point
23735 directly to it. */
23736 result = (const gdb_byte *) DW_STRING (attr);
23737 *len = strlen (DW_STRING (attr));
23738 break;
23739 case DW_FORM_block1:
23740 case DW_FORM_block2:
23741 case DW_FORM_block4:
23742 case DW_FORM_block:
23743 case DW_FORM_exprloc:
0224619f 23744 case DW_FORM_data16:
b6807d98
TT
23745 result = DW_BLOCK (attr)->data;
23746 *len = DW_BLOCK (attr)->size;
23747 break;
23748
23749 /* The DW_AT_const_value attributes are supposed to carry the
23750 symbol's value "represented as it would be on the target
23751 architecture." By the time we get here, it's already been
23752 converted to host endianness, so we just need to sign- or
23753 zero-extend it as appropriate. */
23754 case DW_FORM_data1:
23755 type = die_type (die, cu);
23756 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23757 if (result == NULL)
23758 result = write_constant_as_bytes (obstack, byte_order,
23759 type, value, len);
23760 break;
23761 case DW_FORM_data2:
23762 type = die_type (die, cu);
23763 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23764 if (result == NULL)
23765 result = write_constant_as_bytes (obstack, byte_order,
23766 type, value, len);
23767 break;
23768 case DW_FORM_data4:
23769 type = die_type (die, cu);
23770 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23771 if (result == NULL)
23772 result = write_constant_as_bytes (obstack, byte_order,
23773 type, value, len);
23774 break;
23775 case DW_FORM_data8:
23776 type = die_type (die, cu);
23777 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23778 if (result == NULL)
23779 result = write_constant_as_bytes (obstack, byte_order,
23780 type, value, len);
23781 break;
23782
23783 case DW_FORM_sdata:
663c44ac 23784 case DW_FORM_implicit_const:
b6807d98
TT
23785 type = die_type (die, cu);
23786 result = write_constant_as_bytes (obstack, byte_order,
23787 type, DW_SND (attr), len);
23788 break;
23789
23790 case DW_FORM_udata:
23791 type = die_type (die, cu);
23792 result = write_constant_as_bytes (obstack, byte_order,
23793 type, DW_UNSND (attr), len);
23794 break;
23795
23796 default:
b98664d3 23797 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23798 dwarf_form_name (attr->form));
23799 break;
23800 }
23801
23802 return result;
23803}
23804
7942e96e
AA
23805/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23806 valid type for this die is found. */
23807
23808struct type *
9c541725 23809dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23810 struct dwarf2_per_cu_data *per_cu)
23811{
23812 struct dwarf2_cu *cu;
23813 struct die_info *die;
23814
7942e96e 23815 if (per_cu->cu == NULL)
58f0c718 23816 load_cu (per_cu, false);
7942e96e
AA
23817 cu = per_cu->cu;
23818 if (!cu)
23819 return NULL;
23820
9c541725 23821 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23822 if (!die)
23823 return NULL;
23824
23825 return die_type (die, cu);
23826}
23827
8a9b8146
TT
23828/* Return the type of the DIE at DIE_OFFSET in the CU named by
23829 PER_CU. */
23830
23831struct type *
b64f50a1 23832dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23833 struct dwarf2_per_cu_data *per_cu)
23834{
9c541725 23835 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23836 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23837}
23838
ac9ec31b 23839/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23840 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23841 On exit *REF_CU is the CU of the result.
23842 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23843
23844static struct die_info *
ac9ec31b
DE
23845follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23846 struct dwarf2_cu **ref_cu)
348e048f 23847{
348e048f 23848 struct die_info temp_die;
c24bdb02 23849 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23850 struct die_info *die;
23851
ac9ec31b
DE
23852 /* While it might be nice to assert sig_type->type == NULL here,
23853 we can get here for DW_AT_imported_declaration where we need
23854 the DIE not the type. */
348e048f
DE
23855
23856 /* If necessary, add it to the queue and load its DIEs. */
23857
95554aad 23858 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23859 read_signatured_type (sig_type);
348e048f 23860
348e048f 23861 sig_cu = sig_type->per_cu.cu;
69d751e3 23862 gdb_assert (sig_cu != NULL);
9c541725
PA
23863 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23864 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23865 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23866 to_underlying (temp_die.sect_off));
348e048f
DE
23867 if (die)
23868 {
ed2dc618 23869 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23870 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23871
796a7ff8
DE
23872 /* For .gdb_index version 7 keep track of included TUs.
23873 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23874 if (dwarf2_per_objfile->index_table != NULL
23875 && dwarf2_per_objfile->index_table->version <= 7)
23876 {
ae640021 23877 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23878 }
23879
348e048f 23880 *ref_cu = sig_cu;
c24bdb02
KS
23881 if (sig_cu != cu)
23882 sig_cu->ancestor = cu;
23883
348e048f
DE
23884 return die;
23885 }
23886
ac9ec31b
DE
23887 return NULL;
23888}
23889
23890/* Follow signatured type referenced by ATTR in SRC_DIE.
23891 On entry *REF_CU is the CU of SRC_DIE.
23892 On exit *REF_CU is the CU of the result.
23893 The result is the DIE of the type.
23894 If the referenced type cannot be found an error is thrown. */
23895
23896static struct die_info *
ff39bb5e 23897follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23898 struct dwarf2_cu **ref_cu)
23899{
23900 ULONGEST signature = DW_SIGNATURE (attr);
23901 struct signatured_type *sig_type;
23902 struct die_info *die;
23903
23904 gdb_assert (attr->form == DW_FORM_ref_sig8);
23905
a2ce51a0 23906 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23907 /* sig_type will be NULL if the signatured type is missing from
23908 the debug info. */
23909 if (sig_type == NULL)
23910 {
23911 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23912 " from DIE at %s [in module %s]"),
23913 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23914 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23915 }
23916
23917 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23918 if (die == NULL)
23919 {
23920 dump_die_for_error (src_die);
23921 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23922 " from DIE at %s [in module %s]"),
23923 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23924 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23925 }
23926
23927 return die;
23928}
23929
23930/* Get the type specified by SIGNATURE referenced in DIE/CU,
23931 reading in and processing the type unit if necessary. */
23932
23933static struct type *
23934get_signatured_type (struct die_info *die, ULONGEST signature,
23935 struct dwarf2_cu *cu)
23936{
518817b3
SM
23937 struct dwarf2_per_objfile *dwarf2_per_objfile
23938 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23939 struct signatured_type *sig_type;
23940 struct dwarf2_cu *type_cu;
23941 struct die_info *type_die;
23942 struct type *type;
23943
a2ce51a0 23944 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23945 /* sig_type will be NULL if the signatured type is missing from
23946 the debug info. */
23947 if (sig_type == NULL)
23948 {
b98664d3 23949 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23950 " from DIE at %s [in module %s]"),
23951 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23952 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23953 return build_error_marker_type (cu, die);
23954 }
23955
23956 /* If we already know the type we're done. */
23957 if (sig_type->type != NULL)
23958 return sig_type->type;
23959
23960 type_cu = cu;
23961 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23962 if (type_die != NULL)
23963 {
23964 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23965 is created. This is important, for example, because for c++ classes
23966 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23967 type = read_type_die (type_die, type_cu);
23968 if (type == NULL)
23969 {
b98664d3 23970 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23971 " referenced from DIE at %s [in module %s]"),
23972 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23973 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23974 type = build_error_marker_type (cu, die);
23975 }
23976 }
23977 else
23978 {
b98664d3 23979 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23980 " from DIE at %s [in module %s]"),
23981 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23982 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23983 type = build_error_marker_type (cu, die);
23984 }
23985 sig_type->type = type;
23986
23987 return type;
23988}
23989
23990/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23991 reading in and processing the type unit if necessary. */
23992
23993static struct type *
ff39bb5e 23994get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23995 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23996{
23997 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23998 if (attr_form_is_ref (attr))
ac9ec31b
DE
23999 {
24000 struct dwarf2_cu *type_cu = cu;
24001 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24002
24003 return read_type_die (type_die, type_cu);
24004 }
24005 else if (attr->form == DW_FORM_ref_sig8)
24006 {
24007 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24008 }
24009 else
24010 {
518817b3
SM
24011 struct dwarf2_per_objfile *dwarf2_per_objfile
24012 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24013
b98664d3 24014 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24015 " at %s [in module %s]"),
24016 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 24017 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24018 return build_error_marker_type (cu, die);
24019 }
348e048f
DE
24020}
24021
e5fe5e75 24022/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24023
24024static void
e5fe5e75 24025load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 24026{
52dc124a 24027 struct signatured_type *sig_type;
348e048f 24028
f4dc4d17
DE
24029 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24030 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24031
6721b2ec
DE
24032 /* We have the per_cu, but we need the signatured_type.
24033 Fortunately this is an easy translation. */
24034 gdb_assert (per_cu->is_debug_types);
24035 sig_type = (struct signatured_type *) per_cu;
348e048f 24036
6721b2ec 24037 gdb_assert (per_cu->cu == NULL);
348e048f 24038
52dc124a 24039 read_signatured_type (sig_type);
348e048f 24040
6721b2ec 24041 gdb_assert (per_cu->cu != NULL);
348e048f
DE
24042}
24043
dee91e82
DE
24044/* die_reader_func for read_signatured_type.
24045 This is identical to load_full_comp_unit_reader,
24046 but is kept separate for now. */
348e048f
DE
24047
24048static void
dee91e82 24049read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 24050 const gdb_byte *info_ptr,
dee91e82
DE
24051 struct die_info *comp_unit_die,
24052 int has_children,
24053 void *data)
348e048f 24054{
dee91e82 24055 struct dwarf2_cu *cu = reader->cu;
348e048f 24056
dee91e82
DE
24057 gdb_assert (cu->die_hash == NULL);
24058 cu->die_hash =
24059 htab_create_alloc_ex (cu->header.length / 12,
24060 die_hash,
24061 die_eq,
24062 NULL,
24063 &cu->comp_unit_obstack,
24064 hashtab_obstack_allocate,
24065 dummy_obstack_deallocate);
348e048f 24066
dee91e82
DE
24067 if (has_children)
24068 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24069 &info_ptr, comp_unit_die);
24070 cu->dies = comp_unit_die;
24071 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
24072
24073 /* We try not to read any attributes in this function, because not
9cdd5dbd 24074 all CUs needed for references have been loaded yet, and symbol
348e048f 24075 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
24076 or we won't be able to build types correctly.
24077 Similarly, if we do not read the producer, we can not apply
24078 producer-specific interpretation. */
95554aad 24079 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 24080}
348e048f 24081
3019eac3
DE
24082/* Read in a signatured type and build its CU and DIEs.
24083 If the type is a stub for the real type in a DWO file,
24084 read in the real type from the DWO file as well. */
dee91e82
DE
24085
24086static void
24087read_signatured_type (struct signatured_type *sig_type)
24088{
24089 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24090
3019eac3 24091 gdb_assert (per_cu->is_debug_types);
dee91e82 24092 gdb_assert (per_cu->cu == NULL);
348e048f 24093
58f0c718 24094 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 24095 read_signatured_type_reader, NULL);
7ee85ab1 24096 sig_type->per_cu.tu_read = 1;
c906108c
SS
24097}
24098
c906108c
SS
24099/* Decode simple location descriptions.
24100 Given a pointer to a dwarf block that defines a location, compute
24101 the location and return the value.
24102
4cecd739
DJ
24103 NOTE drow/2003-11-18: This function is called in two situations
24104 now: for the address of static or global variables (partial symbols
24105 only) and for offsets into structures which are expected to be
24106 (more or less) constant. The partial symbol case should go away,
24107 and only the constant case should remain. That will let this
24108 function complain more accurately. A few special modes are allowed
24109 without complaint for global variables (for instance, global
24110 register values and thread-local values).
c906108c
SS
24111
24112 A location description containing no operations indicates that the
4cecd739 24113 object is optimized out. The return value is 0 for that case.
6b992462
DJ
24114 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24115 callers will only want a very basic result and this can become a
21ae7a4d
JK
24116 complaint.
24117
24118 Note that stack[0] is unused except as a default error return. */
c906108c
SS
24119
24120static CORE_ADDR
e7c27a73 24121decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 24122{
518817b3 24123 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
24124 size_t i;
24125 size_t size = blk->size;
d521ce57 24126 const gdb_byte *data = blk->data;
21ae7a4d
JK
24127 CORE_ADDR stack[64];
24128 int stacki;
24129 unsigned int bytes_read, unsnd;
24130 gdb_byte op;
c906108c 24131
21ae7a4d
JK
24132 i = 0;
24133 stacki = 0;
24134 stack[stacki] = 0;
24135 stack[++stacki] = 0;
24136
24137 while (i < size)
24138 {
24139 op = data[i++];
24140 switch (op)
24141 {
24142 case DW_OP_lit0:
24143 case DW_OP_lit1:
24144 case DW_OP_lit2:
24145 case DW_OP_lit3:
24146 case DW_OP_lit4:
24147 case DW_OP_lit5:
24148 case DW_OP_lit6:
24149 case DW_OP_lit7:
24150 case DW_OP_lit8:
24151 case DW_OP_lit9:
24152 case DW_OP_lit10:
24153 case DW_OP_lit11:
24154 case DW_OP_lit12:
24155 case DW_OP_lit13:
24156 case DW_OP_lit14:
24157 case DW_OP_lit15:
24158 case DW_OP_lit16:
24159 case DW_OP_lit17:
24160 case DW_OP_lit18:
24161 case DW_OP_lit19:
24162 case DW_OP_lit20:
24163 case DW_OP_lit21:
24164 case DW_OP_lit22:
24165 case DW_OP_lit23:
24166 case DW_OP_lit24:
24167 case DW_OP_lit25:
24168 case DW_OP_lit26:
24169 case DW_OP_lit27:
24170 case DW_OP_lit28:
24171 case DW_OP_lit29:
24172 case DW_OP_lit30:
24173 case DW_OP_lit31:
24174 stack[++stacki] = op - DW_OP_lit0;
24175 break;
f1bea926 24176
21ae7a4d
JK
24177 case DW_OP_reg0:
24178 case DW_OP_reg1:
24179 case DW_OP_reg2:
24180 case DW_OP_reg3:
24181 case DW_OP_reg4:
24182 case DW_OP_reg5:
24183 case DW_OP_reg6:
24184 case DW_OP_reg7:
24185 case DW_OP_reg8:
24186 case DW_OP_reg9:
24187 case DW_OP_reg10:
24188 case DW_OP_reg11:
24189 case DW_OP_reg12:
24190 case DW_OP_reg13:
24191 case DW_OP_reg14:
24192 case DW_OP_reg15:
24193 case DW_OP_reg16:
24194 case DW_OP_reg17:
24195 case DW_OP_reg18:
24196 case DW_OP_reg19:
24197 case DW_OP_reg20:
24198 case DW_OP_reg21:
24199 case DW_OP_reg22:
24200 case DW_OP_reg23:
24201 case DW_OP_reg24:
24202 case DW_OP_reg25:
24203 case DW_OP_reg26:
24204 case DW_OP_reg27:
24205 case DW_OP_reg28:
24206 case DW_OP_reg29:
24207 case DW_OP_reg30:
24208 case DW_OP_reg31:
24209 stack[++stacki] = op - DW_OP_reg0;
24210 if (i < size)
24211 dwarf2_complex_location_expr_complaint ();
24212 break;
c906108c 24213
21ae7a4d
JK
24214 case DW_OP_regx:
24215 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24216 i += bytes_read;
24217 stack[++stacki] = unsnd;
24218 if (i < size)
24219 dwarf2_complex_location_expr_complaint ();
24220 break;
c906108c 24221
21ae7a4d
JK
24222 case DW_OP_addr:
24223 stack[++stacki] = read_address (objfile->obfd, &data[i],
24224 cu, &bytes_read);
24225 i += bytes_read;
24226 break;
d53d4ac5 24227
21ae7a4d
JK
24228 case DW_OP_const1u:
24229 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24230 i += 1;
24231 break;
24232
24233 case DW_OP_const1s:
24234 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24235 i += 1;
24236 break;
24237
24238 case DW_OP_const2u:
24239 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24240 i += 2;
24241 break;
24242
24243 case DW_OP_const2s:
24244 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24245 i += 2;
24246 break;
d53d4ac5 24247
21ae7a4d
JK
24248 case DW_OP_const4u:
24249 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24250 i += 4;
24251 break;
24252
24253 case DW_OP_const4s:
24254 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24255 i += 4;
24256 break;
24257
585861ea
JK
24258 case DW_OP_const8u:
24259 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24260 i += 8;
24261 break;
24262
21ae7a4d
JK
24263 case DW_OP_constu:
24264 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24265 &bytes_read);
24266 i += bytes_read;
24267 break;
24268
24269 case DW_OP_consts:
24270 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24271 i += bytes_read;
24272 break;
24273
24274 case DW_OP_dup:
24275 stack[stacki + 1] = stack[stacki];
24276 stacki++;
24277 break;
24278
24279 case DW_OP_plus:
24280 stack[stacki - 1] += stack[stacki];
24281 stacki--;
24282 break;
24283
24284 case DW_OP_plus_uconst:
24285 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24286 &bytes_read);
24287 i += bytes_read;
24288 break;
24289
24290 case DW_OP_minus:
24291 stack[stacki - 1] -= stack[stacki];
24292 stacki--;
24293 break;
24294
24295 case DW_OP_deref:
24296 /* If we're not the last op, then we definitely can't encode
24297 this using GDB's address_class enum. This is valid for partial
24298 global symbols, although the variable's address will be bogus
24299 in the psymtab. */
24300 if (i < size)
24301 dwarf2_complex_location_expr_complaint ();
24302 break;
24303
24304 case DW_OP_GNU_push_tls_address:
4aa4e28b 24305 case DW_OP_form_tls_address:
21ae7a4d
JK
24306 /* The top of the stack has the offset from the beginning
24307 of the thread control block at which the variable is located. */
24308 /* Nothing should follow this operator, so the top of stack would
24309 be returned. */
24310 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24311 address will be bogus in the psymtab. Make it always at least
24312 non-zero to not look as a variable garbage collected by linker
24313 which have DW_OP_addr 0. */
21ae7a4d
JK
24314 if (i < size)
24315 dwarf2_complex_location_expr_complaint ();
585861ea 24316 stack[stacki]++;
21ae7a4d
JK
24317 break;
24318
24319 case DW_OP_GNU_uninit:
24320 break;
24321
336d760d 24322 case DW_OP_addrx:
3019eac3 24323 case DW_OP_GNU_addr_index:
49f6c839 24324 case DW_OP_GNU_const_index:
3019eac3
DE
24325 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24326 &bytes_read);
24327 i += bytes_read;
24328 break;
24329
21ae7a4d
JK
24330 default:
24331 {
f39c6ffd 24332 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24333
24334 if (name)
b98664d3 24335 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24336 name);
24337 else
b98664d3 24338 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24339 op);
24340 }
24341
24342 return (stack[stacki]);
d53d4ac5 24343 }
3c6e0cb3 24344
21ae7a4d
JK
24345 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24346 outside of the allocated space. Also enforce minimum>0. */
24347 if (stacki >= ARRAY_SIZE (stack) - 1)
24348 {
b98664d3 24349 complaint (_("location description stack overflow"));
21ae7a4d
JK
24350 return 0;
24351 }
24352
24353 if (stacki <= 0)
24354 {
b98664d3 24355 complaint (_("location description stack underflow"));
21ae7a4d
JK
24356 return 0;
24357 }
24358 }
24359 return (stack[stacki]);
c906108c
SS
24360}
24361
24362/* memory allocation interface */
24363
c906108c 24364static struct dwarf_block *
7b5a2f43 24365dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24366{
8d749320 24367 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24368}
24369
c906108c 24370static struct die_info *
b60c80d6 24371dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24372{
24373 struct die_info *die;
b60c80d6
DJ
24374 size_t size = sizeof (struct die_info);
24375
24376 if (num_attrs > 1)
24377 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24378
b60c80d6 24379 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24380 memset (die, 0, sizeof (struct die_info));
24381 return (die);
24382}
2e276125
JB
24383
24384\f
24385/* Macro support. */
24386
233d95b5
JK
24387/* Return file name relative to the compilation directory of file number I in
24388 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24389 responsible for freeing it. */
233d95b5 24390
2e276125 24391static char *
233d95b5 24392file_file_name (int file, struct line_header *lh)
2e276125 24393{
6a83a1e6
EZ
24394 /* Is the file number a valid index into the line header's file name
24395 table? Remember that file numbers start with one, not zero. */
7ba99d21 24396 if (lh->is_valid_file_index (file))
6a83a1e6 24397 {
7ba99d21 24398 const file_entry *fe = lh->file_name_at (file);
6e70227d 24399
7ba99d21 24400 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24401 {
7ba99d21 24402 const char *dir = fe->include_dir (lh);
8c43009f 24403 if (dir != NULL)
7ba99d21 24404 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24405 }
7ba99d21 24406 return xstrdup (fe->name);
6a83a1e6 24407 }
2e276125
JB
24408 else
24409 {
6a83a1e6
EZ
24410 /* The compiler produced a bogus file number. We can at least
24411 record the macro definitions made in the file, even if we
24412 won't be able to find the file by name. */
24413 char fake_name[80];
9a619af0 24414
8c042590
PM
24415 xsnprintf (fake_name, sizeof (fake_name),
24416 "<bad macro file number %d>", file);
2e276125 24417
b98664d3 24418 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24419 file);
2e276125 24420
6a83a1e6 24421 return xstrdup (fake_name);
2e276125
JB
24422 }
24423}
24424
233d95b5
JK
24425/* Return the full name of file number I in *LH's file name table.
24426 Use COMP_DIR as the name of the current directory of the
24427 compilation. The result is allocated using xmalloc; the caller is
24428 responsible for freeing it. */
24429static char *
24430file_full_name (int file, struct line_header *lh, const char *comp_dir)
24431{
24432 /* Is the file number a valid index into the line header's file name
24433 table? Remember that file numbers start with one, not zero. */
7ba99d21 24434 if (lh->is_valid_file_index (file))
233d95b5
JK
24435 {
24436 char *relative = file_file_name (file, lh);
24437
24438 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24439 return relative;
b36cec19
PA
24440 return reconcat (relative, comp_dir, SLASH_STRING,
24441 relative, (char *) NULL);
233d95b5
JK
24442 }
24443 else
24444 return file_file_name (file, lh);
24445}
24446
2e276125
JB
24447
24448static struct macro_source_file *
804d2729
TT
24449macro_start_file (struct dwarf2_cu *cu,
24450 int file, int line,
2e276125 24451 struct macro_source_file *current_file,
43f3e411 24452 struct line_header *lh)
2e276125 24453{
233d95b5
JK
24454 /* File name relative to the compilation directory of this source file. */
24455 char *file_name = file_file_name (file, lh);
2e276125 24456
2e276125 24457 if (! current_file)
abc9d0dc 24458 {
fc474241
DE
24459 /* Note: We don't create a macro table for this compilation unit
24460 at all until we actually get a filename. */
c24bdb02 24461 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24462
abc9d0dc
TT
24463 /* If we have no current file, then this must be the start_file
24464 directive for the compilation unit's main source file. */
fc474241
DE
24465 current_file = macro_set_main (macro_table, file_name);
24466 macro_define_special (macro_table);
abc9d0dc 24467 }
2e276125 24468 else
233d95b5 24469 current_file = macro_include (current_file, line, file_name);
2e276125 24470
233d95b5 24471 xfree (file_name);
6e70227d 24472
2e276125
JB
24473 return current_file;
24474}
24475
2e276125
JB
24476static const char *
24477consume_improper_spaces (const char *p, const char *body)
24478{
24479 if (*p == ' ')
24480 {
b98664d3 24481 complaint (_("macro definition contains spaces "
3e43a32a 24482 "in formal argument list:\n`%s'"),
4d3c2250 24483 body);
2e276125
JB
24484
24485 while (*p == ' ')
24486 p++;
24487 }
24488
24489 return p;
24490}
24491
24492
24493static void
24494parse_macro_definition (struct macro_source_file *file, int line,
24495 const char *body)
24496{
24497 const char *p;
24498
24499 /* The body string takes one of two forms. For object-like macro
24500 definitions, it should be:
24501
24502 <macro name> " " <definition>
24503
24504 For function-like macro definitions, it should be:
24505
24506 <macro name> "() " <definition>
24507 or
24508 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24509
24510 Spaces may appear only where explicitly indicated, and in the
24511 <definition>.
24512
24513 The Dwarf 2 spec says that an object-like macro's name is always
24514 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24515 the space when the macro's definition is the empty string.
2e276125
JB
24516
24517 The Dwarf 2 spec says that there should be no spaces between the
24518 formal arguments in a function-like macro's formal argument list,
24519 but versions of GCC around March 2002 include spaces after the
24520 commas. */
24521
24522
24523 /* Find the extent of the macro name. The macro name is terminated
24524 by either a space or null character (for an object-like macro) or
24525 an opening paren (for a function-like macro). */
24526 for (p = body; *p; p++)
24527 if (*p == ' ' || *p == '(')
24528 break;
24529
24530 if (*p == ' ' || *p == '\0')
24531 {
24532 /* It's an object-like macro. */
24533 int name_len = p - body;
3f8a7804 24534 char *name = savestring (body, name_len);
2e276125
JB
24535 const char *replacement;
24536
24537 if (*p == ' ')
24538 replacement = body + name_len + 1;
24539 else
24540 {
4d3c2250 24541 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24542 replacement = body + name_len;
24543 }
6e70227d 24544
2e276125
JB
24545 macro_define_object (file, line, name, replacement);
24546
24547 xfree (name);
24548 }
24549 else if (*p == '(')
24550 {
24551 /* It's a function-like macro. */
3f8a7804 24552 char *name = savestring (body, p - body);
2e276125
JB
24553 int argc = 0;
24554 int argv_size = 1;
8d749320 24555 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24556
24557 p++;
24558
24559 p = consume_improper_spaces (p, body);
24560
24561 /* Parse the formal argument list. */
24562 while (*p && *p != ')')
24563 {
24564 /* Find the extent of the current argument name. */
24565 const char *arg_start = p;
24566
24567 while (*p && *p != ',' && *p != ')' && *p != ' ')
24568 p++;
24569
24570 if (! *p || p == arg_start)
4d3c2250 24571 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24572 else
24573 {
24574 /* Make sure argv has room for the new argument. */
24575 if (argc >= argv_size)
24576 {
24577 argv_size *= 2;
224c3ddb 24578 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24579 }
24580
3f8a7804 24581 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24582 }
24583
24584 p = consume_improper_spaces (p, body);
24585
24586 /* Consume the comma, if present. */
24587 if (*p == ',')
24588 {
24589 p++;
24590
24591 p = consume_improper_spaces (p, body);
24592 }
24593 }
24594
24595 if (*p == ')')
24596 {
24597 p++;
24598
24599 if (*p == ' ')
24600 /* Perfectly formed definition, no complaints. */
24601 macro_define_function (file, line, name,
6e70227d 24602 argc, (const char **) argv,
2e276125
JB
24603 p + 1);
24604 else if (*p == '\0')
24605 {
24606 /* Complain, but do define it. */
4d3c2250 24607 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24608 macro_define_function (file, line, name,
6e70227d 24609 argc, (const char **) argv,
2e276125
JB
24610 p);
24611 }
24612 else
24613 /* Just complain. */
4d3c2250 24614 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24615 }
24616 else
24617 /* Just complain. */
4d3c2250 24618 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24619
24620 xfree (name);
24621 {
24622 int i;
24623
24624 for (i = 0; i < argc; i++)
24625 xfree (argv[i]);
24626 }
24627 xfree (argv);
24628 }
24629 else
4d3c2250 24630 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24631}
24632
cf2c3c16
TT
24633/* Skip some bytes from BYTES according to the form given in FORM.
24634 Returns the new pointer. */
2e276125 24635
d521ce57
TT
24636static const gdb_byte *
24637skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24638 enum dwarf_form form,
24639 unsigned int offset_size,
24640 struct dwarf2_section_info *section)
2e276125 24641{
cf2c3c16 24642 unsigned int bytes_read;
2e276125 24643
cf2c3c16 24644 switch (form)
2e276125 24645 {
cf2c3c16
TT
24646 case DW_FORM_data1:
24647 case DW_FORM_flag:
24648 ++bytes;
24649 break;
24650
24651 case DW_FORM_data2:
24652 bytes += 2;
24653 break;
24654
24655 case DW_FORM_data4:
24656 bytes += 4;
24657 break;
24658
24659 case DW_FORM_data8:
24660 bytes += 8;
24661 break;
24662
0224619f
JK
24663 case DW_FORM_data16:
24664 bytes += 16;
24665 break;
24666
cf2c3c16
TT
24667 case DW_FORM_string:
24668 read_direct_string (abfd, bytes, &bytes_read);
24669 bytes += bytes_read;
24670 break;
24671
24672 case DW_FORM_sec_offset:
24673 case DW_FORM_strp:
36586728 24674 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24675 bytes += offset_size;
24676 break;
24677
24678 case DW_FORM_block:
24679 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24680 bytes += bytes_read;
24681 break;
24682
24683 case DW_FORM_block1:
24684 bytes += 1 + read_1_byte (abfd, bytes);
24685 break;
24686 case DW_FORM_block2:
24687 bytes += 2 + read_2_bytes (abfd, bytes);
24688 break;
24689 case DW_FORM_block4:
24690 bytes += 4 + read_4_bytes (abfd, bytes);
24691 break;
24692
336d760d 24693 case DW_FORM_addrx:
cf2c3c16 24694 case DW_FORM_sdata:
cf532bd1 24695 case DW_FORM_strx:
cf2c3c16 24696 case DW_FORM_udata:
3019eac3
DE
24697 case DW_FORM_GNU_addr_index:
24698 case DW_FORM_GNU_str_index:
d521ce57 24699 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24700 if (bytes == NULL)
24701 {
24702 dwarf2_section_buffer_overflow_complaint (section);
24703 return NULL;
24704 }
cf2c3c16
TT
24705 break;
24706
663c44ac
JK
24707 case DW_FORM_implicit_const:
24708 break;
24709
cf2c3c16
TT
24710 default:
24711 {
b98664d3 24712 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24713 form, get_section_name (section));
cf2c3c16
TT
24714 return NULL;
24715 }
2e276125
JB
24716 }
24717
cf2c3c16
TT
24718 return bytes;
24719}
757a13d0 24720
cf2c3c16
TT
24721/* A helper for dwarf_decode_macros that handles skipping an unknown
24722 opcode. Returns an updated pointer to the macro data buffer; or,
24723 on error, issues a complaint and returns NULL. */
757a13d0 24724
d521ce57 24725static const gdb_byte *
cf2c3c16 24726skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24727 const gdb_byte **opcode_definitions,
24728 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24729 bfd *abfd,
24730 unsigned int offset_size,
24731 struct dwarf2_section_info *section)
24732{
24733 unsigned int bytes_read, i;
24734 unsigned long arg;
d521ce57 24735 const gdb_byte *defn;
2e276125 24736
cf2c3c16 24737 if (opcode_definitions[opcode] == NULL)
2e276125 24738 {
b98664d3 24739 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24740 opcode);
24741 return NULL;
24742 }
2e276125 24743
cf2c3c16
TT
24744 defn = opcode_definitions[opcode];
24745 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24746 defn += bytes_read;
2e276125 24747
cf2c3c16
TT
24748 for (i = 0; i < arg; ++i)
24749 {
aead7601
SM
24750 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24751 (enum dwarf_form) defn[i], offset_size,
f664829e 24752 section);
cf2c3c16
TT
24753 if (mac_ptr == NULL)
24754 {
24755 /* skip_form_bytes already issued the complaint. */
24756 return NULL;
24757 }
24758 }
757a13d0 24759
cf2c3c16
TT
24760 return mac_ptr;
24761}
757a13d0 24762
cf2c3c16
TT
24763/* A helper function which parses the header of a macro section.
24764 If the macro section is the extended (for now called "GNU") type,
24765 then this updates *OFFSET_SIZE. Returns a pointer to just after
24766 the header, or issues a complaint and returns NULL on error. */
757a13d0 24767
d521ce57
TT
24768static const gdb_byte *
24769dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24770 bfd *abfd,
d521ce57 24771 const gdb_byte *mac_ptr,
cf2c3c16
TT
24772 unsigned int *offset_size,
24773 int section_is_gnu)
24774{
24775 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24776
cf2c3c16
TT
24777 if (section_is_gnu)
24778 {
24779 unsigned int version, flags;
757a13d0 24780
cf2c3c16 24781 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24782 if (version != 4 && version != 5)
cf2c3c16 24783 {
b98664d3 24784 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24785 version);
24786 return NULL;
24787 }
24788 mac_ptr += 2;
757a13d0 24789
cf2c3c16
TT
24790 flags = read_1_byte (abfd, mac_ptr);
24791 ++mac_ptr;
24792 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24793
cf2c3c16
TT
24794 if ((flags & 2) != 0)
24795 /* We don't need the line table offset. */
24796 mac_ptr += *offset_size;
757a13d0 24797
cf2c3c16
TT
24798 /* Vendor opcode descriptions. */
24799 if ((flags & 4) != 0)
24800 {
24801 unsigned int i, count;
757a13d0 24802
cf2c3c16
TT
24803 count = read_1_byte (abfd, mac_ptr);
24804 ++mac_ptr;
24805 for (i = 0; i < count; ++i)
24806 {
24807 unsigned int opcode, bytes_read;
24808 unsigned long arg;
24809
24810 opcode = read_1_byte (abfd, mac_ptr);
24811 ++mac_ptr;
24812 opcode_definitions[opcode] = mac_ptr;
24813 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24814 mac_ptr += bytes_read;
24815 mac_ptr += arg;
24816 }
757a13d0 24817 }
cf2c3c16 24818 }
757a13d0 24819
cf2c3c16
TT
24820 return mac_ptr;
24821}
757a13d0 24822
cf2c3c16 24823/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24824 including DW_MACRO_import. */
cf2c3c16
TT
24825
24826static void
804d2729 24827dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24828 bfd *abfd,
d521ce57 24829 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24830 struct macro_source_file *current_file,
43f3e411 24831 struct line_header *lh,
cf2c3c16 24832 struct dwarf2_section_info *section,
36586728 24833 int section_is_gnu, int section_is_dwz,
cf2c3c16 24834 unsigned int offset_size,
8fc3fc34 24835 htab_t include_hash)
cf2c3c16 24836{
804d2729
TT
24837 struct dwarf2_per_objfile *dwarf2_per_objfile
24838 = cu->per_cu->dwarf2_per_objfile;
4d663531 24839 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24840 enum dwarf_macro_record_type macinfo_type;
24841 int at_commandline;
d521ce57 24842 const gdb_byte *opcode_definitions[256];
757a13d0 24843
cf2c3c16
TT
24844 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24845 &offset_size, section_is_gnu);
24846 if (mac_ptr == NULL)
24847 {
24848 /* We already issued a complaint. */
24849 return;
24850 }
757a13d0
JK
24851
24852 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24853 GDB is still reading the definitions from command line. First
24854 DW_MACINFO_start_file will need to be ignored as it was already executed
24855 to create CURRENT_FILE for the main source holding also the command line
24856 definitions. On first met DW_MACINFO_start_file this flag is reset to
24857 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24858
24859 at_commandline = 1;
24860
24861 do
24862 {
24863 /* Do we at least have room for a macinfo type byte? */
24864 if (mac_ptr >= mac_end)
24865 {
f664829e 24866 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24867 break;
24868 }
24869
aead7601 24870 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24871 mac_ptr++;
24872
cf2c3c16
TT
24873 /* Note that we rely on the fact that the corresponding GNU and
24874 DWARF constants are the same. */
132448f8
SM
24875 DIAGNOSTIC_PUSH
24876 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24877 switch (macinfo_type)
24878 {
24879 /* A zero macinfo type indicates the end of the macro
24880 information. */
24881 case 0:
24882 break;
2e276125 24883
0af92d60
JK
24884 case DW_MACRO_define:
24885 case DW_MACRO_undef:
24886 case DW_MACRO_define_strp:
24887 case DW_MACRO_undef_strp:
24888 case DW_MACRO_define_sup:
24889 case DW_MACRO_undef_sup:
2e276125 24890 {
891d2f0b 24891 unsigned int bytes_read;
2e276125 24892 int line;
d521ce57 24893 const char *body;
cf2c3c16 24894 int is_define;
2e276125 24895
cf2c3c16
TT
24896 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24897 mac_ptr += bytes_read;
24898
0af92d60
JK
24899 if (macinfo_type == DW_MACRO_define
24900 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24901 {
24902 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24903 mac_ptr += bytes_read;
24904 }
24905 else
24906 {
24907 LONGEST str_offset;
24908
24909 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24910 mac_ptr += offset_size;
2e276125 24911
0af92d60
JK
24912 if (macinfo_type == DW_MACRO_define_sup
24913 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24914 || section_is_dwz)
36586728 24915 {
ed2dc618
SM
24916 struct dwz_file *dwz
24917 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24918
ed2dc618
SM
24919 body = read_indirect_string_from_dwz (objfile,
24920 dwz, str_offset);
36586728
TT
24921 }
24922 else
ed2dc618
SM
24923 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24924 abfd, str_offset);
cf2c3c16
TT
24925 }
24926
0af92d60
JK
24927 is_define = (macinfo_type == DW_MACRO_define
24928 || macinfo_type == DW_MACRO_define_strp
24929 || macinfo_type == DW_MACRO_define_sup);
2e276125 24930 if (! current_file)
757a13d0
JK
24931 {
24932 /* DWARF violation as no main source is present. */
b98664d3 24933 complaint (_("debug info with no main source gives macro %s "
757a13d0 24934 "on line %d: %s"),
cf2c3c16
TT
24935 is_define ? _("definition") : _("undefinition"),
24936 line, body);
757a13d0
JK
24937 break;
24938 }
3e43a32a
MS
24939 if ((line == 0 && !at_commandline)
24940 || (line != 0 && at_commandline))
b98664d3 24941 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24942 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24943 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24944 line == 0 ? _("zero") : _("non-zero"), line, body);
24945
955b06fa 24946 if (body == NULL)
7bede828 24947 {
955b06fa
SDJ
24948 /* Fedora's rpm-build's "debugedit" binary
24949 corrupted .debug_macro sections.
24950
24951 For more info, see
24952 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24953 complaint (_("debug info gives %s invalid macro %s "
24954 "without body (corrupted?) at line %d "
24955 "on file %s"),
24956 at_commandline ? _("command-line") : _("in-file"),
24957 is_define ? _("definition") : _("undefinition"),
24958 line, current_file->filename);
7bede828 24959 }
955b06fa
SDJ
24960 else if (is_define)
24961 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24962 else
24963 {
0af92d60
JK
24964 gdb_assert (macinfo_type == DW_MACRO_undef
24965 || macinfo_type == DW_MACRO_undef_strp
24966 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24967 macro_undef (current_file, line, body);
24968 }
2e276125
JB
24969 }
24970 break;
24971
0af92d60 24972 case DW_MACRO_start_file:
2e276125 24973 {
891d2f0b 24974 unsigned int bytes_read;
2e276125
JB
24975 int line, file;
24976
24977 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24978 mac_ptr += bytes_read;
24979 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24980 mac_ptr += bytes_read;
24981
3e43a32a
MS
24982 if ((line == 0 && !at_commandline)
24983 || (line != 0 && at_commandline))
b98664d3 24984 complaint (_("debug info gives source %d included "
757a13d0
JK
24985 "from %s at %s line %d"),
24986 file, at_commandline ? _("command-line") : _("file"),
24987 line == 0 ? _("zero") : _("non-zero"), line);
24988
24989 if (at_commandline)
24990 {
0af92d60 24991 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24992 pass one. */
757a13d0
JK
24993 at_commandline = 0;
24994 }
24995 else
804d2729
TT
24996 current_file = macro_start_file (cu, file, line, current_file,
24997 lh);
2e276125
JB
24998 }
24999 break;
25000
0af92d60 25001 case DW_MACRO_end_file:
2e276125 25002 if (! current_file)
b98664d3 25003 complaint (_("macro debug info has an unmatched "
3e43a32a 25004 "`close_file' directive"));
2e276125
JB
25005 else
25006 {
25007 current_file = current_file->included_by;
25008 if (! current_file)
25009 {
cf2c3c16 25010 enum dwarf_macro_record_type next_type;
2e276125
JB
25011
25012 /* GCC circa March 2002 doesn't produce the zero
25013 type byte marking the end of the compilation
25014 unit. Complain if it's not there, but exit no
25015 matter what. */
25016
25017 /* Do we at least have room for a macinfo type byte? */
25018 if (mac_ptr >= mac_end)
25019 {
f664829e 25020 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
25021 return;
25022 }
25023
25024 /* We don't increment mac_ptr here, so this is just
25025 a look-ahead. */
aead7601
SM
25026 next_type
25027 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25028 mac_ptr);
2e276125 25029 if (next_type != 0)
b98664d3 25030 complaint (_("no terminating 0-type entry for "
3e43a32a 25031 "macros in `.debug_macinfo' section"));
2e276125
JB
25032
25033 return;
25034 }
25035 }
25036 break;
25037
0af92d60
JK
25038 case DW_MACRO_import:
25039 case DW_MACRO_import_sup:
cf2c3c16
TT
25040 {
25041 LONGEST offset;
8fc3fc34 25042 void **slot;
a036ba48
TT
25043 bfd *include_bfd = abfd;
25044 struct dwarf2_section_info *include_section = section;
d521ce57 25045 const gdb_byte *include_mac_end = mac_end;
a036ba48 25046 int is_dwz = section_is_dwz;
d521ce57 25047 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
25048
25049 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25050 mac_ptr += offset_size;
25051
0af92d60 25052 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 25053 {
ed2dc618 25054 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 25055
4d663531 25056 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 25057
a036ba48 25058 include_section = &dwz->macro;
a32a8923 25059 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
25060 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25061 is_dwz = 1;
25062 }
25063
25064 new_mac_ptr = include_section->buffer + offset;
25065 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25066
8fc3fc34
TT
25067 if (*slot != NULL)
25068 {
25069 /* This has actually happened; see
25070 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 25071 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
25072 ".debug_macro section"));
25073 }
25074 else
25075 {
d521ce57 25076 *slot = (void *) new_mac_ptr;
36586728 25077
804d2729 25078 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 25079 include_mac_end, current_file, lh,
36586728 25080 section, section_is_gnu, is_dwz,
4d663531 25081 offset_size, include_hash);
8fc3fc34 25082
d521ce57 25083 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 25084 }
cf2c3c16
TT
25085 }
25086 break;
25087
2e276125 25088 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
25089 if (!section_is_gnu)
25090 {
25091 unsigned int bytes_read;
2e276125 25092
ac298888
TT
25093 /* This reads the constant, but since we don't recognize
25094 any vendor extensions, we ignore it. */
25095 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
25096 mac_ptr += bytes_read;
25097 read_direct_string (abfd, mac_ptr, &bytes_read);
25098 mac_ptr += bytes_read;
2e276125 25099
cf2c3c16
TT
25100 /* We don't recognize any vendor extensions. */
25101 break;
25102 }
25103 /* FALLTHROUGH */
25104
25105 default:
25106 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25107 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25108 section);
25109 if (mac_ptr == NULL)
25110 return;
25111 break;
2e276125 25112 }
132448f8 25113 DIAGNOSTIC_POP
757a13d0 25114 } while (macinfo_type != 0);
2e276125 25115}
8e19ed76 25116
cf2c3c16 25117static void
09262596 25118dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 25119 int section_is_gnu)
cf2c3c16 25120{
518817b3
SM
25121 struct dwarf2_per_objfile *dwarf2_per_objfile
25122 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25123 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
25124 struct line_header *lh = cu->line_header;
25125 bfd *abfd;
d521ce57 25126 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
25127 struct macro_source_file *current_file = 0;
25128 enum dwarf_macro_record_type macinfo_type;
25129 unsigned int offset_size = cu->header.offset_size;
d521ce57 25130 const gdb_byte *opcode_definitions[256];
8fc3fc34 25131 void **slot;
09262596
DE
25132 struct dwarf2_section_info *section;
25133 const char *section_name;
25134
25135 if (cu->dwo_unit != NULL)
25136 {
25137 if (section_is_gnu)
25138 {
25139 section = &cu->dwo_unit->dwo_file->sections.macro;
25140 section_name = ".debug_macro.dwo";
25141 }
25142 else
25143 {
25144 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25145 section_name = ".debug_macinfo.dwo";
25146 }
25147 }
25148 else
25149 {
25150 if (section_is_gnu)
25151 {
25152 section = &dwarf2_per_objfile->macro;
25153 section_name = ".debug_macro";
25154 }
25155 else
25156 {
25157 section = &dwarf2_per_objfile->macinfo;
25158 section_name = ".debug_macinfo";
25159 }
25160 }
cf2c3c16 25161
bb5ed363 25162 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25163 if (section->buffer == NULL)
25164 {
b98664d3 25165 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
25166 return;
25167 }
a32a8923 25168 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25169
25170 /* First pass: Find the name of the base filename.
25171 This filename is needed in order to process all macros whose definition
25172 (or undefinition) comes from the command line. These macros are defined
25173 before the first DW_MACINFO_start_file entry, and yet still need to be
25174 associated to the base file.
25175
25176 To determine the base file name, we scan the macro definitions until we
25177 reach the first DW_MACINFO_start_file entry. We then initialize
25178 CURRENT_FILE accordingly so that any macro definition found before the
25179 first DW_MACINFO_start_file can still be associated to the base file. */
25180
25181 mac_ptr = section->buffer + offset;
25182 mac_end = section->buffer + section->size;
25183
25184 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25185 &offset_size, section_is_gnu);
25186 if (mac_ptr == NULL)
25187 {
25188 /* We already issued a complaint. */
25189 return;
25190 }
25191
25192 do
25193 {
25194 /* Do we at least have room for a macinfo type byte? */
25195 if (mac_ptr >= mac_end)
25196 {
25197 /* Complaint is printed during the second pass as GDB will probably
25198 stop the first pass earlier upon finding
25199 DW_MACINFO_start_file. */
25200 break;
25201 }
25202
aead7601 25203 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25204 mac_ptr++;
25205
25206 /* Note that we rely on the fact that the corresponding GNU and
25207 DWARF constants are the same. */
132448f8
SM
25208 DIAGNOSTIC_PUSH
25209 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25210 switch (macinfo_type)
25211 {
25212 /* A zero macinfo type indicates the end of the macro
25213 information. */
25214 case 0:
25215 break;
25216
0af92d60
JK
25217 case DW_MACRO_define:
25218 case DW_MACRO_undef:
cf2c3c16
TT
25219 /* Only skip the data by MAC_PTR. */
25220 {
25221 unsigned int bytes_read;
25222
25223 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25224 mac_ptr += bytes_read;
25225 read_direct_string (abfd, mac_ptr, &bytes_read);
25226 mac_ptr += bytes_read;
25227 }
25228 break;
25229
0af92d60 25230 case DW_MACRO_start_file:
cf2c3c16
TT
25231 {
25232 unsigned int bytes_read;
25233 int line, file;
25234
25235 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25236 mac_ptr += bytes_read;
25237 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25238 mac_ptr += bytes_read;
25239
804d2729 25240 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25241 }
25242 break;
25243
0af92d60 25244 case DW_MACRO_end_file:
cf2c3c16
TT
25245 /* No data to skip by MAC_PTR. */
25246 break;
25247
0af92d60
JK
25248 case DW_MACRO_define_strp:
25249 case DW_MACRO_undef_strp:
25250 case DW_MACRO_define_sup:
25251 case DW_MACRO_undef_sup:
cf2c3c16
TT
25252 {
25253 unsigned int bytes_read;
25254
25255 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25256 mac_ptr += bytes_read;
25257 mac_ptr += offset_size;
25258 }
25259 break;
25260
0af92d60
JK
25261 case DW_MACRO_import:
25262 case DW_MACRO_import_sup:
cf2c3c16 25263 /* Note that, according to the spec, a transparent include
0af92d60 25264 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25265 skip this opcode. */
25266 mac_ptr += offset_size;
25267 break;
25268
25269 case DW_MACINFO_vendor_ext:
25270 /* Only skip the data by MAC_PTR. */
25271 if (!section_is_gnu)
25272 {
25273 unsigned int bytes_read;
25274
25275 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25276 mac_ptr += bytes_read;
25277 read_direct_string (abfd, mac_ptr, &bytes_read);
25278 mac_ptr += bytes_read;
25279 }
25280 /* FALLTHROUGH */
25281
25282 default:
25283 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25284 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25285 section);
25286 if (mac_ptr == NULL)
25287 return;
25288 break;
25289 }
132448f8 25290 DIAGNOSTIC_POP
cf2c3c16
TT
25291 } while (macinfo_type != 0 && current_file == NULL);
25292
25293 /* Second pass: Process all entries.
25294
25295 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25296 command-line macro definitions/undefinitions. This flag is unset when we
25297 reach the first DW_MACINFO_start_file entry. */
25298
fc4007c9
TT
25299 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25300 htab_eq_pointer,
25301 NULL, xcalloc, xfree));
8fc3fc34 25302 mac_ptr = section->buffer + offset;
fc4007c9 25303 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25304 *slot = (void *) mac_ptr;
804d2729 25305 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25306 current_file, lh, section,
fc4007c9
TT
25307 section_is_gnu, 0, offset_size,
25308 include_hash.get ());
cf2c3c16
TT
25309}
25310
8e19ed76 25311/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25312 if so return true else false. */
380bca97 25313
8e19ed76 25314static int
6e5a29e1 25315attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25316{
25317 return (attr == NULL ? 0 :
25318 attr->form == DW_FORM_block1
25319 || attr->form == DW_FORM_block2
25320 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25321 || attr->form == DW_FORM_block
25322 || attr->form == DW_FORM_exprloc);
8e19ed76 25323}
4c2df51b 25324
c6a0999f
JB
25325/* Return non-zero if ATTR's value is a section offset --- classes
25326 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25327 You may use DW_UNSND (attr) to retrieve such offsets.
25328
25329 Section 7.5.4, "Attribute Encodings", explains that no attribute
25330 may have a value that belongs to more than one of these classes; it
25331 would be ambiguous if we did, because we use the same forms for all
25332 of them. */
380bca97 25333
3690dd37 25334static int
6e5a29e1 25335attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25336{
25337 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25338 || attr->form == DW_FORM_data8
25339 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25340}
25341
3690dd37
JB
25342/* Return non-zero if ATTR's value falls in the 'constant' class, or
25343 zero otherwise. When this function returns true, you can apply
25344 dwarf2_get_attr_constant_value to it.
25345
25346 However, note that for some attributes you must check
25347 attr_form_is_section_offset before using this test. DW_FORM_data4
25348 and DW_FORM_data8 are members of both the constant class, and of
25349 the classes that contain offsets into other debug sections
25350 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25351 that, if an attribute's can be either a constant or one of the
25352 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25353 taken as section offsets, not constants.
25354
25355 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25356 cannot handle that. */
380bca97 25357
3690dd37 25358static int
6e5a29e1 25359attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25360{
25361 switch (attr->form)
25362 {
25363 case DW_FORM_sdata:
25364 case DW_FORM_udata:
25365 case DW_FORM_data1:
25366 case DW_FORM_data2:
25367 case DW_FORM_data4:
25368 case DW_FORM_data8:
663c44ac 25369 case DW_FORM_implicit_const:
3690dd37
JB
25370 return 1;
25371 default:
25372 return 0;
25373 }
25374}
25375
7771576e
SA
25376
25377/* DW_ADDR is always stored already as sect_offset; despite for the forms
25378 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25379
25380static int
6e5a29e1 25381attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25382{
25383 switch (attr->form)
25384 {
25385 case DW_FORM_ref_addr:
25386 case DW_FORM_ref1:
25387 case DW_FORM_ref2:
25388 case DW_FORM_ref4:
25389 case DW_FORM_ref8:
25390 case DW_FORM_ref_udata:
25391 case DW_FORM_GNU_ref_alt:
25392 return 1;
25393 default:
25394 return 0;
25395 }
25396}
25397
3019eac3
DE
25398/* Return the .debug_loc section to use for CU.
25399 For DWO files use .debug_loc.dwo. */
25400
25401static struct dwarf2_section_info *
25402cu_debug_loc_section (struct dwarf2_cu *cu)
25403{
518817b3
SM
25404 struct dwarf2_per_objfile *dwarf2_per_objfile
25405 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25406
3019eac3 25407 if (cu->dwo_unit)
43988095
JK
25408 {
25409 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25410
43988095
JK
25411 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25412 }
25413 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25414 : &dwarf2_per_objfile->loc);
3019eac3
DE
25415}
25416
8cf6f0b1
TT
25417/* A helper function that fills in a dwarf2_loclist_baton. */
25418
25419static void
25420fill_in_loclist_baton (struct dwarf2_cu *cu,
25421 struct dwarf2_loclist_baton *baton,
ff39bb5e 25422 const struct attribute *attr)
8cf6f0b1 25423{
518817b3
SM
25424 struct dwarf2_per_objfile *dwarf2_per_objfile
25425 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25426 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25427
25428 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25429
25430 baton->per_cu = cu->per_cu;
25431 gdb_assert (baton->per_cu);
25432 /* We don't know how long the location list is, but make sure we
25433 don't run off the edge of the section. */
3019eac3
DE
25434 baton->size = section->size - DW_UNSND (attr);
25435 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25436 baton->base_address = cu->base_address;
f664829e 25437 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25438}
25439
4c2df51b 25440static void
ff39bb5e 25441dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25442 struct dwarf2_cu *cu, int is_block)
4c2df51b 25443{
518817b3
SM
25444 struct dwarf2_per_objfile *dwarf2_per_objfile
25445 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25446 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25447 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25448
3690dd37 25449 if (attr_form_is_section_offset (attr)
3019eac3 25450 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25451 the section. If so, fall through to the complaint in the
25452 other branch. */
3019eac3 25453 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25454 {
0d53c4c4 25455 struct dwarf2_loclist_baton *baton;
4c2df51b 25456
8d749320 25457 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25458
8cf6f0b1 25459 fill_in_loclist_baton (cu, baton, attr);
be391dca 25460
d00adf39 25461 if (cu->base_known == 0)
b98664d3 25462 complaint (_("Location list used without "
3e43a32a 25463 "specifying the CU base address."));
4c2df51b 25464
f1e6e072
TT
25465 SYMBOL_ACLASS_INDEX (sym) = (is_block
25466 ? dwarf2_loclist_block_index
25467 : dwarf2_loclist_index);
0d53c4c4
DJ
25468 SYMBOL_LOCATION_BATON (sym) = baton;
25469 }
25470 else
25471 {
25472 struct dwarf2_locexpr_baton *baton;
25473
8d749320 25474 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25475 baton->per_cu = cu->per_cu;
25476 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25477
25478 if (attr_form_is_block (attr))
25479 {
25480 /* Note that we're just copying the block's data pointer
25481 here, not the actual data. We're still pointing into the
6502dd73
DJ
25482 info_buffer for SYM's objfile; right now we never release
25483 that buffer, but when we do clean up properly this may
25484 need to change. */
0d53c4c4
DJ
25485 baton->size = DW_BLOCK (attr)->size;
25486 baton->data = DW_BLOCK (attr)->data;
25487 }
25488 else
25489 {
25490 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 25491 sym->natural_name ());
0d53c4c4 25492 baton->size = 0;
0d53c4c4 25493 }
6e70227d 25494
f1e6e072
TT
25495 SYMBOL_ACLASS_INDEX (sym) = (is_block
25496 ? dwarf2_locexpr_block_index
25497 : dwarf2_locexpr_index);
0d53c4c4
DJ
25498 SYMBOL_LOCATION_BATON (sym) = baton;
25499 }
4c2df51b 25500}
6502dd73 25501
9aa1f1e3
TT
25502/* Return the OBJFILE associated with the compilation unit CU. If CU
25503 came from a separate debuginfo file, then the master objfile is
25504 returned. */
ae0d2f24
UW
25505
25506struct objfile *
25507dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25508{
e3b94546 25509 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25510
25511 /* Return the master objfile, so that we can report and look up the
25512 correct file containing this variable. */
25513 if (objfile->separate_debug_objfile_backlink)
25514 objfile = objfile->separate_debug_objfile_backlink;
25515
25516 return objfile;
25517}
25518
96408a79
SA
25519/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25520 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25521 CU_HEADERP first. */
25522
25523static const struct comp_unit_head *
25524per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25525 struct dwarf2_per_cu_data *per_cu)
25526{
d521ce57 25527 const gdb_byte *info_ptr;
96408a79
SA
25528
25529 if (per_cu->cu)
25530 return &per_cu->cu->header;
25531
9c541725 25532 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25533
25534 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25535 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25536 rcuh_kind::COMPILE);
96408a79
SA
25537
25538 return cu_headerp;
25539}
25540
ae0d2f24
UW
25541/* Return the address size given in the compilation unit header for CU. */
25542
98714339 25543int
ae0d2f24
UW
25544dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25545{
96408a79
SA
25546 struct comp_unit_head cu_header_local;
25547 const struct comp_unit_head *cu_headerp;
c471e790 25548
96408a79
SA
25549 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25550
25551 return cu_headerp->addr_size;
ae0d2f24
UW
25552}
25553
9eae7c52
TT
25554/* Return the offset size given in the compilation unit header for CU. */
25555
25556int
25557dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25558{
96408a79
SA
25559 struct comp_unit_head cu_header_local;
25560 const struct comp_unit_head *cu_headerp;
9c6c53f7 25561
96408a79
SA
25562 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25563
25564 return cu_headerp->offset_size;
25565}
25566
25567/* See its dwarf2loc.h declaration. */
25568
25569int
25570dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25571{
25572 struct comp_unit_head cu_header_local;
25573 const struct comp_unit_head *cu_headerp;
25574
25575 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25576
25577 if (cu_headerp->version == 2)
25578 return cu_headerp->addr_size;
25579 else
25580 return cu_headerp->offset_size;
181cebd4
JK
25581}
25582
9aa1f1e3
TT
25583/* Return the text offset of the CU. The returned offset comes from
25584 this CU's objfile. If this objfile came from a separate debuginfo
25585 file, then the offset may be different from the corresponding
25586 offset in the parent objfile. */
25587
25588CORE_ADDR
25589dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25590{
e3b94546 25591 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25592
25593 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25594}
25595
9a49df9d
AB
25596/* Return a type that is a generic pointer type, the size of which matches
25597 the address size given in the compilation unit header for PER_CU. */
25598static struct type *
25599dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25600{
25601 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25602 struct type *void_type = objfile_type (objfile)->builtin_void;
25603 struct type *addr_type = lookup_pointer_type (void_type);
25604 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25605
25606 if (TYPE_LENGTH (addr_type) == addr_size)
25607 return addr_type;
25608
25609 addr_type
25610 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25611 return addr_type;
25612}
25613
43988095
JK
25614/* Return DWARF version number of PER_CU. */
25615
25616short
25617dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25618{
25619 return per_cu->dwarf_version;
25620}
25621
348e048f
DE
25622/* Locate the .debug_info compilation unit from CU's objfile which contains
25623 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25624
25625static struct dwarf2_per_cu_data *
9c541725 25626dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25627 unsigned int offset_in_dwz,
ed2dc618 25628 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25629{
25630 struct dwarf2_per_cu_data *this_cu;
25631 int low, high;
25632
ae038cb0 25633 low = 0;
b76e467d 25634 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25635 while (high > low)
25636 {
36586728 25637 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25638 int mid = low + (high - low) / 2;
9a619af0 25639
36586728 25640 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25641 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25642 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25643 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25644 high = mid;
25645 else
25646 low = mid + 1;
25647 }
25648 gdb_assert (low == high);
36586728 25649 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25650 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25651 {
36586728 25652 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25653 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25654 "offset %s [in module %s]"),
25655 sect_offset_str (sect_off),
ed2dc618 25656 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25657
9c541725
PA
25658 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25659 <= sect_off);
ae038cb0
DJ
25660 return dwarf2_per_objfile->all_comp_units[low-1];
25661 }
25662 else
25663 {
b76e467d 25664 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25665 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25666 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25667 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25668 return this_cu;
25669 }
25670}
25671
23745b47 25672/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25673
fcd3b13d
SM
25674dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25675 : per_cu (per_cu_),
9068261f
AB
25676 mark (false),
25677 has_loclist (false),
25678 checked_producer (false),
25679 producer_is_gxx_lt_4_6 (false),
25680 producer_is_gcc_lt_4_3 (false),
eb77c9df 25681 producer_is_icc (false),
9068261f 25682 producer_is_icc_lt_14 (false),
c258c396 25683 producer_is_codewarrior (false),
9068261f 25684 processing_has_namespace_info (false)
93311388 25685{
fcd3b13d
SM
25686 per_cu->cu = this;
25687}
25688
25689/* Destroy a dwarf2_cu. */
25690
25691dwarf2_cu::~dwarf2_cu ()
25692{
25693 per_cu->cu = NULL;
9816fde3
JK
25694}
25695
25696/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25697
25698static void
95554aad
TT
25699prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25700 enum language pretend_language)
9816fde3
JK
25701{
25702 struct attribute *attr;
25703
25704 /* Set the language we're debugging. */
25705 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25706 if (attr != nullptr)
9816fde3
JK
25707 set_cu_language (DW_UNSND (attr), cu);
25708 else
9cded63f 25709 {
95554aad 25710 cu->language = pretend_language;
9cded63f
TT
25711 cu->language_defn = language_def (cu->language);
25712 }
dee91e82 25713
7d45c7c3 25714 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25715}
25716
ae038cb0
DJ
25717/* Increase the age counter on each cached compilation unit, and free
25718 any that are too old. */
25719
25720static void
ed2dc618 25721age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25722{
25723 struct dwarf2_per_cu_data *per_cu, **last_chain;
25724
25725 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25726 per_cu = dwarf2_per_objfile->read_in_chain;
25727 while (per_cu != NULL)
25728 {
25729 per_cu->cu->last_used ++;
b4f54984 25730 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25731 dwarf2_mark (per_cu->cu);
25732 per_cu = per_cu->cu->read_in_chain;
25733 }
25734
25735 per_cu = dwarf2_per_objfile->read_in_chain;
25736 last_chain = &dwarf2_per_objfile->read_in_chain;
25737 while (per_cu != NULL)
25738 {
25739 struct dwarf2_per_cu_data *next_cu;
25740
25741 next_cu = per_cu->cu->read_in_chain;
25742
25743 if (!per_cu->cu->mark)
25744 {
fcd3b13d 25745 delete per_cu->cu;
ae038cb0
DJ
25746 *last_chain = next_cu;
25747 }
25748 else
25749 last_chain = &per_cu->cu->read_in_chain;
25750
25751 per_cu = next_cu;
25752 }
25753}
25754
25755/* Remove a single compilation unit from the cache. */
25756
25757static void
dee91e82 25758free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25759{
25760 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25761 struct dwarf2_per_objfile *dwarf2_per_objfile
25762 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25763
25764 per_cu = dwarf2_per_objfile->read_in_chain;
25765 last_chain = &dwarf2_per_objfile->read_in_chain;
25766 while (per_cu != NULL)
25767 {
25768 struct dwarf2_per_cu_data *next_cu;
25769
25770 next_cu = per_cu->cu->read_in_chain;
25771
dee91e82 25772 if (per_cu == target_per_cu)
ae038cb0 25773 {
fcd3b13d 25774 delete per_cu->cu;
dee91e82 25775 per_cu->cu = NULL;
ae038cb0
DJ
25776 *last_chain = next_cu;
25777 break;
25778 }
25779 else
25780 last_chain = &per_cu->cu->read_in_chain;
25781
25782 per_cu = next_cu;
25783 }
25784}
25785
dee91e82
DE
25786/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25787 We store these in a hash table separate from the DIEs, and preserve them
25788 when the DIEs are flushed out of cache.
25789
25790 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25791 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25792 or the type may come from a DWO file. Furthermore, while it's more logical
25793 to use per_cu->section+offset, with Fission the section with the data is in
25794 the DWO file but we don't know that section at the point we need it.
25795 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25796 because we can enter the lookup routine, get_die_type_at_offset, from
25797 outside this file, and thus won't necessarily have PER_CU->cu.
25798 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25799
dee91e82 25800struct dwarf2_per_cu_offset_and_type
1c379e20 25801{
dee91e82 25802 const struct dwarf2_per_cu_data *per_cu;
9c541725 25803 sect_offset sect_off;
1c379e20
DJ
25804 struct type *type;
25805};
25806
dee91e82 25807/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25808
25809static hashval_t
dee91e82 25810per_cu_offset_and_type_hash (const void *item)
1c379e20 25811{
9a3c8263
SM
25812 const struct dwarf2_per_cu_offset_and_type *ofs
25813 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25814
9c541725 25815 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25816}
25817
dee91e82 25818/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25819
25820static int
dee91e82 25821per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25822{
9a3c8263
SM
25823 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25824 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25825 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25826 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25827
dee91e82 25828 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25829 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25830}
25831
25832/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25833 table if necessary. For convenience, return TYPE.
25834
25835 The DIEs reading must have careful ordering to:
85102364 25836 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25837 reading current DIE.
25838 * Not trying to dereference contents of still incompletely read in types
25839 while reading in other DIEs.
25840 * Enable referencing still incompletely read in types just by a pointer to
25841 the type without accessing its fields.
25842
25843 Therefore caller should follow these rules:
25844 * Try to fetch any prerequisite types we may need to build this DIE type
25845 before building the type and calling set_die_type.
e71ec853 25846 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25847 possible before fetching more types to complete the current type.
25848 * Make the type as complete as possible before fetching more types. */
1c379e20 25849
f792889a 25850static struct type *
1c379e20
DJ
25851set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25852{
518817b3
SM
25853 struct dwarf2_per_objfile *dwarf2_per_objfile
25854 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25855 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25856 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25857 struct attribute *attr;
25858 struct dynamic_prop prop;
1c379e20 25859
b4ba55a1
JB
25860 /* For Ada types, make sure that the gnat-specific data is always
25861 initialized (if not already set). There are a few types where
25862 we should not be doing so, because the type-specific area is
25863 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25864 where the type-specific area is used to store the floatformat).
25865 But this is not a problem, because the gnat-specific information
25866 is actually not needed for these types. */
25867 if (need_gnat_info (cu)
25868 && TYPE_CODE (type) != TYPE_CODE_FUNC
25869 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25870 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25871 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25872 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25873 && !HAVE_GNAT_AUX_INFO (type))
25874 INIT_GNAT_SPECIFIC (type);
25875
3f2f83dd
KB
25876 /* Read DW_AT_allocated and set in type. */
25877 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25878 if (attr_form_is_block (attr))
25879 {
9a49df9d
AB
25880 struct type *prop_type
25881 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25882 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25883 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25884 }
25885 else if (attr != NULL)
25886 {
b98664d3 25887 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25888 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25889 sect_offset_str (die->sect_off));
3f2f83dd
KB
25890 }
25891
25892 /* Read DW_AT_associated and set in type. */
25893 attr = dwarf2_attr (die, DW_AT_associated, cu);
25894 if (attr_form_is_block (attr))
25895 {
9a49df9d
AB
25896 struct type *prop_type
25897 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25898 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25899 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25900 }
25901 else if (attr != NULL)
25902 {
b98664d3 25903 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25904 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25905 sect_offset_str (die->sect_off));
3f2f83dd
KB
25906 }
25907
3cdcd0ce
JB
25908 /* Read DW_AT_data_location and set in type. */
25909 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25910 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25911 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25912 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25913
dee91e82 25914 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25915 {
dee91e82
DE
25916 dwarf2_per_objfile->die_type_hash =
25917 htab_create_alloc_ex (127,
25918 per_cu_offset_and_type_hash,
25919 per_cu_offset_and_type_eq,
25920 NULL,
25921 &objfile->objfile_obstack,
25922 hashtab_obstack_allocate,
25923 dummy_obstack_deallocate);
f792889a 25924 }
1c379e20 25925
dee91e82 25926 ofs.per_cu = cu->per_cu;
9c541725 25927 ofs.sect_off = die->sect_off;
1c379e20 25928 ofs.type = type;
dee91e82
DE
25929 slot = (struct dwarf2_per_cu_offset_and_type **)
25930 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25931 if (*slot)
b98664d3 25932 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25933 sect_offset_str (die->sect_off));
8d749320
SM
25934 *slot = XOBNEW (&objfile->objfile_obstack,
25935 struct dwarf2_per_cu_offset_and_type);
1c379e20 25936 **slot = ofs;
f792889a 25937 return type;
1c379e20
DJ
25938}
25939
9c541725 25940/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25941 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25942
25943static struct type *
9c541725 25944get_die_type_at_offset (sect_offset sect_off,
673bfd45 25945 struct dwarf2_per_cu_data *per_cu)
1c379e20 25946{
dee91e82 25947 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25948 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25949
dee91e82 25950 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25951 return NULL;
1c379e20 25952
dee91e82 25953 ofs.per_cu = per_cu;
9c541725 25954 ofs.sect_off = sect_off;
9a3c8263
SM
25955 slot = ((struct dwarf2_per_cu_offset_and_type *)
25956 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25957 if (slot)
25958 return slot->type;
25959 else
25960 return NULL;
25961}
25962
02142a6c 25963/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25964 or return NULL if DIE does not have a saved type. */
25965
25966static struct type *
25967get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25968{
9c541725 25969 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25970}
25971
10b3939b
DJ
25972/* Add a dependence relationship from CU to REF_PER_CU. */
25973
25974static void
25975dwarf2_add_dependence (struct dwarf2_cu *cu,
25976 struct dwarf2_per_cu_data *ref_per_cu)
25977{
25978 void **slot;
25979
25980 if (cu->dependencies == NULL)
25981 cu->dependencies
25982 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25983 NULL, &cu->comp_unit_obstack,
25984 hashtab_obstack_allocate,
25985 dummy_obstack_deallocate);
25986
25987 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25988 if (*slot == NULL)
25989 *slot = ref_per_cu;
25990}
1c379e20 25991
f504f079
DE
25992/* Subroutine of dwarf2_mark to pass to htab_traverse.
25993 Set the mark field in every compilation unit in the
ae038cb0
DJ
25994 cache that we must keep because we are keeping CU. */
25995
10b3939b
DJ
25996static int
25997dwarf2_mark_helper (void **slot, void *data)
25998{
25999 struct dwarf2_per_cu_data *per_cu;
26000
26001 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
26002
26003 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26004 reading of the chain. As such dependencies remain valid it is not much
26005 useful to track and undo them during QUIT cleanups. */
26006 if (per_cu->cu == NULL)
26007 return 1;
26008
10b3939b
DJ
26009 if (per_cu->cu->mark)
26010 return 1;
9068261f 26011 per_cu->cu->mark = true;
10b3939b
DJ
26012
26013 if (per_cu->cu->dependencies != NULL)
26014 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26015
26016 return 1;
26017}
26018
f504f079
DE
26019/* Set the mark field in CU and in every other compilation unit in the
26020 cache that we must keep because we are keeping CU. */
26021
ae038cb0
DJ
26022static void
26023dwarf2_mark (struct dwarf2_cu *cu)
26024{
26025 if (cu->mark)
26026 return;
9068261f 26027 cu->mark = true;
10b3939b
DJ
26028 if (cu->dependencies != NULL)
26029 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
26030}
26031
26032static void
26033dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26034{
26035 while (per_cu)
26036 {
9068261f 26037 per_cu->cu->mark = false;
ae038cb0
DJ
26038 per_cu = per_cu->cu->read_in_chain;
26039 }
72bf9492
DJ
26040}
26041
72bf9492
DJ
26042/* Trivial hash function for partial_die_info: the hash value of a DIE
26043 is its offset in .debug_info for this objfile. */
26044
26045static hashval_t
26046partial_die_hash (const void *item)
26047{
9a3c8263
SM
26048 const struct partial_die_info *part_die
26049 = (const struct partial_die_info *) item;
9a619af0 26050
9c541725 26051 return to_underlying (part_die->sect_off);
72bf9492
DJ
26052}
26053
26054/* Trivial comparison function for partial_die_info structures: two DIEs
26055 are equal if they have the same offset. */
26056
26057static int
26058partial_die_eq (const void *item_lhs, const void *item_rhs)
26059{
9a3c8263
SM
26060 const struct partial_die_info *part_die_lhs
26061 = (const struct partial_die_info *) item_lhs;
26062 const struct partial_die_info *part_die_rhs
26063 = (const struct partial_die_info *) item_rhs;
9a619af0 26064
9c541725 26065 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
26066}
26067
3c3bb058
AB
26068struct cmd_list_element *set_dwarf_cmdlist;
26069struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
26070
26071static void
981a3fb3 26072set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 26073{
b4f54984 26074 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 26075 gdb_stdout);
ae038cb0
DJ
26076}
26077
26078static void
981a3fb3 26079show_dwarf_cmd (const char *args, int from_tty)
6e70227d 26080{
b4f54984 26081 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
26082}
26083
491144b5 26084bool dwarf_always_disassemble;
437afbb8 26085
437afbb8 26086static void
cd4fb1b2
SM
26087show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26088 struct cmd_list_element *c, const char *value)
9291a0cd 26089{
cd4fb1b2
SM
26090 fprintf_filtered (file,
26091 _("Whether to always disassemble "
26092 "DWARF expressions is %s.\n"),
26093 value);
9291a0cd
TT
26094}
26095
9291a0cd 26096static void
cd4fb1b2
SM
26097show_check_physname (struct ui_file *file, int from_tty,
26098 struct cmd_list_element *c, const char *value)
9291a0cd 26099{
cd4fb1b2
SM
26100 fprintf_filtered (file,
26101 _("Whether to check \"physname\" is %s.\n"),
26102 value);
9291a0cd
TT
26103}
26104
cd4fb1b2
SM
26105void
26106_initialize_dwarf2_read (void)
9291a0cd 26107{
cd4fb1b2
SM
26108 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26109Set DWARF specific variables.\n\
590042fc 26110Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
26111 &set_dwarf_cmdlist, "maintenance set dwarf ",
26112 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 26113
cd4fb1b2 26114 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
26115Show DWARF specific variables.\n\
26116Show DWARF variables such as the cache size."),
cd4fb1b2
SM
26117 &show_dwarf_cmdlist, "maintenance show dwarf ",
26118 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 26119
cd4fb1b2
SM
26120 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26121 &dwarf_max_cache_age, _("\
26122Set the upper bound on the age of cached DWARF compilation units."), _("\
26123Show the upper bound on the age of cached DWARF compilation units."), _("\
26124A higher limit means that cached compilation units will be stored\n\
26125in memory longer, and more total memory will be used. Zero disables\n\
26126caching, which can slow down startup."),
26127 NULL,
26128 show_dwarf_max_cache_age,
26129 &set_dwarf_cmdlist,
26130 &show_dwarf_cmdlist);
156942c7 26131
cd4fb1b2
SM
26132 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26133 &dwarf_always_disassemble, _("\
26134Set whether `info address' always disassembles DWARF expressions."), _("\
26135Show whether `info address' always disassembles DWARF expressions."), _("\
26136When enabled, DWARF expressions are always printed in an assembly-like\n\
26137syntax. When disabled, expressions will be printed in a more\n\
26138conversational style, when possible."),
26139 NULL,
26140 show_dwarf_always_disassemble,
26141 &set_dwarf_cmdlist,
26142 &show_dwarf_cmdlist);
9291a0cd 26143
cd4fb1b2
SM
26144 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26145Set debugging of the DWARF reader."), _("\
26146Show debugging of the DWARF reader."), _("\
26147When enabled (non-zero), debugging messages are printed during DWARF\n\
26148reading and symtab expansion. A value of 1 (one) provides basic\n\
26149information. A value greater than 1 provides more verbose information."),
26150 NULL,
26151 NULL,
26152 &setdebuglist, &showdebuglist);
9291a0cd 26153
cd4fb1b2
SM
26154 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26155Set debugging of the DWARF DIE reader."), _("\
26156Show debugging of the DWARF DIE reader."), _("\
26157When enabled (non-zero), DIEs are dumped after they are read in.\n\
26158The value is the maximum depth to print."),
26159 NULL,
26160 NULL,
26161 &setdebuglist, &showdebuglist);
9291a0cd 26162
cd4fb1b2
SM
26163 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26164Set debugging of the dwarf line reader."), _("\
26165Show debugging of the dwarf line reader."), _("\
26166When enabled (non-zero), line number entries are dumped as they are read in.\n\
26167A value of 1 (one) provides basic information.\n\
26168A value greater than 1 provides more verbose information."),
26169 NULL,
26170 NULL,
26171 &setdebuglist, &showdebuglist);
437afbb8 26172
cd4fb1b2
SM
26173 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26174Set cross-checking of \"physname\" code against demangler."), _("\
26175Show cross-checking of \"physname\" code against demangler."), _("\
26176When enabled, GDB's internal \"physname\" code is checked against\n\
26177the demangler."),
26178 NULL, show_check_physname,
26179 &setdebuglist, &showdebuglist);
900e11f9 26180
e615022a
DE
26181 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26182 no_class, &use_deprecated_index_sections, _("\
26183Set whether to use deprecated gdb_index sections."), _("\
26184Show whether to use deprecated gdb_index sections."), _("\
26185When enabled, deprecated .gdb_index sections are used anyway.\n\
26186Normally they are ignored either because of a missing feature or\n\
26187performance issue.\n\
26188Warning: This option must be enabled before gdb reads the file."),
26189 NULL,
26190 NULL,
26191 &setlist, &showlist);
26192
f1e6e072
TT
26193 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26194 &dwarf2_locexpr_funcs);
26195 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26196 &dwarf2_loclist_funcs);
26197
26198 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26199 &dwarf2_block_frame_base_locexpr_funcs);
26200 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26201 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26202
26203#if GDB_SELF_TEST
26204 selftests::register_test ("dw2_expand_symtabs_matching",
26205 selftests::dw2_expand_symtabs_matching::run_test);
26206#endif
6502dd73 26207}
This page took 6.207138 seconds and 4 git commands to generate.