[gdb/testsuite] Fix regexp for reg value in jit-reader.exp
[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
ecfb656c
PA
912/* A 1-based directory index. This is a strong typedef to prevent
913 accidentally using a directory index as a 0-based index into an
914 array/vector. */
915enum class dir_index : unsigned int {};
916
917/* Likewise, a 1-based file name index. */
918enum class file_name_index : unsigned int {};
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
ecfb656c 970 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 971 is out of bounds. */
ecfb656c 972 const char *include_dir_at (dir_index index) const
8c43009f 973 {
ecfb656c
PA
974 /* Convert directory index number (1-based) to vector index
975 (0-based). */
976 size_t vec_index = to_underlying (index) - 1;
977
978 if (vec_index >= include_dirs.size ())
8c43009f 979 return NULL;
ecfb656c 980 return include_dirs[vec_index];
8c43009f
PA
981 }
982
ecfb656c 983 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 984 is out of bounds. */
ecfb656c 985 file_entry *file_name_at (file_name_index index)
8c43009f 986 {
ecfb656c
PA
987 /* Convert file name index number (1-based) to vector index
988 (0-based). */
989 size_t vec_index = to_underlying (index) - 1;
990
991 if (vec_index >= file_names.size ())
fff8551c 992 return NULL;
ecfb656c 993 return &file_names[vec_index];
fff8551c
PA
994 }
995
527f3840 996 /* Offset of line number information in .debug_line section. */
9c541725 997 sect_offset sect_off {};
527f3840
JK
998
999 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1000 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1001
1002 unsigned int total_length {};
1003 unsigned short version {};
1004 unsigned int header_length {};
1005 unsigned char minimum_instruction_length {};
1006 unsigned char maximum_ops_per_instruction {};
1007 unsigned char default_is_stmt {};
1008 int line_base {};
1009 unsigned char line_range {};
1010 unsigned char opcode_base {};
debd256d
JB
1011
1012 /* standard_opcode_lengths[i] is the number of operands for the
1013 standard opcode whose value is i. This means that
1014 standard_opcode_lengths[0] is unused, and the last meaningful
1015 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1016 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1017
fff8551c
PA
1018 /* The include_directories table. Note these are observing
1019 pointers. The memory is owned by debug_line_buffer. */
1020 std::vector<const char *> include_dirs;
debd256d 1021
fff8551c
PA
1022 /* The file_names table. */
1023 std::vector<file_entry> file_names;
debd256d
JB
1024
1025 /* The start and end of the statement program following this
6502dd73 1026 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1027 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1028};
c906108c 1029
fff8551c
PA
1030typedef std::unique_ptr<line_header> line_header_up;
1031
8c43009f
PA
1032const char *
1033file_entry::include_dir (const line_header *lh) const
1034{
ecfb656c 1035 return lh->include_dir_at (d_index);
8c43009f
PA
1036}
1037
c906108c 1038/* When we construct a partial symbol table entry we only
0963b4bd 1039 need this much information. */
6f06d47b 1040struct partial_die_info : public allocate_on_obstack
c906108c 1041 {
6f06d47b
YQ
1042 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1043
1044 /* Disable assign but still keep copy ctor, which is needed
1045 load_partial_dies. */
1046 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1047
52356b79
YQ
1048 /* Adjust the partial die before generating a symbol for it. This
1049 function may set the is_external flag or change the DIE's
1050 name. */
1051 void fixup (struct dwarf2_cu *cu);
1052
48fbe735
YQ
1053 /* Read a minimal amount of information into the minimal die
1054 structure. */
1055 const gdb_byte *read (const struct die_reader_specs *reader,
1056 const struct abbrev_info &abbrev,
1057 const gdb_byte *info_ptr);
1058
72bf9492 1059 /* Offset of this DIE. */
6f06d47b 1060 const sect_offset sect_off;
72bf9492
DJ
1061
1062 /* DWARF-2 tag for this DIE. */
6f06d47b 1063 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1064
72bf9492 1065 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1066 const unsigned int has_children : 1;
1067
72bf9492
DJ
1068 unsigned int is_external : 1;
1069 unsigned int is_declaration : 1;
1070 unsigned int has_type : 1;
1071 unsigned int has_specification : 1;
1072 unsigned int has_pc_info : 1;
481860b3 1073 unsigned int may_be_inlined : 1;
72bf9492 1074
0c1b455e
TT
1075 /* This DIE has been marked DW_AT_main_subprogram. */
1076 unsigned int main_subprogram : 1;
1077
72bf9492
DJ
1078 /* Flag set if the SCOPE field of this structure has been
1079 computed. */
1080 unsigned int scope_set : 1;
1081
fa4028e9
JB
1082 /* Flag set if the DIE has a byte_size attribute. */
1083 unsigned int has_byte_size : 1;
1084
ff908ebf
AW
1085 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1086 unsigned int has_const_value : 1;
1087
98bfdba5
PA
1088 /* Flag set if any of the DIE's children are template arguments. */
1089 unsigned int has_template_arguments : 1;
1090
52356b79 1091 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1092 unsigned int fixup_called : 1;
1093
36586728
TT
1094 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1095 unsigned int is_dwz : 1;
1096
1097 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1098 unsigned int spec_is_dwz : 1;
1099
72bf9492 1100 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1101 sometimes a default name for unnamed DIEs. */
6f06d47b 1102 const char *name = nullptr;
72bf9492 1103
abc72ce4 1104 /* The linkage name, if present. */
6f06d47b 1105 const char *linkage_name = nullptr;
abc72ce4 1106
72bf9492
DJ
1107 /* The scope to prepend to our children. This is generally
1108 allocated on the comp_unit_obstack, so will disappear
1109 when this compilation unit leaves the cache. */
6f06d47b 1110 const char *scope = nullptr;
72bf9492 1111
95554aad
TT
1112 /* Some data associated with the partial DIE. The tag determines
1113 which field is live. */
1114 union
1115 {
1116 /* The location description associated with this DIE, if any. */
1117 struct dwarf_block *locdesc;
1118 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1119 sect_offset sect_off;
6f06d47b 1120 } d {};
72bf9492
DJ
1121
1122 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1123 CORE_ADDR lowpc = 0;
1124 CORE_ADDR highpc = 0;
72bf9492 1125
93311388 1126 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1127 DW_AT_sibling, if any. */
48fbe735
YQ
1128 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1129 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1130 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1131
1132 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1133 DW_AT_specification (or DW_AT_abstract_origin or
1134 DW_AT_extension). */
6f06d47b 1135 sect_offset spec_offset {};
72bf9492
DJ
1136
1137 /* Pointers to this DIE's parent, first child, and next sibling,
1138 if any. */
6f06d47b
YQ
1139 struct partial_die_info *die_parent = nullptr;
1140 struct partial_die_info *die_child = nullptr;
1141 struct partial_die_info *die_sibling = nullptr;
1142
1143 friend struct partial_die_info *
1144 dwarf2_cu::find_partial_die (sect_offset sect_off);
1145
1146 private:
1147 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1148 partial_die_info (sect_offset sect_off)
1149 : partial_die_info (sect_off, DW_TAG_padding, 0)
1150 {
1151 }
1152
1153 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1154 int has_children_)
1155 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1156 {
1157 is_external = 0;
1158 is_declaration = 0;
1159 has_type = 0;
1160 has_specification = 0;
1161 has_pc_info = 0;
1162 may_be_inlined = 0;
1163 main_subprogram = 0;
1164 scope_set = 0;
1165 has_byte_size = 0;
1166 has_const_value = 0;
1167 has_template_arguments = 0;
1168 fixup_called = 0;
1169 is_dwz = 0;
1170 spec_is_dwz = 0;
1171 }
c906108c
SS
1172 };
1173
0963b4bd 1174/* This data structure holds the information of an abbrev. */
c906108c
SS
1175struct abbrev_info
1176 {
1177 unsigned int number; /* number identifying abbrev */
1178 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1179 unsigned short has_children; /* boolean */
1180 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1181 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1182 struct abbrev_info *next; /* next in chain */
1183 };
1184
1185struct attr_abbrev
1186 {
9d25dd43
DE
1187 ENUM_BITFIELD(dwarf_attribute) name : 16;
1188 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1189
1190 /* It is valid only if FORM is DW_FORM_implicit_const. */
1191 LONGEST implicit_const;
c906108c
SS
1192 };
1193
433df2d4
DE
1194/* Size of abbrev_table.abbrev_hash_table. */
1195#define ABBREV_HASH_SIZE 121
1196
1197/* Top level data structure to contain an abbreviation table. */
1198
1199struct abbrev_table
1200{
685af9cd
TT
1201 explicit abbrev_table (sect_offset off)
1202 : sect_off (off)
1203 {
4a17f768 1204 m_abbrevs =
685af9cd 1205 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1206 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1207 }
1208
1209 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1210
1211 /* Allocate space for a struct abbrev_info object in
1212 ABBREV_TABLE. */
1213 struct abbrev_info *alloc_abbrev ();
1214
1215 /* Add an abbreviation to the table. */
1216 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1217
1218 /* Look up an abbrev in the table.
1219 Returns NULL if the abbrev is not found. */
1220
1221 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1222
1223
f4dc4d17
DE
1224 /* Where the abbrev table came from.
1225 This is used as a sanity check when the table is used. */
685af9cd 1226 const sect_offset sect_off;
433df2d4
DE
1227
1228 /* Storage for the abbrev table. */
685af9cd 1229 auto_obstack abbrev_obstack;
433df2d4 1230
4a17f768
YQ
1231private:
1232
433df2d4
DE
1233 /* Hash table of abbrevs.
1234 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1235 It could be statically allocated, but the previous code didn't so we
1236 don't either. */
4a17f768 1237 struct abbrev_info **m_abbrevs;
433df2d4
DE
1238};
1239
685af9cd
TT
1240typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1241
0963b4bd 1242/* Attributes have a name and a value. */
b60c80d6
DJ
1243struct attribute
1244 {
9d25dd43 1245 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1246 ENUM_BITFIELD(dwarf_form) form : 15;
1247
1248 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1249 field should be in u.str (existing only for DW_STRING) but it is kept
1250 here for better struct attribute alignment. */
1251 unsigned int string_is_canonical : 1;
1252
b60c80d6
DJ
1253 union
1254 {
15d034d0 1255 const char *str;
b60c80d6 1256 struct dwarf_block *blk;
43bbcdc2
PH
1257 ULONGEST unsnd;
1258 LONGEST snd;
b60c80d6 1259 CORE_ADDR addr;
ac9ec31b 1260 ULONGEST signature;
b60c80d6
DJ
1261 }
1262 u;
1263 };
1264
0963b4bd 1265/* This data structure holds a complete die structure. */
c906108c
SS
1266struct die_info
1267 {
76815b17
DE
1268 /* DWARF-2 tag for this DIE. */
1269 ENUM_BITFIELD(dwarf_tag) tag : 16;
1270
1271 /* Number of attributes */
98bfdba5
PA
1272 unsigned char num_attrs;
1273
1274 /* True if we're presently building the full type name for the
1275 type derived from this DIE. */
1276 unsigned char building_fullname : 1;
76815b17 1277
adde2bff
DE
1278 /* True if this die is in process. PR 16581. */
1279 unsigned char in_process : 1;
1280
76815b17
DE
1281 /* Abbrev number */
1282 unsigned int abbrev;
1283
93311388 1284 /* Offset in .debug_info or .debug_types section. */
9c541725 1285 sect_offset sect_off;
78ba4af6
JB
1286
1287 /* The dies in a compilation unit form an n-ary tree. PARENT
1288 points to this die's parent; CHILD points to the first child of
1289 this node; and all the children of a given node are chained
4950bc1c 1290 together via their SIBLING fields. */
639d11d3
DC
1291 struct die_info *child; /* Its first child, if any. */
1292 struct die_info *sibling; /* Its next sibling, if any. */
1293 struct die_info *parent; /* Its parent, if any. */
c906108c 1294
b60c80d6
DJ
1295 /* An array of attributes, with NUM_ATTRS elements. There may be
1296 zero, but it's not common and zero-sized arrays are not
1297 sufficiently portable C. */
1298 struct attribute attrs[1];
c906108c
SS
1299 };
1300
0963b4bd 1301/* Get at parts of an attribute structure. */
c906108c
SS
1302
1303#define DW_STRING(attr) ((attr)->u.str)
8285870a 1304#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1305#define DW_UNSND(attr) ((attr)->u.unsnd)
1306#define DW_BLOCK(attr) ((attr)->u.blk)
1307#define DW_SND(attr) ((attr)->u.snd)
1308#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1309#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1310
0963b4bd 1311/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1312struct dwarf_block
1313 {
56eb65bd 1314 size_t size;
1d6edc3c
JK
1315
1316 /* Valid only if SIZE is not zero. */
d521ce57 1317 const gdb_byte *data;
c906108c
SS
1318 };
1319
c906108c
SS
1320#ifndef ATTR_ALLOC_CHUNK
1321#define ATTR_ALLOC_CHUNK 4
1322#endif
1323
c906108c
SS
1324/* Allocate fields for structs, unions and enums in this size. */
1325#ifndef DW_FIELD_ALLOC_CHUNK
1326#define DW_FIELD_ALLOC_CHUNK 4
1327#endif
1328
c906108c
SS
1329/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1330 but this would require a corresponding change in unpack_field_as_long
1331 and friends. */
1332static int bits_per_byte = 8;
1333
2ddeaf8a
TT
1334/* When reading a variant or variant part, we track a bit more
1335 information about the field, and store it in an object of this
1336 type. */
1337
1338struct variant_field
1339{
1340 /* If we see a DW_TAG_variant, then this will be the discriminant
1341 value. */
1342 ULONGEST discriminant_value;
1343 /* If we see a DW_TAG_variant, then this will be set if this is the
1344 default branch. */
1345 bool default_branch;
1346 /* While reading a DW_TAG_variant_part, this will be set if this
1347 field is the discriminant. */
1348 bool is_discriminant;
1349};
1350
52059ffd
TT
1351struct nextfield
1352{
be2daae6
TT
1353 int accessibility = 0;
1354 int virtuality = 0;
2ddeaf8a 1355 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1356 struct variant_field variant {};
1357 struct field field {};
52059ffd
TT
1358};
1359
1360struct fnfieldlist
1361{
be2daae6
TT
1362 const char *name = nullptr;
1363 std::vector<struct fn_field> fnfields;
52059ffd
TT
1364};
1365
c906108c
SS
1366/* The routines that read and process dies for a C struct or C++ class
1367 pass lists of data member fields and lists of member function fields
1368 in an instance of a field_info structure, as defined below. */
1369struct field_info
c5aa993b 1370 {
0963b4bd 1371 /* List of data member and baseclasses fields. */
be2daae6
TT
1372 std::vector<struct nextfield> fields;
1373 std::vector<struct nextfield> baseclasses;
c906108c 1374
7d0ccb61 1375 /* Number of fields (including baseclasses). */
be2daae6 1376 int nfields = 0;
c906108c 1377
c5aa993b 1378 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1379 int non_public_fields = 0;
c906108c 1380
c5aa993b
JM
1381 /* Member function fieldlist array, contains name of possibly overloaded
1382 member function, number of overloaded member functions and a pointer
1383 to the head of the member function field chain. */
be2daae6 1384 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1385
1386 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1387 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1388 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1389
1390 /* Nested types defined by this class and the number of elements in this
1391 list. */
be2daae6 1392 std::vector<struct decl_field> nested_types_list;
c5aa993b 1393 };
c906108c 1394
10b3939b
DJ
1395/* One item on the queue of compilation units to read in full symbols
1396 for. */
1397struct dwarf2_queue_item
1398{
1399 struct dwarf2_per_cu_data *per_cu;
95554aad 1400 enum language pretend_language;
10b3939b
DJ
1401 struct dwarf2_queue_item *next;
1402};
1403
1404/* The current queue. */
1405static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1406
ae038cb0
DJ
1407/* Loaded secondary compilation units are kept in memory until they
1408 have not been referenced for the processing of this many
1409 compilation units. Set this to zero to disable caching. Cache
1410 sizes of up to at least twenty will improve startup time for
1411 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1412static int dwarf_max_cache_age = 5;
920d2a44 1413static void
b4f54984
DE
1414show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1415 struct cmd_list_element *c, const char *value)
920d2a44 1416{
3e43a32a 1417 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1418 "DWARF compilation units is %s.\n"),
920d2a44
AC
1419 value);
1420}
4390d890 1421\f
c906108c
SS
1422/* local function prototypes */
1423
a32a8923
DE
1424static const char *get_section_name (const struct dwarf2_section_info *);
1425
1426static const char *get_section_file_name (const struct dwarf2_section_info *);
1427
918dd910
JK
1428static void dwarf2_find_base_address (struct die_info *die,
1429 struct dwarf2_cu *cu);
1430
0018ea6f
DE
1431static struct partial_symtab *create_partial_symtab
1432 (struct dwarf2_per_cu_data *per_cu, const char *name);
1433
f1902523
JK
1434static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1435 const gdb_byte *info_ptr,
1436 struct die_info *type_unit_die,
1437 int has_children, void *data);
1438
ed2dc618
SM
1439static void dwarf2_build_psymtabs_hard
1440 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1441
72bf9492
DJ
1442static void scan_partial_symbols (struct partial_die_info *,
1443 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1444 int, struct dwarf2_cu *);
c906108c 1445
72bf9492
DJ
1446static void add_partial_symbol (struct partial_die_info *,
1447 struct dwarf2_cu *);
63d06c5c 1448
72bf9492
DJ
1449static void add_partial_namespace (struct partial_die_info *pdi,
1450 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1451 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1452
5d7cb8df 1453static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1454 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1455 struct dwarf2_cu *cu);
1456
72bf9492
DJ
1457static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1458 struct dwarf2_cu *cu);
91c24f0a 1459
bc30ff58
JB
1460static void add_partial_subprogram (struct partial_die_info *pdi,
1461 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1462 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1463
257e7a09
YQ
1464static void dwarf2_read_symtab (struct partial_symtab *,
1465 struct objfile *);
c906108c 1466
a14ed312 1467static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1468
685af9cd 1469static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1470 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1471 sect_offset);
433df2d4 1472
d521ce57 1473static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1474
dee91e82 1475static struct partial_die_info *load_partial_dies
d521ce57 1476 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1477
fb816e8b
TV
1478/* A pair of partial_die_info and compilation unit. */
1479struct cu_partial_die_info
1480{
1481 /* The compilation unit of the partial_die_info. */
1482 struct dwarf2_cu *cu;
1483 /* A partial_die_info. */
1484 struct partial_die_info *pdi;
122cf0f2
AB
1485
1486 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1487 : cu (cu),
1488 pdi (pdi)
1489 { /* Nothhing. */ }
1490
1491private:
1492 cu_partial_die_info () = delete;
fb816e8b
TV
1493};
1494
122cf0f2
AB
1495static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1496 struct dwarf2_cu *);
72bf9492 1497
d521ce57
TT
1498static const gdb_byte *read_attribute (const struct die_reader_specs *,
1499 struct attribute *, struct attr_abbrev *,
1500 const gdb_byte *);
a8329558 1501
a1855c1d 1502static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1503
a1855c1d 1504static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1505
a1855c1d 1506static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1507
15f18d14
AT
1508/* Read the next three bytes (little-endian order) as an unsigned integer. */
1509static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1510
a1855c1d 1511static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1512
a1855c1d 1513static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1514
d521ce57 1515static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1516 unsigned int *);
c906108c 1517
d521ce57 1518static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1519
1520static LONGEST read_checked_initial_length_and_offset
d521ce57 1521 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1522 unsigned int *, unsigned int *);
613e1657 1523
d521ce57
TT
1524static LONGEST read_offset (bfd *, const gdb_byte *,
1525 const struct comp_unit_head *,
c764a876
DE
1526 unsigned int *);
1527
d521ce57 1528static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1529
ed2dc618
SM
1530static sect_offset read_abbrev_offset
1531 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1532 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1533
d521ce57 1534static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1535
d521ce57 1536static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1537
ed2dc618
SM
1538static const char *read_indirect_string
1539 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1540 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1541
ed2dc618
SM
1542static const char *read_indirect_line_string
1543 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1544 const struct comp_unit_head *, unsigned int *);
36586728 1545
ed2dc618
SM
1546static const char *read_indirect_string_at_offset
1547 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1548 LONGEST str_offset);
927aa2e7 1549
ed2dc618
SM
1550static const char *read_indirect_string_from_dwz
1551 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1552
d521ce57 1553static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1554
d521ce57
TT
1555static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1556 const gdb_byte *,
3019eac3
DE
1557 unsigned int *);
1558
d521ce57 1559static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1560 ULONGEST str_index);
3019eac3 1561
e142c38c 1562static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1563
e142c38c
DJ
1564static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1565 struct dwarf2_cu *);
c906108c 1566
348e048f 1567static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1568 unsigned int);
348e048f 1569
7d45c7c3
KB
1570static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1571 struct dwarf2_cu *cu);
1572
a084a2a6
AT
1573static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1574
05cf31d1
JB
1575static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1576 struct dwarf2_cu *cu);
1577
e142c38c 1578static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1579
e142c38c 1580static struct die_info *die_specification (struct die_info *die,
f2f0e013 1581 struct dwarf2_cu **);
63d06c5c 1582
9c541725 1583static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1584 struct dwarf2_cu *cu);
debd256d 1585
f3f5162e 1586static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1587 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1588 CORE_ADDR, int decode_mapping);
c906108c 1589
804d2729
TT
1590static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1591 const char *);
c906108c 1592
a14ed312 1593static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1594 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1595
ff39bb5e 1596static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1597 struct dwarf2_cu *);
c906108c 1598
ff39bb5e 1599static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1600 struct type *type,
1601 const char *name,
1602 struct obstack *obstack,
12df843f 1603 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1604 const gdb_byte **bytes,
98bfdba5 1605 struct dwarf2_locexpr_baton **baton);
2df3850c 1606
e7c27a73 1607static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1608
b4ba55a1
JB
1609static int need_gnat_info (struct dwarf2_cu *);
1610
3e43a32a
MS
1611static struct type *die_descriptive_type (struct die_info *,
1612 struct dwarf2_cu *);
b4ba55a1
JB
1613
1614static void set_descriptive_type (struct type *, struct die_info *,
1615 struct dwarf2_cu *);
1616
e7c27a73
DJ
1617static struct type *die_containing_type (struct die_info *,
1618 struct dwarf2_cu *);
c906108c 1619
ff39bb5e 1620static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1621 struct dwarf2_cu *);
c906108c 1622
f792889a 1623static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1624
673bfd45
DE
1625static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1626
0d5cff50 1627static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1628
6e70227d 1629static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1630 const char *suffix, int physname,
1631 struct dwarf2_cu *cu);
63d06c5c 1632
e7c27a73 1633static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1634
348e048f
DE
1635static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1636
e7c27a73 1637static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1638
e7c27a73 1639static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1640
96408a79
SA
1641static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1642
71a3c369
TT
1643static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1644
ff013f42
JK
1645static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1646 struct dwarf2_cu *, struct partial_symtab *);
1647
3a2b436a 1648/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1649 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1650enum pc_bounds_kind
1651{
e385593e 1652 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1653 PC_BOUNDS_NOT_PRESENT,
1654
e385593e
JK
1655 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1656 were present but they do not form a valid range of PC addresses. */
1657 PC_BOUNDS_INVALID,
1658
3a2b436a
JK
1659 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1660 PC_BOUNDS_RANGES,
1661
1662 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1663 PC_BOUNDS_HIGH_LOW,
1664};
1665
1666static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1667 CORE_ADDR *, CORE_ADDR *,
1668 struct dwarf2_cu *,
1669 struct partial_symtab *);
c906108c 1670
fae299cd
DC
1671static void get_scope_pc_bounds (struct die_info *,
1672 CORE_ADDR *, CORE_ADDR *,
1673 struct dwarf2_cu *);
1674
801e3a5b
JB
1675static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1676 CORE_ADDR, struct dwarf2_cu *);
1677
a14ed312 1678static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1679 struct dwarf2_cu *);
c906108c 1680
a14ed312 1681static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1682 struct type *, struct dwarf2_cu *);
c906108c 1683
a14ed312 1684static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1685 struct die_info *, struct type *,
e7c27a73 1686 struct dwarf2_cu *);
c906108c 1687
a14ed312 1688static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1689 struct type *,
1690 struct dwarf2_cu *);
c906108c 1691
134d01f1 1692static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1693
e7c27a73 1694static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1695
e7c27a73 1696static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1697
5d7cb8df
JK
1698static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1699
804d2729 1700static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1701
27aa8d6a
SW
1702static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1703
74921315
KS
1704static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1705
f55ee35c
JK
1706static struct type *read_module_type (struct die_info *die,
1707 struct dwarf2_cu *cu);
1708
38d518c9 1709static const char *namespace_name (struct die_info *die,
e142c38c 1710 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1711
134d01f1 1712static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1713
e7c27a73 1714static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1715
6e70227d 1716static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1717 struct dwarf2_cu *);
1718
bf6af496 1719static struct die_info *read_die_and_siblings_1
d521ce57 1720 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1721 struct die_info *);
639d11d3 1722
dee91e82 1723static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1724 const gdb_byte *info_ptr,
1725 const gdb_byte **new_info_ptr,
639d11d3
DC
1726 struct die_info *parent);
1727
d521ce57
TT
1728static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1729 struct die_info **, const gdb_byte *,
1730 int *, int);
3019eac3 1731
d521ce57
TT
1732static const gdb_byte *read_full_die (const struct die_reader_specs *,
1733 struct die_info **, const gdb_byte *,
1734 int *);
93311388 1735
e7c27a73 1736static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1737
15d034d0
TT
1738static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1739 struct obstack *);
71c25dea 1740
15d034d0 1741static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1742
15d034d0 1743static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1744 struct die_info *die,
1745 struct dwarf2_cu *cu);
1746
ca69b9e6
DE
1747static const char *dwarf2_physname (const char *name, struct die_info *die,
1748 struct dwarf2_cu *cu);
1749
e142c38c 1750static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1751 struct dwarf2_cu **);
9219021c 1752
f39c6ffd 1753static const char *dwarf_tag_name (unsigned int);
c906108c 1754
f39c6ffd 1755static const char *dwarf_attr_name (unsigned int);
c906108c 1756
a084a2a6
AT
1757static const char *dwarf_unit_type_name (int unit_type);
1758
f39c6ffd 1759static const char *dwarf_form_name (unsigned int);
c906108c 1760
a121b7c1 1761static const char *dwarf_bool_name (unsigned int);
c906108c 1762
f39c6ffd 1763static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1764
f9aca02d 1765static struct die_info *sibling_die (struct die_info *);
c906108c 1766
d97bc12b
DE
1767static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1768
1769static void dump_die_for_error (struct die_info *);
1770
1771static void dump_die_1 (struct ui_file *, int level, int max_level,
1772 struct die_info *);
c906108c 1773
d97bc12b 1774/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1775
51545339 1776static void store_in_ref_table (struct die_info *,
10b3939b 1777 struct dwarf2_cu *);
c906108c 1778
ff39bb5e 1779static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1780
ff39bb5e 1781static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1782
348e048f 1783static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1784 const struct attribute *,
348e048f
DE
1785 struct dwarf2_cu **);
1786
10b3939b 1787static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1788 const struct attribute *,
f2f0e013 1789 struct dwarf2_cu **);
c906108c 1790
348e048f 1791static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1792 const struct attribute *,
348e048f
DE
1793 struct dwarf2_cu **);
1794
ac9ec31b
DE
1795static struct type *get_signatured_type (struct die_info *, ULONGEST,
1796 struct dwarf2_cu *);
1797
1798static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1799 const struct attribute *,
ac9ec31b
DE
1800 struct dwarf2_cu *);
1801
e5fe5e75 1802static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1803
52dc124a 1804static void read_signatured_type (struct signatured_type *);
348e048f 1805
63e43d3a
PMR
1806static int attr_to_dynamic_prop (const struct attribute *attr,
1807 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1808 struct dynamic_prop *prop, struct type *type);
63e43d3a 1809
c906108c
SS
1810/* memory allocation interface */
1811
7b5a2f43 1812static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1813
b60c80d6 1814static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1815
43f3e411 1816static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1817
6e5a29e1 1818static int attr_form_is_block (const struct attribute *);
8e19ed76 1819
6e5a29e1 1820static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1821
6e5a29e1 1822static int attr_form_is_constant (const struct attribute *);
3690dd37 1823
6e5a29e1 1824static int attr_form_is_ref (const struct attribute *);
7771576e 1825
8cf6f0b1
TT
1826static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1827 struct dwarf2_loclist_baton *baton,
ff39bb5e 1828 const struct attribute *attr);
8cf6f0b1 1829
ff39bb5e 1830static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1831 struct symbol *sym,
f1e6e072
TT
1832 struct dwarf2_cu *cu,
1833 int is_block);
4c2df51b 1834
d521ce57
TT
1835static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1836 const gdb_byte *info_ptr,
1837 struct abbrev_info *abbrev);
4bb7a0a7 1838
72bf9492
DJ
1839static hashval_t partial_die_hash (const void *item);
1840
1841static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1842
ae038cb0 1843static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1844 (sect_offset sect_off, unsigned int offset_in_dwz,
1845 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1846
9816fde3 1847static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1848 struct die_info *comp_unit_die,
1849 enum language pretend_language);
93311388 1850
ed2dc618 1851static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1852
dee91e82 1853static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1854
f792889a
DJ
1855static struct type *set_die_type (struct die_info *, struct type *,
1856 struct dwarf2_cu *);
1c379e20 1857
ed2dc618 1858static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1859
ed2dc618 1860static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1861
58f0c718 1862static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1863 enum language);
10b3939b 1864
95554aad
TT
1865static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1866 enum language);
10b3939b 1867
f4dc4d17
DE
1868static void process_full_type_unit (struct dwarf2_per_cu_data *,
1869 enum language);
1870
10b3939b
DJ
1871static void dwarf2_add_dependence (struct dwarf2_cu *,
1872 struct dwarf2_per_cu_data *);
1873
ae038cb0
DJ
1874static void dwarf2_mark (struct dwarf2_cu *);
1875
1876static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1877
b64f50a1 1878static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1879 struct dwarf2_per_cu_data *);
673bfd45 1880
f792889a 1881static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1882
95554aad
TT
1883static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1884 enum language pretend_language);
1885
ed2dc618 1886static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1887
9a49df9d
AB
1888static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1889static struct type *dwarf2_per_cu_addr_sized_int_type
1890 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1891
b303c6f6
AB
1892/* Class, the destructor of which frees all allocated queue entries. This
1893 will only have work to do if an error was thrown while processing the
1894 dwarf. If no error was thrown then the queue entries should have all
1895 been processed, and freed, as we went along. */
1896
1897class dwarf2_queue_guard
1898{
1899public:
1900 dwarf2_queue_guard () = default;
1901
1902 /* Free any entries remaining on the queue. There should only be
1903 entries left if we hit an error while processing the dwarf. */
1904 ~dwarf2_queue_guard ()
1905 {
1906 struct dwarf2_queue_item *item, *last;
1907
1908 item = dwarf2_queue;
1909 while (item)
1910 {
1911 /* Anything still marked queued is likely to be in an
1912 inconsistent state, so discard it. */
1913 if (item->per_cu->queued)
1914 {
1915 if (item->per_cu->cu != NULL)
1916 free_one_cached_comp_unit (item->per_cu);
1917 item->per_cu->queued = 0;
1918 }
1919
1920 last = item;
1921 item = item->next;
1922 xfree (last);
1923 }
1924
1925 dwarf2_queue = dwarf2_queue_tail = NULL;
1926 }
1927};
1928
d721ba37
PA
1929/* The return type of find_file_and_directory. Note, the enclosed
1930 string pointers are only valid while this object is valid. */
1931
1932struct file_and_directory
1933{
1934 /* The filename. This is never NULL. */
1935 const char *name;
1936
1937 /* The compilation directory. NULL if not known. If we needed to
1938 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1939 points directly to the DW_AT_comp_dir string attribute owned by
1940 the obstack that owns the DIE. */
1941 const char *comp_dir;
1942
1943 /* If we needed to build a new string for comp_dir, this is what
1944 owns the storage. */
1945 std::string comp_dir_storage;
1946};
1947
1948static file_and_directory find_file_and_directory (struct die_info *die,
1949 struct dwarf2_cu *cu);
9291a0cd
TT
1950
1951static char *file_full_name (int file, struct line_header *lh,
1952 const char *comp_dir);
1953
43988095
JK
1954/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1955enum class rcuh_kind { COMPILE, TYPE };
1956
d521ce57 1957static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1958 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1959 struct comp_unit_head *header,
36586728 1960 struct dwarf2_section_info *section,
d521ce57 1961 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1962 rcuh_kind section_kind);
36586728 1963
fd820528 1964static void init_cutu_and_read_dies
f4dc4d17 1965 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1966 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1967 die_reader_func_ftype *die_reader_func, void *data);
1968
dee91e82
DE
1969static void init_cutu_and_read_dies_simple
1970 (struct dwarf2_per_cu_data *this_cu,
1971 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1972
673bfd45 1973static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1974
3019eac3
DE
1975static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1976
57d63ce2 1977static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1978 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1979 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1980 ULONGEST signature, int is_debug_types);
a2ce51a0 1981
ed2dc618
SM
1982static struct dwp_file *get_dwp_file
1983 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1984
3019eac3 1985static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1986 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1987
1988static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1989 (struct signatured_type *, const char *, const char *);
3019eac3 1990
89e63ee4
DE
1991static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1992
263db9a1
TT
1993/* A unique pointer to a dwo_file. */
1994
51ac9db5 1995typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1996
ed2dc618 1997static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1998
1b80a9fa 1999static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2000
2001static void free_line_header_voidp (void *arg);
4390d890
DE
2002\f
2003/* Various complaints about symbol reading that don't abort the process. */
2004
2005static void
2006dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2007{
b98664d3 2008 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2009}
2010
2011static void
2012dwarf2_debug_line_missing_file_complaint (void)
2013{
b98664d3 2014 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2015}
2016
2017static void
2018dwarf2_debug_line_missing_end_sequence_complaint (void)
2019{
b98664d3 2020 complaint (_(".debug_line section has line "
4390d890
DE
2021 "program sequence without an end"));
2022}
2023
2024static void
2025dwarf2_complex_location_expr_complaint (void)
2026{
b98664d3 2027 complaint (_("location expression too complex"));
4390d890
DE
2028}
2029
2030static void
2031dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2032 int arg3)
2033{
b98664d3 2034 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2035 arg1, arg2, arg3);
2036}
2037
2038static void
2039dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2040{
b98664d3 2041 complaint (_("debug info runs off end of %s section"
4390d890 2042 " [in module %s]"),
a32a8923
DE
2043 get_section_name (section),
2044 get_section_file_name (section));
4390d890 2045}
1b80a9fa 2046
4390d890
DE
2047static void
2048dwarf2_macro_malformed_definition_complaint (const char *arg1)
2049{
b98664d3 2050 complaint (_("macro debug info contains a "
4390d890
DE
2051 "malformed macro definition:\n`%s'"),
2052 arg1);
2053}
2054
2055static void
2056dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2057{
b98664d3 2058 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2059 arg1, arg2);
2060}
527f3840
JK
2061
2062/* Hash function for line_header_hash. */
2063
2064static hashval_t
2065line_header_hash (const struct line_header *ofs)
2066{
9c541725 2067 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2068}
2069
2070/* Hash function for htab_create_alloc_ex for line_header_hash. */
2071
2072static hashval_t
2073line_header_hash_voidp (const void *item)
2074{
9a3c8263 2075 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2076
2077 return line_header_hash (ofs);
2078}
2079
2080/* Equality function for line_header_hash. */
2081
2082static int
2083line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2084{
9a3c8263
SM
2085 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2086 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2087
9c541725 2088 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2089 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2090}
2091
4390d890 2092\f
9291a0cd 2093
31aa7e4e
JB
2094/* Read the given attribute value as an address, taking the attribute's
2095 form into account. */
2096
2097static CORE_ADDR
2098attr_value_as_address (struct attribute *attr)
2099{
2100 CORE_ADDR addr;
2101
336d760d
AT
2102 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2103 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2104 {
2105 /* Aside from a few clearly defined exceptions, attributes that
2106 contain an address must always be in DW_FORM_addr form.
2107 Unfortunately, some compilers happen to be violating this
2108 requirement by encoding addresses using other forms, such
2109 as DW_FORM_data4 for example. For those broken compilers,
2110 we try to do our best, without any guarantee of success,
2111 to interpret the address correctly. It would also be nice
2112 to generate a complaint, but that would require us to maintain
2113 a list of legitimate cases where a non-address form is allowed,
2114 as well as update callers to pass in at least the CU's DWARF
2115 version. This is more overhead than what we're willing to
2116 expand for a pretty rare case. */
2117 addr = DW_UNSND (attr);
2118 }
2119 else
2120 addr = DW_ADDR (attr);
2121
2122 return addr;
2123}
2124
330cdd98
PA
2125/* See declaration. */
2126
2127dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2128 const dwarf2_debug_sections *names,
2129 bool can_copy_)
2130 : objfile (objfile_),
2131 can_copy (can_copy_)
330cdd98
PA
2132{
2133 if (names == NULL)
2134 names = &dwarf2_elf_names;
2135
2136 bfd *obfd = objfile->obfd;
2137
2138 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2139 locate_sections (obfd, sec, *names);
2140}
2141
2142dwarf2_per_objfile::~dwarf2_per_objfile ()
2143{
2144 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2145 free_cached_comp_units ();
2146
2147 if (quick_file_names_table)
2148 htab_delete (quick_file_names_table);
2149
2150 if (line_header_hash)
2151 htab_delete (line_header_hash);
2152
b76e467d 2153 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2154 per_cu->imported_symtabs_free ();
fc8e7e75 2155
b2bdb8cf 2156 for (signatured_type *sig_type : all_type_units)
ae640021 2157 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2158
330cdd98
PA
2159 /* Everything else should be on the objfile obstack. */
2160}
2161
2162/* See declaration. */
2163
2164void
2165dwarf2_per_objfile::free_cached_comp_units ()
2166{
2167 dwarf2_per_cu_data *per_cu = read_in_chain;
2168 dwarf2_per_cu_data **last_chain = &read_in_chain;
2169 while (per_cu != NULL)
2170 {
2171 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2172
fcd3b13d 2173 delete per_cu->cu;
330cdd98
PA
2174 *last_chain = next_cu;
2175 per_cu = next_cu;
2176 }
2177}
2178
11ed8cad
TT
2179/* A helper class that calls free_cached_comp_units on
2180 destruction. */
2181
2182class free_cached_comp_units
2183{
2184public:
2185
2186 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2187 : m_per_objfile (per_objfile)
2188 {
2189 }
2190
2191 ~free_cached_comp_units ()
2192 {
2193 m_per_objfile->free_cached_comp_units ();
2194 }
2195
2196 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2197
2198private:
2199
2200 dwarf2_per_objfile *m_per_objfile;
2201};
2202
c906108c 2203/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2204 information and return true if we have enough to do something.
2205 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2206 ELF names are used. CAN_COPY is true for formats where symbol
2207 interposition is possible and so symbol values must follow copy
2208 relocation rules. */
c906108c
SS
2209
2210int
251d32d9 2211dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2212 const struct dwarf2_debug_sections *names,
2213 bool can_copy)
c906108c 2214{
97cbe998
SDJ
2215 if (objfile->flags & OBJF_READNEVER)
2216 return 0;
2217
ed2dc618
SM
2218 struct dwarf2_per_objfile *dwarf2_per_objfile
2219 = get_dwarf2_per_objfile (objfile);
2220
2221 if (dwarf2_per_objfile == NULL)
5bfd760d 2222 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2223 names,
2224 can_copy);
5bfd760d 2225
73869dc2 2226 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2227 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2228 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2229 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2230}
2231
2232/* Return the containing section of virtual section SECTION. */
2233
2234static struct dwarf2_section_info *
2235get_containing_section (const struct dwarf2_section_info *section)
2236{
2237 gdb_assert (section->is_virtual);
2238 return section->s.containing_section;
c906108c
SS
2239}
2240
a32a8923
DE
2241/* Return the bfd owner of SECTION. */
2242
2243static struct bfd *
2244get_section_bfd_owner (const struct dwarf2_section_info *section)
2245{
73869dc2
DE
2246 if (section->is_virtual)
2247 {
2248 section = get_containing_section (section);
2249 gdb_assert (!section->is_virtual);
2250 }
049412e3 2251 return section->s.section->owner;
a32a8923
DE
2252}
2253
2254/* Return the bfd section of SECTION.
2255 Returns NULL if the section is not present. */
2256
2257static asection *
2258get_section_bfd_section (const struct dwarf2_section_info *section)
2259{
73869dc2
DE
2260 if (section->is_virtual)
2261 {
2262 section = get_containing_section (section);
2263 gdb_assert (!section->is_virtual);
2264 }
049412e3 2265 return section->s.section;
a32a8923
DE
2266}
2267
2268/* Return the name of SECTION. */
2269
2270static const char *
2271get_section_name (const struct dwarf2_section_info *section)
2272{
2273 asection *sectp = get_section_bfd_section (section);
2274
2275 gdb_assert (sectp != NULL);
fd361982 2276 return bfd_section_name (sectp);
a32a8923
DE
2277}
2278
2279/* Return the name of the file SECTION is in. */
2280
2281static const char *
2282get_section_file_name (const struct dwarf2_section_info *section)
2283{
2284 bfd *abfd = get_section_bfd_owner (section);
2285
2286 return bfd_get_filename (abfd);
2287}
2288
2289/* Return the id of SECTION.
2290 Returns 0 if SECTION doesn't exist. */
2291
2292static int
2293get_section_id (const struct dwarf2_section_info *section)
2294{
2295 asection *sectp = get_section_bfd_section (section);
2296
2297 if (sectp == NULL)
2298 return 0;
2299 return sectp->id;
2300}
2301
2302/* Return the flags of SECTION.
73869dc2 2303 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2304
2305static int
2306get_section_flags (const struct dwarf2_section_info *section)
2307{
2308 asection *sectp = get_section_bfd_section (section);
2309
2310 gdb_assert (sectp != NULL);
fd361982 2311 return bfd_section_flags (sectp);
a32a8923
DE
2312}
2313
251d32d9
TG
2314/* When loading sections, we look either for uncompressed section or for
2315 compressed section names. */
233a11ab
CS
2316
2317static int
251d32d9
TG
2318section_is_p (const char *section_name,
2319 const struct dwarf2_section_names *names)
233a11ab 2320{
251d32d9
TG
2321 if (names->normal != NULL
2322 && strcmp (section_name, names->normal) == 0)
2323 return 1;
2324 if (names->compressed != NULL
2325 && strcmp (section_name, names->compressed) == 0)
2326 return 1;
2327 return 0;
233a11ab
CS
2328}
2329
330cdd98 2330/* See declaration. */
c906108c 2331
330cdd98
PA
2332void
2333dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2334 const dwarf2_debug_sections &names)
c906108c 2335{
fd361982 2336 flagword aflag = bfd_section_flags (sectp);
251d32d9 2337
dc7650b8
JK
2338 if ((aflag & SEC_HAS_CONTENTS) == 0)
2339 {
2340 }
330cdd98 2341 else if (section_is_p (sectp->name, &names.info))
c906108c 2342 {
330cdd98 2343 this->info.s.section = sectp;
fd361982 2344 this->info.size = bfd_section_size (sectp);
c906108c 2345 }
330cdd98 2346 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2347 {
330cdd98 2348 this->abbrev.s.section = sectp;
fd361982 2349 this->abbrev.size = bfd_section_size (sectp);
c906108c 2350 }
330cdd98 2351 else if (section_is_p (sectp->name, &names.line))
c906108c 2352 {
330cdd98 2353 this->line.s.section = sectp;
fd361982 2354 this->line.size = bfd_section_size (sectp);
c906108c 2355 }
330cdd98 2356 else if (section_is_p (sectp->name, &names.loc))
c906108c 2357 {
330cdd98 2358 this->loc.s.section = sectp;
fd361982 2359 this->loc.size = bfd_section_size (sectp);
c906108c 2360 }
330cdd98 2361 else if (section_is_p (sectp->name, &names.loclists))
43988095 2362 {
330cdd98 2363 this->loclists.s.section = sectp;
fd361982 2364 this->loclists.size = bfd_section_size (sectp);
43988095 2365 }
330cdd98 2366 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2367 {
330cdd98 2368 this->macinfo.s.section = sectp;
fd361982 2369 this->macinfo.size = bfd_section_size (sectp);
c906108c 2370 }
330cdd98 2371 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2372 {
330cdd98 2373 this->macro.s.section = sectp;
fd361982 2374 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2375 }
330cdd98 2376 else if (section_is_p (sectp->name, &names.str))
c906108c 2377 {
330cdd98 2378 this->str.s.section = sectp;
fd361982 2379 this->str.size = bfd_section_size (sectp);
c906108c 2380 }
330cdd98 2381 else if (section_is_p (sectp->name, &names.line_str))
43988095 2382 {
330cdd98 2383 this->line_str.s.section = sectp;
fd361982 2384 this->line_str.size = bfd_section_size (sectp);
43988095 2385 }
330cdd98 2386 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2387 {
330cdd98 2388 this->addr.s.section = sectp;
fd361982 2389 this->addr.size = bfd_section_size (sectp);
3019eac3 2390 }
330cdd98 2391 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2392 {
330cdd98 2393 this->frame.s.section = sectp;
fd361982 2394 this->frame.size = bfd_section_size (sectp);
b6af0555 2395 }
330cdd98 2396 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2397 {
330cdd98 2398 this->eh_frame.s.section = sectp;
fd361982 2399 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2400 }
330cdd98 2401 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2402 {
330cdd98 2403 this->ranges.s.section = sectp;
fd361982 2404 this->ranges.size = bfd_section_size (sectp);
af34e669 2405 }
330cdd98 2406 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2407 {
330cdd98 2408 this->rnglists.s.section = sectp;
fd361982 2409 this->rnglists.size = bfd_section_size (sectp);
43988095 2410 }
330cdd98 2411 else if (section_is_p (sectp->name, &names.types))
348e048f 2412 {
8b70b953
TT
2413 struct dwarf2_section_info type_section;
2414
2415 memset (&type_section, 0, sizeof (type_section));
049412e3 2416 type_section.s.section = sectp;
fd361982 2417 type_section.size = bfd_section_size (sectp);
8b70b953 2418
fd5866f6 2419 this->types.push_back (type_section);
348e048f 2420 }
330cdd98 2421 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2422 {
330cdd98 2423 this->gdb_index.s.section = sectp;
fd361982 2424 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2425 }
927aa2e7
JK
2426 else if (section_is_p (sectp->name, &names.debug_names))
2427 {
2428 this->debug_names.s.section = sectp;
fd361982 2429 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2430 }
2431 else if (section_is_p (sectp->name, &names.debug_aranges))
2432 {
2433 this->debug_aranges.s.section = sectp;
fd361982 2434 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2435 }
dce234bc 2436
fd361982
AM
2437 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2438 && bfd_section_vma (sectp) == 0)
330cdd98 2439 this->has_section_at_zero = true;
c906108c
SS
2440}
2441
fceca515
DE
2442/* A helper function that decides whether a section is empty,
2443 or not present. */
9e0ac564
TT
2444
2445static int
19ac8c2e 2446dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2447{
73869dc2
DE
2448 if (section->is_virtual)
2449 return section->size == 0;
049412e3 2450 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2451}
2452
cd4fb1b2 2453/* See dwarf2read.h. */
c906108c 2454
cd4fb1b2
SM
2455void
2456dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2457{
a32a8923 2458 asection *sectp;
3019eac3 2459 bfd *abfd;
dce234bc 2460 gdb_byte *buf, *retbuf;
c906108c 2461
be391dca
TT
2462 if (info->readin)
2463 return;
dce234bc 2464 info->buffer = NULL;
dc4ccb6f 2465 info->readin = true;
188dd5d6 2466
9e0ac564 2467 if (dwarf2_section_empty_p (info))
dce234bc 2468 return;
c906108c 2469
a32a8923 2470 sectp = get_section_bfd_section (info);
3019eac3 2471
73869dc2
DE
2472 /* If this is a virtual section we need to read in the real one first. */
2473 if (info->is_virtual)
2474 {
2475 struct dwarf2_section_info *containing_section =
2476 get_containing_section (info);
2477
2478 gdb_assert (sectp != NULL);
2479 if ((sectp->flags & SEC_RELOC) != 0)
2480 {
2481 error (_("Dwarf Error: DWP format V2 with relocations is not"
2482 " supported in section %s [in module %s]"),
2483 get_section_name (info), get_section_file_name (info));
2484 }
2485 dwarf2_read_section (objfile, containing_section);
2486 /* Other code should have already caught virtual sections that don't
2487 fit. */
2488 gdb_assert (info->virtual_offset + info->size
2489 <= containing_section->size);
2490 /* If the real section is empty or there was a problem reading the
2491 section we shouldn't get here. */
2492 gdb_assert (containing_section->buffer != NULL);
2493 info->buffer = containing_section->buffer + info->virtual_offset;
2494 return;
2495 }
2496
4bf44c1c
TT
2497 /* If the section has relocations, we must read it ourselves.
2498 Otherwise we attach it to the BFD. */
2499 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2500 {
d521ce57 2501 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2502 return;
dce234bc 2503 }
dce234bc 2504
224c3ddb 2505 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2506 info->buffer = buf;
dce234bc
PP
2507
2508 /* When debugging .o files, we may need to apply relocations; see
2509 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2510 We never compress sections in .o files, so we only need to
2511 try this when the section is not compressed. */
ac8035ab 2512 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2513 if (retbuf != NULL)
2514 {
2515 info->buffer = retbuf;
2516 return;
2517 }
2518
a32a8923
DE
2519 abfd = get_section_bfd_owner (info);
2520 gdb_assert (abfd != NULL);
2521
dce234bc
PP
2522 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2523 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2524 {
2525 error (_("Dwarf Error: Can't read DWARF data"
2526 " in section %s [in module %s]"),
fd361982 2527 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2528 }
dce234bc
PP
2529}
2530
9e0ac564
TT
2531/* A helper function that returns the size of a section in a safe way.
2532 If you are positive that the section has been read before using the
2533 size, then it is safe to refer to the dwarf2_section_info object's
2534 "size" field directly. In other cases, you must call this
2535 function, because for compressed sections the size field is not set
2536 correctly until the section has been read. */
2537
2538static bfd_size_type
2539dwarf2_section_size (struct objfile *objfile,
2540 struct dwarf2_section_info *info)
2541{
2542 if (!info->readin)
2543 dwarf2_read_section (objfile, info);
2544 return info->size;
2545}
2546
dce234bc 2547/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2548 SECTION_NAME. */
af34e669 2549
dce234bc 2550void
3017a003
TG
2551dwarf2_get_section_info (struct objfile *objfile,
2552 enum dwarf2_section_enum sect,
d521ce57 2553 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2554 bfd_size_type *sizep)
2555{
5bfd760d 2556 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2557 struct dwarf2_section_info *info;
a3b2a86b
TT
2558
2559 /* We may see an objfile without any DWARF, in which case we just
2560 return nothing. */
2561 if (data == NULL)
2562 {
2563 *sectp = NULL;
2564 *bufp = NULL;
2565 *sizep = 0;
2566 return;
2567 }
3017a003
TG
2568 switch (sect)
2569 {
2570 case DWARF2_DEBUG_FRAME:
2571 info = &data->frame;
2572 break;
2573 case DWARF2_EH_FRAME:
2574 info = &data->eh_frame;
2575 break;
2576 default:
2577 gdb_assert_not_reached ("unexpected section");
2578 }
dce234bc 2579
9e0ac564 2580 dwarf2_read_section (objfile, info);
dce234bc 2581
a32a8923 2582 *sectp = get_section_bfd_section (info);
dce234bc
PP
2583 *bufp = info->buffer;
2584 *sizep = info->size;
2585}
2586
36586728
TT
2587/* A helper function to find the sections for a .dwz file. */
2588
2589static void
2590locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2591{
9a3c8263 2592 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2593
2594 /* Note that we only support the standard ELF names, because .dwz
2595 is ELF-only (at the time of writing). */
2596 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2597 {
049412e3 2598 dwz_file->abbrev.s.section = sectp;
fd361982 2599 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2600 }
2601 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2602 {
049412e3 2603 dwz_file->info.s.section = sectp;
fd361982 2604 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2605 }
2606 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2607 {
049412e3 2608 dwz_file->str.s.section = sectp;
fd361982 2609 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2610 }
2611 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2612 {
049412e3 2613 dwz_file->line.s.section = sectp;
fd361982 2614 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2615 }
2616 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2617 {
049412e3 2618 dwz_file->macro.s.section = sectp;
fd361982 2619 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2620 }
2ec9a5e0
TT
2621 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2622 {
049412e3 2623 dwz_file->gdb_index.s.section = sectp;
fd361982 2624 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2625 }
927aa2e7
JK
2626 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2627 {
2628 dwz_file->debug_names.s.section = sectp;
fd361982 2629 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2630 }
36586728
TT
2631}
2632
c4973306 2633/* See dwarf2read.h. */
36586728 2634
c4973306 2635struct dwz_file *
ed2dc618 2636dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2637{
36586728 2638 const char *filename;
acd13123 2639 bfd_size_type buildid_len_arg;
dc294be5
TT
2640 size_t buildid_len;
2641 bfd_byte *buildid;
36586728
TT
2642
2643 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2644 return dwarf2_per_objfile->dwz_file.get ();
36586728 2645
4db1a1dc 2646 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2647 gdb::unique_xmalloc_ptr<char> data
2648 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2649 &buildid_len_arg, &buildid));
4db1a1dc
TT
2650 if (data == NULL)
2651 {
2652 if (bfd_get_error () == bfd_error_no_error)
2653 return NULL;
2654 error (_("could not read '.gnu_debugaltlink' section: %s"),
2655 bfd_errmsg (bfd_get_error ()));
2656 }
791afaa2
TT
2657
2658 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2659
acd13123
TT
2660 buildid_len = (size_t) buildid_len_arg;
2661
791afaa2 2662 filename = data.get ();
d721ba37
PA
2663
2664 std::string abs_storage;
36586728
TT
2665 if (!IS_ABSOLUTE_PATH (filename))
2666 {
14278e1f
TT
2667 gdb::unique_xmalloc_ptr<char> abs
2668 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2669
14278e1f 2670 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2671 filename = abs_storage.c_str ();
36586728
TT
2672 }
2673
dc294be5
TT
2674 /* First try the file name given in the section. If that doesn't
2675 work, try to use the build-id instead. */
192b62ce 2676 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2677 if (dwz_bfd != NULL)
36586728 2678 {
192b62ce 2679 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2680 dwz_bfd.reset (nullptr);
36586728
TT
2681 }
2682
dc294be5
TT
2683 if (dwz_bfd == NULL)
2684 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2685
2686 if (dwz_bfd == NULL)
2687 error (_("could not find '.gnu_debugaltlink' file for %s"),
2688 objfile_name (dwarf2_per_objfile->objfile));
2689
7ff8cb8c
TT
2690 std::unique_ptr<struct dwz_file> result
2691 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2692
7ff8cb8c
TT
2693 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2694 result.get ());
36586728 2695
7ff8cb8c
TT
2696 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2697 result->dwz_bfd.get ());
2698 dwarf2_per_objfile->dwz_file = std::move (result);
2699 return dwarf2_per_objfile->dwz_file.get ();
36586728 2700}
9291a0cd 2701\f
7b9f3c50
DE
2702/* DWARF quick_symbols_functions support. */
2703
2704/* TUs can share .debug_line entries, and there can be a lot more TUs than
2705 unique line tables, so we maintain a separate table of all .debug_line
2706 derived entries to support the sharing.
2707 All the quick functions need is the list of file names. We discard the
2708 line_header when we're done and don't need to record it here. */
2709struct quick_file_names
2710{
094b34ac
DE
2711 /* The data used to construct the hash key. */
2712 struct stmt_list_hash hash;
7b9f3c50
DE
2713
2714 /* The number of entries in file_names, real_names. */
2715 unsigned int num_file_names;
2716
2717 /* The file names from the line table, after being run through
2718 file_full_name. */
2719 const char **file_names;
2720
2721 /* The file names from the line table after being run through
2722 gdb_realpath. These are computed lazily. */
2723 const char **real_names;
2724};
2725
2726/* When using the index (and thus not using psymtabs), each CU has an
2727 object of this type. This is used to hold information needed by
2728 the various "quick" methods. */
2729struct dwarf2_per_cu_quick_data
2730{
2731 /* The file table. This can be NULL if there was no file table
2732 or it's currently not read in.
2733 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2734 struct quick_file_names *file_names;
2735
2736 /* The corresponding symbol table. This is NULL if symbols for this
2737 CU have not yet been read. */
43f3e411 2738 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2739
2740 /* A temporary mark bit used when iterating over all CUs in
2741 expand_symtabs_matching. */
2742 unsigned int mark : 1;
2743
2744 /* True if we've tried to read the file table and found there isn't one.
2745 There will be no point in trying to read it again next time. */
2746 unsigned int no_file_data : 1;
2747};
2748
094b34ac
DE
2749/* Utility hash function for a stmt_list_hash. */
2750
2751static hashval_t
2752hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2753{
2754 hashval_t v = 0;
2755
2756 if (stmt_list_hash->dwo_unit != NULL)
2757 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2758 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2759 return v;
2760}
2761
2762/* Utility equality function for a stmt_list_hash. */
2763
2764static int
2765eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2766 const struct stmt_list_hash *rhs)
2767{
2768 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2769 return 0;
2770 if (lhs->dwo_unit != NULL
2771 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2772 return 0;
2773
9c541725 2774 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2775}
2776
7b9f3c50
DE
2777/* Hash function for a quick_file_names. */
2778
2779static hashval_t
2780hash_file_name_entry (const void *e)
2781{
9a3c8263
SM
2782 const struct quick_file_names *file_data
2783 = (const struct quick_file_names *) e;
7b9f3c50 2784
094b34ac 2785 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2786}
2787
2788/* Equality function for a quick_file_names. */
2789
2790static int
2791eq_file_name_entry (const void *a, const void *b)
2792{
9a3c8263
SM
2793 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2794 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2795
094b34ac 2796 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2797}
2798
2799/* Delete function for a quick_file_names. */
2800
2801static void
2802delete_file_name_entry (void *e)
2803{
9a3c8263 2804 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2805 int i;
2806
2807 for (i = 0; i < file_data->num_file_names; ++i)
2808 {
2809 xfree ((void*) file_data->file_names[i]);
2810 if (file_data->real_names)
2811 xfree ((void*) file_data->real_names[i]);
2812 }
2813
2814 /* The space for the struct itself lives on objfile_obstack,
2815 so we don't free it here. */
2816}
2817
2818/* Create a quick_file_names hash table. */
2819
2820static htab_t
2821create_quick_file_names_table (unsigned int nr_initial_entries)
2822{
2823 return htab_create_alloc (nr_initial_entries,
2824 hash_file_name_entry, eq_file_name_entry,
2825 delete_file_name_entry, xcalloc, xfree);
2826}
9291a0cd 2827
918dd910
JK
2828/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2829 have to be created afterwards. You should call age_cached_comp_units after
2830 processing PER_CU->CU. dw2_setup must have been already called. */
2831
2832static void
58f0c718 2833load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2834{
3019eac3 2835 if (per_cu->is_debug_types)
e5fe5e75 2836 load_full_type_unit (per_cu);
918dd910 2837 else
58f0c718 2838 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2839
cc12ce38
DE
2840 if (per_cu->cu == NULL)
2841 return; /* Dummy CU. */
2dc860c0
DE
2842
2843 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2844}
2845
a0f42c21 2846/* Read in the symbols for PER_CU. */
2fdf6df6 2847
9291a0cd 2848static void
58f0c718 2849dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2850{
ed2dc618 2851 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2852
f4dc4d17
DE
2853 /* Skip type_unit_groups, reading the type units they contain
2854 is handled elsewhere. */
2855 if (IS_TYPE_UNIT_GROUP (per_cu))
2856 return;
2857
b303c6f6
AB
2858 /* The destructor of dwarf2_queue_guard frees any entries left on
2859 the queue. After this point we're guaranteed to leave this function
2860 with the dwarf queue empty. */
2861 dwarf2_queue_guard q_guard;
9291a0cd 2862
95554aad 2863 if (dwarf2_per_objfile->using_index
43f3e411 2864 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2865 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2866 {
2867 queue_comp_unit (per_cu, language_minimal);
58f0c718 2868 load_cu (per_cu, skip_partial);
89e63ee4
DE
2869
2870 /* If we just loaded a CU from a DWO, and we're working with an index
2871 that may badly handle TUs, load all the TUs in that DWO as well.
2872 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2873 if (!per_cu->is_debug_types
cc12ce38 2874 && per_cu->cu != NULL
89e63ee4
DE
2875 && per_cu->cu->dwo_unit != NULL
2876 && dwarf2_per_objfile->index_table != NULL
2877 && dwarf2_per_objfile->index_table->version <= 7
2878 /* DWP files aren't supported yet. */
ed2dc618 2879 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2880 queue_and_load_all_dwo_tus (per_cu);
95554aad 2881 }
9291a0cd 2882
ed2dc618 2883 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2884
2885 /* Age the cache, releasing compilation units that have not
2886 been used recently. */
ed2dc618 2887 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2888}
2889
2890/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2891 the objfile from which this CU came. Returns the resulting symbol
2892 table. */
2fdf6df6 2893
43f3e411 2894static struct compunit_symtab *
58f0c718 2895dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2896{
ed2dc618
SM
2897 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2898
95554aad 2899 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2900 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2901 {
11ed8cad 2902 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2903 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2904 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2905 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2906 }
f194fefb 2907
43f3e411 2908 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2909}
2910
ff4c9fec 2911/* See declaration. */
f4dc4d17 2912
ff4c9fec
SM
2913dwarf2_per_cu_data *
2914dwarf2_per_objfile::get_cutu (int index)
2915{
b76e467d 2916 if (index >= this->all_comp_units.size ())
ff4c9fec 2917 {
b76e467d 2918 index -= this->all_comp_units.size ();
b2bdb8cf 2919 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2920 return &this->all_type_units[index]->per_cu;
2921 }
f4dc4d17 2922
ff4c9fec
SM
2923 return this->all_comp_units[index];
2924}
f4dc4d17 2925
ff4c9fec 2926/* See declaration. */
2fdf6df6 2927
ff4c9fec
SM
2928dwarf2_per_cu_data *
2929dwarf2_per_objfile::get_cu (int index)
1fd400ff 2930{
b76e467d 2931 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2932
ff4c9fec 2933 return this->all_comp_units[index];
f4dc4d17
DE
2934}
2935
ff4c9fec 2936/* See declaration. */
f4dc4d17 2937
ff4c9fec
SM
2938signatured_type *
2939dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2940{
b2bdb8cf 2941 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2942
ff4c9fec 2943 return this->all_type_units[index];
1fd400ff
TT
2944}
2945
4b514bc8
JK
2946/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2947 objfile_obstack, and constructed with the specified field
2948 values. */
2949
2950static dwarf2_per_cu_data *
ed2dc618 2951create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2952 struct dwarf2_section_info *section,
2953 int is_dwz,
2954 sect_offset sect_off, ULONGEST length)
2955{
ed2dc618 2956 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2957 dwarf2_per_cu_data *the_cu
2958 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2959 struct dwarf2_per_cu_data);
2960 the_cu->sect_off = sect_off;
2961 the_cu->length = length;
e3b94546 2962 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2963 the_cu->section = section;
2964 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2965 struct dwarf2_per_cu_quick_data);
2966 the_cu->is_dwz = is_dwz;
2967 return the_cu;
2968}
2969
2ec9a5e0
TT
2970/* A helper for create_cus_from_index that handles a given list of
2971 CUs. */
2fdf6df6 2972
74a0d9f6 2973static void
12359b5e 2974create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2975 const gdb_byte *cu_list, offset_type n_elements,
2976 struct dwarf2_section_info *section,
b76e467d 2977 int is_dwz)
9291a0cd 2978{
12359b5e 2979 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2980 {
74a0d9f6 2981 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2982
2983 sect_offset sect_off
2984 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2985 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2986 cu_list += 2 * 8;
2987
b76e467d 2988 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2989 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2990 sect_off, length);
b76e467d 2991 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2992 }
9291a0cd
TT
2993}
2994
2ec9a5e0 2995/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2996 the CU objects for this objfile. */
2ec9a5e0 2997
74a0d9f6 2998static void
12359b5e 2999create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3000 const gdb_byte *cu_list, offset_type cu_list_elements,
3001 const gdb_byte *dwz_list, offset_type dwz_elements)
3002{
b76e467d
SM
3003 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3004 dwarf2_per_objfile->all_comp_units.reserve
3005 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3006
12359b5e 3007 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3008 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3009
3010 if (dwz_elements == 0)
74a0d9f6 3011 return;
2ec9a5e0 3012
12359b5e
SM
3013 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3014 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3015 &dwz->info, 1);
2ec9a5e0
TT
3016}
3017
1fd400ff 3018/* Create the signatured type hash table from the index. */
673bfd45 3019
74a0d9f6 3020static void
12359b5e
SM
3021create_signatured_type_table_from_index
3022 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3023 struct dwarf2_section_info *section,
3024 const gdb_byte *bytes,
3025 offset_type elements)
1fd400ff 3026{
12359b5e 3027 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3028
b2bdb8cf
SM
3029 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3030 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3031
12359b5e 3032 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3033
12359b5e 3034 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3035 {
52dc124a 3036 struct signatured_type *sig_type;
9c541725 3037 ULONGEST signature;
1fd400ff 3038 void **slot;
9c541725 3039 cu_offset type_offset_in_tu;
1fd400ff 3040
74a0d9f6 3041 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3042 sect_offset sect_off
3043 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3044 type_offset_in_tu
3045 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3046 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3047 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3048 bytes += 3 * 8;
3049
52dc124a 3050 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3051 struct signatured_type);
52dc124a 3052 sig_type->signature = signature;
9c541725 3053 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3054 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3055 sig_type->per_cu.section = section;
9c541725 3056 sig_type->per_cu.sect_off = sect_off;
e3b94546 3057 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3058 sig_type->per_cu.v.quick
1fd400ff
TT
3059 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3060 struct dwarf2_per_cu_quick_data);
3061
52dc124a
DE
3062 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3063 *slot = sig_type;
1fd400ff 3064
b2bdb8cf 3065 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3066 }
3067
673bfd45 3068 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3069}
3070
927aa2e7
JK
3071/* Create the signatured type hash table from .debug_names. */
3072
3073static void
3074create_signatured_type_table_from_debug_names
ed2dc618 3075 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3076 const mapped_debug_names &map,
3077 struct dwarf2_section_info *section,
3078 struct dwarf2_section_info *abbrev_section)
3079{
ed2dc618
SM
3080 struct objfile *objfile = dwarf2_per_objfile->objfile;
3081
927aa2e7
JK
3082 dwarf2_read_section (objfile, section);
3083 dwarf2_read_section (objfile, abbrev_section);
3084
b2bdb8cf
SM
3085 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3086 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3087
3088 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3089
3090 for (uint32_t i = 0; i < map.tu_count; ++i)
3091 {
3092 struct signatured_type *sig_type;
927aa2e7 3093 void **slot;
927aa2e7
JK
3094
3095 sect_offset sect_off
3096 = (sect_offset) (extract_unsigned_integer
3097 (map.tu_table_reordered + i * map.offset_size,
3098 map.offset_size,
3099 map.dwarf5_byte_order));
3100
3101 comp_unit_head cu_header;
ed2dc618
SM
3102 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3103 abbrev_section,
927aa2e7
JK
3104 section->buffer + to_underlying (sect_off),
3105 rcuh_kind::TYPE);
3106
3107 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3108 struct signatured_type);
3109 sig_type->signature = cu_header.signature;
3110 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3111 sig_type->per_cu.is_debug_types = 1;
3112 sig_type->per_cu.section = section;
3113 sig_type->per_cu.sect_off = sect_off;
e3b94546 3114 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3115 sig_type->per_cu.v.quick
3116 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3117 struct dwarf2_per_cu_quick_data);
3118
3119 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3120 *slot = sig_type;
3121
b2bdb8cf 3122 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3123 }
3124
3125 dwarf2_per_objfile->signatured_types = sig_types_hash;
3126}
3127
9291a0cd
TT
3128/* Read the address map data from the mapped index, and use it to
3129 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3130
9291a0cd 3131static void
ed2dc618
SM
3132create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3133 struct mapped_index *index)
9291a0cd 3134{
ed2dc618 3135 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3136 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3137 const gdb_byte *iter, *end;
9291a0cd 3138 struct addrmap *mutable_map;
9291a0cd
TT
3139 CORE_ADDR baseaddr;
3140
8268c778
PA
3141 auto_obstack temp_obstack;
3142
9291a0cd
TT
3143 mutable_map = addrmap_create_mutable (&temp_obstack);
3144
f00a2de2
PA
3145 iter = index->address_table.data ();
3146 end = iter + index->address_table.size ();
9291a0cd
TT
3147
3148 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3149
3150 while (iter < end)
3151 {
3152 ULONGEST hi, lo, cu_index;
3153 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3154 iter += 8;
3155 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3156 iter += 8;
3157 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3158 iter += 4;
f652bce2 3159
24a55014 3160 if (lo > hi)
f652bce2 3161 {
b98664d3 3162 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3163 hex_string (lo), hex_string (hi));
24a55014 3164 continue;
f652bce2 3165 }
24a55014 3166
b76e467d 3167 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3168 {
b98664d3 3169 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3170 (unsigned) cu_index);
24a55014 3171 continue;
f652bce2 3172 }
24a55014 3173
79748972
TT
3174 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3175 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3176 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3177 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3178 }
3179
d320c2b5 3180 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3181 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3182}
3183
927aa2e7
JK
3184/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3185 populate the objfile's psymtabs_addrmap. */
3186
3187static void
ed2dc618 3188create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3189 struct dwarf2_section_info *section)
3190{
ed2dc618 3191 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3192 bfd *abfd = objfile->obfd;
3193 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3194 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3195 SECT_OFF_TEXT (objfile));
3196
3197 auto_obstack temp_obstack;
3198 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3199
3200 std::unordered_map<sect_offset,
3201 dwarf2_per_cu_data *,
3202 gdb::hash_enum<sect_offset>>
3203 debug_info_offset_to_per_cu;
b76e467d 3204 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3205 {
927aa2e7
JK
3206 const auto insertpair
3207 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3208 if (!insertpair.second)
3209 {
3210 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3211 "debug_info_offset %s, ignoring .debug_aranges."),
3212 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3213 return;
3214 }
3215 }
3216
3217 dwarf2_read_section (objfile, section);
3218
3219 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3220
3221 const gdb_byte *addr = section->buffer;
3222
3223 while (addr < section->buffer + section->size)
3224 {
3225 const gdb_byte *const entry_addr = addr;
3226 unsigned int bytes_read;
3227
3228 const LONGEST entry_length = read_initial_length (abfd, addr,
3229 &bytes_read);
3230 addr += bytes_read;
3231
3232 const gdb_byte *const entry_end = addr + entry_length;
3233 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3234 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3235 if (addr + entry_length > section->buffer + section->size)
3236 {
47e3f474 3237 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3238 "length %s exceeds section length %s, "
3239 "ignoring .debug_aranges."),
47e3f474
TV
3240 objfile_name (objfile),
3241 plongest (entry_addr - section->buffer),
927aa2e7
JK
3242 plongest (bytes_read + entry_length),
3243 pulongest (section->size));
3244 return;
3245 }
3246
3247 /* The version number. */
3248 const uint16_t version = read_2_bytes (abfd, addr);
3249 addr += 2;
3250 if (version != 2)
3251 {
47e3f474 3252 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3253 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3254 objfile_name (objfile),
3255 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3256 return;
3257 }
3258
3259 const uint64_t debug_info_offset
3260 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3261 addr += offset_size;
3262 const auto per_cu_it
3263 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3264 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3265 {
47e3f474 3266 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3267 "debug_info_offset %s does not exists, "
3268 "ignoring .debug_aranges."),
47e3f474
TV
3269 objfile_name (objfile),
3270 plongest (entry_addr - section->buffer),
927aa2e7
JK
3271 pulongest (debug_info_offset));
3272 return;
3273 }
3274 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3275
3276 const uint8_t address_size = *addr++;
3277 if (address_size < 1 || address_size > 8)
3278 {
47e3f474 3279 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3280 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3281 objfile_name (objfile),
3282 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3283 return;
3284 }
3285
3286 const uint8_t segment_selector_size = *addr++;
3287 if (segment_selector_size != 0)
3288 {
47e3f474 3289 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3290 "segment_selector_size %u is not supported, "
3291 "ignoring .debug_aranges."),
47e3f474
TV
3292 objfile_name (objfile),
3293 plongest (entry_addr - section->buffer),
927aa2e7
JK
3294 segment_selector_size);
3295 return;
3296 }
3297
3298 /* Must pad to an alignment boundary that is twice the address
3299 size. It is undocumented by the DWARF standard but GCC does
3300 use it. */
3301 for (size_t padding = ((-(addr - section->buffer))
3302 & (2 * address_size - 1));
3303 padding > 0; padding--)
3304 if (*addr++ != 0)
3305 {
47e3f474 3306 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3307 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3308 objfile_name (objfile),
3309 plongest (entry_addr - section->buffer));
927aa2e7
JK
3310 return;
3311 }
3312
3313 for (;;)
3314 {
3315 if (addr + 2 * address_size > entry_end)
3316 {
47e3f474 3317 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3318 "address list is not properly terminated, "
3319 "ignoring .debug_aranges."),
47e3f474
TV
3320 objfile_name (objfile),
3321 plongest (entry_addr - section->buffer));
927aa2e7
JK
3322 return;
3323 }
3324 ULONGEST start = extract_unsigned_integer (addr, address_size,
3325 dwarf5_byte_order);
3326 addr += address_size;
3327 ULONGEST length = extract_unsigned_integer (addr, address_size,
3328 dwarf5_byte_order);
3329 addr += address_size;
3330 if (start == 0 && length == 0)
3331 break;
3332 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3333 {
3334 /* Symbol was eliminated due to a COMDAT group. */
3335 continue;
3336 }
3337 ULONGEST end = start + length;
79748972
TT
3338 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3339 - baseaddr);
3340 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3341 - baseaddr);
927aa2e7
JK
3342 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3343 }
3344 }
3345
d320c2b5 3346 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3347 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3348}
3349
9291a0cd
TT
3350/* Find a slot in the mapped index INDEX for the object named NAME.
3351 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3352 constant pool and return true. If NAME cannot be found, return
3353 false. */
2fdf6df6 3354
109483d9 3355static bool
9291a0cd
TT
3356find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3357 offset_type **vec_out)
3358{
0cf03b49 3359 offset_type hash;
9291a0cd 3360 offset_type slot, step;
559a7a62 3361 int (*cmp) (const char *, const char *);
9291a0cd 3362
791afaa2 3363 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3364 if (current_language->la_language == language_cplus
45280282
IB
3365 || current_language->la_language == language_fortran
3366 || current_language->la_language == language_d)
0cf03b49
JK
3367 {
3368 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3369 not contain any. */
a8719064 3370
72998fb3 3371 if (strchr (name, '(') != NULL)
0cf03b49 3372 {
109483d9 3373 without_params = cp_remove_params (name);
0cf03b49 3374
72998fb3 3375 if (without_params != NULL)
791afaa2 3376 name = without_params.get ();
0cf03b49
JK
3377 }
3378 }
3379
559a7a62 3380 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3381 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3382 simulate our NAME being searched is also lowercased. */
3383 hash = mapped_index_string_hash ((index->version == 4
3384 && case_sensitivity == case_sensitive_off
3385 ? 5 : index->version),
3386 name);
3387
f00a2de2
PA
3388 slot = hash & (index->symbol_table.size () - 1);
3389 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3390 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3391
3392 for (;;)
3393 {
9291a0cd 3394 const char *str;
f00a2de2
PA
3395
3396 const auto &bucket = index->symbol_table[slot];
3397 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3398 return false;
9291a0cd 3399
f00a2de2 3400 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3401 if (!cmp (name, str))
9291a0cd
TT
3402 {
3403 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3404 + MAYBE_SWAP (bucket.vec));
109483d9 3405 return true;
9291a0cd
TT
3406 }
3407
f00a2de2 3408 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3409 }
3410}
3411
4485a1c1
SM
3412/* A helper function that reads the .gdb_index from BUFFER and fills
3413 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3414 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3415 ok to use deprecated sections.
3416
3417 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3418 out parameters that are filled in with information about the CU and
3419 TU lists in the section.
3420
4485a1c1 3421 Returns true if all went well, false otherwise. */
2fdf6df6 3422
d33bc52e 3423static bool
4485a1c1
SM
3424read_gdb_index_from_buffer (struct objfile *objfile,
3425 const char *filename,
3426 bool deprecated_ok,
3427 gdb::array_view<const gdb_byte> buffer,
3428 struct mapped_index *map,
3429 const gdb_byte **cu_list,
3430 offset_type *cu_list_elements,
3431 const gdb_byte **types_list,
3432 offset_type *types_list_elements)
3433{
3434 const gdb_byte *addr = &buffer[0];
82430852 3435
9291a0cd 3436 /* Version check. */
4485a1c1 3437 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3438 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3439 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3440 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3441 indices. */
831adc1f 3442 if (version < 4)
481860b3
GB
3443 {
3444 static int warning_printed = 0;
3445 if (!warning_printed)
3446 {
3447 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3448 filename);
481860b3
GB
3449 warning_printed = 1;
3450 }
3451 return 0;
3452 }
3453 /* Index version 4 uses a different hash function than index version
3454 5 and later.
3455
3456 Versions earlier than 6 did not emit psymbols for inlined
3457 functions. Using these files will cause GDB not to be able to
3458 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3459 indices unless the user has done
3460 "set use-deprecated-index-sections on". */
2ec9a5e0 3461 if (version < 6 && !deprecated_ok)
481860b3
GB
3462 {
3463 static int warning_printed = 0;
3464 if (!warning_printed)
3465 {
e615022a
DE
3466 warning (_("\
3467Skipping deprecated .gdb_index section in %s.\n\
3468Do \"set use-deprecated-index-sections on\" before the file is read\n\
3469to use the section anyway."),
2ec9a5e0 3470 filename);
481860b3
GB
3471 warning_printed = 1;
3472 }
3473 return 0;
3474 }
796a7ff8 3475 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3476 of the TU (for symbols coming from TUs),
3477 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3478 Plus gold-generated indices can have duplicate entries for global symbols,
3479 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3480 These are just performance bugs, and we can't distinguish gdb-generated
3481 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3482
481860b3 3483 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3484 longer backward compatible. */
796a7ff8 3485 if (version > 8)
594e8718 3486 return 0;
9291a0cd 3487
559a7a62 3488 map->version = version;
9291a0cd 3489
4485a1c1 3490 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3491
4485a1c1 3492 int i = 0;
2ec9a5e0
TT
3493 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3494 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3495 / 8);
1fd400ff
TT
3496 ++i;
3497
2ec9a5e0
TT
3498 *types_list = addr + MAYBE_SWAP (metadata[i]);
3499 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3500 - MAYBE_SWAP (metadata[i]))
3501 / 8);
987d643c 3502 ++i;
1fd400ff 3503
f00a2de2
PA
3504 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3505 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3506 map->address_table
3507 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3508 ++i;
3509
f00a2de2
PA
3510 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3511 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3512 map->symbol_table
3513 = gdb::array_view<mapped_index::symbol_table_slot>
3514 ((mapped_index::symbol_table_slot *) symbol_table,
3515 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3516
f00a2de2 3517 ++i;
f9d83a0b 3518 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3519
2ec9a5e0
TT
3520 return 1;
3521}
3522
4485a1c1
SM
3523/* Callback types for dwarf2_read_gdb_index. */
3524
3525typedef gdb::function_view
3526 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3527 get_gdb_index_contents_ftype;
3528typedef gdb::function_view
3529 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3530 get_gdb_index_contents_dwz_ftype;
3531
927aa2e7 3532/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3533 elements of all the CUs and return 1. Otherwise, return 0. */
3534
3535static int
4485a1c1
SM
3536dwarf2_read_gdb_index
3537 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3538 get_gdb_index_contents_ftype get_gdb_index_contents,
3539 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3540{
2ec9a5e0
TT
3541 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3542 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3543 struct dwz_file *dwz;
12359b5e 3544 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3545
4485a1c1
SM
3546 gdb::array_view<const gdb_byte> main_index_contents
3547 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3548
3549 if (main_index_contents.empty ())
3550 return 0;
3551
3063847f 3552 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3553 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3554 use_deprecated_index_sections,
3555 main_index_contents, map.get (), &cu_list,
3556 &cu_list_elements, &types_list,
3557 &types_list_elements))
2ec9a5e0
TT
3558 return 0;
3559
0fefef59 3560 /* Don't use the index if it's empty. */
3063847f 3561 if (map->symbol_table.empty ())
0fefef59
DE
3562 return 0;
3563
2ec9a5e0
TT
3564 /* If there is a .dwz file, read it so we can get its CU list as
3565 well. */
ed2dc618 3566 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3567 if (dwz != NULL)
2ec9a5e0 3568 {
2ec9a5e0
TT
3569 struct mapped_index dwz_map;
3570 const gdb_byte *dwz_types_ignore;
3571 offset_type dwz_types_elements_ignore;
3572
4485a1c1
SM
3573 gdb::array_view<const gdb_byte> dwz_index_content
3574 = get_gdb_index_contents_dwz (objfile, dwz);
3575
3576 if (dwz_index_content.empty ())
3577 return 0;
3578
3579 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3580 bfd_get_filename (dwz->dwz_bfd.get ()),
3581 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3582 &dwz_list, &dwz_list_elements,
3583 &dwz_types_ignore,
3584 &dwz_types_elements_ignore))
2ec9a5e0
TT
3585 {
3586 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3587 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3588 return 0;
3589 }
3590 }
3591
12359b5e
SM
3592 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3593 dwz_list, dwz_list_elements);
1fd400ff 3594
8b70b953
TT
3595 if (types_list_elements)
3596 {
8b70b953
TT
3597 /* We can only handle a single .debug_types when we have an
3598 index. */
fd5866f6 3599 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3600 return 0;
3601
fd5866f6 3602 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3603
12359b5e
SM
3604 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3605 types_list, types_list_elements);
8b70b953 3606 }
9291a0cd 3607
3063847f 3608 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3609
3063847f 3610 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3611 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3612 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3613 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3614
3615 return 1;
3616}
3617
dee91e82 3618/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3619
dee91e82
DE
3620static void
3621dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3622 const gdb_byte *info_ptr,
dee91e82
DE
3623 struct die_info *comp_unit_die,
3624 int has_children,
3625 void *data)
9291a0cd 3626{
dee91e82 3627 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3628 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3629 struct dwarf2_per_objfile *dwarf2_per_objfile
3630 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3631 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3632 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3633 struct attribute *attr;
dee91e82 3634 int i;
7b9f3c50
DE
3635 void **slot;
3636 struct quick_file_names *qfn;
9291a0cd 3637
0186c6a7
DE
3638 gdb_assert (! this_cu->is_debug_types);
3639
07261596
TT
3640 /* Our callers never want to match partial units -- instead they
3641 will match the enclosing full CU. */
3642 if (comp_unit_die->tag == DW_TAG_partial_unit)
3643 {
3644 this_cu->v.quick->no_file_data = 1;
3645 return;
3646 }
3647
0186c6a7 3648 lh_cu = this_cu;
7b9f3c50 3649 slot = NULL;
dee91e82 3650
fff8551c 3651 line_header_up lh;
9c541725 3652 sect_offset line_offset {};
fff8551c 3653
dee91e82 3654 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3655 if (attr)
3656 {
7b9f3c50
DE
3657 struct quick_file_names find_entry;
3658
9c541725 3659 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3660
3661 /* We may have already read in this line header (TU line header sharing).
3662 If we have we're done. */
094b34ac 3663 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3664 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3665 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3666 &find_entry, INSERT);
3667 if (*slot != NULL)
3668 {
9a3c8263 3669 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3670 return;
7b9f3c50
DE
3671 }
3672
3019eac3 3673 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3674 }
3675 if (lh == NULL)
3676 {
094b34ac 3677 lh_cu->v.quick->no_file_data = 1;
dee91e82 3678 return;
9291a0cd
TT
3679 }
3680
8d749320 3681 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3682 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3683 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3684 gdb_assert (slot != NULL);
3685 *slot = qfn;
9291a0cd 3686
d721ba37 3687 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3688
aa391654
TT
3689 int offset = 0;
3690 if (strcmp (fnd.name, "<unknown>") != 0)
3691 ++offset;
3692
3693 qfn->num_file_names = offset + lh->file_names.size ();
8d749320 3694 qfn->file_names =
aa391654
TT
3695 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3696 if (offset != 0)
3697 qfn->file_names[0] = xstrdup (fnd.name);
fff8551c 3698 for (i = 0; i < lh->file_names.size (); ++i)
aa391654 3699 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3700 qfn->real_names = NULL;
9291a0cd 3701
094b34ac 3702 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3703}
3704
3705/* A helper for the "quick" functions which attempts to read the line
3706 table for THIS_CU. */
3707
3708static struct quick_file_names *
e4a48d9d 3709dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3710{
0186c6a7
DE
3711 /* This should never be called for TUs. */
3712 gdb_assert (! this_cu->is_debug_types);
3713 /* Nor type unit groups. */
3714 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3715
dee91e82
DE
3716 if (this_cu->v.quick->file_names != NULL)
3717 return this_cu->v.quick->file_names;
3718 /* If we know there is no line data, no point in looking again. */
3719 if (this_cu->v.quick->no_file_data)
3720 return NULL;
3721
0186c6a7 3722 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3723
3724 if (this_cu->v.quick->no_file_data)
3725 return NULL;
3726 return this_cu->v.quick->file_names;
9291a0cd
TT
3727}
3728
3729/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3730 real path for a given file name from the line table. */
2fdf6df6 3731
9291a0cd 3732static const char *
7b9f3c50
DE
3733dw2_get_real_path (struct objfile *objfile,
3734 struct quick_file_names *qfn, int index)
9291a0cd 3735{
7b9f3c50
DE
3736 if (qfn->real_names == NULL)
3737 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3738 qfn->num_file_names, const char *);
9291a0cd 3739
7b9f3c50 3740 if (qfn->real_names[index] == NULL)
14278e1f 3741 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3742
7b9f3c50 3743 return qfn->real_names[index];
9291a0cd
TT
3744}
3745
3746static struct symtab *
3747dw2_find_last_source_symtab (struct objfile *objfile)
3748{
ed2dc618
SM
3749 struct dwarf2_per_objfile *dwarf2_per_objfile
3750 = get_dwarf2_per_objfile (objfile);
b76e467d 3751 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3752 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3753
43f3e411
DE
3754 if (cust == NULL)
3755 return NULL;
ed2dc618 3756
43f3e411 3757 return compunit_primary_filetab (cust);
9291a0cd
TT
3758}
3759
7b9f3c50
DE
3760/* Traversal function for dw2_forget_cached_source_info. */
3761
3762static int
3763dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3764{
7b9f3c50 3765 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3766
7b9f3c50 3767 if (file_data->real_names)
9291a0cd 3768 {
7b9f3c50 3769 int i;
9291a0cd 3770
7b9f3c50 3771 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3772 {
7b9f3c50
DE
3773 xfree ((void*) file_data->real_names[i]);
3774 file_data->real_names[i] = NULL;
9291a0cd
TT
3775 }
3776 }
7b9f3c50
DE
3777
3778 return 1;
3779}
3780
3781static void
3782dw2_forget_cached_source_info (struct objfile *objfile)
3783{
ed2dc618
SM
3784 struct dwarf2_per_objfile *dwarf2_per_objfile
3785 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3786
3787 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3788 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3789}
3790
f8eba3c6
TT
3791/* Helper function for dw2_map_symtabs_matching_filename that expands
3792 the symtabs and calls the iterator. */
3793
3794static int
3795dw2_map_expand_apply (struct objfile *objfile,
3796 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3797 const char *name, const char *real_path,
14bc53a8 3798 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3799{
43f3e411 3800 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3801
3802 /* Don't visit already-expanded CUs. */
43f3e411 3803 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3804 return 0;
3805
3806 /* This may expand more than one symtab, and we want to iterate over
3807 all of them. */
58f0c718 3808 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3809
14bc53a8
PA
3810 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3811 last_made, callback);
f8eba3c6
TT
3812}
3813
3814/* Implementation of the map_symtabs_matching_filename method. */
3815
14bc53a8
PA
3816static bool
3817dw2_map_symtabs_matching_filename
3818 (struct objfile *objfile, const char *name, const char *real_path,
3819 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3820{
c011a4f4 3821 const char *name_basename = lbasename (name);
ed2dc618
SM
3822 struct dwarf2_per_objfile *dwarf2_per_objfile
3823 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3824
848e3e78
DE
3825 /* The rule is CUs specify all the files, including those used by
3826 any TU, so there's no need to scan TUs here. */
f4dc4d17 3827
b76e467d 3828 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3829 {
3d7bb9d9 3830 /* We only need to look at symtabs not already expanded. */
43f3e411 3831 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3832 continue;
3833
b76e467d 3834 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3835 if (file_data == NULL)
9291a0cd
TT
3836 continue;
3837
b76e467d 3838 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3839 {
7b9f3c50 3840 const char *this_name = file_data->file_names[j];
da235a7c 3841 const char *this_real_name;
9291a0cd 3842
af529f8f 3843 if (compare_filenames_for_search (this_name, name))
9291a0cd 3844 {
f5b95b50 3845 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3846 callback))
3847 return true;
288e77a7 3848 continue;
4aac40c8 3849 }
9291a0cd 3850
c011a4f4
DE
3851 /* Before we invoke realpath, which can get expensive when many
3852 files are involved, do a quick comparison of the basenames. */
3853 if (! basenames_may_differ
3854 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3855 continue;
3856
da235a7c
JK
3857 this_real_name = dw2_get_real_path (objfile, file_data, j);
3858 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3859 {
da235a7c 3860 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3861 callback))
3862 return true;
288e77a7 3863 continue;
da235a7c 3864 }
9291a0cd 3865
da235a7c
JK
3866 if (real_path != NULL)
3867 {
af529f8f
JK
3868 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3869 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3870 if (this_real_name != NULL
af529f8f 3871 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3872 {
f5b95b50 3873 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3874 callback))
3875 return true;
288e77a7 3876 continue;
9291a0cd
TT
3877 }
3878 }
3879 }
3880 }
3881
14bc53a8 3882 return false;
9291a0cd
TT
3883}
3884
da51c347
DE
3885/* Struct used to manage iterating over all CUs looking for a symbol. */
3886
3887struct dw2_symtab_iterator
9291a0cd 3888{
ed2dc618
SM
3889 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3890 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3891 /* If set, only look for symbols that match that block. Valid values are
3892 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3893 gdb::optional<block_enum> block_index;
da51c347
DE
3894 /* The kind of symbol we're looking for. */
3895 domain_enum domain;
3896 /* The list of CUs from the index entry of the symbol,
3897 or NULL if not found. */
3898 offset_type *vec;
3899 /* The next element in VEC to look at. */
3900 int next;
3901 /* The number of elements in VEC, or zero if there is no match. */
3902 int length;
8943b874
DE
3903 /* Have we seen a global version of the symbol?
3904 If so we can ignore all further global instances.
3905 This is to work around gold/15646, inefficient gold-generated
3906 indices. */
3907 int global_seen;
da51c347 3908};
9291a0cd 3909
2b79f376 3910/* Initialize the index symtab iterator ITER. */
2fdf6df6 3911
9291a0cd 3912static void
da51c347 3913dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3914 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3915 gdb::optional<block_enum> block_index,
da51c347
DE
3916 domain_enum domain,
3917 const char *name)
3918{
ed2dc618 3919 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3920 iter->block_index = block_index;
3921 iter->domain = domain;
3922 iter->next = 0;
8943b874 3923 iter->global_seen = 0;
da51c347 3924
3063847f 3925 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3926
3927 /* index is NULL if OBJF_READNOW. */
3928 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3929 iter->length = MAYBE_SWAP (*iter->vec);
3930 else
3931 {
3932 iter->vec = NULL;
3933 iter->length = 0;
3934 }
3935}
3936
3937/* Return the next matching CU or NULL if there are no more. */
3938
3939static struct dwarf2_per_cu_data *
3940dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3941{
ed2dc618
SM
3942 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3943
da51c347
DE
3944 for ( ; iter->next < iter->length; ++iter->next)
3945 {
3946 offset_type cu_index_and_attrs =
3947 MAYBE_SWAP (iter->vec[iter->next + 1]);
3948 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3949 gdb_index_symbol_kind symbol_kind =
3950 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3951 /* Only check the symbol attributes if they're present.
3952 Indices prior to version 7 don't record them,
3953 and indices >= 7 may elide them for certain symbols
3954 (gold does this). */
3955 int attrs_valid =
ed2dc618 3956 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3957 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3958
3190f0c6 3959 /* Don't crash on bad data. */
b76e467d 3960 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3961 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3962 {
b98664d3 3963 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3964 " [in module %s]"),
3965 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3966 continue;
3967 }
3968
ff4c9fec 3969 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3970
da51c347 3971 /* Skip if already read in. */
43f3e411 3972 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3973 continue;
3974
8943b874
DE
3975 /* Check static vs global. */
3976 if (attrs_valid)
3977 {
2b79f376
SM
3978 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3979
3980 if (iter->block_index.has_value ())
3981 {
3982 bool want_static = *iter->block_index == STATIC_BLOCK;
3983
3984 if (is_static != want_static)
3985 continue;
3986 }
3987
8943b874
DE
3988 /* Work around gold/15646. */
3989 if (!is_static && iter->global_seen)
3990 continue;
3991 if (!is_static)
3992 iter->global_seen = 1;
3993 }
da51c347
DE
3994
3995 /* Only check the symbol's kind if it has one. */
3996 if (attrs_valid)
3997 {
3998 switch (iter->domain)
3999 {
4000 case VAR_DOMAIN:
4001 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4002 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4003 /* Some types are also in VAR_DOMAIN. */
4004 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4005 continue;
4006 break;
4007 case STRUCT_DOMAIN:
4008 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4009 continue;
4010 break;
4011 case LABEL_DOMAIN:
4012 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4013 continue;
4014 break;
4015 default:
4016 break;
4017 }
4018 }
4019
4020 ++iter->next;
4021 return per_cu;
4022 }
4023
4024 return NULL;
4025}
4026
43f3e411 4027static struct compunit_symtab *
c7f839cb 4028dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4029 const char *name, domain_enum domain)
9291a0cd 4030{
43f3e411 4031 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4032 struct dwarf2_per_objfile *dwarf2_per_objfile
4033 = get_dwarf2_per_objfile (objfile);
9291a0cd 4034
b5ec771e
PA
4035 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4036
ed2dc618
SM
4037 struct dw2_symtab_iterator iter;
4038 struct dwarf2_per_cu_data *per_cu;
da51c347 4039
2b79f376 4040 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4041
ed2dc618
SM
4042 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4043 {
4044 struct symbol *sym, *with_opaque = NULL;
58f0c718 4045 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4046 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4047 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4048
ed2dc618
SM
4049 sym = block_find_symbol (block, name, domain,
4050 block_find_non_opaque_type_preferred,
4051 &with_opaque);
b2e2f908 4052
ed2dc618
SM
4053 /* Some caution must be observed with overloaded functions
4054 and methods, since the index will not contain any overload
4055 information (but NAME might contain it). */
da51c347 4056
ed2dc618
SM
4057 if (sym != NULL
4058 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4059 return stab;
4060 if (with_opaque != NULL
4061 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4062 stab_best = stab;
da51c347 4063
ed2dc618 4064 /* Keep looking through other CUs. */
9291a0cd 4065 }
9291a0cd 4066
da51c347 4067 return stab_best;
9291a0cd
TT
4068}
4069
4070static void
4071dw2_print_stats (struct objfile *objfile)
4072{
ed2dc618
SM
4073 struct dwarf2_per_objfile *dwarf2_per_objfile
4074 = get_dwarf2_per_objfile (objfile);
b76e467d 4075 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4076 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4077 int count = 0;
9291a0cd 4078
ed2dc618 4079 for (int i = 0; i < total; ++i)
9291a0cd 4080 {
ff4c9fec 4081 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4082
43f3e411 4083 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4084 ++count;
4085 }
e4a48d9d 4086 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4087 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4088}
4089
779bd270
DE
4090/* This dumps minimal information about the index.
4091 It is called via "mt print objfiles".
4092 One use is to verify .gdb_index has been loaded by the
4093 gdb.dwarf2/gdb-index.exp testcase. */
4094
9291a0cd
TT
4095static void
4096dw2_dump (struct objfile *objfile)
4097{
ed2dc618
SM
4098 struct dwarf2_per_objfile *dwarf2_per_objfile
4099 = get_dwarf2_per_objfile (objfile);
4100
779bd270
DE
4101 gdb_assert (dwarf2_per_objfile->using_index);
4102 printf_filtered (".gdb_index:");
4103 if (dwarf2_per_objfile->index_table != NULL)
4104 {
4105 printf_filtered (" version %d\n",
4106 dwarf2_per_objfile->index_table->version);
4107 }
4108 else
4109 printf_filtered (" faked for \"readnow\"\n");
4110 printf_filtered ("\n");
9291a0cd
TT
4111}
4112
9291a0cd
TT
4113static void
4114dw2_expand_symtabs_for_function (struct objfile *objfile,
4115 const char *func_name)
4116{
ed2dc618
SM
4117 struct dwarf2_per_objfile *dwarf2_per_objfile
4118 = get_dwarf2_per_objfile (objfile);
da51c347 4119
ed2dc618
SM
4120 struct dw2_symtab_iterator iter;
4121 struct dwarf2_per_cu_data *per_cu;
da51c347 4122
2b79f376 4123 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4124
ed2dc618 4125 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4126 dw2_instantiate_symtab (per_cu, false);
da51c347 4127
9291a0cd
TT
4128}
4129
4130static void
4131dw2_expand_all_symtabs (struct objfile *objfile)
4132{
ed2dc618
SM
4133 struct dwarf2_per_objfile *dwarf2_per_objfile
4134 = get_dwarf2_per_objfile (objfile);
b76e467d 4135 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4136 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4137
ed2dc618 4138 for (int i = 0; i < total_units; ++i)
9291a0cd 4139 {
ff4c9fec 4140 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4141
58f0c718
TT
4142 /* We don't want to directly expand a partial CU, because if we
4143 read it with the wrong language, then assertion failures can
4144 be triggered later on. See PR symtab/23010. So, tell
4145 dw2_instantiate_symtab to skip partial CUs -- any important
4146 partial CU will be read via DW_TAG_imported_unit anyway. */
4147 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4148 }
4149}
4150
4151static void
652a8996
JK
4152dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4153 const char *fullname)
9291a0cd 4154{
ed2dc618
SM
4155 struct dwarf2_per_objfile *dwarf2_per_objfile
4156 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4157
4158 /* We don't need to consider type units here.
4159 This is only called for examining code, e.g. expand_line_sal.
4160 There can be an order of magnitude (or more) more type units
4161 than comp units, and we avoid them if we can. */
4162
b76e467d 4163 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4164 {
3d7bb9d9 4165 /* We only need to look at symtabs not already expanded. */
43f3e411 4166 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4167 continue;
4168
b76e467d 4169 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4170 if (file_data == NULL)
9291a0cd
TT
4171 continue;
4172
b76e467d 4173 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4174 {
652a8996
JK
4175 const char *this_fullname = file_data->file_names[j];
4176
4177 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4178 {
58f0c718 4179 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4180 break;
4181 }
4182 }
4183 }
4184}
4185
9291a0cd 4186static void
199b4314
TT
4187dw2_map_matching_symbols
4188 (struct objfile *objfile,
b054970d 4189 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4190 int global,
4191 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4192 symbol_compare_ftype *ordered_compare)
9291a0cd 4193{
40658b94 4194 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4195 current language is Ada for a non-Ada objfile using GNU index. As Ada
4196 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4197}
4198
e1ef7d7a
PA
4199/* Starting from a search name, return the string that finds the upper
4200 bound of all strings that start with SEARCH_NAME in a sorted name
4201 list. Returns the empty string to indicate that the upper bound is
4202 the end of the list. */
4203
4204static std::string
4205make_sort_after_prefix_name (const char *search_name)
4206{
4207 /* When looking to complete "func", we find the upper bound of all
4208 symbols that start with "func" by looking for where we'd insert
4209 the closest string that would follow "func" in lexicographical
4210 order. Usually, that's "func"-with-last-character-incremented,
4211 i.e. "fund". Mind non-ASCII characters, though. Usually those
4212 will be UTF-8 multi-byte sequences, but we can't be certain.
4213 Especially mind the 0xff character, which is a valid character in
4214 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4215 rule out compilers allowing it in identifiers. Note that
4216 conveniently, strcmp/strcasecmp are specified to compare
4217 characters interpreted as unsigned char. So what we do is treat
4218 the whole string as a base 256 number composed of a sequence of
4219 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4220 to 0, and carries 1 to the following more-significant position.
4221 If the very first character in SEARCH_NAME ends up incremented
4222 and carries/overflows, then the upper bound is the end of the
4223 list. The string after the empty string is also the empty
4224 string.
4225
4226 Some examples of this operation:
4227
4228 SEARCH_NAME => "+1" RESULT
4229
4230 "abc" => "abd"
4231 "ab\xff" => "ac"
4232 "\xff" "a" "\xff" => "\xff" "b"
4233 "\xff" => ""
4234 "\xff\xff" => ""
4235 "" => ""
4236
4237 Then, with these symbols for example:
4238
4239 func
4240 func1
4241 fund
4242
4243 completing "func" looks for symbols between "func" and
4244 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4245 which finds "func" and "func1", but not "fund".
4246
4247 And with:
4248
4249 funcÿ (Latin1 'ÿ' [0xff])
4250 funcÿ1
4251 fund
4252
4253 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4254 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4255
4256 And with:
4257
4258 ÿÿ (Latin1 'ÿ' [0xff])
4259 ÿÿ1
4260
4261 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4262 the end of the list.
4263 */
4264 std::string after = search_name;
4265 while (!after.empty () && (unsigned char) after.back () == 0xff)
4266 after.pop_back ();
4267 if (!after.empty ())
4268 after.back () = (unsigned char) after.back () + 1;
4269 return after;
4270}
4271
5c58de74 4272/* See declaration. */
61d96d7e 4273
5c58de74
PA
4274std::pair<std::vector<name_component>::const_iterator,
4275 std::vector<name_component>::const_iterator>
44ed8f3e 4276mapped_index_base::find_name_components_bounds
3b00ef10 4277 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4278{
5c58de74
PA
4279 auto *name_cmp
4280 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4281
3b00ef10
TT
4282 const char *lang_name
4283 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4284
3f563c84
PA
4285 /* Comparison function object for lower_bound that matches against a
4286 given symbol name. */
4287 auto lookup_compare_lower = [&] (const name_component &elem,
4288 const char *name)
4289 {
5c58de74 4290 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4291 const char *elem_name = elem_qualified + elem.name_offset;
4292 return name_cmp (elem_name, name) < 0;
4293 };
4294
4295 /* Comparison function object for upper_bound that matches against a
4296 given symbol name. */
4297 auto lookup_compare_upper = [&] (const char *name,
4298 const name_component &elem)
4299 {
5c58de74 4300 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4301 const char *elem_name = elem_qualified + elem.name_offset;
4302 return name_cmp (name, elem_name) < 0;
4303 };
4304
5c58de74
PA
4305 auto begin = this->name_components.begin ();
4306 auto end = this->name_components.end ();
3f563c84
PA
4307
4308 /* Find the lower bound. */
4309 auto lower = [&] ()
4310 {
3b00ef10 4311 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4312 return begin;
4313 else
3b00ef10 4314 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4315 } ();
4316
4317 /* Find the upper bound. */
4318 auto upper = [&] ()
4319 {
5c58de74 4320 if (lookup_name_without_params.completion_mode ())
3f563c84 4321 {
e1ef7d7a
PA
4322 /* In completion mode, we want UPPER to point past all
4323 symbols names that have the same prefix. I.e., with
4324 these symbols, and completing "func":
4325
4326 function << lower bound
4327 function1
4328 other_function << upper bound
4329
4330 We find the upper bound by looking for the insertion
4331 point of "func"-with-last-character-incremented,
4332 i.e. "fund". */
3b00ef10 4333 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4334 if (after.empty ())
3f563c84 4335 return end;
e6b2f5ef
PA
4336 return std::lower_bound (lower, end, after.c_str (),
4337 lookup_compare_lower);
3f563c84
PA
4338 }
4339 else
3b00ef10 4340 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4341 } ();
4342
5c58de74
PA
4343 return {lower, upper};
4344}
4345
4346/* See declaration. */
4347
4348void
44ed8f3e 4349mapped_index_base::build_name_components ()
5c58de74
PA
4350{
4351 if (!this->name_components.empty ())
4352 return;
4353
4354 this->name_components_casing = case_sensitivity;
4355 auto *name_cmp
4356 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4357
4358 /* The code below only knows how to break apart components of C++
4359 symbol names (and other languages that use '::' as
3b00ef10 4360 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4361 auto count = this->symbol_name_count ();
4362 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4363 {
44ed8f3e 4364 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4365 continue;
4366
4367 const char *name = this->symbol_name_at (idx);
4368
4369 /* Add each name component to the name component table. */
4370 unsigned int previous_len = 0;
3b00ef10
TT
4371
4372 if (strstr (name, "::") != nullptr)
4373 {
4374 for (unsigned int current_len = cp_find_first_component (name);
4375 name[current_len] != '\0';
4376 current_len += cp_find_first_component (name + current_len))
4377 {
4378 gdb_assert (name[current_len] == ':');
4379 this->name_components.push_back ({previous_len, idx});
4380 /* Skip the '::'. */
4381 current_len += 2;
4382 previous_len = current_len;
4383 }
4384 }
4385 else
5c58de74 4386 {
3b00ef10
TT
4387 /* Handle the Ada encoded (aka mangled) form here. */
4388 for (const char *iter = strstr (name, "__");
4389 iter != nullptr;
4390 iter = strstr (iter, "__"))
4391 {
4392 this->name_components.push_back ({previous_len, idx});
4393 iter += 2;
4394 previous_len = iter - name;
4395 }
5c58de74 4396 }
3b00ef10 4397
5c58de74
PA
4398 this->name_components.push_back ({previous_len, idx});
4399 }
4400
4401 /* Sort name_components elements by name. */
4402 auto name_comp_compare = [&] (const name_component &left,
4403 const name_component &right)
4404 {
4405 const char *left_qualified = this->symbol_name_at (left.idx);
4406 const char *right_qualified = this->symbol_name_at (right.idx);
4407
4408 const char *left_name = left_qualified + left.name_offset;
4409 const char *right_name = right_qualified + right.name_offset;
4410
4411 return name_cmp (left_name, right_name) < 0;
4412 };
4413
4414 std::sort (this->name_components.begin (),
4415 this->name_components.end (),
4416 name_comp_compare);
4417}
4418
4419/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4420 mapped_index_base instead of the containing objfile. This is split
4421 to a separate function in order to be able to unit test the
4422 name_components matching using a mock mapped_index_base. For each
5c58de74 4423 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4424 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4425
4426static void
4427dw2_expand_symtabs_matching_symbol
44ed8f3e 4428 (mapped_index_base &index,
5c58de74
PA
4429 const lookup_name_info &lookup_name_in,
4430 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4431 enum search_domain kind,
3b00ef10 4432 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4433{
4434 lookup_name_info lookup_name_without_params
4435 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4436
4437 /* Build the symbol name component sorted vector, if we haven't
4438 yet. */
4439 index.build_name_components ();
4440
3f563c84
PA
4441 /* The same symbol may appear more than once in the range though.
4442 E.g., if we're looking for symbols that complete "w", and we have
4443 a symbol named "w1::w2", we'll find the two name components for
4444 that same symbol in the range. To be sure we only call the
4445 callback once per symbol, we first collect the symbol name
4446 indexes that matched in a temporary vector and ignore
4447 duplicates. */
4448 std::vector<offset_type> matches;
3f563c84 4449
3b00ef10
TT
4450 struct name_and_matcher
4451 {
4452 symbol_name_matcher_ftype *matcher;
4453 const std::string &name;
4454
4455 bool operator== (const name_and_matcher &other) const
3f563c84 4456 {
3b00ef10
TT
4457 return matcher == other.matcher && name == other.name;
4458 }
4459 };
4460
4461 /* A vector holding all the different symbol name matchers, for all
4462 languages. */
4463 std::vector<name_and_matcher> matchers;
4464
4465 for (int i = 0; i < nr_languages; i++)
4466 {
4467 enum language lang_e = (enum language) i;
4468
4469 const language_defn *lang = language_def (lang_e);
4470 symbol_name_matcher_ftype *name_matcher
4471 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4472
3b00ef10
TT
4473 name_and_matcher key {
4474 name_matcher,
4475 lookup_name_without_params.language_lookup_name (lang_e)
4476 };
4477
4478 /* Don't insert the same comparison routine more than once.
4479 Note that we do this linear walk. This is not a problem in
4480 practice because the number of supported languages is
4481 low. */
4482 if (std::find (matchers.begin (), matchers.end (), key)
4483 != matchers.end ())
9291a0cd 4484 continue;
3b00ef10
TT
4485 matchers.push_back (std::move (key));
4486
4487 auto bounds
4488 = index.find_name_components_bounds (lookup_name_without_params,
4489 lang_e);
4490
4491 /* Now for each symbol name in range, check to see if we have a name
4492 match, and if so, call the MATCH_CALLBACK callback. */
4493
4494 for (; bounds.first != bounds.second; ++bounds.first)
4495 {
4496 const char *qualified = index.symbol_name_at (bounds.first->idx);
4497
4498 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4499 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4500 continue;
9291a0cd 4501
3b00ef10
TT
4502 matches.push_back (bounds.first->idx);
4503 }
3f563c84
PA
4504 }
4505
4506 std::sort (matches.begin (), matches.end ());
4507
4508 /* Finally call the callback, once per match. */
4509 ULONGEST prev = -1;
4510 for (offset_type idx : matches)
4511 {
4512 if (prev != idx)
4513 {
3b00ef10
TT
4514 if (!match_callback (idx))
4515 break;
3f563c84
PA
4516 prev = idx;
4517 }
4518 }
4519
4520 /* Above we use a type wider than idx's for 'prev', since 0 and
4521 (offset_type)-1 are both possible values. */
4522 static_assert (sizeof (prev) > sizeof (offset_type), "");
4523}
4524
c62446b1
PA
4525#if GDB_SELF_TEST
4526
4527namespace selftests { namespace dw2_expand_symtabs_matching {
4528
a3c5fafd
PA
4529/* A mock .gdb_index/.debug_names-like name index table, enough to
4530 exercise dw2_expand_symtabs_matching_symbol, which works with the
4531 mapped_index_base interface. Builds an index from the symbol list
4532 passed as parameter to the constructor. */
4533class mock_mapped_index : public mapped_index_base
c62446b1
PA
4534{
4535public:
a3c5fafd
PA
4536 mock_mapped_index (gdb::array_view<const char *> symbols)
4537 : m_symbol_table (symbols)
c62446b1
PA
4538 {}
4539
a3c5fafd 4540 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4541
a3c5fafd 4542 /* Return the number of names in the symbol table. */
632e107b 4543 size_t symbol_name_count () const override
c62446b1 4544 {
a3c5fafd 4545 return m_symbol_table.size ();
c62446b1
PA
4546 }
4547
a3c5fafd 4548 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4549 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4550 {
4551 return m_symbol_table[idx];
4552 }
c62446b1 4553
a3c5fafd
PA
4554private:
4555 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4556};
4557
4558/* Convenience function that converts a NULL pointer to a "<null>"
4559 string, to pass to print routines. */
4560
4561static const char *
4562string_or_null (const char *str)
4563{
4564 return str != NULL ? str : "<null>";
4565}
4566
4567/* Check if a lookup_name_info built from
4568 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4569 index. EXPECTED_LIST is the list of expected matches, in expected
4570 matching order. If no match expected, then an empty list is
4571 specified. Returns true on success. On failure prints a warning
4572 indicating the file:line that failed, and returns false. */
4573
4574static bool
4575check_match (const char *file, int line,
4576 mock_mapped_index &mock_index,
4577 const char *name, symbol_name_match_type match_type,
4578 bool completion_mode,
4579 std::initializer_list<const char *> expected_list)
4580{
4581 lookup_name_info lookup_name (name, match_type, completion_mode);
4582
4583 bool matched = true;
4584
4585 auto mismatch = [&] (const char *expected_str,
4586 const char *got)
4587 {
4588 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4589 "expected=\"%s\", got=\"%s\"\n"),
4590 file, line,
4591 (match_type == symbol_name_match_type::FULL
4592 ? "FULL" : "WILD"),
4593 name, string_or_null (expected_str), string_or_null (got));
4594 matched = false;
4595 };
4596
4597 auto expected_it = expected_list.begin ();
4598 auto expected_end = expected_list.end ();
4599
a3c5fafd 4600 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4601 NULL, ALL_DOMAIN,
4602 [&] (offset_type idx)
4603 {
a3c5fafd 4604 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4605 const char *expected_str
4606 = expected_it == expected_end ? NULL : *expected_it++;
4607
4608 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4609 mismatch (expected_str, matched_name);
3b00ef10 4610 return true;
c62446b1
PA
4611 });
4612
4613 const char *expected_str
4614 = expected_it == expected_end ? NULL : *expected_it++;
4615 if (expected_str != NULL)
4616 mismatch (expected_str, NULL);
4617
4618 return matched;
4619}
4620
4621/* The symbols added to the mock mapped_index for testing (in
4622 canonical form). */
4623static const char *test_symbols[] = {
4624 "function",
4625 "std::bar",
4626 "std::zfunction",
4627 "std::zfunction2",
4628 "w1::w2",
4629 "ns::foo<char*>",
4630 "ns::foo<int>",
4631 "ns::foo<long>",
a20714ff
PA
4632 "ns2::tmpl<int>::foo2",
4633 "(anonymous namespace)::A::B::C",
c62446b1 4634
e1ef7d7a
PA
4635 /* These are used to check that the increment-last-char in the
4636 matching algorithm for completion doesn't match "t1_fund" when
4637 completing "t1_func". */
4638 "t1_func",
4639 "t1_func1",
4640 "t1_fund",
4641 "t1_fund1",
4642
4643 /* A UTF-8 name with multi-byte sequences to make sure that
4644 cp-name-parser understands this as a single identifier ("função"
4645 is "function" in PT). */
4646 u8"u8função",
4647
4648 /* \377 (0xff) is Latin1 'ÿ'. */
4649 "yfunc\377",
4650
4651 /* \377 (0xff) is Latin1 'ÿ'. */
4652 "\377",
4653 "\377\377123",
4654
c62446b1
PA
4655 /* A name with all sorts of complications. Starts with "z" to make
4656 it easier for the completion tests below. */
4657#define Z_SYM_NAME \
4658 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4659 "::tuple<(anonymous namespace)::ui*, " \
4660 "std::default_delete<(anonymous namespace)::ui>, void>"
4661
4662 Z_SYM_NAME
4663};
4664
a3c5fafd
PA
4665/* Returns true if the mapped_index_base::find_name_component_bounds
4666 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4667 in completion mode. */
5c58de74
PA
4668
4669static bool
a3c5fafd 4670check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4671 const char *search_name,
4672 gdb::array_view<const char *> expected_syms)
4673{
4674 lookup_name_info lookup_name (search_name,
4675 symbol_name_match_type::FULL, true);
4676
3b00ef10
TT
4677 auto bounds = index.find_name_components_bounds (lookup_name,
4678 language_cplus);
5c58de74
PA
4679
4680 size_t distance = std::distance (bounds.first, bounds.second);
4681 if (distance != expected_syms.size ())
4682 return false;
4683
4684 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4685 {
4686 auto nc_elem = bounds.first + exp_elem;
4687 const char *qualified = index.symbol_name_at (nc_elem->idx);
4688 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4689 return false;
4690 }
4691
4692 return true;
4693}
4694
4695/* Test the lower-level mapped_index::find_name_component_bounds
4696 method. */
4697
c62446b1 4698static void
5c58de74
PA
4699test_mapped_index_find_name_component_bounds ()
4700{
4701 mock_mapped_index mock_index (test_symbols);
4702
a3c5fafd 4703 mock_index.build_name_components ();
5c58de74
PA
4704
4705 /* Test the lower-level mapped_index::find_name_component_bounds
4706 method in completion mode. */
4707 {
4708 static const char *expected_syms[] = {
4709 "t1_func",
4710 "t1_func1",
5c58de74
PA
4711 };
4712
a3c5fafd 4713 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4714 "t1_func", expected_syms));
4715 }
4716
4717 /* Check that the increment-last-char in the name matching algorithm
4718 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4719 {
4720 static const char *expected_syms1[] = {
4721 "\377",
4722 "\377\377123",
4723 };
a3c5fafd 4724 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4725 "\377", expected_syms1));
4726
4727 static const char *expected_syms2[] = {
4728 "\377\377123",
4729 };
a3c5fafd 4730 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4731 "\377\377", expected_syms2));
4732 }
4733}
4734
4735/* Test dw2_expand_symtabs_matching_symbol. */
4736
4737static void
4738test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4739{
4740 mock_mapped_index mock_index (test_symbols);
4741
4742 /* We let all tests run until the end even if some fails, for debug
4743 convenience. */
4744 bool any_mismatch = false;
4745
4746 /* Create the expected symbols list (an initializer_list). Needed
4747 because lists have commas, and we need to pass them to CHECK,
4748 which is a macro. */
4749#define EXPECT(...) { __VA_ARGS__ }
4750
4751 /* Wrapper for check_match that passes down the current
4752 __FILE__/__LINE__. */
4753#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4754 any_mismatch |= !check_match (__FILE__, __LINE__, \
4755 mock_index, \
4756 NAME, MATCH_TYPE, COMPLETION_MODE, \
4757 EXPECTED_LIST)
4758
4759 /* Identity checks. */
4760 for (const char *sym : test_symbols)
4761 {
4762 /* Should be able to match all existing symbols. */
4763 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4764 EXPECT (sym));
4765
4766 /* Should be able to match all existing symbols with
4767 parameters. */
4768 std::string with_params = std::string (sym) + "(int)";
4769 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4770 EXPECT (sym));
4771
4772 /* Should be able to match all existing symbols with
4773 parameters and qualifiers. */
4774 with_params = std::string (sym) + " ( int ) const";
4775 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4776 EXPECT (sym));
4777
4778 /* This should really find sym, but cp-name-parser.y doesn't
4779 know about lvalue/rvalue qualifiers yet. */
4780 with_params = std::string (sym) + " ( int ) &&";
4781 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4782 {});
4783 }
4784
e1ef7d7a
PA
4785 /* Check that the name matching algorithm for completion doesn't get
4786 confused with Latin1 'ÿ' / 0xff. */
4787 {
4788 static const char str[] = "\377";
4789 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4790 EXPECT ("\377", "\377\377123"));
4791 }
4792
4793 /* Check that the increment-last-char in the matching algorithm for
4794 completion doesn't match "t1_fund" when completing "t1_func". */
4795 {
4796 static const char str[] = "t1_func";
4797 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4798 EXPECT ("t1_func", "t1_func1"));
4799 }
4800
c62446b1
PA
4801 /* Check that completion mode works at each prefix of the expected
4802 symbol name. */
4803 {
4804 static const char str[] = "function(int)";
4805 size_t len = strlen (str);
4806 std::string lookup;
4807
4808 for (size_t i = 1; i < len; i++)
4809 {
4810 lookup.assign (str, i);
4811 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4812 EXPECT ("function"));
4813 }
4814 }
4815
4816 /* While "w" is a prefix of both components, the match function
4817 should still only be called once. */
4818 {
4819 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4820 EXPECT ("w1::w2"));
a20714ff
PA
4821 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4822 EXPECT ("w1::w2"));
c62446b1
PA
4823 }
4824
4825 /* Same, with a "complicated" symbol. */
4826 {
4827 static const char str[] = Z_SYM_NAME;
4828 size_t len = strlen (str);
4829 std::string lookup;
4830
4831 for (size_t i = 1; i < len; i++)
4832 {
4833 lookup.assign (str, i);
4834 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4835 EXPECT (Z_SYM_NAME));
4836 }
4837 }
4838
4839 /* In FULL mode, an incomplete symbol doesn't match. */
4840 {
4841 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4842 {});
4843 }
4844
4845 /* A complete symbol with parameters matches any overload, since the
4846 index has no overload info. */
4847 {
4848 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4849 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4850 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4851 EXPECT ("std::zfunction", "std::zfunction2"));
4852 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4853 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4854 }
4855
4856 /* Check that whitespace is ignored appropriately. A symbol with a
4857 template argument list. */
4858 {
4859 static const char expected[] = "ns::foo<int>";
4860 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4861 EXPECT (expected));
a20714ff
PA
4862 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4863 EXPECT (expected));
c62446b1
PA
4864 }
4865
4866 /* Check that whitespace is ignored appropriately. A symbol with a
4867 template argument list that includes a pointer. */
4868 {
4869 static const char expected[] = "ns::foo<char*>";
4870 /* Try both completion and non-completion modes. */
4871 static const bool completion_mode[2] = {false, true};
4872 for (size_t i = 0; i < 2; i++)
4873 {
4874 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4875 completion_mode[i], EXPECT (expected));
a20714ff
PA
4876 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4877 completion_mode[i], EXPECT (expected));
c62446b1
PA
4878
4879 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4880 completion_mode[i], EXPECT (expected));
a20714ff
PA
4881 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4882 completion_mode[i], EXPECT (expected));
c62446b1
PA
4883 }
4884 }
4885
4886 {
4887 /* Check method qualifiers are ignored. */
4888 static const char expected[] = "ns::foo<char*>";
4889 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4890 symbol_name_match_type::FULL, true, EXPECT (expected));
4891 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4892 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4893 CHECK_MATCH ("foo < char * > ( int ) const",
4894 symbol_name_match_type::WILD, true, EXPECT (expected));
4895 CHECK_MATCH ("foo < char * > ( int ) &&",
4896 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4897 }
4898
4899 /* Test lookup names that don't match anything. */
4900 {
a20714ff
PA
4901 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4902 {});
4903
c62446b1
PA
4904 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4905 {});
4906 }
4907
a20714ff
PA
4908 /* Some wild matching tests, exercising "(anonymous namespace)",
4909 which should not be confused with a parameter list. */
4910 {
4911 static const char *syms[] = {
4912 "A::B::C",
4913 "B::C",
4914 "C",
4915 "A :: B :: C ( int )",
4916 "B :: C ( int )",
4917 "C ( int )",
4918 };
4919
4920 for (const char *s : syms)
4921 {
4922 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4923 EXPECT ("(anonymous namespace)::A::B::C"));
4924 }
4925 }
4926
4927 {
4928 static const char expected[] = "ns2::tmpl<int>::foo2";
4929 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4930 EXPECT (expected));
4931 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4932 EXPECT (expected));
4933 }
4934
c62446b1
PA
4935 SELF_CHECK (!any_mismatch);
4936
4937#undef EXPECT
4938#undef CHECK_MATCH
4939}
4940
5c58de74
PA
4941static void
4942run_test ()
4943{
4944 test_mapped_index_find_name_component_bounds ();
4945 test_dw2_expand_symtabs_matching_symbol ();
4946}
4947
c62446b1
PA
4948}} // namespace selftests::dw2_expand_symtabs_matching
4949
4950#endif /* GDB_SELF_TEST */
4951
4b514bc8
JK
4952/* If FILE_MATCHER is NULL or if PER_CU has
4953 dwarf2_per_cu_quick_data::MARK set (see
4954 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4955 EXPANSION_NOTIFY on it. */
4956
4957static void
4958dw2_expand_symtabs_matching_one
4959 (struct dwarf2_per_cu_data *per_cu,
4960 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4961 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4962{
4963 if (file_matcher == NULL || per_cu->v.quick->mark)
4964 {
4965 bool symtab_was_null
4966 = (per_cu->v.quick->compunit_symtab == NULL);
4967
58f0c718 4968 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4969
4970 if (expansion_notify != NULL
4971 && symtab_was_null
4972 && per_cu->v.quick->compunit_symtab != NULL)
4973 expansion_notify (per_cu->v.quick->compunit_symtab);
4974 }
4975}
4976
3f563c84
PA
4977/* Helper for dw2_expand_matching symtabs. Called on each symbol
4978 matched, to expand corresponding CUs that were marked. IDX is the
4979 index of the symbol name that matched. */
4980
4981static void
4982dw2_expand_marked_cus
ed2dc618 4983 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4984 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4985 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4986 search_domain kind)
4987{
3f563c84
PA
4988 offset_type *vec, vec_len, vec_idx;
4989 bool global_seen = false;
ed2dc618 4990 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4991
61920122 4992 vec = (offset_type *) (index.constant_pool
f00a2de2 4993 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4994 vec_len = MAYBE_SWAP (vec[0]);
4995 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4996 {
61920122
PA
4997 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4998 /* This value is only valid for index versions >= 7. */
4999 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5000 gdb_index_symbol_kind symbol_kind =
5001 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5002 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5003 /* Only check the symbol attributes if they're present.
5004 Indices prior to version 7 don't record them,
5005 and indices >= 7 may elide them for certain symbols
5006 (gold does this). */
5007 int attrs_valid =
5008 (index.version >= 7
5009 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5010
5011 /* Work around gold/15646. */
5012 if (attrs_valid)
9291a0cd 5013 {
61920122
PA
5014 if (!is_static && global_seen)
5015 continue;
5016 if (!is_static)
5017 global_seen = true;
5018 }
3190f0c6 5019
61920122
PA
5020 /* Only check the symbol's kind if it has one. */
5021 if (attrs_valid)
5022 {
5023 switch (kind)
8943b874 5024 {
61920122
PA
5025 case VARIABLES_DOMAIN:
5026 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5027 continue;
5028 break;
5029 case FUNCTIONS_DOMAIN:
5030 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5031 continue;
61920122
PA
5032 break;
5033 case TYPES_DOMAIN:
5034 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5035 continue;
5036 break;
5037 default:
5038 break;
8943b874 5039 }
61920122 5040 }
8943b874 5041
61920122 5042 /* Don't crash on bad data. */
b76e467d 5043 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5044 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5045 {
b98664d3 5046 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5047 " [in module %s]"),
5048 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5049 continue;
5050 }
5051
ff4c9fec 5052 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5053 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5054 expansion_notify);
61920122
PA
5055 }
5056}
5057
4b514bc8
JK
5058/* If FILE_MATCHER is non-NULL, set all the
5059 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5060 that match FILE_MATCHER. */
5061
61920122 5062static void
4b514bc8 5063dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5064 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5065 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5066{
4b514bc8 5067 if (file_matcher == NULL)
61920122
PA
5068 return;
5069
4b514bc8
JK
5070 objfile *const objfile = dwarf2_per_objfile->objfile;
5071
5072 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5073 htab_eq_pointer,
5074 NULL, xcalloc, xfree));
5075 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5076 htab_eq_pointer,
5077 NULL, xcalloc, xfree));
61920122 5078
4b514bc8
JK
5079 /* The rule is CUs specify all the files, including those used by
5080 any TU, so there's no need to scan TUs here. */
61920122 5081
b76e467d 5082 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5083 {
927aa2e7
JK
5084 QUIT;
5085
5086 per_cu->v.quick->mark = 0;
5087
5088 /* We only need to look at symtabs not already expanded. */
5089 if (per_cu->v.quick->compunit_symtab)
5090 continue;
5091
b76e467d 5092 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5093 if (file_data == NULL)
5094 continue;
5095
5096 if (htab_find (visited_not_found.get (), file_data) != NULL)
5097 continue;
5098 else if (htab_find (visited_found.get (), file_data) != NULL)
5099 {
5100 per_cu->v.quick->mark = 1;
5101 continue;
5102 }
5103
b76e467d 5104 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5105 {
5106 const char *this_real_name;
5107
5108 if (file_matcher (file_data->file_names[j], false))
5109 {
5110 per_cu->v.quick->mark = 1;
5111 break;
5112 }
5113
5114 /* Before we invoke realpath, which can get expensive when many
5115 files are involved, do a quick comparison of the basenames. */
5116 if (!basenames_may_differ
5117 && !file_matcher (lbasename (file_data->file_names[j]),
5118 true))
5119 continue;
5120
5121 this_real_name = dw2_get_real_path (objfile, file_data, j);
5122 if (file_matcher (this_real_name, false))
5123 {
5124 per_cu->v.quick->mark = 1;
5125 break;
5126 }
5127 }
5128
b76e467d
SM
5129 void **slot = htab_find_slot (per_cu->v.quick->mark
5130 ? visited_found.get ()
5131 : visited_not_found.get (),
5132 file_data, INSERT);
927aa2e7
JK
5133 *slot = file_data;
5134 }
5135}
5136
5137static void
5138dw2_expand_symtabs_matching
5139 (struct objfile *objfile,
5140 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5141 const lookup_name_info &lookup_name,
5142 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5143 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5144 enum search_domain kind)
5145{
ed2dc618
SM
5146 struct dwarf2_per_objfile *dwarf2_per_objfile
5147 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5148
5149 /* index_table is NULL if OBJF_READNOW. */
5150 if (!dwarf2_per_objfile->index_table)
5151 return;
5152
ed2dc618 5153 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5154
5155 mapped_index &index = *dwarf2_per_objfile->index_table;
5156
5157 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5158 symbol_matcher,
5159 kind, [&] (offset_type idx)
5160 {
ed2dc618 5161 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5162 expansion_notify, kind);
3b00ef10 5163 return true;
927aa2e7
JK
5164 });
5165}
5166
5167/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5168 symtab. */
5169
5170static struct compunit_symtab *
5171recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5172 CORE_ADDR pc)
5173{
5174 int i;
5175
5176 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5177 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5178 return cust;
5179
5180 if (cust->includes == NULL)
5181 return NULL;
5182
5183 for (i = 0; cust->includes[i]; ++i)
5184 {
5185 struct compunit_symtab *s = cust->includes[i];
5186
5187 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5188 if (s != NULL)
5189 return s;
5190 }
5191
5192 return NULL;
5193}
5194
5195static struct compunit_symtab *
5196dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5197 struct bound_minimal_symbol msymbol,
5198 CORE_ADDR pc,
5199 struct obj_section *section,
5200 int warn_if_readin)
5201{
5202 struct dwarf2_per_cu_data *data;
5203 struct compunit_symtab *result;
5204
d320c2b5 5205 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5206 return NULL;
5207
79748972
TT
5208 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5209 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5210 data = (struct dwarf2_per_cu_data *) addrmap_find
5211 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5212 if (!data)
5213 return NULL;
5214
5215 if (warn_if_readin && data->v.quick->compunit_symtab)
5216 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5217 paddress (get_objfile_arch (objfile), pc));
5218
5219 result
58f0c718
TT
5220 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5221 false),
927aa2e7
JK
5222 pc);
5223 gdb_assert (result != NULL);
5224 return result;
5225}
5226
5227static void
5228dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5229 void *data, int need_fullname)
5230{
ed2dc618
SM
5231 struct dwarf2_per_objfile *dwarf2_per_objfile
5232 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5233
5234 if (!dwarf2_per_objfile->filenames_cache)
5235 {
5236 dwarf2_per_objfile->filenames_cache.emplace ();
5237
5238 htab_up visited (htab_create_alloc (10,
5239 htab_hash_pointer, htab_eq_pointer,
5240 NULL, xcalloc, xfree));
5241
5242 /* The rule is CUs specify all the files, including those used
5243 by any TU, so there's no need to scan TUs here. We can
5244 ignore file names coming from already-expanded CUs. */
5245
b76e467d 5246 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5247 {
927aa2e7
JK
5248 if (per_cu->v.quick->compunit_symtab)
5249 {
5250 void **slot = htab_find_slot (visited.get (),
5251 per_cu->v.quick->file_names,
5252 INSERT);
5253
5254 *slot = per_cu->v.quick->file_names;
5255 }
5256 }
5257
b76e467d 5258 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5259 {
927aa2e7
JK
5260 /* We only need to look at symtabs not already expanded. */
5261 if (per_cu->v.quick->compunit_symtab)
5262 continue;
5263
b76e467d 5264 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5265 if (file_data == NULL)
5266 continue;
5267
b76e467d 5268 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5269 if (*slot)
5270 {
5271 /* Already visited. */
5272 continue;
5273 }
5274 *slot = file_data;
5275
5276 for (int j = 0; j < file_data->num_file_names; ++j)
5277 {
5278 const char *filename = file_data->file_names[j];
5279 dwarf2_per_objfile->filenames_cache->seen (filename);
5280 }
5281 }
5282 }
5283
5284 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5285 {
5286 gdb::unique_xmalloc_ptr<char> this_real_name;
5287
5288 if (need_fullname)
5289 this_real_name = gdb_realpath (filename);
5290 (*fun) (filename, this_real_name.get (), data);
5291 });
5292}
5293
5294static int
5295dw2_has_symbols (struct objfile *objfile)
5296{
5297 return 1;
5298}
5299
5300const struct quick_symbol_functions dwarf2_gdb_index_functions =
5301{
5302 dw2_has_symbols,
5303 dw2_find_last_source_symtab,
5304 dw2_forget_cached_source_info,
5305 dw2_map_symtabs_matching_filename,
5306 dw2_lookup_symbol,
5307 dw2_print_stats,
5308 dw2_dump,
927aa2e7
JK
5309 dw2_expand_symtabs_for_function,
5310 dw2_expand_all_symtabs,
5311 dw2_expand_symtabs_with_fullname,
5312 dw2_map_matching_symbols,
5313 dw2_expand_symtabs_matching,
5314 dw2_find_pc_sect_compunit_symtab,
5315 NULL,
5316 dw2_map_symbol_filenames
5317};
5318
5319/* DWARF-5 debug_names reader. */
5320
5321/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5322static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5323
5324/* A helper function that reads the .debug_names section in SECTION
5325 and fills in MAP. FILENAME is the name of the file containing the
5326 section; it is used for error reporting.
5327
5328 Returns true if all went well, false otherwise. */
5329
5330static bool
5331read_debug_names_from_section (struct objfile *objfile,
5332 const char *filename,
5333 struct dwarf2_section_info *section,
5334 mapped_debug_names &map)
5335{
5336 if (dwarf2_section_empty_p (section))
5337 return false;
5338
5339 /* Older elfutils strip versions could keep the section in the main
5340 executable while splitting it for the separate debug info file. */
5341 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5342 return false;
5343
5344 dwarf2_read_section (objfile, section);
5345
5346 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5347
5348 const gdb_byte *addr = section->buffer;
5349
5350 bfd *const abfd = get_section_bfd_owner (section);
5351
5352 unsigned int bytes_read;
5353 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5354 addr += bytes_read;
5355
5356 map.dwarf5_is_dwarf64 = bytes_read != 4;
5357 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5358 if (bytes_read + length != section->size)
5359 {
5360 /* There may be multiple per-CU indices. */
5361 warning (_("Section .debug_names in %s length %s does not match "
5362 "section length %s, ignoring .debug_names."),
5363 filename, plongest (bytes_read + length),
5364 pulongest (section->size));
5365 return false;
5366 }
5367
5368 /* The version number. */
5369 uint16_t version = read_2_bytes (abfd, addr);
5370 addr += 2;
5371 if (version != 5)
5372 {
5373 warning (_("Section .debug_names in %s has unsupported version %d, "
5374 "ignoring .debug_names."),
5375 filename, version);
5376 return false;
5377 }
5378
5379 /* Padding. */
5380 uint16_t padding = read_2_bytes (abfd, addr);
5381 addr += 2;
5382 if (padding != 0)
5383 {
5384 warning (_("Section .debug_names in %s has unsupported padding %d, "
5385 "ignoring .debug_names."),
5386 filename, padding);
5387 return false;
5388 }
5389
5390 /* comp_unit_count - The number of CUs in the CU list. */
5391 map.cu_count = read_4_bytes (abfd, addr);
5392 addr += 4;
5393
5394 /* local_type_unit_count - The number of TUs in the local TU
5395 list. */
5396 map.tu_count = read_4_bytes (abfd, addr);
5397 addr += 4;
5398
5399 /* foreign_type_unit_count - The number of TUs in the foreign TU
5400 list. */
5401 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5402 addr += 4;
5403 if (foreign_tu_count != 0)
5404 {
5405 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5406 "ignoring .debug_names."),
5407 filename, static_cast<unsigned long> (foreign_tu_count));
5408 return false;
5409 }
5410
5411 /* bucket_count - The number of hash buckets in the hash lookup
5412 table. */
5413 map.bucket_count = read_4_bytes (abfd, addr);
5414 addr += 4;
5415
5416 /* name_count - The number of unique names in the index. */
5417 map.name_count = read_4_bytes (abfd, addr);
5418 addr += 4;
5419
5420 /* abbrev_table_size - The size in bytes of the abbreviations
5421 table. */
5422 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5423 addr += 4;
5424
5425 /* augmentation_string_size - The size in bytes of the augmentation
5426 string. This value is rounded up to a multiple of 4. */
5427 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5428 addr += 4;
5429 map.augmentation_is_gdb = ((augmentation_string_size
5430 == sizeof (dwarf5_augmentation))
5431 && memcmp (addr, dwarf5_augmentation,
5432 sizeof (dwarf5_augmentation)) == 0);
5433 augmentation_string_size += (-augmentation_string_size) & 3;
5434 addr += augmentation_string_size;
5435
5436 /* List of CUs */
5437 map.cu_table_reordered = addr;
5438 addr += map.cu_count * map.offset_size;
5439
5440 /* List of Local TUs */
5441 map.tu_table_reordered = addr;
5442 addr += map.tu_count * map.offset_size;
5443
5444 /* Hash Lookup Table */
5445 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5446 addr += map.bucket_count * 4;
5447 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5448 addr += map.name_count * 4;
5449
5450 /* Name Table */
5451 map.name_table_string_offs_reordered = addr;
5452 addr += map.name_count * map.offset_size;
5453 map.name_table_entry_offs_reordered = addr;
5454 addr += map.name_count * map.offset_size;
5455
5456 const gdb_byte *abbrev_table_start = addr;
5457 for (;;)
5458 {
927aa2e7
JK
5459 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5460 addr += bytes_read;
5461 if (index_num == 0)
5462 break;
5463
5464 const auto insertpair
5465 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5466 if (!insertpair.second)
5467 {
5468 warning (_("Section .debug_names in %s has duplicate index %s, "
5469 "ignoring .debug_names."),
5470 filename, pulongest (index_num));
5471 return false;
5472 }
5473 mapped_debug_names::index_val &indexval = insertpair.first->second;
5474 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5475 addr += bytes_read;
5476
5477 for (;;)
5478 {
5479 mapped_debug_names::index_val::attr attr;
5480 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5481 addr += bytes_read;
5482 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5483 addr += bytes_read;
5484 if (attr.form == DW_FORM_implicit_const)
5485 {
5486 attr.implicit_const = read_signed_leb128 (abfd, addr,
5487 &bytes_read);
5488 addr += bytes_read;
5489 }
5490 if (attr.dw_idx == 0 && attr.form == 0)
5491 break;
5492 indexval.attr_vec.push_back (std::move (attr));
5493 }
5494 }
5495 if (addr != abbrev_table_start + abbrev_table_size)
5496 {
5497 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5498 "of size %s vs. written as %u, ignoring .debug_names."),
5499 filename, plongest (addr - abbrev_table_start),
5500 abbrev_table_size);
927aa2e7
JK
5501 return false;
5502 }
5503 map.entry_pool = addr;
5504
5505 return true;
5506}
5507
5508/* A helper for create_cus_from_debug_names that handles the MAP's CU
5509 list. */
5510
5511static void
ed2dc618 5512create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5513 const mapped_debug_names &map,
5514 dwarf2_section_info &section,
b76e467d 5515 bool is_dwz)
927aa2e7
JK
5516{
5517 sect_offset sect_off_prev;
5518 for (uint32_t i = 0; i <= map.cu_count; ++i)
5519 {
5520 sect_offset sect_off_next;
5521 if (i < map.cu_count)
5522 {
5523 sect_off_next
5524 = (sect_offset) (extract_unsigned_integer
5525 (map.cu_table_reordered + i * map.offset_size,
5526 map.offset_size,
5527 map.dwarf5_byte_order));
5528 }
5529 else
5530 sect_off_next = (sect_offset) section.size;
5531 if (i >= 1)
5532 {
5533 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5534 dwarf2_per_cu_data *per_cu
ed2dc618 5535 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5536 sect_off_prev, length);
b76e467d 5537 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5538 }
5539 sect_off_prev = sect_off_next;
5540 }
5541}
5542
5543/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5544 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5545
5546static void
ed2dc618 5547create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5548 const mapped_debug_names &map,
5549 const mapped_debug_names &dwz_map)
5550{
b76e467d
SM
5551 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5552 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5553
ed2dc618
SM
5554 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5555 dwarf2_per_objfile->info,
b76e467d 5556 false /* is_dwz */);
927aa2e7
JK
5557
5558 if (dwz_map.cu_count == 0)
5559 return;
5560
ed2dc618
SM
5561 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5562 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5563 true /* is_dwz */);
927aa2e7
JK
5564}
5565
5566/* Read .debug_names. If everything went ok, initialize the "quick"
5567 elements of all the CUs and return true. Otherwise, return false. */
5568
5569static bool
ed2dc618 5570dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5571{
22ca247e
TT
5572 std::unique_ptr<mapped_debug_names> map
5573 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5574 mapped_debug_names dwz_map (dwarf2_per_objfile);
5575 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5576
5577 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5578 &dwarf2_per_objfile->debug_names,
22ca247e 5579 *map))
927aa2e7
JK
5580 return false;
5581
5582 /* Don't use the index if it's empty. */
22ca247e 5583 if (map->name_count == 0)
927aa2e7
JK
5584 return false;
5585
5586 /* If there is a .dwz file, read it so we can get its CU list as
5587 well. */
ed2dc618 5588 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5589 if (dwz != NULL)
5590 {
5591 if (!read_debug_names_from_section (objfile,
00f93c44 5592 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5593 &dwz->debug_names, dwz_map))
5594 {
5595 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5596 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5597 return false;
5598 }
5599 }
5600
22ca247e 5601 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5602
22ca247e 5603 if (map->tu_count != 0)
927aa2e7
JK
5604 {
5605 /* We can only handle a single .debug_types when we have an
5606 index. */
fd5866f6 5607 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5608 return false;
5609
fd5866f6 5610 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5611
5612 create_signatured_type_table_from_debug_names
22ca247e 5613 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5614 }
5615
ed2dc618
SM
5616 create_addrmap_from_aranges (dwarf2_per_objfile,
5617 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5618
22ca247e 5619 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5620 dwarf2_per_objfile->using_index = 1;
5621 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5622 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5623
5624 return true;
5625}
5626
927aa2e7
JK
5627/* Type used to manage iterating over all CUs looking for a symbol for
5628 .debug_names. */
5629
5630class dw2_debug_names_iterator
5631{
5632public:
927aa2e7 5633 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5634 gdb::optional<block_enum> block_index,
5635 domain_enum domain,
927aa2e7 5636 const char *name)
2b79f376 5637 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5638 m_addr (find_vec_in_debug_names (map, name))
5639 {}
5640
5641 dw2_debug_names_iterator (const mapped_debug_names &map,
5642 search_domain search, uint32_t namei)
5643 : m_map (map),
5644 m_search (search),
5645 m_addr (find_vec_in_debug_names (map, namei))
5646 {}
5647
3b00ef10
TT
5648 dw2_debug_names_iterator (const mapped_debug_names &map,
5649 block_enum block_index, domain_enum domain,
5650 uint32_t namei)
5651 : m_map (map), m_block_index (block_index), m_domain (domain),
5652 m_addr (find_vec_in_debug_names (map, namei))
5653 {}
5654
927aa2e7
JK
5655 /* Return the next matching CU or NULL if there are no more. */
5656 dwarf2_per_cu_data *next ();
5657
5658private:
5659 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5660 const char *name);
5661 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5662 uint32_t namei);
5663
5664 /* The internalized form of .debug_names. */
5665 const mapped_debug_names &m_map;
5666
2b79f376
SM
5667 /* If set, only look for symbols that match that block. Valid values are
5668 GLOBAL_BLOCK and STATIC_BLOCK. */
5669 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5670
5671 /* The kind of symbol we're looking for. */
5672 const domain_enum m_domain = UNDEF_DOMAIN;
5673 const search_domain m_search = ALL_DOMAIN;
5674
5675 /* The list of CUs from the index entry of the symbol, or NULL if
5676 not found. */
5677 const gdb_byte *m_addr;
5678};
5679
5680const char *
5681mapped_debug_names::namei_to_name (uint32_t namei) const
5682{
5683 const ULONGEST namei_string_offs
5684 = extract_unsigned_integer ((name_table_string_offs_reordered
5685 + namei * offset_size),
5686 offset_size,
5687 dwarf5_byte_order);
5688 return read_indirect_string_at_offset
ed2dc618 5689 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5690}
5691
5692/* Find a slot in .debug_names for the object named NAME. If NAME is
5693 found, return pointer to its pool data. If NAME cannot be found,
5694 return NULL. */
5695
5696const gdb_byte *
5697dw2_debug_names_iterator::find_vec_in_debug_names
5698 (const mapped_debug_names &map, const char *name)
5699{
5700 int (*cmp) (const char *, const char *);
5701
54ee4252 5702 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5703 if (current_language->la_language == language_cplus
5704 || current_language->la_language == language_fortran
5705 || current_language->la_language == language_d)
5706 {
5707 /* NAME is already canonical. Drop any qualifiers as
5708 .debug_names does not contain any. */
5709
5710 if (strchr (name, '(') != NULL)
5711 {
54ee4252 5712 without_params = cp_remove_params (name);
927aa2e7 5713 if (without_params != NULL)
54ee4252 5714 name = without_params.get ();
927aa2e7
JK
5715 }
5716 }
5717
5718 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5719
5720 const uint32_t full_hash = dwarf5_djb_hash (name);
5721 uint32_t namei
5722 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5723 (map.bucket_table_reordered
5724 + (full_hash % map.bucket_count)), 4,
5725 map.dwarf5_byte_order);
5726 if (namei == 0)
5727 return NULL;
5728 --namei;
5729 if (namei >= map.name_count)
5730 {
b98664d3 5731 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5732 "[in module %s]"),
5733 namei, map.name_count,
ed2dc618 5734 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5735 return NULL;
5736 }
5737
5738 for (;;)
5739 {
5740 const uint32_t namei_full_hash
5741 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5742 (map.hash_table_reordered + namei), 4,
5743 map.dwarf5_byte_order);
5744 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5745 return NULL;
5746
5747 if (full_hash == namei_full_hash)
5748 {
5749 const char *const namei_string = map.namei_to_name (namei);
5750
5751#if 0 /* An expensive sanity check. */
5752 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5753 {
b98664d3 5754 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5755 "[in module %s]"),
5756 namei, objfile_name (dwarf2_per_objfile->objfile));
5757 return NULL;
5758 }
5759#endif
5760
5761 if (cmp (namei_string, name) == 0)
5762 {
5763 const ULONGEST namei_entry_offs
5764 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5765 + namei * map.offset_size),
5766 map.offset_size, map.dwarf5_byte_order);
5767 return map.entry_pool + namei_entry_offs;
5768 }
5769 }
5770
5771 ++namei;
5772 if (namei >= map.name_count)
5773 return NULL;
5774 }
5775}
5776
5777const gdb_byte *
5778dw2_debug_names_iterator::find_vec_in_debug_names
5779 (const mapped_debug_names &map, uint32_t namei)
5780{
5781 if (namei >= map.name_count)
5782 {
b98664d3 5783 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5784 "[in module %s]"),
5785 namei, map.name_count,
ed2dc618 5786 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5787 return NULL;
5788 }
5789
5790 const ULONGEST namei_entry_offs
5791 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5792 + namei * map.offset_size),
5793 map.offset_size, map.dwarf5_byte_order);
5794 return map.entry_pool + namei_entry_offs;
5795}
5796
5797/* See dw2_debug_names_iterator. */
5798
5799dwarf2_per_cu_data *
5800dw2_debug_names_iterator::next ()
5801{
5802 if (m_addr == NULL)
5803 return NULL;
5804
ed2dc618
SM
5805 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5806 struct objfile *objfile = dwarf2_per_objfile->objfile;
5807 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5808
5809 again:
5810
5811 unsigned int bytes_read;
5812 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5813 m_addr += bytes_read;
5814 if (abbrev == 0)
5815 return NULL;
5816
5817 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5818 if (indexval_it == m_map.abbrev_map.cend ())
5819 {
b98664d3 5820 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5821 "[in module %s]"),
ed2dc618 5822 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5823 return NULL;
5824 }
5825 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5826 enum class symbol_linkage {
5827 unknown,
5828 static_,
5829 extern_,
23c13d42 5830 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5831 dwarf2_per_cu_data *per_cu = NULL;
5832 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5833 {
5834 ULONGEST ull;
5835 switch (attr.form)
5836 {
5837 case DW_FORM_implicit_const:
5838 ull = attr.implicit_const;
5839 break;
5840 case DW_FORM_flag_present:
5841 ull = 1;
5842 break;
5843 case DW_FORM_udata:
5844 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5845 m_addr += bytes_read;
5846 break;
5847 default:
b98664d3 5848 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5849 dwarf_form_name (attr.form),
ed2dc618 5850 objfile_name (objfile));
927aa2e7
JK
5851 return NULL;
5852 }
5853 switch (attr.dw_idx)
5854 {
5855 case DW_IDX_compile_unit:
5856 /* Don't crash on bad data. */
b76e467d 5857 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5858 {
b98664d3 5859 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5860 " [in module %s]"),
5861 pulongest (ull),
5862 objfile_name (dwarf2_per_objfile->objfile));
5863 continue;
5864 }
ff4c9fec 5865 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5866 break;
8af5c486
JK
5867 case DW_IDX_type_unit:
5868 /* Don't crash on bad data. */
b2bdb8cf 5869 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5870 {
b98664d3 5871 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5872 " [in module %s]"),
5873 pulongest (ull),
5874 objfile_name (dwarf2_per_objfile->objfile));
5875 continue;
5876 }
ff4c9fec 5877 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5878 break;
927aa2e7
JK
5879 case DW_IDX_GNU_internal:
5880 if (!m_map.augmentation_is_gdb)
5881 break;
23c13d42 5882 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5883 break;
5884 case DW_IDX_GNU_external:
5885 if (!m_map.augmentation_is_gdb)
5886 break;
23c13d42 5887 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5888 break;
5889 }
5890 }
5891
5892 /* Skip if already read in. */
5893 if (per_cu->v.quick->compunit_symtab)
5894 goto again;
5895
5896 /* Check static vs global. */
23c13d42 5897 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5898 {
2b79f376 5899 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5900 const bool symbol_is_static =
5901 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5902 if (want_static != symbol_is_static)
2b79f376 5903 goto again;
927aa2e7
JK
5904 }
5905
5906 /* Match dw2_symtab_iter_next, symbol_kind
5907 and debug_names::psymbol_tag. */
5908 switch (m_domain)
5909 {
5910 case VAR_DOMAIN:
5911 switch (indexval.dwarf_tag)
5912 {
5913 case DW_TAG_variable:
5914 case DW_TAG_subprogram:
5915 /* Some types are also in VAR_DOMAIN. */
5916 case DW_TAG_typedef:
5917 case DW_TAG_structure_type:
5918 break;
5919 default:
5920 goto again;
5921 }
5922 break;
5923 case STRUCT_DOMAIN:
5924 switch (indexval.dwarf_tag)
5925 {
5926 case DW_TAG_typedef:
5927 case DW_TAG_structure_type:
5928 break;
5929 default:
5930 goto again;
5931 }
5932 break;
5933 case LABEL_DOMAIN:
5934 switch (indexval.dwarf_tag)
5935 {
5936 case 0:
5937 case DW_TAG_variable:
5938 break;
5939 default:
5940 goto again;
5941 }
5942 break;
5943 default:
5944 break;
5945 }
5946
5947 /* Match dw2_expand_symtabs_matching, symbol_kind and
5948 debug_names::psymbol_tag. */
5949 switch (m_search)
4b514bc8 5950 {
927aa2e7
JK
5951 case VARIABLES_DOMAIN:
5952 switch (indexval.dwarf_tag)
4b514bc8 5953 {
927aa2e7
JK
5954 case DW_TAG_variable:
5955 break;
5956 default:
5957 goto again;
4b514bc8 5958 }
927aa2e7
JK
5959 break;
5960 case FUNCTIONS_DOMAIN:
5961 switch (indexval.dwarf_tag)
4b514bc8 5962 {
927aa2e7
JK
5963 case DW_TAG_subprogram:
5964 break;
5965 default:
5966 goto again;
4b514bc8 5967 }
927aa2e7
JK
5968 break;
5969 case TYPES_DOMAIN:
5970 switch (indexval.dwarf_tag)
5971 {
5972 case DW_TAG_typedef:
5973 case DW_TAG_structure_type:
5974 break;
5975 default:
5976 goto again;
5977 }
5978 break;
5979 default:
5980 break;
4b514bc8 5981 }
927aa2e7
JK
5982
5983 return per_cu;
4b514bc8 5984}
61920122 5985
927aa2e7 5986static struct compunit_symtab *
c7f839cb 5987dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5988 const char *name, domain_enum domain)
4b514bc8 5989{
ed2dc618
SM
5990 struct dwarf2_per_objfile *dwarf2_per_objfile
5991 = get_dwarf2_per_objfile (objfile);
61920122 5992
927aa2e7
JK
5993 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5994 if (!mapp)
61920122 5995 {
927aa2e7
JK
5996 /* index is NULL if OBJF_READNOW. */
5997 return NULL;
5998 }
5999 const auto &map = *mapp;
9291a0cd 6000
2b79f376 6001 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6002
927aa2e7
JK
6003 struct compunit_symtab *stab_best = NULL;
6004 struct dwarf2_per_cu_data *per_cu;
6005 while ((per_cu = iter.next ()) != NULL)
6006 {
6007 struct symbol *sym, *with_opaque = NULL;
58f0c718 6008 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6009 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6010 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6011
927aa2e7
JK
6012 sym = block_find_symbol (block, name, domain,
6013 block_find_non_opaque_type_preferred,
6014 &with_opaque);
9703b513 6015
927aa2e7
JK
6016 /* Some caution must be observed with overloaded functions and
6017 methods, since the index will not contain any overload
6018 information (but NAME might contain it). */
a3ec0bb1 6019
927aa2e7
JK
6020 if (sym != NULL
6021 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6022 return stab;
6023 if (with_opaque != NULL
6024 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6025 stab_best = stab;
9703b513 6026
927aa2e7 6027 /* Keep looking through other CUs. */
9703b513
TT
6028 }
6029
927aa2e7 6030 return stab_best;
9703b513
TT
6031}
6032
927aa2e7
JK
6033/* This dumps minimal information about .debug_names. It is called
6034 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6035 uses this to verify that .debug_names has been loaded. */
9291a0cd 6036
927aa2e7
JK
6037static void
6038dw2_debug_names_dump (struct objfile *objfile)
6039{
ed2dc618
SM
6040 struct dwarf2_per_objfile *dwarf2_per_objfile
6041 = get_dwarf2_per_objfile (objfile);
6042
927aa2e7
JK
6043 gdb_assert (dwarf2_per_objfile->using_index);
6044 printf_filtered (".debug_names:");
6045 if (dwarf2_per_objfile->debug_names_table)
6046 printf_filtered (" exists\n");
6047 else
6048 printf_filtered (" faked for \"readnow\"\n");
6049 printf_filtered ("\n");
9291a0cd
TT
6050}
6051
9291a0cd 6052static void
927aa2e7
JK
6053dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6054 const char *func_name)
9291a0cd 6055{
ed2dc618
SM
6056 struct dwarf2_per_objfile *dwarf2_per_objfile
6057 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6058
927aa2e7
JK
6059 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6060 if (dwarf2_per_objfile->debug_names_table)
24c79950 6061 {
927aa2e7 6062 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6063
2b79f376 6064 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6065
927aa2e7
JK
6066 struct dwarf2_per_cu_data *per_cu;
6067 while ((per_cu = iter.next ()) != NULL)
58f0c718 6068 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6069 }
6070}
24c79950 6071
3b00ef10
TT
6072static void
6073dw2_debug_names_map_matching_symbols
6074 (struct objfile *objfile,
6075 const lookup_name_info &name, domain_enum domain,
6076 int global,
6077 gdb::function_view<symbol_found_callback_ftype> callback,
6078 symbol_compare_ftype *ordered_compare)
6079{
6080 struct dwarf2_per_objfile *dwarf2_per_objfile
6081 = get_dwarf2_per_objfile (objfile);
6082
6083 /* debug_names_table is NULL if OBJF_READNOW. */
6084 if (!dwarf2_per_objfile->debug_names_table)
6085 return;
6086
6087 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6088 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6089
6090 const char *match_name = name.ada ().lookup_name ().c_str ();
6091 auto matcher = [&] (const char *symname)
6092 {
6093 if (ordered_compare == nullptr)
6094 return true;
6095 return ordered_compare (symname, match_name) == 0;
6096 };
6097
6098 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6099 [&] (offset_type namei)
6100 {
6101 /* The name was matched, now expand corresponding CUs that were
6102 marked. */
6103 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6104
6105 struct dwarf2_per_cu_data *per_cu;
6106 while ((per_cu = iter.next ()) != NULL)
6107 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6108 return true;
6109 });
6110
6111 /* It's a shame we couldn't do this inside the
6112 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6113 that have already been expanded. Instead, this loop matches what
6114 the psymtab code does. */
6115 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6116 {
6117 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6118 if (cust != nullptr)
6119 {
6120 const struct block *block
6121 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6122 if (!iterate_over_symbols_terminated (block, name,
6123 domain, callback))
6124 break;
6125 }
6126 }
6127}
6128
927aa2e7
JK
6129static void
6130dw2_debug_names_expand_symtabs_matching
6131 (struct objfile *objfile,
6132 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6133 const lookup_name_info &lookup_name,
6134 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6135 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6136 enum search_domain kind)
6137{
ed2dc618
SM
6138 struct dwarf2_per_objfile *dwarf2_per_objfile
6139 = get_dwarf2_per_objfile (objfile);
9291a0cd 6140
927aa2e7
JK
6141 /* debug_names_table is NULL if OBJF_READNOW. */
6142 if (!dwarf2_per_objfile->debug_names_table)
6143 return;
9291a0cd 6144
ed2dc618 6145 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6146
44ed8f3e 6147 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6148
44ed8f3e
PA
6149 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6150 symbol_matcher,
6151 kind, [&] (offset_type namei)
927aa2e7 6152 {
927aa2e7
JK
6153 /* The name was matched, now expand corresponding CUs that were
6154 marked. */
6155 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6156
927aa2e7
JK
6157 struct dwarf2_per_cu_data *per_cu;
6158 while ((per_cu = iter.next ()) != NULL)
6159 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6160 expansion_notify);
3b00ef10 6161 return true;
44ed8f3e 6162 });
9291a0cd
TT
6163}
6164
927aa2e7 6165const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6166{
6167 dw2_has_symbols,
6168 dw2_find_last_source_symtab,
6169 dw2_forget_cached_source_info,
f8eba3c6 6170 dw2_map_symtabs_matching_filename,
927aa2e7 6171 dw2_debug_names_lookup_symbol,
9291a0cd 6172 dw2_print_stats,
927aa2e7 6173 dw2_debug_names_dump,
927aa2e7 6174 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6175 dw2_expand_all_symtabs,
652a8996 6176 dw2_expand_symtabs_with_fullname,
3b00ef10 6177 dw2_debug_names_map_matching_symbols,
927aa2e7 6178 dw2_debug_names_expand_symtabs_matching,
43f3e411 6179 dw2_find_pc_sect_compunit_symtab,
71a3c369 6180 NULL,
9291a0cd
TT
6181 dw2_map_symbol_filenames
6182};
6183
4485a1c1
SM
6184/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6185 to either a dwarf2_per_objfile or dwz_file object. */
6186
6187template <typename T>
6188static gdb::array_view<const gdb_byte>
6189get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6190{
6191 dwarf2_section_info *section = &section_owner->gdb_index;
6192
6193 if (dwarf2_section_empty_p (section))
6194 return {};
6195
6196 /* Older elfutils strip versions could keep the section in the main
6197 executable while splitting it for the separate debug info file. */
6198 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6199 return {};
6200
6201 dwarf2_read_section (obj, section);
6202
8bebfcda
PA
6203 /* dwarf2_section_info::size is a bfd_size_type, while
6204 gdb::array_view works with size_t. On 32-bit hosts, with
6205 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6206 is 32-bit. So we need an explicit narrowing conversion here.
6207 This is fine, because it's impossible to allocate or mmap an
6208 array/buffer larger than what size_t can represent. */
6209 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6210}
6211
87d6a7aa
SM
6212/* Lookup the index cache for the contents of the index associated to
6213 DWARF2_OBJ. */
6214
6215static gdb::array_view<const gdb_byte>
6216get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6217{
6218 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6219 if (build_id == nullptr)
6220 return {};
6221
6222 return global_index_cache.lookup_gdb_index (build_id,
6223 &dwarf2_obj->index_cache_res);
6224}
6225
6226/* Same as the above, but for DWZ. */
6227
6228static gdb::array_view<const gdb_byte>
6229get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6230{
6231 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6232 if (build_id == nullptr)
6233 return {};
6234
6235 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6236}
6237
3c0aa29a 6238/* See symfile.h. */
9291a0cd 6239
3c0aa29a
PA
6240bool
6241dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6242{
ed2dc618
SM
6243 struct dwarf2_per_objfile *dwarf2_per_objfile
6244 = get_dwarf2_per_objfile (objfile);
6245
9291a0cd
TT
6246 /* If we're about to read full symbols, don't bother with the
6247 indices. In this case we also don't care if some other debug
6248 format is making psymtabs, because they are all about to be
6249 expanded anyway. */
6250 if ((objfile->flags & OBJF_READNOW))
6251 {
9291a0cd 6252 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6253 create_all_comp_units (dwarf2_per_objfile);
6254 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6255 dwarf2_per_objfile->quick_file_names_table
6256 = create_quick_file_names_table
6257 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6258
b76e467d 6259 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6260 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6261 {
ff4c9fec 6262 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6263
e254ef6a
DE
6264 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6265 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6266 }
6267
6268 /* Return 1 so that gdb sees the "quick" functions. However,
6269 these functions will be no-ops because we will have expanded
6270 all symtabs. */
3c0aa29a
PA
6271 *index_kind = dw_index_kind::GDB_INDEX;
6272 return true;
9291a0cd
TT
6273 }
6274
ed2dc618 6275 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6276 {
6277 *index_kind = dw_index_kind::DEBUG_NAMES;
6278 return true;
6279 }
927aa2e7 6280
4485a1c1
SM
6281 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6282 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6283 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6284 {
6285 *index_kind = dw_index_kind::GDB_INDEX;
6286 return true;
6287 }
9291a0cd 6288
87d6a7aa
SM
6289 /* ... otherwise, try to find the index in the index cache. */
6290 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6291 get_gdb_index_contents_from_cache,
6292 get_gdb_index_contents_from_cache_dwz))
6293 {
6294 global_index_cache.hit ();
6295 *index_kind = dw_index_kind::GDB_INDEX;
6296 return true;
6297 }
6298
6299 global_index_cache.miss ();
3c0aa29a 6300 return false;
9291a0cd
TT
6301}
6302
6303\f
6304
dce234bc
PP
6305/* Build a partial symbol table. */
6306
6307void
f29dff0a 6308dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6309{
ed2dc618
SM
6310 struct dwarf2_per_objfile *dwarf2_per_objfile
6311 = get_dwarf2_per_objfile (objfile);
c9bf0622 6312
6eee24ce 6313 init_psymbol_list (objfile, 1024);
c906108c 6314
a70b8144 6315 try
c9bf0622
TT
6316 {
6317 /* This isn't really ideal: all the data we allocate on the
6318 objfile's obstack is still uselessly kept around. However,
6319 freeing it seems unsafe. */
906768f9 6320 psymtab_discarder psymtabs (objfile);
ed2dc618 6321 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6322 psymtabs.keep ();
87d6a7aa
SM
6323
6324 /* (maybe) store an index in the cache. */
6325 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6326 }
230d2906 6327 catch (const gdb_exception_error &except)
492d29ea
PA
6328 {
6329 exception_print (gdb_stderr, except);
6330 }
c906108c 6331}
c906108c 6332
1ce1cefd
DE
6333/* Return the total length of the CU described by HEADER. */
6334
6335static unsigned int
6336get_cu_length (const struct comp_unit_head *header)
6337{
6338 return header->initial_length_size + header->length;
6339}
6340
9c541725 6341/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6342
9c541725
PA
6343static inline bool
6344offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6345{
9c541725
PA
6346 sect_offset bottom = cu_header->sect_off;
6347 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6348
9c541725 6349 return sect_off >= bottom && sect_off < top;
45452591
DE
6350}
6351
3b80fe9b
DE
6352/* Find the base address of the compilation unit for range lists and
6353 location lists. It will normally be specified by DW_AT_low_pc.
6354 In DWARF-3 draft 4, the base address could be overridden by
6355 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6356 compilation units with discontinuous ranges. */
6357
6358static void
6359dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6360{
6361 struct attribute *attr;
6362
6363 cu->base_known = 0;
6364 cu->base_address = 0;
6365
6366 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6367 if (attr)
6368 {
31aa7e4e 6369 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6370 cu->base_known = 1;
6371 }
6372 else
6373 {
6374 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6375 if (attr)
6376 {
31aa7e4e 6377 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6378 cu->base_known = 1;
6379 }
6380 }
6381}
6382
93311388 6383/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6384 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6385 NOTE: This leaves members offset, first_die_offset to be filled in
6386 by the caller. */
107d2387 6387
d521ce57 6388static const gdb_byte *
107d2387 6389read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6390 const gdb_byte *info_ptr,
6391 struct dwarf2_section_info *section,
6392 rcuh_kind section_kind)
107d2387
AC
6393{
6394 int signed_addr;
891d2f0b 6395 unsigned int bytes_read;
43988095
JK
6396 const char *filename = get_section_file_name (section);
6397 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6398
6399 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6400 cu_header->initial_length_size = bytes_read;
6401 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6402 info_ptr += bytes_read;
107d2387 6403 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6404 if (cu_header->version < 2 || cu_header->version > 5)
6405 error (_("Dwarf Error: wrong version in compilation unit header "
6406 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6407 cu_header->version, filename);
107d2387 6408 info_ptr += 2;
43988095
JK
6409 if (cu_header->version < 5)
6410 switch (section_kind)
6411 {
6412 case rcuh_kind::COMPILE:
6413 cu_header->unit_type = DW_UT_compile;
6414 break;
6415 case rcuh_kind::TYPE:
6416 cu_header->unit_type = DW_UT_type;
6417 break;
6418 default:
6419 internal_error (__FILE__, __LINE__,
6420 _("read_comp_unit_head: invalid section_kind"));
6421 }
6422 else
6423 {
6424 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6425 (read_1_byte (abfd, info_ptr));
6426 info_ptr += 1;
6427 switch (cu_header->unit_type)
6428 {
6429 case DW_UT_compile:
a084a2a6
AT
6430 case DW_UT_partial:
6431 case DW_UT_skeleton:
6432 case DW_UT_split_compile:
43988095
JK
6433 if (section_kind != rcuh_kind::COMPILE)
6434 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6435 "(is %s, should be %s) [in module %s]"),
6436 dwarf_unit_type_name (cu_header->unit_type),
6437 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6438 break;
6439 case DW_UT_type:
a084a2a6 6440 case DW_UT_split_type:
43988095
JK
6441 section_kind = rcuh_kind::TYPE;
6442 break;
6443 default:
6444 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6445 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6446 "[in module %s]"), cu_header->unit_type,
6447 dwarf_unit_type_name (DW_UT_compile),
6448 dwarf_unit_type_name (DW_UT_skeleton),
6449 dwarf_unit_type_name (DW_UT_split_compile),
6450 dwarf_unit_type_name (DW_UT_type),
6451 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6452 }
6453
6454 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6455 info_ptr += 1;
6456 }
9c541725
PA
6457 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6458 cu_header,
6459 &bytes_read);
613e1657 6460 info_ptr += bytes_read;
43988095
JK
6461 if (cu_header->version < 5)
6462 {
6463 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6464 info_ptr += 1;
6465 }
107d2387
AC
6466 signed_addr = bfd_get_sign_extend_vma (abfd);
6467 if (signed_addr < 0)
8e65ff28 6468 internal_error (__FILE__, __LINE__,
e2e0b3e5 6469 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6470 cu_header->signed_addr_p = signed_addr;
c764a876 6471
a084a2a6
AT
6472 bool header_has_signature = section_kind == rcuh_kind::TYPE
6473 || cu_header->unit_type == DW_UT_skeleton
6474 || cu_header->unit_type == DW_UT_split_compile;
43988095 6475
a084a2a6
AT
6476 if (header_has_signature)
6477 {
43988095
JK
6478 cu_header->signature = read_8_bytes (abfd, info_ptr);
6479 info_ptr += 8;
a084a2a6 6480 }
43988095 6481
a084a2a6
AT
6482 if (section_kind == rcuh_kind::TYPE)
6483 {
6484 LONGEST type_offset;
43988095
JK
6485 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6486 info_ptr += bytes_read;
9c541725
PA
6487 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6488 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6489 error (_("Dwarf Error: Too big type_offset in compilation unit "
6490 "header (is %s) [in module %s]"), plongest (type_offset),
6491 filename);
6492 }
6493
107d2387
AC
6494 return info_ptr;
6495}
6496
36586728
TT
6497/* Helper function that returns the proper abbrev section for
6498 THIS_CU. */
6499
6500static struct dwarf2_section_info *
6501get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6502{
6503 struct dwarf2_section_info *abbrev;
ed2dc618 6504 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6505
6506 if (this_cu->is_dwz)
ed2dc618 6507 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6508 else
6509 abbrev = &dwarf2_per_objfile->abbrev;
6510
6511 return abbrev;
6512}
6513
9ff913ba
DE
6514/* Subroutine of read_and_check_comp_unit_head and
6515 read_and_check_type_unit_head to simplify them.
6516 Perform various error checking on the header. */
6517
6518static void
ed2dc618
SM
6519error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6520 struct comp_unit_head *header,
4bdcc0c1
DE
6521 struct dwarf2_section_info *section,
6522 struct dwarf2_section_info *abbrev_section)
9ff913ba 6523{
a32a8923 6524 const char *filename = get_section_file_name (section);
9ff913ba 6525
9c541725 6526 if (to_underlying (header->abbrev_sect_off)
36586728 6527 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6528 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6529 "(offset %s + 6) [in module %s]"),
6530 sect_offset_str (header->abbrev_sect_off),
6531 sect_offset_str (header->sect_off),
9ff913ba
DE
6532 filename);
6533
9c541725 6534 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6535 avoid potential 32-bit overflow. */
9c541725 6536 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6537 > section->size)
9c541725 6538 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6539 "(offset %s + 0) [in module %s]"),
6540 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6541 filename);
6542}
6543
6544/* Read in a CU/TU header and perform some basic error checking.
6545 The contents of the header are stored in HEADER.
6546 The result is a pointer to the start of the first DIE. */
adabb602 6547
d521ce57 6548static const gdb_byte *
ed2dc618
SM
6549read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6550 struct comp_unit_head *header,
9ff913ba 6551 struct dwarf2_section_info *section,
4bdcc0c1 6552 struct dwarf2_section_info *abbrev_section,
d521ce57 6553 const gdb_byte *info_ptr,
43988095 6554 rcuh_kind section_kind)
72bf9492 6555{
d521ce57 6556 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6557
9c541725 6558 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6559
43988095 6560 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6561
9c541725 6562 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6563
ed2dc618
SM
6564 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6565 abbrev_section);
9ff913ba
DE
6566
6567 return info_ptr;
348e048f
DE
6568}
6569
f4dc4d17
DE
6570/* Fetch the abbreviation table offset from a comp or type unit header. */
6571
6572static sect_offset
ed2dc618
SM
6573read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6574 struct dwarf2_section_info *section,
9c541725 6575 sect_offset sect_off)
f4dc4d17 6576{
a32a8923 6577 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6578 const gdb_byte *info_ptr;
ac298888 6579 unsigned int initial_length_size, offset_size;
43988095 6580 uint16_t version;
f4dc4d17
DE
6581
6582 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6583 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6584 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6585 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6586 info_ptr += initial_length_size;
6587
6588 version = read_2_bytes (abfd, info_ptr);
6589 info_ptr += 2;
6590 if (version >= 5)
6591 {
6592 /* Skip unit type and address size. */
6593 info_ptr += 2;
6594 }
6595
9c541725 6596 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6597}
6598
aaa75496
JB
6599/* Allocate a new partial symtab for file named NAME and mark this new
6600 partial symtab as being an include of PST. */
6601
6602static void
d521ce57 6603dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6604 struct objfile *objfile)
6605{
6606 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6607
fbd9ab74
JK
6608 if (!IS_ABSOLUTE_PATH (subpst->filename))
6609 {
6610 /* It shares objfile->objfile_obstack. */
6611 subpst->dirname = pst->dirname;
6612 }
6613
a9342b62 6614 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6615 subpst->dependencies[0] = pst;
6616 subpst->number_of_dependencies = 1;
6617
aaa75496 6618 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6619
6620 /* No private part is necessary for include psymtabs. This property
6621 can be used to differentiate between such include psymtabs and
10b3939b 6622 the regular ones. */
58a9656e 6623 subpst->read_symtab_private = NULL;
aaa75496
JB
6624}
6625
6626/* Read the Line Number Program data and extract the list of files
6627 included by the source file represented by PST. Build an include
d85a05f0 6628 partial symtab for each of these included files. */
aaa75496
JB
6629
6630static void
6631dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6632 struct die_info *die,
6633 struct partial_symtab *pst)
aaa75496 6634{
fff8551c 6635 line_header_up lh;
d85a05f0 6636 struct attribute *attr;
aaa75496 6637
d85a05f0
DJ
6638 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6639 if (attr)
9c541725 6640 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6641 if (lh == NULL)
6642 return; /* No linetable, so no includes. */
6643
79748972
TT
6644 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6645 that we pass in the raw text_low here; that is ok because we're
6646 only decoding the line table to make include partial symtabs, and
6647 so the addresses aren't really used. */
4ae976d1 6648 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6649 pst->raw_text_low (), 1);
aaa75496
JB
6650}
6651
348e048f 6652static hashval_t
52dc124a 6653hash_signatured_type (const void *item)
348e048f 6654{
9a3c8263
SM
6655 const struct signatured_type *sig_type
6656 = (const struct signatured_type *) item;
9a619af0 6657
348e048f 6658 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6659 return sig_type->signature;
348e048f
DE
6660}
6661
6662static int
52dc124a 6663eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6664{
9a3c8263
SM
6665 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6666 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6667
348e048f
DE
6668 return lhs->signature == rhs->signature;
6669}
6670
1fd400ff
TT
6671/* Allocate a hash table for signatured types. */
6672
6673static htab_t
673bfd45 6674allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6675{
6676 return htab_create_alloc_ex (41,
52dc124a
DE
6677 hash_signatured_type,
6678 eq_signatured_type,
1fd400ff
TT
6679 NULL,
6680 &objfile->objfile_obstack,
6681 hashtab_obstack_allocate,
6682 dummy_obstack_deallocate);
6683}
6684
d467dd73 6685/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6686
6687static int
d467dd73 6688add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6689{
9a3c8263 6690 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6691 std::vector<signatured_type *> *all_type_units
6692 = (std::vector<signatured_type *> *) datum;
1fd400ff 6693
b2bdb8cf 6694 all_type_units->push_back (sigt);
1fd400ff
TT
6695
6696 return 1;
6697}
6698
78d4d2c5 6699/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6700 and fill them into TYPES_HTAB. It will process only type units,
6701 therefore DW_UT_type. */
c88ee1f0 6702
78d4d2c5 6703static void
ed2dc618
SM
6704create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6705 struct dwo_file *dwo_file,
43988095
JK
6706 dwarf2_section_info *section, htab_t &types_htab,
6707 rcuh_kind section_kind)
348e048f 6708{
3019eac3 6709 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6710 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6711 bfd *abfd;
6712 const gdb_byte *info_ptr, *end_ptr;
348e048f 6713
4bdcc0c1
DE
6714 abbrev_section = (dwo_file != NULL
6715 ? &dwo_file->sections.abbrev
6716 : &dwarf2_per_objfile->abbrev);
6717
b4f54984 6718 if (dwarf_read_debug)
43988095
JK
6719 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6720 get_section_name (section),
a32a8923 6721 get_section_file_name (abbrev_section));
09406207 6722
78d4d2c5
JK
6723 dwarf2_read_section (objfile, section);
6724 info_ptr = section->buffer;
348e048f 6725
78d4d2c5
JK
6726 if (info_ptr == NULL)
6727 return;
348e048f 6728
78d4d2c5
JK
6729 /* We can't set abfd until now because the section may be empty or
6730 not present, in which case the bfd is unknown. */
6731 abfd = get_section_bfd_owner (section);
348e048f 6732
78d4d2c5
JK
6733 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6734 because we don't need to read any dies: the signature is in the
6735 header. */
3019eac3 6736
78d4d2c5
JK
6737 end_ptr = info_ptr + section->size;
6738 while (info_ptr < end_ptr)
6739 {
78d4d2c5
JK
6740 struct signatured_type *sig_type;
6741 struct dwo_unit *dwo_tu;
6742 void **slot;
6743 const gdb_byte *ptr = info_ptr;
6744 struct comp_unit_head header;
6745 unsigned int length;
8b70b953 6746
9c541725 6747 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6748
a49dd8dd
JK
6749 /* Initialize it due to a false compiler warning. */
6750 header.signature = -1;
9c541725 6751 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6752
78d4d2c5
JK
6753 /* We need to read the type's signature in order to build the hash
6754 table, but we don't need anything else just yet. */
348e048f 6755
ed2dc618 6756 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6757 abbrev_section, ptr, section_kind);
348e048f 6758
78d4d2c5 6759 length = get_cu_length (&header);
6caca83c 6760
78d4d2c5
JK
6761 /* Skip dummy type units. */
6762 if (ptr >= info_ptr + length
43988095
JK
6763 || peek_abbrev_code (abfd, ptr) == 0
6764 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6765 {
6766 info_ptr += length;
6767 continue;
6768 }
dee91e82 6769
78d4d2c5
JK
6770 if (types_htab == NULL)
6771 {
6772 if (dwo_file)
6773 types_htab = allocate_dwo_unit_table (objfile);
6774 else
6775 types_htab = allocate_signatured_type_table (objfile);
6776 }
8b70b953 6777
78d4d2c5
JK
6778 if (dwo_file)
6779 {
6780 sig_type = NULL;
6781 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6782 struct dwo_unit);
6783 dwo_tu->dwo_file = dwo_file;
43988095 6784 dwo_tu->signature = header.signature;
9c541725 6785 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6786 dwo_tu->section = section;
9c541725 6787 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6788 dwo_tu->length = length;
6789 }
6790 else
6791 {
6792 /* N.B.: type_offset is not usable if this type uses a DWO file.
6793 The real type_offset is in the DWO file. */
6794 dwo_tu = NULL;
6795 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6796 struct signatured_type);
43988095 6797 sig_type->signature = header.signature;
9c541725 6798 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6799 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6800 sig_type->per_cu.is_debug_types = 1;
6801 sig_type->per_cu.section = section;
9c541725 6802 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6803 sig_type->per_cu.length = length;
6804 }
6805
6806 slot = htab_find_slot (types_htab,
6807 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6808 INSERT);
6809 gdb_assert (slot != NULL);
6810 if (*slot != NULL)
6811 {
9c541725 6812 sect_offset dup_sect_off;
0349ea22 6813
3019eac3
DE
6814 if (dwo_file)
6815 {
78d4d2c5
JK
6816 const struct dwo_unit *dup_tu
6817 = (const struct dwo_unit *) *slot;
6818
9c541725 6819 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6820 }
6821 else
6822 {
78d4d2c5
JK
6823 const struct signatured_type *dup_tu
6824 = (const struct signatured_type *) *slot;
6825
9c541725 6826 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6827 }
8b70b953 6828
b98664d3 6829 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6830 " the entry at offset %s, signature %s"),
6831 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6832 hex_string (header.signature));
78d4d2c5
JK
6833 }
6834 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6835
78d4d2c5 6836 if (dwarf_read_debug > 1)
9d8780f0
SM
6837 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6838 sect_offset_str (sect_off),
43988095 6839 hex_string (header.signature));
3019eac3 6840
78d4d2c5
JK
6841 info_ptr += length;
6842 }
6843}
3019eac3 6844
78d4d2c5
JK
6845/* Create the hash table of all entries in the .debug_types
6846 (or .debug_types.dwo) section(s).
6847 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6848 otherwise it is NULL.
b3c8eb43 6849
78d4d2c5 6850 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6851
78d4d2c5 6852 Note: This function processes DWO files only, not DWP files. */
348e048f 6853
78d4d2c5 6854static void
ed2dc618
SM
6855create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6856 struct dwo_file *dwo_file,
fd5866f6 6857 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6858 htab_t &types_htab)
6859{
fd5866f6
SM
6860 for (dwarf2_section_info &section : type_sections)
6861 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6862 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6863}
6864
6865/* Create the hash table of all entries in the .debug_types section,
6866 and initialize all_type_units.
6867 The result is zero if there is an error (e.g. missing .debug_types section),
6868 otherwise non-zero. */
6869
6870static int
ed2dc618 6871create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6872{
78d4d2c5 6873 htab_t types_htab = NULL;
3019eac3 6874
ed2dc618
SM
6875 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6876 &dwarf2_per_objfile->info, types_htab,
43988095 6877 rcuh_kind::COMPILE);
ed2dc618
SM
6878 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6879 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6880 if (types_htab == NULL)
6881 {
6882 dwarf2_per_objfile->signatured_types = NULL;
6883 return 0;
6884 }
6885
348e048f
DE
6886 dwarf2_per_objfile->signatured_types = types_htab;
6887
b2bdb8cf
SM
6888 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6889 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6890
6891 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6892 &dwarf2_per_objfile->all_type_units);
1fd400ff 6893
348e048f
DE
6894 return 1;
6895}
6896
6aa5f3a6
DE
6897/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6898 If SLOT is non-NULL, it is the entry to use in the hash table.
6899 Otherwise we find one. */
6900
6901static struct signatured_type *
ed2dc618
SM
6902add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6903 void **slot)
6aa5f3a6
DE
6904{
6905 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6906
b2bdb8cf
SM
6907 if (dwarf2_per_objfile->all_type_units.size ()
6908 == dwarf2_per_objfile->all_type_units.capacity ())
6909 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6910
b2bdb8cf
SM
6911 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6912 struct signatured_type);
6913
6914 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6915 sig_type->signature = sig;
6916 sig_type->per_cu.is_debug_types = 1;
6917 if (dwarf2_per_objfile->using_index)
6918 {
6919 sig_type->per_cu.v.quick =
6920 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6921 struct dwarf2_per_cu_quick_data);
6922 }
6923
6924 if (slot == NULL)
6925 {
6926 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6927 sig_type, INSERT);
6928 }
6929 gdb_assert (*slot == NULL);
6930 *slot = sig_type;
6931 /* The rest of sig_type must be filled in by the caller. */
6932 return sig_type;
6933}
6934
a2ce51a0
DE
6935/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6936 Fill in SIG_ENTRY with DWO_ENTRY. */
6937
6938static void
ed2dc618 6939fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6940 struct signatured_type *sig_entry,
6941 struct dwo_unit *dwo_entry)
6942{
7ee85ab1 6943 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6944 gdb_assert (! sig_entry->per_cu.queued);
6945 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6946 if (dwarf2_per_objfile->using_index)
6947 {
6948 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6949 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6950 }
6951 else
6952 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6953 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6954 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6955 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6956 gdb_assert (sig_entry->dwo_unit == NULL);
6957
6958 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6959 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6960 sig_entry->per_cu.length = dwo_entry->length;
6961 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6962 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6963 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6964 sig_entry->dwo_unit = dwo_entry;
6965}
6966
6967/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6968 If we haven't read the TU yet, create the signatured_type data structure
6969 for a TU to be read in directly from a DWO file, bypassing the stub.
6970 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6971 using .gdb_index, then when reading a CU we want to stay in the DWO file
6972 containing that CU. Otherwise we could end up reading several other DWO
6973 files (due to comdat folding) to process the transitive closure of all the
6974 mentioned TUs, and that can be slow. The current DWO file will have every
6975 type signature that it needs.
a2ce51a0
DE
6976 We only do this for .gdb_index because in the psymtab case we already have
6977 to read all the DWOs to build the type unit groups. */
6978
6979static struct signatured_type *
6980lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6981{
518817b3
SM
6982 struct dwarf2_per_objfile *dwarf2_per_objfile
6983 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6984 struct objfile *objfile = dwarf2_per_objfile->objfile;
6985 struct dwo_file *dwo_file;
6986 struct dwo_unit find_dwo_entry, *dwo_entry;
6987 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6988 void **slot;
a2ce51a0
DE
6989
6990 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6991
6aa5f3a6
DE
6992 /* If TU skeletons have been removed then we may not have read in any
6993 TUs yet. */
6994 if (dwarf2_per_objfile->signatured_types == NULL)
6995 {
6996 dwarf2_per_objfile->signatured_types
6997 = allocate_signatured_type_table (objfile);
6998 }
a2ce51a0
DE
6999
7000 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7001 Use the global signatured_types array to do our own comdat-folding
7002 of types. If this is the first time we're reading this TU, and
7003 the TU has an entry in .gdb_index, replace the recorded data from
7004 .gdb_index with this TU. */
a2ce51a0 7005
a2ce51a0 7006 find_sig_entry.signature = sig;
6aa5f3a6
DE
7007 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7008 &find_sig_entry, INSERT);
9a3c8263 7009 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7010
7011 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7012 read. Don't reassign the global entry to point to this DWO if that's
7013 the case. Also note that if the TU is already being read, it may not
7014 have come from a DWO, the program may be a mix of Fission-compiled
7015 code and non-Fission-compiled code. */
7016
7017 /* Have we already tried to read this TU?
7018 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7019 needn't exist in the global table yet). */
7020 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7021 return sig_entry;
7022
6aa5f3a6
DE
7023 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7024 dwo_unit of the TU itself. */
7025 dwo_file = cu->dwo_unit->dwo_file;
7026
a2ce51a0
DE
7027 /* Ok, this is the first time we're reading this TU. */
7028 if (dwo_file->tus == NULL)
7029 return NULL;
7030 find_dwo_entry.signature = sig;
9a3c8263 7031 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7032 if (dwo_entry == NULL)
7033 return NULL;
7034
6aa5f3a6
DE
7035 /* If the global table doesn't have an entry for this TU, add one. */
7036 if (sig_entry == NULL)
ed2dc618 7037 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7038
ed2dc618 7039 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7040 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7041 return sig_entry;
7042}
7043
a2ce51a0
DE
7044/* Subroutine of lookup_signatured_type.
7045 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7046 then try the DWP file. If the TU stub (skeleton) has been removed then
7047 it won't be in .gdb_index. */
a2ce51a0
DE
7048
7049static struct signatured_type *
7050lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7051{
518817b3
SM
7052 struct dwarf2_per_objfile *dwarf2_per_objfile
7053 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7054 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7055 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7056 struct dwo_unit *dwo_entry;
7057 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7058 void **slot;
a2ce51a0
DE
7059
7060 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7061 gdb_assert (dwp_file != NULL);
7062
6aa5f3a6
DE
7063 /* If TU skeletons have been removed then we may not have read in any
7064 TUs yet. */
7065 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7066 {
6aa5f3a6
DE
7067 dwarf2_per_objfile->signatured_types
7068 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7069 }
7070
6aa5f3a6
DE
7071 find_sig_entry.signature = sig;
7072 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7073 &find_sig_entry, INSERT);
9a3c8263 7074 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7075
7076 /* Have we already tried to read this TU?
7077 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7078 needn't exist in the global table yet). */
7079 if (sig_entry != NULL)
7080 return sig_entry;
7081
a2ce51a0
DE
7082 if (dwp_file->tus == NULL)
7083 return NULL;
ed2dc618 7084 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7085 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7086 if (dwo_entry == NULL)
7087 return NULL;
7088
ed2dc618
SM
7089 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7090 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7091
a2ce51a0
DE
7092 return sig_entry;
7093}
7094
380bca97 7095/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7096 Returns NULL if signature SIG is not present in the table.
7097 It is up to the caller to complain about this. */
348e048f
DE
7098
7099static struct signatured_type *
a2ce51a0 7100lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7101{
518817b3
SM
7102 struct dwarf2_per_objfile *dwarf2_per_objfile
7103 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7104
a2ce51a0
DE
7105 if (cu->dwo_unit
7106 && dwarf2_per_objfile->using_index)
7107 {
7108 /* We're in a DWO/DWP file, and we're using .gdb_index.
7109 These cases require special processing. */
ed2dc618 7110 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7111 return lookup_dwo_signatured_type (cu, sig);
7112 else
7113 return lookup_dwp_signatured_type (cu, sig);
7114 }
7115 else
7116 {
7117 struct signatured_type find_entry, *entry;
348e048f 7118
a2ce51a0
DE
7119 if (dwarf2_per_objfile->signatured_types == NULL)
7120 return NULL;
7121 find_entry.signature = sig;
9a3c8263
SM
7122 entry = ((struct signatured_type *)
7123 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7124 return entry;
7125 }
348e048f 7126}
42e7ad6c
DE
7127\f
7128/* Low level DIE reading support. */
348e048f 7129
d85a05f0
DJ
7130/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7131
7132static void
7133init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7134 struct dwarf2_cu *cu,
3019eac3 7135 struct dwarf2_section_info *section,
685af9cd
TT
7136 struct dwo_file *dwo_file,
7137 struct abbrev_table *abbrev_table)
d85a05f0 7138{
fceca515 7139 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7140 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7141 reader->cu = cu;
3019eac3 7142 reader->dwo_file = dwo_file;
dee91e82
DE
7143 reader->die_section = section;
7144 reader->buffer = section->buffer;
f664829e 7145 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7146 reader->comp_dir = NULL;
685af9cd 7147 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7148}
7149
b0c7bfa9
DE
7150/* Subroutine of init_cutu_and_read_dies to simplify it.
7151 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7152 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7153 already.
7154
7155 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7156 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7157 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7158 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7159 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7160 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7161 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7162 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7163 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7164 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7165 kept around for at least as long as *RESULT_READER.
7166
b0c7bfa9
DE
7167 The result is non-zero if a valid (non-dummy) DIE was found. */
7168
7169static int
7170read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7171 struct dwo_unit *dwo_unit,
b0c7bfa9 7172 struct die_info *stub_comp_unit_die,
a2ce51a0 7173 const char *stub_comp_dir,
b0c7bfa9 7174 struct die_reader_specs *result_reader,
d521ce57 7175 const gdb_byte **result_info_ptr,
b0c7bfa9 7176 struct die_info **result_comp_unit_die,
685af9cd
TT
7177 int *result_has_children,
7178 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7179{
ed2dc618 7180 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7181 struct objfile *objfile = dwarf2_per_objfile->objfile;
7182 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7183 bfd *abfd;
d521ce57 7184 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7185 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7186 int i,num_extra_attrs;
7187 struct dwarf2_section_info *dwo_abbrev_section;
7188 struct attribute *attr;
7189 struct die_info *comp_unit_die;
7190
b0aeadb3
DE
7191 /* At most one of these may be provided. */
7192 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7193
b0c7bfa9
DE
7194 /* These attributes aren't processed until later:
7195 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7196 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7197 referenced later. However, these attributes are found in the stub
7198 which we won't have later. In order to not impose this complication
7199 on the rest of the code, we read them here and copy them to the
7200 DWO CU/TU die. */
b0c7bfa9
DE
7201
7202 stmt_list = NULL;
7203 low_pc = NULL;
7204 high_pc = NULL;
7205 ranges = NULL;
7206 comp_dir = NULL;
7207
7208 if (stub_comp_unit_die != NULL)
7209 {
7210 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7211 DWO file. */
7212 if (! this_cu->is_debug_types)
7213 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7214 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7215 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7216 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7217 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7218
7219 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7220 We need the value before we can process DW_FORM_GNU_addr_index
7221 or DW_FORM_addrx. */
b0c7bfa9
DE
7222 cu->addr_base = 0;
7223 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7224 if (attr)
7225 cu->addr_base = DW_UNSND (attr);
7226
7227 /* There should be a DW_AT_ranges_base attribute here (if needed).
7228 We need the value before we can process DW_AT_ranges. */
7229 cu->ranges_base = 0;
7230 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7231 if (attr)
7232 cu->ranges_base = DW_UNSND (attr);
7233 }
a2ce51a0
DE
7234 else if (stub_comp_dir != NULL)
7235 {
7236 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7237 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7238 comp_dir->name = DW_AT_comp_dir;
7239 comp_dir->form = DW_FORM_string;
7240 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7241 DW_STRING (comp_dir) = stub_comp_dir;
7242 }
b0c7bfa9
DE
7243
7244 /* Set up for reading the DWO CU/TU. */
7245 cu->dwo_unit = dwo_unit;
685af9cd 7246 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7247 dwarf2_read_section (objfile, section);
a32a8923 7248 abfd = get_section_bfd_owner (section);
9c541725
PA
7249 begin_info_ptr = info_ptr = (section->buffer
7250 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7251 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7252
7253 if (this_cu->is_debug_types)
7254 {
b0c7bfa9
DE
7255 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7256
ed2dc618
SM
7257 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7258 &cu->header, section,
b0c7bfa9 7259 dwo_abbrev_section,
43988095 7260 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7261 /* This is not an assert because it can be caused by bad debug info. */
43988095 7262 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7263 {
7264 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7265 " TU at offset %s [in module %s]"),
a2ce51a0 7266 hex_string (sig_type->signature),
43988095 7267 hex_string (cu->header.signature),
9d8780f0 7268 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7269 bfd_get_filename (abfd));
7270 }
9c541725 7271 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7272 /* For DWOs coming from DWP files, we don't know the CU length
7273 nor the type's offset in the TU until now. */
7274 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7275 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7276
7277 /* Establish the type offset that can be used to lookup the type.
7278 For DWO files, we don't know it until now. */
9c541725
PA
7279 sig_type->type_offset_in_section
7280 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7281 }
7282 else
7283 {
ed2dc618
SM
7284 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7285 &cu->header, section,
b0c7bfa9 7286 dwo_abbrev_section,
43988095 7287 info_ptr, rcuh_kind::COMPILE);
9c541725 7288 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7289 /* For DWOs coming from DWP files, we don't know the CU length
7290 until now. */
7291 dwo_unit->length = get_cu_length (&cu->header);
7292 }
7293
685af9cd
TT
7294 *result_dwo_abbrev_table
7295 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7296 cu->header.abbrev_sect_off);
7297 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7298 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7299
7300 /* Read in the die, but leave space to copy over the attributes
7301 from the stub. This has the benefit of simplifying the rest of
7302 the code - all the work to maintain the illusion of a single
7303 DW_TAG_{compile,type}_unit DIE is done here. */
7304 num_extra_attrs = ((stmt_list != NULL)
7305 + (low_pc != NULL)
7306 + (high_pc != NULL)
7307 + (ranges != NULL)
7308 + (comp_dir != NULL));
7309 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7310 result_has_children, num_extra_attrs);
7311
7312 /* Copy over the attributes from the stub to the DIE we just read in. */
7313 comp_unit_die = *result_comp_unit_die;
7314 i = comp_unit_die->num_attrs;
7315 if (stmt_list != NULL)
7316 comp_unit_die->attrs[i++] = *stmt_list;
7317 if (low_pc != NULL)
7318 comp_unit_die->attrs[i++] = *low_pc;
7319 if (high_pc != NULL)
7320 comp_unit_die->attrs[i++] = *high_pc;
7321 if (ranges != NULL)
7322 comp_unit_die->attrs[i++] = *ranges;
7323 if (comp_dir != NULL)
7324 comp_unit_die->attrs[i++] = *comp_dir;
7325 comp_unit_die->num_attrs += num_extra_attrs;
7326
b4f54984 7327 if (dwarf_die_debug)
bf6af496
DE
7328 {
7329 fprintf_unfiltered (gdb_stdlog,
7330 "Read die from %s@0x%x of %s:\n",
a32a8923 7331 get_section_name (section),
bf6af496
DE
7332 (unsigned) (begin_info_ptr - section->buffer),
7333 bfd_get_filename (abfd));
b4f54984 7334 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7335 }
7336
a2ce51a0
DE
7337 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7338 TUs by skipping the stub and going directly to the entry in the DWO file.
7339 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7340 to get it via circuitous means. Blech. */
7341 if (comp_dir != NULL)
7342 result_reader->comp_dir = DW_STRING (comp_dir);
7343
b0c7bfa9
DE
7344 /* Skip dummy compilation units. */
7345 if (info_ptr >= begin_info_ptr + dwo_unit->length
7346 || peek_abbrev_code (abfd, info_ptr) == 0)
7347 return 0;
7348
7349 *result_info_ptr = info_ptr;
7350 return 1;
7351}
7352
a084a2a6
AT
7353/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7354 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7355 signature is part of the header. */
7356static gdb::optional<ULONGEST>
7357lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7358{
7359 if (cu->header.version >= 5)
7360 return cu->header.signature;
7361 struct attribute *attr;
7362 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7363 if (attr == nullptr)
7364 return gdb::optional<ULONGEST> ();
7365 return DW_UNSND (attr);
7366}
7367
b0c7bfa9
DE
7368/* Subroutine of init_cutu_and_read_dies to simplify it.
7369 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7370 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7371
7372static struct dwo_unit *
7373lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7374 struct die_info *comp_unit_die)
7375{
7376 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7377 struct dwo_unit *dwo_unit;
7378 const char *comp_dir, *dwo_name;
7379
a2ce51a0
DE
7380 gdb_assert (cu != NULL);
7381
b0c7bfa9 7382 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7383 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7384 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7385
7386 if (this_cu->is_debug_types)
7387 {
7388 struct signatured_type *sig_type;
7389
7390 /* Since this_cu is the first member of struct signatured_type,
7391 we can go from a pointer to one to a pointer to the other. */
7392 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7393 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7394 }
7395 else
7396 {
a084a2a6
AT
7397 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7398 if (!signature.has_value ())
b0c7bfa9
DE
7399 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7400 " [in module %s]"),
e3b94546 7401 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7402 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7403 *signature);
b0c7bfa9
DE
7404 }
7405
b0c7bfa9
DE
7406 return dwo_unit;
7407}
7408
a2ce51a0 7409/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7410 See it for a description of the parameters.
fcd3b13d 7411 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7412
7413static void
6aa5f3a6
DE
7414init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7415 int use_existing_cu, int keep,
a2ce51a0
DE
7416 die_reader_func_ftype *die_reader_func,
7417 void *data)
7418{
fcd3b13d 7419 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7420 struct signatured_type *sig_type;
a2ce51a0
DE
7421 struct die_reader_specs reader;
7422 const gdb_byte *info_ptr;
7423 struct die_info *comp_unit_die;
7424 int has_children;
ed2dc618 7425 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7426
7427 /* Verify we can do the following downcast, and that we have the
7428 data we need. */
7429 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7430 sig_type = (struct signatured_type *) this_cu;
7431 gdb_assert (sig_type->dwo_unit != NULL);
7432
6aa5f3a6
DE
7433 if (use_existing_cu && this_cu->cu != NULL)
7434 {
7435 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7436 /* There's no need to do the rereading_dwo_cu handling that
7437 init_cutu_and_read_dies does since we don't read the stub. */
7438 }
7439 else
7440 {
7441 /* If !use_existing_cu, this_cu->cu must be NULL. */
7442 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7443 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7444 }
7445
7446 /* A future optimization, if needed, would be to use an existing
7447 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7448 could share abbrev tables. */
a2ce51a0 7449
685af9cd
TT
7450 /* The abbreviation table used by READER, this must live at least as long as
7451 READER. */
7452 abbrev_table_up dwo_abbrev_table;
7453
a2ce51a0 7454 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7455 NULL /* stub_comp_unit_die */,
7456 sig_type->dwo_unit->dwo_file->comp_dir,
7457 &reader, &info_ptr,
685af9cd
TT
7458 &comp_unit_die, &has_children,
7459 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7460 {
7461 /* Dummy die. */
a2ce51a0
DE
7462 return;
7463 }
7464
7465 /* All the "real" work is done here. */
7466 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7467
6aa5f3a6 7468 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7469 but the alternative is making the latter more complex.
7470 This function is only for the special case of using DWO files directly:
7471 no point in overly complicating the general case just to handle this. */
fcd3b13d 7472 if (new_cu != NULL && keep)
a2ce51a0 7473 {
fcd3b13d
SM
7474 /* Link this CU into read_in_chain. */
7475 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7476 dwarf2_per_objfile->read_in_chain = this_cu;
7477 /* The chain owns it now. */
7478 new_cu.release ();
a2ce51a0 7479 }
a2ce51a0
DE
7480}
7481
fd820528 7482/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7483 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7484
f4dc4d17
DE
7485 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7486 Otherwise the table specified in the comp unit header is read in and used.
7487 This is an optimization for when we already have the abbrev table.
7488
dee91e82
DE
7489 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7490 Otherwise, a new CU is allocated with xmalloc.
7491
7492 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7493 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7494
7495 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7496 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7497
70221824 7498static void
fd820528 7499init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7500 struct abbrev_table *abbrev_table,
fd820528 7501 int use_existing_cu, int keep,
58f0c718 7502 bool skip_partial,
fd820528
DE
7503 die_reader_func_ftype *die_reader_func,
7504 void *data)
c906108c 7505{
ed2dc618 7506 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7507 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7508 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7509 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7510 struct dwarf2_cu *cu;
d521ce57 7511 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7512 struct die_reader_specs reader;
d85a05f0 7513 struct die_info *comp_unit_die;
dee91e82 7514 int has_children;
dee91e82 7515 struct signatured_type *sig_type = NULL;
4bdcc0c1 7516 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7517 /* Non-zero if CU currently points to a DWO file and we need to
7518 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7519 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7520 int rereading_dwo_cu = 0;
c906108c 7521
b4f54984 7522 if (dwarf_die_debug)
9d8780f0 7523 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7524 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7525 sect_offset_str (this_cu->sect_off));
09406207 7526
dee91e82
DE
7527 if (use_existing_cu)
7528 gdb_assert (keep);
23745b47 7529
a2ce51a0
DE
7530 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7531 file (instead of going through the stub), short-circuit all of this. */
7532 if (this_cu->reading_dwo_directly)
7533 {
7534 /* Narrow down the scope of possibilities to have to understand. */
7535 gdb_assert (this_cu->is_debug_types);
7536 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7537 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7538 die_reader_func, data);
a2ce51a0
DE
7539 return;
7540 }
7541
dee91e82
DE
7542 /* This is cheap if the section is already read in. */
7543 dwarf2_read_section (objfile, section);
7544
9c541725 7545 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7546
7547 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7548
fcd3b13d 7549 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7550 if (use_existing_cu && this_cu->cu != NULL)
7551 {
7552 cu = this_cu->cu;
42e7ad6c
DE
7553 /* If this CU is from a DWO file we need to start over, we need to
7554 refetch the attributes from the skeleton CU.
7555 This could be optimized by retrieving those attributes from when we
7556 were here the first time: the previous comp_unit_die was stored in
7557 comp_unit_obstack. But there's no data yet that we need this
7558 optimization. */
7559 if (cu->dwo_unit != NULL)
7560 rereading_dwo_cu = 1;
dee91e82
DE
7561 }
7562 else
7563 {
7564 /* If !use_existing_cu, this_cu->cu must be NULL. */
7565 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7566 new_cu.reset (new dwarf2_cu (this_cu));
7567 cu = new_cu.get ();
42e7ad6c 7568 }
dee91e82 7569
b0c7bfa9 7570 /* Get the header. */
9c541725 7571 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7572 {
7573 /* We already have the header, there's no need to read it in again. */
9c541725 7574 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7575 }
7576 else
7577 {
3019eac3 7578 if (this_cu->is_debug_types)
dee91e82 7579 {
ed2dc618
SM
7580 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7581 &cu->header, section,
4bdcc0c1 7582 abbrev_section, info_ptr,
43988095 7583 rcuh_kind::TYPE);
dee91e82 7584
42e7ad6c
DE
7585 /* Since per_cu is the first member of struct signatured_type,
7586 we can go from a pointer to one to a pointer to the other. */
7587 sig_type = (struct signatured_type *) this_cu;
43988095 7588 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7589 gdb_assert (sig_type->type_offset_in_tu
7590 == cu->header.type_cu_offset_in_tu);
7591 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7592
42e7ad6c
DE
7593 /* LENGTH has not been set yet for type units if we're
7594 using .gdb_index. */
1ce1cefd 7595 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7596
7597 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7598 sig_type->type_offset_in_section =
7599 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7600
7601 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7602 }
7603 else
7604 {
ed2dc618
SM
7605 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7606 &cu->header, section,
4bdcc0c1 7607 abbrev_section,
43988095
JK
7608 info_ptr,
7609 rcuh_kind::COMPILE);
dee91e82 7610
9c541725 7611 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7612 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7613 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7614 }
7615 }
10b3939b 7616
6caca83c 7617 /* Skip dummy compilation units. */
dee91e82 7618 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7619 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7620 return;
6caca83c 7621
433df2d4
DE
7622 /* If we don't have them yet, read the abbrevs for this compilation unit.
7623 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7624 done (own the table through ABBREV_TABLE_HOLDER). */
7625 abbrev_table_up abbrev_table_holder;
f4dc4d17 7626 if (abbrev_table != NULL)
685af9cd
TT
7627 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7628 else
f4dc4d17 7629 {
685af9cd
TT
7630 abbrev_table_holder
7631 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7632 cu->header.abbrev_sect_off);
7633 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7634 }
af703f96 7635
dee91e82 7636 /* Read the top level CU/TU die. */
685af9cd 7637 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7638 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7639
58f0c718
TT
7640 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7641 return;
7642
b0c7bfa9 7643 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7644 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7645 table from the DWO file and pass the ownership over to us. It will be
7646 referenced from READER, so we must make sure to free it after we're done
7647 with READER.
7648
b0c7bfa9
DE
7649 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7650 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7651 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7652 abbrev_table_up dwo_abbrev_table;
a084a2a6 7653 if (dwo_name != nullptr)
3019eac3 7654 {
3019eac3 7655 struct dwo_unit *dwo_unit;
b0c7bfa9 7656 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7657
7658 if (has_children)
6a506a2d 7659 {
b98664d3 7660 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7661 " has children (offset %s) [in module %s]"),
7662 sect_offset_str (this_cu->sect_off),
7663 bfd_get_filename (abfd));
6a506a2d 7664 }
b0c7bfa9 7665 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7666 if (dwo_unit != NULL)
3019eac3 7667 {
6a506a2d 7668 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7669 comp_unit_die, NULL,
6a506a2d 7670 &reader, &info_ptr,
685af9cd
TT
7671 &dwo_comp_unit_die, &has_children,
7672 &dwo_abbrev_table) == 0)
6a506a2d
DE
7673 {
7674 /* Dummy die. */
6a506a2d
DE
7675 return;
7676 }
7677 comp_unit_die = dwo_comp_unit_die;
7678 }
7679 else
7680 {
7681 /* Yikes, we couldn't find the rest of the DIE, we only have
7682 the stub. A complaint has already been logged. There's
7683 not much more we can do except pass on the stub DIE to
7684 die_reader_func. We don't want to throw an error on bad
7685 debug info. */
3019eac3
DE
7686 }
7687 }
7688
b0c7bfa9 7689 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7690 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7691
b0c7bfa9 7692 /* Done, clean up. */
fcd3b13d 7693 if (new_cu != NULL && keep)
348e048f 7694 {
fcd3b13d
SM
7695 /* Link this CU into read_in_chain. */
7696 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7697 dwarf2_per_objfile->read_in_chain = this_cu;
7698 /* The chain owns it now. */
7699 new_cu.release ();
348e048f 7700 }
dee91e82
DE
7701}
7702
33e80786
DE
7703/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7704 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7705 to have already done the lookup to find the DWO file).
dee91e82
DE
7706
7707 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7708 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7709
7710 We fill in THIS_CU->length.
7711
7712 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7713 linker) then DIE_READER_FUNC will not get called.
7714
7715 THIS_CU->cu is always freed when done.
3019eac3
DE
7716 This is done in order to not leave THIS_CU->cu in a state where we have
7717 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7718
7719static void
7720init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7721 struct dwo_file *dwo_file,
dee91e82
DE
7722 die_reader_func_ftype *die_reader_func,
7723 void *data)
7724{
ed2dc618 7725 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7726 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7727 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7728 bfd *abfd = get_section_bfd_owner (section);
33e80786 7729 struct dwarf2_section_info *abbrev_section;
d521ce57 7730 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7731 struct die_reader_specs reader;
dee91e82
DE
7732 struct die_info *comp_unit_die;
7733 int has_children;
7734
b4f54984 7735 if (dwarf_die_debug)
9d8780f0 7736 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7737 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7738 sect_offset_str (this_cu->sect_off));
09406207 7739
dee91e82
DE
7740 gdb_assert (this_cu->cu == NULL);
7741
33e80786
DE
7742 abbrev_section = (dwo_file != NULL
7743 ? &dwo_file->sections.abbrev
7744 : get_abbrev_section_for_cu (this_cu));
7745
dee91e82
DE
7746 /* This is cheap if the section is already read in. */
7747 dwarf2_read_section (objfile, section);
7748
fcd3b13d 7749 struct dwarf2_cu cu (this_cu);
dee91e82 7750
9c541725 7751 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7752 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7753 &cu.header, section,
4bdcc0c1 7754 abbrev_section, info_ptr,
43988095
JK
7755 (this_cu->is_debug_types
7756 ? rcuh_kind::TYPE
7757 : rcuh_kind::COMPILE));
dee91e82 7758
1ce1cefd 7759 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7760
7761 /* Skip dummy compilation units. */
7762 if (info_ptr >= begin_info_ptr + this_cu->length
7763 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7764 return;
72bf9492 7765
685af9cd
TT
7766 abbrev_table_up abbrev_table
7767 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7768 cu.header.abbrev_sect_off);
dee91e82 7769
685af9cd 7770 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7771 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7772
7773 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7774}
7775
3019eac3
DE
7776/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7777 does not lookup the specified DWO file.
7778 This cannot be used to read DWO files.
dee91e82
DE
7779
7780 THIS_CU->cu is always freed when done.
3019eac3
DE
7781 This is done in order to not leave THIS_CU->cu in a state where we have
7782 to care whether it refers to the "main" CU or the DWO CU.
7783 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7784
7785static void
7786init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7787 die_reader_func_ftype *die_reader_func,
7788 void *data)
7789{
33e80786 7790 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7791}
0018ea6f
DE
7792\f
7793/* Type Unit Groups.
dee91e82 7794
0018ea6f
DE
7795 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7796 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7797 so that all types coming from the same compilation (.o file) are grouped
7798 together. A future step could be to put the types in the same symtab as
7799 the CU the types ultimately came from. */
ff013f42 7800
f4dc4d17
DE
7801static hashval_t
7802hash_type_unit_group (const void *item)
7803{
9a3c8263
SM
7804 const struct type_unit_group *tu_group
7805 = (const struct type_unit_group *) item;
f4dc4d17 7806
094b34ac 7807 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7808}
348e048f
DE
7809
7810static int
f4dc4d17 7811eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7812{
9a3c8263
SM
7813 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7814 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7815
094b34ac 7816 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7817}
348e048f 7818
f4dc4d17
DE
7819/* Allocate a hash table for type unit groups. */
7820
7821static htab_t
ed2dc618 7822allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7823{
7824 return htab_create_alloc_ex (3,
7825 hash_type_unit_group,
7826 eq_type_unit_group,
7827 NULL,
ed2dc618 7828 &objfile->objfile_obstack,
f4dc4d17
DE
7829 hashtab_obstack_allocate,
7830 dummy_obstack_deallocate);
7831}
dee91e82 7832
f4dc4d17
DE
7833/* Type units that don't have DW_AT_stmt_list are grouped into their own
7834 partial symtabs. We combine several TUs per psymtab to not let the size
7835 of any one psymtab grow too big. */
7836#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7837#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7838
094b34ac 7839/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7840 Create the type_unit_group object used to hold one or more TUs. */
7841
7842static struct type_unit_group *
094b34ac 7843create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7844{
518817b3
SM
7845 struct dwarf2_per_objfile *dwarf2_per_objfile
7846 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7847 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7848 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7849 struct type_unit_group *tu_group;
f4dc4d17
DE
7850
7851 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7852 struct type_unit_group);
094b34ac 7853 per_cu = &tu_group->per_cu;
518817b3 7854 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7855
094b34ac
DE
7856 if (dwarf2_per_objfile->using_index)
7857 {
7858 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7859 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7860 }
7861 else
7862 {
9c541725 7863 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7864 struct partial_symtab *pst;
528e1572 7865 std::string name;
094b34ac
DE
7866
7867 /* Give the symtab a useful name for debug purposes. */
7868 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7869 name = string_printf ("<type_units_%d>",
7870 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7871 else
528e1572 7872 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7873
528e1572 7874 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7875 pst->anonymous = 1;
094b34ac 7876 }
f4dc4d17 7877
094b34ac 7878 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7879 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7880
7881 return tu_group;
7882}
7883
094b34ac
DE
7884/* Look up the type_unit_group for type unit CU, and create it if necessary.
7885 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7886
7887static struct type_unit_group *
ff39bb5e 7888get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7889{
518817b3
SM
7890 struct dwarf2_per_objfile *dwarf2_per_objfile
7891 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7892 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7893 struct type_unit_group *tu_group;
7894 void **slot;
7895 unsigned int line_offset;
7896 struct type_unit_group type_unit_group_for_lookup;
7897
7898 if (dwarf2_per_objfile->type_unit_groups == NULL)
7899 {
7900 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7901 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7902 }
7903
7904 /* Do we need to create a new group, or can we use an existing one? */
7905
7906 if (stmt_list)
7907 {
7908 line_offset = DW_UNSND (stmt_list);
7909 ++tu_stats->nr_symtab_sharers;
7910 }
7911 else
7912 {
7913 /* Ugh, no stmt_list. Rare, but we have to handle it.
7914 We can do various things here like create one group per TU or
7915 spread them over multiple groups to split up the expansion work.
7916 To avoid worst case scenarios (too many groups or too large groups)
7917 we, umm, group them in bunches. */
7918 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7919 | (tu_stats->nr_stmt_less_type_units
7920 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7921 ++tu_stats->nr_stmt_less_type_units;
7922 }
7923
094b34ac 7924 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7925 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7926 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7927 &type_unit_group_for_lookup, INSERT);
7928 if (*slot != NULL)
7929 {
9a3c8263 7930 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7931 gdb_assert (tu_group != NULL);
7932 }
7933 else
7934 {
9c541725 7935 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7936 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7937 *slot = tu_group;
7938 ++tu_stats->nr_symtabs;
7939 }
7940
7941 return tu_group;
7942}
0018ea6f
DE
7943\f
7944/* Partial symbol tables. */
7945
7946/* Create a psymtab named NAME and assign it to PER_CU.
7947
7948 The caller must fill in the following details:
7949 dirname, textlow, texthigh. */
7950
7951static struct partial_symtab *
7952create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7953{
e3b94546 7954 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7955 struct partial_symtab *pst;
7956
939652a5 7957 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
7958
7959 pst->psymtabs_addrmap_supported = 1;
7960
7961 /* This is the glue that links PST into GDB's symbol API. */
7962 pst->read_symtab_private = per_cu;
7963 pst->read_symtab = dwarf2_read_symtab;
7964 per_cu->v.psymtab = pst;
7965
7966 return pst;
7967}
7968
b93601f3
TT
7969/* The DATA object passed to process_psymtab_comp_unit_reader has this
7970 type. */
7971
7972struct process_psymtab_comp_unit_data
7973{
7974 /* True if we are reading a DW_TAG_partial_unit. */
7975
7976 int want_partial_unit;
7977
7978 /* The "pretend" language that is used if the CU doesn't declare a
7979 language. */
7980
7981 enum language pretend_language;
7982};
7983
0018ea6f
DE
7984/* die_reader_func for process_psymtab_comp_unit. */
7985
7986static void
7987process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7988 const gdb_byte *info_ptr,
0018ea6f
DE
7989 struct die_info *comp_unit_die,
7990 int has_children,
7991 void *data)
7992{
7993 struct dwarf2_cu *cu = reader->cu;
518817b3 7994 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7995 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7996 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7997 CORE_ADDR baseaddr;
7998 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7999 struct partial_symtab *pst;
3a2b436a 8000 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8001 const char *filename;
9a3c8263
SM
8002 struct process_psymtab_comp_unit_data *info
8003 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8004
b93601f3 8005 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8006 return;
8007
8008 gdb_assert (! per_cu->is_debug_types);
8009
b93601f3 8010 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8011
0018ea6f 8012 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8013 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8014 if (filename == NULL)
0018ea6f 8015 filename = "";
0018ea6f
DE
8016
8017 pst = create_partial_symtab (per_cu, filename);
8018
8019 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8020 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8021
8022 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8023
8024 dwarf2_find_base_address (comp_unit_die, cu);
8025
8026 /* Possibly set the default values of LOWPC and HIGHPC from
8027 `DW_AT_ranges'. */
3a2b436a
JK
8028 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8029 &best_highpc, cu, pst);
8030 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8031 {
8032 CORE_ADDR low
8033 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8034 - baseaddr);
8035 CORE_ADDR high
8036 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8037 - baseaddr - 1);
8038 /* Store the contiguous range if it is not empty; it can be
8039 empty for CUs with no code. */
d320c2b5
TT
8040 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8041 low, high, pst);
79748972 8042 }
0018ea6f
DE
8043
8044 /* Check if comp unit has_children.
8045 If so, read the rest of the partial symbols from this comp unit.
8046 If not, there's no more debug_info for this comp unit. */
8047 if (has_children)
8048 {
8049 struct partial_die_info *first_die;
8050 CORE_ADDR lowpc, highpc;
8051
8052 lowpc = ((CORE_ADDR) -1);
8053 highpc = ((CORE_ADDR) 0);
8054
8055 first_die = load_partial_dies (reader, info_ptr, 1);
8056
8057 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8058 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8059
8060 /* If we didn't find a lowpc, set it to highpc to avoid
8061 complaints from `maint check'. */
8062 if (lowpc == ((CORE_ADDR) -1))
8063 lowpc = highpc;
8064
8065 /* If the compilation unit didn't have an explicit address range,
8066 then use the information extracted from its child dies. */
e385593e 8067 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8068 {
8069 best_lowpc = lowpc;
8070 best_highpc = highpc;
8071 }
8072 }
4ae976d1 8073 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8074 best_lowpc + baseaddr)
8075 - baseaddr);
4ae976d1 8076 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8077 best_highpc + baseaddr)
8078 - baseaddr);
0018ea6f 8079
8763cede 8080 end_psymtab_common (objfile, pst);
0018ea6f 8081
ae640021 8082 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8083 {
8084 int i;
ae640021 8085 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8086
8087 /* Fill in 'dependencies' here; we fill in 'users' in a
8088 post-pass. */
8089 pst->number_of_dependencies = len;
a9342b62
TT
8090 pst->dependencies
8091 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8092 for (i = 0; i < len; ++i)
8093 {
8094 pst->dependencies[i]
8095 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8096 }
0018ea6f 8097
ae640021 8098 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8099 }
8100
8101 /* Get the list of files included in the current compilation unit,
8102 and build a psymtab for each of them. */
8103 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8104
b4f54984 8105 if (dwarf_read_debug)
b926417a
TT
8106 fprintf_unfiltered (gdb_stdlog,
8107 "Psymtab for %s unit @%s: %s - %s"
8108 ", %d global, %d static syms\n",
8109 per_cu->is_debug_types ? "type" : "comp",
8110 sect_offset_str (per_cu->sect_off),
8111 paddress (gdbarch, pst->text_low (objfile)),
8112 paddress (gdbarch, pst->text_high (objfile)),
8113 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8114}
8115
8116/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8117 Process compilation unit THIS_CU for a psymtab. */
8118
8119static void
8120process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8121 int want_partial_unit,
8122 enum language pretend_language)
0018ea6f
DE
8123{
8124 /* If this compilation unit was already read in, free the
8125 cached copy in order to read it in again. This is
8126 necessary because we skipped some symbols when we first
8127 read in the compilation unit (see load_partial_dies).
8128 This problem could be avoided, but the benefit is unclear. */
8129 if (this_cu->cu != NULL)
8130 free_one_cached_comp_unit (this_cu);
8131
f1902523 8132 if (this_cu->is_debug_types)
58f0c718
TT
8133 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8134 build_type_psymtabs_reader, NULL);
f1902523
JK
8135 else
8136 {
8137 process_psymtab_comp_unit_data info;
8138 info.want_partial_unit = want_partial_unit;
8139 info.pretend_language = pretend_language;
58f0c718 8140 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8141 process_psymtab_comp_unit_reader, &info);
8142 }
0018ea6f
DE
8143
8144 /* Age out any secondary CUs. */
ed2dc618 8145 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8146}
f4dc4d17
DE
8147
8148/* Reader function for build_type_psymtabs. */
8149
8150static void
8151build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8152 const gdb_byte *info_ptr,
f4dc4d17
DE
8153 struct die_info *type_unit_die,
8154 int has_children,
8155 void *data)
8156{
ed2dc618 8157 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8158 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8159 struct objfile *objfile = dwarf2_per_objfile->objfile;
8160 struct dwarf2_cu *cu = reader->cu;
8161 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8162 struct signatured_type *sig_type;
f4dc4d17
DE
8163 struct type_unit_group *tu_group;
8164 struct attribute *attr;
8165 struct partial_die_info *first_die;
8166 CORE_ADDR lowpc, highpc;
8167 struct partial_symtab *pst;
8168
8169 gdb_assert (data == NULL);
0186c6a7
DE
8170 gdb_assert (per_cu->is_debug_types);
8171 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8172
8173 if (! has_children)
8174 return;
8175
8176 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8177 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8178
df07e2c7 8179 if (tu_group->tus == nullptr)
a8b3b8e9 8180 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8181 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8182
8183 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8184 pst = create_partial_symtab (per_cu, "");
8185 pst->anonymous = 1;
8186
8187 first_die = load_partial_dies (reader, info_ptr, 1);
8188
8189 lowpc = (CORE_ADDR) -1;
8190 highpc = (CORE_ADDR) 0;
8191 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8192
8763cede 8193 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8194}
8195
73051182
DE
8196/* Struct used to sort TUs by their abbreviation table offset. */
8197
8198struct tu_abbrev_offset
8199{
b2bdb8cf
SM
8200 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8201 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8202 {}
8203
8204 signatured_type *sig_type;
73051182
DE
8205 sect_offset abbrev_offset;
8206};
8207
484cf504 8208/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8209
484cf504
TT
8210static bool
8211sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8212 const struct tu_abbrev_offset &b)
73051182 8213{
484cf504 8214 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8215}
8216
8217/* Efficiently read all the type units.
8218 This does the bulk of the work for build_type_psymtabs.
8219
8220 The efficiency is because we sort TUs by the abbrev table they use and
8221 only read each abbrev table once. In one program there are 200K TUs
8222 sharing 8K abbrev tables.
8223
8224 The main purpose of this function is to support building the
8225 dwarf2_per_objfile->type_unit_groups table.
8226 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8227 can collapse the search space by grouping them by stmt_list.
8228 The savings can be significant, in the same program from above the 200K TUs
8229 share 8K stmt_list tables.
8230
8231 FUNC is expected to call get_type_unit_group, which will create the
8232 struct type_unit_group if necessary and add it to
8233 dwarf2_per_objfile->type_unit_groups. */
8234
8235static void
ed2dc618 8236build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8237{
73051182 8238 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8239 abbrev_table_up abbrev_table;
73051182 8240 sect_offset abbrev_offset;
73051182
DE
8241
8242 /* It's up to the caller to not call us multiple times. */
8243 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8244
b2bdb8cf 8245 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8246 return;
8247
8248 /* TUs typically share abbrev tables, and there can be way more TUs than
8249 abbrev tables. Sort by abbrev table to reduce the number of times we
8250 read each abbrev table in.
8251 Alternatives are to punt or to maintain a cache of abbrev tables.
8252 This is simpler and efficient enough for now.
8253
8254 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8255 symtab to use). Typically TUs with the same abbrev offset have the same
8256 stmt_list value too so in practice this should work well.
8257
8258 The basic algorithm here is:
8259
8260 sort TUs by abbrev table
8261 for each TU with same abbrev table:
8262 read abbrev table if first user
8263 read TU top level DIE
8264 [IWBN if DWO skeletons had DW_AT_stmt_list]
8265 call FUNC */
8266
b4f54984 8267 if (dwarf_read_debug)
73051182
DE
8268 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8269
8270 /* Sort in a separate table to maintain the order of all_type_units
8271 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8272 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8273 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8274
8275 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8276 sorted_by_abbrev.emplace_back
8277 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8278 sig_type->per_cu.section,
8279 sig_type->per_cu.sect_off));
73051182 8280
484cf504
TT
8281 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8282 sort_tu_by_abbrev_offset);
73051182 8283
9c541725 8284 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8285
b2bdb8cf 8286 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8287 {
73051182
DE
8288 /* Switch to the next abbrev table if necessary. */
8289 if (abbrev_table == NULL
b2bdb8cf 8290 || tu.abbrev_offset != abbrev_offset)
73051182 8291 {
b2bdb8cf 8292 abbrev_offset = tu.abbrev_offset;
73051182 8293 abbrev_table =
ed2dc618
SM
8294 abbrev_table_read_table (dwarf2_per_objfile,
8295 &dwarf2_per_objfile->abbrev,
73051182
DE
8296 abbrev_offset);
8297 ++tu_stats->nr_uniq_abbrev_tables;
8298 }
8299
b2bdb8cf 8300 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8301 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8302 }
6aa5f3a6 8303}
73051182 8304
6aa5f3a6
DE
8305/* Print collected type unit statistics. */
8306
8307static void
ed2dc618 8308print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8309{
8310 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8311
8312 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8313 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8314 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8315 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8316 tu_stats->nr_uniq_abbrev_tables);
8317 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8318 tu_stats->nr_symtabs);
8319 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8320 tu_stats->nr_symtab_sharers);
8321 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8322 tu_stats->nr_stmt_less_type_units);
8323 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8324 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8325}
8326
f4dc4d17
DE
8327/* Traversal function for build_type_psymtabs. */
8328
8329static int
8330build_type_psymtab_dependencies (void **slot, void *info)
8331{
ed2dc618
SM
8332 struct dwarf2_per_objfile *dwarf2_per_objfile
8333 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8334 struct objfile *objfile = dwarf2_per_objfile->objfile;
8335 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8336 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8337 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8338 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8339 int i;
8340
8341 gdb_assert (len > 0);
0186c6a7 8342 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8343
8344 pst->number_of_dependencies = len;
a9342b62 8345 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8346 for (i = 0; i < len; ++i)
f4dc4d17 8347 {
df07e2c7 8348 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8349 gdb_assert (iter->per_cu.is_debug_types);
8350 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8351 iter->type_unit_group = tu_group;
f4dc4d17
DE
8352 }
8353
df07e2c7
AB
8354 delete tu_group->tus;
8355 tu_group->tus = nullptr;
348e048f
DE
8356
8357 return 1;
8358}
8359
8360/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8361 Build partial symbol tables for the .debug_types comp-units. */
8362
8363static void
ed2dc618 8364build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8365{
ed2dc618 8366 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8367 return;
8368
ed2dc618 8369 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8370}
f4dc4d17 8371
6aa5f3a6
DE
8372/* Traversal function for process_skeletonless_type_unit.
8373 Read a TU in a DWO file and build partial symbols for it. */
8374
8375static int
8376process_skeletonless_type_unit (void **slot, void *info)
8377{
8378 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8379 struct dwarf2_per_objfile *dwarf2_per_objfile
8380 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8381 struct signatured_type find_entry, *entry;
8382
8383 /* If this TU doesn't exist in the global table, add it and read it in. */
8384
8385 if (dwarf2_per_objfile->signatured_types == NULL)
8386 {
8387 dwarf2_per_objfile->signatured_types
ed2dc618 8388 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8389 }
8390
8391 find_entry.signature = dwo_unit->signature;
8392 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8393 INSERT);
8394 /* If we've already seen this type there's nothing to do. What's happening
8395 is we're doing our own version of comdat-folding here. */
8396 if (*slot != NULL)
8397 return 1;
8398
8399 /* This does the job that create_all_type_units would have done for
8400 this TU. */
ed2dc618
SM
8401 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8402 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8403 *slot = entry;
8404
8405 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8406 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8407 build_type_psymtabs_reader, NULL);
8408
8409 return 1;
8410}
8411
8412/* Traversal function for process_skeletonless_type_units. */
8413
8414static int
8415process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8416{
8417 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8418
8419 if (dwo_file->tus != NULL)
8420 {
8421 htab_traverse_noresize (dwo_file->tus,
8422 process_skeletonless_type_unit, info);
8423 }
8424
8425 return 1;
8426}
8427
8428/* Scan all TUs of DWO files, verifying we've processed them.
8429 This is needed in case a TU was emitted without its skeleton.
8430 Note: This can't be done until we know what all the DWO files are. */
8431
8432static void
ed2dc618 8433process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8434{
8435 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8436 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8437 && dwarf2_per_objfile->dwo_files != NULL)
8438 {
51ac9db5 8439 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8440 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8441 dwarf2_per_objfile);
6aa5f3a6 8442 }
348e048f
DE
8443}
8444
ed2dc618 8445/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8446
8447static void
ed2dc618 8448set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8449{
b76e467d 8450 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8451 {
95554aad 8452 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8453
36586728
TT
8454 if (pst == NULL)
8455 continue;
8456
b76e467d 8457 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8458 {
8459 /* Set the 'user' field only if it is not already set. */
8460 if (pst->dependencies[j]->user == NULL)
8461 pst->dependencies[j]->user = pst;
8462 }
8463 }
8464}
8465
93311388
DE
8466/* Build the partial symbol table by doing a quick pass through the
8467 .debug_info and .debug_abbrev sections. */
72bf9492 8468
93311388 8469static void
ed2dc618 8470dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8471{
ed2dc618 8472 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8473
b4f54984 8474 if (dwarf_read_debug)
45cfd468
DE
8475 {
8476 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8477 objfile_name (objfile));
45cfd468
DE
8478 }
8479
98bfdba5
PA
8480 dwarf2_per_objfile->reading_partial_symbols = 1;
8481
be391dca 8482 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8483
93311388
DE
8484 /* Any cached compilation units will be linked by the per-objfile
8485 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8486 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8487
ed2dc618 8488 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8489
ed2dc618 8490 create_all_comp_units (dwarf2_per_objfile);
c906108c 8491
60606b2c
TT
8492 /* Create a temporary address map on a temporary obstack. We later
8493 copy this to the final obstack. */
8268c778 8494 auto_obstack temp_obstack;
791afaa2
TT
8495
8496 scoped_restore save_psymtabs_addrmap
d320c2b5 8497 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8498 addrmap_create_mutable (&temp_obstack));
72bf9492 8499
b76e467d
SM
8500 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8501 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8502
6aa5f3a6 8503 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8504 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8505
8506 /* Now that all TUs have been processed we can fill in the dependencies. */
8507 if (dwarf2_per_objfile->type_unit_groups != NULL)
8508 {
8509 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8510 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8511 }
8512
b4f54984 8513 if (dwarf_read_debug)
ed2dc618 8514 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8515
ed2dc618 8516 set_partial_user (dwarf2_per_objfile);
95554aad 8517
d320c2b5
TT
8518 objfile->partial_symtabs->psymtabs_addrmap
8519 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8520 objfile->partial_symtabs->obstack ());
791afaa2
TT
8521 /* At this point we want to keep the address map. */
8522 save_psymtabs_addrmap.release ();
ff013f42 8523
b4f54984 8524 if (dwarf_read_debug)
45cfd468 8525 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8526 objfile_name (objfile));
ae038cb0
DJ
8527}
8528
3019eac3 8529/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8530
8531static void
dee91e82 8532load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8533 const gdb_byte *info_ptr,
dee91e82
DE
8534 struct die_info *comp_unit_die,
8535 int has_children,
8536 void *data)
ae038cb0 8537{
dee91e82 8538 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8539
95554aad 8540 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8541
ae038cb0
DJ
8542 /* Check if comp unit has_children.
8543 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8544 If not, there's no more debug_info for this comp unit. */
d85a05f0 8545 if (has_children)
dee91e82
DE
8546 load_partial_dies (reader, info_ptr, 0);
8547}
98bfdba5 8548
dee91e82
DE
8549/* Load the partial DIEs for a secondary CU into memory.
8550 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8551
dee91e82
DE
8552static void
8553load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8554{
58f0c718 8555 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8556 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8557}
8558
ae038cb0 8559static void
ed2dc618 8560read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8561 struct dwarf2_section_info *section,
f1902523 8562 struct dwarf2_section_info *abbrev_section,
b76e467d 8563 unsigned int is_dwz)
ae038cb0 8564{
d521ce57 8565 const gdb_byte *info_ptr;
ed2dc618 8566 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8567
b4f54984 8568 if (dwarf_read_debug)
bf6af496 8569 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8570 get_section_name (section),
8571 get_section_file_name (section));
bf6af496 8572
36586728 8573 dwarf2_read_section (objfile, section);
ae038cb0 8574
36586728 8575 info_ptr = section->buffer;
6e70227d 8576
36586728 8577 while (info_ptr < section->buffer + section->size)
ae038cb0 8578 {
ae038cb0 8579 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8580
9c541725 8581 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8582
f1902523 8583 comp_unit_head cu_header;
ed2dc618
SM
8584 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8585 abbrev_section, info_ptr,
8586 rcuh_kind::COMPILE);
ae038cb0
DJ
8587
8588 /* Save the compilation unit for later lookup. */
f1902523
JK
8589 if (cu_header.unit_type != DW_UT_type)
8590 {
8591 this_cu = XOBNEW (&objfile->objfile_obstack,
8592 struct dwarf2_per_cu_data);
8593 memset (this_cu, 0, sizeof (*this_cu));
8594 }
8595 else
8596 {
8597 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8598 struct signatured_type);
8599 memset (sig_type, 0, sizeof (*sig_type));
8600 sig_type->signature = cu_header.signature;
8601 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8602 this_cu = &sig_type->per_cu;
8603 }
8604 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8605 this_cu->sect_off = sect_off;
f1902523 8606 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8607 this_cu->is_dwz = is_dwz;
e3b94546 8608 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8609 this_cu->section = section;
ae038cb0 8610
b76e467d 8611 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8612
8613 info_ptr = info_ptr + this_cu->length;
8614 }
36586728
TT
8615}
8616
8617/* Create a list of all compilation units in OBJFILE.
8618 This is only done for -readnow and building partial symtabs. */
8619
8620static void
ed2dc618 8621create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8622{
b76e467d 8623 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8624 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8625 &dwarf2_per_objfile->abbrev, 0);
36586728 8626
b76e467d 8627 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8628 if (dwz != NULL)
ed2dc618 8629 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8630 1);
c906108c
SS
8631}
8632
5734ee8b 8633/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8634 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8635 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8636 DW_AT_ranges). See the comments of add_partial_subprogram on how
8637 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8638
72bf9492
DJ
8639static void
8640scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8641 CORE_ADDR *highpc, int set_addrmap,
8642 struct dwarf2_cu *cu)
c906108c 8643{
72bf9492 8644 struct partial_die_info *pdi;
c906108c 8645
91c24f0a
DC
8646 /* Now, march along the PDI's, descending into ones which have
8647 interesting children but skipping the children of the other ones,
8648 until we reach the end of the compilation unit. */
c906108c 8649
72bf9492 8650 pdi = first_die;
91c24f0a 8651
72bf9492
DJ
8652 while (pdi != NULL)
8653 {
52356b79 8654 pdi->fixup (cu);
c906108c 8655
f55ee35c 8656 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8657 children, so we need to look at them. Ditto for anonymous
8658 enums. */
933c6fe4 8659
72bf9492 8660 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8661 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8662 || pdi->tag == DW_TAG_imported_unit
8663 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8664 {
72bf9492 8665 switch (pdi->tag)
c906108c
SS
8666 {
8667 case DW_TAG_subprogram:
b1dc1806 8668 case DW_TAG_inlined_subroutine:
cdc07690 8669 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8670 break;
72929c62 8671 case DW_TAG_constant:
c906108c
SS
8672 case DW_TAG_variable:
8673 case DW_TAG_typedef:
91c24f0a 8674 case DW_TAG_union_type:
72bf9492 8675 if (!pdi->is_declaration)
63d06c5c 8676 {
72bf9492 8677 add_partial_symbol (pdi, cu);
63d06c5c
DC
8678 }
8679 break;
c906108c 8680 case DW_TAG_class_type:
680b30c7 8681 case DW_TAG_interface_type:
c906108c 8682 case DW_TAG_structure_type:
72bf9492 8683 if (!pdi->is_declaration)
c906108c 8684 {
72bf9492 8685 add_partial_symbol (pdi, cu);
c906108c 8686 }
b7fee5a3
KS
8687 if ((cu->language == language_rust
8688 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8689 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8690 set_addrmap, cu);
c906108c 8691 break;
91c24f0a 8692 case DW_TAG_enumeration_type:
72bf9492
DJ
8693 if (!pdi->is_declaration)
8694 add_partial_enumeration (pdi, cu);
c906108c
SS
8695 break;
8696 case DW_TAG_base_type:
a02abb62 8697 case DW_TAG_subrange_type:
c906108c 8698 /* File scope base type definitions are added to the partial
c5aa993b 8699 symbol table. */
72bf9492 8700 add_partial_symbol (pdi, cu);
c906108c 8701 break;
d9fa45fe 8702 case DW_TAG_namespace:
cdc07690 8703 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8704 break;
5d7cb8df 8705 case DW_TAG_module:
cdc07690 8706 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8707 break;
95554aad
TT
8708 case DW_TAG_imported_unit:
8709 {
8710 struct dwarf2_per_cu_data *per_cu;
8711
f4dc4d17
DE
8712 /* For now we don't handle imported units in type units. */
8713 if (cu->per_cu->is_debug_types)
8714 {
8715 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8716 " supported in type units [in module %s]"),
518817b3 8717 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8718 }
8719
e3b94546
SM
8720 per_cu = dwarf2_find_containing_comp_unit
8721 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8722 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8723
8724 /* Go read the partial unit, if needed. */
8725 if (per_cu->v.psymtab == NULL)
b93601f3 8726 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8727
ae640021 8728 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8729 }
8730 break;
74921315
KS
8731 case DW_TAG_imported_declaration:
8732 add_partial_symbol (pdi, cu);
8733 break;
c906108c
SS
8734 default:
8735 break;
8736 }
8737 }
8738
72bf9492
DJ
8739 /* If the die has a sibling, skip to the sibling. */
8740
8741 pdi = pdi->die_sibling;
8742 }
8743}
8744
8745/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8746
72bf9492 8747 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8748 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8749 Enumerators are an exception; they use the scope of their parent
8750 enumeration type, i.e. the name of the enumeration type is not
8751 prepended to the enumerator.
91c24f0a 8752
72bf9492
DJ
8753 There are two complexities. One is DW_AT_specification; in this
8754 case "parent" means the parent of the target of the specification,
8755 instead of the direct parent of the DIE. The other is compilers
8756 which do not emit DW_TAG_namespace; in this case we try to guess
8757 the fully qualified name of structure types from their members'
8758 linkage names. This must be done using the DIE's children rather
8759 than the children of any DW_AT_specification target. We only need
8760 to do this for structures at the top level, i.e. if the target of
8761 any DW_AT_specification (if any; otherwise the DIE itself) does not
8762 have a parent. */
8763
8764/* Compute the scope prefix associated with PDI's parent, in
8765 compilation unit CU. The result will be allocated on CU's
8766 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8767 field. NULL is returned if no prefix is necessary. */
15d034d0 8768static const char *
72bf9492
DJ
8769partial_die_parent_scope (struct partial_die_info *pdi,
8770 struct dwarf2_cu *cu)
8771{
15d034d0 8772 const char *grandparent_scope;
72bf9492 8773 struct partial_die_info *parent, *real_pdi;
91c24f0a 8774
72bf9492
DJ
8775 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8776 then this means the parent of the specification DIE. */
8777
8778 real_pdi = pdi;
72bf9492 8779 while (real_pdi->has_specification)
fb816e8b 8780 {
122cf0f2
AB
8781 auto res = find_partial_die (real_pdi->spec_offset,
8782 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8783 real_pdi = res.pdi;
8784 cu = res.cu;
8785 }
72bf9492
DJ
8786
8787 parent = real_pdi->die_parent;
8788 if (parent == NULL)
8789 return NULL;
8790
8791 if (parent->scope_set)
8792 return parent->scope;
8793
52356b79 8794 parent->fixup (cu);
72bf9492 8795
10b3939b 8796 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8797
acebe513
UW
8798 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8799 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8800 Work around this problem here. */
8801 if (cu->language == language_cplus
6e70227d 8802 && parent->tag == DW_TAG_namespace
acebe513
UW
8803 && strcmp (parent->name, "::") == 0
8804 && grandparent_scope == NULL)
8805 {
8806 parent->scope = NULL;
8807 parent->scope_set = 1;
8808 return NULL;
8809 }
8810
0a4b0913 8811 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8812 if (pdi->tag == DW_TAG_enumerator)
8813 /* Enumerators should not get the name of the enumeration as a prefix. */
8814 parent->scope = grandparent_scope;
8815 else if (parent->tag == DW_TAG_namespace
f55ee35c 8816 || parent->tag == DW_TAG_module
72bf9492
DJ
8817 || parent->tag == DW_TAG_structure_type
8818 || parent->tag == DW_TAG_class_type
680b30c7 8819 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8820 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8821 || parent->tag == DW_TAG_enumeration_type
8822 || (cu->language == language_fortran
8823 && parent->tag == DW_TAG_subprogram
8824 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8825 {
8826 if (grandparent_scope == NULL)
8827 parent->scope = parent->name;
8828 else
3e43a32a
MS
8829 parent->scope = typename_concat (&cu->comp_unit_obstack,
8830 grandparent_scope,
f55ee35c 8831 parent->name, 0, cu);
72bf9492 8832 }
72bf9492
DJ
8833 else
8834 {
8835 /* FIXME drow/2004-04-01: What should we be doing with
8836 function-local names? For partial symbols, we should probably be
8837 ignoring them. */
fa9c3fa0
TT
8838 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8839 dwarf_tag_name (parent->tag),
8840 sect_offset_str (pdi->sect_off));
72bf9492 8841 parent->scope = grandparent_scope;
c906108c
SS
8842 }
8843
72bf9492
DJ
8844 parent->scope_set = 1;
8845 return parent->scope;
8846}
8847
8848/* Return the fully scoped name associated with PDI, from compilation unit
8849 CU. The result will be allocated with malloc. */
4568ecf9 8850
72bf9492
DJ
8851static char *
8852partial_die_full_name (struct partial_die_info *pdi,
8853 struct dwarf2_cu *cu)
8854{
15d034d0 8855 const char *parent_scope;
72bf9492 8856
98bfdba5
PA
8857 /* If this is a template instantiation, we can not work out the
8858 template arguments from partial DIEs. So, unfortunately, we have
8859 to go through the full DIEs. At least any work we do building
8860 types here will be reused if full symbols are loaded later. */
8861 if (pdi->has_template_arguments)
8862 {
52356b79 8863 pdi->fixup (cu);
98bfdba5
PA
8864
8865 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8866 {
8867 struct die_info *die;
8868 struct attribute attr;
8869 struct dwarf2_cu *ref_cu = cu;
8870
b64f50a1 8871 /* DW_FORM_ref_addr is using section offset. */
b4069958 8872 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8873 attr.form = DW_FORM_ref_addr;
9c541725 8874 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8875 die = follow_die_ref (NULL, &attr, &ref_cu);
8876
8877 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8878 }
8879 }
8880
72bf9492
DJ
8881 parent_scope = partial_die_parent_scope (pdi, cu);
8882 if (parent_scope == NULL)
8883 return NULL;
8884 else
f55ee35c 8885 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8886}
8887
8888static void
72bf9492 8889add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8890{
518817b3
SM
8891 struct dwarf2_per_objfile *dwarf2_per_objfile
8892 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8893 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8894 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8895 CORE_ADDR addr = 0;
15d034d0 8896 const char *actual_name = NULL;
e142c38c 8897 CORE_ADDR baseaddr;
15d034d0 8898 char *built_actual_name;
e142c38c
DJ
8899
8900 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8901
15d034d0
TT
8902 built_actual_name = partial_die_full_name (pdi, cu);
8903 if (built_actual_name != NULL)
8904 actual_name = built_actual_name;
63d06c5c 8905
72bf9492
DJ
8906 if (actual_name == NULL)
8907 actual_name = pdi->name;
8908
c906108c
SS
8909 switch (pdi->tag)
8910 {
b1dc1806 8911 case DW_TAG_inlined_subroutine:
c906108c 8912 case DW_TAG_subprogram:
79748972
TT
8913 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8914 - baseaddr);
0a4b0913
AB
8915 if (pdi->is_external
8916 || cu->language == language_ada
8917 || (cu->language == language_fortran
8918 && pdi->die_parent != NULL
8919 && pdi->die_parent->tag == DW_TAG_subprogram))
8920 {
8921 /* Normally, only "external" DIEs are part of the global scope.
8922 But in Ada and Fortran, we want to be able to access nested
8923 procedures globally. So all Ada and Fortran subprograms are
8924 stored in the global scope. */
f47fb265 8925 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8926 built_actual_name != NULL,
f47fb265 8927 VAR_DOMAIN, LOC_BLOCK,
79748972 8928 SECT_OFF_TEXT (objfile),
75aedd27 8929 psymbol_placement::GLOBAL,
79748972
TT
8930 addr,
8931 cu->language, objfile);
c906108c
SS
8932 }
8933 else
8934 {
f47fb265 8935 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8936 built_actual_name != NULL,
f47fb265 8937 VAR_DOMAIN, LOC_BLOCK,
79748972 8938 SECT_OFF_TEXT (objfile),
75aedd27 8939 psymbol_placement::STATIC,
1762568f 8940 addr, cu->language, objfile);
c906108c 8941 }
0c1b455e
TT
8942
8943 if (pdi->main_subprogram && actual_name != NULL)
8944 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8945 break;
72929c62 8946 case DW_TAG_constant:
75aedd27
TT
8947 add_psymbol_to_list (actual_name, strlen (actual_name),
8948 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8949 -1, (pdi->is_external
8950 ? psymbol_placement::GLOBAL
8951 : psymbol_placement::STATIC),
8952 0, cu->language, objfile);
72929c62 8953 break;
c906108c 8954 case DW_TAG_variable:
95554aad
TT
8955 if (pdi->d.locdesc)
8956 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8957
95554aad 8958 if (pdi->d.locdesc
caac4577
JG
8959 && addr == 0
8960 && !dwarf2_per_objfile->has_section_at_zero)
8961 {
8962 /* A global or static variable may also have been stripped
8963 out by the linker if unused, in which case its address
8964 will be nullified; do not add such variables into partial
8965 symbol table then. */
8966 }
8967 else if (pdi->is_external)
c906108c
SS
8968 {
8969 /* Global Variable.
8970 Don't enter into the minimal symbol tables as there is
8971 a minimal symbol table entry from the ELF symbols already.
8972 Enter into partial symbol table if it has a location
8973 descriptor or a type.
8974 If the location descriptor is missing, new_symbol will create
8975 a LOC_UNRESOLVED symbol, the address of the variable will then
8976 be determined from the minimal symbol table whenever the variable
8977 is referenced.
8978 The address for the partial symbol table entry is not
8979 used by GDB, but it comes in handy for debugging partial symbol
8980 table building. */
8981
95554aad 8982 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8983 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8984 built_actual_name != NULL,
f47fb265 8985 VAR_DOMAIN, LOC_STATIC,
79748972 8986 SECT_OFF_TEXT (objfile),
75aedd27 8987 psymbol_placement::GLOBAL,
79748972 8988 addr, cu->language, objfile);
c906108c
SS
8989 }
8990 else
8991 {
ff908ebf
AW
8992 int has_loc = pdi->d.locdesc != NULL;
8993
8994 /* Static Variable. Skip symbols whose value we cannot know (those
8995 without location descriptors or constant values). */
8996 if (!has_loc && !pdi->has_const_value)
decbce07 8997 {
15d034d0 8998 xfree (built_actual_name);
decbce07
MS
8999 return;
9000 }
ff908ebf 9001
f47fb265 9002 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9003 built_actual_name != NULL,
f47fb265 9004 VAR_DOMAIN, LOC_STATIC,
79748972 9005 SECT_OFF_TEXT (objfile),
75aedd27 9006 psymbol_placement::STATIC,
79748972 9007 has_loc ? addr : 0,
f47fb265 9008 cu->language, objfile);
c906108c
SS
9009 }
9010 break;
9011 case DW_TAG_typedef:
9012 case DW_TAG_base_type:
a02abb62 9013 case DW_TAG_subrange_type:
38d518c9 9014 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9015 built_actual_name != NULL,
79748972 9016 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9017 psymbol_placement::STATIC,
1762568f 9018 0, cu->language, objfile);
c906108c 9019 break;
74921315 9020 case DW_TAG_imported_declaration:
72bf9492
DJ
9021 case DW_TAG_namespace:
9022 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9023 built_actual_name != NULL,
79748972 9024 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9025 psymbol_placement::GLOBAL,
1762568f 9026 0, cu->language, objfile);
72bf9492 9027 break;
530e8392 9028 case DW_TAG_module:
a5fd13a9
BH
9029 /* With Fortran 77 there might be a "BLOCK DATA" module
9030 available without any name. If so, we skip the module as it
9031 doesn't bring any value. */
9032 if (actual_name != nullptr)
9033 add_psymbol_to_list (actual_name, strlen (actual_name),
9034 built_actual_name != NULL,
9035 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9036 psymbol_placement::GLOBAL,
9037 0, cu->language, objfile);
530e8392 9038 break;
c906108c 9039 case DW_TAG_class_type:
680b30c7 9040 case DW_TAG_interface_type:
c906108c
SS
9041 case DW_TAG_structure_type:
9042 case DW_TAG_union_type:
9043 case DW_TAG_enumeration_type:
fa4028e9
JB
9044 /* Skip external references. The DWARF standard says in the section
9045 about "Structure, Union, and Class Type Entries": "An incomplete
9046 structure, union or class type is represented by a structure,
9047 union or class entry that does not have a byte size attribute
9048 and that has a DW_AT_declaration attribute." */
9049 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9050 {
15d034d0 9051 xfree (built_actual_name);
decbce07
MS
9052 return;
9053 }
fa4028e9 9054
63d06c5c
DC
9055 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9056 static vs. global. */
38d518c9 9057 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9058 built_actual_name != NULL,
79748972 9059 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9060 cu->language == language_cplus
75aedd27
TT
9061 ? psymbol_placement::GLOBAL
9062 : psymbol_placement::STATIC,
1762568f 9063 0, cu->language, objfile);
c906108c 9064
c906108c
SS
9065 break;
9066 case DW_TAG_enumerator:
38d518c9 9067 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9068 built_actual_name != NULL,
79748972 9069 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9070 cu->language == language_cplus
75aedd27
TT
9071 ? psymbol_placement::GLOBAL
9072 : psymbol_placement::STATIC,
1762568f 9073 0, cu->language, objfile);
c906108c
SS
9074 break;
9075 default:
9076 break;
9077 }
5c4e30ca 9078
15d034d0 9079 xfree (built_actual_name);
c906108c
SS
9080}
9081
5c4e30ca
DC
9082/* Read a partial die corresponding to a namespace; also, add a symbol
9083 corresponding to that namespace to the symbol table. NAMESPACE is
9084 the name of the enclosing namespace. */
91c24f0a 9085
72bf9492
DJ
9086static void
9087add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9088 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9089 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9090{
72bf9492 9091 /* Add a symbol for the namespace. */
e7c27a73 9092
72bf9492 9093 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9094
9095 /* Now scan partial symbols in that namespace. */
9096
91c24f0a 9097 if (pdi->has_children)
cdc07690 9098 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9099}
9100
5d7cb8df
JK
9101/* Read a partial die corresponding to a Fortran module. */
9102
9103static void
9104add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9105 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9106{
530e8392
KB
9107 /* Add a symbol for the namespace. */
9108
9109 add_partial_symbol (pdi, cu);
9110
f55ee35c 9111 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9112
9113 if (pdi->has_children)
cdc07690 9114 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9115}
9116
b1dc1806
XR
9117/* Read a partial die corresponding to a subprogram or an inlined
9118 subprogram and create a partial symbol for that subprogram.
9119 When the CU language allows it, this routine also defines a partial
9120 symbol for each nested subprogram that this subprogram contains.
9121 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9122 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9123
cdc07690
YQ
9124 PDI may also be a lexical block, in which case we simply search
9125 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9126 Again, this is only performed when the CU language allows this
9127 type of definitions. */
9128
9129static void
9130add_partial_subprogram (struct partial_die_info *pdi,
9131 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9132 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9133{
b1dc1806 9134 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9135 {
9136 if (pdi->has_pc_info)
9137 {
9138 if (pdi->lowpc < *lowpc)
9139 *lowpc = pdi->lowpc;
9140 if (pdi->highpc > *highpc)
9141 *highpc = pdi->highpc;
cdc07690 9142 if (set_addrmap)
5734ee8b 9143 {
518817b3 9144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9145 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9146 CORE_ADDR baseaddr;
b926417a
TT
9147 CORE_ADDR this_highpc;
9148 CORE_ADDR this_lowpc;
5734ee8b
DJ
9149
9150 baseaddr = ANOFFSET (objfile->section_offsets,
9151 SECT_OFF_TEXT (objfile));
b926417a
TT
9152 this_lowpc
9153 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9154 pdi->lowpc + baseaddr)
9155 - baseaddr);
9156 this_highpc
9157 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9158 pdi->highpc + baseaddr)
9159 - baseaddr);
d320c2b5 9160 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9161 this_lowpc, this_highpc - 1,
9291a0cd 9162 cu->per_cu->v.psymtab);
5734ee8b 9163 }
481860b3
GB
9164 }
9165
9166 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9167 {
bc30ff58 9168 if (!pdi->is_declaration)
e8d05480
JB
9169 /* Ignore subprogram DIEs that do not have a name, they are
9170 illegal. Do not emit a complaint at this point, we will
9171 do so when we convert this psymtab into a symtab. */
9172 if (pdi->name)
9173 add_partial_symbol (pdi, cu);
bc30ff58
JB
9174 }
9175 }
6e70227d 9176
bc30ff58
JB
9177 if (! pdi->has_children)
9178 return;
9179
0a4b0913 9180 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9181 {
9182 pdi = pdi->die_child;
9183 while (pdi != NULL)
9184 {
52356b79 9185 pdi->fixup (cu);
bc30ff58 9186 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9187 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9188 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9189 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9190 pdi = pdi->die_sibling;
9191 }
9192 }
9193}
9194
91c24f0a
DC
9195/* Read a partial die corresponding to an enumeration type. */
9196
72bf9492
DJ
9197static void
9198add_partial_enumeration (struct partial_die_info *enum_pdi,
9199 struct dwarf2_cu *cu)
91c24f0a 9200{
72bf9492 9201 struct partial_die_info *pdi;
91c24f0a
DC
9202
9203 if (enum_pdi->name != NULL)
72bf9492
DJ
9204 add_partial_symbol (enum_pdi, cu);
9205
9206 pdi = enum_pdi->die_child;
9207 while (pdi)
91c24f0a 9208 {
72bf9492 9209 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9210 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9211 else
72bf9492
DJ
9212 add_partial_symbol (pdi, cu);
9213 pdi = pdi->die_sibling;
91c24f0a 9214 }
91c24f0a
DC
9215}
9216
6caca83c
CC
9217/* Return the initial uleb128 in the die at INFO_PTR. */
9218
9219static unsigned int
d521ce57 9220peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9221{
9222 unsigned int bytes_read;
9223
9224 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9225}
9226
685af9cd
TT
9227/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9228 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9229
4bb7a0a7
DJ
9230 Return the corresponding abbrev, or NULL if the number is zero (indicating
9231 an empty DIE). In either case *BYTES_READ will be set to the length of
9232 the initial number. */
9233
9234static struct abbrev_info *
685af9cd
TT
9235peek_die_abbrev (const die_reader_specs &reader,
9236 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9237{
685af9cd 9238 dwarf2_cu *cu = reader.cu;
518817b3 9239 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9240 unsigned int abbrev_number
9241 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9242
9243 if (abbrev_number == 0)
9244 return NULL;
9245
685af9cd 9246 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9247 if (!abbrev)
9248 {
422b9917 9249 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9250 " at offset %s [in module %s]"),
422b9917 9251 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9252 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9253 }
9254
9255 return abbrev;
9256}
9257
93311388
DE
9258/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9259 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9260 DIE. Any children of the skipped DIEs will also be skipped. */
9261
d521ce57
TT
9262static const gdb_byte *
9263skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9264{
4bb7a0a7
DJ
9265 while (1)
9266 {
685af9cd
TT
9267 unsigned int bytes_read;
9268 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9269
4bb7a0a7
DJ
9270 if (abbrev == NULL)
9271 return info_ptr + bytes_read;
9272 else
dee91e82 9273 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9274 }
9275}
9276
93311388
DE
9277/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9278 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9279 abbrev corresponding to that skipped uleb128 should be passed in
9280 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9281 children. */
9282
d521ce57
TT
9283static const gdb_byte *
9284skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9285 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9286{
9287 unsigned int bytes_read;
9288 struct attribute attr;
dee91e82
DE
9289 bfd *abfd = reader->abfd;
9290 struct dwarf2_cu *cu = reader->cu;
d521ce57 9291 const gdb_byte *buffer = reader->buffer;
f664829e 9292 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9293 unsigned int form, i;
9294
9295 for (i = 0; i < abbrev->num_attrs; i++)
9296 {
9297 /* The only abbrev we care about is DW_AT_sibling. */
9298 if (abbrev->attrs[i].name == DW_AT_sibling)
9299 {
dee91e82 9300 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9301 if (attr.form == DW_FORM_ref_addr)
b98664d3 9302 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9303 else
b9502d3f 9304 {
9c541725
PA
9305 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9306 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9307
9308 if (sibling_ptr < info_ptr)
b98664d3 9309 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9310 else if (sibling_ptr > reader->buffer_end)
9311 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9312 else
9313 return sibling_ptr;
9314 }
4bb7a0a7
DJ
9315 }
9316
9317 /* If it isn't DW_AT_sibling, skip this attribute. */
9318 form = abbrev->attrs[i].form;
9319 skip_attribute:
9320 switch (form)
9321 {
4bb7a0a7 9322 case DW_FORM_ref_addr:
ae411497
TT
9323 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9324 and later it is offset sized. */
9325 if (cu->header.version == 2)
9326 info_ptr += cu->header.addr_size;
9327 else
9328 info_ptr += cu->header.offset_size;
9329 break;
36586728
TT
9330 case DW_FORM_GNU_ref_alt:
9331 info_ptr += cu->header.offset_size;
9332 break;
ae411497 9333 case DW_FORM_addr:
4bb7a0a7
DJ
9334 info_ptr += cu->header.addr_size;
9335 break;
9336 case DW_FORM_data1:
9337 case DW_FORM_ref1:
9338 case DW_FORM_flag:
8fe0f950 9339 case DW_FORM_strx1:
4bb7a0a7
DJ
9340 info_ptr += 1;
9341 break;
2dc7f7b3 9342 case DW_FORM_flag_present:
43988095 9343 case DW_FORM_implicit_const:
2dc7f7b3 9344 break;
4bb7a0a7
DJ
9345 case DW_FORM_data2:
9346 case DW_FORM_ref2:
8fe0f950 9347 case DW_FORM_strx2:
4bb7a0a7
DJ
9348 info_ptr += 2;
9349 break;
8fe0f950
AT
9350 case DW_FORM_strx3:
9351 info_ptr += 3;
9352 break;
4bb7a0a7
DJ
9353 case DW_FORM_data4:
9354 case DW_FORM_ref4:
8fe0f950 9355 case DW_FORM_strx4:
4bb7a0a7
DJ
9356 info_ptr += 4;
9357 break;
9358 case DW_FORM_data8:
9359 case DW_FORM_ref8:
55f1336d 9360 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9361 info_ptr += 8;
9362 break;
0224619f
JK
9363 case DW_FORM_data16:
9364 info_ptr += 16;
9365 break;
4bb7a0a7 9366 case DW_FORM_string:
9b1c24c8 9367 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9368 info_ptr += bytes_read;
9369 break;
2dc7f7b3 9370 case DW_FORM_sec_offset:
4bb7a0a7 9371 case DW_FORM_strp:
36586728 9372 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9373 info_ptr += cu->header.offset_size;
9374 break;
2dc7f7b3 9375 case DW_FORM_exprloc:
4bb7a0a7
DJ
9376 case DW_FORM_block:
9377 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9378 info_ptr += bytes_read;
9379 break;
9380 case DW_FORM_block1:
9381 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9382 break;
9383 case DW_FORM_block2:
9384 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9385 break;
9386 case DW_FORM_block4:
9387 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9388 break;
336d760d 9389 case DW_FORM_addrx:
cf532bd1 9390 case DW_FORM_strx:
4bb7a0a7
DJ
9391 case DW_FORM_sdata:
9392 case DW_FORM_udata:
9393 case DW_FORM_ref_udata:
3019eac3
DE
9394 case DW_FORM_GNU_addr_index:
9395 case DW_FORM_GNU_str_index:
d521ce57 9396 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9397 break;
9398 case DW_FORM_indirect:
9399 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9400 info_ptr += bytes_read;
9401 /* We need to continue parsing from here, so just go back to
9402 the top. */
9403 goto skip_attribute;
9404
9405 default:
3e43a32a
MS
9406 error (_("Dwarf Error: Cannot handle %s "
9407 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9408 dwarf_form_name (form),
9409 bfd_get_filename (abfd));
9410 }
9411 }
9412
9413 if (abbrev->has_children)
dee91e82 9414 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9415 else
9416 return info_ptr;
9417}
9418
93311388 9419/* Locate ORIG_PDI's sibling.
dee91e82 9420 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9421
d521ce57 9422static const gdb_byte *
dee91e82
DE
9423locate_pdi_sibling (const struct die_reader_specs *reader,
9424 struct partial_die_info *orig_pdi,
d521ce57 9425 const gdb_byte *info_ptr)
91c24f0a
DC
9426{
9427 /* Do we know the sibling already? */
72bf9492 9428
91c24f0a
DC
9429 if (orig_pdi->sibling)
9430 return orig_pdi->sibling;
9431
9432 /* Are there any children to deal with? */
9433
9434 if (!orig_pdi->has_children)
9435 return info_ptr;
9436
4bb7a0a7 9437 /* Skip the children the long way. */
91c24f0a 9438
dee91e82 9439 return skip_children (reader, info_ptr);
91c24f0a
DC
9440}
9441
257e7a09 9442/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9443 not NULL. */
c906108c
SS
9444
9445static void
257e7a09
YQ
9446dwarf2_read_symtab (struct partial_symtab *self,
9447 struct objfile *objfile)
c906108c 9448{
ed2dc618
SM
9449 struct dwarf2_per_objfile *dwarf2_per_objfile
9450 = get_dwarf2_per_objfile (objfile);
9451
257e7a09 9452 if (self->readin)
c906108c 9453 {
442e4d9c 9454 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9455 self->filename);
442e4d9c
YQ
9456 }
9457 else
9458 {
9459 if (info_verbose)
c906108c 9460 {
442e4d9c 9461 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9462 self->filename);
442e4d9c 9463 gdb_flush (gdb_stdout);
c906108c 9464 }
c906108c 9465
442e4d9c
YQ
9466 /* If this psymtab is constructed from a debug-only objfile, the
9467 has_section_at_zero flag will not necessarily be correct. We
9468 can get the correct value for this flag by looking at the data
9469 associated with the (presumably stripped) associated objfile. */
9470 if (objfile->separate_debug_objfile_backlink)
9471 {
9472 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9473 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9474
442e4d9c
YQ
9475 dwarf2_per_objfile->has_section_at_zero
9476 = dpo_backlink->has_section_at_zero;
9477 }
b2ab525c 9478
442e4d9c 9479 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9480
257e7a09 9481 psymtab_to_symtab_1 (self);
c906108c 9482
442e4d9c
YQ
9483 /* Finish up the debug error message. */
9484 if (info_verbose)
9485 printf_filtered (_("done.\n"));
c906108c 9486 }
95554aad 9487
ed2dc618 9488 process_cu_includes (dwarf2_per_objfile);
c906108c 9489}
9cdd5dbd
DE
9490\f
9491/* Reading in full CUs. */
c906108c 9492
10b3939b
DJ
9493/* Add PER_CU to the queue. */
9494
9495static void
95554aad
TT
9496queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9497 enum language pretend_language)
10b3939b
DJ
9498{
9499 struct dwarf2_queue_item *item;
9500
9501 per_cu->queued = 1;
8d749320 9502 item = XNEW (struct dwarf2_queue_item);
10b3939b 9503 item->per_cu = per_cu;
95554aad 9504 item->pretend_language = pretend_language;
10b3939b
DJ
9505 item->next = NULL;
9506
9507 if (dwarf2_queue == NULL)
9508 dwarf2_queue = item;
9509 else
9510 dwarf2_queue_tail->next = item;
9511
9512 dwarf2_queue_tail = item;
9513}
9514
89e63ee4
DE
9515/* If PER_CU is not yet queued, add it to the queue.
9516 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9517 dependency.
0907af0c 9518 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9519 meaning either PER_CU is already queued or it is already loaded.
9520
9521 N.B. There is an invariant here that if a CU is queued then it is loaded.
9522 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9523
9524static int
89e63ee4 9525maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9526 struct dwarf2_per_cu_data *per_cu,
9527 enum language pretend_language)
9528{
9529 /* We may arrive here during partial symbol reading, if we need full
9530 DIEs to process an unusual case (e.g. template arguments). Do
9531 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9532 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9533 {
9534 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9535 return 1;
9536 return 0;
9537 }
9538
9539 /* Mark the dependence relation so that we don't flush PER_CU
9540 too early. */
89e63ee4
DE
9541 if (dependent_cu != NULL)
9542 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9543
9544 /* If it's already on the queue, we have nothing to do. */
9545 if (per_cu->queued)
9546 return 0;
9547
9548 /* If the compilation unit is already loaded, just mark it as
9549 used. */
9550 if (per_cu->cu != NULL)
9551 {
9552 per_cu->cu->last_used = 0;
9553 return 0;
9554 }
9555
9556 /* Add it to the queue. */
9557 queue_comp_unit (per_cu, pretend_language);
9558
9559 return 1;
9560}
9561
10b3939b
DJ
9562/* Process the queue. */
9563
9564static void
ed2dc618 9565process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9566{
9567 struct dwarf2_queue_item *item, *next_item;
9568
b4f54984 9569 if (dwarf_read_debug)
45cfd468
DE
9570 {
9571 fprintf_unfiltered (gdb_stdlog,
9572 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9573 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9574 }
9575
03dd20cc
DJ
9576 /* The queue starts out with one item, but following a DIE reference
9577 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9578 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9579 {
cc12ce38
DE
9580 if ((dwarf2_per_objfile->using_index
9581 ? !item->per_cu->v.quick->compunit_symtab
9582 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9583 /* Skip dummy CUs. */
9584 && item->per_cu->cu != NULL)
f4dc4d17
DE
9585 {
9586 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9587 unsigned int debug_print_threshold;
247f5c4f 9588 char buf[100];
f4dc4d17 9589
247f5c4f 9590 if (per_cu->is_debug_types)
f4dc4d17 9591 {
247f5c4f
DE
9592 struct signatured_type *sig_type =
9593 (struct signatured_type *) per_cu;
9594
9d8780f0 9595 sprintf (buf, "TU %s at offset %s",
73be47f5 9596 hex_string (sig_type->signature),
9d8780f0 9597 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9598 /* There can be 100s of TUs.
9599 Only print them in verbose mode. */
9600 debug_print_threshold = 2;
f4dc4d17 9601 }
247f5c4f 9602 else
73be47f5 9603 {
9d8780f0
SM
9604 sprintf (buf, "CU at offset %s",
9605 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9606 debug_print_threshold = 1;
9607 }
247f5c4f 9608
b4f54984 9609 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9610 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9611
9612 if (per_cu->is_debug_types)
9613 process_full_type_unit (per_cu, item->pretend_language);
9614 else
9615 process_full_comp_unit (per_cu, item->pretend_language);
9616
b4f54984 9617 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9618 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9619 }
10b3939b
DJ
9620
9621 item->per_cu->queued = 0;
9622 next_item = item->next;
9623 xfree (item);
9624 }
9625
9626 dwarf2_queue_tail = NULL;
45cfd468 9627
b4f54984 9628 if (dwarf_read_debug)
45cfd468
DE
9629 {
9630 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9631 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9632 }
10b3939b
DJ
9633}
9634
10b3939b
DJ
9635/* Read in full symbols for PST, and anything it depends on. */
9636
c906108c 9637static void
fba45db2 9638psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9639{
10b3939b 9640 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9641 int i;
9642
95554aad
TT
9643 if (pst->readin)
9644 return;
9645
aaa75496 9646 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9647 if (!pst->dependencies[i]->readin
9648 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9649 {
9650 /* Inform about additional files that need to be read in. */
9651 if (info_verbose)
9652 {
a3f17187 9653 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9654 fputs_filtered (" ", gdb_stdout);
9655 wrap_here ("");
9656 fputs_filtered ("and ", gdb_stdout);
9657 wrap_here ("");
9658 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9659 wrap_here (""); /* Flush output. */
aaa75496
JB
9660 gdb_flush (gdb_stdout);
9661 }
9662 psymtab_to_symtab_1 (pst->dependencies[i]);
9663 }
9664
9a3c8263 9665 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9666
9667 if (per_cu == NULL)
aaa75496
JB
9668 {
9669 /* It's an include file, no symbols to read for it.
9670 Everything is in the parent symtab. */
9671 pst->readin = 1;
9672 return;
9673 }
c906108c 9674
58f0c718 9675 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9676}
9677
dee91e82
DE
9678/* Trivial hash function for die_info: the hash value of a DIE
9679 is its offset in .debug_info for this objfile. */
10b3939b 9680
dee91e82
DE
9681static hashval_t
9682die_hash (const void *item)
10b3939b 9683{
9a3c8263 9684 const struct die_info *die = (const struct die_info *) item;
6502dd73 9685
9c541725 9686 return to_underlying (die->sect_off);
dee91e82 9687}
63d06c5c 9688
dee91e82
DE
9689/* Trivial comparison function for die_info structures: two DIEs
9690 are equal if they have the same offset. */
98bfdba5 9691
dee91e82
DE
9692static int
9693die_eq (const void *item_lhs, const void *item_rhs)
9694{
9a3c8263
SM
9695 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9696 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9697
9c541725 9698 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9699}
c906108c 9700
dee91e82
DE
9701/* die_reader_func for load_full_comp_unit.
9702 This is identical to read_signatured_type_reader,
9703 but is kept separate for now. */
c906108c 9704
dee91e82
DE
9705static void
9706load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9707 const gdb_byte *info_ptr,
dee91e82
DE
9708 struct die_info *comp_unit_die,
9709 int has_children,
9710 void *data)
9711{
9712 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9713 enum language *language_ptr = (enum language *) data;
6caca83c 9714
dee91e82
DE
9715 gdb_assert (cu->die_hash == NULL);
9716 cu->die_hash =
9717 htab_create_alloc_ex (cu->header.length / 12,
9718 die_hash,
9719 die_eq,
9720 NULL,
9721 &cu->comp_unit_obstack,
9722 hashtab_obstack_allocate,
9723 dummy_obstack_deallocate);
e142c38c 9724
dee91e82
DE
9725 if (has_children)
9726 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9727 &info_ptr, comp_unit_die);
9728 cu->dies = comp_unit_die;
9729 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9730
9731 /* We try not to read any attributes in this function, because not
9cdd5dbd 9732 all CUs needed for references have been loaded yet, and symbol
10b3939b 9733 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9734 or we won't be able to build types correctly.
9735 Similarly, if we do not read the producer, we can not apply
9736 producer-specific interpretation. */
95554aad 9737 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9738}
10b3939b 9739
dee91e82 9740/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9741
dee91e82 9742static void
95554aad 9743load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9744 bool skip_partial,
95554aad 9745 enum language pretend_language)
dee91e82 9746{
3019eac3 9747 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9748
58f0c718 9749 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9750 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9751}
9752
3da10d80
KS
9753/* Add a DIE to the delayed physname list. */
9754
9755static void
9756add_to_method_list (struct type *type, int fnfield_index, int index,
9757 const char *name, struct die_info *die,
9758 struct dwarf2_cu *cu)
9759{
9760 struct delayed_method_info mi;
9761 mi.type = type;
9762 mi.fnfield_index = fnfield_index;
9763 mi.index = index;
9764 mi.name = name;
9765 mi.die = die;
c89b44cd 9766 cu->method_list.push_back (mi);
3da10d80
KS
9767}
9768
3693fdb3
PA
9769/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9770 "const" / "volatile". If so, decrements LEN by the length of the
9771 modifier and return true. Otherwise return false. */
9772
9773template<size_t N>
9774static bool
9775check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9776{
9777 size_t mod_len = sizeof (mod) - 1;
9778 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9779 {
9780 len -= mod_len;
9781 return true;
9782 }
9783 return false;
9784}
9785
3da10d80
KS
9786/* Compute the physnames of any methods on the CU's method list.
9787
9788 The computation of method physnames is delayed in order to avoid the
9789 (bad) condition that one of the method's formal parameters is of an as yet
9790 incomplete type. */
9791
9792static void
9793compute_delayed_physnames (struct dwarf2_cu *cu)
9794{
3693fdb3 9795 /* Only C++ delays computing physnames. */
c89b44cd 9796 if (cu->method_list.empty ())
3693fdb3
PA
9797 return;
9798 gdb_assert (cu->language == language_cplus);
9799
52941706 9800 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9801 {
1d06ead6 9802 const char *physname;
3da10d80 9803 struct fn_fieldlist *fn_flp
c89b44cd
TT
9804 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9805 physname = dwarf2_physname (mi.name, mi.die, cu);
9806 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9807 = physname ? physname : "";
3693fdb3
PA
9808
9809 /* Since there's no tag to indicate whether a method is a
9810 const/volatile overload, extract that information out of the
9811 demangled name. */
9812 if (physname != NULL)
9813 {
9814 size_t len = strlen (physname);
9815
9816 while (1)
9817 {
9818 if (physname[len] == ')') /* shortcut */
9819 break;
9820 else if (check_modifier (physname, len, " const"))
c89b44cd 9821 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9822 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9823 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9824 else
9825 break;
9826 }
9827 }
3da10d80 9828 }
c89b44cd
TT
9829
9830 /* The list is no longer needed. */
9831 cu->method_list.clear ();
3da10d80
KS
9832}
9833
a766d390
DE
9834/* Go objects should be embedded in a DW_TAG_module DIE,
9835 and it's not clear if/how imported objects will appear.
9836 To keep Go support simple until that's worked out,
9837 go back through what we've read and create something usable.
9838 We could do this while processing each DIE, and feels kinda cleaner,
9839 but that way is more invasive.
9840 This is to, for example, allow the user to type "p var" or "b main"
9841 without having to specify the package name, and allow lookups
9842 of module.object to work in contexts that use the expression
9843 parser. */
9844
9845static void
9846fixup_go_packaging (struct dwarf2_cu *cu)
9847{
9848 char *package_name = NULL;
9849 struct pending *list;
9850 int i;
9851
c24bdb02 9852 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9853 list != NULL;
9854 list = list->next)
a766d390
DE
9855 {
9856 for (i = 0; i < list->nsyms; ++i)
9857 {
9858 struct symbol *sym = list->symbol[i];
9859
9860 if (SYMBOL_LANGUAGE (sym) == language_go
9861 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9862 {
9863 char *this_package_name = go_symbol_package_name (sym);
9864
9865 if (this_package_name == NULL)
9866 continue;
9867 if (package_name == NULL)
9868 package_name = this_package_name;
9869 else
9870 {
518817b3
SM
9871 struct objfile *objfile
9872 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9873 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9874 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9875 (symbol_symtab (sym) != NULL
9876 ? symtab_to_filename_for_display
9877 (symbol_symtab (sym))
e3b94546 9878 : objfile_name (objfile)),
a766d390
DE
9879 this_package_name, package_name);
9880 xfree (this_package_name);
9881 }
9882 }
9883 }
9884 }
9885
9886 if (package_name != NULL)
9887 {
518817b3 9888 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9889 const char *saved_package_name
021887d8 9890 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
19f392bc
UW
9891 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9892 saved_package_name);
a766d390
DE
9893 struct symbol *sym;
9894
e623cf5d 9895 sym = allocate_symbol (objfile);
f85f34ed 9896 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9897 SYMBOL_SET_NAMES (sym, saved_package_name,
9898 strlen (saved_package_name), 0, objfile);
a766d390
DE
9899 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9900 e.g., "main" finds the "main" module and not C's main(). */
9901 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9902 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9903 SYMBOL_TYPE (sym) = type;
9904
c24bdb02 9905 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9906
9907 xfree (package_name);
9908 }
9909}
9910
c9317f21
TT
9911/* Allocate a fully-qualified name consisting of the two parts on the
9912 obstack. */
9913
9914static const char *
9915rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9916{
9917 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9918}
9919
9920/* A helper that allocates a struct discriminant_info to attach to a
9921 union type. */
9922
9923static struct discriminant_info *
9924alloc_discriminant_info (struct type *type, int discriminant_index,
9925 int default_index)
9926{
9927 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9928 gdb_assert (discriminant_index == -1
9929 || (discriminant_index >= 0
9930 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9931 gdb_assert (default_index == -1
c7b15a66 9932 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9933
9934 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9935
9936 struct discriminant_info *disc
9937 = ((struct discriminant_info *)
9938 TYPE_ZALLOC (type,
9939 offsetof (struct discriminant_info, discriminants)
9940 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9941 disc->default_index = default_index;
9942 disc->discriminant_index = discriminant_index;
9943
9944 struct dynamic_prop prop;
9945 prop.kind = PROP_UNDEFINED;
9946 prop.data.baton = disc;
9947
9948 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9949
9950 return disc;
9951}
9952
9953/* Some versions of rustc emitted enums in an unusual way.
9954
9955 Ordinary enums were emitted as unions. The first element of each
9956 structure in the union was named "RUST$ENUM$DISR". This element
9957 held the discriminant.
9958
9959 These versions of Rust also implemented the "non-zero"
9960 optimization. When the enum had two values, and one is empty and
9961 the other holds a pointer that cannot be zero, the pointer is used
9962 as the discriminant, with a zero value meaning the empty variant.
9963 Here, the union's first member is of the form
9964 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9965 where the fieldnos are the indices of the fields that should be
9966 traversed in order to find the field (which may be several fields deep)
9967 and the variantname is the name of the variant of the case when the
9968 field is zero.
9969
9970 This function recognizes whether TYPE is of one of these forms,
9971 and, if so, smashes it to be a variant type. */
9972
9973static void
9974quirk_rust_enum (struct type *type, struct objfile *objfile)
9975{
9976 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9977
9978 /* We don't need to deal with empty enums. */
9979 if (TYPE_NFIELDS (type) == 0)
9980 return;
9981
9982#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9983 if (TYPE_NFIELDS (type) == 1
9984 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9985 {
9986 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9987
9988 /* Decode the field name to find the offset of the
9989 discriminant. */
9990 ULONGEST bit_offset = 0;
9991 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9992 while (name[0] >= '0' && name[0] <= '9')
9993 {
9994 char *tail;
9995 unsigned long index = strtoul (name, &tail, 10);
9996 name = tail;
9997 if (*name != '$'
9998 || index >= TYPE_NFIELDS (field_type)
9999 || (TYPE_FIELD_LOC_KIND (field_type, index)
10000 != FIELD_LOC_KIND_BITPOS))
10001 {
b98664d3 10002 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10003 "[in module %s]"),
10004 TYPE_FIELD_NAME (type, 0),
10005 objfile_name (objfile));
10006 return;
10007 }
10008 ++name;
10009
10010 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10011 field_type = TYPE_FIELD_TYPE (field_type, index);
10012 }
10013
10014 /* Make a union to hold the variants. */
10015 struct type *union_type = alloc_type (objfile);
10016 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10017 TYPE_NFIELDS (union_type) = 3;
10018 TYPE_FIELDS (union_type)
10019 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10020 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10021 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10022
10023 /* Put the discriminant must at index 0. */
10024 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10025 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10026 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10027 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10028
10029 /* The order of fields doesn't really matter, so put the real
10030 field at index 1 and the data-less field at index 2. */
10031 struct discriminant_info *disc
10032 = alloc_discriminant_info (union_type, 0, 1);
10033 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10034 TYPE_FIELD_NAME (union_type, 1)
10035 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10036 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10037 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10038 TYPE_FIELD_NAME (union_type, 1));
10039
10040 const char *dataless_name
10041 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10042 name);
10043 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10044 dataless_name);
10045 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10046 /* NAME points into the original discriminant name, which
10047 already has the correct lifetime. */
10048 TYPE_FIELD_NAME (union_type, 2) = name;
10049 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10050 disc->discriminants[2] = 0;
10051
10052 /* Smash this type to be a structure type. We have to do this
10053 because the type has already been recorded. */
10054 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10055 TYPE_NFIELDS (type) = 1;
10056 TYPE_FIELDS (type)
10057 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10058
10059 /* Install the variant part. */
10060 TYPE_FIELD_TYPE (type, 0) = union_type;
10061 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10062 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10063 }
77c2dba3
TT
10064 /* A union with a single anonymous field is probably an old-style
10065 univariant enum. */
10066 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10067 {
c9317f21
TT
10068 /* Smash this type to be a structure type. We have to do this
10069 because the type has already been recorded. */
10070 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10071
10072 /* Make a union to hold the variants. */
10073 struct type *union_type = alloc_type (objfile);
10074 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10075 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10076 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10077 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10078 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10079
10080 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10081 const char *variant_name
10082 = rust_last_path_segment (TYPE_NAME (field_type));
10083 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10084 TYPE_NAME (field_type)
10085 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10086 TYPE_NAME (type), variant_name);
c9317f21
TT
10087
10088 /* Install the union in the outer struct type. */
10089 TYPE_NFIELDS (type) = 1;
10090 TYPE_FIELDS (type)
10091 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10092 TYPE_FIELD_TYPE (type, 0) = union_type;
10093 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10094 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10095
10096 alloc_discriminant_info (union_type, -1, 0);
10097 }
10098 else
10099 {
10100 struct type *disr_type = nullptr;
10101 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10102 {
10103 disr_type = TYPE_FIELD_TYPE (type, i);
10104
a037790e
TT
10105 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10106 {
10107 /* All fields of a true enum will be structs. */
10108 return;
10109 }
10110 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10111 {
10112 /* Could be data-less variant, so keep going. */
a037790e 10113 disr_type = nullptr;
c9317f21
TT
10114 }
10115 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10116 "RUST$ENUM$DISR") != 0)
10117 {
10118 /* Not a Rust enum. */
10119 return;
10120 }
10121 else
10122 {
10123 /* Found one. */
10124 break;
10125 }
10126 }
10127
10128 /* If we got here without a discriminant, then it's probably
10129 just a union. */
10130 if (disr_type == nullptr)
10131 return;
10132
10133 /* Smash this type to be a structure type. We have to do this
10134 because the type has already been recorded. */
10135 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10136
10137 /* Make a union to hold the variants. */
10138 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10139 struct type *union_type = alloc_type (objfile);
10140 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10141 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10142 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10143 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10144 TYPE_FIELDS (union_type)
10145 = (struct field *) TYPE_ZALLOC (union_type,
10146 (TYPE_NFIELDS (union_type)
10147 * sizeof (struct field)));
10148
10149 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10150 TYPE_NFIELDS (type) * sizeof (struct field));
10151
10152 /* Install the discriminant at index 0 in the union. */
10153 TYPE_FIELD (union_type, 0) = *disr_field;
10154 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10155 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10156
10157 /* Install the union in the outer struct type. */
10158 TYPE_FIELD_TYPE (type, 0) = union_type;
10159 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10160 TYPE_NFIELDS (type) = 1;
10161
10162 /* Set the size and offset of the union type. */
10163 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10164
10165 /* We need a way to find the correct discriminant given a
10166 variant name. For convenience we build a map here. */
10167 struct type *enum_type = FIELD_TYPE (*disr_field);
10168 std::unordered_map<std::string, ULONGEST> discriminant_map;
10169 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10170 {
10171 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10172 {
10173 const char *name
10174 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10175 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10176 }
10177 }
10178
10179 int n_fields = TYPE_NFIELDS (union_type);
10180 struct discriminant_info *disc
10181 = alloc_discriminant_info (union_type, 0, -1);
10182 /* Skip the discriminant here. */
10183 for (int i = 1; i < n_fields; ++i)
10184 {
10185 /* Find the final word in the name of this variant's type.
10186 That name can be used to look up the correct
10187 discriminant. */
10188 const char *variant_name
10189 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10190 i)));
10191
10192 auto iter = discriminant_map.find (variant_name);
10193 if (iter != discriminant_map.end ())
10194 disc->discriminants[i] = iter->second;
10195
bedda9ac 10196 /* Remove the discriminant field, if it exists. */
c9317f21 10197 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10198 if (TYPE_NFIELDS (sub_type) > 0)
10199 {
10200 --TYPE_NFIELDS (sub_type);
10201 ++TYPE_FIELDS (sub_type);
10202 }
c9317f21
TT
10203 TYPE_FIELD_NAME (union_type, i) = variant_name;
10204 TYPE_NAME (sub_type)
10205 = rust_fully_qualify (&objfile->objfile_obstack,
10206 TYPE_NAME (type), variant_name);
10207 }
10208 }
10209}
10210
10211/* Rewrite some Rust unions to be structures with variants parts. */
10212
10213static void
10214rust_union_quirks (struct dwarf2_cu *cu)
10215{
10216 gdb_assert (cu->language == language_rust);
52941706
SM
10217 for (type *type_ : cu->rust_unions)
10218 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10219 /* We don't need this any more. */
10220 cu->rust_unions.clear ();
c9317f21
TT
10221}
10222
95554aad
TT
10223/* Return the symtab for PER_CU. This works properly regardless of
10224 whether we're using the index or psymtabs. */
10225
43f3e411
DE
10226static struct compunit_symtab *
10227get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10228{
ed2dc618 10229 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10230 ? per_cu->v.quick->compunit_symtab
10231 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10232}
10233
10234/* A helper function for computing the list of all symbol tables
10235 included by PER_CU. */
10236
10237static void
4c39bc03 10238recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10239 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10240 struct dwarf2_per_cu_data *per_cu,
43f3e411 10241 struct compunit_symtab *immediate_parent)
95554aad
TT
10242{
10243 void **slot;
43f3e411 10244 struct compunit_symtab *cust;
95554aad
TT
10245
10246 slot = htab_find_slot (all_children, per_cu, INSERT);
10247 if (*slot != NULL)
10248 {
10249 /* This inclusion and its children have been processed. */
10250 return;
10251 }
10252
10253 *slot = per_cu;
10254 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10255 cust = get_compunit_symtab (per_cu);
10256 if (cust != NULL)
ec94af83
DE
10257 {
10258 /* If this is a type unit only add its symbol table if we haven't
10259 seen it yet (type unit per_cu's can share symtabs). */
10260 if (per_cu->is_debug_types)
10261 {
43f3e411 10262 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10263 if (*slot == NULL)
10264 {
43f3e411 10265 *slot = cust;
4c39bc03 10266 result->push_back (cust);
43f3e411
DE
10267 if (cust->user == NULL)
10268 cust->user = immediate_parent;
ec94af83
DE
10269 }
10270 }
10271 else
f9125b6c 10272 {
4c39bc03 10273 result->push_back (cust);
43f3e411
DE
10274 if (cust->user == NULL)
10275 cust->user = immediate_parent;
f9125b6c 10276 }
ec94af83 10277 }
95554aad 10278
ae640021
AB
10279 if (!per_cu->imported_symtabs_empty ())
10280 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10281 {
10282 recursively_compute_inclusions (result, all_children,
10283 all_type_symtabs, ptr, cust);
10284 }
95554aad
TT
10285}
10286
43f3e411 10287/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10288 PER_CU. */
10289
10290static void
43f3e411 10291compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10292{
f4dc4d17
DE
10293 gdb_assert (! per_cu->is_debug_types);
10294
ae640021 10295 if (!per_cu->imported_symtabs_empty ())
95554aad 10296 {
ae640021 10297 int len;
4c39bc03 10298 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10299 htab_t all_children, all_type_symtabs;
43f3e411 10300 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10301
10302 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10303 if (cust == NULL)
95554aad
TT
10304 return;
10305
10306 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10307 NULL, xcalloc, xfree);
ec94af83
DE
10308 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10309 NULL, xcalloc, xfree);
95554aad 10310
ae640021 10311 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10312 {
10313 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10314 all_type_symtabs, ptr, cust);
ec94af83 10315 }
95554aad 10316
ec94af83 10317 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10318 len = result_symtabs.size ();
43f3e411 10319 cust->includes
ed2dc618 10320 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10321 struct compunit_symtab *, len + 1);
4c39bc03
TT
10322 memcpy (cust->includes, result_symtabs.data (),
10323 len * sizeof (compunit_symtab *));
43f3e411 10324 cust->includes[len] = NULL;
95554aad 10325
95554aad 10326 htab_delete (all_children);
ec94af83 10327 htab_delete (all_type_symtabs);
95554aad
TT
10328 }
10329}
10330
10331/* Compute the 'includes' field for the symtabs of all the CUs we just
10332 read. */
10333
10334static void
ed2dc618 10335process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10336{
71b73764 10337 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10338 {
10339 if (! iter->is_debug_types)
43f3e411 10340 compute_compunit_symtab_includes (iter);
f4dc4d17 10341 }
95554aad 10342
c5d0225d 10343 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10344}
10345
9cdd5dbd 10346/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10347 already been loaded into memory. */
10348
10349static void
95554aad
TT
10350process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10351 enum language pretend_language)
10b3939b 10352{
10b3939b 10353 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10354 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10355 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10356 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10357 CORE_ADDR lowpc, highpc;
43f3e411 10358 struct compunit_symtab *cust;
10b3939b 10359 CORE_ADDR baseaddr;
4359dff1 10360 struct block *static_block;
3e29f34a 10361 CORE_ADDR addr;
10b3939b
DJ
10362
10363 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10364
c89b44cd
TT
10365 /* Clear the list here in case something was left over. */
10366 cu->method_list.clear ();
10b3939b 10367
95554aad
TT
10368 cu->language = pretend_language;
10369 cu->language_defn = language_def (cu->language);
10370
c906108c 10371 /* Do line number decoding in read_file_scope () */
10b3939b 10372 process_die (cu->dies, cu);
c906108c 10373
a766d390
DE
10374 /* For now fudge the Go package. */
10375 if (cu->language == language_go)
10376 fixup_go_packaging (cu);
10377
5f48f8f3 10378 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10379 should be complete, and it should now be safe to compute all of the
10380 physnames. */
10381 compute_delayed_physnames (cu);
3da10d80 10382
c9317f21
TT
10383 if (cu->language == language_rust)
10384 rust_union_quirks (cu);
10385
fae299cd
DC
10386 /* Some compilers don't define a DW_AT_high_pc attribute for the
10387 compilation unit. If the DW_AT_high_pc is missing, synthesize
10388 it, by scanning the DIE's below the compilation unit. */
10b3939b 10389 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10390
3e29f34a 10391 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10392 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10393
10394 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10395 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10396 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10397 addrmap to help ensure it has an accurate map of pc values belonging to
10398 this comp unit. */
10399 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10400
c24bdb02 10401 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10402 SECT_OFF_TEXT (objfile),
10403 0);
c906108c 10404
43f3e411 10405 if (cust != NULL)
c906108c 10406 {
df15bd07 10407 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10408
8be455d7
JK
10409 /* Set symtab language to language from DW_AT_language. If the
10410 compilation is from a C file generated by language preprocessors, do
10411 not set the language if it was already deduced by start_subfile. */
43f3e411 10412 if (!(cu->language == language_c
40e3ad0e 10413 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10414 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10415
10416 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10417 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10418 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10419 there were bugs in prologue debug info, fixed later in GCC-4.5
10420 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10421
10422 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10423 needed, it would be wrong due to missing DW_AT_producer there.
10424
10425 Still one can confuse GDB by using non-standard GCC compilation
10426 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10427 */
ab260dad 10428 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10429 cust->locations_valid = 1;
e0d00bc7
JK
10430
10431 if (gcc_4_minor >= 5)
43f3e411 10432 cust->epilogue_unwind_valid = 1;
96408a79 10433
43f3e411 10434 cust->call_site_htab = cu->call_site_htab;
c906108c 10435 }
9291a0cd
TT
10436
10437 if (dwarf2_per_objfile->using_index)
43f3e411 10438 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10439 else
10440 {
10441 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10442 pst->compunit_symtab = cust;
9291a0cd
TT
10443 pst->readin = 1;
10444 }
c906108c 10445
95554aad 10446 /* Push it for inclusion processing later. */
c5d0225d 10447 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10448
10449 /* Not needed any more. */
c24bdb02 10450 cu->reset_builder ();
f4dc4d17 10451}
45cfd468 10452
f4dc4d17
DE
10453/* Generate full symbol information for type unit PER_CU, whose DIEs have
10454 already been loaded into memory. */
10455
10456static void
10457process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10458 enum language pretend_language)
10459{
10460 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10461 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10462 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10463 struct compunit_symtab *cust;
0186c6a7
DE
10464 struct signatured_type *sig_type;
10465
10466 gdb_assert (per_cu->is_debug_types);
10467 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10468
c89b44cd
TT
10469 /* Clear the list here in case something was left over. */
10470 cu->method_list.clear ();
f4dc4d17 10471
f4dc4d17
DE
10472 cu->language = pretend_language;
10473 cu->language_defn = language_def (cu->language);
10474
10475 /* The symbol tables are set up in read_type_unit_scope. */
10476 process_die (cu->dies, cu);
10477
10478 /* For now fudge the Go package. */
10479 if (cu->language == language_go)
10480 fixup_go_packaging (cu);
10481
5f48f8f3 10482 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10483 should be complete, and it should now be safe to compute all of the
10484 physnames. */
10485 compute_delayed_physnames (cu);
f4dc4d17 10486
c9317f21
TT
10487 if (cu->language == language_rust)
10488 rust_union_quirks (cu);
10489
f4dc4d17
DE
10490 /* TUs share symbol tables.
10491 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10492 of it with end_expandable_symtab. Otherwise, complete the addition of
10493 this TU's symbols to the existing symtab. */
43f3e411 10494 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10495 {
c24bdb02
KS
10496 buildsym_compunit *builder = cu->get_builder ();
10497 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10498 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10499
43f3e411 10500 if (cust != NULL)
f4dc4d17
DE
10501 {
10502 /* Set symtab language to language from DW_AT_language. If the
10503 compilation is from a C file generated by language preprocessors,
10504 do not set the language if it was already deduced by
10505 start_subfile. */
43f3e411
DE
10506 if (!(cu->language == language_c
10507 && COMPUNIT_FILETABS (cust)->language != language_c))
10508 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10509 }
10510 }
10511 else
10512 {
c24bdb02 10513 cu->get_builder ()->augment_type_symtab ();
43f3e411 10514 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10515 }
10516
10517 if (dwarf2_per_objfile->using_index)
43f3e411 10518 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10519 else
10520 {
10521 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10522 pst->compunit_symtab = cust;
f4dc4d17 10523 pst->readin = 1;
45cfd468 10524 }
804d2729
TT
10525
10526 /* Not needed any more. */
c24bdb02 10527 cu->reset_builder ();
c906108c
SS
10528}
10529
95554aad
TT
10530/* Process an imported unit DIE. */
10531
10532static void
10533process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10534{
10535 struct attribute *attr;
10536
f4dc4d17
DE
10537 /* For now we don't handle imported units in type units. */
10538 if (cu->per_cu->is_debug_types)
10539 {
10540 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10541 " supported in type units [in module %s]"),
518817b3 10542 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10543 }
10544
95554aad
TT
10545 attr = dwarf2_attr (die, DW_AT_import, cu);
10546 if (attr != NULL)
10547 {
9c541725
PA
10548 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10549 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10550 dwarf2_per_cu_data *per_cu
e3b94546 10551 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10552 cu->per_cu->dwarf2_per_objfile);
95554aad 10553
69d751e3 10554 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10555 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10556 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10557
ae640021 10558 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10559 }
10560}
10561
4c8aa72d
PA
10562/* RAII object that represents a process_die scope: i.e.,
10563 starts/finishes processing a DIE. */
10564class process_die_scope
adde2bff 10565{
4c8aa72d
PA
10566public:
10567 process_die_scope (die_info *die, dwarf2_cu *cu)
10568 : m_die (die), m_cu (cu)
10569 {
10570 /* We should only be processing DIEs not already in process. */
10571 gdb_assert (!m_die->in_process);
10572 m_die->in_process = true;
10573 }
8c3cb9fa 10574
4c8aa72d
PA
10575 ~process_die_scope ()
10576 {
10577 m_die->in_process = false;
10578
10579 /* If we're done processing the DIE for the CU that owns the line
10580 header, we don't need the line header anymore. */
10581 if (m_cu->line_header_die_owner == m_die)
10582 {
10583 delete m_cu->line_header;
10584 m_cu->line_header = NULL;
10585 m_cu->line_header_die_owner = NULL;
10586 }
10587 }
10588
10589private:
10590 die_info *m_die;
10591 dwarf2_cu *m_cu;
10592};
adde2bff 10593
c906108c
SS
10594/* Process a die and its children. */
10595
10596static void
e7c27a73 10597process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10598{
4c8aa72d 10599 process_die_scope scope (die, cu);
adde2bff 10600
c906108c
SS
10601 switch (die->tag)
10602 {
10603 case DW_TAG_padding:
10604 break;
10605 case DW_TAG_compile_unit:
95554aad 10606 case DW_TAG_partial_unit:
e7c27a73 10607 read_file_scope (die, cu);
c906108c 10608 break;
348e048f
DE
10609 case DW_TAG_type_unit:
10610 read_type_unit_scope (die, cu);
10611 break;
c906108c 10612 case DW_TAG_subprogram:
0a4b0913
AB
10613 /* Nested subprograms in Fortran get a prefix. */
10614 if (cu->language == language_fortran
10615 && die->parent != NULL
10616 && die->parent->tag == DW_TAG_subprogram)
10617 cu->processing_has_namespace_info = true;
10618 /* Fall through. */
c906108c 10619 case DW_TAG_inlined_subroutine:
edb3359d 10620 read_func_scope (die, cu);
c906108c
SS
10621 break;
10622 case DW_TAG_lexical_block:
14898363
L
10623 case DW_TAG_try_block:
10624 case DW_TAG_catch_block:
e7c27a73 10625 read_lexical_block_scope (die, cu);
c906108c 10626 break;
216f72a1 10627 case DW_TAG_call_site:
96408a79
SA
10628 case DW_TAG_GNU_call_site:
10629 read_call_site_scope (die, cu);
10630 break;
c906108c 10631 case DW_TAG_class_type:
680b30c7 10632 case DW_TAG_interface_type:
c906108c
SS
10633 case DW_TAG_structure_type:
10634 case DW_TAG_union_type:
134d01f1 10635 process_structure_scope (die, cu);
c906108c
SS
10636 break;
10637 case DW_TAG_enumeration_type:
134d01f1 10638 process_enumeration_scope (die, cu);
c906108c 10639 break;
134d01f1 10640
f792889a
DJ
10641 /* These dies have a type, but processing them does not create
10642 a symbol or recurse to process the children. Therefore we can
10643 read them on-demand through read_type_die. */
c906108c 10644 case DW_TAG_subroutine_type:
72019c9c 10645 case DW_TAG_set_type:
c906108c 10646 case DW_TAG_array_type:
c906108c 10647 case DW_TAG_pointer_type:
c906108c 10648 case DW_TAG_ptr_to_member_type:
c906108c 10649 case DW_TAG_reference_type:
4297a3f0 10650 case DW_TAG_rvalue_reference_type:
c906108c 10651 case DW_TAG_string_type:
c906108c 10652 break;
134d01f1 10653
c906108c 10654 case DW_TAG_base_type:
a02abb62 10655 case DW_TAG_subrange_type:
cb249c71 10656 case DW_TAG_typedef:
134d01f1
DJ
10657 /* Add a typedef symbol for the type definition, if it has a
10658 DW_AT_name. */
f792889a 10659 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10660 break;
c906108c 10661 case DW_TAG_common_block:
e7c27a73 10662 read_common_block (die, cu);
c906108c
SS
10663 break;
10664 case DW_TAG_common_inclusion:
10665 break;
d9fa45fe 10666 case DW_TAG_namespace:
9068261f 10667 cu->processing_has_namespace_info = true;
e7c27a73 10668 read_namespace (die, cu);
d9fa45fe 10669 break;
5d7cb8df 10670 case DW_TAG_module:
9068261f 10671 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10672 read_module (die, cu);
10673 break;
d9fa45fe 10674 case DW_TAG_imported_declaration:
9068261f 10675 cu->processing_has_namespace_info = true;
74921315
KS
10676 if (read_namespace_alias (die, cu))
10677 break;
86a73007
TT
10678 /* The declaration is not a global namespace alias. */
10679 /* Fall through. */
d9fa45fe 10680 case DW_TAG_imported_module:
9068261f 10681 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10682 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10683 || cu->language != language_fortran))
b98664d3 10684 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10685 dwarf_tag_name (die->tag));
10686 read_import_statement (die, cu);
d9fa45fe 10687 break;
95554aad
TT
10688
10689 case DW_TAG_imported_unit:
10690 process_imported_unit_die (die, cu);
10691 break;
10692
71a3c369
TT
10693 case DW_TAG_variable:
10694 read_variable (die, cu);
10695 break;
10696
c906108c 10697 default:
e7c27a73 10698 new_symbol (die, NULL, cu);
c906108c
SS
10699 break;
10700 }
10701}
ca69b9e6
DE
10702\f
10703/* DWARF name computation. */
c906108c 10704
94af9270
KS
10705/* A helper function for dwarf2_compute_name which determines whether DIE
10706 needs to have the name of the scope prepended to the name listed in the
10707 die. */
10708
10709static int
10710die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10711{
1c809c68
TT
10712 struct attribute *attr;
10713
94af9270
KS
10714 switch (die->tag)
10715 {
10716 case DW_TAG_namespace:
10717 case DW_TAG_typedef:
10718 case DW_TAG_class_type:
10719 case DW_TAG_interface_type:
10720 case DW_TAG_structure_type:
10721 case DW_TAG_union_type:
10722 case DW_TAG_enumeration_type:
10723 case DW_TAG_enumerator:
10724 case DW_TAG_subprogram:
08a76f8a 10725 case DW_TAG_inlined_subroutine:
94af9270 10726 case DW_TAG_member:
74921315 10727 case DW_TAG_imported_declaration:
94af9270
KS
10728 return 1;
10729
10730 case DW_TAG_variable:
c2b0a229 10731 case DW_TAG_constant:
94af9270
KS
10732 /* We only need to prefix "globally" visible variables. These include
10733 any variable marked with DW_AT_external or any variable that
10734 lives in a namespace. [Variables in anonymous namespaces
10735 require prefixing, but they are not DW_AT_external.] */
10736
10737 if (dwarf2_attr (die, DW_AT_specification, cu))
10738 {
10739 struct dwarf2_cu *spec_cu = cu;
9a619af0 10740
94af9270
KS
10741 return die_needs_namespace (die_specification (die, &spec_cu),
10742 spec_cu);
10743 }
10744
1c809c68 10745 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10746 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10747 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10748 return 0;
10749 /* A variable in a lexical block of some kind does not need a
10750 namespace, even though in C++ such variables may be external
10751 and have a mangled name. */
10752 if (die->parent->tag == DW_TAG_lexical_block
10753 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10754 || die->parent->tag == DW_TAG_catch_block
10755 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10756 return 0;
10757 return 1;
94af9270
KS
10758
10759 default:
10760 return 0;
10761 }
10762}
10763
73b9be8b
KS
10764/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10765 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10766 defined for the given DIE. */
10767
10768static struct attribute *
10769dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10770{
10771 struct attribute *attr;
10772
10773 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10774 if (attr == NULL)
10775 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10776
10777 return attr;
10778}
10779
10780/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10781 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10782 defined for the given DIE. */
10783
10784static const char *
10785dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10786{
10787 const char *linkage_name;
10788
10789 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10790 if (linkage_name == NULL)
10791 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10792
10793 return linkage_name;
10794}
10795
94af9270 10796/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10797 compute the physname for the object, which include a method's:
9c37b5ae 10798 - formal parameters (C++),
a766d390 10799 - receiver type (Go),
a766d390
DE
10800
10801 The term "physname" is a bit confusing.
10802 For C++, for example, it is the demangled name.
10803 For Go, for example, it's the mangled name.
94af9270 10804
af6b7be1
JB
10805 For Ada, return the DIE's linkage name rather than the fully qualified
10806 name. PHYSNAME is ignored..
10807
94af9270
KS
10808 The result is allocated on the objfile_obstack and canonicalized. */
10809
10810static const char *
15d034d0
TT
10811dwarf2_compute_name (const char *name,
10812 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10813 int physname)
10814{
518817b3 10815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10816
94af9270
KS
10817 if (name == NULL)
10818 name = dwarf2_name (die, cu);
10819
2ee7123e
DE
10820 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10821 but otherwise compute it by typename_concat inside GDB.
10822 FIXME: Actually this is not really true, or at least not always true.
10823 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10824 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10825 will set the demangled name to the result of dwarf2_full_name, and it is
10826 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10827 if (cu->language == language_ada
10828 || (cu->language == language_fortran && physname))
10829 {
10830 /* For Ada unit, we prefer the linkage name over the name, as
10831 the former contains the exported name, which the user expects
10832 to be able to reference. Ideally, we want the user to be able
10833 to reference this entity using either natural or linkage name,
10834 but we haven't started looking at this enhancement yet. */
73b9be8b 10835 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10836
2ee7123e
DE
10837 if (linkage_name != NULL)
10838 return linkage_name;
f55ee35c
JK
10839 }
10840
94af9270
KS
10841 /* These are the only languages we know how to qualify names in. */
10842 if (name != NULL
9c37b5ae 10843 && (cu->language == language_cplus
c44af4eb
TT
10844 || cu->language == language_fortran || cu->language == language_d
10845 || cu->language == language_rust))
94af9270
KS
10846 {
10847 if (die_needs_namespace (die, cu))
10848 {
0d5cff50 10849 const char *prefix;
34a68019 10850 const char *canonical_name = NULL;
94af9270 10851
d7e74731
PA
10852 string_file buf;
10853
94af9270 10854 prefix = determine_prefix (die, cu);
94af9270
KS
10855 if (*prefix != '\0')
10856 {
f55ee35c
JK
10857 char *prefixed_name = typename_concat (NULL, prefix, name,
10858 physname, cu);
9a619af0 10859
d7e74731 10860 buf.puts (prefixed_name);
94af9270
KS
10861 xfree (prefixed_name);
10862 }
10863 else
d7e74731 10864 buf.puts (name);
94af9270 10865
98bfdba5
PA
10866 /* Template parameters may be specified in the DIE's DW_AT_name, or
10867 as children with DW_TAG_template_type_param or
10868 DW_TAG_value_type_param. If the latter, add them to the name
10869 here. If the name already has template parameters, then
10870 skip this step; some versions of GCC emit both, and
10871 it is more efficient to use the pre-computed name.
10872
10873 Something to keep in mind about this process: it is very
10874 unlikely, or in some cases downright impossible, to produce
10875 something that will match the mangled name of a function.
10876 If the definition of the function has the same debug info,
10877 we should be able to match up with it anyway. But fallbacks
10878 using the minimal symbol, for instance to find a method
10879 implemented in a stripped copy of libstdc++, will not work.
10880 If we do not have debug info for the definition, we will have to
10881 match them up some other way.
10882
10883 When we do name matching there is a related problem with function
10884 templates; two instantiated function templates are allowed to
10885 differ only by their return types, which we do not add here. */
10886
10887 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10888 {
10889 struct attribute *attr;
10890 struct die_info *child;
10891 int first = 1;
10892
10893 die->building_fullname = 1;
10894
10895 for (child = die->child; child != NULL; child = child->sibling)
10896 {
10897 struct type *type;
12df843f 10898 LONGEST value;
d521ce57 10899 const gdb_byte *bytes;
98bfdba5
PA
10900 struct dwarf2_locexpr_baton *baton;
10901 struct value *v;
10902
10903 if (child->tag != DW_TAG_template_type_param
10904 && child->tag != DW_TAG_template_value_param)
10905 continue;
10906
10907 if (first)
10908 {
d7e74731 10909 buf.puts ("<");
98bfdba5
PA
10910 first = 0;
10911 }
10912 else
d7e74731 10913 buf.puts (", ");
98bfdba5
PA
10914
10915 attr = dwarf2_attr (child, DW_AT_type, cu);
10916 if (attr == NULL)
10917 {
b98664d3 10918 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10919 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10920 continue;
10921 }
10922 type = die_type (child, cu);
10923
10924 if (child->tag == DW_TAG_template_type_param)
10925 {
c1ec8cea
TT
10926 c_print_type (type, "", &buf, -1, 0, cu->language,
10927 &type_print_raw_options);
98bfdba5
PA
10928 continue;
10929 }
10930
10931 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10932 if (attr == NULL)
10933 {
b98664d3 10934 complaint (_("template parameter missing "
3e43a32a 10935 "DW_AT_const_value"));
d7e74731 10936 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10937 continue;
10938 }
10939
10940 dwarf2_const_value_attr (attr, type, name,
10941 &cu->comp_unit_obstack, cu,
10942 &value, &bytes, &baton);
10943
10944 if (TYPE_NOSIGN (type))
10945 /* GDB prints characters as NUMBER 'CHAR'. If that's
10946 changed, this can use value_print instead. */
d7e74731 10947 c_printchar (value, type, &buf);
98bfdba5
PA
10948 else
10949 {
10950 struct value_print_options opts;
10951
10952 if (baton != NULL)
10953 v = dwarf2_evaluate_loc_desc (type, NULL,
10954 baton->data,
10955 baton->size,
10956 baton->per_cu);
10957 else if (bytes != NULL)
10958 {
10959 v = allocate_value (type);
10960 memcpy (value_contents_writeable (v), bytes,
10961 TYPE_LENGTH (type));
10962 }
10963 else
10964 v = value_from_longest (type, value);
10965
3e43a32a
MS
10966 /* Specify decimal so that we do not depend on
10967 the radix. */
98bfdba5
PA
10968 get_formatted_print_options (&opts, 'd');
10969 opts.raw = 1;
d7e74731 10970 value_print (v, &buf, &opts);
98bfdba5 10971 release_value (v);
98bfdba5
PA
10972 }
10973 }
10974
10975 die->building_fullname = 0;
10976
10977 if (!first)
10978 {
10979 /* Close the argument list, with a space if necessary
10980 (nested templates). */
d7e74731
PA
10981 if (!buf.empty () && buf.string ().back () == '>')
10982 buf.puts (" >");
98bfdba5 10983 else
d7e74731 10984 buf.puts (">");
98bfdba5
PA
10985 }
10986 }
10987
9c37b5ae 10988 /* For C++ methods, append formal parameter type
94af9270 10989 information, if PHYSNAME. */
6e70227d 10990
94af9270 10991 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10992 && cu->language == language_cplus)
94af9270
KS
10993 {
10994 struct type *type = read_type_die (die, cu);
10995
d7e74731 10996 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10997 &type_print_raw_options);
94af9270 10998
9c37b5ae 10999 if (cu->language == language_cplus)
94af9270 11000 {
60430eff
DJ
11001 /* Assume that an artificial first parameter is
11002 "this", but do not crash if it is not. RealView
11003 marks unnamed (and thus unused) parameters as
11004 artificial; there is no way to differentiate
11005 the two cases. */
94af9270
KS
11006 if (TYPE_NFIELDS (type) > 0
11007 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11008 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11009 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11010 0))))
d7e74731 11011 buf.puts (" const");
94af9270
KS
11012 }
11013 }
11014
d7e74731 11015 const std::string &intermediate_name = buf.string ();
94af9270
KS
11016
11017 if (cu->language == language_cplus)
34a68019 11018 canonical_name
322a8516 11019 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11020 &objfile->per_bfd->storage_obstack);
11021
11022 /* If we only computed INTERMEDIATE_NAME, or if
11023 INTERMEDIATE_NAME is already canonical, then we need to
11024 copy it to the appropriate obstack. */
322a8516 11025 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11026 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11027 intermediate_name);
34a68019
TT
11028 else
11029 name = canonical_name;
94af9270
KS
11030 }
11031 }
11032
11033 return name;
11034}
11035
0114d602
DJ
11036/* Return the fully qualified name of DIE, based on its DW_AT_name.
11037 If scope qualifiers are appropriate they will be added. The result
34a68019 11038 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11039 not have a name. NAME may either be from a previous call to
11040 dwarf2_name or NULL.
11041
9c37b5ae 11042 The output string will be canonicalized (if C++). */
0114d602
DJ
11043
11044static const char *
15d034d0 11045dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11046{
94af9270
KS
11047 return dwarf2_compute_name (name, die, cu, 0);
11048}
0114d602 11049
94af9270
KS
11050/* Construct a physname for the given DIE in CU. NAME may either be
11051 from a previous call to dwarf2_name or NULL. The result will be
11052 allocated on the objfile_objstack or NULL if the DIE does not have a
11053 name.
0114d602 11054
9c37b5ae 11055 The output string will be canonicalized (if C++). */
0114d602 11056
94af9270 11057static const char *
15d034d0 11058dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11059{
518817b3 11060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11061 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11062 int need_copy = 1;
11063
11064 /* In this case dwarf2_compute_name is just a shortcut not building anything
11065 on its own. */
11066 if (!die_needs_namespace (die, cu))
11067 return dwarf2_compute_name (name, die, cu, 1);
11068
73b9be8b 11069 mangled = dw2_linkage_name (die, cu);
900e11f9 11070
e98c9e7c
TT
11071 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11072 See https://github.com/rust-lang/rust/issues/32925. */
11073 if (cu->language == language_rust && mangled != NULL
11074 && strchr (mangled, '{') != NULL)
11075 mangled = NULL;
11076
900e11f9
JK
11077 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11078 has computed. */
791afaa2 11079 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11080 if (mangled != NULL)
900e11f9 11081 {
900e11f9 11082
59cc4834
JB
11083 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11084 {
11085 /* Do nothing (do not demangle the symbol name). */
11086 }
11087 else if (cu->language == language_go)
a766d390 11088 {
5e2db402
TT
11089 /* This is a lie, but we already lie to the caller new_symbol.
11090 new_symbol assumes we return the mangled name.
a766d390 11091 This just undoes that lie until things are cleaned up. */
a766d390
DE
11092 }
11093 else
11094 {
0eb876f5
JB
11095 /* Use DMGL_RET_DROP for C++ template functions to suppress
11096 their return type. It is easier for GDB users to search
11097 for such functions as `name(params)' than `long name(params)'.
11098 In such case the minimal symbol names do not match the full
11099 symbol names but for template functions there is never a need
11100 to look up their definition from their declaration so
11101 the only disadvantage remains the minimal symbol variant
11102 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11103 demangled.reset (gdb_demangle (mangled,
11104 (DMGL_PARAMS | DMGL_ANSI
11105 | DMGL_RET_DROP)));
a766d390 11106 }
900e11f9 11107 if (demangled)
791afaa2 11108 canon = demangled.get ();
900e11f9
JK
11109 else
11110 {
11111 canon = mangled;
11112 need_copy = 0;
11113 }
11114 }
11115
11116 if (canon == NULL || check_physname)
11117 {
11118 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11119
11120 if (canon != NULL && strcmp (physname, canon) != 0)
11121 {
11122 /* It may not mean a bug in GDB. The compiler could also
11123 compute DW_AT_linkage_name incorrectly. But in such case
11124 GDB would need to be bug-to-bug compatible. */
11125
b98664d3 11126 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11127 "(from linkage <%s>) - DIE at %s [in module %s]"),
11128 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11129 objfile_name (objfile));
900e11f9
JK
11130
11131 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11132 is available here - over computed PHYSNAME. It is safer
11133 against both buggy GDB and buggy compilers. */
11134
11135 retval = canon;
11136 }
11137 else
11138 {
11139 retval = physname;
11140 need_copy = 0;
11141 }
11142 }
11143 else
11144 retval = canon;
11145
11146 if (need_copy)
021887d8 11147 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11148
900e11f9 11149 return retval;
0114d602
DJ
11150}
11151
74921315
KS
11152/* Inspect DIE in CU for a namespace alias. If one exists, record
11153 a new symbol for it.
11154
11155 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11156
11157static int
11158read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11159{
11160 struct attribute *attr;
11161
11162 /* If the die does not have a name, this is not a namespace
11163 alias. */
11164 attr = dwarf2_attr (die, DW_AT_name, cu);
11165 if (attr != NULL)
11166 {
11167 int num;
11168 struct die_info *d = die;
11169 struct dwarf2_cu *imported_cu = cu;
11170
11171 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11172 keep inspecting DIEs until we hit the underlying import. */
11173#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11174 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11175 {
11176 attr = dwarf2_attr (d, DW_AT_import, cu);
11177 if (attr == NULL)
11178 break;
11179
11180 d = follow_die_ref (d, attr, &imported_cu);
11181 if (d->tag != DW_TAG_imported_declaration)
11182 break;
11183 }
11184
11185 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11186 {
b98664d3 11187 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11188 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11189 return 0;
11190 }
11191
11192 if (attr != NULL)
11193 {
11194 struct type *type;
9c541725 11195 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11196
9c541725 11197 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11198 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11199 {
11200 /* This declaration is a global namespace alias. Add
11201 a symbol for it whose type is the aliased namespace. */
11202 new_symbol (die, type, cu);
11203 return 1;
11204 }
11205 }
11206 }
11207
11208 return 0;
11209}
11210
22cee43f 11211/* Return the using directives repository (global or local?) to use in the
804d2729 11212 current context for CU.
22cee43f
PMR
11213
11214 For Ada, imported declarations can materialize renamings, which *may* be
11215 global. However it is impossible (for now?) in DWARF to distinguish
11216 "external" imported declarations and "static" ones. As all imported
11217 declarations seem to be static in all other languages, make them all CU-wide
11218 global only in Ada. */
11219
11220static struct using_direct **
804d2729 11221using_directives (struct dwarf2_cu *cu)
22cee43f 11222{
c24bdb02
KS
11223 if (cu->language == language_ada
11224 && cu->get_builder ()->outermost_context_p ())
11225 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11226 else
c24bdb02 11227 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11228}
11229
27aa8d6a
SW
11230/* Read the import statement specified by the given die and record it. */
11231
11232static void
11233read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11234{
518817b3 11235 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11236 struct attribute *import_attr;
32019081 11237 struct die_info *imported_die, *child_die;
de4affc9 11238 struct dwarf2_cu *imported_cu;
27aa8d6a 11239 const char *imported_name;
794684b6 11240 const char *imported_name_prefix;
13387711
SW
11241 const char *canonical_name;
11242 const char *import_alias;
11243 const char *imported_declaration = NULL;
794684b6 11244 const char *import_prefix;
eb1e02fd 11245 std::vector<const char *> excludes;
13387711 11246
27aa8d6a
SW
11247 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11248 if (import_attr == NULL)
11249 {
b98664d3 11250 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11251 dwarf_tag_name (die->tag));
11252 return;
11253 }
11254
de4affc9
CC
11255 imported_cu = cu;
11256 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11257 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11258 if (imported_name == NULL)
11259 {
11260 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11261
11262 The import in the following code:
11263 namespace A
11264 {
11265 typedef int B;
11266 }
11267
11268 int main ()
11269 {
11270 using A::B;
11271 B b;
11272 return b;
11273 }
11274
11275 ...
11276 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11277 <52> DW_AT_decl_file : 1
11278 <53> DW_AT_decl_line : 6
11279 <54> DW_AT_import : <0x75>
11280 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11281 <59> DW_AT_name : B
11282 <5b> DW_AT_decl_file : 1
11283 <5c> DW_AT_decl_line : 2
11284 <5d> DW_AT_type : <0x6e>
11285 ...
11286 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11287 <76> DW_AT_byte_size : 4
11288 <77> DW_AT_encoding : 5 (signed)
11289
11290 imports the wrong die ( 0x75 instead of 0x58 ).
11291 This case will be ignored until the gcc bug is fixed. */
11292 return;
11293 }
11294
82856980
SW
11295 /* Figure out the local name after import. */
11296 import_alias = dwarf2_name (die, cu);
27aa8d6a 11297
794684b6
SW
11298 /* Figure out where the statement is being imported to. */
11299 import_prefix = determine_prefix (die, cu);
11300
11301 /* Figure out what the scope of the imported die is and prepend it
11302 to the name of the imported die. */
de4affc9 11303 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11304
f55ee35c
JK
11305 if (imported_die->tag != DW_TAG_namespace
11306 && imported_die->tag != DW_TAG_module)
794684b6 11307 {
13387711
SW
11308 imported_declaration = imported_name;
11309 canonical_name = imported_name_prefix;
794684b6 11310 }
13387711 11311 else if (strlen (imported_name_prefix) > 0)
12aaed36 11312 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11313 imported_name_prefix,
11314 (cu->language == language_d ? "." : "::"),
11315 imported_name, (char *) NULL);
13387711
SW
11316 else
11317 canonical_name = imported_name;
794684b6 11318
32019081
JK
11319 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11320 for (child_die = die->child; child_die && child_die->tag;
11321 child_die = sibling_die (child_die))
11322 {
11323 /* DWARF-4: A Fortran use statement with a “rename list” may be
11324 represented by an imported module entry with an import attribute
11325 referring to the module and owned entries corresponding to those
11326 entities that are renamed as part of being imported. */
11327
11328 if (child_die->tag != DW_TAG_imported_declaration)
11329 {
b98664d3 11330 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11331 "- DIE at %s [in module %s]"),
11332 sect_offset_str (child_die->sect_off),
11333 objfile_name (objfile));
32019081
JK
11334 continue;
11335 }
11336
11337 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11338 if (import_attr == NULL)
11339 {
b98664d3 11340 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11341 dwarf_tag_name (child_die->tag));
11342 continue;
11343 }
11344
11345 imported_cu = cu;
11346 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11347 &imported_cu);
11348 imported_name = dwarf2_name (imported_die, imported_cu);
11349 if (imported_name == NULL)
11350 {
b98664d3 11351 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11352 "imported name - DIE at %s [in module %s]"),
11353 sect_offset_str (child_die->sect_off),
11354 objfile_name (objfile));
32019081
JK
11355 continue;
11356 }
11357
eb1e02fd 11358 excludes.push_back (imported_name);
32019081
JK
11359
11360 process_die (child_die, cu);
11361 }
11362
804d2729 11363 add_using_directive (using_directives (cu),
22cee43f
PMR
11364 import_prefix,
11365 canonical_name,
11366 import_alias,
11367 imported_declaration,
11368 excludes,
11369 0,
11370 &objfile->objfile_obstack);
27aa8d6a
SW
11371}
11372
5230b05a
WT
11373/* ICC<14 does not output the required DW_AT_declaration on incomplete
11374 types, but gives them a size of zero. Starting with version 14,
11375 ICC is compatible with GCC. */
11376
9068261f 11377static bool
5230b05a
WT
11378producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11379{
11380 if (!cu->checked_producer)
11381 check_producer (cu);
11382
11383 return cu->producer_is_icc_lt_14;
11384}
11385
eb77c9df
AB
11386/* ICC generates a DW_AT_type for C void functions. This was observed on
11387 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11388 which says that void functions should not have a DW_AT_type. */
11389
11390static bool
11391producer_is_icc (struct dwarf2_cu *cu)
11392{
11393 if (!cu->checked_producer)
11394 check_producer (cu);
11395
11396 return cu->producer_is_icc;
11397}
11398
1b80a9fa
JK
11399/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11400 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11401 this, it was first present in GCC release 4.3.0. */
11402
9068261f 11403static bool
1b80a9fa
JK
11404producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11405{
11406 if (!cu->checked_producer)
11407 check_producer (cu);
11408
11409 return cu->producer_is_gcc_lt_4_3;
11410}
11411
d721ba37
PA
11412static file_and_directory
11413find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11414{
d721ba37
PA
11415 file_and_directory res;
11416
9291a0cd
TT
11417 /* Find the filename. Do not use dwarf2_name here, since the filename
11418 is not a source language identifier. */
d721ba37
PA
11419 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11420 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11421
d721ba37
PA
11422 if (res.comp_dir == NULL
11423 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11424 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11425 {
d721ba37
PA
11426 res.comp_dir_storage = ldirname (res.name);
11427 if (!res.comp_dir_storage.empty ())
11428 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11429 }
d721ba37 11430 if (res.comp_dir != NULL)
9291a0cd
TT
11431 {
11432 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11433 directory, get rid of it. */
d721ba37 11434 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11435
d721ba37
PA
11436 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11437 res.comp_dir = cp + 1;
9291a0cd
TT
11438 }
11439
d721ba37
PA
11440 if (res.name == NULL)
11441 res.name = "<unknown>";
11442
11443 return res;
9291a0cd
TT
11444}
11445
f4dc4d17
DE
11446/* Handle DW_AT_stmt_list for a compilation unit.
11447 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11448 COMP_DIR is the compilation directory. LOWPC is passed to
11449 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11450
11451static void
11452handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11453 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11454{
518817b3
SM
11455 struct dwarf2_per_objfile *dwarf2_per_objfile
11456 = cu->per_cu->dwarf2_per_objfile;
527f3840 11457 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11458 struct attribute *attr;
527f3840
JK
11459 struct line_header line_header_local;
11460 hashval_t line_header_local_hash;
527f3840
JK
11461 void **slot;
11462 int decode_mapping;
2ab95328 11463
f4dc4d17
DE
11464 gdb_assert (! cu->per_cu->is_debug_types);
11465
2ab95328 11466 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11467 if (attr == NULL)
11468 return;
11469
9c541725 11470 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11471
11472 /* The line header hash table is only created if needed (it exists to
11473 prevent redundant reading of the line table for partial_units).
11474 If we're given a partial_unit, we'll need it. If we're given a
11475 compile_unit, then use the line header hash table if it's already
11476 created, but don't create one just yet. */
11477
11478 if (dwarf2_per_objfile->line_header_hash == NULL
11479 && die->tag == DW_TAG_partial_unit)
2ab95328 11480 {
527f3840
JK
11481 dwarf2_per_objfile->line_header_hash
11482 = htab_create_alloc_ex (127, line_header_hash_voidp,
11483 line_header_eq_voidp,
11484 free_line_header_voidp,
11485 &objfile->objfile_obstack,
11486 hashtab_obstack_allocate,
11487 dummy_obstack_deallocate);
11488 }
2ab95328 11489
9c541725 11490 line_header_local.sect_off = line_offset;
527f3840
JK
11491 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11492 line_header_local_hash = line_header_hash (&line_header_local);
11493 if (dwarf2_per_objfile->line_header_hash != NULL)
11494 {
11495 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11496 &line_header_local,
11497 line_header_local_hash, NO_INSERT);
11498
11499 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11500 is not present in *SLOT (since if there is something in *SLOT then
11501 it will be for a partial_unit). */
11502 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11503 {
527f3840 11504 gdb_assert (*slot != NULL);
9a3c8263 11505 cu->line_header = (struct line_header *) *slot;
527f3840 11506 return;
dee91e82 11507 }
2ab95328 11508 }
527f3840
JK
11509
11510 /* dwarf_decode_line_header does not yet provide sufficient information.
11511 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11512 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11513 if (lh == NULL)
527f3840 11514 return;
4c8aa72d
PA
11515
11516 cu->line_header = lh.release ();
11517 cu->line_header_die_owner = die;
527f3840
JK
11518
11519 if (dwarf2_per_objfile->line_header_hash == NULL)
11520 slot = NULL;
11521 else
11522 {
11523 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11524 &line_header_local,
11525 line_header_local_hash, INSERT);
11526 gdb_assert (slot != NULL);
11527 }
11528 if (slot != NULL && *slot == NULL)
11529 {
11530 /* This newly decoded line number information unit will be owned
11531 by line_header_hash hash table. */
11532 *slot = cu->line_header;
4c8aa72d 11533 cu->line_header_die_owner = NULL;
527f3840
JK
11534 }
11535 else
11536 {
11537 /* We cannot free any current entry in (*slot) as that struct line_header
11538 may be already used by multiple CUs. Create only temporary decoded
11539 line_header for this CU - it may happen at most once for each line
11540 number information unit. And if we're not using line_header_hash
11541 then this is what we want as well. */
11542 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11543 }
11544 decode_mapping = (die->tag != DW_TAG_partial_unit);
11545 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11546 decode_mapping);
fff8551c 11547
2ab95328
TT
11548}
11549
95554aad 11550/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11551
c906108c 11552static void
e7c27a73 11553read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11554{
518817b3
SM
11555 struct dwarf2_per_objfile *dwarf2_per_objfile
11556 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11557 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11558 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11559 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11560 CORE_ADDR highpc = ((CORE_ADDR) 0);
11561 struct attribute *attr;
c906108c 11562 struct die_info *child_die;
e142c38c 11563 CORE_ADDR baseaddr;
6e70227d 11564
380618d6 11565 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11566 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11567
fae299cd 11568 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11569
11570 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11571 from finish_block. */
2acceee2 11572 if (lowpc == ((CORE_ADDR) -1))
c906108c 11573 lowpc = highpc;
3e29f34a 11574 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11575
d721ba37 11576 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11577
f4b8a18d
KW
11578 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11579 standardised yet. As a workaround for the language detection we fall
11580 back to the DW_AT_producer string. */
11581 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11582 cu->language = language_opencl;
11583
3019eac3
DE
11584 /* Similar hack for Go. */
11585 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11586 set_cu_language (DW_LANG_Go, cu);
11587
c24bdb02 11588 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11589
11590 /* Decode line number information if present. We do this before
11591 processing child DIEs, so that the line header table is available
11592 for DW_AT_decl_file. */
d721ba37 11593 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11594
11595 /* Process all dies in compilation unit. */
11596 if (die->child != NULL)
11597 {
11598 child_die = die->child;
11599 while (child_die && child_die->tag)
11600 {
11601 process_die (child_die, cu);
11602 child_die = sibling_die (child_die);
11603 }
11604 }
11605
11606 /* Decode macro information, if present. Dwarf 2 macro information
11607 refers to information in the line number info statement program
11608 header, so we can only read it if we've read the header
11609 successfully. */
0af92d60
JK
11610 attr = dwarf2_attr (die, DW_AT_macros, cu);
11611 if (attr == NULL)
11612 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11613 if (attr && cu->line_header)
11614 {
11615 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11616 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11617
43f3e411 11618 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11619 }
11620 else
11621 {
11622 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11623 if (attr && cu->line_header)
11624 {
11625 unsigned int macro_offset = DW_UNSND (attr);
11626
43f3e411 11627 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11628 }
11629 }
3019eac3
DE
11630}
11631
c24bdb02
KS
11632void
11633dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11634{
f4dc4d17
DE
11635 struct type_unit_group *tu_group;
11636 int first_time;
3019eac3 11637 struct attribute *attr;
9c541725 11638 unsigned int i;
0186c6a7 11639 struct signatured_type *sig_type;
3019eac3 11640
f4dc4d17 11641 gdb_assert (per_cu->is_debug_types);
0186c6a7 11642 sig_type = (struct signatured_type *) per_cu;
3019eac3 11643
c24bdb02 11644 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11645
f4dc4d17 11646 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11647 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11648 if (sig_type->type_unit_group == NULL)
c24bdb02 11649 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11650 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11651
11652 /* If we've already processed this stmt_list there's no real need to
11653 do it again, we could fake it and just recreate the part we need
11654 (file name,index -> symtab mapping). If data shows this optimization
11655 is useful we can do it then. */
43f3e411 11656 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11657
11658 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11659 debug info. */
fff8551c 11660 line_header_up lh;
f4dc4d17 11661 if (attr != NULL)
3019eac3 11662 {
9c541725 11663 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11664 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11665 }
11666 if (lh == NULL)
11667 {
11668 if (first_time)
c24bdb02 11669 start_symtab ("", NULL, 0);
f4dc4d17
DE
11670 else
11671 {
11672 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11673 gdb_assert (m_builder == nullptr);
804d2729 11674 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11675 m_builder.reset (new struct buildsym_compunit
11676 (COMPUNIT_OBJFILE (cust), "",
11677 COMPUNIT_DIRNAME (cust),
11678 compunit_language (cust),
11679 0, cust));
f4dc4d17 11680 }
f4dc4d17 11681 return;
3019eac3
DE
11682 }
11683
c24bdb02
KS
11684 line_header = lh.release ();
11685 line_header_die_owner = die;
3019eac3 11686
f4dc4d17
DE
11687 if (first_time)
11688 {
c24bdb02 11689 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11690
1fd60fc0
DE
11691 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11692 still initializing it, and our caller (a few levels up)
11693 process_full_type_unit still needs to know if this is the first
11694 time. */
11695
c24bdb02 11696 tu_group->num_symtabs = line_header->file_names.size ();
4c8aa72d 11697 tu_group->symtabs = XNEWVEC (struct symtab *,
c24bdb02 11698 line_header->file_names.size ());
3019eac3 11699
c24bdb02 11700 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11701 {
c24bdb02 11702 file_entry &fe = line_header->file_names[i];
3019eac3 11703
c24bdb02
KS
11704 dwarf2_start_subfile (this, fe.name,
11705 fe.include_dir (line_header));
11706 buildsym_compunit *b = get_builder ();
11707 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11708 {
4c8aa72d
PA
11709 /* NOTE: start_subfile will recognize when it's been
11710 passed a file it has already seen. So we can't
11711 assume there's a simple mapping from
11712 cu->line_header->file_names to subfiles, plus
11713 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11714 b->get_current_subfile ()->symtab
11715 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11716 }
11717
c24bdb02 11718 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11719 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11720 }
11721 }
11722 else
3019eac3 11723 {
c24bdb02 11724 gdb_assert (m_builder == nullptr);
804d2729 11725 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11726 m_builder.reset (new struct buildsym_compunit
11727 (COMPUNIT_OBJFILE (cust), "",
11728 COMPUNIT_DIRNAME (cust),
11729 compunit_language (cust),
11730 0, cust));
f4dc4d17 11731
c24bdb02 11732 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11733 {
c24bdb02 11734 file_entry &fe = line_header->file_names[i];
f4dc4d17 11735
4c8aa72d 11736 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11737 }
3019eac3
DE
11738 }
11739
f4dc4d17
DE
11740 /* The main symtab is allocated last. Type units don't have DW_AT_name
11741 so they don't have a "real" (so to speak) symtab anyway.
11742 There is later code that will assign the main symtab to all symbols
11743 that don't have one. We need to handle the case of a symbol with a
11744 missing symtab (DW_AT_decl_file) anyway. */
11745}
3019eac3 11746
f4dc4d17
DE
11747/* Process DW_TAG_type_unit.
11748 For TUs we want to skip the first top level sibling if it's not the
11749 actual type being defined by this TU. In this case the first top
11750 level sibling is there to provide context only. */
3019eac3 11751
f4dc4d17
DE
11752static void
11753read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11754{
11755 struct die_info *child_die;
3019eac3 11756
f4dc4d17
DE
11757 prepare_one_comp_unit (cu, die, language_minimal);
11758
11759 /* Initialize (or reinitialize) the machinery for building symtabs.
11760 We do this before processing child DIEs, so that the line header table
11761 is available for DW_AT_decl_file. */
c24bdb02 11762 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11763
11764 if (die->child != NULL)
11765 {
11766 child_die = die->child;
11767 while (child_die && child_die->tag)
11768 {
11769 process_die (child_die, cu);
11770 child_die = sibling_die (child_die);
11771 }
11772 }
3019eac3
DE
11773}
11774\f
80626a55
DE
11775/* DWO/DWP files.
11776
11777 http://gcc.gnu.org/wiki/DebugFission
11778 http://gcc.gnu.org/wiki/DebugFissionDWP
11779
11780 To simplify handling of both DWO files ("object" files with the DWARF info)
11781 and DWP files (a file with the DWOs packaged up into one file), we treat
11782 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11783
11784static hashval_t
11785hash_dwo_file (const void *item)
11786{
9a3c8263 11787 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11788 hashval_t hash;
3019eac3 11789
a2ce51a0
DE
11790 hash = htab_hash_string (dwo_file->dwo_name);
11791 if (dwo_file->comp_dir != NULL)
11792 hash += htab_hash_string (dwo_file->comp_dir);
11793 return hash;
3019eac3
DE
11794}
11795
11796static int
11797eq_dwo_file (const void *item_lhs, const void *item_rhs)
11798{
9a3c8263
SM
11799 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11800 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11801
a2ce51a0
DE
11802 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11803 return 0;
11804 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11805 return lhs->comp_dir == rhs->comp_dir;
11806 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11807}
11808
11809/* Allocate a hash table for DWO files. */
11810
51ac9db5 11811static htab_up
ed2dc618 11812allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11813{
51ac9db5
SM
11814 auto delete_dwo_file = [] (void *item)
11815 {
11816 struct dwo_file *dwo_file = (struct dwo_file *) item;
11817
11818 delete dwo_file;
11819 };
11820
11821 return htab_up (htab_create_alloc_ex (41,
11822 hash_dwo_file,
11823 eq_dwo_file,
11824 delete_dwo_file,
11825 &objfile->objfile_obstack,
11826 hashtab_obstack_allocate,
11827 dummy_obstack_deallocate));
3019eac3
DE
11828}
11829
80626a55
DE
11830/* Lookup DWO file DWO_NAME. */
11831
11832static void **
ed2dc618
SM
11833lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11834 const char *dwo_name,
11835 const char *comp_dir)
80626a55
DE
11836{
11837 struct dwo_file find_entry;
11838 void **slot;
11839
11840 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11841 dwarf2_per_objfile->dwo_files
11842 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11843
0ac5b59e
DE
11844 find_entry.dwo_name = dwo_name;
11845 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11846 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11847 INSERT);
80626a55
DE
11848
11849 return slot;
11850}
11851
3019eac3
DE
11852static hashval_t
11853hash_dwo_unit (const void *item)
11854{
9a3c8263 11855 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11856
11857 /* This drops the top 32 bits of the id, but is ok for a hash. */
11858 return dwo_unit->signature;
11859}
11860
11861static int
11862eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11863{
9a3c8263
SM
11864 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11865 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11866
11867 /* The signature is assumed to be unique within the DWO file.
11868 So while object file CU dwo_id's always have the value zero,
11869 that's OK, assuming each object file DWO file has only one CU,
11870 and that's the rule for now. */
11871 return lhs->signature == rhs->signature;
11872}
11873
11874/* Allocate a hash table for DWO CUs,TUs.
11875 There is one of these tables for each of CUs,TUs for each DWO file. */
11876
11877static htab_t
11878allocate_dwo_unit_table (struct objfile *objfile)
11879{
11880 /* Start out with a pretty small number.
11881 Generally DWO files contain only one CU and maybe some TUs. */
11882 return htab_create_alloc_ex (3,
11883 hash_dwo_unit,
11884 eq_dwo_unit,
11885 NULL,
11886 &objfile->objfile_obstack,
11887 hashtab_obstack_allocate,
11888 dummy_obstack_deallocate);
11889}
11890
80626a55 11891/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11892
19c3d4c9 11893struct create_dwo_cu_data
3019eac3
DE
11894{
11895 struct dwo_file *dwo_file;
19c3d4c9 11896 struct dwo_unit dwo_unit;
3019eac3
DE
11897};
11898
19c3d4c9 11899/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11900
11901static void
19c3d4c9
DE
11902create_dwo_cu_reader (const struct die_reader_specs *reader,
11903 const gdb_byte *info_ptr,
11904 struct die_info *comp_unit_die,
11905 int has_children,
11906 void *datap)
3019eac3
DE
11907{
11908 struct dwarf2_cu *cu = reader->cu;
9c541725 11909 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11910 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11911 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11912 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11913 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11914
a084a2a6
AT
11915 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11916 if (!signature.has_value ())
3019eac3 11917 {
b98664d3 11918 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11919 " its dwo_id [in module %s]"),
9d8780f0 11920 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11921 return;
11922 }
11923
3019eac3 11924 dwo_unit->dwo_file = dwo_file;
a084a2a6 11925 dwo_unit->signature = *signature;
8a0459fd 11926 dwo_unit->section = section;
9c541725 11927 dwo_unit->sect_off = sect_off;
3019eac3
DE
11928 dwo_unit->length = cu->per_cu->length;
11929
b4f54984 11930 if (dwarf_read_debug)
9d8780f0
SM
11931 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11932 sect_offset_str (sect_off),
9c541725 11933 hex_string (dwo_unit->signature));
3019eac3
DE
11934}
11935
33c5cd75 11936/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11937 Note: This function processes DWO files only, not DWP files. */
3019eac3 11938
33c5cd75 11939static void
ed2dc618
SM
11940create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11941 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11942 htab_t &cus_htab)
3019eac3
DE
11943{
11944 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11945 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11946
33c5cd75
DB
11947 dwarf2_read_section (objfile, &section);
11948 info_ptr = section.buffer;
3019eac3
DE
11949
11950 if (info_ptr == NULL)
33c5cd75 11951 return;
3019eac3 11952
b4f54984 11953 if (dwarf_read_debug)
19c3d4c9
DE
11954 {
11955 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11956 get_section_name (&section),
11957 get_section_file_name (&section));
19c3d4c9 11958 }
3019eac3 11959
33c5cd75 11960 end_ptr = info_ptr + section.size;
3019eac3
DE
11961 while (info_ptr < end_ptr)
11962 {
11963 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11964 struct create_dwo_cu_data create_dwo_cu_data;
11965 struct dwo_unit *dwo_unit;
11966 void **slot;
11967 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11968
19c3d4c9
DE
11969 memset (&create_dwo_cu_data.dwo_unit, 0,
11970 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11971 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11972 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11973 per_cu.is_debug_types = 0;
33c5cd75
DB
11974 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11975 per_cu.section = &section;
c5ed0576 11976 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11977
11978 init_cutu_and_read_dies_no_follow (
11979 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11980 info_ptr += per_cu.length;
11981
11982 // If the unit could not be parsed, skip it.
11983 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11984 continue;
3019eac3 11985
33c5cd75
DB
11986 if (cus_htab == NULL)
11987 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11988
33c5cd75
DB
11989 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11990 *dwo_unit = create_dwo_cu_data.dwo_unit;
11991 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11992 gdb_assert (slot != NULL);
11993 if (*slot != NULL)
19c3d4c9 11994 {
33c5cd75
DB
11995 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11996 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11997
b98664d3 11998 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11999 " the entry at offset %s, signature %s"),
12000 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12001 hex_string (dwo_unit->signature));
19c3d4c9 12002 }
33c5cd75 12003 *slot = (void *)dwo_unit;
3019eac3 12004 }
3019eac3
DE
12005}
12006
80626a55
DE
12007/* DWP file .debug_{cu,tu}_index section format:
12008 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12009
d2415c6c
DE
12010 DWP Version 1:
12011
80626a55
DE
12012 Both index sections have the same format, and serve to map a 64-bit
12013 signature to a set of section numbers. Each section begins with a header,
12014 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12015 indexes, and a pool of 32-bit section numbers. The index sections will be
12016 aligned at 8-byte boundaries in the file.
12017
d2415c6c
DE
12018 The index section header consists of:
12019
12020 V, 32 bit version number
12021 -, 32 bits unused
12022 N, 32 bit number of compilation units or type units in the index
12023 M, 32 bit number of slots in the hash table
80626a55 12024
d2415c6c 12025 Numbers are recorded using the byte order of the application binary.
80626a55 12026
d2415c6c
DE
12027 The hash table begins at offset 16 in the section, and consists of an array
12028 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12029 order of the application binary). Unused slots in the hash table are 0.
12030 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12031
d2415c6c
DE
12032 The parallel table begins immediately after the hash table
12033 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12034 array of 32-bit indexes (using the byte order of the application binary),
12035 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12036 table contains a 32-bit index into the pool of section numbers. For unused
12037 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12038
73869dc2
DE
12039 The pool of section numbers begins immediately following the hash table
12040 (at offset 16 + 12 * M from the beginning of the section). The pool of
12041 section numbers consists of an array of 32-bit words (using the byte order
12042 of the application binary). Each item in the array is indexed starting
12043 from 0. The hash table entry provides the index of the first section
12044 number in the set. Additional section numbers in the set follow, and the
12045 set is terminated by a 0 entry (section number 0 is not used in ELF).
12046
12047 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12048 section must be the first entry in the set, and the .debug_abbrev.dwo must
12049 be the second entry. Other members of the set may follow in any order.
12050
12051 ---
12052
12053 DWP Version 2:
12054
12055 DWP Version 2 combines all the .debug_info, etc. sections into one,
12056 and the entries in the index tables are now offsets into these sections.
12057 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12058 section.
12059
12060 Index Section Contents:
12061 Header
12062 Hash Table of Signatures dwp_hash_table.hash_table
12063 Parallel Table of Indices dwp_hash_table.unit_table
12064 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12065 Table of Section Sizes dwp_hash_table.v2.sizes
12066
12067 The index section header consists of:
12068
12069 V, 32 bit version number
12070 L, 32 bit number of columns in the table of section offsets
12071 N, 32 bit number of compilation units or type units in the index
12072 M, 32 bit number of slots in the hash table
12073
12074 Numbers are recorded using the byte order of the application binary.
12075
12076 The hash table has the same format as version 1.
12077 The parallel table of indices has the same format as version 1,
12078 except that the entries are origin-1 indices into the table of sections
12079 offsets and the table of section sizes.
12080
12081 The table of offsets begins immediately following the parallel table
12082 (at offset 16 + 12 * M from the beginning of the section). The table is
12083 a two-dimensional array of 32-bit words (using the byte order of the
12084 application binary), with L columns and N+1 rows, in row-major order.
12085 Each row in the array is indexed starting from 0. The first row provides
12086 a key to the remaining rows: each column in this row provides an identifier
12087 for a debug section, and the offsets in the same column of subsequent rows
12088 refer to that section. The section identifiers are:
12089
12090 DW_SECT_INFO 1 .debug_info.dwo
12091 DW_SECT_TYPES 2 .debug_types.dwo
12092 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12093 DW_SECT_LINE 4 .debug_line.dwo
12094 DW_SECT_LOC 5 .debug_loc.dwo
12095 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12096 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12097 DW_SECT_MACRO 8 .debug_macro.dwo
12098
12099 The offsets provided by the CU and TU index sections are the base offsets
12100 for the contributions made by each CU or TU to the corresponding section
12101 in the package file. Each CU and TU header contains an abbrev_offset
12102 field, used to find the abbreviations table for that CU or TU within the
12103 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12104 be interpreted as relative to the base offset given in the index section.
12105 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12106 should be interpreted as relative to the base offset for .debug_line.dwo,
12107 and offsets into other debug sections obtained from DWARF attributes should
12108 also be interpreted as relative to the corresponding base offset.
12109
12110 The table of sizes begins immediately following the table of offsets.
12111 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12112 with L columns and N rows, in row-major order. Each row in the array is
12113 indexed starting from 1 (row 0 is shared by the two tables).
12114
12115 ---
12116
12117 Hash table lookup is handled the same in version 1 and 2:
12118
12119 We assume that N and M will not exceed 2^32 - 1.
12120 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12121
d2415c6c
DE
12122 Given a 64-bit compilation unit signature or a type signature S, an entry
12123 in the hash table is located as follows:
80626a55 12124
d2415c6c
DE
12125 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12126 the low-order k bits all set to 1.
80626a55 12127
d2415c6c 12128 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12129
d2415c6c
DE
12130 3) If the hash table entry at index H matches the signature, use that
12131 entry. If the hash table entry at index H is unused (all zeroes),
12132 terminate the search: the signature is not present in the table.
80626a55 12133
d2415c6c 12134 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12135
d2415c6c 12136 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12137 to stop at an unused slot or find the match. */
80626a55
DE
12138
12139/* Create a hash table to map DWO IDs to their CU/TU entry in
12140 .debug_{info,types}.dwo in DWP_FILE.
12141 Returns NULL if there isn't one.
12142 Note: This function processes DWP files only, not DWO files. */
12143
12144static struct dwp_hash_table *
ed2dc618
SM
12145create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12146 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12147{
12148 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12149 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12150 const gdb_byte *index_ptr, *index_end;
80626a55 12151 struct dwarf2_section_info *index;
73869dc2 12152 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12153 struct dwp_hash_table *htab;
12154
12155 if (is_debug_types)
12156 index = &dwp_file->sections.tu_index;
12157 else
12158 index = &dwp_file->sections.cu_index;
12159
12160 if (dwarf2_section_empty_p (index))
12161 return NULL;
12162 dwarf2_read_section (objfile, index);
12163
12164 index_ptr = index->buffer;
12165 index_end = index_ptr + index->size;
12166
12167 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12168 index_ptr += 4;
12169 if (version == 2)
12170 nr_columns = read_4_bytes (dbfd, index_ptr);
12171 else
12172 nr_columns = 0;
12173 index_ptr += 4;
80626a55
DE
12174 nr_units = read_4_bytes (dbfd, index_ptr);
12175 index_ptr += 4;
12176 nr_slots = read_4_bytes (dbfd, index_ptr);
12177 index_ptr += 4;
12178
73869dc2 12179 if (version != 1 && version != 2)
80626a55 12180 {
21aa081e 12181 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12182 " [in module %s]"),
21aa081e 12183 pulongest (version), dwp_file->name);
80626a55
DE
12184 }
12185 if (nr_slots != (nr_slots & -nr_slots))
12186 {
21aa081e 12187 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12188 " is not power of 2 [in module %s]"),
21aa081e 12189 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12190 }
12191
12192 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12193 htab->version = version;
12194 htab->nr_columns = nr_columns;
80626a55
DE
12195 htab->nr_units = nr_units;
12196 htab->nr_slots = nr_slots;
12197 htab->hash_table = index_ptr;
12198 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12199
12200 /* Exit early if the table is empty. */
12201 if (nr_slots == 0 || nr_units == 0
12202 || (version == 2 && nr_columns == 0))
12203 {
12204 /* All must be zero. */
12205 if (nr_slots != 0 || nr_units != 0
12206 || (version == 2 && nr_columns != 0))
12207 {
b98664d3 12208 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12209 " all zero [in modules %s]"),
12210 dwp_file->name);
12211 }
12212 return htab;
12213 }
12214
12215 if (version == 1)
12216 {
12217 htab->section_pool.v1.indices =
12218 htab->unit_table + sizeof (uint32_t) * nr_slots;
12219 /* It's harder to decide whether the section is too small in v1.
12220 V1 is deprecated anyway so we punt. */
12221 }
12222 else
12223 {
12224 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12225 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12226 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12227 /* Reverse map for error checking. */
12228 int ids_seen[DW_SECT_MAX + 1];
12229 int i;
12230
12231 if (nr_columns < 2)
12232 {
12233 error (_("Dwarf Error: bad DWP hash table, too few columns"
12234 " in section table [in module %s]"),
12235 dwp_file->name);
12236 }
12237 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12238 {
12239 error (_("Dwarf Error: bad DWP hash table, too many columns"
12240 " in section table [in module %s]"),
12241 dwp_file->name);
12242 }
04fd5eed
GB
12243 memset (ids, 255, sizeof_ids);
12244 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12245 for (i = 0; i < nr_columns; ++i)
12246 {
12247 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12248
12249 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12250 {
12251 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12252 " in section table [in module %s]"),
12253 id, dwp_file->name);
12254 }
12255 if (ids_seen[id] != -1)
12256 {
12257 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12258 " id %d in section table [in module %s]"),
12259 id, dwp_file->name);
12260 }
12261 ids_seen[id] = i;
12262 ids[i] = id;
12263 }
12264 /* Must have exactly one info or types section. */
12265 if (((ids_seen[DW_SECT_INFO] != -1)
12266 + (ids_seen[DW_SECT_TYPES] != -1))
12267 != 1)
12268 {
12269 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12270 " DWO info/types section [in module %s]"),
12271 dwp_file->name);
12272 }
12273 /* Must have an abbrev section. */
12274 if (ids_seen[DW_SECT_ABBREV] == -1)
12275 {
12276 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12277 " section [in module %s]"),
12278 dwp_file->name);
12279 }
12280 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12281 htab->section_pool.v2.sizes =
12282 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12283 * nr_units * nr_columns);
12284 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12285 * nr_units * nr_columns))
12286 > index_end)
12287 {
12288 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12289 " [in module %s]"),
12290 dwp_file->name);
12291 }
12292 }
80626a55
DE
12293
12294 return htab;
12295}
12296
12297/* Update SECTIONS with the data from SECTP.
12298
12299 This function is like the other "locate" section routines that are
12300 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12301 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12302
12303 The result is non-zero for success, or zero if an error was found. */
12304
12305static int
73869dc2
DE
12306locate_v1_virtual_dwo_sections (asection *sectp,
12307 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12308{
12309 const struct dwop_section_names *names = &dwop_section_names;
12310
12311 if (section_is_p (sectp->name, &names->abbrev_dwo))
12312 {
12313 /* There can be only one. */
049412e3 12314 if (sections->abbrev.s.section != NULL)
80626a55 12315 return 0;
049412e3 12316 sections->abbrev.s.section = sectp;
fd361982 12317 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12318 }
12319 else if (section_is_p (sectp->name, &names->info_dwo)
12320 || section_is_p (sectp->name, &names->types_dwo))
12321 {
12322 /* There can be only one. */
049412e3 12323 if (sections->info_or_types.s.section != NULL)
80626a55 12324 return 0;
049412e3 12325 sections->info_or_types.s.section = sectp;
fd361982 12326 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12327 }
12328 else if (section_is_p (sectp->name, &names->line_dwo))
12329 {
12330 /* There can be only one. */
049412e3 12331 if (sections->line.s.section != NULL)
80626a55 12332 return 0;
049412e3 12333 sections->line.s.section = sectp;
fd361982 12334 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12335 }
12336 else if (section_is_p (sectp->name, &names->loc_dwo))
12337 {
12338 /* There can be only one. */
049412e3 12339 if (sections->loc.s.section != NULL)
80626a55 12340 return 0;
049412e3 12341 sections->loc.s.section = sectp;
fd361982 12342 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12343 }
12344 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12345 {
12346 /* There can be only one. */
049412e3 12347 if (sections->macinfo.s.section != NULL)
80626a55 12348 return 0;
049412e3 12349 sections->macinfo.s.section = sectp;
fd361982 12350 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12351 }
12352 else if (section_is_p (sectp->name, &names->macro_dwo))
12353 {
12354 /* There can be only one. */
049412e3 12355 if (sections->macro.s.section != NULL)
80626a55 12356 return 0;
049412e3 12357 sections->macro.s.section = sectp;
fd361982 12358 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12359 }
12360 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12361 {
12362 /* There can be only one. */
049412e3 12363 if (sections->str_offsets.s.section != NULL)
80626a55 12364 return 0;
049412e3 12365 sections->str_offsets.s.section = sectp;
fd361982 12366 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12367 }
12368 else
12369 {
12370 /* No other kind of section is valid. */
12371 return 0;
12372 }
12373
12374 return 1;
12375}
12376
73869dc2
DE
12377/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12378 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12379 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12380 This is for DWP version 1 files. */
80626a55
DE
12381
12382static struct dwo_unit *
ed2dc618
SM
12383create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12384 struct dwp_file *dwp_file,
73869dc2
DE
12385 uint32_t unit_index,
12386 const char *comp_dir,
12387 ULONGEST signature, int is_debug_types)
80626a55
DE
12388{
12389 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12390 const struct dwp_hash_table *dwp_htab =
12391 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12392 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12393 const char *kind = is_debug_types ? "TU" : "CU";
12394 struct dwo_file *dwo_file;
12395 struct dwo_unit *dwo_unit;
73869dc2 12396 struct virtual_v1_dwo_sections sections;
80626a55 12397 void **dwo_file_slot;
80626a55
DE
12398 int i;
12399
73869dc2
DE
12400 gdb_assert (dwp_file->version == 1);
12401
b4f54984 12402 if (dwarf_read_debug)
80626a55 12403 {
73869dc2 12404 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12405 kind,
73869dc2 12406 pulongest (unit_index), hex_string (signature),
80626a55
DE
12407 dwp_file->name);
12408 }
12409
19ac8c2e 12410 /* Fetch the sections of this DWO unit.
80626a55
DE
12411 Put a limit on the number of sections we look for so that bad data
12412 doesn't cause us to loop forever. */
12413
73869dc2 12414#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12415 (1 /* .debug_info or .debug_types */ \
12416 + 1 /* .debug_abbrev */ \
12417 + 1 /* .debug_line */ \
12418 + 1 /* .debug_loc */ \
12419 + 1 /* .debug_str_offsets */ \
19ac8c2e 12420 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12421 + 1 /* trailing zero */)
12422
12423 memset (&sections, 0, sizeof (sections));
80626a55 12424
73869dc2 12425 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12426 {
12427 asection *sectp;
12428 uint32_t section_nr =
12429 read_4_bytes (dbfd,
73869dc2
DE
12430 dwp_htab->section_pool.v1.indices
12431 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12432
12433 if (section_nr == 0)
12434 break;
12435 if (section_nr >= dwp_file->num_sections)
12436 {
12437 error (_("Dwarf Error: bad DWP hash table, section number too large"
12438 " [in module %s]"),
12439 dwp_file->name);
12440 }
12441
12442 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12443 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12444 {
12445 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12446 " [in module %s]"),
12447 dwp_file->name);
12448 }
12449 }
12450
12451 if (i < 2
a32a8923
DE
12452 || dwarf2_section_empty_p (&sections.info_or_types)
12453 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12454 {
12455 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12456 " [in module %s]"),
12457 dwp_file->name);
12458 }
73869dc2 12459 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12460 {
12461 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12462 " [in module %s]"),
12463 dwp_file->name);
12464 }
12465
12466 /* It's easier for the rest of the code if we fake a struct dwo_file and
12467 have dwo_unit "live" in that. At least for now.
12468
12469 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12470 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12471 file, we can combine them back into a virtual DWO file to save space
12472 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12473 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12474
791afaa2
TT
12475 std::string virtual_dwo_name =
12476 string_printf ("virtual-dwo/%d-%d-%d-%d",
12477 get_section_id (&sections.abbrev),
12478 get_section_id (&sections.line),
12479 get_section_id (&sections.loc),
12480 get_section_id (&sections.str_offsets));
80626a55 12481 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12482 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12483 virtual_dwo_name.c_str (),
12484 comp_dir);
80626a55
DE
12485 /* Create one if necessary. */
12486 if (*dwo_file_slot == NULL)
12487 {
b4f54984 12488 if (dwarf_read_debug)
80626a55
DE
12489 {
12490 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12491 virtual_dwo_name.c_str ());
80626a55 12492 }
51ac9db5 12493 dwo_file = new struct dwo_file;
efba19b0
TT
12494 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12495 virtual_dwo_name);
0ac5b59e 12496 dwo_file->comp_dir = comp_dir;
80626a55
DE
12497 dwo_file->sections.abbrev = sections.abbrev;
12498 dwo_file->sections.line = sections.line;
12499 dwo_file->sections.loc = sections.loc;
12500 dwo_file->sections.macinfo = sections.macinfo;
12501 dwo_file->sections.macro = sections.macro;
12502 dwo_file->sections.str_offsets = sections.str_offsets;
12503 /* The "str" section is global to the entire DWP file. */
12504 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12505 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12506 there's no need to record it in dwo_file.
12507 Also, we can't simply record type sections in dwo_file because
12508 we record a pointer into the vector in dwo_unit. As we collect more
12509 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12510 for it, invalidating all copies of pointers into the previous
12511 contents. */
80626a55
DE
12512 *dwo_file_slot = dwo_file;
12513 }
12514 else
12515 {
b4f54984 12516 if (dwarf_read_debug)
80626a55
DE
12517 {
12518 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12519 virtual_dwo_name.c_str ());
80626a55 12520 }
9a3c8263 12521 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12522 }
80626a55
DE
12523
12524 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12525 dwo_unit->dwo_file = dwo_file;
12526 dwo_unit->signature = signature;
8d749320
SM
12527 dwo_unit->section =
12528 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12529 *dwo_unit->section = sections.info_or_types;
57d63ce2 12530 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12531
12532 return dwo_unit;
12533}
12534
73869dc2
DE
12535/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12536 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12537 piece within that section used by a TU/CU, return a virtual section
12538 of just that piece. */
12539
12540static struct dwarf2_section_info
ed2dc618
SM
12541create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12542 struct dwarf2_section_info *section,
73869dc2
DE
12543 bfd_size_type offset, bfd_size_type size)
12544{
12545 struct dwarf2_section_info result;
12546 asection *sectp;
12547
12548 gdb_assert (section != NULL);
12549 gdb_assert (!section->is_virtual);
12550
12551 memset (&result, 0, sizeof (result));
12552 result.s.containing_section = section;
dc4ccb6f 12553 result.is_virtual = true;
73869dc2
DE
12554
12555 if (size == 0)
12556 return result;
12557
12558 sectp = get_section_bfd_section (section);
12559
12560 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12561 bounds of the real section. This is a pretty-rare event, so just
12562 flag an error (easier) instead of a warning and trying to cope. */
12563 if (sectp == NULL
fd361982 12564 || offset + size > bfd_section_size (sectp))
73869dc2 12565 {
73869dc2
DE
12566 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12567 " in section %s [in module %s]"),
fd361982 12568 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12569 objfile_name (dwarf2_per_objfile->objfile));
12570 }
12571
12572 result.virtual_offset = offset;
12573 result.size = size;
12574 return result;
12575}
12576
12577/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12578 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12579 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12580 This is for DWP version 2 files. */
12581
12582static struct dwo_unit *
ed2dc618
SM
12583create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12584 struct dwp_file *dwp_file,
73869dc2
DE
12585 uint32_t unit_index,
12586 const char *comp_dir,
12587 ULONGEST signature, int is_debug_types)
12588{
12589 struct objfile *objfile = dwarf2_per_objfile->objfile;
12590 const struct dwp_hash_table *dwp_htab =
12591 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12592 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12593 const char *kind = is_debug_types ? "TU" : "CU";
12594 struct dwo_file *dwo_file;
12595 struct dwo_unit *dwo_unit;
12596 struct virtual_v2_dwo_sections sections;
12597 void **dwo_file_slot;
73869dc2
DE
12598 int i;
12599
12600 gdb_assert (dwp_file->version == 2);
12601
b4f54984 12602 if (dwarf_read_debug)
73869dc2
DE
12603 {
12604 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12605 kind,
12606 pulongest (unit_index), hex_string (signature),
12607 dwp_file->name);
12608 }
12609
12610 /* Fetch the section offsets of this DWO unit. */
12611
12612 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12613
12614 for (i = 0; i < dwp_htab->nr_columns; ++i)
12615 {
12616 uint32_t offset = read_4_bytes (dbfd,
12617 dwp_htab->section_pool.v2.offsets
12618 + (((unit_index - 1) * dwp_htab->nr_columns
12619 + i)
12620 * sizeof (uint32_t)));
12621 uint32_t size = read_4_bytes (dbfd,
12622 dwp_htab->section_pool.v2.sizes
12623 + (((unit_index - 1) * dwp_htab->nr_columns
12624 + i)
12625 * sizeof (uint32_t)));
12626
12627 switch (dwp_htab->section_pool.v2.section_ids[i])
12628 {
12629 case DW_SECT_INFO:
12630 case DW_SECT_TYPES:
12631 sections.info_or_types_offset = offset;
12632 sections.info_or_types_size = size;
12633 break;
12634 case DW_SECT_ABBREV:
12635 sections.abbrev_offset = offset;
12636 sections.abbrev_size = size;
12637 break;
12638 case DW_SECT_LINE:
12639 sections.line_offset = offset;
12640 sections.line_size = size;
12641 break;
12642 case DW_SECT_LOC:
12643 sections.loc_offset = offset;
12644 sections.loc_size = size;
12645 break;
12646 case DW_SECT_STR_OFFSETS:
12647 sections.str_offsets_offset = offset;
12648 sections.str_offsets_size = size;
12649 break;
12650 case DW_SECT_MACINFO:
12651 sections.macinfo_offset = offset;
12652 sections.macinfo_size = size;
12653 break;
12654 case DW_SECT_MACRO:
12655 sections.macro_offset = offset;
12656 sections.macro_size = size;
12657 break;
12658 }
12659 }
12660
12661 /* It's easier for the rest of the code if we fake a struct dwo_file and
12662 have dwo_unit "live" in that. At least for now.
12663
12664 The DWP file can be made up of a random collection of CUs and TUs.
12665 However, for each CU + set of TUs that came from the same original DWO
12666 file, we can combine them back into a virtual DWO file to save space
12667 (fewer struct dwo_file objects to allocate). Remember that for really
12668 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12669
791afaa2
TT
12670 std::string virtual_dwo_name =
12671 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12672 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12673 (long) (sections.line_size ? sections.line_offset : 0),
12674 (long) (sections.loc_size ? sections.loc_offset : 0),
12675 (long) (sections.str_offsets_size
12676 ? sections.str_offsets_offset : 0));
73869dc2 12677 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12678 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12679 virtual_dwo_name.c_str (),
12680 comp_dir);
73869dc2
DE
12681 /* Create one if necessary. */
12682 if (*dwo_file_slot == NULL)
12683 {
b4f54984 12684 if (dwarf_read_debug)
73869dc2
DE
12685 {
12686 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12687 virtual_dwo_name.c_str ());
73869dc2 12688 }
51ac9db5 12689 dwo_file = new struct dwo_file;
efba19b0
TT
12690 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12691 virtual_dwo_name);
73869dc2
DE
12692 dwo_file->comp_dir = comp_dir;
12693 dwo_file->sections.abbrev =
ed2dc618 12694 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12695 sections.abbrev_offset, sections.abbrev_size);
12696 dwo_file->sections.line =
ed2dc618 12697 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12698 sections.line_offset, sections.line_size);
12699 dwo_file->sections.loc =
ed2dc618 12700 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12701 sections.loc_offset, sections.loc_size);
12702 dwo_file->sections.macinfo =
ed2dc618 12703 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12704 sections.macinfo_offset, sections.macinfo_size);
12705 dwo_file->sections.macro =
ed2dc618 12706 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12707 sections.macro_offset, sections.macro_size);
12708 dwo_file->sections.str_offsets =
ed2dc618
SM
12709 create_dwp_v2_section (dwarf2_per_objfile,
12710 &dwp_file->sections.str_offsets,
73869dc2
DE
12711 sections.str_offsets_offset,
12712 sections.str_offsets_size);
12713 /* The "str" section is global to the entire DWP file. */
12714 dwo_file->sections.str = dwp_file->sections.str;
12715 /* The info or types section is assigned below to dwo_unit,
12716 there's no need to record it in dwo_file.
12717 Also, we can't simply record type sections in dwo_file because
12718 we record a pointer into the vector in dwo_unit. As we collect more
12719 types we'll grow the vector and eventually have to reallocate space
12720 for it, invalidating all copies of pointers into the previous
12721 contents. */
12722 *dwo_file_slot = dwo_file;
12723 }
12724 else
12725 {
b4f54984 12726 if (dwarf_read_debug)
73869dc2
DE
12727 {
12728 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12729 virtual_dwo_name.c_str ());
73869dc2 12730 }
9a3c8263 12731 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12732 }
73869dc2
DE
12733
12734 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12735 dwo_unit->dwo_file = dwo_file;
12736 dwo_unit->signature = signature;
8d749320
SM
12737 dwo_unit->section =
12738 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12739 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12740 is_debug_types
73869dc2
DE
12741 ? &dwp_file->sections.types
12742 : &dwp_file->sections.info,
12743 sections.info_or_types_offset,
12744 sections.info_or_types_size);
12745 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12746
12747 return dwo_unit;
12748}
12749
57d63ce2
DE
12750/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12751 Returns NULL if the signature isn't found. */
80626a55
DE
12752
12753static struct dwo_unit *
ed2dc618
SM
12754lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12755 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12756 ULONGEST signature, int is_debug_types)
80626a55 12757{
57d63ce2
DE
12758 const struct dwp_hash_table *dwp_htab =
12759 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12760 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12761 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12762 uint32_t hash = signature & mask;
12763 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12764 unsigned int i;
12765 void **slot;
870f88f7 12766 struct dwo_unit find_dwo_cu;
80626a55
DE
12767
12768 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12769 find_dwo_cu.signature = signature;
19ac8c2e
DE
12770 slot = htab_find_slot (is_debug_types
12771 ? dwp_file->loaded_tus
12772 : dwp_file->loaded_cus,
12773 &find_dwo_cu, INSERT);
80626a55
DE
12774
12775 if (*slot != NULL)
9a3c8263 12776 return (struct dwo_unit *) *slot;
80626a55
DE
12777
12778 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12779 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12780 {
12781 ULONGEST signature_in_table;
12782
12783 signature_in_table =
57d63ce2 12784 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12785 if (signature_in_table == signature)
12786 {
57d63ce2
DE
12787 uint32_t unit_index =
12788 read_4_bytes (dbfd,
12789 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12790
73869dc2
DE
12791 if (dwp_file->version == 1)
12792 {
ed2dc618
SM
12793 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12794 dwp_file, unit_index,
73869dc2
DE
12795 comp_dir, signature,
12796 is_debug_types);
12797 }
12798 else
12799 {
ed2dc618
SM
12800 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12801 dwp_file, unit_index,
73869dc2
DE
12802 comp_dir, signature,
12803 is_debug_types);
12804 }
9a3c8263 12805 return (struct dwo_unit *) *slot;
80626a55
DE
12806 }
12807 if (signature_in_table == 0)
12808 return NULL;
12809 hash = (hash + hash2) & mask;
12810 }
12811
12812 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12813 " [in module %s]"),
12814 dwp_file->name);
12815}
12816
ab5088bf 12817/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12818 Open the file specified by FILE_NAME and hand it off to BFD for
12819 preliminary analysis. Return a newly initialized bfd *, which
12820 includes a canonicalized copy of FILE_NAME.
80626a55 12821 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12822 SEARCH_CWD is true if the current directory is to be searched.
12823 It will be searched before debug-file-directory.
13aaf454
DE
12824 If successful, the file is added to the bfd include table of the
12825 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12826 If unable to find/open the file, return NULL.
3019eac3
DE
12827 NOTE: This function is derived from symfile_bfd_open. */
12828
192b62ce 12829static gdb_bfd_ref_ptr
ed2dc618
SM
12830try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12831 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12832{
24b9144d 12833 int desc;
9c02c129
DE
12834 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12835 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12836 to debug_file_directory. */
e0cc99a6 12837 const char *search_path;
9c02c129
DE
12838 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12839
e0cc99a6 12840 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12841 if (search_cwd)
12842 {
12843 if (*debug_file_directory != '\0')
e0cc99a6
TT
12844 {
12845 search_path_holder.reset (concat (".", dirname_separator_string,
12846 debug_file_directory,
12847 (char *) NULL));
12848 search_path = search_path_holder.get ();
12849 }
6ac97d4c 12850 else
e0cc99a6 12851 search_path = ".";
6ac97d4c 12852 }
9c02c129 12853 else
e0cc99a6 12854 search_path = debug_file_directory;
3019eac3 12855
24b9144d 12856 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12857 if (is_dwp)
12858 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12859
12860 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12861 desc = openp (search_path, flags, file_name,
3019eac3
DE
12862 O_RDONLY | O_BINARY, &absolute_name);
12863 if (desc < 0)
12864 return NULL;
12865
e0cc99a6
TT
12866 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12867 gnutarget, desc));
9c02c129
DE
12868 if (sym_bfd == NULL)
12869 return NULL;
192b62ce 12870 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12871
192b62ce
TT
12872 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12873 return NULL;
3019eac3 12874
13aaf454
DE
12875 /* Success. Record the bfd as having been included by the objfile's bfd.
12876 This is important because things like demangled_names_hash lives in the
12877 objfile's per_bfd space and may have references to things like symbol
12878 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12879 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12880
3019eac3
DE
12881 return sym_bfd;
12882}
12883
ab5088bf 12884/* Try to open DWO file FILE_NAME.
3019eac3
DE
12885 COMP_DIR is the DW_AT_comp_dir attribute.
12886 The result is the bfd handle of the file.
12887 If there is a problem finding or opening the file, return NULL.
12888 Upon success, the canonicalized path of the file is stored in the bfd,
12889 same as symfile_bfd_open. */
12890
192b62ce 12891static gdb_bfd_ref_ptr
ed2dc618
SM
12892open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12893 const char *file_name, const char *comp_dir)
3019eac3 12894{
80626a55 12895 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12896 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12897 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12898
12899 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12900
12901 if (comp_dir != NULL)
12902 {
b36cec19
PA
12903 char *path_to_try = concat (comp_dir, SLASH_STRING,
12904 file_name, (char *) NULL);
3019eac3
DE
12905
12906 /* NOTE: If comp_dir is a relative path, this will also try the
12907 search path, which seems useful. */
ed2dc618
SM
12908 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12909 path_to_try,
12910 0 /*is_dwp*/,
192b62ce 12911 1 /*search_cwd*/));
3019eac3
DE
12912 xfree (path_to_try);
12913 if (abfd != NULL)
12914 return abfd;
12915 }
12916
12917 /* That didn't work, try debug-file-directory, which, despite its name,
12918 is a list of paths. */
12919
12920 if (*debug_file_directory == '\0')
12921 return NULL;
12922
ed2dc618
SM
12923 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12924 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12925}
12926
80626a55
DE
12927/* This function is mapped across the sections and remembers the offset and
12928 size of each of the DWO debugging sections we are interested in. */
12929
12930static void
12931dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12932{
9a3c8263 12933 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12934 const struct dwop_section_names *names = &dwop_section_names;
12935
12936 if (section_is_p (sectp->name, &names->abbrev_dwo))
12937 {
049412e3 12938 dwo_sections->abbrev.s.section = sectp;
fd361982 12939 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12940 }
12941 else if (section_is_p (sectp->name, &names->info_dwo))
12942 {
049412e3 12943 dwo_sections->info.s.section = sectp;
fd361982 12944 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12945 }
12946 else if (section_is_p (sectp->name, &names->line_dwo))
12947 {
049412e3 12948 dwo_sections->line.s.section = sectp;
fd361982 12949 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12950 }
12951 else if (section_is_p (sectp->name, &names->loc_dwo))
12952 {
049412e3 12953 dwo_sections->loc.s.section = sectp;
fd361982 12954 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12955 }
12956 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12957 {
049412e3 12958 dwo_sections->macinfo.s.section = sectp;
fd361982 12959 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12960 }
12961 else if (section_is_p (sectp->name, &names->macro_dwo))
12962 {
049412e3 12963 dwo_sections->macro.s.section = sectp;
fd361982 12964 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12965 }
12966 else if (section_is_p (sectp->name, &names->str_dwo))
12967 {
049412e3 12968 dwo_sections->str.s.section = sectp;
fd361982 12969 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12970 }
12971 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12972 {
049412e3 12973 dwo_sections->str_offsets.s.section = sectp;
fd361982 12974 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12975 }
12976 else if (section_is_p (sectp->name, &names->types_dwo))
12977 {
12978 struct dwarf2_section_info type_section;
12979
12980 memset (&type_section, 0, sizeof (type_section));
049412e3 12981 type_section.s.section = sectp;
fd361982 12982 type_section.size = bfd_section_size (sectp);
fd5866f6 12983 dwo_sections->types.push_back (type_section);
80626a55
DE
12984 }
12985}
12986
ab5088bf 12987/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12988 by PER_CU. This is for the non-DWP case.
80626a55 12989 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12990
12991static struct dwo_file *
0ac5b59e
DE
12992open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12993 const char *dwo_name, const char *comp_dir)
3019eac3 12994{
ed2dc618 12995 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12996
fb1eb2f9 12997 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12998 if (dbfd == NULL)
12999 {
b4f54984 13000 if (dwarf_read_debug)
80626a55
DE
13001 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13002 return NULL;
13003 }
263db9a1 13004
51ac9db5 13005 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13006 dwo_file->dwo_name = dwo_name;
13007 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13008 dwo_file->dbfd = std::move (dbfd);
3019eac3 13009
fb1eb2f9 13010 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13011 &dwo_file->sections);
3019eac3 13012
ed2dc618
SM
13013 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13014 dwo_file->cus);
3019eac3 13015
263db9a1 13016 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13017 dwo_file->sections.types, dwo_file->tus);
3019eac3 13018
b4f54984 13019 if (dwarf_read_debug)
80626a55
DE
13020 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13021
263db9a1 13022 return dwo_file.release ();
3019eac3
DE
13023}
13024
80626a55 13025/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13026 size of each of the DWP debugging sections common to version 1 and 2 that
13027 we are interested in. */
3019eac3 13028
80626a55 13029static void
73869dc2
DE
13030dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13031 void *dwp_file_ptr)
3019eac3 13032{
9a3c8263 13033 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13034 const struct dwop_section_names *names = &dwop_section_names;
13035 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13036
80626a55 13037 /* Record the ELF section number for later lookup: this is what the
73869dc2 13038 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13039 gdb_assert (elf_section_nr < dwp_file->num_sections);
13040 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13041
80626a55
DE
13042 /* Look for specific sections that we need. */
13043 if (section_is_p (sectp->name, &names->str_dwo))
13044 {
049412e3 13045 dwp_file->sections.str.s.section = sectp;
fd361982 13046 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13047 }
13048 else if (section_is_p (sectp->name, &names->cu_index))
13049 {
049412e3 13050 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13051 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13052 }
13053 else if (section_is_p (sectp->name, &names->tu_index))
13054 {
049412e3 13055 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13056 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13057 }
13058}
3019eac3 13059
73869dc2
DE
13060/* This function is mapped across the sections and remembers the offset and
13061 size of each of the DWP version 2 debugging sections that we are interested
13062 in. This is split into a separate function because we don't know if we
13063 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13064
13065static void
13066dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13067{
9a3c8263 13068 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13069 const struct dwop_section_names *names = &dwop_section_names;
13070 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13071
13072 /* Record the ELF section number for later lookup: this is what the
13073 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13074 gdb_assert (elf_section_nr < dwp_file->num_sections);
13075 dwp_file->elf_sections[elf_section_nr] = sectp;
13076
13077 /* Look for specific sections that we need. */
13078 if (section_is_p (sectp->name, &names->abbrev_dwo))
13079 {
049412e3 13080 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13081 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13082 }
13083 else if (section_is_p (sectp->name, &names->info_dwo))
13084 {
049412e3 13085 dwp_file->sections.info.s.section = sectp;
fd361982 13086 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13087 }
13088 else if (section_is_p (sectp->name, &names->line_dwo))
13089 {
049412e3 13090 dwp_file->sections.line.s.section = sectp;
fd361982 13091 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13092 }
13093 else if (section_is_p (sectp->name, &names->loc_dwo))
13094 {
049412e3 13095 dwp_file->sections.loc.s.section = sectp;
fd361982 13096 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13097 }
13098 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13099 {
049412e3 13100 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13101 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13102 }
13103 else if (section_is_p (sectp->name, &names->macro_dwo))
13104 {
049412e3 13105 dwp_file->sections.macro.s.section = sectp;
fd361982 13106 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13107 }
13108 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13109 {
049412e3 13110 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13111 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13112 }
13113 else if (section_is_p (sectp->name, &names->types_dwo))
13114 {
049412e3 13115 dwp_file->sections.types.s.section = sectp;
fd361982 13116 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13117 }
13118}
13119
80626a55 13120/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13121
80626a55
DE
13122static hashval_t
13123hash_dwp_loaded_cutus (const void *item)
13124{
9a3c8263 13125 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13126
80626a55
DE
13127 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13128 return dwo_unit->signature;
3019eac3
DE
13129}
13130
80626a55 13131/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13132
80626a55
DE
13133static int
13134eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13135{
9a3c8263
SM
13136 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13137 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13138
80626a55
DE
13139 return dua->signature == dub->signature;
13140}
3019eac3 13141
80626a55 13142/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13143
80626a55
DE
13144static htab_t
13145allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13146{
13147 return htab_create_alloc_ex (3,
13148 hash_dwp_loaded_cutus,
13149 eq_dwp_loaded_cutus,
13150 NULL,
13151 &objfile->objfile_obstack,
13152 hashtab_obstack_allocate,
13153 dummy_obstack_deallocate);
13154}
3019eac3 13155
ab5088bf
DE
13156/* Try to open DWP file FILE_NAME.
13157 The result is the bfd handle of the file.
13158 If there is a problem finding or opening the file, return NULL.
13159 Upon success, the canonicalized path of the file is stored in the bfd,
13160 same as symfile_bfd_open. */
13161
192b62ce 13162static gdb_bfd_ref_ptr
ed2dc618
SM
13163open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13164 const char *file_name)
ab5088bf 13165{
ed2dc618
SM
13166 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13167 1 /*is_dwp*/,
192b62ce 13168 1 /*search_cwd*/));
6ac97d4c
DE
13169 if (abfd != NULL)
13170 return abfd;
13171
13172 /* Work around upstream bug 15652.
13173 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13174 [Whether that's a "bug" is debatable, but it is getting in our way.]
13175 We have no real idea where the dwp file is, because gdb's realpath-ing
13176 of the executable's path may have discarded the needed info.
13177 [IWBN if the dwp file name was recorded in the executable, akin to
13178 .gnu_debuglink, but that doesn't exist yet.]
13179 Strip the directory from FILE_NAME and search again. */
13180 if (*debug_file_directory != '\0')
13181 {
13182 /* Don't implicitly search the current directory here.
13183 If the user wants to search "." to handle this case,
13184 it must be added to debug-file-directory. */
ed2dc618
SM
13185 return try_open_dwop_file (dwarf2_per_objfile,
13186 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13187 0 /*search_cwd*/);
13188 }
13189
13190 return NULL;
ab5088bf
DE
13191}
13192
80626a55
DE
13193/* Initialize the use of the DWP file for the current objfile.
13194 By convention the name of the DWP file is ${objfile}.dwp.
13195 The result is NULL if it can't be found. */
a766d390 13196
400174b1 13197static std::unique_ptr<struct dwp_file>
ed2dc618 13198open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13199{
13200 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13201
82bf32bc
JK
13202 /* Try to find first .dwp for the binary file before any symbolic links
13203 resolving. */
6c447423
DE
13204
13205 /* If the objfile is a debug file, find the name of the real binary
13206 file and get the name of dwp file from there. */
d721ba37 13207 std::string dwp_name;
6c447423
DE
13208 if (objfile->separate_debug_objfile_backlink != NULL)
13209 {
13210 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13211 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13212
d721ba37 13213 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13214 }
13215 else
d721ba37
PA
13216 dwp_name = objfile->original_name;
13217
13218 dwp_name += ".dwp";
80626a55 13219
ed2dc618 13220 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13221 if (dbfd == NULL
13222 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13223 {
13224 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13225 dwp_name = objfile_name (objfile);
13226 dwp_name += ".dwp";
ed2dc618 13227 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13228 }
13229
80626a55
DE
13230 if (dbfd == NULL)
13231 {
b4f54984 13232 if (dwarf_read_debug)
d721ba37 13233 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13234 return std::unique_ptr<dwp_file> ();
3019eac3 13235 }
400174b1
TT
13236
13237 const char *name = bfd_get_filename (dbfd.get ());
13238 std::unique_ptr<struct dwp_file> dwp_file
13239 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13240
0a0f4c01 13241 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13242 dwp_file->elf_sections =
13243 OBSTACK_CALLOC (&objfile->objfile_obstack,
13244 dwp_file->num_sections, asection *);
13245
400174b1
TT
13246 bfd_map_over_sections (dwp_file->dbfd.get (),
13247 dwarf2_locate_common_dwp_sections,
13248 dwp_file.get ());
80626a55 13249
400174b1
TT
13250 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13251 0);
80626a55 13252
400174b1
TT
13253 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13254 1);
80626a55 13255
73869dc2 13256 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13257 if (dwp_file->cus && dwp_file->tus
13258 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13259 {
13260 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13261 pretty bizarre. We use pulongest here because that's the established
4d65956b 13262 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13263 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13264 " TU version %s [in DWP file %s]"),
13265 pulongest (dwp_file->cus->version),
d721ba37 13266 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13267 }
08302ed2
DE
13268
13269 if (dwp_file->cus)
13270 dwp_file->version = dwp_file->cus->version;
13271 else if (dwp_file->tus)
13272 dwp_file->version = dwp_file->tus->version;
13273 else
13274 dwp_file->version = 2;
73869dc2
DE
13275
13276 if (dwp_file->version == 2)
400174b1
TT
13277 bfd_map_over_sections (dwp_file->dbfd.get (),
13278 dwarf2_locate_v2_dwp_sections,
13279 dwp_file.get ());
73869dc2 13280
19ac8c2e
DE
13281 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13282 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13283
b4f54984 13284 if (dwarf_read_debug)
80626a55
DE
13285 {
13286 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13287 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13288 " %s CUs, %s TUs\n",
13289 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13290 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13291 }
13292
13293 return dwp_file;
3019eac3 13294}
c906108c 13295
ab5088bf
DE
13296/* Wrapper around open_and_init_dwp_file, only open it once. */
13297
13298static struct dwp_file *
ed2dc618 13299get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13300{
13301 if (! dwarf2_per_objfile->dwp_checked)
13302 {
ed2dc618
SM
13303 dwarf2_per_objfile->dwp_file
13304 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13305 dwarf2_per_objfile->dwp_checked = 1;
13306 }
400174b1 13307 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13308}
13309
80626a55
DE
13310/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13311 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13312 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13313 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13314 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13315
13316 This is called, for example, when wanting to read a variable with a
13317 complex location. Therefore we don't want to do file i/o for every call.
13318 Therefore we don't want to look for a DWO file on every call.
13319 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13320 then we check if we've already seen DWO_NAME, and only THEN do we check
13321 for a DWO file.
13322
1c658ad5 13323 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13324 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13325
3019eac3 13326static struct dwo_unit *
80626a55
DE
13327lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13328 const char *dwo_name, const char *comp_dir,
13329 ULONGEST signature, int is_debug_types)
3019eac3 13330{
ed2dc618 13331 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13332 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13333 const char *kind = is_debug_types ? "TU" : "CU";
13334 void **dwo_file_slot;
3019eac3 13335 struct dwo_file *dwo_file;
80626a55 13336 struct dwp_file *dwp_file;
cb1df416 13337
6a506a2d
DE
13338 /* First see if there's a DWP file.
13339 If we have a DWP file but didn't find the DWO inside it, don't
13340 look for the original DWO file. It makes gdb behave differently
13341 depending on whether one is debugging in the build tree. */
cf2c3c16 13342
ed2dc618 13343 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13344 if (dwp_file != NULL)
cf2c3c16 13345 {
80626a55
DE
13346 const struct dwp_hash_table *dwp_htab =
13347 is_debug_types ? dwp_file->tus : dwp_file->cus;
13348
13349 if (dwp_htab != NULL)
13350 {
13351 struct dwo_unit *dwo_cutu =
ed2dc618 13352 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13353 signature, is_debug_types);
80626a55
DE
13354
13355 if (dwo_cutu != NULL)
13356 {
b4f54984 13357 if (dwarf_read_debug)
80626a55
DE
13358 {
13359 fprintf_unfiltered (gdb_stdlog,
13360 "Virtual DWO %s %s found: @%s\n",
13361 kind, hex_string (signature),
13362 host_address_to_string (dwo_cutu));
13363 }
13364 return dwo_cutu;
13365 }
13366 }
13367 }
6a506a2d 13368 else
80626a55 13369 {
6a506a2d 13370 /* No DWP file, look for the DWO file. */
80626a55 13371
ed2dc618
SM
13372 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13373 dwo_name, comp_dir);
6a506a2d 13374 if (*dwo_file_slot == NULL)
80626a55 13375 {
6a506a2d
DE
13376 /* Read in the file and build a table of the CUs/TUs it contains. */
13377 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13378 }
6a506a2d 13379 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13380 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13381
6a506a2d 13382 if (dwo_file != NULL)
19c3d4c9 13383 {
6a506a2d
DE
13384 struct dwo_unit *dwo_cutu = NULL;
13385
13386 if (is_debug_types && dwo_file->tus)
13387 {
13388 struct dwo_unit find_dwo_cutu;
13389
13390 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13391 find_dwo_cutu.signature = signature;
9a3c8263
SM
13392 dwo_cutu
13393 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13394 }
33c5cd75 13395 else if (!is_debug_types && dwo_file->cus)
80626a55 13396 {
33c5cd75
DB
13397 struct dwo_unit find_dwo_cutu;
13398
13399 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13400 find_dwo_cutu.signature = signature;
13401 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13402 &find_dwo_cutu);
6a506a2d
DE
13403 }
13404
13405 if (dwo_cutu != NULL)
13406 {
b4f54984 13407 if (dwarf_read_debug)
6a506a2d
DE
13408 {
13409 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13410 kind, dwo_name, hex_string (signature),
13411 host_address_to_string (dwo_cutu));
13412 }
13413 return dwo_cutu;
80626a55
DE
13414 }
13415 }
2e276125 13416 }
9cdd5dbd 13417
80626a55
DE
13418 /* We didn't find it. This could mean a dwo_id mismatch, or
13419 someone deleted the DWO/DWP file, or the search path isn't set up
13420 correctly to find the file. */
13421
b4f54984 13422 if (dwarf_read_debug)
80626a55
DE
13423 {
13424 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13425 kind, dwo_name, hex_string (signature));
13426 }
3019eac3 13427
6656a72d
DE
13428 /* This is a warning and not a complaint because it can be caused by
13429 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13430 {
13431 /* Print the name of the DWP file if we looked there, helps the user
13432 better diagnose the problem. */
791afaa2 13433 std::string dwp_text;
43942612
DE
13434
13435 if (dwp_file != NULL)
791afaa2
TT
13436 dwp_text = string_printf (" [in DWP file %s]",
13437 lbasename (dwp_file->name));
43942612 13438
9d8780f0 13439 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13440 " [in module %s]"),
13441 kind, dwo_name, hex_string (signature),
791afaa2 13442 dwp_text.c_str (),
43942612 13443 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13444 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13445 }
3019eac3 13446 return NULL;
5fb290d7
DJ
13447}
13448
80626a55
DE
13449/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13450 See lookup_dwo_cutu_unit for details. */
13451
13452static struct dwo_unit *
13453lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13454 const char *dwo_name, const char *comp_dir,
13455 ULONGEST signature)
13456{
13457 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13458}
13459
13460/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13461 See lookup_dwo_cutu_unit for details. */
13462
13463static struct dwo_unit *
13464lookup_dwo_type_unit (struct signatured_type *this_tu,
13465 const char *dwo_name, const char *comp_dir)
13466{
13467 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13468}
13469
89e63ee4
DE
13470/* Traversal function for queue_and_load_all_dwo_tus. */
13471
13472static int
13473queue_and_load_dwo_tu (void **slot, void *info)
13474{
13475 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13476 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13477 ULONGEST signature = dwo_unit->signature;
13478 struct signatured_type *sig_type =
13479 lookup_dwo_signatured_type (per_cu->cu, signature);
13480
13481 if (sig_type != NULL)
13482 {
13483 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13484
13485 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13486 a real dependency of PER_CU on SIG_TYPE. That is detected later
13487 while processing PER_CU. */
13488 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13489 load_full_type_unit (sig_cu);
ae640021 13490 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13491 }
13492
13493 return 1;
13494}
13495
13496/* Queue all TUs contained in the DWO of PER_CU to be read in.
13497 The DWO may have the only definition of the type, though it may not be
13498 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13499 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13500
13501static void
13502queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13503{
13504 struct dwo_unit *dwo_unit;
13505 struct dwo_file *dwo_file;
13506
13507 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13508 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13509 gdb_assert (per_cu->cu != NULL);
13510
13511 dwo_unit = per_cu->cu->dwo_unit;
13512 gdb_assert (dwo_unit != NULL);
13513
13514 dwo_file = dwo_unit->dwo_file;
13515 if (dwo_file->tus != NULL)
13516 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13517}
13518
3019eac3 13519/* Read in various DIEs. */
348e048f 13520
d389af10 13521/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13522 Inherit only the children of the DW_AT_abstract_origin DIE not being
13523 already referenced by DW_AT_abstract_origin from the children of the
13524 current DIE. */
d389af10
JK
13525
13526static void
13527inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13528{
13529 struct die_info *child_die;
791afaa2 13530 sect_offset *offsetp;
d389af10
JK
13531 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13532 struct die_info *origin_die;
13533 /* Iterator of the ORIGIN_DIE children. */
13534 struct die_info *origin_child_die;
d389af10 13535 struct attribute *attr;
cd02d79d
PA
13536 struct dwarf2_cu *origin_cu;
13537 struct pending **origin_previous_list_in_scope;
d389af10
JK
13538
13539 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13540 if (!attr)
13541 return;
13542
cd02d79d
PA
13543 /* Note that following die references may follow to a die in a
13544 different cu. */
13545
13546 origin_cu = cu;
13547 origin_die = follow_die_ref (die, attr, &origin_cu);
13548
13549 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13550 symbols in. */
13551 origin_previous_list_in_scope = origin_cu->list_in_scope;
13552 origin_cu->list_in_scope = cu->list_in_scope;
13553
edb3359d
DJ
13554 if (die->tag != origin_die->tag
13555 && !(die->tag == DW_TAG_inlined_subroutine
13556 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13557 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13558 sect_offset_str (die->sect_off),
13559 sect_offset_str (origin_die->sect_off));
d389af10 13560
791afaa2 13561 std::vector<sect_offset> offsets;
d389af10 13562
3ea89b92
PMR
13563 for (child_die = die->child;
13564 child_die && child_die->tag;
13565 child_die = sibling_die (child_die))
13566 {
13567 struct die_info *child_origin_die;
13568 struct dwarf2_cu *child_origin_cu;
13569
13570 /* We are trying to process concrete instance entries:
216f72a1 13571 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13572 it's not relevant to our analysis here. i.e. detecting DIEs that are
13573 present in the abstract instance but not referenced in the concrete
13574 one. */
216f72a1
JK
13575 if (child_die->tag == DW_TAG_call_site
13576 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13577 continue;
13578
c38f313d
DJ
13579 /* For each CHILD_DIE, find the corresponding child of
13580 ORIGIN_DIE. If there is more than one layer of
13581 DW_AT_abstract_origin, follow them all; there shouldn't be,
13582 but GCC versions at least through 4.4 generate this (GCC PR
13583 40573). */
3ea89b92
PMR
13584 child_origin_die = child_die;
13585 child_origin_cu = cu;
c38f313d
DJ
13586 while (1)
13587 {
cd02d79d
PA
13588 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13589 child_origin_cu);
c38f313d
DJ
13590 if (attr == NULL)
13591 break;
cd02d79d
PA
13592 child_origin_die = follow_die_ref (child_origin_die, attr,
13593 &child_origin_cu);
c38f313d
DJ
13594 }
13595
d389af10
JK
13596 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13597 counterpart may exist. */
c38f313d 13598 if (child_origin_die != child_die)
d389af10 13599 {
edb3359d
DJ
13600 if (child_die->tag != child_origin_die->tag
13601 && !(child_die->tag == DW_TAG_inlined_subroutine
13602 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13603 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13604 "different tags"),
9d8780f0
SM
13605 sect_offset_str (child_die->sect_off),
13606 sect_offset_str (child_origin_die->sect_off));
c38f313d 13607 if (child_origin_die->parent != origin_die)
b98664d3 13608 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13609 "different parents"),
9d8780f0
SM
13610 sect_offset_str (child_die->sect_off),
13611 sect_offset_str (child_origin_die->sect_off));
c38f313d 13612 else
791afaa2 13613 offsets.push_back (child_origin_die->sect_off);
d389af10 13614 }
d389af10 13615 }
791afaa2
TT
13616 std::sort (offsets.begin (), offsets.end ());
13617 sect_offset *offsets_end = offsets.data () + offsets.size ();
13618 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13619 if (offsetp[-1] == *offsetp)
b98664d3 13620 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13621 "to DIE %s as their abstract origin"),
13622 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13623
791afaa2 13624 offsetp = offsets.data ();
d389af10
JK
13625 origin_child_die = origin_die->child;
13626 while (origin_child_die && origin_child_die->tag)
13627 {
13628 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13629 while (offsetp < offsets_end
9c541725 13630 && *offsetp < origin_child_die->sect_off)
d389af10 13631 offsetp++;
b64f50a1 13632 if (offsetp >= offsets_end
9c541725 13633 || *offsetp > origin_child_die->sect_off)
d389af10 13634 {
adde2bff
DE
13635 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13636 Check whether we're already processing ORIGIN_CHILD_DIE.
13637 This can happen with mutually referenced abstract_origins.
13638 PR 16581. */
13639 if (!origin_child_die->in_process)
13640 process_die (origin_child_die, origin_cu);
d389af10
JK
13641 }
13642 origin_child_die = sibling_die (origin_child_die);
13643 }
cd02d79d 13644 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13645}
13646
c906108c 13647static void
e7c27a73 13648read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13649{
518817b3 13650 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13651 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13652 struct context_stack *newobj;
c906108c
SS
13653 CORE_ADDR lowpc;
13654 CORE_ADDR highpc;
13655 struct die_info *child_die;
edb3359d 13656 struct attribute *attr, *call_line, *call_file;
15d034d0 13657 const char *name;
e142c38c 13658 CORE_ADDR baseaddr;
801e3a5b 13659 struct block *block;
edb3359d 13660 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13661 std::vector<struct symbol *> template_args;
34eaf542 13662 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13663
13664 if (inlined_func)
13665 {
13666 /* If we do not have call site information, we can't show the
13667 caller of this inlined function. That's too confusing, so
13668 only use the scope for local variables. */
13669 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13670 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13671 if (call_line == NULL || call_file == NULL)
13672 {
13673 read_lexical_block_scope (die, cu);
13674 return;
13675 }
13676 }
c906108c 13677
e142c38c
DJ
13678 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13679
94af9270 13680 name = dwarf2_name (die, cu);
c906108c 13681
e8d05480
JB
13682 /* Ignore functions with missing or empty names. These are actually
13683 illegal according to the DWARF standard. */
13684 if (name == NULL)
13685 {
b98664d3 13686 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13687 sect_offset_str (die->sect_off));
e8d05480
JB
13688 return;
13689 }
13690
13691 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13692 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13693 <= PC_BOUNDS_INVALID)
e8d05480 13694 {
ae4d0c03
PM
13695 attr = dwarf2_attr (die, DW_AT_external, cu);
13696 if (!attr || !DW_UNSND (attr))
b98664d3 13697 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13698 "for subprogram DIE at %s"),
13699 sect_offset_str (die->sect_off));
e8d05480
JB
13700 return;
13701 }
c906108c 13702
3e29f34a
MR
13703 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13704 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13705
34eaf542
TT
13706 /* If we have any template arguments, then we must allocate a
13707 different sort of symbol. */
13708 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13709 {
13710 if (child_die->tag == DW_TAG_template_type_param
13711 || child_die->tag == DW_TAG_template_value_param)
13712 {
e623cf5d 13713 templ_func = allocate_template_symbol (objfile);
cf724bc9 13714 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13715 break;
13716 }
13717 }
13718
c24bdb02 13719 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13720 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13721 (struct symbol *) templ_func);
4c2df51b 13722
81873cc8
TV
13723 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13724 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13725 cu->language);
13726
4cecd739
DJ
13727 /* If there is a location expression for DW_AT_frame_base, record
13728 it. */
e142c38c 13729 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13730 if (attr)
fe978cb0 13731 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13732
63e43d3a
PMR
13733 /* If there is a location for the static link, record it. */
13734 newobj->static_link = NULL;
13735 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13736 if (attr)
13737 {
224c3ddb
SM
13738 newobj->static_link
13739 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13740 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13741 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13742 }
13743
c24bdb02 13744 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13745
639d11d3 13746 if (die->child != NULL)
c906108c 13747 {
639d11d3 13748 child_die = die->child;
c906108c
SS
13749 while (child_die && child_die->tag)
13750 {
34eaf542
TT
13751 if (child_die->tag == DW_TAG_template_type_param
13752 || child_die->tag == DW_TAG_template_value_param)
13753 {
13754 struct symbol *arg = new_symbol (child_die, NULL, cu);
13755
f1078f66 13756 if (arg != NULL)
2f4732b0 13757 template_args.push_back (arg);
34eaf542
TT
13758 }
13759 else
13760 process_die (child_die, cu);
c906108c
SS
13761 child_die = sibling_die (child_die);
13762 }
13763 }
13764
d389af10
JK
13765 inherit_abstract_dies (die, cu);
13766
4a811a97
UW
13767 /* If we have a DW_AT_specification, we might need to import using
13768 directives from the context of the specification DIE. See the
13769 comment in determine_prefix. */
13770 if (cu->language == language_cplus
13771 && dwarf2_attr (die, DW_AT_specification, cu))
13772 {
13773 struct dwarf2_cu *spec_cu = cu;
13774 struct die_info *spec_die = die_specification (die, &spec_cu);
13775
13776 while (spec_die)
13777 {
13778 child_die = spec_die->child;
13779 while (child_die && child_die->tag)
13780 {
13781 if (child_die->tag == DW_TAG_imported_module)
13782 process_die (child_die, spec_cu);
13783 child_die = sibling_die (child_die);
13784 }
13785
13786 /* In some cases, GCC generates specification DIEs that
13787 themselves contain DW_AT_specification attributes. */
13788 spec_die = die_specification (spec_die, &spec_cu);
13789 }
13790 }
13791
c24bdb02 13792 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13793 /* Make a block for the local symbols within. */
c24bdb02 13794 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13795 cstk.static_link, lowpc, highpc);
801e3a5b 13796
df8a16a1 13797 /* For C++, set the block's scope. */
45280282
IB
13798 if ((cu->language == language_cplus
13799 || cu->language == language_fortran
c44af4eb
TT
13800 || cu->language == language_d
13801 || cu->language == language_rust)
4d4ec4e5 13802 && cu->processing_has_namespace_info)
195a3f6c
TT
13803 block_set_scope (block, determine_prefix (die, cu),
13804 &objfile->objfile_obstack);
df8a16a1 13805
801e3a5b
JB
13806 /* If we have address ranges, record them. */
13807 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13808
a60f3166 13809 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13810
34eaf542 13811 /* Attach template arguments to function. */
2f4732b0 13812 if (!template_args.empty ())
34eaf542
TT
13813 {
13814 gdb_assert (templ_func != NULL);
13815
2f4732b0 13816 templ_func->n_template_arguments = template_args.size ();
34eaf542 13817 templ_func->template_arguments
8d749320
SM
13818 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13819 templ_func->n_template_arguments);
34eaf542 13820 memcpy (templ_func->template_arguments,
2f4732b0 13821 template_args.data (),
34eaf542 13822 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13823
13824 /* Make sure that the symtab is set on the new symbols. Even
13825 though they don't appear in this symtab directly, other parts
13826 of gdb assume that symbols do, and this is reasonably
13827 true. */
8634679f 13828 for (symbol *sym : template_args)
3e1d3d8c 13829 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13830 }
13831
208d8187
JB
13832 /* In C++, we can have functions nested inside functions (e.g., when
13833 a function declares a class that has methods). This means that
13834 when we finish processing a function scope, we may need to go
13835 back to building a containing block's symbol lists. */
c24bdb02
KS
13836 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13837 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13838
921e78cf
JB
13839 /* If we've finished processing a top-level function, subsequent
13840 symbols go in the file symbol list. */
c24bdb02
KS
13841 if (cu->get_builder ()->outermost_context_p ())
13842 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13843}
13844
13845/* Process all the DIES contained within a lexical block scope. Start
13846 a new scope, process the dies, and then close the scope. */
13847
13848static void
e7c27a73 13849read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13850{
518817b3 13851 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13852 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13853 CORE_ADDR lowpc, highpc;
13854 struct die_info *child_die;
e142c38c
DJ
13855 CORE_ADDR baseaddr;
13856
13857 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13858
13859 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13860 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13861 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13862 be nasty. Might be easier to properly extend generic blocks to
af34e669 13863 describe ranges. */
e385593e
JK
13864 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13865 {
13866 case PC_BOUNDS_NOT_PRESENT:
13867 /* DW_TAG_lexical_block has no attributes, process its children as if
13868 there was no wrapping by that DW_TAG_lexical_block.
13869 GCC does no longer produces such DWARF since GCC r224161. */
13870 for (child_die = die->child;
13871 child_die != NULL && child_die->tag;
13872 child_die = sibling_die (child_die))
13873 process_die (child_die, cu);
13874 return;
13875 case PC_BOUNDS_INVALID:
13876 return;
13877 }
3e29f34a
MR
13878 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13879 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13880
c24bdb02 13881 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13882 if (die->child != NULL)
c906108c 13883 {
639d11d3 13884 child_die = die->child;
c906108c
SS
13885 while (child_die && child_die->tag)
13886 {
e7c27a73 13887 process_die (child_die, cu);
c906108c
SS
13888 child_die = sibling_die (child_die);
13889 }
13890 }
3ea89b92 13891 inherit_abstract_dies (die, cu);
c24bdb02 13892 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13893
c24bdb02
KS
13894 if (*cu->get_builder ()->get_local_symbols () != NULL
13895 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13896 {
801e3a5b 13897 struct block *block
c24bdb02 13898 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13899 cstk.start_addr, highpc);
801e3a5b
JB
13900
13901 /* Note that recording ranges after traversing children, as we
13902 do here, means that recording a parent's ranges entails
13903 walking across all its children's ranges as they appear in
13904 the address map, which is quadratic behavior.
13905
13906 It would be nicer to record the parent's ranges before
13907 traversing its children, simply overriding whatever you find
13908 there. But since we don't even decide whether to create a
13909 block until after we've traversed its children, that's hard
13910 to do. */
13911 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13912 }
c24bdb02
KS
13913 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13914 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13915}
13916
216f72a1 13917/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13918
13919static void
13920read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13921{
518817b3 13922 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13923 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13924 CORE_ADDR pc, baseaddr;
13925 struct attribute *attr;
13926 struct call_site *call_site, call_site_local;
13927 void **slot;
13928 int nparams;
13929 struct die_info *child_die;
13930
13931 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13932
216f72a1
JK
13933 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13934 if (attr == NULL)
13935 {
13936 /* This was a pre-DWARF-5 GNU extension alias
13937 for DW_AT_call_return_pc. */
13938 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13939 }
96408a79
SA
13940 if (!attr)
13941 {
b98664d3 13942 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13943 "DIE %s [in module %s]"),
13944 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13945 return;
13946 }
31aa7e4e 13947 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13948 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13949
13950 if (cu->call_site_htab == NULL)
13951 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13952 NULL, &objfile->objfile_obstack,
13953 hashtab_obstack_allocate, NULL);
13954 call_site_local.pc = pc;
13955 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13956 if (*slot != NULL)
13957 {
b98664d3 13958 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13959 "DIE %s [in module %s]"),
13960 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13961 objfile_name (objfile));
96408a79
SA
13962 return;
13963 }
13964
13965 /* Count parameters at the caller. */
13966
13967 nparams = 0;
13968 for (child_die = die->child; child_die && child_die->tag;
13969 child_die = sibling_die (child_die))
13970 {
216f72a1
JK
13971 if (child_die->tag != DW_TAG_call_site_parameter
13972 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13973 {
b98664d3 13974 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13975 "DW_TAG_call_site child DIE %s [in module %s]"),
13976 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13977 objfile_name (objfile));
96408a79
SA
13978 continue;
13979 }
13980
13981 nparams++;
13982 }
13983
224c3ddb
SM
13984 call_site
13985 = ((struct call_site *)
13986 obstack_alloc (&objfile->objfile_obstack,
13987 sizeof (*call_site)
13988 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13989 *slot = call_site;
13990 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13991 call_site->pc = pc;
13992
216f72a1
JK
13993 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13994 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13995 {
13996 struct die_info *func_die;
13997
13998 /* Skip also over DW_TAG_inlined_subroutine. */
13999 for (func_die = die->parent;
14000 func_die && func_die->tag != DW_TAG_subprogram
14001 && func_die->tag != DW_TAG_subroutine_type;
14002 func_die = func_die->parent);
14003
216f72a1
JK
14004 /* DW_AT_call_all_calls is a superset
14005 of DW_AT_call_all_tail_calls. */
96408a79 14006 if (func_die
216f72a1 14007 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14008 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14009 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14010 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14011 {
14012 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14013 not complete. But keep CALL_SITE for look ups via call_site_htab,
14014 both the initial caller containing the real return address PC and
14015 the final callee containing the current PC of a chain of tail
14016 calls do not need to have the tail call list complete. But any
14017 function candidate for a virtual tail call frame searched via
14018 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14019 determined unambiguously. */
14020 }
14021 else
14022 {
14023 struct type *func_type = NULL;
14024
14025 if (func_die)
14026 func_type = get_die_type (func_die, cu);
14027 if (func_type != NULL)
14028 {
14029 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14030
14031 /* Enlist this call site to the function. */
14032 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14033 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14034 }
14035 else
b98664d3 14036 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14037 "DIE %s [in module %s]"),
14038 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14039 }
14040 }
14041
216f72a1
JK
14042 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14043 if (attr == NULL)
14044 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14045 if (attr == NULL)
14046 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14047 if (attr == NULL)
216f72a1
JK
14048 {
14049 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14050 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14051 }
96408a79
SA
14052 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14053 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14054 /* Keep NULL DWARF_BLOCK. */;
14055 else if (attr_form_is_block (attr))
14056 {
14057 struct dwarf2_locexpr_baton *dlbaton;
14058
8d749320 14059 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14060 dlbaton->data = DW_BLOCK (attr)->data;
14061 dlbaton->size = DW_BLOCK (attr)->size;
14062 dlbaton->per_cu = cu->per_cu;
14063
14064 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14065 }
7771576e 14066 else if (attr_form_is_ref (attr))
96408a79 14067 {
96408a79
SA
14068 struct dwarf2_cu *target_cu = cu;
14069 struct die_info *target_die;
14070
ac9ec31b 14071 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14072 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14073 if (die_is_declaration (target_die, target_cu))
14074 {
7d45c7c3 14075 const char *target_physname;
9112db09
JK
14076
14077 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14078 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14079 if (target_physname == NULL)
9112db09 14080 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14081 if (target_physname == NULL)
b98664d3 14082 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14083 "physname, for referencing DIE %s [in module %s]"),
14084 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14085 else
7d455152 14086 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14087 }
14088 else
14089 {
14090 CORE_ADDR lowpc;
14091
14092 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14093 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14094 <= PC_BOUNDS_INVALID)
b98664d3 14095 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14096 "low pc, for referencing DIE %s [in module %s]"),
14097 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14098 else
3e29f34a
MR
14099 {
14100 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14101 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14102 }
96408a79
SA
14103 }
14104 }
14105 else
b98664d3 14106 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14107 "block nor reference, for DIE %s [in module %s]"),
14108 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14109
14110 call_site->per_cu = cu->per_cu;
14111
14112 for (child_die = die->child;
14113 child_die && child_die->tag;
14114 child_die = sibling_die (child_die))
14115 {
96408a79 14116 struct call_site_parameter *parameter;
1788b2d3 14117 struct attribute *loc, *origin;
96408a79 14118
216f72a1
JK
14119 if (child_die->tag != DW_TAG_call_site_parameter
14120 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14121 {
14122 /* Already printed the complaint above. */
14123 continue;
14124 }
14125
14126 gdb_assert (call_site->parameter_count < nparams);
14127 parameter = &call_site->parameter[call_site->parameter_count];
14128
1788b2d3
JK
14129 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14130 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14131 register is contained in DW_AT_call_value. */
96408a79 14132
24c5c679 14133 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14134 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14135 if (origin == NULL)
14136 {
14137 /* This was a pre-DWARF-5 GNU extension alias
14138 for DW_AT_call_parameter. */
14139 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14140 }
7771576e 14141 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14142 {
1788b2d3 14143 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14144
14145 sect_offset sect_off
14146 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14147 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14148 {
14149 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14150 binding can be done only inside one CU. Such referenced DIE
14151 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14152 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14153 "DW_TAG_call_site child DIE %s [in module %s]"),
14154 sect_offset_str (child_die->sect_off),
9c541725 14155 objfile_name (objfile));
d76b7dbc
JK
14156 continue;
14157 }
9c541725
PA
14158 parameter->u.param_cu_off
14159 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14160 }
14161 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14162 {
b98664d3 14163 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14164 "DW_TAG_call_site child DIE %s [in module %s]"),
14165 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14166 continue;
14167 }
24c5c679 14168 else
96408a79 14169 {
24c5c679
JK
14170 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14171 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14172 if (parameter->u.dwarf_reg != -1)
14173 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14174 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14175 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14176 &parameter->u.fb_offset))
14177 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14178 else
14179 {
b98664d3 14180 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14181 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14182 "DW_TAG_call_site child DIE %s "
24c5c679 14183 "[in module %s]"),
9d8780f0 14184 sect_offset_str (child_die->sect_off),
9c541725 14185 objfile_name (objfile));
24c5c679
JK
14186 continue;
14187 }
96408a79
SA
14188 }
14189
216f72a1
JK
14190 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14191 if (attr == NULL)
14192 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14193 if (!attr_form_is_block (attr))
14194 {
b98664d3 14195 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14196 "DW_TAG_call_site child DIE %s [in module %s]"),
14197 sect_offset_str (child_die->sect_off),
9c541725 14198 objfile_name (objfile));
96408a79
SA
14199 continue;
14200 }
14201 parameter->value = DW_BLOCK (attr)->data;
14202 parameter->value_size = DW_BLOCK (attr)->size;
14203
14204 /* Parameters are not pre-cleared by memset above. */
14205 parameter->data_value = NULL;
14206 parameter->data_value_size = 0;
14207 call_site->parameter_count++;
14208
216f72a1
JK
14209 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14210 if (attr == NULL)
14211 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14212 if (attr)
14213 {
14214 if (!attr_form_is_block (attr))
b98664d3 14215 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14216 "DW_TAG_call_site child DIE %s [in module %s]"),
14217 sect_offset_str (child_die->sect_off),
9c541725 14218 objfile_name (objfile));
96408a79
SA
14219 else
14220 {
14221 parameter->data_value = DW_BLOCK (attr)->data;
14222 parameter->data_value_size = DW_BLOCK (attr)->size;
14223 }
14224 }
14225 }
14226}
14227
71a3c369
TT
14228/* Helper function for read_variable. If DIE represents a virtual
14229 table, then return the type of the concrete object that is
14230 associated with the virtual table. Otherwise, return NULL. */
14231
14232static struct type *
14233rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14234{
14235 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14236 if (attr == NULL)
14237 return NULL;
14238
14239 /* Find the type DIE. */
14240 struct die_info *type_die = NULL;
14241 struct dwarf2_cu *type_cu = cu;
14242
14243 if (attr_form_is_ref (attr))
14244 type_die = follow_die_ref (die, attr, &type_cu);
14245 if (type_die == NULL)
14246 return NULL;
14247
14248 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14249 return NULL;
14250 return die_containing_type (type_die, type_cu);
14251}
14252
14253/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14254
14255static void
14256read_variable (struct die_info *die, struct dwarf2_cu *cu)
14257{
14258 struct rust_vtable_symbol *storage = NULL;
14259
14260 if (cu->language == language_rust)
14261 {
14262 struct type *containing_type = rust_containing_type (die, cu);
14263
14264 if (containing_type != NULL)
14265 {
518817b3 14266 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14267
14268 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14269 struct rust_vtable_symbol);
14270 initialize_objfile_symbol (storage);
14271 storage->concrete_type = containing_type;
cf724bc9 14272 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14273 }
14274 }
14275
e4a62c65
TV
14276 struct symbol *res = new_symbol (die, NULL, cu, storage);
14277 struct attribute *abstract_origin
14278 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14279 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14280 if (res == NULL && loc && abstract_origin)
14281 {
14282 /* We have a variable without a name, but with a location and an abstract
14283 origin. This may be a concrete instance of an abstract variable
14284 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14285 later. */
14286 struct dwarf2_cu *origin_cu = cu;
14287 struct die_info *origin_die
14288 = follow_die_ref (die, abstract_origin, &origin_cu);
14289 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14290 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14291 }
71a3c369
TT
14292}
14293
43988095
JK
14294/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14295 reading .debug_rnglists.
14296 Callback's type should be:
14297 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14298 Return true if the attributes are present and valid, otherwise,
14299 return false. */
14300
14301template <typename Callback>
14302static bool
14303dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14304 Callback &&callback)
14305{
ed2dc618 14306 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14307 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14308 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14309 bfd *obfd = objfile->obfd;
43988095
JK
14310 /* Base address selection entry. */
14311 CORE_ADDR base;
14312 int found_base;
43988095 14313 const gdb_byte *buffer;
43988095
JK
14314 CORE_ADDR baseaddr;
14315 bool overflow = false;
14316
14317 found_base = cu->base_known;
14318 base = cu->base_address;
14319
14320 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14321 if (offset >= dwarf2_per_objfile->rnglists.size)
14322 {
b98664d3 14323 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14324 offset);
14325 return false;
14326 }
14327 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14328
14329 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14330
14331 while (1)
14332 {
7814882a
JK
14333 /* Initialize it due to a false compiler warning. */
14334 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14335 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14336 + dwarf2_per_objfile->rnglists.size);
14337 unsigned int bytes_read;
14338
14339 if (buffer == buf_end)
14340 {
14341 overflow = true;
14342 break;
14343 }
14344 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14345 switch (rlet)
14346 {
14347 case DW_RLE_end_of_list:
14348 break;
14349 case DW_RLE_base_address:
14350 if (buffer + cu->header.addr_size > buf_end)
14351 {
14352 overflow = true;
14353 break;
14354 }
14355 base = read_address (obfd, buffer, cu, &bytes_read);
14356 found_base = 1;
14357 buffer += bytes_read;
14358 break;
14359 case DW_RLE_start_length:
14360 if (buffer + cu->header.addr_size > buf_end)
14361 {
14362 overflow = true;
14363 break;
14364 }
14365 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14366 buffer += bytes_read;
14367 range_end = (range_beginning
14368 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14369 buffer += bytes_read;
14370 if (buffer > buf_end)
14371 {
14372 overflow = true;
14373 break;
14374 }
14375 break;
14376 case DW_RLE_offset_pair:
14377 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14378 buffer += bytes_read;
14379 if (buffer > buf_end)
14380 {
14381 overflow = true;
14382 break;
14383 }
14384 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14385 buffer += bytes_read;
14386 if (buffer > buf_end)
14387 {
14388 overflow = true;
14389 break;
14390 }
14391 break;
14392 case DW_RLE_start_end:
14393 if (buffer + 2 * cu->header.addr_size > buf_end)
14394 {
14395 overflow = true;
14396 break;
14397 }
14398 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14399 buffer += bytes_read;
14400 range_end = read_address (obfd, buffer, cu, &bytes_read);
14401 buffer += bytes_read;
14402 break;
14403 default:
b98664d3 14404 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14405 return false;
14406 }
14407 if (rlet == DW_RLE_end_of_list || overflow)
14408 break;
14409 if (rlet == DW_RLE_base_address)
14410 continue;
14411
14412 if (!found_base)
14413 {
14414 /* We have no valid base address for the ranges
14415 data. */
b98664d3 14416 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14417 return false;
14418 }
14419
14420 if (range_beginning > range_end)
14421 {
14422 /* Inverted range entries are invalid. */
b98664d3 14423 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14424 return false;
14425 }
14426
14427 /* Empty range entries have no effect. */
14428 if (range_beginning == range_end)
14429 continue;
14430
14431 range_beginning += base;
14432 range_end += base;
14433
14434 /* A not-uncommon case of bad debug info.
14435 Don't pollute the addrmap with bad data. */
14436 if (range_beginning + baseaddr == 0
14437 && !dwarf2_per_objfile->has_section_at_zero)
14438 {
b98664d3 14439 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14440 " [in module %s]"), objfile_name (objfile));
14441 continue;
14442 }
14443
14444 callback (range_beginning, range_end);
14445 }
14446
14447 if (overflow)
14448 {
b98664d3 14449 complaint (_("Offset %d is not terminated "
43988095
JK
14450 "for DW_AT_ranges attribute"),
14451 offset);
14452 return false;
14453 }
14454
14455 return true;
14456}
14457
14458/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14459 Callback's type should be:
14460 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14461 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14462
43988095 14463template <typename Callback>
43039443 14464static int
5f46c5a5 14465dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14466 Callback &&callback)
43039443 14467{
ed2dc618 14468 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14469 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14470 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14471 struct comp_unit_head *cu_header = &cu->header;
14472 bfd *obfd = objfile->obfd;
14473 unsigned int addr_size = cu_header->addr_size;
14474 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14475 /* Base address selection entry. */
14476 CORE_ADDR base;
14477 int found_base;
14478 unsigned int dummy;
d521ce57 14479 const gdb_byte *buffer;
ff013f42 14480 CORE_ADDR baseaddr;
43039443 14481
43988095
JK
14482 if (cu_header->version >= 5)
14483 return dwarf2_rnglists_process (offset, cu, callback);
14484
d00adf39
DE
14485 found_base = cu->base_known;
14486 base = cu->base_address;
43039443 14487
be391dca 14488 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14489 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14490 {
b98664d3 14491 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14492 offset);
14493 return 0;
14494 }
dce234bc 14495 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14496
e7030f15 14497 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14498
43039443
JK
14499 while (1)
14500 {
14501 CORE_ADDR range_beginning, range_end;
14502
14503 range_beginning = read_address (obfd, buffer, cu, &dummy);
14504 buffer += addr_size;
14505 range_end = read_address (obfd, buffer, cu, &dummy);
14506 buffer += addr_size;
14507 offset += 2 * addr_size;
14508
14509 /* An end of list marker is a pair of zero addresses. */
14510 if (range_beginning == 0 && range_end == 0)
14511 /* Found the end of list entry. */
14512 break;
14513
14514 /* Each base address selection entry is a pair of 2 values.
14515 The first is the largest possible address, the second is
14516 the base address. Check for a base address here. */
14517 if ((range_beginning & mask) == mask)
14518 {
28d2bfb9
AB
14519 /* If we found the largest possible address, then we already
14520 have the base address in range_end. */
14521 base = range_end;
43039443
JK
14522 found_base = 1;
14523 continue;
14524 }
14525
14526 if (!found_base)
14527 {
14528 /* We have no valid base address for the ranges
14529 data. */
b98664d3 14530 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14531 return 0;
14532 }
14533
9277c30c
UW
14534 if (range_beginning > range_end)
14535 {
14536 /* Inverted range entries are invalid. */
b98664d3 14537 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14538 return 0;
14539 }
14540
14541 /* Empty range entries have no effect. */
14542 if (range_beginning == range_end)
14543 continue;
14544
43039443
JK
14545 range_beginning += base;
14546 range_end += base;
14547
01093045
DE
14548 /* A not-uncommon case of bad debug info.
14549 Don't pollute the addrmap with bad data. */
14550 if (range_beginning + baseaddr == 0
14551 && !dwarf2_per_objfile->has_section_at_zero)
14552 {
b98664d3 14553 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14554 " [in module %s]"), objfile_name (objfile));
01093045
DE
14555 continue;
14556 }
14557
5f46c5a5
JK
14558 callback (range_beginning, range_end);
14559 }
14560
14561 return 1;
14562}
14563
14564/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14565 Return 1 if the attributes are present and valid, otherwise, return 0.
14566 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14567
14568static int
14569dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14570 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14571 struct partial_symtab *ranges_pst)
14572{
518817b3 14573 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14574 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14575 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14576 SECT_OFF_TEXT (objfile));
14577 int low_set = 0;
14578 CORE_ADDR low = 0;
14579 CORE_ADDR high = 0;
14580 int retval;
14581
14582 retval = dwarf2_ranges_process (offset, cu,
14583 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14584 {
9277c30c 14585 if (ranges_pst != NULL)
3e29f34a
MR
14586 {
14587 CORE_ADDR lowpc;
14588 CORE_ADDR highpc;
14589
79748972
TT
14590 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14591 range_beginning + baseaddr)
14592 - baseaddr);
14593 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14594 range_end + baseaddr)
14595 - baseaddr);
d320c2b5
TT
14596 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14597 lowpc, highpc - 1, ranges_pst);
3e29f34a 14598 }
ff013f42 14599
43039443
JK
14600 /* FIXME: This is recording everything as a low-high
14601 segment of consecutive addresses. We should have a
14602 data structure for discontiguous block ranges
14603 instead. */
14604 if (! low_set)
14605 {
14606 low = range_beginning;
14607 high = range_end;
14608 low_set = 1;
14609 }
14610 else
14611 {
14612 if (range_beginning < low)
14613 low = range_beginning;
14614 if (range_end > high)
14615 high = range_end;
14616 }
5f46c5a5
JK
14617 });
14618 if (!retval)
14619 return 0;
43039443
JK
14620
14621 if (! low_set)
14622 /* If the first entry is an end-of-list marker, the range
14623 describes an empty scope, i.e. no instructions. */
14624 return 0;
14625
14626 if (low_return)
14627 *low_return = low;
14628 if (high_return)
14629 *high_return = high;
14630 return 1;
14631}
14632
3a2b436a
JK
14633/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14634 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14635 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14636
3a2b436a 14637static enum pc_bounds_kind
af34e669 14638dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14639 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14640 struct partial_symtab *pst)
c906108c 14641{
518817b3
SM
14642 struct dwarf2_per_objfile *dwarf2_per_objfile
14643 = cu->per_cu->dwarf2_per_objfile;
c906108c 14644 struct attribute *attr;
91da1414 14645 struct attribute *attr_high;
af34e669
DJ
14646 CORE_ADDR low = 0;
14647 CORE_ADDR high = 0;
e385593e 14648 enum pc_bounds_kind ret;
c906108c 14649
91da1414
MW
14650 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14651 if (attr_high)
af34e669 14652 {
e142c38c 14653 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14654 if (attr)
91da1414 14655 {
31aa7e4e
JB
14656 low = attr_value_as_address (attr);
14657 high = attr_value_as_address (attr_high);
14658 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14659 high += low;
91da1414 14660 }
af34e669
DJ
14661 else
14662 /* Found high w/o low attribute. */
e385593e 14663 return PC_BOUNDS_INVALID;
af34e669
DJ
14664
14665 /* Found consecutive range of addresses. */
3a2b436a 14666 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14667 }
c906108c 14668 else
af34e669 14669 {
e142c38c 14670 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14671 if (attr != NULL)
14672 {
ab435259
DE
14673 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14674 We take advantage of the fact that DW_AT_ranges does not appear
14675 in DW_TAG_compile_unit of DWO files. */
14676 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14677 unsigned int ranges_offset = (DW_UNSND (attr)
14678 + (need_ranges_base
14679 ? cu->ranges_base
14680 : 0));
2e3cf129 14681
af34e669 14682 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14683 .debug_ranges section. */
2e3cf129 14684 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14685 return PC_BOUNDS_INVALID;
43039443 14686 /* Found discontinuous range of addresses. */
3a2b436a 14687 ret = PC_BOUNDS_RANGES;
af34e669 14688 }
e385593e
JK
14689 else
14690 return PC_BOUNDS_NOT_PRESENT;
af34e669 14691 }
c906108c 14692
48fbe735 14693 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14694 if (high <= low)
e385593e 14695 return PC_BOUNDS_INVALID;
c906108c
SS
14696
14697 /* When using the GNU linker, .gnu.linkonce. sections are used to
14698 eliminate duplicate copies of functions and vtables and such.
14699 The linker will arbitrarily choose one and discard the others.
14700 The AT_*_pc values for such functions refer to local labels in
14701 these sections. If the section from that file was discarded, the
14702 labels are not in the output, so the relocs get a value of 0.
14703 If this is a discarded function, mark the pc bounds as invalid,
14704 so that GDB will ignore it. */
72dca2f5 14705 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14706 return PC_BOUNDS_INVALID;
c906108c
SS
14707
14708 *lowpc = low;
96408a79
SA
14709 if (highpc)
14710 *highpc = high;
af34e669 14711 return ret;
c906108c
SS
14712}
14713
b084d499
JB
14714/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14715 its low and high PC addresses. Do nothing if these addresses could not
14716 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14717 and HIGHPC to the high address if greater than HIGHPC. */
14718
14719static void
14720dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14721 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14722 struct dwarf2_cu *cu)
14723{
14724 CORE_ADDR low, high;
14725 struct die_info *child = die->child;
14726
e385593e 14727 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14728 {
325fac50
PA
14729 *lowpc = std::min (*lowpc, low);
14730 *highpc = std::max (*highpc, high);
b084d499
JB
14731 }
14732
14733 /* If the language does not allow nested subprograms (either inside
14734 subprograms or lexical blocks), we're done. */
14735 if (cu->language != language_ada)
14736 return;
6e70227d 14737
b084d499
JB
14738 /* Check all the children of the given DIE. If it contains nested
14739 subprograms, then check their pc bounds. Likewise, we need to
14740 check lexical blocks as well, as they may also contain subprogram
14741 definitions. */
14742 while (child && child->tag)
14743 {
14744 if (child->tag == DW_TAG_subprogram
14745 || child->tag == DW_TAG_lexical_block)
14746 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14747 child = sibling_die (child);
14748 }
14749}
14750
fae299cd
DC
14751/* Get the low and high pc's represented by the scope DIE, and store
14752 them in *LOWPC and *HIGHPC. If the correct values can't be
14753 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14754
14755static void
14756get_scope_pc_bounds (struct die_info *die,
14757 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14758 struct dwarf2_cu *cu)
14759{
14760 CORE_ADDR best_low = (CORE_ADDR) -1;
14761 CORE_ADDR best_high = (CORE_ADDR) 0;
14762 CORE_ADDR current_low, current_high;
14763
3a2b436a 14764 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14765 >= PC_BOUNDS_RANGES)
fae299cd
DC
14766 {
14767 best_low = current_low;
14768 best_high = current_high;
14769 }
14770 else
14771 {
14772 struct die_info *child = die->child;
14773
14774 while (child && child->tag)
14775 {
14776 switch (child->tag) {
14777 case DW_TAG_subprogram:
b084d499 14778 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14779 break;
14780 case DW_TAG_namespace:
f55ee35c 14781 case DW_TAG_module:
fae299cd
DC
14782 /* FIXME: carlton/2004-01-16: Should we do this for
14783 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14784 that current GCC's always emit the DIEs corresponding
14785 to definitions of methods of classes as children of a
14786 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14787 the DIEs giving the declarations, which could be
14788 anywhere). But I don't see any reason why the
14789 standards says that they have to be there. */
14790 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14791
14792 if (current_low != ((CORE_ADDR) -1))
14793 {
325fac50
PA
14794 best_low = std::min (best_low, current_low);
14795 best_high = std::max (best_high, current_high);
fae299cd
DC
14796 }
14797 break;
14798 default:
0963b4bd 14799 /* Ignore. */
fae299cd
DC
14800 break;
14801 }
14802
14803 child = sibling_die (child);
14804 }
14805 }
14806
14807 *lowpc = best_low;
14808 *highpc = best_high;
14809}
14810
801e3a5b
JB
14811/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14812 in DIE. */
380bca97 14813
801e3a5b
JB
14814static void
14815dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14816 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14817{
518817b3 14818 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14819 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14820 struct attribute *attr;
91da1414 14821 struct attribute *attr_high;
801e3a5b 14822
91da1414
MW
14823 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14824 if (attr_high)
801e3a5b 14825 {
801e3a5b
JB
14826 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14827 if (attr)
14828 {
31aa7e4e
JB
14829 CORE_ADDR low = attr_value_as_address (attr);
14830 CORE_ADDR high = attr_value_as_address (attr_high);
14831
14832 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14833 high += low;
9a619af0 14834
3e29f34a
MR
14835 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14836 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14837 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14838 }
14839 }
14840
14841 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14842 if (attr)
14843 {
ab435259
DE
14844 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14845 We take advantage of the fact that DW_AT_ranges does not appear
14846 in DW_TAG_compile_unit of DWO files. */
14847 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14848
14849 /* The value of the DW_AT_ranges attribute is the offset of the
14850 address range list in the .debug_ranges section. */
ab435259
DE
14851 unsigned long offset = (DW_UNSND (attr)
14852 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14853
2d5f09ec 14854 std::vector<blockrange> blockvec;
5f46c5a5
JK
14855 dwarf2_ranges_process (offset, cu,
14856 [&] (CORE_ADDR start, CORE_ADDR end)
14857 {
58fdfd2c
JK
14858 start += baseaddr;
14859 end += baseaddr;
5f46c5a5
JK
14860 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14861 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14862 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14863 blockvec.emplace_back (start, end);
5f46c5a5 14864 });
2d5f09ec
KB
14865
14866 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14867 }
14868}
14869
685b1105
JK
14870/* Check whether the producer field indicates either of GCC < 4.6, or the
14871 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14872
685b1105
JK
14873static void
14874check_producer (struct dwarf2_cu *cu)
60d5a603 14875{
38360086 14876 int major, minor;
60d5a603
JK
14877
14878 if (cu->producer == NULL)
14879 {
14880 /* For unknown compilers expect their behavior is DWARF version
14881 compliant.
14882
14883 GCC started to support .debug_types sections by -gdwarf-4 since
14884 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14885 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14886 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14887 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14888 }
b1ffba5a 14889 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14890 {
38360086
MW
14891 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14892 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14893 }
5230b05a 14894 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14895 {
14896 cu->producer_is_icc = true;
14897 cu->producer_is_icc_lt_14 = major < 14;
14898 }
c258c396
JD
14899 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14900 cu->producer_is_codewarrior = true;
685b1105
JK
14901 else
14902 {
14903 /* For other non-GCC compilers, expect their behavior is DWARF version
14904 compliant. */
60d5a603
JK
14905 }
14906
9068261f 14907 cu->checked_producer = true;
685b1105 14908}
ba919b58 14909
685b1105
JK
14910/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14911 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14912 during 4.6.0 experimental. */
14913
9068261f 14914static bool
685b1105
JK
14915producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14916{
14917 if (!cu->checked_producer)
14918 check_producer (cu);
14919
14920 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14921}
14922
c258c396
JD
14923
14924/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14925 with incorrect is_stmt attributes. */
14926
14927static bool
14928producer_is_codewarrior (struct dwarf2_cu *cu)
14929{
14930 if (!cu->checked_producer)
14931 check_producer (cu);
14932
14933 return cu->producer_is_codewarrior;
14934}
14935
60d5a603
JK
14936/* Return the default accessibility type if it is not overriden by
14937 DW_AT_accessibility. */
14938
14939static enum dwarf_access_attribute
14940dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14941{
14942 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14943 {
14944 /* The default DWARF 2 accessibility for members is public, the default
14945 accessibility for inheritance is private. */
14946
14947 if (die->tag != DW_TAG_inheritance)
14948 return DW_ACCESS_public;
14949 else
14950 return DW_ACCESS_private;
14951 }
14952 else
14953 {
14954 /* DWARF 3+ defines the default accessibility a different way. The same
14955 rules apply now for DW_TAG_inheritance as for the members and it only
14956 depends on the container kind. */
14957
14958 if (die->parent->tag == DW_TAG_class_type)
14959 return DW_ACCESS_private;
14960 else
14961 return DW_ACCESS_public;
14962 }
14963}
14964
74ac6d43
TT
14965/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14966 offset. If the attribute was not found return 0, otherwise return
14967 1. If it was found but could not properly be handled, set *OFFSET
14968 to 0. */
14969
14970static int
14971handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14972 LONGEST *offset)
14973{
14974 struct attribute *attr;
14975
14976 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14977 if (attr != NULL)
14978 {
14979 *offset = 0;
14980
14981 /* Note that we do not check for a section offset first here.
14982 This is because DW_AT_data_member_location is new in DWARF 4,
14983 so if we see it, we can assume that a constant form is really
14984 a constant and not a section offset. */
14985 if (attr_form_is_constant (attr))
14986 *offset = dwarf2_get_attr_constant_value (attr, 0);
14987 else if (attr_form_is_section_offset (attr))
14988 dwarf2_complex_location_expr_complaint ();
14989 else if (attr_form_is_block (attr))
14990 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14991 else
14992 dwarf2_complex_location_expr_complaint ();
14993
14994 return 1;
14995 }
14996
14997 return 0;
14998}
14999
c906108c
SS
15000/* Add an aggregate field to the field list. */
15001
15002static void
107d2387 15003dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15004 struct dwarf2_cu *cu)
6e70227d 15005{
518817b3 15006 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15007 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15008 struct nextfield *new_field;
15009 struct attribute *attr;
15010 struct field *fp;
15d034d0 15011 const char *fieldname = "";
c906108c 15012
7d0ccb61
DJ
15013 if (die->tag == DW_TAG_inheritance)
15014 {
be2daae6
TT
15015 fip->baseclasses.emplace_back ();
15016 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15017 }
15018 else
15019 {
be2daae6
TT
15020 fip->fields.emplace_back ();
15021 new_field = &fip->fields.back ();
7d0ccb61 15022 }
be2daae6 15023
c906108c
SS
15024 fip->nfields++;
15025
e142c38c 15026 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15027 if (attr)
15028 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15029 else
15030 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15031 if (new_field->accessibility != DW_ACCESS_public)
15032 fip->non_public_fields = 1;
60d5a603 15033
e142c38c 15034 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15035 if (attr)
15036 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15037 else
15038 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15039
15040 fp = &new_field->field;
a9a9bd0f 15041
e142c38c 15042 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15043 {
74ac6d43
TT
15044 LONGEST offset;
15045
a9a9bd0f 15046 /* Data member other than a C++ static data member. */
6e70227d 15047
c906108c 15048 /* Get type of field. */
e7c27a73 15049 fp->type = die_type (die, cu);
c906108c 15050
d6a843b5 15051 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15052
c906108c 15053 /* Get bit size of field (zero if none). */
e142c38c 15054 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15055 if (attr)
15056 {
15057 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15058 }
15059 else
15060 {
15061 FIELD_BITSIZE (*fp) = 0;
15062 }
15063
15064 /* Get bit offset of field. */
74ac6d43
TT
15065 if (handle_data_member_location (die, cu, &offset))
15066 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15067 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15068 if (attr)
15069 {
5e2b427d 15070 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15071 {
15072 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15073 additional bit offset from the MSB of the containing
15074 anonymous object to the MSB of the field. We don't
15075 have to do anything special since we don't need to
15076 know the size of the anonymous object. */
f41f5e61 15077 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15078 }
15079 else
15080 {
15081 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15082 MSB of the anonymous object, subtract off the number of
15083 bits from the MSB of the field to the MSB of the
15084 object, and then subtract off the number of bits of
15085 the field itself. The result is the bit offset of
15086 the LSB of the field. */
c906108c
SS
15087 int anonymous_size;
15088 int bit_offset = DW_UNSND (attr);
15089
e142c38c 15090 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15091 if (attr)
15092 {
15093 /* The size of the anonymous object containing
15094 the bit field is explicit, so use the
15095 indicated size (in bytes). */
15096 anonymous_size = DW_UNSND (attr);
15097 }
15098 else
15099 {
15100 /* The size of the anonymous object containing
15101 the bit field must be inferred from the type
15102 attribute of the data member containing the
15103 bit field. */
15104 anonymous_size = TYPE_LENGTH (fp->type);
15105 }
f41f5e61
PA
15106 SET_FIELD_BITPOS (*fp,
15107 (FIELD_BITPOS (*fp)
15108 + anonymous_size * bits_per_byte
15109 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15110 }
15111 }
da5b30da
AA
15112 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15113 if (attr != NULL)
15114 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15115 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15116
15117 /* Get name of field. */
39cbfefa
DJ
15118 fieldname = dwarf2_name (die, cu);
15119 if (fieldname == NULL)
15120 fieldname = "";
d8151005
DJ
15121
15122 /* The name is already allocated along with this objfile, so we don't
15123 need to duplicate it for the type. */
15124 fp->name = fieldname;
c906108c
SS
15125
15126 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15127 pointer or virtual base class pointer) to private. */
e142c38c 15128 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15129 {
d48cc9dd 15130 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15131 new_field->accessibility = DW_ACCESS_private;
15132 fip->non_public_fields = 1;
15133 }
15134 }
a9a9bd0f 15135 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15136 {
a9a9bd0f
DC
15137 /* C++ static member. */
15138
15139 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15140 is a declaration, but all versions of G++ as of this writing
15141 (so through at least 3.2.1) incorrectly generate
15142 DW_TAG_variable tags. */
6e70227d 15143
ff355380 15144 const char *physname;
c906108c 15145
a9a9bd0f 15146 /* Get name of field. */
39cbfefa
DJ
15147 fieldname = dwarf2_name (die, cu);
15148 if (fieldname == NULL)
c906108c
SS
15149 return;
15150
254e6b9e 15151 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15152 if (attr
15153 /* Only create a symbol if this is an external value.
15154 new_symbol checks this and puts the value in the global symbol
15155 table, which we want. If it is not external, new_symbol
15156 will try to put the value in cu->list_in_scope which is wrong. */
15157 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15158 {
15159 /* A static const member, not much different than an enum as far as
15160 we're concerned, except that we can support more types. */
15161 new_symbol (die, NULL, cu);
15162 }
15163
2df3850c 15164 /* Get physical name. */
ff355380 15165 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15166
d8151005
DJ
15167 /* The name is already allocated along with this objfile, so we don't
15168 need to duplicate it for the type. */
15169 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15170 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15171 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15172 }
15173 else if (die->tag == DW_TAG_inheritance)
15174 {
74ac6d43 15175 LONGEST offset;
d4b96c9a 15176
74ac6d43
TT
15177 /* C++ base class field. */
15178 if (handle_data_member_location (die, cu, &offset))
15179 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15180 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15181 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15182 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15183 }
2ddeaf8a
TT
15184 else if (die->tag == DW_TAG_variant_part)
15185 {
15186 /* process_structure_scope will treat this DIE as a union. */
15187 process_structure_scope (die, cu);
15188
15189 /* The variant part is relative to the start of the enclosing
15190 structure. */
15191 SET_FIELD_BITPOS (*fp, 0);
15192 fp->type = get_die_type (die, cu);
15193 fp->artificial = 1;
15194 fp->name = "<<variant>>";
c8c81635
TT
15195
15196 /* Normally a DW_TAG_variant_part won't have a size, but our
15197 representation requires one, so set it to the maximum of the
15198 child sizes. */
15199 if (TYPE_LENGTH (fp->type) == 0)
15200 {
15201 unsigned max = 0;
15202 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15203 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15204 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15205 TYPE_LENGTH (fp->type) = max;
15206 }
2ddeaf8a
TT
15207 }
15208 else
15209 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15210}
15211
883fd55a
KS
15212/* Can the type given by DIE define another type? */
15213
15214static bool
15215type_can_define_types (const struct die_info *die)
15216{
15217 switch (die->tag)
15218 {
15219 case DW_TAG_typedef:
15220 case DW_TAG_class_type:
15221 case DW_TAG_structure_type:
15222 case DW_TAG_union_type:
15223 case DW_TAG_enumeration_type:
15224 return true;
15225
15226 default:
15227 return false;
15228 }
15229}
15230
15231/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15232
15233static void
883fd55a
KS
15234dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15235 struct dwarf2_cu *cu)
6e70227d 15236{
be2daae6
TT
15237 struct decl_field fp;
15238 memset (&fp, 0, sizeof (fp));
98751a41 15239
883fd55a 15240 gdb_assert (type_can_define_types (die));
98751a41 15241
883fd55a 15242 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15243 fp.name = dwarf2_name (die, cu);
15244 fp.type = read_type_die (die, cu);
98751a41 15245
c191a687
KS
15246 /* Save accessibility. */
15247 enum dwarf_access_attribute accessibility;
15248 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15249 if (attr != NULL)
15250 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15251 else
15252 accessibility = dwarf2_default_access_attribute (die, cu);
15253 switch (accessibility)
15254 {
15255 case DW_ACCESS_public:
15256 /* The assumed value if neither private nor protected. */
15257 break;
15258 case DW_ACCESS_private:
be2daae6 15259 fp.is_private = 1;
c191a687
KS
15260 break;
15261 case DW_ACCESS_protected:
be2daae6 15262 fp.is_protected = 1;
c191a687
KS
15263 break;
15264 default:
b98664d3 15265 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15266 }
15267
883fd55a 15268 if (die->tag == DW_TAG_typedef)
be2daae6 15269 fip->typedef_field_list.push_back (fp);
883fd55a 15270 else
be2daae6 15271 fip->nested_types_list.push_back (fp);
98751a41
JK
15272}
15273
c906108c
SS
15274/* Create the vector of fields, and attach it to the type. */
15275
15276static void
fba45db2 15277dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15278 struct dwarf2_cu *cu)
c906108c
SS
15279{
15280 int nfields = fip->nfields;
15281
15282 /* Record the field count, allocate space for the array of fields,
15283 and create blank accessibility bitfields if necessary. */
15284 TYPE_NFIELDS (type) = nfields;
15285 TYPE_FIELDS (type) = (struct field *)
be2daae6 15286 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15287
b4ba55a1 15288 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15289 {
15290 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15291
15292 TYPE_FIELD_PRIVATE_BITS (type) =
15293 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15294 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15295
15296 TYPE_FIELD_PROTECTED_BITS (type) =
15297 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15298 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15299
774b6a14
TT
15300 TYPE_FIELD_IGNORE_BITS (type) =
15301 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15302 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15303 }
15304
15305 /* If the type has baseclasses, allocate and clear a bit vector for
15306 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15307 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15308 {
be2daae6 15309 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15310 unsigned char *pointer;
c906108c
SS
15311
15312 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15313 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15314 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15315 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15316 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15317 }
15318
2ddeaf8a
TT
15319 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15320 {
15321 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15322
be2daae6 15323 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15324 {
be2daae6
TT
15325 struct nextfield &field = fip->fields[index];
15326
15327 if (field.variant.is_discriminant)
2ddeaf8a 15328 di->discriminant_index = index;
be2daae6 15329 else if (field.variant.default_branch)
2ddeaf8a
TT
15330 di->default_index = index;
15331 else
be2daae6 15332 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15333 }
15334 }
15335
be2daae6
TT
15336 /* Copy the saved-up fields into the field vector. */
15337 for (int i = 0; i < nfields; ++i)
c906108c 15338 {
be2daae6
TT
15339 struct nextfield &field
15340 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15341 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15342
be2daae6
TT
15343 TYPE_FIELD (type, i) = field.field;
15344 switch (field.accessibility)
c906108c 15345 {
c5aa993b 15346 case DW_ACCESS_private:
b4ba55a1 15347 if (cu->language != language_ada)
be2daae6 15348 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15349 break;
c906108c 15350
c5aa993b 15351 case DW_ACCESS_protected:
b4ba55a1 15352 if (cu->language != language_ada)
be2daae6 15353 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15354 break;
c906108c 15355
c5aa993b
JM
15356 case DW_ACCESS_public:
15357 break;
c906108c 15358
c5aa993b
JM
15359 default:
15360 /* Unknown accessibility. Complain and treat it as public. */
15361 {
b98664d3 15362 complaint (_("unsupported accessibility %d"),
be2daae6 15363 field.accessibility);
c5aa993b
JM
15364 }
15365 break;
c906108c 15366 }
be2daae6 15367 if (i < fip->baseclasses.size ())
c906108c 15368 {
be2daae6 15369 switch (field.virtuality)
c906108c 15370 {
c5aa993b
JM
15371 case DW_VIRTUALITY_virtual:
15372 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15373 if (cu->language == language_ada)
a73c6dcd 15374 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15375 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15376 break;
c906108c
SS
15377 }
15378 }
c906108c
SS
15379 }
15380}
15381
7d27a96d
TT
15382/* Return true if this member function is a constructor, false
15383 otherwise. */
15384
15385static int
15386dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15387{
15388 const char *fieldname;
fe978cb0 15389 const char *type_name;
7d27a96d
TT
15390 int len;
15391
15392 if (die->parent == NULL)
15393 return 0;
15394
15395 if (die->parent->tag != DW_TAG_structure_type
15396 && die->parent->tag != DW_TAG_union_type
15397 && die->parent->tag != DW_TAG_class_type)
15398 return 0;
15399
15400 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15401 type_name = dwarf2_name (die->parent, cu);
15402 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15403 return 0;
15404
15405 len = strlen (fieldname);
fe978cb0
PA
15406 return (strncmp (fieldname, type_name, len) == 0
15407 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15408}
15409
c906108c
SS
15410/* Add a member function to the proper fieldlist. */
15411
15412static void
107d2387 15413dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15414 struct type *type, struct dwarf2_cu *cu)
c906108c 15415{
518817b3 15416 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15417 struct attribute *attr;
c906108c 15418 int i;
be2daae6 15419 struct fnfieldlist *flp = nullptr;
c906108c 15420 struct fn_field *fnp;
15d034d0 15421 const char *fieldname;
f792889a 15422 struct type *this_type;
60d5a603 15423 enum dwarf_access_attribute accessibility;
c906108c 15424
b4ba55a1 15425 if (cu->language == language_ada)
a73c6dcd 15426 error (_("unexpected member function in Ada type"));
b4ba55a1 15427
2df3850c 15428 /* Get name of member function. */
39cbfefa
DJ
15429 fieldname = dwarf2_name (die, cu);
15430 if (fieldname == NULL)
2df3850c 15431 return;
c906108c 15432
c906108c 15433 /* Look up member function name in fieldlist. */
be2daae6 15434 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15435 {
27bfe10e 15436 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15437 {
15438 flp = &fip->fnfieldlists[i];
15439 break;
15440 }
c906108c
SS
15441 }
15442
be2daae6
TT
15443 /* Create a new fnfieldlist if necessary. */
15444 if (flp == nullptr)
c906108c 15445 {
be2daae6
TT
15446 fip->fnfieldlists.emplace_back ();
15447 flp = &fip->fnfieldlists.back ();
c906108c 15448 flp->name = fieldname;
be2daae6 15449 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15450 }
15451
be2daae6
TT
15452 /* Create a new member function field and add it to the vector of
15453 fnfieldlists. */
15454 flp->fnfields.emplace_back ();
15455 fnp = &flp->fnfields.back ();
3da10d80
KS
15456
15457 /* Delay processing of the physname until later. */
9c37b5ae 15458 if (cu->language == language_cplus)
be2daae6
TT
15459 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15460 die, cu);
3da10d80
KS
15461 else
15462 {
1d06ead6 15463 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15464 fnp->physname = physname ? physname : "";
15465 }
15466
c906108c 15467 fnp->type = alloc_type (objfile);
f792889a
DJ
15468 this_type = read_type_die (die, cu);
15469 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15470 {
f792889a 15471 int nparams = TYPE_NFIELDS (this_type);
c906108c 15472
f792889a 15473 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15474 of the method itself (TYPE_CODE_METHOD). */
15475 smash_to_method_type (fnp->type, type,
f792889a
DJ
15476 TYPE_TARGET_TYPE (this_type),
15477 TYPE_FIELDS (this_type),
15478 TYPE_NFIELDS (this_type),
15479 TYPE_VARARGS (this_type));
c906108c
SS
15480
15481 /* Handle static member functions.
c5aa993b 15482 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15483 member functions. G++ helps GDB by marking the first
15484 parameter for non-static member functions (which is the this
15485 pointer) as artificial. We obtain this information from
15486 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15487 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15488 fnp->voffset = VOFFSET_STATIC;
15489 }
15490 else
b98664d3 15491 complaint (_("member function type missing for '%s'"),
3da10d80 15492 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15493
15494 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15495 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15496 fnp->fcontext = die_containing_type (die, cu);
c906108c 15497
3e43a32a
MS
15498 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15499 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15500
15501 /* Get accessibility. */
e142c38c 15502 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15503 if (attr)
aead7601 15504 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15505 else
15506 accessibility = dwarf2_default_access_attribute (die, cu);
15507 switch (accessibility)
c906108c 15508 {
60d5a603
JK
15509 case DW_ACCESS_private:
15510 fnp->is_private = 1;
15511 break;
15512 case DW_ACCESS_protected:
15513 fnp->is_protected = 1;
15514 break;
c906108c
SS
15515 }
15516
b02dede2 15517 /* Check for artificial methods. */
e142c38c 15518 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15519 if (attr && DW_UNSND (attr) != 0)
15520 fnp->is_artificial = 1;
15521
7d27a96d
TT
15522 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15523
0d564a31 15524 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15525 function. For older versions of GCC, this is an offset in the
15526 appropriate virtual table, as specified by DW_AT_containing_type.
15527 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15528 to the object address. */
15529
e142c38c 15530 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15531 if (attr)
8e19ed76 15532 {
aec5aa8b 15533 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15534 {
aec5aa8b
TT
15535 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15536 {
15537 /* Old-style GCC. */
15538 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15539 }
15540 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15541 || (DW_BLOCK (attr)->size > 1
15542 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15543 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15544 {
aec5aa8b
TT
15545 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15546 if ((fnp->voffset % cu->header.addr_size) != 0)
15547 dwarf2_complex_location_expr_complaint ();
15548 else
15549 fnp->voffset /= cu->header.addr_size;
15550 fnp->voffset += 2;
15551 }
15552 else
15553 dwarf2_complex_location_expr_complaint ();
15554
15555 if (!fnp->fcontext)
7e993ebf
KS
15556 {
15557 /* If there is no `this' field and no DW_AT_containing_type,
15558 we cannot actually find a base class context for the
15559 vtable! */
15560 if (TYPE_NFIELDS (this_type) == 0
15561 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15562 {
b98664d3 15563 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15564 "function \"%s\" (offset %s)"),
15565 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15566 }
15567 else
15568 {
15569 fnp->fcontext
15570 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15571 }
15572 }
aec5aa8b 15573 }
3690dd37 15574 else if (attr_form_is_section_offset (attr))
8e19ed76 15575 {
4d3c2250 15576 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15577 }
15578 else
15579 {
4d3c2250
KB
15580 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15581 fieldname);
8e19ed76 15582 }
0d564a31 15583 }
d48cc9dd
DJ
15584 else
15585 {
15586 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15587 if (attr && DW_UNSND (attr))
15588 {
15589 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15590 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15591 "but the vtable offset is not specified"),
9d8780f0 15592 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15593 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15594 TYPE_CPLUS_DYNAMIC (type) = 1;
15595 }
15596 }
c906108c
SS
15597}
15598
15599/* Create the vector of member function fields, and attach it to the type. */
15600
15601static void
fba45db2 15602dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15603 struct dwarf2_cu *cu)
c906108c 15604{
b4ba55a1 15605 if (cu->language == language_ada)
a73c6dcd 15606 error (_("unexpected member functions in Ada type"));
b4ba55a1 15607
c906108c
SS
15608 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15609 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15610 TYPE_ALLOC (type,
15611 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15612
be2daae6 15613 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15614 {
be2daae6 15615 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15616 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15617
be2daae6
TT
15618 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15619 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15620 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15621 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15622
15623 for (int k = 0; k < nf.fnfields.size (); ++k)
15624 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15625 }
15626
be2daae6 15627 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15628}
15629
1168df01
JB
15630/* Returns non-zero if NAME is the name of a vtable member in CU's
15631 language, zero otherwise. */
15632static int
15633is_vtable_name (const char *name, struct dwarf2_cu *cu)
15634{
15635 static const char vptr[] = "_vptr";
15636
9c37b5ae
TT
15637 /* Look for the C++ form of the vtable. */
15638 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15639 return 1;
15640
15641 return 0;
15642}
15643
c0dd20ea 15644/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15645 functions, with the ABI-specified layout. If TYPE describes
15646 such a structure, smash it into a member function type.
61049d3b
DJ
15647
15648 GCC shouldn't do this; it should just output pointer to member DIEs.
15649 This is GCC PR debug/28767. */
c0dd20ea 15650
0b92b5bb
TT
15651static void
15652quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15653{
09e2d7c7 15654 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15655
15656 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15657 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15658 return;
c0dd20ea
DJ
15659
15660 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15661 if (TYPE_FIELD_NAME (type, 0) == NULL
15662 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15663 || TYPE_FIELD_NAME (type, 1) == NULL
15664 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15665 return;
c0dd20ea
DJ
15666
15667 /* Find the type of the method. */
0b92b5bb 15668 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15669 if (pfn_type == NULL
15670 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15671 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15672 return;
c0dd20ea
DJ
15673
15674 /* Look for the "this" argument. */
15675 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15676 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15677 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15678 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15679 return;
c0dd20ea 15680
09e2d7c7 15681 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15682 new_type = alloc_type (objfile);
09e2d7c7 15683 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15684 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15685 TYPE_VARARGS (pfn_type));
0b92b5bb 15686 smash_to_methodptr_type (type, new_type);
c0dd20ea 15687}
1168df01 15688
2b4424c3
TT
15689/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15690 appropriate error checking and issuing complaints if there is a
15691 problem. */
15692
15693static ULONGEST
15694get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15695{
15696 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15697
15698 if (attr == nullptr)
15699 return 0;
15700
15701 if (!attr_form_is_constant (attr))
15702 {
b98664d3 15703 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15704 " - DIE at %s [in module %s]"),
15705 sect_offset_str (die->sect_off),
15706 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15707 return 0;
15708 }
15709
15710 ULONGEST align;
15711 if (attr->form == DW_FORM_sdata)
15712 {
15713 LONGEST val = DW_SND (attr);
15714 if (val < 0)
15715 {
b98664d3 15716 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15717 " - DIE at %s [in module %s]"),
15718 sect_offset_str (die->sect_off),
15719 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15720 return 0;
15721 }
15722 align = val;
15723 }
15724 else
15725 align = DW_UNSND (attr);
15726
15727 if (align == 0)
15728 {
b98664d3 15729 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15730 " - DIE at %s [in module %s]"),
15731 sect_offset_str (die->sect_off),
15732 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15733 return 0;
15734 }
15735 if ((align & (align - 1)) != 0)
15736 {
b98664d3 15737 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15738 " - DIE at %s [in module %s]"),
15739 sect_offset_str (die->sect_off),
15740 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15741 return 0;
15742 }
15743
15744 return align;
15745}
15746
15747/* If the DIE has a DW_AT_alignment attribute, use its value to set
15748 the alignment for TYPE. */
15749
15750static void
15751maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15752 struct type *type)
15753{
15754 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15755 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15756 " - DIE at %s [in module %s]"),
15757 sect_offset_str (die->sect_off),
15758 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15759}
685b1105 15760
c906108c 15761/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15762 (definition) to create a type for the structure or union. Fill in
15763 the type's name and general properties; the members will not be
83655187
DE
15764 processed until process_structure_scope. A symbol table entry for
15765 the type will also not be done until process_structure_scope (assuming
15766 the type has a name).
c906108c 15767
c767944b
DJ
15768 NOTE: we need to call these functions regardless of whether or not the
15769 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15770 structure or union. This gets the type entered into our set of
83655187 15771 user defined types. */
c906108c 15772
f792889a 15773static struct type *
134d01f1 15774read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15775{
518817b3 15776 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15777 struct type *type;
15778 struct attribute *attr;
15d034d0 15779 const char *name;
c906108c 15780
348e048f
DE
15781 /* If the definition of this type lives in .debug_types, read that type.
15782 Don't follow DW_AT_specification though, that will take us back up
15783 the chain and we want to go down. */
45e58e77 15784 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15785 if (attr)
15786 {
ac9ec31b 15787 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15788
ac9ec31b 15789 /* The type's CU may not be the same as CU.
02142a6c 15790 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15791 return set_die_type (die, type, cu);
15792 }
15793
c0dd20ea 15794 type = alloc_type (objfile);
c906108c 15795 INIT_CPLUS_SPECIFIC (type);
93311388 15796
39cbfefa
DJ
15797 name = dwarf2_name (die, cu);
15798 if (name != NULL)
c906108c 15799 {
987504bb 15800 if (cu->language == language_cplus
c44af4eb
TT
15801 || cu->language == language_d
15802 || cu->language == language_rust)
63d06c5c 15803 {
15d034d0 15804 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15805
15806 /* dwarf2_full_name might have already finished building the DIE's
15807 type. If so, there is no need to continue. */
15808 if (get_die_type (die, cu) != NULL)
15809 return get_die_type (die, cu);
15810
e86ca25f 15811 TYPE_NAME (type) = full_name;
63d06c5c
DC
15812 }
15813 else
15814 {
d8151005
DJ
15815 /* The name is already allocated along with this objfile, so
15816 we don't need to duplicate it for the type. */
e86ca25f 15817 TYPE_NAME (type) = name;
63d06c5c 15818 }
c906108c
SS
15819 }
15820
15821 if (die->tag == DW_TAG_structure_type)
15822 {
15823 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15824 }
15825 else if (die->tag == DW_TAG_union_type)
15826 {
15827 TYPE_CODE (type) = TYPE_CODE_UNION;
15828 }
2ddeaf8a
TT
15829 else if (die->tag == DW_TAG_variant_part)
15830 {
15831 TYPE_CODE (type) = TYPE_CODE_UNION;
15832 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15833 }
c906108c
SS
15834 else
15835 {
4753d33b 15836 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15837 }
15838
0cc2414c
TT
15839 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15840 TYPE_DECLARED_CLASS (type) = 1;
15841
e142c38c 15842 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15843 if (attr)
15844 {
155bfbd3
JB
15845 if (attr_form_is_constant (attr))
15846 TYPE_LENGTH (type) = DW_UNSND (attr);
15847 else
15848 {
15849 /* For the moment, dynamic type sizes are not supported
15850 by GDB's struct type. The actual size is determined
15851 on-demand when resolving the type of a given object,
15852 so set the type's length to zero for now. Otherwise,
15853 we record an expression as the length, and that expression
15854 could lead to a very large value, which could eventually
15855 lead to us trying to allocate that much memory when creating
15856 a value of that type. */
15857 TYPE_LENGTH (type) = 0;
15858 }
c906108c
SS
15859 }
15860 else
15861 {
15862 TYPE_LENGTH (type) = 0;
15863 }
15864
2b4424c3
TT
15865 maybe_set_alignment (cu, die, type);
15866
5230b05a 15867 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15868 {
5230b05a
WT
15869 /* ICC<14 does not output the required DW_AT_declaration on
15870 incomplete types, but gives them a size of zero. */
422b1cb0 15871 TYPE_STUB (type) = 1;
685b1105
JK
15872 }
15873 else
15874 TYPE_STUB_SUPPORTED (type) = 1;
15875
dc718098 15876 if (die_is_declaration (die, cu))
876cecd0 15877 TYPE_STUB (type) = 1;
a6c727b2
DJ
15878 else if (attr == NULL && die->child == NULL
15879 && producer_is_realview (cu->producer))
15880 /* RealView does not output the required DW_AT_declaration
15881 on incomplete types. */
15882 TYPE_STUB (type) = 1;
dc718098 15883
c906108c
SS
15884 /* We need to add the type field to the die immediately so we don't
15885 infinitely recurse when dealing with pointers to the structure
0963b4bd 15886 type within the structure itself. */
1c379e20 15887 set_die_type (die, type, cu);
c906108c 15888
7e314c57
JK
15889 /* set_die_type should be already done. */
15890 set_descriptive_type (type, die, cu);
15891
c767944b
DJ
15892 return type;
15893}
15894
2ddeaf8a
TT
15895/* A helper for process_structure_scope that handles a single member
15896 DIE. */
15897
15898static void
15899handle_struct_member_die (struct die_info *child_die, struct type *type,
15900 struct field_info *fi,
15901 std::vector<struct symbol *> *template_args,
15902 struct dwarf2_cu *cu)
15903{
15904 if (child_die->tag == DW_TAG_member
15905 || child_die->tag == DW_TAG_variable
15906 || child_die->tag == DW_TAG_variant_part)
15907 {
15908 /* NOTE: carlton/2002-11-05: A C++ static data member
15909 should be a DW_TAG_member that is a declaration, but
15910 all versions of G++ as of this writing (so through at
15911 least 3.2.1) incorrectly generate DW_TAG_variable
15912 tags for them instead. */
15913 dwarf2_add_field (fi, child_die, cu);
15914 }
15915 else if (child_die->tag == DW_TAG_subprogram)
15916 {
15917 /* Rust doesn't have member functions in the C++ sense.
15918 However, it does emit ordinary functions as children
15919 of a struct DIE. */
15920 if (cu->language == language_rust)
15921 read_func_scope (child_die, cu);
15922 else
15923 {
15924 /* C++ member function. */
15925 dwarf2_add_member_fn (fi, child_die, type, cu);
15926 }
15927 }
15928 else if (child_die->tag == DW_TAG_inheritance)
15929 {
15930 /* C++ base class field. */
15931 dwarf2_add_field (fi, child_die, cu);
15932 }
15933 else if (type_can_define_types (child_die))
15934 dwarf2_add_type_defn (fi, child_die, cu);
15935 else if (child_die->tag == DW_TAG_template_type_param
15936 || child_die->tag == DW_TAG_template_value_param)
15937 {
15938 struct symbol *arg = new_symbol (child_die, NULL, cu);
15939
15940 if (arg != NULL)
15941 template_args->push_back (arg);
15942 }
15943 else if (child_die->tag == DW_TAG_variant)
15944 {
15945 /* In a variant we want to get the discriminant and also add a
15946 field for our sole member child. */
15947 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15948
bde09ab7 15949 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15950 variant_child != NULL;
15951 variant_child = sibling_die (variant_child))
15952 {
15953 if (variant_child->tag == DW_TAG_member)
15954 {
15955 handle_struct_member_die (variant_child, type, fi,
15956 template_args, cu);
15957 /* Only handle the one. */
15958 break;
15959 }
15960 }
15961
15962 /* We don't handle this but we might as well report it if we see
15963 it. */
15964 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15965 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15966 " - DIE at %s [in module %s]"),
15967 sect_offset_str (child_die->sect_off),
15968 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15969
15970 /* The first field was just added, so we can stash the
15971 discriminant there. */
be2daae6 15972 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15973 if (discr == NULL)
be2daae6 15974 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15975 else
be2daae6 15976 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15977 }
15978}
15979
c767944b
DJ
15980/* Finish creating a structure or union type, including filling in
15981 its members and creating a symbol for it. */
15982
15983static void
15984process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15985{
518817b3 15986 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15987 struct die_info *child_die;
c767944b
DJ
15988 struct type *type;
15989
15990 type = get_die_type (die, cu);
15991 if (type == NULL)
15992 type = read_structure_type (die, cu);
15993
2ddeaf8a
TT
15994 /* When reading a DW_TAG_variant_part, we need to notice when we
15995 read the discriminant member, so we can record it later in the
15996 discriminant_info. */
15997 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15998 sect_offset discr_offset;
3e1d3d8c 15999 bool has_template_parameters = false;
2ddeaf8a
TT
16000
16001 if (is_variant_part)
16002 {
16003 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16004 if (discr == NULL)
16005 {
16006 /* Maybe it's a univariant form, an extension we support.
16007 In this case arrange not to check the offset. */
16008 is_variant_part = false;
16009 }
16010 else if (attr_form_is_ref (discr))
16011 {
16012 struct dwarf2_cu *target_cu = cu;
16013 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16014
16015 discr_offset = target_die->sect_off;
16016 }
16017 else
16018 {
b98664d3 16019 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16020 " - DIE at %s [in module %s]"),
16021 sect_offset_str (die->sect_off),
16022 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16023 is_variant_part = false;
16024 }
16025 }
16026
e142c38c 16027 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16028 {
16029 struct field_info fi;
2f4732b0 16030 std::vector<struct symbol *> template_args;
c906108c 16031
639d11d3 16032 child_die = die->child;
c906108c
SS
16033
16034 while (child_die && child_die->tag)
16035 {
2ddeaf8a 16036 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16037
2ddeaf8a 16038 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16039 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16040
c906108c
SS
16041 child_die = sibling_die (child_die);
16042 }
16043
34eaf542 16044 /* Attach template arguments to type. */
2f4732b0 16045 if (!template_args.empty ())
34eaf542 16046 {
3e1d3d8c 16047 has_template_parameters = true;
34eaf542 16048 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16049 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16050 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16051 = XOBNEWVEC (&objfile->objfile_obstack,
16052 struct symbol *,
16053 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16054 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16055 template_args.data (),
34eaf542
TT
16056 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16057 * sizeof (struct symbol *)));
34eaf542
TT
16058 }
16059
c906108c
SS
16060 /* Attach fields and member functions to the type. */
16061 if (fi.nfields)
e7c27a73 16062 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16063 if (!fi.fnfieldlists.empty ())
c906108c 16064 {
e7c27a73 16065 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16066
c5aa993b 16067 /* Get the type which refers to the base class (possibly this
c906108c 16068 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16069 class from the DW_AT_containing_type attribute. This use of
16070 DW_AT_containing_type is a GNU extension. */
c906108c 16071
e142c38c 16072 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16073 {
e7c27a73 16074 struct type *t = die_containing_type (die, cu);
c906108c 16075
ae6ae975 16076 set_type_vptr_basetype (type, t);
c906108c
SS
16077 if (type == t)
16078 {
c906108c
SS
16079 int i;
16080
16081 /* Our own class provides vtbl ptr. */
16082 for (i = TYPE_NFIELDS (t) - 1;
16083 i >= TYPE_N_BASECLASSES (t);
16084 --i)
16085 {
0d5cff50 16086 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16087
1168df01 16088 if (is_vtable_name (fieldname, cu))
c906108c 16089 {
ae6ae975 16090 set_type_vptr_fieldno (type, i);
c906108c
SS
16091 break;
16092 }
16093 }
16094
16095 /* Complain if virtual function table field not found. */
16096 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16097 complaint (_("virtual function table pointer "
3e43a32a 16098 "not found when defining class '%s'"),
e86ca25f 16099 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16100 }
16101 else
16102 {
ae6ae975 16103 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16104 }
16105 }
f6235d4c 16106 else if (cu->producer
61012eef 16107 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16108 {
16109 /* The IBM XLC compiler does not provide direct indication
16110 of the containing type, but the vtable pointer is
16111 always named __vfp. */
16112
16113 int i;
16114
16115 for (i = TYPE_NFIELDS (type) - 1;
16116 i >= TYPE_N_BASECLASSES (type);
16117 --i)
16118 {
16119 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16120 {
ae6ae975
DE
16121 set_type_vptr_fieldno (type, i);
16122 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16123 break;
16124 }
16125 }
16126 }
c906108c 16127 }
98751a41
JK
16128
16129 /* Copy fi.typedef_field_list linked list elements content into the
16130 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16131 if (!fi.typedef_field_list.empty ())
98751a41 16132 {
be2daae6 16133 int count = fi.typedef_field_list.size ();
98751a41 16134
a0d7a4ff 16135 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16136 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16137 = ((struct decl_field *)
be2daae6
TT
16138 TYPE_ALLOC (type,
16139 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16140 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16141
be2daae6
TT
16142 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16143 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16144 }
c767944b 16145
883fd55a
KS
16146 /* Copy fi.nested_types_list linked list elements content into the
16147 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16148 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16149 {
be2daae6 16150 int count = fi.nested_types_list.size ();
883fd55a
KS
16151
16152 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16153 TYPE_NESTED_TYPES_ARRAY (type)
16154 = ((struct decl_field *)
be2daae6
TT
16155 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16156 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16157
be2daae6
TT
16158 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16159 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16160 }
c906108c 16161 }
63d06c5c 16162
bb5ed363 16163 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16164 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16165 cu->rust_unions.push_back (type);
0b92b5bb 16166
90aeadfc
DC
16167 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16168 snapshots) has been known to create a die giving a declaration
16169 for a class that has, as a child, a die giving a definition for a
16170 nested class. So we have to process our children even if the
16171 current die is a declaration. Normally, of course, a declaration
16172 won't have any children at all. */
134d01f1 16173
ca040673
DE
16174 child_die = die->child;
16175
90aeadfc
DC
16176 while (child_die != NULL && child_die->tag)
16177 {
16178 if (child_die->tag == DW_TAG_member
16179 || child_die->tag == DW_TAG_variable
34eaf542
TT
16180 || child_die->tag == DW_TAG_inheritance
16181 || child_die->tag == DW_TAG_template_value_param
16182 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16183 {
90aeadfc 16184 /* Do nothing. */
134d01f1 16185 }
90aeadfc
DC
16186 else
16187 process_die (child_die, cu);
134d01f1 16188
90aeadfc 16189 child_die = sibling_die (child_die);
134d01f1
DJ
16190 }
16191
fa4028e9
JB
16192 /* Do not consider external references. According to the DWARF standard,
16193 these DIEs are identified by the fact that they have no byte_size
16194 attribute, and a declaration attribute. */
16195 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16196 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16197 {
16198 struct symbol *sym = new_symbol (die, type, cu);
16199
16200 if (has_template_parameters)
16201 {
a776957c
TT
16202 struct symtab *symtab;
16203 if (sym != nullptr)
16204 symtab = symbol_symtab (sym);
16205 else if (cu->line_header != nullptr)
16206 {
16207 /* Any related symtab will do. */
16208 symtab
16209 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16210 }
16211 else
16212 {
16213 symtab = nullptr;
16214 complaint (_("could not find suitable "
16215 "symtab for template parameter"
16216 " - DIE at %s [in module %s]"),
16217 sect_offset_str (die->sect_off),
16218 objfile_name (objfile));
16219 }
16220
16221 if (symtab != nullptr)
16222 {
16223 /* Make sure that the symtab is set on the new symbols.
16224 Even though they don't appear in this symtab directly,
16225 other parts of gdb assume that symbols do, and this is
16226 reasonably true. */
16227 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16228 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16229 }
3e1d3d8c
TT
16230 }
16231 }
134d01f1
DJ
16232}
16233
55426c9d
JB
16234/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16235 update TYPE using some information only available in DIE's children. */
16236
16237static void
16238update_enumeration_type_from_children (struct die_info *die,
16239 struct type *type,
16240 struct dwarf2_cu *cu)
16241{
60f7655a 16242 struct die_info *child_die;
55426c9d
JB
16243 int unsigned_enum = 1;
16244 int flag_enum = 1;
16245 ULONGEST mask = 0;
55426c9d 16246
8268c778 16247 auto_obstack obstack;
55426c9d 16248
60f7655a
DE
16249 for (child_die = die->child;
16250 child_die != NULL && child_die->tag;
16251 child_die = sibling_die (child_die))
55426c9d
JB
16252 {
16253 struct attribute *attr;
16254 LONGEST value;
16255 const gdb_byte *bytes;
16256 struct dwarf2_locexpr_baton *baton;
16257 const char *name;
60f7655a 16258
55426c9d
JB
16259 if (child_die->tag != DW_TAG_enumerator)
16260 continue;
16261
16262 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16263 if (attr == NULL)
16264 continue;
16265
16266 name = dwarf2_name (child_die, cu);
16267 if (name == NULL)
16268 name = "<anonymous enumerator>";
16269
16270 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16271 &value, &bytes, &baton);
16272 if (value < 0)
16273 {
16274 unsigned_enum = 0;
16275 flag_enum = 0;
16276 }
16277 else if ((mask & value) != 0)
16278 flag_enum = 0;
16279 else
16280 mask |= value;
16281
16282 /* If we already know that the enum type is neither unsigned, nor
16283 a flag type, no need to look at the rest of the enumerates. */
16284 if (!unsigned_enum && !flag_enum)
16285 break;
55426c9d
JB
16286 }
16287
16288 if (unsigned_enum)
16289 TYPE_UNSIGNED (type) = 1;
16290 if (flag_enum)
16291 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16292}
16293
134d01f1
DJ
16294/* Given a DW_AT_enumeration_type die, set its type. We do not
16295 complete the type's fields yet, or create any symbols. */
c906108c 16296
f792889a 16297static struct type *
134d01f1 16298read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16299{
518817b3 16300 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16301 struct type *type;
c906108c 16302 struct attribute *attr;
0114d602 16303 const char *name;
134d01f1 16304
348e048f
DE
16305 /* If the definition of this type lives in .debug_types, read that type.
16306 Don't follow DW_AT_specification though, that will take us back up
16307 the chain and we want to go down. */
45e58e77 16308 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16309 if (attr)
16310 {
ac9ec31b 16311 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16312
ac9ec31b 16313 /* The type's CU may not be the same as CU.
02142a6c 16314 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16315 return set_die_type (die, type, cu);
16316 }
16317
c906108c
SS
16318 type = alloc_type (objfile);
16319
16320 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16321 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16322 if (name != NULL)
e86ca25f 16323 TYPE_NAME (type) = name;
c906108c 16324
0626fc76
TT
16325 attr = dwarf2_attr (die, DW_AT_type, cu);
16326 if (attr != NULL)
16327 {
16328 struct type *underlying_type = die_type (die, cu);
16329
16330 TYPE_TARGET_TYPE (type) = underlying_type;
16331 }
16332
e142c38c 16333 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16334 if (attr)
16335 {
16336 TYPE_LENGTH (type) = DW_UNSND (attr);
16337 }
16338 else
16339 {
16340 TYPE_LENGTH (type) = 0;
16341 }
16342
2b4424c3
TT
16343 maybe_set_alignment (cu, die, type);
16344
137033e9
JB
16345 /* The enumeration DIE can be incomplete. In Ada, any type can be
16346 declared as private in the package spec, and then defined only
16347 inside the package body. Such types are known as Taft Amendment
16348 Types. When another package uses such a type, an incomplete DIE
16349 may be generated by the compiler. */
02eb380e 16350 if (die_is_declaration (die, cu))
876cecd0 16351 TYPE_STUB (type) = 1;
02eb380e 16352
0626fc76
TT
16353 /* Finish the creation of this type by using the enum's children.
16354 We must call this even when the underlying type has been provided
16355 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16356 update_enumeration_type_from_children (die, type, cu);
16357
0626fc76
TT
16358 /* If this type has an underlying type that is not a stub, then we
16359 may use its attributes. We always use the "unsigned" attribute
16360 in this situation, because ordinarily we guess whether the type
16361 is unsigned -- but the guess can be wrong and the underlying type
16362 can tell us the reality. However, we defer to a local size
16363 attribute if one exists, because this lets the compiler override
16364 the underlying type if needed. */
16365 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16366 {
16367 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16368 if (TYPE_LENGTH (type) == 0)
16369 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16370 if (TYPE_RAW_ALIGN (type) == 0
16371 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16372 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16373 }
16374
3d567982
TT
16375 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16376
f792889a 16377 return set_die_type (die, type, cu);
134d01f1
DJ
16378}
16379
16380/* Given a pointer to a die which begins an enumeration, process all
16381 the dies that define the members of the enumeration, and create the
16382 symbol for the enumeration type.
16383
16384 NOTE: We reverse the order of the element list. */
16385
16386static void
16387process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16388{
f792889a 16389 struct type *this_type;
134d01f1 16390
f792889a
DJ
16391 this_type = get_die_type (die, cu);
16392 if (this_type == NULL)
16393 this_type = read_enumeration_type (die, cu);
9dc481d3 16394
639d11d3 16395 if (die->child != NULL)
c906108c 16396 {
9dc481d3
DE
16397 struct die_info *child_die;
16398 struct symbol *sym;
16399 struct field *fields = NULL;
16400 int num_fields = 0;
15d034d0 16401 const char *name;
9dc481d3 16402
639d11d3 16403 child_die = die->child;
c906108c
SS
16404 while (child_die && child_die->tag)
16405 {
16406 if (child_die->tag != DW_TAG_enumerator)
16407 {
e7c27a73 16408 process_die (child_die, cu);
c906108c
SS
16409 }
16410 else
16411 {
39cbfefa
DJ
16412 name = dwarf2_name (child_die, cu);
16413 if (name)
c906108c 16414 {
f792889a 16415 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16416
16417 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16418 {
16419 fields = (struct field *)
16420 xrealloc (fields,
16421 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16422 * sizeof (struct field));
c906108c
SS
16423 }
16424
3567439c 16425 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16426 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16427 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16428 FIELD_BITSIZE (fields[num_fields]) = 0;
16429
16430 num_fields++;
16431 }
16432 }
16433
16434 child_die = sibling_die (child_die);
16435 }
16436
16437 if (num_fields)
16438 {
f792889a
DJ
16439 TYPE_NFIELDS (this_type) = num_fields;
16440 TYPE_FIELDS (this_type) = (struct field *)
16441 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16442 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16443 sizeof (struct field) * num_fields);
b8c9b27d 16444 xfree (fields);
c906108c 16445 }
c906108c 16446 }
134d01f1 16447
6c83ed52
TT
16448 /* If we are reading an enum from a .debug_types unit, and the enum
16449 is a declaration, and the enum is not the signatured type in the
16450 unit, then we do not want to add a symbol for it. Adding a
16451 symbol would in some cases obscure the true definition of the
16452 enum, giving users an incomplete type when the definition is
16453 actually available. Note that we do not want to do this for all
16454 enums which are just declarations, because C++0x allows forward
16455 enum declarations. */
3019eac3 16456 if (cu->per_cu->is_debug_types
6c83ed52
TT
16457 && die_is_declaration (die, cu))
16458 {
52dc124a 16459 struct signatured_type *sig_type;
6c83ed52 16460
c0f78cd4 16461 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16462 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16463 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16464 return;
16465 }
16466
f792889a 16467 new_symbol (die, this_type, cu);
c906108c
SS
16468}
16469
16470/* Extract all information from a DW_TAG_array_type DIE and put it in
16471 the DIE's type field. For now, this only handles one dimensional
16472 arrays. */
16473
f792889a 16474static struct type *
e7c27a73 16475read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16476{
518817b3 16477 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16478 struct die_info *child_die;
7e314c57 16479 struct type *type;
c906108c 16480 struct type *element_type, *range_type, *index_type;
c906108c 16481 struct attribute *attr;
15d034d0 16482 const char *name;
a405673c 16483 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16484 unsigned int bit_stride = 0;
c906108c 16485
e7c27a73 16486 element_type = die_type (die, cu);
c906108c 16487
7e314c57
JK
16488 /* The die_type call above may have already set the type for this DIE. */
16489 type = get_die_type (die, cu);
16490 if (type)
16491 return type;
16492
dc53a7ad
JB
16493 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16494 if (attr != NULL)
a405673c
JB
16495 {
16496 int stride_ok;
9a49df9d
AB
16497 struct type *prop_type
16498 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16499
16500 byte_stride_prop
16501 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16502 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16503 prop_type);
a405673c
JB
16504 if (!stride_ok)
16505 {
b98664d3 16506 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16507 " - DIE at %s [in module %s]"),
16508 sect_offset_str (die->sect_off),
518817b3 16509 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16510 /* Ignore this attribute. We will likely not be able to print
16511 arrays of this type correctly, but there is little we can do
16512 to help if we cannot read the attribute's value. */
16513 byte_stride_prop = NULL;
16514 }
16515 }
dc53a7ad
JB
16516
16517 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16518 if (attr != NULL)
16519 bit_stride = DW_UNSND (attr);
16520
c906108c
SS
16521 /* Irix 6.2 native cc creates array types without children for
16522 arrays with unspecified length. */
639d11d3 16523 if (die->child == NULL)
c906108c 16524 {
46bf5051 16525 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16526 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16527 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16528 byte_stride_prop, bit_stride);
f792889a 16529 return set_die_type (die, type, cu);
c906108c
SS
16530 }
16531
791afaa2 16532 std::vector<struct type *> range_types;
639d11d3 16533 child_die = die->child;
c906108c
SS
16534 while (child_die && child_die->tag)
16535 {
16536 if (child_die->tag == DW_TAG_subrange_type)
16537 {
f792889a 16538 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16539
f792889a 16540 if (child_type != NULL)
a02abb62 16541 {
0963b4bd
MS
16542 /* The range type was succesfully read. Save it for the
16543 array type creation. */
791afaa2 16544 range_types.push_back (child_type);
a02abb62 16545 }
c906108c
SS
16546 }
16547 child_die = sibling_die (child_die);
16548 }
16549
16550 /* Dwarf2 dimensions are output from left to right, create the
16551 necessary array types in backwards order. */
7ca2d3a3 16552
c906108c 16553 type = element_type;
7ca2d3a3
DL
16554
16555 if (read_array_order (die, cu) == DW_ORD_col_major)
16556 {
16557 int i = 0;
9a619af0 16558
791afaa2 16559 while (i < range_types.size ())
dc53a7ad 16560 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16561 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16562 }
16563 else
16564 {
791afaa2 16565 size_t ndim = range_types.size ();
7ca2d3a3 16566 while (ndim-- > 0)
dc53a7ad 16567 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16568 byte_stride_prop, bit_stride);
7ca2d3a3 16569 }
c906108c 16570
f5f8a009
EZ
16571 /* Understand Dwarf2 support for vector types (like they occur on
16572 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16573 array type. This is not part of the Dwarf2/3 standard yet, but a
16574 custom vendor extension. The main difference between a regular
16575 array and the vector variant is that vectors are passed by value
16576 to functions. */
e142c38c 16577 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16578 if (attr)
ea37ba09 16579 make_vector_type (type);
f5f8a009 16580
dbc98a8b
KW
16581 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16582 implementation may choose to implement triple vectors using this
16583 attribute. */
16584 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16585 if (attr)
16586 {
16587 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16588 TYPE_LENGTH (type) = DW_UNSND (attr);
16589 else
b98664d3 16590 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16591 "than the total size of elements"));
dbc98a8b
KW
16592 }
16593
39cbfefa
DJ
16594 name = dwarf2_name (die, cu);
16595 if (name)
16596 TYPE_NAME (type) = name;
6e70227d 16597
2b4424c3
TT
16598 maybe_set_alignment (cu, die, type);
16599
0963b4bd 16600 /* Install the type in the die. */
7e314c57
JK
16601 set_die_type (die, type, cu);
16602
16603 /* set_die_type should be already done. */
b4ba55a1
JB
16604 set_descriptive_type (type, die, cu);
16605
7e314c57 16606 return type;
c906108c
SS
16607}
16608
7ca2d3a3 16609static enum dwarf_array_dim_ordering
6e70227d 16610read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16611{
16612 struct attribute *attr;
16613
16614 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16615
aead7601
SM
16616 if (attr)
16617 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16618
0963b4bd
MS
16619 /* GNU F77 is a special case, as at 08/2004 array type info is the
16620 opposite order to the dwarf2 specification, but data is still
16621 laid out as per normal fortran.
7ca2d3a3 16622
0963b4bd
MS
16623 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16624 version checking. */
7ca2d3a3 16625
905e0470
PM
16626 if (cu->language == language_fortran
16627 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16628 {
16629 return DW_ORD_row_major;
16630 }
16631
6e70227d 16632 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16633 {
16634 case array_column_major:
16635 return DW_ORD_col_major;
16636 case array_row_major:
16637 default:
16638 return DW_ORD_row_major;
16639 };
16640}
16641
72019c9c 16642/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16643 the DIE's type field. */
72019c9c 16644
f792889a 16645static struct type *
72019c9c
GM
16646read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16647{
7e314c57
JK
16648 struct type *domain_type, *set_type;
16649 struct attribute *attr;
f792889a 16650
7e314c57
JK
16651 domain_type = die_type (die, cu);
16652
16653 /* The die_type call above may have already set the type for this DIE. */
16654 set_type = get_die_type (die, cu);
16655 if (set_type)
16656 return set_type;
16657
16658 set_type = create_set_type (NULL, domain_type);
16659
16660 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16661 if (attr)
16662 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16663
2b4424c3
TT
16664 maybe_set_alignment (cu, die, set_type);
16665
f792889a 16666 return set_die_type (die, set_type, cu);
72019c9c 16667}
7ca2d3a3 16668
0971de02
TT
16669/* A helper for read_common_block that creates a locexpr baton.
16670 SYM is the symbol which we are marking as computed.
16671 COMMON_DIE is the DIE for the common block.
16672 COMMON_LOC is the location expression attribute for the common
16673 block itself.
16674 MEMBER_LOC is the location expression attribute for the particular
16675 member of the common block that we are processing.
16676 CU is the CU from which the above come. */
16677
16678static void
16679mark_common_block_symbol_computed (struct symbol *sym,
16680 struct die_info *common_die,
16681 struct attribute *common_loc,
16682 struct attribute *member_loc,
16683 struct dwarf2_cu *cu)
16684{
518817b3
SM
16685 struct dwarf2_per_objfile *dwarf2_per_objfile
16686 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16687 struct objfile *objfile = dwarf2_per_objfile->objfile;
16688 struct dwarf2_locexpr_baton *baton;
16689 gdb_byte *ptr;
16690 unsigned int cu_off;
16691 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16692 LONGEST offset = 0;
16693
16694 gdb_assert (common_loc && member_loc);
16695 gdb_assert (attr_form_is_block (common_loc));
16696 gdb_assert (attr_form_is_block (member_loc)
16697 || attr_form_is_constant (member_loc));
16698
8d749320 16699 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16700 baton->per_cu = cu->per_cu;
16701 gdb_assert (baton->per_cu);
16702
16703 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16704
16705 if (attr_form_is_constant (member_loc))
16706 {
16707 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16708 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16709 }
16710 else
16711 baton->size += DW_BLOCK (member_loc)->size;
16712
224c3ddb 16713 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16714 baton->data = ptr;
16715
16716 *ptr++ = DW_OP_call4;
9c541725 16717 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16718 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16719 ptr += 4;
16720
16721 if (attr_form_is_constant (member_loc))
16722 {
16723 *ptr++ = DW_OP_addr;
16724 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16725 ptr += cu->header.addr_size;
16726 }
16727 else
16728 {
16729 /* We have to copy the data here, because DW_OP_call4 will only
16730 use a DW_AT_location attribute. */
16731 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16732 ptr += DW_BLOCK (member_loc)->size;
16733 }
16734
16735 *ptr++ = DW_OP_plus;
16736 gdb_assert (ptr - baton->data == baton->size);
16737
0971de02 16738 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16739 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16740}
16741
4357ac6c
TT
16742/* Create appropriate locally-scoped variables for all the
16743 DW_TAG_common_block entries. Also create a struct common_block
16744 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16745 is used to sepate the common blocks name namespace from regular
16746 variable names. */
c906108c
SS
16747
16748static void
e7c27a73 16749read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16750{
0971de02
TT
16751 struct attribute *attr;
16752
16753 attr = dwarf2_attr (die, DW_AT_location, cu);
16754 if (attr)
16755 {
16756 /* Support the .debug_loc offsets. */
16757 if (attr_form_is_block (attr))
16758 {
16759 /* Ok. */
16760 }
16761 else if (attr_form_is_section_offset (attr))
16762 {
16763 dwarf2_complex_location_expr_complaint ();
16764 attr = NULL;
16765 }
16766 else
16767 {
16768 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16769 "common block member");
16770 attr = NULL;
16771 }
16772 }
16773
639d11d3 16774 if (die->child != NULL)
c906108c 16775 {
518817b3 16776 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16777 struct die_info *child_die;
16778 size_t n_entries = 0, size;
16779 struct common_block *common_block;
16780 struct symbol *sym;
74ac6d43 16781
4357ac6c
TT
16782 for (child_die = die->child;
16783 child_die && child_die->tag;
16784 child_die = sibling_die (child_die))
16785 ++n_entries;
16786
16787 size = (sizeof (struct common_block)
16788 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16789 common_block
16790 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16791 size);
4357ac6c
TT
16792 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16793 common_block->n_entries = 0;
16794
16795 for (child_die = die->child;
16796 child_die && child_die->tag;
16797 child_die = sibling_die (child_die))
16798 {
16799 /* Create the symbol in the DW_TAG_common_block block in the current
16800 symbol scope. */
e7c27a73 16801 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16802 if (sym != NULL)
16803 {
16804 struct attribute *member_loc;
16805
16806 common_block->contents[common_block->n_entries++] = sym;
16807
16808 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16809 cu);
16810 if (member_loc)
16811 {
16812 /* GDB has handled this for a long time, but it is
16813 not specified by DWARF. It seems to have been
16814 emitted by gfortran at least as recently as:
16815 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16816 complaint (_("Variable in common block has "
0971de02 16817 "DW_AT_data_member_location "
9d8780f0
SM
16818 "- DIE at %s [in module %s]"),
16819 sect_offset_str (child_die->sect_off),
518817b3 16820 objfile_name (objfile));
0971de02
TT
16821
16822 if (attr_form_is_section_offset (member_loc))
16823 dwarf2_complex_location_expr_complaint ();
16824 else if (attr_form_is_constant (member_loc)
16825 || attr_form_is_block (member_loc))
16826 {
16827 if (attr)
16828 mark_common_block_symbol_computed (sym, die, attr,
16829 member_loc, cu);
16830 }
16831 else
16832 dwarf2_complex_location_expr_complaint ();
16833 }
16834 }
c906108c 16835 }
4357ac6c
TT
16836
16837 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16838 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16839 }
16840}
16841
0114d602 16842/* Create a type for a C++ namespace. */
d9fa45fe 16843
0114d602
DJ
16844static struct type *
16845read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16846{
518817b3 16847 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16848 const char *previous_prefix, *name;
9219021c 16849 int is_anonymous;
0114d602
DJ
16850 struct type *type;
16851
16852 /* For extensions, reuse the type of the original namespace. */
16853 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16854 {
16855 struct die_info *ext_die;
16856 struct dwarf2_cu *ext_cu = cu;
9a619af0 16857
0114d602
DJ
16858 ext_die = dwarf2_extension (die, &ext_cu);
16859 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16860
16861 /* EXT_CU may not be the same as CU.
02142a6c 16862 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16863 return set_die_type (die, type, cu);
16864 }
9219021c 16865
e142c38c 16866 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16867
16868 /* Now build the name of the current namespace. */
16869
0114d602
DJ
16870 previous_prefix = determine_prefix (die, cu);
16871 if (previous_prefix[0] != '\0')
16872 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16873 previous_prefix, name, 0, cu);
0114d602
DJ
16874
16875 /* Create the type. */
19f392bc 16876 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16877
60531b24 16878 return set_die_type (die, type, cu);
0114d602
DJ
16879}
16880
22cee43f 16881/* Read a namespace scope. */
0114d602
DJ
16882
16883static void
16884read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16885{
518817b3 16886 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16887 int is_anonymous;
9219021c 16888
5c4e30ca
DC
16889 /* Add a symbol associated to this if we haven't seen the namespace
16890 before. Also, add a using directive if it's an anonymous
16891 namespace. */
9219021c 16892
f2f0e013 16893 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16894 {
16895 struct type *type;
16896
0114d602 16897 type = read_type_die (die, cu);
e7c27a73 16898 new_symbol (die, type, cu);
5c4e30ca 16899
e8e80198 16900 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16901 if (is_anonymous)
0114d602
DJ
16902 {
16903 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16904
eb1e02fd 16905 std::vector<const char *> excludes;
804d2729 16906 add_using_directive (using_directives (cu),
22cee43f 16907 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16908 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16909 }
5c4e30ca 16910 }
9219021c 16911
639d11d3 16912 if (die->child != NULL)
d9fa45fe 16913 {
639d11d3 16914 struct die_info *child_die = die->child;
6e70227d 16915
d9fa45fe
DC
16916 while (child_die && child_die->tag)
16917 {
e7c27a73 16918 process_die (child_die, cu);
d9fa45fe
DC
16919 child_die = sibling_die (child_die);
16920 }
16921 }
38d518c9
EZ
16922}
16923
f55ee35c
JK
16924/* Read a Fortran module as type. This DIE can be only a declaration used for
16925 imported module. Still we need that type as local Fortran "use ... only"
16926 declaration imports depend on the created type in determine_prefix. */
16927
16928static struct type *
16929read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16930{
518817b3 16931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16932 const char *module_name;
f55ee35c
JK
16933 struct type *type;
16934
16935 module_name = dwarf2_name (die, cu);
19f392bc 16936 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16937
f55ee35c
JK
16938 return set_die_type (die, type, cu);
16939}
16940
5d7cb8df
JK
16941/* Read a Fortran module. */
16942
16943static void
16944read_module (struct die_info *die, struct dwarf2_cu *cu)
16945{
16946 struct die_info *child_die = die->child;
530e8392
KB
16947 struct type *type;
16948
16949 type = read_type_die (die, cu);
16950 new_symbol (die, type, cu);
5d7cb8df 16951
5d7cb8df
JK
16952 while (child_die && child_die->tag)
16953 {
16954 process_die (child_die, cu);
16955 child_die = sibling_die (child_die);
16956 }
16957}
16958
38d518c9
EZ
16959/* Return the name of the namespace represented by DIE. Set
16960 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16961 namespace. */
16962
16963static const char *
e142c38c 16964namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16965{
16966 struct die_info *current_die;
16967 const char *name = NULL;
16968
16969 /* Loop through the extensions until we find a name. */
16970
16971 for (current_die = die;
16972 current_die != NULL;
f2f0e013 16973 current_die = dwarf2_extension (die, &cu))
38d518c9 16974 {
96553a0c
DE
16975 /* We don't use dwarf2_name here so that we can detect the absence
16976 of a name -> anonymous namespace. */
7d45c7c3 16977 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16978
38d518c9
EZ
16979 if (name != NULL)
16980 break;
16981 }
16982
16983 /* Is it an anonymous namespace? */
16984
16985 *is_anonymous = (name == NULL);
16986 if (*is_anonymous)
2b1dbab0 16987 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16988
16989 return name;
d9fa45fe
DC
16990}
16991
c906108c
SS
16992/* Extract all information from a DW_TAG_pointer_type DIE and add to
16993 the user defined type vector. */
16994
f792889a 16995static struct type *
e7c27a73 16996read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16997{
518817b3
SM
16998 struct gdbarch *gdbarch
16999 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17000 struct comp_unit_head *cu_header = &cu->header;
c906108c 17001 struct type *type;
8b2dbe47
KB
17002 struct attribute *attr_byte_size;
17003 struct attribute *attr_address_class;
17004 int byte_size, addr_class;
7e314c57
JK
17005 struct type *target_type;
17006
17007 target_type = die_type (die, cu);
c906108c 17008
7e314c57
JK
17009 /* The die_type call above may have already set the type for this DIE. */
17010 type = get_die_type (die, cu);
17011 if (type)
17012 return type;
17013
17014 type = lookup_pointer_type (target_type);
8b2dbe47 17015
e142c38c 17016 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17017 if (attr_byte_size)
17018 byte_size = DW_UNSND (attr_byte_size);
c906108c 17019 else
8b2dbe47
KB
17020 byte_size = cu_header->addr_size;
17021
e142c38c 17022 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17023 if (attr_address_class)
17024 addr_class = DW_UNSND (attr_address_class);
17025 else
17026 addr_class = DW_ADDR_none;
17027
2b4424c3
TT
17028 ULONGEST alignment = get_alignment (cu, die);
17029
17030 /* If the pointer size, alignment, or address class is different
17031 than the default, create a type variant marked as such and set
17032 the length accordingly. */
17033 if (TYPE_LENGTH (type) != byte_size
17034 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17035 && alignment != TYPE_RAW_ALIGN (type))
17036 || addr_class != DW_ADDR_none)
c906108c 17037 {
5e2b427d 17038 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17039 {
17040 int type_flags;
17041
849957d9 17042 type_flags = gdbarch_address_class_type_flags
5e2b427d 17043 (gdbarch, byte_size, addr_class);
876cecd0
TT
17044 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17045 == 0);
8b2dbe47
KB
17046 type = make_type_with_address_space (type, type_flags);
17047 }
17048 else if (TYPE_LENGTH (type) != byte_size)
17049 {
b98664d3 17050 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17051 }
2b4424c3
TT
17052 else if (TYPE_RAW_ALIGN (type) != alignment)
17053 {
b98664d3 17054 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17055 " - DIE at %s [in module %s]"),
17056 sect_offset_str (die->sect_off),
17057 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17058 }
6e70227d 17059 else
9a619af0
MS
17060 {
17061 /* Should we also complain about unhandled address classes? */
17062 }
c906108c 17063 }
8b2dbe47
KB
17064
17065 TYPE_LENGTH (type) = byte_size;
2b4424c3 17066 set_type_align (type, alignment);
f792889a 17067 return set_die_type (die, type, cu);
c906108c
SS
17068}
17069
17070/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17071 the user defined type vector. */
17072
f792889a 17073static struct type *
e7c27a73 17074read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17075{
17076 struct type *type;
17077 struct type *to_type;
17078 struct type *domain;
17079
e7c27a73
DJ
17080 to_type = die_type (die, cu);
17081 domain = die_containing_type (die, cu);
0d5de010 17082
7e314c57
JK
17083 /* The calls above may have already set the type for this DIE. */
17084 type = get_die_type (die, cu);
17085 if (type)
17086 return type;
17087
0d5de010
DJ
17088 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17089 type = lookup_methodptr_type (to_type);
7078baeb
TT
17090 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17091 {
518817b3
SM
17092 struct type *new_type
17093 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17094
17095 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17096 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17097 TYPE_VARARGS (to_type));
17098 type = lookup_methodptr_type (new_type);
17099 }
0d5de010
DJ
17100 else
17101 type = lookup_memberptr_type (to_type, domain);
c906108c 17102
f792889a 17103 return set_die_type (die, type, cu);
c906108c
SS
17104}
17105
4297a3f0 17106/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17107 the user defined type vector. */
17108
f792889a 17109static struct type *
4297a3f0
AV
17110read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17111 enum type_code refcode)
c906108c 17112{
e7c27a73 17113 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17114 struct type *type, *target_type;
c906108c
SS
17115 struct attribute *attr;
17116
4297a3f0
AV
17117 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17118
7e314c57
JK
17119 target_type = die_type (die, cu);
17120
17121 /* The die_type call above may have already set the type for this DIE. */
17122 type = get_die_type (die, cu);
17123 if (type)
17124 return type;
17125
4297a3f0 17126 type = lookup_reference_type (target_type, refcode);
e142c38c 17127 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17128 if (attr)
17129 {
17130 TYPE_LENGTH (type) = DW_UNSND (attr);
17131 }
17132 else
17133 {
107d2387 17134 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17135 }
2b4424c3 17136 maybe_set_alignment (cu, die, type);
f792889a 17137 return set_die_type (die, type, cu);
c906108c
SS
17138}
17139
cf363f18
MW
17140/* Add the given cv-qualifiers to the element type of the array. GCC
17141 outputs DWARF type qualifiers that apply to an array, not the
17142 element type. But GDB relies on the array element type to carry
17143 the cv-qualifiers. This mimics section 6.7.3 of the C99
17144 specification. */
17145
17146static struct type *
17147add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17148 struct type *base_type, int cnst, int voltl)
17149{
17150 struct type *el_type, *inner_array;
17151
17152 base_type = copy_type (base_type);
17153 inner_array = base_type;
17154
17155 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17156 {
17157 TYPE_TARGET_TYPE (inner_array) =
17158 copy_type (TYPE_TARGET_TYPE (inner_array));
17159 inner_array = TYPE_TARGET_TYPE (inner_array);
17160 }
17161
17162 el_type = TYPE_TARGET_TYPE (inner_array);
17163 cnst |= TYPE_CONST (el_type);
17164 voltl |= TYPE_VOLATILE (el_type);
17165 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17166
17167 return set_die_type (die, base_type, cu);
17168}
17169
f792889a 17170static struct type *
e7c27a73 17171read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17172{
f792889a 17173 struct type *base_type, *cv_type;
c906108c 17174
e7c27a73 17175 base_type = die_type (die, cu);
7e314c57
JK
17176
17177 /* The die_type call above may have already set the type for this DIE. */
17178 cv_type = get_die_type (die, cu);
17179 if (cv_type)
17180 return cv_type;
17181
2f608a3a
KW
17182 /* In case the const qualifier is applied to an array type, the element type
17183 is so qualified, not the array type (section 6.7.3 of C99). */
17184 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17185 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17186
f792889a
DJ
17187 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17188 return set_die_type (die, cv_type, cu);
c906108c
SS
17189}
17190
f792889a 17191static struct type *
e7c27a73 17192read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17193{
f792889a 17194 struct type *base_type, *cv_type;
c906108c 17195
e7c27a73 17196 base_type = die_type (die, cu);
7e314c57
JK
17197
17198 /* The die_type call above may have already set the type for this DIE. */
17199 cv_type = get_die_type (die, cu);
17200 if (cv_type)
17201 return cv_type;
17202
cf363f18
MW
17203 /* In case the volatile qualifier is applied to an array type, the
17204 element type is so qualified, not the array type (section 6.7.3
17205 of C99). */
17206 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17207 return add_array_cv_type (die, cu, base_type, 0, 1);
17208
f792889a
DJ
17209 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17210 return set_die_type (die, cv_type, cu);
c906108c
SS
17211}
17212
06d66ee9
TT
17213/* Handle DW_TAG_restrict_type. */
17214
17215static struct type *
17216read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17217{
17218 struct type *base_type, *cv_type;
17219
17220 base_type = die_type (die, cu);
17221
17222 /* The die_type call above may have already set the type for this DIE. */
17223 cv_type = get_die_type (die, cu);
17224 if (cv_type)
17225 return cv_type;
17226
17227 cv_type = make_restrict_type (base_type);
17228 return set_die_type (die, cv_type, cu);
17229}
17230
a2c2acaf
MW
17231/* Handle DW_TAG_atomic_type. */
17232
17233static struct type *
17234read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17235{
17236 struct type *base_type, *cv_type;
17237
17238 base_type = die_type (die, cu);
17239
17240 /* The die_type call above may have already set the type for this DIE. */
17241 cv_type = get_die_type (die, cu);
17242 if (cv_type)
17243 return cv_type;
17244
17245 cv_type = make_atomic_type (base_type);
17246 return set_die_type (die, cv_type, cu);
17247}
17248
c906108c
SS
17249/* Extract all information from a DW_TAG_string_type DIE and add to
17250 the user defined type vector. It isn't really a user defined type,
17251 but it behaves like one, with other DIE's using an AT_user_def_type
17252 attribute to reference it. */
17253
f792889a 17254static struct type *
e7c27a73 17255read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17256{
518817b3 17257 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17258 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17259 struct type *type, *range_type, *index_type, *char_type;
17260 struct attribute *attr;
17261 unsigned int length;
17262
e142c38c 17263 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17264 if (attr)
17265 {
17266 length = DW_UNSND (attr);
17267 }
17268 else
17269 {
0963b4bd 17270 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17271 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17272 if (attr)
17273 {
17274 length = DW_UNSND (attr);
17275 }
17276 else
17277 {
17278 length = 1;
17279 }
c906108c 17280 }
6ccb9162 17281
46bf5051 17282 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17283 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17284 char_type = language_string_char_type (cu->language_defn, gdbarch);
17285 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17286
f792889a 17287 return set_die_type (die, type, cu);
c906108c
SS
17288}
17289
4d804846
JB
17290/* Assuming that DIE corresponds to a function, returns nonzero
17291 if the function is prototyped. */
17292
17293static int
17294prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17295{
17296 struct attribute *attr;
17297
17298 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17299 if (attr && (DW_UNSND (attr) != 0))
17300 return 1;
17301
17302 /* The DWARF standard implies that the DW_AT_prototyped attribute
17303 is only meaninful for C, but the concept also extends to other
17304 languages that allow unprototyped functions (Eg: Objective C).
17305 For all other languages, assume that functions are always
17306 prototyped. */
17307 if (cu->language != language_c
17308 && cu->language != language_objc
17309 && cu->language != language_opencl)
17310 return 1;
17311
17312 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17313 prototyped and unprototyped functions; default to prototyped,
17314 since that is more common in modern code (and RealView warns
17315 about unprototyped functions). */
17316 if (producer_is_realview (cu->producer))
17317 return 1;
17318
17319 return 0;
17320}
17321
c906108c
SS
17322/* Handle DIES due to C code like:
17323
17324 struct foo
c5aa993b
JM
17325 {
17326 int (*funcp)(int a, long l);
17327 int b;
17328 };
c906108c 17329
0963b4bd 17330 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17331
f792889a 17332static struct type *
e7c27a73 17333read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17334{
518817b3 17335 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17336 struct type *type; /* Type that this function returns. */
17337 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17338 struct attribute *attr;
17339
e7c27a73 17340 type = die_type (die, cu);
7e314c57
JK
17341
17342 /* The die_type call above may have already set the type for this DIE. */
17343 ftype = get_die_type (die, cu);
17344 if (ftype)
17345 return ftype;
17346
0c8b41f1 17347 ftype = lookup_function_type (type);
c906108c 17348
4d804846 17349 if (prototyped_function_p (die, cu))
a6c727b2 17350 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17351
c055b101
CV
17352 /* Store the calling convention in the type if it's available in
17353 the subroutine die. Otherwise set the calling convention to
17354 the default value DW_CC_normal. */
17355 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17356 if (attr)
17357 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17358 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17359 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17360 else
17361 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17362
743649fd
MW
17363 /* Record whether the function returns normally to its caller or not
17364 if the DWARF producer set that information. */
17365 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17366 if (attr && (DW_UNSND (attr) != 0))
17367 TYPE_NO_RETURN (ftype) = 1;
17368
76c10ea2
GM
17369 /* We need to add the subroutine type to the die immediately so
17370 we don't infinitely recurse when dealing with parameters
0963b4bd 17371 declared as the same subroutine type. */
76c10ea2 17372 set_die_type (die, ftype, cu);
6e70227d 17373
639d11d3 17374 if (die->child != NULL)
c906108c 17375 {
bb5ed363 17376 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17377 struct die_info *child_die;
8072405b 17378 int nparams, iparams;
c906108c
SS
17379
17380 /* Count the number of parameters.
17381 FIXME: GDB currently ignores vararg functions, but knows about
17382 vararg member functions. */
8072405b 17383 nparams = 0;
639d11d3 17384 child_die = die->child;
c906108c
SS
17385 while (child_die && child_die->tag)
17386 {
17387 if (child_die->tag == DW_TAG_formal_parameter)
17388 nparams++;
17389 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17390 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17391 child_die = sibling_die (child_die);
17392 }
17393
17394 /* Allocate storage for parameters and fill them in. */
17395 TYPE_NFIELDS (ftype) = nparams;
17396 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17397 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17398
8072405b
JK
17399 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17400 even if we error out during the parameters reading below. */
17401 for (iparams = 0; iparams < nparams; iparams++)
17402 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17403
17404 iparams = 0;
639d11d3 17405 child_die = die->child;
c906108c
SS
17406 while (child_die && child_die->tag)
17407 {
17408 if (child_die->tag == DW_TAG_formal_parameter)
17409 {
3ce3b1ba
PA
17410 struct type *arg_type;
17411
17412 /* DWARF version 2 has no clean way to discern C++
17413 static and non-static member functions. G++ helps
17414 GDB by marking the first parameter for non-static
17415 member functions (which is the this pointer) as
17416 artificial. We pass this information to
17417 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17418
17419 DWARF version 3 added DW_AT_object_pointer, which GCC
17420 4.5 does not yet generate. */
e142c38c 17421 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17422 if (attr)
17423 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17424 else
9c37b5ae 17425 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17426 arg_type = die_type (child_die, cu);
17427
17428 /* RealView does not mark THIS as const, which the testsuite
17429 expects. GCC marks THIS as const in method definitions,
17430 but not in the class specifications (GCC PR 43053). */
17431 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17432 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17433 {
17434 int is_this = 0;
17435 struct dwarf2_cu *arg_cu = cu;
17436 const char *name = dwarf2_name (child_die, cu);
17437
17438 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17439 if (attr)
17440 {
17441 /* If the compiler emits this, use it. */
17442 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17443 is_this = 1;
17444 }
17445 else if (name && strcmp (name, "this") == 0)
17446 /* Function definitions will have the argument names. */
17447 is_this = 1;
17448 else if (name == NULL && iparams == 0)
17449 /* Declarations may not have the names, so like
17450 elsewhere in GDB, assume an artificial first
17451 argument is "this". */
17452 is_this = 1;
17453
17454 if (is_this)
17455 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17456 arg_type, 0);
17457 }
17458
17459 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17460 iparams++;
17461 }
17462 child_die = sibling_die (child_die);
17463 }
17464 }
17465
76c10ea2 17466 return ftype;
c906108c
SS
17467}
17468
f792889a 17469static struct type *
e7c27a73 17470read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17471{
518817b3 17472 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17473 const char *name = NULL;
3c8e0968 17474 struct type *this_type, *target_type;
c906108c 17475
94af9270 17476 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17477 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17478 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17479 set_die_type (die, this_type, cu);
3c8e0968
DE
17480 target_type = die_type (die, cu);
17481 if (target_type != this_type)
17482 TYPE_TARGET_TYPE (this_type) = target_type;
17483 else
17484 {
17485 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17486 spec and cause infinite loops in GDB. */
b98664d3 17487 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17488 "- DIE at %s [in module %s]"),
17489 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17490 TYPE_TARGET_TYPE (this_type) = NULL;
17491 }
f792889a 17492 return this_type;
c906108c
SS
17493}
17494
9b790ce7
UW
17495/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17496 (which may be different from NAME) to the architecture back-end to allow
17497 it to guess the correct format if necessary. */
17498
17499static struct type *
17500dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17501 const char *name_hint)
17502{
17503 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17504 const struct floatformat **format;
17505 struct type *type;
17506
17507 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17508 if (format)
17509 type = init_float_type (objfile, bits, name, format);
17510 else
77b7c781 17511 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17512
17513 return type;
17514}
17515
eb77c9df
AB
17516/* Allocate an integer type of size BITS and name NAME. */
17517
17518static struct type *
17519dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17520 int bits, int unsigned_p, const char *name)
17521{
17522 struct type *type;
17523
17524 /* Versions of Intel's C Compiler generate an integer type called "void"
17525 instead of using DW_TAG_unspecified_type. This has been seen on
17526 at least versions 14, 17, and 18. */
35ee2dc2
AB
17527 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17528 && strcmp (name, "void") == 0)
eb77c9df
AB
17529 type = objfile_type (objfile)->builtin_void;
17530 else
17531 type = init_integer_type (objfile, bits, unsigned_p, name);
17532
17533 return type;
17534}
17535
8bdc1658
AB
17536/* Initialise and return a floating point type of size BITS suitable for
17537 use as a component of a complex number. The NAME_HINT is passed through
17538 when initialising the floating point type and is the name of the complex
17539 type.
17540
17541 As DWARF doesn't currently provide an explicit name for the components
17542 of a complex number, but it can be helpful to have these components
17543 named, we try to select a suitable name based on the size of the
17544 component. */
17545static struct type *
17546dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17547 struct objfile *objfile,
17548 int bits, const char *name_hint)
17549{
17550 gdbarch *gdbarch = get_objfile_arch (objfile);
17551 struct type *tt = nullptr;
17552
35add35e
AB
17553 /* Try to find a suitable floating point builtin type of size BITS.
17554 We're going to use the name of this type as the name for the complex
17555 target type that we are about to create. */
1db455a7 17556 switch (cu->language)
8bdc1658 17557 {
1db455a7
AB
17558 case language_fortran:
17559 switch (bits)
17560 {
17561 case 32:
17562 tt = builtin_f_type (gdbarch)->builtin_real;
17563 break;
17564 case 64:
17565 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17566 break;
17567 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17568 case 128:
17569 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17570 break;
17571 }
8bdc1658 17572 break;
1db455a7
AB
17573 default:
17574 switch (bits)
17575 {
17576 case 32:
17577 tt = builtin_type (gdbarch)->builtin_float;
17578 break;
17579 case 64:
17580 tt = builtin_type (gdbarch)->builtin_double;
17581 break;
17582 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17583 case 128:
17584 tt = builtin_type (gdbarch)->builtin_long_double;
17585 break;
17586 }
8bdc1658
AB
17587 break;
17588 }
17589
35add35e
AB
17590 /* If the type we found doesn't match the size we were looking for, then
17591 pretend we didn't find a type at all, the complex target type we
17592 create will then be nameless. */
a12e5744 17593 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17594 tt = nullptr;
17595
8bdc1658
AB
17596 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17597 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17598}
17599
c906108c
SS
17600/* Find a representation of a given base type and install
17601 it in the TYPE field of the die. */
17602
f792889a 17603static struct type *
e7c27a73 17604read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17605{
518817b3 17606 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17607 struct type *type;
17608 struct attribute *attr;
19f392bc 17609 int encoding = 0, bits = 0;
15d034d0 17610 const char *name;
c906108c 17611
e142c38c 17612 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17613 if (attr)
17614 {
17615 encoding = DW_UNSND (attr);
17616 }
e142c38c 17617 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17618 if (attr)
17619 {
19f392bc 17620 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17621 }
39cbfefa 17622 name = dwarf2_name (die, cu);
6ccb9162 17623 if (!name)
c906108c 17624 {
b98664d3 17625 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17626 }
6ccb9162
UW
17627
17628 switch (encoding)
c906108c 17629 {
6ccb9162
UW
17630 case DW_ATE_address:
17631 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17632 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17633 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17634 break;
17635 case DW_ATE_boolean:
19f392bc 17636 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17637 break;
17638 case DW_ATE_complex_float:
8bdc1658 17639 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
19f392bc 17640 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17641 break;
17642 case DW_ATE_decimal_float:
19f392bc 17643 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17644 break;
17645 case DW_ATE_float:
9b790ce7 17646 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17647 break;
17648 case DW_ATE_signed:
eb77c9df 17649 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17650 break;
17651 case DW_ATE_unsigned:
3b2b8fea
TT
17652 if (cu->language == language_fortran
17653 && name
61012eef 17654 && startswith (name, "character("))
19f392bc
UW
17655 type = init_character_type (objfile, bits, 1, name);
17656 else
eb77c9df 17657 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17658 break;
17659 case DW_ATE_signed_char:
6e70227d 17660 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17661 || cu->language == language_pascal
17662 || cu->language == language_fortran)
19f392bc
UW
17663 type = init_character_type (objfile, bits, 0, name);
17664 else
eb77c9df 17665 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17666 break;
17667 case DW_ATE_unsigned_char:
868a0084 17668 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17669 || cu->language == language_pascal
c44af4eb
TT
17670 || cu->language == language_fortran
17671 || cu->language == language_rust)
19f392bc
UW
17672 type = init_character_type (objfile, bits, 1, name);
17673 else
eb77c9df 17674 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17675 break;
75079b2b 17676 case DW_ATE_UTF:
53e710ac
PA
17677 {
17678 gdbarch *arch = get_objfile_arch (objfile);
17679
17680 if (bits == 16)
17681 type = builtin_type (arch)->builtin_char16;
17682 else if (bits == 32)
17683 type = builtin_type (arch)->builtin_char32;
17684 else
17685 {
b98664d3 17686 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17687 bits);
eb77c9df 17688 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17689 }
17690 return set_die_type (die, type, cu);
17691 }
75079b2b
TT
17692 break;
17693
6ccb9162 17694 default:
b98664d3 17695 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17696 dwarf_type_encoding_name (encoding));
77b7c781 17697 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17698 break;
c906108c 17699 }
6ccb9162 17700
0114d602 17701 if (name && strcmp (name, "char") == 0)
876cecd0 17702 TYPE_NOSIGN (type) = 1;
0114d602 17703
2b4424c3
TT
17704 maybe_set_alignment (cu, die, type);
17705
f792889a 17706 return set_die_type (die, type, cu);
c906108c
SS
17707}
17708
80180f79
SA
17709/* Parse dwarf attribute if it's a block, reference or constant and put the
17710 resulting value of the attribute into struct bound_prop.
17711 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17712
17713static int
17714attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17715 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17716 struct type *default_type)
80180f79
SA
17717{
17718 struct dwarf2_property_baton *baton;
518817b3
SM
17719 struct obstack *obstack
17720 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17721
9a49df9d
AB
17722 gdb_assert (default_type != NULL);
17723
80180f79
SA
17724 if (attr == NULL || prop == NULL)
17725 return 0;
17726
17727 if (attr_form_is_block (attr))
17728 {
8d749320 17729 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17730 baton->property_type = default_type;
80180f79
SA
17731 baton->locexpr.per_cu = cu->per_cu;
17732 baton->locexpr.size = DW_BLOCK (attr)->size;
17733 baton->locexpr.data = DW_BLOCK (attr)->data;
9a49df9d 17734 baton->locexpr.is_reference = false;
80180f79
SA
17735 prop->data.baton = baton;
17736 prop->kind = PROP_LOCEXPR;
17737 gdb_assert (prop->data.baton != NULL);
17738 }
17739 else if (attr_form_is_ref (attr))
17740 {
17741 struct dwarf2_cu *target_cu = cu;
17742 struct die_info *target_die;
17743 struct attribute *target_attr;
17744
17745 target_die = follow_die_ref (die, attr, &target_cu);
17746 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17747 if (target_attr == NULL)
17748 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17749 target_cu);
80180f79
SA
17750 if (target_attr == NULL)
17751 return 0;
17752
df25ebbd 17753 switch (target_attr->name)
80180f79 17754 {
df25ebbd
JB
17755 case DW_AT_location:
17756 if (attr_form_is_section_offset (target_attr))
17757 {
8d749320 17758 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17759 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17760 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17761 prop->data.baton = baton;
17762 prop->kind = PROP_LOCLIST;
17763 gdb_assert (prop->data.baton != NULL);
17764 }
17765 else if (attr_form_is_block (target_attr))
17766 {
8d749320 17767 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17768 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17769 baton->locexpr.per_cu = cu->per_cu;
17770 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17771 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17772 baton->locexpr.is_reference = true;
df25ebbd
JB
17773 prop->data.baton = baton;
17774 prop->kind = PROP_LOCEXPR;
17775 gdb_assert (prop->data.baton != NULL);
17776 }
17777 else
17778 {
17779 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17780 "dynamic property");
17781 return 0;
17782 }
17783 break;
17784 case DW_AT_data_member_location:
17785 {
17786 LONGEST offset;
17787
17788 if (!handle_data_member_location (target_die, target_cu,
17789 &offset))
17790 return 0;
17791
8d749320 17792 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17793 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17794 target_cu);
df25ebbd
JB
17795 baton->offset_info.offset = offset;
17796 baton->offset_info.type = die_type (target_die, target_cu);
17797 prop->data.baton = baton;
17798 prop->kind = PROP_ADDR_OFFSET;
17799 break;
17800 }
80180f79
SA
17801 }
17802 }
17803 else if (attr_form_is_constant (attr))
17804 {
17805 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17806 prop->kind = PROP_CONST;
17807 }
17808 else
17809 {
17810 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17811 dwarf2_name (die, cu));
17812 return 0;
17813 }
17814
17815 return 1;
17816}
17817
9a49df9d
AB
17818/* Find an integer type the same size as the address size given in the
17819 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17820 is unsigned or not. */
17821
17822static struct type *
17823dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17824 bool unsigned_p)
17825{
17826 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17827 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17828 struct type *int_type;
17829
17830 /* Helper macro to examine the various builtin types. */
17831#define TRY_TYPE(F) \
17832 int_type = (unsigned_p \
17833 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17834 : objfile_type (objfile)->builtin_ ## F); \
17835 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17836 return int_type
17837
17838 TRY_TYPE (char);
17839 TRY_TYPE (short);
17840 TRY_TYPE (int);
17841 TRY_TYPE (long);
17842 TRY_TYPE (long_long);
17843
17844#undef TRY_TYPE
17845
17846 gdb_assert_not_reached ("unable to find suitable integer type");
17847}
17848
b86352cf
AB
17849/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17850 present (which is valid) then compute the default type based on the
17851 compilation units address size. */
17852
17853static struct type *
17854read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17855{
17856 struct type *index_type = die_type (die, cu);
17857
17858 /* Dwarf-2 specifications explicitly allows to create subrange types
17859 without specifying a base type.
17860 In that case, the base type must be set to the type of
17861 the lower bound, upper bound or count, in that order, if any of these
17862 three attributes references an object that has a type.
17863 If no base type is found, the Dwarf-2 specifications say that
17864 a signed integer type of size equal to the size of an address should
17865 be used.
17866 For the following C code: `extern char gdb_int [];'
17867 GCC produces an empty range DIE.
17868 FIXME: muller/2010-05-28: Possible references to object for low bound,
17869 high bound or count are not yet handled by this code. */
17870 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 17871 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
17872
17873 return index_type;
17874}
17875
a02abb62
JB
17876/* Read the given DW_AT_subrange DIE. */
17877
f792889a 17878static struct type *
a02abb62
JB
17879read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17880{
4c9ad8c2 17881 struct type *base_type, *orig_base_type;
a02abb62
JB
17882 struct type *range_type;
17883 struct attribute *attr;
729efb13 17884 struct dynamic_prop low, high;
4fae6e18 17885 int low_default_is_valid;
c451ebe5 17886 int high_bound_is_count = 0;
15d034d0 17887 const char *name;
d359392f 17888 ULONGEST negative_mask;
e77813c8 17889
b86352cf
AB
17890 orig_base_type = read_subrange_index_type (die, cu);
17891
4c9ad8c2
TT
17892 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17893 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17894 creating the range type, but we use the result of check_typedef
17895 when examining properties of the type. */
17896 base_type = check_typedef (orig_base_type);
a02abb62 17897
7e314c57
JK
17898 /* The die_type call above may have already set the type for this DIE. */
17899 range_type = get_die_type (die, cu);
17900 if (range_type)
17901 return range_type;
17902
729efb13
SA
17903 low.kind = PROP_CONST;
17904 high.kind = PROP_CONST;
17905 high.data.const_val = 0;
17906
4fae6e18
JK
17907 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17908 omitting DW_AT_lower_bound. */
17909 switch (cu->language)
6e70227d 17910 {
4fae6e18
JK
17911 case language_c:
17912 case language_cplus:
729efb13 17913 low.data.const_val = 0;
4fae6e18
JK
17914 low_default_is_valid = 1;
17915 break;
17916 case language_fortran:
729efb13 17917 low.data.const_val = 1;
4fae6e18
JK
17918 low_default_is_valid = 1;
17919 break;
17920 case language_d:
4fae6e18 17921 case language_objc:
c44af4eb 17922 case language_rust:
729efb13 17923 low.data.const_val = 0;
4fae6e18
JK
17924 low_default_is_valid = (cu->header.version >= 4);
17925 break;
17926 case language_ada:
17927 case language_m2:
17928 case language_pascal:
729efb13 17929 low.data.const_val = 1;
4fae6e18
JK
17930 low_default_is_valid = (cu->header.version >= 4);
17931 break;
17932 default:
729efb13 17933 low.data.const_val = 0;
4fae6e18
JK
17934 low_default_is_valid = 0;
17935 break;
a02abb62
JB
17936 }
17937
e142c38c 17938 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17939 if (attr)
9a49df9d 17940 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17941 else if (!low_default_is_valid)
b98664d3 17942 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17943 "- DIE at %s [in module %s]"),
17944 sect_offset_str (die->sect_off),
518817b3 17945 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17946
506f5c41
TV
17947 struct attribute *attr_ub, *attr_count;
17948 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17949 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17950 {
506f5c41 17951 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17952 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17953 {
c451ebe5
SA
17954 /* If bounds are constant do the final calculation here. */
17955 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17956 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17957 else
17958 high_bound_is_count = 1;
c2ff108b 17959 }
506f5c41
TV
17960 else
17961 {
17962 if (attr_ub != NULL)
17963 complaint (_("Unresolved DW_AT_upper_bound "
17964 "- DIE at %s [in module %s]"),
17965 sect_offset_str (die->sect_off),
17966 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17967 if (attr_count != NULL)
17968 complaint (_("Unresolved DW_AT_count "
17969 "- DIE at %s [in module %s]"),
17970 sect_offset_str (die->sect_off),
17971 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17972 }
e77813c8 17973 }
a02abb62 17974
4e962e74
TT
17975 LONGEST bias = 0;
17976 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17977 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
17978 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17979
dbb9c2b1
JB
17980 /* Normally, the DWARF producers are expected to use a signed
17981 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17982 But this is unfortunately not always the case, as witnessed
17983 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17984 is used instead. To work around that ambiguity, we treat
17985 the bounds as signed, and thus sign-extend their values, when
17986 the base type is signed. */
6e70227d 17987 negative_mask =
d359392f 17988 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17989 if (low.kind == PROP_CONST
17990 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17991 low.data.const_val |= negative_mask;
17992 if (high.kind == PROP_CONST
17993 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17994 high.data.const_val |= negative_mask;
43bbcdc2 17995
4e962e74 17996 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17997
c451ebe5
SA
17998 if (high_bound_is_count)
17999 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18000
c2ff108b
JK
18001 /* Ada expects an empty array on no boundary attributes. */
18002 if (attr == NULL && cu->language != language_ada)
729efb13 18003 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18004
39cbfefa
DJ
18005 name = dwarf2_name (die, cu);
18006 if (name)
18007 TYPE_NAME (range_type) = name;
6e70227d 18008
e142c38c 18009 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
18010 if (attr)
18011 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18012
2b4424c3
TT
18013 maybe_set_alignment (cu, die, range_type);
18014
7e314c57
JK
18015 set_die_type (die, range_type, cu);
18016
18017 /* set_die_type should be already done. */
b4ba55a1
JB
18018 set_descriptive_type (range_type, die, cu);
18019
7e314c57 18020 return range_type;
a02abb62 18021}
6e70227d 18022
f792889a 18023static struct type *
81a17f79
JB
18024read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18025{
18026 struct type *type;
81a17f79 18027
518817b3
SM
18028 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18029 NULL);
0114d602 18030 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18031
74a2f8ff
JB
18032 /* In Ada, an unspecified type is typically used when the description
18033 of the type is defered to a different unit. When encountering
18034 such a type, we treat it as a stub, and try to resolve it later on,
18035 when needed. */
18036 if (cu->language == language_ada)
18037 TYPE_STUB (type) = 1;
18038
f792889a 18039 return set_die_type (die, type, cu);
81a17f79 18040}
a02abb62 18041
639d11d3
DC
18042/* Read a single die and all its descendents. Set the die's sibling
18043 field to NULL; set other fields in the die correctly, and set all
18044 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18045 location of the info_ptr after reading all of those dies. PARENT
18046 is the parent of the die in question. */
18047
18048static struct die_info *
dee91e82 18049read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18050 const gdb_byte *info_ptr,
18051 const gdb_byte **new_info_ptr,
dee91e82 18052 struct die_info *parent)
639d11d3
DC
18053{
18054 struct die_info *die;
d521ce57 18055 const gdb_byte *cur_ptr;
639d11d3
DC
18056 int has_children;
18057
bf6af496 18058 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18059 if (die == NULL)
18060 {
18061 *new_info_ptr = cur_ptr;
18062 return NULL;
18063 }
93311388 18064 store_in_ref_table (die, reader->cu);
639d11d3
DC
18065
18066 if (has_children)
bf6af496 18067 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18068 else
18069 {
18070 die->child = NULL;
18071 *new_info_ptr = cur_ptr;
18072 }
18073
18074 die->sibling = NULL;
18075 die->parent = parent;
18076 return die;
18077}
18078
18079/* Read a die, all of its descendents, and all of its siblings; set
18080 all of the fields of all of the dies correctly. Arguments are as
18081 in read_die_and_children. */
18082
18083static struct die_info *
bf6af496 18084read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18085 const gdb_byte *info_ptr,
18086 const gdb_byte **new_info_ptr,
bf6af496 18087 struct die_info *parent)
639d11d3
DC
18088{
18089 struct die_info *first_die, *last_sibling;
d521ce57 18090 const gdb_byte *cur_ptr;
639d11d3 18091
c906108c 18092 cur_ptr = info_ptr;
639d11d3
DC
18093 first_die = last_sibling = NULL;
18094
18095 while (1)
c906108c 18096 {
639d11d3 18097 struct die_info *die
dee91e82 18098 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18099
1d325ec1 18100 if (die == NULL)
c906108c 18101 {
639d11d3
DC
18102 *new_info_ptr = cur_ptr;
18103 return first_die;
c906108c 18104 }
1d325ec1
DJ
18105
18106 if (!first_die)
18107 first_die = die;
c906108c 18108 else
1d325ec1
DJ
18109 last_sibling->sibling = die;
18110
18111 last_sibling = die;
c906108c 18112 }
c906108c
SS
18113}
18114
bf6af496
DE
18115/* Read a die, all of its descendents, and all of its siblings; set
18116 all of the fields of all of the dies correctly. Arguments are as
18117 in read_die_and_children.
18118 This the main entry point for reading a DIE and all its children. */
18119
18120static struct die_info *
18121read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18122 const gdb_byte *info_ptr,
18123 const gdb_byte **new_info_ptr,
bf6af496
DE
18124 struct die_info *parent)
18125{
18126 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18127 new_info_ptr, parent);
18128
b4f54984 18129 if (dwarf_die_debug)
bf6af496
DE
18130 {
18131 fprintf_unfiltered (gdb_stdlog,
18132 "Read die from %s@0x%x of %s:\n",
a32a8923 18133 get_section_name (reader->die_section),
bf6af496
DE
18134 (unsigned) (info_ptr - reader->die_section->buffer),
18135 bfd_get_filename (reader->abfd));
b4f54984 18136 dump_die (die, dwarf_die_debug);
bf6af496
DE
18137 }
18138
18139 return die;
18140}
18141
3019eac3
DE
18142/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18143 attributes.
18144 The caller is responsible for filling in the extra attributes
18145 and updating (*DIEP)->num_attrs.
18146 Set DIEP to point to a newly allocated die with its information,
18147 except for its child, sibling, and parent fields.
18148 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18149
d521ce57 18150static const gdb_byte *
3019eac3 18151read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18152 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18153 int *has_children, int num_extra_attrs)
93311388 18154{
b64f50a1 18155 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18156 struct abbrev_info *abbrev;
18157 struct die_info *die;
18158 struct dwarf2_cu *cu = reader->cu;
18159 bfd *abfd = reader->abfd;
18160
9c541725 18161 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18162 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18163 info_ptr += bytes_read;
18164 if (!abbrev_number)
18165 {
18166 *diep = NULL;
18167 *has_children = 0;
18168 return info_ptr;
18169 }
18170
685af9cd 18171 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18172 if (!abbrev)
348e048f
DE
18173 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18174 abbrev_number,
18175 bfd_get_filename (abfd));
18176
3019eac3 18177 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18178 die->sect_off = sect_off;
93311388
DE
18179 die->tag = abbrev->tag;
18180 die->abbrev = abbrev_number;
18181
3019eac3
DE
18182 /* Make the result usable.
18183 The caller needs to update num_attrs after adding the extra
18184 attributes. */
93311388
DE
18185 die->num_attrs = abbrev->num_attrs;
18186
18187 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18188 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18189 info_ptr);
93311388
DE
18190
18191 *diep = die;
18192 *has_children = abbrev->has_children;
18193 return info_ptr;
18194}
18195
3019eac3
DE
18196/* Read a die and all its attributes.
18197 Set DIEP to point to a newly allocated die with its information,
18198 except for its child, sibling, and parent fields.
18199 Set HAS_CHILDREN to tell whether the die has children or not. */
18200
d521ce57 18201static const gdb_byte *
3019eac3 18202read_full_die (const struct die_reader_specs *reader,
d521ce57 18203 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18204 int *has_children)
18205{
d521ce57 18206 const gdb_byte *result;
bf6af496
DE
18207
18208 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18209
b4f54984 18210 if (dwarf_die_debug)
bf6af496
DE
18211 {
18212 fprintf_unfiltered (gdb_stdlog,
18213 "Read die from %s@0x%x of %s:\n",
a32a8923 18214 get_section_name (reader->die_section),
bf6af496
DE
18215 (unsigned) (info_ptr - reader->die_section->buffer),
18216 bfd_get_filename (reader->abfd));
b4f54984 18217 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18218 }
18219
18220 return result;
3019eac3 18221}
433df2d4
DE
18222\f
18223/* Abbreviation tables.
3019eac3 18224
433df2d4 18225 In DWARF version 2, the description of the debugging information is
c906108c
SS
18226 stored in a separate .debug_abbrev section. Before we read any
18227 dies from a section we read in all abbreviations and install them
433df2d4
DE
18228 in a hash table. */
18229
18230/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18231
685af9cd
TT
18232struct abbrev_info *
18233abbrev_table::alloc_abbrev ()
433df2d4
DE
18234{
18235 struct abbrev_info *abbrev;
18236
685af9cd 18237 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18238 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18239
433df2d4
DE
18240 return abbrev;
18241}
18242
18243/* Add an abbreviation to the table. */
c906108c 18244
685af9cd
TT
18245void
18246abbrev_table::add_abbrev (unsigned int abbrev_number,
18247 struct abbrev_info *abbrev)
433df2d4
DE
18248{
18249 unsigned int hash_number;
18250
18251 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18252 abbrev->next = m_abbrevs[hash_number];
18253 m_abbrevs[hash_number] = abbrev;
433df2d4 18254}
dee91e82 18255
433df2d4
DE
18256/* Look up an abbrev in the table.
18257 Returns NULL if the abbrev is not found. */
18258
685af9cd
TT
18259struct abbrev_info *
18260abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18261{
433df2d4
DE
18262 unsigned int hash_number;
18263 struct abbrev_info *abbrev;
18264
18265 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18266 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18267
18268 while (abbrev)
18269 {
18270 if (abbrev->number == abbrev_number)
18271 return abbrev;
18272 abbrev = abbrev->next;
18273 }
18274 return NULL;
18275}
18276
18277/* Read in an abbrev table. */
18278
685af9cd 18279static abbrev_table_up
ed2dc618
SM
18280abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18281 struct dwarf2_section_info *section,
9c541725 18282 sect_offset sect_off)
433df2d4
DE
18283{
18284 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18285 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18286 const gdb_byte *abbrev_ptr;
c906108c
SS
18287 struct abbrev_info *cur_abbrev;
18288 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18289 unsigned int abbrev_form;
f3dd6933
DJ
18290 struct attr_abbrev *cur_attrs;
18291 unsigned int allocated_attrs;
c906108c 18292
685af9cd 18293 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18294
433df2d4 18295 dwarf2_read_section (objfile, section);
9c541725 18296 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18297 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18298 abbrev_ptr += bytes_read;
18299
f3dd6933 18300 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18301 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18302
0963b4bd 18303 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18304 while (abbrev_number)
18305 {
685af9cd 18306 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18307
18308 /* read in abbrev header */
18309 cur_abbrev->number = abbrev_number;
aead7601
SM
18310 cur_abbrev->tag
18311 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18312 abbrev_ptr += bytes_read;
18313 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18314 abbrev_ptr += 1;
18315
18316 /* now read in declarations */
22d2f3ab 18317 for (;;)
c906108c 18318 {
43988095
JK
18319 LONGEST implicit_const;
18320
22d2f3ab
JK
18321 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18322 abbrev_ptr += bytes_read;
18323 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18324 abbrev_ptr += bytes_read;
43988095
JK
18325 if (abbrev_form == DW_FORM_implicit_const)
18326 {
18327 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18328 &bytes_read);
18329 abbrev_ptr += bytes_read;
18330 }
18331 else
18332 {
18333 /* Initialize it due to a false compiler warning. */
18334 implicit_const = -1;
18335 }
22d2f3ab
JK
18336
18337 if (abbrev_name == 0)
18338 break;
18339
f3dd6933 18340 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18341 {
f3dd6933
DJ
18342 allocated_attrs += ATTR_ALLOC_CHUNK;
18343 cur_attrs
224c3ddb 18344 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18345 }
ae038cb0 18346
aead7601
SM
18347 cur_attrs[cur_abbrev->num_attrs].name
18348 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18349 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18350 = (enum dwarf_form) abbrev_form;
43988095 18351 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18352 ++cur_abbrev->num_attrs;
c906108c
SS
18353 }
18354
8d749320
SM
18355 cur_abbrev->attrs =
18356 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18357 cur_abbrev->num_attrs);
f3dd6933
DJ
18358 memcpy (cur_abbrev->attrs, cur_attrs,
18359 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18360
685af9cd 18361 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18362
18363 /* Get next abbreviation.
18364 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18365 always properly terminated with an abbrev number of 0.
18366 Exit loop if we encounter an abbreviation which we have
18367 already read (which means we are about to read the abbreviations
18368 for the next compile unit) or if the end of the abbreviation
18369 table is reached. */
433df2d4 18370 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18371 break;
18372 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18373 abbrev_ptr += bytes_read;
685af9cd 18374 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18375 break;
18376 }
f3dd6933
DJ
18377
18378 xfree (cur_attrs);
433df2d4 18379 return abbrev_table;
c906108c
SS
18380}
18381
72bf9492
DJ
18382/* Returns nonzero if TAG represents a type that we might generate a partial
18383 symbol for. */
18384
18385static int
18386is_type_tag_for_partial (int tag)
18387{
18388 switch (tag)
18389 {
18390#if 0
18391 /* Some types that would be reasonable to generate partial symbols for,
18392 that we don't at present. */
18393 case DW_TAG_array_type:
18394 case DW_TAG_file_type:
18395 case DW_TAG_ptr_to_member_type:
18396 case DW_TAG_set_type:
18397 case DW_TAG_string_type:
18398 case DW_TAG_subroutine_type:
18399#endif
18400 case DW_TAG_base_type:
18401 case DW_TAG_class_type:
680b30c7 18402 case DW_TAG_interface_type:
72bf9492
DJ
18403 case DW_TAG_enumeration_type:
18404 case DW_TAG_structure_type:
18405 case DW_TAG_subrange_type:
18406 case DW_TAG_typedef:
18407 case DW_TAG_union_type:
18408 return 1;
18409 default:
18410 return 0;
18411 }
18412}
18413
18414/* Load all DIEs that are interesting for partial symbols into memory. */
18415
18416static struct partial_die_info *
dee91e82 18417load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18418 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18419{
dee91e82 18420 struct dwarf2_cu *cu = reader->cu;
518817b3 18421 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18422 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18423 unsigned int bytes_read;
5afb4e99 18424 unsigned int load_all = 0;
72bf9492
DJ
18425 int nesting_level = 1;
18426
18427 parent_die = NULL;
18428 last_die = NULL;
18429
7adf1e79
DE
18430 gdb_assert (cu->per_cu != NULL);
18431 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18432 load_all = 1;
18433
72bf9492
DJ
18434 cu->partial_dies
18435 = htab_create_alloc_ex (cu->header.length / 12,
18436 partial_die_hash,
18437 partial_die_eq,
18438 NULL,
18439 &cu->comp_unit_obstack,
18440 hashtab_obstack_allocate,
18441 dummy_obstack_deallocate);
18442
72bf9492
DJ
18443 while (1)
18444 {
685af9cd 18445 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18446
18447 /* A NULL abbrev means the end of a series of children. */
18448 if (abbrev == NULL)
18449 {
18450 if (--nesting_level == 0)
cd9983dd
YQ
18451 return first_die;
18452
72bf9492
DJ
18453 info_ptr += bytes_read;
18454 last_die = parent_die;
18455 parent_die = parent_die->die_parent;
18456 continue;
18457 }
18458
98bfdba5
PA
18459 /* Check for template arguments. We never save these; if
18460 they're seen, we just mark the parent, and go on our way. */
18461 if (parent_die != NULL
18462 && cu->language == language_cplus
18463 && (abbrev->tag == DW_TAG_template_type_param
18464 || abbrev->tag == DW_TAG_template_value_param))
18465 {
18466 parent_die->has_template_arguments = 1;
18467
18468 if (!load_all)
18469 {
18470 /* We don't need a partial DIE for the template argument. */
dee91e82 18471 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18472 continue;
18473 }
18474 }
18475
0d99eb77 18476 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18477 Skip their other children. */
18478 if (!load_all
18479 && cu->language == language_cplus
18480 && parent_die != NULL
18481 && parent_die->tag == DW_TAG_subprogram)
18482 {
dee91e82 18483 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18484 continue;
18485 }
18486
5afb4e99
DJ
18487 /* Check whether this DIE is interesting enough to save. Normally
18488 we would not be interested in members here, but there may be
18489 later variables referencing them via DW_AT_specification (for
18490 static members). */
18491 if (!load_all
18492 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18493 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18494 && abbrev->tag != DW_TAG_enumerator
18495 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18496 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18497 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18498 && abbrev->tag != DW_TAG_variable
5afb4e99 18499 && abbrev->tag != DW_TAG_namespace
f55ee35c 18500 && abbrev->tag != DW_TAG_module
95554aad 18501 && abbrev->tag != DW_TAG_member
74921315
KS
18502 && abbrev->tag != DW_TAG_imported_unit
18503 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18504 {
18505 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18506 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18507 continue;
18508 }
18509
6f06d47b
YQ
18510 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18511 abbrev);
cd9983dd 18512
48fbe735 18513 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18514
18515 /* This two-pass algorithm for processing partial symbols has a
18516 high cost in cache pressure. Thus, handle some simple cases
18517 here which cover the majority of C partial symbols. DIEs
18518 which neither have specification tags in them, nor could have
18519 specification tags elsewhere pointing at them, can simply be
18520 processed and discarded.
18521
18522 This segment is also optional; scan_partial_symbols and
18523 add_partial_symbol will handle these DIEs if we chain
18524 them in normally. When compilers which do not emit large
18525 quantities of duplicate debug information are more common,
18526 this code can probably be removed. */
18527
18528 /* Any complete simple types at the top level (pretty much all
18529 of them, for a language without namespaces), can be processed
18530 directly. */
18531 if (parent_die == NULL
cd9983dd
YQ
18532 && pdi.has_specification == 0
18533 && pdi.is_declaration == 0
18534 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18535 || pdi.tag == DW_TAG_base_type
18536 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18537 {
cd9983dd 18538 if (building_psymtab && pdi.name != NULL)
67547d89 18539 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
79748972 18540 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18541 psymbol_placement::STATIC,
1762568f 18542 0, cu->language, objfile);
cd9983dd 18543 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18544 continue;
18545 }
18546
d8228535
JK
18547 /* The exception for DW_TAG_typedef with has_children above is
18548 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18549 type_name_or_error will error on such types later.
d8228535
JK
18550
18551 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18552 it could not find the child DIEs referenced later, this is checked
18553 above. In correct DWARF DW_TAG_typedef should have no children. */
18554
cd9983dd 18555 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18556 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18557 "- DIE at %s [in module %s]"),
cd9983dd 18558 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18559
72bf9492
DJ
18560 /* If we're at the second level, and we're an enumerator, and
18561 our parent has no specification (meaning possibly lives in a
18562 namespace elsewhere), then we can add the partial symbol now
18563 instead of queueing it. */
cd9983dd 18564 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18565 && parent_die != NULL
18566 && parent_die->die_parent == NULL
18567 && parent_die->tag == DW_TAG_enumeration_type
18568 && parent_die->has_specification == 0)
18569 {
cd9983dd 18570 if (pdi.name == NULL)
b98664d3 18571 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18572 else if (building_psymtab)
67547d89 18573 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
79748972 18574 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18575 cu->language == language_cplus
75aedd27
TT
18576 ? psymbol_placement::GLOBAL
18577 : psymbol_placement::STATIC,
1762568f 18578 0, cu->language, objfile);
72bf9492 18579
cd9983dd 18580 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18581 continue;
18582 }
18583
cd9983dd 18584 struct partial_die_info *part_die
6f06d47b 18585 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18586
72bf9492
DJ
18587 /* We'll save this DIE so link it in. */
18588 part_die->die_parent = parent_die;
18589 part_die->die_sibling = NULL;
18590 part_die->die_child = NULL;
18591
18592 if (last_die && last_die == parent_die)
18593 last_die->die_child = part_die;
18594 else if (last_die)
18595 last_die->die_sibling = part_die;
18596
18597 last_die = part_die;
18598
18599 if (first_die == NULL)
18600 first_die = part_die;
18601
18602 /* Maybe add the DIE to the hash table. Not all DIEs that we
18603 find interesting need to be in the hash table, because we
18604 also have the parent/sibling/child chains; only those that we
18605 might refer to by offset later during partial symbol reading.
18606
18607 For now this means things that might have be the target of a
18608 DW_AT_specification, DW_AT_abstract_origin, or
18609 DW_AT_extension. DW_AT_extension will refer only to
18610 namespaces; DW_AT_abstract_origin refers to functions (and
18611 many things under the function DIE, but we do not recurse
18612 into function DIEs during partial symbol reading) and
18613 possibly variables as well; DW_AT_specification refers to
18614 declarations. Declarations ought to have the DW_AT_declaration
18615 flag. It happens that GCC forgets to put it in sometimes, but
18616 only for functions, not for types.
18617
18618 Adding more things than necessary to the hash table is harmless
18619 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18620 wasted time in find_partial_die, when we reread the compilation
18621 unit with load_all_dies set. */
72bf9492 18622
5afb4e99 18623 if (load_all
72929c62 18624 || abbrev->tag == DW_TAG_constant
5afb4e99 18625 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18626 || abbrev->tag == DW_TAG_variable
18627 || abbrev->tag == DW_TAG_namespace
18628 || part_die->is_declaration)
18629 {
18630 void **slot;
18631
18632 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18633 to_underlying (part_die->sect_off),
18634 INSERT);
72bf9492
DJ
18635 *slot = part_die;
18636 }
18637
72bf9492 18638 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18639 we have no reason to follow the children of structures; for other
98bfdba5
PA
18640 languages we have to, so that we can get at method physnames
18641 to infer fully qualified class names, for DW_AT_specification,
18642 and for C++ template arguments. For C++, we also look one level
18643 inside functions to find template arguments (if the name of the
18644 function does not already contain the template arguments).
bc30ff58 18645
0a4b0913
AB
18646 For Ada and Fortran, we need to scan the children of subprograms
18647 and lexical blocks as well because these languages allow the
18648 definition of nested entities that could be interesting for the
18649 debugger, such as nested subprograms for instance. */
72bf9492 18650 if (last_die->has_children
5afb4e99
DJ
18651 && (load_all
18652 || last_die->tag == DW_TAG_namespace
f55ee35c 18653 || last_die->tag == DW_TAG_module
72bf9492 18654 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18655 || (cu->language == language_cplus
18656 && last_die->tag == DW_TAG_subprogram
18657 && (last_die->name == NULL
18658 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18659 || (cu->language != language_c
18660 && (last_die->tag == DW_TAG_class_type
680b30c7 18661 || last_die->tag == DW_TAG_interface_type
72bf9492 18662 || last_die->tag == DW_TAG_structure_type
bc30ff58 18663 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18664 || ((cu->language == language_ada
18665 || cu->language == language_fortran)
bc30ff58
JB
18666 && (last_die->tag == DW_TAG_subprogram
18667 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18668 {
18669 nesting_level++;
18670 parent_die = last_die;
18671 continue;
18672 }
18673
18674 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18675 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18676
18677 /* Back to the top, do it again. */
18678 }
18679}
18680
6f06d47b
YQ
18681partial_die_info::partial_die_info (sect_offset sect_off_,
18682 struct abbrev_info *abbrev)
18683 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18684{
18685}
18686
35cc7ed7
YQ
18687/* Read a minimal amount of information into the minimal die structure.
18688 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18689
48fbe735
YQ
18690const gdb_byte *
18691partial_die_info::read (const struct die_reader_specs *reader,
18692 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18693{
dee91e82 18694 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18695 struct dwarf2_per_objfile *dwarf2_per_objfile
18696 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18697 unsigned int i;
c5aa993b 18698 int has_low_pc_attr = 0;
c906108c 18699 int has_high_pc_attr = 0;
91da1414 18700 int high_pc_relative = 0;
c906108c 18701
fd0a254f 18702 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18703 {
48fbe735
YQ
18704 struct attribute attr;
18705
fd0a254f 18706 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18707
18708 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18709 partial symbol table. */
c906108c
SS
18710 switch (attr.name)
18711 {
18712 case DW_AT_name:
48fbe735 18713 switch (tag)
71c25dea
TT
18714 {
18715 case DW_TAG_compile_unit:
95554aad 18716 case DW_TAG_partial_unit:
348e048f 18717 case DW_TAG_type_unit:
71c25dea
TT
18718 /* Compilation units have a DW_AT_name that is a filename, not
18719 a source language identifier. */
18720 case DW_TAG_enumeration_type:
18721 case DW_TAG_enumerator:
18722 /* These tags always have simple identifiers already; no need
18723 to canonicalize them. */
48fbe735 18724 name = DW_STRING (&attr);
71c25dea
TT
18725 break;
18726 default:
48fbe735
YQ
18727 {
18728 struct objfile *objfile = dwarf2_per_objfile->objfile;
18729
18730 name
18731 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18732 &objfile->per_bfd->storage_obstack);
18733 }
71c25dea
TT
18734 break;
18735 }
c906108c 18736 break;
31ef98ae 18737 case DW_AT_linkage_name:
c906108c 18738 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18739 /* Note that both forms of linkage name might appear. We
18740 assume they will be the same, and we only store the last
18741 one we see. */
48fbe735 18742 linkage_name = DW_STRING (&attr);
c906108c
SS
18743 break;
18744 case DW_AT_low_pc:
18745 has_low_pc_attr = 1;
48fbe735 18746 lowpc = attr_value_as_address (&attr);
c906108c
SS
18747 break;
18748 case DW_AT_high_pc:
18749 has_high_pc_attr = 1;
48fbe735 18750 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18751 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18752 high_pc_relative = 1;
c906108c
SS
18753 break;
18754 case DW_AT_location:
0963b4bd 18755 /* Support the .debug_loc offsets. */
8e19ed76
PS
18756 if (attr_form_is_block (&attr))
18757 {
48fbe735 18758 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18759 }
3690dd37 18760 else if (attr_form_is_section_offset (&attr))
8e19ed76 18761 {
4d3c2250 18762 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18763 }
18764 else
18765 {
4d3c2250
KB
18766 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18767 "partial symbol information");
8e19ed76 18768 }
c906108c 18769 break;
c906108c 18770 case DW_AT_external:
48fbe735 18771 is_external = DW_UNSND (&attr);
c906108c
SS
18772 break;
18773 case DW_AT_declaration:
48fbe735 18774 is_declaration = DW_UNSND (&attr);
c906108c
SS
18775 break;
18776 case DW_AT_type:
48fbe735 18777 has_type = 1;
c906108c
SS
18778 break;
18779 case DW_AT_abstract_origin:
18780 case DW_AT_specification:
72bf9492 18781 case DW_AT_extension:
48fbe735
YQ
18782 has_specification = 1;
18783 spec_offset = dwarf2_get_ref_die_offset (&attr);
18784 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18785 || cu->per_cu->is_dwz);
c906108c
SS
18786 break;
18787 case DW_AT_sibling:
18788 /* Ignore absolute siblings, they might point outside of
18789 the current compile unit. */
18790 if (attr.form == DW_FORM_ref_addr)
b98664d3 18791 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18792 else
b9502d3f 18793 {
48fbe735 18794 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18795 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18796 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18797
18798 if (sibling_ptr < info_ptr)
b98664d3 18799 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18800 else if (sibling_ptr > reader->buffer_end)
18801 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18802 else
48fbe735 18803 sibling = sibling_ptr;
b9502d3f 18804 }
c906108c 18805 break;
fa4028e9 18806 case DW_AT_byte_size:
48fbe735 18807 has_byte_size = 1;
fa4028e9 18808 break;
ff908ebf 18809 case DW_AT_const_value:
48fbe735 18810 has_const_value = 1;
ff908ebf 18811 break;
68511cec
CES
18812 case DW_AT_calling_convention:
18813 /* DWARF doesn't provide a way to identify a program's source-level
18814 entry point. DW_AT_calling_convention attributes are only meant
18815 to describe functions' calling conventions.
18816
18817 However, because it's a necessary piece of information in
0c1b455e
TT
18818 Fortran, and before DWARF 4 DW_CC_program was the only
18819 piece of debugging information whose definition refers to
18820 a 'main program' at all, several compilers marked Fortran
18821 main programs with DW_CC_program --- even when those
18822 functions use the standard calling conventions.
18823
18824 Although DWARF now specifies a way to provide this
18825 information, we support this practice for backward
18826 compatibility. */
68511cec 18827 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18828 && cu->language == language_fortran)
48fbe735 18829 main_subprogram = 1;
68511cec 18830 break;
481860b3
GB
18831 case DW_AT_inline:
18832 if (DW_UNSND (&attr) == DW_INL_inlined
18833 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18834 may_be_inlined = 1;
481860b3 18835 break;
95554aad
TT
18836
18837 case DW_AT_import:
48fbe735 18838 if (tag == DW_TAG_imported_unit)
36586728 18839 {
48fbe735
YQ
18840 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18841 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18842 || cu->per_cu->is_dwz);
18843 }
95554aad
TT
18844 break;
18845
0c1b455e 18846 case DW_AT_main_subprogram:
48fbe735 18847 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18848 break;
18849
05caa1d2
TT
18850 case DW_AT_ranges:
18851 {
18852 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18853 but that requires a full DIE, so instead we just
18854 reimplement it. */
18855 int need_ranges_base = tag != DW_TAG_compile_unit;
18856 unsigned int ranges_offset = (DW_UNSND (&attr)
18857 + (need_ranges_base
18858 ? cu->ranges_base
18859 : 0));
18860
18861 /* Value of the DW_AT_ranges attribute is the offset in the
18862 .debug_ranges section. */
18863 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18864 nullptr))
18865 has_pc_info = 1;
18866 }
18867 break;
18868
c906108c
SS
18869 default:
18870 break;
18871 }
18872 }
18873
10d06d82
TT
18874 /* For Ada, if both the name and the linkage name appear, we prefer
18875 the latter. This lets "catch exception" work better, regardless
18876 of the order in which the name and linkage name were emitted.
18877 Really, though, this is just a workaround for the fact that gdb
18878 doesn't store both the name and the linkage name. */
18879 if (cu->language == language_ada && linkage_name != nullptr)
18880 name = linkage_name;
18881
91da1414 18882 if (high_pc_relative)
48fbe735 18883 highpc += lowpc;
91da1414 18884
9373cf26
JK
18885 if (has_low_pc_attr && has_high_pc_attr)
18886 {
18887 /* When using the GNU linker, .gnu.linkonce. sections are used to
18888 eliminate duplicate copies of functions and vtables and such.
18889 The linker will arbitrarily choose one and discard the others.
18890 The AT_*_pc values for such functions refer to local labels in
18891 these sections. If the section from that file was discarded, the
18892 labels are not in the output, so the relocs get a value of 0.
18893 If this is a discarded function, mark the pc bounds as invalid,
18894 so that GDB will ignore it. */
48fbe735 18895 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18896 {
48fbe735 18897 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18898 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18899
b98664d3 18900 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18901 "for DIE at %s [in module %s]"),
48fbe735
YQ
18902 paddress (gdbarch, lowpc),
18903 sect_offset_str (sect_off),
9d8780f0 18904 objfile_name (objfile));
9373cf26
JK
18905 }
18906 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18907 else if (lowpc >= highpc)
9373cf26 18908 {
48fbe735 18909 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18910 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18911
b98664d3 18912 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18913 "for DIE at %s [in module %s]"),
48fbe735
YQ
18914 paddress (gdbarch, lowpc),
18915 paddress (gdbarch, highpc),
18916 sect_offset_str (sect_off),
9c541725 18917 objfile_name (objfile));
9373cf26
JK
18918 }
18919 else
48fbe735 18920 has_pc_info = 1;
9373cf26 18921 }
85cbf3d3 18922
c906108c
SS
18923 return info_ptr;
18924}
18925
72bf9492
DJ
18926/* Find a cached partial DIE at OFFSET in CU. */
18927
d590ff25
YQ
18928struct partial_die_info *
18929dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18930{
18931 struct partial_die_info *lookup_die = NULL;
6f06d47b 18932 struct partial_die_info part_die (sect_off);
72bf9492 18933
9a3c8263 18934 lookup_die = ((struct partial_die_info *)
d590ff25 18935 htab_find_with_hash (partial_dies, &part_die,
9c541725 18936 to_underlying (sect_off)));
72bf9492 18937
72bf9492
DJ
18938 return lookup_die;
18939}
18940
348e048f
DE
18941/* Find a partial DIE at OFFSET, which may or may not be in CU,
18942 except in the case of .debug_types DIEs which do not reference
18943 outside their CU (they do however referencing other types via
55f1336d 18944 DW_FORM_ref_sig8). */
72bf9492 18945
122cf0f2 18946static const struct cu_partial_die_info
9c541725 18947find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18948{
518817b3
SM
18949 struct dwarf2_per_objfile *dwarf2_per_objfile
18950 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18951 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18952 struct dwarf2_per_cu_data *per_cu = NULL;
18953 struct partial_die_info *pd = NULL;
72bf9492 18954
36586728 18955 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18956 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18957 {
d590ff25 18958 pd = cu->find_partial_die (sect_off);
5afb4e99 18959 if (pd != NULL)
fb816e8b 18960 return { cu, pd };
0d99eb77
DE
18961 /* We missed recording what we needed.
18962 Load all dies and try again. */
18963 per_cu = cu->per_cu;
5afb4e99 18964 }
0d99eb77
DE
18965 else
18966 {
18967 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18968 if (cu->per_cu->is_debug_types)
0d99eb77 18969 {
9d8780f0
SM
18970 error (_("Dwarf Error: Type Unit at offset %s contains"
18971 " external reference to offset %s [in module %s].\n"),
18972 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18973 bfd_get_filename (objfile->obfd));
18974 }
9c541725 18975 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18976 dwarf2_per_objfile);
72bf9492 18977
0d99eb77
DE
18978 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18979 load_partial_comp_unit (per_cu);
ae038cb0 18980
0d99eb77 18981 per_cu->cu->last_used = 0;
d590ff25 18982 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18983 }
5afb4e99 18984
dee91e82
DE
18985 /* If we didn't find it, and not all dies have been loaded,
18986 load them all and try again. */
18987
5afb4e99
DJ
18988 if (pd == NULL && per_cu->load_all_dies == 0)
18989 {
5afb4e99 18990 per_cu->load_all_dies = 1;
fd820528
DE
18991
18992 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18993 THIS_CU->cu may already be in use. So we can't just free it and
18994 replace its DIEs with the ones we read in. Instead, we leave those
18995 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18996 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18997 set. */
dee91e82 18998 load_partial_comp_unit (per_cu);
5afb4e99 18999
d590ff25 19000 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19001 }
19002
19003 if (pd == NULL)
19004 internal_error (__FILE__, __LINE__,
9d8780f0 19005 _("could not find partial DIE %s "
3e43a32a 19006 "in cache [from module %s]\n"),
9d8780f0 19007 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19008 return { per_cu->cu, pd };
72bf9492
DJ
19009}
19010
abc72ce4
DE
19011/* See if we can figure out if the class lives in a namespace. We do
19012 this by looking for a member function; its demangled name will
19013 contain namespace info, if there is any. */
19014
19015static void
19016guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19017 struct dwarf2_cu *cu)
19018{
19019 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19020 what template types look like, because the demangler
19021 frequently doesn't give the same name as the debug info. We
19022 could fix this by only using the demangled name to get the
19023 prefix (but see comment in read_structure_type). */
19024
19025 struct partial_die_info *real_pdi;
19026 struct partial_die_info *child_pdi;
19027
19028 /* If this DIE (this DIE's specification, if any) has a parent, then
19029 we should not do this. We'll prepend the parent's fully qualified
19030 name when we create the partial symbol. */
19031
19032 real_pdi = struct_pdi;
19033 while (real_pdi->has_specification)
fb816e8b 19034 {
122cf0f2
AB
19035 auto res = find_partial_die (real_pdi->spec_offset,
19036 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19037 real_pdi = res.pdi;
19038 cu = res.cu;
19039 }
abc72ce4
DE
19040
19041 if (real_pdi->die_parent != NULL)
19042 return;
19043
19044 for (child_pdi = struct_pdi->die_child;
19045 child_pdi != NULL;
19046 child_pdi = child_pdi->die_sibling)
19047 {
19048 if (child_pdi->tag == DW_TAG_subprogram
19049 && child_pdi->linkage_name != NULL)
19050 {
19051 char *actual_class_name
19052 = language_class_name_from_physname (cu->language_defn,
19053 child_pdi->linkage_name);
19054 if (actual_class_name != NULL)
19055 {
518817b3 19056 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19057 struct_pdi->name
021887d8
TT
19058 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19059 actual_class_name);
abc72ce4
DE
19060 xfree (actual_class_name);
19061 }
19062 break;
19063 }
19064 }
19065}
19066
52356b79
YQ
19067void
19068partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19069{
abc72ce4
DE
19070 /* Once we've fixed up a die, there's no point in doing so again.
19071 This also avoids a memory leak if we were to call
19072 guess_partial_die_structure_name multiple times. */
52356b79 19073 if (fixup_called)
abc72ce4
DE
19074 return;
19075
72bf9492
DJ
19076 /* If we found a reference attribute and the DIE has no name, try
19077 to find a name in the referred to DIE. */
19078
52356b79 19079 if (name == NULL && has_specification)
72bf9492
DJ
19080 {
19081 struct partial_die_info *spec_die;
72bf9492 19082
122cf0f2 19083 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19084 spec_die = res.pdi;
19085 cu = res.cu;
72bf9492 19086
52356b79 19087 spec_die->fixup (cu);
72bf9492
DJ
19088
19089 if (spec_die->name)
19090 {
52356b79 19091 name = spec_die->name;
72bf9492
DJ
19092
19093 /* Copy DW_AT_external attribute if it is set. */
19094 if (spec_die->is_external)
52356b79 19095 is_external = spec_die->is_external;
72bf9492
DJ
19096 }
19097 }
19098
19099 /* Set default names for some unnamed DIEs. */
72bf9492 19100
52356b79
YQ
19101 if (name == NULL && tag == DW_TAG_namespace)
19102 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19103
abc72ce4
DE
19104 /* If there is no parent die to provide a namespace, and there are
19105 children, see if we can determine the namespace from their linkage
122d1940 19106 name. */
abc72ce4 19107 if (cu->language == language_cplus
fd5866f6 19108 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19109 && die_parent == NULL
19110 && has_children
19111 && (tag == DW_TAG_class_type
19112 || tag == DW_TAG_structure_type
19113 || tag == DW_TAG_union_type))
19114 guess_partial_die_structure_name (this, cu);
abc72ce4 19115
53832f31
TT
19116 /* GCC might emit a nameless struct or union that has a linkage
19117 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19118 if (name == NULL
19119 && (tag == DW_TAG_class_type
19120 || tag == DW_TAG_interface_type
19121 || tag == DW_TAG_structure_type
19122 || tag == DW_TAG_union_type)
19123 && linkage_name != NULL)
53832f31
TT
19124 {
19125 char *demangled;
19126
52356b79 19127 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19128 if (demangled)
19129 {
96408a79
SA
19130 const char *base;
19131
19132 /* Strip any leading namespaces/classes, keep only the base name.
19133 DW_AT_name for named DIEs does not contain the prefixes. */
19134 base = strrchr (demangled, ':');
19135 if (base && base > demangled && base[-1] == ':')
19136 base++;
19137 else
19138 base = demangled;
19139
518817b3 19140 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19141 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19142 xfree (demangled);
19143 }
19144 }
19145
52356b79 19146 fixup_called = 1;
72bf9492
DJ
19147}
19148
a8329558 19149/* Read an attribute value described by an attribute form. */
c906108c 19150
d521ce57 19151static const gdb_byte *
dee91e82
DE
19152read_attribute_value (const struct die_reader_specs *reader,
19153 struct attribute *attr, unsigned form,
43988095 19154 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19155{
dee91e82 19156 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19157 struct dwarf2_per_objfile *dwarf2_per_objfile
19158 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19159 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19160 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19161 bfd *abfd = reader->abfd;
e7c27a73 19162 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19163 unsigned int bytes_read;
19164 struct dwarf_block *blk;
19165
aead7601 19166 attr->form = (enum dwarf_form) form;
a8329558 19167 switch (form)
c906108c 19168 {
c906108c 19169 case DW_FORM_ref_addr:
ae411497 19170 if (cu->header.version == 2)
4568ecf9 19171 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19172 else
4568ecf9
DE
19173 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19174 &cu->header, &bytes_read);
ae411497
TT
19175 info_ptr += bytes_read;
19176 break;
36586728
TT
19177 case DW_FORM_GNU_ref_alt:
19178 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19179 info_ptr += bytes_read;
19180 break;
ae411497 19181 case DW_FORM_addr:
e7c27a73 19182 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19183 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19184 info_ptr += bytes_read;
c906108c
SS
19185 break;
19186 case DW_FORM_block2:
7b5a2f43 19187 blk = dwarf_alloc_block (cu);
c906108c
SS
19188 blk->size = read_2_bytes (abfd, info_ptr);
19189 info_ptr += 2;
19190 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19191 info_ptr += blk->size;
19192 DW_BLOCK (attr) = blk;
19193 break;
19194 case DW_FORM_block4:
7b5a2f43 19195 blk = dwarf_alloc_block (cu);
c906108c
SS
19196 blk->size = read_4_bytes (abfd, info_ptr);
19197 info_ptr += 4;
19198 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19199 info_ptr += blk->size;
19200 DW_BLOCK (attr) = blk;
19201 break;
19202 case DW_FORM_data2:
19203 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19204 info_ptr += 2;
19205 break;
19206 case DW_FORM_data4:
19207 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19208 info_ptr += 4;
19209 break;
19210 case DW_FORM_data8:
19211 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19212 info_ptr += 8;
19213 break;
0224619f
JK
19214 case DW_FORM_data16:
19215 blk = dwarf_alloc_block (cu);
19216 blk->size = 16;
19217 blk->data = read_n_bytes (abfd, info_ptr, 16);
19218 info_ptr += 16;
19219 DW_BLOCK (attr) = blk;
19220 break;
2dc7f7b3
TT
19221 case DW_FORM_sec_offset:
19222 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19223 info_ptr += bytes_read;
19224 break;
c906108c 19225 case DW_FORM_string:
9b1c24c8 19226 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19227 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19228 info_ptr += bytes_read;
19229 break;
4bdf3d34 19230 case DW_FORM_strp:
36586728
TT
19231 if (!cu->per_cu->is_dwz)
19232 {
ed2dc618
SM
19233 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19234 abfd, info_ptr, cu_header,
36586728
TT
19235 &bytes_read);
19236 DW_STRING_IS_CANONICAL (attr) = 0;
19237 info_ptr += bytes_read;
19238 break;
19239 }
19240 /* FALLTHROUGH */
43988095
JK
19241 case DW_FORM_line_strp:
19242 if (!cu->per_cu->is_dwz)
19243 {
ed2dc618
SM
19244 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19245 abfd, info_ptr,
43988095
JK
19246 cu_header, &bytes_read);
19247 DW_STRING_IS_CANONICAL (attr) = 0;
19248 info_ptr += bytes_read;
19249 break;
19250 }
19251 /* FALLTHROUGH */
36586728
TT
19252 case DW_FORM_GNU_strp_alt:
19253 {
ed2dc618 19254 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19255 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19256 &bytes_read);
19257
ed2dc618
SM
19258 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19259 dwz, str_offset);
36586728
TT
19260 DW_STRING_IS_CANONICAL (attr) = 0;
19261 info_ptr += bytes_read;
19262 }
4bdf3d34 19263 break;
2dc7f7b3 19264 case DW_FORM_exprloc:
c906108c 19265 case DW_FORM_block:
7b5a2f43 19266 blk = dwarf_alloc_block (cu);
c906108c
SS
19267 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19268 info_ptr += bytes_read;
19269 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19270 info_ptr += blk->size;
19271 DW_BLOCK (attr) = blk;
19272 break;
19273 case DW_FORM_block1:
7b5a2f43 19274 blk = dwarf_alloc_block (cu);
c906108c
SS
19275 blk->size = read_1_byte (abfd, info_ptr);
19276 info_ptr += 1;
19277 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19278 info_ptr += blk->size;
19279 DW_BLOCK (attr) = blk;
19280 break;
19281 case DW_FORM_data1:
19282 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19283 info_ptr += 1;
19284 break;
19285 case DW_FORM_flag:
19286 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19287 info_ptr += 1;
19288 break;
2dc7f7b3
TT
19289 case DW_FORM_flag_present:
19290 DW_UNSND (attr) = 1;
19291 break;
c906108c
SS
19292 case DW_FORM_sdata:
19293 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19294 info_ptr += bytes_read;
19295 break;
19296 case DW_FORM_udata:
19297 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19298 info_ptr += bytes_read;
19299 break;
19300 case DW_FORM_ref1:
9c541725 19301 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19302 + read_1_byte (abfd, info_ptr));
c906108c
SS
19303 info_ptr += 1;
19304 break;
19305 case DW_FORM_ref2:
9c541725 19306 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19307 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19308 info_ptr += 2;
19309 break;
19310 case DW_FORM_ref4:
9c541725 19311 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19312 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19313 info_ptr += 4;
19314 break;
613e1657 19315 case DW_FORM_ref8:
9c541725 19316 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19317 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19318 info_ptr += 8;
19319 break;
55f1336d 19320 case DW_FORM_ref_sig8:
ac9ec31b 19321 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19322 info_ptr += 8;
19323 break;
c906108c 19324 case DW_FORM_ref_udata:
9c541725 19325 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19326 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19327 info_ptr += bytes_read;
19328 break;
c906108c 19329 case DW_FORM_indirect:
a8329558
KW
19330 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19331 info_ptr += bytes_read;
43988095
JK
19332 if (form == DW_FORM_implicit_const)
19333 {
19334 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19335 info_ptr += bytes_read;
19336 }
19337 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19338 info_ptr);
19339 break;
19340 case DW_FORM_implicit_const:
19341 DW_SND (attr) = implicit_const;
a8329558 19342 break;
336d760d 19343 case DW_FORM_addrx:
3019eac3
DE
19344 case DW_FORM_GNU_addr_index:
19345 if (reader->dwo_file == NULL)
19346 {
19347 /* For now flag a hard error.
19348 Later we can turn this into a complaint. */
19349 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19350 dwarf_form_name (form),
19351 bfd_get_filename (abfd));
19352 }
19353 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19354 info_ptr += bytes_read;
19355 break;
cf532bd1 19356 case DW_FORM_strx:
15f18d14
AT
19357 case DW_FORM_strx1:
19358 case DW_FORM_strx2:
19359 case DW_FORM_strx3:
19360 case DW_FORM_strx4:
3019eac3
DE
19361 case DW_FORM_GNU_str_index:
19362 if (reader->dwo_file == NULL)
19363 {
19364 /* For now flag a hard error.
19365 Later we can turn this into a complaint if warranted. */
19366 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19367 dwarf_form_name (form),
19368 bfd_get_filename (abfd));
19369 }
19370 {
15f18d14
AT
19371 ULONGEST str_index;
19372 if (form == DW_FORM_strx1)
19373 {
19374 str_index = read_1_byte (abfd, info_ptr);
19375 info_ptr += 1;
19376 }
19377 else if (form == DW_FORM_strx2)
19378 {
19379 str_index = read_2_bytes (abfd, info_ptr);
19380 info_ptr += 2;
19381 }
19382 else if (form == DW_FORM_strx3)
19383 {
19384 str_index = read_3_bytes (abfd, info_ptr);
19385 info_ptr += 3;
19386 }
19387 else if (form == DW_FORM_strx4)
19388 {
19389 str_index = read_4_bytes (abfd, info_ptr);
19390 info_ptr += 4;
19391 }
19392 else
19393 {
19394 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19395 info_ptr += bytes_read;
19396 }
342587c4 19397 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19398 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19399 }
19400 break;
c906108c 19401 default:
8a3fe4f8 19402 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19403 dwarf_form_name (form),
19404 bfd_get_filename (abfd));
c906108c 19405 }
28e94949 19406
36586728 19407 /* Super hack. */
7771576e 19408 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19409 attr->form = DW_FORM_GNU_ref_alt;
19410
28e94949
JB
19411 /* We have seen instances where the compiler tried to emit a byte
19412 size attribute of -1 which ended up being encoded as an unsigned
19413 0xffffffff. Although 0xffffffff is technically a valid size value,
19414 an object of this size seems pretty unlikely so we can relatively
19415 safely treat these cases as if the size attribute was invalid and
19416 treat them as zero by default. */
19417 if (attr->name == DW_AT_byte_size
19418 && form == DW_FORM_data4
19419 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19420 {
19421 complaint
b98664d3 19422 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19423 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19424 DW_UNSND (attr) = 0;
19425 }
28e94949 19426
c906108c
SS
19427 return info_ptr;
19428}
19429
a8329558
KW
19430/* Read an attribute described by an abbreviated attribute. */
19431
d521ce57 19432static const gdb_byte *
dee91e82
DE
19433read_attribute (const struct die_reader_specs *reader,
19434 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19435 const gdb_byte *info_ptr)
a8329558
KW
19436{
19437 attr->name = abbrev->name;
43988095
JK
19438 return read_attribute_value (reader, attr, abbrev->form,
19439 abbrev->implicit_const, info_ptr);
a8329558
KW
19440}
19441
0963b4bd 19442/* Read dwarf information from a buffer. */
c906108c
SS
19443
19444static unsigned int
a1855c1d 19445read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19446{
fe1b8b76 19447 return bfd_get_8 (abfd, buf);
c906108c
SS
19448}
19449
19450static int
a1855c1d 19451read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19452{
fe1b8b76 19453 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19454}
19455
19456static unsigned int
a1855c1d 19457read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19458{
fe1b8b76 19459 return bfd_get_16 (abfd, buf);
c906108c
SS
19460}
19461
21ae7a4d 19462static int
a1855c1d 19463read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19464{
19465 return bfd_get_signed_16 (abfd, buf);
19466}
19467
15f18d14
AT
19468static unsigned int
19469read_3_bytes (bfd *abfd, const gdb_byte *buf)
19470{
19471 unsigned int result = 0;
19472 for (int i = 0; i < 3; ++i)
19473 {
19474 unsigned char byte = bfd_get_8 (abfd, buf);
19475 buf++;
19476 result |= ((unsigned int) byte << (i * 8));
19477 }
19478 return result;
19479}
19480
c906108c 19481static unsigned int
a1855c1d 19482read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19483{
fe1b8b76 19484 return bfd_get_32 (abfd, buf);
c906108c
SS
19485}
19486
21ae7a4d 19487static int
a1855c1d 19488read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19489{
19490 return bfd_get_signed_32 (abfd, buf);
19491}
19492
93311388 19493static ULONGEST
a1855c1d 19494read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19495{
fe1b8b76 19496 return bfd_get_64 (abfd, buf);
c906108c
SS
19497}
19498
19499static CORE_ADDR
d521ce57 19500read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19501 unsigned int *bytes_read)
c906108c 19502{
e7c27a73 19503 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19504 CORE_ADDR retval = 0;
19505
107d2387 19506 if (cu_header->signed_addr_p)
c906108c 19507 {
107d2387
AC
19508 switch (cu_header->addr_size)
19509 {
19510 case 2:
fe1b8b76 19511 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19512 break;
19513 case 4:
fe1b8b76 19514 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19515 break;
19516 case 8:
fe1b8b76 19517 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19518 break;
19519 default:
8e65ff28 19520 internal_error (__FILE__, __LINE__,
e2e0b3e5 19521 _("read_address: bad switch, signed [in module %s]"),
659b0389 19522 bfd_get_filename (abfd));
107d2387
AC
19523 }
19524 }
19525 else
19526 {
19527 switch (cu_header->addr_size)
19528 {
19529 case 2:
fe1b8b76 19530 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19531 break;
19532 case 4:
fe1b8b76 19533 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19534 break;
19535 case 8:
fe1b8b76 19536 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19537 break;
19538 default:
8e65ff28 19539 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19540 _("read_address: bad switch, "
19541 "unsigned [in module %s]"),
659b0389 19542 bfd_get_filename (abfd));
107d2387 19543 }
c906108c 19544 }
64367e0a 19545
107d2387
AC
19546 *bytes_read = cu_header->addr_size;
19547 return retval;
c906108c
SS
19548}
19549
f7ef9339 19550/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19551 specification allows the initial length to take up either 4 bytes
19552 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19553 bytes describe the length and all offsets will be 8 bytes in length
19554 instead of 4.
19555
f7ef9339
KB
19556 An older, non-standard 64-bit format is also handled by this
19557 function. The older format in question stores the initial length
19558 as an 8-byte quantity without an escape value. Lengths greater
19559 than 2^32 aren't very common which means that the initial 4 bytes
19560 is almost always zero. Since a length value of zero doesn't make
19561 sense for the 32-bit format, this initial zero can be considered to
19562 be an escape value which indicates the presence of the older 64-bit
19563 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19564 greater than 4GB. If it becomes necessary to handle lengths
19565 somewhat larger than 4GB, we could allow other small values (such
19566 as the non-sensical values of 1, 2, and 3) to also be used as
19567 escape values indicating the presence of the old format.
f7ef9339 19568
917c78fc
MK
19569 The value returned via bytes_read should be used to increment the
19570 relevant pointer after calling read_initial_length().
c764a876 19571
613e1657
KB
19572 [ Note: read_initial_length() and read_offset() are based on the
19573 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19574 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19575 from:
19576
f7ef9339 19577 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19578
613e1657
KB
19579 This document is only a draft and is subject to change. (So beware.)
19580
f7ef9339 19581 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19582 determined empirically by examining 64-bit ELF files produced by
19583 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19584
19585 - Kevin, July 16, 2002
613e1657
KB
19586 ] */
19587
19588static LONGEST
d521ce57 19589read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19590{
fe1b8b76 19591 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19592
dd373385 19593 if (length == 0xffffffff)
613e1657 19594 {
fe1b8b76 19595 length = bfd_get_64 (abfd, buf + 4);
613e1657 19596 *bytes_read = 12;
613e1657 19597 }
dd373385 19598 else if (length == 0)
f7ef9339 19599 {
dd373385 19600 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19601 length = bfd_get_64 (abfd, buf);
f7ef9339 19602 *bytes_read = 8;
f7ef9339 19603 }
613e1657
KB
19604 else
19605 {
19606 *bytes_read = 4;
613e1657
KB
19607 }
19608
c764a876
DE
19609 return length;
19610}
dd373385 19611
c764a876
DE
19612/* Cover function for read_initial_length.
19613 Returns the length of the object at BUF, and stores the size of the
19614 initial length in *BYTES_READ and stores the size that offsets will be in
19615 *OFFSET_SIZE.
19616 If the initial length size is not equivalent to that specified in
19617 CU_HEADER then issue a complaint.
19618 This is useful when reading non-comp-unit headers. */
dd373385 19619
c764a876 19620static LONGEST
d521ce57 19621read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19622 const struct comp_unit_head *cu_header,
19623 unsigned int *bytes_read,
19624 unsigned int *offset_size)
19625{
19626 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19627
19628 gdb_assert (cu_header->initial_length_size == 4
19629 || cu_header->initial_length_size == 8
19630 || cu_header->initial_length_size == 12);
19631
19632 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19633 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19634
c764a876 19635 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19636 return length;
613e1657
KB
19637}
19638
19639/* Read an offset from the data stream. The size of the offset is
917c78fc 19640 given by cu_header->offset_size. */
613e1657
KB
19641
19642static LONGEST
d521ce57
TT
19643read_offset (bfd *abfd, const gdb_byte *buf,
19644 const struct comp_unit_head *cu_header,
891d2f0b 19645 unsigned int *bytes_read)
c764a876
DE
19646{
19647 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19648
c764a876
DE
19649 *bytes_read = cu_header->offset_size;
19650 return offset;
19651}
19652
19653/* Read an offset from the data stream. */
19654
19655static LONGEST
d521ce57 19656read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19657{
19658 LONGEST retval = 0;
19659
c764a876 19660 switch (offset_size)
613e1657
KB
19661 {
19662 case 4:
fe1b8b76 19663 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19664 break;
19665 case 8:
fe1b8b76 19666 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19667 break;
19668 default:
8e65ff28 19669 internal_error (__FILE__, __LINE__,
c764a876 19670 _("read_offset_1: bad switch [in module %s]"),
659b0389 19671 bfd_get_filename (abfd));
613e1657
KB
19672 }
19673
917c78fc 19674 return retval;
613e1657
KB
19675}
19676
d521ce57
TT
19677static const gdb_byte *
19678read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19679{
19680 /* If the size of a host char is 8 bits, we can return a pointer
19681 to the buffer, otherwise we have to copy the data to a buffer
19682 allocated on the temporary obstack. */
4bdf3d34 19683 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19684 return buf;
c906108c
SS
19685}
19686
d521ce57
TT
19687static const char *
19688read_direct_string (bfd *abfd, const gdb_byte *buf,
19689 unsigned int *bytes_read_ptr)
c906108c
SS
19690{
19691 /* If the size of a host char is 8 bits, we can return a pointer
19692 to the string, otherwise we have to copy the string to a buffer
19693 allocated on the temporary obstack. */
4bdf3d34 19694 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19695 if (*buf == '\0')
19696 {
19697 *bytes_read_ptr = 1;
19698 return NULL;
19699 }
d521ce57
TT
19700 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19701 return (const char *) buf;
4bdf3d34
JJ
19702}
19703
43988095
JK
19704/* Return pointer to string at section SECT offset STR_OFFSET with error
19705 reporting strings FORM_NAME and SECT_NAME. */
19706
d521ce57 19707static const char *
ed2dc618
SM
19708read_indirect_string_at_offset_from (struct objfile *objfile,
19709 bfd *abfd, LONGEST str_offset,
43988095
JK
19710 struct dwarf2_section_info *sect,
19711 const char *form_name,
19712 const char *sect_name)
19713{
ed2dc618 19714 dwarf2_read_section (objfile, sect);
43988095
JK
19715 if (sect->buffer == NULL)
19716 error (_("%s used without %s section [in module %s]"),
19717 form_name, sect_name, bfd_get_filename (abfd));
19718 if (str_offset >= sect->size)
19719 error (_("%s pointing outside of %s section [in module %s]"),
19720 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19721 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19722 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19723 return NULL;
43988095
JK
19724 return (const char *) (sect->buffer + str_offset);
19725}
19726
19727/* Return pointer to string at .debug_str offset STR_OFFSET. */
19728
19729static const char *
ed2dc618
SM
19730read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19731 bfd *abfd, LONGEST str_offset)
43988095 19732{
ed2dc618
SM
19733 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19734 abfd, str_offset,
43988095
JK
19735 &dwarf2_per_objfile->str,
19736 "DW_FORM_strp", ".debug_str");
19737}
19738
19739/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19740
19741static const char *
ed2dc618
SM
19742read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19743 bfd *abfd, LONGEST str_offset)
43988095 19744{
ed2dc618
SM
19745 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19746 abfd, str_offset,
43988095
JK
19747 &dwarf2_per_objfile->line_str,
19748 "DW_FORM_line_strp",
19749 ".debug_line_str");
c906108c
SS
19750}
19751
36586728
TT
19752/* Read a string at offset STR_OFFSET in the .debug_str section from
19753 the .dwz file DWZ. Throw an error if the offset is too large. If
19754 the string consists of a single NUL byte, return NULL; otherwise
19755 return a pointer to the string. */
19756
d521ce57 19757static const char *
ed2dc618
SM
19758read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19759 LONGEST str_offset)
36586728 19760{
ed2dc618 19761 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19762
19763 if (dwz->str.buffer == NULL)
19764 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19765 "section [in module %s]"),
00f93c44 19766 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19767 if (str_offset >= dwz->str.size)
19768 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19769 ".debug_str section [in module %s]"),
00f93c44 19770 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19771 gdb_assert (HOST_CHAR_BIT == 8);
19772 if (dwz->str.buffer[str_offset] == '\0')
19773 return NULL;
d521ce57 19774 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19775}
19776
43988095
JK
19777/* Return pointer to string at .debug_str offset as read from BUF.
19778 BUF is assumed to be in a compilation unit described by CU_HEADER.
19779 Return *BYTES_READ_PTR count of bytes read from BUF. */
19780
d521ce57 19781static const char *
ed2dc618
SM
19782read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19783 const gdb_byte *buf,
cf2c3c16
TT
19784 const struct comp_unit_head *cu_header,
19785 unsigned int *bytes_read_ptr)
19786{
19787 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19788
ed2dc618 19789 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19790}
19791
43988095
JK
19792/* Return pointer to string at .debug_line_str offset as read from BUF.
19793 BUF is assumed to be in a compilation unit described by CU_HEADER.
19794 Return *BYTES_READ_PTR count of bytes read from BUF. */
19795
19796static const char *
ed2dc618
SM
19797read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19798 bfd *abfd, const gdb_byte *buf,
43988095
JK
19799 const struct comp_unit_head *cu_header,
19800 unsigned int *bytes_read_ptr)
19801{
19802 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19803
ed2dc618
SM
19804 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19805 str_offset);
43988095
JK
19806}
19807
19808ULONGEST
d521ce57 19809read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19810 unsigned int *bytes_read_ptr)
c906108c 19811{
12df843f 19812 ULONGEST result;
ce5d95e1 19813 unsigned int num_read;
870f88f7 19814 int shift;
c906108c
SS
19815 unsigned char byte;
19816
19817 result = 0;
19818 shift = 0;
19819 num_read = 0;
c906108c
SS
19820 while (1)
19821 {
fe1b8b76 19822 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19823 buf++;
19824 num_read++;
12df843f 19825 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19826 if ((byte & 128) == 0)
19827 {
19828 break;
19829 }
19830 shift += 7;
19831 }
19832 *bytes_read_ptr = num_read;
19833 return result;
19834}
19835
12df843f 19836static LONGEST
d521ce57
TT
19837read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19838 unsigned int *bytes_read_ptr)
c906108c 19839{
4dd1b460 19840 ULONGEST result;
870f88f7 19841 int shift, num_read;
c906108c
SS
19842 unsigned char byte;
19843
19844 result = 0;
19845 shift = 0;
c906108c 19846 num_read = 0;
c906108c
SS
19847 while (1)
19848 {
fe1b8b76 19849 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19850 buf++;
19851 num_read++;
4dd1b460 19852 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19853 shift += 7;
19854 if ((byte & 128) == 0)
19855 {
19856 break;
19857 }
19858 }
77e0b926 19859 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 19860 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
19861 *bytes_read_ptr = num_read;
19862 return result;
19863}
19864
3019eac3
DE
19865/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19866 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19867 ADDR_SIZE is the size of addresses from the CU header. */
19868
19869static CORE_ADDR
ed2dc618
SM
19870read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19871 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19872{
19873 struct objfile *objfile = dwarf2_per_objfile->objfile;
19874 bfd *abfd = objfile->obfd;
19875 const gdb_byte *info_ptr;
19876
19877 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19878 if (dwarf2_per_objfile->addr.buffer == NULL)
19879 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19880 objfile_name (objfile));
3019eac3
DE
19881 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19882 error (_("DW_FORM_addr_index pointing outside of "
19883 ".debug_addr section [in module %s]"),
4262abfb 19884 objfile_name (objfile));
3019eac3
DE
19885 info_ptr = (dwarf2_per_objfile->addr.buffer
19886 + addr_base + addr_index * addr_size);
19887 if (addr_size == 4)
19888 return bfd_get_32 (abfd, info_ptr);
19889 else
19890 return bfd_get_64 (abfd, info_ptr);
19891}
19892
19893/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19894
19895static CORE_ADDR
19896read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19897{
518817b3
SM
19898 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19899 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19900}
19901
19902/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19903
19904static CORE_ADDR
d521ce57 19905read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19906 unsigned int *bytes_read)
19907{
518817b3 19908 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19909 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19910
19911 return read_addr_index (cu, addr_index);
19912}
19913
19914/* Data structure to pass results from dwarf2_read_addr_index_reader
19915 back to dwarf2_read_addr_index. */
19916
19917struct dwarf2_read_addr_index_data
19918{
19919 ULONGEST addr_base;
19920 int addr_size;
19921};
19922
19923/* die_reader_func for dwarf2_read_addr_index. */
19924
19925static void
19926dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19927 const gdb_byte *info_ptr,
3019eac3
DE
19928 struct die_info *comp_unit_die,
19929 int has_children,
19930 void *data)
19931{
19932 struct dwarf2_cu *cu = reader->cu;
19933 struct dwarf2_read_addr_index_data *aidata =
19934 (struct dwarf2_read_addr_index_data *) data;
19935
19936 aidata->addr_base = cu->addr_base;
19937 aidata->addr_size = cu->header.addr_size;
19938}
19939
19940/* Given an index in .debug_addr, fetch the value.
19941 NOTE: This can be called during dwarf expression evaluation,
19942 long after the debug information has been read, and thus per_cu->cu
19943 may no longer exist. */
19944
19945CORE_ADDR
19946dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19947 unsigned int addr_index)
19948{
ed2dc618 19949 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19950 struct dwarf2_cu *cu = per_cu->cu;
19951 ULONGEST addr_base;
19952 int addr_size;
19953
3019eac3
DE
19954 /* We need addr_base and addr_size.
19955 If we don't have PER_CU->cu, we have to get it.
19956 Nasty, but the alternative is storing the needed info in PER_CU,
19957 which at this point doesn't seem justified: it's not clear how frequently
19958 it would get used and it would increase the size of every PER_CU.
19959 Entry points like dwarf2_per_cu_addr_size do a similar thing
19960 so we're not in uncharted territory here.
19961 Alas we need to be a bit more complicated as addr_base is contained
19962 in the DIE.
19963
19964 We don't need to read the entire CU(/TU).
19965 We just need the header and top level die.
a1b64ce1 19966
3019eac3 19967 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19968 For now we skip this optimization. */
3019eac3
DE
19969
19970 if (cu != NULL)
19971 {
19972 addr_base = cu->addr_base;
19973 addr_size = cu->header.addr_size;
19974 }
19975 else
19976 {
19977 struct dwarf2_read_addr_index_data aidata;
19978
a1b64ce1
DE
19979 /* Note: We can't use init_cutu_and_read_dies_simple here,
19980 we need addr_base. */
58f0c718 19981 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 19982 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19983 addr_base = aidata.addr_base;
19984 addr_size = aidata.addr_size;
19985 }
19986
ed2dc618
SM
19987 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19988 addr_size);
3019eac3
DE
19989}
19990
cf532bd1 19991/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 19992 This is only used by the Fission support. */
3019eac3 19993
d521ce57 19994static const char *
342587c4 19995read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19996{
ed2dc618 19997 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19998 struct dwarf2_per_objfile *dwarf2_per_objfile
19999 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20000 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20001 const char *objf_name = objfile_name (objfile);
3019eac3 20002 bfd *abfd = objfile->obfd;
73869dc2
DE
20003 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20004 struct dwarf2_section_info *str_offsets_section =
20005 &reader->dwo_file->sections.str_offsets;
d521ce57 20006 const gdb_byte *info_ptr;
3019eac3 20007 ULONGEST str_offset;
cf532bd1 20008 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20009
73869dc2
DE
20010 dwarf2_read_section (objfile, str_section);
20011 dwarf2_read_section (objfile, str_offsets_section);
20012 if (str_section->buffer == NULL)
57d63ce2 20013 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20014 " in CU at offset %s [in module %s]"),
20015 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20016 if (str_offsets_section->buffer == NULL)
57d63ce2 20017 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20018 " in CU at offset %s [in module %s]"),
20019 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20020 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20021 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20022 " section in CU at offset %s [in module %s]"),
20023 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20024 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20025 + str_index * cu->header.offset_size);
20026 if (cu->header.offset_size == 4)
20027 str_offset = bfd_get_32 (abfd, info_ptr);
20028 else
20029 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20030 if (str_offset >= str_section->size)
57d63ce2 20031 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20032 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20033 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20034 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20035}
20036
3019eac3
DE
20037/* Return the length of an LEB128 number in BUF. */
20038
20039static int
20040leb128_size (const gdb_byte *buf)
20041{
20042 const gdb_byte *begin = buf;
20043 gdb_byte byte;
20044
20045 while (1)
20046 {
20047 byte = *buf++;
20048 if ((byte & 128) == 0)
20049 return buf - begin;
20050 }
20051}
20052
c906108c 20053static void
e142c38c 20054set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20055{
20056 switch (lang)
20057 {
20058 case DW_LANG_C89:
76bee0cc 20059 case DW_LANG_C99:
0cfd832f 20060 case DW_LANG_C11:
c906108c 20061 case DW_LANG_C:
d1be3247 20062 case DW_LANG_UPC:
e142c38c 20063 cu->language = language_c;
c906108c 20064 break;
9c37b5ae 20065 case DW_LANG_Java:
c906108c 20066 case DW_LANG_C_plus_plus:
0cfd832f
MW
20067 case DW_LANG_C_plus_plus_11:
20068 case DW_LANG_C_plus_plus_14:
e142c38c 20069 cu->language = language_cplus;
c906108c 20070 break;
6aecb9c2
JB
20071 case DW_LANG_D:
20072 cu->language = language_d;
20073 break;
c906108c
SS
20074 case DW_LANG_Fortran77:
20075 case DW_LANG_Fortran90:
b21b22e0 20076 case DW_LANG_Fortran95:
f7de9aab
MW
20077 case DW_LANG_Fortran03:
20078 case DW_LANG_Fortran08:
e142c38c 20079 cu->language = language_fortran;
c906108c 20080 break;
a766d390
DE
20081 case DW_LANG_Go:
20082 cu->language = language_go;
20083 break;
c906108c 20084 case DW_LANG_Mips_Assembler:
e142c38c 20085 cu->language = language_asm;
c906108c
SS
20086 break;
20087 case DW_LANG_Ada83:
8aaf0b47 20088 case DW_LANG_Ada95:
bc5f45f8
JB
20089 cu->language = language_ada;
20090 break;
72019c9c
GM
20091 case DW_LANG_Modula2:
20092 cu->language = language_m2;
20093 break;
fe8e67fd
PM
20094 case DW_LANG_Pascal83:
20095 cu->language = language_pascal;
20096 break;
22566fbd
DJ
20097 case DW_LANG_ObjC:
20098 cu->language = language_objc;
20099 break;
c44af4eb
TT
20100 case DW_LANG_Rust:
20101 case DW_LANG_Rust_old:
20102 cu->language = language_rust;
20103 break;
c906108c
SS
20104 case DW_LANG_Cobol74:
20105 case DW_LANG_Cobol85:
c906108c 20106 default:
e142c38c 20107 cu->language = language_minimal;
c906108c
SS
20108 break;
20109 }
e142c38c 20110 cu->language_defn = language_def (cu->language);
c906108c
SS
20111}
20112
20113/* Return the named attribute or NULL if not there. */
20114
20115static struct attribute *
e142c38c 20116dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20117{
a48e046c 20118 for (;;)
c906108c 20119 {
a48e046c
TT
20120 unsigned int i;
20121 struct attribute *spec = NULL;
20122
20123 for (i = 0; i < die->num_attrs; ++i)
20124 {
20125 if (die->attrs[i].name == name)
20126 return &die->attrs[i];
20127 if (die->attrs[i].name == DW_AT_specification
20128 || die->attrs[i].name == DW_AT_abstract_origin)
20129 spec = &die->attrs[i];
20130 }
20131
20132 if (!spec)
20133 break;
c906108c 20134
f2f0e013 20135 die = follow_die_ref (die, spec, &cu);
f2f0e013 20136 }
c5aa993b 20137
c906108c
SS
20138 return NULL;
20139}
20140
348e048f
DE
20141/* Return the named attribute or NULL if not there,
20142 but do not follow DW_AT_specification, etc.
20143 This is for use in contexts where we're reading .debug_types dies.
20144 Following DW_AT_specification, DW_AT_abstract_origin will take us
20145 back up the chain, and we want to go down. */
20146
20147static struct attribute *
45e58e77 20148dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20149{
20150 unsigned int i;
20151
20152 for (i = 0; i < die->num_attrs; ++i)
20153 if (die->attrs[i].name == name)
20154 return &die->attrs[i];
20155
20156 return NULL;
20157}
20158
7d45c7c3
KB
20159/* Return the string associated with a string-typed attribute, or NULL if it
20160 is either not found or is of an incorrect type. */
20161
20162static const char *
20163dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20164{
20165 struct attribute *attr;
20166 const char *str = NULL;
20167
20168 attr = dwarf2_attr (die, name, cu);
20169
20170 if (attr != NULL)
20171 {
43988095 20172 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20173 || attr->form == DW_FORM_string
cf532bd1 20174 || attr->form == DW_FORM_strx
8fe0f950
AT
20175 || attr->form == DW_FORM_strx1
20176 || attr->form == DW_FORM_strx2
20177 || attr->form == DW_FORM_strx3
20178 || attr->form == DW_FORM_strx4
b3340438 20179 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20180 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20181 str = DW_STRING (attr);
20182 else
b98664d3 20183 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20184 "DIE at %s in module %s"),
20185 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20186 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20187 }
20188
20189 return str;
20190}
20191
a084a2a6
AT
20192/* Return the dwo name or NULL if not present. If present, it is in either
20193 DW_AT_GNU_dwo_name or DW_AT_dwo_name atrribute. */
20194static const char *
20195dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20196{
20197 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20198 if (dwo_name == nullptr)
20199 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20200 return dwo_name;
20201}
20202
05cf31d1
JB
20203/* Return non-zero iff the attribute NAME is defined for the given DIE,
20204 and holds a non-zero value. This function should only be used for
2dc7f7b3 20205 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20206
20207static int
20208dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20209{
20210 struct attribute *attr = dwarf2_attr (die, name, cu);
20211
20212 return (attr && DW_UNSND (attr));
20213}
20214
3ca72b44 20215static int
e142c38c 20216die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20217{
05cf31d1
JB
20218 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20219 which value is non-zero. However, we have to be careful with
20220 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20221 (via dwarf2_flag_true_p) follows this attribute. So we may
20222 end up accidently finding a declaration attribute that belongs
20223 to a different DIE referenced by the specification attribute,
20224 even though the given DIE does not have a declaration attribute. */
20225 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20226 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20227}
20228
63d06c5c 20229/* Return the die giving the specification for DIE, if there is
f2f0e013 20230 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20231 containing the return value on output. If there is no
20232 specification, but there is an abstract origin, that is
20233 returned. */
63d06c5c
DC
20234
20235static struct die_info *
f2f0e013 20236die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20237{
f2f0e013
DJ
20238 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20239 *spec_cu);
63d06c5c 20240
edb3359d
DJ
20241 if (spec_attr == NULL)
20242 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20243
63d06c5c
DC
20244 if (spec_attr == NULL)
20245 return NULL;
20246 else
f2f0e013 20247 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20248}
c906108c 20249
527f3840
JK
20250/* Stub for free_line_header to match void * callback types. */
20251
20252static void
20253free_line_header_voidp (void *arg)
20254{
9a3c8263 20255 struct line_header *lh = (struct line_header *) arg;
527f3840 20256
fff8551c 20257 delete lh;
527f3840
JK
20258}
20259
fff8551c
PA
20260void
20261line_header::add_include_dir (const char *include_dir)
c906108c 20262{
27e0867f 20263 if (dwarf_line_debug >= 2)
fff8551c
PA
20264 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20265 include_dirs.size () + 1, include_dir);
27e0867f 20266
fff8551c 20267 include_dirs.push_back (include_dir);
debd256d 20268}
6e70227d 20269
fff8551c
PA
20270void
20271line_header::add_file_name (const char *name,
ecfb656c 20272 dir_index d_index,
fff8551c
PA
20273 unsigned int mod_time,
20274 unsigned int length)
debd256d 20275{
27e0867f
DE
20276 if (dwarf_line_debug >= 2)
20277 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20278 (unsigned) file_names.size () + 1, name);
27e0867f 20279
ecfb656c 20280 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20281}
6e70227d 20282
83769d0b 20283/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20284
20285static struct dwarf2_section_info *
20286get_debug_line_section (struct dwarf2_cu *cu)
20287{
20288 struct dwarf2_section_info *section;
518817b3
SM
20289 struct dwarf2_per_objfile *dwarf2_per_objfile
20290 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20291
20292 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20293 DWO file. */
20294 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20295 section = &cu->dwo_unit->dwo_file->sections.line;
20296 else if (cu->per_cu->is_dwz)
20297 {
ed2dc618 20298 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20299
20300 section = &dwz->line;
20301 }
20302 else
20303 section = &dwarf2_per_objfile->line;
20304
20305 return section;
20306}
20307
43988095
JK
20308/* Read directory or file name entry format, starting with byte of
20309 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20310 entries count and the entries themselves in the described entry
20311 format. */
20312
20313static void
ed2dc618
SM
20314read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20315 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20316 struct line_header *lh,
20317 const struct comp_unit_head *cu_header,
20318 void (*callback) (struct line_header *lh,
20319 const char *name,
ecfb656c 20320 dir_index d_index,
43988095
JK
20321 unsigned int mod_time,
20322 unsigned int length))
20323{
20324 gdb_byte format_count, formati;
20325 ULONGEST data_count, datai;
20326 const gdb_byte *buf = *bufp;
20327 const gdb_byte *format_header_data;
43988095
JK
20328 unsigned int bytes_read;
20329
20330 format_count = read_1_byte (abfd, buf);
20331 buf += 1;
20332 format_header_data = buf;
20333 for (formati = 0; formati < format_count; formati++)
20334 {
20335 read_unsigned_leb128 (abfd, buf, &bytes_read);
20336 buf += bytes_read;
20337 read_unsigned_leb128 (abfd, buf, &bytes_read);
20338 buf += bytes_read;
20339 }
20340
20341 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20342 buf += bytes_read;
20343 for (datai = 0; datai < data_count; datai++)
20344 {
20345 const gdb_byte *format = format_header_data;
20346 struct file_entry fe;
20347
43988095
JK
20348 for (formati = 0; formati < format_count; formati++)
20349 {
ecfb656c 20350 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20351 format += bytes_read;
43988095 20352
ecfb656c 20353 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20354 format += bytes_read;
ecfb656c
PA
20355
20356 gdb::optional<const char *> string;
20357 gdb::optional<unsigned int> uint;
20358
43988095
JK
20359 switch (form)
20360 {
20361 case DW_FORM_string:
ecfb656c 20362 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20363 buf += bytes_read;
20364 break;
20365
20366 case DW_FORM_line_strp:
ed2dc618
SM
20367 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20368 abfd, buf,
ecfb656c
PA
20369 cu_header,
20370 &bytes_read));
43988095
JK
20371 buf += bytes_read;
20372 break;
20373
20374 case DW_FORM_data1:
ecfb656c 20375 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20376 buf += 1;
20377 break;
20378
20379 case DW_FORM_data2:
ecfb656c 20380 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20381 buf += 2;
20382 break;
20383
20384 case DW_FORM_data4:
ecfb656c 20385 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20386 buf += 4;
20387 break;
20388
20389 case DW_FORM_data8:
ecfb656c 20390 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20391 buf += 8;
20392 break;
20393
20394 case DW_FORM_udata:
ecfb656c 20395 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20396 buf += bytes_read;
20397 break;
20398
20399 case DW_FORM_block:
20400 /* It is valid only for DW_LNCT_timestamp which is ignored by
20401 current GDB. */
20402 break;
20403 }
ecfb656c
PA
20404
20405 switch (content_type)
20406 {
20407 case DW_LNCT_path:
20408 if (string.has_value ())
20409 fe.name = *string;
20410 break;
20411 case DW_LNCT_directory_index:
20412 if (uint.has_value ())
20413 fe.d_index = (dir_index) *uint;
20414 break;
20415 case DW_LNCT_timestamp:
20416 if (uint.has_value ())
20417 fe.mod_time = *uint;
20418 break;
20419 case DW_LNCT_size:
20420 if (uint.has_value ())
20421 fe.length = *uint;
20422 break;
20423 case DW_LNCT_MD5:
20424 break;
20425 default:
b98664d3 20426 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20427 pulongest (content_type));
20428 }
43988095
JK
20429 }
20430
ecfb656c 20431 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20432 }
20433
20434 *bufp = buf;
20435}
20436
debd256d 20437/* Read the statement program header starting at OFFSET in
3019eac3 20438 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20439 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20440 Returns NULL if there is a problem reading the header, e.g., if it
20441 has a version we don't understand.
debd256d
JB
20442
20443 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20444 the returned object point into the dwarf line section buffer,
20445 and must not be freed. */
ae2de4f8 20446
fff8551c 20447static line_header_up
9c541725 20448dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20449{
d521ce57 20450 const gdb_byte *line_ptr;
c764a876 20451 unsigned int bytes_read, offset_size;
debd256d 20452 int i;
d521ce57 20453 const char *cur_dir, *cur_file;
3019eac3
DE
20454 struct dwarf2_section_info *section;
20455 bfd *abfd;
518817b3
SM
20456 struct dwarf2_per_objfile *dwarf2_per_objfile
20457 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20458
36586728 20459 section = get_debug_line_section (cu);
3019eac3
DE
20460 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20461 if (section->buffer == NULL)
debd256d 20462 {
3019eac3 20463 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20464 complaint (_("missing .debug_line.dwo section"));
3019eac3 20465 else
b98664d3 20466 complaint (_("missing .debug_line section"));
debd256d
JB
20467 return 0;
20468 }
20469
fceca515
DE
20470 /* We can't do this until we know the section is non-empty.
20471 Only then do we know we have such a section. */
a32a8923 20472 abfd = get_section_bfd_owner (section);
fceca515 20473
a738430d
MK
20474 /* Make sure that at least there's room for the total_length field.
20475 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20476 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20477 {
4d3c2250 20478 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20479 return 0;
20480 }
20481
fff8551c 20482 line_header_up lh (new line_header ());
debd256d 20483
9c541725 20484 lh->sect_off = sect_off;
527f3840
JK
20485 lh->offset_in_dwz = cu->per_cu->is_dwz;
20486
9c541725 20487 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20488
a738430d 20489 /* Read in the header. */
6e70227d 20490 lh->total_length =
c764a876
DE
20491 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20492 &bytes_read, &offset_size);
debd256d 20493 line_ptr += bytes_read;
3019eac3 20494 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20495 {
4d3c2250 20496 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20497 return 0;
20498 }
20499 lh->statement_program_end = line_ptr + lh->total_length;
20500 lh->version = read_2_bytes (abfd, line_ptr);
20501 line_ptr += 2;
43988095 20502 if (lh->version > 5)
cd366ee8
DE
20503 {
20504 /* This is a version we don't understand. The format could have
20505 changed in ways we don't handle properly so just punt. */
b98664d3 20506 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20507 return NULL;
20508 }
43988095
JK
20509 if (lh->version >= 5)
20510 {
20511 gdb_byte segment_selector_size;
20512
20513 /* Skip address size. */
20514 read_1_byte (abfd, line_ptr);
20515 line_ptr += 1;
20516
20517 segment_selector_size = read_1_byte (abfd, line_ptr);
20518 line_ptr += 1;
20519 if (segment_selector_size != 0)
20520 {
b98664d3 20521 complaint (_("unsupported segment selector size %u "
43988095
JK
20522 "in .debug_line section"),
20523 segment_selector_size);
20524 return NULL;
20525 }
20526 }
c764a876
DE
20527 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20528 line_ptr += offset_size;
debd256d
JB
20529 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20530 line_ptr += 1;
2dc7f7b3
TT
20531 if (lh->version >= 4)
20532 {
20533 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20534 line_ptr += 1;
20535 }
20536 else
20537 lh->maximum_ops_per_instruction = 1;
20538
20539 if (lh->maximum_ops_per_instruction == 0)
20540 {
20541 lh->maximum_ops_per_instruction = 1;
b98664d3 20542 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20543 "in `.debug_line' section"));
2dc7f7b3
TT
20544 }
20545
debd256d
JB
20546 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20547 line_ptr += 1;
20548 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20549 line_ptr += 1;
20550 lh->line_range = read_1_byte (abfd, line_ptr);
20551 line_ptr += 1;
20552 lh->opcode_base = read_1_byte (abfd, line_ptr);
20553 line_ptr += 1;
fff8551c 20554 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20555
20556 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20557 for (i = 1; i < lh->opcode_base; ++i)
20558 {
20559 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20560 line_ptr += 1;
20561 }
20562
43988095 20563 if (lh->version >= 5)
debd256d 20564 {
43988095 20565 /* Read directory table. */
ed2dc618
SM
20566 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20567 &cu->header,
b926417a 20568 [] (struct line_header *header, const char *name,
ecfb656c 20569 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20570 unsigned int length)
20571 {
b926417a 20572 header->add_include_dir (name);
fff8551c 20573 });
debd256d 20574
43988095 20575 /* Read file name table. */
ed2dc618
SM
20576 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20577 &cu->header,
b926417a 20578 [] (struct line_header *header, const char *name,
ecfb656c 20579 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20580 unsigned int length)
20581 {
b926417a 20582 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20583 });
43988095
JK
20584 }
20585 else
debd256d 20586 {
43988095
JK
20587 /* Read directory table. */
20588 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20589 {
20590 line_ptr += bytes_read;
fff8551c 20591 lh->add_include_dir (cur_dir);
43988095 20592 }
debd256d
JB
20593 line_ptr += bytes_read;
20594
43988095
JK
20595 /* Read file name table. */
20596 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20597 {
ecfb656c
PA
20598 unsigned int mod_time, length;
20599 dir_index d_index;
43988095
JK
20600
20601 line_ptr += bytes_read;
ecfb656c 20602 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20603 line_ptr += bytes_read;
20604 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20605 line_ptr += bytes_read;
20606 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20607 line_ptr += bytes_read;
20608
ecfb656c 20609 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20610 }
20611 line_ptr += bytes_read;
debd256d 20612 }
6e70227d 20613 lh->statement_program_start = line_ptr;
debd256d 20614
3019eac3 20615 if (line_ptr > (section->buffer + section->size))
b98664d3 20616 complaint (_("line number info header doesn't "
3e43a32a 20617 "fit in `.debug_line' section"));
debd256d 20618
debd256d
JB
20619 return lh;
20620}
c906108c 20621
c6da4cef
DE
20622/* Subroutine of dwarf_decode_lines to simplify it.
20623 Return the file name of the psymtab for included file FILE_INDEX
20624 in line header LH of PST.
20625 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20626 If space for the result is malloc'd, *NAME_HOLDER will be set.
20627 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20628
d521ce57 20629static const char *
c6da4cef
DE
20630psymtab_include_file_name (const struct line_header *lh, int file_index,
20631 const struct partial_symtab *pst,
c89b44cd
TT
20632 const char *comp_dir,
20633 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20634{
8c43009f 20635 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20636 const char *include_name = fe.name;
20637 const char *include_name_to_compare = include_name;
72b9f47f 20638 const char *pst_filename;
c6da4cef
DE
20639 int file_is_pst;
20640
8c43009f 20641 const char *dir_name = fe.include_dir (lh);
c6da4cef 20642
c89b44cd 20643 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20644 if (!IS_ABSOLUTE_PATH (include_name)
20645 && (dir_name != NULL || comp_dir != NULL))
20646 {
20647 /* Avoid creating a duplicate psymtab for PST.
20648 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20649 Before we do the comparison, however, we need to account
20650 for DIR_NAME and COMP_DIR.
20651 First prepend dir_name (if non-NULL). If we still don't
20652 have an absolute path prepend comp_dir (if non-NULL).
20653 However, the directory we record in the include-file's
20654 psymtab does not contain COMP_DIR (to match the
20655 corresponding symtab(s)).
20656
20657 Example:
20658
20659 bash$ cd /tmp
20660 bash$ gcc -g ./hello.c
20661 include_name = "hello.c"
20662 dir_name = "."
20663 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20664 DW_AT_name = "./hello.c"
20665
20666 */
c6da4cef
DE
20667
20668 if (dir_name != NULL)
20669 {
c89b44cd
TT
20670 name_holder->reset (concat (dir_name, SLASH_STRING,
20671 include_name, (char *) NULL));
20672 include_name = name_holder->get ();
c6da4cef 20673 include_name_to_compare = include_name;
c6da4cef
DE
20674 }
20675 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20676 {
c89b44cd
TT
20677 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20678 include_name, (char *) NULL));
20679 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20680 }
20681 }
20682
20683 pst_filename = pst->filename;
c89b44cd 20684 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20685 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20686 {
c89b44cd
TT
20687 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20688 pst_filename, (char *) NULL));
20689 pst_filename = copied_name.get ();
c6da4cef
DE
20690 }
20691
1e3fad37 20692 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20693
c6da4cef
DE
20694 if (file_is_pst)
20695 return NULL;
20696 return include_name;
20697}
20698
d9b3de22
DE
20699/* State machine to track the state of the line number program. */
20700
6f77053d 20701class lnp_state_machine
d9b3de22 20702{
6f77053d
PA
20703public:
20704 /* Initialize a machine state for the start of a line number
20705 program. */
804d2729
TT
20706 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20707 bool record_lines_p);
6f77053d 20708
8c43009f
PA
20709 file_entry *current_file ()
20710 {
20711 /* lh->file_names is 0-based, but the file name numbers in the
20712 statement program are 1-based. */
6f77053d
PA
20713 return m_line_header->file_name_at (m_file);
20714 }
20715
20716 /* Record the line in the state machine. END_SEQUENCE is true if
20717 we're processing the end of a sequence. */
20718 void record_line (bool end_sequence);
20719
7ab6656f
OJ
20720 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20721 nop-out rest of the lines in this sequence. */
6f77053d
PA
20722 void check_line_address (struct dwarf2_cu *cu,
20723 const gdb_byte *line_ptr,
7ab6656f 20724 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20725
20726 void handle_set_discriminator (unsigned int discriminator)
20727 {
20728 m_discriminator = discriminator;
20729 m_line_has_non_zero_discriminator |= discriminator != 0;
20730 }
20731
20732 /* Handle DW_LNE_set_address. */
20733 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20734 {
20735 m_op_index = 0;
20736 address += baseaddr;
20737 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20738 }
20739
20740 /* Handle DW_LNS_advance_pc. */
20741 void handle_advance_pc (CORE_ADDR adjust);
20742
20743 /* Handle a special opcode. */
20744 void handle_special_opcode (unsigned char op_code);
20745
20746 /* Handle DW_LNS_advance_line. */
20747 void handle_advance_line (int line_delta)
20748 {
20749 advance_line (line_delta);
20750 }
20751
20752 /* Handle DW_LNS_set_file. */
20753 void handle_set_file (file_name_index file);
20754
20755 /* Handle DW_LNS_negate_stmt. */
20756 void handle_negate_stmt ()
20757 {
20758 m_is_stmt = !m_is_stmt;
20759 }
20760
20761 /* Handle DW_LNS_const_add_pc. */
20762 void handle_const_add_pc ();
20763
20764 /* Handle DW_LNS_fixed_advance_pc. */
20765 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20766 {
20767 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20768 m_op_index = 0;
20769 }
20770
20771 /* Handle DW_LNS_copy. */
20772 void handle_copy ()
20773 {
20774 record_line (false);
20775 m_discriminator = 0;
20776 }
20777
20778 /* Handle DW_LNE_end_sequence. */
20779 void handle_end_sequence ()
20780 {
804d2729 20781 m_currently_recording_lines = true;
6f77053d
PA
20782 }
20783
20784private:
20785 /* Advance the line by LINE_DELTA. */
20786 void advance_line (int line_delta)
20787 {
20788 m_line += line_delta;
20789
20790 if (line_delta != 0)
20791 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20792 }
20793
804d2729
TT
20794 struct dwarf2_cu *m_cu;
20795
6f77053d
PA
20796 gdbarch *m_gdbarch;
20797
20798 /* True if we're recording lines.
20799 Otherwise we're building partial symtabs and are just interested in
20800 finding include files mentioned by the line number program. */
20801 bool m_record_lines_p;
20802
8c43009f 20803 /* The line number header. */
6f77053d 20804 line_header *m_line_header;
8c43009f 20805
6f77053d
PA
20806 /* These are part of the standard DWARF line number state machine,
20807 and initialized according to the DWARF spec. */
d9b3de22 20808
6f77053d 20809 unsigned char m_op_index = 0;
8c43009f 20810 /* The line table index (1-based) of the current file. */
6f77053d
PA
20811 file_name_index m_file = (file_name_index) 1;
20812 unsigned int m_line = 1;
20813
20814 /* These are initialized in the constructor. */
20815
20816 CORE_ADDR m_address;
20817 bool m_is_stmt;
20818 unsigned int m_discriminator;
d9b3de22
DE
20819
20820 /* Additional bits of state we need to track. */
20821
20822 /* The last file that we called dwarf2_start_subfile for.
20823 This is only used for TLLs. */
6f77053d 20824 unsigned int m_last_file = 0;
d9b3de22 20825 /* The last file a line number was recorded for. */
6f77053d 20826 struct subfile *m_last_subfile = NULL;
d9b3de22 20827
804d2729
TT
20828 /* When true, record the lines we decode. */
20829 bool m_currently_recording_lines = false;
d9b3de22
DE
20830
20831 /* The last line number that was recorded, used to coalesce
20832 consecutive entries for the same line. This can happen, for
20833 example, when discriminators are present. PR 17276. */
6f77053d
PA
20834 unsigned int m_last_line = 0;
20835 bool m_line_has_non_zero_discriminator = false;
8c43009f 20836};
d9b3de22 20837
6f77053d
PA
20838void
20839lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20840{
20841 CORE_ADDR addr_adj = (((m_op_index + adjust)
20842 / m_line_header->maximum_ops_per_instruction)
20843 * m_line_header->minimum_instruction_length);
20844 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20845 m_op_index = ((m_op_index + adjust)
20846 % m_line_header->maximum_ops_per_instruction);
20847}
d9b3de22 20848
6f77053d
PA
20849void
20850lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20851{
6f77053d
PA
20852 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20853 CORE_ADDR addr_adj = (((m_op_index
20854 + (adj_opcode / m_line_header->line_range))
20855 / m_line_header->maximum_ops_per_instruction)
20856 * m_line_header->minimum_instruction_length);
20857 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20858 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20859 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20860
6f77053d
PA
20861 int line_delta = (m_line_header->line_base
20862 + (adj_opcode % m_line_header->line_range));
20863 advance_line (line_delta);
20864 record_line (false);
20865 m_discriminator = 0;
20866}
d9b3de22 20867
6f77053d
PA
20868void
20869lnp_state_machine::handle_set_file (file_name_index file)
20870{
20871 m_file = file;
20872
20873 const file_entry *fe = current_file ();
20874 if (fe == NULL)
20875 dwarf2_debug_line_missing_file_complaint ();
20876 else if (m_record_lines_p)
20877 {
20878 const char *dir = fe->include_dir (m_line_header);
20879
c24bdb02 20880 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20881 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20882 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20883 }
20884}
20885
20886void
20887lnp_state_machine::handle_const_add_pc ()
20888{
20889 CORE_ADDR adjust
20890 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20891
20892 CORE_ADDR addr_adj
20893 = (((m_op_index + adjust)
20894 / m_line_header->maximum_ops_per_instruction)
20895 * m_line_header->minimum_instruction_length);
20896
20897 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20898 m_op_index = ((m_op_index + adjust)
20899 % m_line_header->maximum_ops_per_instruction);
20900}
d9b3de22 20901
a05a36a5
DE
20902/* Return non-zero if we should add LINE to the line number table.
20903 LINE is the line to add, LAST_LINE is the last line that was added,
20904 LAST_SUBFILE is the subfile for LAST_LINE.
20905 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20906 had a non-zero discriminator.
20907
20908 We have to be careful in the presence of discriminators.
20909 E.g., for this line:
20910
20911 for (i = 0; i < 100000; i++);
20912
20913 clang can emit four line number entries for that one line,
20914 each with a different discriminator.
20915 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20916
20917 However, we want gdb to coalesce all four entries into one.
20918 Otherwise the user could stepi into the middle of the line and
20919 gdb would get confused about whether the pc really was in the
20920 middle of the line.
20921
20922 Things are further complicated by the fact that two consecutive
20923 line number entries for the same line is a heuristic used by gcc
20924 to denote the end of the prologue. So we can't just discard duplicate
20925 entries, we have to be selective about it. The heuristic we use is
20926 that we only collapse consecutive entries for the same line if at least
20927 one of those entries has a non-zero discriminator. PR 17276.
20928
20929 Note: Addresses in the line number state machine can never go backwards
20930 within one sequence, thus this coalescing is ok. */
20931
20932static int
804d2729
TT
20933dwarf_record_line_p (struct dwarf2_cu *cu,
20934 unsigned int line, unsigned int last_line,
a05a36a5
DE
20935 int line_has_non_zero_discriminator,
20936 struct subfile *last_subfile)
20937{
c24bdb02 20938 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20939 return 1;
20940 if (line != last_line)
20941 return 1;
20942 /* Same line for the same file that we've seen already.
20943 As a last check, for pr 17276, only record the line if the line
20944 has never had a non-zero discriminator. */
20945 if (!line_has_non_zero_discriminator)
20946 return 1;
20947 return 0;
20948}
20949
804d2729
TT
20950/* Use the CU's builder to record line number LINE beginning at
20951 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20952
20953static void
d9b3de22
DE
20954dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20955 unsigned int line, CORE_ADDR address,
804d2729 20956 struct dwarf2_cu *cu)
252a6764
DE
20957{
20958 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20959
27e0867f
DE
20960 if (dwarf_line_debug)
20961 {
20962 fprintf_unfiltered (gdb_stdlog,
20963 "Recording line %u, file %s, address %s\n",
20964 line, lbasename (subfile->name),
20965 paddress (gdbarch, address));
20966 }
20967
804d2729 20968 if (cu != nullptr)
c24bdb02 20969 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
20970}
20971
20972/* Subroutine of dwarf_decode_lines_1 to simplify it.
20973 Mark the end of a set of line number records.
d9b3de22 20974 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20975 If SUBFILE is NULL the request is ignored. */
20976
20977static void
20978dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20979 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20980{
27e0867f
DE
20981 if (subfile == NULL)
20982 return;
20983
20984 if (dwarf_line_debug)
20985 {
20986 fprintf_unfiltered (gdb_stdlog,
20987 "Finishing current line, file %s, address %s\n",
20988 lbasename (subfile->name),
20989 paddress (gdbarch, address));
20990 }
20991
804d2729 20992 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
20993}
20994
6f77053d
PA
20995void
20996lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20997{
d9b3de22
DE
20998 if (dwarf_line_debug)
20999 {
21000 fprintf_unfiltered (gdb_stdlog,
21001 "Processing actual line %u: file %u,"
21002 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
21003 m_line, to_underlying (m_file),
21004 paddress (m_gdbarch, m_address),
21005 m_is_stmt, m_discriminator);
d9b3de22
DE
21006 }
21007
6f77053d 21008 file_entry *fe = current_file ();
8c43009f
PA
21009
21010 if (fe == NULL)
d9b3de22
DE
21011 dwarf2_debug_line_missing_file_complaint ();
21012 /* For now we ignore lines not starting on an instruction boundary.
21013 But not when processing end_sequence for compatibility with the
21014 previous version of the code. */
6f77053d 21015 else if (m_op_index == 0 || end_sequence)
d9b3de22 21016 {
8c43009f 21017 fe->included_p = 1;
c258c396 21018 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21019 {
c24bdb02 21020 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21021 || end_sequence)
d9b3de22 21022 {
804d2729
TT
21023 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21024 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21025 }
21026
21027 if (!end_sequence)
21028 {
804d2729 21029 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21030 m_line_has_non_zero_discriminator,
21031 m_last_subfile))
d9b3de22 21032 {
c24bdb02 21033 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21034 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21035 builder->get_current_subfile (),
6f77053d 21036 m_line, m_address,
804d2729 21037 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21038 }
c24bdb02 21039 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21040 m_last_line = m_line;
d9b3de22
DE
21041 }
21042 }
21043 }
21044}
21045
804d2729
TT
21046lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21047 line_header *lh, bool record_lines_p)
d9b3de22 21048{
804d2729 21049 m_cu = cu;
6f77053d
PA
21050 m_gdbarch = arch;
21051 m_record_lines_p = record_lines_p;
21052 m_line_header = lh;
d9b3de22 21053
804d2729 21054 m_currently_recording_lines = true;
d9b3de22 21055
d9b3de22
DE
21056 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21057 was a line entry for it so that the backend has a chance to adjust it
21058 and also record it in case it needs it. This is currently used by MIPS
21059 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21060 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21061 m_is_stmt = lh->default_is_stmt;
21062 m_discriminator = 0;
252a6764
DE
21063}
21064
6f77053d
PA
21065void
21066lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21067 const gdb_byte *line_ptr,
7ab6656f 21068 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21069{
7ab6656f
OJ
21070 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21071 the pc range of the CU. However, we restrict the test to only ADDRESS
21072 values of zero to preserve GDB's previous behaviour which is to handle
21073 the specific case of a function being GC'd by the linker. */
924c2928 21074
7ab6656f 21075 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21076 {
21077 /* This line table is for a function which has been
21078 GCd by the linker. Ignore it. PR gdb/12528 */
21079
518817b3 21080 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21081 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21082
b98664d3 21083 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21084 line_offset, objfile_name (objfile));
804d2729
TT
21085 m_currently_recording_lines = false;
21086 /* Note: m_currently_recording_lines is left as false until we see
21087 DW_LNE_end_sequence. */
924c2928
DE
21088 }
21089}
21090
f3f5162e 21091/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21092 Process the line number information in LH.
21093 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21094 program in order to set included_p for every referenced header. */
debd256d 21095
c906108c 21096static void
43f3e411
DE
21097dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21098 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21099{
d521ce57
TT
21100 const gdb_byte *line_ptr, *extended_end;
21101 const gdb_byte *line_end;
a8c50c1f 21102 unsigned int bytes_read, extended_len;
699ca60a 21103 unsigned char op_code, extended_op;
e142c38c 21104 CORE_ADDR baseaddr;
518817b3 21105 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21106 bfd *abfd = objfile->obfd;
fbf65064 21107 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21108 /* True if we're recording line info (as opposed to building partial
21109 symtabs and just interested in finding include files mentioned by
21110 the line number program). */
21111 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21112
21113 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21114
debd256d
JB
21115 line_ptr = lh->statement_program_start;
21116 line_end = lh->statement_program_end;
c906108c
SS
21117
21118 /* Read the statement sequences until there's nothing left. */
21119 while (line_ptr < line_end)
21120 {
6f77053d
PA
21121 /* The DWARF line number program state machine. Reset the state
21122 machine at the start of each sequence. */
804d2729 21123 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21124 bool end_sequence = false;
d9b3de22 21125
8c43009f 21126 if (record_lines_p)
c906108c 21127 {
8c43009f
PA
21128 /* Start a subfile for the current file of the state
21129 machine. */
21130 const file_entry *fe = state_machine.current_file ();
21131
21132 if (fe != NULL)
804d2729 21133 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21134 }
21135
a738430d 21136 /* Decode the table. */
d9b3de22 21137 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21138 {
21139 op_code = read_1_byte (abfd, line_ptr);
21140 line_ptr += 1;
9aa1fe7e 21141
debd256d 21142 if (op_code >= lh->opcode_base)
6e70227d 21143 {
8e07a239 21144 /* Special opcode. */
6f77053d 21145 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21146 }
21147 else switch (op_code)
c906108c
SS
21148 {
21149 case DW_LNS_extended_op:
3e43a32a
MS
21150 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21151 &bytes_read);
473b7be6 21152 line_ptr += bytes_read;
a8c50c1f 21153 extended_end = line_ptr + extended_len;
c906108c
SS
21154 extended_op = read_1_byte (abfd, line_ptr);
21155 line_ptr += 1;
21156 switch (extended_op)
21157 {
21158 case DW_LNE_end_sequence:
6f77053d
PA
21159 state_machine.handle_end_sequence ();
21160 end_sequence = true;
c906108c
SS
21161 break;
21162 case DW_LNE_set_address:
d9b3de22
DE
21163 {
21164 CORE_ADDR address
21165 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21166 line_ptr += bytes_read;
6f77053d
PA
21167
21168 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21169 lowpc - baseaddr, address);
6f77053d 21170 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21171 }
c906108c
SS
21172 break;
21173 case DW_LNE_define_file:
debd256d 21174 {
d521ce57 21175 const char *cur_file;
ecfb656c
PA
21176 unsigned int mod_time, length;
21177 dir_index dindex;
6e70227d 21178
3e43a32a
MS
21179 cur_file = read_direct_string (abfd, line_ptr,
21180 &bytes_read);
debd256d 21181 line_ptr += bytes_read;
ecfb656c 21182 dindex = (dir_index)
debd256d
JB
21183 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21184 line_ptr += bytes_read;
21185 mod_time =
21186 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21187 line_ptr += bytes_read;
21188 length =
21189 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21190 line_ptr += bytes_read;
ecfb656c 21191 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21192 }
c906108c 21193 break;
d0c6ba3d 21194 case DW_LNE_set_discriminator:
6f77053d
PA
21195 {
21196 /* The discriminator is not interesting to the
21197 debugger; just ignore it. We still need to
21198 check its value though:
21199 if there are consecutive entries for the same
21200 (non-prologue) line we want to coalesce them.
21201 PR 17276. */
21202 unsigned int discr
21203 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21204 line_ptr += bytes_read;
21205
21206 state_machine.handle_set_discriminator (discr);
21207 }
d0c6ba3d 21208 break;
c906108c 21209 default:
b98664d3 21210 complaint (_("mangled .debug_line section"));
debd256d 21211 return;
c906108c 21212 }
a8c50c1f
DJ
21213 /* Make sure that we parsed the extended op correctly. If e.g.
21214 we expected a different address size than the producer used,
21215 we may have read the wrong number of bytes. */
21216 if (line_ptr != extended_end)
21217 {
b98664d3 21218 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21219 return;
21220 }
c906108c
SS
21221 break;
21222 case DW_LNS_copy:
6f77053d 21223 state_machine.handle_copy ();
c906108c
SS
21224 break;
21225 case DW_LNS_advance_pc:
2dc7f7b3
TT
21226 {
21227 CORE_ADDR adjust
21228 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21229 line_ptr += bytes_read;
6f77053d
PA
21230
21231 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21232 }
c906108c
SS
21233 break;
21234 case DW_LNS_advance_line:
a05a36a5
DE
21235 {
21236 int line_delta
21237 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21238 line_ptr += bytes_read;
6f77053d
PA
21239
21240 state_machine.handle_advance_line (line_delta);
a05a36a5 21241 }
c906108c
SS
21242 break;
21243 case DW_LNS_set_file:
d9b3de22 21244 {
6f77053d 21245 file_name_index file
ecfb656c
PA
21246 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21247 &bytes_read);
d9b3de22 21248 line_ptr += bytes_read;
8c43009f 21249
6f77053d 21250 state_machine.handle_set_file (file);
d9b3de22 21251 }
c906108c
SS
21252 break;
21253 case DW_LNS_set_column:
0ad93d4f 21254 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21255 line_ptr += bytes_read;
21256 break;
21257 case DW_LNS_negate_stmt:
6f77053d 21258 state_machine.handle_negate_stmt ();
c906108c
SS
21259 break;
21260 case DW_LNS_set_basic_block:
c906108c 21261 break;
c2c6d25f
JM
21262 /* Add to the address register of the state machine the
21263 address increment value corresponding to special opcode
a738430d
MK
21264 255. I.e., this value is scaled by the minimum
21265 instruction length since special opcode 255 would have
b021a221 21266 scaled the increment. */
c906108c 21267 case DW_LNS_const_add_pc:
6f77053d 21268 state_machine.handle_const_add_pc ();
c906108c
SS
21269 break;
21270 case DW_LNS_fixed_advance_pc:
3e29f34a 21271 {
6f77053d 21272 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21273 line_ptr += 2;
6f77053d
PA
21274
21275 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21276 }
c906108c 21277 break;
9aa1fe7e 21278 default:
a738430d
MK
21279 {
21280 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21281 int i;
a738430d 21282
debd256d 21283 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21284 {
21285 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21286 line_ptr += bytes_read;
21287 }
21288 }
c906108c
SS
21289 }
21290 }
d9b3de22
DE
21291
21292 if (!end_sequence)
21293 dwarf2_debug_line_missing_end_sequence_complaint ();
21294
21295 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21296 in which case we still finish recording the last line). */
6f77053d 21297 state_machine.record_line (true);
c906108c 21298 }
f3f5162e
DE
21299}
21300
21301/* Decode the Line Number Program (LNP) for the given line_header
21302 structure and CU. The actual information extracted and the type
21303 of structures created from the LNP depends on the value of PST.
21304
21305 1. If PST is NULL, then this procedure uses the data from the program
21306 to create all necessary symbol tables, and their linetables.
21307
21308 2. If PST is not NULL, this procedure reads the program to determine
21309 the list of files included by the unit represented by PST, and
21310 builds all the associated partial symbol tables.
21311
21312 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21313 It is used for relative paths in the line table.
21314 NOTE: When processing partial symtabs (pst != NULL),
21315 comp_dir == pst->dirname.
21316
21317 NOTE: It is important that psymtabs have the same file name (via strcmp)
21318 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21319 symtab we don't use it in the name of the psymtabs we create.
21320 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21321 A good testcase for this is mb-inline.exp.
21322
527f3840
JK
21323 LOWPC is the lowest address in CU (or 0 if not known).
21324
21325 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21326 for its PC<->lines mapping information. Otherwise only the filename
21327 table is read in. */
f3f5162e
DE
21328
21329static void
21330dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21331 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21332 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21333{
518817b3 21334 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21335 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21336
527f3840
JK
21337 if (decode_mapping)
21338 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21339
21340 if (decode_for_pst_p)
21341 {
21342 int file_index;
21343
21344 /* Now that we're done scanning the Line Header Program, we can
21345 create the psymtab of each included file. */
fff8551c 21346 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21347 if (lh->file_names[file_index].included_p == 1)
21348 {
c89b44cd 21349 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21350 const char *include_name =
c89b44cd
TT
21351 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21352 &name_holder);
c6da4cef 21353 if (include_name != NULL)
aaa75496
JB
21354 dwarf2_create_include_psymtab (include_name, pst, objfile);
21355 }
21356 }
cb1df416
DJ
21357 else
21358 {
21359 /* Make sure a symtab is created for every file, even files
21360 which contain only variables (i.e. no code with associated
21361 line numbers). */
c24bdb02
KS
21362 buildsym_compunit *builder = cu->get_builder ();
21363 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21364 int i;
cb1df416 21365
fff8551c 21366 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21367 {
8c43009f 21368 file_entry &fe = lh->file_names[i];
9a619af0 21369
804d2729 21370 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
cb1df416 21371
c24bdb02 21372 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21373 {
c24bdb02 21374 builder->get_current_subfile ()->symtab
804d2729 21375 = allocate_symtab (cust,
c24bdb02 21376 builder->get_current_subfile ()->name);
43f3e411 21377 }
c24bdb02 21378 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21379 }
21380 }
c906108c
SS
21381}
21382
21383/* Start a subfile for DWARF. FILENAME is the name of the file and
21384 DIRNAME the name of the source directory which contains FILENAME
4d663531 21385 or NULL if not known.
c906108c
SS
21386 This routine tries to keep line numbers from identical absolute and
21387 relative file names in a common subfile.
21388
21389 Using the `list' example from the GDB testsuite, which resides in
21390 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21391 of /srcdir/list0.c yields the following debugging information for list0.c:
21392
c5aa993b 21393 DW_AT_name: /srcdir/list0.c
4d663531 21394 DW_AT_comp_dir: /compdir
357e46e7 21395 files.files[0].name: list0.h
c5aa993b 21396 files.files[0].dir: /srcdir
357e46e7 21397 files.files[1].name: list0.c
c5aa993b 21398 files.files[1].dir: /srcdir
c906108c
SS
21399
21400 The line number information for list0.c has to end up in a single
4f1520fb
FR
21401 subfile, so that `break /srcdir/list0.c:1' works as expected.
21402 start_subfile will ensure that this happens provided that we pass the
21403 concatenation of files.files[1].dir and files.files[1].name as the
21404 subfile's name. */
c906108c
SS
21405
21406static void
804d2729
TT
21407dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21408 const char *dirname)
c906108c 21409{
d521ce57 21410 char *copy = NULL;
4f1520fb 21411
4d663531 21412 /* In order not to lose the line information directory,
4f1520fb
FR
21413 we concatenate it to the filename when it makes sense.
21414 Note that the Dwarf3 standard says (speaking of filenames in line
21415 information): ``The directory index is ignored for file names
21416 that represent full path names''. Thus ignoring dirname in the
21417 `else' branch below isn't an issue. */
c906108c 21418
d5166ae1 21419 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21420 {
21421 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21422 filename = copy;
21423 }
c906108c 21424
c24bdb02 21425 cu->get_builder ()->start_subfile (filename);
4f1520fb 21426
d521ce57
TT
21427 if (copy != NULL)
21428 xfree (copy);
c906108c
SS
21429}
21430
804d2729
TT
21431/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21432 buildsym_compunit constructor. */
f4dc4d17 21433
c24bdb02
KS
21434struct compunit_symtab *
21435dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21436 CORE_ADDR low_pc)
f4dc4d17 21437{
c24bdb02 21438 gdb_assert (m_builder == nullptr);
43f3e411 21439
c24bdb02
KS
21440 m_builder.reset (new struct buildsym_compunit
21441 (per_cu->dwarf2_per_objfile->objfile,
21442 name, comp_dir, language, low_pc));
93b8bea4 21443
c24bdb02 21444 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21445
c24bdb02
KS
21446 get_builder ()->record_debugformat ("DWARF 2");
21447 get_builder ()->record_producer (producer);
f4dc4d17 21448
c24bdb02 21449 processing_has_namespace_info = false;
43f3e411 21450
c24bdb02 21451 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21452}
21453
4c2df51b
DJ
21454static void
21455var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21456 struct dwarf2_cu *cu)
4c2df51b 21457{
518817b3 21458 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21459 struct comp_unit_head *cu_header = &cu->header;
21460
4c2df51b
DJ
21461 /* NOTE drow/2003-01-30: There used to be a comment and some special
21462 code here to turn a symbol with DW_AT_external and a
21463 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21464 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21465 with some versions of binutils) where shared libraries could have
21466 relocations against symbols in their debug information - the
21467 minimal symbol would have the right address, but the debug info
21468 would not. It's no longer necessary, because we will explicitly
21469 apply relocations when we read in the debug information now. */
21470
21471 /* A DW_AT_location attribute with no contents indicates that a
21472 variable has been optimized away. */
21473 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21474 {
f1e6e072 21475 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21476 return;
21477 }
21478
21479 /* Handle one degenerate form of location expression specially, to
21480 preserve GDB's previous behavior when section offsets are
336d760d
AT
21481 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21482 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21483
21484 if (attr_form_is_block (attr)
3019eac3
DE
21485 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21486 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21487 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21488 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21489 && (DW_BLOCK (attr)->size
21490 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21491 {
891d2f0b 21492 unsigned int dummy;
4c2df51b 21493
3019eac3 21494 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21495 SET_SYMBOL_VALUE_ADDRESS (sym,
21496 read_address (objfile->obfd,
21497 DW_BLOCK (attr)->data + 1,
21498 cu, &dummy));
3019eac3 21499 else
38583298
TT
21500 SET_SYMBOL_VALUE_ADDRESS
21501 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21502 &dummy));
f1e6e072 21503 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21504 fixup_symbol_section (sym, objfile);
38583298
TT
21505 SET_SYMBOL_VALUE_ADDRESS (sym,
21506 SYMBOL_VALUE_ADDRESS (sym)
21507 + ANOFFSET (objfile->section_offsets,
21508 SYMBOL_SECTION (sym)));
4c2df51b
DJ
21509 return;
21510 }
21511
21512 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21513 expression evaluator, and use LOC_COMPUTED only when necessary
21514 (i.e. when the value of a register or memory location is
21515 referenced, or a thread-local block, etc.). Then again, it might
21516 not be worthwhile. I'm assuming that it isn't unless performance
21517 or memory numbers show me otherwise. */
21518
f1e6e072 21519 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21520
f1e6e072 21521 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21522 cu->has_loclist = true;
4c2df51b
DJ
21523}
21524
c906108c
SS
21525/* Given a pointer to a DWARF information entry, figure out if we need
21526 to make a symbol table entry for it, and if so, create a new entry
21527 and return a pointer to it.
21528 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21529 used the passed type.
21530 If SPACE is not NULL, use it to hold the new symbol. If it is
21531 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21532
21533static struct symbol *
5e2db402
TT
21534new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21535 struct symbol *space)
c906108c 21536{
518817b3
SM
21537 struct dwarf2_per_objfile *dwarf2_per_objfile
21538 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21539 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21540 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21541 struct symbol *sym = NULL;
15d034d0 21542 const char *name;
c906108c
SS
21543 struct attribute *attr = NULL;
21544 struct attribute *attr2 = NULL;
e142c38c 21545 CORE_ADDR baseaddr;
e37fd15a
SW
21546 struct pending **list_to_add = NULL;
21547
edb3359d 21548 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21549
21550 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21551
94af9270 21552 name = dwarf2_name (die, cu);
c906108c
SS
21553 if (name)
21554 {
94af9270 21555 const char *linkagename;
34eaf542 21556 int suppress_add = 0;
94af9270 21557
34eaf542
TT
21558 if (space)
21559 sym = space;
21560 else
e623cf5d 21561 sym = allocate_symbol (objfile);
c906108c 21562 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21563
21564 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21565 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21566 linkagename = dwarf2_physname (name, die, cu);
21567 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21568
f55ee35c
JK
21569 /* Fortran does not have mangling standard and the mangling does differ
21570 between gfortran, iFort etc. */
21571 if (cu->language == language_fortran
b250c185 21572 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21573 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21574 dwarf2_full_name (name, die, cu),
29df156d 21575 NULL);
f55ee35c 21576
c906108c 21577 /* Default assumptions.
c5aa993b 21578 Use the passed type or decode it from the die. */
176620f1 21579 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21580 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21581 if (type != NULL)
21582 SYMBOL_TYPE (sym) = type;
21583 else
e7c27a73 21584 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21585 attr = dwarf2_attr (die,
21586 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21587 cu);
c906108c
SS
21588 if (attr)
21589 {
21590 SYMBOL_LINE (sym) = DW_UNSND (attr);
21591 }
cb1df416 21592
edb3359d
DJ
21593 attr = dwarf2_attr (die,
21594 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21595 cu);
cb1df416
DJ
21596 if (attr)
21597 {
ecfb656c 21598 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21599 struct file_entry *fe;
9a619af0 21600
ecfb656c
PA
21601 if (cu->line_header != NULL)
21602 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21603 else
21604 fe = NULL;
21605
21606 if (fe == NULL)
b98664d3 21607 complaint (_("file index out of range"));
8c43009f
PA
21608 else
21609 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21610 }
21611
c906108c
SS
21612 switch (die->tag)
21613 {
21614 case DW_TAG_label:
e142c38c 21615 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21616 if (attr)
3e29f34a
MR
21617 {
21618 CORE_ADDR addr;
21619
21620 addr = attr_value_as_address (attr);
21621 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21622 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21623 }
0f5238ed
TT
21624 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21625 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21626 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21627 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21628 break;
21629 case DW_TAG_subprogram:
21630 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21631 finish_block. */
f1e6e072 21632 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21633 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21634 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21635 || cu->language == language_ada
21636 || cu->language == language_fortran)
c906108c 21637 {
2cfa0c8d 21638 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21639 Ada and Fortran subprograms, whether marked external or
21640 not, are always stored as a global symbol, because we want
21641 to be able to access them globally. For instance, we want
21642 to be able to break on a nested subprogram without having
21643 to specify the context. */
c24bdb02 21644 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21645 }
21646 else
21647 {
e37fd15a 21648 list_to_add = cu->list_in_scope;
c906108c
SS
21649 }
21650 break;
edb3359d
DJ
21651 case DW_TAG_inlined_subroutine:
21652 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21653 finish_block. */
f1e6e072 21654 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21655 SYMBOL_INLINED (sym) = 1;
481860b3 21656 list_to_add = cu->list_in_scope;
edb3359d 21657 break;
34eaf542
TT
21658 case DW_TAG_template_value_param:
21659 suppress_add = 1;
21660 /* Fall through. */
72929c62 21661 case DW_TAG_constant:
c906108c 21662 case DW_TAG_variable:
254e6b9e 21663 case DW_TAG_member:
0963b4bd
MS
21664 /* Compilation with minimal debug info may result in
21665 variables with missing type entries. Change the
21666 misleading `void' type to something sensible. */
c906108c 21667 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21668 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21669
e142c38c 21670 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21671 /* In the case of DW_TAG_member, we should only be called for
21672 static const members. */
21673 if (die->tag == DW_TAG_member)
21674 {
3863f96c
DE
21675 /* dwarf2_add_field uses die_is_declaration,
21676 so we do the same. */
254e6b9e
DE
21677 gdb_assert (die_is_declaration (die, cu));
21678 gdb_assert (attr);
21679 }
c906108c
SS
21680 if (attr)
21681 {
e7c27a73 21682 dwarf2_const_value (attr, sym, cu);
e142c38c 21683 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21684 if (!suppress_add)
34eaf542
TT
21685 {
21686 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21687 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21688 else
e37fd15a 21689 list_to_add = cu->list_in_scope;
34eaf542 21690 }
c906108c
SS
21691 break;
21692 }
e142c38c 21693 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21694 if (attr)
21695 {
e7c27a73 21696 var_decode_location (attr, sym, cu);
e142c38c 21697 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21698
21699 /* Fortran explicitly imports any global symbols to the local
21700 scope by DW_TAG_common_block. */
21701 if (cu->language == language_fortran && die->parent
21702 && die->parent->tag == DW_TAG_common_block)
21703 attr2 = NULL;
21704
caac4577
JG
21705 if (SYMBOL_CLASS (sym) == LOC_STATIC
21706 && SYMBOL_VALUE_ADDRESS (sym) == 0
21707 && !dwarf2_per_objfile->has_section_at_zero)
21708 {
21709 /* When a static variable is eliminated by the linker,
21710 the corresponding debug information is not stripped
21711 out, but the variable address is set to null;
21712 do not add such variables into symbol table. */
21713 }
21714 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21715 {
4b610737
TT
21716 if (SYMBOL_CLASS (sym) == LOC_STATIC
21717 && (objfile->flags & OBJF_MAINLINE) == 0
21718 && dwarf2_per_objfile->can_copy)
21719 {
21720 /* A global static variable might be subject to
21721 copy relocation. We first check for a local
21722 minsym, though, because maybe the symbol was
21723 marked hidden, in which case this would not
21724 apply. */
21725 bound_minimal_symbol found
21726 = (lookup_minimal_symbol_linkage
21727 (SYMBOL_LINKAGE_NAME (sym), objfile));
21728 if (found.minsym != nullptr)
21729 sym->maybe_copied = 1;
21730 }
f55ee35c 21731
1c809c68
TT
21732 /* A variable with DW_AT_external is never static,
21733 but it may be block-scoped. */
804d2729 21734 list_to_add
c24bdb02
KS
21735 = ((cu->list_in_scope
21736 == cu->get_builder ()->get_file_symbols ())
21737 ? cu->get_builder ()->get_global_symbols ()
804d2729 21738 : cu->list_in_scope);
1c809c68 21739 }
c906108c 21740 else
e37fd15a 21741 list_to_add = cu->list_in_scope;
c906108c
SS
21742 }
21743 else
21744 {
21745 /* We do not know the address of this symbol.
c5aa993b
JM
21746 If it is an external symbol and we have type information
21747 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21748 The address of the variable will then be determined from
21749 the minimal symbol table whenever the variable is
21750 referenced. */
e142c38c 21751 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21752
21753 /* Fortran explicitly imports any global symbols to the local
21754 scope by DW_TAG_common_block. */
21755 if (cu->language == language_fortran && die->parent
21756 && die->parent->tag == DW_TAG_common_block)
21757 {
21758 /* SYMBOL_CLASS doesn't matter here because
21759 read_common_block is going to reset it. */
21760 if (!suppress_add)
21761 list_to_add = cu->list_in_scope;
21762 }
21763 else if (attr2 && (DW_UNSND (attr2) != 0)
21764 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21765 {
0fe7935b
DJ
21766 /* A variable with DW_AT_external is never static, but it
21767 may be block-scoped. */
804d2729 21768 list_to_add
c24bdb02
KS
21769 = ((cu->list_in_scope
21770 == cu->get_builder ()->get_file_symbols ())
21771 ? cu->get_builder ()->get_global_symbols ()
804d2729 21772 : cu->list_in_scope);
0fe7935b 21773
f1e6e072 21774 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21775 }
442ddf59
JK
21776 else if (!die_is_declaration (die, cu))
21777 {
21778 /* Use the default LOC_OPTIMIZED_OUT class. */
21779 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21780 if (!suppress_add)
21781 list_to_add = cu->list_in_scope;
442ddf59 21782 }
c906108c
SS
21783 }
21784 break;
21785 case DW_TAG_formal_parameter:
a60f3166
TT
21786 {
21787 /* If we are inside a function, mark this as an argument. If
21788 not, we might be looking at an argument to an inlined function
21789 when we do not have enough information to show inlined frames;
21790 pretend it's a local variable in that case so that the user can
21791 still see it. */
804d2729 21792 struct context_stack *curr
c24bdb02 21793 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21794 if (curr != nullptr && curr->name != nullptr)
21795 SYMBOL_IS_ARGUMENT (sym) = 1;
21796 attr = dwarf2_attr (die, DW_AT_location, cu);
21797 if (attr)
21798 {
21799 var_decode_location (attr, sym, cu);
21800 }
21801 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21802 if (attr)
21803 {
21804 dwarf2_const_value (attr, sym, cu);
21805 }
f346a30d 21806
a60f3166
TT
21807 list_to_add = cu->list_in_scope;
21808 }
c906108c
SS
21809 break;
21810 case DW_TAG_unspecified_parameters:
21811 /* From varargs functions; gdb doesn't seem to have any
21812 interest in this information, so just ignore it for now.
21813 (FIXME?) */
21814 break;
34eaf542
TT
21815 case DW_TAG_template_type_param:
21816 suppress_add = 1;
21817 /* Fall through. */
c906108c 21818 case DW_TAG_class_type:
680b30c7 21819 case DW_TAG_interface_type:
c906108c
SS
21820 case DW_TAG_structure_type:
21821 case DW_TAG_union_type:
72019c9c 21822 case DW_TAG_set_type:
c906108c 21823 case DW_TAG_enumeration_type:
f1e6e072 21824 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21825 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21826
63d06c5c 21827 {
9c37b5ae 21828 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21829 really ever be static objects: otherwise, if you try
21830 to, say, break of a class's method and you're in a file
21831 which doesn't mention that class, it won't work unless
21832 the check for all static symbols in lookup_symbol_aux
21833 saves you. See the OtherFileClass tests in
21834 gdb.c++/namespace.exp. */
21835
e37fd15a 21836 if (!suppress_add)
34eaf542 21837 {
c24bdb02 21838 buildsym_compunit *builder = cu->get_builder ();
804d2729 21839 list_to_add
c24bdb02 21840 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21841 && cu->language == language_cplus
c24bdb02 21842 ? builder->get_global_symbols ()
804d2729 21843 : cu->list_in_scope);
63d06c5c 21844
64382290 21845 /* The semantics of C++ state that "struct foo {
9c37b5ae 21846 ... }" also defines a typedef for "foo". */
64382290 21847 if (cu->language == language_cplus
45280282 21848 || cu->language == language_ada
c44af4eb
TT
21849 || cu->language == language_d
21850 || cu->language == language_rust)
64382290
TT
21851 {
21852 /* The symbol's name is already allocated along
21853 with this objfile, so we don't need to
21854 duplicate it for the type. */
21855 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21856 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21857 }
63d06c5c
DC
21858 }
21859 }
c906108c
SS
21860 break;
21861 case DW_TAG_typedef:
f1e6e072 21862 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21863 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21864 list_to_add = cu->list_in_scope;
63d06c5c 21865 break;
c906108c 21866 case DW_TAG_base_type:
a02abb62 21867 case DW_TAG_subrange_type:
f1e6e072 21868 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21869 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21870 list_to_add = cu->list_in_scope;
c906108c
SS
21871 break;
21872 case DW_TAG_enumerator:
e142c38c 21873 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21874 if (attr)
21875 {
e7c27a73 21876 dwarf2_const_value (attr, sym, cu);
c906108c 21877 }
63d06c5c
DC
21878 {
21879 /* NOTE: carlton/2003-11-10: See comment above in the
21880 DW_TAG_class_type, etc. block. */
21881
804d2729 21882 list_to_add
c24bdb02 21883 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21884 && cu->language == language_cplus
c24bdb02 21885 ? cu->get_builder ()->get_global_symbols ()
804d2729 21886 : cu->list_in_scope);
63d06c5c 21887 }
c906108c 21888 break;
74921315 21889 case DW_TAG_imported_declaration:
5c4e30ca 21890 case DW_TAG_namespace:
f1e6e072 21891 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21892 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21893 break;
530e8392
KB
21894 case DW_TAG_module:
21895 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21896 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21897 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21898 break;
4357ac6c 21899 case DW_TAG_common_block:
f1e6e072 21900 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21901 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21902 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21903 break;
c906108c
SS
21904 default:
21905 /* Not a tag we recognize. Hopefully we aren't processing
21906 trash data, but since we must specifically ignore things
21907 we don't recognize, there is nothing else we should do at
0963b4bd 21908 this point. */
b98664d3 21909 complaint (_("unsupported tag: '%s'"),
4d3c2250 21910 dwarf_tag_name (die->tag));
c906108c
SS
21911 break;
21912 }
df8a16a1 21913
e37fd15a
SW
21914 if (suppress_add)
21915 {
21916 sym->hash_next = objfile->template_symbols;
21917 objfile->template_symbols = sym;
21918 list_to_add = NULL;
21919 }
21920
21921 if (list_to_add != NULL)
d3cb6808 21922 add_symbol_to_list (sym, list_to_add);
e37fd15a 21923
df8a16a1
DJ
21924 /* For the benefit of old versions of GCC, check for anonymous
21925 namespaces based on the demangled name. */
4d4ec4e5 21926 if (!cu->processing_has_namespace_info
94af9270 21927 && cu->language == language_cplus)
c24bdb02 21928 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21929 }
21930 return (sym);
21931}
21932
98bfdba5
PA
21933/* Given an attr with a DW_FORM_dataN value in host byte order,
21934 zero-extend it as appropriate for the symbol's type. The DWARF
21935 standard (v4) is not entirely clear about the meaning of using
21936 DW_FORM_dataN for a constant with a signed type, where the type is
21937 wider than the data. The conclusion of a discussion on the DWARF
21938 list was that this is unspecified. We choose to always zero-extend
21939 because that is the interpretation long in use by GCC. */
c906108c 21940
98bfdba5 21941static gdb_byte *
ff39bb5e 21942dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21943 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21944{
518817b3 21945 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21946 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21947 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21948 LONGEST l = DW_UNSND (attr);
21949
21950 if (bits < sizeof (*value) * 8)
21951 {
21952 l &= ((LONGEST) 1 << bits) - 1;
21953 *value = l;
21954 }
21955 else if (bits == sizeof (*value) * 8)
21956 *value = l;
21957 else
21958 {
224c3ddb 21959 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21960 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21961 return bytes;
21962 }
21963
21964 return NULL;
21965}
21966
21967/* Read a constant value from an attribute. Either set *VALUE, or if
21968 the value does not fit in *VALUE, set *BYTES - either already
21969 allocated on the objfile obstack, or newly allocated on OBSTACK,
21970 or, set *BATON, if we translated the constant to a location
21971 expression. */
21972
21973static void
ff39bb5e 21974dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21975 const char *name, struct obstack *obstack,
21976 struct dwarf2_cu *cu,
d521ce57 21977 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21978 struct dwarf2_locexpr_baton **baton)
21979{
518817b3 21980 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21981 struct comp_unit_head *cu_header = &cu->header;
c906108c 21982 struct dwarf_block *blk;
98bfdba5
PA
21983 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21984 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21985
21986 *value = 0;
21987 *bytes = NULL;
21988 *baton = NULL;
c906108c
SS
21989
21990 switch (attr->form)
21991 {
21992 case DW_FORM_addr:
336d760d 21993 case DW_FORM_addrx:
3019eac3 21994 case DW_FORM_GNU_addr_index:
ac56253d 21995 {
ac56253d
TT
21996 gdb_byte *data;
21997
98bfdba5
PA
21998 if (TYPE_LENGTH (type) != cu_header->addr_size)
21999 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22000 cu_header->addr_size,
98bfdba5 22001 TYPE_LENGTH (type));
ac56253d
TT
22002 /* Symbols of this form are reasonably rare, so we just
22003 piggyback on the existing location code rather than writing
22004 a new implementation of symbol_computed_ops. */
8d749320 22005 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22006 (*baton)->per_cu = cu->per_cu;
22007 gdb_assert ((*baton)->per_cu);
ac56253d 22008
98bfdba5 22009 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22010 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22011 (*baton)->data = data;
ac56253d
TT
22012
22013 data[0] = DW_OP_addr;
22014 store_unsigned_integer (&data[1], cu_header->addr_size,
22015 byte_order, DW_ADDR (attr));
22016 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22017 }
c906108c 22018 break;
4ac36638 22019 case DW_FORM_string:
93b5768b 22020 case DW_FORM_strp:
cf532bd1 22021 case DW_FORM_strx:
3019eac3 22022 case DW_FORM_GNU_str_index:
36586728 22023 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22024 /* DW_STRING is already allocated on the objfile obstack, point
22025 directly to it. */
d521ce57 22026 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22027 break;
c906108c
SS
22028 case DW_FORM_block1:
22029 case DW_FORM_block2:
22030 case DW_FORM_block4:
22031 case DW_FORM_block:
2dc7f7b3 22032 case DW_FORM_exprloc:
0224619f 22033 case DW_FORM_data16:
c906108c 22034 blk = DW_BLOCK (attr);
98bfdba5
PA
22035 if (TYPE_LENGTH (type) != blk->size)
22036 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22037 TYPE_LENGTH (type));
22038 *bytes = blk->data;
c906108c 22039 break;
2df3850c
JM
22040
22041 /* The DW_AT_const_value attributes are supposed to carry the
22042 symbol's value "represented as it would be on the target
22043 architecture." By the time we get here, it's already been
22044 converted to host endianness, so we just need to sign- or
22045 zero-extend it as appropriate. */
22046 case DW_FORM_data1:
3aef2284 22047 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22048 break;
c906108c 22049 case DW_FORM_data2:
3aef2284 22050 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22051 break;
c906108c 22052 case DW_FORM_data4:
3aef2284 22053 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22054 break;
c906108c 22055 case DW_FORM_data8:
3aef2284 22056 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22057 break;
22058
c906108c 22059 case DW_FORM_sdata:
663c44ac 22060 case DW_FORM_implicit_const:
98bfdba5 22061 *value = DW_SND (attr);
2df3850c
JM
22062 break;
22063
c906108c 22064 case DW_FORM_udata:
98bfdba5 22065 *value = DW_UNSND (attr);
c906108c 22066 break;
2df3850c 22067
c906108c 22068 default:
b98664d3 22069 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22070 dwarf_form_name (attr->form));
98bfdba5 22071 *value = 0;
c906108c
SS
22072 break;
22073 }
22074}
22075
2df3850c 22076
98bfdba5
PA
22077/* Copy constant value from an attribute to a symbol. */
22078
2df3850c 22079static void
ff39bb5e 22080dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22081 struct dwarf2_cu *cu)
2df3850c 22082{
518817b3 22083 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22084 LONGEST value;
d521ce57 22085 const gdb_byte *bytes;
98bfdba5 22086 struct dwarf2_locexpr_baton *baton;
2df3850c 22087
98bfdba5
PA
22088 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22089 SYMBOL_PRINT_NAME (sym),
22090 &objfile->objfile_obstack, cu,
22091 &value, &bytes, &baton);
2df3850c 22092
98bfdba5
PA
22093 if (baton != NULL)
22094 {
98bfdba5 22095 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22096 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22097 }
22098 else if (bytes != NULL)
22099 {
22100 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22101 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22102 }
22103 else
22104 {
22105 SYMBOL_VALUE (sym) = value;
f1e6e072 22106 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22107 }
2df3850c
JM
22108}
22109
c906108c
SS
22110/* Return the type of the die in question using its DW_AT_type attribute. */
22111
22112static struct type *
e7c27a73 22113die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22114{
c906108c 22115 struct attribute *type_attr;
c906108c 22116
e142c38c 22117 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22118 if (!type_attr)
22119 {
518817b3 22120 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22121 /* A missing DW_AT_type represents a void type. */
518817b3 22122 return objfile_type (objfile)->builtin_void;
c906108c 22123 }
348e048f 22124
673bfd45 22125 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22126}
22127
b4ba55a1
JB
22128/* True iff CU's producer generates GNAT Ada auxiliary information
22129 that allows to find parallel types through that information instead
22130 of having to do expensive parallel lookups by type name. */
22131
22132static int
22133need_gnat_info (struct dwarf2_cu *cu)
22134{
de4cb04a
JB
22135 /* Assume that the Ada compiler was GNAT, which always produces
22136 the auxiliary information. */
22137 return (cu->language == language_ada);
b4ba55a1
JB
22138}
22139
b4ba55a1
JB
22140/* Return the auxiliary type of the die in question using its
22141 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22142 attribute is not present. */
22143
22144static struct type *
22145die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22146{
b4ba55a1 22147 struct attribute *type_attr;
b4ba55a1
JB
22148
22149 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22150 if (!type_attr)
22151 return NULL;
22152
673bfd45 22153 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22154}
22155
22156/* If DIE has a descriptive_type attribute, then set the TYPE's
22157 descriptive type accordingly. */
22158
22159static void
22160set_descriptive_type (struct type *type, struct die_info *die,
22161 struct dwarf2_cu *cu)
22162{
22163 struct type *descriptive_type = die_descriptive_type (die, cu);
22164
22165 if (descriptive_type)
22166 {
22167 ALLOCATE_GNAT_AUX_TYPE (type);
22168 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22169 }
22170}
22171
c906108c
SS
22172/* Return the containing type of the die in question using its
22173 DW_AT_containing_type attribute. */
22174
22175static struct type *
e7c27a73 22176die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22177{
c906108c 22178 struct attribute *type_attr;
518817b3 22179 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22180
e142c38c 22181 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22182 if (!type_attr)
22183 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22184 "[in module %s]"), objfile_name (objfile));
33ac96f0 22185
673bfd45 22186 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22187}
22188
ac9ec31b
DE
22189/* Return an error marker type to use for the ill formed type in DIE/CU. */
22190
22191static struct type *
22192build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22193{
518817b3
SM
22194 struct dwarf2_per_objfile *dwarf2_per_objfile
22195 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22196 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22197 char *saved;
ac9ec31b 22198
528e1572
SM
22199 std::string message
22200 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22201 objfile_name (objfile),
22202 sect_offset_str (cu->header.sect_off),
22203 sect_offset_str (die->sect_off));
efba19b0 22204 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22205
19f392bc 22206 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22207}
22208
673bfd45 22209/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22210 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22211 DW_AT_containing_type.
673bfd45
DE
22212 If there is no type substitute an error marker. */
22213
c906108c 22214static struct type *
ff39bb5e 22215lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22216 struct dwarf2_cu *cu)
c906108c 22217{
518817b3
SM
22218 struct dwarf2_per_objfile *dwarf2_per_objfile
22219 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22220 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22221 struct type *this_type;
22222
ac9ec31b
DE
22223 gdb_assert (attr->name == DW_AT_type
22224 || attr->name == DW_AT_GNAT_descriptive_type
22225 || attr->name == DW_AT_containing_type);
22226
673bfd45
DE
22227 /* First see if we have it cached. */
22228
36586728
TT
22229 if (attr->form == DW_FORM_GNU_ref_alt)
22230 {
22231 struct dwarf2_per_cu_data *per_cu;
9c541725 22232 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22233
ed2dc618
SM
22234 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22235 dwarf2_per_objfile);
9c541725 22236 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22237 }
7771576e 22238 else if (attr_form_is_ref (attr))
673bfd45 22239 {
9c541725 22240 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22241
9c541725 22242 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22243 }
55f1336d 22244 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22245 {
ac9ec31b 22246 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22247
ac9ec31b 22248 return get_signatured_type (die, signature, cu);
673bfd45
DE
22249 }
22250 else
22251 {
b98664d3 22252 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22253 " at %s [in module %s]"),
22254 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22255 objfile_name (objfile));
ac9ec31b 22256 return build_error_marker_type (cu, die);
673bfd45
DE
22257 }
22258
22259 /* If not cached we need to read it in. */
22260
22261 if (this_type == NULL)
22262 {
ac9ec31b 22263 struct die_info *type_die = NULL;
673bfd45
DE
22264 struct dwarf2_cu *type_cu = cu;
22265
7771576e 22266 if (attr_form_is_ref (attr))
ac9ec31b
DE
22267 type_die = follow_die_ref (die, attr, &type_cu);
22268 if (type_die == NULL)
22269 return build_error_marker_type (cu, die);
22270 /* If we find the type now, it's probably because the type came
3019eac3
DE
22271 from an inter-CU reference and the type's CU got expanded before
22272 ours. */
ac9ec31b 22273 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22274 }
22275
22276 /* If we still don't have a type use an error marker. */
22277
22278 if (this_type == NULL)
ac9ec31b 22279 return build_error_marker_type (cu, die);
673bfd45 22280
f792889a 22281 return this_type;
c906108c
SS
22282}
22283
673bfd45
DE
22284/* Return the type in DIE, CU.
22285 Returns NULL for invalid types.
22286
02142a6c 22287 This first does a lookup in die_type_hash,
673bfd45
DE
22288 and only reads the die in if necessary.
22289
22290 NOTE: This can be called when reading in partial or full symbols. */
22291
f792889a 22292static struct type *
e7c27a73 22293read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22294{
f792889a
DJ
22295 struct type *this_type;
22296
22297 this_type = get_die_type (die, cu);
22298 if (this_type)
22299 return this_type;
22300
673bfd45
DE
22301 return read_type_die_1 (die, cu);
22302}
22303
22304/* Read the type in DIE, CU.
22305 Returns NULL for invalid types. */
22306
22307static struct type *
22308read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22309{
22310 struct type *this_type = NULL;
22311
c906108c
SS
22312 switch (die->tag)
22313 {
22314 case DW_TAG_class_type:
680b30c7 22315 case DW_TAG_interface_type:
c906108c
SS
22316 case DW_TAG_structure_type:
22317 case DW_TAG_union_type:
f792889a 22318 this_type = read_structure_type (die, cu);
c906108c
SS
22319 break;
22320 case DW_TAG_enumeration_type:
f792889a 22321 this_type = read_enumeration_type (die, cu);
c906108c
SS
22322 break;
22323 case DW_TAG_subprogram:
22324 case DW_TAG_subroutine_type:
edb3359d 22325 case DW_TAG_inlined_subroutine:
f792889a 22326 this_type = read_subroutine_type (die, cu);
c906108c
SS
22327 break;
22328 case DW_TAG_array_type:
f792889a 22329 this_type = read_array_type (die, cu);
c906108c 22330 break;
72019c9c 22331 case DW_TAG_set_type:
f792889a 22332 this_type = read_set_type (die, cu);
72019c9c 22333 break;
c906108c 22334 case DW_TAG_pointer_type:
f792889a 22335 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22336 break;
22337 case DW_TAG_ptr_to_member_type:
f792889a 22338 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22339 break;
22340 case DW_TAG_reference_type:
4297a3f0
AV
22341 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22342 break;
22343 case DW_TAG_rvalue_reference_type:
22344 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22345 break;
22346 case DW_TAG_const_type:
f792889a 22347 this_type = read_tag_const_type (die, cu);
c906108c
SS
22348 break;
22349 case DW_TAG_volatile_type:
f792889a 22350 this_type = read_tag_volatile_type (die, cu);
c906108c 22351 break;
06d66ee9
TT
22352 case DW_TAG_restrict_type:
22353 this_type = read_tag_restrict_type (die, cu);
22354 break;
c906108c 22355 case DW_TAG_string_type:
f792889a 22356 this_type = read_tag_string_type (die, cu);
c906108c
SS
22357 break;
22358 case DW_TAG_typedef:
f792889a 22359 this_type = read_typedef (die, cu);
c906108c 22360 break;
a02abb62 22361 case DW_TAG_subrange_type:
f792889a 22362 this_type = read_subrange_type (die, cu);
a02abb62 22363 break;
c906108c 22364 case DW_TAG_base_type:
f792889a 22365 this_type = read_base_type (die, cu);
c906108c 22366 break;
81a17f79 22367 case DW_TAG_unspecified_type:
f792889a 22368 this_type = read_unspecified_type (die, cu);
81a17f79 22369 break;
0114d602
DJ
22370 case DW_TAG_namespace:
22371 this_type = read_namespace_type (die, cu);
22372 break;
f55ee35c
JK
22373 case DW_TAG_module:
22374 this_type = read_module_type (die, cu);
22375 break;
a2c2acaf
MW
22376 case DW_TAG_atomic_type:
22377 this_type = read_tag_atomic_type (die, cu);
22378 break;
c906108c 22379 default:
b98664d3 22380 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22381 dwarf_tag_name (die->tag));
c906108c
SS
22382 break;
22383 }
63d06c5c 22384
f792889a 22385 return this_type;
63d06c5c
DC
22386}
22387
abc72ce4
DE
22388/* See if we can figure out if the class lives in a namespace. We do
22389 this by looking for a member function; its demangled name will
22390 contain namespace info, if there is any.
22391 Return the computed name or NULL.
22392 Space for the result is allocated on the objfile's obstack.
22393 This is the full-die version of guess_partial_die_structure_name.
22394 In this case we know DIE has no useful parent. */
22395
22396static char *
22397guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22398{
22399 struct die_info *spec_die;
22400 struct dwarf2_cu *spec_cu;
22401 struct die_info *child;
518817b3 22402 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22403
22404 spec_cu = cu;
22405 spec_die = die_specification (die, &spec_cu);
22406 if (spec_die != NULL)
22407 {
22408 die = spec_die;
22409 cu = spec_cu;
22410 }
22411
22412 for (child = die->child;
22413 child != NULL;
22414 child = child->sibling)
22415 {
22416 if (child->tag == DW_TAG_subprogram)
22417 {
73b9be8b 22418 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22419
7d45c7c3 22420 if (linkage_name != NULL)
abc72ce4
DE
22421 {
22422 char *actual_name
22423 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22424 linkage_name);
abc72ce4
DE
22425 char *name = NULL;
22426
22427 if (actual_name != NULL)
22428 {
15d034d0 22429 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22430
22431 if (die_name != NULL
22432 && strcmp (die_name, actual_name) != 0)
22433 {
22434 /* Strip off the class name from the full name.
22435 We want the prefix. */
22436 int die_name_len = strlen (die_name);
22437 int actual_name_len = strlen (actual_name);
22438
22439 /* Test for '::' as a sanity check. */
22440 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22441 && actual_name[actual_name_len
22442 - die_name_len - 1] == ':')
0cf9feb9 22443 name = obstack_strndup (
e3b94546 22444 &objfile->per_bfd->storage_obstack,
224c3ddb 22445 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22446 }
22447 }
22448 xfree (actual_name);
22449 return name;
22450 }
22451 }
22452 }
22453
22454 return NULL;
22455}
22456
96408a79
SA
22457/* GCC might emit a nameless typedef that has a linkage name. Determine the
22458 prefix part in such case. See
22459 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22460
a121b7c1 22461static const char *
96408a79
SA
22462anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22463{
22464 struct attribute *attr;
e6a959d6 22465 const char *base;
96408a79
SA
22466
22467 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22468 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22469 return NULL;
22470
7d45c7c3 22471 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22472 return NULL;
22473
73b9be8b 22474 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22475 if (attr == NULL || DW_STRING (attr) == NULL)
22476 return NULL;
22477
22478 /* dwarf2_name had to be already called. */
22479 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22480
22481 /* Strip the base name, keep any leading namespaces/classes. */
22482 base = strrchr (DW_STRING (attr), ':');
22483 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22484 return "";
22485
518817b3 22486 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22487 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22488 DW_STRING (attr),
22489 &base[-1] - DW_STRING (attr));
96408a79
SA
22490}
22491
fdde2d81 22492/* Return the name of the namespace/class that DIE is defined within,
0114d602 22493 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22494
0114d602
DJ
22495 For example, if we're within the method foo() in the following
22496 code:
22497
22498 namespace N {
22499 class C {
22500 void foo () {
22501 }
22502 };
22503 }
22504
22505 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22506
0d5cff50 22507static const char *
e142c38c 22508determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22509{
518817b3
SM
22510 struct dwarf2_per_objfile *dwarf2_per_objfile
22511 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22512 struct die_info *parent, *spec_die;
22513 struct dwarf2_cu *spec_cu;
22514 struct type *parent_type;
a121b7c1 22515 const char *retval;
63d06c5c 22516
9c37b5ae 22517 if (cu->language != language_cplus
c44af4eb
TT
22518 && cu->language != language_fortran && cu->language != language_d
22519 && cu->language != language_rust)
0114d602
DJ
22520 return "";
22521
96408a79
SA
22522 retval = anonymous_struct_prefix (die, cu);
22523 if (retval)
22524 return retval;
22525
0114d602
DJ
22526 /* We have to be careful in the presence of DW_AT_specification.
22527 For example, with GCC 3.4, given the code
22528
22529 namespace N {
22530 void foo() {
22531 // Definition of N::foo.
22532 }
22533 }
22534
22535 then we'll have a tree of DIEs like this:
22536
22537 1: DW_TAG_compile_unit
22538 2: DW_TAG_namespace // N
22539 3: DW_TAG_subprogram // declaration of N::foo
22540 4: DW_TAG_subprogram // definition of N::foo
22541 DW_AT_specification // refers to die #3
22542
22543 Thus, when processing die #4, we have to pretend that we're in
22544 the context of its DW_AT_specification, namely the contex of die
22545 #3. */
22546 spec_cu = cu;
22547 spec_die = die_specification (die, &spec_cu);
22548 if (spec_die == NULL)
22549 parent = die->parent;
22550 else
63d06c5c 22551 {
0114d602
DJ
22552 parent = spec_die->parent;
22553 cu = spec_cu;
63d06c5c 22554 }
0114d602
DJ
22555
22556 if (parent == NULL)
22557 return "";
98bfdba5
PA
22558 else if (parent->building_fullname)
22559 {
22560 const char *name;
22561 const char *parent_name;
22562
22563 /* It has been seen on RealView 2.2 built binaries,
22564 DW_TAG_template_type_param types actually _defined_ as
22565 children of the parent class:
22566
22567 enum E {};
22568 template class <class Enum> Class{};
22569 Class<enum E> class_e;
22570
22571 1: DW_TAG_class_type (Class)
22572 2: DW_TAG_enumeration_type (E)
22573 3: DW_TAG_enumerator (enum1:0)
22574 3: DW_TAG_enumerator (enum2:1)
22575 ...
22576 2: DW_TAG_template_type_param
22577 DW_AT_type DW_FORM_ref_udata (E)
22578
22579 Besides being broken debug info, it can put GDB into an
22580 infinite loop. Consider:
22581
22582 When we're building the full name for Class<E>, we'll start
22583 at Class, and go look over its template type parameters,
22584 finding E. We'll then try to build the full name of E, and
22585 reach here. We're now trying to build the full name of E,
22586 and look over the parent DIE for containing scope. In the
22587 broken case, if we followed the parent DIE of E, we'd again
22588 find Class, and once again go look at its template type
22589 arguments, etc., etc. Simply don't consider such parent die
22590 as source-level parent of this die (it can't be, the language
22591 doesn't allow it), and break the loop here. */
22592 name = dwarf2_name (die, cu);
22593 parent_name = dwarf2_name (parent, cu);
b98664d3 22594 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22595 name ? name : "<unknown>",
22596 parent_name ? parent_name : "<unknown>");
22597 return "";
22598 }
63d06c5c 22599 else
0114d602
DJ
22600 switch (parent->tag)
22601 {
63d06c5c 22602 case DW_TAG_namespace:
0114d602 22603 parent_type = read_type_die (parent, cu);
acebe513
UW
22604 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22605 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22606 Work around this problem here. */
22607 if (cu->language == language_cplus
e86ca25f 22608 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22609 return "";
0114d602 22610 /* We give a name to even anonymous namespaces. */
e86ca25f 22611 return TYPE_NAME (parent_type);
63d06c5c 22612 case DW_TAG_class_type:
680b30c7 22613 case DW_TAG_interface_type:
63d06c5c 22614 case DW_TAG_structure_type:
0114d602 22615 case DW_TAG_union_type:
f55ee35c 22616 case DW_TAG_module:
0114d602 22617 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22618 if (TYPE_NAME (parent_type) != NULL)
22619 return TYPE_NAME (parent_type);
0114d602
DJ
22620 else
22621 /* An anonymous structure is only allowed non-static data
22622 members; no typedefs, no member functions, et cetera.
22623 So it does not need a prefix. */
22624 return "";
abc72ce4 22625 case DW_TAG_compile_unit:
95554aad 22626 case DW_TAG_partial_unit:
abc72ce4
DE
22627 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22628 if (cu->language == language_cplus
fd5866f6 22629 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22630 && die->child != NULL
22631 && (die->tag == DW_TAG_class_type
22632 || die->tag == DW_TAG_structure_type
22633 || die->tag == DW_TAG_union_type))
22634 {
22635 char *name = guess_full_die_structure_name (die, cu);
22636 if (name != NULL)
22637 return name;
22638 }
22639 return "";
0a4b0913
AB
22640 case DW_TAG_subprogram:
22641 /* Nested subroutines in Fortran get a prefix with the name
22642 of the parent's subroutine. */
22643 if (cu->language == language_fortran)
22644 {
22645 if ((die->tag == DW_TAG_subprogram)
22646 && (dwarf2_name (parent, cu) != NULL))
22647 return dwarf2_name (parent, cu);
22648 }
22649 return determine_prefix (parent, cu);
3d567982
TT
22650 case DW_TAG_enumeration_type:
22651 parent_type = read_type_die (parent, cu);
22652 if (TYPE_DECLARED_CLASS (parent_type))
22653 {
e86ca25f
TT
22654 if (TYPE_NAME (parent_type) != NULL)
22655 return TYPE_NAME (parent_type);
3d567982
TT
22656 return "";
22657 }
22658 /* Fall through. */
63d06c5c 22659 default:
8176b9b8 22660 return determine_prefix (parent, cu);
63d06c5c 22661 }
63d06c5c
DC
22662}
22663
3e43a32a
MS
22664/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22665 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22666 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22667 an obconcat, otherwise allocate storage for the result. The CU argument is
22668 used to determine the language and hence, the appropriate separator. */
987504bb 22669
f55ee35c 22670#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22671
22672static char *
f55ee35c
JK
22673typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22674 int physname, struct dwarf2_cu *cu)
63d06c5c 22675{
f55ee35c 22676 const char *lead = "";
5c315b68 22677 const char *sep;
63d06c5c 22678
3e43a32a
MS
22679 if (suffix == NULL || suffix[0] == '\0'
22680 || prefix == NULL || prefix[0] == '\0')
987504bb 22681 sep = "";
45280282
IB
22682 else if (cu->language == language_d)
22683 {
22684 /* For D, the 'main' function could be defined in any module, but it
22685 should never be prefixed. */
22686 if (strcmp (suffix, "D main") == 0)
22687 {
22688 prefix = "";
22689 sep = "";
22690 }
22691 else
22692 sep = ".";
22693 }
f55ee35c
JK
22694 else if (cu->language == language_fortran && physname)
22695 {
22696 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22697 DW_AT_MIPS_linkage_name is preferred and used instead. */
22698
22699 lead = "__";
22700 sep = "_MOD_";
22701 }
987504bb
JJ
22702 else
22703 sep = "::";
63d06c5c 22704
6dd47d34
DE
22705 if (prefix == NULL)
22706 prefix = "";
22707 if (suffix == NULL)
22708 suffix = "";
22709
987504bb
JJ
22710 if (obs == NULL)
22711 {
3e43a32a 22712 char *retval
224c3ddb
SM
22713 = ((char *)
22714 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22715
f55ee35c
JK
22716 strcpy (retval, lead);
22717 strcat (retval, prefix);
6dd47d34
DE
22718 strcat (retval, sep);
22719 strcat (retval, suffix);
63d06c5c
DC
22720 return retval;
22721 }
987504bb
JJ
22722 else
22723 {
22724 /* We have an obstack. */
f55ee35c 22725 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22726 }
63d06c5c
DC
22727}
22728
c906108c
SS
22729/* Return sibling of die, NULL if no sibling. */
22730
f9aca02d 22731static struct die_info *
fba45db2 22732sibling_die (struct die_info *die)
c906108c 22733{
639d11d3 22734 return die->sibling;
c906108c
SS
22735}
22736
71c25dea
TT
22737/* Get name of a die, return NULL if not found. */
22738
15d034d0
TT
22739static const char *
22740dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22741 struct obstack *obstack)
22742{
22743 if (name && cu->language == language_cplus)
22744 {
2f408ecb 22745 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22746
2f408ecb 22747 if (!canon_name.empty ())
71c25dea 22748 {
2f408ecb 22749 if (canon_name != name)
efba19b0 22750 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22751 }
22752 }
22753
22754 return name;
c906108c
SS
22755}
22756
96553a0c
DE
22757/* Get name of a die, return NULL if not found.
22758 Anonymous namespaces are converted to their magic string. */
9219021c 22759
15d034d0 22760static const char *
e142c38c 22761dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22762{
22763 struct attribute *attr;
518817b3 22764 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22765
e142c38c 22766 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22767 if ((!attr || !DW_STRING (attr))
96553a0c 22768 && die->tag != DW_TAG_namespace
53832f31
TT
22769 && die->tag != DW_TAG_class_type
22770 && die->tag != DW_TAG_interface_type
22771 && die->tag != DW_TAG_structure_type
22772 && die->tag != DW_TAG_union_type)
71c25dea
TT
22773 return NULL;
22774
22775 switch (die->tag)
22776 {
22777 case DW_TAG_compile_unit:
95554aad 22778 case DW_TAG_partial_unit:
71c25dea
TT
22779 /* Compilation units have a DW_AT_name that is a filename, not
22780 a source language identifier. */
22781 case DW_TAG_enumeration_type:
22782 case DW_TAG_enumerator:
22783 /* These tags always have simple identifiers already; no need
22784 to canonicalize them. */
22785 return DW_STRING (attr);
907af001 22786
96553a0c
DE
22787 case DW_TAG_namespace:
22788 if (attr != NULL && DW_STRING (attr) != NULL)
22789 return DW_STRING (attr);
22790 return CP_ANONYMOUS_NAMESPACE_STR;
22791
907af001
UW
22792 case DW_TAG_class_type:
22793 case DW_TAG_interface_type:
22794 case DW_TAG_structure_type:
22795 case DW_TAG_union_type:
22796 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22797 structures or unions. These were of the form "._%d" in GCC 4.1,
22798 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22799 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22800 if (attr && DW_STRING (attr)
61012eef
GB
22801 && (startswith (DW_STRING (attr), "._")
22802 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22803 return NULL;
53832f31
TT
22804
22805 /* GCC might emit a nameless typedef that has a linkage name. See
22806 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22807 if (!attr || DW_STRING (attr) == NULL)
22808 {
df5c6c50 22809 char *demangled = NULL;
53832f31 22810
73b9be8b 22811 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22812 if (attr == NULL || DW_STRING (attr) == NULL)
22813 return NULL;
22814
df5c6c50
JK
22815 /* Avoid demangling DW_STRING (attr) the second time on a second
22816 call for the same DIE. */
22817 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22818 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22819
22820 if (demangled)
22821 {
e6a959d6 22822 const char *base;
96408a79 22823
53832f31 22824 /* FIXME: we already did this for the partial symbol... */
34a68019 22825 DW_STRING (attr)
021887d8
TT
22826 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22827 demangled);
53832f31
TT
22828 DW_STRING_IS_CANONICAL (attr) = 1;
22829 xfree (demangled);
96408a79
SA
22830
22831 /* Strip any leading namespaces/classes, keep only the base name.
22832 DW_AT_name for named DIEs does not contain the prefixes. */
22833 base = strrchr (DW_STRING (attr), ':');
22834 if (base && base > DW_STRING (attr) && base[-1] == ':')
22835 return &base[1];
22836 else
22837 return DW_STRING (attr);
53832f31
TT
22838 }
22839 }
907af001
UW
22840 break;
22841
71c25dea 22842 default:
907af001
UW
22843 break;
22844 }
22845
22846 if (!DW_STRING_IS_CANONICAL (attr))
22847 {
22848 DW_STRING (attr)
22849 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22850 &objfile->per_bfd->storage_obstack);
907af001 22851 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22852 }
907af001 22853 return DW_STRING (attr);
9219021c
DC
22854}
22855
22856/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22857 is none. *EXT_CU is the CU containing DIE on input, and the CU
22858 containing the return value on output. */
9219021c
DC
22859
22860static struct die_info *
f2f0e013 22861dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22862{
22863 struct attribute *attr;
9219021c 22864
f2f0e013 22865 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22866 if (attr == NULL)
22867 return NULL;
22868
f2f0e013 22869 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22870}
22871
fa9c3fa0
TT
22872/* A convenience function that returns an "unknown" DWARF name,
22873 including the value of V. STR is the name of the entity being
22874 printed, e.g., "TAG". */
22875
22876static const char *
22877dwarf_unknown (const char *str, unsigned v)
22878{
22879 char *cell = get_print_cell ();
22880 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22881 return cell;
22882}
22883
c906108c
SS
22884/* Convert a DIE tag into its string name. */
22885
f39c6ffd 22886static const char *
aa1ee363 22887dwarf_tag_name (unsigned tag)
c906108c 22888{
f39c6ffd
TT
22889 const char *name = get_DW_TAG_name (tag);
22890
22891 if (name == NULL)
fa9c3fa0 22892 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
22893
22894 return name;
c906108c
SS
22895}
22896
22897/* Convert a DWARF attribute code into its string name. */
22898
f39c6ffd 22899static const char *
aa1ee363 22900dwarf_attr_name (unsigned attr)
c906108c 22901{
f39c6ffd
TT
22902 const char *name;
22903
c764a876 22904#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22905 if (attr == DW_AT_MIPS_fde)
22906 return "DW_AT_MIPS_fde";
22907#else
22908 if (attr == DW_AT_HP_block_index)
22909 return "DW_AT_HP_block_index";
c764a876 22910#endif
f39c6ffd
TT
22911
22912 name = get_DW_AT_name (attr);
22913
22914 if (name == NULL)
fa9c3fa0 22915 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
22916
22917 return name;
c906108c
SS
22918}
22919
a084a2a6
AT
22920/* Convert a unit type to corresponding DW_UT name. */
22921
22922static const char *
22923dwarf_unit_type_name (int unit_type) {
22924 switch (unit_type)
22925 {
22926 case 0x01:
22927 return "DW_UT_compile (0x01)";
22928 case 0x02:
22929 return "DW_UT_type (0x02)";
22930 case 0x03:
22931 return "DW_UT_partial (0x03)";
22932 case 0x04:
22933 return "DW_UT_skeleton (0x04)";
22934 case 0x05:
22935 return "DW_UT_split_compile (0x05)";
22936 case 0x06:
22937 return "DW_UT_split_type (0x06)";
22938 case 0x80:
22939 return "DW_UT_lo_user (0x80)";
22940 case 0xff:
22941 return "DW_UT_hi_user (0xff)";
22942 default:
22943 return nullptr;
22944 }
22945}
22946
c906108c
SS
22947/* Convert a DWARF value form code into its string name. */
22948
f39c6ffd 22949static const char *
aa1ee363 22950dwarf_form_name (unsigned form)
c906108c 22951{
f39c6ffd
TT
22952 const char *name = get_DW_FORM_name (form);
22953
22954 if (name == NULL)
fa9c3fa0 22955 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
22956
22957 return name;
c906108c
SS
22958}
22959
a121b7c1 22960static const char *
fba45db2 22961dwarf_bool_name (unsigned mybool)
c906108c
SS
22962{
22963 if (mybool)
22964 return "TRUE";
22965 else
22966 return "FALSE";
22967}
22968
22969/* Convert a DWARF type code into its string name. */
22970
f39c6ffd 22971static const char *
aa1ee363 22972dwarf_type_encoding_name (unsigned enc)
c906108c 22973{
f39c6ffd 22974 const char *name = get_DW_ATE_name (enc);
c906108c 22975
f39c6ffd 22976 if (name == NULL)
fa9c3fa0 22977 return dwarf_unknown ("ATE", enc);
c906108c 22978
f39c6ffd 22979 return name;
c906108c 22980}
c906108c 22981
f9aca02d 22982static void
d97bc12b 22983dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22984{
22985 unsigned int i;
22986
d97bc12b 22987 print_spaces (indent, f);
9d8780f0 22988 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22989 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22990 sect_offset_str (die->sect_off));
d97bc12b
DE
22991
22992 if (die->parent != NULL)
22993 {
22994 print_spaces (indent, f);
9d8780f0
SM
22995 fprintf_unfiltered (f, " parent at offset: %s\n",
22996 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22997 }
22998
22999 print_spaces (indent, f);
23000 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23001 dwarf_bool_name (die->child != NULL));
c906108c 23002
d97bc12b
DE
23003 print_spaces (indent, f);
23004 fprintf_unfiltered (f, " attributes:\n");
23005
c906108c
SS
23006 for (i = 0; i < die->num_attrs; ++i)
23007 {
d97bc12b
DE
23008 print_spaces (indent, f);
23009 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23010 dwarf_attr_name (die->attrs[i].name),
23011 dwarf_form_name (die->attrs[i].form));
d97bc12b 23012
c906108c
SS
23013 switch (die->attrs[i].form)
23014 {
c906108c 23015 case DW_FORM_addr:
336d760d 23016 case DW_FORM_addrx:
3019eac3 23017 case DW_FORM_GNU_addr_index:
d97bc12b 23018 fprintf_unfiltered (f, "address: ");
5af949e3 23019 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23020 break;
23021 case DW_FORM_block2:
23022 case DW_FORM_block4:
23023 case DW_FORM_block:
23024 case DW_FORM_block1:
56eb65bd
SP
23025 fprintf_unfiltered (f, "block: size %s",
23026 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23027 break;
2dc7f7b3 23028 case DW_FORM_exprloc:
56eb65bd
SP
23029 fprintf_unfiltered (f, "expression: size %s",
23030 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23031 break;
0224619f
JK
23032 case DW_FORM_data16:
23033 fprintf_unfiltered (f, "constant of 16 bytes");
23034 break;
4568ecf9
DE
23035 case DW_FORM_ref_addr:
23036 fprintf_unfiltered (f, "ref address: ");
23037 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23038 break;
36586728
TT
23039 case DW_FORM_GNU_ref_alt:
23040 fprintf_unfiltered (f, "alt ref address: ");
23041 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23042 break;
10b3939b
DJ
23043 case DW_FORM_ref1:
23044 case DW_FORM_ref2:
23045 case DW_FORM_ref4:
4568ecf9
DE
23046 case DW_FORM_ref8:
23047 case DW_FORM_ref_udata:
d97bc12b 23048 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23049 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23050 break;
c906108c
SS
23051 case DW_FORM_data1:
23052 case DW_FORM_data2:
23053 case DW_FORM_data4:
ce5d95e1 23054 case DW_FORM_data8:
c906108c
SS
23055 case DW_FORM_udata:
23056 case DW_FORM_sdata:
43bbcdc2
PH
23057 fprintf_unfiltered (f, "constant: %s",
23058 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23059 break;
2dc7f7b3
TT
23060 case DW_FORM_sec_offset:
23061 fprintf_unfiltered (f, "section offset: %s",
23062 pulongest (DW_UNSND (&die->attrs[i])));
23063 break;
55f1336d 23064 case DW_FORM_ref_sig8:
ac9ec31b
DE
23065 fprintf_unfiltered (f, "signature: %s",
23066 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23067 break;
c906108c 23068 case DW_FORM_string:
4bdf3d34 23069 case DW_FORM_strp:
43988095 23070 case DW_FORM_line_strp:
cf532bd1 23071 case DW_FORM_strx:
3019eac3 23072 case DW_FORM_GNU_str_index:
36586728 23073 case DW_FORM_GNU_strp_alt:
8285870a 23074 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23075 DW_STRING (&die->attrs[i])
8285870a
JK
23076 ? DW_STRING (&die->attrs[i]) : "",
23077 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23078 break;
23079 case DW_FORM_flag:
23080 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23081 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23082 else
d97bc12b 23083 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23084 break;
2dc7f7b3
TT
23085 case DW_FORM_flag_present:
23086 fprintf_unfiltered (f, "flag: TRUE");
23087 break;
a8329558 23088 case DW_FORM_indirect:
0963b4bd
MS
23089 /* The reader will have reduced the indirect form to
23090 the "base form" so this form should not occur. */
5f48f8f3 23091 fprintf_unfiltered (f,
3e43a32a 23092 "unexpected attribute form: DW_FORM_indirect");
a8329558 23093 break;
663c44ac
JK
23094 case DW_FORM_implicit_const:
23095 fprintf_unfiltered (f, "constant: %s",
23096 plongest (DW_SND (&die->attrs[i])));
23097 break;
c906108c 23098 default:
d97bc12b 23099 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23100 die->attrs[i].form);
d97bc12b 23101 break;
c906108c 23102 }
d97bc12b 23103 fprintf_unfiltered (f, "\n");
c906108c
SS
23104 }
23105}
23106
f9aca02d 23107static void
d97bc12b 23108dump_die_for_error (struct die_info *die)
c906108c 23109{
d97bc12b
DE
23110 dump_die_shallow (gdb_stderr, 0, die);
23111}
23112
23113static void
23114dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23115{
23116 int indent = level * 4;
23117
23118 gdb_assert (die != NULL);
23119
23120 if (level >= max_level)
23121 return;
23122
23123 dump_die_shallow (f, indent, die);
23124
23125 if (die->child != NULL)
c906108c 23126 {
d97bc12b
DE
23127 print_spaces (indent, f);
23128 fprintf_unfiltered (f, " Children:");
23129 if (level + 1 < max_level)
23130 {
23131 fprintf_unfiltered (f, "\n");
23132 dump_die_1 (f, level + 1, max_level, die->child);
23133 }
23134 else
23135 {
3e43a32a
MS
23136 fprintf_unfiltered (f,
23137 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23138 }
23139 }
23140
23141 if (die->sibling != NULL && level > 0)
23142 {
23143 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23144 }
23145}
23146
d97bc12b
DE
23147/* This is called from the pdie macro in gdbinit.in.
23148 It's not static so gcc will keep a copy callable from gdb. */
23149
23150void
23151dump_die (struct die_info *die, int max_level)
23152{
23153 dump_die_1 (gdb_stdlog, 0, max_level, die);
23154}
23155
f9aca02d 23156static void
51545339 23157store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23158{
51545339 23159 void **slot;
c906108c 23160
9c541725
PA
23161 slot = htab_find_slot_with_hash (cu->die_hash, die,
23162 to_underlying (die->sect_off),
b64f50a1 23163 INSERT);
51545339
DJ
23164
23165 *slot = die;
c906108c
SS
23166}
23167
b64f50a1
JK
23168/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23169 required kind. */
23170
23171static sect_offset
ff39bb5e 23172dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23173{
7771576e 23174 if (attr_form_is_ref (attr))
9c541725 23175 return (sect_offset) DW_UNSND (attr);
93311388 23176
b98664d3 23177 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23178 dwarf_form_name (attr->form));
9c541725 23179 return {};
c906108c
SS
23180}
23181
43bbcdc2
PH
23182/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23183 * the value held by the attribute is not constant. */
a02abb62 23184
43bbcdc2 23185static LONGEST
ff39bb5e 23186dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23187{
663c44ac 23188 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23189 return DW_SND (attr);
23190 else if (attr->form == DW_FORM_udata
23191 || attr->form == DW_FORM_data1
23192 || attr->form == DW_FORM_data2
23193 || attr->form == DW_FORM_data4
23194 || attr->form == DW_FORM_data8)
23195 return DW_UNSND (attr);
23196 else
23197 {
0224619f 23198 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23199 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23200 dwarf_form_name (attr->form));
23201 return default_value;
23202 }
23203}
23204
348e048f
DE
23205/* Follow reference or signature attribute ATTR of SRC_DIE.
23206 On entry *REF_CU is the CU of SRC_DIE.
23207 On exit *REF_CU is the CU of the result. */
23208
23209static struct die_info *
ff39bb5e 23210follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23211 struct dwarf2_cu **ref_cu)
23212{
23213 struct die_info *die;
23214
7771576e 23215 if (attr_form_is_ref (attr))
348e048f 23216 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23217 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23218 die = follow_die_sig (src_die, attr, ref_cu);
23219 else
23220 {
23221 dump_die_for_error (src_die);
23222 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23223 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23224 }
23225
23226 return die;
03dd20cc
DJ
23227}
23228
5c631832 23229/* Follow reference OFFSET.
673bfd45
DE
23230 On entry *REF_CU is the CU of the source die referencing OFFSET.
23231 On exit *REF_CU is the CU of the result.
23232 Returns NULL if OFFSET is invalid. */
f504f079 23233
f9aca02d 23234static struct die_info *
9c541725 23235follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23236 struct dwarf2_cu **ref_cu)
c906108c 23237{
10b3939b 23238 struct die_info temp_die;
f2f0e013 23239 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23240 struct dwarf2_per_objfile *dwarf2_per_objfile
23241 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23242
348e048f
DE
23243 gdb_assert (cu->per_cu != NULL);
23244
98bfdba5
PA
23245 target_cu = cu;
23246
3019eac3 23247 if (cu->per_cu->is_debug_types)
348e048f
DE
23248 {
23249 /* .debug_types CUs cannot reference anything outside their CU.
23250 If they need to, they have to reference a signatured type via
55f1336d 23251 DW_FORM_ref_sig8. */
9c541725 23252 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23253 return NULL;
348e048f 23254 }
36586728 23255 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23256 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23257 {
23258 struct dwarf2_per_cu_data *per_cu;
9a619af0 23259
9c541725 23260 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23261 dwarf2_per_objfile);
03dd20cc
DJ
23262
23263 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23264 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23265 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23266
10b3939b
DJ
23267 target_cu = per_cu->cu;
23268 }
98bfdba5
PA
23269 else if (cu->dies == NULL)
23270 {
23271 /* We're loading full DIEs during partial symbol reading. */
23272 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23273 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23274 }
c906108c 23275
f2f0e013 23276 *ref_cu = target_cu;
9c541725 23277 temp_die.sect_off = sect_off;
c24bdb02
KS
23278
23279 if (target_cu != cu)
23280 target_cu->ancestor = cu;
23281
9a3c8263 23282 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23283 &temp_die,
23284 to_underlying (sect_off));
5c631832 23285}
10b3939b 23286
5c631832
JK
23287/* Follow reference attribute ATTR of SRC_DIE.
23288 On entry *REF_CU is the CU of SRC_DIE.
23289 On exit *REF_CU is the CU of the result. */
23290
23291static struct die_info *
ff39bb5e 23292follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23293 struct dwarf2_cu **ref_cu)
23294{
9c541725 23295 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23296 struct dwarf2_cu *cu = *ref_cu;
23297 struct die_info *die;
23298
9c541725 23299 die = follow_die_offset (sect_off,
36586728
TT
23300 (attr->form == DW_FORM_GNU_ref_alt
23301 || cu->per_cu->is_dwz),
23302 ref_cu);
5c631832 23303 if (!die)
9d8780f0
SM
23304 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23305 "at %s [in module %s]"),
23306 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23307 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23308
5c631832
JK
23309 return die;
23310}
23311
9c541725 23312/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23313 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23314 dwarf2_locexpr_baton->data has lifetime of
23315 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23316
23317struct dwarf2_locexpr_baton
9c541725 23318dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23319 struct dwarf2_per_cu_data *per_cu,
23320 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23321 void *baton, bool resolve_abstract_p)
5c631832 23322{
918dd910 23323 struct dwarf2_cu *cu;
5c631832
JK
23324 struct die_info *die;
23325 struct attribute *attr;
23326 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23327 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23328 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23329
918dd910 23330 if (per_cu->cu == NULL)
58f0c718 23331 load_cu (per_cu, false);
918dd910 23332 cu = per_cu->cu;
cc12ce38
DE
23333 if (cu == NULL)
23334 {
23335 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23336 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23337 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23338 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23339 }
918dd910 23340
9c541725 23341 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23342 if (!die)
9d8780f0
SM
23343 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23344 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23345
23346 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23347 if (!attr && resolve_abstract_p
3360b6e7 23348 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23349 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23350 {
23351 CORE_ADDR pc = (*get_frame_pc) (baton);
eba4caf2
TV
23352 CORE_ADDR baseaddr
23353 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23354 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23355
3360b6e7
TV
23356 for (const auto &cand_off
23357 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23358 {
3360b6e7
TV
23359 struct dwarf2_cu *cand_cu = cu;
23360 struct die_info *cand
23361 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23362 if (!cand
23363 || !cand->parent
e4a62c65
TV
23364 || cand->parent->tag != DW_TAG_subprogram)
23365 continue;
23366
23367 CORE_ADDR pc_low, pc_high;
23368 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23369 if (pc_low == ((CORE_ADDR) -1))
23370 continue;
23371 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23372 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23373 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23374 continue;
23375
23376 die = cand;
23377 attr = dwarf2_attr (die, DW_AT_location, cu);
23378 break;
23379 }
23380 }
23381
5c631832
JK
23382 if (!attr)
23383 {
e103e986
JK
23384 /* DWARF: "If there is no such attribute, then there is no effect.".
23385 DATA is ignored if SIZE is 0. */
5c631832 23386
e103e986 23387 retval.data = NULL;
5c631832
JK
23388 retval.size = 0;
23389 }
8cf6f0b1
TT
23390 else if (attr_form_is_section_offset (attr))
23391 {
23392 struct dwarf2_loclist_baton loclist_baton;
23393 CORE_ADDR pc = (*get_frame_pc) (baton);
23394 size_t size;
23395
23396 fill_in_loclist_baton (cu, &loclist_baton, attr);
23397
23398 retval.data = dwarf2_find_location_expression (&loclist_baton,
23399 &size, pc);
23400 retval.size = size;
23401 }
5c631832
JK
23402 else
23403 {
23404 if (!attr_form_is_block (attr))
9d8780f0 23405 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23406 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23407 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23408
23409 retval.data = DW_BLOCK (attr)->data;
23410 retval.size = DW_BLOCK (attr)->size;
23411 }
23412 retval.per_cu = cu->per_cu;
918dd910 23413
ed2dc618 23414 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23415
5c631832 23416 return retval;
348e048f
DE
23417}
23418
8b9737bf
TT
23419/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23420 offset. */
23421
23422struct dwarf2_locexpr_baton
23423dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23424 struct dwarf2_per_cu_data *per_cu,
23425 CORE_ADDR (*get_frame_pc) (void *baton),
23426 void *baton)
23427{
9c541725 23428 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23429
9c541725 23430 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23431}
23432
b6807d98
TT
23433/* Write a constant of a given type as target-ordered bytes into
23434 OBSTACK. */
23435
23436static const gdb_byte *
23437write_constant_as_bytes (struct obstack *obstack,
23438 enum bfd_endian byte_order,
23439 struct type *type,
23440 ULONGEST value,
23441 LONGEST *len)
23442{
23443 gdb_byte *result;
23444
23445 *len = TYPE_LENGTH (type);
224c3ddb 23446 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23447 store_unsigned_integer (result, *len, byte_order, value);
23448
23449 return result;
23450}
23451
23452/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23453 pointer to the constant bytes and set LEN to the length of the
23454 data. If memory is needed, allocate it on OBSTACK. If the DIE
23455 does not have a DW_AT_const_value, return NULL. */
23456
23457const gdb_byte *
9c541725 23458dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23459 struct dwarf2_per_cu_data *per_cu,
23460 struct obstack *obstack,
23461 LONGEST *len)
23462{
23463 struct dwarf2_cu *cu;
23464 struct die_info *die;
23465 struct attribute *attr;
23466 const gdb_byte *result = NULL;
23467 struct type *type;
23468 LONGEST value;
23469 enum bfd_endian byte_order;
e3b94546 23470 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23471
b6807d98 23472 if (per_cu->cu == NULL)
58f0c718 23473 load_cu (per_cu, false);
b6807d98 23474 cu = per_cu->cu;
cc12ce38
DE
23475 if (cu == NULL)
23476 {
23477 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23478 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23479 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23480 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23481 }
b6807d98 23482
9c541725 23483 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23484 if (!die)
9d8780f0
SM
23485 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23486 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23487
23488 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23489 if (attr == NULL)
23490 return NULL;
23491
e3b94546 23492 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23493 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23494
23495 switch (attr->form)
23496 {
23497 case DW_FORM_addr:
336d760d 23498 case DW_FORM_addrx:
b6807d98
TT
23499 case DW_FORM_GNU_addr_index:
23500 {
23501 gdb_byte *tem;
23502
23503 *len = cu->header.addr_size;
224c3ddb 23504 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23505 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23506 result = tem;
23507 }
23508 break;
23509 case DW_FORM_string:
23510 case DW_FORM_strp:
cf532bd1 23511 case DW_FORM_strx:
b6807d98
TT
23512 case DW_FORM_GNU_str_index:
23513 case DW_FORM_GNU_strp_alt:
23514 /* DW_STRING is already allocated on the objfile obstack, point
23515 directly to it. */
23516 result = (const gdb_byte *) DW_STRING (attr);
23517 *len = strlen (DW_STRING (attr));
23518 break;
23519 case DW_FORM_block1:
23520 case DW_FORM_block2:
23521 case DW_FORM_block4:
23522 case DW_FORM_block:
23523 case DW_FORM_exprloc:
0224619f 23524 case DW_FORM_data16:
b6807d98
TT
23525 result = DW_BLOCK (attr)->data;
23526 *len = DW_BLOCK (attr)->size;
23527 break;
23528
23529 /* The DW_AT_const_value attributes are supposed to carry the
23530 symbol's value "represented as it would be on the target
23531 architecture." By the time we get here, it's already been
23532 converted to host endianness, so we just need to sign- or
23533 zero-extend it as appropriate. */
23534 case DW_FORM_data1:
23535 type = die_type (die, cu);
23536 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23537 if (result == NULL)
23538 result = write_constant_as_bytes (obstack, byte_order,
23539 type, value, len);
23540 break;
23541 case DW_FORM_data2:
23542 type = die_type (die, cu);
23543 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23544 if (result == NULL)
23545 result = write_constant_as_bytes (obstack, byte_order,
23546 type, value, len);
23547 break;
23548 case DW_FORM_data4:
23549 type = die_type (die, cu);
23550 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23551 if (result == NULL)
23552 result = write_constant_as_bytes (obstack, byte_order,
23553 type, value, len);
23554 break;
23555 case DW_FORM_data8:
23556 type = die_type (die, cu);
23557 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23558 if (result == NULL)
23559 result = write_constant_as_bytes (obstack, byte_order,
23560 type, value, len);
23561 break;
23562
23563 case DW_FORM_sdata:
663c44ac 23564 case DW_FORM_implicit_const:
b6807d98
TT
23565 type = die_type (die, cu);
23566 result = write_constant_as_bytes (obstack, byte_order,
23567 type, DW_SND (attr), len);
23568 break;
23569
23570 case DW_FORM_udata:
23571 type = die_type (die, cu);
23572 result = write_constant_as_bytes (obstack, byte_order,
23573 type, DW_UNSND (attr), len);
23574 break;
23575
23576 default:
b98664d3 23577 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23578 dwarf_form_name (attr->form));
23579 break;
23580 }
23581
23582 return result;
23583}
23584
7942e96e
AA
23585/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23586 valid type for this die is found. */
23587
23588struct type *
9c541725 23589dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23590 struct dwarf2_per_cu_data *per_cu)
23591{
23592 struct dwarf2_cu *cu;
23593 struct die_info *die;
23594
7942e96e 23595 if (per_cu->cu == NULL)
58f0c718 23596 load_cu (per_cu, false);
7942e96e
AA
23597 cu = per_cu->cu;
23598 if (!cu)
23599 return NULL;
23600
9c541725 23601 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23602 if (!die)
23603 return NULL;
23604
23605 return die_type (die, cu);
23606}
23607
8a9b8146
TT
23608/* Return the type of the DIE at DIE_OFFSET in the CU named by
23609 PER_CU. */
23610
23611struct type *
b64f50a1 23612dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23613 struct dwarf2_per_cu_data *per_cu)
23614{
9c541725 23615 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23616 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23617}
23618
ac9ec31b 23619/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23620 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23621 On exit *REF_CU is the CU of the result.
23622 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23623
23624static struct die_info *
ac9ec31b
DE
23625follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23626 struct dwarf2_cu **ref_cu)
348e048f 23627{
348e048f 23628 struct die_info temp_die;
c24bdb02 23629 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23630 struct die_info *die;
23631
ac9ec31b
DE
23632 /* While it might be nice to assert sig_type->type == NULL here,
23633 we can get here for DW_AT_imported_declaration where we need
23634 the DIE not the type. */
348e048f
DE
23635
23636 /* If necessary, add it to the queue and load its DIEs. */
23637
95554aad 23638 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23639 read_signatured_type (sig_type);
348e048f 23640
348e048f 23641 sig_cu = sig_type->per_cu.cu;
69d751e3 23642 gdb_assert (sig_cu != NULL);
9c541725
PA
23643 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23644 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23645 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23646 to_underlying (temp_die.sect_off));
348e048f
DE
23647 if (die)
23648 {
ed2dc618 23649 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23650 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23651
796a7ff8
DE
23652 /* For .gdb_index version 7 keep track of included TUs.
23653 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23654 if (dwarf2_per_objfile->index_table != NULL
23655 && dwarf2_per_objfile->index_table->version <= 7)
23656 {
ae640021 23657 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23658 }
23659
348e048f 23660 *ref_cu = sig_cu;
c24bdb02
KS
23661 if (sig_cu != cu)
23662 sig_cu->ancestor = cu;
23663
348e048f
DE
23664 return die;
23665 }
23666
ac9ec31b
DE
23667 return NULL;
23668}
23669
23670/* Follow signatured type referenced by ATTR in SRC_DIE.
23671 On entry *REF_CU is the CU of SRC_DIE.
23672 On exit *REF_CU is the CU of the result.
23673 The result is the DIE of the type.
23674 If the referenced type cannot be found an error is thrown. */
23675
23676static struct die_info *
ff39bb5e 23677follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23678 struct dwarf2_cu **ref_cu)
23679{
23680 ULONGEST signature = DW_SIGNATURE (attr);
23681 struct signatured_type *sig_type;
23682 struct die_info *die;
23683
23684 gdb_assert (attr->form == DW_FORM_ref_sig8);
23685
a2ce51a0 23686 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23687 /* sig_type will be NULL if the signatured type is missing from
23688 the debug info. */
23689 if (sig_type == NULL)
23690 {
23691 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23692 " from DIE at %s [in module %s]"),
23693 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23694 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23695 }
23696
23697 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23698 if (die == NULL)
23699 {
23700 dump_die_for_error (src_die);
23701 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23702 " from DIE at %s [in module %s]"),
23703 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23704 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23705 }
23706
23707 return die;
23708}
23709
23710/* Get the type specified by SIGNATURE referenced in DIE/CU,
23711 reading in and processing the type unit if necessary. */
23712
23713static struct type *
23714get_signatured_type (struct die_info *die, ULONGEST signature,
23715 struct dwarf2_cu *cu)
23716{
518817b3
SM
23717 struct dwarf2_per_objfile *dwarf2_per_objfile
23718 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23719 struct signatured_type *sig_type;
23720 struct dwarf2_cu *type_cu;
23721 struct die_info *type_die;
23722 struct type *type;
23723
a2ce51a0 23724 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23725 /* sig_type will be NULL if the signatured type is missing from
23726 the debug info. */
23727 if (sig_type == NULL)
23728 {
b98664d3 23729 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23730 " from DIE at %s [in module %s]"),
23731 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23732 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23733 return build_error_marker_type (cu, die);
23734 }
23735
23736 /* If we already know the type we're done. */
23737 if (sig_type->type != NULL)
23738 return sig_type->type;
23739
23740 type_cu = cu;
23741 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23742 if (type_die != NULL)
23743 {
23744 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23745 is created. This is important, for example, because for c++ classes
23746 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23747 type = read_type_die (type_die, type_cu);
23748 if (type == NULL)
23749 {
b98664d3 23750 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23751 " referenced from DIE at %s [in module %s]"),
23752 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23753 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23754 type = build_error_marker_type (cu, die);
23755 }
23756 }
23757 else
23758 {
b98664d3 23759 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23760 " from DIE at %s [in module %s]"),
23761 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23762 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23763 type = build_error_marker_type (cu, die);
23764 }
23765 sig_type->type = type;
23766
23767 return type;
23768}
23769
23770/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23771 reading in and processing the type unit if necessary. */
23772
23773static struct type *
ff39bb5e 23774get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23775 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23776{
23777 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23778 if (attr_form_is_ref (attr))
ac9ec31b
DE
23779 {
23780 struct dwarf2_cu *type_cu = cu;
23781 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23782
23783 return read_type_die (type_die, type_cu);
23784 }
23785 else if (attr->form == DW_FORM_ref_sig8)
23786 {
23787 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23788 }
23789 else
23790 {
518817b3
SM
23791 struct dwarf2_per_objfile *dwarf2_per_objfile
23792 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23793
b98664d3 23794 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23795 " at %s [in module %s]"),
23796 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23797 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23798 return build_error_marker_type (cu, die);
23799 }
348e048f
DE
23800}
23801
e5fe5e75 23802/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23803
23804static void
e5fe5e75 23805load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23806{
52dc124a 23807 struct signatured_type *sig_type;
348e048f 23808
f4dc4d17
DE
23809 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23810 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23811
6721b2ec
DE
23812 /* We have the per_cu, but we need the signatured_type.
23813 Fortunately this is an easy translation. */
23814 gdb_assert (per_cu->is_debug_types);
23815 sig_type = (struct signatured_type *) per_cu;
348e048f 23816
6721b2ec 23817 gdb_assert (per_cu->cu == NULL);
348e048f 23818
52dc124a 23819 read_signatured_type (sig_type);
348e048f 23820
6721b2ec 23821 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23822}
23823
dee91e82
DE
23824/* die_reader_func for read_signatured_type.
23825 This is identical to load_full_comp_unit_reader,
23826 but is kept separate for now. */
348e048f
DE
23827
23828static void
dee91e82 23829read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23830 const gdb_byte *info_ptr,
dee91e82
DE
23831 struct die_info *comp_unit_die,
23832 int has_children,
23833 void *data)
348e048f 23834{
dee91e82 23835 struct dwarf2_cu *cu = reader->cu;
348e048f 23836
dee91e82
DE
23837 gdb_assert (cu->die_hash == NULL);
23838 cu->die_hash =
23839 htab_create_alloc_ex (cu->header.length / 12,
23840 die_hash,
23841 die_eq,
23842 NULL,
23843 &cu->comp_unit_obstack,
23844 hashtab_obstack_allocate,
23845 dummy_obstack_deallocate);
348e048f 23846
dee91e82
DE
23847 if (has_children)
23848 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23849 &info_ptr, comp_unit_die);
23850 cu->dies = comp_unit_die;
23851 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23852
23853 /* We try not to read any attributes in this function, because not
9cdd5dbd 23854 all CUs needed for references have been loaded yet, and symbol
348e048f 23855 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23856 or we won't be able to build types correctly.
23857 Similarly, if we do not read the producer, we can not apply
23858 producer-specific interpretation. */
95554aad 23859 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23860}
348e048f 23861
3019eac3
DE
23862/* Read in a signatured type and build its CU and DIEs.
23863 If the type is a stub for the real type in a DWO file,
23864 read in the real type from the DWO file as well. */
dee91e82
DE
23865
23866static void
23867read_signatured_type (struct signatured_type *sig_type)
23868{
23869 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23870
3019eac3 23871 gdb_assert (per_cu->is_debug_types);
dee91e82 23872 gdb_assert (per_cu->cu == NULL);
348e048f 23873
58f0c718 23874 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23875 read_signatured_type_reader, NULL);
7ee85ab1 23876 sig_type->per_cu.tu_read = 1;
c906108c
SS
23877}
23878
c906108c
SS
23879/* Decode simple location descriptions.
23880 Given a pointer to a dwarf block that defines a location, compute
23881 the location and return the value.
23882
4cecd739
DJ
23883 NOTE drow/2003-11-18: This function is called in two situations
23884 now: for the address of static or global variables (partial symbols
23885 only) and for offsets into structures which are expected to be
23886 (more or less) constant. The partial symbol case should go away,
23887 and only the constant case should remain. That will let this
23888 function complain more accurately. A few special modes are allowed
23889 without complaint for global variables (for instance, global
23890 register values and thread-local values).
c906108c
SS
23891
23892 A location description containing no operations indicates that the
4cecd739 23893 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23894 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23895 callers will only want a very basic result and this can become a
21ae7a4d
JK
23896 complaint.
23897
23898 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23899
23900static CORE_ADDR
e7c27a73 23901decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23902{
518817b3 23903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23904 size_t i;
23905 size_t size = blk->size;
d521ce57 23906 const gdb_byte *data = blk->data;
21ae7a4d
JK
23907 CORE_ADDR stack[64];
23908 int stacki;
23909 unsigned int bytes_read, unsnd;
23910 gdb_byte op;
c906108c 23911
21ae7a4d
JK
23912 i = 0;
23913 stacki = 0;
23914 stack[stacki] = 0;
23915 stack[++stacki] = 0;
23916
23917 while (i < size)
23918 {
23919 op = data[i++];
23920 switch (op)
23921 {
23922 case DW_OP_lit0:
23923 case DW_OP_lit1:
23924 case DW_OP_lit2:
23925 case DW_OP_lit3:
23926 case DW_OP_lit4:
23927 case DW_OP_lit5:
23928 case DW_OP_lit6:
23929 case DW_OP_lit7:
23930 case DW_OP_lit8:
23931 case DW_OP_lit9:
23932 case DW_OP_lit10:
23933 case DW_OP_lit11:
23934 case DW_OP_lit12:
23935 case DW_OP_lit13:
23936 case DW_OP_lit14:
23937 case DW_OP_lit15:
23938 case DW_OP_lit16:
23939 case DW_OP_lit17:
23940 case DW_OP_lit18:
23941 case DW_OP_lit19:
23942 case DW_OP_lit20:
23943 case DW_OP_lit21:
23944 case DW_OP_lit22:
23945 case DW_OP_lit23:
23946 case DW_OP_lit24:
23947 case DW_OP_lit25:
23948 case DW_OP_lit26:
23949 case DW_OP_lit27:
23950 case DW_OP_lit28:
23951 case DW_OP_lit29:
23952 case DW_OP_lit30:
23953 case DW_OP_lit31:
23954 stack[++stacki] = op - DW_OP_lit0;
23955 break;
f1bea926 23956
21ae7a4d
JK
23957 case DW_OP_reg0:
23958 case DW_OP_reg1:
23959 case DW_OP_reg2:
23960 case DW_OP_reg3:
23961 case DW_OP_reg4:
23962 case DW_OP_reg5:
23963 case DW_OP_reg6:
23964 case DW_OP_reg7:
23965 case DW_OP_reg8:
23966 case DW_OP_reg9:
23967 case DW_OP_reg10:
23968 case DW_OP_reg11:
23969 case DW_OP_reg12:
23970 case DW_OP_reg13:
23971 case DW_OP_reg14:
23972 case DW_OP_reg15:
23973 case DW_OP_reg16:
23974 case DW_OP_reg17:
23975 case DW_OP_reg18:
23976 case DW_OP_reg19:
23977 case DW_OP_reg20:
23978 case DW_OP_reg21:
23979 case DW_OP_reg22:
23980 case DW_OP_reg23:
23981 case DW_OP_reg24:
23982 case DW_OP_reg25:
23983 case DW_OP_reg26:
23984 case DW_OP_reg27:
23985 case DW_OP_reg28:
23986 case DW_OP_reg29:
23987 case DW_OP_reg30:
23988 case DW_OP_reg31:
23989 stack[++stacki] = op - DW_OP_reg0;
23990 if (i < size)
23991 dwarf2_complex_location_expr_complaint ();
23992 break;
c906108c 23993
21ae7a4d
JK
23994 case DW_OP_regx:
23995 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23996 i += bytes_read;
23997 stack[++stacki] = unsnd;
23998 if (i < size)
23999 dwarf2_complex_location_expr_complaint ();
24000 break;
c906108c 24001
21ae7a4d
JK
24002 case DW_OP_addr:
24003 stack[++stacki] = read_address (objfile->obfd, &data[i],
24004 cu, &bytes_read);
24005 i += bytes_read;
24006 break;
d53d4ac5 24007
21ae7a4d
JK
24008 case DW_OP_const1u:
24009 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24010 i += 1;
24011 break;
24012
24013 case DW_OP_const1s:
24014 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24015 i += 1;
24016 break;
24017
24018 case DW_OP_const2u:
24019 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24020 i += 2;
24021 break;
24022
24023 case DW_OP_const2s:
24024 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24025 i += 2;
24026 break;
d53d4ac5 24027
21ae7a4d
JK
24028 case DW_OP_const4u:
24029 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24030 i += 4;
24031 break;
24032
24033 case DW_OP_const4s:
24034 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24035 i += 4;
24036 break;
24037
585861ea
JK
24038 case DW_OP_const8u:
24039 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24040 i += 8;
24041 break;
24042
21ae7a4d
JK
24043 case DW_OP_constu:
24044 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24045 &bytes_read);
24046 i += bytes_read;
24047 break;
24048
24049 case DW_OP_consts:
24050 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24051 i += bytes_read;
24052 break;
24053
24054 case DW_OP_dup:
24055 stack[stacki + 1] = stack[stacki];
24056 stacki++;
24057 break;
24058
24059 case DW_OP_plus:
24060 stack[stacki - 1] += stack[stacki];
24061 stacki--;
24062 break;
24063
24064 case DW_OP_plus_uconst:
24065 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24066 &bytes_read);
24067 i += bytes_read;
24068 break;
24069
24070 case DW_OP_minus:
24071 stack[stacki - 1] -= stack[stacki];
24072 stacki--;
24073 break;
24074
24075 case DW_OP_deref:
24076 /* If we're not the last op, then we definitely can't encode
24077 this using GDB's address_class enum. This is valid for partial
24078 global symbols, although the variable's address will be bogus
24079 in the psymtab. */
24080 if (i < size)
24081 dwarf2_complex_location_expr_complaint ();
24082 break;
24083
24084 case DW_OP_GNU_push_tls_address:
4aa4e28b 24085 case DW_OP_form_tls_address:
21ae7a4d
JK
24086 /* The top of the stack has the offset from the beginning
24087 of the thread control block at which the variable is located. */
24088 /* Nothing should follow this operator, so the top of stack would
24089 be returned. */
24090 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24091 address will be bogus in the psymtab. Make it always at least
24092 non-zero to not look as a variable garbage collected by linker
24093 which have DW_OP_addr 0. */
21ae7a4d
JK
24094 if (i < size)
24095 dwarf2_complex_location_expr_complaint ();
585861ea 24096 stack[stacki]++;
21ae7a4d
JK
24097 break;
24098
24099 case DW_OP_GNU_uninit:
24100 break;
24101
336d760d 24102 case DW_OP_addrx:
3019eac3 24103 case DW_OP_GNU_addr_index:
49f6c839 24104 case DW_OP_GNU_const_index:
3019eac3
DE
24105 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24106 &bytes_read);
24107 i += bytes_read;
24108 break;
24109
21ae7a4d
JK
24110 default:
24111 {
f39c6ffd 24112 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24113
24114 if (name)
b98664d3 24115 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24116 name);
24117 else
b98664d3 24118 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24119 op);
24120 }
24121
24122 return (stack[stacki]);
d53d4ac5 24123 }
3c6e0cb3 24124
21ae7a4d
JK
24125 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24126 outside of the allocated space. Also enforce minimum>0. */
24127 if (stacki >= ARRAY_SIZE (stack) - 1)
24128 {
b98664d3 24129 complaint (_("location description stack overflow"));
21ae7a4d
JK
24130 return 0;
24131 }
24132
24133 if (stacki <= 0)
24134 {
b98664d3 24135 complaint (_("location description stack underflow"));
21ae7a4d
JK
24136 return 0;
24137 }
24138 }
24139 return (stack[stacki]);
c906108c
SS
24140}
24141
24142/* memory allocation interface */
24143
c906108c 24144static struct dwarf_block *
7b5a2f43 24145dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24146{
8d749320 24147 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24148}
24149
c906108c 24150static struct die_info *
b60c80d6 24151dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24152{
24153 struct die_info *die;
b60c80d6
DJ
24154 size_t size = sizeof (struct die_info);
24155
24156 if (num_attrs > 1)
24157 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24158
b60c80d6 24159 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24160 memset (die, 0, sizeof (struct die_info));
24161 return (die);
24162}
2e276125
JB
24163
24164\f
24165/* Macro support. */
24166
233d95b5
JK
24167/* Return file name relative to the compilation directory of file number I in
24168 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24169 responsible for freeing it. */
233d95b5 24170
2e276125 24171static char *
233d95b5 24172file_file_name (int file, struct line_header *lh)
2e276125 24173{
6a83a1e6
EZ
24174 /* Is the file number a valid index into the line header's file name
24175 table? Remember that file numbers start with one, not zero. */
fff8551c 24176 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 24177 {
8c43009f 24178 const file_entry &fe = lh->file_names[file - 1];
6e70227d 24179
8c43009f
PA
24180 if (!IS_ABSOLUTE_PATH (fe.name))
24181 {
24182 const char *dir = fe.include_dir (lh);
24183 if (dir != NULL)
24184 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24185 }
24186 return xstrdup (fe.name);
6a83a1e6 24187 }
2e276125
JB
24188 else
24189 {
6a83a1e6
EZ
24190 /* The compiler produced a bogus file number. We can at least
24191 record the macro definitions made in the file, even if we
24192 won't be able to find the file by name. */
24193 char fake_name[80];
9a619af0 24194
8c042590
PM
24195 xsnprintf (fake_name, sizeof (fake_name),
24196 "<bad macro file number %d>", file);
2e276125 24197
b98664d3 24198 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24199 file);
2e276125 24200
6a83a1e6 24201 return xstrdup (fake_name);
2e276125
JB
24202 }
24203}
24204
233d95b5
JK
24205/* Return the full name of file number I in *LH's file name table.
24206 Use COMP_DIR as the name of the current directory of the
24207 compilation. The result is allocated using xmalloc; the caller is
24208 responsible for freeing it. */
24209static char *
24210file_full_name (int file, struct line_header *lh, const char *comp_dir)
24211{
24212 /* Is the file number a valid index into the line header's file name
24213 table? Remember that file numbers start with one, not zero. */
fff8551c 24214 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
24215 {
24216 char *relative = file_file_name (file, lh);
24217
24218 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24219 return relative;
b36cec19
PA
24220 return reconcat (relative, comp_dir, SLASH_STRING,
24221 relative, (char *) NULL);
233d95b5
JK
24222 }
24223 else
24224 return file_file_name (file, lh);
24225}
24226
2e276125
JB
24227
24228static struct macro_source_file *
804d2729
TT
24229macro_start_file (struct dwarf2_cu *cu,
24230 int file, int line,
2e276125 24231 struct macro_source_file *current_file,
43f3e411 24232 struct line_header *lh)
2e276125 24233{
233d95b5
JK
24234 /* File name relative to the compilation directory of this source file. */
24235 char *file_name = file_file_name (file, lh);
2e276125 24236
2e276125 24237 if (! current_file)
abc9d0dc 24238 {
fc474241
DE
24239 /* Note: We don't create a macro table for this compilation unit
24240 at all until we actually get a filename. */
c24bdb02 24241 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24242
abc9d0dc
TT
24243 /* If we have no current file, then this must be the start_file
24244 directive for the compilation unit's main source file. */
fc474241
DE
24245 current_file = macro_set_main (macro_table, file_name);
24246 macro_define_special (macro_table);
abc9d0dc 24247 }
2e276125 24248 else
233d95b5 24249 current_file = macro_include (current_file, line, file_name);
2e276125 24250
233d95b5 24251 xfree (file_name);
6e70227d 24252
2e276125
JB
24253 return current_file;
24254}
24255
2e276125
JB
24256static const char *
24257consume_improper_spaces (const char *p, const char *body)
24258{
24259 if (*p == ' ')
24260 {
b98664d3 24261 complaint (_("macro definition contains spaces "
3e43a32a 24262 "in formal argument list:\n`%s'"),
4d3c2250 24263 body);
2e276125
JB
24264
24265 while (*p == ' ')
24266 p++;
24267 }
24268
24269 return p;
24270}
24271
24272
24273static void
24274parse_macro_definition (struct macro_source_file *file, int line,
24275 const char *body)
24276{
24277 const char *p;
24278
24279 /* The body string takes one of two forms. For object-like macro
24280 definitions, it should be:
24281
24282 <macro name> " " <definition>
24283
24284 For function-like macro definitions, it should be:
24285
24286 <macro name> "() " <definition>
24287 or
24288 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24289
24290 Spaces may appear only where explicitly indicated, and in the
24291 <definition>.
24292
24293 The Dwarf 2 spec says that an object-like macro's name is always
24294 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24295 the space when the macro's definition is the empty string.
2e276125
JB
24296
24297 The Dwarf 2 spec says that there should be no spaces between the
24298 formal arguments in a function-like macro's formal argument list,
24299 but versions of GCC around March 2002 include spaces after the
24300 commas. */
24301
24302
24303 /* Find the extent of the macro name. The macro name is terminated
24304 by either a space or null character (for an object-like macro) or
24305 an opening paren (for a function-like macro). */
24306 for (p = body; *p; p++)
24307 if (*p == ' ' || *p == '(')
24308 break;
24309
24310 if (*p == ' ' || *p == '\0')
24311 {
24312 /* It's an object-like macro. */
24313 int name_len = p - body;
3f8a7804 24314 char *name = savestring (body, name_len);
2e276125
JB
24315 const char *replacement;
24316
24317 if (*p == ' ')
24318 replacement = body + name_len + 1;
24319 else
24320 {
4d3c2250 24321 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24322 replacement = body + name_len;
24323 }
6e70227d 24324
2e276125
JB
24325 macro_define_object (file, line, name, replacement);
24326
24327 xfree (name);
24328 }
24329 else if (*p == '(')
24330 {
24331 /* It's a function-like macro. */
3f8a7804 24332 char *name = savestring (body, p - body);
2e276125
JB
24333 int argc = 0;
24334 int argv_size = 1;
8d749320 24335 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24336
24337 p++;
24338
24339 p = consume_improper_spaces (p, body);
24340
24341 /* Parse the formal argument list. */
24342 while (*p && *p != ')')
24343 {
24344 /* Find the extent of the current argument name. */
24345 const char *arg_start = p;
24346
24347 while (*p && *p != ',' && *p != ')' && *p != ' ')
24348 p++;
24349
24350 if (! *p || p == arg_start)
4d3c2250 24351 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24352 else
24353 {
24354 /* Make sure argv has room for the new argument. */
24355 if (argc >= argv_size)
24356 {
24357 argv_size *= 2;
224c3ddb 24358 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24359 }
24360
3f8a7804 24361 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24362 }
24363
24364 p = consume_improper_spaces (p, body);
24365
24366 /* Consume the comma, if present. */
24367 if (*p == ',')
24368 {
24369 p++;
24370
24371 p = consume_improper_spaces (p, body);
24372 }
24373 }
24374
24375 if (*p == ')')
24376 {
24377 p++;
24378
24379 if (*p == ' ')
24380 /* Perfectly formed definition, no complaints. */
24381 macro_define_function (file, line, name,
6e70227d 24382 argc, (const char **) argv,
2e276125
JB
24383 p + 1);
24384 else if (*p == '\0')
24385 {
24386 /* Complain, but do define it. */
4d3c2250 24387 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24388 macro_define_function (file, line, name,
6e70227d 24389 argc, (const char **) argv,
2e276125
JB
24390 p);
24391 }
24392 else
24393 /* Just complain. */
4d3c2250 24394 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24395 }
24396 else
24397 /* Just complain. */
4d3c2250 24398 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24399
24400 xfree (name);
24401 {
24402 int i;
24403
24404 for (i = 0; i < argc; i++)
24405 xfree (argv[i]);
24406 }
24407 xfree (argv);
24408 }
24409 else
4d3c2250 24410 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24411}
24412
cf2c3c16
TT
24413/* Skip some bytes from BYTES according to the form given in FORM.
24414 Returns the new pointer. */
2e276125 24415
d521ce57
TT
24416static const gdb_byte *
24417skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24418 enum dwarf_form form,
24419 unsigned int offset_size,
24420 struct dwarf2_section_info *section)
2e276125 24421{
cf2c3c16 24422 unsigned int bytes_read;
2e276125 24423
cf2c3c16 24424 switch (form)
2e276125 24425 {
cf2c3c16
TT
24426 case DW_FORM_data1:
24427 case DW_FORM_flag:
24428 ++bytes;
24429 break;
24430
24431 case DW_FORM_data2:
24432 bytes += 2;
24433 break;
24434
24435 case DW_FORM_data4:
24436 bytes += 4;
24437 break;
24438
24439 case DW_FORM_data8:
24440 bytes += 8;
24441 break;
24442
0224619f
JK
24443 case DW_FORM_data16:
24444 bytes += 16;
24445 break;
24446
cf2c3c16
TT
24447 case DW_FORM_string:
24448 read_direct_string (abfd, bytes, &bytes_read);
24449 bytes += bytes_read;
24450 break;
24451
24452 case DW_FORM_sec_offset:
24453 case DW_FORM_strp:
36586728 24454 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24455 bytes += offset_size;
24456 break;
24457
24458 case DW_FORM_block:
24459 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24460 bytes += bytes_read;
24461 break;
24462
24463 case DW_FORM_block1:
24464 bytes += 1 + read_1_byte (abfd, bytes);
24465 break;
24466 case DW_FORM_block2:
24467 bytes += 2 + read_2_bytes (abfd, bytes);
24468 break;
24469 case DW_FORM_block4:
24470 bytes += 4 + read_4_bytes (abfd, bytes);
24471 break;
24472
336d760d 24473 case DW_FORM_addrx:
cf2c3c16 24474 case DW_FORM_sdata:
cf532bd1 24475 case DW_FORM_strx:
cf2c3c16 24476 case DW_FORM_udata:
3019eac3
DE
24477 case DW_FORM_GNU_addr_index:
24478 case DW_FORM_GNU_str_index:
d521ce57 24479 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24480 if (bytes == NULL)
24481 {
24482 dwarf2_section_buffer_overflow_complaint (section);
24483 return NULL;
24484 }
cf2c3c16
TT
24485 break;
24486
663c44ac
JK
24487 case DW_FORM_implicit_const:
24488 break;
24489
cf2c3c16
TT
24490 default:
24491 {
b98664d3 24492 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24493 form, get_section_name (section));
cf2c3c16
TT
24494 return NULL;
24495 }
2e276125
JB
24496 }
24497
cf2c3c16
TT
24498 return bytes;
24499}
757a13d0 24500
cf2c3c16
TT
24501/* A helper for dwarf_decode_macros that handles skipping an unknown
24502 opcode. Returns an updated pointer to the macro data buffer; or,
24503 on error, issues a complaint and returns NULL. */
757a13d0 24504
d521ce57 24505static const gdb_byte *
cf2c3c16 24506skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24507 const gdb_byte **opcode_definitions,
24508 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24509 bfd *abfd,
24510 unsigned int offset_size,
24511 struct dwarf2_section_info *section)
24512{
24513 unsigned int bytes_read, i;
24514 unsigned long arg;
d521ce57 24515 const gdb_byte *defn;
2e276125 24516
cf2c3c16 24517 if (opcode_definitions[opcode] == NULL)
2e276125 24518 {
b98664d3 24519 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24520 opcode);
24521 return NULL;
24522 }
2e276125 24523
cf2c3c16
TT
24524 defn = opcode_definitions[opcode];
24525 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24526 defn += bytes_read;
2e276125 24527
cf2c3c16
TT
24528 for (i = 0; i < arg; ++i)
24529 {
aead7601
SM
24530 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24531 (enum dwarf_form) defn[i], offset_size,
f664829e 24532 section);
cf2c3c16
TT
24533 if (mac_ptr == NULL)
24534 {
24535 /* skip_form_bytes already issued the complaint. */
24536 return NULL;
24537 }
24538 }
757a13d0 24539
cf2c3c16
TT
24540 return mac_ptr;
24541}
757a13d0 24542
cf2c3c16
TT
24543/* A helper function which parses the header of a macro section.
24544 If the macro section is the extended (for now called "GNU") type,
24545 then this updates *OFFSET_SIZE. Returns a pointer to just after
24546 the header, or issues a complaint and returns NULL on error. */
757a13d0 24547
d521ce57
TT
24548static const gdb_byte *
24549dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24550 bfd *abfd,
d521ce57 24551 const gdb_byte *mac_ptr,
cf2c3c16
TT
24552 unsigned int *offset_size,
24553 int section_is_gnu)
24554{
24555 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24556
cf2c3c16
TT
24557 if (section_is_gnu)
24558 {
24559 unsigned int version, flags;
757a13d0 24560
cf2c3c16 24561 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24562 if (version != 4 && version != 5)
cf2c3c16 24563 {
b98664d3 24564 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24565 version);
24566 return NULL;
24567 }
24568 mac_ptr += 2;
757a13d0 24569
cf2c3c16
TT
24570 flags = read_1_byte (abfd, mac_ptr);
24571 ++mac_ptr;
24572 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24573
cf2c3c16
TT
24574 if ((flags & 2) != 0)
24575 /* We don't need the line table offset. */
24576 mac_ptr += *offset_size;
757a13d0 24577
cf2c3c16
TT
24578 /* Vendor opcode descriptions. */
24579 if ((flags & 4) != 0)
24580 {
24581 unsigned int i, count;
757a13d0 24582
cf2c3c16
TT
24583 count = read_1_byte (abfd, mac_ptr);
24584 ++mac_ptr;
24585 for (i = 0; i < count; ++i)
24586 {
24587 unsigned int opcode, bytes_read;
24588 unsigned long arg;
24589
24590 opcode = read_1_byte (abfd, mac_ptr);
24591 ++mac_ptr;
24592 opcode_definitions[opcode] = mac_ptr;
24593 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24594 mac_ptr += bytes_read;
24595 mac_ptr += arg;
24596 }
757a13d0 24597 }
cf2c3c16 24598 }
757a13d0 24599
cf2c3c16
TT
24600 return mac_ptr;
24601}
757a13d0 24602
cf2c3c16 24603/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24604 including DW_MACRO_import. */
cf2c3c16
TT
24605
24606static void
804d2729 24607dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24608 bfd *abfd,
d521ce57 24609 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24610 struct macro_source_file *current_file,
43f3e411 24611 struct line_header *lh,
cf2c3c16 24612 struct dwarf2_section_info *section,
36586728 24613 int section_is_gnu, int section_is_dwz,
cf2c3c16 24614 unsigned int offset_size,
8fc3fc34 24615 htab_t include_hash)
cf2c3c16 24616{
804d2729
TT
24617 struct dwarf2_per_objfile *dwarf2_per_objfile
24618 = cu->per_cu->dwarf2_per_objfile;
4d663531 24619 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24620 enum dwarf_macro_record_type macinfo_type;
24621 int at_commandline;
d521ce57 24622 const gdb_byte *opcode_definitions[256];
757a13d0 24623
cf2c3c16
TT
24624 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24625 &offset_size, section_is_gnu);
24626 if (mac_ptr == NULL)
24627 {
24628 /* We already issued a complaint. */
24629 return;
24630 }
757a13d0
JK
24631
24632 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24633 GDB is still reading the definitions from command line. First
24634 DW_MACINFO_start_file will need to be ignored as it was already executed
24635 to create CURRENT_FILE for the main source holding also the command line
24636 definitions. On first met DW_MACINFO_start_file this flag is reset to
24637 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24638
24639 at_commandline = 1;
24640
24641 do
24642 {
24643 /* Do we at least have room for a macinfo type byte? */
24644 if (mac_ptr >= mac_end)
24645 {
f664829e 24646 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24647 break;
24648 }
24649
aead7601 24650 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24651 mac_ptr++;
24652
cf2c3c16
TT
24653 /* Note that we rely on the fact that the corresponding GNU and
24654 DWARF constants are the same. */
132448f8
SM
24655 DIAGNOSTIC_PUSH
24656 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24657 switch (macinfo_type)
24658 {
24659 /* A zero macinfo type indicates the end of the macro
24660 information. */
24661 case 0:
24662 break;
2e276125 24663
0af92d60
JK
24664 case DW_MACRO_define:
24665 case DW_MACRO_undef:
24666 case DW_MACRO_define_strp:
24667 case DW_MACRO_undef_strp:
24668 case DW_MACRO_define_sup:
24669 case DW_MACRO_undef_sup:
2e276125 24670 {
891d2f0b 24671 unsigned int bytes_read;
2e276125 24672 int line;
d521ce57 24673 const char *body;
cf2c3c16 24674 int is_define;
2e276125 24675
cf2c3c16
TT
24676 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24677 mac_ptr += bytes_read;
24678
0af92d60
JK
24679 if (macinfo_type == DW_MACRO_define
24680 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24681 {
24682 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24683 mac_ptr += bytes_read;
24684 }
24685 else
24686 {
24687 LONGEST str_offset;
24688
24689 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24690 mac_ptr += offset_size;
2e276125 24691
0af92d60
JK
24692 if (macinfo_type == DW_MACRO_define_sup
24693 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24694 || section_is_dwz)
36586728 24695 {
ed2dc618
SM
24696 struct dwz_file *dwz
24697 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24698
ed2dc618
SM
24699 body = read_indirect_string_from_dwz (objfile,
24700 dwz, str_offset);
36586728
TT
24701 }
24702 else
ed2dc618
SM
24703 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24704 abfd, str_offset);
cf2c3c16
TT
24705 }
24706
0af92d60
JK
24707 is_define = (macinfo_type == DW_MACRO_define
24708 || macinfo_type == DW_MACRO_define_strp
24709 || macinfo_type == DW_MACRO_define_sup);
2e276125 24710 if (! current_file)
757a13d0
JK
24711 {
24712 /* DWARF violation as no main source is present. */
b98664d3 24713 complaint (_("debug info with no main source gives macro %s "
757a13d0 24714 "on line %d: %s"),
cf2c3c16
TT
24715 is_define ? _("definition") : _("undefinition"),
24716 line, body);
757a13d0
JK
24717 break;
24718 }
3e43a32a
MS
24719 if ((line == 0 && !at_commandline)
24720 || (line != 0 && at_commandline))
b98664d3 24721 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24722 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24723 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24724 line == 0 ? _("zero") : _("non-zero"), line, body);
24725
955b06fa 24726 if (body == NULL)
7bede828 24727 {
955b06fa
SDJ
24728 /* Fedora's rpm-build's "debugedit" binary
24729 corrupted .debug_macro sections.
24730
24731 For more info, see
24732 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24733 complaint (_("debug info gives %s invalid macro %s "
24734 "without body (corrupted?) at line %d "
24735 "on file %s"),
24736 at_commandline ? _("command-line") : _("in-file"),
24737 is_define ? _("definition") : _("undefinition"),
24738 line, current_file->filename);
7bede828 24739 }
955b06fa
SDJ
24740 else if (is_define)
24741 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24742 else
24743 {
0af92d60
JK
24744 gdb_assert (macinfo_type == DW_MACRO_undef
24745 || macinfo_type == DW_MACRO_undef_strp
24746 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24747 macro_undef (current_file, line, body);
24748 }
2e276125
JB
24749 }
24750 break;
24751
0af92d60 24752 case DW_MACRO_start_file:
2e276125 24753 {
891d2f0b 24754 unsigned int bytes_read;
2e276125
JB
24755 int line, file;
24756
24757 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24758 mac_ptr += bytes_read;
24759 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24760 mac_ptr += bytes_read;
24761
3e43a32a
MS
24762 if ((line == 0 && !at_commandline)
24763 || (line != 0 && at_commandline))
b98664d3 24764 complaint (_("debug info gives source %d included "
757a13d0
JK
24765 "from %s at %s line %d"),
24766 file, at_commandline ? _("command-line") : _("file"),
24767 line == 0 ? _("zero") : _("non-zero"), line);
24768
24769 if (at_commandline)
24770 {
0af92d60 24771 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24772 pass one. */
757a13d0
JK
24773 at_commandline = 0;
24774 }
24775 else
804d2729
TT
24776 current_file = macro_start_file (cu, file, line, current_file,
24777 lh);
2e276125
JB
24778 }
24779 break;
24780
0af92d60 24781 case DW_MACRO_end_file:
2e276125 24782 if (! current_file)
b98664d3 24783 complaint (_("macro debug info has an unmatched "
3e43a32a 24784 "`close_file' directive"));
2e276125
JB
24785 else
24786 {
24787 current_file = current_file->included_by;
24788 if (! current_file)
24789 {
cf2c3c16 24790 enum dwarf_macro_record_type next_type;
2e276125
JB
24791
24792 /* GCC circa March 2002 doesn't produce the zero
24793 type byte marking the end of the compilation
24794 unit. Complain if it's not there, but exit no
24795 matter what. */
24796
24797 /* Do we at least have room for a macinfo type byte? */
24798 if (mac_ptr >= mac_end)
24799 {
f664829e 24800 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24801 return;
24802 }
24803
24804 /* We don't increment mac_ptr here, so this is just
24805 a look-ahead. */
aead7601
SM
24806 next_type
24807 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24808 mac_ptr);
2e276125 24809 if (next_type != 0)
b98664d3 24810 complaint (_("no terminating 0-type entry for "
3e43a32a 24811 "macros in `.debug_macinfo' section"));
2e276125
JB
24812
24813 return;
24814 }
24815 }
24816 break;
24817
0af92d60
JK
24818 case DW_MACRO_import:
24819 case DW_MACRO_import_sup:
cf2c3c16
TT
24820 {
24821 LONGEST offset;
8fc3fc34 24822 void **slot;
a036ba48
TT
24823 bfd *include_bfd = abfd;
24824 struct dwarf2_section_info *include_section = section;
d521ce57 24825 const gdb_byte *include_mac_end = mac_end;
a036ba48 24826 int is_dwz = section_is_dwz;
d521ce57 24827 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24828
24829 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24830 mac_ptr += offset_size;
24831
0af92d60 24832 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24833 {
ed2dc618 24834 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24835
4d663531 24836 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24837
a036ba48 24838 include_section = &dwz->macro;
a32a8923 24839 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24840 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24841 is_dwz = 1;
24842 }
24843
24844 new_mac_ptr = include_section->buffer + offset;
24845 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24846
8fc3fc34
TT
24847 if (*slot != NULL)
24848 {
24849 /* This has actually happened; see
24850 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24851 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24852 ".debug_macro section"));
24853 }
24854 else
24855 {
d521ce57 24856 *slot = (void *) new_mac_ptr;
36586728 24857
804d2729 24858 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24859 include_mac_end, current_file, lh,
36586728 24860 section, section_is_gnu, is_dwz,
4d663531 24861 offset_size, include_hash);
8fc3fc34 24862
d521ce57 24863 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24864 }
cf2c3c16
TT
24865 }
24866 break;
24867
2e276125 24868 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24869 if (!section_is_gnu)
24870 {
24871 unsigned int bytes_read;
2e276125 24872
ac298888
TT
24873 /* This reads the constant, but since we don't recognize
24874 any vendor extensions, we ignore it. */
24875 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24876 mac_ptr += bytes_read;
24877 read_direct_string (abfd, mac_ptr, &bytes_read);
24878 mac_ptr += bytes_read;
2e276125 24879
cf2c3c16
TT
24880 /* We don't recognize any vendor extensions. */
24881 break;
24882 }
24883 /* FALLTHROUGH */
24884
24885 default:
24886 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24887 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24888 section);
24889 if (mac_ptr == NULL)
24890 return;
24891 break;
2e276125 24892 }
132448f8 24893 DIAGNOSTIC_POP
757a13d0 24894 } while (macinfo_type != 0);
2e276125 24895}
8e19ed76 24896
cf2c3c16 24897static void
09262596 24898dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24899 int section_is_gnu)
cf2c3c16 24900{
518817b3
SM
24901 struct dwarf2_per_objfile *dwarf2_per_objfile
24902 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24903 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24904 struct line_header *lh = cu->line_header;
24905 bfd *abfd;
d521ce57 24906 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24907 struct macro_source_file *current_file = 0;
24908 enum dwarf_macro_record_type macinfo_type;
24909 unsigned int offset_size = cu->header.offset_size;
d521ce57 24910 const gdb_byte *opcode_definitions[256];
8fc3fc34 24911 void **slot;
09262596
DE
24912 struct dwarf2_section_info *section;
24913 const char *section_name;
24914
24915 if (cu->dwo_unit != NULL)
24916 {
24917 if (section_is_gnu)
24918 {
24919 section = &cu->dwo_unit->dwo_file->sections.macro;
24920 section_name = ".debug_macro.dwo";
24921 }
24922 else
24923 {
24924 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24925 section_name = ".debug_macinfo.dwo";
24926 }
24927 }
24928 else
24929 {
24930 if (section_is_gnu)
24931 {
24932 section = &dwarf2_per_objfile->macro;
24933 section_name = ".debug_macro";
24934 }
24935 else
24936 {
24937 section = &dwarf2_per_objfile->macinfo;
24938 section_name = ".debug_macinfo";
24939 }
24940 }
cf2c3c16 24941
bb5ed363 24942 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24943 if (section->buffer == NULL)
24944 {
b98664d3 24945 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24946 return;
24947 }
a32a8923 24948 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24949
24950 /* First pass: Find the name of the base filename.
24951 This filename is needed in order to process all macros whose definition
24952 (or undefinition) comes from the command line. These macros are defined
24953 before the first DW_MACINFO_start_file entry, and yet still need to be
24954 associated to the base file.
24955
24956 To determine the base file name, we scan the macro definitions until we
24957 reach the first DW_MACINFO_start_file entry. We then initialize
24958 CURRENT_FILE accordingly so that any macro definition found before the
24959 first DW_MACINFO_start_file can still be associated to the base file. */
24960
24961 mac_ptr = section->buffer + offset;
24962 mac_end = section->buffer + section->size;
24963
24964 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24965 &offset_size, section_is_gnu);
24966 if (mac_ptr == NULL)
24967 {
24968 /* We already issued a complaint. */
24969 return;
24970 }
24971
24972 do
24973 {
24974 /* Do we at least have room for a macinfo type byte? */
24975 if (mac_ptr >= mac_end)
24976 {
24977 /* Complaint is printed during the second pass as GDB will probably
24978 stop the first pass earlier upon finding
24979 DW_MACINFO_start_file. */
24980 break;
24981 }
24982
aead7601 24983 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24984 mac_ptr++;
24985
24986 /* Note that we rely on the fact that the corresponding GNU and
24987 DWARF constants are the same. */
132448f8
SM
24988 DIAGNOSTIC_PUSH
24989 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24990 switch (macinfo_type)
24991 {
24992 /* A zero macinfo type indicates the end of the macro
24993 information. */
24994 case 0:
24995 break;
24996
0af92d60
JK
24997 case DW_MACRO_define:
24998 case DW_MACRO_undef:
cf2c3c16
TT
24999 /* Only skip the data by MAC_PTR. */
25000 {
25001 unsigned int bytes_read;
25002
25003 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25004 mac_ptr += bytes_read;
25005 read_direct_string (abfd, mac_ptr, &bytes_read);
25006 mac_ptr += bytes_read;
25007 }
25008 break;
25009
0af92d60 25010 case DW_MACRO_start_file:
cf2c3c16
TT
25011 {
25012 unsigned int bytes_read;
25013 int line, file;
25014
25015 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25016 mac_ptr += bytes_read;
25017 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25018 mac_ptr += bytes_read;
25019
804d2729 25020 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25021 }
25022 break;
25023
0af92d60 25024 case DW_MACRO_end_file:
cf2c3c16
TT
25025 /* No data to skip by MAC_PTR. */
25026 break;
25027
0af92d60
JK
25028 case DW_MACRO_define_strp:
25029 case DW_MACRO_undef_strp:
25030 case DW_MACRO_define_sup:
25031 case DW_MACRO_undef_sup:
cf2c3c16
TT
25032 {
25033 unsigned int bytes_read;
25034
25035 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25036 mac_ptr += bytes_read;
25037 mac_ptr += offset_size;
25038 }
25039 break;
25040
0af92d60
JK
25041 case DW_MACRO_import:
25042 case DW_MACRO_import_sup:
cf2c3c16 25043 /* Note that, according to the spec, a transparent include
0af92d60 25044 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25045 skip this opcode. */
25046 mac_ptr += offset_size;
25047 break;
25048
25049 case DW_MACINFO_vendor_ext:
25050 /* Only skip the data by MAC_PTR. */
25051 if (!section_is_gnu)
25052 {
25053 unsigned int bytes_read;
25054
25055 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25056 mac_ptr += bytes_read;
25057 read_direct_string (abfd, mac_ptr, &bytes_read);
25058 mac_ptr += bytes_read;
25059 }
25060 /* FALLTHROUGH */
25061
25062 default:
25063 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25064 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25065 section);
25066 if (mac_ptr == NULL)
25067 return;
25068 break;
25069 }
132448f8 25070 DIAGNOSTIC_POP
cf2c3c16
TT
25071 } while (macinfo_type != 0 && current_file == NULL);
25072
25073 /* Second pass: Process all entries.
25074
25075 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25076 command-line macro definitions/undefinitions. This flag is unset when we
25077 reach the first DW_MACINFO_start_file entry. */
25078
fc4007c9
TT
25079 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25080 htab_eq_pointer,
25081 NULL, xcalloc, xfree));
8fc3fc34 25082 mac_ptr = section->buffer + offset;
fc4007c9 25083 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25084 *slot = (void *) mac_ptr;
804d2729 25085 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25086 current_file, lh, section,
fc4007c9
TT
25087 section_is_gnu, 0, offset_size,
25088 include_hash.get ());
cf2c3c16
TT
25089}
25090
8e19ed76 25091/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25092 if so return true else false. */
380bca97 25093
8e19ed76 25094static int
6e5a29e1 25095attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25096{
25097 return (attr == NULL ? 0 :
25098 attr->form == DW_FORM_block1
25099 || attr->form == DW_FORM_block2
25100 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25101 || attr->form == DW_FORM_block
25102 || attr->form == DW_FORM_exprloc);
8e19ed76 25103}
4c2df51b 25104
c6a0999f
JB
25105/* Return non-zero if ATTR's value is a section offset --- classes
25106 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25107 You may use DW_UNSND (attr) to retrieve such offsets.
25108
25109 Section 7.5.4, "Attribute Encodings", explains that no attribute
25110 may have a value that belongs to more than one of these classes; it
25111 would be ambiguous if we did, because we use the same forms for all
25112 of them. */
380bca97 25113
3690dd37 25114static int
6e5a29e1 25115attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25116{
25117 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25118 || attr->form == DW_FORM_data8
25119 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25120}
25121
3690dd37
JB
25122/* Return non-zero if ATTR's value falls in the 'constant' class, or
25123 zero otherwise. When this function returns true, you can apply
25124 dwarf2_get_attr_constant_value to it.
25125
25126 However, note that for some attributes you must check
25127 attr_form_is_section_offset before using this test. DW_FORM_data4
25128 and DW_FORM_data8 are members of both the constant class, and of
25129 the classes that contain offsets into other debug sections
25130 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25131 that, if an attribute's can be either a constant or one of the
25132 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25133 taken as section offsets, not constants.
25134
25135 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25136 cannot handle that. */
380bca97 25137
3690dd37 25138static int
6e5a29e1 25139attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25140{
25141 switch (attr->form)
25142 {
25143 case DW_FORM_sdata:
25144 case DW_FORM_udata:
25145 case DW_FORM_data1:
25146 case DW_FORM_data2:
25147 case DW_FORM_data4:
25148 case DW_FORM_data8:
663c44ac 25149 case DW_FORM_implicit_const:
3690dd37
JB
25150 return 1;
25151 default:
25152 return 0;
25153 }
25154}
25155
7771576e
SA
25156
25157/* DW_ADDR is always stored already as sect_offset; despite for the forms
25158 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25159
25160static int
6e5a29e1 25161attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25162{
25163 switch (attr->form)
25164 {
25165 case DW_FORM_ref_addr:
25166 case DW_FORM_ref1:
25167 case DW_FORM_ref2:
25168 case DW_FORM_ref4:
25169 case DW_FORM_ref8:
25170 case DW_FORM_ref_udata:
25171 case DW_FORM_GNU_ref_alt:
25172 return 1;
25173 default:
25174 return 0;
25175 }
25176}
25177
3019eac3
DE
25178/* Return the .debug_loc section to use for CU.
25179 For DWO files use .debug_loc.dwo. */
25180
25181static struct dwarf2_section_info *
25182cu_debug_loc_section (struct dwarf2_cu *cu)
25183{
518817b3
SM
25184 struct dwarf2_per_objfile *dwarf2_per_objfile
25185 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25186
3019eac3 25187 if (cu->dwo_unit)
43988095
JK
25188 {
25189 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25190
43988095
JK
25191 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25192 }
25193 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25194 : &dwarf2_per_objfile->loc);
3019eac3
DE
25195}
25196
8cf6f0b1
TT
25197/* A helper function that fills in a dwarf2_loclist_baton. */
25198
25199static void
25200fill_in_loclist_baton (struct dwarf2_cu *cu,
25201 struct dwarf2_loclist_baton *baton,
ff39bb5e 25202 const struct attribute *attr)
8cf6f0b1 25203{
518817b3
SM
25204 struct dwarf2_per_objfile *dwarf2_per_objfile
25205 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25206 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25207
25208 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25209
25210 baton->per_cu = cu->per_cu;
25211 gdb_assert (baton->per_cu);
25212 /* We don't know how long the location list is, but make sure we
25213 don't run off the edge of the section. */
3019eac3
DE
25214 baton->size = section->size - DW_UNSND (attr);
25215 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25216 baton->base_address = cu->base_address;
f664829e 25217 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25218}
25219
4c2df51b 25220static void
ff39bb5e 25221dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25222 struct dwarf2_cu *cu, int is_block)
4c2df51b 25223{
518817b3
SM
25224 struct dwarf2_per_objfile *dwarf2_per_objfile
25225 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25226 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25227 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25228
3690dd37 25229 if (attr_form_is_section_offset (attr)
3019eac3 25230 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25231 the section. If so, fall through to the complaint in the
25232 other branch. */
3019eac3 25233 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25234 {
0d53c4c4 25235 struct dwarf2_loclist_baton *baton;
4c2df51b 25236
8d749320 25237 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25238
8cf6f0b1 25239 fill_in_loclist_baton (cu, baton, attr);
be391dca 25240
d00adf39 25241 if (cu->base_known == 0)
b98664d3 25242 complaint (_("Location list used without "
3e43a32a 25243 "specifying the CU base address."));
4c2df51b 25244
f1e6e072
TT
25245 SYMBOL_ACLASS_INDEX (sym) = (is_block
25246 ? dwarf2_loclist_block_index
25247 : dwarf2_loclist_index);
0d53c4c4
DJ
25248 SYMBOL_LOCATION_BATON (sym) = baton;
25249 }
25250 else
25251 {
25252 struct dwarf2_locexpr_baton *baton;
25253
8d749320 25254 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25255 baton->per_cu = cu->per_cu;
25256 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25257
25258 if (attr_form_is_block (attr))
25259 {
25260 /* Note that we're just copying the block's data pointer
25261 here, not the actual data. We're still pointing into the
6502dd73
DJ
25262 info_buffer for SYM's objfile; right now we never release
25263 that buffer, but when we do clean up properly this may
25264 need to change. */
0d53c4c4
DJ
25265 baton->size = DW_BLOCK (attr)->size;
25266 baton->data = DW_BLOCK (attr)->data;
25267 }
25268 else
25269 {
25270 dwarf2_invalid_attrib_class_complaint ("location description",
25271 SYMBOL_NATURAL_NAME (sym));
25272 baton->size = 0;
0d53c4c4 25273 }
6e70227d 25274
f1e6e072
TT
25275 SYMBOL_ACLASS_INDEX (sym) = (is_block
25276 ? dwarf2_locexpr_block_index
25277 : dwarf2_locexpr_index);
0d53c4c4
DJ
25278 SYMBOL_LOCATION_BATON (sym) = baton;
25279 }
4c2df51b 25280}
6502dd73 25281
9aa1f1e3
TT
25282/* Return the OBJFILE associated with the compilation unit CU. If CU
25283 came from a separate debuginfo file, then the master objfile is
25284 returned. */
ae0d2f24
UW
25285
25286struct objfile *
25287dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25288{
e3b94546 25289 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25290
25291 /* Return the master objfile, so that we can report and look up the
25292 correct file containing this variable. */
25293 if (objfile->separate_debug_objfile_backlink)
25294 objfile = objfile->separate_debug_objfile_backlink;
25295
25296 return objfile;
25297}
25298
96408a79
SA
25299/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25300 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25301 CU_HEADERP first. */
25302
25303static const struct comp_unit_head *
25304per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25305 struct dwarf2_per_cu_data *per_cu)
25306{
d521ce57 25307 const gdb_byte *info_ptr;
96408a79
SA
25308
25309 if (per_cu->cu)
25310 return &per_cu->cu->header;
25311
9c541725 25312 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25313
25314 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25315 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25316 rcuh_kind::COMPILE);
96408a79
SA
25317
25318 return cu_headerp;
25319}
25320
ae0d2f24
UW
25321/* Return the address size given in the compilation unit header for CU. */
25322
98714339 25323int
ae0d2f24
UW
25324dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25325{
96408a79
SA
25326 struct comp_unit_head cu_header_local;
25327 const struct comp_unit_head *cu_headerp;
c471e790 25328
96408a79
SA
25329 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25330
25331 return cu_headerp->addr_size;
ae0d2f24
UW
25332}
25333
9eae7c52
TT
25334/* Return the offset size given in the compilation unit header for CU. */
25335
25336int
25337dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25338{
96408a79
SA
25339 struct comp_unit_head cu_header_local;
25340 const struct comp_unit_head *cu_headerp;
9c6c53f7 25341
96408a79
SA
25342 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25343
25344 return cu_headerp->offset_size;
25345}
25346
25347/* See its dwarf2loc.h declaration. */
25348
25349int
25350dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25351{
25352 struct comp_unit_head cu_header_local;
25353 const struct comp_unit_head *cu_headerp;
25354
25355 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25356
25357 if (cu_headerp->version == 2)
25358 return cu_headerp->addr_size;
25359 else
25360 return cu_headerp->offset_size;
181cebd4
JK
25361}
25362
9aa1f1e3
TT
25363/* Return the text offset of the CU. The returned offset comes from
25364 this CU's objfile. If this objfile came from a separate debuginfo
25365 file, then the offset may be different from the corresponding
25366 offset in the parent objfile. */
25367
25368CORE_ADDR
25369dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25370{
e3b94546 25371 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25372
25373 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25374}
25375
9a49df9d
AB
25376/* Return a type that is a generic pointer type, the size of which matches
25377 the address size given in the compilation unit header for PER_CU. */
25378static struct type *
25379dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25380{
25381 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25382 struct type *void_type = objfile_type (objfile)->builtin_void;
25383 struct type *addr_type = lookup_pointer_type (void_type);
25384 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25385
25386 if (TYPE_LENGTH (addr_type) == addr_size)
25387 return addr_type;
25388
25389 addr_type
25390 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25391 return addr_type;
25392}
25393
43988095
JK
25394/* Return DWARF version number of PER_CU. */
25395
25396short
25397dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25398{
25399 return per_cu->dwarf_version;
25400}
25401
348e048f
DE
25402/* Locate the .debug_info compilation unit from CU's objfile which contains
25403 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25404
25405static struct dwarf2_per_cu_data *
9c541725 25406dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25407 unsigned int offset_in_dwz,
ed2dc618 25408 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25409{
25410 struct dwarf2_per_cu_data *this_cu;
25411 int low, high;
25412
ae038cb0 25413 low = 0;
b76e467d 25414 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25415 while (high > low)
25416 {
36586728 25417 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25418 int mid = low + (high - low) / 2;
9a619af0 25419
36586728 25420 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25421 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25422 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25423 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25424 high = mid;
25425 else
25426 low = mid + 1;
25427 }
25428 gdb_assert (low == high);
36586728 25429 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25430 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25431 {
36586728 25432 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25433 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25434 "offset %s [in module %s]"),
25435 sect_offset_str (sect_off),
ed2dc618 25436 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25437
9c541725
PA
25438 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25439 <= sect_off);
ae038cb0
DJ
25440 return dwarf2_per_objfile->all_comp_units[low-1];
25441 }
25442 else
25443 {
b76e467d 25444 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25445 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25446 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25447 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25448 return this_cu;
25449 }
25450}
25451
23745b47 25452/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25453
fcd3b13d
SM
25454dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25455 : per_cu (per_cu_),
9068261f
AB
25456 mark (false),
25457 has_loclist (false),
25458 checked_producer (false),
25459 producer_is_gxx_lt_4_6 (false),
25460 producer_is_gcc_lt_4_3 (false),
eb77c9df 25461 producer_is_icc (false),
9068261f 25462 producer_is_icc_lt_14 (false),
c258c396 25463 producer_is_codewarrior (false),
9068261f 25464 processing_has_namespace_info (false)
93311388 25465{
fcd3b13d
SM
25466 per_cu->cu = this;
25467}
25468
25469/* Destroy a dwarf2_cu. */
25470
25471dwarf2_cu::~dwarf2_cu ()
25472{
25473 per_cu->cu = NULL;
9816fde3
JK
25474}
25475
25476/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25477
25478static void
95554aad
TT
25479prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25480 enum language pretend_language)
9816fde3
JK
25481{
25482 struct attribute *attr;
25483
25484 /* Set the language we're debugging. */
25485 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25486 if (attr)
25487 set_cu_language (DW_UNSND (attr), cu);
25488 else
9cded63f 25489 {
95554aad 25490 cu->language = pretend_language;
9cded63f
TT
25491 cu->language_defn = language_def (cu->language);
25492 }
dee91e82 25493
7d45c7c3 25494 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25495}
25496
ae038cb0
DJ
25497/* Increase the age counter on each cached compilation unit, and free
25498 any that are too old. */
25499
25500static void
ed2dc618 25501age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25502{
25503 struct dwarf2_per_cu_data *per_cu, **last_chain;
25504
25505 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25506 per_cu = dwarf2_per_objfile->read_in_chain;
25507 while (per_cu != NULL)
25508 {
25509 per_cu->cu->last_used ++;
b4f54984 25510 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25511 dwarf2_mark (per_cu->cu);
25512 per_cu = per_cu->cu->read_in_chain;
25513 }
25514
25515 per_cu = dwarf2_per_objfile->read_in_chain;
25516 last_chain = &dwarf2_per_objfile->read_in_chain;
25517 while (per_cu != NULL)
25518 {
25519 struct dwarf2_per_cu_data *next_cu;
25520
25521 next_cu = per_cu->cu->read_in_chain;
25522
25523 if (!per_cu->cu->mark)
25524 {
fcd3b13d 25525 delete per_cu->cu;
ae038cb0
DJ
25526 *last_chain = next_cu;
25527 }
25528 else
25529 last_chain = &per_cu->cu->read_in_chain;
25530
25531 per_cu = next_cu;
25532 }
25533}
25534
25535/* Remove a single compilation unit from the cache. */
25536
25537static void
dee91e82 25538free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25539{
25540 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25541 struct dwarf2_per_objfile *dwarf2_per_objfile
25542 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25543
25544 per_cu = dwarf2_per_objfile->read_in_chain;
25545 last_chain = &dwarf2_per_objfile->read_in_chain;
25546 while (per_cu != NULL)
25547 {
25548 struct dwarf2_per_cu_data *next_cu;
25549
25550 next_cu = per_cu->cu->read_in_chain;
25551
dee91e82 25552 if (per_cu == target_per_cu)
ae038cb0 25553 {
fcd3b13d 25554 delete per_cu->cu;
dee91e82 25555 per_cu->cu = NULL;
ae038cb0
DJ
25556 *last_chain = next_cu;
25557 break;
25558 }
25559 else
25560 last_chain = &per_cu->cu->read_in_chain;
25561
25562 per_cu = next_cu;
25563 }
25564}
25565
dee91e82
DE
25566/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25567 We store these in a hash table separate from the DIEs, and preserve them
25568 when the DIEs are flushed out of cache.
25569
25570 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25571 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25572 or the type may come from a DWO file. Furthermore, while it's more logical
25573 to use per_cu->section+offset, with Fission the section with the data is in
25574 the DWO file but we don't know that section at the point we need it.
25575 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25576 because we can enter the lookup routine, get_die_type_at_offset, from
25577 outside this file, and thus won't necessarily have PER_CU->cu.
25578 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25579
dee91e82 25580struct dwarf2_per_cu_offset_and_type
1c379e20 25581{
dee91e82 25582 const struct dwarf2_per_cu_data *per_cu;
9c541725 25583 sect_offset sect_off;
1c379e20
DJ
25584 struct type *type;
25585};
25586
dee91e82 25587/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25588
25589static hashval_t
dee91e82 25590per_cu_offset_and_type_hash (const void *item)
1c379e20 25591{
9a3c8263
SM
25592 const struct dwarf2_per_cu_offset_and_type *ofs
25593 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25594
9c541725 25595 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25596}
25597
dee91e82 25598/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25599
25600static int
dee91e82 25601per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25602{
9a3c8263
SM
25603 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25604 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25605 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25606 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25607
dee91e82 25608 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25609 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25610}
25611
25612/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25613 table if necessary. For convenience, return TYPE.
25614
25615 The DIEs reading must have careful ordering to:
25616 * Not cause infite loops trying to read in DIEs as a prerequisite for
25617 reading current DIE.
25618 * Not trying to dereference contents of still incompletely read in types
25619 while reading in other DIEs.
25620 * Enable referencing still incompletely read in types just by a pointer to
25621 the type without accessing its fields.
25622
25623 Therefore caller should follow these rules:
25624 * Try to fetch any prerequisite types we may need to build this DIE type
25625 before building the type and calling set_die_type.
e71ec853 25626 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25627 possible before fetching more types to complete the current type.
25628 * Make the type as complete as possible before fetching more types. */
1c379e20 25629
f792889a 25630static struct type *
1c379e20
DJ
25631set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25632{
518817b3
SM
25633 struct dwarf2_per_objfile *dwarf2_per_objfile
25634 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25635 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25636 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25637 struct attribute *attr;
25638 struct dynamic_prop prop;
1c379e20 25639
b4ba55a1
JB
25640 /* For Ada types, make sure that the gnat-specific data is always
25641 initialized (if not already set). There are a few types where
25642 we should not be doing so, because the type-specific area is
25643 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25644 where the type-specific area is used to store the floatformat).
25645 But this is not a problem, because the gnat-specific information
25646 is actually not needed for these types. */
25647 if (need_gnat_info (cu)
25648 && TYPE_CODE (type) != TYPE_CODE_FUNC
25649 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25650 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25651 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25652 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25653 && !HAVE_GNAT_AUX_INFO (type))
25654 INIT_GNAT_SPECIFIC (type);
25655
3f2f83dd
KB
25656 /* Read DW_AT_allocated and set in type. */
25657 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25658 if (attr_form_is_block (attr))
25659 {
9a49df9d
AB
25660 struct type *prop_type
25661 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25662 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25663 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25664 }
25665 else if (attr != NULL)
25666 {
b98664d3 25667 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25668 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25669 sect_offset_str (die->sect_off));
3f2f83dd
KB
25670 }
25671
25672 /* Read DW_AT_associated and set in type. */
25673 attr = dwarf2_attr (die, DW_AT_associated, cu);
25674 if (attr_form_is_block (attr))
25675 {
9a49df9d
AB
25676 struct type *prop_type
25677 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25678 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25679 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25680 }
25681 else if (attr != NULL)
25682 {
b98664d3 25683 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25684 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25685 sect_offset_str (die->sect_off));
3f2f83dd
KB
25686 }
25687
3cdcd0ce
JB
25688 /* Read DW_AT_data_location and set in type. */
25689 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25690 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25691 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25692 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25693
dee91e82 25694 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25695 {
dee91e82
DE
25696 dwarf2_per_objfile->die_type_hash =
25697 htab_create_alloc_ex (127,
25698 per_cu_offset_and_type_hash,
25699 per_cu_offset_and_type_eq,
25700 NULL,
25701 &objfile->objfile_obstack,
25702 hashtab_obstack_allocate,
25703 dummy_obstack_deallocate);
f792889a 25704 }
1c379e20 25705
dee91e82 25706 ofs.per_cu = cu->per_cu;
9c541725 25707 ofs.sect_off = die->sect_off;
1c379e20 25708 ofs.type = type;
dee91e82
DE
25709 slot = (struct dwarf2_per_cu_offset_and_type **)
25710 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25711 if (*slot)
b98664d3 25712 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25713 sect_offset_str (die->sect_off));
8d749320
SM
25714 *slot = XOBNEW (&objfile->objfile_obstack,
25715 struct dwarf2_per_cu_offset_and_type);
1c379e20 25716 **slot = ofs;
f792889a 25717 return type;
1c379e20
DJ
25718}
25719
9c541725 25720/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25721 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25722
25723static struct type *
9c541725 25724get_die_type_at_offset (sect_offset sect_off,
673bfd45 25725 struct dwarf2_per_cu_data *per_cu)
1c379e20 25726{
dee91e82 25727 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25728 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25729
dee91e82 25730 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25731 return NULL;
1c379e20 25732
dee91e82 25733 ofs.per_cu = per_cu;
9c541725 25734 ofs.sect_off = sect_off;
9a3c8263
SM
25735 slot = ((struct dwarf2_per_cu_offset_and_type *)
25736 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25737 if (slot)
25738 return slot->type;
25739 else
25740 return NULL;
25741}
25742
02142a6c 25743/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25744 or return NULL if DIE does not have a saved type. */
25745
25746static struct type *
25747get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25748{
9c541725 25749 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25750}
25751
10b3939b
DJ
25752/* Add a dependence relationship from CU to REF_PER_CU. */
25753
25754static void
25755dwarf2_add_dependence (struct dwarf2_cu *cu,
25756 struct dwarf2_per_cu_data *ref_per_cu)
25757{
25758 void **slot;
25759
25760 if (cu->dependencies == NULL)
25761 cu->dependencies
25762 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25763 NULL, &cu->comp_unit_obstack,
25764 hashtab_obstack_allocate,
25765 dummy_obstack_deallocate);
25766
25767 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25768 if (*slot == NULL)
25769 *slot = ref_per_cu;
25770}
1c379e20 25771
f504f079
DE
25772/* Subroutine of dwarf2_mark to pass to htab_traverse.
25773 Set the mark field in every compilation unit in the
ae038cb0
DJ
25774 cache that we must keep because we are keeping CU. */
25775
10b3939b
DJ
25776static int
25777dwarf2_mark_helper (void **slot, void *data)
25778{
25779 struct dwarf2_per_cu_data *per_cu;
25780
25781 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25782
25783 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25784 reading of the chain. As such dependencies remain valid it is not much
25785 useful to track and undo them during QUIT cleanups. */
25786 if (per_cu->cu == NULL)
25787 return 1;
25788
10b3939b
DJ
25789 if (per_cu->cu->mark)
25790 return 1;
9068261f 25791 per_cu->cu->mark = true;
10b3939b
DJ
25792
25793 if (per_cu->cu->dependencies != NULL)
25794 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25795
25796 return 1;
25797}
25798
f504f079
DE
25799/* Set the mark field in CU and in every other compilation unit in the
25800 cache that we must keep because we are keeping CU. */
25801
ae038cb0
DJ
25802static void
25803dwarf2_mark (struct dwarf2_cu *cu)
25804{
25805 if (cu->mark)
25806 return;
9068261f 25807 cu->mark = true;
10b3939b
DJ
25808 if (cu->dependencies != NULL)
25809 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25810}
25811
25812static void
25813dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25814{
25815 while (per_cu)
25816 {
9068261f 25817 per_cu->cu->mark = false;
ae038cb0
DJ
25818 per_cu = per_cu->cu->read_in_chain;
25819 }
72bf9492
DJ
25820}
25821
72bf9492
DJ
25822/* Trivial hash function for partial_die_info: the hash value of a DIE
25823 is its offset in .debug_info for this objfile. */
25824
25825static hashval_t
25826partial_die_hash (const void *item)
25827{
9a3c8263
SM
25828 const struct partial_die_info *part_die
25829 = (const struct partial_die_info *) item;
9a619af0 25830
9c541725 25831 return to_underlying (part_die->sect_off);
72bf9492
DJ
25832}
25833
25834/* Trivial comparison function for partial_die_info structures: two DIEs
25835 are equal if they have the same offset. */
25836
25837static int
25838partial_die_eq (const void *item_lhs, const void *item_rhs)
25839{
9a3c8263
SM
25840 const struct partial_die_info *part_die_lhs
25841 = (const struct partial_die_info *) item_lhs;
25842 const struct partial_die_info *part_die_rhs
25843 = (const struct partial_die_info *) item_rhs;
9a619af0 25844
9c541725 25845 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25846}
25847
3c3bb058
AB
25848struct cmd_list_element *set_dwarf_cmdlist;
25849struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25850
25851static void
981a3fb3 25852set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25853{
b4f54984 25854 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25855 gdb_stdout);
ae038cb0
DJ
25856}
25857
25858static void
981a3fb3 25859show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25860{
b4f54984 25861 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25862}
25863
491144b5 25864bool dwarf_always_disassemble;
437afbb8 25865
437afbb8 25866static void
cd4fb1b2
SM
25867show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25868 struct cmd_list_element *c, const char *value)
9291a0cd 25869{
cd4fb1b2
SM
25870 fprintf_filtered (file,
25871 _("Whether to always disassemble "
25872 "DWARF expressions is %s.\n"),
25873 value);
9291a0cd
TT
25874}
25875
9291a0cd 25876static void
cd4fb1b2
SM
25877show_check_physname (struct ui_file *file, int from_tty,
25878 struct cmd_list_element *c, const char *value)
9291a0cd 25879{
cd4fb1b2
SM
25880 fprintf_filtered (file,
25881 _("Whether to check \"physname\" is %s.\n"),
25882 value);
9291a0cd
TT
25883}
25884
cd4fb1b2
SM
25885void
25886_initialize_dwarf2_read (void)
9291a0cd 25887{
cd4fb1b2
SM
25888 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25889Set DWARF specific variables.\n\
590042fc 25890Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
25891 &set_dwarf_cmdlist, "maintenance set dwarf ",
25892 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25893
cd4fb1b2 25894 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
25895Show DWARF specific variables.\n\
25896Show DWARF variables such as the cache size."),
cd4fb1b2
SM
25897 &show_dwarf_cmdlist, "maintenance show dwarf ",
25898 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25899
cd4fb1b2
SM
25900 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25901 &dwarf_max_cache_age, _("\
25902Set the upper bound on the age of cached DWARF compilation units."), _("\
25903Show the upper bound on the age of cached DWARF compilation units."), _("\
25904A higher limit means that cached compilation units will be stored\n\
25905in memory longer, and more total memory will be used. Zero disables\n\
25906caching, which can slow down startup."),
25907 NULL,
25908 show_dwarf_max_cache_age,
25909 &set_dwarf_cmdlist,
25910 &show_dwarf_cmdlist);
156942c7 25911
cd4fb1b2
SM
25912 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25913 &dwarf_always_disassemble, _("\
25914Set whether `info address' always disassembles DWARF expressions."), _("\
25915Show whether `info address' always disassembles DWARF expressions."), _("\
25916When enabled, DWARF expressions are always printed in an assembly-like\n\
25917syntax. When disabled, expressions will be printed in a more\n\
25918conversational style, when possible."),
25919 NULL,
25920 show_dwarf_always_disassemble,
25921 &set_dwarf_cmdlist,
25922 &show_dwarf_cmdlist);
9291a0cd 25923
cd4fb1b2
SM
25924 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25925Set debugging of the DWARF reader."), _("\
25926Show debugging of the DWARF reader."), _("\
25927When enabled (non-zero), debugging messages are printed during DWARF\n\
25928reading and symtab expansion. A value of 1 (one) provides basic\n\
25929information. A value greater than 1 provides more verbose information."),
25930 NULL,
25931 NULL,
25932 &setdebuglist, &showdebuglist);
9291a0cd 25933
cd4fb1b2
SM
25934 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25935Set debugging of the DWARF DIE reader."), _("\
25936Show debugging of the DWARF DIE reader."), _("\
25937When enabled (non-zero), DIEs are dumped after they are read in.\n\
25938The value is the maximum depth to print."),
25939 NULL,
25940 NULL,
25941 &setdebuglist, &showdebuglist);
9291a0cd 25942
cd4fb1b2
SM
25943 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25944Set debugging of the dwarf line reader."), _("\
25945Show debugging of the dwarf line reader."), _("\
25946When enabled (non-zero), line number entries are dumped as they are read in.\n\
25947A value of 1 (one) provides basic information.\n\
25948A value greater than 1 provides more verbose information."),
25949 NULL,
25950 NULL,
25951 &setdebuglist, &showdebuglist);
437afbb8 25952
cd4fb1b2
SM
25953 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25954Set cross-checking of \"physname\" code against demangler."), _("\
25955Show cross-checking of \"physname\" code against demangler."), _("\
25956When enabled, GDB's internal \"physname\" code is checked against\n\
25957the demangler."),
25958 NULL, show_check_physname,
25959 &setdebuglist, &showdebuglist);
900e11f9 25960
e615022a
DE
25961 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25962 no_class, &use_deprecated_index_sections, _("\
25963Set whether to use deprecated gdb_index sections."), _("\
25964Show whether to use deprecated gdb_index sections."), _("\
25965When enabled, deprecated .gdb_index sections are used anyway.\n\
25966Normally they are ignored either because of a missing feature or\n\
25967performance issue.\n\
25968Warning: This option must be enabled before gdb reads the file."),
25969 NULL,
25970 NULL,
25971 &setlist, &showlist);
25972
f1e6e072
TT
25973 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25974 &dwarf2_locexpr_funcs);
25975 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25976 &dwarf2_loclist_funcs);
25977
25978 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25979 &dwarf2_block_frame_base_locexpr_funcs);
25980 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25981 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25982
25983#if GDB_SELF_TEST
25984 selftests::register_test ("dw2_expand_symtabs_matching",
25985 selftests::dw2_expand_symtabs_matching::run_test);
25986#endif
6502dd73 25987}
This page took 4.851812 seconds and 4 git commands to generate.