Treat inactive TUI specially in "info win"
[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);
1913
b303c6f6
AB
1914/* Class, the destructor of which frees all allocated queue entries. This
1915 will only have work to do if an error was thrown while processing the
1916 dwarf. If no error was thrown then the queue entries should have all
1917 been processed, and freed, as we went along. */
1918
1919class dwarf2_queue_guard
1920{
1921public:
1922 dwarf2_queue_guard () = default;
1923
1924 /* Free any entries remaining on the queue. There should only be
1925 entries left if we hit an error while processing the dwarf. */
1926 ~dwarf2_queue_guard ()
1927 {
1928 struct dwarf2_queue_item *item, *last;
1929
1930 item = dwarf2_queue;
1931 while (item)
1932 {
1933 /* Anything still marked queued is likely to be in an
1934 inconsistent state, so discard it. */
1935 if (item->per_cu->queued)
1936 {
1937 if (item->per_cu->cu != NULL)
1938 free_one_cached_comp_unit (item->per_cu);
1939 item->per_cu->queued = 0;
1940 }
1941
1942 last = item;
1943 item = item->next;
1944 xfree (last);
1945 }
1946
1947 dwarf2_queue = dwarf2_queue_tail = NULL;
1948 }
1949};
1950
d721ba37
PA
1951/* The return type of find_file_and_directory. Note, the enclosed
1952 string pointers are only valid while this object is valid. */
1953
1954struct file_and_directory
1955{
1956 /* The filename. This is never NULL. */
1957 const char *name;
1958
1959 /* The compilation directory. NULL if not known. If we needed to
1960 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1961 points directly to the DW_AT_comp_dir string attribute owned by
1962 the obstack that owns the DIE. */
1963 const char *comp_dir;
1964
1965 /* If we needed to build a new string for comp_dir, this is what
1966 owns the storage. */
1967 std::string comp_dir_storage;
1968};
1969
1970static file_and_directory find_file_and_directory (struct die_info *die,
1971 struct dwarf2_cu *cu);
9291a0cd
TT
1972
1973static char *file_full_name (int file, struct line_header *lh,
1974 const char *comp_dir);
1975
43988095
JK
1976/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1977enum class rcuh_kind { COMPILE, TYPE };
1978
d521ce57 1979static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1980 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1981 struct comp_unit_head *header,
36586728 1982 struct dwarf2_section_info *section,
d521ce57 1983 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1984 rcuh_kind section_kind);
36586728 1985
fd820528 1986static void init_cutu_and_read_dies
f4dc4d17 1987 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1988 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1989 die_reader_func_ftype *die_reader_func, void *data);
1990
dee91e82
DE
1991static void init_cutu_and_read_dies_simple
1992 (struct dwarf2_per_cu_data *this_cu,
1993 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1994
673bfd45 1995static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1996
3019eac3
DE
1997static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1998
57d63ce2 1999static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2000 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2001 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2002 ULONGEST signature, int is_debug_types);
a2ce51a0 2003
ed2dc618
SM
2004static struct dwp_file *get_dwp_file
2005 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2006
3019eac3 2007static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2008 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2009
2010static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2011 (struct signatured_type *, const char *, const char *);
3019eac3 2012
89e63ee4
DE
2013static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2014
263db9a1
TT
2015/* A unique pointer to a dwo_file. */
2016
51ac9db5 2017typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2018
ed2dc618 2019static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2020
1b80a9fa 2021static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2022
2023static void free_line_header_voidp (void *arg);
4390d890
DE
2024\f
2025/* Various complaints about symbol reading that don't abort the process. */
2026
2027static void
2028dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2029{
b98664d3 2030 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2031}
2032
2033static void
2034dwarf2_debug_line_missing_file_complaint (void)
2035{
b98664d3 2036 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2037}
2038
2039static void
2040dwarf2_debug_line_missing_end_sequence_complaint (void)
2041{
b98664d3 2042 complaint (_(".debug_line section has line "
4390d890
DE
2043 "program sequence without an end"));
2044}
2045
2046static void
2047dwarf2_complex_location_expr_complaint (void)
2048{
b98664d3 2049 complaint (_("location expression too complex"));
4390d890
DE
2050}
2051
2052static void
2053dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2054 int arg3)
2055{
b98664d3 2056 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2057 arg1, arg2, arg3);
2058}
2059
2060static void
2061dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2062{
b98664d3 2063 complaint (_("debug info runs off end of %s section"
4390d890 2064 " [in module %s]"),
a32a8923
DE
2065 get_section_name (section),
2066 get_section_file_name (section));
4390d890 2067}
1b80a9fa 2068
4390d890
DE
2069static void
2070dwarf2_macro_malformed_definition_complaint (const char *arg1)
2071{
b98664d3 2072 complaint (_("macro debug info contains a "
4390d890
DE
2073 "malformed macro definition:\n`%s'"),
2074 arg1);
2075}
2076
2077static void
2078dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2079{
b98664d3 2080 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2081 arg1, arg2);
2082}
527f3840
JK
2083
2084/* Hash function for line_header_hash. */
2085
2086static hashval_t
2087line_header_hash (const struct line_header *ofs)
2088{
9c541725 2089 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2090}
2091
2092/* Hash function for htab_create_alloc_ex for line_header_hash. */
2093
2094static hashval_t
2095line_header_hash_voidp (const void *item)
2096{
9a3c8263 2097 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2098
2099 return line_header_hash (ofs);
2100}
2101
2102/* Equality function for line_header_hash. */
2103
2104static int
2105line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2106{
9a3c8263
SM
2107 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2108 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2109
9c541725 2110 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2111 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2112}
2113
4390d890 2114\f
9291a0cd 2115
31aa7e4e
JB
2116/* Read the given attribute value as an address, taking the attribute's
2117 form into account. */
2118
2119static CORE_ADDR
2120attr_value_as_address (struct attribute *attr)
2121{
2122 CORE_ADDR addr;
2123
336d760d
AT
2124 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2125 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2126 {
2127 /* Aside from a few clearly defined exceptions, attributes that
2128 contain an address must always be in DW_FORM_addr form.
2129 Unfortunately, some compilers happen to be violating this
2130 requirement by encoding addresses using other forms, such
2131 as DW_FORM_data4 for example. For those broken compilers,
2132 we try to do our best, without any guarantee of success,
2133 to interpret the address correctly. It would also be nice
2134 to generate a complaint, but that would require us to maintain
2135 a list of legitimate cases where a non-address form is allowed,
2136 as well as update callers to pass in at least the CU's DWARF
2137 version. This is more overhead than what we're willing to
2138 expand for a pretty rare case. */
2139 addr = DW_UNSND (attr);
2140 }
2141 else
2142 addr = DW_ADDR (attr);
2143
2144 return addr;
2145}
2146
330cdd98
PA
2147/* See declaration. */
2148
2149dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2150 const dwarf2_debug_sections *names,
2151 bool can_copy_)
2152 : objfile (objfile_),
2153 can_copy (can_copy_)
330cdd98
PA
2154{
2155 if (names == NULL)
2156 names = &dwarf2_elf_names;
2157
2158 bfd *obfd = objfile->obfd;
2159
2160 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2161 locate_sections (obfd, sec, *names);
2162}
2163
2164dwarf2_per_objfile::~dwarf2_per_objfile ()
2165{
2166 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2167 free_cached_comp_units ();
2168
2169 if (quick_file_names_table)
2170 htab_delete (quick_file_names_table);
2171
2172 if (line_header_hash)
2173 htab_delete (line_header_hash);
2174
b76e467d 2175 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2176 per_cu->imported_symtabs_free ();
fc8e7e75 2177
b2bdb8cf 2178 for (signatured_type *sig_type : all_type_units)
ae640021 2179 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2180
330cdd98
PA
2181 /* Everything else should be on the objfile obstack. */
2182}
2183
2184/* See declaration. */
2185
2186void
2187dwarf2_per_objfile::free_cached_comp_units ()
2188{
2189 dwarf2_per_cu_data *per_cu = read_in_chain;
2190 dwarf2_per_cu_data **last_chain = &read_in_chain;
2191 while (per_cu != NULL)
2192 {
2193 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2194
fcd3b13d 2195 delete per_cu->cu;
330cdd98
PA
2196 *last_chain = next_cu;
2197 per_cu = next_cu;
2198 }
2199}
2200
11ed8cad
TT
2201/* A helper class that calls free_cached_comp_units on
2202 destruction. */
2203
2204class free_cached_comp_units
2205{
2206public:
2207
2208 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2209 : m_per_objfile (per_objfile)
2210 {
2211 }
2212
2213 ~free_cached_comp_units ()
2214 {
2215 m_per_objfile->free_cached_comp_units ();
2216 }
2217
2218 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2219
2220private:
2221
2222 dwarf2_per_objfile *m_per_objfile;
2223};
2224
c906108c 2225/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2226 information and return true if we have enough to do something.
2227 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2228 ELF names are used. CAN_COPY is true for formats where symbol
2229 interposition is possible and so symbol values must follow copy
2230 relocation rules. */
c906108c
SS
2231
2232int
251d32d9 2233dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2234 const struct dwarf2_debug_sections *names,
2235 bool can_copy)
c906108c 2236{
97cbe998
SDJ
2237 if (objfile->flags & OBJF_READNEVER)
2238 return 0;
2239
ed2dc618
SM
2240 struct dwarf2_per_objfile *dwarf2_per_objfile
2241 = get_dwarf2_per_objfile (objfile);
2242
2243 if (dwarf2_per_objfile == NULL)
5bfd760d 2244 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2245 names,
2246 can_copy);
5bfd760d 2247
73869dc2 2248 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2249 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2250 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2251 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2252}
2253
2254/* Return the containing section of virtual section SECTION. */
2255
2256static struct dwarf2_section_info *
2257get_containing_section (const struct dwarf2_section_info *section)
2258{
2259 gdb_assert (section->is_virtual);
2260 return section->s.containing_section;
c906108c
SS
2261}
2262
a32a8923
DE
2263/* Return the bfd owner of SECTION. */
2264
2265static struct bfd *
2266get_section_bfd_owner (const struct dwarf2_section_info *section)
2267{
73869dc2
DE
2268 if (section->is_virtual)
2269 {
2270 section = get_containing_section (section);
2271 gdb_assert (!section->is_virtual);
2272 }
049412e3 2273 return section->s.section->owner;
a32a8923
DE
2274}
2275
2276/* Return the bfd section of SECTION.
2277 Returns NULL if the section is not present. */
2278
2279static asection *
2280get_section_bfd_section (const struct dwarf2_section_info *section)
2281{
73869dc2
DE
2282 if (section->is_virtual)
2283 {
2284 section = get_containing_section (section);
2285 gdb_assert (!section->is_virtual);
2286 }
049412e3 2287 return section->s.section;
a32a8923
DE
2288}
2289
2290/* Return the name of SECTION. */
2291
2292static const char *
2293get_section_name (const struct dwarf2_section_info *section)
2294{
2295 asection *sectp = get_section_bfd_section (section);
2296
2297 gdb_assert (sectp != NULL);
fd361982 2298 return bfd_section_name (sectp);
a32a8923
DE
2299}
2300
2301/* Return the name of the file SECTION is in. */
2302
2303static const char *
2304get_section_file_name (const struct dwarf2_section_info *section)
2305{
2306 bfd *abfd = get_section_bfd_owner (section);
2307
2308 return bfd_get_filename (abfd);
2309}
2310
2311/* Return the id of SECTION.
2312 Returns 0 if SECTION doesn't exist. */
2313
2314static int
2315get_section_id (const struct dwarf2_section_info *section)
2316{
2317 asection *sectp = get_section_bfd_section (section);
2318
2319 if (sectp == NULL)
2320 return 0;
2321 return sectp->id;
2322}
2323
2324/* Return the flags of SECTION.
73869dc2 2325 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2326
2327static int
2328get_section_flags (const struct dwarf2_section_info *section)
2329{
2330 asection *sectp = get_section_bfd_section (section);
2331
2332 gdb_assert (sectp != NULL);
fd361982 2333 return bfd_section_flags (sectp);
a32a8923
DE
2334}
2335
251d32d9
TG
2336/* When loading sections, we look either for uncompressed section or for
2337 compressed section names. */
233a11ab
CS
2338
2339static int
251d32d9
TG
2340section_is_p (const char *section_name,
2341 const struct dwarf2_section_names *names)
233a11ab 2342{
251d32d9
TG
2343 if (names->normal != NULL
2344 && strcmp (section_name, names->normal) == 0)
2345 return 1;
2346 if (names->compressed != NULL
2347 && strcmp (section_name, names->compressed) == 0)
2348 return 1;
2349 return 0;
233a11ab
CS
2350}
2351
330cdd98 2352/* See declaration. */
c906108c 2353
330cdd98
PA
2354void
2355dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2356 const dwarf2_debug_sections &names)
c906108c 2357{
fd361982 2358 flagword aflag = bfd_section_flags (sectp);
251d32d9 2359
dc7650b8
JK
2360 if ((aflag & SEC_HAS_CONTENTS) == 0)
2361 {
2362 }
950b7495
KS
2363 else if (elf_section_data (sectp)->this_hdr.sh_size
2364 > bfd_get_file_size (abfd))
2365 {
2366 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2367 warning (_("Discarding section %s which has a section size (%s"
2368 ") larger than the file size [in module %s]"),
2369 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2370 bfd_get_filename (abfd));
2371 }
330cdd98 2372 else if (section_is_p (sectp->name, &names.info))
c906108c 2373 {
330cdd98 2374 this->info.s.section = sectp;
fd361982 2375 this->info.size = bfd_section_size (sectp);
c906108c 2376 }
330cdd98 2377 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2378 {
330cdd98 2379 this->abbrev.s.section = sectp;
fd361982 2380 this->abbrev.size = bfd_section_size (sectp);
c906108c 2381 }
330cdd98 2382 else if (section_is_p (sectp->name, &names.line))
c906108c 2383 {
330cdd98 2384 this->line.s.section = sectp;
fd361982 2385 this->line.size = bfd_section_size (sectp);
c906108c 2386 }
330cdd98 2387 else if (section_is_p (sectp->name, &names.loc))
c906108c 2388 {
330cdd98 2389 this->loc.s.section = sectp;
fd361982 2390 this->loc.size = bfd_section_size (sectp);
c906108c 2391 }
330cdd98 2392 else if (section_is_p (sectp->name, &names.loclists))
43988095 2393 {
330cdd98 2394 this->loclists.s.section = sectp;
fd361982 2395 this->loclists.size = bfd_section_size (sectp);
43988095 2396 }
330cdd98 2397 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2398 {
330cdd98 2399 this->macinfo.s.section = sectp;
fd361982 2400 this->macinfo.size = bfd_section_size (sectp);
c906108c 2401 }
330cdd98 2402 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2403 {
330cdd98 2404 this->macro.s.section = sectp;
fd361982 2405 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2406 }
330cdd98 2407 else if (section_is_p (sectp->name, &names.str))
c906108c 2408 {
330cdd98 2409 this->str.s.section = sectp;
fd361982 2410 this->str.size = bfd_section_size (sectp);
c906108c 2411 }
330cdd98 2412 else if (section_is_p (sectp->name, &names.line_str))
43988095 2413 {
330cdd98 2414 this->line_str.s.section = sectp;
fd361982 2415 this->line_str.size = bfd_section_size (sectp);
43988095 2416 }
330cdd98 2417 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2418 {
330cdd98 2419 this->addr.s.section = sectp;
fd361982 2420 this->addr.size = bfd_section_size (sectp);
3019eac3 2421 }
330cdd98 2422 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2423 {
330cdd98 2424 this->frame.s.section = sectp;
fd361982 2425 this->frame.size = bfd_section_size (sectp);
b6af0555 2426 }
330cdd98 2427 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2428 {
330cdd98 2429 this->eh_frame.s.section = sectp;
fd361982 2430 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2431 }
330cdd98 2432 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2433 {
330cdd98 2434 this->ranges.s.section = sectp;
fd361982 2435 this->ranges.size = bfd_section_size (sectp);
af34e669 2436 }
330cdd98 2437 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2438 {
330cdd98 2439 this->rnglists.s.section = sectp;
fd361982 2440 this->rnglists.size = bfd_section_size (sectp);
43988095 2441 }
330cdd98 2442 else if (section_is_p (sectp->name, &names.types))
348e048f 2443 {
8b70b953
TT
2444 struct dwarf2_section_info type_section;
2445
2446 memset (&type_section, 0, sizeof (type_section));
049412e3 2447 type_section.s.section = sectp;
fd361982 2448 type_section.size = bfd_section_size (sectp);
8b70b953 2449
fd5866f6 2450 this->types.push_back (type_section);
348e048f 2451 }
330cdd98 2452 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2453 {
330cdd98 2454 this->gdb_index.s.section = sectp;
fd361982 2455 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2456 }
927aa2e7
JK
2457 else if (section_is_p (sectp->name, &names.debug_names))
2458 {
2459 this->debug_names.s.section = sectp;
fd361982 2460 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2461 }
2462 else if (section_is_p (sectp->name, &names.debug_aranges))
2463 {
2464 this->debug_aranges.s.section = sectp;
fd361982 2465 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2466 }
dce234bc 2467
fd361982
AM
2468 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2469 && bfd_section_vma (sectp) == 0)
330cdd98 2470 this->has_section_at_zero = true;
c906108c
SS
2471}
2472
fceca515
DE
2473/* A helper function that decides whether a section is empty,
2474 or not present. */
9e0ac564
TT
2475
2476static int
19ac8c2e 2477dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2478{
73869dc2
DE
2479 if (section->is_virtual)
2480 return section->size == 0;
049412e3 2481 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2482}
2483
cd4fb1b2 2484/* See dwarf2read.h. */
c906108c 2485
cd4fb1b2
SM
2486void
2487dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2488{
a32a8923 2489 asection *sectp;
3019eac3 2490 bfd *abfd;
dce234bc 2491 gdb_byte *buf, *retbuf;
c906108c 2492
be391dca
TT
2493 if (info->readin)
2494 return;
dce234bc 2495 info->buffer = NULL;
dc4ccb6f 2496 info->readin = true;
188dd5d6 2497
9e0ac564 2498 if (dwarf2_section_empty_p (info))
dce234bc 2499 return;
c906108c 2500
a32a8923 2501 sectp = get_section_bfd_section (info);
3019eac3 2502
73869dc2
DE
2503 /* If this is a virtual section we need to read in the real one first. */
2504 if (info->is_virtual)
2505 {
2506 struct dwarf2_section_info *containing_section =
2507 get_containing_section (info);
2508
2509 gdb_assert (sectp != NULL);
2510 if ((sectp->flags & SEC_RELOC) != 0)
2511 {
2512 error (_("Dwarf Error: DWP format V2 with relocations is not"
2513 " supported in section %s [in module %s]"),
2514 get_section_name (info), get_section_file_name (info));
2515 }
2516 dwarf2_read_section (objfile, containing_section);
2517 /* Other code should have already caught virtual sections that don't
2518 fit. */
2519 gdb_assert (info->virtual_offset + info->size
2520 <= containing_section->size);
2521 /* If the real section is empty or there was a problem reading the
2522 section we shouldn't get here. */
2523 gdb_assert (containing_section->buffer != NULL);
2524 info->buffer = containing_section->buffer + info->virtual_offset;
2525 return;
2526 }
2527
4bf44c1c
TT
2528 /* If the section has relocations, we must read it ourselves.
2529 Otherwise we attach it to the BFD. */
2530 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2531 {
d521ce57 2532 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2533 return;
dce234bc 2534 }
dce234bc 2535
224c3ddb 2536 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2537 info->buffer = buf;
dce234bc
PP
2538
2539 /* When debugging .o files, we may need to apply relocations; see
2540 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2541 We never compress sections in .o files, so we only need to
2542 try this when the section is not compressed. */
ac8035ab 2543 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2544 if (retbuf != NULL)
2545 {
2546 info->buffer = retbuf;
2547 return;
2548 }
2549
a32a8923
DE
2550 abfd = get_section_bfd_owner (info);
2551 gdb_assert (abfd != NULL);
2552
dce234bc
PP
2553 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2554 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2555 {
2556 error (_("Dwarf Error: Can't read DWARF data"
2557 " in section %s [in module %s]"),
fd361982 2558 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2559 }
dce234bc
PP
2560}
2561
9e0ac564
TT
2562/* A helper function that returns the size of a section in a safe way.
2563 If you are positive that the section has been read before using the
2564 size, then it is safe to refer to the dwarf2_section_info object's
2565 "size" field directly. In other cases, you must call this
2566 function, because for compressed sections the size field is not set
2567 correctly until the section has been read. */
2568
2569static bfd_size_type
2570dwarf2_section_size (struct objfile *objfile,
2571 struct dwarf2_section_info *info)
2572{
2573 if (!info->readin)
2574 dwarf2_read_section (objfile, info);
2575 return info->size;
2576}
2577
dce234bc 2578/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2579 SECTION_NAME. */
af34e669 2580
dce234bc 2581void
3017a003
TG
2582dwarf2_get_section_info (struct objfile *objfile,
2583 enum dwarf2_section_enum sect,
d521ce57 2584 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2585 bfd_size_type *sizep)
2586{
5bfd760d 2587 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2588 struct dwarf2_section_info *info;
a3b2a86b
TT
2589
2590 /* We may see an objfile without any DWARF, in which case we just
2591 return nothing. */
2592 if (data == NULL)
2593 {
2594 *sectp = NULL;
2595 *bufp = NULL;
2596 *sizep = 0;
2597 return;
2598 }
3017a003
TG
2599 switch (sect)
2600 {
2601 case DWARF2_DEBUG_FRAME:
2602 info = &data->frame;
2603 break;
2604 case DWARF2_EH_FRAME:
2605 info = &data->eh_frame;
2606 break;
2607 default:
2608 gdb_assert_not_reached ("unexpected section");
2609 }
dce234bc 2610
9e0ac564 2611 dwarf2_read_section (objfile, info);
dce234bc 2612
a32a8923 2613 *sectp = get_section_bfd_section (info);
dce234bc
PP
2614 *bufp = info->buffer;
2615 *sizep = info->size;
2616}
2617
36586728
TT
2618/* A helper function to find the sections for a .dwz file. */
2619
2620static void
2621locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2622{
9a3c8263 2623 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2624
2625 /* Note that we only support the standard ELF names, because .dwz
2626 is ELF-only (at the time of writing). */
2627 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2628 {
049412e3 2629 dwz_file->abbrev.s.section = sectp;
fd361982 2630 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2631 }
2632 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2633 {
049412e3 2634 dwz_file->info.s.section = sectp;
fd361982 2635 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2636 }
2637 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2638 {
049412e3 2639 dwz_file->str.s.section = sectp;
fd361982 2640 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2641 }
2642 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2643 {
049412e3 2644 dwz_file->line.s.section = sectp;
fd361982 2645 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2646 }
2647 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2648 {
049412e3 2649 dwz_file->macro.s.section = sectp;
fd361982 2650 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2651 }
2ec9a5e0
TT
2652 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2653 {
049412e3 2654 dwz_file->gdb_index.s.section = sectp;
fd361982 2655 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2656 }
927aa2e7
JK
2657 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2658 {
2659 dwz_file->debug_names.s.section = sectp;
fd361982 2660 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2661 }
36586728
TT
2662}
2663
c4973306 2664/* See dwarf2read.h. */
36586728 2665
c4973306 2666struct dwz_file *
ed2dc618 2667dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2668{
36586728 2669 const char *filename;
acd13123 2670 bfd_size_type buildid_len_arg;
dc294be5
TT
2671 size_t buildid_len;
2672 bfd_byte *buildid;
36586728
TT
2673
2674 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2675 return dwarf2_per_objfile->dwz_file.get ();
36586728 2676
4db1a1dc 2677 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2678 gdb::unique_xmalloc_ptr<char> data
2679 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2680 &buildid_len_arg, &buildid));
4db1a1dc
TT
2681 if (data == NULL)
2682 {
2683 if (bfd_get_error () == bfd_error_no_error)
2684 return NULL;
2685 error (_("could not read '.gnu_debugaltlink' section: %s"),
2686 bfd_errmsg (bfd_get_error ()));
2687 }
791afaa2
TT
2688
2689 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2690
acd13123
TT
2691 buildid_len = (size_t) buildid_len_arg;
2692
791afaa2 2693 filename = data.get ();
d721ba37
PA
2694
2695 std::string abs_storage;
36586728
TT
2696 if (!IS_ABSOLUTE_PATH (filename))
2697 {
14278e1f
TT
2698 gdb::unique_xmalloc_ptr<char> abs
2699 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2700
14278e1f 2701 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2702 filename = abs_storage.c_str ();
36586728
TT
2703 }
2704
dc294be5
TT
2705 /* First try the file name given in the section. If that doesn't
2706 work, try to use the build-id instead. */
192b62ce 2707 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2708 if (dwz_bfd != NULL)
36586728 2709 {
192b62ce 2710 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2711 dwz_bfd.reset (nullptr);
36586728
TT
2712 }
2713
dc294be5
TT
2714 if (dwz_bfd == NULL)
2715 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2716
2717 if (dwz_bfd == NULL)
2718 error (_("could not find '.gnu_debugaltlink' file for %s"),
2719 objfile_name (dwarf2_per_objfile->objfile));
2720
7ff8cb8c
TT
2721 std::unique_ptr<struct dwz_file> result
2722 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2723
7ff8cb8c
TT
2724 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2725 result.get ());
36586728 2726
7ff8cb8c
TT
2727 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2728 result->dwz_bfd.get ());
2729 dwarf2_per_objfile->dwz_file = std::move (result);
2730 return dwarf2_per_objfile->dwz_file.get ();
36586728 2731}
9291a0cd 2732\f
7b9f3c50
DE
2733/* DWARF quick_symbols_functions support. */
2734
2735/* TUs can share .debug_line entries, and there can be a lot more TUs than
2736 unique line tables, so we maintain a separate table of all .debug_line
2737 derived entries to support the sharing.
2738 All the quick functions need is the list of file names. We discard the
2739 line_header when we're done and don't need to record it here. */
2740struct quick_file_names
2741{
094b34ac
DE
2742 /* The data used to construct the hash key. */
2743 struct stmt_list_hash hash;
7b9f3c50
DE
2744
2745 /* The number of entries in file_names, real_names. */
2746 unsigned int num_file_names;
2747
2748 /* The file names from the line table, after being run through
2749 file_full_name. */
2750 const char **file_names;
2751
2752 /* The file names from the line table after being run through
2753 gdb_realpath. These are computed lazily. */
2754 const char **real_names;
2755};
2756
2757/* When using the index (and thus not using psymtabs), each CU has an
2758 object of this type. This is used to hold information needed by
2759 the various "quick" methods. */
2760struct dwarf2_per_cu_quick_data
2761{
2762 /* The file table. This can be NULL if there was no file table
2763 or it's currently not read in.
2764 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2765 struct quick_file_names *file_names;
2766
2767 /* The corresponding symbol table. This is NULL if symbols for this
2768 CU have not yet been read. */
43f3e411 2769 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2770
2771 /* A temporary mark bit used when iterating over all CUs in
2772 expand_symtabs_matching. */
2773 unsigned int mark : 1;
2774
2775 /* True if we've tried to read the file table and found there isn't one.
2776 There will be no point in trying to read it again next time. */
2777 unsigned int no_file_data : 1;
2778};
2779
094b34ac
DE
2780/* Utility hash function for a stmt_list_hash. */
2781
2782static hashval_t
2783hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2784{
2785 hashval_t v = 0;
2786
2787 if (stmt_list_hash->dwo_unit != NULL)
2788 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2789 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2790 return v;
2791}
2792
2793/* Utility equality function for a stmt_list_hash. */
2794
2795static int
2796eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2797 const struct stmt_list_hash *rhs)
2798{
2799 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2800 return 0;
2801 if (lhs->dwo_unit != NULL
2802 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2803 return 0;
2804
9c541725 2805 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2806}
2807
7b9f3c50
DE
2808/* Hash function for a quick_file_names. */
2809
2810static hashval_t
2811hash_file_name_entry (const void *e)
2812{
9a3c8263
SM
2813 const struct quick_file_names *file_data
2814 = (const struct quick_file_names *) e;
7b9f3c50 2815
094b34ac 2816 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2817}
2818
2819/* Equality function for a quick_file_names. */
2820
2821static int
2822eq_file_name_entry (const void *a, const void *b)
2823{
9a3c8263
SM
2824 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2825 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2826
094b34ac 2827 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2828}
2829
2830/* Delete function for a quick_file_names. */
2831
2832static void
2833delete_file_name_entry (void *e)
2834{
9a3c8263 2835 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2836 int i;
2837
2838 for (i = 0; i < file_data->num_file_names; ++i)
2839 {
2840 xfree ((void*) file_data->file_names[i]);
2841 if (file_data->real_names)
2842 xfree ((void*) file_data->real_names[i]);
2843 }
2844
2845 /* The space for the struct itself lives on objfile_obstack,
2846 so we don't free it here. */
2847}
2848
2849/* Create a quick_file_names hash table. */
2850
2851static htab_t
2852create_quick_file_names_table (unsigned int nr_initial_entries)
2853{
2854 return htab_create_alloc (nr_initial_entries,
2855 hash_file_name_entry, eq_file_name_entry,
2856 delete_file_name_entry, xcalloc, xfree);
2857}
9291a0cd 2858
918dd910
JK
2859/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2860 have to be created afterwards. You should call age_cached_comp_units after
2861 processing PER_CU->CU. dw2_setup must have been already called. */
2862
2863static void
58f0c718 2864load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2865{
3019eac3 2866 if (per_cu->is_debug_types)
e5fe5e75 2867 load_full_type_unit (per_cu);
918dd910 2868 else
58f0c718 2869 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2870
cc12ce38
DE
2871 if (per_cu->cu == NULL)
2872 return; /* Dummy CU. */
2dc860c0
DE
2873
2874 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2875}
2876
a0f42c21 2877/* Read in the symbols for PER_CU. */
2fdf6df6 2878
9291a0cd 2879static void
58f0c718 2880dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2881{
ed2dc618 2882 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2883
f4dc4d17
DE
2884 /* Skip type_unit_groups, reading the type units they contain
2885 is handled elsewhere. */
2886 if (IS_TYPE_UNIT_GROUP (per_cu))
2887 return;
2888
b303c6f6
AB
2889 /* The destructor of dwarf2_queue_guard frees any entries left on
2890 the queue. After this point we're guaranteed to leave this function
2891 with the dwarf queue empty. */
2892 dwarf2_queue_guard q_guard;
9291a0cd 2893
95554aad 2894 if (dwarf2_per_objfile->using_index
43f3e411 2895 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2896 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2897 {
2898 queue_comp_unit (per_cu, language_minimal);
58f0c718 2899 load_cu (per_cu, skip_partial);
89e63ee4
DE
2900
2901 /* If we just loaded a CU from a DWO, and we're working with an index
2902 that may badly handle TUs, load all the TUs in that DWO as well.
2903 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2904 if (!per_cu->is_debug_types
cc12ce38 2905 && per_cu->cu != NULL
89e63ee4
DE
2906 && per_cu->cu->dwo_unit != NULL
2907 && dwarf2_per_objfile->index_table != NULL
2908 && dwarf2_per_objfile->index_table->version <= 7
2909 /* DWP files aren't supported yet. */
ed2dc618 2910 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2911 queue_and_load_all_dwo_tus (per_cu);
95554aad 2912 }
9291a0cd 2913
ed2dc618 2914 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2915
2916 /* Age the cache, releasing compilation units that have not
2917 been used recently. */
ed2dc618 2918 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2919}
2920
2921/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2922 the objfile from which this CU came. Returns the resulting symbol
2923 table. */
2fdf6df6 2924
43f3e411 2925static struct compunit_symtab *
58f0c718 2926dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2927{
ed2dc618
SM
2928 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2929
95554aad 2930 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2931 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2932 {
11ed8cad 2933 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2934 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2935 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2936 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2937 }
f194fefb 2938
43f3e411 2939 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2940}
2941
ff4c9fec 2942/* See declaration. */
f4dc4d17 2943
ff4c9fec
SM
2944dwarf2_per_cu_data *
2945dwarf2_per_objfile::get_cutu (int index)
2946{
b76e467d 2947 if (index >= this->all_comp_units.size ())
ff4c9fec 2948 {
b76e467d 2949 index -= this->all_comp_units.size ();
b2bdb8cf 2950 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2951 return &this->all_type_units[index]->per_cu;
2952 }
f4dc4d17 2953
ff4c9fec
SM
2954 return this->all_comp_units[index];
2955}
f4dc4d17 2956
ff4c9fec 2957/* See declaration. */
2fdf6df6 2958
ff4c9fec
SM
2959dwarf2_per_cu_data *
2960dwarf2_per_objfile::get_cu (int index)
1fd400ff 2961{
b76e467d 2962 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2963
ff4c9fec 2964 return this->all_comp_units[index];
f4dc4d17
DE
2965}
2966
ff4c9fec 2967/* See declaration. */
f4dc4d17 2968
ff4c9fec
SM
2969signatured_type *
2970dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2971{
b2bdb8cf 2972 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2973
ff4c9fec 2974 return this->all_type_units[index];
1fd400ff
TT
2975}
2976
4b514bc8
JK
2977/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2978 objfile_obstack, and constructed with the specified field
2979 values. */
2980
2981static dwarf2_per_cu_data *
ed2dc618 2982create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2983 struct dwarf2_section_info *section,
2984 int is_dwz,
2985 sect_offset sect_off, ULONGEST length)
2986{
ed2dc618 2987 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2988 dwarf2_per_cu_data *the_cu
2989 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2990 struct dwarf2_per_cu_data);
2991 the_cu->sect_off = sect_off;
2992 the_cu->length = length;
e3b94546 2993 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2994 the_cu->section = section;
2995 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2996 struct dwarf2_per_cu_quick_data);
2997 the_cu->is_dwz = is_dwz;
2998 return the_cu;
2999}
3000
2ec9a5e0
TT
3001/* A helper for create_cus_from_index that handles a given list of
3002 CUs. */
2fdf6df6 3003
74a0d9f6 3004static void
12359b5e 3005create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3006 const gdb_byte *cu_list, offset_type n_elements,
3007 struct dwarf2_section_info *section,
b76e467d 3008 int is_dwz)
9291a0cd 3009{
12359b5e 3010 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3011 {
74a0d9f6 3012 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3013
3014 sect_offset sect_off
3015 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3016 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3017 cu_list += 2 * 8;
3018
b76e467d 3019 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3020 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3021 sect_off, length);
b76e467d 3022 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3023 }
9291a0cd
TT
3024}
3025
2ec9a5e0 3026/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3027 the CU objects for this objfile. */
2ec9a5e0 3028
74a0d9f6 3029static void
12359b5e 3030create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3031 const gdb_byte *cu_list, offset_type cu_list_elements,
3032 const gdb_byte *dwz_list, offset_type dwz_elements)
3033{
b76e467d
SM
3034 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3035 dwarf2_per_objfile->all_comp_units.reserve
3036 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3037
12359b5e 3038 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3039 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3040
3041 if (dwz_elements == 0)
74a0d9f6 3042 return;
2ec9a5e0 3043
12359b5e
SM
3044 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3045 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3046 &dwz->info, 1);
2ec9a5e0
TT
3047}
3048
1fd400ff 3049/* Create the signatured type hash table from the index. */
673bfd45 3050
74a0d9f6 3051static void
12359b5e
SM
3052create_signatured_type_table_from_index
3053 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3054 struct dwarf2_section_info *section,
3055 const gdb_byte *bytes,
3056 offset_type elements)
1fd400ff 3057{
12359b5e 3058 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3059
b2bdb8cf
SM
3060 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3061 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3062
12359b5e 3063 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3064
12359b5e 3065 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3066 {
52dc124a 3067 struct signatured_type *sig_type;
9c541725 3068 ULONGEST signature;
1fd400ff 3069 void **slot;
9c541725 3070 cu_offset type_offset_in_tu;
1fd400ff 3071
74a0d9f6 3072 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3073 sect_offset sect_off
3074 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3075 type_offset_in_tu
3076 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3077 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3078 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3079 bytes += 3 * 8;
3080
52dc124a 3081 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3082 struct signatured_type);
52dc124a 3083 sig_type->signature = signature;
9c541725 3084 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3085 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3086 sig_type->per_cu.section = section;
9c541725 3087 sig_type->per_cu.sect_off = sect_off;
e3b94546 3088 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3089 sig_type->per_cu.v.quick
1fd400ff
TT
3090 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3091 struct dwarf2_per_cu_quick_data);
3092
52dc124a
DE
3093 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3094 *slot = sig_type;
1fd400ff 3095
b2bdb8cf 3096 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3097 }
3098
673bfd45 3099 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3100}
3101
927aa2e7
JK
3102/* Create the signatured type hash table from .debug_names. */
3103
3104static void
3105create_signatured_type_table_from_debug_names
ed2dc618 3106 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3107 const mapped_debug_names &map,
3108 struct dwarf2_section_info *section,
3109 struct dwarf2_section_info *abbrev_section)
3110{
ed2dc618
SM
3111 struct objfile *objfile = dwarf2_per_objfile->objfile;
3112
927aa2e7
JK
3113 dwarf2_read_section (objfile, section);
3114 dwarf2_read_section (objfile, abbrev_section);
3115
b2bdb8cf
SM
3116 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3117 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3118
3119 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3120
3121 for (uint32_t i = 0; i < map.tu_count; ++i)
3122 {
3123 struct signatured_type *sig_type;
927aa2e7 3124 void **slot;
927aa2e7
JK
3125
3126 sect_offset sect_off
3127 = (sect_offset) (extract_unsigned_integer
3128 (map.tu_table_reordered + i * map.offset_size,
3129 map.offset_size,
3130 map.dwarf5_byte_order));
3131
3132 comp_unit_head cu_header;
ed2dc618
SM
3133 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3134 abbrev_section,
927aa2e7
JK
3135 section->buffer + to_underlying (sect_off),
3136 rcuh_kind::TYPE);
3137
3138 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3139 struct signatured_type);
3140 sig_type->signature = cu_header.signature;
3141 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3142 sig_type->per_cu.is_debug_types = 1;
3143 sig_type->per_cu.section = section;
3144 sig_type->per_cu.sect_off = sect_off;
e3b94546 3145 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3146 sig_type->per_cu.v.quick
3147 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3148 struct dwarf2_per_cu_quick_data);
3149
3150 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3151 *slot = sig_type;
3152
b2bdb8cf 3153 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3154 }
3155
3156 dwarf2_per_objfile->signatured_types = sig_types_hash;
3157}
3158
9291a0cd
TT
3159/* Read the address map data from the mapped index, and use it to
3160 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3161
9291a0cd 3162static void
ed2dc618
SM
3163create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3164 struct mapped_index *index)
9291a0cd 3165{
ed2dc618 3166 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3167 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3168 const gdb_byte *iter, *end;
9291a0cd 3169 struct addrmap *mutable_map;
9291a0cd
TT
3170 CORE_ADDR baseaddr;
3171
8268c778
PA
3172 auto_obstack temp_obstack;
3173
9291a0cd
TT
3174 mutable_map = addrmap_create_mutable (&temp_obstack);
3175
f00a2de2
PA
3176 iter = index->address_table.data ();
3177 end = iter + index->address_table.size ();
9291a0cd
TT
3178
3179 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3180
3181 while (iter < end)
3182 {
3183 ULONGEST hi, lo, cu_index;
3184 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3185 iter += 8;
3186 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3187 iter += 8;
3188 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3189 iter += 4;
f652bce2 3190
24a55014 3191 if (lo > hi)
f652bce2 3192 {
b98664d3 3193 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3194 hex_string (lo), hex_string (hi));
24a55014 3195 continue;
f652bce2 3196 }
24a55014 3197
b76e467d 3198 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3199 {
b98664d3 3200 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3201 (unsigned) cu_index);
24a55014 3202 continue;
f652bce2 3203 }
24a55014 3204
79748972
TT
3205 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3206 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3207 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3208 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3209 }
3210
d320c2b5 3211 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3212 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3213}
3214
927aa2e7
JK
3215/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3216 populate the objfile's psymtabs_addrmap. */
3217
3218static void
ed2dc618 3219create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3220 struct dwarf2_section_info *section)
3221{
ed2dc618 3222 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3223 bfd *abfd = objfile->obfd;
3224 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3225 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3226 SECT_OFF_TEXT (objfile));
3227
3228 auto_obstack temp_obstack;
3229 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3230
3231 std::unordered_map<sect_offset,
3232 dwarf2_per_cu_data *,
3233 gdb::hash_enum<sect_offset>>
3234 debug_info_offset_to_per_cu;
b76e467d 3235 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3236 {
927aa2e7
JK
3237 const auto insertpair
3238 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3239 if (!insertpair.second)
3240 {
3241 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3242 "debug_info_offset %s, ignoring .debug_aranges."),
3243 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3244 return;
3245 }
3246 }
3247
3248 dwarf2_read_section (objfile, section);
3249
3250 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3251
3252 const gdb_byte *addr = section->buffer;
3253
3254 while (addr < section->buffer + section->size)
3255 {
3256 const gdb_byte *const entry_addr = addr;
3257 unsigned int bytes_read;
3258
3259 const LONGEST entry_length = read_initial_length (abfd, addr,
3260 &bytes_read);
3261 addr += bytes_read;
3262
3263 const gdb_byte *const entry_end = addr + entry_length;
3264 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3265 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3266 if (addr + entry_length > section->buffer + section->size)
3267 {
47e3f474 3268 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3269 "length %s exceeds section length %s, "
3270 "ignoring .debug_aranges."),
47e3f474
TV
3271 objfile_name (objfile),
3272 plongest (entry_addr - section->buffer),
927aa2e7
JK
3273 plongest (bytes_read + entry_length),
3274 pulongest (section->size));
3275 return;
3276 }
3277
3278 /* The version number. */
3279 const uint16_t version = read_2_bytes (abfd, addr);
3280 addr += 2;
3281 if (version != 2)
3282 {
47e3f474 3283 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3284 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3285 objfile_name (objfile),
3286 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3287 return;
3288 }
3289
3290 const uint64_t debug_info_offset
3291 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3292 addr += offset_size;
3293 const auto per_cu_it
3294 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3295 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3296 {
47e3f474 3297 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3298 "debug_info_offset %s does not exists, "
3299 "ignoring .debug_aranges."),
47e3f474
TV
3300 objfile_name (objfile),
3301 plongest (entry_addr - section->buffer),
927aa2e7
JK
3302 pulongest (debug_info_offset));
3303 return;
3304 }
3305 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3306
3307 const uint8_t address_size = *addr++;
3308 if (address_size < 1 || address_size > 8)
3309 {
47e3f474 3310 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3311 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3312 objfile_name (objfile),
3313 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3314 return;
3315 }
3316
3317 const uint8_t segment_selector_size = *addr++;
3318 if (segment_selector_size != 0)
3319 {
47e3f474 3320 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3321 "segment_selector_size %u is not supported, "
3322 "ignoring .debug_aranges."),
47e3f474
TV
3323 objfile_name (objfile),
3324 plongest (entry_addr - section->buffer),
927aa2e7
JK
3325 segment_selector_size);
3326 return;
3327 }
3328
3329 /* Must pad to an alignment boundary that is twice the address
3330 size. It is undocumented by the DWARF standard but GCC does
3331 use it. */
3332 for (size_t padding = ((-(addr - section->buffer))
3333 & (2 * address_size - 1));
3334 padding > 0; padding--)
3335 if (*addr++ != 0)
3336 {
47e3f474 3337 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3338 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3339 objfile_name (objfile),
3340 plongest (entry_addr - section->buffer));
927aa2e7
JK
3341 return;
3342 }
3343
3344 for (;;)
3345 {
3346 if (addr + 2 * address_size > entry_end)
3347 {
47e3f474 3348 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3349 "address list is not properly terminated, "
3350 "ignoring .debug_aranges."),
47e3f474
TV
3351 objfile_name (objfile),
3352 plongest (entry_addr - section->buffer));
927aa2e7
JK
3353 return;
3354 }
3355 ULONGEST start = extract_unsigned_integer (addr, address_size,
3356 dwarf5_byte_order);
3357 addr += address_size;
3358 ULONGEST length = extract_unsigned_integer (addr, address_size,
3359 dwarf5_byte_order);
3360 addr += address_size;
3361 if (start == 0 && length == 0)
3362 break;
3363 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3364 {
3365 /* Symbol was eliminated due to a COMDAT group. */
3366 continue;
3367 }
3368 ULONGEST end = start + length;
79748972
TT
3369 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3370 - baseaddr);
3371 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3372 - baseaddr);
927aa2e7
JK
3373 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3374 }
3375 }
3376
d320c2b5 3377 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3378 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3379}
3380
9291a0cd
TT
3381/* Find a slot in the mapped index INDEX for the object named NAME.
3382 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3383 constant pool and return true. If NAME cannot be found, return
3384 false. */
2fdf6df6 3385
109483d9 3386static bool
9291a0cd
TT
3387find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3388 offset_type **vec_out)
3389{
0cf03b49 3390 offset_type hash;
9291a0cd 3391 offset_type slot, step;
559a7a62 3392 int (*cmp) (const char *, const char *);
9291a0cd 3393
791afaa2 3394 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3395 if (current_language->la_language == language_cplus
45280282
IB
3396 || current_language->la_language == language_fortran
3397 || current_language->la_language == language_d)
0cf03b49
JK
3398 {
3399 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3400 not contain any. */
a8719064 3401
72998fb3 3402 if (strchr (name, '(') != NULL)
0cf03b49 3403 {
109483d9 3404 without_params = cp_remove_params (name);
0cf03b49 3405
72998fb3 3406 if (without_params != NULL)
791afaa2 3407 name = without_params.get ();
0cf03b49
JK
3408 }
3409 }
3410
559a7a62 3411 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3412 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3413 simulate our NAME being searched is also lowercased. */
3414 hash = mapped_index_string_hash ((index->version == 4
3415 && case_sensitivity == case_sensitive_off
3416 ? 5 : index->version),
3417 name);
3418
f00a2de2
PA
3419 slot = hash & (index->symbol_table.size () - 1);
3420 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3421 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3422
3423 for (;;)
3424 {
9291a0cd 3425 const char *str;
f00a2de2
PA
3426
3427 const auto &bucket = index->symbol_table[slot];
3428 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3429 return false;
9291a0cd 3430
f00a2de2 3431 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3432 if (!cmp (name, str))
9291a0cd
TT
3433 {
3434 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3435 + MAYBE_SWAP (bucket.vec));
109483d9 3436 return true;
9291a0cd
TT
3437 }
3438
f00a2de2 3439 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3440 }
3441}
3442
4485a1c1
SM
3443/* A helper function that reads the .gdb_index from BUFFER and fills
3444 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3445 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3446 ok to use deprecated sections.
3447
3448 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3449 out parameters that are filled in with information about the CU and
3450 TU lists in the section.
3451
4485a1c1 3452 Returns true if all went well, false otherwise. */
2fdf6df6 3453
d33bc52e 3454static bool
4485a1c1
SM
3455read_gdb_index_from_buffer (struct objfile *objfile,
3456 const char *filename,
3457 bool deprecated_ok,
3458 gdb::array_view<const gdb_byte> buffer,
3459 struct mapped_index *map,
3460 const gdb_byte **cu_list,
3461 offset_type *cu_list_elements,
3462 const gdb_byte **types_list,
3463 offset_type *types_list_elements)
3464{
3465 const gdb_byte *addr = &buffer[0];
82430852 3466
9291a0cd 3467 /* Version check. */
4485a1c1 3468 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3469 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3470 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3471 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3472 indices. */
831adc1f 3473 if (version < 4)
481860b3
GB
3474 {
3475 static int warning_printed = 0;
3476 if (!warning_printed)
3477 {
3478 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3479 filename);
481860b3
GB
3480 warning_printed = 1;
3481 }
3482 return 0;
3483 }
3484 /* Index version 4 uses a different hash function than index version
3485 5 and later.
3486
3487 Versions earlier than 6 did not emit psymbols for inlined
3488 functions. Using these files will cause GDB not to be able to
3489 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3490 indices unless the user has done
3491 "set use-deprecated-index-sections on". */
2ec9a5e0 3492 if (version < 6 && !deprecated_ok)
481860b3
GB
3493 {
3494 static int warning_printed = 0;
3495 if (!warning_printed)
3496 {
e615022a
DE
3497 warning (_("\
3498Skipping deprecated .gdb_index section in %s.\n\
3499Do \"set use-deprecated-index-sections on\" before the file is read\n\
3500to use the section anyway."),
2ec9a5e0 3501 filename);
481860b3
GB
3502 warning_printed = 1;
3503 }
3504 return 0;
3505 }
796a7ff8 3506 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3507 of the TU (for symbols coming from TUs),
3508 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3509 Plus gold-generated indices can have duplicate entries for global symbols,
3510 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3511 These are just performance bugs, and we can't distinguish gdb-generated
3512 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3513
481860b3 3514 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3515 longer backward compatible. */
796a7ff8 3516 if (version > 8)
594e8718 3517 return 0;
9291a0cd 3518
559a7a62 3519 map->version = version;
9291a0cd 3520
4485a1c1 3521 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3522
4485a1c1 3523 int i = 0;
2ec9a5e0
TT
3524 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3525 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3526 / 8);
1fd400ff
TT
3527 ++i;
3528
2ec9a5e0
TT
3529 *types_list = addr + MAYBE_SWAP (metadata[i]);
3530 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3531 - MAYBE_SWAP (metadata[i]))
3532 / 8);
987d643c 3533 ++i;
1fd400ff 3534
f00a2de2
PA
3535 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3536 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3537 map->address_table
3538 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3539 ++i;
3540
f00a2de2
PA
3541 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3542 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3543 map->symbol_table
3544 = gdb::array_view<mapped_index::symbol_table_slot>
3545 ((mapped_index::symbol_table_slot *) symbol_table,
3546 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3547
f00a2de2 3548 ++i;
f9d83a0b 3549 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3550
2ec9a5e0
TT
3551 return 1;
3552}
3553
4485a1c1
SM
3554/* Callback types for dwarf2_read_gdb_index. */
3555
3556typedef gdb::function_view
3557 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3558 get_gdb_index_contents_ftype;
3559typedef gdb::function_view
3560 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3561 get_gdb_index_contents_dwz_ftype;
3562
927aa2e7 3563/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3564 elements of all the CUs and return 1. Otherwise, return 0. */
3565
3566static int
4485a1c1
SM
3567dwarf2_read_gdb_index
3568 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3569 get_gdb_index_contents_ftype get_gdb_index_contents,
3570 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3571{
2ec9a5e0
TT
3572 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3573 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3574 struct dwz_file *dwz;
12359b5e 3575 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3576
4485a1c1
SM
3577 gdb::array_view<const gdb_byte> main_index_contents
3578 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3579
3580 if (main_index_contents.empty ())
3581 return 0;
3582
3063847f 3583 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3584 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3585 use_deprecated_index_sections,
3586 main_index_contents, map.get (), &cu_list,
3587 &cu_list_elements, &types_list,
3588 &types_list_elements))
2ec9a5e0
TT
3589 return 0;
3590
0fefef59 3591 /* Don't use the index if it's empty. */
3063847f 3592 if (map->symbol_table.empty ())
0fefef59
DE
3593 return 0;
3594
2ec9a5e0
TT
3595 /* If there is a .dwz file, read it so we can get its CU list as
3596 well. */
ed2dc618 3597 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3598 if (dwz != NULL)
2ec9a5e0 3599 {
2ec9a5e0
TT
3600 struct mapped_index dwz_map;
3601 const gdb_byte *dwz_types_ignore;
3602 offset_type dwz_types_elements_ignore;
3603
4485a1c1
SM
3604 gdb::array_view<const gdb_byte> dwz_index_content
3605 = get_gdb_index_contents_dwz (objfile, dwz);
3606
3607 if (dwz_index_content.empty ())
3608 return 0;
3609
3610 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3611 bfd_get_filename (dwz->dwz_bfd.get ()),
3612 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3613 &dwz_list, &dwz_list_elements,
3614 &dwz_types_ignore,
3615 &dwz_types_elements_ignore))
2ec9a5e0
TT
3616 {
3617 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3618 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3619 return 0;
3620 }
3621 }
3622
12359b5e
SM
3623 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3624 dwz_list, dwz_list_elements);
1fd400ff 3625
8b70b953
TT
3626 if (types_list_elements)
3627 {
8b70b953
TT
3628 /* We can only handle a single .debug_types when we have an
3629 index. */
fd5866f6 3630 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3631 return 0;
3632
fd5866f6 3633 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3634
12359b5e
SM
3635 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3636 types_list, types_list_elements);
8b70b953 3637 }
9291a0cd 3638
3063847f 3639 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3640
3063847f 3641 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3642 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3643 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3644 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3645
3646 return 1;
3647}
3648
dee91e82 3649/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3650
dee91e82
DE
3651static void
3652dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3653 const gdb_byte *info_ptr,
dee91e82
DE
3654 struct die_info *comp_unit_die,
3655 int has_children,
3656 void *data)
9291a0cd 3657{
dee91e82 3658 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3659 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3660 struct dwarf2_per_objfile *dwarf2_per_objfile
3661 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3662 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3663 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3664 struct attribute *attr;
7b9f3c50
DE
3665 void **slot;
3666 struct quick_file_names *qfn;
9291a0cd 3667
0186c6a7
DE
3668 gdb_assert (! this_cu->is_debug_types);
3669
07261596
TT
3670 /* Our callers never want to match partial units -- instead they
3671 will match the enclosing full CU. */
3672 if (comp_unit_die->tag == DW_TAG_partial_unit)
3673 {
3674 this_cu->v.quick->no_file_data = 1;
3675 return;
3676 }
3677
0186c6a7 3678 lh_cu = this_cu;
7b9f3c50 3679 slot = NULL;
dee91e82 3680
fff8551c 3681 line_header_up lh;
9c541725 3682 sect_offset line_offset {};
fff8551c 3683
dee91e82 3684 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3685 if (attr != nullptr)
9291a0cd 3686 {
7b9f3c50
DE
3687 struct quick_file_names find_entry;
3688
9c541725 3689 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3690
3691 /* We may have already read in this line header (TU line header sharing).
3692 If we have we're done. */
094b34ac 3693 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3694 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3695 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3696 &find_entry, INSERT);
3697 if (*slot != NULL)
3698 {
9a3c8263 3699 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3700 return;
7b9f3c50
DE
3701 }
3702
3019eac3 3703 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3704 }
3705 if (lh == NULL)
3706 {
094b34ac 3707 lh_cu->v.quick->no_file_data = 1;
dee91e82 3708 return;
9291a0cd
TT
3709 }
3710
8d749320 3711 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3712 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3713 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3714 gdb_assert (slot != NULL);
3715 *slot = qfn;
9291a0cd 3716
d721ba37 3717 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3718
aa391654
TT
3719 int offset = 0;
3720 if (strcmp (fnd.name, "<unknown>") != 0)
3721 ++offset;
3722
7ba99d21 3723 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3724 qfn->file_names =
aa391654
TT
3725 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3726 if (offset != 0)
3727 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3728 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3729 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3730 qfn->real_names = NULL;
9291a0cd 3731
094b34ac 3732 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3733}
3734
3735/* A helper for the "quick" functions which attempts to read the line
3736 table for THIS_CU. */
3737
3738static struct quick_file_names *
e4a48d9d 3739dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3740{
0186c6a7
DE
3741 /* This should never be called for TUs. */
3742 gdb_assert (! this_cu->is_debug_types);
3743 /* Nor type unit groups. */
3744 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3745
dee91e82
DE
3746 if (this_cu->v.quick->file_names != NULL)
3747 return this_cu->v.quick->file_names;
3748 /* If we know there is no line data, no point in looking again. */
3749 if (this_cu->v.quick->no_file_data)
3750 return NULL;
3751
0186c6a7 3752 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3753
3754 if (this_cu->v.quick->no_file_data)
3755 return NULL;
3756 return this_cu->v.quick->file_names;
9291a0cd
TT
3757}
3758
3759/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3760 real path for a given file name from the line table. */
2fdf6df6 3761
9291a0cd 3762static const char *
7b9f3c50
DE
3763dw2_get_real_path (struct objfile *objfile,
3764 struct quick_file_names *qfn, int index)
9291a0cd 3765{
7b9f3c50
DE
3766 if (qfn->real_names == NULL)
3767 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3768 qfn->num_file_names, const char *);
9291a0cd 3769
7b9f3c50 3770 if (qfn->real_names[index] == NULL)
14278e1f 3771 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3772
7b9f3c50 3773 return qfn->real_names[index];
9291a0cd
TT
3774}
3775
3776static struct symtab *
3777dw2_find_last_source_symtab (struct objfile *objfile)
3778{
ed2dc618
SM
3779 struct dwarf2_per_objfile *dwarf2_per_objfile
3780 = get_dwarf2_per_objfile (objfile);
b76e467d 3781 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3782 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3783
43f3e411
DE
3784 if (cust == NULL)
3785 return NULL;
ed2dc618 3786
43f3e411 3787 return compunit_primary_filetab (cust);
9291a0cd
TT
3788}
3789
7b9f3c50
DE
3790/* Traversal function for dw2_forget_cached_source_info. */
3791
3792static int
3793dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3794{
7b9f3c50 3795 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3796
7b9f3c50 3797 if (file_data->real_names)
9291a0cd 3798 {
7b9f3c50 3799 int i;
9291a0cd 3800
7b9f3c50 3801 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3802 {
7b9f3c50
DE
3803 xfree ((void*) file_data->real_names[i]);
3804 file_data->real_names[i] = NULL;
9291a0cd
TT
3805 }
3806 }
7b9f3c50
DE
3807
3808 return 1;
3809}
3810
3811static void
3812dw2_forget_cached_source_info (struct objfile *objfile)
3813{
ed2dc618
SM
3814 struct dwarf2_per_objfile *dwarf2_per_objfile
3815 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3816
3817 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3818 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3819}
3820
f8eba3c6
TT
3821/* Helper function for dw2_map_symtabs_matching_filename that expands
3822 the symtabs and calls the iterator. */
3823
3824static int
3825dw2_map_expand_apply (struct objfile *objfile,
3826 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3827 const char *name, const char *real_path,
14bc53a8 3828 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3829{
43f3e411 3830 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3831
3832 /* Don't visit already-expanded CUs. */
43f3e411 3833 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3834 return 0;
3835
3836 /* This may expand more than one symtab, and we want to iterate over
3837 all of them. */
58f0c718 3838 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3839
14bc53a8
PA
3840 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3841 last_made, callback);
f8eba3c6
TT
3842}
3843
3844/* Implementation of the map_symtabs_matching_filename method. */
3845
14bc53a8
PA
3846static bool
3847dw2_map_symtabs_matching_filename
3848 (struct objfile *objfile, const char *name, const char *real_path,
3849 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3850{
c011a4f4 3851 const char *name_basename = lbasename (name);
ed2dc618
SM
3852 struct dwarf2_per_objfile *dwarf2_per_objfile
3853 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3854
848e3e78
DE
3855 /* The rule is CUs specify all the files, including those used by
3856 any TU, so there's no need to scan TUs here. */
f4dc4d17 3857
b76e467d 3858 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3859 {
3d7bb9d9 3860 /* We only need to look at symtabs not already expanded. */
43f3e411 3861 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3862 continue;
3863
b76e467d 3864 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3865 if (file_data == NULL)
9291a0cd
TT
3866 continue;
3867
b76e467d 3868 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3869 {
7b9f3c50 3870 const char *this_name = file_data->file_names[j];
da235a7c 3871 const char *this_real_name;
9291a0cd 3872
af529f8f 3873 if (compare_filenames_for_search (this_name, name))
9291a0cd 3874 {
f5b95b50 3875 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3876 callback))
3877 return true;
288e77a7 3878 continue;
4aac40c8 3879 }
9291a0cd 3880
c011a4f4
DE
3881 /* Before we invoke realpath, which can get expensive when many
3882 files are involved, do a quick comparison of the basenames. */
3883 if (! basenames_may_differ
3884 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3885 continue;
3886
da235a7c
JK
3887 this_real_name = dw2_get_real_path (objfile, file_data, j);
3888 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3889 {
da235a7c 3890 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3891 callback))
3892 return true;
288e77a7 3893 continue;
da235a7c 3894 }
9291a0cd 3895
da235a7c
JK
3896 if (real_path != NULL)
3897 {
af529f8f
JK
3898 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3899 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3900 if (this_real_name != NULL
af529f8f 3901 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3902 {
f5b95b50 3903 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3904 callback))
3905 return true;
288e77a7 3906 continue;
9291a0cd
TT
3907 }
3908 }
3909 }
3910 }
3911
14bc53a8 3912 return false;
9291a0cd
TT
3913}
3914
da51c347
DE
3915/* Struct used to manage iterating over all CUs looking for a symbol. */
3916
3917struct dw2_symtab_iterator
9291a0cd 3918{
ed2dc618
SM
3919 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3920 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3921 /* If set, only look for symbols that match that block. Valid values are
3922 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3923 gdb::optional<block_enum> block_index;
da51c347
DE
3924 /* The kind of symbol we're looking for. */
3925 domain_enum domain;
3926 /* The list of CUs from the index entry of the symbol,
3927 or NULL if not found. */
3928 offset_type *vec;
3929 /* The next element in VEC to look at. */
3930 int next;
3931 /* The number of elements in VEC, or zero if there is no match. */
3932 int length;
8943b874
DE
3933 /* Have we seen a global version of the symbol?
3934 If so we can ignore all further global instances.
3935 This is to work around gold/15646, inefficient gold-generated
3936 indices. */
3937 int global_seen;
da51c347 3938};
9291a0cd 3939
2b79f376 3940/* Initialize the index symtab iterator ITER. */
2fdf6df6 3941
9291a0cd 3942static void
da51c347 3943dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3944 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3945 gdb::optional<block_enum> block_index,
da51c347
DE
3946 domain_enum domain,
3947 const char *name)
3948{
ed2dc618 3949 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3950 iter->block_index = block_index;
3951 iter->domain = domain;
3952 iter->next = 0;
8943b874 3953 iter->global_seen = 0;
da51c347 3954
3063847f 3955 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3956
3957 /* index is NULL if OBJF_READNOW. */
3958 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3959 iter->length = MAYBE_SWAP (*iter->vec);
3960 else
3961 {
3962 iter->vec = NULL;
3963 iter->length = 0;
3964 }
3965}
3966
3967/* Return the next matching CU or NULL if there are no more. */
3968
3969static struct dwarf2_per_cu_data *
3970dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3971{
ed2dc618
SM
3972 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3973
da51c347
DE
3974 for ( ; iter->next < iter->length; ++iter->next)
3975 {
3976 offset_type cu_index_and_attrs =
3977 MAYBE_SWAP (iter->vec[iter->next + 1]);
3978 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3979 gdb_index_symbol_kind symbol_kind =
3980 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3981 /* Only check the symbol attributes if they're present.
3982 Indices prior to version 7 don't record them,
3983 and indices >= 7 may elide them for certain symbols
3984 (gold does this). */
3985 int attrs_valid =
ed2dc618 3986 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3987 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3988
3190f0c6 3989 /* Don't crash on bad data. */
b76e467d 3990 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3991 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3992 {
b98664d3 3993 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3994 " [in module %s]"),
3995 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3996 continue;
3997 }
3998
ff4c9fec 3999 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 4000
da51c347 4001 /* Skip if already read in. */
43f3e411 4002 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4003 continue;
4004
8943b874
DE
4005 /* Check static vs global. */
4006 if (attrs_valid)
4007 {
2b79f376
SM
4008 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4009
4010 if (iter->block_index.has_value ())
4011 {
4012 bool want_static = *iter->block_index == STATIC_BLOCK;
4013
4014 if (is_static != want_static)
4015 continue;
4016 }
4017
8943b874
DE
4018 /* Work around gold/15646. */
4019 if (!is_static && iter->global_seen)
4020 continue;
4021 if (!is_static)
4022 iter->global_seen = 1;
4023 }
da51c347
DE
4024
4025 /* Only check the symbol's kind if it has one. */
4026 if (attrs_valid)
4027 {
4028 switch (iter->domain)
4029 {
4030 case VAR_DOMAIN:
4031 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4032 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4033 /* Some types are also in VAR_DOMAIN. */
4034 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4035 continue;
4036 break;
4037 case STRUCT_DOMAIN:
4038 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4039 continue;
4040 break;
4041 case LABEL_DOMAIN:
4042 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4043 continue;
4044 break;
59c35742
AB
4045 case MODULE_DOMAIN:
4046 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4047 continue;
4048 break;
da51c347
DE
4049 default:
4050 break;
4051 }
4052 }
4053
4054 ++iter->next;
4055 return per_cu;
4056 }
4057
4058 return NULL;
4059}
4060
43f3e411 4061static struct compunit_symtab *
c7f839cb 4062dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4063 const char *name, domain_enum domain)
9291a0cd 4064{
43f3e411 4065 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4066 struct dwarf2_per_objfile *dwarf2_per_objfile
4067 = get_dwarf2_per_objfile (objfile);
9291a0cd 4068
b5ec771e
PA
4069 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4070
ed2dc618
SM
4071 struct dw2_symtab_iterator iter;
4072 struct dwarf2_per_cu_data *per_cu;
da51c347 4073
2b79f376 4074 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4075
ed2dc618
SM
4076 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4077 {
4078 struct symbol *sym, *with_opaque = NULL;
58f0c718 4079 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4080 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4081 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4082
ed2dc618
SM
4083 sym = block_find_symbol (block, name, domain,
4084 block_find_non_opaque_type_preferred,
4085 &with_opaque);
b2e2f908 4086
ed2dc618
SM
4087 /* Some caution must be observed with overloaded functions
4088 and methods, since the index will not contain any overload
4089 information (but NAME might contain it). */
da51c347 4090
ed2dc618
SM
4091 if (sym != NULL
4092 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4093 return stab;
4094 if (with_opaque != NULL
4095 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4096 stab_best = stab;
da51c347 4097
ed2dc618 4098 /* Keep looking through other CUs. */
9291a0cd 4099 }
9291a0cd 4100
da51c347 4101 return stab_best;
9291a0cd
TT
4102}
4103
4104static void
4105dw2_print_stats (struct objfile *objfile)
4106{
ed2dc618
SM
4107 struct dwarf2_per_objfile *dwarf2_per_objfile
4108 = get_dwarf2_per_objfile (objfile);
b76e467d 4109 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4110 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4111 int count = 0;
9291a0cd 4112
ed2dc618 4113 for (int i = 0; i < total; ++i)
9291a0cd 4114 {
ff4c9fec 4115 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4116
43f3e411 4117 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4118 ++count;
4119 }
e4a48d9d 4120 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4121 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4122}
4123
779bd270
DE
4124/* This dumps minimal information about the index.
4125 It is called via "mt print objfiles".
4126 One use is to verify .gdb_index has been loaded by the
4127 gdb.dwarf2/gdb-index.exp testcase. */
4128
9291a0cd
TT
4129static void
4130dw2_dump (struct objfile *objfile)
4131{
ed2dc618
SM
4132 struct dwarf2_per_objfile *dwarf2_per_objfile
4133 = get_dwarf2_per_objfile (objfile);
4134
779bd270
DE
4135 gdb_assert (dwarf2_per_objfile->using_index);
4136 printf_filtered (".gdb_index:");
4137 if (dwarf2_per_objfile->index_table != NULL)
4138 {
4139 printf_filtered (" version %d\n",
4140 dwarf2_per_objfile->index_table->version);
4141 }
4142 else
4143 printf_filtered (" faked for \"readnow\"\n");
4144 printf_filtered ("\n");
9291a0cd
TT
4145}
4146
9291a0cd
TT
4147static void
4148dw2_expand_symtabs_for_function (struct objfile *objfile,
4149 const char *func_name)
4150{
ed2dc618
SM
4151 struct dwarf2_per_objfile *dwarf2_per_objfile
4152 = get_dwarf2_per_objfile (objfile);
da51c347 4153
ed2dc618
SM
4154 struct dw2_symtab_iterator iter;
4155 struct dwarf2_per_cu_data *per_cu;
da51c347 4156
2b79f376 4157 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4158
ed2dc618 4159 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4160 dw2_instantiate_symtab (per_cu, false);
da51c347 4161
9291a0cd
TT
4162}
4163
4164static void
4165dw2_expand_all_symtabs (struct objfile *objfile)
4166{
ed2dc618
SM
4167 struct dwarf2_per_objfile *dwarf2_per_objfile
4168 = get_dwarf2_per_objfile (objfile);
b76e467d 4169 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4170 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4171
ed2dc618 4172 for (int i = 0; i < total_units; ++i)
9291a0cd 4173 {
ff4c9fec 4174 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4175
58f0c718
TT
4176 /* We don't want to directly expand a partial CU, because if we
4177 read it with the wrong language, then assertion failures can
4178 be triggered later on. See PR symtab/23010. So, tell
4179 dw2_instantiate_symtab to skip partial CUs -- any important
4180 partial CU will be read via DW_TAG_imported_unit anyway. */
4181 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4182 }
4183}
4184
4185static void
652a8996
JK
4186dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4187 const char *fullname)
9291a0cd 4188{
ed2dc618
SM
4189 struct dwarf2_per_objfile *dwarf2_per_objfile
4190 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4191
4192 /* We don't need to consider type units here.
4193 This is only called for examining code, e.g. expand_line_sal.
4194 There can be an order of magnitude (or more) more type units
4195 than comp units, and we avoid them if we can. */
4196
b76e467d 4197 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4198 {
3d7bb9d9 4199 /* We only need to look at symtabs not already expanded. */
43f3e411 4200 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4201 continue;
4202
b76e467d 4203 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4204 if (file_data == NULL)
9291a0cd
TT
4205 continue;
4206
b76e467d 4207 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4208 {
652a8996
JK
4209 const char *this_fullname = file_data->file_names[j];
4210
4211 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4212 {
58f0c718 4213 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4214 break;
4215 }
4216 }
4217 }
4218}
4219
9291a0cd 4220static void
199b4314
TT
4221dw2_map_matching_symbols
4222 (struct objfile *objfile,
b054970d 4223 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4224 int global,
4225 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4226 symbol_compare_ftype *ordered_compare)
9291a0cd 4227{
40658b94 4228 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4229 current language is Ada for a non-Ada objfile using GNU index. As Ada
4230 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4231}
4232
e1ef7d7a
PA
4233/* Starting from a search name, return the string that finds the upper
4234 bound of all strings that start with SEARCH_NAME in a sorted name
4235 list. Returns the empty string to indicate that the upper bound is
4236 the end of the list. */
4237
4238static std::string
4239make_sort_after_prefix_name (const char *search_name)
4240{
4241 /* When looking to complete "func", we find the upper bound of all
4242 symbols that start with "func" by looking for where we'd insert
4243 the closest string that would follow "func" in lexicographical
4244 order. Usually, that's "func"-with-last-character-incremented,
4245 i.e. "fund". Mind non-ASCII characters, though. Usually those
4246 will be UTF-8 multi-byte sequences, but we can't be certain.
4247 Especially mind the 0xff character, which is a valid character in
4248 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4249 rule out compilers allowing it in identifiers. Note that
4250 conveniently, strcmp/strcasecmp are specified to compare
4251 characters interpreted as unsigned char. So what we do is treat
4252 the whole string as a base 256 number composed of a sequence of
4253 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4254 to 0, and carries 1 to the following more-significant position.
4255 If the very first character in SEARCH_NAME ends up incremented
4256 and carries/overflows, then the upper bound is the end of the
4257 list. The string after the empty string is also the empty
4258 string.
4259
4260 Some examples of this operation:
4261
4262 SEARCH_NAME => "+1" RESULT
4263
4264 "abc" => "abd"
4265 "ab\xff" => "ac"
4266 "\xff" "a" "\xff" => "\xff" "b"
4267 "\xff" => ""
4268 "\xff\xff" => ""
4269 "" => ""
4270
4271 Then, with these symbols for example:
4272
4273 func
4274 func1
4275 fund
4276
4277 completing "func" looks for symbols between "func" and
4278 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4279 which finds "func" and "func1", but not "fund".
4280
4281 And with:
4282
4283 funcÿ (Latin1 'ÿ' [0xff])
4284 funcÿ1
4285 fund
4286
4287 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4288 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4289
4290 And with:
4291
4292 ÿÿ (Latin1 'ÿ' [0xff])
4293 ÿÿ1
4294
4295 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4296 the end of the list.
4297 */
4298 std::string after = search_name;
4299 while (!after.empty () && (unsigned char) after.back () == 0xff)
4300 after.pop_back ();
4301 if (!after.empty ())
4302 after.back () = (unsigned char) after.back () + 1;
4303 return after;
4304}
4305
5c58de74 4306/* See declaration. */
61d96d7e 4307
5c58de74
PA
4308std::pair<std::vector<name_component>::const_iterator,
4309 std::vector<name_component>::const_iterator>
44ed8f3e 4310mapped_index_base::find_name_components_bounds
3b00ef10 4311 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4312{
5c58de74
PA
4313 auto *name_cmp
4314 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4315
3b00ef10
TT
4316 const char *lang_name
4317 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4318
3f563c84
PA
4319 /* Comparison function object for lower_bound that matches against a
4320 given symbol name. */
4321 auto lookup_compare_lower = [&] (const name_component &elem,
4322 const char *name)
4323 {
5c58de74 4324 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4325 const char *elem_name = elem_qualified + elem.name_offset;
4326 return name_cmp (elem_name, name) < 0;
4327 };
4328
4329 /* Comparison function object for upper_bound that matches against a
4330 given symbol name. */
4331 auto lookup_compare_upper = [&] (const char *name,
4332 const name_component &elem)
4333 {
5c58de74 4334 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4335 const char *elem_name = elem_qualified + elem.name_offset;
4336 return name_cmp (name, elem_name) < 0;
4337 };
4338
5c58de74
PA
4339 auto begin = this->name_components.begin ();
4340 auto end = this->name_components.end ();
3f563c84
PA
4341
4342 /* Find the lower bound. */
4343 auto lower = [&] ()
4344 {
3b00ef10 4345 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4346 return begin;
4347 else
3b00ef10 4348 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4349 } ();
4350
4351 /* Find the upper bound. */
4352 auto upper = [&] ()
4353 {
5c58de74 4354 if (lookup_name_without_params.completion_mode ())
3f563c84 4355 {
e1ef7d7a
PA
4356 /* In completion mode, we want UPPER to point past all
4357 symbols names that have the same prefix. I.e., with
4358 these symbols, and completing "func":
4359
4360 function << lower bound
4361 function1
4362 other_function << upper bound
4363
4364 We find the upper bound by looking for the insertion
4365 point of "func"-with-last-character-incremented,
4366 i.e. "fund". */
3b00ef10 4367 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4368 if (after.empty ())
3f563c84 4369 return end;
e6b2f5ef
PA
4370 return std::lower_bound (lower, end, after.c_str (),
4371 lookup_compare_lower);
3f563c84
PA
4372 }
4373 else
3b00ef10 4374 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4375 } ();
4376
5c58de74
PA
4377 return {lower, upper};
4378}
4379
4380/* See declaration. */
4381
4382void
44ed8f3e 4383mapped_index_base::build_name_components ()
5c58de74
PA
4384{
4385 if (!this->name_components.empty ())
4386 return;
4387
4388 this->name_components_casing = case_sensitivity;
4389 auto *name_cmp
4390 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4391
4392 /* The code below only knows how to break apart components of C++
4393 symbol names (and other languages that use '::' as
3b00ef10 4394 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4395 auto count = this->symbol_name_count ();
4396 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4397 {
44ed8f3e 4398 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4399 continue;
4400
4401 const char *name = this->symbol_name_at (idx);
4402
4403 /* Add each name component to the name component table. */
4404 unsigned int previous_len = 0;
3b00ef10
TT
4405
4406 if (strstr (name, "::") != nullptr)
4407 {
4408 for (unsigned int current_len = cp_find_first_component (name);
4409 name[current_len] != '\0';
4410 current_len += cp_find_first_component (name + current_len))
4411 {
4412 gdb_assert (name[current_len] == ':');
4413 this->name_components.push_back ({previous_len, idx});
4414 /* Skip the '::'. */
4415 current_len += 2;
4416 previous_len = current_len;
4417 }
4418 }
4419 else
5c58de74 4420 {
3b00ef10
TT
4421 /* Handle the Ada encoded (aka mangled) form here. */
4422 for (const char *iter = strstr (name, "__");
4423 iter != nullptr;
4424 iter = strstr (iter, "__"))
4425 {
4426 this->name_components.push_back ({previous_len, idx});
4427 iter += 2;
4428 previous_len = iter - name;
4429 }
5c58de74 4430 }
3b00ef10 4431
5c58de74
PA
4432 this->name_components.push_back ({previous_len, idx});
4433 }
4434
4435 /* Sort name_components elements by name. */
4436 auto name_comp_compare = [&] (const name_component &left,
4437 const name_component &right)
4438 {
4439 const char *left_qualified = this->symbol_name_at (left.idx);
4440 const char *right_qualified = this->symbol_name_at (right.idx);
4441
4442 const char *left_name = left_qualified + left.name_offset;
4443 const char *right_name = right_qualified + right.name_offset;
4444
4445 return name_cmp (left_name, right_name) < 0;
4446 };
4447
4448 std::sort (this->name_components.begin (),
4449 this->name_components.end (),
4450 name_comp_compare);
4451}
4452
4453/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4454 mapped_index_base instead of the containing objfile. This is split
4455 to a separate function in order to be able to unit test the
4456 name_components matching using a mock mapped_index_base. For each
5c58de74 4457 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4458 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4459
4460static void
4461dw2_expand_symtabs_matching_symbol
44ed8f3e 4462 (mapped_index_base &index,
5c58de74
PA
4463 const lookup_name_info &lookup_name_in,
4464 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4465 enum search_domain kind,
3b00ef10 4466 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4467{
4468 lookup_name_info lookup_name_without_params
4469 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4470
4471 /* Build the symbol name component sorted vector, if we haven't
4472 yet. */
4473 index.build_name_components ();
4474
3f563c84
PA
4475 /* The same symbol may appear more than once in the range though.
4476 E.g., if we're looking for symbols that complete "w", and we have
4477 a symbol named "w1::w2", we'll find the two name components for
4478 that same symbol in the range. To be sure we only call the
4479 callback once per symbol, we first collect the symbol name
4480 indexes that matched in a temporary vector and ignore
4481 duplicates. */
4482 std::vector<offset_type> matches;
3f563c84 4483
3b00ef10
TT
4484 struct name_and_matcher
4485 {
4486 symbol_name_matcher_ftype *matcher;
4487 const std::string &name;
4488
4489 bool operator== (const name_and_matcher &other) const
3f563c84 4490 {
3b00ef10
TT
4491 return matcher == other.matcher && name == other.name;
4492 }
4493 };
4494
4495 /* A vector holding all the different symbol name matchers, for all
4496 languages. */
4497 std::vector<name_and_matcher> matchers;
4498
4499 for (int i = 0; i < nr_languages; i++)
4500 {
4501 enum language lang_e = (enum language) i;
4502
4503 const language_defn *lang = language_def (lang_e);
4504 symbol_name_matcher_ftype *name_matcher
4505 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4506
3b00ef10
TT
4507 name_and_matcher key {
4508 name_matcher,
4509 lookup_name_without_params.language_lookup_name (lang_e)
4510 };
4511
4512 /* Don't insert the same comparison routine more than once.
4513 Note that we do this linear walk. This is not a problem in
4514 practice because the number of supported languages is
4515 low. */
4516 if (std::find (matchers.begin (), matchers.end (), key)
4517 != matchers.end ())
9291a0cd 4518 continue;
3b00ef10
TT
4519 matchers.push_back (std::move (key));
4520
4521 auto bounds
4522 = index.find_name_components_bounds (lookup_name_without_params,
4523 lang_e);
4524
4525 /* Now for each symbol name in range, check to see if we have a name
4526 match, and if so, call the MATCH_CALLBACK callback. */
4527
4528 for (; bounds.first != bounds.second; ++bounds.first)
4529 {
4530 const char *qualified = index.symbol_name_at (bounds.first->idx);
4531
4532 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4533 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4534 continue;
9291a0cd 4535
3b00ef10
TT
4536 matches.push_back (bounds.first->idx);
4537 }
3f563c84
PA
4538 }
4539
4540 std::sort (matches.begin (), matches.end ());
4541
4542 /* Finally call the callback, once per match. */
4543 ULONGEST prev = -1;
4544 for (offset_type idx : matches)
4545 {
4546 if (prev != idx)
4547 {
3b00ef10
TT
4548 if (!match_callback (idx))
4549 break;
3f563c84
PA
4550 prev = idx;
4551 }
4552 }
4553
4554 /* Above we use a type wider than idx's for 'prev', since 0 and
4555 (offset_type)-1 are both possible values. */
4556 static_assert (sizeof (prev) > sizeof (offset_type), "");
4557}
4558
c62446b1
PA
4559#if GDB_SELF_TEST
4560
4561namespace selftests { namespace dw2_expand_symtabs_matching {
4562
a3c5fafd
PA
4563/* A mock .gdb_index/.debug_names-like name index table, enough to
4564 exercise dw2_expand_symtabs_matching_symbol, which works with the
4565 mapped_index_base interface. Builds an index from the symbol list
4566 passed as parameter to the constructor. */
4567class mock_mapped_index : public mapped_index_base
c62446b1
PA
4568{
4569public:
a3c5fafd
PA
4570 mock_mapped_index (gdb::array_view<const char *> symbols)
4571 : m_symbol_table (symbols)
c62446b1
PA
4572 {}
4573
a3c5fafd 4574 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4575
a3c5fafd 4576 /* Return the number of names in the symbol table. */
632e107b 4577 size_t symbol_name_count () const override
c62446b1 4578 {
a3c5fafd 4579 return m_symbol_table.size ();
c62446b1
PA
4580 }
4581
a3c5fafd 4582 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4583 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4584 {
4585 return m_symbol_table[idx];
4586 }
c62446b1 4587
a3c5fafd
PA
4588private:
4589 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4590};
4591
4592/* Convenience function that converts a NULL pointer to a "<null>"
4593 string, to pass to print routines. */
4594
4595static const char *
4596string_or_null (const char *str)
4597{
4598 return str != NULL ? str : "<null>";
4599}
4600
4601/* Check if a lookup_name_info built from
4602 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4603 index. EXPECTED_LIST is the list of expected matches, in expected
4604 matching order. If no match expected, then an empty list is
4605 specified. Returns true on success. On failure prints a warning
4606 indicating the file:line that failed, and returns false. */
4607
4608static bool
4609check_match (const char *file, int line,
4610 mock_mapped_index &mock_index,
4611 const char *name, symbol_name_match_type match_type,
4612 bool completion_mode,
4613 std::initializer_list<const char *> expected_list)
4614{
4615 lookup_name_info lookup_name (name, match_type, completion_mode);
4616
4617 bool matched = true;
4618
4619 auto mismatch = [&] (const char *expected_str,
4620 const char *got)
4621 {
4622 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4623 "expected=\"%s\", got=\"%s\"\n"),
4624 file, line,
4625 (match_type == symbol_name_match_type::FULL
4626 ? "FULL" : "WILD"),
4627 name, string_or_null (expected_str), string_or_null (got));
4628 matched = false;
4629 };
4630
4631 auto expected_it = expected_list.begin ();
4632 auto expected_end = expected_list.end ();
4633
a3c5fafd 4634 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4635 NULL, ALL_DOMAIN,
4636 [&] (offset_type idx)
4637 {
a3c5fafd 4638 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4639 const char *expected_str
4640 = expected_it == expected_end ? NULL : *expected_it++;
4641
4642 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4643 mismatch (expected_str, matched_name);
3b00ef10 4644 return true;
c62446b1
PA
4645 });
4646
4647 const char *expected_str
4648 = expected_it == expected_end ? NULL : *expected_it++;
4649 if (expected_str != NULL)
4650 mismatch (expected_str, NULL);
4651
4652 return matched;
4653}
4654
4655/* The symbols added to the mock mapped_index for testing (in
4656 canonical form). */
4657static const char *test_symbols[] = {
4658 "function",
4659 "std::bar",
4660 "std::zfunction",
4661 "std::zfunction2",
4662 "w1::w2",
4663 "ns::foo<char*>",
4664 "ns::foo<int>",
4665 "ns::foo<long>",
a20714ff
PA
4666 "ns2::tmpl<int>::foo2",
4667 "(anonymous namespace)::A::B::C",
c62446b1 4668
e1ef7d7a
PA
4669 /* These are used to check that the increment-last-char in the
4670 matching algorithm for completion doesn't match "t1_fund" when
4671 completing "t1_func". */
4672 "t1_func",
4673 "t1_func1",
4674 "t1_fund",
4675 "t1_fund1",
4676
4677 /* A UTF-8 name with multi-byte sequences to make sure that
4678 cp-name-parser understands this as a single identifier ("função"
4679 is "function" in PT). */
4680 u8"u8função",
4681
4682 /* \377 (0xff) is Latin1 'ÿ'. */
4683 "yfunc\377",
4684
4685 /* \377 (0xff) is Latin1 'ÿ'. */
4686 "\377",
4687 "\377\377123",
4688
c62446b1
PA
4689 /* A name with all sorts of complications. Starts with "z" to make
4690 it easier for the completion tests below. */
4691#define Z_SYM_NAME \
4692 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4693 "::tuple<(anonymous namespace)::ui*, " \
4694 "std::default_delete<(anonymous namespace)::ui>, void>"
4695
4696 Z_SYM_NAME
4697};
4698
a3c5fafd
PA
4699/* Returns true if the mapped_index_base::find_name_component_bounds
4700 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4701 in completion mode. */
5c58de74
PA
4702
4703static bool
a3c5fafd 4704check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4705 const char *search_name,
4706 gdb::array_view<const char *> expected_syms)
4707{
4708 lookup_name_info lookup_name (search_name,
4709 symbol_name_match_type::FULL, true);
4710
3b00ef10
TT
4711 auto bounds = index.find_name_components_bounds (lookup_name,
4712 language_cplus);
5c58de74
PA
4713
4714 size_t distance = std::distance (bounds.first, bounds.second);
4715 if (distance != expected_syms.size ())
4716 return false;
4717
4718 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4719 {
4720 auto nc_elem = bounds.first + exp_elem;
4721 const char *qualified = index.symbol_name_at (nc_elem->idx);
4722 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4723 return false;
4724 }
4725
4726 return true;
4727}
4728
4729/* Test the lower-level mapped_index::find_name_component_bounds
4730 method. */
4731
c62446b1 4732static void
5c58de74
PA
4733test_mapped_index_find_name_component_bounds ()
4734{
4735 mock_mapped_index mock_index (test_symbols);
4736
a3c5fafd 4737 mock_index.build_name_components ();
5c58de74
PA
4738
4739 /* Test the lower-level mapped_index::find_name_component_bounds
4740 method in completion mode. */
4741 {
4742 static const char *expected_syms[] = {
4743 "t1_func",
4744 "t1_func1",
5c58de74
PA
4745 };
4746
a3c5fafd 4747 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4748 "t1_func", expected_syms));
4749 }
4750
4751 /* Check that the increment-last-char in the name matching algorithm
4752 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4753 {
4754 static const char *expected_syms1[] = {
4755 "\377",
4756 "\377\377123",
4757 };
a3c5fafd 4758 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4759 "\377", expected_syms1));
4760
4761 static const char *expected_syms2[] = {
4762 "\377\377123",
4763 };
a3c5fafd 4764 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4765 "\377\377", expected_syms2));
4766 }
4767}
4768
4769/* Test dw2_expand_symtabs_matching_symbol. */
4770
4771static void
4772test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4773{
4774 mock_mapped_index mock_index (test_symbols);
4775
4776 /* We let all tests run until the end even if some fails, for debug
4777 convenience. */
4778 bool any_mismatch = false;
4779
4780 /* Create the expected symbols list (an initializer_list). Needed
4781 because lists have commas, and we need to pass them to CHECK,
4782 which is a macro. */
4783#define EXPECT(...) { __VA_ARGS__ }
4784
4785 /* Wrapper for check_match that passes down the current
4786 __FILE__/__LINE__. */
4787#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4788 any_mismatch |= !check_match (__FILE__, __LINE__, \
4789 mock_index, \
4790 NAME, MATCH_TYPE, COMPLETION_MODE, \
4791 EXPECTED_LIST)
4792
4793 /* Identity checks. */
4794 for (const char *sym : test_symbols)
4795 {
4796 /* Should be able to match all existing symbols. */
4797 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4798 EXPECT (sym));
4799
4800 /* Should be able to match all existing symbols with
4801 parameters. */
4802 std::string with_params = std::string (sym) + "(int)";
4803 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4804 EXPECT (sym));
4805
4806 /* Should be able to match all existing symbols with
4807 parameters and qualifiers. */
4808 with_params = std::string (sym) + " ( int ) const";
4809 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4810 EXPECT (sym));
4811
4812 /* This should really find sym, but cp-name-parser.y doesn't
4813 know about lvalue/rvalue qualifiers yet. */
4814 with_params = std::string (sym) + " ( int ) &&";
4815 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4816 {});
4817 }
4818
e1ef7d7a
PA
4819 /* Check that the name matching algorithm for completion doesn't get
4820 confused with Latin1 'ÿ' / 0xff. */
4821 {
4822 static const char str[] = "\377";
4823 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4824 EXPECT ("\377", "\377\377123"));
4825 }
4826
4827 /* Check that the increment-last-char in the matching algorithm for
4828 completion doesn't match "t1_fund" when completing "t1_func". */
4829 {
4830 static const char str[] = "t1_func";
4831 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4832 EXPECT ("t1_func", "t1_func1"));
4833 }
4834
c62446b1
PA
4835 /* Check that completion mode works at each prefix of the expected
4836 symbol name. */
4837 {
4838 static const char str[] = "function(int)";
4839 size_t len = strlen (str);
4840 std::string lookup;
4841
4842 for (size_t i = 1; i < len; i++)
4843 {
4844 lookup.assign (str, i);
4845 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4846 EXPECT ("function"));
4847 }
4848 }
4849
4850 /* While "w" is a prefix of both components, the match function
4851 should still only be called once. */
4852 {
4853 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4854 EXPECT ("w1::w2"));
a20714ff
PA
4855 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4856 EXPECT ("w1::w2"));
c62446b1
PA
4857 }
4858
4859 /* Same, with a "complicated" symbol. */
4860 {
4861 static const char str[] = Z_SYM_NAME;
4862 size_t len = strlen (str);
4863 std::string lookup;
4864
4865 for (size_t i = 1; i < len; i++)
4866 {
4867 lookup.assign (str, i);
4868 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4869 EXPECT (Z_SYM_NAME));
4870 }
4871 }
4872
4873 /* In FULL mode, an incomplete symbol doesn't match. */
4874 {
4875 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4876 {});
4877 }
4878
4879 /* A complete symbol with parameters matches any overload, since the
4880 index has no overload info. */
4881 {
4882 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4883 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4884 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4885 EXPECT ("std::zfunction", "std::zfunction2"));
4886 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4887 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4888 }
4889
4890 /* Check that whitespace is ignored appropriately. A symbol with a
4891 template argument list. */
4892 {
4893 static const char expected[] = "ns::foo<int>";
4894 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4895 EXPECT (expected));
a20714ff
PA
4896 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4897 EXPECT (expected));
c62446b1
PA
4898 }
4899
4900 /* Check that whitespace is ignored appropriately. A symbol with a
4901 template argument list that includes a pointer. */
4902 {
4903 static const char expected[] = "ns::foo<char*>";
4904 /* Try both completion and non-completion modes. */
4905 static const bool completion_mode[2] = {false, true};
4906 for (size_t i = 0; i < 2; i++)
4907 {
4908 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4909 completion_mode[i], EXPECT (expected));
a20714ff
PA
4910 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4911 completion_mode[i], EXPECT (expected));
c62446b1
PA
4912
4913 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4914 completion_mode[i], EXPECT (expected));
a20714ff
PA
4915 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4916 completion_mode[i], EXPECT (expected));
c62446b1
PA
4917 }
4918 }
4919
4920 {
4921 /* Check method qualifiers are ignored. */
4922 static const char expected[] = "ns::foo<char*>";
4923 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4924 symbol_name_match_type::FULL, true, EXPECT (expected));
4925 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4926 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4927 CHECK_MATCH ("foo < char * > ( int ) const",
4928 symbol_name_match_type::WILD, true, EXPECT (expected));
4929 CHECK_MATCH ("foo < char * > ( int ) &&",
4930 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4931 }
4932
4933 /* Test lookup names that don't match anything. */
4934 {
a20714ff
PA
4935 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4936 {});
4937
c62446b1
PA
4938 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4939 {});
4940 }
4941
a20714ff
PA
4942 /* Some wild matching tests, exercising "(anonymous namespace)",
4943 which should not be confused with a parameter list. */
4944 {
4945 static const char *syms[] = {
4946 "A::B::C",
4947 "B::C",
4948 "C",
4949 "A :: B :: C ( int )",
4950 "B :: C ( int )",
4951 "C ( int )",
4952 };
4953
4954 for (const char *s : syms)
4955 {
4956 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4957 EXPECT ("(anonymous namespace)::A::B::C"));
4958 }
4959 }
4960
4961 {
4962 static const char expected[] = "ns2::tmpl<int>::foo2";
4963 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4964 EXPECT (expected));
4965 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4966 EXPECT (expected));
4967 }
4968
c62446b1
PA
4969 SELF_CHECK (!any_mismatch);
4970
4971#undef EXPECT
4972#undef CHECK_MATCH
4973}
4974
5c58de74
PA
4975static void
4976run_test ()
4977{
4978 test_mapped_index_find_name_component_bounds ();
4979 test_dw2_expand_symtabs_matching_symbol ();
4980}
4981
c62446b1
PA
4982}} // namespace selftests::dw2_expand_symtabs_matching
4983
4984#endif /* GDB_SELF_TEST */
4985
4b514bc8
JK
4986/* If FILE_MATCHER is NULL or if PER_CU has
4987 dwarf2_per_cu_quick_data::MARK set (see
4988 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4989 EXPANSION_NOTIFY on it. */
4990
4991static void
4992dw2_expand_symtabs_matching_one
4993 (struct dwarf2_per_cu_data *per_cu,
4994 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4995 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4996{
4997 if (file_matcher == NULL || per_cu->v.quick->mark)
4998 {
4999 bool symtab_was_null
5000 = (per_cu->v.quick->compunit_symtab == NULL);
5001
58f0c718 5002 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5003
5004 if (expansion_notify != NULL
5005 && symtab_was_null
5006 && per_cu->v.quick->compunit_symtab != NULL)
5007 expansion_notify (per_cu->v.quick->compunit_symtab);
5008 }
5009}
5010
3f563c84
PA
5011/* Helper for dw2_expand_matching symtabs. Called on each symbol
5012 matched, to expand corresponding CUs that were marked. IDX is the
5013 index of the symbol name that matched. */
5014
5015static void
5016dw2_expand_marked_cus
ed2dc618 5017 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5018 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5019 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5020 search_domain kind)
5021{
3f563c84
PA
5022 offset_type *vec, vec_len, vec_idx;
5023 bool global_seen = false;
ed2dc618 5024 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5025
61920122 5026 vec = (offset_type *) (index.constant_pool
f00a2de2 5027 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5028 vec_len = MAYBE_SWAP (vec[0]);
5029 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5030 {
61920122
PA
5031 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5032 /* This value is only valid for index versions >= 7. */
5033 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5034 gdb_index_symbol_kind symbol_kind =
5035 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5036 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5037 /* Only check the symbol attributes if they're present.
5038 Indices prior to version 7 don't record them,
5039 and indices >= 7 may elide them for certain symbols
5040 (gold does this). */
5041 int attrs_valid =
5042 (index.version >= 7
5043 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5044
5045 /* Work around gold/15646. */
5046 if (attrs_valid)
9291a0cd 5047 {
61920122
PA
5048 if (!is_static && global_seen)
5049 continue;
5050 if (!is_static)
5051 global_seen = true;
5052 }
3190f0c6 5053
61920122
PA
5054 /* Only check the symbol's kind if it has one. */
5055 if (attrs_valid)
5056 {
5057 switch (kind)
8943b874 5058 {
61920122
PA
5059 case VARIABLES_DOMAIN:
5060 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5061 continue;
5062 break;
5063 case FUNCTIONS_DOMAIN:
5064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5065 continue;
61920122
PA
5066 break;
5067 case TYPES_DOMAIN:
5068 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5069 continue;
5070 break;
59c35742
AB
5071 case MODULES_DOMAIN:
5072 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5073 continue;
5074 break;
61920122
PA
5075 default:
5076 break;
8943b874 5077 }
61920122 5078 }
8943b874 5079
61920122 5080 /* Don't crash on bad data. */
b76e467d 5081 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5082 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5083 {
b98664d3 5084 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5085 " [in module %s]"),
5086 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5087 continue;
5088 }
5089
ff4c9fec 5090 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5091 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5092 expansion_notify);
61920122
PA
5093 }
5094}
5095
4b514bc8
JK
5096/* If FILE_MATCHER is non-NULL, set all the
5097 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5098 that match FILE_MATCHER. */
5099
61920122 5100static void
4b514bc8 5101dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5102 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5103 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5104{
4b514bc8 5105 if (file_matcher == NULL)
61920122
PA
5106 return;
5107
4b514bc8
JK
5108 objfile *const objfile = dwarf2_per_objfile->objfile;
5109
5110 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5111 htab_eq_pointer,
5112 NULL, xcalloc, xfree));
5113 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5114 htab_eq_pointer,
5115 NULL, xcalloc, xfree));
61920122 5116
4b514bc8
JK
5117 /* The rule is CUs specify all the files, including those used by
5118 any TU, so there's no need to scan TUs here. */
61920122 5119
b76e467d 5120 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5121 {
927aa2e7
JK
5122 QUIT;
5123
5124 per_cu->v.quick->mark = 0;
5125
5126 /* We only need to look at symtabs not already expanded. */
5127 if (per_cu->v.quick->compunit_symtab)
5128 continue;
5129
b76e467d 5130 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5131 if (file_data == NULL)
5132 continue;
5133
5134 if (htab_find (visited_not_found.get (), file_data) != NULL)
5135 continue;
5136 else if (htab_find (visited_found.get (), file_data) != NULL)
5137 {
5138 per_cu->v.quick->mark = 1;
5139 continue;
5140 }
5141
b76e467d 5142 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5143 {
5144 const char *this_real_name;
5145
5146 if (file_matcher (file_data->file_names[j], false))
5147 {
5148 per_cu->v.quick->mark = 1;
5149 break;
5150 }
5151
5152 /* Before we invoke realpath, which can get expensive when many
5153 files are involved, do a quick comparison of the basenames. */
5154 if (!basenames_may_differ
5155 && !file_matcher (lbasename (file_data->file_names[j]),
5156 true))
5157 continue;
5158
5159 this_real_name = dw2_get_real_path (objfile, file_data, j);
5160 if (file_matcher (this_real_name, false))
5161 {
5162 per_cu->v.quick->mark = 1;
5163 break;
5164 }
5165 }
5166
b76e467d
SM
5167 void **slot = htab_find_slot (per_cu->v.quick->mark
5168 ? visited_found.get ()
5169 : visited_not_found.get (),
5170 file_data, INSERT);
927aa2e7
JK
5171 *slot = file_data;
5172 }
5173}
5174
5175static void
5176dw2_expand_symtabs_matching
5177 (struct objfile *objfile,
5178 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5179 const lookup_name_info &lookup_name,
5180 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5181 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5182 enum search_domain kind)
5183{
ed2dc618
SM
5184 struct dwarf2_per_objfile *dwarf2_per_objfile
5185 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5186
5187 /* index_table is NULL if OBJF_READNOW. */
5188 if (!dwarf2_per_objfile->index_table)
5189 return;
5190
ed2dc618 5191 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5192
5193 mapped_index &index = *dwarf2_per_objfile->index_table;
5194
5195 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5196 symbol_matcher,
5197 kind, [&] (offset_type idx)
5198 {
ed2dc618 5199 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5200 expansion_notify, kind);
3b00ef10 5201 return true;
927aa2e7
JK
5202 });
5203}
5204
5205/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5206 symtab. */
5207
5208static struct compunit_symtab *
5209recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5210 CORE_ADDR pc)
5211{
5212 int i;
5213
5214 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5215 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5216 return cust;
5217
5218 if (cust->includes == NULL)
5219 return NULL;
5220
5221 for (i = 0; cust->includes[i]; ++i)
5222 {
5223 struct compunit_symtab *s = cust->includes[i];
5224
5225 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5226 if (s != NULL)
5227 return s;
5228 }
5229
5230 return NULL;
5231}
5232
5233static struct compunit_symtab *
5234dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5235 struct bound_minimal_symbol msymbol,
5236 CORE_ADDR pc,
5237 struct obj_section *section,
5238 int warn_if_readin)
5239{
5240 struct dwarf2_per_cu_data *data;
5241 struct compunit_symtab *result;
5242
d320c2b5 5243 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5244 return NULL;
5245
79748972
TT
5246 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5247 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5248 data = (struct dwarf2_per_cu_data *) addrmap_find
5249 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5250 if (!data)
5251 return NULL;
5252
5253 if (warn_if_readin && data->v.quick->compunit_symtab)
5254 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5255 paddress (get_objfile_arch (objfile), pc));
5256
5257 result
58f0c718
TT
5258 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5259 false),
927aa2e7
JK
5260 pc);
5261 gdb_assert (result != NULL);
5262 return result;
5263}
5264
5265static void
5266dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5267 void *data, int need_fullname)
5268{
ed2dc618
SM
5269 struct dwarf2_per_objfile *dwarf2_per_objfile
5270 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5271
5272 if (!dwarf2_per_objfile->filenames_cache)
5273 {
5274 dwarf2_per_objfile->filenames_cache.emplace ();
5275
5276 htab_up visited (htab_create_alloc (10,
5277 htab_hash_pointer, htab_eq_pointer,
5278 NULL, xcalloc, xfree));
5279
5280 /* The rule is CUs specify all the files, including those used
5281 by any TU, so there's no need to scan TUs here. We can
5282 ignore file names coming from already-expanded CUs. */
5283
b76e467d 5284 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5285 {
927aa2e7
JK
5286 if (per_cu->v.quick->compunit_symtab)
5287 {
5288 void **slot = htab_find_slot (visited.get (),
5289 per_cu->v.quick->file_names,
5290 INSERT);
5291
5292 *slot = per_cu->v.quick->file_names;
5293 }
5294 }
5295
b76e467d 5296 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5297 {
927aa2e7
JK
5298 /* We only need to look at symtabs not already expanded. */
5299 if (per_cu->v.quick->compunit_symtab)
5300 continue;
5301
b76e467d 5302 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5303 if (file_data == NULL)
5304 continue;
5305
b76e467d 5306 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5307 if (*slot)
5308 {
5309 /* Already visited. */
5310 continue;
5311 }
5312 *slot = file_data;
5313
5314 for (int j = 0; j < file_data->num_file_names; ++j)
5315 {
5316 const char *filename = file_data->file_names[j];
5317 dwarf2_per_objfile->filenames_cache->seen (filename);
5318 }
5319 }
5320 }
5321
5322 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5323 {
5324 gdb::unique_xmalloc_ptr<char> this_real_name;
5325
5326 if (need_fullname)
5327 this_real_name = gdb_realpath (filename);
5328 (*fun) (filename, this_real_name.get (), data);
5329 });
5330}
5331
5332static int
5333dw2_has_symbols (struct objfile *objfile)
5334{
5335 return 1;
5336}
5337
5338const struct quick_symbol_functions dwarf2_gdb_index_functions =
5339{
5340 dw2_has_symbols,
5341 dw2_find_last_source_symtab,
5342 dw2_forget_cached_source_info,
5343 dw2_map_symtabs_matching_filename,
5344 dw2_lookup_symbol,
5345 dw2_print_stats,
5346 dw2_dump,
927aa2e7
JK
5347 dw2_expand_symtabs_for_function,
5348 dw2_expand_all_symtabs,
5349 dw2_expand_symtabs_with_fullname,
5350 dw2_map_matching_symbols,
5351 dw2_expand_symtabs_matching,
5352 dw2_find_pc_sect_compunit_symtab,
5353 NULL,
5354 dw2_map_symbol_filenames
5355};
5356
5357/* DWARF-5 debug_names reader. */
5358
5359/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5360static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5361
5362/* A helper function that reads the .debug_names section in SECTION
5363 and fills in MAP. FILENAME is the name of the file containing the
5364 section; it is used for error reporting.
5365
5366 Returns true if all went well, false otherwise. */
5367
5368static bool
5369read_debug_names_from_section (struct objfile *objfile,
5370 const char *filename,
5371 struct dwarf2_section_info *section,
5372 mapped_debug_names &map)
5373{
5374 if (dwarf2_section_empty_p (section))
5375 return false;
5376
5377 /* Older elfutils strip versions could keep the section in the main
5378 executable while splitting it for the separate debug info file. */
5379 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5380 return false;
5381
5382 dwarf2_read_section (objfile, section);
5383
5384 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5385
5386 const gdb_byte *addr = section->buffer;
5387
5388 bfd *const abfd = get_section_bfd_owner (section);
5389
5390 unsigned int bytes_read;
5391 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5392 addr += bytes_read;
5393
5394 map.dwarf5_is_dwarf64 = bytes_read != 4;
5395 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5396 if (bytes_read + length != section->size)
5397 {
5398 /* There may be multiple per-CU indices. */
5399 warning (_("Section .debug_names in %s length %s does not match "
5400 "section length %s, ignoring .debug_names."),
5401 filename, plongest (bytes_read + length),
5402 pulongest (section->size));
5403 return false;
5404 }
5405
5406 /* The version number. */
5407 uint16_t version = read_2_bytes (abfd, addr);
5408 addr += 2;
5409 if (version != 5)
5410 {
5411 warning (_("Section .debug_names in %s has unsupported version %d, "
5412 "ignoring .debug_names."),
5413 filename, version);
5414 return false;
5415 }
5416
5417 /* Padding. */
5418 uint16_t padding = read_2_bytes (abfd, addr);
5419 addr += 2;
5420 if (padding != 0)
5421 {
5422 warning (_("Section .debug_names in %s has unsupported padding %d, "
5423 "ignoring .debug_names."),
5424 filename, padding);
5425 return false;
5426 }
5427
5428 /* comp_unit_count - The number of CUs in the CU list. */
5429 map.cu_count = read_4_bytes (abfd, addr);
5430 addr += 4;
5431
5432 /* local_type_unit_count - The number of TUs in the local TU
5433 list. */
5434 map.tu_count = read_4_bytes (abfd, addr);
5435 addr += 4;
5436
5437 /* foreign_type_unit_count - The number of TUs in the foreign TU
5438 list. */
5439 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5440 addr += 4;
5441 if (foreign_tu_count != 0)
5442 {
5443 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5444 "ignoring .debug_names."),
5445 filename, static_cast<unsigned long> (foreign_tu_count));
5446 return false;
5447 }
5448
5449 /* bucket_count - The number of hash buckets in the hash lookup
5450 table. */
5451 map.bucket_count = read_4_bytes (abfd, addr);
5452 addr += 4;
5453
5454 /* name_count - The number of unique names in the index. */
5455 map.name_count = read_4_bytes (abfd, addr);
5456 addr += 4;
5457
5458 /* abbrev_table_size - The size in bytes of the abbreviations
5459 table. */
5460 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5461 addr += 4;
5462
5463 /* augmentation_string_size - The size in bytes of the augmentation
5464 string. This value is rounded up to a multiple of 4. */
5465 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5466 addr += 4;
5467 map.augmentation_is_gdb = ((augmentation_string_size
5468 == sizeof (dwarf5_augmentation))
5469 && memcmp (addr, dwarf5_augmentation,
5470 sizeof (dwarf5_augmentation)) == 0);
5471 augmentation_string_size += (-augmentation_string_size) & 3;
5472 addr += augmentation_string_size;
5473
5474 /* List of CUs */
5475 map.cu_table_reordered = addr;
5476 addr += map.cu_count * map.offset_size;
5477
5478 /* List of Local TUs */
5479 map.tu_table_reordered = addr;
5480 addr += map.tu_count * map.offset_size;
5481
5482 /* Hash Lookup Table */
5483 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5484 addr += map.bucket_count * 4;
5485 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5486 addr += map.name_count * 4;
5487
5488 /* Name Table */
5489 map.name_table_string_offs_reordered = addr;
5490 addr += map.name_count * map.offset_size;
5491 map.name_table_entry_offs_reordered = addr;
5492 addr += map.name_count * map.offset_size;
5493
5494 const gdb_byte *abbrev_table_start = addr;
5495 for (;;)
5496 {
927aa2e7
JK
5497 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5498 addr += bytes_read;
5499 if (index_num == 0)
5500 break;
5501
5502 const auto insertpair
5503 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5504 if (!insertpair.second)
5505 {
5506 warning (_("Section .debug_names in %s has duplicate index %s, "
5507 "ignoring .debug_names."),
5508 filename, pulongest (index_num));
5509 return false;
5510 }
5511 mapped_debug_names::index_val &indexval = insertpair.first->second;
5512 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5513 addr += bytes_read;
5514
5515 for (;;)
5516 {
5517 mapped_debug_names::index_val::attr attr;
5518 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5519 addr += bytes_read;
5520 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5521 addr += bytes_read;
5522 if (attr.form == DW_FORM_implicit_const)
5523 {
5524 attr.implicit_const = read_signed_leb128 (abfd, addr,
5525 &bytes_read);
5526 addr += bytes_read;
5527 }
5528 if (attr.dw_idx == 0 && attr.form == 0)
5529 break;
5530 indexval.attr_vec.push_back (std::move (attr));
5531 }
5532 }
5533 if (addr != abbrev_table_start + abbrev_table_size)
5534 {
5535 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5536 "of size %s vs. written as %u, ignoring .debug_names."),
5537 filename, plongest (addr - abbrev_table_start),
5538 abbrev_table_size);
927aa2e7
JK
5539 return false;
5540 }
5541 map.entry_pool = addr;
5542
5543 return true;
5544}
5545
5546/* A helper for create_cus_from_debug_names that handles the MAP's CU
5547 list. */
5548
5549static void
ed2dc618 5550create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5551 const mapped_debug_names &map,
5552 dwarf2_section_info &section,
b76e467d 5553 bool is_dwz)
927aa2e7
JK
5554{
5555 sect_offset sect_off_prev;
5556 for (uint32_t i = 0; i <= map.cu_count; ++i)
5557 {
5558 sect_offset sect_off_next;
5559 if (i < map.cu_count)
5560 {
5561 sect_off_next
5562 = (sect_offset) (extract_unsigned_integer
5563 (map.cu_table_reordered + i * map.offset_size,
5564 map.offset_size,
5565 map.dwarf5_byte_order));
5566 }
5567 else
5568 sect_off_next = (sect_offset) section.size;
5569 if (i >= 1)
5570 {
5571 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5572 dwarf2_per_cu_data *per_cu
ed2dc618 5573 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5574 sect_off_prev, length);
b76e467d 5575 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5576 }
5577 sect_off_prev = sect_off_next;
5578 }
5579}
5580
5581/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5582 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5583
5584static void
ed2dc618 5585create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5586 const mapped_debug_names &map,
5587 const mapped_debug_names &dwz_map)
5588{
b76e467d
SM
5589 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5590 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5591
ed2dc618
SM
5592 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5593 dwarf2_per_objfile->info,
b76e467d 5594 false /* is_dwz */);
927aa2e7
JK
5595
5596 if (dwz_map.cu_count == 0)
5597 return;
5598
ed2dc618
SM
5599 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5600 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5601 true /* is_dwz */);
927aa2e7
JK
5602}
5603
5604/* Read .debug_names. If everything went ok, initialize the "quick"
5605 elements of all the CUs and return true. Otherwise, return false. */
5606
5607static bool
ed2dc618 5608dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5609{
22ca247e
TT
5610 std::unique_ptr<mapped_debug_names> map
5611 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5612 mapped_debug_names dwz_map (dwarf2_per_objfile);
5613 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5614
5615 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5616 &dwarf2_per_objfile->debug_names,
22ca247e 5617 *map))
927aa2e7
JK
5618 return false;
5619
5620 /* Don't use the index if it's empty. */
22ca247e 5621 if (map->name_count == 0)
927aa2e7
JK
5622 return false;
5623
5624 /* If there is a .dwz file, read it so we can get its CU list as
5625 well. */
ed2dc618 5626 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5627 if (dwz != NULL)
5628 {
5629 if (!read_debug_names_from_section (objfile,
00f93c44 5630 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5631 &dwz->debug_names, dwz_map))
5632 {
5633 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5634 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5635 return false;
5636 }
5637 }
5638
22ca247e 5639 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5640
22ca247e 5641 if (map->tu_count != 0)
927aa2e7
JK
5642 {
5643 /* We can only handle a single .debug_types when we have an
5644 index. */
fd5866f6 5645 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5646 return false;
5647
fd5866f6 5648 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5649
5650 create_signatured_type_table_from_debug_names
22ca247e 5651 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5652 }
5653
ed2dc618
SM
5654 create_addrmap_from_aranges (dwarf2_per_objfile,
5655 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5656
22ca247e 5657 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5658 dwarf2_per_objfile->using_index = 1;
5659 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5660 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5661
5662 return true;
5663}
5664
927aa2e7
JK
5665/* Type used to manage iterating over all CUs looking for a symbol for
5666 .debug_names. */
5667
5668class dw2_debug_names_iterator
5669{
5670public:
927aa2e7 5671 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5672 gdb::optional<block_enum> block_index,
5673 domain_enum domain,
927aa2e7 5674 const char *name)
2b79f376 5675 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5676 m_addr (find_vec_in_debug_names (map, name))
5677 {}
5678
5679 dw2_debug_names_iterator (const mapped_debug_names &map,
5680 search_domain search, uint32_t namei)
5681 : m_map (map),
5682 m_search (search),
5683 m_addr (find_vec_in_debug_names (map, namei))
5684 {}
5685
3b00ef10
TT
5686 dw2_debug_names_iterator (const mapped_debug_names &map,
5687 block_enum block_index, domain_enum domain,
5688 uint32_t namei)
5689 : m_map (map), m_block_index (block_index), m_domain (domain),
5690 m_addr (find_vec_in_debug_names (map, namei))
5691 {}
5692
927aa2e7
JK
5693 /* Return the next matching CU or NULL if there are no more. */
5694 dwarf2_per_cu_data *next ();
5695
5696private:
5697 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5698 const char *name);
5699 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5700 uint32_t namei);
5701
5702 /* The internalized form of .debug_names. */
5703 const mapped_debug_names &m_map;
5704
2b79f376
SM
5705 /* If set, only look for symbols that match that block. Valid values are
5706 GLOBAL_BLOCK and STATIC_BLOCK. */
5707 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5708
5709 /* The kind of symbol we're looking for. */
5710 const domain_enum m_domain = UNDEF_DOMAIN;
5711 const search_domain m_search = ALL_DOMAIN;
5712
5713 /* The list of CUs from the index entry of the symbol, or NULL if
5714 not found. */
5715 const gdb_byte *m_addr;
5716};
5717
5718const char *
5719mapped_debug_names::namei_to_name (uint32_t namei) const
5720{
5721 const ULONGEST namei_string_offs
5722 = extract_unsigned_integer ((name_table_string_offs_reordered
5723 + namei * offset_size),
5724 offset_size,
5725 dwarf5_byte_order);
5726 return read_indirect_string_at_offset
ed2dc618 5727 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5728}
5729
5730/* Find a slot in .debug_names for the object named NAME. If NAME is
5731 found, return pointer to its pool data. If NAME cannot be found,
5732 return NULL. */
5733
5734const gdb_byte *
5735dw2_debug_names_iterator::find_vec_in_debug_names
5736 (const mapped_debug_names &map, const char *name)
5737{
5738 int (*cmp) (const char *, const char *);
5739
54ee4252 5740 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5741 if (current_language->la_language == language_cplus
5742 || current_language->la_language == language_fortran
5743 || current_language->la_language == language_d)
5744 {
5745 /* NAME is already canonical. Drop any qualifiers as
5746 .debug_names does not contain any. */
5747
5748 if (strchr (name, '(') != NULL)
5749 {
54ee4252 5750 without_params = cp_remove_params (name);
927aa2e7 5751 if (without_params != NULL)
54ee4252 5752 name = without_params.get ();
927aa2e7
JK
5753 }
5754 }
5755
5756 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5757
5758 const uint32_t full_hash = dwarf5_djb_hash (name);
5759 uint32_t namei
5760 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5761 (map.bucket_table_reordered
5762 + (full_hash % map.bucket_count)), 4,
5763 map.dwarf5_byte_order);
5764 if (namei == 0)
5765 return NULL;
5766 --namei;
5767 if (namei >= map.name_count)
5768 {
b98664d3 5769 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5770 "[in module %s]"),
5771 namei, map.name_count,
ed2dc618 5772 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5773 return NULL;
5774 }
5775
5776 for (;;)
5777 {
5778 const uint32_t namei_full_hash
5779 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5780 (map.hash_table_reordered + namei), 4,
5781 map.dwarf5_byte_order);
5782 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5783 return NULL;
5784
5785 if (full_hash == namei_full_hash)
5786 {
5787 const char *const namei_string = map.namei_to_name (namei);
5788
5789#if 0 /* An expensive sanity check. */
5790 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5791 {
b98664d3 5792 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5793 "[in module %s]"),
5794 namei, objfile_name (dwarf2_per_objfile->objfile));
5795 return NULL;
5796 }
5797#endif
5798
5799 if (cmp (namei_string, name) == 0)
5800 {
5801 const ULONGEST namei_entry_offs
5802 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5803 + namei * map.offset_size),
5804 map.offset_size, map.dwarf5_byte_order);
5805 return map.entry_pool + namei_entry_offs;
5806 }
5807 }
5808
5809 ++namei;
5810 if (namei >= map.name_count)
5811 return NULL;
5812 }
5813}
5814
5815const gdb_byte *
5816dw2_debug_names_iterator::find_vec_in_debug_names
5817 (const mapped_debug_names &map, uint32_t namei)
5818{
5819 if (namei >= map.name_count)
5820 {
b98664d3 5821 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5822 "[in module %s]"),
5823 namei, map.name_count,
ed2dc618 5824 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5825 return NULL;
5826 }
5827
5828 const ULONGEST namei_entry_offs
5829 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5830 + namei * map.offset_size),
5831 map.offset_size, map.dwarf5_byte_order);
5832 return map.entry_pool + namei_entry_offs;
5833}
5834
5835/* See dw2_debug_names_iterator. */
5836
5837dwarf2_per_cu_data *
5838dw2_debug_names_iterator::next ()
5839{
5840 if (m_addr == NULL)
5841 return NULL;
5842
ed2dc618
SM
5843 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5844 struct objfile *objfile = dwarf2_per_objfile->objfile;
5845 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5846
5847 again:
5848
5849 unsigned int bytes_read;
5850 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5851 m_addr += bytes_read;
5852 if (abbrev == 0)
5853 return NULL;
5854
5855 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5856 if (indexval_it == m_map.abbrev_map.cend ())
5857 {
b98664d3 5858 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5859 "[in module %s]"),
ed2dc618 5860 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5861 return NULL;
5862 }
5863 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5864 enum class symbol_linkage {
5865 unknown,
5866 static_,
5867 extern_,
23c13d42 5868 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5869 dwarf2_per_cu_data *per_cu = NULL;
5870 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5871 {
5872 ULONGEST ull;
5873 switch (attr.form)
5874 {
5875 case DW_FORM_implicit_const:
5876 ull = attr.implicit_const;
5877 break;
5878 case DW_FORM_flag_present:
5879 ull = 1;
5880 break;
5881 case DW_FORM_udata:
5882 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5883 m_addr += bytes_read;
5884 break;
5885 default:
b98664d3 5886 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5887 dwarf_form_name (attr.form),
ed2dc618 5888 objfile_name (objfile));
927aa2e7
JK
5889 return NULL;
5890 }
5891 switch (attr.dw_idx)
5892 {
5893 case DW_IDX_compile_unit:
5894 /* Don't crash on bad data. */
b76e467d 5895 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5896 {
b98664d3 5897 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5898 " [in module %s]"),
5899 pulongest (ull),
5900 objfile_name (dwarf2_per_objfile->objfile));
5901 continue;
5902 }
ff4c9fec 5903 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5904 break;
8af5c486
JK
5905 case DW_IDX_type_unit:
5906 /* Don't crash on bad data. */
b2bdb8cf 5907 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5908 {
b98664d3 5909 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5910 " [in module %s]"),
5911 pulongest (ull),
5912 objfile_name (dwarf2_per_objfile->objfile));
5913 continue;
5914 }
ff4c9fec 5915 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5916 break;
927aa2e7
JK
5917 case DW_IDX_GNU_internal:
5918 if (!m_map.augmentation_is_gdb)
5919 break;
23c13d42 5920 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5921 break;
5922 case DW_IDX_GNU_external:
5923 if (!m_map.augmentation_is_gdb)
5924 break;
23c13d42 5925 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5926 break;
5927 }
5928 }
5929
5930 /* Skip if already read in. */
5931 if (per_cu->v.quick->compunit_symtab)
5932 goto again;
5933
5934 /* Check static vs global. */
23c13d42 5935 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5936 {
2b79f376 5937 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5938 const bool symbol_is_static =
5939 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5940 if (want_static != symbol_is_static)
2b79f376 5941 goto again;
927aa2e7
JK
5942 }
5943
5944 /* Match dw2_symtab_iter_next, symbol_kind
5945 and debug_names::psymbol_tag. */
5946 switch (m_domain)
5947 {
5948 case VAR_DOMAIN:
5949 switch (indexval.dwarf_tag)
5950 {
5951 case DW_TAG_variable:
5952 case DW_TAG_subprogram:
5953 /* Some types are also in VAR_DOMAIN. */
5954 case DW_TAG_typedef:
5955 case DW_TAG_structure_type:
5956 break;
5957 default:
5958 goto again;
5959 }
5960 break;
5961 case STRUCT_DOMAIN:
5962 switch (indexval.dwarf_tag)
5963 {
5964 case DW_TAG_typedef:
5965 case DW_TAG_structure_type:
5966 break;
5967 default:
5968 goto again;
5969 }
5970 break;
5971 case LABEL_DOMAIN:
5972 switch (indexval.dwarf_tag)
5973 {
5974 case 0:
5975 case DW_TAG_variable:
5976 break;
5977 default:
5978 goto again;
5979 }
5980 break;
59c35742
AB
5981 case MODULE_DOMAIN:
5982 switch (indexval.dwarf_tag)
5983 {
5984 case DW_TAG_module:
5985 break;
5986 default:
5987 goto again;
5988 }
5989 break;
927aa2e7
JK
5990 default:
5991 break;
5992 }
5993
5994 /* Match dw2_expand_symtabs_matching, symbol_kind and
5995 debug_names::psymbol_tag. */
5996 switch (m_search)
4b514bc8 5997 {
927aa2e7
JK
5998 case VARIABLES_DOMAIN:
5999 switch (indexval.dwarf_tag)
4b514bc8 6000 {
927aa2e7
JK
6001 case DW_TAG_variable:
6002 break;
6003 default:
6004 goto again;
4b514bc8 6005 }
927aa2e7
JK
6006 break;
6007 case FUNCTIONS_DOMAIN:
6008 switch (indexval.dwarf_tag)
4b514bc8 6009 {
927aa2e7
JK
6010 case DW_TAG_subprogram:
6011 break;
6012 default:
6013 goto again;
4b514bc8 6014 }
927aa2e7
JK
6015 break;
6016 case TYPES_DOMAIN:
6017 switch (indexval.dwarf_tag)
6018 {
6019 case DW_TAG_typedef:
6020 case DW_TAG_structure_type:
6021 break;
6022 default:
6023 goto again;
6024 }
6025 break;
59c35742
AB
6026 case MODULES_DOMAIN:
6027 switch (indexval.dwarf_tag)
6028 {
6029 case DW_TAG_module:
6030 break;
6031 default:
6032 goto again;
6033 }
927aa2e7
JK
6034 default:
6035 break;
4b514bc8 6036 }
927aa2e7
JK
6037
6038 return per_cu;
4b514bc8 6039}
61920122 6040
927aa2e7 6041static struct compunit_symtab *
c7f839cb 6042dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6043 const char *name, domain_enum domain)
4b514bc8 6044{
ed2dc618
SM
6045 struct dwarf2_per_objfile *dwarf2_per_objfile
6046 = get_dwarf2_per_objfile (objfile);
61920122 6047
927aa2e7
JK
6048 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6049 if (!mapp)
61920122 6050 {
927aa2e7
JK
6051 /* index is NULL if OBJF_READNOW. */
6052 return NULL;
6053 }
6054 const auto &map = *mapp;
9291a0cd 6055
2b79f376 6056 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6057
927aa2e7
JK
6058 struct compunit_symtab *stab_best = NULL;
6059 struct dwarf2_per_cu_data *per_cu;
6060 while ((per_cu = iter.next ()) != NULL)
6061 {
6062 struct symbol *sym, *with_opaque = NULL;
58f0c718 6063 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6064 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6065 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6066
927aa2e7
JK
6067 sym = block_find_symbol (block, name, domain,
6068 block_find_non_opaque_type_preferred,
6069 &with_opaque);
9703b513 6070
927aa2e7
JK
6071 /* Some caution must be observed with overloaded functions and
6072 methods, since the index will not contain any overload
6073 information (but NAME might contain it). */
a3ec0bb1 6074
927aa2e7 6075 if (sym != NULL
987012b8 6076 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
6077 return stab;
6078 if (with_opaque != NULL
987012b8 6079 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 6080 stab_best = stab;
9703b513 6081
927aa2e7 6082 /* Keep looking through other CUs. */
9703b513
TT
6083 }
6084
927aa2e7 6085 return stab_best;
9703b513
TT
6086}
6087
927aa2e7
JK
6088/* This dumps minimal information about .debug_names. It is called
6089 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6090 uses this to verify that .debug_names has been loaded. */
9291a0cd 6091
927aa2e7
JK
6092static void
6093dw2_debug_names_dump (struct objfile *objfile)
6094{
ed2dc618
SM
6095 struct dwarf2_per_objfile *dwarf2_per_objfile
6096 = get_dwarf2_per_objfile (objfile);
6097
927aa2e7
JK
6098 gdb_assert (dwarf2_per_objfile->using_index);
6099 printf_filtered (".debug_names:");
6100 if (dwarf2_per_objfile->debug_names_table)
6101 printf_filtered (" exists\n");
6102 else
6103 printf_filtered (" faked for \"readnow\"\n");
6104 printf_filtered ("\n");
9291a0cd
TT
6105}
6106
9291a0cd 6107static void
927aa2e7
JK
6108dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6109 const char *func_name)
9291a0cd 6110{
ed2dc618
SM
6111 struct dwarf2_per_objfile *dwarf2_per_objfile
6112 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6113
927aa2e7
JK
6114 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6115 if (dwarf2_per_objfile->debug_names_table)
24c79950 6116 {
927aa2e7 6117 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6118
2b79f376 6119 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6120
927aa2e7
JK
6121 struct dwarf2_per_cu_data *per_cu;
6122 while ((per_cu = iter.next ()) != NULL)
58f0c718 6123 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6124 }
6125}
24c79950 6126
3b00ef10
TT
6127static void
6128dw2_debug_names_map_matching_symbols
6129 (struct objfile *objfile,
6130 const lookup_name_info &name, domain_enum domain,
6131 int global,
6132 gdb::function_view<symbol_found_callback_ftype> callback,
6133 symbol_compare_ftype *ordered_compare)
6134{
6135 struct dwarf2_per_objfile *dwarf2_per_objfile
6136 = get_dwarf2_per_objfile (objfile);
6137
6138 /* debug_names_table is NULL if OBJF_READNOW. */
6139 if (!dwarf2_per_objfile->debug_names_table)
6140 return;
6141
6142 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6143 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6144
6145 const char *match_name = name.ada ().lookup_name ().c_str ();
6146 auto matcher = [&] (const char *symname)
6147 {
6148 if (ordered_compare == nullptr)
6149 return true;
6150 return ordered_compare (symname, match_name) == 0;
6151 };
6152
6153 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6154 [&] (offset_type namei)
6155 {
6156 /* The name was matched, now expand corresponding CUs that were
6157 marked. */
6158 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6159
6160 struct dwarf2_per_cu_data *per_cu;
6161 while ((per_cu = iter.next ()) != NULL)
6162 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6163 return true;
6164 });
6165
6166 /* It's a shame we couldn't do this inside the
6167 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6168 that have already been expanded. Instead, this loop matches what
6169 the psymtab code does. */
6170 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6171 {
6172 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6173 if (cust != nullptr)
6174 {
6175 const struct block *block
6176 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6177 if (!iterate_over_symbols_terminated (block, name,
6178 domain, callback))
6179 break;
6180 }
6181 }
6182}
6183
927aa2e7
JK
6184static void
6185dw2_debug_names_expand_symtabs_matching
6186 (struct objfile *objfile,
6187 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6188 const lookup_name_info &lookup_name,
6189 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6190 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6191 enum search_domain kind)
6192{
ed2dc618
SM
6193 struct dwarf2_per_objfile *dwarf2_per_objfile
6194 = get_dwarf2_per_objfile (objfile);
9291a0cd 6195
927aa2e7
JK
6196 /* debug_names_table is NULL if OBJF_READNOW. */
6197 if (!dwarf2_per_objfile->debug_names_table)
6198 return;
9291a0cd 6199
ed2dc618 6200 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6201
44ed8f3e 6202 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6203
44ed8f3e
PA
6204 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6205 symbol_matcher,
6206 kind, [&] (offset_type namei)
927aa2e7 6207 {
927aa2e7
JK
6208 /* The name was matched, now expand corresponding CUs that were
6209 marked. */
6210 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6211
927aa2e7
JK
6212 struct dwarf2_per_cu_data *per_cu;
6213 while ((per_cu = iter.next ()) != NULL)
6214 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6215 expansion_notify);
3b00ef10 6216 return true;
44ed8f3e 6217 });
9291a0cd
TT
6218}
6219
927aa2e7 6220const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6221{
6222 dw2_has_symbols,
6223 dw2_find_last_source_symtab,
6224 dw2_forget_cached_source_info,
f8eba3c6 6225 dw2_map_symtabs_matching_filename,
927aa2e7 6226 dw2_debug_names_lookup_symbol,
9291a0cd 6227 dw2_print_stats,
927aa2e7 6228 dw2_debug_names_dump,
927aa2e7 6229 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6230 dw2_expand_all_symtabs,
652a8996 6231 dw2_expand_symtabs_with_fullname,
3b00ef10 6232 dw2_debug_names_map_matching_symbols,
927aa2e7 6233 dw2_debug_names_expand_symtabs_matching,
43f3e411 6234 dw2_find_pc_sect_compunit_symtab,
71a3c369 6235 NULL,
9291a0cd
TT
6236 dw2_map_symbol_filenames
6237};
6238
4485a1c1
SM
6239/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6240 to either a dwarf2_per_objfile or dwz_file object. */
6241
6242template <typename T>
6243static gdb::array_view<const gdb_byte>
6244get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6245{
6246 dwarf2_section_info *section = &section_owner->gdb_index;
6247
6248 if (dwarf2_section_empty_p (section))
6249 return {};
6250
6251 /* Older elfutils strip versions could keep the section in the main
6252 executable while splitting it for the separate debug info file. */
6253 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6254 return {};
6255
6256 dwarf2_read_section (obj, section);
6257
8bebfcda
PA
6258 /* dwarf2_section_info::size is a bfd_size_type, while
6259 gdb::array_view works with size_t. On 32-bit hosts, with
6260 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6261 is 32-bit. So we need an explicit narrowing conversion here.
6262 This is fine, because it's impossible to allocate or mmap an
6263 array/buffer larger than what size_t can represent. */
6264 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6265}
6266
87d6a7aa
SM
6267/* Lookup the index cache for the contents of the index associated to
6268 DWARF2_OBJ. */
6269
6270static gdb::array_view<const gdb_byte>
6271get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6272{
6273 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6274 if (build_id == nullptr)
6275 return {};
6276
6277 return global_index_cache.lookup_gdb_index (build_id,
6278 &dwarf2_obj->index_cache_res);
6279}
6280
6281/* Same as the above, but for DWZ. */
6282
6283static gdb::array_view<const gdb_byte>
6284get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6285{
6286 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6287 if (build_id == nullptr)
6288 return {};
6289
6290 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6291}
6292
3c0aa29a 6293/* See symfile.h. */
9291a0cd 6294
3c0aa29a
PA
6295bool
6296dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6297{
ed2dc618
SM
6298 struct dwarf2_per_objfile *dwarf2_per_objfile
6299 = get_dwarf2_per_objfile (objfile);
6300
9291a0cd
TT
6301 /* If we're about to read full symbols, don't bother with the
6302 indices. In this case we also don't care if some other debug
6303 format is making psymtabs, because they are all about to be
6304 expanded anyway. */
6305 if ((objfile->flags & OBJF_READNOW))
6306 {
9291a0cd 6307 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6308 create_all_comp_units (dwarf2_per_objfile);
6309 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6310 dwarf2_per_objfile->quick_file_names_table
6311 = create_quick_file_names_table
6312 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6313
b76e467d 6314 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6315 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6316 {
ff4c9fec 6317 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6318
e254ef6a
DE
6319 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6320 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6321 }
6322
6323 /* Return 1 so that gdb sees the "quick" functions. However,
6324 these functions will be no-ops because we will have expanded
6325 all symtabs. */
3c0aa29a
PA
6326 *index_kind = dw_index_kind::GDB_INDEX;
6327 return true;
9291a0cd
TT
6328 }
6329
ed2dc618 6330 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6331 {
6332 *index_kind = dw_index_kind::DEBUG_NAMES;
6333 return true;
6334 }
927aa2e7 6335
4485a1c1
SM
6336 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6337 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6338 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6339 {
6340 *index_kind = dw_index_kind::GDB_INDEX;
6341 return true;
6342 }
9291a0cd 6343
87d6a7aa
SM
6344 /* ... otherwise, try to find the index in the index cache. */
6345 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6346 get_gdb_index_contents_from_cache,
6347 get_gdb_index_contents_from_cache_dwz))
6348 {
6349 global_index_cache.hit ();
6350 *index_kind = dw_index_kind::GDB_INDEX;
6351 return true;
6352 }
6353
6354 global_index_cache.miss ();
3c0aa29a 6355 return false;
9291a0cd
TT
6356}
6357
6358\f
6359
dce234bc
PP
6360/* Build a partial symbol table. */
6361
6362void
f29dff0a 6363dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6364{
ed2dc618
SM
6365 struct dwarf2_per_objfile *dwarf2_per_objfile
6366 = get_dwarf2_per_objfile (objfile);
c9bf0622 6367
6eee24ce 6368 init_psymbol_list (objfile, 1024);
c906108c 6369
a70b8144 6370 try
c9bf0622
TT
6371 {
6372 /* This isn't really ideal: all the data we allocate on the
6373 objfile's obstack is still uselessly kept around. However,
6374 freeing it seems unsafe. */
906768f9 6375 psymtab_discarder psymtabs (objfile);
ed2dc618 6376 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6377 psymtabs.keep ();
87d6a7aa
SM
6378
6379 /* (maybe) store an index in the cache. */
6380 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6381 }
230d2906 6382 catch (const gdb_exception_error &except)
492d29ea
PA
6383 {
6384 exception_print (gdb_stderr, except);
6385 }
c906108c 6386}
c906108c 6387
1ce1cefd
DE
6388/* Return the total length of the CU described by HEADER. */
6389
6390static unsigned int
6391get_cu_length (const struct comp_unit_head *header)
6392{
6393 return header->initial_length_size + header->length;
6394}
6395
9c541725 6396/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6397
9c541725
PA
6398static inline bool
6399offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6400{
9c541725
PA
6401 sect_offset bottom = cu_header->sect_off;
6402 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6403
9c541725 6404 return sect_off >= bottom && sect_off < top;
45452591
DE
6405}
6406
3b80fe9b
DE
6407/* Find the base address of the compilation unit for range lists and
6408 location lists. It will normally be specified by DW_AT_low_pc.
6409 In DWARF-3 draft 4, the base address could be overridden by
6410 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6411 compilation units with discontinuous ranges. */
6412
6413static void
6414dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6415{
6416 struct attribute *attr;
6417
6418 cu->base_known = 0;
6419 cu->base_address = 0;
6420
6421 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6422 if (attr != nullptr)
3b80fe9b 6423 {
31aa7e4e 6424 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6425 cu->base_known = 1;
6426 }
6427 else
6428 {
6429 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6430 if (attr != nullptr)
3b80fe9b 6431 {
31aa7e4e 6432 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6433 cu->base_known = 1;
6434 }
6435 }
6436}
6437
93311388 6438/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6439 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6440 NOTE: This leaves members offset, first_die_offset to be filled in
6441 by the caller. */
107d2387 6442
d521ce57 6443static const gdb_byte *
107d2387 6444read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6445 const gdb_byte *info_ptr,
6446 struct dwarf2_section_info *section,
6447 rcuh_kind section_kind)
107d2387
AC
6448{
6449 int signed_addr;
891d2f0b 6450 unsigned int bytes_read;
43988095
JK
6451 const char *filename = get_section_file_name (section);
6452 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6453
6454 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6455 cu_header->initial_length_size = bytes_read;
6456 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6457 info_ptr += bytes_read;
107d2387 6458 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6459 if (cu_header->version < 2 || cu_header->version > 5)
6460 error (_("Dwarf Error: wrong version in compilation unit header "
6461 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6462 cu_header->version, filename);
107d2387 6463 info_ptr += 2;
43988095
JK
6464 if (cu_header->version < 5)
6465 switch (section_kind)
6466 {
6467 case rcuh_kind::COMPILE:
6468 cu_header->unit_type = DW_UT_compile;
6469 break;
6470 case rcuh_kind::TYPE:
6471 cu_header->unit_type = DW_UT_type;
6472 break;
6473 default:
6474 internal_error (__FILE__, __LINE__,
6475 _("read_comp_unit_head: invalid section_kind"));
6476 }
6477 else
6478 {
6479 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6480 (read_1_byte (abfd, info_ptr));
6481 info_ptr += 1;
6482 switch (cu_header->unit_type)
6483 {
6484 case DW_UT_compile:
a084a2a6
AT
6485 case DW_UT_partial:
6486 case DW_UT_skeleton:
6487 case DW_UT_split_compile:
43988095
JK
6488 if (section_kind != rcuh_kind::COMPILE)
6489 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6490 "(is %s, should be %s) [in module %s]"),
6491 dwarf_unit_type_name (cu_header->unit_type),
6492 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6493 break;
6494 case DW_UT_type:
a084a2a6 6495 case DW_UT_split_type:
43988095
JK
6496 section_kind = rcuh_kind::TYPE;
6497 break;
6498 default:
6499 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6500 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6501 "[in module %s]"), cu_header->unit_type,
6502 dwarf_unit_type_name (DW_UT_compile),
6503 dwarf_unit_type_name (DW_UT_skeleton),
6504 dwarf_unit_type_name (DW_UT_split_compile),
6505 dwarf_unit_type_name (DW_UT_type),
6506 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6507 }
6508
6509 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6510 info_ptr += 1;
6511 }
9c541725
PA
6512 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6513 cu_header,
6514 &bytes_read);
613e1657 6515 info_ptr += bytes_read;
43988095
JK
6516 if (cu_header->version < 5)
6517 {
6518 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6519 info_ptr += 1;
6520 }
107d2387
AC
6521 signed_addr = bfd_get_sign_extend_vma (abfd);
6522 if (signed_addr < 0)
8e65ff28 6523 internal_error (__FILE__, __LINE__,
e2e0b3e5 6524 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6525 cu_header->signed_addr_p = signed_addr;
c764a876 6526
a084a2a6
AT
6527 bool header_has_signature = section_kind == rcuh_kind::TYPE
6528 || cu_header->unit_type == DW_UT_skeleton
6529 || cu_header->unit_type == DW_UT_split_compile;
43988095 6530
a084a2a6
AT
6531 if (header_has_signature)
6532 {
43988095
JK
6533 cu_header->signature = read_8_bytes (abfd, info_ptr);
6534 info_ptr += 8;
a084a2a6 6535 }
43988095 6536
a084a2a6
AT
6537 if (section_kind == rcuh_kind::TYPE)
6538 {
6539 LONGEST type_offset;
43988095
JK
6540 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6541 info_ptr += bytes_read;
9c541725
PA
6542 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6543 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6544 error (_("Dwarf Error: Too big type_offset in compilation unit "
6545 "header (is %s) [in module %s]"), plongest (type_offset),
6546 filename);
6547 }
6548
107d2387
AC
6549 return info_ptr;
6550}
6551
36586728
TT
6552/* Helper function that returns the proper abbrev section for
6553 THIS_CU. */
6554
6555static struct dwarf2_section_info *
6556get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6557{
6558 struct dwarf2_section_info *abbrev;
ed2dc618 6559 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6560
6561 if (this_cu->is_dwz)
ed2dc618 6562 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6563 else
6564 abbrev = &dwarf2_per_objfile->abbrev;
6565
6566 return abbrev;
6567}
6568
9ff913ba
DE
6569/* Subroutine of read_and_check_comp_unit_head and
6570 read_and_check_type_unit_head to simplify them.
6571 Perform various error checking on the header. */
6572
6573static void
ed2dc618
SM
6574error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6575 struct comp_unit_head *header,
4bdcc0c1
DE
6576 struct dwarf2_section_info *section,
6577 struct dwarf2_section_info *abbrev_section)
9ff913ba 6578{
a32a8923 6579 const char *filename = get_section_file_name (section);
9ff913ba 6580
9c541725 6581 if (to_underlying (header->abbrev_sect_off)
36586728 6582 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6583 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6584 "(offset %s + 6) [in module %s]"),
6585 sect_offset_str (header->abbrev_sect_off),
6586 sect_offset_str (header->sect_off),
9ff913ba
DE
6587 filename);
6588
9c541725 6589 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6590 avoid potential 32-bit overflow. */
9c541725 6591 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6592 > section->size)
9c541725 6593 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6594 "(offset %s + 0) [in module %s]"),
6595 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6596 filename);
6597}
6598
6599/* Read in a CU/TU header and perform some basic error checking.
6600 The contents of the header are stored in HEADER.
6601 The result is a pointer to the start of the first DIE. */
adabb602 6602
d521ce57 6603static const gdb_byte *
ed2dc618
SM
6604read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6605 struct comp_unit_head *header,
9ff913ba 6606 struct dwarf2_section_info *section,
4bdcc0c1 6607 struct dwarf2_section_info *abbrev_section,
d521ce57 6608 const gdb_byte *info_ptr,
43988095 6609 rcuh_kind section_kind)
72bf9492 6610{
d521ce57 6611 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6612
9c541725 6613 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6614
43988095 6615 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6616
9c541725 6617 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6618
ed2dc618
SM
6619 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6620 abbrev_section);
9ff913ba
DE
6621
6622 return info_ptr;
348e048f
DE
6623}
6624
f4dc4d17
DE
6625/* Fetch the abbreviation table offset from a comp or type unit header. */
6626
6627static sect_offset
ed2dc618
SM
6628read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6629 struct dwarf2_section_info *section,
9c541725 6630 sect_offset sect_off)
f4dc4d17 6631{
a32a8923 6632 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6633 const gdb_byte *info_ptr;
ac298888 6634 unsigned int initial_length_size, offset_size;
43988095 6635 uint16_t version;
f4dc4d17
DE
6636
6637 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6638 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6639 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6640 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6641 info_ptr += initial_length_size;
6642
6643 version = read_2_bytes (abfd, info_ptr);
6644 info_ptr += 2;
6645 if (version >= 5)
6646 {
6647 /* Skip unit type and address size. */
6648 info_ptr += 2;
6649 }
6650
9c541725 6651 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6652}
6653
aaa75496
JB
6654/* Allocate a new partial symtab for file named NAME and mark this new
6655 partial symtab as being an include of PST. */
6656
6657static void
d521ce57 6658dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6659 struct objfile *objfile)
6660{
6661 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6662
fbd9ab74
JK
6663 if (!IS_ABSOLUTE_PATH (subpst->filename))
6664 {
6665 /* It shares objfile->objfile_obstack. */
6666 subpst->dirname = pst->dirname;
6667 }
6668
a9342b62 6669 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6670 subpst->dependencies[0] = pst;
6671 subpst->number_of_dependencies = 1;
6672
aaa75496 6673 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6674
6675 /* No private part is necessary for include psymtabs. This property
6676 can be used to differentiate between such include psymtabs and
10b3939b 6677 the regular ones. */
58a9656e 6678 subpst->read_symtab_private = NULL;
aaa75496
JB
6679}
6680
6681/* Read the Line Number Program data and extract the list of files
6682 included by the source file represented by PST. Build an include
d85a05f0 6683 partial symtab for each of these included files. */
aaa75496
JB
6684
6685static void
6686dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6687 struct die_info *die,
6688 struct partial_symtab *pst)
aaa75496 6689{
fff8551c 6690 line_header_up lh;
d85a05f0 6691 struct attribute *attr;
aaa75496 6692
d85a05f0 6693 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6694 if (attr != nullptr)
9c541725 6695 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6696 if (lh == NULL)
6697 return; /* No linetable, so no includes. */
6698
79748972
TT
6699 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6700 that we pass in the raw text_low here; that is ok because we're
6701 only decoding the line table to make include partial symtabs, and
6702 so the addresses aren't really used. */
4ae976d1 6703 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6704 pst->raw_text_low (), 1);
aaa75496
JB
6705}
6706
348e048f 6707static hashval_t
52dc124a 6708hash_signatured_type (const void *item)
348e048f 6709{
9a3c8263
SM
6710 const struct signatured_type *sig_type
6711 = (const struct signatured_type *) item;
9a619af0 6712
348e048f 6713 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6714 return sig_type->signature;
348e048f
DE
6715}
6716
6717static int
52dc124a 6718eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6719{
9a3c8263
SM
6720 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6721 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6722
348e048f
DE
6723 return lhs->signature == rhs->signature;
6724}
6725
1fd400ff
TT
6726/* Allocate a hash table for signatured types. */
6727
6728static htab_t
673bfd45 6729allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6730{
6731 return htab_create_alloc_ex (41,
52dc124a
DE
6732 hash_signatured_type,
6733 eq_signatured_type,
1fd400ff
TT
6734 NULL,
6735 &objfile->objfile_obstack,
6736 hashtab_obstack_allocate,
6737 dummy_obstack_deallocate);
6738}
6739
d467dd73 6740/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6741
6742static int
d467dd73 6743add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6744{
9a3c8263 6745 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6746 std::vector<signatured_type *> *all_type_units
6747 = (std::vector<signatured_type *> *) datum;
1fd400ff 6748
b2bdb8cf 6749 all_type_units->push_back (sigt);
1fd400ff
TT
6750
6751 return 1;
6752}
6753
78d4d2c5 6754/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6755 and fill them into TYPES_HTAB. It will process only type units,
6756 therefore DW_UT_type. */
c88ee1f0 6757
78d4d2c5 6758static void
ed2dc618
SM
6759create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6760 struct dwo_file *dwo_file,
43988095
JK
6761 dwarf2_section_info *section, htab_t &types_htab,
6762 rcuh_kind section_kind)
348e048f 6763{
3019eac3 6764 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6765 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6766 bfd *abfd;
6767 const gdb_byte *info_ptr, *end_ptr;
348e048f 6768
4bdcc0c1
DE
6769 abbrev_section = (dwo_file != NULL
6770 ? &dwo_file->sections.abbrev
6771 : &dwarf2_per_objfile->abbrev);
6772
b4f54984 6773 if (dwarf_read_debug)
43988095
JK
6774 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6775 get_section_name (section),
a32a8923 6776 get_section_file_name (abbrev_section));
09406207 6777
78d4d2c5
JK
6778 dwarf2_read_section (objfile, section);
6779 info_ptr = section->buffer;
348e048f 6780
78d4d2c5
JK
6781 if (info_ptr == NULL)
6782 return;
348e048f 6783
78d4d2c5
JK
6784 /* We can't set abfd until now because the section may be empty or
6785 not present, in which case the bfd is unknown. */
6786 abfd = get_section_bfd_owner (section);
348e048f 6787
78d4d2c5
JK
6788 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6789 because we don't need to read any dies: the signature is in the
6790 header. */
3019eac3 6791
78d4d2c5
JK
6792 end_ptr = info_ptr + section->size;
6793 while (info_ptr < end_ptr)
6794 {
78d4d2c5
JK
6795 struct signatured_type *sig_type;
6796 struct dwo_unit *dwo_tu;
6797 void **slot;
6798 const gdb_byte *ptr = info_ptr;
6799 struct comp_unit_head header;
6800 unsigned int length;
8b70b953 6801
9c541725 6802 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6803
a49dd8dd
JK
6804 /* Initialize it due to a false compiler warning. */
6805 header.signature = -1;
9c541725 6806 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6807
78d4d2c5
JK
6808 /* We need to read the type's signature in order to build the hash
6809 table, but we don't need anything else just yet. */
348e048f 6810
ed2dc618 6811 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6812 abbrev_section, ptr, section_kind);
348e048f 6813
78d4d2c5 6814 length = get_cu_length (&header);
6caca83c 6815
78d4d2c5
JK
6816 /* Skip dummy type units. */
6817 if (ptr >= info_ptr + length
43988095
JK
6818 || peek_abbrev_code (abfd, ptr) == 0
6819 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6820 {
6821 info_ptr += length;
6822 continue;
6823 }
dee91e82 6824
78d4d2c5
JK
6825 if (types_htab == NULL)
6826 {
6827 if (dwo_file)
6828 types_htab = allocate_dwo_unit_table (objfile);
6829 else
6830 types_htab = allocate_signatured_type_table (objfile);
6831 }
8b70b953 6832
78d4d2c5
JK
6833 if (dwo_file)
6834 {
6835 sig_type = NULL;
6836 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6837 struct dwo_unit);
6838 dwo_tu->dwo_file = dwo_file;
43988095 6839 dwo_tu->signature = header.signature;
9c541725 6840 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6841 dwo_tu->section = section;
9c541725 6842 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6843 dwo_tu->length = length;
6844 }
6845 else
6846 {
6847 /* N.B.: type_offset is not usable if this type uses a DWO file.
6848 The real type_offset is in the DWO file. */
6849 dwo_tu = NULL;
6850 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6851 struct signatured_type);
43988095 6852 sig_type->signature = header.signature;
9c541725 6853 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6854 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6855 sig_type->per_cu.is_debug_types = 1;
6856 sig_type->per_cu.section = section;
9c541725 6857 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6858 sig_type->per_cu.length = length;
6859 }
6860
6861 slot = htab_find_slot (types_htab,
6862 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6863 INSERT);
6864 gdb_assert (slot != NULL);
6865 if (*slot != NULL)
6866 {
9c541725 6867 sect_offset dup_sect_off;
0349ea22 6868
3019eac3
DE
6869 if (dwo_file)
6870 {
78d4d2c5
JK
6871 const struct dwo_unit *dup_tu
6872 = (const struct dwo_unit *) *slot;
6873
9c541725 6874 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6875 }
6876 else
6877 {
78d4d2c5
JK
6878 const struct signatured_type *dup_tu
6879 = (const struct signatured_type *) *slot;
6880
9c541725 6881 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6882 }
8b70b953 6883
b98664d3 6884 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6885 " the entry at offset %s, signature %s"),
6886 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6887 hex_string (header.signature));
78d4d2c5
JK
6888 }
6889 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6890
78d4d2c5 6891 if (dwarf_read_debug > 1)
9d8780f0
SM
6892 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6893 sect_offset_str (sect_off),
43988095 6894 hex_string (header.signature));
3019eac3 6895
78d4d2c5
JK
6896 info_ptr += length;
6897 }
6898}
3019eac3 6899
78d4d2c5
JK
6900/* Create the hash table of all entries in the .debug_types
6901 (or .debug_types.dwo) section(s).
6902 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6903 otherwise it is NULL.
b3c8eb43 6904
78d4d2c5 6905 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6906
78d4d2c5 6907 Note: This function processes DWO files only, not DWP files. */
348e048f 6908
78d4d2c5 6909static void
ed2dc618
SM
6910create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6911 struct dwo_file *dwo_file,
fd5866f6 6912 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6913 htab_t &types_htab)
6914{
fd5866f6
SM
6915 for (dwarf2_section_info &section : type_sections)
6916 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6917 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6918}
6919
6920/* Create the hash table of all entries in the .debug_types section,
6921 and initialize all_type_units.
6922 The result is zero if there is an error (e.g. missing .debug_types section),
6923 otherwise non-zero. */
6924
6925static int
ed2dc618 6926create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6927{
78d4d2c5 6928 htab_t types_htab = NULL;
3019eac3 6929
ed2dc618
SM
6930 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6931 &dwarf2_per_objfile->info, types_htab,
43988095 6932 rcuh_kind::COMPILE);
ed2dc618
SM
6933 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6934 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6935 if (types_htab == NULL)
6936 {
6937 dwarf2_per_objfile->signatured_types = NULL;
6938 return 0;
6939 }
6940
348e048f
DE
6941 dwarf2_per_objfile->signatured_types = types_htab;
6942
b2bdb8cf
SM
6943 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6944 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6945
6946 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6947 &dwarf2_per_objfile->all_type_units);
1fd400ff 6948
348e048f
DE
6949 return 1;
6950}
6951
6aa5f3a6
DE
6952/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6953 If SLOT is non-NULL, it is the entry to use in the hash table.
6954 Otherwise we find one. */
6955
6956static struct signatured_type *
ed2dc618
SM
6957add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6958 void **slot)
6aa5f3a6
DE
6959{
6960 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6961
b2bdb8cf
SM
6962 if (dwarf2_per_objfile->all_type_units.size ()
6963 == dwarf2_per_objfile->all_type_units.capacity ())
6964 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6965
b2bdb8cf
SM
6966 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6967 struct signatured_type);
6968
6969 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6970 sig_type->signature = sig;
6971 sig_type->per_cu.is_debug_types = 1;
6972 if (dwarf2_per_objfile->using_index)
6973 {
6974 sig_type->per_cu.v.quick =
6975 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6976 struct dwarf2_per_cu_quick_data);
6977 }
6978
6979 if (slot == NULL)
6980 {
6981 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6982 sig_type, INSERT);
6983 }
6984 gdb_assert (*slot == NULL);
6985 *slot = sig_type;
6986 /* The rest of sig_type must be filled in by the caller. */
6987 return sig_type;
6988}
6989
a2ce51a0
DE
6990/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6991 Fill in SIG_ENTRY with DWO_ENTRY. */
6992
6993static void
ed2dc618 6994fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6995 struct signatured_type *sig_entry,
6996 struct dwo_unit *dwo_entry)
6997{
7ee85ab1 6998 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6999 gdb_assert (! sig_entry->per_cu.queued);
7000 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7001 if (dwarf2_per_objfile->using_index)
7002 {
7003 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7004 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7005 }
7006 else
7007 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7008 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7009 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7010 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7011 gdb_assert (sig_entry->dwo_unit == NULL);
7012
7013 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7014 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7015 sig_entry->per_cu.length = dwo_entry->length;
7016 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7017 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7018 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7019 sig_entry->dwo_unit = dwo_entry;
7020}
7021
7022/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7023 If we haven't read the TU yet, create the signatured_type data structure
7024 for a TU to be read in directly from a DWO file, bypassing the stub.
7025 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7026 using .gdb_index, then when reading a CU we want to stay in the DWO file
7027 containing that CU. Otherwise we could end up reading several other DWO
7028 files (due to comdat folding) to process the transitive closure of all the
7029 mentioned TUs, and that can be slow. The current DWO file will have every
7030 type signature that it needs.
a2ce51a0
DE
7031 We only do this for .gdb_index because in the psymtab case we already have
7032 to read all the DWOs to build the type unit groups. */
7033
7034static struct signatured_type *
7035lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7036{
518817b3
SM
7037 struct dwarf2_per_objfile *dwarf2_per_objfile
7038 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7039 struct objfile *objfile = dwarf2_per_objfile->objfile;
7040 struct dwo_file *dwo_file;
7041 struct dwo_unit find_dwo_entry, *dwo_entry;
7042 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7043 void **slot;
a2ce51a0
DE
7044
7045 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7046
6aa5f3a6
DE
7047 /* If TU skeletons have been removed then we may not have read in any
7048 TUs yet. */
7049 if (dwarf2_per_objfile->signatured_types == NULL)
7050 {
7051 dwarf2_per_objfile->signatured_types
7052 = allocate_signatured_type_table (objfile);
7053 }
a2ce51a0
DE
7054
7055 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7056 Use the global signatured_types array to do our own comdat-folding
7057 of types. If this is the first time we're reading this TU, and
7058 the TU has an entry in .gdb_index, replace the recorded data from
7059 .gdb_index with this TU. */
a2ce51a0 7060
a2ce51a0 7061 find_sig_entry.signature = sig;
6aa5f3a6
DE
7062 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7063 &find_sig_entry, INSERT);
9a3c8263 7064 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7065
7066 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7067 read. Don't reassign the global entry to point to this DWO if that's
7068 the case. Also note that if the TU is already being read, it may not
7069 have come from a DWO, the program may be a mix of Fission-compiled
7070 code and non-Fission-compiled code. */
7071
7072 /* Have we already tried to read this TU?
7073 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7074 needn't exist in the global table yet). */
7075 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7076 return sig_entry;
7077
6aa5f3a6
DE
7078 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7079 dwo_unit of the TU itself. */
7080 dwo_file = cu->dwo_unit->dwo_file;
7081
a2ce51a0
DE
7082 /* Ok, this is the first time we're reading this TU. */
7083 if (dwo_file->tus == NULL)
7084 return NULL;
7085 find_dwo_entry.signature = sig;
9a3c8263 7086 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7087 if (dwo_entry == NULL)
7088 return NULL;
7089
6aa5f3a6
DE
7090 /* If the global table doesn't have an entry for this TU, add one. */
7091 if (sig_entry == NULL)
ed2dc618 7092 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7093
ed2dc618 7094 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7095 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7096 return sig_entry;
7097}
7098
a2ce51a0
DE
7099/* Subroutine of lookup_signatured_type.
7100 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7101 then try the DWP file. If the TU stub (skeleton) has been removed then
7102 it won't be in .gdb_index. */
a2ce51a0
DE
7103
7104static struct signatured_type *
7105lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7106{
518817b3
SM
7107 struct dwarf2_per_objfile *dwarf2_per_objfile
7108 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7109 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7110 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7111 struct dwo_unit *dwo_entry;
7112 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7113 void **slot;
a2ce51a0
DE
7114
7115 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7116 gdb_assert (dwp_file != NULL);
7117
6aa5f3a6
DE
7118 /* If TU skeletons have been removed then we may not have read in any
7119 TUs yet. */
7120 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7121 {
6aa5f3a6
DE
7122 dwarf2_per_objfile->signatured_types
7123 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7124 }
7125
6aa5f3a6
DE
7126 find_sig_entry.signature = sig;
7127 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7128 &find_sig_entry, INSERT);
9a3c8263 7129 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7130
7131 /* Have we already tried to read this TU?
7132 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7133 needn't exist in the global table yet). */
7134 if (sig_entry != NULL)
7135 return sig_entry;
7136
a2ce51a0
DE
7137 if (dwp_file->tus == NULL)
7138 return NULL;
ed2dc618 7139 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7140 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7141 if (dwo_entry == NULL)
7142 return NULL;
7143
ed2dc618
SM
7144 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7145 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7146
a2ce51a0
DE
7147 return sig_entry;
7148}
7149
380bca97 7150/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7151 Returns NULL if signature SIG is not present in the table.
7152 It is up to the caller to complain about this. */
348e048f
DE
7153
7154static struct signatured_type *
a2ce51a0 7155lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7156{
518817b3
SM
7157 struct dwarf2_per_objfile *dwarf2_per_objfile
7158 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7159
a2ce51a0
DE
7160 if (cu->dwo_unit
7161 && dwarf2_per_objfile->using_index)
7162 {
7163 /* We're in a DWO/DWP file, and we're using .gdb_index.
7164 These cases require special processing. */
ed2dc618 7165 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7166 return lookup_dwo_signatured_type (cu, sig);
7167 else
7168 return lookup_dwp_signatured_type (cu, sig);
7169 }
7170 else
7171 {
7172 struct signatured_type find_entry, *entry;
348e048f 7173
a2ce51a0
DE
7174 if (dwarf2_per_objfile->signatured_types == NULL)
7175 return NULL;
7176 find_entry.signature = sig;
9a3c8263
SM
7177 entry = ((struct signatured_type *)
7178 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7179 return entry;
7180 }
348e048f 7181}
42e7ad6c
DE
7182\f
7183/* Low level DIE reading support. */
348e048f 7184
d85a05f0
DJ
7185/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7186
7187static void
7188init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7189 struct dwarf2_cu *cu,
3019eac3 7190 struct dwarf2_section_info *section,
685af9cd
TT
7191 struct dwo_file *dwo_file,
7192 struct abbrev_table *abbrev_table)
d85a05f0 7193{
fceca515 7194 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7195 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7196 reader->cu = cu;
3019eac3 7197 reader->dwo_file = dwo_file;
dee91e82
DE
7198 reader->die_section = section;
7199 reader->buffer = section->buffer;
f664829e 7200 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7201 reader->comp_dir = NULL;
685af9cd 7202 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7203}
7204
b0c7bfa9
DE
7205/* Subroutine of init_cutu_and_read_dies to simplify it.
7206 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7207 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7208 already.
7209
7210 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7211 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7212 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7213 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7214 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7215 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7216 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7217 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7218 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7219 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7220 kept around for at least as long as *RESULT_READER.
7221
b0c7bfa9
DE
7222 The result is non-zero if a valid (non-dummy) DIE was found. */
7223
7224static int
7225read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7226 struct dwo_unit *dwo_unit,
b0c7bfa9 7227 struct die_info *stub_comp_unit_die,
a2ce51a0 7228 const char *stub_comp_dir,
b0c7bfa9 7229 struct die_reader_specs *result_reader,
d521ce57 7230 const gdb_byte **result_info_ptr,
b0c7bfa9 7231 struct die_info **result_comp_unit_die,
685af9cd
TT
7232 int *result_has_children,
7233 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7234{
ed2dc618 7235 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7236 struct objfile *objfile = dwarf2_per_objfile->objfile;
7237 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7238 bfd *abfd;
d521ce57 7239 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7240 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7241 int i,num_extra_attrs;
7242 struct dwarf2_section_info *dwo_abbrev_section;
7243 struct attribute *attr;
7244 struct die_info *comp_unit_die;
7245
b0aeadb3
DE
7246 /* At most one of these may be provided. */
7247 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7248
b0c7bfa9
DE
7249 /* These attributes aren't processed until later:
7250 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7251 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7252 referenced later. However, these attributes are found in the stub
7253 which we won't have later. In order to not impose this complication
7254 on the rest of the code, we read them here and copy them to the
7255 DWO CU/TU die. */
b0c7bfa9
DE
7256
7257 stmt_list = NULL;
7258 low_pc = NULL;
7259 high_pc = NULL;
7260 ranges = NULL;
7261 comp_dir = NULL;
7262
7263 if (stub_comp_unit_die != NULL)
7264 {
7265 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7266 DWO file. */
7267 if (! this_cu->is_debug_types)
7268 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7269 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7270 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7271 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7272 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7273
7274 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7275 We need the value before we can process DW_FORM_GNU_addr_index
7276 or DW_FORM_addrx. */
b0c7bfa9
DE
7277 cu->addr_base = 0;
7278 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
435d3d88 7279 if (attr != nullptr)
b0c7bfa9
DE
7280 cu->addr_base = DW_UNSND (attr);
7281
7282 /* There should be a DW_AT_ranges_base attribute here (if needed).
7283 We need the value before we can process DW_AT_ranges. */
7284 cu->ranges_base = 0;
7285 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
435d3d88 7286 if (attr != nullptr)
b0c7bfa9
DE
7287 cu->ranges_base = DW_UNSND (attr);
7288 }
a2ce51a0
DE
7289 else if (stub_comp_dir != NULL)
7290 {
7291 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7292 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7293 comp_dir->name = DW_AT_comp_dir;
7294 comp_dir->form = DW_FORM_string;
7295 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7296 DW_STRING (comp_dir) = stub_comp_dir;
7297 }
b0c7bfa9
DE
7298
7299 /* Set up for reading the DWO CU/TU. */
7300 cu->dwo_unit = dwo_unit;
685af9cd 7301 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7302 dwarf2_read_section (objfile, section);
a32a8923 7303 abfd = get_section_bfd_owner (section);
9c541725
PA
7304 begin_info_ptr = info_ptr = (section->buffer
7305 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7306 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7307
7308 if (this_cu->is_debug_types)
7309 {
b0c7bfa9
DE
7310 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7311
ed2dc618
SM
7312 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7313 &cu->header, section,
b0c7bfa9 7314 dwo_abbrev_section,
43988095 7315 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7316 /* This is not an assert because it can be caused by bad debug info. */
43988095 7317 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7318 {
7319 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7320 " TU at offset %s [in module %s]"),
a2ce51a0 7321 hex_string (sig_type->signature),
43988095 7322 hex_string (cu->header.signature),
9d8780f0 7323 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7324 bfd_get_filename (abfd));
7325 }
9c541725 7326 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7327 /* For DWOs coming from DWP files, we don't know the CU length
7328 nor the type's offset in the TU until now. */
7329 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7330 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7331
7332 /* Establish the type offset that can be used to lookup the type.
7333 For DWO files, we don't know it until now. */
9c541725
PA
7334 sig_type->type_offset_in_section
7335 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7336 }
7337 else
7338 {
ed2dc618
SM
7339 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7340 &cu->header, section,
b0c7bfa9 7341 dwo_abbrev_section,
43988095 7342 info_ptr, rcuh_kind::COMPILE);
9c541725 7343 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7344 /* For DWOs coming from DWP files, we don't know the CU length
7345 until now. */
7346 dwo_unit->length = get_cu_length (&cu->header);
7347 }
7348
685af9cd
TT
7349 *result_dwo_abbrev_table
7350 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7351 cu->header.abbrev_sect_off);
7352 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7353 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7354
7355 /* Read in the die, but leave space to copy over the attributes
7356 from the stub. This has the benefit of simplifying the rest of
7357 the code - all the work to maintain the illusion of a single
7358 DW_TAG_{compile,type}_unit DIE is done here. */
7359 num_extra_attrs = ((stmt_list != NULL)
7360 + (low_pc != NULL)
7361 + (high_pc != NULL)
7362 + (ranges != NULL)
7363 + (comp_dir != NULL));
7364 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7365 result_has_children, num_extra_attrs);
7366
7367 /* Copy over the attributes from the stub to the DIE we just read in. */
7368 comp_unit_die = *result_comp_unit_die;
7369 i = comp_unit_die->num_attrs;
7370 if (stmt_list != NULL)
7371 comp_unit_die->attrs[i++] = *stmt_list;
7372 if (low_pc != NULL)
7373 comp_unit_die->attrs[i++] = *low_pc;
7374 if (high_pc != NULL)
7375 comp_unit_die->attrs[i++] = *high_pc;
7376 if (ranges != NULL)
7377 comp_unit_die->attrs[i++] = *ranges;
7378 if (comp_dir != NULL)
7379 comp_unit_die->attrs[i++] = *comp_dir;
7380 comp_unit_die->num_attrs += num_extra_attrs;
7381
b4f54984 7382 if (dwarf_die_debug)
bf6af496
DE
7383 {
7384 fprintf_unfiltered (gdb_stdlog,
7385 "Read die from %s@0x%x of %s:\n",
a32a8923 7386 get_section_name (section),
bf6af496
DE
7387 (unsigned) (begin_info_ptr - section->buffer),
7388 bfd_get_filename (abfd));
b4f54984 7389 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7390 }
7391
a2ce51a0
DE
7392 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7393 TUs by skipping the stub and going directly to the entry in the DWO file.
7394 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7395 to get it via circuitous means. Blech. */
7396 if (comp_dir != NULL)
7397 result_reader->comp_dir = DW_STRING (comp_dir);
7398
b0c7bfa9
DE
7399 /* Skip dummy compilation units. */
7400 if (info_ptr >= begin_info_ptr + dwo_unit->length
7401 || peek_abbrev_code (abfd, info_ptr) == 0)
7402 return 0;
7403
7404 *result_info_ptr = info_ptr;
7405 return 1;
7406}
7407
a084a2a6
AT
7408/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7409 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7410 signature is part of the header. */
7411static gdb::optional<ULONGEST>
7412lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7413{
7414 if (cu->header.version >= 5)
7415 return cu->header.signature;
7416 struct attribute *attr;
7417 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7418 if (attr == nullptr)
7419 return gdb::optional<ULONGEST> ();
7420 return DW_UNSND (attr);
7421}
7422
b0c7bfa9
DE
7423/* Subroutine of init_cutu_and_read_dies to simplify it.
7424 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7425 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7426
7427static struct dwo_unit *
7428lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7429 struct die_info *comp_unit_die)
7430{
7431 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7432 struct dwo_unit *dwo_unit;
7433 const char *comp_dir, *dwo_name;
7434
a2ce51a0
DE
7435 gdb_assert (cu != NULL);
7436
b0c7bfa9 7437 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7438 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7439 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7440
7441 if (this_cu->is_debug_types)
7442 {
7443 struct signatured_type *sig_type;
7444
7445 /* Since this_cu is the first member of struct signatured_type,
7446 we can go from a pointer to one to a pointer to the other. */
7447 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7448 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7449 }
7450 else
7451 {
a084a2a6
AT
7452 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7453 if (!signature.has_value ())
b0c7bfa9
DE
7454 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7455 " [in module %s]"),
e3b94546 7456 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7457 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7458 *signature);
b0c7bfa9
DE
7459 }
7460
b0c7bfa9
DE
7461 return dwo_unit;
7462}
7463
a2ce51a0 7464/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7465 See it for a description of the parameters.
fcd3b13d 7466 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7467
7468static void
6aa5f3a6
DE
7469init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7470 int use_existing_cu, int keep,
a2ce51a0
DE
7471 die_reader_func_ftype *die_reader_func,
7472 void *data)
7473{
fcd3b13d 7474 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7475 struct signatured_type *sig_type;
a2ce51a0
DE
7476 struct die_reader_specs reader;
7477 const gdb_byte *info_ptr;
7478 struct die_info *comp_unit_die;
7479 int has_children;
ed2dc618 7480 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7481
7482 /* Verify we can do the following downcast, and that we have the
7483 data we need. */
7484 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7485 sig_type = (struct signatured_type *) this_cu;
7486 gdb_assert (sig_type->dwo_unit != NULL);
7487
6aa5f3a6
DE
7488 if (use_existing_cu && this_cu->cu != NULL)
7489 {
7490 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7491 /* There's no need to do the rereading_dwo_cu handling that
7492 init_cutu_and_read_dies does since we don't read the stub. */
7493 }
7494 else
7495 {
7496 /* If !use_existing_cu, this_cu->cu must be NULL. */
7497 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7498 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7499 }
7500
7501 /* A future optimization, if needed, would be to use an existing
7502 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7503 could share abbrev tables. */
a2ce51a0 7504
685af9cd
TT
7505 /* The abbreviation table used by READER, this must live at least as long as
7506 READER. */
7507 abbrev_table_up dwo_abbrev_table;
7508
a2ce51a0 7509 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7510 NULL /* stub_comp_unit_die */,
7511 sig_type->dwo_unit->dwo_file->comp_dir,
7512 &reader, &info_ptr,
685af9cd
TT
7513 &comp_unit_die, &has_children,
7514 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7515 {
7516 /* Dummy die. */
a2ce51a0
DE
7517 return;
7518 }
7519
7520 /* All the "real" work is done here. */
7521 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7522
6aa5f3a6 7523 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7524 but the alternative is making the latter more complex.
7525 This function is only for the special case of using DWO files directly:
7526 no point in overly complicating the general case just to handle this. */
fcd3b13d 7527 if (new_cu != NULL && keep)
a2ce51a0 7528 {
fcd3b13d
SM
7529 /* Link this CU into read_in_chain. */
7530 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7531 dwarf2_per_objfile->read_in_chain = this_cu;
7532 /* The chain owns it now. */
7533 new_cu.release ();
a2ce51a0 7534 }
a2ce51a0
DE
7535}
7536
fd820528 7537/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7538 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7539
f4dc4d17
DE
7540 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7541 Otherwise the table specified in the comp unit header is read in and used.
7542 This is an optimization for when we already have the abbrev table.
7543
dee91e82
DE
7544 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7545 Otherwise, a new CU is allocated with xmalloc.
7546
7547 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7548 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7549
7550 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7551 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7552
70221824 7553static void
fd820528 7554init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7555 struct abbrev_table *abbrev_table,
fd820528 7556 int use_existing_cu, int keep,
58f0c718 7557 bool skip_partial,
fd820528
DE
7558 die_reader_func_ftype *die_reader_func,
7559 void *data)
c906108c 7560{
ed2dc618 7561 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7562 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7563 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7564 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7565 struct dwarf2_cu *cu;
d521ce57 7566 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7567 struct die_reader_specs reader;
d85a05f0 7568 struct die_info *comp_unit_die;
dee91e82 7569 int has_children;
dee91e82 7570 struct signatured_type *sig_type = NULL;
4bdcc0c1 7571 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7572 /* Non-zero if CU currently points to a DWO file and we need to
7573 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7574 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7575 int rereading_dwo_cu = 0;
c906108c 7576
b4f54984 7577 if (dwarf_die_debug)
9d8780f0 7578 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7579 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7580 sect_offset_str (this_cu->sect_off));
09406207 7581
dee91e82
DE
7582 if (use_existing_cu)
7583 gdb_assert (keep);
23745b47 7584
a2ce51a0
DE
7585 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7586 file (instead of going through the stub), short-circuit all of this. */
7587 if (this_cu->reading_dwo_directly)
7588 {
7589 /* Narrow down the scope of possibilities to have to understand. */
7590 gdb_assert (this_cu->is_debug_types);
7591 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7592 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7593 die_reader_func, data);
a2ce51a0
DE
7594 return;
7595 }
7596
dee91e82
DE
7597 /* This is cheap if the section is already read in. */
7598 dwarf2_read_section (objfile, section);
7599
9c541725 7600 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7601
7602 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7603
fcd3b13d 7604 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7605 if (use_existing_cu && this_cu->cu != NULL)
7606 {
7607 cu = this_cu->cu;
42e7ad6c
DE
7608 /* If this CU is from a DWO file we need to start over, we need to
7609 refetch the attributes from the skeleton CU.
7610 This could be optimized by retrieving those attributes from when we
7611 were here the first time: the previous comp_unit_die was stored in
7612 comp_unit_obstack. But there's no data yet that we need this
7613 optimization. */
7614 if (cu->dwo_unit != NULL)
7615 rereading_dwo_cu = 1;
dee91e82
DE
7616 }
7617 else
7618 {
7619 /* If !use_existing_cu, this_cu->cu must be NULL. */
7620 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7621 new_cu.reset (new dwarf2_cu (this_cu));
7622 cu = new_cu.get ();
42e7ad6c 7623 }
dee91e82 7624
b0c7bfa9 7625 /* Get the header. */
9c541725 7626 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7627 {
7628 /* We already have the header, there's no need to read it in again. */
9c541725 7629 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7630 }
7631 else
7632 {
3019eac3 7633 if (this_cu->is_debug_types)
dee91e82 7634 {
ed2dc618
SM
7635 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7636 &cu->header, section,
4bdcc0c1 7637 abbrev_section, info_ptr,
43988095 7638 rcuh_kind::TYPE);
dee91e82 7639
42e7ad6c
DE
7640 /* Since per_cu is the first member of struct signatured_type,
7641 we can go from a pointer to one to a pointer to the other. */
7642 sig_type = (struct signatured_type *) this_cu;
43988095 7643 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7644 gdb_assert (sig_type->type_offset_in_tu
7645 == cu->header.type_cu_offset_in_tu);
7646 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7647
42e7ad6c
DE
7648 /* LENGTH has not been set yet for type units if we're
7649 using .gdb_index. */
1ce1cefd 7650 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7651
7652 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7653 sig_type->type_offset_in_section =
7654 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7655
7656 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7657 }
7658 else
7659 {
ed2dc618
SM
7660 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7661 &cu->header, section,
4bdcc0c1 7662 abbrev_section,
43988095
JK
7663 info_ptr,
7664 rcuh_kind::COMPILE);
dee91e82 7665
9c541725 7666 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7667 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7668 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7669 }
7670 }
10b3939b 7671
6caca83c 7672 /* Skip dummy compilation units. */
dee91e82 7673 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7674 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7675 return;
6caca83c 7676
433df2d4
DE
7677 /* If we don't have them yet, read the abbrevs for this compilation unit.
7678 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7679 done (own the table through ABBREV_TABLE_HOLDER). */
7680 abbrev_table_up abbrev_table_holder;
f4dc4d17 7681 if (abbrev_table != NULL)
685af9cd
TT
7682 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7683 else
f4dc4d17 7684 {
685af9cd
TT
7685 abbrev_table_holder
7686 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7687 cu->header.abbrev_sect_off);
7688 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7689 }
af703f96 7690
dee91e82 7691 /* Read the top level CU/TU die. */
685af9cd 7692 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7693 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7694
58f0c718
TT
7695 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7696 return;
7697
b0c7bfa9 7698 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7699 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7700 table from the DWO file and pass the ownership over to us. It will be
7701 referenced from READER, so we must make sure to free it after we're done
7702 with READER.
7703
b0c7bfa9
DE
7704 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7705 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7706 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7707 abbrev_table_up dwo_abbrev_table;
a084a2a6 7708 if (dwo_name != nullptr)
3019eac3 7709 {
3019eac3 7710 struct dwo_unit *dwo_unit;
b0c7bfa9 7711 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7712
7713 if (has_children)
6a506a2d 7714 {
b98664d3 7715 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7716 " has children (offset %s) [in module %s]"),
7717 sect_offset_str (this_cu->sect_off),
7718 bfd_get_filename (abfd));
6a506a2d 7719 }
b0c7bfa9 7720 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7721 if (dwo_unit != NULL)
3019eac3 7722 {
6a506a2d 7723 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7724 comp_unit_die, NULL,
6a506a2d 7725 &reader, &info_ptr,
685af9cd
TT
7726 &dwo_comp_unit_die, &has_children,
7727 &dwo_abbrev_table) == 0)
6a506a2d
DE
7728 {
7729 /* Dummy die. */
6a506a2d
DE
7730 return;
7731 }
7732 comp_unit_die = dwo_comp_unit_die;
7733 }
7734 else
7735 {
7736 /* Yikes, we couldn't find the rest of the DIE, we only have
7737 the stub. A complaint has already been logged. There's
7738 not much more we can do except pass on the stub DIE to
7739 die_reader_func. We don't want to throw an error on bad
7740 debug info. */
3019eac3
DE
7741 }
7742 }
7743
b0c7bfa9 7744 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7745 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7746
b0c7bfa9 7747 /* Done, clean up. */
fcd3b13d 7748 if (new_cu != NULL && keep)
348e048f 7749 {
fcd3b13d
SM
7750 /* Link this CU into read_in_chain. */
7751 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7752 dwarf2_per_objfile->read_in_chain = this_cu;
7753 /* The chain owns it now. */
7754 new_cu.release ();
348e048f 7755 }
dee91e82
DE
7756}
7757
33e80786
DE
7758/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7759 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7760 to have already done the lookup to find the DWO file).
dee91e82
DE
7761
7762 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7763 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7764
7765 We fill in THIS_CU->length.
7766
7767 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7768 linker) then DIE_READER_FUNC will not get called.
7769
7770 THIS_CU->cu is always freed when done.
3019eac3
DE
7771 This is done in order to not leave THIS_CU->cu in a state where we have
7772 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7773
7774static void
7775init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7776 struct dwo_file *dwo_file,
dee91e82
DE
7777 die_reader_func_ftype *die_reader_func,
7778 void *data)
7779{
ed2dc618 7780 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7781 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7782 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7783 bfd *abfd = get_section_bfd_owner (section);
33e80786 7784 struct dwarf2_section_info *abbrev_section;
d521ce57 7785 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7786 struct die_reader_specs reader;
dee91e82
DE
7787 struct die_info *comp_unit_die;
7788 int has_children;
7789
b4f54984 7790 if (dwarf_die_debug)
9d8780f0 7791 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7792 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7793 sect_offset_str (this_cu->sect_off));
09406207 7794
dee91e82
DE
7795 gdb_assert (this_cu->cu == NULL);
7796
33e80786
DE
7797 abbrev_section = (dwo_file != NULL
7798 ? &dwo_file->sections.abbrev
7799 : get_abbrev_section_for_cu (this_cu));
7800
dee91e82
DE
7801 /* This is cheap if the section is already read in. */
7802 dwarf2_read_section (objfile, section);
7803
fcd3b13d 7804 struct dwarf2_cu cu (this_cu);
dee91e82 7805
9c541725 7806 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7807 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7808 &cu.header, section,
4bdcc0c1 7809 abbrev_section, info_ptr,
43988095
JK
7810 (this_cu->is_debug_types
7811 ? rcuh_kind::TYPE
7812 : rcuh_kind::COMPILE));
dee91e82 7813
1ce1cefd 7814 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7815
7816 /* Skip dummy compilation units. */
7817 if (info_ptr >= begin_info_ptr + this_cu->length
7818 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7819 return;
72bf9492 7820
685af9cd
TT
7821 abbrev_table_up abbrev_table
7822 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7823 cu.header.abbrev_sect_off);
dee91e82 7824
685af9cd 7825 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7826 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7827
7828 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7829}
7830
3019eac3
DE
7831/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7832 does not lookup the specified DWO file.
7833 This cannot be used to read DWO files.
dee91e82
DE
7834
7835 THIS_CU->cu is always freed when done.
3019eac3
DE
7836 This is done in order to not leave THIS_CU->cu in a state where we have
7837 to care whether it refers to the "main" CU or the DWO CU.
7838 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7839
7840static void
7841init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7842 die_reader_func_ftype *die_reader_func,
7843 void *data)
7844{
33e80786 7845 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7846}
0018ea6f
DE
7847\f
7848/* Type Unit Groups.
dee91e82 7849
0018ea6f
DE
7850 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7851 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7852 so that all types coming from the same compilation (.o file) are grouped
7853 together. A future step could be to put the types in the same symtab as
7854 the CU the types ultimately came from. */
ff013f42 7855
f4dc4d17
DE
7856static hashval_t
7857hash_type_unit_group (const void *item)
7858{
9a3c8263
SM
7859 const struct type_unit_group *tu_group
7860 = (const struct type_unit_group *) item;
f4dc4d17 7861
094b34ac 7862 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7863}
348e048f
DE
7864
7865static int
f4dc4d17 7866eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7867{
9a3c8263
SM
7868 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7869 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7870
094b34ac 7871 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7872}
348e048f 7873
f4dc4d17
DE
7874/* Allocate a hash table for type unit groups. */
7875
7876static htab_t
ed2dc618 7877allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7878{
7879 return htab_create_alloc_ex (3,
7880 hash_type_unit_group,
7881 eq_type_unit_group,
7882 NULL,
ed2dc618 7883 &objfile->objfile_obstack,
f4dc4d17
DE
7884 hashtab_obstack_allocate,
7885 dummy_obstack_deallocate);
7886}
dee91e82 7887
f4dc4d17
DE
7888/* Type units that don't have DW_AT_stmt_list are grouped into their own
7889 partial symtabs. We combine several TUs per psymtab to not let the size
7890 of any one psymtab grow too big. */
7891#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7892#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7893
094b34ac 7894/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7895 Create the type_unit_group object used to hold one or more TUs. */
7896
7897static struct type_unit_group *
094b34ac 7898create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7899{
518817b3
SM
7900 struct dwarf2_per_objfile *dwarf2_per_objfile
7901 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7902 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7903 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7904 struct type_unit_group *tu_group;
f4dc4d17
DE
7905
7906 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7907 struct type_unit_group);
094b34ac 7908 per_cu = &tu_group->per_cu;
518817b3 7909 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7910
094b34ac
DE
7911 if (dwarf2_per_objfile->using_index)
7912 {
7913 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7914 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7915 }
7916 else
7917 {
9c541725 7918 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7919 struct partial_symtab *pst;
528e1572 7920 std::string name;
094b34ac
DE
7921
7922 /* Give the symtab a useful name for debug purposes. */
7923 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7924 name = string_printf ("<type_units_%d>",
7925 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7926 else
528e1572 7927 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7928
528e1572 7929 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7930 pst->anonymous = 1;
094b34ac 7931 }
f4dc4d17 7932
094b34ac 7933 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7934 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7935
7936 return tu_group;
7937}
7938
094b34ac
DE
7939/* Look up the type_unit_group for type unit CU, and create it if necessary.
7940 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7941
7942static struct type_unit_group *
ff39bb5e 7943get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7944{
518817b3
SM
7945 struct dwarf2_per_objfile *dwarf2_per_objfile
7946 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7947 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7948 struct type_unit_group *tu_group;
7949 void **slot;
7950 unsigned int line_offset;
7951 struct type_unit_group type_unit_group_for_lookup;
7952
7953 if (dwarf2_per_objfile->type_unit_groups == NULL)
7954 {
7955 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7956 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7957 }
7958
7959 /* Do we need to create a new group, or can we use an existing one? */
7960
7961 if (stmt_list)
7962 {
7963 line_offset = DW_UNSND (stmt_list);
7964 ++tu_stats->nr_symtab_sharers;
7965 }
7966 else
7967 {
7968 /* Ugh, no stmt_list. Rare, but we have to handle it.
7969 We can do various things here like create one group per TU or
7970 spread them over multiple groups to split up the expansion work.
7971 To avoid worst case scenarios (too many groups or too large groups)
7972 we, umm, group them in bunches. */
7973 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7974 | (tu_stats->nr_stmt_less_type_units
7975 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7976 ++tu_stats->nr_stmt_less_type_units;
7977 }
7978
094b34ac 7979 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7980 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7981 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7982 &type_unit_group_for_lookup, INSERT);
7983 if (*slot != NULL)
7984 {
9a3c8263 7985 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7986 gdb_assert (tu_group != NULL);
7987 }
7988 else
7989 {
9c541725 7990 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7991 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7992 *slot = tu_group;
7993 ++tu_stats->nr_symtabs;
7994 }
7995
7996 return tu_group;
7997}
0018ea6f
DE
7998\f
7999/* Partial symbol tables. */
8000
8001/* Create a psymtab named NAME and assign it to PER_CU.
8002
8003 The caller must fill in the following details:
8004 dirname, textlow, texthigh. */
8005
8006static struct partial_symtab *
8007create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8008{
e3b94546 8009 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8010 struct partial_symtab *pst;
8011
939652a5 8012 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
8013
8014 pst->psymtabs_addrmap_supported = 1;
8015
8016 /* This is the glue that links PST into GDB's symbol API. */
8017 pst->read_symtab_private = per_cu;
8018 pst->read_symtab = dwarf2_read_symtab;
8019 per_cu->v.psymtab = pst;
8020
8021 return pst;
8022}
8023
b93601f3
TT
8024/* The DATA object passed to process_psymtab_comp_unit_reader has this
8025 type. */
8026
8027struct process_psymtab_comp_unit_data
8028{
8029 /* True if we are reading a DW_TAG_partial_unit. */
8030
8031 int want_partial_unit;
8032
8033 /* The "pretend" language that is used if the CU doesn't declare a
8034 language. */
8035
8036 enum language pretend_language;
8037};
8038
0018ea6f
DE
8039/* die_reader_func for process_psymtab_comp_unit. */
8040
8041static void
8042process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8043 const gdb_byte *info_ptr,
0018ea6f
DE
8044 struct die_info *comp_unit_die,
8045 int has_children,
8046 void *data)
8047{
8048 struct dwarf2_cu *cu = reader->cu;
518817b3 8049 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8050 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8051 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8052 CORE_ADDR baseaddr;
8053 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8054 struct partial_symtab *pst;
3a2b436a 8055 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8056 const char *filename;
9a3c8263
SM
8057 struct process_psymtab_comp_unit_data *info
8058 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8059
b93601f3 8060 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8061 return;
8062
8063 gdb_assert (! per_cu->is_debug_types);
8064
b93601f3 8065 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8066
0018ea6f 8067 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8068 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8069 if (filename == NULL)
0018ea6f 8070 filename = "";
0018ea6f
DE
8071
8072 pst = create_partial_symtab (per_cu, filename);
8073
8074 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8075 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8076
8077 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8078
8079 dwarf2_find_base_address (comp_unit_die, cu);
8080
8081 /* Possibly set the default values of LOWPC and HIGHPC from
8082 `DW_AT_ranges'. */
3a2b436a
JK
8083 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8084 &best_highpc, cu, pst);
8085 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8086 {
8087 CORE_ADDR low
8088 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8089 - baseaddr);
8090 CORE_ADDR high
8091 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8092 - baseaddr - 1);
8093 /* Store the contiguous range if it is not empty; it can be
8094 empty for CUs with no code. */
d320c2b5
TT
8095 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8096 low, high, pst);
79748972 8097 }
0018ea6f
DE
8098
8099 /* Check if comp unit has_children.
8100 If so, read the rest of the partial symbols from this comp unit.
8101 If not, there's no more debug_info for this comp unit. */
8102 if (has_children)
8103 {
8104 struct partial_die_info *first_die;
8105 CORE_ADDR lowpc, highpc;
8106
8107 lowpc = ((CORE_ADDR) -1);
8108 highpc = ((CORE_ADDR) 0);
8109
8110 first_die = load_partial_dies (reader, info_ptr, 1);
8111
8112 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8113 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8114
8115 /* If we didn't find a lowpc, set it to highpc to avoid
8116 complaints from `maint check'. */
8117 if (lowpc == ((CORE_ADDR) -1))
8118 lowpc = highpc;
8119
8120 /* If the compilation unit didn't have an explicit address range,
8121 then use the information extracted from its child dies. */
e385593e 8122 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8123 {
8124 best_lowpc = lowpc;
8125 best_highpc = highpc;
8126 }
8127 }
4ae976d1 8128 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8129 best_lowpc + baseaddr)
8130 - baseaddr);
4ae976d1 8131 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8132 best_highpc + baseaddr)
8133 - baseaddr);
0018ea6f 8134
8763cede 8135 end_psymtab_common (objfile, pst);
0018ea6f 8136
ae640021 8137 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8138 {
8139 int i;
ae640021 8140 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8141
8142 /* Fill in 'dependencies' here; we fill in 'users' in a
8143 post-pass. */
8144 pst->number_of_dependencies = len;
a9342b62
TT
8145 pst->dependencies
8146 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8147 for (i = 0; i < len; ++i)
8148 {
8149 pst->dependencies[i]
8150 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8151 }
0018ea6f 8152
ae640021 8153 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8154 }
8155
8156 /* Get the list of files included in the current compilation unit,
8157 and build a psymtab for each of them. */
8158 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8159
b4f54984 8160 if (dwarf_read_debug)
b926417a
TT
8161 fprintf_unfiltered (gdb_stdlog,
8162 "Psymtab for %s unit @%s: %s - %s"
8163 ", %d global, %d static syms\n",
8164 per_cu->is_debug_types ? "type" : "comp",
8165 sect_offset_str (per_cu->sect_off),
8166 paddress (gdbarch, pst->text_low (objfile)),
8167 paddress (gdbarch, pst->text_high (objfile)),
8168 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8169}
8170
8171/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8172 Process compilation unit THIS_CU for a psymtab. */
8173
8174static void
8175process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8176 int want_partial_unit,
8177 enum language pretend_language)
0018ea6f
DE
8178{
8179 /* If this compilation unit was already read in, free the
8180 cached copy in order to read it in again. This is
8181 necessary because we skipped some symbols when we first
8182 read in the compilation unit (see load_partial_dies).
8183 This problem could be avoided, but the benefit is unclear. */
8184 if (this_cu->cu != NULL)
8185 free_one_cached_comp_unit (this_cu);
8186
f1902523 8187 if (this_cu->is_debug_types)
58f0c718
TT
8188 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8189 build_type_psymtabs_reader, NULL);
f1902523
JK
8190 else
8191 {
8192 process_psymtab_comp_unit_data info;
8193 info.want_partial_unit = want_partial_unit;
8194 info.pretend_language = pretend_language;
58f0c718 8195 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8196 process_psymtab_comp_unit_reader, &info);
8197 }
0018ea6f
DE
8198
8199 /* Age out any secondary CUs. */
ed2dc618 8200 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8201}
f4dc4d17
DE
8202
8203/* Reader function for build_type_psymtabs. */
8204
8205static void
8206build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8207 const gdb_byte *info_ptr,
f4dc4d17
DE
8208 struct die_info *type_unit_die,
8209 int has_children,
8210 void *data)
8211{
ed2dc618 8212 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8213 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8214 struct objfile *objfile = dwarf2_per_objfile->objfile;
8215 struct dwarf2_cu *cu = reader->cu;
8216 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8217 struct signatured_type *sig_type;
f4dc4d17
DE
8218 struct type_unit_group *tu_group;
8219 struct attribute *attr;
8220 struct partial_die_info *first_die;
8221 CORE_ADDR lowpc, highpc;
8222 struct partial_symtab *pst;
8223
8224 gdb_assert (data == NULL);
0186c6a7
DE
8225 gdb_assert (per_cu->is_debug_types);
8226 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8227
8228 if (! has_children)
8229 return;
8230
8231 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8232 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8233
df07e2c7 8234 if (tu_group->tus == nullptr)
a8b3b8e9 8235 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8236 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8237
8238 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8239 pst = create_partial_symtab (per_cu, "");
8240 pst->anonymous = 1;
8241
8242 first_die = load_partial_dies (reader, info_ptr, 1);
8243
8244 lowpc = (CORE_ADDR) -1;
8245 highpc = (CORE_ADDR) 0;
8246 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8247
8763cede 8248 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8249}
8250
73051182
DE
8251/* Struct used to sort TUs by their abbreviation table offset. */
8252
8253struct tu_abbrev_offset
8254{
b2bdb8cf
SM
8255 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8256 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8257 {}
8258
8259 signatured_type *sig_type;
73051182
DE
8260 sect_offset abbrev_offset;
8261};
8262
484cf504 8263/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8264
484cf504
TT
8265static bool
8266sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8267 const struct tu_abbrev_offset &b)
73051182 8268{
484cf504 8269 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8270}
8271
8272/* Efficiently read all the type units.
8273 This does the bulk of the work for build_type_psymtabs.
8274
8275 The efficiency is because we sort TUs by the abbrev table they use and
8276 only read each abbrev table once. In one program there are 200K TUs
8277 sharing 8K abbrev tables.
8278
8279 The main purpose of this function is to support building the
8280 dwarf2_per_objfile->type_unit_groups table.
8281 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8282 can collapse the search space by grouping them by stmt_list.
8283 The savings can be significant, in the same program from above the 200K TUs
8284 share 8K stmt_list tables.
8285
8286 FUNC is expected to call get_type_unit_group, which will create the
8287 struct type_unit_group if necessary and add it to
8288 dwarf2_per_objfile->type_unit_groups. */
8289
8290static void
ed2dc618 8291build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8292{
73051182 8293 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8294 abbrev_table_up abbrev_table;
73051182 8295 sect_offset abbrev_offset;
73051182
DE
8296
8297 /* It's up to the caller to not call us multiple times. */
8298 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8299
b2bdb8cf 8300 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8301 return;
8302
8303 /* TUs typically share abbrev tables, and there can be way more TUs than
8304 abbrev tables. Sort by abbrev table to reduce the number of times we
8305 read each abbrev table in.
8306 Alternatives are to punt or to maintain a cache of abbrev tables.
8307 This is simpler and efficient enough for now.
8308
8309 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8310 symtab to use). Typically TUs with the same abbrev offset have the same
8311 stmt_list value too so in practice this should work well.
8312
8313 The basic algorithm here is:
8314
8315 sort TUs by abbrev table
8316 for each TU with same abbrev table:
8317 read abbrev table if first user
8318 read TU top level DIE
8319 [IWBN if DWO skeletons had DW_AT_stmt_list]
8320 call FUNC */
8321
b4f54984 8322 if (dwarf_read_debug)
73051182
DE
8323 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8324
8325 /* Sort in a separate table to maintain the order of all_type_units
8326 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8327 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8328 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8329
8330 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8331 sorted_by_abbrev.emplace_back
8332 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8333 sig_type->per_cu.section,
8334 sig_type->per_cu.sect_off));
73051182 8335
484cf504
TT
8336 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8337 sort_tu_by_abbrev_offset);
73051182 8338
9c541725 8339 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8340
b2bdb8cf 8341 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8342 {
73051182
DE
8343 /* Switch to the next abbrev table if necessary. */
8344 if (abbrev_table == NULL
b2bdb8cf 8345 || tu.abbrev_offset != abbrev_offset)
73051182 8346 {
b2bdb8cf 8347 abbrev_offset = tu.abbrev_offset;
73051182 8348 abbrev_table =
ed2dc618
SM
8349 abbrev_table_read_table (dwarf2_per_objfile,
8350 &dwarf2_per_objfile->abbrev,
73051182
DE
8351 abbrev_offset);
8352 ++tu_stats->nr_uniq_abbrev_tables;
8353 }
8354
b2bdb8cf 8355 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8356 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8357 }
6aa5f3a6 8358}
73051182 8359
6aa5f3a6
DE
8360/* Print collected type unit statistics. */
8361
8362static void
ed2dc618 8363print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8364{
8365 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8366
8367 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8368 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8369 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8370 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8371 tu_stats->nr_uniq_abbrev_tables);
8372 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8373 tu_stats->nr_symtabs);
8374 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8375 tu_stats->nr_symtab_sharers);
8376 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8377 tu_stats->nr_stmt_less_type_units);
8378 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8379 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8380}
8381
f4dc4d17
DE
8382/* Traversal function for build_type_psymtabs. */
8383
8384static int
8385build_type_psymtab_dependencies (void **slot, void *info)
8386{
ed2dc618
SM
8387 struct dwarf2_per_objfile *dwarf2_per_objfile
8388 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8389 struct objfile *objfile = dwarf2_per_objfile->objfile;
8390 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8391 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8392 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8393 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8394 int i;
8395
8396 gdb_assert (len > 0);
0186c6a7 8397 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8398
8399 pst->number_of_dependencies = len;
a9342b62 8400 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8401 for (i = 0; i < len; ++i)
f4dc4d17 8402 {
df07e2c7 8403 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8404 gdb_assert (iter->per_cu.is_debug_types);
8405 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8406 iter->type_unit_group = tu_group;
f4dc4d17
DE
8407 }
8408
df07e2c7
AB
8409 delete tu_group->tus;
8410 tu_group->tus = nullptr;
348e048f
DE
8411
8412 return 1;
8413}
8414
8415/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8416 Build partial symbol tables for the .debug_types comp-units. */
8417
8418static void
ed2dc618 8419build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8420{
ed2dc618 8421 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8422 return;
8423
ed2dc618 8424 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8425}
f4dc4d17 8426
6aa5f3a6
DE
8427/* Traversal function for process_skeletonless_type_unit.
8428 Read a TU in a DWO file and build partial symbols for it. */
8429
8430static int
8431process_skeletonless_type_unit (void **slot, void *info)
8432{
8433 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8434 struct dwarf2_per_objfile *dwarf2_per_objfile
8435 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8436 struct signatured_type find_entry, *entry;
8437
8438 /* If this TU doesn't exist in the global table, add it and read it in. */
8439
8440 if (dwarf2_per_objfile->signatured_types == NULL)
8441 {
8442 dwarf2_per_objfile->signatured_types
ed2dc618 8443 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8444 }
8445
8446 find_entry.signature = dwo_unit->signature;
8447 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8448 INSERT);
8449 /* If we've already seen this type there's nothing to do. What's happening
8450 is we're doing our own version of comdat-folding here. */
8451 if (*slot != NULL)
8452 return 1;
8453
8454 /* This does the job that create_all_type_units would have done for
8455 this TU. */
ed2dc618
SM
8456 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8457 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8458 *slot = entry;
8459
8460 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8461 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8462 build_type_psymtabs_reader, NULL);
8463
8464 return 1;
8465}
8466
8467/* Traversal function for process_skeletonless_type_units. */
8468
8469static int
8470process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8471{
8472 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8473
8474 if (dwo_file->tus != NULL)
8475 {
8476 htab_traverse_noresize (dwo_file->tus,
8477 process_skeletonless_type_unit, info);
8478 }
8479
8480 return 1;
8481}
8482
8483/* Scan all TUs of DWO files, verifying we've processed them.
8484 This is needed in case a TU was emitted without its skeleton.
8485 Note: This can't be done until we know what all the DWO files are. */
8486
8487static void
ed2dc618 8488process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8489{
8490 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8491 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8492 && dwarf2_per_objfile->dwo_files != NULL)
8493 {
51ac9db5 8494 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8495 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8496 dwarf2_per_objfile);
6aa5f3a6 8497 }
348e048f
DE
8498}
8499
ed2dc618 8500/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8501
8502static void
ed2dc618 8503set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8504{
b76e467d 8505 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8506 {
95554aad 8507 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8508
36586728
TT
8509 if (pst == NULL)
8510 continue;
8511
b76e467d 8512 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8513 {
8514 /* Set the 'user' field only if it is not already set. */
8515 if (pst->dependencies[j]->user == NULL)
8516 pst->dependencies[j]->user = pst;
8517 }
8518 }
8519}
8520
93311388
DE
8521/* Build the partial symbol table by doing a quick pass through the
8522 .debug_info and .debug_abbrev sections. */
72bf9492 8523
93311388 8524static void
ed2dc618 8525dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8526{
ed2dc618 8527 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8528
b4f54984 8529 if (dwarf_read_debug)
45cfd468
DE
8530 {
8531 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8532 objfile_name (objfile));
45cfd468
DE
8533 }
8534
98bfdba5
PA
8535 dwarf2_per_objfile->reading_partial_symbols = 1;
8536
be391dca 8537 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8538
93311388
DE
8539 /* Any cached compilation units will be linked by the per-objfile
8540 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8541 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8542
ed2dc618 8543 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8544
ed2dc618 8545 create_all_comp_units (dwarf2_per_objfile);
c906108c 8546
60606b2c
TT
8547 /* Create a temporary address map on a temporary obstack. We later
8548 copy this to the final obstack. */
8268c778 8549 auto_obstack temp_obstack;
791afaa2
TT
8550
8551 scoped_restore save_psymtabs_addrmap
d320c2b5 8552 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8553 addrmap_create_mutable (&temp_obstack));
72bf9492 8554
b76e467d
SM
8555 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8556 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8557
6aa5f3a6 8558 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8559 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8560
8561 /* Now that all TUs have been processed we can fill in the dependencies. */
8562 if (dwarf2_per_objfile->type_unit_groups != NULL)
8563 {
8564 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8565 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8566 }
8567
b4f54984 8568 if (dwarf_read_debug)
ed2dc618 8569 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8570
ed2dc618 8571 set_partial_user (dwarf2_per_objfile);
95554aad 8572
d320c2b5
TT
8573 objfile->partial_symtabs->psymtabs_addrmap
8574 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8575 objfile->partial_symtabs->obstack ());
791afaa2
TT
8576 /* At this point we want to keep the address map. */
8577 save_psymtabs_addrmap.release ();
ff013f42 8578
b4f54984 8579 if (dwarf_read_debug)
45cfd468 8580 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8581 objfile_name (objfile));
ae038cb0
DJ
8582}
8583
3019eac3 8584/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8585
8586static void
dee91e82 8587load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8588 const gdb_byte *info_ptr,
dee91e82
DE
8589 struct die_info *comp_unit_die,
8590 int has_children,
8591 void *data)
ae038cb0 8592{
dee91e82 8593 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8594
95554aad 8595 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8596
ae038cb0
DJ
8597 /* Check if comp unit has_children.
8598 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8599 If not, there's no more debug_info for this comp unit. */
d85a05f0 8600 if (has_children)
dee91e82
DE
8601 load_partial_dies (reader, info_ptr, 0);
8602}
98bfdba5 8603
dee91e82
DE
8604/* Load the partial DIEs for a secondary CU into memory.
8605 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8606
dee91e82
DE
8607static void
8608load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8609{
58f0c718 8610 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8611 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8612}
8613
ae038cb0 8614static void
ed2dc618 8615read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8616 struct dwarf2_section_info *section,
f1902523 8617 struct dwarf2_section_info *abbrev_section,
b76e467d 8618 unsigned int is_dwz)
ae038cb0 8619{
d521ce57 8620 const gdb_byte *info_ptr;
ed2dc618 8621 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8622
b4f54984 8623 if (dwarf_read_debug)
bf6af496 8624 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8625 get_section_name (section),
8626 get_section_file_name (section));
bf6af496 8627
36586728 8628 dwarf2_read_section (objfile, section);
ae038cb0 8629
36586728 8630 info_ptr = section->buffer;
6e70227d 8631
36586728 8632 while (info_ptr < section->buffer + section->size)
ae038cb0 8633 {
ae038cb0 8634 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8635
9c541725 8636 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8637
f1902523 8638 comp_unit_head cu_header;
ed2dc618
SM
8639 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8640 abbrev_section, info_ptr,
8641 rcuh_kind::COMPILE);
ae038cb0
DJ
8642
8643 /* Save the compilation unit for later lookup. */
f1902523
JK
8644 if (cu_header.unit_type != DW_UT_type)
8645 {
8646 this_cu = XOBNEW (&objfile->objfile_obstack,
8647 struct dwarf2_per_cu_data);
8648 memset (this_cu, 0, sizeof (*this_cu));
8649 }
8650 else
8651 {
8652 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8653 struct signatured_type);
8654 memset (sig_type, 0, sizeof (*sig_type));
8655 sig_type->signature = cu_header.signature;
8656 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8657 this_cu = &sig_type->per_cu;
8658 }
8659 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8660 this_cu->sect_off = sect_off;
f1902523 8661 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8662 this_cu->is_dwz = is_dwz;
e3b94546 8663 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8664 this_cu->section = section;
ae038cb0 8665
b76e467d 8666 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8667
8668 info_ptr = info_ptr + this_cu->length;
8669 }
36586728
TT
8670}
8671
8672/* Create a list of all compilation units in OBJFILE.
8673 This is only done for -readnow and building partial symtabs. */
8674
8675static void
ed2dc618 8676create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8677{
b76e467d 8678 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8679 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8680 &dwarf2_per_objfile->abbrev, 0);
36586728 8681
b76e467d 8682 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8683 if (dwz != NULL)
ed2dc618 8684 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8685 1);
c906108c
SS
8686}
8687
5734ee8b 8688/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8689 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8690 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8691 DW_AT_ranges). See the comments of add_partial_subprogram on how
8692 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8693
72bf9492
DJ
8694static void
8695scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8696 CORE_ADDR *highpc, int set_addrmap,
8697 struct dwarf2_cu *cu)
c906108c 8698{
72bf9492 8699 struct partial_die_info *pdi;
c906108c 8700
91c24f0a
DC
8701 /* Now, march along the PDI's, descending into ones which have
8702 interesting children but skipping the children of the other ones,
8703 until we reach the end of the compilation unit. */
c906108c 8704
72bf9492 8705 pdi = first_die;
91c24f0a 8706
72bf9492
DJ
8707 while (pdi != NULL)
8708 {
52356b79 8709 pdi->fixup (cu);
c906108c 8710
f55ee35c 8711 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8712 children, so we need to look at them. Ditto for anonymous
8713 enums. */
933c6fe4 8714
72bf9492 8715 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8716 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8717 || pdi->tag == DW_TAG_imported_unit
8718 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8719 {
72bf9492 8720 switch (pdi->tag)
c906108c
SS
8721 {
8722 case DW_TAG_subprogram:
b1dc1806 8723 case DW_TAG_inlined_subroutine:
cdc07690 8724 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8725 break;
72929c62 8726 case DW_TAG_constant:
c906108c
SS
8727 case DW_TAG_variable:
8728 case DW_TAG_typedef:
91c24f0a 8729 case DW_TAG_union_type:
72bf9492 8730 if (!pdi->is_declaration)
63d06c5c 8731 {
72bf9492 8732 add_partial_symbol (pdi, cu);
63d06c5c
DC
8733 }
8734 break;
c906108c 8735 case DW_TAG_class_type:
680b30c7 8736 case DW_TAG_interface_type:
c906108c 8737 case DW_TAG_structure_type:
72bf9492 8738 if (!pdi->is_declaration)
c906108c 8739 {
72bf9492 8740 add_partial_symbol (pdi, cu);
c906108c 8741 }
b7fee5a3
KS
8742 if ((cu->language == language_rust
8743 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8744 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8745 set_addrmap, cu);
c906108c 8746 break;
91c24f0a 8747 case DW_TAG_enumeration_type:
72bf9492
DJ
8748 if (!pdi->is_declaration)
8749 add_partial_enumeration (pdi, cu);
c906108c
SS
8750 break;
8751 case DW_TAG_base_type:
a02abb62 8752 case DW_TAG_subrange_type:
c906108c 8753 /* File scope base type definitions are added to the partial
c5aa993b 8754 symbol table. */
72bf9492 8755 add_partial_symbol (pdi, cu);
c906108c 8756 break;
d9fa45fe 8757 case DW_TAG_namespace:
cdc07690 8758 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8759 break;
5d7cb8df 8760 case DW_TAG_module:
59c35742
AB
8761 if (!pdi->is_declaration)
8762 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8763 break;
95554aad
TT
8764 case DW_TAG_imported_unit:
8765 {
8766 struct dwarf2_per_cu_data *per_cu;
8767
f4dc4d17
DE
8768 /* For now we don't handle imported units in type units. */
8769 if (cu->per_cu->is_debug_types)
8770 {
8771 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8772 " supported in type units [in module %s]"),
518817b3 8773 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8774 }
8775
e3b94546
SM
8776 per_cu = dwarf2_find_containing_comp_unit
8777 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8778 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8779
8780 /* Go read the partial unit, if needed. */
8781 if (per_cu->v.psymtab == NULL)
b93601f3 8782 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8783
ae640021 8784 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8785 }
8786 break;
74921315
KS
8787 case DW_TAG_imported_declaration:
8788 add_partial_symbol (pdi, cu);
8789 break;
c906108c
SS
8790 default:
8791 break;
8792 }
8793 }
8794
72bf9492
DJ
8795 /* If the die has a sibling, skip to the sibling. */
8796
8797 pdi = pdi->die_sibling;
8798 }
8799}
8800
8801/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8802
72bf9492 8803 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8804 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8805 Enumerators are an exception; they use the scope of their parent
8806 enumeration type, i.e. the name of the enumeration type is not
8807 prepended to the enumerator.
91c24f0a 8808
72bf9492
DJ
8809 There are two complexities. One is DW_AT_specification; in this
8810 case "parent" means the parent of the target of the specification,
8811 instead of the direct parent of the DIE. The other is compilers
8812 which do not emit DW_TAG_namespace; in this case we try to guess
8813 the fully qualified name of structure types from their members'
8814 linkage names. This must be done using the DIE's children rather
8815 than the children of any DW_AT_specification target. We only need
8816 to do this for structures at the top level, i.e. if the target of
8817 any DW_AT_specification (if any; otherwise the DIE itself) does not
8818 have a parent. */
8819
8820/* Compute the scope prefix associated with PDI's parent, in
8821 compilation unit CU. The result will be allocated on CU's
8822 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8823 field. NULL is returned if no prefix is necessary. */
15d034d0 8824static const char *
72bf9492
DJ
8825partial_die_parent_scope (struct partial_die_info *pdi,
8826 struct dwarf2_cu *cu)
8827{
15d034d0 8828 const char *grandparent_scope;
72bf9492 8829 struct partial_die_info *parent, *real_pdi;
91c24f0a 8830
72bf9492
DJ
8831 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8832 then this means the parent of the specification DIE. */
8833
8834 real_pdi = pdi;
72bf9492 8835 while (real_pdi->has_specification)
fb816e8b 8836 {
122cf0f2
AB
8837 auto res = find_partial_die (real_pdi->spec_offset,
8838 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8839 real_pdi = res.pdi;
8840 cu = res.cu;
8841 }
72bf9492
DJ
8842
8843 parent = real_pdi->die_parent;
8844 if (parent == NULL)
8845 return NULL;
8846
8847 if (parent->scope_set)
8848 return parent->scope;
8849
52356b79 8850 parent->fixup (cu);
72bf9492 8851
10b3939b 8852 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8853
acebe513
UW
8854 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8855 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8856 Work around this problem here. */
8857 if (cu->language == language_cplus
6e70227d 8858 && parent->tag == DW_TAG_namespace
acebe513
UW
8859 && strcmp (parent->name, "::") == 0
8860 && grandparent_scope == NULL)
8861 {
8862 parent->scope = NULL;
8863 parent->scope_set = 1;
8864 return NULL;
8865 }
8866
0a4b0913 8867 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8868 if (pdi->tag == DW_TAG_enumerator)
8869 /* Enumerators should not get the name of the enumeration as a prefix. */
8870 parent->scope = grandparent_scope;
8871 else if (parent->tag == DW_TAG_namespace
f55ee35c 8872 || parent->tag == DW_TAG_module
72bf9492
DJ
8873 || parent->tag == DW_TAG_structure_type
8874 || parent->tag == DW_TAG_class_type
680b30c7 8875 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8876 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8877 || parent->tag == DW_TAG_enumeration_type
8878 || (cu->language == language_fortran
8879 && parent->tag == DW_TAG_subprogram
8880 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8881 {
8882 if (grandparent_scope == NULL)
8883 parent->scope = parent->name;
8884 else
3e43a32a
MS
8885 parent->scope = typename_concat (&cu->comp_unit_obstack,
8886 grandparent_scope,
f55ee35c 8887 parent->name, 0, cu);
72bf9492 8888 }
72bf9492
DJ
8889 else
8890 {
8891 /* FIXME drow/2004-04-01: What should we be doing with
8892 function-local names? For partial symbols, we should probably be
8893 ignoring them. */
fa9c3fa0
TT
8894 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8895 dwarf_tag_name (parent->tag),
8896 sect_offset_str (pdi->sect_off));
72bf9492 8897 parent->scope = grandparent_scope;
c906108c
SS
8898 }
8899
72bf9492
DJ
8900 parent->scope_set = 1;
8901 return parent->scope;
8902}
8903
8904/* Return the fully scoped name associated with PDI, from compilation unit
8905 CU. The result will be allocated with malloc. */
4568ecf9 8906
72bf9492
DJ
8907static char *
8908partial_die_full_name (struct partial_die_info *pdi,
8909 struct dwarf2_cu *cu)
8910{
15d034d0 8911 const char *parent_scope;
72bf9492 8912
98bfdba5
PA
8913 /* If this is a template instantiation, we can not work out the
8914 template arguments from partial DIEs. So, unfortunately, we have
8915 to go through the full DIEs. At least any work we do building
8916 types here will be reused if full symbols are loaded later. */
8917 if (pdi->has_template_arguments)
8918 {
52356b79 8919 pdi->fixup (cu);
98bfdba5
PA
8920
8921 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8922 {
8923 struct die_info *die;
8924 struct attribute attr;
8925 struct dwarf2_cu *ref_cu = cu;
8926
b64f50a1 8927 /* DW_FORM_ref_addr is using section offset. */
b4069958 8928 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8929 attr.form = DW_FORM_ref_addr;
9c541725 8930 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8931 die = follow_die_ref (NULL, &attr, &ref_cu);
8932
8933 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8934 }
8935 }
8936
72bf9492
DJ
8937 parent_scope = partial_die_parent_scope (pdi, cu);
8938 if (parent_scope == NULL)
8939 return NULL;
8940 else
f55ee35c 8941 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8942}
8943
8944static void
72bf9492 8945add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8946{
518817b3
SM
8947 struct dwarf2_per_objfile *dwarf2_per_objfile
8948 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8949 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8950 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8951 CORE_ADDR addr = 0;
15d034d0 8952 const char *actual_name = NULL;
e142c38c 8953 CORE_ADDR baseaddr;
15d034d0 8954 char *built_actual_name;
e142c38c
DJ
8955
8956 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8957
15d034d0
TT
8958 built_actual_name = partial_die_full_name (pdi, cu);
8959 if (built_actual_name != NULL)
8960 actual_name = built_actual_name;
63d06c5c 8961
72bf9492
DJ
8962 if (actual_name == NULL)
8963 actual_name = pdi->name;
8964
c906108c
SS
8965 switch (pdi->tag)
8966 {
b1dc1806 8967 case DW_TAG_inlined_subroutine:
c906108c 8968 case DW_TAG_subprogram:
79748972
TT
8969 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8970 - baseaddr);
0a4b0913
AB
8971 if (pdi->is_external
8972 || cu->language == language_ada
8973 || (cu->language == language_fortran
8974 && pdi->die_parent != NULL
8975 && pdi->die_parent->tag == DW_TAG_subprogram))
8976 {
8977 /* Normally, only "external" DIEs are part of the global scope.
8978 But in Ada and Fortran, we want to be able to access nested
8979 procedures globally. So all Ada and Fortran subprograms are
8980 stored in the global scope. */
31edb802 8981 add_psymbol_to_list (actual_name,
15d034d0 8982 built_actual_name != NULL,
f47fb265 8983 VAR_DOMAIN, LOC_BLOCK,
79748972 8984 SECT_OFF_TEXT (objfile),
75aedd27 8985 psymbol_placement::GLOBAL,
79748972
TT
8986 addr,
8987 cu->language, objfile);
c906108c
SS
8988 }
8989 else
8990 {
31edb802 8991 add_psymbol_to_list (actual_name,
15d034d0 8992 built_actual_name != NULL,
f47fb265 8993 VAR_DOMAIN, LOC_BLOCK,
79748972 8994 SECT_OFF_TEXT (objfile),
75aedd27 8995 psymbol_placement::STATIC,
1762568f 8996 addr, cu->language, objfile);
c906108c 8997 }
0c1b455e
TT
8998
8999 if (pdi->main_subprogram && actual_name != NULL)
9000 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9001 break;
72929c62 9002 case DW_TAG_constant:
31edb802 9003 add_psymbol_to_list (actual_name,
75aedd27
TT
9004 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9005 -1, (pdi->is_external
9006 ? psymbol_placement::GLOBAL
9007 : psymbol_placement::STATIC),
9008 0, cu->language, objfile);
72929c62 9009 break;
c906108c 9010 case DW_TAG_variable:
95554aad
TT
9011 if (pdi->d.locdesc)
9012 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9013
95554aad 9014 if (pdi->d.locdesc
caac4577
JG
9015 && addr == 0
9016 && !dwarf2_per_objfile->has_section_at_zero)
9017 {
9018 /* A global or static variable may also have been stripped
9019 out by the linker if unused, in which case its address
9020 will be nullified; do not add such variables into partial
9021 symbol table then. */
9022 }
9023 else if (pdi->is_external)
c906108c
SS
9024 {
9025 /* Global Variable.
9026 Don't enter into the minimal symbol tables as there is
9027 a minimal symbol table entry from the ELF symbols already.
9028 Enter into partial symbol table if it has a location
9029 descriptor or a type.
9030 If the location descriptor is missing, new_symbol will create
9031 a LOC_UNRESOLVED symbol, the address of the variable will then
9032 be determined from the minimal symbol table whenever the variable
9033 is referenced.
9034 The address for the partial symbol table entry is not
9035 used by GDB, but it comes in handy for debugging partial symbol
9036 table building. */
9037
95554aad 9038 if (pdi->d.locdesc || pdi->has_type)
31edb802 9039 add_psymbol_to_list (actual_name,
15d034d0 9040 built_actual_name != NULL,
f47fb265 9041 VAR_DOMAIN, LOC_STATIC,
79748972 9042 SECT_OFF_TEXT (objfile),
75aedd27 9043 psymbol_placement::GLOBAL,
79748972 9044 addr, cu->language, objfile);
c906108c
SS
9045 }
9046 else
9047 {
ff908ebf
AW
9048 int has_loc = pdi->d.locdesc != NULL;
9049
9050 /* Static Variable. Skip symbols whose value we cannot know (those
9051 without location descriptors or constant values). */
9052 if (!has_loc && !pdi->has_const_value)
decbce07 9053 {
15d034d0 9054 xfree (built_actual_name);
decbce07
MS
9055 return;
9056 }
ff908ebf 9057
31edb802 9058 add_psymbol_to_list (actual_name,
15d034d0 9059 built_actual_name != NULL,
f47fb265 9060 VAR_DOMAIN, LOC_STATIC,
79748972 9061 SECT_OFF_TEXT (objfile),
75aedd27 9062 psymbol_placement::STATIC,
79748972 9063 has_loc ? addr : 0,
f47fb265 9064 cu->language, objfile);
c906108c
SS
9065 }
9066 break;
9067 case DW_TAG_typedef:
9068 case DW_TAG_base_type:
a02abb62 9069 case DW_TAG_subrange_type:
31edb802 9070 add_psymbol_to_list (actual_name,
15d034d0 9071 built_actual_name != NULL,
79748972 9072 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9073 psymbol_placement::STATIC,
1762568f 9074 0, cu->language, objfile);
c906108c 9075 break;
74921315 9076 case DW_TAG_imported_declaration:
72bf9492 9077 case DW_TAG_namespace:
31edb802 9078 add_psymbol_to_list (actual_name,
15d034d0 9079 built_actual_name != NULL,
79748972 9080 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9081 psymbol_placement::GLOBAL,
1762568f 9082 0, cu->language, objfile);
72bf9492 9083 break;
530e8392 9084 case DW_TAG_module:
a5fd13a9
BH
9085 /* With Fortran 77 there might be a "BLOCK DATA" module
9086 available without any name. If so, we skip the module as it
9087 doesn't bring any value. */
9088 if (actual_name != nullptr)
31edb802 9089 add_psymbol_to_list (actual_name,
a5fd13a9
BH
9090 built_actual_name != NULL,
9091 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9092 psymbol_placement::GLOBAL,
9093 0, cu->language, objfile);
530e8392 9094 break;
c906108c 9095 case DW_TAG_class_type:
680b30c7 9096 case DW_TAG_interface_type:
c906108c
SS
9097 case DW_TAG_structure_type:
9098 case DW_TAG_union_type:
9099 case DW_TAG_enumeration_type:
fa4028e9
JB
9100 /* Skip external references. The DWARF standard says in the section
9101 about "Structure, Union, and Class Type Entries": "An incomplete
9102 structure, union or class type is represented by a structure,
9103 union or class entry that does not have a byte size attribute
9104 and that has a DW_AT_declaration attribute." */
9105 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9106 {
15d034d0 9107 xfree (built_actual_name);
decbce07
MS
9108 return;
9109 }
fa4028e9 9110
63d06c5c
DC
9111 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9112 static vs. global. */
31edb802 9113 add_psymbol_to_list (actual_name,
15d034d0 9114 built_actual_name != NULL,
79748972 9115 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9116 cu->language == language_cplus
75aedd27
TT
9117 ? psymbol_placement::GLOBAL
9118 : psymbol_placement::STATIC,
1762568f 9119 0, cu->language, objfile);
c906108c 9120
c906108c
SS
9121 break;
9122 case DW_TAG_enumerator:
31edb802 9123 add_psymbol_to_list (actual_name,
15d034d0 9124 built_actual_name != NULL,
79748972 9125 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9126 cu->language == language_cplus
75aedd27
TT
9127 ? psymbol_placement::GLOBAL
9128 : psymbol_placement::STATIC,
1762568f 9129 0, cu->language, objfile);
c906108c
SS
9130 break;
9131 default:
9132 break;
9133 }
5c4e30ca 9134
15d034d0 9135 xfree (built_actual_name);
c906108c
SS
9136}
9137
5c4e30ca
DC
9138/* Read a partial die corresponding to a namespace; also, add a symbol
9139 corresponding to that namespace to the symbol table. NAMESPACE is
9140 the name of the enclosing namespace. */
91c24f0a 9141
72bf9492
DJ
9142static void
9143add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9144 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9145 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9146{
72bf9492 9147 /* Add a symbol for the namespace. */
e7c27a73 9148
72bf9492 9149 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9150
9151 /* Now scan partial symbols in that namespace. */
9152
91c24f0a 9153 if (pdi->has_children)
cdc07690 9154 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9155}
9156
5d7cb8df
JK
9157/* Read a partial die corresponding to a Fortran module. */
9158
9159static void
9160add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9161 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9162{
530e8392
KB
9163 /* Add a symbol for the namespace. */
9164
9165 add_partial_symbol (pdi, cu);
9166
f55ee35c 9167 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9168
9169 if (pdi->has_children)
cdc07690 9170 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9171}
9172
b1dc1806
XR
9173/* Read a partial die corresponding to a subprogram or an inlined
9174 subprogram and create a partial symbol for that subprogram.
9175 When the CU language allows it, this routine also defines a partial
9176 symbol for each nested subprogram that this subprogram contains.
9177 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9178 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9179
cdc07690
YQ
9180 PDI may also be a lexical block, in which case we simply search
9181 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9182 Again, this is only performed when the CU language allows this
9183 type of definitions. */
9184
9185static void
9186add_partial_subprogram (struct partial_die_info *pdi,
9187 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9188 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9189{
b1dc1806 9190 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9191 {
9192 if (pdi->has_pc_info)
9193 {
9194 if (pdi->lowpc < *lowpc)
9195 *lowpc = pdi->lowpc;
9196 if (pdi->highpc > *highpc)
9197 *highpc = pdi->highpc;
cdc07690 9198 if (set_addrmap)
5734ee8b 9199 {
518817b3 9200 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9201 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9202 CORE_ADDR baseaddr;
b926417a
TT
9203 CORE_ADDR this_highpc;
9204 CORE_ADDR this_lowpc;
5734ee8b
DJ
9205
9206 baseaddr = ANOFFSET (objfile->section_offsets,
9207 SECT_OFF_TEXT (objfile));
b926417a
TT
9208 this_lowpc
9209 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9210 pdi->lowpc + baseaddr)
9211 - baseaddr);
9212 this_highpc
9213 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9214 pdi->highpc + baseaddr)
9215 - baseaddr);
d320c2b5 9216 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9217 this_lowpc, this_highpc - 1,
9291a0cd 9218 cu->per_cu->v.psymtab);
5734ee8b 9219 }
481860b3
GB
9220 }
9221
9222 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9223 {
bc30ff58 9224 if (!pdi->is_declaration)
e8d05480
JB
9225 /* Ignore subprogram DIEs that do not have a name, they are
9226 illegal. Do not emit a complaint at this point, we will
9227 do so when we convert this psymtab into a symtab. */
9228 if (pdi->name)
9229 add_partial_symbol (pdi, cu);
bc30ff58
JB
9230 }
9231 }
6e70227d 9232
bc30ff58
JB
9233 if (! pdi->has_children)
9234 return;
9235
0a4b0913 9236 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9237 {
9238 pdi = pdi->die_child;
9239 while (pdi != NULL)
9240 {
52356b79 9241 pdi->fixup (cu);
bc30ff58 9242 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9243 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9244 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9245 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9246 pdi = pdi->die_sibling;
9247 }
9248 }
9249}
9250
91c24f0a
DC
9251/* Read a partial die corresponding to an enumeration type. */
9252
72bf9492
DJ
9253static void
9254add_partial_enumeration (struct partial_die_info *enum_pdi,
9255 struct dwarf2_cu *cu)
91c24f0a 9256{
72bf9492 9257 struct partial_die_info *pdi;
91c24f0a
DC
9258
9259 if (enum_pdi->name != NULL)
72bf9492
DJ
9260 add_partial_symbol (enum_pdi, cu);
9261
9262 pdi = enum_pdi->die_child;
9263 while (pdi)
91c24f0a 9264 {
72bf9492 9265 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9266 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9267 else
72bf9492
DJ
9268 add_partial_symbol (pdi, cu);
9269 pdi = pdi->die_sibling;
91c24f0a 9270 }
91c24f0a
DC
9271}
9272
6caca83c
CC
9273/* Return the initial uleb128 in the die at INFO_PTR. */
9274
9275static unsigned int
d521ce57 9276peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9277{
9278 unsigned int bytes_read;
9279
9280 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9281}
9282
685af9cd
TT
9283/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9284 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9285
4bb7a0a7
DJ
9286 Return the corresponding abbrev, or NULL if the number is zero (indicating
9287 an empty DIE). In either case *BYTES_READ will be set to the length of
9288 the initial number. */
9289
9290static struct abbrev_info *
685af9cd
TT
9291peek_die_abbrev (const die_reader_specs &reader,
9292 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9293{
685af9cd 9294 dwarf2_cu *cu = reader.cu;
518817b3 9295 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9296 unsigned int abbrev_number
9297 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9298
9299 if (abbrev_number == 0)
9300 return NULL;
9301
685af9cd 9302 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9303 if (!abbrev)
9304 {
422b9917 9305 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9306 " at offset %s [in module %s]"),
422b9917 9307 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9308 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9309 }
9310
9311 return abbrev;
9312}
9313
93311388
DE
9314/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9315 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9316 DIE. Any children of the skipped DIEs will also be skipped. */
9317
d521ce57
TT
9318static const gdb_byte *
9319skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9320{
4bb7a0a7
DJ
9321 while (1)
9322 {
685af9cd
TT
9323 unsigned int bytes_read;
9324 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9325
4bb7a0a7
DJ
9326 if (abbrev == NULL)
9327 return info_ptr + bytes_read;
9328 else
dee91e82 9329 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9330 }
9331}
9332
93311388
DE
9333/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9334 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9335 abbrev corresponding to that skipped uleb128 should be passed in
9336 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9337 children. */
9338
d521ce57
TT
9339static const gdb_byte *
9340skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9341 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9342{
9343 unsigned int bytes_read;
9344 struct attribute attr;
dee91e82
DE
9345 bfd *abfd = reader->abfd;
9346 struct dwarf2_cu *cu = reader->cu;
d521ce57 9347 const gdb_byte *buffer = reader->buffer;
f664829e 9348 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9349 unsigned int form, i;
9350
9351 for (i = 0; i < abbrev->num_attrs; i++)
9352 {
9353 /* The only abbrev we care about is DW_AT_sibling. */
9354 if (abbrev->attrs[i].name == DW_AT_sibling)
9355 {
dee91e82 9356 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9357 if (attr.form == DW_FORM_ref_addr)
b98664d3 9358 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9359 else
b9502d3f 9360 {
9c541725
PA
9361 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9362 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9363
9364 if (sibling_ptr < info_ptr)
b98664d3 9365 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9366 else if (sibling_ptr > reader->buffer_end)
9367 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9368 else
9369 return sibling_ptr;
9370 }
4bb7a0a7
DJ
9371 }
9372
9373 /* If it isn't DW_AT_sibling, skip this attribute. */
9374 form = abbrev->attrs[i].form;
9375 skip_attribute:
9376 switch (form)
9377 {
4bb7a0a7 9378 case DW_FORM_ref_addr:
ae411497
TT
9379 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9380 and later it is offset sized. */
9381 if (cu->header.version == 2)
9382 info_ptr += cu->header.addr_size;
9383 else
9384 info_ptr += cu->header.offset_size;
9385 break;
36586728
TT
9386 case DW_FORM_GNU_ref_alt:
9387 info_ptr += cu->header.offset_size;
9388 break;
ae411497 9389 case DW_FORM_addr:
4bb7a0a7
DJ
9390 info_ptr += cu->header.addr_size;
9391 break;
9392 case DW_FORM_data1:
9393 case DW_FORM_ref1:
9394 case DW_FORM_flag:
8fe0f950 9395 case DW_FORM_strx1:
4bb7a0a7
DJ
9396 info_ptr += 1;
9397 break;
2dc7f7b3 9398 case DW_FORM_flag_present:
43988095 9399 case DW_FORM_implicit_const:
2dc7f7b3 9400 break;
4bb7a0a7
DJ
9401 case DW_FORM_data2:
9402 case DW_FORM_ref2:
8fe0f950 9403 case DW_FORM_strx2:
4bb7a0a7
DJ
9404 info_ptr += 2;
9405 break;
8fe0f950
AT
9406 case DW_FORM_strx3:
9407 info_ptr += 3;
9408 break;
4bb7a0a7
DJ
9409 case DW_FORM_data4:
9410 case DW_FORM_ref4:
8fe0f950 9411 case DW_FORM_strx4:
4bb7a0a7
DJ
9412 info_ptr += 4;
9413 break;
9414 case DW_FORM_data8:
9415 case DW_FORM_ref8:
55f1336d 9416 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9417 info_ptr += 8;
9418 break;
0224619f
JK
9419 case DW_FORM_data16:
9420 info_ptr += 16;
9421 break;
4bb7a0a7 9422 case DW_FORM_string:
9b1c24c8 9423 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9424 info_ptr += bytes_read;
9425 break;
2dc7f7b3 9426 case DW_FORM_sec_offset:
4bb7a0a7 9427 case DW_FORM_strp:
36586728 9428 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9429 info_ptr += cu->header.offset_size;
9430 break;
2dc7f7b3 9431 case DW_FORM_exprloc:
4bb7a0a7
DJ
9432 case DW_FORM_block:
9433 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9434 info_ptr += bytes_read;
9435 break;
9436 case DW_FORM_block1:
9437 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9438 break;
9439 case DW_FORM_block2:
9440 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9441 break;
9442 case DW_FORM_block4:
9443 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9444 break;
336d760d 9445 case DW_FORM_addrx:
cf532bd1 9446 case DW_FORM_strx:
4bb7a0a7
DJ
9447 case DW_FORM_sdata:
9448 case DW_FORM_udata:
9449 case DW_FORM_ref_udata:
3019eac3
DE
9450 case DW_FORM_GNU_addr_index:
9451 case DW_FORM_GNU_str_index:
d521ce57 9452 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9453 break;
9454 case DW_FORM_indirect:
9455 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9456 info_ptr += bytes_read;
9457 /* We need to continue parsing from here, so just go back to
9458 the top. */
9459 goto skip_attribute;
9460
9461 default:
3e43a32a
MS
9462 error (_("Dwarf Error: Cannot handle %s "
9463 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9464 dwarf_form_name (form),
9465 bfd_get_filename (abfd));
9466 }
9467 }
9468
9469 if (abbrev->has_children)
dee91e82 9470 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9471 else
9472 return info_ptr;
9473}
9474
93311388 9475/* Locate ORIG_PDI's sibling.
dee91e82 9476 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9477
d521ce57 9478static const gdb_byte *
dee91e82
DE
9479locate_pdi_sibling (const struct die_reader_specs *reader,
9480 struct partial_die_info *orig_pdi,
d521ce57 9481 const gdb_byte *info_ptr)
91c24f0a
DC
9482{
9483 /* Do we know the sibling already? */
72bf9492 9484
91c24f0a
DC
9485 if (orig_pdi->sibling)
9486 return orig_pdi->sibling;
9487
9488 /* Are there any children to deal with? */
9489
9490 if (!orig_pdi->has_children)
9491 return info_ptr;
9492
4bb7a0a7 9493 /* Skip the children the long way. */
91c24f0a 9494
dee91e82 9495 return skip_children (reader, info_ptr);
91c24f0a
DC
9496}
9497
257e7a09 9498/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9499 not NULL. */
c906108c
SS
9500
9501static void
257e7a09
YQ
9502dwarf2_read_symtab (struct partial_symtab *self,
9503 struct objfile *objfile)
c906108c 9504{
ed2dc618
SM
9505 struct dwarf2_per_objfile *dwarf2_per_objfile
9506 = get_dwarf2_per_objfile (objfile);
9507
257e7a09 9508 if (self->readin)
c906108c 9509 {
442e4d9c 9510 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9511 self->filename);
442e4d9c
YQ
9512 }
9513 else
9514 {
9515 if (info_verbose)
c906108c 9516 {
442e4d9c 9517 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9518 self->filename);
442e4d9c 9519 gdb_flush (gdb_stdout);
c906108c 9520 }
c906108c 9521
442e4d9c
YQ
9522 /* If this psymtab is constructed from a debug-only objfile, the
9523 has_section_at_zero flag will not necessarily be correct. We
9524 can get the correct value for this flag by looking at the data
9525 associated with the (presumably stripped) associated objfile. */
9526 if (objfile->separate_debug_objfile_backlink)
9527 {
9528 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9529 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9530
442e4d9c
YQ
9531 dwarf2_per_objfile->has_section_at_zero
9532 = dpo_backlink->has_section_at_zero;
9533 }
b2ab525c 9534
442e4d9c 9535 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9536
257e7a09 9537 psymtab_to_symtab_1 (self);
c906108c 9538
442e4d9c
YQ
9539 /* Finish up the debug error message. */
9540 if (info_verbose)
9541 printf_filtered (_("done.\n"));
c906108c 9542 }
95554aad 9543
ed2dc618 9544 process_cu_includes (dwarf2_per_objfile);
c906108c 9545}
9cdd5dbd
DE
9546\f
9547/* Reading in full CUs. */
c906108c 9548
10b3939b
DJ
9549/* Add PER_CU to the queue. */
9550
9551static void
95554aad
TT
9552queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9553 enum language pretend_language)
10b3939b
DJ
9554{
9555 struct dwarf2_queue_item *item;
9556
9557 per_cu->queued = 1;
8d749320 9558 item = XNEW (struct dwarf2_queue_item);
10b3939b 9559 item->per_cu = per_cu;
95554aad 9560 item->pretend_language = pretend_language;
10b3939b
DJ
9561 item->next = NULL;
9562
9563 if (dwarf2_queue == NULL)
9564 dwarf2_queue = item;
9565 else
9566 dwarf2_queue_tail->next = item;
9567
9568 dwarf2_queue_tail = item;
9569}
9570
89e63ee4
DE
9571/* If PER_CU is not yet queued, add it to the queue.
9572 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9573 dependency.
0907af0c 9574 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9575 meaning either PER_CU is already queued or it is already loaded.
9576
9577 N.B. There is an invariant here that if a CU is queued then it is loaded.
9578 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9579
9580static int
89e63ee4 9581maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9582 struct dwarf2_per_cu_data *per_cu,
9583 enum language pretend_language)
9584{
9585 /* We may arrive here during partial symbol reading, if we need full
9586 DIEs to process an unusual case (e.g. template arguments). Do
9587 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9588 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9589 {
9590 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9591 return 1;
9592 return 0;
9593 }
9594
9595 /* Mark the dependence relation so that we don't flush PER_CU
9596 too early. */
89e63ee4
DE
9597 if (dependent_cu != NULL)
9598 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9599
9600 /* If it's already on the queue, we have nothing to do. */
9601 if (per_cu->queued)
9602 return 0;
9603
9604 /* If the compilation unit is already loaded, just mark it as
9605 used. */
9606 if (per_cu->cu != NULL)
9607 {
9608 per_cu->cu->last_used = 0;
9609 return 0;
9610 }
9611
9612 /* Add it to the queue. */
9613 queue_comp_unit (per_cu, pretend_language);
9614
9615 return 1;
9616}
9617
10b3939b
DJ
9618/* Process the queue. */
9619
9620static void
ed2dc618 9621process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9622{
9623 struct dwarf2_queue_item *item, *next_item;
9624
b4f54984 9625 if (dwarf_read_debug)
45cfd468
DE
9626 {
9627 fprintf_unfiltered (gdb_stdlog,
9628 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9629 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9630 }
9631
03dd20cc
DJ
9632 /* The queue starts out with one item, but following a DIE reference
9633 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9634 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9635 {
cc12ce38
DE
9636 if ((dwarf2_per_objfile->using_index
9637 ? !item->per_cu->v.quick->compunit_symtab
9638 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9639 /* Skip dummy CUs. */
9640 && item->per_cu->cu != NULL)
f4dc4d17
DE
9641 {
9642 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9643 unsigned int debug_print_threshold;
247f5c4f 9644 char buf[100];
f4dc4d17 9645
247f5c4f 9646 if (per_cu->is_debug_types)
f4dc4d17 9647 {
247f5c4f
DE
9648 struct signatured_type *sig_type =
9649 (struct signatured_type *) per_cu;
9650
9d8780f0 9651 sprintf (buf, "TU %s at offset %s",
73be47f5 9652 hex_string (sig_type->signature),
9d8780f0 9653 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9654 /* There can be 100s of TUs.
9655 Only print them in verbose mode. */
9656 debug_print_threshold = 2;
f4dc4d17 9657 }
247f5c4f 9658 else
73be47f5 9659 {
9d8780f0
SM
9660 sprintf (buf, "CU at offset %s",
9661 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9662 debug_print_threshold = 1;
9663 }
247f5c4f 9664
b4f54984 9665 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9666 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9667
9668 if (per_cu->is_debug_types)
9669 process_full_type_unit (per_cu, item->pretend_language);
9670 else
9671 process_full_comp_unit (per_cu, item->pretend_language);
9672
b4f54984 9673 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9674 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9675 }
10b3939b
DJ
9676
9677 item->per_cu->queued = 0;
9678 next_item = item->next;
9679 xfree (item);
9680 }
9681
9682 dwarf2_queue_tail = NULL;
45cfd468 9683
b4f54984 9684 if (dwarf_read_debug)
45cfd468
DE
9685 {
9686 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9687 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9688 }
10b3939b
DJ
9689}
9690
10b3939b
DJ
9691/* Read in full symbols for PST, and anything it depends on. */
9692
c906108c 9693static void
fba45db2 9694psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9695{
10b3939b 9696 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9697 int i;
9698
95554aad
TT
9699 if (pst->readin)
9700 return;
9701
aaa75496 9702 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9703 if (!pst->dependencies[i]->readin
9704 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9705 {
9706 /* Inform about additional files that need to be read in. */
9707 if (info_verbose)
9708 {
a3f17187 9709 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9710 fputs_filtered (" ", gdb_stdout);
9711 wrap_here ("");
9712 fputs_filtered ("and ", gdb_stdout);
9713 wrap_here ("");
9714 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9715 wrap_here (""); /* Flush output. */
aaa75496
JB
9716 gdb_flush (gdb_stdout);
9717 }
9718 psymtab_to_symtab_1 (pst->dependencies[i]);
9719 }
9720
9a3c8263 9721 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9722
9723 if (per_cu == NULL)
aaa75496
JB
9724 {
9725 /* It's an include file, no symbols to read for it.
9726 Everything is in the parent symtab. */
9727 pst->readin = 1;
9728 return;
9729 }
c906108c 9730
58f0c718 9731 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9732}
9733
dee91e82
DE
9734/* Trivial hash function for die_info: the hash value of a DIE
9735 is its offset in .debug_info for this objfile. */
10b3939b 9736
dee91e82
DE
9737static hashval_t
9738die_hash (const void *item)
10b3939b 9739{
9a3c8263 9740 const struct die_info *die = (const struct die_info *) item;
6502dd73 9741
9c541725 9742 return to_underlying (die->sect_off);
dee91e82 9743}
63d06c5c 9744
dee91e82
DE
9745/* Trivial comparison function for die_info structures: two DIEs
9746 are equal if they have the same offset. */
98bfdba5 9747
dee91e82
DE
9748static int
9749die_eq (const void *item_lhs, const void *item_rhs)
9750{
9a3c8263
SM
9751 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9752 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9753
9c541725 9754 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9755}
c906108c 9756
dee91e82
DE
9757/* die_reader_func for load_full_comp_unit.
9758 This is identical to read_signatured_type_reader,
9759 but is kept separate for now. */
c906108c 9760
dee91e82
DE
9761static void
9762load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9763 const gdb_byte *info_ptr,
dee91e82
DE
9764 struct die_info *comp_unit_die,
9765 int has_children,
9766 void *data)
9767{
9768 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9769 enum language *language_ptr = (enum language *) data;
6caca83c 9770
dee91e82
DE
9771 gdb_assert (cu->die_hash == NULL);
9772 cu->die_hash =
9773 htab_create_alloc_ex (cu->header.length / 12,
9774 die_hash,
9775 die_eq,
9776 NULL,
9777 &cu->comp_unit_obstack,
9778 hashtab_obstack_allocate,
9779 dummy_obstack_deallocate);
e142c38c 9780
dee91e82
DE
9781 if (has_children)
9782 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9783 &info_ptr, comp_unit_die);
9784 cu->dies = comp_unit_die;
9785 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9786
9787 /* We try not to read any attributes in this function, because not
9cdd5dbd 9788 all CUs needed for references have been loaded yet, and symbol
10b3939b 9789 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9790 or we won't be able to build types correctly.
9791 Similarly, if we do not read the producer, we can not apply
9792 producer-specific interpretation. */
95554aad 9793 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9794}
10b3939b 9795
dee91e82 9796/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9797
dee91e82 9798static void
95554aad 9799load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9800 bool skip_partial,
95554aad 9801 enum language pretend_language)
dee91e82 9802{
3019eac3 9803 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9804
58f0c718 9805 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9806 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9807}
9808
3da10d80
KS
9809/* Add a DIE to the delayed physname list. */
9810
9811static void
9812add_to_method_list (struct type *type, int fnfield_index, int index,
9813 const char *name, struct die_info *die,
9814 struct dwarf2_cu *cu)
9815{
9816 struct delayed_method_info mi;
9817 mi.type = type;
9818 mi.fnfield_index = fnfield_index;
9819 mi.index = index;
9820 mi.name = name;
9821 mi.die = die;
c89b44cd 9822 cu->method_list.push_back (mi);
3da10d80
KS
9823}
9824
3693fdb3
PA
9825/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9826 "const" / "volatile". If so, decrements LEN by the length of the
9827 modifier and return true. Otherwise return false. */
9828
9829template<size_t N>
9830static bool
9831check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9832{
9833 size_t mod_len = sizeof (mod) - 1;
9834 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9835 {
9836 len -= mod_len;
9837 return true;
9838 }
9839 return false;
9840}
9841
3da10d80
KS
9842/* Compute the physnames of any methods on the CU's method list.
9843
9844 The computation of method physnames is delayed in order to avoid the
9845 (bad) condition that one of the method's formal parameters is of an as yet
9846 incomplete type. */
9847
9848static void
9849compute_delayed_physnames (struct dwarf2_cu *cu)
9850{
3693fdb3 9851 /* Only C++ delays computing physnames. */
c89b44cd 9852 if (cu->method_list.empty ())
3693fdb3
PA
9853 return;
9854 gdb_assert (cu->language == language_cplus);
9855
52941706 9856 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9857 {
1d06ead6 9858 const char *physname;
3da10d80 9859 struct fn_fieldlist *fn_flp
c89b44cd
TT
9860 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9861 physname = dwarf2_physname (mi.name, mi.die, cu);
9862 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9863 = physname ? physname : "";
3693fdb3
PA
9864
9865 /* Since there's no tag to indicate whether a method is a
9866 const/volatile overload, extract that information out of the
9867 demangled name. */
9868 if (physname != NULL)
9869 {
9870 size_t len = strlen (physname);
9871
9872 while (1)
9873 {
9874 if (physname[len] == ')') /* shortcut */
9875 break;
9876 else if (check_modifier (physname, len, " const"))
c89b44cd 9877 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9878 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9879 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9880 else
9881 break;
9882 }
9883 }
3da10d80 9884 }
c89b44cd
TT
9885
9886 /* The list is no longer needed. */
9887 cu->method_list.clear ();
3da10d80
KS
9888}
9889
a766d390
DE
9890/* Go objects should be embedded in a DW_TAG_module DIE,
9891 and it's not clear if/how imported objects will appear.
9892 To keep Go support simple until that's worked out,
9893 go back through what we've read and create something usable.
9894 We could do this while processing each DIE, and feels kinda cleaner,
9895 but that way is more invasive.
9896 This is to, for example, allow the user to type "p var" or "b main"
9897 without having to specify the package name, and allow lookups
9898 of module.object to work in contexts that use the expression
9899 parser. */
9900
9901static void
9902fixup_go_packaging (struct dwarf2_cu *cu)
9903{
9904 char *package_name = NULL;
9905 struct pending *list;
9906 int i;
9907
c24bdb02 9908 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9909 list != NULL;
9910 list = list->next)
a766d390
DE
9911 {
9912 for (i = 0; i < list->nsyms; ++i)
9913 {
9914 struct symbol *sym = list->symbol[i];
9915
9916 if (SYMBOL_LANGUAGE (sym) == language_go
9917 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9918 {
9919 char *this_package_name = go_symbol_package_name (sym);
9920
9921 if (this_package_name == NULL)
9922 continue;
9923 if (package_name == NULL)
9924 package_name = this_package_name;
9925 else
9926 {
518817b3
SM
9927 struct objfile *objfile
9928 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9929 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9930 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9931 (symbol_symtab (sym) != NULL
9932 ? symtab_to_filename_for_display
9933 (symbol_symtab (sym))
e3b94546 9934 : objfile_name (objfile)),
a766d390
DE
9935 this_package_name, package_name);
9936 xfree (this_package_name);
9937 }
9938 }
9939 }
9940 }
9941
9942 if (package_name != NULL)
9943 {
518817b3 9944 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9945 const char *saved_package_name
021887d8 9946 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
19f392bc
UW
9947 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9948 saved_package_name);
a766d390
DE
9949 struct symbol *sym;
9950
e623cf5d 9951 sym = allocate_symbol (objfile);
f85f34ed 9952 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
31edb802 9953 SYMBOL_SET_NAMES (sym, saved_package_name, false, objfile);
a766d390
DE
9954 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9955 e.g., "main" finds the "main" module and not C's main(). */
9956 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9957 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9958 SYMBOL_TYPE (sym) = type;
9959
c24bdb02 9960 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9961
9962 xfree (package_name);
9963 }
9964}
9965
c9317f21
TT
9966/* Allocate a fully-qualified name consisting of the two parts on the
9967 obstack. */
9968
9969static const char *
9970rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9971{
9972 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9973}
9974
9975/* A helper that allocates a struct discriminant_info to attach to a
9976 union type. */
9977
9978static struct discriminant_info *
9979alloc_discriminant_info (struct type *type, int discriminant_index,
9980 int default_index)
9981{
9982 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9983 gdb_assert (discriminant_index == -1
9984 || (discriminant_index >= 0
9985 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9986 gdb_assert (default_index == -1
c7b15a66 9987 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9988
9989 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9990
9991 struct discriminant_info *disc
9992 = ((struct discriminant_info *)
9993 TYPE_ZALLOC (type,
9994 offsetof (struct discriminant_info, discriminants)
9995 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9996 disc->default_index = default_index;
9997 disc->discriminant_index = discriminant_index;
9998
9999 struct dynamic_prop prop;
10000 prop.kind = PROP_UNDEFINED;
10001 prop.data.baton = disc;
10002
10003 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10004
10005 return disc;
10006}
10007
10008/* Some versions of rustc emitted enums in an unusual way.
10009
10010 Ordinary enums were emitted as unions. The first element of each
10011 structure in the union was named "RUST$ENUM$DISR". This element
10012 held the discriminant.
10013
10014 These versions of Rust also implemented the "non-zero"
10015 optimization. When the enum had two values, and one is empty and
10016 the other holds a pointer that cannot be zero, the pointer is used
10017 as the discriminant, with a zero value meaning the empty variant.
10018 Here, the union's first member is of the form
10019 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10020 where the fieldnos are the indices of the fields that should be
10021 traversed in order to find the field (which may be several fields deep)
10022 and the variantname is the name of the variant of the case when the
10023 field is zero.
10024
10025 This function recognizes whether TYPE is of one of these forms,
10026 and, if so, smashes it to be a variant type. */
10027
10028static void
10029quirk_rust_enum (struct type *type, struct objfile *objfile)
10030{
10031 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10032
10033 /* We don't need to deal with empty enums. */
10034 if (TYPE_NFIELDS (type) == 0)
10035 return;
10036
10037#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10038 if (TYPE_NFIELDS (type) == 1
10039 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10040 {
10041 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10042
10043 /* Decode the field name to find the offset of the
10044 discriminant. */
10045 ULONGEST bit_offset = 0;
10046 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10047 while (name[0] >= '0' && name[0] <= '9')
10048 {
10049 char *tail;
10050 unsigned long index = strtoul (name, &tail, 10);
10051 name = tail;
10052 if (*name != '$'
10053 || index >= TYPE_NFIELDS (field_type)
10054 || (TYPE_FIELD_LOC_KIND (field_type, index)
10055 != FIELD_LOC_KIND_BITPOS))
10056 {
b98664d3 10057 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10058 "[in module %s]"),
10059 TYPE_FIELD_NAME (type, 0),
10060 objfile_name (objfile));
10061 return;
10062 }
10063 ++name;
10064
10065 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10066 field_type = TYPE_FIELD_TYPE (field_type, index);
10067 }
10068
10069 /* Make a union to hold the variants. */
10070 struct type *union_type = alloc_type (objfile);
10071 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10072 TYPE_NFIELDS (union_type) = 3;
10073 TYPE_FIELDS (union_type)
10074 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10075 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10076 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10077
10078 /* Put the discriminant must at index 0. */
10079 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10080 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10081 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10082 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10083
10084 /* The order of fields doesn't really matter, so put the real
10085 field at index 1 and the data-less field at index 2. */
10086 struct discriminant_info *disc
10087 = alloc_discriminant_info (union_type, 0, 1);
10088 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10089 TYPE_FIELD_NAME (union_type, 1)
10090 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10091 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10092 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10093 TYPE_FIELD_NAME (union_type, 1));
10094
10095 const char *dataless_name
10096 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10097 name);
10098 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10099 dataless_name);
10100 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10101 /* NAME points into the original discriminant name, which
10102 already has the correct lifetime. */
10103 TYPE_FIELD_NAME (union_type, 2) = name;
10104 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10105 disc->discriminants[2] = 0;
10106
10107 /* Smash this type to be a structure type. We have to do this
10108 because the type has already been recorded. */
10109 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10110 TYPE_NFIELDS (type) = 1;
10111 TYPE_FIELDS (type)
10112 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10113
10114 /* Install the variant part. */
10115 TYPE_FIELD_TYPE (type, 0) = union_type;
10116 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10117 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10118 }
77c2dba3
TT
10119 /* A union with a single anonymous field is probably an old-style
10120 univariant enum. */
10121 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10122 {
c9317f21
TT
10123 /* Smash this type to be a structure type. We have to do this
10124 because the type has already been recorded. */
10125 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10126
10127 /* Make a union to hold the variants. */
10128 struct type *union_type = alloc_type (objfile);
10129 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10130 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10131 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10132 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10133 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10134
10135 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10136 const char *variant_name
10137 = rust_last_path_segment (TYPE_NAME (field_type));
10138 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10139 TYPE_NAME (field_type)
10140 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10141 TYPE_NAME (type), variant_name);
c9317f21
TT
10142
10143 /* Install the union in the outer struct type. */
10144 TYPE_NFIELDS (type) = 1;
10145 TYPE_FIELDS (type)
10146 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10147 TYPE_FIELD_TYPE (type, 0) = union_type;
10148 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10149 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10150
10151 alloc_discriminant_info (union_type, -1, 0);
10152 }
10153 else
10154 {
10155 struct type *disr_type = nullptr;
10156 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10157 {
10158 disr_type = TYPE_FIELD_TYPE (type, i);
10159
a037790e
TT
10160 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10161 {
10162 /* All fields of a true enum will be structs. */
10163 return;
10164 }
10165 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10166 {
10167 /* Could be data-less variant, so keep going. */
a037790e 10168 disr_type = nullptr;
c9317f21
TT
10169 }
10170 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10171 "RUST$ENUM$DISR") != 0)
10172 {
10173 /* Not a Rust enum. */
10174 return;
10175 }
10176 else
10177 {
10178 /* Found one. */
10179 break;
10180 }
10181 }
10182
10183 /* If we got here without a discriminant, then it's probably
10184 just a union. */
10185 if (disr_type == nullptr)
10186 return;
10187
10188 /* Smash this type to be a structure type. We have to do this
10189 because the type has already been recorded. */
10190 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10191
10192 /* Make a union to hold the variants. */
10193 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10194 struct type *union_type = alloc_type (objfile);
10195 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10196 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10197 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10198 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10199 TYPE_FIELDS (union_type)
10200 = (struct field *) TYPE_ZALLOC (union_type,
10201 (TYPE_NFIELDS (union_type)
10202 * sizeof (struct field)));
10203
10204 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10205 TYPE_NFIELDS (type) * sizeof (struct field));
10206
10207 /* Install the discriminant at index 0 in the union. */
10208 TYPE_FIELD (union_type, 0) = *disr_field;
10209 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10210 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10211
10212 /* Install the union in the outer struct type. */
10213 TYPE_FIELD_TYPE (type, 0) = union_type;
10214 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10215 TYPE_NFIELDS (type) = 1;
10216
10217 /* Set the size and offset of the union type. */
10218 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10219
10220 /* We need a way to find the correct discriminant given a
10221 variant name. For convenience we build a map here. */
10222 struct type *enum_type = FIELD_TYPE (*disr_field);
10223 std::unordered_map<std::string, ULONGEST> discriminant_map;
10224 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10225 {
10226 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10227 {
10228 const char *name
10229 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10230 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10231 }
10232 }
10233
10234 int n_fields = TYPE_NFIELDS (union_type);
10235 struct discriminant_info *disc
10236 = alloc_discriminant_info (union_type, 0, -1);
10237 /* Skip the discriminant here. */
10238 for (int i = 1; i < n_fields; ++i)
10239 {
10240 /* Find the final word in the name of this variant's type.
10241 That name can be used to look up the correct
10242 discriminant. */
10243 const char *variant_name
10244 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10245 i)));
10246
10247 auto iter = discriminant_map.find (variant_name);
10248 if (iter != discriminant_map.end ())
10249 disc->discriminants[i] = iter->second;
10250
bedda9ac 10251 /* Remove the discriminant field, if it exists. */
c9317f21 10252 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10253 if (TYPE_NFIELDS (sub_type) > 0)
10254 {
10255 --TYPE_NFIELDS (sub_type);
10256 ++TYPE_FIELDS (sub_type);
10257 }
c9317f21
TT
10258 TYPE_FIELD_NAME (union_type, i) = variant_name;
10259 TYPE_NAME (sub_type)
10260 = rust_fully_qualify (&objfile->objfile_obstack,
10261 TYPE_NAME (type), variant_name);
10262 }
10263 }
10264}
10265
10266/* Rewrite some Rust unions to be structures with variants parts. */
10267
10268static void
10269rust_union_quirks (struct dwarf2_cu *cu)
10270{
10271 gdb_assert (cu->language == language_rust);
52941706
SM
10272 for (type *type_ : cu->rust_unions)
10273 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10274 /* We don't need this any more. */
10275 cu->rust_unions.clear ();
c9317f21
TT
10276}
10277
95554aad
TT
10278/* Return the symtab for PER_CU. This works properly regardless of
10279 whether we're using the index or psymtabs. */
10280
43f3e411
DE
10281static struct compunit_symtab *
10282get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10283{
ed2dc618 10284 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10285 ? per_cu->v.quick->compunit_symtab
10286 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10287}
10288
10289/* A helper function for computing the list of all symbol tables
10290 included by PER_CU. */
10291
10292static void
4c39bc03 10293recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10294 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10295 struct dwarf2_per_cu_data *per_cu,
43f3e411 10296 struct compunit_symtab *immediate_parent)
95554aad
TT
10297{
10298 void **slot;
43f3e411 10299 struct compunit_symtab *cust;
95554aad
TT
10300
10301 slot = htab_find_slot (all_children, per_cu, INSERT);
10302 if (*slot != NULL)
10303 {
10304 /* This inclusion and its children have been processed. */
10305 return;
10306 }
10307
10308 *slot = per_cu;
10309 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10310 cust = get_compunit_symtab (per_cu);
10311 if (cust != NULL)
ec94af83
DE
10312 {
10313 /* If this is a type unit only add its symbol table if we haven't
10314 seen it yet (type unit per_cu's can share symtabs). */
10315 if (per_cu->is_debug_types)
10316 {
43f3e411 10317 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10318 if (*slot == NULL)
10319 {
43f3e411 10320 *slot = cust;
4c39bc03 10321 result->push_back (cust);
43f3e411
DE
10322 if (cust->user == NULL)
10323 cust->user = immediate_parent;
ec94af83
DE
10324 }
10325 }
10326 else
f9125b6c 10327 {
4c39bc03 10328 result->push_back (cust);
43f3e411
DE
10329 if (cust->user == NULL)
10330 cust->user = immediate_parent;
f9125b6c 10331 }
ec94af83 10332 }
95554aad 10333
ae640021
AB
10334 if (!per_cu->imported_symtabs_empty ())
10335 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10336 {
10337 recursively_compute_inclusions (result, all_children,
10338 all_type_symtabs, ptr, cust);
10339 }
95554aad
TT
10340}
10341
43f3e411 10342/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10343 PER_CU. */
10344
10345static void
43f3e411 10346compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10347{
f4dc4d17
DE
10348 gdb_assert (! per_cu->is_debug_types);
10349
ae640021 10350 if (!per_cu->imported_symtabs_empty ())
95554aad 10351 {
ae640021 10352 int len;
4c39bc03 10353 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10354 htab_t all_children, all_type_symtabs;
43f3e411 10355 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10356
10357 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10358 if (cust == NULL)
95554aad
TT
10359 return;
10360
10361 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10362 NULL, xcalloc, xfree);
ec94af83
DE
10363 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10364 NULL, xcalloc, xfree);
95554aad 10365
ae640021 10366 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10367 {
10368 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10369 all_type_symtabs, ptr, cust);
ec94af83 10370 }
95554aad 10371
ec94af83 10372 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10373 len = result_symtabs.size ();
43f3e411 10374 cust->includes
ed2dc618 10375 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10376 struct compunit_symtab *, len + 1);
4c39bc03
TT
10377 memcpy (cust->includes, result_symtabs.data (),
10378 len * sizeof (compunit_symtab *));
43f3e411 10379 cust->includes[len] = NULL;
95554aad 10380
95554aad 10381 htab_delete (all_children);
ec94af83 10382 htab_delete (all_type_symtabs);
95554aad
TT
10383 }
10384}
10385
10386/* Compute the 'includes' field for the symtabs of all the CUs we just
10387 read. */
10388
10389static void
ed2dc618 10390process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10391{
71b73764 10392 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10393 {
10394 if (! iter->is_debug_types)
43f3e411 10395 compute_compunit_symtab_includes (iter);
f4dc4d17 10396 }
95554aad 10397
c5d0225d 10398 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10399}
10400
9cdd5dbd 10401/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10402 already been loaded into memory. */
10403
10404static void
95554aad
TT
10405process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10406 enum language pretend_language)
10b3939b 10407{
10b3939b 10408 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10409 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10410 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10411 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10412 CORE_ADDR lowpc, highpc;
43f3e411 10413 struct compunit_symtab *cust;
10b3939b 10414 CORE_ADDR baseaddr;
4359dff1 10415 struct block *static_block;
3e29f34a 10416 CORE_ADDR addr;
10b3939b
DJ
10417
10418 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10419
c89b44cd
TT
10420 /* Clear the list here in case something was left over. */
10421 cu->method_list.clear ();
10b3939b 10422
95554aad
TT
10423 cu->language = pretend_language;
10424 cu->language_defn = language_def (cu->language);
10425
c906108c 10426 /* Do line number decoding in read_file_scope () */
10b3939b 10427 process_die (cu->dies, cu);
c906108c 10428
a766d390
DE
10429 /* For now fudge the Go package. */
10430 if (cu->language == language_go)
10431 fixup_go_packaging (cu);
10432
5f48f8f3 10433 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10434 should be complete, and it should now be safe to compute all of the
10435 physnames. */
10436 compute_delayed_physnames (cu);
3da10d80 10437
c9317f21
TT
10438 if (cu->language == language_rust)
10439 rust_union_quirks (cu);
10440
fae299cd
DC
10441 /* Some compilers don't define a DW_AT_high_pc attribute for the
10442 compilation unit. If the DW_AT_high_pc is missing, synthesize
10443 it, by scanning the DIE's below the compilation unit. */
10b3939b 10444 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10445
3e29f34a 10446 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10447 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10448
10449 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10450 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10451 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10452 addrmap to help ensure it has an accurate map of pc values belonging to
10453 this comp unit. */
10454 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10455
c24bdb02 10456 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10457 SECT_OFF_TEXT (objfile),
10458 0);
c906108c 10459
43f3e411 10460 if (cust != NULL)
c906108c 10461 {
df15bd07 10462 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10463
8be455d7
JK
10464 /* Set symtab language to language from DW_AT_language. If the
10465 compilation is from a C file generated by language preprocessors, do
10466 not set the language if it was already deduced by start_subfile. */
43f3e411 10467 if (!(cu->language == language_c
40e3ad0e 10468 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10469 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10470
10471 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10472 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10473 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10474 there were bugs in prologue debug info, fixed later in GCC-4.5
10475 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10476
10477 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10478 needed, it would be wrong due to missing DW_AT_producer there.
10479
10480 Still one can confuse GDB by using non-standard GCC compilation
10481 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10482 */
ab260dad 10483 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10484 cust->locations_valid = 1;
e0d00bc7
JK
10485
10486 if (gcc_4_minor >= 5)
43f3e411 10487 cust->epilogue_unwind_valid = 1;
96408a79 10488
43f3e411 10489 cust->call_site_htab = cu->call_site_htab;
c906108c 10490 }
9291a0cd
TT
10491
10492 if (dwarf2_per_objfile->using_index)
43f3e411 10493 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10494 else
10495 {
10496 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10497 pst->compunit_symtab = cust;
9291a0cd
TT
10498 pst->readin = 1;
10499 }
c906108c 10500
95554aad 10501 /* Push it for inclusion processing later. */
c5d0225d 10502 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10503
10504 /* Not needed any more. */
c24bdb02 10505 cu->reset_builder ();
f4dc4d17 10506}
45cfd468 10507
f4dc4d17
DE
10508/* Generate full symbol information for type unit PER_CU, whose DIEs have
10509 already been loaded into memory. */
10510
10511static void
10512process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10513 enum language pretend_language)
10514{
10515 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10516 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10517 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10518 struct compunit_symtab *cust;
0186c6a7
DE
10519 struct signatured_type *sig_type;
10520
10521 gdb_assert (per_cu->is_debug_types);
10522 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10523
c89b44cd
TT
10524 /* Clear the list here in case something was left over. */
10525 cu->method_list.clear ();
f4dc4d17 10526
f4dc4d17
DE
10527 cu->language = pretend_language;
10528 cu->language_defn = language_def (cu->language);
10529
10530 /* The symbol tables are set up in read_type_unit_scope. */
10531 process_die (cu->dies, cu);
10532
10533 /* For now fudge the Go package. */
10534 if (cu->language == language_go)
10535 fixup_go_packaging (cu);
10536
5f48f8f3 10537 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10538 should be complete, and it should now be safe to compute all of the
10539 physnames. */
10540 compute_delayed_physnames (cu);
f4dc4d17 10541
c9317f21
TT
10542 if (cu->language == language_rust)
10543 rust_union_quirks (cu);
10544
f4dc4d17
DE
10545 /* TUs share symbol tables.
10546 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10547 of it with end_expandable_symtab. Otherwise, complete the addition of
10548 this TU's symbols to the existing symtab. */
43f3e411 10549 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10550 {
c24bdb02
KS
10551 buildsym_compunit *builder = cu->get_builder ();
10552 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10553 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10554
43f3e411 10555 if (cust != NULL)
f4dc4d17
DE
10556 {
10557 /* Set symtab language to language from DW_AT_language. If the
10558 compilation is from a C file generated by language preprocessors,
10559 do not set the language if it was already deduced by
10560 start_subfile. */
43f3e411
DE
10561 if (!(cu->language == language_c
10562 && COMPUNIT_FILETABS (cust)->language != language_c))
10563 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10564 }
10565 }
10566 else
10567 {
c24bdb02 10568 cu->get_builder ()->augment_type_symtab ();
43f3e411 10569 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10570 }
10571
10572 if (dwarf2_per_objfile->using_index)
43f3e411 10573 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10574 else
10575 {
10576 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10577 pst->compunit_symtab = cust;
f4dc4d17 10578 pst->readin = 1;
45cfd468 10579 }
804d2729
TT
10580
10581 /* Not needed any more. */
c24bdb02 10582 cu->reset_builder ();
c906108c
SS
10583}
10584
95554aad
TT
10585/* Process an imported unit DIE. */
10586
10587static void
10588process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10589{
10590 struct attribute *attr;
10591
f4dc4d17
DE
10592 /* For now we don't handle imported units in type units. */
10593 if (cu->per_cu->is_debug_types)
10594 {
10595 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10596 " supported in type units [in module %s]"),
518817b3 10597 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10598 }
10599
95554aad
TT
10600 attr = dwarf2_attr (die, DW_AT_import, cu);
10601 if (attr != NULL)
10602 {
9c541725
PA
10603 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10604 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10605 dwarf2_per_cu_data *per_cu
e3b94546 10606 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10607 cu->per_cu->dwarf2_per_objfile);
95554aad 10608
69d751e3 10609 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10610 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10611 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10612
ae640021 10613 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10614 }
10615}
10616
4c8aa72d
PA
10617/* RAII object that represents a process_die scope: i.e.,
10618 starts/finishes processing a DIE. */
10619class process_die_scope
adde2bff 10620{
4c8aa72d
PA
10621public:
10622 process_die_scope (die_info *die, dwarf2_cu *cu)
10623 : m_die (die), m_cu (cu)
10624 {
10625 /* We should only be processing DIEs not already in process. */
10626 gdb_assert (!m_die->in_process);
10627 m_die->in_process = true;
10628 }
8c3cb9fa 10629
4c8aa72d
PA
10630 ~process_die_scope ()
10631 {
10632 m_die->in_process = false;
10633
10634 /* If we're done processing the DIE for the CU that owns the line
10635 header, we don't need the line header anymore. */
10636 if (m_cu->line_header_die_owner == m_die)
10637 {
10638 delete m_cu->line_header;
10639 m_cu->line_header = NULL;
10640 m_cu->line_header_die_owner = NULL;
10641 }
10642 }
10643
10644private:
10645 die_info *m_die;
10646 dwarf2_cu *m_cu;
10647};
adde2bff 10648
c906108c
SS
10649/* Process a die and its children. */
10650
10651static void
e7c27a73 10652process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10653{
4c8aa72d 10654 process_die_scope scope (die, cu);
adde2bff 10655
c906108c
SS
10656 switch (die->tag)
10657 {
10658 case DW_TAG_padding:
10659 break;
10660 case DW_TAG_compile_unit:
95554aad 10661 case DW_TAG_partial_unit:
e7c27a73 10662 read_file_scope (die, cu);
c906108c 10663 break;
348e048f
DE
10664 case DW_TAG_type_unit:
10665 read_type_unit_scope (die, cu);
10666 break;
c906108c 10667 case DW_TAG_subprogram:
0a4b0913
AB
10668 /* Nested subprograms in Fortran get a prefix. */
10669 if (cu->language == language_fortran
10670 && die->parent != NULL
10671 && die->parent->tag == DW_TAG_subprogram)
10672 cu->processing_has_namespace_info = true;
10673 /* Fall through. */
c906108c 10674 case DW_TAG_inlined_subroutine:
edb3359d 10675 read_func_scope (die, cu);
c906108c
SS
10676 break;
10677 case DW_TAG_lexical_block:
14898363
L
10678 case DW_TAG_try_block:
10679 case DW_TAG_catch_block:
e7c27a73 10680 read_lexical_block_scope (die, cu);
c906108c 10681 break;
216f72a1 10682 case DW_TAG_call_site:
96408a79
SA
10683 case DW_TAG_GNU_call_site:
10684 read_call_site_scope (die, cu);
10685 break;
c906108c 10686 case DW_TAG_class_type:
680b30c7 10687 case DW_TAG_interface_type:
c906108c
SS
10688 case DW_TAG_structure_type:
10689 case DW_TAG_union_type:
134d01f1 10690 process_structure_scope (die, cu);
c906108c
SS
10691 break;
10692 case DW_TAG_enumeration_type:
134d01f1 10693 process_enumeration_scope (die, cu);
c906108c 10694 break;
134d01f1 10695
f792889a
DJ
10696 /* These dies have a type, but processing them does not create
10697 a symbol or recurse to process the children. Therefore we can
10698 read them on-demand through read_type_die. */
c906108c 10699 case DW_TAG_subroutine_type:
72019c9c 10700 case DW_TAG_set_type:
c906108c 10701 case DW_TAG_array_type:
c906108c 10702 case DW_TAG_pointer_type:
c906108c 10703 case DW_TAG_ptr_to_member_type:
c906108c 10704 case DW_TAG_reference_type:
4297a3f0 10705 case DW_TAG_rvalue_reference_type:
c906108c 10706 case DW_TAG_string_type:
c906108c 10707 break;
134d01f1 10708
c906108c 10709 case DW_TAG_base_type:
a02abb62 10710 case DW_TAG_subrange_type:
cb249c71 10711 case DW_TAG_typedef:
134d01f1
DJ
10712 /* Add a typedef symbol for the type definition, if it has a
10713 DW_AT_name. */
f792889a 10714 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10715 break;
c906108c 10716 case DW_TAG_common_block:
e7c27a73 10717 read_common_block (die, cu);
c906108c
SS
10718 break;
10719 case DW_TAG_common_inclusion:
10720 break;
d9fa45fe 10721 case DW_TAG_namespace:
9068261f 10722 cu->processing_has_namespace_info = true;
e7c27a73 10723 read_namespace (die, cu);
d9fa45fe 10724 break;
5d7cb8df 10725 case DW_TAG_module:
9068261f 10726 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10727 read_module (die, cu);
10728 break;
d9fa45fe 10729 case DW_TAG_imported_declaration:
9068261f 10730 cu->processing_has_namespace_info = true;
74921315
KS
10731 if (read_namespace_alias (die, cu))
10732 break;
86a73007
TT
10733 /* The declaration is not a global namespace alias. */
10734 /* Fall through. */
d9fa45fe 10735 case DW_TAG_imported_module:
9068261f 10736 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10737 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10738 || cu->language != language_fortran))
b98664d3 10739 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10740 dwarf_tag_name (die->tag));
10741 read_import_statement (die, cu);
d9fa45fe 10742 break;
95554aad
TT
10743
10744 case DW_TAG_imported_unit:
10745 process_imported_unit_die (die, cu);
10746 break;
10747
71a3c369
TT
10748 case DW_TAG_variable:
10749 read_variable (die, cu);
10750 break;
10751
c906108c 10752 default:
e7c27a73 10753 new_symbol (die, NULL, cu);
c906108c
SS
10754 break;
10755 }
10756}
ca69b9e6
DE
10757\f
10758/* DWARF name computation. */
c906108c 10759
94af9270
KS
10760/* A helper function for dwarf2_compute_name which determines whether DIE
10761 needs to have the name of the scope prepended to the name listed in the
10762 die. */
10763
10764static int
10765die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10766{
1c809c68
TT
10767 struct attribute *attr;
10768
94af9270
KS
10769 switch (die->tag)
10770 {
10771 case DW_TAG_namespace:
10772 case DW_TAG_typedef:
10773 case DW_TAG_class_type:
10774 case DW_TAG_interface_type:
10775 case DW_TAG_structure_type:
10776 case DW_TAG_union_type:
10777 case DW_TAG_enumeration_type:
10778 case DW_TAG_enumerator:
10779 case DW_TAG_subprogram:
08a76f8a 10780 case DW_TAG_inlined_subroutine:
94af9270 10781 case DW_TAG_member:
74921315 10782 case DW_TAG_imported_declaration:
94af9270
KS
10783 return 1;
10784
10785 case DW_TAG_variable:
c2b0a229 10786 case DW_TAG_constant:
94af9270
KS
10787 /* We only need to prefix "globally" visible variables. These include
10788 any variable marked with DW_AT_external or any variable that
10789 lives in a namespace. [Variables in anonymous namespaces
10790 require prefixing, but they are not DW_AT_external.] */
10791
10792 if (dwarf2_attr (die, DW_AT_specification, cu))
10793 {
10794 struct dwarf2_cu *spec_cu = cu;
9a619af0 10795
94af9270
KS
10796 return die_needs_namespace (die_specification (die, &spec_cu),
10797 spec_cu);
10798 }
10799
1c809c68 10800 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10801 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10802 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10803 return 0;
10804 /* A variable in a lexical block of some kind does not need a
10805 namespace, even though in C++ such variables may be external
10806 and have a mangled name. */
10807 if (die->parent->tag == DW_TAG_lexical_block
10808 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10809 || die->parent->tag == DW_TAG_catch_block
10810 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10811 return 0;
10812 return 1;
94af9270
KS
10813
10814 default:
10815 return 0;
10816 }
10817}
10818
73b9be8b
KS
10819/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10820 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10821 defined for the given DIE. */
10822
10823static struct attribute *
10824dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10825{
10826 struct attribute *attr;
10827
10828 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10829 if (attr == NULL)
10830 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10831
10832 return attr;
10833}
10834
10835/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10836 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10837 defined for the given DIE. */
10838
10839static const char *
10840dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10841{
10842 const char *linkage_name;
10843
10844 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10845 if (linkage_name == NULL)
10846 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10847
10848 return linkage_name;
10849}
10850
94af9270 10851/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10852 compute the physname for the object, which include a method's:
9c37b5ae 10853 - formal parameters (C++),
a766d390 10854 - receiver type (Go),
a766d390
DE
10855
10856 The term "physname" is a bit confusing.
10857 For C++, for example, it is the demangled name.
10858 For Go, for example, it's the mangled name.
94af9270 10859
af6b7be1
JB
10860 For Ada, return the DIE's linkage name rather than the fully qualified
10861 name. PHYSNAME is ignored..
10862
94af9270
KS
10863 The result is allocated on the objfile_obstack and canonicalized. */
10864
10865static const char *
15d034d0
TT
10866dwarf2_compute_name (const char *name,
10867 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10868 int physname)
10869{
518817b3 10870 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10871
94af9270
KS
10872 if (name == NULL)
10873 name = dwarf2_name (die, cu);
10874
2ee7123e
DE
10875 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10876 but otherwise compute it by typename_concat inside GDB.
10877 FIXME: Actually this is not really true, or at least not always true.
10878 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10879 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10880 will set the demangled name to the result of dwarf2_full_name, and it is
10881 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10882 if (cu->language == language_ada
10883 || (cu->language == language_fortran && physname))
10884 {
10885 /* For Ada unit, we prefer the linkage name over the name, as
10886 the former contains the exported name, which the user expects
10887 to be able to reference. Ideally, we want the user to be able
10888 to reference this entity using either natural or linkage name,
10889 but we haven't started looking at this enhancement yet. */
73b9be8b 10890 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10891
2ee7123e
DE
10892 if (linkage_name != NULL)
10893 return linkage_name;
f55ee35c
JK
10894 }
10895
94af9270
KS
10896 /* These are the only languages we know how to qualify names in. */
10897 if (name != NULL
9c37b5ae 10898 && (cu->language == language_cplus
c44af4eb
TT
10899 || cu->language == language_fortran || cu->language == language_d
10900 || cu->language == language_rust))
94af9270
KS
10901 {
10902 if (die_needs_namespace (die, cu))
10903 {
0d5cff50 10904 const char *prefix;
34a68019 10905 const char *canonical_name = NULL;
94af9270 10906
d7e74731
PA
10907 string_file buf;
10908
94af9270 10909 prefix = determine_prefix (die, cu);
94af9270
KS
10910 if (*prefix != '\0')
10911 {
f55ee35c
JK
10912 char *prefixed_name = typename_concat (NULL, prefix, name,
10913 physname, cu);
9a619af0 10914
d7e74731 10915 buf.puts (prefixed_name);
94af9270
KS
10916 xfree (prefixed_name);
10917 }
10918 else
d7e74731 10919 buf.puts (name);
94af9270 10920
98bfdba5
PA
10921 /* Template parameters may be specified in the DIE's DW_AT_name, or
10922 as children with DW_TAG_template_type_param or
10923 DW_TAG_value_type_param. If the latter, add them to the name
10924 here. If the name already has template parameters, then
10925 skip this step; some versions of GCC emit both, and
10926 it is more efficient to use the pre-computed name.
10927
10928 Something to keep in mind about this process: it is very
10929 unlikely, or in some cases downright impossible, to produce
10930 something that will match the mangled name of a function.
10931 If the definition of the function has the same debug info,
10932 we should be able to match up with it anyway. But fallbacks
10933 using the minimal symbol, for instance to find a method
10934 implemented in a stripped copy of libstdc++, will not work.
10935 If we do not have debug info for the definition, we will have to
10936 match them up some other way.
10937
10938 When we do name matching there is a related problem with function
10939 templates; two instantiated function templates are allowed to
10940 differ only by their return types, which we do not add here. */
10941
10942 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10943 {
10944 struct attribute *attr;
10945 struct die_info *child;
10946 int first = 1;
10947
10948 die->building_fullname = 1;
10949
10950 for (child = die->child; child != NULL; child = child->sibling)
10951 {
10952 struct type *type;
12df843f 10953 LONGEST value;
d521ce57 10954 const gdb_byte *bytes;
98bfdba5
PA
10955 struct dwarf2_locexpr_baton *baton;
10956 struct value *v;
10957
10958 if (child->tag != DW_TAG_template_type_param
10959 && child->tag != DW_TAG_template_value_param)
10960 continue;
10961
10962 if (first)
10963 {
d7e74731 10964 buf.puts ("<");
98bfdba5
PA
10965 first = 0;
10966 }
10967 else
d7e74731 10968 buf.puts (", ");
98bfdba5
PA
10969
10970 attr = dwarf2_attr (child, DW_AT_type, cu);
10971 if (attr == NULL)
10972 {
b98664d3 10973 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10974 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10975 continue;
10976 }
10977 type = die_type (child, cu);
10978
10979 if (child->tag == DW_TAG_template_type_param)
10980 {
c1ec8cea
TT
10981 c_print_type (type, "", &buf, -1, 0, cu->language,
10982 &type_print_raw_options);
98bfdba5
PA
10983 continue;
10984 }
10985
10986 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10987 if (attr == NULL)
10988 {
b98664d3 10989 complaint (_("template parameter missing "
3e43a32a 10990 "DW_AT_const_value"));
d7e74731 10991 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10992 continue;
10993 }
10994
10995 dwarf2_const_value_attr (attr, type, name,
10996 &cu->comp_unit_obstack, cu,
10997 &value, &bytes, &baton);
10998
10999 if (TYPE_NOSIGN (type))
11000 /* GDB prints characters as NUMBER 'CHAR'. If that's
11001 changed, this can use value_print instead. */
d7e74731 11002 c_printchar (value, type, &buf);
98bfdba5
PA
11003 else
11004 {
11005 struct value_print_options opts;
11006
11007 if (baton != NULL)
11008 v = dwarf2_evaluate_loc_desc (type, NULL,
11009 baton->data,
11010 baton->size,
11011 baton->per_cu);
11012 else if (bytes != NULL)
11013 {
11014 v = allocate_value (type);
11015 memcpy (value_contents_writeable (v), bytes,
11016 TYPE_LENGTH (type));
11017 }
11018 else
11019 v = value_from_longest (type, value);
11020
3e43a32a
MS
11021 /* Specify decimal so that we do not depend on
11022 the radix. */
98bfdba5
PA
11023 get_formatted_print_options (&opts, 'd');
11024 opts.raw = 1;
d7e74731 11025 value_print (v, &buf, &opts);
98bfdba5 11026 release_value (v);
98bfdba5
PA
11027 }
11028 }
11029
11030 die->building_fullname = 0;
11031
11032 if (!first)
11033 {
11034 /* Close the argument list, with a space if necessary
11035 (nested templates). */
d7e74731
PA
11036 if (!buf.empty () && buf.string ().back () == '>')
11037 buf.puts (" >");
98bfdba5 11038 else
d7e74731 11039 buf.puts (">");
98bfdba5
PA
11040 }
11041 }
11042
9c37b5ae 11043 /* For C++ methods, append formal parameter type
94af9270 11044 information, if PHYSNAME. */
6e70227d 11045
94af9270 11046 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11047 && cu->language == language_cplus)
94af9270
KS
11048 {
11049 struct type *type = read_type_die (die, cu);
11050
d7e74731 11051 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11052 &type_print_raw_options);
94af9270 11053
9c37b5ae 11054 if (cu->language == language_cplus)
94af9270 11055 {
60430eff
DJ
11056 /* Assume that an artificial first parameter is
11057 "this", but do not crash if it is not. RealView
11058 marks unnamed (and thus unused) parameters as
11059 artificial; there is no way to differentiate
11060 the two cases. */
94af9270
KS
11061 if (TYPE_NFIELDS (type) > 0
11062 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11063 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11064 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11065 0))))
d7e74731 11066 buf.puts (" const");
94af9270
KS
11067 }
11068 }
11069
d7e74731 11070 const std::string &intermediate_name = buf.string ();
94af9270
KS
11071
11072 if (cu->language == language_cplus)
34a68019 11073 canonical_name
322a8516 11074 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11075 &objfile->per_bfd->storage_obstack);
11076
11077 /* If we only computed INTERMEDIATE_NAME, or if
11078 INTERMEDIATE_NAME is already canonical, then we need to
11079 copy it to the appropriate obstack. */
322a8516 11080 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11081 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11082 intermediate_name);
34a68019
TT
11083 else
11084 name = canonical_name;
94af9270
KS
11085 }
11086 }
11087
11088 return name;
11089}
11090
0114d602
DJ
11091/* Return the fully qualified name of DIE, based on its DW_AT_name.
11092 If scope qualifiers are appropriate they will be added. The result
34a68019 11093 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11094 not have a name. NAME may either be from a previous call to
11095 dwarf2_name or NULL.
11096
9c37b5ae 11097 The output string will be canonicalized (if C++). */
0114d602
DJ
11098
11099static const char *
15d034d0 11100dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11101{
94af9270
KS
11102 return dwarf2_compute_name (name, die, cu, 0);
11103}
0114d602 11104
94af9270
KS
11105/* Construct a physname for the given DIE in CU. NAME may either be
11106 from a previous call to dwarf2_name or NULL. The result will be
11107 allocated on the objfile_objstack or NULL if the DIE does not have a
11108 name.
0114d602 11109
9c37b5ae 11110 The output string will be canonicalized (if C++). */
0114d602 11111
94af9270 11112static const char *
15d034d0 11113dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11114{
518817b3 11115 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11116 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11117 int need_copy = 1;
11118
11119 /* In this case dwarf2_compute_name is just a shortcut not building anything
11120 on its own. */
11121 if (!die_needs_namespace (die, cu))
11122 return dwarf2_compute_name (name, die, cu, 1);
11123
73b9be8b 11124 mangled = dw2_linkage_name (die, cu);
900e11f9 11125
e98c9e7c
TT
11126 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11127 See https://github.com/rust-lang/rust/issues/32925. */
11128 if (cu->language == language_rust && mangled != NULL
11129 && strchr (mangled, '{') != NULL)
11130 mangled = NULL;
11131
900e11f9
JK
11132 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11133 has computed. */
791afaa2 11134 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11135 if (mangled != NULL)
900e11f9 11136 {
900e11f9 11137
59cc4834
JB
11138 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11139 {
11140 /* Do nothing (do not demangle the symbol name). */
11141 }
11142 else if (cu->language == language_go)
a766d390 11143 {
5e2db402
TT
11144 /* This is a lie, but we already lie to the caller new_symbol.
11145 new_symbol assumes we return the mangled name.
a766d390 11146 This just undoes that lie until things are cleaned up. */
a766d390
DE
11147 }
11148 else
11149 {
0eb876f5
JB
11150 /* Use DMGL_RET_DROP for C++ template functions to suppress
11151 their return type. It is easier for GDB users to search
11152 for such functions as `name(params)' than `long name(params)'.
11153 In such case the minimal symbol names do not match the full
11154 symbol names but for template functions there is never a need
11155 to look up their definition from their declaration so
11156 the only disadvantage remains the minimal symbol variant
11157 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11158 demangled.reset (gdb_demangle (mangled,
11159 (DMGL_PARAMS | DMGL_ANSI
11160 | DMGL_RET_DROP)));
a766d390 11161 }
900e11f9 11162 if (demangled)
791afaa2 11163 canon = demangled.get ();
900e11f9
JK
11164 else
11165 {
11166 canon = mangled;
11167 need_copy = 0;
11168 }
11169 }
11170
11171 if (canon == NULL || check_physname)
11172 {
11173 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11174
11175 if (canon != NULL && strcmp (physname, canon) != 0)
11176 {
11177 /* It may not mean a bug in GDB. The compiler could also
11178 compute DW_AT_linkage_name incorrectly. But in such case
11179 GDB would need to be bug-to-bug compatible. */
11180
b98664d3 11181 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11182 "(from linkage <%s>) - DIE at %s [in module %s]"),
11183 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11184 objfile_name (objfile));
900e11f9
JK
11185
11186 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11187 is available here - over computed PHYSNAME. It is safer
11188 against both buggy GDB and buggy compilers. */
11189
11190 retval = canon;
11191 }
11192 else
11193 {
11194 retval = physname;
11195 need_copy = 0;
11196 }
11197 }
11198 else
11199 retval = canon;
11200
11201 if (need_copy)
021887d8 11202 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11203
900e11f9 11204 return retval;
0114d602
DJ
11205}
11206
74921315
KS
11207/* Inspect DIE in CU for a namespace alias. If one exists, record
11208 a new symbol for it.
11209
11210 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11211
11212static int
11213read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11214{
11215 struct attribute *attr;
11216
11217 /* If the die does not have a name, this is not a namespace
11218 alias. */
11219 attr = dwarf2_attr (die, DW_AT_name, cu);
11220 if (attr != NULL)
11221 {
11222 int num;
11223 struct die_info *d = die;
11224 struct dwarf2_cu *imported_cu = cu;
11225
11226 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11227 keep inspecting DIEs until we hit the underlying import. */
11228#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11229 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11230 {
11231 attr = dwarf2_attr (d, DW_AT_import, cu);
11232 if (attr == NULL)
11233 break;
11234
11235 d = follow_die_ref (d, attr, &imported_cu);
11236 if (d->tag != DW_TAG_imported_declaration)
11237 break;
11238 }
11239
11240 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11241 {
b98664d3 11242 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11243 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11244 return 0;
11245 }
11246
11247 if (attr != NULL)
11248 {
11249 struct type *type;
9c541725 11250 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11251
9c541725 11252 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11253 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11254 {
11255 /* This declaration is a global namespace alias. Add
11256 a symbol for it whose type is the aliased namespace. */
11257 new_symbol (die, type, cu);
11258 return 1;
11259 }
11260 }
11261 }
11262
11263 return 0;
11264}
11265
22cee43f 11266/* Return the using directives repository (global or local?) to use in the
804d2729 11267 current context for CU.
22cee43f
PMR
11268
11269 For Ada, imported declarations can materialize renamings, which *may* be
11270 global. However it is impossible (for now?) in DWARF to distinguish
11271 "external" imported declarations and "static" ones. As all imported
11272 declarations seem to be static in all other languages, make them all CU-wide
11273 global only in Ada. */
11274
11275static struct using_direct **
804d2729 11276using_directives (struct dwarf2_cu *cu)
22cee43f 11277{
c24bdb02
KS
11278 if (cu->language == language_ada
11279 && cu->get_builder ()->outermost_context_p ())
11280 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11281 else
c24bdb02 11282 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11283}
11284
27aa8d6a
SW
11285/* Read the import statement specified by the given die and record it. */
11286
11287static void
11288read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11289{
518817b3 11290 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11291 struct attribute *import_attr;
32019081 11292 struct die_info *imported_die, *child_die;
de4affc9 11293 struct dwarf2_cu *imported_cu;
27aa8d6a 11294 const char *imported_name;
794684b6 11295 const char *imported_name_prefix;
13387711
SW
11296 const char *canonical_name;
11297 const char *import_alias;
11298 const char *imported_declaration = NULL;
794684b6 11299 const char *import_prefix;
eb1e02fd 11300 std::vector<const char *> excludes;
13387711 11301
27aa8d6a
SW
11302 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11303 if (import_attr == NULL)
11304 {
b98664d3 11305 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11306 dwarf_tag_name (die->tag));
11307 return;
11308 }
11309
de4affc9
CC
11310 imported_cu = cu;
11311 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11312 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11313 if (imported_name == NULL)
11314 {
11315 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11316
11317 The import in the following code:
11318 namespace A
11319 {
11320 typedef int B;
11321 }
11322
11323 int main ()
11324 {
11325 using A::B;
11326 B b;
11327 return b;
11328 }
11329
11330 ...
11331 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11332 <52> DW_AT_decl_file : 1
11333 <53> DW_AT_decl_line : 6
11334 <54> DW_AT_import : <0x75>
11335 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11336 <59> DW_AT_name : B
11337 <5b> DW_AT_decl_file : 1
11338 <5c> DW_AT_decl_line : 2
11339 <5d> DW_AT_type : <0x6e>
11340 ...
11341 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11342 <76> DW_AT_byte_size : 4
11343 <77> DW_AT_encoding : 5 (signed)
11344
11345 imports the wrong die ( 0x75 instead of 0x58 ).
11346 This case will be ignored until the gcc bug is fixed. */
11347 return;
11348 }
11349
82856980
SW
11350 /* Figure out the local name after import. */
11351 import_alias = dwarf2_name (die, cu);
27aa8d6a 11352
794684b6
SW
11353 /* Figure out where the statement is being imported to. */
11354 import_prefix = determine_prefix (die, cu);
11355
11356 /* Figure out what the scope of the imported die is and prepend it
11357 to the name of the imported die. */
de4affc9 11358 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11359
f55ee35c
JK
11360 if (imported_die->tag != DW_TAG_namespace
11361 && imported_die->tag != DW_TAG_module)
794684b6 11362 {
13387711
SW
11363 imported_declaration = imported_name;
11364 canonical_name = imported_name_prefix;
794684b6 11365 }
13387711 11366 else if (strlen (imported_name_prefix) > 0)
12aaed36 11367 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11368 imported_name_prefix,
11369 (cu->language == language_d ? "." : "::"),
11370 imported_name, (char *) NULL);
13387711
SW
11371 else
11372 canonical_name = imported_name;
794684b6 11373
32019081
JK
11374 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11375 for (child_die = die->child; child_die && child_die->tag;
11376 child_die = sibling_die (child_die))
11377 {
11378 /* DWARF-4: A Fortran use statement with a “rename list” may be
11379 represented by an imported module entry with an import attribute
11380 referring to the module and owned entries corresponding to those
11381 entities that are renamed as part of being imported. */
11382
11383 if (child_die->tag != DW_TAG_imported_declaration)
11384 {
b98664d3 11385 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11386 "- DIE at %s [in module %s]"),
11387 sect_offset_str (child_die->sect_off),
11388 objfile_name (objfile));
32019081
JK
11389 continue;
11390 }
11391
11392 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11393 if (import_attr == NULL)
11394 {
b98664d3 11395 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11396 dwarf_tag_name (child_die->tag));
11397 continue;
11398 }
11399
11400 imported_cu = cu;
11401 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11402 &imported_cu);
11403 imported_name = dwarf2_name (imported_die, imported_cu);
11404 if (imported_name == NULL)
11405 {
b98664d3 11406 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11407 "imported name - DIE at %s [in module %s]"),
11408 sect_offset_str (child_die->sect_off),
11409 objfile_name (objfile));
32019081
JK
11410 continue;
11411 }
11412
eb1e02fd 11413 excludes.push_back (imported_name);
32019081
JK
11414
11415 process_die (child_die, cu);
11416 }
11417
804d2729 11418 add_using_directive (using_directives (cu),
22cee43f
PMR
11419 import_prefix,
11420 canonical_name,
11421 import_alias,
11422 imported_declaration,
11423 excludes,
11424 0,
11425 &objfile->objfile_obstack);
27aa8d6a
SW
11426}
11427
5230b05a
WT
11428/* ICC<14 does not output the required DW_AT_declaration on incomplete
11429 types, but gives them a size of zero. Starting with version 14,
11430 ICC is compatible with GCC. */
11431
9068261f 11432static bool
5230b05a
WT
11433producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11434{
11435 if (!cu->checked_producer)
11436 check_producer (cu);
11437
11438 return cu->producer_is_icc_lt_14;
11439}
11440
eb77c9df
AB
11441/* ICC generates a DW_AT_type for C void functions. This was observed on
11442 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11443 which says that void functions should not have a DW_AT_type. */
11444
11445static bool
11446producer_is_icc (struct dwarf2_cu *cu)
11447{
11448 if (!cu->checked_producer)
11449 check_producer (cu);
11450
11451 return cu->producer_is_icc;
11452}
11453
1b80a9fa
JK
11454/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11455 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11456 this, it was first present in GCC release 4.3.0. */
11457
9068261f 11458static bool
1b80a9fa
JK
11459producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11460{
11461 if (!cu->checked_producer)
11462 check_producer (cu);
11463
11464 return cu->producer_is_gcc_lt_4_3;
11465}
11466
d721ba37
PA
11467static file_and_directory
11468find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11469{
d721ba37
PA
11470 file_and_directory res;
11471
9291a0cd
TT
11472 /* Find the filename. Do not use dwarf2_name here, since the filename
11473 is not a source language identifier. */
d721ba37
PA
11474 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11475 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11476
d721ba37
PA
11477 if (res.comp_dir == NULL
11478 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11479 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11480 {
d721ba37
PA
11481 res.comp_dir_storage = ldirname (res.name);
11482 if (!res.comp_dir_storage.empty ())
11483 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11484 }
d721ba37 11485 if (res.comp_dir != NULL)
9291a0cd
TT
11486 {
11487 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11488 directory, get rid of it. */
d721ba37 11489 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11490
d721ba37
PA
11491 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11492 res.comp_dir = cp + 1;
9291a0cd
TT
11493 }
11494
d721ba37
PA
11495 if (res.name == NULL)
11496 res.name = "<unknown>";
11497
11498 return res;
9291a0cd
TT
11499}
11500
f4dc4d17
DE
11501/* Handle DW_AT_stmt_list for a compilation unit.
11502 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11503 COMP_DIR is the compilation directory. LOWPC is passed to
11504 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11505
11506static void
11507handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11508 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11509{
518817b3
SM
11510 struct dwarf2_per_objfile *dwarf2_per_objfile
11511 = cu->per_cu->dwarf2_per_objfile;
527f3840 11512 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11513 struct attribute *attr;
527f3840
JK
11514 struct line_header line_header_local;
11515 hashval_t line_header_local_hash;
527f3840
JK
11516 void **slot;
11517 int decode_mapping;
2ab95328 11518
f4dc4d17
DE
11519 gdb_assert (! cu->per_cu->is_debug_types);
11520
2ab95328 11521 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11522 if (attr == NULL)
11523 return;
11524
9c541725 11525 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11526
11527 /* The line header hash table is only created if needed (it exists to
11528 prevent redundant reading of the line table for partial_units).
11529 If we're given a partial_unit, we'll need it. If we're given a
11530 compile_unit, then use the line header hash table if it's already
11531 created, but don't create one just yet. */
11532
11533 if (dwarf2_per_objfile->line_header_hash == NULL
11534 && die->tag == DW_TAG_partial_unit)
2ab95328 11535 {
527f3840
JK
11536 dwarf2_per_objfile->line_header_hash
11537 = htab_create_alloc_ex (127, line_header_hash_voidp,
11538 line_header_eq_voidp,
11539 free_line_header_voidp,
11540 &objfile->objfile_obstack,
11541 hashtab_obstack_allocate,
11542 dummy_obstack_deallocate);
11543 }
2ab95328 11544
9c541725 11545 line_header_local.sect_off = line_offset;
527f3840
JK
11546 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11547 line_header_local_hash = line_header_hash (&line_header_local);
11548 if (dwarf2_per_objfile->line_header_hash != NULL)
11549 {
11550 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11551 &line_header_local,
11552 line_header_local_hash, NO_INSERT);
11553
11554 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11555 is not present in *SLOT (since if there is something in *SLOT then
11556 it will be for a partial_unit). */
11557 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11558 {
527f3840 11559 gdb_assert (*slot != NULL);
9a3c8263 11560 cu->line_header = (struct line_header *) *slot;
527f3840 11561 return;
dee91e82 11562 }
2ab95328 11563 }
527f3840
JK
11564
11565 /* dwarf_decode_line_header does not yet provide sufficient information.
11566 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11567 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11568 if (lh == NULL)
527f3840 11569 return;
4c8aa72d
PA
11570
11571 cu->line_header = lh.release ();
11572 cu->line_header_die_owner = die;
527f3840
JK
11573
11574 if (dwarf2_per_objfile->line_header_hash == NULL)
11575 slot = NULL;
11576 else
11577 {
11578 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11579 &line_header_local,
11580 line_header_local_hash, INSERT);
11581 gdb_assert (slot != NULL);
11582 }
11583 if (slot != NULL && *slot == NULL)
11584 {
11585 /* This newly decoded line number information unit will be owned
11586 by line_header_hash hash table. */
11587 *slot = cu->line_header;
4c8aa72d 11588 cu->line_header_die_owner = NULL;
527f3840
JK
11589 }
11590 else
11591 {
11592 /* We cannot free any current entry in (*slot) as that struct line_header
11593 may be already used by multiple CUs. Create only temporary decoded
11594 line_header for this CU - it may happen at most once for each line
11595 number information unit. And if we're not using line_header_hash
11596 then this is what we want as well. */
11597 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11598 }
11599 decode_mapping = (die->tag != DW_TAG_partial_unit);
11600 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11601 decode_mapping);
fff8551c 11602
2ab95328
TT
11603}
11604
95554aad 11605/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11606
c906108c 11607static void
e7c27a73 11608read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11609{
518817b3
SM
11610 struct dwarf2_per_objfile *dwarf2_per_objfile
11611 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11612 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11613 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11614 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11615 CORE_ADDR highpc = ((CORE_ADDR) 0);
11616 struct attribute *attr;
c906108c 11617 struct die_info *child_die;
e142c38c 11618 CORE_ADDR baseaddr;
6e70227d 11619
380618d6 11620 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11621 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11622
fae299cd 11623 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11624
11625 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11626 from finish_block. */
2acceee2 11627 if (lowpc == ((CORE_ADDR) -1))
c906108c 11628 lowpc = highpc;
3e29f34a 11629 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11630
d721ba37 11631 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11632
f4b8a18d
KW
11633 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11634 standardised yet. As a workaround for the language detection we fall
11635 back to the DW_AT_producer string. */
11636 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11637 cu->language = language_opencl;
11638
3019eac3
DE
11639 /* Similar hack for Go. */
11640 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11641 set_cu_language (DW_LANG_Go, cu);
11642
c24bdb02 11643 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11644
11645 /* Decode line number information if present. We do this before
11646 processing child DIEs, so that the line header table is available
11647 for DW_AT_decl_file. */
d721ba37 11648 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11649
11650 /* Process all dies in compilation unit. */
11651 if (die->child != NULL)
11652 {
11653 child_die = die->child;
11654 while (child_die && child_die->tag)
11655 {
11656 process_die (child_die, cu);
11657 child_die = sibling_die (child_die);
11658 }
11659 }
11660
11661 /* Decode macro information, if present. Dwarf 2 macro information
11662 refers to information in the line number info statement program
11663 header, so we can only read it if we've read the header
11664 successfully. */
0af92d60
JK
11665 attr = dwarf2_attr (die, DW_AT_macros, cu);
11666 if (attr == NULL)
11667 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11668 if (attr && cu->line_header)
11669 {
11670 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11671 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11672
43f3e411 11673 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11674 }
11675 else
11676 {
11677 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11678 if (attr && cu->line_header)
11679 {
11680 unsigned int macro_offset = DW_UNSND (attr);
11681
43f3e411 11682 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11683 }
11684 }
3019eac3
DE
11685}
11686
c24bdb02
KS
11687void
11688dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11689{
f4dc4d17
DE
11690 struct type_unit_group *tu_group;
11691 int first_time;
3019eac3 11692 struct attribute *attr;
9c541725 11693 unsigned int i;
0186c6a7 11694 struct signatured_type *sig_type;
3019eac3 11695
f4dc4d17 11696 gdb_assert (per_cu->is_debug_types);
0186c6a7 11697 sig_type = (struct signatured_type *) per_cu;
3019eac3 11698
c24bdb02 11699 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11700
f4dc4d17 11701 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11702 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11703 if (sig_type->type_unit_group == NULL)
c24bdb02 11704 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11705 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11706
11707 /* If we've already processed this stmt_list there's no real need to
11708 do it again, we could fake it and just recreate the part we need
11709 (file name,index -> symtab mapping). If data shows this optimization
11710 is useful we can do it then. */
43f3e411 11711 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11712
11713 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11714 debug info. */
fff8551c 11715 line_header_up lh;
f4dc4d17 11716 if (attr != NULL)
3019eac3 11717 {
9c541725 11718 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11719 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11720 }
11721 if (lh == NULL)
11722 {
11723 if (first_time)
c24bdb02 11724 start_symtab ("", NULL, 0);
f4dc4d17
DE
11725 else
11726 {
11727 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11728 gdb_assert (m_builder == nullptr);
804d2729 11729 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11730 m_builder.reset (new struct buildsym_compunit
11731 (COMPUNIT_OBJFILE (cust), "",
11732 COMPUNIT_DIRNAME (cust),
11733 compunit_language (cust),
11734 0, cust));
f4dc4d17 11735 }
f4dc4d17 11736 return;
3019eac3
DE
11737 }
11738
c24bdb02
KS
11739 line_header = lh.release ();
11740 line_header_die_owner = die;
3019eac3 11741
f4dc4d17
DE
11742 if (first_time)
11743 {
c24bdb02 11744 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11745
1fd60fc0
DE
11746 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11747 still initializing it, and our caller (a few levels up)
11748 process_full_type_unit still needs to know if this is the first
11749 time. */
11750
7ba99d21 11751 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11752 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11753 line_header->file_names_size ());
3019eac3 11754
7ba99d21
AT
11755 auto &file_names = line_header->file_names ();
11756 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11757 {
7ba99d21 11758 file_entry &fe = file_names[i];
c24bdb02
KS
11759 dwarf2_start_subfile (this, fe.name,
11760 fe.include_dir (line_header));
11761 buildsym_compunit *b = get_builder ();
11762 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11763 {
4c8aa72d
PA
11764 /* NOTE: start_subfile will recognize when it's been
11765 passed a file it has already seen. So we can't
11766 assume there's a simple mapping from
11767 cu->line_header->file_names to subfiles, plus
11768 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11769 b->get_current_subfile ()->symtab
11770 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11771 }
11772
c24bdb02 11773 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11774 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11775 }
11776 }
11777 else
3019eac3 11778 {
c24bdb02 11779 gdb_assert (m_builder == nullptr);
804d2729 11780 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11781 m_builder.reset (new struct buildsym_compunit
11782 (COMPUNIT_OBJFILE (cust), "",
11783 COMPUNIT_DIRNAME (cust),
11784 compunit_language (cust),
11785 0, cust));
f4dc4d17 11786
7ba99d21
AT
11787 auto &file_names = line_header->file_names ();
11788 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11789 {
7ba99d21 11790 file_entry &fe = file_names[i];
4c8aa72d 11791 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11792 }
3019eac3
DE
11793 }
11794
f4dc4d17
DE
11795 /* The main symtab is allocated last. Type units don't have DW_AT_name
11796 so they don't have a "real" (so to speak) symtab anyway.
11797 There is later code that will assign the main symtab to all symbols
11798 that don't have one. We need to handle the case of a symbol with a
11799 missing symtab (DW_AT_decl_file) anyway. */
11800}
3019eac3 11801
f4dc4d17
DE
11802/* Process DW_TAG_type_unit.
11803 For TUs we want to skip the first top level sibling if it's not the
11804 actual type being defined by this TU. In this case the first top
11805 level sibling is there to provide context only. */
3019eac3 11806
f4dc4d17
DE
11807static void
11808read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11809{
11810 struct die_info *child_die;
3019eac3 11811
f4dc4d17
DE
11812 prepare_one_comp_unit (cu, die, language_minimal);
11813
11814 /* Initialize (or reinitialize) the machinery for building symtabs.
11815 We do this before processing child DIEs, so that the line header table
11816 is available for DW_AT_decl_file. */
c24bdb02 11817 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11818
11819 if (die->child != NULL)
11820 {
11821 child_die = die->child;
11822 while (child_die && child_die->tag)
11823 {
11824 process_die (child_die, cu);
11825 child_die = sibling_die (child_die);
11826 }
11827 }
3019eac3
DE
11828}
11829\f
80626a55
DE
11830/* DWO/DWP files.
11831
11832 http://gcc.gnu.org/wiki/DebugFission
11833 http://gcc.gnu.org/wiki/DebugFissionDWP
11834
11835 To simplify handling of both DWO files ("object" files with the DWARF info)
11836 and DWP files (a file with the DWOs packaged up into one file), we treat
11837 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11838
11839static hashval_t
11840hash_dwo_file (const void *item)
11841{
9a3c8263 11842 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11843 hashval_t hash;
3019eac3 11844
a2ce51a0
DE
11845 hash = htab_hash_string (dwo_file->dwo_name);
11846 if (dwo_file->comp_dir != NULL)
11847 hash += htab_hash_string (dwo_file->comp_dir);
11848 return hash;
3019eac3
DE
11849}
11850
11851static int
11852eq_dwo_file (const void *item_lhs, const void *item_rhs)
11853{
9a3c8263
SM
11854 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11855 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11856
a2ce51a0
DE
11857 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11858 return 0;
11859 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11860 return lhs->comp_dir == rhs->comp_dir;
11861 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11862}
11863
11864/* Allocate a hash table for DWO files. */
11865
51ac9db5 11866static htab_up
ed2dc618 11867allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11868{
51ac9db5
SM
11869 auto delete_dwo_file = [] (void *item)
11870 {
11871 struct dwo_file *dwo_file = (struct dwo_file *) item;
11872
11873 delete dwo_file;
11874 };
11875
11876 return htab_up (htab_create_alloc_ex (41,
11877 hash_dwo_file,
11878 eq_dwo_file,
11879 delete_dwo_file,
11880 &objfile->objfile_obstack,
11881 hashtab_obstack_allocate,
11882 dummy_obstack_deallocate));
3019eac3
DE
11883}
11884
80626a55
DE
11885/* Lookup DWO file DWO_NAME. */
11886
11887static void **
ed2dc618
SM
11888lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11889 const char *dwo_name,
11890 const char *comp_dir)
80626a55
DE
11891{
11892 struct dwo_file find_entry;
11893 void **slot;
11894
11895 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11896 dwarf2_per_objfile->dwo_files
11897 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11898
0ac5b59e
DE
11899 find_entry.dwo_name = dwo_name;
11900 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11901 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11902 INSERT);
80626a55
DE
11903
11904 return slot;
11905}
11906
3019eac3
DE
11907static hashval_t
11908hash_dwo_unit (const void *item)
11909{
9a3c8263 11910 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11911
11912 /* This drops the top 32 bits of the id, but is ok for a hash. */
11913 return dwo_unit->signature;
11914}
11915
11916static int
11917eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11918{
9a3c8263
SM
11919 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11920 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11921
11922 /* The signature is assumed to be unique within the DWO file.
11923 So while object file CU dwo_id's always have the value zero,
11924 that's OK, assuming each object file DWO file has only one CU,
11925 and that's the rule for now. */
11926 return lhs->signature == rhs->signature;
11927}
11928
11929/* Allocate a hash table for DWO CUs,TUs.
11930 There is one of these tables for each of CUs,TUs for each DWO file. */
11931
11932static htab_t
11933allocate_dwo_unit_table (struct objfile *objfile)
11934{
11935 /* Start out with a pretty small number.
11936 Generally DWO files contain only one CU and maybe some TUs. */
11937 return htab_create_alloc_ex (3,
11938 hash_dwo_unit,
11939 eq_dwo_unit,
11940 NULL,
11941 &objfile->objfile_obstack,
11942 hashtab_obstack_allocate,
11943 dummy_obstack_deallocate);
11944}
11945
80626a55 11946/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11947
19c3d4c9 11948struct create_dwo_cu_data
3019eac3
DE
11949{
11950 struct dwo_file *dwo_file;
19c3d4c9 11951 struct dwo_unit dwo_unit;
3019eac3
DE
11952};
11953
19c3d4c9 11954/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11955
11956static void
19c3d4c9
DE
11957create_dwo_cu_reader (const struct die_reader_specs *reader,
11958 const gdb_byte *info_ptr,
11959 struct die_info *comp_unit_die,
11960 int has_children,
11961 void *datap)
3019eac3
DE
11962{
11963 struct dwarf2_cu *cu = reader->cu;
9c541725 11964 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11965 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11966 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11967 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11968 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11969
a084a2a6
AT
11970 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11971 if (!signature.has_value ())
3019eac3 11972 {
b98664d3 11973 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11974 " its dwo_id [in module %s]"),
9d8780f0 11975 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11976 return;
11977 }
11978
3019eac3 11979 dwo_unit->dwo_file = dwo_file;
a084a2a6 11980 dwo_unit->signature = *signature;
8a0459fd 11981 dwo_unit->section = section;
9c541725 11982 dwo_unit->sect_off = sect_off;
3019eac3
DE
11983 dwo_unit->length = cu->per_cu->length;
11984
b4f54984 11985 if (dwarf_read_debug)
9d8780f0
SM
11986 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11987 sect_offset_str (sect_off),
9c541725 11988 hex_string (dwo_unit->signature));
3019eac3
DE
11989}
11990
33c5cd75 11991/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11992 Note: This function processes DWO files only, not DWP files. */
3019eac3 11993
33c5cd75 11994static void
ed2dc618
SM
11995create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11996 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11997 htab_t &cus_htab)
3019eac3
DE
11998{
11999 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 12000 const gdb_byte *info_ptr, *end_ptr;
3019eac3 12001
33c5cd75
DB
12002 dwarf2_read_section (objfile, &section);
12003 info_ptr = section.buffer;
3019eac3
DE
12004
12005 if (info_ptr == NULL)
33c5cd75 12006 return;
3019eac3 12007
b4f54984 12008 if (dwarf_read_debug)
19c3d4c9
DE
12009 {
12010 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
12011 get_section_name (&section),
12012 get_section_file_name (&section));
19c3d4c9 12013 }
3019eac3 12014
33c5cd75 12015 end_ptr = info_ptr + section.size;
3019eac3
DE
12016 while (info_ptr < end_ptr)
12017 {
12018 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12019 struct create_dwo_cu_data create_dwo_cu_data;
12020 struct dwo_unit *dwo_unit;
12021 void **slot;
12022 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12023
19c3d4c9
DE
12024 memset (&create_dwo_cu_data.dwo_unit, 0,
12025 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12026 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12027 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12028 per_cu.is_debug_types = 0;
33c5cd75
DB
12029 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12030 per_cu.section = &section;
c5ed0576 12031 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12032
12033 init_cutu_and_read_dies_no_follow (
12034 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12035 info_ptr += per_cu.length;
12036
12037 // If the unit could not be parsed, skip it.
12038 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12039 continue;
3019eac3 12040
33c5cd75
DB
12041 if (cus_htab == NULL)
12042 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12043
33c5cd75
DB
12044 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12045 *dwo_unit = create_dwo_cu_data.dwo_unit;
12046 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12047 gdb_assert (slot != NULL);
12048 if (*slot != NULL)
19c3d4c9 12049 {
33c5cd75
DB
12050 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12051 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12052
b98664d3 12053 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12054 " the entry at offset %s, signature %s"),
12055 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12056 hex_string (dwo_unit->signature));
19c3d4c9 12057 }
33c5cd75 12058 *slot = (void *)dwo_unit;
3019eac3 12059 }
3019eac3
DE
12060}
12061
80626a55
DE
12062/* DWP file .debug_{cu,tu}_index section format:
12063 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12064
d2415c6c
DE
12065 DWP Version 1:
12066
80626a55
DE
12067 Both index sections have the same format, and serve to map a 64-bit
12068 signature to a set of section numbers. Each section begins with a header,
12069 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12070 indexes, and a pool of 32-bit section numbers. The index sections will be
12071 aligned at 8-byte boundaries in the file.
12072
d2415c6c
DE
12073 The index section header consists of:
12074
12075 V, 32 bit version number
12076 -, 32 bits unused
12077 N, 32 bit number of compilation units or type units in the index
12078 M, 32 bit number of slots in the hash table
80626a55 12079
d2415c6c 12080 Numbers are recorded using the byte order of the application binary.
80626a55 12081
d2415c6c
DE
12082 The hash table begins at offset 16 in the section, and consists of an array
12083 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12084 order of the application binary). Unused slots in the hash table are 0.
12085 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12086
d2415c6c
DE
12087 The parallel table begins immediately after the hash table
12088 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12089 array of 32-bit indexes (using the byte order of the application binary),
12090 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12091 table contains a 32-bit index into the pool of section numbers. For unused
12092 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12093
73869dc2
DE
12094 The pool of section numbers begins immediately following the hash table
12095 (at offset 16 + 12 * M from the beginning of the section). The pool of
12096 section numbers consists of an array of 32-bit words (using the byte order
12097 of the application binary). Each item in the array is indexed starting
12098 from 0. The hash table entry provides the index of the first section
12099 number in the set. Additional section numbers in the set follow, and the
12100 set is terminated by a 0 entry (section number 0 is not used in ELF).
12101
12102 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12103 section must be the first entry in the set, and the .debug_abbrev.dwo must
12104 be the second entry. Other members of the set may follow in any order.
12105
12106 ---
12107
12108 DWP Version 2:
12109
12110 DWP Version 2 combines all the .debug_info, etc. sections into one,
12111 and the entries in the index tables are now offsets into these sections.
12112 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12113 section.
12114
12115 Index Section Contents:
12116 Header
12117 Hash Table of Signatures dwp_hash_table.hash_table
12118 Parallel Table of Indices dwp_hash_table.unit_table
12119 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12120 Table of Section Sizes dwp_hash_table.v2.sizes
12121
12122 The index section header consists of:
12123
12124 V, 32 bit version number
12125 L, 32 bit number of columns in the table of section offsets
12126 N, 32 bit number of compilation units or type units in the index
12127 M, 32 bit number of slots in the hash table
12128
12129 Numbers are recorded using the byte order of the application binary.
12130
12131 The hash table has the same format as version 1.
12132 The parallel table of indices has the same format as version 1,
12133 except that the entries are origin-1 indices into the table of sections
12134 offsets and the table of section sizes.
12135
12136 The table of offsets begins immediately following the parallel table
12137 (at offset 16 + 12 * M from the beginning of the section). The table is
12138 a two-dimensional array of 32-bit words (using the byte order of the
12139 application binary), with L columns and N+1 rows, in row-major order.
12140 Each row in the array is indexed starting from 0. The first row provides
12141 a key to the remaining rows: each column in this row provides an identifier
12142 for a debug section, and the offsets in the same column of subsequent rows
12143 refer to that section. The section identifiers are:
12144
12145 DW_SECT_INFO 1 .debug_info.dwo
12146 DW_SECT_TYPES 2 .debug_types.dwo
12147 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12148 DW_SECT_LINE 4 .debug_line.dwo
12149 DW_SECT_LOC 5 .debug_loc.dwo
12150 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12151 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12152 DW_SECT_MACRO 8 .debug_macro.dwo
12153
12154 The offsets provided by the CU and TU index sections are the base offsets
12155 for the contributions made by each CU or TU to the corresponding section
12156 in the package file. Each CU and TU header contains an abbrev_offset
12157 field, used to find the abbreviations table for that CU or TU within the
12158 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12159 be interpreted as relative to the base offset given in the index section.
12160 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12161 should be interpreted as relative to the base offset for .debug_line.dwo,
12162 and offsets into other debug sections obtained from DWARF attributes should
12163 also be interpreted as relative to the corresponding base offset.
12164
12165 The table of sizes begins immediately following the table of offsets.
12166 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12167 with L columns and N rows, in row-major order. Each row in the array is
12168 indexed starting from 1 (row 0 is shared by the two tables).
12169
12170 ---
12171
12172 Hash table lookup is handled the same in version 1 and 2:
12173
12174 We assume that N and M will not exceed 2^32 - 1.
12175 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12176
d2415c6c
DE
12177 Given a 64-bit compilation unit signature or a type signature S, an entry
12178 in the hash table is located as follows:
80626a55 12179
d2415c6c
DE
12180 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12181 the low-order k bits all set to 1.
80626a55 12182
d2415c6c 12183 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12184
d2415c6c
DE
12185 3) If the hash table entry at index H matches the signature, use that
12186 entry. If the hash table entry at index H is unused (all zeroes),
12187 terminate the search: the signature is not present in the table.
80626a55 12188
d2415c6c 12189 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12190
d2415c6c 12191 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12192 to stop at an unused slot or find the match. */
80626a55
DE
12193
12194/* Create a hash table to map DWO IDs to their CU/TU entry in
12195 .debug_{info,types}.dwo in DWP_FILE.
12196 Returns NULL if there isn't one.
12197 Note: This function processes DWP files only, not DWO files. */
12198
12199static struct dwp_hash_table *
ed2dc618
SM
12200create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12201 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12202{
12203 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12204 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12205 const gdb_byte *index_ptr, *index_end;
80626a55 12206 struct dwarf2_section_info *index;
73869dc2 12207 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12208 struct dwp_hash_table *htab;
12209
12210 if (is_debug_types)
12211 index = &dwp_file->sections.tu_index;
12212 else
12213 index = &dwp_file->sections.cu_index;
12214
12215 if (dwarf2_section_empty_p (index))
12216 return NULL;
12217 dwarf2_read_section (objfile, index);
12218
12219 index_ptr = index->buffer;
12220 index_end = index_ptr + index->size;
12221
12222 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12223 index_ptr += 4;
12224 if (version == 2)
12225 nr_columns = read_4_bytes (dbfd, index_ptr);
12226 else
12227 nr_columns = 0;
12228 index_ptr += 4;
80626a55
DE
12229 nr_units = read_4_bytes (dbfd, index_ptr);
12230 index_ptr += 4;
12231 nr_slots = read_4_bytes (dbfd, index_ptr);
12232 index_ptr += 4;
12233
73869dc2 12234 if (version != 1 && version != 2)
80626a55 12235 {
21aa081e 12236 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12237 " [in module %s]"),
21aa081e 12238 pulongest (version), dwp_file->name);
80626a55
DE
12239 }
12240 if (nr_slots != (nr_slots & -nr_slots))
12241 {
21aa081e 12242 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12243 " is not power of 2 [in module %s]"),
21aa081e 12244 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12245 }
12246
12247 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12248 htab->version = version;
12249 htab->nr_columns = nr_columns;
80626a55
DE
12250 htab->nr_units = nr_units;
12251 htab->nr_slots = nr_slots;
12252 htab->hash_table = index_ptr;
12253 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12254
12255 /* Exit early if the table is empty. */
12256 if (nr_slots == 0 || nr_units == 0
12257 || (version == 2 && nr_columns == 0))
12258 {
12259 /* All must be zero. */
12260 if (nr_slots != 0 || nr_units != 0
12261 || (version == 2 && nr_columns != 0))
12262 {
b98664d3 12263 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12264 " all zero [in modules %s]"),
12265 dwp_file->name);
12266 }
12267 return htab;
12268 }
12269
12270 if (version == 1)
12271 {
12272 htab->section_pool.v1.indices =
12273 htab->unit_table + sizeof (uint32_t) * nr_slots;
12274 /* It's harder to decide whether the section is too small in v1.
12275 V1 is deprecated anyway so we punt. */
12276 }
12277 else
12278 {
12279 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12280 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12281 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12282 /* Reverse map for error checking. */
12283 int ids_seen[DW_SECT_MAX + 1];
12284 int i;
12285
12286 if (nr_columns < 2)
12287 {
12288 error (_("Dwarf Error: bad DWP hash table, too few columns"
12289 " in section table [in module %s]"),
12290 dwp_file->name);
12291 }
12292 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12293 {
12294 error (_("Dwarf Error: bad DWP hash table, too many columns"
12295 " in section table [in module %s]"),
12296 dwp_file->name);
12297 }
04fd5eed
GB
12298 memset (ids, 255, sizeof_ids);
12299 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12300 for (i = 0; i < nr_columns; ++i)
12301 {
12302 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12303
12304 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12305 {
12306 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12307 " in section table [in module %s]"),
12308 id, dwp_file->name);
12309 }
12310 if (ids_seen[id] != -1)
12311 {
12312 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12313 " id %d in section table [in module %s]"),
12314 id, dwp_file->name);
12315 }
12316 ids_seen[id] = i;
12317 ids[i] = id;
12318 }
12319 /* Must have exactly one info or types section. */
12320 if (((ids_seen[DW_SECT_INFO] != -1)
12321 + (ids_seen[DW_SECT_TYPES] != -1))
12322 != 1)
12323 {
12324 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12325 " DWO info/types section [in module %s]"),
12326 dwp_file->name);
12327 }
12328 /* Must have an abbrev section. */
12329 if (ids_seen[DW_SECT_ABBREV] == -1)
12330 {
12331 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12332 " section [in module %s]"),
12333 dwp_file->name);
12334 }
12335 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12336 htab->section_pool.v2.sizes =
12337 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12338 * nr_units * nr_columns);
12339 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12340 * nr_units * nr_columns))
12341 > index_end)
12342 {
12343 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12344 " [in module %s]"),
12345 dwp_file->name);
12346 }
12347 }
80626a55
DE
12348
12349 return htab;
12350}
12351
12352/* Update SECTIONS with the data from SECTP.
12353
12354 This function is like the other "locate" section routines that are
12355 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12356 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12357
12358 The result is non-zero for success, or zero if an error was found. */
12359
12360static int
73869dc2
DE
12361locate_v1_virtual_dwo_sections (asection *sectp,
12362 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12363{
12364 const struct dwop_section_names *names = &dwop_section_names;
12365
12366 if (section_is_p (sectp->name, &names->abbrev_dwo))
12367 {
12368 /* There can be only one. */
049412e3 12369 if (sections->abbrev.s.section != NULL)
80626a55 12370 return 0;
049412e3 12371 sections->abbrev.s.section = sectp;
fd361982 12372 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12373 }
12374 else if (section_is_p (sectp->name, &names->info_dwo)
12375 || section_is_p (sectp->name, &names->types_dwo))
12376 {
12377 /* There can be only one. */
049412e3 12378 if (sections->info_or_types.s.section != NULL)
80626a55 12379 return 0;
049412e3 12380 sections->info_or_types.s.section = sectp;
fd361982 12381 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12382 }
12383 else if (section_is_p (sectp->name, &names->line_dwo))
12384 {
12385 /* There can be only one. */
049412e3 12386 if (sections->line.s.section != NULL)
80626a55 12387 return 0;
049412e3 12388 sections->line.s.section = sectp;
fd361982 12389 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12390 }
12391 else if (section_is_p (sectp->name, &names->loc_dwo))
12392 {
12393 /* There can be only one. */
049412e3 12394 if (sections->loc.s.section != NULL)
80626a55 12395 return 0;
049412e3 12396 sections->loc.s.section = sectp;
fd361982 12397 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12398 }
12399 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12400 {
12401 /* There can be only one. */
049412e3 12402 if (sections->macinfo.s.section != NULL)
80626a55 12403 return 0;
049412e3 12404 sections->macinfo.s.section = sectp;
fd361982 12405 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12406 }
12407 else if (section_is_p (sectp->name, &names->macro_dwo))
12408 {
12409 /* There can be only one. */
049412e3 12410 if (sections->macro.s.section != NULL)
80626a55 12411 return 0;
049412e3 12412 sections->macro.s.section = sectp;
fd361982 12413 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12414 }
12415 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12416 {
12417 /* There can be only one. */
049412e3 12418 if (sections->str_offsets.s.section != NULL)
80626a55 12419 return 0;
049412e3 12420 sections->str_offsets.s.section = sectp;
fd361982 12421 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12422 }
12423 else
12424 {
12425 /* No other kind of section is valid. */
12426 return 0;
12427 }
12428
12429 return 1;
12430}
12431
73869dc2
DE
12432/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12433 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12434 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12435 This is for DWP version 1 files. */
80626a55
DE
12436
12437static struct dwo_unit *
ed2dc618
SM
12438create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12439 struct dwp_file *dwp_file,
73869dc2
DE
12440 uint32_t unit_index,
12441 const char *comp_dir,
12442 ULONGEST signature, int is_debug_types)
80626a55
DE
12443{
12444 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12445 const struct dwp_hash_table *dwp_htab =
12446 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12447 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12448 const char *kind = is_debug_types ? "TU" : "CU";
12449 struct dwo_file *dwo_file;
12450 struct dwo_unit *dwo_unit;
73869dc2 12451 struct virtual_v1_dwo_sections sections;
80626a55 12452 void **dwo_file_slot;
80626a55
DE
12453 int i;
12454
73869dc2
DE
12455 gdb_assert (dwp_file->version == 1);
12456
b4f54984 12457 if (dwarf_read_debug)
80626a55 12458 {
73869dc2 12459 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12460 kind,
73869dc2 12461 pulongest (unit_index), hex_string (signature),
80626a55
DE
12462 dwp_file->name);
12463 }
12464
19ac8c2e 12465 /* Fetch the sections of this DWO unit.
80626a55
DE
12466 Put a limit on the number of sections we look for so that bad data
12467 doesn't cause us to loop forever. */
12468
73869dc2 12469#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12470 (1 /* .debug_info or .debug_types */ \
12471 + 1 /* .debug_abbrev */ \
12472 + 1 /* .debug_line */ \
12473 + 1 /* .debug_loc */ \
12474 + 1 /* .debug_str_offsets */ \
19ac8c2e 12475 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12476 + 1 /* trailing zero */)
12477
12478 memset (&sections, 0, sizeof (sections));
80626a55 12479
73869dc2 12480 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12481 {
12482 asection *sectp;
12483 uint32_t section_nr =
12484 read_4_bytes (dbfd,
73869dc2
DE
12485 dwp_htab->section_pool.v1.indices
12486 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12487
12488 if (section_nr == 0)
12489 break;
12490 if (section_nr >= dwp_file->num_sections)
12491 {
12492 error (_("Dwarf Error: bad DWP hash table, section number too large"
12493 " [in module %s]"),
12494 dwp_file->name);
12495 }
12496
12497 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12498 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12499 {
12500 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12501 " [in module %s]"),
12502 dwp_file->name);
12503 }
12504 }
12505
12506 if (i < 2
a32a8923
DE
12507 || dwarf2_section_empty_p (&sections.info_or_types)
12508 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12509 {
12510 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12511 " [in module %s]"),
12512 dwp_file->name);
12513 }
73869dc2 12514 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12515 {
12516 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12517 " [in module %s]"),
12518 dwp_file->name);
12519 }
12520
12521 /* It's easier for the rest of the code if we fake a struct dwo_file and
12522 have dwo_unit "live" in that. At least for now.
12523
12524 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12525 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12526 file, we can combine them back into a virtual DWO file to save space
12527 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12528 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12529
791afaa2
TT
12530 std::string virtual_dwo_name =
12531 string_printf ("virtual-dwo/%d-%d-%d-%d",
12532 get_section_id (&sections.abbrev),
12533 get_section_id (&sections.line),
12534 get_section_id (&sections.loc),
12535 get_section_id (&sections.str_offsets));
80626a55 12536 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12537 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12538 virtual_dwo_name.c_str (),
12539 comp_dir);
80626a55
DE
12540 /* Create one if necessary. */
12541 if (*dwo_file_slot == NULL)
12542 {
b4f54984 12543 if (dwarf_read_debug)
80626a55
DE
12544 {
12545 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12546 virtual_dwo_name.c_str ());
80626a55 12547 }
51ac9db5 12548 dwo_file = new struct dwo_file;
efba19b0
TT
12549 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12550 virtual_dwo_name);
0ac5b59e 12551 dwo_file->comp_dir = comp_dir;
80626a55
DE
12552 dwo_file->sections.abbrev = sections.abbrev;
12553 dwo_file->sections.line = sections.line;
12554 dwo_file->sections.loc = sections.loc;
12555 dwo_file->sections.macinfo = sections.macinfo;
12556 dwo_file->sections.macro = sections.macro;
12557 dwo_file->sections.str_offsets = sections.str_offsets;
12558 /* The "str" section is global to the entire DWP file. */
12559 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12560 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12561 there's no need to record it in dwo_file.
12562 Also, we can't simply record type sections in dwo_file because
12563 we record a pointer into the vector in dwo_unit. As we collect more
12564 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12565 for it, invalidating all copies of pointers into the previous
12566 contents. */
80626a55
DE
12567 *dwo_file_slot = dwo_file;
12568 }
12569 else
12570 {
b4f54984 12571 if (dwarf_read_debug)
80626a55
DE
12572 {
12573 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12574 virtual_dwo_name.c_str ());
80626a55 12575 }
9a3c8263 12576 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12577 }
80626a55
DE
12578
12579 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12580 dwo_unit->dwo_file = dwo_file;
12581 dwo_unit->signature = signature;
8d749320
SM
12582 dwo_unit->section =
12583 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12584 *dwo_unit->section = sections.info_or_types;
57d63ce2 12585 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12586
12587 return dwo_unit;
12588}
12589
73869dc2
DE
12590/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12591 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12592 piece within that section used by a TU/CU, return a virtual section
12593 of just that piece. */
12594
12595static struct dwarf2_section_info
ed2dc618
SM
12596create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12597 struct dwarf2_section_info *section,
73869dc2
DE
12598 bfd_size_type offset, bfd_size_type size)
12599{
12600 struct dwarf2_section_info result;
12601 asection *sectp;
12602
12603 gdb_assert (section != NULL);
12604 gdb_assert (!section->is_virtual);
12605
12606 memset (&result, 0, sizeof (result));
12607 result.s.containing_section = section;
dc4ccb6f 12608 result.is_virtual = true;
73869dc2
DE
12609
12610 if (size == 0)
12611 return result;
12612
12613 sectp = get_section_bfd_section (section);
12614
12615 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12616 bounds of the real section. This is a pretty-rare event, so just
12617 flag an error (easier) instead of a warning and trying to cope. */
12618 if (sectp == NULL
fd361982 12619 || offset + size > bfd_section_size (sectp))
73869dc2 12620 {
73869dc2
DE
12621 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12622 " in section %s [in module %s]"),
fd361982 12623 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12624 objfile_name (dwarf2_per_objfile->objfile));
12625 }
12626
12627 result.virtual_offset = offset;
12628 result.size = size;
12629 return result;
12630}
12631
12632/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12633 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12634 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12635 This is for DWP version 2 files. */
12636
12637static struct dwo_unit *
ed2dc618
SM
12638create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12639 struct dwp_file *dwp_file,
73869dc2
DE
12640 uint32_t unit_index,
12641 const char *comp_dir,
12642 ULONGEST signature, int is_debug_types)
12643{
12644 struct objfile *objfile = dwarf2_per_objfile->objfile;
12645 const struct dwp_hash_table *dwp_htab =
12646 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12647 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12648 const char *kind = is_debug_types ? "TU" : "CU";
12649 struct dwo_file *dwo_file;
12650 struct dwo_unit *dwo_unit;
12651 struct virtual_v2_dwo_sections sections;
12652 void **dwo_file_slot;
73869dc2
DE
12653 int i;
12654
12655 gdb_assert (dwp_file->version == 2);
12656
b4f54984 12657 if (dwarf_read_debug)
73869dc2
DE
12658 {
12659 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12660 kind,
12661 pulongest (unit_index), hex_string (signature),
12662 dwp_file->name);
12663 }
12664
12665 /* Fetch the section offsets of this DWO unit. */
12666
12667 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12668
12669 for (i = 0; i < dwp_htab->nr_columns; ++i)
12670 {
12671 uint32_t offset = read_4_bytes (dbfd,
12672 dwp_htab->section_pool.v2.offsets
12673 + (((unit_index - 1) * dwp_htab->nr_columns
12674 + i)
12675 * sizeof (uint32_t)));
12676 uint32_t size = read_4_bytes (dbfd,
12677 dwp_htab->section_pool.v2.sizes
12678 + (((unit_index - 1) * dwp_htab->nr_columns
12679 + i)
12680 * sizeof (uint32_t)));
12681
12682 switch (dwp_htab->section_pool.v2.section_ids[i])
12683 {
12684 case DW_SECT_INFO:
12685 case DW_SECT_TYPES:
12686 sections.info_or_types_offset = offset;
12687 sections.info_or_types_size = size;
12688 break;
12689 case DW_SECT_ABBREV:
12690 sections.abbrev_offset = offset;
12691 sections.abbrev_size = size;
12692 break;
12693 case DW_SECT_LINE:
12694 sections.line_offset = offset;
12695 sections.line_size = size;
12696 break;
12697 case DW_SECT_LOC:
12698 sections.loc_offset = offset;
12699 sections.loc_size = size;
12700 break;
12701 case DW_SECT_STR_OFFSETS:
12702 sections.str_offsets_offset = offset;
12703 sections.str_offsets_size = size;
12704 break;
12705 case DW_SECT_MACINFO:
12706 sections.macinfo_offset = offset;
12707 sections.macinfo_size = size;
12708 break;
12709 case DW_SECT_MACRO:
12710 sections.macro_offset = offset;
12711 sections.macro_size = size;
12712 break;
12713 }
12714 }
12715
12716 /* It's easier for the rest of the code if we fake a struct dwo_file and
12717 have dwo_unit "live" in that. At least for now.
12718
12719 The DWP file can be made up of a random collection of CUs and TUs.
12720 However, for each CU + set of TUs that came from the same original DWO
12721 file, we can combine them back into a virtual DWO file to save space
12722 (fewer struct dwo_file objects to allocate). Remember that for really
12723 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12724
791afaa2
TT
12725 std::string virtual_dwo_name =
12726 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12727 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12728 (long) (sections.line_size ? sections.line_offset : 0),
12729 (long) (sections.loc_size ? sections.loc_offset : 0),
12730 (long) (sections.str_offsets_size
12731 ? sections.str_offsets_offset : 0));
73869dc2 12732 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12733 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12734 virtual_dwo_name.c_str (),
12735 comp_dir);
73869dc2
DE
12736 /* Create one if necessary. */
12737 if (*dwo_file_slot == NULL)
12738 {
b4f54984 12739 if (dwarf_read_debug)
73869dc2
DE
12740 {
12741 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12742 virtual_dwo_name.c_str ());
73869dc2 12743 }
51ac9db5 12744 dwo_file = new struct dwo_file;
efba19b0
TT
12745 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12746 virtual_dwo_name);
73869dc2
DE
12747 dwo_file->comp_dir = comp_dir;
12748 dwo_file->sections.abbrev =
ed2dc618 12749 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12750 sections.abbrev_offset, sections.abbrev_size);
12751 dwo_file->sections.line =
ed2dc618 12752 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12753 sections.line_offset, sections.line_size);
12754 dwo_file->sections.loc =
ed2dc618 12755 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12756 sections.loc_offset, sections.loc_size);
12757 dwo_file->sections.macinfo =
ed2dc618 12758 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12759 sections.macinfo_offset, sections.macinfo_size);
12760 dwo_file->sections.macro =
ed2dc618 12761 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12762 sections.macro_offset, sections.macro_size);
12763 dwo_file->sections.str_offsets =
ed2dc618
SM
12764 create_dwp_v2_section (dwarf2_per_objfile,
12765 &dwp_file->sections.str_offsets,
73869dc2
DE
12766 sections.str_offsets_offset,
12767 sections.str_offsets_size);
12768 /* The "str" section is global to the entire DWP file. */
12769 dwo_file->sections.str = dwp_file->sections.str;
12770 /* The info or types section is assigned below to dwo_unit,
12771 there's no need to record it in dwo_file.
12772 Also, we can't simply record type sections in dwo_file because
12773 we record a pointer into the vector in dwo_unit. As we collect more
12774 types we'll grow the vector and eventually have to reallocate space
12775 for it, invalidating all copies of pointers into the previous
12776 contents. */
12777 *dwo_file_slot = dwo_file;
12778 }
12779 else
12780 {
b4f54984 12781 if (dwarf_read_debug)
73869dc2
DE
12782 {
12783 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12784 virtual_dwo_name.c_str ());
73869dc2 12785 }
9a3c8263 12786 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12787 }
73869dc2
DE
12788
12789 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12790 dwo_unit->dwo_file = dwo_file;
12791 dwo_unit->signature = signature;
8d749320
SM
12792 dwo_unit->section =
12793 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12794 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12795 is_debug_types
73869dc2
DE
12796 ? &dwp_file->sections.types
12797 : &dwp_file->sections.info,
12798 sections.info_or_types_offset,
12799 sections.info_or_types_size);
12800 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12801
12802 return dwo_unit;
12803}
12804
57d63ce2
DE
12805/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12806 Returns NULL if the signature isn't found. */
80626a55
DE
12807
12808static struct dwo_unit *
ed2dc618
SM
12809lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12810 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12811 ULONGEST signature, int is_debug_types)
80626a55 12812{
57d63ce2
DE
12813 const struct dwp_hash_table *dwp_htab =
12814 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12815 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12816 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12817 uint32_t hash = signature & mask;
12818 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12819 unsigned int i;
12820 void **slot;
870f88f7 12821 struct dwo_unit find_dwo_cu;
80626a55
DE
12822
12823 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12824 find_dwo_cu.signature = signature;
19ac8c2e
DE
12825 slot = htab_find_slot (is_debug_types
12826 ? dwp_file->loaded_tus
12827 : dwp_file->loaded_cus,
12828 &find_dwo_cu, INSERT);
80626a55
DE
12829
12830 if (*slot != NULL)
9a3c8263 12831 return (struct dwo_unit *) *slot;
80626a55
DE
12832
12833 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12834 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12835 {
12836 ULONGEST signature_in_table;
12837
12838 signature_in_table =
57d63ce2 12839 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12840 if (signature_in_table == signature)
12841 {
57d63ce2
DE
12842 uint32_t unit_index =
12843 read_4_bytes (dbfd,
12844 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12845
73869dc2
DE
12846 if (dwp_file->version == 1)
12847 {
ed2dc618
SM
12848 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12849 dwp_file, unit_index,
73869dc2
DE
12850 comp_dir, signature,
12851 is_debug_types);
12852 }
12853 else
12854 {
ed2dc618
SM
12855 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12856 dwp_file, unit_index,
73869dc2
DE
12857 comp_dir, signature,
12858 is_debug_types);
12859 }
9a3c8263 12860 return (struct dwo_unit *) *slot;
80626a55
DE
12861 }
12862 if (signature_in_table == 0)
12863 return NULL;
12864 hash = (hash + hash2) & mask;
12865 }
12866
12867 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12868 " [in module %s]"),
12869 dwp_file->name);
12870}
12871
ab5088bf 12872/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12873 Open the file specified by FILE_NAME and hand it off to BFD for
12874 preliminary analysis. Return a newly initialized bfd *, which
12875 includes a canonicalized copy of FILE_NAME.
80626a55 12876 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12877 SEARCH_CWD is true if the current directory is to be searched.
12878 It will be searched before debug-file-directory.
13aaf454
DE
12879 If successful, the file is added to the bfd include table of the
12880 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12881 If unable to find/open the file, return NULL.
3019eac3
DE
12882 NOTE: This function is derived from symfile_bfd_open. */
12883
192b62ce 12884static gdb_bfd_ref_ptr
ed2dc618
SM
12885try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12886 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12887{
24b9144d 12888 int desc;
9c02c129
DE
12889 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12890 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12891 to debug_file_directory. */
e0cc99a6 12892 const char *search_path;
9c02c129
DE
12893 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12894
e0cc99a6 12895 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12896 if (search_cwd)
12897 {
12898 if (*debug_file_directory != '\0')
e0cc99a6
TT
12899 {
12900 search_path_holder.reset (concat (".", dirname_separator_string,
12901 debug_file_directory,
12902 (char *) NULL));
12903 search_path = search_path_holder.get ();
12904 }
6ac97d4c 12905 else
e0cc99a6 12906 search_path = ".";
6ac97d4c 12907 }
9c02c129 12908 else
e0cc99a6 12909 search_path = debug_file_directory;
3019eac3 12910
24b9144d 12911 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12912 if (is_dwp)
12913 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12914
12915 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12916 desc = openp (search_path, flags, file_name,
3019eac3
DE
12917 O_RDONLY | O_BINARY, &absolute_name);
12918 if (desc < 0)
12919 return NULL;
12920
e0cc99a6
TT
12921 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12922 gnutarget, desc));
9c02c129
DE
12923 if (sym_bfd == NULL)
12924 return NULL;
192b62ce 12925 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12926
192b62ce
TT
12927 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12928 return NULL;
3019eac3 12929
13aaf454
DE
12930 /* Success. Record the bfd as having been included by the objfile's bfd.
12931 This is important because things like demangled_names_hash lives in the
12932 objfile's per_bfd space and may have references to things like symbol
12933 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12934 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12935
3019eac3
DE
12936 return sym_bfd;
12937}
12938
ab5088bf 12939/* Try to open DWO file FILE_NAME.
3019eac3
DE
12940 COMP_DIR is the DW_AT_comp_dir attribute.
12941 The result is the bfd handle of the file.
12942 If there is a problem finding or opening the file, return NULL.
12943 Upon success, the canonicalized path of the file is stored in the bfd,
12944 same as symfile_bfd_open. */
12945
192b62ce 12946static gdb_bfd_ref_ptr
ed2dc618
SM
12947open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12948 const char *file_name, const char *comp_dir)
3019eac3 12949{
80626a55 12950 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12951 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12952 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12953
12954 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12955
12956 if (comp_dir != NULL)
12957 {
b36cec19
PA
12958 char *path_to_try = concat (comp_dir, SLASH_STRING,
12959 file_name, (char *) NULL);
3019eac3
DE
12960
12961 /* NOTE: If comp_dir is a relative path, this will also try the
12962 search path, which seems useful. */
ed2dc618
SM
12963 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12964 path_to_try,
12965 0 /*is_dwp*/,
192b62ce 12966 1 /*search_cwd*/));
3019eac3
DE
12967 xfree (path_to_try);
12968 if (abfd != NULL)
12969 return abfd;
12970 }
12971
12972 /* That didn't work, try debug-file-directory, which, despite its name,
12973 is a list of paths. */
12974
12975 if (*debug_file_directory == '\0')
12976 return NULL;
12977
ed2dc618
SM
12978 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12979 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12980}
12981
80626a55
DE
12982/* This function is mapped across the sections and remembers the offset and
12983 size of each of the DWO debugging sections we are interested in. */
12984
12985static void
12986dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12987{
9a3c8263 12988 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12989 const struct dwop_section_names *names = &dwop_section_names;
12990
12991 if (section_is_p (sectp->name, &names->abbrev_dwo))
12992 {
049412e3 12993 dwo_sections->abbrev.s.section = sectp;
fd361982 12994 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12995 }
12996 else if (section_is_p (sectp->name, &names->info_dwo))
12997 {
049412e3 12998 dwo_sections->info.s.section = sectp;
fd361982 12999 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
13000 }
13001 else if (section_is_p (sectp->name, &names->line_dwo))
13002 {
049412e3 13003 dwo_sections->line.s.section = sectp;
fd361982 13004 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
13005 }
13006 else if (section_is_p (sectp->name, &names->loc_dwo))
13007 {
049412e3 13008 dwo_sections->loc.s.section = sectp;
fd361982 13009 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
13010 }
13011 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13012 {
049412e3 13013 dwo_sections->macinfo.s.section = sectp;
fd361982 13014 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
13015 }
13016 else if (section_is_p (sectp->name, &names->macro_dwo))
13017 {
049412e3 13018 dwo_sections->macro.s.section = sectp;
fd361982 13019 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
13020 }
13021 else if (section_is_p (sectp->name, &names->str_dwo))
13022 {
049412e3 13023 dwo_sections->str.s.section = sectp;
fd361982 13024 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
13025 }
13026 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13027 {
049412e3 13028 dwo_sections->str_offsets.s.section = sectp;
fd361982 13029 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
13030 }
13031 else if (section_is_p (sectp->name, &names->types_dwo))
13032 {
13033 struct dwarf2_section_info type_section;
13034
13035 memset (&type_section, 0, sizeof (type_section));
049412e3 13036 type_section.s.section = sectp;
fd361982 13037 type_section.size = bfd_section_size (sectp);
fd5866f6 13038 dwo_sections->types.push_back (type_section);
80626a55
DE
13039 }
13040}
13041
ab5088bf 13042/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13043 by PER_CU. This is for the non-DWP case.
80626a55 13044 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13045
13046static struct dwo_file *
0ac5b59e
DE
13047open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13048 const char *dwo_name, const char *comp_dir)
3019eac3 13049{
ed2dc618 13050 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13051
fb1eb2f9 13052 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13053 if (dbfd == NULL)
13054 {
b4f54984 13055 if (dwarf_read_debug)
80626a55
DE
13056 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13057 return NULL;
13058 }
263db9a1 13059
51ac9db5 13060 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13061 dwo_file->dwo_name = dwo_name;
13062 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13063 dwo_file->dbfd = std::move (dbfd);
3019eac3 13064
fb1eb2f9 13065 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13066 &dwo_file->sections);
3019eac3 13067
ed2dc618
SM
13068 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13069 dwo_file->cus);
3019eac3 13070
263db9a1 13071 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13072 dwo_file->sections.types, dwo_file->tus);
3019eac3 13073
b4f54984 13074 if (dwarf_read_debug)
80626a55
DE
13075 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13076
263db9a1 13077 return dwo_file.release ();
3019eac3
DE
13078}
13079
80626a55 13080/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13081 size of each of the DWP debugging sections common to version 1 and 2 that
13082 we are interested in. */
3019eac3 13083
80626a55 13084static void
73869dc2
DE
13085dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13086 void *dwp_file_ptr)
3019eac3 13087{
9a3c8263 13088 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13089 const struct dwop_section_names *names = &dwop_section_names;
13090 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13091
80626a55 13092 /* Record the ELF section number for later lookup: this is what the
73869dc2 13093 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13094 gdb_assert (elf_section_nr < dwp_file->num_sections);
13095 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13096
80626a55
DE
13097 /* Look for specific sections that we need. */
13098 if (section_is_p (sectp->name, &names->str_dwo))
13099 {
049412e3 13100 dwp_file->sections.str.s.section = sectp;
fd361982 13101 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13102 }
13103 else if (section_is_p (sectp->name, &names->cu_index))
13104 {
049412e3 13105 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13106 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13107 }
13108 else if (section_is_p (sectp->name, &names->tu_index))
13109 {
049412e3 13110 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13111 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13112 }
13113}
3019eac3 13114
73869dc2
DE
13115/* This function is mapped across the sections and remembers the offset and
13116 size of each of the DWP version 2 debugging sections that we are interested
13117 in. This is split into a separate function because we don't know if we
13118 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13119
13120static void
13121dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13122{
9a3c8263 13123 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13124 const struct dwop_section_names *names = &dwop_section_names;
13125 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13126
13127 /* Record the ELF section number for later lookup: this is what the
13128 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13129 gdb_assert (elf_section_nr < dwp_file->num_sections);
13130 dwp_file->elf_sections[elf_section_nr] = sectp;
13131
13132 /* Look for specific sections that we need. */
13133 if (section_is_p (sectp->name, &names->abbrev_dwo))
13134 {
049412e3 13135 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13136 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13137 }
13138 else if (section_is_p (sectp->name, &names->info_dwo))
13139 {
049412e3 13140 dwp_file->sections.info.s.section = sectp;
fd361982 13141 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13142 }
13143 else if (section_is_p (sectp->name, &names->line_dwo))
13144 {
049412e3 13145 dwp_file->sections.line.s.section = sectp;
fd361982 13146 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13147 }
13148 else if (section_is_p (sectp->name, &names->loc_dwo))
13149 {
049412e3 13150 dwp_file->sections.loc.s.section = sectp;
fd361982 13151 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13152 }
13153 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13154 {
049412e3 13155 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13156 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13157 }
13158 else if (section_is_p (sectp->name, &names->macro_dwo))
13159 {
049412e3 13160 dwp_file->sections.macro.s.section = sectp;
fd361982 13161 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13162 }
13163 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13164 {
049412e3 13165 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13166 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13167 }
13168 else if (section_is_p (sectp->name, &names->types_dwo))
13169 {
049412e3 13170 dwp_file->sections.types.s.section = sectp;
fd361982 13171 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13172 }
13173}
13174
80626a55 13175/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13176
80626a55
DE
13177static hashval_t
13178hash_dwp_loaded_cutus (const void *item)
13179{
9a3c8263 13180 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13181
80626a55
DE
13182 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13183 return dwo_unit->signature;
3019eac3
DE
13184}
13185
80626a55 13186/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13187
80626a55
DE
13188static int
13189eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13190{
9a3c8263
SM
13191 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13192 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13193
80626a55
DE
13194 return dua->signature == dub->signature;
13195}
3019eac3 13196
80626a55 13197/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13198
80626a55
DE
13199static htab_t
13200allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13201{
13202 return htab_create_alloc_ex (3,
13203 hash_dwp_loaded_cutus,
13204 eq_dwp_loaded_cutus,
13205 NULL,
13206 &objfile->objfile_obstack,
13207 hashtab_obstack_allocate,
13208 dummy_obstack_deallocate);
13209}
3019eac3 13210
ab5088bf
DE
13211/* Try to open DWP file FILE_NAME.
13212 The result is the bfd handle of the file.
13213 If there is a problem finding or opening the file, return NULL.
13214 Upon success, the canonicalized path of the file is stored in the bfd,
13215 same as symfile_bfd_open. */
13216
192b62ce 13217static gdb_bfd_ref_ptr
ed2dc618
SM
13218open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13219 const char *file_name)
ab5088bf 13220{
ed2dc618
SM
13221 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13222 1 /*is_dwp*/,
192b62ce 13223 1 /*search_cwd*/));
6ac97d4c
DE
13224 if (abfd != NULL)
13225 return abfd;
13226
13227 /* Work around upstream bug 15652.
13228 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13229 [Whether that's a "bug" is debatable, but it is getting in our way.]
13230 We have no real idea where the dwp file is, because gdb's realpath-ing
13231 of the executable's path may have discarded the needed info.
13232 [IWBN if the dwp file name was recorded in the executable, akin to
13233 .gnu_debuglink, but that doesn't exist yet.]
13234 Strip the directory from FILE_NAME and search again. */
13235 if (*debug_file_directory != '\0')
13236 {
13237 /* Don't implicitly search the current directory here.
13238 If the user wants to search "." to handle this case,
13239 it must be added to debug-file-directory. */
ed2dc618
SM
13240 return try_open_dwop_file (dwarf2_per_objfile,
13241 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13242 0 /*search_cwd*/);
13243 }
13244
13245 return NULL;
ab5088bf
DE
13246}
13247
80626a55
DE
13248/* Initialize the use of the DWP file for the current objfile.
13249 By convention the name of the DWP file is ${objfile}.dwp.
13250 The result is NULL if it can't be found. */
a766d390 13251
400174b1 13252static std::unique_ptr<struct dwp_file>
ed2dc618 13253open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13254{
13255 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13256
82bf32bc
JK
13257 /* Try to find first .dwp for the binary file before any symbolic links
13258 resolving. */
6c447423
DE
13259
13260 /* If the objfile is a debug file, find the name of the real binary
13261 file and get the name of dwp file from there. */
d721ba37 13262 std::string dwp_name;
6c447423
DE
13263 if (objfile->separate_debug_objfile_backlink != NULL)
13264 {
13265 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13266 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13267
d721ba37 13268 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13269 }
13270 else
d721ba37
PA
13271 dwp_name = objfile->original_name;
13272
13273 dwp_name += ".dwp";
80626a55 13274
ed2dc618 13275 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13276 if (dbfd == NULL
13277 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13278 {
13279 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13280 dwp_name = objfile_name (objfile);
13281 dwp_name += ".dwp";
ed2dc618 13282 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13283 }
13284
80626a55
DE
13285 if (dbfd == NULL)
13286 {
b4f54984 13287 if (dwarf_read_debug)
d721ba37 13288 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13289 return std::unique_ptr<dwp_file> ();
3019eac3 13290 }
400174b1
TT
13291
13292 const char *name = bfd_get_filename (dbfd.get ());
13293 std::unique_ptr<struct dwp_file> dwp_file
13294 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13295
0a0f4c01 13296 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13297 dwp_file->elf_sections =
13298 OBSTACK_CALLOC (&objfile->objfile_obstack,
13299 dwp_file->num_sections, asection *);
13300
400174b1
TT
13301 bfd_map_over_sections (dwp_file->dbfd.get (),
13302 dwarf2_locate_common_dwp_sections,
13303 dwp_file.get ());
80626a55 13304
400174b1
TT
13305 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13306 0);
80626a55 13307
400174b1
TT
13308 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13309 1);
80626a55 13310
73869dc2 13311 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13312 if (dwp_file->cus && dwp_file->tus
13313 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13314 {
13315 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13316 pretty bizarre. We use pulongest here because that's the established
4d65956b 13317 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13318 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13319 " TU version %s [in DWP file %s]"),
13320 pulongest (dwp_file->cus->version),
d721ba37 13321 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13322 }
08302ed2
DE
13323
13324 if (dwp_file->cus)
13325 dwp_file->version = dwp_file->cus->version;
13326 else if (dwp_file->tus)
13327 dwp_file->version = dwp_file->tus->version;
13328 else
13329 dwp_file->version = 2;
73869dc2
DE
13330
13331 if (dwp_file->version == 2)
400174b1
TT
13332 bfd_map_over_sections (dwp_file->dbfd.get (),
13333 dwarf2_locate_v2_dwp_sections,
13334 dwp_file.get ());
73869dc2 13335
19ac8c2e
DE
13336 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13337 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13338
b4f54984 13339 if (dwarf_read_debug)
80626a55
DE
13340 {
13341 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13342 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13343 " %s CUs, %s TUs\n",
13344 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13345 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13346 }
13347
13348 return dwp_file;
3019eac3 13349}
c906108c 13350
ab5088bf
DE
13351/* Wrapper around open_and_init_dwp_file, only open it once. */
13352
13353static struct dwp_file *
ed2dc618 13354get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13355{
13356 if (! dwarf2_per_objfile->dwp_checked)
13357 {
ed2dc618
SM
13358 dwarf2_per_objfile->dwp_file
13359 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13360 dwarf2_per_objfile->dwp_checked = 1;
13361 }
400174b1 13362 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13363}
13364
80626a55
DE
13365/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13366 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13367 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13368 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13369 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13370
13371 This is called, for example, when wanting to read a variable with a
13372 complex location. Therefore we don't want to do file i/o for every call.
13373 Therefore we don't want to look for a DWO file on every call.
13374 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13375 then we check if we've already seen DWO_NAME, and only THEN do we check
13376 for a DWO file.
13377
1c658ad5 13378 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13379 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13380
3019eac3 13381static struct dwo_unit *
80626a55
DE
13382lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13383 const char *dwo_name, const char *comp_dir,
13384 ULONGEST signature, int is_debug_types)
3019eac3 13385{
ed2dc618 13386 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13387 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13388 const char *kind = is_debug_types ? "TU" : "CU";
13389 void **dwo_file_slot;
3019eac3 13390 struct dwo_file *dwo_file;
80626a55 13391 struct dwp_file *dwp_file;
cb1df416 13392
6a506a2d
DE
13393 /* First see if there's a DWP file.
13394 If we have a DWP file but didn't find the DWO inside it, don't
13395 look for the original DWO file. It makes gdb behave differently
13396 depending on whether one is debugging in the build tree. */
cf2c3c16 13397
ed2dc618 13398 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13399 if (dwp_file != NULL)
cf2c3c16 13400 {
80626a55
DE
13401 const struct dwp_hash_table *dwp_htab =
13402 is_debug_types ? dwp_file->tus : dwp_file->cus;
13403
13404 if (dwp_htab != NULL)
13405 {
13406 struct dwo_unit *dwo_cutu =
ed2dc618 13407 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13408 signature, is_debug_types);
80626a55
DE
13409
13410 if (dwo_cutu != NULL)
13411 {
b4f54984 13412 if (dwarf_read_debug)
80626a55
DE
13413 {
13414 fprintf_unfiltered (gdb_stdlog,
13415 "Virtual DWO %s %s found: @%s\n",
13416 kind, hex_string (signature),
13417 host_address_to_string (dwo_cutu));
13418 }
13419 return dwo_cutu;
13420 }
13421 }
13422 }
6a506a2d 13423 else
80626a55 13424 {
6a506a2d 13425 /* No DWP file, look for the DWO file. */
80626a55 13426
ed2dc618
SM
13427 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13428 dwo_name, comp_dir);
6a506a2d 13429 if (*dwo_file_slot == NULL)
80626a55 13430 {
6a506a2d
DE
13431 /* Read in the file and build a table of the CUs/TUs it contains. */
13432 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13433 }
6a506a2d 13434 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13435 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13436
6a506a2d 13437 if (dwo_file != NULL)
19c3d4c9 13438 {
6a506a2d
DE
13439 struct dwo_unit *dwo_cutu = NULL;
13440
13441 if (is_debug_types && dwo_file->tus)
13442 {
13443 struct dwo_unit find_dwo_cutu;
13444
13445 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13446 find_dwo_cutu.signature = signature;
9a3c8263
SM
13447 dwo_cutu
13448 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13449 }
33c5cd75 13450 else if (!is_debug_types && dwo_file->cus)
80626a55 13451 {
33c5cd75
DB
13452 struct dwo_unit find_dwo_cutu;
13453
13454 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13455 find_dwo_cutu.signature = signature;
13456 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13457 &find_dwo_cutu);
6a506a2d
DE
13458 }
13459
13460 if (dwo_cutu != NULL)
13461 {
b4f54984 13462 if (dwarf_read_debug)
6a506a2d
DE
13463 {
13464 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13465 kind, dwo_name, hex_string (signature),
13466 host_address_to_string (dwo_cutu));
13467 }
13468 return dwo_cutu;
80626a55
DE
13469 }
13470 }
2e276125 13471 }
9cdd5dbd 13472
80626a55
DE
13473 /* We didn't find it. This could mean a dwo_id mismatch, or
13474 someone deleted the DWO/DWP file, or the search path isn't set up
13475 correctly to find the file. */
13476
b4f54984 13477 if (dwarf_read_debug)
80626a55
DE
13478 {
13479 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13480 kind, dwo_name, hex_string (signature));
13481 }
3019eac3 13482
6656a72d
DE
13483 /* This is a warning and not a complaint because it can be caused by
13484 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13485 {
13486 /* Print the name of the DWP file if we looked there, helps the user
13487 better diagnose the problem. */
791afaa2 13488 std::string dwp_text;
43942612
DE
13489
13490 if (dwp_file != NULL)
791afaa2
TT
13491 dwp_text = string_printf (" [in DWP file %s]",
13492 lbasename (dwp_file->name));
43942612 13493
9d8780f0 13494 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13495 " [in module %s]"),
13496 kind, dwo_name, hex_string (signature),
791afaa2 13497 dwp_text.c_str (),
43942612 13498 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13499 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13500 }
3019eac3 13501 return NULL;
5fb290d7
DJ
13502}
13503
80626a55
DE
13504/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13505 See lookup_dwo_cutu_unit for details. */
13506
13507static struct dwo_unit *
13508lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13509 const char *dwo_name, const char *comp_dir,
13510 ULONGEST signature)
13511{
13512 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13513}
13514
13515/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13516 See lookup_dwo_cutu_unit for details. */
13517
13518static struct dwo_unit *
13519lookup_dwo_type_unit (struct signatured_type *this_tu,
13520 const char *dwo_name, const char *comp_dir)
13521{
13522 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13523}
13524
89e63ee4
DE
13525/* Traversal function for queue_and_load_all_dwo_tus. */
13526
13527static int
13528queue_and_load_dwo_tu (void **slot, void *info)
13529{
13530 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13531 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13532 ULONGEST signature = dwo_unit->signature;
13533 struct signatured_type *sig_type =
13534 lookup_dwo_signatured_type (per_cu->cu, signature);
13535
13536 if (sig_type != NULL)
13537 {
13538 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13539
13540 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13541 a real dependency of PER_CU on SIG_TYPE. That is detected later
13542 while processing PER_CU. */
13543 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13544 load_full_type_unit (sig_cu);
ae640021 13545 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13546 }
13547
13548 return 1;
13549}
13550
13551/* Queue all TUs contained in the DWO of PER_CU to be read in.
13552 The DWO may have the only definition of the type, though it may not be
13553 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13554 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13555
13556static void
13557queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13558{
13559 struct dwo_unit *dwo_unit;
13560 struct dwo_file *dwo_file;
13561
13562 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13563 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13564 gdb_assert (per_cu->cu != NULL);
13565
13566 dwo_unit = per_cu->cu->dwo_unit;
13567 gdb_assert (dwo_unit != NULL);
13568
13569 dwo_file = dwo_unit->dwo_file;
13570 if (dwo_file->tus != NULL)
13571 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13572}
13573
3019eac3 13574/* Read in various DIEs. */
348e048f 13575
d389af10 13576/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13577 Inherit only the children of the DW_AT_abstract_origin DIE not being
13578 already referenced by DW_AT_abstract_origin from the children of the
13579 current DIE. */
d389af10
JK
13580
13581static void
13582inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13583{
13584 struct die_info *child_die;
791afaa2 13585 sect_offset *offsetp;
d389af10
JK
13586 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13587 struct die_info *origin_die;
13588 /* Iterator of the ORIGIN_DIE children. */
13589 struct die_info *origin_child_die;
d389af10 13590 struct attribute *attr;
cd02d79d
PA
13591 struct dwarf2_cu *origin_cu;
13592 struct pending **origin_previous_list_in_scope;
d389af10
JK
13593
13594 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13595 if (!attr)
13596 return;
13597
cd02d79d
PA
13598 /* Note that following die references may follow to a die in a
13599 different cu. */
13600
13601 origin_cu = cu;
13602 origin_die = follow_die_ref (die, attr, &origin_cu);
13603
13604 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13605 symbols in. */
13606 origin_previous_list_in_scope = origin_cu->list_in_scope;
13607 origin_cu->list_in_scope = cu->list_in_scope;
13608
edb3359d
DJ
13609 if (die->tag != origin_die->tag
13610 && !(die->tag == DW_TAG_inlined_subroutine
13611 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13612 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13613 sect_offset_str (die->sect_off),
13614 sect_offset_str (origin_die->sect_off));
d389af10 13615
791afaa2 13616 std::vector<sect_offset> offsets;
d389af10 13617
3ea89b92
PMR
13618 for (child_die = die->child;
13619 child_die && child_die->tag;
13620 child_die = sibling_die (child_die))
13621 {
13622 struct die_info *child_origin_die;
13623 struct dwarf2_cu *child_origin_cu;
13624
13625 /* We are trying to process concrete instance entries:
216f72a1 13626 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13627 it's not relevant to our analysis here. i.e. detecting DIEs that are
13628 present in the abstract instance but not referenced in the concrete
13629 one. */
216f72a1
JK
13630 if (child_die->tag == DW_TAG_call_site
13631 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13632 continue;
13633
c38f313d
DJ
13634 /* For each CHILD_DIE, find the corresponding child of
13635 ORIGIN_DIE. If there is more than one layer of
13636 DW_AT_abstract_origin, follow them all; there shouldn't be,
13637 but GCC versions at least through 4.4 generate this (GCC PR
13638 40573). */
3ea89b92
PMR
13639 child_origin_die = child_die;
13640 child_origin_cu = cu;
c38f313d
DJ
13641 while (1)
13642 {
cd02d79d
PA
13643 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13644 child_origin_cu);
c38f313d
DJ
13645 if (attr == NULL)
13646 break;
cd02d79d
PA
13647 child_origin_die = follow_die_ref (child_origin_die, attr,
13648 &child_origin_cu);
c38f313d
DJ
13649 }
13650
d389af10
JK
13651 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13652 counterpart may exist. */
c38f313d 13653 if (child_origin_die != child_die)
d389af10 13654 {
edb3359d
DJ
13655 if (child_die->tag != child_origin_die->tag
13656 && !(child_die->tag == DW_TAG_inlined_subroutine
13657 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13658 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13659 "different tags"),
9d8780f0
SM
13660 sect_offset_str (child_die->sect_off),
13661 sect_offset_str (child_origin_die->sect_off));
c38f313d 13662 if (child_origin_die->parent != origin_die)
b98664d3 13663 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13664 "different parents"),
9d8780f0
SM
13665 sect_offset_str (child_die->sect_off),
13666 sect_offset_str (child_origin_die->sect_off));
c38f313d 13667 else
791afaa2 13668 offsets.push_back (child_origin_die->sect_off);
d389af10 13669 }
d389af10 13670 }
791afaa2
TT
13671 std::sort (offsets.begin (), offsets.end ());
13672 sect_offset *offsets_end = offsets.data () + offsets.size ();
13673 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13674 if (offsetp[-1] == *offsetp)
b98664d3 13675 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13676 "to DIE %s as their abstract origin"),
13677 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13678
791afaa2 13679 offsetp = offsets.data ();
d389af10
JK
13680 origin_child_die = origin_die->child;
13681 while (origin_child_die && origin_child_die->tag)
13682 {
13683 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13684 while (offsetp < offsets_end
9c541725 13685 && *offsetp < origin_child_die->sect_off)
d389af10 13686 offsetp++;
b64f50a1 13687 if (offsetp >= offsets_end
9c541725 13688 || *offsetp > origin_child_die->sect_off)
d389af10 13689 {
adde2bff
DE
13690 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13691 Check whether we're already processing ORIGIN_CHILD_DIE.
13692 This can happen with mutually referenced abstract_origins.
13693 PR 16581. */
13694 if (!origin_child_die->in_process)
13695 process_die (origin_child_die, origin_cu);
d389af10
JK
13696 }
13697 origin_child_die = sibling_die (origin_child_die);
13698 }
cd02d79d 13699 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13700
13701 if (cu != origin_cu)
13702 compute_delayed_physnames (origin_cu);
d389af10
JK
13703}
13704
c906108c 13705static void
e7c27a73 13706read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13707{
518817b3 13708 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13709 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13710 struct context_stack *newobj;
c906108c
SS
13711 CORE_ADDR lowpc;
13712 CORE_ADDR highpc;
13713 struct die_info *child_die;
edb3359d 13714 struct attribute *attr, *call_line, *call_file;
15d034d0 13715 const char *name;
e142c38c 13716 CORE_ADDR baseaddr;
801e3a5b 13717 struct block *block;
edb3359d 13718 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13719 std::vector<struct symbol *> template_args;
34eaf542 13720 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13721
13722 if (inlined_func)
13723 {
13724 /* If we do not have call site information, we can't show the
13725 caller of this inlined function. That's too confusing, so
13726 only use the scope for local variables. */
13727 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13728 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13729 if (call_line == NULL || call_file == NULL)
13730 {
13731 read_lexical_block_scope (die, cu);
13732 return;
13733 }
13734 }
c906108c 13735
e142c38c
DJ
13736 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13737
94af9270 13738 name = dwarf2_name (die, cu);
c906108c 13739
e8d05480
JB
13740 /* Ignore functions with missing or empty names. These are actually
13741 illegal according to the DWARF standard. */
13742 if (name == NULL)
13743 {
b98664d3 13744 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13745 sect_offset_str (die->sect_off));
e8d05480
JB
13746 return;
13747 }
13748
13749 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13750 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13751 <= PC_BOUNDS_INVALID)
e8d05480 13752 {
ae4d0c03
PM
13753 attr = dwarf2_attr (die, DW_AT_external, cu);
13754 if (!attr || !DW_UNSND (attr))
b98664d3 13755 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13756 "for subprogram DIE at %s"),
13757 sect_offset_str (die->sect_off));
e8d05480
JB
13758 return;
13759 }
c906108c 13760
3e29f34a
MR
13761 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13762 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13763
34eaf542
TT
13764 /* If we have any template arguments, then we must allocate a
13765 different sort of symbol. */
13766 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13767 {
13768 if (child_die->tag == DW_TAG_template_type_param
13769 || child_die->tag == DW_TAG_template_value_param)
13770 {
e623cf5d 13771 templ_func = allocate_template_symbol (objfile);
cf724bc9 13772 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13773 break;
13774 }
13775 }
13776
c24bdb02 13777 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13778 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13779 (struct symbol *) templ_func);
4c2df51b 13780
81873cc8 13781 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13782 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13783 cu->language);
13784
4cecd739
DJ
13785 /* If there is a location expression for DW_AT_frame_base, record
13786 it. */
e142c38c 13787 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13788 if (attr != nullptr)
fe978cb0 13789 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13790
63e43d3a
PMR
13791 /* If there is a location for the static link, record it. */
13792 newobj->static_link = NULL;
13793 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13794 if (attr != nullptr)
63e43d3a 13795 {
224c3ddb
SM
13796 newobj->static_link
13797 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13798 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13799 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13800 }
13801
c24bdb02 13802 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13803
639d11d3 13804 if (die->child != NULL)
c906108c 13805 {
639d11d3 13806 child_die = die->child;
c906108c
SS
13807 while (child_die && child_die->tag)
13808 {
34eaf542
TT
13809 if (child_die->tag == DW_TAG_template_type_param
13810 || child_die->tag == DW_TAG_template_value_param)
13811 {
13812 struct symbol *arg = new_symbol (child_die, NULL, cu);
13813
f1078f66 13814 if (arg != NULL)
2f4732b0 13815 template_args.push_back (arg);
34eaf542
TT
13816 }
13817 else
13818 process_die (child_die, cu);
c906108c
SS
13819 child_die = sibling_die (child_die);
13820 }
13821 }
13822
d389af10
JK
13823 inherit_abstract_dies (die, cu);
13824
4a811a97
UW
13825 /* If we have a DW_AT_specification, we might need to import using
13826 directives from the context of the specification DIE. See the
13827 comment in determine_prefix. */
13828 if (cu->language == language_cplus
13829 && dwarf2_attr (die, DW_AT_specification, cu))
13830 {
13831 struct dwarf2_cu *spec_cu = cu;
13832 struct die_info *spec_die = die_specification (die, &spec_cu);
13833
13834 while (spec_die)
13835 {
13836 child_die = spec_die->child;
13837 while (child_die && child_die->tag)
13838 {
13839 if (child_die->tag == DW_TAG_imported_module)
13840 process_die (child_die, spec_cu);
13841 child_die = sibling_die (child_die);
13842 }
13843
13844 /* In some cases, GCC generates specification DIEs that
13845 themselves contain DW_AT_specification attributes. */
13846 spec_die = die_specification (spec_die, &spec_cu);
13847 }
13848 }
13849
c24bdb02 13850 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13851 /* Make a block for the local symbols within. */
c24bdb02 13852 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13853 cstk.static_link, lowpc, highpc);
801e3a5b 13854
df8a16a1 13855 /* For C++, set the block's scope. */
45280282
IB
13856 if ((cu->language == language_cplus
13857 || cu->language == language_fortran
c44af4eb
TT
13858 || cu->language == language_d
13859 || cu->language == language_rust)
4d4ec4e5 13860 && cu->processing_has_namespace_info)
195a3f6c
TT
13861 block_set_scope (block, determine_prefix (die, cu),
13862 &objfile->objfile_obstack);
df8a16a1 13863
801e3a5b
JB
13864 /* If we have address ranges, record them. */
13865 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13866
a60f3166 13867 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13868
34eaf542 13869 /* Attach template arguments to function. */
2f4732b0 13870 if (!template_args.empty ())
34eaf542
TT
13871 {
13872 gdb_assert (templ_func != NULL);
13873
2f4732b0 13874 templ_func->n_template_arguments = template_args.size ();
34eaf542 13875 templ_func->template_arguments
8d749320
SM
13876 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13877 templ_func->n_template_arguments);
34eaf542 13878 memcpy (templ_func->template_arguments,
2f4732b0 13879 template_args.data (),
34eaf542 13880 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13881
13882 /* Make sure that the symtab is set on the new symbols. Even
13883 though they don't appear in this symtab directly, other parts
13884 of gdb assume that symbols do, and this is reasonably
13885 true. */
8634679f 13886 for (symbol *sym : template_args)
3e1d3d8c 13887 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13888 }
13889
208d8187
JB
13890 /* In C++, we can have functions nested inside functions (e.g., when
13891 a function declares a class that has methods). This means that
13892 when we finish processing a function scope, we may need to go
13893 back to building a containing block's symbol lists. */
c24bdb02
KS
13894 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13895 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13896
921e78cf
JB
13897 /* If we've finished processing a top-level function, subsequent
13898 symbols go in the file symbol list. */
c24bdb02
KS
13899 if (cu->get_builder ()->outermost_context_p ())
13900 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13901}
13902
13903/* Process all the DIES contained within a lexical block scope. Start
13904 a new scope, process the dies, and then close the scope. */
13905
13906static void
e7c27a73 13907read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13908{
518817b3 13909 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13910 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13911 CORE_ADDR lowpc, highpc;
13912 struct die_info *child_die;
e142c38c
DJ
13913 CORE_ADDR baseaddr;
13914
13915 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13916
13917 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13918 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13919 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13920 be nasty. Might be easier to properly extend generic blocks to
af34e669 13921 describe ranges. */
e385593e
JK
13922 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13923 {
13924 case PC_BOUNDS_NOT_PRESENT:
13925 /* DW_TAG_lexical_block has no attributes, process its children as if
13926 there was no wrapping by that DW_TAG_lexical_block.
13927 GCC does no longer produces such DWARF since GCC r224161. */
13928 for (child_die = die->child;
13929 child_die != NULL && child_die->tag;
13930 child_die = sibling_die (child_die))
13931 process_die (child_die, cu);
13932 return;
13933 case PC_BOUNDS_INVALID:
13934 return;
13935 }
3e29f34a
MR
13936 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13937 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13938
c24bdb02 13939 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13940 if (die->child != NULL)
c906108c 13941 {
639d11d3 13942 child_die = die->child;
c906108c
SS
13943 while (child_die && child_die->tag)
13944 {
e7c27a73 13945 process_die (child_die, cu);
c906108c
SS
13946 child_die = sibling_die (child_die);
13947 }
13948 }
3ea89b92 13949 inherit_abstract_dies (die, cu);
c24bdb02 13950 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13951
c24bdb02
KS
13952 if (*cu->get_builder ()->get_local_symbols () != NULL
13953 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13954 {
801e3a5b 13955 struct block *block
c24bdb02 13956 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13957 cstk.start_addr, highpc);
801e3a5b
JB
13958
13959 /* Note that recording ranges after traversing children, as we
13960 do here, means that recording a parent's ranges entails
13961 walking across all its children's ranges as they appear in
13962 the address map, which is quadratic behavior.
13963
13964 It would be nicer to record the parent's ranges before
13965 traversing its children, simply overriding whatever you find
13966 there. But since we don't even decide whether to create a
13967 block until after we've traversed its children, that's hard
13968 to do. */
13969 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13970 }
c24bdb02
KS
13971 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13972 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13973}
13974
216f72a1 13975/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13976
13977static void
13978read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13979{
518817b3 13980 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13981 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13982 CORE_ADDR pc, baseaddr;
13983 struct attribute *attr;
13984 struct call_site *call_site, call_site_local;
13985 void **slot;
13986 int nparams;
13987 struct die_info *child_die;
13988
13989 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13990
216f72a1
JK
13991 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13992 if (attr == NULL)
13993 {
13994 /* This was a pre-DWARF-5 GNU extension alias
13995 for DW_AT_call_return_pc. */
13996 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13997 }
96408a79
SA
13998 if (!attr)
13999 {
b98664d3 14000 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
14001 "DIE %s [in module %s]"),
14002 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14003 return;
14004 }
31aa7e4e 14005 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 14006 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
14007
14008 if (cu->call_site_htab == NULL)
14009 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14010 NULL, &objfile->objfile_obstack,
14011 hashtab_obstack_allocate, NULL);
14012 call_site_local.pc = pc;
14013 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14014 if (*slot != NULL)
14015 {
b98664d3 14016 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
14017 "DIE %s [in module %s]"),
14018 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 14019 objfile_name (objfile));
96408a79
SA
14020 return;
14021 }
14022
14023 /* Count parameters at the caller. */
14024
14025 nparams = 0;
14026 for (child_die = die->child; child_die && child_die->tag;
14027 child_die = sibling_die (child_die))
14028 {
216f72a1
JK
14029 if (child_die->tag != DW_TAG_call_site_parameter
14030 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 14031 {
b98664d3 14032 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14033 "DW_TAG_call_site child DIE %s [in module %s]"),
14034 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14035 objfile_name (objfile));
96408a79
SA
14036 continue;
14037 }
14038
14039 nparams++;
14040 }
14041
224c3ddb
SM
14042 call_site
14043 = ((struct call_site *)
14044 obstack_alloc (&objfile->objfile_obstack,
14045 sizeof (*call_site)
14046 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14047 *slot = call_site;
14048 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14049 call_site->pc = pc;
14050
216f72a1
JK
14051 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14052 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14053 {
14054 struct die_info *func_die;
14055
14056 /* Skip also over DW_TAG_inlined_subroutine. */
14057 for (func_die = die->parent;
14058 func_die && func_die->tag != DW_TAG_subprogram
14059 && func_die->tag != DW_TAG_subroutine_type;
14060 func_die = func_die->parent);
14061
216f72a1
JK
14062 /* DW_AT_call_all_calls is a superset
14063 of DW_AT_call_all_tail_calls. */
96408a79 14064 if (func_die
216f72a1 14065 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14066 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14067 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14068 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14069 {
14070 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14071 not complete. But keep CALL_SITE for look ups via call_site_htab,
14072 both the initial caller containing the real return address PC and
14073 the final callee containing the current PC of a chain of tail
14074 calls do not need to have the tail call list complete. But any
14075 function candidate for a virtual tail call frame searched via
14076 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14077 determined unambiguously. */
14078 }
14079 else
14080 {
14081 struct type *func_type = NULL;
14082
14083 if (func_die)
14084 func_type = get_die_type (func_die, cu);
14085 if (func_type != NULL)
14086 {
14087 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14088
14089 /* Enlist this call site to the function. */
14090 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14091 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14092 }
14093 else
b98664d3 14094 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14095 "DIE %s [in module %s]"),
14096 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14097 }
14098 }
14099
216f72a1
JK
14100 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14101 if (attr == NULL)
14102 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14103 if (attr == NULL)
14104 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14105 if (attr == NULL)
216f72a1
JK
14106 {
14107 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14108 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14109 }
96408a79
SA
14110 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14111 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14112 /* Keep NULL DWARF_BLOCK. */;
14113 else if (attr_form_is_block (attr))
14114 {
14115 struct dwarf2_locexpr_baton *dlbaton;
14116
8d749320 14117 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14118 dlbaton->data = DW_BLOCK (attr)->data;
14119 dlbaton->size = DW_BLOCK (attr)->size;
14120 dlbaton->per_cu = cu->per_cu;
14121
14122 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14123 }
7771576e 14124 else if (attr_form_is_ref (attr))
96408a79 14125 {
96408a79
SA
14126 struct dwarf2_cu *target_cu = cu;
14127 struct die_info *target_die;
14128
ac9ec31b 14129 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14130 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14131 if (die_is_declaration (target_die, target_cu))
14132 {
7d45c7c3 14133 const char *target_physname;
9112db09
JK
14134
14135 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14136 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14137 if (target_physname == NULL)
9112db09 14138 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14139 if (target_physname == NULL)
b98664d3 14140 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14141 "physname, for referencing DIE %s [in module %s]"),
14142 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14143 else
7d455152 14144 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14145 }
14146 else
14147 {
14148 CORE_ADDR lowpc;
14149
14150 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14151 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14152 <= PC_BOUNDS_INVALID)
b98664d3 14153 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14154 "low pc, for referencing DIE %s [in module %s]"),
14155 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14156 else
3e29f34a
MR
14157 {
14158 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14159 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14160 }
96408a79
SA
14161 }
14162 }
14163 else
b98664d3 14164 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14165 "block nor reference, for DIE %s [in module %s]"),
14166 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14167
14168 call_site->per_cu = cu->per_cu;
14169
14170 for (child_die = die->child;
14171 child_die && child_die->tag;
14172 child_die = sibling_die (child_die))
14173 {
96408a79 14174 struct call_site_parameter *parameter;
1788b2d3 14175 struct attribute *loc, *origin;
96408a79 14176
216f72a1
JK
14177 if (child_die->tag != DW_TAG_call_site_parameter
14178 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14179 {
14180 /* Already printed the complaint above. */
14181 continue;
14182 }
14183
14184 gdb_assert (call_site->parameter_count < nparams);
14185 parameter = &call_site->parameter[call_site->parameter_count];
14186
1788b2d3
JK
14187 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14188 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14189 register is contained in DW_AT_call_value. */
96408a79 14190
24c5c679 14191 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14192 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14193 if (origin == NULL)
14194 {
14195 /* This was a pre-DWARF-5 GNU extension alias
14196 for DW_AT_call_parameter. */
14197 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14198 }
7771576e 14199 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14200 {
1788b2d3 14201 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14202
14203 sect_offset sect_off
14204 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14205 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14206 {
14207 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14208 binding can be done only inside one CU. Such referenced DIE
14209 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14210 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14211 "DW_TAG_call_site child DIE %s [in module %s]"),
14212 sect_offset_str (child_die->sect_off),
9c541725 14213 objfile_name (objfile));
d76b7dbc
JK
14214 continue;
14215 }
9c541725
PA
14216 parameter->u.param_cu_off
14217 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14218 }
14219 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14220 {
b98664d3 14221 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14222 "DW_TAG_call_site child DIE %s [in module %s]"),
14223 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14224 continue;
14225 }
24c5c679 14226 else
96408a79 14227 {
24c5c679
JK
14228 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14229 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14230 if (parameter->u.dwarf_reg != -1)
14231 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14232 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14233 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14234 &parameter->u.fb_offset))
14235 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14236 else
14237 {
b98664d3 14238 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14239 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14240 "DW_TAG_call_site child DIE %s "
24c5c679 14241 "[in module %s]"),
9d8780f0 14242 sect_offset_str (child_die->sect_off),
9c541725 14243 objfile_name (objfile));
24c5c679
JK
14244 continue;
14245 }
96408a79
SA
14246 }
14247
216f72a1
JK
14248 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14249 if (attr == NULL)
14250 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14251 if (!attr_form_is_block (attr))
14252 {
b98664d3 14253 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14254 "DW_TAG_call_site child DIE %s [in module %s]"),
14255 sect_offset_str (child_die->sect_off),
9c541725 14256 objfile_name (objfile));
96408a79
SA
14257 continue;
14258 }
14259 parameter->value = DW_BLOCK (attr)->data;
14260 parameter->value_size = DW_BLOCK (attr)->size;
14261
14262 /* Parameters are not pre-cleared by memset above. */
14263 parameter->data_value = NULL;
14264 parameter->data_value_size = 0;
14265 call_site->parameter_count++;
14266
216f72a1
JK
14267 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14268 if (attr == NULL)
14269 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14270 if (attr != nullptr)
96408a79
SA
14271 {
14272 if (!attr_form_is_block (attr))
b98664d3 14273 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14274 "DW_TAG_call_site child DIE %s [in module %s]"),
14275 sect_offset_str (child_die->sect_off),
9c541725 14276 objfile_name (objfile));
96408a79
SA
14277 else
14278 {
14279 parameter->data_value = DW_BLOCK (attr)->data;
14280 parameter->data_value_size = DW_BLOCK (attr)->size;
14281 }
14282 }
14283 }
14284}
14285
71a3c369
TT
14286/* Helper function for read_variable. If DIE represents a virtual
14287 table, then return the type of the concrete object that is
14288 associated with the virtual table. Otherwise, return NULL. */
14289
14290static struct type *
14291rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14292{
14293 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14294 if (attr == NULL)
14295 return NULL;
14296
14297 /* Find the type DIE. */
14298 struct die_info *type_die = NULL;
14299 struct dwarf2_cu *type_cu = cu;
14300
14301 if (attr_form_is_ref (attr))
14302 type_die = follow_die_ref (die, attr, &type_cu);
14303 if (type_die == NULL)
14304 return NULL;
14305
14306 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14307 return NULL;
14308 return die_containing_type (type_die, type_cu);
14309}
14310
14311/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14312
14313static void
14314read_variable (struct die_info *die, struct dwarf2_cu *cu)
14315{
14316 struct rust_vtable_symbol *storage = NULL;
14317
14318 if (cu->language == language_rust)
14319 {
14320 struct type *containing_type = rust_containing_type (die, cu);
14321
14322 if (containing_type != NULL)
14323 {
518817b3 14324 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 14325
468c0cbb 14326 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
14327 initialize_objfile_symbol (storage);
14328 storage->concrete_type = containing_type;
cf724bc9 14329 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14330 }
14331 }
14332
e4a62c65
TV
14333 struct symbol *res = new_symbol (die, NULL, cu, storage);
14334 struct attribute *abstract_origin
14335 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14336 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14337 if (res == NULL && loc && abstract_origin)
14338 {
14339 /* We have a variable without a name, but with a location and an abstract
14340 origin. This may be a concrete instance of an abstract variable
14341 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14342 later. */
14343 struct dwarf2_cu *origin_cu = cu;
14344 struct die_info *origin_die
14345 = follow_die_ref (die, abstract_origin, &origin_cu);
14346 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14347 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14348 }
71a3c369
TT
14349}
14350
43988095
JK
14351/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14352 reading .debug_rnglists.
14353 Callback's type should be:
14354 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14355 Return true if the attributes are present and valid, otherwise,
14356 return false. */
14357
14358template <typename Callback>
14359static bool
14360dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14361 Callback &&callback)
14362{
ed2dc618 14363 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14364 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14365 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14366 bfd *obfd = objfile->obfd;
43988095
JK
14367 /* Base address selection entry. */
14368 CORE_ADDR base;
14369 int found_base;
43988095 14370 const gdb_byte *buffer;
43988095
JK
14371 CORE_ADDR baseaddr;
14372 bool overflow = false;
14373
14374 found_base = cu->base_known;
14375 base = cu->base_address;
14376
14377 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14378 if (offset >= dwarf2_per_objfile->rnglists.size)
14379 {
b98664d3 14380 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14381 offset);
14382 return false;
14383 }
14384 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14385
14386 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14387
14388 while (1)
14389 {
7814882a
JK
14390 /* Initialize it due to a false compiler warning. */
14391 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14392 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14393 + dwarf2_per_objfile->rnglists.size);
14394 unsigned int bytes_read;
14395
14396 if (buffer == buf_end)
14397 {
14398 overflow = true;
14399 break;
14400 }
14401 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14402 switch (rlet)
14403 {
14404 case DW_RLE_end_of_list:
14405 break;
14406 case DW_RLE_base_address:
14407 if (buffer + cu->header.addr_size > buf_end)
14408 {
14409 overflow = true;
14410 break;
14411 }
14412 base = read_address (obfd, buffer, cu, &bytes_read);
14413 found_base = 1;
14414 buffer += bytes_read;
14415 break;
14416 case DW_RLE_start_length:
14417 if (buffer + cu->header.addr_size > buf_end)
14418 {
14419 overflow = true;
14420 break;
14421 }
14422 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14423 buffer += bytes_read;
14424 range_end = (range_beginning
14425 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14426 buffer += bytes_read;
14427 if (buffer > buf_end)
14428 {
14429 overflow = true;
14430 break;
14431 }
14432 break;
14433 case DW_RLE_offset_pair:
14434 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14435 buffer += bytes_read;
14436 if (buffer > buf_end)
14437 {
14438 overflow = true;
14439 break;
14440 }
14441 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14442 buffer += bytes_read;
14443 if (buffer > buf_end)
14444 {
14445 overflow = true;
14446 break;
14447 }
14448 break;
14449 case DW_RLE_start_end:
14450 if (buffer + 2 * cu->header.addr_size > buf_end)
14451 {
14452 overflow = true;
14453 break;
14454 }
14455 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14456 buffer += bytes_read;
14457 range_end = read_address (obfd, buffer, cu, &bytes_read);
14458 buffer += bytes_read;
14459 break;
14460 default:
b98664d3 14461 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14462 return false;
14463 }
14464 if (rlet == DW_RLE_end_of_list || overflow)
14465 break;
14466 if (rlet == DW_RLE_base_address)
14467 continue;
14468
14469 if (!found_base)
14470 {
14471 /* We have no valid base address for the ranges
14472 data. */
b98664d3 14473 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14474 return false;
14475 }
14476
14477 if (range_beginning > range_end)
14478 {
14479 /* Inverted range entries are invalid. */
b98664d3 14480 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14481 return false;
14482 }
14483
14484 /* Empty range entries have no effect. */
14485 if (range_beginning == range_end)
14486 continue;
14487
14488 range_beginning += base;
14489 range_end += base;
14490
14491 /* A not-uncommon case of bad debug info.
14492 Don't pollute the addrmap with bad data. */
14493 if (range_beginning + baseaddr == 0
14494 && !dwarf2_per_objfile->has_section_at_zero)
14495 {
b98664d3 14496 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14497 " [in module %s]"), objfile_name (objfile));
14498 continue;
14499 }
14500
14501 callback (range_beginning, range_end);
14502 }
14503
14504 if (overflow)
14505 {
b98664d3 14506 complaint (_("Offset %d is not terminated "
43988095
JK
14507 "for DW_AT_ranges attribute"),
14508 offset);
14509 return false;
14510 }
14511
14512 return true;
14513}
14514
14515/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14516 Callback's type should be:
14517 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14518 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14519
43988095 14520template <typename Callback>
43039443 14521static int
5f46c5a5 14522dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14523 Callback &&callback)
43039443 14524{
ed2dc618 14525 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14526 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14527 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14528 struct comp_unit_head *cu_header = &cu->header;
14529 bfd *obfd = objfile->obfd;
14530 unsigned int addr_size = cu_header->addr_size;
14531 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14532 /* Base address selection entry. */
14533 CORE_ADDR base;
14534 int found_base;
14535 unsigned int dummy;
d521ce57 14536 const gdb_byte *buffer;
ff013f42 14537 CORE_ADDR baseaddr;
43039443 14538
43988095
JK
14539 if (cu_header->version >= 5)
14540 return dwarf2_rnglists_process (offset, cu, callback);
14541
d00adf39
DE
14542 found_base = cu->base_known;
14543 base = cu->base_address;
43039443 14544
be391dca 14545 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14546 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14547 {
b98664d3 14548 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14549 offset);
14550 return 0;
14551 }
dce234bc 14552 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14553
e7030f15 14554 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14555
43039443
JK
14556 while (1)
14557 {
14558 CORE_ADDR range_beginning, range_end;
14559
14560 range_beginning = read_address (obfd, buffer, cu, &dummy);
14561 buffer += addr_size;
14562 range_end = read_address (obfd, buffer, cu, &dummy);
14563 buffer += addr_size;
14564 offset += 2 * addr_size;
14565
14566 /* An end of list marker is a pair of zero addresses. */
14567 if (range_beginning == 0 && range_end == 0)
14568 /* Found the end of list entry. */
14569 break;
14570
14571 /* Each base address selection entry is a pair of 2 values.
14572 The first is the largest possible address, the second is
14573 the base address. Check for a base address here. */
14574 if ((range_beginning & mask) == mask)
14575 {
28d2bfb9
AB
14576 /* If we found the largest possible address, then we already
14577 have the base address in range_end. */
14578 base = range_end;
43039443
JK
14579 found_base = 1;
14580 continue;
14581 }
14582
14583 if (!found_base)
14584 {
14585 /* We have no valid base address for the ranges
14586 data. */
b98664d3 14587 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14588 return 0;
14589 }
14590
9277c30c
UW
14591 if (range_beginning > range_end)
14592 {
14593 /* Inverted range entries are invalid. */
b98664d3 14594 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14595 return 0;
14596 }
14597
14598 /* Empty range entries have no effect. */
14599 if (range_beginning == range_end)
14600 continue;
14601
43039443
JK
14602 range_beginning += base;
14603 range_end += base;
14604
01093045
DE
14605 /* A not-uncommon case of bad debug info.
14606 Don't pollute the addrmap with bad data. */
14607 if (range_beginning + baseaddr == 0
14608 && !dwarf2_per_objfile->has_section_at_zero)
14609 {
b98664d3 14610 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14611 " [in module %s]"), objfile_name (objfile));
01093045
DE
14612 continue;
14613 }
14614
5f46c5a5
JK
14615 callback (range_beginning, range_end);
14616 }
14617
14618 return 1;
14619}
14620
14621/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14622 Return 1 if the attributes are present and valid, otherwise, return 0.
14623 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14624
14625static int
14626dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14627 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14628 struct partial_symtab *ranges_pst)
14629{
518817b3 14630 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14631 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14632 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14633 SECT_OFF_TEXT (objfile));
14634 int low_set = 0;
14635 CORE_ADDR low = 0;
14636 CORE_ADDR high = 0;
14637 int retval;
14638
14639 retval = dwarf2_ranges_process (offset, cu,
14640 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14641 {
9277c30c 14642 if (ranges_pst != NULL)
3e29f34a
MR
14643 {
14644 CORE_ADDR lowpc;
14645 CORE_ADDR highpc;
14646
79748972
TT
14647 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14648 range_beginning + baseaddr)
14649 - baseaddr);
14650 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14651 range_end + baseaddr)
14652 - baseaddr);
d320c2b5
TT
14653 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14654 lowpc, highpc - 1, ranges_pst);
3e29f34a 14655 }
ff013f42 14656
43039443
JK
14657 /* FIXME: This is recording everything as a low-high
14658 segment of consecutive addresses. We should have a
14659 data structure for discontiguous block ranges
14660 instead. */
14661 if (! low_set)
14662 {
14663 low = range_beginning;
14664 high = range_end;
14665 low_set = 1;
14666 }
14667 else
14668 {
14669 if (range_beginning < low)
14670 low = range_beginning;
14671 if (range_end > high)
14672 high = range_end;
14673 }
5f46c5a5
JK
14674 });
14675 if (!retval)
14676 return 0;
43039443
JK
14677
14678 if (! low_set)
14679 /* If the first entry is an end-of-list marker, the range
14680 describes an empty scope, i.e. no instructions. */
14681 return 0;
14682
14683 if (low_return)
14684 *low_return = low;
14685 if (high_return)
14686 *high_return = high;
14687 return 1;
14688}
14689
3a2b436a
JK
14690/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14691 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14692 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14693
3a2b436a 14694static enum pc_bounds_kind
af34e669 14695dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14696 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14697 struct partial_symtab *pst)
c906108c 14698{
518817b3
SM
14699 struct dwarf2_per_objfile *dwarf2_per_objfile
14700 = cu->per_cu->dwarf2_per_objfile;
c906108c 14701 struct attribute *attr;
91da1414 14702 struct attribute *attr_high;
af34e669
DJ
14703 CORE_ADDR low = 0;
14704 CORE_ADDR high = 0;
e385593e 14705 enum pc_bounds_kind ret;
c906108c 14706
91da1414
MW
14707 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14708 if (attr_high)
af34e669 14709 {
e142c38c 14710 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14711 if (attr != nullptr)
91da1414 14712 {
31aa7e4e
JB
14713 low = attr_value_as_address (attr);
14714 high = attr_value_as_address (attr_high);
14715 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14716 high += low;
91da1414 14717 }
af34e669
DJ
14718 else
14719 /* Found high w/o low attribute. */
e385593e 14720 return PC_BOUNDS_INVALID;
af34e669
DJ
14721
14722 /* Found consecutive range of addresses. */
3a2b436a 14723 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14724 }
c906108c 14725 else
af34e669 14726 {
e142c38c 14727 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14728 if (attr != NULL)
14729 {
ab435259
DE
14730 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14731 We take advantage of the fact that DW_AT_ranges does not appear
14732 in DW_TAG_compile_unit of DWO files. */
14733 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14734 unsigned int ranges_offset = (DW_UNSND (attr)
14735 + (need_ranges_base
14736 ? cu->ranges_base
14737 : 0));
2e3cf129 14738
af34e669 14739 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14740 .debug_ranges section. */
2e3cf129 14741 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14742 return PC_BOUNDS_INVALID;
43039443 14743 /* Found discontinuous range of addresses. */
3a2b436a 14744 ret = PC_BOUNDS_RANGES;
af34e669 14745 }
e385593e
JK
14746 else
14747 return PC_BOUNDS_NOT_PRESENT;
af34e669 14748 }
c906108c 14749
48fbe735 14750 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14751 if (high <= low)
e385593e 14752 return PC_BOUNDS_INVALID;
c906108c
SS
14753
14754 /* When using the GNU linker, .gnu.linkonce. sections are used to
14755 eliminate duplicate copies of functions and vtables and such.
14756 The linker will arbitrarily choose one and discard the others.
14757 The AT_*_pc values for such functions refer to local labels in
14758 these sections. If the section from that file was discarded, the
14759 labels are not in the output, so the relocs get a value of 0.
14760 If this is a discarded function, mark the pc bounds as invalid,
14761 so that GDB will ignore it. */
72dca2f5 14762 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14763 return PC_BOUNDS_INVALID;
c906108c
SS
14764
14765 *lowpc = low;
96408a79
SA
14766 if (highpc)
14767 *highpc = high;
af34e669 14768 return ret;
c906108c
SS
14769}
14770
b084d499
JB
14771/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14772 its low and high PC addresses. Do nothing if these addresses could not
14773 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14774 and HIGHPC to the high address if greater than HIGHPC. */
14775
14776static void
14777dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14778 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14779 struct dwarf2_cu *cu)
14780{
14781 CORE_ADDR low, high;
14782 struct die_info *child = die->child;
14783
e385593e 14784 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14785 {
325fac50
PA
14786 *lowpc = std::min (*lowpc, low);
14787 *highpc = std::max (*highpc, high);
b084d499
JB
14788 }
14789
14790 /* If the language does not allow nested subprograms (either inside
14791 subprograms or lexical blocks), we're done. */
14792 if (cu->language != language_ada)
14793 return;
6e70227d 14794
b084d499
JB
14795 /* Check all the children of the given DIE. If it contains nested
14796 subprograms, then check their pc bounds. Likewise, we need to
14797 check lexical blocks as well, as they may also contain subprogram
14798 definitions. */
14799 while (child && child->tag)
14800 {
14801 if (child->tag == DW_TAG_subprogram
14802 || child->tag == DW_TAG_lexical_block)
14803 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14804 child = sibling_die (child);
14805 }
14806}
14807
fae299cd
DC
14808/* Get the low and high pc's represented by the scope DIE, and store
14809 them in *LOWPC and *HIGHPC. If the correct values can't be
14810 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14811
14812static void
14813get_scope_pc_bounds (struct die_info *die,
14814 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14815 struct dwarf2_cu *cu)
14816{
14817 CORE_ADDR best_low = (CORE_ADDR) -1;
14818 CORE_ADDR best_high = (CORE_ADDR) 0;
14819 CORE_ADDR current_low, current_high;
14820
3a2b436a 14821 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14822 >= PC_BOUNDS_RANGES)
fae299cd
DC
14823 {
14824 best_low = current_low;
14825 best_high = current_high;
14826 }
14827 else
14828 {
14829 struct die_info *child = die->child;
14830
14831 while (child && child->tag)
14832 {
14833 switch (child->tag) {
14834 case DW_TAG_subprogram:
b084d499 14835 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14836 break;
14837 case DW_TAG_namespace:
f55ee35c 14838 case DW_TAG_module:
fae299cd
DC
14839 /* FIXME: carlton/2004-01-16: Should we do this for
14840 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14841 that current GCC's always emit the DIEs corresponding
14842 to definitions of methods of classes as children of a
14843 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14844 the DIEs giving the declarations, which could be
14845 anywhere). But I don't see any reason why the
14846 standards says that they have to be there. */
14847 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14848
14849 if (current_low != ((CORE_ADDR) -1))
14850 {
325fac50
PA
14851 best_low = std::min (best_low, current_low);
14852 best_high = std::max (best_high, current_high);
fae299cd
DC
14853 }
14854 break;
14855 default:
0963b4bd 14856 /* Ignore. */
fae299cd
DC
14857 break;
14858 }
14859
14860 child = sibling_die (child);
14861 }
14862 }
14863
14864 *lowpc = best_low;
14865 *highpc = best_high;
14866}
14867
801e3a5b
JB
14868/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14869 in DIE. */
380bca97 14870
801e3a5b
JB
14871static void
14872dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14873 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14874{
518817b3 14875 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14876 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14877 struct attribute *attr;
91da1414 14878 struct attribute *attr_high;
801e3a5b 14879
91da1414
MW
14880 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14881 if (attr_high)
801e3a5b 14882 {
801e3a5b 14883 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14884 if (attr != nullptr)
801e3a5b 14885 {
31aa7e4e
JB
14886 CORE_ADDR low = attr_value_as_address (attr);
14887 CORE_ADDR high = attr_value_as_address (attr_high);
14888
14889 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14890 high += low;
9a619af0 14891
3e29f34a
MR
14892 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14893 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14894 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14895 }
14896 }
14897
14898 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14899 if (attr != nullptr)
801e3a5b 14900 {
ab435259
DE
14901 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14902 We take advantage of the fact that DW_AT_ranges does not appear
14903 in DW_TAG_compile_unit of DWO files. */
14904 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14905
14906 /* The value of the DW_AT_ranges attribute is the offset of the
14907 address range list in the .debug_ranges section. */
ab435259
DE
14908 unsigned long offset = (DW_UNSND (attr)
14909 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14910
2d5f09ec 14911 std::vector<blockrange> blockvec;
5f46c5a5
JK
14912 dwarf2_ranges_process (offset, cu,
14913 [&] (CORE_ADDR start, CORE_ADDR end)
14914 {
58fdfd2c
JK
14915 start += baseaddr;
14916 end += baseaddr;
5f46c5a5
JK
14917 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14918 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14919 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14920 blockvec.emplace_back (start, end);
5f46c5a5 14921 });
2d5f09ec
KB
14922
14923 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14924 }
14925}
14926
685b1105
JK
14927/* Check whether the producer field indicates either of GCC < 4.6, or the
14928 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14929
685b1105
JK
14930static void
14931check_producer (struct dwarf2_cu *cu)
60d5a603 14932{
38360086 14933 int major, minor;
60d5a603
JK
14934
14935 if (cu->producer == NULL)
14936 {
14937 /* For unknown compilers expect their behavior is DWARF version
14938 compliant.
14939
14940 GCC started to support .debug_types sections by -gdwarf-4 since
14941 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14942 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14943 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14944 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14945 }
b1ffba5a 14946 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14947 {
38360086
MW
14948 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14949 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14950 }
5230b05a 14951 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14952 {
14953 cu->producer_is_icc = true;
14954 cu->producer_is_icc_lt_14 = major < 14;
14955 }
c258c396
JD
14956 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14957 cu->producer_is_codewarrior = true;
685b1105
JK
14958 else
14959 {
14960 /* For other non-GCC compilers, expect their behavior is DWARF version
14961 compliant. */
60d5a603
JK
14962 }
14963
9068261f 14964 cu->checked_producer = true;
685b1105 14965}
ba919b58 14966
685b1105
JK
14967/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14968 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14969 during 4.6.0 experimental. */
14970
9068261f 14971static bool
685b1105
JK
14972producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14973{
14974 if (!cu->checked_producer)
14975 check_producer (cu);
14976
14977 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14978}
14979
c258c396
JD
14980
14981/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14982 with incorrect is_stmt attributes. */
14983
14984static bool
14985producer_is_codewarrior (struct dwarf2_cu *cu)
14986{
14987 if (!cu->checked_producer)
14988 check_producer (cu);
14989
14990 return cu->producer_is_codewarrior;
14991}
14992
405feb71 14993/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14994 DW_AT_accessibility. */
14995
14996static enum dwarf_access_attribute
14997dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14998{
14999 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15000 {
15001 /* The default DWARF 2 accessibility for members is public, the default
15002 accessibility for inheritance is private. */
15003
15004 if (die->tag != DW_TAG_inheritance)
15005 return DW_ACCESS_public;
15006 else
15007 return DW_ACCESS_private;
15008 }
15009 else
15010 {
15011 /* DWARF 3+ defines the default accessibility a different way. The same
15012 rules apply now for DW_TAG_inheritance as for the members and it only
15013 depends on the container kind. */
15014
15015 if (die->parent->tag == DW_TAG_class_type)
15016 return DW_ACCESS_private;
15017 else
15018 return DW_ACCESS_public;
15019 }
15020}
15021
74ac6d43
TT
15022/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15023 offset. If the attribute was not found return 0, otherwise return
15024 1. If it was found but could not properly be handled, set *OFFSET
15025 to 0. */
15026
15027static int
15028handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15029 LONGEST *offset)
15030{
15031 struct attribute *attr;
15032
15033 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15034 if (attr != NULL)
15035 {
15036 *offset = 0;
15037
15038 /* Note that we do not check for a section offset first here.
15039 This is because DW_AT_data_member_location is new in DWARF 4,
15040 so if we see it, we can assume that a constant form is really
15041 a constant and not a section offset. */
15042 if (attr_form_is_constant (attr))
15043 *offset = dwarf2_get_attr_constant_value (attr, 0);
15044 else if (attr_form_is_section_offset (attr))
15045 dwarf2_complex_location_expr_complaint ();
15046 else if (attr_form_is_block (attr))
15047 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15048 else
15049 dwarf2_complex_location_expr_complaint ();
15050
15051 return 1;
15052 }
15053
15054 return 0;
15055}
15056
c906108c
SS
15057/* Add an aggregate field to the field list. */
15058
15059static void
107d2387 15060dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15061 struct dwarf2_cu *cu)
6e70227d 15062{
518817b3 15063 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15064 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15065 struct nextfield *new_field;
15066 struct attribute *attr;
15067 struct field *fp;
15d034d0 15068 const char *fieldname = "";
c906108c 15069
7d0ccb61
DJ
15070 if (die->tag == DW_TAG_inheritance)
15071 {
be2daae6
TT
15072 fip->baseclasses.emplace_back ();
15073 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15074 }
15075 else
15076 {
be2daae6
TT
15077 fip->fields.emplace_back ();
15078 new_field = &fip->fields.back ();
7d0ccb61 15079 }
be2daae6 15080
c906108c
SS
15081 fip->nfields++;
15082
e142c38c 15083 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15084 if (attr != nullptr)
c906108c 15085 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15086 else
15087 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15088 if (new_field->accessibility != DW_ACCESS_public)
15089 fip->non_public_fields = 1;
60d5a603 15090
e142c38c 15091 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15092 if (attr != nullptr)
c906108c 15093 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15094 else
15095 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15096
15097 fp = &new_field->field;
a9a9bd0f 15098
e142c38c 15099 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15100 {
74ac6d43
TT
15101 LONGEST offset;
15102
a9a9bd0f 15103 /* Data member other than a C++ static data member. */
6e70227d 15104
c906108c 15105 /* Get type of field. */
e7c27a73 15106 fp->type = die_type (die, cu);
c906108c 15107
d6a843b5 15108 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15109
c906108c 15110 /* Get bit size of field (zero if none). */
e142c38c 15111 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15112 if (attr != nullptr)
c906108c
SS
15113 {
15114 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15115 }
15116 else
15117 {
15118 FIELD_BITSIZE (*fp) = 0;
15119 }
15120
15121 /* Get bit offset of field. */
74ac6d43
TT
15122 if (handle_data_member_location (die, cu, &offset))
15123 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15124 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15125 if (attr != nullptr)
c906108c 15126 {
5e2b427d 15127 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15128 {
15129 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15130 additional bit offset from the MSB of the containing
15131 anonymous object to the MSB of the field. We don't
15132 have to do anything special since we don't need to
15133 know the size of the anonymous object. */
f41f5e61 15134 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15135 }
15136 else
15137 {
15138 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15139 MSB of the anonymous object, subtract off the number of
15140 bits from the MSB of the field to the MSB of the
15141 object, and then subtract off the number of bits of
15142 the field itself. The result is the bit offset of
15143 the LSB of the field. */
c906108c
SS
15144 int anonymous_size;
15145 int bit_offset = DW_UNSND (attr);
15146
e142c38c 15147 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15148 if (attr != nullptr)
c906108c
SS
15149 {
15150 /* The size of the anonymous object containing
15151 the bit field is explicit, so use the
15152 indicated size (in bytes). */
15153 anonymous_size = DW_UNSND (attr);
15154 }
15155 else
15156 {
15157 /* The size of the anonymous object containing
15158 the bit field must be inferred from the type
15159 attribute of the data member containing the
15160 bit field. */
15161 anonymous_size = TYPE_LENGTH (fp->type);
15162 }
f41f5e61
PA
15163 SET_FIELD_BITPOS (*fp,
15164 (FIELD_BITPOS (*fp)
15165 + anonymous_size * bits_per_byte
15166 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15167 }
15168 }
da5b30da
AA
15169 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15170 if (attr != NULL)
15171 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15172 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15173
15174 /* Get name of field. */
39cbfefa
DJ
15175 fieldname = dwarf2_name (die, cu);
15176 if (fieldname == NULL)
15177 fieldname = "";
d8151005
DJ
15178
15179 /* The name is already allocated along with this objfile, so we don't
15180 need to duplicate it for the type. */
15181 fp->name = fieldname;
c906108c
SS
15182
15183 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15184 pointer or virtual base class pointer) to private. */
e142c38c 15185 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15186 {
d48cc9dd 15187 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15188 new_field->accessibility = DW_ACCESS_private;
15189 fip->non_public_fields = 1;
15190 }
15191 }
a9a9bd0f 15192 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15193 {
a9a9bd0f
DC
15194 /* C++ static member. */
15195
15196 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15197 is a declaration, but all versions of G++ as of this writing
15198 (so through at least 3.2.1) incorrectly generate
15199 DW_TAG_variable tags. */
6e70227d 15200
ff355380 15201 const char *physname;
c906108c 15202
a9a9bd0f 15203 /* Get name of field. */
39cbfefa
DJ
15204 fieldname = dwarf2_name (die, cu);
15205 if (fieldname == NULL)
c906108c
SS
15206 return;
15207
254e6b9e 15208 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15209 if (attr
15210 /* Only create a symbol if this is an external value.
15211 new_symbol checks this and puts the value in the global symbol
15212 table, which we want. If it is not external, new_symbol
15213 will try to put the value in cu->list_in_scope which is wrong. */
15214 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15215 {
15216 /* A static const member, not much different than an enum as far as
15217 we're concerned, except that we can support more types. */
15218 new_symbol (die, NULL, cu);
15219 }
15220
2df3850c 15221 /* Get physical name. */
ff355380 15222 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15223
d8151005
DJ
15224 /* The name is already allocated along with this objfile, so we don't
15225 need to duplicate it for the type. */
15226 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15227 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15228 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15229 }
15230 else if (die->tag == DW_TAG_inheritance)
15231 {
74ac6d43 15232 LONGEST offset;
d4b96c9a 15233
74ac6d43
TT
15234 /* C++ base class field. */
15235 if (handle_data_member_location (die, cu, &offset))
15236 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15237 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15238 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15239 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15240 }
2ddeaf8a
TT
15241 else if (die->tag == DW_TAG_variant_part)
15242 {
15243 /* process_structure_scope will treat this DIE as a union. */
15244 process_structure_scope (die, cu);
15245
15246 /* The variant part is relative to the start of the enclosing
15247 structure. */
15248 SET_FIELD_BITPOS (*fp, 0);
15249 fp->type = get_die_type (die, cu);
15250 fp->artificial = 1;
15251 fp->name = "<<variant>>";
c8c81635
TT
15252
15253 /* Normally a DW_TAG_variant_part won't have a size, but our
15254 representation requires one, so set it to the maximum of the
489dbda6
TT
15255 child sizes, being sure to account for the offset at which
15256 each child is seen. */
c8c81635
TT
15257 if (TYPE_LENGTH (fp->type) == 0)
15258 {
15259 unsigned max = 0;
15260 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
15261 {
15262 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15263 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15264 if (len > max)
15265 max = len;
15266 }
c8c81635
TT
15267 TYPE_LENGTH (fp->type) = max;
15268 }
2ddeaf8a
TT
15269 }
15270 else
15271 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15272}
15273
883fd55a
KS
15274/* Can the type given by DIE define another type? */
15275
15276static bool
15277type_can_define_types (const struct die_info *die)
15278{
15279 switch (die->tag)
15280 {
15281 case DW_TAG_typedef:
15282 case DW_TAG_class_type:
15283 case DW_TAG_structure_type:
15284 case DW_TAG_union_type:
15285 case DW_TAG_enumeration_type:
15286 return true;
15287
15288 default:
15289 return false;
15290 }
15291}
15292
15293/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15294
15295static void
883fd55a
KS
15296dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15297 struct dwarf2_cu *cu)
6e70227d 15298{
be2daae6
TT
15299 struct decl_field fp;
15300 memset (&fp, 0, sizeof (fp));
98751a41 15301
883fd55a 15302 gdb_assert (type_can_define_types (die));
98751a41 15303
883fd55a 15304 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15305 fp.name = dwarf2_name (die, cu);
15306 fp.type = read_type_die (die, cu);
98751a41 15307
c191a687
KS
15308 /* Save accessibility. */
15309 enum dwarf_access_attribute accessibility;
15310 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15311 if (attr != NULL)
15312 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15313 else
15314 accessibility = dwarf2_default_access_attribute (die, cu);
15315 switch (accessibility)
15316 {
15317 case DW_ACCESS_public:
15318 /* The assumed value if neither private nor protected. */
15319 break;
15320 case DW_ACCESS_private:
be2daae6 15321 fp.is_private = 1;
c191a687
KS
15322 break;
15323 case DW_ACCESS_protected:
be2daae6 15324 fp.is_protected = 1;
c191a687
KS
15325 break;
15326 default:
b98664d3 15327 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15328 }
15329
883fd55a 15330 if (die->tag == DW_TAG_typedef)
be2daae6 15331 fip->typedef_field_list.push_back (fp);
883fd55a 15332 else
be2daae6 15333 fip->nested_types_list.push_back (fp);
98751a41
JK
15334}
15335
c906108c
SS
15336/* Create the vector of fields, and attach it to the type. */
15337
15338static void
fba45db2 15339dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15340 struct dwarf2_cu *cu)
c906108c
SS
15341{
15342 int nfields = fip->nfields;
15343
15344 /* Record the field count, allocate space for the array of fields,
15345 and create blank accessibility bitfields if necessary. */
15346 TYPE_NFIELDS (type) = nfields;
15347 TYPE_FIELDS (type) = (struct field *)
be2daae6 15348 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15349
b4ba55a1 15350 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15351 {
15352 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15353
15354 TYPE_FIELD_PRIVATE_BITS (type) =
15355 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15356 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15357
15358 TYPE_FIELD_PROTECTED_BITS (type) =
15359 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15360 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15361
774b6a14
TT
15362 TYPE_FIELD_IGNORE_BITS (type) =
15363 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15364 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15365 }
15366
15367 /* If the type has baseclasses, allocate and clear a bit vector for
15368 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15369 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15370 {
be2daae6 15371 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15372 unsigned char *pointer;
c906108c
SS
15373
15374 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15375 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15376 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15377 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15378 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15379 }
15380
2ddeaf8a
TT
15381 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15382 {
15383 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15384
be2daae6 15385 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15386 {
be2daae6
TT
15387 struct nextfield &field = fip->fields[index];
15388
15389 if (field.variant.is_discriminant)
2ddeaf8a 15390 di->discriminant_index = index;
be2daae6 15391 else if (field.variant.default_branch)
2ddeaf8a
TT
15392 di->default_index = index;
15393 else
be2daae6 15394 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15395 }
15396 }
15397
be2daae6
TT
15398 /* Copy the saved-up fields into the field vector. */
15399 for (int i = 0; i < nfields; ++i)
c906108c 15400 {
be2daae6
TT
15401 struct nextfield &field
15402 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15403 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15404
be2daae6
TT
15405 TYPE_FIELD (type, i) = field.field;
15406 switch (field.accessibility)
c906108c 15407 {
c5aa993b 15408 case DW_ACCESS_private:
b4ba55a1 15409 if (cu->language != language_ada)
be2daae6 15410 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15411 break;
c906108c 15412
c5aa993b 15413 case DW_ACCESS_protected:
b4ba55a1 15414 if (cu->language != language_ada)
be2daae6 15415 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15416 break;
c906108c 15417
c5aa993b
JM
15418 case DW_ACCESS_public:
15419 break;
c906108c 15420
c5aa993b
JM
15421 default:
15422 /* Unknown accessibility. Complain and treat it as public. */
15423 {
b98664d3 15424 complaint (_("unsupported accessibility %d"),
be2daae6 15425 field.accessibility);
c5aa993b
JM
15426 }
15427 break;
c906108c 15428 }
be2daae6 15429 if (i < fip->baseclasses.size ())
c906108c 15430 {
be2daae6 15431 switch (field.virtuality)
c906108c 15432 {
c5aa993b
JM
15433 case DW_VIRTUALITY_virtual:
15434 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15435 if (cu->language == language_ada)
a73c6dcd 15436 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15437 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15438 break;
c906108c
SS
15439 }
15440 }
c906108c
SS
15441 }
15442}
15443
7d27a96d
TT
15444/* Return true if this member function is a constructor, false
15445 otherwise. */
15446
15447static int
15448dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15449{
15450 const char *fieldname;
fe978cb0 15451 const char *type_name;
7d27a96d
TT
15452 int len;
15453
15454 if (die->parent == NULL)
15455 return 0;
15456
15457 if (die->parent->tag != DW_TAG_structure_type
15458 && die->parent->tag != DW_TAG_union_type
15459 && die->parent->tag != DW_TAG_class_type)
15460 return 0;
15461
15462 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15463 type_name = dwarf2_name (die->parent, cu);
15464 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15465 return 0;
15466
15467 len = strlen (fieldname);
fe978cb0
PA
15468 return (strncmp (fieldname, type_name, len) == 0
15469 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15470}
15471
c906108c
SS
15472/* Add a member function to the proper fieldlist. */
15473
15474static void
107d2387 15475dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15476 struct type *type, struct dwarf2_cu *cu)
c906108c 15477{
518817b3 15478 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15479 struct attribute *attr;
c906108c 15480 int i;
be2daae6 15481 struct fnfieldlist *flp = nullptr;
c906108c 15482 struct fn_field *fnp;
15d034d0 15483 const char *fieldname;
f792889a 15484 struct type *this_type;
60d5a603 15485 enum dwarf_access_attribute accessibility;
c906108c 15486
b4ba55a1 15487 if (cu->language == language_ada)
a73c6dcd 15488 error (_("unexpected member function in Ada type"));
b4ba55a1 15489
2df3850c 15490 /* Get name of member function. */
39cbfefa
DJ
15491 fieldname = dwarf2_name (die, cu);
15492 if (fieldname == NULL)
2df3850c 15493 return;
c906108c 15494
c906108c 15495 /* Look up member function name in fieldlist. */
be2daae6 15496 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15497 {
27bfe10e 15498 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15499 {
15500 flp = &fip->fnfieldlists[i];
15501 break;
15502 }
c906108c
SS
15503 }
15504
be2daae6
TT
15505 /* Create a new fnfieldlist if necessary. */
15506 if (flp == nullptr)
c906108c 15507 {
be2daae6
TT
15508 fip->fnfieldlists.emplace_back ();
15509 flp = &fip->fnfieldlists.back ();
c906108c 15510 flp->name = fieldname;
be2daae6 15511 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15512 }
15513
be2daae6
TT
15514 /* Create a new member function field and add it to the vector of
15515 fnfieldlists. */
15516 flp->fnfields.emplace_back ();
15517 fnp = &flp->fnfields.back ();
3da10d80
KS
15518
15519 /* Delay processing of the physname until later. */
9c37b5ae 15520 if (cu->language == language_cplus)
be2daae6
TT
15521 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15522 die, cu);
3da10d80
KS
15523 else
15524 {
1d06ead6 15525 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15526 fnp->physname = physname ? physname : "";
15527 }
15528
c906108c 15529 fnp->type = alloc_type (objfile);
f792889a
DJ
15530 this_type = read_type_die (die, cu);
15531 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15532 {
f792889a 15533 int nparams = TYPE_NFIELDS (this_type);
c906108c 15534
f792889a 15535 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15536 of the method itself (TYPE_CODE_METHOD). */
15537 smash_to_method_type (fnp->type, type,
f792889a
DJ
15538 TYPE_TARGET_TYPE (this_type),
15539 TYPE_FIELDS (this_type),
15540 TYPE_NFIELDS (this_type),
15541 TYPE_VARARGS (this_type));
c906108c
SS
15542
15543 /* Handle static member functions.
c5aa993b 15544 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15545 member functions. G++ helps GDB by marking the first
15546 parameter for non-static member functions (which is the this
15547 pointer) as artificial. We obtain this information from
15548 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15549 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15550 fnp->voffset = VOFFSET_STATIC;
15551 }
15552 else
b98664d3 15553 complaint (_("member function type missing for '%s'"),
3da10d80 15554 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15555
15556 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15557 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15558 fnp->fcontext = die_containing_type (die, cu);
c906108c 15559
3e43a32a
MS
15560 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15561 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15562
15563 /* Get accessibility. */
e142c38c 15564 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15565 if (attr != nullptr)
aead7601 15566 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15567 else
15568 accessibility = dwarf2_default_access_attribute (die, cu);
15569 switch (accessibility)
c906108c 15570 {
60d5a603
JK
15571 case DW_ACCESS_private:
15572 fnp->is_private = 1;
15573 break;
15574 case DW_ACCESS_protected:
15575 fnp->is_protected = 1;
15576 break;
c906108c
SS
15577 }
15578
b02dede2 15579 /* Check for artificial methods. */
e142c38c 15580 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15581 if (attr && DW_UNSND (attr) != 0)
15582 fnp->is_artificial = 1;
15583
7d27a96d
TT
15584 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15585
0d564a31 15586 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15587 function. For older versions of GCC, this is an offset in the
15588 appropriate virtual table, as specified by DW_AT_containing_type.
15589 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15590 to the object address. */
15591
e142c38c 15592 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15593 if (attr != nullptr)
8e19ed76 15594 {
aec5aa8b 15595 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15596 {
aec5aa8b
TT
15597 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15598 {
15599 /* Old-style GCC. */
15600 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15601 }
15602 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15603 || (DW_BLOCK (attr)->size > 1
15604 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15605 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15606 {
aec5aa8b
TT
15607 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15608 if ((fnp->voffset % cu->header.addr_size) != 0)
15609 dwarf2_complex_location_expr_complaint ();
15610 else
15611 fnp->voffset /= cu->header.addr_size;
15612 fnp->voffset += 2;
15613 }
15614 else
15615 dwarf2_complex_location_expr_complaint ();
15616
15617 if (!fnp->fcontext)
7e993ebf
KS
15618 {
15619 /* If there is no `this' field and no DW_AT_containing_type,
15620 we cannot actually find a base class context for the
15621 vtable! */
15622 if (TYPE_NFIELDS (this_type) == 0
15623 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15624 {
b98664d3 15625 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15626 "function \"%s\" (offset %s)"),
15627 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15628 }
15629 else
15630 {
15631 fnp->fcontext
15632 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15633 }
15634 }
aec5aa8b 15635 }
3690dd37 15636 else if (attr_form_is_section_offset (attr))
8e19ed76 15637 {
4d3c2250 15638 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15639 }
15640 else
15641 {
4d3c2250
KB
15642 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15643 fieldname);
8e19ed76 15644 }
0d564a31 15645 }
d48cc9dd
DJ
15646 else
15647 {
15648 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15649 if (attr && DW_UNSND (attr))
15650 {
15651 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15652 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15653 "but the vtable offset is not specified"),
9d8780f0 15654 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15655 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15656 TYPE_CPLUS_DYNAMIC (type) = 1;
15657 }
15658 }
c906108c
SS
15659}
15660
15661/* Create the vector of member function fields, and attach it to the type. */
15662
15663static void
fba45db2 15664dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15665 struct dwarf2_cu *cu)
c906108c 15666{
b4ba55a1 15667 if (cu->language == language_ada)
a73c6dcd 15668 error (_("unexpected member functions in Ada type"));
b4ba55a1 15669
c906108c
SS
15670 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15671 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15672 TYPE_ALLOC (type,
15673 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15674
be2daae6 15675 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15676 {
be2daae6 15677 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15678 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15679
be2daae6
TT
15680 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15681 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15682 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15683 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15684
15685 for (int k = 0; k < nf.fnfields.size (); ++k)
15686 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15687 }
15688
be2daae6 15689 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15690}
15691
1168df01
JB
15692/* Returns non-zero if NAME is the name of a vtable member in CU's
15693 language, zero otherwise. */
15694static int
15695is_vtable_name (const char *name, struct dwarf2_cu *cu)
15696{
15697 static const char vptr[] = "_vptr";
15698
9c37b5ae
TT
15699 /* Look for the C++ form of the vtable. */
15700 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15701 return 1;
15702
15703 return 0;
15704}
15705
c0dd20ea 15706/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15707 functions, with the ABI-specified layout. If TYPE describes
15708 such a structure, smash it into a member function type.
61049d3b
DJ
15709
15710 GCC shouldn't do this; it should just output pointer to member DIEs.
15711 This is GCC PR debug/28767. */
c0dd20ea 15712
0b92b5bb
TT
15713static void
15714quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15715{
09e2d7c7 15716 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15717
15718 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15719 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15720 return;
c0dd20ea
DJ
15721
15722 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15723 if (TYPE_FIELD_NAME (type, 0) == NULL
15724 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15725 || TYPE_FIELD_NAME (type, 1) == NULL
15726 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15727 return;
c0dd20ea
DJ
15728
15729 /* Find the type of the method. */
0b92b5bb 15730 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15731 if (pfn_type == NULL
15732 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15733 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15734 return;
c0dd20ea
DJ
15735
15736 /* Look for the "this" argument. */
15737 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15738 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15739 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15740 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15741 return;
c0dd20ea 15742
09e2d7c7 15743 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15744 new_type = alloc_type (objfile);
09e2d7c7 15745 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15746 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15747 TYPE_VARARGS (pfn_type));
0b92b5bb 15748 smash_to_methodptr_type (type, new_type);
c0dd20ea 15749}
1168df01 15750
2b4424c3
TT
15751/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15752 appropriate error checking and issuing complaints if there is a
15753 problem. */
15754
15755static ULONGEST
15756get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15757{
15758 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15759
15760 if (attr == nullptr)
15761 return 0;
15762
15763 if (!attr_form_is_constant (attr))
15764 {
b98664d3 15765 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15766 " - DIE at %s [in module %s]"),
15767 sect_offset_str (die->sect_off),
15768 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15769 return 0;
15770 }
15771
15772 ULONGEST align;
15773 if (attr->form == DW_FORM_sdata)
15774 {
15775 LONGEST val = DW_SND (attr);
15776 if (val < 0)
15777 {
b98664d3 15778 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15779 " - DIE at %s [in module %s]"),
15780 sect_offset_str (die->sect_off),
15781 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15782 return 0;
15783 }
15784 align = val;
15785 }
15786 else
15787 align = DW_UNSND (attr);
15788
15789 if (align == 0)
15790 {
b98664d3 15791 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15792 " - DIE at %s [in module %s]"),
15793 sect_offset_str (die->sect_off),
15794 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15795 return 0;
15796 }
15797 if ((align & (align - 1)) != 0)
15798 {
b98664d3 15799 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15800 " - DIE at %s [in module %s]"),
15801 sect_offset_str (die->sect_off),
15802 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15803 return 0;
15804 }
15805
15806 return align;
15807}
15808
15809/* If the DIE has a DW_AT_alignment attribute, use its value to set
15810 the alignment for TYPE. */
15811
15812static void
15813maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15814 struct type *type)
15815{
15816 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15817 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15818 " - DIE at %s [in module %s]"),
15819 sect_offset_str (die->sect_off),
15820 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15821}
685b1105 15822
c906108c 15823/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15824 (definition) to create a type for the structure or union. Fill in
15825 the type's name and general properties; the members will not be
83655187
DE
15826 processed until process_structure_scope. A symbol table entry for
15827 the type will also not be done until process_structure_scope (assuming
15828 the type has a name).
c906108c 15829
c767944b
DJ
15830 NOTE: we need to call these functions regardless of whether or not the
15831 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15832 structure or union. This gets the type entered into our set of
83655187 15833 user defined types. */
c906108c 15834
f792889a 15835static struct type *
134d01f1 15836read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15837{
518817b3 15838 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15839 struct type *type;
15840 struct attribute *attr;
15d034d0 15841 const char *name;
c906108c 15842
348e048f
DE
15843 /* If the definition of this type lives in .debug_types, read that type.
15844 Don't follow DW_AT_specification though, that will take us back up
15845 the chain and we want to go down. */
45e58e77 15846 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15847 if (attr != nullptr)
348e048f 15848 {
ac9ec31b 15849 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15850
ac9ec31b 15851 /* The type's CU may not be the same as CU.
02142a6c 15852 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15853 return set_die_type (die, type, cu);
15854 }
15855
c0dd20ea 15856 type = alloc_type (objfile);
c906108c 15857 INIT_CPLUS_SPECIFIC (type);
93311388 15858
39cbfefa
DJ
15859 name = dwarf2_name (die, cu);
15860 if (name != NULL)
c906108c 15861 {
987504bb 15862 if (cu->language == language_cplus
c44af4eb
TT
15863 || cu->language == language_d
15864 || cu->language == language_rust)
63d06c5c 15865 {
15d034d0 15866 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15867
15868 /* dwarf2_full_name might have already finished building the DIE's
15869 type. If so, there is no need to continue. */
15870 if (get_die_type (die, cu) != NULL)
15871 return get_die_type (die, cu);
15872
e86ca25f 15873 TYPE_NAME (type) = full_name;
63d06c5c
DC
15874 }
15875 else
15876 {
d8151005
DJ
15877 /* The name is already allocated along with this objfile, so
15878 we don't need to duplicate it for the type. */
e86ca25f 15879 TYPE_NAME (type) = name;
63d06c5c 15880 }
c906108c
SS
15881 }
15882
15883 if (die->tag == DW_TAG_structure_type)
15884 {
15885 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15886 }
15887 else if (die->tag == DW_TAG_union_type)
15888 {
15889 TYPE_CODE (type) = TYPE_CODE_UNION;
15890 }
2ddeaf8a
TT
15891 else if (die->tag == DW_TAG_variant_part)
15892 {
15893 TYPE_CODE (type) = TYPE_CODE_UNION;
15894 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15895 }
c906108c
SS
15896 else
15897 {
4753d33b 15898 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15899 }
15900
0cc2414c
TT
15901 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15902 TYPE_DECLARED_CLASS (type) = 1;
15903
e142c38c 15904 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15905 if (attr != nullptr)
c906108c 15906 {
155bfbd3
JB
15907 if (attr_form_is_constant (attr))
15908 TYPE_LENGTH (type) = DW_UNSND (attr);
15909 else
15910 {
15911 /* For the moment, dynamic type sizes are not supported
15912 by GDB's struct type. The actual size is determined
15913 on-demand when resolving the type of a given object,
15914 so set the type's length to zero for now. Otherwise,
15915 we record an expression as the length, and that expression
15916 could lead to a very large value, which could eventually
15917 lead to us trying to allocate that much memory when creating
15918 a value of that type. */
15919 TYPE_LENGTH (type) = 0;
15920 }
c906108c
SS
15921 }
15922 else
15923 {
15924 TYPE_LENGTH (type) = 0;
15925 }
15926
2b4424c3
TT
15927 maybe_set_alignment (cu, die, type);
15928
5230b05a 15929 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15930 {
5230b05a
WT
15931 /* ICC<14 does not output the required DW_AT_declaration on
15932 incomplete types, but gives them a size of zero. */
422b1cb0 15933 TYPE_STUB (type) = 1;
685b1105
JK
15934 }
15935 else
15936 TYPE_STUB_SUPPORTED (type) = 1;
15937
dc718098 15938 if (die_is_declaration (die, cu))
876cecd0 15939 TYPE_STUB (type) = 1;
a6c727b2
DJ
15940 else if (attr == NULL && die->child == NULL
15941 && producer_is_realview (cu->producer))
15942 /* RealView does not output the required DW_AT_declaration
15943 on incomplete types. */
15944 TYPE_STUB (type) = 1;
dc718098 15945
c906108c
SS
15946 /* We need to add the type field to the die immediately so we don't
15947 infinitely recurse when dealing with pointers to the structure
0963b4bd 15948 type within the structure itself. */
1c379e20 15949 set_die_type (die, type, cu);
c906108c 15950
7e314c57
JK
15951 /* set_die_type should be already done. */
15952 set_descriptive_type (type, die, cu);
15953
c767944b
DJ
15954 return type;
15955}
15956
2ddeaf8a
TT
15957/* A helper for process_structure_scope that handles a single member
15958 DIE. */
15959
15960static void
15961handle_struct_member_die (struct die_info *child_die, struct type *type,
15962 struct field_info *fi,
15963 std::vector<struct symbol *> *template_args,
15964 struct dwarf2_cu *cu)
15965{
15966 if (child_die->tag == DW_TAG_member
15967 || child_die->tag == DW_TAG_variable
15968 || child_die->tag == DW_TAG_variant_part)
15969 {
15970 /* NOTE: carlton/2002-11-05: A C++ static data member
15971 should be a DW_TAG_member that is a declaration, but
15972 all versions of G++ as of this writing (so through at
15973 least 3.2.1) incorrectly generate DW_TAG_variable
15974 tags for them instead. */
15975 dwarf2_add_field (fi, child_die, cu);
15976 }
15977 else if (child_die->tag == DW_TAG_subprogram)
15978 {
15979 /* Rust doesn't have member functions in the C++ sense.
15980 However, it does emit ordinary functions as children
15981 of a struct DIE. */
15982 if (cu->language == language_rust)
15983 read_func_scope (child_die, cu);
15984 else
15985 {
15986 /* C++ member function. */
15987 dwarf2_add_member_fn (fi, child_die, type, cu);
15988 }
15989 }
15990 else if (child_die->tag == DW_TAG_inheritance)
15991 {
15992 /* C++ base class field. */
15993 dwarf2_add_field (fi, child_die, cu);
15994 }
15995 else if (type_can_define_types (child_die))
15996 dwarf2_add_type_defn (fi, child_die, cu);
15997 else if (child_die->tag == DW_TAG_template_type_param
15998 || child_die->tag == DW_TAG_template_value_param)
15999 {
16000 struct symbol *arg = new_symbol (child_die, NULL, cu);
16001
16002 if (arg != NULL)
16003 template_args->push_back (arg);
16004 }
16005 else if (child_die->tag == DW_TAG_variant)
16006 {
16007 /* In a variant we want to get the discriminant and also add a
16008 field for our sole member child. */
16009 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16010
bde09ab7 16011 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
16012 variant_child != NULL;
16013 variant_child = sibling_die (variant_child))
16014 {
16015 if (variant_child->tag == DW_TAG_member)
16016 {
16017 handle_struct_member_die (variant_child, type, fi,
16018 template_args, cu);
16019 /* Only handle the one. */
16020 break;
16021 }
16022 }
16023
16024 /* We don't handle this but we might as well report it if we see
16025 it. */
16026 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 16027 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
16028 " - DIE at %s [in module %s]"),
16029 sect_offset_str (child_die->sect_off),
16030 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16031
16032 /* The first field was just added, so we can stash the
16033 discriminant there. */
be2daae6 16034 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16035 if (discr == NULL)
be2daae6 16036 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16037 else
be2daae6 16038 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16039 }
16040}
16041
c767944b
DJ
16042/* Finish creating a structure or union type, including filling in
16043 its members and creating a symbol for it. */
16044
16045static void
16046process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16047{
518817b3 16048 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16049 struct die_info *child_die;
c767944b
DJ
16050 struct type *type;
16051
16052 type = get_die_type (die, cu);
16053 if (type == NULL)
16054 type = read_structure_type (die, cu);
16055
2ddeaf8a
TT
16056 /* When reading a DW_TAG_variant_part, we need to notice when we
16057 read the discriminant member, so we can record it later in the
16058 discriminant_info. */
16059 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16060 sect_offset discr_offset;
3e1d3d8c 16061 bool has_template_parameters = false;
2ddeaf8a
TT
16062
16063 if (is_variant_part)
16064 {
16065 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16066 if (discr == NULL)
16067 {
16068 /* Maybe it's a univariant form, an extension we support.
16069 In this case arrange not to check the offset. */
16070 is_variant_part = false;
16071 }
16072 else if (attr_form_is_ref (discr))
16073 {
16074 struct dwarf2_cu *target_cu = cu;
16075 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16076
16077 discr_offset = target_die->sect_off;
16078 }
16079 else
16080 {
b98664d3 16081 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16082 " - DIE at %s [in module %s]"),
16083 sect_offset_str (die->sect_off),
16084 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16085 is_variant_part = false;
16086 }
16087 }
16088
e142c38c 16089 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16090 {
16091 struct field_info fi;
2f4732b0 16092 std::vector<struct symbol *> template_args;
c906108c 16093
639d11d3 16094 child_die = die->child;
c906108c
SS
16095
16096 while (child_die && child_die->tag)
16097 {
2ddeaf8a 16098 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16099
2ddeaf8a 16100 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16101 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16102
c906108c
SS
16103 child_die = sibling_die (child_die);
16104 }
16105
34eaf542 16106 /* Attach template arguments to type. */
2f4732b0 16107 if (!template_args.empty ())
34eaf542 16108 {
3e1d3d8c 16109 has_template_parameters = true;
34eaf542 16110 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16111 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16112 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16113 = XOBNEWVEC (&objfile->objfile_obstack,
16114 struct symbol *,
16115 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16116 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16117 template_args.data (),
34eaf542
TT
16118 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16119 * sizeof (struct symbol *)));
34eaf542
TT
16120 }
16121
c906108c
SS
16122 /* Attach fields and member functions to the type. */
16123 if (fi.nfields)
e7c27a73 16124 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16125 if (!fi.fnfieldlists.empty ())
c906108c 16126 {
e7c27a73 16127 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16128
c5aa993b 16129 /* Get the type which refers to the base class (possibly this
c906108c 16130 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16131 class from the DW_AT_containing_type attribute. This use of
16132 DW_AT_containing_type is a GNU extension. */
c906108c 16133
e142c38c 16134 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16135 {
e7c27a73 16136 struct type *t = die_containing_type (die, cu);
c906108c 16137
ae6ae975 16138 set_type_vptr_basetype (type, t);
c906108c
SS
16139 if (type == t)
16140 {
c906108c
SS
16141 int i;
16142
16143 /* Our own class provides vtbl ptr. */
16144 for (i = TYPE_NFIELDS (t) - 1;
16145 i >= TYPE_N_BASECLASSES (t);
16146 --i)
16147 {
0d5cff50 16148 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16149
1168df01 16150 if (is_vtable_name (fieldname, cu))
c906108c 16151 {
ae6ae975 16152 set_type_vptr_fieldno (type, i);
c906108c
SS
16153 break;
16154 }
16155 }
16156
16157 /* Complain if virtual function table field not found. */
16158 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16159 complaint (_("virtual function table pointer "
3e43a32a 16160 "not found when defining class '%s'"),
e86ca25f 16161 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16162 }
16163 else
16164 {
ae6ae975 16165 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16166 }
16167 }
f6235d4c 16168 else if (cu->producer
61012eef 16169 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16170 {
16171 /* The IBM XLC compiler does not provide direct indication
16172 of the containing type, but the vtable pointer is
16173 always named __vfp. */
16174
16175 int i;
16176
16177 for (i = TYPE_NFIELDS (type) - 1;
16178 i >= TYPE_N_BASECLASSES (type);
16179 --i)
16180 {
16181 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16182 {
ae6ae975
DE
16183 set_type_vptr_fieldno (type, i);
16184 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16185 break;
16186 }
16187 }
16188 }
c906108c 16189 }
98751a41
JK
16190
16191 /* Copy fi.typedef_field_list linked list elements content into the
16192 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16193 if (!fi.typedef_field_list.empty ())
98751a41 16194 {
be2daae6 16195 int count = fi.typedef_field_list.size ();
98751a41 16196
a0d7a4ff 16197 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16198 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16199 = ((struct decl_field *)
be2daae6
TT
16200 TYPE_ALLOC (type,
16201 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16202 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16203
be2daae6
TT
16204 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16205 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16206 }
c767944b 16207
883fd55a
KS
16208 /* Copy fi.nested_types_list linked list elements content into the
16209 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16210 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16211 {
be2daae6 16212 int count = fi.nested_types_list.size ();
883fd55a
KS
16213
16214 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16215 TYPE_NESTED_TYPES_ARRAY (type)
16216 = ((struct decl_field *)
be2daae6
TT
16217 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16218 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16219
be2daae6
TT
16220 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16221 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16222 }
c906108c 16223 }
63d06c5c 16224
bb5ed363 16225 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16226 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16227 cu->rust_unions.push_back (type);
0b92b5bb 16228
90aeadfc
DC
16229 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16230 snapshots) has been known to create a die giving a declaration
16231 for a class that has, as a child, a die giving a definition for a
16232 nested class. So we have to process our children even if the
16233 current die is a declaration. Normally, of course, a declaration
16234 won't have any children at all. */
134d01f1 16235
ca040673
DE
16236 child_die = die->child;
16237
90aeadfc
DC
16238 while (child_die != NULL && child_die->tag)
16239 {
16240 if (child_die->tag == DW_TAG_member
16241 || child_die->tag == DW_TAG_variable
34eaf542
TT
16242 || child_die->tag == DW_TAG_inheritance
16243 || child_die->tag == DW_TAG_template_value_param
16244 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16245 {
90aeadfc 16246 /* Do nothing. */
134d01f1 16247 }
90aeadfc
DC
16248 else
16249 process_die (child_die, cu);
134d01f1 16250
90aeadfc 16251 child_die = sibling_die (child_die);
134d01f1
DJ
16252 }
16253
fa4028e9
JB
16254 /* Do not consider external references. According to the DWARF standard,
16255 these DIEs are identified by the fact that they have no byte_size
16256 attribute, and a declaration attribute. */
16257 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16258 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16259 {
16260 struct symbol *sym = new_symbol (die, type, cu);
16261
16262 if (has_template_parameters)
16263 {
a776957c
TT
16264 struct symtab *symtab;
16265 if (sym != nullptr)
16266 symtab = symbol_symtab (sym);
16267 else if (cu->line_header != nullptr)
16268 {
16269 /* Any related symtab will do. */
16270 symtab
7ba99d21 16271 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16272 }
16273 else
16274 {
16275 symtab = nullptr;
16276 complaint (_("could not find suitable "
16277 "symtab for template parameter"
16278 " - DIE at %s [in module %s]"),
16279 sect_offset_str (die->sect_off),
16280 objfile_name (objfile));
16281 }
16282
16283 if (symtab != nullptr)
16284 {
16285 /* Make sure that the symtab is set on the new symbols.
16286 Even though they don't appear in this symtab directly,
16287 other parts of gdb assume that symbols do, and this is
16288 reasonably true. */
16289 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16290 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16291 }
3e1d3d8c
TT
16292 }
16293 }
134d01f1
DJ
16294}
16295
55426c9d
JB
16296/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16297 update TYPE using some information only available in DIE's children. */
16298
16299static void
16300update_enumeration_type_from_children (struct die_info *die,
16301 struct type *type,
16302 struct dwarf2_cu *cu)
16303{
60f7655a 16304 struct die_info *child_die;
55426c9d
JB
16305 int unsigned_enum = 1;
16306 int flag_enum = 1;
16307 ULONGEST mask = 0;
55426c9d 16308
8268c778 16309 auto_obstack obstack;
55426c9d 16310
60f7655a
DE
16311 for (child_die = die->child;
16312 child_die != NULL && child_die->tag;
16313 child_die = sibling_die (child_die))
55426c9d
JB
16314 {
16315 struct attribute *attr;
16316 LONGEST value;
16317 const gdb_byte *bytes;
16318 struct dwarf2_locexpr_baton *baton;
16319 const char *name;
60f7655a 16320
55426c9d
JB
16321 if (child_die->tag != DW_TAG_enumerator)
16322 continue;
16323
16324 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16325 if (attr == NULL)
16326 continue;
16327
16328 name = dwarf2_name (child_die, cu);
16329 if (name == NULL)
16330 name = "<anonymous enumerator>";
16331
16332 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16333 &value, &bytes, &baton);
16334 if (value < 0)
16335 {
16336 unsigned_enum = 0;
16337 flag_enum = 0;
16338 }
16339 else if ((mask & value) != 0)
16340 flag_enum = 0;
16341 else
16342 mask |= value;
16343
16344 /* If we already know that the enum type is neither unsigned, nor
16345 a flag type, no need to look at the rest of the enumerates. */
16346 if (!unsigned_enum && !flag_enum)
16347 break;
55426c9d
JB
16348 }
16349
16350 if (unsigned_enum)
16351 TYPE_UNSIGNED (type) = 1;
16352 if (flag_enum)
16353 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16354}
16355
134d01f1
DJ
16356/* Given a DW_AT_enumeration_type die, set its type. We do not
16357 complete the type's fields yet, or create any symbols. */
c906108c 16358
f792889a 16359static struct type *
134d01f1 16360read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16361{
518817b3 16362 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16363 struct type *type;
c906108c 16364 struct attribute *attr;
0114d602 16365 const char *name;
134d01f1 16366
348e048f
DE
16367 /* If the definition of this type lives in .debug_types, read that type.
16368 Don't follow DW_AT_specification though, that will take us back up
16369 the chain and we want to go down. */
45e58e77 16370 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16371 if (attr != nullptr)
348e048f 16372 {
ac9ec31b 16373 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16374
ac9ec31b 16375 /* The type's CU may not be the same as CU.
02142a6c 16376 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16377 return set_die_type (die, type, cu);
16378 }
16379
c906108c
SS
16380 type = alloc_type (objfile);
16381
16382 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16383 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16384 if (name != NULL)
e86ca25f 16385 TYPE_NAME (type) = name;
c906108c 16386
0626fc76
TT
16387 attr = dwarf2_attr (die, DW_AT_type, cu);
16388 if (attr != NULL)
16389 {
16390 struct type *underlying_type = die_type (die, cu);
16391
16392 TYPE_TARGET_TYPE (type) = underlying_type;
16393 }
16394
e142c38c 16395 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16396 if (attr != nullptr)
c906108c
SS
16397 {
16398 TYPE_LENGTH (type) = DW_UNSND (attr);
16399 }
16400 else
16401 {
16402 TYPE_LENGTH (type) = 0;
16403 }
16404
2b4424c3
TT
16405 maybe_set_alignment (cu, die, type);
16406
137033e9
JB
16407 /* The enumeration DIE can be incomplete. In Ada, any type can be
16408 declared as private in the package spec, and then defined only
16409 inside the package body. Such types are known as Taft Amendment
16410 Types. When another package uses such a type, an incomplete DIE
16411 may be generated by the compiler. */
02eb380e 16412 if (die_is_declaration (die, cu))
876cecd0 16413 TYPE_STUB (type) = 1;
02eb380e 16414
0626fc76
TT
16415 /* Finish the creation of this type by using the enum's children.
16416 We must call this even when the underlying type has been provided
16417 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16418 update_enumeration_type_from_children (die, type, cu);
16419
0626fc76
TT
16420 /* If this type has an underlying type that is not a stub, then we
16421 may use its attributes. We always use the "unsigned" attribute
16422 in this situation, because ordinarily we guess whether the type
16423 is unsigned -- but the guess can be wrong and the underlying type
16424 can tell us the reality. However, we defer to a local size
16425 attribute if one exists, because this lets the compiler override
16426 the underlying type if needed. */
16427 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16428 {
16429 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16430 if (TYPE_LENGTH (type) == 0)
16431 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16432 if (TYPE_RAW_ALIGN (type) == 0
16433 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16434 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16435 }
16436
3d567982
TT
16437 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16438
f792889a 16439 return set_die_type (die, type, cu);
134d01f1
DJ
16440}
16441
16442/* Given a pointer to a die which begins an enumeration, process all
16443 the dies that define the members of the enumeration, and create the
16444 symbol for the enumeration type.
16445
16446 NOTE: We reverse the order of the element list. */
16447
16448static void
16449process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16450{
f792889a 16451 struct type *this_type;
134d01f1 16452
f792889a
DJ
16453 this_type = get_die_type (die, cu);
16454 if (this_type == NULL)
16455 this_type = read_enumeration_type (die, cu);
9dc481d3 16456
639d11d3 16457 if (die->child != NULL)
c906108c 16458 {
9dc481d3
DE
16459 struct die_info *child_die;
16460 struct symbol *sym;
16461 struct field *fields = NULL;
16462 int num_fields = 0;
15d034d0 16463 const char *name;
9dc481d3 16464
639d11d3 16465 child_die = die->child;
c906108c
SS
16466 while (child_die && child_die->tag)
16467 {
16468 if (child_die->tag != DW_TAG_enumerator)
16469 {
e7c27a73 16470 process_die (child_die, cu);
c906108c
SS
16471 }
16472 else
16473 {
39cbfefa
DJ
16474 name = dwarf2_name (child_die, cu);
16475 if (name)
c906108c 16476 {
f792889a 16477 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16478
16479 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16480 {
16481 fields = (struct field *)
16482 xrealloc (fields,
16483 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16484 * sizeof (struct field));
c906108c
SS
16485 }
16486
987012b8 16487 FIELD_NAME (fields[num_fields]) = sym->linkage_name ();
c906108c 16488 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16489 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16490 FIELD_BITSIZE (fields[num_fields]) = 0;
16491
16492 num_fields++;
16493 }
16494 }
16495
16496 child_die = sibling_die (child_die);
16497 }
16498
16499 if (num_fields)
16500 {
f792889a
DJ
16501 TYPE_NFIELDS (this_type) = num_fields;
16502 TYPE_FIELDS (this_type) = (struct field *)
16503 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16504 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16505 sizeof (struct field) * num_fields);
b8c9b27d 16506 xfree (fields);
c906108c 16507 }
c906108c 16508 }
134d01f1 16509
6c83ed52
TT
16510 /* If we are reading an enum from a .debug_types unit, and the enum
16511 is a declaration, and the enum is not the signatured type in the
16512 unit, then we do not want to add a symbol for it. Adding a
16513 symbol would in some cases obscure the true definition of the
16514 enum, giving users an incomplete type when the definition is
16515 actually available. Note that we do not want to do this for all
16516 enums which are just declarations, because C++0x allows forward
16517 enum declarations. */
3019eac3 16518 if (cu->per_cu->is_debug_types
6c83ed52
TT
16519 && die_is_declaration (die, cu))
16520 {
52dc124a 16521 struct signatured_type *sig_type;
6c83ed52 16522
c0f78cd4 16523 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16524 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16525 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16526 return;
16527 }
16528
f792889a 16529 new_symbol (die, this_type, cu);
c906108c
SS
16530}
16531
16532/* Extract all information from a DW_TAG_array_type DIE and put it in
16533 the DIE's type field. For now, this only handles one dimensional
16534 arrays. */
16535
f792889a 16536static struct type *
e7c27a73 16537read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16538{
518817b3 16539 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16540 struct die_info *child_die;
7e314c57 16541 struct type *type;
c906108c 16542 struct type *element_type, *range_type, *index_type;
c906108c 16543 struct attribute *attr;
15d034d0 16544 const char *name;
a405673c 16545 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16546 unsigned int bit_stride = 0;
c906108c 16547
e7c27a73 16548 element_type = die_type (die, cu);
c906108c 16549
7e314c57
JK
16550 /* The die_type call above may have already set the type for this DIE. */
16551 type = get_die_type (die, cu);
16552 if (type)
16553 return type;
16554
dc53a7ad
JB
16555 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16556 if (attr != NULL)
a405673c
JB
16557 {
16558 int stride_ok;
9a49df9d
AB
16559 struct type *prop_type
16560 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16561
16562 byte_stride_prop
16563 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16564 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16565 prop_type);
a405673c
JB
16566 if (!stride_ok)
16567 {
b98664d3 16568 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16569 " - DIE at %s [in module %s]"),
16570 sect_offset_str (die->sect_off),
518817b3 16571 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16572 /* Ignore this attribute. We will likely not be able to print
16573 arrays of this type correctly, but there is little we can do
16574 to help if we cannot read the attribute's value. */
16575 byte_stride_prop = NULL;
16576 }
16577 }
dc53a7ad
JB
16578
16579 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16580 if (attr != NULL)
16581 bit_stride = DW_UNSND (attr);
16582
c906108c
SS
16583 /* Irix 6.2 native cc creates array types without children for
16584 arrays with unspecified length. */
639d11d3 16585 if (die->child == NULL)
c906108c 16586 {
46bf5051 16587 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16588 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16589 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16590 byte_stride_prop, bit_stride);
f792889a 16591 return set_die_type (die, type, cu);
c906108c
SS
16592 }
16593
791afaa2 16594 std::vector<struct type *> range_types;
639d11d3 16595 child_die = die->child;
c906108c
SS
16596 while (child_die && child_die->tag)
16597 {
16598 if (child_die->tag == DW_TAG_subrange_type)
16599 {
f792889a 16600 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16601
f792889a 16602 if (child_type != NULL)
a02abb62 16603 {
0963b4bd
MS
16604 /* The range type was succesfully read. Save it for the
16605 array type creation. */
791afaa2 16606 range_types.push_back (child_type);
a02abb62 16607 }
c906108c
SS
16608 }
16609 child_die = sibling_die (child_die);
16610 }
16611
16612 /* Dwarf2 dimensions are output from left to right, create the
16613 necessary array types in backwards order. */
7ca2d3a3 16614
c906108c 16615 type = element_type;
7ca2d3a3
DL
16616
16617 if (read_array_order (die, cu) == DW_ORD_col_major)
16618 {
16619 int i = 0;
9a619af0 16620
791afaa2 16621 while (i < range_types.size ())
dc53a7ad 16622 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16623 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16624 }
16625 else
16626 {
791afaa2 16627 size_t ndim = range_types.size ();
7ca2d3a3 16628 while (ndim-- > 0)
dc53a7ad 16629 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16630 byte_stride_prop, bit_stride);
7ca2d3a3 16631 }
c906108c 16632
f5f8a009
EZ
16633 /* Understand Dwarf2 support for vector types (like they occur on
16634 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16635 array type. This is not part of the Dwarf2/3 standard yet, but a
16636 custom vendor extension. The main difference between a regular
16637 array and the vector variant is that vectors are passed by value
16638 to functions. */
e142c38c 16639 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16640 if (attr != nullptr)
ea37ba09 16641 make_vector_type (type);
f5f8a009 16642
dbc98a8b
KW
16643 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16644 implementation may choose to implement triple vectors using this
16645 attribute. */
16646 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16647 if (attr != nullptr)
dbc98a8b
KW
16648 {
16649 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16650 TYPE_LENGTH (type) = DW_UNSND (attr);
16651 else
b98664d3 16652 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16653 "than the total size of elements"));
dbc98a8b
KW
16654 }
16655
39cbfefa
DJ
16656 name = dwarf2_name (die, cu);
16657 if (name)
16658 TYPE_NAME (type) = name;
6e70227d 16659
2b4424c3
TT
16660 maybe_set_alignment (cu, die, type);
16661
0963b4bd 16662 /* Install the type in the die. */
7e314c57
JK
16663 set_die_type (die, type, cu);
16664
16665 /* set_die_type should be already done. */
b4ba55a1
JB
16666 set_descriptive_type (type, die, cu);
16667
7e314c57 16668 return type;
c906108c
SS
16669}
16670
7ca2d3a3 16671static enum dwarf_array_dim_ordering
6e70227d 16672read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16673{
16674 struct attribute *attr;
16675
16676 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16677
435d3d88 16678 if (attr != nullptr)
aead7601 16679 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16680
0963b4bd
MS
16681 /* GNU F77 is a special case, as at 08/2004 array type info is the
16682 opposite order to the dwarf2 specification, but data is still
16683 laid out as per normal fortran.
7ca2d3a3 16684
0963b4bd
MS
16685 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16686 version checking. */
7ca2d3a3 16687
905e0470
PM
16688 if (cu->language == language_fortran
16689 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16690 {
16691 return DW_ORD_row_major;
16692 }
16693
6e70227d 16694 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16695 {
16696 case array_column_major:
16697 return DW_ORD_col_major;
16698 case array_row_major:
16699 default:
16700 return DW_ORD_row_major;
16701 };
16702}
16703
72019c9c 16704/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16705 the DIE's type field. */
72019c9c 16706
f792889a 16707static struct type *
72019c9c
GM
16708read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16709{
7e314c57
JK
16710 struct type *domain_type, *set_type;
16711 struct attribute *attr;
f792889a 16712
7e314c57
JK
16713 domain_type = die_type (die, cu);
16714
16715 /* The die_type call above may have already set the type for this DIE. */
16716 set_type = get_die_type (die, cu);
16717 if (set_type)
16718 return set_type;
16719
16720 set_type = create_set_type (NULL, domain_type);
16721
16722 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16723 if (attr != nullptr)
d09039dd 16724 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16725
2b4424c3
TT
16726 maybe_set_alignment (cu, die, set_type);
16727
f792889a 16728 return set_die_type (die, set_type, cu);
72019c9c 16729}
7ca2d3a3 16730
0971de02
TT
16731/* A helper for read_common_block that creates a locexpr baton.
16732 SYM is the symbol which we are marking as computed.
16733 COMMON_DIE is the DIE for the common block.
16734 COMMON_LOC is the location expression attribute for the common
16735 block itself.
16736 MEMBER_LOC is the location expression attribute for the particular
16737 member of the common block that we are processing.
16738 CU is the CU from which the above come. */
16739
16740static void
16741mark_common_block_symbol_computed (struct symbol *sym,
16742 struct die_info *common_die,
16743 struct attribute *common_loc,
16744 struct attribute *member_loc,
16745 struct dwarf2_cu *cu)
16746{
518817b3
SM
16747 struct dwarf2_per_objfile *dwarf2_per_objfile
16748 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16749 struct objfile *objfile = dwarf2_per_objfile->objfile;
16750 struct dwarf2_locexpr_baton *baton;
16751 gdb_byte *ptr;
16752 unsigned int cu_off;
16753 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16754 LONGEST offset = 0;
16755
16756 gdb_assert (common_loc && member_loc);
16757 gdb_assert (attr_form_is_block (common_loc));
16758 gdb_assert (attr_form_is_block (member_loc)
16759 || attr_form_is_constant (member_loc));
16760
8d749320 16761 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16762 baton->per_cu = cu->per_cu;
16763 gdb_assert (baton->per_cu);
16764
16765 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16766
16767 if (attr_form_is_constant (member_loc))
16768 {
16769 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16770 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16771 }
16772 else
16773 baton->size += DW_BLOCK (member_loc)->size;
16774
224c3ddb 16775 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16776 baton->data = ptr;
16777
16778 *ptr++ = DW_OP_call4;
9c541725 16779 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16780 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16781 ptr += 4;
16782
16783 if (attr_form_is_constant (member_loc))
16784 {
16785 *ptr++ = DW_OP_addr;
16786 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16787 ptr += cu->header.addr_size;
16788 }
16789 else
16790 {
16791 /* We have to copy the data here, because DW_OP_call4 will only
16792 use a DW_AT_location attribute. */
16793 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16794 ptr += DW_BLOCK (member_loc)->size;
16795 }
16796
16797 *ptr++ = DW_OP_plus;
16798 gdb_assert (ptr - baton->data == baton->size);
16799
0971de02 16800 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16801 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16802}
16803
4357ac6c
TT
16804/* Create appropriate locally-scoped variables for all the
16805 DW_TAG_common_block entries. Also create a struct common_block
16806 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16807 is used to separate the common blocks name namespace from regular
4357ac6c 16808 variable names. */
c906108c
SS
16809
16810static void
e7c27a73 16811read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16812{
0971de02
TT
16813 struct attribute *attr;
16814
16815 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16816 if (attr != nullptr)
0971de02
TT
16817 {
16818 /* Support the .debug_loc offsets. */
16819 if (attr_form_is_block (attr))
16820 {
16821 /* Ok. */
16822 }
16823 else if (attr_form_is_section_offset (attr))
16824 {
16825 dwarf2_complex_location_expr_complaint ();
16826 attr = NULL;
16827 }
16828 else
16829 {
16830 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16831 "common block member");
16832 attr = NULL;
16833 }
16834 }
16835
639d11d3 16836 if (die->child != NULL)
c906108c 16837 {
518817b3 16838 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16839 struct die_info *child_die;
16840 size_t n_entries = 0, size;
16841 struct common_block *common_block;
16842 struct symbol *sym;
74ac6d43 16843
4357ac6c
TT
16844 for (child_die = die->child;
16845 child_die && child_die->tag;
16846 child_die = sibling_die (child_die))
16847 ++n_entries;
16848
16849 size = (sizeof (struct common_block)
16850 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16851 common_block
16852 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16853 size);
4357ac6c
TT
16854 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16855 common_block->n_entries = 0;
16856
16857 for (child_die = die->child;
16858 child_die && child_die->tag;
16859 child_die = sibling_die (child_die))
16860 {
16861 /* Create the symbol in the DW_TAG_common_block block in the current
16862 symbol scope. */
e7c27a73 16863 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16864 if (sym != NULL)
16865 {
16866 struct attribute *member_loc;
16867
16868 common_block->contents[common_block->n_entries++] = sym;
16869
16870 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16871 cu);
16872 if (member_loc)
16873 {
16874 /* GDB has handled this for a long time, but it is
16875 not specified by DWARF. It seems to have been
16876 emitted by gfortran at least as recently as:
16877 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16878 complaint (_("Variable in common block has "
0971de02 16879 "DW_AT_data_member_location "
9d8780f0
SM
16880 "- DIE at %s [in module %s]"),
16881 sect_offset_str (child_die->sect_off),
518817b3 16882 objfile_name (objfile));
0971de02
TT
16883
16884 if (attr_form_is_section_offset (member_loc))
16885 dwarf2_complex_location_expr_complaint ();
16886 else if (attr_form_is_constant (member_loc)
16887 || attr_form_is_block (member_loc))
16888 {
435d3d88 16889 if (attr != nullptr)
0971de02
TT
16890 mark_common_block_symbol_computed (sym, die, attr,
16891 member_loc, cu);
16892 }
16893 else
16894 dwarf2_complex_location_expr_complaint ();
16895 }
16896 }
c906108c 16897 }
4357ac6c
TT
16898
16899 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16900 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16901 }
16902}
16903
0114d602 16904/* Create a type for a C++ namespace. */
d9fa45fe 16905
0114d602
DJ
16906static struct type *
16907read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16908{
518817b3 16909 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16910 const char *previous_prefix, *name;
9219021c 16911 int is_anonymous;
0114d602
DJ
16912 struct type *type;
16913
16914 /* For extensions, reuse the type of the original namespace. */
16915 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16916 {
16917 struct die_info *ext_die;
16918 struct dwarf2_cu *ext_cu = cu;
9a619af0 16919
0114d602
DJ
16920 ext_die = dwarf2_extension (die, &ext_cu);
16921 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16922
16923 /* EXT_CU may not be the same as CU.
02142a6c 16924 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16925 return set_die_type (die, type, cu);
16926 }
9219021c 16927
e142c38c 16928 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16929
16930 /* Now build the name of the current namespace. */
16931
0114d602
DJ
16932 previous_prefix = determine_prefix (die, cu);
16933 if (previous_prefix[0] != '\0')
16934 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16935 previous_prefix, name, 0, cu);
0114d602
DJ
16936
16937 /* Create the type. */
19f392bc 16938 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16939
60531b24 16940 return set_die_type (die, type, cu);
0114d602
DJ
16941}
16942
22cee43f 16943/* Read a namespace scope. */
0114d602
DJ
16944
16945static void
16946read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16947{
518817b3 16948 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16949 int is_anonymous;
9219021c 16950
5c4e30ca
DC
16951 /* Add a symbol associated to this if we haven't seen the namespace
16952 before. Also, add a using directive if it's an anonymous
16953 namespace. */
9219021c 16954
f2f0e013 16955 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16956 {
16957 struct type *type;
16958
0114d602 16959 type = read_type_die (die, cu);
e7c27a73 16960 new_symbol (die, type, cu);
5c4e30ca 16961
e8e80198 16962 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16963 if (is_anonymous)
0114d602
DJ
16964 {
16965 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16966
eb1e02fd 16967 std::vector<const char *> excludes;
804d2729 16968 add_using_directive (using_directives (cu),
22cee43f 16969 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16970 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16971 }
5c4e30ca 16972 }
9219021c 16973
639d11d3 16974 if (die->child != NULL)
d9fa45fe 16975 {
639d11d3 16976 struct die_info *child_die = die->child;
6e70227d 16977
d9fa45fe
DC
16978 while (child_die && child_die->tag)
16979 {
e7c27a73 16980 process_die (child_die, cu);
d9fa45fe
DC
16981 child_die = sibling_die (child_die);
16982 }
16983 }
38d518c9
EZ
16984}
16985
f55ee35c
JK
16986/* Read a Fortran module as type. This DIE can be only a declaration used for
16987 imported module. Still we need that type as local Fortran "use ... only"
16988 declaration imports depend on the created type in determine_prefix. */
16989
16990static struct type *
16991read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16992{
518817b3 16993 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16994 const char *module_name;
f55ee35c
JK
16995 struct type *type;
16996
16997 module_name = dwarf2_name (die, cu);
19f392bc 16998 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16999
f55ee35c
JK
17000 return set_die_type (die, type, cu);
17001}
17002
5d7cb8df
JK
17003/* Read a Fortran module. */
17004
17005static void
17006read_module (struct die_info *die, struct dwarf2_cu *cu)
17007{
17008 struct die_info *child_die = die->child;
530e8392
KB
17009 struct type *type;
17010
17011 type = read_type_die (die, cu);
17012 new_symbol (die, type, cu);
5d7cb8df 17013
5d7cb8df
JK
17014 while (child_die && child_die->tag)
17015 {
17016 process_die (child_die, cu);
17017 child_die = sibling_die (child_die);
17018 }
17019}
17020
38d518c9
EZ
17021/* Return the name of the namespace represented by DIE. Set
17022 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17023 namespace. */
17024
17025static const char *
e142c38c 17026namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17027{
17028 struct die_info *current_die;
17029 const char *name = NULL;
17030
17031 /* Loop through the extensions until we find a name. */
17032
17033 for (current_die = die;
17034 current_die != NULL;
f2f0e013 17035 current_die = dwarf2_extension (die, &cu))
38d518c9 17036 {
96553a0c
DE
17037 /* We don't use dwarf2_name here so that we can detect the absence
17038 of a name -> anonymous namespace. */
7d45c7c3 17039 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17040
38d518c9
EZ
17041 if (name != NULL)
17042 break;
17043 }
17044
17045 /* Is it an anonymous namespace? */
17046
17047 *is_anonymous = (name == NULL);
17048 if (*is_anonymous)
2b1dbab0 17049 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17050
17051 return name;
d9fa45fe
DC
17052}
17053
c906108c
SS
17054/* Extract all information from a DW_TAG_pointer_type DIE and add to
17055 the user defined type vector. */
17056
f792889a 17057static struct type *
e7c27a73 17058read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17059{
518817b3
SM
17060 struct gdbarch *gdbarch
17061 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17062 struct comp_unit_head *cu_header = &cu->header;
c906108c 17063 struct type *type;
8b2dbe47
KB
17064 struct attribute *attr_byte_size;
17065 struct attribute *attr_address_class;
17066 int byte_size, addr_class;
7e314c57
JK
17067 struct type *target_type;
17068
17069 target_type = die_type (die, cu);
c906108c 17070
7e314c57
JK
17071 /* The die_type call above may have already set the type for this DIE. */
17072 type = get_die_type (die, cu);
17073 if (type)
17074 return type;
17075
17076 type = lookup_pointer_type (target_type);
8b2dbe47 17077
e142c38c 17078 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17079 if (attr_byte_size)
17080 byte_size = DW_UNSND (attr_byte_size);
c906108c 17081 else
8b2dbe47
KB
17082 byte_size = cu_header->addr_size;
17083
e142c38c 17084 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17085 if (attr_address_class)
17086 addr_class = DW_UNSND (attr_address_class);
17087 else
17088 addr_class = DW_ADDR_none;
17089
2b4424c3
TT
17090 ULONGEST alignment = get_alignment (cu, die);
17091
17092 /* If the pointer size, alignment, or address class is different
17093 than the default, create a type variant marked as such and set
17094 the length accordingly. */
17095 if (TYPE_LENGTH (type) != byte_size
17096 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17097 && alignment != TYPE_RAW_ALIGN (type))
17098 || addr_class != DW_ADDR_none)
c906108c 17099 {
5e2b427d 17100 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17101 {
17102 int type_flags;
17103
849957d9 17104 type_flags = gdbarch_address_class_type_flags
5e2b427d 17105 (gdbarch, byte_size, addr_class);
876cecd0
TT
17106 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17107 == 0);
8b2dbe47
KB
17108 type = make_type_with_address_space (type, type_flags);
17109 }
17110 else if (TYPE_LENGTH (type) != byte_size)
17111 {
b98664d3 17112 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17113 }
2b4424c3
TT
17114 else if (TYPE_RAW_ALIGN (type) != alignment)
17115 {
b98664d3 17116 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17117 " - DIE at %s [in module %s]"),
17118 sect_offset_str (die->sect_off),
17119 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17120 }
6e70227d 17121 else
9a619af0
MS
17122 {
17123 /* Should we also complain about unhandled address classes? */
17124 }
c906108c 17125 }
8b2dbe47
KB
17126
17127 TYPE_LENGTH (type) = byte_size;
2b4424c3 17128 set_type_align (type, alignment);
f792889a 17129 return set_die_type (die, type, cu);
c906108c
SS
17130}
17131
17132/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17133 the user defined type vector. */
17134
f792889a 17135static struct type *
e7c27a73 17136read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17137{
17138 struct type *type;
17139 struct type *to_type;
17140 struct type *domain;
17141
e7c27a73
DJ
17142 to_type = die_type (die, cu);
17143 domain = die_containing_type (die, cu);
0d5de010 17144
7e314c57
JK
17145 /* The calls above may have already set the type for this DIE. */
17146 type = get_die_type (die, cu);
17147 if (type)
17148 return type;
17149
0d5de010
DJ
17150 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17151 type = lookup_methodptr_type (to_type);
7078baeb
TT
17152 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17153 {
518817b3
SM
17154 struct type *new_type
17155 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17156
17157 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17158 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17159 TYPE_VARARGS (to_type));
17160 type = lookup_methodptr_type (new_type);
17161 }
0d5de010
DJ
17162 else
17163 type = lookup_memberptr_type (to_type, domain);
c906108c 17164
f792889a 17165 return set_die_type (die, type, cu);
c906108c
SS
17166}
17167
4297a3f0 17168/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17169 the user defined type vector. */
17170
f792889a 17171static struct type *
4297a3f0
AV
17172read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17173 enum type_code refcode)
c906108c 17174{
e7c27a73 17175 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17176 struct type *type, *target_type;
c906108c
SS
17177 struct attribute *attr;
17178
4297a3f0
AV
17179 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17180
7e314c57
JK
17181 target_type = die_type (die, cu);
17182
17183 /* The die_type call above may have already set the type for this DIE. */
17184 type = get_die_type (die, cu);
17185 if (type)
17186 return type;
17187
4297a3f0 17188 type = lookup_reference_type (target_type, refcode);
e142c38c 17189 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17190 if (attr != nullptr)
c906108c
SS
17191 {
17192 TYPE_LENGTH (type) = DW_UNSND (attr);
17193 }
17194 else
17195 {
107d2387 17196 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17197 }
2b4424c3 17198 maybe_set_alignment (cu, die, type);
f792889a 17199 return set_die_type (die, type, cu);
c906108c
SS
17200}
17201
cf363f18
MW
17202/* Add the given cv-qualifiers to the element type of the array. GCC
17203 outputs DWARF type qualifiers that apply to an array, not the
17204 element type. But GDB relies on the array element type to carry
17205 the cv-qualifiers. This mimics section 6.7.3 of the C99
17206 specification. */
17207
17208static struct type *
17209add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17210 struct type *base_type, int cnst, int voltl)
17211{
17212 struct type *el_type, *inner_array;
17213
17214 base_type = copy_type (base_type);
17215 inner_array = base_type;
17216
17217 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17218 {
17219 TYPE_TARGET_TYPE (inner_array) =
17220 copy_type (TYPE_TARGET_TYPE (inner_array));
17221 inner_array = TYPE_TARGET_TYPE (inner_array);
17222 }
17223
17224 el_type = TYPE_TARGET_TYPE (inner_array);
17225 cnst |= TYPE_CONST (el_type);
17226 voltl |= TYPE_VOLATILE (el_type);
17227 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17228
17229 return set_die_type (die, base_type, cu);
17230}
17231
f792889a 17232static struct type *
e7c27a73 17233read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17234{
f792889a 17235 struct type *base_type, *cv_type;
c906108c 17236
e7c27a73 17237 base_type = die_type (die, cu);
7e314c57
JK
17238
17239 /* The die_type call above may have already set the type for this DIE. */
17240 cv_type = get_die_type (die, cu);
17241 if (cv_type)
17242 return cv_type;
17243
2f608a3a
KW
17244 /* In case the const qualifier is applied to an array type, the element type
17245 is so qualified, not the array type (section 6.7.3 of C99). */
17246 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17247 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17248
f792889a
DJ
17249 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17250 return set_die_type (die, cv_type, cu);
c906108c
SS
17251}
17252
f792889a 17253static struct type *
e7c27a73 17254read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17255{
f792889a 17256 struct type *base_type, *cv_type;
c906108c 17257
e7c27a73 17258 base_type = die_type (die, cu);
7e314c57
JK
17259
17260 /* The die_type call above may have already set the type for this DIE. */
17261 cv_type = get_die_type (die, cu);
17262 if (cv_type)
17263 return cv_type;
17264
cf363f18
MW
17265 /* In case the volatile qualifier is applied to an array type, the
17266 element type is so qualified, not the array type (section 6.7.3
17267 of C99). */
17268 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17269 return add_array_cv_type (die, cu, base_type, 0, 1);
17270
f792889a
DJ
17271 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17272 return set_die_type (die, cv_type, cu);
c906108c
SS
17273}
17274
06d66ee9
TT
17275/* Handle DW_TAG_restrict_type. */
17276
17277static struct type *
17278read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17279{
17280 struct type *base_type, *cv_type;
17281
17282 base_type = die_type (die, cu);
17283
17284 /* The die_type call above may have already set the type for this DIE. */
17285 cv_type = get_die_type (die, cu);
17286 if (cv_type)
17287 return cv_type;
17288
17289 cv_type = make_restrict_type (base_type);
17290 return set_die_type (die, cv_type, cu);
17291}
17292
a2c2acaf
MW
17293/* Handle DW_TAG_atomic_type. */
17294
17295static struct type *
17296read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17297{
17298 struct type *base_type, *cv_type;
17299
17300 base_type = die_type (die, cu);
17301
17302 /* The die_type call above may have already set the type for this DIE. */
17303 cv_type = get_die_type (die, cu);
17304 if (cv_type)
17305 return cv_type;
17306
17307 cv_type = make_atomic_type (base_type);
17308 return set_die_type (die, cv_type, cu);
17309}
17310
c906108c
SS
17311/* Extract all information from a DW_TAG_string_type DIE and add to
17312 the user defined type vector. It isn't really a user defined type,
17313 but it behaves like one, with other DIE's using an AT_user_def_type
17314 attribute to reference it. */
17315
f792889a 17316static struct type *
e7c27a73 17317read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17318{
518817b3 17319 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17320 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17321 struct type *type, *range_type, *index_type, *char_type;
17322 struct attribute *attr;
17323 unsigned int length;
17324
e142c38c 17325 attr = dwarf2_attr (die, DW_AT_string_length, cu);
435d3d88 17326 if (attr != nullptr)
c906108c
SS
17327 {
17328 length = DW_UNSND (attr);
17329 }
17330 else
17331 {
0963b4bd 17332 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17333 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17334 if (attr != nullptr)
b21b22e0
PS
17335 {
17336 length = DW_UNSND (attr);
17337 }
17338 else
17339 {
17340 length = 1;
17341 }
c906108c 17342 }
6ccb9162 17343
46bf5051 17344 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17345 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17346 char_type = language_string_char_type (cu->language_defn, gdbarch);
17347 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17348
f792889a 17349 return set_die_type (die, type, cu);
c906108c
SS
17350}
17351
4d804846
JB
17352/* Assuming that DIE corresponds to a function, returns nonzero
17353 if the function is prototyped. */
17354
17355static int
17356prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17357{
17358 struct attribute *attr;
17359
17360 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17361 if (attr && (DW_UNSND (attr) != 0))
17362 return 1;
17363
17364 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17365 is only meaningful for C, but the concept also extends to other
4d804846
JB
17366 languages that allow unprototyped functions (Eg: Objective C).
17367 For all other languages, assume that functions are always
17368 prototyped. */
17369 if (cu->language != language_c
17370 && cu->language != language_objc
17371 && cu->language != language_opencl)
17372 return 1;
17373
17374 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17375 prototyped and unprototyped functions; default to prototyped,
17376 since that is more common in modern code (and RealView warns
17377 about unprototyped functions). */
17378 if (producer_is_realview (cu->producer))
17379 return 1;
17380
17381 return 0;
17382}
17383
c906108c
SS
17384/* Handle DIES due to C code like:
17385
17386 struct foo
c5aa993b
JM
17387 {
17388 int (*funcp)(int a, long l);
17389 int b;
17390 };
c906108c 17391
0963b4bd 17392 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17393
f792889a 17394static struct type *
e7c27a73 17395read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17396{
518817b3 17397 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17398 struct type *type; /* Type that this function returns. */
17399 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17400 struct attribute *attr;
17401
e7c27a73 17402 type = die_type (die, cu);
7e314c57
JK
17403
17404 /* The die_type call above may have already set the type for this DIE. */
17405 ftype = get_die_type (die, cu);
17406 if (ftype)
17407 return ftype;
17408
0c8b41f1 17409 ftype = lookup_function_type (type);
c906108c 17410
4d804846 17411 if (prototyped_function_p (die, cu))
a6c727b2 17412 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17413
c055b101
CV
17414 /* Store the calling convention in the type if it's available in
17415 the subroutine die. Otherwise set the calling convention to
17416 the default value DW_CC_normal. */
17417 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
435d3d88 17418 if (attr != nullptr)
54fcddd0
UW
17419 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17420 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17421 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17422 else
17423 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17424
743649fd
MW
17425 /* Record whether the function returns normally to its caller or not
17426 if the DWARF producer set that information. */
17427 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17428 if (attr && (DW_UNSND (attr) != 0))
17429 TYPE_NO_RETURN (ftype) = 1;
17430
76c10ea2
GM
17431 /* We need to add the subroutine type to the die immediately so
17432 we don't infinitely recurse when dealing with parameters
0963b4bd 17433 declared as the same subroutine type. */
76c10ea2 17434 set_die_type (die, ftype, cu);
6e70227d 17435
639d11d3 17436 if (die->child != NULL)
c906108c 17437 {
bb5ed363 17438 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17439 struct die_info *child_die;
8072405b 17440 int nparams, iparams;
c906108c
SS
17441
17442 /* Count the number of parameters.
17443 FIXME: GDB currently ignores vararg functions, but knows about
17444 vararg member functions. */
8072405b 17445 nparams = 0;
639d11d3 17446 child_die = die->child;
c906108c
SS
17447 while (child_die && child_die->tag)
17448 {
17449 if (child_die->tag == DW_TAG_formal_parameter)
17450 nparams++;
17451 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17452 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17453 child_die = sibling_die (child_die);
17454 }
17455
17456 /* Allocate storage for parameters and fill them in. */
17457 TYPE_NFIELDS (ftype) = nparams;
17458 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17459 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17460
8072405b
JK
17461 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17462 even if we error out during the parameters reading below. */
17463 for (iparams = 0; iparams < nparams; iparams++)
17464 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17465
17466 iparams = 0;
639d11d3 17467 child_die = die->child;
c906108c
SS
17468 while (child_die && child_die->tag)
17469 {
17470 if (child_die->tag == DW_TAG_formal_parameter)
17471 {
3ce3b1ba
PA
17472 struct type *arg_type;
17473
17474 /* DWARF version 2 has no clean way to discern C++
17475 static and non-static member functions. G++ helps
17476 GDB by marking the first parameter for non-static
17477 member functions (which is the this pointer) as
17478 artificial. We pass this information to
17479 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17480
17481 DWARF version 3 added DW_AT_object_pointer, which GCC
17482 4.5 does not yet generate. */
e142c38c 17483 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17484 if (attr != nullptr)
c906108c
SS
17485 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17486 else
9c37b5ae 17487 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17488 arg_type = die_type (child_die, cu);
17489
17490 /* RealView does not mark THIS as const, which the testsuite
17491 expects. GCC marks THIS as const in method definitions,
17492 but not in the class specifications (GCC PR 43053). */
17493 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17494 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17495 {
17496 int is_this = 0;
17497 struct dwarf2_cu *arg_cu = cu;
17498 const char *name = dwarf2_name (child_die, cu);
17499
17500 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17501 if (attr != nullptr)
3ce3b1ba
PA
17502 {
17503 /* If the compiler emits this, use it. */
17504 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17505 is_this = 1;
17506 }
17507 else if (name && strcmp (name, "this") == 0)
17508 /* Function definitions will have the argument names. */
17509 is_this = 1;
17510 else if (name == NULL && iparams == 0)
17511 /* Declarations may not have the names, so like
17512 elsewhere in GDB, assume an artificial first
17513 argument is "this". */
17514 is_this = 1;
17515
17516 if (is_this)
17517 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17518 arg_type, 0);
17519 }
17520
17521 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17522 iparams++;
17523 }
17524 child_die = sibling_die (child_die);
17525 }
17526 }
17527
76c10ea2 17528 return ftype;
c906108c
SS
17529}
17530
f792889a 17531static struct type *
e7c27a73 17532read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17533{
518817b3 17534 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17535 const char *name = NULL;
3c8e0968 17536 struct type *this_type, *target_type;
c906108c 17537
94af9270 17538 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17539 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17540 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17541 set_die_type (die, this_type, cu);
3c8e0968
DE
17542 target_type = die_type (die, cu);
17543 if (target_type != this_type)
17544 TYPE_TARGET_TYPE (this_type) = target_type;
17545 else
17546 {
17547 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17548 spec and cause infinite loops in GDB. */
b98664d3 17549 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17550 "- DIE at %s [in module %s]"),
17551 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17552 TYPE_TARGET_TYPE (this_type) = NULL;
17553 }
f792889a 17554 return this_type;
c906108c
SS
17555}
17556
9b790ce7
UW
17557/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17558 (which may be different from NAME) to the architecture back-end to allow
17559 it to guess the correct format if necessary. */
17560
17561static struct type *
17562dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17563 const char *name_hint)
17564{
17565 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17566 const struct floatformat **format;
17567 struct type *type;
17568
17569 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17570 if (format)
17571 type = init_float_type (objfile, bits, name, format);
17572 else
77b7c781 17573 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17574
17575 return type;
17576}
17577
eb77c9df
AB
17578/* Allocate an integer type of size BITS and name NAME. */
17579
17580static struct type *
17581dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17582 int bits, int unsigned_p, const char *name)
17583{
17584 struct type *type;
17585
17586 /* Versions of Intel's C Compiler generate an integer type called "void"
17587 instead of using DW_TAG_unspecified_type. This has been seen on
17588 at least versions 14, 17, and 18. */
35ee2dc2
AB
17589 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17590 && strcmp (name, "void") == 0)
eb77c9df
AB
17591 type = objfile_type (objfile)->builtin_void;
17592 else
17593 type = init_integer_type (objfile, bits, unsigned_p, name);
17594
17595 return type;
17596}
17597
8bdc1658
AB
17598/* Initialise and return a floating point type of size BITS suitable for
17599 use as a component of a complex number. The NAME_HINT is passed through
17600 when initialising the floating point type and is the name of the complex
17601 type.
17602
17603 As DWARF doesn't currently provide an explicit name for the components
17604 of a complex number, but it can be helpful to have these components
17605 named, we try to select a suitable name based on the size of the
17606 component. */
17607static struct type *
17608dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17609 struct objfile *objfile,
17610 int bits, const char *name_hint)
17611{
17612 gdbarch *gdbarch = get_objfile_arch (objfile);
17613 struct type *tt = nullptr;
17614
35add35e
AB
17615 /* Try to find a suitable floating point builtin type of size BITS.
17616 We're going to use the name of this type as the name for the complex
17617 target type that we are about to create. */
1db455a7 17618 switch (cu->language)
8bdc1658 17619 {
1db455a7
AB
17620 case language_fortran:
17621 switch (bits)
17622 {
17623 case 32:
17624 tt = builtin_f_type (gdbarch)->builtin_real;
17625 break;
17626 case 64:
17627 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17628 break;
17629 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17630 case 128:
17631 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17632 break;
17633 }
8bdc1658 17634 break;
1db455a7
AB
17635 default:
17636 switch (bits)
17637 {
17638 case 32:
17639 tt = builtin_type (gdbarch)->builtin_float;
17640 break;
17641 case 64:
17642 tt = builtin_type (gdbarch)->builtin_double;
17643 break;
17644 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17645 case 128:
17646 tt = builtin_type (gdbarch)->builtin_long_double;
17647 break;
17648 }
8bdc1658
AB
17649 break;
17650 }
17651
35add35e
AB
17652 /* If the type we found doesn't match the size we were looking for, then
17653 pretend we didn't find a type at all, the complex target type we
17654 create will then be nameless. */
a12e5744 17655 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17656 tt = nullptr;
17657
8bdc1658
AB
17658 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17659 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17660}
17661
c906108c
SS
17662/* Find a representation of a given base type and install
17663 it in the TYPE field of the die. */
17664
f792889a 17665static struct type *
e7c27a73 17666read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17667{
518817b3 17668 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17669 struct type *type;
17670 struct attribute *attr;
19f392bc 17671 int encoding = 0, bits = 0;
34877895 17672 int endianity = 0;
15d034d0 17673 const char *name;
34877895 17674 gdbarch *arch;
c906108c 17675
e142c38c 17676 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17677 if (attr != nullptr)
34877895 17678 encoding = DW_UNSND (attr);
e142c38c 17679 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17680 if (attr != nullptr)
34877895 17681 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17682 name = dwarf2_name (die, cu);
6ccb9162 17683 if (!name)
34877895
PJ
17684 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17685 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17686 if (attr)
17687 endianity = DW_UNSND (attr);
6ccb9162 17688
34877895 17689 arch = get_objfile_arch (objfile);
6ccb9162 17690 switch (encoding)
c906108c 17691 {
6ccb9162
UW
17692 case DW_ATE_address:
17693 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17694 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17695 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17696 break;
17697 case DW_ATE_boolean:
19f392bc 17698 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17699 break;
17700 case DW_ATE_complex_float:
8bdc1658 17701 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
19f392bc 17702 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17703 break;
17704 case DW_ATE_decimal_float:
19f392bc 17705 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17706 break;
17707 case DW_ATE_float:
9b790ce7 17708 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17709 break;
17710 case DW_ATE_signed:
eb77c9df 17711 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17712 break;
17713 case DW_ATE_unsigned:
3b2b8fea
TT
17714 if (cu->language == language_fortran
17715 && name
61012eef 17716 && startswith (name, "character("))
19f392bc
UW
17717 type = init_character_type (objfile, bits, 1, name);
17718 else
eb77c9df 17719 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17720 break;
17721 case DW_ATE_signed_char:
6e70227d 17722 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17723 || cu->language == language_pascal
17724 || cu->language == language_fortran)
19f392bc
UW
17725 type = init_character_type (objfile, bits, 0, name);
17726 else
eb77c9df 17727 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17728 break;
17729 case DW_ATE_unsigned_char:
868a0084 17730 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17731 || cu->language == language_pascal
c44af4eb
TT
17732 || cu->language == language_fortran
17733 || cu->language == language_rust)
19f392bc
UW
17734 type = init_character_type (objfile, bits, 1, name);
17735 else
eb77c9df 17736 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17737 break;
75079b2b 17738 case DW_ATE_UTF:
53e710ac 17739 {
53e710ac
PA
17740 if (bits == 16)
17741 type = builtin_type (arch)->builtin_char16;
17742 else if (bits == 32)
17743 type = builtin_type (arch)->builtin_char32;
17744 else
17745 {
b98664d3 17746 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17747 bits);
eb77c9df 17748 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17749 }
17750 return set_die_type (die, type, cu);
17751 }
75079b2b
TT
17752 break;
17753
6ccb9162 17754 default:
b98664d3 17755 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17756 dwarf_type_encoding_name (encoding));
77b7c781 17757 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17758 break;
c906108c 17759 }
6ccb9162 17760
0114d602 17761 if (name && strcmp (name, "char") == 0)
876cecd0 17762 TYPE_NOSIGN (type) = 1;
0114d602 17763
2b4424c3
TT
17764 maybe_set_alignment (cu, die, type);
17765
34877895
PJ
17766 switch (endianity)
17767 {
17768 case DW_END_big:
17769 if (gdbarch_byte_order (arch) == BFD_ENDIAN_LITTLE)
17770 TYPE_ENDIANITY_NOT_DEFAULT (type) = 1;
17771 break;
17772 case DW_END_little:
17773 if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
17774 TYPE_ENDIANITY_NOT_DEFAULT (type) = 1;
17775 break;
17776 }
17777
f792889a 17778 return set_die_type (die, type, cu);
c906108c
SS
17779}
17780
80180f79
SA
17781/* Parse dwarf attribute if it's a block, reference or constant and put the
17782 resulting value of the attribute into struct bound_prop.
17783 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17784
17785static int
17786attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17787 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17788 struct type *default_type)
80180f79
SA
17789{
17790 struct dwarf2_property_baton *baton;
518817b3
SM
17791 struct obstack *obstack
17792 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17793
9a49df9d
AB
17794 gdb_assert (default_type != NULL);
17795
80180f79
SA
17796 if (attr == NULL || prop == NULL)
17797 return 0;
17798
17799 if (attr_form_is_block (attr))
17800 {
8d749320 17801 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17802 baton->property_type = default_type;
80180f79
SA
17803 baton->locexpr.per_cu = cu->per_cu;
17804 baton->locexpr.size = DW_BLOCK (attr)->size;
17805 baton->locexpr.data = DW_BLOCK (attr)->data;
9a49df9d 17806 baton->locexpr.is_reference = false;
80180f79
SA
17807 prop->data.baton = baton;
17808 prop->kind = PROP_LOCEXPR;
17809 gdb_assert (prop->data.baton != NULL);
17810 }
17811 else if (attr_form_is_ref (attr))
17812 {
17813 struct dwarf2_cu *target_cu = cu;
17814 struct die_info *target_die;
17815 struct attribute *target_attr;
17816
17817 target_die = follow_die_ref (die, attr, &target_cu);
17818 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17819 if (target_attr == NULL)
17820 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17821 target_cu);
80180f79
SA
17822 if (target_attr == NULL)
17823 return 0;
17824
df25ebbd 17825 switch (target_attr->name)
80180f79 17826 {
df25ebbd
JB
17827 case DW_AT_location:
17828 if (attr_form_is_section_offset (target_attr))
17829 {
8d749320 17830 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17831 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17832 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17833 prop->data.baton = baton;
17834 prop->kind = PROP_LOCLIST;
17835 gdb_assert (prop->data.baton != NULL);
17836 }
17837 else if (attr_form_is_block (target_attr))
17838 {
8d749320 17839 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17840 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17841 baton->locexpr.per_cu = cu->per_cu;
17842 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17843 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17844 baton->locexpr.is_reference = true;
df25ebbd
JB
17845 prop->data.baton = baton;
17846 prop->kind = PROP_LOCEXPR;
17847 gdb_assert (prop->data.baton != NULL);
17848 }
17849 else
17850 {
17851 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17852 "dynamic property");
17853 return 0;
17854 }
17855 break;
17856 case DW_AT_data_member_location:
17857 {
17858 LONGEST offset;
17859
17860 if (!handle_data_member_location (target_die, target_cu,
17861 &offset))
17862 return 0;
17863
8d749320 17864 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17865 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17866 target_cu);
df25ebbd
JB
17867 baton->offset_info.offset = offset;
17868 baton->offset_info.type = die_type (target_die, target_cu);
17869 prop->data.baton = baton;
17870 prop->kind = PROP_ADDR_OFFSET;
17871 break;
17872 }
80180f79
SA
17873 }
17874 }
17875 else if (attr_form_is_constant (attr))
17876 {
17877 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17878 prop->kind = PROP_CONST;
17879 }
17880 else
17881 {
17882 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17883 dwarf2_name (die, cu));
17884 return 0;
17885 }
17886
17887 return 1;
17888}
17889
9a49df9d
AB
17890/* Find an integer type the same size as the address size given in the
17891 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17892 is unsigned or not. */
17893
17894static struct type *
17895dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17896 bool unsigned_p)
17897{
17898 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17899 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17900 struct type *int_type;
17901
17902 /* Helper macro to examine the various builtin types. */
17903#define TRY_TYPE(F) \
17904 int_type = (unsigned_p \
17905 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17906 : objfile_type (objfile)->builtin_ ## F); \
17907 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17908 return int_type
17909
17910 TRY_TYPE (char);
17911 TRY_TYPE (short);
17912 TRY_TYPE (int);
17913 TRY_TYPE (long);
17914 TRY_TYPE (long_long);
17915
17916#undef TRY_TYPE
17917
17918 gdb_assert_not_reached ("unable to find suitable integer type");
17919}
17920
b86352cf
AB
17921/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17922 present (which is valid) then compute the default type based on the
17923 compilation units address size. */
17924
17925static struct type *
17926read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17927{
17928 struct type *index_type = die_type (die, cu);
17929
17930 /* Dwarf-2 specifications explicitly allows to create subrange types
17931 without specifying a base type.
17932 In that case, the base type must be set to the type of
17933 the lower bound, upper bound or count, in that order, if any of these
17934 three attributes references an object that has a type.
17935 If no base type is found, the Dwarf-2 specifications say that
17936 a signed integer type of size equal to the size of an address should
17937 be used.
17938 For the following C code: `extern char gdb_int [];'
17939 GCC produces an empty range DIE.
17940 FIXME: muller/2010-05-28: Possible references to object for low bound,
17941 high bound or count are not yet handled by this code. */
17942 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 17943 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
17944
17945 return index_type;
17946}
17947
a02abb62
JB
17948/* Read the given DW_AT_subrange DIE. */
17949
f792889a 17950static struct type *
a02abb62
JB
17951read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17952{
4c9ad8c2 17953 struct type *base_type, *orig_base_type;
a02abb62
JB
17954 struct type *range_type;
17955 struct attribute *attr;
729efb13 17956 struct dynamic_prop low, high;
4fae6e18 17957 int low_default_is_valid;
c451ebe5 17958 int high_bound_is_count = 0;
15d034d0 17959 const char *name;
d359392f 17960 ULONGEST negative_mask;
e77813c8 17961
b86352cf
AB
17962 orig_base_type = read_subrange_index_type (die, cu);
17963
4c9ad8c2
TT
17964 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17965 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17966 creating the range type, but we use the result of check_typedef
17967 when examining properties of the type. */
17968 base_type = check_typedef (orig_base_type);
a02abb62 17969
7e314c57
JK
17970 /* The die_type call above may have already set the type for this DIE. */
17971 range_type = get_die_type (die, cu);
17972 if (range_type)
17973 return range_type;
17974
729efb13
SA
17975 low.kind = PROP_CONST;
17976 high.kind = PROP_CONST;
17977 high.data.const_val = 0;
17978
4fae6e18
JK
17979 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17980 omitting DW_AT_lower_bound. */
17981 switch (cu->language)
6e70227d 17982 {
4fae6e18
JK
17983 case language_c:
17984 case language_cplus:
729efb13 17985 low.data.const_val = 0;
4fae6e18
JK
17986 low_default_is_valid = 1;
17987 break;
17988 case language_fortran:
729efb13 17989 low.data.const_val = 1;
4fae6e18
JK
17990 low_default_is_valid = 1;
17991 break;
17992 case language_d:
4fae6e18 17993 case language_objc:
c44af4eb 17994 case language_rust:
729efb13 17995 low.data.const_val = 0;
4fae6e18
JK
17996 low_default_is_valid = (cu->header.version >= 4);
17997 break;
17998 case language_ada:
17999 case language_m2:
18000 case language_pascal:
729efb13 18001 low.data.const_val = 1;
4fae6e18
JK
18002 low_default_is_valid = (cu->header.version >= 4);
18003 break;
18004 default:
729efb13 18005 low.data.const_val = 0;
4fae6e18
JK
18006 low_default_is_valid = 0;
18007 break;
a02abb62
JB
18008 }
18009
e142c38c 18010 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18011 if (attr != nullptr)
9a49df9d 18012 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18013 else if (!low_default_is_valid)
b98664d3 18014 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18015 "- DIE at %s [in module %s]"),
18016 sect_offset_str (die->sect_off),
518817b3 18017 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18018
506f5c41
TV
18019 struct attribute *attr_ub, *attr_count;
18020 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18021 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18022 {
506f5c41 18023 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18024 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18025 {
c451ebe5
SA
18026 /* If bounds are constant do the final calculation here. */
18027 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18028 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18029 else
18030 high_bound_is_count = 1;
c2ff108b 18031 }
506f5c41
TV
18032 else
18033 {
18034 if (attr_ub != NULL)
18035 complaint (_("Unresolved DW_AT_upper_bound "
18036 "- DIE at %s [in module %s]"),
18037 sect_offset_str (die->sect_off),
18038 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18039 if (attr_count != NULL)
18040 complaint (_("Unresolved DW_AT_count "
18041 "- DIE at %s [in module %s]"),
18042 sect_offset_str (die->sect_off),
18043 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18044 }
e77813c8 18045 }
a02abb62 18046
4e962e74
TT
18047 LONGEST bias = 0;
18048 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18049 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18050 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18051
dbb9c2b1
JB
18052 /* Normally, the DWARF producers are expected to use a signed
18053 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18054 But this is unfortunately not always the case, as witnessed
18055 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18056 is used instead. To work around that ambiguity, we treat
18057 the bounds as signed, and thus sign-extend their values, when
18058 the base type is signed. */
6e70227d 18059 negative_mask =
d359392f 18060 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18061 if (low.kind == PROP_CONST
18062 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18063 low.data.const_val |= negative_mask;
18064 if (high.kind == PROP_CONST
18065 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18066 high.data.const_val |= negative_mask;
43bbcdc2 18067
4e962e74 18068 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18069
c451ebe5
SA
18070 if (high_bound_is_count)
18071 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18072
c2ff108b
JK
18073 /* Ada expects an empty array on no boundary attributes. */
18074 if (attr == NULL && cu->language != language_ada)
729efb13 18075 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18076
39cbfefa
DJ
18077 name = dwarf2_name (die, cu);
18078 if (name)
18079 TYPE_NAME (range_type) = name;
6e70227d 18080
e142c38c 18081 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18082 if (attr != nullptr)
a02abb62
JB
18083 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18084
2b4424c3
TT
18085 maybe_set_alignment (cu, die, range_type);
18086
7e314c57
JK
18087 set_die_type (die, range_type, cu);
18088
18089 /* set_die_type should be already done. */
b4ba55a1
JB
18090 set_descriptive_type (range_type, die, cu);
18091
7e314c57 18092 return range_type;
a02abb62 18093}
6e70227d 18094
f792889a 18095static struct type *
81a17f79
JB
18096read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18097{
18098 struct type *type;
81a17f79 18099
518817b3
SM
18100 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18101 NULL);
0114d602 18102 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18103
74a2f8ff 18104 /* In Ada, an unspecified type is typically used when the description
85102364 18105 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18106 such a type, we treat it as a stub, and try to resolve it later on,
18107 when needed. */
18108 if (cu->language == language_ada)
18109 TYPE_STUB (type) = 1;
18110
f792889a 18111 return set_die_type (die, type, cu);
81a17f79 18112}
a02abb62 18113
639d11d3
DC
18114/* Read a single die and all its descendents. Set the die's sibling
18115 field to NULL; set other fields in the die correctly, and set all
18116 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18117 location of the info_ptr after reading all of those dies. PARENT
18118 is the parent of the die in question. */
18119
18120static struct die_info *
dee91e82 18121read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18122 const gdb_byte *info_ptr,
18123 const gdb_byte **new_info_ptr,
dee91e82 18124 struct die_info *parent)
639d11d3
DC
18125{
18126 struct die_info *die;
d521ce57 18127 const gdb_byte *cur_ptr;
639d11d3
DC
18128 int has_children;
18129
bf6af496 18130 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18131 if (die == NULL)
18132 {
18133 *new_info_ptr = cur_ptr;
18134 return NULL;
18135 }
93311388 18136 store_in_ref_table (die, reader->cu);
639d11d3
DC
18137
18138 if (has_children)
bf6af496 18139 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18140 else
18141 {
18142 die->child = NULL;
18143 *new_info_ptr = cur_ptr;
18144 }
18145
18146 die->sibling = NULL;
18147 die->parent = parent;
18148 return die;
18149}
18150
18151/* Read a die, all of its descendents, and all of its siblings; set
18152 all of the fields of all of the dies correctly. Arguments are as
18153 in read_die_and_children. */
18154
18155static struct die_info *
bf6af496 18156read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18157 const gdb_byte *info_ptr,
18158 const gdb_byte **new_info_ptr,
bf6af496 18159 struct die_info *parent)
639d11d3
DC
18160{
18161 struct die_info *first_die, *last_sibling;
d521ce57 18162 const gdb_byte *cur_ptr;
639d11d3 18163
c906108c 18164 cur_ptr = info_ptr;
639d11d3
DC
18165 first_die = last_sibling = NULL;
18166
18167 while (1)
c906108c 18168 {
639d11d3 18169 struct die_info *die
dee91e82 18170 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18171
1d325ec1 18172 if (die == NULL)
c906108c 18173 {
639d11d3
DC
18174 *new_info_ptr = cur_ptr;
18175 return first_die;
c906108c 18176 }
1d325ec1
DJ
18177
18178 if (!first_die)
18179 first_die = die;
c906108c 18180 else
1d325ec1
DJ
18181 last_sibling->sibling = die;
18182
18183 last_sibling = die;
c906108c 18184 }
c906108c
SS
18185}
18186
bf6af496
DE
18187/* Read a die, all of its descendents, and all of its siblings; set
18188 all of the fields of all of the dies correctly. Arguments are as
18189 in read_die_and_children.
18190 This the main entry point for reading a DIE and all its children. */
18191
18192static struct die_info *
18193read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18194 const gdb_byte *info_ptr,
18195 const gdb_byte **new_info_ptr,
bf6af496
DE
18196 struct die_info *parent)
18197{
18198 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18199 new_info_ptr, parent);
18200
b4f54984 18201 if (dwarf_die_debug)
bf6af496
DE
18202 {
18203 fprintf_unfiltered (gdb_stdlog,
18204 "Read die from %s@0x%x of %s:\n",
a32a8923 18205 get_section_name (reader->die_section),
bf6af496
DE
18206 (unsigned) (info_ptr - reader->die_section->buffer),
18207 bfd_get_filename (reader->abfd));
b4f54984 18208 dump_die (die, dwarf_die_debug);
bf6af496
DE
18209 }
18210
18211 return die;
18212}
18213
3019eac3
DE
18214/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18215 attributes.
18216 The caller is responsible for filling in the extra attributes
18217 and updating (*DIEP)->num_attrs.
18218 Set DIEP to point to a newly allocated die with its information,
18219 except for its child, sibling, and parent fields.
18220 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18221
d521ce57 18222static const gdb_byte *
3019eac3 18223read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18224 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18225 int *has_children, int num_extra_attrs)
93311388 18226{
b64f50a1 18227 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18228 struct abbrev_info *abbrev;
18229 struct die_info *die;
18230 struct dwarf2_cu *cu = reader->cu;
18231 bfd *abfd = reader->abfd;
18232
9c541725 18233 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18234 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18235 info_ptr += bytes_read;
18236 if (!abbrev_number)
18237 {
18238 *diep = NULL;
18239 *has_children = 0;
18240 return info_ptr;
18241 }
18242
685af9cd 18243 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18244 if (!abbrev)
348e048f
DE
18245 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18246 abbrev_number,
18247 bfd_get_filename (abfd));
18248
3019eac3 18249 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18250 die->sect_off = sect_off;
93311388
DE
18251 die->tag = abbrev->tag;
18252 die->abbrev = abbrev_number;
18253
3019eac3
DE
18254 /* Make the result usable.
18255 The caller needs to update num_attrs after adding the extra
18256 attributes. */
93311388
DE
18257 die->num_attrs = abbrev->num_attrs;
18258
18259 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18260 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18261 info_ptr);
93311388
DE
18262
18263 *diep = die;
18264 *has_children = abbrev->has_children;
18265 return info_ptr;
18266}
18267
3019eac3
DE
18268/* Read a die and all its attributes.
18269 Set DIEP to point to a newly allocated die with its information,
18270 except for its child, sibling, and parent fields.
18271 Set HAS_CHILDREN to tell whether the die has children or not. */
18272
d521ce57 18273static const gdb_byte *
3019eac3 18274read_full_die (const struct die_reader_specs *reader,
d521ce57 18275 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18276 int *has_children)
18277{
d521ce57 18278 const gdb_byte *result;
bf6af496
DE
18279
18280 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18281
b4f54984 18282 if (dwarf_die_debug)
bf6af496
DE
18283 {
18284 fprintf_unfiltered (gdb_stdlog,
18285 "Read die from %s@0x%x of %s:\n",
a32a8923 18286 get_section_name (reader->die_section),
bf6af496
DE
18287 (unsigned) (info_ptr - reader->die_section->buffer),
18288 bfd_get_filename (reader->abfd));
b4f54984 18289 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18290 }
18291
18292 return result;
3019eac3 18293}
433df2d4
DE
18294\f
18295/* Abbreviation tables.
3019eac3 18296
433df2d4 18297 In DWARF version 2, the description of the debugging information is
c906108c
SS
18298 stored in a separate .debug_abbrev section. Before we read any
18299 dies from a section we read in all abbreviations and install them
433df2d4
DE
18300 in a hash table. */
18301
18302/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18303
685af9cd
TT
18304struct abbrev_info *
18305abbrev_table::alloc_abbrev ()
433df2d4
DE
18306{
18307 struct abbrev_info *abbrev;
18308
685af9cd 18309 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18310 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18311
433df2d4
DE
18312 return abbrev;
18313}
18314
18315/* Add an abbreviation to the table. */
c906108c 18316
685af9cd
TT
18317void
18318abbrev_table::add_abbrev (unsigned int abbrev_number,
18319 struct abbrev_info *abbrev)
433df2d4
DE
18320{
18321 unsigned int hash_number;
18322
18323 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18324 abbrev->next = m_abbrevs[hash_number];
18325 m_abbrevs[hash_number] = abbrev;
433df2d4 18326}
dee91e82 18327
433df2d4
DE
18328/* Look up an abbrev in the table.
18329 Returns NULL if the abbrev is not found. */
18330
685af9cd
TT
18331struct abbrev_info *
18332abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18333{
433df2d4
DE
18334 unsigned int hash_number;
18335 struct abbrev_info *abbrev;
18336
18337 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18338 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18339
18340 while (abbrev)
18341 {
18342 if (abbrev->number == abbrev_number)
18343 return abbrev;
18344 abbrev = abbrev->next;
18345 }
18346 return NULL;
18347}
18348
18349/* Read in an abbrev table. */
18350
685af9cd 18351static abbrev_table_up
ed2dc618
SM
18352abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18353 struct dwarf2_section_info *section,
9c541725 18354 sect_offset sect_off)
433df2d4
DE
18355{
18356 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18357 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18358 const gdb_byte *abbrev_ptr;
c906108c
SS
18359 struct abbrev_info *cur_abbrev;
18360 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18361 unsigned int abbrev_form;
f3dd6933
DJ
18362 struct attr_abbrev *cur_attrs;
18363 unsigned int allocated_attrs;
c906108c 18364
685af9cd 18365 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18366
433df2d4 18367 dwarf2_read_section (objfile, section);
9c541725 18368 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18369 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18370 abbrev_ptr += bytes_read;
18371
f3dd6933 18372 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18373 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18374
0963b4bd 18375 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18376 while (abbrev_number)
18377 {
685af9cd 18378 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18379
18380 /* read in abbrev header */
18381 cur_abbrev->number = abbrev_number;
aead7601
SM
18382 cur_abbrev->tag
18383 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18384 abbrev_ptr += bytes_read;
18385 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18386 abbrev_ptr += 1;
18387
18388 /* now read in declarations */
22d2f3ab 18389 for (;;)
c906108c 18390 {
43988095
JK
18391 LONGEST implicit_const;
18392
22d2f3ab
JK
18393 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18394 abbrev_ptr += bytes_read;
18395 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18396 abbrev_ptr += bytes_read;
43988095
JK
18397 if (abbrev_form == DW_FORM_implicit_const)
18398 {
18399 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18400 &bytes_read);
18401 abbrev_ptr += bytes_read;
18402 }
18403 else
18404 {
18405 /* Initialize it due to a false compiler warning. */
18406 implicit_const = -1;
18407 }
22d2f3ab
JK
18408
18409 if (abbrev_name == 0)
18410 break;
18411
f3dd6933 18412 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18413 {
f3dd6933
DJ
18414 allocated_attrs += ATTR_ALLOC_CHUNK;
18415 cur_attrs
224c3ddb 18416 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18417 }
ae038cb0 18418
aead7601
SM
18419 cur_attrs[cur_abbrev->num_attrs].name
18420 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18421 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18422 = (enum dwarf_form) abbrev_form;
43988095 18423 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18424 ++cur_abbrev->num_attrs;
c906108c
SS
18425 }
18426
8d749320
SM
18427 cur_abbrev->attrs =
18428 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18429 cur_abbrev->num_attrs);
f3dd6933
DJ
18430 memcpy (cur_abbrev->attrs, cur_attrs,
18431 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18432
685af9cd 18433 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18434
18435 /* Get next abbreviation.
18436 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18437 always properly terminated with an abbrev number of 0.
18438 Exit loop if we encounter an abbreviation which we have
18439 already read (which means we are about to read the abbreviations
18440 for the next compile unit) or if the end of the abbreviation
18441 table is reached. */
433df2d4 18442 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18443 break;
18444 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18445 abbrev_ptr += bytes_read;
685af9cd 18446 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18447 break;
18448 }
f3dd6933
DJ
18449
18450 xfree (cur_attrs);
433df2d4 18451 return abbrev_table;
c906108c
SS
18452}
18453
72bf9492
DJ
18454/* Returns nonzero if TAG represents a type that we might generate a partial
18455 symbol for. */
18456
18457static int
18458is_type_tag_for_partial (int tag)
18459{
18460 switch (tag)
18461 {
18462#if 0
18463 /* Some types that would be reasonable to generate partial symbols for,
18464 that we don't at present. */
18465 case DW_TAG_array_type:
18466 case DW_TAG_file_type:
18467 case DW_TAG_ptr_to_member_type:
18468 case DW_TAG_set_type:
18469 case DW_TAG_string_type:
18470 case DW_TAG_subroutine_type:
18471#endif
18472 case DW_TAG_base_type:
18473 case DW_TAG_class_type:
680b30c7 18474 case DW_TAG_interface_type:
72bf9492
DJ
18475 case DW_TAG_enumeration_type:
18476 case DW_TAG_structure_type:
18477 case DW_TAG_subrange_type:
18478 case DW_TAG_typedef:
18479 case DW_TAG_union_type:
18480 return 1;
18481 default:
18482 return 0;
18483 }
18484}
18485
18486/* Load all DIEs that are interesting for partial symbols into memory. */
18487
18488static struct partial_die_info *
dee91e82 18489load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18490 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18491{
dee91e82 18492 struct dwarf2_cu *cu = reader->cu;
518817b3 18493 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18494 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18495 unsigned int bytes_read;
5afb4e99 18496 unsigned int load_all = 0;
72bf9492
DJ
18497 int nesting_level = 1;
18498
18499 parent_die = NULL;
18500 last_die = NULL;
18501
7adf1e79
DE
18502 gdb_assert (cu->per_cu != NULL);
18503 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18504 load_all = 1;
18505
72bf9492
DJ
18506 cu->partial_dies
18507 = htab_create_alloc_ex (cu->header.length / 12,
18508 partial_die_hash,
18509 partial_die_eq,
18510 NULL,
18511 &cu->comp_unit_obstack,
18512 hashtab_obstack_allocate,
18513 dummy_obstack_deallocate);
18514
72bf9492
DJ
18515 while (1)
18516 {
685af9cd 18517 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18518
18519 /* A NULL abbrev means the end of a series of children. */
18520 if (abbrev == NULL)
18521 {
18522 if (--nesting_level == 0)
cd9983dd
YQ
18523 return first_die;
18524
72bf9492
DJ
18525 info_ptr += bytes_read;
18526 last_die = parent_die;
18527 parent_die = parent_die->die_parent;
18528 continue;
18529 }
18530
98bfdba5
PA
18531 /* Check for template arguments. We never save these; if
18532 they're seen, we just mark the parent, and go on our way. */
18533 if (parent_die != NULL
18534 && cu->language == language_cplus
18535 && (abbrev->tag == DW_TAG_template_type_param
18536 || abbrev->tag == DW_TAG_template_value_param))
18537 {
18538 parent_die->has_template_arguments = 1;
18539
18540 if (!load_all)
18541 {
18542 /* We don't need a partial DIE for the template argument. */
dee91e82 18543 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18544 continue;
18545 }
18546 }
18547
0d99eb77 18548 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18549 Skip their other children. */
18550 if (!load_all
18551 && cu->language == language_cplus
18552 && parent_die != NULL
18553 && parent_die->tag == DW_TAG_subprogram)
18554 {
dee91e82 18555 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18556 continue;
18557 }
18558
5afb4e99
DJ
18559 /* Check whether this DIE is interesting enough to save. Normally
18560 we would not be interested in members here, but there may be
18561 later variables referencing them via DW_AT_specification (for
18562 static members). */
18563 if (!load_all
18564 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18565 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18566 && abbrev->tag != DW_TAG_enumerator
18567 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18568 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18569 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18570 && abbrev->tag != DW_TAG_variable
5afb4e99 18571 && abbrev->tag != DW_TAG_namespace
f55ee35c 18572 && abbrev->tag != DW_TAG_module
95554aad 18573 && abbrev->tag != DW_TAG_member
74921315
KS
18574 && abbrev->tag != DW_TAG_imported_unit
18575 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18576 {
18577 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18578 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18579 continue;
18580 }
18581
6f06d47b
YQ
18582 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18583 abbrev);
cd9983dd 18584
48fbe735 18585 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18586
18587 /* This two-pass algorithm for processing partial symbols has a
18588 high cost in cache pressure. Thus, handle some simple cases
18589 here which cover the majority of C partial symbols. DIEs
18590 which neither have specification tags in them, nor could have
18591 specification tags elsewhere pointing at them, can simply be
18592 processed and discarded.
18593
18594 This segment is also optional; scan_partial_symbols and
18595 add_partial_symbol will handle these DIEs if we chain
18596 them in normally. When compilers which do not emit large
18597 quantities of duplicate debug information are more common,
18598 this code can probably be removed. */
18599
18600 /* Any complete simple types at the top level (pretty much all
18601 of them, for a language without namespaces), can be processed
18602 directly. */
18603 if (parent_die == NULL
cd9983dd
YQ
18604 && pdi.has_specification == 0
18605 && pdi.is_declaration == 0
18606 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18607 || pdi.tag == DW_TAG_base_type
18608 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18609 {
cd9983dd 18610 if (building_psymtab && pdi.name != NULL)
31edb802 18611 add_psymbol_to_list (pdi.name, false,
79748972 18612 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18613 psymbol_placement::STATIC,
1762568f 18614 0, cu->language, objfile);
cd9983dd 18615 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18616 continue;
18617 }
18618
d8228535
JK
18619 /* The exception for DW_TAG_typedef with has_children above is
18620 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18621 type_name_or_error will error on such types later.
d8228535
JK
18622
18623 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18624 it could not find the child DIEs referenced later, this is checked
18625 above. In correct DWARF DW_TAG_typedef should have no children. */
18626
cd9983dd 18627 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18628 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18629 "- DIE at %s [in module %s]"),
cd9983dd 18630 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18631
72bf9492
DJ
18632 /* If we're at the second level, and we're an enumerator, and
18633 our parent has no specification (meaning possibly lives in a
18634 namespace elsewhere), then we can add the partial symbol now
18635 instead of queueing it. */
cd9983dd 18636 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18637 && parent_die != NULL
18638 && parent_die->die_parent == NULL
18639 && parent_die->tag == DW_TAG_enumeration_type
18640 && parent_die->has_specification == 0)
18641 {
cd9983dd 18642 if (pdi.name == NULL)
b98664d3 18643 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18644 else if (building_psymtab)
31edb802 18645 add_psymbol_to_list (pdi.name, false,
79748972 18646 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18647 cu->language == language_cplus
75aedd27
TT
18648 ? psymbol_placement::GLOBAL
18649 : psymbol_placement::STATIC,
1762568f 18650 0, cu->language, objfile);
72bf9492 18651
cd9983dd 18652 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18653 continue;
18654 }
18655
cd9983dd 18656 struct partial_die_info *part_die
6f06d47b 18657 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18658
72bf9492
DJ
18659 /* We'll save this DIE so link it in. */
18660 part_die->die_parent = parent_die;
18661 part_die->die_sibling = NULL;
18662 part_die->die_child = NULL;
18663
18664 if (last_die && last_die == parent_die)
18665 last_die->die_child = part_die;
18666 else if (last_die)
18667 last_die->die_sibling = part_die;
18668
18669 last_die = part_die;
18670
18671 if (first_die == NULL)
18672 first_die = part_die;
18673
18674 /* Maybe add the DIE to the hash table. Not all DIEs that we
18675 find interesting need to be in the hash table, because we
18676 also have the parent/sibling/child chains; only those that we
18677 might refer to by offset later during partial symbol reading.
18678
18679 For now this means things that might have be the target of a
18680 DW_AT_specification, DW_AT_abstract_origin, or
18681 DW_AT_extension. DW_AT_extension will refer only to
18682 namespaces; DW_AT_abstract_origin refers to functions (and
18683 many things under the function DIE, but we do not recurse
18684 into function DIEs during partial symbol reading) and
18685 possibly variables as well; DW_AT_specification refers to
18686 declarations. Declarations ought to have the DW_AT_declaration
18687 flag. It happens that GCC forgets to put it in sometimes, but
18688 only for functions, not for types.
18689
18690 Adding more things than necessary to the hash table is harmless
18691 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18692 wasted time in find_partial_die, when we reread the compilation
18693 unit with load_all_dies set. */
72bf9492 18694
5afb4e99 18695 if (load_all
72929c62 18696 || abbrev->tag == DW_TAG_constant
5afb4e99 18697 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18698 || abbrev->tag == DW_TAG_variable
18699 || abbrev->tag == DW_TAG_namespace
18700 || part_die->is_declaration)
18701 {
18702 void **slot;
18703
18704 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18705 to_underlying (part_die->sect_off),
18706 INSERT);
72bf9492
DJ
18707 *slot = part_die;
18708 }
18709
72bf9492 18710 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18711 we have no reason to follow the children of structures; for other
98bfdba5
PA
18712 languages we have to, so that we can get at method physnames
18713 to infer fully qualified class names, for DW_AT_specification,
18714 and for C++ template arguments. For C++, we also look one level
18715 inside functions to find template arguments (if the name of the
18716 function does not already contain the template arguments).
bc30ff58 18717
0a4b0913
AB
18718 For Ada and Fortran, we need to scan the children of subprograms
18719 and lexical blocks as well because these languages allow the
18720 definition of nested entities that could be interesting for the
18721 debugger, such as nested subprograms for instance. */
72bf9492 18722 if (last_die->has_children
5afb4e99
DJ
18723 && (load_all
18724 || last_die->tag == DW_TAG_namespace
f55ee35c 18725 || last_die->tag == DW_TAG_module
72bf9492 18726 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18727 || (cu->language == language_cplus
18728 && last_die->tag == DW_TAG_subprogram
18729 && (last_die->name == NULL
18730 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18731 || (cu->language != language_c
18732 && (last_die->tag == DW_TAG_class_type
680b30c7 18733 || last_die->tag == DW_TAG_interface_type
72bf9492 18734 || last_die->tag == DW_TAG_structure_type
bc30ff58 18735 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18736 || ((cu->language == language_ada
18737 || cu->language == language_fortran)
bc30ff58
JB
18738 && (last_die->tag == DW_TAG_subprogram
18739 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18740 {
18741 nesting_level++;
18742 parent_die = last_die;
18743 continue;
18744 }
18745
18746 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18747 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18748
18749 /* Back to the top, do it again. */
18750 }
18751}
18752
6f06d47b
YQ
18753partial_die_info::partial_die_info (sect_offset sect_off_,
18754 struct abbrev_info *abbrev)
18755 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18756{
18757}
18758
35cc7ed7
YQ
18759/* Read a minimal amount of information into the minimal die structure.
18760 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18761
48fbe735
YQ
18762const gdb_byte *
18763partial_die_info::read (const struct die_reader_specs *reader,
18764 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18765{
dee91e82 18766 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18767 struct dwarf2_per_objfile *dwarf2_per_objfile
18768 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18769 unsigned int i;
c5aa993b 18770 int has_low_pc_attr = 0;
c906108c 18771 int has_high_pc_attr = 0;
91da1414 18772 int high_pc_relative = 0;
c906108c 18773
fd0a254f 18774 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18775 {
48fbe735
YQ
18776 struct attribute attr;
18777
fd0a254f 18778 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18779
18780 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18781 partial symbol table. */
c906108c
SS
18782 switch (attr.name)
18783 {
18784 case DW_AT_name:
48fbe735 18785 switch (tag)
71c25dea
TT
18786 {
18787 case DW_TAG_compile_unit:
95554aad 18788 case DW_TAG_partial_unit:
348e048f 18789 case DW_TAG_type_unit:
71c25dea
TT
18790 /* Compilation units have a DW_AT_name that is a filename, not
18791 a source language identifier. */
18792 case DW_TAG_enumeration_type:
18793 case DW_TAG_enumerator:
18794 /* These tags always have simple identifiers already; no need
18795 to canonicalize them. */
48fbe735 18796 name = DW_STRING (&attr);
71c25dea
TT
18797 break;
18798 default:
48fbe735
YQ
18799 {
18800 struct objfile *objfile = dwarf2_per_objfile->objfile;
18801
18802 name
18803 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18804 &objfile->per_bfd->storage_obstack);
18805 }
71c25dea
TT
18806 break;
18807 }
c906108c 18808 break;
31ef98ae 18809 case DW_AT_linkage_name:
c906108c 18810 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18811 /* Note that both forms of linkage name might appear. We
18812 assume they will be the same, and we only store the last
18813 one we see. */
48fbe735 18814 linkage_name = DW_STRING (&attr);
c906108c
SS
18815 break;
18816 case DW_AT_low_pc:
18817 has_low_pc_attr = 1;
48fbe735 18818 lowpc = attr_value_as_address (&attr);
c906108c
SS
18819 break;
18820 case DW_AT_high_pc:
18821 has_high_pc_attr = 1;
48fbe735 18822 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18823 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18824 high_pc_relative = 1;
c906108c
SS
18825 break;
18826 case DW_AT_location:
0963b4bd 18827 /* Support the .debug_loc offsets. */
8e19ed76
PS
18828 if (attr_form_is_block (&attr))
18829 {
48fbe735 18830 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18831 }
3690dd37 18832 else if (attr_form_is_section_offset (&attr))
8e19ed76 18833 {
4d3c2250 18834 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18835 }
18836 else
18837 {
4d3c2250
KB
18838 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18839 "partial symbol information");
8e19ed76 18840 }
c906108c 18841 break;
c906108c 18842 case DW_AT_external:
48fbe735 18843 is_external = DW_UNSND (&attr);
c906108c
SS
18844 break;
18845 case DW_AT_declaration:
48fbe735 18846 is_declaration = DW_UNSND (&attr);
c906108c
SS
18847 break;
18848 case DW_AT_type:
48fbe735 18849 has_type = 1;
c906108c
SS
18850 break;
18851 case DW_AT_abstract_origin:
18852 case DW_AT_specification:
72bf9492 18853 case DW_AT_extension:
48fbe735
YQ
18854 has_specification = 1;
18855 spec_offset = dwarf2_get_ref_die_offset (&attr);
18856 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18857 || cu->per_cu->is_dwz);
c906108c
SS
18858 break;
18859 case DW_AT_sibling:
18860 /* Ignore absolute siblings, they might point outside of
18861 the current compile unit. */
18862 if (attr.form == DW_FORM_ref_addr)
b98664d3 18863 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18864 else
b9502d3f 18865 {
48fbe735 18866 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18867 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18868 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18869
18870 if (sibling_ptr < info_ptr)
b98664d3 18871 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18872 else if (sibling_ptr > reader->buffer_end)
18873 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18874 else
48fbe735 18875 sibling = sibling_ptr;
b9502d3f 18876 }
c906108c 18877 break;
fa4028e9 18878 case DW_AT_byte_size:
48fbe735 18879 has_byte_size = 1;
fa4028e9 18880 break;
ff908ebf 18881 case DW_AT_const_value:
48fbe735 18882 has_const_value = 1;
ff908ebf 18883 break;
68511cec
CES
18884 case DW_AT_calling_convention:
18885 /* DWARF doesn't provide a way to identify a program's source-level
18886 entry point. DW_AT_calling_convention attributes are only meant
18887 to describe functions' calling conventions.
18888
18889 However, because it's a necessary piece of information in
0c1b455e
TT
18890 Fortran, and before DWARF 4 DW_CC_program was the only
18891 piece of debugging information whose definition refers to
18892 a 'main program' at all, several compilers marked Fortran
18893 main programs with DW_CC_program --- even when those
18894 functions use the standard calling conventions.
18895
18896 Although DWARF now specifies a way to provide this
18897 information, we support this practice for backward
18898 compatibility. */
68511cec 18899 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18900 && cu->language == language_fortran)
48fbe735 18901 main_subprogram = 1;
68511cec 18902 break;
481860b3
GB
18903 case DW_AT_inline:
18904 if (DW_UNSND (&attr) == DW_INL_inlined
18905 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18906 may_be_inlined = 1;
481860b3 18907 break;
95554aad
TT
18908
18909 case DW_AT_import:
48fbe735 18910 if (tag == DW_TAG_imported_unit)
36586728 18911 {
48fbe735
YQ
18912 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18913 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18914 || cu->per_cu->is_dwz);
18915 }
95554aad
TT
18916 break;
18917
0c1b455e 18918 case DW_AT_main_subprogram:
48fbe735 18919 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18920 break;
18921
05caa1d2
TT
18922 case DW_AT_ranges:
18923 {
18924 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18925 but that requires a full DIE, so instead we just
18926 reimplement it. */
18927 int need_ranges_base = tag != DW_TAG_compile_unit;
18928 unsigned int ranges_offset = (DW_UNSND (&attr)
18929 + (need_ranges_base
18930 ? cu->ranges_base
18931 : 0));
18932
18933 /* Value of the DW_AT_ranges attribute is the offset in the
18934 .debug_ranges section. */
18935 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18936 nullptr))
18937 has_pc_info = 1;
18938 }
18939 break;
18940
c906108c
SS
18941 default:
18942 break;
18943 }
18944 }
18945
10d06d82
TT
18946 /* For Ada, if both the name and the linkage name appear, we prefer
18947 the latter. This lets "catch exception" work better, regardless
18948 of the order in which the name and linkage name were emitted.
18949 Really, though, this is just a workaround for the fact that gdb
18950 doesn't store both the name and the linkage name. */
18951 if (cu->language == language_ada && linkage_name != nullptr)
18952 name = linkage_name;
18953
91da1414 18954 if (high_pc_relative)
48fbe735 18955 highpc += lowpc;
91da1414 18956
9373cf26
JK
18957 if (has_low_pc_attr && has_high_pc_attr)
18958 {
18959 /* When using the GNU linker, .gnu.linkonce. sections are used to
18960 eliminate duplicate copies of functions and vtables and such.
18961 The linker will arbitrarily choose one and discard the others.
18962 The AT_*_pc values for such functions refer to local labels in
18963 these sections. If the section from that file was discarded, the
18964 labels are not in the output, so the relocs get a value of 0.
18965 If this is a discarded function, mark the pc bounds as invalid,
18966 so that GDB will ignore it. */
48fbe735 18967 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18968 {
48fbe735 18969 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18970 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18971
b98664d3 18972 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18973 "for DIE at %s [in module %s]"),
48fbe735
YQ
18974 paddress (gdbarch, lowpc),
18975 sect_offset_str (sect_off),
9d8780f0 18976 objfile_name (objfile));
9373cf26
JK
18977 }
18978 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18979 else if (lowpc >= highpc)
9373cf26 18980 {
48fbe735 18981 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18982 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18983
b98664d3 18984 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18985 "for DIE at %s [in module %s]"),
48fbe735
YQ
18986 paddress (gdbarch, lowpc),
18987 paddress (gdbarch, highpc),
18988 sect_offset_str (sect_off),
9c541725 18989 objfile_name (objfile));
9373cf26
JK
18990 }
18991 else
48fbe735 18992 has_pc_info = 1;
9373cf26 18993 }
85cbf3d3 18994
c906108c
SS
18995 return info_ptr;
18996}
18997
72bf9492
DJ
18998/* Find a cached partial DIE at OFFSET in CU. */
18999
d590ff25
YQ
19000struct partial_die_info *
19001dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19002{
19003 struct partial_die_info *lookup_die = NULL;
6f06d47b 19004 struct partial_die_info part_die (sect_off);
72bf9492 19005
9a3c8263 19006 lookup_die = ((struct partial_die_info *)
d590ff25 19007 htab_find_with_hash (partial_dies, &part_die,
9c541725 19008 to_underlying (sect_off)));
72bf9492 19009
72bf9492
DJ
19010 return lookup_die;
19011}
19012
348e048f
DE
19013/* Find a partial DIE at OFFSET, which may or may not be in CU,
19014 except in the case of .debug_types DIEs which do not reference
19015 outside their CU (they do however referencing other types via
55f1336d 19016 DW_FORM_ref_sig8). */
72bf9492 19017
122cf0f2 19018static const struct cu_partial_die_info
9c541725 19019find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19020{
518817b3
SM
19021 struct dwarf2_per_objfile *dwarf2_per_objfile
19022 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19023 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19024 struct dwarf2_per_cu_data *per_cu = NULL;
19025 struct partial_die_info *pd = NULL;
72bf9492 19026
36586728 19027 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19028 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19029 {
d590ff25 19030 pd = cu->find_partial_die (sect_off);
5afb4e99 19031 if (pd != NULL)
fb816e8b 19032 return { cu, pd };
0d99eb77
DE
19033 /* We missed recording what we needed.
19034 Load all dies and try again. */
19035 per_cu = cu->per_cu;
5afb4e99 19036 }
0d99eb77
DE
19037 else
19038 {
19039 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19040 if (cu->per_cu->is_debug_types)
0d99eb77 19041 {
9d8780f0
SM
19042 error (_("Dwarf Error: Type Unit at offset %s contains"
19043 " external reference to offset %s [in module %s].\n"),
19044 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19045 bfd_get_filename (objfile->obfd));
19046 }
9c541725 19047 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19048 dwarf2_per_objfile);
72bf9492 19049
0d99eb77
DE
19050 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19051 load_partial_comp_unit (per_cu);
ae038cb0 19052
0d99eb77 19053 per_cu->cu->last_used = 0;
d590ff25 19054 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19055 }
5afb4e99 19056
dee91e82
DE
19057 /* If we didn't find it, and not all dies have been loaded,
19058 load them all and try again. */
19059
5afb4e99
DJ
19060 if (pd == NULL && per_cu->load_all_dies == 0)
19061 {
5afb4e99 19062 per_cu->load_all_dies = 1;
fd820528
DE
19063
19064 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19065 THIS_CU->cu may already be in use. So we can't just free it and
19066 replace its DIEs with the ones we read in. Instead, we leave those
19067 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19068 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19069 set. */
dee91e82 19070 load_partial_comp_unit (per_cu);
5afb4e99 19071
d590ff25 19072 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19073 }
19074
19075 if (pd == NULL)
19076 internal_error (__FILE__, __LINE__,
9d8780f0 19077 _("could not find partial DIE %s "
3e43a32a 19078 "in cache [from module %s]\n"),
9d8780f0 19079 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19080 return { per_cu->cu, pd };
72bf9492
DJ
19081}
19082
abc72ce4
DE
19083/* See if we can figure out if the class lives in a namespace. We do
19084 this by looking for a member function; its demangled name will
19085 contain namespace info, if there is any. */
19086
19087static void
19088guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19089 struct dwarf2_cu *cu)
19090{
19091 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19092 what template types look like, because the demangler
19093 frequently doesn't give the same name as the debug info. We
19094 could fix this by only using the demangled name to get the
19095 prefix (but see comment in read_structure_type). */
19096
19097 struct partial_die_info *real_pdi;
19098 struct partial_die_info *child_pdi;
19099
19100 /* If this DIE (this DIE's specification, if any) has a parent, then
19101 we should not do this. We'll prepend the parent's fully qualified
19102 name when we create the partial symbol. */
19103
19104 real_pdi = struct_pdi;
19105 while (real_pdi->has_specification)
fb816e8b 19106 {
122cf0f2
AB
19107 auto res = find_partial_die (real_pdi->spec_offset,
19108 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19109 real_pdi = res.pdi;
19110 cu = res.cu;
19111 }
abc72ce4
DE
19112
19113 if (real_pdi->die_parent != NULL)
19114 return;
19115
19116 for (child_pdi = struct_pdi->die_child;
19117 child_pdi != NULL;
19118 child_pdi = child_pdi->die_sibling)
19119 {
19120 if (child_pdi->tag == DW_TAG_subprogram
19121 && child_pdi->linkage_name != NULL)
19122 {
19123 char *actual_class_name
19124 = language_class_name_from_physname (cu->language_defn,
19125 child_pdi->linkage_name);
19126 if (actual_class_name != NULL)
19127 {
518817b3 19128 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19129 struct_pdi->name
021887d8
TT
19130 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19131 actual_class_name);
abc72ce4
DE
19132 xfree (actual_class_name);
19133 }
19134 break;
19135 }
19136 }
19137}
19138
52356b79
YQ
19139void
19140partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19141{
abc72ce4
DE
19142 /* Once we've fixed up a die, there's no point in doing so again.
19143 This also avoids a memory leak if we were to call
19144 guess_partial_die_structure_name multiple times. */
52356b79 19145 if (fixup_called)
abc72ce4
DE
19146 return;
19147
72bf9492
DJ
19148 /* If we found a reference attribute and the DIE has no name, try
19149 to find a name in the referred to DIE. */
19150
52356b79 19151 if (name == NULL && has_specification)
72bf9492
DJ
19152 {
19153 struct partial_die_info *spec_die;
72bf9492 19154
122cf0f2 19155 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19156 spec_die = res.pdi;
19157 cu = res.cu;
72bf9492 19158
52356b79 19159 spec_die->fixup (cu);
72bf9492
DJ
19160
19161 if (spec_die->name)
19162 {
52356b79 19163 name = spec_die->name;
72bf9492
DJ
19164
19165 /* Copy DW_AT_external attribute if it is set. */
19166 if (spec_die->is_external)
52356b79 19167 is_external = spec_die->is_external;
72bf9492
DJ
19168 }
19169 }
19170
19171 /* Set default names for some unnamed DIEs. */
72bf9492 19172
52356b79
YQ
19173 if (name == NULL && tag == DW_TAG_namespace)
19174 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19175
abc72ce4
DE
19176 /* If there is no parent die to provide a namespace, and there are
19177 children, see if we can determine the namespace from their linkage
122d1940 19178 name. */
abc72ce4 19179 if (cu->language == language_cplus
fd5866f6 19180 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19181 && die_parent == NULL
19182 && has_children
19183 && (tag == DW_TAG_class_type
19184 || tag == DW_TAG_structure_type
19185 || tag == DW_TAG_union_type))
19186 guess_partial_die_structure_name (this, cu);
abc72ce4 19187
53832f31
TT
19188 /* GCC might emit a nameless struct or union that has a linkage
19189 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19190 if (name == NULL
19191 && (tag == DW_TAG_class_type
19192 || tag == DW_TAG_interface_type
19193 || tag == DW_TAG_structure_type
19194 || tag == DW_TAG_union_type)
19195 && linkage_name != NULL)
53832f31
TT
19196 {
19197 char *demangled;
19198
52356b79 19199 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19200 if (demangled)
19201 {
96408a79
SA
19202 const char *base;
19203
19204 /* Strip any leading namespaces/classes, keep only the base name.
19205 DW_AT_name for named DIEs does not contain the prefixes. */
19206 base = strrchr (demangled, ':');
19207 if (base && base > demangled && base[-1] == ':')
19208 base++;
19209 else
19210 base = demangled;
19211
518817b3 19212 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19213 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19214 xfree (demangled);
19215 }
19216 }
19217
52356b79 19218 fixup_called = 1;
72bf9492
DJ
19219}
19220
a8329558 19221/* Read an attribute value described by an attribute form. */
c906108c 19222
d521ce57 19223static const gdb_byte *
dee91e82
DE
19224read_attribute_value (const struct die_reader_specs *reader,
19225 struct attribute *attr, unsigned form,
43988095 19226 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19227{
dee91e82 19228 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19229 struct dwarf2_per_objfile *dwarf2_per_objfile
19230 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19231 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19232 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19233 bfd *abfd = reader->abfd;
e7c27a73 19234 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19235 unsigned int bytes_read;
19236 struct dwarf_block *blk;
19237
aead7601 19238 attr->form = (enum dwarf_form) form;
a8329558 19239 switch (form)
c906108c 19240 {
c906108c 19241 case DW_FORM_ref_addr:
ae411497 19242 if (cu->header.version == 2)
4568ecf9 19243 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19244 else
4568ecf9
DE
19245 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19246 &cu->header, &bytes_read);
ae411497
TT
19247 info_ptr += bytes_read;
19248 break;
36586728
TT
19249 case DW_FORM_GNU_ref_alt:
19250 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19251 info_ptr += bytes_read;
19252 break;
ae411497 19253 case DW_FORM_addr:
e7c27a73 19254 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19255 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19256 info_ptr += bytes_read;
c906108c
SS
19257 break;
19258 case DW_FORM_block2:
7b5a2f43 19259 blk = dwarf_alloc_block (cu);
c906108c
SS
19260 blk->size = read_2_bytes (abfd, info_ptr);
19261 info_ptr += 2;
19262 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19263 info_ptr += blk->size;
19264 DW_BLOCK (attr) = blk;
19265 break;
19266 case DW_FORM_block4:
7b5a2f43 19267 blk = dwarf_alloc_block (cu);
c906108c
SS
19268 blk->size = read_4_bytes (abfd, info_ptr);
19269 info_ptr += 4;
19270 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19271 info_ptr += blk->size;
19272 DW_BLOCK (attr) = blk;
19273 break;
19274 case DW_FORM_data2:
19275 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19276 info_ptr += 2;
19277 break;
19278 case DW_FORM_data4:
19279 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19280 info_ptr += 4;
19281 break;
19282 case DW_FORM_data8:
19283 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19284 info_ptr += 8;
19285 break;
0224619f
JK
19286 case DW_FORM_data16:
19287 blk = dwarf_alloc_block (cu);
19288 blk->size = 16;
19289 blk->data = read_n_bytes (abfd, info_ptr, 16);
19290 info_ptr += 16;
19291 DW_BLOCK (attr) = blk;
19292 break;
2dc7f7b3
TT
19293 case DW_FORM_sec_offset:
19294 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19295 info_ptr += bytes_read;
19296 break;
c906108c 19297 case DW_FORM_string:
9b1c24c8 19298 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19299 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19300 info_ptr += bytes_read;
19301 break;
4bdf3d34 19302 case DW_FORM_strp:
36586728
TT
19303 if (!cu->per_cu->is_dwz)
19304 {
ed2dc618
SM
19305 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19306 abfd, info_ptr, cu_header,
36586728
TT
19307 &bytes_read);
19308 DW_STRING_IS_CANONICAL (attr) = 0;
19309 info_ptr += bytes_read;
19310 break;
19311 }
19312 /* FALLTHROUGH */
43988095
JK
19313 case DW_FORM_line_strp:
19314 if (!cu->per_cu->is_dwz)
19315 {
ed2dc618
SM
19316 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19317 abfd, info_ptr,
43988095
JK
19318 cu_header, &bytes_read);
19319 DW_STRING_IS_CANONICAL (attr) = 0;
19320 info_ptr += bytes_read;
19321 break;
19322 }
19323 /* FALLTHROUGH */
36586728
TT
19324 case DW_FORM_GNU_strp_alt:
19325 {
ed2dc618 19326 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19327 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19328 &bytes_read);
19329
ed2dc618
SM
19330 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19331 dwz, str_offset);
36586728
TT
19332 DW_STRING_IS_CANONICAL (attr) = 0;
19333 info_ptr += bytes_read;
19334 }
4bdf3d34 19335 break;
2dc7f7b3 19336 case DW_FORM_exprloc:
c906108c 19337 case DW_FORM_block:
7b5a2f43 19338 blk = dwarf_alloc_block (cu);
c906108c
SS
19339 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19340 info_ptr += bytes_read;
19341 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19342 info_ptr += blk->size;
19343 DW_BLOCK (attr) = blk;
19344 break;
19345 case DW_FORM_block1:
7b5a2f43 19346 blk = dwarf_alloc_block (cu);
c906108c
SS
19347 blk->size = read_1_byte (abfd, info_ptr);
19348 info_ptr += 1;
19349 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19350 info_ptr += blk->size;
19351 DW_BLOCK (attr) = blk;
19352 break;
19353 case DW_FORM_data1:
19354 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19355 info_ptr += 1;
19356 break;
19357 case DW_FORM_flag:
19358 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19359 info_ptr += 1;
19360 break;
2dc7f7b3
TT
19361 case DW_FORM_flag_present:
19362 DW_UNSND (attr) = 1;
19363 break;
c906108c
SS
19364 case DW_FORM_sdata:
19365 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19366 info_ptr += bytes_read;
19367 break;
19368 case DW_FORM_udata:
19369 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19370 info_ptr += bytes_read;
19371 break;
19372 case DW_FORM_ref1:
9c541725 19373 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19374 + read_1_byte (abfd, info_ptr));
c906108c
SS
19375 info_ptr += 1;
19376 break;
19377 case DW_FORM_ref2:
9c541725 19378 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19379 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19380 info_ptr += 2;
19381 break;
19382 case DW_FORM_ref4:
9c541725 19383 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19384 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19385 info_ptr += 4;
19386 break;
613e1657 19387 case DW_FORM_ref8:
9c541725 19388 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19389 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19390 info_ptr += 8;
19391 break;
55f1336d 19392 case DW_FORM_ref_sig8:
ac9ec31b 19393 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19394 info_ptr += 8;
19395 break;
c906108c 19396 case DW_FORM_ref_udata:
9c541725 19397 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19398 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19399 info_ptr += bytes_read;
19400 break;
c906108c 19401 case DW_FORM_indirect:
a8329558
KW
19402 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19403 info_ptr += bytes_read;
43988095
JK
19404 if (form == DW_FORM_implicit_const)
19405 {
19406 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19407 info_ptr += bytes_read;
19408 }
19409 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19410 info_ptr);
19411 break;
19412 case DW_FORM_implicit_const:
19413 DW_SND (attr) = implicit_const;
a8329558 19414 break;
336d760d 19415 case DW_FORM_addrx:
3019eac3
DE
19416 case DW_FORM_GNU_addr_index:
19417 if (reader->dwo_file == NULL)
19418 {
19419 /* For now flag a hard error.
19420 Later we can turn this into a complaint. */
19421 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19422 dwarf_form_name (form),
19423 bfd_get_filename (abfd));
19424 }
19425 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19426 info_ptr += bytes_read;
19427 break;
cf532bd1 19428 case DW_FORM_strx:
15f18d14
AT
19429 case DW_FORM_strx1:
19430 case DW_FORM_strx2:
19431 case DW_FORM_strx3:
19432 case DW_FORM_strx4:
3019eac3
DE
19433 case DW_FORM_GNU_str_index:
19434 if (reader->dwo_file == NULL)
19435 {
19436 /* For now flag a hard error.
19437 Later we can turn this into a complaint if warranted. */
19438 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19439 dwarf_form_name (form),
19440 bfd_get_filename (abfd));
19441 }
19442 {
15f18d14
AT
19443 ULONGEST str_index;
19444 if (form == DW_FORM_strx1)
19445 {
19446 str_index = read_1_byte (abfd, info_ptr);
19447 info_ptr += 1;
19448 }
19449 else if (form == DW_FORM_strx2)
19450 {
19451 str_index = read_2_bytes (abfd, info_ptr);
19452 info_ptr += 2;
19453 }
19454 else if (form == DW_FORM_strx3)
19455 {
19456 str_index = read_3_bytes (abfd, info_ptr);
19457 info_ptr += 3;
19458 }
19459 else if (form == DW_FORM_strx4)
19460 {
19461 str_index = read_4_bytes (abfd, info_ptr);
19462 info_ptr += 4;
19463 }
19464 else
19465 {
19466 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19467 info_ptr += bytes_read;
19468 }
342587c4 19469 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19470 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19471 }
19472 break;
c906108c 19473 default:
8a3fe4f8 19474 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19475 dwarf_form_name (form),
19476 bfd_get_filename (abfd));
c906108c 19477 }
28e94949 19478
36586728 19479 /* Super hack. */
7771576e 19480 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19481 attr->form = DW_FORM_GNU_ref_alt;
19482
28e94949
JB
19483 /* We have seen instances where the compiler tried to emit a byte
19484 size attribute of -1 which ended up being encoded as an unsigned
19485 0xffffffff. Although 0xffffffff is technically a valid size value,
19486 an object of this size seems pretty unlikely so we can relatively
19487 safely treat these cases as if the size attribute was invalid and
19488 treat them as zero by default. */
19489 if (attr->name == DW_AT_byte_size
19490 && form == DW_FORM_data4
19491 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19492 {
19493 complaint
b98664d3 19494 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19495 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19496 DW_UNSND (attr) = 0;
19497 }
28e94949 19498
c906108c
SS
19499 return info_ptr;
19500}
19501
a8329558
KW
19502/* Read an attribute described by an abbreviated attribute. */
19503
d521ce57 19504static const gdb_byte *
dee91e82
DE
19505read_attribute (const struct die_reader_specs *reader,
19506 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19507 const gdb_byte *info_ptr)
a8329558
KW
19508{
19509 attr->name = abbrev->name;
43988095
JK
19510 return read_attribute_value (reader, attr, abbrev->form,
19511 abbrev->implicit_const, info_ptr);
a8329558
KW
19512}
19513
0963b4bd 19514/* Read dwarf information from a buffer. */
c906108c
SS
19515
19516static unsigned int
a1855c1d 19517read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19518{
fe1b8b76 19519 return bfd_get_8 (abfd, buf);
c906108c
SS
19520}
19521
19522static int
a1855c1d 19523read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19524{
fe1b8b76 19525 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19526}
19527
19528static unsigned int
a1855c1d 19529read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19530{
fe1b8b76 19531 return bfd_get_16 (abfd, buf);
c906108c
SS
19532}
19533
21ae7a4d 19534static int
a1855c1d 19535read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19536{
19537 return bfd_get_signed_16 (abfd, buf);
19538}
19539
15f18d14
AT
19540static unsigned int
19541read_3_bytes (bfd *abfd, const gdb_byte *buf)
19542{
19543 unsigned int result = 0;
19544 for (int i = 0; i < 3; ++i)
19545 {
19546 unsigned char byte = bfd_get_8 (abfd, buf);
19547 buf++;
19548 result |= ((unsigned int) byte << (i * 8));
19549 }
19550 return result;
19551}
19552
c906108c 19553static unsigned int
a1855c1d 19554read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19555{
fe1b8b76 19556 return bfd_get_32 (abfd, buf);
c906108c
SS
19557}
19558
21ae7a4d 19559static int
a1855c1d 19560read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19561{
19562 return bfd_get_signed_32 (abfd, buf);
19563}
19564
93311388 19565static ULONGEST
a1855c1d 19566read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19567{
fe1b8b76 19568 return bfd_get_64 (abfd, buf);
c906108c
SS
19569}
19570
19571static CORE_ADDR
d521ce57 19572read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19573 unsigned int *bytes_read)
c906108c 19574{
e7c27a73 19575 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19576 CORE_ADDR retval = 0;
19577
107d2387 19578 if (cu_header->signed_addr_p)
c906108c 19579 {
107d2387
AC
19580 switch (cu_header->addr_size)
19581 {
19582 case 2:
fe1b8b76 19583 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19584 break;
19585 case 4:
fe1b8b76 19586 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19587 break;
19588 case 8:
fe1b8b76 19589 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19590 break;
19591 default:
8e65ff28 19592 internal_error (__FILE__, __LINE__,
e2e0b3e5 19593 _("read_address: bad switch, signed [in module %s]"),
659b0389 19594 bfd_get_filename (abfd));
107d2387
AC
19595 }
19596 }
19597 else
19598 {
19599 switch (cu_header->addr_size)
19600 {
19601 case 2:
fe1b8b76 19602 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19603 break;
19604 case 4:
fe1b8b76 19605 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19606 break;
19607 case 8:
fe1b8b76 19608 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19609 break;
19610 default:
8e65ff28 19611 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19612 _("read_address: bad switch, "
19613 "unsigned [in module %s]"),
659b0389 19614 bfd_get_filename (abfd));
107d2387 19615 }
c906108c 19616 }
64367e0a 19617
107d2387
AC
19618 *bytes_read = cu_header->addr_size;
19619 return retval;
c906108c
SS
19620}
19621
f7ef9339 19622/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19623 specification allows the initial length to take up either 4 bytes
19624 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19625 bytes describe the length and all offsets will be 8 bytes in length
19626 instead of 4.
19627
f7ef9339
KB
19628 An older, non-standard 64-bit format is also handled by this
19629 function. The older format in question stores the initial length
19630 as an 8-byte quantity without an escape value. Lengths greater
19631 than 2^32 aren't very common which means that the initial 4 bytes
19632 is almost always zero. Since a length value of zero doesn't make
19633 sense for the 32-bit format, this initial zero can be considered to
19634 be an escape value which indicates the presence of the older 64-bit
19635 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19636 greater than 4GB. If it becomes necessary to handle lengths
19637 somewhat larger than 4GB, we could allow other small values (such
19638 as the non-sensical values of 1, 2, and 3) to also be used as
19639 escape values indicating the presence of the old format.
f7ef9339 19640
917c78fc
MK
19641 The value returned via bytes_read should be used to increment the
19642 relevant pointer after calling read_initial_length().
c764a876 19643
613e1657
KB
19644 [ Note: read_initial_length() and read_offset() are based on the
19645 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19646 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19647 from:
19648
f7ef9339 19649 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19650
613e1657
KB
19651 This document is only a draft and is subject to change. (So beware.)
19652
f7ef9339 19653 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19654 determined empirically by examining 64-bit ELF files produced by
19655 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19656
19657 - Kevin, July 16, 2002
613e1657
KB
19658 ] */
19659
19660static LONGEST
d521ce57 19661read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19662{
fe1b8b76 19663 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19664
dd373385 19665 if (length == 0xffffffff)
613e1657 19666 {
fe1b8b76 19667 length = bfd_get_64 (abfd, buf + 4);
613e1657 19668 *bytes_read = 12;
613e1657 19669 }
dd373385 19670 else if (length == 0)
f7ef9339 19671 {
dd373385 19672 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19673 length = bfd_get_64 (abfd, buf);
f7ef9339 19674 *bytes_read = 8;
f7ef9339 19675 }
613e1657
KB
19676 else
19677 {
19678 *bytes_read = 4;
613e1657
KB
19679 }
19680
c764a876
DE
19681 return length;
19682}
dd373385 19683
c764a876
DE
19684/* Cover function for read_initial_length.
19685 Returns the length of the object at BUF, and stores the size of the
19686 initial length in *BYTES_READ and stores the size that offsets will be in
19687 *OFFSET_SIZE.
19688 If the initial length size is not equivalent to that specified in
19689 CU_HEADER then issue a complaint.
19690 This is useful when reading non-comp-unit headers. */
dd373385 19691
c764a876 19692static LONGEST
d521ce57 19693read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19694 const struct comp_unit_head *cu_header,
19695 unsigned int *bytes_read,
19696 unsigned int *offset_size)
19697{
19698 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19699
19700 gdb_assert (cu_header->initial_length_size == 4
19701 || cu_header->initial_length_size == 8
19702 || cu_header->initial_length_size == 12);
19703
19704 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19705 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19706
c764a876 19707 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19708 return length;
613e1657
KB
19709}
19710
19711/* Read an offset from the data stream. The size of the offset is
917c78fc 19712 given by cu_header->offset_size. */
613e1657
KB
19713
19714static LONGEST
d521ce57
TT
19715read_offset (bfd *abfd, const gdb_byte *buf,
19716 const struct comp_unit_head *cu_header,
891d2f0b 19717 unsigned int *bytes_read)
c764a876
DE
19718{
19719 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19720
c764a876
DE
19721 *bytes_read = cu_header->offset_size;
19722 return offset;
19723}
19724
19725/* Read an offset from the data stream. */
19726
19727static LONGEST
d521ce57 19728read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19729{
19730 LONGEST retval = 0;
19731
c764a876 19732 switch (offset_size)
613e1657
KB
19733 {
19734 case 4:
fe1b8b76 19735 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19736 break;
19737 case 8:
fe1b8b76 19738 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19739 break;
19740 default:
8e65ff28 19741 internal_error (__FILE__, __LINE__,
c764a876 19742 _("read_offset_1: bad switch [in module %s]"),
659b0389 19743 bfd_get_filename (abfd));
613e1657
KB
19744 }
19745
917c78fc 19746 return retval;
613e1657
KB
19747}
19748
d521ce57
TT
19749static const gdb_byte *
19750read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19751{
19752 /* If the size of a host char is 8 bits, we can return a pointer
19753 to the buffer, otherwise we have to copy the data to a buffer
19754 allocated on the temporary obstack. */
4bdf3d34 19755 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19756 return buf;
c906108c
SS
19757}
19758
d521ce57
TT
19759static const char *
19760read_direct_string (bfd *abfd, const gdb_byte *buf,
19761 unsigned int *bytes_read_ptr)
c906108c
SS
19762{
19763 /* If the size of a host char is 8 bits, we can return a pointer
19764 to the string, otherwise we have to copy the string to a buffer
19765 allocated on the temporary obstack. */
4bdf3d34 19766 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19767 if (*buf == '\0')
19768 {
19769 *bytes_read_ptr = 1;
19770 return NULL;
19771 }
d521ce57
TT
19772 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19773 return (const char *) buf;
4bdf3d34
JJ
19774}
19775
43988095
JK
19776/* Return pointer to string at section SECT offset STR_OFFSET with error
19777 reporting strings FORM_NAME and SECT_NAME. */
19778
d521ce57 19779static const char *
ed2dc618
SM
19780read_indirect_string_at_offset_from (struct objfile *objfile,
19781 bfd *abfd, LONGEST str_offset,
43988095
JK
19782 struct dwarf2_section_info *sect,
19783 const char *form_name,
19784 const char *sect_name)
19785{
ed2dc618 19786 dwarf2_read_section (objfile, sect);
43988095
JK
19787 if (sect->buffer == NULL)
19788 error (_("%s used without %s section [in module %s]"),
19789 form_name, sect_name, bfd_get_filename (abfd));
19790 if (str_offset >= sect->size)
19791 error (_("%s pointing outside of %s section [in module %s]"),
19792 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19793 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19794 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19795 return NULL;
43988095
JK
19796 return (const char *) (sect->buffer + str_offset);
19797}
19798
19799/* Return pointer to string at .debug_str offset STR_OFFSET. */
19800
19801static const char *
ed2dc618
SM
19802read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19803 bfd *abfd, LONGEST str_offset)
43988095 19804{
ed2dc618
SM
19805 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19806 abfd, str_offset,
43988095
JK
19807 &dwarf2_per_objfile->str,
19808 "DW_FORM_strp", ".debug_str");
19809}
19810
19811/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19812
19813static const char *
ed2dc618
SM
19814read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19815 bfd *abfd, LONGEST str_offset)
43988095 19816{
ed2dc618
SM
19817 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19818 abfd, str_offset,
43988095
JK
19819 &dwarf2_per_objfile->line_str,
19820 "DW_FORM_line_strp",
19821 ".debug_line_str");
c906108c
SS
19822}
19823
36586728
TT
19824/* Read a string at offset STR_OFFSET in the .debug_str section from
19825 the .dwz file DWZ. Throw an error if the offset is too large. If
19826 the string consists of a single NUL byte, return NULL; otherwise
19827 return a pointer to the string. */
19828
d521ce57 19829static const char *
ed2dc618
SM
19830read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19831 LONGEST str_offset)
36586728 19832{
ed2dc618 19833 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19834
19835 if (dwz->str.buffer == NULL)
19836 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19837 "section [in module %s]"),
00f93c44 19838 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19839 if (str_offset >= dwz->str.size)
19840 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19841 ".debug_str section [in module %s]"),
00f93c44 19842 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19843 gdb_assert (HOST_CHAR_BIT == 8);
19844 if (dwz->str.buffer[str_offset] == '\0')
19845 return NULL;
d521ce57 19846 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19847}
19848
43988095
JK
19849/* Return pointer to string at .debug_str offset as read from BUF.
19850 BUF is assumed to be in a compilation unit described by CU_HEADER.
19851 Return *BYTES_READ_PTR count of bytes read from BUF. */
19852
d521ce57 19853static const char *
ed2dc618
SM
19854read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19855 const gdb_byte *buf,
cf2c3c16
TT
19856 const struct comp_unit_head *cu_header,
19857 unsigned int *bytes_read_ptr)
19858{
19859 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19860
ed2dc618 19861 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19862}
19863
43988095
JK
19864/* Return pointer to string at .debug_line_str offset as read from BUF.
19865 BUF is assumed to be in a compilation unit described by CU_HEADER.
19866 Return *BYTES_READ_PTR count of bytes read from BUF. */
19867
19868static const char *
ed2dc618
SM
19869read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19870 bfd *abfd, const gdb_byte *buf,
43988095
JK
19871 const struct comp_unit_head *cu_header,
19872 unsigned int *bytes_read_ptr)
19873{
19874 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19875
ed2dc618
SM
19876 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19877 str_offset);
43988095
JK
19878}
19879
19880ULONGEST
d521ce57 19881read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19882 unsigned int *bytes_read_ptr)
c906108c 19883{
12df843f 19884 ULONGEST result;
ce5d95e1 19885 unsigned int num_read;
870f88f7 19886 int shift;
c906108c
SS
19887 unsigned char byte;
19888
19889 result = 0;
19890 shift = 0;
19891 num_read = 0;
c906108c
SS
19892 while (1)
19893 {
fe1b8b76 19894 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19895 buf++;
19896 num_read++;
12df843f 19897 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19898 if ((byte & 128) == 0)
19899 {
19900 break;
19901 }
19902 shift += 7;
19903 }
19904 *bytes_read_ptr = num_read;
19905 return result;
19906}
19907
12df843f 19908static LONGEST
d521ce57
TT
19909read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19910 unsigned int *bytes_read_ptr)
c906108c 19911{
4dd1b460 19912 ULONGEST result;
870f88f7 19913 int shift, num_read;
c906108c
SS
19914 unsigned char byte;
19915
19916 result = 0;
19917 shift = 0;
c906108c 19918 num_read = 0;
c906108c
SS
19919 while (1)
19920 {
fe1b8b76 19921 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19922 buf++;
19923 num_read++;
4dd1b460 19924 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19925 shift += 7;
19926 if ((byte & 128) == 0)
19927 {
19928 break;
19929 }
19930 }
77e0b926 19931 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 19932 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
19933 *bytes_read_ptr = num_read;
19934 return result;
19935}
19936
3019eac3
DE
19937/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19938 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19939 ADDR_SIZE is the size of addresses from the CU header. */
19940
19941static CORE_ADDR
ed2dc618
SM
19942read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19943 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19944{
19945 struct objfile *objfile = dwarf2_per_objfile->objfile;
19946 bfd *abfd = objfile->obfd;
19947 const gdb_byte *info_ptr;
19948
19949 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19950 if (dwarf2_per_objfile->addr.buffer == NULL)
19951 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19952 objfile_name (objfile));
3019eac3
DE
19953 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19954 error (_("DW_FORM_addr_index pointing outside of "
19955 ".debug_addr section [in module %s]"),
4262abfb 19956 objfile_name (objfile));
3019eac3
DE
19957 info_ptr = (dwarf2_per_objfile->addr.buffer
19958 + addr_base + addr_index * addr_size);
19959 if (addr_size == 4)
19960 return bfd_get_32 (abfd, info_ptr);
19961 else
19962 return bfd_get_64 (abfd, info_ptr);
19963}
19964
19965/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19966
19967static CORE_ADDR
19968read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19969{
518817b3
SM
19970 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19971 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19972}
19973
19974/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19975
19976static CORE_ADDR
d521ce57 19977read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19978 unsigned int *bytes_read)
19979{
518817b3 19980 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19981 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19982
19983 return read_addr_index (cu, addr_index);
19984}
19985
19986/* Data structure to pass results from dwarf2_read_addr_index_reader
19987 back to dwarf2_read_addr_index. */
19988
19989struct dwarf2_read_addr_index_data
19990{
19991 ULONGEST addr_base;
19992 int addr_size;
19993};
19994
19995/* die_reader_func for dwarf2_read_addr_index. */
19996
19997static void
19998dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19999 const gdb_byte *info_ptr,
3019eac3
DE
20000 struct die_info *comp_unit_die,
20001 int has_children,
20002 void *data)
20003{
20004 struct dwarf2_cu *cu = reader->cu;
20005 struct dwarf2_read_addr_index_data *aidata =
20006 (struct dwarf2_read_addr_index_data *) data;
20007
20008 aidata->addr_base = cu->addr_base;
20009 aidata->addr_size = cu->header.addr_size;
20010}
20011
20012/* Given an index in .debug_addr, fetch the value.
20013 NOTE: This can be called during dwarf expression evaluation,
20014 long after the debug information has been read, and thus per_cu->cu
20015 may no longer exist. */
20016
20017CORE_ADDR
20018dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20019 unsigned int addr_index)
20020{
ed2dc618 20021 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
20022 struct dwarf2_cu *cu = per_cu->cu;
20023 ULONGEST addr_base;
20024 int addr_size;
20025
3019eac3
DE
20026 /* We need addr_base and addr_size.
20027 If we don't have PER_CU->cu, we have to get it.
20028 Nasty, but the alternative is storing the needed info in PER_CU,
20029 which at this point doesn't seem justified: it's not clear how frequently
20030 it would get used and it would increase the size of every PER_CU.
20031 Entry points like dwarf2_per_cu_addr_size do a similar thing
20032 so we're not in uncharted territory here.
20033 Alas we need to be a bit more complicated as addr_base is contained
20034 in the DIE.
20035
20036 We don't need to read the entire CU(/TU).
20037 We just need the header and top level die.
a1b64ce1 20038
3019eac3 20039 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20040 For now we skip this optimization. */
3019eac3
DE
20041
20042 if (cu != NULL)
20043 {
20044 addr_base = cu->addr_base;
20045 addr_size = cu->header.addr_size;
20046 }
20047 else
20048 {
20049 struct dwarf2_read_addr_index_data aidata;
20050
a1b64ce1
DE
20051 /* Note: We can't use init_cutu_and_read_dies_simple here,
20052 we need addr_base. */
58f0c718 20053 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 20054 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20055 addr_base = aidata.addr_base;
20056 addr_size = aidata.addr_size;
20057 }
20058
ed2dc618
SM
20059 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20060 addr_size);
3019eac3
DE
20061}
20062
cf532bd1 20063/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 20064 This is only used by the Fission support. */
3019eac3 20065
d521ce57 20066static const char *
342587c4 20067read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20068{
ed2dc618 20069 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20070 struct dwarf2_per_objfile *dwarf2_per_objfile
20071 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20072 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20073 const char *objf_name = objfile_name (objfile);
3019eac3 20074 bfd *abfd = objfile->obfd;
73869dc2
DE
20075 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20076 struct dwarf2_section_info *str_offsets_section =
20077 &reader->dwo_file->sections.str_offsets;
d521ce57 20078 const gdb_byte *info_ptr;
3019eac3 20079 ULONGEST str_offset;
cf532bd1 20080 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20081
73869dc2
DE
20082 dwarf2_read_section (objfile, str_section);
20083 dwarf2_read_section (objfile, str_offsets_section);
20084 if (str_section->buffer == NULL)
57d63ce2 20085 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20086 " in CU at offset %s [in module %s]"),
20087 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20088 if (str_offsets_section->buffer == NULL)
57d63ce2 20089 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20090 " in CU at offset %s [in module %s]"),
20091 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20092 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20093 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20094 " section in CU at offset %s [in module %s]"),
20095 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20096 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20097 + str_index * cu->header.offset_size);
20098 if (cu->header.offset_size == 4)
20099 str_offset = bfd_get_32 (abfd, info_ptr);
20100 else
20101 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20102 if (str_offset >= str_section->size)
57d63ce2 20103 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20104 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20105 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20106 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20107}
20108
3019eac3
DE
20109/* Return the length of an LEB128 number in BUF. */
20110
20111static int
20112leb128_size (const gdb_byte *buf)
20113{
20114 const gdb_byte *begin = buf;
20115 gdb_byte byte;
20116
20117 while (1)
20118 {
20119 byte = *buf++;
20120 if ((byte & 128) == 0)
20121 return buf - begin;
20122 }
20123}
20124
c906108c 20125static void
e142c38c 20126set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20127{
20128 switch (lang)
20129 {
20130 case DW_LANG_C89:
76bee0cc 20131 case DW_LANG_C99:
0cfd832f 20132 case DW_LANG_C11:
c906108c 20133 case DW_LANG_C:
d1be3247 20134 case DW_LANG_UPC:
e142c38c 20135 cu->language = language_c;
c906108c 20136 break;
9c37b5ae 20137 case DW_LANG_Java:
c906108c 20138 case DW_LANG_C_plus_plus:
0cfd832f
MW
20139 case DW_LANG_C_plus_plus_11:
20140 case DW_LANG_C_plus_plus_14:
e142c38c 20141 cu->language = language_cplus;
c906108c 20142 break;
6aecb9c2
JB
20143 case DW_LANG_D:
20144 cu->language = language_d;
20145 break;
c906108c
SS
20146 case DW_LANG_Fortran77:
20147 case DW_LANG_Fortran90:
b21b22e0 20148 case DW_LANG_Fortran95:
f7de9aab
MW
20149 case DW_LANG_Fortran03:
20150 case DW_LANG_Fortran08:
e142c38c 20151 cu->language = language_fortran;
c906108c 20152 break;
a766d390
DE
20153 case DW_LANG_Go:
20154 cu->language = language_go;
20155 break;
c906108c 20156 case DW_LANG_Mips_Assembler:
e142c38c 20157 cu->language = language_asm;
c906108c
SS
20158 break;
20159 case DW_LANG_Ada83:
8aaf0b47 20160 case DW_LANG_Ada95:
bc5f45f8
JB
20161 cu->language = language_ada;
20162 break;
72019c9c
GM
20163 case DW_LANG_Modula2:
20164 cu->language = language_m2;
20165 break;
fe8e67fd
PM
20166 case DW_LANG_Pascal83:
20167 cu->language = language_pascal;
20168 break;
22566fbd
DJ
20169 case DW_LANG_ObjC:
20170 cu->language = language_objc;
20171 break;
c44af4eb
TT
20172 case DW_LANG_Rust:
20173 case DW_LANG_Rust_old:
20174 cu->language = language_rust;
20175 break;
c906108c
SS
20176 case DW_LANG_Cobol74:
20177 case DW_LANG_Cobol85:
c906108c 20178 default:
e142c38c 20179 cu->language = language_minimal;
c906108c
SS
20180 break;
20181 }
e142c38c 20182 cu->language_defn = language_def (cu->language);
c906108c
SS
20183}
20184
20185/* Return the named attribute or NULL if not there. */
20186
20187static struct attribute *
e142c38c 20188dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20189{
a48e046c 20190 for (;;)
c906108c 20191 {
a48e046c
TT
20192 unsigned int i;
20193 struct attribute *spec = NULL;
20194
20195 for (i = 0; i < die->num_attrs; ++i)
20196 {
20197 if (die->attrs[i].name == name)
20198 return &die->attrs[i];
20199 if (die->attrs[i].name == DW_AT_specification
20200 || die->attrs[i].name == DW_AT_abstract_origin)
20201 spec = &die->attrs[i];
20202 }
20203
20204 if (!spec)
20205 break;
c906108c 20206
f2f0e013 20207 die = follow_die_ref (die, spec, &cu);
f2f0e013 20208 }
c5aa993b 20209
c906108c
SS
20210 return NULL;
20211}
20212
348e048f
DE
20213/* Return the named attribute or NULL if not there,
20214 but do not follow DW_AT_specification, etc.
20215 This is for use in contexts where we're reading .debug_types dies.
20216 Following DW_AT_specification, DW_AT_abstract_origin will take us
20217 back up the chain, and we want to go down. */
20218
20219static struct attribute *
45e58e77 20220dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20221{
20222 unsigned int i;
20223
20224 for (i = 0; i < die->num_attrs; ++i)
20225 if (die->attrs[i].name == name)
20226 return &die->attrs[i];
20227
20228 return NULL;
20229}
20230
7d45c7c3
KB
20231/* Return the string associated with a string-typed attribute, or NULL if it
20232 is either not found or is of an incorrect type. */
20233
20234static const char *
20235dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20236{
20237 struct attribute *attr;
20238 const char *str = NULL;
20239
20240 attr = dwarf2_attr (die, name, cu);
20241
20242 if (attr != NULL)
20243 {
43988095 20244 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20245 || attr->form == DW_FORM_string
cf532bd1 20246 || attr->form == DW_FORM_strx
8fe0f950
AT
20247 || attr->form == DW_FORM_strx1
20248 || attr->form == DW_FORM_strx2
20249 || attr->form == DW_FORM_strx3
20250 || attr->form == DW_FORM_strx4
b3340438 20251 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20252 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20253 str = DW_STRING (attr);
20254 else
b98664d3 20255 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20256 "DIE at %s in module %s"),
20257 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20258 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20259 }
20260
20261 return str;
20262}
20263
a084a2a6 20264/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20265 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20266static const char *
20267dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20268{
20269 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20270 if (dwo_name == nullptr)
20271 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20272 return dwo_name;
20273}
20274
05cf31d1
JB
20275/* Return non-zero iff the attribute NAME is defined for the given DIE,
20276 and holds a non-zero value. This function should only be used for
2dc7f7b3 20277 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20278
20279static int
20280dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20281{
20282 struct attribute *attr = dwarf2_attr (die, name, cu);
20283
20284 return (attr && DW_UNSND (attr));
20285}
20286
3ca72b44 20287static int
e142c38c 20288die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20289{
05cf31d1
JB
20290 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20291 which value is non-zero. However, we have to be careful with
20292 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20293 (via dwarf2_flag_true_p) follows this attribute. So we may
20294 end up accidently finding a declaration attribute that belongs
20295 to a different DIE referenced by the specification attribute,
20296 even though the given DIE does not have a declaration attribute. */
20297 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20298 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20299}
20300
63d06c5c 20301/* Return the die giving the specification for DIE, if there is
f2f0e013 20302 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20303 containing the return value on output. If there is no
20304 specification, but there is an abstract origin, that is
20305 returned. */
63d06c5c
DC
20306
20307static struct die_info *
f2f0e013 20308die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20309{
f2f0e013
DJ
20310 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20311 *spec_cu);
63d06c5c 20312
edb3359d
DJ
20313 if (spec_attr == NULL)
20314 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20315
63d06c5c
DC
20316 if (spec_attr == NULL)
20317 return NULL;
20318 else
f2f0e013 20319 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20320}
c906108c 20321
527f3840
JK
20322/* Stub for free_line_header to match void * callback types. */
20323
20324static void
20325free_line_header_voidp (void *arg)
20326{
9a3c8263 20327 struct line_header *lh = (struct line_header *) arg;
527f3840 20328
fff8551c 20329 delete lh;
527f3840
JK
20330}
20331
fff8551c
PA
20332void
20333line_header::add_include_dir (const char *include_dir)
c906108c 20334{
27e0867f 20335 if (dwarf_line_debug >= 2)
7ba99d21
AT
20336 {
20337 size_t new_size;
20338 if (version >= 5)
20339 new_size = m_include_dirs.size ();
20340 else
20341 new_size = m_include_dirs.size () + 1;
20342 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20343 new_size, include_dir);
20344 }
20345 m_include_dirs.push_back (include_dir);
debd256d 20346}
6e70227d 20347
fff8551c
PA
20348void
20349line_header::add_file_name (const char *name,
ecfb656c 20350 dir_index d_index,
fff8551c
PA
20351 unsigned int mod_time,
20352 unsigned int length)
debd256d 20353{
27e0867f 20354 if (dwarf_line_debug >= 2)
7ba99d21
AT
20355 {
20356 size_t new_size;
20357 if (version >= 5)
20358 new_size = file_names_size ();
20359 else
20360 new_size = file_names_size () + 1;
20361 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20362 new_size, name);
20363 }
20364 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20365}
6e70227d 20366
83769d0b 20367/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20368
20369static struct dwarf2_section_info *
20370get_debug_line_section (struct dwarf2_cu *cu)
20371{
20372 struct dwarf2_section_info *section;
518817b3
SM
20373 struct dwarf2_per_objfile *dwarf2_per_objfile
20374 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20375
20376 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20377 DWO file. */
20378 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20379 section = &cu->dwo_unit->dwo_file->sections.line;
20380 else if (cu->per_cu->is_dwz)
20381 {
ed2dc618 20382 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20383
20384 section = &dwz->line;
20385 }
20386 else
20387 section = &dwarf2_per_objfile->line;
20388
20389 return section;
20390}
20391
43988095
JK
20392/* Read directory or file name entry format, starting with byte of
20393 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20394 entries count and the entries themselves in the described entry
20395 format. */
20396
20397static void
ed2dc618
SM
20398read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20399 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20400 struct line_header *lh,
20401 const struct comp_unit_head *cu_header,
20402 void (*callback) (struct line_header *lh,
20403 const char *name,
ecfb656c 20404 dir_index d_index,
43988095
JK
20405 unsigned int mod_time,
20406 unsigned int length))
20407{
20408 gdb_byte format_count, formati;
20409 ULONGEST data_count, datai;
20410 const gdb_byte *buf = *bufp;
20411 const gdb_byte *format_header_data;
43988095
JK
20412 unsigned int bytes_read;
20413
20414 format_count = read_1_byte (abfd, buf);
20415 buf += 1;
20416 format_header_data = buf;
20417 for (formati = 0; formati < format_count; formati++)
20418 {
20419 read_unsigned_leb128 (abfd, buf, &bytes_read);
20420 buf += bytes_read;
20421 read_unsigned_leb128 (abfd, buf, &bytes_read);
20422 buf += bytes_read;
20423 }
20424
20425 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20426 buf += bytes_read;
20427 for (datai = 0; datai < data_count; datai++)
20428 {
20429 const gdb_byte *format = format_header_data;
20430 struct file_entry fe;
20431
43988095
JK
20432 for (formati = 0; formati < format_count; formati++)
20433 {
ecfb656c 20434 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20435 format += bytes_read;
43988095 20436
ecfb656c 20437 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20438 format += bytes_read;
ecfb656c
PA
20439
20440 gdb::optional<const char *> string;
20441 gdb::optional<unsigned int> uint;
20442
43988095
JK
20443 switch (form)
20444 {
20445 case DW_FORM_string:
ecfb656c 20446 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20447 buf += bytes_read;
20448 break;
20449
20450 case DW_FORM_line_strp:
ed2dc618
SM
20451 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20452 abfd, buf,
ecfb656c
PA
20453 cu_header,
20454 &bytes_read));
43988095
JK
20455 buf += bytes_read;
20456 break;
20457
20458 case DW_FORM_data1:
ecfb656c 20459 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20460 buf += 1;
20461 break;
20462
20463 case DW_FORM_data2:
ecfb656c 20464 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20465 buf += 2;
20466 break;
20467
20468 case DW_FORM_data4:
ecfb656c 20469 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20470 buf += 4;
20471 break;
20472
20473 case DW_FORM_data8:
ecfb656c 20474 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20475 buf += 8;
20476 break;
20477
7ba99d21
AT
20478 case DW_FORM_data16:
20479 /* This is used for MD5, but file_entry does not record MD5s. */
20480 buf += 16;
20481 break;
20482
43988095 20483 case DW_FORM_udata:
ecfb656c 20484 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20485 buf += bytes_read;
20486 break;
20487
20488 case DW_FORM_block:
20489 /* It is valid only for DW_LNCT_timestamp which is ignored by
20490 current GDB. */
20491 break;
20492 }
ecfb656c
PA
20493
20494 switch (content_type)
20495 {
20496 case DW_LNCT_path:
20497 if (string.has_value ())
20498 fe.name = *string;
20499 break;
20500 case DW_LNCT_directory_index:
20501 if (uint.has_value ())
20502 fe.d_index = (dir_index) *uint;
20503 break;
20504 case DW_LNCT_timestamp:
20505 if (uint.has_value ())
20506 fe.mod_time = *uint;
20507 break;
20508 case DW_LNCT_size:
20509 if (uint.has_value ())
20510 fe.length = *uint;
20511 break;
20512 case DW_LNCT_MD5:
20513 break;
20514 default:
b98664d3 20515 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20516 pulongest (content_type));
20517 }
43988095
JK
20518 }
20519
ecfb656c 20520 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20521 }
20522
20523 *bufp = buf;
20524}
20525
debd256d 20526/* Read the statement program header starting at OFFSET in
3019eac3 20527 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20528 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20529 Returns NULL if there is a problem reading the header, e.g., if it
20530 has a version we don't understand.
debd256d
JB
20531
20532 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20533 the returned object point into the dwarf line section buffer,
20534 and must not be freed. */
ae2de4f8 20535
fff8551c 20536static line_header_up
9c541725 20537dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20538{
d521ce57 20539 const gdb_byte *line_ptr;
c764a876 20540 unsigned int bytes_read, offset_size;
debd256d 20541 int i;
d521ce57 20542 const char *cur_dir, *cur_file;
3019eac3
DE
20543 struct dwarf2_section_info *section;
20544 bfd *abfd;
518817b3
SM
20545 struct dwarf2_per_objfile *dwarf2_per_objfile
20546 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20547
36586728 20548 section = get_debug_line_section (cu);
3019eac3
DE
20549 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20550 if (section->buffer == NULL)
debd256d 20551 {
3019eac3 20552 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20553 complaint (_("missing .debug_line.dwo section"));
3019eac3 20554 else
b98664d3 20555 complaint (_("missing .debug_line section"));
debd256d
JB
20556 return 0;
20557 }
20558
fceca515
DE
20559 /* We can't do this until we know the section is non-empty.
20560 Only then do we know we have such a section. */
a32a8923 20561 abfd = get_section_bfd_owner (section);
fceca515 20562
a738430d
MK
20563 /* Make sure that at least there's room for the total_length field.
20564 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20565 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20566 {
4d3c2250 20567 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20568 return 0;
20569 }
20570
fff8551c 20571 line_header_up lh (new line_header ());
debd256d 20572
9c541725 20573 lh->sect_off = sect_off;
527f3840
JK
20574 lh->offset_in_dwz = cu->per_cu->is_dwz;
20575
9c541725 20576 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20577
a738430d 20578 /* Read in the header. */
6e70227d 20579 lh->total_length =
c764a876
DE
20580 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20581 &bytes_read, &offset_size);
debd256d 20582 line_ptr += bytes_read;
7ba99d21
AT
20583
20584 const gdb_byte *start_here = line_ptr;
20585
3019eac3 20586 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20587 {
4d3c2250 20588 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20589 return 0;
20590 }
7ba99d21 20591 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20592 lh->version = read_2_bytes (abfd, line_ptr);
20593 line_ptr += 2;
43988095 20594 if (lh->version > 5)
cd366ee8
DE
20595 {
20596 /* This is a version we don't understand. The format could have
20597 changed in ways we don't handle properly so just punt. */
b98664d3 20598 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20599 return NULL;
20600 }
43988095
JK
20601 if (lh->version >= 5)
20602 {
20603 gdb_byte segment_selector_size;
20604
20605 /* Skip address size. */
20606 read_1_byte (abfd, line_ptr);
20607 line_ptr += 1;
20608
20609 segment_selector_size = read_1_byte (abfd, line_ptr);
20610 line_ptr += 1;
20611 if (segment_selector_size != 0)
20612 {
b98664d3 20613 complaint (_("unsupported segment selector size %u "
43988095
JK
20614 "in .debug_line section"),
20615 segment_selector_size);
20616 return NULL;
20617 }
20618 }
c764a876
DE
20619 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20620 line_ptr += offset_size;
7ba99d21 20621 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20622 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20623 line_ptr += 1;
2dc7f7b3
TT
20624 if (lh->version >= 4)
20625 {
20626 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20627 line_ptr += 1;
20628 }
20629 else
20630 lh->maximum_ops_per_instruction = 1;
20631
20632 if (lh->maximum_ops_per_instruction == 0)
20633 {
20634 lh->maximum_ops_per_instruction = 1;
b98664d3 20635 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20636 "in `.debug_line' section"));
2dc7f7b3
TT
20637 }
20638
debd256d
JB
20639 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20640 line_ptr += 1;
20641 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20642 line_ptr += 1;
20643 lh->line_range = read_1_byte (abfd, line_ptr);
20644 line_ptr += 1;
20645 lh->opcode_base = read_1_byte (abfd, line_ptr);
20646 line_ptr += 1;
fff8551c 20647 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20648
20649 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20650 for (i = 1; i < lh->opcode_base; ++i)
20651 {
20652 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20653 line_ptr += 1;
20654 }
20655
43988095 20656 if (lh->version >= 5)
debd256d 20657 {
43988095 20658 /* Read directory table. */
ed2dc618
SM
20659 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20660 &cu->header,
b926417a 20661 [] (struct line_header *header, const char *name,
ecfb656c 20662 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20663 unsigned int length)
20664 {
b926417a 20665 header->add_include_dir (name);
fff8551c 20666 });
debd256d 20667
43988095 20668 /* Read file name table. */
ed2dc618
SM
20669 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20670 &cu->header,
b926417a 20671 [] (struct line_header *header, const char *name,
ecfb656c 20672 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20673 unsigned int length)
20674 {
b926417a 20675 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20676 });
43988095
JK
20677 }
20678 else
debd256d 20679 {
43988095
JK
20680 /* Read directory table. */
20681 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20682 {
20683 line_ptr += bytes_read;
fff8551c 20684 lh->add_include_dir (cur_dir);
43988095 20685 }
debd256d
JB
20686 line_ptr += bytes_read;
20687
43988095
JK
20688 /* Read file name table. */
20689 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20690 {
ecfb656c
PA
20691 unsigned int mod_time, length;
20692 dir_index d_index;
43988095
JK
20693
20694 line_ptr += bytes_read;
ecfb656c 20695 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20696 line_ptr += bytes_read;
20697 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20698 line_ptr += bytes_read;
20699 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20700 line_ptr += bytes_read;
20701
ecfb656c 20702 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20703 }
20704 line_ptr += bytes_read;
debd256d 20705 }
debd256d 20706
3019eac3 20707 if (line_ptr > (section->buffer + section->size))
b98664d3 20708 complaint (_("line number info header doesn't "
3e43a32a 20709 "fit in `.debug_line' section"));
debd256d 20710
debd256d
JB
20711 return lh;
20712}
c906108c 20713
c6da4cef 20714/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20715 Return the file name of the psymtab for the given file_entry.
c6da4cef 20716 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20717 If space for the result is malloc'd, *NAME_HOLDER will be set.
20718 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20719
d521ce57 20720static const char *
7ba99d21 20721psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
c6da4cef 20722 const struct partial_symtab *pst,
c89b44cd
TT
20723 const char *comp_dir,
20724 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20725{
d521ce57
TT
20726 const char *include_name = fe.name;
20727 const char *include_name_to_compare = include_name;
72b9f47f 20728 const char *pst_filename;
c6da4cef
DE
20729 int file_is_pst;
20730
8c43009f 20731 const char *dir_name = fe.include_dir (lh);
c6da4cef 20732
c89b44cd 20733 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20734 if (!IS_ABSOLUTE_PATH (include_name)
20735 && (dir_name != NULL || comp_dir != NULL))
20736 {
20737 /* Avoid creating a duplicate psymtab for PST.
20738 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20739 Before we do the comparison, however, we need to account
20740 for DIR_NAME and COMP_DIR.
20741 First prepend dir_name (if non-NULL). If we still don't
20742 have an absolute path prepend comp_dir (if non-NULL).
20743 However, the directory we record in the include-file's
20744 psymtab does not contain COMP_DIR (to match the
20745 corresponding symtab(s)).
20746
20747 Example:
20748
20749 bash$ cd /tmp
20750 bash$ gcc -g ./hello.c
20751 include_name = "hello.c"
20752 dir_name = "."
20753 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20754 DW_AT_name = "./hello.c"
20755
20756 */
c6da4cef
DE
20757
20758 if (dir_name != NULL)
20759 {
c89b44cd
TT
20760 name_holder->reset (concat (dir_name, SLASH_STRING,
20761 include_name, (char *) NULL));
20762 include_name = name_holder->get ();
c6da4cef 20763 include_name_to_compare = include_name;
c6da4cef
DE
20764 }
20765 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20766 {
c89b44cd
TT
20767 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20768 include_name, (char *) NULL));
20769 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20770 }
20771 }
20772
20773 pst_filename = pst->filename;
c89b44cd 20774 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20775 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20776 {
c89b44cd
TT
20777 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20778 pst_filename, (char *) NULL));
20779 pst_filename = copied_name.get ();
c6da4cef
DE
20780 }
20781
1e3fad37 20782 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20783
c6da4cef
DE
20784 if (file_is_pst)
20785 return NULL;
20786 return include_name;
20787}
20788
d9b3de22
DE
20789/* State machine to track the state of the line number program. */
20790
6f77053d 20791class lnp_state_machine
d9b3de22 20792{
6f77053d
PA
20793public:
20794 /* Initialize a machine state for the start of a line number
20795 program. */
804d2729
TT
20796 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20797 bool record_lines_p);
6f77053d 20798
8c43009f
PA
20799 file_entry *current_file ()
20800 {
20801 /* lh->file_names is 0-based, but the file name numbers in the
20802 statement program are 1-based. */
6f77053d
PA
20803 return m_line_header->file_name_at (m_file);
20804 }
20805
20806 /* Record the line in the state machine. END_SEQUENCE is true if
20807 we're processing the end of a sequence. */
20808 void record_line (bool end_sequence);
20809
7ab6656f
OJ
20810 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20811 nop-out rest of the lines in this sequence. */
6f77053d
PA
20812 void check_line_address (struct dwarf2_cu *cu,
20813 const gdb_byte *line_ptr,
7ab6656f 20814 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20815
20816 void handle_set_discriminator (unsigned int discriminator)
20817 {
20818 m_discriminator = discriminator;
20819 m_line_has_non_zero_discriminator |= discriminator != 0;
20820 }
20821
20822 /* Handle DW_LNE_set_address. */
20823 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20824 {
20825 m_op_index = 0;
20826 address += baseaddr;
20827 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20828 }
20829
20830 /* Handle DW_LNS_advance_pc. */
20831 void handle_advance_pc (CORE_ADDR adjust);
20832
20833 /* Handle a special opcode. */
20834 void handle_special_opcode (unsigned char op_code);
20835
20836 /* Handle DW_LNS_advance_line. */
20837 void handle_advance_line (int line_delta)
20838 {
20839 advance_line (line_delta);
20840 }
20841
20842 /* Handle DW_LNS_set_file. */
20843 void handle_set_file (file_name_index file);
20844
20845 /* Handle DW_LNS_negate_stmt. */
20846 void handle_negate_stmt ()
20847 {
20848 m_is_stmt = !m_is_stmt;
20849 }
20850
20851 /* Handle DW_LNS_const_add_pc. */
20852 void handle_const_add_pc ();
20853
20854 /* Handle DW_LNS_fixed_advance_pc. */
20855 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20856 {
20857 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20858 m_op_index = 0;
20859 }
20860
20861 /* Handle DW_LNS_copy. */
20862 void handle_copy ()
20863 {
20864 record_line (false);
20865 m_discriminator = 0;
20866 }
20867
20868 /* Handle DW_LNE_end_sequence. */
20869 void handle_end_sequence ()
20870 {
804d2729 20871 m_currently_recording_lines = true;
6f77053d
PA
20872 }
20873
20874private:
20875 /* Advance the line by LINE_DELTA. */
20876 void advance_line (int line_delta)
20877 {
20878 m_line += line_delta;
20879
20880 if (line_delta != 0)
20881 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20882 }
20883
804d2729
TT
20884 struct dwarf2_cu *m_cu;
20885
6f77053d
PA
20886 gdbarch *m_gdbarch;
20887
20888 /* True if we're recording lines.
20889 Otherwise we're building partial symtabs and are just interested in
20890 finding include files mentioned by the line number program. */
20891 bool m_record_lines_p;
20892
8c43009f 20893 /* The line number header. */
6f77053d 20894 line_header *m_line_header;
8c43009f 20895
6f77053d
PA
20896 /* These are part of the standard DWARF line number state machine,
20897 and initialized according to the DWARF spec. */
d9b3de22 20898
6f77053d 20899 unsigned char m_op_index = 0;
7ba99d21
AT
20900 /* The line table index of the current file. */
20901 file_name_index m_file = 1;
6f77053d
PA
20902 unsigned int m_line = 1;
20903
20904 /* These are initialized in the constructor. */
20905
20906 CORE_ADDR m_address;
20907 bool m_is_stmt;
20908 unsigned int m_discriminator;
d9b3de22
DE
20909
20910 /* Additional bits of state we need to track. */
20911
20912 /* The last file that we called dwarf2_start_subfile for.
20913 This is only used for TLLs. */
6f77053d 20914 unsigned int m_last_file = 0;
d9b3de22 20915 /* The last file a line number was recorded for. */
6f77053d 20916 struct subfile *m_last_subfile = NULL;
d9b3de22 20917
804d2729
TT
20918 /* When true, record the lines we decode. */
20919 bool m_currently_recording_lines = false;
d9b3de22
DE
20920
20921 /* The last line number that was recorded, used to coalesce
20922 consecutive entries for the same line. This can happen, for
20923 example, when discriminators are present. PR 17276. */
6f77053d
PA
20924 unsigned int m_last_line = 0;
20925 bool m_line_has_non_zero_discriminator = false;
8c43009f 20926};
d9b3de22 20927
6f77053d
PA
20928void
20929lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20930{
20931 CORE_ADDR addr_adj = (((m_op_index + adjust)
20932 / m_line_header->maximum_ops_per_instruction)
20933 * m_line_header->minimum_instruction_length);
20934 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20935 m_op_index = ((m_op_index + adjust)
20936 % m_line_header->maximum_ops_per_instruction);
20937}
d9b3de22 20938
6f77053d
PA
20939void
20940lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20941{
6f77053d
PA
20942 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20943 CORE_ADDR addr_adj = (((m_op_index
20944 + (adj_opcode / m_line_header->line_range))
20945 / m_line_header->maximum_ops_per_instruction)
20946 * m_line_header->minimum_instruction_length);
20947 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20948 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20949 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20950
6f77053d
PA
20951 int line_delta = (m_line_header->line_base
20952 + (adj_opcode % m_line_header->line_range));
20953 advance_line (line_delta);
20954 record_line (false);
20955 m_discriminator = 0;
20956}
d9b3de22 20957
6f77053d
PA
20958void
20959lnp_state_machine::handle_set_file (file_name_index file)
20960{
20961 m_file = file;
20962
20963 const file_entry *fe = current_file ();
20964 if (fe == NULL)
20965 dwarf2_debug_line_missing_file_complaint ();
20966 else if (m_record_lines_p)
20967 {
20968 const char *dir = fe->include_dir (m_line_header);
20969
c24bdb02 20970 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20971 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20972 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20973 }
20974}
20975
20976void
20977lnp_state_machine::handle_const_add_pc ()
20978{
20979 CORE_ADDR adjust
20980 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20981
20982 CORE_ADDR addr_adj
20983 = (((m_op_index + adjust)
20984 / m_line_header->maximum_ops_per_instruction)
20985 * m_line_header->minimum_instruction_length);
20986
20987 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20988 m_op_index = ((m_op_index + adjust)
20989 % m_line_header->maximum_ops_per_instruction);
20990}
d9b3de22 20991
a05a36a5
DE
20992/* Return non-zero if we should add LINE to the line number table.
20993 LINE is the line to add, LAST_LINE is the last line that was added,
20994 LAST_SUBFILE is the subfile for LAST_LINE.
20995 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20996 had a non-zero discriminator.
20997
20998 We have to be careful in the presence of discriminators.
20999 E.g., for this line:
21000
21001 for (i = 0; i < 100000; i++);
21002
21003 clang can emit four line number entries for that one line,
21004 each with a different discriminator.
21005 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21006
21007 However, we want gdb to coalesce all four entries into one.
21008 Otherwise the user could stepi into the middle of the line and
21009 gdb would get confused about whether the pc really was in the
21010 middle of the line.
21011
21012 Things are further complicated by the fact that two consecutive
21013 line number entries for the same line is a heuristic used by gcc
21014 to denote the end of the prologue. So we can't just discard duplicate
21015 entries, we have to be selective about it. The heuristic we use is
21016 that we only collapse consecutive entries for the same line if at least
21017 one of those entries has a non-zero discriminator. PR 17276.
21018
21019 Note: Addresses in the line number state machine can never go backwards
21020 within one sequence, thus this coalescing is ok. */
21021
21022static int
804d2729
TT
21023dwarf_record_line_p (struct dwarf2_cu *cu,
21024 unsigned int line, unsigned int last_line,
a05a36a5
DE
21025 int line_has_non_zero_discriminator,
21026 struct subfile *last_subfile)
21027{
c24bdb02 21028 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21029 return 1;
21030 if (line != last_line)
21031 return 1;
21032 /* Same line for the same file that we've seen already.
21033 As a last check, for pr 17276, only record the line if the line
21034 has never had a non-zero discriminator. */
21035 if (!line_has_non_zero_discriminator)
21036 return 1;
21037 return 0;
21038}
21039
804d2729
TT
21040/* Use the CU's builder to record line number LINE beginning at
21041 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21042
21043static void
d9b3de22
DE
21044dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21045 unsigned int line, CORE_ADDR address,
804d2729 21046 struct dwarf2_cu *cu)
252a6764
DE
21047{
21048 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21049
27e0867f
DE
21050 if (dwarf_line_debug)
21051 {
21052 fprintf_unfiltered (gdb_stdlog,
21053 "Recording line %u, file %s, address %s\n",
21054 line, lbasename (subfile->name),
21055 paddress (gdbarch, address));
21056 }
21057
804d2729 21058 if (cu != nullptr)
c24bdb02 21059 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21060}
21061
21062/* Subroutine of dwarf_decode_lines_1 to simplify it.
21063 Mark the end of a set of line number records.
d9b3de22 21064 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21065 If SUBFILE is NULL the request is ignored. */
21066
21067static void
21068dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21069 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21070{
27e0867f
DE
21071 if (subfile == NULL)
21072 return;
21073
21074 if (dwarf_line_debug)
21075 {
21076 fprintf_unfiltered (gdb_stdlog,
21077 "Finishing current line, file %s, address %s\n",
21078 lbasename (subfile->name),
21079 paddress (gdbarch, address));
21080 }
21081
804d2729 21082 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21083}
21084
6f77053d
PA
21085void
21086lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21087{
d9b3de22
DE
21088 if (dwarf_line_debug)
21089 {
21090 fprintf_unfiltered (gdb_stdlog,
21091 "Processing actual line %u: file %u,"
21092 " address %s, is_stmt %u, discrim %u\n",
7ba99d21 21093 m_line, m_file,
6f77053d
PA
21094 paddress (m_gdbarch, m_address),
21095 m_is_stmt, m_discriminator);
d9b3de22
DE
21096 }
21097
6f77053d 21098 file_entry *fe = current_file ();
8c43009f
PA
21099
21100 if (fe == NULL)
d9b3de22
DE
21101 dwarf2_debug_line_missing_file_complaint ();
21102 /* For now we ignore lines not starting on an instruction boundary.
21103 But not when processing end_sequence for compatibility with the
21104 previous version of the code. */
6f77053d 21105 else if (m_op_index == 0 || end_sequence)
d9b3de22 21106 {
8c43009f 21107 fe->included_p = 1;
c258c396 21108 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21109 {
c24bdb02 21110 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21111 || end_sequence)
d9b3de22 21112 {
804d2729
TT
21113 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21114 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21115 }
21116
21117 if (!end_sequence)
21118 {
804d2729 21119 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21120 m_line_has_non_zero_discriminator,
21121 m_last_subfile))
d9b3de22 21122 {
c24bdb02 21123 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21124 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21125 builder->get_current_subfile (),
6f77053d 21126 m_line, m_address,
804d2729 21127 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21128 }
c24bdb02 21129 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21130 m_last_line = m_line;
d9b3de22
DE
21131 }
21132 }
21133 }
21134}
21135
804d2729
TT
21136lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21137 line_header *lh, bool record_lines_p)
d9b3de22 21138{
804d2729 21139 m_cu = cu;
6f77053d
PA
21140 m_gdbarch = arch;
21141 m_record_lines_p = record_lines_p;
21142 m_line_header = lh;
d9b3de22 21143
804d2729 21144 m_currently_recording_lines = true;
d9b3de22 21145
d9b3de22
DE
21146 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21147 was a line entry for it so that the backend has a chance to adjust it
21148 and also record it in case it needs it. This is currently used by MIPS
21149 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21150 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21151 m_is_stmt = lh->default_is_stmt;
21152 m_discriminator = 0;
252a6764
DE
21153}
21154
6f77053d
PA
21155void
21156lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21157 const gdb_byte *line_ptr,
7ab6656f 21158 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21159{
7ab6656f
OJ
21160 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21161 the pc range of the CU. However, we restrict the test to only ADDRESS
21162 values of zero to preserve GDB's previous behaviour which is to handle
21163 the specific case of a function being GC'd by the linker. */
924c2928 21164
7ab6656f 21165 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21166 {
21167 /* This line table is for a function which has been
21168 GCd by the linker. Ignore it. PR gdb/12528 */
21169
518817b3 21170 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21171 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21172
b98664d3 21173 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21174 line_offset, objfile_name (objfile));
804d2729
TT
21175 m_currently_recording_lines = false;
21176 /* Note: m_currently_recording_lines is left as false until we see
21177 DW_LNE_end_sequence. */
924c2928
DE
21178 }
21179}
21180
f3f5162e 21181/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21182 Process the line number information in LH.
21183 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21184 program in order to set included_p for every referenced header. */
debd256d 21185
c906108c 21186static void
43f3e411
DE
21187dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21188 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21189{
d521ce57
TT
21190 const gdb_byte *line_ptr, *extended_end;
21191 const gdb_byte *line_end;
a8c50c1f 21192 unsigned int bytes_read, extended_len;
699ca60a 21193 unsigned char op_code, extended_op;
e142c38c 21194 CORE_ADDR baseaddr;
518817b3 21195 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21196 bfd *abfd = objfile->obfd;
fbf65064 21197 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21198 /* True if we're recording line info (as opposed to building partial
21199 symtabs and just interested in finding include files mentioned by
21200 the line number program). */
21201 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21202
21203 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21204
debd256d
JB
21205 line_ptr = lh->statement_program_start;
21206 line_end = lh->statement_program_end;
c906108c
SS
21207
21208 /* Read the statement sequences until there's nothing left. */
21209 while (line_ptr < line_end)
21210 {
6f77053d
PA
21211 /* The DWARF line number program state machine. Reset the state
21212 machine at the start of each sequence. */
804d2729 21213 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21214 bool end_sequence = false;
d9b3de22 21215
8c43009f 21216 if (record_lines_p)
c906108c 21217 {
8c43009f
PA
21218 /* Start a subfile for the current file of the state
21219 machine. */
21220 const file_entry *fe = state_machine.current_file ();
21221
21222 if (fe != NULL)
804d2729 21223 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21224 }
21225
a738430d 21226 /* Decode the table. */
d9b3de22 21227 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21228 {
21229 op_code = read_1_byte (abfd, line_ptr);
21230 line_ptr += 1;
9aa1fe7e 21231
debd256d 21232 if (op_code >= lh->opcode_base)
6e70227d 21233 {
8e07a239 21234 /* Special opcode. */
6f77053d 21235 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21236 }
21237 else switch (op_code)
c906108c
SS
21238 {
21239 case DW_LNS_extended_op:
3e43a32a
MS
21240 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21241 &bytes_read);
473b7be6 21242 line_ptr += bytes_read;
a8c50c1f 21243 extended_end = line_ptr + extended_len;
c906108c
SS
21244 extended_op = read_1_byte (abfd, line_ptr);
21245 line_ptr += 1;
21246 switch (extended_op)
21247 {
21248 case DW_LNE_end_sequence:
6f77053d
PA
21249 state_machine.handle_end_sequence ();
21250 end_sequence = true;
c906108c
SS
21251 break;
21252 case DW_LNE_set_address:
d9b3de22
DE
21253 {
21254 CORE_ADDR address
21255 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21256 line_ptr += bytes_read;
6f77053d
PA
21257
21258 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21259 lowpc - baseaddr, address);
6f77053d 21260 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21261 }
c906108c
SS
21262 break;
21263 case DW_LNE_define_file:
debd256d 21264 {
d521ce57 21265 const char *cur_file;
ecfb656c
PA
21266 unsigned int mod_time, length;
21267 dir_index dindex;
6e70227d 21268
3e43a32a
MS
21269 cur_file = read_direct_string (abfd, line_ptr,
21270 &bytes_read);
debd256d 21271 line_ptr += bytes_read;
ecfb656c 21272 dindex = (dir_index)
debd256d
JB
21273 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21274 line_ptr += bytes_read;
21275 mod_time =
21276 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21277 line_ptr += bytes_read;
21278 length =
21279 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21280 line_ptr += bytes_read;
ecfb656c 21281 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21282 }
c906108c 21283 break;
d0c6ba3d 21284 case DW_LNE_set_discriminator:
6f77053d
PA
21285 {
21286 /* The discriminator is not interesting to the
21287 debugger; just ignore it. We still need to
21288 check its value though:
21289 if there are consecutive entries for the same
21290 (non-prologue) line we want to coalesce them.
21291 PR 17276. */
21292 unsigned int discr
21293 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21294 line_ptr += bytes_read;
21295
21296 state_machine.handle_set_discriminator (discr);
21297 }
d0c6ba3d 21298 break;
c906108c 21299 default:
b98664d3 21300 complaint (_("mangled .debug_line section"));
debd256d 21301 return;
c906108c 21302 }
a8c50c1f
DJ
21303 /* Make sure that we parsed the extended op correctly. If e.g.
21304 we expected a different address size than the producer used,
21305 we may have read the wrong number of bytes. */
21306 if (line_ptr != extended_end)
21307 {
b98664d3 21308 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21309 return;
21310 }
c906108c
SS
21311 break;
21312 case DW_LNS_copy:
6f77053d 21313 state_machine.handle_copy ();
c906108c
SS
21314 break;
21315 case DW_LNS_advance_pc:
2dc7f7b3
TT
21316 {
21317 CORE_ADDR adjust
21318 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21319 line_ptr += bytes_read;
6f77053d
PA
21320
21321 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21322 }
c906108c
SS
21323 break;
21324 case DW_LNS_advance_line:
a05a36a5
DE
21325 {
21326 int line_delta
21327 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21328 line_ptr += bytes_read;
6f77053d
PA
21329
21330 state_machine.handle_advance_line (line_delta);
a05a36a5 21331 }
c906108c
SS
21332 break;
21333 case DW_LNS_set_file:
d9b3de22 21334 {
6f77053d 21335 file_name_index file
ecfb656c
PA
21336 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21337 &bytes_read);
d9b3de22 21338 line_ptr += bytes_read;
8c43009f 21339
6f77053d 21340 state_machine.handle_set_file (file);
d9b3de22 21341 }
c906108c
SS
21342 break;
21343 case DW_LNS_set_column:
0ad93d4f 21344 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21345 line_ptr += bytes_read;
21346 break;
21347 case DW_LNS_negate_stmt:
6f77053d 21348 state_machine.handle_negate_stmt ();
c906108c
SS
21349 break;
21350 case DW_LNS_set_basic_block:
c906108c 21351 break;
c2c6d25f
JM
21352 /* Add to the address register of the state machine the
21353 address increment value corresponding to special opcode
a738430d
MK
21354 255. I.e., this value is scaled by the minimum
21355 instruction length since special opcode 255 would have
b021a221 21356 scaled the increment. */
c906108c 21357 case DW_LNS_const_add_pc:
6f77053d 21358 state_machine.handle_const_add_pc ();
c906108c
SS
21359 break;
21360 case DW_LNS_fixed_advance_pc:
3e29f34a 21361 {
6f77053d 21362 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21363 line_ptr += 2;
6f77053d
PA
21364
21365 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21366 }
c906108c 21367 break;
9aa1fe7e 21368 default:
a738430d
MK
21369 {
21370 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21371 int i;
a738430d 21372
debd256d 21373 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21374 {
21375 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21376 line_ptr += bytes_read;
21377 }
21378 }
c906108c
SS
21379 }
21380 }
d9b3de22
DE
21381
21382 if (!end_sequence)
21383 dwarf2_debug_line_missing_end_sequence_complaint ();
21384
21385 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21386 in which case we still finish recording the last line). */
6f77053d 21387 state_machine.record_line (true);
c906108c 21388 }
f3f5162e
DE
21389}
21390
21391/* Decode the Line Number Program (LNP) for the given line_header
21392 structure and CU. The actual information extracted and the type
21393 of structures created from the LNP depends on the value of PST.
21394
21395 1. If PST is NULL, then this procedure uses the data from the program
21396 to create all necessary symbol tables, and their linetables.
21397
21398 2. If PST is not NULL, this procedure reads the program to determine
21399 the list of files included by the unit represented by PST, and
21400 builds all the associated partial symbol tables.
21401
21402 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21403 It is used for relative paths in the line table.
21404 NOTE: When processing partial symtabs (pst != NULL),
21405 comp_dir == pst->dirname.
21406
21407 NOTE: It is important that psymtabs have the same file name (via strcmp)
21408 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21409 symtab we don't use it in the name of the psymtabs we create.
21410 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21411 A good testcase for this is mb-inline.exp.
21412
527f3840
JK
21413 LOWPC is the lowest address in CU (or 0 if not known).
21414
21415 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21416 for its PC<->lines mapping information. Otherwise only the filename
21417 table is read in. */
f3f5162e
DE
21418
21419static void
21420dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21421 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21422 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21423{
518817b3 21424 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21425 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21426
527f3840
JK
21427 if (decode_mapping)
21428 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21429
21430 if (decode_for_pst_p)
21431 {
aaa75496
JB
21432 /* Now that we're done scanning the Line Header Program, we can
21433 create the psymtab of each included file. */
7ba99d21
AT
21434 for (auto &file_entry : lh->file_names ())
21435 if (file_entry.included_p == 1)
aaa75496 21436 {
c89b44cd 21437 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21438 const char *include_name =
7ba99d21
AT
21439 psymtab_include_file_name (lh, file_entry, pst,
21440 comp_dir, &name_holder);
c6da4cef 21441 if (include_name != NULL)
aaa75496
JB
21442 dwarf2_create_include_psymtab (include_name, pst, objfile);
21443 }
21444 }
cb1df416
DJ
21445 else
21446 {
21447 /* Make sure a symtab is created for every file, even files
21448 which contain only variables (i.e. no code with associated
21449 line numbers). */
c24bdb02
KS
21450 buildsym_compunit *builder = cu->get_builder ();
21451 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21452
7ba99d21 21453 for (auto &fe : lh->file_names ())
cb1df416 21454 {
804d2729 21455 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21456 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21457 {
c24bdb02 21458 builder->get_current_subfile ()->symtab
804d2729 21459 = allocate_symtab (cust,
c24bdb02 21460 builder->get_current_subfile ()->name);
43f3e411 21461 }
c24bdb02 21462 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21463 }
21464 }
c906108c
SS
21465}
21466
21467/* Start a subfile for DWARF. FILENAME is the name of the file and
21468 DIRNAME the name of the source directory which contains FILENAME
4d663531 21469 or NULL if not known.
c906108c
SS
21470 This routine tries to keep line numbers from identical absolute and
21471 relative file names in a common subfile.
21472
21473 Using the `list' example from the GDB testsuite, which resides in
21474 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21475 of /srcdir/list0.c yields the following debugging information for list0.c:
21476
c5aa993b 21477 DW_AT_name: /srcdir/list0.c
4d663531 21478 DW_AT_comp_dir: /compdir
357e46e7 21479 files.files[0].name: list0.h
c5aa993b 21480 files.files[0].dir: /srcdir
357e46e7 21481 files.files[1].name: list0.c
c5aa993b 21482 files.files[1].dir: /srcdir
c906108c
SS
21483
21484 The line number information for list0.c has to end up in a single
4f1520fb
FR
21485 subfile, so that `break /srcdir/list0.c:1' works as expected.
21486 start_subfile will ensure that this happens provided that we pass the
21487 concatenation of files.files[1].dir and files.files[1].name as the
21488 subfile's name. */
c906108c
SS
21489
21490static void
804d2729
TT
21491dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21492 const char *dirname)
c906108c 21493{
d521ce57 21494 char *copy = NULL;
4f1520fb 21495
4d663531 21496 /* In order not to lose the line information directory,
4f1520fb
FR
21497 we concatenate it to the filename when it makes sense.
21498 Note that the Dwarf3 standard says (speaking of filenames in line
21499 information): ``The directory index is ignored for file names
21500 that represent full path names''. Thus ignoring dirname in the
21501 `else' branch below isn't an issue. */
c906108c 21502
d5166ae1 21503 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21504 {
21505 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21506 filename = copy;
21507 }
c906108c 21508
c24bdb02 21509 cu->get_builder ()->start_subfile (filename);
4f1520fb 21510
d521ce57
TT
21511 if (copy != NULL)
21512 xfree (copy);
c906108c
SS
21513}
21514
804d2729
TT
21515/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21516 buildsym_compunit constructor. */
f4dc4d17 21517
c24bdb02
KS
21518struct compunit_symtab *
21519dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21520 CORE_ADDR low_pc)
f4dc4d17 21521{
c24bdb02 21522 gdb_assert (m_builder == nullptr);
43f3e411 21523
c24bdb02
KS
21524 m_builder.reset (new struct buildsym_compunit
21525 (per_cu->dwarf2_per_objfile->objfile,
21526 name, comp_dir, language, low_pc));
93b8bea4 21527
c24bdb02 21528 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21529
c24bdb02
KS
21530 get_builder ()->record_debugformat ("DWARF 2");
21531 get_builder ()->record_producer (producer);
f4dc4d17 21532
c24bdb02 21533 processing_has_namespace_info = false;
43f3e411 21534
c24bdb02 21535 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21536}
21537
4c2df51b
DJ
21538static void
21539var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21540 struct dwarf2_cu *cu)
4c2df51b 21541{
518817b3 21542 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21543 struct comp_unit_head *cu_header = &cu->header;
21544
4c2df51b
DJ
21545 /* NOTE drow/2003-01-30: There used to be a comment and some special
21546 code here to turn a symbol with DW_AT_external and a
21547 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21548 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21549 with some versions of binutils) where shared libraries could have
21550 relocations against symbols in their debug information - the
21551 minimal symbol would have the right address, but the debug info
21552 would not. It's no longer necessary, because we will explicitly
21553 apply relocations when we read in the debug information now. */
21554
21555 /* A DW_AT_location attribute with no contents indicates that a
21556 variable has been optimized away. */
21557 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21558 {
f1e6e072 21559 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21560 return;
21561 }
21562
21563 /* Handle one degenerate form of location expression specially, to
21564 preserve GDB's previous behavior when section offsets are
336d760d
AT
21565 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21566 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21567
21568 if (attr_form_is_block (attr)
3019eac3
DE
21569 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21570 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21571 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21572 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21573 && (DW_BLOCK (attr)->size
21574 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21575 {
891d2f0b 21576 unsigned int dummy;
4c2df51b 21577
3019eac3 21578 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21579 SET_SYMBOL_VALUE_ADDRESS (sym,
21580 read_address (objfile->obfd,
21581 DW_BLOCK (attr)->data + 1,
21582 cu, &dummy));
3019eac3 21583 else
38583298
TT
21584 SET_SYMBOL_VALUE_ADDRESS
21585 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21586 &dummy));
f1e6e072 21587 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21588 fixup_symbol_section (sym, objfile);
38583298
TT
21589 SET_SYMBOL_VALUE_ADDRESS (sym,
21590 SYMBOL_VALUE_ADDRESS (sym)
21591 + ANOFFSET (objfile->section_offsets,
21592 SYMBOL_SECTION (sym)));
4c2df51b
DJ
21593 return;
21594 }
21595
21596 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21597 expression evaluator, and use LOC_COMPUTED only when necessary
21598 (i.e. when the value of a register or memory location is
21599 referenced, or a thread-local block, etc.). Then again, it might
21600 not be worthwhile. I'm assuming that it isn't unless performance
21601 or memory numbers show me otherwise. */
21602
f1e6e072 21603 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21604
f1e6e072 21605 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21606 cu->has_loclist = true;
4c2df51b
DJ
21607}
21608
c906108c
SS
21609/* Given a pointer to a DWARF information entry, figure out if we need
21610 to make a symbol table entry for it, and if so, create a new entry
21611 and return a pointer to it.
21612 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21613 used the passed type.
21614 If SPACE is not NULL, use it to hold the new symbol. If it is
21615 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21616
21617static struct symbol *
5e2db402
TT
21618new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21619 struct symbol *space)
c906108c 21620{
518817b3
SM
21621 struct dwarf2_per_objfile *dwarf2_per_objfile
21622 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21623 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21624 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21625 struct symbol *sym = NULL;
15d034d0 21626 const char *name;
c906108c
SS
21627 struct attribute *attr = NULL;
21628 struct attribute *attr2 = NULL;
e142c38c 21629 CORE_ADDR baseaddr;
e37fd15a
SW
21630 struct pending **list_to_add = NULL;
21631
edb3359d 21632 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21633
21634 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21635
94af9270 21636 name = dwarf2_name (die, cu);
c906108c
SS
21637 if (name)
21638 {
94af9270 21639 const char *linkagename;
34eaf542 21640 int suppress_add = 0;
94af9270 21641
34eaf542
TT
21642 if (space)
21643 sym = space;
21644 else
e623cf5d 21645 sym = allocate_symbol (objfile);
c906108c 21646 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21647
21648 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21649 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270 21650 linkagename = dwarf2_physname (name, die, cu);
31edb802 21651 SYMBOL_SET_NAMES (sym, linkagename, false, objfile);
c906108c 21652
f55ee35c
JK
21653 /* Fortran does not have mangling standard and the mangling does differ
21654 between gfortran, iFort etc. */
21655 if (cu->language == language_fortran
468c0cbb
CB
21656 && symbol_get_demangled_name (sym) == NULL)
21657 symbol_set_demangled_name (sym,
cfc594ee 21658 dwarf2_full_name (name, die, cu),
29df156d 21659 NULL);
f55ee35c 21660
c906108c 21661 /* Default assumptions.
c5aa993b 21662 Use the passed type or decode it from the die. */
176620f1 21663 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21664 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21665 if (type != NULL)
21666 SYMBOL_TYPE (sym) = type;
21667 else
e7c27a73 21668 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21669 attr = dwarf2_attr (die,
21670 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21671 cu);
435d3d88 21672 if (attr != nullptr)
c906108c
SS
21673 {
21674 SYMBOL_LINE (sym) = DW_UNSND (attr);
21675 }
cb1df416 21676
edb3359d
DJ
21677 attr = dwarf2_attr (die,
21678 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21679 cu);
435d3d88 21680 if (attr != nullptr)
cb1df416 21681 {
ecfb656c 21682 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21683 struct file_entry *fe;
9a619af0 21684
ecfb656c
PA
21685 if (cu->line_header != NULL)
21686 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21687 else
21688 fe = NULL;
21689
21690 if (fe == NULL)
b98664d3 21691 complaint (_("file index out of range"));
8c43009f
PA
21692 else
21693 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21694 }
21695
c906108c
SS
21696 switch (die->tag)
21697 {
21698 case DW_TAG_label:
e142c38c 21699 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21700 if (attr != nullptr)
3e29f34a
MR
21701 {
21702 CORE_ADDR addr;
21703
21704 addr = attr_value_as_address (attr);
21705 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21706 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21707 }
0f5238ed
TT
21708 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21709 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21710 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21711 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21712 break;
21713 case DW_TAG_subprogram:
21714 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21715 finish_block. */
f1e6e072 21716 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21717 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21718 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21719 || cu->language == language_ada
21720 || cu->language == language_fortran)
c906108c 21721 {
2cfa0c8d 21722 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21723 Ada and Fortran subprograms, whether marked external or
21724 not, are always stored as a global symbol, because we want
21725 to be able to access them globally. For instance, we want
21726 to be able to break on a nested subprogram without having
21727 to specify the context. */
c24bdb02 21728 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21729 }
21730 else
21731 {
e37fd15a 21732 list_to_add = cu->list_in_scope;
c906108c
SS
21733 }
21734 break;
edb3359d
DJ
21735 case DW_TAG_inlined_subroutine:
21736 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21737 finish_block. */
f1e6e072 21738 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21739 SYMBOL_INLINED (sym) = 1;
481860b3 21740 list_to_add = cu->list_in_scope;
edb3359d 21741 break;
34eaf542
TT
21742 case DW_TAG_template_value_param:
21743 suppress_add = 1;
21744 /* Fall through. */
72929c62 21745 case DW_TAG_constant:
c906108c 21746 case DW_TAG_variable:
254e6b9e 21747 case DW_TAG_member:
0963b4bd
MS
21748 /* Compilation with minimal debug info may result in
21749 variables with missing type entries. Change the
21750 misleading `void' type to something sensible. */
c906108c 21751 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21752 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21753
e142c38c 21754 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21755 /* In the case of DW_TAG_member, we should only be called for
21756 static const members. */
21757 if (die->tag == DW_TAG_member)
21758 {
3863f96c
DE
21759 /* dwarf2_add_field uses die_is_declaration,
21760 so we do the same. */
254e6b9e
DE
21761 gdb_assert (die_is_declaration (die, cu));
21762 gdb_assert (attr);
21763 }
435d3d88 21764 if (attr != nullptr)
c906108c 21765 {
e7c27a73 21766 dwarf2_const_value (attr, sym, cu);
e142c38c 21767 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21768 if (!suppress_add)
34eaf542
TT
21769 {
21770 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21771 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21772 else
e37fd15a 21773 list_to_add = cu->list_in_scope;
34eaf542 21774 }
c906108c
SS
21775 break;
21776 }
e142c38c 21777 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21778 if (attr != nullptr)
c906108c 21779 {
e7c27a73 21780 var_decode_location (attr, sym, cu);
e142c38c 21781 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21782
21783 /* Fortran explicitly imports any global symbols to the local
21784 scope by DW_TAG_common_block. */
21785 if (cu->language == language_fortran && die->parent
21786 && die->parent->tag == DW_TAG_common_block)
21787 attr2 = NULL;
21788
caac4577
JG
21789 if (SYMBOL_CLASS (sym) == LOC_STATIC
21790 && SYMBOL_VALUE_ADDRESS (sym) == 0
21791 && !dwarf2_per_objfile->has_section_at_zero)
21792 {
21793 /* When a static variable is eliminated by the linker,
21794 the corresponding debug information is not stripped
21795 out, but the variable address is set to null;
21796 do not add such variables into symbol table. */
21797 }
21798 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21799 {
4b610737
TT
21800 if (SYMBOL_CLASS (sym) == LOC_STATIC
21801 && (objfile->flags & OBJF_MAINLINE) == 0
21802 && dwarf2_per_objfile->can_copy)
21803 {
21804 /* A global static variable might be subject to
21805 copy relocation. We first check for a local
21806 minsym, though, because maybe the symbol was
21807 marked hidden, in which case this would not
21808 apply. */
21809 bound_minimal_symbol found
21810 = (lookup_minimal_symbol_linkage
987012b8 21811 (sym->linkage_name (), objfile));
4b610737
TT
21812 if (found.minsym != nullptr)
21813 sym->maybe_copied = 1;
21814 }
f55ee35c 21815
1c809c68
TT
21816 /* A variable with DW_AT_external is never static,
21817 but it may be block-scoped. */
804d2729 21818 list_to_add
c24bdb02
KS
21819 = ((cu->list_in_scope
21820 == cu->get_builder ()->get_file_symbols ())
21821 ? cu->get_builder ()->get_global_symbols ()
804d2729 21822 : cu->list_in_scope);
1c809c68 21823 }
c906108c 21824 else
e37fd15a 21825 list_to_add = cu->list_in_scope;
c906108c
SS
21826 }
21827 else
21828 {
21829 /* We do not know the address of this symbol.
c5aa993b
JM
21830 If it is an external symbol and we have type information
21831 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21832 The address of the variable will then be determined from
21833 the minimal symbol table whenever the variable is
21834 referenced. */
e142c38c 21835 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21836
21837 /* Fortran explicitly imports any global symbols to the local
21838 scope by DW_TAG_common_block. */
21839 if (cu->language == language_fortran && die->parent
21840 && die->parent->tag == DW_TAG_common_block)
21841 {
21842 /* SYMBOL_CLASS doesn't matter here because
21843 read_common_block is going to reset it. */
21844 if (!suppress_add)
21845 list_to_add = cu->list_in_scope;
21846 }
21847 else if (attr2 && (DW_UNSND (attr2) != 0)
21848 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21849 {
0fe7935b
DJ
21850 /* A variable with DW_AT_external is never static, but it
21851 may be block-scoped. */
804d2729 21852 list_to_add
c24bdb02
KS
21853 = ((cu->list_in_scope
21854 == cu->get_builder ()->get_file_symbols ())
21855 ? cu->get_builder ()->get_global_symbols ()
804d2729 21856 : cu->list_in_scope);
0fe7935b 21857
f1e6e072 21858 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21859 }
442ddf59
JK
21860 else if (!die_is_declaration (die, cu))
21861 {
21862 /* Use the default LOC_OPTIMIZED_OUT class. */
21863 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21864 if (!suppress_add)
21865 list_to_add = cu->list_in_scope;
442ddf59 21866 }
c906108c
SS
21867 }
21868 break;
21869 case DW_TAG_formal_parameter:
a60f3166
TT
21870 {
21871 /* If we are inside a function, mark this as an argument. If
21872 not, we might be looking at an argument to an inlined function
21873 when we do not have enough information to show inlined frames;
21874 pretend it's a local variable in that case so that the user can
21875 still see it. */
804d2729 21876 struct context_stack *curr
c24bdb02 21877 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21878 if (curr != nullptr && curr->name != nullptr)
21879 SYMBOL_IS_ARGUMENT (sym) = 1;
21880 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21881 if (attr != nullptr)
a60f3166
TT
21882 {
21883 var_decode_location (attr, sym, cu);
21884 }
21885 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21886 if (attr != nullptr)
a60f3166
TT
21887 {
21888 dwarf2_const_value (attr, sym, cu);
21889 }
f346a30d 21890
a60f3166
TT
21891 list_to_add = cu->list_in_scope;
21892 }
c906108c
SS
21893 break;
21894 case DW_TAG_unspecified_parameters:
21895 /* From varargs functions; gdb doesn't seem to have any
21896 interest in this information, so just ignore it for now.
21897 (FIXME?) */
21898 break;
34eaf542
TT
21899 case DW_TAG_template_type_param:
21900 suppress_add = 1;
21901 /* Fall through. */
c906108c 21902 case DW_TAG_class_type:
680b30c7 21903 case DW_TAG_interface_type:
c906108c
SS
21904 case DW_TAG_structure_type:
21905 case DW_TAG_union_type:
72019c9c 21906 case DW_TAG_set_type:
c906108c 21907 case DW_TAG_enumeration_type:
f1e6e072 21908 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21909 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21910
63d06c5c 21911 {
9c37b5ae 21912 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21913 really ever be static objects: otherwise, if you try
21914 to, say, break of a class's method and you're in a file
21915 which doesn't mention that class, it won't work unless
21916 the check for all static symbols in lookup_symbol_aux
21917 saves you. See the OtherFileClass tests in
21918 gdb.c++/namespace.exp. */
21919
e37fd15a 21920 if (!suppress_add)
34eaf542 21921 {
c24bdb02 21922 buildsym_compunit *builder = cu->get_builder ();
804d2729 21923 list_to_add
c24bdb02 21924 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21925 && cu->language == language_cplus
c24bdb02 21926 ? builder->get_global_symbols ()
804d2729 21927 : cu->list_in_scope);
63d06c5c 21928
64382290 21929 /* The semantics of C++ state that "struct foo {
9c37b5ae 21930 ... }" also defines a typedef for "foo". */
64382290 21931 if (cu->language == language_cplus
45280282 21932 || cu->language == language_ada
c44af4eb
TT
21933 || cu->language == language_d
21934 || cu->language == language_rust)
64382290
TT
21935 {
21936 /* The symbol's name is already allocated along
21937 with this objfile, so we don't need to
21938 duplicate it for the type. */
21939 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 21940 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 21941 }
63d06c5c
DC
21942 }
21943 }
c906108c
SS
21944 break;
21945 case DW_TAG_typedef:
f1e6e072 21946 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21947 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21948 list_to_add = cu->list_in_scope;
63d06c5c 21949 break;
c906108c 21950 case DW_TAG_base_type:
a02abb62 21951 case DW_TAG_subrange_type:
f1e6e072 21952 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21953 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21954 list_to_add = cu->list_in_scope;
c906108c
SS
21955 break;
21956 case DW_TAG_enumerator:
e142c38c 21957 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21958 if (attr != nullptr)
c906108c 21959 {
e7c27a73 21960 dwarf2_const_value (attr, sym, cu);
c906108c 21961 }
63d06c5c
DC
21962 {
21963 /* NOTE: carlton/2003-11-10: See comment above in the
21964 DW_TAG_class_type, etc. block. */
21965
804d2729 21966 list_to_add
c24bdb02 21967 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21968 && cu->language == language_cplus
c24bdb02 21969 ? cu->get_builder ()->get_global_symbols ()
804d2729 21970 : cu->list_in_scope);
63d06c5c 21971 }
c906108c 21972 break;
74921315 21973 case DW_TAG_imported_declaration:
5c4e30ca 21974 case DW_TAG_namespace:
f1e6e072 21975 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21976 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21977 break;
530e8392
KB
21978 case DW_TAG_module:
21979 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21980 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21981 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21982 break;
4357ac6c 21983 case DW_TAG_common_block:
f1e6e072 21984 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21985 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21986 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21987 break;
c906108c
SS
21988 default:
21989 /* Not a tag we recognize. Hopefully we aren't processing
21990 trash data, but since we must specifically ignore things
21991 we don't recognize, there is nothing else we should do at
0963b4bd 21992 this point. */
b98664d3 21993 complaint (_("unsupported tag: '%s'"),
4d3c2250 21994 dwarf_tag_name (die->tag));
c906108c
SS
21995 break;
21996 }
df8a16a1 21997
e37fd15a
SW
21998 if (suppress_add)
21999 {
22000 sym->hash_next = objfile->template_symbols;
22001 objfile->template_symbols = sym;
22002 list_to_add = NULL;
22003 }
22004
22005 if (list_to_add != NULL)
d3cb6808 22006 add_symbol_to_list (sym, list_to_add);
e37fd15a 22007
df8a16a1
DJ
22008 /* For the benefit of old versions of GCC, check for anonymous
22009 namespaces based on the demangled name. */
4d4ec4e5 22010 if (!cu->processing_has_namespace_info
94af9270 22011 && cu->language == language_cplus)
c24bdb02 22012 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22013 }
22014 return (sym);
22015}
22016
98bfdba5
PA
22017/* Given an attr with a DW_FORM_dataN value in host byte order,
22018 zero-extend it as appropriate for the symbol's type. The DWARF
22019 standard (v4) is not entirely clear about the meaning of using
22020 DW_FORM_dataN for a constant with a signed type, where the type is
22021 wider than the data. The conclusion of a discussion on the DWARF
22022 list was that this is unspecified. We choose to always zero-extend
22023 because that is the interpretation long in use by GCC. */
c906108c 22024
98bfdba5 22025static gdb_byte *
ff39bb5e 22026dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22027 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22028{
518817b3 22029 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22030 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22031 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22032 LONGEST l = DW_UNSND (attr);
22033
22034 if (bits < sizeof (*value) * 8)
22035 {
22036 l &= ((LONGEST) 1 << bits) - 1;
22037 *value = l;
22038 }
22039 else if (bits == sizeof (*value) * 8)
22040 *value = l;
22041 else
22042 {
224c3ddb 22043 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22044 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22045 return bytes;
22046 }
22047
22048 return NULL;
22049}
22050
22051/* Read a constant value from an attribute. Either set *VALUE, or if
22052 the value does not fit in *VALUE, set *BYTES - either already
22053 allocated on the objfile obstack, or newly allocated on OBSTACK,
22054 or, set *BATON, if we translated the constant to a location
22055 expression. */
22056
22057static void
ff39bb5e 22058dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22059 const char *name, struct obstack *obstack,
22060 struct dwarf2_cu *cu,
d521ce57 22061 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22062 struct dwarf2_locexpr_baton **baton)
22063{
518817b3 22064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22065 struct comp_unit_head *cu_header = &cu->header;
c906108c 22066 struct dwarf_block *blk;
98bfdba5
PA
22067 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22068 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22069
22070 *value = 0;
22071 *bytes = NULL;
22072 *baton = NULL;
c906108c
SS
22073
22074 switch (attr->form)
22075 {
22076 case DW_FORM_addr:
336d760d 22077 case DW_FORM_addrx:
3019eac3 22078 case DW_FORM_GNU_addr_index:
ac56253d 22079 {
ac56253d
TT
22080 gdb_byte *data;
22081
98bfdba5
PA
22082 if (TYPE_LENGTH (type) != cu_header->addr_size)
22083 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22084 cu_header->addr_size,
98bfdba5 22085 TYPE_LENGTH (type));
ac56253d
TT
22086 /* Symbols of this form are reasonably rare, so we just
22087 piggyback on the existing location code rather than writing
22088 a new implementation of symbol_computed_ops. */
8d749320 22089 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22090 (*baton)->per_cu = cu->per_cu;
22091 gdb_assert ((*baton)->per_cu);
ac56253d 22092
98bfdba5 22093 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22094 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22095 (*baton)->data = data;
ac56253d
TT
22096
22097 data[0] = DW_OP_addr;
22098 store_unsigned_integer (&data[1], cu_header->addr_size,
22099 byte_order, DW_ADDR (attr));
22100 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22101 }
c906108c 22102 break;
4ac36638 22103 case DW_FORM_string:
93b5768b 22104 case DW_FORM_strp:
cf532bd1 22105 case DW_FORM_strx:
3019eac3 22106 case DW_FORM_GNU_str_index:
36586728 22107 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22108 /* DW_STRING is already allocated on the objfile obstack, point
22109 directly to it. */
d521ce57 22110 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22111 break;
c906108c
SS
22112 case DW_FORM_block1:
22113 case DW_FORM_block2:
22114 case DW_FORM_block4:
22115 case DW_FORM_block:
2dc7f7b3 22116 case DW_FORM_exprloc:
0224619f 22117 case DW_FORM_data16:
c906108c 22118 blk = DW_BLOCK (attr);
98bfdba5
PA
22119 if (TYPE_LENGTH (type) != blk->size)
22120 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22121 TYPE_LENGTH (type));
22122 *bytes = blk->data;
c906108c 22123 break;
2df3850c
JM
22124
22125 /* The DW_AT_const_value attributes are supposed to carry the
22126 symbol's value "represented as it would be on the target
22127 architecture." By the time we get here, it's already been
22128 converted to host endianness, so we just need to sign- or
22129 zero-extend it as appropriate. */
22130 case DW_FORM_data1:
3aef2284 22131 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22132 break;
c906108c 22133 case DW_FORM_data2:
3aef2284 22134 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22135 break;
c906108c 22136 case DW_FORM_data4:
3aef2284 22137 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22138 break;
c906108c 22139 case DW_FORM_data8:
3aef2284 22140 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22141 break;
22142
c906108c 22143 case DW_FORM_sdata:
663c44ac 22144 case DW_FORM_implicit_const:
98bfdba5 22145 *value = DW_SND (attr);
2df3850c
JM
22146 break;
22147
c906108c 22148 case DW_FORM_udata:
98bfdba5 22149 *value = DW_UNSND (attr);
c906108c 22150 break;
2df3850c 22151
c906108c 22152 default:
b98664d3 22153 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22154 dwarf_form_name (attr->form));
98bfdba5 22155 *value = 0;
c906108c
SS
22156 break;
22157 }
22158}
22159
2df3850c 22160
98bfdba5
PA
22161/* Copy constant value from an attribute to a symbol. */
22162
2df3850c 22163static void
ff39bb5e 22164dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22165 struct dwarf2_cu *cu)
2df3850c 22166{
518817b3 22167 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22168 LONGEST value;
d521ce57 22169 const gdb_byte *bytes;
98bfdba5 22170 struct dwarf2_locexpr_baton *baton;
2df3850c 22171
98bfdba5 22172 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22173 sym->print_name (),
98bfdba5
PA
22174 &objfile->objfile_obstack, cu,
22175 &value, &bytes, &baton);
2df3850c 22176
98bfdba5
PA
22177 if (baton != NULL)
22178 {
98bfdba5 22179 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22180 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22181 }
22182 else if (bytes != NULL)
22183 {
22184 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22185 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22186 }
22187 else
22188 {
22189 SYMBOL_VALUE (sym) = value;
f1e6e072 22190 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22191 }
2df3850c
JM
22192}
22193
c906108c
SS
22194/* Return the type of the die in question using its DW_AT_type attribute. */
22195
22196static struct type *
e7c27a73 22197die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22198{
c906108c 22199 struct attribute *type_attr;
c906108c 22200
e142c38c 22201 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22202 if (!type_attr)
22203 {
518817b3 22204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22205 /* A missing DW_AT_type represents a void type. */
518817b3 22206 return objfile_type (objfile)->builtin_void;
c906108c 22207 }
348e048f 22208
673bfd45 22209 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22210}
22211
b4ba55a1
JB
22212/* True iff CU's producer generates GNAT Ada auxiliary information
22213 that allows to find parallel types through that information instead
22214 of having to do expensive parallel lookups by type name. */
22215
22216static int
22217need_gnat_info (struct dwarf2_cu *cu)
22218{
de4cb04a
JB
22219 /* Assume that the Ada compiler was GNAT, which always produces
22220 the auxiliary information. */
22221 return (cu->language == language_ada);
b4ba55a1
JB
22222}
22223
b4ba55a1
JB
22224/* Return the auxiliary type of the die in question using its
22225 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22226 attribute is not present. */
22227
22228static struct type *
22229die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22230{
b4ba55a1 22231 struct attribute *type_attr;
b4ba55a1
JB
22232
22233 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22234 if (!type_attr)
22235 return NULL;
22236
673bfd45 22237 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22238}
22239
22240/* If DIE has a descriptive_type attribute, then set the TYPE's
22241 descriptive type accordingly. */
22242
22243static void
22244set_descriptive_type (struct type *type, struct die_info *die,
22245 struct dwarf2_cu *cu)
22246{
22247 struct type *descriptive_type = die_descriptive_type (die, cu);
22248
22249 if (descriptive_type)
22250 {
22251 ALLOCATE_GNAT_AUX_TYPE (type);
22252 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22253 }
22254}
22255
c906108c
SS
22256/* Return the containing type of the die in question using its
22257 DW_AT_containing_type attribute. */
22258
22259static struct type *
e7c27a73 22260die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22261{
c906108c 22262 struct attribute *type_attr;
518817b3 22263 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22264
e142c38c 22265 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22266 if (!type_attr)
22267 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22268 "[in module %s]"), objfile_name (objfile));
33ac96f0 22269
673bfd45 22270 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22271}
22272
ac9ec31b
DE
22273/* Return an error marker type to use for the ill formed type in DIE/CU. */
22274
22275static struct type *
22276build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22277{
518817b3
SM
22278 struct dwarf2_per_objfile *dwarf2_per_objfile
22279 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22280 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22281 char *saved;
ac9ec31b 22282
528e1572
SM
22283 std::string message
22284 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22285 objfile_name (objfile),
22286 sect_offset_str (cu->header.sect_off),
22287 sect_offset_str (die->sect_off));
efba19b0 22288 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22289
19f392bc 22290 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22291}
22292
673bfd45 22293/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22294 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22295 DW_AT_containing_type.
673bfd45
DE
22296 If there is no type substitute an error marker. */
22297
c906108c 22298static struct type *
ff39bb5e 22299lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22300 struct dwarf2_cu *cu)
c906108c 22301{
518817b3
SM
22302 struct dwarf2_per_objfile *dwarf2_per_objfile
22303 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22304 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22305 struct type *this_type;
22306
ac9ec31b
DE
22307 gdb_assert (attr->name == DW_AT_type
22308 || attr->name == DW_AT_GNAT_descriptive_type
22309 || attr->name == DW_AT_containing_type);
22310
673bfd45
DE
22311 /* First see if we have it cached. */
22312
36586728
TT
22313 if (attr->form == DW_FORM_GNU_ref_alt)
22314 {
22315 struct dwarf2_per_cu_data *per_cu;
9c541725 22316 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22317
ed2dc618
SM
22318 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22319 dwarf2_per_objfile);
9c541725 22320 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22321 }
7771576e 22322 else if (attr_form_is_ref (attr))
673bfd45 22323 {
9c541725 22324 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22325
9c541725 22326 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22327 }
55f1336d 22328 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22329 {
ac9ec31b 22330 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22331
ac9ec31b 22332 return get_signatured_type (die, signature, cu);
673bfd45
DE
22333 }
22334 else
22335 {
b98664d3 22336 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22337 " at %s [in module %s]"),
22338 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22339 objfile_name (objfile));
ac9ec31b 22340 return build_error_marker_type (cu, die);
673bfd45
DE
22341 }
22342
22343 /* If not cached we need to read it in. */
22344
22345 if (this_type == NULL)
22346 {
ac9ec31b 22347 struct die_info *type_die = NULL;
673bfd45
DE
22348 struct dwarf2_cu *type_cu = cu;
22349
7771576e 22350 if (attr_form_is_ref (attr))
ac9ec31b
DE
22351 type_die = follow_die_ref (die, attr, &type_cu);
22352 if (type_die == NULL)
22353 return build_error_marker_type (cu, die);
22354 /* If we find the type now, it's probably because the type came
3019eac3
DE
22355 from an inter-CU reference and the type's CU got expanded before
22356 ours. */
ac9ec31b 22357 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22358 }
22359
22360 /* If we still don't have a type use an error marker. */
22361
22362 if (this_type == NULL)
ac9ec31b 22363 return build_error_marker_type (cu, die);
673bfd45 22364
f792889a 22365 return this_type;
c906108c
SS
22366}
22367
673bfd45
DE
22368/* Return the type in DIE, CU.
22369 Returns NULL for invalid types.
22370
02142a6c 22371 This first does a lookup in die_type_hash,
673bfd45
DE
22372 and only reads the die in if necessary.
22373
22374 NOTE: This can be called when reading in partial or full symbols. */
22375
f792889a 22376static struct type *
e7c27a73 22377read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22378{
f792889a
DJ
22379 struct type *this_type;
22380
22381 this_type = get_die_type (die, cu);
22382 if (this_type)
22383 return this_type;
22384
673bfd45
DE
22385 return read_type_die_1 (die, cu);
22386}
22387
22388/* Read the type in DIE, CU.
22389 Returns NULL for invalid types. */
22390
22391static struct type *
22392read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22393{
22394 struct type *this_type = NULL;
22395
c906108c
SS
22396 switch (die->tag)
22397 {
22398 case DW_TAG_class_type:
680b30c7 22399 case DW_TAG_interface_type:
c906108c
SS
22400 case DW_TAG_structure_type:
22401 case DW_TAG_union_type:
f792889a 22402 this_type = read_structure_type (die, cu);
c906108c
SS
22403 break;
22404 case DW_TAG_enumeration_type:
f792889a 22405 this_type = read_enumeration_type (die, cu);
c906108c
SS
22406 break;
22407 case DW_TAG_subprogram:
22408 case DW_TAG_subroutine_type:
edb3359d 22409 case DW_TAG_inlined_subroutine:
f792889a 22410 this_type = read_subroutine_type (die, cu);
c906108c
SS
22411 break;
22412 case DW_TAG_array_type:
f792889a 22413 this_type = read_array_type (die, cu);
c906108c 22414 break;
72019c9c 22415 case DW_TAG_set_type:
f792889a 22416 this_type = read_set_type (die, cu);
72019c9c 22417 break;
c906108c 22418 case DW_TAG_pointer_type:
f792889a 22419 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22420 break;
22421 case DW_TAG_ptr_to_member_type:
f792889a 22422 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22423 break;
22424 case DW_TAG_reference_type:
4297a3f0
AV
22425 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22426 break;
22427 case DW_TAG_rvalue_reference_type:
22428 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22429 break;
22430 case DW_TAG_const_type:
f792889a 22431 this_type = read_tag_const_type (die, cu);
c906108c
SS
22432 break;
22433 case DW_TAG_volatile_type:
f792889a 22434 this_type = read_tag_volatile_type (die, cu);
c906108c 22435 break;
06d66ee9
TT
22436 case DW_TAG_restrict_type:
22437 this_type = read_tag_restrict_type (die, cu);
22438 break;
c906108c 22439 case DW_TAG_string_type:
f792889a 22440 this_type = read_tag_string_type (die, cu);
c906108c
SS
22441 break;
22442 case DW_TAG_typedef:
f792889a 22443 this_type = read_typedef (die, cu);
c906108c 22444 break;
a02abb62 22445 case DW_TAG_subrange_type:
f792889a 22446 this_type = read_subrange_type (die, cu);
a02abb62 22447 break;
c906108c 22448 case DW_TAG_base_type:
f792889a 22449 this_type = read_base_type (die, cu);
c906108c 22450 break;
81a17f79 22451 case DW_TAG_unspecified_type:
f792889a 22452 this_type = read_unspecified_type (die, cu);
81a17f79 22453 break;
0114d602
DJ
22454 case DW_TAG_namespace:
22455 this_type = read_namespace_type (die, cu);
22456 break;
f55ee35c
JK
22457 case DW_TAG_module:
22458 this_type = read_module_type (die, cu);
22459 break;
a2c2acaf
MW
22460 case DW_TAG_atomic_type:
22461 this_type = read_tag_atomic_type (die, cu);
22462 break;
c906108c 22463 default:
b98664d3 22464 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22465 dwarf_tag_name (die->tag));
c906108c
SS
22466 break;
22467 }
63d06c5c 22468
f792889a 22469 return this_type;
63d06c5c
DC
22470}
22471
abc72ce4
DE
22472/* See if we can figure out if the class lives in a namespace. We do
22473 this by looking for a member function; its demangled name will
22474 contain namespace info, if there is any.
22475 Return the computed name or NULL.
22476 Space for the result is allocated on the objfile's obstack.
22477 This is the full-die version of guess_partial_die_structure_name.
22478 In this case we know DIE has no useful parent. */
22479
22480static char *
22481guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22482{
22483 struct die_info *spec_die;
22484 struct dwarf2_cu *spec_cu;
22485 struct die_info *child;
518817b3 22486 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22487
22488 spec_cu = cu;
22489 spec_die = die_specification (die, &spec_cu);
22490 if (spec_die != NULL)
22491 {
22492 die = spec_die;
22493 cu = spec_cu;
22494 }
22495
22496 for (child = die->child;
22497 child != NULL;
22498 child = child->sibling)
22499 {
22500 if (child->tag == DW_TAG_subprogram)
22501 {
73b9be8b 22502 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22503
7d45c7c3 22504 if (linkage_name != NULL)
abc72ce4
DE
22505 {
22506 char *actual_name
22507 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22508 linkage_name);
abc72ce4
DE
22509 char *name = NULL;
22510
22511 if (actual_name != NULL)
22512 {
15d034d0 22513 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22514
22515 if (die_name != NULL
22516 && strcmp (die_name, actual_name) != 0)
22517 {
22518 /* Strip off the class name from the full name.
22519 We want the prefix. */
22520 int die_name_len = strlen (die_name);
22521 int actual_name_len = strlen (actual_name);
22522
22523 /* Test for '::' as a sanity check. */
22524 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22525 && actual_name[actual_name_len
22526 - die_name_len - 1] == ':')
0cf9feb9 22527 name = obstack_strndup (
e3b94546 22528 &objfile->per_bfd->storage_obstack,
224c3ddb 22529 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22530 }
22531 }
22532 xfree (actual_name);
22533 return name;
22534 }
22535 }
22536 }
22537
22538 return NULL;
22539}
22540
96408a79
SA
22541/* GCC might emit a nameless typedef that has a linkage name. Determine the
22542 prefix part in such case. See
22543 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22544
a121b7c1 22545static const char *
96408a79
SA
22546anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22547{
22548 struct attribute *attr;
e6a959d6 22549 const char *base;
96408a79
SA
22550
22551 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22552 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22553 return NULL;
22554
7d45c7c3 22555 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22556 return NULL;
22557
73b9be8b 22558 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22559 if (attr == NULL || DW_STRING (attr) == NULL)
22560 return NULL;
22561
22562 /* dwarf2_name had to be already called. */
22563 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22564
22565 /* Strip the base name, keep any leading namespaces/classes. */
22566 base = strrchr (DW_STRING (attr), ':');
22567 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22568 return "";
22569
518817b3 22570 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22571 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22572 DW_STRING (attr),
22573 &base[-1] - DW_STRING (attr));
96408a79
SA
22574}
22575
fdde2d81 22576/* Return the name of the namespace/class that DIE is defined within,
0114d602 22577 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22578
0114d602
DJ
22579 For example, if we're within the method foo() in the following
22580 code:
22581
22582 namespace N {
22583 class C {
22584 void foo () {
22585 }
22586 };
22587 }
22588
22589 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22590
0d5cff50 22591static const char *
e142c38c 22592determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22593{
518817b3
SM
22594 struct dwarf2_per_objfile *dwarf2_per_objfile
22595 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22596 struct die_info *parent, *spec_die;
22597 struct dwarf2_cu *spec_cu;
22598 struct type *parent_type;
a121b7c1 22599 const char *retval;
63d06c5c 22600
9c37b5ae 22601 if (cu->language != language_cplus
c44af4eb
TT
22602 && cu->language != language_fortran && cu->language != language_d
22603 && cu->language != language_rust)
0114d602
DJ
22604 return "";
22605
96408a79
SA
22606 retval = anonymous_struct_prefix (die, cu);
22607 if (retval)
22608 return retval;
22609
0114d602
DJ
22610 /* We have to be careful in the presence of DW_AT_specification.
22611 For example, with GCC 3.4, given the code
22612
22613 namespace N {
22614 void foo() {
22615 // Definition of N::foo.
22616 }
22617 }
22618
22619 then we'll have a tree of DIEs like this:
22620
22621 1: DW_TAG_compile_unit
22622 2: DW_TAG_namespace // N
22623 3: DW_TAG_subprogram // declaration of N::foo
22624 4: DW_TAG_subprogram // definition of N::foo
22625 DW_AT_specification // refers to die #3
22626
22627 Thus, when processing die #4, we have to pretend that we're in
22628 the context of its DW_AT_specification, namely the contex of die
22629 #3. */
22630 spec_cu = cu;
22631 spec_die = die_specification (die, &spec_cu);
22632 if (spec_die == NULL)
22633 parent = die->parent;
22634 else
63d06c5c 22635 {
0114d602
DJ
22636 parent = spec_die->parent;
22637 cu = spec_cu;
63d06c5c 22638 }
0114d602
DJ
22639
22640 if (parent == NULL)
22641 return "";
98bfdba5
PA
22642 else if (parent->building_fullname)
22643 {
22644 const char *name;
22645 const char *parent_name;
22646
22647 /* It has been seen on RealView 2.2 built binaries,
22648 DW_TAG_template_type_param types actually _defined_ as
22649 children of the parent class:
22650
22651 enum E {};
22652 template class <class Enum> Class{};
22653 Class<enum E> class_e;
22654
22655 1: DW_TAG_class_type (Class)
22656 2: DW_TAG_enumeration_type (E)
22657 3: DW_TAG_enumerator (enum1:0)
22658 3: DW_TAG_enumerator (enum2:1)
22659 ...
22660 2: DW_TAG_template_type_param
22661 DW_AT_type DW_FORM_ref_udata (E)
22662
22663 Besides being broken debug info, it can put GDB into an
22664 infinite loop. Consider:
22665
22666 When we're building the full name for Class<E>, we'll start
22667 at Class, and go look over its template type parameters,
22668 finding E. We'll then try to build the full name of E, and
22669 reach here. We're now trying to build the full name of E,
22670 and look over the parent DIE for containing scope. In the
22671 broken case, if we followed the parent DIE of E, we'd again
22672 find Class, and once again go look at its template type
22673 arguments, etc., etc. Simply don't consider such parent die
22674 as source-level parent of this die (it can't be, the language
22675 doesn't allow it), and break the loop here. */
22676 name = dwarf2_name (die, cu);
22677 parent_name = dwarf2_name (parent, cu);
b98664d3 22678 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22679 name ? name : "<unknown>",
22680 parent_name ? parent_name : "<unknown>");
22681 return "";
22682 }
63d06c5c 22683 else
0114d602
DJ
22684 switch (parent->tag)
22685 {
63d06c5c 22686 case DW_TAG_namespace:
0114d602 22687 parent_type = read_type_die (parent, cu);
acebe513
UW
22688 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22689 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22690 Work around this problem here. */
22691 if (cu->language == language_cplus
e86ca25f 22692 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22693 return "";
0114d602 22694 /* We give a name to even anonymous namespaces. */
e86ca25f 22695 return TYPE_NAME (parent_type);
63d06c5c 22696 case DW_TAG_class_type:
680b30c7 22697 case DW_TAG_interface_type:
63d06c5c 22698 case DW_TAG_structure_type:
0114d602 22699 case DW_TAG_union_type:
f55ee35c 22700 case DW_TAG_module:
0114d602 22701 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22702 if (TYPE_NAME (parent_type) != NULL)
22703 return TYPE_NAME (parent_type);
0114d602
DJ
22704 else
22705 /* An anonymous structure is only allowed non-static data
22706 members; no typedefs, no member functions, et cetera.
22707 So it does not need a prefix. */
22708 return "";
abc72ce4 22709 case DW_TAG_compile_unit:
95554aad 22710 case DW_TAG_partial_unit:
abc72ce4
DE
22711 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22712 if (cu->language == language_cplus
fd5866f6 22713 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22714 && die->child != NULL
22715 && (die->tag == DW_TAG_class_type
22716 || die->tag == DW_TAG_structure_type
22717 || die->tag == DW_TAG_union_type))
22718 {
22719 char *name = guess_full_die_structure_name (die, cu);
22720 if (name != NULL)
22721 return name;
22722 }
22723 return "";
0a4b0913
AB
22724 case DW_TAG_subprogram:
22725 /* Nested subroutines in Fortran get a prefix with the name
22726 of the parent's subroutine. */
22727 if (cu->language == language_fortran)
22728 {
22729 if ((die->tag == DW_TAG_subprogram)
22730 && (dwarf2_name (parent, cu) != NULL))
22731 return dwarf2_name (parent, cu);
22732 }
22733 return determine_prefix (parent, cu);
3d567982
TT
22734 case DW_TAG_enumeration_type:
22735 parent_type = read_type_die (parent, cu);
22736 if (TYPE_DECLARED_CLASS (parent_type))
22737 {
e86ca25f
TT
22738 if (TYPE_NAME (parent_type) != NULL)
22739 return TYPE_NAME (parent_type);
3d567982
TT
22740 return "";
22741 }
22742 /* Fall through. */
63d06c5c 22743 default:
8176b9b8 22744 return determine_prefix (parent, cu);
63d06c5c 22745 }
63d06c5c
DC
22746}
22747
3e43a32a
MS
22748/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22749 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22750 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22751 an obconcat, otherwise allocate storage for the result. The CU argument is
22752 used to determine the language and hence, the appropriate separator. */
987504bb 22753
f55ee35c 22754#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22755
22756static char *
f55ee35c
JK
22757typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22758 int physname, struct dwarf2_cu *cu)
63d06c5c 22759{
f55ee35c 22760 const char *lead = "";
5c315b68 22761 const char *sep;
63d06c5c 22762
3e43a32a
MS
22763 if (suffix == NULL || suffix[0] == '\0'
22764 || prefix == NULL || prefix[0] == '\0')
987504bb 22765 sep = "";
45280282
IB
22766 else if (cu->language == language_d)
22767 {
22768 /* For D, the 'main' function could be defined in any module, but it
22769 should never be prefixed. */
22770 if (strcmp (suffix, "D main") == 0)
22771 {
22772 prefix = "";
22773 sep = "";
22774 }
22775 else
22776 sep = ".";
22777 }
f55ee35c
JK
22778 else if (cu->language == language_fortran && physname)
22779 {
22780 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22781 DW_AT_MIPS_linkage_name is preferred and used instead. */
22782
22783 lead = "__";
22784 sep = "_MOD_";
22785 }
987504bb
JJ
22786 else
22787 sep = "::";
63d06c5c 22788
6dd47d34
DE
22789 if (prefix == NULL)
22790 prefix = "";
22791 if (suffix == NULL)
22792 suffix = "";
22793
987504bb
JJ
22794 if (obs == NULL)
22795 {
3e43a32a 22796 char *retval
224c3ddb
SM
22797 = ((char *)
22798 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22799
f55ee35c
JK
22800 strcpy (retval, lead);
22801 strcat (retval, prefix);
6dd47d34
DE
22802 strcat (retval, sep);
22803 strcat (retval, suffix);
63d06c5c
DC
22804 return retval;
22805 }
987504bb
JJ
22806 else
22807 {
22808 /* We have an obstack. */
f55ee35c 22809 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22810 }
63d06c5c
DC
22811}
22812
c906108c
SS
22813/* Return sibling of die, NULL if no sibling. */
22814
f9aca02d 22815static struct die_info *
fba45db2 22816sibling_die (struct die_info *die)
c906108c 22817{
639d11d3 22818 return die->sibling;
c906108c
SS
22819}
22820
71c25dea
TT
22821/* Get name of a die, return NULL if not found. */
22822
15d034d0
TT
22823static const char *
22824dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22825 struct obstack *obstack)
22826{
22827 if (name && cu->language == language_cplus)
22828 {
2f408ecb 22829 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22830
2f408ecb 22831 if (!canon_name.empty ())
71c25dea 22832 {
2f408ecb 22833 if (canon_name != name)
efba19b0 22834 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22835 }
22836 }
22837
22838 return name;
c906108c
SS
22839}
22840
96553a0c
DE
22841/* Get name of a die, return NULL if not found.
22842 Anonymous namespaces are converted to their magic string. */
9219021c 22843
15d034d0 22844static const char *
e142c38c 22845dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22846{
22847 struct attribute *attr;
518817b3 22848 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22849
e142c38c 22850 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22851 if ((!attr || !DW_STRING (attr))
96553a0c 22852 && die->tag != DW_TAG_namespace
53832f31
TT
22853 && die->tag != DW_TAG_class_type
22854 && die->tag != DW_TAG_interface_type
22855 && die->tag != DW_TAG_structure_type
22856 && die->tag != DW_TAG_union_type)
71c25dea
TT
22857 return NULL;
22858
22859 switch (die->tag)
22860 {
22861 case DW_TAG_compile_unit:
95554aad 22862 case DW_TAG_partial_unit:
71c25dea
TT
22863 /* Compilation units have a DW_AT_name that is a filename, not
22864 a source language identifier. */
22865 case DW_TAG_enumeration_type:
22866 case DW_TAG_enumerator:
22867 /* These tags always have simple identifiers already; no need
22868 to canonicalize them. */
22869 return DW_STRING (attr);
907af001 22870
96553a0c
DE
22871 case DW_TAG_namespace:
22872 if (attr != NULL && DW_STRING (attr) != NULL)
22873 return DW_STRING (attr);
22874 return CP_ANONYMOUS_NAMESPACE_STR;
22875
907af001
UW
22876 case DW_TAG_class_type:
22877 case DW_TAG_interface_type:
22878 case DW_TAG_structure_type:
22879 case DW_TAG_union_type:
22880 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22881 structures or unions. These were of the form "._%d" in GCC 4.1,
22882 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22883 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22884 if (attr && DW_STRING (attr)
61012eef
GB
22885 && (startswith (DW_STRING (attr), "._")
22886 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22887 return NULL;
53832f31
TT
22888
22889 /* GCC might emit a nameless typedef that has a linkage name. See
22890 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22891 if (!attr || DW_STRING (attr) == NULL)
22892 {
df5c6c50 22893 char *demangled = NULL;
53832f31 22894
73b9be8b 22895 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22896 if (attr == NULL || DW_STRING (attr) == NULL)
22897 return NULL;
22898
df5c6c50
JK
22899 /* Avoid demangling DW_STRING (attr) the second time on a second
22900 call for the same DIE. */
22901 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22902 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22903
22904 if (demangled)
22905 {
e6a959d6 22906 const char *base;
96408a79 22907
53832f31 22908 /* FIXME: we already did this for the partial symbol... */
34a68019 22909 DW_STRING (attr)
021887d8
TT
22910 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22911 demangled);
53832f31
TT
22912 DW_STRING_IS_CANONICAL (attr) = 1;
22913 xfree (demangled);
96408a79
SA
22914
22915 /* Strip any leading namespaces/classes, keep only the base name.
22916 DW_AT_name for named DIEs does not contain the prefixes. */
22917 base = strrchr (DW_STRING (attr), ':');
22918 if (base && base > DW_STRING (attr) && base[-1] == ':')
22919 return &base[1];
22920 else
22921 return DW_STRING (attr);
53832f31
TT
22922 }
22923 }
907af001
UW
22924 break;
22925
71c25dea 22926 default:
907af001
UW
22927 break;
22928 }
22929
22930 if (!DW_STRING_IS_CANONICAL (attr))
22931 {
22932 DW_STRING (attr)
22933 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22934 &objfile->per_bfd->storage_obstack);
907af001 22935 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22936 }
907af001 22937 return DW_STRING (attr);
9219021c
DC
22938}
22939
22940/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22941 is none. *EXT_CU is the CU containing DIE on input, and the CU
22942 containing the return value on output. */
9219021c
DC
22943
22944static struct die_info *
f2f0e013 22945dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22946{
22947 struct attribute *attr;
9219021c 22948
f2f0e013 22949 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22950 if (attr == NULL)
22951 return NULL;
22952
f2f0e013 22953 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22954}
22955
fa9c3fa0
TT
22956/* A convenience function that returns an "unknown" DWARF name,
22957 including the value of V. STR is the name of the entity being
22958 printed, e.g., "TAG". */
22959
22960static const char *
22961dwarf_unknown (const char *str, unsigned v)
22962{
22963 char *cell = get_print_cell ();
22964 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22965 return cell;
22966}
22967
c906108c
SS
22968/* Convert a DIE tag into its string name. */
22969
f39c6ffd 22970static const char *
aa1ee363 22971dwarf_tag_name (unsigned tag)
c906108c 22972{
f39c6ffd
TT
22973 const char *name = get_DW_TAG_name (tag);
22974
22975 if (name == NULL)
fa9c3fa0 22976 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
22977
22978 return name;
c906108c
SS
22979}
22980
22981/* Convert a DWARF attribute code into its string name. */
22982
f39c6ffd 22983static const char *
aa1ee363 22984dwarf_attr_name (unsigned attr)
c906108c 22985{
f39c6ffd
TT
22986 const char *name;
22987
c764a876 22988#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22989 if (attr == DW_AT_MIPS_fde)
22990 return "DW_AT_MIPS_fde";
22991#else
22992 if (attr == DW_AT_HP_block_index)
22993 return "DW_AT_HP_block_index";
c764a876 22994#endif
f39c6ffd
TT
22995
22996 name = get_DW_AT_name (attr);
22997
22998 if (name == NULL)
fa9c3fa0 22999 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
23000
23001 return name;
c906108c
SS
23002}
23003
a084a2a6
AT
23004/* Convert a unit type to corresponding DW_UT name. */
23005
23006static const char *
23007dwarf_unit_type_name (int unit_type) {
23008 switch (unit_type)
23009 {
23010 case 0x01:
23011 return "DW_UT_compile (0x01)";
23012 case 0x02:
23013 return "DW_UT_type (0x02)";
23014 case 0x03:
23015 return "DW_UT_partial (0x03)";
23016 case 0x04:
23017 return "DW_UT_skeleton (0x04)";
23018 case 0x05:
23019 return "DW_UT_split_compile (0x05)";
23020 case 0x06:
23021 return "DW_UT_split_type (0x06)";
23022 case 0x80:
23023 return "DW_UT_lo_user (0x80)";
23024 case 0xff:
23025 return "DW_UT_hi_user (0xff)";
23026 default:
23027 return nullptr;
23028 }
23029}
23030
c906108c
SS
23031/* Convert a DWARF value form code into its string name. */
23032
f39c6ffd 23033static const char *
aa1ee363 23034dwarf_form_name (unsigned form)
c906108c 23035{
f39c6ffd
TT
23036 const char *name = get_DW_FORM_name (form);
23037
23038 if (name == NULL)
fa9c3fa0 23039 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
23040
23041 return name;
c906108c
SS
23042}
23043
a121b7c1 23044static const char *
fba45db2 23045dwarf_bool_name (unsigned mybool)
c906108c
SS
23046{
23047 if (mybool)
23048 return "TRUE";
23049 else
23050 return "FALSE";
23051}
23052
23053/* Convert a DWARF type code into its string name. */
23054
f39c6ffd 23055static const char *
aa1ee363 23056dwarf_type_encoding_name (unsigned enc)
c906108c 23057{
f39c6ffd 23058 const char *name = get_DW_ATE_name (enc);
c906108c 23059
f39c6ffd 23060 if (name == NULL)
fa9c3fa0 23061 return dwarf_unknown ("ATE", enc);
c906108c 23062
f39c6ffd 23063 return name;
c906108c 23064}
c906108c 23065
f9aca02d 23066static void
d97bc12b 23067dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23068{
23069 unsigned int i;
23070
d97bc12b 23071 print_spaces (indent, f);
9d8780f0 23072 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23073 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23074 sect_offset_str (die->sect_off));
d97bc12b
DE
23075
23076 if (die->parent != NULL)
23077 {
23078 print_spaces (indent, f);
9d8780f0
SM
23079 fprintf_unfiltered (f, " parent at offset: %s\n",
23080 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23081 }
23082
23083 print_spaces (indent, f);
23084 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23085 dwarf_bool_name (die->child != NULL));
c906108c 23086
d97bc12b
DE
23087 print_spaces (indent, f);
23088 fprintf_unfiltered (f, " attributes:\n");
23089
c906108c
SS
23090 for (i = 0; i < die->num_attrs; ++i)
23091 {
d97bc12b
DE
23092 print_spaces (indent, f);
23093 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23094 dwarf_attr_name (die->attrs[i].name),
23095 dwarf_form_name (die->attrs[i].form));
d97bc12b 23096
c906108c
SS
23097 switch (die->attrs[i].form)
23098 {
c906108c 23099 case DW_FORM_addr:
336d760d 23100 case DW_FORM_addrx:
3019eac3 23101 case DW_FORM_GNU_addr_index:
d97bc12b 23102 fprintf_unfiltered (f, "address: ");
5af949e3 23103 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23104 break;
23105 case DW_FORM_block2:
23106 case DW_FORM_block4:
23107 case DW_FORM_block:
23108 case DW_FORM_block1:
56eb65bd
SP
23109 fprintf_unfiltered (f, "block: size %s",
23110 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23111 break;
2dc7f7b3 23112 case DW_FORM_exprloc:
56eb65bd
SP
23113 fprintf_unfiltered (f, "expression: size %s",
23114 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23115 break;
0224619f
JK
23116 case DW_FORM_data16:
23117 fprintf_unfiltered (f, "constant of 16 bytes");
23118 break;
4568ecf9
DE
23119 case DW_FORM_ref_addr:
23120 fprintf_unfiltered (f, "ref address: ");
23121 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23122 break;
36586728
TT
23123 case DW_FORM_GNU_ref_alt:
23124 fprintf_unfiltered (f, "alt ref address: ");
23125 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23126 break;
10b3939b
DJ
23127 case DW_FORM_ref1:
23128 case DW_FORM_ref2:
23129 case DW_FORM_ref4:
4568ecf9
DE
23130 case DW_FORM_ref8:
23131 case DW_FORM_ref_udata:
d97bc12b 23132 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23133 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23134 break;
c906108c
SS
23135 case DW_FORM_data1:
23136 case DW_FORM_data2:
23137 case DW_FORM_data4:
ce5d95e1 23138 case DW_FORM_data8:
c906108c
SS
23139 case DW_FORM_udata:
23140 case DW_FORM_sdata:
43bbcdc2
PH
23141 fprintf_unfiltered (f, "constant: %s",
23142 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23143 break;
2dc7f7b3
TT
23144 case DW_FORM_sec_offset:
23145 fprintf_unfiltered (f, "section offset: %s",
23146 pulongest (DW_UNSND (&die->attrs[i])));
23147 break;
55f1336d 23148 case DW_FORM_ref_sig8:
ac9ec31b
DE
23149 fprintf_unfiltered (f, "signature: %s",
23150 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23151 break;
c906108c 23152 case DW_FORM_string:
4bdf3d34 23153 case DW_FORM_strp:
43988095 23154 case DW_FORM_line_strp:
cf532bd1 23155 case DW_FORM_strx:
3019eac3 23156 case DW_FORM_GNU_str_index:
36586728 23157 case DW_FORM_GNU_strp_alt:
8285870a 23158 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23159 DW_STRING (&die->attrs[i])
8285870a
JK
23160 ? DW_STRING (&die->attrs[i]) : "",
23161 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23162 break;
23163 case DW_FORM_flag:
23164 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23165 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23166 else
d97bc12b 23167 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23168 break;
2dc7f7b3
TT
23169 case DW_FORM_flag_present:
23170 fprintf_unfiltered (f, "flag: TRUE");
23171 break;
a8329558 23172 case DW_FORM_indirect:
0963b4bd
MS
23173 /* The reader will have reduced the indirect form to
23174 the "base form" so this form should not occur. */
5f48f8f3 23175 fprintf_unfiltered (f,
3e43a32a 23176 "unexpected attribute form: DW_FORM_indirect");
a8329558 23177 break;
663c44ac
JK
23178 case DW_FORM_implicit_const:
23179 fprintf_unfiltered (f, "constant: %s",
23180 plongest (DW_SND (&die->attrs[i])));
23181 break;
c906108c 23182 default:
d97bc12b 23183 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23184 die->attrs[i].form);
d97bc12b 23185 break;
c906108c 23186 }
d97bc12b 23187 fprintf_unfiltered (f, "\n");
c906108c
SS
23188 }
23189}
23190
f9aca02d 23191static void
d97bc12b 23192dump_die_for_error (struct die_info *die)
c906108c 23193{
d97bc12b
DE
23194 dump_die_shallow (gdb_stderr, 0, die);
23195}
23196
23197static void
23198dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23199{
23200 int indent = level * 4;
23201
23202 gdb_assert (die != NULL);
23203
23204 if (level >= max_level)
23205 return;
23206
23207 dump_die_shallow (f, indent, die);
23208
23209 if (die->child != NULL)
c906108c 23210 {
d97bc12b
DE
23211 print_spaces (indent, f);
23212 fprintf_unfiltered (f, " Children:");
23213 if (level + 1 < max_level)
23214 {
23215 fprintf_unfiltered (f, "\n");
23216 dump_die_1 (f, level + 1, max_level, die->child);
23217 }
23218 else
23219 {
3e43a32a
MS
23220 fprintf_unfiltered (f,
23221 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23222 }
23223 }
23224
23225 if (die->sibling != NULL && level > 0)
23226 {
23227 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23228 }
23229}
23230
d97bc12b
DE
23231/* This is called from the pdie macro in gdbinit.in.
23232 It's not static so gcc will keep a copy callable from gdb. */
23233
23234void
23235dump_die (struct die_info *die, int max_level)
23236{
23237 dump_die_1 (gdb_stdlog, 0, max_level, die);
23238}
23239
f9aca02d 23240static void
51545339 23241store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23242{
51545339 23243 void **slot;
c906108c 23244
9c541725
PA
23245 slot = htab_find_slot_with_hash (cu->die_hash, die,
23246 to_underlying (die->sect_off),
b64f50a1 23247 INSERT);
51545339
DJ
23248
23249 *slot = die;
c906108c
SS
23250}
23251
b64f50a1
JK
23252/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23253 required kind. */
23254
23255static sect_offset
ff39bb5e 23256dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23257{
7771576e 23258 if (attr_form_is_ref (attr))
9c541725 23259 return (sect_offset) DW_UNSND (attr);
93311388 23260
b98664d3 23261 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23262 dwarf_form_name (attr->form));
9c541725 23263 return {};
c906108c
SS
23264}
23265
43bbcdc2
PH
23266/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23267 * the value held by the attribute is not constant. */
a02abb62 23268
43bbcdc2 23269static LONGEST
ff39bb5e 23270dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23271{
663c44ac 23272 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23273 return DW_SND (attr);
23274 else if (attr->form == DW_FORM_udata
23275 || attr->form == DW_FORM_data1
23276 || attr->form == DW_FORM_data2
23277 || attr->form == DW_FORM_data4
23278 || attr->form == DW_FORM_data8)
23279 return DW_UNSND (attr);
23280 else
23281 {
0224619f 23282 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23283 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23284 dwarf_form_name (attr->form));
23285 return default_value;
23286 }
23287}
23288
348e048f
DE
23289/* Follow reference or signature attribute ATTR of SRC_DIE.
23290 On entry *REF_CU is the CU of SRC_DIE.
23291 On exit *REF_CU is the CU of the result. */
23292
23293static struct die_info *
ff39bb5e 23294follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23295 struct dwarf2_cu **ref_cu)
23296{
23297 struct die_info *die;
23298
7771576e 23299 if (attr_form_is_ref (attr))
348e048f 23300 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23301 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23302 die = follow_die_sig (src_die, attr, ref_cu);
23303 else
23304 {
23305 dump_die_for_error (src_die);
23306 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23307 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23308 }
23309
23310 return die;
03dd20cc
DJ
23311}
23312
5c631832 23313/* Follow reference OFFSET.
673bfd45
DE
23314 On entry *REF_CU is the CU of the source die referencing OFFSET.
23315 On exit *REF_CU is the CU of the result.
23316 Returns NULL if OFFSET is invalid. */
f504f079 23317
f9aca02d 23318static struct die_info *
9c541725 23319follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23320 struct dwarf2_cu **ref_cu)
c906108c 23321{
10b3939b 23322 struct die_info temp_die;
f2f0e013 23323 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23324 struct dwarf2_per_objfile *dwarf2_per_objfile
23325 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23326
348e048f
DE
23327 gdb_assert (cu->per_cu != NULL);
23328
98bfdba5
PA
23329 target_cu = cu;
23330
3019eac3 23331 if (cu->per_cu->is_debug_types)
348e048f
DE
23332 {
23333 /* .debug_types CUs cannot reference anything outside their CU.
23334 If they need to, they have to reference a signatured type via
55f1336d 23335 DW_FORM_ref_sig8. */
9c541725 23336 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23337 return NULL;
348e048f 23338 }
36586728 23339 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23340 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23341 {
23342 struct dwarf2_per_cu_data *per_cu;
9a619af0 23343
9c541725 23344 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23345 dwarf2_per_objfile);
03dd20cc
DJ
23346
23347 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23348 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23349 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23350
10b3939b
DJ
23351 target_cu = per_cu->cu;
23352 }
98bfdba5
PA
23353 else if (cu->dies == NULL)
23354 {
23355 /* We're loading full DIEs during partial symbol reading. */
23356 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23357 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23358 }
c906108c 23359
f2f0e013 23360 *ref_cu = target_cu;
9c541725 23361 temp_die.sect_off = sect_off;
c24bdb02
KS
23362
23363 if (target_cu != cu)
23364 target_cu->ancestor = cu;
23365
9a3c8263 23366 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23367 &temp_die,
23368 to_underlying (sect_off));
5c631832 23369}
10b3939b 23370
5c631832
JK
23371/* Follow reference attribute ATTR of SRC_DIE.
23372 On entry *REF_CU is the CU of SRC_DIE.
23373 On exit *REF_CU is the CU of the result. */
23374
23375static struct die_info *
ff39bb5e 23376follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23377 struct dwarf2_cu **ref_cu)
23378{
9c541725 23379 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23380 struct dwarf2_cu *cu = *ref_cu;
23381 struct die_info *die;
23382
9c541725 23383 die = follow_die_offset (sect_off,
36586728
TT
23384 (attr->form == DW_FORM_GNU_ref_alt
23385 || cu->per_cu->is_dwz),
23386 ref_cu);
5c631832 23387 if (!die)
9d8780f0
SM
23388 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23389 "at %s [in module %s]"),
23390 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23391 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23392
5c631832
JK
23393 return die;
23394}
23395
9c541725 23396/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23397 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23398 dwarf2_locexpr_baton->data has lifetime of
23399 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23400
23401struct dwarf2_locexpr_baton
9c541725 23402dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23403 struct dwarf2_per_cu_data *per_cu,
23404 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23405 void *baton, bool resolve_abstract_p)
5c631832 23406{
918dd910 23407 struct dwarf2_cu *cu;
5c631832
JK
23408 struct die_info *die;
23409 struct attribute *attr;
23410 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23411 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23412 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23413
918dd910 23414 if (per_cu->cu == NULL)
58f0c718 23415 load_cu (per_cu, false);
918dd910 23416 cu = per_cu->cu;
cc12ce38
DE
23417 if (cu == NULL)
23418 {
23419 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23420 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23421 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23422 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23423 }
918dd910 23424
9c541725 23425 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23426 if (!die)
9d8780f0
SM
23427 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23428 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23429
23430 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23431 if (!attr && resolve_abstract_p
3360b6e7 23432 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23433 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23434 {
23435 CORE_ADDR pc = (*get_frame_pc) (baton);
eba4caf2
TV
23436 CORE_ADDR baseaddr
23437 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23438 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23439
3360b6e7
TV
23440 for (const auto &cand_off
23441 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23442 {
3360b6e7
TV
23443 struct dwarf2_cu *cand_cu = cu;
23444 struct die_info *cand
23445 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23446 if (!cand
23447 || !cand->parent
e4a62c65
TV
23448 || cand->parent->tag != DW_TAG_subprogram)
23449 continue;
23450
23451 CORE_ADDR pc_low, pc_high;
23452 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23453 if (pc_low == ((CORE_ADDR) -1))
23454 continue;
23455 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23456 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23457 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23458 continue;
23459
23460 die = cand;
23461 attr = dwarf2_attr (die, DW_AT_location, cu);
23462 break;
23463 }
23464 }
23465
5c631832
JK
23466 if (!attr)
23467 {
e103e986
JK
23468 /* DWARF: "If there is no such attribute, then there is no effect.".
23469 DATA is ignored if SIZE is 0. */
5c631832 23470
e103e986 23471 retval.data = NULL;
5c631832
JK
23472 retval.size = 0;
23473 }
8cf6f0b1
TT
23474 else if (attr_form_is_section_offset (attr))
23475 {
23476 struct dwarf2_loclist_baton loclist_baton;
23477 CORE_ADDR pc = (*get_frame_pc) (baton);
23478 size_t size;
23479
23480 fill_in_loclist_baton (cu, &loclist_baton, attr);
23481
23482 retval.data = dwarf2_find_location_expression (&loclist_baton,
23483 &size, pc);
23484 retval.size = size;
23485 }
5c631832
JK
23486 else
23487 {
23488 if (!attr_form_is_block (attr))
9d8780f0 23489 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23490 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23491 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23492
23493 retval.data = DW_BLOCK (attr)->data;
23494 retval.size = DW_BLOCK (attr)->size;
23495 }
23496 retval.per_cu = cu->per_cu;
918dd910 23497
ed2dc618 23498 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23499
5c631832 23500 return retval;
348e048f
DE
23501}
23502
8b9737bf
TT
23503/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23504 offset. */
23505
23506struct dwarf2_locexpr_baton
23507dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23508 struct dwarf2_per_cu_data *per_cu,
23509 CORE_ADDR (*get_frame_pc) (void *baton),
23510 void *baton)
23511{
9c541725 23512 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23513
9c541725 23514 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23515}
23516
b6807d98
TT
23517/* Write a constant of a given type as target-ordered bytes into
23518 OBSTACK. */
23519
23520static const gdb_byte *
23521write_constant_as_bytes (struct obstack *obstack,
23522 enum bfd_endian byte_order,
23523 struct type *type,
23524 ULONGEST value,
23525 LONGEST *len)
23526{
23527 gdb_byte *result;
23528
23529 *len = TYPE_LENGTH (type);
224c3ddb 23530 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23531 store_unsigned_integer (result, *len, byte_order, value);
23532
23533 return result;
23534}
23535
23536/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23537 pointer to the constant bytes and set LEN to the length of the
23538 data. If memory is needed, allocate it on OBSTACK. If the DIE
23539 does not have a DW_AT_const_value, return NULL. */
23540
23541const gdb_byte *
9c541725 23542dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23543 struct dwarf2_per_cu_data *per_cu,
23544 struct obstack *obstack,
23545 LONGEST *len)
23546{
23547 struct dwarf2_cu *cu;
23548 struct die_info *die;
23549 struct attribute *attr;
23550 const gdb_byte *result = NULL;
23551 struct type *type;
23552 LONGEST value;
23553 enum bfd_endian byte_order;
e3b94546 23554 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23555
b6807d98 23556 if (per_cu->cu == NULL)
58f0c718 23557 load_cu (per_cu, false);
b6807d98 23558 cu = per_cu->cu;
cc12ce38
DE
23559 if (cu == NULL)
23560 {
23561 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23562 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23563 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23564 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23565 }
b6807d98 23566
9c541725 23567 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23568 if (!die)
9d8780f0
SM
23569 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23570 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23571
23572 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23573 if (attr == NULL)
23574 return NULL;
23575
e3b94546 23576 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23577 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23578
23579 switch (attr->form)
23580 {
23581 case DW_FORM_addr:
336d760d 23582 case DW_FORM_addrx:
b6807d98
TT
23583 case DW_FORM_GNU_addr_index:
23584 {
23585 gdb_byte *tem;
23586
23587 *len = cu->header.addr_size;
224c3ddb 23588 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23589 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23590 result = tem;
23591 }
23592 break;
23593 case DW_FORM_string:
23594 case DW_FORM_strp:
cf532bd1 23595 case DW_FORM_strx:
b6807d98
TT
23596 case DW_FORM_GNU_str_index:
23597 case DW_FORM_GNU_strp_alt:
23598 /* DW_STRING is already allocated on the objfile obstack, point
23599 directly to it. */
23600 result = (const gdb_byte *) DW_STRING (attr);
23601 *len = strlen (DW_STRING (attr));
23602 break;
23603 case DW_FORM_block1:
23604 case DW_FORM_block2:
23605 case DW_FORM_block4:
23606 case DW_FORM_block:
23607 case DW_FORM_exprloc:
0224619f 23608 case DW_FORM_data16:
b6807d98
TT
23609 result = DW_BLOCK (attr)->data;
23610 *len = DW_BLOCK (attr)->size;
23611 break;
23612
23613 /* The DW_AT_const_value attributes are supposed to carry the
23614 symbol's value "represented as it would be on the target
23615 architecture." By the time we get here, it's already been
23616 converted to host endianness, so we just need to sign- or
23617 zero-extend it as appropriate. */
23618 case DW_FORM_data1:
23619 type = die_type (die, cu);
23620 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23621 if (result == NULL)
23622 result = write_constant_as_bytes (obstack, byte_order,
23623 type, value, len);
23624 break;
23625 case DW_FORM_data2:
23626 type = die_type (die, cu);
23627 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23628 if (result == NULL)
23629 result = write_constant_as_bytes (obstack, byte_order,
23630 type, value, len);
23631 break;
23632 case DW_FORM_data4:
23633 type = die_type (die, cu);
23634 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23635 if (result == NULL)
23636 result = write_constant_as_bytes (obstack, byte_order,
23637 type, value, len);
23638 break;
23639 case DW_FORM_data8:
23640 type = die_type (die, cu);
23641 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23642 if (result == NULL)
23643 result = write_constant_as_bytes (obstack, byte_order,
23644 type, value, len);
23645 break;
23646
23647 case DW_FORM_sdata:
663c44ac 23648 case DW_FORM_implicit_const:
b6807d98
TT
23649 type = die_type (die, cu);
23650 result = write_constant_as_bytes (obstack, byte_order,
23651 type, DW_SND (attr), len);
23652 break;
23653
23654 case DW_FORM_udata:
23655 type = die_type (die, cu);
23656 result = write_constant_as_bytes (obstack, byte_order,
23657 type, DW_UNSND (attr), len);
23658 break;
23659
23660 default:
b98664d3 23661 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23662 dwarf_form_name (attr->form));
23663 break;
23664 }
23665
23666 return result;
23667}
23668
7942e96e
AA
23669/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23670 valid type for this die is found. */
23671
23672struct type *
9c541725 23673dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23674 struct dwarf2_per_cu_data *per_cu)
23675{
23676 struct dwarf2_cu *cu;
23677 struct die_info *die;
23678
7942e96e 23679 if (per_cu->cu == NULL)
58f0c718 23680 load_cu (per_cu, false);
7942e96e
AA
23681 cu = per_cu->cu;
23682 if (!cu)
23683 return NULL;
23684
9c541725 23685 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23686 if (!die)
23687 return NULL;
23688
23689 return die_type (die, cu);
23690}
23691
8a9b8146
TT
23692/* Return the type of the DIE at DIE_OFFSET in the CU named by
23693 PER_CU. */
23694
23695struct type *
b64f50a1 23696dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23697 struct dwarf2_per_cu_data *per_cu)
23698{
9c541725 23699 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23700 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23701}
23702
ac9ec31b 23703/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23704 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23705 On exit *REF_CU is the CU of the result.
23706 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23707
23708static struct die_info *
ac9ec31b
DE
23709follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23710 struct dwarf2_cu **ref_cu)
348e048f 23711{
348e048f 23712 struct die_info temp_die;
c24bdb02 23713 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23714 struct die_info *die;
23715
ac9ec31b
DE
23716 /* While it might be nice to assert sig_type->type == NULL here,
23717 we can get here for DW_AT_imported_declaration where we need
23718 the DIE not the type. */
348e048f
DE
23719
23720 /* If necessary, add it to the queue and load its DIEs. */
23721
95554aad 23722 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23723 read_signatured_type (sig_type);
348e048f 23724
348e048f 23725 sig_cu = sig_type->per_cu.cu;
69d751e3 23726 gdb_assert (sig_cu != NULL);
9c541725
PA
23727 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23728 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23729 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23730 to_underlying (temp_die.sect_off));
348e048f
DE
23731 if (die)
23732 {
ed2dc618 23733 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23734 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23735
796a7ff8
DE
23736 /* For .gdb_index version 7 keep track of included TUs.
23737 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23738 if (dwarf2_per_objfile->index_table != NULL
23739 && dwarf2_per_objfile->index_table->version <= 7)
23740 {
ae640021 23741 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23742 }
23743
348e048f 23744 *ref_cu = sig_cu;
c24bdb02
KS
23745 if (sig_cu != cu)
23746 sig_cu->ancestor = cu;
23747
348e048f
DE
23748 return die;
23749 }
23750
ac9ec31b
DE
23751 return NULL;
23752}
23753
23754/* Follow signatured type referenced by ATTR in SRC_DIE.
23755 On entry *REF_CU is the CU of SRC_DIE.
23756 On exit *REF_CU is the CU of the result.
23757 The result is the DIE of the type.
23758 If the referenced type cannot be found an error is thrown. */
23759
23760static struct die_info *
ff39bb5e 23761follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23762 struct dwarf2_cu **ref_cu)
23763{
23764 ULONGEST signature = DW_SIGNATURE (attr);
23765 struct signatured_type *sig_type;
23766 struct die_info *die;
23767
23768 gdb_assert (attr->form == DW_FORM_ref_sig8);
23769
a2ce51a0 23770 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23771 /* sig_type will be NULL if the signatured type is missing from
23772 the debug info. */
23773 if (sig_type == NULL)
23774 {
23775 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23776 " from DIE at %s [in module %s]"),
23777 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23778 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23779 }
23780
23781 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23782 if (die == NULL)
23783 {
23784 dump_die_for_error (src_die);
23785 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23786 " from DIE at %s [in module %s]"),
23787 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23788 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23789 }
23790
23791 return die;
23792}
23793
23794/* Get the type specified by SIGNATURE referenced in DIE/CU,
23795 reading in and processing the type unit if necessary. */
23796
23797static struct type *
23798get_signatured_type (struct die_info *die, ULONGEST signature,
23799 struct dwarf2_cu *cu)
23800{
518817b3
SM
23801 struct dwarf2_per_objfile *dwarf2_per_objfile
23802 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23803 struct signatured_type *sig_type;
23804 struct dwarf2_cu *type_cu;
23805 struct die_info *type_die;
23806 struct type *type;
23807
a2ce51a0 23808 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23809 /* sig_type will be NULL if the signatured type is missing from
23810 the debug info. */
23811 if (sig_type == NULL)
23812 {
b98664d3 23813 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23814 " from DIE at %s [in module %s]"),
23815 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23816 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23817 return build_error_marker_type (cu, die);
23818 }
23819
23820 /* If we already know the type we're done. */
23821 if (sig_type->type != NULL)
23822 return sig_type->type;
23823
23824 type_cu = cu;
23825 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23826 if (type_die != NULL)
23827 {
23828 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23829 is created. This is important, for example, because for c++ classes
23830 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23831 type = read_type_die (type_die, type_cu);
23832 if (type == NULL)
23833 {
b98664d3 23834 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23835 " referenced from DIE at %s [in module %s]"),
23836 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23837 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23838 type = build_error_marker_type (cu, die);
23839 }
23840 }
23841 else
23842 {
b98664d3 23843 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23844 " from DIE at %s [in module %s]"),
23845 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23846 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23847 type = build_error_marker_type (cu, die);
23848 }
23849 sig_type->type = type;
23850
23851 return type;
23852}
23853
23854/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23855 reading in and processing the type unit if necessary. */
23856
23857static struct type *
ff39bb5e 23858get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23859 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23860{
23861 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23862 if (attr_form_is_ref (attr))
ac9ec31b
DE
23863 {
23864 struct dwarf2_cu *type_cu = cu;
23865 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23866
23867 return read_type_die (type_die, type_cu);
23868 }
23869 else if (attr->form == DW_FORM_ref_sig8)
23870 {
23871 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23872 }
23873 else
23874 {
518817b3
SM
23875 struct dwarf2_per_objfile *dwarf2_per_objfile
23876 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23877
b98664d3 23878 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23879 " at %s [in module %s]"),
23880 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23881 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23882 return build_error_marker_type (cu, die);
23883 }
348e048f
DE
23884}
23885
e5fe5e75 23886/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23887
23888static void
e5fe5e75 23889load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23890{
52dc124a 23891 struct signatured_type *sig_type;
348e048f 23892
f4dc4d17
DE
23893 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23894 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23895
6721b2ec
DE
23896 /* We have the per_cu, but we need the signatured_type.
23897 Fortunately this is an easy translation. */
23898 gdb_assert (per_cu->is_debug_types);
23899 sig_type = (struct signatured_type *) per_cu;
348e048f 23900
6721b2ec 23901 gdb_assert (per_cu->cu == NULL);
348e048f 23902
52dc124a 23903 read_signatured_type (sig_type);
348e048f 23904
6721b2ec 23905 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23906}
23907
dee91e82
DE
23908/* die_reader_func for read_signatured_type.
23909 This is identical to load_full_comp_unit_reader,
23910 but is kept separate for now. */
348e048f
DE
23911
23912static void
dee91e82 23913read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23914 const gdb_byte *info_ptr,
dee91e82
DE
23915 struct die_info *comp_unit_die,
23916 int has_children,
23917 void *data)
348e048f 23918{
dee91e82 23919 struct dwarf2_cu *cu = reader->cu;
348e048f 23920
dee91e82
DE
23921 gdb_assert (cu->die_hash == NULL);
23922 cu->die_hash =
23923 htab_create_alloc_ex (cu->header.length / 12,
23924 die_hash,
23925 die_eq,
23926 NULL,
23927 &cu->comp_unit_obstack,
23928 hashtab_obstack_allocate,
23929 dummy_obstack_deallocate);
348e048f 23930
dee91e82
DE
23931 if (has_children)
23932 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23933 &info_ptr, comp_unit_die);
23934 cu->dies = comp_unit_die;
23935 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23936
23937 /* We try not to read any attributes in this function, because not
9cdd5dbd 23938 all CUs needed for references have been loaded yet, and symbol
348e048f 23939 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23940 or we won't be able to build types correctly.
23941 Similarly, if we do not read the producer, we can not apply
23942 producer-specific interpretation. */
95554aad 23943 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23944}
348e048f 23945
3019eac3
DE
23946/* Read in a signatured type and build its CU and DIEs.
23947 If the type is a stub for the real type in a DWO file,
23948 read in the real type from the DWO file as well. */
dee91e82
DE
23949
23950static void
23951read_signatured_type (struct signatured_type *sig_type)
23952{
23953 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23954
3019eac3 23955 gdb_assert (per_cu->is_debug_types);
dee91e82 23956 gdb_assert (per_cu->cu == NULL);
348e048f 23957
58f0c718 23958 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23959 read_signatured_type_reader, NULL);
7ee85ab1 23960 sig_type->per_cu.tu_read = 1;
c906108c
SS
23961}
23962
c906108c
SS
23963/* Decode simple location descriptions.
23964 Given a pointer to a dwarf block that defines a location, compute
23965 the location and return the value.
23966
4cecd739
DJ
23967 NOTE drow/2003-11-18: This function is called in two situations
23968 now: for the address of static or global variables (partial symbols
23969 only) and for offsets into structures which are expected to be
23970 (more or less) constant. The partial symbol case should go away,
23971 and only the constant case should remain. That will let this
23972 function complain more accurately. A few special modes are allowed
23973 without complaint for global variables (for instance, global
23974 register values and thread-local values).
c906108c
SS
23975
23976 A location description containing no operations indicates that the
4cecd739 23977 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23978 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23979 callers will only want a very basic result and this can become a
21ae7a4d
JK
23980 complaint.
23981
23982 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23983
23984static CORE_ADDR
e7c27a73 23985decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23986{
518817b3 23987 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23988 size_t i;
23989 size_t size = blk->size;
d521ce57 23990 const gdb_byte *data = blk->data;
21ae7a4d
JK
23991 CORE_ADDR stack[64];
23992 int stacki;
23993 unsigned int bytes_read, unsnd;
23994 gdb_byte op;
c906108c 23995
21ae7a4d
JK
23996 i = 0;
23997 stacki = 0;
23998 stack[stacki] = 0;
23999 stack[++stacki] = 0;
24000
24001 while (i < size)
24002 {
24003 op = data[i++];
24004 switch (op)
24005 {
24006 case DW_OP_lit0:
24007 case DW_OP_lit1:
24008 case DW_OP_lit2:
24009 case DW_OP_lit3:
24010 case DW_OP_lit4:
24011 case DW_OP_lit5:
24012 case DW_OP_lit6:
24013 case DW_OP_lit7:
24014 case DW_OP_lit8:
24015 case DW_OP_lit9:
24016 case DW_OP_lit10:
24017 case DW_OP_lit11:
24018 case DW_OP_lit12:
24019 case DW_OP_lit13:
24020 case DW_OP_lit14:
24021 case DW_OP_lit15:
24022 case DW_OP_lit16:
24023 case DW_OP_lit17:
24024 case DW_OP_lit18:
24025 case DW_OP_lit19:
24026 case DW_OP_lit20:
24027 case DW_OP_lit21:
24028 case DW_OP_lit22:
24029 case DW_OP_lit23:
24030 case DW_OP_lit24:
24031 case DW_OP_lit25:
24032 case DW_OP_lit26:
24033 case DW_OP_lit27:
24034 case DW_OP_lit28:
24035 case DW_OP_lit29:
24036 case DW_OP_lit30:
24037 case DW_OP_lit31:
24038 stack[++stacki] = op - DW_OP_lit0;
24039 break;
f1bea926 24040
21ae7a4d
JK
24041 case DW_OP_reg0:
24042 case DW_OP_reg1:
24043 case DW_OP_reg2:
24044 case DW_OP_reg3:
24045 case DW_OP_reg4:
24046 case DW_OP_reg5:
24047 case DW_OP_reg6:
24048 case DW_OP_reg7:
24049 case DW_OP_reg8:
24050 case DW_OP_reg9:
24051 case DW_OP_reg10:
24052 case DW_OP_reg11:
24053 case DW_OP_reg12:
24054 case DW_OP_reg13:
24055 case DW_OP_reg14:
24056 case DW_OP_reg15:
24057 case DW_OP_reg16:
24058 case DW_OP_reg17:
24059 case DW_OP_reg18:
24060 case DW_OP_reg19:
24061 case DW_OP_reg20:
24062 case DW_OP_reg21:
24063 case DW_OP_reg22:
24064 case DW_OP_reg23:
24065 case DW_OP_reg24:
24066 case DW_OP_reg25:
24067 case DW_OP_reg26:
24068 case DW_OP_reg27:
24069 case DW_OP_reg28:
24070 case DW_OP_reg29:
24071 case DW_OP_reg30:
24072 case DW_OP_reg31:
24073 stack[++stacki] = op - DW_OP_reg0;
24074 if (i < size)
24075 dwarf2_complex_location_expr_complaint ();
24076 break;
c906108c 24077
21ae7a4d
JK
24078 case DW_OP_regx:
24079 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24080 i += bytes_read;
24081 stack[++stacki] = unsnd;
24082 if (i < size)
24083 dwarf2_complex_location_expr_complaint ();
24084 break;
c906108c 24085
21ae7a4d
JK
24086 case DW_OP_addr:
24087 stack[++stacki] = read_address (objfile->obfd, &data[i],
24088 cu, &bytes_read);
24089 i += bytes_read;
24090 break;
d53d4ac5 24091
21ae7a4d
JK
24092 case DW_OP_const1u:
24093 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24094 i += 1;
24095 break;
24096
24097 case DW_OP_const1s:
24098 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24099 i += 1;
24100 break;
24101
24102 case DW_OP_const2u:
24103 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24104 i += 2;
24105 break;
24106
24107 case DW_OP_const2s:
24108 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24109 i += 2;
24110 break;
d53d4ac5 24111
21ae7a4d
JK
24112 case DW_OP_const4u:
24113 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24114 i += 4;
24115 break;
24116
24117 case DW_OP_const4s:
24118 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24119 i += 4;
24120 break;
24121
585861ea
JK
24122 case DW_OP_const8u:
24123 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24124 i += 8;
24125 break;
24126
21ae7a4d
JK
24127 case DW_OP_constu:
24128 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24129 &bytes_read);
24130 i += bytes_read;
24131 break;
24132
24133 case DW_OP_consts:
24134 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24135 i += bytes_read;
24136 break;
24137
24138 case DW_OP_dup:
24139 stack[stacki + 1] = stack[stacki];
24140 stacki++;
24141 break;
24142
24143 case DW_OP_plus:
24144 stack[stacki - 1] += stack[stacki];
24145 stacki--;
24146 break;
24147
24148 case DW_OP_plus_uconst:
24149 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24150 &bytes_read);
24151 i += bytes_read;
24152 break;
24153
24154 case DW_OP_minus:
24155 stack[stacki - 1] -= stack[stacki];
24156 stacki--;
24157 break;
24158
24159 case DW_OP_deref:
24160 /* If we're not the last op, then we definitely can't encode
24161 this using GDB's address_class enum. This is valid for partial
24162 global symbols, although the variable's address will be bogus
24163 in the psymtab. */
24164 if (i < size)
24165 dwarf2_complex_location_expr_complaint ();
24166 break;
24167
24168 case DW_OP_GNU_push_tls_address:
4aa4e28b 24169 case DW_OP_form_tls_address:
21ae7a4d
JK
24170 /* The top of the stack has the offset from the beginning
24171 of the thread control block at which the variable is located. */
24172 /* Nothing should follow this operator, so the top of stack would
24173 be returned. */
24174 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24175 address will be bogus in the psymtab. Make it always at least
24176 non-zero to not look as a variable garbage collected by linker
24177 which have DW_OP_addr 0. */
21ae7a4d
JK
24178 if (i < size)
24179 dwarf2_complex_location_expr_complaint ();
585861ea 24180 stack[stacki]++;
21ae7a4d
JK
24181 break;
24182
24183 case DW_OP_GNU_uninit:
24184 break;
24185
336d760d 24186 case DW_OP_addrx:
3019eac3 24187 case DW_OP_GNU_addr_index:
49f6c839 24188 case DW_OP_GNU_const_index:
3019eac3
DE
24189 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24190 &bytes_read);
24191 i += bytes_read;
24192 break;
24193
21ae7a4d
JK
24194 default:
24195 {
f39c6ffd 24196 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24197
24198 if (name)
b98664d3 24199 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24200 name);
24201 else
b98664d3 24202 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24203 op);
24204 }
24205
24206 return (stack[stacki]);
d53d4ac5 24207 }
3c6e0cb3 24208
21ae7a4d
JK
24209 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24210 outside of the allocated space. Also enforce minimum>0. */
24211 if (stacki >= ARRAY_SIZE (stack) - 1)
24212 {
b98664d3 24213 complaint (_("location description stack overflow"));
21ae7a4d
JK
24214 return 0;
24215 }
24216
24217 if (stacki <= 0)
24218 {
b98664d3 24219 complaint (_("location description stack underflow"));
21ae7a4d
JK
24220 return 0;
24221 }
24222 }
24223 return (stack[stacki]);
c906108c
SS
24224}
24225
24226/* memory allocation interface */
24227
c906108c 24228static struct dwarf_block *
7b5a2f43 24229dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24230{
8d749320 24231 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24232}
24233
c906108c 24234static struct die_info *
b60c80d6 24235dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24236{
24237 struct die_info *die;
b60c80d6
DJ
24238 size_t size = sizeof (struct die_info);
24239
24240 if (num_attrs > 1)
24241 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24242
b60c80d6 24243 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24244 memset (die, 0, sizeof (struct die_info));
24245 return (die);
24246}
2e276125
JB
24247
24248\f
24249/* Macro support. */
24250
233d95b5
JK
24251/* Return file name relative to the compilation directory of file number I in
24252 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24253 responsible for freeing it. */
233d95b5 24254
2e276125 24255static char *
233d95b5 24256file_file_name (int file, struct line_header *lh)
2e276125 24257{
6a83a1e6
EZ
24258 /* Is the file number a valid index into the line header's file name
24259 table? Remember that file numbers start with one, not zero. */
7ba99d21 24260 if (lh->is_valid_file_index (file))
6a83a1e6 24261 {
7ba99d21 24262 const file_entry *fe = lh->file_name_at (file);
6e70227d 24263
7ba99d21 24264 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24265 {
7ba99d21 24266 const char *dir = fe->include_dir (lh);
8c43009f 24267 if (dir != NULL)
7ba99d21 24268 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24269 }
7ba99d21 24270 return xstrdup (fe->name);
6a83a1e6 24271 }
2e276125
JB
24272 else
24273 {
6a83a1e6
EZ
24274 /* The compiler produced a bogus file number. We can at least
24275 record the macro definitions made in the file, even if we
24276 won't be able to find the file by name. */
24277 char fake_name[80];
9a619af0 24278
8c042590
PM
24279 xsnprintf (fake_name, sizeof (fake_name),
24280 "<bad macro file number %d>", file);
2e276125 24281
b98664d3 24282 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24283 file);
2e276125 24284
6a83a1e6 24285 return xstrdup (fake_name);
2e276125
JB
24286 }
24287}
24288
233d95b5
JK
24289/* Return the full name of file number I in *LH's file name table.
24290 Use COMP_DIR as the name of the current directory of the
24291 compilation. The result is allocated using xmalloc; the caller is
24292 responsible for freeing it. */
24293static char *
24294file_full_name (int file, struct line_header *lh, const char *comp_dir)
24295{
24296 /* Is the file number a valid index into the line header's file name
24297 table? Remember that file numbers start with one, not zero. */
7ba99d21 24298 if (lh->is_valid_file_index (file))
233d95b5
JK
24299 {
24300 char *relative = file_file_name (file, lh);
24301
24302 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24303 return relative;
b36cec19
PA
24304 return reconcat (relative, comp_dir, SLASH_STRING,
24305 relative, (char *) NULL);
233d95b5
JK
24306 }
24307 else
24308 return file_file_name (file, lh);
24309}
24310
2e276125
JB
24311
24312static struct macro_source_file *
804d2729
TT
24313macro_start_file (struct dwarf2_cu *cu,
24314 int file, int line,
2e276125 24315 struct macro_source_file *current_file,
43f3e411 24316 struct line_header *lh)
2e276125 24317{
233d95b5
JK
24318 /* File name relative to the compilation directory of this source file. */
24319 char *file_name = file_file_name (file, lh);
2e276125 24320
2e276125 24321 if (! current_file)
abc9d0dc 24322 {
fc474241
DE
24323 /* Note: We don't create a macro table for this compilation unit
24324 at all until we actually get a filename. */
c24bdb02 24325 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24326
abc9d0dc
TT
24327 /* If we have no current file, then this must be the start_file
24328 directive for the compilation unit's main source file. */
fc474241
DE
24329 current_file = macro_set_main (macro_table, file_name);
24330 macro_define_special (macro_table);
abc9d0dc 24331 }
2e276125 24332 else
233d95b5 24333 current_file = macro_include (current_file, line, file_name);
2e276125 24334
233d95b5 24335 xfree (file_name);
6e70227d 24336
2e276125
JB
24337 return current_file;
24338}
24339
2e276125
JB
24340static const char *
24341consume_improper_spaces (const char *p, const char *body)
24342{
24343 if (*p == ' ')
24344 {
b98664d3 24345 complaint (_("macro definition contains spaces "
3e43a32a 24346 "in formal argument list:\n`%s'"),
4d3c2250 24347 body);
2e276125
JB
24348
24349 while (*p == ' ')
24350 p++;
24351 }
24352
24353 return p;
24354}
24355
24356
24357static void
24358parse_macro_definition (struct macro_source_file *file, int line,
24359 const char *body)
24360{
24361 const char *p;
24362
24363 /* The body string takes one of two forms. For object-like macro
24364 definitions, it should be:
24365
24366 <macro name> " " <definition>
24367
24368 For function-like macro definitions, it should be:
24369
24370 <macro name> "() " <definition>
24371 or
24372 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24373
24374 Spaces may appear only where explicitly indicated, and in the
24375 <definition>.
24376
24377 The Dwarf 2 spec says that an object-like macro's name is always
24378 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24379 the space when the macro's definition is the empty string.
2e276125
JB
24380
24381 The Dwarf 2 spec says that there should be no spaces between the
24382 formal arguments in a function-like macro's formal argument list,
24383 but versions of GCC around March 2002 include spaces after the
24384 commas. */
24385
24386
24387 /* Find the extent of the macro name. The macro name is terminated
24388 by either a space or null character (for an object-like macro) or
24389 an opening paren (for a function-like macro). */
24390 for (p = body; *p; p++)
24391 if (*p == ' ' || *p == '(')
24392 break;
24393
24394 if (*p == ' ' || *p == '\0')
24395 {
24396 /* It's an object-like macro. */
24397 int name_len = p - body;
3f8a7804 24398 char *name = savestring (body, name_len);
2e276125
JB
24399 const char *replacement;
24400
24401 if (*p == ' ')
24402 replacement = body + name_len + 1;
24403 else
24404 {
4d3c2250 24405 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24406 replacement = body + name_len;
24407 }
6e70227d 24408
2e276125
JB
24409 macro_define_object (file, line, name, replacement);
24410
24411 xfree (name);
24412 }
24413 else if (*p == '(')
24414 {
24415 /* It's a function-like macro. */
3f8a7804 24416 char *name = savestring (body, p - body);
2e276125
JB
24417 int argc = 0;
24418 int argv_size = 1;
8d749320 24419 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24420
24421 p++;
24422
24423 p = consume_improper_spaces (p, body);
24424
24425 /* Parse the formal argument list. */
24426 while (*p && *p != ')')
24427 {
24428 /* Find the extent of the current argument name. */
24429 const char *arg_start = p;
24430
24431 while (*p && *p != ',' && *p != ')' && *p != ' ')
24432 p++;
24433
24434 if (! *p || p == arg_start)
4d3c2250 24435 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24436 else
24437 {
24438 /* Make sure argv has room for the new argument. */
24439 if (argc >= argv_size)
24440 {
24441 argv_size *= 2;
224c3ddb 24442 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24443 }
24444
3f8a7804 24445 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24446 }
24447
24448 p = consume_improper_spaces (p, body);
24449
24450 /* Consume the comma, if present. */
24451 if (*p == ',')
24452 {
24453 p++;
24454
24455 p = consume_improper_spaces (p, body);
24456 }
24457 }
24458
24459 if (*p == ')')
24460 {
24461 p++;
24462
24463 if (*p == ' ')
24464 /* Perfectly formed definition, no complaints. */
24465 macro_define_function (file, line, name,
6e70227d 24466 argc, (const char **) argv,
2e276125
JB
24467 p + 1);
24468 else if (*p == '\0')
24469 {
24470 /* Complain, but do define it. */
4d3c2250 24471 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24472 macro_define_function (file, line, name,
6e70227d 24473 argc, (const char **) argv,
2e276125
JB
24474 p);
24475 }
24476 else
24477 /* Just complain. */
4d3c2250 24478 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24479 }
24480 else
24481 /* Just complain. */
4d3c2250 24482 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24483
24484 xfree (name);
24485 {
24486 int i;
24487
24488 for (i = 0; i < argc; i++)
24489 xfree (argv[i]);
24490 }
24491 xfree (argv);
24492 }
24493 else
4d3c2250 24494 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24495}
24496
cf2c3c16
TT
24497/* Skip some bytes from BYTES according to the form given in FORM.
24498 Returns the new pointer. */
2e276125 24499
d521ce57
TT
24500static const gdb_byte *
24501skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24502 enum dwarf_form form,
24503 unsigned int offset_size,
24504 struct dwarf2_section_info *section)
2e276125 24505{
cf2c3c16 24506 unsigned int bytes_read;
2e276125 24507
cf2c3c16 24508 switch (form)
2e276125 24509 {
cf2c3c16
TT
24510 case DW_FORM_data1:
24511 case DW_FORM_flag:
24512 ++bytes;
24513 break;
24514
24515 case DW_FORM_data2:
24516 bytes += 2;
24517 break;
24518
24519 case DW_FORM_data4:
24520 bytes += 4;
24521 break;
24522
24523 case DW_FORM_data8:
24524 bytes += 8;
24525 break;
24526
0224619f
JK
24527 case DW_FORM_data16:
24528 bytes += 16;
24529 break;
24530
cf2c3c16
TT
24531 case DW_FORM_string:
24532 read_direct_string (abfd, bytes, &bytes_read);
24533 bytes += bytes_read;
24534 break;
24535
24536 case DW_FORM_sec_offset:
24537 case DW_FORM_strp:
36586728 24538 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24539 bytes += offset_size;
24540 break;
24541
24542 case DW_FORM_block:
24543 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24544 bytes += bytes_read;
24545 break;
24546
24547 case DW_FORM_block1:
24548 bytes += 1 + read_1_byte (abfd, bytes);
24549 break;
24550 case DW_FORM_block2:
24551 bytes += 2 + read_2_bytes (abfd, bytes);
24552 break;
24553 case DW_FORM_block4:
24554 bytes += 4 + read_4_bytes (abfd, bytes);
24555 break;
24556
336d760d 24557 case DW_FORM_addrx:
cf2c3c16 24558 case DW_FORM_sdata:
cf532bd1 24559 case DW_FORM_strx:
cf2c3c16 24560 case DW_FORM_udata:
3019eac3
DE
24561 case DW_FORM_GNU_addr_index:
24562 case DW_FORM_GNU_str_index:
d521ce57 24563 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24564 if (bytes == NULL)
24565 {
24566 dwarf2_section_buffer_overflow_complaint (section);
24567 return NULL;
24568 }
cf2c3c16
TT
24569 break;
24570
663c44ac
JK
24571 case DW_FORM_implicit_const:
24572 break;
24573
cf2c3c16
TT
24574 default:
24575 {
b98664d3 24576 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24577 form, get_section_name (section));
cf2c3c16
TT
24578 return NULL;
24579 }
2e276125
JB
24580 }
24581
cf2c3c16
TT
24582 return bytes;
24583}
757a13d0 24584
cf2c3c16
TT
24585/* A helper for dwarf_decode_macros that handles skipping an unknown
24586 opcode. Returns an updated pointer to the macro data buffer; or,
24587 on error, issues a complaint and returns NULL. */
757a13d0 24588
d521ce57 24589static const gdb_byte *
cf2c3c16 24590skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24591 const gdb_byte **opcode_definitions,
24592 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24593 bfd *abfd,
24594 unsigned int offset_size,
24595 struct dwarf2_section_info *section)
24596{
24597 unsigned int bytes_read, i;
24598 unsigned long arg;
d521ce57 24599 const gdb_byte *defn;
2e276125 24600
cf2c3c16 24601 if (opcode_definitions[opcode] == NULL)
2e276125 24602 {
b98664d3 24603 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24604 opcode);
24605 return NULL;
24606 }
2e276125 24607
cf2c3c16
TT
24608 defn = opcode_definitions[opcode];
24609 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24610 defn += bytes_read;
2e276125 24611
cf2c3c16
TT
24612 for (i = 0; i < arg; ++i)
24613 {
aead7601
SM
24614 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24615 (enum dwarf_form) defn[i], offset_size,
f664829e 24616 section);
cf2c3c16
TT
24617 if (mac_ptr == NULL)
24618 {
24619 /* skip_form_bytes already issued the complaint. */
24620 return NULL;
24621 }
24622 }
757a13d0 24623
cf2c3c16
TT
24624 return mac_ptr;
24625}
757a13d0 24626
cf2c3c16
TT
24627/* A helper function which parses the header of a macro section.
24628 If the macro section is the extended (for now called "GNU") type,
24629 then this updates *OFFSET_SIZE. Returns a pointer to just after
24630 the header, or issues a complaint and returns NULL on error. */
757a13d0 24631
d521ce57
TT
24632static const gdb_byte *
24633dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24634 bfd *abfd,
d521ce57 24635 const gdb_byte *mac_ptr,
cf2c3c16
TT
24636 unsigned int *offset_size,
24637 int section_is_gnu)
24638{
24639 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24640
cf2c3c16
TT
24641 if (section_is_gnu)
24642 {
24643 unsigned int version, flags;
757a13d0 24644
cf2c3c16 24645 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24646 if (version != 4 && version != 5)
cf2c3c16 24647 {
b98664d3 24648 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24649 version);
24650 return NULL;
24651 }
24652 mac_ptr += 2;
757a13d0 24653
cf2c3c16
TT
24654 flags = read_1_byte (abfd, mac_ptr);
24655 ++mac_ptr;
24656 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24657
cf2c3c16
TT
24658 if ((flags & 2) != 0)
24659 /* We don't need the line table offset. */
24660 mac_ptr += *offset_size;
757a13d0 24661
cf2c3c16
TT
24662 /* Vendor opcode descriptions. */
24663 if ((flags & 4) != 0)
24664 {
24665 unsigned int i, count;
757a13d0 24666
cf2c3c16
TT
24667 count = read_1_byte (abfd, mac_ptr);
24668 ++mac_ptr;
24669 for (i = 0; i < count; ++i)
24670 {
24671 unsigned int opcode, bytes_read;
24672 unsigned long arg;
24673
24674 opcode = read_1_byte (abfd, mac_ptr);
24675 ++mac_ptr;
24676 opcode_definitions[opcode] = mac_ptr;
24677 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24678 mac_ptr += bytes_read;
24679 mac_ptr += arg;
24680 }
757a13d0 24681 }
cf2c3c16 24682 }
757a13d0 24683
cf2c3c16
TT
24684 return mac_ptr;
24685}
757a13d0 24686
cf2c3c16 24687/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24688 including DW_MACRO_import. */
cf2c3c16
TT
24689
24690static void
804d2729 24691dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24692 bfd *abfd,
d521ce57 24693 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24694 struct macro_source_file *current_file,
43f3e411 24695 struct line_header *lh,
cf2c3c16 24696 struct dwarf2_section_info *section,
36586728 24697 int section_is_gnu, int section_is_dwz,
cf2c3c16 24698 unsigned int offset_size,
8fc3fc34 24699 htab_t include_hash)
cf2c3c16 24700{
804d2729
TT
24701 struct dwarf2_per_objfile *dwarf2_per_objfile
24702 = cu->per_cu->dwarf2_per_objfile;
4d663531 24703 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24704 enum dwarf_macro_record_type macinfo_type;
24705 int at_commandline;
d521ce57 24706 const gdb_byte *opcode_definitions[256];
757a13d0 24707
cf2c3c16
TT
24708 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24709 &offset_size, section_is_gnu);
24710 if (mac_ptr == NULL)
24711 {
24712 /* We already issued a complaint. */
24713 return;
24714 }
757a13d0
JK
24715
24716 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24717 GDB is still reading the definitions from command line. First
24718 DW_MACINFO_start_file will need to be ignored as it was already executed
24719 to create CURRENT_FILE for the main source holding also the command line
24720 definitions. On first met DW_MACINFO_start_file this flag is reset to
24721 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24722
24723 at_commandline = 1;
24724
24725 do
24726 {
24727 /* Do we at least have room for a macinfo type byte? */
24728 if (mac_ptr >= mac_end)
24729 {
f664829e 24730 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24731 break;
24732 }
24733
aead7601 24734 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24735 mac_ptr++;
24736
cf2c3c16
TT
24737 /* Note that we rely on the fact that the corresponding GNU and
24738 DWARF constants are the same. */
132448f8
SM
24739 DIAGNOSTIC_PUSH
24740 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24741 switch (macinfo_type)
24742 {
24743 /* A zero macinfo type indicates the end of the macro
24744 information. */
24745 case 0:
24746 break;
2e276125 24747
0af92d60
JK
24748 case DW_MACRO_define:
24749 case DW_MACRO_undef:
24750 case DW_MACRO_define_strp:
24751 case DW_MACRO_undef_strp:
24752 case DW_MACRO_define_sup:
24753 case DW_MACRO_undef_sup:
2e276125 24754 {
891d2f0b 24755 unsigned int bytes_read;
2e276125 24756 int line;
d521ce57 24757 const char *body;
cf2c3c16 24758 int is_define;
2e276125 24759
cf2c3c16
TT
24760 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24761 mac_ptr += bytes_read;
24762
0af92d60
JK
24763 if (macinfo_type == DW_MACRO_define
24764 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24765 {
24766 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24767 mac_ptr += bytes_read;
24768 }
24769 else
24770 {
24771 LONGEST str_offset;
24772
24773 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24774 mac_ptr += offset_size;
2e276125 24775
0af92d60
JK
24776 if (macinfo_type == DW_MACRO_define_sup
24777 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24778 || section_is_dwz)
36586728 24779 {
ed2dc618
SM
24780 struct dwz_file *dwz
24781 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24782
ed2dc618
SM
24783 body = read_indirect_string_from_dwz (objfile,
24784 dwz, str_offset);
36586728
TT
24785 }
24786 else
ed2dc618
SM
24787 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24788 abfd, str_offset);
cf2c3c16
TT
24789 }
24790
0af92d60
JK
24791 is_define = (macinfo_type == DW_MACRO_define
24792 || macinfo_type == DW_MACRO_define_strp
24793 || macinfo_type == DW_MACRO_define_sup);
2e276125 24794 if (! current_file)
757a13d0
JK
24795 {
24796 /* DWARF violation as no main source is present. */
b98664d3 24797 complaint (_("debug info with no main source gives macro %s "
757a13d0 24798 "on line %d: %s"),
cf2c3c16
TT
24799 is_define ? _("definition") : _("undefinition"),
24800 line, body);
757a13d0
JK
24801 break;
24802 }
3e43a32a
MS
24803 if ((line == 0 && !at_commandline)
24804 || (line != 0 && at_commandline))
b98664d3 24805 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24806 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24807 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24808 line == 0 ? _("zero") : _("non-zero"), line, body);
24809
955b06fa 24810 if (body == NULL)
7bede828 24811 {
955b06fa
SDJ
24812 /* Fedora's rpm-build's "debugedit" binary
24813 corrupted .debug_macro sections.
24814
24815 For more info, see
24816 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24817 complaint (_("debug info gives %s invalid macro %s "
24818 "without body (corrupted?) at line %d "
24819 "on file %s"),
24820 at_commandline ? _("command-line") : _("in-file"),
24821 is_define ? _("definition") : _("undefinition"),
24822 line, current_file->filename);
7bede828 24823 }
955b06fa
SDJ
24824 else if (is_define)
24825 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24826 else
24827 {
0af92d60
JK
24828 gdb_assert (macinfo_type == DW_MACRO_undef
24829 || macinfo_type == DW_MACRO_undef_strp
24830 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24831 macro_undef (current_file, line, body);
24832 }
2e276125
JB
24833 }
24834 break;
24835
0af92d60 24836 case DW_MACRO_start_file:
2e276125 24837 {
891d2f0b 24838 unsigned int bytes_read;
2e276125
JB
24839 int line, file;
24840
24841 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24842 mac_ptr += bytes_read;
24843 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24844 mac_ptr += bytes_read;
24845
3e43a32a
MS
24846 if ((line == 0 && !at_commandline)
24847 || (line != 0 && at_commandline))
b98664d3 24848 complaint (_("debug info gives source %d included "
757a13d0
JK
24849 "from %s at %s line %d"),
24850 file, at_commandline ? _("command-line") : _("file"),
24851 line == 0 ? _("zero") : _("non-zero"), line);
24852
24853 if (at_commandline)
24854 {
0af92d60 24855 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24856 pass one. */
757a13d0
JK
24857 at_commandline = 0;
24858 }
24859 else
804d2729
TT
24860 current_file = macro_start_file (cu, file, line, current_file,
24861 lh);
2e276125
JB
24862 }
24863 break;
24864
0af92d60 24865 case DW_MACRO_end_file:
2e276125 24866 if (! current_file)
b98664d3 24867 complaint (_("macro debug info has an unmatched "
3e43a32a 24868 "`close_file' directive"));
2e276125
JB
24869 else
24870 {
24871 current_file = current_file->included_by;
24872 if (! current_file)
24873 {
cf2c3c16 24874 enum dwarf_macro_record_type next_type;
2e276125
JB
24875
24876 /* GCC circa March 2002 doesn't produce the zero
24877 type byte marking the end of the compilation
24878 unit. Complain if it's not there, but exit no
24879 matter what. */
24880
24881 /* Do we at least have room for a macinfo type byte? */
24882 if (mac_ptr >= mac_end)
24883 {
f664829e 24884 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24885 return;
24886 }
24887
24888 /* We don't increment mac_ptr here, so this is just
24889 a look-ahead. */
aead7601
SM
24890 next_type
24891 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24892 mac_ptr);
2e276125 24893 if (next_type != 0)
b98664d3 24894 complaint (_("no terminating 0-type entry for "
3e43a32a 24895 "macros in `.debug_macinfo' section"));
2e276125
JB
24896
24897 return;
24898 }
24899 }
24900 break;
24901
0af92d60
JK
24902 case DW_MACRO_import:
24903 case DW_MACRO_import_sup:
cf2c3c16
TT
24904 {
24905 LONGEST offset;
8fc3fc34 24906 void **slot;
a036ba48
TT
24907 bfd *include_bfd = abfd;
24908 struct dwarf2_section_info *include_section = section;
d521ce57 24909 const gdb_byte *include_mac_end = mac_end;
a036ba48 24910 int is_dwz = section_is_dwz;
d521ce57 24911 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24912
24913 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24914 mac_ptr += offset_size;
24915
0af92d60 24916 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24917 {
ed2dc618 24918 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24919
4d663531 24920 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24921
a036ba48 24922 include_section = &dwz->macro;
a32a8923 24923 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24924 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24925 is_dwz = 1;
24926 }
24927
24928 new_mac_ptr = include_section->buffer + offset;
24929 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24930
8fc3fc34
TT
24931 if (*slot != NULL)
24932 {
24933 /* This has actually happened; see
24934 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24935 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24936 ".debug_macro section"));
24937 }
24938 else
24939 {
d521ce57 24940 *slot = (void *) new_mac_ptr;
36586728 24941
804d2729 24942 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24943 include_mac_end, current_file, lh,
36586728 24944 section, section_is_gnu, is_dwz,
4d663531 24945 offset_size, include_hash);
8fc3fc34 24946
d521ce57 24947 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24948 }
cf2c3c16
TT
24949 }
24950 break;
24951
2e276125 24952 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24953 if (!section_is_gnu)
24954 {
24955 unsigned int bytes_read;
2e276125 24956
ac298888
TT
24957 /* This reads the constant, but since we don't recognize
24958 any vendor extensions, we ignore it. */
24959 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24960 mac_ptr += bytes_read;
24961 read_direct_string (abfd, mac_ptr, &bytes_read);
24962 mac_ptr += bytes_read;
2e276125 24963
cf2c3c16
TT
24964 /* We don't recognize any vendor extensions. */
24965 break;
24966 }
24967 /* FALLTHROUGH */
24968
24969 default:
24970 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24971 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24972 section);
24973 if (mac_ptr == NULL)
24974 return;
24975 break;
2e276125 24976 }
132448f8 24977 DIAGNOSTIC_POP
757a13d0 24978 } while (macinfo_type != 0);
2e276125 24979}
8e19ed76 24980
cf2c3c16 24981static void
09262596 24982dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24983 int section_is_gnu)
cf2c3c16 24984{
518817b3
SM
24985 struct dwarf2_per_objfile *dwarf2_per_objfile
24986 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24987 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24988 struct line_header *lh = cu->line_header;
24989 bfd *abfd;
d521ce57 24990 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24991 struct macro_source_file *current_file = 0;
24992 enum dwarf_macro_record_type macinfo_type;
24993 unsigned int offset_size = cu->header.offset_size;
d521ce57 24994 const gdb_byte *opcode_definitions[256];
8fc3fc34 24995 void **slot;
09262596
DE
24996 struct dwarf2_section_info *section;
24997 const char *section_name;
24998
24999 if (cu->dwo_unit != NULL)
25000 {
25001 if (section_is_gnu)
25002 {
25003 section = &cu->dwo_unit->dwo_file->sections.macro;
25004 section_name = ".debug_macro.dwo";
25005 }
25006 else
25007 {
25008 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25009 section_name = ".debug_macinfo.dwo";
25010 }
25011 }
25012 else
25013 {
25014 if (section_is_gnu)
25015 {
25016 section = &dwarf2_per_objfile->macro;
25017 section_name = ".debug_macro";
25018 }
25019 else
25020 {
25021 section = &dwarf2_per_objfile->macinfo;
25022 section_name = ".debug_macinfo";
25023 }
25024 }
cf2c3c16 25025
bb5ed363 25026 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25027 if (section->buffer == NULL)
25028 {
b98664d3 25029 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
25030 return;
25031 }
a32a8923 25032 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25033
25034 /* First pass: Find the name of the base filename.
25035 This filename is needed in order to process all macros whose definition
25036 (or undefinition) comes from the command line. These macros are defined
25037 before the first DW_MACINFO_start_file entry, and yet still need to be
25038 associated to the base file.
25039
25040 To determine the base file name, we scan the macro definitions until we
25041 reach the first DW_MACINFO_start_file entry. We then initialize
25042 CURRENT_FILE accordingly so that any macro definition found before the
25043 first DW_MACINFO_start_file can still be associated to the base file. */
25044
25045 mac_ptr = section->buffer + offset;
25046 mac_end = section->buffer + section->size;
25047
25048 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25049 &offset_size, section_is_gnu);
25050 if (mac_ptr == NULL)
25051 {
25052 /* We already issued a complaint. */
25053 return;
25054 }
25055
25056 do
25057 {
25058 /* Do we at least have room for a macinfo type byte? */
25059 if (mac_ptr >= mac_end)
25060 {
25061 /* Complaint is printed during the second pass as GDB will probably
25062 stop the first pass earlier upon finding
25063 DW_MACINFO_start_file. */
25064 break;
25065 }
25066
aead7601 25067 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25068 mac_ptr++;
25069
25070 /* Note that we rely on the fact that the corresponding GNU and
25071 DWARF constants are the same. */
132448f8
SM
25072 DIAGNOSTIC_PUSH
25073 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25074 switch (macinfo_type)
25075 {
25076 /* A zero macinfo type indicates the end of the macro
25077 information. */
25078 case 0:
25079 break;
25080
0af92d60
JK
25081 case DW_MACRO_define:
25082 case DW_MACRO_undef:
cf2c3c16
TT
25083 /* Only skip the data by MAC_PTR. */
25084 {
25085 unsigned int bytes_read;
25086
25087 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25088 mac_ptr += bytes_read;
25089 read_direct_string (abfd, mac_ptr, &bytes_read);
25090 mac_ptr += bytes_read;
25091 }
25092 break;
25093
0af92d60 25094 case DW_MACRO_start_file:
cf2c3c16
TT
25095 {
25096 unsigned int bytes_read;
25097 int line, file;
25098
25099 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25100 mac_ptr += bytes_read;
25101 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25102 mac_ptr += bytes_read;
25103
804d2729 25104 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25105 }
25106 break;
25107
0af92d60 25108 case DW_MACRO_end_file:
cf2c3c16
TT
25109 /* No data to skip by MAC_PTR. */
25110 break;
25111
0af92d60
JK
25112 case DW_MACRO_define_strp:
25113 case DW_MACRO_undef_strp:
25114 case DW_MACRO_define_sup:
25115 case DW_MACRO_undef_sup:
cf2c3c16
TT
25116 {
25117 unsigned int bytes_read;
25118
25119 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25120 mac_ptr += bytes_read;
25121 mac_ptr += offset_size;
25122 }
25123 break;
25124
0af92d60
JK
25125 case DW_MACRO_import:
25126 case DW_MACRO_import_sup:
cf2c3c16 25127 /* Note that, according to the spec, a transparent include
0af92d60 25128 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25129 skip this opcode. */
25130 mac_ptr += offset_size;
25131 break;
25132
25133 case DW_MACINFO_vendor_ext:
25134 /* Only skip the data by MAC_PTR. */
25135 if (!section_is_gnu)
25136 {
25137 unsigned int bytes_read;
25138
25139 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25140 mac_ptr += bytes_read;
25141 read_direct_string (abfd, mac_ptr, &bytes_read);
25142 mac_ptr += bytes_read;
25143 }
25144 /* FALLTHROUGH */
25145
25146 default:
25147 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25148 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25149 section);
25150 if (mac_ptr == NULL)
25151 return;
25152 break;
25153 }
132448f8 25154 DIAGNOSTIC_POP
cf2c3c16
TT
25155 } while (macinfo_type != 0 && current_file == NULL);
25156
25157 /* Second pass: Process all entries.
25158
25159 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25160 command-line macro definitions/undefinitions. This flag is unset when we
25161 reach the first DW_MACINFO_start_file entry. */
25162
fc4007c9
TT
25163 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25164 htab_eq_pointer,
25165 NULL, xcalloc, xfree));
8fc3fc34 25166 mac_ptr = section->buffer + offset;
fc4007c9 25167 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25168 *slot = (void *) mac_ptr;
804d2729 25169 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25170 current_file, lh, section,
fc4007c9
TT
25171 section_is_gnu, 0, offset_size,
25172 include_hash.get ());
cf2c3c16
TT
25173}
25174
8e19ed76 25175/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25176 if so return true else false. */
380bca97 25177
8e19ed76 25178static int
6e5a29e1 25179attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25180{
25181 return (attr == NULL ? 0 :
25182 attr->form == DW_FORM_block1
25183 || attr->form == DW_FORM_block2
25184 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25185 || attr->form == DW_FORM_block
25186 || attr->form == DW_FORM_exprloc);
8e19ed76 25187}
4c2df51b 25188
c6a0999f
JB
25189/* Return non-zero if ATTR's value is a section offset --- classes
25190 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25191 You may use DW_UNSND (attr) to retrieve such offsets.
25192
25193 Section 7.5.4, "Attribute Encodings", explains that no attribute
25194 may have a value that belongs to more than one of these classes; it
25195 would be ambiguous if we did, because we use the same forms for all
25196 of them. */
380bca97 25197
3690dd37 25198static int
6e5a29e1 25199attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25200{
25201 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25202 || attr->form == DW_FORM_data8
25203 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25204}
25205
3690dd37
JB
25206/* Return non-zero if ATTR's value falls in the 'constant' class, or
25207 zero otherwise. When this function returns true, you can apply
25208 dwarf2_get_attr_constant_value to it.
25209
25210 However, note that for some attributes you must check
25211 attr_form_is_section_offset before using this test. DW_FORM_data4
25212 and DW_FORM_data8 are members of both the constant class, and of
25213 the classes that contain offsets into other debug sections
25214 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25215 that, if an attribute's can be either a constant or one of the
25216 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25217 taken as section offsets, not constants.
25218
25219 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25220 cannot handle that. */
380bca97 25221
3690dd37 25222static int
6e5a29e1 25223attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25224{
25225 switch (attr->form)
25226 {
25227 case DW_FORM_sdata:
25228 case DW_FORM_udata:
25229 case DW_FORM_data1:
25230 case DW_FORM_data2:
25231 case DW_FORM_data4:
25232 case DW_FORM_data8:
663c44ac 25233 case DW_FORM_implicit_const:
3690dd37
JB
25234 return 1;
25235 default:
25236 return 0;
25237 }
25238}
25239
7771576e
SA
25240
25241/* DW_ADDR is always stored already as sect_offset; despite for the forms
25242 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25243
25244static int
6e5a29e1 25245attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25246{
25247 switch (attr->form)
25248 {
25249 case DW_FORM_ref_addr:
25250 case DW_FORM_ref1:
25251 case DW_FORM_ref2:
25252 case DW_FORM_ref4:
25253 case DW_FORM_ref8:
25254 case DW_FORM_ref_udata:
25255 case DW_FORM_GNU_ref_alt:
25256 return 1;
25257 default:
25258 return 0;
25259 }
25260}
25261
3019eac3
DE
25262/* Return the .debug_loc section to use for CU.
25263 For DWO files use .debug_loc.dwo. */
25264
25265static struct dwarf2_section_info *
25266cu_debug_loc_section (struct dwarf2_cu *cu)
25267{
518817b3
SM
25268 struct dwarf2_per_objfile *dwarf2_per_objfile
25269 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25270
3019eac3 25271 if (cu->dwo_unit)
43988095
JK
25272 {
25273 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25274
43988095
JK
25275 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25276 }
25277 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25278 : &dwarf2_per_objfile->loc);
3019eac3
DE
25279}
25280
8cf6f0b1
TT
25281/* A helper function that fills in a dwarf2_loclist_baton. */
25282
25283static void
25284fill_in_loclist_baton (struct dwarf2_cu *cu,
25285 struct dwarf2_loclist_baton *baton,
ff39bb5e 25286 const struct attribute *attr)
8cf6f0b1 25287{
518817b3
SM
25288 struct dwarf2_per_objfile *dwarf2_per_objfile
25289 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25290 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25291
25292 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25293
25294 baton->per_cu = cu->per_cu;
25295 gdb_assert (baton->per_cu);
25296 /* We don't know how long the location list is, but make sure we
25297 don't run off the edge of the section. */
3019eac3
DE
25298 baton->size = section->size - DW_UNSND (attr);
25299 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25300 baton->base_address = cu->base_address;
f664829e 25301 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25302}
25303
4c2df51b 25304static void
ff39bb5e 25305dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25306 struct dwarf2_cu *cu, int is_block)
4c2df51b 25307{
518817b3
SM
25308 struct dwarf2_per_objfile *dwarf2_per_objfile
25309 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25310 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25311 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25312
3690dd37 25313 if (attr_form_is_section_offset (attr)
3019eac3 25314 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25315 the section. If so, fall through to the complaint in the
25316 other branch. */
3019eac3 25317 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25318 {
0d53c4c4 25319 struct dwarf2_loclist_baton *baton;
4c2df51b 25320
8d749320 25321 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25322
8cf6f0b1 25323 fill_in_loclist_baton (cu, baton, attr);
be391dca 25324
d00adf39 25325 if (cu->base_known == 0)
b98664d3 25326 complaint (_("Location list used without "
3e43a32a 25327 "specifying the CU base address."));
4c2df51b 25328
f1e6e072
TT
25329 SYMBOL_ACLASS_INDEX (sym) = (is_block
25330 ? dwarf2_loclist_block_index
25331 : dwarf2_loclist_index);
0d53c4c4
DJ
25332 SYMBOL_LOCATION_BATON (sym) = baton;
25333 }
25334 else
25335 {
25336 struct dwarf2_locexpr_baton *baton;
25337
8d749320 25338 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25339 baton->per_cu = cu->per_cu;
25340 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25341
25342 if (attr_form_is_block (attr))
25343 {
25344 /* Note that we're just copying the block's data pointer
25345 here, not the actual data. We're still pointing into the
6502dd73
DJ
25346 info_buffer for SYM's objfile; right now we never release
25347 that buffer, but when we do clean up properly this may
25348 need to change. */
0d53c4c4
DJ
25349 baton->size = DW_BLOCK (attr)->size;
25350 baton->data = DW_BLOCK (attr)->data;
25351 }
25352 else
25353 {
25354 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 25355 sym->natural_name ());
0d53c4c4 25356 baton->size = 0;
0d53c4c4 25357 }
6e70227d 25358
f1e6e072
TT
25359 SYMBOL_ACLASS_INDEX (sym) = (is_block
25360 ? dwarf2_locexpr_block_index
25361 : dwarf2_locexpr_index);
0d53c4c4
DJ
25362 SYMBOL_LOCATION_BATON (sym) = baton;
25363 }
4c2df51b 25364}
6502dd73 25365
9aa1f1e3
TT
25366/* Return the OBJFILE associated with the compilation unit CU. If CU
25367 came from a separate debuginfo file, then the master objfile is
25368 returned. */
ae0d2f24
UW
25369
25370struct objfile *
25371dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25372{
e3b94546 25373 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25374
25375 /* Return the master objfile, so that we can report and look up the
25376 correct file containing this variable. */
25377 if (objfile->separate_debug_objfile_backlink)
25378 objfile = objfile->separate_debug_objfile_backlink;
25379
25380 return objfile;
25381}
25382
96408a79
SA
25383/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25384 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25385 CU_HEADERP first. */
25386
25387static const struct comp_unit_head *
25388per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25389 struct dwarf2_per_cu_data *per_cu)
25390{
d521ce57 25391 const gdb_byte *info_ptr;
96408a79
SA
25392
25393 if (per_cu->cu)
25394 return &per_cu->cu->header;
25395
9c541725 25396 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25397
25398 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25399 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25400 rcuh_kind::COMPILE);
96408a79
SA
25401
25402 return cu_headerp;
25403}
25404
ae0d2f24
UW
25405/* Return the address size given in the compilation unit header for CU. */
25406
98714339 25407int
ae0d2f24
UW
25408dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25409{
96408a79
SA
25410 struct comp_unit_head cu_header_local;
25411 const struct comp_unit_head *cu_headerp;
c471e790 25412
96408a79
SA
25413 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25414
25415 return cu_headerp->addr_size;
ae0d2f24
UW
25416}
25417
9eae7c52
TT
25418/* Return the offset size given in the compilation unit header for CU. */
25419
25420int
25421dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25422{
96408a79
SA
25423 struct comp_unit_head cu_header_local;
25424 const struct comp_unit_head *cu_headerp;
9c6c53f7 25425
96408a79
SA
25426 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25427
25428 return cu_headerp->offset_size;
25429}
25430
25431/* See its dwarf2loc.h declaration. */
25432
25433int
25434dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25435{
25436 struct comp_unit_head cu_header_local;
25437 const struct comp_unit_head *cu_headerp;
25438
25439 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25440
25441 if (cu_headerp->version == 2)
25442 return cu_headerp->addr_size;
25443 else
25444 return cu_headerp->offset_size;
181cebd4
JK
25445}
25446
9aa1f1e3
TT
25447/* Return the text offset of the CU. The returned offset comes from
25448 this CU's objfile. If this objfile came from a separate debuginfo
25449 file, then the offset may be different from the corresponding
25450 offset in the parent objfile. */
25451
25452CORE_ADDR
25453dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25454{
e3b94546 25455 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25456
25457 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25458}
25459
9a49df9d
AB
25460/* Return a type that is a generic pointer type, the size of which matches
25461 the address size given in the compilation unit header for PER_CU. */
25462static struct type *
25463dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25464{
25465 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25466 struct type *void_type = objfile_type (objfile)->builtin_void;
25467 struct type *addr_type = lookup_pointer_type (void_type);
25468 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25469
25470 if (TYPE_LENGTH (addr_type) == addr_size)
25471 return addr_type;
25472
25473 addr_type
25474 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25475 return addr_type;
25476}
25477
43988095
JK
25478/* Return DWARF version number of PER_CU. */
25479
25480short
25481dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25482{
25483 return per_cu->dwarf_version;
25484}
25485
348e048f
DE
25486/* Locate the .debug_info compilation unit from CU's objfile which contains
25487 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25488
25489static struct dwarf2_per_cu_data *
9c541725 25490dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25491 unsigned int offset_in_dwz,
ed2dc618 25492 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25493{
25494 struct dwarf2_per_cu_data *this_cu;
25495 int low, high;
25496
ae038cb0 25497 low = 0;
b76e467d 25498 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25499 while (high > low)
25500 {
36586728 25501 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25502 int mid = low + (high - low) / 2;
9a619af0 25503
36586728 25504 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25505 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25506 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25507 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25508 high = mid;
25509 else
25510 low = mid + 1;
25511 }
25512 gdb_assert (low == high);
36586728 25513 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25514 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25515 {
36586728 25516 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25517 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25518 "offset %s [in module %s]"),
25519 sect_offset_str (sect_off),
ed2dc618 25520 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25521
9c541725
PA
25522 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25523 <= sect_off);
ae038cb0
DJ
25524 return dwarf2_per_objfile->all_comp_units[low-1];
25525 }
25526 else
25527 {
b76e467d 25528 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25529 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25530 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25531 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25532 return this_cu;
25533 }
25534}
25535
23745b47 25536/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25537
fcd3b13d
SM
25538dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25539 : per_cu (per_cu_),
9068261f
AB
25540 mark (false),
25541 has_loclist (false),
25542 checked_producer (false),
25543 producer_is_gxx_lt_4_6 (false),
25544 producer_is_gcc_lt_4_3 (false),
eb77c9df 25545 producer_is_icc (false),
9068261f 25546 producer_is_icc_lt_14 (false),
c258c396 25547 producer_is_codewarrior (false),
9068261f 25548 processing_has_namespace_info (false)
93311388 25549{
fcd3b13d
SM
25550 per_cu->cu = this;
25551}
25552
25553/* Destroy a dwarf2_cu. */
25554
25555dwarf2_cu::~dwarf2_cu ()
25556{
25557 per_cu->cu = NULL;
9816fde3
JK
25558}
25559
25560/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25561
25562static void
95554aad
TT
25563prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25564 enum language pretend_language)
9816fde3
JK
25565{
25566 struct attribute *attr;
25567
25568 /* Set the language we're debugging. */
25569 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25570 if (attr != nullptr)
9816fde3
JK
25571 set_cu_language (DW_UNSND (attr), cu);
25572 else
9cded63f 25573 {
95554aad 25574 cu->language = pretend_language;
9cded63f
TT
25575 cu->language_defn = language_def (cu->language);
25576 }
dee91e82 25577
7d45c7c3 25578 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25579}
25580
ae038cb0
DJ
25581/* Increase the age counter on each cached compilation unit, and free
25582 any that are too old. */
25583
25584static void
ed2dc618 25585age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25586{
25587 struct dwarf2_per_cu_data *per_cu, **last_chain;
25588
25589 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25590 per_cu = dwarf2_per_objfile->read_in_chain;
25591 while (per_cu != NULL)
25592 {
25593 per_cu->cu->last_used ++;
b4f54984 25594 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25595 dwarf2_mark (per_cu->cu);
25596 per_cu = per_cu->cu->read_in_chain;
25597 }
25598
25599 per_cu = dwarf2_per_objfile->read_in_chain;
25600 last_chain = &dwarf2_per_objfile->read_in_chain;
25601 while (per_cu != NULL)
25602 {
25603 struct dwarf2_per_cu_data *next_cu;
25604
25605 next_cu = per_cu->cu->read_in_chain;
25606
25607 if (!per_cu->cu->mark)
25608 {
fcd3b13d 25609 delete per_cu->cu;
ae038cb0
DJ
25610 *last_chain = next_cu;
25611 }
25612 else
25613 last_chain = &per_cu->cu->read_in_chain;
25614
25615 per_cu = next_cu;
25616 }
25617}
25618
25619/* Remove a single compilation unit from the cache. */
25620
25621static void
dee91e82 25622free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25623{
25624 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25625 struct dwarf2_per_objfile *dwarf2_per_objfile
25626 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25627
25628 per_cu = dwarf2_per_objfile->read_in_chain;
25629 last_chain = &dwarf2_per_objfile->read_in_chain;
25630 while (per_cu != NULL)
25631 {
25632 struct dwarf2_per_cu_data *next_cu;
25633
25634 next_cu = per_cu->cu->read_in_chain;
25635
dee91e82 25636 if (per_cu == target_per_cu)
ae038cb0 25637 {
fcd3b13d 25638 delete per_cu->cu;
dee91e82 25639 per_cu->cu = NULL;
ae038cb0
DJ
25640 *last_chain = next_cu;
25641 break;
25642 }
25643 else
25644 last_chain = &per_cu->cu->read_in_chain;
25645
25646 per_cu = next_cu;
25647 }
25648}
25649
dee91e82
DE
25650/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25651 We store these in a hash table separate from the DIEs, and preserve them
25652 when the DIEs are flushed out of cache.
25653
25654 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25655 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25656 or the type may come from a DWO file. Furthermore, while it's more logical
25657 to use per_cu->section+offset, with Fission the section with the data is in
25658 the DWO file but we don't know that section at the point we need it.
25659 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25660 because we can enter the lookup routine, get_die_type_at_offset, from
25661 outside this file, and thus won't necessarily have PER_CU->cu.
25662 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25663
dee91e82 25664struct dwarf2_per_cu_offset_and_type
1c379e20 25665{
dee91e82 25666 const struct dwarf2_per_cu_data *per_cu;
9c541725 25667 sect_offset sect_off;
1c379e20
DJ
25668 struct type *type;
25669};
25670
dee91e82 25671/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25672
25673static hashval_t
dee91e82 25674per_cu_offset_and_type_hash (const void *item)
1c379e20 25675{
9a3c8263
SM
25676 const struct dwarf2_per_cu_offset_and_type *ofs
25677 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25678
9c541725 25679 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25680}
25681
dee91e82 25682/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25683
25684static int
dee91e82 25685per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25686{
9a3c8263
SM
25687 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25688 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25689 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25690 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25691
dee91e82 25692 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25693 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25694}
25695
25696/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25697 table if necessary. For convenience, return TYPE.
25698
25699 The DIEs reading must have careful ordering to:
85102364 25700 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25701 reading current DIE.
25702 * Not trying to dereference contents of still incompletely read in types
25703 while reading in other DIEs.
25704 * Enable referencing still incompletely read in types just by a pointer to
25705 the type without accessing its fields.
25706
25707 Therefore caller should follow these rules:
25708 * Try to fetch any prerequisite types we may need to build this DIE type
25709 before building the type and calling set_die_type.
e71ec853 25710 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25711 possible before fetching more types to complete the current type.
25712 * Make the type as complete as possible before fetching more types. */
1c379e20 25713
f792889a 25714static struct type *
1c379e20
DJ
25715set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25716{
518817b3
SM
25717 struct dwarf2_per_objfile *dwarf2_per_objfile
25718 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25719 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25720 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25721 struct attribute *attr;
25722 struct dynamic_prop prop;
1c379e20 25723
b4ba55a1
JB
25724 /* For Ada types, make sure that the gnat-specific data is always
25725 initialized (if not already set). There are a few types where
25726 we should not be doing so, because the type-specific area is
25727 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25728 where the type-specific area is used to store the floatformat).
25729 But this is not a problem, because the gnat-specific information
25730 is actually not needed for these types. */
25731 if (need_gnat_info (cu)
25732 && TYPE_CODE (type) != TYPE_CODE_FUNC
25733 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25734 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25735 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25736 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25737 && !HAVE_GNAT_AUX_INFO (type))
25738 INIT_GNAT_SPECIFIC (type);
25739
3f2f83dd
KB
25740 /* Read DW_AT_allocated and set in type. */
25741 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25742 if (attr_form_is_block (attr))
25743 {
9a49df9d
AB
25744 struct type *prop_type
25745 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25746 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25747 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25748 }
25749 else if (attr != NULL)
25750 {
b98664d3 25751 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25752 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25753 sect_offset_str (die->sect_off));
3f2f83dd
KB
25754 }
25755
25756 /* Read DW_AT_associated and set in type. */
25757 attr = dwarf2_attr (die, DW_AT_associated, cu);
25758 if (attr_form_is_block (attr))
25759 {
9a49df9d
AB
25760 struct type *prop_type
25761 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25762 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25763 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25764 }
25765 else if (attr != NULL)
25766 {
b98664d3 25767 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25768 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25769 sect_offset_str (die->sect_off));
3f2f83dd
KB
25770 }
25771
3cdcd0ce
JB
25772 /* Read DW_AT_data_location and set in type. */
25773 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25774 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25775 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25776 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25777
dee91e82 25778 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25779 {
dee91e82
DE
25780 dwarf2_per_objfile->die_type_hash =
25781 htab_create_alloc_ex (127,
25782 per_cu_offset_and_type_hash,
25783 per_cu_offset_and_type_eq,
25784 NULL,
25785 &objfile->objfile_obstack,
25786 hashtab_obstack_allocate,
25787 dummy_obstack_deallocate);
f792889a 25788 }
1c379e20 25789
dee91e82 25790 ofs.per_cu = cu->per_cu;
9c541725 25791 ofs.sect_off = die->sect_off;
1c379e20 25792 ofs.type = type;
dee91e82
DE
25793 slot = (struct dwarf2_per_cu_offset_and_type **)
25794 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25795 if (*slot)
b98664d3 25796 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25797 sect_offset_str (die->sect_off));
8d749320
SM
25798 *slot = XOBNEW (&objfile->objfile_obstack,
25799 struct dwarf2_per_cu_offset_and_type);
1c379e20 25800 **slot = ofs;
f792889a 25801 return type;
1c379e20
DJ
25802}
25803
9c541725 25804/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25805 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25806
25807static struct type *
9c541725 25808get_die_type_at_offset (sect_offset sect_off,
673bfd45 25809 struct dwarf2_per_cu_data *per_cu)
1c379e20 25810{
dee91e82 25811 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25812 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25813
dee91e82 25814 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25815 return NULL;
1c379e20 25816
dee91e82 25817 ofs.per_cu = per_cu;
9c541725 25818 ofs.sect_off = sect_off;
9a3c8263
SM
25819 slot = ((struct dwarf2_per_cu_offset_and_type *)
25820 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25821 if (slot)
25822 return slot->type;
25823 else
25824 return NULL;
25825}
25826
02142a6c 25827/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25828 or return NULL if DIE does not have a saved type. */
25829
25830static struct type *
25831get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25832{
9c541725 25833 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25834}
25835
10b3939b
DJ
25836/* Add a dependence relationship from CU to REF_PER_CU. */
25837
25838static void
25839dwarf2_add_dependence (struct dwarf2_cu *cu,
25840 struct dwarf2_per_cu_data *ref_per_cu)
25841{
25842 void **slot;
25843
25844 if (cu->dependencies == NULL)
25845 cu->dependencies
25846 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25847 NULL, &cu->comp_unit_obstack,
25848 hashtab_obstack_allocate,
25849 dummy_obstack_deallocate);
25850
25851 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25852 if (*slot == NULL)
25853 *slot = ref_per_cu;
25854}
1c379e20 25855
f504f079
DE
25856/* Subroutine of dwarf2_mark to pass to htab_traverse.
25857 Set the mark field in every compilation unit in the
ae038cb0
DJ
25858 cache that we must keep because we are keeping CU. */
25859
10b3939b
DJ
25860static int
25861dwarf2_mark_helper (void **slot, void *data)
25862{
25863 struct dwarf2_per_cu_data *per_cu;
25864
25865 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25866
25867 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25868 reading of the chain. As such dependencies remain valid it is not much
25869 useful to track and undo them during QUIT cleanups. */
25870 if (per_cu->cu == NULL)
25871 return 1;
25872
10b3939b
DJ
25873 if (per_cu->cu->mark)
25874 return 1;
9068261f 25875 per_cu->cu->mark = true;
10b3939b
DJ
25876
25877 if (per_cu->cu->dependencies != NULL)
25878 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25879
25880 return 1;
25881}
25882
f504f079
DE
25883/* Set the mark field in CU and in every other compilation unit in the
25884 cache that we must keep because we are keeping CU. */
25885
ae038cb0
DJ
25886static void
25887dwarf2_mark (struct dwarf2_cu *cu)
25888{
25889 if (cu->mark)
25890 return;
9068261f 25891 cu->mark = true;
10b3939b
DJ
25892 if (cu->dependencies != NULL)
25893 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25894}
25895
25896static void
25897dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25898{
25899 while (per_cu)
25900 {
9068261f 25901 per_cu->cu->mark = false;
ae038cb0
DJ
25902 per_cu = per_cu->cu->read_in_chain;
25903 }
72bf9492
DJ
25904}
25905
72bf9492
DJ
25906/* Trivial hash function for partial_die_info: the hash value of a DIE
25907 is its offset in .debug_info for this objfile. */
25908
25909static hashval_t
25910partial_die_hash (const void *item)
25911{
9a3c8263
SM
25912 const struct partial_die_info *part_die
25913 = (const struct partial_die_info *) item;
9a619af0 25914
9c541725 25915 return to_underlying (part_die->sect_off);
72bf9492
DJ
25916}
25917
25918/* Trivial comparison function for partial_die_info structures: two DIEs
25919 are equal if they have the same offset. */
25920
25921static int
25922partial_die_eq (const void *item_lhs, const void *item_rhs)
25923{
9a3c8263
SM
25924 const struct partial_die_info *part_die_lhs
25925 = (const struct partial_die_info *) item_lhs;
25926 const struct partial_die_info *part_die_rhs
25927 = (const struct partial_die_info *) item_rhs;
9a619af0 25928
9c541725 25929 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25930}
25931
3c3bb058
AB
25932struct cmd_list_element *set_dwarf_cmdlist;
25933struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25934
25935static void
981a3fb3 25936set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25937{
b4f54984 25938 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25939 gdb_stdout);
ae038cb0
DJ
25940}
25941
25942static void
981a3fb3 25943show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25944{
b4f54984 25945 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25946}
25947
491144b5 25948bool dwarf_always_disassemble;
437afbb8 25949
437afbb8 25950static void
cd4fb1b2
SM
25951show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25952 struct cmd_list_element *c, const char *value)
9291a0cd 25953{
cd4fb1b2
SM
25954 fprintf_filtered (file,
25955 _("Whether to always disassemble "
25956 "DWARF expressions is %s.\n"),
25957 value);
9291a0cd
TT
25958}
25959
9291a0cd 25960static void
cd4fb1b2
SM
25961show_check_physname (struct ui_file *file, int from_tty,
25962 struct cmd_list_element *c, const char *value)
9291a0cd 25963{
cd4fb1b2
SM
25964 fprintf_filtered (file,
25965 _("Whether to check \"physname\" is %s.\n"),
25966 value);
9291a0cd
TT
25967}
25968
cd4fb1b2
SM
25969void
25970_initialize_dwarf2_read (void)
9291a0cd 25971{
cd4fb1b2
SM
25972 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25973Set DWARF specific variables.\n\
590042fc 25974Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
25975 &set_dwarf_cmdlist, "maintenance set dwarf ",
25976 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25977
cd4fb1b2 25978 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
25979Show DWARF specific variables.\n\
25980Show DWARF variables such as the cache size."),
cd4fb1b2
SM
25981 &show_dwarf_cmdlist, "maintenance show dwarf ",
25982 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25983
cd4fb1b2
SM
25984 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25985 &dwarf_max_cache_age, _("\
25986Set the upper bound on the age of cached DWARF compilation units."), _("\
25987Show the upper bound on the age of cached DWARF compilation units."), _("\
25988A higher limit means that cached compilation units will be stored\n\
25989in memory longer, and more total memory will be used. Zero disables\n\
25990caching, which can slow down startup."),
25991 NULL,
25992 show_dwarf_max_cache_age,
25993 &set_dwarf_cmdlist,
25994 &show_dwarf_cmdlist);
156942c7 25995
cd4fb1b2
SM
25996 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25997 &dwarf_always_disassemble, _("\
25998Set whether `info address' always disassembles DWARF expressions."), _("\
25999Show whether `info address' always disassembles DWARF expressions."), _("\
26000When enabled, DWARF expressions are always printed in an assembly-like\n\
26001syntax. When disabled, expressions will be printed in a more\n\
26002conversational style, when possible."),
26003 NULL,
26004 show_dwarf_always_disassemble,
26005 &set_dwarf_cmdlist,
26006 &show_dwarf_cmdlist);
9291a0cd 26007
cd4fb1b2
SM
26008 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26009Set debugging of the DWARF reader."), _("\
26010Show debugging of the DWARF reader."), _("\
26011When enabled (non-zero), debugging messages are printed during DWARF\n\
26012reading and symtab expansion. A value of 1 (one) provides basic\n\
26013information. A value greater than 1 provides more verbose information."),
26014 NULL,
26015 NULL,
26016 &setdebuglist, &showdebuglist);
9291a0cd 26017
cd4fb1b2
SM
26018 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26019Set debugging of the DWARF DIE reader."), _("\
26020Show debugging of the DWARF DIE reader."), _("\
26021When enabled (non-zero), DIEs are dumped after they are read in.\n\
26022The value is the maximum depth to print."),
26023 NULL,
26024 NULL,
26025 &setdebuglist, &showdebuglist);
9291a0cd 26026
cd4fb1b2
SM
26027 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26028Set debugging of the dwarf line reader."), _("\
26029Show debugging of the dwarf line reader."), _("\
26030When enabled (non-zero), line number entries are dumped as they are read in.\n\
26031A value of 1 (one) provides basic information.\n\
26032A value greater than 1 provides more verbose information."),
26033 NULL,
26034 NULL,
26035 &setdebuglist, &showdebuglist);
437afbb8 26036
cd4fb1b2
SM
26037 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26038Set cross-checking of \"physname\" code against demangler."), _("\
26039Show cross-checking of \"physname\" code against demangler."), _("\
26040When enabled, GDB's internal \"physname\" code is checked against\n\
26041the demangler."),
26042 NULL, show_check_physname,
26043 &setdebuglist, &showdebuglist);
900e11f9 26044
e615022a
DE
26045 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26046 no_class, &use_deprecated_index_sections, _("\
26047Set whether to use deprecated gdb_index sections."), _("\
26048Show whether to use deprecated gdb_index sections."), _("\
26049When enabled, deprecated .gdb_index sections are used anyway.\n\
26050Normally they are ignored either because of a missing feature or\n\
26051performance issue.\n\
26052Warning: This option must be enabled before gdb reads the file."),
26053 NULL,
26054 NULL,
26055 &setlist, &showlist);
26056
f1e6e072
TT
26057 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26058 &dwarf2_locexpr_funcs);
26059 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26060 &dwarf2_loclist_funcs);
26061
26062 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26063 &dwarf2_block_frame_base_locexpr_funcs);
26064 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26065 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26066
26067#if GDB_SELF_TEST
26068 selftests::register_test ("dw2_expand_symtabs_matching",
26069 selftests::dw2_expand_symtabs_matching::run_test);
26070#endif
6502dd73 26071}
This page took 4.697257 seconds and 4 git commands to generate.